* opt-functions.awk (var_type): New function.
[official-gcc.git] / gcc / java / parse.y
blob388062e625a348d34239a098aebd0135671ed6a1
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
36 The following modifications were brought to the original grammar:
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "pretty-print.h"
61 #include "diagnostic.h"
62 #include "flags.h"
63 #include "java-tree.h"
64 #include "jcf.h"
65 #include "lex.h"
66 #include "parse.h"
67 #include "zipfile.h"
68 #include "convert.h"
69 #include "buffer.h"
70 #include "function.h"
71 #include "except.h"
72 #include "ggc.h"
73 #include "debug.h"
74 #include "tree-inline.h"
75 #include "cgraph.h"
76 #include "target.h"
78 /* Local function prototypes */
79 static char *java_accstring_lookup (int);
80 static const char *accessibility_string (int);
81 static void classitf_redefinition_error (const char *,tree, tree, tree);
82 static void variable_redefinition_error (tree, tree, tree, int);
83 static tree create_class (int, tree, tree, tree);
84 static tree create_interface (int, tree, tree);
85 static void end_class_declaration (int);
86 static tree find_field (tree, tree);
87 static tree lookup_field_wrapper (tree, tree);
88 static int duplicate_declaration_error_p (tree, tree, tree);
89 static void register_fields (int, tree, tree);
90 static tree parser_qualified_classname (tree);
91 static int parser_check_super (tree, tree, tree);
92 static int parser_check_super_interface (tree, tree, tree);
93 static void check_modifiers_consistency (int);
94 static tree lookup_cl (tree);
95 static tree lookup_java_method2 (tree, tree, int);
96 static tree method_header (int, tree, tree, tree);
97 static void fix_method_argument_names (tree ,tree);
98 static tree method_declarator (tree, tree);
99 static void parse_warning_context (tree cl, const char *msgid, ...);
100 #ifdef USE_MAPPED_LOCATION
101 static void issue_warning_error_from_context
102 (source_location, const char *msgid, va_list *);
103 #else
104 static void issue_warning_error_from_context
105 (tree, const char *msgid, va_list *);
106 #endif
107 static void parse_ctor_invocation_error (void);
108 static tree parse_jdk1_1_error (const char *);
109 static void complete_class_report_errors (jdep *);
110 static int process_imports (void);
111 static void read_import_dir (tree);
112 static int find_in_imports_on_demand (tree, tree);
113 static void find_in_imports (tree, tree);
114 static void check_inner_class_access (tree, tree, tree);
115 static int check_pkg_class_access (tree, tree, bool, tree);
116 static 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);
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_outer_field_access (tree, tree);
324 static tree build_outer_field_access_methods (tree);
325 static tree build_outer_field_access_expr (int, tree, tree,
326 tree, tree);
327 static tree build_outer_method_access_method (tree);
328 static tree build_new_access_id (void);
329 static tree build_outer_field_access_method (tree, tree, tree,
330 tree, tree);
332 static int outer_field_access_p (tree, tree);
333 static int outer_field_expanded_access_p (tree, tree *,
334 tree *, tree *);
335 static tree outer_field_access_fix (tree, tree, tree);
336 static tree build_incomplete_class_ref (int, tree);
337 static tree patch_incomplete_class_ref (tree);
338 static tree create_anonymous_class (tree);
339 static void patch_anonymous_class (tree, tree, tree);
340 static void add_inner_class_fields (tree, tree);
342 static tree build_dot_class_method (tree);
343 static tree build_dot_class_method_invocation (tree, tree);
344 static void create_new_parser_context (int);
345 static tree maybe_build_class_init_for_field (tree, tree);
347 static int emit_test_initialization (void **, void *);
349 static char *string_convert_int_cst (tree);
351 /* Number of error found so far. */
352 int java_error_count;
353 /* Number of warning found so far. */
354 int java_warning_count;
355 /* Cyclic inheritance report, as it can be set by layout_class */
356 const char *cyclic_inheritance_report;
358 /* The current parser context */
359 struct parser_ctxt *ctxp;
361 /* List of things that were analyzed for which code will be generated */
362 struct parser_ctxt *ctxp_for_generation = NULL;
363 struct parser_ctxt *ctxp_for_generation_last = NULL;
365 /* binop_lookup maps token to tree_code. It is used where binary
366 operations are involved and required by the parser. RDIV_EXPR
367 covers both integral/floating point division. The code is changed
368 once the type of both operator is worked out. */
370 static const enum tree_code binop_lookup[19] =
372 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
373 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
374 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
375 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
376 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
378 #define BINOP_LOOKUP(VALUE) \
379 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
381 /* This is the end index for binary operators that can also be used
382 in compound assignments. */
383 #define BINOP_COMPOUND_CANDIDATES 11
385 /* The "$L" identifier we use to create labels. */
386 static GTY(()) tree label_id;
388 /* The "StringBuffer" identifier used for the String `+' operator. */
389 static GTY(()) tree wfl_string_buffer;
391 /* The "append" identifier used for String `+' operator. */
392 static GTY(()) tree wfl_append;
394 /* The "toString" identifier used for String `+' operator. */
395 static GTY(()) tree wfl_to_string;
397 /* The "java.lang" import qualified name. */
398 static GTY(()) tree java_lang_id;
400 /* The generated `inst$' identifier used for generated enclosing
401 instance/field access functions. */
402 static GTY(()) tree inst_id;
404 /* Context and flag for static blocks */
405 static GTY(()) tree current_static_block;
407 /* The generated `write_parm_value$' identifier. */
408 static GTY(()) tree wpv_id;
410 /* Hold THIS for the scope of the current method decl. */
411 static GTY(()) tree current_this;
413 /* Hold a list of catch clauses list. The first element of this list is
414 the list of the catch clauses of the currently analyzed try block. */
415 static GTY(()) tree currently_caught_type_list;
417 /* This holds a linked list of all the case labels for the current
418 switch statement. It is only used when checking to see if there
419 are duplicate labels. FIXME: probably this should just be attached
420 to the switch itself; then it could be referenced via
421 `ctxp->current_loop'. */
422 static GTY(()) tree case_label_list;
424 /* Anonymous class counter. Will be reset to 1 every time a non
425 anonymous class gets created. */
426 static int anonymous_class_counter = 1;
428 static GTY(()) tree src_parse_roots[1];
430 /* All classes seen from source code */
431 #define gclass_list src_parse_roots[0]
433 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
434 line and point it out. */
435 /* Should point out the one that don't fit. ASCII/unicode, going
436 backward. FIXME */
438 #define check_modifiers(__message, __value, __mask) do { \
439 if ((__value) & ~(__mask)) \
441 size_t i, remainder = (__value) & ~(__mask); \
442 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
443 if ((1 << i) & remainder) \
444 parse_error_context (ctxp->modifier_ctx [i], (__message), \
445 java_accstring_lookup (1 << i)); \
447 } while (0)
451 %union {
452 tree node;
453 int sub_token;
454 struct {
455 int token;
456 #ifdef USE_MAPPED_LOCATION
457 source_location location;
458 #else
459 int location;
460 #endif
461 } operator;
462 int value;
466 #ifdef USE_MAPPED_LOCATION
467 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
468 SET_EXPR_LOCATION(EXPR, (TOKEN).location)
469 #else
470 #define SET_EXPR_LOCATION_FROM_TOKEN(EXPR, TOKEN) \
471 (EXPR_WFL_LINECOL (EXPR) = (TOKEN).location)
472 #endif
474 #include "lex.c"
477 %pure_parser
479 /* Things defined here have to match the order of what's in the
480 binop_lookup table. */
482 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
483 %token LS_TK SRS_TK ZRS_TK
484 %token AND_TK XOR_TK OR_TK
485 %token BOOL_AND_TK BOOL_OR_TK
486 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
488 /* This maps to the same binop_lookup entry than the token above */
490 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
491 %token REM_ASSIGN_TK
492 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
493 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
496 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
498 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
499 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
500 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
501 %token PAD_TK ABSTRACT_TK STRICT_TK
502 %token MODIFIER_TK
504 /* Keep those two in order, too */
505 %token DECR_TK INCR_TK
507 /* From now one, things can be in any order */
509 %token DEFAULT_TK IF_TK THROW_TK
510 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
511 %token THROWS_TK BREAK_TK IMPORT_TK
512 %token ELSE_TK INSTANCEOF_TK RETURN_TK
513 %token VOID_TK CATCH_TK INTERFACE_TK
514 %token CASE_TK EXTENDS_TK FINALLY_TK
515 %token SUPER_TK WHILE_TK CLASS_TK
516 %token SWITCH_TK CONST_TK TRY_TK
517 %token FOR_TK NEW_TK CONTINUE_TK
518 %token GOTO_TK PACKAGE_TK THIS_TK
519 %token ASSERT_TK
521 %token BYTE_TK SHORT_TK INT_TK LONG_TK
522 %token CHAR_TK INTEGRAL_TK
524 %token FLOAT_TK DOUBLE_TK FP_TK
526 %token ID_TK
528 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
530 %token ASSIGN_ANY_TK ASSIGN_TK
531 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
533 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
534 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
536 %type <value> modifiers MODIFIER_TK final synchronized
538 %type <node> super ID_TK identifier
539 %type <node> name simple_name qualified_name
540 %type <node> type_declaration compilation_unit
541 field_declaration method_declaration extends_interfaces
542 interfaces interface_type_list
543 import_declarations package_declaration
544 type_declarations interface_body
545 interface_member_declaration constant_declaration
546 interface_member_declarations interface_type
547 abstract_method_declaration
548 %type <node> class_body_declaration class_member_declaration
549 static_initializer constructor_declaration block
550 %type <node> class_body_declarations constructor_header
551 %type <node> class_or_interface_type class_type class_type_list
552 constructor_declarator explicit_constructor_invocation
553 %type <node> dim_expr dim_exprs this_or_super throws
555 %type <node> variable_declarator_id variable_declarator
556 variable_declarators variable_initializer
557 variable_initializers constructor_body
558 array_initializer
560 %type <node> class_body block_end constructor_block_end
561 %type <node> statement statement_without_trailing_substatement
562 labeled_statement if_then_statement label_decl
563 if_then_else_statement while_statement for_statement
564 statement_nsi labeled_statement_nsi do_statement
565 if_then_else_statement_nsi while_statement_nsi
566 for_statement_nsi statement_expression_list for_init
567 for_update statement_expression expression_statement
568 primary_no_new_array expression primary array_type
569 array_creation_initialized array_creation_uninitialized
570 class_instance_creation_expression field_access
571 method_invocation array_access something_dot_new
572 argument_list postfix_expression while_expression
573 post_increment_expression post_decrement_expression
574 unary_expression_not_plus_minus unary_expression
575 pre_increment_expression pre_decrement_expression
576 cast_expression
577 multiplicative_expression additive_expression
578 shift_expression relational_expression
579 equality_expression and_expression
580 exclusive_or_expression inclusive_or_expression
581 conditional_and_expression conditional_or_expression
582 conditional_expression assignment_expression
583 left_hand_side assignment for_header for_begin
584 constant_expression do_statement_begin empty_statement
585 switch_statement synchronized_statement throw_statement
586 try_statement assert_statement
587 switch_expression switch_block
588 catches catch_clause catch_clause_parameter finally
589 anonymous_class_creation trap_overflow_corner_case
590 %type <node> return_statement break_statement continue_statement
592 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
593 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
594 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
595 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
596 %type <operator> ASSIGN_ANY_TK assignment_operator
597 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
598 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
599 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
600 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
601 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
602 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
603 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
604 %type <operator> NEW_TK ASSERT_TK
606 %type <node> method_body
608 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
609 STRING_LIT_TK NULL_TK VOID_TK
611 %type <node> IF_TK WHILE_TK FOR_TK
613 %type <node> formal_parameter_list formal_parameter
614 method_declarator method_header
616 %type <node> primitive_type reference_type type
617 BOOLEAN_TK INTEGRAL_TK FP_TK
619 /* Added or modified JDK 1.1 rule types */
620 %type <node> type_literals
623 /* 19.2 Production from 2.3: The Syntactic Grammar */
624 goal: compilation_unit
628 /* 19.3 Productions from 3: Lexical structure */
629 literal:
630 INT_LIT_TK
631 | FP_LIT_TK
632 | BOOL_LIT_TK
633 | CHAR_LIT_TK
634 | STRING_LIT_TK
635 | NULL_TK
638 /* 19.4 Productions from 4: Types, Values and Variables */
639 type:
640 primitive_type
641 | reference_type
644 primitive_type:
645 INTEGRAL_TK
646 | FP_TK
647 | BOOLEAN_TK
650 reference_type:
651 class_or_interface_type
652 | array_type
655 class_or_interface_type:
656 name
659 class_type:
660 class_or_interface_type /* Default rule */
663 interface_type:
664 class_or_interface_type
667 array_type:
668 primitive_type dims
670 int osb = pop_current_osb (ctxp);
671 tree t = build_java_array_type (($1), -1);
672 while (--osb)
673 t = build_unresolved_array_type (t);
674 $$ = t;
676 | name dims
678 int osb = pop_current_osb (ctxp);
679 tree t = $1;
680 while (osb--)
681 t = build_unresolved_array_type (t);
682 $$ = t;
686 /* 19.5 Productions from 6: Names */
687 name:
688 simple_name /* Default rule */
689 | qualified_name /* Default rule */
692 simple_name:
693 identifier /* Default rule */
696 qualified_name:
697 name DOT_TK identifier
698 { $$ = make_qualified_name ($1, $3, $2.location); }
701 identifier:
702 ID_TK
705 /* 19.6: Production from 7: Packages */
706 compilation_unit:
707 {$$ = NULL;}
708 | package_declaration
709 | import_declarations
710 | type_declarations
711 | package_declaration import_declarations
712 | package_declaration type_declarations
713 | import_declarations type_declarations
714 | package_declaration import_declarations type_declarations
717 import_declarations:
718 import_declaration
720 $$ = NULL;
722 | import_declarations import_declaration
724 $$ = NULL;
728 type_declarations:
729 type_declaration
730 | type_declarations type_declaration
733 package_declaration:
734 PACKAGE_TK name SC_TK
736 ctxp->package = EXPR_WFL_NODE ($2);
738 | PACKAGE_TK error
739 {yyerror ("Missing name"); RECOVER;}
740 | PACKAGE_TK name error
741 {yyerror ("';' expected"); RECOVER;}
744 import_declaration:
745 single_type_import_declaration
746 | type_import_on_demand_declaration
749 single_type_import_declaration:
750 IMPORT_TK name SC_TK
752 tree name = EXPR_WFL_NODE ($2), last_name;
753 int i = IDENTIFIER_LENGTH (name)-1;
754 const char *last = &IDENTIFIER_POINTER (name)[i];
755 while (last != IDENTIFIER_POINTER (name))
757 if (last [0] == '.')
758 break;
759 last--;
761 last_name = get_identifier (++last);
762 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
764 tree err = find_name_in_single_imports (last_name);
765 if (err && err != name)
766 parse_error_context
767 ($2, "Ambiguous class: %qs and %qs",
768 IDENTIFIER_POINTER (name),
769 IDENTIFIER_POINTER (err));
770 else
771 REGISTER_IMPORT ($2, last_name);
773 else
774 REGISTER_IMPORT ($2, last_name);
776 | IMPORT_TK error
777 {yyerror ("Missing name"); RECOVER;}
778 | IMPORT_TK name error
779 {yyerror ("';' expected"); RECOVER;}
782 type_import_on_demand_declaration:
783 IMPORT_TK name DOT_TK MULT_TK SC_TK
785 tree name = EXPR_WFL_NODE ($2);
786 tree it;
787 /* Search for duplicates. */
788 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
789 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
790 break;
791 /* Don't import the same thing more than once, just ignore
792 duplicates (7.5.2) */
793 if (! it)
795 read_import_dir ($2);
796 ctxp->import_demand_list =
797 chainon (ctxp->import_demand_list,
798 build_tree_list ($2, NULL_TREE));
801 | IMPORT_TK name DOT_TK error
802 {yyerror ("'*' expected"); RECOVER;}
803 | IMPORT_TK name DOT_TK MULT_TK error
804 {yyerror ("';' expected"); RECOVER;}
807 type_declaration:
808 class_declaration
809 { end_class_declaration (0); }
810 | interface_declaration
811 { end_class_declaration (0); }
812 | empty_statement
813 | error
815 YYERROR_NOW;
816 yyerror ("Class or interface declaration expected");
820 /* 19.7 Shortened from the original:
821 modifiers: modifier | modifiers modifier
822 modifier: any of public... */
823 modifiers:
824 MODIFIER_TK
826 $$ = (1 << $1);
828 | modifiers MODIFIER_TK
830 int acc = (1 << $2);
831 if ($$ & acc)
832 parse_error_context
833 (ctxp->modifier_ctx [$2], "Modifier %qs declared twice",
834 java_accstring_lookup (acc));
835 else
837 $$ |= acc;
842 /* 19.8.1 Production from $8.1: Class Declaration */
843 class_declaration:
844 modifiers CLASS_TK identifier super interfaces
845 { create_class ($1, $3, $4, $5); }
846 class_body
848 | CLASS_TK identifier super interfaces
849 { create_class (0, $2, $3, $4); }
850 class_body
852 | modifiers CLASS_TK error
853 { yyerror ("Missing class name"); RECOVER; }
854 | CLASS_TK error
855 { yyerror ("Missing class name"); RECOVER; }
856 | CLASS_TK identifier error
858 if (!ctxp->class_err) yyerror ("'{' expected");
859 DRECOVER(class1);
861 | modifiers CLASS_TK identifier error
862 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
865 super:
866 { $$ = NULL; }
867 | EXTENDS_TK class_type
868 { $$ = $2; }
869 | EXTENDS_TK class_type error
870 {yyerror ("'{' expected"); ctxp->class_err=1;}
871 | EXTENDS_TK error
872 {yyerror ("Missing super class name"); ctxp->class_err=1;}
875 interfaces:
876 { $$ = NULL_TREE; }
877 | IMPLEMENTS_TK interface_type_list
878 { $$ = $2; }
879 | IMPLEMENTS_TK error
881 ctxp->class_err=1;
882 yyerror ("Missing interface name");
886 interface_type_list:
887 interface_type
889 ctxp->interface_number = 1;
890 $$ = build_tree_list ($1, NULL_TREE);
892 | interface_type_list C_TK interface_type
894 ctxp->interface_number++;
895 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
897 | interface_type_list C_TK error
898 {yyerror ("Missing interface name"); RECOVER;}
901 class_body:
902 OCB_TK CCB_TK
904 $$ = GET_CPC ();
906 | OCB_TK class_body_declarations CCB_TK
908 $$ = GET_CPC ();
912 class_body_declarations:
913 class_body_declaration
914 | class_body_declarations class_body_declaration
917 class_body_declaration:
918 class_member_declaration
919 | static_initializer
920 | constructor_declaration
921 | block /* Added, JDK1.1, instance initializer */
923 if (!IS_EMPTY_STMT ($1))
925 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
926 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
931 class_member_declaration:
932 field_declaration
933 | method_declaration
934 | class_declaration /* Added, JDK1.1 inner classes */
935 { end_class_declaration (1); }
936 | interface_declaration /* Added, JDK1.1 inner interfaces */
937 { end_class_declaration (1); }
938 | empty_statement
941 /* 19.8.2 Productions from 8.3: Field Declarations */
942 field_declaration:
943 type variable_declarators SC_TK
944 { register_fields (0, $1, $2); }
945 | modifiers type variable_declarators SC_TK
947 check_modifiers
948 ("Illegal modifier %qs for field declaration",
949 $1, FIELD_MODIFIERS);
950 check_modifiers_consistency ($1);
951 register_fields ($1, $2, $3);
955 variable_declarators:
956 /* Should we use build_decl_list () instead ? FIXME */
957 variable_declarator /* Default rule */
958 | variable_declarators C_TK variable_declarator
959 { $$ = chainon ($1, $3); }
960 | variable_declarators C_TK error
961 {yyerror ("Missing term"); RECOVER;}
964 variable_declarator:
965 variable_declarator_id
966 { $$ = build_tree_list ($1, NULL_TREE); }
967 | variable_declarator_id ASSIGN_TK variable_initializer
969 if (java_error_count)
970 $3 = NULL_TREE;
971 $$ = build_tree_list
972 ($1, build_assignment ($2.token, $2.location, $1, $3));
974 | variable_declarator_id ASSIGN_TK error
976 yyerror ("Missing variable initializer");
977 $$ = build_tree_list ($1, NULL_TREE);
978 RECOVER;
980 | variable_declarator_id ASSIGN_TK variable_initializer error
982 yyerror ("';' expected");
983 $$ = build_tree_list ($1, NULL_TREE);
984 RECOVER;
988 variable_declarator_id:
989 identifier
990 | variable_declarator_id OSB_TK CSB_TK
991 { $$ = build_unresolved_array_type ($1); }
992 | identifier error
993 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
994 | variable_declarator_id OSB_TK error
996 yyerror ("']' expected");
997 DRECOVER(vdi);
999 | variable_declarator_id CSB_TK error
1000 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1003 variable_initializer:
1004 expression
1005 | array_initializer
1008 /* 19.8.3 Productions from 8.4: Method Declarations */
1009 method_declaration:
1010 method_header
1012 current_function_decl = $1;
1013 if (current_function_decl
1014 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015 source_start_java_method (current_function_decl);
1016 else
1017 current_function_decl = NULL_TREE;
1019 method_body
1020 { finish_method_declaration ($3); }
1021 | method_header error
1022 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1025 method_header:
1026 type method_declarator throws
1027 { $$ = method_header (0, $1, $2, $3); }
1028 | VOID_TK method_declarator throws
1029 { $$ = method_header (0, void_type_node, $2, $3); }
1030 | modifiers type method_declarator throws
1031 { $$ = method_header ($1, $2, $3, $4); }
1032 | modifiers VOID_TK method_declarator throws
1033 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 | type error
1036 yyerror ("Invalid method declaration, method name required");
1037 RECOVER;
1039 | modifiers type error
1041 yyerror ("Identifier expected");
1042 RECOVER;
1044 | VOID_TK error
1046 yyerror ("Identifier expected");
1047 RECOVER;
1049 | modifiers VOID_TK error
1051 yyerror ("Identifier expected");
1052 RECOVER;
1054 | modifiers error
1056 yyerror ("Invalid method declaration, return type required");
1057 RECOVER;
1061 method_declarator:
1062 identifier OP_TK CP_TK
1064 ctxp->formal_parameter_number = 0;
1065 $$ = method_declarator ($1, NULL_TREE);
1067 | identifier OP_TK formal_parameter_list CP_TK
1068 { $$ = method_declarator ($1, $3); }
1069 | method_declarator OSB_TK CSB_TK
1071 SET_EXPR_LOCATION_FROM_TOKEN (wfl_operator, $2);
1072 TREE_PURPOSE ($1) =
1073 build_unresolved_array_type (TREE_PURPOSE ($1));
1074 parse_warning_context
1075 (wfl_operator,
1076 "Discouraged form of returned type specification");
1078 | identifier OP_TK error
1079 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1080 | method_declarator OSB_TK error
1081 {yyerror ("']' expected"); RECOVER;}
1084 formal_parameter_list:
1085 formal_parameter
1087 ctxp->formal_parameter_number = 1;
1089 | formal_parameter_list C_TK formal_parameter
1091 ctxp->formal_parameter_number += 1;
1092 $$ = chainon ($1, $3);
1094 | formal_parameter_list C_TK error
1095 { yyerror ("Missing formal parameter term"); RECOVER; }
1098 formal_parameter:
1099 type variable_declarator_id
1101 $$ = build_tree_list ($2, $1);
1103 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1105 $$ = build_tree_list ($3, $2);
1106 ARG_FINAL_P ($$) = 1;
1108 | type error
1110 yyerror ("Missing identifier"); RECOVER;
1111 $$ = NULL_TREE;
1113 | final type error
1115 yyerror ("Missing identifier"); RECOVER;
1116 $$ = NULL_TREE;
1120 final:
1121 modifiers
1123 check_modifiers ("Illegal modifier %qs. Only %<final%> was expected here",
1124 $1, ACC_FINAL);
1125 if ($1 != ACC_FINAL)
1126 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1130 throws:
1131 { $$ = NULL_TREE; }
1132 | THROWS_TK class_type_list
1133 { $$ = $2; }
1134 | THROWS_TK error
1135 {yyerror ("Missing class type term"); RECOVER;}
1138 class_type_list:
1139 class_type
1140 { $$ = build_tree_list ($1, $1); }
1141 | class_type_list C_TK class_type
1142 { $$ = tree_cons ($3, $3, $1); }
1143 | class_type_list C_TK error
1144 {yyerror ("Missing class type term"); RECOVER;}
1147 method_body:
1148 block
1149 | SC_TK { $$ = NULL_TREE; }
1152 /* 19.8.4 Productions from 8.5: Static Initializers */
1153 static_initializer:
1154 static block
1156 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1157 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1158 current_static_block = NULL_TREE;
1162 static: /* Test lval.sub_token here */
1163 modifiers
1165 check_modifiers ("Illegal modifier %qs for static initializer", $1, ACC_STATIC);
1166 /* Can't have a static initializer in an innerclass */
1167 if ($1 | ACC_STATIC &&
1168 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1169 parse_error_context
1170 (MODIFIER_WFL (STATIC_TK),
1171 "Can't define static initializer in class %qs. Static initializer can only be defined in top-level classes",
1172 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1173 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1177 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1178 constructor_declaration:
1179 constructor_header
1181 current_function_decl = $1;
1182 source_start_java_method (current_function_decl);
1184 constructor_body
1185 { finish_method_declaration ($3); }
1188 constructor_header:
1189 constructor_declarator throws
1190 { $$ = method_header (0, NULL_TREE, $1, $2); }
1191 | modifiers constructor_declarator throws
1192 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1195 constructor_declarator:
1196 simple_name OP_TK CP_TK
1198 ctxp->formal_parameter_number = 0;
1199 $$ = method_declarator ($1, NULL_TREE);
1201 | simple_name OP_TK formal_parameter_list CP_TK
1202 { $$ = method_declarator ($1, $3); }
1205 constructor_body:
1206 /* Unlike regular method, we always need a complete (empty)
1207 body so we can safely perform all the required code
1208 addition (super invocation and field initialization) */
1209 block_begin constructor_block_end
1211 BLOCK_EXPR_BODY ($2) = build_java_empty_stmt ();
1212 $$ = $2;
1214 | block_begin explicit_constructor_invocation constructor_block_end
1215 { $$ = $3; }
1216 | block_begin block_statements constructor_block_end
1217 { $$ = $3; }
1218 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1219 { $$ = $4; }
1222 constructor_block_end:
1223 block_end
1226 /* Error recovery for that rule moved down expression_statement: rule. */
1227 explicit_constructor_invocation:
1228 this_or_super OP_TK CP_TK SC_TK
1230 $$ = build_method_invocation ($1, NULL_TREE);
1231 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1232 $$ = java_method_add_stmt (current_function_decl, $$);
1234 | this_or_super OP_TK argument_list CP_TK SC_TK
1236 $$ = build_method_invocation ($1, $3);
1237 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1238 $$ = java_method_add_stmt (current_function_decl, $$);
1240 /* Added, JDK1.1 inner classes. Modified because the rule
1241 'primary' couldn't work. */
1242 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1243 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1244 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1245 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1248 this_or_super: /* Added, simplifies error diagnostics */
1249 THIS_TK
1251 tree wfl = build_wfl_node (this_identifier_node);
1252 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1253 $$ = wfl;
1255 | SUPER_TK
1257 tree wfl = build_wfl_node (super_identifier_node);
1258 SET_EXPR_LOCATION_FROM_TOKEN (wfl, $1);
1259 $$ = wfl;
1263 /* 19.9 Productions from 9: Interfaces */
1264 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1265 interface_declaration:
1266 INTERFACE_TK identifier
1267 { create_interface (0, $2, NULL_TREE); }
1268 interface_body
1269 { ; }
1270 | modifiers INTERFACE_TK identifier
1271 { create_interface ($1, $3, NULL_TREE); }
1272 interface_body
1273 { ; }
1274 | INTERFACE_TK identifier extends_interfaces
1275 { create_interface (0, $2, $3); }
1276 interface_body
1277 { ; }
1278 | modifiers INTERFACE_TK identifier extends_interfaces
1279 { create_interface ($1, $3, $4); }
1280 interface_body
1281 { ; }
1282 | INTERFACE_TK identifier error
1283 { yyerror ("'{' expected"); RECOVER; }
1284 | modifiers INTERFACE_TK identifier error
1285 { yyerror ("'{' expected"); RECOVER; }
1288 extends_interfaces:
1289 EXTENDS_TK interface_type
1291 ctxp->interface_number = 1;
1292 $$ = build_tree_list ($2, NULL_TREE);
1294 | extends_interfaces C_TK interface_type
1296 ctxp->interface_number++;
1297 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1299 | EXTENDS_TK error
1300 {yyerror ("Invalid interface type"); RECOVER;}
1301 | extends_interfaces C_TK error
1302 {yyerror ("Missing term"); RECOVER;}
1305 interface_body:
1306 OCB_TK CCB_TK
1307 { $$ = NULL_TREE; }
1308 | OCB_TK interface_member_declarations CCB_TK
1309 { $$ = NULL_TREE; }
1312 interface_member_declarations:
1313 interface_member_declaration
1314 | interface_member_declarations interface_member_declaration
1317 interface_member_declaration:
1318 constant_declaration
1319 | abstract_method_declaration
1320 | class_declaration /* Added, JDK1.1 inner classes */
1321 { end_class_declaration (1); }
1322 | interface_declaration /* Added, JDK1.1 inner interfaces */
1323 { end_class_declaration (1); }
1326 constant_declaration:
1327 field_declaration
1330 abstract_method_declaration:
1331 method_header SC_TK
1333 check_abstract_method_header ($1);
1334 current_function_decl = NULL_TREE; /* FIXME ? */
1336 | method_header error
1337 {yyerror ("';' expected"); RECOVER;}
1340 /* 19.10 Productions from 10: Arrays */
1341 array_initializer:
1342 OCB_TK CCB_TK
1343 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1344 | OCB_TK C_TK CCB_TK
1345 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1346 | OCB_TK variable_initializers CCB_TK
1347 { $$ = build_new_array_init ($1.location, $2); }
1348 | OCB_TK variable_initializers C_TK CCB_TK
1349 { $$ = build_new_array_init ($1.location, $2); }
1352 variable_initializers:
1353 variable_initializer
1355 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1356 $1, NULL_TREE);
1358 | variable_initializers C_TK variable_initializer
1360 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1362 | variable_initializers C_TK error
1363 {yyerror ("Missing term"); RECOVER;}
1366 /* 19.11 Production from 14: Blocks and Statements */
1367 block:
1368 block_begin block_end
1369 { $$ = $2; }
1370 | block_begin block_statements block_end
1371 { $$ = $3; }
1374 block_begin:
1375 OCB_TK
1376 { enter_block (); }
1379 block_end:
1380 CCB_TK
1382 maybe_absorb_scoping_blocks ();
1383 $$ = exit_block ();
1384 if (!BLOCK_SUBBLOCKS ($$))
1385 BLOCK_SUBBLOCKS ($$) = build_java_empty_stmt ();
1389 block_statements:
1390 block_statement
1391 | block_statements block_statement
1394 block_statement:
1395 local_variable_declaration_statement
1396 | statement
1397 { java_method_add_stmt (current_function_decl, $1); }
1398 | class_declaration /* Added, JDK1.1 local classes */
1400 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1401 end_class_declaration (1);
1405 local_variable_declaration_statement:
1406 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1409 local_variable_declaration:
1410 type variable_declarators
1411 { declare_local_variables (0, $1, $2); }
1412 | final type variable_declarators /* Added, JDK1.1 final locals */
1413 { declare_local_variables ($1, $2, $3); }
1416 statement:
1417 statement_without_trailing_substatement
1418 | labeled_statement
1419 | if_then_statement
1420 | if_then_else_statement
1421 | while_statement
1422 | for_statement
1423 { $$ = exit_block (); }
1426 statement_nsi:
1427 statement_without_trailing_substatement
1428 | labeled_statement_nsi
1429 | if_then_else_statement_nsi
1430 | while_statement_nsi
1431 | for_statement_nsi
1432 { $$ = exit_block (); }
1435 statement_without_trailing_substatement:
1436 block
1437 | empty_statement
1438 | expression_statement
1439 | switch_statement
1440 | do_statement
1441 | break_statement
1442 | continue_statement
1443 | return_statement
1444 | synchronized_statement
1445 | throw_statement
1446 | try_statement
1447 | assert_statement
1450 empty_statement:
1451 SC_TK
1453 if (flag_extraneous_semicolon
1454 && ! current_static_block
1455 && (! current_function_decl ||
1456 /* Verify we're not in a inner class declaration */
1457 (GET_CPC () != TYPE_NAME
1458 (DECL_CONTEXT (current_function_decl)))))
1461 #ifdef USE_MAPPED_LOCATION
1462 SET_EXPR_LOCATION (wfl_operator, input_location);
1463 #else
1464 EXPR_WFL_SET_LINECOL (wfl_operator, input_line, -1);
1465 #endif
1466 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1468 $$ = build_java_empty_stmt ();
1472 label_decl:
1473 identifier REL_CL_TK
1475 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1476 EXPR_WFL_NODE ($1));
1477 pushlevel (2);
1478 push_labeled_block ($$);
1479 PUSH_LABELED_BLOCK ($$);
1483 labeled_statement:
1484 label_decl statement
1485 { $$ = finish_labeled_statement ($1, $2); }
1486 | identifier error
1487 {yyerror ("':' expected"); RECOVER;}
1490 labeled_statement_nsi:
1491 label_decl statement_nsi
1492 { $$ = finish_labeled_statement ($1, $2); }
1495 /* We concentrate here a bunch of error handling rules that we couldn't write
1496 earlier, because expression_statement catches a missing ';'. */
1497 expression_statement:
1498 statement_expression SC_TK
1500 /* We have a statement. Generate a WFL around it so
1501 we can debug it */
1502 #ifdef USE_MAPPED_LOCATION
1503 $$ = expr_add_location ($1, input_location, 1);
1504 #else
1505 $$ = build_expr_wfl ($1, input_filename, input_line, 0);
1506 JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1507 #endif
1508 /* We know we have a statement, so set the debug
1509 info to be eventually generate here. */
1511 | error SC_TK
1513 YYNOT_TWICE yyerror ("Invalid expression statement");
1514 DRECOVER (expr_stmt);
1516 | error OCB_TK
1518 YYNOT_TWICE yyerror ("Invalid expression statement");
1519 DRECOVER (expr_stmt);
1521 | error CCB_TK
1523 YYNOT_TWICE yyerror ("Invalid expression statement");
1524 DRECOVER (expr_stmt);
1526 | this_or_super OP_TK error
1527 {yyerror ("')' expected"); RECOVER;}
1528 | this_or_super OP_TK CP_TK error
1530 parse_ctor_invocation_error ();
1531 RECOVER;
1533 | this_or_super OP_TK argument_list error
1534 {yyerror ("')' expected"); RECOVER;}
1535 | this_or_super OP_TK argument_list CP_TK error
1537 parse_ctor_invocation_error ();
1538 RECOVER;
1540 | name DOT_TK SUPER_TK error
1541 {yyerror ("'(' expected"); RECOVER;}
1542 | name DOT_TK SUPER_TK OP_TK error
1543 {yyerror ("')' expected"); RECOVER;}
1544 | name DOT_TK SUPER_TK OP_TK argument_list error
1545 {yyerror ("')' expected"); RECOVER;}
1546 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1547 {yyerror ("';' expected"); RECOVER;}
1548 | name DOT_TK SUPER_TK OP_TK CP_TK error
1549 {yyerror ("';' expected"); RECOVER;}
1552 statement_expression:
1553 assignment
1554 | pre_increment_expression
1555 | pre_decrement_expression
1556 | post_increment_expression
1557 | post_decrement_expression
1558 | method_invocation
1559 | class_instance_creation_expression
1562 if_then_statement:
1563 IF_TK OP_TK expression CP_TK statement
1565 $$ = build_if_else_statement ($2.location, $3,
1566 $5, NULL_TREE);
1568 | IF_TK error
1569 {yyerror ("'(' expected"); RECOVER;}
1570 | IF_TK OP_TK error
1571 {yyerror ("Missing term"); RECOVER;}
1572 | IF_TK OP_TK expression error
1573 {yyerror ("')' expected"); RECOVER;}
1576 if_then_else_statement:
1577 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1578 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1581 if_then_else_statement_nsi:
1582 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1583 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1586 switch_statement:
1587 switch_expression
1589 enter_block ();
1591 switch_block
1593 /* Make into "proper list" of COMPOUND_EXPRs.
1594 I.e. make the last statement also have its own
1595 COMPOUND_EXPR. */
1596 maybe_absorb_scoping_blocks ();
1597 TREE_OPERAND ($1, 1) = exit_block ();
1598 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1602 switch_expression:
1603 SWITCH_TK OP_TK expression CP_TK
1605 $$ = build3 (SWITCH_EXPR, NULL_TREE, $3,
1606 NULL_TREE, NULL_TREE);
1607 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
1609 | SWITCH_TK error
1610 {yyerror ("'(' expected"); RECOVER;}
1611 | SWITCH_TK OP_TK error
1612 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1613 | SWITCH_TK OP_TK expression CP_TK error
1614 {yyerror ("'{' expected"); RECOVER;}
1617 /* Default assignment is there to avoid type node on switch_block
1618 node. */
1620 switch_block:
1621 OCB_TK CCB_TK
1622 { $$ = NULL_TREE; }
1623 | OCB_TK switch_labels CCB_TK
1624 { $$ = NULL_TREE; }
1625 | OCB_TK switch_block_statement_groups CCB_TK
1626 { $$ = NULL_TREE; }
1627 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1628 { $$ = NULL_TREE; }
1631 switch_block_statement_groups:
1632 switch_block_statement_group
1633 | switch_block_statement_groups switch_block_statement_group
1636 switch_block_statement_group:
1637 switch_labels block_statements
1640 switch_labels:
1641 switch_label
1642 | switch_labels switch_label
1645 switch_label:
1646 CASE_TK constant_expression REL_CL_TK
1648 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1649 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1650 java_method_add_stmt (current_function_decl, lab);
1652 | DEFAULT_TK REL_CL_TK
1654 tree lab = make_node (DEFAULT_EXPR);
1655 SET_EXPR_LOCATION_FROM_TOKEN (lab, $1);
1656 java_method_add_stmt (current_function_decl, lab);
1658 | CASE_TK error
1659 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1660 | CASE_TK constant_expression error
1661 {yyerror ("':' expected"); RECOVER;}
1662 | DEFAULT_TK error
1663 {yyerror ("':' expected"); RECOVER;}
1666 while_expression:
1667 WHILE_TK OP_TK expression CP_TK
1669 tree body = build_loop_body ($2.location, $3, 0);
1670 $$ = build_new_loop (body);
1674 while_statement:
1675 while_expression statement
1676 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1677 | WHILE_TK error
1678 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1679 | WHILE_TK OP_TK error
1680 {yyerror ("Missing term and ')' expected"); RECOVER;}
1681 | WHILE_TK OP_TK expression error
1682 {yyerror ("')' expected"); RECOVER;}
1685 while_statement_nsi:
1686 while_expression statement_nsi
1687 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1690 do_statement_begin:
1691 DO_TK
1693 tree body = build_loop_body (0, NULL_TREE, 1);
1694 $$ = build_new_loop (body);
1696 /* Need error handing here. FIXME */
1699 do_statement:
1700 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1701 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1704 for_statement:
1705 for_begin SC_TK expression SC_TK for_update CP_TK statement
1707 if (CONSTANT_CLASS_P ($3))
1708 $3 = build_wfl_node ($3);
1709 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1711 | for_begin SC_TK SC_TK for_update CP_TK statement
1713 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1714 /* We have not condition, so we get rid of the EXIT_EXPR */
1715 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1716 build_java_empty_stmt ();
1718 | for_begin SC_TK error
1719 {yyerror ("Invalid control expression"); RECOVER;}
1720 | for_begin SC_TK expression SC_TK error
1721 {yyerror ("Invalid update expression"); RECOVER;}
1722 | for_begin SC_TK SC_TK error
1723 {yyerror ("Invalid update expression"); RECOVER;}
1726 for_statement_nsi:
1727 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1728 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1729 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1731 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1732 /* We have not condition, so we get rid of the EXIT_EXPR */
1733 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1734 build_java_empty_stmt ();
1738 for_header:
1739 FOR_TK OP_TK
1741 /* This scope defined for local variable that may be
1742 defined within the scope of the for loop */
1743 enter_block ();
1745 | FOR_TK error
1746 {yyerror ("'(' expected"); DRECOVER(for_1);}
1747 | FOR_TK OP_TK error
1748 {yyerror ("Invalid init statement"); RECOVER;}
1751 for_begin:
1752 for_header for_init
1754 /* We now declare the loop body. The loop is
1755 declared as a for loop. */
1756 tree body = build_loop_body (0, NULL_TREE, 0);
1757 $$ = build_new_loop (body);
1758 FOR_LOOP_P ($$) = 1;
1759 /* The loop is added to the current block the for
1760 statement is defined within */
1761 java_method_add_stmt (current_function_decl, $$);
1764 for_init: /* Can be empty */
1765 { $$ = build_java_empty_stmt (); }
1766 | statement_expression_list
1768 /* Init statement recorded within the previously
1769 defined block scope */
1770 $$ = java_method_add_stmt (current_function_decl, $1);
1772 | local_variable_declaration
1774 /* Local variable are recorded within the previously
1775 defined block scope */
1776 $$ = NULL_TREE;
1778 | statement_expression_list error
1779 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1782 for_update: /* Can be empty */
1783 {$$ = build_java_empty_stmt ();}
1784 | statement_expression_list
1785 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1788 statement_expression_list:
1789 statement_expression
1790 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1791 | statement_expression_list C_TK statement_expression
1792 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1793 | statement_expression_list C_TK error
1794 {yyerror ("Missing term"); RECOVER;}
1797 break_statement:
1798 BREAK_TK SC_TK
1799 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1800 | BREAK_TK identifier SC_TK
1801 { $$ = build_bc_statement ($1.location, 1, $2); }
1802 | BREAK_TK error
1803 {yyerror ("Missing term"); RECOVER;}
1804 | BREAK_TK identifier error
1805 {yyerror ("';' expected"); RECOVER;}
1808 continue_statement:
1809 CONTINUE_TK SC_TK
1810 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1811 | CONTINUE_TK identifier SC_TK
1812 { $$ = build_bc_statement ($1.location, 0, $2); }
1813 | CONTINUE_TK error
1814 {yyerror ("Missing term"); RECOVER;}
1815 | CONTINUE_TK identifier error
1816 {yyerror ("';' expected"); RECOVER;}
1819 return_statement:
1820 RETURN_TK SC_TK
1821 { $$ = build_return ($1.location, NULL_TREE); }
1822 | RETURN_TK expression SC_TK
1823 { $$ = build_return ($1.location, $2); }
1824 | RETURN_TK error
1825 {yyerror ("Missing term"); RECOVER;}
1826 | RETURN_TK expression error
1827 {yyerror ("';' expected"); RECOVER;}
1830 throw_statement:
1831 THROW_TK expression SC_TK
1833 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1834 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1836 | THROW_TK error
1837 {yyerror ("Missing term"); RECOVER;}
1838 | THROW_TK expression error
1839 {yyerror ("';' expected"); RECOVER;}
1842 assert_statement:
1843 ASSERT_TK expression REL_CL_TK expression SC_TK
1845 $$ = build_assertion ($1.location, $2, $4);
1847 | ASSERT_TK expression SC_TK
1849 $$ = build_assertion ($1.location, $2, NULL_TREE);
1851 | ASSERT_TK error
1852 {yyerror ("Missing term"); RECOVER;}
1853 | ASSERT_TK expression error
1854 {yyerror ("';' expected"); RECOVER;}
1857 synchronized_statement:
1858 synchronized OP_TK expression CP_TK block
1860 $$ = build2 (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1861 EXPR_WFL_LINECOL ($$) =
1862 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1864 | synchronized OP_TK expression CP_TK error
1865 {yyerror ("'{' expected"); RECOVER;}
1866 | synchronized error
1867 {yyerror ("'(' expected"); RECOVER;}
1868 | synchronized OP_TK error CP_TK
1869 {yyerror ("Missing term"); RECOVER;}
1870 | synchronized OP_TK error
1871 {yyerror ("Missing term"); RECOVER;}
1874 synchronized:
1875 modifiers
1877 check_modifiers (
1878 "Illegal modifier %qs. Only %<synchronized%> was expected here",
1879 $1, ACC_SYNCHRONIZED);
1880 if ($1 != ACC_SYNCHRONIZED)
1881 MODIFIER_WFL (SYNCHRONIZED_TK) =
1882 build_wfl_node (NULL_TREE);
1886 try_statement:
1887 TRY_TK block catches
1888 { $$ = build_try_statement ($1.location, $2, $3); }
1889 | TRY_TK block finally
1890 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1891 | TRY_TK block catches finally
1892 { $$ = build_try_finally_statement
1893 ($1.location, build_try_statement ($1.location,
1894 $2, $3), $4);
1896 | TRY_TK error
1897 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1900 catches:
1901 catch_clause
1902 | catches catch_clause
1904 TREE_CHAIN ($2) = $1;
1905 $$ = $2;
1909 catch_clause:
1910 catch_clause_parameter block
1912 java_method_add_stmt (current_function_decl, $2);
1913 exit_block ();
1914 $$ = $1;
1918 catch_clause_parameter:
1919 CATCH_TK OP_TK formal_parameter CP_TK
1921 /* We add a block to define a scope for
1922 formal_parameter (CCBP). The formal parameter is
1923 declared initialized by the appropriate function
1924 call */
1925 tree ccpb;
1926 tree init;
1927 if ($3)
1929 ccpb = enter_block ();
1930 init = build_assignment
1931 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1932 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1933 declare_local_variables (0, TREE_VALUE ($3),
1934 build_tree_list
1935 (TREE_PURPOSE ($3), init));
1936 $$ = build1 (JAVA_CATCH_EXPR, NULL_TREE, ccpb);
1937 SET_EXPR_LOCATION_FROM_TOKEN ($$, $1);
1939 else
1941 $$ = error_mark_node;
1944 | CATCH_TK error
1945 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1946 | CATCH_TK OP_TK error
1948 yyerror ("Missing term or ')' expected");
1949 RECOVER; $$ = NULL_TREE;
1951 | CATCH_TK OP_TK error CP_TK /* That's for () */
1952 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1955 finally:
1956 FINALLY_TK block
1957 { $$ = $2; }
1958 | FINALLY_TK error
1959 {yyerror ("'{' expected"); RECOVER; }
1962 /* 19.12 Production from 15: Expressions */
1963 primary:
1964 primary_no_new_array
1965 | array_creation_uninitialized
1966 | array_creation_initialized
1969 primary_no_new_array:
1970 literal
1971 | THIS_TK
1972 { $$ = build_this ($1.location); }
1973 | OP_TK expression CP_TK
1974 {$$ = $2;}
1975 | class_instance_creation_expression
1976 | field_access
1977 | method_invocation
1978 | array_access
1979 | type_literals
1980 /* Added, JDK1.1 inner classes. Documentation is wrong
1981 referring to a 'ClassName' (class_name) rule that doesn't
1982 exist. Used name: instead. */
1983 | name DOT_TK THIS_TK
1985 tree wfl = build_wfl_node (this_identifier_node);
1986 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1988 | OP_TK expression error
1989 {yyerror ("')' expected"); RECOVER;}
1990 | name DOT_TK error
1991 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1992 | primitive_type DOT_TK error
1993 {yyerror ("'class' expected" ); RECOVER;}
1994 | VOID_TK DOT_TK error
1995 {yyerror ("'class' expected" ); RECOVER;}
1998 type_literals:
1999 name DOT_TK CLASS_TK
2000 { $$ = build_incomplete_class_ref ($2.location, $1); }
2001 | array_type DOT_TK CLASS_TK
2002 { $$ = build_incomplete_class_ref ($2.location, $1); }
2003 | primitive_type DOT_TK CLASS_TK
2004 { $$ = build_incomplete_class_ref ($2.location, $1); }
2005 | VOID_TK DOT_TK CLASS_TK
2007 $$ = build_incomplete_class_ref ($2.location,
2008 void_type_node);
2012 class_instance_creation_expression:
2013 NEW_TK class_type OP_TK argument_list CP_TK
2014 { $$ = build_new_invocation ($2, $4); }
2015 | NEW_TK class_type OP_TK CP_TK
2016 { $$ = build_new_invocation ($2, NULL_TREE); }
2017 | anonymous_class_creation
2018 /* Added, JDK1.1 inner classes, modified to use name or
2019 primary instead of primary solely which couldn't work in
2020 all situations. */
2021 | something_dot_new identifier OP_TK CP_TK
2023 tree ctor = build_new_invocation ($2, NULL_TREE);
2024 $$ = make_qualified_primary ($1, ctor,
2025 EXPR_WFL_LINECOL ($1));
2027 | something_dot_new identifier OP_TK CP_TK class_body
2028 | something_dot_new identifier OP_TK argument_list CP_TK
2030 tree ctor = build_new_invocation ($2, $4);
2031 $$ = make_qualified_primary ($1, ctor,
2032 EXPR_WFL_LINECOL ($1));
2034 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2035 | NEW_TK error SC_TK
2036 {$$ = NULL_TREE; yyerror ("'(' expected"); DRECOVER(new_1);}
2037 | NEW_TK class_type error
2038 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2039 | NEW_TK class_type OP_TK error
2040 {$$ = NULL_TREE; yyerror ("')' or term expected"); RECOVER;}
2041 | NEW_TK class_type OP_TK argument_list error
2042 {$$ = NULL_TREE; yyerror ("')' expected"); RECOVER;}
2043 | something_dot_new error
2045 $$ = NULL_TREE;
2046 YYERROR_NOW;
2047 yyerror ("Identifier expected");
2048 RECOVER;
2050 | something_dot_new identifier error
2051 {$$ = NULL_TREE; yyerror ("'(' expected"); RECOVER;}
2054 /* Created after JDK1.1 rules originally added to
2055 class_instance_creation_expression, but modified to use
2056 'class_type' instead of 'TypeName' (type_name) which is mentioned
2057 in the documentation but doesn't exist. */
2059 anonymous_class_creation:
2060 NEW_TK class_type OP_TK argument_list CP_TK
2061 { create_anonymous_class ($2); }
2062 class_body
2064 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2065 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2067 end_class_declaration (1);
2069 /* Now we can craft the new expression */
2070 $$ = build_new_invocation (id, $4);
2072 /* Note that we can't possibly be here if
2073 `class_type' is an interface (in which case the
2074 anonymous class extends Object and implements
2075 `class_type', hence its constructor can't have
2076 arguments.) */
2078 /* Otherwise, the innerclass must feature a
2079 constructor matching `argument_list'. Anonymous
2080 classes are a bit special: it's impossible to
2081 define constructor for them, hence constructors
2082 must be generated following the hints provided by
2083 the `new' expression. Whether a super constructor
2084 of that nature exists or not is to be verified
2085 later on in verify_constructor_super.
2087 It's during the expansion of a `new' statement
2088 referring to an anonymous class that a ctor will
2089 be generated for the anonymous class, with the
2090 right arguments. */
2093 | NEW_TK class_type OP_TK CP_TK
2094 { create_anonymous_class ($2); }
2095 class_body
2097 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2098 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2100 end_class_declaration (1);
2102 /* Now we can craft the new expression. The
2103 statement doesn't need to be remember so that a
2104 constructor can be generated, since its signature
2105 is already known. */
2106 $$ = build_new_invocation (id, NULL_TREE);
2110 something_dot_new: /* Added, not part of the specs. */
2111 name DOT_TK NEW_TK
2112 { $$ = $1; }
2113 | primary DOT_TK NEW_TK
2114 { $$ = $1; }
2117 argument_list:
2118 expression
2120 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2121 ctxp->formal_parameter_number = 1;
2123 | argument_list C_TK expression
2125 ctxp->formal_parameter_number += 1;
2126 $$ = tree_cons (NULL_TREE, $3, $1);
2128 | argument_list C_TK error
2129 {yyerror ("Missing term"); RECOVER;}
2132 array_creation_uninitialized:
2133 NEW_TK primitive_type dim_exprs
2134 { $$ = build_newarray_node ($2, $3, 0); }
2135 | NEW_TK class_or_interface_type dim_exprs
2136 { $$ = build_newarray_node ($2, $3, 0); }
2137 | NEW_TK primitive_type dim_exprs dims
2138 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2139 | NEW_TK class_or_interface_type dim_exprs dims
2140 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2141 | NEW_TK error CSB_TK
2142 {yyerror ("'[' expected"); DRECOVER ("]");}
2143 | NEW_TK error OSB_TK
2144 {yyerror ("']' expected"); RECOVER;}
2147 array_creation_initialized:
2148 /* Added, JDK1.1 anonymous array. Initial documentation rule
2149 modified */
2150 NEW_TK class_or_interface_type dims array_initializer
2152 char *sig;
2153 int osb = pop_current_osb (ctxp);
2154 while (osb--)
2155 obstack_grow (&temporary_obstack, "[]", 2);
2156 obstack_1grow (&temporary_obstack, '\0');
2157 sig = obstack_finish (&temporary_obstack);
2158 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2159 $2, get_identifier (sig), $4);
2161 | NEW_TK primitive_type dims array_initializer
2163 int osb = pop_current_osb (ctxp);
2164 tree type = $2;
2165 while (osb--)
2166 type = build_java_array_type (type, -1);
2167 $$ = build3 (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2168 build_pointer_type (type), NULL_TREE, $4);
2170 | NEW_TK error CSB_TK
2171 {yyerror ("'[' expected"); DRECOVER ("]");}
2172 | NEW_TK error OSB_TK
2173 {yyerror ("']' expected"); RECOVER;}
2176 dim_exprs:
2177 dim_expr
2178 { $$ = build_tree_list (NULL_TREE, $1); }
2179 | dim_exprs dim_expr
2180 { $$ = tree_cons (NULL_TREE, $2, $$); }
2183 dim_expr:
2184 OSB_TK expression CSB_TK
2186 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2188 $2 = build_wfl_node ($2);
2189 TREE_TYPE ($2) = NULL_TREE;
2191 EXPR_WFL_LINECOL ($2) = $1.location;
2192 $$ = $2;
2194 | OSB_TK expression error
2195 {yyerror ("']' expected"); RECOVER;}
2196 | OSB_TK error
2198 yyerror ("Missing term");
2199 yyerror ("']' expected");
2200 RECOVER;
2204 dims:
2205 OSB_TK CSB_TK
2207 int allocate = 0;
2208 /* If not initialized, allocate memory for the osb
2209 numbers stack */
2210 if (!ctxp->osb_limit)
2212 allocate = ctxp->osb_limit = 32;
2213 ctxp->osb_depth = -1;
2215 /* If capacity overflown, reallocate a bigger chunk */
2216 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2217 allocate = ctxp->osb_limit << 1;
2219 if (allocate)
2221 allocate *= sizeof (int);
2222 if (ctxp->osb_number)
2223 ctxp->osb_number = xrealloc (ctxp->osb_number,
2224 allocate);
2225 else
2226 ctxp->osb_number = xmalloc (allocate);
2228 ctxp->osb_depth++;
2229 CURRENT_OSB (ctxp) = 1;
2231 | dims OSB_TK CSB_TK
2232 { CURRENT_OSB (ctxp)++; }
2233 | dims OSB_TK error
2234 { yyerror ("']' expected"); RECOVER;}
2237 field_access:
2238 primary DOT_TK identifier
2239 { $$ = make_qualified_primary ($1, $3, $2.location); }
2240 /* FIXME - REWRITE TO:
2241 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2242 | SUPER_TK DOT_TK identifier
2244 tree super_wfl = build_wfl_node (super_identifier_node);
2245 SET_EXPR_LOCATION_FROM_TOKEN (super_wfl, $1);
2246 $$ = make_qualified_name (super_wfl, $3, $2.location);
2248 | SUPER_TK error
2249 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2252 method_invocation:
2253 name OP_TK CP_TK
2254 { $$ = build_method_invocation ($1, NULL_TREE); }
2255 | name OP_TK argument_list CP_TK
2256 { $$ = build_method_invocation ($1, $3); }
2257 | primary DOT_TK identifier OP_TK CP_TK
2259 if (TREE_CODE ($1) == THIS_EXPR)
2260 $$ = build_this_super_qualified_invocation
2261 (1, $3, NULL_TREE, 0, $2.location);
2262 else
2264 tree invok = build_method_invocation ($3, NULL_TREE);
2265 $$ = make_qualified_primary ($1, invok, $2.location);
2268 | primary DOT_TK identifier OP_TK argument_list CP_TK
2270 if (TREE_CODE ($1) == THIS_EXPR)
2271 $$ = build_this_super_qualified_invocation
2272 (1, $3, $5, 0, $2.location);
2273 else
2275 tree invok = build_method_invocation ($3, $5);
2276 $$ = make_qualified_primary ($1, invok, $2.location);
2279 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2281 $$ = build_this_super_qualified_invocation
2282 (0, $3, NULL_TREE, $1.location, $2.location);
2284 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2286 $$ = build_this_super_qualified_invocation
2287 (0, $3, $5, $1.location, $2.location);
2289 /* Screws up thing. I let it here until I'm convinced it can
2290 be removed. FIXME
2291 | primary DOT_TK error
2292 {yyerror ("'(' expected"); DRECOVER(bad);} */
2293 | SUPER_TK DOT_TK error CP_TK
2294 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2295 | SUPER_TK DOT_TK error DOT_TK
2296 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2299 array_access:
2300 name OSB_TK expression CSB_TK
2301 { $$ = build_array_ref ($2.location, $1, $3); }
2302 | primary_no_new_array OSB_TK expression CSB_TK
2303 { $$ = build_array_ref ($2.location, $1, $3); }
2304 | array_creation_initialized OSB_TK expression CSB_TK
2305 { $$ = build_array_ref ($2.location, $1, $3); }
2306 | name OSB_TK error
2308 yyerror ("Missing term and ']' expected");
2309 DRECOVER(array_access);
2311 | name OSB_TK expression error
2313 yyerror ("']' expected");
2314 DRECOVER(array_access);
2316 | primary_no_new_array OSB_TK error
2318 yyerror ("Missing term and ']' expected");
2319 DRECOVER(array_access);
2321 | primary_no_new_array OSB_TK expression error
2323 yyerror ("']' expected");
2324 DRECOVER(array_access);
2326 | array_creation_initialized OSB_TK error
2328 yyerror ("Missing term and ']' expected");
2329 DRECOVER(array_access);
2331 | array_creation_initialized OSB_TK expression error
2333 yyerror ("']' expected");
2334 DRECOVER(array_access);
2338 postfix_expression:
2339 primary
2340 | name
2341 | post_increment_expression
2342 | post_decrement_expression
2345 post_increment_expression:
2346 postfix_expression INCR_TK
2347 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2350 post_decrement_expression:
2351 postfix_expression DECR_TK
2352 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2355 trap_overflow_corner_case:
2356 pre_increment_expression
2357 | pre_decrement_expression
2358 | PLUS_TK unary_expression
2359 {$$ = build_unaryop ($1.token, $1.location, $2); }
2360 | unary_expression_not_plus_minus
2361 | PLUS_TK error
2362 {yyerror ("Missing term"); RECOVER}
2365 unary_expression:
2366 trap_overflow_corner_case
2368 if ($1)
2369 error_if_numeric_overflow ($1);
2370 $$ = $1;
2372 | MINUS_TK trap_overflow_corner_case
2373 {$$ = build_unaryop ($1.token, $1.location, $2); }
2374 | MINUS_TK error
2375 {yyerror ("Missing term"); RECOVER}
2378 pre_increment_expression:
2379 INCR_TK unary_expression
2380 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2381 | INCR_TK error
2382 {yyerror ("Missing term"); RECOVER}
2385 pre_decrement_expression:
2386 DECR_TK unary_expression
2387 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2388 | DECR_TK error
2389 {yyerror ("Missing term"); RECOVER}
2392 unary_expression_not_plus_minus:
2393 postfix_expression
2394 | NOT_TK unary_expression
2395 {$$ = build_unaryop ($1.token, $1.location, $2); }
2396 | NEG_TK unary_expression
2397 {$$ = build_unaryop ($1.token, $1.location, $2); }
2398 | cast_expression
2399 | NOT_TK error
2400 {yyerror ("Missing term"); RECOVER}
2401 | NEG_TK error
2402 {yyerror ("Missing term"); RECOVER}
2405 cast_expression: /* Error handling here is potentially weak */
2406 OP_TK primitive_type dims CP_TK unary_expression
2408 tree type = $2;
2409 int osb = pop_current_osb (ctxp);
2410 while (osb--)
2411 type = build_java_array_type (type, -1);
2412 $$ = build_cast ($1.location, type, $5);
2414 | OP_TK primitive_type CP_TK unary_expression
2415 { $$ = build_cast ($1.location, $2, $4); }
2416 | OP_TK expression CP_TK unary_expression_not_plus_minus
2417 { $$ = build_cast ($1.location, $2, $4); }
2418 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2420 const char *ptr;
2421 int osb = pop_current_osb (ctxp);
2422 obstack_grow (&temporary_obstack,
2423 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2424 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2425 while (osb--)
2426 obstack_grow (&temporary_obstack, "[]", 2);
2427 obstack_1grow (&temporary_obstack, '\0');
2428 ptr = obstack_finish (&temporary_obstack);
2429 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2430 $$ = build_cast ($1.location, $2, $5);
2432 | OP_TK primitive_type OSB_TK error
2433 {yyerror ("']' expected, invalid type expression");}
2434 | OP_TK error
2436 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2437 RECOVER;
2439 | OP_TK primitive_type dims CP_TK error
2440 {yyerror ("Missing term"); RECOVER;}
2441 | OP_TK primitive_type CP_TK error
2442 {yyerror ("Missing term"); RECOVER;}
2443 | OP_TK name dims CP_TK error
2444 {yyerror ("Missing term"); RECOVER;}
2447 multiplicative_expression:
2448 unary_expression
2449 | multiplicative_expression MULT_TK unary_expression
2451 $$ = build_binop (BINOP_LOOKUP ($2.token),
2452 $2.location, $1, $3);
2454 | multiplicative_expression DIV_TK unary_expression
2456 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2457 $1, $3);
2459 | multiplicative_expression REM_TK unary_expression
2461 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2462 $1, $3);
2464 | multiplicative_expression MULT_TK error
2465 {yyerror ("Missing term"); RECOVER;}
2466 | multiplicative_expression DIV_TK error
2467 {yyerror ("Missing term"); RECOVER;}
2468 | multiplicative_expression REM_TK error
2469 {yyerror ("Missing term"); RECOVER;}
2472 additive_expression:
2473 multiplicative_expression
2474 | additive_expression PLUS_TK multiplicative_expression
2476 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 $1, $3);
2479 | additive_expression MINUS_TK multiplicative_expression
2481 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2482 $1, $3);
2484 | additive_expression PLUS_TK error
2485 {yyerror ("Missing term"); RECOVER;}
2486 | additive_expression MINUS_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2490 shift_expression:
2491 additive_expression
2492 | shift_expression LS_TK additive_expression
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 $1, $3);
2497 | shift_expression SRS_TK additive_expression
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 $1, $3);
2502 | shift_expression ZRS_TK additive_expression
2504 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2505 $1, $3);
2507 | shift_expression LS_TK error
2508 {yyerror ("Missing term"); RECOVER;}
2509 | shift_expression SRS_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2511 | shift_expression ZRS_TK error
2512 {yyerror ("Missing term"); RECOVER;}
2515 relational_expression:
2516 shift_expression
2517 | relational_expression LT_TK shift_expression
2519 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2520 $1, $3);
2522 | relational_expression GT_TK shift_expression
2524 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2525 $1, $3);
2527 | relational_expression LTE_TK shift_expression
2529 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 $1, $3);
2532 | relational_expression GTE_TK shift_expression
2534 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 $1, $3);
2537 | relational_expression INSTANCEOF_TK reference_type
2538 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2539 | relational_expression LT_TK error
2540 {yyerror ("Missing term"); RECOVER;}
2541 | relational_expression GT_TK error
2542 {yyerror ("Missing term"); RECOVER;}
2543 | relational_expression LTE_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2545 | relational_expression GTE_TK error
2546 {yyerror ("Missing term"); RECOVER;}
2547 | relational_expression INSTANCEOF_TK error
2548 {yyerror ("Invalid reference type"); RECOVER;}
2551 equality_expression:
2552 relational_expression
2553 | equality_expression EQ_TK relational_expression
2555 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2556 $1, $3);
2558 | equality_expression NEQ_TK relational_expression
2560 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2561 $1, $3);
2563 | equality_expression EQ_TK error
2564 {yyerror ("Missing term"); RECOVER;}
2565 | equality_expression NEQ_TK error
2566 {yyerror ("Missing term"); RECOVER;}
2569 and_expression:
2570 equality_expression
2571 | and_expression AND_TK equality_expression
2573 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2574 $1, $3);
2576 | and_expression AND_TK error
2577 {yyerror ("Missing term"); RECOVER;}
2580 exclusive_or_expression:
2581 and_expression
2582 | exclusive_or_expression XOR_TK and_expression
2584 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2585 $1, $3);
2587 | exclusive_or_expression XOR_TK error
2588 {yyerror ("Missing term"); RECOVER;}
2591 inclusive_or_expression:
2592 exclusive_or_expression
2593 | inclusive_or_expression OR_TK exclusive_or_expression
2595 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2596 $1, $3);
2598 | inclusive_or_expression OR_TK error
2599 {yyerror ("Missing term"); RECOVER;}
2602 conditional_and_expression:
2603 inclusive_or_expression
2604 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2606 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2607 $1, $3);
2609 | conditional_and_expression BOOL_AND_TK error
2610 {yyerror ("Missing term"); RECOVER;}
2613 conditional_or_expression:
2614 conditional_and_expression
2615 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2617 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2618 $1, $3);
2620 | conditional_or_expression BOOL_OR_TK error
2621 {yyerror ("Missing term"); RECOVER;}
2624 conditional_expression: /* Error handling here is weak */
2625 conditional_or_expression
2626 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2628 $$ = build3 (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2629 SET_EXPR_LOCATION_FROM_TOKEN ($$, $2);
2631 | conditional_or_expression REL_QM_TK REL_CL_TK error
2633 YYERROR_NOW;
2634 yyerror ("Missing term");
2635 DRECOVER (1);
2637 | conditional_or_expression REL_QM_TK error
2638 {yyerror ("Missing term"); DRECOVER (2);}
2639 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2640 {yyerror ("Missing term"); DRECOVER (3);}
2643 assignment_expression:
2644 conditional_expression
2645 | assignment
2648 assignment:
2649 left_hand_side assignment_operator assignment_expression
2650 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2651 | left_hand_side assignment_operator error
2653 YYNOT_TWICE yyerror ("Missing term");
2654 DRECOVER (assign);
2658 left_hand_side:
2659 name
2660 | field_access
2661 | array_access
2664 assignment_operator:
2665 ASSIGN_ANY_TK
2666 | ASSIGN_TK
2669 expression:
2670 assignment_expression
2673 constant_expression:
2674 expression
2679 /* Helper function to retrieve an OSB count. Should be used when the
2680 `dims:' rule is being used. */
2682 static int
2683 pop_current_osb (struct parser_ctxt *ctxp)
2685 int to_return;
2687 if (ctxp->osb_depth < 0)
2688 abort ();
2690 to_return = CURRENT_OSB (ctxp);
2691 ctxp->osb_depth--;
2693 return to_return;
2698 /* This section of the code deal with save/restoring parser contexts.
2699 Add mode documentation here. FIXME */
2701 /* Helper function. Create a new parser context. With
2702 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2703 context is copied, otherwise, the new context is zeroed. The newly
2704 created context becomes the current one. */
2706 static void
2707 create_new_parser_context (int copy_from_previous)
2709 struct parser_ctxt *new;
2711 new = ggc_alloc (sizeof (struct parser_ctxt));
2712 if (copy_from_previous)
2714 memcpy (new, ctxp, sizeof (struct parser_ctxt));
2715 /* This flag, indicating the context saves global values,
2716 should only be set by java_parser_context_save_global. */
2717 new->saved_data_ctx = 0;
2719 else
2720 memset (new, 0, sizeof (struct parser_ctxt));
2722 new->next = ctxp;
2723 ctxp = new;
2726 /* Create a new parser context and make it the current one. */
2728 void
2729 java_push_parser_context (void)
2731 create_new_parser_context (0);
2734 void
2735 java_pop_parser_context (int generate)
2737 tree current;
2738 struct parser_ctxt *next;
2740 if (!ctxp)
2741 return;
2743 next = ctxp->next;
2744 if (next)
2746 input_location = ctxp->save_location;
2747 current_class = ctxp->class_type;
2750 /* If the old and new lexers differ, then free the old one. */
2751 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2752 java_destroy_lexer (ctxp->lexer);
2754 /* Set the single import class file flag to 0 for the current list
2755 of imported things */
2756 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2757 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2759 /* If we pushed a context to parse a class intended to be generated,
2760 we keep it so we can remember the class. What we could actually
2761 do is to just update a list of class names. */
2762 if (generate)
2764 if (ctxp_for_generation_last == NULL)
2765 ctxp_for_generation = ctxp;
2766 else
2767 ctxp_for_generation_last->next = ctxp;
2768 ctxp->next = NULL;
2769 ctxp_for_generation_last = ctxp;
2772 /* And restore those of the previous context */
2773 if ((ctxp = next)) /* Assignment is really meant here */
2774 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2775 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2778 /* Create a parser context for the use of saving some global
2779 variables. */
2781 void
2782 java_parser_context_save_global (void)
2784 if (!ctxp)
2786 java_push_parser_context ();
2787 ctxp->saved_data_ctx = 1;
2790 /* If this context already stores data, create a new one suitable
2791 for data storage. */
2792 else if (ctxp->saved_data)
2794 create_new_parser_context (1);
2795 ctxp->saved_data_ctx = 1;
2798 ctxp->save_location = input_location;
2799 ctxp->class_type = current_class;
2800 ctxp->function_decl = current_function_decl;
2801 ctxp->saved_data = 1;
2804 /* Restore some global variables from the previous context. Make the
2805 previous context the current one. */
2807 void
2808 java_parser_context_restore_global (void)
2810 input_location = ctxp->save_location;
2811 current_class = ctxp->class_type;
2812 if (wfl_operator)
2813 #ifdef USE_MAPPED_LOCATION
2814 SET_EXPR_LOCATION (wfl_operator, ctxp->save_location);
2815 #else
2816 EXPR_WFL_FILENAME_NODE (wfl_operator) = get_identifier (input_filename);
2817 #endif
2818 current_function_decl = ctxp->function_decl;
2819 ctxp->saved_data = 0;
2820 if (ctxp->saved_data_ctx)
2821 java_pop_parser_context (0);
2824 /* Suspend vital data for the current class/function being parsed so
2825 that an other class can be parsed. Used to let local/anonymous
2826 classes be parsed. */
2828 static void
2829 java_parser_context_suspend (void)
2831 /* This makes debugging through java_debug_context easier */
2832 static const char *const name = "<inner buffer context>";
2834 /* Duplicate the previous context, use it to save the globals we're
2835 interested in */
2836 create_new_parser_context (1);
2837 ctxp->function_decl = current_function_decl;
2838 ctxp->class_type = current_class;
2840 /* Then create a new context which inherits all data from the
2841 previous one. This will be the new current context */
2842 create_new_parser_context (1);
2844 /* Help debugging */
2845 ctxp->next->filename = name;
2848 /* Resume vital data for the current class/function being parsed so
2849 that an other class can be parsed. Used to let local/anonymous
2850 classes be parsed. The trick is the data storing file position
2851 informations must be restored to their current value, so parsing
2852 can resume as if no context was ever saved. */
2854 static void
2855 java_parser_context_resume (void)
2857 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2858 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2859 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2861 /* We need to inherit the list of classes to complete/generate */
2862 restored->classd_list = old->classd_list;
2863 restored->class_list = old->class_list;
2865 /* Restore the current class and function from the saver */
2866 current_class = saver->class_type;
2867 current_function_decl = saver->function_decl;
2869 /* Retrieve the restored context */
2870 ctxp = restored;
2872 /* Re-installed the data for the parsing to carry on */
2873 memcpy (&ctxp->marker_begining, &old->marker_begining,
2874 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2877 /* Add a new anchor node to which all statement(s) initializing static
2878 and non static initialized upon declaration field(s) will be
2879 linked. */
2881 static void
2882 java_parser_context_push_initialized_field (void)
2884 tree node;
2886 node = build_tree_list (NULL_TREE, NULL_TREE);
2887 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2888 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2890 node = build_tree_list (NULL_TREE, NULL_TREE);
2891 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2892 CPC_INITIALIZER_LIST (ctxp) = node;
2894 node = build_tree_list (NULL_TREE, NULL_TREE);
2895 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2896 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2899 /* Pop the lists of initialized field. If this lists aren't empty,
2900 remember them so we can use it to create and populate the finit$
2901 or <clinit> functions. */
2903 static void
2904 java_parser_context_pop_initialized_field (void)
2906 tree stmts;
2907 tree class_type = TREE_TYPE (GET_CPC ());
2909 if (CPC_INITIALIZER_LIST (ctxp))
2911 stmts = CPC_INITIALIZER_STMT (ctxp);
2912 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2913 if (stmts && !java_error_count)
2914 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2917 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2919 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2920 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2921 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2922 /* Keep initialization in order to enforce 8.5 */
2923 if (stmts && !java_error_count)
2924 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2927 /* JDK 1.1 instance initializers */
2928 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2930 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2931 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2932 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2933 if (stmts && !java_error_count)
2934 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2938 static tree
2939 reorder_static_initialized (tree list)
2941 /* We have to keep things in order. The alias initializer have to
2942 come first, then the initialized regular field, in reverse to
2943 keep them in lexical order. */
2944 tree marker, previous = NULL_TREE;
2945 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2946 if (TREE_CODE (marker) == TREE_LIST
2947 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2948 break;
2950 /* No static initialized, the list is fine as is */
2951 if (!previous)
2952 list = TREE_CHAIN (marker);
2954 /* No marker? reverse the whole list */
2955 else if (!marker)
2956 list = nreverse (list);
2958 /* Otherwise, reverse what's after the marker and the new reordered
2959 sublist will replace the marker. */
2960 else
2962 TREE_CHAIN (previous) = NULL_TREE;
2963 list = nreverse (list);
2964 list = chainon (TREE_CHAIN (marker), list);
2966 return list;
2969 /* Helper functions to dump the parser context stack. */
2971 #define TAB_CONTEXT(C) \
2972 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2974 static void
2975 java_debug_context_do (int tab)
2977 struct parser_ctxt *copy = ctxp;
2978 while (copy)
2980 TAB_CONTEXT (tab);
2981 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2982 TAB_CONTEXT (tab);
2983 fprintf (stderr, "filename: %s\n", copy->filename);
2984 TAB_CONTEXT (tab);
2985 fprintf (stderr, "package: %s\n",
2986 (copy->package ?
2987 IDENTIFIER_POINTER (copy->package) : "<none>"));
2988 TAB_CONTEXT (tab);
2989 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2990 TAB_CONTEXT (tab);
2991 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2992 copy = copy->next;
2993 tab += 2;
2997 /* Dump the stacked up parser contexts. Intended to be called from a
2998 debugger. */
3000 void
3001 java_debug_context (void)
3003 java_debug_context_do (0);
3008 /* Flag for the error report routine to issue the error the first time
3009 it's called (overriding the default behavior which is to drop the
3010 first invocation and honor the second one, taking advantage of a
3011 richer context. */
3012 static int force_error = 0;
3014 /* Reporting an constructor invocation error. */
3015 static void
3016 parse_ctor_invocation_error (void)
3018 if (DECL_CONSTRUCTOR_P (current_function_decl))
3019 yyerror ("Constructor invocation must be first thing in a constructor");
3020 else
3021 yyerror ("Only constructors can invoke constructors");
3024 /* Reporting JDK1.1 features not implemented. */
3026 static tree
3027 parse_jdk1_1_error (const char *msg)
3029 sorry (": %qs JDK1.1(TM) feature", msg);
3030 java_error_count++;
3031 return build_java_empty_stmt ();
3034 static int do_warning = 0;
3036 void
3037 yyerror (const char *msgid)
3039 #ifdef USE_MAPPED_LOCATION
3040 static source_location elc;
3041 expanded_location xloc = expand_location (input_location);
3042 int current_line = xloc.line;
3043 #else
3044 static java_lc elc;
3045 int save_lineno;
3046 int current_line = input_line;
3047 #endif
3048 static int prev_lineno;
3049 static const char *prev_msg;
3051 char *remainder, *code_from_source;
3053 if (!force_error && prev_lineno == current_line)
3054 return;
3055 #ifndef USE_MAPPED_LOCATION
3056 current_line = ctxp->lexer->token_start.line;
3057 #endif
3059 /* Save current error location but report latter, when the context is
3060 richer. */
3061 if (ctxp->java_error_flag == 0)
3063 ctxp->java_error_flag = 1;
3064 #ifdef USE_MAPPED_LOCATION
3065 elc = input_location;
3066 #else
3067 elc = ctxp->lexer->token_start;
3068 #endif
3069 /* Do something to use the previous line if we're reaching the
3070 end of the file... */
3071 #ifdef VERBOSE_SKELETON
3072 printf ("* Error detected (%s)\n", (msgid ? msgid : "(null)"));
3073 #endif
3074 return;
3077 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3078 if (!force_error && msgid == prev_msg && prev_lineno == current_line)
3079 return;
3081 ctxp->java_error_flag = 0;
3082 if (do_warning)
3083 java_warning_count++;
3084 else
3085 java_error_count++;
3087 #if 0 /* FIXME */
3088 if (elc.col == 0 && msgid && msgid[1] == ';')
3089 elc = ctxp->prev_line_end;
3090 #endif
3092 prev_msg = msgid;
3094 #ifdef USE_MAPPED_LOCATION
3095 prev_lineno = current_line;
3096 code_from_source = java_get_line_col (xloc.file, current_line, xloc.column);
3097 #else
3098 save_lineno = input_line;
3099 prev_lineno = input_line = current_line;
3100 code_from_source = java_get_line_col (input_filename, current_line,
3101 ctxp->lexer->token_start.col);
3102 #endif
3105 obstack_grow0 (&temporary_obstack,
3106 code_from_source, strlen (code_from_source));
3107 remainder = obstack_finish (&temporary_obstack);
3108 if (do_warning)
3109 warning (0, "%s.\n%s", msgid, remainder);
3110 else
3111 error ("%s.\n%s", msgid, remainder);
3113 /* This allow us to cheaply avoid an extra 'Invalid expression
3114 statement' error report when errors have been already reported on
3115 the same line. This occurs when we report an error but don't have
3116 a synchronization point other than ';', which
3117 expression_statement is the only one to take care of. */
3118 #ifndef USE_MAPPED_LOCATION
3119 input_line = save_lineno;
3120 #endif
3121 ctxp->prevent_ese = input_line;
3124 static void
3125 issue_warning_error_from_context (
3126 #ifdef USE_MAPPED_LOCATION
3127 source_location cl,
3128 #else
3129 tree cl,
3130 #endif
3131 const char *msgid, va_list *ap)
3133 #ifdef USE_MAPPED_LOCATION
3134 source_location saved_location = input_location;
3135 expanded_location xloc = expand_location (cl);
3136 #else
3137 java_lc save_lc = ctxp->lexer->token_start;
3138 const char *saved = ctxp->filename, *saved_input_filename;
3139 #endif
3140 char buffer [4096];
3141 text_info text;
3143 text.err_no = errno;
3144 text.args_ptr = ap;
3145 text.format_spec = msgid;
3146 pp_format_text (global_dc->printer, &text);
3147 strncpy (buffer, pp_formatted_text (global_dc->printer), sizeof (buffer) - 1);
3148 buffer[sizeof (buffer) - 1] = '\0';
3149 pp_clear_output_area (global_dc->printer);
3151 force_error = 1;
3153 #ifdef USE_MAPPED_LOCATION
3154 if (xloc.file != NULL)
3156 ctxp->filename = xloc.file;
3157 input_location = cl;
3159 #else
3160 ctxp->lexer->token_start.line = EXPR_WFL_LINENO (cl);
3161 ctxp->lexer->token_start.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1
3162 : EXPR_WFL_COLNO (cl) == 0xffe ? -2
3163 : EXPR_WFL_COLNO (cl));
3165 /* We have a CL, that's a good reason for using it if it contains data */
3166 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3167 ctxp->filename = EXPR_WFL_FILENAME (cl);
3168 saved_input_filename = input_filename;
3169 input_filename = ctxp->filename;
3170 #endif
3171 java_error (NULL);
3172 java_error (buffer);
3173 #ifdef USE_MAPPED_LOCATION
3174 input_location = saved_location;
3175 #else
3176 ctxp->filename = saved;
3177 input_filename = saved_input_filename;
3178 ctxp->lexer->token_start = save_lc;
3179 #endif
3180 force_error = 0;
3183 /* Issue an error message at a current source line CL.
3184 FUTURE/FIXME: change cl to be a source_location. */
3186 void
3187 parse_error_context (tree cl, const char *msgid, ...)
3189 va_list ap;
3190 va_start (ap, msgid);
3191 #ifdef USE_MAPPED_LOCATION
3192 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3193 #else
3194 issue_warning_error_from_context (cl, msgid, &ap);
3195 #endif
3196 va_end (ap);
3199 /* Issue a warning at a current source line CL.
3200 FUTURE/FIXME: change cl to be a source_location. */
3202 static void
3203 parse_warning_context (tree cl, const char *msgid, ...)
3205 va_list ap;
3206 va_start (ap, msgid);
3208 do_warning = 1;
3209 #ifdef USE_MAPPED_LOCATION
3210 issue_warning_error_from_context (EXPR_LOCATION (cl), msgid, &ap);
3211 #else
3212 issue_warning_error_from_context (cl, msgid, &ap);
3213 #endif
3214 do_warning = 0;
3215 va_end (ap);
3218 static tree
3219 find_expr_with_wfl (tree node)
3221 while (node)
3223 enum tree_code_class code;
3224 tree to_return;
3226 switch (TREE_CODE (node))
3228 case BLOCK:
3229 node = BLOCK_EXPR_BODY (node);
3230 continue;
3232 case COMPOUND_EXPR:
3233 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3234 if (to_return)
3235 return to_return;
3236 node = TREE_OPERAND (node, 1);
3237 continue;
3239 case LOOP_EXPR:
3240 node = TREE_OPERAND (node, 0);
3241 continue;
3243 case LABELED_BLOCK_EXPR:
3244 node = LABELED_BLOCK_BODY (node);
3245 continue;
3247 default:
3248 code = TREE_CODE_CLASS (TREE_CODE (node));
3249 if (((code == tcc_unary) || (code == tcc_binary)
3250 || (code == tcc_expression))
3251 && EXPR_WFL_LINECOL (node))
3252 return node;
3253 return NULL_TREE;
3256 return NULL_TREE;
3259 /* Issue a missing return statement error. Uses METHOD to figure the
3260 last line of the method the error occurs in. */
3262 static void
3263 missing_return_error (tree method)
3265 #ifdef USE_MAPPED_LOCATION
3266 SET_EXPR_LOCATION (wfl_operator, DECL_FUNCTION_LAST_LINE (method));
3267 #else
3268 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_FUNCTION_LAST_LINE (method), -2);
3269 #endif
3270 parse_error_context (wfl_operator, "Missing return statement");
3273 /* Issue an unreachable statement error. From NODE, find the next
3274 statement to report appropriately. */
3275 static void
3276 unreachable_stmt_error (tree node)
3278 /* Browse node to find the next expression node that has a WFL. Use
3279 the location to report the error */
3280 if (TREE_CODE (node) == COMPOUND_EXPR)
3281 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3282 else
3283 node = find_expr_with_wfl (node);
3285 if (node)
3287 #ifdef USE_MAPPED_LOCATION
3288 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
3289 #else
3290 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3291 #endif
3292 parse_error_context (wfl_operator, "Unreachable statement");
3294 else
3295 abort ();
3298 static int
3299 not_accessible_field_error (tree wfl, tree decl)
3301 parse_error_context
3302 (wfl, "Can't access %s field %<%s.%s%> from %qs",
3303 accessibility_string (get_access_flags_from_decl (decl)),
3304 GET_TYPE_NAME (DECL_CONTEXT (decl)),
3305 IDENTIFIER_POINTER (DECL_NAME (decl)),
3306 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
3307 return 1;
3311 java_report_errors (void)
3313 if (java_error_count)
3314 fprintf (stderr, "%d error%s",
3315 java_error_count, (java_error_count == 1 ? "" : "s"));
3316 if (java_warning_count)
3317 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3318 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3319 if (java_error_count || java_warning_count)
3320 putc ('\n', stderr);
3321 return java_error_count;
3324 static char *
3325 java_accstring_lookup (int flags)
3327 static char buffer [80];
3328 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3330 /* Access modifier looked-up first for easier report on forbidden
3331 access. */
3332 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3333 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3334 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3335 if (flags & ACC_STATIC) COPY_RETURN ("static");
3336 if (flags & ACC_FINAL) COPY_RETURN ("final");
3337 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3338 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3339 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3340 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3341 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3342 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3344 buffer [0] = '\0';
3345 return buffer;
3346 #undef COPY_RETURN
3349 /* Returns a string denoting the accessibility of a class or a member as
3350 indicated by FLAGS. We need a separate function from
3351 java_accstring_lookup, as the latter can return spurious "static", etc.
3352 if package-private access is defined (in which case none of the
3353 relevant access control bits in FLAGS is set). */
3355 static const char *
3356 accessibility_string (int flags)
3358 if (flags & ACC_PRIVATE) return "private";
3359 if (flags & ACC_PROTECTED) return "protected";
3360 if (flags & ACC_PUBLIC) return "public";
3362 return "package-private";
3365 /* Issuing error messages upon redefinition of classes, interfaces or
3366 variables. */
3368 static void
3369 classitf_redefinition_error (const char *context, tree id, tree decl, tree cl)
3371 parse_error_context (cl, "%s %qs already defined in %s:%d",
3372 context, IDENTIFIER_POINTER (id),
3373 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3374 /* Here we should point out where its redefined. It's a unicode. FIXME */
3377 static void
3378 variable_redefinition_error (tree context, tree name, tree type, int line)
3380 const char *type_name;
3382 /* Figure a proper name for type. We might haven't resolved it */
3383 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3384 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3385 else
3386 type_name = lang_printable_name (type, 0);
3388 parse_error_context (context,
3389 "Variable %qs is already defined in this method and was declared %<%s %s%> at line %d",
3390 IDENTIFIER_POINTER (name),
3391 type_name, IDENTIFIER_POINTER (name), line);
3394 /* If ANAME is terminated with `[]', it indicates an array. This
3395 function returns the number of `[]' found and if this number is
3396 greater than zero, it extracts the array type name and places it in
3397 the node pointed to by TRIMMED unless TRIMMED is null. */
3399 static int
3400 build_type_name_from_array_name (tree aname, tree *trimmed)
3402 const char *name = IDENTIFIER_POINTER (aname);
3403 int len = IDENTIFIER_LENGTH (aname);
3404 int array_dims;
3406 STRING_STRIP_BRACKETS (name, len, array_dims);
3408 if (array_dims && trimmed)
3409 *trimmed = get_identifier_with_length (name, len);
3411 return array_dims;
3414 static tree
3415 build_array_from_name (tree type, tree type_wfl, tree name, tree *ret_name)
3417 int more_dims = 0;
3419 /* Eventually get more dims */
3420 more_dims = build_type_name_from_array_name (name, &name);
3422 /* If we have, then craft a new type for this variable */
3423 if (more_dims)
3425 tree save = type;
3427 /* If we have a pointer, use its type */
3428 if (TREE_CODE (type) == POINTER_TYPE)
3429 type = TREE_TYPE (type);
3431 /* Building the first dimension of a primitive type uses this
3432 function */
3433 if (JPRIMITIVE_TYPE_P (type))
3435 type = build_java_array_type (type, -1);
3436 more_dims--;
3438 /* Otherwise, if we have a WFL for this type, use it (the type
3439 is already an array on an unresolved type, and we just keep
3440 on adding dimensions) */
3441 else if (type_wfl)
3443 type = type_wfl;
3444 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3445 NULL);
3448 /* Add all the dimensions */
3449 while (more_dims--)
3450 type = build_unresolved_array_type (type);
3452 /* The type may have been incomplete in the first place */
3453 if (type_wfl)
3454 type = obtain_incomplete_type (type);
3457 if (ret_name)
3458 *ret_name = name;
3459 return type;
3462 /* Build something that the type identifier resolver will identify as
3463 being an array to an unresolved type. TYPE_WFL is a WFL on a
3464 identifier. */
3466 static tree
3467 build_unresolved_array_type (tree type_or_wfl)
3469 const char *ptr;
3470 tree wfl;
3472 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3473 just create a array type */
3474 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3475 return build_java_array_type (type_or_wfl, -1);
3477 obstack_grow (&temporary_obstack,
3478 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3479 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3480 obstack_grow0 (&temporary_obstack, "[]", 2);
3481 ptr = obstack_finish (&temporary_obstack);
3482 #ifdef USE_MAPPED_LOCATION
3483 wfl = build_expr_wfl (get_identifier (ptr), EXPR_LOCATION (type_or_wfl));
3484 #else
3485 wfl = build_expr_wfl (get_identifier (ptr),
3486 EXPR_WFL_FILENAME (type_or_wfl),
3487 EXPR_WFL_LINENO (type_or_wfl),
3488 EXPR_WFL_COLNO (type_or_wfl));
3489 #endif
3490 /* Re-install the existing qualifications so that the type can be
3491 resolved properly. */
3492 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3493 return wfl;
3496 static void
3497 parser_add_interface (tree class_decl, tree interface_decl, tree wfl)
3499 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3500 parse_error_context (wfl, "Interface %qs repeated",
3501 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3504 /* Bulk of common class/interface checks. Return 1 if an error was
3505 encountered. TAG is 0 for a class, 1 for an interface. */
3507 static int
3508 check_class_interface_creation (int is_interface, int flags, tree raw_name,
3509 tree qualified_name, tree decl, tree cl)
3511 tree node;
3512 int sca = 0; /* Static class allowed */
3513 int icaf = 0; /* Inner class allowed flags */
3514 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3516 if (!quiet_flag)
3517 fprintf (stderr, " %s%s %s",
3518 (CPC_INNER_P () ? "inner" : ""),
3519 (is_interface ? "interface" : "class"),
3520 IDENTIFIER_POINTER (qualified_name));
3522 /* Scope of an interface/class type name:
3523 - Can't be imported by a single type import
3524 - Can't already exists in the package */
3525 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3526 && (node = find_name_in_single_imports (raw_name))
3527 && !CPC_INNER_P ())
3529 parse_error_context
3530 (cl, "%s name %qs clashes with imported type %qs",
3531 (is_interface ? "Interface" : "Class"),
3532 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3533 return 1;
3535 if (decl && CLASS_COMPLETE_P (decl))
3537 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3538 qualified_name, decl, cl);
3539 return 1;
3542 if (check_inner_class_redefinition (raw_name, cl))
3543 return 1;
3545 /* If public, file name should match class/interface name, except
3546 when dealing with an inner class */
3547 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3549 const char *fname = input_filename;
3550 const char *f;
3552 for (f = fname + strlen (fname);
3553 f != fname && ! IS_DIR_SEPARATOR (*f);
3554 f--)
3556 if (IS_DIR_SEPARATOR (*f))
3557 f++;
3558 if (strncmp (IDENTIFIER_POINTER (raw_name),
3559 f , IDENTIFIER_LENGTH (raw_name)) ||
3560 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3561 parse_error_context
3562 (cl, "Public %s %qs must be defined in a file called %<%s.java%>",
3563 (is_interface ? "interface" : "class"),
3564 IDENTIFIER_POINTER (qualified_name),
3565 IDENTIFIER_POINTER (raw_name));
3568 /* Static classes can be declared only in top level classes. Note:
3569 once static, a inner class is a top level class. */
3570 if (flags & ACC_STATIC)
3572 /* Catch the specific error of declaring an class inner class
3573 with no toplevel enclosing class. Prevent check_modifiers from
3574 complaining a second time */
3575 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3577 parse_error_context (cl, "Inner class %qs can't be static. Static classes can only occur in interfaces and top-level classes",
3578 IDENTIFIER_POINTER (qualified_name));
3579 sca = ACC_STATIC;
3581 /* Else, in the context of a top-level class declaration, let
3582 `check_modifiers' do its job, otherwise, give it a go */
3583 else
3584 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3587 /* Inner classes can be declared private or protected
3588 within their enclosing classes. */
3589 if (CPC_INNER_P ())
3591 /* A class which is local to a block can't be public, private,
3592 protected or static. But it is created final, so allow this
3593 one. */
3594 if (current_function_decl)
3595 icaf = sca = uaaf = ACC_FINAL;
3596 else
3598 check_modifiers_consistency (flags);
3599 icaf = ACC_PROTECTED;
3600 if (! CLASS_INTERFACE (GET_CPC ()))
3601 icaf |= ACC_PRIVATE;
3605 if (is_interface)
3607 if (CPC_INNER_P ())
3608 uaaf = INTERFACE_INNER_MODIFIERS;
3609 else
3610 uaaf = INTERFACE_MODIFIERS;
3612 check_modifiers ("Illegal modifier %qs for interface declaration",
3613 flags, uaaf);
3615 else
3616 check_modifiers ((current_function_decl ?
3617 "Illegal modifier %qs for local class declaration" :
3618 "Illegal modifier %qs for class declaration"),
3619 flags, uaaf|sca|icaf);
3620 return 0;
3623 /* Construct a nested class name. If the final component starts with
3624 a digit, return true. Otherwise return false. */
3625 static int
3626 make_nested_class_name (tree cpc_list)
3628 tree name;
3630 if (!cpc_list)
3631 return 0;
3633 make_nested_class_name (TREE_CHAIN (cpc_list));
3635 /* Pick the qualified name when dealing with the first upmost
3636 enclosing class */
3637 name = (TREE_CHAIN (cpc_list)
3638 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3639 obstack_grow (&temporary_obstack,
3640 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3641 obstack_1grow (&temporary_obstack, '$');
3643 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3646 /* Can't redefine a class already defined in an earlier scope. */
3648 static int
3649 check_inner_class_redefinition (tree raw_name, tree cl)
3651 tree scope_list;
3653 for (scope_list = GET_CPC_LIST (); scope_list;
3654 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3655 if (raw_name == GET_CPC_UN_NODE (scope_list))
3657 parse_error_context
3658 (cl, "The class name %qs is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3659 IDENTIFIER_POINTER (raw_name));
3660 return 1;
3662 return 0;
3665 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3666 we remember ENCLOSING and SUPER. */
3668 static tree
3669 resolve_inner_class (htab_t circularity_hash, tree cl, tree *enclosing,
3670 tree *super, tree class_type)
3672 tree local_enclosing = *enclosing;
3673 tree local_super = NULL_TREE;
3675 while (local_enclosing)
3677 tree intermediate, decl;
3679 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3680 local_enclosing;
3682 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3683 return decl;
3685 intermediate = local_enclosing;
3686 /* Explore enclosing contexts. */
3687 while (INNER_CLASS_DECL_P (intermediate))
3689 intermediate = DECL_CONTEXT (intermediate);
3690 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3691 return decl;
3694 /* Now go to the upper classes, bail out if necessary. We will
3695 analyze the returned SUPER and act accordingly (see
3696 do_resolve_class). */
3697 if (JPRIMITIVE_TYPE_P (TREE_TYPE (local_enclosing))
3698 || TREE_TYPE (local_enclosing) == void_type_node)
3700 parse_error_context (cl, "Qualifier must be a reference");
3701 local_enclosing = NULL_TREE;
3702 break;
3704 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3705 if (!local_super || local_super == object_type_node)
3706 break;
3708 if (TREE_CODE (local_super) == POINTER_TYPE)
3709 local_super = do_resolve_class (NULL, NULL, local_super, NULL, NULL);
3710 else
3711 local_super = TYPE_NAME (local_super);
3713 /* We may not have checked for circular inheritance yet, so do so
3714 here to prevent an infinite loop. */
3715 if (htab_find (circularity_hash, local_super) != NULL)
3717 if (!cl)
3718 cl = lookup_cl (local_enclosing);
3720 parse_error_context
3721 (cl, "Cyclic inheritance involving %s",
3722 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3723 local_enclosing = NULL_TREE;
3725 else
3726 local_enclosing = local_super;
3729 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3730 *super = local_super;
3731 *enclosing = local_enclosing;
3733 return NULL_TREE;
3736 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3737 qualified. */
3739 static tree
3740 find_as_inner_class (tree enclosing, tree name, tree cl)
3742 tree qual, to_return;
3743 if (!enclosing)
3744 return NULL_TREE;
3746 name = TYPE_NAME (name);
3748 /* First search: within the scope of `enclosing', search for name */
3749 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3750 qual = EXPR_WFL_QUALIFICATION (cl);
3751 else if (cl)
3752 qual = build_tree_list (cl, NULL_TREE);
3753 else
3754 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3756 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3757 return to_return;
3759 /* We're dealing with a qualified name. Try to resolve thing until
3760 we get something that is an enclosing class. */
3761 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3763 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3765 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3766 qual = TREE_CHAIN (qual))
3768 acc = merge_qualified_name (acc,
3769 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3770 BUILD_PTR_FROM_NAME (ptr, acc);
3771 decl = do_resolve_class (NULL_TREE, NULL_TREE, ptr, NULL_TREE, cl);
3774 /* A NULL qual and a decl means that the search ended
3775 successfully?!? We have to do something then. FIXME */
3777 if (decl)
3778 enclosing = decl;
3779 else
3780 qual = EXPR_WFL_QUALIFICATION (cl);
3782 /* Otherwise, create a qual for the other part of the resolution. */
3783 else
3784 qual = build_tree_list (build_unknown_wfl (name), NULL_TREE);
3786 return find_as_inner_class_do (qual, enclosing);
3789 /* We go inside the list of sub classes and try to find a way
3790 through. */
3792 static tree
3793 find_as_inner_class_do (tree qual, tree enclosing)
3795 if (!qual)
3796 return NULL_TREE;
3798 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3800 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3801 tree next_enclosing = NULL_TREE;
3802 tree inner_list;
3804 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3805 inner_list; inner_list = TREE_CHAIN (inner_list))
3807 if (TREE_VALUE (inner_list) == name_to_match)
3809 next_enclosing = TREE_PURPOSE (inner_list);
3810 break;
3813 enclosing = next_enclosing;
3816 return (!qual && enclosing ? enclosing : NULL_TREE);
3819 static void
3820 link_nested_class_to_enclosing (void)
3822 if (GET_ENCLOSING_CPC ())
3824 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3825 DECL_INNER_CLASS_LIST (enclosing) =
3826 tree_cons (GET_CPC (), GET_CPC_UN (),
3827 DECL_INNER_CLASS_LIST (enclosing));
3831 static tree
3832 maybe_make_nested_class_name (tree name)
3834 tree id = NULL_TREE;
3836 if (CPC_INNER_P ())
3838 /* If we're in a function, we must append a number to create the
3839 nested class name. However, we don't do this if the class we
3840 are constructing is anonymous, because in that case we'll
3841 already have a number as the class name. */
3842 if (! make_nested_class_name (GET_CPC_LIST ())
3843 && current_function_decl != NULL_TREE
3844 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3846 char buf[10];
3847 sprintf (buf, "%d", anonymous_class_counter);
3848 ++anonymous_class_counter;
3849 obstack_grow (&temporary_obstack, buf, strlen (buf));
3850 obstack_1grow (&temporary_obstack, '$');
3852 obstack_grow0 (&temporary_obstack,
3853 IDENTIFIER_POINTER (name),
3854 IDENTIFIER_LENGTH (name));
3855 id = get_identifier (obstack_finish (&temporary_obstack));
3856 if (ctxp->package)
3857 QUALIFIED_P (id) = 1;
3859 return id;
3862 /* If DECL is NULL, create and push a new DECL, record the current
3863 line CL and do other maintenance things. */
3865 static tree
3866 maybe_create_class_interface_decl (tree decl, tree raw_name,
3867 tree qualified_name, tree cl)
3869 if (!decl)
3870 decl = push_class (make_class (), qualified_name);
3872 /* Take care of the file and line business */
3873 #ifdef USE_MAPPED_LOCATION
3874 DECL_SOURCE_LOCATION (decl) = EXPR_LOCATION (cl);
3875 #else
3876 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3877 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3878 #endif
3879 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3880 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3881 #ifdef USE_MAPPED_LOCATION
3883 tree tmp = maybe_get_identifier (EXPR_FILENAME (cl));
3884 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3885 tmp && IS_A_COMMAND_LINE_FILENAME_P (tmp);
3887 #else
3888 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3889 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3890 #endif
3892 PUSH_CPC (decl, raw_name);
3893 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3895 /* Link the declaration to the already seen ones */
3896 TREE_CHAIN (decl) = ctxp->class_list;
3897 ctxp->class_list = decl;
3899 /* Create a new nodes in the global lists */
3900 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3901 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3903 /* Install a new dependency list element */
3904 create_jdep_list (ctxp);
3906 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3907 IDENTIFIER_POINTER (qualified_name)));
3908 return decl;
3911 static void
3912 add_superinterfaces (tree decl, tree interface_list)
3914 tree node;
3915 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3916 takes care of ensuring that:
3917 - This is an accessible interface type,
3918 - Circularity detection.
3919 parser_add_interface is then called. If present but not defined,
3920 the check operation is delayed until the super interface gets
3921 defined. */
3922 for (node = interface_list; node; node = TREE_CHAIN (node))
3924 tree current = TREE_PURPOSE (node);
3925 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3926 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3928 if (!parser_check_super_interface (idecl, decl, current))
3929 parser_add_interface (decl, idecl, current);
3931 else
3932 register_incomplete_type (JDEP_INTERFACE,
3933 current, decl, NULL_TREE);
3937 /* Create an interface in pass1 and return its decl. Return the
3938 interface's decl in pass 2. */
3940 static tree
3941 create_interface (int flags, tree id, tree super)
3943 tree raw_name = EXPR_WFL_NODE (id);
3944 tree q_name = parser_qualified_classname (raw_name);
3945 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3947 /* Certain syntax errors are making SUPER be like ID. Avoid this
3948 case. */
3949 if (ctxp->class_err && id == super)
3950 super = NULL;
3952 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3954 /* Basic checks: scope, redefinition, modifiers */
3955 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3957 PUSH_ERROR ();
3958 return NULL_TREE;
3961 /* Suspend the current parsing context if we're parsing an inner
3962 interface */
3963 if (CPC_INNER_P ())
3965 java_parser_context_suspend ();
3966 /* Interface members are public. */
3967 if (CLASS_INTERFACE (GET_CPC ()))
3968 flags |= ACC_PUBLIC;
3971 /* Push a new context for (static) initialized upon declaration fields */
3972 java_parser_context_push_initialized_field ();
3974 /* Interface modifiers check
3975 - public/abstract allowed (already done at that point)
3976 - abstract is obsolete (comes first, it's a warning, or should be)
3977 - Can't use twice the same (checked in the modifier rule) */
3978 if ((flags & ACC_ABSTRACT) && flag_redundant)
3979 parse_warning_context
3980 (MODIFIER_WFL (ABSTRACT_TK),
3981 "Redundant use of %<abstract%> modifier. Interface %qs is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3983 /* Create a new decl if DECL is NULL, otherwise fix it */
3984 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3986 /* Interfaces are always abstract. */
3987 flags |= ACC_ABSTRACT;
3989 /* Inner interfaces are always static. */
3990 if (INNER_CLASS_DECL_P (decl))
3991 flags |= ACC_STATIC;
3993 /* Set super info and mark the class a complete */
3994 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3995 object_type_node, ctxp->interface_number);
3996 ctxp->interface_number = 0;
3997 CLASS_COMPLETE_P (decl) = 1;
3998 add_superinterfaces (decl, super);
4000 /* Eventually sets the @deprecated tag flag */
4001 CHECK_DEPRECATED (decl);
4003 return decl;
4006 /* Patch anonymous class CLASS, by either extending or implementing
4007 DEP. */
4009 static void
4010 patch_anonymous_class (tree type_decl, tree class_decl, tree wfl)
4012 tree class = TREE_TYPE (class_decl);
4013 tree type = TREE_TYPE (type_decl);
4014 tree binfo = TYPE_BINFO (class);
4016 /* If it's an interface, implement it */
4017 if (CLASS_INTERFACE (type_decl))
4019 if (parser_check_super_interface (type_decl, class_decl, wfl))
4020 return;
4022 if (!VEC_space (tree, BINFO_BASE_BINFOS (binfo), 1))
4024 /* Extend the binfo - by reallocating and copying it. */
4025 tree new_binfo;
4026 tree base_binfo;
4027 int i;
4029 new_binfo = make_tree_binfo ((BINFO_N_BASE_BINFOS (binfo) + 1) * 2);
4030 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4031 BINFO_BASE_APPEND (new_binfo, base_binfo);
4032 CLASS_HAS_SUPER_FLAG (new_binfo) = CLASS_HAS_SUPER_FLAG (binfo);
4033 BINFO_VTABLE (new_binfo) = BINFO_VTABLE (binfo);
4034 TYPE_BINFO (class) = new_binfo;
4037 /* And add the interface */
4038 parser_add_interface (class_decl, type_decl, wfl);
4040 /* Otherwise, it's a type we want to extend */
4041 else
4043 if (parser_check_super (type_decl, class_decl, wfl))
4044 return;
4045 BINFO_TYPE (BINFO_BASE_BINFO (binfo, 0)) = type;
4049 /* Create an anonymous class which extends/implements TYPE_NAME, and return
4050 its decl. */
4052 static tree
4053 create_anonymous_class (tree type_name)
4055 char buffer [80];
4056 tree super = NULL_TREE, itf = NULL_TREE;
4057 tree id, type_decl, class;
4059 /* The unqualified name of the anonymous class. It's just a number. */
4060 sprintf (buffer, "%d", anonymous_class_counter++);
4061 id = build_wfl_node (get_identifier (buffer));
4062 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL (type_name);
4064 /* We know about the type to extend/implement. We go ahead */
4065 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
4067 /* Create a class which either implements on extends the designated
4068 class. The class bears an inaccessible name. */
4069 if (CLASS_INTERFACE (type_decl))
4071 /* It's OK to modify it here. It's been already used and
4072 shouldn't be reused */
4073 ctxp->interface_number = 1;
4074 /* Interfaces should presented as a list of WFLs */
4075 itf = build_tree_list (type_name, NULL_TREE);
4077 else
4078 super = type_name;
4081 class = create_class (ACC_FINAL, id, super, itf);
4083 /* We didn't know anything about the stuff. We register a dependence. */
4084 if (!type_decl)
4085 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
4087 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
4088 return class;
4091 /* Create a class in pass1 and return its decl. Return class
4092 interface's decl in pass 2. */
4094 static tree
4095 create_class (int flags, tree id, tree super, tree interfaces)
4097 tree raw_name = EXPR_WFL_NODE (id);
4098 tree class_id, decl;
4099 tree super_decl_type;
4101 /* Certain syntax errors are making SUPER be like ID. Avoid this
4102 case. */
4103 if (ctxp->class_err && id == super)
4104 super = NULL;
4106 class_id = parser_qualified_classname (raw_name);
4107 decl = IDENTIFIER_CLASS_VALUE (class_id);
4108 EXPR_WFL_NODE (id) = class_id;
4110 /* Basic check: scope, redefinition, modifiers */
4111 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4113 PUSH_ERROR ();
4114 return NULL_TREE;
4117 /* Suspend the current parsing context if we're parsing an inner
4118 class or an anonymous class. */
4119 if (CPC_INNER_P ())
4121 java_parser_context_suspend ();
4122 /* Interface members are public. */
4123 if (CLASS_INTERFACE (GET_CPC ()))
4124 flags |= ACC_PUBLIC;
4127 /* Push a new context for (static) initialized upon declaration fields */
4128 java_parser_context_push_initialized_field ();
4130 /* Class modifier check:
4131 - Allowed modifier (already done at that point)
4132 - abstract AND final forbidden
4133 - Public classes defined in the correct file */
4134 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4135 parse_error_context
4136 (id, "Class %qs can't be declared both abstract and final",
4137 IDENTIFIER_POINTER (raw_name));
4139 /* Create a new decl if DECL is NULL, otherwise fix it */
4140 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4142 /* If SUPER exists, use it, otherwise use Object */
4143 if (super)
4145 /* java.lang.Object can't extend anything. */
4146 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4148 parse_error_context (id, "%<java.lang.Object%> can't extend anything");
4149 return NULL_TREE;
4152 super_decl_type =
4153 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4155 else if (TREE_TYPE (decl) != object_type_node)
4156 super_decl_type = object_type_node;
4157 /* We're defining java.lang.Object */
4158 else
4159 super_decl_type = NULL_TREE;
4161 /* A class nested in an interface is implicitly static. */
4162 if (INNER_CLASS_DECL_P (decl)
4163 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4165 flags |= ACC_STATIC;
4168 /* Set super info and mark the class as complete. */
4169 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4170 ctxp->interface_number);
4171 ctxp->interface_number = 0;
4172 CLASS_COMPLETE_P (decl) = 1;
4173 add_superinterfaces (decl, interfaces);
4175 /* TYPE_VFIELD' is a compiler-generated field used to point to
4176 virtual function tables. In gcj, every class has a common base
4177 virtual function table in java.lang.object. */
4178 TYPE_VFIELD (TREE_TYPE (decl)) = TYPE_VFIELD (object_type_node);
4180 /* We keep the compilation unit imports in the class so that
4181 they can be used later to resolve type dependencies that
4182 aren't necessary to solve now. */
4183 TYPE_IMPORT_LIST (TREE_TYPE (decl)) = ctxp->import_list;
4184 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (decl)) = ctxp->import_demand_list;
4186 /* Add the private this$<n> field, Replicate final locals still in
4187 scope as private final fields mangled like val$<local_name>.
4188 This does not occur for top level (static) inner classes. */
4189 if (PURE_INNER_CLASS_DECL_P (decl))
4190 add_inner_class_fields (decl, current_function_decl);
4192 /* Eventually sets the @deprecated tag flag */
4193 CHECK_DEPRECATED (decl);
4195 /* Reset the anonymous class counter when declaring non inner classes */
4196 if (!INNER_CLASS_DECL_P (decl))
4197 anonymous_class_counter = 1;
4199 return decl;
4202 /* End a class declaration: register the statements used to create
4203 finit$ and <clinit>, pop the current class and resume the prior
4204 parser context if necessary. */
4206 static void
4207 end_class_declaration (int resume)
4209 /* If an error occurred, context weren't pushed and won't need to be
4210 popped by a resume. */
4211 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4213 if (GET_CPC () != error_mark_node)
4214 dump_java_tree (TDI_class, GET_CPC ());
4216 java_parser_context_pop_initialized_field ();
4217 POP_CPC ();
4218 if (resume && no_error_occurred)
4219 java_parser_context_resume ();
4221 /* We're ending a class declaration, this is a good time to reset
4222 the interface cout. Note that might have been already done in
4223 create_interface, but if at that time an inner class was being
4224 dealt with, the interface count was reset in a context created
4225 for the sake of handling inner classes declaration. */
4226 ctxp->interface_number = 0;
4229 static void
4230 add_inner_class_fields (tree class_decl, tree fct_decl)
4232 tree block, marker, f;
4234 f = add_field (TREE_TYPE (class_decl),
4235 build_current_thisn (TREE_TYPE (class_decl)),
4236 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4237 ACC_PRIVATE);
4238 FIELD_THISN (f) = 1;
4240 if (!fct_decl)
4241 return;
4243 for (block = GET_CURRENT_BLOCK (fct_decl);
4244 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4246 tree decl;
4247 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4249 tree name, pname;
4250 tree wfl, init, list;
4252 /* Avoid non final arguments. */
4253 if (!LOCAL_FINAL_P (decl))
4254 continue;
4256 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4257 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4258 wfl = build_wfl_node (name);
4259 init = build_wfl_node (pname);
4260 /* Build an initialization for the field: it will be
4261 initialized by a parameter added to finit$, bearing a
4262 mangled name of the field itself (param$<n>.) The
4263 parameter is provided to finit$ by the constructor
4264 invoking it (hence the constructor will also feature a
4265 hidden parameter, set to the value of the outer context
4266 local at the time the inner class is created.)
4268 Note: we take into account all possible locals that can
4269 be accessed by the inner class. It's actually not trivial
4270 to minimize these aliases down to the ones really
4271 used. One way to do that would be to expand all regular
4272 methods first, then finit$ to get a picture of what's
4273 used. It works with the exception that we would have to
4274 go back on all constructor invoked in regular methods to
4275 have their invocation reworked (to include the right amount
4276 of alias initializer parameters.)
4278 The only real way around, I think, is a first pass to
4279 identify locals really used in the inner class. We leave
4280 the flag FIELD_LOCAL_ALIAS_USED around for that future
4281 use.
4283 On the other hand, it only affect local inner classes,
4284 whose constructors (and finit$ call) will be featuring
4285 unnecessary arguments. It's easy for a developer to keep
4286 this number of parameter down by using the `final'
4287 keyword only when necessary. For the time being, we can
4288 issue a warning on unnecessary finals. FIXME */
4289 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4290 wfl, init);
4292 /* Register the field. The TREE_LIST holding the part
4293 initialized/initializer will be marked ARG_FINAL_P so
4294 that the created field can be marked
4295 FIELD_LOCAL_ALIAS. */
4296 list = build_tree_list (wfl, init);
4297 ARG_FINAL_P (list) = 1;
4298 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4302 if (!CPC_INITIALIZER_STMT (ctxp))
4303 return;
4305 /* If we ever registered an alias field, insert and marker to
4306 remember where the list ends. The second part of the list (the one
4307 featuring initialized fields) so it can be later reversed to
4308 enforce 8.5. The marker will be removed during that operation. */
4309 marker = build_tree_list (NULL_TREE, NULL_TREE);
4310 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4311 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4314 /* Can't use lookup_field () since we don't want to load the class and
4315 can't set the CLASS_LOADED_P flag */
4317 static tree
4318 find_field (tree class, tree name)
4320 tree decl;
4321 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4323 if (DECL_NAME (decl) == name)
4324 return decl;
4326 return NULL_TREE;
4329 /* Wrap around lookup_field that doesn't potentially upset the value
4330 of CLASS */
4332 static tree
4333 lookup_field_wrapper (tree class, tree name)
4335 tree type = class;
4336 tree decl = NULL_TREE;
4337 java_parser_context_save_global ();
4339 /* Last chance: if we're within the context of an inner class, we
4340 might be trying to access a local variable defined in an outer
4341 context. We try to look for it now. */
4342 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4344 tree new_name;
4345 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4346 decl = lookup_field (&type, new_name);
4347 if (decl && decl != error_mark_node)
4348 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4350 if (!decl || decl == error_mark_node)
4352 type = class;
4353 decl = lookup_field (&type, name);
4356 /* If the field still hasn't been found, try the next enclosing context. */
4357 if (!decl && INNER_CLASS_TYPE_P (class))
4359 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4360 decl = lookup_field_wrapper (outer_type, name);
4363 java_parser_context_restore_global ();
4364 return decl == error_mark_node ? NULL : decl;
4367 /* Find duplicate field within the same class declarations and report
4368 the error. Returns 1 if a duplicated field was found, 0
4369 otherwise. */
4371 static int
4372 duplicate_declaration_error_p (tree new_field_name, tree new_type, tree cl)
4374 /* This might be modified to work with method decl as well */
4375 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4376 if (decl)
4378 char *t1 = xstrdup (purify_type_name
4379 ((TREE_CODE (new_type) == POINTER_TYPE
4380 && TREE_TYPE (new_type) == NULL_TREE) ?
4381 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4382 lang_printable_name (new_type, 1)));
4383 /* The type may not have been completed by the time we report
4384 the error */
4385 char *t2 = xstrdup (purify_type_name
4386 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4387 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4388 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4389 lang_printable_name (TREE_TYPE (decl), 1)));
4390 parse_error_context
4391 (cl, "Duplicate variable declaration: %<%s %s%> was %<%s %s%> (%s:%d)",
4392 t1, IDENTIFIER_POINTER (new_field_name),
4393 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4394 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4395 free (t1);
4396 free (t2);
4397 return 1;
4399 return 0;
4402 /* Field registration routine. If TYPE doesn't exist, field
4403 declarations are linked to the undefined TYPE dependency list, to
4404 be later resolved in java_complete_class () */
4406 static void
4407 register_fields (int flags, tree type, tree variable_list)
4409 tree current, saved_type;
4410 tree class_type = NULL_TREE;
4411 location_t saved_location = input_location;
4412 int must_chain = 0;
4413 tree wfl = NULL_TREE;
4415 if (GET_CPC ())
4416 class_type = TREE_TYPE (GET_CPC ());
4418 if (!class_type || class_type == error_mark_node)
4419 return;
4421 /* If we're adding fields to interfaces, those fields are public,
4422 static, final */
4423 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4425 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4426 flags, ACC_PUBLIC, "interface field(s)");
4427 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4428 flags, ACC_STATIC, "interface field(s)");
4429 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4430 flags, ACC_FINAL, "interface field(s)");
4431 check_modifiers ("Illegal interface member modifier %qs", flags,
4432 INTERFACE_FIELD_MODIFIERS);
4433 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4436 /* Obtain a suitable type for resolution, if necessary */
4437 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4439 /* If TYPE is fully resolved and we don't have a reference, make one */
4440 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4442 for (current = variable_list, saved_type = type; current;
4443 current = TREE_CHAIN (current), type = saved_type)
4445 tree real_type;
4446 tree field_decl;
4447 tree cl = TREE_PURPOSE (current);
4448 tree init = TREE_VALUE (current);
4449 tree current_name = EXPR_WFL_NODE (cl);
4451 /* Can't declare non-final static fields in inner classes */
4452 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4453 && !(flags & ACC_FINAL))
4454 parse_error_context
4455 (cl, "Field %qs can't be static in inner class %qs unless it is final",
4456 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4457 lang_printable_name (class_type, 0));
4459 /* Process NAME, as it may specify extra dimension(s) for it */
4460 type = build_array_from_name (type, wfl, current_name, &current_name);
4462 /* Type adjustment. We may have just readjusted TYPE because
4463 the variable specified more dimensions. Make sure we have
4464 a reference if we can and don't have one already. Also
4465 change the name if we have an init. */
4466 if (type != saved_type)
4468 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4469 if (init)
4470 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4473 real_type = GET_REAL_TYPE (type);
4474 /* Check for redeclarations */
4475 if (duplicate_declaration_error_p (current_name, real_type, cl))
4476 continue;
4478 /* Set input_line to the line the field was found and create a
4479 declaration for it. Eventually sets the @deprecated tag flag. */
4480 #ifdef USE_MAPPED_LOCATION
4481 input_location = EXPR_LOCATION (cl);
4482 #else
4483 input_line = EXPR_WFL_LINENO (cl);
4484 #endif
4485 field_decl = add_field (class_type, current_name, real_type, flags);
4486 CHECK_DEPRECATED_NO_RESET (field_decl);
4488 /* If the field denotes a final instance variable, then we
4489 allocate a LANG_DECL_SPECIFIC part to keep track of its
4490 initialization. We also mark whether the field was
4491 initialized upon its declaration. We don't do that if the
4492 created field is an alias to a final local. */
4493 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4495 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4496 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4499 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4500 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4501 hide parameters to this inner class finit$ and
4502 constructors. It also means that the field isn't final per
4503 say. */
4504 if (ARG_FINAL_P (current))
4506 FIELD_LOCAL_ALIAS (field_decl) = 1;
4507 FIELD_FINAL (field_decl) = 0;
4510 /* Check if we must chain. */
4511 if (must_chain)
4512 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4514 /* If we have an initialization value tied to the field */
4515 if (init)
4517 /* The field is declared static */
4518 if (flags & ACC_STATIC)
4520 /* We include the field and its initialization part into
4521 a list used to generate <clinit>. After <clinit> is
4522 walked, field initializations will be processed and
4523 fields initialized with known constants will be taken
4524 out of <clinit> and have their DECL_INITIAL set
4525 appropriately. */
4526 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4527 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4528 if (TREE_OPERAND (init, 1)
4529 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4530 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4532 /* A non-static field declared with an immediate initialization is
4533 to be initialized in <init>, if any. This field is remembered
4534 to be processed at the time of the generation of <init>. */
4535 else
4537 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4538 SET_CPC_INITIALIZER_STMT (ctxp, init);
4540 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4541 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4545 CLEAR_DEPRECATED;
4546 input_location = saved_location;
4549 /* Generate finit$, using the list of initialized fields to populate
4550 its body. finit$'s parameter(s) list is adjusted to include the
4551 one(s) used to initialized the field(s) caching outer context
4552 local(s). */
4554 static tree
4555 generate_finit (tree class_type)
4557 int count = 0;
4558 tree list = TYPE_FINIT_STMT_LIST (class_type);
4559 tree mdecl, current, parms;
4561 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4562 class_type, NULL_TREE,
4563 &count);
4564 CRAFTED_PARAM_LIST_FIXUP (parms);
4565 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4566 finit_identifier_node, parms);
4567 fix_method_argument_names (parms, mdecl);
4568 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4569 mdecl, NULL_TREE);
4570 DECL_FUNCTION_NAP (mdecl) = count;
4571 start_artificial_method_body (mdecl);
4573 for (current = list; current; current = TREE_CHAIN (current))
4574 java_method_add_stmt (mdecl,
4575 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4576 current));
4577 end_artificial_method_body (mdecl);
4578 return mdecl;
4581 /* Generate a function to run the instance initialization code. The
4582 private method is called `instinit$'. Unless we're dealing with an
4583 anonymous class, we determine whether all ctors of CLASS_TYPE
4584 declare a checked exception in their `throws' clause in order to
4585 see whether it's necessary to encapsulate the instance initializer
4586 statements in a try/catch/rethrow sequence. */
4588 static tree
4589 generate_instinit (tree class_type)
4591 tree current;
4592 tree compound = NULL_TREE;
4593 tree parms = tree_cons (this_identifier_node,
4594 build_pointer_type (class_type), end_params_node);
4595 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4596 void_type_node,
4597 instinit_identifier_node, parms);
4599 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4600 mdecl, NULL_TREE);
4602 /* Gather all the statements in a compound */
4603 for (current = TYPE_II_STMT_LIST (class_type);
4604 current; current = TREE_CHAIN (current))
4605 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4607 /* We need to encapsulate COMPOUND by a try/catch statement to
4608 rethrow exceptions that might occur in the instance initializer.
4609 We do that only if all ctors of CLASS_TYPE are set to catch a
4610 checked exception. This doesn't apply to anonymous classes (since
4611 they don't have declared ctors.) */
4612 if (!ANONYMOUS_CLASS_P (class_type) &&
4613 ctors_unchecked_throws_clause_p (class_type))
4615 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4616 build1 (THROW_EXPR, NULL_TREE,
4617 build_wfl_node (wpv_id)));
4618 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4619 exception_type_node);
4622 start_artificial_method_body (mdecl);
4623 java_method_add_stmt (mdecl, compound);
4624 end_artificial_method_body (mdecl);
4626 return mdecl;
4629 /* FIXME */
4630 static tree
4631 build_instinit_invocation (tree class_type)
4633 tree to_return = NULL_TREE;
4635 if (TYPE_II_STMT_LIST (class_type))
4637 tree parm = build_tree_list (NULL_TREE,
4638 build_wfl_node (this_identifier_node));
4639 to_return =
4640 build_method_invocation (build_wfl_node (instinit_identifier_node),
4641 parm);
4643 return to_return;
4646 /* Shared across method_declarator and method_header to remember the
4647 patch stage that was reached during the declaration of the method.
4648 A method DECL is built differently is there is no patch
4649 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4650 pending on the currently defined method. */
4652 static int patch_stage;
4654 /* Check the method declaration and add the method to its current
4655 class. If the argument list is known to contain incomplete types,
4656 the method is partially added and the registration will be resume
4657 once the method arguments resolved. If TYPE is NULL, we're dealing
4658 with a constructor. */
4660 static tree
4661 method_header (int flags, tree type, tree mdecl, tree throws)
4663 tree type_wfl = NULL_TREE;
4664 tree meth_name = NULL_TREE;
4665 tree current, orig_arg, this_class = NULL;
4666 tree id, meth;
4667 location_t saved_location;
4668 int constructor_ok = 0, must_chain;
4669 int count;
4671 if (mdecl == error_mark_node)
4672 return error_mark_node;
4673 meth = TREE_VALUE (mdecl);
4674 id = TREE_PURPOSE (mdecl);
4676 check_modifiers_consistency (flags);
4678 if (GET_CPC ())
4679 this_class = TREE_TYPE (GET_CPC ());
4681 if (!this_class || this_class == error_mark_node)
4682 return NULL_TREE;
4684 /* There are some forbidden modifiers for an abstract method and its
4685 class must be abstract as well. */
4686 if (type && (flags & ACC_ABSTRACT))
4688 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4689 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4690 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4691 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4692 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4693 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4694 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4695 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4696 parse_error_context
4697 (id,
4698 "Class %qs must be declared abstract to define abstract method %qs",
4699 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4700 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4703 /* A native method can't be strictfp. */
4704 if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4705 parse_error_context (id, "native method %qs can't be strictfp",
4706 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4707 /* No such thing as a transient or volatile method. */
4708 if ((flags & ACC_TRANSIENT))
4709 parse_error_context (id, "method %qs can't be transient",
4710 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4711 if ((flags & ACC_VOLATILE))
4712 parse_error_context (id, "method %qs can't be volatile",
4713 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4715 /* Things to be checked when declaring a constructor */
4716 if (!type)
4718 int ec = java_error_count;
4719 /* 8.6: Constructor declarations: we might be trying to define a
4720 method without specifying a return type. */
4721 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4722 parse_error_context
4723 (id, "Invalid method declaration, return type required");
4724 /* 8.6.3: Constructor modifiers */
4725 else
4727 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4728 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4729 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4730 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4731 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4732 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4734 /* If we found error here, we don't consider it's OK to tread
4735 the method definition as a constructor, for the rest of this
4736 function */
4737 if (ec == java_error_count)
4738 constructor_ok = 1;
4741 /* Method declared within the scope of an interface are implicitly
4742 abstract and public. Conflicts with other erroneously provided
4743 modifiers are checked right after. */
4745 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4747 /* If FLAGS isn't set because of a modifier, turn the
4748 corresponding modifier WFL to NULL so we issue a warning on
4749 the obsolete use of the modifier */
4750 if (!(flags & ACC_PUBLIC))
4751 MODIFIER_WFL (PUBLIC_TK) = NULL;
4752 if (!(flags & ACC_ABSTRACT))
4753 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4754 flags |= ACC_PUBLIC;
4755 flags |= ACC_ABSTRACT;
4758 /* Inner class can't declare static methods */
4759 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4761 parse_error_context
4762 (id, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
4763 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4764 lang_printable_name (this_class, 0));
4767 /* Modifiers context reset moved up, so abstract method declaration
4768 modifiers can be later checked. */
4770 /* Set constructor returned type to void and method name to <init>,
4771 unless we found an error identifier the constructor (in which
4772 case we retain the original name) */
4773 if (!type)
4775 type = void_type_node;
4776 if (constructor_ok)
4777 meth_name = init_identifier_node;
4779 else
4780 meth_name = EXPR_WFL_NODE (id);
4782 /* Do the returned type resolution and registration if necessary */
4783 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4785 if (meth_name)
4786 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4787 EXPR_WFL_NODE (id) = meth_name;
4788 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4790 if (must_chain)
4792 patch_stage = JDEP_METHOD_RETURN;
4793 register_incomplete_type (patch_stage, type_wfl, id, type);
4794 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4796 else
4797 TREE_TYPE (meth) = type;
4799 saved_location = input_location;
4800 /* When defining an abstract or interface method, the curly
4801 bracket at level 1 doesn't exist because there is no function
4802 body */
4803 #ifdef USE_MAPPED_LOCATION
4804 input_location = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4805 EXPR_LOCATION (id));
4806 #else
4807 input_line = (ctxp->first_ccb_indent1 ? (int) ctxp->first_ccb_indent1 :
4808 EXPR_WFL_LINENO (id));
4809 #endif
4811 /* Remember the original argument list */
4812 orig_arg = TYPE_ARG_TYPES (meth);
4814 if (patch_stage) /* includes ret type and/or all args */
4816 jdep *jdep;
4817 meth = add_method_1 (this_class, flags, meth_name, meth);
4818 /* Patch for the return type */
4819 if (patch_stage == JDEP_METHOD_RETURN)
4821 jdep = CLASSD_LAST (ctxp->classd_list);
4822 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4824 /* This is the stop JDEP. METH allows the function's signature
4825 to be computed. */
4826 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4828 else
4829 meth = add_method (this_class, flags, meth_name,
4830 build_java_signature (meth));
4832 /* Remember final parameters */
4833 MARK_FINAL_PARMS (meth, orig_arg);
4835 /* Fix the method argument list so we have the argument name
4836 information */
4837 fix_method_argument_names (orig_arg, meth);
4839 /* Register the parameter number and re-install the current line
4840 number */
4841 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4842 input_location = saved_location;
4844 /* Register exception specified by the `throws' keyword for
4845 resolution and set the method decl appropriate field to the list.
4846 Note: the grammar ensures that what we get here are class
4847 types. */
4848 if (throws)
4850 throws = nreverse (throws);
4851 for (current = throws; current; current = TREE_CHAIN (current))
4853 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4854 NULL_TREE, NULL_TREE);
4855 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4856 &TREE_VALUE (current);
4858 DECL_FUNCTION_THROWS (meth) = throws;
4861 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4862 DECL_FUNCTION_WFL (meth) = id;
4864 /* Set the flag if we correctly processed a constructor */
4865 if (constructor_ok)
4867 DECL_CONSTRUCTOR_P (meth) = 1;
4868 /* Compute and store the number of artificial parameters declared
4869 for this constructor */
4870 for (count = 0, current = TYPE_FIELDS (this_class); current;
4871 current = TREE_CHAIN (current))
4872 if (FIELD_LOCAL_ALIAS (current))
4873 count++;
4874 DECL_FUNCTION_NAP (meth) = count;
4877 /* Eventually set the @deprecated tag flag */
4878 CHECK_DEPRECATED (meth);
4880 return meth;
4883 static void
4884 fix_method_argument_names (tree orig_arg, tree meth)
4886 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4887 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4889 TREE_PURPOSE (arg) = this_identifier_node;
4890 arg = TREE_CHAIN (arg);
4892 while (orig_arg != end_params_node)
4894 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4895 orig_arg = TREE_CHAIN (orig_arg);
4896 arg = TREE_CHAIN (arg);
4900 /* Complete the method declaration with METHOD_BODY. */
4902 static void
4903 finish_method_declaration (tree method_body)
4905 int flags;
4907 if (!current_function_decl)
4908 return;
4910 flags = get_access_flags_from_decl (current_function_decl);
4912 /* 8.4.5 Method Body */
4913 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4915 tree name = DECL_NAME (current_function_decl);
4916 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4917 "%s method %qs can't have a body defined",
4918 (METHOD_NATIVE (current_function_decl) ?
4919 "Native" : "Abstract"),
4920 IDENTIFIER_POINTER (name));
4921 method_body = NULL_TREE;
4923 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4925 tree name = DECL_NAME (current_function_decl);
4926 parse_error_context
4927 (DECL_FUNCTION_WFL (current_function_decl),
4928 "Non native and non abstract method %qs must have a body defined",
4929 IDENTIFIER_POINTER (name));
4930 method_body = NULL_TREE;
4933 if (flag_emit_class_files && method_body
4934 && TREE_CODE (method_body) == NOP_EXPR
4935 && TREE_TYPE (current_function_decl)
4936 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4937 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4939 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4940 maybe_absorb_scoping_blocks ();
4941 /* Exit function's body */
4942 exit_block ();
4943 /* Merge last line of the function with first line, directly in the
4944 function decl. It will be used to emit correct debug info. */
4945 DECL_FUNCTION_LAST_LINE (current_function_decl) = ctxp->last_ccb_indent1;
4947 /* Since function's argument's list are shared, reset the
4948 ARG_FINAL_P parameter that might have been set on some of this
4949 function parameters. */
4950 UNMARK_FINAL_PARMS (current_function_decl);
4952 /* So we don't have an irrelevant function declaration context for
4953 the next static block we'll see. */
4954 current_function_decl = NULL_TREE;
4957 /* Build a an error message for constructor circularity errors. */
4959 static char *
4960 constructor_circularity_msg (tree from, tree to)
4962 static char string [4096];
4963 char *t = xstrdup (lang_printable_name (from, 2));
4964 sprintf (string, "'%s' invokes '%s'", t, lang_printable_name (to, 2));
4965 free (t);
4966 return string;
4969 /* Verify a circular call to METH. Return 1 if an error is found, 0
4970 otherwise. */
4972 static GTY(()) tree vcc_list;
4973 static int
4974 verify_constructor_circularity (tree meth, tree current)
4976 tree c;
4978 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4980 if (TREE_VALUE (c) == meth)
4982 char *t;
4983 if (vcc_list)
4985 tree liste;
4986 vcc_list = nreverse (vcc_list);
4987 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4989 parse_error_context
4990 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4991 constructor_circularity_msg
4992 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4993 java_error_count--;
4996 t = xstrdup (lang_printable_name (meth, 2));
4997 parse_error_context (TREE_PURPOSE (c),
4998 "%s: recursive invocation of constructor %qs",
4999 constructor_circularity_msg (current, meth), t);
5000 free (t);
5001 vcc_list = NULL_TREE;
5002 return 1;
5005 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
5007 vcc_list = tree_cons (c, current, vcc_list);
5008 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
5009 return 1;
5010 vcc_list = TREE_CHAIN (vcc_list);
5012 return 0;
5015 /* Check modifiers that can be declared but exclusively */
5017 static void
5018 check_modifiers_consistency (int flags)
5020 int acc_count = 0;
5021 tree cl = NULL_TREE;
5023 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
5024 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
5025 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
5026 if (acc_count > 1)
5027 parse_error_context
5028 (cl, "Inconsistent member declaration. At most one of %<public%>, %<private%>, or %<protected%> may be specified");
5030 acc_count = 0;
5031 cl = NULL_TREE;
5032 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
5033 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
5034 if (acc_count > 1)
5035 parse_error_context (cl,
5036 "Inconsistent member declaration. At most one of %<final%> or %<volatile%> may be specified");
5039 /* Check the methode header METH for abstract specifics features */
5041 static void
5042 check_abstract_method_header (tree meth)
5044 int flags = get_access_flags_from_decl (meth);
5046 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
5047 ACC_ABSTRACT, "abstract method",
5048 IDENTIFIER_POINTER (DECL_NAME (meth)));
5049 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
5050 ACC_PUBLIC, "abstract method",
5051 IDENTIFIER_POINTER (DECL_NAME (meth)));
5053 check_modifiers ("Illegal modifier %qs for interface method",
5054 flags, INTERFACE_METHOD_MODIFIERS);
5057 /* Create a FUNCTION_TYPE node and start augmenting it with the
5058 declared function arguments. Arguments type that can't be resolved
5059 are left as they are, but the returned node is marked as containing
5060 incomplete types. */
5062 static tree
5063 method_declarator (tree id, tree list)
5065 tree arg_types = NULL_TREE, current, node;
5066 tree meth = make_node (FUNCTION_TYPE);
5067 jdep *jdep;
5069 patch_stage = JDEP_NO_PATCH;
5071 if (GET_CPC () == error_mark_node)
5072 return error_mark_node;
5074 /* If we're dealing with an inner class constructor, we hide the
5075 this$<n> decl in the name field of its parameter declaration. We
5076 also might have to hide the outer context local alias
5077 initializers. Not done when the class is a toplevel class. */
5078 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
5079 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
5081 tree aliases_list, type, thisn;
5082 /* First the aliases, linked to the regular parameters */
5083 aliases_list =
5084 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
5085 TREE_TYPE (GET_CPC ()),
5086 NULL_TREE, NULL);
5087 list = chainon (nreverse (aliases_list), list);
5089 /* Then this$<n> */
5090 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
5091 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
5092 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
5093 list);
5096 for (current = list; current; current = TREE_CHAIN (current))
5098 int must_chain = 0;
5099 tree wfl_name = TREE_PURPOSE (current);
5100 tree type = TREE_VALUE (current);
5101 tree name = EXPR_WFL_NODE (wfl_name);
5102 tree already, arg_node;
5103 tree type_wfl = NULL_TREE;
5104 tree real_type;
5106 /* Obtain a suitable type for resolution, if necessary */
5107 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5109 /* Process NAME, as it may specify extra dimension(s) for it */
5110 type = build_array_from_name (type, type_wfl, name, &name);
5111 EXPR_WFL_NODE (wfl_name) = name;
5113 real_type = GET_REAL_TYPE (type);
5114 if (TREE_CODE (real_type) == RECORD_TYPE)
5116 real_type = promote_type (real_type);
5117 if (TREE_CODE (type) == TREE_LIST)
5118 TREE_PURPOSE (type) = real_type;
5121 /* Check redefinition */
5122 for (already = arg_types; already; already = TREE_CHAIN (already))
5123 if (TREE_PURPOSE (already) == name)
5125 parse_error_context
5126 (wfl_name, "Variable %qs is used more than once in the argument list of method %qs",
5127 IDENTIFIER_POINTER (name),
5128 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5129 break;
5132 /* If we've an incomplete argument type, we know there is a location
5133 to patch when the type get resolved, later. */
5134 jdep = NULL;
5135 if (must_chain)
5137 patch_stage = JDEP_METHOD;
5138 type = register_incomplete_type (patch_stage,
5139 type_wfl, wfl_name, type);
5140 jdep = CLASSD_LAST (ctxp->classd_list);
5141 JDEP_MISC (jdep) = id;
5144 /* The argument node: a name and a (possibly) incomplete type. */
5145 arg_node = build_tree_list (name, real_type);
5146 /* Remember arguments declared final. */
5147 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5149 if (jdep)
5150 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5151 TREE_CHAIN (arg_node) = arg_types;
5152 arg_types = arg_node;
5154 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5155 node = build_tree_list (id, meth);
5156 return node;
5159 static int
5160 unresolved_type_p (tree wfl, tree *returned)
5162 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5164 if (returned)
5166 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5167 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5168 *returned = TREE_TYPE (decl);
5169 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5170 *returned = TREE_TYPE (GET_CPC ());
5171 else
5172 *returned = NULL_TREE;
5174 return 1;
5176 if (returned)
5177 *returned = wfl;
5178 return 0;
5181 /* From NAME, build a qualified identifier node using the
5182 qualification from the current package definition. */
5184 static tree
5185 parser_qualified_classname (tree name)
5187 tree nested_class_name;
5189 if ((nested_class_name = maybe_make_nested_class_name (name)))
5190 return nested_class_name;
5192 if (ctxp->package)
5193 return merge_qualified_name (ctxp->package, name);
5194 else
5195 return name;
5198 /* Called once the type a interface extends is resolved. Returns 0 if
5199 everything is OK. */
5201 static int
5202 parser_check_super_interface (tree super_decl, tree this_decl, tree this_wfl)
5204 tree super_type = TREE_TYPE (super_decl);
5206 /* Has to be an interface */
5207 if (!CLASS_INTERFACE (super_decl))
5209 parse_error_context
5210 (this_wfl, "%s %qs can't implement/extend %s %qs",
5211 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5212 "Interface" : "Class"),
5213 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5214 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5215 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5216 return 1;
5219 /* Check top-level interface access. Inner classes are subject to member
5220 access rules (6.6.1). */
5221 if (! INNER_CLASS_P (super_type)
5222 && check_pkg_class_access (DECL_NAME (super_decl),
5223 NULL_TREE, true, this_decl))
5224 return 1;
5226 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5227 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5228 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5229 return 0;
5232 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5233 0 if everything is OK. */
5235 static int
5236 parser_check_super (tree super_decl, tree this_decl, tree wfl)
5238 tree super_type = TREE_TYPE (super_decl);
5240 /* SUPER should be a CLASS (neither an array nor an interface) */
5241 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5243 parse_error_context
5244 (wfl, "Class %qs can't subclass %s %qs",
5245 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5246 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5247 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5248 return 1;
5251 if (CLASS_FINAL (TYPE_NAME (super_type)))
5253 parse_error_context (wfl, "Can't subclass final classes: %s",
5254 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5255 return 1;
5258 /* Check top-level class scope. Inner classes are subject to member access
5259 rules (6.6.1). */
5260 if (! INNER_CLASS_P (super_type)
5261 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true, NULL_TREE)))
5262 return 1;
5264 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5265 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5266 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5267 return 0;
5270 /* Create a new dependency list and link it (in a LIFO manner) to the
5271 CTXP list of type dependency list. */
5273 static void
5274 create_jdep_list (struct parser_ctxt *ctxp)
5276 jdeplist *new = xmalloc (sizeof (jdeplist));
5277 new->first = new->last = NULL;
5278 new->next = ctxp->classd_list;
5279 ctxp->classd_list = new;
5282 static jdeplist *
5283 reverse_jdep_list (struct parser_ctxt *ctxp)
5285 jdeplist *prev = NULL, *current, *next;
5286 for (current = ctxp->classd_list; current; current = next)
5288 next = current->next;
5289 current->next = prev;
5290 prev = current;
5292 return prev;
5295 /* Create a fake pointer based on the ID stored in
5296 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5297 registered again. */
5299 static tree
5300 obtain_incomplete_type (tree type_name)
5302 tree ptr = NULL_TREE, name;
5304 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5305 name = EXPR_WFL_NODE (type_name);
5306 else if (INCOMPLETE_TYPE_P (type_name))
5307 name = TYPE_NAME (type_name);
5308 else
5309 abort ();
5311 /* Workaround from build_pointer_type for incomplete types. */
5312 BUILD_PTR_FROM_NAME (ptr, name);
5313 TYPE_MODE (ptr) = ptr_mode;
5314 layout_type (ptr);
5316 return ptr;
5319 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5320 non NULL instead of computing a new fake type based on WFL. The new
5321 dependency is inserted in the current type dependency list, in FIFO
5322 manner. */
5324 static tree
5325 register_incomplete_type (int kind, tree wfl, tree decl, tree ptr)
5327 jdep *new = xmalloc (sizeof (jdep));
5329 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5330 ptr = obtain_incomplete_type (wfl);
5332 JDEP_KIND (new) = kind;
5333 JDEP_DECL (new) = decl;
5334 JDEP_TO_RESOLVE (new) = ptr;
5335 JDEP_WFL (new) = wfl;
5336 JDEP_CHAIN (new) = NULL;
5337 JDEP_MISC (new) = NULL_TREE;
5338 /* For some dependencies, set the enclosing class of the current
5339 class to be the enclosing context */
5340 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS || kind == JDEP_SUPER)
5341 && GET_ENCLOSING_CPC ())
5342 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5343 else
5344 JDEP_ENCLOSING (new) = GET_CPC ();
5345 JDEP_GET_PATCH (new) = (tree *)NULL;
5347 JDEP_INSERT (ctxp->classd_list, new);
5349 return ptr;
5352 /* This checks for circular references with innerclasses. We start
5353 from SOURCE and should never reach TARGET. Extended/implemented
5354 types in SOURCE have their enclosing context checked not to reach
5355 TARGET. When the last enclosing context of SOURCE is reached, its
5356 extended/implemented types are also checked not to reach TARGET.
5357 In case of error, WFL of the offending type is returned; NULL_TREE
5358 otherwise. */
5360 static tree
5361 check_inner_circular_reference (tree source, tree target)
5363 tree base_binfo;
5364 tree ctx, cl;
5365 int i;
5367 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (source), i, base_binfo); i++)
5369 tree su;
5371 /* We can end up with a NULL_TREE or an incomplete type here if
5372 we encountered previous type resolution errors. It's safe to
5373 simply ignore these cases. */
5374 su = BINFO_TYPE (base_binfo);
5375 if (INCOMPLETE_TYPE_P (su))
5376 continue;
5378 if (inherits_from_p (su, target))
5379 return lookup_cl (TYPE_NAME (su));
5381 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5383 /* An enclosing context shouldn't be TARGET */
5384 if (ctx == TYPE_NAME (target))
5385 return lookup_cl (TYPE_NAME (su));
5387 /* When we reach the enclosing last context, start a check
5388 on it, with the same target */
5389 if (! DECL_CONTEXT (ctx) &&
5390 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5391 return cl;
5394 return NULL_TREE;
5397 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5398 offending type if a circularity is detected. NULL_TREE is returned
5399 otherwise. TYPE can be an interface or a class. */
5401 static tree
5402 check_circular_reference (tree type)
5404 tree base_binfo;
5405 int i;
5407 if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
5408 return NULL_TREE;
5410 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5412 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5413 return lookup_cl (TYPE_NAME (type));
5414 return NULL_TREE;
5417 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo); i++)
5419 if (BINFO_TYPE (base_binfo) != object_type_node
5420 && interface_of_p (type, BINFO_TYPE (base_binfo)))
5421 return lookup_cl (TYPE_NAME (BINFO_TYPE (base_binfo)));
5423 return NULL_TREE;
5426 void
5427 java_check_circular_reference (void)
5429 tree current;
5430 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5432 tree type = TREE_TYPE (current);
5433 tree cl;
5435 cl = check_circular_reference (type);
5436 if (! cl)
5437 cl = check_inner_circular_reference (type, type);
5438 if (cl)
5439 parse_error_context (cl, "Cyclic class inheritance%s",
5440 (cyclic_inheritance_report ?
5441 cyclic_inheritance_report : ""));
5445 /* Augment the parameter list PARM with parameters crafted to
5446 initialize outer context locals aliases. Through ARTIFICIAL, a
5447 count is kept of the number of crafted parameters. MODE governs
5448 what eventually gets created: something suitable for a function
5449 creation or a function invocation, either the constructor or
5450 finit$. */
5452 static tree
5453 build_alias_initializer_parameter_list (int mode, tree class_type, tree parm,
5454 int *artificial)
5456 tree field;
5457 tree additional_parms = NULL_TREE;
5459 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5460 if (FIELD_LOCAL_ALIAS (field))
5462 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5463 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5464 tree mangled_id;
5466 switch (mode)
5468 case AIPL_FUNCTION_DECLARATION:
5469 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5470 &buffer [4]);
5471 purpose = build_wfl_node (mangled_id);
5472 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5473 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5474 else
5475 value = TREE_TYPE (field);
5476 break;
5478 case AIPL_FUNCTION_CREATION:
5479 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5480 &buffer [4]);
5481 value = TREE_TYPE (field);
5482 break;
5484 case AIPL_FUNCTION_FINIT_INVOCATION:
5485 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5486 &buffer [4]);
5487 /* Now, this is wrong. purpose should always be the NAME
5488 of something and value its matching value (decl, type,
5489 etc...) FIXME -- but there is a lot to fix. */
5491 /* When invoked for this kind of operation, we already
5492 know whether a field is used or not. */
5493 purpose = TREE_TYPE (field);
5494 value = build_wfl_node (mangled_id);
5495 break;
5497 case AIPL_FUNCTION_CTOR_INVOCATION:
5498 /* There are two case: the constructor invocation happens
5499 outside the local inner, in which case, locales from the outer
5500 context are directly used.
5502 Otherwise, we fold to using the alias directly. */
5503 if (class_type == current_class)
5504 value = field;
5505 else
5507 name = get_identifier (&buffer[4]);
5508 value = IDENTIFIER_LOCAL_VALUE (name);
5510 break;
5512 additional_parms = tree_cons (purpose, value, additional_parms);
5513 if (artificial)
5514 *artificial +=1;
5516 if (additional_parms)
5518 if (ANONYMOUS_CLASS_P (class_type)
5519 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5520 additional_parms = nreverse (additional_parms);
5521 parm = chainon (additional_parms, parm);
5524 return parm;
5527 /* Craft a constructor for CLASS_DECL -- what we should do when none
5528 where found. ARGS is non NULL when a special signature must be
5529 enforced. This is the case for anonymous classes. */
5531 static tree
5532 craft_constructor (tree class_decl, tree args)
5534 tree class_type = TREE_TYPE (class_decl);
5535 tree parm = NULL_TREE;
5536 /* Inherit access flags for the constructor from its enclosing class. */
5537 int valid_ctor_flags = ACC_PUBLIC | ACC_PROTECTED | ACC_PRIVATE;
5538 int flags = (get_access_flags_from_decl (class_decl) & valid_ctor_flags);
5539 int i = 0, artificial = 0;
5540 tree decl, ctor_name;
5541 char buffer [80];
5543 ctor_name = init_identifier_node;
5545 /* If we're dealing with an inner class constructor, we hide the
5546 this$<n> decl in the name field of its parameter declaration. */
5547 if (PURE_INNER_CLASS_TYPE_P (class_type))
5549 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5550 parm = tree_cons (build_current_thisn (class_type),
5551 build_pointer_type (type), parm);
5553 /* Some more arguments to be hidden here. The values of the local
5554 variables of the outer context that the inner class needs to see. */
5555 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5556 class_type, parm,
5557 &artificial);
5560 /* Then if there are any args to be enforced, enforce them now */
5561 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5563 /* If we see a `void *', we need to change it to Object. */
5564 if (TREE_VALUE (args) == TREE_TYPE (null_pointer_node))
5565 TREE_VALUE (args) = object_ptr_type_node;
5567 sprintf (buffer, "parm%d", i++);
5568 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5571 CRAFTED_PARAM_LIST_FIXUP (parm);
5572 decl = create_artificial_method (class_type, flags, void_type_node,
5573 ctor_name, parm);
5574 fix_method_argument_names (parm, decl);
5575 /* Now, mark the artificial parameters. */
5576 DECL_FUNCTION_NAP (decl) = artificial;
5577 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5578 DECL_INLINE (decl) = 1;
5579 return decl;
5583 /* Fix the constructors. This will be called right after circular
5584 references have been checked. It is necessary to fix constructors
5585 early even if no code generation will take place for that class:
5586 some generated constructor might be required by the class whose
5587 compilation triggered this one to be simply loaded. */
5589 void
5590 java_fix_constructors (void)
5592 tree current;
5594 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5596 tree class_type = TREE_TYPE (current);
5597 int saw_ctor = 0;
5598 tree decl;
5600 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5601 continue;
5603 output_class = current_class = class_type;
5604 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5606 if (DECL_CONSTRUCTOR_P (decl))
5608 fix_constructors (decl);
5609 saw_ctor = 1;
5613 /* Anonymous class constructor can't be generated that early. */
5614 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5615 craft_constructor (current, NULL_TREE);
5619 /* safe_layout_class just makes sure that we can load a class without
5620 disrupting the current_class, input_file, input_line, etc, information
5621 about the class processed currently. */
5623 void
5624 safe_layout_class (tree class)
5626 tree save_current_class = current_class;
5627 location_t save_location = input_location;
5629 layout_class (class);
5631 current_class = save_current_class;
5632 input_location = save_location;
5635 static tree
5636 jdep_resolve_class (jdep *dep)
5638 tree decl;
5640 if (JDEP_RESOLVED_P (dep))
5641 decl = JDEP_RESOLVED_DECL (dep);
5642 else
5644 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5645 JDEP_DECL (dep), JDEP_WFL (dep));
5646 JDEP_RESOLVED (dep, decl);
5647 /* If there is no WFL, that's ok. We generate this warning
5648 elsewhere. */
5649 if (decl && JDEP_WFL (dep) != NULL_TREE)
5650 check_deprecation (JDEP_WFL (dep), decl);
5653 if (!decl)
5654 complete_class_report_errors (dep);
5655 else if (INNER_CLASS_DECL_P (decl))
5657 tree inner = TREE_TYPE (decl);
5658 if (! CLASS_LOADED_P (inner))
5660 safe_layout_class (inner);
5661 if (TYPE_SIZE (inner) == error_mark_node)
5662 TYPE_SIZE (inner) = NULL_TREE;
5664 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5666 return decl;
5669 /* Complete unsatisfied class declaration and their dependencies */
5671 void
5672 java_complete_class (void)
5674 tree cclass;
5675 jdeplist *cclassd;
5676 int error_found;
5677 tree type;
5679 /* Process imports */
5680 process_imports ();
5682 /* Reverse things so we have the right order */
5683 ctxp->class_list = nreverse (ctxp->class_list);
5684 ctxp->classd_list = reverse_jdep_list (ctxp);
5686 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5687 cclass && cclassd;
5688 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5690 jdep *dep;
5692 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5694 tree decl;
5695 if (!(decl = jdep_resolve_class (dep)))
5696 continue;
5698 /* Now it's time to patch */
5699 switch (JDEP_KIND (dep))
5701 case JDEP_SUPER:
5702 /* Simply patch super */
5703 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5704 continue;
5705 BINFO_TYPE (BINFO_BASE_BINFO
5706 (TYPE_BINFO (TREE_TYPE (JDEP_DECL (dep))), 0))
5707 = TREE_TYPE (decl);
5708 break;
5710 case JDEP_FIELD:
5712 /* We do part of the job done in add_field */
5713 tree field_decl = JDEP_DECL (dep);
5714 tree field_type = TREE_TYPE (decl);
5715 if (TREE_CODE (field_type) == RECORD_TYPE)
5716 field_type = promote_type (field_type);
5717 TREE_TYPE (field_decl) = field_type;
5718 DECL_ALIGN (field_decl) = 0;
5719 DECL_USER_ALIGN (field_decl) = 0;
5720 layout_decl (field_decl, 0);
5721 SOURCE_FRONTEND_DEBUG
5722 (("Completed field/var decl '%s' with '%s'",
5723 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5724 IDENTIFIER_POINTER (DECL_NAME (decl))));
5725 break;
5727 case JDEP_METHOD: /* We start patching a method */
5728 case JDEP_METHOD_RETURN:
5729 error_found = 0;
5730 while (1)
5732 if (decl)
5734 type = TREE_TYPE(decl);
5735 if (TREE_CODE (type) == RECORD_TYPE)
5736 type = promote_type (type);
5737 JDEP_APPLY_PATCH (dep, type);
5738 SOURCE_FRONTEND_DEBUG
5739 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5740 "Completing fct '%s' with ret type '%s'":
5741 "Completing arg '%s' with type '%s'"),
5742 IDENTIFIER_POINTER (EXPR_WFL_NODE
5743 (JDEP_DECL_WFL (dep))),
5744 IDENTIFIER_POINTER (DECL_NAME (decl))));
5746 else
5747 error_found = 1;
5748 dep = JDEP_CHAIN (dep);
5749 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5750 break;
5751 else
5752 decl = jdep_resolve_class (dep);
5754 if (!error_found)
5756 tree mdecl = JDEP_DECL (dep), signature;
5757 /* Recompute and reset the signature, check first that
5758 all types are now defined. If they're not,
5759 don't build the signature. */
5760 if (check_method_types_complete (mdecl))
5762 signature = build_java_signature (TREE_TYPE (mdecl));
5763 set_java_signature (TREE_TYPE (mdecl), signature);
5766 else
5767 continue;
5768 break;
5770 case JDEP_INTERFACE:
5771 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5772 JDEP_WFL (dep)))
5773 continue;
5774 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5775 break;
5777 case JDEP_PARM:
5778 case JDEP_VARIABLE:
5779 type = TREE_TYPE(decl);
5780 if (TREE_CODE (type) == RECORD_TYPE)
5781 type = promote_type (type);
5782 JDEP_APPLY_PATCH (dep, type);
5783 break;
5785 case JDEP_TYPE:
5786 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5787 SOURCE_FRONTEND_DEBUG
5788 (("Completing a random type dependency on a '%s' node",
5789 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5790 break;
5792 case JDEP_EXCEPTION:
5793 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5794 SOURCE_FRONTEND_DEBUG
5795 (("Completing '%s' 'throws' argument node",
5796 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5797 break;
5799 case JDEP_ANONYMOUS:
5800 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5801 break;
5803 default:
5804 abort ();
5808 return;
5811 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5812 array. */
5814 static tree
5815 resolve_class (tree enclosing, tree class_type, tree decl, tree cl)
5817 tree tname = TYPE_NAME (class_type);
5818 tree resolved_type = TREE_TYPE (class_type);
5819 int array_dims = 0;
5820 tree resolved_type_decl;
5822 if (resolved_type != NULL_TREE)
5824 tree resolved_type_decl = TYPE_NAME (resolved_type);
5825 if (resolved_type_decl == NULL_TREE
5826 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5828 resolved_type_decl = build_decl (TYPE_DECL,
5829 TYPE_NAME (class_type),
5830 resolved_type);
5832 return resolved_type_decl;
5835 /* 1- Check to see if we have an array. If true, find what we really
5836 want to resolve */
5837 if ((array_dims = build_type_name_from_array_name (tname,
5838 &TYPE_NAME (class_type))))
5839 WFL_STRIP_BRACKET (cl, cl);
5841 /* 2- Resolve the bare type */
5842 if (!(resolved_type_decl = do_resolve_class (enclosing, NULL_TREE, class_type,
5843 decl, cl)))
5844 return NULL_TREE;
5845 resolved_type = TREE_TYPE (resolved_type_decl);
5847 /* 3- If we have an array, reconstruct the array down to its nesting */
5848 if (array_dims)
5850 for (; array_dims; array_dims--)
5851 resolved_type = build_java_array_type (resolved_type, -1);
5852 resolved_type_decl = TYPE_NAME (resolved_type);
5854 TREE_TYPE (class_type) = resolved_type;
5855 return resolved_type_decl;
5858 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5859 are used to report error messages; CL must either be NULL_TREE or a
5860 WFL wrapping a class. Do not try to replace TYPE_NAME (class_type)
5861 by a variable, since it is changed by find_in_imports{_on_demand}
5862 and (but it doesn't really matter) qualify_and_find. */
5864 tree
5865 do_resolve_class (tree enclosing, tree import_type, tree class_type, tree decl,
5866 tree cl)
5868 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5869 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5870 tree decl_result;
5871 htab_t circularity_hash;
5873 if (QUALIFIED_P (TYPE_NAME (class_type)))
5875 /* If the type name is of the form `Q . Id', then Q is either a
5876 package name or a class name. First we try to find Q as a
5877 class and then treat Id as a member type. If we can't find Q
5878 as a class then we fall through. */
5879 tree q, left, left_type, right;
5880 if (split_qualified_name (&left, &right, TYPE_NAME (class_type)) == 0)
5882 BUILD_PTR_FROM_NAME (left_type, left);
5883 q = do_resolve_class (enclosing, import_type, left_type, decl, cl);
5884 if (q)
5886 enclosing = q;
5887 saved_enclosing_type = TREE_TYPE (q);
5888 BUILD_PTR_FROM_NAME (class_type, right);
5893 if (enclosing)
5895 /* This hash table is used to register the classes we're going
5896 through when searching the current class as an inner class, in
5897 order to detect circular references. Remember to free it before
5898 returning the section 0- of this function. */
5899 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5900 NULL);
5902 /* 0- Search in the current class as an inner class.
5903 Maybe some code here should be added to load the class or
5904 something, at least if the class isn't an inner class and ended
5905 being loaded from class file. FIXME. */
5906 while (enclosing)
5908 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5909 &super, class_type);
5910 if (new_class_decl)
5911 break;
5913 /* If we haven't found anything because SUPER reached Object and
5914 ENCLOSING happens to be an innerclass, try the enclosing context. */
5915 if ((!super || super == object_type_node) &&
5916 enclosing && INNER_CLASS_DECL_P (enclosing))
5917 enclosing = DECL_CONTEXT (enclosing);
5918 else
5919 enclosing = NULL_TREE;
5922 htab_delete (circularity_hash);
5924 if (new_class_decl)
5925 return new_class_decl;
5928 /* 1- Check for the type in single imports. Look at enclosing classes and,
5929 if we're laying out a superclass, at the import list for the subclass.
5930 This will change TYPE_NAME() if something relevant is found. */
5931 if (import_type && TYPE_IMPORT_LIST (import_type))
5932 find_in_imports (import_type, class_type);
5933 find_in_imports (saved_enclosing_type, class_type);
5935 /* 2- And check for the type in the current compilation unit */
5936 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5938 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
5939 load_class (TYPE_NAME (class_type), 0);
5940 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5943 /* 3- Search according to the current package definition */
5944 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5946 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5947 TYPE_NAME (class_type))))
5948 return new_class_decl;
5951 /* 4- Check the import on demands. Don't allow bar.baz to be
5952 imported from foo.* */
5953 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5955 if (import_type
5956 && TYPE_IMPORT_DEMAND_LIST (import_type)
5957 && find_in_imports_on_demand (import_type, class_type))
5958 return NULL_TREE;
5959 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5960 return NULL_TREE;
5963 /* If found in find_in_imports_on_demand, the type has already been
5964 loaded. */
5965 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5966 return new_class_decl;
5968 /* 5- Check another compilation unit that bears the name of type */
5969 load_class (TYPE_NAME (class_type), 0);
5971 if (!cl)
5972 cl = lookup_cl (decl);
5974 /* If we don't have a value for CL, then we're being called recursively.
5975 We can't check package access just yet, but it will be taken care of
5976 by the caller. */
5977 if (cl)
5979 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true, NULL_TREE))
5980 return NULL_TREE;
5983 /* 6- Last call for a resolution */
5984 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5986 /* The final lookup might have registered a.b.c into a.b$c If we
5987 failed at the first lookup, progressively change the name if
5988 applicable and use the matching DECL instead. */
5989 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5991 char *separator;
5992 tree name = TYPE_NAME (class_type);
5993 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5995 strcpy (namebuffer, IDENTIFIER_POINTER (name));
5997 do {
5999 /* Reach the last '.', and if applicable, replace it by a `$' and
6000 see if this exists as a type. */
6001 if ((separator = strrchr (namebuffer, '.')))
6003 *separator = '$';
6004 name = get_identifier (namebuffer);
6005 decl_result = IDENTIFIER_CLASS_VALUE (name);
6007 } while (!decl_result && separator);
6009 return decl_result;
6012 static tree
6013 qualify_and_find (tree class_type, tree package, tree name)
6015 tree new_qualified = merge_qualified_name (package, name);
6016 tree new_class_decl;
6018 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
6019 load_class (new_qualified, 0);
6020 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
6022 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)))
6023 load_class (TREE_TYPE (new_class_decl), 0);
6024 TYPE_NAME (class_type) = new_qualified;
6025 return IDENTIFIER_CLASS_VALUE (new_qualified);
6027 return NULL_TREE;
6030 /* Resolve NAME and lay it out (if not done and if not the current
6031 parsed class). Return a decl node. This function is meant to be
6032 called when type resolution is necessary during the walk pass. */
6034 static tree
6035 resolve_and_layout (tree something, tree cl)
6037 tree decl, decl_type;
6039 /* Don't do that on the current class */
6040 if (something == current_class)
6041 return TYPE_NAME (current_class);
6043 /* Don't do anything for void and other primitive types */
6044 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6045 return NULL_TREE;
6047 /* Pointer types can be reall pointer types or fake pointers. When
6048 finding a real pointer, recheck for primitive types */
6049 if (TREE_CODE (something) == POINTER_TYPE)
6051 if (TREE_TYPE (something))
6053 something = TREE_TYPE (something);
6054 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
6055 return NULL_TREE;
6057 else
6058 something = TYPE_NAME (something);
6061 /* Don't do anything for arrays of primitive types */
6062 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
6063 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
6064 return NULL_TREE;
6066 /* Something might be a WFL */
6067 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6068 something = EXPR_WFL_NODE (something);
6070 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6071 TYPE_DECL or a real TYPE */
6072 else if (TREE_CODE (something) != IDENTIFIER_NODE)
6073 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6074 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6076 if (!(decl = resolve_no_layout (something, cl)))
6077 return NULL_TREE;
6079 /* Resolve and layout if necessary */
6080 decl_type = TREE_TYPE (decl);
6081 layout_class_methods (decl_type);
6082 /* Check methods */
6083 if (CLASS_FROM_SOURCE_P (decl_type))
6084 java_check_methods (decl);
6085 /* Layout the type if necessary */
6086 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6087 safe_layout_class (decl_type);
6089 return decl;
6092 /* Resolve a class, returns its decl but doesn't perform any
6093 layout. The current parsing context is saved and restored */
6095 static tree
6096 resolve_no_layout (tree name, tree cl)
6098 tree ptr, decl;
6099 BUILD_PTR_FROM_NAME (ptr, name);
6100 java_parser_context_save_global ();
6101 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6102 java_parser_context_restore_global ();
6104 return decl;
6107 /* Called when reporting errors. Skip the '[]'s in a complex array
6108 type description that failed to be resolved. purify_type_name can't
6109 use an identifier tree. */
6111 static const char *
6112 purify_type_name (const char *name)
6114 int len = strlen (name);
6115 int bracket_found;
6117 STRING_STRIP_BRACKETS (name, len, bracket_found);
6118 if (bracket_found)
6120 char *stripped_name = xmemdup (name, len, len+1);
6121 stripped_name [len] = '\0';
6122 return stripped_name;
6124 return name;
6127 /* The type CURRENT refers to can't be found. We print error messages. */
6129 static void
6130 complete_class_report_errors (jdep *dep)
6132 const char *name;
6134 if (!JDEP_WFL (dep))
6135 return;
6137 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6138 switch (JDEP_KIND (dep))
6140 case JDEP_SUPER:
6141 parse_error_context
6142 (JDEP_WFL (dep), "Superclass %qs of class %qs not found",
6143 purify_type_name (name),
6144 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6145 break;
6146 case JDEP_FIELD:
6147 parse_error_context
6148 (JDEP_WFL (dep), "Type %qs not found in declaration of field %qs",
6149 purify_type_name (name),
6150 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6151 break;
6152 case JDEP_METHOD: /* Covers arguments */
6153 parse_error_context
6154 (JDEP_WFL (dep), "Type %qs not found in the declaration of the argument %qs of method %qs",
6155 purify_type_name (name),
6156 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6157 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6158 break;
6159 case JDEP_METHOD_RETURN: /* Covers return type */
6160 parse_error_context
6161 (JDEP_WFL (dep), "Type %qs not found in the declaration of the return type of method %qs",
6162 purify_type_name (name),
6163 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6164 break;
6165 case JDEP_INTERFACE:
6166 parse_error_context
6167 (JDEP_WFL (dep), "Superinterface %qs of %s %qs not found",
6168 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6169 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6170 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6171 break;
6172 case JDEP_VARIABLE:
6173 parse_error_context
6174 (JDEP_WFL (dep), "Type %qs not found in the declaration of the local variable %qs",
6175 purify_type_name (IDENTIFIER_POINTER
6176 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6177 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6178 break;
6179 case JDEP_EXCEPTION: /* As specified by `throws' */
6180 parse_error_context
6181 (JDEP_WFL (dep), "Class %qs not found in %<throws%>",
6182 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6183 break;
6184 default:
6185 /* Fix for -Wall. Just break doing nothing. The error will be
6186 caught later */
6187 break;
6191 /* Return a static string containing the DECL prototype string. If
6192 DECL is a constructor, use the class name instead of the form
6193 <init> */
6195 static const char *
6196 get_printable_method_name (tree decl)
6198 const char *to_return;
6199 tree name = NULL_TREE;
6201 if (DECL_CONSTRUCTOR_P (decl))
6203 name = DECL_NAME (decl);
6204 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6207 to_return = lang_printable_name (decl, 2);
6208 if (DECL_CONSTRUCTOR_P (decl))
6209 DECL_NAME (decl) = name;
6211 return to_return;
6214 /* Track method being redefined inside the same class. As a side
6215 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6216 function it's a FWL, so we can track errors more accurately.) */
6218 static int
6219 check_method_redefinition (tree class, tree method)
6221 tree redef, sig;
6223 /* There's no need to verify <clinit> and finit$ and instinit$ */
6224 if (DECL_CLINIT_P (method)
6225 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6226 return 0;
6228 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6229 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6231 if (redef == method)
6232 break;
6233 if (DECL_NAME (redef) == DECL_NAME (method)
6234 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6235 && !DECL_ARTIFICIAL (method))
6237 parse_error_context
6238 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration %qs",
6239 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6240 get_printable_method_name (redef));
6241 return 1;
6244 return 0;
6247 /* Return 1 if check went ok, 0 otherwise. */
6248 static int
6249 check_abstract_method_definitions (int do_interface, tree class_decl,
6250 tree type)
6252 tree class = TREE_TYPE (class_decl);
6253 tree method, end_type;
6254 int ok = 1;
6256 end_type = (do_interface ? object_type_node : type);
6257 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6259 tree other_super, other_method, method_sig, method_name;
6260 int found = 0;
6261 int end_type_reached = 0;
6263 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6264 continue;
6266 /* Now verify that somewhere in between TYPE and CLASS,
6267 abstract method METHOD gets a non abstract definition
6268 that is inherited by CLASS. */
6270 method_sig = build_java_signature (TREE_TYPE (method));
6271 method_name = DECL_NAME (method);
6272 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6273 method_name = EXPR_WFL_NODE (method_name);
6275 other_super = class;
6276 do {
6277 if (other_super == end_type)
6278 end_type_reached = 1;
6280 /* Method search */
6281 for (other_method = TYPE_METHODS (other_super); other_method;
6282 other_method = TREE_CHAIN (other_method))
6284 tree s = build_java_signature (TREE_TYPE (other_method));
6285 tree other_name = DECL_NAME (other_method);
6287 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6288 other_name = EXPR_WFL_NODE (other_name);
6289 if (!DECL_CLINIT_P (other_method)
6290 && !DECL_CONSTRUCTOR_P (other_method)
6291 && method_name == other_name
6292 && method_sig == s
6293 && !METHOD_ABSTRACT (other_method))
6295 found = 1;
6296 break;
6299 other_super = CLASSTYPE_SUPER (other_super);
6300 } while (!end_type_reached);
6302 /* Report that abstract METHOD didn't find an implementation
6303 that CLASS can use. */
6304 if (!found)
6306 char *t = xstrdup (lang_printable_name
6307 (TREE_TYPE (TREE_TYPE (method)), 0));
6308 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6310 parse_error_context
6311 (lookup_cl (class_decl),
6312 "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",
6313 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6314 t, lang_printable_name (method, 2),
6315 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6316 "interface" : "class"),
6317 IDENTIFIER_POINTER (ccn),
6318 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6319 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6320 ok = 0;
6321 free (t);
6325 if (ok && do_interface)
6327 /* Check for implemented interfaces. */
6328 int i;
6329 tree base_binfo;
6331 for (i = 1;
6332 ok && BINFO_BASE_ITERATE (TYPE_BINFO (type), i, base_binfo);
6333 i++)
6334 ok = check_abstract_method_definitions (1, class_decl,
6335 BINFO_TYPE (base_binfo));
6338 return ok;
6341 /* Check that CLASS_DECL somehow implements all inherited abstract
6342 methods. */
6344 static void
6345 java_check_abstract_method_definitions (tree class_decl)
6347 tree class = TREE_TYPE (class_decl);
6348 tree super, base_binfo;
6349 int i;
6351 if (CLASS_ABSTRACT (class_decl))
6352 return;
6354 /* Check for inherited types */
6355 super = class;
6356 do {
6357 super = CLASSTYPE_SUPER (super);
6358 check_abstract_method_definitions (0, class_decl, super);
6359 } while (super != object_type_node);
6361 /* Check for implemented interfaces. */
6362 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6363 check_abstract_method_definitions (1, class_decl, BINFO_TYPE (base_binfo));
6366 /* Check all the types method DECL uses and return 1 if all of them
6367 are now complete, 0 otherwise. This is used to check whether its
6368 safe to build a method signature or not. */
6370 static int
6371 check_method_types_complete (tree decl)
6373 tree type = TREE_TYPE (decl);
6374 tree args;
6376 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6377 return 0;
6379 args = TYPE_ARG_TYPES (type);
6380 if (TREE_CODE (type) == METHOD_TYPE)
6381 args = TREE_CHAIN (args);
6382 for (; args != end_params_node; args = TREE_CHAIN (args))
6383 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6384 return 0;
6386 return 1;
6389 /* Visible interface to check methods contained in CLASS_DECL */
6391 void
6392 java_check_methods (tree class_decl)
6394 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6395 return;
6397 if (CLASS_INTERFACE (class_decl))
6398 java_check_abstract_methods (class_decl);
6399 else
6400 java_check_regular_methods (class_decl);
6402 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6405 /* Like not_accessible_p, but doesn't refer to the current class at
6406 all. */
6407 static bool
6408 hack_is_accessible_p (tree member, tree from_where)
6410 int flags = get_access_flags_from_decl (member);
6412 if (from_where == DECL_CONTEXT (member)
6413 || (flags & ACC_PUBLIC))
6414 return true;
6416 if ((flags & ACC_PROTECTED))
6418 if (inherits_from_p (from_where, DECL_CONTEXT (member)))
6419 return true;
6422 if ((flags & ACC_PRIVATE))
6423 return false;
6425 /* Package private, or protected. */
6426 return in_same_package (TYPE_NAME (from_where),
6427 TYPE_NAME (DECL_CONTEXT (member)));
6430 /* Check all the methods of CLASS_DECL. Methods are first completed
6431 then checked according to regular method existence rules. If no
6432 constructor for CLASS_DECL were encountered, then build its
6433 declaration. */
6434 static void
6435 java_check_regular_methods (tree class_decl)
6437 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6438 tree method;
6439 tree class = TREE_TYPE (class_decl);
6440 tree found = NULL_TREE;
6441 tree mthrows;
6443 /* It is not necessary to check methods defined in java.lang.Object */
6444 if (class == object_type_node)
6445 return;
6447 if (!TYPE_NVIRTUALS (class))
6448 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6450 /* Should take interfaces into account. FIXME */
6451 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6453 tree sig;
6454 tree method_wfl = DECL_FUNCTION_WFL (method);
6455 int aflags;
6457 /* Check for redefinitions */
6458 if (check_method_redefinition (class, method))
6459 continue;
6461 /* We verify things thrown by the method. They must inherit from
6462 java.lang.Throwable. */
6463 for (mthrows = DECL_FUNCTION_THROWS (method);
6464 mthrows; mthrows = TREE_CHAIN (mthrows))
6466 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6467 parse_error_context
6468 (TREE_PURPOSE (mthrows), "Class %qs in %<throws%> clause must be a subclass of class %<java.lang.Throwable%>",
6469 IDENTIFIER_POINTER
6470 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6473 /* If we see one constructor a mark so we don't generate the
6474 default one. Also skip other verifications: constructors
6475 can't be inherited hence hidden or overridden. */
6476 if (DECL_CONSTRUCTOR_P (method))
6478 saw_constructor = 1;
6479 continue;
6482 sig = build_java_argument_signature (TREE_TYPE (method));
6483 found = lookup_argument_method_generic (class, DECL_NAME (method), sig,
6484 SEARCH_SUPER | SEARCH_INTERFACE);
6486 /* Inner class can't declare static methods */
6487 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6489 char *t = xstrdup (lang_printable_name (class, 0));
6490 parse_error_context
6491 (method_wfl, "Method %qs can't be static in inner class %qs. Only members of interfaces and top-level classes can be static",
6492 lang_printable_name (method, 2), t);
6493 free (t);
6496 /* Nothing overrides or it's a private method. */
6497 if (!found)
6498 continue;
6499 if (METHOD_PRIVATE (found))
6501 found = NULL_TREE;
6502 continue;
6505 /* If `found' is declared in an interface, make sure the
6506 modifier matches. */
6507 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6508 && clinit_identifier_node != DECL_NAME (found)
6509 && !METHOD_PUBLIC (method))
6511 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6512 parse_error_context (method_wfl, "Class %qs must override %qs with a public method in order to implement interface %qs",
6513 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6514 lang_printable_name (method, 2),
6515 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6518 /* Can't override a method with the same name and different return
6519 types. */
6520 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6522 char *t = xstrdup
6523 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6524 parse_error_context
6525 (method_wfl,
6526 "Method %qs was defined with return type %qs in class %qs",
6527 lang_printable_name (found, 2), t,
6528 IDENTIFIER_POINTER
6529 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6530 free (t);
6533 aflags = get_access_flags_from_decl (found);
6535 /* Can't override final. Can't override static. */
6536 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6538 /* Static *can* override static */
6539 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6540 continue;
6541 parse_error_context
6542 (method_wfl,
6543 "%s methods can't be overridden. Method %qs is %s in class %qs",
6544 (METHOD_FINAL (found) ? "Final" : "Static"),
6545 lang_printable_name (found, 2),
6546 (METHOD_FINAL (found) ? "final" : "static"),
6547 IDENTIFIER_POINTER
6548 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6549 continue;
6552 /* Static method can't override instance method. */
6553 if (METHOD_STATIC (method))
6555 parse_error_context
6556 (method_wfl,
6557 "Instance methods can't be overridden by a static method. Method %qs is an instance method in class %qs",
6558 lang_printable_name (found, 2),
6559 IDENTIFIER_POINTER
6560 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6561 continue;
6564 /* - Overriding/hiding public must be public
6565 - Overriding/hiding protected must be protected or public
6566 - If the overridden or hidden method has default (package)
6567 access, then the overriding or hiding method must not be
6568 private; otherwise, a compile-time error occurs. If
6569 `found' belongs to an interface, things have been already
6570 taken care of. */
6571 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6572 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6573 || (METHOD_PROTECTED (found)
6574 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6575 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6576 && METHOD_PRIVATE (method))))
6578 parse_error_context
6579 (method_wfl,
6580 "Methods can't be overridden to be more private. Method %qs is not %s in class %qs", lang_printable_name (method, 2),
6581 (METHOD_PUBLIC (method) ? "public" :
6582 (METHOD_PRIVATE (method) ? "private" : "protected")),
6583 IDENTIFIER_POINTER (DECL_NAME
6584 (TYPE_NAME (DECL_CONTEXT (found)))));
6585 continue;
6588 /* Check this method against all the other implementations it
6589 overrides. Here we only check the class hierarchy; the rest
6590 of the checking is done later. If this method is just a
6591 Miranda method, we can skip the check. */
6592 if (! METHOD_INVISIBLE (method))
6593 check_concrete_throws_clauses (class, method, DECL_NAME (method), sig);
6596 /* The above throws clause check only looked at superclasses. Now
6597 we must also make sure that all methods declared in interfaces
6598 have compatible throws clauses. FIXME: there are more efficient
6599 ways to organize this checking; we should implement one. */
6600 check_interface_throws_clauses (class, class);
6602 if (!TYPE_NVIRTUALS (class))
6603 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6605 /* Search for inherited abstract method not yet implemented in this
6606 class. */
6607 java_check_abstract_method_definitions (class_decl);
6609 if (!saw_constructor)
6610 abort ();
6613 /* Check to make sure that all the methods in all the interfaces
6614 implemented by CLASS_DECL are compatible with the concrete
6615 implementations available in CHECK_CLASS_DECL. */
6616 static void
6617 check_interface_throws_clauses (tree check_class_decl, tree class_decl)
6619 for (; class_decl != NULL_TREE; class_decl = CLASSTYPE_SUPER (class_decl))
6621 int i;
6623 if (! CLASS_LOADED_P (class_decl))
6625 if (CLASS_FROM_SOURCE_P (class_decl))
6626 safe_layout_class (class_decl);
6627 else
6628 load_class (class_decl, 1);
6631 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (class_decl)) - 1; i > 0; --i)
6633 tree interface
6634 = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (class_decl), i));
6635 tree iface_method;
6637 for (iface_method = TYPE_METHODS (interface);
6638 iface_method != NULL_TREE;
6639 iface_method = TREE_CHAIN (iface_method))
6641 tree sig, method;
6643 /* First look for a concrete method implemented or
6644 inherited by this class. No need to search
6645 interfaces here, since we're already looking through
6646 all of them. */
6647 sig = build_java_argument_signature (TREE_TYPE (iface_method));
6648 method
6649 = lookup_argument_method_generic (check_class_decl,
6650 DECL_NAME (iface_method),
6651 sig, SEARCH_VISIBLE);
6652 /* If we don't find an implementation, that is ok. Any
6653 potential errors from that are diagnosed elsewhere.
6654 Also, multiple inheritance with conflicting throws
6655 clauses is fine in the absence of a concrete
6656 implementation. */
6657 if (method != NULL_TREE && !METHOD_ABSTRACT (method)
6658 && !METHOD_INVISIBLE (iface_method))
6660 tree method_wfl = DECL_FUNCTION_WFL (method);
6661 check_throws_clauses (method, method_wfl, iface_method);
6665 /* Now check superinterfaces. */
6666 check_interface_throws_clauses (check_class_decl, interface);
6671 /* Check throws clauses of a method against the clauses of all the
6672 methods it overrides. We do this by searching up the class
6673 hierarchy, examining all matching accessible methods. */
6674 static void
6675 check_concrete_throws_clauses (tree class, tree self_method,
6676 tree name, tree signature)
6678 tree method = lookup_argument_method_generic (class, name, signature,
6679 SEARCH_SUPER | SEARCH_VISIBLE);
6680 while (method != NULL_TREE)
6682 if (! METHOD_INVISIBLE (method) && hack_is_accessible_p (method, class))
6683 check_throws_clauses (self_method, DECL_FUNCTION_WFL (self_method),
6684 method);
6686 method = lookup_argument_method_generic (DECL_CONTEXT (method),
6687 name, signature,
6688 SEARCH_SUPER | SEARCH_VISIBLE);
6692 /* Generate an error if the `throws' clause of METHOD (if any) is
6693 incompatible with the `throws' clause of FOUND (if any). */
6694 static void
6695 check_throws_clauses (tree method, tree method_wfl, tree found)
6697 tree mthrows;
6699 for (mthrows = DECL_FUNCTION_THROWS (method);
6700 mthrows; mthrows = TREE_CHAIN (mthrows))
6702 tree fthrows;
6704 /* We don't verify unchecked expressions */
6705 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6706 continue;
6707 /* Checked expression must be compatible */
6708 for (fthrows = DECL_FUNCTION_THROWS (found);
6709 fthrows; fthrows = TREE_CHAIN (fthrows))
6711 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6712 break;
6714 if (!fthrows)
6716 parse_error_context
6717 (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",
6718 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6719 lang_printable_name (found, 2),
6720 IDENTIFIER_POINTER
6721 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6726 /* Check abstract method of interface INTERFACE */
6727 static void
6728 java_check_abstract_methods (tree interface_decl)
6730 int i;
6731 tree method, found;
6732 tree interface = TREE_TYPE (interface_decl);
6733 tree base_binfo;
6735 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6737 /* 2- Check for double definition inside the defining interface */
6738 if (check_method_redefinition (interface, method))
6739 continue;
6741 /* 3- Overriding is OK as far as we preserve the return type. */
6742 found = lookup_java_interface_method2 (interface, method);
6743 if (found)
6745 char *t;
6746 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 2));
6747 parse_error_context
6748 (DECL_FUNCTION_WFL (found),
6749 "Method %qs was defined with return type %qs in class %qs",
6750 lang_printable_name (found, 2), t,
6751 IDENTIFIER_POINTER
6752 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6753 free (t);
6754 continue;
6758 /* 4- Inherited methods can't differ by their returned types */
6759 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (interface), i, base_binfo); i++)
6761 tree sub_interface_method, sub_interface;
6763 sub_interface = BINFO_TYPE (base_binfo);
6764 for (sub_interface_method = TYPE_METHODS (sub_interface);
6765 sub_interface_method;
6766 sub_interface_method = TREE_CHAIN (sub_interface_method))
6768 found = lookup_java_interface_method2 (interface,
6769 sub_interface_method);
6770 if (found && (found != sub_interface_method))
6772 parse_error_context
6773 (lookup_cl (sub_interface_method),
6774 "Interface %qs inherits method %qs from interface %qs. This method is redefined with a different return type in interface %qs",
6775 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6776 lang_printable_name (found, 2),
6777 IDENTIFIER_POINTER
6778 (DECL_NAME (TYPE_NAME
6779 (DECL_CONTEXT (sub_interface_method)))),
6780 IDENTIFIER_POINTER
6781 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6787 /* Lookup methods in interfaces using their name and partial
6788 signature. Return a matching method only if their types differ. */
6790 static tree
6791 lookup_java_interface_method2 (tree class, tree method_decl)
6793 int i;
6794 tree base_binfo;
6795 tree to_return;
6797 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6799 if ((BINFO_TYPE (base_binfo) != object_type_node)
6800 && (to_return =
6801 lookup_java_method2 (BINFO_TYPE (base_binfo), method_decl, 1)))
6802 return to_return;
6804 for (i = 0; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
6806 to_return = lookup_java_interface_method2
6807 (BINFO_TYPE (base_binfo), method_decl);
6808 if (to_return)
6809 return to_return;
6812 return NULL_TREE;
6815 /* Lookup method using their name and partial signature. Return a
6816 matching method only if their types differ. */
6818 static tree
6819 lookup_java_method2 (tree clas, tree method_decl, int do_interface)
6821 tree method, method_signature, method_name, method_type, name;
6823 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6824 name = DECL_NAME (method_decl);
6825 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6826 EXPR_WFL_NODE (name) : name);
6827 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6829 while (clas != NULL_TREE)
6831 for (method = TYPE_METHODS (clas);
6832 method != NULL_TREE; method = TREE_CHAIN (method))
6834 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6835 tree name = DECL_NAME (method);
6836 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6837 EXPR_WFL_NODE (name) : name) == method_name
6838 && method_sig == method_signature
6839 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6840 return method;
6842 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6844 return NULL_TREE;
6847 /* Return the line that matches DECL line number, and try its best to
6848 position the column number. Used during error reports.
6849 FUTURE/FIXME: return source_location instead of node. */
6851 static GTY(()) tree cl_v;
6852 static tree
6853 lookup_cl (tree decl)
6855 #ifndef USE_MAPPED_LOCATION
6856 char *line, *found;
6857 #endif
6859 if (!decl)
6860 return NULL_TREE;
6862 if (cl_v == NULL_TREE)
6864 cl_v = build_unknown_wfl (NULL_TREE);
6867 #ifdef USE_MAPPED_LOCATION
6868 SET_EXPR_LOCATION (cl_v, DECL_SOURCE_LOCATION (decl));
6869 #else
6870 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6871 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE (decl), -1);
6873 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6874 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6876 found = strstr ((const char *)line,
6877 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6878 if (found)
6879 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6880 #endif
6882 return cl_v;
6885 /* Look for a simple name in the single-type import list */
6887 static tree
6888 find_name_in_single_imports (tree name)
6890 tree node;
6892 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6893 if (TREE_VALUE (node) == name)
6894 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6896 return NULL_TREE;
6899 /* Process all single-type import. */
6901 static int
6902 process_imports (void)
6904 tree import;
6905 int error_found;
6907 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6909 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6910 char *original_name;
6912 /* Don't load twice something already defined. */
6913 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6914 continue;
6916 original_name = xmemdup (IDENTIFIER_POINTER (to_be_found),
6917 IDENTIFIER_LENGTH (to_be_found),
6918 IDENTIFIER_LENGTH (to_be_found) + 1);
6920 while (1)
6922 tree left;
6924 QUALIFIED_P (to_be_found) = 1;
6925 load_class (to_be_found, 0);
6926 error_found =
6927 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true, NULL_TREE);
6929 /* We found it, we can bail out */
6930 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6932 check_deprecation (TREE_PURPOSE (import),
6933 IDENTIFIER_CLASS_VALUE (to_be_found));
6934 break;
6937 /* We haven't found it. Maybe we're trying to access an
6938 inner class. The only way for us to know is to try again
6939 after having dropped a qualifier. If we can't break it further,
6940 we have an error. */
6941 if (split_qualified_name (&left, NULL, to_be_found))
6942 break;
6944 to_be_found = left;
6946 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6948 parse_error_context (TREE_PURPOSE (import),
6949 "Class or interface %qs not found in import",
6950 original_name);
6951 error_found = 1;
6954 free (original_name);
6955 if (error_found)
6956 return 1;
6958 return 0;
6961 /* Possibly find and mark a class imported by a single-type import
6962 statement. */
6964 static void
6965 find_in_imports (tree enclosing_type, tree class_type)
6967 tree import;
6968 if (enclosing_type && TYPE_IMPORT_LIST (enclosing_type))
6969 import = TYPE_IMPORT_LIST (enclosing_type);
6970 else
6971 import = ctxp->import_list;
6973 while (import)
6975 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6977 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6978 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6979 return;
6981 import = TREE_CHAIN (import);
6985 static int
6986 note_possible_classname (const char *name, int len)
6988 tree node;
6989 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6990 len = len - 5;
6991 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6992 len = len - 6;
6993 else
6994 return 0;
6995 node = ident_subst (name, len, "", '/', '.', "");
6996 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6997 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6998 return 1;
7001 /* Read a import directory, gathering potential match for further type
7002 references. Indifferently reads a filesystem or a ZIP archive
7003 directory. */
7005 static void
7006 read_import_dir (tree wfl)
7008 tree package_id = EXPR_WFL_NODE (wfl);
7009 const char *package_name = IDENTIFIER_POINTER (package_id);
7010 int package_length = IDENTIFIER_LENGTH (package_id);
7011 DIR *dirp = NULL;
7012 JCF *saved_jcf = current_jcf;
7014 int found = 0;
7015 int k;
7016 void *entry;
7017 struct buffer filename[1];
7019 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
7020 return;
7021 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
7023 BUFFER_INIT (filename);
7024 buffer_grow (filename, package_length + 100);
7026 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
7028 const char *entry_name = jcf_path_name (entry);
7029 int entry_length = strlen (entry_name);
7030 if (jcf_path_is_zipfile (entry))
7032 ZipFile *zipf;
7033 buffer_grow (filename, entry_length);
7034 memcpy (filename->data, entry_name, entry_length - 1);
7035 filename->data[entry_length-1] = '\0';
7036 zipf = opendir_in_zip ((const char *) filename->data, jcf_path_is_system (entry));
7037 if (zipf == NULL)
7038 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
7039 else
7041 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
7042 BUFFER_RESET (filename);
7043 for (k = 0; k < package_length; k++)
7045 char ch = package_name[k];
7046 *filename->ptr++ = ch == '.' ? '/' : ch;
7048 *filename->ptr++ = '/';
7050 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
7052 const char *current_entry = ZIPDIR_FILENAME (zipd);
7053 int current_entry_len = zipd->filename_length;
7055 if (current_entry_len >= BUFFER_LENGTH (filename)
7056 && strncmp ((const char *) filename->data, current_entry,
7057 BUFFER_LENGTH (filename)) != 0)
7058 continue;
7059 found |= note_possible_classname (current_entry,
7060 current_entry_len);
7064 else
7066 BUFFER_RESET (filename);
7067 buffer_grow (filename, entry_length + package_length + 4);
7068 strcpy ((char *) filename->data, entry_name);
7069 filename->ptr = filename->data + entry_length;
7070 for (k = 0; k < package_length; k++)
7072 char ch = package_name[k];
7073 *filename->ptr++ = ch == '.' ? '/' : ch;
7075 *filename->ptr = '\0';
7077 dirp = opendir ((const char *) filename->data);
7078 if (dirp == NULL)
7079 continue;
7080 *filename->ptr++ = '/';
7081 for (;;)
7083 int len;
7084 const char *d_name;
7085 struct dirent *direntp = readdir (dirp);
7086 if (!direntp)
7087 break;
7088 d_name = direntp->d_name;
7089 len = strlen (direntp->d_name);
7090 buffer_grow (filename, len+1);
7091 strcpy ((char *) filename->ptr, d_name);
7092 found |= note_possible_classname ((const char *) filename->data + entry_length,
7093 package_length+len+1);
7095 if (dirp)
7096 closedir (dirp);
7100 free (filename->data);
7102 /* Here we should have a unified way of retrieving an entry, to be
7103 indexed. */
7104 if (!found)
7106 static int first = 1;
7107 if (first)
7109 error ("Can't find default package %qs. Check the CLASSPATH environment variable and the access to the archives", package_name);
7110 java_error_count++;
7111 first = 0;
7113 else
7114 parse_error_context (wfl, "Package %qs not found in import",
7115 package_name);
7116 current_jcf = saved_jcf;
7117 return;
7119 current_jcf = saved_jcf;
7122 /* Possibly find a type in the import on demands specified
7123 types. Returns 1 if an error occurred, 0 otherwise. Run through the
7124 entire list, to detected potential double definitions. */
7126 static int
7127 find_in_imports_on_demand (tree enclosing_type, tree class_type)
7129 tree class_type_name = TYPE_NAME (class_type);
7130 tree cl = NULL_TREE;
7131 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
7132 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
7133 tree node;
7134 tree import;
7136 if (enclosing_type && TYPE_IMPORT_DEMAND_LIST (enclosing_type))
7137 import = TYPE_IMPORT_DEMAND_LIST (enclosing_type);
7138 else
7139 import = ctxp->import_demand_list;
7141 for (; import; import = TREE_CHAIN (import))
7143 location_t saved_location = input_location;
7144 int access_check;
7145 const char *id_name;
7146 tree decl, type_name_copy;
7148 obstack_grow (&temporary_obstack,
7149 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
7150 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
7151 obstack_1grow (&temporary_obstack, '.');
7152 obstack_grow0 (&temporary_obstack,
7153 IDENTIFIER_POINTER (class_type_name),
7154 IDENTIFIER_LENGTH (class_type_name));
7155 id_name = obstack_finish (&temporary_obstack);
7157 if (! (node = maybe_get_identifier (id_name)))
7158 continue;
7160 /* Setup input_line so that it refers to the line of the import (in
7161 case we parse a class file and encounter errors */
7162 #ifdef USE_MAPPED_LOCATION
7163 input_location = EXPR_LOCATION (TREE_PURPOSE (import));
7164 #else
7165 input_line = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7166 #endif
7168 type_name_copy = TYPE_NAME (class_type);
7169 TYPE_NAME (class_type) = node;
7170 QUALIFIED_P (node) = 1;
7171 decl = IDENTIFIER_CLASS_VALUE (node);
7172 access_check = -1;
7173 /* If there is no DECL set for the class or if the class isn't
7174 loaded and not seen in source yet, then load */
7175 if (!decl || ! CLASS_LOADED_P (TREE_TYPE (decl)))
7177 load_class (node, 0);
7178 decl = IDENTIFIER_CLASS_VALUE (node);
7180 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7181 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7182 false, NULL_TREE);
7183 else
7184 /* 6.6.1: Inner classes are subject to member access rules. */
7185 access_check = 0;
7187 input_location = saved_location;
7189 /* If the loaded class is not accessible or couldn't be loaded,
7190 we restore the original TYPE_NAME and process the next
7191 import. */
7192 if (access_check || !decl)
7194 TYPE_NAME (class_type) = type_name_copy;
7195 continue;
7198 /* If the loaded class is accessible, we keep a tab on it to
7199 detect and report multiple inclusions. */
7200 if (IS_A_CLASSFILE_NAME (node))
7202 if (seen_once < 0)
7204 cl = TREE_PURPOSE (import);
7205 seen_once = 1;
7207 else if (seen_once >= 0)
7209 tree location = (cl ? cl : TREE_PURPOSE (import));
7210 tree package = (cl ? EXPR_WFL_NODE (cl) :
7211 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7212 seen_once++;
7213 parse_error_context
7214 (location,
7215 "Type %qs also potentially defined in package %qs",
7216 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7217 IDENTIFIER_POINTER (package));
7220 to_return = access_check;
7223 if (seen_once == 1)
7224 return to_return;
7225 else
7226 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7229 static tree
7230 resolve_package (tree pkg, tree *next, tree *type_name)
7232 tree current;
7233 tree decl = NULL_TREE;
7234 *type_name = NULL_TREE;
7236 /* The trick is to determine when the package name stops and were
7237 the name of something contained in the package starts. Then we
7238 return a fully qualified name of what we want to get. */
7240 *next = EXPR_WFL_QUALIFICATION (pkg);
7242 /* Try to progressively construct a type name */
7243 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7244 for (current = EXPR_WFL_QUALIFICATION (pkg);
7245 current; current = TREE_CHAIN (current))
7247 /* If we don't have what we're expecting, exit now. TYPE_NAME
7248 will be null and the error caught later. */
7249 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7250 break;
7251 *type_name =
7252 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7253 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7255 /* resolve_package should be used in a loop, hence we
7256 point at this one to naturally process the next one at
7257 the next iteration. */
7258 *next = current;
7259 break;
7262 return decl;
7266 /* Check accessibility of inner classes according to member access rules.
7267 DECL is the inner class, ENCLOSING_DECL is the class from which the
7268 access is being attempted. */
7270 static void
7271 check_inner_class_access (tree decl, tree enclosing_decl, tree cl)
7273 const char *access;
7274 tree enclosing_decl_type;
7276 /* We don't issue an error message when CL is null. CL can be null
7277 as a result of processing a JDEP crafted by source_start_java_method
7278 for the purpose of patching its parm decl. But the error would
7279 have been already trapped when fixing the method's signature.
7280 DECL can also be NULL in case of earlier errors. */
7281 if (!decl || !cl)
7282 return;
7284 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7286 if (CLASS_PRIVATE (decl))
7288 /* Access is permitted only within the body of the top-level
7289 class in which DECL is declared. */
7290 tree top_level = decl;
7291 while (DECL_CONTEXT (top_level))
7292 top_level = DECL_CONTEXT (top_level);
7293 while (DECL_CONTEXT (enclosing_decl))
7294 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7295 if (top_level == enclosing_decl)
7296 return;
7297 access = "private";
7299 else if (CLASS_PROTECTED (decl))
7301 tree decl_context;
7302 /* Access is permitted from within the same package... */
7303 if (in_same_package (decl, enclosing_decl))
7304 return;
7306 /* ... or from within the body of a subtype of the context in which
7307 DECL is declared. */
7308 decl_context = DECL_CONTEXT (decl);
7309 while (enclosing_decl)
7311 if (CLASS_INTERFACE (decl))
7313 if (interface_of_p (TREE_TYPE (decl_context),
7314 enclosing_decl_type))
7315 return;
7317 else
7319 /* Eww. The order of the arguments is different!! */
7320 if (inherits_from_p (enclosing_decl_type,
7321 TREE_TYPE (decl_context)))
7322 return;
7324 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7326 access = "protected";
7328 else if (! CLASS_PUBLIC (decl))
7330 /* Access is permitted only from within the same package as DECL. */
7331 if (in_same_package (decl, enclosing_decl))
7332 return;
7333 access = "non-public";
7335 else
7336 /* Class is public. */
7337 return;
7339 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7340 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7341 lang_printable_name (decl, 2), access);
7344 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7345 foreign package, it must be PUBLIC. Return 0 if no access
7346 violations were found, 1 otherwise. If VERBOSE is true and an error
7347 was found, it is reported and accounted for. If CL is NULL then
7348 look it up with THIS_DECL. */
7350 static int
7351 check_pkg_class_access (tree class_name, tree cl, bool verbose, tree this_decl)
7353 tree type;
7355 if (!IDENTIFIER_CLASS_VALUE (class_name))
7356 return 0;
7358 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7359 return 0;
7361 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7363 /* Access to a private class within the same package is
7364 allowed. */
7365 tree l, r;
7366 split_qualified_name (&l, &r, class_name);
7367 if (!QUALIFIED_P (class_name) && !ctxp->package)
7368 /* Both in the empty package. */
7369 return 0;
7370 if (l == ctxp->package)
7371 /* Both in the same package. */
7372 return 0;
7374 if (verbose)
7375 parse_error_context
7376 (cl == NULL ? lookup_cl (this_decl): cl,
7377 "Can't access %s %qs. Only public classes and interfaces in other packages can be accessed",
7378 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7379 IDENTIFIER_POINTER (class_name));
7380 return 1;
7382 return 0;
7385 /* Local variable declaration. */
7387 static void
7388 declare_local_variables (int modifier, tree type, tree vlist)
7390 tree decl, current, saved_type;
7391 tree type_wfl = NULL_TREE;
7392 int must_chain = 0;
7393 int final_p = 0;
7395 /* Push a new block if statements were seen between the last time we
7396 pushed a block and now. Keep a count of blocks to close */
7397 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7399 tree b = enter_block ();
7400 BLOCK_IS_IMPLICIT (b) = 1;
7403 if (modifier)
7405 size_t i;
7406 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7407 if (1 << i & modifier)
7408 break;
7409 if (modifier == ACC_FINAL)
7410 final_p = 1;
7411 else
7413 parse_error_context
7414 (ctxp->modifier_ctx [i],
7415 "Only %<final%> is allowed as a local variables modifier");
7416 return;
7420 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7421 hold the TYPE value if a new incomplete has to be created (as
7422 opposed to being found already existing and reused). */
7423 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7425 /* If TYPE is fully resolved and we don't have a reference, make one */
7426 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7428 /* Go through all the declared variables */
7429 for (current = vlist, saved_type = type; current;
7430 current = TREE_CHAIN (current), type = saved_type)
7432 tree other, real_type;
7433 tree wfl = TREE_PURPOSE (current);
7434 tree name = EXPR_WFL_NODE (wfl);
7435 tree init = TREE_VALUE (current);
7437 /* Process NAME, as it may specify extra dimension(s) for it */
7438 type = build_array_from_name (type, type_wfl, name, &name);
7440 /* Variable redefinition check */
7441 if ((other = lookup_name_in_blocks (name)))
7443 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7444 DECL_SOURCE_LINE (other));
7445 continue;
7448 /* Type adjustment. We may have just readjusted TYPE because
7449 the variable specified more dimensions. Make sure we have
7450 a reference if we can and don't have one already. */
7451 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7453 real_type = GET_REAL_TYPE (type);
7454 /* Never layout this decl. This will be done when its scope
7455 will be entered */
7456 decl = build_decl (VAR_DECL, name, real_type);
7457 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7458 DECL_FINAL (decl) = final_p;
7459 BLOCK_CHAIN_DECL (decl);
7461 /* Don't try to use an INIT statement when an error was found */
7462 if (init && java_error_count)
7463 init = NULL_TREE;
7465 /* Remember it if this is an initialized-upon-declaration final
7466 variable. */
7467 if (init && final_p)
7469 DECL_LOCAL_FINAL_IUD (decl) = 1;
7472 /* Add the initialization function to the current function's code */
7473 if (init)
7475 /* Name might have been readjusted */
7476 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7477 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7478 java_method_add_stmt (current_function_decl,
7479 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7480 init));
7483 /* Setup dependency the type of the decl */
7484 if (must_chain)
7486 jdep *dep;
7487 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7488 dep = CLASSD_LAST (ctxp->classd_list);
7489 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7492 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7495 /* Called during parsing. Build decls from argument list. */
7497 static void
7498 source_start_java_method (tree fndecl)
7500 tree tem;
7501 tree parm_decl;
7502 int i;
7504 if (!fndecl)
7505 return;
7507 current_function_decl = fndecl;
7509 /* New scope for the function */
7510 enter_block ();
7511 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7512 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7514 tree type = TREE_VALUE (tem);
7515 tree name = TREE_PURPOSE (tem);
7517 /* If type is incomplete. Create an incomplete decl and ask for
7518 the decl to be patched later */
7519 if (INCOMPLETE_TYPE_P (type))
7521 jdep *jdep;
7522 tree real_type = GET_REAL_TYPE (type);
7523 parm_decl = build_decl (PARM_DECL, name, real_type);
7524 type = obtain_incomplete_type (type);
7525 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7526 jdep = CLASSD_LAST (ctxp->classd_list);
7527 JDEP_MISC (jdep) = name;
7528 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7530 else
7531 parm_decl = build_decl (PARM_DECL, name, type);
7533 /* Remember if a local variable was declared final (via its
7534 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7535 if (ARG_FINAL_P (tem))
7537 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7538 DECL_FINAL (parm_decl) = 1;
7541 BLOCK_CHAIN_DECL (parm_decl);
7543 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7544 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7545 nreverse (tem);
7546 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7547 DECL_MAX_LOCALS (current_function_decl) = i;
7550 /* Called during parsing. Creates an artificial method declaration. */
7552 static tree
7553 create_artificial_method (tree class, int flags, tree type,
7554 tree name, tree args)
7556 tree mdecl;
7557 location_t save_location = input_location;
7559 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (class));
7560 mdecl = make_node (FUNCTION_TYPE);
7561 TREE_TYPE (mdecl) = type;
7562 TYPE_ARG_TYPES (mdecl) = args;
7563 /* We used to compute the signature of MDECL here and then use
7564 add_method(), but that failed because our caller might modify
7565 the type of the returned method, which trashes the cache in
7566 get_type_from_signature(). */
7567 mdecl = add_method_1 (class, flags, name, mdecl);
7568 input_location = save_location;
7569 DECL_ARTIFICIAL (mdecl) = 1;
7570 return mdecl;
7573 /* Starts the body if an artificial method. */
7575 static void
7576 start_artificial_method_body (tree mdecl)
7578 #ifdef USE_MAPPED_LOCATION
7579 DECL_SOURCE_LOCATION (mdecl) = ctxp->file_start_location;
7580 DECL_FUNCTION_LAST_LINE (mdecl) = ctxp->file_start_location;
7581 #else
7582 DECL_SOURCE_LINE (mdecl) = 1;
7583 DECL_FUNCTION_LAST_LINE (mdecl) = 1;
7584 #endif
7585 source_start_java_method (mdecl);
7586 enter_block ();
7589 static void
7590 end_artificial_method_body (tree mdecl)
7592 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7593 It has to be evaluated first. (if mdecl is current_function_decl,
7594 we have an undefined behavior if no temporary variable is used.) */
7595 tree b = exit_block ();
7596 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7597 exit_block ();
7600 /* Dump a tree of some kind. This is a convenience wrapper for the
7601 dump_* functions in tree-dump.c. */
7602 static void
7603 dump_java_tree (enum tree_dump_index phase, tree t)
7605 FILE *stream;
7606 int flags;
7608 stream = dump_begin (phase, &flags);
7609 flags |= TDF_SLIM;
7610 if (stream)
7612 dump_node (t, flags, stream);
7613 dump_end (phase, stream);
7617 /* Terminate a function and expand its body. */
7619 static void
7620 source_end_java_method (void)
7622 tree fndecl = current_function_decl;
7624 if (!fndecl)
7625 return;
7627 java_parser_context_save_global ();
7628 #ifdef USE_MAPPED_LOCATION
7629 input_location = ctxp->last_ccb_indent1;
7630 #else
7631 input_line = ctxp->last_ccb_indent1;
7632 #endif
7634 /* Turn function bodies with only a NOP expr null, so they don't get
7635 generated at all and we won't get warnings when using the -W
7636 -Wall flags. */
7637 if (IS_EMPTY_STMT (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))))
7638 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7640 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7641 && ! flag_emit_class_files)
7642 finish_method (fndecl);
7644 current_function_decl = NULL_TREE;
7645 java_parser_context_restore_global ();
7646 current_function_decl = NULL_TREE;
7649 /* Record EXPR in the current function block. Complements compound
7650 expression second operand if necessary. */
7652 tree
7653 java_method_add_stmt (tree fndecl, tree expr)
7655 if (!GET_CURRENT_BLOCK (fndecl))
7656 return NULL_TREE;
7657 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7660 static tree
7661 add_stmt_to_block (tree b, tree type, tree stmt)
7663 tree body = BLOCK_EXPR_BODY (b), c;
7665 if (java_error_count)
7666 return body;
7668 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7669 return body;
7671 BLOCK_EXPR_BODY (b) = c;
7672 TREE_SIDE_EFFECTS (c) = 1;
7673 return c;
7676 /* Lays out the methods for the classes seen so far. */
7678 void
7679 java_layout_seen_class_methods (void)
7681 tree previous_list = all_class_list;
7682 tree end = NULL_TREE;
7683 tree current;
7685 while (1)
7687 for (current = previous_list;
7688 current != end; current = TREE_CHAIN (current))
7690 tree decl = TREE_VALUE (current);
7691 tree cls = TREE_TYPE (decl);
7693 input_location = DECL_SOURCE_LOCATION (decl);
7695 if (! CLASS_LOADED_P (cls))
7696 load_class (cls, 0);
7698 layout_class_methods (cls);
7701 /* Note that new classes might have been added while laying out
7702 methods, changing the value of all_class_list. */
7704 if (previous_list != all_class_list)
7706 end = previous_list;
7707 previous_list = all_class_list;
7709 else
7710 break;
7714 static GTY(()) tree stop_reordering;
7715 void
7716 java_reorder_fields (void)
7718 tree current;
7720 for (current = gclass_list; current; current = TREE_CHAIN (current))
7722 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7724 if (current_class == stop_reordering)
7725 break;
7727 /* Reverse the fields, but leave the dummy field in front.
7728 Fields are already ordered for Object and Class */
7729 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7730 && current_class != class_type_node)
7732 /* If the dummy field is there, reverse the right fields and
7733 just layout the type for proper fields offset */
7734 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7736 tree fields = TYPE_FIELDS (current_class);
7737 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7738 TYPE_SIZE (current_class) = NULL_TREE;
7740 /* We don't have a dummy field, we need to layout the class,
7741 after having reversed the fields */
7742 else
7744 TYPE_FIELDS (current_class) =
7745 nreverse (TYPE_FIELDS (current_class));
7746 TYPE_SIZE (current_class) = NULL_TREE;
7750 /* There are cases were gclass_list will be empty. */
7751 if (gclass_list)
7752 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7755 /* Layout the methods of all classes loaded in one way or another.
7756 Check methods of source parsed classes. Then reorder the
7757 fields and layout the classes or the type of all source parsed
7758 classes */
7760 void
7761 java_layout_classes (void)
7763 tree current;
7764 int save_error_count = java_error_count;
7766 /* Layout the methods of all classes seen so far */
7767 java_layout_seen_class_methods ();
7768 java_parse_abort_on_error ();
7769 all_class_list = NULL_TREE;
7771 /* Then check the methods of all parsed classes */
7772 for (current = gclass_list; current; current = TREE_CHAIN (current))
7773 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7774 java_check_methods (TREE_VALUE (current));
7775 java_parse_abort_on_error ();
7777 for (current = gclass_list; current; current = TREE_CHAIN (current))
7779 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
7780 layout_class (current_class);
7782 /* Error reported by the caller */
7783 if (java_error_count)
7784 return;
7787 /* We might have reloaded classes durign the process of laying out
7788 classes for code generation. We must layout the methods of those
7789 late additions, as constructor checks might use them */
7790 java_layout_seen_class_methods ();
7791 java_parse_abort_on_error ();
7794 /* Expand methods in the current set of classes remembered for
7795 generation. */
7797 static void
7798 java_complete_expand_classes (void)
7800 tree current;
7802 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7803 if (!INNER_CLASS_DECL_P (current))
7804 java_complete_expand_class (current);
7807 /* Expand the methods found in OUTER, starting first by OUTER's inner
7808 classes, if any. */
7810 static void
7811 java_complete_expand_class (tree outer)
7813 tree inner_list;
7815 /* We need to go after all inner classes and start expanding them,
7816 starting with most nested ones. We have to do that because nested
7817 classes might add functions to outer classes */
7819 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7820 inner_list; inner_list = TREE_CHAIN (inner_list))
7821 java_complete_expand_class (TREE_PURPOSE (inner_list));
7823 java_complete_expand_methods (outer);
7826 /* Expand methods registered in CLASS_DECL. The general idea is that
7827 we expand regular methods first. This allows us get an estimate on
7828 how outer context local alias fields are really used so we can add
7829 to the constructor just enough code to initialize them properly (it
7830 also lets us generate finit$ correctly.) Then we expand the
7831 constructors and then <clinit>. */
7833 static void
7834 java_complete_expand_methods (tree class_decl)
7836 tree clinit, decl, first_decl;
7838 output_class = current_class = TREE_TYPE (class_decl);
7840 /* Pre-expand <clinit> to figure whether we really need it or
7841 not. If we do need it, we pre-expand the static fields so they're
7842 ready to be used somewhere else. <clinit> will be fully expanded
7843 after we processed the constructors. */
7844 first_decl = TYPE_METHODS (current_class);
7845 clinit = maybe_generate_pre_expand_clinit (current_class);
7847 /* Then generate finit$ (if we need to) because constructors will
7848 try to use it.*/
7849 if (TYPE_FINIT_STMT_LIST (current_class))
7850 java_complete_expand_method (generate_finit (current_class));
7852 /* Then generate instinit$ (if we need to) because constructors will
7853 try to use it. */
7854 if (TYPE_II_STMT_LIST (current_class))
7855 java_complete_expand_method (generate_instinit (current_class));
7857 /* Now do the constructors */
7858 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7860 if (!DECL_CONSTRUCTOR_P (decl))
7861 continue;
7862 java_complete_expand_method (decl);
7865 /* First, do the ordinary methods. */
7866 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7868 /* Ctors aren't part of this batch. */
7869 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7870 continue;
7872 /* Skip abstract or native methods -- but do handle native
7873 methods when generating JNI stubs. */
7874 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7876 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7877 continue;
7880 if (METHOD_NATIVE (decl))
7882 tree body;
7883 current_function_decl = decl;
7884 body = build_jni_stub (decl);
7885 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7888 java_complete_expand_method (decl);
7891 /* If there is indeed a <clinit>, fully expand it now */
7892 if (clinit)
7894 /* Prevent the use of `this' inside <clinit> */
7895 ctxp->explicit_constructor_p = 1;
7896 java_complete_expand_method (clinit);
7897 ctxp->explicit_constructor_p = 0;
7900 /* We might have generated a class$ that we now want to expand */
7901 if (TYPE_DOT_CLASS (current_class))
7902 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7904 /* Now verify constructor circularity (stop after the first one we
7905 prove wrong.) */
7906 if (!CLASS_INTERFACE (class_decl))
7907 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7908 if (DECL_CONSTRUCTOR_P (decl)
7909 && verify_constructor_circularity (decl, decl))
7910 break;
7913 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7914 safely used in some other methods/constructors. */
7916 static tree
7917 maybe_generate_pre_expand_clinit (tree class_type)
7919 tree current, mdecl;
7921 if (!TYPE_CLINIT_STMT_LIST (class_type))
7922 return NULL_TREE;
7924 /* Go through all static fields and pre expand them */
7925 for (current = TYPE_FIELDS (class_type); current;
7926 current = TREE_CHAIN (current))
7927 if (FIELD_STATIC (current))
7928 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7930 /* Then build the <clinit> method */
7931 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7932 clinit_identifier_node, end_params_node);
7933 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7934 mdecl, NULL_TREE);
7935 start_artificial_method_body (mdecl);
7937 /* We process the list of assignment we produced as the result of
7938 the declaration of initialized static field and add them as
7939 statement to the <clinit> method. */
7940 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7941 current = TREE_CHAIN (current))
7943 tree stmt = current;
7944 /* We build the assignment expression that will initialize the
7945 field to its value. There are strict rules on static
7946 initializers (8.5). FIXME */
7947 if (TREE_CODE (stmt) != BLOCK && !IS_EMPTY_STMT (stmt))
7948 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7949 java_method_add_stmt (mdecl, stmt);
7952 end_artificial_method_body (mdecl);
7954 /* Now we want to place <clinit> as the last method (because we need
7955 it at least for interface so that it doesn't interfere with the
7956 dispatch table based lookup. */
7957 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7959 current = TREE_CHAIN (TYPE_METHODS (class_type));
7960 TYPE_METHODS (class_type) = current;
7962 while (TREE_CHAIN (current))
7963 current = TREE_CHAIN (current);
7965 TREE_CHAIN (current) = mdecl;
7966 TREE_CHAIN (mdecl) = NULL_TREE;
7969 return mdecl;
7972 /* Analyzes a method body and look for something that isn't a
7973 MODIFY_EXPR with a constant value. */
7975 static int
7976 analyze_clinit_body (tree this_class, tree bbody)
7978 while (bbody)
7979 switch (TREE_CODE (bbody))
7981 case BLOCK:
7982 bbody = BLOCK_EXPR_BODY (bbody);
7983 break;
7985 case EXPR_WITH_FILE_LOCATION:
7986 bbody = EXPR_WFL_NODE (bbody);
7987 break;
7989 case COMPOUND_EXPR:
7990 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7991 return 1;
7992 bbody = TREE_OPERAND (bbody, 1);
7993 break;
7995 case MODIFY_EXPR:
7996 /* If we're generating to class file and we're dealing with an
7997 array initialization, we return 1 to keep <clinit> */
7998 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7999 && flag_emit_class_files)
8000 return 1;
8002 /* There are a few cases where we're required to keep
8003 <clinit>:
8004 - If this is an assignment whose operand is not constant,
8005 - If this is an assignment to a non-initialized field,
8006 - If this field is not a member of the current class.
8008 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
8009 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
8010 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
8012 default:
8013 return 1;
8015 return 0;
8019 /* See whether we could get rid of <clinit>. Criteria are: all static
8020 final fields have constant initial values and the body of <clinit>
8021 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
8023 static int
8024 maybe_yank_clinit (tree mdecl)
8026 tree type, current;
8027 tree fbody, bbody;
8029 if (!DECL_CLINIT_P (mdecl))
8030 return 0;
8032 /* If the body isn't empty, then we keep <clinit>. Note that if
8033 we're emitting classfiles, this isn't enough not to rule it
8034 out. */
8035 fbody = DECL_FUNCTION_BODY (mdecl);
8036 bbody = BLOCK_EXPR_BODY (fbody);
8037 if (bbody && bbody != error_mark_node)
8038 bbody = BLOCK_EXPR_BODY (bbody);
8039 else
8040 return 0;
8041 if (bbody && ! flag_emit_class_files && !IS_EMPTY_STMT (bbody))
8042 return 0;
8044 type = DECL_CONTEXT (mdecl);
8045 current = TYPE_FIELDS (type);
8047 for (current = (current ? TREE_CHAIN (current) : current);
8048 current; current = TREE_CHAIN (current))
8050 tree f_init;
8052 /* We're not interested in non-static fields. */
8053 if (!FIELD_STATIC (current))
8054 continue;
8056 /* Nor in fields without initializers. */
8057 f_init = DECL_INITIAL (current);
8058 if (f_init == NULL_TREE)
8059 continue;
8061 /* Anything that isn't String or a basic type is ruled out -- or
8062 if we know how to deal with it (when doing things natively) we
8063 should generated an empty <clinit> so that SUID are computed
8064 correctly. */
8065 if (! JSTRING_TYPE_P (TREE_TYPE (current))
8066 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
8067 return 0;
8069 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
8070 return 0;
8073 /* Now we analyze the method body and look for something that
8074 isn't a MODIFY_EXPR */
8075 if (bbody && !IS_EMPTY_STMT (bbody) && analyze_clinit_body (type, bbody))
8076 return 0;
8078 /* Get rid of <clinit> in the class' list of methods */
8079 if (TYPE_METHODS (type) == mdecl)
8080 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
8081 else
8082 for (current = TYPE_METHODS (type); current;
8083 current = TREE_CHAIN (current))
8084 if (TREE_CHAIN (current) == mdecl)
8086 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8087 break;
8090 return 1;
8093 /* Install the argument from MDECL. Suitable to completion and
8094 expansion of mdecl's body. */
8096 void
8097 start_complete_expand_method (tree mdecl)
8099 tree tem;
8101 pushlevel (1); /* Prepare for a parameter push */
8102 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8103 DECL_ARGUMENTS (mdecl) = tem;
8105 for (; tem; tem = TREE_CHAIN (tem))
8107 /* TREE_CHAIN (tem) will change after pushdecl. */
8108 tree next = TREE_CHAIN (tem);
8109 tree type = TREE_TYPE (tem);
8110 if (targetm.calls.promote_prototypes (type)
8111 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8112 && INTEGRAL_TYPE_P (type))
8113 type = integer_type_node;
8114 DECL_ARG_TYPE (tem) = type;
8115 layout_decl (tem, 0);
8116 pushdecl (tem);
8117 /* Re-install the next so that the list is kept and the loop
8118 advances. */
8119 TREE_CHAIN (tem) = next;
8121 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8122 input_location = DECL_SOURCE_LOCATION (mdecl);
8123 build_result_decl (mdecl);
8127 /* Complete and expand a method. */
8129 static void
8130 java_complete_expand_method (tree mdecl)
8132 tree fbody, block_body, exception_copy;
8134 current_function_decl = mdecl;
8135 /* Fix constructors before expanding them */
8136 if (DECL_CONSTRUCTOR_P (mdecl))
8137 fix_constructors (mdecl);
8139 /* Expand functions that have a body */
8140 if (!DECL_FUNCTION_BODY (mdecl))
8141 return;
8143 fbody = DECL_FUNCTION_BODY (mdecl);
8144 block_body = BLOCK_EXPR_BODY (fbody);
8145 exception_copy = NULL_TREE;
8147 current_function_decl = mdecl;
8149 if (! quiet_flag)
8150 fprintf (stderr, " [%s.",
8151 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8152 announce_function (mdecl);
8153 if (! quiet_flag)
8154 fprintf (stderr, "]");
8156 /* Prepare the function for tree completion */
8157 start_complete_expand_method (mdecl);
8159 /* Install the current this */
8160 current_this = (!METHOD_STATIC (mdecl) ?
8161 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8163 /* Purge the `throws' list of unchecked exceptions (we save a copy
8164 of the list and re-install it later.) */
8165 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8166 purge_unchecked_exceptions (mdecl);
8168 /* Install exceptions thrown with `throws' */
8169 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8171 if (block_body != NULL_TREE)
8173 block_body = java_complete_tree (block_body);
8175 /* Before we check initialization, attached all class initialization
8176 variable to the block_body */
8177 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8178 attach_init_test_initialization_flags, block_body);
8180 if (! METHOD_NATIVE (mdecl))
8182 check_for_initialization (block_body, mdecl);
8184 /* Go through all the flags marking the initialization of
8185 static variables and see whether they're definitively
8186 assigned, in which case the type is remembered as
8187 definitively initialized in MDECL. */
8188 if (STATIC_CLASS_INIT_OPT_P ())
8190 /* Always register the context as properly initialized in
8191 MDECL. This used with caution helps removing extra
8192 initialization of self. */
8193 if (METHOD_STATIC (mdecl))
8195 *(htab_find_slot
8196 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8197 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8201 ctxp->explicit_constructor_p = 0;
8204 BLOCK_EXPR_BODY (fbody) = block_body;
8206 /* If we saw a return but couldn't evaluate it properly, we'll have
8207 an error_mark_node here. */
8208 if (block_body != error_mark_node
8209 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8210 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
8211 missing_return_error (current_function_decl);
8213 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8214 maybe_yank_clinit (mdecl);
8216 /* Pop the current level, with special measures if we found errors. */
8217 if (java_error_count)
8218 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8219 poplevel (1, 0, 1);
8221 /* Pop the exceptions and sanity check */
8222 POP_EXCEPTIONS();
8223 if (currently_caught_type_list)
8224 abort ();
8226 /* Restore the copy of the list of exceptions. */
8227 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8230 /* For with each class for which there's code to generate. */
8232 static void
8233 java_expand_method_bodies (tree class)
8235 tree decl;
8236 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8238 tree block;
8240 if (! DECL_FUNCTION_BODY (decl))
8241 continue;
8243 current_function_decl = decl;
8245 block = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8247 /* Save the function body for gimplify and inlining. */
8248 DECL_SAVED_TREE (decl) = block;
8250 /* It's time to assign the variable flagging static class
8251 initialization based on which classes invoked static methods
8252 are definitely initializing. This should be flagged. */
8253 if (STATIC_CLASS_INIT_OPT_P ())
8255 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8256 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8258 /* Executed for each statement calling a static function.
8259 LIST is a TREE_LIST whose PURPOSE is the called function
8260 and VALUE is a compound whose second operand can be patched
8261 with static class initialization flag assignments. */
8263 tree called_method = TREE_PURPOSE (list);
8264 tree compound = TREE_VALUE (list);
8265 tree assignment_compound_list
8266 = build_tree_list (called_method, NULL);
8268 /* For each class definitely initialized in
8269 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8270 assignment to the class initialization flag. */
8271 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8272 emit_test_initialization,
8273 assignment_compound_list);
8275 if (TREE_VALUE (assignment_compound_list))
8276 TREE_OPERAND (compound, 1)
8277 = TREE_VALUE (assignment_compound_list);
8281 /* Expand the function body. */
8282 source_end_java_method ();
8288 /* This section of the code deals with accessing enclosing context
8289 fields either directly by using the relevant access to this$<n> or
8290 by invoking an access method crafted for that purpose. */
8292 /* Build the necessary access from an inner class to an outer
8293 class. This routine could be optimized to cache previous result
8294 (decl, current_class and returned access). When an access method
8295 needs to be generated, it always takes the form of a read. It might
8296 be later turned into a write by calling outer_field_access_fix. */
8298 static tree
8299 build_outer_field_access (tree id, tree decl)
8301 tree access = NULL_TREE;
8302 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8303 tree decl_ctx = DECL_CONTEXT (decl);
8305 /* If the immediate enclosing context of the current class is the
8306 field decl's class or inherits from it; build the access as
8307 `this$<n>.<field>'. Note that we will break the `private' barrier
8308 if we're not emitting bytecodes. */
8309 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8310 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8312 tree thisn = build_current_thisn (current_class);
8313 access = make_qualified_primary (build_wfl_node (thisn),
8314 id, EXPR_WFL_LINECOL (id));
8316 /* Otherwise, generate access methods to outer this and access the
8317 field (either using an access method or by direct access.) */
8318 else
8320 int lc = EXPR_WFL_LINECOL (id);
8322 /* Now we chain the required number of calls to the access$0 to
8323 get a hold to the enclosing instance we need, and then we
8324 build the field access. */
8325 access = build_access_to_thisn (current_class, decl_ctx, lc);
8327 /* If the field is private and we're generating bytecode, then
8328 we generate an access method */
8329 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8331 tree name = build_outer_field_access_methods (decl);
8332 access = build_outer_field_access_expr (lc, decl_ctx,
8333 name, access, NULL_TREE);
8335 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8336 Once again we break the `private' access rule from a foreign
8337 class. */
8338 else
8339 access = make_qualified_primary (access, id, lc);
8341 return resolve_expression_name (access, NULL);
8344 /* Return a nonzero value if NODE describes an outer field inner
8345 access. */
8347 static int
8348 outer_field_access_p (tree type, tree decl)
8350 if (!INNER_CLASS_TYPE_P (type)
8351 || TREE_CODE (decl) != FIELD_DECL
8352 || DECL_CONTEXT (decl) == type)
8353 return 0;
8355 /* If the inner class extends the declaration context of the field
8356 we're trying to access, then this isn't an outer field access */
8357 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8358 return 0;
8360 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8361 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8363 if (type == DECL_CONTEXT (decl))
8364 return 1;
8366 if (!DECL_CONTEXT (TYPE_NAME (type)))
8368 /* Before we give up, see whether the field is inherited from
8369 the enclosing context we're considering. */
8370 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8371 return 1;
8372 break;
8376 return 0;
8379 /* Return a nonzero value if NODE represents an outer field inner
8380 access that was been already expanded. As a side effect, it returns
8381 the name of the field being accessed and the argument passed to the
8382 access function, suitable for a regeneration of the access method
8383 call if necessary. */
8385 static int
8386 outer_field_expanded_access_p (tree node, tree *name, tree *arg_type,
8387 tree *arg)
8389 int identified = 0;
8391 if (TREE_CODE (node) != CALL_EXPR)
8392 return 0;
8394 /* Well, gcj generates slightly different tree nodes when compiling
8395 to native or bytecodes. It's the case for function calls. */
8397 if (flag_emit_class_files
8398 && TREE_CODE (node) == CALL_EXPR
8399 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8400 identified = 1;
8401 else if (!flag_emit_class_files)
8403 node = TREE_OPERAND (node, 0);
8405 if (node && TREE_OPERAND (node, 0)
8406 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8408 node = TREE_OPERAND (node, 0);
8409 if (TREE_OPERAND (node, 0)
8410 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8411 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8412 (DECL_NAME (TREE_OPERAND (node, 0)))))
8413 identified = 1;
8417 if (identified && name && arg_type && arg)
8419 tree argument = TREE_OPERAND (node, 1);
8420 *name = DECL_NAME (TREE_OPERAND (node, 0));
8421 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8422 *arg = TREE_VALUE (argument);
8424 return identified;
8427 /* Detect in NODE an outer field read access from an inner class and
8428 transform it into a write with RHS as an argument. This function is
8429 called from the java_complete_lhs when an assignment to a LHS can
8430 be identified. */
8432 static tree
8433 outer_field_access_fix (tree wfl, tree node, tree rhs)
8435 tree name, arg_type, arg;
8437 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8439 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8440 arg_type, name, arg, rhs);
8441 return java_complete_tree (node);
8443 return NULL_TREE;
8446 /* Construct the expression that calls an access method:
8447 <type>.access$<n>(<arg1> [, <arg2>]);
8449 ARG2 can be NULL and will be omitted in that case. It will denote a
8450 read access. */
8452 static tree
8453 build_outer_field_access_expr (int lc, tree type, tree access_method_name,
8454 tree arg1, tree arg2)
8456 tree args, cn, access;
8458 args = arg1 ? arg1 :
8459 build_wfl_node (build_current_thisn (current_class));
8460 args = build_tree_list (NULL_TREE, args);
8462 if (arg2)
8463 args = tree_cons (NULL_TREE, arg2, args);
8465 access = build_method_invocation (build_wfl_node (access_method_name), args);
8466 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8467 return make_qualified_primary (cn, access, lc);
8470 static tree
8471 build_new_access_id (void)
8473 static int access_n_counter = 1;
8474 char buffer [128];
8476 sprintf (buffer, "access$%d", access_n_counter++);
8477 return get_identifier (buffer);
8480 /* Create the static access functions for the outer field DECL. We define a
8481 read:
8482 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8483 return inst$.field;
8485 and a write access:
8486 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8487 TREE_TYPE (<field>) value$) {
8488 return inst$.field = value$;
8490 We should have a usage flags on the DECL so we can lazily turn the ones
8491 we're using for code generation. FIXME.
8494 static tree
8495 build_outer_field_access_methods (tree decl)
8497 tree id, args, stmt, mdecl;
8499 if (FIELD_INNER_ACCESS_P (decl))
8500 return FIELD_INNER_ACCESS (decl);
8502 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8504 /* Create the identifier and a function named after it. */
8505 id = build_new_access_id ();
8507 /* The identifier is marked as bearing the name of a generated write
8508 access function for outer field accessed from inner classes. */
8509 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8511 /* Create the read access */
8512 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8513 TREE_CHAIN (args) = end_params_node;
8514 stmt = make_qualified_primary (build_wfl_node (inst_id),
8515 build_wfl_node (DECL_NAME (decl)), 0);
8516 stmt = build_return (0, stmt);
8517 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8518 TREE_TYPE (decl), id, args, stmt);
8519 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8521 /* Create the write access method. No write access for final variable */
8522 if (!FIELD_FINAL (decl))
8524 args = build_tree_list (inst_id,
8525 build_pointer_type (DECL_CONTEXT (decl)));
8526 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8527 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8528 stmt = make_qualified_primary (build_wfl_node (inst_id),
8529 build_wfl_node (DECL_NAME (decl)), 0);
8530 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8531 build_wfl_node (wpv_id)));
8532 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8533 TREE_TYPE (decl), id,
8534 args, stmt);
8536 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8538 /* Return the access name */
8539 return FIELD_INNER_ACCESS (decl) = id;
8542 /* Build an field access method NAME. */
8544 static tree
8545 build_outer_field_access_method (tree class, tree type, tree name,
8546 tree args, tree body)
8548 tree saved_current_function_decl, mdecl;
8550 /* Create the method */
8551 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8552 fix_method_argument_names (args, mdecl);
8553 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8555 /* Attach the method body. */
8556 saved_current_function_decl = current_function_decl;
8557 start_artificial_method_body (mdecl);
8558 java_method_add_stmt (mdecl, body);
8559 end_artificial_method_body (mdecl);
8560 current_function_decl = saved_current_function_decl;
8562 return mdecl;
8566 /* This section deals with building access function necessary for
8567 certain kinds of method invocation from inner classes. */
8569 static tree
8570 build_outer_method_access_method (tree decl)
8572 tree saved_current_function_decl, mdecl;
8573 tree args = NULL_TREE, call_args = NULL_TREE;
8574 tree carg, id, body, class;
8575 char buffer [80];
8576 int parm_id_count = 0;
8578 /* Test this abort with an access to a private field */
8579 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8580 abort ();
8582 /* Check the cache first */
8583 if (DECL_FUNCTION_INNER_ACCESS (decl))
8584 return DECL_FUNCTION_INNER_ACCESS (decl);
8586 class = DECL_CONTEXT (decl);
8588 /* Obtain an access identifier and mark it */
8589 id = build_new_access_id ();
8590 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8592 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8593 /* Create the arguments, as much as the original */
8594 for (; carg && carg != end_params_node;
8595 carg = TREE_CHAIN (carg))
8597 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8598 args = chainon (args, build_tree_list (get_identifier (buffer),
8599 TREE_VALUE (carg)));
8601 args = chainon (args, end_params_node);
8603 /* Create the method */
8604 mdecl = create_artificial_method (class, ACC_STATIC,
8605 TREE_TYPE (TREE_TYPE (decl)), id, args);
8606 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8607 /* There is a potential bug here. We should be able to use
8608 fix_method_argument_names, but then arg names get mixed up and
8609 eventually a constructor will have its this$0 altered and the
8610 outer context won't be assignment properly. The testcase is
8611 stub.java FIXME */
8612 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8614 /* Attach the method body. */
8615 saved_current_function_decl = current_function_decl;
8616 start_artificial_method_body (mdecl);
8618 /* The actual method invocation uses the same args. When invoking a
8619 static methods that way, we don't want to skip the first
8620 argument. */
8621 carg = args;
8622 if (!METHOD_STATIC (decl))
8623 carg = TREE_CHAIN (carg);
8624 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8625 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8626 call_args);
8628 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8629 call_args);
8630 if (!METHOD_STATIC (decl))
8631 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8632 body, 0);
8633 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8634 body = build_return (0, body);
8635 java_method_add_stmt (mdecl,body);
8636 end_artificial_method_body (mdecl);
8637 current_function_decl = saved_current_function_decl;
8639 /* Back tag the access function so it know what it accesses */
8640 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8642 /* Tag the current method so it knows it has an access generated */
8643 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8647 /* This section of the code deals with building expressions to access
8648 the enclosing instance of an inner class. The enclosing instance is
8649 kept in a generated field called this$<n>, with <n> being the
8650 inner class nesting level (starting from 0.) */
8652 /* Build an access to a given this$<n>, always chaining access call to
8653 others. Access methods to this$<n> are build on the fly if
8654 necessary. This CAN'T be used to solely access this$<n-1> from
8655 this$<n> (which alway yield to special cases and optimization, see
8656 for example build_outer_field_access). */
8658 static tree
8659 build_access_to_thisn (tree from, tree to, int lc)
8661 tree access = NULL_TREE;
8663 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8665 if (!access)
8667 access = build_current_thisn (from);
8668 access = build_wfl_node (access);
8670 else
8672 tree access0_wfl, cn;
8674 maybe_build_thisn_access_method (from);
8675 access0_wfl = build_wfl_node (access0_identifier_node);
8676 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8677 EXPR_WFL_LINECOL (access0_wfl) = lc;
8678 access = build_tree_list (NULL_TREE, access);
8679 access = build_method_invocation (access0_wfl, access);
8680 access = make_qualified_primary (cn, access, lc);
8683 /* If FROM isn't an inner class, that's fine, we've done enough.
8684 What we're looking for can be accessed from there. */
8685 from = DECL_CONTEXT (TYPE_NAME (from));
8686 if (!from)
8687 break;
8688 from = TREE_TYPE (from);
8690 return access;
8693 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8694 is returned if nothing needs to be generated. Otherwise, the method
8695 generated and a method decl is returned.
8697 NOTE: These generated methods should be declared in a class file
8698 attribute so that they can't be referred to directly. */
8700 static tree
8701 maybe_build_thisn_access_method (tree type)
8703 tree mdecl, args, stmt, rtype;
8704 tree saved_current_function_decl;
8706 /* If TYPE is a top-level class, no access method is required.
8707 If there already is such an access method, bail out. */
8708 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8709 return NULL_TREE;
8711 /* We generate the method. The method looks like:
8712 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8714 args = build_tree_list (inst_id, build_pointer_type (type));
8715 TREE_CHAIN (args) = end_params_node;
8716 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8717 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8718 access0_identifier_node, args);
8719 fix_method_argument_names (args, mdecl);
8720 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8721 stmt = build_current_thisn (type);
8722 stmt = make_qualified_primary (build_wfl_node (inst_id),
8723 build_wfl_node (stmt), 0);
8724 stmt = build_return (0, stmt);
8726 saved_current_function_decl = current_function_decl;
8727 start_artificial_method_body (mdecl);
8728 java_method_add_stmt (mdecl, stmt);
8729 end_artificial_method_body (mdecl);
8730 current_function_decl = saved_current_function_decl;
8732 CLASS_ACCESS0_GENERATED_P (type) = 1;
8734 return mdecl;
8737 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8738 the first level of innerclassing. this$1 for the next one, etc...
8739 This function can be invoked with TYPE to NULL, available and then
8740 has to count the parser context. */
8742 static GTY(()) tree saved_thisn;
8743 static GTY(()) tree saved_type;
8745 static tree
8746 build_current_thisn (tree type)
8748 static int saved_i = -1;
8749 static int saved_type_i = 0;
8750 tree decl;
8751 char buffer [24];
8752 int i = 0;
8754 if (type)
8756 if (type == saved_type)
8757 i = saved_type_i;
8758 else
8760 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8761 decl; decl = DECL_CONTEXT (decl), i++)
8764 saved_type = type;
8765 saved_type_i = i;
8768 else
8769 i = list_length (GET_CPC_LIST ())-2;
8771 if (i == saved_i)
8772 return saved_thisn;
8774 sprintf (buffer, "this$%d", i);
8775 saved_i = i;
8776 saved_thisn = get_identifier (buffer);
8777 return saved_thisn;
8780 /* Return the assignment to the hidden enclosing context `this$<n>'
8781 by the second incoming parameter to the innerclass constructor. The
8782 form used is `this.this$<n> = this$<n>;'. */
8784 static tree
8785 build_thisn_assign (void)
8787 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8789 tree thisn = build_current_thisn (current_class);
8790 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8791 build_wfl_node (thisn), 0);
8792 tree rhs = build_wfl_node (thisn);
8793 #ifdef USE_MAPPED_LOCATION
8794 SET_EXPR_LOCATION (lhs, input_location);
8795 #else
8796 EXPR_WFL_SET_LINECOL (lhs, input_line, 0);
8797 #endif
8798 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8800 return NULL_TREE;
8804 /* Building the synthetic `class$' used to implement the `.class' 1.1
8805 extension for non primitive types. This method looks like:
8807 static Class class$(String type) throws NoClassDefFoundError
8809 try {return (java.lang.Class.forName (String));}
8810 catch (ClassNotFoundException e) {
8811 throw new NoClassDefFoundError(e.getMessage());}
8812 } */
8814 static GTY(()) tree get_message_wfl;
8815 static GTY(()) tree type_parm_wfl;
8817 static tree
8818 build_dot_class_method (tree class)
8820 #define BWF(S) build_wfl_node (get_identifier ((S)))
8821 #ifdef USE_MAPPED_LOCATION
8822 #define MQN(X,Y) make_qualified_name ((X), (Y), UNKNOWN_LOCATION)
8823 #else
8824 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8825 #endif
8826 tree args, tmp, saved_current_function_decl, mdecl, qual_name;
8827 tree stmt, throw_stmt;
8829 if (!get_message_wfl)
8831 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8832 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8835 /* Build the arguments */
8836 args = build_tree_list (get_identifier ("type$"),
8837 build_pointer_type (string_type_node));
8838 TREE_CHAIN (args) = end_params_node;
8840 /* Build the qualified name java.lang.Class.forName */
8841 tmp = MQN (MQN (MQN (BWF ("java"),
8842 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8844 /* Create the "class$" function */
8845 mdecl = create_artificial_method (class, ACC_STATIC,
8846 build_pointer_type (class_type_node),
8847 classdollar_identifier_node, args);
8848 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8849 BWF ("NoClassDefFoundError"));
8850 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE, qual_name);
8851 register_incomplete_type (JDEP_EXCEPTION, qual_name, NULL_TREE, NULL_TREE);
8852 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
8853 &TREE_VALUE (DECL_FUNCTION_THROWS (mdecl));
8855 /* We start by building the try block. We need to build:
8856 return (java.lang.Class.forName (type)); */
8857 stmt = build_method_invocation (tmp,
8858 build_tree_list (NULL_TREE, type_parm_wfl));
8859 stmt = build_return (0, stmt);
8861 /* Now onto the catch block. We start by building the expression
8862 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8863 #ifdef USE_MAPPED_LOCATION
8864 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8865 get_message_wfl, UNKNOWN_LOCATION);
8866 #else
8867 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8868 get_message_wfl, 0);
8869 #endif
8870 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8872 /* Build new NoClassDefFoundError (_.getMessage) */
8873 throw_stmt = build_new_invocation
8874 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8875 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8877 /* Build the throw, (it's too early to use BUILD_THROW) */
8878 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8880 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8881 qual_name = MQN (MQN (BWF ("java"), BWF ("lang")),
8882 BWF ("ClassNotFoundException"));
8883 stmt = encapsulate_with_try_catch (0, qual_name, stmt, throw_stmt);
8885 fix_method_argument_names (args, mdecl);
8886 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8887 saved_current_function_decl = current_function_decl;
8888 start_artificial_method_body (mdecl);
8889 java_method_add_stmt (mdecl, stmt);
8890 end_artificial_method_body (mdecl);
8891 current_function_decl = saved_current_function_decl;
8892 TYPE_DOT_CLASS (class) = mdecl;
8894 return mdecl;
8897 static tree
8898 build_dot_class_method_invocation (tree this_class, tree type)
8900 tree dot_class_method = TYPE_DOT_CLASS (this_class);
8901 tree sig_id, s, t;
8903 if (TYPE_ARRAY_P (type))
8904 sig_id = build_java_signature (type);
8905 else
8906 sig_id = DECL_NAME (TYPE_NAME (type));
8908 /* Ensure that the proper name separator is used */
8909 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8910 IDENTIFIER_LENGTH (sig_id));
8912 s = build_string (IDENTIFIER_LENGTH (sig_id),
8913 IDENTIFIER_POINTER (sig_id));
8914 t = build_method_invocation (build_wfl_node (DECL_NAME (dot_class_method)),
8915 build_tree_list (NULL_TREE, s));
8916 if (DECL_CONTEXT (dot_class_method) != this_class)
8918 tree class_name = DECL_NAME (TYPE_NAME (DECL_CONTEXT (dot_class_method)));
8919 t = make_qualified_primary (build_wfl_node (class_name), t, 0);
8921 return t;
8924 /* This section of the code deals with constructor. */
8926 /* Craft a body for default constructor. Patch existing constructor
8927 bodies with call to super() and field initialization statements if
8928 necessary. */
8930 static void
8931 fix_constructors (tree mdecl)
8933 tree iii; /* Instance Initializer Invocation */
8934 tree *bodyp = &DECL_FUNCTION_BODY (mdecl);
8935 tree thisn_assign, compound = NULL_TREE;
8936 tree class_type = DECL_CONTEXT (mdecl);
8938 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8939 return;
8940 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8942 if (!*bodyp)
8944 /* It is an error for the compiler to generate a default
8945 constructor if the superclass doesn't have a constructor that
8946 takes no argument, or the same args for an anonymous class */
8947 if (verify_constructor_super (mdecl))
8949 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8950 tree save = DECL_NAME (mdecl);
8951 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8952 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8953 parse_error_context
8954 (lookup_cl (TYPE_NAME (class_type)),
8955 "No constructor matching %qs found in class %qs",
8956 lang_printable_name (mdecl, 2), n);
8957 DECL_NAME (mdecl) = save;
8960 /* The constructor body must be crafted by hand. It's the
8961 constructor we defined when we realize we didn't have the
8962 CLASSNAME() constructor */
8963 start_artificial_method_body (mdecl);
8965 /* Insert an assignment to the this$<n> hidden field, if
8966 necessary */
8967 if ((thisn_assign = build_thisn_assign ()))
8968 java_method_add_stmt (mdecl, thisn_assign);
8970 /* We don't generate a super constructor invocation if we're
8971 compiling java.lang.Object. build_super_invocation takes care
8972 of that. */
8973 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8975 /* FIXME */
8976 if ((iii = build_instinit_invocation (class_type)))
8977 java_method_add_stmt (mdecl, iii);
8979 end_artificial_method_body (mdecl);
8981 /* Search for an explicit constructor invocation */
8982 else
8984 int found = 0;
8985 int invokes_this = 0;
8986 tree main_block = BLOCK_EXPR_BODY (*bodyp);
8988 while (*bodyp)
8990 tree body = *bodyp;
8991 switch (TREE_CODE (body))
8993 case CALL_EXPR:
8994 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8995 if (CALL_THIS_CONSTRUCTOR_P (body))
8996 invokes_this = 1;
8997 break;
8998 case COMPOUND_EXPR:
8999 case EXPR_WITH_FILE_LOCATION:
9000 bodyp = &TREE_OPERAND (body, 0);
9001 continue;
9002 case BLOCK:
9003 bodyp = &BLOCK_EXPR_BODY (body);
9004 continue;
9005 default:
9006 break;
9008 break;
9011 /* Generate the assignment to this$<n>, if necessary */
9012 if ((thisn_assign = build_thisn_assign ()))
9013 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
9015 /* The constructor is missing an invocation of super() */
9016 if (!found)
9017 compound = add_stmt_to_compound (compound, NULL_TREE,
9018 build_super_invocation (mdecl));
9019 /* Explicit super() invocation should take place before the
9020 instance initializer blocks. */
9021 else
9023 compound = add_stmt_to_compound (compound, NULL_TREE, *bodyp);
9024 *bodyp = build_java_empty_stmt ();
9027 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
9029 /* Insert the instance initializer block right after. */
9030 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
9031 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
9033 /* Fix the constructor main block if we're adding extra stmts */
9034 if (compound)
9036 compound = add_stmt_to_compound (compound, NULL_TREE,
9037 BLOCK_EXPR_BODY (main_block));
9038 BLOCK_EXPR_BODY (main_block) = compound;
9043 /* Browse constructors in the super class, searching for a constructor
9044 that doesn't take any argument. Return 0 if one is found, 1
9045 otherwise. If the current class is an anonymous inner class, look
9046 for something that has the same signature. */
9048 static int
9049 verify_constructor_super (tree mdecl)
9051 tree class = CLASSTYPE_SUPER (current_class);
9052 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
9053 tree sdecl;
9055 if (!class)
9056 return 0;
9058 if (ANONYMOUS_CLASS_P (current_class))
9060 tree mdecl_arg_type;
9061 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
9062 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9063 if (DECL_CONSTRUCTOR_P (sdecl))
9065 tree m_arg_type;
9066 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9067 if (super_inner)
9068 arg_type = TREE_CHAIN (arg_type);
9069 for (m_arg_type = mdecl_arg_type;
9070 (arg_type != end_params_node
9071 && m_arg_type != end_params_node);
9072 arg_type = TREE_CHAIN (arg_type),
9073 m_arg_type = TREE_CHAIN (m_arg_type))
9074 if (!valid_method_invocation_conversion_p
9075 (TREE_VALUE (arg_type),
9076 TREE_VALUE (m_arg_type)))
9077 break;
9079 if (arg_type == end_params_node && m_arg_type == end_params_node)
9080 return 0;
9083 else
9085 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9087 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9088 if (super_inner)
9089 arg = TREE_CHAIN (arg);
9090 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9091 return 0;
9094 return 1;
9097 /* Generate code for all context remembered for code generation. */
9099 static GTY(()) tree reversed_class_list;
9100 void
9101 java_expand_classes (void)
9103 int save_error_count = 0;
9104 static struct parser_ctxt *cur_ctxp = NULL;
9105 location_t save_location;
9107 java_parse_abort_on_error ();
9108 if (!(ctxp = ctxp_for_generation))
9109 return;
9110 java_layout_classes ();
9111 java_parse_abort_on_error ();
9112 save_location = input_location;
9114 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9116 tree current;
9117 for (current = cur_ctxp->class_list;
9118 current;
9119 current = TREE_CHAIN (current))
9120 gen_indirect_dispatch_tables (TREE_TYPE (current));
9123 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9125 ctxp = cur_ctxp;
9126 input_location = ctxp->file_start_location;
9127 lang_init_source (2); /* Error msgs have method prototypes */
9128 java_complete_expand_classes (); /* Complete and expand classes */
9129 java_parse_abort_on_error ();
9131 input_location = save_location;
9133 /* Find anonymous classes and expand their constructor. This extra pass is
9134 necessary because the constructor itself is only generated when the
9135 method in which it is defined is expanded. */
9136 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9138 tree current;
9139 ctxp = cur_ctxp;
9140 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9142 output_class = current_class = TREE_TYPE (current);
9143 if (ANONYMOUS_CLASS_P (current_class))
9145 tree d;
9146 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9148 if (DECL_CONSTRUCTOR_P (d))
9150 java_complete_expand_method (d);
9151 break; /* There is only one constructor. */
9158 /* Expanding the constructors of anonymous classes generates access
9159 methods. Scan all the methods looking for null DECL_RESULTs --
9160 this will be the case if a method hasn't been expanded. */
9161 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9163 tree current;
9164 ctxp = cur_ctxp;
9165 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9167 tree d;
9168 output_class = current_class = TREE_TYPE (current);
9169 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9171 if (DECL_RESULT (d) == NULL_TREE)
9172 java_complete_expand_method (d);
9177 /* ??? Instead of all this we could iterate around the list of
9178 classes until there were no more un-expanded methods. It would
9179 take a little longer -- one pass over the whole list of methods
9180 -- but it would be simpler. Like this: */
9181 #if 0
9183 int something_changed;
9187 something_changed = 0;
9188 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9190 tree current;
9191 ctxp = cur_ctxp;
9192 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9194 tree d;
9195 output_class = current_class = TREE_TYPE (current);
9196 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9198 if (DECL_RESULT (d) == NULL_TREE)
9200 something_changed = 1;
9201 java_complete_expand_method (d);
9207 while (something_changed);
9209 #endif
9211 /* If we've found error at that stage, don't try to generate
9212 anything, unless we're checking the syntax only
9213 (but not using -fsyntax-only for the purpose of generating
9214 bytecode). */
9215 if (java_error_count
9216 && (!flag_syntax_only && !flag_emit_class_files))
9217 return;
9219 /* Now things are stable, go for generation of the class data. */
9221 /* We pessimistically marked all methods and fields external until
9222 we knew what set of classes we were planning to compile. Now mark
9223 those that will be generated locally as not external. */
9224 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9226 tree current;
9227 ctxp = cur_ctxp;
9228 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9229 java_mark_class_local (TREE_TYPE (current));
9232 /* Compile the classes. */
9233 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9235 tree current;
9236 reversed_class_list = NULL;
9238 ctxp = cur_ctxp;
9240 /* We write out the classes in reverse order. This ensures that
9241 inner classes are written before their containing classes,
9242 which is important for parallel builds. Otherwise, the
9243 class file for the outer class may be found, but the class
9244 file for the inner class may not be present. In that
9245 situation, the compiler cannot fall back to the original
9246 source, having already read the outer class, so we must
9247 prevent that situation. */
9248 for (current = ctxp->class_list;
9249 current;
9250 current = TREE_CHAIN (current))
9251 reversed_class_list
9252 = tree_cons (NULL_TREE, current, reversed_class_list);
9254 for (current = reversed_class_list;
9255 current;
9256 current = TREE_CHAIN (current))
9258 output_class = current_class = TREE_TYPE (TREE_VALUE (current));
9259 if (flag_emit_class_files)
9260 write_classfile (current_class);
9261 else if (! flag_syntax_only)
9262 java_expand_method_bodies (current_class);
9267 void
9268 java_finish_classes (void)
9270 static struct parser_ctxt *cur_ctxp = NULL;
9271 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9273 tree current;
9274 ctxp = cur_ctxp;
9275 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9277 output_class = current_class = TREE_TYPE (current);
9278 finish_class ();
9283 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9284 a tree list node containing RIGHT. Fore coming RIGHTs will be
9285 chained to this hook. LOCATION contains the location of the
9286 separating `.' operator. */
9288 static tree
9289 make_qualified_primary (tree primary, tree right, int location)
9291 tree wfl;
9293 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9294 wfl = build_wfl_wrap (primary, location);
9295 else
9297 wfl = primary;
9298 /* If wfl wasn't qualified, we build a first anchor */
9299 if (!EXPR_WFL_QUALIFICATION (wfl))
9300 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9303 /* And chain them */
9304 EXPR_WFL_LINECOL (right) = location;
9305 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9306 PRIMARY_P (wfl) = 1;
9307 return wfl;
9310 /* Simple merge of two name separated by a `.' */
9312 static tree
9313 merge_qualified_name (tree left, tree right)
9315 tree node;
9316 if (!left && !right)
9317 return NULL_TREE;
9319 if (!left)
9320 return right;
9322 if (!right)
9323 return left;
9325 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9326 IDENTIFIER_LENGTH (left));
9327 obstack_1grow (&temporary_obstack, '.');
9328 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9329 IDENTIFIER_LENGTH (right));
9330 node = get_identifier (obstack_base (&temporary_obstack));
9331 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9332 QUALIFIED_P (node) = 1;
9333 return node;
9336 /* Merge the two parts of a qualified name into LEFT. Set the
9337 location information of the resulting node to LOCATION, usually
9338 inherited from the location information of the `.' operator. */
9340 static tree
9341 make_qualified_name (tree left, tree right,
9342 #ifdef USE_MAPPED_LOCATION
9343 source_location location
9344 #else
9345 int location
9346 #endif
9349 #ifdef USE_COMPONENT_REF
9350 tree node = build3 (COMPONENT_REF, NULL_TREE, left, right, NULL_TREE);
9351 SET_EXPR_LOCATION (node, location);
9352 return node;
9353 #else
9354 tree left_id = EXPR_WFL_NODE (left);
9355 tree right_id = EXPR_WFL_NODE (right);
9356 tree wfl, merge;
9358 merge = merge_qualified_name (left_id, right_id);
9360 /* Left wasn't qualified and is now qualified */
9361 #ifdef USE_MAPPED_LOCATION
9362 if (!QUALIFIED_P (left_id))
9364 tree wfl = build_expr_wfl (left_id, EXPR_LOCATION (left));
9365 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9368 wfl = build_expr_wfl (right_id, location);
9369 #else
9370 if (!QUALIFIED_P (left_id))
9372 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9373 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9374 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9377 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9378 EXPR_WFL_LINECOL (wfl) = location;
9379 #endif
9380 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9381 EXPR_WFL_NODE (left) = merge;
9382 return left;
9383 #endif
9386 /* Extract the last identifier component of the qualified in WFL. The
9387 last identifier is removed from the linked list */
9389 static tree
9390 cut_identifier_in_qualified (tree wfl)
9392 tree q;
9393 tree previous = NULL_TREE;
9394 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9395 if (!TREE_CHAIN (q))
9397 if (!previous)
9398 /* Operating on a non qualified qualified WFL. */
9399 abort ();
9401 TREE_CHAIN (previous) = NULL_TREE;
9402 return TREE_PURPOSE (q);
9406 /* Resolve the expression name NAME. Return its decl. */
9408 static tree
9409 resolve_expression_name (tree id, tree *orig)
9411 tree name = EXPR_WFL_NODE (id);
9412 tree decl;
9414 /* 6.5.5.1: Simple expression names */
9415 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9417 /* 15.13.1: NAME can appear within the scope of a local variable
9418 declaration */
9419 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9420 return decl;
9422 /* 15.13.1: NAME can appear within a class declaration */
9423 else
9425 decl = lookup_field_wrapper (current_class, name);
9426 if (decl)
9428 tree access = NULL_TREE;
9429 int fs = FIELD_STATIC (decl);
9431 /* If we're accessing an outer scope local alias, make
9432 sure we change the name of the field we're going to
9433 build access to. */
9434 if (FIELD_LOCAL_ALIAS_USED (decl))
9435 name = DECL_NAME (decl);
9437 check_deprecation (id, decl);
9439 /* Instance variable (8.3.1.1) can't appear within
9440 static method, static initializer or initializer for
9441 a static variable. */
9442 if (!fs && METHOD_STATIC (current_function_decl))
9444 static_ref_err (id, name, current_class);
9445 return error_mark_node;
9447 /* Instance variables can't appear as an argument of
9448 an explicit constructor invocation */
9449 if (!fs && ctxp->explicit_constructor_p
9450 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9452 parse_error_context
9453 (id, "Can't reference %qs before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9454 return error_mark_node;
9457 /* If we're processing an inner class and we're trying
9458 to access a field belonging to an outer class, build
9459 the access to the field */
9460 if (!fs && outer_field_access_p (current_class, decl))
9462 if (CLASS_STATIC (TYPE_NAME (current_class)))
9464 static_ref_err (id, DECL_NAME (decl), current_class);
9465 return error_mark_node;
9467 access = build_outer_field_access (id, decl);
9468 if (orig)
9469 *orig = access;
9470 return access;
9473 /* Otherwise build what it takes to access the field */
9474 access = build_field_ref ((fs ? NULL_TREE : current_this),
9475 DECL_CONTEXT (decl), name);
9476 if (fs)
9477 access = maybe_build_class_init_for_field (decl, access);
9478 /* We may be asked to save the real field access node */
9479 if (orig)
9480 *orig = access;
9481 /* Last check: can we access the field? */
9482 if (not_accessible_p (current_class, decl, NULL_TREE, 0))
9484 not_accessible_field_error (id, decl);
9485 return error_mark_node;
9487 /* And we return what we got */
9488 return access;
9490 /* Fall down to error report on undefined variable */
9493 /* 6.5.5.2 Qualified Expression Names */
9494 else
9496 if (orig)
9497 *orig = NULL_TREE;
9498 qualify_ambiguous_name (id);
9499 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9500 /* 15.10.2: Accessing Superclass Members using super */
9501 return resolve_field_access (id, orig, NULL);
9504 /* We've got an error here */
9505 if (INNER_CLASS_TYPE_P (current_class))
9506 parse_error_context (id,
9507 "Local variable %qs can't be accessed from within the inner class %qs unless it is declared final",
9508 IDENTIFIER_POINTER (name),
9509 IDENTIFIER_POINTER (DECL_NAME
9510 (TYPE_NAME (current_class))));
9511 else
9512 parse_error_context (id, "Undefined variable %qs",
9513 IDENTIFIER_POINTER (name));
9515 return error_mark_node;
9518 static void
9519 static_ref_err (tree wfl, tree field_id, tree class_type)
9521 parse_error_context
9522 (wfl,
9523 "Can't make a static reference to nonstatic variable %qs in class %qs",
9524 IDENTIFIER_POINTER (field_id),
9525 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9528 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9529 We return something suitable to generate the field access. We also
9530 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9531 recipient's address can be null. */
9533 static tree
9534 resolve_field_access (tree qual_wfl, tree *field_decl, tree *field_type)
9536 int is_static = 0;
9537 tree field_ref;
9538 tree decl = NULL_TREE, where_found, type_found;
9540 if (resolve_qualified_expression_name (qual_wfl, &decl,
9541 &where_found, &type_found))
9542 return error_mark_node;
9544 /* Resolve the LENGTH field of an array here */
9545 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9546 && type_found && TYPE_ARRAY_P (type_found)
9547 && ! flag_emit_class_files)
9549 tree length = build_java_array_length_access (where_found);
9550 field_ref = length;
9552 /* In case we're dealing with a static array, we need to
9553 initialize its class before the array length can be fetched. */
9554 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9556 build_static_field_ref (where_found);
9557 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9560 /* We might have been trying to resolve field.method(). In which
9561 case, the resolution is over and decl is the answer */
9562 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9563 field_ref = decl;
9564 else if (JDECL_P (decl))
9566 if (!type_found)
9567 type_found = DECL_CONTEXT (decl);
9568 is_static = FIELD_STATIC (decl);
9569 field_ref = build_field_ref ((is_static ?
9570 NULL_TREE : where_found),
9571 type_found, DECL_NAME (decl));
9572 if (field_ref == error_mark_node)
9573 return error_mark_node;
9574 if (is_static)
9575 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9577 /* If we're looking at a static field, we may need to generate a
9578 class initialization for it. This can happen when the access
9579 looks like `field.ref', where `field' is a static field in an
9580 interface we implement. */
9581 if (!flag_emit_class_files
9582 && TREE_CODE (where_found) == VAR_DECL
9583 && FIELD_STATIC (where_found))
9585 build_static_field_ref (where_found);
9586 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9589 else
9590 field_ref = decl;
9592 if (field_decl)
9593 *field_decl = decl;
9594 if (field_type)
9595 *field_type = (QUAL_DECL_TYPE (decl) ?
9596 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9597 return field_ref;
9600 /* If NODE is an access to a static field, strip out the class
9601 initialization part and return the field decl, otherwise, return
9602 NODE. */
9604 tree
9605 extract_field_decl (tree node)
9607 if (TREE_CODE (node) == COMPOUND_EXPR)
9609 tree op1 = TREE_OPERAND (node, 1);
9610 if (TREE_CODE (op1) == COMPOUND_EXPR)
9612 tree call = TREE_OPERAND (op1, 0);
9613 if (TREE_CODE (call) == CALL_EXPR
9614 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9615 && (TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9616 == soft_initclass_node))
9617 return TREE_OPERAND (op1, 1);
9619 else if (JDECL_P (op1))
9620 return op1;
9622 return node;
9625 /* 6.5.5.2: Qualified Expression Names */
9627 static int
9628 resolve_qualified_expression_name (tree wfl, tree *found_decl,
9629 tree *where_found, tree *type_found)
9631 int from_type = 0; /* Field search initiated from a type */
9632 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9633 int previous_call_static = 0;
9634 int is_static;
9635 tree decl = NULL_TREE, type = NULL_TREE, q;
9636 /* For certain for of inner class instantiation */
9637 tree saved_current, saved_this;
9638 #define RESTORE_THIS_AND_CURRENT_CLASS \
9639 { current_class = saved_current; current_this = saved_this;}
9641 *type_found = *where_found = NULL_TREE;
9643 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9645 tree qual_wfl = QUAL_WFL (q);
9646 tree ret_decl; /* for EH checking */
9647 #ifdef USE_MAPPED_LOCATION
9648 source_location location; /* for EH checking */
9649 #else
9650 int location; /* for EH checking */
9651 #endif
9653 /* 15.10.1 Field Access Using a Primary */
9654 switch (TREE_CODE (qual_wfl))
9656 case CALL_EXPR:
9657 case NEW_CLASS_EXPR:
9658 /* If the access to the function call is a non static field,
9659 build the code to access it. */
9660 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9662 decl = maybe_access_field (decl, *where_found,
9663 DECL_CONTEXT (decl));
9664 if (decl == error_mark_node)
9665 return 1;
9668 /* And code for the function call */
9669 if (complete_function_arguments (qual_wfl))
9670 return 1;
9672 /* We might have to setup a new current class and a new this
9673 for the search of an inner class, relative to the type of
9674 a expression resolved as `decl'. The current values are
9675 saved and restored shortly after */
9676 saved_current = current_class;
9677 saved_this = current_this;
9678 if (decl
9679 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9680 || from_qualified_this))
9682 /* If we still have `from_qualified_this', we have the form
9683 <T>.this.f() and we need to build <T>.this */
9684 if (from_qualified_this)
9686 decl = build_access_to_thisn (current_class, type, 0);
9687 decl = java_complete_tree (decl);
9688 type = TREE_TYPE (TREE_TYPE (decl));
9690 current_class = type;
9691 current_this = decl;
9692 from_qualified_this = 0;
9695 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9696 CALL_USING_SUPER (qual_wfl) = 1;
9697 #ifdef USE_MAPPED_LOCATION
9698 location = (TREE_CODE (qual_wfl) == CALL_EXPR
9699 ? EXPR_LOCATION (TREE_OPERAND (qual_wfl, 0))
9700 : UNKNOWN_LOCATION);
9701 #else
9702 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9703 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9704 #endif
9705 *where_found = patch_method_invocation (qual_wfl, decl, type,
9706 from_super,
9707 &is_static, &ret_decl);
9708 from_super = 0;
9709 if (*where_found == error_mark_node)
9711 RESTORE_THIS_AND_CURRENT_CLASS;
9712 return 1;
9714 *type_found = type = QUAL_DECL_TYPE (*where_found);
9716 *where_found = force_evaluation_order (*where_found);
9718 /* If we're creating an inner class instance, check for that
9719 an enclosing instance is in scope */
9720 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9721 && INNER_ENCLOSING_SCOPE_CHECK (type))
9723 parse_error_context
9724 (qual_wfl, "No enclosing instance for inner class %qs is in scope%s",
9725 lang_printable_name (type, 0),
9726 (!current_this ? "" :
9727 "; an explicit one must be provided when creating this inner class"));
9728 RESTORE_THIS_AND_CURRENT_CLASS;
9729 return 1;
9732 /* In case we had to change then to resolve a inner class
9733 instantiation using a primary qualified by a `new' */
9734 RESTORE_THIS_AND_CURRENT_CLASS;
9736 #ifdef USE_MAPPED_LOCATION
9737 if (location != UNKNOWN_LOCATION)
9738 #else
9739 if (location)
9740 #endif
9742 tree arguments = NULL_TREE;
9743 if (TREE_CODE (qual_wfl) == CALL_EXPR
9744 && TREE_OPERAND (qual_wfl, 1) != NULL_TREE)
9745 arguments = TREE_VALUE (TREE_OPERAND (qual_wfl, 1));
9746 check_thrown_exceptions (location, ret_decl, arguments);
9749 /* If the previous call was static and this one is too,
9750 build a compound expression to hold the two (because in
9751 that case, previous function calls aren't transported as
9752 forcoming function's argument. */
9753 if (previous_call_static && is_static)
9755 /* We must set CAN_COMPLETE_NORMALLY for the first call
9756 since it is done nowhere else. */
9757 CAN_COMPLETE_NORMALLY (decl) = 1;
9758 decl = build2 (COMPOUND_EXPR, TREE_TYPE (*where_found),
9759 decl, *where_found);
9760 TREE_SIDE_EFFECTS (decl) = 1;
9762 else
9764 previous_call_static = is_static;
9765 decl = *where_found;
9767 from_type = 0;
9768 continue;
9770 case NEW_ARRAY_EXPR:
9771 case NEW_ANONYMOUS_ARRAY_EXPR:
9772 *where_found = decl = java_complete_tree (qual_wfl);
9773 if (decl == error_mark_node)
9774 return 1;
9775 *type_found = type = QUAL_DECL_TYPE (decl);
9776 continue;
9778 case CONVERT_EXPR:
9779 *where_found = decl = java_complete_tree (qual_wfl);
9780 if (decl == error_mark_node)
9781 return 1;
9782 *type_found = type = QUAL_DECL_TYPE (decl);
9783 from_cast = 1;
9784 continue;
9786 case CONDITIONAL_EXPR:
9787 case STRING_CST:
9788 case MODIFY_EXPR:
9789 *where_found = decl = java_complete_tree (qual_wfl);
9790 if (decl == error_mark_node)
9791 return 1;
9792 *type_found = type = QUAL_DECL_TYPE (decl);
9793 continue;
9795 case ARRAY_REF:
9796 /* If the access to the function call is a non static field,
9797 build the code to access it. */
9798 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9800 decl = maybe_access_field (decl, *where_found, type);
9801 if (decl == error_mark_node)
9802 return 1;
9804 /* And code for the array reference expression */
9805 decl = java_complete_tree (qual_wfl);
9806 if (decl == error_mark_node)
9807 return 1;
9808 type = QUAL_DECL_TYPE (decl);
9809 continue;
9811 case PLUS_EXPR:
9812 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9813 return 1;
9814 if ((type = patch_string (decl)))
9815 decl = type;
9816 *where_found = QUAL_RESOLUTION (q) = decl;
9817 *type_found = type = TREE_TYPE (decl);
9818 break;
9820 case CLASS_LITERAL:
9821 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9822 return 1;
9823 *where_found = QUAL_RESOLUTION (q) = decl;
9824 *type_found = type = TREE_TYPE (decl);
9825 break;
9827 default:
9828 /* Fix for -Wall Just go to the next statement. Don't
9829 continue */
9830 break;
9833 /* If we fall here, we weren't processing a (static) function call. */
9834 previous_call_static = 0;
9836 /* It can be the keyword THIS */
9837 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9838 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9840 if (!current_this)
9842 parse_error_context
9843 (wfl, "Keyword %<this%> used outside allowed context");
9844 return 1;
9846 if (ctxp->explicit_constructor_p
9847 && type == current_class)
9849 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
9850 return 1;
9852 /* We have to generate code for intermediate access */
9853 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9855 *where_found = decl = current_this;
9856 *type_found = type = QUAL_DECL_TYPE (decl);
9858 /* We're trying to access the this from somewhere else. Make sure
9859 it's allowed before doing so. */
9860 else
9862 if (!enclosing_context_p (type, current_class))
9864 char *p = xstrdup (lang_printable_name (type, 0));
9865 parse_error_context (qual_wfl, "Can't use variable %<%s.this%>: type %qs isn't an outer type of type %qs",
9866 p, p,
9867 lang_printable_name (current_class, 0));
9868 free (p);
9869 return 1;
9871 from_qualified_this = 1;
9872 /* If there's nothing else after that, we need to
9873 produce something now, otherwise, the section of the
9874 code that needs to produce <T>.this will generate
9875 what is necessary. */
9876 if (!TREE_CHAIN (q))
9878 decl = build_access_to_thisn (current_class, type, 0);
9879 *where_found = decl = java_complete_tree (decl);
9880 *type_found = type = TREE_TYPE (decl);
9884 from_type = 0;
9885 continue;
9888 /* 15.10.2 Accessing Superclass Members using SUPER */
9889 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9890 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9892 tree node;
9893 /* Check on the restricted use of SUPER */
9894 if (METHOD_STATIC (current_function_decl)
9895 || current_class == object_type_node)
9897 parse_error_context
9898 (wfl, "Keyword %<super%> used outside allowed context");
9899 return 1;
9901 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9902 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9903 CLASSTYPE_SUPER (current_class),
9904 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9905 *where_found = decl = java_complete_tree (node);
9906 if (decl == error_mark_node)
9907 return 1;
9908 *type_found = type = QUAL_DECL_TYPE (decl);
9909 from_super = from_type = 1;
9910 continue;
9913 /* 15.13.1: Can't search for field name in packages, so we
9914 assume a variable/class name was meant. */
9915 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9917 tree name;
9918 if ((decl = resolve_package (wfl, &q, &name)))
9920 tree list;
9921 *where_found = decl;
9923 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
9925 /* We want to be absolutely sure that the class is laid
9926 out. We're going to search something inside it. */
9927 *type_found = type = TREE_TYPE (decl);
9928 layout_class (type);
9929 from_type = 1;
9931 /* Fix them all the way down, if any are left. */
9932 if (q)
9934 list = TREE_CHAIN (q);
9935 while (list)
9937 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9938 list = TREE_CHAIN (list);
9942 else
9944 if (from_super || from_cast)
9945 parse_error_context
9946 ((from_cast ? qual_wfl : wfl),
9947 "No variable %qs defined in class %qs",
9948 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9949 lang_printable_name (type, 0));
9950 else
9951 parse_error_context
9952 (qual_wfl, "Undefined variable or class name: %qs",
9953 IDENTIFIER_POINTER (name));
9954 return 1;
9958 /* We have a type name. It's been already resolved when the
9959 expression was qualified. */
9960 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9962 decl = QUAL_RESOLUTION (q);
9964 /* Sneak preview. If next we see a `new', we're facing a
9965 qualification which resulted in a type being selected
9966 instead of a field. Report the error. */
9967 if(TREE_CHAIN (q)
9968 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9970 parse_error_context (qual_wfl, "Undefined variable %qs",
9971 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9972 return 1;
9975 check_pkg_class_access (DECL_NAME (decl), qual_wfl, true, NULL);
9977 check_deprecation (qual_wfl, decl);
9979 type = TREE_TYPE (decl);
9980 from_type = 1;
9982 /* We resolve an expression name */
9983 else
9985 tree field_decl = NULL_TREE;
9987 /* If there exists an early resolution, use it. That occurs
9988 only once and we know that there are more things to
9989 come. Don't do that when processing something after SUPER
9990 (we need more thing to be put in place below */
9991 if (!from_super && QUAL_RESOLUTION (q))
9993 decl = QUAL_RESOLUTION (q);
9994 if (!type)
9996 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9998 if (current_this)
9999 *where_found = current_this;
10000 else
10002 static_ref_err (qual_wfl, DECL_NAME (decl),
10003 current_class);
10004 return 1;
10006 if (outer_field_access_p (current_class, decl))
10007 decl = build_outer_field_access (qual_wfl, decl);
10009 else
10011 *where_found = TREE_TYPE (decl);
10012 if (TREE_CODE (*where_found) == POINTER_TYPE)
10013 *where_found = TREE_TYPE (*where_found);
10018 /* Report and error if we're using a numerical literal as a
10019 qualifier. It can only be an INTEGER_CST. */
10020 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
10022 parse_error_context
10023 (wfl, "Can't use type %qs as a qualifier",
10024 lang_printable_name (TREE_TYPE (qual_wfl), 0));
10025 return 1;
10028 /* We have to search for a field, knowing the type of its
10029 container. The flag FROM_TYPE indicates that we resolved
10030 the last member of the expression as a type name, which
10031 means that for the resolution of this field, we'll look
10032 for other errors than if it was resolved as a member of
10033 an other field. */
10034 else
10036 int is_static;
10037 tree field_decl_type; /* For layout */
10039 if (!from_type && !JREFERENCE_TYPE_P (type))
10041 parse_error_context
10042 (qual_wfl, "Attempt to reference field %qs in %<%s %s%>",
10043 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10044 lang_printable_name (type, 0),
10045 IDENTIFIER_POINTER (DECL_NAME (decl)));
10046 return 1;
10049 field_decl = lookup_field_wrapper (type,
10050 EXPR_WFL_NODE (qual_wfl));
10052 /* Maybe what we're trying to access to is an inner
10053 class, only if decl is a TYPE_DECL. */
10054 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
10056 tree ptr, inner_decl;
10058 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
10059 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
10060 if (inner_decl)
10062 check_inner_class_access (inner_decl, decl, qual_wfl);
10063 type = TREE_TYPE (inner_decl);
10064 decl = inner_decl;
10065 from_type = 1;
10066 continue;
10070 if (field_decl == NULL_TREE)
10072 parse_error_context
10073 (qual_wfl, "No variable %qs defined in type %qs",
10074 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
10075 GET_TYPE_NAME (type));
10076 return 1;
10078 if (field_decl == error_mark_node)
10079 return 1;
10081 /* Layout the type of field_decl, since we may need
10082 it. Don't do primitive types or loaded classes. The
10083 situation of non primitive arrays may not handled
10084 properly here. FIXME */
10085 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10086 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
10087 else
10088 field_decl_type = TREE_TYPE (field_decl);
10089 if (!JPRIMITIVE_TYPE_P (field_decl_type)
10090 && !CLASS_LOADED_P (field_decl_type)
10091 && !TYPE_ARRAY_P (field_decl_type))
10092 resolve_and_layout (field_decl_type, NULL_TREE);
10094 /* Check on accessibility here */
10095 if (not_accessible_p (current_class, field_decl,
10096 *type_found, from_super))
10097 return not_accessible_field_error (qual_wfl,field_decl);
10098 check_deprecation (qual_wfl, field_decl);
10100 /* There are things to check when fields are accessed
10101 from type. There are no restrictions on a static
10102 declaration of the field when it is accessed from an
10103 interface */
10104 is_static = FIELD_STATIC (field_decl);
10105 if (!from_super && from_type
10106 && !TYPE_INTERFACE_P (type)
10107 && !is_static
10108 && (current_function_decl
10109 && METHOD_STATIC (current_function_decl)))
10111 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10112 return 1;
10114 from_cast = from_super = 0;
10116 /* It's an access from a type but it isn't static, we
10117 make it relative to `this'. */
10118 if (!is_static && from_type)
10119 decl = current_this;
10121 /* If we need to generate something to get a proper
10122 handle on what this field is accessed from, do it
10123 now. */
10124 if (!is_static)
10126 decl = maybe_access_field (decl, *where_found, *type_found);
10127 if (decl == error_mark_node)
10128 return 1;
10131 /* We want to keep the location were found it, and the type
10132 we found. */
10133 *where_found = decl;
10134 *type_found = type;
10136 /* Generate the correct expression for field access from
10137 qualified this */
10138 if (from_qualified_this)
10140 field_decl = build_outer_field_access (qual_wfl, field_decl);
10141 from_qualified_this = 0;
10144 /* This is the decl found and eventually the next one to
10145 search from */
10146 decl = field_decl;
10148 from_type = 0;
10149 type = QUAL_DECL_TYPE (decl);
10151 /* Sneak preview. If decl is qualified by a `new', report
10152 the error here to be accurate on the peculiar construct */
10153 if (TREE_CHAIN (q)
10154 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10155 && !JREFERENCE_TYPE_P (type))
10157 parse_error_context (qual_wfl, "Attempt to reference field %<new%> in a %qs",
10158 lang_printable_name (type, 0));
10159 return 1;
10162 /* `q' might have changed due to a after package resolution
10163 re-qualification */
10164 if (!q)
10165 break;
10167 *found_decl = decl;
10168 return 0;
10171 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10172 can't be accessed from REFERENCE (a record type). If MEMBER
10173 features a protected access, we then use WHERE which, if non null,
10174 holds the type of MEMBER's access that is checked against
10175 6.6.2.1. This function should be used when decl is a field or a
10176 method. */
10178 static int
10179 not_accessible_p (tree reference, tree member, tree where, int from_super)
10181 int access_flag = get_access_flags_from_decl (member);
10182 bool is_static = false;
10184 if (TREE_CODE (member) == FIELD_DECL ||
10185 TREE_CODE (member) == VAR_DECL)
10186 is_static = FIELD_STATIC (member);
10187 else
10188 is_static = METHOD_STATIC (member);
10190 /* Access always granted for members declared public */
10191 if (access_flag & ACC_PUBLIC)
10192 return 0;
10194 /* Check access on protected members */
10195 if (access_flag & ACC_PROTECTED)
10197 /* Access granted if it occurs from within the package
10198 containing the class in which the protected member is
10199 declared */
10200 if (class_in_current_package (DECL_CONTEXT (member)))
10201 return 0;
10203 /* If accessed with the form `super.member', then access is granted */
10204 if (from_super)
10205 return 0;
10207 /* If WHERE is active, access was made through a qualifier. For
10208 non-static members, access is granted if the type of the qualifier
10209 is or is a sublass of the type the access is made from (6.6.2.1.) */
10210 if (where && !is_static)
10212 while (reference)
10214 if (inherits_from_p (where, reference))
10215 return 0;
10216 if (INNER_CLASS_TYPE_P (reference))
10217 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10218 else
10219 break;
10221 return 1;
10224 /* Otherwise, access is granted if occurring from within the class
10225 where member is declared, or a subclass of it. */
10226 while (reference)
10228 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10229 return 0;
10230 if (INNER_CLASS_TYPE_P (reference))
10231 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10232 else
10233 break;
10235 return 1;
10238 /* Check access on private members. Access is granted only if it
10239 occurs from within the class in which it is declared -- that does
10240 it for innerclasses too. */
10241 if (access_flag & ACC_PRIVATE)
10243 if (reference == DECL_CONTEXT (member) ||
10244 common_enclosing_context_p (DECL_CONTEXT (member), reference))
10245 return 0;
10246 return 1;
10249 /* Default access is permitted only when occurring from within the
10250 package in which the context (MEMBER) is declared. */
10251 return !class_in_current_package (DECL_CONTEXT (member));
10254 /* Test deprecated decl access. */
10255 static void
10256 check_deprecation (tree wfl, tree decl)
10258 const char *file;
10259 tree elt;
10261 if (! warn_deprecated)
10262 return;
10264 /* We want to look at the element type of arrays here, so we strip
10265 all surrounding array types. */
10266 if (TYPE_ARRAY_P (TREE_TYPE (decl)))
10268 elt = TREE_TYPE (decl);
10269 while (TYPE_ARRAY_P (elt))
10270 elt = TYPE_ARRAY_ELEMENT (elt);
10271 /* We'll end up with a pointer type, so we use TREE_TYPE to go
10272 to the record. */
10273 decl = TYPE_NAME (TREE_TYPE (elt));
10275 file = DECL_SOURCE_FILE (decl);
10277 /* Complain if the field is deprecated and the file it was defined
10278 in isn't compiled at the same time the file which contains its
10279 use is */
10280 if (DECL_DEPRECATED (decl)
10281 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10283 const char *the;
10284 switch (TREE_CODE (decl))
10286 case FUNCTION_DECL:
10287 the = "method";
10288 break;
10289 case FIELD_DECL:
10290 case VAR_DECL:
10291 the = "field";
10292 break;
10293 case TYPE_DECL:
10294 parse_warning_context (wfl, "The class %qs has been deprecated",
10295 IDENTIFIER_POINTER (DECL_NAME (decl)));
10296 return;
10297 default:
10298 abort ();
10300 /* Don't issue a message if the context as been deprecated as a
10301 whole. */
10302 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10303 parse_warning_context
10304 (wfl, "The %s %qs in class %qs has been deprecated",
10305 the, lang_printable_name (decl, 0),
10306 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10310 /* Returns 1 if class was declared in the current package, 0 otherwise */
10312 static GTY(()) tree cicp_cache;
10313 static int
10314 class_in_current_package (tree class)
10316 int qualified_flag;
10317 tree left;
10319 if (cicp_cache == class)
10320 return 1;
10322 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10324 /* If the current package is empty and the name of CLASS is
10325 qualified, class isn't in the current package. If there is a
10326 current package and the name of the CLASS is not qualified, class
10327 isn't in the current package */
10328 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10329 return 0;
10331 /* If there is not package and the name of CLASS isn't qualified,
10332 they belong to the same unnamed package */
10333 if (!ctxp->package && !qualified_flag)
10334 return 1;
10336 /* Compare the left part of the name of CLASS with the package name */
10337 split_qualified_name (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10338 if (ctxp->package == left)
10340 cicp_cache = class;
10341 return 1;
10343 return 0;
10346 /* This function may generate code to access DECL from WHERE. This is
10347 done only if certain conditions meet. */
10349 static tree
10350 maybe_access_field (tree decl, tree where, tree type)
10352 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10353 && !FIELD_STATIC (decl))
10354 decl = build_field_ref (where ? where : current_this,
10355 (type ? type : DECL_CONTEXT (decl)),
10356 DECL_NAME (decl));
10357 return decl;
10360 /* Build a method invocation, by patching PATCH. If non NULL
10361 and according to the situation, PRIMARY and WHERE may be
10362 used. IS_STATIC is set to 1 if the invoked function is static. */
10364 static tree
10365 patch_method_invocation (tree patch, tree primary, tree where, int from_super,
10366 int *is_static, tree *ret_decl)
10368 tree wfl = TREE_OPERAND (patch, 0);
10369 tree args = TREE_OPERAND (patch, 1);
10370 tree name = EXPR_WFL_NODE (wfl);
10371 tree list;
10372 int is_static_flag = 0;
10373 int is_super_init = 0;
10374 tree this_arg = NULL_TREE;
10375 int is_array_clone_call = 0;
10377 /* Should be overridden if everything goes well. Otherwise, if
10378 something fails, it should keep this value. It stop the
10379 evaluation of a bogus assignment. See java_complete_tree,
10380 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10381 evaluating an assignment */
10382 TREE_TYPE (patch) = error_mark_node;
10384 /* Since lookup functions are messing with line numbers, save the
10385 context now. */
10386 java_parser_context_save_global ();
10388 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10390 /* Resolution of qualified name, excluding constructors */
10391 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10393 tree identifier, identifier_wfl, type, resolved;
10394 /* Extract the last IDENTIFIER of the qualified
10395 expression. This is a wfl and we will use it's location
10396 data during error report. */
10397 identifier_wfl = cut_identifier_in_qualified (wfl);
10398 identifier = EXPR_WFL_NODE (identifier_wfl);
10400 /* Given the context, IDENTIFIER is syntactically qualified
10401 as a MethodName. We need to qualify what's before */
10402 qualify_ambiguous_name (wfl);
10403 resolved = resolve_field_access (wfl, NULL, NULL);
10405 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10406 && FIELD_FINAL (resolved)
10407 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10408 && !flag_emit_class_files)
10409 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10411 if (resolved == error_mark_node)
10412 PATCH_METHOD_RETURN_ERROR ();
10414 type = GET_SKIP_TYPE (resolved);
10415 resolve_and_layout (type, NULL_TREE);
10417 if (JPRIMITIVE_TYPE_P (type))
10419 parse_error_context
10420 (identifier_wfl,
10421 "Can't invoke a method on primitive type %qs",
10422 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10423 PATCH_METHOD_RETURN_ERROR ();
10426 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10427 args = nreverse (args);
10429 /* We're resolving a call from a type */
10430 if (TREE_CODE (resolved) == TYPE_DECL)
10432 if (CLASS_INTERFACE (resolved))
10434 parse_error_context
10435 (identifier_wfl,
10436 "Can't make static reference to method %qs in interface %qs",
10437 IDENTIFIER_POINTER (identifier),
10438 IDENTIFIER_POINTER (name));
10439 PATCH_METHOD_RETURN_ERROR ();
10441 if (list && !METHOD_STATIC (list))
10443 char *fct_name = xstrdup (lang_printable_name (list, 2));
10444 parse_error_context
10445 (identifier_wfl,
10446 "Can't make static reference to method %<%s %s%> in class %qs",
10447 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10448 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10449 free (fct_name);
10450 PATCH_METHOD_RETURN_ERROR ();
10453 else
10454 this_arg = primary = resolved;
10456 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10457 is_array_clone_call = 1;
10459 /* IDENTIFIER_WFL will be used to report any problem further */
10460 wfl = identifier_wfl;
10462 /* Resolution of simple names, names generated after a primary: or
10463 constructors */
10464 else
10466 tree class_to_search = NULL_TREE;
10467 int lc; /* Looking for Constructor */
10469 /* We search constructor in their target class */
10470 if (CALL_CONSTRUCTOR_P (patch))
10472 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10473 class_to_search = EXPR_WFL_NODE (wfl);
10474 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10475 this_identifier_node)
10476 class_to_search = NULL_TREE;
10477 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10478 super_identifier_node)
10480 is_super_init = 1;
10481 if (CLASSTYPE_SUPER (current_class))
10482 class_to_search =
10483 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10484 else
10486 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10487 PATCH_METHOD_RETURN_ERROR ();
10491 /* Class to search is NULL if we're searching the current one */
10492 if (class_to_search)
10494 class_to_search = resolve_and_layout (class_to_search, wfl);
10496 if (!class_to_search)
10498 parse_error_context
10499 (wfl, "Class %qs not found in type declaration",
10500 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10501 PATCH_METHOD_RETURN_ERROR ();
10504 /* Can't instantiate an abstract class, but we can
10505 invoke it's constructor. It's use within the `new'
10506 context is denied here. */
10507 if (CLASS_ABSTRACT (class_to_search)
10508 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10510 parse_error_context
10511 (wfl, "Class %qs is an abstract class. It can't be instantiated",
10512 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10513 PATCH_METHOD_RETURN_ERROR ();
10516 class_to_search = TREE_TYPE (class_to_search);
10518 else
10519 class_to_search = current_class;
10520 lc = 1;
10522 /* This is a regular search in the local class, unless an
10523 alternate class is specified. */
10524 else
10526 if (where != NULL_TREE)
10527 class_to_search = where;
10528 else if (QUALIFIED_P (name))
10529 class_to_search = current_class;
10530 else
10532 class_to_search = current_class;
10534 for (;;)
10536 if (has_method (class_to_search, name))
10537 break;
10538 if (! INNER_CLASS_TYPE_P (class_to_search))
10540 parse_error_context (wfl,
10541 "No method named %qs in scope",
10542 IDENTIFIER_POINTER (name));
10543 PATCH_METHOD_RETURN_ERROR ();
10545 class_to_search
10546 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10549 lc = 0;
10552 /* NAME is a simple identifier or comes from a primary. Search
10553 in the class whose declaration contain the method being
10554 invoked. */
10555 resolve_and_layout (class_to_search, NULL_TREE);
10557 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10558 /* Don't continue if no method were found, as the next statement
10559 can't be executed then. */
10560 if (!list)
10561 PATCH_METHOD_RETURN_ERROR ();
10563 if (TYPE_ARRAY_P (class_to_search)
10564 && DECL_NAME (list) == get_identifier ("clone"))
10565 is_array_clone_call = 1;
10567 /* Check for static reference if non static methods */
10568 if (check_for_static_method_reference (wfl, patch, list,
10569 class_to_search, primary))
10570 PATCH_METHOD_RETURN_ERROR ();
10572 /* Check for inner classes creation from illegal contexts */
10573 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10574 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10575 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10576 && !DECL_INIT_P (current_function_decl))
10578 parse_error_context
10579 (wfl, "No enclosing instance for inner class %qs is in scope%s",
10580 lang_printable_name (class_to_search, 0),
10581 (!current_this ? "" :
10582 "; an explicit one must be provided when creating this inner class"));
10583 PATCH_METHOD_RETURN_ERROR ();
10586 /* Non static methods are called with the current object extra
10587 argument. If patch a `new TYPE()', the argument is the value
10588 returned by the object allocator. If method is resolved as a
10589 primary, use the primary otherwise use the current THIS. */
10590 args = nreverse (args);
10591 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10593 this_arg = primary ? primary : current_this;
10595 /* If we're using an access method, things are different.
10596 There are two family of cases:
10598 1) We're not generating bytecodes:
10600 - LIST is non static. It's invocation is transformed from
10601 x(a1,...,an) into this$<n>.x(a1,....an).
10602 - LIST is static. It's invocation is transformed from
10603 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10605 2) We're generating bytecodes:
10607 - LIST is non static. It's invocation is transformed from
10608 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10609 - LIST is static. It's invocation is transformed from
10610 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10612 Of course, this$<n> can be arbitrarily complex, ranging from
10613 this$0 (the immediate outer context) to
10614 access$0(access$0(...(this$0))).
10616 maybe_use_access_method returns a nonzero value if the
10617 this_arg has to be moved into the (then generated) stub
10618 argument list. In the meantime, the selected function
10619 might have be replaced by a generated stub. */
10620 if (!primary &&
10621 maybe_use_access_method (is_super_init, &list, &this_arg))
10623 args = tree_cons (NULL_TREE, this_arg, args);
10624 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10629 /* Merge point of all resolution schemes. If we have nothing, this
10630 is an error, already signaled */
10631 if (!list)
10632 PATCH_METHOD_RETURN_ERROR ();
10634 /* Check accessibility, position the is_static flag, build and
10635 return the call */
10636 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10637 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10638 NULL_TREE), from_super)
10639 /* Calls to clone() on array types are permitted as a special-case. */
10640 && !is_array_clone_call)
10642 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10643 const char *const access =
10644 accessibility_string (get_access_flags_from_decl (list));
10645 const char *const klass =
10646 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10647 const char *const refklass =
10648 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10649 const char *const what = (DECL_CONSTRUCTOR_P (list)
10650 ? "constructor" : "method");
10651 parse_error_context (wfl,
10652 "Can't access %s %s %<%s.%s%> from %qs",
10653 access, what, klass, fct_name, refklass);
10654 PATCH_METHOD_RETURN_ERROR ();
10657 /* Deprecation check: check whether the method being invoked or the
10658 instance-being-created's type are deprecated. */
10659 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10660 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10661 check_deprecation (wfl, list);
10663 /* If invoking a innerclass constructor, there are hidden parameters
10664 to pass */
10665 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10666 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10668 /* And make sure we add the accessed local variables to be saved
10669 in field aliases. */
10670 args = build_alias_initializer_parameter_list
10671 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10673 /* Secretly pass the current_this/primary as a second argument */
10674 if (primary || current_this)
10676 tree extra_arg;
10677 tree this_type = (current_this ?
10678 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10679 /* Method's (list) enclosing context */
10680 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10681 /* If we have a primary, use it. */
10682 if (primary)
10683 extra_arg = primary;
10684 /* The current `this' is an inner class but isn't a direct
10685 enclosing context for the inner class we're trying to
10686 create. Build an access to the proper enclosing context
10687 and use it. */
10688 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10689 && this_type != TREE_TYPE (mec))
10692 extra_arg = build_access_to_thisn (current_class,
10693 TREE_TYPE (mec), 0);
10694 extra_arg = java_complete_tree (extra_arg);
10696 /* Otherwise, just use the current `this' as an enclosing
10697 context. */
10698 else
10699 extra_arg = current_this;
10700 args = tree_cons (NULL_TREE, extra_arg, args);
10702 else
10703 args = tree_cons (NULL_TREE, integer_zero_node, args);
10706 /* This handles the situation where a constructor invocation needs
10707 to have an enclosing context passed as a second parameter (the
10708 constructor is one of an inner class). */
10709 if ((is_super_init ||
10710 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10711 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10713 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10714 tree extra_arg =
10715 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10716 extra_arg = java_complete_tree (extra_arg);
10717 args = tree_cons (NULL_TREE, extra_arg, args);
10720 is_static_flag = METHOD_STATIC (list);
10721 if (! is_static_flag && this_arg != NULL_TREE)
10722 args = tree_cons (NULL_TREE, this_arg, args);
10724 /* In the context of an explicit constructor invocation, we can't
10725 invoke any method relying on `this'. Exceptions are: we're
10726 invoking a static function, primary exists and is not the current
10727 this, we're creating a new object. */
10728 if (ctxp->explicit_constructor_p
10729 && !is_static_flag
10730 && (!primary || primary == current_this)
10731 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10733 parse_error_context (wfl, "Can't reference %<this%> before the superclass constructor has been called");
10734 PATCH_METHOD_RETURN_ERROR ();
10736 java_parser_context_restore_global ();
10737 if (is_static)
10738 *is_static = is_static_flag;
10739 /* Sometimes, we want the decl of the selected method. Such as for
10740 EH checking */
10741 if (ret_decl)
10742 *ret_decl = list;
10743 patch = patch_invoke (patch, list, args);
10745 /* Now is a good time to insert the call to finit$ */
10746 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10748 tree finit_parms, finit_call;
10750 /* Prepare to pass hidden parameters to finit$, if any. */
10751 finit_parms = build_alias_initializer_parameter_list
10752 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10754 finit_call =
10755 build_method_invocation (build_wfl_node (finit_identifier_node),
10756 finit_parms);
10758 /* Generate the code used to initialize fields declared with an
10759 initialization statement and build a compound statement along
10760 with the super constructor invocation. */
10761 CAN_COMPLETE_NORMALLY (patch) = 1;
10762 patch = build2 (COMPOUND_EXPR, void_type_node, patch,
10763 java_complete_tree (finit_call));
10765 return patch;
10768 /* Check that we're not trying to do a static reference to a method in
10769 non static method. Return 1 if it's the case, 0 otherwise. */
10771 static int
10772 check_for_static_method_reference (tree wfl, tree node, tree method,
10773 tree where, tree primary)
10775 if (METHOD_STATIC (current_function_decl)
10776 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10778 char *fct_name = xstrdup (lang_printable_name (method, 0));
10779 parse_error_context
10780 (wfl, "Can't make static reference to method %<%s %s%> in class %qs",
10781 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10782 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10783 free (fct_name);
10784 return 1;
10786 return 0;
10789 /* Fix the invocation of *MDECL if necessary in the case of a
10790 invocation from an inner class. *THIS_ARG might be modified
10791 appropriately and an alternative access to *MDECL might be
10792 returned. */
10794 static int
10795 maybe_use_access_method (int is_super_init, tree *mdecl, tree *this_arg)
10797 tree ctx;
10798 tree md = *mdecl, ta = *this_arg;
10799 int to_return = 0;
10800 int non_static_context = !METHOD_STATIC (md);
10802 if (is_super_init
10803 || DECL_CONTEXT (md) == current_class
10804 || !PURE_INNER_CLASS_TYPE_P (current_class)
10805 || DECL_FINIT_P (md)
10806 || DECL_INSTINIT_P (md))
10807 return 0;
10809 /* If we're calling a method found in an enclosing class, generate
10810 what it takes to retrieve the right this. Don't do that if we're
10811 invoking a static method. Note that if MD's type is unrelated to
10812 CURRENT_CLASS, then the current this can be used. */
10814 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10816 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10817 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10819 ta = build_current_thisn (current_class);
10820 ta = build_wfl_node (ta);
10822 else
10824 tree type = ctx;
10825 while (type)
10827 maybe_build_thisn_access_method (type);
10828 if (inherits_from_p (type, DECL_CONTEXT (md)))
10830 ta = build_access_to_thisn (ctx, type, 0);
10831 break;
10833 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10834 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10837 ta = java_complete_tree (ta);
10840 /* We might have to use an access method to get to MD. We can
10841 break the method access rule as far as we're not generating
10842 bytecode */
10843 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10845 md = build_outer_method_access_method (md);
10846 to_return = 1;
10849 *mdecl = md;
10850 *this_arg = ta;
10852 /* Returning a nonzero value indicates we were doing a non static
10853 method invocation that is now a static invocation. It will have
10854 callee displace `this' to insert it in the regular argument
10855 list. */
10856 return (non_static_context && to_return);
10859 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10860 mode. */
10862 static tree
10863 patch_invoke (tree patch, tree method, tree args)
10865 tree dtable, func;
10866 tree original_call, t, ta;
10867 tree check = NULL_TREE;
10869 /* Last step for args: convert build-in types. If we're dealing with
10870 a new TYPE() type call, the first argument to the constructor
10871 isn't found in the incoming argument list, but delivered by
10872 `new' */
10873 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10874 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10875 t = TREE_CHAIN (t);
10876 for (ta = args; t != end_params_node && ta;
10877 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10878 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10879 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10880 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10882 /* Resolve unresolved returned type issues */
10883 t = TREE_TYPE (TREE_TYPE (method));
10884 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10885 resolve_and_layout (TREE_TYPE (t), NULL);
10887 if (flag_emit_class_files)
10888 func = method;
10889 else
10891 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10893 case INVOKE_VIRTUAL:
10894 dtable = invoke_build_dtable (0, args);
10895 func = build_invokevirtual (dtable, method);
10896 break;
10898 case INVOKE_NONVIRTUAL:
10899 /* If the object for the method call is null, we throw an
10900 exception. We don't do this if the object is the current
10901 method's `this'. In other cases we just rely on an
10902 optimization pass to eliminate redundant checks. */
10903 if (TREE_VALUE (args) != current_this)
10905 /* We use a save_expr here to make sure we only evaluate
10906 the new `self' expression once. */
10907 tree save_arg = save_expr (TREE_VALUE (args));
10908 TREE_VALUE (args) = save_arg;
10909 check = java_check_reference (save_arg, 1);
10911 /* Fall through. */
10913 case INVOKE_SUPER:
10914 case INVOKE_STATIC:
10916 tree signature = build_java_signature (TREE_TYPE (method));
10917 func = build_known_method_ref (method, TREE_TYPE (method),
10918 DECL_CONTEXT (method),
10919 signature, args);
10921 break;
10923 case INVOKE_INTERFACE:
10924 dtable = invoke_build_dtable (1, args);
10925 func = build_invokeinterface (dtable, method);
10926 break;
10928 default:
10929 abort ();
10932 /* Ensure self_type is initialized, (invokestatic). FIXME */
10933 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10936 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10937 TREE_OPERAND (patch, 0) = func;
10938 TREE_OPERAND (patch, 1) = args;
10939 patch = check_for_builtin (method, patch);
10940 original_call = patch;
10942 /* We're processing a `new TYPE ()' form. New is called and its
10943 returned value is the first argument to the constructor. We build
10944 a COMPOUND_EXPR and use saved expression so that the overall NEW
10945 expression value is a pointer to a newly created and initialized
10946 class. */
10947 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10949 tree class = DECL_CONTEXT (method);
10950 tree c1, saved_new, new;
10951 tree alloc_node;
10953 if (flag_emit_class_files)
10955 TREE_TYPE (patch) = build_pointer_type (class);
10956 return patch;
10958 if (!TYPE_SIZE (class))
10959 safe_layout_class (class);
10960 alloc_node =
10961 (class_has_finalize_method (class) ? alloc_object_node
10962 : alloc_no_finalizer_node);
10963 new = build3 (CALL_EXPR, promote_type (class),
10964 build_address_of (alloc_node),
10965 build_tree_list (NULL_TREE, build_class_ref (class)),
10966 NULL_TREE);
10967 saved_new = save_expr (new);
10968 c1 = build_tree_list (NULL_TREE, saved_new);
10969 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10970 TREE_OPERAND (original_call, 1) = c1;
10971 TREE_SET_CODE (original_call, CALL_EXPR);
10972 patch = build2 (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10975 /* If CHECK is set, then we are building a check to see if the object
10976 is NULL. */
10977 if (check != NULL_TREE)
10979 /* We have to call force_evaluation_order now because creating a
10980 COMPOUND_EXPR wraps the arg list in a way that makes it
10981 unrecognizable by force_evaluation_order later. Yuk. */
10982 patch = build2 (COMPOUND_EXPR, TREE_TYPE (patch), check,
10983 force_evaluation_order (patch));
10984 TREE_SIDE_EFFECTS (patch) = 1;
10987 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10988 put it as the first expression of a COMPOUND_EXPR. The second
10989 expression being an empty statement to be later patched if
10990 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10991 the VALUE is the compound) in a hashtable and return a
10992 COMPOUND_EXPR built so that the result of the evaluation of the
10993 original PATCH node is returned. */
10994 if (STATIC_CLASS_INIT_OPT_P ()
10995 && current_function_decl && METHOD_STATIC (method))
10997 tree list;
10998 tree fndecl = current_function_decl;
10999 /* We have to call force_evaluation_order now because creating a
11000 COMPOUND_EXPR wraps the arg list in a way that makes it
11001 unrecognizable by force_evaluation_order later. Yuk. */
11002 tree save = force_evaluation_order (patch);
11003 tree type = TREE_TYPE (patch);
11005 patch = build2 (COMPOUND_EXPR, type, save, build_java_empty_stmt ());
11006 list = tree_cons (method, patch,
11007 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
11009 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
11011 patch = build2 (COMPOUND_EXPR, type, patch, save);
11014 return patch;
11017 static int
11018 invocation_mode (tree method, int super)
11020 int access = get_access_flags_from_decl (method);
11022 if (super)
11023 return INVOKE_SUPER;
11025 if (access & ACC_STATIC)
11026 return INVOKE_STATIC;
11028 /* We have to look for a constructor before we handle nonvirtual
11029 calls; otherwise the constructor will look nonvirtual. */
11030 if (DECL_CONSTRUCTOR_P (method))
11031 return INVOKE_STATIC;
11033 if (access & ACC_PRIVATE)
11034 return INVOKE_NONVIRTUAL;
11036 /* Binary compatibility: just because it's final today, that doesn't
11037 mean it'll be final tomorrow. */
11038 if (! flag_indirect_dispatch
11039 || DECL_CONTEXT (method) == object_type_node)
11041 if (access & ACC_FINAL)
11042 return INVOKE_NONVIRTUAL;
11044 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
11045 return INVOKE_NONVIRTUAL;
11048 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
11049 return INVOKE_INTERFACE;
11051 return INVOKE_VIRTUAL;
11054 /* Retrieve a refined list of matching methods. It covers the step
11055 15.11.2 (Compile-Time Step 2) */
11057 static tree
11058 lookup_method_invoke (int lc, tree cl, tree class, tree name, tree arg_list)
11060 tree atl = end_params_node; /* Arg Type List */
11061 tree method, signature, list, node;
11062 const char *candidates; /* Used for error report */
11063 char *dup;
11065 /* Fix the arguments */
11066 for (node = arg_list; node; node = TREE_CHAIN (node))
11068 tree current_arg = TREE_TYPE (TREE_VALUE (node));
11069 /* Non primitive type may have to be resolved */
11070 if (!JPRIMITIVE_TYPE_P (current_arg))
11071 resolve_and_layout (current_arg, NULL_TREE);
11072 /* And promoted */
11073 if (TREE_CODE (current_arg) == RECORD_TYPE)
11074 current_arg = promote_type (current_arg);
11075 atl = tree_cons (NULL_TREE, current_arg, atl);
11078 /* Presto. If we're dealing with an anonymous class and a
11079 constructor call, generate the right constructor now, since we
11080 know the arguments' types. */
11082 if (lc && ANONYMOUS_CLASS_P (class))
11084 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
11085 /* The anonymous class may have already been laid out, so make sure
11086 the new constructor is laid out here. */
11087 layout_class_method (class, CLASSTYPE_SUPER (class), mdecl, NULL_TREE);
11090 /* Find all candidates and then refine the list, searching for the
11091 most specific method. */
11092 list = find_applicable_accessible_methods_list (lc, class, name, atl);
11093 list = find_most_specific_methods_list (list);
11094 if (list && !TREE_CHAIN (list))
11095 return TREE_VALUE (list);
11097 /* Issue an error. List candidates if any. Candidates are listed
11098 only if accessible (non accessible methods may end-up here for
11099 the sake of a better error report). */
11100 candidates = NULL;
11101 if (list)
11103 tree current;
11104 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11105 for (current = list; current; current = TREE_CHAIN (current))
11107 tree cm = TREE_VALUE (current);
11108 char string [4096];
11109 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11110 continue;
11111 sprintf
11112 (string, " '%s' in '%s'%s",
11113 get_printable_method_name (cm),
11114 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11115 (TREE_CHAIN (current) ? "\n" : ""));
11116 obstack_grow (&temporary_obstack, string, strlen (string));
11118 obstack_1grow (&temporary_obstack, '\0');
11119 candidates = obstack_finish (&temporary_obstack);
11121 /* Issue the error message */
11122 method = make_node (FUNCTION_TYPE);
11123 TYPE_ARG_TYPES (method) = atl;
11124 signature = build_java_argument_signature (method);
11125 dup = xstrdup (lang_printable_name (class, 0));
11126 parse_error_context (cl, "Can't find %s %<%s(%s)%> in type %qs%s",
11127 (lc ? "constructor" : "method"),
11128 (lc ? dup : IDENTIFIER_POINTER (name)),
11129 IDENTIFIER_POINTER (signature), dup,
11130 (candidates ? candidates : ""));
11131 free (dup);
11132 return NULL_TREE;
11135 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11136 when we're looking for a constructor. */
11138 static tree
11139 find_applicable_accessible_methods_list (int lc, tree class, tree name,
11140 tree arglist)
11142 static htab_t searched_classes;
11143 static int search_not_done = 0;
11144 tree list = NULL_TREE, all_list = NULL_TREE;
11145 tree base_binfo;
11146 int i;
11148 /* Check the hash table to determine if this class has been searched
11149 already. */
11150 if (searched_classes)
11152 if (htab_find (searched_classes, class) != NULL)
11153 return NULL;
11155 else
11157 searched_classes = htab_create (10, htab_hash_pointer,
11158 htab_eq_pointer, NULL);
11161 search_not_done++;
11162 *htab_find_slot (searched_classes, class, INSERT) = class;
11164 if (!CLASS_LOADED_P (class))
11166 load_class (class, 1);
11167 safe_layout_class (class);
11170 /* Search interfaces */
11171 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11172 && CLASS_INTERFACE (TYPE_NAME (class)))
11174 search_applicable_methods_list (lc, TYPE_METHODS (class),
11175 name, arglist, &list, &all_list);
11176 for (i = 1; BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11178 tree t = BINFO_TYPE (base_binfo);
11179 tree rlist;
11181 rlist = find_applicable_accessible_methods_list (lc, t, name,
11182 arglist);
11183 list = chainon (rlist, list);
11186 /* Search classes */
11187 else
11189 search_applicable_methods_list (lc, TYPE_METHODS (class),
11190 name, arglist, &list, &all_list);
11192 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11193 that we only search in class. Note that we should have found
11194 something at this point. */
11195 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11197 lc = 1;
11198 if (!list)
11199 abort ();
11202 /* We must search all interfaces of this class */
11203 if (!lc)
11205 for (i = 1;
11206 BINFO_BASE_ITERATE (TYPE_BINFO (class), i, base_binfo); i++)
11208 tree t = BINFO_TYPE (base_binfo);
11209 if (t != object_type_node)
11211 tree rlist
11212 = find_applicable_accessible_methods_list (lc, t,
11213 name, arglist);
11214 list = chainon (rlist, list);
11219 /* Search superclass */
11220 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11222 tree rlist;
11223 class = CLASSTYPE_SUPER (class);
11224 rlist = find_applicable_accessible_methods_list (lc, class,
11225 name, arglist);
11226 list = chainon (rlist, list);
11230 search_not_done--;
11232 /* We're done. Reset the searched classes list and finally search
11233 java.lang.Object if it wasn't searched already. */
11234 if (!search_not_done)
11236 if (!lc
11237 && TYPE_METHODS (object_type_node)
11238 && htab_find (searched_classes, object_type_node) == NULL)
11240 search_applicable_methods_list (lc,
11241 TYPE_METHODS (object_type_node),
11242 name, arglist, &list, &all_list);
11244 htab_delete (searched_classes);
11245 searched_classes = NULL;
11248 /* Either return the list obtained or all selected (but
11249 inaccessible) methods for better error report. */
11250 return (!list ? all_list : list);
11253 /* Effectively search for the appropriate method in method */
11255 static void
11256 search_applicable_methods_list (int lc, tree method, tree name, tree arglist,
11257 tree *list, tree *all_list)
11259 for (; method; method = TREE_CHAIN (method))
11261 /* When dealing with constructor, stop here, otherwise search
11262 other classes */
11263 if (lc && !DECL_CONSTRUCTOR_P (method))
11264 continue;
11265 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11266 || (DECL_NAME (method) != name)))
11267 continue;
11269 if (argument_types_convertible (method, arglist))
11271 /* Retain accessible methods only */
11272 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11273 method, NULL_TREE, 0))
11274 *list = tree_cons (NULL_TREE, method, *list);
11275 else
11276 /* Also retain all selected method here */
11277 *all_list = tree_cons (NULL_TREE, method, *list);
11282 /* 15.11.2.2 Choose the Most Specific Method */
11284 static tree
11285 find_most_specific_methods_list (tree list)
11287 int max = 0;
11288 int abstract, candidates;
11289 tree current, new_list = NULL_TREE;
11290 for (current = list; current; current = TREE_CHAIN (current))
11292 tree method;
11293 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11295 for (method = list; method; method = TREE_CHAIN (method))
11297 tree method_v, current_v;
11298 /* Don't test a method against itself */
11299 if (method == current)
11300 continue;
11302 method_v = TREE_VALUE (method);
11303 current_v = TREE_VALUE (current);
11305 /* Compare arguments and location where methods where declared */
11306 if (argument_types_convertible (method_v, current_v))
11308 if (valid_method_invocation_conversion_p
11309 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11310 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11311 && enclosing_context_p (DECL_CONTEXT (method_v),
11312 DECL_CONTEXT (current_v))))
11314 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11315 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11316 max = (v > max ? v : max);
11322 /* Review the list and select the maximally specific methods */
11323 for (current = list, abstract = -1, candidates = -1;
11324 current; current = TREE_CHAIN (current))
11325 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11327 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11328 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11329 candidates++;
11332 /* If we have several and they're all abstract, just pick the
11333 closest one. */
11334 if (candidates > 0 && candidates == abstract)
11336 /* FIXME: merge the throws clauses. There is no convenient way
11337 to do this in gcj right now, since ideally we'd like to
11338 introduce a new METHOD_DECL here, but that is really not
11339 possible. */
11340 new_list = nreverse (new_list);
11341 TREE_CHAIN (new_list) = NULL_TREE;
11342 return new_list;
11345 /* We have several (we couldn't find a most specific), all but one
11346 are abstract, we pick the only non abstract one. */
11347 if (candidates > 0 && (candidates == abstract+1))
11349 for (current = new_list; current; current = TREE_CHAIN (current))
11350 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11352 TREE_CHAIN (current) = NULL_TREE;
11353 new_list = current;
11357 /* If we can't find one, lower expectations and try to gather multiple
11358 maximally specific methods */
11359 while (!new_list && max)
11361 while (--max > 0)
11363 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11364 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11368 return new_list;
11371 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11372 converted by method invocation conversion (5.3) to the type of the
11373 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11374 to change less often than M1. */
11376 static GTY(()) tree m2_arg_value;
11377 static GTY(()) tree m2_arg_cache;
11379 static int
11380 argument_types_convertible (tree m1, tree m2_or_arglist)
11382 tree m1_arg, m2_arg;
11384 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11386 if (m2_arg_value == m2_or_arglist)
11387 m2_arg = m2_arg_cache;
11388 else
11390 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11391 argument types */
11392 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11394 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11395 if (!METHOD_STATIC (m2_or_arglist))
11396 m2_arg = TREE_CHAIN (m2_arg);
11398 else
11399 m2_arg = m2_or_arglist;
11401 m2_arg_value = m2_or_arglist;
11402 m2_arg_cache = m2_arg;
11405 while (m1_arg != end_params_node && m2_arg != end_params_node)
11407 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11408 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11409 TREE_VALUE (m2_arg)))
11410 break;
11411 m1_arg = TREE_CHAIN (m1_arg);
11412 m2_arg = TREE_CHAIN (m2_arg);
11414 return m1_arg == end_params_node && m2_arg == end_params_node;
11417 /* Qualification routines */
11419 /* Given a name x.y.z, look up x locally. If it's found, save the
11420 decl. If it's not found, mark the name as RESOLVE_PACKAGE_NAME_P,
11421 so that we later try and load the appropriate classes. */
11422 static void
11423 qualify_ambiguous_name (tree id)
11425 tree name, decl;
11427 /* We inspect the first item of the qualification list. As a sanity
11428 check, make sure that it is an identfier node. */
11429 tree qual = EXPR_WFL_QUALIFICATION (id);
11430 tree qual_wfl = QUAL_WFL (qual);
11432 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11433 return;
11435 name = EXPR_WFL_NODE (qual_wfl);
11437 /* If we don't have an identifier, or we have a 'this' or 'super',
11438 then field access processing is all we need : there is nothing
11439 for us to do. */
11440 if (!name || TREE_CODE (name) != IDENTIFIER_NODE ||
11441 name == this_identifier_node ||
11442 name == super_identifier_node)
11443 return;
11445 /* If name appears within the scope of a local variable declaration
11446 or parameter declaration, or is a field within an enclosing
11447 class, then it is an expression name. Save the decl and let
11448 resolve_field_access do it's work. */
11449 if ((decl = IDENTIFIER_LOCAL_VALUE (name)) ||
11450 (decl = lookup_field_wrapper (current_class, name)))
11452 QUAL_RESOLUTION (qual) = decl;
11453 return;
11456 /* If name is a known class name (either declared or imported), mark
11457 us as a type name. */
11458 if ((decl = resolve_and_layout (name, NULL_TREE)))
11460 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11461 QUAL_RESOLUTION (qual) = decl;
11464 /* Check here that NAME isn't declared by more than one
11465 type-import-on-demand declaration of the compilation unit
11466 containing NAME. FIXME */
11468 /* We couldn't find a declaration for the name. Assume for now that
11469 we have a qualified class name that needs to be loaded from an
11470 external class file. */
11471 else
11472 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11474 /* Propagate the qualification across other components of the
11475 qualified name */
11476 for (qual = TREE_CHAIN (qual); qual;
11477 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11479 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11480 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11483 /* Store the global qualification for the ambiguous part of ID back
11484 into ID fields */
11485 if (RESOLVE_TYPE_NAME_P (qual_wfl))
11486 RESOLVE_TYPE_NAME_P (id) = 1;
11487 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11488 RESOLVE_PACKAGE_NAME_P (id) = 1;
11491 /* Patch tree nodes in a function body. When a BLOCK is found, push
11492 local variable decls if present.
11493 Same as java_complete_lhs, but does resolve static finals to values. */
11495 static tree
11496 java_complete_tree (tree node)
11498 node = java_complete_lhs (node);
11499 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11500 && DECL_INITIAL (node) != NULL_TREE)
11502 tree value = fold_constant_for_init (node, node);
11503 if (value != NULL_TREE)
11504 return value;
11506 return node;
11509 static tree
11510 java_stabilize_reference (tree node)
11512 if (TREE_CODE (node) == COMPOUND_EXPR)
11514 tree op0 = TREE_OPERAND (node, 0);
11515 tree op1 = TREE_OPERAND (node, 1);
11516 TREE_OPERAND (node, 0) = save_expr (op0);
11517 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11518 return node;
11520 return stabilize_reference (node);
11523 /* Patch tree nodes in a function body. When a BLOCK is found, push
11524 local variable decls if present.
11525 Same as java_complete_tree, but does not resolve static finals to values. */
11527 static tree
11528 java_complete_lhs (tree node)
11530 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11531 int flag;
11533 /* CONVERT_EXPR always has its type set, even though it needs to be
11534 worked out. */
11535 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11536 return node;
11538 /* The switch block implements cases processing container nodes
11539 first. Contained nodes are always written back. Leaves come
11540 next and return a value. */
11541 switch (TREE_CODE (node))
11543 case BLOCK:
11545 /* 1- Block section.
11546 Set the local values on decl names so we can identify them
11547 faster when they're referenced. At that stage, identifiers
11548 are legal so we don't check for declaration errors. */
11549 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11551 DECL_CONTEXT (cn) = current_function_decl;
11552 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11554 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11555 CAN_COMPLETE_NORMALLY (node) = 1;
11556 else
11558 tree stmt = BLOCK_EXPR_BODY (node);
11559 tree *ptr;
11560 int error_seen = 0;
11561 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11563 /* Re-order from (((A; B); C); ...; Z) to
11564 (A; (B; (C ; (...; Z)))).
11565 This makes it easier to scan the statements left-to-right
11566 without using recursion (which might overflow the stack
11567 if the block has many statements. */
11568 for (;;)
11570 tree left = TREE_OPERAND (stmt, 0);
11571 if (TREE_CODE (left) != COMPOUND_EXPR)
11572 break;
11573 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11574 TREE_OPERAND (left, 1) = stmt;
11575 stmt = left;
11577 BLOCK_EXPR_BODY (node) = stmt;
11580 /* Now do the actual complete, without deep recursion for
11581 long blocks. */
11582 ptr = &BLOCK_EXPR_BODY (node);
11583 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11584 && !IS_EMPTY_STMT (TREE_OPERAND (*ptr, 1)))
11586 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11587 tree *next = &TREE_OPERAND (*ptr, 1);
11588 TREE_OPERAND (*ptr, 0) = cur;
11589 if (IS_EMPTY_STMT (cur))
11591 /* Optimization; makes it easier to detect empty bodies.
11592 Most useful for <clinit> with all-constant initializer. */
11593 *ptr = *next;
11594 continue;
11596 if (TREE_CODE (cur) == ERROR_MARK)
11597 error_seen++;
11598 else if (! CAN_COMPLETE_NORMALLY (cur))
11600 wfl_op2 = *next;
11601 for (;;)
11603 if (TREE_CODE (wfl_op2) == BLOCK)
11604 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11605 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11606 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11607 else
11608 break;
11610 if (TREE_CODE (wfl_op2) != CASE_EXPR
11611 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11612 unreachable_stmt_error (*ptr);
11614 if (TREE_TYPE (*ptr) == NULL_TREE)
11615 TREE_TYPE (*ptr) = void_type_node;
11616 ptr = next;
11618 *ptr = java_complete_tree (*ptr);
11620 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11621 return error_mark_node;
11622 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11624 /* Turn local bindings to null */
11625 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11626 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11628 TREE_TYPE (node) = void_type_node;
11629 break;
11631 /* 2- They are expressions but ultimately deal with statements */
11633 case THROW_EXPR:
11634 wfl_op1 = TREE_OPERAND (node, 0);
11635 COMPLETE_CHECK_OP_0 (node);
11636 /* 14.19 A throw statement cannot complete normally. */
11637 CAN_COMPLETE_NORMALLY (node) = 0;
11638 return patch_throw_statement (node, wfl_op1);
11640 case SYNCHRONIZED_EXPR:
11641 wfl_op1 = TREE_OPERAND (node, 0);
11642 return patch_synchronized_statement (node, wfl_op1);
11644 case TRY_EXPR:
11645 return patch_try_statement (node);
11647 case TRY_FINALLY_EXPR:
11648 COMPLETE_CHECK_OP_0 (node);
11649 COMPLETE_CHECK_OP_1 (node);
11650 if (IS_EMPTY_STMT (TREE_OPERAND (node, 0)))
11651 /* Reduce try/finally nodes with an empty try block. */
11652 return TREE_OPERAND (node, 1);
11653 if (IS_EMPTY_STMT (TREE_OPERAND (node, 1)))
11654 /* Likewise for an empty finally block. */
11655 return TREE_OPERAND (node, 0);
11656 CAN_COMPLETE_NORMALLY (node)
11657 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11658 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11659 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11660 return node;
11662 case LABELED_BLOCK_EXPR:
11663 PUSH_LABELED_BLOCK (node);
11664 if (LABELED_BLOCK_BODY (node))
11665 COMPLETE_CHECK_OP_1 (node);
11666 TREE_TYPE (node) = void_type_node;
11667 POP_LABELED_BLOCK ();
11669 if (IS_EMPTY_STMT (LABELED_BLOCK_BODY (node)))
11671 LABELED_BLOCK_BODY (node) = NULL_TREE;
11672 CAN_COMPLETE_NORMALLY (node) = 1;
11674 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11675 CAN_COMPLETE_NORMALLY (node) = 1;
11676 return node;
11678 case EXIT_BLOCK_EXPR:
11679 return patch_bc_statement (node);
11681 case CASE_EXPR:
11682 cn = java_complete_tree (TREE_OPERAND (node, 0));
11683 if (cn == error_mark_node)
11684 return cn;
11686 /* First, the case expression must be constant. Values of final
11687 fields are accepted. */
11688 cn = fold (cn);
11689 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11690 && JDECL_P (TREE_OPERAND (cn, 1))
11691 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11692 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11694 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11695 TREE_OPERAND (cn, 1));
11697 /* Accept final locals too. */
11698 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn)
11699 && DECL_INITIAL (cn))
11700 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11702 if (!TREE_CONSTANT (cn))
11704 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11705 parse_error_context (node, "Constant expression required");
11706 return error_mark_node;
11709 nn = ctxp->current_loop;
11711 /* It must be assignable to the type of the switch expression. */
11712 if (!try_builtin_assignconv (NULL_TREE,
11713 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11715 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11716 parse_error_context
11717 (wfl_operator,
11718 "Incompatible type for case. Can't convert %qs to %<int%>",
11719 lang_printable_name (TREE_TYPE (cn), 0));
11720 return error_mark_node;
11723 cn = fold (convert (int_type_node, cn));
11724 TREE_CONSTANT_OVERFLOW (cn) = 0;
11725 CAN_COMPLETE_NORMALLY (cn) = 1;
11727 /* Save the label on a list so that we can later check for
11728 duplicates. */
11729 case_label_list = tree_cons (node, cn, case_label_list);
11731 /* Multiple instance of a case label bearing the same value is
11732 checked later. The case expression is all right so far. */
11733 if (TREE_CODE (cn) == VAR_DECL)
11734 cn = DECL_INITIAL (cn);
11735 TREE_OPERAND (node, 0) = cn;
11736 TREE_TYPE (node) = void_type_node;
11737 CAN_COMPLETE_NORMALLY (node) = 1;
11738 TREE_SIDE_EFFECTS (node) = 1;
11739 break;
11741 case DEFAULT_EXPR:
11742 nn = ctxp->current_loop;
11743 /* Only one default label is allowed per switch statement */
11744 if (SWITCH_HAS_DEFAULT (nn))
11746 #ifdef USE_MAPPED_LOCATION
11747 SET_EXPR_LOCATION (wfl_operator, EXPR_LOCATION (node));
11748 #else
11749 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11750 #endif
11751 parse_error_context (wfl_operator,
11752 "Duplicate case label: %<default%>");
11753 return error_mark_node;
11755 else
11756 SWITCH_HAS_DEFAULT (nn) = 1;
11757 TREE_TYPE (node) = void_type_node;
11758 TREE_SIDE_EFFECTS (node) = 1;
11759 CAN_COMPLETE_NORMALLY (node) = 1;
11760 break;
11762 case SWITCH_EXPR:
11763 case LOOP_EXPR:
11764 PUSH_LOOP (node);
11765 /* Check whether the loop was enclosed in a labeled
11766 statement. If not, create one, insert the loop in it and
11767 return the node */
11768 nn = patch_loop_statement (node);
11770 /* Anyways, walk the body of the loop */
11771 if (TREE_CODE (node) == LOOP_EXPR)
11772 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11773 /* Switch statement: walk the switch expression and the cases */
11774 else
11775 node = patch_switch_statement (node);
11777 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11778 nn = error_mark_node;
11779 else
11781 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11782 /* If we returned something different, that's because we
11783 inserted a label. Pop the label too. */
11784 if (nn != node)
11786 if (CAN_COMPLETE_NORMALLY (node))
11787 CAN_COMPLETE_NORMALLY (nn) = 1;
11788 POP_LABELED_BLOCK ();
11791 POP_LOOP ();
11792 return nn;
11794 case EXIT_EXPR:
11795 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11796 return patch_exit_expr (node);
11798 case COND_EXPR:
11799 /* Condition */
11800 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11801 if (TREE_OPERAND (node, 0) == error_mark_node)
11802 return error_mark_node;
11803 /* then-else branches */
11804 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11805 if (TREE_OPERAND (node, 1) == error_mark_node)
11806 return error_mark_node;
11808 /* This is a special case due to build_assertion(). When
11809 assertions are disabled we build a COND_EXPR in which
11810 Operand 1 is the body of the assertion. If that happens to
11811 be a string concatenation we'll need to patch it here. */
11812 tree patched = patch_string (TREE_OPERAND (node, 1));
11813 if (patched)
11814 TREE_OPERAND (node, 1) = patched;
11816 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11817 if (TREE_OPERAND (node, 2) == error_mark_node)
11818 return error_mark_node;
11819 return patch_if_else_statement (node);
11820 break;
11822 case CONDITIONAL_EXPR:
11823 /* Condition */
11824 wfl_op1 = TREE_OPERAND (node, 0);
11825 COMPLETE_CHECK_OP_0 (node);
11826 wfl_op2 = TREE_OPERAND (node, 1);
11827 COMPLETE_CHECK_OP_1 (node);
11828 wfl_op3 = TREE_OPERAND (node, 2);
11829 COMPLETE_CHECK_OP_2 (node);
11830 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11832 /* 3- Expression section */
11833 case COMPOUND_EXPR:
11834 wfl_op2 = TREE_OPERAND (node, 1);
11835 TREE_OPERAND (node, 0) = nn =
11836 java_complete_tree (TREE_OPERAND (node, 0));
11837 if (IS_EMPTY_STMT (wfl_op2))
11838 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11839 else
11841 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11843 /* An unreachable condition in a do-while statement
11844 is *not* (technically) an unreachable statement. */
11845 nn = wfl_op2;
11846 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11847 nn = EXPR_WFL_NODE (nn);
11848 /* NN can be NULL_TREE exactly when UPDATE is, in
11849 finish_for_loop. */
11850 if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11852 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11853 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11855 /* Perhaps this warning should have an
11856 associated flag. The code being compiled is
11857 pedantically correct, but useless. */
11858 parse_warning_context (wfl_operator,
11859 "Unreachable statement");
11861 else
11862 parse_error_context (wfl_operator,
11863 "Unreachable statement");
11866 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11867 if (TREE_OPERAND (node, 1) == error_mark_node)
11868 return error_mark_node;
11869 /* Even though we might allow the case where the first
11870 operand doesn't return normally, we still should compute
11871 CAN_COMPLETE_NORMALLY correctly. */
11872 CAN_COMPLETE_NORMALLY (node)
11873 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11874 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11876 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11877 break;
11879 case RETURN_EXPR:
11880 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11881 return patch_return (node);
11883 case EXPR_WITH_FILE_LOCATION:
11884 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11885 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11887 node = resolve_expression_name (node, NULL);
11888 if (node == error_mark_node)
11889 return node;
11890 CAN_COMPLETE_NORMALLY (node) = 1;
11892 else
11894 tree body;
11895 location_t save_location = input_location;
11896 #ifdef USE_MAPPED_LOCATION
11897 input_location = EXPR_LOCATION (node);
11898 if (input_location == UNKNOWN_LOCATION)
11899 input_location = save_location;
11900 #else
11901 input_line = EXPR_WFL_LINENO (node);
11902 #endif
11903 body = java_complete_tree (EXPR_WFL_NODE (node));
11904 input_location = save_location;
11905 EXPR_WFL_NODE (node) = body;
11906 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11907 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11908 if (IS_EMPTY_STMT (body) || TREE_CONSTANT (body))
11910 /* Makes it easier to constant fold, detect empty bodies. */
11911 return body;
11913 if (body == error_mark_node)
11915 /* Its important for the evaluation of assignment that
11916 this mark on the TREE_TYPE is propagated. */
11917 TREE_TYPE (node) = error_mark_node;
11918 return error_mark_node;
11920 else
11921 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11924 break;
11926 case NEW_ARRAY_EXPR:
11927 /* Patch all the dimensions */
11928 flag = 0;
11929 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11931 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11932 tree dim = convert (int_type_node,
11933 java_complete_tree (TREE_VALUE (cn)));
11934 if (dim == error_mark_node)
11936 flag = 1;
11937 continue;
11939 else
11941 TREE_VALUE (cn) = dim;
11942 /* Setup the location of the current dimension, for
11943 later error report. */
11944 #ifdef USE_MAPPED_LOCATION
11945 TREE_PURPOSE (cn) = expr_add_location (NULL_TREE, location, 0);
11946 #else
11947 TREE_PURPOSE (cn) =
11948 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11949 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11950 #endif
11953 /* They complete the array creation expression, if no errors
11954 were found. */
11955 CAN_COMPLETE_NORMALLY (node) = 1;
11956 return (flag ? error_mark_node
11957 : force_evaluation_order (patch_newarray (node)));
11959 case NEW_ANONYMOUS_ARRAY_EXPR:
11960 /* Create the array type if necessary. */
11961 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11963 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11964 if (!(type = resolve_type_during_patch (type)))
11965 return error_mark_node;
11966 type = build_array_from_name (type, NULL_TREE,
11967 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11968 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11970 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11971 ANONYMOUS_ARRAY_INITIALIZER (node));
11972 if (node == error_mark_node)
11973 return error_mark_node;
11974 CAN_COMPLETE_NORMALLY (node) = 1;
11975 return node;
11977 case NEW_CLASS_EXPR:
11978 case CALL_EXPR:
11979 /* Complete function's argument(s) first */
11980 if (complete_function_arguments (node))
11981 return error_mark_node;
11982 else
11984 tree decl, wfl = TREE_OPERAND (node, 0);
11985 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11986 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11987 super_identifier_node);
11988 tree arguments;
11989 #ifdef USE_MAPPED_LOCATION
11990 source_location location = EXPR_LOCATION (node);
11991 #else
11992 int location = EXPR_WFL_LINECOL (node);
11993 #endif
11995 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11996 from_super, 0, &decl);
11997 if (node == error_mark_node)
11998 return error_mark_node;
12000 if (TREE_CODE (node) == CALL_EXPR
12001 && TREE_OPERAND (node, 1) != NULL_TREE)
12002 arguments = TREE_VALUE (TREE_OPERAND (node, 1));
12003 else
12004 arguments = NULL_TREE;
12005 check_thrown_exceptions (location, decl, arguments);
12006 /* If we call this(...), register signature and positions */
12007 if (in_this)
12008 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12009 tree_cons (wfl, decl,
12010 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12011 CAN_COMPLETE_NORMALLY (node) = 1;
12012 return force_evaluation_order (node);
12015 case MODIFY_EXPR:
12016 /* Save potential wfls */
12017 wfl_op1 = TREE_OPERAND (node, 0);
12018 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12020 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12021 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12022 && DECL_INITIAL (nn) != NULL_TREE)
12024 tree value;
12026 value = fold_constant_for_init (nn, nn);
12028 /* When we have a primitype type, or a string and we're not
12029 emitting a class file, we actually don't want to generate
12030 anything for the assignment. */
12031 if (value != NULL_TREE &&
12032 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12033 (TREE_TYPE (value) == string_ptr_type_node &&
12034 ! flag_emit_class_files)))
12036 /* Prepare node for patch_assignment */
12037 TREE_OPERAND (node, 1) = value;
12038 /* Call patch assignment to verify the assignment */
12039 if (patch_assignment (node, wfl_op1) == error_mark_node)
12040 return error_mark_node;
12041 /* Set DECL_INITIAL properly (a conversion might have
12042 been decided by patch_assignment) and return the
12043 empty statement. */
12044 else
12046 tree patched = patch_string (TREE_OPERAND (node, 1));
12047 if (patched)
12048 DECL_INITIAL (nn) = patched;
12049 else
12050 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12051 DECL_FIELD_FINAL_IUD (nn) = 1;
12052 return build_java_empty_stmt ();
12055 if (! flag_emit_class_files)
12056 DECL_INITIAL (nn) = NULL_TREE;
12058 wfl_op2 = TREE_OPERAND (node, 1);
12060 if (TREE_OPERAND (node, 0) == error_mark_node)
12061 return error_mark_node;
12063 flag = COMPOUND_ASSIGN_P (wfl_op2);
12064 if (flag)
12066 /* This might break when accessing outer field from inner
12067 class. TESTME, FIXME */
12068 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12070 /* Hand stabilize the lhs on both places */
12071 TREE_OPERAND (node, 0) = lvalue;
12072 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12073 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12075 /* 15.25.2.a: Left hand is not an array access. FIXME */
12076 /* Now complete the RHS. We write it back later on. */
12077 nn = java_complete_tree (TREE_OPERAND (node, 1));
12079 if ((cn = patch_string (nn)))
12080 nn = cn;
12082 /* The last part of the rewrite for E1 op= E2 is to have
12083 E1 = (T)(E1 op E2), with T being the type of E1. */
12084 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12085 TREE_TYPE (lvalue), nn));
12087 /* If the assignment is compound and has reference type,
12088 then ensure the LHS has type String and nothing else. */
12089 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12090 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12091 parse_error_context (wfl_op2,
12092 "Incompatible type for %<+=%>. Can't convert %qs to %<java.lang.String%>",
12093 lang_printable_name (TREE_TYPE (lvalue), 0));
12095 /* 15.25.2.b: Left hand is an array access. FIXME */
12098 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12099 function to complete this RHS. Note that a NEW_ARRAY_INIT
12100 might have been already fully expanded if created as a result
12101 of processing an anonymous array initializer. We avoid doing
12102 the operation twice by testing whether the node already bears
12103 a type. */
12104 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12105 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12106 TREE_OPERAND (node, 1));
12107 /* Otherwise we simply complete the RHS */
12108 else
12109 nn = java_complete_tree (TREE_OPERAND (node, 1));
12111 if (nn == error_mark_node)
12112 return error_mark_node;
12114 /* Write back the RHS as we evaluated it. */
12115 TREE_OPERAND (node, 1) = nn;
12117 /* In case we're handling = with a String as a RHS, we need to
12118 produce a String out of the RHS (it might still be a
12119 STRING_CST or a StringBuffer at this stage */
12120 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12121 TREE_OPERAND (node, 1) = nn;
12123 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12124 TREE_OPERAND (node, 1))))
12126 /* We return error_mark_node if outer_field_access_fix
12127 detects we write into a final. */
12128 if (nn == error_mark_node)
12129 return error_mark_node;
12130 node = nn;
12132 else
12134 node = patch_assignment (node, wfl_op1);
12135 if (node == error_mark_node)
12136 return error_mark_node;
12137 /* Reorganize the tree if necessary. */
12138 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12139 || JSTRING_P (TREE_TYPE (node))))
12140 node = java_refold (node);
12143 /* Seek to set DECL_INITIAL to a proper value, since it might have
12144 undergone a conversion in patch_assignment. We do that only when
12145 it's necessary to have DECL_INITIAL properly set. */
12146 nn = TREE_OPERAND (node, 0);
12147 if (TREE_CODE (nn) == VAR_DECL
12148 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12149 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12150 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12151 || TREE_TYPE (nn) == string_ptr_type_node))
12152 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12154 CAN_COMPLETE_NORMALLY (node) = 1;
12155 return node;
12157 case MULT_EXPR:
12158 case PLUS_EXPR:
12159 case MINUS_EXPR:
12160 case LSHIFT_EXPR:
12161 case RSHIFT_EXPR:
12162 case URSHIFT_EXPR:
12163 case BIT_AND_EXPR:
12164 case BIT_XOR_EXPR:
12165 case BIT_IOR_EXPR:
12166 case TRUNC_MOD_EXPR:
12167 case TRUNC_DIV_EXPR:
12168 case RDIV_EXPR:
12169 case TRUTH_ANDIF_EXPR:
12170 case TRUTH_ORIF_EXPR:
12171 case EQ_EXPR:
12172 case NE_EXPR:
12173 case GT_EXPR:
12174 case GE_EXPR:
12175 case LT_EXPR:
12176 case LE_EXPR:
12177 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12178 knows how to handle those cases. */
12179 wfl_op1 = TREE_OPERAND (node, 0);
12180 wfl_op2 = TREE_OPERAND (node, 1);
12182 CAN_COMPLETE_NORMALLY (node) = 1;
12183 /* Don't complete string nodes if dealing with the PLUS operand. */
12184 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12186 nn = java_complete_tree (wfl_op1);
12187 if (nn == error_mark_node)
12188 return error_mark_node;
12190 TREE_OPERAND (node, 0) = nn;
12192 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12194 nn = java_complete_tree (wfl_op2);
12195 if (nn == error_mark_node)
12196 return error_mark_node;
12198 TREE_OPERAND (node, 1) = nn;
12200 return patch_binop (node, wfl_op1, wfl_op2);
12202 case INSTANCEOF_EXPR:
12203 wfl_op1 = TREE_OPERAND (node, 0);
12204 COMPLETE_CHECK_OP_0 (node);
12205 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12207 case UNARY_PLUS_EXPR:
12208 case NEGATE_EXPR:
12209 case TRUTH_NOT_EXPR:
12210 case BIT_NOT_EXPR:
12211 case PREDECREMENT_EXPR:
12212 case PREINCREMENT_EXPR:
12213 case POSTDECREMENT_EXPR:
12214 case POSTINCREMENT_EXPR:
12215 case CONVERT_EXPR:
12216 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12217 how to handle those cases. */
12218 wfl_op1 = TREE_OPERAND (node, 0);
12219 CAN_COMPLETE_NORMALLY (node) = 1;
12220 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12221 if (TREE_OPERAND (node, 0) == error_mark_node)
12222 return error_mark_node;
12223 node = patch_unaryop (node, wfl_op1);
12224 CAN_COMPLETE_NORMALLY (node) = 1;
12225 break;
12227 case ARRAY_REF:
12228 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12229 how to handle those cases. */
12230 wfl_op1 = TREE_OPERAND (node, 0);
12231 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12232 if (TREE_OPERAND (node, 0) == error_mark_node)
12233 return error_mark_node;
12234 if (!flag_emit_class_files)
12235 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12236 /* The same applies to wfl_op2 */
12237 wfl_op2 = TREE_OPERAND (node, 1);
12238 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12239 if (TREE_OPERAND (node, 1) == error_mark_node)
12240 return error_mark_node;
12241 if (!flag_emit_class_files)
12242 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12243 return patch_array_ref (node);
12245 case RECORD_TYPE:
12246 return node;;
12248 case COMPONENT_REF:
12249 /* The first step in the re-write of qualified name handling. FIXME.
12250 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12251 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12252 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12254 tree name = TREE_OPERAND (node, 1);
12255 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12256 if (field == NULL_TREE)
12258 error ("missing static field %qs", IDENTIFIER_POINTER (name));
12259 return error_mark_node;
12261 if (! FIELD_STATIC (field))
12263 error ("not a static field %qs", IDENTIFIER_POINTER (name));
12264 return error_mark_node;
12266 return field;
12268 else
12269 abort ();
12270 break;
12272 case THIS_EXPR:
12273 /* Can't use THIS in a static environment */
12274 if (!current_this)
12276 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12277 parse_error_context (wfl_operator,
12278 "Keyword %<this%> used outside allowed context");
12279 TREE_TYPE (node) = error_mark_node;
12280 return error_mark_node;
12282 if (ctxp->explicit_constructor_p)
12284 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12285 parse_error_context
12286 (wfl_operator, "Can't reference %<this%> or %<super%> before the superclass constructor has been called");
12287 TREE_TYPE (node) = error_mark_node;
12288 return error_mark_node;
12290 return current_this;
12292 case CLASS_LITERAL:
12293 CAN_COMPLETE_NORMALLY (node) = 1;
12294 node = patch_incomplete_class_ref (node);
12295 if (node == error_mark_node)
12296 return error_mark_node;
12297 break;
12299 default:
12300 CAN_COMPLETE_NORMALLY (node) = 1;
12301 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12302 and it's time to turn it into the appropriate String object */
12303 if ((nn = patch_string (node)))
12304 node = nn;
12305 else
12306 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12308 return node;
12311 /* Complete function call's argument. Return a nonzero value is an
12312 error was found. */
12314 static int
12315 complete_function_arguments (tree node)
12317 int flag = 0;
12318 tree cn;
12320 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12321 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12323 tree wfl = TREE_VALUE (cn), parm, temp;
12324 parm = java_complete_tree (wfl);
12326 if (parm == error_mark_node)
12328 flag = 1;
12329 continue;
12331 /* If we have a string literal that we haven't transformed yet or a
12332 crafted string buffer, as a result of the use of the String
12333 `+' operator. Build `parm.toString()' and expand it. */
12334 if ((temp = patch_string (parm)))
12335 parm = temp;
12337 TREE_VALUE (cn) = parm;
12339 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12340 return flag;
12343 /* Sometimes (for loops and variable initialized during their
12344 declaration), we want to wrap a statement around a WFL and turn it
12345 debugable. */
12347 static tree
12348 build_debugable_stmt (int location, tree stmt)
12350 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12352 #ifdef USE_MAPPED_LOCATION
12353 stmt = expr_add_location (stmt, location, 1);
12354 #else
12355 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12356 EXPR_WFL_LINECOL (stmt) = location;
12357 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12358 #endif
12360 return stmt;
12363 static tree
12364 build_expr_block (tree body, tree decls)
12366 tree node = make_node (BLOCK);
12367 BLOCK_EXPR_DECLS (node) = decls;
12368 BLOCK_EXPR_BODY (node) = body;
12369 if (body)
12370 TREE_TYPE (node) = TREE_TYPE (body);
12371 TREE_SIDE_EFFECTS (node) = 1;
12372 return node;
12375 /* Create a new function block and link it appropriately to current
12376 function block chain */
12378 static tree
12379 enter_block (void)
12381 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12383 /* Link block B supercontext to the previous block. The current
12384 function DECL is used as supercontext when enter_a_block is called
12385 for the first time for a given function. The current function body
12386 (DECL_FUNCTION_BODY) is set to be block B. */
12388 tree fndecl = current_function_decl;
12390 if (!fndecl) {
12391 BLOCK_SUPERCONTEXT (b) = current_static_block;
12392 current_static_block = b;
12395 else if (!DECL_FUNCTION_BODY (fndecl))
12397 BLOCK_SUPERCONTEXT (b) = fndecl;
12398 DECL_FUNCTION_BODY (fndecl) = b;
12400 else
12402 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12403 DECL_FUNCTION_BODY (fndecl) = b;
12405 return b;
12408 /* Exit a block by changing the current function body
12409 (DECL_FUNCTION_BODY) to the current block super context, only if
12410 the block being exited isn't the method's top level one. */
12412 static tree
12413 exit_block (void)
12415 tree b;
12416 if (current_function_decl)
12418 b = DECL_FUNCTION_BODY (current_function_decl);
12419 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12420 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12422 else
12424 b = current_static_block;
12426 if (BLOCK_SUPERCONTEXT (b))
12427 current_static_block = BLOCK_SUPERCONTEXT (b);
12429 return b;
12432 /* Lookup for NAME in the nested function's blocks, all the way up to
12433 the current toplevel one. It complies with Java's local variable
12434 scoping rules. */
12436 static tree
12437 lookup_name_in_blocks (tree name)
12439 tree b = GET_CURRENT_BLOCK (current_function_decl);
12441 while (b != current_function_decl)
12443 tree current;
12445 /* Paranoid sanity check. To be removed */
12446 if (TREE_CODE (b) != BLOCK)
12447 abort ();
12449 for (current = BLOCK_EXPR_DECLS (b); current;
12450 current = TREE_CHAIN (current))
12451 if (DECL_NAME (current) == name)
12452 return current;
12453 b = BLOCK_SUPERCONTEXT (b);
12455 return NULL_TREE;
12458 static void
12459 maybe_absorb_scoping_blocks (void)
12461 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12463 tree b = exit_block ();
12464 java_method_add_stmt (current_function_decl, b);
12465 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", input_line));
12470 /* This section of the source is reserved to build_* functions that
12471 are building incomplete tree nodes and the patch_* functions that
12472 are completing them. */
12474 /* Wrap a non WFL node around a WFL. */
12476 static tree
12477 build_wfl_wrap (tree node, int location)
12479 tree wfl, node_to_insert = node;
12481 /* We want to process THIS . xxx symbolically, to keep it consistent
12482 with the way we're processing SUPER. A THIS from a primary as a
12483 different form than a SUPER. Turn THIS into something symbolic */
12484 if (TREE_CODE (node) == THIS_EXPR)
12485 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12486 else
12487 #ifdef USE_MAPPED_LOCATION
12488 wfl = build_unknown_wfl (NULL_TREE);
12490 SET_EXPR_LOCATION (wfl, location);
12491 #else
12492 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12494 EXPR_WFL_LINECOL (wfl) = location;
12495 #endif
12496 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12497 return wfl;
12500 /* Build a super() constructor invocation. Returns an empty statement if
12501 we're currently dealing with the class java.lang.Object. */
12503 static tree
12504 build_super_invocation (tree mdecl)
12506 if (DECL_CONTEXT (mdecl) == object_type_node)
12507 return build_java_empty_stmt ();
12508 else
12510 tree super_wfl = build_wfl_node (super_identifier_node);
12511 tree a = NULL_TREE, t;
12513 /* This is called after parsing is done, so the parser context
12514 won't be accurate. Set location info from current_class decl. */
12515 tree class_wfl = lookup_cl (TYPE_NAME (current_class));
12516 EXPR_WFL_LINECOL (super_wfl) = EXPR_WFL_LINECOL (class_wfl);
12518 /* If we're dealing with an anonymous class, pass the arguments
12519 of the crafted constructor along. */
12520 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12522 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12523 for (; t != end_params_node; t = TREE_CHAIN (t))
12524 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12526 return build_method_invocation (super_wfl, a);
12530 /* Build a SUPER/THIS qualified method invocation. */
12532 static tree
12533 build_this_super_qualified_invocation (int use_this, tree name, tree args,
12534 int lloc, int rloc)
12536 tree invok;
12537 tree wfl =
12538 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12539 EXPR_WFL_LINECOL (wfl) = lloc;
12540 invok = build_method_invocation (name, args);
12541 return make_qualified_primary (wfl, invok, rloc);
12544 /* Build an incomplete CALL_EXPR node. */
12546 static tree
12547 build_method_invocation (tree name, tree args)
12549 tree call = build3 (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12550 TREE_SIDE_EFFECTS (call) = 1;
12551 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12552 return call;
12555 /* Build an incomplete new xxx(...) node. */
12557 static tree
12558 build_new_invocation (tree name, tree args)
12560 tree call = build3 (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12561 TREE_SIDE_EFFECTS (call) = 1;
12562 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12563 return call;
12566 /* Build an incomplete assignment expression. */
12568 static tree
12569 build_assignment (int op, int op_location, tree lhs, tree rhs)
12571 tree assignment;
12572 /* Build the corresponding binop if we deal with a Compound
12573 Assignment operator. Mark the binop sub-tree as part of a
12574 Compound Assignment expression */
12575 if (op != ASSIGN_TK)
12577 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12578 COMPOUND_ASSIGN_P (rhs) = 1;
12580 assignment = build2 (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12581 TREE_SIDE_EFFECTS (assignment) = 1;
12582 EXPR_WFL_LINECOL (assignment) = op_location;
12583 return assignment;
12586 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12587 the buffer. This is used only for string conversion. */
12588 static char *
12589 string_convert_int_cst (tree node)
12591 /* Long.MIN_VALUE is -9223372036854775808, 20 characters. */
12592 static char buffer[21];
12594 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12595 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12596 char *p = buffer + sizeof (buffer);
12597 int neg = 0;
12599 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12600 << (HOST_BITS_PER_WIDE_INT - 1));
12602 *--p = '\0';
12604 /* If negative, note the fact and negate the value. */
12605 if ((hi & hibit))
12607 lo = ~lo;
12608 hi = ~hi;
12609 if (++lo == 0)
12610 ++hi;
12611 neg = 1;
12614 /* Divide by 10 until there are no bits left. */
12617 unsigned HOST_WIDE_INT acc = 0;
12618 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12619 unsigned int i;
12621 /* Use long division to compute the result and the remainder. */
12622 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12624 /* Shift a bit into accumulator. */
12625 acc <<= 1;
12626 if ((hi & hibit))
12627 acc |= 1;
12629 /* Shift the value. */
12630 hi <<= 1;
12631 if ((lo & hibit))
12632 hi |= 1;
12633 lo <<= 1;
12635 /* Shift the correct bit into the result. */
12636 outhi <<= 1;
12637 if ((outlo & hibit))
12638 outhi |= 1;
12639 outlo <<= 1;
12640 if (acc >= 10)
12642 acc -= 10;
12643 outlo |= 1;
12647 /* '0' == 060 in Java, but might not be here (think EBCDIC). */
12648 *--p = '\060' + acc;
12650 hi = outhi;
12651 lo = outlo;
12653 while (hi || lo);
12655 if (neg)
12656 *--p = '\055'; /* '-' == 055 in Java, but might not be here. */
12658 return p;
12661 /* Print an INTEGER_CST node in a static buffer, and return the
12662 buffer. This is used only for error handling. */
12663 char *
12664 print_int_node (tree node)
12666 static char buffer [80];
12667 if (TREE_CONSTANT_OVERFLOW (node))
12668 sprintf (buffer, "<overflow>");
12670 if (TREE_INT_CST_HIGH (node) == 0)
12671 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12672 TREE_INT_CST_LOW (node));
12673 else if (TREE_INT_CST_HIGH (node) == -1
12674 && TREE_INT_CST_LOW (node) != 0)
12675 sprintf (buffer, "-" HOST_WIDE_INT_PRINT_UNSIGNED,
12676 -TREE_INT_CST_LOW (node));
12677 else
12678 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12679 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12681 return buffer;
12685 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12686 context. */
12688 /* 15.25 Assignment operators. */
12690 static tree
12691 patch_assignment (tree node, tree wfl_op1)
12693 tree rhs = TREE_OPERAND (node, 1);
12694 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12695 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12696 int error_found = 0;
12697 int lvalue_from_array = 0;
12698 int is_return = 0;
12700 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12702 /* Lhs can be a named variable */
12703 if (JDECL_P (lvalue))
12705 lhs_type = TREE_TYPE (lvalue);
12707 /* Or Lhs can be an array access. */
12708 else if (TREE_CODE (lvalue) == ARRAY_REF)
12710 lhs_type = TREE_TYPE (lvalue);
12711 lvalue_from_array = 1;
12713 /* Or a field access */
12714 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12715 lhs_type = TREE_TYPE (lvalue);
12716 /* Or a function return slot */
12717 else if (TREE_CODE (lvalue) == RESULT_DECL)
12719 /* If the return type is an integral type, then we create the
12720 RESULT_DECL with a promoted type, but we need to do these
12721 checks against the unpromoted type to ensure type safety. So
12722 here we look at the real type, not the type of the decl we
12723 are modifying. */
12724 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12725 is_return = 1;
12727 /* Otherwise, we might want to try to write into an optimized static
12728 final, this is an of a different nature, reported further on. */
12729 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12730 && resolve_expression_name (wfl_op1, &llvalue))
12732 lhs_type = TREE_TYPE (lvalue);
12734 else
12736 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12737 error_found = 1;
12740 rhs_type = TREE_TYPE (rhs);
12742 /* 5.1 Try the assignment conversion for builtin type. */
12743 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12745 /* 5.2 If it failed, try a reference conversion */
12746 if (!new_rhs)
12747 new_rhs = try_reference_assignconv (lhs_type, rhs);
12749 /* 15.25.2 If we have a compound assignment, convert RHS into the
12750 type of the LHS */
12751 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12752 new_rhs = convert (lhs_type, rhs);
12754 /* Explicit cast required. This is an error */
12755 if (!new_rhs)
12757 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12758 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12759 tree wfl;
12760 char operation [32]; /* Max size known */
12762 /* If the assignment is part of a declaration, we use the WFL of
12763 the declared variable to point out the error and call it a
12764 declaration problem. If the assignment is a genuine =
12765 operator, we call is a operator `=' problem, otherwise we
12766 call it an assignment problem. In both of these last cases,
12767 we use the WFL of the operator to indicate the error. */
12769 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12771 wfl = wfl_op1;
12772 strcpy (operation, "declaration");
12774 else
12776 wfl = wfl_operator;
12777 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12778 strcpy (operation, "assignment");
12779 else if (is_return)
12780 strcpy (operation, "'return'");
12781 else
12782 strcpy (operation, "'='");
12785 if (!valid_cast_to_p (rhs_type, lhs_type))
12786 parse_error_context
12787 (wfl, "Incompatible type for %s. Can't convert %qs to %qs",
12788 operation, t1, t2);
12789 else
12790 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert %qs to %qs",
12791 operation, t1, t2);
12792 free (t1); free (t2);
12793 error_found = 1;
12796 if (error_found)
12797 return error_mark_node;
12799 /* If we're processing a `return' statement, promote the actual type
12800 to the promoted type. */
12801 if (is_return)
12802 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12804 /* 10.10: Array Store Exception runtime check */
12805 if (!flag_emit_class_files
12806 && lvalue_from_array
12807 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12809 tree array, store_check, base, index_expr;
12811 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12812 new_rhs = save_expr (new_rhs);
12814 /* Get the INDIRECT_REF. */
12815 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12816 /* Get the array pointer expr. */
12817 array = TREE_OPERAND (array, 0);
12818 store_check = build_java_arraystore_check (array, new_rhs);
12820 index_expr = TREE_OPERAND (lvalue, 1);
12822 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12824 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12825 happen before the store check, so prepare to insert the store
12826 check within the second operand of the existing COMPOUND_EXPR. */
12827 base = index_expr;
12829 else
12830 base = lvalue;
12832 index_expr = TREE_OPERAND (base, 1);
12833 TREE_OPERAND (base, 1) = build2 (COMPOUND_EXPR, TREE_TYPE (index_expr),
12834 store_check, index_expr);
12837 /* Final locals can be used as case values in switch
12838 statement. Prepare them for this eventuality. */
12839 if (TREE_CODE (lvalue) == VAR_DECL
12840 && DECL_FINAL (lvalue)
12841 && TREE_CONSTANT (new_rhs)
12842 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12843 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12846 TREE_CONSTANT (lvalue) = 1;
12847 TREE_INVARIANT (lvalue) = 1;
12848 DECL_INITIAL (lvalue) = new_rhs;
12851 /* Copy the rhs if it's a reference. */
12852 if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12854 switch (TREE_CODE (new_rhs))
12856 case ARRAY_REF:
12857 case INDIRECT_REF:
12858 case COMPONENT_REF:
12859 /* Transform a = foo.bar
12860 into a = ({int tmp; tmp = foo.bar;}).
12861 We need to ensure that if a read from memory fails
12862 because of a NullPointerException, a destination variable
12863 will remain unchanged. An explicit temporary does what
12864 we need.
12866 If flag_check_references is set, this is unnecessary
12867 because we'll check each reference before doing any
12868 reads. If optimize is not set the result will never be
12869 written to a stack slot that contains the LHS. */
12871 tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12872 TREE_TYPE (new_rhs));
12873 tree block = make_node (BLOCK);
12874 tree assignment
12875 = build2 (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12876 DECL_CONTEXT (tmp) = current_function_decl;
12877 TREE_TYPE (block) = TREE_TYPE (new_rhs);
12878 BLOCK_VARS (block) = tmp;
12879 BLOCK_EXPR_BODY (block) = assignment;
12880 TREE_SIDE_EFFECTS (block) = 1;
12881 new_rhs = block;
12883 break;
12884 default:
12885 break;
12889 TREE_OPERAND (node, 0) = lvalue;
12890 TREE_OPERAND (node, 1) = new_rhs;
12891 TREE_TYPE (node) = lhs_type;
12892 return node;
12895 /* Check that type SOURCE can be cast into type DEST. If the cast
12896 can't occur at all, return NULL; otherwise, return a possibly
12897 modified rhs. */
12899 static tree
12900 try_reference_assignconv (tree lhs_type, tree rhs)
12902 tree new_rhs = NULL_TREE;
12903 tree rhs_type = TREE_TYPE (rhs);
12905 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12907 /* `null' may be assigned to any reference type */
12908 if (rhs == null_pointer_node)
12909 new_rhs = null_pointer_node;
12910 /* Try the reference assignment conversion */
12911 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12912 new_rhs = rhs;
12913 /* This is a magic assignment that we process differently */
12914 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12915 new_rhs = rhs;
12917 return new_rhs;
12920 /* Check that RHS can be converted into LHS_TYPE by the assignment
12921 conversion (5.2), for the cases of RHS being a builtin type. Return
12922 NULL_TREE if the conversion fails or if because RHS isn't of a
12923 builtin type. Return a converted RHS if the conversion is possible. */
12925 static tree
12926 try_builtin_assignconv (tree wfl_op1, tree lhs_type, tree rhs)
12928 tree new_rhs = NULL_TREE;
12929 tree rhs_type = TREE_TYPE (rhs);
12931 /* Handle boolean specially. */
12932 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12933 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12935 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12936 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12937 new_rhs = rhs;
12940 /* 5.1.1 Try Identity Conversion,
12941 5.1.2 Try Widening Primitive Conversion */
12942 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12943 new_rhs = convert (lhs_type, rhs);
12945 /* Try a narrowing primitive conversion (5.1.3):
12946 - expression is a constant expression of type byte, short, char,
12947 or int, AND
12948 - variable is byte, short or char AND
12949 - The value of the expression is representable in the type of the
12950 variable */
12951 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12952 || rhs_type == char_type_node || rhs_type == int_type_node)
12953 && TREE_CONSTANT (rhs)
12954 && (lhs_type == byte_type_node || lhs_type == char_type_node
12955 || lhs_type == short_type_node))
12957 if (int_fits_type_p (rhs, lhs_type))
12958 new_rhs = convert (lhs_type, rhs);
12959 else if (wfl_op1) /* Might be called with a NULL */
12960 parse_warning_context
12961 (wfl_op1,
12962 "Constant expression %qs too wide for narrowing primitive conversion to %qs",
12963 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12964 /* Reported a warning that will turn into an error further
12965 down, so we don't return */
12968 return new_rhs;
12971 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12972 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12973 0 is the conversion test fails. This implements parts the method
12974 invocation conversion (5.3). */
12976 static int
12977 valid_builtin_assignconv_identity_widening_p (tree lhs_type, tree rhs_type)
12979 /* 5.1.1: This is the identity conversion part. */
12980 if (lhs_type == rhs_type)
12981 return 1;
12983 /* Reject non primitive types and boolean conversions. */
12984 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12985 return 0;
12987 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12988 than a char can't be converted into a char. Short can't too, but
12989 the < test below takes care of that */
12990 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12991 return 0;
12993 /* Accept all promoted type here. Note, we can't use <= in the test
12994 below, because we still need to bounce out assignments of short
12995 to char and the likes */
12996 if (lhs_type == int_type_node
12997 && (rhs_type == promoted_byte_type_node
12998 || rhs_type == promoted_short_type_node
12999 || rhs_type == promoted_char_type_node
13000 || rhs_type == promoted_boolean_type_node))
13001 return 1;
13003 /* From here, an integral is widened if its precision is smaller
13004 than the precision of the LHS or if the LHS is a floating point
13005 type, or the RHS is a float and the RHS a double. */
13006 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13007 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13008 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13009 || (rhs_type == float_type_node && lhs_type == double_type_node))
13010 return 1;
13012 return 0;
13015 /* Check that something of SOURCE type can be assigned or cast to
13016 something of DEST type at runtime. Return 1 if the operation is
13017 valid, 0 otherwise. If CAST is set to 1, we're treating the case
13018 were SOURCE is cast into DEST, which borrows a lot of the
13019 assignment check. */
13021 static int
13022 valid_ref_assignconv_cast_p (tree source, tree dest, int cast)
13024 /* SOURCE or DEST might be null if not from a declared entity. */
13025 if (!source || !dest)
13026 return 0;
13027 if (JNULLP_TYPE_P (source))
13028 return 1;
13029 if (TREE_CODE (source) == POINTER_TYPE)
13030 source = TREE_TYPE (source);
13031 if (TREE_CODE (dest) == POINTER_TYPE)
13032 dest = TREE_TYPE (dest);
13034 /* If source and dest are being compiled from bytecode, they may need to
13035 be loaded. */
13036 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13038 load_class (source, 1);
13039 safe_layout_class (source);
13041 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13043 load_class (dest, 1);
13044 safe_layout_class (dest);
13047 /* Case where SOURCE is a class type */
13048 if (TYPE_CLASS_P (source))
13050 if (TYPE_CLASS_P (dest))
13051 return (source == dest
13052 || inherits_from_p (source, dest)
13053 || (cast && inherits_from_p (dest, source)));
13054 if (TYPE_INTERFACE_P (dest))
13056 /* If doing a cast and SOURCE is final, the operation is
13057 always correct a compile time (because even if SOURCE
13058 does not implement DEST, a subclass of SOURCE might). */
13059 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13060 return 1;
13061 /* Otherwise, SOURCE must implement DEST */
13062 return interface_of_p (dest, source);
13064 /* DEST is an array, cast permitted if SOURCE is of Object type */
13065 return (cast && source == object_type_node ? 1 : 0);
13067 if (TYPE_INTERFACE_P (source))
13069 if (TYPE_CLASS_P (dest))
13071 /* If not casting, DEST must be the Object type */
13072 if (!cast)
13073 return dest == object_type_node;
13074 /* We're doing a cast. The cast is always valid is class
13075 DEST is not final, otherwise, DEST must implement SOURCE */
13076 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13077 return 1;
13078 else
13079 return interface_of_p (source, dest);
13081 if (TYPE_INTERFACE_P (dest))
13083 /* If doing a cast, then if SOURCE and DEST contain method
13084 with the same signature but different return type, then
13085 this is a (compile time) error */
13086 if (cast)
13088 tree method_source, method_dest;
13089 tree source_type;
13090 tree source_sig;
13091 tree source_name;
13092 for (method_source = TYPE_METHODS (source); method_source;
13093 method_source = TREE_CHAIN (method_source))
13095 source_sig =
13096 build_java_argument_signature (TREE_TYPE (method_source));
13097 source_type = TREE_TYPE (TREE_TYPE (method_source));
13098 source_name = DECL_NAME (method_source);
13099 for (method_dest = TYPE_METHODS (dest);
13100 method_dest; method_dest = TREE_CHAIN (method_dest))
13101 if (source_sig ==
13102 build_java_argument_signature (TREE_TYPE (method_dest))
13103 && source_name == DECL_NAME (method_dest)
13104 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13105 return 0;
13107 return 1;
13109 else
13110 return source == dest || interface_of_p (dest, source);
13112 else
13114 /* Array */
13115 return (cast
13116 && (DECL_NAME (TYPE_NAME (source))
13117 == java_lang_cloneable_identifier_node
13118 || (DECL_NAME (TYPE_NAME (source))
13119 == java_io_serializable_identifier_node)));
13122 if (TYPE_ARRAY_P (source))
13124 if (TYPE_CLASS_P (dest))
13125 return dest == object_type_node;
13126 /* Can't cast an array to an interface unless the interface is
13127 java.lang.Cloneable or java.io.Serializable. */
13128 if (TYPE_INTERFACE_P (dest))
13129 return (DECL_NAME (TYPE_NAME (dest))
13130 == java_lang_cloneable_identifier_node
13131 || (DECL_NAME (TYPE_NAME (dest))
13132 == java_io_serializable_identifier_node));
13133 else /* Arrays */
13135 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13136 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13138 /* In case of severe errors, they turn out null */
13139 if (!dest_element_type || !source_element_type)
13140 return 0;
13141 if (source_element_type == dest_element_type)
13142 return 1;
13143 return valid_ref_assignconv_cast_p (source_element_type,
13144 dest_element_type, cast);
13146 return 0;
13148 return 0;
13151 static int
13152 valid_cast_to_p (tree source, tree dest)
13154 if (TREE_CODE (source) == POINTER_TYPE)
13155 source = TREE_TYPE (source);
13156 if (TREE_CODE (dest) == POINTER_TYPE)
13157 dest = TREE_TYPE (dest);
13159 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13160 return valid_ref_assignconv_cast_p (source, dest, 1);
13162 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13163 return 1;
13165 else if (TREE_CODE (source) == BOOLEAN_TYPE
13166 && TREE_CODE (dest) == BOOLEAN_TYPE)
13167 return 1;
13169 return 0;
13172 static tree
13173 do_unary_numeric_promotion (tree arg)
13175 tree type = TREE_TYPE (arg);
13176 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13177 || TREE_CODE (type) == CHAR_TYPE)
13178 arg = convert (int_type_node, arg);
13179 return arg;
13182 /* Return a nonzero value if SOURCE can be converted into DEST using
13183 the method invocation conversion rule (5.3). */
13184 static int
13185 valid_method_invocation_conversion_p (tree dest, tree source)
13187 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13188 && valid_builtin_assignconv_identity_widening_p (dest, source))
13189 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13190 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13191 && valid_ref_assignconv_cast_p (source, dest, 0)));
13194 /* Build an incomplete binop expression. */
13196 static tree
13197 build_binop (enum tree_code op, int op_location, tree op1, tree op2)
13199 tree binop = build2 (op, NULL_TREE, op1, op2);
13200 TREE_SIDE_EFFECTS (binop) = 1;
13201 /* Store the location of the operator, for better error report. The
13202 string of the operator will be rebuild based on the OP value. */
13203 EXPR_WFL_LINECOL (binop) = op_location;
13204 return binop;
13207 /* Build the string of the operator retained by NODE. If NODE is part
13208 of a compound expression, add an '=' at the end of the string. This
13209 function is called when an error needs to be reported on an
13210 operator. The string is returned as a pointer to a static character
13211 buffer. */
13213 static char *
13214 operator_string (tree node)
13216 #define BUILD_OPERATOR_STRING(S) \
13218 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13219 return buffer; \
13222 static char buffer [10];
13223 switch (TREE_CODE (node))
13225 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13226 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13227 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13228 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13229 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13230 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13231 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13232 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13233 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13234 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13235 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13236 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13237 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13238 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13239 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13240 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13241 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13242 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13243 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13244 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13245 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13246 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13247 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13248 case PREINCREMENT_EXPR: /* Fall through */
13249 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13250 case PREDECREMENT_EXPR: /* Fall through */
13251 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13252 default:
13253 internal_error ("unregistered operator %s",
13254 tree_code_name [TREE_CODE (node)]);
13256 return NULL;
13257 #undef BUILD_OPERATOR_STRING
13260 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13262 static int
13263 java_decl_equiv (tree var_acc1, tree var_acc2)
13265 if (JDECL_P (var_acc1))
13266 return (var_acc1 == var_acc2);
13268 return (TREE_CODE (var_acc1) == COMPONENT_REF
13269 && TREE_CODE (var_acc2) == COMPONENT_REF
13270 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13271 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13272 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13275 /* Return a nonzero value if CODE is one of the operators that can be
13276 used in conjunction with the `=' operator in a compound assignment. */
13278 static int
13279 binop_compound_p (enum tree_code code)
13281 int i;
13282 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13283 if (binop_lookup [i] == code)
13284 break;
13286 return i < BINOP_COMPOUND_CANDIDATES;
13289 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13291 static tree
13292 java_refold (tree t)
13294 tree c, b, ns, decl;
13296 if (TREE_CODE (t) != MODIFY_EXPR)
13297 return t;
13299 c = TREE_OPERAND (t, 1);
13300 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13301 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13302 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13303 return t;
13305 /* Now the left branch of the binary operator. */
13306 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13307 if (! (b && TREE_CODE (b) == NOP_EXPR
13308 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13309 return t;
13311 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13312 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13313 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13314 return t;
13316 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13317 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13318 /* It's got to be the an equivalent decl */
13319 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13321 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13322 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13323 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13324 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13325 /* Change the right part of the BINOP_EXPR */
13326 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13329 return t;
13332 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13333 errors but we modify NODE so that it contains the type computed
13334 according to the expression, when it's fixed. Otherwise, we write
13335 error_mark_node as the type. It allows us to further the analysis
13336 of remaining nodes and detects more errors in certain cases. */
13338 static tree
13339 patch_binop (tree node, tree wfl_op1, tree wfl_op2)
13341 tree op1 = TREE_OPERAND (node, 0);
13342 tree op2 = TREE_OPERAND (node, 1);
13343 tree op1_type = TREE_TYPE (op1);
13344 tree op2_type = TREE_TYPE (op2);
13345 tree prom_type = NULL_TREE, cn;
13346 enum tree_code code = TREE_CODE (node);
13348 /* If 1, tell the routine that we have to return error_mark_node
13349 after checking for the initialization of the RHS */
13350 int error_found = 0;
13352 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13354 /* If either op<n>_type are NULL, this might be early signs of an
13355 error situation, unless it's too early to tell (in case we're
13356 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13357 correctly so the error can be later on reported accurately. */
13358 if (! (code == PLUS_EXPR || code == NE_EXPR
13359 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13361 tree n;
13362 if (! op1_type)
13364 n = java_complete_tree (op1);
13365 op1_type = TREE_TYPE (n);
13367 if (! op2_type)
13369 n = java_complete_tree (op2);
13370 op2_type = TREE_TYPE (n);
13374 switch (code)
13376 /* 15.16 Multiplicative operators */
13377 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13378 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13379 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13380 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13381 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13383 if (!JNUMERIC_TYPE_P (op1_type))
13384 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13385 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13386 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13387 TREE_TYPE (node) = error_mark_node;
13388 error_found = 1;
13389 break;
13391 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13393 /* Detect integral division by zero */
13394 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13395 && TREE_CODE (prom_type) == INTEGER_TYPE
13396 && (op2 == integer_zero_node || op2 == long_zero_node ||
13397 (TREE_CODE (op2) == INTEGER_CST &&
13398 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13400 parse_warning_context
13401 (wfl_operator,
13402 "Evaluating this expression will result in an arithmetic exception being thrown");
13403 TREE_CONSTANT (node) = 0;
13404 TREE_INVARIANT (node) = 0;
13407 /* Change the division operator if necessary */
13408 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13409 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13411 /* Before divisions as is disappear, try to simplify and bail if
13412 applicable, otherwise we won't perform even simple
13413 simplifications like (1-1)/3. We can't do that with floating
13414 point number, folds can't handle them at this stage. */
13415 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13416 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13418 TREE_TYPE (node) = prom_type;
13419 node = fold (node);
13420 if (TREE_CODE (node) != code)
13421 return node;
13424 if (TREE_CODE (prom_type) == INTEGER_TYPE
13425 && flag_use_divide_subroutine
13426 && ! flag_emit_class_files
13427 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13428 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13430 /* This one is more complicated. FLOATs are processed by a
13431 function call to soft_fmod. Duplicate the value of the
13432 COMPOUND_ASSIGN_P flag. */
13433 if (code == TRUNC_MOD_EXPR)
13435 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13436 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13437 return mod;
13439 break;
13441 /* 15.17 Additive Operators */
13442 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13444 /* Operation is valid if either one argument is a string
13445 constant, a String object or a StringBuffer crafted for the
13446 purpose of the a previous usage of the String concatenation
13447 operator */
13449 if (TREE_CODE (op1) == STRING_CST
13450 || TREE_CODE (op2) == STRING_CST
13451 || JSTRING_TYPE_P (op1_type)
13452 || JSTRING_TYPE_P (op2_type)
13453 || IS_CRAFTED_STRING_BUFFER_P (op1)
13454 || IS_CRAFTED_STRING_BUFFER_P (op2))
13455 return build_string_concatenation (op1, op2);
13457 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13458 Numeric Types */
13459 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13461 if (!JNUMERIC_TYPE_P (op1_type))
13462 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13463 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13464 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13465 TREE_TYPE (node) = error_mark_node;
13466 error_found = 1;
13467 break;
13469 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13470 break;
13472 /* 15.18 Shift Operators */
13473 case LSHIFT_EXPR:
13474 case RSHIFT_EXPR:
13475 case URSHIFT_EXPR:
13476 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13478 if (!JINTEGRAL_TYPE_P (op1_type))
13479 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13480 else
13482 if (JNUMERIC_TYPE_P (op2_type))
13483 parse_error_context (wfl_operator,
13484 "Incompatible type for %qs. Explicit cast needed to convert shift distance from %qs to integral",
13485 operator_string (node),
13486 lang_printable_name (op2_type, 0));
13487 else
13488 parse_error_context (wfl_operator,
13489 "Incompatible type for %qs. Can't convert shift distance from %qs to integral",
13490 operator_string (node),
13491 lang_printable_name (op2_type, 0));
13493 TREE_TYPE (node) = error_mark_node;
13494 error_found = 1;
13495 break;
13498 /* Unary numeric promotion (5.6.1) is performed on each operand
13499 separately */
13500 op1 = do_unary_numeric_promotion (op1);
13501 op2 = do_unary_numeric_promotion (op2);
13503 /* If the right hand side is of type `long', first cast it to
13504 `int'. */
13505 if (TREE_TYPE (op2) == long_type_node)
13506 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13508 /* The type of the shift expression is the type of the promoted
13509 type of the left-hand operand */
13510 prom_type = TREE_TYPE (op1);
13512 /* Shift int only up to 0x1f and long up to 0x3f */
13513 if (prom_type == int_type_node)
13514 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13515 build_int_cst (NULL_TREE, 0x1f)));
13516 else
13517 op2 = fold (build2 (BIT_AND_EXPR, int_type_node, op2,
13518 build_int_cst (NULL_TREE, 0x3f)));
13520 /* The >>> operator is a >> operating on unsigned quantities */
13521 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13523 tree to_return;
13524 tree utype = java_unsigned_type (prom_type);
13525 op1 = convert (utype, op1);
13526 TREE_SET_CODE (node, RSHIFT_EXPR);
13527 TREE_OPERAND (node, 0) = op1;
13528 TREE_OPERAND (node, 1) = op2;
13529 TREE_TYPE (node) = utype;
13530 to_return = convert (prom_type, node);
13531 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13532 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13533 TREE_SIDE_EFFECTS (to_return)
13534 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13535 return to_return;
13537 break;
13539 /* 15.19.1 Type Comparison Operator instanceof */
13540 case INSTANCEOF_EXPR:
13542 TREE_TYPE (node) = boolean_type_node;
13544 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13545 if ((cn = patch_string (op1)))
13547 op1 = cn;
13548 op1_type = TREE_TYPE (op1);
13550 if (op1_type == NULL_TREE)
13551 abort ();
13553 if (!(op2_type = resolve_type_during_patch (op2)))
13554 return error_mark_node;
13556 /* The first operand must be a reference type or the null type */
13557 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13558 error_found = 1; /* Error reported further below */
13560 /* The second operand must be a reference type */
13561 if (!JREFERENCE_TYPE_P (op2_type))
13563 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13564 parse_error_context
13565 (wfl_operator, "Invalid argument %qs for %<instanceof%>",
13566 lang_printable_name (op2_type, 0));
13567 error_found = 1;
13570 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13572 /* If the first operand is null, the result is always false */
13573 if (op1 == null_pointer_node)
13574 return boolean_false_node;
13575 else if (flag_emit_class_files)
13577 TREE_OPERAND (node, 1) = op2_type;
13578 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13579 return node;
13581 /* Otherwise we have to invoke instance of to figure it out */
13582 else
13583 return build_instanceof (op1, op2_type);
13585 /* There is no way the expression operand can be an instance of
13586 the type operand. This is a compile time error. */
13587 else
13589 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13590 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13591 parse_error_context
13592 (wfl_operator, "Impossible for %qs to be instance of %qs",
13593 t1, lang_printable_name (op2_type, 0));
13594 free (t1);
13595 error_found = 1;
13598 break;
13600 /* 15.21 Bitwise and Logical Operators */
13601 case BIT_AND_EXPR:
13602 case BIT_XOR_EXPR:
13603 case BIT_IOR_EXPR:
13604 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13605 /* Binary numeric promotion is performed on both operand and the
13606 expression retain that type */
13607 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13609 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13610 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13611 /* The type of the bitwise operator expression is BOOLEAN */
13612 prom_type = boolean_type_node;
13613 else
13615 if (!JINTEGRAL_TYPE_P (op1_type))
13616 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13617 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13618 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13619 TREE_TYPE (node) = error_mark_node;
13620 error_found = 1;
13621 /* Insert a break here if adding thing before the switch's
13622 break for this case */
13624 break;
13626 /* 15.22 Conditional-And Operator */
13627 case TRUTH_ANDIF_EXPR:
13628 /* 15.23 Conditional-Or Operator */
13629 case TRUTH_ORIF_EXPR:
13630 /* Operands must be of BOOLEAN type */
13631 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13632 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13634 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13635 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13636 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13637 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13638 TREE_TYPE (node) = boolean_type_node;
13639 error_found = 1;
13640 break;
13642 else if (integer_zerop (op1))
13644 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13646 else if (integer_onep (op1))
13648 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13650 /* The type of the conditional operators is BOOLEAN */
13651 prom_type = boolean_type_node;
13652 break;
13654 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13655 case LT_EXPR:
13656 case GT_EXPR:
13657 case LE_EXPR:
13658 case GE_EXPR:
13659 /* The type of each of the operands must be a primitive numeric
13660 type */
13661 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13663 if (!JNUMERIC_TYPE_P (op1_type))
13664 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13665 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13666 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13667 TREE_TYPE (node) = boolean_type_node;
13668 error_found = 1;
13669 break;
13671 /* Binary numeric promotion is performed on the operands */
13672 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13673 /* The type of the relation expression is always BOOLEAN */
13674 prom_type = boolean_type_node;
13675 break;
13677 /* 15.20 Equality Operator */
13678 case EQ_EXPR:
13679 case NE_EXPR:
13680 /* It's time for us to patch the strings. */
13681 if ((cn = patch_string (op1)))
13683 op1 = cn;
13684 op1_type = TREE_TYPE (op1);
13686 if ((cn = patch_string (op2)))
13688 op2 = cn;
13689 op2_type = TREE_TYPE (op2);
13692 /* 15.20.1 Numerical Equality Operators == and != */
13693 /* Binary numeric promotion is performed on the operands */
13694 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13695 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13697 /* 15.20.2 Boolean Equality Operators == and != */
13698 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13699 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13700 ; /* Nothing to do here */
13702 /* 15.20.3 Reference Equality Operators == and != */
13703 /* Types have to be either references or the null type. If
13704 they're references, it must be possible to convert either
13705 type to the other by casting conversion. */
13706 else if ((op1 == null_pointer_node && op2 == null_pointer_node)
13707 || (op1 == null_pointer_node && JREFERENCE_TYPE_P (op2_type))
13708 || (JREFERENCE_TYPE_P (op1_type) && op2 == null_pointer_node)
13709 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13710 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13711 || valid_ref_assignconv_cast_p (op2_type,
13712 op1_type, 1))))
13713 ; /* Nothing to do here */
13715 /* Else we have an error figure what can't be converted into
13716 what and report the error */
13717 else
13719 char *t1;
13720 t1 = xstrdup (lang_printable_name (op1_type, 0));
13721 parse_error_context
13722 (wfl_operator,
13723 "Incompatible type for %qs. Can't convert %qs to %qs",
13724 operator_string (node), t1,
13725 lang_printable_name (op2_type, 0));
13726 free (t1);
13727 TREE_TYPE (node) = boolean_type_node;
13728 error_found = 1;
13729 break;
13731 prom_type = boolean_type_node;
13732 break;
13733 default:
13734 abort ();
13737 if (error_found)
13738 return error_mark_node;
13740 TREE_OPERAND (node, 0) = op1;
13741 TREE_OPERAND (node, 1) = op2;
13742 TREE_TYPE (node) = prom_type;
13743 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13745 /* fold does not respect side-effect order as required for Java but not C.
13746 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13747 * bytecode.
13749 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13750 : ! TREE_SIDE_EFFECTS (node))
13751 node = fold (node);
13752 return node;
13755 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13756 zero value, the value of CSTE comes after the valude of STRING */
13758 static tree
13759 do_merge_string_cste (tree cste, const char *string, int string_len, int after)
13761 const char *old = TREE_STRING_POINTER (cste);
13762 int old_len = TREE_STRING_LENGTH (cste);
13763 int len = old_len + string_len;
13764 char *new = alloca (len+1);
13766 if (after)
13768 memcpy (new, string, string_len);
13769 memcpy (&new [string_len], old, old_len);
13771 else
13773 memcpy (new, old, old_len);
13774 memcpy (&new [old_len], string, string_len);
13776 new [len] = '\0';
13777 return build_string (len, new);
13780 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13781 new STRING_CST on success, NULL_TREE on failure. */
13783 static tree
13784 merge_string_cste (tree op1, tree op2, int after)
13786 /* Handle two string constants right away. */
13787 if (TREE_CODE (op2) == STRING_CST)
13788 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13789 TREE_STRING_LENGTH (op2), after);
13791 /* Reasonable integer constant can be treated right away. */
13792 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13794 static const char *const boolean_true = "true";
13795 static const char *const boolean_false = "false";
13796 static const char *const null_pointer = "null";
13797 char ch[4];
13798 const char *string;
13800 if (op2 == boolean_true_node)
13801 string = boolean_true;
13802 else if (op2 == boolean_false_node)
13803 string = boolean_false;
13804 else if (op2 == null_pointer_node
13805 || (integer_zerop (op2)
13806 && TREE_CODE (TREE_TYPE (op2)) == POINTER_TYPE))
13807 /* FIXME: null is not a compile-time constant, so it is only safe to
13808 merge if the overall expression is non-constant. However, this
13809 code always merges without checking the overall expression. */
13810 string = null_pointer;
13811 else if (TREE_TYPE (op2) == char_type_node)
13813 /* Convert the character into UTF-8. */
13814 unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13815 unsigned char *p = (unsigned char *) ch;
13816 if (0x01 <= c && c <= 0x7f)
13817 *p++ = (unsigned char) c;
13818 else if (c < 0x7ff)
13820 *p++ = (unsigned char) (c >> 6 | 0xc0);
13821 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13823 else
13825 *p++ = (unsigned char) (c >> 12 | 0xe0);
13826 *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13827 *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13829 *p = '\0';
13831 string = ch;
13833 else
13834 string = string_convert_int_cst (op2);
13836 return do_merge_string_cste (op1, string, strlen (string), after);
13838 return NULL_TREE;
13841 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13842 has to be a STRING_CST and the other part must be a STRING_CST or a
13843 INTEGRAL constant. Return a new STRING_CST if the operation
13844 succeed, NULL_TREE otherwise.
13846 If the case we want to optimize for space, we might want to return
13847 NULL_TREE for each invocation of this routine. FIXME */
13849 static tree
13850 string_constant_concatenation (tree op1, tree op2)
13852 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13854 tree string, rest;
13855 int invert;
13857 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13858 rest = (string == op1 ? op2 : op1);
13859 invert = (string == op1 ? 0 : 1 );
13861 /* Walk REST, only if it looks reasonable */
13862 if (TREE_CODE (rest) != STRING_CST
13863 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13864 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13865 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13867 rest = java_complete_tree (rest);
13868 if (rest == error_mark_node)
13869 return error_mark_node;
13870 rest = fold (rest);
13872 return merge_string_cste (string, rest, invert);
13874 return NULL_TREE;
13877 /* Implement the `+' operator. Does static optimization if possible,
13878 otherwise create (if necessary) and append elements to a
13879 StringBuffer. The StringBuffer will be carried around until it is
13880 used for a function call or an assignment. Then toString() will be
13881 called on it to turn it into a String object. */
13883 static tree
13884 build_string_concatenation (tree op1, tree op2)
13886 tree result;
13887 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13889 /* Try to do some static optimization */
13890 if ((result = string_constant_concatenation (op1, op2)))
13891 return result;
13893 /* Discard empty strings on either side of the expression */
13894 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13896 op1 = op2;
13897 op2 = NULL_TREE;
13899 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13900 op2 = NULL_TREE;
13902 /* If operands are string constant, turn then into object references */
13903 if (TREE_CODE (op1) == STRING_CST)
13904 op1 = patch_string_cst (op1);
13905 if (op2 && TREE_CODE (op2) == STRING_CST)
13906 op2 = patch_string_cst (op2);
13908 /* If either one of the constant is null and the other non null
13909 operand is a String constant, return it. */
13910 if ((TREE_CODE (op1) == STRING_CST) && !op2)
13911 return op1;
13913 /* If OP1 isn't already a StringBuffer, create and
13914 initialize a new one */
13915 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13917 /* Two solutions here:
13918 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13919 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13920 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13921 op1 = BUILD_STRING_BUFFER (op1);
13922 else
13924 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13925 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13929 if (op2)
13931 /* OP1 is no longer the last node holding a crafted StringBuffer */
13932 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13933 /* Create a node for `{new...,xxx}.append (op2)' */
13934 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13937 /* Mark the last node holding a crafted StringBuffer */
13938 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13940 TREE_SIDE_EFFECTS (op1) = side_effects;
13941 return op1;
13944 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13945 StringBuffer. If no string were found to be patched, return
13946 NULL. */
13948 static tree
13949 patch_string (tree node)
13951 if (node == error_mark_node)
13952 return error_mark_node;
13953 if (TREE_CODE (node) == STRING_CST)
13954 return patch_string_cst (node);
13955 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13957 int saved = ctxp->explicit_constructor_p;
13958 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13959 tree ret;
13960 /* Temporary disable forbid the use of `this'. */
13961 ctxp->explicit_constructor_p = 0;
13962 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13963 /* String concatenation arguments must be evaluated in order too. */
13964 ret = force_evaluation_order (ret);
13965 /* Restore it at its previous value */
13966 ctxp->explicit_constructor_p = saved;
13967 return ret;
13969 return NULL_TREE;
13972 /* Build the internal representation of a string constant. */
13974 static tree
13975 patch_string_cst (tree node)
13977 int location;
13978 if (! flag_emit_class_files)
13980 node = get_identifier (TREE_STRING_POINTER (node));
13981 location = alloc_name_constant (CONSTANT_String, node);
13982 node = build_ref_from_constant_pool (location);
13984 TREE_CONSTANT (node) = 1;
13985 TREE_INVARIANT (node) = 1;
13987 /* ??? Guessing that the class file code can't handle casts. */
13988 if (! flag_emit_class_files)
13989 node = convert (string_ptr_type_node, node);
13990 else
13991 TREE_TYPE (node) = string_ptr_type_node;
13993 return node;
13996 /* Build an incomplete unary operator expression. */
13998 static tree
13999 build_unaryop (int op_token, int op_location, tree op1)
14001 enum tree_code op;
14002 tree unaryop;
14003 switch (op_token)
14005 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14006 case MINUS_TK: op = NEGATE_EXPR; break;
14007 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14008 case NOT_TK: op = BIT_NOT_EXPR; break;
14009 default: abort ();
14012 unaryop = build1 (op, NULL_TREE, op1);
14013 TREE_SIDE_EFFECTS (unaryop) = 1;
14014 /* Store the location of the operator, for better error report. The
14015 string of the operator will be rebuild based on the OP value. */
14016 EXPR_WFL_LINECOL (unaryop) = op_location;
14017 return unaryop;
14020 /* Special case for the ++/-- operators, since they require an extra
14021 argument to build, which is set to NULL and patched
14022 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14024 static tree
14025 build_incdec (int op_token, int op_location, tree op1, int is_post_p)
14027 static const enum tree_code lookup [2][2] =
14029 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14030 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14032 tree node = build2 (lookup [is_post_p][(op_token - DECR_TK)],
14033 NULL_TREE, op1, NULL_TREE);
14034 TREE_SIDE_EFFECTS (node) = 1;
14035 /* Store the location of the operator, for better error report. The
14036 string of the operator will be rebuild based on the OP value. */
14037 EXPR_WFL_LINECOL (node) = op_location;
14038 return node;
14041 /* Build an incomplete cast operator, based on the use of the
14042 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14043 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14044 though its type is already set. */
14046 static tree
14047 build_cast (int location, tree type, tree exp)
14049 tree node = build1 (CONVERT_EXPR, type, exp);
14050 EXPR_WFL_LINECOL (node) = location;
14051 return node;
14054 /* Build an incomplete class reference operator. */
14055 static tree
14056 build_incomplete_class_ref (int location, tree class_name)
14058 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14059 tree class_decl = GET_CPC ();
14060 tree this_class = TREE_TYPE (class_decl);
14062 /* Generate the synthetic static method `class$'. (Previously we
14063 deferred this, causing different method tables to be emitted
14064 for native code and bytecode.) */
14065 if (!TYPE_DOT_CLASS (this_class)
14066 && !JPRIMITIVE_TYPE_P (class_name)
14067 && !(TREE_CODE (class_name) == VOID_TYPE))
14069 tree cpc_list = GET_CPC_LIST();
14070 tree cpc = cpc_list;
14071 tree target_class;
14073 /* For inner classes, add a 'class$' method to their outermost
14074 context, creating it if necessary. */
14076 while (GET_NEXT_ENCLOSING_CPC(cpc))
14077 cpc = GET_NEXT_ENCLOSING_CPC(cpc);
14078 class_decl = TREE_VALUE (cpc);
14080 target_class = TREE_TYPE (class_decl);
14082 if (CLASS_INTERFACE (TYPE_NAME (target_class)))
14084 /* For interfaces, adding a static 'class$' method directly
14085 is illegal. So create an inner class to contain the new
14086 method. Empirically this matches the behavior of javac. */
14087 tree t, inner;
14088 /* We want the generated inner class inside the outermost class. */
14089 GET_CPC_LIST() = cpc;
14090 t = build_wfl_node (DECL_NAME (TYPE_NAME (object_type_node)));
14091 inner = create_anonymous_class (t);
14092 target_class = TREE_TYPE (inner);
14093 end_class_declaration (1);
14094 GET_CPC_LIST() = cpc_list;
14097 if (TYPE_DOT_CLASS (target_class) == NULL_TREE)
14098 build_dot_class_method (target_class);
14100 if (this_class != target_class)
14101 TYPE_DOT_CLASS (this_class) = TYPE_DOT_CLASS (target_class);
14104 EXPR_WFL_LINECOL (node) = location;
14105 return node;
14108 /* Complete an incomplete class reference operator. */
14109 static tree
14110 patch_incomplete_class_ref (tree node)
14112 tree type = TREE_OPERAND (node, 0);
14113 tree ref_type;
14115 if (!(ref_type = resolve_type_during_patch (type)))
14116 return error_mark_node;
14118 /* If we're not emitting class files and we know ref_type is a
14119 compiled class, build a direct reference. */
14120 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14121 || JPRIMITIVE_TYPE_P (ref_type)
14122 || TREE_CODE (ref_type) == VOID_TYPE)
14124 tree dot = build_class_ref (ref_type);
14125 /* A class referenced by `foo.class' is initialized. */
14126 if (!flag_emit_class_files)
14127 dot = build_class_init (ref_type, dot);
14128 return java_complete_tree (dot);
14131 /* If we're emitting class files and we have to deal with non
14132 primitive types, we invoke the synthetic static method `class$'. */
14133 ref_type = build_dot_class_method_invocation (current_class, ref_type);
14134 return java_complete_tree (ref_type);
14137 /* 15.14 Unary operators. We return error_mark_node in case of error,
14138 but preserve the type of NODE if the type is fixed. */
14140 static tree
14141 patch_unaryop (tree node, tree wfl_op)
14143 tree op = TREE_OPERAND (node, 0);
14144 tree op_type = TREE_TYPE (op);
14145 tree prom_type = NULL_TREE, value, decl;
14146 int outer_field_flag = 0;
14147 int code = TREE_CODE (node);
14148 int error_found = 0;
14150 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14152 switch (code)
14154 /* 15.13.2 Postfix Increment Operator ++ */
14155 case POSTINCREMENT_EXPR:
14156 /* 15.13.3 Postfix Increment Operator -- */
14157 case POSTDECREMENT_EXPR:
14158 /* 15.14.1 Prefix Increment Operator ++ */
14159 case PREINCREMENT_EXPR:
14160 /* 15.14.2 Prefix Decrement Operator -- */
14161 case PREDECREMENT_EXPR:
14162 op = decl = extract_field_decl (op);
14163 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14164 /* We might be trying to change an outer field accessed using
14165 access method. */
14166 if (outer_field_flag)
14168 /* Retrieve the decl of the field we're trying to access. We
14169 do that by first retrieving the function we would call to
14170 access the field. It has been already verified that this
14171 field isn't final */
14172 if (flag_emit_class_files)
14173 decl = TREE_OPERAND (op, 0);
14174 else
14175 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14176 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14178 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14179 else if (!JDECL_P (decl)
14180 && TREE_CODE (decl) != COMPONENT_REF
14181 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14182 && TREE_CODE (decl) != INDIRECT_REF
14183 && !(TREE_CODE (decl) == COMPOUND_EXPR
14184 && TREE_OPERAND (decl, 1)
14185 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14187 TREE_TYPE (node) = error_mark_node;
14188 error_found = 1;
14191 /* From now on, we know that op if a variable and that it has a
14192 valid wfl. We use wfl_op to locate errors related to the
14193 ++/-- operand. */
14194 if (!JNUMERIC_TYPE_P (op_type))
14196 parse_error_context
14197 (wfl_op, "Invalid argument type %qs to %qs",
14198 lang_printable_name (op_type, 0), operator_string (node));
14199 TREE_TYPE (node) = error_mark_node;
14200 error_found = 1;
14202 else
14204 /* Before the addition, binary numeric promotion is performed on
14205 both operands, if really necessary */
14206 if (JINTEGRAL_TYPE_P (op_type))
14208 value = build_int_cst (op_type, 1);
14209 TREE_TYPE (node) = op_type;
14211 else
14213 value = build_int_cst (NULL_TREE, 1);
14214 TREE_TYPE (node) =
14215 binary_numeric_promotion (op_type,
14216 TREE_TYPE (value), &op, &value);
14219 /* We remember we might be accessing an outer field */
14220 if (outer_field_flag)
14222 /* We re-generate an access to the field */
14223 value = build2 (PLUS_EXPR, TREE_TYPE (op),
14224 build_outer_field_access (wfl_op, decl), value);
14226 /* And we patch the original access$() into a write
14227 with plus_op as a rhs */
14228 return outer_field_access_fix (node, op, value);
14231 /* And write back into the node. */
14232 TREE_OPERAND (node, 0) = op;
14233 TREE_OPERAND (node, 1) = value;
14234 /* Convert the overall back into its original type, if
14235 necessary, and return */
14236 if (JINTEGRAL_TYPE_P (op_type))
14237 return fold (node);
14238 else
14239 return fold (convert (op_type, node));
14241 break;
14243 /* 15.14.3 Unary Plus Operator + */
14244 case UNARY_PLUS_EXPR:
14245 /* 15.14.4 Unary Minus Operator - */
14246 case NEGATE_EXPR:
14247 if (!JNUMERIC_TYPE_P (op_type))
14249 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14250 TREE_TYPE (node) = error_mark_node;
14251 error_found = 1;
14253 /* Unary numeric promotion is performed on operand */
14254 else
14256 op = do_unary_numeric_promotion (op);
14257 prom_type = TREE_TYPE (op);
14258 if (code == UNARY_PLUS_EXPR)
14259 return fold (op);
14261 break;
14263 /* 15.14.5 Bitwise Complement Operator ~ */
14264 case BIT_NOT_EXPR:
14265 if (!JINTEGRAL_TYPE_P (op_type))
14267 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14268 TREE_TYPE (node) = error_mark_node;
14269 error_found = 1;
14271 else
14273 op = do_unary_numeric_promotion (op);
14274 prom_type = TREE_TYPE (op);
14276 break;
14278 /* 15.14.6 Logical Complement Operator ! */
14279 case TRUTH_NOT_EXPR:
14280 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14282 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14283 /* But the type is known. We will report an error if further
14284 attempt of a assignment is made with this rhs */
14285 TREE_TYPE (node) = boolean_type_node;
14286 error_found = 1;
14288 else
14289 prom_type = boolean_type_node;
14290 break;
14292 /* 15.15 Cast Expression */
14293 case CONVERT_EXPR:
14294 value = patch_cast (node, wfl_operator);
14295 if (value == error_mark_node)
14297 /* If this cast is part of an assignment, we tell the code
14298 that deals with it not to complain about a mismatch,
14299 because things have been cast, anyways */
14300 TREE_TYPE (node) = error_mark_node;
14301 error_found = 1;
14303 else
14305 value = fold (value);
14306 return value;
14308 break;
14311 if (error_found)
14312 return error_mark_node;
14314 /* There are cases where node has been replaced by something else
14315 and we don't end up returning here: UNARY_PLUS_EXPR,
14316 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14317 TREE_OPERAND (node, 0) = fold (op);
14318 TREE_TYPE (node) = prom_type;
14319 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14320 return fold (node);
14323 /* Generic type resolution that sometimes takes place during node
14324 patching. Returned the resolved type or generate an error
14325 message. Return the resolved type or NULL_TREE. */
14327 static tree
14328 resolve_type_during_patch (tree type)
14330 if (unresolved_type_p (type, NULL))
14332 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14333 if (!type_decl)
14335 parse_error_context (type,
14336 "Class %qs not found in type declaration",
14337 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14338 return NULL_TREE;
14341 check_deprecation (type, type_decl);
14343 return TREE_TYPE (type_decl);
14345 return type;
14348 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14349 found. Otherwise NODE or something meant to replace it is returned. */
14351 static tree
14352 patch_cast (tree node, tree wfl_op)
14354 tree op = TREE_OPERAND (node, 0);
14355 tree cast_type = TREE_TYPE (node);
14356 tree patched, op_type;
14357 char *t1;
14359 /* Some string patching might be necessary at this stage */
14360 if ((patched = patch_string (op)))
14361 TREE_OPERAND (node, 0) = op = patched;
14362 op_type = TREE_TYPE (op);
14364 /* First resolve OP_TYPE if unresolved */
14365 if (!(cast_type = resolve_type_during_patch (cast_type)))
14366 return error_mark_node;
14368 /* Check on cast that are proven correct at compile time */
14369 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14371 /* Same type */
14372 if (cast_type == op_type)
14373 return node;
14375 /* A narrowing conversion from a floating-point number to an
14376 integral type requires special handling (5.1.3). */
14377 if (JFLOAT_TYPE_P (op_type) && JINTEGRAL_TYPE_P (cast_type))
14378 if (cast_type != long_type_node)
14379 op = convert (integer_type_node, op);
14381 /* Try widening/narrowing conversion. Potentially, things need
14382 to be worked out in gcc so we implement the extreme cases
14383 correctly. fold_convert() needs to be fixed. */
14384 return convert (cast_type, op);
14387 /* It's also valid to cast a boolean into a boolean */
14388 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14389 return node;
14391 /* null can be casted to references */
14392 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14393 return build_null_of_type (cast_type);
14395 /* The remaining legal casts involve conversion between reference
14396 types. Check for their compile time correctness. */
14397 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14398 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14400 TREE_TYPE (node) = promote_type (cast_type);
14401 /* Now, the case can be determined correct at compile time if
14402 OP_TYPE can be converted into CAST_TYPE by assignment
14403 conversion (5.2) */
14405 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14407 TREE_SET_CODE (node, NOP_EXPR);
14408 return node;
14411 if (flag_emit_class_files)
14413 TREE_SET_CODE (node, CONVERT_EXPR);
14414 return node;
14417 /* The cast requires a run-time check */
14418 return build3 (CALL_EXPR, promote_type (cast_type),
14419 build_address_of (soft_checkcast_node),
14420 tree_cons (NULL_TREE, build_class_ref (cast_type),
14421 build_tree_list (NULL_TREE, op)),
14422 NULL_TREE);
14425 /* Any other casts are proven incorrect at compile time */
14426 t1 = xstrdup (lang_printable_name (op_type, 0));
14427 parse_error_context (wfl_op, "Invalid cast from %qs to %qs",
14428 t1, lang_printable_name (cast_type, 0));
14429 free (t1);
14430 return error_mark_node;
14433 /* Build a null constant and give it the type TYPE. */
14435 static tree
14436 build_null_of_type (tree type)
14438 tree node = build_int_cst (promote_type (type), 0);
14439 return node;
14442 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14443 a list of indices. */
14444 static tree
14445 build_array_ref (int location, tree array, tree index)
14447 tree node = build4 (ARRAY_REF, NULL_TREE, array, index,
14448 NULL_TREE, NULL_TREE);
14449 EXPR_WFL_LINECOL (node) = location;
14450 return node;
14453 /* 15.12 Array Access Expression */
14455 static tree
14456 patch_array_ref (tree node)
14458 tree array = TREE_OPERAND (node, 0);
14459 tree array_type = TREE_TYPE (array);
14460 tree index = TREE_OPERAND (node, 1);
14461 tree index_type = TREE_TYPE (index);
14462 int error_found = 0;
14464 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14466 if (TREE_CODE (array_type) == POINTER_TYPE)
14467 array_type = TREE_TYPE (array_type);
14469 /* The array reference must be an array */
14470 if (!TYPE_ARRAY_P (array_type))
14472 parse_error_context
14473 (wfl_operator,
14474 "%<[]%> can only be applied to arrays. It can't be applied to %qs",
14475 lang_printable_name (array_type, 0));
14476 TREE_TYPE (node) = error_mark_node;
14477 error_found = 1;
14480 /* The array index undergoes unary numeric promotion. The promoted
14481 type must be int */
14482 index = do_unary_numeric_promotion (index);
14483 if (TREE_TYPE (index) != int_type_node)
14485 if (valid_cast_to_p (index_type, int_type_node))
14486 parse_error_context (wfl_operator,
14487 "Incompatible type for %<[]%>. Explicit cast needed to convert %qs to %<int%>",
14488 lang_printable_name (index_type, 0));
14489 else
14490 parse_error_context (wfl_operator,
14491 "Incompatible type for %<[]%>. Can't convert %qs to %<int%>",
14492 lang_printable_name (index_type, 0));
14493 TREE_TYPE (node) = error_mark_node;
14494 error_found = 1;
14497 if (error_found)
14498 return error_mark_node;
14500 array_type = TYPE_ARRAY_ELEMENT (array_type);
14502 if (flag_emit_class_files)
14504 TREE_OPERAND (node, 0) = array;
14505 TREE_OPERAND (node, 1) = index;
14507 else
14508 node = build_java_arrayaccess (array, array_type, index);
14509 TREE_TYPE (node) = array_type;
14510 return node;
14513 /* 15.9 Array Creation Expressions */
14515 static tree
14516 build_newarray_node (tree type, tree dims, int extra_dims)
14518 tree node = build3 (NEW_ARRAY_EXPR, NULL_TREE, type,
14519 nreverse (dims),
14520 build_int_cst (NULL_TREE, extra_dims));
14521 return node;
14524 static tree
14525 patch_newarray (tree node)
14527 tree type = TREE_OPERAND (node, 0);
14528 tree dims = TREE_OPERAND (node, 1);
14529 tree cdim, array_type;
14530 int error_found = 0;
14531 int ndims = 0;
14532 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14534 /* Dimension types are verified. It's better for the types to be
14535 verified in order. */
14536 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14538 int dim_error = 0;
14539 tree dim = TREE_VALUE (cdim);
14541 /* Dim might have been saved during its evaluation */
14542 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14544 /* The type of each specified dimension must be an integral type. */
14545 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14546 dim_error = 1;
14548 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14549 promoted type must be int. */
14550 else
14552 dim = do_unary_numeric_promotion (dim);
14553 if (TREE_TYPE (dim) != int_type_node)
14554 dim_error = 1;
14557 /* Report errors on types here */
14558 if (dim_error)
14560 parse_error_context
14561 (TREE_PURPOSE (cdim),
14562 "Incompatible type for dimension in array creation expression. %s convert %qs to %<int%>",
14563 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14564 "Explicit cast needed to" : "Can't"),
14565 lang_printable_name (TREE_TYPE (dim), 0));
14566 error_found = 1;
14569 TREE_PURPOSE (cdim) = NULL_TREE;
14572 /* Resolve array base type if unresolved */
14573 if (!(type = resolve_type_during_patch (type)))
14574 error_found = 1;
14576 if (error_found)
14578 /* We don't want further evaluation of this bogus array creation
14579 operation */
14580 TREE_TYPE (node) = error_mark_node;
14581 return error_mark_node;
14584 /* Set array_type to the actual (promoted) array type of the result. */
14585 if (TREE_CODE (type) == RECORD_TYPE)
14586 type = build_pointer_type (type);
14587 while (--xdims >= 0)
14589 type = promote_type (build_java_array_type (type, -1));
14591 dims = nreverse (dims);
14592 array_type = type;
14593 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14595 type = array_type;
14596 array_type
14597 = build_java_array_type (type,
14598 TREE_CODE (cdim) == INTEGER_CST
14599 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14600 : -1);
14601 array_type = promote_type (array_type);
14603 dims = nreverse (dims);
14605 /* The node is transformed into a function call. Things are done
14606 differently according to the number of dimensions. If the number
14607 of dimension is equal to 1, then the nature of the base type
14608 (primitive or not) matters. */
14609 if (ndims == 1)
14610 return build_new_array (type, TREE_VALUE (dims));
14612 /* Can't reuse what's already written in expr.c because it uses the
14613 JVM stack representation. Provide a build_multianewarray. FIXME */
14614 return build3 (CALL_EXPR, array_type,
14615 build_address_of (soft_multianewarray_node),
14616 tree_cons (NULL_TREE,
14617 build_class_ref (TREE_TYPE (array_type)),
14618 tree_cons (NULL_TREE,
14619 build_int_cst (NULL_TREE, ndims),
14620 dims)),
14621 NULL_TREE);
14624 /* 10.6 Array initializer. */
14626 /* Build a wfl for array element that don't have one, so we can
14627 pin-point errors. */
14629 static tree
14630 maybe_build_array_element_wfl (tree node)
14632 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14634 /* FIXME - old code used "prev_lc.line" and "elc.prev_col */
14635 return build_expr_wfl (NULL_TREE,
14636 #ifdef USE_MAPPED_LOCATION
14637 input_location
14638 #else
14639 ctxp->filename,
14640 ctxp->lexer->token_start.line,
14641 ctxp->lexer->token_start.col
14642 #endif
14645 else
14646 return NULL_TREE;
14649 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14650 identification of initialized arrays easier to detect during walk
14651 and expansion. */
14653 static tree
14654 build_new_array_init (int location, tree values)
14656 tree constructor = build_constructor (NULL_TREE, values);
14657 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14658 EXPR_WFL_LINECOL (to_return) = location;
14659 return to_return;
14662 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14663 occurred. Otherwise return NODE after having set its type
14664 appropriately. */
14666 static tree
14667 patch_new_array_init (tree type, tree node)
14669 int error_seen = 0;
14670 tree current, element_type;
14671 HOST_WIDE_INT length;
14672 int all_constant = 1;
14673 tree init = TREE_OPERAND (node, 0);
14675 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14677 parse_error_context (node,
14678 "Invalid array initializer for non-array type %qs",
14679 lang_printable_name (type, 1));
14680 return error_mark_node;
14682 type = TREE_TYPE (type);
14683 element_type = TYPE_ARRAY_ELEMENT (type);
14685 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14687 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14688 current; length++, current = TREE_CHAIN (current))
14690 tree elt = TREE_VALUE (current);
14691 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14693 error_seen |= array_constructor_check_entry (element_type, current);
14694 elt = TREE_VALUE (current);
14695 /* When compiling to native code, STRING_CST is converted to
14696 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14697 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14698 all_constant = 0;
14700 else
14702 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14703 TREE_PURPOSE (current) = NULL_TREE;
14704 all_constant = 0;
14706 if (elt && TREE_CODE (elt) == TREE_LIST
14707 && TREE_VALUE (elt) == error_mark_node)
14708 error_seen = 1;
14711 if (error_seen)
14712 return error_mark_node;
14714 /* Create a new type. We can't reuse the one we have here by
14715 patching its dimension because it originally is of dimension -1
14716 hence reused by gcc. This would prevent triangular arrays. */
14717 type = build_java_array_type (element_type, length);
14718 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14719 TREE_TYPE (node) = promote_type (type);
14720 TREE_CONSTANT (init) = all_constant;
14721 TREE_INVARIANT (init) = all_constant;
14722 TREE_CONSTANT (node) = all_constant;
14723 TREE_INVARIANT (node) = all_constant;
14724 return node;
14727 /* Verify that one entry of the initializer element list can be
14728 assigned to the array base type. Report 1 if an error occurred, 0
14729 otherwise. */
14731 static int
14732 array_constructor_check_entry (tree type, tree entry)
14734 char *array_type_string = NULL; /* For error reports */
14735 tree value, type_value, new_value, wfl_value, patched;
14736 int error_seen = 0;
14738 new_value = NULL_TREE;
14739 wfl_value = TREE_VALUE (entry);
14741 value = java_complete_tree (TREE_VALUE (entry));
14742 /* patch_string return error_mark_node if arg is error_mark_node */
14743 if ((patched = patch_string (value)))
14744 value = patched;
14745 if (value == error_mark_node)
14746 return 1;
14748 type_value = TREE_TYPE (value);
14750 /* At anytime, try_builtin_assignconv can report a warning on
14751 constant overflow during narrowing. */
14752 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14753 new_value = try_builtin_assignconv (wfl_operator, type, value);
14754 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14755 type_value = promote_type (type);
14757 /* Check and report errors */
14758 if (!new_value)
14760 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14761 "Can't" : "Explicit cast needed to");
14762 if (!array_type_string)
14763 array_type_string = xstrdup (lang_printable_name (type, 1));
14764 parse_error_context
14765 (wfl_operator, "Incompatible type for array. %s convert %qs to %qs",
14766 msg, lang_printable_name (type_value, 1), array_type_string);
14767 error_seen = 1;
14770 if (new_value)
14771 TREE_VALUE (entry) = new_value;
14773 if (array_type_string)
14774 free (array_type_string);
14776 TREE_PURPOSE (entry) = NULL_TREE;
14777 return error_seen;
14780 static tree
14781 build_this (int location)
14783 tree node = build_wfl_node (this_identifier_node);
14784 TREE_SET_CODE (node, THIS_EXPR);
14785 EXPR_WFL_LINECOL (node) = location;
14786 return node;
14789 /* 14.15 The return statement. It builds a modify expression that
14790 assigns the returned value to the RESULT_DECL that hold the value
14791 to be returned. */
14793 static tree
14794 build_return (int location, tree op)
14796 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14797 EXPR_WFL_LINECOL (node) = location;
14798 node = build_debugable_stmt (location, node);
14799 return node;
14802 static tree
14803 patch_return (tree node)
14805 tree return_exp = TREE_OPERAND (node, 0);
14806 tree meth = current_function_decl;
14807 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14808 int error_found = 0;
14810 TREE_TYPE (node) = error_mark_node;
14811 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14813 /* It's invalid to have a return value within a function that is
14814 declared with the keyword void or that is a constructor */
14815 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14816 error_found = 1;
14818 /* It's invalid to use a return statement in a static block */
14819 if (DECL_CLINIT_P (current_function_decl))
14820 error_found = 1;
14822 /* It's invalid to have a no return value within a function that
14823 isn't declared with the keyword `void' */
14824 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14825 error_found = 2;
14827 if (DECL_INSTINIT_P (current_function_decl))
14828 error_found = 1;
14830 if (error_found)
14832 if (DECL_INSTINIT_P (current_function_decl))
14833 parse_error_context (wfl_operator,
14834 "%<return%> inside instance initializer");
14836 else if (DECL_CLINIT_P (current_function_decl))
14837 parse_error_context (wfl_operator,
14838 "%<return%> inside static initializer");
14840 else if (!DECL_CONSTRUCTOR_P (meth))
14842 char *t = xstrdup (lang_printable_name (mtype, 0));
14843 parse_error_context (wfl_operator,
14844 "%<return%> with%s value from %<%s %s%>",
14845 (error_found == 1 ? "" : "out"),
14846 t, lang_printable_name (meth, 2));
14847 free (t);
14849 else
14850 parse_error_context (wfl_operator,
14851 "%<return%> with value from constructor %qs",
14852 lang_printable_name (meth, 2));
14853 return error_mark_node;
14856 /* If we have a return_exp, build a modify expression and expand
14857 it. Note: at that point, the assignment is declared valid, but we
14858 may want to carry some more hacks */
14859 if (return_exp)
14861 tree exp = java_complete_tree (return_exp);
14862 tree modify, patched;
14864 if ((patched = patch_string (exp)))
14865 exp = patched;
14867 modify = build2 (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14868 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14869 modify = java_complete_tree (modify);
14871 if (modify != error_mark_node)
14873 TREE_SIDE_EFFECTS (modify) = 1;
14874 TREE_OPERAND (node, 0) = modify;
14876 else
14877 return error_mark_node;
14879 TREE_TYPE (node) = void_type_node;
14880 TREE_SIDE_EFFECTS (node) = 1;
14881 return node;
14884 /* 14.8 The if Statement */
14886 static tree
14887 build_if_else_statement (int location, tree expression, tree if_body,
14888 tree else_body)
14890 tree node;
14891 if (!else_body)
14892 else_body = build_java_empty_stmt ();
14893 node = build3 (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14894 EXPR_WFL_LINECOL (node) = location;
14895 node = build_debugable_stmt (location, node);
14896 return node;
14899 static tree
14900 patch_if_else_statement (tree node)
14902 tree expression = TREE_OPERAND (node, 0);
14903 int can_complete_normally
14904 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14905 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14907 TREE_TYPE (node) = error_mark_node;
14908 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14910 /* The type of expression must be boolean */
14911 if (TREE_TYPE (expression) != boolean_type_node
14912 && TREE_TYPE (expression) != promoted_boolean_type_node)
14914 parse_error_context
14915 (wfl_operator,
14916 "Incompatible type for %<if%>. Can't convert %qs to %<boolean%>",
14917 lang_printable_name (TREE_TYPE (expression), 0));
14918 return error_mark_node;
14921 TREE_TYPE (node) = void_type_node;
14922 TREE_SIDE_EFFECTS (node) = 1;
14923 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14924 return node;
14927 /* 14.6 Labeled Statements */
14929 /* Action taken when a labeled statement is parsed. a new
14930 LABELED_BLOCK_EXPR is created. No statement is attached to the
14931 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14933 static tree
14934 build_labeled_block (int location, tree label)
14936 tree label_name ;
14937 tree label_decl, node;
14938 if (label == NULL_TREE || label == continue_identifier_node)
14939 label_name = label;
14940 else
14942 label_name = merge_qualified_name (label_id, label);
14943 /* Issue an error if we try to reuse a label that was previously
14944 declared */
14945 if (IDENTIFIER_LOCAL_VALUE (label_name))
14947 EXPR_WFL_LINECOL (wfl_operator) = location;
14948 parse_error_context (wfl_operator,
14949 "Declaration of %qs shadows a previous label declaration",
14950 IDENTIFIER_POINTER (label));
14951 EXPR_WFL_LINECOL (wfl_operator) =
14952 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14953 parse_error_context (wfl_operator,
14954 "This is the location of the previous declaration of label %qs",
14955 IDENTIFIER_POINTER (label));
14956 java_error_count--;
14960 label_decl = create_label_decl (label_name);
14961 node = build2 (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14962 EXPR_WFL_LINECOL (node) = location;
14963 TREE_SIDE_EFFECTS (node) = 1;
14964 return node;
14967 /* A labeled statement LBE is attached a statement. */
14969 static tree
14970 finish_labeled_statement (tree lbe, /* Labeled block expr */
14971 tree statement)
14973 /* In anyways, tie the loop to its statement */
14974 LABELED_BLOCK_BODY (lbe) = statement;
14975 pop_labeled_block ();
14976 POP_LABELED_BLOCK ();
14977 return lbe;
14980 /* 14.10, 14.11, 14.12 Loop Statements */
14982 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14983 list. */
14985 static tree
14986 build_new_loop (tree loop_body)
14988 tree loop = build1 (LOOP_EXPR, NULL_TREE, loop_body);
14989 TREE_SIDE_EFFECTS (loop) = 1;
14990 PUSH_LOOP (loop);
14991 return loop;
14994 /* Create a loop body according to the following structure:
14995 COMPOUND_EXPR
14996 COMPOUND_EXPR (loop main body)
14997 EXIT_EXPR (this order is for while/for loops.
14998 LABELED_BLOCK_EXPR the order is reversed for do loops)
14999 LABEL_DECL (a continue occurring here branches at the
15000 BODY end of this labeled block)
15001 INCREMENT (if any)
15003 REVERSED, if nonzero, tells that the loop condition expr comes
15004 after the body, like in the do-while loop.
15006 To obtain a loop, the loop body structure described above is
15007 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15009 LABELED_BLOCK_EXPR
15010 LABEL_DECL (use this label to exit the loop)
15011 LOOP_EXPR
15012 <structure described above> */
15014 static tree
15015 build_loop_body (int location, tree condition, int reversed)
15017 tree first, second, body;
15019 condition = build1 (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15020 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15021 condition = build_debugable_stmt (location, condition);
15022 TREE_SIDE_EFFECTS (condition) = 1;
15024 body = build_labeled_block (0, continue_identifier_node);
15025 first = (reversed ? body : condition);
15026 second = (reversed ? condition : body);
15027 return build2 (COMPOUND_EXPR, NULL_TREE,
15028 build2 (COMPOUND_EXPR, NULL_TREE, first, second),
15029 build_java_empty_stmt ());
15032 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15033 their order) on the current loop. Unlink the current loop from the
15034 loop list. */
15036 static tree
15037 finish_loop_body (int location, tree condition, tree body, int reversed)
15039 tree to_return = ctxp->current_loop;
15040 tree loop_body = LOOP_EXPR_BODY (to_return);
15041 if (condition)
15043 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15044 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15045 The real EXIT_EXPR is one operand further. */
15046 EXPR_WFL_LINECOL (cnode) = location;
15047 if (TREE_CODE (cnode) == EXPR_WITH_FILE_LOCATION)
15049 cnode = EXPR_WFL_NODE (cnode);
15050 /* This one is for accurate error reports */
15051 EXPR_WFL_LINECOL (cnode) = location;
15053 TREE_OPERAND (cnode, 0) = condition;
15055 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15056 POP_LOOP ();
15057 return to_return;
15060 /* Tailored version of finish_loop_body for FOR loops, when FOR
15061 loops feature the condition part */
15063 static tree
15064 finish_for_loop (int location, tree condition, tree update, tree body)
15066 /* Put the condition and the loop body in place */
15067 tree loop = finish_loop_body (location, condition, body, 0);
15068 /* LOOP is the current loop which has been now popped of the loop
15069 stack. Mark the update block as reachable and install it. We do
15070 this because the (current interpretation of the) JLS requires
15071 that the update expression be considered reachable even if the
15072 for loop's body doesn't complete normally. */
15073 if (update != NULL_TREE && !IS_EMPTY_STMT (update))
15075 tree up2 = update;
15076 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15077 up2 = EXPR_WFL_NODE (up2);
15078 /* It is possible for the update expression to be an
15079 EXPR_WFL_NODE wrapping nothing. */
15080 if (up2 != NULL_TREE && !IS_EMPTY_STMT (up2))
15082 /* Try to detect constraint violations. These would be
15083 programming errors somewhere. */
15084 if (! EXPR_P (up2) || TREE_CODE (up2) == LOOP_EXPR)
15085 abort ();
15086 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15089 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15090 return loop;
15093 /* Try to find the loop a block might be related to. This comprises
15094 the case where the LOOP_EXPR is found as the second operand of a
15095 COMPOUND_EXPR, because the loop happens to have an initialization
15096 part, then expressed as the first operand of the COMPOUND_EXPR. If
15097 the search finds something, 1 is returned. Otherwise, 0 is
15098 returned. The search is assumed to start from a
15099 LABELED_BLOCK_EXPR's block. */
15101 static tree
15102 search_loop (tree statement)
15104 if (TREE_CODE (statement) == LOOP_EXPR)
15105 return statement;
15107 if (TREE_CODE (statement) == BLOCK)
15108 statement = BLOCK_SUBBLOCKS (statement);
15109 else
15110 return NULL_TREE;
15112 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15113 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15114 statement = TREE_OPERAND (statement, 1);
15116 return (TREE_CODE (statement) == LOOP_EXPR
15117 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15120 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15121 returned otherwise. */
15123 static int
15124 labeled_block_contains_loop_p (tree block, tree loop)
15126 if (!block)
15127 return 0;
15129 if (LABELED_BLOCK_BODY (block) == loop)
15130 return 1;
15132 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15133 return 1;
15135 return 0;
15138 /* If the loop isn't surrounded by a labeled statement, create one and
15139 insert LOOP as its body. */
15141 static tree
15142 patch_loop_statement (tree loop)
15144 tree loop_label;
15146 TREE_TYPE (loop) = void_type_node;
15147 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15148 return loop;
15150 loop_label = build_labeled_block (0, NULL_TREE);
15151 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15152 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15153 LABELED_BLOCK_BODY (loop_label) = loop;
15154 PUSH_LABELED_BLOCK (loop_label);
15155 return loop_label;
15158 /* 14.13, 14.14: break and continue Statements */
15160 /* Build a break or a continue statement. a null NAME indicates an
15161 unlabeled break/continue statement. */
15163 static tree
15164 build_bc_statement (int location, int is_break, tree name)
15166 tree break_continue, label_block_expr = NULL_TREE;
15168 if (name)
15170 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15171 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15172 /* Null means that we don't have a target for this named
15173 break/continue. In this case, we make the target to be the
15174 label name, so that the error can be reported accurately in
15175 patch_bc_statement. */
15176 label_block_expr = EXPR_WFL_NODE (name);
15178 /* Unlabeled break/continue will be handled during the
15179 break/continue patch operation */
15180 break_continue = build1 (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr);
15182 IS_BREAK_STMT_P (break_continue) = is_break;
15183 TREE_SIDE_EFFECTS (break_continue) = 1;
15184 EXPR_WFL_LINECOL (break_continue) = location;
15185 break_continue = build_debugable_stmt (location, break_continue);
15186 return break_continue;
15189 /* Verification of a break/continue statement. */
15191 static tree
15192 patch_bc_statement (tree node)
15194 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15195 tree labeled_block = ctxp->current_labeled_block;
15196 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15198 /* Having an identifier here means that the target is unknown. */
15199 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15201 parse_error_context (wfl_operator, "No label definition found for %qs",
15202 IDENTIFIER_POINTER (bc_label));
15203 return error_mark_node;
15205 if (! IS_BREAK_STMT_P (node))
15207 /* It's a continue statement. */
15208 for (;; labeled_block = TREE_CHAIN (labeled_block))
15210 if (labeled_block == NULL_TREE)
15212 if (bc_label == NULL_TREE)
15213 parse_error_context (wfl_operator,
15214 "%<continue%> must be in loop");
15215 else
15216 parse_error_context
15217 (wfl_operator, "continue label %qs does not name a loop",
15218 IDENTIFIER_POINTER (bc_label));
15219 return error_mark_node;
15221 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15222 == continue_identifier_node)
15223 && (bc_label == NULL_TREE
15224 || TREE_CHAIN (labeled_block) == bc_label))
15226 bc_label = labeled_block;
15227 break;
15231 else if (!bc_label)
15233 for (;; labeled_block = TREE_CHAIN (labeled_block))
15235 if (labeled_block == NULL_TREE)
15237 parse_error_context (wfl_operator,
15238 "%<break%> must be in loop or switch");
15239 return error_mark_node;
15241 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15242 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15243 || search_loop (target_stmt))
15245 bc_label = labeled_block;
15246 break;
15251 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15252 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15254 /* Our break/continue don't return values. */
15255 TREE_TYPE (node) = void_type_node;
15256 /* Encapsulate the break within a compound statement so that it's
15257 expanded all the times by expand_expr (and not clobbered
15258 sometimes, like after a if statement) */
15259 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15260 TREE_SIDE_EFFECTS (node) = 1;
15261 return node;
15264 /* Process the exit expression belonging to a loop. Its type must be
15265 boolean. */
15267 static tree
15268 patch_exit_expr (tree node)
15270 tree expression = TREE_OPERAND (node, 0);
15271 TREE_TYPE (node) = error_mark_node;
15272 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15274 /* The type of expression must be boolean */
15275 if (TREE_TYPE (expression) != boolean_type_node)
15277 parse_error_context
15278 (wfl_operator,
15279 "Incompatible type for loop conditional. Can't convert %qs to %<boolean%>",
15280 lang_printable_name (TREE_TYPE (expression), 0));
15281 return error_mark_node;
15283 /* Now we know things are allright, invert the condition, fold and
15284 return */
15285 TREE_OPERAND (node, 0) =
15286 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15288 if (! integer_zerop (TREE_OPERAND (node, 0))
15289 && ctxp->current_loop != NULL_TREE
15290 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15291 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15292 if (! integer_onep (TREE_OPERAND (node, 0)))
15293 CAN_COMPLETE_NORMALLY (node) = 1;
15296 TREE_TYPE (node) = void_type_node;
15297 return node;
15300 /* 14.9 Switch statement */
15302 static tree
15303 patch_switch_statement (tree node)
15305 tree se = TREE_OPERAND (node, 0), se_type;
15306 tree save, iter;
15308 /* Complete the switch expression */
15309 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15310 se_type = TREE_TYPE (se);
15311 /* The type of the switch expression must be char, byte, short or
15312 int */
15313 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15315 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15316 parse_error_context (wfl_operator,
15317 "Incompatible type for %<switch%>. Can't convert %qs to %<int%>",
15318 lang_printable_name (se_type, 0));
15319 /* This is what java_complete_tree will check */
15320 TREE_OPERAND (node, 0) = error_mark_node;
15321 return error_mark_node;
15324 /* Save and restore the outer case label list. */
15325 save = case_label_list;
15326 case_label_list = NULL_TREE;
15328 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15330 /* See if we've found a duplicate label. We can't leave this until
15331 code generation, because in `--syntax-only' and `-C' modes we
15332 don't do ordinary code generation. */
15333 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15335 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15336 tree subiter;
15337 for (subiter = TREE_CHAIN (iter);
15338 subiter != NULL_TREE;
15339 subiter = TREE_CHAIN (subiter))
15341 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15342 if (val == subval)
15344 EXPR_WFL_LINECOL (wfl_operator)
15345 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15346 /* The case_label_list is in reverse order, so print the
15347 outer label first. */
15348 parse_error_context (wfl_operator, "duplicate case label: %<"
15349 HOST_WIDE_INT_PRINT_DEC "%>", subval);
15350 EXPR_WFL_LINECOL (wfl_operator)
15351 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15352 parse_error_context (wfl_operator, "original label is here");
15354 break;
15359 case_label_list = save;
15361 /* Ready to return */
15362 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15364 TREE_TYPE (node) = error_mark_node;
15365 return error_mark_node;
15367 TREE_TYPE (node) = void_type_node;
15368 TREE_SIDE_EFFECTS (node) = 1;
15369 CAN_COMPLETE_NORMALLY (node)
15370 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15371 || ! SWITCH_HAS_DEFAULT (node);
15372 return node;
15375 /* Assertions. */
15377 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15378 might be NULL_TREE. */
15379 static tree
15380 build_assertion (
15381 #ifdef USE_MAPPED_LOCATION
15382 source_location location,
15383 #else
15384 int location,
15385 #endif
15386 tree condition, tree value)
15388 tree node;
15389 tree klass = GET_CPC ();
15391 if (! enable_assertions (klass))
15393 condition = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15394 boolean_false_node, condition);
15395 if (value == NULL_TREE)
15396 value = build_java_empty_stmt ();
15397 return build_if_else_statement (location, condition,
15398 value, NULL_TREE);
15401 if (! CLASS_USES_ASSERTIONS (klass))
15403 tree field, classdollar, id, call;
15404 tree class_type = TREE_TYPE (klass);
15406 field = add_field (class_type,
15407 get_identifier ("$assertionsDisabled"),
15408 boolean_type_node,
15409 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15410 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15411 FIELD_SYNTHETIC (field) = 1;
15413 classdollar = build_incomplete_class_ref (location, class_type);
15415 /* Call CLASS.desiredAssertionStatus(). */
15416 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15417 call = build3 (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15418 call = make_qualified_primary (classdollar, call, location);
15419 TREE_SIDE_EFFECTS (call) = 1;
15421 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15422 seem odd, but we do it to generate code identical to that of
15423 the JDK. */
15424 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15425 TREE_SIDE_EFFECTS (call) = 1;
15426 DECL_INITIAL (field) = call;
15428 /* Record the initializer in the initializer statement list. */
15429 call = build2 (MODIFY_EXPR, NULL_TREE, field, call);
15430 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15431 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15432 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15434 CLASS_USES_ASSERTIONS (klass) = 1;
15437 if (value != NULL_TREE)
15438 value = tree_cons (NULL_TREE, value, NULL_TREE);
15440 node = build_wfl_node (get_identifier ("java"));
15441 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15442 location);
15443 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15444 location);
15446 node = build3 (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15447 TREE_SIDE_EFFECTS (node) = 1;
15448 /* It is too early to use BUILD_THROW. */
15449 node = build1 (THROW_EXPR, NULL_TREE, node);
15450 TREE_SIDE_EFFECTS (node) = 1;
15452 /* We invert the condition; if we just put NODE as the `else' part
15453 then we generate weird-looking bytecode. */
15454 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15455 /* Check $assertionsDisabled. */
15456 condition
15457 = build2 (TRUTH_ANDIF_EXPR, NULL_TREE,
15458 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15459 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15460 condition);
15461 node = build_if_else_statement (location, condition, node, NULL_TREE);
15462 return node;
15465 /* 14.18 The try/catch statements */
15467 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15468 catches TYPE and executes CATCH_STMTS. */
15470 static tree
15471 encapsulate_with_try_catch (int location, tree type_or_name, tree try_stmts,
15472 tree catch_stmts)
15474 tree try_block, catch_clause_param, catch_block, catch;
15476 /* First build a try block */
15477 try_block = build_expr_block (try_stmts, NULL_TREE);
15479 /* Build a catch block: we need a catch clause parameter */
15480 if (TREE_CODE (type_or_name) == EXPR_WITH_FILE_LOCATION)
15482 tree catch_type = obtain_incomplete_type (type_or_name);
15483 jdep *dep;
15484 catch_clause_param = build_decl (VAR_DECL, wpv_id, catch_type);
15485 register_incomplete_type (JDEP_VARIABLE, type_or_name,
15486 catch_clause_param, catch_type);
15487 dep = CLASSD_LAST (ctxp->classd_list);
15488 JDEP_GET_PATCH (dep) = &TREE_TYPE (catch_clause_param);
15490 else
15491 catch_clause_param = build_decl (VAR_DECL, wpv_id,
15492 build_pointer_type (type_or_name));
15494 /* And a block */
15495 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15497 /* Initialize the variable and store in the block */
15498 catch = build2 (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15499 build0 (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15500 add_stmt_to_block (catch_block, NULL_TREE, catch);
15502 /* Add the catch statements */
15503 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15505 /* Now we can build a JAVA_CATCH_EXPR */
15506 catch_block = build1 (JAVA_CATCH_EXPR, NULL_TREE, catch_block);
15508 return build_try_statement (location, try_block, catch_block);
15511 static tree
15512 build_try_statement (int location, tree try_block, tree catches)
15514 tree node = build2 (TRY_EXPR, NULL_TREE, try_block, catches);
15515 EXPR_WFL_LINECOL (node) = location;
15516 return node;
15519 static tree
15520 build_try_finally_statement (int location, tree try_block, tree finally)
15522 tree node = build2 (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15523 EXPR_WFL_LINECOL (node) = location;
15524 return node;
15527 static tree
15528 patch_try_statement (tree node)
15530 int error_found = 0;
15531 tree try = TREE_OPERAND (node, 0);
15532 /* Exception handlers are considered in left to right order */
15533 tree catch = nreverse (TREE_OPERAND (node, 1));
15534 tree current, caught_type_list = NULL_TREE;
15536 /* Check catch clauses, if any. Every time we find an error, we try
15537 to process the next catch clause. We process the catch clause before
15538 the try block so that when processing the try block we can check thrown
15539 exceptions against the caught type list. */
15540 for (current = catch; current; current = TREE_CHAIN (current))
15542 tree carg_decl, carg_type;
15543 tree sub_current, catch_block, catch_clause;
15544 int unreachable;
15546 /* At this point, the structure of the catch clause is
15547 JAVA_CATCH_EXPR (catch node)
15548 BLOCK (with the decl of the parameter)
15549 COMPOUND_EXPR
15550 MODIFY_EXPR (assignment of the catch parameter)
15551 BLOCK (catch clause block)
15553 catch_clause = TREE_OPERAND (current, 0);
15554 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15555 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15557 /* Catch clauses can't have more than one parameter declared,
15558 but it's already enforced by the grammar. Make sure that the
15559 only parameter of the clause statement in of class Throwable
15560 or a subclass of Throwable, but that was done earlier. The
15561 catch clause parameter type has also been resolved. */
15563 /* Just make sure that the catch clause parameter type inherits
15564 from java.lang.Throwable */
15565 if (!inherits_from_p (carg_type, throwable_type_node))
15567 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15568 parse_error_context (wfl_operator,
15569 "Can't catch class %qs. Catch clause parameter type must be a subclass of class %<java.lang.Throwable%>",
15570 lang_printable_name (carg_type, 0));
15571 error_found = 1;
15572 continue;
15575 /* Partial check for unreachable catch statement: The catch
15576 clause is reachable iff is no earlier catch block A in
15577 the try statement such that the type of the catch
15578 clause's parameter is the same as or a subclass of the
15579 type of A's parameter */
15580 unreachable = 0;
15581 for (sub_current = catch;
15582 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15584 tree sub_catch_clause, decl;
15585 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15586 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15588 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15590 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15591 parse_error_context
15592 (wfl_operator,
15593 "%<catch%> not reached because of the catch clause at line %d",
15594 EXPR_WFL_LINENO (sub_current));
15595 unreachable = error_found = 1;
15596 break;
15599 /* Complete the catch clause block */
15600 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15601 if (catch_block == error_mark_node)
15603 error_found = 1;
15604 continue;
15606 if (CAN_COMPLETE_NORMALLY (catch_block))
15607 CAN_COMPLETE_NORMALLY (node) = 1;
15608 TREE_OPERAND (current, 0) = catch_block;
15610 if (unreachable)
15611 continue;
15613 /* Things to do here: the exception must be thrown */
15615 /* Link this type to the caught type list */
15616 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15619 PUSH_EXCEPTIONS (caught_type_list);
15620 if ((try = java_complete_tree (try)) == error_mark_node)
15621 error_found = 1;
15622 if (CAN_COMPLETE_NORMALLY (try))
15623 CAN_COMPLETE_NORMALLY (node) = 1;
15624 POP_EXCEPTIONS ();
15626 /* Verification ends here */
15627 if (error_found)
15628 return error_mark_node;
15630 TREE_OPERAND (node, 0) = try;
15631 TREE_OPERAND (node, 1) = catch;
15632 TREE_TYPE (node) = void_type_node;
15633 return node;
15636 /* 14.17 The synchronized Statement */
15638 static tree
15639 patch_synchronized_statement (tree node, tree wfl_op1)
15641 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15642 tree block = TREE_OPERAND (node, 1);
15644 tree tmp, enter, exit, expr_decl, assignment;
15646 if (expr == error_mark_node)
15648 block = java_complete_tree (block);
15649 return expr;
15652 /* We might be trying to synchronize on a STRING_CST */
15653 if ((tmp = patch_string (expr)))
15654 expr = tmp;
15656 /* The TYPE of expr must be a reference type */
15657 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15659 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15660 parse_error_context (wfl_operator, "Incompatible type for %<synchronized%>. Can't convert %qs to %<java.lang.Object%>",
15661 lang_printable_name (TREE_TYPE (expr), 0));
15662 return error_mark_node;
15665 /* Generate a try-finally for the synchronized statement, except
15666 that the handler that catches all throw exception calls
15667 _Jv_MonitorExit and then rethrow the exception.
15668 The synchronized statement is then implemented as:
15671 _Jv_MonitorEnter (expression)
15672 synchronized_block
15673 _Jv_MonitorExit (expression)
15675 CATCH_ALL
15677 e = _Jv_exception_info ();
15678 _Jv_MonitorExit (expression)
15679 Throw (e);
15680 } */
15682 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15683 BUILD_MONITOR_ENTER (enter, expr_decl);
15684 BUILD_MONITOR_EXIT (exit, expr_decl);
15685 CAN_COMPLETE_NORMALLY (enter) = 1;
15686 CAN_COMPLETE_NORMALLY (exit) = 1;
15687 assignment = build2 (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15688 TREE_SIDE_EFFECTS (assignment) = 1;
15689 node = build2 (COMPOUND_EXPR, NULL_TREE,
15690 build2 (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15691 build2 (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15692 node = build_expr_block (node, expr_decl);
15694 return java_complete_tree (node);
15697 /* 14.16 The throw Statement */
15699 static tree
15700 patch_throw_statement (tree node, tree wfl_op1)
15702 tree expr = TREE_OPERAND (node, 0);
15703 tree type = TREE_TYPE (expr);
15704 int unchecked_ok = 0, tryblock_throws_ok = 0;
15706 /* Thrown expression must be assignable to java.lang.Throwable */
15707 if (!try_reference_assignconv (throwable_type_node, expr))
15709 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15710 parse_error_context (wfl_operator,
15711 "Can't throw %qs; it must be a subclass of class %<java.lang.Throwable%>",
15712 lang_printable_name (type, 0));
15713 /* If the thrown expression was a reference, we further the
15714 compile-time check. */
15715 if (!JREFERENCE_TYPE_P (type))
15716 return error_mark_node;
15719 /* At least one of the following must be true */
15721 /* The type of the throw expression is a not checked exception,
15722 i.e. is a unchecked expression. */
15723 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15725 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15726 /* An instance can't throw a checked exception unless that exception
15727 is explicitly declared in the `throws' clause of each
15728 constructor. This doesn't apply to anonymous classes, since they
15729 don't have declared constructors. */
15730 if (!unchecked_ok
15731 && DECL_INSTINIT_P (current_function_decl)
15732 && !ANONYMOUS_CLASS_P (current_class))
15734 tree current;
15735 for (current = TYPE_METHODS (current_class); current;
15736 current = TREE_CHAIN (current))
15737 if (DECL_CONSTRUCTOR_P (current)
15738 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15740 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)",
15741 lang_printable_name (TREE_TYPE (expr), 0));
15742 return error_mark_node;
15746 /* Throw is contained in a try statement and at least one catch
15747 clause can receive the thrown expression or the current method is
15748 declared to throw such an exception. Or, the throw statement is
15749 contained in a method or constructor declaration and the type of
15750 the Expression is assignable to at least one type listed in the
15751 throws clause the declaration. */
15752 if (!unchecked_ok)
15753 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15754 if (!(unchecked_ok || tryblock_throws_ok))
15756 /* If there is a surrounding try block that has no matching
15757 clatch clause, report it first. A surrounding try block exits
15758 only if there is something after the list of checked
15759 exception thrown by the current function (if any). */
15760 if (IN_TRY_BLOCK_P ())
15761 parse_error_context (wfl_operator, "Checked exception %qs can't be caught by any of the catch clause(s) of the surrounding %<try%> block",
15762 lang_printable_name (type, 0));
15763 /* If we have no surrounding try statement and the method doesn't have
15764 any throws, report it now. FIXME */
15766 /* We report that the exception can't be throw from a try block
15767 in all circumstances but when the `throw' is inside a static
15768 block. */
15769 else if (!EXCEPTIONS_P (currently_caught_type_list)
15770 && !tryblock_throws_ok)
15772 if (DECL_CLINIT_P (current_function_decl))
15773 parse_error_context (wfl_operator,
15774 "Checked exception %qs can't be thrown in initializer",
15775 lang_printable_name (type, 0));
15776 else
15777 parse_error_context (wfl_operator,
15778 "Checked exception %qs isn't thrown from a %<try%> block",
15779 lang_printable_name (type, 0));
15781 /* Otherwise, the current method doesn't have the appropriate
15782 throws declaration */
15783 else
15784 parse_error_context (wfl_operator, "Checked exception %qs doesn't match any of current method's %<throws%> declaration(s)",
15785 lang_printable_name (type, 0));
15786 return error_mark_node;
15789 if (! flag_emit_class_files)
15790 BUILD_THROW (node, expr);
15792 return node;
15795 /* Check that exception said to be thrown by method DECL can be
15796 effectively caught from where DECL is invoked. THIS_EXPR is the
15797 expression that computes `this' for the method call. */
15798 static void
15799 check_thrown_exceptions (
15800 #ifdef USE_MAPPED_LOCATION
15801 source_location location,
15802 #else
15804 int location,
15805 #endif
15806 tree decl, tree this_expr)
15808 tree throws;
15809 int is_array_call = 0;
15811 /* Skip check within generated methods, such as access$<n>. */
15812 if (OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (current_function_decl)))
15813 return;
15815 if (this_expr != NULL_TREE
15816 && TREE_CODE (TREE_TYPE (this_expr)) == POINTER_TYPE
15817 && TYPE_ARRAY_P (TREE_TYPE (TREE_TYPE (this_expr))))
15818 is_array_call = 1;
15820 /* For all the unchecked exceptions thrown by DECL. */
15821 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15822 throws = TREE_CHAIN (throws))
15823 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15825 /* Suppress errors about cloning arrays. */
15826 if (is_array_call && DECL_NAME (decl) == get_identifier ("clone"))
15827 continue;
15829 #ifdef USE_MAPPED_LOCATION
15830 SET_EXPR_LOCATION (wfl_operator, location);
15831 #else
15832 EXPR_WFL_LINECOL (wfl_operator) = location;
15833 #endif
15834 if (DECL_FINIT_P (current_function_decl))
15835 parse_error_context
15836 (wfl_operator, "Exception %qs can't be thrown in initializer",
15837 lang_printable_name (TREE_VALUE (throws), 0));
15838 else
15840 parse_error_context
15841 (wfl_operator, "Exception %qs must be caught, or it must be declared in the %<throws%> clause of %qs",
15842 lang_printable_name (TREE_VALUE (throws), 0),
15843 (DECL_INIT_P (current_function_decl) ?
15844 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15845 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15850 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15851 try-catch blocks, OR is listed in the `throws' clause of the
15852 current method. */
15854 static int
15855 check_thrown_exceptions_do (tree exception)
15857 tree list = currently_caught_type_list;
15858 resolve_and_layout (exception, NULL_TREE);
15859 /* First, all the nested try-catch-finally at that stage. The
15860 last element contains `throws' clause exceptions, if any. */
15861 if (IS_UNCHECKED_EXCEPTION_P (exception))
15862 return 1;
15863 while (list)
15865 tree caught;
15866 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15867 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15868 return 1;
15869 list = TREE_CHAIN (list);
15871 return 0;
15874 static void
15875 purge_unchecked_exceptions (tree mdecl)
15877 tree throws = DECL_FUNCTION_THROWS (mdecl);
15878 tree new = NULL_TREE;
15880 while (throws)
15882 tree next = TREE_CHAIN (throws);
15883 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15885 TREE_CHAIN (throws) = new;
15886 new = throws;
15888 throws = next;
15890 /* List is inverted here, but it doesn't matter */
15891 DECL_FUNCTION_THROWS (mdecl) = new;
15894 /* This function goes over all of CLASS_TYPE ctors and checks whether
15895 each of them features at least one unchecked exception in its
15896 `throws' clause. If it's the case, it returns `true', `false'
15897 otherwise. */
15899 static bool
15900 ctors_unchecked_throws_clause_p (tree class_type)
15902 tree current;
15904 for (current = TYPE_METHODS (class_type); current;
15905 current = TREE_CHAIN (current))
15907 bool ctu = false; /* Ctor Throws Unchecked */
15908 if (DECL_CONSTRUCTOR_P (current))
15910 tree throws;
15911 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15912 throws = TREE_CHAIN (throws))
15913 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15914 ctu = true;
15916 /* We return false as we found one ctor that is unfit. */
15917 if (!ctu && DECL_CONSTRUCTOR_P (current))
15918 return false;
15920 /* All ctors feature at least one unchecked exception in their
15921 `throws' clause. */
15922 return true;
15925 /* 15.24 Conditional Operator ?: */
15927 static tree
15928 patch_conditional_expr (tree node, tree wfl_cond, tree wfl_op1)
15930 tree cond = TREE_OPERAND (node, 0);
15931 tree op1 = TREE_OPERAND (node, 1);
15932 tree op2 = TREE_OPERAND (node, 2);
15933 tree resulting_type = NULL_TREE;
15934 tree t1, t2, patched;
15935 int error_found = 0;
15937 /* The condition and operands of ?: might be StringBuffers crafted
15938 as a result of a string concatenation. Obtain decent ones here. */
15939 if ((patched = patch_string (cond)))
15940 TREE_OPERAND (node, 0) = cond = patched;
15941 if ((patched = patch_string (op1)))
15942 TREE_OPERAND (node, 1) = op1 = patched;
15943 if ((patched = patch_string (op2)))
15944 TREE_OPERAND (node, 2) = op2 = patched;
15946 t1 = TREE_TYPE (op1);
15947 t2 = TREE_TYPE (op2);
15949 /* The first expression must be a boolean */
15950 if (TREE_TYPE (cond) != boolean_type_node)
15952 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15953 parse_error_context (wfl_operator,
15954 "Incompatible type for %<?:%>. Can't convert %qs to %<boolean%>",
15955 lang_printable_name (TREE_TYPE (cond), 0));
15956 error_found = 1;
15959 /* Second and third can be numeric, boolean (i.e. primitive),
15960 references or null. Anything else results in an error */
15961 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15962 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15963 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15964 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15965 error_found = 1;
15967 /* Determine the type of the conditional expression. Same types are
15968 easy to deal with */
15969 else if (t1 == t2)
15970 resulting_type = t1;
15972 /* There are different rules for numeric types */
15973 else if (JNUMERIC_TYPE_P (t1))
15975 /* if byte/short found, the resulting type is short */
15976 if ((t1 == byte_type_node && t2 == short_type_node)
15977 || (t1 == short_type_node && t2 == byte_type_node))
15978 resulting_type = short_type_node;
15980 /* If t1 is a constant int and t2 is of type byte, short or char
15981 and t1's value fits in t2, then the resulting type is t2 */
15982 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15983 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15984 resulting_type = t2;
15986 /* If t2 is a constant int and t1 is of type byte, short or char
15987 and t2's value fits in t1, then the resulting type is t1 */
15988 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15989 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15990 resulting_type = t1;
15992 /* Otherwise, binary numeric promotion is applied and the
15993 resulting type is the promoted type of operand 1 and 2 */
15994 else
15995 resulting_type = binary_numeric_promotion (t1, t2,
15996 &TREE_OPERAND (node, 1),
15997 &TREE_OPERAND (node, 2));
16000 /* Cases of a reference and a null type */
16001 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16002 resulting_type = t1;
16004 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16005 resulting_type = t2;
16007 /* Last case: different reference types. If a type can be converted
16008 into the other one by assignment conversion, the latter
16009 determines the type of the expression */
16010 else if ((resulting_type = try_reference_assignconv (t1, op2)))
16011 resulting_type = promote_type (t1);
16013 else if ((resulting_type = try_reference_assignconv (t2, op1)))
16014 resulting_type = promote_type (t2);
16016 /* If we don't have any resulting type, we're in trouble */
16017 if (!resulting_type)
16019 char *t = xstrdup (lang_printable_name (t1, 0));
16020 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16021 parse_error_context (wfl_operator,
16022 "Incompatible type for %<?:%>. Can't convert %qs to %qs",
16023 t, lang_printable_name (t2, 0));
16024 free (t);
16025 error_found = 1;
16028 if (error_found)
16030 TREE_TYPE (node) = error_mark_node;
16031 return error_mark_node;
16034 TREE_TYPE (node) = resulting_type;
16035 TREE_SET_CODE (node, COND_EXPR);
16036 CAN_COMPLETE_NORMALLY (node) = 1;
16037 return node;
16040 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16042 static tree
16043 maybe_build_class_init_for_field (tree decl, tree expr)
16045 tree clas = DECL_CONTEXT (decl);
16046 if (flag_emit_class_files)
16047 return expr;
16049 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16050 && FIELD_FINAL (decl))
16052 tree init = DECL_INITIAL (decl);
16053 if (init != NULL_TREE)
16054 init = fold_constant_for_init (init, decl);
16055 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16056 return expr;
16059 return build_class_init (clas, expr);
16062 /* Try to constant fold NODE.
16063 If NODE is not a constant expression, return NULL_EXPR.
16064 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16066 static tree
16067 fold_constant_for_init (tree node, tree context)
16069 tree op0, op1, val;
16070 enum tree_code code = TREE_CODE (node);
16072 switch (code)
16074 case INTEGER_CST:
16075 if (node == null_pointer_node)
16076 return NULL_TREE;
16077 case STRING_CST:
16078 case REAL_CST:
16079 return node;
16081 case PLUS_EXPR:
16082 case MINUS_EXPR:
16083 case MULT_EXPR:
16084 case TRUNC_MOD_EXPR:
16085 case RDIV_EXPR:
16086 case LSHIFT_EXPR:
16087 case RSHIFT_EXPR:
16088 case URSHIFT_EXPR:
16089 case BIT_AND_EXPR:
16090 case BIT_XOR_EXPR:
16091 case BIT_IOR_EXPR:
16092 case TRUTH_ANDIF_EXPR:
16093 case TRUTH_ORIF_EXPR:
16094 case EQ_EXPR:
16095 case NE_EXPR:
16096 case GT_EXPR:
16097 case GE_EXPR:
16098 case LT_EXPR:
16099 case LE_EXPR:
16100 op0 = TREE_OPERAND (node, 0);
16101 op1 = TREE_OPERAND (node, 1);
16102 val = fold_constant_for_init (op0, context);
16103 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16104 return NULL_TREE;
16105 TREE_OPERAND (node, 0) = val;
16106 val = fold_constant_for_init (op1, context);
16107 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16108 return NULL_TREE;
16109 TREE_OPERAND (node, 1) = val;
16110 return patch_binop (node, op0, op1);
16112 case UNARY_PLUS_EXPR:
16113 case NEGATE_EXPR:
16114 case TRUTH_NOT_EXPR:
16115 case BIT_NOT_EXPR:
16116 case CONVERT_EXPR:
16117 op0 = TREE_OPERAND (node, 0);
16118 val = fold_constant_for_init (op0, context);
16119 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16120 return NULL_TREE;
16121 TREE_OPERAND (node, 0) = val;
16122 val = patch_unaryop (node, op0);
16123 if (! TREE_CONSTANT (val))
16124 return NULL_TREE;
16125 return val;
16127 break;
16129 case COND_EXPR:
16130 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16131 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16132 return NULL_TREE;
16133 TREE_OPERAND (node, 0) = val;
16134 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16135 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16136 return NULL_TREE;
16137 TREE_OPERAND (node, 1) = val;
16138 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16139 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16140 return NULL_TREE;
16141 TREE_OPERAND (node, 2) = val;
16142 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16143 : TREE_OPERAND (node, 2);
16145 case VAR_DECL:
16146 case FIELD_DECL:
16147 if (! FIELD_FINAL (node)
16148 || DECL_INITIAL (node) == NULL_TREE)
16149 return NULL_TREE;
16150 val = DECL_INITIAL (node);
16151 /* Guard against infinite recursion. */
16152 DECL_INITIAL (node) = NULL_TREE;
16153 val = fold_constant_for_init (val, node);
16154 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16155 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16156 DECL_INITIAL (node) = val;
16157 return val;
16159 case EXPR_WITH_FILE_LOCATION:
16160 /* Compare java_complete_tree and resolve_expression_name. */
16161 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16162 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16164 tree name = EXPR_WFL_NODE (node);
16165 tree decl;
16166 if (PRIMARY_P (node))
16167 return NULL_TREE;
16168 else if (! QUALIFIED_P (name))
16170 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16171 if (decl == NULL_TREE
16172 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16173 return NULL_TREE;
16174 return fold_constant_for_init (decl, decl);
16176 else
16178 tree r = NULL_TREE;
16179 /* Install the proper context for the field resolution. */
16180 tree saved_current_class = current_class;
16181 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16182 current_class = DECL_CONTEXT (context);
16183 qualify_ambiguous_name (node);
16184 r = resolve_field_access (node, &decl, NULL);
16185 /* Restore prior context. */
16186 current_class = saved_current_class;
16187 if (r != error_mark_node && decl != NULL_TREE)
16188 return fold_constant_for_init (decl, decl);
16189 return NULL_TREE;
16192 else
16194 op0 = TREE_OPERAND (node, 0);
16195 val = fold_constant_for_init (op0, context);
16196 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16197 return NULL_TREE;
16198 TREE_OPERAND (node, 0) = val;
16199 return val;
16202 #ifdef USE_COMPONENT_REF
16203 case IDENTIFIER:
16204 case COMPONENT_REF:
16206 #endif
16208 default:
16209 return NULL_TREE;
16213 #ifdef USE_COMPONENT_REF
16214 /* Context is 'T' for TypeName, 'P' for PackageName,
16215 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16217 tree
16218 resolve_simple_name (tree name, int context)
16222 tree
16223 resolve_qualified_name (tree name, int context)
16226 #endif
16228 void
16229 init_src_parse (void)
16231 /* Sanity check; we've been bit by this before. */
16232 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16233 abort ();
16238 /* This section deals with the functions that are called when tables
16239 recording class initialization information are traversed. */
16241 /* This function is called for each class that is known definitely
16242 initialized when a given static method was called. This function
16243 augments a compound expression (INFO) storing all assignment to
16244 initialized static class flags if a flag already existed, otherwise
16245 a new one is created. */
16247 static int
16248 emit_test_initialization (void **entry_p, void *info)
16250 tree l = (tree) info;
16251 tree decl, init;
16252 tree key = (tree) *entry_p;
16253 tree *ite;
16254 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16256 /* If we haven't found a flag and we're dealing with self registered
16257 with current_function_decl, then don't do anything. Self is
16258 always added as definitely initialized but this information is
16259 valid only if used outside the current function. */
16260 if (current_function_decl == TREE_PURPOSE (l)
16261 && java_treetreehash_find (cf_ht, key) == NULL)
16262 return true;
16264 ite = java_treetreehash_new (cf_ht, key);
16266 /* If we don't have a variable, create one and install it. */
16267 if (*ite == NULL)
16269 tree block;
16271 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16272 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16273 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16274 DECL_CONTEXT (decl) = current_function_decl;
16275 DECL_INITIAL (decl) = boolean_true_node;
16276 /* Don't emit any symbolic debugging info for this decl. */
16277 DECL_IGNORED_P (decl) = 1;
16279 /* The trick is to find the right context for it. */
16280 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16281 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16282 BLOCK_EXPR_DECLS (block) = decl;
16283 *ite = decl;
16285 else
16286 decl = *ite;
16288 /* Now simply augment the compound that holds all the assignments
16289 pertaining to this method invocation. */
16290 init = build2 (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16291 TREE_SIDE_EFFECTS (init) = 1;
16292 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16293 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16295 return true;
16298 #ifdef __XGETTEXT__
16299 /* Depending on the version of Bison used to compile this grammar,
16300 it may issue generic diagnostics spelled "syntax error" or
16301 "parse error". To prevent this from changing the translation
16302 template randomly, we list all the variants of this particular
16303 diagnostic here. Translators: there is no fine distinction
16304 between diagnostics with "syntax error" in them, and diagnostics
16305 with "parse error" in them. It's okay to give them both the same
16306 translation. */
16307 const char d1[] = N_("syntax error");
16308 const char d2[] = N_("parse error");
16309 const char d3[] = N_("syntax error; also virtual memory exhausted");
16310 const char d4[] = N_("parse error; also virtual memory exhausted");
16311 const char d5[] = N_("syntax error: cannot back up");
16312 const char d6[] = N_("parse error: cannot back up");
16313 #endif
16315 #include "gt-java-parse.h"
16316 #include "gtype-java.h"