]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
parse.h (JDEP_SOLV): Removed.
[gcc.git] / gcc / java / parse.y
CommitLineData
e04a16fb
AG
1/* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
dc08e603 3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
e04a16fb
AG
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23Java and all Java-based marks are trademarks or registered trademarks
24of Sun Microsystems, Inc. in the United States and other countries.
25The Free Software Foundation is independent of Sun Microsystems, Inc. */
26
27/* This file parses java source code and issues a tree node image
28suitable for code generation (byte code and targeted CPU assembly
29language).
30
31The grammar conforms to the Java grammar described in "The Java(TM)
32Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
331996, ISBN 0-201-63451-1"
34
35The following modifications were brought to the original grammar:
36
37method_body: added the rule '| block SC_TK'
e04a16fb
AG
38static_initializer: added the rule 'static block SC_TK'.
39
40Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43statement_nsi: 'nsi' should be read no_short_if.
44
45Some rules have been modified to support JDK1.1 inner classes
46definitions and other extensions. */
47
48%{
e04a16fb 49#include "config.h"
36635152
GS
50#include "system.h"
51#include <dirent.h>
e04a16fb
AG
52#include "tree.h"
53#include "rtl.h"
54#include "obstack.h"
0a2138e2 55#include "toplev.h"
e04a16fb
AG
56#include "flags.h"
57#include "java-tree.h"
58#include "jcf.h"
59#include "lex.h"
60#include "parse.h"
61#include "zipfile.h"
5e942c50 62#include "convert.h"
63a212ed 63#include "buffer.h"
f099f336 64#include "xref.h"
b384405b 65#include "function.h"
138657ec 66#include "except.h"
19e223db 67#include "ggc.h"
e04a16fb 68
c2952b01
APB
69#ifndef DIR_SEPARATOR
70#define DIR_SEPARATOR '/'
71#endif
72
82371d41 73/* Local function prototypes */
df32d2ce
KG
74static char *java_accstring_lookup PARAMS ((int));
75static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
77static tree create_class PARAMS ((int, tree, tree, tree));
78static tree create_interface PARAMS ((int, tree, tree));
c2952b01 79static void end_class_declaration PARAMS ((int));
df32d2ce
KG
80static tree find_field PARAMS ((tree, tree));
81static tree lookup_field_wrapper PARAMS ((tree, tree));
82static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83static void register_fields PARAMS ((int, tree, tree));
98a52c2c 84static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
85static int parser_check_super PARAMS ((tree, tree, tree));
86static int parser_check_super_interface PARAMS ((tree, tree, tree));
87static void check_modifiers_consistency PARAMS ((int));
88static tree lookup_cl PARAMS ((tree));
89static tree lookup_java_method2 PARAMS ((tree, tree, int));
90static tree method_header PARAMS ((int, tree, tree, tree));
91static void fix_method_argument_names PARAMS ((tree ,tree));
92static tree method_declarator PARAMS ((tree, tree));
93static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 94 ATTRIBUTE_PRINTF_2;
88f3c477
JM
95static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
df32d2ce
KG
97static void parse_ctor_invocation_error PARAMS ((void));
98static tree parse_jdk1_1_error PARAMS ((const char *));
99static void complete_class_report_errors PARAMS ((jdep *));
100static int process_imports PARAMS ((void));
101static void read_import_dir PARAMS ((tree));
102static int find_in_imports_on_demand PARAMS ((tree));
9a7ab4b3 103static void find_in_imports PARAMS ((tree));
c7303e41
APB
104static void check_static_final_variable_assignment_flag PARAMS ((tree));
105static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108static int check_final_variable_indirect_assignment PARAMS ((tree));
109static void check_final_variable_global_assignment_flag PARAMS ((tree));
cf1748bf 110static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 111static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 112static void register_package PARAMS ((tree));
df32d2ce
KG
113static tree resolve_package PARAMS ((tree, tree *));
114static tree lookup_package_type PARAMS ((const char *, int));
c2952b01 115static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
116static void declare_local_variables PARAMS ((int, tree, tree));
117static void source_start_java_method PARAMS ((tree));
118static void source_end_java_method PARAMS ((void));
df32d2ce
KG
119static tree find_name_in_single_imports PARAMS ((tree));
120static void check_abstract_method_header PARAMS ((tree));
121static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 123static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 124static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 125 tree, tree, tree));
e101152f 126static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
89e09b9a 127 int *, tree *));
df32d2ce 128static int breakdown_qualified PARAMS ((tree *, tree *, tree));
a648f4e4 129static int in_same_package PARAMS ((tree, tree));
df32d2ce 130static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 131static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
132static tree resolve_no_layout PARAMS ((tree, tree));
133static int invocation_mode PARAMS ((tree, int));
134static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 135 tree, tree));
df32d2ce 136static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 137 tree *, tree *));
df32d2ce
KG
138static tree find_most_specific_methods_list PARAMS ((tree));
139static int argument_types_convertible PARAMS ((tree, tree));
140static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 141static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
142static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144static tree obtain_incomplete_type PARAMS ((tree));
145static tree java_complete_lhs PARAMS ((tree));
146static tree java_complete_tree PARAMS ((tree));
c2952b01 147static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 148static int analyze_clinit_body PARAMS ((tree));
92d83515 149static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
150static void java_complete_expand_method PARAMS ((tree));
151static int unresolved_type_p PARAMS ((tree, tree *));
152static void create_jdep_list PARAMS ((struct parser_ctxt *));
153static tree build_expr_block PARAMS ((tree, tree));
154static tree enter_block PARAMS ((void));
df32d2ce
KG
155static tree exit_block PARAMS ((void));
156static tree lookup_name_in_blocks PARAMS ((tree));
157static void maybe_absorb_scoping_blocks PARAMS ((void));
158static tree build_method_invocation PARAMS ((tree, tree));
159static tree build_new_invocation PARAMS ((tree, tree));
160static tree build_assignment PARAMS ((int, int, tree, tree));
161static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162static int check_final_assignment PARAMS ((tree ,tree));
163static tree patch_assignment PARAMS ((tree, tree, tree ));
164static tree patch_binop PARAMS ((tree, tree, tree));
165static tree build_unaryop PARAMS ((int, int, tree));
166static tree build_incdec PARAMS ((int, int, tree, int));
167static tree patch_unaryop PARAMS ((tree, tree));
168static tree build_cast PARAMS ((int, tree, tree));
169static tree build_null_of_type PARAMS ((tree));
170static tree patch_cast PARAMS ((tree, tree));
171static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173static int valid_cast_to_p PARAMS ((tree, tree));
174static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176static tree try_reference_assignconv PARAMS ((tree, tree));
177static tree build_unresolved_array_type PARAMS ((tree));
178static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179static tree build_array_ref PARAMS ((int, tree, tree));
180static tree patch_array_ref PARAMS ((tree));
181static tree make_qualified_name PARAMS ((tree, tree, int));
182static tree merge_qualified_name PARAMS ((tree, tree));
183static tree make_qualified_primary PARAMS ((tree, tree, int));
184static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 185 tree *, tree *));
df32d2ce 186static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
187static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188static tree build_newarray_node PARAMS ((tree, tree, int));
189static tree patch_newarray PARAMS ((tree));
190static tree resolve_type_during_patch PARAMS ((tree));
191static tree build_this PARAMS ((int));
9a7ab4b3 192static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
193static tree build_return PARAMS ((int, tree));
194static tree patch_return PARAMS ((tree));
195static tree maybe_access_field PARAMS ((tree, tree, tree));
196static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
197static int check_for_static_method_reference PARAMS ((tree, tree, tree,
198 tree, tree));
e101152f 199static int not_accessible_p PARAMS ((tree, tree, tree, int));
df32d2ce
KG
200static void check_deprecation PARAMS ((tree, tree));
201static int class_in_current_package PARAMS ((tree));
202static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203static tree patch_if_else_statement PARAMS ((tree));
204static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206static tree patch_exit_expr PARAMS ((tree));
207static tree build_labeled_block PARAMS ((int, tree));
208static tree finish_labeled_statement PARAMS ((tree, tree));
209static tree build_bc_statement PARAMS ((int, int, tree));
210static tree patch_bc_statement PARAMS ((tree));
211static tree patch_loop_statement PARAMS ((tree));
212static tree build_new_loop PARAMS ((tree));
213static tree build_loop_body PARAMS ((int, tree, int));
214static tree finish_loop_body PARAMS ((int, tree, tree, int));
215static tree build_debugable_stmt PARAMS ((int, tree));
216static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217static tree patch_switch_statement PARAMS ((tree));
218static tree string_constant_concatenation PARAMS ((tree, tree));
219static tree build_string_concatenation PARAMS ((tree, tree));
220static tree patch_string_cst PARAMS ((tree));
221static tree patch_string PARAMS ((tree));
222static tree build_try_statement PARAMS ((int, tree, tree));
223static tree build_try_finally_statement PARAMS ((int, tree, tree));
224static tree patch_try_statement PARAMS ((tree));
225static tree patch_synchronized_statement PARAMS ((tree, tree));
226static tree patch_throw_statement PARAMS ((tree, tree));
227static void check_thrown_exceptions PARAMS ((int, tree));
228static int check_thrown_exceptions_do PARAMS ((tree));
229static void purge_unchecked_exceptions PARAMS ((tree));
230static void check_throws_clauses PARAMS ((tree, tree, tree));
231static void finish_method_declaration PARAMS ((tree));
232static tree build_super_invocation PARAMS ((tree));
233static int verify_constructor_circularity PARAMS ((tree, tree));
234static char *constructor_circularity_msg PARAMS ((tree, tree));
235static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 236 int, int));
df32d2ce
KG
237static const char *get_printable_method_name PARAMS ((tree));
238static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
239static tree generate_finit PARAMS ((tree));
240static void add_instance_initializer PARAMS ((tree));
df32d2ce 241static void fix_constructors PARAMS ((tree));
c2952b01
APB
242static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
243 tree, int *));
244static void craft_constructor PARAMS ((tree, tree));
245static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
246static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
247static void start_artificial_method_body PARAMS ((tree));
248static void end_artificial_method_body PARAMS ((tree));
249static int check_method_redefinition PARAMS ((tree, tree));
165f37bc 250static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
251static void java_check_regular_methods PARAMS ((tree));
252static void java_check_abstract_methods PARAMS ((tree));
253static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
254static void unreachable_stmt_error PARAMS ((tree));
255static tree find_expr_with_wfl PARAMS ((tree));
256static void missing_return_error PARAMS ((tree));
257static tree build_new_array_init PARAMS ((int, tree));
258static tree patch_new_array_init PARAMS ((tree, tree));
259static tree maybe_build_array_element_wfl PARAMS ((tree));
260static int array_constructor_check_entry PARAMS ((tree, tree));
261static const char *purify_type_name PARAMS ((const char *));
262static tree fold_constant_for_init PARAMS ((tree, tree));
263static tree strip_out_static_field_access_decl PARAMS ((tree));
264static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
265static void static_ref_err PARAMS ((tree, tree, tree));
266static void parser_add_interface PARAMS ((tree, tree, tree));
267static void add_superinterfaces PARAMS ((tree, tree));
268static tree jdep_resolve_class PARAMS ((jdep *));
269static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
270static void java_complete_expand_classes PARAMS ((void));
271static void java_complete_expand_class PARAMS ((tree));
272static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
273static tree cut_identifier_in_qualified PARAMS ((tree));
274static tree java_stabilize_reference PARAMS ((tree));
275static tree do_unary_numeric_promotion PARAMS ((tree));
276static char * operator_string PARAMS ((tree));
277static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
278static tree merge_string_cste PARAMS ((tree, tree, int));
279static tree java_refold PARAMS ((tree));
280static int java_decl_equiv PARAMS ((tree, tree));
281static int binop_compound_p PARAMS ((enum tree_code));
282static tree search_loop PARAMS ((tree));
283static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 284static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
285static void java_check_abstract_method_definitions PARAMS ((tree));
286static void java_debug_context_do PARAMS ((int));
c2952b01
APB
287static void java_parser_context_push_initialized_field PARAMS ((void));
288static void java_parser_context_pop_initialized_field PARAMS ((void));
289static tree reorder_static_initialized PARAMS ((tree));
290static void java_parser_context_suspend PARAMS ((void));
291static void java_parser_context_resume PARAMS ((void));
c7303e41 292static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
293
294/* JDK 1.1 work. FIXME */
295
296static tree maybe_make_nested_class_name PARAMS ((tree));
297static void make_nested_class_name PARAMS ((tree));
298static void set_nested_class_simple_name_value PARAMS ((tree, int));
299static void link_nested_class_to_enclosing PARAMS ((void));
300static tree find_as_inner_class PARAMS ((tree, tree, tree));
301static tree find_as_inner_class_do PARAMS ((tree, tree));
302static int check_inner_class_redefinition PARAMS ((tree, tree));
303
304static tree build_thisn_assign PARAMS ((void));
305static tree build_current_thisn PARAMS ((tree));
306static tree build_access_to_thisn PARAMS ((tree, tree, int));
307static tree maybe_build_thisn_access_method PARAMS ((tree));
308
309static tree build_outer_field_access PARAMS ((tree, tree));
310static tree build_outer_field_access_methods PARAMS ((tree));
311static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
312 tree, tree));
313static tree build_outer_method_access_method PARAMS ((tree));
314static tree build_new_access_id PARAMS ((void));
315static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
316 tree, tree));
317
318static int outer_field_access_p PARAMS ((tree, tree));
319static int outer_field_expanded_access_p PARAMS ((tree, tree *,
320 tree *, tree *));
321static tree outer_field_access_fix PARAMS ((tree, tree, tree));
322static tree build_incomplete_class_ref PARAMS ((int, tree));
323static tree patch_incomplete_class_ref PARAMS ((tree));
324static tree create_anonymous_class PARAMS ((int, tree));
325static void patch_anonymous_class PARAMS ((tree, tree, tree));
326static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 327
165f37bc
APB
328static tree build_dot_class_method PARAMS ((tree));
329static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 330static void create_new_parser_context PARAMS ((int));
f15b9af9 331static void mark_parser_ctxt PARAMS ((void *));
165f37bc 332
e04a16fb
AG
333/* Number of error found so far. */
334int java_error_count;
335/* Number of warning found so far. */
336int java_warning_count;
ce6e9147
APB
337/* Tell when not to fold, when doing xrefs */
338int do_not_fold;
c2952b01 339/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 340const char *cyclic_inheritance_report;
f15b9af9 341
c2952b01
APB
342/* Tell when we're within an instance initializer */
343static int in_instance_initializer;
e04a16fb
AG
344
345/* The current parser context */
d4370213 346struct parser_ctxt *ctxp;
e04a16fb 347
d4370213 348/* List of things that were analyzed for which code will be generated */
fea2d5da 349struct parser_ctxt *ctxp_for_generation = NULL;
b351b287 350
e04a16fb
AG
351/* binop_lookup maps token to tree_code. It is used where binary
352 operations are involved and required by the parser. RDIV_EXPR
353 covers both integral/floating point division. The code is changed
354 once the type of both operator is worked out. */
355
356static enum tree_code binop_lookup[19] =
357 {
358 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
359 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
360 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
361 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
362 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
363 };
364#define BINOP_LOOKUP(VALUE) \
6e2aa220 365 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 366
5cbdba64
APB
367/* This is the end index for binary operators that can also be used
368 in compound assignements. */
369#define BINOP_COMPOUND_CANDIDATES 11
370
e04a16fb 371/* The "$L" identifier we use to create labels. */
b67d701b
PB
372static tree label_id = NULL_TREE;
373
374/* The "StringBuffer" identifier used for the String `+' operator. */
375static tree wfl_string_buffer = NULL_TREE;
376
377/* The "append" identifier used for String `+' operator. */
378static tree wfl_append = NULL_TREE;
379
380/* The "toString" identifier used for String `+' operator. */
381static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
382
383/* The "java.lang" import qualified name. */
384static tree java_lang_id = NULL_TREE;
09ed0f70 385
c2952b01
APB
386/* The generated `inst$' identifier used for generated enclosing
387 instance/field access functions. */
388static tree inst_id = NULL_TREE;
389
09ed0f70
APB
390/* The "java.lang.Cloneable" qualified name. */
391static tree java_lang_cloneable = NULL_TREE;
f099f336 392
ee17a290
TT
393/* The "java.io.Serializable" qualified name. */
394static tree java_io_serializable = NULL_TREE;
395
f099f336
APB
396/* Context and flag for static blocks */
397static tree current_static_block = NULL_TREE;
398
c2952b01
APB
399/* The generated `write_parm_value$' identifier. */
400static tree wpv_id;
401
ee07f4f4
APB
402/* The list of all packages we've seen so far */
403static tree package_list = NULL_TREE;
2884c41e 404
19e223db
MM
405/* Hold THIS for the scope of the current public method decl. */
406static tree current_this;
407
408/* Hold a list of catch clauses list. The first element of this list is
409 the list of the catch clauses of the currently analysed try block. */
410static tree currently_caught_type_list;
411
daaaf29f 412static tree src_parse_roots[1] = { NULL_TREE };
fea2d5da
PB
413
414/* All classes seen from source code */
415#define gclass_list src_parse_roots[0]
416
2884c41e
KG
417/* Check modifiers. If one doesn't fit, retrieve it in its declaration
418 line and point it out. */
419/* Should point out the one that don't fit. ASCII/unicode, going
420 backward. FIXME */
421
422#define check_modifiers(__message, __value, __mask) do { \
423 if ((__value) & ~(__mask)) \
424 { \
425 int i, remainder = (__value) & ~(__mask); \
426 for (i = 0; i <= 10; i++) \
427 if ((1 << i) & remainder) \
428 parse_error_context (ctxp->modifier_ctx [i], (__message), \
429 java_accstring_lookup (1 << i)); \
430 } \
431} while (0)
ee07f4f4 432
e04a16fb
AG
433%}
434
435%union {
436 tree node;
437 int sub_token;
438 struct {
439 int token;
440 int location;
441 } operator;
442 int value;
443}
444
9ee9b555
KG
445%{
446#include "lex.c"
447%}
448
e04a16fb
AG
449%pure_parser
450
451/* Things defined here have to match the order of what's in the
452 binop_lookup table. */
453
454%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
455%token LS_TK SRS_TK ZRS_TK
456%token AND_TK XOR_TK OR_TK
457%token BOOL_AND_TK BOOL_OR_TK
458%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
459
460/* This maps to the same binop_lookup entry than the token above */
461
462%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
463%token REM_ASSIGN_TK
464%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
465%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
466
467
468/* Modifier TOKEN have to be kept in this order. Don't scramble it */
469
470%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
471%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
472%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
473%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 474%token STRICT_TK
e04a16fb
AG
475
476/* Keep those two in order, too */
477%token DECR_TK INCR_TK
478
479/* From now one, things can be in any order */
480
481%token DEFAULT_TK IF_TK THROW_TK
482%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
483%token THROWS_TK BREAK_TK IMPORT_TK
484%token ELSE_TK INSTANCEOF_TK RETURN_TK
485%token VOID_TK CATCH_TK INTERFACE_TK
486%token CASE_TK EXTENDS_TK FINALLY_TK
487%token SUPER_TK WHILE_TK CLASS_TK
488%token SWITCH_TK CONST_TK TRY_TK
489%token FOR_TK NEW_TK CONTINUE_TK
490%token GOTO_TK PACKAGE_TK THIS_TK
491
492%token BYTE_TK SHORT_TK INT_TK LONG_TK
493%token CHAR_TK INTEGRAL_TK
494
495%token FLOAT_TK DOUBLE_TK FP_TK
496
497%token ID_TK
498
499%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
500
501%token ASSIGN_ANY_TK ASSIGN_TK
502%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
503
504%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
505%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
506
c2952b01 507%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
508
509%type <node> super ID_TK identifier
510%type <node> name simple_name qualified_name
c2952b01 511%type <node> type_declaration compilation_unit
e04a16fb
AG
512 field_declaration method_declaration extends_interfaces
513 interfaces interface_type_list
c2952b01 514 class_member_declaration
e04a16fb
AG
515 import_declarations package_declaration
516 type_declarations interface_body
517 interface_member_declaration constant_declaration
518 interface_member_declarations interface_type
519 abstract_method_declaration interface_type_list
520%type <node> class_body_declaration class_member_declaration
521 static_initializer constructor_declaration block
22eed1e6 522%type <node> class_body_declarations constructor_header
e04a16fb
AG
523%type <node> class_or_interface_type class_type class_type_list
524 constructor_declarator explicit_constructor_invocation
b9f7e36c 525%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
526
527%type <node> variable_declarator_id variable_declarator
528 variable_declarators variable_initializer
22eed1e6 529 variable_initializers constructor_body
ac825856 530 array_initializer
e04a16fb 531
2e5eb5c5 532%type <node> class_body block_end constructor_block_end
e04a16fb
AG
533%type <node> statement statement_without_trailing_substatement
534 labeled_statement if_then_statement label_decl
535 if_then_else_statement while_statement for_statement
536 statement_nsi labeled_statement_nsi do_statement
537 if_then_else_statement_nsi while_statement_nsi
538 for_statement_nsi statement_expression_list for_init
539 for_update statement_expression expression_statement
540 primary_no_new_array expression primary
541 array_creation_expression array_type
542 class_instance_creation_expression field_access
543 method_invocation array_access something_dot_new
544 argument_list postfix_expression while_expression
545 post_increment_expression post_decrement_expression
546 unary_expression_not_plus_minus unary_expression
547 pre_increment_expression pre_decrement_expression
548 unary_expression_not_plus_minus cast_expression
549 multiplicative_expression additive_expression
550 shift_expression relational_expression
551 equality_expression and_expression
552 exclusive_or_expression inclusive_or_expression
553 conditional_and_expression conditional_or_expression
554 conditional_expression assignment_expression
555 left_hand_side assignment for_header for_begin
556 constant_expression do_statement_begin empty_statement
b67d701b 557 switch_statement synchronized_statement throw_statement
f8976021 558 try_statement switch_expression switch_block
15fdcfe9 559 catches catch_clause catch_clause_parameter finally
c2952b01 560 anonymous_class_creation
e04a16fb
AG
561%type <node> return_statement break_statement continue_statement
562
563%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
564%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
565%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
566%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
567%type <operator> ASSIGN_ANY_TK assignment_operator
568%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
569%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
570%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 571%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 572%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
573%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
574%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 575%type <operator> NEW_TK
e04a16fb
AG
576
577%type <node> method_body
578
579%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
580 STRING_LIT_TK NULL_TK VOID_TK
581
582%type <node> IF_TK WHILE_TK FOR_TK
583
584%type <node> formal_parameter_list formal_parameter
585 method_declarator method_header
586
c2952b01 587%type <node> primitive_type reference_type type
e04a16fb
AG
588 BOOLEAN_TK INTEGRAL_TK FP_TK
589
c2952b01 590/* Added or modified JDK 1.1 rule types */
c7303e41 591%type <node> type_literals
c2952b01 592
e04a16fb
AG
593%%
594/* 19.2 Production from 2.3: The Syntactic Grammar */
595goal:
19e223db
MM
596 {
597 /* Register static variables with the garbage
598 collector. */
599 ggc_add_tree_root (&label_id, 1);
600 ggc_add_tree_root (&wfl_string_buffer, 1);
601 ggc_add_tree_root (&wfl_append, 1);
602 ggc_add_tree_root (&wfl_to_string, 1);
603 ggc_add_tree_root (&java_lang_id, 1);
604 ggc_add_tree_root (&inst_id, 1);
605 ggc_add_tree_root (&java_lang_cloneable, 1);
606 ggc_add_tree_root (&java_io_serializable, 1);
607 ggc_add_tree_root (&current_static_block, 1);
608 ggc_add_tree_root (&wpv_id, 1);
609 ggc_add_tree_root (&package_list, 1);
610 ggc_add_tree_root (&current_this, 1);
611 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
612 ggc_add_root (&ctxp, 1,
613 sizeof (struct parser_ctxt *),
614 mark_parser_ctxt);
615 ggc_add_root (&ctxp_for_generation, 1,
616 sizeof (struct parser_ctxt *),
617 mark_parser_ctxt);
19e223db 618 }
e04a16fb
AG
619 compilation_unit
620 {}
621;
622
623/* 19.3 Productions from 3: Lexical structure */
624literal:
625 INT_LIT_TK
626| FP_LIT_TK
627| BOOL_LIT_TK
628| CHAR_LIT_TK
629| STRING_LIT_TK
630| NULL_TK
631;
632
633/* 19.4 Productions from 4: Types, Values and Variables */
634type:
635 primitive_type
636| reference_type
637;
638
639primitive_type:
640 INTEGRAL_TK
641| FP_TK
642| BOOLEAN_TK
643;
644
645reference_type:
646 class_or_interface_type
647| array_type
648;
649
650class_or_interface_type:
651 name
652;
653
654class_type:
655 class_or_interface_type /* Default rule */
656;
657
658interface_type:
659 class_or_interface_type
660;
661
662array_type:
c7303e41 663 primitive_type dims
e04a16fb 664 {
c7303e41
APB
665 int osb = pop_current_osb (ctxp);
666 tree t = build_java_array_type (($1), -1);
667 CLASS_LOADED_P (t) = 1;
668 while (--osb)
669 t = build_unresolved_array_type (t);
670 $$ = t;
671 }
672| name dims
673 {
674 int osb = pop_current_osb (ctxp);
675 tree t = $1;
676 while (osb--)
677 t = build_unresolved_array_type (t);
678 $$ = t;
e04a16fb 679 }
e04a16fb
AG
680;
681
682/* 19.5 Productions from 6: Names */
683name:
684 simple_name /* Default rule */
685| qualified_name /* Default rule */
686;
687
688simple_name:
689 identifier /* Default rule */
690;
691
692qualified_name:
693 name DOT_TK identifier
694 { $$ = make_qualified_name ($1, $3, $2.location); }
695;
696
697identifier:
698 ID_TK
699;
700
701/* 19.6: Production from 7: Packages */
702compilation_unit:
703 {$$ = NULL;}
704| package_declaration
705| import_declarations
706| type_declarations
707| package_declaration import_declarations
708| package_declaration type_declarations
709| import_declarations type_declarations
710| package_declaration import_declarations type_declarations
711;
712
713import_declarations:
714 import_declaration
715 {
716 $$ = NULL;
717 }
718| import_declarations import_declaration
719 {
720 $$ = NULL;
721 }
722;
723
724type_declarations:
725 type_declaration
726| type_declarations type_declaration
727;
728
729package_declaration:
730 PACKAGE_TK name SC_TK
ee07f4f4
APB
731 {
732 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 733 register_package (ctxp->package);
ee07f4f4 734 }
e04a16fb
AG
735| PACKAGE_TK error
736 {yyerror ("Missing name"); RECOVER;}
737| PACKAGE_TK name error
738 {yyerror ("';' expected"); RECOVER;}
739;
740
741import_declaration:
742 single_type_import_declaration
743| type_import_on_demand_declaration
744;
745
746single_type_import_declaration:
747 IMPORT_TK name SC_TK
748 {
9a7ab4b3 749 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 750 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 751 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
752 while (last != IDENTIFIER_POINTER (name))
753 {
754 if (last [0] == '.')
755 break;
756 last--;
757 }
758 last_name = get_identifier (++last);
759 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
760 {
761 tree err = find_name_in_single_imports (last_name);
762 if (err && err != name)
763 parse_error_context
764 ($2, "Ambiguous class: `%s' and `%s'",
765 IDENTIFIER_POINTER (name),
766 IDENTIFIER_POINTER (err));
5e942c50 767 else
9a7ab4b3 768 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
769 }
770 else
5e942c50 771 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
772 }
773| IMPORT_TK error
774 {yyerror ("Missing name"); RECOVER;}
775| IMPORT_TK name error
776 {yyerror ("';' expected"); RECOVER;}
777;
778
779type_import_on_demand_declaration:
780 IMPORT_TK name DOT_TK MULT_TK SC_TK
781 {
782 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
783 /* Don't import java.lang.* twice. */
784 if (name != java_lang_id)
785 {
ba179f9f 786 read_import_dir ($2);
9a7ab4b3
APB
787 ctxp->import_demand_list =
788 chainon (ctxp->import_demand_list,
789 build_tree_list ($2, NULL_TREE));
ba179f9f 790 }
e04a16fb
AG
791 }
792| IMPORT_TK name DOT_TK error
793 {yyerror ("'*' expected"); RECOVER;}
794| IMPORT_TK name DOT_TK MULT_TK error
795 {yyerror ("';' expected"); RECOVER;}
796;
797
798type_declaration:
799 class_declaration
c2952b01 800 { end_class_declaration (0); }
e04a16fb 801| interface_declaration
c2952b01 802 { end_class_declaration (0); }
5f1c312a 803| empty_statement
e04a16fb
AG
804| error
805 {
806 YYERROR_NOW;
807 yyerror ("Class or interface declaration expected");
808 }
809;
810
811/* 19.7 Shortened from the original:
812 modifiers: modifier | modifiers modifier
813 modifier: any of public... */
814modifiers:
815 MODIFIER_TK
816 {
817 $$ = (1 << $1);
818 }
819| modifiers MODIFIER_TK
820 {
821 int acc = (1 << $2);
822 if ($$ & acc)
823 parse_error_context
824 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
825 java_accstring_lookup (acc));
826 else
827 {
828 $$ |= acc;
829 }
830 }
831;
832
833/* 19.8.1 Production from $8.1: Class Declaration */
834class_declaration:
835 modifiers CLASS_TK identifier super interfaces
836 { create_class ($1, $3, $4, $5); }
837 class_body
e04a16fb
AG
838| CLASS_TK identifier super interfaces
839 { create_class (0, $2, $3, $4); }
840 class_body
e04a16fb
AG
841| modifiers CLASS_TK error
842 {yyerror ("Missing class name"); RECOVER;}
843| CLASS_TK error
844 {yyerror ("Missing class name"); RECOVER;}
845| CLASS_TK identifier error
0b4d333e
APB
846 {
847 if (!ctxp->class_err) yyerror ("'{' expected");
848 DRECOVER(class1);
849 }
e04a16fb
AG
850| modifiers CLASS_TK identifier error
851 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
852;
853
854super:
855 { $$ = NULL; }
856| EXTENDS_TK class_type
857 { $$ = $2; }
858| EXTENDS_TK class_type error
859 {yyerror ("'{' expected"); ctxp->class_err=1;}
860| EXTENDS_TK error
861 {yyerror ("Missing super class name"); ctxp->class_err=1;}
862;
863
864interfaces:
865 { $$ = NULL_TREE; }
866| IMPLEMENTS_TK interface_type_list
867 { $$ = $2; }
868| IMPLEMENTS_TK error
869 {
870 ctxp->class_err=1;
871 yyerror ("Missing interface name");
872 }
873;
874
875interface_type_list:
876 interface_type
877 {
878 ctxp->interface_number = 1;
879 $$ = build_tree_list ($1, NULL_TREE);
880 }
881| interface_type_list C_TK interface_type
882 {
883 ctxp->interface_number++;
884 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
885 }
886| interface_type_list C_TK error
887 {yyerror ("Missing interface name"); RECOVER;}
888;
889
890class_body:
891 OCB_TK CCB_TK
7f10c2e2
APB
892 {
893 /* Store the location of the `}' when doing xrefs */
894 if (flag_emit_xref)
c2952b01 895 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 896 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 897 $$ = GET_CPC ();
7f10c2e2 898 }
e04a16fb 899| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
900 {
901 /* Store the location of the `}' when doing xrefs */
902 if (flag_emit_xref)
c2952b01 903 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 904 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 905 $$ = GET_CPC ();
7f10c2e2 906 }
e04a16fb
AG
907;
908
909class_body_declarations:
910 class_body_declaration
911| class_body_declarations class_body_declaration
912;
913
914class_body_declaration:
915 class_member_declaration
916| static_initializer
917| constructor_declaration
918| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
919 {
920 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
921 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
922 }
e04a16fb
AG
923;
924
925class_member_declaration:
926 field_declaration
927| method_declaration
928| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
929 { end_class_declaration (1); }
930| interface_declaration /* Added, JDK1.1 inner interfaces */
931 { end_class_declaration (1); }
5f1c312a 932| empty_statement
e04a16fb
AG
933;
934
935/* 19.8.2 Productions from 8.3: Field Declarations */
936field_declaration:
937 type variable_declarators SC_TK
938 { register_fields (0, $1, $2); }
939| modifiers type variable_declarators SC_TK
940 {
e04a16fb
AG
941 check_modifiers
942 ("Illegal modifier `%s' for field declaration",
943 $1, FIELD_MODIFIERS);
944 check_modifiers_consistency ($1);
945 register_fields ($1, $2, $3);
946 }
947;
948
949variable_declarators:
950 /* Should we use build_decl_list () instead ? FIXME */
951 variable_declarator /* Default rule */
952| variable_declarators C_TK variable_declarator
953 { $$ = chainon ($1, $3); }
954| variable_declarators C_TK error
955 {yyerror ("Missing term"); RECOVER;}
956;
957
958variable_declarator:
959 variable_declarator_id
960 { $$ = build_tree_list ($1, NULL_TREE); }
961| variable_declarator_id ASSIGN_TK variable_initializer
962 {
963 if (java_error_count)
964 $3 = NULL_TREE;
965 $$ = build_tree_list
966 ($1, build_assignment ($2.token, $2.location, $1, $3));
967 }
968| variable_declarator_id ASSIGN_TK error
969 {
970 yyerror ("Missing variable initializer");
971 $$ = build_tree_list ($1, NULL_TREE);
972 RECOVER;
973 }
974| variable_declarator_id ASSIGN_TK variable_initializer error
975 {
976 yyerror ("';' expected");
977 $$ = build_tree_list ($1, NULL_TREE);
978 RECOVER;
979 }
980;
981
982variable_declarator_id:
983 identifier
984| variable_declarator_id OSB_TK CSB_TK
c583dd46 985 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
986| identifier error
987 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
988| variable_declarator_id OSB_TK error
29f8b718
APB
989 {
990 tree node = java_lval.node;
991 if (node && (TREE_CODE (node) == INTEGER_CST
992 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
993 yyerror ("Can't specify array dimension in a declaration");
994 else
995 yyerror ("']' expected");
996 DRECOVER(vdi);
997 }
e04a16fb
AG
998| variable_declarator_id CSB_TK error
999 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1000;
1001
1002variable_initializer:
1003 expression
1004| array_initializer
e04a16fb
AG
1005;
1006
1007/* 19.8.3 Productions from 8.4: Method Declarations */
1008method_declaration:
1009 method_header
1010 {
1011 current_function_decl = $1;
c2952b01
APB
1012 if (current_function_decl
1013 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1014 source_start_java_method (current_function_decl);
1015 else
1016 current_function_decl = NULL_TREE;
e04a16fb
AG
1017 }
1018 method_body
b635eb2f 1019 { finish_method_declaration ($3); }
e04a16fb
AG
1020| method_header error
1021 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1022;
1023
1024method_header:
1025 type method_declarator throws
b9f7e36c 1026 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1027| VOID_TK method_declarator throws
b9f7e36c 1028 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1029| modifiers type method_declarator throws
b9f7e36c 1030 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1031| modifiers VOID_TK method_declarator throws
b9f7e36c 1032 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1033| type error
efa0a23f
APB
1034 {
1035 yyerror ("Invalid method declaration, method name required");
1036 RECOVER;
1037 }
e04a16fb
AG
1038| modifiers type error
1039 {RECOVER;}
1040| VOID_TK error
1041 {yyerror ("Identifier expected"); RECOVER;}
1042| modifiers VOID_TK error
1043 {yyerror ("Identifier expected"); RECOVER;}
1044| modifiers error
1045 {
1046 yyerror ("Invalid method declaration, return type required");
1047 RECOVER;
1048 }
1049;
1050
1051method_declarator:
1052 identifier OP_TK CP_TK
c2952b01
APB
1053 {
1054 ctxp->formal_parameter_number = 0;
1055 $$ = method_declarator ($1, NULL_TREE);
1056 }
e04a16fb
AG
1057| identifier OP_TK formal_parameter_list CP_TK
1058 { $$ = method_declarator ($1, $3); }
1059| method_declarator OSB_TK CSB_TK
1060 {
1886c9d8
APB
1061 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1062 TREE_PURPOSE ($1) =
1063 build_unresolved_array_type (TREE_PURPOSE ($1));
1064 parse_warning_context
1065 (wfl_operator,
1066 "Discouraged form of returned type specification");
e04a16fb
AG
1067 }
1068| identifier OP_TK error
1069 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1070| method_declarator OSB_TK error
1071 {yyerror ("']' expected"); RECOVER;}
1072;
1073
1074formal_parameter_list:
1075 formal_parameter
1076 {
1077 ctxp->formal_parameter_number = 1;
1078 }
1079| formal_parameter_list C_TK formal_parameter
1080 {
1081 ctxp->formal_parameter_number += 1;
1082 $$ = chainon ($1, $3);
1083 }
1084| formal_parameter_list C_TK error
c2952b01 1085 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1086;
1087
1088formal_parameter:
1089 type variable_declarator_id
1090 {
1091 $$ = build_tree_list ($2, $1);
1092 }
18990de5 1093| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1094 {
5256aa37 1095 $$ = build_tree_list ($3, $2);
c2952b01 1096 ARG_FINAL_P ($$) = 1;
5256aa37 1097 }
e04a16fb 1098| type error
f8989a66
APB
1099 {
1100 yyerror ("Missing identifier"); RECOVER;
1101 $$ = NULL_TREE;
1102 }
18990de5 1103| final type error
e04a16fb 1104 {
e04a16fb 1105 yyerror ("Missing identifier"); RECOVER;
f8989a66 1106 $$ = NULL_TREE;
e04a16fb
AG
1107 }
1108;
1109
18990de5
JB
1110final:
1111 modifiers
1112 {
1113 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1114 $1, ACC_FINAL);
1115 if ($1 != ACC_FINAL)
1116 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1117 }
1118;
1119
e04a16fb 1120throws:
b9f7e36c 1121 { $$ = NULL_TREE; }
e04a16fb 1122| THROWS_TK class_type_list
b9f7e36c 1123 { $$ = $2; }
e04a16fb
AG
1124| THROWS_TK error
1125 {yyerror ("Missing class type term"); RECOVER;}
1126;
1127
1128class_type_list:
1129 class_type
c877974e 1130 { $$ = build_tree_list ($1, $1); }
e04a16fb 1131| class_type_list C_TK class_type
c877974e 1132 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1133| class_type_list C_TK error
1134 {yyerror ("Missing class type term"); RECOVER;}
1135;
1136
1137method_body:
1138 block
5f1c312a 1139| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1140;
1141
1142/* 19.8.4 Productions from 8.5: Static Initializers */
1143static_initializer:
1144 static block
1145 {
c2952b01
APB
1146 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1147 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1148 current_static_block = NULL_TREE;
e04a16fb 1149 }
e04a16fb
AG
1150;
1151
1152static: /* Test lval.sub_token here */
c2952b01 1153 modifiers
e04a16fb 1154 {
c2952b01
APB
1155 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1156 /* Can't have a static initializer in an innerclass */
1157 if ($1 | ACC_STATIC &&
1158 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1159 parse_error_context
1160 (MODIFIER_WFL (STATIC_TK),
1161 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1162 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1163 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1164 }
1165;
1166
1167/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1168constructor_declaration:
22eed1e6 1169 constructor_header
e04a16fb 1170 {
22eed1e6
APB
1171 current_function_decl = $1;
1172 source_start_java_method (current_function_decl);
e04a16fb 1173 }
22eed1e6 1174 constructor_body
b635eb2f 1175 { finish_method_declaration ($3); }
22eed1e6
APB
1176;
1177
1178constructor_header:
1179 constructor_declarator throws
1180 { $$ = method_header (0, NULL_TREE, $1, $2); }
1181| modifiers constructor_declarator throws
1182 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1183;
1184
1185constructor_declarator:
1186 simple_name OP_TK CP_TK
c2952b01
APB
1187 {
1188 ctxp->formal_parameter_number = 0;
1189 $$ = method_declarator ($1, NULL_TREE);
1190 }
e04a16fb 1191| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1192 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1193;
1194
1195constructor_body:
22eed1e6
APB
1196 /* Unlike regular method, we always need a complete (empty)
1197 body so we can safely perform all the required code
1198 addition (super invocation and field initialization) */
2e5eb5c5 1199 block_begin constructor_block_end
22eed1e6 1200 {
9bbc7d9f 1201 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1202 $$ = $2;
1203 }
2e5eb5c5 1204| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1205 { $$ = $3; }
2e5eb5c5 1206| block_begin block_statements constructor_block_end
22eed1e6 1207 { $$ = $3; }
2e5eb5c5 1208| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1209 { $$ = $4; }
e04a16fb
AG
1210;
1211
2e5eb5c5
APB
1212constructor_block_end:
1213 block_end
5f1c312a 1214;
2e5eb5c5 1215
e04a16fb
AG
1216/* Error recovery for that rule moved down expression_statement: rule. */
1217explicit_constructor_invocation:
1218 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1219 {
1220 $$ = build_method_invocation ($1, NULL_TREE);
1221 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1222 $$ = java_method_add_stmt (current_function_decl, $$);
1223 }
e04a16fb 1224| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1225 {
1226 $$ = build_method_invocation ($1, $3);
1227 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1228 $$ = java_method_add_stmt (current_function_decl, $$);
1229 }
e04a16fb
AG
1230 /* Added, JDK1.1 inner classes. Modified because the rule
1231 'primary' couldn't work. */
1232| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1233 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1234| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1235 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1236;
1237
1238this_or_super: /* Added, simplifies error diagnostics */
1239 THIS_TK
1240 {
9ee9b555 1241 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1242 EXPR_WFL_LINECOL (wfl) = $1.location;
1243 $$ = wfl;
1244 }
1245| SUPER_TK
1246 {
9ee9b555 1247 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1248 EXPR_WFL_LINECOL (wfl) = $1.location;
1249 $$ = wfl;
1250 }
1251;
1252
1253/* 19.9 Productions from 9: Interfaces */
1254/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1255interface_declaration:
1256 INTERFACE_TK identifier
1257 { create_interface (0, $2, NULL_TREE); }
1258 interface_body
e04a16fb
AG
1259| modifiers INTERFACE_TK identifier
1260 { create_interface ($1, $3, NULL_TREE); }
1261 interface_body
e04a16fb
AG
1262| INTERFACE_TK identifier extends_interfaces
1263 { create_interface (0, $2, $3); }
1264 interface_body
e04a16fb
AG
1265| modifiers INTERFACE_TK identifier extends_interfaces
1266 { create_interface ($1, $3, $4); }
1267 interface_body
e04a16fb 1268| INTERFACE_TK identifier error
0b4d333e 1269 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1270| modifiers INTERFACE_TK identifier error
0b4d333e 1271 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1272;
1273
1274extends_interfaces:
1275 EXTENDS_TK interface_type
1276 {
1277 ctxp->interface_number = 1;
1278 $$ = build_tree_list ($2, NULL_TREE);
1279 }
1280| extends_interfaces C_TK interface_type
1281 {
1282 ctxp->interface_number++;
1283 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1284 }
1285| EXTENDS_TK error
1286 {yyerror ("Invalid interface type"); RECOVER;}
1287| extends_interfaces C_TK error
1288 {yyerror ("Missing term"); RECOVER;}
1289;
1290
1291interface_body:
1292 OCB_TK CCB_TK
1293 { $$ = NULL_TREE; }
1294| OCB_TK interface_member_declarations CCB_TK
1295 { $$ = NULL_TREE; }
1296;
1297
1298interface_member_declarations:
1299 interface_member_declaration
1300| interface_member_declarations interface_member_declaration
1301;
1302
1303interface_member_declaration:
1304 constant_declaration
1305| abstract_method_declaration
1306| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1307 { end_class_declaration (1); }
1308| interface_declaration /* Added, JDK1.1 inner interfaces */
1309 { end_class_declaration (1); }
e04a16fb
AG
1310;
1311
1312constant_declaration:
1313 field_declaration
1314;
1315
1316abstract_method_declaration:
1317 method_header SC_TK
1318 {
1319 check_abstract_method_header ($1);
1320 current_function_decl = NULL_TREE; /* FIXME ? */
1321 }
1322| method_header error
1323 {yyerror ("';' expected"); RECOVER;}
1324;
1325
1326/* 19.10 Productions from 10: Arrays */
1327array_initializer:
1328 OCB_TK CCB_TK
1179ebc2 1329 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1330| OCB_TK variable_initializers CCB_TK
f8976021 1331 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1332| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1333 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1334;
1335
1336variable_initializers:
1337 variable_initializer
f8976021
APB
1338 {
1339 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1340 $1, NULL_TREE);
1341 }
e04a16fb 1342| variable_initializers C_TK variable_initializer
1179ebc2
APB
1343 {
1344 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1345 }
e04a16fb
AG
1346| variable_initializers C_TK error
1347 {yyerror ("Missing term"); RECOVER;}
1348;
1349
1350/* 19.11 Production from 14: Blocks and Statements */
1351block:
1352 OCB_TK CCB_TK
7f10c2e2
APB
1353 {
1354 /* Store the location of the `}' when doing xrefs */
1355 if (current_function_decl && flag_emit_xref)
1356 DECL_END_SOURCE_LINE (current_function_decl) =
1357 EXPR_WFL_ADD_COL ($2.location, 1);
1358 $$ = empty_stmt_node;
1359 }
22eed1e6
APB
1360| block_begin block_statements block_end
1361 { $$ = $3; }
1362;
1363
1364block_begin:
1365 OCB_TK
e04a16fb 1366 { enter_block (); }
22eed1e6
APB
1367;
1368
1369block_end:
e04a16fb
AG
1370 CCB_TK
1371 {
1372 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1373 /* Store the location of the `}' when doing xrefs */
1374 if (current_function_decl && flag_emit_xref)
1375 DECL_END_SOURCE_LINE (current_function_decl) =
1376 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1377 $$ = exit_block ();
c280e37a
APB
1378 if (!BLOCK_SUBBLOCKS ($$))
1379 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1380 }
1381;
1382
1383block_statements:
1384 block_statement
1385| block_statements block_statement
1386;
1387
1388block_statement:
1389 local_variable_declaration_statement
1390| statement
15fdcfe9 1391 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1392| class_declaration /* Added, JDK1.1 local classes */
1393 {
1394 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1395 end_class_declaration (1);
1396 }
e04a16fb
AG
1397;
1398
1399local_variable_declaration_statement:
1400 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1401;
1402
1403local_variable_declaration:
1404 type variable_declarators
1405 { declare_local_variables (0, $1, $2); }
a003f638 1406| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1407 { declare_local_variables ($1, $2, $3); }
1408;
1409
1410statement:
1411 statement_without_trailing_substatement
1412| labeled_statement
e04a16fb 1413| if_then_statement
e04a16fb 1414| if_then_else_statement
e04a16fb 1415| while_statement
e04a16fb 1416| for_statement
cd9643f7 1417 { $$ = exit_block (); }
e04a16fb
AG
1418;
1419
1420statement_nsi:
1421 statement_without_trailing_substatement
1422| labeled_statement_nsi
e04a16fb 1423| if_then_else_statement_nsi
e04a16fb 1424| while_statement_nsi
e04a16fb 1425| for_statement_nsi
9dd939b2 1426 { $$ = exit_block (); }
e04a16fb
AG
1427;
1428
1429statement_without_trailing_substatement:
1430 block
e04a16fb 1431| empty_statement
e04a16fb 1432| expression_statement
e04a16fb 1433| switch_statement
e04a16fb 1434| do_statement
e04a16fb 1435| break_statement
e04a16fb 1436| continue_statement
e04a16fb
AG
1437| return_statement
1438| synchronized_statement
e04a16fb 1439| throw_statement
e04a16fb 1440| try_statement
e04a16fb
AG
1441;
1442
1443empty_statement:
1444 SC_TK
5f1c312a
APB
1445 {
1446 if (flag_extraneous_semicolon)
1447 {
1448 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1449 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1450 }
1451 $$ = empty_stmt_node;
1452 }
e04a16fb
AG
1453;
1454
1455label_decl:
1456 identifier REL_CL_TK
1457 {
1458 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1459 EXPR_WFL_NODE ($1));
e04a16fb
AG
1460 pushlevel (2);
1461 push_labeled_block ($$);
1462 PUSH_LABELED_BLOCK ($$);
1463 }
1464;
1465
1466labeled_statement:
1467 label_decl statement
b635eb2f 1468 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1469| identifier error
1470 {yyerror ("':' expected"); RECOVER;}
1471;
1472
1473labeled_statement_nsi:
1474 label_decl statement_nsi
b635eb2f 1475 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1476;
1477
1478/* We concentrate here a bunch of error handling rules that we couldn't write
1479 earlier, because expression_statement catches a missing ';'. */
1480expression_statement:
1481 statement_expression SC_TK
1482 {
1483 /* We have a statement. Generate a WFL around it so
1484 we can debug it */
1485 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1486 /* We know we have a statement, so set the debug
1487 info to be eventually generate here. */
1488 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1489 }
1490| error SC_TK
1491 {
29f8b718 1492 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1493 DRECOVER (expr_stmt);
1494 }
1495| error OCB_TK
1496 {
29f8b718 1497 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1498 DRECOVER (expr_stmt);
1499 }
1500| error CCB_TK
1501 {
29f8b718 1502 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1503 DRECOVER (expr_stmt);
1504 }
1505| this_or_super OP_TK error
1506 {yyerror ("')' expected"); RECOVER;}
1507| this_or_super OP_TK CP_TK error
22eed1e6 1508 {
8119c720 1509 parse_ctor_invocation_error ();
22eed1e6
APB
1510 RECOVER;
1511 }
e04a16fb
AG
1512| this_or_super OP_TK argument_list error
1513 {yyerror ("')' expected"); RECOVER;}
1514| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1515 {
8119c720 1516 parse_ctor_invocation_error ();
22eed1e6
APB
1517 RECOVER;
1518 }
e04a16fb
AG
1519| name DOT_TK SUPER_TK error
1520 {yyerror ("'(' expected"); RECOVER;}
1521| name DOT_TK SUPER_TK OP_TK error
1522 {yyerror ("')' expected"); RECOVER;}
1523| name DOT_TK SUPER_TK OP_TK argument_list error
1524 {yyerror ("')' expected"); RECOVER;}
1525| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1526 {yyerror ("';' expected"); RECOVER;}
1527| name DOT_TK SUPER_TK OP_TK CP_TK error
1528 {yyerror ("';' expected"); RECOVER;}
1529;
1530
1531statement_expression:
1532 assignment
1533| pre_increment_expression
e04a16fb 1534| pre_decrement_expression
e04a16fb 1535| post_increment_expression
e04a16fb 1536| post_decrement_expression
e04a16fb
AG
1537| method_invocation
1538| class_instance_creation_expression
e04a16fb
AG
1539;
1540
1541if_then_statement:
1542 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1543 {
1544 $$ = build_if_else_statement ($2.location, $3,
1545 $5, NULL_TREE);
1546 }
e04a16fb
AG
1547| IF_TK error
1548 {yyerror ("'(' expected"); RECOVER;}
1549| IF_TK OP_TK error
1550 {yyerror ("Missing term"); RECOVER;}
1551| IF_TK OP_TK expression error
1552 {yyerror ("')' expected"); RECOVER;}
1553;
1554
1555if_then_else_statement:
1556 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1557 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1558;
1559
1560if_then_else_statement_nsi:
1561 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1562 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1563;
1564
1565switch_statement:
15fdcfe9
PB
1566 switch_expression
1567 {
1568 enter_block ();
1569 }
1570 switch_block
b67d701b 1571 {
15fdcfe9 1572 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1573 I.e. make the last statment also have its own
1574 COMPOUND_EXPR. */
15fdcfe9
PB
1575 maybe_absorb_scoping_blocks ();
1576 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1577 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1578 }
1579;
1580
1581switch_expression:
1582 SWITCH_TK OP_TK expression CP_TK
1583 {
1584 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1585 EXPR_WFL_LINECOL ($$) = $2.location;
1586 }
e04a16fb
AG
1587| SWITCH_TK error
1588 {yyerror ("'(' expected"); RECOVER;}
1589| SWITCH_TK OP_TK error
1590 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1591| SWITCH_TK OP_TK expression CP_TK error
1592 {yyerror ("'{' expected"); RECOVER;}
1593;
1594
f8976021
APB
1595/* Default assignment is there to avoid type node on switch_block
1596 node. */
1597
e04a16fb
AG
1598switch_block:
1599 OCB_TK CCB_TK
f8976021 1600 { $$ = NULL_TREE; }
e04a16fb 1601| OCB_TK switch_labels CCB_TK
f8976021 1602 { $$ = NULL_TREE; }
e04a16fb 1603| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1604 { $$ = NULL_TREE; }
e04a16fb 1605| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1606 { $$ = NULL_TREE; }
e04a16fb
AG
1607;
1608
1609switch_block_statement_groups:
1610 switch_block_statement_group
1611| switch_block_statement_groups switch_block_statement_group
1612;
1613
1614switch_block_statement_group:
15fdcfe9 1615 switch_labels block_statements
e04a16fb
AG
1616;
1617
e04a16fb
AG
1618switch_labels:
1619 switch_label
1620| switch_labels switch_label
1621;
1622
1623switch_label:
1624 CASE_TK constant_expression REL_CL_TK
b67d701b 1625 {
15fdcfe9
PB
1626 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1627 EXPR_WFL_LINECOL (lab) = $1.location;
1628 java_method_add_stmt (current_function_decl, lab);
b67d701b 1629 }
e04a16fb 1630| DEFAULT_TK REL_CL_TK
b67d701b 1631 {
ac39dac0 1632 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1633 EXPR_WFL_LINECOL (lab) = $1.location;
1634 java_method_add_stmt (current_function_decl, lab);
b67d701b 1635 }
e04a16fb
AG
1636| CASE_TK error
1637 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1638| CASE_TK constant_expression error
1639 {yyerror ("':' expected"); RECOVER;}
1640| DEFAULT_TK error
1641 {yyerror ("':' expected"); RECOVER;}
1642;
1643
1644while_expression:
1645 WHILE_TK OP_TK expression CP_TK
1646 {
1647 tree body = build_loop_body ($2.location, $3, 0);
1648 $$ = build_new_loop (body);
1649 }
1650;
1651
1652while_statement:
1653 while_expression statement
b635eb2f 1654 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1655| WHILE_TK error
1656 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1657| WHILE_TK OP_TK error
1658 {yyerror ("Missing term and ')' expected"); RECOVER;}
1659| WHILE_TK OP_TK expression error
1660 {yyerror ("')' expected"); RECOVER;}
1661;
1662
1663while_statement_nsi:
1664 while_expression statement_nsi
b635eb2f 1665 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1666;
1667
1668do_statement_begin:
1669 DO_TK
1670 {
1671 tree body = build_loop_body (0, NULL_TREE, 1);
1672 $$ = build_new_loop (body);
1673 }
1674 /* Need error handing here. FIXME */
1675;
1676
1677do_statement:
1678 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1679 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1680;
1681
1682for_statement:
1683 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1684 {
1685 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1686 $3 = build_wfl_node ($3);
1687 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1688 }
e04a16fb
AG
1689| for_begin SC_TK SC_TK for_update CP_TK statement
1690 {
b635eb2f 1691 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1692 /* We have not condition, so we get rid of the EXIT_EXPR */
1693 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1694 empty_stmt_node;
e04a16fb
AG
1695 }
1696| for_begin SC_TK error
1697 {yyerror ("Invalid control expression"); RECOVER;}
1698| for_begin SC_TK expression SC_TK error
1699 {yyerror ("Invalid update expression"); RECOVER;}
1700| for_begin SC_TK SC_TK error
1701 {yyerror ("Invalid update expression"); RECOVER;}
1702;
1703
1704for_statement_nsi:
1705 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1706 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1707| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1708 {
b635eb2f 1709 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1710 /* We have not condition, so we get rid of the EXIT_EXPR */
1711 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1712 empty_stmt_node;
e04a16fb
AG
1713 }
1714;
1715
1716for_header:
1717 FOR_TK OP_TK
1718 {
1719 /* This scope defined for local variable that may be
1720 defined within the scope of the for loop */
1721 enter_block ();
1722 }
1723| FOR_TK error
1724 {yyerror ("'(' expected"); DRECOVER(for_1);}
1725| FOR_TK OP_TK error
1726 {yyerror ("Invalid init statement"); RECOVER;}
1727;
1728
1729for_begin:
1730 for_header for_init
1731 {
1732 /* We now declare the loop body. The loop is
1733 declared as a for loop. */
1734 tree body = build_loop_body (0, NULL_TREE, 0);
1735 $$ = build_new_loop (body);
c2952b01 1736 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1737 /* The loop is added to the current block the for
1738 statement is defined within */
1739 java_method_add_stmt (current_function_decl, $$);
1740 }
1741;
1742for_init: /* Can be empty */
9bbc7d9f 1743 { $$ = empty_stmt_node; }
e04a16fb
AG
1744| statement_expression_list
1745 {
1746 /* Init statement recorded within the previously
1747 defined block scope */
1748 $$ = java_method_add_stmt (current_function_decl, $1);
1749 }
1750| local_variable_declaration
1751 {
1752 /* Local variable are recorded within the previously
1753 defined block scope */
1754 $$ = NULL_TREE;
1755 }
1756| statement_expression_list error
1757 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1758;
1759
1760for_update: /* Can be empty */
9bbc7d9f 1761 {$$ = empty_stmt_node;}
e04a16fb
AG
1762| statement_expression_list
1763 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1764;
1765
1766statement_expression_list:
1767 statement_expression
1768 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1769| statement_expression_list C_TK statement_expression
1770 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1771| statement_expression_list C_TK error
1772 {yyerror ("Missing term"); RECOVER;}
1773;
1774
1775break_statement:
1776 BREAK_TK SC_TK
1777 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1778| BREAK_TK identifier SC_TK
1779 { $$ = build_bc_statement ($1.location, 1, $2); }
1780| BREAK_TK error
1781 {yyerror ("Missing term"); RECOVER;}
1782| BREAK_TK identifier error
1783 {yyerror ("';' expected"); RECOVER;}
1784;
1785
1786continue_statement:
1787 CONTINUE_TK SC_TK
1788 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1789| CONTINUE_TK identifier SC_TK
1790 { $$ = build_bc_statement ($1.location, 0, $2); }
1791| CONTINUE_TK error
1792 {yyerror ("Missing term"); RECOVER;}
1793| CONTINUE_TK identifier error
1794 {yyerror ("';' expected"); RECOVER;}
1795;
1796
1797return_statement:
1798 RETURN_TK SC_TK
1799 { $$ = build_return ($1.location, NULL_TREE); }
1800| RETURN_TK expression SC_TK
1801 { $$ = build_return ($1.location, $2); }
1802| RETURN_TK error
1803 {yyerror ("Missing term"); RECOVER;}
1804| RETURN_TK expression error
1805 {yyerror ("';' expected"); RECOVER;}
1806;
1807
1808throw_statement:
1809 THROW_TK expression SC_TK
b9f7e36c
APB
1810 {
1811 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1812 EXPR_WFL_LINECOL ($$) = $1.location;
1813 }
e04a16fb
AG
1814| THROW_TK error
1815 {yyerror ("Missing term"); RECOVER;}
1816| THROW_TK expression error
1817 {yyerror ("';' expected"); RECOVER;}
1818;
1819
1820synchronized_statement:
1821 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1822 {
1823 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1824 EXPR_WFL_LINECOL ($$) =
1825 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1826 }
e04a16fb
AG
1827| synchronized OP_TK expression CP_TK error
1828 {yyerror ("'{' expected"); RECOVER;}
1829| synchronized error
1830 {yyerror ("'(' expected"); RECOVER;}
1831| synchronized OP_TK error CP_TK
1832 {yyerror ("Missing term"); RECOVER;}
1833| synchronized OP_TK error
1834 {yyerror ("Missing term"); RECOVER;}
1835;
1836
b9f7e36c 1837synchronized:
efa0a23f 1838 modifiers
e04a16fb 1839 {
781b0558
KG
1840 check_modifiers (
1841 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1842 $1, ACC_SYNCHRONIZED);
1843 if ($1 != ACC_SYNCHRONIZED)
1844 MODIFIER_WFL (SYNCHRONIZED_TK) =
1845 build_wfl_node (NULL_TREE);
e04a16fb
AG
1846 }
1847;
1848
1849try_statement:
1850 TRY_TK block catches
a7d8d81f 1851 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1852| TRY_TK block finally
a7d8d81f 1853 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1854| TRY_TK block catches finally
2aa11e97
APB
1855 { $$ = build_try_finally_statement
1856 ($1.location, build_try_statement ($1.location,
1857 $2, $3), $4);
1858 }
e04a16fb
AG
1859| TRY_TK error
1860 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1861;
1862
1863catches:
1864 catch_clause
1865| catches catch_clause
b67d701b
PB
1866 {
1867 TREE_CHAIN ($2) = $1;
1868 $$ = $2;
1869 }
e04a16fb
AG
1870;
1871
1872catch_clause:
b67d701b
PB
1873 catch_clause_parameter block
1874 {
1875 java_method_add_stmt (current_function_decl, $2);
1876 exit_block ();
1877 $$ = $1;
1878 }
1879
1880catch_clause_parameter:
1881 CATCH_TK OP_TK formal_parameter CP_TK
1882 {
1883 /* We add a block to define a scope for
1884 formal_parameter (CCBP). The formal parameter is
1885 declared initialized by the appropriate function
1886 call */
1887 tree ccpb = enter_block ();
1888 tree init = build_assignment (ASSIGN_TK, $2.location,
1889 TREE_PURPOSE ($3),
1890 soft_exceptioninfo_call_node);
1891 declare_local_variables (0, TREE_VALUE ($3),
1892 build_tree_list (TREE_PURPOSE ($3),
1893 init));
1894 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1895 EXPR_WFL_LINECOL ($$) = $1.location;
1896 }
e04a16fb 1897| CATCH_TK error
97f30284 1898 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1899| CATCH_TK OP_TK error
97f30284
APB
1900 {
1901 yyerror ("Missing term or ')' expected");
1902 RECOVER; $$ = NULL_TREE;
1903 }
b67d701b 1904| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1905 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1906;
1907
1908finally:
1909 FINALLY_TK block
a7d8d81f 1910 { $$ = $2; }
e04a16fb
AG
1911| FINALLY_TK error
1912 {yyerror ("'{' expected"); RECOVER; }
1913;
1914
1915/* 19.12 Production from 15: Expressions */
1916primary:
1917 primary_no_new_array
1918| array_creation_expression
1919;
1920
1921primary_no_new_array:
1922 literal
1923| THIS_TK
1924 { $$ = build_this ($1.location); }
1925| OP_TK expression CP_TK
1926 {$$ = $2;}
1927| class_instance_creation_expression
1928| field_access
1929| method_invocation
1930| array_access
c2952b01 1931| type_literals
e04a16fb
AG
1932 /* Added, JDK1.1 inner classes. Documentation is wrong
1933 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1934 exist. Used name: instead. */
e04a16fb 1935| name DOT_TK THIS_TK
c2952b01
APB
1936 {
1937 tree wfl = build_wfl_node (this_identifier_node);
1938 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1939 }
e04a16fb
AG
1940| OP_TK expression error
1941 {yyerror ("')' expected"); RECOVER;}
1942| name DOT_TK error
1943 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1944| primitive_type DOT_TK error
1945 {yyerror ("'class' expected" ); RECOVER;}
1946| VOID_TK DOT_TK error
1947 {yyerror ("'class' expected" ); RECOVER;}
1948;
1949
c2952b01
APB
1950type_literals:
1951 name DOT_TK CLASS_TK
1952 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1953| array_type DOT_TK CLASS_TK
c2952b01
APB
1954 { $$ = build_incomplete_class_ref ($2.location, $1); }
1955| primitive_type DOT_TK CLASS_TK
1956 { $$ = build_class_ref ($1); }
1957| VOID_TK DOT_TK CLASS_TK
1958 { $$ = build_class_ref (void_type_node); }
1959;
1960
e04a16fb
AG
1961class_instance_creation_expression:
1962 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1963 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1964| NEW_TK class_type OP_TK CP_TK
b67d701b 1965 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1966| anonymous_class_creation
e04a16fb
AG
1967 /* Added, JDK1.1 inner classes, modified to use name or
1968 primary instead of primary solely which couldn't work in
1969 all situations. */
1970| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1971 {
1972 tree ctor = build_new_invocation ($2, NULL_TREE);
1973 $$ = make_qualified_primary ($1, ctor,
1974 EXPR_WFL_LINECOL ($1));
1975 }
e04a16fb
AG
1976| something_dot_new identifier OP_TK CP_TK class_body
1977| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1978 {
1979 tree ctor = build_new_invocation ($2, $4);
1980 $$ = make_qualified_primary ($1, ctor,
1981 EXPR_WFL_LINECOL ($1));
1982 }
e04a16fb
AG
1983| something_dot_new identifier OP_TK argument_list CP_TK class_body
1984| NEW_TK error SC_TK
1985 {yyerror ("'(' expected"); DRECOVER(new_1);}
1986| NEW_TK class_type error
1987 {yyerror ("'(' expected"); RECOVER;}
1988| NEW_TK class_type OP_TK error
1989 {yyerror ("')' or term expected"); RECOVER;}
1990| NEW_TK class_type OP_TK argument_list error
1991 {yyerror ("')' expected"); RECOVER;}
1992| something_dot_new error
1993 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1994| something_dot_new identifier error
1995 {yyerror ("'(' expected"); RECOVER;}
1996;
1997
c2952b01
APB
1998/* Created after JDK1.1 rules originally added to
1999 class_instance_creation_expression, but modified to use
2000 'class_type' instead of 'TypeName' (type_name) which is mentionned
2001 in the documentation but doesn't exist. */
2002
2003anonymous_class_creation:
2004 NEW_TK class_type OP_TK argument_list CP_TK
2005 { create_anonymous_class ($1.location, $2); }
2006 class_body
2007 {
2008 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2009 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2010
2011 end_class_declaration (1);
2012
2013 /* Now we can craft the new expression */
2014 $$ = build_new_invocation (id, $4);
2015
2016 /* Note that we can't possibly be here if
2017 `class_type' is an interface (in which case the
2018 anonymous class extends Object and implements
2019 `class_type', hence its constructor can't have
2020 arguments.) */
2021
2022 /* Otherwise, the innerclass must feature a
2023 constructor matching `argument_list'. Anonymous
2024 classes are a bit special: it's impossible to
2025 define constructor for them, hence constructors
2026 must be generated following the hints provided by
2027 the `new' expression. Whether a super constructor
2028 of that nature exists or not is to be verified
2029 later on in verify_constructor_super.
2030
2031 It's during the expansion of a `new' statement
2032 refering to an anonymous class that a ctor will
2033 be generated for the anonymous class, with the
2034 right arguments. */
2035
2036 }
2037| NEW_TK class_type OP_TK CP_TK
2038 { create_anonymous_class ($1.location, $2); }
2039 class_body
2040 {
2041 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2042 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2043
2044 end_class_declaration (1);
2045
2046 /* Now we can craft the new expression. The
2047 statement doesn't need to be remember so that a
2048 constructor can be generated, since its signature
2049 is already known. */
2050 $$ = build_new_invocation (id, NULL_TREE);
2051 }
2052;
2053
e04a16fb
AG
2054something_dot_new: /* Added, not part of the specs. */
2055 name DOT_TK NEW_TK
c2952b01 2056 { $$ = $1; }
e04a16fb 2057| primary DOT_TK NEW_TK
c2952b01 2058 { $$ = $1; }
e04a16fb
AG
2059;
2060
2061argument_list:
2062 expression
2063 {
2064 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2065 ctxp->formal_parameter_number = 1;
2066 }
2067| argument_list C_TK expression
2068 {
2069 ctxp->formal_parameter_number += 1;
2070 $$ = tree_cons (NULL_TREE, $3, $1);
2071 }
2072| argument_list C_TK error
2073 {yyerror ("Missing term"); RECOVER;}
2074;
2075
2076array_creation_expression:
2077 NEW_TK primitive_type dim_exprs
2078 { $$ = build_newarray_node ($2, $3, 0); }
2079| NEW_TK class_or_interface_type dim_exprs
2080 { $$ = build_newarray_node ($2, $3, 0); }
2081| NEW_TK primitive_type dim_exprs dims
c7303e41 2082 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2083| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2084 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2085 /* Added, JDK1.1 anonymous array. Initial documentation rule
2086 modified */
2087| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2088 {
2089 char *sig;
c7303e41
APB
2090 int osb = pop_current_osb (ctxp);
2091 while (osb--)
c2952b01
APB
2092 obstack_1grow (&temporary_obstack, '[');
2093 sig = obstack_finish (&temporary_obstack);
2094 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2095 $2, get_identifier (sig), $4);
2096 }
e04a16fb 2097| NEW_TK primitive_type dims array_initializer
c2952b01 2098 {
c7303e41 2099 int osb = pop_current_osb (ctxp);
c2952b01 2100 tree type = $2;
c7303e41 2101 while (osb--)
c2952b01
APB
2102 type = build_java_array_type (type, -1);
2103 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2104 build_pointer_type (type), NULL_TREE, $4);
2105 }
e04a16fb
AG
2106| NEW_TK error CSB_TK
2107 {yyerror ("'[' expected"); DRECOVER ("]");}
2108| NEW_TK error OSB_TK
2109 {yyerror ("']' expected"); RECOVER;}
2110;
2111
2112dim_exprs:
2113 dim_expr
2114 { $$ = build_tree_list (NULL_TREE, $1); }
2115| dim_exprs dim_expr
2116 { $$ = tree_cons (NULL_TREE, $2, $$); }
2117;
2118
2119dim_expr:
2120 OSB_TK expression CSB_TK
2121 {
9a7ab4b3
APB
2122 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2123 {
2124 $2 = build_wfl_node ($2);
2125 TREE_TYPE ($2) = NULL_TREE;
2126 }
e04a16fb
AG
2127 EXPR_WFL_LINECOL ($2) = $1.location;
2128 $$ = $2;
2129 }
2130| OSB_TK expression error
2131 {yyerror ("']' expected"); RECOVER;}
2132| OSB_TK error
2133 {
2134 yyerror ("Missing term");
2135 yyerror ("']' expected");
2136 RECOVER;
2137 }
2138;
2139
2140dims:
2141 OSB_TK CSB_TK
ba179f9f
APB
2142 {
2143 int allocate = 0;
2144 /* If not initialized, allocate memory for the osb
2145 numbers stack */
2146 if (!ctxp->osb_limit)
2147 {
2148 allocate = ctxp->osb_limit = 32;
2149 ctxp->osb_depth = -1;
2150 }
c2952b01 2151 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2152 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2153 allocate = ctxp->osb_limit << 1;
2154
2155 if (allocate)
2156 {
2157 allocate *= sizeof (int);
2158 if (ctxp->osb_number)
2159 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2160 allocate);
2161 else
2162 ctxp->osb_number = (int *)xmalloc (allocate);
2163 }
2164 ctxp->osb_depth++;
2165 CURRENT_OSB (ctxp) = 1;
2166 }
e04a16fb 2167| dims OSB_TK CSB_TK
ba179f9f 2168 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2169| dims OSB_TK error
2170 { yyerror ("']' expected"); RECOVER;}
2171;
2172
2173field_access:
2174 primary DOT_TK identifier
2175 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2176 /* FIXME - REWRITE TO:
2177 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2178| SUPER_TK DOT_TK identifier
2179 {
6e22695a 2180 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2181 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2182 $$ = make_qualified_name (super_wfl, $3, $2.location);
2183 }
2184| SUPER_TK error
2185 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2186;
2187
2188method_invocation:
2189 name OP_TK CP_TK
2190 { $$ = build_method_invocation ($1, NULL_TREE); }
2191| name OP_TK argument_list CP_TK
2192 { $$ = build_method_invocation ($1, $3); }
2193| primary DOT_TK identifier OP_TK CP_TK
2194 {
22eed1e6
APB
2195 if (TREE_CODE ($1) == THIS_EXPR)
2196 $$ = build_this_super_qualified_invocation
2197 (1, $3, NULL_TREE, 0, $2.location);
2198 else
2199 {
2200 tree invok = build_method_invocation ($3, NULL_TREE);
2201 $$ = make_qualified_primary ($1, invok, $2.location);
2202 }
e04a16fb
AG
2203 }
2204| primary DOT_TK identifier OP_TK argument_list CP_TK
2205 {
22eed1e6
APB
2206 if (TREE_CODE ($1) == THIS_EXPR)
2207 $$ = build_this_super_qualified_invocation
2208 (1, $3, $5, 0, $2.location);
2209 else
2210 {
2211 tree invok = build_method_invocation ($3, $5);
2212 $$ = make_qualified_primary ($1, invok, $2.location);
2213 }
e04a16fb
AG
2214 }
2215| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2216 {
2217 $$ = build_this_super_qualified_invocation
2218 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2219 }
2220| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2221 {
22eed1e6
APB
2222 $$ = build_this_super_qualified_invocation
2223 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2224 }
2225 /* Screws up thing. I let it here until I'm convinced it can
2226 be removed. FIXME
2227| primary DOT_TK error
2228 {yyerror ("'(' expected"); DRECOVER(bad);} */
2229| SUPER_TK DOT_TK error CP_TK
2230 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2231| SUPER_TK DOT_TK error DOT_TK
2232 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2233;
2234
2235array_access:
2236 name OSB_TK expression CSB_TK
2237 { $$ = build_array_ref ($2.location, $1, $3); }
2238| primary_no_new_array OSB_TK expression CSB_TK
2239 { $$ = build_array_ref ($2.location, $1, $3); }
2240| name OSB_TK error
2241 {
2242 yyerror ("Missing term and ']' expected");
2243 DRECOVER(array_access);
2244 }
2245| name OSB_TK expression error
2246 {
2247 yyerror ("']' expected");
2248 DRECOVER(array_access);
2249 }
2250| primary_no_new_array OSB_TK error
2251 {
2252 yyerror ("Missing term and ']' expected");
2253 DRECOVER(array_access);
2254 }
2255| primary_no_new_array OSB_TK expression error
2256 {
2257 yyerror ("']' expected");
2258 DRECOVER(array_access);
2259 }
2260;
2261
2262postfix_expression:
2263 primary
2264| name
2265| post_increment_expression
2266| post_decrement_expression
2267;
2268
2269post_increment_expression:
2270 postfix_expression INCR_TK
2271 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2272;
2273
2274post_decrement_expression:
2275 postfix_expression DECR_TK
2276 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2277;
2278
2279unary_expression:
2280 pre_increment_expression
2281| pre_decrement_expression
2282| PLUS_TK unary_expression
2283 {$$ = build_unaryop ($1.token, $1.location, $2); }
2284| MINUS_TK unary_expression
2285 {$$ = build_unaryop ($1.token, $1.location, $2); }
2286| unary_expression_not_plus_minus
2287| PLUS_TK error
2288 {yyerror ("Missing term"); RECOVER}
2289| MINUS_TK error
2290 {yyerror ("Missing term"); RECOVER}
2291;
2292
2293pre_increment_expression:
2294 INCR_TK unary_expression
2295 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2296| INCR_TK error
2297 {yyerror ("Missing term"); RECOVER}
2298;
2299
2300pre_decrement_expression:
2301 DECR_TK unary_expression
2302 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2303| DECR_TK error
2304 {yyerror ("Missing term"); RECOVER}
2305;
2306
2307unary_expression_not_plus_minus:
2308 postfix_expression
2309| NOT_TK unary_expression
2310 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311| NEG_TK unary_expression
2312 {$$ = build_unaryop ($1.token, $1.location, $2); }
2313| cast_expression
2314| NOT_TK error
2315 {yyerror ("Missing term"); RECOVER}
2316| NEG_TK error
2317 {yyerror ("Missing term"); RECOVER}
2318;
2319
2320cast_expression: /* Error handling here is potentially weak */
2321 OP_TK primitive_type dims CP_TK unary_expression
2322 {
2323 tree type = $2;
c7303e41
APB
2324 int osb = pop_current_osb (ctxp);
2325 while (osb--)
e04a16fb
AG
2326 type = build_java_array_type (type, -1);
2327 $$ = build_cast ($1.location, type, $5);
2328 }
2329| OP_TK primitive_type CP_TK unary_expression
2330 { $$ = build_cast ($1.location, $2, $4); }
2331| OP_TK expression CP_TK unary_expression_not_plus_minus
2332 { $$ = build_cast ($1.location, $2, $4); }
2333| OP_TK name dims CP_TK unary_expression_not_plus_minus
2334 {
49f48c71 2335 const char *ptr;
c7303e41
APB
2336 int osb = pop_current_osb (ctxp);
2337 while (osb--)
e04a16fb
AG
2338 obstack_1grow (&temporary_obstack, '[');
2339 obstack_grow0 (&temporary_obstack,
2340 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2341 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2342 ptr = obstack_finish (&temporary_obstack);
2343 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2344 $$ = build_cast ($1.location, $2, $5);
2345 }
2346| OP_TK primitive_type OSB_TK error
2347 {yyerror ("']' expected, invalid type expression");}
2348| OP_TK error
2349 {
29f8b718 2350 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2351 RECOVER;
2352 }
2353| OP_TK primitive_type dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355| OP_TK primitive_type CP_TK error
2356 {yyerror ("Missing term"); RECOVER;}
2357| OP_TK name dims CP_TK error
2358 {yyerror ("Missing term"); RECOVER;}
2359;
2360
2361multiplicative_expression:
2362 unary_expression
2363| multiplicative_expression MULT_TK unary_expression
2364 {
2365 $$ = build_binop (BINOP_LOOKUP ($2.token),
2366 $2.location, $1, $3);
2367 }
2368| multiplicative_expression DIV_TK unary_expression
2369 {
2370 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2371 $1, $3);
2372 }
2373| multiplicative_expression REM_TK unary_expression
2374 {
2375 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2376 $1, $3);
2377 }
2378| multiplicative_expression MULT_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380| multiplicative_expression DIV_TK error
2381 {yyerror ("Missing term"); RECOVER;}
2382| multiplicative_expression REM_TK error
2383 {yyerror ("Missing term"); RECOVER;}
2384;
2385
2386additive_expression:
2387 multiplicative_expression
2388| additive_expression PLUS_TK multiplicative_expression
2389 {
2390 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2391 $1, $3);
2392 }
2393| additive_expression MINUS_TK multiplicative_expression
2394 {
2395 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2396 $1, $3);
2397 }
2398| additive_expression PLUS_TK error
2399 {yyerror ("Missing term"); RECOVER;}
2400| additive_expression MINUS_TK error
2401 {yyerror ("Missing term"); RECOVER;}
2402;
2403
2404shift_expression:
2405 additive_expression
2406| shift_expression LS_TK additive_expression
2407 {
2408 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2409 $1, $3);
2410 }
2411| shift_expression SRS_TK additive_expression
2412 {
2413 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2414 $1, $3);
2415 }
2416| shift_expression ZRS_TK additive_expression
2417 {
2418 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2419 $1, $3);
2420 }
2421| shift_expression LS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423| shift_expression SRS_TK error
2424 {yyerror ("Missing term"); RECOVER;}
2425| shift_expression ZRS_TK error
2426 {yyerror ("Missing term"); RECOVER;}
2427;
2428
2429relational_expression:
2430 shift_expression
2431| relational_expression LT_TK shift_expression
2432 {
2433 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2434 $1, $3);
2435 }
2436| relational_expression GT_TK shift_expression
2437 {
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439 $1, $3);
2440 }
2441| relational_expression LTE_TK shift_expression
2442 {
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2445 }
2446| relational_expression GTE_TK shift_expression
2447 {
2448 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2449 $1, $3);
2450 }
2451| relational_expression INSTANCEOF_TK reference_type
5e942c50 2452 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2453| relational_expression LT_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455| relational_expression GT_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457| relational_expression LTE_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459| relational_expression GTE_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461| relational_expression INSTANCEOF_TK error
2462 {yyerror ("Invalid reference type"); RECOVER;}
2463;
2464
2465equality_expression:
2466 relational_expression
2467| equality_expression EQ_TK relational_expression
2468 {
2469 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2470 $1, $3);
2471 }
2472| equality_expression NEQ_TK relational_expression
2473 {
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 $1, $3);
2476 }
2477| equality_expression EQ_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2479| equality_expression NEQ_TK error
2480 {yyerror ("Missing term"); RECOVER;}
2481;
2482
2483and_expression:
2484 equality_expression
2485| and_expression AND_TK equality_expression
2486 {
2487 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488 $1, $3);
2489 }
2490| and_expression AND_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2492;
2493
2494exclusive_or_expression:
2495 and_expression
2496| exclusive_or_expression XOR_TK and_expression
2497 {
2498 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2499 $1, $3);
2500 }
2501| exclusive_or_expression XOR_TK error
2502 {yyerror ("Missing term"); RECOVER;}
2503;
2504
2505inclusive_or_expression:
2506 exclusive_or_expression
2507| inclusive_or_expression OR_TK exclusive_or_expression
2508 {
2509 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2510 $1, $3);
2511 }
2512| inclusive_or_expression OR_TK error
2513 {yyerror ("Missing term"); RECOVER;}
2514;
2515
2516conditional_and_expression:
2517 inclusive_or_expression
2518| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2519 {
2520 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2521 $1, $3);
2522 }
2523| conditional_and_expression BOOL_AND_TK error
2524 {yyerror ("Missing term"); RECOVER;}
2525;
2526
2527conditional_or_expression:
2528 conditional_and_expression
2529| conditional_or_expression BOOL_OR_TK conditional_and_expression
2530 {
2531 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2532 $1, $3);
2533 }
2534| conditional_or_expression BOOL_OR_TK error
2535 {yyerror ("Missing term"); RECOVER;}
2536;
2537
2538conditional_expression: /* Error handling here is weak */
2539 conditional_or_expression
2540| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2541 {
2542 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2543 EXPR_WFL_LINECOL ($$) = $2.location;
2544 }
e04a16fb
AG
2545| conditional_or_expression REL_QM_TK REL_CL_TK error
2546 {
2547 YYERROR_NOW;
2548 yyerror ("Missing term");
2549 DRECOVER (1);
2550 }
2551| conditional_or_expression REL_QM_TK error
2552 {yyerror ("Missing term"); DRECOVER (2);}
2553| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2554 {yyerror ("Missing term"); DRECOVER (3);}
2555;
2556
2557assignment_expression:
2558 conditional_expression
2559| assignment
2560;
2561
2562assignment:
2563 left_hand_side assignment_operator assignment_expression
2564 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2565| left_hand_side assignment_operator error
2566 {
29f8b718 2567 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2568 DRECOVER (assign);
2569 }
2570;
2571
2572left_hand_side:
2573 name
2574| field_access
2575| array_access
2576;
2577
2578assignment_operator:
2579 ASSIGN_ANY_TK
2580| ASSIGN_TK
2581;
2582
2583expression:
2584 assignment_expression
2585;
2586
2587constant_expression:
2588 expression
2589;
2590
2591%%
c7303e41
APB
2592
2593/* Helper function to retrieve an OSB count. Should be used when the
2594 `dims:' rule is being used. */
2595
2596static int
2597pop_current_osb (ctxp)
2598 struct parser_ctxt *ctxp;
2599{
2600 int to_return;
2601
2602 if (ctxp->osb_depth < 0)
400500c4 2603 abort ();
c7303e41
APB
2604
2605 to_return = CURRENT_OSB (ctxp);
2606 ctxp->osb_depth--;
2607
2608 return to_return;
2609}
2610
e04a16fb
AG
2611\f
2612
c2952b01
APB
2613/* This section of the code deal with save/restoring parser contexts.
2614 Add mode documentation here. FIXME */
e04a16fb 2615
c2952b01
APB
2616/* Helper function. Create a new parser context. With
2617 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2618 context is copied, otherwise, the new context is zeroed. The newly
2619 created context becomes the current one. */
e04a16fb 2620
c2952b01
APB
2621static void
2622create_new_parser_context (copy_from_previous)
2623 int copy_from_previous;
e04a16fb 2624{
c2952b01 2625 struct parser_ctxt *new;
e04a16fb 2626
c2952b01
APB
2627 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2628 if (copy_from_previous)
2629 {
2630 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2631 new->saved_data_ctx = 1;
2632 }
2633 else
2e09e75a 2634 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2635
e04a16fb
AG
2636 new->next = ctxp;
2637 ctxp = new;
c2952b01
APB
2638}
2639
2640/* Create a new parser context and make it the current one. */
2641
2642void
2643java_push_parser_context ()
2644{
2645 create_new_parser_context (0);
e04a16fb
AG
2646}
2647
c2952b01
APB
2648void
2649java_pop_parser_context (generate)
2650 int generate;
2651{
2652 tree current;
2653 struct parser_ctxt *toFree, *next;
2654
2655 if (!ctxp)
2656 return;
2657
2658 toFree = ctxp;
2659 next = ctxp->next;
2660 if (next)
2661 {
c2952b01 2662 lineno = ctxp->lineno;
19e223db 2663 current_class = ctxp->class_type;
c2952b01
APB
2664 }
2665
d19cbcb5
TT
2666 /* If the old and new lexers differ, then free the old one. */
2667 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2668 java_destroy_lexer (ctxp->lexer);
2669
c2952b01
APB
2670 /* Set the single import class file flag to 0 for the current list
2671 of imported things */
2672 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2673 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2674
2675 /* And restore those of the previous context */
2676 if ((ctxp = next)) /* Assignment is really meant here */
2677 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2678 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2679
2680 /* If we pushed a context to parse a class intended to be generated,
2681 we keep it so we can remember the class. What we could actually
2682 do is to just update a list of class names. */
2683 if (generate)
2684 {
2685 toFree->next = ctxp_for_generation;
2686 ctxp_for_generation = toFree;
2687 }
2688 else
2689 free (toFree);
2690}
2691
2692/* Create a parser context for the use of saving some global
2693 variables. */
2694
e04a16fb
AG
2695void
2696java_parser_context_save_global ()
2697{
22eed1e6
APB
2698 if (!ctxp)
2699 {
2700 java_push_parser_context ();
ee07f4f4
APB
2701 ctxp->saved_data_ctx = 1;
2702 }
c2952b01
APB
2703
2704 /* If this context already stores data, create a new one suitable
2705 for data storage. */
ee07f4f4 2706 else if (ctxp->saved_data)
c2952b01
APB
2707 create_new_parser_context (1);
2708
e04a16fb 2709 ctxp->lineno = lineno;
19e223db 2710 ctxp->class_type = current_class;
e04a16fb 2711 ctxp->filename = input_filename;
19e223db 2712 ctxp->function_decl = current_function_decl;
ee07f4f4 2713 ctxp->saved_data = 1;
e04a16fb
AG
2714}
2715
c2952b01
APB
2716/* Restore some global variables from the previous context. Make the
2717 previous context the current one. */
2718
e04a16fb
AG
2719void
2720java_parser_context_restore_global ()
2721{
e04a16fb 2722 lineno = ctxp->lineno;
19e223db 2723 current_class = ctxp->class_type;
e04a16fb 2724 input_filename = ctxp->filename;
dba41d30
APB
2725 if (wfl_operator)
2726 {
2727 tree s;
2728 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2729 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2730 }
19e223db 2731 current_function_decl = ctxp->function_decl;
c2952b01 2732 ctxp->saved_data = 0;
ee07f4f4
APB
2733 if (ctxp->saved_data_ctx)
2734 java_pop_parser_context (0);
e04a16fb
AG
2735}
2736
c2952b01
APB
2737/* Suspend vital data for the current class/function being parsed so
2738 that an other class can be parsed. Used to let local/anonymous
2739 classes be parsed. */
2740
2741static void
2742java_parser_context_suspend ()
e04a16fb 2743{
c2952b01 2744 /* This makes debugging through java_debug_context easier */
3b304f5b 2745 static const char *name = "<inner buffer context>";
e04a16fb 2746
c2952b01
APB
2747 /* Duplicate the previous context, use it to save the globals we're
2748 interested in */
2749 create_new_parser_context (1);
19e223db
MM
2750 ctxp->function_decl = current_function_decl;
2751 ctxp->class_type = current_class;
5e942c50 2752
c2952b01
APB
2753 /* Then create a new context which inherits all data from the
2754 previous one. This will be the new current context */
2755 create_new_parser_context (1);
2756
2757 /* Help debugging */
2758 ctxp->next->filename = name;
2759}
2760
2761/* Resume vital data for the current class/function being parsed so
2762 that an other class can be parsed. Used to let local/anonymous
2763 classes be parsed. The trick is the data storing file position
2764 informations must be restored to their current value, so parsing
2765 can resume as if no context was ever saved. */
2766
2767static void
2768java_parser_context_resume ()
2769{
2770 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2771 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2772 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2773
2774 /* We need to inherit the list of classes to complete/generate */
c2952b01
APB
2775 restored->classd_list = old->classd_list;
2776 restored->class_list = old->class_list;
2777
2778 /* Restore the current class and function from the saver */
19e223db
MM
2779 current_class = saver->class_type;
2780 current_function_decl = saver->function_decl;
c2952b01
APB
2781
2782 /* Retrive the restored context */
2783 ctxp = restored;
2784
2785 /* Re-installed the data for the parsing to carry on */
2786 bcopy (&old->marker_begining, &ctxp->marker_begining,
2787 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2788
2789 /* Buffer context can now be discarded */
2790 free (saver);
2791 free (old);
2792}
2793
2794/* Add a new anchor node to which all statement(s) initializing static
2795 and non static initialized upon declaration field(s) will be
2796 linked. */
2797
2798static void
2799java_parser_context_push_initialized_field ()
2800{
2801 tree node;
2802
2803 node = build_tree_list (NULL_TREE, NULL_TREE);
2804 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2805 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2806
2807 node = build_tree_list (NULL_TREE, NULL_TREE);
2808 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2809 CPC_INITIALIZER_LIST (ctxp) = node;
2810
2811 node = build_tree_list (NULL_TREE, NULL_TREE);
2812 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2813 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2814}
2815
2816/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2817 remember them so we can use it to create and populate the finit$
c2952b01
APB
2818 or <clinit> functions. */
2819
2820static void
2821java_parser_context_pop_initialized_field ()
2822{
2823 tree stmts;
2824 tree class_type = TREE_TYPE (GET_CPC ());
2825
2826 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2827 {
c2952b01
APB
2828 stmts = CPC_INITIALIZER_STMT (ctxp);
2829 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2830 if (stmts && !java_error_count)
2831 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2832 }
2833
c2952b01
APB
2834 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2835 {
2836 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2837 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2838 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2839 /* Keep initialization in order to enforce 8.5 */
2840 if (stmts && !java_error_count)
2841 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2842 }
e04a16fb 2843
c2952b01
APB
2844 /* JDK 1.1 instance initializers */
2845 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2846 {
c2952b01
APB
2847 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2848 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2849 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2850 if (stmts && !java_error_count)
2851 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2852 }
c2952b01
APB
2853}
2854
2855static tree
2856reorder_static_initialized (list)
2857 tree list;
2858{
2859 /* We have to keep things in order. The alias initializer have to
2860 come first, then the initialized regular field, in reverse to
2861 keep them in lexical order. */
2862 tree marker, previous = NULL_TREE;
2863 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2864 if (TREE_CODE (marker) == TREE_LIST
2865 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2866 break;
2867
2868 /* No static initialized, the list is fine as is */
2869 if (!previous)
2870 list = TREE_CHAIN (marker);
2871
2872 /* No marker? reverse the whole list */
2873 else if (!marker)
2874 list = nreverse (list);
2875
2876 /* Otherwise, reverse what's after the marker and the new reordered
2877 sublist will replace the marker. */
b351b287 2878 else
c2952b01
APB
2879 {
2880 TREE_CHAIN (previous) = NULL_TREE;
2881 list = nreverse (list);
2882 list = chainon (TREE_CHAIN (marker), list);
2883 }
2884 return list;
e04a16fb
AG
2885}
2886
c2952b01
APB
2887/* Helper functions to dump the parser context stack. */
2888
2889#define TAB_CONTEXT(C) \
2890 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2891
2892static void
2893java_debug_context_do (tab)
2894 int tab;
2895{
ee07f4f4
APB
2896 struct parser_ctxt *copy = ctxp;
2897 while (copy)
2898 {
c2952b01 2899 TAB_CONTEXT (tab);
ee07f4f4 2900 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2901 TAB_CONTEXT (tab);
ee07f4f4 2902 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2903 TAB_CONTEXT (tab);
2904 fprintf (stderr, "lineno: %d\n", copy->lineno);
2905 TAB_CONTEXT (tab);
ee07f4f4
APB
2906 fprintf (stderr, "package: %s\n",
2907 (copy->package ?
2908 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2909 TAB_CONTEXT (tab);
ee07f4f4 2910 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2911 TAB_CONTEXT (tab);
ee07f4f4
APB
2912 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2913 copy = copy->next;
2914 tab += 2;
2915 }
ee07f4f4
APB
2916}
2917
c2952b01
APB
2918/* Dump the stacked up parser contexts. Intended to be called from a
2919 debugger. */
2920
ee07f4f4
APB
2921void
2922java_debug_context ()
2923{
2924 java_debug_context_do (0);
2925}
2926
c2952b01
APB
2927\f
2928
2929/* Flag for the error report routine to issue the error the first time
2930 it's called (overriding the default behavior which is to drop the
2931 first invocation and honor the second one, taking advantage of a
2932 richer context. */
2933static int force_error = 0;
ee07f4f4 2934
8119c720
APB
2935/* Reporting an constructor invocation error. */
2936static void
2937parse_ctor_invocation_error ()
2938{
2939 if (DECL_CONSTRUCTOR_P (current_function_decl))
2940 yyerror ("Constructor invocation must be first thing in a constructor");
2941 else
2942 yyerror ("Only constructors can invoke constructors");
2943}
2944
2945/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2946
2947static tree
2948parse_jdk1_1_error (msg)
49f48c71 2949 const char *msg;
b67d701b
PB
2950{
2951 sorry (": `%s' JDK1.1(TM) feature", msg);
2952 java_error_count++;
9bbc7d9f 2953 return empty_stmt_node;
b67d701b
PB
2954}
2955
e04a16fb
AG
2956static int do_warning = 0;
2957
2958void
2959yyerror (msg)
49f48c71 2960 const char *msg;
e04a16fb
AG
2961{
2962 static java_lc elc;
2963 static int prev_lineno;
49f48c71 2964 static const char *prev_msg;
e04a16fb 2965
0a2138e2 2966 int save_lineno;
e04a16fb 2967 char *remainder, *code_from_source;
e04a16fb
AG
2968
2969 if (!force_error && prev_lineno == lineno)
2970 return;
2971
2972 /* Save current error location but report latter, when the context is
2973 richer. */
2974 if (ctxp->java_error_flag == 0)
2975 {
2976 ctxp->java_error_flag = 1;
2977 elc = ctxp->elc;
2978 /* Do something to use the previous line if we're reaching the
2979 end of the file... */
2980#ifdef VERBOSE_SKELETON
2981 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2982#endif
2983 return;
2984 }
2985
2986 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2987 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2988 return;
2989
2990 ctxp->java_error_flag = 0;
2991 if (do_warning)
2992 java_warning_count++;
2993 else
2994 java_error_count++;
2995
807bc1db 2996 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2997 {
2998 elc.col = ctxp->p_line->char_col-1;
2999 elc.line = ctxp->p_line->lineno;
3000 }
3001
3002 save_lineno = lineno;
3003 prev_lineno = lineno = elc.line;
3004 prev_msg = msg;
3005
3006 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3007 obstack_grow0 (&temporary_obstack,
3008 code_from_source, strlen (code_from_source));
3009 remainder = obstack_finish (&temporary_obstack);
3010 if (do_warning)
3011 warning ("%s.\n%s", msg, remainder);
3012 else
3013 error ("%s.\n%s", msg, remainder);
3014
3015 /* This allow us to cheaply avoid an extra 'Invalid expression
3016 statement' error report when errors have been already reported on
3017 the same line. This occurs when we report an error but don't have
3018 a synchronization point other than ';', which
3019 expression_statement is the only one to take care of. */
3020 ctxp->prevent_ese = lineno = save_lineno;
3021}
3022
3023static void
15fdcfe9 3024issue_warning_error_from_context (cl, msg, ap)
5e942c50 3025 tree cl;
d4476be2 3026 const char *msg;
15fdcfe9 3027 va_list ap;
5e942c50 3028{
3b304f5b 3029 const char *saved, *saved_input_filename;
15fdcfe9
PB
3030 char buffer [4096];
3031 vsprintf (buffer, msg, ap);
3032 force_error = 1;
5e942c50
APB
3033
3034 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3035 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3036 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3037
3038 /* We have a CL, that's a good reason for using it if it contains data */
3039 saved = ctxp->filename;
3040 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3041 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3042 saved_input_filename = input_filename;
3043 input_filename = ctxp->filename;
15fdcfe9
PB
3044 java_error (NULL);
3045 java_error (buffer);
5e942c50 3046 ctxp->filename = saved;
1886c9d8 3047 input_filename = saved_input_filename;
15fdcfe9 3048 force_error = 0;
5e942c50
APB
3049}
3050
e04a16fb
AG
3051/* Issue an error message at a current source line CL */
3052
15fdcfe9 3053void
df32d2ce 3054parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3055{
d4476be2 3056#ifndef ANSI_PROTOTYPES
e04a16fb 3057 tree cl;
d4476be2 3058 const char *msg;
e04a16fb 3059#endif
e04a16fb
AG
3060 va_list ap;
3061
3062 VA_START (ap, msg);
d4476be2 3063#ifndef ANSI_PROTOTYPES
e04a16fb 3064 cl = va_arg (ap, tree);
d4476be2 3065 msg = va_arg (ap, const char *);
e04a16fb 3066#endif
15fdcfe9
PB
3067 issue_warning_error_from_context (cl, msg, ap);
3068 va_end (ap);
e04a16fb
AG
3069}
3070
3071/* Issue a warning at a current source line CL */
3072
3073static void
df32d2ce 3074parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3075{
d4476be2 3076#ifndef ANSI_PROTOTYPES
e04a16fb 3077 tree cl;
d4476be2 3078 const char *msg;
e04a16fb 3079#endif
e04a16fb
AG
3080 va_list ap;
3081
3082 VA_START (ap, msg);
d4476be2 3083#ifndef ANSI_PROTOTYPES
e04a16fb 3084 cl = va_arg (ap, tree);
d4476be2 3085 msg = va_arg (ap, const char *);
e04a16fb 3086#endif
e04a16fb 3087
c877974e 3088 force_error = do_warning = 1;
15fdcfe9 3089 issue_warning_error_from_context (cl, msg, ap);
c877974e 3090 do_warning = force_error = 0;
15fdcfe9 3091 va_end (ap);
e04a16fb
AG
3092}
3093
82371d41
APB
3094static tree
3095find_expr_with_wfl (node)
3096 tree node;
3097{
3098 while (node)
3099 {
3100 char code;
3101 tree to_return;
3102
3103 switch (TREE_CODE (node))
3104 {
3105 case BLOCK:
c0d87ff6
PB
3106 node = BLOCK_EXPR_BODY (node);
3107 continue;
82371d41
APB
3108
3109 case COMPOUND_EXPR:
3110 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3111 if (to_return)
3112 return to_return;
c0d87ff6
PB
3113 node = TREE_OPERAND (node, 1);
3114 continue;
82371d41
APB
3115
3116 case LOOP_EXPR:
c0d87ff6
PB
3117 node = TREE_OPERAND (node, 0);
3118 continue;
82371d41
APB
3119
3120 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3121 node = TREE_OPERAND (node, 1);
3122 continue;
3123
82371d41
APB
3124 default:
3125 code = TREE_CODE_CLASS (TREE_CODE (node));
3126 if (((code == '1') || (code == '2') || (code == 'e'))
3127 && EXPR_WFL_LINECOL (node))
3128 return node;
ba179f9f 3129 return NULL_TREE;
82371d41
APB
3130 }
3131 }
3132 return NULL_TREE;
3133}
3134
3135/* Issue a missing return statement error. Uses METHOD to figure the
3136 last line of the method the error occurs in. */
3137
3138static void
3139missing_return_error (method)
3140 tree method;
3141{
3142 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3143 parse_error_context (wfl_operator, "Missing return statement");
3144}
3145
3146/* Issue an unreachable statement error. From NODE, find the next
3147 statement to report appropriately. */
3148static void
3149unreachable_stmt_error (node)
3150 tree node;
3151{
3152 /* Browse node to find the next expression node that has a WFL. Use
3153 the location to report the error */
3154 if (TREE_CODE (node) == COMPOUND_EXPR)
3155 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3156 else
3157 node = find_expr_with_wfl (node);
3158
3159 if (node)
3160 {
3161 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3162 parse_error_context (wfl_operator, "Unreachable statement");
3163 }
3164 else
400500c4 3165 abort ();
82371d41
APB
3166}
3167
c877974e 3168int
e04a16fb
AG
3169java_report_errors ()
3170{
3171 if (java_error_count)
3172 fprintf (stderr, "%d error%s",
3173 java_error_count, (java_error_count == 1 ? "" : "s"));
3174 if (java_warning_count)
3175 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3176 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3177 if (java_error_count || java_warning_count)
3178 putc ('\n', stderr);
c877974e 3179 return java_error_count;
e04a16fb
AG
3180}
3181
3182static char *
3183java_accstring_lookup (flags)
3184 int flags;
3185{
3186 static char buffer [80];
3187#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3188
3189 /* Access modifier looked-up first for easier report on forbidden
3190 access. */
3191 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3192 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3193 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3194 if (flags & ACC_STATIC) COPY_RETURN ("static");
3195 if (flags & ACC_FINAL) COPY_RETURN ("final");
3196 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3197 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3198 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3199 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3200 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3201 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3202
3203 buffer [0] = '\0';
3204 return buffer;
3205#undef COPY_RETURN
3206}
3207
b67d701b
PB
3208/* Issuing error messages upon redefinition of classes, interfaces or
3209 variables. */
3210
e04a16fb 3211static void
b67d701b 3212classitf_redefinition_error (context, id, decl, cl)
49f48c71 3213 const char *context;
e04a16fb
AG
3214 tree id, decl, cl;
3215{
3216 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3217 context, IDENTIFIER_POINTER (id),
3218 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3219 /* Here we should point out where its redefined. It's a unicode. FIXME */
3220}
3221
b67d701b
PB
3222static void
3223variable_redefinition_error (context, name, type, line)
3224 tree context, name, type;
3225 int line;
3226{
49f48c71 3227 const char *type_name;
b67d701b
PB
3228
3229 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3230 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3231 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3232 else
0a2138e2 3233 type_name = lang_printable_name (type, 0);
b67d701b
PB
3234
3235 parse_error_context (context,
781b0558 3236 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3237 IDENTIFIER_POINTER (name),
3238 type_name, IDENTIFIER_POINTER (name), line);
3239}
3240
c583dd46
APB
3241static tree
3242build_array_from_name (type, type_wfl, name, ret_name)
3243 tree type, type_wfl, name, *ret_name;
3244{
3245 int more_dims = 0;
49f48c71 3246 const char *string;
c583dd46
APB
3247
3248 /* Eventually get more dims */
3249 string = IDENTIFIER_POINTER (name);
3250 while (string [more_dims] == '[')
3251 more_dims++;
3252
3253 /* If we have, then craft a new type for this variable */
3254 if (more_dims)
3255 {
c0d87ff6 3256 name = get_identifier (&string [more_dims]);
c583dd46 3257
34f4db93
APB
3258 /* If we have a pointer, use its type */
3259 if (TREE_CODE (type) == POINTER_TYPE)
3260 type = TREE_TYPE (type);
c583dd46
APB
3261
3262 /* Building the first dimension of a primitive type uses this
3263 function */
3264 if (JPRIMITIVE_TYPE_P (type))
3265 {
3266 type = build_java_array_type (type, -1);
22eed1e6 3267 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3268 more_dims--;
3269 }
3270 /* Otherwise, if we have a WFL for this type, use it (the type
3271 is already an array on an unresolved type, and we just keep
3272 on adding dimensions) */
3273 else if (type_wfl)
3274 type = type_wfl;
3275
3276 /* Add all the dimensions */
3277 while (more_dims--)
3278 type = build_unresolved_array_type (type);
3279
3280 /* The type may have been incomplete in the first place */
3281 if (type_wfl)
3282 type = obtain_incomplete_type (type);
3283 }
3284
c2952b01
APB
3285 if (ret_name)
3286 *ret_name = name;
c583dd46
APB
3287 return type;
3288}
3289
e04a16fb
AG
3290/* Build something that the type identifier resolver will identify as
3291 being an array to an unresolved type. TYPE_WFL is a WFL on a
3292 identifier. */
3293
3294static tree
3295build_unresolved_array_type (type_or_wfl)
3296 tree type_or_wfl;
3297{
49f48c71 3298 const char *ptr;
e04a16fb 3299
1886c9d8 3300 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3301 just create a array type */
3302 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3303 {
3304 tree type = build_java_array_type (type_or_wfl, -1);
3305 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3306 return type;
3307 }
3308
3309 obstack_1grow (&temporary_obstack, '[');
3310 obstack_grow0 (&temporary_obstack,
3311 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3312 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3313 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3314 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3315 return type_or_wfl;
e04a16fb
AG
3316}
3317
e04a16fb
AG
3318static void
3319parser_add_interface (class_decl, interface_decl, wfl)
3320 tree class_decl, interface_decl, wfl;
3321{
3322 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3323 parse_error_context (wfl, "Interface `%s' repeated",
3324 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3325}
3326
3327/* Bulk of common class/interface checks. Return 1 if an error was
3328 encountered. TAG is 0 for a class, 1 for an interface. */
3329
3330static int
3331check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3332 int is_interface, flags;
3333 tree raw_name, qualified_name, decl, cl;
3334{
3335 tree node;
c2952b01
APB
3336 int sca = 0; /* Static class allowed */
3337 int icaf = 0; /* Inner class allowed flags */
3338 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3339
3340 if (!quiet_flag)
c2952b01
APB
3341 fprintf (stderr, " %s%s %s",
3342 (CPC_INNER_P () ? "inner" : ""),
3343 (is_interface ? "interface" : "class"),
e04a16fb
AG
3344 IDENTIFIER_POINTER (qualified_name));
3345
3346 /* Scope of an interface/class type name:
3347 - Can't be imported by a single type import
3348 - Can't already exists in the package */
3349 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3350 && (node = find_name_in_single_imports (raw_name))
3351 && !CPC_INNER_P ())
e04a16fb
AG
3352 {
3353 parse_error_context
3354 (cl, "%s name `%s' clashes with imported type `%s'",
3355 (is_interface ? "Interface" : "Class"),
3356 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3357 return 1;
3358 }
3359 if (decl && CLASS_COMPLETE_P (decl))
3360 {
b67d701b
PB
3361 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3362 qualified_name, decl, cl);
e04a16fb
AG
3363 return 1;
3364 }
3365
c2952b01
APB
3366 if (check_inner_class_redefinition (raw_name, cl))
3367 return 1;
3368
3369 /* If public, file name should match class/interface name, except
3370 when dealing with an inner class */
3371 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3372 {
49f48c71 3373 const char *f;
e04a16fb
AG
3374
3375 /* Contains OS dependent assumption on path separator. FIXME */
3376 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3377 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3378 f--)
3379 ;
847fe791 3380 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3381 f++;
3382 if (strncmp (IDENTIFIER_POINTER (raw_name),
3383 f , IDENTIFIER_LENGTH (raw_name)) ||
3384 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3385 parse_error_context
3386 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3387 (is_interface ? "interface" : "class"),
3388 IDENTIFIER_POINTER (qualified_name),
3389 IDENTIFIER_POINTER (raw_name));
3390 }
3391
c2952b01
APB
3392 /* Static classes can be declared only in top level classes. Note:
3393 once static, a inner class is a top level class. */
3394 if (flags & ACC_STATIC)
3395 {
3396 /* Catch the specific error of declaring an class inner class
3397 with no toplevel enclosing class. Prevent check_modifiers from
3398 complaining a second time */
3399 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3400 {
3401 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3402 IDENTIFIER_POINTER (qualified_name));
3403 sca = ACC_STATIC;
3404 }
3405 /* Else, in the context of a top-level class declaration, let
3406 `check_modifiers' do its job, otherwise, give it a go */
3407 else
3408 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3409 }
3410
a40d21da 3411 /* Inner classes can be declared private or protected
c2952b01
APB
3412 within their enclosing classes. */
3413 if (CPC_INNER_P ())
3414 {
3415 /* A class which is local to a block can't be public, private,
3416 protected or static. But it is created final, so allow this
3417 one. */
3418 if (current_function_decl)
3419 icaf = sca = uaaf = ACC_FINAL;
3420 else
3421 {
3422 check_modifiers_consistency (flags);
3423 icaf = ACC_PRIVATE|ACC_PROTECTED;
3424 }
3425 }
3426
a40d21da
APB
3427 if (is_interface)
3428 {
3429 if (CPC_INNER_P ())
3430 uaaf = INTERFACE_INNER_MODIFIERS;
3431 else
3432 uaaf = INTERFACE_MODIFIERS;
3433
3434 check_modifiers ("Illegal modifier `%s' for interface declaration",
3435 flags, uaaf);
3436 }
2884c41e 3437 else
a40d21da
APB
3438 check_modifiers ((current_function_decl ?
3439 "Illegal modifier `%s' for local class declaration" :
3440 "Illegal modifier `%s' for class declaration"),
c2952b01 3441 flags, uaaf|sca|icaf);
e04a16fb
AG
3442 return 0;
3443}
3444
c2952b01
APB
3445static void
3446make_nested_class_name (cpc_list)
3447 tree cpc_list;
3448{
3449 tree name;
3450
3451 if (!cpc_list)
3452 return;
3453 else
3454 make_nested_class_name (TREE_CHAIN (cpc_list));
3455
3456 /* Pick the qualified name when dealing with the first upmost
3457 enclosing class */
3458 name = (TREE_CHAIN (cpc_list) ?
3459 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3460 obstack_grow (&temporary_obstack,
3461 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3462 /* Why is NO_DOLLAR_IN_LABEL defined? */
3463#if 0
3464#ifdef NO_DOLLAR_IN_LABEL
400500c4 3465 internal_error ("Can't use '$' as a separator for inner classes");
c2952b01
APB
3466#endif
3467#endif
3468 obstack_1grow (&temporary_obstack, '$');
3469}
3470
3471/* Can't redefine a class already defined in an earlier scope. */
3472
3473static int
3474check_inner_class_redefinition (raw_name, cl)
3475 tree raw_name, cl;
3476{
3477 tree scope_list;
3478
3479 for (scope_list = GET_CPC_LIST (); scope_list;
3480 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3481 if (raw_name == GET_CPC_UN_NODE (scope_list))
3482 {
3483 parse_error_context
3484 (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3485 IDENTIFIER_POINTER (raw_name));
3486 return 1;
3487 }
3488 return 0;
3489}
3490
3491static tree
3492find_as_inner_class (enclosing, name, cl)
3493 tree enclosing, name, cl;
3494{
3495 tree qual, to_return;
3496 if (!enclosing)
3497 return NULL_TREE;
3498
3499 name = TYPE_NAME (name);
3500
3501 /* First search: within the scope of `enclosing', search for name */
3502 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3503 qual = EXPR_WFL_QUALIFICATION (cl);
3504 else if (cl)
3505 qual = build_tree_list (cl, NULL_TREE);
3506 else
3507 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3508
3509 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3510 return to_return;
3511
3512 /* We're dealing with a qualified name. Try to resolve thing until
3513 we get something that is an enclosing class. */
3514 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3515 {
3516 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3517
0c2b8145
APB
3518 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3519 qual = TREE_CHAIN (qual))
c2952b01
APB
3520 {
3521 acc = merge_qualified_name (acc,
3522 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3523 BUILD_PTR_FROM_NAME (ptr, acc);
3524 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3525 }
3526
3527 /* A NULL qual and a decl means that the search ended
3528 successfully?!? We have to do something then. FIXME */
3529
3530 if (decl)
3531 enclosing = decl;
3532 else
3533 qual = EXPR_WFL_QUALIFICATION (cl);
3534 }
3535 /* Otherwise, create a qual for the other part of the resolution. */
3536 else
3537 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3538
1e12ab9b 3539 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3540}
3541
3542/* We go inside the list of sub classes and try to find a way
3543 through. */
3544
3545static tree
3546find_as_inner_class_do (qual, enclosing)
3547 tree qual, enclosing;
3548{
3549 if (!qual)
3550 return NULL_TREE;
3551
3552 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3553 {
3554 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3555 tree next_enclosing = NULL_TREE;
3556 tree inner_list;
3557
3558 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3559 inner_list; inner_list = TREE_CHAIN (inner_list))
3560 {
3561 if (TREE_VALUE (inner_list) == name_to_match)
3562 {
3563 next_enclosing = TREE_PURPOSE (inner_list);
3564 break;
3565 }
3566 }
3567 enclosing = next_enclosing;
3568 }
3569
3570 return (!qual && enclosing ? enclosing : NULL_TREE);
3571}
3572
3573/* Reach all inner classes and tie their unqualified name to a
3574 DECL. */
3575
3576static void
3577set_nested_class_simple_name_value (outer, set)
3578 tree outer;
3579 int set;
3580{
3581 tree l;
3582
3583 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3584 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3585 TREE_PURPOSE (l) : NULL_TREE);
3586}
3587
3588static void
3589link_nested_class_to_enclosing ()
3590{
3591 if (GET_ENCLOSING_CPC ())
3592 {
3593 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3594 DECL_INNER_CLASS_LIST (enclosing) =
3595 tree_cons (GET_CPC (), GET_CPC_UN (),
3596 DECL_INNER_CLASS_LIST (enclosing));
3597 enclosing = enclosing;
3598 }
3599}
3600
3601static tree
3602maybe_make_nested_class_name (name)
3603 tree name;
3604{
3605 tree id = NULL_TREE;
3606
3607 if (CPC_INNER_P ())
3608 {
3609 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3610 obstack_grow0 (&temporary_obstack,
3611 IDENTIFIER_POINTER (name),
3612 IDENTIFIER_LENGTH (name));
c2952b01
APB
3613 id = get_identifier (obstack_finish (&temporary_obstack));
3614 if (ctxp->package)
3615 QUALIFIED_P (id) = 1;
3616 }
3617 return id;
3618}
3619
3620/* If DECL is NULL, create and push a new DECL, record the current
3621 line CL and do other maintenance things. */
3622
e04a16fb 3623static tree
c2952b01
APB
3624maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3625 tree decl, raw_name, qualified_name, cl;
e04a16fb 3626{
5e942c50 3627 if (!decl)
e04a16fb 3628 decl = push_class (make_class (), qualified_name);
c2952b01 3629
e04a16fb
AG
3630 /* Take care of the file and line business */
3631 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3632 /* If we're emiting xrefs, store the line/col number information */
3633 if (flag_emit_xref)
3634 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3635 else
3636 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3637 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3638 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3639 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3640
c2952b01
APB
3641 PUSH_CPC (decl, raw_name);
3642 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3643
e04a16fb
AG
3644 /* Link the declaration to the already seen ones */
3645 TREE_CHAIN (decl) = ctxp->class_list;
3646 ctxp->class_list = decl;
5e942c50 3647
23a79c61 3648 /* Create a new nodes in the global lists */
fea2d5da 3649 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
23a79c61 3650 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3651
e04a16fb
AG
3652 /* Install a new dependency list element */
3653 create_jdep_list (ctxp);
3654
3655 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3656 IDENTIFIER_POINTER (qualified_name)));
3657 return decl;
3658}
3659
3660static void
3661add_superinterfaces (decl, interface_list)
3662 tree decl, interface_list;
3663{
3664 tree node;
3665 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3666 takes care of ensuring that:
3667 - This is an accessible interface type,
3668 - Circularity detection.
3669 parser_add_interface is then called. If present but not defined,
3670 the check operation is delayed until the super interface gets
3671 defined. */
3672 for (node = interface_list; node; node = TREE_CHAIN (node))
3673 {
15fdcfe9 3674 tree current = TREE_PURPOSE (node);
5e942c50
APB
3675 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3676 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3677 {
5e942c50
APB
3678 if (!parser_check_super_interface (idecl, decl, current))
3679 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3680 }
3681 else
3682 register_incomplete_type (JDEP_INTERFACE,
3683 current, decl, NULL_TREE);
3684 }
3685}
3686
3687/* Create an interface in pass1 and return its decl. Return the
3688 interface's decl in pass 2. */
3689
3690static tree
3691create_interface (flags, id, super)
3692 int flags;
3693 tree id, super;
3694{
e04a16fb 3695 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3696 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3697 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3698
3699 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3700
3701 /* Basic checks: scope, redefinition, modifiers */
3702 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3703 {
3704 PUSH_ERROR ();
3705 return NULL_TREE;
3706 }
3707
3708 /* Suspend the current parsing context if we're parsing an inner
3709 interface */
3710 if (CPC_INNER_P ())
3711 java_parser_context_suspend ();
3712
3713 /* Push a new context for (static) initialized upon declaration fields */
3714 java_parser_context_push_initialized_field ();
e04a16fb
AG
3715
3716 /* Interface modifiers check
3717 - public/abstract allowed (already done at that point)
3718 - abstract is obsolete (comes first, it's a warning, or should be)
3719 - Can't use twice the same (checked in the modifier rule) */
c877974e 3720 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3721 parse_warning_context
3722 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3723 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3724
3725 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3726 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3727
3728 /* Set super info and mark the class a complete */
2aa11e97 3729 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3730 object_type_node, ctxp->interface_number);
3731 ctxp->interface_number = 0;
3732 CLASS_COMPLETE_P (decl) = 1;
3733 add_superinterfaces (decl, super);
3734
3735 return decl;
3736}
3737
c2952b01
APB
3738/* Anonymous class counter. Will be reset to 1 every time a non
3739 anonymous class gets created. */
3740static int anonymous_class_counter = 1;
3741
3742/* Patch anonymous class CLASS, by either extending or implementing
3743 DEP. */
3744
3745static void
3746patch_anonymous_class (type_decl, class_decl, wfl)
3747 tree type_decl, class_decl, wfl;
3748{
3749 tree class = TREE_TYPE (class_decl);
3750 tree type = TREE_TYPE (type_decl);
3751 tree binfo = TYPE_BINFO (class);
3752
3753 /* If it's an interface, implement it */
3754 if (CLASS_INTERFACE (type_decl))
3755 {
3756 tree s_binfo;
3757 int length;
3758
3759 if (parser_check_super_interface (type_decl, class_decl, wfl))
3760 return;
3761
3762 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3763 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3764 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3765 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3766 /* And add the interface */
3767 parser_add_interface (class_decl, type_decl, wfl);
3768 }
3769 /* Otherwise, it's a type we want to extend */
3770 else
3771 {
3772 if (parser_check_super (type_decl, class_decl, wfl))
3773 return;
3774 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3775 }
3776}
3777
3778static tree
3779create_anonymous_class (location, type_name)
3780 int location;
3781 tree type_name;
3782{
3783 char buffer [80];
3784 tree super = NULL_TREE, itf = NULL_TREE;
3785 tree id, type_decl, class;
3786
3787 /* The unqualified name of the anonymous class. It's just a number. */
3788 sprintf (buffer, "%d", anonymous_class_counter++);
3789 id = build_wfl_node (get_identifier (buffer));
3790 EXPR_WFL_LINECOL (id) = location;
3791
3792 /* We know about the type to extend/implement. We go ahead */
3793 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3794 {
3795 /* Create a class which either implements on extends the designated
3796 class. The class bears an innacessible name. */
3797 if (CLASS_INTERFACE (type_decl))
3798 {
3799 /* It's OK to modify it here. It's been already used and
3800 shouldn't be reused */
3801 ctxp->interface_number = 1;
3802 /* Interfaces should presented as a list of WFLs */
3803 itf = build_tree_list (type_name, NULL_TREE);
3804 }
3805 else
3806 super = type_name;
3807 }
3808
3809 class = create_class (ACC_FINAL, id, super, itf);
3810
3811 /* We didn't know anything about the stuff. We register a dependence. */
3812 if (!type_decl)
3813 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3814
3815 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3816 return class;
3817}
3818
a40d21da 3819/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3820 interface's decl in pass 2. */
3821
3822static tree
3823create_class (flags, id, super, interfaces)
3824 int flags;
3825 tree id, super, interfaces;
3826{
e04a16fb
AG
3827 tree raw_name = EXPR_WFL_NODE (id);
3828 tree class_id, decl;
9ee9b555 3829 tree super_decl_type;
e04a16fb 3830
98a52c2c 3831 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3832 decl = IDENTIFIER_CLASS_VALUE (class_id);
3833 EXPR_WFL_NODE (id) = class_id;
3834
3835 /* Basic check: scope, redefinition, modifiers */
3836 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3837 {
3838 PUSH_ERROR ();
3839 return NULL_TREE;
3840 }
3841
3842 /* Suspend the current parsing context if we're parsing an inner
3843 class or an anonymous class. */
3844 if (CPC_INNER_P ())
3845 java_parser_context_suspend ();
3846 /* Push a new context for (static) initialized upon declaration fields */
3847 java_parser_context_push_initialized_field ();
e04a16fb
AG
3848
3849 /* Class modifier check:
3850 - Allowed modifier (already done at that point)
3851 - abstract AND final forbidden
3852 - Public classes defined in the correct file */
3853 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3854 parse_error_context
3855 (id, "Class `%s' can't be declared both abstract and final",
3856 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3857
3858 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3859 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3860
3861 /* If SUPER exists, use it, otherwise use Object */
3862 if (super)
3863 {
3864 /* Can't extend java.lang.Object */
3865 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3866 {
3867 parse_error_context (id, "Can't extend `java.lang.Object'");
3868 return NULL_TREE;
3869 }
3870
2c3199bc
PB
3871 super_decl_type =
3872 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3873 }
3874 else if (TREE_TYPE (decl) != object_type_node)
3875 super_decl_type = object_type_node;
3876 /* We're defining java.lang.Object */
3877 else
3878 super_decl_type = NULL_TREE;
3879
6d003d5c
BM
3880 /* A class nested in an interface is implicitly static. */
3881 if (INNER_CLASS_DECL_P (decl)
3882 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3883 {
3884 flags |= ACC_STATIC;
3885 }
3886
3887 /* Set super info and mark the class as complete. */
e04a16fb
AG
3888 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3889 ctxp->interface_number);
3890 ctxp->interface_number = 0;
3891 CLASS_COMPLETE_P (decl) = 1;
3892 add_superinterfaces (decl, interfaces);
3893
c2952b01
APB
3894 /* Add the private this$<n> field, Replicate final locals still in
3895 scope as private final fields mangled like val$<local_name>.
3896 This doesn't not occur for top level (static) inner classes. */
3897 if (PURE_INNER_CLASS_DECL_P (decl))
3898 add_inner_class_fields (decl, current_function_decl);
3899
7f10c2e2
APB
3900 /* If doing xref, store the location at which the inherited class
3901 (if any) was seen. */
3902 if (flag_emit_xref && super)
3903 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3904
5e942c50
APB
3905 /* Eventually sets the @deprecated tag flag */
3906 CHECK_DEPRECATED (decl);
3907
165f37bc
APB
3908 /* Reset the anonymous class counter when declaring non inner classes */
3909 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3910 anonymous_class_counter = 1;
3911
e04a16fb
AG
3912 return decl;
3913}
3914
c2952b01 3915/* End a class declaration: register the statements used to create
c00f0fb2 3916 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3917 parser context if necessary. */
3918
3919static void
3920end_class_declaration (resume)
3921 int resume;
3922{
3923 /* If an error occured, context weren't pushed and won't need to be
3924 popped by a resume. */
3925 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3926
3927 java_parser_context_pop_initialized_field ();
3928 POP_CPC ();
3929 if (resume && no_error_occured)
3930 java_parser_context_resume ();
93220702
APB
3931
3932 /* We're ending a class declaration, this is a good time to reset
3933 the interface cout. Note that might have been already done in
3934 create_interface, but if at that time an inner class was being
3935 dealt with, the interface count was reset in a context created
3936 for the sake of handling inner classes declaration. */
3937 ctxp->interface_number = 0;
c2952b01
APB
3938}
3939
3940static void
3941add_inner_class_fields (class_decl, fct_decl)
3942 tree class_decl;
3943 tree fct_decl;
3944{
3945 tree block, marker, f;
3946
3947 f = add_field (TREE_TYPE (class_decl),
3948 build_current_thisn (TREE_TYPE (class_decl)),
3949 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3950 ACC_PRIVATE);
3951 FIELD_THISN (f) = 1;
3952
3953 if (!fct_decl)
3954 return;
3955
3956 for (block = GET_CURRENT_BLOCK (fct_decl);
3957 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3958 {
3959 tree decl;
3960 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3961 {
1f8f4a0b 3962 tree name, pname;
c2952b01
APB
3963 tree wfl, init, list;
3964
3965 /* Avoid non final arguments. */
c7303e41 3966 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3967 continue;
3968
3969 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3970 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3971 wfl = build_wfl_node (name);
3972 init = build_wfl_node (pname);
c2952b01 3973 /* Build an initialization for the field: it will be
c00f0fb2 3974 initialized by a parameter added to finit$, bearing a
c2952b01 3975 mangled name of the field itself (param$<n>.) The
c00f0fb2 3976 parameter is provided to finit$ by the constructor
c2952b01
APB
3977 invoking it (hence the constructor will also feature a
3978 hidden parameter, set to the value of the outer context
3979 local at the time the inner class is created.)
3980
3981 Note: we take into account all possible locals that can
3982 be accessed by the inner class. It's actually not trivial
3983 to minimize these aliases down to the ones really
3984 used. One way to do that would be to expand all regular
c00f0fb2 3985 methods first, then finit$ to get a picture of what's
c2952b01
APB
3986 used. It works with the exception that we would have to
3987 go back on all constructor invoked in regular methods to
3988 have their invokation reworked (to include the right amount
3989 of alias initializer parameters.)
3990
3991 The only real way around, I think, is a first pass to
3992 identify locals really used in the inner class. We leave
3993 the flag FIELD_LOCAL_ALIAS_USED around for that future
3994 use.
3995
3996 On the other hand, it only affect local inner classes,
c00f0fb2 3997 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3998 unecessary arguments. It's easy for a developper to keep
3999 this number of parameter down by using the `final'
4000 keyword only when necessary. For the time being, we can
4001 issue a warning on unecessary finals. FIXME */
4002 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4003 wfl, init);
4004
4005 /* Register the field. The TREE_LIST holding the part
4006 initialized/initializer will be marked ARG_FINAL_P so
4007 that the created field can be marked
4008 FIELD_LOCAL_ALIAS. */
4009 list = build_tree_list (wfl, init);
4010 ARG_FINAL_P (list) = 1;
4011 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4012 }
4013 }
4014
4015 if (!CPC_INITIALIZER_STMT (ctxp))
4016 return;
4017
4018 /* If we ever registered an alias field, insert and marker to
4019 remeber where the list ends. The second part of the list (the one
4020 featuring initialized fields) so it can be later reversed to
4021 enforce 8.5. The marker will be removed during that operation. */
4022 marker = build_tree_list (NULL_TREE, NULL_TREE);
4023 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4024 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4025}
4026
e04a16fb
AG
4027/* Can't use lookup_field () since we don't want to load the class and
4028 can't set the CLASS_LOADED_P flag */
4029
4030static tree
4031find_field (class, name)
4032 tree class;
4033 tree name;
4034{
4035 tree decl;
4036 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4037 {
4038 if (DECL_NAME (decl) == name)
4039 return decl;
4040 }
4041 return NULL_TREE;
4042}
4043
4044/* Wrap around lookup_field that doesn't potentially upset the value
4045 of CLASS */
4046
4047static tree
4048lookup_field_wrapper (class, name)
4049 tree class, name;
4050{
4051 tree type = class;
9a7ab4b3 4052 tree decl = NULL_TREE;
c877974e 4053 java_parser_context_save_global ();
f2760b27
APB
4054
4055 /* Last chance: if we're within the context of an inner class, we
4056 might be trying to access a local variable defined in an outer
4057 context. We try to look for it now. */
9a7ab4b3 4058 if (INNER_CLASS_TYPE_P (class))
f2760b27 4059 {
9a7ab4b3 4060 tree new_name;
1f8f4a0b 4061 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4062 decl = lookup_field (&type, new_name);
f2760b27
APB
4063 if (decl && decl != error_mark_node)
4064 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4065 }
9a7ab4b3
APB
4066 if (!decl || decl == error_mark_node)
4067 {
4068 type = class;
4069 decl = lookup_field (&type, name);
4070 }
f2760b27 4071
c877974e 4072 java_parser_context_restore_global ();
93024893 4073 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4074}
4075
4076/* Find duplicate field within the same class declarations and report
c583dd46
APB
4077 the error. Returns 1 if a duplicated field was found, 0
4078 otherwise. */
e04a16fb
AG
4079
4080static int
c583dd46 4081duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4082 tree new_field_name, new_type, cl;
e04a16fb
AG
4083{
4084 /* This might be modified to work with method decl as well */
c2952b01 4085 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4086 if (decl)
4087 {
c2e3db92 4088 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4089 ((TREE_CODE (new_type) == POINTER_TYPE
4090 && TREE_TYPE (new_type) == NULL_TREE) ?
4091 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4092 lang_printable_name (new_type, 1)));
c877974e
APB
4093 /* The type may not have been completed by the time we report
4094 the error */
c2e3db92 4095 char *t2 = xstrdup (purify_type_name
4a5f66c3 4096 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4097 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4098 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4099 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4100 parse_error_context
4101 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4102 t1, IDENTIFIER_POINTER (new_field_name),
4103 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4104 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4105 free (t1);
4106 free (t2);
c583dd46 4107 return 1;
e04a16fb 4108 }
c583dd46 4109 return 0;
e04a16fb
AG
4110}
4111
4112/* Field registration routine. If TYPE doesn't exist, field
4113 declarations are linked to the undefined TYPE dependency list, to
4114 be later resolved in java_complete_class () */
4115
4116static void
4117register_fields (flags, type, variable_list)
4118 int flags;
4119 tree type, variable_list;
4120{
c583dd46 4121 tree current, saved_type;
c2952b01 4122 tree class_type = NULL_TREE;
e04a16fb
AG
4123 int saved_lineno = lineno;
4124 int must_chain = 0;
4125 tree wfl = NULL_TREE;
4126
c2952b01
APB
4127 if (GET_CPC ())
4128 class_type = TREE_TYPE (GET_CPC ());
4129
4130 if (!class_type || class_type == error_mark_node)
4131 return;
4132
e04a16fb
AG
4133 /* If we're adding fields to interfaces, those fields are public,
4134 static, final */
4135 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4136 {
4137 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4138 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4139 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4140 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4141 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4142 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4143 check_modifiers ("Illegal interface member modifier `%s'", flags,
4144 INTERFACE_FIELD_MODIFIERS);
4145 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4146 }
4147
c583dd46
APB
4148 /* Obtain a suitable type for resolution, if necessary */
4149 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4150
4151 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4152 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4153
c583dd46
APB
4154 for (current = variable_list, saved_type = type; current;
4155 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4156 {
c877974e 4157 tree real_type;
c583dd46 4158 tree field_decl;
e04a16fb
AG
4159 tree cl = TREE_PURPOSE (current);
4160 tree init = TREE_VALUE (current);
4161 tree current_name = EXPR_WFL_NODE (cl);
4162
cf1748bf 4163 /* Can't declare non-final static fields in inner classes */
c2952b01 4164 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4165 && !(flags & ACC_FINAL))
c2952b01 4166 parse_error_context
cf1748bf 4167 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4168 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4169 lang_printable_name (class_type, 0));
4170
c583dd46
APB
4171 /* Process NAME, as it may specify extra dimension(s) for it */
4172 type = build_array_from_name (type, wfl, current_name, &current_name);
4173
c583dd46
APB
4174 /* Type adjustment. We may have just readjusted TYPE because
4175 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4176 a reference if we can and don't have one already. Also
4177 change the name if we have an init. */
4178 if (type != saved_type)
4179 {
1886c9d8 4180 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4181 if (init)
4182 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4183 }
e04a16fb 4184
c877974e
APB
4185 real_type = GET_REAL_TYPE (type);
4186 /* Check for redeclarations */
4187 if (duplicate_declaration_error_p (current_name, real_type, cl))
4188 continue;
4189
c583dd46 4190 /* Set lineno to the line the field was found and create a
5e942c50 4191 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4192 if (flag_emit_xref)
4193 lineno = EXPR_WFL_LINECOL (cl);
4194 else
4195 lineno = EXPR_WFL_LINENO (cl);
c877974e 4196 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4197 CHECK_DEPRECATED (field_decl);
c2952b01 4198
c7303e41
APB
4199 /* If the field denotes a final instance variable, then we
4200 allocate a LANG_DECL_SPECIFIC part to keep track of its
4201 initialization. We also mark whether the field was
4202 initialized upon it's declaration. We don't do that if the
4203 created field is an alias to a final local. */
4204 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4205 {
4206 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4207 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4208 if ((flags & ACC_STATIC) && init)
4209 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4210 }
4211
4212 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4213 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4214 hide parameters to this inner class finit$ and
4215 constructors. It also means that the field isn't final per
4216 say. */
c2952b01 4217 if (ARG_FINAL_P (current))
c7303e41
APB
4218 {
4219 FIELD_LOCAL_ALIAS (field_decl) = 1;
4220 FIELD_FINAL (field_decl) = 0;
4221 }
c583dd46
APB
4222
4223 /* Check if we must chain. */
4224 if (must_chain)
4225 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4226
c583dd46
APB
4227 /* If we have an initialization value tied to the field */
4228 if (init)
4229 {
4230 /* The field is declared static */
e04a16fb 4231 if (flags & ACC_STATIC)
e04a16fb 4232 {
7525cc04
APB
4233 /* We include the field and its initialization part into
4234 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4235 walked, field initializations will be processed and
4236 fields initialized with known constants will be taken
4237 out of <clinit> and have their DECL_INITIAL set
7525cc04 4238 appropriately. */
c2952b01
APB
4239 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4240 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4241 if (TREE_OPERAND (init, 1)
4242 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4243 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4244 }
5e942c50
APB
4245 /* A non-static field declared with an immediate initialization is
4246 to be initialized in <init>, if any. This field is remembered
4247 to be processed at the time of the generation of <init>. */
c583dd46
APB
4248 else
4249 {
c2952b01
APB
4250 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4251 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4252 }
5b09b33e 4253 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4254 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4255 }
4256 }
4257 lineno = saved_lineno;
4258}
4259
c00f0fb2
APB
4260/* Generate finit$, using the list of initialized fields to populate
4261 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4262 one(s) used to initialized the field(s) caching outer context
4263 local(s). */
22eed1e6 4264
c2952b01
APB
4265static tree
4266generate_finit (class_type)
4267 tree class_type;
22eed1e6 4268{
c2952b01
APB
4269 int count = 0;
4270 tree list = TYPE_FINIT_STMT_LIST (class_type);
4271 tree mdecl, current, parms;
4272
4273 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4274 class_type, NULL_TREE,
4275 &count);
4276 CRAFTED_PARAM_LIST_FIXUP (parms);
4277 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4278 finit_identifier_node, parms);
4279 fix_method_argument_names (parms, mdecl);
4280 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4281 mdecl, NULL_TREE);
4282 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4283 start_artificial_method_body (mdecl);
4284
c2952b01 4285 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4286 java_method_add_stmt (mdecl,
4287 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4288 current));
22eed1e6 4289 end_artificial_method_body (mdecl);
c2952b01 4290 return mdecl;
22eed1e6
APB
4291}
4292
e04a16fb 4293static void
c2952b01
APB
4294add_instance_initializer (mdecl)
4295 tree mdecl;
e04a16fb 4296{
c2952b01
APB
4297 tree current;
4298 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4299 tree compound = NULL_TREE;
e04a16fb 4300
c2952b01 4301 if (stmt_list)
e04a16fb 4302 {
c2952b01
APB
4303 for (current = stmt_list; current; current = TREE_CHAIN (current))
4304 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4305
c2952b01
APB
4306 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4307 NULL_TREE, compound));
4308 }
e04a16fb
AG
4309}
4310
4311/* Shared accros method_declarator and method_header to remember the
4312 patch stage that was reached during the declaration of the method.
4313 A method DECL is built differently is there is no patch
4314 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4315 pending on the currently defined method. */
4316
4317static int patch_stage;
4318
4319/* Check the method declaration and add the method to its current
4320 class. If the argument list is known to contain incomplete types,
4321 the method is partially added and the registration will be resume
22eed1e6
APB
4322 once the method arguments resolved. If TYPE is NULL, we're dealing
4323 with a constructor. */
e04a16fb
AG
4324
4325static tree
4326method_header (flags, type, mdecl, throws)
4327 int flags;
4328 tree type, mdecl, throws;
4329{
1886c9d8 4330 tree type_wfl = NULL_TREE;
79d13333 4331 tree meth_name = NULL_TREE;
c2952b01 4332 tree current, orig_arg, this_class = NULL;
34d4df06 4333 tree id, meth;
e04a16fb 4334 int saved_lineno;
1886c9d8 4335 int constructor_ok = 0, must_chain;
c2952b01 4336 int count;
34d4df06
APB
4337
4338 if (mdecl == error_mark_node)
4339 return error_mark_node;
4340 meth = TREE_VALUE (mdecl);
4341 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4342
4343 check_modifiers_consistency (flags);
79d13333 4344
c2952b01
APB
4345 if (GET_CPC ())
4346 this_class = TREE_TYPE (GET_CPC ());
4347
4348 if (!this_class || this_class == error_mark_node)
79d13333 4349 return NULL_TREE;
e04a16fb
AG
4350
4351 /* There are some forbidden modifiers for an abstract method and its
4352 class must be abstract as well. */
22eed1e6 4353 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4354 {
4355 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4356 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4357 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4358 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4359 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4360 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4361 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4362 parse_error_context
781b0558 4363 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4364 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4365 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4366 }
c2952b01 4367
22eed1e6
APB
4368 /* Things to be checked when declaring a constructor */
4369 if (!type)
4370 {
4371 int ec = java_error_count;
4372 /* 8.6: Constructor declarations: we might be trying to define a
4373 method without specifying a return type. */
c2952b01 4374 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4375 parse_error_context
4376 (id, "Invalid method declaration, return type required");
4377 /* 8.6.3: Constructor modifiers */
4378 else
4379 {
4380 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4381 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4382 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4383 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4384 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4385 }
4386 /* If we found error here, we don't consider it's OK to tread
4387 the method definition as a constructor, for the rest of this
4388 function */
4389 if (ec == java_error_count)
4390 constructor_ok = 1;
4391 }
e04a16fb
AG
4392
4393 /* Method declared within the scope of an interface are implicitly
4394 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4395 modifiers are checked right after. */
e04a16fb
AG
4396
4397 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4398 {
4399 /* If FLAGS isn't set because of a modifier, turn the
4400 corresponding modifier WFL to NULL so we issue a warning on
4401 the obsolete use of the modifier */
4402 if (!(flags & ACC_PUBLIC))
4403 MODIFIER_WFL (PUBLIC_TK) = NULL;
4404 if (!(flags & ACC_ABSTRACT))
4405 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4406 flags |= ACC_PUBLIC;
4407 flags |= ACC_ABSTRACT;
4408 }
4409
c2952b01
APB
4410 /* Inner class can't declare static methods */
4411 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4412 {
4413 parse_error_context
4414 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4415 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4416 lang_printable_name (this_class, 0));
4417 }
4418
e04a16fb
AG
4419 /* Modifiers context reset moved up, so abstract method declaration
4420 modifiers can be later checked. */
4421
22eed1e6
APB
4422 /* Set constructor returned type to void and method name to <init>,
4423 unless we found an error identifier the constructor (in which
4424 case we retain the original name) */
4425 if (!type)
4426 {
4427 type = void_type_node;
4428 if (constructor_ok)
4429 meth_name = init_identifier_node;
4430 }
4431 else
4432 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4433
1886c9d8
APB
4434 /* Do the returned type resolution and registration if necessary */
4435 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4436
4a5f66c3
APB
4437 if (meth_name)
4438 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4439 EXPR_WFL_NODE (id) = meth_name;
4440 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4441
4442 if (must_chain)
e04a16fb 4443 {
1886c9d8
APB
4444 patch_stage = JDEP_METHOD_RETURN;
4445 register_incomplete_type (patch_stage, type_wfl, id, type);
4446 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4447 }
4448 else
1886c9d8 4449 TREE_TYPE (meth) = type;
e04a16fb
AG
4450
4451 saved_lineno = lineno;
4452 /* When defining an abstract or interface method, the curly
4453 bracket at level 1 doesn't exist because there is no function
4454 body */
4455 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4456 EXPR_WFL_LINENO (id));
4457
5e942c50
APB
4458 /* Remember the original argument list */
4459 orig_arg = TYPE_ARG_TYPES (meth);
4460
e04a16fb
AG
4461 if (patch_stage) /* includes ret type and/or all args */
4462 {
4463 jdep *jdep;
4464 meth = add_method_1 (this_class, flags, meth_name, meth);
4465 /* Patch for the return type */
4466 if (patch_stage == JDEP_METHOD_RETURN)
4467 {
4468 jdep = CLASSD_LAST (ctxp->classd_list);
4469 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4470 }
4471 /* This is the stop JDEP. METH allows the function's signature
4472 to be computed. */
4473 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4474 }
4475 else
5e942c50
APB
4476 meth = add_method (this_class, flags, meth_name,
4477 build_java_signature (meth));
4478
c2952b01
APB
4479 /* Remember final parameters */
4480 MARK_FINAL_PARMS (meth, orig_arg);
4481
5e942c50
APB
4482 /* Fix the method argument list so we have the argument name
4483 information */
4484 fix_method_argument_names (orig_arg, meth);
4485
4486 /* Register the parameter number and re-install the current line
4487 number */
e04a16fb
AG
4488 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4489 lineno = saved_lineno;
b9f7e36c
APB
4490
4491 /* Register exception specified by the `throws' keyword for
4492 resolution and set the method decl appropriate field to the list.
4493 Note: the grammar ensures that what we get here are class
4494 types. */
4495 if (throws)
4496 {
4497 throws = nreverse (throws);
4498 for (current = throws; current; current = TREE_CHAIN (current))
4499 {
4500 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4501 NULL_TREE, NULL_TREE);
4502 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4503 &TREE_VALUE (current);
4504 }
4505 DECL_FUNCTION_THROWS (meth) = throws;
4506 }
4507
c4faeb92
APB
4508 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4509 DECL_FUNCTION_WFL (meth) = id;
4510
22eed1e6
APB
4511 /* Set the flag if we correctly processed a constructor */
4512 if (constructor_ok)
c2952b01
APB
4513 {
4514 DECL_CONSTRUCTOR_P (meth) = 1;
4515 /* Compute and store the number of artificial parameters declared
4516 for this constructor */
4517 for (count = 0, current = TYPE_FIELDS (this_class); current;
4518 current = TREE_CHAIN (current))
4519 if (FIELD_LOCAL_ALIAS (current))
4520 count++;
4521 DECL_FUNCTION_NAP (meth) = count;
4522 }
22eed1e6 4523
5e942c50
APB
4524 /* Eventually set the @deprecated tag flag */
4525 CHECK_DEPRECATED (meth);
4526
7f10c2e2
APB
4527 /* If doing xref, store column and line number information instead
4528 of the line number only. */
4529 if (flag_emit_xref)
4530 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4531
e04a16fb
AG
4532 return meth;
4533}
4534
5e942c50
APB
4535static void
4536fix_method_argument_names (orig_arg, meth)
4537 tree orig_arg, meth;
4538{
4539 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4540 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4541 {
4542 TREE_PURPOSE (arg) = this_identifier_node;
4543 arg = TREE_CHAIN (arg);
4544 }
de4c7b02 4545 while (orig_arg != end_params_node)
5e942c50
APB
4546 {
4547 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4548 orig_arg = TREE_CHAIN (orig_arg);
4549 arg = TREE_CHAIN (arg);
4550 }
4551}
4552
22eed1e6
APB
4553/* Complete the method declaration with METHOD_BODY. */
4554
4555static void
b635eb2f 4556finish_method_declaration (method_body)
22eed1e6
APB
4557 tree method_body;
4558{
79d13333
APB
4559 int flags;
4560
4561 if (!current_function_decl)
4562 return;
4563
4564 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4565
4566 /* 8.4.5 Method Body */
4567 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4568 {
c0b00d37
APB
4569 tree name = DECL_NAME (current_function_decl);
4570 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
5256aa37
APB
4571 "%s method `%s' can't have a body defined",
4572 (METHOD_NATIVE (current_function_decl) ?
4573 "Native" : "Abstract"),
c0b00d37 4574 IDENTIFIER_POINTER (name));
5256aa37
APB
4575 method_body = NULL_TREE;
4576 }
4577 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4578 {
c0b00d37 4579 tree name = DECL_NAME (current_function_decl);
781b0558 4580 parse_error_context
c0b00d37 4581 (DECL_FUNCTION_WFL (current_function_decl),
781b0558 4582 "Non native and non abstract method `%s' must have a body defined",
c0b00d37 4583 IDENTIFIER_POINTER (name));
5256aa37
APB
4584 method_body = NULL_TREE;
4585 }
4586
2c56429a
APB
4587 if (flag_emit_class_files && method_body
4588 && TREE_CODE (method_body) == NOP_EXPR
4589 && TREE_TYPE (current_function_decl)
4590 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4591 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4592
22eed1e6
APB
4593 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4594 maybe_absorb_scoping_blocks ();
4595 /* Exit function's body */
4596 exit_block ();
4597 /* Merge last line of the function with first line, directly in the
4598 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4599 if (!flag_emit_xref)
4600 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4601
4602 /* Since function's argument's list are shared, reset the
4603 ARG_FINAL_P parameter that might have been set on some of this
4604 function parameters. */
4605 UNMARK_FINAL_PARMS (current_function_decl);
4606
f099f336
APB
4607 /* So we don't have an irrelevant function declaration context for
4608 the next static block we'll see. */
4609 current_function_decl = NULL_TREE;
22eed1e6
APB
4610}
4611
4612/* Build a an error message for constructor circularity errors. */
4613
4614static char *
4615constructor_circularity_msg (from, to)
4616 tree from, to;
4617{
4618 static char string [4096];
c2e3db92 4619 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4620 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4621 free (t);
4622 return string;
4623}
4624
4625/* Verify a circular call to METH. Return 1 if an error is found, 0
4626 otherwise. */
4627
4628static int
4629verify_constructor_circularity (meth, current)
4630 tree meth, current;
4631{
4632 static tree list = NULL_TREE;
19e223db 4633 static int initialized_p;
22eed1e6 4634 tree c;
19e223db
MM
4635
4636 /* If we haven't already registered LIST with the garbage collector,
4637 do so now. */
4638 if (!initialized_p)
4639 {
4640 ggc_add_tree_root (&list, 1);
4641 initialized_p = 1;
4642 }
4643
22eed1e6
APB
4644 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4645 {
4646 if (TREE_VALUE (c) == meth)
4647 {
4648 char *t;
4649 if (list)
4650 {
4651 tree liste;
4652 list = nreverse (list);
4653 for (liste = list; liste; liste = TREE_CHAIN (liste))
4654 {
4655 parse_error_context
c63b98cd 4656 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4657 constructor_circularity_msg
4658 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4659 java_error_count--;
4660 }
4661 }
c2e3db92 4662 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4663 parse_error_context (TREE_PURPOSE (c),
4664 "%s: recursive invocation of constructor `%s'",
4665 constructor_circularity_msg (current, meth), t);
4666 free (t);
4667 list = NULL_TREE;
4668 return 1;
4669 }
4670 }
4671 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4672 {
4673 list = tree_cons (c, current, list);
4674 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4675 return 1;
4676 list = TREE_CHAIN (list);
4677 }
4678 return 0;
4679}
4680
e04a16fb
AG
4681/* Check modifiers that can be declared but exclusively */
4682
4683static void
4684check_modifiers_consistency (flags)
4685 int flags;
4686{
4687 int acc_count = 0;
4688 tree cl = NULL_TREE;
4689
e0fc4118
TT
4690 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4691 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4692 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4693 if (acc_count > 1)
4694 parse_error_context
e0fc4118
TT
4695 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4696
4697 acc_count = 0;
4698 cl = NULL_TREE;
14d075d8
TT
4699 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4700 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4701 if (acc_count > 1)
4702 parse_error_context (cl,
4703 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4704}
4705
4706/* Check the methode header METH for abstract specifics features */
4707
4708static void
4709check_abstract_method_header (meth)
4710 tree meth;
4711{
4712 int flags = get_access_flags_from_decl (meth);
e04a16fb 4713
2884c41e
KG
4714 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4715 ACC_ABSTRACT, "abstract method",
c4faeb92 4716 IDENTIFIER_POINTER (DECL_NAME (meth)));
2884c41e
KG
4717 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4718 ACC_PUBLIC, "abstract method",
c4faeb92 4719 IDENTIFIER_POINTER (DECL_NAME (meth)));
e04a16fb
AG
4720
4721 check_modifiers ("Illegal modifier `%s' for interface method",
4722 flags, INTERFACE_METHOD_MODIFIERS);
4723}
4724
4725/* Create a FUNCTION_TYPE node and start augmenting it with the
4726 declared function arguments. Arguments type that can't be resolved
4727 are left as they are, but the returned node is marked as containing
4728 incomplete types. */
4729
4730static tree
4731method_declarator (id, list)
4732 tree id, list;
4733{
4734 tree arg_types = NULL_TREE, current, node;
4735 tree meth = make_node (FUNCTION_TYPE);
4736 jdep *jdep;
e04a16fb
AG
4737
4738 patch_stage = JDEP_NO_PATCH;
c2952b01 4739
34d4df06
APB
4740 if (GET_CPC () == error_mark_node)
4741 return error_mark_node;
4742
c2952b01
APB
4743 /* If we're dealing with an inner class constructor, we hide the
4744 this$<n> decl in the name field of its parameter declaration. We
4745 also might have to hide the outer context local alias
4746 initializers. Not done when the class is a toplevel class. */
4747 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4748 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4749 {
4750 tree aliases_list, type, thisn;
4751 /* First the aliases, linked to the regular parameters */
4752 aliases_list =
4753 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4754 TREE_TYPE (GET_CPC ()),
4755 NULL_TREE, NULL);
4756 list = chainon (nreverse (aliases_list), list);
4757
4758 /* Then this$<n> */
4759 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4760 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4761 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4762 list);
4763 }
e04a16fb
AG
4764
4765 for (current = list; current; current = TREE_CHAIN (current))
4766 {
c583dd46 4767 int must_chain = 0;
e04a16fb
AG
4768 tree wfl_name = TREE_PURPOSE (current);
4769 tree type = TREE_VALUE (current);
4770 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4771 tree already, arg_node;
4772 tree type_wfl = NULL_TREE;
23a79c61 4773 tree real_type;
c583dd46
APB
4774
4775 /* Obtain a suitable type for resolution, if necessary */
4776 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4777
4778 /* Process NAME, as it may specify extra dimension(s) for it */
4779 type = build_array_from_name (type, type_wfl, name, &name);
4780 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4781
23a79c61
APB
4782 real_type = GET_REAL_TYPE (type);
4783 if (TREE_CODE (real_type) == RECORD_TYPE)
4784 {
4785 real_type = promote_type (real_type);
4786 if (TREE_CODE (type) == TREE_LIST)
4787 TREE_PURPOSE (type) = real_type;
4788 }
5e942c50 4789
e04a16fb
AG
4790 /* Check redefinition */
4791 for (already = arg_types; already; already = TREE_CHAIN (already))
4792 if (TREE_PURPOSE (already) == name)
4793 {
781b0558
KG
4794 parse_error_context
4795 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4796 IDENTIFIER_POINTER (name),
e04a16fb
AG
4797 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4798 break;
4799 }
4800
4801 /* If we've an incomplete argument type, we know there is a location
4802 to patch when the type get resolved, later. */
4803 jdep = NULL;
c583dd46 4804 if (must_chain)
e04a16fb 4805 {
c583dd46
APB
4806 patch_stage = JDEP_METHOD;
4807 type = register_incomplete_type (patch_stage,
4808 type_wfl, wfl_name, type);
4809 jdep = CLASSD_LAST (ctxp->classd_list);
4810 JDEP_MISC (jdep) = id;
e04a16fb 4811 }
c583dd46 4812
c2952b01 4813 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4814 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4815 /* Remeber arguments declared final. */
4816 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4817
e04a16fb
AG
4818 if (jdep)
4819 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4820 TREE_CHAIN (arg_node) = arg_types;
4821 arg_types = arg_node;
4822 }
de4c7b02 4823 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4824 node = build_tree_list (id, meth);
4825 return node;
4826}
4827
4828static int
4829unresolved_type_p (wfl, returned)
4830 tree wfl;
4831 tree *returned;
4832
4833{
4834 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4835 {
e04a16fb 4836 if (returned)
165f37bc
APB
4837 {
4838 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4839 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4840 *returned = TREE_TYPE (decl);
4841 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4842 *returned = TREE_TYPE (GET_CPC ());
4843 else
4844 *returned = NULL_TREE;
4845 }
e04a16fb
AG
4846 return 1;
4847 }
4848 if (returned)
4849 *returned = wfl;
4850 return 0;
4851}
4852
4853/* From NAME, build a qualified identifier node using the
4854 qualification from the current package definition. */
4855
4856static tree
98a52c2c 4857parser_qualified_classname (name)
e04a16fb
AG
4858 tree name;
4859{
c2952b01
APB
4860 tree nested_class_name;
4861
98a52c2c 4862 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4863 return nested_class_name;
4864
e04a16fb 4865 if (ctxp->package)
c2952b01 4866 return merge_qualified_name (ctxp->package, name);
e04a16fb 4867 else
c2952b01 4868 return name;
e04a16fb
AG
4869}
4870
4871/* Called once the type a interface extends is resolved. Returns 0 if
4872 everything is OK. */
4873
4874static int
4875parser_check_super_interface (super_decl, this_decl, this_wfl)
4876 tree super_decl, this_decl, this_wfl;
4877{
4878 tree super_type = TREE_TYPE (super_decl);
4879
4880 /* Has to be an interface */
c2952b01 4881 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4882 {
4883 parse_error_context
4884 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4885 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4886 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4887 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4888 "interface" : "class"),
4889 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4890 return 1;
4891 }
4892
a648f4e4
BM
4893 /* Check top-level interface access. Inner classes are subject to member
4894 access rules (6.6.1). */
4895 if (! INNER_CLASS_P (super_type)
4896 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
e04a16fb
AG
4897 return 1;
4898
4899 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4900 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4901 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4902 return 0;
4903}
4904
4905/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4906 0 if everthing is OK. */
4907
4908static int
4909parser_check_super (super_decl, this_decl, wfl)
4910 tree super_decl, this_decl, wfl;
4911{
e04a16fb
AG
4912 tree super_type = TREE_TYPE (super_decl);
4913
4914 /* SUPER should be a CLASS (neither an array nor an interface) */
4915 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4916 {
4917 parse_error_context
4918 (wfl, "Class `%s' can't subclass %s `%s'",
4919 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4920 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4921 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4922 return 1;
4923 }
4924
4925 if (CLASS_FINAL (TYPE_NAME (super_type)))
4926 {
4927 parse_error_context (wfl, "Can't subclass final classes: %s",
4928 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4929 return 1;
4930 }
4931
a648f4e4
BM
4932 /* Check top-level class scope. Inner classes are subject to member access
4933 rules (6.6.1). */
4934 if (! INNER_CLASS_P (super_type)
4935 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
e04a16fb
AG
4936 return 1;
4937
4938 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4939 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4940 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4941 return 0;
4942}
4943
4944/* Create a new dependency list and link it (in a LIFO manner) to the
4945 CTXP list of type dependency list. */
4946
4947static void
4948create_jdep_list (ctxp)
4949 struct parser_ctxt *ctxp;
4950{
23a79c61 4951 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4952 new->first = new->last = NULL;
4953 new->next = ctxp->classd_list;
4954 ctxp->classd_list = new;
4955}
4956
4957static jdeplist *
4958reverse_jdep_list (ctxp)
4959 struct parser_ctxt *ctxp;
4960{
4961 register jdeplist *prev = NULL, *current, *next;
4962 for (current = ctxp->classd_list; current; current = next)
4963 {
4964 next = current->next;
4965 current->next = prev;
4966 prev = current;
4967 }
4968 return prev;
4969}
4970
23a79c61
APB
4971/* Create a fake pointer based on the ID stored in
4972 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4973 registered again. */
e04a16fb
AG
4974
4975static tree
23a79c61
APB
4976obtain_incomplete_type (type_name)
4977 tree type_name;
e04a16fb 4978{
daaaf29f 4979 tree ptr = NULL_TREE, name;
23a79c61
APB
4980
4981 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4982 name = EXPR_WFL_NODE (type_name);
4983 else if (INCOMPLETE_TYPE_P (type_name))
4984 name = TYPE_NAME (type_name);
4985 else
400500c4 4986 abort ();
e04a16fb 4987
daaaf29f
PB
4988 BUILD_PTR_FROM_NAME (ptr, name);
4989 layout_type (ptr);
e04a16fb
AG
4990
4991 return ptr;
4992}
4993
4994/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4995 non NULL instead of computing a new fake type based on WFL. The new
4996 dependency is inserted in the current type dependency list, in FIFO
4997 manner. */
4998
4999static tree
5000register_incomplete_type (kind, wfl, decl, ptr)
5001 int kind;
5002 tree wfl, decl, ptr;
5003{
23a79c61 5004 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5005
e04a16fb
AG
5006 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5007 ptr = obtain_incomplete_type (wfl);
5008
5009 JDEP_KIND (new) = kind;
5010 JDEP_DECL (new) = decl;
daaaf29f 5011 JDEP_TO_RESOLVE (new) = ptr;
e04a16fb
AG
5012 JDEP_WFL (new) = wfl;
5013 JDEP_CHAIN (new) = NULL;
5014 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5015 /* For some dependencies, set the enclosing class of the current
5016 class to be the enclosing context */
8ac1de05
APB
5017 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5018 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5019 && GET_ENCLOSING_CPC ())
5020 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5021 else
324ed8fd 5022 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5023 JDEP_GET_PATCH (new) = (tree *)NULL;
5024
5025 JDEP_INSERT (ctxp->classd_list, new);
5026
5027 return ptr;
5028}
5029
5030void
5031java_check_circular_reference ()
5032{
5033 tree current;
5034 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5035 {
5036 tree type = TREE_TYPE (current);
e920ebc9 5037 if (CLASS_INTERFACE (current))
e04a16fb
AG
5038 {
5039 /* Check all interfaces this class extends */
5040 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5041 int n, i;
5042
5043 if (!basetype_vec)
5044 return;
5045 n = TREE_VEC_LENGTH (basetype_vec);
5046 for (i = 0; i < n; i++)
5047 {
5048 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5049 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5050 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5051 parse_error_context (lookup_cl (current),
5052 "Cyclic interface inheritance");
5053 }
5054 }
5055 else
5056 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5057 parse_error_context (lookup_cl (current),
c2952b01
APB
5058 "Cyclic class inheritance%s",
5059 (cyclic_inheritance_report ?
5060 cyclic_inheritance_report : ""));
5061 }
5062}
5063
5064/* Augment the parameter list PARM with parameters crafted to
5065 initialize outer context locals aliases. Through ARTIFICIAL, a
5066 count is kept of the number of crafted parameters. MODE governs
5067 what eventually gets created: something suitable for a function
5068 creation or a function invocation, either the constructor or
c00f0fb2 5069 finit$. */
c2952b01
APB
5070
5071static tree
5072build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5073 int mode;
5074 tree class_type, parm;
5075 int *artificial;
5076{
5077 tree field;
da632f2c
APB
5078 tree additional_parms = NULL_TREE;
5079
c2952b01
APB
5080 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5081 if (FIELD_LOCAL_ALIAS (field))
5082 {
63ad61ed 5083 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5084 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5085 tree mangled_id;
c2952b01
APB
5086
5087 switch (mode)
5088 {
5089 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5090 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5091 &buffer [4]);
5092 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5093 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5094 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5095 else
5096 value = TREE_TYPE (field);
5097 break;
5098
5099 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5101 &buffer [4]);
c2952b01
APB
5102 value = TREE_TYPE (field);
5103 break;
5104
5105 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5106 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5107 &buffer [4]);
c2952b01
APB
5108 /* Now, this is wrong. purpose should always be the NAME
5109 of something and value its matching value (decl, type,
5110 etc...) FIXME -- but there is a lot to fix. */
5111
5112 /* When invoked for this kind of operation, we already
5113 know whether a field is used or not. */
5114 purpose = TREE_TYPE (field);
1f8f4a0b 5115 value = build_wfl_node (mangled_id);
c2952b01
APB
5116 break;
5117
5118 case AIPL_FUNCTION_CTOR_INVOCATION:
5119 /* There are two case: the constructor invokation happends
5120 outside the local inner, in which case, locales from the outer
5121 context are directly used.
5122
5123 Otherwise, we fold to using the alias directly. */
5124 if (class_type == current_class)
5125 value = field;
5126 else
5127 {
5128 name = get_identifier (&buffer[4]);
5129 value = IDENTIFIER_LOCAL_VALUE (name);
5130 }
5131 break;
5132 }
da632f2c 5133 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5134 if (artificial)
5135 *artificial +=1;
5136 }
da632f2c
APB
5137 if (additional_parms)
5138 {
5139 if (ANONYMOUS_CLASS_P (class_type)
5140 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5141 additional_parms = nreverse (additional_parms);
5142 parm = chainon (additional_parms, parm);
5143 }
5144
5145 return parm;
c2952b01
APB
5146}
5147
5148/* Craft a constructor for CLASS_DECL -- what we should do when none
5149 where found. ARGS is non NULL when a special signature must be
5150 enforced. This is the case for anonymous classes. */
5151
5152static void
5153craft_constructor (class_decl, args)
5154 tree class_decl, args;
5155{
5156 tree class_type = TREE_TYPE (class_decl);
5157 tree parm = NULL_TREE;
5158 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5159 ACC_PUBLIC : 0);
5160 int i = 0, artificial = 0;
5161 tree decl, ctor_name;
5162 char buffer [80];
5163
c2952b01
APB
5164 /* The constructor name is <init> unless we're dealing with an
5165 anonymous class, in which case the name will be fixed after having
5166 be expanded. */
5167 if (ANONYMOUS_CLASS_P (class_type))
5168 ctor_name = DECL_NAME (class_decl);
5169 else
5170 ctor_name = init_identifier_node;
5171
5172 /* If we're dealing with an inner class constructor, we hide the
5173 this$<n> decl in the name field of its parameter declaration. */
5174 if (PURE_INNER_CLASS_TYPE_P (class_type))
5175 {
5176 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5177 parm = tree_cons (build_current_thisn (class_type),
5178 build_pointer_type (type), parm);
5179
5180 /* Some more arguments to be hidden here. The values of the local
5181 variables of the outer context that the inner class needs to see. */
5182 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5183 class_type, parm,
5184 &artificial);
5185 }
5186
5187 /* Then if there are any args to be enforced, enforce them now */
5188 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5189 {
5190 sprintf (buffer, "parm%d", i++);
5191 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5192 }
c2952b01
APB
5193
5194 CRAFTED_PARAM_LIST_FIXUP (parm);
5195 decl = create_artificial_method (class_type, flags, void_type_node,
5196 ctor_name, parm);
5197 fix_method_argument_names (parm, decl);
5198 /* Now, mark the artificial parameters. */
5199 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5200 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5201}
5202
c2952b01 5203
e920ebc9
APB
5204/* Fix the constructors. This will be called right after circular
5205 references have been checked. It is necessary to fix constructors
5206 early even if no code generation will take place for that class:
5207 some generated constructor might be required by the class whose
5208 compilation triggered this one to be simply loaded. */
5209
5210void
5211java_fix_constructors ()
5212{
5213 tree current;
5214
5215 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5216 {
e920ebc9
APB
5217 tree class_type = TREE_TYPE (current);
5218 int saw_ctor = 0;
c2952b01
APB
5219 tree decl;
5220
5221 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5222 continue;
e920ebc9
APB
5223
5224 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5225 {
5226 if (DECL_CONSTRUCTOR_P (decl))
5227 {
5228 fix_constructors (decl);
5229 saw_ctor = 1;
5230 }
5231 }
5232
c2952b01
APB
5233 /* Anonymous class constructor can't be generated that early. */
5234 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5235 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5236 }
5237}
5238
23a79c61
APB
5239/* safe_layout_class just makes sure that we can load a class without
5240 disrupting the current_class, input_file, lineno, etc, information
5241 about the class processed currently. */
5242
e04a16fb
AG
5243void
5244safe_layout_class (class)
5245 tree class;
5246{
5247 tree save_current_class = current_class;
3b304f5b 5248 const char *save_input_filename = input_filename;
e04a16fb 5249 int save_lineno = lineno;
5e942c50 5250
e04a16fb 5251 layout_class (class);
5e942c50 5252
e04a16fb
AG
5253 current_class = save_current_class;
5254 input_filename = save_input_filename;
5255 lineno = save_lineno;
5256 CLASS_LOADED_P (class) = 1;
5257}
5258
5259static tree
5260jdep_resolve_class (dep)
5261 jdep *dep;
5262{
5263 tree decl;
5264
23a79c61
APB
5265 if (JDEP_RESOLVED_P (dep))
5266 decl = JDEP_RESOLVED_DECL (dep);
5267 else
e04a16fb 5268 {
c2952b01 5269 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5270 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5271 JDEP_RESOLVED (dep, decl);
5272 }
23a79c61 5273
e04a16fb 5274 if (!decl)
23a79c61 5275 complete_class_report_errors (dep);
1e12ab9b 5276 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5277 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5278 return decl;
5279}
5280
5281/* Complete unsatisfied class declaration and their dependencies */
5282
5283void
5284java_complete_class ()
5285{
e04a16fb
AG
5286 tree cclass;
5287 jdeplist *cclassd;
5288 int error_found;
b67d701b 5289 tree type;
e04a16fb 5290
9a7ab4b3 5291 /* Process imports */
e04a16fb 5292 process_imports ();
e04a16fb
AG
5293
5294 /* Rever things so we have the right order */
5295 ctxp->class_list = nreverse (ctxp->class_list);
5296 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5297
e04a16fb
AG
5298 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5299 cclass && cclassd;
5300 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5301 {
5302 jdep *dep;
5303 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5304 {
5305 tree decl;
e04a16fb
AG
5306 if (!(decl = jdep_resolve_class (dep)))
5307 continue;
5308
5309 /* Now it's time to patch */
5310 switch (JDEP_KIND (dep))
5311 {
5312 case JDEP_SUPER:
5313 /* Simply patch super */
5314 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5315 continue;
5316 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5317 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5318 break;
5319
5320 case JDEP_FIELD:
5321 {
5322 /* We do part of the job done in add_field */
5323 tree field_decl = JDEP_DECL (dep);
5324 tree field_type = TREE_TYPE (decl);
e04a16fb 5325 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5326 field_type = promote_type (field_type);
e04a16fb 5327 TREE_TYPE (field_decl) = field_type;
5e942c50 5328 DECL_ALIGN (field_decl) = 0;
11cf4d18 5329 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5330 layout_decl (field_decl, 0);
e04a16fb
AG
5331 SOURCE_FRONTEND_DEBUG
5332 (("Completed field/var decl `%s' with `%s'",
5333 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5334 IDENTIFIER_POINTER (DECL_NAME (decl))));
5335 break;
5336 }
5337 case JDEP_METHOD: /* We start patching a method */
5338 case JDEP_METHOD_RETURN:
5339 error_found = 0;
5340 while (1)
5341 {
5342 if (decl)
5343 {
b67d701b
PB
5344 type = TREE_TYPE(decl);
5345 if (TREE_CODE (type) == RECORD_TYPE)
5346 type = promote_type (type);
e04a16fb
AG
5347 JDEP_APPLY_PATCH (dep, type);
5348 SOURCE_FRONTEND_DEBUG
5349 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5350 "Completing fct `%s' with ret type `%s'":
5351 "Completing arg `%s' with type `%s'"),
5352 IDENTIFIER_POINTER (EXPR_WFL_NODE
5353 (JDEP_DECL_WFL (dep))),
5354 IDENTIFIER_POINTER (DECL_NAME (decl))));
5355 }
5356 else
5357 error_found = 1;
5358 dep = JDEP_CHAIN (dep);
5359 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5360 break;
5361 else
5362 decl = jdep_resolve_class (dep);
5363 }
5364 if (!error_found)
5365 {
5366 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5367 /* Recompute and reset the signature, check first that
5368 all types are now defined. If they're not,
5369 dont build the signature. */
5370 if (check_method_types_complete (mdecl))
5371 {
5372 signature = build_java_signature (TREE_TYPE (mdecl));
5373 set_java_signature (TREE_TYPE (mdecl), signature);
5374 }
e04a16fb
AG
5375 }
5376 else
5377 continue;
5378 break;
5379
5380 case JDEP_INTERFACE:
5381 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5382 JDEP_WFL (dep)))
5383 continue;
5384 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5385 break;
5386
b67d701b 5387 case JDEP_PARM:
e04a16fb 5388 case JDEP_VARIABLE:
b67d701b
PB
5389 type = TREE_TYPE(decl);
5390 if (TREE_CODE (type) == RECORD_TYPE)
5391 type = promote_type (type);
5392 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5393 break;
5394
5395 case JDEP_TYPE:
5396 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5397 SOURCE_FRONTEND_DEBUG
5398 (("Completing a random type dependency on a '%s' node",
5399 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5400 break;
5401
b9f7e36c 5402 case JDEP_EXCEPTION:
c877974e
APB
5403 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5404 SOURCE_FRONTEND_DEBUG
5405 (("Completing `%s' `throws' argument node",
5406 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5407 break;
5408
c2952b01
APB
5409 case JDEP_ANONYMOUS:
5410 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5411 break;
5412
e04a16fb 5413 default:
400500c4 5414 abort ();
e04a16fb
AG
5415 }
5416 }
5417 }
e04a16fb
AG
5418 return;
5419}
5420
5421/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5422 array. */
5423
5424static tree
c2952b01
APB
5425resolve_class (enclosing, class_type, decl, cl)
5426 tree enclosing, class_type, decl, cl;
e04a16fb 5427{
49f48c71
KG
5428 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5429 const char *base = name;
78d21f92
PB
5430 tree resolved_type = TREE_TYPE (class_type);
5431 tree resolved_type_decl;
e04a16fb 5432
78d21f92
PB
5433 if (resolved_type != NULL_TREE)
5434 {
5435 tree resolved_type_decl = TYPE_NAME (resolved_type);
5436 if (resolved_type_decl == NULL_TREE
5437 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5438 {
5439 resolved_type_decl = build_decl (TYPE_DECL,
5440 TYPE_NAME (class_type),
5441 resolved_type);
5442 }
5443 return resolved_type_decl;
5444 }
5445
e04a16fb
AG
5446 /* 1- Check to see if we have an array. If true, find what we really
5447 want to resolve */
5448 while (name[0] == '[')
5449 name++;
5450 if (base != name)
34d4df06
APB
5451 {
5452 TYPE_NAME (class_type) = get_identifier (name);
5453 WFL_STRIP_BRACKET (cl, cl);
5454 }
e04a16fb
AG
5455
5456 /* 2- Resolve the bare type */
c2952b01
APB
5457 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5458 decl, cl)))
e04a16fb
AG
5459 return NULL_TREE;
5460 resolved_type = TREE_TYPE (resolved_type_decl);
5461
5462 /* 3- If we have and array, reconstruct the array down to its nesting */
5463 if (base != name)
5464 {
5465 while (base != name)
5466 {
e04a16fb 5467 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5468 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5469 name--;
5470 }
e101152f
APB
5471 /* A TYPE_NAME that is a TYPE_DECL was set in
5472 build_java_array_type, return it. */
5473 resolved_type_decl = TYPE_NAME (resolved_type);
e04a16fb 5474 }
78d21f92 5475 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5476 return resolved_type_decl;
5477}
5478
5479/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5480 are used to report error messages. */
5481
78d21f92 5482tree
c2952b01
APB
5483do_resolve_class (enclosing, class_type, decl, cl)
5484 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5485{
5486 tree new_class_decl;
e04a16fb
AG
5487
5488 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5489 it is changed by find_in_imports{_on_demand} and (but it doesn't
5490 really matter) qualify_and_find */
e04a16fb 5491
c2952b01
APB
5492 /* 0- Search in the current class as an inner class */
5493
5494 /* Maybe some code here should be added to load the class or
5495 something, at least if the class isn't an inner class and ended
5496 being loaded from class file. FIXME. */
a40d21da
APB
5497 while (enclosing)
5498 {
e1d565ab 5499 tree intermediate;
a40d21da
APB
5500
5501 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5502 return new_class_decl;
8ac1de05 5503
e1d565ab 5504 intermediate = enclosing;
0c2b8145 5505 /* Explore enclosing contexts. */
e1d565ab 5506 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5507 {
e1d565ab
BM
5508 intermediate = DECL_CONTEXT (intermediate);
5509 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5510 class_type, cl)))
5511 return new_class_decl;
5512 }
5513
a40d21da
APB
5514 /* Now go to the upper classes, bail out if necessary. */
5515 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5516 if (!enclosing || enclosing == object_type_node)
a648f4e4 5517 break;
a40d21da 5518
a648f4e4
BM
5519 if (TREE_CODE (enclosing) == POINTER_TYPE)
5520 enclosing = do_resolve_class (NULL, enclosing, NULL, NULL);
a40d21da 5521 else
a648f4e4 5522 enclosing = TYPE_NAME (enclosing);
a40d21da 5523 }
c2952b01 5524
9a7ab4b3
APB
5525 /* 1- Check for the type in single imports. This will change
5526 TYPE_NAME() if something relevant is found */
5527 find_in_imports (class_type);
e04a16fb 5528
9a7ab4b3 5529 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5530 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5531 {
5532 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5533 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5534 load_class (TYPE_NAME (class_type), 0);
5535 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5536 }
5537
9a7ab4b3
APB
5538 /* 3- Search according to the current package definition */
5539 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5540 {
5541 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5542 TYPE_NAME (class_type))))
5543 return new_class_decl;
5544 }
5545
5546 /* 4- Check the import on demands. Don't allow bar.baz to be
5547 imported from foo.* */
5548 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5549 if (find_in_imports_on_demand (class_type))
5550 return NULL_TREE;
5551
5552 /* If found in find_in_imports_on_demant, the type has already been
5553 loaded. */
5554 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5555 return new_class_decl;
5556
5557 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5558 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5559 {
ee07f4f4 5560 tree package;
d6baf6f5
APB
5561
5562 /* If there is a current package (ctxp->package), it's the first
5563 element of package_list and we can skip it. */
5564 for (package = (ctxp->package ?
5565 TREE_CHAIN (package_list) : package_list);
5566 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5567 if ((new_class_decl = qualify_and_find (class_type,
5568 TREE_PURPOSE (package),
5569 TYPE_NAME (class_type))))
5570 return new_class_decl;
5571 }
5572
5573 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb 5574 load_class (TYPE_NAME (class_type), 0);
a648f4e4
BM
5575
5576 if (!cl)
5577 cl = lookup_cl (decl);
5578
5579 /* If we don't have a value for CL, then we're being called recursively.
5580 We can't check package access just yet, but it will be taken care of
5581 by the caller. */
5582 if (cl)
5583 {
5584 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5585 return NULL_TREE;
5586 }
5587
9a7ab4b3 5588 /* 6- Last call for a resolution */
e04a16fb
AG
5589 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5590}
5591
9a7ab4b3
APB
5592static tree
5593qualify_and_find (class_type, package, name)
5594 tree class_type, package, name;
5595{
5596 tree new_qualified = merge_qualified_name (package, name);
5597 tree new_class_decl;
5598
5599 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5600 load_class (new_qualified, 0);
5601 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5602 {
5603 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5604 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5605 load_class (new_qualified, 0);
5606 TYPE_NAME (class_type) = new_qualified;
5607 return IDENTIFIER_CLASS_VALUE (new_qualified);
5608 }
5609 return NULL_TREE;
5610}
5611
e04a16fb 5612/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5613 parsed class). Return a decl node. This function is meant to be
5614 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5615
5616static tree
c877974e
APB
5617resolve_and_layout (something, cl)
5618 tree something;
e04a16fb
AG
5619 tree cl;
5620{
34d4df06 5621 tree decl, decl_type;
c877974e 5622
23a79c61
APB
5623 /* Don't do that on the current class */
5624 if (something == current_class)
5625 return TYPE_NAME (current_class);
c877974e 5626
23a79c61 5627 /* Don't do anything for void and other primitive types */
c877974e
APB
5628 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5629 return NULL_TREE;
5630
23a79c61
APB
5631 /* Pointer types can be reall pointer types or fake pointers. When
5632 finding a real pointer, recheck for primitive types */
5633 if (TREE_CODE (something) == POINTER_TYPE)
5634 {
5635 if (TREE_TYPE (something))
5636 {
5637 something = TREE_TYPE (something);
5638 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5639 return NULL_TREE;
5640 }
5641 else
5642 something = TYPE_NAME (something);
5643 }
5644
5645 /* Don't do anything for arrays of primitive types */
5646 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5647 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5648 return NULL_TREE;
5649
c2952b01
APB
5650 /* Something might be a WFL */
5651 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5652 something = EXPR_WFL_NODE (something);
5653
5654 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5655 TYPE_DECL or a real TYPE */
5656 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5657 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5658 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5659
23a79c61
APB
5660 if (!(decl = resolve_no_layout (something, cl)))
5661 return NULL_TREE;
5662
5663 /* Resolve and layout if necessary */
34d4df06
APB
5664 decl_type = TREE_TYPE (decl);
5665 layout_class_methods (decl_type);
5666 /* Check methods */
5667 if (CLASS_FROM_SOURCE_P (decl_type))
5668 java_check_methods (decl);
5669 /* Layout the type if necessary */
5670 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5671 safe_layout_class (decl_type);
23a79c61 5672
e04a16fb
AG
5673 return decl;
5674}
5675
5676/* Resolve a class, returns its decl but doesn't perform any
5677 layout. The current parsing context is saved and restored */
5678
5679static tree
5680resolve_no_layout (name, cl)
5681 tree name, cl;
5682{
5683 tree ptr, decl;
5684 BUILD_PTR_FROM_NAME (ptr, name);
5685 java_parser_context_save_global ();
c2952b01 5686 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5687 java_parser_context_restore_global ();
5688
5689 return decl;
5690}
5691
23a79c61
APB
5692/* Called when reporting errors. Skip leader '[' in a complex array
5693 type description that failed to be resolved. */
e04a16fb 5694
49f48c71 5695static const char *
e04a16fb 5696purify_type_name (name)
49f48c71 5697 const char *name;
e04a16fb
AG
5698{
5699 while (*name && *name == '[')
5700 name++;
5701 return name;
5702}
5703
5704/* The type CURRENT refers to can't be found. We print error messages. */
5705
5706static void
5707complete_class_report_errors (dep)
5708 jdep *dep;
5709{
49f48c71 5710 const char *name;
23a79c61
APB
5711
5712 if (!JDEP_WFL (dep))
5713 return;
5714
5715 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5716 switch (JDEP_KIND (dep))
5717 {
5718 case JDEP_SUPER:
5719 parse_error_context
5720 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5721 purify_type_name (name),
e04a16fb
AG
5722 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5723 break;
5724 case JDEP_FIELD:
5725 parse_error_context
5726 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5727 purify_type_name (name),
e04a16fb
AG
5728 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5729 break;
5730 case JDEP_METHOD: /* Covers arguments */
5731 parse_error_context
781b0558 5732 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5733 purify_type_name (name),
e04a16fb
AG
5734 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5735 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5736 break;
5737 case JDEP_METHOD_RETURN: /* Covers return type */
5738 parse_error_context
781b0558 5739 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5740 purify_type_name (name),
e04a16fb
AG
5741 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5742 break;
5743 case JDEP_INTERFACE:
5744 parse_error_context
5745 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5746 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5747 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5748 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5749 break;
5750 case JDEP_VARIABLE:
5751 parse_error_context
781b0558 5752 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5753 purify_type_name (IDENTIFIER_POINTER
5754 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5755 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5756 break;
b9f7e36c
APB
5757 case JDEP_EXCEPTION: /* As specified by `throws' */
5758 parse_error_context
5759 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5760 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5761 break;
0a2138e2
APB
5762 default:
5763 /* Fix for -Wall. Just break doing nothing. The error will be
5764 caught later */
5765 break;
e04a16fb
AG
5766 }
5767}
5768
22eed1e6
APB
5769/* Return a static string containing the DECL prototype string. If
5770 DECL is a constructor, use the class name instead of the form
5771 <init> */
5772
49f48c71 5773static const char *
22eed1e6
APB
5774get_printable_method_name (decl)
5775 tree decl;
5776{
49f48c71 5777 const char *to_return;
9ee9b555 5778 tree name = NULL_TREE;
22eed1e6
APB
5779
5780 if (DECL_CONSTRUCTOR_P (decl))
5781 {
5782 name = DECL_NAME (decl);
5e942c50 5783 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5784 }
5785
5786 to_return = lang_printable_name (decl, 0);
5787 if (DECL_CONSTRUCTOR_P (decl))
5788 DECL_NAME (decl) = name;
5789
5790 return to_return;
5791}
5792
5793/* Track method being redefined inside the same class. As a side
5794 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5795 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5796
e04a16fb
AG
5797static int
5798check_method_redefinition (class, method)
5799 tree class, method;
5800{
c4faeb92 5801 tree redef, sig;
5e942c50 5802
c4faeb92
APB
5803 /* There's no need to verify <clinit> and finit$ */
5804 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
e04a16fb 5805 return 0;
5e942c50 5806
c4faeb92 5807 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
e04a16fb
AG
5808 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5809 {
c3f2a476 5810 if (redef == method)
e04a16fb 5811 break;
c4faeb92
APB
5812 if (DECL_NAME (redef) == DECL_NAME (method)
5813 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5814 && !DECL_ARTIFICIAL (method))
e04a16fb 5815 {
22eed1e6 5816 parse_error_context
c4faeb92 5817 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
22eed1e6
APB
5818 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5819 get_printable_method_name (redef));
e04a16fb
AG
5820 return 1;
5821 }
5822 }
5823 return 0;
5824}
5825
1175b9b4
TT
5826/* Return 1 if check went ok, 0 otherwise. */
5827static int
d77613be
APB
5828check_abstract_method_definitions (do_interface, class_decl, type)
5829 int do_interface;
5830 tree class_decl, type;
5831{
5832 tree class = TREE_TYPE (class_decl);
5833 tree method, end_type;
1175b9b4 5834 int ok = 1;
d77613be
APB
5835
5836 end_type = (do_interface ? object_type_node : type);
5837 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5838 {
5839 tree other_super, other_method, method_sig, method_name;
5840 int found = 0;
165f37bc 5841 int end_type_reached = 0;
d77613be
APB
5842
5843 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5844 continue;
5845
5846 /* Now verify that somewhere in between TYPE and CLASS,
5847 abstract method METHOD gets a non abstract definition
5848 that is inherited by CLASS. */
5849
5850 method_sig = build_java_signature (TREE_TYPE (method));
5851 method_name = DECL_NAME (method);
5852 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5853 method_name = EXPR_WFL_NODE (method_name);
5854
165f37bc
APB
5855 other_super = class;
5856 do {
5857 if (other_super == end_type)
5858 end_type_reached = 1;
5859
5860 /* Method search */
5861 for (other_method = TYPE_METHODS (other_super); other_method;
5862 other_method = TREE_CHAIN (other_method))
5863 {
5864 tree s = build_java_signature (TREE_TYPE (other_method));
5865 tree other_name = DECL_NAME (other_method);
5866
5867 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5868 other_name = EXPR_WFL_NODE (other_name);
5869 if (!DECL_CLINIT_P (other_method)
5870 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5871 && method_name == other_name
5872 && method_sig == s
5873 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5874 {
5875 found = 1;
5876 break;
5877 }
5878 }
5879 other_super = CLASSTYPE_SUPER (other_super);
5880 } while (!end_type_reached);
5881
d77613be
APB
5882 /* Report that abstract METHOD didn't find an implementation
5883 that CLASS can use. */
5884 if (!found)
5885 {
c2e3db92 5886 char *t = xstrdup (lang_printable_name
d77613be
APB
5887 (TREE_TYPE (TREE_TYPE (method)), 0));
5888 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
d77613be
APB
5889
5890 parse_error_context
5891 (lookup_cl (class_decl),
781b0558 5892 "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract",
d77613be
APB
5893 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5894 t, lang_printable_name (method, 0),
5895 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5896 "interface" : "class"),
5897 IDENTIFIER_POINTER (ccn),
5898 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5899 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5900 ok = 0;
d77613be 5901 free (t);
d77613be
APB
5902 }
5903 }
1175b9b4
TT
5904
5905 if (ok && do_interface)
5906 {
5907 /* Check for implemented interfaces. */
5908 int i;
5909 tree vector = TYPE_BINFO_BASETYPES (type);
5910 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5911 {
5912 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5913 ok = check_abstract_method_definitions (1, class_decl, super);
5914 }
5915 }
5916
5917 return ok;
d77613be
APB
5918}
5919
614eaae0 5920/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5921 methods. */
5922
5923static void
5924java_check_abstract_method_definitions (class_decl)
5925 tree class_decl;
5926{
5927 tree class = TREE_TYPE (class_decl);
5928 tree super, vector;
5929 int i;
5930
5931 if (CLASS_ABSTRACT (class_decl))
5932 return;
5933
5934 /* Check for inherited types */
165f37bc
APB
5935 super = class;
5936 do {
5937 super = CLASSTYPE_SUPER (super);
5938 check_abstract_method_definitions (0, class_decl, super);
5939 } while (super != object_type_node);
d77613be
APB
5940
5941 /* Check for implemented interfaces. */
5942 vector = TYPE_BINFO_BASETYPES (class);
5943 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5944 {
5945 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5946 check_abstract_method_definitions (1, class_decl, super);
5947 }
5948}
5949
165f37bc
APB
5950/* Check all the types method DECL uses and return 1 if all of them
5951 are now complete, 0 otherwise. This is used to check whether its
5952 safe to build a method signature or not. */
5953
5954static int
5955check_method_types_complete (decl)
5956 tree decl;
5957{
5958 tree type = TREE_TYPE (decl);
5959 tree args;
5960
5961 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5962 return 0;
5963
5964 args = TYPE_ARG_TYPES (type);
5965 if (TREE_CODE (type) == METHOD_TYPE)
5966 args = TREE_CHAIN (args);
5967 for (; args != end_params_node; args = TREE_CHAIN (args))
5968 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5969 return 0;
5970
5971 return 1;
5972}
5973
34d4df06
APB
5974/* Visible interface to check methods contained in CLASS_DECL */
5975
5976void
5977java_check_methods (class_decl)
5978 tree class_decl;
5979{
5980 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5981 return;
5982
5983 if (CLASS_INTERFACE (class_decl))
5984 java_check_abstract_methods (class_decl);
5985 else
5986 java_check_regular_methods (class_decl);
5987
5988 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
5989}
5990
d77613be
APB
5991/* Check all the methods of CLASS_DECL. Methods are first completed
5992 then checked according to regular method existance rules. If no
5993 constructor for CLASS_DECL were encountered, then build its
5994 declaration. */
e04a16fb
AG
5995
5996static void
5997java_check_regular_methods (class_decl)
5998 tree class_decl;
5999{
c2952b01 6000 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6001 tree method;
6002 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
c4faeb92 6003 tree found = NULL_TREE;
c877974e
APB
6004 tree mthrows;
6005
6006 /* It is not necessary to check methods defined in java.lang.Object */
6007 if (class == object_type_node)
6008 return;
e04a16fb 6009
23a79c61
APB
6010 if (!TYPE_NVIRTUALS (class))
6011 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6012
6013 /* Should take interfaces into account. FIXME */
6014 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6015 {
5e942c50 6016 tree sig;
c4faeb92 6017 tree method_wfl = DECL_FUNCTION_WFL (method);
e04a16fb
AG
6018 int aflags;
6019
e04a16fb
AG
6020 /* Check for redefinitions */
6021 if (check_method_redefinition (class, method))
6022 continue;
6023
22eed1e6
APB
6024 /* If we see one constructor a mark so we don't generate the
6025 default one. Also skip other verifications: constructors
6026 can't be inherited hence hiden or overriden */
6027 if (DECL_CONSTRUCTOR_P (method))
6028 {
6029 saw_constructor = 1;
6030 continue;
6031 }
6032
c877974e
APB
6033 /* We verify things thrown by the method. They must inherits from
6034 java.lang.Throwable */
6035 for (mthrows = DECL_FUNCTION_THROWS (method);
6036 mthrows; mthrows = TREE_CHAIN (mthrows))
6037 {
6038 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6039 parse_error_context
781b0558 6040 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6041 IDENTIFIER_POINTER
6042 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6043 }
6044
e04a16fb 6045 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6046 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6047
c2952b01
APB
6048 /* Inner class can't declare static methods */
6049 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6050 {
6051 char *t = xstrdup (lang_printable_name (class, 0));
6052 parse_error_context
6053 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6054 lang_printable_name (method, 0), t);
6055 free (t);
6056 }
6057
5e942c50 6058 /* Nothing overrides or it's a private method. */
aabd7048 6059 if (!found)
5e942c50 6060 continue;
aabd7048
PB
6061 if (METHOD_PRIVATE (found))
6062 {
6063 found = NULL_TREE;
6064 continue;
6065 }
5e942c50 6066
614eaae0
APB
6067 /* If `found' is declared in an interface, make sure the
6068 modifier matches. */
6069 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6070 && clinit_identifier_node != DECL_NAME (found)
6071 && !METHOD_PUBLIC (method))
6072 {
6073 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6074 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6075 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6076 lang_printable_name (method, 0),
6077 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6078 }
6079
e04a16fb
AG
6080 /* Can't override a method with the same name and different return
6081 types. */
6082 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6083 {
614eaae0
APB
6084 char *t = xstrdup
6085 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6086 parse_error_context
7f10c2e2 6087 (method_wfl,
b9f7e36c 6088 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6089 lang_printable_name (found, 0), t,
b9f7e36c
APB
6090 IDENTIFIER_POINTER
6091 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6092 free (t);
6093 }
e04a16fb 6094
7f10c2e2 6095 aflags = get_access_flags_from_decl (found);
7f10c2e2 6096
e04a16fb
AG
6097 /* Can't override final. Can't override static. */
6098 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6099 {
6100 /* Static *can* override static */
6101 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6102 continue;
6103 parse_error_context
6104 (method_wfl,
6105 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6106 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6107 lang_printable_name (found, 0),
e04a16fb
AG
6108 (METHOD_FINAL (found) ? "final" : "static"),
6109 IDENTIFIER_POINTER
6110 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6111 continue;
6112 }
7f10c2e2 6113
e04a16fb
AG
6114 /* Static method can't override instance method. */
6115 if (METHOD_STATIC (method))
6116 {
6117 parse_error_context
6118 (method_wfl,
781b0558 6119 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6120 lang_printable_name (found, 0),
e04a16fb
AG
6121 IDENTIFIER_POINTER
6122 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6123 continue;
6124 }
5e942c50 6125
5e942c50
APB
6126 /* - Overriding/hiding public must be public
6127 - Overriding/hiding protected must be protected or public
6128 - If the overriden or hidden method has default (package)
6129 access, then the overriding or hiding method must not be
614eaae0
APB
6130 private; otherwise, a compile-time error occurs. If
6131 `found' belongs to an interface, things have been already
6132 taken care of. */
6133 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6134 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6135 || (METHOD_PROTECTED (found)
6136 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6137 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6138 && METHOD_PRIVATE (method))))
e04a16fb
AG
6139 {
6140 parse_error_context
6141 (method_wfl,
781b0558 6142 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6143 (METHOD_PUBLIC (method) ? "public" :
6144 (METHOD_PRIVATE (method) ? "private" : "protected")),
6145 IDENTIFIER_POINTER (DECL_NAME
6146 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6147 continue;
6148 }
6149
b9f7e36c
APB
6150 /* Overriding methods must have compatible `throws' clauses on checked
6151 exceptions, if any */
6152 check_throws_clauses (method, method_wfl, found);
6153
e04a16fb
AG
6154 /* Inheriting multiple methods with the same signature. FIXME */
6155 }
6156
23a79c61
APB
6157 if (!TYPE_NVIRTUALS (class))
6158 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6159
d77613be
APB
6160 /* Search for inherited abstract method not yet implemented in this
6161 class. */
6162 java_check_abstract_method_definitions (class_decl);
6163
22eed1e6 6164 if (!saw_constructor)
400500c4 6165 abort ();
e04a16fb
AG
6166}
6167
b9f7e36c
APB
6168/* Return a non zero value if the `throws' clause of METHOD (if any)
6169 is incompatible with the `throws' clause of FOUND (if any). */
6170
6171static void
6172check_throws_clauses (method, method_wfl, found)
6173 tree method, method_wfl, found;
6174{
6175 tree mthrows, fthrows;
6176
c877974e
APB
6177 /* Can't check these things with class loaded from bytecode. FIXME */
6178 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6179 return;
6180
b9f7e36c
APB
6181 for (mthrows = DECL_FUNCTION_THROWS (method);
6182 mthrows; mthrows = TREE_CHAIN (mthrows))
6183 {
6184 /* We don't verify unchecked expressions */
c877974e 6185 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6186 continue;
6187 /* Checked expression must be compatible */
6188 for (fthrows = DECL_FUNCTION_THROWS (found);
6189 fthrows; fthrows = TREE_CHAIN (fthrows))
6190 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6191 break;
6192 if (!fthrows)
6193 {
6194 parse_error_context
781b0558 6195 (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'",
b9f7e36c 6196 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6197 lang_printable_name (found, 0),
b9f7e36c
APB
6198 IDENTIFIER_POINTER
6199 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6200 }
6201 }
6202}
6203
e04a16fb
AG
6204/* Check abstract method of interface INTERFACE */
6205
6206static void
5e942c50
APB
6207java_check_abstract_methods (interface_decl)
6208 tree interface_decl;
e04a16fb
AG
6209{
6210 int i, n;
6211 tree method, basetype_vec, found;
5e942c50 6212 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6213
6214 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6215 {
e04a16fb
AG
6216 /* 2- Check for double definition inside the defining interface */
6217 if (check_method_redefinition (interface, method))
6218 continue;
6219
6220 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6221 the thrown exceptions (FIXME) */
e04a16fb
AG
6222 found = lookup_java_interface_method2 (interface, method);
6223 if (found)
6224 {
5e942c50 6225 char *t;
c2e3db92 6226 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6227 parse_error_context
c4faeb92 6228 (DECL_FUNCTION_WFL (found),
5e942c50 6229 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6230 lang_printable_name (found, 0), t,
b9f7e36c
APB
6231 IDENTIFIER_POINTER
6232 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6233 free (t);
c63b98cd 6234 continue;
e04a16fb
AG
6235 }
6236 }
6237
6238 /* 4- Inherited methods can't differ by their returned types */
6239 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6240 return;
6241 n = TREE_VEC_LENGTH (basetype_vec);
6242 for (i = 0; i < n; i++)
6243 {
6244 tree sub_interface_method, sub_interface;
6245 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6246 if (!vec_elt)
6247 continue;
6248 sub_interface = BINFO_TYPE (vec_elt);
6249 for (sub_interface_method = TYPE_METHODS (sub_interface);
6250 sub_interface_method;
6251 sub_interface_method = TREE_CHAIN (sub_interface_method))
6252 {
6253 found = lookup_java_interface_method2 (interface,
6254 sub_interface_method);
6255 if (found && (found != sub_interface_method))
5e942c50 6256 {
5e942c50
APB
6257 parse_error_context
6258 (lookup_cl (sub_interface_method),
781b0558 6259 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6260 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6261 lang_printable_name (found, 0),
6262 IDENTIFIER_POINTER
6263 (DECL_NAME (TYPE_NAME
6264 (DECL_CONTEXT (sub_interface_method)))),
6265 IDENTIFIER_POINTER
6266 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5e942c50 6267 }
e04a16fb
AG
6268 }
6269 }
6270}
6271
e04a16fb
AG
6272/* Lookup methods in interfaces using their name and partial
6273 signature. Return a matching method only if their types differ. */
6274
6275static tree
6276lookup_java_interface_method2 (class, method_decl)
6277 tree class, method_decl;
6278{
6279 int i, n;
6280 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6281
6282 if (!basetype_vec)
6283 return NULL_TREE;
6284
6285 n = TREE_VEC_LENGTH (basetype_vec);
6286 for (i = 0; i < n; i++)
6287 {
6288 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6289 if ((BINFO_TYPE (vec_elt) != object_type_node)
6290 && (to_return =
6291 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6292 return to_return;
6293 }
6294 for (i = 0; i < n; i++)
6295 {
6296 to_return = lookup_java_interface_method2
6297 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6298 if (to_return)
6299 return to_return;
6300 }
6301
6302 return NULL_TREE;
6303}
6304
6305/* Lookup method using their name and partial signature. Return a
6306 matching method only if their types differ. */
6307
6308static tree
6309lookup_java_method2 (clas, method_decl, do_interface)
6310 tree clas, method_decl;
6311 int do_interface;
6312{
5e942c50
APB
6313 tree method, method_signature, method_name, method_type, name;
6314
e04a16fb 6315 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6316 name = DECL_NAME (method_decl);
6317 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6318 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6319 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6320
6321 while (clas != NULL_TREE)
6322 {
6323 for (method = TYPE_METHODS (clas);
6324 method != NULL_TREE; method = TREE_CHAIN (method))
6325 {
6326 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6327 tree name = DECL_NAME (method);
6328 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6329 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6330 && method_sig == method_signature
6331 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6332 return method;
e04a16fb
AG
6333 }
6334 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6335 }
6336 return NULL_TREE;
6337}
6338
f441f671
APB
6339/* Return the line that matches DECL line number, and try its best to
6340 position the column number. Used during error reports. */
e04a16fb
AG
6341
6342static tree
6343lookup_cl (decl)
6344 tree decl;
6345{
6346 static tree cl = NULL_TREE;
f441f671 6347 char *line, *found;
e04a16fb
AG
6348
6349 if (!decl)
6350 return NULL_TREE;
6351
6352 if (cl == NULL_TREE)
19e223db
MM
6353 {
6354 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6355 ggc_add_tree_root (&cl, 1);
6356 }
e04a16fb
AG
6357
6358 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6359 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6360
dba41d30 6361 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6362 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6363
6364 found = strstr ((const char *)line,
6365 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6366 if (found)
6367 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6368
e04a16fb
AG
6369 return cl;
6370}
6371
6372/* Look for a simple name in the single-type import list */
6373
6374static tree
6375find_name_in_single_imports (name)
6376 tree name;
6377{
6378 tree node;
6379
6380 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6381 if (TREE_VALUE (node) == name)
6382 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6383
6384 return NULL_TREE;
6385}
6386
6387/* Process all single-type import. */
6388
6389static int
6390process_imports ()
6391{
6392 tree import;
6393 int error_found;
6394
6395 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6396 {
6397 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
1ebb5e73
APB
6398 char *original_name;
6399
6400 obstack_grow0 (&temporary_obstack,
6401 IDENTIFIER_POINTER (to_be_found),
6402 IDENTIFIER_LENGTH (to_be_found));
6403 original_name = obstack_finish (&temporary_obstack);
e04a16fb
AG
6404
6405 /* Don't load twice something already defined. */
6406 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6407 continue;
02ae6e2e
APB
6408
6409 while (1)
6410 {
6411 tree left;
6412
6413 QUALIFIED_P (to_be_found) = 1;
6414 load_class (to_be_found, 0);
6415 error_found =
6416 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6417
6418 /* We found it, we can bail out */
6419 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6420 break;
6421
6422 /* We haven't found it. Maybe we're trying to access an
6423 inner class. The only way for us to know is to try again
6424 after having dropped a qualifier. If we can't break it further,
6425 we have an error. */
6426 if (breakdown_qualified (&left, NULL, to_be_found))
6427 break;
6428
6429 to_be_found = left;
6430 }
e04a16fb
AG
6431 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6432 {
6433 parse_error_context (TREE_PURPOSE (import),
6434 "Class or interface `%s' not found in import",
1ebb5e73
APB
6435 original_name);
6436 error_found = 1;
e04a16fb 6437 }
1ebb5e73
APB
6438
6439 obstack_free (&temporary_obstack, original_name);
e04a16fb
AG
6440 if (error_found)
6441 return 1;
6442 }
6443 return 0;
6444}
6445
9a7ab4b3
APB
6446/* Possibly find and mark a class imported by a single-type import
6447 statement. */
e04a16fb 6448
9a7ab4b3 6449static void
e04a16fb
AG
6450find_in_imports (class_type)
6451 tree class_type;
6452{
6453 tree import;
6454
6455 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6456 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6457 {
6458 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6459 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6460 }
e04a16fb
AG
6461}
6462
e04a16fb 6463static int
63a212ed 6464note_possible_classname (name, len)
49f48c71 6465 const char *name;
63a212ed 6466 int len;
e04a16fb 6467{
63a212ed
PB
6468 tree node;
6469 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6470 len = len - 5;
6471 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6472 len = len - 6;
e04a16fb 6473 else
63a212ed
PB
6474 return 0;
6475 node = ident_subst (name, len, "", '/', '.', "");
6476 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6477 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6478 return 1;
e04a16fb
AG
6479}
6480
6481/* Read a import directory, gathering potential match for further type
6482 references. Indifferently reads a filesystem or a ZIP archive
6483 directory. */
6484
6485static void
6486read_import_dir (wfl)
6487 tree wfl;
6488{
63a212ed 6489 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6490 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6491 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6492 DIR *dirp = NULL;
d8fccff5 6493 JCF *saved_jcf = current_jcf;
e04a16fb 6494
63a212ed
PB
6495 int found = 0;
6496 int k;
6497 void *entry;
6498 struct buffer filename[1];
6499
6500
6501 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6502 return;
6503 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6504
6505 BUFFER_INIT (filename);
6506 buffer_grow (filename, package_length + 100);
6507
6508 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6509 {
49f48c71 6510 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6511 int entry_length = strlen (entry_name);
6512 if (jcf_path_is_zipfile (entry))
6513 {
6514 ZipFile *zipf;
6515 buffer_grow (filename, entry_length);
6516 memcpy (filename->data, entry_name, entry_length - 1);
6517 filename->data[entry_length-1] = '\0';
6518 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6519 if (zipf == NULL)
6520 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6521 else
6522 {
6523 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6524 BUFFER_RESET (filename);
6525 for (k = 0; k < package_length; k++)
6526 {
6527 char ch = package_name[k];
6528 *filename->ptr++ = ch == '.' ? '/' : ch;
6529 }
6530 *filename->ptr++ = '/';
6531
345137c7 6532 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6533 {
49f48c71 6534 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6535 int current_entry_len = zipd->filename_length;
6536
345137c7
TT
6537 if (current_entry_len >= BUFFER_LENGTH (filename)
6538 && strncmp (filename->data, current_entry,
6539 BUFFER_LENGTH (filename)) != 0)
63a212ed 6540 continue;
345137c7 6541 found |= note_possible_classname (current_entry,
63a212ed
PB
6542 current_entry_len);
6543 }
6544 }
6545 }
6546 else
6547 {
6548 BUFFER_RESET (filename);
6549 buffer_grow (filename, entry_length + package_length + 4);
6550 strcpy (filename->data, entry_name);
6551 filename->ptr = filename->data + entry_length;
6552 for (k = 0; k < package_length; k++)
6553 {
6554 char ch = package_name[k];
6555 *filename->ptr++ = ch == '.' ? '/' : ch;
6556 }
6557 *filename->ptr = '\0';
6558
6559 dirp = opendir (filename->data);
6560 if (dirp == NULL)
6561 continue;
6562 *filename->ptr++ = '/';
6563 for (;;)
6564 {
63a212ed 6565 int len;
49f48c71 6566 const char *d_name;
63a212ed
PB
6567 struct dirent *direntp = readdir (dirp);
6568 if (!direntp)
6569 break;
6570 d_name = direntp->d_name;
6571 len = strlen (direntp->d_name);
6572 buffer_grow (filename, len+1);
6573 strcpy (filename->ptr, d_name);
345137c7 6574 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6575 package_length+len+1);
6576 }
6577 if (dirp)
6578 closedir (dirp);
6579 }
6580 }
e04a16fb 6581
63a212ed 6582 free (filename->data);
e04a16fb 6583
63a212ed
PB
6584 /* Here we should have a unified way of retrieving an entry, to be
6585 indexed. */
6586 if (!found)
e04a16fb
AG
6587 {
6588 static int first = 1;
6589 if (first)
6590 {
781b0558 6591 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6592 java_error_count++;
6593 first = 0;
6594 }
6595 else
63a212ed
PB
6596 parse_error_context (wfl, "Package `%s' not found in import",
6597 package_name);
e04a16fb
AG
6598 current_jcf = saved_jcf;
6599 return;
6600 }
e04a16fb
AG
6601 current_jcf = saved_jcf;
6602}
6603
6604/* Possibly find a type in the import on demands specified
6605 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6606 entire list, to detected potential double definitions. */
6607
6608static int
6609find_in_imports_on_demand (class_type)
6610 tree class_type;
6611{
ab3a6dd6 6612 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6613 int seen_once = -1;
ab3a6dd6 6614 tree cl = NULL_TREE;
e04a16fb
AG
6615
6616 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6617 {
49f48c71 6618 const char *id_name;
e04a16fb
AG
6619 obstack_grow (&temporary_obstack,
6620 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6621 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6622 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6623 obstack_grow0 (&temporary_obstack,
6624 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6625 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6626 id_name = obstack_finish (&temporary_obstack);
6627
6628 node = maybe_get_identifier (id_name);
6629 if (node && IS_A_CLASSFILE_NAME (node))
6630 {
6631 if (seen_once < 0)
6632 {
6633 cl = TREE_PURPOSE (import);
6634 seen_once = 1;
6635 node_to_use = node;
6636 }
6637 else
6638 {
6639 seen_once++;
6640 parse_error_context
1e12ab9b
APB
6641 (TREE_PURPOSE (import),
6642 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6643 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6644 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6645 }
6646 }
6647 }
6648
6649 if (seen_once == 1)
6650 {
6651 /* Setup lineno so that it refers to the line of the import (in
6652 case we parse a class file and encounter errors */
6653 tree decl;
6654 int saved_lineno = lineno;
6655 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6656 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6657 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6658 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6659 /* If there is no DECL set for the class or if the class isn't
6660 loaded and not seen in source yet, the load */
6661 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6662 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
a648f4e4
BM
6663 {
6664 load_class (node_to_use, 0);
6665 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6666 }
e04a16fb 6667 lineno = saved_lineno;
a648f4e4
BM
6668 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6669 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6670 else
6671 /* 6.6.1: Inner classes are subject to member access rules. */
6672 return 0;
e04a16fb
AG
6673 }
6674 else
6675 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6676}
6677
9a7ab4b3
APB
6678/* Add package NAME to the list of package encountered so far. To
6679 speed up class lookup in do_resolve_class, we make sure a
6680 particular package is added only once. */
6681
6682static void
6683register_package (name)
6684 tree name;
6685{
6686 static struct hash_table _pht, *pht = NULL;
6687
6688 if (!pht)
6689 {
6690 hash_table_init (&_pht, hash_newfunc,
6691 java_hash_hash_tree_node, java_hash_compare_tree_node);
6692 pht = &_pht;
6693 }
6694
6695 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6696 {
6697 package_list = chainon (package_list, build_tree_list (name, NULL));
6698 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6699 }
6700}
6701
5e942c50
APB
6702static tree
6703resolve_package (pkg, next)
6704 tree pkg, *next;
6705{
c2952b01 6706 tree current, acc;
5e942c50 6707 tree type_name = NULL_TREE;
49f48c71 6708 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6709
6710 /* The trick is to determine when the package name stops and were
6711 the name of something contained in the package starts. Then we
6712 return a fully qualified name of what we want to get. */
6713
6714 /* Do a quick search on well known package names */
6715 if (!strncmp (name, "java.lang.reflect", 17))
6716 {
6717 *next =
6718 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6719 type_name = lookup_package_type (name, 17);
6720 }
6721 else if (!strncmp (name, "java.lang", 9))
6722 {
6723 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6724 type_name = lookup_package_type (name, 9);
6725 }
5e942c50 6726
2c56429a
APB
6727 /* If we found something here, return */
6728 if (type_name)
6729 return type_name;
6730
6731 *next = EXPR_WFL_QUALIFICATION (pkg);
6732
c2952b01
APB
6733 /* Try to progressively construct a type name */
6734 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6735 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6736 current; current = TREE_CHAIN (current))
6737 {
6738 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6739 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6740 {
6741 type_name = acc;
6b48deee
APB
6742 /* resolve_package should be used in a loop, hence we
6743 point at this one to naturally process the next one at
6744 the next iteration. */
6745 *next = current;
c2952b01
APB
6746 break;
6747 }
6748 }
2c56429a
APB
6749 return type_name;
6750}
6751
5e942c50
APB
6752static tree
6753lookup_package_type (name, from)
49f48c71 6754 const char *name;
5e942c50
APB
6755 int from;
6756{
6757 char subname [128];
49f48c71 6758 const char *sub = &name[from+1];
5e942c50
APB
6759 while (*sub != '.' && *sub)
6760 sub++;
6761 strncpy (subname, name, sub-name);
6762 subname [sub-name] = '\0';
6763 return get_identifier (subname);
6764}
6765
a648f4e4
BM
6766/* Check accessibility of inner classes according to member access rules.
6767 DECL is the inner class, ENCLOSING_DECL is the class from which the
6768 access is being attempted. */
6769
cf1748bf 6770static void
4dbf4496
APB
6771check_inner_class_access (decl, enclosing_decl, cl)
6772 tree decl, enclosing_decl, cl;
cf1748bf 6773{
a648f4e4 6774 const char *access;
064a552c 6775 tree enclosing_decl_type;
4dbf4496 6776
cf1748bf 6777 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6778 as a result of processing a JDEP crafted by source_start_java_method
6779 for the purpose of patching its parm decl. But the error would
6780 have been already trapped when fixing the method's signature.
6781 DECL can also be NULL in case of earlier errors. */
6782 if (!decl || !cl)
cf1748bf
APB
6783 return;
6784
064a552c 6785 enclosing_decl_type = TREE_TYPE (enclosing_decl);
a648f4e4 6786
4dbf4496 6787 if (CLASS_PRIVATE (decl))
a648f4e4
BM
6788 {
6789 /* Access is permitted only within the body of the top-level
6790 class in which DECL is declared. */
6791 tree top_level = decl;
6792 while (DECL_CONTEXT (top_level))
6793 top_level = DECL_CONTEXT (top_level);
6794 while (DECL_CONTEXT (enclosing_decl))
6795 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6796 if (top_level == enclosing_decl)
6797 return;
6798 access = "private";
6799 }
6800 else if (CLASS_PROTECTED (decl))
6801 {
6802 tree decl_context;
6803 /* Access is permitted from within the same package... */
6804 if (in_same_package (decl, enclosing_decl))
6805 return;
4dbf4496 6806
a648f4e4
BM
6807 /* ... or from within the body of a subtype of the context in which
6808 DECL is declared. */
6809 decl_context = DECL_CONTEXT (decl);
6810 while (enclosing_decl)
6811 {
6812 if (CLASS_INTERFACE (decl))
6813 {
6814 if (interface_of_p (TREE_TYPE (decl_context),
6815 enclosing_decl_type))
6816 return;
6817 }
6818 else
6819 {
6820 /* Eww. The order of the arguments is different!! */
6821 if (inherits_from_p (enclosing_decl_type,
6822 TREE_TYPE (decl_context)))
6823 return;
6824 }
6825 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6826 }
6827 access = "protected";
6828 }
6829 else if (! CLASS_PUBLIC (decl))
6830 {
6831 /* Access is permitted only from within the same package as DECL. */
6832 if (in_same_package (decl, enclosing_decl))
6833 return;
6834 access = "non-public";
6835 }
6836 else
6837 /* Class is public. */
4dbf4496
APB
6838 return;
6839
a648f4e4 6840 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
cf1748bf 6841 (CLASS_INTERFACE (decl) ? "interface" : "class"),
a648f4e4 6842 lang_printable_name (decl, 0), access);
cf1748bf
APB
6843}
6844
a648f4e4
BM
6845/* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6846 package, it must be PUBLIC. Return 0 if no access violations were found,
6847 1 otherwise. */
e04a16fb
AG
6848
6849static int
6850check_pkg_class_access (class_name, cl)
6851 tree class_name;
6852 tree cl;
6853{
6854 tree type;
e04a16fb 6855
a648f4e4 6856 if (!IDENTIFIER_CLASS_VALUE (class_name))
e04a16fb
AG
6857 return 0;
6858
6859 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6860 return 0;
6861
6862 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6863 {
e28cd97b
APB
6864 /* Access to a private class within the same package is
6865 allowed. */
6866 tree l, r;
6867 breakdown_qualified (&l, &r, class_name);
a648f4e4
BM
6868 if (!QUALIFIED_P (class_name) && !ctxp->package)
6869 /* Both in the empty package. */
6870 return 0;
e28cd97b 6871 if (l == ctxp->package)
a648f4e4 6872 /* Both in the same package. */
e28cd97b
APB
6873 return 0;
6874
e04a16fb 6875 parse_error_context
781b0558 6876 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6877 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6878 IDENTIFIER_POINTER (class_name));
6879 return 1;
6880 }
6881 return 0;
6882}
6883
6884/* Local variable declaration. */
6885
6886static void
6887declare_local_variables (modifier, type, vlist)
6888 int modifier;
6889 tree type;
6890 tree vlist;
6891{
c583dd46
APB
6892 tree decl, current, saved_type;
6893 tree type_wfl = NULL_TREE;
e04a16fb 6894 int must_chain = 0;
c2952b01 6895 int final_p = 0;
e04a16fb 6896
2aa11e97 6897 /* Push a new block if statements were seen between the last time we
b16e8f08 6898 pushed a block and now. Keep a count of blocks to close */
f099f336 6899 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6900 {
e04a16fb 6901 tree b = enter_block ();
b16e8f08 6902 BLOCK_IS_IMPLICIT (b) = 1;
e04a16fb
AG
6903 }
6904
6905 if (modifier)
6906 {
6907 int i;
6908 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6909 if (modifier == ACC_FINAL)
c2952b01 6910 final_p = 1;
c877974e
APB
6911 else
6912 {
6913 parse_error_context
6914 (ctxp->modifier_ctx [i],
6915 "Only `final' is allowed as a local variables modifier");
6916 return;
6917 }
e04a16fb
AG
6918 }
6919
c583dd46
APB
6920 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6921 hold the TYPE value if a new incomplete has to be created (as
6922 opposed to being found already existing and reused). */
6923 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6924
6925 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6926 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6927
6928 /* Go through all the declared variables */
6929 for (current = vlist, saved_type = type; current;
6930 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6931 {
c877974e 6932 tree other, real_type;
e04a16fb
AG
6933 tree wfl = TREE_PURPOSE (current);
6934 tree name = EXPR_WFL_NODE (wfl);
6935 tree init = TREE_VALUE (current);
e04a16fb 6936
c583dd46
APB
6937 /* Process NAME, as it may specify extra dimension(s) for it */
6938 type = build_array_from_name (type, type_wfl, name, &name);
6939
6940 /* Variable redefinition check */
6941 if ((other = lookup_name_in_blocks (name)))
6942 {
6943 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6944 DECL_SOURCE_LINE (other));
6945 continue;
6946 }
6947
6948 /* Type adjustment. We may have just readjusted TYPE because
6949 the variable specified more dimensions. Make sure we have
6950 a reference if we can and don't have one already. */
1886c9d8 6951 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6952
6953 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6954 /* Never layout this decl. This will be done when its scope
6955 will be entered */
c877974e 6956 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 6957 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 6958 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6959 BLOCK_CHAIN_DECL (decl);
6960
d4370213
APB
6961 /* If doing xreferencing, replace the line number with the WFL
6962 compound value */
6963 if (flag_emit_xref)
6964 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6965
e04a16fb
AG
6966 /* Don't try to use an INIT statement when an error was found */
6967 if (init && java_error_count)
6968 init = NULL_TREE;
c583dd46
APB
6969
6970 /* Add the initialization function to the current function's code */
6971 if (init)
e04a16fb 6972 {
c583dd46
APB
6973 /* Name might have been readjusted */
6974 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6975 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6976 java_method_add_stmt (current_function_decl,
6977 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6978 init));
6979 }
6980
6981 /* Setup dependency the type of the decl */
6982 if (must_chain)
6983 {
6984 jdep *dep;
6985 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6986 dep = CLASSD_LAST (ctxp->classd_list);
6987 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6988 }
6989 }
6990 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6991}
6992
6993/* Called during parsing. Build decls from argument list. */
6994
6995static void
6996source_start_java_method (fndecl)
6997 tree fndecl;
6998{
6999 tree tem;
7000 tree parm_decl;
7001 int i;
7002
79d13333
APB
7003 if (!fndecl)
7004 return;
7005
e04a16fb
AG
7006 current_function_decl = fndecl;
7007
7008 /* New scope for the function */
7009 enter_block ();
7010 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7011 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7012 {
7013 tree type = TREE_VALUE (tem);
7014 tree name = TREE_PURPOSE (tem);
7015
23a79c61
APB
7016 /* If type is incomplete. Create an incomplete decl and ask for
7017 the decl to be patched later */
e04a16fb
AG
7018 if (INCOMPLETE_TYPE_P (type))
7019 {
7020 jdep *jdep;
c877974e
APB
7021 tree real_type = GET_REAL_TYPE (type);
7022 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7023 type = obtain_incomplete_type (type);
e04a16fb
AG
7024 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7025 jdep = CLASSD_LAST (ctxp->classd_list);
7026 JDEP_MISC (jdep) = name;
7027 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7028 }
7029 else
7030 parm_decl = build_decl (PARM_DECL, name, type);
7031
c2952b01
APB
7032 /* Remember if a local variable was declared final (via its
7033 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7034 if (ARG_FINAL_P (tem))
c7303e41
APB
7035 {
7036 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7037 LOCAL_FINAL (parm_decl) = 1;
7038 }
c2952b01 7039
e04a16fb
AG
7040 BLOCK_CHAIN_DECL (parm_decl);
7041 }
7042 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7043 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7044 nreverse (tem);
7045 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7046 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7047}
7048
22eed1e6
APB
7049/* Called during parsing. Creates an artificial method declaration. */
7050
7051static tree
7052create_artificial_method (class, flags, type, name, args)
7053 tree class;
7054 int flags;
7055 tree type, name, args;
7056{
22eed1e6
APB
7057 tree mdecl;
7058
c2952b01 7059 java_parser_context_save_global ();
22eed1e6
APB
7060 lineno = 0;
7061 mdecl = make_node (FUNCTION_TYPE);
7062 TREE_TYPE (mdecl) = type;
7063 TYPE_ARG_TYPES (mdecl) = args;
7064 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7065 java_parser_context_restore_global ();
22eed1e6
APB
7066 DECL_ARTIFICIAL (mdecl) = 1;
7067 return mdecl;
7068}
7069
7070/* Starts the body if an artifical method. */
7071
7072static void
7073start_artificial_method_body (mdecl)
7074 tree mdecl;
7075{
7076 DECL_SOURCE_LINE (mdecl) = 1;
7077 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7078 source_start_java_method (mdecl);
7079 enter_block ();
7080}
7081
7082static void
7083end_artificial_method_body (mdecl)
7084 tree mdecl;
7085{
1e0cdc10
APB
7086 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7087 It has to be evaluated first. (if mdecl is current_function_decl,
7088 we have an undefined behavior if no temporary variable is used.) */
7089 tree b = exit_block ();
7090 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7091 exit_block ();
7092}
7093
e04a16fb
AG
7094/* Terminate a function and expand its body. */
7095
7096static void
7097source_end_java_method ()
7098{
7099 tree fndecl = current_function_decl;
138657ec 7100 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7101
79d13333
APB
7102 if (!fndecl)
7103 return;
7104
e04a16fb
AG
7105 java_parser_context_save_global ();
7106 lineno = ctxp->last_ccb_indent1;
7107
b67d701b
PB
7108 /* Set EH language codes */
7109 java_set_exception_lang_code ();
7110
5423609c
APB
7111 /* Turn function bodies with only a NOP expr null, so they don't get
7112 generated at all and we won't get warnings when using the -W
7113 -Wall flags. */
7114 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7115 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7116
e04a16fb
AG
7117 /* Generate function's code */
7118 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7119 && ! flag_emit_class_files
7120 && ! flag_emit_xref)
e04a16fb
AG
7121 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7122
7123 /* pop out of its parameters */
7124 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7125 poplevel (1, 0, 1);
7126 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7127
7128 /* Generate rtl for function exit. */
e8fc7396 7129 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7130 {
7131 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7132 /* Emit catch-finally clauses */
7133 emit_handlers ();
e04a16fb
AG
7134 expand_function_end (input_filename, lineno, 0);
7135
138657ec
AH
7136 /* FIXME: If the current method contains any exception handlers,
7137 force asynchronous_exceptions: this is necessary because signal
7138 handlers in libjava may throw exceptions. This is far from being
7139 a perfect solution, but it's better than doing nothing at all.*/
7140 if (catch_clauses)
7141 asynchronous_exceptions = 1;
7142
e04a16fb
AG
7143 /* Run the optimizers and output assembler code for this function. */
7144 rest_of_compilation (fndecl);
7145 }
7146
7147 current_function_decl = NULL_TREE;
e04a16fb 7148 java_parser_context_restore_global ();
138657ec 7149 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7150}
7151
7152/* Record EXPR in the current function block. Complements compound
7153 expression second operand if necessary. */
7154
7155tree
7156java_method_add_stmt (fndecl, expr)
7157 tree fndecl, expr;
7158{
b771925e
APB
7159 if (!GET_CURRENT_BLOCK (fndecl))
7160 return NULL_TREE;
f099f336 7161 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7162}
e04a16fb 7163
b67d701b
PB
7164static tree
7165add_stmt_to_block (b, type, stmt)
7166 tree b, type, stmt;
7167{
7168 tree body = BLOCK_EXPR_BODY (b), c;
7169
e04a16fb
AG
7170 if (java_error_count)
7171 return body;
b67d701b
PB
7172
7173 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7174 return body;
7175
b67d701b
PB
7176 BLOCK_EXPR_BODY (b) = c;
7177 TREE_SIDE_EFFECTS (c) = 1;
7178 return c;
e04a16fb
AG
7179}
7180
7181/* Add STMT to EXISTING if possible, otherwise create a new
7182 COMPOUND_EXPR and add STMT to it. */
7183
7184static tree
7185add_stmt_to_compound (existing, type, stmt)
7186 tree existing, type, stmt;
7187{
15fdcfe9
PB
7188 if (existing)
7189 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7190 else
15fdcfe9 7191 return stmt;
e04a16fb
AG
7192}
7193
1886c9d8
APB
7194void java_layout_seen_class_methods ()
7195{
7196 tree previous_list = all_class_list;
7197 tree end = NULL_TREE;
7198 tree current;
7199
7200 while (1)
7201 {
7202 for (current = previous_list;
7203 current != end; current = TREE_CHAIN (current))
7204 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7205
7206 if (previous_list != all_class_list)
7207 {
7208 end = previous_list;
7209 previous_list = all_class_list;
7210 }
7211 else
7212 break;
7213 }
7214}
7215
e04a16fb 7216void
c2952b01 7217java_reorder_fields ()
e04a16fb 7218{
c2952b01 7219 static tree stop_reordering = NULL_TREE;
19e223db 7220 static int initialized_p;
c2952b01 7221 tree current;
19e223db
MM
7222
7223 /* Register STOP_REORDERING with the garbage collector. */
7224 if (!initialized_p)
7225 {
7226 ggc_add_tree_root (&stop_reordering, 1);
7227 initialized_p = 1;
7228 }
7229
fea2d5da 7230 for (current = gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7231 {
5e942c50 7232 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7233
c2952b01
APB
7234 if (current_class == stop_reordering)
7235 break;
7236
c877974e
APB
7237 /* Reverse the fields, but leave the dummy field in front.
7238 Fields are already ordered for Object and Class */
7239 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7240 && current_class != class_type_node)
7241 {
23a79c61
APB
7242 /* If the dummy field is there, reverse the right fields and
7243 just layout the type for proper fields offset */
c877974e
APB
7244 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7245 {
7246 tree fields = TYPE_FIELDS (current_class);
7247 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7248 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7249 }
23a79c61
APB
7250 /* We don't have a dummy field, we need to layout the class,
7251 after having reversed the fields */
c877974e
APB
7252 else
7253 {
7254 TYPE_FIELDS (current_class) =
7255 nreverse (TYPE_FIELDS (current_class));
7256 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7257 }
7258 }
c2952b01 7259 }
fea2d5da 7260 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
c2952b01
APB
7261}
7262
fea2d5da
PB
7263/* Layout the methods of all classes loaded in one way or another.
7264 Check methods of source parsed classes. Then reorder the
c2952b01
APB
7265 fields and layout the classes or the type of all source parsed
7266 classes */
7267
7268void
7269java_layout_classes ()
7270{
7271 tree current;
7272 int save_error_count = java_error_count;
7273
7274 /* Layout the methods of all classes seen so far */
7275 java_layout_seen_class_methods ();
7276 java_parse_abort_on_error ();
7277 all_class_list = NULL_TREE;
7278
7279 /* Then check the methods of all parsed classes */
fea2d5da 7280 for (current = gclass_list; current; current = TREE_CHAIN (current))
c2952b01 7281 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7282 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7283 java_parse_abort_on_error ();
7284
fea2d5da 7285 for (current = gclass_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7286 {
7287 current_class = TREE_TYPE (TREE_VALUE (current));
7288 layout_class (current_class);
5e942c50 7289
c877974e
APB
7290 /* From now on, the class is considered completely loaded */
7291 CLASS_LOADED_P (current_class) = 1;
7292
5e942c50
APB
7293 /* Error reported by the caller */
7294 if (java_error_count)
7295 return;
e04a16fb 7296 }
23a79c61
APB
7297
7298 /* We might have reloaded classes durign the process of laying out
7299 classes for code generation. We must layout the methods of those
7300 late additions, as constructor checks might use them */
1886c9d8 7301 java_layout_seen_class_methods ();
23a79c61 7302 java_parse_abort_on_error ();
e04a16fb
AG
7303}
7304
c2952b01
APB
7305/* Expand methods in the current set of classes rememebered for
7306 generation. */
e04a16fb 7307
49f48c71 7308static void
c2952b01 7309java_complete_expand_classes ()
e04a16fb
AG
7310{
7311 tree current;
ce6e9147
APB
7312
7313 do_not_fold = flag_emit_xref;
c2952b01 7314
e04a16fb 7315 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7316 if (!INNER_CLASS_DECL_P (current))
7317 java_complete_expand_class (current);
7318}
e04a16fb 7319
c2952b01
APB
7320/* Expand the methods found in OUTER, starting first by OUTER's inner
7321 classes, if any. */
e04a16fb 7322
c2952b01
APB
7323static void
7324java_complete_expand_class (outer)
7325 tree outer;
7326{
7327 tree inner_list;
e04a16fb 7328
c2952b01 7329 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7330
c2952b01
APB
7331 /* We need to go after all inner classes and start expanding them,
7332 starting with most nested ones. We have to do that because nested
7333 classes might add functions to outer classes */
e04a16fb 7334
c2952b01
APB
7335 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7336 inner_list; inner_list = TREE_CHAIN (inner_list))
7337 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7338
c2952b01
APB
7339 java_complete_expand_methods (outer);
7340 set_nested_class_simple_name_value (outer, 0); /* Reset */
7341}
7342
7343/* Expand methods registered in CLASS_DECL. The general idea is that
7344 we expand regular methods first. This allows us get an estimate on
7345 how outer context local alias fields are really used so we can add
7346 to the constructor just enough code to initialize them properly (it
c00f0fb2 7347 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7348 constructors and then <clinit>. */
7349
7350static void
7351java_complete_expand_methods (class_decl)
7352 tree class_decl;
7353{
7354 tree clinit, finit, decl, first_decl;
7355
7356 current_class = TREE_TYPE (class_decl);
7357
c7303e41
APB
7358 /* Find whether the class has final variables */
7359 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7360 if (FIELD_FINAL (decl))
7361 {
7362 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7363 break;
7364 }
7365
c2952b01
APB
7366 /* Initialize a new constant pool */
7367 init_outgoing_cpool ();
7368
7369 /* Pre-expand <clinit> to figure whether we really need it or
7370 not. If we do need it, we pre-expand the static fields so they're
7371 ready to be used somewhere else. <clinit> will be fully expanded
7372 after we processed the constructors. */
7373 first_decl = TYPE_METHODS (current_class);
7374 clinit = maybe_generate_pre_expand_clinit (current_class);
7375
c00f0fb2 7376 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7377 try to use it.*/
7378 if (TYPE_FINIT_STMT_LIST (current_class))
7379 {
7380 finit = generate_finit (current_class);
7381 java_complete_expand_method (finit);
7382 }
7383
7384 /* Now do the constructors */
7385 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7386 {
7387 int no_body;
7388
7389 if (!DECL_CONSTRUCTOR_P (decl))
7390 continue;
7391
7392 no_body = !DECL_FUNCTION_BODY (decl);
7393 /* Don't generate debug info on line zero when expanding a
7394 generated constructor. */
7395 if (no_body)
7396 restore_line_number_status (1);
7397
c7303e41
APB
7398 /* Reset the final local variable assignment flags */
7399 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7400 reset_final_variable_local_assignment_flag (current_class);
7401
c2952b01 7402 java_complete_expand_method (decl);
c7303e41
APB
7403
7404 /* Check for missed out final variable assignment */
7405 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7406 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7407
7408 if (no_body)
7409 restore_line_number_status (0);
7410 }
7411
7412 /* First, do the ordinary methods. */
7413 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7414 {
7145d9fe
TT
7415 /* Skip abstract or native methods -- but do handle native
7416 methods when generating JNI stubs. */
7417 if (METHOD_ABSTRACT (decl)
7418 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7419 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7420 continue;
7145d9fe
TT
7421
7422 if (METHOD_NATIVE (decl))
7423 {
7424 tree body = build_jni_stub (decl);
7425 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7426 }
7427
c2952b01
APB
7428 java_complete_expand_method (decl);
7429 }
7430
7431 /* If there is indeed a <clinit>, fully expand it now */
7432 if (clinit)
7433 {
c7303e41
APB
7434 /* Reset the final local variable assignment flags */
7435 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7436 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7437 /* Prevent the use of `this' inside <clinit> */
7438 ctxp->explicit_constructor_p = 1;
7439 java_complete_expand_method (clinit);
7440 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7441 /* Check for missed out static final variable assignment */
7442 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7443 && !CLASS_INTERFACE (class_decl))
7444 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7445 }
c2952b01 7446
165f37bc
APB
7447 /* We might have generated a class$ that we now want to expand */
7448 if (TYPE_DOT_CLASS (current_class))
7449 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7450
c2952b01
APB
7451 /* Now verify constructor circularity (stop after the first one we
7452 prove wrong.) */
7453 if (!CLASS_INTERFACE (class_decl))
7454 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7455 if (DECL_CONSTRUCTOR_P (decl)
7456 && verify_constructor_circularity (decl, decl))
7457 break;
7458
c7303e41
APB
7459 /* Final check on the initialization of final variables. */
7460 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7461 {
7462 check_final_variable_global_assignment_flag (current_class);
7463 /* If we have an interface, check for uninitialized fields. */
7464 if (CLASS_INTERFACE (class_decl))
7465 check_static_final_variable_assignment_flag (current_class);
7466 }
7467
c2952b01
APB
7468 /* Save the constant pool. We'll need to restore it later. */
7469 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7470}
7471
c2952b01
APB
7472/* Attempt to create <clinit>. Pre-expand static fields so they can be
7473 safely used in some other methods/constructors. */
e920ebc9 7474
c2952b01
APB
7475static tree
7476maybe_generate_pre_expand_clinit (class_type)
7477 tree class_type;
e920ebc9 7478{
c2952b01
APB
7479 tree current, mdecl;
7480
7481 if (!TYPE_CLINIT_STMT_LIST (class_type))
7482 return NULL_TREE;
e920ebc9 7483
c2952b01
APB
7484 /* Go through all static fields and pre expand them */
7485 for (current = TYPE_FIELDS (class_type); current;
7486 current = TREE_CHAIN (current))
7487 if (FIELD_STATIC (current))
7488 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7489
7490 /* Then build the <clinit> method */
7491 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7492 clinit_identifier_node, end_params_node);
7493 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7494 mdecl, NULL_TREE);
7495 start_artificial_method_body (mdecl);
7496
7497 /* We process the list of assignment we produced as the result of
7498 the declaration of initialized static field and add them as
7499 statement to the <clinit> method. */
7500 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7501 current = TREE_CHAIN (current))
e920ebc9 7502 {
9a7ab4b3 7503 tree stmt = current;
c2952b01
APB
7504 /* We build the assignment expression that will initialize the
7505 field to its value. There are strict rules on static
7506 initializers (8.5). FIXME */
98a52c2c 7507 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7508 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7509 java_method_add_stmt (mdecl, stmt);
7510 }
e920ebc9 7511
c2952b01
APB
7512 end_artificial_method_body (mdecl);
7513
92d83515
APB
7514 /* Now we want to place <clinit> as the last method (because we need
7515 it at least for interface so that it doesn't interfere with the
7516 dispatch table based lookup. */
7517 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7518 {
92d83515
APB
7519 current = TREE_CHAIN (TYPE_METHODS (class_type));
7520 TYPE_METHODS (class_type) = current;
c2952b01
APB
7521
7522 while (TREE_CHAIN (current))
7523 current = TREE_CHAIN (current);
92d83515 7524
c2952b01
APB
7525 TREE_CHAIN (current) = mdecl;
7526 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7527 }
c2952b01
APB
7528
7529 return mdecl;
e920ebc9
APB
7530}
7531
dba41d30
APB
7532/* Analyzes a method body and look for something that isn't a
7533 MODIFY_EXPR. */
7534
7535static int
7536analyze_clinit_body (bbody)
7537 tree bbody;
7538{
7539 while (bbody)
7540 switch (TREE_CODE (bbody))
7541 {
7542 case BLOCK:
7543 bbody = BLOCK_EXPR_BODY (bbody);
7544 break;
7545
7546 case EXPR_WITH_FILE_LOCATION:
7547 bbody = EXPR_WFL_NODE (bbody);
7548 break;
7549
7550 case COMPOUND_EXPR:
7551 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7552 return 1;
7553 bbody = TREE_OPERAND (bbody, 1);
7554 break;
7555
7556 case MODIFY_EXPR:
7557 bbody = NULL_TREE;
7558 break;
7559
7560 default:
7561 bbody = NULL_TREE;
7562 return 1;
7563 }
7564 return 0;
7565}
7566
7567
92d83515
APB
7568/* See whether we could get rid of <clinit>. Criteria are: all static
7569 final fields have constant initial values and the body of <clinit>
7570 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7571
7572static int
7573maybe_yank_clinit (mdecl)
7574 tree mdecl;
7575{
7576 tree type, current;
7577 tree fbody, bbody;
99eaf8d4 7578 int found = 0;
92d83515
APB
7579
7580 if (!DECL_CLINIT_P (mdecl))
7581 return 0;
f0f3a777
APB
7582
7583 /* If the body isn't empty, then we keep <clinit>. Note that if
7584 we're emitting classfiles, this isn't enough not to rule it
7585 out. */
92d83515 7586 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7587 bbody = BLOCK_EXPR_BODY (fbody);
7588 if (bbody && bbody != error_mark_node)
92d83515 7589 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7590 else
7591 return 0;
f0f3a777 7592 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7593 return 0;
7594
7595 type = DECL_CONTEXT (mdecl);
7596 current = TYPE_FIELDS (type);
7597
7598 for (current = (current ? TREE_CHAIN (current) : current);
7599 current; current = TREE_CHAIN (current))
f0f3a777
APB
7600 {
7601 tree f_init;
7602
7603 /* We're not interested in non static field */
7604 if (!FIELD_STATIC (current))
7605 continue;
7606
7607 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7608 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7609 should generated an empty <clinit> so that SUID are computed
7610 correctly. */
7611 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7612 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7613 break;
7614
7615 f_init = DECL_INITIAL (current);
7616 /* If we're emitting native code, we want static final fields to
7617 have constant initializers. If we don't meet these
7618 conditions, we keep <clinit> */
7619 if (!flag_emit_class_files
7620 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7621 break;
7622 /* If we're emitting bytecode, we want static fields to have
7623 constant initializers or no initializer. If we don't meet
7624 these conditions, we keep <clinit> */
7625 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7626 break;
7627 }
92d83515 7628
99eaf8d4
APB
7629 /* Now we analyze the method body and look for something that
7630 isn't a MODIFY_EXPR */
7631 if (bbody == empty_stmt_node)
dba41d30
APB
7632 found = 0;
7633 else
7634 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7635
7636 if (current || found)
92d83515
APB
7637 return 0;
7638
7639 /* Get rid of <clinit> in the class' list of methods */
7640 if (TYPE_METHODS (type) == mdecl)
7641 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7642 else
7643 for (current = TYPE_METHODS (type); current;
7644 current = TREE_CHAIN (current))
7645 if (TREE_CHAIN (current) == mdecl)
7646 {
7647 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7648 break;
7649 }
7650
7651 return 1;
7652}
7653
7654
e04a16fb
AG
7655/* Complete and expand a method. */
7656
7657static void
7658java_complete_expand_method (mdecl)
7659 tree mdecl;
7660{
92d83515
APB
7661 int yank_clinit = 0;
7662
c2952b01 7663 current_function_decl = mdecl;
22eed1e6
APB
7664 /* Fix constructors before expanding them */
7665 if (DECL_CONSTRUCTOR_P (mdecl))
7666 fix_constructors (mdecl);
e04a16fb 7667
22eed1e6 7668 /* Expand functions that have a body */
e04a16fb
AG
7669 if (DECL_FUNCTION_BODY (mdecl))
7670 {
9bbc7d9f
PB
7671 tree fbody = DECL_FUNCTION_BODY (mdecl);
7672 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7673 tree exception_copy = NULL_TREE;
47a50de9
PB
7674 tree tem, *ptr;
7675
7676 current_function_decl = mdecl;
7677
7678 if (! quiet_flag)
7679 fprintf (stderr, " [%s.",
7680 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7681 announce_function (mdecl);
7682 if (! quiet_flag)
7683 fprintf (stderr, "]");
7684
7685 pushlevel (1); /* Prepare for a parameter push */
7686 ptr = &DECL_ARGUMENTS (mdecl);
7687 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7688 while (tem)
7689 {
7690 tree next = TREE_CHAIN (tem);
7691 tree type = TREE_TYPE (tem);
7692 if (PROMOTE_PROTOTYPES
7693 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7694 && INTEGRAL_TYPE_P (type))
7695 type = integer_type_node;
7696 DECL_ARG_TYPE (tem) = type;
7697 layout_decl (tem, 0);
7698 pushdecl (tem);
7699 *ptr = tem;
7700 ptr = &TREE_CHAIN (tem);
7701 tem = next;
7702 }
7703 *ptr = NULL_TREE;
7704 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7705 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7706
939d7216 7707 build_result_decl (mdecl);
e04a16fb
AG
7708
7709 current_this
7710 = (!METHOD_STATIC (mdecl) ?
7711 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7712
ce6e9147
APB
7713 /* Purge the `throws' list of unchecked exceptions. If we're
7714 doing xref, save a copy of the list and re-install it
7715 later. */
7716 if (flag_emit_xref)
7717 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7718
b9f7e36c
APB
7719 purge_unchecked_exceptions (mdecl);
7720
7721 /* Install exceptions thrown with `throws' */
7722 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7723
9bbc7d9f 7724 if (block_body != NULL_TREE)
bc3ca41b
PB
7725 {
7726 block_body = java_complete_tree (block_body);
c2952b01 7727
7145d9fe 7728 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7729 check_for_initialization (block_body);
f099f336 7730 ctxp->explicit_constructor_p = 0;
bc3ca41b 7731 }
e803d3b2 7732
9bbc7d9f 7733 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7734
c2952b01
APB
7735 /* If we saw a return but couldn't evaluate it properly, we'll
7736 have an error_mark_node here. */
7737 if (block_body != error_mark_node
7738 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7739 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7740 && !flag_emit_xref)
82371d41 7741 missing_return_error (current_function_decl);
7525cc04 7742
92d83515
APB
7743 /* Check wether we could just get rid of clinit, now the picture
7744 is complete. */
7745 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7746 complete_start_java_method (mdecl);
7747
e04a16fb 7748 /* Don't go any further if we've found error(s) during the
92d83515
APB
7749 expansion */
7750 if (!java_error_count && !yank_clinit)
e04a16fb 7751 source_end_java_method ();
22eed1e6
APB
7752 else
7753 {
92d83515
APB
7754 if (java_error_count)
7755 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7756 poplevel (1, 0, 1);
7757 }
b9f7e36c
APB
7758
7759 /* Pop the exceptions and sanity check */
7760 POP_EXCEPTIONS();
7761 if (currently_caught_type_list)
400500c4 7762 abort ();
ce6e9147
APB
7763
7764 if (flag_emit_xref)
7765 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7766 }
7767}
7768
c2952b01
APB
7769\f
7770
7771/* This section of the code deals with accessing enclosing context
7772 fields either directly by using the relevant access to this$<n> or
7773 by invoking an access method crafted for that purpose. */
7774
7775/* Build the necessary access from an inner class to an outer
7776 class. This routine could be optimized to cache previous result
7777 (decl, current_class and returned access). When an access method
7778 needs to be generated, it always takes the form of a read. It might
7779 be later turned into a write by calling outer_field_access_fix. */
7780
7781static tree
7782build_outer_field_access (id, decl)
7783 tree id, decl;
7784{
7785 tree access = NULL_TREE;
7786 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7787 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7788
5e18f6d6
APB
7789 /* If the immediate enclosing context of the current class is the
7790 field decl's class or inherits from it; build the access as
7791 `this$<n>.<field>'. Note that we will break the `private' barrier
7792 if we're not emitting bytecodes. */
7793 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7794 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7795 {
7796 tree thisn = build_current_thisn (current_class);
7797 access = make_qualified_primary (build_wfl_node (thisn),
7798 id, EXPR_WFL_LINECOL (id));
7799 }
7800 /* Otherwise, generate access methods to outer this and access the
7801 field (either using an access method or by direct access.) */
7802 else
7803 {
7804 int lc = EXPR_WFL_LINECOL (id);
7805
7806 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7807 get a hold to the enclosing instance we need, and then we
c2952b01 7808 build the field access. */
5e18f6d6 7809 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7810
7811 /* If the field is private and we're generating bytecode, then
7812 we generate an access method */
7813 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7814 {
7815 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7816 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7817 name, access, NULL_TREE);
7818 }
7819 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7820 Once again we break the `private' access rule from a foreign
7821 class. */
7822 else
7823 access = make_qualified_primary (access, id, lc);
7824 }
7825 return resolve_expression_name (access, NULL);
7826}
7827
7828/* Return a non zero value if NODE describes an outer field inner
7829 access. */
7830
7831static int
7832outer_field_access_p (type, decl)
7833 tree type, decl;
7834{
7835 if (!INNER_CLASS_TYPE_P (type)
7836 || TREE_CODE (decl) != FIELD_DECL
7837 || DECL_CONTEXT (decl) == type)
7838 return 0;
ee5f86dc
APB
7839
7840 /* If the inner class extends the declaration context of the field
7841 we're try to acces, then this isn't an outer field access */
7842 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7843 return 0;
c2952b01
APB
7844
7845 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7846 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7847 {
7848 if (type == DECL_CONTEXT (decl))
7849 return 1;
5e18f6d6 7850
c2952b01 7851 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7852 {
7853 /* Before we give up, see whether the field is inherited from
7854 the enclosing context we're considering. */
7855 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7856 return 1;
7857 break;
7858 }
c2952b01
APB
7859 }
7860
7861 return 0;
7862}
7863
7864/* Return a non zero value if NODE represents an outer field inner
7865 access that was been already expanded. As a side effect, it returns
7866 the name of the field being accessed and the argument passed to the
7867 access function, suitable for a regeneration of the access method
7868 call if necessary. */
7869
7870static int
7871outer_field_expanded_access_p (node, name, arg_type, arg)
7872 tree node, *name, *arg_type, *arg;
7873{
7874 int identified = 0;
7875
7876 if (TREE_CODE (node) != CALL_EXPR)
7877 return 0;
7878
7879 /* Well, gcj generates slightly different tree nodes when compiling
7880 to native or bytecodes. It's the case for function calls. */
7881
7882 if (flag_emit_class_files
7883 && TREE_CODE (node) == CALL_EXPR
7884 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7885 identified = 1;
7886 else if (!flag_emit_class_files)
7887 {
7888 node = TREE_OPERAND (node, 0);
7889
7890 if (node && TREE_OPERAND (node, 0)
7891 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7892 {
7893 node = TREE_OPERAND (node, 0);
7894 if (TREE_OPERAND (node, 0)
7895 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7896 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7897 (DECL_NAME (TREE_OPERAND (node, 0)))))
7898 identified = 1;
7899 }
7900 }
7901
7902 if (identified && name && arg_type && arg)
7903 {
7904 tree argument = TREE_OPERAND (node, 1);
7905 *name = DECL_NAME (TREE_OPERAND (node, 0));
7906 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7907 *arg = TREE_VALUE (argument);
7908 }
7909 return identified;
7910}
7911
7912/* Detect in NODE an outer field read access from an inner class and
7913 transform it into a write with RHS as an argument. This function is
7914 called from the java_complete_lhs when an assignment to a LHS can
7915 be identified. */
7916
7917static tree
7918outer_field_access_fix (wfl, node, rhs)
7919 tree wfl, node, rhs;
7920{
7921 tree name, arg_type, arg;
7922
7923 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7924 {
7925 /* At any rate, check whether we're trying to assign a value to
7926 a final. */
7927 tree accessed = (JDECL_P (node) ? node :
7928 (TREE_CODE (node) == COMPONENT_REF ?
7929 TREE_OPERAND (node, 1) : node));
7930 if (check_final_assignment (accessed, wfl))
7931 return error_mark_node;
7932
7933 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7934 arg_type, name, arg, rhs);
7935 return java_complete_tree (node);
7936 }
7937 return NULL_TREE;
7938}
7939
7940/* Construct the expression that calls an access method:
7941 <type>.access$<n>(<arg1> [, <arg2>]);
7942
7943 ARG2 can be NULL and will be omitted in that case. It will denote a
7944 read access. */
7945
7946static tree
7947build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7948 int lc;
7949 tree type, access_method_name, arg1, arg2;
7950{
7951 tree args, cn, access;
7952
7953 args = arg1 ? arg1 :
7954 build_wfl_node (build_current_thisn (current_class));
7955 args = build_tree_list (NULL_TREE, args);
7956
7957 if (arg2)
7958 args = tree_cons (NULL_TREE, arg2, args);
7959
7960 access = build_method_invocation (build_wfl_node (access_method_name), args);
7961 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7962 return make_qualified_primary (cn, access, lc);
7963}
7964
7965static tree
7966build_new_access_id ()
7967{
7968 static int access_n_counter = 1;
7969 char buffer [128];
7970
7971 sprintf (buffer, "access$%d", access_n_counter++);
7972 return get_identifier (buffer);
7973}
7974
7975/* Create the static access functions for the outer field DECL. We define a
7976 read:
7977 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7978 return inst$.field;
7979 }
7980 and a write access:
7981 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7982 TREE_TYPE (<field>) value$) {
7983 return inst$.field = value$;
7984 }
7985 We should have a usage flags on the DECL so we can lazily turn the ones
7986 we're using for code generation. FIXME.
7987*/
7988
7989static tree
7990build_outer_field_access_methods (decl)
7991 tree decl;
7992{
7993 tree id, args, stmt, mdecl;
7994
c7303e41 7995 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
7996 return FIELD_INNER_ACCESS (decl);
7997
c7303e41
APB
7998 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7999
c2952b01
APB
8000 /* Create the identifier and a function named after it. */
8001 id = build_new_access_id ();
8002
8003 /* The identifier is marked as bearing the name of a generated write
8004 access function for outer field accessed from inner classes. */
8005 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8006
8007 /* Create the read access */
8008 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8009 TREE_CHAIN (args) = end_params_node;
8010 stmt = make_qualified_primary (build_wfl_node (inst_id),
8011 build_wfl_node (DECL_NAME (decl)), 0);
8012 stmt = build_return (0, stmt);
8013 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8014 TREE_TYPE (decl), id, args, stmt);
8015 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8016
c7303e41
APB
8017 /* Create the write access method. No write access for final variable */
8018 if (!FIELD_FINAL (decl))
8019 {
8020 args = build_tree_list (inst_id,
8021 build_pointer_type (DECL_CONTEXT (decl)));
8022 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8023 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8024 stmt = make_qualified_primary (build_wfl_node (inst_id),
8025 build_wfl_node (DECL_NAME (decl)), 0);
8026 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8027 build_wfl_node (wpv_id)));
8028 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8029 TREE_TYPE (decl), id,
8030 args, stmt);
8031 }
c2952b01 8032 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8033
8034 /* Return the access name */
8035 return FIELD_INNER_ACCESS (decl) = id;
8036}
8037
8038/* Build an field access method NAME. */
8039
8040static tree
8041build_outer_field_access_method (class, type, name, args, body)
8042 tree class, type, name, args, body;
8043{
8044 tree saved_current_function_decl, mdecl;
8045
8046 /* Create the method */
8047 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8048 fix_method_argument_names (args, mdecl);
8049 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8050
8051 /* Attach the method body. */
8052 saved_current_function_decl = current_function_decl;
8053 start_artificial_method_body (mdecl);
8054 java_method_add_stmt (mdecl, body);
8055 end_artificial_method_body (mdecl);
8056 current_function_decl = saved_current_function_decl;
8057
8058 return mdecl;
8059}
8060
8061\f
8062/* This section deals with building access function necessary for
8063 certain kinds of method invocation from inner classes. */
8064
8065static tree
8066build_outer_method_access_method (decl)
8067 tree decl;
8068{
8069 tree saved_current_function_decl, mdecl;
8070 tree args = NULL_TREE, call_args = NULL_TREE;
8071 tree carg, id, body, class;
8072 char buffer [80];
8073 int parm_id_count = 0;
8074
8075 /* Test this abort with an access to a private field */
8076 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8077 abort ();
8078
8079 /* Check the cache first */
8080 if (DECL_FUNCTION_INNER_ACCESS (decl))
8081 return DECL_FUNCTION_INNER_ACCESS (decl);
8082
8083 class = DECL_CONTEXT (decl);
8084
8085 /* Obtain an access identifier and mark it */
8086 id = build_new_access_id ();
8087 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8088
c2952b01
APB
8089 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8090 /* Create the arguments, as much as the original */
8091 for (; carg && carg != end_params_node;
8092 carg = TREE_CHAIN (carg))
8093 {
8094 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8095 args = chainon (args, build_tree_list (get_identifier (buffer),
8096 TREE_VALUE (carg)));
8097 }
8098 args = chainon (args, end_params_node);
8099
8100 /* Create the method */
8101 mdecl = create_artificial_method (class, ACC_STATIC,
8102 TREE_TYPE (TREE_TYPE (decl)), id, args);
8103 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8104 /* There is a potential bug here. We should be able to use
8105 fix_method_argument_names, but then arg names get mixed up and
8106 eventually a constructor will have its this$0 altered and the
8107 outer context won't be assignment properly. The test case is
8108 stub.java FIXME */
8109 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8110
8111 /* Attach the method body. */
8112 saved_current_function_decl = current_function_decl;
8113 start_artificial_method_body (mdecl);
8114
8115 /* The actual method invocation uses the same args. When invoking a
8116 static methods that way, we don't want to skip the first
8117 argument. */
8118 carg = args;
8119 if (!METHOD_STATIC (decl))
8120 carg = TREE_CHAIN (carg);
8121 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8122 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8123 call_args);
8124
8125 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8126 call_args);
8127 if (!METHOD_STATIC (decl))
8128 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8129 body, 0);
8130 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8131 body = build_return (0, body);
8132 java_method_add_stmt (mdecl,body);
8133 end_artificial_method_body (mdecl);
8134 current_function_decl = saved_current_function_decl;
c2952b01
APB
8135
8136 /* Back tag the access function so it know what it accesses */
8137 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8138
8139 /* Tag the current method so it knows it has an access generated */
8140 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8141}
8142
8143\f
8144/* This section of the code deals with building expressions to access
8145 the enclosing instance of an inner class. The enclosing instance is
8146 kept in a generated field called this$<n>, with <n> being the
8147 inner class nesting level (starting from 0.) */
8148
dba41d30
APB
8149/* Build an access to a given this$<n>, always chaining access call to
8150 others. Access methods to this$<n> are build on the fly if
8151 necessary. This CAN'T be used to solely access this$<n-1> from
8152 this$<n> (which alway yield to special cases and optimization, see
8153 for example build_outer_field_access). */
c2952b01
APB
8154
8155static tree
8156build_access_to_thisn (from, to, lc)
8157 tree from, to;
8158 int lc;
8159{
8160 tree access = NULL_TREE;
8161
8162 while (from != to)
8163 {
c2952b01 8164 if (!access)
dba41d30
APB
8165 {
8166 access = build_current_thisn (from);
8167 access = build_wfl_node (access);
8168 }
8169 else
c2952b01 8170 {
dba41d30
APB
8171 tree access0_wfl, cn;
8172
8173 maybe_build_thisn_access_method (from);
8174 access0_wfl = build_wfl_node (access0_identifier_node);
8175 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8176 EXPR_WFL_LINECOL (access0_wfl) = lc;
8177 access = build_tree_list (NULL_TREE, access);
8178 access = build_method_invocation (access0_wfl, access);
8179 access = make_qualified_primary (cn, access, lc);
c2952b01 8180 }
5e18f6d6
APB
8181
8182 /* if FROM isn't an inter class, that's fine, we've done
8183 enough. What we're looking for can be accessed from there. */
8184 from = DECL_CONTEXT (TYPE_NAME (from));
8185 if (!from)
8186 break;
8187 from = TREE_TYPE (from);
c2952b01
APB
8188 }
8189 return access;
8190}
8191
8192/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8193 is returned if nothing needs to be generated. Otherwise, the method
152de068 8194 generated and a method decl is returned.
c2952b01
APB
8195
8196 NOTE: These generated methods should be declared in a class file
8197 attribute so that they can't be referred to directly. */
8198
8199static tree
8200maybe_build_thisn_access_method (type)
8201 tree type;
8202{
8203 tree mdecl, args, stmt, rtype;
8204 tree saved_current_function_decl;
8205
8206 /* If TYPE is a top-level class, no access method is required.
8207 If there already is such an access method, bail out. */
ee5f86dc 8208 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8209 return NULL_TREE;
8210
8211 /* We generate the method. The method looks like:
8212 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8213 */
c2952b01
APB
8214 args = build_tree_list (inst_id, build_pointer_type (type));
8215 TREE_CHAIN (args) = end_params_node;
8216 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8217 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8218 access0_identifier_node, args);
8219 fix_method_argument_names (args, mdecl);
8220 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8221 stmt = build_current_thisn (type);
8222 stmt = make_qualified_primary (build_wfl_node (inst_id),
8223 build_wfl_node (stmt), 0);
8224 stmt = build_return (0, stmt);
8225
8226 saved_current_function_decl = current_function_decl;
8227 start_artificial_method_body (mdecl);
8228 java_method_add_stmt (mdecl, stmt);
8229 end_artificial_method_body (mdecl);
8230 current_function_decl = saved_current_function_decl;
c2952b01
APB
8231
8232 CLASS_ACCESS0_GENERATED_P (type) = 1;
8233
8234 return mdecl;
8235}
8236
8237/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8238 the first level of innerclassing. this$1 for the next one, etc...
8239 This function can be invoked with TYPE to NULL, available and then
8240 has to count the parser context. */
8241
8242static tree
8243build_current_thisn (type)
8244 tree type;
8245{
8246 static int saved_i = -1;
8247 static tree saved_thisn = NULL_TREE;
19e223db
MM
8248 static tree saved_type = NULL_TREE;
8249 static int saved_type_i = 0;
8250 static int initialized_p;
c2952b01
APB
8251 tree decl;
8252 char buffer [80];
8253 int i = 0;
8254
19e223db
MM
8255 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8256 if (!initialized_p)
c2952b01 8257 {
19e223db
MM
8258 ggc_add_tree_root (&saved_thisn, 1);
8259 ggc_add_tree_root (&saved_type, 1);
8260 initialized_p = 1;
8261 }
c2952b01 8262
19e223db
MM
8263 if (type)
8264 {
c2952b01
APB
8265 if (type == saved_type)
8266 i = saved_type_i;
8267 else
8268 {
8269 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8270 decl; decl = DECL_CONTEXT (decl), i++)
8271 ;
8272
8273 saved_type = type;
8274 saved_type_i = i;
8275 }
8276 }
8277 else
8278 i = list_length (GET_CPC_LIST ())-2;
8279
8280 if (i == saved_i)
8281 return saved_thisn;
8282
8283 sprintf (buffer, "this$%d", i);
8284 saved_i = i;
8285 saved_thisn = get_identifier (buffer);
8286 return saved_thisn;
8287}
8288
8289/* Return the assignement to the hidden enclosing context `this$<n>'
8290 by the second incoming parameter to the innerclass constructor. The
8291 form used is `this.this$<n> = this$<n>;'. */
8292
8293static tree
8294build_thisn_assign ()
8295{
8296 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8297 {
8298 tree thisn = build_current_thisn (current_class);
8299 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8300 build_wfl_node (thisn), 0);
8301 tree rhs = build_wfl_node (thisn);
8302 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8303 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8304 }
8305 return NULL_TREE;
8306}
8307
8308\f
165f37bc
APB
8309/* Building the synthetic `class$' used to implement the `.class' 1.1
8310 extension for non primitive types. This method looks like:
8311
8312 static Class class$(String type) throws NoClassDefFoundError
8313 {
8314 try {return (java.lang.Class.forName (String));}
8315 catch (ClassNotFoundException e) {
8316 throw new NoClassDefFoundError(e.getMessage());}
8317 } */
8318
8319static tree
8320build_dot_class_method (class)
8321 tree class;
8322{
8323#define BWF(S) build_wfl_node (get_identifier ((S)))
8324#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8325 tree args, tmp, saved_current_function_decl, mdecl;
8326 tree stmt, throw_stmt, catch, catch_block, try_block;
8327 tree catch_clause_param;
8328 tree class_not_found_exception, no_class_def_found_error;
8329
8330 static tree get_message_wfl, type_parm_wfl;
8331
8332 if (!get_message_wfl)
8333 {
8334 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8335 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8336 ggc_add_tree_root (&get_message_wfl, 1);
8337 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8338 }
8339
8340 /* Build the arguments */
8341 args = build_tree_list (get_identifier ("type$"),
8342 build_pointer_type (string_type_node));
8343 TREE_CHAIN (args) = end_params_node;
8344
8345 /* Build the qualified name java.lang.Class.forName */
8346 tmp = MQN (MQN (MQN (BWF ("java"),
8347 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8348
8349 /* For things we have to catch and throw */
8350 class_not_found_exception =
8351 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8352 no_class_def_found_error =
8353 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8354 load_class (class_not_found_exception, 1);
8355 load_class (no_class_def_found_error, 1);
8356
8357 /* Create the "class$" function */
8358 mdecl = create_artificial_method (class, ACC_STATIC,
8359 build_pointer_type (class_type_node),
94807d33 8360 classdollar_identifier_node, args);
165f37bc
APB
8361 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8362 no_class_def_found_error);
8363
8364 /* We start by building the try block. We need to build:
8365 return (java.lang.Class.forName (type)); */
8366 stmt = build_method_invocation (tmp,
8367 build_tree_list (NULL_TREE, type_parm_wfl));
8368 stmt = build_return (0, stmt);
8369 /* Put it in a block. That's the try block */
8370 try_block = build_expr_block (stmt, NULL_TREE);
8371
8372 /* Now onto the catch block. We start by building the expression
8373 throwing a new exception:
8374 throw new NoClassDefFoundError (_.getMessage); */
8375 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8376 get_message_wfl, 0);
8377 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8378
8379 /* Build new NoClassDefFoundError (_.getMessage) */
8380 throw_stmt = build_new_invocation
8381 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8382 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8383
8384 /* Build the throw, (it's too early to use BUILD_THROW) */
8385 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8386
8387 /* Build the catch block to encapsulate all this. We begin by
8388 building an decl for the catch clause parameter and link it to
8389 newly created block, the catch block. */
8390 catch_clause_param =
8391 build_decl (VAR_DECL, wpv_id,
8392 build_pointer_type (class_not_found_exception));
8393 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8394
8395 /* We initialize the variable with the exception handler. */
8396 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8397 soft_exceptioninfo_call_node);
8398 add_stmt_to_block (catch_block, NULL_TREE, catch);
8399
8400 /* We add the statement throwing the new exception */
8401 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8402
8403 /* Build a catch expression for all this */
8404 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8405
8406 /* Build the try/catch sequence */
8407 stmt = build_try_statement (0, try_block, catch_block);
8408
8409 fix_method_argument_names (args, mdecl);
8410 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8411 saved_current_function_decl = current_function_decl;
8412 start_artificial_method_body (mdecl);
8413 java_method_add_stmt (mdecl, stmt);
8414 end_artificial_method_body (mdecl);
8415 current_function_decl = saved_current_function_decl;
8416 TYPE_DOT_CLASS (class) = mdecl;
8417
8418 return mdecl;
8419}
8420
8421static tree
f0f3a777
APB
8422build_dot_class_method_invocation (type)
8423 tree type;
165f37bc 8424{
f0f3a777
APB
8425 tree sig_id, s;
8426
8427 if (TYPE_ARRAY_P (type))
8428 sig_id = build_java_signature (type);
8429 else
8430 sig_id = DECL_NAME (TYPE_NAME (type));
8431
1f8f4a0b
MM
8432 s = build_string (IDENTIFIER_LENGTH (sig_id),
8433 IDENTIFIER_POINTER (sig_id));
94807d33 8434 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
165f37bc
APB
8435 build_tree_list (NULL_TREE, s));
8436}
8437
c2952b01
APB
8438/* This section of the code deals with constructor. */
8439
22eed1e6
APB
8440/* Craft a body for default constructor. Patch existing constructor
8441 bodies with call to super() and field initialization statements if
8442 necessary. */
8443
8444static void
8445fix_constructors (mdecl)
8446 tree mdecl;
8447{
8448 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8449 tree thisn_assign, compound = NULL_TREE;
8450 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8451
22eed1e6
APB
8452 if (!body)
8453 {
22eed1e6
APB
8454 /* It is an error for the compiler to generate a default
8455 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8456 takes no argument, or the same args for an anonymous class */
8457 if (verify_constructor_super (mdecl))
22eed1e6 8458 {
c2952b01
APB
8459 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8460 tree save = DECL_NAME (mdecl);
49f48c71 8461 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8462 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8463 parse_error_context
c2952b01
APB
8464 (lookup_cl (TYPE_NAME (class_type)),
8465 "No constructor matching `%s' found in class `%s'",
8466 lang_printable_name (mdecl, 0), n);
8467 DECL_NAME (mdecl) = save;
22eed1e6
APB
8468 }
8469
c2952b01
APB
8470 /* The constructor body must be crafted by hand. It's the
8471 constructor we defined when we realize we didn't have the
8472 CLASSNAME() constructor */
22eed1e6
APB
8473 start_artificial_method_body (mdecl);
8474
f0f3a777
APB
8475 /* Insert an assignment to the this$<n> hidden field, if
8476 necessary */
8477 if ((thisn_assign = build_thisn_assign ()))
8478 java_method_add_stmt (mdecl, thisn_assign);
8479
22eed1e6
APB
8480 /* We don't generate a super constructor invocation if we're
8481 compiling java.lang.Object. build_super_invocation takes care
8482 of that. */
e920ebc9 8483 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8484
c2952b01
APB
8485 /* Insert the instance initializer block right here, after the
8486 super invocation. */
8487 add_instance_initializer (mdecl);
8488
22eed1e6
APB
8489 end_artificial_method_body (mdecl);
8490 }
8491 /* Search for an explicit constructor invocation */
8492 else
8493 {
8494 int found = 0;
8495 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8496
8497 while (body)
8498 switch (TREE_CODE (body))
8499 {
8500 case CALL_EXPR:
8501 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8502 body = NULL_TREE;
8503 break;
8504 case COMPOUND_EXPR:
8505 case EXPR_WITH_FILE_LOCATION:
8506 body = TREE_OPERAND (body, 0);
8507 break;
8508 case BLOCK:
8509 body = BLOCK_EXPR_BODY (body);
8510 break;
8511 default:
8512 found = 0;
8513 body = NULL_TREE;
8514 }
8515 /* The constructor is missing an invocation of super() */
8516 if (!found)
8517 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8518 build_super_invocation (mdecl));
22eed1e6 8519
c2952b01
APB
8520 /* Generate the assignment to this$<n>, if necessary */
8521 if ((thisn_assign = build_thisn_assign ()))
8522 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8523
f0f3a777
APB
8524 /* Insert the instance initializer block right here, after the
8525 super invocation. */
8526 add_instance_initializer (mdecl);
8527
22eed1e6
APB
8528 /* Fix the constructor main block if we're adding extra stmts */
8529 if (compound)
8530 {
8531 compound = add_stmt_to_compound (compound, NULL_TREE,
8532 BLOCK_EXPR_BODY (main_block));
8533 BLOCK_EXPR_BODY (main_block) = compound;
8534 }
8535 }
8536}
8537
8538/* Browse constructors in the super class, searching for a constructor
8539 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8540 otherwise. If the current class is an anonymous inner class, look
8541 for something that has the same signature. */
22eed1e6
APB
8542
8543static int
c2952b01
APB
8544verify_constructor_super (mdecl)
8545 tree mdecl;
22eed1e6
APB
8546{
8547 tree class = CLASSTYPE_SUPER (current_class);
152de068 8548 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8549 tree sdecl;
8550
22eed1e6
APB
8551 if (!class)
8552 return 0;
8553
c2952b01 8554 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8555 {
c2952b01
APB
8556 tree mdecl_arg_type;
8557 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8558 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8559 if (DECL_CONSTRUCTOR_P (sdecl))
8560 {
cf1b2274 8561 tree m_arg_type;
152de068
APB
8562 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8563 if (super_inner)
8564 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8565 for (m_arg_type = mdecl_arg_type;
8566 (arg_type != end_params_node
8567 && m_arg_type != end_params_node);
c2952b01 8568 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8569 m_arg_type = TREE_CHAIN (m_arg_type))
8570 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8571 break;
8572
cf1b2274 8573 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8574 return 0;
8575 }
8576 }
8577 else
8578 {
8579 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8580 {
152de068
APB
8581 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8582 if (super_inner)
8583 arg = TREE_CHAIN (arg);
8584 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8585 return 0;
8586 }
8587 }
8588 return 1;
8589}
8590
22eed1e6 8591/* Generate code for all context remembered for code generation. */
b351b287
APB
8592
8593void
8594java_expand_classes ()
8595{
5423609c 8596 int save_error_count = 0;
c2952b01
APB
8597 static struct parser_ctxt *saved_ctxp = NULL;
8598
23a79c61
APB
8599 java_parse_abort_on_error ();
8600 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8601 return;
8602 java_layout_classes ();
8603 java_parse_abort_on_error ();
8604
c2952b01 8605 saved_ctxp = ctxp_for_generation;
b351b287
APB
8606 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8607 {
8608 ctxp = ctxp_for_generation;
3a2e5926 8609 input_filename = ctxp->filename;
b351b287 8610 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8611 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8612 java_parse_abort_on_error ();
8613 }
3a2e5926 8614 input_filename = main_input_filename;
c2952b01
APB
8615
8616 /* Find anonymous classes and expand their constructor, now they
8617 have been fixed. */
8618 for (ctxp_for_generation = saved_ctxp;
8619 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8620 {
8621 tree current;
8622 ctxp = ctxp_for_generation;
8623 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8624 {
8625 current_class = TREE_TYPE (current);
8626 if (ANONYMOUS_CLASS_P (current_class))
8627 {
8628 tree d;
8629 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8630 {
8631 if (DECL_CONSTRUCTOR_P (d))
8632 {
8633 restore_line_number_status (1);
c2952b01
APB
8634 java_complete_expand_method (d);
8635 restore_line_number_status (0);
8636 break; /* We now there are no other ones */
8637 }
8638 }
8639 }
8640 }
8641 }
8642
8643 /* If we've found error at that stage, don't try to generate
8644 anything, unless we're emitting xrefs or checking the syntax only
8645 (but not using -fsyntax-only for the purpose of generating
8646 bytecode. */
8647 if (java_error_count && !flag_emit_xref
8648 && (!flag_syntax_only && !flag_emit_class_files))
8649 return;
8650
8651 /* Now things are stable, go for generation of the class data. */
8652 for (ctxp_for_generation = saved_ctxp;
8653 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8654 {
8655 tree current;
8656 ctxp = ctxp_for_generation;
8657 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8658 {
8659 current_class = TREE_TYPE (current);
8660 outgoing_cpool = TYPE_CPOOL (current_class);
8661 if (flag_emit_class_files)
8662 write_classfile (current_class);
8663 if (flag_emit_xref)
8664 expand_xref (current_class);
8665 else if (! flag_syntax_only)
8666 finish_class ();
8667 }
8668 }
b351b287
APB
8669}
8670
e04a16fb
AG
8671/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8672 a tree list node containing RIGHT. Fore coming RIGHTs will be
8673 chained to this hook. LOCATION contains the location of the
8674 separating `.' operator. */
8675
8676static tree
8677make_qualified_primary (primary, right, location)
8678 tree primary, right;
8679 int location;
8680{
8681 tree wfl;
8682
c2952b01 8683 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8684 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8685 else
8686 {
8687 wfl = primary;
c2952b01
APB
8688 /* If wfl wasn't qualified, we build a first anchor */
8689 if (!EXPR_WFL_QUALIFICATION (wfl))
8690 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8691 }
8692
c2952b01 8693 /* And chain them */
e04a16fb
AG
8694 EXPR_WFL_LINECOL (right) = location;
8695 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8696 PRIMARY_P (wfl) = 1;
8697 return wfl;
8698}
8699
8700/* Simple merge of two name separated by a `.' */
8701
8702static tree
8703merge_qualified_name (left, right)
8704 tree left, right;
8705{
8706 tree node;
c2952b01
APB
8707 if (!left && !right)
8708 return NULL_TREE;
8709
8710 if (!left)
8711 return right;
8712
8713 if (!right)
8714 return left;
8715
e04a16fb
AG
8716 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8717 IDENTIFIER_LENGTH (left));
8718 obstack_1grow (&temporary_obstack, '.');
8719 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8720 IDENTIFIER_LENGTH (right));
8721 node = get_identifier (obstack_base (&temporary_obstack));
8722 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8723 QUALIFIED_P (node) = 1;
8724 return node;
8725}
8726
8727/* Merge the two parts of a qualified name into LEFT. Set the
8728 location information of the resulting node to LOCATION, usually
8729 inherited from the location information of the `.' operator. */
8730
8731static tree
8732make_qualified_name (left, right, location)
8733 tree left, right;
8734 int location;
8735{
bc3ca41b
PB
8736#ifdef USE_COMPONENT_REF
8737 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8738 EXPR_WFL_LINECOL (node) = location;
8739 return node;
8740#else
e04a16fb
AG
8741 tree left_id = EXPR_WFL_NODE (left);
8742 tree right_id = EXPR_WFL_NODE (right);
8743 tree wfl, merge;
8744
8745 merge = merge_qualified_name (left_id, right_id);
8746
8747 /* Left wasn't qualified and is now qualified */
8748 if (!QUALIFIED_P (left_id))
8749 {
8750 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8751 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8752 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8753 }
8754
8755 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8756 EXPR_WFL_LINECOL (wfl) = location;
8757 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8758
8759 EXPR_WFL_NODE (left) = merge;
8760 return left;
bc3ca41b 8761#endif
e04a16fb
AG
8762}
8763
8764/* Extract the last identifier component of the qualified in WFL. The
8765 last identifier is removed from the linked list */
8766
8767static tree
8768cut_identifier_in_qualified (wfl)
8769 tree wfl;
8770{
8771 tree q;
8772 tree previous = NULL_TREE;
8773 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8774 if (!TREE_CHAIN (q))
8775 {
8776 if (!previous)
400500c4
RK
8777 /* Operating on a non qualified qualified WFL. */
8778 abort ();
8779
e04a16fb
AG
8780 TREE_CHAIN (previous) = NULL_TREE;
8781 return TREE_PURPOSE (q);
8782 }
8783}
8784
8785/* Resolve the expression name NAME. Return its decl. */
8786
8787static tree
5e942c50 8788resolve_expression_name (id, orig)
e04a16fb 8789 tree id;
5e942c50 8790 tree *orig;
e04a16fb
AG
8791{
8792 tree name = EXPR_WFL_NODE (id);
8793 tree decl;
8794
8795 /* 6.5.5.1: Simple expression names */
8796 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8797 {
8798 /* 15.13.1: NAME can appear within the scope of a local variable
8799 declaration */
8800 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8801 return decl;
8802
8803 /* 15.13.1: NAME can appear within a class declaration */
8804 else
8805 {
8806 decl = lookup_field_wrapper (current_class, name);
8807 if (decl)
8808 {
c2952b01 8809 tree access = NULL_TREE;
e04a16fb 8810 int fs = FIELD_STATIC (decl);
f2760b27
APB
8811
8812 /* If we're accessing an outer scope local alias, make
8813 sure we change the name of the field we're going to
8814 build access to. */
8815 if (FIELD_LOCAL_ALIAS_USED (decl))
8816 name = DECL_NAME (decl);
8817
e04a16fb
AG
8818 /* Instance variable (8.3.1.1) can't appear within
8819 static method, static initializer or initializer for
8820 a static variable. */
8821 if (!fs && METHOD_STATIC (current_function_decl))
8822 {
7f10c2e2 8823 static_ref_err (id, name, current_class);
e04a16fb
AG
8824 return error_mark_node;
8825 }
22eed1e6
APB
8826 /* Instance variables can't appear as an argument of
8827 an explicit constructor invocation */
7e1376a1
BM
8828 if (!fs && ctxp->explicit_constructor_p
8829 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
22eed1e6
APB
8830 {
8831 parse_error_context
781b0558 8832 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8833 return error_mark_node;
8834 }
5e942c50 8835
c2952b01
APB
8836 /* If we're processing an inner class and we're trying
8837 to access a field belonging to an outer class, build
8838 the access to the field */
8839 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8840 {
8841 if (CLASS_STATIC (TYPE_NAME (current_class)))
8842 {
8843 static_ref_err (id, DECL_NAME (decl), current_class);
8844 return error_mark_node;
8845 }
8846 return build_outer_field_access (id, decl);
8847 }
c2952b01 8848
5e942c50 8849 /* Otherwise build what it takes to access the field */
c2952b01
APB
8850 access = build_field_ref ((fs ? NULL_TREE : current_this),
8851 DECL_CONTEXT (decl), name);
e8fc7396 8852 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8853 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8854 /* We may be asked to save the real field access node */
8855 if (orig)
c2952b01 8856 *orig = access;
5e942c50 8857 /* And we return what we got */
c2952b01 8858 return access;
e04a16fb
AG
8859 }
8860 /* Fall down to error report on undefined variable */
8861 }
8862 }
8863 /* 6.5.5.2 Qualified Expression Names */
8864 else
8865 {
5e942c50
APB
8866 if (orig)
8867 *orig = NULL_TREE;
e04a16fb
AG
8868 qualify_ambiguous_name (id);
8869 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8870 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8871 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8872 }
8873
8874 /* We've got an error here */
8875 parse_error_context (id, "Undefined variable `%s'",
8876 IDENTIFIER_POINTER (name));
8877
8878 return error_mark_node;
8879}
8880
7f10c2e2
APB
8881static void
8882static_ref_err (wfl, field_id, class_type)
8883 tree wfl, field_id, class_type;
8884{
8885 parse_error_context
8886 (wfl,
8887 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8888 IDENTIFIER_POINTER (field_id),
8889 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8890}
8891
e04a16fb
AG
8892/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8893 We return something suitable to generate the field access. We also
8894 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8895 recipient's address can be null. */
8896
8897static tree
8898resolve_field_access (qual_wfl, field_decl, field_type)
8899 tree qual_wfl;
8900 tree *field_decl, *field_type;
8901{
8902 int is_static = 0;
8903 tree field_ref;
8904 tree decl, where_found, type_found;
8905
8906 if (resolve_qualified_expression_name (qual_wfl, &decl,
8907 &where_found, &type_found))
8908 return error_mark_node;
8909
8910 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8911 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8912 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8913 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8914 {
8915 tree length = build_java_array_length_access (where_found);
8916 field_ref =
8917 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8918
8919 /* In case we're dealing with a static array, we need to
8920 initialize its class before the array length can be fetched.
8921 It's also a good time to create a DECL_RTL for the field if
8922 none already exists, otherwise if the field was declared in a
8923 class found in an external file and hasn't been (and won't
8924 be) accessed for its value, none will be created. */
8925 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8926 {
8927 build_static_field_ref (where_found);
8928 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8929 }
e04a16fb
AG
8930 }
8931 /* We might have been trying to resolve field.method(). In which
8932 case, the resolution is over and decl is the answer */
34f4db93 8933 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8934 field_ref = decl;
34f4db93 8935 else if (JDECL_P (decl))
e04a16fb 8936 {
5e942c50
APB
8937 int static_final_found = 0;
8938 if (!type_found)
8939 type_found = DECL_CONTEXT (decl);
34f4db93 8940 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 8941 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 8942 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8943 && DECL_INITIAL (decl))
5e942c50 8944 {
0c2b8145
APB
8945 /* When called on a FIELD_DECL of the right (primitive)
8946 type, java_complete_tree will try to substitue the decl
8947 for it's initial value. */
70541f45 8948 field_ref = java_complete_tree (decl);
5e942c50
APB
8949 static_final_found = 1;
8950 }
8951 else
7f10c2e2
APB
8952 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8953 NULL_TREE : where_found),
5e942c50 8954 type_found, DECL_NAME (decl));
e04a16fb
AG
8955 if (field_ref == error_mark_node)
8956 return error_mark_node;
e8fc7396
APB
8957 if (is_static && !static_final_found
8958 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8959 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8960 }
8961 else
8962 field_ref = decl;
8963
8964 if (field_decl)
8965 *field_decl = decl;
8966 if (field_type)
c877974e
APB
8967 *field_type = (QUAL_DECL_TYPE (decl) ?
8968 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8969 return field_ref;
8970}
8971
e28cd97b
APB
8972/* If NODE is an access to f static field, strip out the class
8973 initialization part and return the field decl, otherwise, return
8974 NODE. */
8975
8976static tree
8977strip_out_static_field_access_decl (node)
8978 tree node;
8979{
8980 if (TREE_CODE (node) == COMPOUND_EXPR)
8981 {
8982 tree op1 = TREE_OPERAND (node, 1);
8983 if (TREE_CODE (op1) == COMPOUND_EXPR)
8984 {
8985 tree call = TREE_OPERAND (op1, 0);
8986 if (TREE_CODE (call) == CALL_EXPR
8987 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8988 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8989 == soft_initclass_node)
8990 return TREE_OPERAND (op1, 1);
8991 }
2f11d407
TT
8992 else if (JDECL_P (op1))
8993 return op1;
e28cd97b
APB
8994 }
8995 return node;
8996}
8997
e04a16fb
AG
8998/* 6.5.5.2: Qualified Expression Names */
8999
9000static int
9001resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9002 tree wfl;
9003 tree *found_decl, *type_found, *where_found;
9004{
9005 int from_type = 0; /* Field search initiated from a type */
c2952b01 9006 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9007 int previous_call_static = 0;
9008 int is_static;
9009 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9010 /* For certain for of inner class instantiation */
9011 tree saved_current, saved_this;
9012#define RESTORE_THIS_AND_CURRENT_CLASS \
9013 { current_class = saved_current; current_this = saved_this;}
9014
c877974e 9015 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9016
9017 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9018 {
9019 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9020 tree ret_decl; /* for EH checking */
9021 int location; /* for EH checking */
e04a16fb
AG
9022
9023 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9024 switch (TREE_CODE (qual_wfl))
9025 {
9026 case CALL_EXPR:
b67d701b 9027 case NEW_CLASS_EXPR:
e04a16fb
AG
9028 /* If the access to the function call is a non static field,
9029 build the code to access it. */
34f4db93 9030 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9031 {
ac825856
APB
9032 decl = maybe_access_field (decl, *where_found,
9033 DECL_CONTEXT (decl));
e04a16fb
AG
9034 if (decl == error_mark_node)
9035 return 1;
9036 }
c2952b01 9037
e04a16fb
AG
9038 /* And code for the function call */
9039 if (complete_function_arguments (qual_wfl))
9040 return 1;
c2952b01
APB
9041
9042 /* We might have to setup a new current class and a new this
9043 for the search of an inner class, relative to the type of
9044 a expression resolved as `decl'. The current values are
9045 saved and restored shortly after */
9046 saved_current = current_class;
9047 saved_this = current_this;
dba41d30
APB
9048 if (decl
9049 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9050 || from_qualified_this))
c2952b01 9051 {
dba41d30
APB
9052 /* If we still have `from_qualified_this', we have the form
9053 <T>.this.f() and we need to build <T>.this */
9054 if (from_qualified_this)
9055 {
9056 decl = build_access_to_thisn (current_class, type, 0);
9057 decl = java_complete_tree (decl);
9058 type = TREE_TYPE (TREE_TYPE (decl));
9059 }
c2952b01
APB
9060 current_class = type;
9061 current_this = decl;
dba41d30 9062 from_qualified_this = 0;
c2952b01
APB
9063 }
9064
89e09b9a
PB
9065 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9066 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9067 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9068 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
e101152f
APB
9069 *where_found = patch_method_invocation (qual_wfl, decl, type,
9070 from_super,
7705e9db 9071 &is_static, &ret_decl);
e04a16fb 9072 if (*where_found == error_mark_node)
c2952b01
APB
9073 {
9074 RESTORE_THIS_AND_CURRENT_CLASS;
9075 return 1;
9076 }
e04a16fb
AG
9077 *type_found = type = QUAL_DECL_TYPE (*where_found);
9078
c2952b01
APB
9079 /* If we're creating an inner class instance, check for that
9080 an enclosing instance is in scope */
9081 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9082 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9083 {
9084 parse_error_context
165f37bc
APB
9085 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9086 lang_printable_name (type, 0),
9087 (!current_this ? "" :
9088 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9089 RESTORE_THIS_AND_CURRENT_CLASS;
9090 return 1;
9091 }
9092
9093 /* In case we had to change then to resolve a inner class
9094 instantiation using a primary qualified by a `new' */
9095 RESTORE_THIS_AND_CURRENT_CLASS;
9096
34d4df06
APB
9097 /* EH check. No check on access$<n> functions */
9098 if (location
9099 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9100 (DECL_NAME (current_function_decl)))
7705e9db
APB
9101 check_thrown_exceptions (location, ret_decl);
9102
e04a16fb
AG
9103 /* If the previous call was static and this one is too,
9104 build a compound expression to hold the two (because in
9105 that case, previous function calls aren't transported as
9106 forcoming function's argument. */
9107 if (previous_call_static && is_static)
9108 {
9109 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9110 TREE_SIDE_EFFECTS (decl) = 1;
9111 }
9112 else
9113 {
9114 previous_call_static = is_static;
9115 decl = *where_found;
9116 }
c2952b01 9117 from_type = 0;
e04a16fb
AG
9118 continue;
9119
d8fccff5 9120 case NEW_ARRAY_EXPR:
c2952b01 9121 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9122 *where_found = decl = java_complete_tree (qual_wfl);
9123 if (decl == error_mark_node)
9124 return 1;
9125 *type_found = type = QUAL_DECL_TYPE (decl);
9126 CLASS_LOADED_P (type) = 1;
9127 continue;
9128
e04a16fb
AG
9129 case CONVERT_EXPR:
9130 *where_found = decl = java_complete_tree (qual_wfl);
9131 if (decl == error_mark_node)
9132 return 1;
9133 *type_found = type = QUAL_DECL_TYPE (decl);
9134 from_cast = 1;
9135 continue;
9136
22eed1e6 9137 case CONDITIONAL_EXPR:
5e942c50 9138 case STRING_CST:
ac22f9cb 9139 case MODIFY_EXPR:
22eed1e6
APB
9140 *where_found = decl = java_complete_tree (qual_wfl);
9141 if (decl == error_mark_node)
9142 return 1;
9143 *type_found = type = QUAL_DECL_TYPE (decl);
9144 continue;
9145
e04a16fb
AG
9146 case ARRAY_REF:
9147 /* If the access to the function call is a non static field,
9148 build the code to access it. */
34f4db93 9149 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9150 {
9151 decl = maybe_access_field (decl, *where_found, type);
9152 if (decl == error_mark_node)
9153 return 1;
9154 }
9155 /* And code for the array reference expression */
9156 decl = java_complete_tree (qual_wfl);
9157 if (decl == error_mark_node)
9158 return 1;
9159 type = QUAL_DECL_TYPE (decl);
9160 continue;
0a2138e2 9161
37feda7d
APB
9162 case PLUS_EXPR:
9163 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9164 return 1;
9165 if ((type = patch_string (decl)))
9166 decl = type;
9167 *where_found = QUAL_RESOLUTION (q) = decl;
9168 *type_found = type = TREE_TYPE (decl);
9169 break;
9170
165f37bc
APB
9171 case CLASS_LITERAL:
9172 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9173 return 1;
9174 *where_found = QUAL_RESOLUTION (q) = decl;
9175 *type_found = type = TREE_TYPE (decl);
9176 break;
9177
0a2138e2
APB
9178 default:
9179 /* Fix for -Wall Just go to the next statement. Don't
9180 continue */
a3f406ce 9181 break;
e04a16fb
AG
9182 }
9183
9184 /* If we fall here, we weren't processing a (static) function call. */
9185 previous_call_static = 0;
9186
9187 /* It can be the keyword THIS */
9188 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9189 {
9190 if (!current_this)
9191 {
9192 parse_error_context
9193 (wfl, "Keyword `this' used outside allowed context");
9194 return 1;
9195 }
7e1376a1
BM
9196 if (ctxp->explicit_constructor_p
9197 && type == current_class)
f63991a8 9198 {
781b0558 9199 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9200 return 1;
9201 }
e04a16fb 9202 /* We have to generate code for intermediate acess */
c2952b01
APB
9203 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9204 {
9205 *where_found = decl = current_this;
9206 *type_found = type = QUAL_DECL_TYPE (decl);
9207 }
4dbf4496
APB
9208 /* We're trying to access the this from somewhere else. Make sure
9209 it's allowed before doing so. */
c2952b01
APB
9210 else
9211 {
4dbf4496
APB
9212 if (!enclosing_context_p (type, current_class))
9213 {
9214 char *p = xstrdup (lang_printable_name (type, 0));
9215 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9216 p, p,
9217 lang_printable_name (current_class, 0));
9218 free (p);
9219 return 1;
9220 }
c2952b01 9221 from_qualified_this = 1;
dba41d30
APB
9222 /* If there's nothing else after that, we need to
9223 produce something now, otherwise, the section of the
9224 code that needs to produce <T>.this will generate
9225 what is necessary. */
9226 if (!TREE_CHAIN (q))
9227 {
9228 decl = build_access_to_thisn (current_class, type, 0);
9229 *where_found = decl = java_complete_tree (decl);
9230 *type_found = type = TREE_TYPE (decl);
9231 }
c2952b01
APB
9232 }
9233
9234 from_type = 0;
e04a16fb
AG
9235 continue;
9236 }
9237
9238 /* 15.10.2 Accessing Superclass Members using SUPER */
9239 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9240 {
9241 tree node;
9242 /* Check on the restricted use of SUPER */
9243 if (METHOD_STATIC (current_function_decl)
9244 || current_class == object_type_node)
9245 {
9246 parse_error_context
9247 (wfl, "Keyword `super' used outside allowed context");
9248 return 1;
9249 }
9250 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9251 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9252 CLASSTYPE_SUPER (current_class),
9253 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9254 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9255 if (decl == error_mark_node)
9256 return 1;
e04a16fb
AG
9257 *type_found = type = QUAL_DECL_TYPE (decl);
9258 from_super = from_type = 1;
9259 continue;
9260 }
9261
9262 /* 15.13.1: Can't search for field name in packages, so we
9263 assume a variable/class name was meant. */
9264 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9265 {
5e942c50
APB
9266 tree name = resolve_package (wfl, &q);
9267 if (name)
9268 {
c2952b01 9269 tree list;
5e942c50 9270 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9271 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9272 out. We're going to search something inside it. */
9273 *type_found = type = TREE_TYPE (decl);
9274 layout_class (type);
9275 from_type = 1;
c2952b01 9276
dde1da72
APB
9277 /* Fix them all the way down, if any are left. */
9278 if (q)
c2952b01 9279 {
dde1da72
APB
9280 list = TREE_CHAIN (q);
9281 while (list)
9282 {
9283 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9284 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9285 list = TREE_CHAIN (list);
9286 }
c2952b01 9287 }
5e942c50 9288 }
e04a16fb 9289 else
5e942c50
APB
9290 {
9291 if (from_super || from_cast)
9292 parse_error_context
9293 ((from_cast ? qual_wfl : wfl),
9294 "No variable `%s' defined in class `%s'",
9295 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9296 lang_printable_name (type, 0));
9297 else
9298 parse_error_context
9299 (qual_wfl, "Undefined variable or class name: `%s'",
9300 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9301 return 1;
9302 }
e04a16fb
AG
9303 }
9304
9305 /* We have a type name. It's been already resolved when the
9306 expression was qualified. */
9307 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9308 {
9309 if (!(decl = QUAL_RESOLUTION (q)))
9310 return 1; /* Error reported already */
9311
c2952b01
APB
9312 /* Sneak preview. If next we see a `new', we're facing a
9313 qualification with resulted in a type being selected
9314 instead of a field. Report the error */
9315 if(TREE_CHAIN (q)
9316 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9317 {
9318 parse_error_context (qual_wfl, "Undefined variable `%s'",
9319 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9320 return 1;
9321 }
9322
e101152f 9323 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
e04a16fb
AG
9324 {
9325 parse_error_context
9326 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9327 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9328 GET_TYPE_NAME (type),
e04a16fb
AG
9329 IDENTIFIER_POINTER (DECL_NAME (decl)),
9330 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9331 return 1;
9332 }
5e942c50 9333 check_deprecation (qual_wfl, decl);
c2952b01 9334
e04a16fb
AG
9335 type = TREE_TYPE (decl);
9336 from_type = 1;
9337 }
9338 /* We resolve and expression name */
9339 else
9340 {
cd531a2e 9341 tree field_decl = NULL_TREE;
e04a16fb
AG
9342
9343 /* If there exists an early resolution, use it. That occurs
9344 only once and we know that there are more things to
9345 come. Don't do that when processing something after SUPER
9346 (we need more thing to be put in place below */
9347 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9348 {
9349 decl = QUAL_RESOLUTION (q);
c877974e 9350 if (!type)
5e942c50 9351 {
7f10c2e2
APB
9352 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9353 {
9354 if (current_this)
9355 *where_found = current_this;
9356 else
9357 {
9358 static_ref_err (qual_wfl, DECL_NAME (decl),
9359 current_class);
9360 return 1;
9361 }
f0f3a777
APB
9362 if (outer_field_access_p (current_class, decl))
9363 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9364 }
c877974e
APB
9365 else
9366 {
9367 *where_found = TREE_TYPE (decl);
9368 if (TREE_CODE (*where_found) == POINTER_TYPE)
9369 *where_found = TREE_TYPE (*where_found);
9370 }
5e942c50 9371 }
b67d701b 9372 }
e04a16fb
AG
9373
9374 /* We have to search for a field, knowing the type of its
9375 container. The flag FROM_TYPE indicates that we resolved
9376 the last member of the expression as a type name, which
5e942c50
APB
9377 means that for the resolution of this field, we'll look
9378 for other errors than if it was resolved as a member of
9379 an other field. */
e04a16fb
AG
9380 else
9381 {
9382 int is_static;
5e942c50
APB
9383 tree field_decl_type; /* For layout */
9384
e04a16fb
AG
9385 if (!from_type && !JREFERENCE_TYPE_P (type))
9386 {
9387 parse_error_context
9388 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9389 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9390 lang_printable_name (type, 0),
e04a16fb
AG
9391 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9392 return 1;
9393 }
9394
dc0b3eff
PB
9395 field_decl = lookup_field_wrapper (type,
9396 EXPR_WFL_NODE (qual_wfl));
4dbf4496 9397
863cd85a
APB
9398 /* Maybe what we're trying to access to is an inner
9399 class, only if decl is a TYPE_DECL. */
9400 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
4dbf4496
APB
9401 {
9402 tree ptr, inner_decl;
9403
9404 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9405 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9406 if (inner_decl)
9407 {
9408 check_inner_class_access (inner_decl, decl, qual_wfl);
9409 type = TREE_TYPE (inner_decl);
9410 decl = inner_decl;
9411 from_type = 1;
9412 continue;
9413 }
9414 }
9415
dc0b3eff 9416 if (field_decl == NULL_TREE)
e04a16fb
AG
9417 {
9418 parse_error_context
2aa11e97 9419 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9420 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9421 GET_TYPE_NAME (type));
e04a16fb
AG
9422 return 1;
9423 }
dc0b3eff
PB
9424 if (field_decl == error_mark_node)
9425 return 1;
5e942c50
APB
9426
9427 /* Layout the type of field_decl, since we may need
c877974e
APB
9428 it. Don't do primitive types or loaded classes. The
9429 situation of non primitive arrays may not handled
9430 properly here. FIXME */
5e942c50
APB
9431 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9432 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9433 else
9434 field_decl_type = TREE_TYPE (field_decl);
9435 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9436 && !CLASS_LOADED_P (field_decl_type)
9437 && !TYPE_ARRAY_P (field_decl_type))
9438 resolve_and_layout (field_decl_type, NULL_TREE);
9439 if (TYPE_ARRAY_P (field_decl_type))
9440 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9441
9442 /* Check on accessibility here */
e101152f
APB
9443 if (not_accessible_p (current_class, field_decl,
9444 TREE_TYPE (decl), from_super))
e04a16fb
AG
9445 {
9446 parse_error_context
9447 (qual_wfl,
9448 "Can't access %s field `%s.%s' from `%s'",
9449 java_accstring_lookup
9450 (get_access_flags_from_decl (field_decl)),
2aa11e97 9451 GET_TYPE_NAME (type),
e04a16fb
AG
9452 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9453 IDENTIFIER_POINTER
9454 (DECL_NAME (TYPE_NAME (current_class))));
9455 return 1;
9456 }
5e942c50 9457 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9458
9459 /* There are things to check when fields are accessed
9460 from type. There are no restrictions on a static
9461 declaration of the field when it is accessed from an
9462 interface */
9463 is_static = FIELD_STATIC (field_decl);
9464 if (!from_super && from_type
c2952b01
APB
9465 && !TYPE_INTERFACE_P (type)
9466 && !is_static
9467 && (current_function_decl
9468 && METHOD_STATIC (current_function_decl)))
e04a16fb 9469 {
7f10c2e2 9470 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9471 return 1;
9472 }
9473 from_cast = from_super = 0;
9474
c2952b01
APB
9475 /* It's an access from a type but it isn't static, we
9476 make it relative to `this'. */
9477 if (!is_static && from_type)
9478 decl = current_this;
9479
5e942c50
APB
9480 /* If we need to generate something to get a proper
9481 handle on what this field is accessed from, do it
9482 now. */
e04a16fb
AG
9483 if (!is_static)
9484 {
c583dd46 9485 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9486 if (decl == error_mark_node)
9487 return 1;
9488 }
9489
9490 /* We want to keep the location were found it, and the type
9491 we found. */
9492 *where_found = decl;
9493 *type_found = type;
9494
c2952b01
APB
9495 /* Generate the correct expression for field access from
9496 qualified this */
9497 if (from_qualified_this)
9498 {
9499 field_decl = build_outer_field_access (qual_wfl, field_decl);
9500 from_qualified_this = 0;
9501 }
9502
e04a16fb
AG
9503 /* This is the decl found and eventually the next one to
9504 search from */
9505 decl = field_decl;
9506 }
e04a16fb
AG
9507 from_type = 0;
9508 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9509
9510 /* Sneak preview. If decl is qualified by a `new', report
9511 the error here to be accurate on the peculiar construct */
9512 if (TREE_CHAIN (q)
9513 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9514 && !JREFERENCE_TYPE_P (type))
9515 {
9516 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9517 lang_printable_name (type, 0));
9518 return 1;
9519 }
e04a16fb 9520 }
dde1da72
APB
9521 /* `q' might have changed due to a after package resolution
9522 re-qualification */
9523 if (!q)
9524 break;
e04a16fb
AG
9525 }
9526 *found_decl = decl;
9527 return 0;
9528}
9529
9530/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
e101152f
APB
9531 can't be accessed from REFERENCE (a record type). If MEMBER
9532 features a protected access, we then use WHERE which, if non null,
9533 holds the type of MEMBER's access that is checked against
9534 6.6.2.1. This function should be used when decl is a field or a
9535 method. */
e04a16fb 9536
be245ac0 9537static int
e101152f 9538not_accessible_p (reference, member, where, from_super)
e04a16fb 9539 tree reference, member;
e101152f 9540 tree where;
e04a16fb
AG
9541 int from_super;
9542{
9543 int access_flag = get_access_flags_from_decl (member);
9544
4dbf4496
APB
9545 /* Inner classes are processed by check_inner_class_access */
9546 if (INNER_CLASS_TYPE_P (reference))
9547 return 0;
9548
e04a16fb
AG
9549 /* Access always granted for members declared public */
9550 if (access_flag & ACC_PUBLIC)
9551 return 0;
9552
9553 /* Check access on protected members */
9554 if (access_flag & ACC_PROTECTED)
9555 {
9556 /* Access granted if it occurs from within the package
9557 containing the class in which the protected member is
9558 declared */
9559 if (class_in_current_package (DECL_CONTEXT (member)))
9560 return 0;
9561
9bbc7d9f
PB
9562 /* If accessed with the form `super.member', then access is granted */
9563 if (from_super)
9564 return 0;
e04a16fb 9565
e101152f
APB
9566 /* If where is active, access was made through a
9567 qualifier. Access is granted if the type of the qualifier is
9568 or is a sublass of the type the access made from (6.6.2.1.) */
9569 if (where && !inherits_from_p (where, reference))
9570 return 1;
9571
9bbc7d9f 9572 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9573 member is declared or a subclass of it. Find the right
9574 context to perform the check */
9575 if (PURE_INNER_CLASS_TYPE_P (reference))
9576 {
9577 while (INNER_CLASS_TYPE_P (reference))
9578 {
9579 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9580 return 0;
9581 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9582 }
9583 }
473e7b07 9584 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9585 return 0;
e04a16fb
AG
9586 return 1;
9587 }
9588
9589 /* Check access on private members. Access is granted only if it
761491c8
APB
9590 occurs from within the class in which it is declared -- that does
9591 it for innerclasses too. */
e04a16fb 9592 if (access_flag & ACC_PRIVATE)
761491c8
APB
9593 {
9594 if (reference == DECL_CONTEXT (member))
9595 return 0;
9596 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9597 return 0;
9598 return 1;
9599 }
e04a16fb
AG
9600
9601 /* Default access are permitted only when occuring within the
9602 package in which the type (REFERENCE) is declared. In other words,
9603 REFERENCE is defined in the current package */
9604 if (ctxp->package)
9605 return !class_in_current_package (reference);
473e7b07 9606
e04a16fb
AG
9607 /* Otherwise, access is granted */
9608 return 0;
9609}
9610
5e942c50
APB
9611/* Test deprecated decl access. */
9612static void
9613check_deprecation (wfl, decl)
9614 tree wfl, decl;
9615{
49f48c71 9616 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9617 /* Complain if the field is deprecated and the file it was defined
9618 in isn't compiled at the same time the file which contains its
9619 use is */
9620 if (DECL_DEPRECATED (decl)
9621 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9622 {
9623 char the [20];
9624 switch (TREE_CODE (decl))
9625 {
9626 case FUNCTION_DECL:
9627 strcpy (the, "method");
9628 break;
9629 case FIELD_DECL:
9630 strcpy (the, "field");
9631 break;
9632 case TYPE_DECL:
9633 strcpy (the, "class");
9634 break;
15fdcfe9 9635 default:
400500c4 9636 abort ();
5e942c50
APB
9637 }
9638 parse_warning_context
9639 (wfl, "The %s `%s' in class `%s' has been deprecated",
9640 the, lang_printable_name (decl, 0),
9641 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9642 }
9643}
9644
e04a16fb
AG
9645/* Returns 1 if class was declared in the current package, 0 otherwise */
9646
9647static int
9648class_in_current_package (class)
9649 tree class;
9650{
9651 static tree cache = NULL_TREE;
9652 int qualified_flag;
9653 tree left;
9654
9655 if (cache == class)
9656 return 1;
9657
9658 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9659
9660 /* If the current package is empty and the name of CLASS is
9661 qualified, class isn't in the current package. If there is a
9662 current package and the name of the CLASS is not qualified, class
9663 isn't in the current package */
0a2138e2 9664 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9665 return 0;
9666
9667 /* If there is not package and the name of CLASS isn't qualified,
9668 they belong to the same unnamed package */
9669 if (!ctxp->package && !qualified_flag)
9670 return 1;
9671
9672 /* Compare the left part of the name of CLASS with the package name */
9673 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9674 if (ctxp->package == left)
9675 {
19e223db
MM
9676 static int initialized_p;
9677 /* Register CACHE with the garbage collector. */
9678 if (!initialized_p)
9679 {
9680 ggc_add_tree_root (&cache, 1);
9681 initialized_p = 1;
9682 }
9683
e04a16fb
AG
9684 cache = class;
9685 return 1;
9686 }
9687 return 0;
9688}
9689
9690/* This function may generate code to access DECL from WHERE. This is
9691 done only if certain conditions meet. */
9692
9693static tree
9694maybe_access_field (decl, where, type)
9695 tree decl, where, type;
9696{
5e942c50
APB
9697 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9698 && !FIELD_STATIC (decl))
e04a16fb 9699 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9700 (type ? type : DECL_CONTEXT (decl)),
9701 DECL_NAME (decl));
e04a16fb
AG
9702 return decl;
9703}
9704
15fdcfe9 9705/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9706 and according to the situation, PRIMARY and WHERE may be
9707 used. IS_STATIC is set to 1 if the invoked function is static. */
9708
9709static tree
e101152f
APB
9710patch_method_invocation (patch, primary, where, from_super,
9711 is_static, ret_decl)
e04a16fb 9712 tree patch, primary, where;
e101152f 9713 int from_super;
e04a16fb 9714 int *is_static;
b9f7e36c 9715 tree *ret_decl;
e04a16fb
AG
9716{
9717 tree wfl = TREE_OPERAND (patch, 0);
9718 tree args = TREE_OPERAND (patch, 1);
9719 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9720 tree list;
22eed1e6 9721 int is_static_flag = 0;
89e09b9a 9722 int is_super_init = 0;
bccaf73a 9723 tree this_arg = NULL_TREE;
35ab11f0 9724 int is_array_clone_call = 0;
e04a16fb
AG
9725
9726 /* Should be overriden if everything goes well. Otherwise, if
9727 something fails, it should keep this value. It stop the
9728 evaluation of a bogus assignment. See java_complete_tree,
9729 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9730 evaluating an assignment */
9731 TREE_TYPE (patch) = error_mark_node;
9732
9733 /* Since lookup functions are messing with line numbers, save the
9734 context now. */
9735 java_parser_context_save_global ();
9736
9737 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9738
9739 /* Resolution of qualified name, excluding constructors */
9740 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9741 {
dde1da72 9742 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9743 /* Extract the last IDENTIFIER of the qualified
9744 expression. This is a wfl and we will use it's location
9745 data during error report. */
9746 identifier_wfl = cut_identifier_in_qualified (wfl);
9747 identifier = EXPR_WFL_NODE (identifier_wfl);
9748
9749 /* Given the context, IDENTIFIER is syntactically qualified
9750 as a MethodName. We need to qualify what's before */
9751 qualify_ambiguous_name (wfl);
dde1da72 9752 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9753
dde1da72
APB
9754 if (resolved == error_mark_node)
9755 PATCH_METHOD_RETURN_ERROR ();
9756
9757 type = GET_SKIP_TYPE (resolved);
9758 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9759
9760 if (JPRIMITIVE_TYPE_P (type))
9761 {
7e51098e
TT
9762 parse_error_context
9763 (identifier_wfl,
9764 "Can't invoke a method on primitive type `%s'",
9765 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9766 PATCH_METHOD_RETURN_ERROR ();
9767 }
9768
dde1da72
APB
9769 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9770 args = nreverse (args);
2c56429a 9771
e04a16fb 9772 /* We're resolving a call from a type */
dde1da72 9773 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9774 {
dde1da72 9775 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9776 {
9777 parse_error_context
781b0558
KG
9778 (identifier_wfl,
9779 "Can't make static reference to method `%s' in interface `%s'",
9780 IDENTIFIER_POINTER (identifier),
e04a16fb 9781 IDENTIFIER_POINTER (name));
b9f7e36c 9782 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9783 }
e04a16fb
AG
9784 if (list && !METHOD_STATIC (list))
9785 {
c2e3db92 9786 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9787 parse_error_context
9788 (identifier_wfl,
9789 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9790 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9791 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9792 free (fct_name);
b9f7e36c 9793 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9794 }
9795 }
e04a16fb 9796 else
dde1da72
APB
9797 this_arg = primary = resolved;
9798
35ab11f0
BM
9799 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9800 is_array_clone_call = 1;
9801
5e942c50 9802 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9803 wfl = identifier_wfl;
9804 }
9805 /* Resolution of simple names, names generated after a primary: or
9806 constructors */
9807 else
9808 {
cd531a2e 9809 tree class_to_search = NULL_TREE;
c2952b01 9810 int lc; /* Looking for Constructor */
e04a16fb
AG
9811
9812 /* We search constructor in their target class */
9813 if (CALL_CONSTRUCTOR_P (patch))
9814 {
22eed1e6
APB
9815 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9816 class_to_search = EXPR_WFL_NODE (wfl);
9817 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9818 this_identifier_node)
9819 class_to_search = NULL_TREE;
9820 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9821 super_identifier_node)
e04a16fb 9822 {
89e09b9a 9823 is_super_init = 1;
22eed1e6
APB
9824 if (CLASSTYPE_SUPER (current_class))
9825 class_to_search =
9826 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9827 else
9828 {
781b0558 9829 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9830 PATCH_METHOD_RETURN_ERROR ();
9831 }
e04a16fb 9832 }
22eed1e6
APB
9833
9834 /* Class to search is NULL if we're searching the current one */
9835 if (class_to_search)
e04a16fb 9836 {
c2952b01
APB
9837 class_to_search = resolve_and_layout (class_to_search, wfl);
9838
22eed1e6
APB
9839 if (!class_to_search)
9840 {
9841 parse_error_context
9842 (wfl, "Class `%s' not found in type declaration",
9843 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9844 PATCH_METHOD_RETURN_ERROR ();
9845 }
9846
5e942c50
APB
9847 /* Can't instantiate an abstract class, but we can
9848 invoke it's constructor. It's use within the `new'
9849 context is denied here. */
9850 if (CLASS_ABSTRACT (class_to_search)
9851 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9852 {
9853 parse_error_context
781b0558
KG
9854 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9855 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9856 PATCH_METHOD_RETURN_ERROR ();
9857 }
c2952b01 9858
22eed1e6 9859 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9860 }
22eed1e6
APB
9861 else
9862 class_to_search = current_class;
e04a16fb
AG
9863 lc = 1;
9864 }
9865 /* This is a regular search in the local class, unless an
9866 alternate class is specified. */
9867 else
9868 {
9869 class_to_search = (where ? where : current_class);
9870 lc = 0;
9871 }
c2952b01 9872
e04a16fb
AG
9873 /* NAME is a simple identifier or comes from a primary. Search
9874 in the class whose declaration contain the method being
9875 invoked. */
c877974e 9876 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9877
c2952b01 9878 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9879 /* Don't continue if no method were found, as the next statement
9880 can't be executed then. */
b9f7e36c
APB
9881 if (!list)
9882 PATCH_METHOD_RETURN_ERROR ();
35ab11f0
BM
9883
9884 if (TYPE_ARRAY_P (class_to_search)
9885 && DECL_NAME (list) == get_identifier ("clone"))
9886 is_array_clone_call = 1;
e04a16fb
AG
9887
9888 /* Check for static reference if non static methods */
9889 if (check_for_static_method_reference (wfl, patch, list,
9890 class_to_search, primary))
b9f7e36c 9891 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9892
165f37bc
APB
9893 /* Check for inner classes creation from illegal contexts */
9894 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9895 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9896 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9897 {
9898 parse_error_context
9899 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9900 lang_printable_name (class_to_search, 0),
9901 (!current_this ? "" :
9902 "; an explicit one must be provided when creating this inner class"));
9903 PATCH_METHOD_RETURN_ERROR ();
9904 }
9905
22eed1e6
APB
9906 /* Non static methods are called with the current object extra
9907 argument. If patch a `new TYPE()', the argument is the value
9908 returned by the object allocator. If method is resolved as a
9909 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9910 args = nreverse (args);
bccaf73a 9911 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9912 {
9913 this_arg = primary ? primary : current_this;
9914
9915 /* If we're using an access method, things are different.
9916 There are two familly of cases:
9917
9918 1) We're not generating bytecodes:
9919
9920 - LIST is non static. It's invocation is transformed from
9921 x(a1,...,an) into this$<n>.x(a1,....an).
9922 - LIST is static. It's invocation is transformed from
9923 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9924
9925 2) We're generating bytecodes:
9926
9927 - LIST is non static. It's invocation is transformed from
9928 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9929 - LIST is static. It's invocation is transformed from
9930 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9931
9932 Of course, this$<n> can be abitrary complex, ranging from
9933 this$0 (the immediate outer context) to
9934 access$0(access$0(...(this$0))).
9935
9936 maybe_use_access_method returns a non zero value if the
dfb99c83 9937 this_arg has to be moved into the (then generated) stub
4dbf4496 9938 argument list. In the meantime, the selected function
dfb99c83 9939 might have be replaced by a generated stub. */
c2952b01 9940 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9941 {
9942 args = tree_cons (NULL_TREE, this_arg, args);
9943 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9944 }
c2952b01 9945 }
e04a16fb 9946 }
b67d701b 9947
e04a16fb
AG
9948 /* Merge point of all resolution schemes. If we have nothing, this
9949 is an error, already signaled */
b9f7e36c
APB
9950 if (!list)
9951 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9952
e04a16fb
AG
9953 /* Check accessibility, position the is_static flag, build and
9954 return the call */
e101152f
APB
9955 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9956 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
35ab11f0
BM
9957 NULL_TREE), from_super)
9958 /* Calls to clone() on array types are permitted as a special-case. */
9959 && !is_array_clone_call)
e101152f
APB
9960 {
9961 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9962 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9963 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9964 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9965 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9966 ? "constructor" : "method");
9967 /* FIXME: WFL yields the wrong message here but I don't know
9968 what else to use. */
9969 parse_error_context (wfl,
9970 "Can't access %s %s `%s.%s' from `%s'",
9971 access, what, klass, fct_name, refklass);
b9f7e36c 9972 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9973 }
5e942c50 9974 check_deprecation (wfl, list);
22eed1e6 9975
c2952b01
APB
9976 /* If invoking a innerclass constructor, there are hidden parameters
9977 to pass */
9978 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9979 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9980 {
9981 /* And make sure we add the accessed local variables to be saved
9982 in field aliases. */
9983 args = build_alias_initializer_parameter_list
9984 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9985
da632f2c 9986 /* Secretly pass the current_this/primary as a second argument */
165f37bc 9987 if (primary || current_this)
f8b93ea7
APB
9988 {
9989 tree extra_arg;
9990 tree this_type = (current_this ?
9991 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9992 /* Method's (list) enclosing context */
9993 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9994 /* If we have a primary, use it. */
9995 if (primary)
9996 extra_arg = primary;
9997 /* The current `this' is an inner class but isn't a direct
9998 enclosing context for the inner class we're trying to
9999 create. Build an access to the proper enclosing context
10000 and use it. */
10001 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10002 && this_type != TREE_TYPE (mec))
10003 {
10004
10005 extra_arg = build_access_to_thisn (current_class,
10006 TREE_TYPE (mec), 0);
10007 extra_arg = java_complete_tree (extra_arg);
10008 }
10009 /* Otherwise, just use the current `this' as an enclosing
10010 context. */
10011 else
10012 extra_arg = current_this;
10013 args = tree_cons (NULL_TREE, extra_arg, args);
10014 }
165f37bc
APB
10015 else
10016 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10017 }
10018
152de068
APB
10019 /* This handles the situation where a constructor invocation needs
10020 to have an enclosing context passed as a second parameter (the
10021 constructor is one of an inner class. We extract it from the
10022 current function. */
10023 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10024 {
10025 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10026 tree extra_arg;
10027
10028 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10029 {
10030 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10031 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10032 }
10033 else
10034 {
10035 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10036 extra_arg =
10037 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10038 extra_arg = java_complete_tree (extra_arg);
10039 }
10040 args = tree_cons (NULL_TREE, extra_arg, args);
10041 }
10042
22eed1e6 10043 is_static_flag = METHOD_STATIC (list);
dba41d30 10044 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10045 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10046
c3f2a476
APB
10047 /* In the context of an explicit constructor invocation, we can't
10048 invoke any method relying on `this'. Exceptions are: we're
10049 invoking a static function, primary exists and is not the current
10050 this, we're creating a new object. */
22eed1e6 10051 if (ctxp->explicit_constructor_p
c3f2a476
APB
10052 && !is_static_flag
10053 && (!primary || primary == current_this)
10054 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10055 {
781b0558 10056 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10057 PATCH_METHOD_RETURN_ERROR ();
10058 }
e04a16fb 10059 java_parser_context_restore_global ();
22eed1e6
APB
10060 if (is_static)
10061 *is_static = is_static_flag;
b9f7e36c
APB
10062 /* Sometimes, we want the decl of the selected method. Such as for
10063 EH checking */
10064 if (ret_decl)
10065 *ret_decl = list;
89e09b9a
PB
10066 patch = patch_invoke (patch, list, args);
10067 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10068 {
c2952b01
APB
10069 tree finit_parms, finit_call;
10070
c00f0fb2 10071 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10072 finit_parms = build_alias_initializer_parameter_list
10073 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10074
c2952b01
APB
10075 finit_call =
10076 build_method_invocation (build_wfl_node (finit_identifier_node),
10077 finit_parms);
10078
10079 /* Generate the code used to initialize fields declared with an
10080 initialization statement and build a compound statement along
10081 with the super constructor invocation. */
89e09b9a
PB
10082 patch = build (COMPOUND_EXPR, void_type_node, patch,
10083 java_complete_tree (finit_call));
10084 CAN_COMPLETE_NORMALLY (patch) = 1;
10085 }
10086 return patch;
e04a16fb
AG
10087}
10088
10089/* Check that we're not trying to do a static reference to a method in
10090 non static method. Return 1 if it's the case, 0 otherwise. */
10091
10092static int
10093check_for_static_method_reference (wfl, node, method, where, primary)
10094 tree wfl, node, method, where, primary;
10095{
10096 if (METHOD_STATIC (current_function_decl)
10097 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10098 {
c2e3db92 10099 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10100 parse_error_context
10101 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10102 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10103 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10104 free (fct_name);
10105 return 1;
10106 }
10107 return 0;
10108}
10109
c2952b01
APB
10110/* Fix the invocation of *MDECL if necessary in the case of a
10111 invocation from an inner class. *THIS_ARG might be modified
10112 appropriately and an alternative access to *MDECL might be
10113 returned. */
10114
10115static int
10116maybe_use_access_method (is_super_init, mdecl, this_arg)
10117 int is_super_init;
10118 tree *mdecl, *this_arg;
10119{
10120 tree ctx;
10121 tree md = *mdecl, ta = *this_arg;
10122 int to_return = 0;
10123 int non_static_context = !METHOD_STATIC (md);
10124
10125 if (is_super_init
165f37bc
APB
10126 || DECL_CONTEXT (md) == current_class
10127 || !PURE_INNER_CLASS_TYPE_P (current_class)
10128 || DECL_FINIT_P (md))
c2952b01
APB
10129 return 0;
10130
10131 /* If we're calling a method found in an enclosing class, generate
10132 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10133 invoking a static method. Note that if MD's type is unrelated to
10134 CURRENT_CLASS, then the current this can be used. */
c2952b01 10135
2cb3951d 10136 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10137 {
10138 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10139 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10140 {
10141 ta = build_current_thisn (current_class);
10142 ta = build_wfl_node (ta);
10143 }
10144 else
10145 {
10146 tree type = ctx;
10147 while (type)
10148 {
10149 maybe_build_thisn_access_method (type);
4dbf4496 10150 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10151 {
10152 ta = build_access_to_thisn (ctx, type, 0);
10153 break;
10154 }
10155 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10156 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10157 }
10158 }
10159 ta = java_complete_tree (ta);
10160 }
10161
10162 /* We might have to use an access method to get to MD. We can
10163 break the method access rule as far as we're not generating
10164 bytecode */
10165 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10166 {
10167 md = build_outer_method_access_method (md);
10168 to_return = 1;
10169 }
10170
10171 *mdecl = md;
10172 *this_arg = ta;
10173
10174 /* Returnin a non zero value indicates we were doing a non static
10175 method invokation that is now a static invocation. It will have
10176 callee displace `this' to insert it in the regular argument
10177 list. */
10178 return (non_static_context && to_return);
10179}
10180
e04a16fb
AG
10181/* Patch an invoke expression METHOD and ARGS, based on its invocation
10182 mode. */
10183
10184static tree
89e09b9a 10185patch_invoke (patch, method, args)
e04a16fb 10186 tree patch, method, args;
e04a16fb
AG
10187{
10188 tree dtable, func;
0a2138e2 10189 tree original_call, t, ta;
e815887f 10190 tree cond = NULL_TREE;
e04a16fb 10191
5e942c50
APB
10192 /* Last step for args: convert build-in types. If we're dealing with
10193 a new TYPE() type call, the first argument to the constructor
e815887f 10194 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10195 `new' */
10196 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10197 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10198 t = TREE_CHAIN (t);
ac825856
APB
10199 for (ta = args; t != end_params_node && ta;
10200 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10201 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10202 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10203 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10204
10205 /* Resolve unresolved returned type isses */
10206 t = TREE_TYPE (TREE_TYPE (method));
10207 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10208 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10209
e8fc7396 10210 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10211 func = method;
10212 else
e04a16fb 10213 {
15fdcfe9 10214 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10215 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10216 {
10217 case INVOKE_VIRTUAL:
10218 dtable = invoke_build_dtable (0, args);
10219 func = build_invokevirtual (dtable, method);
10220 break;
b9f7e36c 10221
e815887f
TT
10222 case INVOKE_NONVIRTUAL:
10223 /* If the object for the method call is null, we throw an
10224 exception. We don't do this if the object is the current
10225 method's `this'. In other cases we just rely on an
10226 optimization pass to eliminate redundant checks. */
10227 if (TREE_VALUE (args) != current_this)
10228 {
10229 /* We use a SAVE_EXPR here to make sure we only evaluate
10230 the new `self' expression once. */
10231 tree save_arg = save_expr (TREE_VALUE (args));
10232 TREE_VALUE (args) = save_arg;
10233 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10234 null_pointer_node);
10235 }
10236 /* Fall through. */
10237
15fdcfe9
PB
10238 case INVOKE_SUPER:
10239 case INVOKE_STATIC:
10240 func = build_known_method_ref (method, TREE_TYPE (method),
10241 DECL_CONTEXT (method),
10242 signature, args);
10243 break;
e04a16fb 10244
15fdcfe9
PB
10245 case INVOKE_INTERFACE:
10246 dtable = invoke_build_dtable (1, args);
173f556c 10247 func = build_invokeinterface (dtable, method);
15fdcfe9 10248 break;
5e942c50 10249
15fdcfe9 10250 default:
400500c4 10251 abort ();
15fdcfe9
PB
10252 }
10253
10254 /* Ensure self_type is initialized, (invokestatic). FIXME */
10255 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10256 }
10257
e04a16fb
AG
10258 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10259 TREE_OPERAND (patch, 0) = func;
10260 TREE_OPERAND (patch, 1) = args;
10261 original_call = patch;
10262
e815887f 10263 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10264 returned value is the first argument to the constructor. We build
10265 a COMPOUND_EXPR and use saved expression so that the overall NEW
10266 expression value is a pointer to a newly created and initialized
10267 class. */
10268 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10269 {
10270 tree class = DECL_CONTEXT (method);
10271 tree c1, saved_new, size, new;
e8fc7396 10272 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10273 {
10274 TREE_TYPE (patch) = build_pointer_type (class);
10275 return patch;
10276 }
e04a16fb
AG
10277 if (!TYPE_SIZE (class))
10278 safe_layout_class (class);
10279 size = size_in_bytes (class);
10280 new = build (CALL_EXPR, promote_type (class),
10281 build_address_of (alloc_object_node),
10282 tree_cons (NULL_TREE, build_class_ref (class),
10283 build_tree_list (NULL_TREE,
10284 size_in_bytes (class))),
10285 NULL_TREE);
10286 saved_new = save_expr (new);
10287 c1 = build_tree_list (NULL_TREE, saved_new);
10288 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10289 TREE_OPERAND (original_call, 1) = c1;
10290 TREE_SET_CODE (original_call, CALL_EXPR);
10291 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10292 }
e815887f
TT
10293
10294 /* If COND is set, then we are building a check to see if the object
10295 is NULL. */
10296 if (cond != NULL_TREE)
10297 {
10298 /* We have to make the `then' branch a compound expression to
10299 make the types turn out right. This seems bizarre. */
10300 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10301 build (COMPOUND_EXPR, TREE_TYPE (patch),
10302 build (CALL_EXPR, void_type_node,
10303 build_address_of (soft_nullpointer_node),
10304 NULL_TREE, NULL_TREE),
10305 (FLOAT_TYPE_P (TREE_TYPE (patch))
10306 ? build_real (TREE_TYPE (patch), dconst0)
10307 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10308 integer_zero_node))),
10309 patch);
10310 TREE_SIDE_EFFECTS (patch) = 1;
10311 }
10312
e04a16fb
AG
10313 return patch;
10314}
10315
10316static int
10317invocation_mode (method, super)
10318 tree method;
10319 int super;
10320{
10321 int access = get_access_flags_from_decl (method);
10322
22eed1e6
APB
10323 if (super)
10324 return INVOKE_SUPER;
10325
e815887f 10326 if (access & ACC_STATIC)
e04a16fb
AG
10327 return INVOKE_STATIC;
10328
e815887f
TT
10329 /* We have to look for a constructor before we handle nonvirtual
10330 calls; otherwise the constructor will look nonvirtual. */
10331 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10332 return INVOKE_STATIC;
e815887f
TT
10333
10334 if (access & ACC_FINAL || access & ACC_PRIVATE)
10335 return INVOKE_NONVIRTUAL;
10336
10337 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10338 return INVOKE_NONVIRTUAL;
10339
e04a16fb
AG
10340 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10341 return INVOKE_INTERFACE;
22eed1e6 10342
e04a16fb
AG
10343 return INVOKE_VIRTUAL;
10344}
10345
b67d701b
PB
10346/* Retrieve a refined list of matching methods. It covers the step
10347 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10348
10349static tree
10350lookup_method_invoke (lc, cl, class, name, arg_list)
10351 int lc;
10352 tree cl;
10353 tree class, name, arg_list;
10354{
de4c7b02 10355 tree atl = end_params_node; /* Arg Type List */
c877974e 10356 tree method, signature, list, node;
49f48c71 10357 const char *candidates; /* Used for error report */
b5b8a0e7 10358 char *dup;
e04a16fb 10359
5e942c50 10360 /* Fix the arguments */
e04a16fb
AG
10361 for (node = arg_list; node; node = TREE_CHAIN (node))
10362 {
e3884b71 10363 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10364 /* Non primitive type may have to be resolved */
e3884b71 10365 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10366 resolve_and_layout (current_arg, NULL_TREE);
10367 /* And promoted */
b67d701b 10368 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10369 current_arg = promote_type (current_arg);
5e942c50 10370 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10371 }
e04a16fb 10372
c2952b01
APB
10373 /* Presto. If we're dealing with an anonymous class and a
10374 constructor call, generate the right constructor now, since we
10375 know the arguments' types. */
10376
10377 if (lc && ANONYMOUS_CLASS_P (class))
10378 craft_constructor (TYPE_NAME (class), atl);
10379
5e942c50
APB
10380 /* Find all candidates and then refine the list, searching for the
10381 most specific method. */
10382 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10383 list = find_most_specific_methods_list (list);
b67d701b
PB
10384 if (list && !TREE_CHAIN (list))
10385 return TREE_VALUE (list);
e04a16fb 10386
b67d701b
PB
10387 /* Issue an error. List candidates if any. Candidates are listed
10388 only if accessible (non accessible methods may end-up here for
10389 the sake of a better error report). */
10390 candidates = NULL;
10391 if (list)
e04a16fb 10392 {
e04a16fb 10393 tree current;
b67d701b 10394 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10395 for (current = list; current; current = TREE_CHAIN (current))
10396 {
b67d701b
PB
10397 tree cm = TREE_VALUE (current);
10398 char string [4096];
e101152f 10399 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
b67d701b 10400 continue;
b67d701b 10401 sprintf
22eed1e6
APB
10402 (string, " `%s' in `%s'%s",
10403 get_printable_method_name (cm),
b67d701b
PB
10404 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10405 (TREE_CHAIN (current) ? "\n" : ""));
10406 obstack_grow (&temporary_obstack, string, strlen (string));
10407 }
10408 obstack_1grow (&temporary_obstack, '\0');
10409 candidates = obstack_finish (&temporary_obstack);
10410 }
10411 /* Issue the error message */
c877974e
APB
10412 method = make_node (FUNCTION_TYPE);
10413 TYPE_ARG_TYPES (method) = atl;
b67d701b 10414 signature = build_java_argument_signature (method);
c63b98cd 10415 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10416 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10417 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10418 (lc ? dup : IDENTIFIER_POINTER (name)),
10419 IDENTIFIER_POINTER (signature), dup,
b67d701b 10420 (candidates ? candidates : ""));
b5b8a0e7 10421 free (dup);
b67d701b
PB
10422 return NULL_TREE;
10423}
10424
5e942c50
APB
10425/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10426 when we're looking for a constructor. */
b67d701b
PB
10427
10428static tree
5e942c50
APB
10429find_applicable_accessible_methods_list (lc, class, name, arglist)
10430 int lc;
b67d701b
PB
10431 tree class, name, arglist;
10432{
ad69b5b6
BM
10433 static struct hash_table t, *searched_classes = NULL;
10434 static int search_not_done = 0;
b67d701b
PB
10435 tree list = NULL_TREE, all_list = NULL_TREE;
10436
ad69b5b6
BM
10437 /* Check the hash table to determine if this class has been searched
10438 already. */
10439 if (searched_classes)
10440 {
10441 if (hash_lookup (searched_classes,
10442 (const hash_table_key) class, FALSE, NULL))
10443 return NULL;
10444 }
10445 else
10446 {
10447 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10448 java_hash_compare_tree_node);
10449 searched_classes = &t;
10450 }
10451
10452 search_not_done++;
10453 hash_lookup (searched_classes,
0c2b8145 10454 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10455
c2952b01
APB
10456 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10457 {
10458 load_class (class, 1);
10459 safe_layout_class (class);
10460 }
10461
1982388a 10462 /* Search interfaces */
9a7ab4b3
APB
10463 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10464 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10465 {
1982388a
APB
10466 int i, n;
10467 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10468 search_applicable_methods_list (lc, TYPE_METHODS (class),
10469 name, arglist, &list, &all_list);
1982388a 10470 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10471 for (i = 1; i < n; i++)
b67d701b 10472 {
de0b553f
APB
10473 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10474 tree rlist;
10475
de0b553f
APB
10476 rlist = find_applicable_accessible_methods_list (lc, t, name,
10477 arglist);
165f37bc 10478 list = chainon (rlist, list);
e04a16fb 10479 }
e04a16fb 10480 }
1982388a
APB
10481 /* Search classes */
10482 else
c2952b01 10483 {
165f37bc
APB
10484 tree sc = class;
10485 int seen_inner_class = 0;
c2952b01
APB
10486 search_applicable_methods_list (lc, TYPE_METHODS (class),
10487 name, arglist, &list, &all_list);
10488
94807d33
APB
10489 /* When looking finit$ or class$, we turn LC to 1 so that we
10490 only search in class. Note that we should have found
10491 something at this point. */
10492 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
493d561d
APB
10493 {
10494 lc = 1;
10495 if (!list)
400500c4 10496 abort ();
493d561d
APB
10497 }
10498
165f37bc
APB
10499 /* We must search all interfaces of this class */
10500 if (!lc)
10501 {
10502 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10503 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10504 for (i = 1; i < n; i++)
10505 {
10506 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10507 if (t != object_type_node)
30a3caef
ZW
10508 {
10509 tree rlist
10510 = find_applicable_accessible_methods_list (lc, t,
10511 name, arglist);
10512 list = chainon (rlist, list);
10513 }
165f37bc 10514 }
165f37bc
APB
10515 }
10516
c2952b01
APB
10517 /* Search enclosing context of inner classes before looking
10518 ancestors up. */
10519 while (!lc && INNER_CLASS_TYPE_P (class))
10520 {
165f37bc
APB
10521 tree rlist;
10522 seen_inner_class = 1;
c2952b01 10523 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10524 rlist = find_applicable_accessible_methods_list (lc, class,
10525 name, arglist);
10526 list = chainon (rlist, list);
c2952b01 10527 }
165f37bc
APB
10528
10529 if (!lc && seen_inner_class
10530 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10531 class = CLASSTYPE_SUPER (sc);
10532 else
10533 class = sc;
10534
ad69b5b6
BM
10535 /* Search superclass */
10536 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10537 {
10538 tree rlist;
10539 class = CLASSTYPE_SUPER (class);
10540 rlist = find_applicable_accessible_methods_list (lc, class,
10541 name, arglist);
10542 list = chainon (rlist, list);
10543 }
10544 }
10545
10546 search_not_done--;
10547
10548 /* We're done. Reset the searched classes list and finally search
10549 java.lang.Object if it wasn't searched already. */
10550 if (!search_not_done)
10551 {
10552 if (!lc
10553 && TYPE_METHODS (object_type_node)
10554 && !hash_lookup (searched_classes,
10555 (const hash_table_key) object_type_node,
10556 FALSE, NULL))
10557 {
10558 search_applicable_methods_list (lc,
10559 TYPE_METHODS (object_type_node),
10560 name, arglist, &list, &all_list);
10561 }
10562 hash_table_free (searched_classes);
10563 searched_classes = NULL;
c2952b01 10564 }
1982388a 10565
b67d701b
PB
10566 /* Either return the list obtained or all selected (but
10567 inaccessible) methods for better error report. */
10568 return (!list ? all_list : list);
10569}
e04a16fb 10570
ad69b5b6 10571/* Effectively search for the appropriate method in method */
1982388a
APB
10572
10573static void
c2952b01 10574search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10575 int lc;
10576 tree method, name, arglist;
10577 tree *list, *all_list;
10578{
10579 for (; method; method = TREE_CHAIN (method))
10580 {
10581 /* When dealing with constructor, stop here, otherwise search
10582 other classes */
10583 if (lc && !DECL_CONSTRUCTOR_P (method))
10584 continue;
10585 else if (!lc && (DECL_CONSTRUCTOR_P (method)
c4faeb92 10586 || (DECL_NAME (method) != name)))
1982388a 10587 continue;
c7303e41 10588
1982388a
APB
10589 if (argument_types_convertible (method, arglist))
10590 {
10591 /* Retain accessible methods only */
10592 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
e101152f 10593 method, NULL_TREE, 0))
1982388a
APB
10594 *list = tree_cons (NULL_TREE, method, *list);
10595 else
10596 /* Also retain all selected method here */
10597 *all_list = tree_cons (NULL_TREE, method, *list);
10598 }
10599 }
ad69b5b6 10600}
1982388a 10601
b67d701b
PB
10602/* 15.11.2.2 Choose the Most Specific Method */
10603
10604static tree
10605find_most_specific_methods_list (list)
10606 tree list;
10607{
10608 int max = 0;
9a7ab4b3 10609 int abstract, candidates;
b67d701b
PB
10610 tree current, new_list = NULL_TREE;
10611 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10612 {
b67d701b
PB
10613 tree method;
10614 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10615
10616 for (method = list; method; method = TREE_CHAIN (method))
10617 {
0c2b8145 10618 tree method_v, current_v;
b67d701b
PB
10619 /* Don't test a method against itself */
10620 if (method == current)
10621 continue;
10622
0c2b8145
APB
10623 method_v = TREE_VALUE (method);
10624 current_v = TREE_VALUE (current);
10625
10626 /* Compare arguments and location where methods where declared */
10627 if (argument_types_convertible (method_v, current_v))
b67d701b 10628 {
0c2b8145
APB
10629 if (valid_method_invocation_conversion_p
10630 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10631 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10632 && enclosing_context_p (DECL_CONTEXT (method_v),
10633 DECL_CONTEXT (current_v))))
10634 {
10635 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10636 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10637 max = (v > max ? v : max);
10638 }
b67d701b
PB
10639 }
10640 }
e04a16fb
AG
10641 }
10642
b67d701b 10643 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10644 for (current = list, abstract = -1, candidates = -1;
10645 current; current = TREE_CHAIN (current))
b67d701b 10646 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10647 {
10648 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10649 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10650 candidates++;
10651 }
b67d701b 10652
165f37bc
APB
10653 /* If we have several and they're all abstract, just pick the
10654 closest one. */
9a7ab4b3
APB
10655 if (candidates > 0 && (candidates == abstract))
10656 {
10657 new_list = nreverse (new_list);
10658 TREE_CHAIN (new_list) = NULL_TREE;
10659 }
165f37bc 10660
cab8e2bd
BM
10661 /* We have several (we couldn't find a most specific), all but one
10662 are abstract, we pick the only non abstract one. */
10663 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10664 {
9a7ab4b3
APB
10665 for (current = new_list; current; current = TREE_CHAIN (current))
10666 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10667 {
10668 TREE_CHAIN (current) = NULL_TREE;
10669 new_list = current;
10670 }
165f37bc
APB
10671 }
10672
b67d701b
PB
10673 /* If we can't find one, lower expectations and try to gather multiple
10674 maximally specific methods */
165f37bc 10675 while (!new_list && max)
b67d701b
PB
10676 {
10677 while (--max > 0)
10678 {
10679 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10680 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10681 }
b67d701b
PB
10682 }
10683
10684 return new_list;
e04a16fb
AG
10685}
10686
b67d701b
PB
10687/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10688 converted by method invocation conversion (5.3) to the type of the
10689 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10690 to change less often than M1. */
e04a16fb 10691
b67d701b
PB
10692static int
10693argument_types_convertible (m1, m2_or_arglist)
10694 tree m1, m2_or_arglist;
e04a16fb 10695{
b67d701b
PB
10696 static tree m2_arg_value = NULL_TREE;
10697 static tree m2_arg_cache = NULL_TREE;
19e223db 10698 static int initialized_p;
e04a16fb 10699
b67d701b 10700 register tree m1_arg, m2_arg;
e04a16fb 10701
19e223db
MM
10702 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10703 collector. */
10704 if (!initialized_p)
10705 {
10706 ggc_add_tree_root (&m2_arg_value, 1);
10707 ggc_add_tree_root (&m2_arg_cache, 1);
10708 initialized_p = 1;
10709 }
10710
c2952b01 10711 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10712
b67d701b
PB
10713 if (m2_arg_value == m2_or_arglist)
10714 m2_arg = m2_arg_cache;
10715 else
10716 {
10717 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10718 argument types */
10719 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10720 {
10721 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10722 if (!METHOD_STATIC (m2_or_arglist))
10723 m2_arg = TREE_CHAIN (m2_arg);
10724 }
10725 else
10726 m2_arg = m2_or_arglist;
e04a16fb 10727
b67d701b
PB
10728 m2_arg_value = m2_or_arglist;
10729 m2_arg_cache = m2_arg;
10730 }
e04a16fb 10731
de4c7b02 10732 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10733 {
c877974e 10734 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10735 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10736 TREE_VALUE (m2_arg)))
10737 break;
10738 m1_arg = TREE_CHAIN (m1_arg);
10739 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10740 }
de4c7b02 10741 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10742}
10743
10744/* Qualification routines */
10745
10746static void
10747qualify_ambiguous_name (id)
10748 tree id;
10749{
cd531a2e
KG
10750 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10751 saved_current_class;
d8fccff5 10752 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10753 int code;
e04a16fb
AG
10754
10755 /* We first qualify the first element, then derive qualification of
10756 others based on the first one. If the first element is qualified
10757 by a resolution (field or type), this resolution is stored in the
10758 QUAL_RESOLUTION of the qual element being examined. We need to
10759 save the current_class since the use of SUPER might change the
10760 its value. */
10761 saved_current_class = current_class;
10762 qual = EXPR_WFL_QUALIFICATION (id);
10763 do {
10764
10765 /* Simple qualified expression feature a qual_wfl that is a
10766 WFL. Expression derived from a primary feature more complicated
10767 things like a CALL_EXPR. Expression from primary need to be
10768 worked out to extract the part on which the qualification will
10769 take place. */
10770 qual_wfl = QUAL_WFL (qual);
10771 switch (TREE_CODE (qual_wfl))
10772 {
10773 case CALL_EXPR:
10774 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10775 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10776 {
10777 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10778 qual_wfl = QUAL_WFL (qual);
10779 }
10780 break;
d8fccff5 10781 case NEW_ARRAY_EXPR:
c2952b01 10782 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10783 qual = TREE_CHAIN (qual);
1a6d4fb7 10784 again = new_array_found = 1;
d8fccff5 10785 continue;
e04a16fb 10786 case CONVERT_EXPR:
f2760b27
APB
10787 break;
10788 case NEW_CLASS_EXPR:
e04a16fb
AG
10789 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10790 break;
c583dd46
APB
10791 case ARRAY_REF:
10792 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10793 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10794 break;
8576f094
APB
10795 case STRING_CST:
10796 qual = TREE_CHAIN (qual);
10797 qual_wfl = QUAL_WFL (qual);
10798 break;
165f37bc
APB
10799 case CLASS_LITERAL:
10800 qual = TREE_CHAIN (qual);
10801 qual_wfl = QUAL_WFL (qual);
10802 break;
0a2138e2
APB
10803 default:
10804 /* Fix for -Wall. Just break doing nothing */
10805 break;
e04a16fb 10806 }
8576f094 10807
e04a16fb
AG
10808 ptr_type = current_class;
10809 again = 0;
8576f094
APB
10810 code = TREE_CODE (qual_wfl);
10811
10812 /* Pos evaluation: non WFL leading expression nodes */
10813 if (code == CONVERT_EXPR
10814 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10815 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10816
cd7c5840
APB
10817 else if (code == INTEGER_CST)
10818 name = qual_wfl;
10819
ac22f9cb 10820 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10821 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10822 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10823
c2952b01
APB
10824 else if (code == TREE_LIST)
10825 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10826
37feda7d
APB
10827 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10828 || code == PLUS_EXPR)
8576f094
APB
10829 {
10830 qual = TREE_CHAIN (qual);
10831 qual_wfl = QUAL_WFL (qual);
10832 again = 1;
10833 }
10834 else
f441f671
APB
10835 {
10836 name = EXPR_WFL_NODE (qual_wfl);
10837 if (!name)
10838 {
10839 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10840 again = 1;
10841 }
10842 }
10843
e04a16fb
AG
10844 /* If we have a THIS (from a primary), we set the context accordingly */
10845 if (name == this_identifier_node)
10846 {
6e22695a
APB
10847 /* This isn't really elegant. One more added irregularity
10848 before I start using COMPONENT_REF (hopefully very soon.) */
10849 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10850 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10851 EXPR_WITH_FILE_LOCATION
10852 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10853 this_identifier_node)
10854 {
10855 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10856 qual = EXPR_WFL_QUALIFICATION (qual);
10857 }
e04a16fb
AG
10858 qual = TREE_CHAIN (qual);
10859 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10860 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10861 again = 1;
10862 else
10863 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10864 this_found = 1;
10865 }
10866 /* If we have a SUPER, we set the context accordingly */
10867 if (name == super_identifier_node)
10868 {
10869 current_class = CLASSTYPE_SUPER (ptr_type);
10870 /* Check that there is such a thing as a super class. If not,
10871 return. The error will be caught later on, during the
10872 resolution */
10873 if (!current_class)
10874 {
10875 current_class = saved_current_class;
10876 return;
10877 }
10878 qual = TREE_CHAIN (qual);
10879 /* Do one more interation to set things up */
10880 super_found = again = 1;
10881 }
10882 } while (again);
10883
f2760b27
APB
10884 /* If name appears within the scope of a local variable declaration
10885 or parameter declaration, then it is an expression name. We don't
10886 carry this test out if we're in the context of the use of SUPER
10887 or THIS */
cd7c5840
APB
10888 if (!this_found && !super_found
10889 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10890 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10891 {
10892 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10893 QUAL_RESOLUTION (qual) = decl;
10894 }
10895
10896 /* If within the class/interface NAME was found to be used there
10897 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10898 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10899 address length, it is OK. */
10900 else if ((decl = lookup_field_wrapper (ptr_type, name))
10901 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10902 {
10903 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10904 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10905 }
10906
1a6d4fb7 10907 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10908 - NAME is a class/interface declared within the compilation
10909 unit containing NAME,
10910 - NAME is imported via a single-type-import declaration,
10911 - NAME is declared in an another compilation unit of the package
10912 of the compilation unit containing NAME,
10913 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10914 of the compilation unit containing NAME.
10915 - NAME is actually a STRING_CST. */
cd7c5840
APB
10916 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10917 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10918 {
10919 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10920 QUAL_RESOLUTION (qual) = decl;
10921 }
10922
f2760b27 10923 /* Method call, array references and cast are expression name */
9bbc7d9f 10924 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10925 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10926 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10927 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10928
10929 /* Check here that NAME isn't declared by more than one
10930 type-import-on-demand declaration of the compilation unit
10931 containing NAME. FIXME */
10932
10933 /* Otherwise, NAME is reclassified as a package name */
10934 else
10935 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10936
10937 /* Propagate the qualification accross other components of the
10938 qualified name */
10939 for (qual = TREE_CHAIN (qual); qual;
10940 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10941 {
10942 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10943 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10944 else
10945 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10946 }
10947
10948 /* Store the global qualification for the ambiguous part of ID back
10949 into ID fields */
10950 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10951 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10952 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10953 RESOLVE_TYPE_NAME_P (id) = 1;
10954 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10955 RESOLVE_PACKAGE_NAME_P (id) = 1;
10956
10957 /* Restore the current class */
10958 current_class = saved_current_class;
10959}
10960
10961static int
10962breakdown_qualified (left, right, source)
10963 tree *left, *right, source;
10964{
63ad61ed 10965 char *p, *base;
e04a16fb
AG
10966 int l = IDENTIFIER_LENGTH (source);
10967
63ad61ed
ZW
10968 base = alloca (l + 1);
10969 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10970
e04a16fb 10971 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10972 p = base + l - 1;
e04a16fb
AG
10973 while (*p != '.' && p != base)
10974 p--;
10975
10976 /* We didn't find a '.'. Return an error */
10977 if (p == base)
10978 return 1;
10979
10980 *p = '\0';
10981 if (right)
10982 *right = get_identifier (p+1);
63ad61ed 10983 *left = get_identifier (base);
e04a16fb
AG
10984
10985 return 0;
10986}
10987
a648f4e4
BM
10988/* Return TRUE if two classes are from the same package. */
10989
10990static int
10991in_same_package (name1, name2)
10992 tree name1, name2;
10993{
10994 tree tmp;
10995 tree pkg1;
10996 tree pkg2;
10997
10998 if (TREE_CODE (name1) == TYPE_DECL)
10999 name1 = DECL_NAME (name1);
11000 if (TREE_CODE (name2) == TYPE_DECL)
11001 name2 = DECL_NAME (name2);
11002
11003 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11004 /* One in empty package. */
11005 return 0;
11006
11007 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11008 /* Both in empty package. */
11009 return 1;
11010
11011 breakdown_qualified (&pkg1, &tmp, name1);
11012 breakdown_qualified (&pkg2, &tmp, name2);
11013
11014 return (pkg1 == pkg2);
11015}
11016
e04a16fb 11017/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11018 local variable decls if present.
11019 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11020
11021static tree
11022java_complete_tree (node)
11023 tree node;
5b09b33e
PB
11024{
11025 node = java_complete_lhs (node);
c7303e41 11026 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11027 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11028 && !flag_emit_xref)
5b09b33e
PB
11029 {
11030 tree value = DECL_INITIAL (node);
11031 DECL_INITIAL (node) = NULL_TREE;
11032 value = fold_constant_for_init (value, node);
11033 DECL_INITIAL (node) = value;
11034 if (value != NULL_TREE)
c2952b01
APB
11035 {
11036 /* fold_constant_for_init sometimes widen the original type
11037 of the constant (i.e. byte to int.) It's not desirable,
11038 especially if NODE is a function argument. */
11039 if (TREE_CODE (value) == INTEGER_CST
11040 && TREE_TYPE (node) != TREE_TYPE (value))
11041 return convert (TREE_TYPE (node), value);
11042 else
11043 return value;
11044 }
c7303e41
APB
11045 else
11046 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11047 }
11048 return node;
11049}
11050
2aa11e97
APB
11051static tree
11052java_stabilize_reference (node)
11053 tree node;
11054{
11055 if (TREE_CODE (node) == COMPOUND_EXPR)
11056 {
11057 tree op0 = TREE_OPERAND (node, 0);
11058 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11059 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11060 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11061 return node;
11062 }
5cbdba64 11063 return stabilize_reference (node);
2aa11e97
APB
11064}
11065
5b09b33e
PB
11066/* Patch tree nodes in a function body. When a BLOCK is found, push
11067 local variable decls if present.
11068 Same as java_complete_tree, but does not resolve static finals to values. */
11069
11070static tree
11071java_complete_lhs (node)
11072 tree node;
e04a16fb 11073{
22eed1e6 11074 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11075 int flag;
e04a16fb
AG
11076
11077 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11078 worked out. */
e04a16fb
AG
11079 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11080 return node;
11081
11082 /* The switch block implements cases processing container nodes
11083 first. Contained nodes are always written back. Leaves come
11084 next and return a value. */
11085 switch (TREE_CODE (node))
11086 {
11087 case BLOCK:
11088
11089 /* 1- Block section.
11090 Set the local values on decl names so we can identify them
11091 faster when they're referenced. At that stage, identifiers
11092 are legal so we don't check for declaration errors. */
11093 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11094 {
11095 DECL_CONTEXT (cn) = current_function_decl;
11096 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11097 }
15fdcfe9
PB
11098 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11099 CAN_COMPLETE_NORMALLY (node) = 1;
11100 else
e04a16fb 11101 {
15fdcfe9
PB
11102 tree stmt = BLOCK_EXPR_BODY (node);
11103 tree *ptr;
11104 int error_seen = 0;
11105 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11106 {
c877974e
APB
11107 /* Re-order from (((A; B); C); ...; Z) to
11108 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11109 This makes it easier to scan the statements left-to-right
11110 without using recursion (which might overflow the stack
11111 if the block has many statements. */
11112 for (;;)
11113 {
11114 tree left = TREE_OPERAND (stmt, 0);
11115 if (TREE_CODE (left) != COMPOUND_EXPR)
11116 break;
11117 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11118 TREE_OPERAND (left, 1) = stmt;
11119 stmt = left;
11120 }
11121 BLOCK_EXPR_BODY (node) = stmt;
11122 }
11123
c877974e
APB
11124 /* Now do the actual complete, without deep recursion for
11125 long blocks. */
15fdcfe9 11126 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11127 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11128 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11129 {
11130 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11131 tree *next = &TREE_OPERAND (*ptr, 1);
11132 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11133 if (cur == empty_stmt_node)
11134 {
11135 /* Optimization; makes it easier to detect empty bodies.
11136 Most useful for <clinit> with all-constant initializer. */
11137 *ptr = *next;
11138 continue;
11139 }
15fdcfe9
PB
11140 if (TREE_CODE (cur) == ERROR_MARK)
11141 error_seen++;
11142 else if (! CAN_COMPLETE_NORMALLY (cur))
11143 {
11144 wfl_op2 = *next;
11145 for (;;)
11146 {
11147 if (TREE_CODE (wfl_op2) == BLOCK)
11148 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11149 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11150 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11151 else
11152 break;
11153 }
11154 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11155 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11156 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11157 }
11158 ptr = next;
11159 }
11160 *ptr = java_complete_tree (*ptr);
11161
11162 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11163 return error_mark_node;
15fdcfe9 11164 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11165 }
11166 /* Turn local bindings to null */
11167 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11168 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11169
11170 TREE_TYPE (node) = void_type_node;
11171 break;
11172
11173 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11174
b9f7e36c
APB
11175 case THROW_EXPR:
11176 wfl_op1 = TREE_OPERAND (node, 0);
11177 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11178 /* 14.19 A throw statement cannot complete normally. */
11179 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11180 return patch_throw_statement (node, wfl_op1);
11181
11182 case SYNCHRONIZED_EXPR:
11183 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11184 return patch_synchronized_statement (node, wfl_op1);
11185
b67d701b
PB
11186 case TRY_EXPR:
11187 return patch_try_statement (node);
11188
a7d8d81f
PB
11189 case TRY_FINALLY_EXPR:
11190 COMPLETE_CHECK_OP_0 (node);
11191 COMPLETE_CHECK_OP_1 (node);
11192 CAN_COMPLETE_NORMALLY (node)
11193 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11194 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11195 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11196 return node;
11197
5a005d9e
PB
11198 case CLEANUP_POINT_EXPR:
11199 COMPLETE_CHECK_OP_0 (node);
11200 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11201 CAN_COMPLETE_NORMALLY (node) =
11202 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11203 return node;
11204
11205 case WITH_CLEANUP_EXPR:
11206 COMPLETE_CHECK_OP_0 (node);
11207 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11208 CAN_COMPLETE_NORMALLY (node) =
11209 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11210 TREE_TYPE (node) = void_type_node;
11211 return node;
11212
e04a16fb
AG
11213 case LABELED_BLOCK_EXPR:
11214 PUSH_LABELED_BLOCK (node);
11215 if (LABELED_BLOCK_BODY (node))
11216 COMPLETE_CHECK_OP_1 (node);
11217 TREE_TYPE (node) = void_type_node;
11218 POP_LABELED_BLOCK ();
1fb89a4d
APB
11219
11220 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11221 {
11222 LABELED_BLOCK_BODY (node) = NULL_TREE;
11223 CAN_COMPLETE_NORMALLY (node) = 1;
11224 }
1fb89a4d 11225 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11226 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11227 return node;
11228
11229 case EXIT_BLOCK_EXPR:
11230 /* We don't complete operand 1, because it's the return value of
11231 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11232 return patch_bc_statement (node);
11233
15fdcfe9
PB
11234 case CASE_EXPR:
11235 cn = java_complete_tree (TREE_OPERAND (node, 0));
11236 if (cn == error_mark_node)
11237 return cn;
11238
8576f094
APB
11239 /* First, the case expression must be constant. Values of final
11240 fields are accepted. */
15fdcfe9 11241 cn = fold (cn);
8576f094
APB
11242 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11243 && JDECL_P (TREE_OPERAND (cn, 1))
11244 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11245 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11246 {
100f7cd8
APB
11247 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11248 TREE_OPERAND (cn, 1));
100f7cd8 11249 }
15fdcfe9 11250
ce6e9147 11251 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11252 {
11253 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11254 parse_error_context (node, "Constant expression required");
11255 return error_mark_node;
11256 }
11257
11258 nn = ctxp->current_loop;
11259
11260 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11261 if (!try_builtin_assignconv (NULL_TREE,
11262 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11263 {
11264 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11265 parse_error_context
11266 (wfl_operator,
11267 "Incompatible type for case. Can't convert `%s' to `int'",
11268 lang_printable_name (TREE_TYPE (cn), 0));
11269 return error_mark_node;
11270 }
11271
11272 cn = fold (convert (int_type_node, cn));
11273
11274 /* Multiple instance of a case label bearing the same
11275 value is checked during code generation. The case
11276 expression is allright so far. */
34d4df06
APB
11277 if (TREE_CODE (cn) == VAR_DECL)
11278 cn = DECL_INITIAL (cn);
15fdcfe9 11279 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11280 TREE_TYPE (node) = void_type_node;
15fdcfe9 11281 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11282 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11283 break;
11284
11285 case DEFAULT_EXPR:
11286 nn = ctxp->current_loop;
11287 /* Only one default label is allowed per switch statement */
11288 if (SWITCH_HAS_DEFAULT (nn))
11289 {
11290 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11291 parse_error_context (wfl_operator,
11292 "Duplicate case label: `default'");
11293 return error_mark_node;
11294 }
11295 else
11296 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11297 TREE_TYPE (node) = void_type_node;
10100cc7 11298 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11299 CAN_COMPLETE_NORMALLY (node) = 1;
11300 break;
11301
b67d701b 11302 case SWITCH_EXPR:
e04a16fb
AG
11303 case LOOP_EXPR:
11304 PUSH_LOOP (node);
11305 /* Check whether the loop was enclosed in a labeled
11306 statement. If not, create one, insert the loop in it and
11307 return the node */
11308 nn = patch_loop_statement (node);
b67d701b 11309
e04a16fb 11310 /* Anyways, walk the body of the loop */
b67d701b
PB
11311 if (TREE_CODE (node) == LOOP_EXPR)
11312 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11313 /* Switch statement: walk the switch expression and the cases */
11314 else
11315 node = patch_switch_statement (node);
11316
e7c7bcef 11317 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11318 nn = error_mark_node;
11319 else
15fdcfe9 11320 {
b635eb2f
PB
11321 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11322 /* If we returned something different, that's because we
11323 inserted a label. Pop the label too. */
11324 if (nn != node)
11325 {
11326 if (CAN_COMPLETE_NORMALLY (node))
11327 CAN_COMPLETE_NORMALLY (nn) = 1;
11328 POP_LABELED_BLOCK ();
11329 }
15fdcfe9 11330 }
e04a16fb
AG
11331 POP_LOOP ();
11332 return nn;
11333
11334 case EXIT_EXPR:
11335 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11336 return patch_exit_expr (node);
11337
11338 case COND_EXPR:
11339 /* Condition */
11340 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11341 if (TREE_OPERAND (node, 0) == error_mark_node)
11342 return error_mark_node;
11343 /* then-else branches */
11344 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11345 if (TREE_OPERAND (node, 1) == error_mark_node)
11346 return error_mark_node;
11347 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11348 if (TREE_OPERAND (node, 2) == error_mark_node)
11349 return error_mark_node;
11350 return patch_if_else_statement (node);
11351 break;
11352
22eed1e6
APB
11353 case CONDITIONAL_EXPR:
11354 /* Condition */
11355 wfl_op1 = TREE_OPERAND (node, 0);
11356 COMPLETE_CHECK_OP_0 (node);
11357 wfl_op2 = TREE_OPERAND (node, 1);
11358 COMPLETE_CHECK_OP_1 (node);
11359 wfl_op3 = TREE_OPERAND (node, 2);
11360 COMPLETE_CHECK_OP_2 (node);
11361 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11362
e04a16fb
AG
11363 /* 3- Expression section */
11364 case COMPOUND_EXPR:
15fdcfe9 11365 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11366 TREE_OPERAND (node, 0) = nn =
11367 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11368 if (wfl_op2 == empty_stmt_node)
11369 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11370 else
15fdcfe9 11371 {
dc0b3eff 11372 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11373 {
dc0b3eff
PB
11374 /* An unreachable condition in a do-while statement
11375 is *not* (technically) an unreachable statement. */
11376 nn = wfl_op2;
11377 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11378 nn = EXPR_WFL_NODE (nn);
11379 if (TREE_CODE (nn) != EXIT_EXPR)
11380 {
11381 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11382 parse_error_context (wfl_operator, "Unreachable statement");
11383 }
bccaf73a 11384 }
dc0b3eff
PB
11385 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11386 if (TREE_OPERAND (node, 1) == error_mark_node)
11387 return error_mark_node;
11388 CAN_COMPLETE_NORMALLY (node)
11389 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11390 }
e04a16fb
AG
11391 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11392 break;
11393
11394 case RETURN_EXPR:
15fdcfe9 11395 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11396 return patch_return (node);
11397
11398 case EXPR_WITH_FILE_LOCATION:
11399 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11400 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11401 {
5423609c 11402 tree wfl = node;
15fdcfe9 11403 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11404 if (node == error_mark_node)
11405 return node;
5423609c
APB
11406 /* Keep line number information somewhere were it doesn't
11407 disrupt the completion process. */
2c56429a 11408 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11409 {
11410 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11411 TREE_OPERAND (node, 1) = wfl;
11412 }
15fdcfe9
PB
11413 CAN_COMPLETE_NORMALLY (node) = 1;
11414 }
e04a16fb
AG
11415 else
11416 {
5b09b33e
PB
11417 tree body;
11418 int save_lineno = lineno;
11419 lineno = EXPR_WFL_LINENO (node);
11420 body = java_complete_tree (EXPR_WFL_NODE (node));
11421 lineno = save_lineno;
15fdcfe9 11422 EXPR_WFL_NODE (node) = body;
dc0b3eff 11423 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11424 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
00b4575d 11425 if (body == empty_stmt_node || TREE_CONSTANT (body))
cd9643f7 11426 {
00b4575d 11427 /* Makes it easier to constant fold, detect empty bodies. */
cd9643f7
PB
11428 return body;
11429 }
dc0b3eff 11430 if (body == error_mark_node)
e04a16fb
AG
11431 {
11432 /* Its important for the evaluation of assignment that
11433 this mark on the TREE_TYPE is propagated. */
11434 TREE_TYPE (node) = error_mark_node;
11435 return error_mark_node;
11436 }
11437 else
11438 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11439
e04a16fb
AG
11440 }
11441 break;
11442
b67d701b 11443 case NEW_ARRAY_EXPR:
e04a16fb
AG
11444 /* Patch all the dimensions */
11445 flag = 0;
11446 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11447 {
11448 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11449 tree dim = convert (int_type_node,
11450 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11451 if (dim == error_mark_node)
11452 {
11453 flag = 1;
11454 continue;
11455 }
11456 else
11457 {
b9f7e36c 11458 TREE_VALUE (cn) = dim;
e04a16fb
AG
11459 /* Setup the location of the current dimension, for
11460 later error report. */
11461 TREE_PURPOSE (cn) =
11462 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11463 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11464 }
11465 }
11466 /* They complete the array creation expression, if no errors
11467 were found. */
15fdcfe9 11468 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11469 return (flag ? error_mark_node
11470 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11471
c2952b01
APB
11472 case NEW_ANONYMOUS_ARRAY_EXPR:
11473 /* Create the array type if necessary. */
11474 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11475 {
11476 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11477 if (!(type = resolve_type_during_patch (type)))
11478 return error_mark_node;
11479 type = build_array_from_name (type, NULL_TREE,
11480 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11481 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11482 }
11483 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11484 ANONYMOUS_ARRAY_INITIALIZER (node));
11485 if (node == error_mark_node)
11486 return error_mark_node;
11487 CAN_COMPLETE_NORMALLY (node) = 1;
11488 return node;
11489
b67d701b 11490 case NEW_CLASS_EXPR:
e04a16fb 11491 case CALL_EXPR:
b67d701b 11492 /* Complete function's argument(s) first */
e04a16fb
AG
11493 if (complete_function_arguments (node))
11494 return error_mark_node;
11495 else
b9f7e36c 11496 {
22eed1e6
APB
11497 tree decl, wfl = TREE_OPERAND (node, 0);
11498 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
e101152f
APB
11499 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11500 super_identifier_node);
22eed1e6 11501
e101152f
APB
11502 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11503 from_super, 0, &decl);
c877974e
APB
11504 if (node == error_mark_node)
11505 return error_mark_node;
11506
11507 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11508 /* If we call this(...), register signature and positions */
11509 if (in_this)
11510 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11511 tree_cons (wfl, decl,
11512 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11513 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11514 return force_evaluation_order (node);
b9f7e36c 11515 }
e04a16fb
AG
11516
11517 case MODIFY_EXPR:
11518 /* Save potential wfls */
11519 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11520 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11521
cd9643f7
PB
11522 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11523 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11524 && DECL_INITIAL (nn) != NULL_TREE)
11525 {
100f7cd8
APB
11526 tree value;
11527
100f7cd8 11528 value = fold_constant_for_init (nn, nn);
c2952b01 11529
cd9643f7
PB
11530 if (value != NULL_TREE)
11531 {
11532 tree type = TREE_TYPE (value);
c2952b01
APB
11533 if (JPRIMITIVE_TYPE_P (type) ||
11534 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11535 return empty_stmt_node;
11536 }
629d4b4d
APB
11537 if (! flag_emit_class_files)
11538 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11539 if (CLASS_FINAL_VARIABLE_P (nn))
11540 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11541 }
e04a16fb 11542 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11543
e04a16fb
AG
11544 if (TREE_OPERAND (node, 0) == error_mark_node)
11545 return error_mark_node;
11546
5cbdba64
APB
11547 flag = COMPOUND_ASSIGN_P (wfl_op2);
11548 if (flag)
e04a16fb 11549 {
c2952b01
APB
11550 /* This might break when accessing outer field from inner
11551 class. TESTME, FIXME */
2aa11e97 11552 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb 11553
e101152f 11554 /* Hand stabilize the lhs on both places */
e04a16fb 11555 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11556 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11557 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11558
5cbdba64 11559 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11560 /* Now complete the RHS. We write it back later on. */
11561 nn = java_complete_tree (TREE_OPERAND (node, 1));
11562
642f15d1
APB
11563 if ((cn = patch_string (nn)))
11564 nn = cn;
11565
2aa11e97
APB
11566 /* The last part of the rewrite for E1 op= E2 is to have
11567 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11568 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11569 TREE_TYPE (lvalue), nn));
5cbdba64 11570
568aac9c
TT
11571 /* If the assignment is compound and has reference type,
11572 then ensure the LHS has type String and nothing else. */
11573 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11574 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11575 parse_error_context (wfl_op2,
11576 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11577 lang_printable_name (TREE_TYPE (lvalue), 0));
11578
5cbdba64 11579 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11580 }
11581
f8976021 11582 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11583 function to complete this RHS. Note that a NEW_ARRAY_INIT
11584 might have been already fully expanded if created as a result
11585 of processing an anonymous array initializer. We avoid doing
11586 the operation twice by testing whether the node already bears
11587 a type. */
11588 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11589 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11590 TREE_OPERAND (node, 1));
2aa11e97 11591 /* Otherwise we simply complete the RHS */
f8976021
APB
11592 else
11593 nn = java_complete_tree (TREE_OPERAND (node, 1));
11594
e04a16fb 11595 if (nn == error_mark_node)
c0d87ff6 11596 return error_mark_node;
2aa11e97
APB
11597
11598 /* Write back the RHS as we evaluated it. */
e04a16fb 11599 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11600
11601 /* In case we're handling = with a String as a RHS, we need to
11602 produce a String out of the RHS (it might still be a
11603 STRING_CST or a StringBuffer at this stage */
11604 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11605 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11606
11607 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11608 TREE_OPERAND (node, 1))))
11609 {
11610 /* We return error_mark_node if outer_field_access_fix
11611 detects we write into a final. */
11612 if (nn == error_mark_node)
11613 return error_mark_node;
11614 node = nn;
11615 }
11616 else
11617 {
11618 node = patch_assignment (node, wfl_op1, wfl_op2);
11619 /* Reorganize the tree if necessary. */
11620 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11621 || JSTRING_P (TREE_TYPE (node))))
11622 node = java_refold (node);
11623 }
11624
15fdcfe9
PB
11625 CAN_COMPLETE_NORMALLY (node) = 1;
11626 return node;
e04a16fb
AG
11627
11628 case MULT_EXPR:
11629 case PLUS_EXPR:
11630 case MINUS_EXPR:
11631 case LSHIFT_EXPR:
11632 case RSHIFT_EXPR:
11633 case URSHIFT_EXPR:
11634 case BIT_AND_EXPR:
11635 case BIT_XOR_EXPR:
11636 case BIT_IOR_EXPR:
11637 case TRUNC_MOD_EXPR:
c2952b01 11638 case TRUNC_DIV_EXPR:
e04a16fb
AG
11639 case RDIV_EXPR:
11640 case TRUTH_ANDIF_EXPR:
11641 case TRUTH_ORIF_EXPR:
11642 case EQ_EXPR:
11643 case NE_EXPR:
11644 case GT_EXPR:
11645 case GE_EXPR:
11646 case LT_EXPR:
11647 case LE_EXPR:
11648 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11649 knows how to handle those cases. */
11650 wfl_op1 = TREE_OPERAND (node, 0);
11651 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11652
15fdcfe9 11653 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11654 /* Don't complete string nodes if dealing with the PLUS operand. */
11655 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11656 {
11657 nn = java_complete_tree (wfl_op1);
11658 if (nn == error_mark_node)
11659 return error_mark_node;
48a840d9 11660
2aa11e97
APB
11661 TREE_OPERAND (node, 0) = nn;
11662 }
b67d701b 11663 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11664 {
11665 nn = java_complete_tree (wfl_op2);
11666 if (nn == error_mark_node)
11667 return error_mark_node;
48a840d9 11668
2aa11e97
APB
11669 TREE_OPERAND (node, 1) = nn;
11670 }
dc0b3eff 11671 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11672
5e942c50
APB
11673 case INSTANCEOF_EXPR:
11674 wfl_op1 = TREE_OPERAND (node, 0);
11675 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11676 if (flag_emit_xref)
11677 {
11678 TREE_TYPE (node) = boolean_type_node;
11679 return node;
11680 }
5e942c50
APB
11681 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11682
b67d701b 11683 case UNARY_PLUS_EXPR:
e04a16fb
AG
11684 case NEGATE_EXPR:
11685 case TRUTH_NOT_EXPR:
11686 case BIT_NOT_EXPR:
11687 case PREDECREMENT_EXPR:
11688 case PREINCREMENT_EXPR:
11689 case POSTDECREMENT_EXPR:
11690 case POSTINCREMENT_EXPR:
11691 case CONVERT_EXPR:
11692 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11693 how to handle those cases. */
11694 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11695 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11696 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11697 if (TREE_OPERAND (node, 0) == error_mark_node)
11698 return error_mark_node;
4a5f66c3
APB
11699 node = patch_unaryop (node, wfl_op1);
11700 CAN_COMPLETE_NORMALLY (node) = 1;
11701 break;
e04a16fb
AG
11702
11703 case ARRAY_REF:
11704 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11705 how to handle those cases. */
11706 wfl_op1 = TREE_OPERAND (node, 0);
11707 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11708 if (TREE_OPERAND (node, 0) == error_mark_node)
11709 return error_mark_node;
7f1d4866 11710 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11711 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11712 /* The same applies to wfl_op2 */
11713 wfl_op2 = TREE_OPERAND (node, 1);
11714 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11715 if (TREE_OPERAND (node, 1) == error_mark_node)
11716 return error_mark_node;
7f1d4866 11717 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11718 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11719 return patch_array_ref (node);
e04a16fb 11720
63a212ed
PB
11721 case RECORD_TYPE:
11722 return node;;
11723
11724 case COMPONENT_REF:
11725 /* The first step in the re-write of qualified name handling. FIXME.
11726 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11727 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11728 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11729 {
11730 tree name = TREE_OPERAND (node, 1);
11731 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11732 if (field == NULL_TREE)
11733 {
11734 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11735 return error_mark_node;
11736 }
11737 if (! FIELD_STATIC (field))
11738 {
11739 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11740 return error_mark_node;
11741 }
11742 return field;
11743 }
11744 else
400500c4 11745 abort ();
9bbc7d9f 11746 break;
9bbc7d9f 11747
b67d701b 11748 case THIS_EXPR:
e04a16fb
AG
11749 /* Can't use THIS in a static environment */
11750 if (!current_this)
11751 {
11752 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11753 parse_error_context (wfl_operator,
11754 "Keyword `this' used outside allowed context");
e04a16fb
AG
11755 TREE_TYPE (node) = error_mark_node;
11756 return error_mark_node;
11757 }
22eed1e6
APB
11758 if (ctxp->explicit_constructor_p)
11759 {
11760 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11761 parse_error_context
781b0558 11762 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11763 TREE_TYPE (node) = error_mark_node;
11764 return error_mark_node;
11765 }
e04a16fb 11766 return current_this;
c2952b01
APB
11767
11768 case CLASS_LITERAL:
11769 CAN_COMPLETE_NORMALLY (node) = 1;
11770 node = patch_incomplete_class_ref (node);
11771 if (node == error_mark_node)
11772 return error_mark_node;
11773 break;
11774
11775 case INSTANCE_INITIALIZERS_EXPR:
11776 in_instance_initializer++;
11777 node = java_complete_tree (TREE_OPERAND (node, 0));
11778 in_instance_initializer--;
11779 if (node != error_mark_node)
11780 TREE_TYPE (node) = void_type_node;
11781 else
11782 return error_mark_node;
11783 break;
e04a16fb 11784
e04a16fb 11785 default:
15fdcfe9 11786 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11787 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11788 and it's time to turn it into the appropriate String object */
11789 if ((nn = patch_string (node)))
11790 node = nn;
11791 else
400500c4 11792 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11793 }
11794 return node;
11795}
11796
11797/* Complete function call's argument. Return a non zero value is an
11798 error was found. */
11799
11800static int
11801complete_function_arguments (node)
11802 tree node;
11803{
11804 int flag = 0;
11805 tree cn;
11806
f63991a8 11807 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11808 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11809 {
b67d701b 11810 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11811 parm = java_complete_tree (wfl);
c2952b01 11812
e04a16fb
AG
11813 if (parm == error_mark_node)
11814 {
11815 flag = 1;
11816 continue;
11817 }
b67d701b
PB
11818 /* If have a string literal that we haven't transformed yet or a
11819 crafted string buffer, as a result of use of the the String
11820 `+' operator. Build `parm.toString()' and expand it. */
11821 if ((temp = patch_string (parm)))
b9f7e36c 11822 parm = temp;
5e942c50
APB
11823 /* Inline PRIMTYPE.TYPE read access */
11824 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11825
5e942c50 11826 TREE_VALUE (cn) = parm;
e04a16fb 11827 }
f63991a8 11828 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11829 return flag;
11830}
11831
11832/* Sometimes (for loops and variable initialized during their
11833 declaration), we want to wrap a statement around a WFL and turn it
11834 debugable. */
11835
11836static tree
11837build_debugable_stmt (location, stmt)
11838 int location;
11839 tree stmt;
11840{
11841 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11842 {
11843 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11844 EXPR_WFL_LINECOL (stmt) = location;
11845 }
11846 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11847 return stmt;
11848}
11849
11850static tree
11851build_expr_block (body, decls)
11852 tree body, decls;
11853{
11854 tree node = make_node (BLOCK);
11855 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11856 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11857 if (body)
11858 TREE_TYPE (node) = TREE_TYPE (body);
11859 TREE_SIDE_EFFECTS (node) = 1;
11860 return node;
11861}
11862
b67d701b
PB
11863/* Create a new function block and link it approriately to current
11864 function block chain */
e04a16fb
AG
11865
11866static tree
11867enter_block ()
11868{
47a50de9 11869 tree b = build_expr_block (NULL_TREE, NULL_TREE);
b67d701b 11870
47a50de9
PB
11871 /* Link block B supercontext to the previous block. The current
11872 function DECL is used as supercontext when enter_a_block is called
11873 for the first time for a given function. The current function body
11874 (DECL_FUNCTION_BODY) is set to be block B. */
b67d701b 11875
e04a16fb
AG
11876 tree fndecl = current_function_decl;
11877
f099f336
APB
11878 if (!fndecl) {
11879 BLOCK_SUPERCONTEXT (b) = current_static_block;
11880 current_static_block = b;
11881 }
11882
11883 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11884 {
11885 BLOCK_SUPERCONTEXT (b) = fndecl;
11886 DECL_FUNCTION_BODY (fndecl) = b;
11887 }
11888 else
11889 {
11890 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11891 DECL_FUNCTION_BODY (fndecl) = b;
11892 }
11893 return b;
11894}
11895
11896/* Exit a block by changing the current function body
11897 (DECL_FUNCTION_BODY) to the current block super context, only if
11898 the block being exited isn't the method's top level one. */
11899
11900static tree
11901exit_block ()
11902{
f099f336
APB
11903 tree b;
11904 if (current_function_decl)
11905 {
11906 b = DECL_FUNCTION_BODY (current_function_decl);
11907 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11908 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11909 }
11910 else
11911 {
11912 b = current_static_block;
e04a16fb 11913
f099f336
APB
11914 if (BLOCK_SUPERCONTEXT (b))
11915 current_static_block = BLOCK_SUPERCONTEXT (b);
11916 }
e04a16fb
AG
11917 return b;
11918}
11919
11920/* Lookup for NAME in the nested function's blocks, all the way up to
11921 the current toplevel one. It complies with Java's local variable
11922 scoping rules. */
11923
11924static tree
11925lookup_name_in_blocks (name)
11926 tree name;
11927{
f099f336 11928 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11929
11930 while (b != current_function_decl)
11931 {
11932 tree current;
11933
11934 /* Paranoid sanity check. To be removed */
11935 if (TREE_CODE (b) != BLOCK)
400500c4 11936 abort ();
e04a16fb
AG
11937
11938 for (current = BLOCK_EXPR_DECLS (b); current;
11939 current = TREE_CHAIN (current))
11940 if (DECL_NAME (current) == name)
11941 return current;
11942 b = BLOCK_SUPERCONTEXT (b);
11943 }
11944 return NULL_TREE;
11945}
11946
11947static void
11948maybe_absorb_scoping_blocks ()
11949{
b16e8f08 11950 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11951 {
11952 tree b = exit_block ();
11953 java_method_add_stmt (current_function_decl, b);
11954 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11955 }
11956}
11957
11958\f
11959/* This section of the source is reserved to build_* functions that
11960 are building incomplete tree nodes and the patch_* functions that
11961 are completing them. */
11962
c2952b01
APB
11963/* Wrap a non WFL node around a WFL. */
11964static tree
9a7ab4b3 11965build_wfl_wrap (node, location)
c2952b01 11966 tree node;
9a7ab4b3 11967 int location;
c2952b01
APB
11968{
11969 tree wfl, node_to_insert = node;
11970
11971 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11972 with the way we're processing SUPER. A THIS from a primary as a
11973 different form than a SUPER. Turn THIS into something symbolic */
11974 if (TREE_CODE (node) == THIS_EXPR)
11975 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11976 else
11977 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11978
9a7ab4b3 11979 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11980 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11981 return wfl;
11982}
11983
11984
9bbc7d9f 11985/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11986 we're currently dealing with the class java.lang.Object. */
11987
11988static tree
e920ebc9
APB
11989build_super_invocation (mdecl)
11990 tree mdecl;
22eed1e6 11991{
e920ebc9 11992 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11993 return empty_stmt_node;
22eed1e6
APB
11994 else
11995 {
9ee9b555 11996 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11997 tree a = NULL_TREE, t;
11998 /* If we're dealing with an anonymous class, pass the arguments
11999 of the crafted constructor along. */
12000 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12001 {
12002 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12003 for (; t != end_params_node; t = TREE_CHAIN (t))
12004 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12005 }
12006 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12007 }
12008}
12009
12010/* Build a SUPER/THIS qualified method invocation. */
12011
12012static tree
12013build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12014 int use_this;
12015 tree name, args;
12016 int lloc, rloc;
22eed1e6
APB
12017{
12018 tree invok;
12019 tree wfl =
9ee9b555 12020 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12021 EXPR_WFL_LINECOL (wfl) = lloc;
12022 invok = build_method_invocation (name, args);
12023 return make_qualified_primary (wfl, invok, rloc);
12024}
12025
b67d701b 12026/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12027
12028static tree
12029build_method_invocation (name, args)
12030 tree name;
12031 tree args;
12032{
12033 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12034 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12035 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12036 return call;
12037}
12038
12039/* Build an incomplete new xxx(...) node. */
12040
12041static tree
12042build_new_invocation (name, args)
12043 tree name, args;
12044{
12045 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12046 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12047 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12048 return call;
12049}
12050
12051/* Build an incomplete assignment expression. */
12052
12053static tree
12054build_assignment (op, op_location, lhs, rhs)
12055 int op, op_location;
12056 tree lhs, rhs;
12057{
12058 tree assignment;
12059 /* Build the corresponding binop if we deal with a Compound
12060 Assignment operator. Mark the binop sub-tree as part of a
12061 Compound Assignment expression */
12062 if (op != ASSIGN_TK)
12063 {
12064 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12065 COMPOUND_ASSIGN_P (rhs) = 1;
12066 }
12067 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12068 TREE_SIDE_EFFECTS (assignment) = 1;
12069 EXPR_WFL_LINECOL (assignment) = op_location;
12070 return assignment;
12071}
12072
12073/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12074
15fdcfe9 12075char *
e04a16fb
AG
12076print_int_node (node)
12077 tree node;
12078{
12079 static char buffer [80];
12080 if (TREE_CONSTANT_OVERFLOW (node))
12081 sprintf (buffer, "<overflow>");
12082
12083 if (TREE_INT_CST_HIGH (node) == 0)
12084 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12085 TREE_INT_CST_LOW (node));
12086 else if (TREE_INT_CST_HIGH (node) == -1
12087 && TREE_INT_CST_LOW (node) != 0)
12088 {
12089 buffer [0] = '-';
12090 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12091 -TREE_INT_CST_LOW (node));
12092 }
12093 else
12094 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12095 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12096
12097 return buffer;
12098}
12099
c7303e41
APB
12100\f
12101
12102/* This section of the code handle assignment check with FINAL
12103 variables. */
12104
12105static void
12106reset_static_final_variable_assignment_flag (class)
12107 tree class;
12108{
12109 tree field;
12110 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12111 if (CLASS_FINAL_VARIABLE_P (field))
12112 DECL_FIELD_FINAL_LIIC (field) = 0;
12113}
12114
12115/* Figure whether all final static variable have been initialized. */
12116
12117static void
12118check_static_final_variable_assignment_flag (class)
12119 tree class;
12120{
12121 tree field;
12122
12123 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12124 if (CLASS_FINAL_VARIABLE_P (field)
12125 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12126 parse_error_context
12127 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12128 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12129 IDENTIFIER_POINTER (DECL_NAME (field)));
12130}
12131
12132/* This function marks all final variable locally unassigned. */
12133
12134static void
12135reset_final_variable_local_assignment_flag (class)
12136 tree class;
12137{
12138 tree field;
12139 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12140 if (FINAL_VARIABLE_P (field))
12141 DECL_FIELD_FINAL_LIIC (field) = 0;
12142}
12143
12144/* Figure whether all final variables have beem initialized in MDECL
12145 and mark MDECL accordingly. */
12146
12147static void
12148check_final_variable_local_assignment_flag (class, mdecl)
12149 tree class;
12150 tree mdecl;
12151{
12152 tree field;
12153 int initialized = 0;
12154 int non_initialized = 0;
12155
12156 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12157 return;
12158
12159 /* First find out whether all final variables or no final variable
12160 are initialized in this ctor. We don't take into account final
12161 variable that have been initialized upon declaration. */
12162 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12163 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12164 {
12165 if (DECL_FIELD_FINAL_LIIC (field))
12166 initialized++;
12167 else
12168 non_initialized++;
12169 }
12170
12171 /* There were no non initialized variable and no initialized variable.
12172 This ctor is fine. */
12173 if (!non_initialized && !initialized)
12174 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12175 /* If no variables have been initialized, that fine. We'll check
12176 later whether this ctor calls a constructor which initializes
12177 them. We mark the ctor as not initializing all its finals. */
12178 else if (initialized == 0)
12179 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12180 /* If we have a mixed bag, then we have a problem. We need to report
12181 all the variables we're not initializing. */
12182 else if (initialized && non_initialized)
12183 {
12184 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12185 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12186 if (FIELD_FINAL (field)
12187 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12188 {
12189 parse_error_context
12190 (lookup_cl (mdecl),
12191 "Blank final variable `%s' may not have been initialized in this constructor",
12192 IDENTIFIER_POINTER (DECL_NAME (field)));
12193 DECL_FIELD_FINAL_IERR (field) = 1;
12194 }
12195 }
12196 /* Otherwise we know this ctor is initializing all its final
12197 variable. We mark it so. */
12198 else
12199 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12200}
12201
12202/* This function recurses in a simple what through STMT and stops when
12203 it finds a constructor call. It then verifies that the called
12204 constructor initialized its final properly. Return 1 upon success,
12205 0 or -1 otherwise. */
12206
12207static int
12208check_final_variable_indirect_assignment (stmt)
12209 tree stmt;
12210{
12211 int res;
12212 switch (TREE_CODE (stmt))
12213 {
12214 case EXPR_WITH_FILE_LOCATION:
12215 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12216 case COMPOUND_EXPR:
12217 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12218 if (res)
12219 return res;
12220 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12221 case SAVE_EXPR:
12222 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12223 case CALL_EXPR:
12224 {
12225 tree decl = TREE_OPERAND (stmt, 0);
12226 tree fbody;
12227
12228 if (TREE_CODE (decl) != FUNCTION_DECL)
12229 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12230 if (TREE_CODE (decl) != FUNCTION_DECL)
400500c4 12231 abort ();
c7303e41
APB
12232 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12233 return 1;
12234 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12235 return -1;
12236 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12237 if (fbody == error_mark_node)
12238 return -1;
12239 fbody = BLOCK_EXPR_BODY (fbody);
12240 return check_final_variable_indirect_assignment (fbody);
12241 }
12242 default:
12243 break;
12244 }
12245 return 0;
12246}
12247
12248/* This is the last chance to catch a final variable initialization
12249 problem. This routine will report an error if a final variable was
12250 never (globally) initialized and never reported as not having been
12251 initialized properly. */
12252
12253static void
12254check_final_variable_global_assignment_flag (class)
12255 tree class;
12256{
12257 tree field, mdecl;
12258 int nnctor = 0;
12259
12260 /* We go through all natural ctors and see whether they're
12261 initializing all their final variables or not. */
12262 current_function_decl = NULL_TREE; /* For the error report. */
12263 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12264 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12265 {
12266 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12267 {
12268 /* It doesn't. Maybe it calls a constructor that initializes
12269 them. find out. */
12270 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12271 if (fbody == error_mark_node)
12272 continue;
12273 fbody = BLOCK_EXPR_BODY (fbody);
12274 if (check_final_variable_indirect_assignment (fbody) == 1)
12275 {
12276 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12277 nnctor++;
12278 }
12279 else
12280 parse_error_context
12281 (lookup_cl (mdecl),
12282 "Final variable initialization error in this constructor");
12283 }
12284 else
12285 nnctor++;
12286 }
12287
12288 /* Finally we catch final variables that never were initialized */
12289 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12290 if (FINAL_VARIABLE_P (field)
12291 /* If the field wasn't initialized upon declaration */
12292 && !DECL_FIELD_FINAL_IUD (field)
12293 /* There wasn't natural ctor in which the field could have been
12294 initialized */
12295 && !nnctor
12296 /* If we never reported a problem with this field */
12297 && !DECL_FIELD_FINAL_IERR (field))
12298 {
12299 current_function_decl = NULL;
12300 parse_error_context
12301 (DECL_FIELD_FINAL_WFL (field),
12302 "Final variable `%s' hasn't been initialized upon its declaration",
12303 IDENTIFIER_POINTER (DECL_NAME (field)));
12304 }
12305
12306}
12307
7f1d4866
APB
12308/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12309 context. */
5e942c50
APB
12310
12311static int
12312check_final_assignment (lvalue, wfl)
12313 tree lvalue, wfl;
12314{
c7303e41
APB
12315 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12316 return 0;
12317
12318 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12319 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12320 lvalue = TREE_OPERAND (lvalue, 1);
12321
c7303e41
APB
12322 if (!FIELD_FINAL (lvalue))
12323 return 0;
12324
12325 /* Now the logic. We can modify a final VARIABLE:
12326 1) in finit$, (its declaration was followed by an initialization,)
12327 2) consistently in each natural ctor, if it wasn't initialized in
12328 finit$ or once in <clinit>. In any other cases, an error should be
12329 reported. */
12330 if (DECL_FINIT_P (current_function_decl))
5e942c50 12331 {
c7303e41
APB
12332 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12333 return 0;
5e942c50 12334 }
c7303e41
APB
12335
12336 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12337 /* Only if it wasn't given a value upon initialization */
12338 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12339 /* If it was never assigned a value in this constructor */
12340 && !DECL_FIELD_FINAL_LIIC (lvalue))
12341 {
12342 /* Turn the locally assigned flag on, it will be checked later
12343 on to point out at discrepancies. */
12344 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12345 if (DECL_CLINIT_P (current_function_decl))
12346 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12347 return 0;
12348 }
12349
12350 /* Other problems should be reported right away. */
12351 parse_error_context
12352 (wfl, "Can't %sassign a value to the final variable `%s'",
12353 (FIELD_STATIC (lvalue) ? "re" : ""),
12354 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12355
12356 /* Note that static field can be initialized once and only once. */
12357 if (FIELD_STATIC (lvalue))
12358 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12359
12360 return 1;
5e942c50
APB
12361}
12362
12363/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12364 read. This is needed to avoid circularities in the implementation
12365 of these fields in libjava. */
12366
12367static tree
12368maybe_build_primttype_type_ref (rhs, wfl)
12369 tree rhs, wfl;
12370{
12371 tree to_return = NULL_TREE;
12372 tree rhs_type = TREE_TYPE (rhs);
12373 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12374 {
12375 tree n = TREE_OPERAND (rhs, 1);
12376 if (TREE_CODE (n) == VAR_DECL
12377 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12378 && rhs_type == class_ptr_type
12379 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12380 {
49f48c71 12381 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12382 if (!strncmp (self_name, "java.lang.", 10))
12383 to_return = build_primtype_type_ref (self_name);
12384 }
12385 }
12386 return (to_return ? to_return : rhs );
12387}
12388
e04a16fb
AG
12389/* 15.25 Assignment operators. */
12390
12391static tree
12392patch_assignment (node, wfl_op1, wfl_op2)
12393 tree node;
12394 tree wfl_op1;
12395 tree wfl_op2;
12396{
0a2138e2 12397 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12398 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12399 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12400 int error_found = 0;
12401 int lvalue_from_array = 0;
12402
c2952b01 12403 /* Can't assign to a (blank) final. */
5e942c50
APB
12404 if (check_final_assignment (lvalue, wfl_op1))
12405 error_found = 1;
e04a16fb
AG
12406
12407 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12408
12409 /* Lhs can be a named variable */
34f4db93 12410 if (JDECL_P (lvalue))
e04a16fb 12411 {
e04a16fb
AG
12412 lhs_type = TREE_TYPE (lvalue);
12413 }
12414 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12415 comment on reason why */
12416 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12417 {
12418 lhs_type = TREE_TYPE (lvalue);
12419 lvalue_from_array = 1;
12420 }
12421 /* Or a field access */
12422 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12423 lhs_type = TREE_TYPE (lvalue);
12424 /* Or a function return slot */
12425 else if (TREE_CODE (lvalue) == RESULT_DECL)
12426 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12427 /* Otherwise, we might want to try to write into an optimized static
12428 final, this is an of a different nature, reported further on. */
12429 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12430 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12431 {
6632dcdd 12432 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12433 {
12434 /* What we should do instead is resetting the all the flags
12435 previously set, exchange lvalue for llvalue and continue. */
12436 error_found = 1;
12437 return error_mark_node;
12438 }
12439 else
12440 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12441 }
12442 else
e04a16fb
AG
12443 {
12444 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12445 error_found = 1;
12446 }
12447
12448 rhs_type = TREE_TYPE (rhs);
b67d701b 12449 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12450 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12451
b67d701b 12452 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12453 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12454 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12455
12456 /* 15.25.2 If we have a compound assignment, convert RHS into the
12457 type of the LHS */
12458 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12459 new_rhs = convert (lhs_type, rhs);
12460
12461 /* Explicit cast required. This is an error */
12462 if (!new_rhs)
12463 {
c2e3db92
KG
12464 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12465 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12466 tree wfl;
12467 char operation [32]; /* Max size known */
12468
12469 /* If the assignment is part of a declaration, we use the WFL of
12470 the declared variable to point out the error and call it a
12471 declaration problem. If the assignment is a genuine =
12472 operator, we call is a operator `=' problem, otherwise we
12473 call it an assignment problem. In both of these last cases,
12474 we use the WFL of the operator to indicate the error. */
12475
12476 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12477 {
12478 wfl = wfl_op1;
12479 strcpy (operation, "declaration");
12480 }
12481 else
12482 {
12483 wfl = wfl_operator;
12484 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12485 strcpy (operation, "assignment");
12486 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12487 strcpy (operation, "`return'");
12488 else
12489 strcpy (operation, "`='");
12490 }
12491
1ebadc60 12492 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12493 parse_error_context
12494 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12495 operation, t1, t2);
1ebadc60 12496 else
781b0558 12497 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12498 operation, t1, t2);
e04a16fb
AG
12499 free (t1); free (t2);
12500 error_found = 1;
12501 }
12502
c877974e
APB
12503 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12504 if (new_rhs)
12505 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12506
e04a16fb
AG
12507 if (error_found)
12508 return error_mark_node;
12509
2622b947
APB
12510 /* 10.10: Array Store Exception runtime check */
12511 if (!flag_emit_class_files
e8fc7396 12512 && !flag_emit_xref
2622b947 12513 && lvalue_from_array
afc390b1 12514 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12515 {
12516 tree check;
12517 tree base = lvalue;
12518
12519 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12520 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12521 base = TREE_OPERAND (lvalue, 0);
12522 else
12523 {
12524 if (flag_bounds_check)
12525 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12526 else
12527 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12528 }
12529
12530 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12531 new_rhs = save_expr (new_rhs);
2622b947
APB
12532 check = build (CALL_EXPR, void_type_node,
12533 build_address_of (soft_checkarraystore_node),
12534 tree_cons (NULL_TREE, base,
12535 build_tree_list (NULL_TREE, new_rhs)),
12536 NULL_TREE);
12537 TREE_SIDE_EFFECTS (check) = 1;
12538
12539 /* We have to decide on an insertion point */
12540 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12541 {
12542 tree t;
12543 if (flag_bounds_check)
12544 {
12545 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12546 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12547 build (COMPOUND_EXPR, void_type_node, t, check);
12548 }
12549 else
12550 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12551 check, TREE_OPERAND (lvalue, 1));
12552 }
12553 else
12554 {
12555 /* Make sure the bound check will happen before the store check */
12556 if (flag_bounds_check)
12557 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12558 build (COMPOUND_EXPR, void_type_node,
12559 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12560 else
12561 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12562 }
12563 }
22eed1e6 12564
34d4df06
APB
12565 /* Final locals can be used as case values in switch
12566 statement. Prepare them for this eventuality. */
12567 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12568 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12569 && TREE_CONSTANT (new_rhs)
12570 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12571 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12572 )
12573 {
12574 TREE_CONSTANT (lvalue) = 1;
12575 DECL_INITIAL (lvalue) = new_rhs;
12576 }
12577
e04a16fb
AG
12578 TREE_OPERAND (node, 0) = lvalue;
12579 TREE_OPERAND (node, 1) = new_rhs;
12580 TREE_TYPE (node) = lhs_type;
12581 return node;
12582}
12583
b67d701b
PB
12584/* Check that type SOURCE can be cast into type DEST. If the cast
12585 can't occur at all, return 0 otherwise 1. This function is used to
12586 produce accurate error messages on the reasons why an assignment
12587 failed. */
e04a16fb 12588
b67d701b
PB
12589static tree
12590try_reference_assignconv (lhs_type, rhs)
12591 tree lhs_type, rhs;
e04a16fb 12592{
b67d701b
PB
12593 tree new_rhs = NULL_TREE;
12594 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12595
b67d701b
PB
12596 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12597 {
12598 /* `null' may be assigned to any reference type */
12599 if (rhs == null_pointer_node)
12600 new_rhs = null_pointer_node;
12601 /* Try the reference assignment conversion */
12602 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12603 new_rhs = rhs;
12604 /* This is a magic assignment that we process differently */
12605 else if (rhs == soft_exceptioninfo_call_node)
12606 new_rhs = rhs;
12607 }
12608 return new_rhs;
12609}
12610
12611/* Check that RHS can be converted into LHS_TYPE by the assignment
12612 conversion (5.2), for the cases of RHS being a builtin type. Return
12613 NULL_TREE if the conversion fails or if because RHS isn't of a
12614 builtin type. Return a converted RHS if the conversion is possible. */
12615
12616static tree
12617try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12618 tree wfl_op1, lhs_type, rhs;
12619{
12620 tree new_rhs = NULL_TREE;
12621 tree rhs_type = TREE_TYPE (rhs);
12622
7e51098e
TT
12623 /* Handle boolean specially. */
12624 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12625 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12626 {
12627 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12628 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12629 new_rhs = rhs;
12630 }
12631
5e942c50 12632 /* Zero accepted everywhere */
7e51098e 12633 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12634 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12635 && JPRIMITIVE_TYPE_P (rhs_type))
12636 new_rhs = convert (lhs_type, rhs);
12637
b67d701b
PB
12638 /* 5.1.1 Try Identity Conversion,
12639 5.1.2 Try Widening Primitive Conversion */
5e942c50 12640 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12641 new_rhs = convert (lhs_type, rhs);
12642
12643 /* Try a narrowing primitive conversion (5.1.3):
12644 - expression is a constant expression of type int AND
12645 - variable is byte, short or char AND
12646 - The value of the expression is representable in the type of the
12647 variable */
12648 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12649 && (lhs_type == byte_type_node || lhs_type == char_type_node
12650 || lhs_type == short_type_node))
12651 {
12652 if (int_fits_type_p (rhs, lhs_type))
12653 new_rhs = convert (lhs_type, rhs);
12654 else if (wfl_op1) /* Might be called with a NULL */
12655 parse_warning_context
7e51098e 12656 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12657 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12658 /* Reported a warning that will turn into an error further
12659 down, so we don't return */
12660 }
12661
12662 return new_rhs;
12663}
12664
12665/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12666 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12667 0 is the conversion test fails. This implements parts the method
12668 invocation convertion (5.3). */
12669
12670static int
12671valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12672 tree lhs_type, rhs_type;
12673{
acd663ee 12674 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12675 if (lhs_type == rhs_type)
12676 return 1;
12677
7e51098e
TT
12678 /* Reject non primitive types and boolean conversions. */
12679 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12680 return 0;
12681
acd663ee
APB
12682 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12683 than a char can't be converted into a char. Short can't too, but
12684 the < test below takes care of that */
b67d701b
PB
12685 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12686 return 0;
12687
5e942c50
APB
12688 /* Accept all promoted type here. Note, we can't use <= in the test
12689 below, because we still need to bounce out assignments of short
12690 to char and the likes */
12691 if (lhs_type == int_type_node
12692 && (rhs_type == promoted_byte_type_node
12693 || rhs_type == promoted_short_type_node
12694 || rhs_type == promoted_char_type_node
12695 || rhs_type == promoted_boolean_type_node))
12696 return 1;
12697
acd663ee
APB
12698 /* From here, an integral is widened if its precision is smaller
12699 than the precision of the LHS or if the LHS is a floating point
12700 type, or the RHS is a float and the RHS a double. */
12701 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12702 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12703 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12704 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12705 return 1;
12706
12707 return 0;
e04a16fb
AG
12708}
12709
12710/* Check that something of SOURCE type can be assigned or cast to
12711 something of DEST type at runtime. Return 1 if the operation is
12712 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12713 were SOURCE is cast into DEST, which borrows a lot of the
12714 assignment check. */
12715
12716static int
12717valid_ref_assignconv_cast_p (source, dest, cast)
12718 tree source;
12719 tree dest;
12720 int cast;
12721{
09ed0f70
APB
12722 /* SOURCE or DEST might be null if not from a declared entity. */
12723 if (!source || !dest)
12724 return 0;
5e942c50
APB
12725 if (JNULLP_TYPE_P (source))
12726 return 1;
e04a16fb
AG
12727 if (TREE_CODE (source) == POINTER_TYPE)
12728 source = TREE_TYPE (source);
12729 if (TREE_CODE (dest) == POINTER_TYPE)
12730 dest = TREE_TYPE (dest);
c1eacb70
BM
12731
12732 /* If source and dest are being compiled from bytecode, they may need to
12733 be loaded. */
12734 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12735 {
12736 load_class (source, 1);
12737 safe_layout_class (source);
12738 }
12739 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12740 {
12741 load_class (dest, 1);
12742 safe_layout_class (dest);
12743 }
12744
e04a16fb
AG
12745 /* Case where SOURCE is a class type */
12746 if (TYPE_CLASS_P (source))
12747 {
12748 if (TYPE_CLASS_P (dest))
c2952b01
APB
12749 return (source == dest
12750 || inherits_from_p (source, dest)
c2952b01 12751 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12752 if (TYPE_INTERFACE_P (dest))
12753 {
12754 /* If doing a cast and SOURCE is final, the operation is
12755 always correct a compile time (because even if SOURCE
12756 does not implement DEST, a subclass of SOURCE might). */
12757 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12758 return 1;
12759 /* Otherwise, SOURCE must implement DEST */
12760 return interface_of_p (dest, source);
12761 }
12762 /* DEST is an array, cast permited if SOURCE is of Object type */
12763 return (cast && source == object_type_node ? 1 : 0);
12764 }
12765 if (TYPE_INTERFACE_P (source))
12766 {
12767 if (TYPE_CLASS_P (dest))
12768 {
12769 /* If not casting, DEST must be the Object type */
12770 if (!cast)
12771 return dest == object_type_node;
12772 /* We're doing a cast. The cast is always valid is class
12773 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12774 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12775 return 1;
12776 else
12777 return interface_of_p (source, dest);
12778 }
12779 if (TYPE_INTERFACE_P (dest))
12780 {
12781 /* If doing a cast, then if SOURCE and DEST contain method
12782 with the same signature but different return type, then
12783 this is a (compile time) error */
12784 if (cast)
12785 {
12786 tree method_source, method_dest;
12787 tree source_type;
0a2138e2 12788 tree source_sig;
e04a16fb
AG
12789 tree source_name;
12790 for (method_source = TYPE_METHODS (source); method_source;
12791 method_source = TREE_CHAIN (method_source))
12792 {
12793 source_sig =
12794 build_java_argument_signature (TREE_TYPE (method_source));
12795 source_type = TREE_TYPE (TREE_TYPE (method_source));
12796 source_name = DECL_NAME (method_source);
12797 for (method_dest = TYPE_METHODS (dest);
12798 method_dest; method_dest = TREE_CHAIN (method_dest))
12799 if (source_sig ==
12800 build_java_argument_signature (TREE_TYPE (method_dest))
12801 && source_name == DECL_NAME (method_dest)
12802 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12803 return 0;
12804 }
12805 return 1;
12806 }
12807 else
12808 return source == dest || interface_of_p (dest, source);
12809 }
ee17a290
TT
12810 else
12811 {
12812 /* Array */
12813 return (cast
12814 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12815 || (DECL_NAME (TYPE_NAME (source))
12816 == java_io_serializable)));
12817 }
e04a16fb
AG
12818 }
12819 if (TYPE_ARRAY_P (source))
12820 {
12821 if (TYPE_CLASS_P (dest))
12822 return dest == object_type_node;
09ed0f70 12823 /* Can't cast an array to an interface unless the interface is
ee17a290 12824 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12825 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12826 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12827 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12828 else /* Arrays */
12829 {
12830 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12831 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12832
b9f7e36c
APB
12833 /* In case of severe errors, they turn out null */
12834 if (!dest_element_type || !source_element_type)
12835 return 0;
e04a16fb
AG
12836 if (source_element_type == dest_element_type)
12837 return 1;
12838 return valid_ref_assignconv_cast_p (source_element_type,
12839 dest_element_type, cast);
12840 }
12841 return 0;
12842 }
12843 return 0;
12844}
12845
b67d701b
PB
12846static int
12847valid_cast_to_p (source, dest)
12848 tree source;
12849 tree dest;
12850{
12851 if (TREE_CODE (source) == POINTER_TYPE)
12852 source = TREE_TYPE (source);
12853 if (TREE_CODE (dest) == POINTER_TYPE)
12854 dest = TREE_TYPE (dest);
12855
12856 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12857 return valid_ref_assignconv_cast_p (source, dest, 1);
12858
12859 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12860 return 1;
12861
7e51098e
TT
12862 else if (TREE_CODE (source) == BOOLEAN_TYPE
12863 && TREE_CODE (dest) == BOOLEAN_TYPE)
12864 return 1;
12865
b67d701b
PB
12866 return 0;
12867}
12868
15fdcfe9
PB
12869static tree
12870do_unary_numeric_promotion (arg)
12871 tree arg;
12872{
12873 tree type = TREE_TYPE (arg);
7e51098e
TT
12874 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12875 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12876 arg = convert (int_type_node, arg);
12877 return arg;
12878}
12879
acd663ee
APB
12880/* Return a non zero value if SOURCE can be converted into DEST using
12881 the method invocation conversion rule (5.3). */
b67d701b
PB
12882static int
12883valid_method_invocation_conversion_p (dest, source)
12884 tree dest, source;
12885{
e3884b71 12886 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12887 && valid_builtin_assignconv_identity_widening_p (dest, source))
12888 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12889 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12890 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12891}
12892
e04a16fb
AG
12893/* Build an incomplete binop expression. */
12894
12895static tree
12896build_binop (op, op_location, op1, op2)
12897 enum tree_code op;
12898 int op_location;
12899 tree op1, op2;
12900{
5e942c50 12901 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12902 TREE_SIDE_EFFECTS (binop) = 1;
12903 /* Store the location of the operator, for better error report. The
12904 string of the operator will be rebuild based on the OP value. */
12905 EXPR_WFL_LINECOL (binop) = op_location;
12906 return binop;
12907}
12908
12909/* Build the string of the operator retained by NODE. If NODE is part
12910 of a compound expression, add an '=' at the end of the string. This
12911 function is called when an error needs to be reported on an
12912 operator. The string is returned as a pointer to a static character
12913 buffer. */
12914
12915static char *
12916operator_string (node)
12917 tree node;
12918{
12919#define BUILD_OPERATOR_STRING(S) \
12920 { \
12921 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12922 return buffer; \
12923 }
12924
12925 static char buffer [10];
12926 switch (TREE_CODE (node))
12927 {
12928 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12929 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12930 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12931 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12932 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12933 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12934 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12935 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12936 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12937 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12938 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12939 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12940 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12941 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12942 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12943 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12944 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12945 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12946 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12947 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12948 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12949 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12950 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12951 case PREINCREMENT_EXPR: /* Fall through */
12952 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12953 case PREDECREMENT_EXPR: /* Fall through */
12954 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12955 default:
400500c4
RK
12956 internal_error ("unregistered operator %s",
12957 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
12958 }
12959 return NULL;
12960#undef BUILD_OPERATOR_STRING
12961}
12962
5cbdba64
APB
12963/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12964
12965static int
12966java_decl_equiv (var_acc1, var_acc2)
12967 tree var_acc1, var_acc2;
12968{
12969 if (JDECL_P (var_acc1))
12970 return (var_acc1 == var_acc2);
12971
12972 return (TREE_CODE (var_acc1) == COMPONENT_REF
12973 && TREE_CODE (var_acc2) == COMPONENT_REF
12974 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12975 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12976 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12977}
12978
12979/* Return a non zero value if CODE is one of the operators that can be
12980 used in conjunction with the `=' operator in a compound assignment. */
12981
12982static int
12983binop_compound_p (code)
12984 enum tree_code code;
12985{
12986 int i;
12987 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12988 if (binop_lookup [i] == code)
12989 break;
12990
12991 return i < BINOP_COMPOUND_CANDIDATES;
12992}
12993
12994/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12995
12996static tree
12997java_refold (t)
12998 tree t;
12999{
13000 tree c, b, ns, decl;
13001
13002 if (TREE_CODE (t) != MODIFY_EXPR)
13003 return t;
13004
13005 c = TREE_OPERAND (t, 1);
13006 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13007 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13008 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13009 return t;
13010
13011 /* Now the left branch of the binary operator. */
13012 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13013 if (! (b && TREE_CODE (b) == NOP_EXPR
13014 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13015 return t;
13016
13017 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13018 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13019 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13020 return t;
13021
13022 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13023 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13024 /* It's got to be the an equivalent decl */
13025 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13026 {
13027 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13028 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13029 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13030 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13031 /* Change the right part of the BINOP_EXPR */
13032 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13033 }
13034
13035 return t;
13036}
13037
e04a16fb
AG
13038/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13039 errors but we modify NODE so that it contains the type computed
13040 according to the expression, when it's fixed. Otherwise, we write
13041 error_mark_node as the type. It allows us to further the analysis
13042 of remaining nodes and detects more errors in certain cases. */
13043
13044static tree
13045patch_binop (node, wfl_op1, wfl_op2)
13046 tree node;
13047 tree wfl_op1;
13048 tree wfl_op2;
13049{
13050 tree op1 = TREE_OPERAND (node, 0);
13051 tree op2 = TREE_OPERAND (node, 1);
13052 tree op1_type = TREE_TYPE (op1);
13053 tree op2_type = TREE_TYPE (op2);
48a840d9 13054 tree prom_type = NULL_TREE, cn;
e04a16fb 13055 int code = TREE_CODE (node);
b67d701b 13056
e04a16fb
AG
13057 /* If 1, tell the routine that we have to return error_mark_node
13058 after checking for the initialization of the RHS */
13059 int error_found = 0;
13060
e04a16fb
AG
13061 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13062
761491c8
APB
13063 /* If either op<n>_type are NULL, this might be early signs of an
13064 error situation, unless it's too early to tell (in case we're
13065 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13066 correctly so the error can be later on reported accurately. */
13067 if (! (code == PLUS_EXPR || code == NE_EXPR
13068 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13069 {
13070 tree n;
13071 if (! op1_type)
13072 {
13073 n = java_complete_tree (op1);
13074 op1_type = TREE_TYPE (n);
13075 }
13076 if (! op2_type)
13077 {
13078 n = java_complete_tree (op2);
13079 op2_type = TREE_TYPE (n);
13080 }
13081 }
13082
e04a16fb
AG
13083 switch (code)
13084 {
13085 /* 15.16 Multiplicative operators */
13086 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13087 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13088 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13089 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13090 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13091 {
7e51098e 13092 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13093 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13094 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13095 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13096 TREE_TYPE (node) = error_mark_node;
13097 error_found = 1;
13098 break;
13099 }
13100 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13101 /* Change the division operator if necessary */
13102 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13103 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13104
aa4759c1
AH
13105 if (TREE_CODE (prom_type) == INTEGER_TYPE
13106 && flag_use_divide_subroutine
13107 && ! flag_emit_class_files
13108 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13109 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13110
0b4d333e
APB
13111 /* This one is more complicated. FLOATs are processed by a
13112 function call to soft_fmod. Duplicate the value of the
13113 COMPOUND_ASSIGN_P flag. */
e04a16fb 13114 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13115 {
13116 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13117 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13118 TREE_SIDE_EFFECTS (mod)
13119 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13120 return mod;
13121 }
e04a16fb
AG
13122 break;
13123
13124 /* 15.17 Additive Operators */
13125 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13126
13127 /* Operation is valid if either one argument is a string
13128 constant, a String object or a StringBuffer crafted for the
13129 purpose of the a previous usage of the String concatenation
13130 operator */
13131
13132 if (TREE_CODE (op1) == STRING_CST
13133 || TREE_CODE (op2) == STRING_CST
13134 || JSTRING_TYPE_P (op1_type)
13135 || JSTRING_TYPE_P (op2_type)
13136 || IS_CRAFTED_STRING_BUFFER_P (op1)
13137 || IS_CRAFTED_STRING_BUFFER_P (op2))
13138 return build_string_concatenation (op1, op2);
13139
e04a16fb
AG
13140 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13141 Numeric Types */
7e51098e 13142 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13143 {
7e51098e 13144 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13145 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13146 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13147 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13148 TREE_TYPE (node) = error_mark_node;
13149 error_found = 1;
13150 break;
13151 }
13152 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13153 break;
13154
13155 /* 15.18 Shift Operators */
13156 case LSHIFT_EXPR:
13157 case RSHIFT_EXPR:
13158 case URSHIFT_EXPR:
13159 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13160 {
13161 if (!JINTEGRAL_TYPE_P (op1_type))
13162 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13163 else
1ebadc60 13164 {
7e51098e 13165 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13166 parse_error_context (wfl_operator,
781b0558 13167 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13168 operator_string (node),
13169 lang_printable_name (op2_type, 0));
13170 else
781b0558
KG
13171 parse_error_context (wfl_operator,
13172 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13173 operator_string (node),
13174 lang_printable_name (op2_type, 0));
13175 }
e04a16fb
AG
13176 TREE_TYPE (node) = error_mark_node;
13177 error_found = 1;
13178 break;
13179 }
13180
13181 /* Unary numeric promotion (5.6.1) is performed on each operand
13182 separatly */
15fdcfe9
PB
13183 op1 = do_unary_numeric_promotion (op1);
13184 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13185
13186 /* The type of the shift expression is the type of the promoted
13187 type of the left-hand operand */
13188 prom_type = TREE_TYPE (op1);
13189
c2952b01
APB
13190 /* Shift int only up to 0x1f and long up to 0x3f */
13191 if (prom_type == int_type_node)
13192 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13193 build_int_2 (0x1f, 0)));
13194 else
13195 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13196 build_int_2 (0x3f, 0)));
e04a16fb
AG
13197
13198 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13199 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13200 {
0b4d333e 13201 tree to_return;
73333a87
AH
13202 tree utype = unsigned_type (prom_type);
13203 op1 = convert (utype, op1);
e04a16fb 13204 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13205 TREE_OPERAND (node, 0) = op1;
13206 TREE_OPERAND (node, 1) = op2;
13207 TREE_TYPE (node) = utype;
0b4d333e
APB
13208 to_return = convert (prom_type, node);
13209 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13210 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13211 TREE_SIDE_EFFECTS (to_return)
13212 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13213 return to_return;
e04a16fb
AG
13214 }
13215 break;
5e942c50
APB
13216
13217 /* 15.19.1 Type Comparison Operator instaceof */
13218 case INSTANCEOF_EXPR:
13219
13220 TREE_TYPE (node) = boolean_type_node;
13221
13222 if (!(op2_type = resolve_type_during_patch (op2)))
13223 return error_mark_node;
13224
13225 /* The first operand must be a reference type or the null type */
13226 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13227 error_found = 1; /* Error reported further below */
13228
13229 /* The second operand must be a reference type */
13230 if (!JREFERENCE_TYPE_P (op2_type))
13231 {
13232 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13233 parse_error_context
13234 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13235 lang_printable_name (op2_type, 0));
13236 error_found = 1;
13237 }
13238
13239 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13240 {
13241 /* If the first operand is null, the result is always false */
13242 if (op1 == null_pointer_node)
13243 return boolean_false_node;
15fdcfe9
PB
13244 else if (flag_emit_class_files)
13245 {
13246 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13247 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13248 return node;
13249 }
5e942c50
APB
13250 /* Otherwise we have to invoke instance of to figure it out */
13251 else
67db0ce7 13252 return build_instanceof (op1, op2_type);
5e942c50
APB
13253 }
13254 /* There is no way the expression operand can be an instance of
13255 the type operand. This is a compile time error. */
13256 else
13257 {
c2e3db92 13258 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13259 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13260 parse_error_context
13261 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13262 t1, lang_printable_name (op2_type, 0));
13263 free (t1);
13264 error_found = 1;
13265 }
e04a16fb 13266
5e942c50 13267 break;
e04a16fb
AG
13268
13269 /* 15.21 Bitwise and Logical Operators */
13270 case BIT_AND_EXPR:
13271 case BIT_XOR_EXPR:
13272 case BIT_IOR_EXPR:
13273 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13274 /* Binary numeric promotion is performed on both operand and the
13275 expression retain that type */
13276 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13277
13278 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13279 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13280 /* The type of the bitwise operator expression is BOOLEAN */
13281 prom_type = boolean_type_node;
13282 else
13283 {
13284 if (!JINTEGRAL_TYPE_P (op1_type))
13285 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13286 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13287 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13288 TREE_TYPE (node) = error_mark_node;
13289 error_found = 1;
13290 /* Insert a break here if adding thing before the switch's
13291 break for this case */
13292 }
13293 break;
13294
13295 /* 15.22 Conditional-And Operator */
13296 case TRUTH_ANDIF_EXPR:
13297 /* 15.23 Conditional-Or Operator */
13298 case TRUTH_ORIF_EXPR:
13299 /* Operands must be of BOOLEAN type */
13300 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13301 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13302 {
13303 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13304 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13305 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13306 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13307 TREE_TYPE (node) = boolean_type_node;
13308 error_found = 1;
13309 break;
13310 }
13311 /* The type of the conditional operators is BOOLEAN */
13312 prom_type = boolean_type_node;
13313 break;
13314
13315 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13316 case LT_EXPR:
13317 case GT_EXPR:
13318 case LE_EXPR:
13319 case GE_EXPR:
13320 /* The type of each of the operands must be a primitive numeric
13321 type */
13322 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13323 {
13324 if (!JNUMERIC_TYPE_P (op1_type))
13325 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13326 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13327 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13328 TREE_TYPE (node) = boolean_type_node;
13329 error_found = 1;
13330 break;
13331 }
13332 /* Binary numeric promotion is performed on the operands */
13333 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13334 /* The type of the relation expression is always BOOLEAN */
13335 prom_type = boolean_type_node;
13336 break;
13337
13338 /* 15.20 Equality Operator */
13339 case EQ_EXPR:
13340 case NE_EXPR:
48a840d9
APB
13341 /* It's time for us to patch the strings. */
13342 if ((cn = patch_string (op1)))
13343 {
13344 op1 = cn;
13345 op1_type = TREE_TYPE (op1);
13346 }
13347 if ((cn = patch_string (op2)))
13348 {
13349 op2 = cn;
13350 op2_type = TREE_TYPE (op2);
13351 }
13352
e04a16fb
AG
13353 /* 15.20.1 Numerical Equality Operators == and != */
13354 /* Binary numeric promotion is performed on the operands */
5e942c50 13355 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13356 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13357
13358 /* 15.20.2 Boolean Equality Operators == and != */
13359 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13360 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13361 ; /* Nothing to do here */
13362
13363 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13364 /* Types have to be either references or the null type. If
13365 they're references, it must be possible to convert either
13366 type to the other by casting conversion. */
b9f7e36c
APB
13367 else if (op1 == null_pointer_node || op2 == null_pointer_node
13368 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13369 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13370 || valid_ref_assignconv_cast_p (op2_type,
13371 op1_type, 1))))
e04a16fb
AG
13372 ; /* Nothing to do here */
13373
13374 /* Else we have an error figure what can't be converted into
13375 what and report the error */
13376 else
13377 {
13378 char *t1;
c2e3db92 13379 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13380 parse_error_context
781b0558
KG
13381 (wfl_operator,
13382 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13383 operator_string (node), t1,
0a2138e2 13384 lang_printable_name (op2_type, 0));
e04a16fb
AG
13385 free (t1);
13386 TREE_TYPE (node) = boolean_type_node;
13387 error_found = 1;
13388 break;
13389 }
13390 prom_type = boolean_type_node;
13391 break;
13392 }
13393
e04a16fb
AG
13394 if (error_found)
13395 return error_mark_node;
13396
13397 TREE_OPERAND (node, 0) = op1;
13398 TREE_OPERAND (node, 1) = op2;
13399 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13400 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13401
ce6e9147
APB
13402 if (flag_emit_xref)
13403 return node;
13404
d1472141
PB
13405 /* fold does not respect side-effect order as required for Java but not C.
13406 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13407 * bytecode.
13408 */
13409 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13410 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13411 node = fold (node);
13412 return node;
e04a16fb
AG
13413}
13414
b67d701b
PB
13415/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13416 zero value, the value of CSTE comes after the valude of STRING */
13417
13418static tree
13419do_merge_string_cste (cste, string, string_len, after)
13420 tree cste;
49f48c71 13421 const char *string;
b67d701b
PB
13422 int string_len, after;
13423{
49f48c71 13424 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13425 int old_len = TREE_STRING_LENGTH (cste);
13426 int len = old_len + string_len;
520a57c8 13427 char *new = alloca (len+1);
354e99ce 13428
b67d701b
PB
13429 if (after)
13430 {
354e99ce
APB
13431 memcpy (new, string, string_len);
13432 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13433 }
13434 else
13435 {
354e99ce
APB
13436 memcpy (new, old, old_len);
13437 memcpy (&new [old_len], string, string_len);
b67d701b 13438 }
354e99ce 13439 new [len] = '\0';
520a57c8 13440 return build_string (len, new);
b67d701b
PB
13441}
13442
13443/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13444 new STRING_CST on success, NULL_TREE on failure */
13445
13446static tree
13447merge_string_cste (op1, op2, after)
13448 tree op1, op2;
13449 int after;
13450{
13451 /* Handle two string constants right away */
13452 if (TREE_CODE (op2) == STRING_CST)
13453 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13454 TREE_STRING_LENGTH (op2), after);
13455
13456 /* Reasonable integer constant can be treated right away */
13457 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13458 {
49f48c71
KG
13459 static const char *boolean_true = "true";
13460 static const char *boolean_false = "false";
13461 static const char *null_pointer = "null";
b67d701b 13462 char ch[3];
49f48c71 13463 const char *string;
b67d701b
PB
13464
13465 if (op2 == boolean_true_node)
13466 string = boolean_true;
13467 else if (op2 == boolean_false_node)
13468 string = boolean_false;
13469 else if (op2 == null_pointer_node)
13470 string = null_pointer;
13471 else if (TREE_TYPE (op2) == char_type_node)
13472 {
13473 ch[0] = (char )TREE_INT_CST_LOW (op2);
13474 ch[1] = '\0';
13475 string = ch;
13476 }
13477 else
13478 string = print_int_node (op2);
13479
13480 return do_merge_string_cste (op1, string, strlen (string), after);
13481 }
13482 return NULL_TREE;
13483}
13484
13485/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13486 has to be a STRING_CST and the other part must be a STRING_CST or a
13487 INTEGRAL constant. Return a new STRING_CST if the operation
13488 succeed, NULL_TREE otherwise.
13489
13490 If the case we want to optimize for space, we might want to return
13491 NULL_TREE for each invocation of this routine. FIXME */
13492
13493static tree
13494string_constant_concatenation (op1, op2)
13495 tree op1, op2;
13496{
13497 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13498 {
0a2138e2 13499 tree string, rest;
b67d701b
PB
13500 int invert;
13501
13502 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13503 rest = (string == op1 ? op2 : op1);
13504 invert = (string == op1 ? 0 : 1 );
13505
13506 /* Walk REST, only if it looks reasonable */
13507 if (TREE_CODE (rest) != STRING_CST
13508 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13509 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13510 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13511 {
13512 rest = java_complete_tree (rest);
13513 if (rest == error_mark_node)
13514 return error_mark_node;
13515 rest = fold (rest);
13516 }
13517 return merge_string_cste (string, rest, invert);
13518 }
13519 return NULL_TREE;
13520}
13521
13522/* Implement the `+' operator. Does static optimization if possible,
13523 otherwise create (if necessary) and append elements to a
13524 StringBuffer. The StringBuffer will be carried around until it is
13525 used for a function call or an assignment. Then toString() will be
13526 called on it to turn it into a String object. */
13527
13528static tree
13529build_string_concatenation (op1, op2)
13530 tree op1, op2;
13531{
13532 tree result;
dc0b3eff 13533 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13534
13535 if (flag_emit_xref)
13536 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13537
13538 /* Try to do some static optimization */
13539 if ((result = string_constant_concatenation (op1, op2)))
13540 return result;
13541
c0d87ff6
PB
13542 /* Discard empty strings on either side of the expression */
13543 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13544 {
13545 op1 = op2;
13546 op2 = NULL_TREE;
13547 }
c0d87ff6 13548 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13549 op2 = NULL_TREE;
b67d701b 13550
acd663ee 13551 /* If operands are string constant, turn then into object references */
b67d701b
PB
13552 if (TREE_CODE (op1) == STRING_CST)
13553 op1 = patch_string_cst (op1);
acd663ee 13554 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13555 op2 = patch_string_cst (op2);
13556
acd663ee
APB
13557 /* If either one of the constant is null and the other non null
13558 operand is a String object, return it. */
13559 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13560 return op1;
13561
b67d701b
PB
13562 /* If OP1 isn't already a StringBuffer, create and
13563 initialize a new one */
13564 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13565 {
13566 /* Two solutions here:
c52b5771
AG
13567 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13568 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13569 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13570 op1 = BUILD_STRING_BUFFER (op1);
13571 else
13572 {
13573 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13574 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13575 }
13576 }
13577
acd663ee
APB
13578 if (op2)
13579 {
13580 /* OP1 is no longer the last node holding a crafted StringBuffer */
13581 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13582 /* Create a node for `{new...,xxx}.append (op2)' */
13583 if (op2)
13584 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13585 }
13586
b67d701b
PB
13587 /* Mark the last node holding a crafted StringBuffer */
13588 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13589
13590 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13591 return op1;
13592}
13593
13594/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13595 StringBuffer. If no string were found to be patched, return
13596 NULL. */
13597
13598static tree
13599patch_string (node)
13600 tree node;
13601{
1179ebc2
APB
13602 if (node == error_mark_node)
13603 return error_mark_node;
b67d701b
PB
13604 if (TREE_CODE (node) == STRING_CST)
13605 return patch_string_cst (node);
13606 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13607 {
c877974e 13608 int saved = ctxp->explicit_constructor_p;
b67d701b 13609 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13610 tree ret;
13611 /* Temporary disable forbid the use of `this'. */
13612 ctxp->explicit_constructor_p = 0;
13613 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13614 /* String concatenation arguments must be evaluated in order too. */
13615 ret = force_evaluation_order (ret);
c877974e
APB
13616 /* Restore it at its previous value */
13617 ctxp->explicit_constructor_p = saved;
13618 return ret;
b67d701b
PB
13619 }
13620 return NULL_TREE;
13621}
13622
13623/* Build the internal representation of a string constant. */
13624
13625static tree
13626patch_string_cst (node)
13627 tree node;
13628{
13629 int location;
15fdcfe9
PB
13630 if (! flag_emit_class_files)
13631 {
15fdcfe9
PB
13632 node = get_identifier (TREE_STRING_POINTER (node));
13633 location = alloc_name_constant (CONSTANT_String, node);
13634 node = build_ref_from_constant_pool (location);
13635 }
cd9643f7 13636 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13637 TREE_CONSTANT (node) = 1;
13638 return node;
13639}
13640
13641/* Build an incomplete unary operator expression. */
e04a16fb
AG
13642
13643static tree
13644build_unaryop (op_token, op_location, op1)
13645 int op_token, op_location;
13646 tree op1;
13647{
13648 enum tree_code op;
13649 tree unaryop;
13650 switch (op_token)
13651 {
b67d701b 13652 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13653 case MINUS_TK: op = NEGATE_EXPR; break;
13654 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13655 case NOT_TK: op = BIT_NOT_EXPR; break;
400500c4 13656 default: abort ();
e04a16fb
AG
13657 }
13658
13659 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13660 TREE_SIDE_EFFECTS (unaryop) = 1;
13661 /* Store the location of the operator, for better error report. The
13662 string of the operator will be rebuild based on the OP value. */
13663 EXPR_WFL_LINECOL (unaryop) = op_location;
13664 return unaryop;
13665}
13666
13667/* Special case for the ++/-- operators, since they require an extra
13668 argument to build, which is set to NULL and patched
13669 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13670
13671static tree
13672build_incdec (op_token, op_location, op1, is_post_p)
13673 int op_token, op_location;
13674 tree op1;
13675 int is_post_p;
13676{
13677 static enum tree_code lookup [2][2] =
13678 {
13679 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13680 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13681 };
13682 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13683 NULL_TREE, op1, NULL_TREE);
13684 TREE_SIDE_EFFECTS (node) = 1;
13685 /* Store the location of the operator, for better error report. The
13686 string of the operator will be rebuild based on the OP value. */
13687 EXPR_WFL_LINECOL (node) = op_location;
13688 return node;
13689}
13690
13691/* Build an incomplete cast operator, based on the use of the
13692 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13693 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13694 though its type is already set. */
13695
13696static tree
13697build_cast (location, type, exp)
13698 int location;
13699 tree type, exp;
13700{
13701 tree node = build1 (CONVERT_EXPR, type, exp);
13702 EXPR_WFL_LINECOL (node) = location;
13703 return node;
13704}
13705
c2952b01
APB
13706/* Build an incomplete class reference operator. */
13707static tree
13708build_incomplete_class_ref (location, class_name)
13709 int location;
13710 tree class_name;
13711{
13712 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13713 EXPR_WFL_LINECOL (node) = location;
13714 return node;
13715}
13716
13717/* Complete an incomplete class reference operator. */
13718static tree
13719patch_incomplete_class_ref (node)
13720 tree node;
13721{
13722 tree type = TREE_OPERAND (node, 0);
13723 tree ref_type;
13724
13725 if (!(ref_type = resolve_type_during_patch (type)))
13726 return error_mark_node;
13727
165f37bc 13728 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13729 {
13730 /* A class referenced by `foo.class' is initialized. */
13731 return build_class_init (ref_type, build_class_ref (ref_type));
13732 }
165f37bc
APB
13733
13734 /* If we're emitting class files and we have to deal with non
13735 primitive types, we invoke (and consider generating) the
13736 synthetic static method `class$'. */
13737 if (!TYPE_DOT_CLASS (current_class))
13738 build_dot_class_method (current_class);
f0f3a777 13739 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13740 return java_complete_tree (ref_type);
c2952b01
APB
13741}
13742
e04a16fb
AG
13743/* 15.14 Unary operators. We return error_mark_node in case of error,
13744 but preserve the type of NODE if the type is fixed. */
13745
13746static tree
13747patch_unaryop (node, wfl_op)
13748 tree node;
13749 tree wfl_op;
13750{
13751 tree op = TREE_OPERAND (node, 0);
13752 tree op_type = TREE_TYPE (op);
ab3a6dd6 13753 tree prom_type = NULL_TREE, value, decl;
c2952b01 13754 int outer_field_flag = 0;
e04a16fb
AG
13755 int code = TREE_CODE (node);
13756 int error_found = 0;
13757
13758 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13759
13760 switch (code)
13761 {
13762 /* 15.13.2 Postfix Increment Operator ++ */
13763 case POSTINCREMENT_EXPR:
13764 /* 15.13.3 Postfix Increment Operator -- */
13765 case POSTDECREMENT_EXPR:
13766 /* 15.14.1 Prefix Increment Operator ++ */
13767 case PREINCREMENT_EXPR:
13768 /* 15.14.2 Prefix Decrement Operator -- */
13769 case PREDECREMENT_EXPR:
5cbdba64 13770 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13771 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13772 /* We might be trying to change an outer field accessed using
13773 access method. */
13774 if (outer_field_flag)
13775 {
13776 /* Retrieve the decl of the field we're trying to access. We
13777 do that by first retrieving the function we would call to
13778 access the field. It has been already verified that this
13779 field isn't final */
13780 if (flag_emit_class_files)
13781 decl = TREE_OPERAND (op, 0);
13782 else
13783 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13784 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13785 }
b3edebcf 13786 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13787 else if (!JDECL_P (decl)
b3edebcf
APB
13788 && TREE_CODE (decl) != COMPONENT_REF
13789 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13790 && TREE_CODE (decl) != INDIRECT_REF
13791 && !(TREE_CODE (decl) == COMPOUND_EXPR
13792 && TREE_OPERAND (decl, 1)
13793 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13794 {
5e942c50
APB
13795 tree lvalue;
13796 /* Before screaming, check that we're not in fact trying to
13797 increment a optimized static final access, in which case
13798 we issue an different error message. */
13799 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13800 && resolve_expression_name (wfl_op, &lvalue)
13801 && check_final_assignment (lvalue, wfl_op)))
13802 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13803 operator_string (node));
e04a16fb
AG
13804 TREE_TYPE (node) = error_mark_node;
13805 error_found = 1;
13806 }
c2952b01
APB
13807
13808 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13809 error_found = 1;
13810
e04a16fb
AG
13811 /* From now on, we know that op if a variable and that it has a
13812 valid wfl. We use wfl_op to locate errors related to the
13813 ++/-- operand. */
13814 else if (!JNUMERIC_TYPE_P (op_type))
13815 {
13816 parse_error_context
13817 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13818 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13819 TREE_TYPE (node) = error_mark_node;
13820 error_found = 1;
13821 }
13822 else
13823 {
4a5f66c3 13824 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13825 both operands, if really necessary */
13826 if (JINTEGRAL_TYPE_P (op_type))
13827 {
13828 value = build_int_2 (1, 0);
13829 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13830 }
13831 else
13832 {
13833 value = build_int_2 (1, 0);
13834 TREE_TYPE (node) =
13835 binary_numeric_promotion (op_type,
13836 TREE_TYPE (value), &op, &value);
13837 }
c2952b01
APB
13838
13839 /* We remember we might be accessing an outer field */
13840 if (outer_field_flag)
13841 {
13842 /* We re-generate an access to the field */
13843 value = build (PLUS_EXPR, TREE_TYPE (op),
13844 build_outer_field_access (wfl_op, decl), value);
13845
13846 /* And we patch the original access$() into a write
13847 with plus_op as a rhs */
13848 return outer_field_access_fix (node, op, value);
13849 }
13850
5cbdba64 13851 /* And write back into the node. */
4a5f66c3 13852 TREE_OPERAND (node, 0) = op;
e04a16fb 13853 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13854 /* Convert the overall back into its original type, if
13855 necessary, and return */
13856 if (JINTEGRAL_TYPE_P (op_type))
13857 return fold (node);
13858 else
13859 return fold (convert (op_type, node));
e04a16fb
AG
13860 }
13861 break;
13862
13863 /* 15.14.3 Unary Plus Operator + */
b67d701b 13864 case UNARY_PLUS_EXPR:
e04a16fb
AG
13865 /* 15.14.4 Unary Minus Operator - */
13866 case NEGATE_EXPR:
13867 if (!JNUMERIC_TYPE_P (op_type))
13868 {
13869 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13870 TREE_TYPE (node) = error_mark_node;
13871 error_found = 1;
13872 }
13873 /* Unary numeric promotion is performed on operand */
13874 else
13875 {
15fdcfe9
PB
13876 op = do_unary_numeric_promotion (op);
13877 prom_type = TREE_TYPE (op);
b67d701b 13878 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13879 return fold (op);
e04a16fb
AG
13880 }
13881 break;
13882
13883 /* 15.14.5 Bitwise Complement Operator ~ */
13884 case BIT_NOT_EXPR:
13885 if (!JINTEGRAL_TYPE_P (op_type))
13886 {
13887 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13888 TREE_TYPE (node) = error_mark_node;
13889 error_found = 1;
13890 }
13891 else
13892 {
15fdcfe9
PB
13893 op = do_unary_numeric_promotion (op);
13894 prom_type = TREE_TYPE (op);
e04a16fb
AG
13895 }
13896 break;
13897
13898 /* 15.14.6 Logical Complement Operator ! */
13899 case TRUTH_NOT_EXPR:
13900 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13901 {
13902 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13903 /* But the type is known. We will report an error if further
13904 attempt of a assignment is made with this rhs */
e04a16fb
AG
13905 TREE_TYPE (node) = boolean_type_node;
13906 error_found = 1;
13907 }
13908 else
13909 prom_type = boolean_type_node;
13910 break;
13911
13912 /* 15.15 Cast Expression */
13913 case CONVERT_EXPR:
0a2138e2 13914 value = patch_cast (node, wfl_operator);
e04a16fb 13915 if (value == error_mark_node)
c877974e
APB
13916 {
13917 /* If this cast is part of an assignment, we tell the code
13918 that deals with it not to complain about a mismatch,
13919 because things have been cast, anyways */
13920 TREE_TYPE (node) = error_mark_node;
13921 error_found = 1;
13922 }
13923 else
dc0b3eff
PB
13924 {
13925 value = fold (value);
13926 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13927 return value;
13928 }
e04a16fb
AG
13929 break;
13930 }
13931
e04a16fb
AG
13932 if (error_found)
13933 return error_mark_node;
4a5f66c3
APB
13934
13935 /* There are cases where node has been replaced by something else
13936 and we don't end up returning here: UNARY_PLUS_EXPR,
13937 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13938 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13939 TREE_TYPE (node) = prom_type;
dc0b3eff 13940 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13941 return fold (node);
13942}
13943
13944/* Generic type resolution that sometimes takes place during node
13945 patching. Returned the resolved type or generate an error
13946 message. Return the resolved type or NULL_TREE. */
13947
13948static tree
13949resolve_type_during_patch (type)
13950 tree type;
13951{
13952 if (unresolved_type_p (type, NULL))
13953 {
c0b00d37 13954 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13955 if (!type_decl)
13956 {
13957 parse_error_context (type,
13958 "Class `%s' not found in type declaration",
13959 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13960 return NULL_TREE;
13961 }
13962 else
5e942c50
APB
13963 {
13964 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13965 return TREE_TYPE (type_decl);
13966 }
e04a16fb
AG
13967 }
13968 return type;
13969}
13970/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13971 found. Otherwise NODE or something meant to replace it is returned. */
13972
13973static tree
19e223db 13974patch_cast (node, wfl_op)
e04a16fb 13975 tree node;
19e223db 13976 tree wfl_op;
e04a16fb
AG
13977{
13978 tree op = TREE_OPERAND (node, 0);
13979 tree op_type = TREE_TYPE (op);
13980 tree cast_type = TREE_TYPE (node);
13981 char *t1;
13982
13983 /* First resolve OP_TYPE if unresolved */
13984 if (!(cast_type = resolve_type_during_patch (cast_type)))
13985 return error_mark_node;
13986
13987 /* Check on cast that are proven correct at compile time */
13988 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13989 {
e04a16fb
AG
13990 /* Same type */
13991 if (cast_type == op_type)
13992 return node;
13993
0b4d333e
APB
13994 /* float and double type are converted to the original type main
13995 variant and then to the target type. */
13996 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13997 op = convert (integer_type_node, op);
13998
e04a16fb
AG
13999 /* Try widening/narowwing convertion. Potentially, things need
14000 to be worked out in gcc so we implement the extreme cases
14001 correctly. fold_convert() needs to be fixed. */
14002 return convert (cast_type, op);
14003 }
14004
0b4d333e
APB
14005 /* It's also valid to cast a boolean into a boolean */
14006 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14007 return node;
14008
5e942c50
APB
14009 /* null can be casted to references */
14010 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14011 return build_null_of_type (cast_type);
14012
e04a16fb
AG
14013 /* The remaining legal casts involve conversion between reference
14014 types. Check for their compile time correctness. */
14015 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14016 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14017 {
14018 TREE_TYPE (node) = promote_type (cast_type);
14019 /* Now, the case can be determined correct at compile time if
14020 OP_TYPE can be converted into CAST_TYPE by assignment
14021 conversion (5.2) */
14022
14023 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14024 {
14025 TREE_SET_CODE (node, NOP_EXPR);
14026 return node;
14027 }
14028
14029 if (flag_emit_class_files)
14030 {
14031 TREE_SET_CODE (node, CONVERT_EXPR);
14032 return node;
14033 }
e04a16fb
AG
14034
14035 /* The cast requires a run-time check */
14036 return build (CALL_EXPR, promote_type (cast_type),
14037 build_address_of (soft_checkcast_node),
14038 tree_cons (NULL_TREE, build_class_ref (cast_type),
14039 build_tree_list (NULL_TREE, op)),
14040 NULL_TREE);
14041 }
14042
14043 /* Any other casts are proven incorrect at compile time */
c2e3db92 14044 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14045 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14046 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14047 free (t1);
14048 return error_mark_node;
14049}
14050
5e942c50
APB
14051/* Build a null constant and give it the type TYPE. */
14052
14053static tree
14054build_null_of_type (type)
14055 tree type;
14056{
14057 tree node = build_int_2 (0, 0);
14058 TREE_TYPE (node) = promote_type (type);
14059 return node;
14060}
14061
e04a16fb
AG
14062/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14063 a list of indices. */
14064static tree
14065build_array_ref (location, array, index)
14066 int location;
14067 tree array, index;
14068{
14069 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14070 EXPR_WFL_LINECOL (node) = location;
14071 return node;
14072}
14073
14074/* 15.12 Array Access Expression */
14075
14076static tree
c877974e
APB
14077patch_array_ref (node)
14078 tree node;
e04a16fb
AG
14079{
14080 tree array = TREE_OPERAND (node, 0);
14081 tree array_type = TREE_TYPE (array);
14082 tree index = TREE_OPERAND (node, 1);
14083 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14084 int error_found = 0;
14085
14086 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14087
e04a16fb
AG
14088 if (TREE_CODE (array_type) == POINTER_TYPE)
14089 array_type = TREE_TYPE (array_type);
14090
14091 /* The array reference must be an array */
14092 if (!TYPE_ARRAY_P (array_type))
14093 {
14094 parse_error_context
781b0558
KG
14095 (wfl_operator,
14096 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14097 lang_printable_name (array_type, 0));
e04a16fb
AG
14098 TREE_TYPE (node) = error_mark_node;
14099 error_found = 1;
14100 }
14101
c2952b01 14102 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14103 type must be int */
15fdcfe9
PB
14104 index = do_unary_numeric_promotion (index);
14105 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14106 {
1ebadc60 14107 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14108 parse_error_context (wfl_operator,
14109 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14110 lang_printable_name (index_type, 0));
14111 else
781b0558
KG
14112 parse_error_context (wfl_operator,
14113 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14114 lang_printable_name (index_type, 0));
e04a16fb
AG
14115 TREE_TYPE (node) = error_mark_node;
14116 error_found = 1;
14117 }
14118
e04a16fb
AG
14119 if (error_found)
14120 return error_mark_node;
e04a16fb 14121
5e942c50 14122 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14123
7f1d4866 14124 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14125 {
15fdcfe9
PB
14126 TREE_OPERAND (node, 0) = array;
14127 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14128 }
14129 else
939d7216
PB
14130 {
14131 /* The save_expr is for correct evaluation order. It would be cleaner
14132 to use force_evaluation_order (see comment there), but that is
14133 difficult when we also have to deal with bounds checking. */
14134 if (TREE_SIDE_EFFECTS (index))
14135 array = save_expr (array);
14136 node = build_java_arrayaccess (array, array_type, index);
14137 if (TREE_SIDE_EFFECTS (index))
14138 node = build (COMPOUND_EXPR, array_type, array, node);
14139 }
e04a16fb
AG
14140 TREE_TYPE (node) = array_type;
14141 return node;
14142}
14143
14144/* 15.9 Array Creation Expressions */
14145
14146static tree
14147build_newarray_node (type, dims, extra_dims)
14148 tree type;
14149 tree dims;
14150 int extra_dims;
14151{
14152 tree node =
b67d701b 14153 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14154 build_int_2 (extra_dims, 0));
e04a16fb
AG
14155 return node;
14156}
14157
14158static tree
14159patch_newarray (node)
14160 tree node;
14161{
14162 tree type = TREE_OPERAND (node, 0);
14163 tree dims = TREE_OPERAND (node, 1);
14164 tree cdim, array_type;
14165 int error_found = 0;
14166 int ndims = 0;
14167 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14168
14169 /* Dimension types are verified. It's better for the types to be
14170 verified in order. */
14171 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14172 {
14173 int dim_error = 0;
14174 tree dim = TREE_VALUE (cdim);
14175
14176 /* Dim might have been saved during its evaluation */
dba41d30 14177 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14178
14179 /* The type of each specified dimension must be an integral type. */
14180 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14181 dim_error = 1;
14182
14183 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14184 promoted type must be int. */
14185 else
14186 {
15fdcfe9 14187 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14188 if (TREE_TYPE (dim) != int_type_node)
14189 dim_error = 1;
14190 }
14191
14192 /* Report errors on types here */
14193 if (dim_error)
14194 {
14195 parse_error_context
14196 (TREE_PURPOSE (cdim),
781b0558 14197 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14198 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14199 "Explicit cast needed to" : "Can't"),
0a2138e2 14200 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14201 error_found = 1;
14202 }
14203
e04a16fb
AG
14204 TREE_PURPOSE (cdim) = NULL_TREE;
14205 }
14206
14207 /* Resolve array base type if unresolved */
14208 if (!(type = resolve_type_during_patch (type)))
14209 error_found = 1;
14210
14211 if (error_found)
14212 {
14213 /* We don't want further evaluation of this bogus array creation
14214 operation */
14215 TREE_TYPE (node) = error_mark_node;
14216 return error_mark_node;
14217 }
14218
15fdcfe9
PB
14219 /* Set array_type to the actual (promoted) array type of the result. */
14220 if (TREE_CODE (type) == RECORD_TYPE)
14221 type = build_pointer_type (type);
14222 while (--xdims >= 0)
14223 {
14224 type = promote_type (build_java_array_type (type, -1));
14225 }
14226 dims = nreverse (dims);
14227 array_type = type;
14228 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14229 {
14230 type = array_type;
05bccae2
RK
14231 array_type
14232 = build_java_array_type (type,
14233 TREE_CODE (cdim) == INTEGER_CST
14234 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14235 : -1);
15fdcfe9
PB
14236 array_type = promote_type (array_type);
14237 }
14238 dims = nreverse (dims);
14239
e04a16fb
AG
14240 /* The node is transformed into a function call. Things are done
14241 differently according to the number of dimensions. If the number
14242 of dimension is equal to 1, then the nature of the base type
14243 (primitive or not) matters. */
15fdcfe9 14244 if (ndims == 1)
fdec99c6 14245 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14246
e04a16fb
AG
14247 /* Can't reuse what's already written in expr.c because it uses the
14248 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14249 return build (CALL_EXPR, array_type,
e04a16fb 14250 build_address_of (soft_multianewarray_node),
15fdcfe9 14251 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14252 tree_cons (NULL_TREE,
15fdcfe9 14253 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14254 NULL_TREE);
14255}
14256
f8976021
APB
14257/* 10.6 Array initializer. */
14258
14259/* Build a wfl for array element that don't have one, so we can
14260 pin-point errors. */
14261
14262static tree
14263maybe_build_array_element_wfl (node)
14264 tree node;
14265{
14266 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14267 return build_expr_wfl (NULL_TREE, ctxp->filename,
14268 ctxp->elc.line, ctxp->elc.prev_col);
14269 else
14270 return NULL_TREE;
14271}
14272
14273/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14274 identification of initialized arrays easier to detect during walk
14275 and expansion. */
14276
14277static tree
14278build_new_array_init (location, values)
14279 int location;
14280 tree values;
14281{
14282 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14283 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14284 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14285 return to_return;
14286}
14287
14288/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14289 occurred. Otherwise return NODE after having set its type
14290 appropriately. */
14291
14292static tree
14293patch_new_array_init (type, node)
14294 tree type, node;
f8976021
APB
14295{
14296 int error_seen = 0;
fdec99c6 14297 tree current, element_type;
f8976021 14298 HOST_WIDE_INT length;
fdec99c6
PB
14299 int all_constant = 1;
14300 tree init = TREE_OPERAND (node, 0);
f8976021 14301
fdec99c6
PB
14302 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14303 {
14304 parse_error_context (node,
14305 "Invalid array initializer for non-array type `%s'",
14306 lang_printable_name (type, 1));
14307 return error_mark_node;
14308 }
14309 type = TREE_TYPE (type);
14310 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14311
fdec99c6
PB
14312 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14313
14314 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14315 current; length++, current = TREE_CHAIN (current))
f8976021 14316 {
fdec99c6
PB
14317 tree elt = TREE_VALUE (current);
14318 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14319 {
fdec99c6 14320 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14321 elt = TREE_VALUE (current);
14322 /* When compiling to native code, STRING_CST is converted to
14323 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14324 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14325 all_constant = 0;
f8976021 14326 }
fdec99c6
PB
14327 else
14328 {
14329 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14330 TREE_PURPOSE (current) = NULL_TREE;
14331 all_constant = 0;
14332 }
9a7ab4b3
APB
14333 if (elt && TREE_CODE (elt) == TREE_LIST
14334 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14335 error_seen = 1;
f8976021
APB
14336 }
14337
14338 if (error_seen)
14339 return error_mark_node;
14340
14341 /* Create a new type. We can't reuse the one we have here by
14342 patching its dimension because it originally is of dimension -1
14343 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14344 type = build_java_array_type (element_type, length);
14345 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14346 TREE_TYPE (node) = promote_type (type);
14347 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14348 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14349 return node;
14350}
14351
14352/* Verify that one entry of the initializer element list can be
14353 assigned to the array base type. Report 1 if an error occurred, 0
14354 otherwise. */
14355
14356static int
14357array_constructor_check_entry (type, entry)
14358 tree type, entry;
14359{
14360 char *array_type_string = NULL; /* For error reports */
14361 tree value, type_value, new_value, wfl_value, patched;
14362 int error_seen = 0;
14363
14364 new_value = NULL_TREE;
14365 wfl_value = TREE_VALUE (entry);
14366
f8976021 14367 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14368 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14369 if ((patched = patch_string (value)))
14370 value = patched;
1179ebc2
APB
14371 if (value == error_mark_node)
14372 return 1;
f8976021 14373
f8976021
APB
14374 type_value = TREE_TYPE (value);
14375
1179ebc2 14376 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14377 constant overflow during narrowing. */
14378 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14379 new_value = try_builtin_assignconv (wfl_operator, type, value);
14380 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14381 type_value = promote_type (type);
100f7cd8 14382
f8976021
APB
14383 /* Check and report errors */
14384 if (!new_value)
14385 {
49f48c71 14386 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14387 "Can't" : "Explicit cast needed to");
14388 if (!array_type_string)
c2e3db92 14389 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14390 parse_error_context
14391 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14392 msg, lang_printable_name (type_value, 1), array_type_string);
14393 error_seen = 1;
14394 }
14395
14396 if (new_value)
14397 {
b8c5b1c6 14398 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14399 TREE_VALUE (entry) = new_value;
14400 }
14401
14402 if (array_type_string)
14403 free (array_type_string);
14404
14405 TREE_PURPOSE (entry) = NULL_TREE;
14406 return error_seen;
14407}
14408
e04a16fb
AG
14409static tree
14410build_this (location)
14411 int location;
14412{
9ee9b555 14413 tree node = build_wfl_node (this_identifier_node);
b67d701b 14414 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14415 EXPR_WFL_LINECOL (node) = location;
14416 return node;
14417}
14418
14419/* 14.15 The return statement. It builds a modify expression that
14420 assigns the returned value to the RESULT_DECL that hold the value
14421 to be returned. */
14422
14423static tree
14424build_return (location, op)
14425 int location;
14426 tree op;
14427{
14428 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14429 EXPR_WFL_LINECOL (node) = location;
b67d701b 14430 node = build_debugable_stmt (location, node);
e04a16fb
AG
14431 return node;
14432}
14433
14434static tree
14435patch_return (node)
14436 tree node;
14437{
14438 tree return_exp = TREE_OPERAND (node, 0);
14439 tree meth = current_function_decl;
14440 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14441 int error_found = 0;
14442
14443 TREE_TYPE (node) = error_mark_node;
14444 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14445
14446 /* It's invalid to have a return value within a function that is
14447 declared with the keyword void or that is a constructor */
14448 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14449 error_found = 1;
14450
f099f336 14451 /* It's invalid to use a return statement in a static block */
c2952b01 14452 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14453 error_found = 1;
14454
e04a16fb
AG
14455 /* It's invalid to have a no return value within a function that
14456 isn't declared with the keyword `void' */
14457 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14458 error_found = 2;
c2952b01
APB
14459
14460 if (in_instance_initializer)
14461 error_found = 1;
e04a16fb
AG
14462
14463 if (error_found)
14464 {
c2952b01 14465 if (in_instance_initializer)
f099f336 14466 parse_error_context (wfl_operator,
c2952b01
APB
14467 "`return' inside instance initializer");
14468
14469 else if (DECL_CLINIT_P (current_function_decl))
14470 parse_error_context (wfl_operator,
14471 "`return' inside static initializer");
f099f336
APB
14472
14473 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14474 {
c2e3db92 14475 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14476 parse_error_context (wfl_operator,
14477 "`return' with%s value from `%s %s'",
14478 (error_found == 1 ? "" : "out"),
14479 t, lang_printable_name (meth, 0));
14480 free (t);
14481 }
14482 else
14483 parse_error_context (wfl_operator,
14484 "`return' with value from constructor `%s'",
14485 lang_printable_name (meth, 0));
e04a16fb
AG
14486 return error_mark_node;
14487 }
14488
5e942c50
APB
14489 /* If we have a return_exp, build a modify expression and expand
14490 it. Note: at that point, the assignment is declared valid, but we
14491 may want to carry some more hacks */
e04a16fb
AG
14492 if (return_exp)
14493 {
5e942c50
APB
14494 tree exp = java_complete_tree (return_exp);
14495 tree modify, patched;
14496
14497 /* If the function returned value and EXP are booleans, EXP has
14498 to be converted into the type of DECL_RESULT, which is integer
14499 (see complete_start_java_method) */
14500 if (TREE_TYPE (exp) == boolean_type_node &&
14501 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14502 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14503
14504 /* `null' can be assigned to a function returning a reference */
14505 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14506 exp == null_pointer_node)
14507 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14508
14509 if ((patched = patch_string (exp)))
14510 exp = patched;
14511
14512 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14513 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14514 modify = java_complete_tree (modify);
5e942c50 14515
e04a16fb
AG
14516 if (modify != error_mark_node)
14517 {
14518 TREE_SIDE_EFFECTS (modify) = 1;
14519 TREE_OPERAND (node, 0) = modify;
14520 }
14521 else
14522 return error_mark_node;
14523 }
14524 TREE_TYPE (node) = void_type_node;
14525 TREE_SIDE_EFFECTS (node) = 1;
14526 return node;
14527}
14528
14529/* 14.8 The if Statement */
14530
14531static tree
14532build_if_else_statement (location, expression, if_body, else_body)
14533 int location;
14534 tree expression, if_body, else_body;
14535{
14536 tree node;
e04a16fb 14537 if (!else_body)
9bbc7d9f 14538 else_body = empty_stmt_node;
e04a16fb
AG
14539 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14540 EXPR_WFL_LINECOL (node) = location;
b67d701b 14541 node = build_debugable_stmt (location, node);
e04a16fb
AG
14542 return node;
14543}
14544
14545static tree
14546patch_if_else_statement (node)
14547 tree node;
14548{
14549 tree expression = TREE_OPERAND (node, 0);
14550
14551 TREE_TYPE (node) = error_mark_node;
14552 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14553
14554 /* The type of expression must be boolean */
b67d701b
PB
14555 if (TREE_TYPE (expression) != boolean_type_node
14556 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14557 {
14558 parse_error_context
14559 (wfl_operator,
14560 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14561 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14562 return error_mark_node;
14563 }
14564
14565 TREE_TYPE (node) = void_type_node;
14566 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14567 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14568 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14569 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14570 return node;
14571}
14572
14573/* 14.6 Labeled Statements */
14574
14575/* Action taken when a lableled statement is parsed. a new
14576 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14577 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14578
14579static tree
0a2138e2 14580build_labeled_block (location, label)
e04a16fb 14581 int location;
0a2138e2 14582 tree label;
e04a16fb 14583{
b635eb2f 14584 tree label_name ;
e04a16fb 14585 tree label_decl, node;
b635eb2f
PB
14586 if (label == NULL_TREE || label == continue_identifier_node)
14587 label_name = label;
14588 else
e04a16fb 14589 {
b635eb2f
PB
14590 label_name = merge_qualified_name (label_id, label);
14591 /* Issue an error if we try to reuse a label that was previously
14592 declared */
14593 if (IDENTIFIER_LOCAL_VALUE (label_name))
14594 {
14595 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14596 parse_error_context (wfl_operator,
14597 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14598 IDENTIFIER_POINTER (label));
14599 EXPR_WFL_LINECOL (wfl_operator) =
14600 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14601 parse_error_context (wfl_operator,
14602 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14603 IDENTIFIER_POINTER (label));
14604 java_error_count--;
14605 }
e04a16fb
AG
14606 }
14607
14608 label_decl = create_label_decl (label_name);
14609 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14610 EXPR_WFL_LINECOL (node) = location;
14611 TREE_SIDE_EFFECTS (node) = 1;
14612 return node;
14613}
14614
b67d701b 14615/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14616
14617static tree
b635eb2f 14618finish_labeled_statement (lbe, statement)
e04a16fb
AG
14619 tree lbe; /* Labeled block expr */
14620 tree statement;
14621{
14622 /* In anyways, tie the loop to its statement */
14623 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14624 pop_labeled_block ();
14625 POP_LABELED_BLOCK ();
e04a16fb
AG
14626 return lbe;
14627}
14628
14629/* 14.10, 14.11, 14.12 Loop Statements */
14630
14631/* Create an empty LOOP_EXPR and make it the last in the nested loop
14632 list. */
14633
14634static tree
14635build_new_loop (loop_body)
14636 tree loop_body;
14637{
14638 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14639 TREE_SIDE_EFFECTS (loop) = 1;
14640 PUSH_LOOP (loop);
14641 return loop;
14642}
14643
14644/* Create a loop body according to the following structure:
14645 COMPOUND_EXPR
14646 COMPOUND_EXPR (loop main body)
14647 EXIT_EXPR (this order is for while/for loops.
14648 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14649 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14650 BODY end of this labeled block)
14651 INCREMENT (if any)
14652
14653 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14654 after the body, like in the do-while loop.
14655
14656 To obtain a loop, the loop body structure described above is
14657 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14658
14659 LABELED_BLOCK_EXPR
14660 LABEL_DECL (use this label to exit the loop)
14661 LOOP_EXPR
14662 <structure described above> */
e04a16fb
AG
14663
14664static tree
14665build_loop_body (location, condition, reversed)
14666 int location;
14667 tree condition;
14668 int reversed;
14669{
0a2138e2 14670 tree first, second, body;
e04a16fb
AG
14671
14672 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14673 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14674 condition = build_debugable_stmt (location, condition);
14675 TREE_SIDE_EFFECTS (condition) = 1;
14676
b635eb2f 14677 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14678 first = (reversed ? body : condition);
14679 second = (reversed ? condition : body);
14680 return
14681 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14682 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14683}
14684
14685/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14686 their order) on the current loop. Unlink the current loop from the
14687 loop list. */
14688
14689static tree
b635eb2f 14690finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14691 int location;
14692 tree condition, body;
14693 int reversed;
14694{
14695 tree to_return = ctxp->current_loop;
14696 tree loop_body = LOOP_EXPR_BODY (to_return);
14697 if (condition)
14698 {
14699 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14700 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14701 The real EXIT_EXPR is one operand further. */
14702 EXPR_WFL_LINECOL (cnode) = location;
14703 /* This one is for accurate error reports */
14704 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14705 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14706 }
14707 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14708 POP_LOOP ();
14709 return to_return;
14710}
14711
b635eb2f 14712/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14713 loops feature the condition part */
14714
14715static tree
b635eb2f 14716finish_for_loop (location, condition, update, body)
e04a16fb
AG
14717 int location;
14718 tree condition, update, body;
14719{
14720 /* Put the condition and the loop body in place */
b635eb2f 14721 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14722 /* LOOP is the current loop which has been now popped of the loop
14723 stack. Install the update block */
14724 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14725 return loop;
14726}
14727
5cbdba64
APB
14728/* Try to find the loop a block might be related to. This comprises
14729 the case where the LOOP_EXPR is found as the second operand of a
14730 COMPOUND_EXPR, because the loop happens to have an initialization
14731 part, then expressed as the first operand of the COMPOUND_EXPR. If
14732 the search finds something, 1 is returned. Otherwise, 0 is
14733 returned. The search is assumed to start from a
14734 LABELED_BLOCK_EXPR's block. */
14735
14736static tree
14737search_loop (statement)
14738 tree statement;
14739{
14740 if (TREE_CODE (statement) == LOOP_EXPR)
14741 return statement;
14742
14743 if (TREE_CODE (statement) == BLOCK)
14744 statement = BLOCK_SUBBLOCKS (statement);
14745 else
14746 return NULL_TREE;
14747
14748 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14749 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14750 statement = TREE_OPERAND (statement, 1);
14751
14752 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14753 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14754}
14755
14756/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14757 returned otherwise. */
14758
14759static int
14760labeled_block_contains_loop_p (block, loop)
14761 tree block, loop;
14762{
14763 if (!block)
14764 return 0;
14765
14766 if (LABELED_BLOCK_BODY (block) == loop)
14767 return 1;
14768
c2952b01 14769 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14770 return 1;
14771
14772 return 0;
14773}
14774
e04a16fb 14775/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14776 insert LOOP as its body. */
e04a16fb
AG
14777
14778static tree
14779patch_loop_statement (loop)
14780 tree loop;
14781{
cd9643f7 14782 tree loop_label;
5cbdba64 14783
cd9643f7 14784 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14785 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14786 return loop;
14787
cd9643f7 14788 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14789 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14790 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14791 LABELED_BLOCK_BODY (loop_label) = loop;
14792 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14793 return loop_label;
e04a16fb
AG
14794}
14795
14796/* 14.13, 14.14: break and continue Statements */
14797
14798/* Build a break or a continue statement. a null NAME indicates an
14799 unlabeled break/continue statement. */
14800
14801static tree
14802build_bc_statement (location, is_break, name)
14803 int location, is_break;
14804 tree name;
14805{
14806 tree break_continue, label_block_expr = NULL_TREE;
14807
14808 if (name)
14809 {
14810 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14811 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14812 /* Null means that we don't have a target for this named
14813 break/continue. In this case, we make the target to be the
14814 label name, so that the error can be reported accuratly in
14815 patch_bc_statement. */
14816 label_block_expr = EXPR_WFL_NODE (name);
14817 }
14818 /* Unlabeled break/continue will be handled during the
14819 break/continue patch operation */
14820 break_continue
14821 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14822
14823 IS_BREAK_STMT_P (break_continue) = is_break;
14824 TREE_SIDE_EFFECTS (break_continue) = 1;
14825 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14826 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14827 return break_continue;
14828}
14829
14830/* Verification of a break/continue statement. */
14831
14832static tree
14833patch_bc_statement (node)
14834 tree node;
14835{
14836 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14837 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14838 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14839
e04a16fb 14840 /* Having an identifier here means that the target is unknown. */
b635eb2f 14841 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14842 {
14843 parse_error_context (wfl_operator, "No label definition found for `%s'",
14844 IDENTIFIER_POINTER (bc_label));
14845 return error_mark_node;
14846 }
b635eb2f 14847 if (! IS_BREAK_STMT_P (node))
e04a16fb 14848 {
b635eb2f
PB
14849 /* It's a continue statement. */
14850 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14851 {
b635eb2f
PB
14852 if (labeled_block == NULL_TREE)
14853 {
14854 if (bc_label == NULL_TREE)
14855 parse_error_context (wfl_operator,
14856 "`continue' must be in loop");
14857 else
1504b2b4
APB
14858 parse_error_context
14859 (wfl_operator, "continue label `%s' does not name a loop",
14860 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14861 return error_mark_node;
14862 }
14863 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14864 == continue_identifier_node)
14865 && (bc_label == NULL_TREE
14866 || TREE_CHAIN (labeled_block) == bc_label))
14867 {
14868 bc_label = labeled_block;
14869 break;
14870 }
e04a16fb 14871 }
e04a16fb 14872 }
b635eb2f 14873 else if (!bc_label)
34f4db93 14874 {
b635eb2f 14875 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14876 {
b635eb2f
PB
14877 if (labeled_block == NULL_TREE)
14878 {
14879 parse_error_context (wfl_operator,
14880 "`break' must be in loop or switch");
14881 return error_mark_node;
14882 }
14883 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14884 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14885 || search_loop (target_stmt))
b635eb2f
PB
14886 {
14887 bc_label = labeled_block;
14888 break;
14889 }
e04a16fb 14890 }
e04a16fb
AG
14891 }
14892
b635eb2f 14893 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14894 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14895
e04a16fb
AG
14896 /* Our break/continue don't return values. */
14897 TREE_TYPE (node) = void_type_node;
14898 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14899 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14900 sometimes, like after a if statement) */
14901 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14902 TREE_SIDE_EFFECTS (node) = 1;
14903 return node;
14904}
14905
14906/* Process the exit expression belonging to a loop. Its type must be
14907 boolean. */
14908
14909static tree
14910patch_exit_expr (node)
14911 tree node;
14912{
14913 tree expression = TREE_OPERAND (node, 0);
14914 TREE_TYPE (node) = error_mark_node;
14915 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14916
14917 /* The type of expression must be boolean */
14918 if (TREE_TYPE (expression) != boolean_type_node)
14919 {
14920 parse_error_context
14921 (wfl_operator,
781b0558 14922 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14923 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14924 return error_mark_node;
14925 }
14926 /* Now we know things are allright, invert the condition, fold and
14927 return */
14928 TREE_OPERAND (node, 0) =
14929 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14930
14931 if (! integer_zerop (TREE_OPERAND (node, 0))
14932 && ctxp->current_loop != NULL_TREE
14933 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14934 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14935 if (! integer_onep (TREE_OPERAND (node, 0)))
14936 CAN_COMPLETE_NORMALLY (node) = 1;
14937
14938
e04a16fb
AG
14939 TREE_TYPE (node) = void_type_node;
14940 return node;
14941}
b67d701b
PB
14942
14943/* 14.9 Switch statement */
14944
14945static tree
14946patch_switch_statement (node)
14947 tree node;
14948{
c877974e 14949 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14950
14951 /* Complete the switch expression */
14952 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14953 se_type = TREE_TYPE (se);
14954 /* The type of the switch expression must be char, byte, short or
14955 int */
2e0f0aff 14956 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14957 {
14958 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14959 parse_error_context (wfl_operator,
14960 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14961 lang_printable_name (se_type, 0));
b67d701b
PB
14962 /* This is what java_complete_tree will check */
14963 TREE_OPERAND (node, 0) = error_mark_node;
14964 return error_mark_node;
14965 }
14966
15fdcfe9 14967 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14968
14969 /* Ready to return */
15fdcfe9 14970 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14971 {
14972 TREE_TYPE (node) = error_mark_node;
14973 return error_mark_node;
14974 }
14975 TREE_TYPE (node) = void_type_node;
14976 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14977 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14978 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14979 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14980 return node;
14981}
14982
165f37bc 14983/* 14.18 The try/catch statements */
b67d701b 14984
b67d701b 14985static tree
a7d8d81f 14986build_try_statement (location, try_block, catches)
b67d701b 14987 int location;
a7d8d81f
PB
14988 tree try_block, catches;
14989{
14990 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14991 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14992 return node;
b67d701b
PB
14993}
14994
a7d8d81f
PB
14995static tree
14996build_try_finally_statement (location, try_block, finally)
14997 int location;
14998 tree try_block, finally;
b67d701b 14999{
a7d8d81f
PB
15000 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15001 EXPR_WFL_LINECOL (node) = location;
15002 return node;
b67d701b
PB
15003}
15004
15005static tree
15006patch_try_statement (node)
15007 tree node;
15008{
15009 int error_found = 0;
15010 tree try = TREE_OPERAND (node, 0);
15011 /* Exception handlers are considered in left to right order */
15012 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15013 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15014
15015 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15016 to process the next catch clause. We process the catch clause before
15017 the try block so that when processing the try block we can check thrown
15018 exceptions againts the caught type list. */
b67d701b
PB
15019 for (current = catch; current; current = TREE_CHAIN (current))
15020 {
15021 tree carg_decl, carg_type;
15022 tree sub_current, catch_block, catch_clause;
15023 int unreachable;
15024
b67d701b 15025 /* At this point, the structure of the catch clause is
b67d701b
PB
15026 CATCH_EXPR (catch node)
15027 BLOCK (with the decl of the parameter)
15028 COMPOUND_EXPR
7525cc04 15029 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15030 BLOCK (catch clause block)
a7d8d81f
PB
15031 */
15032 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15033 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15034 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15035
15036 /* Catch clauses can't have more than one parameter declared,
15037 but it's already enforced by the grammar. Make sure that the
15038 only parameter of the clause statement in of class Throwable
15039 or a subclass of Throwable, but that was done earlier. The
15040 catch clause parameter type has also been resolved. */
15041
15042 /* Just make sure that the catch clause parameter type inherits
15043 from java.lang.Throwable */
15044 if (!inherits_from_p (carg_type, throwable_type_node))
15045 {
15046 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15047 parse_error_context (wfl_operator,
781b0558 15048 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15049 lang_printable_name (carg_type, 0));
b67d701b
PB
15050 error_found = 1;
15051 continue;
15052 }
15053
15054 /* Partial check for unreachable catch statement: The catch
15055 clause is reachable iff is no earlier catch block A in
15056 the try statement such that the type of the catch
15057 clause's parameter is the same as or a subclass of the
15058 type of A's parameter */
15059 unreachable = 0;
15060 for (sub_current = catch;
15061 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15062 {
15063 tree sub_catch_clause, decl;
a7d8d81f 15064 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15065 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15066
15067 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15068 {
15069 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15070 parse_error_context
781b0558
KG
15071 (wfl_operator,
15072 "`catch' not reached because of the catch clause at line %d",
15073 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15074 unreachable = error_found = 1;
15075 break;
15076 }
15077 }
b67d701b
PB
15078 /* Complete the catch clause block */
15079 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15080 if (catch_block == error_mark_node)
15081 {
15082 error_found = 1;
15083 continue;
15084 }
15fdcfe9
PB
15085 if (CAN_COMPLETE_NORMALLY (catch_block))
15086 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15087 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15088
15089 if (unreachable)
15090 continue;
15091
15092 /* Things to do here: the exception must be thrown */
15093
15094 /* Link this type to the caught type list */
15095 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15096 }
15097
b9f7e36c
APB
15098 PUSH_EXCEPTIONS (caught_type_list);
15099 if ((try = java_complete_tree (try)) == error_mark_node)
15100 error_found = 1;
15fdcfe9
PB
15101 if (CAN_COMPLETE_NORMALLY (try))
15102 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15103 POP_EXCEPTIONS ();
15104
b67d701b
PB
15105 /* Verification ends here */
15106 if (error_found)
15107 return error_mark_node;
15108
15109 TREE_OPERAND (node, 0) = try;
15110 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15111 TREE_TYPE (node) = void_type_node;
15112 return node;
15113}
b9f7e36c
APB
15114
15115/* 14.17 The synchronized Statement */
15116
15117static tree
15118patch_synchronized_statement (node, wfl_op1)
15119 tree node, wfl_op1;
15120{
5a005d9e 15121 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15122 tree block = TREE_OPERAND (node, 1);
5a005d9e 15123
c7303e41 15124 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15125
15126 if (expr == error_mark_node)
15127 {
15128 block = java_complete_tree (block);
15129 return expr;
15130 }
b9f7e36c 15131
c7303e41
APB
15132 /* We might be trying to synchronize on a STRING_CST */
15133 if ((tmp = patch_string (expr)))
15134 expr = tmp;
15135
b9f7e36c 15136 /* The TYPE of expr must be a reference type */
5a005d9e 15137 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15138 {
15139 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15140 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15141 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15142 return error_mark_node;
15143 }
15144
ce6e9147
APB
15145 if (flag_emit_xref)
15146 {
15147 TREE_OPERAND (node, 0) = expr;
15148 TREE_OPERAND (node, 1) = java_complete_tree (block);
15149 CAN_COMPLETE_NORMALLY (node) = 1;
15150 return node;
15151 }
15152
b9f7e36c
APB
15153 /* Generate a try-finally for the synchronized statement, except
15154 that the handler that catches all throw exception calls
15155 _Jv_MonitorExit and then rethrow the exception.
15156 The synchronized statement is then implemented as:
15157 TRY
15158 {
15159 _Jv_MonitorEnter (expression)
15160 synchronized_block
15161 _Jv_MonitorExit (expression)
15162 }
15163 CATCH_ALL
15164 {
15165 e = _Jv_exception_info ();
15166 _Jv_MonitorExit (expression)
15167 Throw (e);
15168 } */
15169
5a005d9e
PB
15170 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15171 BUILD_MONITOR_ENTER (enter, expr_decl);
15172 BUILD_MONITOR_EXIT (exit, expr_decl);
15173 CAN_COMPLETE_NORMALLY (enter) = 1;
15174 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15175 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15176 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15177 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15178 build (COMPOUND_EXPR, NULL_TREE,
15179 build (WITH_CLEANUP_EXPR, NULL_TREE,
15180 build (COMPOUND_EXPR, NULL_TREE,
96847892 15181 assignment, enter),
5a005d9e
PB
15182 NULL_TREE, exit),
15183 block));
15184 node = build_expr_block (node, expr_decl);
15185
15186 return java_complete_tree (node);
b9f7e36c
APB
15187}
15188
15189/* 14.16 The throw Statement */
15190
15191static tree
15192patch_throw_statement (node, wfl_op1)
15193 tree node, wfl_op1;
15194{
15195 tree expr = TREE_OPERAND (node, 0);
15196 tree type = TREE_TYPE (expr);
15197 int unchecked_ok = 0, tryblock_throws_ok = 0;
15198
15199 /* Thrown expression must be assignable to java.lang.Throwable */
15200 if (!try_reference_assignconv (throwable_type_node, expr))
15201 {
15202 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15203 parse_error_context (wfl_operator,
15204 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15205 lang_printable_name (type, 0));
b9f7e36c
APB
15206 /* If the thrown expression was a reference, we further the
15207 compile-time check. */
15208 if (!JREFERENCE_TYPE_P (type))
15209 return error_mark_node;
15210 }
15211
15212 /* At least one of the following must be true */
15213
15214 /* The type of the throw expression is a not checked exception,
15215 i.e. is a unchecked expression. */
c877974e 15216 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15217
c2952b01
APB
15218 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15219 /* An instance can't throw a checked excetion unless that exception
15220 is explicitely declared in the `throws' clause of each
15221 constructor. This doesn't apply to anonymous classes, since they
15222 don't have declared constructors. */
15223 if (!unchecked_ok
15224 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15225 {
15226 tree current;
15227 for (current = TYPE_METHODS (current_class); current;
15228 current = TREE_CHAIN (current))
15229 if (DECL_CONSTRUCTOR_P (current)
15230 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15231 {
15232 parse_error_context (wfl_operator, "Checked exception `%s' can't be thrown in instance initializer (not all declared constructor are declaring it in their `throws' clause)",
15233 lang_printable_name (TREE_TYPE (expr), 0));
15234 return error_mark_node;
15235 }
15236 }
15237
b9f7e36c
APB
15238 /* Throw is contained in a try statement and at least one catch
15239 clause can receive the thrown expression or the current method is
15240 declared to throw such an exception. Or, the throw statement is
15241 contained in a method or constructor declaration and the type of
15242 the Expression is assignable to at least one type listed in the
15243 throws clause the declaration. */
b9f7e36c 15244 if (!unchecked_ok)
f099f336 15245 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15246 if (!(unchecked_ok || tryblock_throws_ok))
15247 {
15248 /* If there is a surrounding try block that has no matching
15249 clatch clause, report it first. A surrounding try block exits
15250 only if there is something after the list of checked
15251 exception thrown by the current function (if any). */
15252 if (IN_TRY_BLOCK_P ())
781b0558 15253 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
0a2138e2 15254 lang_printable_name (type, 0));
b9f7e36c
APB
15255 /* If we have no surrounding try statement and the method doesn't have
15256 any throws, report it now. FIXME */
f099f336
APB
15257
15258 /* We report that the exception can't be throw from a try block
15259 in all circumstances but when the `throw' is inside a static
15260 block. */
b9f7e36c
APB
15261 else if (!EXCEPTIONS_P (currently_caught_type_list)
15262 && !tryblock_throws_ok)
f099f336 15263 {
c2952b01 15264 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15265 parse_error_context (wfl_operator,
15266 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15267 lang_printable_name (type, 0));
15268 else
781b0558
KG
15269 parse_error_context (wfl_operator,
15270 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15271 lang_printable_name (type, 0));
15272 }
b9f7e36c
APB
15273 /* Otherwise, the current method doesn't have the appropriate
15274 throws declaration */
15275 else
781b0558 15276 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15277 lang_printable_name (type, 0));
b9f7e36c
APB
15278 return error_mark_node;
15279 }
15280
ce6e9147 15281 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15282 BUILD_THROW (node, expr);
ce6e9147
APB
15283
15284 /* If doing xrefs, keep the location where the `throw' was seen. */
15285 if (flag_emit_xref)
15286 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15287 return node;
15288}
15289
15290/* Check that exception said to be thrown by method DECL can be
15291 effectively caught from where DECL is invoked. */
15292
15293static void
15294check_thrown_exceptions (location, decl)
15295 int location;
15296 tree decl;
15297{
15298 tree throws;
15299 /* For all the unchecked exceptions thrown by DECL */
15300 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15301 throws = TREE_CHAIN (throws))
0a2138e2 15302 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15303 {
3e78f871
PB
15304#if 1
15305 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15306 if (DECL_NAME (decl) == get_identifier ("clone"))
15307 continue;
15308#endif
b9f7e36c 15309 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15310 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15311 parse_error_context
15312 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15313 lang_printable_name (TREE_VALUE (throws), 0));
15314 else
15315 {
15316 parse_error_context
781b0558 15317 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15318 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15319 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15320 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15321 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15322 }
b9f7e36c
APB
15323 }
15324}
15325
c877974e 15326/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15327 try-catch blocks, OR is listed in the `throws' clause of the
15328 current method. */
15329
15330static int
0a2138e2 15331check_thrown_exceptions_do (exception)
b9f7e36c
APB
15332 tree exception;
15333{
15334 tree list = currently_caught_type_list;
c877974e 15335 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15336 /* First, all the nested try-catch-finally at that stage. The
15337 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15338 if (IS_UNCHECKED_EXCEPTION_P (exception))
15339 return 1;
b9f7e36c
APB
15340 while (list)
15341 {
15342 tree caught;
15343 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15344 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15345 return 1;
15346 list = TREE_CHAIN (list);
15347 }
15348 return 0;
15349}
15350
15351static void
15352purge_unchecked_exceptions (mdecl)
15353 tree mdecl;
15354{
15355 tree throws = DECL_FUNCTION_THROWS (mdecl);
15356 tree new = NULL_TREE;
15357
15358 while (throws)
15359 {
15360 tree next = TREE_CHAIN (throws);
c877974e 15361 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15362 {
15363 TREE_CHAIN (throws) = new;
15364 new = throws;
15365 }
15366 throws = next;
15367 }
15368 /* List is inverted here, but it doesn't matter */
15369 DECL_FUNCTION_THROWS (mdecl) = new;
15370}
22eed1e6
APB
15371
15372/* 15.24 Conditional Operator ?: */
15373
15374static tree
15375patch_conditional_expr (node, wfl_cond, wfl_op1)
15376 tree node, wfl_cond, wfl_op1;
15377{
15378 tree cond = TREE_OPERAND (node, 0);
15379 tree op1 = TREE_OPERAND (node, 1);
15380 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15381 tree resulting_type = NULL_TREE;
ac825856 15382 tree t1, t2, patched;
22eed1e6
APB
15383 int error_found = 0;
15384
ac825856
APB
15385 /* Operands of ?: might be StringBuffers crafted as a result of a
15386 string concatenation. Obtain a descent operand here. */
15387 if ((patched = patch_string (op1)))
15388 TREE_OPERAND (node, 1) = op1 = patched;
15389 if ((patched = patch_string (op2)))
15390 TREE_OPERAND (node, 2) = op2 = patched;
15391
15392 t1 = TREE_TYPE (op1);
15393 t2 = TREE_TYPE (op2);
15394
22eed1e6
APB
15395 /* The first expression must be a boolean */
15396 if (TREE_TYPE (cond) != boolean_type_node)
15397 {
15398 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15399 parse_error_context (wfl_operator,
15400 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15401 lang_printable_name (TREE_TYPE (cond), 0));
15402 error_found = 1;
15403 }
15404
15405 /* Second and third can be numeric, boolean (i.e. primitive),
15406 references or null. Anything else results in an error */
15407 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15408 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15409 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15410 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15411 error_found = 1;
15412
15413 /* Determine the type of the conditional expression. Same types are
15414 easy to deal with */
15415 else if (t1 == t2)
15416 resulting_type = t1;
15417
15418 /* There are different rules for numeric types */
15419 else if (JNUMERIC_TYPE_P (t1))
15420 {
15421 /* if byte/short found, the resulting type is short */
15422 if ((t1 == byte_type_node && t2 == short_type_node)
15423 || (t1 == short_type_node && t2 == byte_type_node))
15424 resulting_type = short_type_node;
15425
15426 /* If t1 is a constant int and t2 is of type byte, short or char
15427 and t1's value fits in t2, then the resulting type is t2 */
15428 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15429 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15430 resulting_type = t2;
15431
15432 /* If t2 is a constant int and t1 is of type byte, short or char
15433 and t2's value fits in t1, then the resulting type is t1 */
15434 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15435 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15436 resulting_type = t1;
15437
15438 /* Otherwise, binary numeric promotion is applied and the
15439 resulting type is the promoted type of operand 1 and 2 */
15440 else
93024893 15441 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15442 &TREE_OPERAND (node, 1),
15443 &TREE_OPERAND (node, 2));
15444 }
15445
15446 /* Cases of a reference and a null type */
15447 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15448 resulting_type = t1;
15449
15450 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15451 resulting_type = t2;
15452
15453 /* Last case: different reference types. If a type can be converted
15454 into the other one by assignment conversion, the latter
15455 determines the type of the expression */
15456 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15457 resulting_type = promote_type (t1);
15458
15459 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15460 resulting_type = promote_type (t2);
15461
15462 /* If we don't have any resulting type, we're in trouble */
15463 if (!resulting_type)
15464 {
c2e3db92 15465 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15466 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15467 parse_error_context (wfl_operator,
15468 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15469 t, lang_printable_name (t2, 0));
22eed1e6
APB
15470 free (t);
15471 error_found = 1;
15472 }
15473
15474 if (error_found)
15475 {
15476 TREE_TYPE (node) = error_mark_node;
15477 return error_mark_node;
15478 }
15479
15480 TREE_TYPE (node) = resulting_type;
15481 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15482 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15483 return node;
15484}
ac825856 15485
5b09b33e
PB
15486/* Try to constant fold NODE.
15487 If NODE is not a constant expression, return NULL_EXPR.
15488 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15489
15490static tree
15491fold_constant_for_init (node, context)
15492 tree node;
15493 tree context;
15494{
15495 tree op0, op1, val;
15496 enum tree_code code = TREE_CODE (node);
15497
ee97d354 15498 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15499 return node;
93024893 15500
5b09b33e
PB
15501 switch (code)
15502 {
5b09b33e
PB
15503 case PLUS_EXPR:
15504 case MINUS_EXPR:
bc3ca41b
PB
15505 case MULT_EXPR:
15506 case TRUNC_MOD_EXPR:
15507 case RDIV_EXPR:
5b09b33e
PB
15508 case LSHIFT_EXPR:
15509 case RSHIFT_EXPR:
15510 case URSHIFT_EXPR:
15511 case BIT_AND_EXPR:
15512 case BIT_XOR_EXPR:
15513 case BIT_IOR_EXPR:
5b09b33e
PB
15514 case TRUTH_ANDIF_EXPR:
15515 case TRUTH_ORIF_EXPR:
15516 case EQ_EXPR:
15517 case NE_EXPR:
15518 case GT_EXPR:
15519 case GE_EXPR:
15520 case LT_EXPR:
15521 case LE_EXPR:
15522 op0 = TREE_OPERAND (node, 0);
15523 op1 = TREE_OPERAND (node, 1);
15524 val = fold_constant_for_init (op0, context);
15525 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15526 return NULL_TREE;
15527 TREE_OPERAND (node, 0) = val;
15528 val = fold_constant_for_init (op1, context);
15529 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15530 return NULL_TREE;
15531 TREE_OPERAND (node, 1) = val;
15532 return patch_binop (node, op0, op1);
15533
15534 case UNARY_PLUS_EXPR:
15535 case NEGATE_EXPR:
15536 case TRUTH_NOT_EXPR:
15537 case BIT_NOT_EXPR:
15538 case CONVERT_EXPR:
15539 op0 = TREE_OPERAND (node, 0);
15540 val = fold_constant_for_init (op0, context);
15541 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15542 return NULL_TREE;
15543 TREE_OPERAND (node, 0) = val;
5a005d9e 15544 return patch_unaryop (node, op0);
5b09b33e
PB
15545 break;
15546
15547 case COND_EXPR:
15548 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15549 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15550 return NULL_TREE;
15551 TREE_OPERAND (node, 0) = val;
15552 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15553 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15554 return NULL_TREE;
15555 TREE_OPERAND (node, 1) = val;
15556 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15557 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15558 return NULL_TREE;
15559 TREE_OPERAND (node, 2) = val;
15560 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15561 : TREE_OPERAND (node, 2);
15562
15563 case VAR_DECL:
8576f094
APB
15564 case FIELD_DECL:
15565 if (! FIELD_FINAL (node)
5b09b33e
PB
15566 || DECL_INITIAL (node) == NULL_TREE)
15567 return NULL_TREE;
15568 val = DECL_INITIAL (node);
15569 /* Guard against infinite recursion. */
15570 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15571 val = fold_constant_for_init (val, node);
5b09b33e 15572 DECL_INITIAL (node) = val;
c7303e41
APB
15573 if (!val && CLASS_FINAL_VARIABLE_P (node))
15574 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15575 return val;
15576
15577 case EXPR_WITH_FILE_LOCATION:
15578 /* Compare java_complete_tree and resolve_expression_name. */
15579 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15580 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15581 {
15582 tree name = EXPR_WFL_NODE (node);
15583 tree decl;
15584 if (PRIMARY_P (node))
15585 return NULL_TREE;
15586 else if (! QUALIFIED_P (name))
15587 {
15588 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15589 if (decl == NULL_TREE
15590 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15591 return NULL_TREE;
15592 return fold_constant_for_init (decl, decl);
15593 }
15594 else
15595 {
5b09b33e
PB
15596 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15597 qualify_ambiguous_name (node);
15598 if (resolve_field_access (node, &decl, NULL)
15599 && decl != NULL_TREE)
15600 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15601 return NULL_TREE;
15602 }
15603 }
15604 else
15605 {
15606 op0 = TREE_OPERAND (node, 0);
15607 val = fold_constant_for_init (op0, context);
15608 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15609 return NULL_TREE;
15610 TREE_OPERAND (node, 0) = val;
15611 return val;
15612 }
15613
bc3ca41b
PB
15614#ifdef USE_COMPONENT_REF
15615 case IDENTIFIER:
15616 case COMPONENT_REF:
15617 ?;
15618#endif
15619
5b09b33e
PB
15620 default:
15621 return NULL_TREE;
15622 }
15623}
bc3ca41b
PB
15624
15625#ifdef USE_COMPONENT_REF
15626/* Context is 'T' for TypeName, 'P' for PackageName,
15627 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15628
15629tree
15630resolve_simple_name (name, context)
15631 tree name;
15632 int context;
15633{
15634}
15635
15636tree
15637resolve_qualified_name (name, context)
15638 tree name;
15639 int context;
15640{
15641}
15642#endif
f15b9af9
MM
15643
15644/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15645
15646static void
15647mark_parser_ctxt (p)
15648 void *p;
15649{
15650 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15651 int i;
15652
15653 if (!pc)
15654 return;
15655
15656#ifndef JC1_LITE
15657 for (i = 0; i < 11; ++i)
15658 ggc_mark_tree (pc->modifier_ctx[i]);
15659 ggc_mark_tree (pc->class_type);
15660 ggc_mark_tree (pc->function_decl);
15661 ggc_mark_tree (pc->package);
f15b9af9
MM
15662 ggc_mark_tree (pc->class_list);
15663 ggc_mark_tree (pc->current_parsed_class);
15664 ggc_mark_tree (pc->current_parsed_class_un);
15665 ggc_mark_tree (pc->non_static_initialized);
15666 ggc_mark_tree (pc->static_initialized);
15667 ggc_mark_tree (pc->instance_initializers);
15668 ggc_mark_tree (pc->import_list);
15669 ggc_mark_tree (pc->import_demand_list);
15670 ggc_mark_tree (pc->current_loop);
15671 ggc_mark_tree (pc->current_labeled_block);
15672#endif /* JC1_LITE */
15673
15674 if (pc->next)
15675 mark_parser_ctxt (&pc->next);
15676}
fea2d5da
PB
15677
15678void
15679init_src_parse ()
15680{
15681 /* Register roots with the garbage collector. */
15682 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
15683}
This page took 2.860205 seconds and 5 git commands to generate.