]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
i386.md (movsfcc_1): Support integer cmove instruction.
[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));
119static void expand_start_java_method PARAMS ((tree));
120static tree find_name_in_single_imports PARAMS ((tree));
121static void check_abstract_method_header PARAMS ((tree));
122static tree lookup_java_interface_method2 PARAMS ((tree, tree));
123static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 124static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 125static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 126 tree, tree, tree));
df32d2ce 127static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 128 int *, tree *));
df32d2ce
KG
129static int breakdown_qualified PARAMS ((tree *, tree *, tree));
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));
155static tree enter_a_block PARAMS ((tree));
156static tree exit_block PARAMS ((void));
157static tree lookup_name_in_blocks PARAMS ((tree));
158static void maybe_absorb_scoping_blocks PARAMS ((void));
159static tree build_method_invocation PARAMS ((tree, tree));
160static tree build_new_invocation PARAMS ((tree, tree));
161static tree build_assignment PARAMS ((int, int, tree, tree));
162static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
163static int check_final_assignment PARAMS ((tree ,tree));
164static tree patch_assignment PARAMS ((tree, tree, tree ));
165static tree patch_binop PARAMS ((tree, tree, tree));
166static tree build_unaryop PARAMS ((int, int, tree));
167static tree build_incdec PARAMS ((int, int, tree, int));
168static tree patch_unaryop PARAMS ((tree, tree));
169static tree build_cast PARAMS ((int, tree, tree));
170static tree build_null_of_type PARAMS ((tree));
171static tree patch_cast PARAMS ((tree, tree));
172static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
173static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
174static int valid_cast_to_p PARAMS ((tree, tree));
175static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
176static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
177static tree try_reference_assignconv PARAMS ((tree, tree));
178static tree build_unresolved_array_type PARAMS ((tree));
179static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
180static tree build_array_ref PARAMS ((int, tree, tree));
181static tree patch_array_ref PARAMS ((tree));
182static tree make_qualified_name PARAMS ((tree, tree, int));
183static tree merge_qualified_name PARAMS ((tree, tree));
184static tree make_qualified_primary PARAMS ((tree, tree, int));
185static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 186 tree *, tree *));
df32d2ce 187static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
188static tree resolve_field_access PARAMS ((tree, tree *, tree *));
189static tree build_newarray_node PARAMS ((tree, tree, int));
190static tree patch_newarray PARAMS ((tree));
191static tree resolve_type_during_patch PARAMS ((tree));
192static tree build_this PARAMS ((int));
9a7ab4b3 193static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
194static tree build_return PARAMS ((int, tree));
195static tree patch_return PARAMS ((tree));
196static tree maybe_access_field PARAMS ((tree, tree, tree));
197static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
198static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199 tree, tree));
df32d2ce
KG
200static int not_accessible_p PARAMS ((tree, tree, int));
201static void check_deprecation PARAMS ((tree, tree));
202static int class_in_current_package PARAMS ((tree));
203static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
204static tree patch_if_else_statement PARAMS ((tree));
205static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
206static tree add_stmt_to_block PARAMS ((tree, tree, tree));
207static tree patch_exit_expr PARAMS ((tree));
208static tree build_labeled_block PARAMS ((int, tree));
209static tree finish_labeled_statement PARAMS ((tree, tree));
210static tree build_bc_statement PARAMS ((int, int, tree));
211static tree patch_bc_statement PARAMS ((tree));
212static tree patch_loop_statement PARAMS ((tree));
213static tree build_new_loop PARAMS ((tree));
214static tree build_loop_body PARAMS ((int, tree, int));
215static tree finish_loop_body PARAMS ((int, tree, tree, int));
216static tree build_debugable_stmt PARAMS ((int, tree));
217static tree finish_for_loop PARAMS ((int, tree, tree, tree));
218static tree patch_switch_statement PARAMS ((tree));
219static tree string_constant_concatenation PARAMS ((tree, tree));
220static tree build_string_concatenation PARAMS ((tree, tree));
221static tree patch_string_cst PARAMS ((tree));
222static tree patch_string PARAMS ((tree));
223static tree build_try_statement PARAMS ((int, tree, tree));
224static tree build_try_finally_statement PARAMS ((int, tree, tree));
225static tree patch_try_statement PARAMS ((tree));
226static tree patch_synchronized_statement PARAMS ((tree, tree));
227static tree patch_throw_statement PARAMS ((tree, tree));
228static void check_thrown_exceptions PARAMS ((int, tree));
229static int check_thrown_exceptions_do PARAMS ((tree));
230static void purge_unchecked_exceptions PARAMS ((tree));
231static void check_throws_clauses PARAMS ((tree, tree, tree));
232static void finish_method_declaration PARAMS ((tree));
233static tree build_super_invocation PARAMS ((tree));
234static int verify_constructor_circularity PARAMS ((tree, tree));
235static char *constructor_circularity_msg PARAMS ((tree, tree));
236static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 237 int, int));
df32d2ce
KG
238static const char *get_printable_method_name PARAMS ((tree));
239static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
240static tree generate_finit PARAMS ((tree));
241static void add_instance_initializer PARAMS ((tree));
df32d2ce 242static void fix_constructors PARAMS ((tree));
c2952b01
APB
243static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244 tree, int *));
245static void craft_constructor PARAMS ((tree, tree));
246static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
247static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248static void start_artificial_method_body PARAMS ((tree));
249static void end_artificial_method_body PARAMS ((tree));
250static int check_method_redefinition PARAMS ((tree, tree));
165f37bc 251static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
252static void java_check_regular_methods PARAMS ((tree));
253static void java_check_abstract_methods PARAMS ((tree));
254static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
255static void unreachable_stmt_error PARAMS ((tree));
256static tree find_expr_with_wfl PARAMS ((tree));
257static void missing_return_error PARAMS ((tree));
258static tree build_new_array_init PARAMS ((int, tree));
259static tree patch_new_array_init PARAMS ((tree, tree));
260static tree maybe_build_array_element_wfl PARAMS ((tree));
261static int array_constructor_check_entry PARAMS ((tree, tree));
262static const char *purify_type_name PARAMS ((const char *));
263static tree fold_constant_for_init PARAMS ((tree, tree));
264static tree strip_out_static_field_access_decl PARAMS ((tree));
265static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
266static void static_ref_err PARAMS ((tree, tree, tree));
267static void parser_add_interface PARAMS ((tree, tree, tree));
268static void add_superinterfaces PARAMS ((tree, tree));
269static tree jdep_resolve_class PARAMS ((jdep *));
270static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
271static void java_complete_expand_classes PARAMS ((void));
272static void java_complete_expand_class PARAMS ((tree));
273static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
274static tree cut_identifier_in_qualified PARAMS ((tree));
275static tree java_stabilize_reference PARAMS ((tree));
276static tree do_unary_numeric_promotion PARAMS ((tree));
277static char * operator_string PARAMS ((tree));
278static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
279static tree merge_string_cste PARAMS ((tree, tree, int));
280static tree java_refold PARAMS ((tree));
281static int java_decl_equiv PARAMS ((tree, tree));
282static int binop_compound_p PARAMS ((enum tree_code));
283static tree search_loop PARAMS ((tree));
284static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 285static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
286static void java_check_abstract_method_definitions PARAMS ((tree));
287static void java_debug_context_do PARAMS ((int));
c2952b01
APB
288static void java_parser_context_push_initialized_field PARAMS ((void));
289static void java_parser_context_pop_initialized_field PARAMS ((void));
290static tree reorder_static_initialized PARAMS ((tree));
291static void java_parser_context_suspend PARAMS ((void));
292static void java_parser_context_resume PARAMS ((void));
c7303e41 293static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
294
295/* JDK 1.1 work. FIXME */
296
297static tree maybe_make_nested_class_name PARAMS ((tree));
298static void make_nested_class_name PARAMS ((tree));
299static void set_nested_class_simple_name_value PARAMS ((tree, int));
300static void link_nested_class_to_enclosing PARAMS ((void));
301static tree find_as_inner_class PARAMS ((tree, tree, tree));
302static tree find_as_inner_class_do PARAMS ((tree, tree));
303static int check_inner_class_redefinition PARAMS ((tree, tree));
304
305static tree build_thisn_assign PARAMS ((void));
306static tree build_current_thisn PARAMS ((tree));
307static tree build_access_to_thisn PARAMS ((tree, tree, int));
308static tree maybe_build_thisn_access_method PARAMS ((tree));
309
310static tree build_outer_field_access PARAMS ((tree, tree));
311static tree build_outer_field_access_methods PARAMS ((tree));
312static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
313 tree, tree));
314static tree build_outer_method_access_method PARAMS ((tree));
315static tree build_new_access_id PARAMS ((void));
316static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
317 tree, tree));
318
319static int outer_field_access_p PARAMS ((tree, tree));
320static int outer_field_expanded_access_p PARAMS ((tree, tree *,
321 tree *, tree *));
322static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323static tree build_incomplete_class_ref PARAMS ((int, tree));
324static tree patch_incomplete_class_ref PARAMS ((tree));
325static tree create_anonymous_class PARAMS ((int, tree));
326static void patch_anonymous_class PARAMS ((tree, tree, tree));
327static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 328
165f37bc
APB
329static tree build_dot_class_method PARAMS ((tree));
330static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 331static void create_new_parser_context PARAMS ((int));
f15b9af9 332static void mark_parser_ctxt PARAMS ((void *));
165f37bc 333
e04a16fb
AG
334/* Number of error found so far. */
335int java_error_count;
336/* Number of warning found so far. */
337int java_warning_count;
ce6e9147
APB
338/* Tell when not to fold, when doing xrefs */
339int do_not_fold;
c2952b01 340/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 341const char *cyclic_inheritance_report;
f15b9af9 342
c2952b01
APB
343/* Tell when we're within an instance initializer */
344static int in_instance_initializer;
e04a16fb
AG
345
346/* The current parser context */
d4370213 347struct parser_ctxt *ctxp;
e04a16fb 348
d4370213 349/* List of things that were analyzed for which code will be generated */
b351b287
APB
350static struct parser_ctxt *ctxp_for_generation = NULL;
351
e04a16fb
AG
352/* binop_lookup maps token to tree_code. It is used where binary
353 operations are involved and required by the parser. RDIV_EXPR
354 covers both integral/floating point division. The code is changed
355 once the type of both operator is worked out. */
356
357static enum tree_code binop_lookup[19] =
358 {
359 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
360 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
361 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
362 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
363 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
364 };
365#define BINOP_LOOKUP(VALUE) \
6e2aa220 366 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 367
5cbdba64
APB
368/* This is the end index for binary operators that can also be used
369 in compound assignements. */
370#define BINOP_COMPOUND_CANDIDATES 11
371
e04a16fb 372/* The "$L" identifier we use to create labels. */
b67d701b
PB
373static tree label_id = NULL_TREE;
374
375/* The "StringBuffer" identifier used for the String `+' operator. */
376static tree wfl_string_buffer = NULL_TREE;
377
378/* The "append" identifier used for String `+' operator. */
379static tree wfl_append = NULL_TREE;
380
381/* The "toString" identifier used for String `+' operator. */
382static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
383
384/* The "java.lang" import qualified name. */
385static tree java_lang_id = NULL_TREE;
09ed0f70 386
c2952b01
APB
387/* The generated `inst$' identifier used for generated enclosing
388 instance/field access functions. */
389static tree inst_id = NULL_TREE;
390
09ed0f70
APB
391/* The "java.lang.Cloneable" qualified name. */
392static tree java_lang_cloneable = NULL_TREE;
f099f336 393
ee17a290
TT
394/* The "java.io.Serializable" qualified name. */
395static tree java_io_serializable = NULL_TREE;
396
f099f336
APB
397/* Context and flag for static blocks */
398static tree current_static_block = NULL_TREE;
399
c2952b01
APB
400/* The generated `write_parm_value$' identifier. */
401static tree wpv_id;
402
ee07f4f4
APB
403/* The list of all packages we've seen so far */
404static tree package_list = NULL_TREE;
2884c41e 405
19e223db
MM
406/* Hold THIS for the scope of the current public method decl. */
407static tree current_this;
408
409/* Hold a list of catch clauses list. The first element of this list is
410 the list of the catch clauses of the currently analysed try block. */
411static tree currently_caught_type_list;
412
2884c41e
KG
413/* Check modifiers. If one doesn't fit, retrieve it in its declaration
414 line and point it out. */
415/* Should point out the one that don't fit. ASCII/unicode, going
416 backward. FIXME */
417
418#define check_modifiers(__message, __value, __mask) do { \
419 if ((__value) & ~(__mask)) \
420 { \
421 int i, remainder = (__value) & ~(__mask); \
422 for (i = 0; i <= 10; i++) \
423 if ((1 << i) & remainder) \
424 parse_error_context (ctxp->modifier_ctx [i], (__message), \
425 java_accstring_lookup (1 << i)); \
426 } \
427} while (0)
ee07f4f4 428
e04a16fb
AG
429%}
430
431%union {
432 tree node;
433 int sub_token;
434 struct {
435 int token;
436 int location;
437 } operator;
438 int value;
439}
440
9ee9b555
KG
441%{
442#include "lex.c"
443%}
444
e04a16fb
AG
445%pure_parser
446
447/* Things defined here have to match the order of what's in the
448 binop_lookup table. */
449
450%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
451%token LS_TK SRS_TK ZRS_TK
452%token AND_TK XOR_TK OR_TK
453%token BOOL_AND_TK BOOL_OR_TK
454%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
455
456/* This maps to the same binop_lookup entry than the token above */
457
458%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
459%token REM_ASSIGN_TK
460%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
461%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
462
463
464/* Modifier TOKEN have to be kept in this order. Don't scramble it */
465
466%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
467%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
468%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
469%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 470%token STRICT_TK
e04a16fb
AG
471
472/* Keep those two in order, too */
473%token DECR_TK INCR_TK
474
475/* From now one, things can be in any order */
476
477%token DEFAULT_TK IF_TK THROW_TK
478%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
479%token THROWS_TK BREAK_TK IMPORT_TK
480%token ELSE_TK INSTANCEOF_TK RETURN_TK
481%token VOID_TK CATCH_TK INTERFACE_TK
482%token CASE_TK EXTENDS_TK FINALLY_TK
483%token SUPER_TK WHILE_TK CLASS_TK
484%token SWITCH_TK CONST_TK TRY_TK
485%token FOR_TK NEW_TK CONTINUE_TK
486%token GOTO_TK PACKAGE_TK THIS_TK
487
488%token BYTE_TK SHORT_TK INT_TK LONG_TK
489%token CHAR_TK INTEGRAL_TK
490
491%token FLOAT_TK DOUBLE_TK FP_TK
492
493%token ID_TK
494
495%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
496
497%token ASSIGN_ANY_TK ASSIGN_TK
498%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
499
500%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
501%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
502
c2952b01 503%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
504
505%type <node> super ID_TK identifier
506%type <node> name simple_name qualified_name
c2952b01 507%type <node> type_declaration compilation_unit
e04a16fb
AG
508 field_declaration method_declaration extends_interfaces
509 interfaces interface_type_list
c2952b01 510 class_member_declaration
e04a16fb
AG
511 import_declarations package_declaration
512 type_declarations interface_body
513 interface_member_declaration constant_declaration
514 interface_member_declarations interface_type
515 abstract_method_declaration interface_type_list
516%type <node> class_body_declaration class_member_declaration
517 static_initializer constructor_declaration block
22eed1e6 518%type <node> class_body_declarations constructor_header
e04a16fb
AG
519%type <node> class_or_interface_type class_type class_type_list
520 constructor_declarator explicit_constructor_invocation
b9f7e36c 521%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
522
523%type <node> variable_declarator_id variable_declarator
524 variable_declarators variable_initializer
22eed1e6 525 variable_initializers constructor_body
ac825856 526 array_initializer
e04a16fb 527
2e5eb5c5 528%type <node> class_body block_end constructor_block_end
e04a16fb
AG
529%type <node> statement statement_without_trailing_substatement
530 labeled_statement if_then_statement label_decl
531 if_then_else_statement while_statement for_statement
532 statement_nsi labeled_statement_nsi do_statement
533 if_then_else_statement_nsi while_statement_nsi
534 for_statement_nsi statement_expression_list for_init
535 for_update statement_expression expression_statement
536 primary_no_new_array expression primary
537 array_creation_expression array_type
538 class_instance_creation_expression field_access
539 method_invocation array_access something_dot_new
540 argument_list postfix_expression while_expression
541 post_increment_expression post_decrement_expression
542 unary_expression_not_plus_minus unary_expression
543 pre_increment_expression pre_decrement_expression
544 unary_expression_not_plus_minus cast_expression
545 multiplicative_expression additive_expression
546 shift_expression relational_expression
547 equality_expression and_expression
548 exclusive_or_expression inclusive_or_expression
549 conditional_and_expression conditional_or_expression
550 conditional_expression assignment_expression
551 left_hand_side assignment for_header for_begin
552 constant_expression do_statement_begin empty_statement
b67d701b 553 switch_statement synchronized_statement throw_statement
f8976021 554 try_statement switch_expression switch_block
15fdcfe9 555 catches catch_clause catch_clause_parameter finally
c2952b01 556 anonymous_class_creation
e04a16fb
AG
557%type <node> return_statement break_statement continue_statement
558
559%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
560%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
561%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
562%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
563%type <operator> ASSIGN_ANY_TK assignment_operator
564%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
565%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
566%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 567%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 568%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
569%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
570%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 571%type <operator> NEW_TK
e04a16fb
AG
572
573%type <node> method_body
574
575%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
576 STRING_LIT_TK NULL_TK VOID_TK
577
578%type <node> IF_TK WHILE_TK FOR_TK
579
580%type <node> formal_parameter_list formal_parameter
581 method_declarator method_header
582
c2952b01 583%type <node> primitive_type reference_type type
e04a16fb
AG
584 BOOLEAN_TK INTEGRAL_TK FP_TK
585
c2952b01 586/* Added or modified JDK 1.1 rule types */
c7303e41 587%type <node> type_literals
c2952b01 588
e04a16fb
AG
589%%
590/* 19.2 Production from 2.3: The Syntactic Grammar */
591goal:
19e223db
MM
592 {
593 /* Register static variables with the garbage
594 collector. */
595 ggc_add_tree_root (&label_id, 1);
596 ggc_add_tree_root (&wfl_string_buffer, 1);
597 ggc_add_tree_root (&wfl_append, 1);
598 ggc_add_tree_root (&wfl_to_string, 1);
599 ggc_add_tree_root (&java_lang_id, 1);
600 ggc_add_tree_root (&inst_id, 1);
601 ggc_add_tree_root (&java_lang_cloneable, 1);
602 ggc_add_tree_root (&java_io_serializable, 1);
603 ggc_add_tree_root (&current_static_block, 1);
604 ggc_add_tree_root (&wpv_id, 1);
605 ggc_add_tree_root (&package_list, 1);
606 ggc_add_tree_root (&current_this, 1);
607 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
608 ggc_add_root (&ctxp, 1,
609 sizeof (struct parser_ctxt *),
610 mark_parser_ctxt);
611 ggc_add_root (&ctxp_for_generation, 1,
612 sizeof (struct parser_ctxt *),
613 mark_parser_ctxt);
19e223db 614 }
e04a16fb
AG
615 compilation_unit
616 {}
617;
618
619/* 19.3 Productions from 3: Lexical structure */
620literal:
621 INT_LIT_TK
622| FP_LIT_TK
623| BOOL_LIT_TK
624| CHAR_LIT_TK
625| STRING_LIT_TK
626| NULL_TK
627;
628
629/* 19.4 Productions from 4: Types, Values and Variables */
630type:
631 primitive_type
632| reference_type
633;
634
635primitive_type:
636 INTEGRAL_TK
637| FP_TK
638| BOOLEAN_TK
639;
640
641reference_type:
642 class_or_interface_type
643| array_type
644;
645
646class_or_interface_type:
647 name
648;
649
650class_type:
651 class_or_interface_type /* Default rule */
652;
653
654interface_type:
655 class_or_interface_type
656;
657
658array_type:
c7303e41 659 primitive_type dims
e04a16fb 660 {
c7303e41
APB
661 int osb = pop_current_osb (ctxp);
662 tree t = build_java_array_type (($1), -1);
663 CLASS_LOADED_P (t) = 1;
664 while (--osb)
665 t = build_unresolved_array_type (t);
666 $$ = t;
667 }
668| name dims
669 {
670 int osb = pop_current_osb (ctxp);
671 tree t = $1;
672 while (osb--)
673 t = build_unresolved_array_type (t);
674 $$ = t;
e04a16fb 675 }
e04a16fb
AG
676;
677
678/* 19.5 Productions from 6: Names */
679name:
680 simple_name /* Default rule */
681| qualified_name /* Default rule */
682;
683
684simple_name:
685 identifier /* Default rule */
686;
687
688qualified_name:
689 name DOT_TK identifier
690 { $$ = make_qualified_name ($1, $3, $2.location); }
691;
692
693identifier:
694 ID_TK
695;
696
697/* 19.6: Production from 7: Packages */
698compilation_unit:
699 {$$ = NULL;}
700| package_declaration
701| import_declarations
702| type_declarations
703| package_declaration import_declarations
704| package_declaration type_declarations
705| import_declarations type_declarations
706| package_declaration import_declarations type_declarations
707;
708
709import_declarations:
710 import_declaration
711 {
712 $$ = NULL;
713 }
714| import_declarations import_declaration
715 {
716 $$ = NULL;
717 }
718;
719
720type_declarations:
721 type_declaration
722| type_declarations type_declaration
723;
724
725package_declaration:
726 PACKAGE_TK name SC_TK
ee07f4f4
APB
727 {
728 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 729 register_package (ctxp->package);
ee07f4f4 730 }
e04a16fb
AG
731| PACKAGE_TK error
732 {yyerror ("Missing name"); RECOVER;}
733| PACKAGE_TK name error
734 {yyerror ("';' expected"); RECOVER;}
735;
736
737import_declaration:
738 single_type_import_declaration
739| type_import_on_demand_declaration
740;
741
742single_type_import_declaration:
743 IMPORT_TK name SC_TK
744 {
9a7ab4b3 745 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 746 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 747 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
748 while (last != IDENTIFIER_POINTER (name))
749 {
750 if (last [0] == '.')
751 break;
752 last--;
753 }
754 last_name = get_identifier (++last);
755 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
756 {
757 tree err = find_name_in_single_imports (last_name);
758 if (err && err != name)
759 parse_error_context
760 ($2, "Ambiguous class: `%s' and `%s'",
761 IDENTIFIER_POINTER (name),
762 IDENTIFIER_POINTER (err));
5e942c50 763 else
9a7ab4b3 764 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
765 }
766 else
5e942c50 767 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
768 }
769| IMPORT_TK error
770 {yyerror ("Missing name"); RECOVER;}
771| IMPORT_TK name error
772 {yyerror ("';' expected"); RECOVER;}
773;
774
775type_import_on_demand_declaration:
776 IMPORT_TK name DOT_TK MULT_TK SC_TK
777 {
778 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
779 /* Don't import java.lang.* twice. */
780 if (name != java_lang_id)
781 {
ba179f9f 782 read_import_dir ($2);
9a7ab4b3
APB
783 ctxp->import_demand_list =
784 chainon (ctxp->import_demand_list,
785 build_tree_list ($2, NULL_TREE));
ba179f9f 786 }
e04a16fb
AG
787 }
788| IMPORT_TK name DOT_TK error
789 {yyerror ("'*' expected"); RECOVER;}
790| IMPORT_TK name DOT_TK MULT_TK error
791 {yyerror ("';' expected"); RECOVER;}
792;
793
794type_declaration:
795 class_declaration
c2952b01 796 { end_class_declaration (0); }
e04a16fb 797| interface_declaration
c2952b01 798 { end_class_declaration (0); }
5f1c312a 799| empty_statement
e04a16fb
AG
800| error
801 {
802 YYERROR_NOW;
803 yyerror ("Class or interface declaration expected");
804 }
805;
806
807/* 19.7 Shortened from the original:
808 modifiers: modifier | modifiers modifier
809 modifier: any of public... */
810modifiers:
811 MODIFIER_TK
812 {
813 $$ = (1 << $1);
814 }
815| modifiers MODIFIER_TK
816 {
817 int acc = (1 << $2);
818 if ($$ & acc)
819 parse_error_context
820 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
821 java_accstring_lookup (acc));
822 else
823 {
824 $$ |= acc;
825 }
826 }
827;
828
829/* 19.8.1 Production from $8.1: Class Declaration */
830class_declaration:
831 modifiers CLASS_TK identifier super interfaces
832 { create_class ($1, $3, $4, $5); }
833 class_body
e04a16fb
AG
834| CLASS_TK identifier super interfaces
835 { create_class (0, $2, $3, $4); }
836 class_body
e04a16fb
AG
837| modifiers CLASS_TK error
838 {yyerror ("Missing class name"); RECOVER;}
839| CLASS_TK error
840 {yyerror ("Missing class name"); RECOVER;}
841| CLASS_TK identifier error
0b4d333e
APB
842 {
843 if (!ctxp->class_err) yyerror ("'{' expected");
844 DRECOVER(class1);
845 }
e04a16fb
AG
846| modifiers CLASS_TK identifier error
847 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
848;
849
850super:
851 { $$ = NULL; }
852| EXTENDS_TK class_type
853 { $$ = $2; }
854| EXTENDS_TK class_type error
855 {yyerror ("'{' expected"); ctxp->class_err=1;}
856| EXTENDS_TK error
857 {yyerror ("Missing super class name"); ctxp->class_err=1;}
858;
859
860interfaces:
861 { $$ = NULL_TREE; }
862| IMPLEMENTS_TK interface_type_list
863 { $$ = $2; }
864| IMPLEMENTS_TK error
865 {
866 ctxp->class_err=1;
867 yyerror ("Missing interface name");
868 }
869;
870
871interface_type_list:
872 interface_type
873 {
874 ctxp->interface_number = 1;
875 $$ = build_tree_list ($1, NULL_TREE);
876 }
877| interface_type_list C_TK interface_type
878 {
879 ctxp->interface_number++;
880 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
881 }
882| interface_type_list C_TK error
883 {yyerror ("Missing interface name"); RECOVER;}
884;
885
886class_body:
887 OCB_TK CCB_TK
7f10c2e2
APB
888 {
889 /* Store the location of the `}' when doing xrefs */
890 if (flag_emit_xref)
c2952b01 891 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 892 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 893 $$ = GET_CPC ();
7f10c2e2 894 }
e04a16fb 895| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
896 {
897 /* Store the location of the `}' when doing xrefs */
898 if (flag_emit_xref)
c2952b01 899 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 900 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 901 $$ = GET_CPC ();
7f10c2e2 902 }
e04a16fb
AG
903;
904
905class_body_declarations:
906 class_body_declaration
907| class_body_declarations class_body_declaration
908;
909
910class_body_declaration:
911 class_member_declaration
912| static_initializer
913| constructor_declaration
914| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
915 {
916 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
917 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
918 }
e04a16fb
AG
919;
920
921class_member_declaration:
922 field_declaration
923| method_declaration
924| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
925 { end_class_declaration (1); }
926| interface_declaration /* Added, JDK1.1 inner interfaces */
927 { end_class_declaration (1); }
5f1c312a 928| empty_statement
e04a16fb
AG
929;
930
931/* 19.8.2 Productions from 8.3: Field Declarations */
932field_declaration:
933 type variable_declarators SC_TK
934 { register_fields (0, $1, $2); }
935| modifiers type variable_declarators SC_TK
936 {
e04a16fb
AG
937 check_modifiers
938 ("Illegal modifier `%s' for field declaration",
939 $1, FIELD_MODIFIERS);
940 check_modifiers_consistency ($1);
941 register_fields ($1, $2, $3);
942 }
943;
944
945variable_declarators:
946 /* Should we use build_decl_list () instead ? FIXME */
947 variable_declarator /* Default rule */
948| variable_declarators C_TK variable_declarator
949 { $$ = chainon ($1, $3); }
950| variable_declarators C_TK error
951 {yyerror ("Missing term"); RECOVER;}
952;
953
954variable_declarator:
955 variable_declarator_id
956 { $$ = build_tree_list ($1, NULL_TREE); }
957| variable_declarator_id ASSIGN_TK variable_initializer
958 {
959 if (java_error_count)
960 $3 = NULL_TREE;
961 $$ = build_tree_list
962 ($1, build_assignment ($2.token, $2.location, $1, $3));
963 }
964| variable_declarator_id ASSIGN_TK error
965 {
966 yyerror ("Missing variable initializer");
967 $$ = build_tree_list ($1, NULL_TREE);
968 RECOVER;
969 }
970| variable_declarator_id ASSIGN_TK variable_initializer error
971 {
972 yyerror ("';' expected");
973 $$ = build_tree_list ($1, NULL_TREE);
974 RECOVER;
975 }
976;
977
978variable_declarator_id:
979 identifier
980| variable_declarator_id OSB_TK CSB_TK
c583dd46 981 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
982| identifier error
983 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
984| variable_declarator_id OSB_TK error
29f8b718
APB
985 {
986 tree node = java_lval.node;
987 if (node && (TREE_CODE (node) == INTEGER_CST
988 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
989 yyerror ("Can't specify array dimension in a declaration");
990 else
991 yyerror ("']' expected");
992 DRECOVER(vdi);
993 }
e04a16fb
AG
994| variable_declarator_id CSB_TK error
995 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
996;
997
998variable_initializer:
999 expression
1000| array_initializer
e04a16fb
AG
1001;
1002
1003/* 19.8.3 Productions from 8.4: Method Declarations */
1004method_declaration:
1005 method_header
1006 {
1007 current_function_decl = $1;
c2952b01
APB
1008 if (current_function_decl
1009 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1010 source_start_java_method (current_function_decl);
1011 else
1012 current_function_decl = NULL_TREE;
e04a16fb
AG
1013 }
1014 method_body
b635eb2f 1015 { finish_method_declaration ($3); }
e04a16fb
AG
1016| method_header error
1017 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1018;
1019
1020method_header:
1021 type method_declarator throws
b9f7e36c 1022 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1023| VOID_TK method_declarator throws
b9f7e36c 1024 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1025| modifiers type method_declarator throws
b9f7e36c 1026 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1027| modifiers VOID_TK method_declarator throws
b9f7e36c 1028 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1029| type error
efa0a23f
APB
1030 {
1031 yyerror ("Invalid method declaration, method name required");
1032 RECOVER;
1033 }
e04a16fb
AG
1034| modifiers type error
1035 {RECOVER;}
1036| VOID_TK error
1037 {yyerror ("Identifier expected"); RECOVER;}
1038| modifiers VOID_TK error
1039 {yyerror ("Identifier expected"); RECOVER;}
1040| modifiers error
1041 {
1042 yyerror ("Invalid method declaration, return type required");
1043 RECOVER;
1044 }
1045;
1046
1047method_declarator:
1048 identifier OP_TK CP_TK
c2952b01
APB
1049 {
1050 ctxp->formal_parameter_number = 0;
1051 $$ = method_declarator ($1, NULL_TREE);
1052 }
e04a16fb
AG
1053| identifier OP_TK formal_parameter_list CP_TK
1054 { $$ = method_declarator ($1, $3); }
1055| method_declarator OSB_TK CSB_TK
1056 {
1886c9d8
APB
1057 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1058 TREE_PURPOSE ($1) =
1059 build_unresolved_array_type (TREE_PURPOSE ($1));
1060 parse_warning_context
1061 (wfl_operator,
1062 "Discouraged form of returned type specification");
e04a16fb
AG
1063 }
1064| identifier OP_TK error
1065 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1066| method_declarator OSB_TK error
1067 {yyerror ("']' expected"); RECOVER;}
1068;
1069
1070formal_parameter_list:
1071 formal_parameter
1072 {
1073 ctxp->formal_parameter_number = 1;
1074 }
1075| formal_parameter_list C_TK formal_parameter
1076 {
1077 ctxp->formal_parameter_number += 1;
1078 $$ = chainon ($1, $3);
1079 }
1080| formal_parameter_list C_TK error
c2952b01 1081 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1082;
1083
1084formal_parameter:
1085 type variable_declarator_id
1086 {
1087 $$ = build_tree_list ($2, $1);
1088 }
18990de5 1089| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1090 {
5256aa37 1091 $$ = build_tree_list ($3, $2);
c2952b01 1092 ARG_FINAL_P ($$) = 1;
5256aa37 1093 }
e04a16fb 1094| type error
f8989a66
APB
1095 {
1096 yyerror ("Missing identifier"); RECOVER;
1097 $$ = NULL_TREE;
1098 }
18990de5 1099| final type error
e04a16fb 1100 {
e04a16fb 1101 yyerror ("Missing identifier"); RECOVER;
f8989a66 1102 $$ = NULL_TREE;
e04a16fb
AG
1103 }
1104;
1105
18990de5
JB
1106final:
1107 modifiers
1108 {
1109 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1110 $1, ACC_FINAL);
1111 if ($1 != ACC_FINAL)
1112 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1113 }
1114;
1115
e04a16fb 1116throws:
b9f7e36c 1117 { $$ = NULL_TREE; }
e04a16fb 1118| THROWS_TK class_type_list
b9f7e36c 1119 { $$ = $2; }
e04a16fb
AG
1120| THROWS_TK error
1121 {yyerror ("Missing class type term"); RECOVER;}
1122;
1123
1124class_type_list:
1125 class_type
c877974e 1126 { $$ = build_tree_list ($1, $1); }
e04a16fb 1127| class_type_list C_TK class_type
c877974e 1128 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1129| class_type_list C_TK error
1130 {yyerror ("Missing class type term"); RECOVER;}
1131;
1132
1133method_body:
1134 block
5f1c312a 1135| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1136;
1137
1138/* 19.8.4 Productions from 8.5: Static Initializers */
1139static_initializer:
1140 static block
1141 {
c2952b01
APB
1142 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1143 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1144 current_static_block = NULL_TREE;
e04a16fb 1145 }
e04a16fb
AG
1146;
1147
1148static: /* Test lval.sub_token here */
c2952b01 1149 modifiers
e04a16fb 1150 {
c2952b01
APB
1151 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1152 /* Can't have a static initializer in an innerclass */
1153 if ($1 | ACC_STATIC &&
1154 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1155 parse_error_context
1156 (MODIFIER_WFL (STATIC_TK),
1157 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1158 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1159 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1160 }
1161;
1162
1163/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1164constructor_declaration:
22eed1e6 1165 constructor_header
e04a16fb 1166 {
22eed1e6
APB
1167 current_function_decl = $1;
1168 source_start_java_method (current_function_decl);
e04a16fb 1169 }
22eed1e6 1170 constructor_body
b635eb2f 1171 { finish_method_declaration ($3); }
22eed1e6
APB
1172;
1173
1174constructor_header:
1175 constructor_declarator throws
1176 { $$ = method_header (0, NULL_TREE, $1, $2); }
1177| modifiers constructor_declarator throws
1178 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1179;
1180
1181constructor_declarator:
1182 simple_name OP_TK CP_TK
c2952b01
APB
1183 {
1184 ctxp->formal_parameter_number = 0;
1185 $$ = method_declarator ($1, NULL_TREE);
1186 }
e04a16fb 1187| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1188 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1189;
1190
1191constructor_body:
22eed1e6
APB
1192 /* Unlike regular method, we always need a complete (empty)
1193 body so we can safely perform all the required code
1194 addition (super invocation and field initialization) */
2e5eb5c5 1195 block_begin constructor_block_end
22eed1e6 1196 {
9bbc7d9f 1197 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1198 $$ = $2;
1199 }
2e5eb5c5 1200| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1201 { $$ = $3; }
2e5eb5c5 1202| block_begin block_statements constructor_block_end
22eed1e6 1203 { $$ = $3; }
2e5eb5c5 1204| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1205 { $$ = $4; }
e04a16fb
AG
1206;
1207
2e5eb5c5
APB
1208constructor_block_end:
1209 block_end
5f1c312a 1210;
2e5eb5c5 1211
e04a16fb
AG
1212/* Error recovery for that rule moved down expression_statement: rule. */
1213explicit_constructor_invocation:
1214 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1215 {
1216 $$ = build_method_invocation ($1, NULL_TREE);
1217 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1218 $$ = java_method_add_stmt (current_function_decl, $$);
1219 }
e04a16fb 1220| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1221 {
1222 $$ = build_method_invocation ($1, $3);
1223 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1224 $$ = java_method_add_stmt (current_function_decl, $$);
1225 }
e04a16fb
AG
1226 /* Added, JDK1.1 inner classes. Modified because the rule
1227 'primary' couldn't work. */
1228| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1229 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1230| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1231 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1232;
1233
1234this_or_super: /* Added, simplifies error diagnostics */
1235 THIS_TK
1236 {
9ee9b555 1237 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1238 EXPR_WFL_LINECOL (wfl) = $1.location;
1239 $$ = wfl;
1240 }
1241| SUPER_TK
1242 {
9ee9b555 1243 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1244 EXPR_WFL_LINECOL (wfl) = $1.location;
1245 $$ = wfl;
1246 }
1247;
1248
1249/* 19.9 Productions from 9: Interfaces */
1250/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1251interface_declaration:
1252 INTERFACE_TK identifier
1253 { create_interface (0, $2, NULL_TREE); }
1254 interface_body
e04a16fb
AG
1255| modifiers INTERFACE_TK identifier
1256 { create_interface ($1, $3, NULL_TREE); }
1257 interface_body
e04a16fb
AG
1258| INTERFACE_TK identifier extends_interfaces
1259 { create_interface (0, $2, $3); }
1260 interface_body
e04a16fb
AG
1261| modifiers INTERFACE_TK identifier extends_interfaces
1262 { create_interface ($1, $3, $4); }
1263 interface_body
e04a16fb 1264| INTERFACE_TK identifier error
0b4d333e 1265 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1266| modifiers INTERFACE_TK identifier error
0b4d333e 1267 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1268;
1269
1270extends_interfaces:
1271 EXTENDS_TK interface_type
1272 {
1273 ctxp->interface_number = 1;
1274 $$ = build_tree_list ($2, NULL_TREE);
1275 }
1276| extends_interfaces C_TK interface_type
1277 {
1278 ctxp->interface_number++;
1279 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1280 }
1281| EXTENDS_TK error
1282 {yyerror ("Invalid interface type"); RECOVER;}
1283| extends_interfaces C_TK error
1284 {yyerror ("Missing term"); RECOVER;}
1285;
1286
1287interface_body:
1288 OCB_TK CCB_TK
1289 { $$ = NULL_TREE; }
1290| OCB_TK interface_member_declarations CCB_TK
1291 { $$ = NULL_TREE; }
1292;
1293
1294interface_member_declarations:
1295 interface_member_declaration
1296| interface_member_declarations interface_member_declaration
1297;
1298
1299interface_member_declaration:
1300 constant_declaration
1301| abstract_method_declaration
1302| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1303 { end_class_declaration (1); }
1304| interface_declaration /* Added, JDK1.1 inner interfaces */
1305 { end_class_declaration (1); }
e04a16fb
AG
1306;
1307
1308constant_declaration:
1309 field_declaration
1310;
1311
1312abstract_method_declaration:
1313 method_header SC_TK
1314 {
1315 check_abstract_method_header ($1);
1316 current_function_decl = NULL_TREE; /* FIXME ? */
1317 }
1318| method_header error
1319 {yyerror ("';' expected"); RECOVER;}
1320;
1321
1322/* 19.10 Productions from 10: Arrays */
1323array_initializer:
1324 OCB_TK CCB_TK
1179ebc2 1325 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1326| OCB_TK variable_initializers CCB_TK
f8976021 1327 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1328| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1329 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1330;
1331
1332variable_initializers:
1333 variable_initializer
f8976021
APB
1334 {
1335 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1336 $1, NULL_TREE);
1337 }
e04a16fb 1338| variable_initializers C_TK variable_initializer
1179ebc2
APB
1339 {
1340 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1341 }
e04a16fb
AG
1342| variable_initializers C_TK error
1343 {yyerror ("Missing term"); RECOVER;}
1344;
1345
1346/* 19.11 Production from 14: Blocks and Statements */
1347block:
1348 OCB_TK CCB_TK
7f10c2e2
APB
1349 {
1350 /* Store the location of the `}' when doing xrefs */
1351 if (current_function_decl && flag_emit_xref)
1352 DECL_END_SOURCE_LINE (current_function_decl) =
1353 EXPR_WFL_ADD_COL ($2.location, 1);
1354 $$ = empty_stmt_node;
1355 }
22eed1e6
APB
1356| block_begin block_statements block_end
1357 { $$ = $3; }
1358;
1359
1360block_begin:
1361 OCB_TK
e04a16fb 1362 { enter_block (); }
22eed1e6
APB
1363;
1364
1365block_end:
e04a16fb
AG
1366 CCB_TK
1367 {
1368 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1369 /* Store the location of the `}' when doing xrefs */
1370 if (current_function_decl && flag_emit_xref)
1371 DECL_END_SOURCE_LINE (current_function_decl) =
1372 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1373 $$ = exit_block ();
c280e37a
APB
1374 if (!BLOCK_SUBBLOCKS ($$))
1375 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1376 }
1377;
1378
1379block_statements:
1380 block_statement
1381| block_statements block_statement
1382;
1383
1384block_statement:
1385 local_variable_declaration_statement
1386| statement
15fdcfe9 1387 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1388| class_declaration /* Added, JDK1.1 local classes */
1389 {
1390 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1391 end_class_declaration (1);
1392 }
e04a16fb
AG
1393;
1394
1395local_variable_declaration_statement:
1396 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1397;
1398
1399local_variable_declaration:
1400 type variable_declarators
1401 { declare_local_variables (0, $1, $2); }
a003f638 1402| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1403 { declare_local_variables ($1, $2, $3); }
1404;
1405
1406statement:
1407 statement_without_trailing_substatement
1408| labeled_statement
e04a16fb 1409| if_then_statement
e04a16fb 1410| if_then_else_statement
e04a16fb 1411| while_statement
e04a16fb 1412| for_statement
cd9643f7 1413 { $$ = exit_block (); }
e04a16fb
AG
1414;
1415
1416statement_nsi:
1417 statement_without_trailing_substatement
1418| labeled_statement_nsi
e04a16fb 1419| if_then_else_statement_nsi
e04a16fb 1420| while_statement_nsi
e04a16fb 1421| for_statement_nsi
9dd939b2 1422 { $$ = exit_block (); }
e04a16fb
AG
1423;
1424
1425statement_without_trailing_substatement:
1426 block
e04a16fb 1427| empty_statement
e04a16fb 1428| expression_statement
e04a16fb 1429| switch_statement
e04a16fb 1430| do_statement
e04a16fb 1431| break_statement
e04a16fb 1432| continue_statement
e04a16fb
AG
1433| return_statement
1434| synchronized_statement
e04a16fb 1435| throw_statement
e04a16fb 1436| try_statement
e04a16fb
AG
1437;
1438
1439empty_statement:
1440 SC_TK
5f1c312a
APB
1441 {
1442 if (flag_extraneous_semicolon)
1443 {
1444 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1445 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1446 }
1447 $$ = empty_stmt_node;
1448 }
e04a16fb
AG
1449;
1450
1451label_decl:
1452 identifier REL_CL_TK
1453 {
1454 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1455 EXPR_WFL_NODE ($1));
e04a16fb
AG
1456 pushlevel (2);
1457 push_labeled_block ($$);
1458 PUSH_LABELED_BLOCK ($$);
1459 }
1460;
1461
1462labeled_statement:
1463 label_decl statement
b635eb2f 1464 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1465| identifier error
1466 {yyerror ("':' expected"); RECOVER;}
1467;
1468
1469labeled_statement_nsi:
1470 label_decl statement_nsi
b635eb2f 1471 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1472;
1473
1474/* We concentrate here a bunch of error handling rules that we couldn't write
1475 earlier, because expression_statement catches a missing ';'. */
1476expression_statement:
1477 statement_expression SC_TK
1478 {
1479 /* We have a statement. Generate a WFL around it so
1480 we can debug it */
1481 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1482 /* We know we have a statement, so set the debug
1483 info to be eventually generate here. */
1484 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1485 }
1486| error SC_TK
1487 {
29f8b718 1488 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1489 DRECOVER (expr_stmt);
1490 }
1491| error OCB_TK
1492 {
29f8b718 1493 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1494 DRECOVER (expr_stmt);
1495 }
1496| error CCB_TK
1497 {
29f8b718 1498 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1499 DRECOVER (expr_stmt);
1500 }
1501| this_or_super OP_TK error
1502 {yyerror ("')' expected"); RECOVER;}
1503| this_or_super OP_TK CP_TK error
22eed1e6 1504 {
8119c720 1505 parse_ctor_invocation_error ();
22eed1e6
APB
1506 RECOVER;
1507 }
e04a16fb
AG
1508| this_or_super OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1511 {
8119c720 1512 parse_ctor_invocation_error ();
22eed1e6
APB
1513 RECOVER;
1514 }
e04a16fb
AG
1515| name DOT_TK SUPER_TK error
1516 {yyerror ("'(' expected"); RECOVER;}
1517| name DOT_TK SUPER_TK OP_TK error
1518 {yyerror ("')' expected"); RECOVER;}
1519| name DOT_TK SUPER_TK OP_TK argument_list error
1520 {yyerror ("')' expected"); RECOVER;}
1521| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1522 {yyerror ("';' expected"); RECOVER;}
1523| name DOT_TK SUPER_TK OP_TK CP_TK error
1524 {yyerror ("';' expected"); RECOVER;}
1525;
1526
1527statement_expression:
1528 assignment
1529| pre_increment_expression
e04a16fb 1530| pre_decrement_expression
e04a16fb 1531| post_increment_expression
e04a16fb 1532| post_decrement_expression
e04a16fb
AG
1533| method_invocation
1534| class_instance_creation_expression
e04a16fb
AG
1535;
1536
1537if_then_statement:
1538 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1539 {
1540 $$ = build_if_else_statement ($2.location, $3,
1541 $5, NULL_TREE);
1542 }
e04a16fb
AG
1543| IF_TK error
1544 {yyerror ("'(' expected"); RECOVER;}
1545| IF_TK OP_TK error
1546 {yyerror ("Missing term"); RECOVER;}
1547| IF_TK OP_TK expression error
1548 {yyerror ("')' expected"); RECOVER;}
1549;
1550
1551if_then_else_statement:
1552 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1553 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1554;
1555
1556if_then_else_statement_nsi:
1557 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1558 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1559;
1560
1561switch_statement:
15fdcfe9
PB
1562 switch_expression
1563 {
1564 enter_block ();
1565 }
1566 switch_block
b67d701b 1567 {
15fdcfe9 1568 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1569 I.e. make the last statment also have its own
1570 COMPOUND_EXPR. */
15fdcfe9
PB
1571 maybe_absorb_scoping_blocks ();
1572 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1573 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1574 }
1575;
1576
1577switch_expression:
1578 SWITCH_TK OP_TK expression CP_TK
1579 {
1580 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1581 EXPR_WFL_LINECOL ($$) = $2.location;
1582 }
e04a16fb
AG
1583| SWITCH_TK error
1584 {yyerror ("'(' expected"); RECOVER;}
1585| SWITCH_TK OP_TK error
1586 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1587| SWITCH_TK OP_TK expression CP_TK error
1588 {yyerror ("'{' expected"); RECOVER;}
1589;
1590
f8976021
APB
1591/* Default assignment is there to avoid type node on switch_block
1592 node. */
1593
e04a16fb
AG
1594switch_block:
1595 OCB_TK CCB_TK
f8976021 1596 { $$ = NULL_TREE; }
e04a16fb 1597| OCB_TK switch_labels CCB_TK
f8976021 1598 { $$ = NULL_TREE; }
e04a16fb 1599| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1600 { $$ = NULL_TREE; }
e04a16fb 1601| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1602 { $$ = NULL_TREE; }
e04a16fb
AG
1603;
1604
1605switch_block_statement_groups:
1606 switch_block_statement_group
1607| switch_block_statement_groups switch_block_statement_group
1608;
1609
1610switch_block_statement_group:
15fdcfe9 1611 switch_labels block_statements
e04a16fb
AG
1612;
1613
e04a16fb
AG
1614switch_labels:
1615 switch_label
1616| switch_labels switch_label
1617;
1618
1619switch_label:
1620 CASE_TK constant_expression REL_CL_TK
b67d701b 1621 {
15fdcfe9
PB
1622 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1623 EXPR_WFL_LINECOL (lab) = $1.location;
1624 java_method_add_stmt (current_function_decl, lab);
b67d701b 1625 }
e04a16fb 1626| DEFAULT_TK REL_CL_TK
b67d701b 1627 {
ac39dac0 1628 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1629 EXPR_WFL_LINECOL (lab) = $1.location;
1630 java_method_add_stmt (current_function_decl, lab);
b67d701b 1631 }
e04a16fb
AG
1632| CASE_TK error
1633 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1634| CASE_TK constant_expression error
1635 {yyerror ("':' expected"); RECOVER;}
1636| DEFAULT_TK error
1637 {yyerror ("':' expected"); RECOVER;}
1638;
1639
1640while_expression:
1641 WHILE_TK OP_TK expression CP_TK
1642 {
1643 tree body = build_loop_body ($2.location, $3, 0);
1644 $$ = build_new_loop (body);
1645 }
1646;
1647
1648while_statement:
1649 while_expression statement
b635eb2f 1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1651| WHILE_TK error
1652 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1653| WHILE_TK OP_TK error
1654 {yyerror ("Missing term and ')' expected"); RECOVER;}
1655| WHILE_TK OP_TK expression error
1656 {yyerror ("')' expected"); RECOVER;}
1657;
1658
1659while_statement_nsi:
1660 while_expression statement_nsi
b635eb2f 1661 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1662;
1663
1664do_statement_begin:
1665 DO_TK
1666 {
1667 tree body = build_loop_body (0, NULL_TREE, 1);
1668 $$ = build_new_loop (body);
1669 }
1670 /* Need error handing here. FIXME */
1671;
1672
1673do_statement:
1674 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1675 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1676;
1677
1678for_statement:
1679 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1680 {
1681 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1682 $3 = build_wfl_node ($3);
1683 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1684 }
e04a16fb
AG
1685| for_begin SC_TK SC_TK for_update CP_TK statement
1686 {
b635eb2f 1687 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1688 /* We have not condition, so we get rid of the EXIT_EXPR */
1689 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1690 empty_stmt_node;
e04a16fb
AG
1691 }
1692| for_begin SC_TK error
1693 {yyerror ("Invalid control expression"); RECOVER;}
1694| for_begin SC_TK expression SC_TK error
1695 {yyerror ("Invalid update expression"); RECOVER;}
1696| for_begin SC_TK SC_TK error
1697 {yyerror ("Invalid update expression"); RECOVER;}
1698;
1699
1700for_statement_nsi:
1701 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1702 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1703| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1704 {
b635eb2f 1705 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1706 /* We have not condition, so we get rid of the EXIT_EXPR */
1707 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1708 empty_stmt_node;
e04a16fb
AG
1709 }
1710;
1711
1712for_header:
1713 FOR_TK OP_TK
1714 {
1715 /* This scope defined for local variable that may be
1716 defined within the scope of the for loop */
1717 enter_block ();
1718 }
1719| FOR_TK error
1720 {yyerror ("'(' expected"); DRECOVER(for_1);}
1721| FOR_TK OP_TK error
1722 {yyerror ("Invalid init statement"); RECOVER;}
1723;
1724
1725for_begin:
1726 for_header for_init
1727 {
1728 /* We now declare the loop body. The loop is
1729 declared as a for loop. */
1730 tree body = build_loop_body (0, NULL_TREE, 0);
1731 $$ = build_new_loop (body);
c2952b01 1732 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1733 /* The loop is added to the current block the for
1734 statement is defined within */
1735 java_method_add_stmt (current_function_decl, $$);
1736 }
1737;
1738for_init: /* Can be empty */
9bbc7d9f 1739 { $$ = empty_stmt_node; }
e04a16fb
AG
1740| statement_expression_list
1741 {
1742 /* Init statement recorded within the previously
1743 defined block scope */
1744 $$ = java_method_add_stmt (current_function_decl, $1);
1745 }
1746| local_variable_declaration
1747 {
1748 /* Local variable are recorded within the previously
1749 defined block scope */
1750 $$ = NULL_TREE;
1751 }
1752| statement_expression_list error
1753 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1754;
1755
1756for_update: /* Can be empty */
9bbc7d9f 1757 {$$ = empty_stmt_node;}
e04a16fb
AG
1758| statement_expression_list
1759 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1760;
1761
1762statement_expression_list:
1763 statement_expression
1764 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1765| statement_expression_list C_TK statement_expression
1766 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1767| statement_expression_list C_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1769;
1770
1771break_statement:
1772 BREAK_TK SC_TK
1773 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1774| BREAK_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 1, $2); }
1776| BREAK_TK error
1777 {yyerror ("Missing term"); RECOVER;}
1778| BREAK_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1780;
1781
1782continue_statement:
1783 CONTINUE_TK SC_TK
1784 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1785| CONTINUE_TK identifier SC_TK
1786 { $$ = build_bc_statement ($1.location, 0, $2); }
1787| CONTINUE_TK error
1788 {yyerror ("Missing term"); RECOVER;}
1789| CONTINUE_TK identifier error
1790 {yyerror ("';' expected"); RECOVER;}
1791;
1792
1793return_statement:
1794 RETURN_TK SC_TK
1795 { $$ = build_return ($1.location, NULL_TREE); }
1796| RETURN_TK expression SC_TK
1797 { $$ = build_return ($1.location, $2); }
1798| RETURN_TK error
1799 {yyerror ("Missing term"); RECOVER;}
1800| RETURN_TK expression error
1801 {yyerror ("';' expected"); RECOVER;}
1802;
1803
1804throw_statement:
1805 THROW_TK expression SC_TK
b9f7e36c
APB
1806 {
1807 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1808 EXPR_WFL_LINECOL ($$) = $1.location;
1809 }
e04a16fb
AG
1810| THROW_TK error
1811 {yyerror ("Missing term"); RECOVER;}
1812| THROW_TK expression error
1813 {yyerror ("';' expected"); RECOVER;}
1814;
1815
1816synchronized_statement:
1817 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1818 {
1819 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1820 EXPR_WFL_LINECOL ($$) =
1821 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1822 }
e04a16fb
AG
1823| synchronized OP_TK expression CP_TK error
1824 {yyerror ("'{' expected"); RECOVER;}
1825| synchronized error
1826 {yyerror ("'(' expected"); RECOVER;}
1827| synchronized OP_TK error CP_TK
1828 {yyerror ("Missing term"); RECOVER;}
1829| synchronized OP_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1831;
1832
b9f7e36c 1833synchronized:
efa0a23f 1834 modifiers
e04a16fb 1835 {
781b0558
KG
1836 check_modifiers (
1837 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1838 $1, ACC_SYNCHRONIZED);
1839 if ($1 != ACC_SYNCHRONIZED)
1840 MODIFIER_WFL (SYNCHRONIZED_TK) =
1841 build_wfl_node (NULL_TREE);
e04a16fb
AG
1842 }
1843;
1844
1845try_statement:
1846 TRY_TK block catches
a7d8d81f 1847 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1848| TRY_TK block finally
a7d8d81f 1849 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1850| TRY_TK block catches finally
2aa11e97
APB
1851 { $$ = build_try_finally_statement
1852 ($1.location, build_try_statement ($1.location,
1853 $2, $3), $4);
1854 }
e04a16fb
AG
1855| TRY_TK error
1856 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1857;
1858
1859catches:
1860 catch_clause
1861| catches catch_clause
b67d701b
PB
1862 {
1863 TREE_CHAIN ($2) = $1;
1864 $$ = $2;
1865 }
e04a16fb
AG
1866;
1867
1868catch_clause:
b67d701b
PB
1869 catch_clause_parameter block
1870 {
1871 java_method_add_stmt (current_function_decl, $2);
1872 exit_block ();
1873 $$ = $1;
1874 }
1875
1876catch_clause_parameter:
1877 CATCH_TK OP_TK formal_parameter CP_TK
1878 {
1879 /* We add a block to define a scope for
1880 formal_parameter (CCBP). The formal parameter is
1881 declared initialized by the appropriate function
1882 call */
1883 tree ccpb = enter_block ();
1884 tree init = build_assignment (ASSIGN_TK, $2.location,
1885 TREE_PURPOSE ($3),
1886 soft_exceptioninfo_call_node);
1887 declare_local_variables (0, TREE_VALUE ($3),
1888 build_tree_list (TREE_PURPOSE ($3),
1889 init));
1890 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1891 EXPR_WFL_LINECOL ($$) = $1.location;
1892 }
e04a16fb 1893| CATCH_TK error
97f30284 1894 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1895| CATCH_TK OP_TK error
97f30284
APB
1896 {
1897 yyerror ("Missing term or ')' expected");
1898 RECOVER; $$ = NULL_TREE;
1899 }
b67d701b 1900| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1901 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1902;
1903
1904finally:
1905 FINALLY_TK block
a7d8d81f 1906 { $$ = $2; }
e04a16fb
AG
1907| FINALLY_TK error
1908 {yyerror ("'{' expected"); RECOVER; }
1909;
1910
1911/* 19.12 Production from 15: Expressions */
1912primary:
1913 primary_no_new_array
1914| array_creation_expression
1915;
1916
1917primary_no_new_array:
1918 literal
1919| THIS_TK
1920 { $$ = build_this ($1.location); }
1921| OP_TK expression CP_TK
1922 {$$ = $2;}
1923| class_instance_creation_expression
1924| field_access
1925| method_invocation
1926| array_access
c2952b01 1927| type_literals
e04a16fb
AG
1928 /* Added, JDK1.1 inner classes. Documentation is wrong
1929 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1930 exist. Used name: instead. */
e04a16fb 1931| name DOT_TK THIS_TK
c2952b01
APB
1932 {
1933 tree wfl = build_wfl_node (this_identifier_node);
1934 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1935 }
e04a16fb
AG
1936| OP_TK expression error
1937 {yyerror ("')' expected"); RECOVER;}
1938| name DOT_TK error
1939 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1940| primitive_type DOT_TK error
1941 {yyerror ("'class' expected" ); RECOVER;}
1942| VOID_TK DOT_TK error
1943 {yyerror ("'class' expected" ); RECOVER;}
1944;
1945
c2952b01
APB
1946type_literals:
1947 name DOT_TK CLASS_TK
1948 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1949| array_type DOT_TK CLASS_TK
c2952b01
APB
1950 { $$ = build_incomplete_class_ref ($2.location, $1); }
1951| primitive_type DOT_TK CLASS_TK
1952 { $$ = build_class_ref ($1); }
1953| VOID_TK DOT_TK CLASS_TK
1954 { $$ = build_class_ref (void_type_node); }
1955;
1956
e04a16fb
AG
1957class_instance_creation_expression:
1958 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1959 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1960| NEW_TK class_type OP_TK CP_TK
b67d701b 1961 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1962| anonymous_class_creation
e04a16fb
AG
1963 /* Added, JDK1.1 inner classes, modified to use name or
1964 primary instead of primary solely which couldn't work in
1965 all situations. */
1966| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1967 {
1968 tree ctor = build_new_invocation ($2, NULL_TREE);
1969 $$ = make_qualified_primary ($1, ctor,
1970 EXPR_WFL_LINECOL ($1));
1971 }
e04a16fb
AG
1972| something_dot_new identifier OP_TK CP_TK class_body
1973| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1974 {
1975 tree ctor = build_new_invocation ($2, $4);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1978 }
e04a16fb
AG
1979| something_dot_new identifier OP_TK argument_list CP_TK class_body
1980| NEW_TK error SC_TK
1981 {yyerror ("'(' expected"); DRECOVER(new_1);}
1982| NEW_TK class_type error
1983 {yyerror ("'(' expected"); RECOVER;}
1984| NEW_TK class_type OP_TK error
1985 {yyerror ("')' or term expected"); RECOVER;}
1986| NEW_TK class_type OP_TK argument_list error
1987 {yyerror ("')' expected"); RECOVER;}
1988| something_dot_new error
1989 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1990| something_dot_new identifier error
1991 {yyerror ("'(' expected"); RECOVER;}
1992;
1993
c2952b01
APB
1994/* Created after JDK1.1 rules originally added to
1995 class_instance_creation_expression, but modified to use
1996 'class_type' instead of 'TypeName' (type_name) which is mentionned
1997 in the documentation but doesn't exist. */
1998
1999anonymous_class_creation:
2000 NEW_TK class_type OP_TK argument_list CP_TK
2001 { create_anonymous_class ($1.location, $2); }
2002 class_body
2003 {
2004 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2005 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2006
2007 end_class_declaration (1);
2008
2009 /* Now we can craft the new expression */
2010 $$ = build_new_invocation (id, $4);
2011
2012 /* Note that we can't possibly be here if
2013 `class_type' is an interface (in which case the
2014 anonymous class extends Object and implements
2015 `class_type', hence its constructor can't have
2016 arguments.) */
2017
2018 /* Otherwise, the innerclass must feature a
2019 constructor matching `argument_list'. Anonymous
2020 classes are a bit special: it's impossible to
2021 define constructor for them, hence constructors
2022 must be generated following the hints provided by
2023 the `new' expression. Whether a super constructor
2024 of that nature exists or not is to be verified
2025 later on in verify_constructor_super.
2026
2027 It's during the expansion of a `new' statement
2028 refering to an anonymous class that a ctor will
2029 be generated for the anonymous class, with the
2030 right arguments. */
2031
2032 }
2033| NEW_TK class_type OP_TK CP_TK
2034 { create_anonymous_class ($1.location, $2); }
2035 class_body
2036 {
2037 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2038 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2039
2040 end_class_declaration (1);
2041
2042 /* Now we can craft the new expression. The
2043 statement doesn't need to be remember so that a
2044 constructor can be generated, since its signature
2045 is already known. */
2046 $$ = build_new_invocation (id, NULL_TREE);
2047 }
2048;
2049
e04a16fb
AG
2050something_dot_new: /* Added, not part of the specs. */
2051 name DOT_TK NEW_TK
c2952b01 2052 { $$ = $1; }
e04a16fb 2053| primary DOT_TK NEW_TK
c2952b01 2054 { $$ = $1; }
e04a16fb
AG
2055;
2056
2057argument_list:
2058 expression
2059 {
2060 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2061 ctxp->formal_parameter_number = 1;
2062 }
2063| argument_list C_TK expression
2064 {
2065 ctxp->formal_parameter_number += 1;
2066 $$ = tree_cons (NULL_TREE, $3, $1);
2067 }
2068| argument_list C_TK error
2069 {yyerror ("Missing term"); RECOVER;}
2070;
2071
2072array_creation_expression:
2073 NEW_TK primitive_type dim_exprs
2074 { $$ = build_newarray_node ($2, $3, 0); }
2075| NEW_TK class_or_interface_type dim_exprs
2076 { $$ = build_newarray_node ($2, $3, 0); }
2077| NEW_TK primitive_type dim_exprs dims
c7303e41 2078 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2079| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2080 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2081 /* Added, JDK1.1 anonymous array. Initial documentation rule
2082 modified */
2083| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2084 {
2085 char *sig;
c7303e41
APB
2086 int osb = pop_current_osb (ctxp);
2087 while (osb--)
c2952b01
APB
2088 obstack_1grow (&temporary_obstack, '[');
2089 sig = obstack_finish (&temporary_obstack);
2090 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2091 $2, get_identifier (sig), $4);
2092 }
e04a16fb 2093| NEW_TK primitive_type dims array_initializer
c2952b01 2094 {
c7303e41 2095 int osb = pop_current_osb (ctxp);
c2952b01 2096 tree type = $2;
c7303e41 2097 while (osb--)
c2952b01
APB
2098 type = build_java_array_type (type, -1);
2099 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2100 build_pointer_type (type), NULL_TREE, $4);
2101 }
e04a16fb
AG
2102| NEW_TK error CSB_TK
2103 {yyerror ("'[' expected"); DRECOVER ("]");}
2104| NEW_TK error OSB_TK
2105 {yyerror ("']' expected"); RECOVER;}
2106;
2107
2108dim_exprs:
2109 dim_expr
2110 { $$ = build_tree_list (NULL_TREE, $1); }
2111| dim_exprs dim_expr
2112 { $$ = tree_cons (NULL_TREE, $2, $$); }
2113;
2114
2115dim_expr:
2116 OSB_TK expression CSB_TK
2117 {
9a7ab4b3
APB
2118 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2119 {
2120 $2 = build_wfl_node ($2);
2121 TREE_TYPE ($2) = NULL_TREE;
2122 }
e04a16fb
AG
2123 EXPR_WFL_LINECOL ($2) = $1.location;
2124 $$ = $2;
2125 }
2126| OSB_TK expression error
2127 {yyerror ("']' expected"); RECOVER;}
2128| OSB_TK error
2129 {
2130 yyerror ("Missing term");
2131 yyerror ("']' expected");
2132 RECOVER;
2133 }
2134;
2135
2136dims:
2137 OSB_TK CSB_TK
ba179f9f
APB
2138 {
2139 int allocate = 0;
2140 /* If not initialized, allocate memory for the osb
2141 numbers stack */
2142 if (!ctxp->osb_limit)
2143 {
2144 allocate = ctxp->osb_limit = 32;
2145 ctxp->osb_depth = -1;
2146 }
c2952b01 2147 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2148 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2149 allocate = ctxp->osb_limit << 1;
2150
2151 if (allocate)
2152 {
2153 allocate *= sizeof (int);
2154 if (ctxp->osb_number)
2155 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2156 allocate);
2157 else
2158 ctxp->osb_number = (int *)xmalloc (allocate);
2159 }
2160 ctxp->osb_depth++;
2161 CURRENT_OSB (ctxp) = 1;
2162 }
e04a16fb 2163| dims OSB_TK CSB_TK
ba179f9f 2164 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2165| dims OSB_TK error
2166 { yyerror ("']' expected"); RECOVER;}
2167;
2168
2169field_access:
2170 primary DOT_TK identifier
2171 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2172 /* FIXME - REWRITE TO:
2173 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2174| SUPER_TK DOT_TK identifier
2175 {
6e22695a 2176 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2177 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2178 $$ = make_qualified_name (super_wfl, $3, $2.location);
2179 }
2180| SUPER_TK error
2181 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2182;
2183
2184method_invocation:
2185 name OP_TK CP_TK
2186 { $$ = build_method_invocation ($1, NULL_TREE); }
2187| name OP_TK argument_list CP_TK
2188 { $$ = build_method_invocation ($1, $3); }
2189| primary DOT_TK identifier OP_TK CP_TK
2190 {
22eed1e6
APB
2191 if (TREE_CODE ($1) == THIS_EXPR)
2192 $$ = build_this_super_qualified_invocation
2193 (1, $3, NULL_TREE, 0, $2.location);
2194 else
2195 {
2196 tree invok = build_method_invocation ($3, NULL_TREE);
2197 $$ = make_qualified_primary ($1, invok, $2.location);
2198 }
e04a16fb
AG
2199 }
2200| primary DOT_TK identifier OP_TK argument_list CP_TK
2201 {
22eed1e6
APB
2202 if (TREE_CODE ($1) == THIS_EXPR)
2203 $$ = build_this_super_qualified_invocation
2204 (1, $3, $5, 0, $2.location);
2205 else
2206 {
2207 tree invok = build_method_invocation ($3, $5);
2208 $$ = make_qualified_primary ($1, invok, $2.location);
2209 }
e04a16fb
AG
2210 }
2211| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2212 {
2213 $$ = build_this_super_qualified_invocation
2214 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2215 }
2216| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2217 {
22eed1e6
APB
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2220 }
2221 /* Screws up thing. I let it here until I'm convinced it can
2222 be removed. FIXME
2223| primary DOT_TK error
2224 {yyerror ("'(' expected"); DRECOVER(bad);} */
2225| SUPER_TK DOT_TK error CP_TK
2226 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2227| SUPER_TK DOT_TK error DOT_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2229;
2230
2231array_access:
2232 name OSB_TK expression CSB_TK
2233 { $$ = build_array_ref ($2.location, $1, $3); }
2234| primary_no_new_array OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2236| name OSB_TK error
2237 {
2238 yyerror ("Missing term and ']' expected");
2239 DRECOVER(array_access);
2240 }
2241| name OSB_TK expression error
2242 {
2243 yyerror ("']' expected");
2244 DRECOVER(array_access);
2245 }
2246| primary_no_new_array OSB_TK error
2247 {
2248 yyerror ("Missing term and ']' expected");
2249 DRECOVER(array_access);
2250 }
2251| primary_no_new_array OSB_TK expression error
2252 {
2253 yyerror ("']' expected");
2254 DRECOVER(array_access);
2255 }
2256;
2257
2258postfix_expression:
2259 primary
2260| name
2261| post_increment_expression
2262| post_decrement_expression
2263;
2264
2265post_increment_expression:
2266 postfix_expression INCR_TK
2267 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2268;
2269
2270post_decrement_expression:
2271 postfix_expression DECR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2273;
2274
2275unary_expression:
2276 pre_increment_expression
2277| pre_decrement_expression
2278| PLUS_TK unary_expression
2279 {$$ = build_unaryop ($1.token, $1.location, $2); }
2280| MINUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282| unary_expression_not_plus_minus
2283| PLUS_TK error
2284 {yyerror ("Missing term"); RECOVER}
2285| MINUS_TK error
2286 {yyerror ("Missing term"); RECOVER}
2287;
2288
2289pre_increment_expression:
2290 INCR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2292| INCR_TK error
2293 {yyerror ("Missing term"); RECOVER}
2294;
2295
2296pre_decrement_expression:
2297 DECR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299| DECR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2301;
2302
2303unary_expression_not_plus_minus:
2304 postfix_expression
2305| NOT_TK unary_expression
2306 {$$ = build_unaryop ($1.token, $1.location, $2); }
2307| NEG_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2309| cast_expression
2310| NOT_TK error
2311 {yyerror ("Missing term"); RECOVER}
2312| NEG_TK error
2313 {yyerror ("Missing term"); RECOVER}
2314;
2315
2316cast_expression: /* Error handling here is potentially weak */
2317 OP_TK primitive_type dims CP_TK unary_expression
2318 {
2319 tree type = $2;
c7303e41
APB
2320 int osb = pop_current_osb (ctxp);
2321 while (osb--)
e04a16fb
AG
2322 type = build_java_array_type (type, -1);
2323 $$ = build_cast ($1.location, type, $5);
2324 }
2325| OP_TK primitive_type CP_TK unary_expression
2326 { $$ = build_cast ($1.location, $2, $4); }
2327| OP_TK expression CP_TK unary_expression_not_plus_minus
2328 { $$ = build_cast ($1.location, $2, $4); }
2329| OP_TK name dims CP_TK unary_expression_not_plus_minus
2330 {
49f48c71 2331 const char *ptr;
c7303e41
APB
2332 int osb = pop_current_osb (ctxp);
2333 while (osb--)
e04a16fb
AG
2334 obstack_1grow (&temporary_obstack, '[');
2335 obstack_grow0 (&temporary_obstack,
2336 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2337 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2338 ptr = obstack_finish (&temporary_obstack);
2339 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2340 $$ = build_cast ($1.location, $2, $5);
2341 }
2342| OP_TK primitive_type OSB_TK error
2343 {yyerror ("']' expected, invalid type expression");}
2344| OP_TK error
2345 {
29f8b718 2346 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2347 RECOVER;
2348 }
2349| OP_TK primitive_type dims CP_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351| OP_TK primitive_type CP_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353| OP_TK name dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2355;
2356
2357multiplicative_expression:
2358 unary_expression
2359| multiplicative_expression MULT_TK unary_expression
2360 {
2361 $$ = build_binop (BINOP_LOOKUP ($2.token),
2362 $2.location, $1, $3);
2363 }
2364| multiplicative_expression DIV_TK unary_expression
2365 {
2366 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2367 $1, $3);
2368 }
2369| multiplicative_expression REM_TK unary_expression
2370 {
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372 $1, $3);
2373 }
2374| multiplicative_expression MULT_TK error
2375 {yyerror ("Missing term"); RECOVER;}
2376| multiplicative_expression DIV_TK error
2377 {yyerror ("Missing term"); RECOVER;}
2378| multiplicative_expression REM_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380;
2381
2382additive_expression:
2383 multiplicative_expression
2384| additive_expression PLUS_TK multiplicative_expression
2385 {
2386 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2387 $1, $3);
2388 }
2389| additive_expression MINUS_TK multiplicative_expression
2390 {
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2392 $1, $3);
2393 }
2394| additive_expression PLUS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2396| additive_expression MINUS_TK error
2397 {yyerror ("Missing term"); RECOVER;}
2398;
2399
2400shift_expression:
2401 additive_expression
2402| shift_expression LS_TK additive_expression
2403 {
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2405 $1, $3);
2406 }
2407| shift_expression SRS_TK additive_expression
2408 {
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 $1, $3);
2411 }
2412| shift_expression ZRS_TK additive_expression
2413 {
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 $1, $3);
2416 }
2417| shift_expression LS_TK error
2418 {yyerror ("Missing term"); RECOVER;}
2419| shift_expression SRS_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421| shift_expression ZRS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2423;
2424
2425relational_expression:
2426 shift_expression
2427| relational_expression LT_TK shift_expression
2428 {
2429 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2430 $1, $3);
2431 }
2432| relational_expression GT_TK shift_expression
2433 {
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435 $1, $3);
2436 }
2437| relational_expression LTE_TK shift_expression
2438 {
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440 $1, $3);
2441 }
2442| relational_expression GTE_TK shift_expression
2443 {
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445 $1, $3);
2446 }
2447| relational_expression INSTANCEOF_TK reference_type
5e942c50 2448 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2449| relational_expression LT_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451| relational_expression GT_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453| relational_expression LTE_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455| relational_expression GTE_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457| relational_expression INSTANCEOF_TK error
2458 {yyerror ("Invalid reference type"); RECOVER;}
2459;
2460
2461equality_expression:
2462 relational_expression
2463| equality_expression EQ_TK relational_expression
2464 {
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2466 $1, $3);
2467 }
2468| equality_expression NEQ_TK relational_expression
2469 {
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2471 $1, $3);
2472 }
2473| equality_expression EQ_TK error
2474 {yyerror ("Missing term"); RECOVER;}
2475| equality_expression NEQ_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2477;
2478
2479and_expression:
2480 equality_expression
2481| and_expression AND_TK equality_expression
2482 {
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2484 $1, $3);
2485 }
2486| and_expression AND_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2488;
2489
2490exclusive_or_expression:
2491 and_expression
2492| exclusive_or_expression XOR_TK and_expression
2493 {
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 $1, $3);
2496 }
2497| exclusive_or_expression XOR_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2499;
2500
2501inclusive_or_expression:
2502 exclusive_or_expression
2503| inclusive_or_expression OR_TK exclusive_or_expression
2504 {
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2506 $1, $3);
2507 }
2508| inclusive_or_expression OR_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2510;
2511
2512conditional_and_expression:
2513 inclusive_or_expression
2514| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2515 {
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517 $1, $3);
2518 }
2519| conditional_and_expression BOOL_AND_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2521;
2522
2523conditional_or_expression:
2524 conditional_and_expression
2525| conditional_or_expression BOOL_OR_TK conditional_and_expression
2526 {
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528 $1, $3);
2529 }
2530| conditional_or_expression BOOL_OR_TK error
2531 {yyerror ("Missing term"); RECOVER;}
2532;
2533
2534conditional_expression: /* Error handling here is weak */
2535 conditional_or_expression
2536| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2537 {
2538 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2539 EXPR_WFL_LINECOL ($$) = $2.location;
2540 }
e04a16fb
AG
2541| conditional_or_expression REL_QM_TK REL_CL_TK error
2542 {
2543 YYERROR_NOW;
2544 yyerror ("Missing term");
2545 DRECOVER (1);
2546 }
2547| conditional_or_expression REL_QM_TK error
2548 {yyerror ("Missing term"); DRECOVER (2);}
2549| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2550 {yyerror ("Missing term"); DRECOVER (3);}
2551;
2552
2553assignment_expression:
2554 conditional_expression
2555| assignment
2556;
2557
2558assignment:
2559 left_hand_side assignment_operator assignment_expression
2560 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2561| left_hand_side assignment_operator error
2562 {
29f8b718 2563 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2564 DRECOVER (assign);
2565 }
2566;
2567
2568left_hand_side:
2569 name
2570| field_access
2571| array_access
2572;
2573
2574assignment_operator:
2575 ASSIGN_ANY_TK
2576| ASSIGN_TK
2577;
2578
2579expression:
2580 assignment_expression
2581;
2582
2583constant_expression:
2584 expression
2585;
2586
2587%%
c7303e41
APB
2588
2589/* Helper function to retrieve an OSB count. Should be used when the
2590 `dims:' rule is being used. */
2591
2592static int
2593pop_current_osb (ctxp)
2594 struct parser_ctxt *ctxp;
2595{
2596 int to_return;
2597
2598 if (ctxp->osb_depth < 0)
400500c4 2599 abort ();
c7303e41
APB
2600
2601 to_return = CURRENT_OSB (ctxp);
2602 ctxp->osb_depth--;
2603
2604 return to_return;
2605}
2606
e04a16fb
AG
2607\f
2608
c2952b01
APB
2609/* This section of the code deal with save/restoring parser contexts.
2610 Add mode documentation here. FIXME */
e04a16fb 2611
c2952b01
APB
2612/* Helper function. Create a new parser context. With
2613 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2614 context is copied, otherwise, the new context is zeroed. The newly
2615 created context becomes the current one. */
e04a16fb 2616
c2952b01
APB
2617static void
2618create_new_parser_context (copy_from_previous)
2619 int copy_from_previous;
e04a16fb 2620{
c2952b01 2621 struct parser_ctxt *new;
e04a16fb 2622
c2952b01
APB
2623 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2624 if (copy_from_previous)
2625 {
2626 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2627 new->saved_data_ctx = 1;
2628 }
2629 else
2e09e75a 2630 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2631
e04a16fb
AG
2632 new->next = ctxp;
2633 ctxp = new;
c2952b01
APB
2634}
2635
2636/* Create a new parser context and make it the current one. */
2637
2638void
2639java_push_parser_context ()
2640{
2641 create_new_parser_context (0);
e04a16fb 2642 if (ctxp->next)
5e942c50
APB
2643 {
2644 ctxp->incomplete_class = ctxp->next->incomplete_class;
2645 ctxp->gclass_list = ctxp->next->gclass_list;
2646 }
e04a16fb
AG
2647}
2648
c2952b01
APB
2649void
2650java_pop_parser_context (generate)
2651 int generate;
2652{
2653 tree current;
2654 struct parser_ctxt *toFree, *next;
2655
2656 if (!ctxp)
2657 return;
2658
2659 toFree = ctxp;
2660 next = ctxp->next;
2661 if (next)
2662 {
2663 next->incomplete_class = ctxp->incomplete_class;
2664 next->gclass_list = ctxp->gclass_list;
2665 lineno = ctxp->lineno;
19e223db 2666 current_class = ctxp->class_type;
c2952b01
APB
2667 }
2668
d19cbcb5
TT
2669 /* If the old and new lexers differ, then free the old one. */
2670 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2671 java_destroy_lexer (ctxp->lexer);
2672
c2952b01
APB
2673 /* Set the single import class file flag to 0 for the current list
2674 of imported things */
2675 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2676 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2677
2678 /* And restore those of the previous context */
2679 if ((ctxp = next)) /* Assignment is really meant here */
2680 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2681 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2682
2683 /* If we pushed a context to parse a class intended to be generated,
2684 we keep it so we can remember the class. What we could actually
2685 do is to just update a list of class names. */
2686 if (generate)
2687 {
2688 toFree->next = ctxp_for_generation;
2689 ctxp_for_generation = toFree;
2690 }
2691 else
2692 free (toFree);
2693}
2694
2695/* Create a parser context for the use of saving some global
2696 variables. */
2697
e04a16fb
AG
2698void
2699java_parser_context_save_global ()
2700{
22eed1e6
APB
2701 if (!ctxp)
2702 {
2703 java_push_parser_context ();
ee07f4f4
APB
2704 ctxp->saved_data_ctx = 1;
2705 }
c2952b01
APB
2706
2707 /* If this context already stores data, create a new one suitable
2708 for data storage. */
ee07f4f4 2709 else if (ctxp->saved_data)
c2952b01
APB
2710 create_new_parser_context (1);
2711
e04a16fb 2712 ctxp->lineno = lineno;
19e223db 2713 ctxp->class_type = current_class;
e04a16fb 2714 ctxp->filename = input_filename;
19e223db 2715 ctxp->function_decl = current_function_decl;
ee07f4f4 2716 ctxp->saved_data = 1;
e04a16fb
AG
2717}
2718
c2952b01
APB
2719/* Restore some global variables from the previous context. Make the
2720 previous context the current one. */
2721
e04a16fb
AG
2722void
2723java_parser_context_restore_global ()
2724{
e04a16fb 2725 lineno = ctxp->lineno;
19e223db 2726 current_class = ctxp->class_type;
e04a16fb 2727 input_filename = ctxp->filename;
dba41d30
APB
2728 if (wfl_operator)
2729 {
2730 tree s;
2731 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2732 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2733 }
19e223db 2734 current_function_decl = ctxp->function_decl;
c2952b01 2735 ctxp->saved_data = 0;
ee07f4f4
APB
2736 if (ctxp->saved_data_ctx)
2737 java_pop_parser_context (0);
e04a16fb
AG
2738}
2739
c2952b01
APB
2740/* Suspend vital data for the current class/function being parsed so
2741 that an other class can be parsed. Used to let local/anonymous
2742 classes be parsed. */
2743
2744static void
2745java_parser_context_suspend ()
e04a16fb 2746{
c2952b01 2747 /* This makes debugging through java_debug_context easier */
3b304f5b 2748 static const char *name = "<inner buffer context>";
e04a16fb 2749
c2952b01
APB
2750 /* Duplicate the previous context, use it to save the globals we're
2751 interested in */
2752 create_new_parser_context (1);
19e223db
MM
2753 ctxp->function_decl = current_function_decl;
2754 ctxp->class_type = current_class;
5e942c50 2755
c2952b01
APB
2756 /* Then create a new context which inherits all data from the
2757 previous one. This will be the new current context */
2758 create_new_parser_context (1);
2759
2760 /* Help debugging */
2761 ctxp->next->filename = name;
2762}
2763
2764/* Resume vital data for the current class/function being parsed so
2765 that an other class can be parsed. Used to let local/anonymous
2766 classes be parsed. The trick is the data storing file position
2767 informations must be restored to their current value, so parsing
2768 can resume as if no context was ever saved. */
2769
2770static void
2771java_parser_context_resume ()
2772{
2773 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2774 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2775 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2776
2777 /* We need to inherit the list of classes to complete/generate */
2778 restored->incomplete_class = old->incomplete_class;
2779 restored->gclass_list = old->gclass_list;
2780 restored->classd_list = old->classd_list;
2781 restored->class_list = old->class_list;
2782
2783 /* Restore the current class and function from the saver */
19e223db
MM
2784 current_class = saver->class_type;
2785 current_function_decl = saver->function_decl;
c2952b01
APB
2786
2787 /* Retrive the restored context */
2788 ctxp = restored;
2789
2790 /* Re-installed the data for the parsing to carry on */
2791 bcopy (&old->marker_begining, &ctxp->marker_begining,
2792 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2793
2794 /* Buffer context can now be discarded */
2795 free (saver);
2796 free (old);
2797}
2798
2799/* Add a new anchor node to which all statement(s) initializing static
2800 and non static initialized upon declaration field(s) will be
2801 linked. */
2802
2803static void
2804java_parser_context_push_initialized_field ()
2805{
2806 tree node;
2807
2808 node = build_tree_list (NULL_TREE, NULL_TREE);
2809 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2810 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2811
2812 node = build_tree_list (NULL_TREE, NULL_TREE);
2813 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = node;
2815
2816 node = build_tree_list (NULL_TREE, NULL_TREE);
2817 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2818 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2819}
2820
2821/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2822 remember them so we can use it to create and populate the finit$
c2952b01
APB
2823 or <clinit> functions. */
2824
2825static void
2826java_parser_context_pop_initialized_field ()
2827{
2828 tree stmts;
2829 tree class_type = TREE_TYPE (GET_CPC ());
2830
2831 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2832 {
c2952b01
APB
2833 stmts = CPC_INITIALIZER_STMT (ctxp);
2834 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2837 }
2838
c2952b01
APB
2839 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2840 {
2841 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2842 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2843 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2844 /* Keep initialization in order to enforce 8.5 */
2845 if (stmts && !java_error_count)
2846 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2847 }
e04a16fb 2848
c2952b01
APB
2849 /* JDK 1.1 instance initializers */
2850 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2851 {
c2952b01
APB
2852 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2853 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2854 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2855 if (stmts && !java_error_count)
2856 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2857 }
c2952b01
APB
2858}
2859
2860static tree
2861reorder_static_initialized (list)
2862 tree list;
2863{
2864 /* We have to keep things in order. The alias initializer have to
2865 come first, then the initialized regular field, in reverse to
2866 keep them in lexical order. */
2867 tree marker, previous = NULL_TREE;
2868 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2869 if (TREE_CODE (marker) == TREE_LIST
2870 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2871 break;
2872
2873 /* No static initialized, the list is fine as is */
2874 if (!previous)
2875 list = TREE_CHAIN (marker);
2876
2877 /* No marker? reverse the whole list */
2878 else if (!marker)
2879 list = nreverse (list);
2880
2881 /* Otherwise, reverse what's after the marker and the new reordered
2882 sublist will replace the marker. */
b351b287 2883 else
c2952b01
APB
2884 {
2885 TREE_CHAIN (previous) = NULL_TREE;
2886 list = nreverse (list);
2887 list = chainon (TREE_CHAIN (marker), list);
2888 }
2889 return list;
e04a16fb
AG
2890}
2891
c2952b01
APB
2892/* Helper functions to dump the parser context stack. */
2893
2894#define TAB_CONTEXT(C) \
2895 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2896
2897static void
2898java_debug_context_do (tab)
2899 int tab;
2900{
ee07f4f4
APB
2901 struct parser_ctxt *copy = ctxp;
2902 while (copy)
2903 {
c2952b01 2904 TAB_CONTEXT (tab);
ee07f4f4 2905 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2906 TAB_CONTEXT (tab);
ee07f4f4 2907 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2908 TAB_CONTEXT (tab);
2909 fprintf (stderr, "lineno: %d\n", copy->lineno);
2910 TAB_CONTEXT (tab);
ee07f4f4
APB
2911 fprintf (stderr, "package: %s\n",
2912 (copy->package ?
2913 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2914 TAB_CONTEXT (tab);
ee07f4f4 2915 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2916 TAB_CONTEXT (tab);
ee07f4f4
APB
2917 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2918 copy = copy->next;
2919 tab += 2;
2920 }
ee07f4f4
APB
2921}
2922
c2952b01
APB
2923/* Dump the stacked up parser contexts. Intended to be called from a
2924 debugger. */
2925
ee07f4f4
APB
2926void
2927java_debug_context ()
2928{
2929 java_debug_context_do (0);
2930}
2931
c2952b01
APB
2932\f
2933
2934/* Flag for the error report routine to issue the error the first time
2935 it's called (overriding the default behavior which is to drop the
2936 first invocation and honor the second one, taking advantage of a
2937 richer context. */
2938static int force_error = 0;
ee07f4f4 2939
8119c720
APB
2940/* Reporting an constructor invocation error. */
2941static void
2942parse_ctor_invocation_error ()
2943{
2944 if (DECL_CONSTRUCTOR_P (current_function_decl))
2945 yyerror ("Constructor invocation must be first thing in a constructor");
2946 else
2947 yyerror ("Only constructors can invoke constructors");
2948}
2949
2950/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2951
2952static tree
2953parse_jdk1_1_error (msg)
49f48c71 2954 const char *msg;
b67d701b
PB
2955{
2956 sorry (": `%s' JDK1.1(TM) feature", msg);
2957 java_error_count++;
9bbc7d9f 2958 return empty_stmt_node;
b67d701b
PB
2959}
2960
e04a16fb
AG
2961static int do_warning = 0;
2962
2963void
2964yyerror (msg)
49f48c71 2965 const char *msg;
e04a16fb
AG
2966{
2967 static java_lc elc;
2968 static int prev_lineno;
49f48c71 2969 static const char *prev_msg;
e04a16fb 2970
0a2138e2 2971 int save_lineno;
e04a16fb 2972 char *remainder, *code_from_source;
e04a16fb
AG
2973
2974 if (!force_error && prev_lineno == lineno)
2975 return;
2976
2977 /* Save current error location but report latter, when the context is
2978 richer. */
2979 if (ctxp->java_error_flag == 0)
2980 {
2981 ctxp->java_error_flag = 1;
2982 elc = ctxp->elc;
2983 /* Do something to use the previous line if we're reaching the
2984 end of the file... */
2985#ifdef VERBOSE_SKELETON
2986 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2987#endif
2988 return;
2989 }
2990
2991 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2992 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2993 return;
2994
2995 ctxp->java_error_flag = 0;
2996 if (do_warning)
2997 java_warning_count++;
2998 else
2999 java_error_count++;
3000
807bc1db 3001 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
3002 {
3003 elc.col = ctxp->p_line->char_col-1;
3004 elc.line = ctxp->p_line->lineno;
3005 }
3006
3007 save_lineno = lineno;
3008 prev_lineno = lineno = elc.line;
3009 prev_msg = msg;
3010
3011 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3012 obstack_grow0 (&temporary_obstack,
3013 code_from_source, strlen (code_from_source));
3014 remainder = obstack_finish (&temporary_obstack);
3015 if (do_warning)
3016 warning ("%s.\n%s", msg, remainder);
3017 else
3018 error ("%s.\n%s", msg, remainder);
3019
3020 /* This allow us to cheaply avoid an extra 'Invalid expression
3021 statement' error report when errors have been already reported on
3022 the same line. This occurs when we report an error but don't have
3023 a synchronization point other than ';', which
3024 expression_statement is the only one to take care of. */
3025 ctxp->prevent_ese = lineno = save_lineno;
3026}
3027
3028static void
15fdcfe9 3029issue_warning_error_from_context (cl, msg, ap)
5e942c50 3030 tree cl;
d4476be2 3031 const char *msg;
15fdcfe9 3032 va_list ap;
5e942c50 3033{
3b304f5b 3034 const char *saved, *saved_input_filename;
15fdcfe9
PB
3035 char buffer [4096];
3036 vsprintf (buffer, msg, ap);
3037 force_error = 1;
5e942c50
APB
3038
3039 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3040 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3041 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3042
3043 /* We have a CL, that's a good reason for using it if it contains data */
3044 saved = ctxp->filename;
3045 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3046 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3047 saved_input_filename = input_filename;
3048 input_filename = ctxp->filename;
15fdcfe9
PB
3049 java_error (NULL);
3050 java_error (buffer);
5e942c50 3051 ctxp->filename = saved;
1886c9d8 3052 input_filename = saved_input_filename;
15fdcfe9 3053 force_error = 0;
5e942c50
APB
3054}
3055
e04a16fb
AG
3056/* Issue an error message at a current source line CL */
3057
15fdcfe9 3058void
df32d2ce 3059parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3060{
d4476be2 3061#ifndef ANSI_PROTOTYPES
e04a16fb 3062 tree cl;
d4476be2 3063 const char *msg;
e04a16fb 3064#endif
e04a16fb
AG
3065 va_list ap;
3066
3067 VA_START (ap, msg);
d4476be2 3068#ifndef ANSI_PROTOTYPES
e04a16fb 3069 cl = va_arg (ap, tree);
d4476be2 3070 msg = va_arg (ap, const char *);
e04a16fb 3071#endif
15fdcfe9
PB
3072 issue_warning_error_from_context (cl, msg, ap);
3073 va_end (ap);
e04a16fb
AG
3074}
3075
3076/* Issue a warning at a current source line CL */
3077
3078static void
df32d2ce 3079parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3080{
d4476be2 3081#ifndef ANSI_PROTOTYPES
e04a16fb 3082 tree cl;
d4476be2 3083 const char *msg;
e04a16fb 3084#endif
e04a16fb
AG
3085 va_list ap;
3086
3087 VA_START (ap, msg);
d4476be2 3088#ifndef ANSI_PROTOTYPES
e04a16fb 3089 cl = va_arg (ap, tree);
d4476be2 3090 msg = va_arg (ap, const char *);
e04a16fb 3091#endif
e04a16fb 3092
c877974e 3093 force_error = do_warning = 1;
15fdcfe9 3094 issue_warning_error_from_context (cl, msg, ap);
c877974e 3095 do_warning = force_error = 0;
15fdcfe9 3096 va_end (ap);
e04a16fb
AG
3097}
3098
82371d41
APB
3099static tree
3100find_expr_with_wfl (node)
3101 tree node;
3102{
3103 while (node)
3104 {
3105 char code;
3106 tree to_return;
3107
3108 switch (TREE_CODE (node))
3109 {
3110 case BLOCK:
c0d87ff6
PB
3111 node = BLOCK_EXPR_BODY (node);
3112 continue;
82371d41
APB
3113
3114 case COMPOUND_EXPR:
3115 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3116 if (to_return)
3117 return to_return;
c0d87ff6
PB
3118 node = TREE_OPERAND (node, 1);
3119 continue;
82371d41
APB
3120
3121 case LOOP_EXPR:
c0d87ff6
PB
3122 node = TREE_OPERAND (node, 0);
3123 continue;
82371d41
APB
3124
3125 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3126 node = TREE_OPERAND (node, 1);
3127 continue;
3128
82371d41
APB
3129 default:
3130 code = TREE_CODE_CLASS (TREE_CODE (node));
3131 if (((code == '1') || (code == '2') || (code == 'e'))
3132 && EXPR_WFL_LINECOL (node))
3133 return node;
ba179f9f 3134 return NULL_TREE;
82371d41
APB
3135 }
3136 }
3137 return NULL_TREE;
3138}
3139
3140/* Issue a missing return statement error. Uses METHOD to figure the
3141 last line of the method the error occurs in. */
3142
3143static void
3144missing_return_error (method)
3145 tree method;
3146{
3147 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3148 parse_error_context (wfl_operator, "Missing return statement");
3149}
3150
3151/* Issue an unreachable statement error. From NODE, find the next
3152 statement to report appropriately. */
3153static void
3154unreachable_stmt_error (node)
3155 tree node;
3156{
3157 /* Browse node to find the next expression node that has a WFL. Use
3158 the location to report the error */
3159 if (TREE_CODE (node) == COMPOUND_EXPR)
3160 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3161 else
3162 node = find_expr_with_wfl (node);
3163
3164 if (node)
3165 {
3166 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3167 parse_error_context (wfl_operator, "Unreachable statement");
3168 }
3169 else
400500c4 3170 abort ();
82371d41
APB
3171}
3172
c877974e 3173int
e04a16fb
AG
3174java_report_errors ()
3175{
3176 if (java_error_count)
3177 fprintf (stderr, "%d error%s",
3178 java_error_count, (java_error_count == 1 ? "" : "s"));
3179 if (java_warning_count)
3180 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3181 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3182 if (java_error_count || java_warning_count)
3183 putc ('\n', stderr);
c877974e 3184 return java_error_count;
e04a16fb
AG
3185}
3186
3187static char *
3188java_accstring_lookup (flags)
3189 int flags;
3190{
3191 static char buffer [80];
3192#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3193
3194 /* Access modifier looked-up first for easier report on forbidden
3195 access. */
3196 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3197 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3198 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3199 if (flags & ACC_STATIC) COPY_RETURN ("static");
3200 if (flags & ACC_FINAL) COPY_RETURN ("final");
3201 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3202 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3203 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3204 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3205 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3206 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3207
3208 buffer [0] = '\0';
3209 return buffer;
3210#undef COPY_RETURN
3211}
3212
b67d701b
PB
3213/* Issuing error messages upon redefinition of classes, interfaces or
3214 variables. */
3215
e04a16fb 3216static void
b67d701b 3217classitf_redefinition_error (context, id, decl, cl)
49f48c71 3218 const char *context;
e04a16fb
AG
3219 tree id, decl, cl;
3220{
3221 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3222 context, IDENTIFIER_POINTER (id),
3223 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3224 /* Here we should point out where its redefined. It's a unicode. FIXME */
3225}
3226
b67d701b
PB
3227static void
3228variable_redefinition_error (context, name, type, line)
3229 tree context, name, type;
3230 int line;
3231{
49f48c71 3232 const char *type_name;
b67d701b
PB
3233
3234 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3235 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3236 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3237 else
0a2138e2 3238 type_name = lang_printable_name (type, 0);
b67d701b
PB
3239
3240 parse_error_context (context,
781b0558 3241 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3242 IDENTIFIER_POINTER (name),
3243 type_name, IDENTIFIER_POINTER (name), line);
3244}
3245
c583dd46
APB
3246static tree
3247build_array_from_name (type, type_wfl, name, ret_name)
3248 tree type, type_wfl, name, *ret_name;
3249{
3250 int more_dims = 0;
49f48c71 3251 const char *string;
c583dd46
APB
3252
3253 /* Eventually get more dims */
3254 string = IDENTIFIER_POINTER (name);
3255 while (string [more_dims] == '[')
3256 more_dims++;
3257
3258 /* If we have, then craft a new type for this variable */
3259 if (more_dims)
3260 {
c0d87ff6 3261 name = get_identifier (&string [more_dims]);
c583dd46 3262
34f4db93
APB
3263 /* If we have a pointer, use its type */
3264 if (TREE_CODE (type) == POINTER_TYPE)
3265 type = TREE_TYPE (type);
c583dd46
APB
3266
3267 /* Building the first dimension of a primitive type uses this
3268 function */
3269 if (JPRIMITIVE_TYPE_P (type))
3270 {
3271 type = build_java_array_type (type, -1);
22eed1e6 3272 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3273 more_dims--;
3274 }
3275 /* Otherwise, if we have a WFL for this type, use it (the type
3276 is already an array on an unresolved type, and we just keep
3277 on adding dimensions) */
3278 else if (type_wfl)
3279 type = type_wfl;
3280
3281 /* Add all the dimensions */
3282 while (more_dims--)
3283 type = build_unresolved_array_type (type);
3284
3285 /* The type may have been incomplete in the first place */
3286 if (type_wfl)
3287 type = obtain_incomplete_type (type);
3288 }
3289
c2952b01
APB
3290 if (ret_name)
3291 *ret_name = name;
c583dd46
APB
3292 return type;
3293}
3294
e04a16fb
AG
3295/* Build something that the type identifier resolver will identify as
3296 being an array to an unresolved type. TYPE_WFL is a WFL on a
3297 identifier. */
3298
3299static tree
3300build_unresolved_array_type (type_or_wfl)
3301 tree type_or_wfl;
3302{
49f48c71 3303 const char *ptr;
e04a16fb 3304
1886c9d8 3305 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3306 just create a array type */
3307 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3308 {
3309 tree type = build_java_array_type (type_or_wfl, -1);
3310 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3311 return type;
3312 }
3313
3314 obstack_1grow (&temporary_obstack, '[');
3315 obstack_grow0 (&temporary_obstack,
3316 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3317 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3318 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3319 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3320 return type_or_wfl;
e04a16fb
AG
3321}
3322
e04a16fb
AG
3323static void
3324parser_add_interface (class_decl, interface_decl, wfl)
3325 tree class_decl, interface_decl, wfl;
3326{
3327 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3328 parse_error_context (wfl, "Interface `%s' repeated",
3329 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3330}
3331
3332/* Bulk of common class/interface checks. Return 1 if an error was
3333 encountered. TAG is 0 for a class, 1 for an interface. */
3334
3335static int
3336check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3337 int is_interface, flags;
3338 tree raw_name, qualified_name, decl, cl;
3339{
3340 tree node;
c2952b01
APB
3341 int sca = 0; /* Static class allowed */
3342 int icaf = 0; /* Inner class allowed flags */
3343 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3344
3345 if (!quiet_flag)
c2952b01
APB
3346 fprintf (stderr, " %s%s %s",
3347 (CPC_INNER_P () ? "inner" : ""),
3348 (is_interface ? "interface" : "class"),
e04a16fb
AG
3349 IDENTIFIER_POINTER (qualified_name));
3350
3351 /* Scope of an interface/class type name:
3352 - Can't be imported by a single type import
3353 - Can't already exists in the package */
3354 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3355 && (node = find_name_in_single_imports (raw_name))
3356 && !CPC_INNER_P ())
e04a16fb
AG
3357 {
3358 parse_error_context
3359 (cl, "%s name `%s' clashes with imported type `%s'",
3360 (is_interface ? "Interface" : "Class"),
3361 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3362 return 1;
3363 }
3364 if (decl && CLASS_COMPLETE_P (decl))
3365 {
b67d701b
PB
3366 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3367 qualified_name, decl, cl);
e04a16fb
AG
3368 return 1;
3369 }
3370
c2952b01
APB
3371 if (check_inner_class_redefinition (raw_name, cl))
3372 return 1;
3373
3374 /* If public, file name should match class/interface name, except
3375 when dealing with an inner class */
3376 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3377 {
49f48c71 3378 const char *f;
e04a16fb
AG
3379
3380 /* Contains OS dependent assumption on path separator. FIXME */
3381 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3382 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3383 f--)
3384 ;
847fe791 3385 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3386 f++;
3387 if (strncmp (IDENTIFIER_POINTER (raw_name),
3388 f , IDENTIFIER_LENGTH (raw_name)) ||
3389 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3390 parse_error_context
3391 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3392 (is_interface ? "interface" : "class"),
3393 IDENTIFIER_POINTER (qualified_name),
3394 IDENTIFIER_POINTER (raw_name));
3395 }
3396
c2952b01
APB
3397 /* Static classes can be declared only in top level classes. Note:
3398 once static, a inner class is a top level class. */
3399 if (flags & ACC_STATIC)
3400 {
3401 /* Catch the specific error of declaring an class inner class
3402 with no toplevel enclosing class. Prevent check_modifiers from
3403 complaining a second time */
3404 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3405 {
3406 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3407 IDENTIFIER_POINTER (qualified_name));
3408 sca = ACC_STATIC;
3409 }
3410 /* Else, in the context of a top-level class declaration, let
3411 `check_modifiers' do its job, otherwise, give it a go */
3412 else
3413 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3414 }
3415
a40d21da 3416 /* Inner classes can be declared private or protected
c2952b01
APB
3417 within their enclosing classes. */
3418 if (CPC_INNER_P ())
3419 {
3420 /* A class which is local to a block can't be public, private,
3421 protected or static. But it is created final, so allow this
3422 one. */
3423 if (current_function_decl)
3424 icaf = sca = uaaf = ACC_FINAL;
3425 else
3426 {
3427 check_modifiers_consistency (flags);
3428 icaf = ACC_PRIVATE|ACC_PROTECTED;
3429 }
3430 }
3431
a40d21da
APB
3432 if (is_interface)
3433 {
3434 if (CPC_INNER_P ())
3435 uaaf = INTERFACE_INNER_MODIFIERS;
3436 else
3437 uaaf = INTERFACE_MODIFIERS;
3438
3439 check_modifiers ("Illegal modifier `%s' for interface declaration",
3440 flags, uaaf);
3441 }
2884c41e 3442 else
a40d21da
APB
3443 check_modifiers ((current_function_decl ?
3444 "Illegal modifier `%s' for local class declaration" :
3445 "Illegal modifier `%s' for class declaration"),
c2952b01 3446 flags, uaaf|sca|icaf);
e04a16fb
AG
3447 return 0;
3448}
3449
c2952b01
APB
3450static void
3451make_nested_class_name (cpc_list)
3452 tree cpc_list;
3453{
3454 tree name;
3455
3456 if (!cpc_list)
3457 return;
3458 else
3459 make_nested_class_name (TREE_CHAIN (cpc_list));
3460
3461 /* Pick the qualified name when dealing with the first upmost
3462 enclosing class */
3463 name = (TREE_CHAIN (cpc_list) ?
3464 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3465 obstack_grow (&temporary_obstack,
3466 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3467 /* Why is NO_DOLLAR_IN_LABEL defined? */
3468#if 0
3469#ifdef NO_DOLLAR_IN_LABEL
400500c4 3470 internal_error ("Can't use '$' as a separator for inner classes");
c2952b01
APB
3471#endif
3472#endif
3473 obstack_1grow (&temporary_obstack, '$');
3474}
3475
3476/* Can't redefine a class already defined in an earlier scope. */
3477
3478static int
3479check_inner_class_redefinition (raw_name, cl)
3480 tree raw_name, cl;
3481{
3482 tree scope_list;
3483
3484 for (scope_list = GET_CPC_LIST (); scope_list;
3485 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3486 if (raw_name == GET_CPC_UN_NODE (scope_list))
3487 {
3488 parse_error_context
3489 (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",
3490 IDENTIFIER_POINTER (raw_name));
3491 return 1;
3492 }
3493 return 0;
3494}
3495
3496static tree
3497find_as_inner_class (enclosing, name, cl)
3498 tree enclosing, name, cl;
3499{
3500 tree qual, to_return;
3501 if (!enclosing)
3502 return NULL_TREE;
3503
3504 name = TYPE_NAME (name);
3505
3506 /* First search: within the scope of `enclosing', search for name */
3507 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3508 qual = EXPR_WFL_QUALIFICATION (cl);
3509 else if (cl)
3510 qual = build_tree_list (cl, NULL_TREE);
3511 else
3512 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3513
3514 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3515 return to_return;
3516
3517 /* We're dealing with a qualified name. Try to resolve thing until
3518 we get something that is an enclosing class. */
3519 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3520 {
3521 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3522
0c2b8145
APB
3523 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3524 qual = TREE_CHAIN (qual))
c2952b01
APB
3525 {
3526 acc = merge_qualified_name (acc,
3527 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3528 BUILD_PTR_FROM_NAME (ptr, acc);
3529 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3530 }
3531
3532 /* A NULL qual and a decl means that the search ended
3533 successfully?!? We have to do something then. FIXME */
3534
3535 if (decl)
3536 enclosing = decl;
3537 else
3538 qual = EXPR_WFL_QUALIFICATION (cl);
3539 }
3540 /* Otherwise, create a qual for the other part of the resolution. */
3541 else
3542 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3543
1e12ab9b 3544 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3545}
3546
3547/* We go inside the list of sub classes and try to find a way
3548 through. */
3549
3550static tree
3551find_as_inner_class_do (qual, enclosing)
3552 tree qual, enclosing;
3553{
3554 if (!qual)
3555 return NULL_TREE;
3556
3557 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3558 {
3559 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3560 tree next_enclosing = NULL_TREE;
3561 tree inner_list;
3562
3563 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3564 inner_list; inner_list = TREE_CHAIN (inner_list))
3565 {
3566 if (TREE_VALUE (inner_list) == name_to_match)
3567 {
3568 next_enclosing = TREE_PURPOSE (inner_list);
3569 break;
3570 }
3571 }
3572 enclosing = next_enclosing;
3573 }
3574
3575 return (!qual && enclosing ? enclosing : NULL_TREE);
3576}
3577
3578/* Reach all inner classes and tie their unqualified name to a
3579 DECL. */
3580
3581static void
3582set_nested_class_simple_name_value (outer, set)
3583 tree outer;
3584 int set;
3585{
3586 tree l;
3587
3588 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3589 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3590 TREE_PURPOSE (l) : NULL_TREE);
3591}
3592
3593static void
3594link_nested_class_to_enclosing ()
3595{
3596 if (GET_ENCLOSING_CPC ())
3597 {
3598 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3599 DECL_INNER_CLASS_LIST (enclosing) =
3600 tree_cons (GET_CPC (), GET_CPC_UN (),
3601 DECL_INNER_CLASS_LIST (enclosing));
3602 enclosing = enclosing;
3603 }
3604}
3605
3606static tree
3607maybe_make_nested_class_name (name)
3608 tree name;
3609{
3610 tree id = NULL_TREE;
3611
3612 if (CPC_INNER_P ())
3613 {
3614 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3615 obstack_grow0 (&temporary_obstack,
3616 IDENTIFIER_POINTER (name),
3617 IDENTIFIER_LENGTH (name));
c2952b01
APB
3618 id = get_identifier (obstack_finish (&temporary_obstack));
3619 if (ctxp->package)
3620 QUALIFIED_P (id) = 1;
3621 }
3622 return id;
3623}
3624
3625/* If DECL is NULL, create and push a new DECL, record the current
3626 line CL and do other maintenance things. */
3627
e04a16fb 3628static tree
c2952b01
APB
3629maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3630 tree decl, raw_name, qualified_name, cl;
e04a16fb 3631{
5e942c50 3632 if (!decl)
e04a16fb 3633 decl = push_class (make_class (), qualified_name);
c2952b01 3634
e04a16fb
AG
3635 /* Take care of the file and line business */
3636 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3637 /* If we're emiting xrefs, store the line/col number information */
3638 if (flag_emit_xref)
3639 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3640 else
3641 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3642 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3643 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3644 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3645
c2952b01
APB
3646 PUSH_CPC (decl, raw_name);
3647 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3648
e04a16fb
AG
3649 /* Link the declaration to the already seen ones */
3650 TREE_CHAIN (decl) = ctxp->class_list;
3651 ctxp->class_list = decl;
5e942c50 3652
23a79c61 3653 /* Create a new nodes in the global lists */
5e942c50 3654 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3655 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3656
e04a16fb
AG
3657 /* Install a new dependency list element */
3658 create_jdep_list (ctxp);
3659
3660 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3661 IDENTIFIER_POINTER (qualified_name)));
3662 return decl;
3663}
3664
3665static void
3666add_superinterfaces (decl, interface_list)
3667 tree decl, interface_list;
3668{
3669 tree node;
3670 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3671 takes care of ensuring that:
3672 - This is an accessible interface type,
3673 - Circularity detection.
3674 parser_add_interface is then called. If present but not defined,
3675 the check operation is delayed until the super interface gets
3676 defined. */
3677 for (node = interface_list; node; node = TREE_CHAIN (node))
3678 {
15fdcfe9 3679 tree current = TREE_PURPOSE (node);
5e942c50
APB
3680 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3681 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3682 {
5e942c50
APB
3683 if (!parser_check_super_interface (idecl, decl, current))
3684 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3685 }
3686 else
3687 register_incomplete_type (JDEP_INTERFACE,
3688 current, decl, NULL_TREE);
3689 }
3690}
3691
3692/* Create an interface in pass1 and return its decl. Return the
3693 interface's decl in pass 2. */
3694
3695static tree
3696create_interface (flags, id, super)
3697 int flags;
3698 tree id, super;
3699{
e04a16fb 3700 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3701 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3702 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3703
3704 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3705
3706 /* Basic checks: scope, redefinition, modifiers */
3707 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3708 {
3709 PUSH_ERROR ();
3710 return NULL_TREE;
3711 }
3712
3713 /* Suspend the current parsing context if we're parsing an inner
3714 interface */
3715 if (CPC_INNER_P ())
3716 java_parser_context_suspend ();
3717
3718 /* Push a new context for (static) initialized upon declaration fields */
3719 java_parser_context_push_initialized_field ();
e04a16fb
AG
3720
3721 /* Interface modifiers check
3722 - public/abstract allowed (already done at that point)
3723 - abstract is obsolete (comes first, it's a warning, or should be)
3724 - Can't use twice the same (checked in the modifier rule) */
c877974e 3725 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3726 parse_warning_context
3727 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3728 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3729
3730 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3731 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3732
3733 /* Set super info and mark the class a complete */
2aa11e97 3734 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3735 object_type_node, ctxp->interface_number);
3736 ctxp->interface_number = 0;
3737 CLASS_COMPLETE_P (decl) = 1;
3738 add_superinterfaces (decl, super);
3739
3740 return decl;
3741}
3742
c2952b01
APB
3743/* Anonymous class counter. Will be reset to 1 every time a non
3744 anonymous class gets created. */
3745static int anonymous_class_counter = 1;
3746
3747/* Patch anonymous class CLASS, by either extending or implementing
3748 DEP. */
3749
3750static void
3751patch_anonymous_class (type_decl, class_decl, wfl)
3752 tree type_decl, class_decl, wfl;
3753{
3754 tree class = TREE_TYPE (class_decl);
3755 tree type = TREE_TYPE (type_decl);
3756 tree binfo = TYPE_BINFO (class);
3757
3758 /* If it's an interface, implement it */
3759 if (CLASS_INTERFACE (type_decl))
3760 {
3761 tree s_binfo;
3762 int length;
3763
3764 if (parser_check_super_interface (type_decl, class_decl, wfl))
3765 return;
3766
3767 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3768 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3769 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3770 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3771 /* And add the interface */
3772 parser_add_interface (class_decl, type_decl, wfl);
3773 }
3774 /* Otherwise, it's a type we want to extend */
3775 else
3776 {
3777 if (parser_check_super (type_decl, class_decl, wfl))
3778 return;
3779 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3780 }
3781}
3782
3783static tree
3784create_anonymous_class (location, type_name)
3785 int location;
3786 tree type_name;
3787{
3788 char buffer [80];
3789 tree super = NULL_TREE, itf = NULL_TREE;
3790 tree id, type_decl, class;
3791
3792 /* The unqualified name of the anonymous class. It's just a number. */
3793 sprintf (buffer, "%d", anonymous_class_counter++);
3794 id = build_wfl_node (get_identifier (buffer));
3795 EXPR_WFL_LINECOL (id) = location;
3796
3797 /* We know about the type to extend/implement. We go ahead */
3798 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3799 {
3800 /* Create a class which either implements on extends the designated
3801 class. The class bears an innacessible name. */
3802 if (CLASS_INTERFACE (type_decl))
3803 {
3804 /* It's OK to modify it here. It's been already used and
3805 shouldn't be reused */
3806 ctxp->interface_number = 1;
3807 /* Interfaces should presented as a list of WFLs */
3808 itf = build_tree_list (type_name, NULL_TREE);
3809 }
3810 else
3811 super = type_name;
3812 }
3813
3814 class = create_class (ACC_FINAL, id, super, itf);
3815
3816 /* We didn't know anything about the stuff. We register a dependence. */
3817 if (!type_decl)
3818 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3819
3820 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3821 return class;
3822}
3823
a40d21da 3824/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3825 interface's decl in pass 2. */
3826
3827static tree
3828create_class (flags, id, super, interfaces)
3829 int flags;
3830 tree id, super, interfaces;
3831{
e04a16fb
AG
3832 tree raw_name = EXPR_WFL_NODE (id);
3833 tree class_id, decl;
9ee9b555 3834 tree super_decl_type;
e04a16fb 3835
98a52c2c 3836 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3837 decl = IDENTIFIER_CLASS_VALUE (class_id);
3838 EXPR_WFL_NODE (id) = class_id;
3839
3840 /* Basic check: scope, redefinition, modifiers */
3841 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3842 {
3843 PUSH_ERROR ();
3844 return NULL_TREE;
3845 }
3846
3847 /* Suspend the current parsing context if we're parsing an inner
3848 class or an anonymous class. */
3849 if (CPC_INNER_P ())
3850 java_parser_context_suspend ();
3851 /* Push a new context for (static) initialized upon declaration fields */
3852 java_parser_context_push_initialized_field ();
e04a16fb
AG
3853
3854 /* Class modifier check:
3855 - Allowed modifier (already done at that point)
3856 - abstract AND final forbidden
3857 - Public classes defined in the correct file */
3858 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3859 parse_error_context
3860 (id, "Class `%s' can't be declared both abstract and final",
3861 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3862
3863 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3864 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3865
3866 /* If SUPER exists, use it, otherwise use Object */
3867 if (super)
3868 {
3869 /* Can't extend java.lang.Object */
3870 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3871 {
3872 parse_error_context (id, "Can't extend `java.lang.Object'");
3873 return NULL_TREE;
3874 }
3875
2c3199bc
PB
3876 super_decl_type =
3877 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3878 }
3879 else if (TREE_TYPE (decl) != object_type_node)
3880 super_decl_type = object_type_node;
3881 /* We're defining java.lang.Object */
3882 else
3883 super_decl_type = NULL_TREE;
3884
6d003d5c
BM
3885 /* A class nested in an interface is implicitly static. */
3886 if (INNER_CLASS_DECL_P (decl)
3887 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3888 {
3889 flags |= ACC_STATIC;
3890 }
3891
3892 /* Set super info and mark the class as complete. */
e04a16fb
AG
3893 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3894 ctxp->interface_number);
3895 ctxp->interface_number = 0;
3896 CLASS_COMPLETE_P (decl) = 1;
3897 add_superinterfaces (decl, interfaces);
3898
c2952b01
APB
3899 /* Add the private this$<n> field, Replicate final locals still in
3900 scope as private final fields mangled like val$<local_name>.
3901 This doesn't not occur for top level (static) inner classes. */
3902 if (PURE_INNER_CLASS_DECL_P (decl))
3903 add_inner_class_fields (decl, current_function_decl);
3904
7f10c2e2
APB
3905 /* If doing xref, store the location at which the inherited class
3906 (if any) was seen. */
3907 if (flag_emit_xref && super)
3908 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3909
5e942c50
APB
3910 /* Eventually sets the @deprecated tag flag */
3911 CHECK_DEPRECATED (decl);
3912
165f37bc
APB
3913 /* Reset the anonymous class counter when declaring non inner classes */
3914 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3915 anonymous_class_counter = 1;
3916
e04a16fb
AG
3917 return decl;
3918}
3919
c2952b01 3920/* End a class declaration: register the statements used to create
c00f0fb2 3921 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3922 parser context if necessary. */
3923
3924static void
3925end_class_declaration (resume)
3926 int resume;
3927{
3928 /* If an error occured, context weren't pushed and won't need to be
3929 popped by a resume. */
3930 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3931
3932 java_parser_context_pop_initialized_field ();
3933 POP_CPC ();
3934 if (resume && no_error_occured)
3935 java_parser_context_resume ();
93220702
APB
3936
3937 /* We're ending a class declaration, this is a good time to reset
3938 the interface cout. Note that might have been already done in
3939 create_interface, but if at that time an inner class was being
3940 dealt with, the interface count was reset in a context created
3941 for the sake of handling inner classes declaration. */
3942 ctxp->interface_number = 0;
c2952b01
APB
3943}
3944
3945static void
3946add_inner_class_fields (class_decl, fct_decl)
3947 tree class_decl;
3948 tree fct_decl;
3949{
3950 tree block, marker, f;
3951
3952 f = add_field (TREE_TYPE (class_decl),
3953 build_current_thisn (TREE_TYPE (class_decl)),
3954 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3955 ACC_PRIVATE);
3956 FIELD_THISN (f) = 1;
3957
3958 if (!fct_decl)
3959 return;
3960
3961 for (block = GET_CURRENT_BLOCK (fct_decl);
3962 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3963 {
3964 tree decl;
3965 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3966 {
1f8f4a0b 3967 tree name, pname;
c2952b01
APB
3968 tree wfl, init, list;
3969
3970 /* Avoid non final arguments. */
c7303e41 3971 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3972 continue;
3973
3974 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3975 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3976 wfl = build_wfl_node (name);
3977 init = build_wfl_node (pname);
c2952b01 3978 /* Build an initialization for the field: it will be
c00f0fb2 3979 initialized by a parameter added to finit$, bearing a
c2952b01 3980 mangled name of the field itself (param$<n>.) The
c00f0fb2 3981 parameter is provided to finit$ by the constructor
c2952b01
APB
3982 invoking it (hence the constructor will also feature a
3983 hidden parameter, set to the value of the outer context
3984 local at the time the inner class is created.)
3985
3986 Note: we take into account all possible locals that can
3987 be accessed by the inner class. It's actually not trivial
3988 to minimize these aliases down to the ones really
3989 used. One way to do that would be to expand all regular
c00f0fb2 3990 methods first, then finit$ to get a picture of what's
c2952b01
APB
3991 used. It works with the exception that we would have to
3992 go back on all constructor invoked in regular methods to
3993 have their invokation reworked (to include the right amount
3994 of alias initializer parameters.)
3995
3996 The only real way around, I think, is a first pass to
3997 identify locals really used in the inner class. We leave
3998 the flag FIELD_LOCAL_ALIAS_USED around for that future
3999 use.
4000
4001 On the other hand, it only affect local inner classes,
c00f0fb2 4002 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4003 unecessary arguments. It's easy for a developper to keep
4004 this number of parameter down by using the `final'
4005 keyword only when necessary. For the time being, we can
4006 issue a warning on unecessary finals. FIXME */
4007 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4008 wfl, init);
4009
4010 /* Register the field. The TREE_LIST holding the part
4011 initialized/initializer will be marked ARG_FINAL_P so
4012 that the created field can be marked
4013 FIELD_LOCAL_ALIAS. */
4014 list = build_tree_list (wfl, init);
4015 ARG_FINAL_P (list) = 1;
4016 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4017 }
4018 }
4019
4020 if (!CPC_INITIALIZER_STMT (ctxp))
4021 return;
4022
4023 /* If we ever registered an alias field, insert and marker to
4024 remeber where the list ends. The second part of the list (the one
4025 featuring initialized fields) so it can be later reversed to
4026 enforce 8.5. The marker will be removed during that operation. */
4027 marker = build_tree_list (NULL_TREE, NULL_TREE);
4028 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4029 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4030}
4031
e04a16fb
AG
4032/* Can't use lookup_field () since we don't want to load the class and
4033 can't set the CLASS_LOADED_P flag */
4034
4035static tree
4036find_field (class, name)
4037 tree class;
4038 tree name;
4039{
4040 tree decl;
4041 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4042 {
4043 if (DECL_NAME (decl) == name)
4044 return decl;
4045 }
4046 return NULL_TREE;
4047}
4048
4049/* Wrap around lookup_field that doesn't potentially upset the value
4050 of CLASS */
4051
4052static tree
4053lookup_field_wrapper (class, name)
4054 tree class, name;
4055{
4056 tree type = class;
9a7ab4b3 4057 tree decl = NULL_TREE;
c877974e 4058 java_parser_context_save_global ();
f2760b27
APB
4059
4060 /* Last chance: if we're within the context of an inner class, we
4061 might be trying to access a local variable defined in an outer
4062 context. We try to look for it now. */
9a7ab4b3 4063 if (INNER_CLASS_TYPE_P (class))
f2760b27 4064 {
9a7ab4b3 4065 tree new_name;
1f8f4a0b 4066 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4067 decl = lookup_field (&type, new_name);
f2760b27
APB
4068 if (decl && decl != error_mark_node)
4069 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4070 }
9a7ab4b3
APB
4071 if (!decl || decl == error_mark_node)
4072 {
4073 type = class;
4074 decl = lookup_field (&type, name);
4075 }
f2760b27 4076
c877974e 4077 java_parser_context_restore_global ();
93024893 4078 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4079}
4080
4081/* Find duplicate field within the same class declarations and report
c583dd46
APB
4082 the error. Returns 1 if a duplicated field was found, 0
4083 otherwise. */
e04a16fb
AG
4084
4085static int
c583dd46 4086duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4087 tree new_field_name, new_type, cl;
e04a16fb
AG
4088{
4089 /* This might be modified to work with method decl as well */
c2952b01 4090 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4091 if (decl)
4092 {
c2e3db92 4093 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4094 ((TREE_CODE (new_type) == POINTER_TYPE
4095 && TREE_TYPE (new_type) == NULL_TREE) ?
4096 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4097 lang_printable_name (new_type, 1)));
c877974e
APB
4098 /* The type may not have been completed by the time we report
4099 the error */
c2e3db92 4100 char *t2 = xstrdup (purify_type_name
4a5f66c3 4101 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4102 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4103 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4104 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4105 parse_error_context
4106 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4107 t1, IDENTIFIER_POINTER (new_field_name),
4108 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4109 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4110 free (t1);
4111 free (t2);
c583dd46 4112 return 1;
e04a16fb 4113 }
c583dd46 4114 return 0;
e04a16fb
AG
4115}
4116
4117/* Field registration routine. If TYPE doesn't exist, field
4118 declarations are linked to the undefined TYPE dependency list, to
4119 be later resolved in java_complete_class () */
4120
4121static void
4122register_fields (flags, type, variable_list)
4123 int flags;
4124 tree type, variable_list;
4125{
c583dd46 4126 tree current, saved_type;
c2952b01 4127 tree class_type = NULL_TREE;
e04a16fb
AG
4128 int saved_lineno = lineno;
4129 int must_chain = 0;
4130 tree wfl = NULL_TREE;
4131
c2952b01
APB
4132 if (GET_CPC ())
4133 class_type = TREE_TYPE (GET_CPC ());
4134
4135 if (!class_type || class_type == error_mark_node)
4136 return;
4137
e04a16fb
AG
4138 /* If we're adding fields to interfaces, those fields are public,
4139 static, final */
4140 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4141 {
4142 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4143 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4144 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4145 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4146 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4147 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4148 check_modifiers ("Illegal interface member modifier `%s'", flags,
4149 INTERFACE_FIELD_MODIFIERS);
4150 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4151 }
4152
c583dd46
APB
4153 /* Obtain a suitable type for resolution, if necessary */
4154 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4155
4156 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4157 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4158
c583dd46
APB
4159 for (current = variable_list, saved_type = type; current;
4160 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4161 {
c877974e 4162 tree real_type;
c583dd46 4163 tree field_decl;
e04a16fb
AG
4164 tree cl = TREE_PURPOSE (current);
4165 tree init = TREE_VALUE (current);
4166 tree current_name = EXPR_WFL_NODE (cl);
4167
cf1748bf 4168 /* Can't declare non-final static fields in inner classes */
c2952b01 4169 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4170 && !(flags & ACC_FINAL))
c2952b01 4171 parse_error_context
cf1748bf 4172 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4173 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4174 lang_printable_name (class_type, 0));
4175
c583dd46
APB
4176 /* Process NAME, as it may specify extra dimension(s) for it */
4177 type = build_array_from_name (type, wfl, current_name, &current_name);
4178
c583dd46
APB
4179 /* Type adjustment. We may have just readjusted TYPE because
4180 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4181 a reference if we can and don't have one already. Also
4182 change the name if we have an init. */
4183 if (type != saved_type)
4184 {
1886c9d8 4185 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4186 if (init)
4187 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4188 }
e04a16fb 4189
c877974e
APB
4190 real_type = GET_REAL_TYPE (type);
4191 /* Check for redeclarations */
4192 if (duplicate_declaration_error_p (current_name, real_type, cl))
4193 continue;
4194
c583dd46 4195 /* Set lineno to the line the field was found and create a
5e942c50 4196 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4197 if (flag_emit_xref)
4198 lineno = EXPR_WFL_LINECOL (cl);
4199 else
4200 lineno = EXPR_WFL_LINENO (cl);
c877974e 4201 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4202 CHECK_DEPRECATED (field_decl);
c2952b01 4203
c7303e41
APB
4204 /* If the field denotes a final instance variable, then we
4205 allocate a LANG_DECL_SPECIFIC part to keep track of its
4206 initialization. We also mark whether the field was
4207 initialized upon it's declaration. We don't do that if the
4208 created field is an alias to a final local. */
4209 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4210 {
4211 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4212 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4213 if ((flags & ACC_STATIC) && init)
4214 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4215 }
4216
4217 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4218 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4219 hide parameters to this inner class finit$ and
4220 constructors. It also means that the field isn't final per
4221 say. */
c2952b01 4222 if (ARG_FINAL_P (current))
c7303e41
APB
4223 {
4224 FIELD_LOCAL_ALIAS (field_decl) = 1;
4225 FIELD_FINAL (field_decl) = 0;
4226 }
c583dd46
APB
4227
4228 /* Check if we must chain. */
4229 if (must_chain)
4230 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4231
c583dd46
APB
4232 /* If we have an initialization value tied to the field */
4233 if (init)
4234 {
4235 /* The field is declared static */
e04a16fb 4236 if (flags & ACC_STATIC)
e04a16fb 4237 {
7525cc04
APB
4238 /* We include the field and its initialization part into
4239 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4240 walked, field initializations will be processed and
4241 fields initialized with known constants will be taken
4242 out of <clinit> and have their DECL_INITIAL set
7525cc04 4243 appropriately. */
c2952b01
APB
4244 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4245 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4246 if (TREE_OPERAND (init, 1)
4247 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4248 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4249 }
5e942c50
APB
4250 /* A non-static field declared with an immediate initialization is
4251 to be initialized in <init>, if any. This field is remembered
4252 to be processed at the time of the generation of <init>. */
c583dd46
APB
4253 else
4254 {
c2952b01
APB
4255 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4256 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4257 }
5b09b33e 4258 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4259 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4260 }
4261 }
4262 lineno = saved_lineno;
4263}
4264
c00f0fb2
APB
4265/* Generate finit$, using the list of initialized fields to populate
4266 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4267 one(s) used to initialized the field(s) caching outer context
4268 local(s). */
22eed1e6 4269
c2952b01
APB
4270static tree
4271generate_finit (class_type)
4272 tree class_type;
22eed1e6 4273{
c2952b01
APB
4274 int count = 0;
4275 tree list = TYPE_FINIT_STMT_LIST (class_type);
4276 tree mdecl, current, parms;
4277
4278 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4279 class_type, NULL_TREE,
4280 &count);
4281 CRAFTED_PARAM_LIST_FIXUP (parms);
4282 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4283 finit_identifier_node, parms);
4284 fix_method_argument_names (parms, mdecl);
4285 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4286 mdecl, NULL_TREE);
4287 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4288 start_artificial_method_body (mdecl);
4289
c2952b01 4290 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4291 java_method_add_stmt (mdecl,
4292 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4293 current));
22eed1e6 4294 end_artificial_method_body (mdecl);
c2952b01 4295 return mdecl;
22eed1e6
APB
4296}
4297
e04a16fb 4298static void
c2952b01
APB
4299add_instance_initializer (mdecl)
4300 tree mdecl;
e04a16fb 4301{
c2952b01
APB
4302 tree current;
4303 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4304 tree compound = NULL_TREE;
e04a16fb 4305
c2952b01 4306 if (stmt_list)
e04a16fb 4307 {
c2952b01
APB
4308 for (current = stmt_list; current; current = TREE_CHAIN (current))
4309 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4310
c2952b01
APB
4311 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4312 NULL_TREE, compound));
4313 }
e04a16fb
AG
4314}
4315
4316/* Shared accros method_declarator and method_header to remember the
4317 patch stage that was reached during the declaration of the method.
4318 A method DECL is built differently is there is no patch
4319 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4320 pending on the currently defined method. */
4321
4322static int patch_stage;
4323
4324/* Check the method declaration and add the method to its current
4325 class. If the argument list is known to contain incomplete types,
4326 the method is partially added and the registration will be resume
22eed1e6
APB
4327 once the method arguments resolved. If TYPE is NULL, we're dealing
4328 with a constructor. */
e04a16fb
AG
4329
4330static tree
4331method_header (flags, type, mdecl, throws)
4332 int flags;
4333 tree type, mdecl, throws;
4334{
1886c9d8 4335 tree type_wfl = NULL_TREE;
79d13333 4336 tree meth_name = NULL_TREE;
c2952b01 4337 tree current, orig_arg, this_class = NULL;
34d4df06 4338 tree id, meth;
e04a16fb 4339 int saved_lineno;
1886c9d8 4340 int constructor_ok = 0, must_chain;
c2952b01 4341 int count;
34d4df06
APB
4342
4343 if (mdecl == error_mark_node)
4344 return error_mark_node;
4345 meth = TREE_VALUE (mdecl);
4346 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4347
4348 check_modifiers_consistency (flags);
79d13333 4349
c2952b01
APB
4350 if (GET_CPC ())
4351 this_class = TREE_TYPE (GET_CPC ());
4352
4353 if (!this_class || this_class == error_mark_node)
79d13333 4354 return NULL_TREE;
e04a16fb
AG
4355
4356 /* There are some forbidden modifiers for an abstract method and its
4357 class must be abstract as well. */
22eed1e6 4358 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4359 {
4360 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4361 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4362 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4363 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4364 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4365 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4366 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4367 parse_error_context
781b0558 4368 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4369 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4370 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4371 }
c2952b01 4372
22eed1e6
APB
4373 /* Things to be checked when declaring a constructor */
4374 if (!type)
4375 {
4376 int ec = java_error_count;
4377 /* 8.6: Constructor declarations: we might be trying to define a
4378 method without specifying a return type. */
c2952b01 4379 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4380 parse_error_context
4381 (id, "Invalid method declaration, return type required");
4382 /* 8.6.3: Constructor modifiers */
4383 else
4384 {
4385 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4386 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4387 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4388 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4389 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4390 }
4391 /* If we found error here, we don't consider it's OK to tread
4392 the method definition as a constructor, for the rest of this
4393 function */
4394 if (ec == java_error_count)
4395 constructor_ok = 1;
4396 }
e04a16fb
AG
4397
4398 /* Method declared within the scope of an interface are implicitly
4399 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4400 modifiers are checked right after. */
e04a16fb
AG
4401
4402 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4403 {
4404 /* If FLAGS isn't set because of a modifier, turn the
4405 corresponding modifier WFL to NULL so we issue a warning on
4406 the obsolete use of the modifier */
4407 if (!(flags & ACC_PUBLIC))
4408 MODIFIER_WFL (PUBLIC_TK) = NULL;
4409 if (!(flags & ACC_ABSTRACT))
4410 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4411 flags |= ACC_PUBLIC;
4412 flags |= ACC_ABSTRACT;
4413 }
4414
c2952b01
APB
4415 /* Inner class can't declare static methods */
4416 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4417 {
4418 parse_error_context
4419 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4420 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4421 lang_printable_name (this_class, 0));
4422 }
4423
e04a16fb
AG
4424 /* Modifiers context reset moved up, so abstract method declaration
4425 modifiers can be later checked. */
4426
22eed1e6
APB
4427 /* Set constructor returned type to void and method name to <init>,
4428 unless we found an error identifier the constructor (in which
4429 case we retain the original name) */
4430 if (!type)
4431 {
4432 type = void_type_node;
4433 if (constructor_ok)
4434 meth_name = init_identifier_node;
4435 }
4436 else
4437 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4438
1886c9d8
APB
4439 /* Do the returned type resolution and registration if necessary */
4440 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4441
4a5f66c3
APB
4442 if (meth_name)
4443 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4444 EXPR_WFL_NODE (id) = meth_name;
4445 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4446
4447 if (must_chain)
e04a16fb 4448 {
1886c9d8
APB
4449 patch_stage = JDEP_METHOD_RETURN;
4450 register_incomplete_type (patch_stage, type_wfl, id, type);
4451 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4452 }
4453 else
1886c9d8 4454 TREE_TYPE (meth) = type;
e04a16fb
AG
4455
4456 saved_lineno = lineno;
4457 /* When defining an abstract or interface method, the curly
4458 bracket at level 1 doesn't exist because there is no function
4459 body */
4460 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4461 EXPR_WFL_LINENO (id));
4462
5e942c50
APB
4463 /* Remember the original argument list */
4464 orig_arg = TYPE_ARG_TYPES (meth);
4465
e04a16fb
AG
4466 if (patch_stage) /* includes ret type and/or all args */
4467 {
4468 jdep *jdep;
4469 meth = add_method_1 (this_class, flags, meth_name, meth);
4470 /* Patch for the return type */
4471 if (patch_stage == JDEP_METHOD_RETURN)
4472 {
4473 jdep = CLASSD_LAST (ctxp->classd_list);
4474 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4475 }
4476 /* This is the stop JDEP. METH allows the function's signature
4477 to be computed. */
4478 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4479 }
4480 else
5e942c50
APB
4481 meth = add_method (this_class, flags, meth_name,
4482 build_java_signature (meth));
4483
c2952b01
APB
4484 /* Remember final parameters */
4485 MARK_FINAL_PARMS (meth, orig_arg);
4486
5e942c50
APB
4487 /* Fix the method argument list so we have the argument name
4488 information */
4489 fix_method_argument_names (orig_arg, meth);
4490
4491 /* Register the parameter number and re-install the current line
4492 number */
e04a16fb
AG
4493 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4494 lineno = saved_lineno;
b9f7e36c
APB
4495
4496 /* Register exception specified by the `throws' keyword for
4497 resolution and set the method decl appropriate field to the list.
4498 Note: the grammar ensures that what we get here are class
4499 types. */
4500 if (throws)
4501 {
4502 throws = nreverse (throws);
4503 for (current = throws; current; current = TREE_CHAIN (current))
4504 {
4505 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4506 NULL_TREE, NULL_TREE);
4507 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4508 &TREE_VALUE (current);
4509 }
4510 DECL_FUNCTION_THROWS (meth) = throws;
4511 }
4512
c4faeb92
APB
4513 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4514 DECL_FUNCTION_WFL (meth) = id;
4515
22eed1e6
APB
4516 /* Set the flag if we correctly processed a constructor */
4517 if (constructor_ok)
c2952b01
APB
4518 {
4519 DECL_CONSTRUCTOR_P (meth) = 1;
4520 /* Compute and store the number of artificial parameters declared
4521 for this constructor */
4522 for (count = 0, current = TYPE_FIELDS (this_class); current;
4523 current = TREE_CHAIN (current))
4524 if (FIELD_LOCAL_ALIAS (current))
4525 count++;
4526 DECL_FUNCTION_NAP (meth) = count;
4527 }
22eed1e6 4528
5e942c50
APB
4529 /* Eventually set the @deprecated tag flag */
4530 CHECK_DEPRECATED (meth);
4531
7f10c2e2
APB
4532 /* If doing xref, store column and line number information instead
4533 of the line number only. */
4534 if (flag_emit_xref)
4535 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4536
e04a16fb
AG
4537 return meth;
4538}
4539
5e942c50
APB
4540static void
4541fix_method_argument_names (orig_arg, meth)
4542 tree orig_arg, meth;
4543{
4544 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4545 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4546 {
4547 TREE_PURPOSE (arg) = this_identifier_node;
4548 arg = TREE_CHAIN (arg);
4549 }
de4c7b02 4550 while (orig_arg != end_params_node)
5e942c50
APB
4551 {
4552 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4553 orig_arg = TREE_CHAIN (orig_arg);
4554 arg = TREE_CHAIN (arg);
4555 }
4556}
4557
22eed1e6
APB
4558/* Complete the method declaration with METHOD_BODY. */
4559
4560static void
b635eb2f 4561finish_method_declaration (method_body)
22eed1e6
APB
4562 tree method_body;
4563{
79d13333
APB
4564 int flags;
4565
4566 if (!current_function_decl)
4567 return;
4568
4569 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4570
4571 /* 8.4.5 Method Body */
4572 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4573 {
c0b00d37
APB
4574 tree name = DECL_NAME (current_function_decl);
4575 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
5256aa37
APB
4576 "%s method `%s' can't have a body defined",
4577 (METHOD_NATIVE (current_function_decl) ?
4578 "Native" : "Abstract"),
c0b00d37 4579 IDENTIFIER_POINTER (name));
5256aa37
APB
4580 method_body = NULL_TREE;
4581 }
4582 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4583 {
c0b00d37 4584 tree name = DECL_NAME (current_function_decl);
781b0558 4585 parse_error_context
c0b00d37 4586 (DECL_FUNCTION_WFL (current_function_decl),
781b0558 4587 "Non native and non abstract method `%s' must have a body defined",
c0b00d37 4588 IDENTIFIER_POINTER (name));
5256aa37
APB
4589 method_body = NULL_TREE;
4590 }
4591
2c56429a
APB
4592 if (flag_emit_class_files && method_body
4593 && TREE_CODE (method_body) == NOP_EXPR
4594 && TREE_TYPE (current_function_decl)
4595 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4596 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4597
22eed1e6
APB
4598 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4599 maybe_absorb_scoping_blocks ();
4600 /* Exit function's body */
4601 exit_block ();
4602 /* Merge last line of the function with first line, directly in the
4603 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4604 if (!flag_emit_xref)
4605 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4606
4607 /* Since function's argument's list are shared, reset the
4608 ARG_FINAL_P parameter that might have been set on some of this
4609 function parameters. */
4610 UNMARK_FINAL_PARMS (current_function_decl);
4611
f099f336
APB
4612 /* So we don't have an irrelevant function declaration context for
4613 the next static block we'll see. */
4614 current_function_decl = NULL_TREE;
22eed1e6
APB
4615}
4616
4617/* Build a an error message for constructor circularity errors. */
4618
4619static char *
4620constructor_circularity_msg (from, to)
4621 tree from, to;
4622{
4623 static char string [4096];
c2e3db92 4624 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4625 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4626 free (t);
4627 return string;
4628}
4629
4630/* Verify a circular call to METH. Return 1 if an error is found, 0
4631 otherwise. */
4632
4633static int
4634verify_constructor_circularity (meth, current)
4635 tree meth, current;
4636{
4637 static tree list = NULL_TREE;
19e223db 4638 static int initialized_p;
22eed1e6 4639 tree c;
19e223db
MM
4640
4641 /* If we haven't already registered LIST with the garbage collector,
4642 do so now. */
4643 if (!initialized_p)
4644 {
4645 ggc_add_tree_root (&list, 1);
4646 initialized_p = 1;
4647 }
4648
22eed1e6
APB
4649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4650 {
4651 if (TREE_VALUE (c) == meth)
4652 {
4653 char *t;
4654 if (list)
4655 {
4656 tree liste;
4657 list = nreverse (list);
4658 for (liste = list; liste; liste = TREE_CHAIN (liste))
4659 {
4660 parse_error_context
c63b98cd 4661 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4662 constructor_circularity_msg
4663 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4664 java_error_count--;
4665 }
4666 }
c2e3db92 4667 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4668 parse_error_context (TREE_PURPOSE (c),
4669 "%s: recursive invocation of constructor `%s'",
4670 constructor_circularity_msg (current, meth), t);
4671 free (t);
4672 list = NULL_TREE;
4673 return 1;
4674 }
4675 }
4676 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4677 {
4678 list = tree_cons (c, current, list);
4679 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4680 return 1;
4681 list = TREE_CHAIN (list);
4682 }
4683 return 0;
4684}
4685
e04a16fb
AG
4686/* Check modifiers that can be declared but exclusively */
4687
4688static void
4689check_modifiers_consistency (flags)
4690 int flags;
4691{
4692 int acc_count = 0;
4693 tree cl = NULL_TREE;
4694
e0fc4118
TT
4695 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4696 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4698 if (acc_count > 1)
4699 parse_error_context
e0fc4118
TT
4700 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4701
4702 acc_count = 0;
4703 cl = NULL_TREE;
14d075d8
TT
4704 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4705 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4706 if (acc_count > 1)
4707 parse_error_context (cl,
4708 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4709}
4710
4711/* Check the methode header METH for abstract specifics features */
4712
4713static void
4714check_abstract_method_header (meth)
4715 tree meth;
4716{
4717 int flags = get_access_flags_from_decl (meth);
e04a16fb 4718
2884c41e
KG
4719 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4720 ACC_ABSTRACT, "abstract method",
c4faeb92 4721 IDENTIFIER_POINTER (DECL_NAME (meth)));
2884c41e
KG
4722 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4723 ACC_PUBLIC, "abstract method",
c4faeb92 4724 IDENTIFIER_POINTER (DECL_NAME (meth)));
e04a16fb
AG
4725
4726 check_modifiers ("Illegal modifier `%s' for interface method",
4727 flags, INTERFACE_METHOD_MODIFIERS);
4728}
4729
4730/* Create a FUNCTION_TYPE node and start augmenting it with the
4731 declared function arguments. Arguments type that can't be resolved
4732 are left as they are, but the returned node is marked as containing
4733 incomplete types. */
4734
4735static tree
4736method_declarator (id, list)
4737 tree id, list;
4738{
4739 tree arg_types = NULL_TREE, current, node;
4740 tree meth = make_node (FUNCTION_TYPE);
4741 jdep *jdep;
e04a16fb
AG
4742
4743 patch_stage = JDEP_NO_PATCH;
c2952b01 4744
34d4df06
APB
4745 if (GET_CPC () == error_mark_node)
4746 return error_mark_node;
4747
c2952b01
APB
4748 /* If we're dealing with an inner class constructor, we hide the
4749 this$<n> decl in the name field of its parameter declaration. We
4750 also might have to hide the outer context local alias
4751 initializers. Not done when the class is a toplevel class. */
4752 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4753 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4754 {
4755 tree aliases_list, type, thisn;
4756 /* First the aliases, linked to the regular parameters */
4757 aliases_list =
4758 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4759 TREE_TYPE (GET_CPC ()),
4760 NULL_TREE, NULL);
4761 list = chainon (nreverse (aliases_list), list);
4762
4763 /* Then this$<n> */
4764 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4765 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4766 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4767 list);
4768 }
e04a16fb
AG
4769
4770 for (current = list; current; current = TREE_CHAIN (current))
4771 {
c583dd46 4772 int must_chain = 0;
e04a16fb
AG
4773 tree wfl_name = TREE_PURPOSE (current);
4774 tree type = TREE_VALUE (current);
4775 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4776 tree already, arg_node;
4777 tree type_wfl = NULL_TREE;
23a79c61 4778 tree real_type;
c583dd46
APB
4779
4780 /* Obtain a suitable type for resolution, if necessary */
4781 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4782
4783 /* Process NAME, as it may specify extra dimension(s) for it */
4784 type = build_array_from_name (type, type_wfl, name, &name);
4785 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4786
23a79c61
APB
4787 real_type = GET_REAL_TYPE (type);
4788 if (TREE_CODE (real_type) == RECORD_TYPE)
4789 {
4790 real_type = promote_type (real_type);
4791 if (TREE_CODE (type) == TREE_LIST)
4792 TREE_PURPOSE (type) = real_type;
4793 }
5e942c50 4794
e04a16fb
AG
4795 /* Check redefinition */
4796 for (already = arg_types; already; already = TREE_CHAIN (already))
4797 if (TREE_PURPOSE (already) == name)
4798 {
781b0558
KG
4799 parse_error_context
4800 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4801 IDENTIFIER_POINTER (name),
e04a16fb
AG
4802 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4803 break;
4804 }
4805
4806 /* If we've an incomplete argument type, we know there is a location
4807 to patch when the type get resolved, later. */
4808 jdep = NULL;
c583dd46 4809 if (must_chain)
e04a16fb 4810 {
c583dd46
APB
4811 patch_stage = JDEP_METHOD;
4812 type = register_incomplete_type (patch_stage,
4813 type_wfl, wfl_name, type);
4814 jdep = CLASSD_LAST (ctxp->classd_list);
4815 JDEP_MISC (jdep) = id;
e04a16fb 4816 }
c583dd46 4817
c2952b01 4818 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4819 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4820 /* Remeber arguments declared final. */
4821 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4822
e04a16fb
AG
4823 if (jdep)
4824 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4825 TREE_CHAIN (arg_node) = arg_types;
4826 arg_types = arg_node;
4827 }
de4c7b02 4828 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4829 node = build_tree_list (id, meth);
4830 return node;
4831}
4832
4833static int
4834unresolved_type_p (wfl, returned)
4835 tree wfl;
4836 tree *returned;
4837
4838{
4839 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4840 {
e04a16fb 4841 if (returned)
165f37bc
APB
4842 {
4843 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4844 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4845 *returned = TREE_TYPE (decl);
4846 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4847 *returned = TREE_TYPE (GET_CPC ());
4848 else
4849 *returned = NULL_TREE;
4850 }
e04a16fb
AG
4851 return 1;
4852 }
4853 if (returned)
4854 *returned = wfl;
4855 return 0;
4856}
4857
4858/* From NAME, build a qualified identifier node using the
4859 qualification from the current package definition. */
4860
4861static tree
98a52c2c 4862parser_qualified_classname (name)
e04a16fb
AG
4863 tree name;
4864{
c2952b01
APB
4865 tree nested_class_name;
4866
98a52c2c 4867 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4868 return nested_class_name;
4869
e04a16fb 4870 if (ctxp->package)
c2952b01 4871 return merge_qualified_name (ctxp->package, name);
e04a16fb 4872 else
c2952b01 4873 return name;
e04a16fb
AG
4874}
4875
4876/* Called once the type a interface extends is resolved. Returns 0 if
4877 everything is OK. */
4878
4879static int
4880parser_check_super_interface (super_decl, this_decl, this_wfl)
4881 tree super_decl, this_decl, this_wfl;
4882{
4883 tree super_type = TREE_TYPE (super_decl);
4884
4885 /* Has to be an interface */
c2952b01 4886 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4887 {
4888 parse_error_context
4889 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4890 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4891 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4892 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4893 "interface" : "class"),
4894 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4895 return 1;
4896 }
4897
4898 /* Check scope: same package OK, other package: OK if public */
4899 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4900 return 1;
4901
4902 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4903 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4904 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4905 return 0;
4906}
4907
4908/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4909 0 if everthing is OK. */
4910
4911static int
4912parser_check_super (super_decl, this_decl, wfl)
4913 tree super_decl, this_decl, wfl;
4914{
e04a16fb
AG
4915 tree super_type = TREE_TYPE (super_decl);
4916
4917 /* SUPER should be a CLASS (neither an array nor an interface) */
4918 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4919 {
4920 parse_error_context
4921 (wfl, "Class `%s' can't subclass %s `%s'",
4922 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4923 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4924 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4925 return 1;
4926 }
4927
4928 if (CLASS_FINAL (TYPE_NAME (super_type)))
4929 {
4930 parse_error_context (wfl, "Can't subclass final classes: %s",
4931 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4932 return 1;
4933 }
4934
4935 /* Check scope: same package OK, other package: OK if public */
4936 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4937 return 1;
4938
4939 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4940 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4941 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4942 return 0;
4943}
4944
4945/* Create a new dependency list and link it (in a LIFO manner) to the
4946 CTXP list of type dependency list. */
4947
4948static void
4949create_jdep_list (ctxp)
4950 struct parser_ctxt *ctxp;
4951{
23a79c61 4952 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4953 new->first = new->last = NULL;
4954 new->next = ctxp->classd_list;
4955 ctxp->classd_list = new;
4956}
4957
4958static jdeplist *
4959reverse_jdep_list (ctxp)
4960 struct parser_ctxt *ctxp;
4961{
4962 register jdeplist *prev = NULL, *current, *next;
4963 for (current = ctxp->classd_list; current; current = next)
4964 {
4965 next = current->next;
4966 current->next = prev;
4967 prev = current;
4968 }
4969 return prev;
4970}
4971
23a79c61
APB
4972/* Create a fake pointer based on the ID stored in
4973 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4974 registered again. */
e04a16fb
AG
4975
4976static tree
23a79c61
APB
4977obtain_incomplete_type (type_name)
4978 tree type_name;
e04a16fb 4979{
23a79c61
APB
4980 tree ptr, name;
4981
4982 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4983 name = EXPR_WFL_NODE (type_name);
4984 else if (INCOMPLETE_TYPE_P (type_name))
4985 name = TYPE_NAME (type_name);
4986 else
400500c4 4987 abort ();
e04a16fb
AG
4988
4989 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4990 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4991 break;
4992
4993 if (!ptr)
4994 {
78d21f92
PB
4995 BUILD_PTR_FROM_NAME (ptr, name);
4996 layout_type (ptr);
e04a16fb
AG
4997 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4998 ctxp->incomplete_class = ptr;
4999 }
5000
5001 return ptr;
5002}
5003
5004/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5005 non NULL instead of computing a new fake type based on WFL. The new
5006 dependency is inserted in the current type dependency list, in FIFO
5007 manner. */
5008
5009static tree
5010register_incomplete_type (kind, wfl, decl, ptr)
5011 int kind;
5012 tree wfl, decl, ptr;
5013{
23a79c61 5014 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5015
e04a16fb
AG
5016 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5017 ptr = obtain_incomplete_type (wfl);
5018
5019 JDEP_KIND (new) = kind;
5020 JDEP_DECL (new) = decl;
5021 JDEP_SOLV (new) = ptr;
5022 JDEP_WFL (new) = wfl;
5023 JDEP_CHAIN (new) = NULL;
5024 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5025 /* For some dependencies, set the enclosing class of the current
5026 class to be the enclosing context */
8ac1de05
APB
5027 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5028 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5029 && GET_ENCLOSING_CPC ())
5030 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5031 else
324ed8fd 5032 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5033 JDEP_GET_PATCH (new) = (tree *)NULL;
5034
5035 JDEP_INSERT (ctxp->classd_list, new);
5036
5037 return ptr;
5038}
5039
5040void
5041java_check_circular_reference ()
5042{
5043 tree current;
5044 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5045 {
5046 tree type = TREE_TYPE (current);
e920ebc9 5047 if (CLASS_INTERFACE (current))
e04a16fb
AG
5048 {
5049 /* Check all interfaces this class extends */
5050 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5051 int n, i;
5052
5053 if (!basetype_vec)
5054 return;
5055 n = TREE_VEC_LENGTH (basetype_vec);
5056 for (i = 0; i < n; i++)
5057 {
5058 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5059 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5060 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5061 parse_error_context (lookup_cl (current),
5062 "Cyclic interface inheritance");
5063 }
5064 }
5065 else
5066 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5067 parse_error_context (lookup_cl (current),
c2952b01
APB
5068 "Cyclic class inheritance%s",
5069 (cyclic_inheritance_report ?
5070 cyclic_inheritance_report : ""));
5071 }
5072}
5073
5074/* Augment the parameter list PARM with parameters crafted to
5075 initialize outer context locals aliases. Through ARTIFICIAL, a
5076 count is kept of the number of crafted parameters. MODE governs
5077 what eventually gets created: something suitable for a function
5078 creation or a function invocation, either the constructor or
c00f0fb2 5079 finit$. */
c2952b01
APB
5080
5081static tree
5082build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5083 int mode;
5084 tree class_type, parm;
5085 int *artificial;
5086{
5087 tree field;
da632f2c
APB
5088 tree additional_parms = NULL_TREE;
5089
c2952b01
APB
5090 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5091 if (FIELD_LOCAL_ALIAS (field))
5092 {
63ad61ed 5093 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5094 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5095 tree mangled_id;
c2952b01
APB
5096
5097 switch (mode)
5098 {
5099 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5101 &buffer [4]);
5102 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5103 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5104 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5105 else
5106 value = TREE_TYPE (field);
5107 break;
5108
5109 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5110 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5111 &buffer [4]);
c2952b01
APB
5112 value = TREE_TYPE (field);
5113 break;
5114
5115 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5116 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5117 &buffer [4]);
c2952b01
APB
5118 /* Now, this is wrong. purpose should always be the NAME
5119 of something and value its matching value (decl, type,
5120 etc...) FIXME -- but there is a lot to fix. */
5121
5122 /* When invoked for this kind of operation, we already
5123 know whether a field is used or not. */
5124 purpose = TREE_TYPE (field);
1f8f4a0b 5125 value = build_wfl_node (mangled_id);
c2952b01
APB
5126 break;
5127
5128 case AIPL_FUNCTION_CTOR_INVOCATION:
5129 /* There are two case: the constructor invokation happends
5130 outside the local inner, in which case, locales from the outer
5131 context are directly used.
5132
5133 Otherwise, we fold to using the alias directly. */
5134 if (class_type == current_class)
5135 value = field;
5136 else
5137 {
5138 name = get_identifier (&buffer[4]);
5139 value = IDENTIFIER_LOCAL_VALUE (name);
5140 }
5141 break;
5142 }
da632f2c 5143 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5144 if (artificial)
5145 *artificial +=1;
5146 }
da632f2c
APB
5147 if (additional_parms)
5148 {
5149 if (ANONYMOUS_CLASS_P (class_type)
5150 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5151 additional_parms = nreverse (additional_parms);
5152 parm = chainon (additional_parms, parm);
5153 }
5154
5155 return parm;
c2952b01
APB
5156}
5157
5158/* Craft a constructor for CLASS_DECL -- what we should do when none
5159 where found. ARGS is non NULL when a special signature must be
5160 enforced. This is the case for anonymous classes. */
5161
5162static void
5163craft_constructor (class_decl, args)
5164 tree class_decl, args;
5165{
5166 tree class_type = TREE_TYPE (class_decl);
5167 tree parm = NULL_TREE;
5168 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5169 ACC_PUBLIC : 0);
5170 int i = 0, artificial = 0;
5171 tree decl, ctor_name;
5172 char buffer [80];
5173
c2952b01
APB
5174 /* The constructor name is <init> unless we're dealing with an
5175 anonymous class, in which case the name will be fixed after having
5176 be expanded. */
5177 if (ANONYMOUS_CLASS_P (class_type))
5178 ctor_name = DECL_NAME (class_decl);
5179 else
5180 ctor_name = init_identifier_node;
5181
5182 /* If we're dealing with an inner class constructor, we hide the
5183 this$<n> decl in the name field of its parameter declaration. */
5184 if (PURE_INNER_CLASS_TYPE_P (class_type))
5185 {
5186 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5187 parm = tree_cons (build_current_thisn (class_type),
5188 build_pointer_type (type), parm);
5189
5190 /* Some more arguments to be hidden here. The values of the local
5191 variables of the outer context that the inner class needs to see. */
5192 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5193 class_type, parm,
5194 &artificial);
5195 }
5196
5197 /* Then if there are any args to be enforced, enforce them now */
5198 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5199 {
5200 sprintf (buffer, "parm%d", i++);
5201 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5202 }
c2952b01
APB
5203
5204 CRAFTED_PARAM_LIST_FIXUP (parm);
5205 decl = create_artificial_method (class_type, flags, void_type_node,
5206 ctor_name, parm);
5207 fix_method_argument_names (parm, decl);
5208 /* Now, mark the artificial parameters. */
5209 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5210 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5211}
5212
c2952b01 5213
e920ebc9
APB
5214/* Fix the constructors. This will be called right after circular
5215 references have been checked. It is necessary to fix constructors
5216 early even if no code generation will take place for that class:
5217 some generated constructor might be required by the class whose
5218 compilation triggered this one to be simply loaded. */
5219
5220void
5221java_fix_constructors ()
5222{
5223 tree current;
5224
5225 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5226 {
e920ebc9
APB
5227 tree class_type = TREE_TYPE (current);
5228 int saw_ctor = 0;
c2952b01
APB
5229 tree decl;
5230
5231 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5232 continue;
e920ebc9
APB
5233
5234 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5235 {
5236 if (DECL_CONSTRUCTOR_P (decl))
5237 {
5238 fix_constructors (decl);
5239 saw_ctor = 1;
5240 }
5241 }
5242
c2952b01
APB
5243 /* Anonymous class constructor can't be generated that early. */
5244 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5245 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5246 }
5247}
5248
23a79c61
APB
5249/* safe_layout_class just makes sure that we can load a class without
5250 disrupting the current_class, input_file, lineno, etc, information
5251 about the class processed currently. */
5252
e04a16fb
AG
5253void
5254safe_layout_class (class)
5255 tree class;
5256{
5257 tree save_current_class = current_class;
3b304f5b 5258 const char *save_input_filename = input_filename;
e04a16fb 5259 int save_lineno = lineno;
5e942c50 5260
e04a16fb 5261 layout_class (class);
5e942c50 5262
e04a16fb
AG
5263 current_class = save_current_class;
5264 input_filename = save_input_filename;
5265 lineno = save_lineno;
5266 CLASS_LOADED_P (class) = 1;
5267}
5268
5269static tree
5270jdep_resolve_class (dep)
5271 jdep *dep;
5272{
5273 tree decl;
5274
23a79c61
APB
5275 if (JDEP_RESOLVED_P (dep))
5276 decl = JDEP_RESOLVED_DECL (dep);
5277 else
e04a16fb 5278 {
c2952b01 5279 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5280 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5281 JDEP_RESOLVED (dep, decl);
5282 }
23a79c61 5283
e04a16fb 5284 if (!decl)
23a79c61 5285 complete_class_report_errors (dep);
1e12ab9b 5286 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5287 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5288 return decl;
5289}
5290
5291/* Complete unsatisfied class declaration and their dependencies */
5292
5293void
5294java_complete_class ()
5295{
e04a16fb
AG
5296 tree cclass;
5297 jdeplist *cclassd;
5298 int error_found;
b67d701b 5299 tree type;
e04a16fb 5300
9a7ab4b3 5301 /* Process imports */
e04a16fb 5302 process_imports ();
e04a16fb
AG
5303
5304 /* Rever things so we have the right order */
5305 ctxp->class_list = nreverse (ctxp->class_list);
5306 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5307
e04a16fb
AG
5308 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5309 cclass && cclassd;
5310 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5311 {
5312 jdep *dep;
5313 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5314 {
5315 tree decl;
e04a16fb
AG
5316 if (!(decl = jdep_resolve_class (dep)))
5317 continue;
5318
5319 /* Now it's time to patch */
5320 switch (JDEP_KIND (dep))
5321 {
5322 case JDEP_SUPER:
5323 /* Simply patch super */
5324 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5325 continue;
5326 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5327 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5328 break;
5329
5330 case JDEP_FIELD:
5331 {
5332 /* We do part of the job done in add_field */
5333 tree field_decl = JDEP_DECL (dep);
5334 tree field_type = TREE_TYPE (decl);
e04a16fb 5335 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5336 field_type = promote_type (field_type);
e04a16fb 5337 TREE_TYPE (field_decl) = field_type;
5e942c50 5338 DECL_ALIGN (field_decl) = 0;
11cf4d18 5339 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5340 layout_decl (field_decl, 0);
e04a16fb
AG
5341 SOURCE_FRONTEND_DEBUG
5342 (("Completed field/var decl `%s' with `%s'",
5343 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5344 IDENTIFIER_POINTER (DECL_NAME (decl))));
5345 break;
5346 }
5347 case JDEP_METHOD: /* We start patching a method */
5348 case JDEP_METHOD_RETURN:
5349 error_found = 0;
5350 while (1)
5351 {
5352 if (decl)
5353 {
b67d701b
PB
5354 type = TREE_TYPE(decl);
5355 if (TREE_CODE (type) == RECORD_TYPE)
5356 type = promote_type (type);
e04a16fb
AG
5357 JDEP_APPLY_PATCH (dep, type);
5358 SOURCE_FRONTEND_DEBUG
5359 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5360 "Completing fct `%s' with ret type `%s'":
5361 "Completing arg `%s' with type `%s'"),
5362 IDENTIFIER_POINTER (EXPR_WFL_NODE
5363 (JDEP_DECL_WFL (dep))),
5364 IDENTIFIER_POINTER (DECL_NAME (decl))));
5365 }
5366 else
5367 error_found = 1;
5368 dep = JDEP_CHAIN (dep);
5369 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5370 break;
5371 else
5372 decl = jdep_resolve_class (dep);
5373 }
5374 if (!error_found)
5375 {
5376 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5377 /* Recompute and reset the signature, check first that
5378 all types are now defined. If they're not,
5379 dont build the signature. */
5380 if (check_method_types_complete (mdecl))
5381 {
5382 signature = build_java_signature (TREE_TYPE (mdecl));
5383 set_java_signature (TREE_TYPE (mdecl), signature);
5384 }
e04a16fb
AG
5385 }
5386 else
5387 continue;
5388 break;
5389
5390 case JDEP_INTERFACE:
5391 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5392 JDEP_WFL (dep)))
5393 continue;
5394 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5395 break;
5396
b67d701b 5397 case JDEP_PARM:
e04a16fb 5398 case JDEP_VARIABLE:
b67d701b
PB
5399 type = TREE_TYPE(decl);
5400 if (TREE_CODE (type) == RECORD_TYPE)
5401 type = promote_type (type);
5402 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5403 break;
5404
5405 case JDEP_TYPE:
5406 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5407 SOURCE_FRONTEND_DEBUG
5408 (("Completing a random type dependency on a '%s' node",
5409 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5410 break;
5411
b9f7e36c 5412 case JDEP_EXCEPTION:
c877974e
APB
5413 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5414 SOURCE_FRONTEND_DEBUG
5415 (("Completing `%s' `throws' argument node",
5416 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5417 break;
5418
c2952b01
APB
5419 case JDEP_ANONYMOUS:
5420 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5421 break;
5422
e04a16fb 5423 default:
400500c4 5424 abort ();
e04a16fb
AG
5425 }
5426 }
5427 }
e04a16fb
AG
5428 return;
5429}
5430
5431/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5432 array. */
5433
5434static tree
c2952b01
APB
5435resolve_class (enclosing, class_type, decl, cl)
5436 tree enclosing, class_type, decl, cl;
e04a16fb 5437{
49f48c71
KG
5438 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5439 const char *base = name;
78d21f92
PB
5440 tree resolved_type = TREE_TYPE (class_type);
5441 tree resolved_type_decl;
e04a16fb 5442
78d21f92
PB
5443 if (resolved_type != NULL_TREE)
5444 {
5445 tree resolved_type_decl = TYPE_NAME (resolved_type);
5446 if (resolved_type_decl == NULL_TREE
5447 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5448 {
5449 resolved_type_decl = build_decl (TYPE_DECL,
5450 TYPE_NAME (class_type),
5451 resolved_type);
5452 }
5453 return resolved_type_decl;
5454 }
5455
e04a16fb
AG
5456 /* 1- Check to see if we have an array. If true, find what we really
5457 want to resolve */
5458 while (name[0] == '[')
5459 name++;
5460 if (base != name)
34d4df06
APB
5461 {
5462 TYPE_NAME (class_type) = get_identifier (name);
5463 WFL_STRIP_BRACKET (cl, cl);
5464 }
e04a16fb
AG
5465
5466 /* 2- Resolve the bare type */
c2952b01
APB
5467 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5468 decl, cl)))
e04a16fb
AG
5469 return NULL_TREE;
5470 resolved_type = TREE_TYPE (resolved_type_decl);
5471
5472 /* 3- If we have and array, reconstruct the array down to its nesting */
5473 if (base != name)
5474 {
5475 while (base != name)
5476 {
5477 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5478 resolved_type = promote_type (resolved_type);
5479 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5480 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5481 name--;
5482 }
5483 /* Build a fake decl for this, since this is what is expected to
5484 be returned. */
5485 resolved_type_decl =
5486 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5487 /* Figure how those two things are important for error report. FIXME */
5488 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5489 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5490 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5491 }
78d21f92 5492 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5493 return resolved_type_decl;
5494}
5495
5496/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5497 are used to report error messages. */
5498
78d21f92 5499tree
c2952b01
APB
5500do_resolve_class (enclosing, class_type, decl, cl)
5501 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5502{
5503 tree new_class_decl;
e04a16fb
AG
5504
5505 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5506 it is changed by find_in_imports{_on_demand} and (but it doesn't
5507 really matter) qualify_and_find */
e04a16fb 5508
c2952b01
APB
5509 /* 0- Search in the current class as an inner class */
5510
5511 /* Maybe some code here should be added to load the class or
5512 something, at least if the class isn't an inner class and ended
5513 being loaded from class file. FIXME. */
a40d21da
APB
5514 while (enclosing)
5515 {
5516 tree name;
e1d565ab 5517 tree intermediate;
a40d21da
APB
5518
5519 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5520 return new_class_decl;
8ac1de05 5521
e1d565ab 5522 intermediate = enclosing;
0c2b8145 5523 /* Explore enclosing contexts. */
e1d565ab 5524 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5525 {
e1d565ab
BM
5526 intermediate = DECL_CONTEXT (intermediate);
5527 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5528 class_type, cl)))
5529 return new_class_decl;
5530 }
5531
a40d21da
APB
5532 /* Now go to the upper classes, bail out if necessary. */
5533 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5534 if (!enclosing || enclosing == object_type_node)
5535 break;
5536
5537 if (TREE_CODE (enclosing) == RECORD_TYPE)
5538 {
5539 enclosing = TYPE_NAME (enclosing);
5540 continue;
5541 }
5542
5543 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5544 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5545 else
5546 name = enclosing;
5547 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5548 }
c2952b01 5549
9a7ab4b3
APB
5550 /* 1- Check for the type in single imports. This will change
5551 TYPE_NAME() if something relevant is found */
5552 find_in_imports (class_type);
e04a16fb 5553
9a7ab4b3 5554 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5555 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5556 {
5557 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5558 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5559 load_class (TYPE_NAME (class_type), 0);
5560 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5561 }
5562
9a7ab4b3
APB
5563 /* 3- Search according to the current package definition */
5564 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5565 {
5566 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5567 TYPE_NAME (class_type))))
5568 return new_class_decl;
5569 }
5570
5571 /* 4- Check the import on demands. Don't allow bar.baz to be
5572 imported from foo.* */
5573 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5574 if (find_in_imports_on_demand (class_type))
5575 return NULL_TREE;
5576
5577 /* If found in find_in_imports_on_demant, the type has already been
5578 loaded. */
5579 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5580 return new_class_decl;
5581
5582 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5583 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5584 {
ee07f4f4 5585 tree package;
d6baf6f5
APB
5586
5587 /* If there is a current package (ctxp->package), it's the first
5588 element of package_list and we can skip it. */
5589 for (package = (ctxp->package ?
5590 TREE_CHAIN (package_list) : package_list);
5591 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5592 if ((new_class_decl = qualify_and_find (class_type,
5593 TREE_PURPOSE (package),
5594 TYPE_NAME (class_type))))
5595 return new_class_decl;
5596 }
5597
5598 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5599 load_class (TYPE_NAME (class_type), 0);
5600 if (check_pkg_class_access (TYPE_NAME (class_type),
5601 (cl ? cl : lookup_cl (decl))))
5602 return NULL_TREE;
5603
9a7ab4b3 5604 /* 6- Last call for a resolution */
e04a16fb
AG
5605 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5606}
5607
9a7ab4b3
APB
5608static tree
5609qualify_and_find (class_type, package, name)
5610 tree class_type, package, name;
5611{
5612 tree new_qualified = merge_qualified_name (package, name);
5613 tree new_class_decl;
5614
5615 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5616 load_class (new_qualified, 0);
5617 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5618 {
5619 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5620 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5621 load_class (new_qualified, 0);
5622 TYPE_NAME (class_type) = new_qualified;
5623 return IDENTIFIER_CLASS_VALUE (new_qualified);
5624 }
5625 return NULL_TREE;
5626}
5627
e04a16fb 5628/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5629 parsed class). Return a decl node. This function is meant to be
5630 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5631
5632static tree
c877974e
APB
5633resolve_and_layout (something, cl)
5634 tree something;
e04a16fb
AG
5635 tree cl;
5636{
34d4df06 5637 tree decl, decl_type;
c877974e 5638
23a79c61
APB
5639 /* Don't do that on the current class */
5640 if (something == current_class)
5641 return TYPE_NAME (current_class);
c877974e 5642
23a79c61 5643 /* Don't do anything for void and other primitive types */
c877974e
APB
5644 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5645 return NULL_TREE;
5646
23a79c61
APB
5647 /* Pointer types can be reall pointer types or fake pointers. When
5648 finding a real pointer, recheck for primitive types */
5649 if (TREE_CODE (something) == POINTER_TYPE)
5650 {
5651 if (TREE_TYPE (something))
5652 {
5653 something = TREE_TYPE (something);
5654 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5655 return NULL_TREE;
5656 }
5657 else
5658 something = TYPE_NAME (something);
5659 }
5660
5661 /* Don't do anything for arrays of primitive types */
5662 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5663 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5664 return NULL_TREE;
5665
c2952b01
APB
5666 /* Something might be a WFL */
5667 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5668 something = EXPR_WFL_NODE (something);
5669
5670 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5671 TYPE_DECL or a real TYPE */
5672 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5673 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5674 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5675
23a79c61
APB
5676 if (!(decl = resolve_no_layout (something, cl)))
5677 return NULL_TREE;
5678
5679 /* Resolve and layout if necessary */
34d4df06
APB
5680 decl_type = TREE_TYPE (decl);
5681 layout_class_methods (decl_type);
5682 /* Check methods */
5683 if (CLASS_FROM_SOURCE_P (decl_type))
5684 java_check_methods (decl);
5685 /* Layout the type if necessary */
5686 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5687 safe_layout_class (decl_type);
23a79c61 5688
e04a16fb
AG
5689 return decl;
5690}
5691
5692/* Resolve a class, returns its decl but doesn't perform any
5693 layout. The current parsing context is saved and restored */
5694
5695static tree
5696resolve_no_layout (name, cl)
5697 tree name, cl;
5698{
5699 tree ptr, decl;
5700 BUILD_PTR_FROM_NAME (ptr, name);
5701 java_parser_context_save_global ();
c2952b01 5702 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5703 java_parser_context_restore_global ();
5704
5705 return decl;
5706}
5707
23a79c61
APB
5708/* Called when reporting errors. Skip leader '[' in a complex array
5709 type description that failed to be resolved. */
e04a16fb 5710
49f48c71 5711static const char *
e04a16fb 5712purify_type_name (name)
49f48c71 5713 const char *name;
e04a16fb
AG
5714{
5715 while (*name && *name == '[')
5716 name++;
5717 return name;
5718}
5719
5720/* The type CURRENT refers to can't be found. We print error messages. */
5721
5722static void
5723complete_class_report_errors (dep)
5724 jdep *dep;
5725{
49f48c71 5726 const char *name;
23a79c61
APB
5727
5728 if (!JDEP_WFL (dep))
5729 return;
5730
5731 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5732 switch (JDEP_KIND (dep))
5733 {
5734 case JDEP_SUPER:
5735 parse_error_context
5736 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5737 purify_type_name (name),
e04a16fb
AG
5738 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5739 break;
5740 case JDEP_FIELD:
5741 parse_error_context
5742 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5743 purify_type_name (name),
e04a16fb
AG
5744 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5745 break;
5746 case JDEP_METHOD: /* Covers arguments */
5747 parse_error_context
781b0558 5748 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5749 purify_type_name (name),
e04a16fb
AG
5750 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5751 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5752 break;
5753 case JDEP_METHOD_RETURN: /* Covers return type */
5754 parse_error_context
781b0558 5755 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5756 purify_type_name (name),
e04a16fb
AG
5757 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5758 break;
5759 case JDEP_INTERFACE:
5760 parse_error_context
5761 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5762 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5763 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5764 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5765 break;
5766 case JDEP_VARIABLE:
5767 parse_error_context
781b0558 5768 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5769 purify_type_name (IDENTIFIER_POINTER
5770 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5771 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5772 break;
b9f7e36c
APB
5773 case JDEP_EXCEPTION: /* As specified by `throws' */
5774 parse_error_context
5775 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5776 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5777 break;
0a2138e2
APB
5778 default:
5779 /* Fix for -Wall. Just break doing nothing. The error will be
5780 caught later */
5781 break;
e04a16fb
AG
5782 }
5783}
5784
22eed1e6
APB
5785/* Return a static string containing the DECL prototype string. If
5786 DECL is a constructor, use the class name instead of the form
5787 <init> */
5788
49f48c71 5789static const char *
22eed1e6
APB
5790get_printable_method_name (decl)
5791 tree decl;
5792{
49f48c71 5793 const char *to_return;
9ee9b555 5794 tree name = NULL_TREE;
22eed1e6
APB
5795
5796 if (DECL_CONSTRUCTOR_P (decl))
5797 {
5798 name = DECL_NAME (decl);
5e942c50 5799 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5800 }
5801
5802 to_return = lang_printable_name (decl, 0);
5803 if (DECL_CONSTRUCTOR_P (decl))
5804 DECL_NAME (decl) = name;
5805
5806 return to_return;
5807}
5808
5809/* Track method being redefined inside the same class. As a side
5810 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5811 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5812
e04a16fb
AG
5813static int
5814check_method_redefinition (class, method)
5815 tree class, method;
5816{
c4faeb92 5817 tree redef, sig;
5e942c50 5818
c4faeb92
APB
5819 /* There's no need to verify <clinit> and finit$ */
5820 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
e04a16fb 5821 return 0;
5e942c50 5822
c4faeb92 5823 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
e04a16fb
AG
5824 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5825 {
c3f2a476 5826 if (redef == method)
e04a16fb 5827 break;
c4faeb92
APB
5828 if (DECL_NAME (redef) == DECL_NAME (method)
5829 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5830 && !DECL_ARTIFICIAL (method))
e04a16fb 5831 {
22eed1e6 5832 parse_error_context
c4faeb92 5833 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
22eed1e6
APB
5834 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5835 get_printable_method_name (redef));
e04a16fb
AG
5836 return 1;
5837 }
5838 }
5839 return 0;
5840}
5841
1175b9b4
TT
5842/* Return 1 if check went ok, 0 otherwise. */
5843static int
d77613be
APB
5844check_abstract_method_definitions (do_interface, class_decl, type)
5845 int do_interface;
5846 tree class_decl, type;
5847{
5848 tree class = TREE_TYPE (class_decl);
5849 tree method, end_type;
1175b9b4 5850 int ok = 1;
d77613be
APB
5851
5852 end_type = (do_interface ? object_type_node : type);
5853 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5854 {
5855 tree other_super, other_method, method_sig, method_name;
5856 int found = 0;
165f37bc 5857 int end_type_reached = 0;
d77613be
APB
5858
5859 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5860 continue;
5861
5862 /* Now verify that somewhere in between TYPE and CLASS,
5863 abstract method METHOD gets a non abstract definition
5864 that is inherited by CLASS. */
5865
5866 method_sig = build_java_signature (TREE_TYPE (method));
5867 method_name = DECL_NAME (method);
5868 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5869 method_name = EXPR_WFL_NODE (method_name);
5870
165f37bc
APB
5871 other_super = class;
5872 do {
5873 if (other_super == end_type)
5874 end_type_reached = 1;
5875
5876 /* Method search */
5877 for (other_method = TYPE_METHODS (other_super); other_method;
5878 other_method = TREE_CHAIN (other_method))
5879 {
5880 tree s = build_java_signature (TREE_TYPE (other_method));
5881 tree other_name = DECL_NAME (other_method);
5882
5883 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5884 other_name = EXPR_WFL_NODE (other_name);
5885 if (!DECL_CLINIT_P (other_method)
5886 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5887 && method_name == other_name
5888 && method_sig == s
5889 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5890 {
5891 found = 1;
5892 break;
5893 }
5894 }
5895 other_super = CLASSTYPE_SUPER (other_super);
5896 } while (!end_type_reached);
5897
d77613be
APB
5898 /* Report that abstract METHOD didn't find an implementation
5899 that CLASS can use. */
5900 if (!found)
5901 {
c2e3db92 5902 char *t = xstrdup (lang_printable_name
d77613be
APB
5903 (TREE_TYPE (TREE_TYPE (method)), 0));
5904 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
d77613be
APB
5905
5906 parse_error_context
5907 (lookup_cl (class_decl),
781b0558 5908 "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
5909 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5910 t, lang_printable_name (method, 0),
5911 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5912 "interface" : "class"),
5913 IDENTIFIER_POINTER (ccn),
5914 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5915 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5916 ok = 0;
d77613be 5917 free (t);
d77613be
APB
5918 }
5919 }
1175b9b4
TT
5920
5921 if (ok && do_interface)
5922 {
5923 /* Check for implemented interfaces. */
5924 int i;
5925 tree vector = TYPE_BINFO_BASETYPES (type);
5926 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5927 {
5928 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5929 ok = check_abstract_method_definitions (1, class_decl, super);
5930 }
5931 }
5932
5933 return ok;
d77613be
APB
5934}
5935
614eaae0 5936/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5937 methods. */
5938
5939static void
5940java_check_abstract_method_definitions (class_decl)
5941 tree class_decl;
5942{
5943 tree class = TREE_TYPE (class_decl);
5944 tree super, vector;
5945 int i;
5946
5947 if (CLASS_ABSTRACT (class_decl))
5948 return;
5949
5950 /* Check for inherited types */
165f37bc
APB
5951 super = class;
5952 do {
5953 super = CLASSTYPE_SUPER (super);
5954 check_abstract_method_definitions (0, class_decl, super);
5955 } while (super != object_type_node);
d77613be
APB
5956
5957 /* Check for implemented interfaces. */
5958 vector = TYPE_BINFO_BASETYPES (class);
5959 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5960 {
5961 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5962 check_abstract_method_definitions (1, class_decl, super);
5963 }
5964}
5965
165f37bc
APB
5966/* Check all the types method DECL uses and return 1 if all of them
5967 are now complete, 0 otherwise. This is used to check whether its
5968 safe to build a method signature or not. */
5969
5970static int
5971check_method_types_complete (decl)
5972 tree decl;
5973{
5974 tree type = TREE_TYPE (decl);
5975 tree args;
5976
5977 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5978 return 0;
5979
5980 args = TYPE_ARG_TYPES (type);
5981 if (TREE_CODE (type) == METHOD_TYPE)
5982 args = TREE_CHAIN (args);
5983 for (; args != end_params_node; args = TREE_CHAIN (args))
5984 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5985 return 0;
5986
5987 return 1;
5988}
5989
34d4df06
APB
5990/* Visible interface to check methods contained in CLASS_DECL */
5991
5992void
5993java_check_methods (class_decl)
5994 tree class_decl;
5995{
5996 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5997 return;
5998
5999 if (CLASS_INTERFACE (class_decl))
6000 java_check_abstract_methods (class_decl);
6001 else
6002 java_check_regular_methods (class_decl);
6003
6004 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6005}
6006
d77613be
APB
6007/* Check all the methods of CLASS_DECL. Methods are first completed
6008 then checked according to regular method existance rules. If no
6009 constructor for CLASS_DECL were encountered, then build its
6010 declaration. */
e04a16fb
AG
6011
6012static void
6013java_check_regular_methods (class_decl)
6014 tree class_decl;
6015{
c2952b01 6016 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6017 tree method;
6018 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
c4faeb92 6019 tree found = NULL_TREE;
c877974e
APB
6020 tree mthrows;
6021
6022 /* It is not necessary to check methods defined in java.lang.Object */
6023 if (class == object_type_node)
6024 return;
e04a16fb 6025
23a79c61
APB
6026 if (!TYPE_NVIRTUALS (class))
6027 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6028
6029 /* Should take interfaces into account. FIXME */
6030 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6031 {
5e942c50 6032 tree sig;
c4faeb92 6033 tree method_wfl = DECL_FUNCTION_WFL (method);
e04a16fb
AG
6034 int aflags;
6035
e04a16fb
AG
6036 /* Check for redefinitions */
6037 if (check_method_redefinition (class, method))
6038 continue;
6039
22eed1e6
APB
6040 /* If we see one constructor a mark so we don't generate the
6041 default one. Also skip other verifications: constructors
6042 can't be inherited hence hiden or overriden */
6043 if (DECL_CONSTRUCTOR_P (method))
6044 {
6045 saw_constructor = 1;
6046 continue;
6047 }
6048
c877974e
APB
6049 /* We verify things thrown by the method. They must inherits from
6050 java.lang.Throwable */
6051 for (mthrows = DECL_FUNCTION_THROWS (method);
6052 mthrows; mthrows = TREE_CHAIN (mthrows))
6053 {
6054 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6055 parse_error_context
781b0558 6056 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6057 IDENTIFIER_POINTER
6058 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6059 }
6060
e04a16fb 6061 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6062 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6063
c2952b01
APB
6064 /* Inner class can't declare static methods */
6065 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6066 {
6067 char *t = xstrdup (lang_printable_name (class, 0));
6068 parse_error_context
6069 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6070 lang_printable_name (method, 0), t);
6071 free (t);
6072 }
6073
5e942c50 6074 /* Nothing overrides or it's a private method. */
aabd7048 6075 if (!found)
5e942c50 6076 continue;
aabd7048
PB
6077 if (METHOD_PRIVATE (found))
6078 {
6079 found = NULL_TREE;
6080 continue;
6081 }
5e942c50 6082
614eaae0
APB
6083 /* If `found' is declared in an interface, make sure the
6084 modifier matches. */
6085 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6086 && clinit_identifier_node != DECL_NAME (found)
6087 && !METHOD_PUBLIC (method))
6088 {
6089 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6090 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6091 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6092 lang_printable_name (method, 0),
6093 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6094 }
6095
e04a16fb
AG
6096 /* Can't override a method with the same name and different return
6097 types. */
6098 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6099 {
614eaae0
APB
6100 char *t = xstrdup
6101 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6102 parse_error_context
7f10c2e2 6103 (method_wfl,
b9f7e36c 6104 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6105 lang_printable_name (found, 0), t,
b9f7e36c
APB
6106 IDENTIFIER_POINTER
6107 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6108 free (t);
6109 }
e04a16fb 6110
7f10c2e2 6111 aflags = get_access_flags_from_decl (found);
7f10c2e2 6112
e04a16fb
AG
6113 /* Can't override final. Can't override static. */
6114 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6115 {
6116 /* Static *can* override static */
6117 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6118 continue;
6119 parse_error_context
6120 (method_wfl,
6121 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6122 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6123 lang_printable_name (found, 0),
e04a16fb
AG
6124 (METHOD_FINAL (found) ? "final" : "static"),
6125 IDENTIFIER_POINTER
6126 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6127 continue;
6128 }
7f10c2e2 6129
e04a16fb
AG
6130 /* Static method can't override instance method. */
6131 if (METHOD_STATIC (method))
6132 {
6133 parse_error_context
6134 (method_wfl,
781b0558 6135 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6136 lang_printable_name (found, 0),
e04a16fb
AG
6137 IDENTIFIER_POINTER
6138 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6139 continue;
6140 }
5e942c50 6141
5e942c50
APB
6142 /* - Overriding/hiding public must be public
6143 - Overriding/hiding protected must be protected or public
6144 - If the overriden or hidden method has default (package)
6145 access, then the overriding or hiding method must not be
614eaae0
APB
6146 private; otherwise, a compile-time error occurs. If
6147 `found' belongs to an interface, things have been already
6148 taken care of. */
6149 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6150 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6151 || (METHOD_PROTECTED (found)
6152 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6153 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6154 && METHOD_PRIVATE (method))))
e04a16fb
AG
6155 {
6156 parse_error_context
6157 (method_wfl,
781b0558 6158 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6159 (METHOD_PUBLIC (method) ? "public" :
6160 (METHOD_PRIVATE (method) ? "private" : "protected")),
6161 IDENTIFIER_POINTER (DECL_NAME
6162 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6163 continue;
6164 }
6165
b9f7e36c
APB
6166 /* Overriding methods must have compatible `throws' clauses on checked
6167 exceptions, if any */
6168 check_throws_clauses (method, method_wfl, found);
6169
e04a16fb
AG
6170 /* Inheriting multiple methods with the same signature. FIXME */
6171 }
6172
23a79c61
APB
6173 if (!TYPE_NVIRTUALS (class))
6174 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6175
d77613be
APB
6176 /* Search for inherited abstract method not yet implemented in this
6177 class. */
6178 java_check_abstract_method_definitions (class_decl);
6179
22eed1e6 6180 if (!saw_constructor)
400500c4 6181 abort ();
e04a16fb
AG
6182}
6183
b9f7e36c
APB
6184/* Return a non zero value if the `throws' clause of METHOD (if any)
6185 is incompatible with the `throws' clause of FOUND (if any). */
6186
6187static void
6188check_throws_clauses (method, method_wfl, found)
6189 tree method, method_wfl, found;
6190{
6191 tree mthrows, fthrows;
6192
c877974e
APB
6193 /* Can't check these things with class loaded from bytecode. FIXME */
6194 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6195 return;
6196
b9f7e36c
APB
6197 for (mthrows = DECL_FUNCTION_THROWS (method);
6198 mthrows; mthrows = TREE_CHAIN (mthrows))
6199 {
6200 /* We don't verify unchecked expressions */
c877974e 6201 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6202 continue;
6203 /* Checked expression must be compatible */
6204 for (fthrows = DECL_FUNCTION_THROWS (found);
6205 fthrows; fthrows = TREE_CHAIN (fthrows))
6206 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6207 break;
6208 if (!fthrows)
6209 {
6210 parse_error_context
781b0558 6211 (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 6212 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6213 lang_printable_name (found, 0),
b9f7e36c
APB
6214 IDENTIFIER_POINTER
6215 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6216 }
6217 }
6218}
6219
e04a16fb
AG
6220/* Check abstract method of interface INTERFACE */
6221
6222static void
5e942c50
APB
6223java_check_abstract_methods (interface_decl)
6224 tree interface_decl;
e04a16fb
AG
6225{
6226 int i, n;
6227 tree method, basetype_vec, found;
5e942c50 6228 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6229
6230 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6231 {
e04a16fb
AG
6232 /* 2- Check for double definition inside the defining interface */
6233 if (check_method_redefinition (interface, method))
6234 continue;
6235
6236 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6237 the thrown exceptions (FIXME) */
e04a16fb
AG
6238 found = lookup_java_interface_method2 (interface, method);
6239 if (found)
6240 {
5e942c50 6241 char *t;
c2e3db92 6242 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6243 parse_error_context
c4faeb92 6244 (DECL_FUNCTION_WFL (found),
5e942c50 6245 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6246 lang_printable_name (found, 0), t,
b9f7e36c
APB
6247 IDENTIFIER_POINTER
6248 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6249 free (t);
c63b98cd 6250 continue;
e04a16fb
AG
6251 }
6252 }
6253
6254 /* 4- Inherited methods can't differ by their returned types */
6255 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6256 return;
6257 n = TREE_VEC_LENGTH (basetype_vec);
6258 for (i = 0; i < n; i++)
6259 {
6260 tree sub_interface_method, sub_interface;
6261 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6262 if (!vec_elt)
6263 continue;
6264 sub_interface = BINFO_TYPE (vec_elt);
6265 for (sub_interface_method = TYPE_METHODS (sub_interface);
6266 sub_interface_method;
6267 sub_interface_method = TREE_CHAIN (sub_interface_method))
6268 {
6269 found = lookup_java_interface_method2 (interface,
6270 sub_interface_method);
6271 if (found && (found != sub_interface_method))
5e942c50 6272 {
5e942c50
APB
6273 parse_error_context
6274 (lookup_cl (sub_interface_method),
781b0558 6275 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6276 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6277 lang_printable_name (found, 0),
6278 IDENTIFIER_POINTER
6279 (DECL_NAME (TYPE_NAME
6280 (DECL_CONTEXT (sub_interface_method)))),
6281 IDENTIFIER_POINTER
6282 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5e942c50 6283 }
e04a16fb
AG
6284 }
6285 }
6286}
6287
e04a16fb
AG
6288/* Lookup methods in interfaces using their name and partial
6289 signature. Return a matching method only if their types differ. */
6290
6291static tree
6292lookup_java_interface_method2 (class, method_decl)
6293 tree class, method_decl;
6294{
6295 int i, n;
6296 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6297
6298 if (!basetype_vec)
6299 return NULL_TREE;
6300
6301 n = TREE_VEC_LENGTH (basetype_vec);
6302 for (i = 0; i < n; i++)
6303 {
6304 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6305 if ((BINFO_TYPE (vec_elt) != object_type_node)
6306 && (to_return =
6307 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6308 return to_return;
6309 }
6310 for (i = 0; i < n; i++)
6311 {
6312 to_return = lookup_java_interface_method2
6313 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6314 if (to_return)
6315 return to_return;
6316 }
6317
6318 return NULL_TREE;
6319}
6320
6321/* Lookup method using their name and partial signature. Return a
6322 matching method only if their types differ. */
6323
6324static tree
6325lookup_java_method2 (clas, method_decl, do_interface)
6326 tree clas, method_decl;
6327 int do_interface;
6328{
5e942c50
APB
6329 tree method, method_signature, method_name, method_type, name;
6330
e04a16fb 6331 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6332 name = DECL_NAME (method_decl);
6333 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6334 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6335 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6336
6337 while (clas != NULL_TREE)
6338 {
6339 for (method = TYPE_METHODS (clas);
6340 method != NULL_TREE; method = TREE_CHAIN (method))
6341 {
6342 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6343 tree name = DECL_NAME (method);
6344 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6345 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6346 && method_sig == method_signature
6347 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6348 return method;
e04a16fb
AG
6349 }
6350 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6351 }
6352 return NULL_TREE;
6353}
6354
f441f671
APB
6355/* Return the line that matches DECL line number, and try its best to
6356 position the column number. Used during error reports. */
e04a16fb
AG
6357
6358static tree
6359lookup_cl (decl)
6360 tree decl;
6361{
6362 static tree cl = NULL_TREE;
f441f671 6363 char *line, *found;
e04a16fb
AG
6364
6365 if (!decl)
6366 return NULL_TREE;
6367
6368 if (cl == NULL_TREE)
19e223db
MM
6369 {
6370 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6371 ggc_add_tree_root (&cl, 1);
6372 }
e04a16fb
AG
6373
6374 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6375 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6376
dba41d30 6377 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6378 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6379
6380 found = strstr ((const char *)line,
6381 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6382 if (found)
6383 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6384
e04a16fb
AG
6385 return cl;
6386}
6387
6388/* Look for a simple name in the single-type import list */
6389
6390static tree
6391find_name_in_single_imports (name)
6392 tree name;
6393{
6394 tree node;
6395
6396 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6397 if (TREE_VALUE (node) == name)
6398 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6399
6400 return NULL_TREE;
6401}
6402
6403/* Process all single-type import. */
6404
6405static int
6406process_imports ()
6407{
6408 tree import;
6409 int error_found;
6410
6411 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6412 {
6413 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
1ebb5e73
APB
6414 char *original_name;
6415
6416 obstack_grow0 (&temporary_obstack,
6417 IDENTIFIER_POINTER (to_be_found),
6418 IDENTIFIER_LENGTH (to_be_found));
6419 original_name = obstack_finish (&temporary_obstack);
e04a16fb
AG
6420
6421 /* Don't load twice something already defined. */
6422 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6423 continue;
02ae6e2e
APB
6424
6425 while (1)
6426 {
6427 tree left;
6428
6429 QUALIFIED_P (to_be_found) = 1;
6430 load_class (to_be_found, 0);
6431 error_found =
6432 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6433
6434 /* We found it, we can bail out */
6435 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6436 break;
6437
6438 /* We haven't found it. Maybe we're trying to access an
6439 inner class. The only way for us to know is to try again
6440 after having dropped a qualifier. If we can't break it further,
6441 we have an error. */
6442 if (breakdown_qualified (&left, NULL, to_be_found))
6443 break;
6444
6445 to_be_found = left;
6446 }
e04a16fb
AG
6447 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6448 {
6449 parse_error_context (TREE_PURPOSE (import),
6450 "Class or interface `%s' not found in import",
1ebb5e73
APB
6451 original_name);
6452 error_found = 1;
e04a16fb 6453 }
1ebb5e73
APB
6454
6455 obstack_free (&temporary_obstack, original_name);
e04a16fb
AG
6456 if (error_found)
6457 return 1;
6458 }
6459 return 0;
6460}
6461
9a7ab4b3
APB
6462/* Possibly find and mark a class imported by a single-type import
6463 statement. */
e04a16fb 6464
9a7ab4b3 6465static void
e04a16fb
AG
6466find_in_imports (class_type)
6467 tree class_type;
6468{
6469 tree import;
6470
6471 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6472 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6473 {
6474 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6475 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6476 }
e04a16fb
AG
6477}
6478
e04a16fb 6479static int
63a212ed 6480note_possible_classname (name, len)
49f48c71 6481 const char *name;
63a212ed 6482 int len;
e04a16fb 6483{
63a212ed
PB
6484 tree node;
6485 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6486 len = len - 5;
6487 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6488 len = len - 6;
e04a16fb 6489 else
63a212ed
PB
6490 return 0;
6491 node = ident_subst (name, len, "", '/', '.', "");
6492 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6493 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6494 return 1;
e04a16fb
AG
6495}
6496
6497/* Read a import directory, gathering potential match for further type
6498 references. Indifferently reads a filesystem or a ZIP archive
6499 directory. */
6500
6501static void
6502read_import_dir (wfl)
6503 tree wfl;
6504{
63a212ed 6505 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6506 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6507 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6508 DIR *dirp = NULL;
d8fccff5 6509 JCF *saved_jcf = current_jcf;
e04a16fb 6510
63a212ed
PB
6511 int found = 0;
6512 int k;
6513 void *entry;
6514 struct buffer filename[1];
6515
6516
6517 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6518 return;
6519 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6520
6521 BUFFER_INIT (filename);
6522 buffer_grow (filename, package_length + 100);
6523
6524 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6525 {
49f48c71 6526 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6527 int entry_length = strlen (entry_name);
6528 if (jcf_path_is_zipfile (entry))
6529 {
6530 ZipFile *zipf;
6531 buffer_grow (filename, entry_length);
6532 memcpy (filename->data, entry_name, entry_length - 1);
6533 filename->data[entry_length-1] = '\0';
6534 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6535 if (zipf == NULL)
6536 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6537 else
6538 {
6539 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6540 BUFFER_RESET (filename);
6541 for (k = 0; k < package_length; k++)
6542 {
6543 char ch = package_name[k];
6544 *filename->ptr++ = ch == '.' ? '/' : ch;
6545 }
6546 *filename->ptr++ = '/';
6547
345137c7 6548 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6549 {
49f48c71 6550 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6551 int current_entry_len = zipd->filename_length;
6552
345137c7
TT
6553 if (current_entry_len >= BUFFER_LENGTH (filename)
6554 && strncmp (filename->data, current_entry,
6555 BUFFER_LENGTH (filename)) != 0)
63a212ed 6556 continue;
345137c7 6557 found |= note_possible_classname (current_entry,
63a212ed
PB
6558 current_entry_len);
6559 }
6560 }
6561 }
6562 else
6563 {
6564 BUFFER_RESET (filename);
6565 buffer_grow (filename, entry_length + package_length + 4);
6566 strcpy (filename->data, entry_name);
6567 filename->ptr = filename->data + entry_length;
6568 for (k = 0; k < package_length; k++)
6569 {
6570 char ch = package_name[k];
6571 *filename->ptr++ = ch == '.' ? '/' : ch;
6572 }
6573 *filename->ptr = '\0';
6574
6575 dirp = opendir (filename->data);
6576 if (dirp == NULL)
6577 continue;
6578 *filename->ptr++ = '/';
6579 for (;;)
6580 {
63a212ed 6581 int len;
49f48c71 6582 const char *d_name;
63a212ed
PB
6583 struct dirent *direntp = readdir (dirp);
6584 if (!direntp)
6585 break;
6586 d_name = direntp->d_name;
6587 len = strlen (direntp->d_name);
6588 buffer_grow (filename, len+1);
6589 strcpy (filename->ptr, d_name);
345137c7 6590 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6591 package_length+len+1);
6592 }
6593 if (dirp)
6594 closedir (dirp);
6595 }
6596 }
e04a16fb 6597
63a212ed 6598 free (filename->data);
e04a16fb 6599
63a212ed
PB
6600 /* Here we should have a unified way of retrieving an entry, to be
6601 indexed. */
6602 if (!found)
e04a16fb
AG
6603 {
6604 static int first = 1;
6605 if (first)
6606 {
781b0558 6607 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6608 java_error_count++;
6609 first = 0;
6610 }
6611 else
63a212ed
PB
6612 parse_error_context (wfl, "Package `%s' not found in import",
6613 package_name);
e04a16fb
AG
6614 current_jcf = saved_jcf;
6615 return;
6616 }
e04a16fb
AG
6617 current_jcf = saved_jcf;
6618}
6619
6620/* Possibly find a type in the import on demands specified
6621 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6622 entire list, to detected potential double definitions. */
6623
6624static int
6625find_in_imports_on_demand (class_type)
6626 tree class_type;
6627{
ab3a6dd6 6628 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6629 int seen_once = -1;
ab3a6dd6 6630 tree cl = NULL_TREE;
e04a16fb
AG
6631
6632 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6633 {
49f48c71 6634 const char *id_name;
e04a16fb
AG
6635 obstack_grow (&temporary_obstack,
6636 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6637 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6638 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6639 obstack_grow0 (&temporary_obstack,
6640 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6641 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6642 id_name = obstack_finish (&temporary_obstack);
6643
6644 node = maybe_get_identifier (id_name);
6645 if (node && IS_A_CLASSFILE_NAME (node))
6646 {
6647 if (seen_once < 0)
6648 {
6649 cl = TREE_PURPOSE (import);
6650 seen_once = 1;
6651 node_to_use = node;
6652 }
6653 else
6654 {
6655 seen_once++;
6656 parse_error_context
1e12ab9b
APB
6657 (TREE_PURPOSE (import),
6658 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6659 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6660 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6661 }
6662 }
6663 }
6664
6665 if (seen_once == 1)
6666 {
6667 /* Setup lineno so that it refers to the line of the import (in
6668 case we parse a class file and encounter errors */
6669 tree decl;
6670 int saved_lineno = lineno;
6671 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6672 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6673 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6674 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6675 /* If there is no DECL set for the class or if the class isn't
6676 loaded and not seen in source yet, the load */
6677 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6678 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6679 load_class (node_to_use, 0);
6680 lineno = saved_lineno;
6681 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6682 }
6683 else
6684 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6685}
6686
9a7ab4b3
APB
6687/* Add package NAME to the list of package encountered so far. To
6688 speed up class lookup in do_resolve_class, we make sure a
6689 particular package is added only once. */
6690
6691static void
6692register_package (name)
6693 tree name;
6694{
6695 static struct hash_table _pht, *pht = NULL;
6696
6697 if (!pht)
6698 {
6699 hash_table_init (&_pht, hash_newfunc,
6700 java_hash_hash_tree_node, java_hash_compare_tree_node);
6701 pht = &_pht;
6702 }
6703
6704 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6705 {
6706 package_list = chainon (package_list, build_tree_list (name, NULL));
6707 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6708 }
6709}
6710
5e942c50
APB
6711static tree
6712resolve_package (pkg, next)
6713 tree pkg, *next;
6714{
c2952b01 6715 tree current, acc;
5e942c50 6716 tree type_name = NULL_TREE;
49f48c71 6717 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6718
6719 /* The trick is to determine when the package name stops and were
6720 the name of something contained in the package starts. Then we
6721 return a fully qualified name of what we want to get. */
6722
6723 /* Do a quick search on well known package names */
6724 if (!strncmp (name, "java.lang.reflect", 17))
6725 {
6726 *next =
6727 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6728 type_name = lookup_package_type (name, 17);
6729 }
6730 else if (!strncmp (name, "java.lang", 9))
6731 {
6732 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6733 type_name = lookup_package_type (name, 9);
6734 }
5e942c50 6735
2c56429a
APB
6736 /* If we found something here, return */
6737 if (type_name)
6738 return type_name;
6739
6740 *next = EXPR_WFL_QUALIFICATION (pkg);
6741
c2952b01
APB
6742 /* Try to progressively construct a type name */
6743 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6744 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6745 current; current = TREE_CHAIN (current))
6746 {
6747 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6748 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6749 {
6750 type_name = acc;
6b48deee
APB
6751 /* resolve_package should be used in a loop, hence we
6752 point at this one to naturally process the next one at
6753 the next iteration. */
6754 *next = current;
c2952b01
APB
6755 break;
6756 }
6757 }
2c56429a
APB
6758 return type_name;
6759}
6760
5e942c50
APB
6761static tree
6762lookup_package_type (name, from)
49f48c71 6763 const char *name;
5e942c50
APB
6764 int from;
6765{
6766 char subname [128];
49f48c71 6767 const char *sub = &name[from+1];
5e942c50
APB
6768 while (*sub != '.' && *sub)
6769 sub++;
6770 strncpy (subname, name, sub-name);
6771 subname [sub-name] = '\0';
6772 return get_identifier (subname);
6773}
6774
cf1748bf 6775static void
4dbf4496
APB
6776check_inner_class_access (decl, enclosing_decl, cl)
6777 tree decl, enclosing_decl, cl;
cf1748bf 6778{
4dbf4496
APB
6779 int access = 0;
6780
cf1748bf 6781 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6782 as a result of processing a JDEP crafted by source_start_java_method
6783 for the purpose of patching its parm decl. But the error would
6784 have been already trapped when fixing the method's signature.
6785 DECL can also be NULL in case of earlier errors. */
6786 if (!decl || !cl)
cf1748bf
APB
6787 return;
6788
4dbf4496
APB
6789 /* We grant access to private and protected inner classes if the
6790 location from where we're trying to access DECL is an enclosing
6791 context for DECL or if both have a common enclosing context. */
6792 if (CLASS_PRIVATE (decl))
6793 access = 1;
6794 if (CLASS_PROTECTED (decl))
6795 access = 2;
6796 if (!access)
6797 return;
6798
6799 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6800 TREE_TYPE (decl))
6801 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6802 TREE_TYPE (decl)))
6803 return;
6804
6805 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6806 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6807 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6808 lang_printable_name (decl, 0));
6809}
6810
e04a16fb
AG
6811/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6812 access violations were found, 1 otherwise. */
6813
6814static int
6815check_pkg_class_access (class_name, cl)
6816 tree class_name;
6817 tree cl;
6818{
6819 tree type;
e04a16fb
AG
6820
6821 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6822 return 0;
6823
6824 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6825 return 0;
6826
6827 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6828 {
e28cd97b
APB
6829 /* Access to a private class within the same package is
6830 allowed. */
6831 tree l, r;
6832 breakdown_qualified (&l, &r, class_name);
6833 if (l == ctxp->package)
6834 return 0;
6835
e04a16fb 6836 parse_error_context
781b0558 6837 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6838 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6839 IDENTIFIER_POINTER (class_name));
6840 return 1;
6841 }
6842 return 0;
6843}
6844
6845/* Local variable declaration. */
6846
6847static void
6848declare_local_variables (modifier, type, vlist)
6849 int modifier;
6850 tree type;
6851 tree vlist;
6852{
c583dd46
APB
6853 tree decl, current, saved_type;
6854 tree type_wfl = NULL_TREE;
e04a16fb 6855 int must_chain = 0;
c2952b01 6856 int final_p = 0;
e04a16fb 6857
2aa11e97 6858 /* Push a new block if statements were seen between the last time we
e04a16fb 6859 pushed a block and now. Keep a cound of block to close */
f099f336 6860 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6861 {
f099f336 6862 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6863 tree b = enter_block ();
f099f336 6864 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6865 }
6866
6867 if (modifier)
6868 {
6869 int i;
6870 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6871 if (modifier == ACC_FINAL)
c2952b01 6872 final_p = 1;
c877974e
APB
6873 else
6874 {
6875 parse_error_context
6876 (ctxp->modifier_ctx [i],
6877 "Only `final' is allowed as a local variables modifier");
6878 return;
6879 }
e04a16fb
AG
6880 }
6881
c583dd46
APB
6882 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6883 hold the TYPE value if a new incomplete has to be created (as
6884 opposed to being found already existing and reused). */
6885 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6886
6887 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6888 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6889
6890 /* Go through all the declared variables */
6891 for (current = vlist, saved_type = type; current;
6892 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6893 {
c877974e 6894 tree other, real_type;
e04a16fb
AG
6895 tree wfl = TREE_PURPOSE (current);
6896 tree name = EXPR_WFL_NODE (wfl);
6897 tree init = TREE_VALUE (current);
e04a16fb 6898
c583dd46
APB
6899 /* Process NAME, as it may specify extra dimension(s) for it */
6900 type = build_array_from_name (type, type_wfl, name, &name);
6901
6902 /* Variable redefinition check */
6903 if ((other = lookup_name_in_blocks (name)))
6904 {
6905 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6906 DECL_SOURCE_LINE (other));
6907 continue;
6908 }
6909
6910 /* Type adjustment. We may have just readjusted TYPE because
6911 the variable specified more dimensions. Make sure we have
6912 a reference if we can and don't have one already. */
1886c9d8 6913 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6914
6915 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6916 /* Never layout this decl. This will be done when its scope
6917 will be entered */
c877974e 6918 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 6919 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 6920 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6921 BLOCK_CHAIN_DECL (decl);
6922
d4370213
APB
6923 /* If doing xreferencing, replace the line number with the WFL
6924 compound value */
6925 if (flag_emit_xref)
6926 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6927
e04a16fb
AG
6928 /* Don't try to use an INIT statement when an error was found */
6929 if (init && java_error_count)
6930 init = NULL_TREE;
c583dd46
APB
6931
6932 /* Add the initialization function to the current function's code */
6933 if (init)
e04a16fb 6934 {
c583dd46
APB
6935 /* Name might have been readjusted */
6936 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6937 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6938 java_method_add_stmt (current_function_decl,
6939 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6940 init));
6941 }
6942
6943 /* Setup dependency the type of the decl */
6944 if (must_chain)
6945 {
6946 jdep *dep;
6947 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6948 dep = CLASSD_LAST (ctxp->classd_list);
6949 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6950 }
6951 }
6952 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6953}
6954
6955/* Called during parsing. Build decls from argument list. */
6956
6957static void
6958source_start_java_method (fndecl)
6959 tree fndecl;
6960{
6961 tree tem;
6962 tree parm_decl;
6963 int i;
6964
79d13333
APB
6965 if (!fndecl)
6966 return;
6967
e04a16fb
AG
6968 current_function_decl = fndecl;
6969
6970 /* New scope for the function */
6971 enter_block ();
6972 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 6973 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
6974 {
6975 tree type = TREE_VALUE (tem);
6976 tree name = TREE_PURPOSE (tem);
6977
23a79c61
APB
6978 /* If type is incomplete. Create an incomplete decl and ask for
6979 the decl to be patched later */
e04a16fb
AG
6980 if (INCOMPLETE_TYPE_P (type))
6981 {
6982 jdep *jdep;
c877974e
APB
6983 tree real_type = GET_REAL_TYPE (type);
6984 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 6985 type = obtain_incomplete_type (type);
e04a16fb
AG
6986 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6987 jdep = CLASSD_LAST (ctxp->classd_list);
6988 JDEP_MISC (jdep) = name;
6989 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6990 }
6991 else
6992 parm_decl = build_decl (PARM_DECL, name, type);
6993
c2952b01
APB
6994 /* Remember if a local variable was declared final (via its
6995 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6996 if (ARG_FINAL_P (tem))
c7303e41
APB
6997 {
6998 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
6999 LOCAL_FINAL (parm_decl) = 1;
7000 }
c2952b01 7001
e04a16fb
AG
7002 BLOCK_CHAIN_DECL (parm_decl);
7003 }
7004 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7005 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7006 nreverse (tem);
7007 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7008 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7009}
7010
22eed1e6
APB
7011/* Called during parsing. Creates an artificial method declaration. */
7012
7013static tree
7014create_artificial_method (class, flags, type, name, args)
7015 tree class;
7016 int flags;
7017 tree type, name, args;
7018{
22eed1e6
APB
7019 tree mdecl;
7020
c2952b01 7021 java_parser_context_save_global ();
22eed1e6
APB
7022 lineno = 0;
7023 mdecl = make_node (FUNCTION_TYPE);
7024 TREE_TYPE (mdecl) = type;
7025 TYPE_ARG_TYPES (mdecl) = args;
7026 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7027 java_parser_context_restore_global ();
22eed1e6
APB
7028 DECL_ARTIFICIAL (mdecl) = 1;
7029 return mdecl;
7030}
7031
7032/* Starts the body if an artifical method. */
7033
7034static void
7035start_artificial_method_body (mdecl)
7036 tree mdecl;
7037{
7038 DECL_SOURCE_LINE (mdecl) = 1;
7039 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7040 source_start_java_method (mdecl);
7041 enter_block ();
7042}
7043
7044static void
7045end_artificial_method_body (mdecl)
7046 tree mdecl;
7047{
1e0cdc10
APB
7048 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7049 It has to be evaluated first. (if mdecl is current_function_decl,
7050 we have an undefined behavior if no temporary variable is used.) */
7051 tree b = exit_block ();
7052 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7053 exit_block ();
7054}
7055
e04a16fb
AG
7056/* Called during expansion. Push decls formerly built from argument
7057 list so they're usable during expansion. */
7058
7059static void
7060expand_start_java_method (fndecl)
7061 tree fndecl;
7062{
7063 tree tem, *ptr;
e04a16fb 7064
e04a16fb
AG
7065 current_function_decl = fndecl;
7066
c2952b01
APB
7067 if (! quiet_flag)
7068 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7069 announce_function (fndecl);
c2952b01
APB
7070 if (! quiet_flag)
7071 fprintf (stderr, "]");
7072
7073 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7074 ptr = &DECL_ARGUMENTS (fndecl);
7075 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7076 while (tem)
7077 {
7078 tree next = TREE_CHAIN (tem);
b67d701b 7079 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7080 if (PROMOTE_PROTOTYPES
7081 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7082 && INTEGRAL_TYPE_P (type))
7083 type = integer_type_node;
b67d701b 7084 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7085 layout_decl (tem, 0);
7086 pushdecl (tem);
e04a16fb
AG
7087 *ptr = tem;
7088 ptr = &TREE_CHAIN (tem);
7089 tem = next;
7090 }
7091 *ptr = NULL_TREE;
7092 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7093 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7094}
7095
7096/* Terminate a function and expand its body. */
7097
7098static void
7099source_end_java_method ()
7100{
7101 tree fndecl = current_function_decl;
138657ec 7102 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7103
79d13333
APB
7104 if (!fndecl)
7105 return;
7106
e04a16fb
AG
7107 java_parser_context_save_global ();
7108 lineno = ctxp->last_ccb_indent1;
7109
b67d701b
PB
7110 /* Set EH language codes */
7111 java_set_exception_lang_code ();
7112
5423609c
APB
7113 /* Turn function bodies with only a NOP expr null, so they don't get
7114 generated at all and we won't get warnings when using the -W
7115 -Wall flags. */
7116 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7117 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7118
e04a16fb
AG
7119 /* Generate function's code */
7120 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7121 && ! flag_emit_class_files
7122 && ! flag_emit_xref)
e04a16fb
AG
7123 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7124
7125 /* pop out of its parameters */
7126 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7127 poplevel (1, 0, 1);
7128 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7129
7130 /* Generate rtl for function exit. */
e8fc7396 7131 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7132 {
7133 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7134 /* Emit catch-finally clauses */
7135 emit_handlers ();
e04a16fb
AG
7136 expand_function_end (input_filename, lineno, 0);
7137
138657ec
AH
7138 /* FIXME: If the current method contains any exception handlers,
7139 force asynchronous_exceptions: this is necessary because signal
7140 handlers in libjava may throw exceptions. This is far from being
7141 a perfect solution, but it's better than doing nothing at all.*/
7142 if (catch_clauses)
7143 asynchronous_exceptions = 1;
7144
e04a16fb
AG
7145 /* Run the optimizers and output assembler code for this function. */
7146 rest_of_compilation (fndecl);
7147 }
7148
7149 current_function_decl = NULL_TREE;
e04a16fb 7150 java_parser_context_restore_global ();
138657ec 7151 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7152}
7153
7154/* Record EXPR in the current function block. Complements compound
7155 expression second operand if necessary. */
7156
7157tree
7158java_method_add_stmt (fndecl, expr)
7159 tree fndecl, expr;
7160{
b771925e
APB
7161 if (!GET_CURRENT_BLOCK (fndecl))
7162 return NULL_TREE;
f099f336 7163 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7164}
e04a16fb 7165
b67d701b
PB
7166static tree
7167add_stmt_to_block (b, type, stmt)
7168 tree b, type, stmt;
7169{
7170 tree body = BLOCK_EXPR_BODY (b), c;
7171
e04a16fb
AG
7172 if (java_error_count)
7173 return body;
b67d701b
PB
7174
7175 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7176 return body;
7177
b67d701b
PB
7178 BLOCK_EXPR_BODY (b) = c;
7179 TREE_SIDE_EFFECTS (c) = 1;
7180 return c;
e04a16fb
AG
7181}
7182
7183/* Add STMT to EXISTING if possible, otherwise create a new
7184 COMPOUND_EXPR and add STMT to it. */
7185
7186static tree
7187add_stmt_to_compound (existing, type, stmt)
7188 tree existing, type, stmt;
7189{
15fdcfe9
PB
7190 if (existing)
7191 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7192 else
15fdcfe9 7193 return stmt;
e04a16fb
AG
7194}
7195
1886c9d8
APB
7196void java_layout_seen_class_methods ()
7197{
7198 tree previous_list = all_class_list;
7199 tree end = NULL_TREE;
7200 tree current;
7201
7202 while (1)
7203 {
7204 for (current = previous_list;
7205 current != end; current = TREE_CHAIN (current))
7206 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7207
7208 if (previous_list != all_class_list)
7209 {
7210 end = previous_list;
7211 previous_list = all_class_list;
7212 }
7213 else
7214 break;
7215 }
7216}
7217
e04a16fb 7218void
c2952b01 7219java_reorder_fields ()
e04a16fb 7220{
c2952b01 7221 static tree stop_reordering = NULL_TREE;
19e223db 7222 static int initialized_p;
c2952b01 7223 tree current;
19e223db
MM
7224
7225 /* Register STOP_REORDERING with the garbage collector. */
7226 if (!initialized_p)
7227 {
7228 ggc_add_tree_root (&stop_reordering, 1);
7229 initialized_p = 1;
7230 }
7231
5e942c50 7232 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7233 {
5e942c50 7234 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7235
c2952b01
APB
7236 if (current_class == stop_reordering)
7237 break;
7238
c877974e
APB
7239 /* Reverse the fields, but leave the dummy field in front.
7240 Fields are already ordered for Object and Class */
7241 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7242 && current_class != class_type_node)
7243 {
23a79c61
APB
7244 /* If the dummy field is there, reverse the right fields and
7245 just layout the type for proper fields offset */
c877974e
APB
7246 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7247 {
7248 tree fields = TYPE_FIELDS (current_class);
7249 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7250 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7251 }
23a79c61
APB
7252 /* We don't have a dummy field, we need to layout the class,
7253 after having reversed the fields */
c877974e
APB
7254 else
7255 {
7256 TYPE_FIELDS (current_class) =
7257 nreverse (TYPE_FIELDS (current_class));
7258 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7259 }
7260 }
c2952b01
APB
7261 }
7262 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7263}
7264
7265/* Layout the methods of all classes loaded in one way on an
7266 other. Check methods of source parsed classes. Then reorder the
7267 fields and layout the classes or the type of all source parsed
7268 classes */
7269
7270void
7271java_layout_classes ()
7272{
7273 tree current;
7274 int save_error_count = java_error_count;
7275
7276 /* Layout the methods of all classes seen so far */
7277 java_layout_seen_class_methods ();
7278 java_parse_abort_on_error ();
7279 all_class_list = NULL_TREE;
7280
7281 /* Then check the methods of all parsed classes */
7282 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7283 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7284 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7285 java_parse_abort_on_error ();
7286
7287 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7288 {
7289 current_class = TREE_TYPE (TREE_VALUE (current));
7290 layout_class (current_class);
5e942c50 7291
c877974e
APB
7292 /* From now on, the class is considered completely loaded */
7293 CLASS_LOADED_P (current_class) = 1;
7294
5e942c50
APB
7295 /* Error reported by the caller */
7296 if (java_error_count)
7297 return;
e04a16fb 7298 }
23a79c61
APB
7299
7300 /* We might have reloaded classes durign the process of laying out
7301 classes for code generation. We must layout the methods of those
7302 late additions, as constructor checks might use them */
1886c9d8 7303 java_layout_seen_class_methods ();
23a79c61 7304 java_parse_abort_on_error ();
e04a16fb
AG
7305}
7306
c2952b01
APB
7307/* Expand methods in the current set of classes rememebered for
7308 generation. */
e04a16fb 7309
49f48c71 7310static void
c2952b01 7311java_complete_expand_classes ()
e04a16fb
AG
7312{
7313 tree current;
ce6e9147
APB
7314
7315 do_not_fold = flag_emit_xref;
c2952b01 7316
e04a16fb 7317 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7318 if (!INNER_CLASS_DECL_P (current))
7319 java_complete_expand_class (current);
7320}
e04a16fb 7321
c2952b01
APB
7322/* Expand the methods found in OUTER, starting first by OUTER's inner
7323 classes, if any. */
e04a16fb 7324
c2952b01
APB
7325static void
7326java_complete_expand_class (outer)
7327 tree outer;
7328{
7329 tree inner_list;
e04a16fb 7330
c2952b01 7331 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7332
c2952b01
APB
7333 /* We need to go after all inner classes and start expanding them,
7334 starting with most nested ones. We have to do that because nested
7335 classes might add functions to outer classes */
e04a16fb 7336
c2952b01
APB
7337 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7338 inner_list; inner_list = TREE_CHAIN (inner_list))
7339 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7340
c2952b01
APB
7341 java_complete_expand_methods (outer);
7342 set_nested_class_simple_name_value (outer, 0); /* Reset */
7343}
7344
7345/* Expand methods registered in CLASS_DECL. The general idea is that
7346 we expand regular methods first. This allows us get an estimate on
7347 how outer context local alias fields are really used so we can add
7348 to the constructor just enough code to initialize them properly (it
c00f0fb2 7349 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7350 constructors and then <clinit>. */
7351
7352static void
7353java_complete_expand_methods (class_decl)
7354 tree class_decl;
7355{
7356 tree clinit, finit, decl, first_decl;
7357
7358 current_class = TREE_TYPE (class_decl);
7359
c7303e41
APB
7360 /* Find whether the class has final variables */
7361 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7362 if (FIELD_FINAL (decl))
7363 {
7364 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7365 break;
7366 }
7367
c2952b01
APB
7368 /* Initialize a new constant pool */
7369 init_outgoing_cpool ();
7370
7371 /* Pre-expand <clinit> to figure whether we really need it or
7372 not. If we do need it, we pre-expand the static fields so they're
7373 ready to be used somewhere else. <clinit> will be fully expanded
7374 after we processed the constructors. */
7375 first_decl = TYPE_METHODS (current_class);
7376 clinit = maybe_generate_pre_expand_clinit (current_class);
7377
c00f0fb2 7378 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7379 try to use it.*/
7380 if (TYPE_FINIT_STMT_LIST (current_class))
7381 {
7382 finit = generate_finit (current_class);
7383 java_complete_expand_method (finit);
7384 }
7385
7386 /* Now do the constructors */
7387 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7388 {
7389 int no_body;
7390
7391 if (!DECL_CONSTRUCTOR_P (decl))
7392 continue;
7393
7394 no_body = !DECL_FUNCTION_BODY (decl);
7395 /* Don't generate debug info on line zero when expanding a
7396 generated constructor. */
7397 if (no_body)
7398 restore_line_number_status (1);
7399
c7303e41
APB
7400 /* Reset the final local variable assignment flags */
7401 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7402 reset_final_variable_local_assignment_flag (current_class);
7403
c2952b01 7404 java_complete_expand_method (decl);
c7303e41
APB
7405
7406 /* Check for missed out final variable assignment */
7407 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7408 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7409
7410 if (no_body)
7411 restore_line_number_status (0);
7412 }
7413
7414 /* First, do the ordinary methods. */
7415 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7416 {
7145d9fe
TT
7417 /* Skip abstract or native methods -- but do handle native
7418 methods when generating JNI stubs. */
7419 if (METHOD_ABSTRACT (decl)
7420 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7421 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7422 continue;
7145d9fe
TT
7423
7424 if (METHOD_NATIVE (decl))
7425 {
7426 tree body = build_jni_stub (decl);
7427 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7428 }
7429
c2952b01
APB
7430 java_complete_expand_method (decl);
7431 }
7432
7433 /* If there is indeed a <clinit>, fully expand it now */
7434 if (clinit)
7435 {
c7303e41
APB
7436 /* Reset the final local variable assignment flags */
7437 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7438 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7439 /* Prevent the use of `this' inside <clinit> */
7440 ctxp->explicit_constructor_p = 1;
7441 java_complete_expand_method (clinit);
7442 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7443 /* Check for missed out static final variable assignment */
7444 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7445 && !CLASS_INTERFACE (class_decl))
7446 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7447 }
c2952b01 7448
165f37bc
APB
7449 /* We might have generated a class$ that we now want to expand */
7450 if (TYPE_DOT_CLASS (current_class))
7451 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7452
c2952b01
APB
7453 /* Now verify constructor circularity (stop after the first one we
7454 prove wrong.) */
7455 if (!CLASS_INTERFACE (class_decl))
7456 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7457 if (DECL_CONSTRUCTOR_P (decl)
7458 && verify_constructor_circularity (decl, decl))
7459 break;
7460
c7303e41
APB
7461 /* Final check on the initialization of final variables. */
7462 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7463 {
7464 check_final_variable_global_assignment_flag (current_class);
7465 /* If we have an interface, check for uninitialized fields. */
7466 if (CLASS_INTERFACE (class_decl))
7467 check_static_final_variable_assignment_flag (current_class);
7468 }
7469
c2952b01
APB
7470 /* Save the constant pool. We'll need to restore it later. */
7471 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7472}
7473
c2952b01
APB
7474/* Attempt to create <clinit>. Pre-expand static fields so they can be
7475 safely used in some other methods/constructors. */
e920ebc9 7476
c2952b01
APB
7477static tree
7478maybe_generate_pre_expand_clinit (class_type)
7479 tree class_type;
e920ebc9 7480{
c2952b01
APB
7481 tree current, mdecl;
7482
7483 if (!TYPE_CLINIT_STMT_LIST (class_type))
7484 return NULL_TREE;
e920ebc9 7485
c2952b01
APB
7486 /* Go through all static fields and pre expand them */
7487 for (current = TYPE_FIELDS (class_type); current;
7488 current = TREE_CHAIN (current))
7489 if (FIELD_STATIC (current))
7490 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7491
7492 /* Then build the <clinit> method */
7493 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7494 clinit_identifier_node, end_params_node);
7495 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7496 mdecl, NULL_TREE);
7497 start_artificial_method_body (mdecl);
7498
7499 /* We process the list of assignment we produced as the result of
7500 the declaration of initialized static field and add them as
7501 statement to the <clinit> method. */
7502 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7503 current = TREE_CHAIN (current))
e920ebc9 7504 {
9a7ab4b3 7505 tree stmt = current;
c2952b01
APB
7506 /* We build the assignment expression that will initialize the
7507 field to its value. There are strict rules on static
7508 initializers (8.5). FIXME */
98a52c2c 7509 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7510 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7511 java_method_add_stmt (mdecl, stmt);
7512 }
e920ebc9 7513
c2952b01
APB
7514 end_artificial_method_body (mdecl);
7515
92d83515
APB
7516 /* Now we want to place <clinit> as the last method (because we need
7517 it at least for interface so that it doesn't interfere with the
7518 dispatch table based lookup. */
7519 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7520 {
92d83515
APB
7521 current = TREE_CHAIN (TYPE_METHODS (class_type));
7522 TYPE_METHODS (class_type) = current;
c2952b01
APB
7523
7524 while (TREE_CHAIN (current))
7525 current = TREE_CHAIN (current);
92d83515 7526
c2952b01
APB
7527 TREE_CHAIN (current) = mdecl;
7528 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7529 }
c2952b01
APB
7530
7531 return mdecl;
e920ebc9
APB
7532}
7533
dba41d30
APB
7534/* Analyzes a method body and look for something that isn't a
7535 MODIFY_EXPR. */
7536
7537static int
7538analyze_clinit_body (bbody)
7539 tree bbody;
7540{
7541 while (bbody)
7542 switch (TREE_CODE (bbody))
7543 {
7544 case BLOCK:
7545 bbody = BLOCK_EXPR_BODY (bbody);
7546 break;
7547
7548 case EXPR_WITH_FILE_LOCATION:
7549 bbody = EXPR_WFL_NODE (bbody);
7550 break;
7551
7552 case COMPOUND_EXPR:
7553 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7554 return 1;
7555 bbody = TREE_OPERAND (bbody, 1);
7556 break;
7557
7558 case MODIFY_EXPR:
7559 bbody = NULL_TREE;
7560 break;
7561
7562 default:
7563 bbody = NULL_TREE;
7564 return 1;
7565 }
7566 return 0;
7567}
7568
7569
92d83515
APB
7570/* See whether we could get rid of <clinit>. Criteria are: all static
7571 final fields have constant initial values and the body of <clinit>
7572 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7573
7574static int
7575maybe_yank_clinit (mdecl)
7576 tree mdecl;
7577{
7578 tree type, current;
7579 tree fbody, bbody;
99eaf8d4 7580 int found = 0;
92d83515
APB
7581
7582 if (!DECL_CLINIT_P (mdecl))
7583 return 0;
f0f3a777
APB
7584
7585 /* If the body isn't empty, then we keep <clinit>. Note that if
7586 we're emitting classfiles, this isn't enough not to rule it
7587 out. */
92d83515 7588 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7589 bbody = BLOCK_EXPR_BODY (fbody);
7590 if (bbody && bbody != error_mark_node)
92d83515 7591 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7592 else
7593 return 0;
f0f3a777 7594 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7595 return 0;
7596
7597 type = DECL_CONTEXT (mdecl);
7598 current = TYPE_FIELDS (type);
7599
7600 for (current = (current ? TREE_CHAIN (current) : current);
7601 current; current = TREE_CHAIN (current))
f0f3a777
APB
7602 {
7603 tree f_init;
7604
7605 /* We're not interested in non static field */
7606 if (!FIELD_STATIC (current))
7607 continue;
7608
7609 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7610 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7611 should generated an empty <clinit> so that SUID are computed
7612 correctly. */
7613 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7614 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7615 break;
7616
7617 f_init = DECL_INITIAL (current);
7618 /* If we're emitting native code, we want static final fields to
7619 have constant initializers. If we don't meet these
7620 conditions, we keep <clinit> */
7621 if (!flag_emit_class_files
7622 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7623 break;
7624 /* If we're emitting bytecode, we want static fields to have
7625 constant initializers or no initializer. If we don't meet
7626 these conditions, we keep <clinit> */
7627 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7628 break;
7629 }
92d83515 7630
99eaf8d4
APB
7631 /* Now we analyze the method body and look for something that
7632 isn't a MODIFY_EXPR */
7633 if (bbody == empty_stmt_node)
dba41d30
APB
7634 found = 0;
7635 else
7636 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7637
7638 if (current || found)
92d83515
APB
7639 return 0;
7640
7641 /* Get rid of <clinit> in the class' list of methods */
7642 if (TYPE_METHODS (type) == mdecl)
7643 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7644 else
7645 for (current = TYPE_METHODS (type); current;
7646 current = TREE_CHAIN (current))
7647 if (TREE_CHAIN (current) == mdecl)
7648 {
7649 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7650 break;
7651 }
7652
7653 return 1;
7654}
7655
7656
e04a16fb
AG
7657/* Complete and expand a method. */
7658
7659static void
7660java_complete_expand_method (mdecl)
7661 tree mdecl;
7662{
92d83515
APB
7663 int yank_clinit = 0;
7664
c2952b01 7665 current_function_decl = mdecl;
22eed1e6
APB
7666 /* Fix constructors before expanding them */
7667 if (DECL_CONSTRUCTOR_P (mdecl))
7668 fix_constructors (mdecl);
e04a16fb 7669
22eed1e6 7670 /* Expand functions that have a body */
e04a16fb
AG
7671 if (DECL_FUNCTION_BODY (mdecl))
7672 {
9bbc7d9f
PB
7673 tree fbody = DECL_FUNCTION_BODY (mdecl);
7674 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7675 tree exception_copy = NULL_TREE;
e04a16fb 7676 expand_start_java_method (mdecl);
939d7216 7677 build_result_decl (mdecl);
e04a16fb
AG
7678
7679 current_this
7680 = (!METHOD_STATIC (mdecl) ?
7681 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7682
ce6e9147
APB
7683 /* Purge the `throws' list of unchecked exceptions. If we're
7684 doing xref, save a copy of the list and re-install it
7685 later. */
7686 if (flag_emit_xref)
7687 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7688
b9f7e36c
APB
7689 purge_unchecked_exceptions (mdecl);
7690
7691 /* Install exceptions thrown with `throws' */
7692 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7693
9bbc7d9f 7694 if (block_body != NULL_TREE)
bc3ca41b
PB
7695 {
7696 block_body = java_complete_tree (block_body);
c2952b01 7697
7145d9fe 7698 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7699 check_for_initialization (block_body);
f099f336 7700 ctxp->explicit_constructor_p = 0;
bc3ca41b 7701 }
e803d3b2 7702
9bbc7d9f 7703 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7704
c2952b01
APB
7705 /* If we saw a return but couldn't evaluate it properly, we'll
7706 have an error_mark_node here. */
7707 if (block_body != error_mark_node
7708 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7709 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7710 && !flag_emit_xref)
82371d41 7711 missing_return_error (current_function_decl);
7525cc04 7712
92d83515
APB
7713 /* Check wether we could just get rid of clinit, now the picture
7714 is complete. */
7715 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7716 complete_start_java_method (mdecl);
7717
e04a16fb 7718 /* Don't go any further if we've found error(s) during the
92d83515
APB
7719 expansion */
7720 if (!java_error_count && !yank_clinit)
e04a16fb 7721 source_end_java_method ();
22eed1e6
APB
7722 else
7723 {
92d83515
APB
7724 if (java_error_count)
7725 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7726 poplevel (1, 0, 1);
7727 }
b9f7e36c
APB
7728
7729 /* Pop the exceptions and sanity check */
7730 POP_EXCEPTIONS();
7731 if (currently_caught_type_list)
400500c4 7732 abort ();
ce6e9147
APB
7733
7734 if (flag_emit_xref)
7735 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7736 }
7737}
7738
c2952b01
APB
7739\f
7740
7741/* This section of the code deals with accessing enclosing context
7742 fields either directly by using the relevant access to this$<n> or
7743 by invoking an access method crafted for that purpose. */
7744
7745/* Build the necessary access from an inner class to an outer
7746 class. This routine could be optimized to cache previous result
7747 (decl, current_class and returned access). When an access method
7748 needs to be generated, it always takes the form of a read. It might
7749 be later turned into a write by calling outer_field_access_fix. */
7750
7751static tree
7752build_outer_field_access (id, decl)
7753 tree id, decl;
7754{
7755 tree access = NULL_TREE;
7756 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7757 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7758
5e18f6d6
APB
7759 /* If the immediate enclosing context of the current class is the
7760 field decl's class or inherits from it; build the access as
7761 `this$<n>.<field>'. Note that we will break the `private' barrier
7762 if we're not emitting bytecodes. */
7763 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7764 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7765 {
7766 tree thisn = build_current_thisn (current_class);
7767 access = make_qualified_primary (build_wfl_node (thisn),
7768 id, EXPR_WFL_LINECOL (id));
7769 }
7770 /* Otherwise, generate access methods to outer this and access the
7771 field (either using an access method or by direct access.) */
7772 else
7773 {
7774 int lc = EXPR_WFL_LINECOL (id);
7775
7776 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7777 get a hold to the enclosing instance we need, and then we
c2952b01 7778 build the field access. */
5e18f6d6 7779 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7780
7781 /* If the field is private and we're generating bytecode, then
7782 we generate an access method */
7783 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7784 {
7785 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7786 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7787 name, access, NULL_TREE);
7788 }
7789 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7790 Once again we break the `private' access rule from a foreign
7791 class. */
7792 else
7793 access = make_qualified_primary (access, id, lc);
7794 }
7795 return resolve_expression_name (access, NULL);
7796}
7797
7798/* Return a non zero value if NODE describes an outer field inner
7799 access. */
7800
7801static int
7802outer_field_access_p (type, decl)
7803 tree type, decl;
7804{
7805 if (!INNER_CLASS_TYPE_P (type)
7806 || TREE_CODE (decl) != FIELD_DECL
7807 || DECL_CONTEXT (decl) == type)
7808 return 0;
ee5f86dc
APB
7809
7810 /* If the inner class extends the declaration context of the field
7811 we're try to acces, then this isn't an outer field access */
7812 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7813 return 0;
c2952b01
APB
7814
7815 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7816 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7817 {
7818 if (type == DECL_CONTEXT (decl))
7819 return 1;
5e18f6d6 7820
c2952b01 7821 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7822 {
7823 /* Before we give up, see whether the field is inherited from
7824 the enclosing context we're considering. */
7825 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7826 return 1;
7827 break;
7828 }
c2952b01
APB
7829 }
7830
7831 return 0;
7832}
7833
7834/* Return a non zero value if NODE represents an outer field inner
7835 access that was been already expanded. As a side effect, it returns
7836 the name of the field being accessed and the argument passed to the
7837 access function, suitable for a regeneration of the access method
7838 call if necessary. */
7839
7840static int
7841outer_field_expanded_access_p (node, name, arg_type, arg)
7842 tree node, *name, *arg_type, *arg;
7843{
7844 int identified = 0;
7845
7846 if (TREE_CODE (node) != CALL_EXPR)
7847 return 0;
7848
7849 /* Well, gcj generates slightly different tree nodes when compiling
7850 to native or bytecodes. It's the case for function calls. */
7851
7852 if (flag_emit_class_files
7853 && TREE_CODE (node) == CALL_EXPR
7854 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7855 identified = 1;
7856 else if (!flag_emit_class_files)
7857 {
7858 node = TREE_OPERAND (node, 0);
7859
7860 if (node && TREE_OPERAND (node, 0)
7861 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7862 {
7863 node = TREE_OPERAND (node, 0);
7864 if (TREE_OPERAND (node, 0)
7865 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7866 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7867 (DECL_NAME (TREE_OPERAND (node, 0)))))
7868 identified = 1;
7869 }
7870 }
7871
7872 if (identified && name && arg_type && arg)
7873 {
7874 tree argument = TREE_OPERAND (node, 1);
7875 *name = DECL_NAME (TREE_OPERAND (node, 0));
7876 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7877 *arg = TREE_VALUE (argument);
7878 }
7879 return identified;
7880}
7881
7882/* Detect in NODE an outer field read access from an inner class and
7883 transform it into a write with RHS as an argument. This function is
7884 called from the java_complete_lhs when an assignment to a LHS can
7885 be identified. */
7886
7887static tree
7888outer_field_access_fix (wfl, node, rhs)
7889 tree wfl, node, rhs;
7890{
7891 tree name, arg_type, arg;
7892
7893 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7894 {
7895 /* At any rate, check whether we're trying to assign a value to
7896 a final. */
7897 tree accessed = (JDECL_P (node) ? node :
7898 (TREE_CODE (node) == COMPONENT_REF ?
7899 TREE_OPERAND (node, 1) : node));
7900 if (check_final_assignment (accessed, wfl))
7901 return error_mark_node;
7902
7903 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7904 arg_type, name, arg, rhs);
7905 return java_complete_tree (node);
7906 }
7907 return NULL_TREE;
7908}
7909
7910/* Construct the expression that calls an access method:
7911 <type>.access$<n>(<arg1> [, <arg2>]);
7912
7913 ARG2 can be NULL and will be omitted in that case. It will denote a
7914 read access. */
7915
7916static tree
7917build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7918 int lc;
7919 tree type, access_method_name, arg1, arg2;
7920{
7921 tree args, cn, access;
7922
7923 args = arg1 ? arg1 :
7924 build_wfl_node (build_current_thisn (current_class));
7925 args = build_tree_list (NULL_TREE, args);
7926
7927 if (arg2)
7928 args = tree_cons (NULL_TREE, arg2, args);
7929
7930 access = build_method_invocation (build_wfl_node (access_method_name), args);
7931 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7932 return make_qualified_primary (cn, access, lc);
7933}
7934
7935static tree
7936build_new_access_id ()
7937{
7938 static int access_n_counter = 1;
7939 char buffer [128];
7940
7941 sprintf (buffer, "access$%d", access_n_counter++);
7942 return get_identifier (buffer);
7943}
7944
7945/* Create the static access functions for the outer field DECL. We define a
7946 read:
7947 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7948 return inst$.field;
7949 }
7950 and a write access:
7951 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7952 TREE_TYPE (<field>) value$) {
7953 return inst$.field = value$;
7954 }
7955 We should have a usage flags on the DECL so we can lazily turn the ones
7956 we're using for code generation. FIXME.
7957*/
7958
7959static tree
7960build_outer_field_access_methods (decl)
7961 tree decl;
7962{
7963 tree id, args, stmt, mdecl;
7964
c7303e41 7965 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
7966 return FIELD_INNER_ACCESS (decl);
7967
c7303e41
APB
7968 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7969
c2952b01
APB
7970 /* Create the identifier and a function named after it. */
7971 id = build_new_access_id ();
7972
7973 /* The identifier is marked as bearing the name of a generated write
7974 access function for outer field accessed from inner classes. */
7975 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7976
7977 /* Create the read access */
7978 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7979 TREE_CHAIN (args) = end_params_node;
7980 stmt = make_qualified_primary (build_wfl_node (inst_id),
7981 build_wfl_node (DECL_NAME (decl)), 0);
7982 stmt = build_return (0, stmt);
7983 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7984 TREE_TYPE (decl), id, args, stmt);
7985 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7986
c7303e41
APB
7987 /* Create the write access method. No write access for final variable */
7988 if (!FIELD_FINAL (decl))
7989 {
7990 args = build_tree_list (inst_id,
7991 build_pointer_type (DECL_CONTEXT (decl)));
7992 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7993 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7994 stmt = make_qualified_primary (build_wfl_node (inst_id),
7995 build_wfl_node (DECL_NAME (decl)), 0);
7996 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7997 build_wfl_node (wpv_id)));
7998 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7999 TREE_TYPE (decl), id,
8000 args, stmt);
8001 }
c2952b01 8002 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8003
8004 /* Return the access name */
8005 return FIELD_INNER_ACCESS (decl) = id;
8006}
8007
8008/* Build an field access method NAME. */
8009
8010static tree
8011build_outer_field_access_method (class, type, name, args, body)
8012 tree class, type, name, args, body;
8013{
8014 tree saved_current_function_decl, mdecl;
8015
8016 /* Create the method */
8017 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8018 fix_method_argument_names (args, mdecl);
8019 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8020
8021 /* Attach the method body. */
8022 saved_current_function_decl = current_function_decl;
8023 start_artificial_method_body (mdecl);
8024 java_method_add_stmt (mdecl, body);
8025 end_artificial_method_body (mdecl);
8026 current_function_decl = saved_current_function_decl;
8027
8028 return mdecl;
8029}
8030
8031\f
8032/* This section deals with building access function necessary for
8033 certain kinds of method invocation from inner classes. */
8034
8035static tree
8036build_outer_method_access_method (decl)
8037 tree decl;
8038{
8039 tree saved_current_function_decl, mdecl;
8040 tree args = NULL_TREE, call_args = NULL_TREE;
8041 tree carg, id, body, class;
8042 char buffer [80];
8043 int parm_id_count = 0;
8044
8045 /* Test this abort with an access to a private field */
8046 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8047 abort ();
8048
8049 /* Check the cache first */
8050 if (DECL_FUNCTION_INNER_ACCESS (decl))
8051 return DECL_FUNCTION_INNER_ACCESS (decl);
8052
8053 class = DECL_CONTEXT (decl);
8054
8055 /* Obtain an access identifier and mark it */
8056 id = build_new_access_id ();
8057 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8058
c2952b01
APB
8059 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8060 /* Create the arguments, as much as the original */
8061 for (; carg && carg != end_params_node;
8062 carg = TREE_CHAIN (carg))
8063 {
8064 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8065 args = chainon (args, build_tree_list (get_identifier (buffer),
8066 TREE_VALUE (carg)));
8067 }
8068 args = chainon (args, end_params_node);
8069
8070 /* Create the method */
8071 mdecl = create_artificial_method (class, ACC_STATIC,
8072 TREE_TYPE (TREE_TYPE (decl)), id, args);
8073 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8074 /* There is a potential bug here. We should be able to use
8075 fix_method_argument_names, but then arg names get mixed up and
8076 eventually a constructor will have its this$0 altered and the
8077 outer context won't be assignment properly. The test case is
8078 stub.java FIXME */
8079 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8080
8081 /* Attach the method body. */
8082 saved_current_function_decl = current_function_decl;
8083 start_artificial_method_body (mdecl);
8084
8085 /* The actual method invocation uses the same args. When invoking a
8086 static methods that way, we don't want to skip the first
8087 argument. */
8088 carg = args;
8089 if (!METHOD_STATIC (decl))
8090 carg = TREE_CHAIN (carg);
8091 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8092 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8093 call_args);
8094
8095 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8096 call_args);
8097 if (!METHOD_STATIC (decl))
8098 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8099 body, 0);
8100 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8101 body = build_return (0, body);
8102 java_method_add_stmt (mdecl,body);
8103 end_artificial_method_body (mdecl);
8104 current_function_decl = saved_current_function_decl;
c2952b01
APB
8105
8106 /* Back tag the access function so it know what it accesses */
8107 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8108
8109 /* Tag the current method so it knows it has an access generated */
8110 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8111}
8112
8113\f
8114/* This section of the code deals with building expressions to access
8115 the enclosing instance of an inner class. The enclosing instance is
8116 kept in a generated field called this$<n>, with <n> being the
8117 inner class nesting level (starting from 0.) */
8118
dba41d30
APB
8119/* Build an access to a given this$<n>, always chaining access call to
8120 others. Access methods to this$<n> are build on the fly if
8121 necessary. This CAN'T be used to solely access this$<n-1> from
8122 this$<n> (which alway yield to special cases and optimization, see
8123 for example build_outer_field_access). */
c2952b01
APB
8124
8125static tree
8126build_access_to_thisn (from, to, lc)
8127 tree from, to;
8128 int lc;
8129{
8130 tree access = NULL_TREE;
8131
8132 while (from != to)
8133 {
c2952b01 8134 if (!access)
dba41d30
APB
8135 {
8136 access = build_current_thisn (from);
8137 access = build_wfl_node (access);
8138 }
8139 else
c2952b01 8140 {
dba41d30
APB
8141 tree access0_wfl, cn;
8142
8143 maybe_build_thisn_access_method (from);
8144 access0_wfl = build_wfl_node (access0_identifier_node);
8145 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8146 EXPR_WFL_LINECOL (access0_wfl) = lc;
8147 access = build_tree_list (NULL_TREE, access);
8148 access = build_method_invocation (access0_wfl, access);
8149 access = make_qualified_primary (cn, access, lc);
c2952b01 8150 }
5e18f6d6
APB
8151
8152 /* if FROM isn't an inter class, that's fine, we've done
8153 enough. What we're looking for can be accessed from there. */
8154 from = DECL_CONTEXT (TYPE_NAME (from));
8155 if (!from)
8156 break;
8157 from = TREE_TYPE (from);
c2952b01
APB
8158 }
8159 return access;
8160}
8161
8162/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8163 is returned if nothing needs to be generated. Otherwise, the method
152de068 8164 generated and a method decl is returned.
c2952b01
APB
8165
8166 NOTE: These generated methods should be declared in a class file
8167 attribute so that they can't be referred to directly. */
8168
8169static tree
8170maybe_build_thisn_access_method (type)
8171 tree type;
8172{
8173 tree mdecl, args, stmt, rtype;
8174 tree saved_current_function_decl;
8175
8176 /* If TYPE is a top-level class, no access method is required.
8177 If there already is such an access method, bail out. */
ee5f86dc 8178 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8179 return NULL_TREE;
8180
8181 /* We generate the method. The method looks like:
8182 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8183 */
c2952b01
APB
8184 args = build_tree_list (inst_id, build_pointer_type (type));
8185 TREE_CHAIN (args) = end_params_node;
8186 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8187 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8188 access0_identifier_node, args);
8189 fix_method_argument_names (args, mdecl);
8190 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8191 stmt = build_current_thisn (type);
8192 stmt = make_qualified_primary (build_wfl_node (inst_id),
8193 build_wfl_node (stmt), 0);
8194 stmt = build_return (0, stmt);
8195
8196 saved_current_function_decl = current_function_decl;
8197 start_artificial_method_body (mdecl);
8198 java_method_add_stmt (mdecl, stmt);
8199 end_artificial_method_body (mdecl);
8200 current_function_decl = saved_current_function_decl;
c2952b01
APB
8201
8202 CLASS_ACCESS0_GENERATED_P (type) = 1;
8203
8204 return mdecl;
8205}
8206
8207/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8208 the first level of innerclassing. this$1 for the next one, etc...
8209 This function can be invoked with TYPE to NULL, available and then
8210 has to count the parser context. */
8211
8212static tree
8213build_current_thisn (type)
8214 tree type;
8215{
8216 static int saved_i = -1;
8217 static tree saved_thisn = NULL_TREE;
19e223db
MM
8218 static tree saved_type = NULL_TREE;
8219 static int saved_type_i = 0;
8220 static int initialized_p;
c2952b01
APB
8221 tree decl;
8222 char buffer [80];
8223 int i = 0;
8224
19e223db
MM
8225 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8226 if (!initialized_p)
c2952b01 8227 {
19e223db
MM
8228 ggc_add_tree_root (&saved_thisn, 1);
8229 ggc_add_tree_root (&saved_type, 1);
8230 initialized_p = 1;
8231 }
c2952b01 8232
19e223db
MM
8233 if (type)
8234 {
c2952b01
APB
8235 if (type == saved_type)
8236 i = saved_type_i;
8237 else
8238 {
8239 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8240 decl; decl = DECL_CONTEXT (decl), i++)
8241 ;
8242
8243 saved_type = type;
8244 saved_type_i = i;
8245 }
8246 }
8247 else
8248 i = list_length (GET_CPC_LIST ())-2;
8249
8250 if (i == saved_i)
8251 return saved_thisn;
8252
8253 sprintf (buffer, "this$%d", i);
8254 saved_i = i;
8255 saved_thisn = get_identifier (buffer);
8256 return saved_thisn;
8257}
8258
8259/* Return the assignement to the hidden enclosing context `this$<n>'
8260 by the second incoming parameter to the innerclass constructor. The
8261 form used is `this.this$<n> = this$<n>;'. */
8262
8263static tree
8264build_thisn_assign ()
8265{
8266 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8267 {
8268 tree thisn = build_current_thisn (current_class);
8269 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8270 build_wfl_node (thisn), 0);
8271 tree rhs = build_wfl_node (thisn);
8272 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8273 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8274 }
8275 return NULL_TREE;
8276}
8277
8278\f
165f37bc
APB
8279/* Building the synthetic `class$' used to implement the `.class' 1.1
8280 extension for non primitive types. This method looks like:
8281
8282 static Class class$(String type) throws NoClassDefFoundError
8283 {
8284 try {return (java.lang.Class.forName (String));}
8285 catch (ClassNotFoundException e) {
8286 throw new NoClassDefFoundError(e.getMessage());}
8287 } */
8288
8289static tree
8290build_dot_class_method (class)
8291 tree class;
8292{
8293#define BWF(S) build_wfl_node (get_identifier ((S)))
8294#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8295 tree args, tmp, saved_current_function_decl, mdecl;
8296 tree stmt, throw_stmt, catch, catch_block, try_block;
8297 tree catch_clause_param;
8298 tree class_not_found_exception, no_class_def_found_error;
8299
8300 static tree get_message_wfl, type_parm_wfl;
8301
8302 if (!get_message_wfl)
8303 {
8304 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8305 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8306 ggc_add_tree_root (&get_message_wfl, 1);
8307 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8308 }
8309
8310 /* Build the arguments */
8311 args = build_tree_list (get_identifier ("type$"),
8312 build_pointer_type (string_type_node));
8313 TREE_CHAIN (args) = end_params_node;
8314
8315 /* Build the qualified name java.lang.Class.forName */
8316 tmp = MQN (MQN (MQN (BWF ("java"),
8317 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8318
8319 /* For things we have to catch and throw */
8320 class_not_found_exception =
8321 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8322 no_class_def_found_error =
8323 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8324 load_class (class_not_found_exception, 1);
8325 load_class (no_class_def_found_error, 1);
8326
8327 /* Create the "class$" function */
8328 mdecl = create_artificial_method (class, ACC_STATIC,
8329 build_pointer_type (class_type_node),
8330 get_identifier ("class$"), args);
8331 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8332 no_class_def_found_error);
8333
8334 /* We start by building the try block. We need to build:
8335 return (java.lang.Class.forName (type)); */
8336 stmt = build_method_invocation (tmp,
8337 build_tree_list (NULL_TREE, type_parm_wfl));
8338 stmt = build_return (0, stmt);
8339 /* Put it in a block. That's the try block */
8340 try_block = build_expr_block (stmt, NULL_TREE);
8341
8342 /* Now onto the catch block. We start by building the expression
8343 throwing a new exception:
8344 throw new NoClassDefFoundError (_.getMessage); */
8345 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8346 get_message_wfl, 0);
8347 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8348
8349 /* Build new NoClassDefFoundError (_.getMessage) */
8350 throw_stmt = build_new_invocation
8351 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8352 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8353
8354 /* Build the throw, (it's too early to use BUILD_THROW) */
8355 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8356
8357 /* Build the catch block to encapsulate all this. We begin by
8358 building an decl for the catch clause parameter and link it to
8359 newly created block, the catch block. */
8360 catch_clause_param =
8361 build_decl (VAR_DECL, wpv_id,
8362 build_pointer_type (class_not_found_exception));
8363 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8364
8365 /* We initialize the variable with the exception handler. */
8366 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8367 soft_exceptioninfo_call_node);
8368 add_stmt_to_block (catch_block, NULL_TREE, catch);
8369
8370 /* We add the statement throwing the new exception */
8371 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8372
8373 /* Build a catch expression for all this */
8374 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8375
8376 /* Build the try/catch sequence */
8377 stmt = build_try_statement (0, try_block, catch_block);
8378
8379 fix_method_argument_names (args, mdecl);
8380 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8381 saved_current_function_decl = current_function_decl;
8382 start_artificial_method_body (mdecl);
8383 java_method_add_stmt (mdecl, stmt);
8384 end_artificial_method_body (mdecl);
8385 current_function_decl = saved_current_function_decl;
8386 TYPE_DOT_CLASS (class) = mdecl;
8387
8388 return mdecl;
8389}
8390
8391static tree
f0f3a777
APB
8392build_dot_class_method_invocation (type)
8393 tree type;
165f37bc 8394{
f0f3a777
APB
8395 tree sig_id, s;
8396
8397 if (TYPE_ARRAY_P (type))
8398 sig_id = build_java_signature (type);
8399 else
8400 sig_id = DECL_NAME (TYPE_NAME (type));
8401
1f8f4a0b
MM
8402 s = build_string (IDENTIFIER_LENGTH (sig_id),
8403 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8404 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8405 build_tree_list (NULL_TREE, s));
8406}
8407
c2952b01
APB
8408/* This section of the code deals with constructor. */
8409
22eed1e6
APB
8410/* Craft a body for default constructor. Patch existing constructor
8411 bodies with call to super() and field initialization statements if
8412 necessary. */
8413
8414static void
8415fix_constructors (mdecl)
8416 tree mdecl;
8417{
8418 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8419 tree thisn_assign, compound = NULL_TREE;
8420 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8421
22eed1e6
APB
8422 if (!body)
8423 {
22eed1e6
APB
8424 /* It is an error for the compiler to generate a default
8425 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8426 takes no argument, or the same args for an anonymous class */
8427 if (verify_constructor_super (mdecl))
22eed1e6 8428 {
c2952b01
APB
8429 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8430 tree save = DECL_NAME (mdecl);
49f48c71 8431 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8432 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8433 parse_error_context
c2952b01
APB
8434 (lookup_cl (TYPE_NAME (class_type)),
8435 "No constructor matching `%s' found in class `%s'",
8436 lang_printable_name (mdecl, 0), n);
8437 DECL_NAME (mdecl) = save;
22eed1e6
APB
8438 }
8439
c2952b01
APB
8440 /* The constructor body must be crafted by hand. It's the
8441 constructor we defined when we realize we didn't have the
8442 CLASSNAME() constructor */
22eed1e6
APB
8443 start_artificial_method_body (mdecl);
8444
f0f3a777
APB
8445 /* Insert an assignment to the this$<n> hidden field, if
8446 necessary */
8447 if ((thisn_assign = build_thisn_assign ()))
8448 java_method_add_stmt (mdecl, thisn_assign);
8449
22eed1e6
APB
8450 /* We don't generate a super constructor invocation if we're
8451 compiling java.lang.Object. build_super_invocation takes care
8452 of that. */
e920ebc9 8453 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8454
c2952b01
APB
8455 /* Insert the instance initializer block right here, after the
8456 super invocation. */
8457 add_instance_initializer (mdecl);
8458
22eed1e6
APB
8459 end_artificial_method_body (mdecl);
8460 }
8461 /* Search for an explicit constructor invocation */
8462 else
8463 {
8464 int found = 0;
8465 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8466
8467 while (body)
8468 switch (TREE_CODE (body))
8469 {
8470 case CALL_EXPR:
8471 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8472 body = NULL_TREE;
8473 break;
8474 case COMPOUND_EXPR:
8475 case EXPR_WITH_FILE_LOCATION:
8476 body = TREE_OPERAND (body, 0);
8477 break;
8478 case BLOCK:
8479 body = BLOCK_EXPR_BODY (body);
8480 break;
8481 default:
8482 found = 0;
8483 body = NULL_TREE;
8484 }
8485 /* The constructor is missing an invocation of super() */
8486 if (!found)
8487 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8488 build_super_invocation (mdecl));
22eed1e6 8489
c2952b01
APB
8490 /* Generate the assignment to this$<n>, if necessary */
8491 if ((thisn_assign = build_thisn_assign ()))
8492 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8493
f0f3a777
APB
8494 /* Insert the instance initializer block right here, after the
8495 super invocation. */
8496 add_instance_initializer (mdecl);
8497
22eed1e6
APB
8498 /* Fix the constructor main block if we're adding extra stmts */
8499 if (compound)
8500 {
8501 compound = add_stmt_to_compound (compound, NULL_TREE,
8502 BLOCK_EXPR_BODY (main_block));
8503 BLOCK_EXPR_BODY (main_block) = compound;
8504 }
8505 }
8506}
8507
8508/* Browse constructors in the super class, searching for a constructor
8509 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8510 otherwise. If the current class is an anonymous inner class, look
8511 for something that has the same signature. */
22eed1e6
APB
8512
8513static int
c2952b01
APB
8514verify_constructor_super (mdecl)
8515 tree mdecl;
22eed1e6
APB
8516{
8517 tree class = CLASSTYPE_SUPER (current_class);
152de068 8518 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8519 tree sdecl;
8520
22eed1e6
APB
8521 if (!class)
8522 return 0;
8523
c2952b01 8524 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8525 {
c2952b01
APB
8526 tree mdecl_arg_type;
8527 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8528 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8529 if (DECL_CONSTRUCTOR_P (sdecl))
8530 {
cf1b2274 8531 tree m_arg_type;
152de068
APB
8532 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8533 if (super_inner)
8534 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8535 for (m_arg_type = mdecl_arg_type;
8536 (arg_type != end_params_node
8537 && m_arg_type != end_params_node);
c2952b01 8538 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8539 m_arg_type = TREE_CHAIN (m_arg_type))
8540 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8541 break;
8542
cf1b2274 8543 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8544 return 0;
8545 }
8546 }
8547 else
8548 {
8549 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8550 {
152de068
APB
8551 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8552 if (super_inner)
8553 arg = TREE_CHAIN (arg);
8554 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8555 return 0;
8556 }
8557 }
8558 return 1;
8559}
8560
22eed1e6 8561/* Generate code for all context remembered for code generation. */
b351b287
APB
8562
8563void
8564java_expand_classes ()
8565{
5423609c 8566 int save_error_count = 0;
c2952b01
APB
8567 static struct parser_ctxt *saved_ctxp = NULL;
8568
23a79c61
APB
8569 java_parse_abort_on_error ();
8570 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8571 return;
8572 java_layout_classes ();
8573 java_parse_abort_on_error ();
8574
c2952b01 8575 saved_ctxp = ctxp_for_generation;
b351b287
APB
8576 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8577 {
8578 ctxp = ctxp_for_generation;
8579 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8580 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8581 java_parse_abort_on_error ();
8582 }
c2952b01
APB
8583
8584 /* Find anonymous classes and expand their constructor, now they
8585 have been fixed. */
8586 for (ctxp_for_generation = saved_ctxp;
8587 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8588 {
8589 tree current;
8590 ctxp = ctxp_for_generation;
8591 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8592 {
8593 current_class = TREE_TYPE (current);
8594 if (ANONYMOUS_CLASS_P (current_class))
8595 {
8596 tree d;
8597 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8598 {
8599 if (DECL_CONSTRUCTOR_P (d))
8600 {
8601 restore_line_number_status (1);
c2952b01
APB
8602 java_complete_expand_method (d);
8603 restore_line_number_status (0);
8604 break; /* We now there are no other ones */
8605 }
8606 }
8607 }
8608 }
8609 }
8610
8611 /* If we've found error at that stage, don't try to generate
8612 anything, unless we're emitting xrefs or checking the syntax only
8613 (but not using -fsyntax-only for the purpose of generating
8614 bytecode. */
8615 if (java_error_count && !flag_emit_xref
8616 && (!flag_syntax_only && !flag_emit_class_files))
8617 return;
8618
8619 /* Now things are stable, go for generation of the class data. */
8620 for (ctxp_for_generation = saved_ctxp;
8621 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8622 {
8623 tree current;
8624 ctxp = ctxp_for_generation;
8625 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8626 {
8627 current_class = TREE_TYPE (current);
8628 outgoing_cpool = TYPE_CPOOL (current_class);
8629 if (flag_emit_class_files)
8630 write_classfile (current_class);
8631 if (flag_emit_xref)
8632 expand_xref (current_class);
8633 else if (! flag_syntax_only)
8634 finish_class ();
8635 }
8636 }
b351b287
APB
8637}
8638
e04a16fb
AG
8639/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8640 a tree list node containing RIGHT. Fore coming RIGHTs will be
8641 chained to this hook. LOCATION contains the location of the
8642 separating `.' operator. */
8643
8644static tree
8645make_qualified_primary (primary, right, location)
8646 tree primary, right;
8647 int location;
8648{
8649 tree wfl;
8650
c2952b01 8651 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8652 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8653 else
8654 {
8655 wfl = primary;
c2952b01
APB
8656 /* If wfl wasn't qualified, we build a first anchor */
8657 if (!EXPR_WFL_QUALIFICATION (wfl))
8658 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8659 }
8660
c2952b01 8661 /* And chain them */
e04a16fb
AG
8662 EXPR_WFL_LINECOL (right) = location;
8663 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8664 PRIMARY_P (wfl) = 1;
8665 return wfl;
8666}
8667
8668/* Simple merge of two name separated by a `.' */
8669
8670static tree
8671merge_qualified_name (left, right)
8672 tree left, right;
8673{
8674 tree node;
c2952b01
APB
8675 if (!left && !right)
8676 return NULL_TREE;
8677
8678 if (!left)
8679 return right;
8680
8681 if (!right)
8682 return left;
8683
e04a16fb
AG
8684 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8685 IDENTIFIER_LENGTH (left));
8686 obstack_1grow (&temporary_obstack, '.');
8687 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8688 IDENTIFIER_LENGTH (right));
8689 node = get_identifier (obstack_base (&temporary_obstack));
8690 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8691 QUALIFIED_P (node) = 1;
8692 return node;
8693}
8694
8695/* Merge the two parts of a qualified name into LEFT. Set the
8696 location information of the resulting node to LOCATION, usually
8697 inherited from the location information of the `.' operator. */
8698
8699static tree
8700make_qualified_name (left, right, location)
8701 tree left, right;
8702 int location;
8703{
bc3ca41b
PB
8704#ifdef USE_COMPONENT_REF
8705 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8706 EXPR_WFL_LINECOL (node) = location;
8707 return node;
8708#else
e04a16fb
AG
8709 tree left_id = EXPR_WFL_NODE (left);
8710 tree right_id = EXPR_WFL_NODE (right);
8711 tree wfl, merge;
8712
8713 merge = merge_qualified_name (left_id, right_id);
8714
8715 /* Left wasn't qualified and is now qualified */
8716 if (!QUALIFIED_P (left_id))
8717 {
8718 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8719 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8720 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8721 }
8722
8723 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8724 EXPR_WFL_LINECOL (wfl) = location;
8725 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8726
8727 EXPR_WFL_NODE (left) = merge;
8728 return left;
bc3ca41b 8729#endif
e04a16fb
AG
8730}
8731
8732/* Extract the last identifier component of the qualified in WFL. The
8733 last identifier is removed from the linked list */
8734
8735static tree
8736cut_identifier_in_qualified (wfl)
8737 tree wfl;
8738{
8739 tree q;
8740 tree previous = NULL_TREE;
8741 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8742 if (!TREE_CHAIN (q))
8743 {
8744 if (!previous)
400500c4
RK
8745 /* Operating on a non qualified qualified WFL. */
8746 abort ();
8747
e04a16fb
AG
8748 TREE_CHAIN (previous) = NULL_TREE;
8749 return TREE_PURPOSE (q);
8750 }
8751}
8752
8753/* Resolve the expression name NAME. Return its decl. */
8754
8755static tree
5e942c50 8756resolve_expression_name (id, orig)
e04a16fb 8757 tree id;
5e942c50 8758 tree *orig;
e04a16fb
AG
8759{
8760 tree name = EXPR_WFL_NODE (id);
8761 tree decl;
8762
8763 /* 6.5.5.1: Simple expression names */
8764 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8765 {
8766 /* 15.13.1: NAME can appear within the scope of a local variable
8767 declaration */
8768 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8769 return decl;
8770
8771 /* 15.13.1: NAME can appear within a class declaration */
8772 else
8773 {
8774 decl = lookup_field_wrapper (current_class, name);
8775 if (decl)
8776 {
c2952b01 8777 tree access = NULL_TREE;
e04a16fb 8778 int fs = FIELD_STATIC (decl);
f2760b27
APB
8779
8780 /* If we're accessing an outer scope local alias, make
8781 sure we change the name of the field we're going to
8782 build access to. */
8783 if (FIELD_LOCAL_ALIAS_USED (decl))
8784 name = DECL_NAME (decl);
8785
e04a16fb
AG
8786 /* Instance variable (8.3.1.1) can't appear within
8787 static method, static initializer or initializer for
8788 a static variable. */
8789 if (!fs && METHOD_STATIC (current_function_decl))
8790 {
7f10c2e2 8791 static_ref_err (id, name, current_class);
e04a16fb
AG
8792 return error_mark_node;
8793 }
22eed1e6
APB
8794 /* Instance variables can't appear as an argument of
8795 an explicit constructor invocation */
7e1376a1
BM
8796 if (!fs && ctxp->explicit_constructor_p
8797 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
22eed1e6
APB
8798 {
8799 parse_error_context
781b0558 8800 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8801 return error_mark_node;
8802 }
5e942c50 8803
c2952b01
APB
8804 /* If we're processing an inner class and we're trying
8805 to access a field belonging to an outer class, build
8806 the access to the field */
8807 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8808 {
8809 if (CLASS_STATIC (TYPE_NAME (current_class)))
8810 {
8811 static_ref_err (id, DECL_NAME (decl), current_class);
8812 return error_mark_node;
8813 }
8814 return build_outer_field_access (id, decl);
8815 }
c2952b01 8816
5e942c50 8817 /* Otherwise build what it takes to access the field */
c2952b01
APB
8818 access = build_field_ref ((fs ? NULL_TREE : current_this),
8819 DECL_CONTEXT (decl), name);
e8fc7396 8820 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8821 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8822 /* We may be asked to save the real field access node */
8823 if (orig)
c2952b01 8824 *orig = access;
5e942c50 8825 /* And we return what we got */
c2952b01 8826 return access;
e04a16fb
AG
8827 }
8828 /* Fall down to error report on undefined variable */
8829 }
8830 }
8831 /* 6.5.5.2 Qualified Expression Names */
8832 else
8833 {
5e942c50
APB
8834 if (orig)
8835 *orig = NULL_TREE;
e04a16fb
AG
8836 qualify_ambiguous_name (id);
8837 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8838 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8839 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8840 }
8841
8842 /* We've got an error here */
8843 parse_error_context (id, "Undefined variable `%s'",
8844 IDENTIFIER_POINTER (name));
8845
8846 return error_mark_node;
8847}
8848
7f10c2e2
APB
8849static void
8850static_ref_err (wfl, field_id, class_type)
8851 tree wfl, field_id, class_type;
8852{
8853 parse_error_context
8854 (wfl,
8855 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8856 IDENTIFIER_POINTER (field_id),
8857 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8858}
8859
e04a16fb
AG
8860/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8861 We return something suitable to generate the field access. We also
8862 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8863 recipient's address can be null. */
8864
8865static tree
8866resolve_field_access (qual_wfl, field_decl, field_type)
8867 tree qual_wfl;
8868 tree *field_decl, *field_type;
8869{
8870 int is_static = 0;
8871 tree field_ref;
8872 tree decl, where_found, type_found;
8873
8874 if (resolve_qualified_expression_name (qual_wfl, &decl,
8875 &where_found, &type_found))
8876 return error_mark_node;
8877
8878 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8879 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8880 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8881 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8882 {
8883 tree length = build_java_array_length_access (where_found);
8884 field_ref =
8885 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8886
8887 /* In case we're dealing with a static array, we need to
8888 initialize its class before the array length can be fetched.
8889 It's also a good time to create a DECL_RTL for the field if
8890 none already exists, otherwise if the field was declared in a
8891 class found in an external file and hasn't been (and won't
8892 be) accessed for its value, none will be created. */
8893 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8894 {
8895 build_static_field_ref (where_found);
8896 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8897 }
e04a16fb
AG
8898 }
8899 /* We might have been trying to resolve field.method(). In which
8900 case, the resolution is over and decl is the answer */
34f4db93 8901 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8902 field_ref = decl;
34f4db93 8903 else if (JDECL_P (decl))
e04a16fb 8904 {
5e942c50
APB
8905 int static_final_found = 0;
8906 if (!type_found)
8907 type_found = DECL_CONTEXT (decl);
34f4db93 8908 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 8909 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 8910 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8911 && DECL_INITIAL (decl))
5e942c50 8912 {
0c2b8145
APB
8913 /* When called on a FIELD_DECL of the right (primitive)
8914 type, java_complete_tree will try to substitue the decl
8915 for it's initial value. */
70541f45 8916 field_ref = java_complete_tree (decl);
5e942c50
APB
8917 static_final_found = 1;
8918 }
8919 else
7f10c2e2
APB
8920 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8921 NULL_TREE : where_found),
5e942c50 8922 type_found, DECL_NAME (decl));
e04a16fb
AG
8923 if (field_ref == error_mark_node)
8924 return error_mark_node;
e8fc7396
APB
8925 if (is_static && !static_final_found
8926 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8927 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8928 }
8929 else
8930 field_ref = decl;
8931
8932 if (field_decl)
8933 *field_decl = decl;
8934 if (field_type)
c877974e
APB
8935 *field_type = (QUAL_DECL_TYPE (decl) ?
8936 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8937 return field_ref;
8938}
8939
e28cd97b
APB
8940/* If NODE is an access to f static field, strip out the class
8941 initialization part and return the field decl, otherwise, return
8942 NODE. */
8943
8944static tree
8945strip_out_static_field_access_decl (node)
8946 tree node;
8947{
8948 if (TREE_CODE (node) == COMPOUND_EXPR)
8949 {
8950 tree op1 = TREE_OPERAND (node, 1);
8951 if (TREE_CODE (op1) == COMPOUND_EXPR)
8952 {
8953 tree call = TREE_OPERAND (op1, 0);
8954 if (TREE_CODE (call) == CALL_EXPR
8955 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8956 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8957 == soft_initclass_node)
8958 return TREE_OPERAND (op1, 1);
8959 }
2f11d407
TT
8960 else if (JDECL_P (op1))
8961 return op1;
e28cd97b
APB
8962 }
8963 return node;
8964}
8965
e04a16fb
AG
8966/* 6.5.5.2: Qualified Expression Names */
8967
8968static int
8969resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8970 tree wfl;
8971 tree *found_decl, *type_found, *where_found;
8972{
8973 int from_type = 0; /* Field search initiated from a type */
c2952b01 8974 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8975 int previous_call_static = 0;
8976 int is_static;
8977 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8978 /* For certain for of inner class instantiation */
8979 tree saved_current, saved_this;
8980#define RESTORE_THIS_AND_CURRENT_CLASS \
8981 { current_class = saved_current; current_this = saved_this;}
8982
c877974e 8983 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8984
8985 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8986 {
8987 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8988 tree ret_decl; /* for EH checking */
8989 int location; /* for EH checking */
e04a16fb
AG
8990
8991 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8992 switch (TREE_CODE (qual_wfl))
8993 {
8994 case CALL_EXPR:
b67d701b 8995 case NEW_CLASS_EXPR:
e04a16fb
AG
8996 /* If the access to the function call is a non static field,
8997 build the code to access it. */
34f4db93 8998 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8999 {
ac825856
APB
9000 decl = maybe_access_field (decl, *where_found,
9001 DECL_CONTEXT (decl));
e04a16fb
AG
9002 if (decl == error_mark_node)
9003 return 1;
9004 }
c2952b01 9005
e04a16fb
AG
9006 /* And code for the function call */
9007 if (complete_function_arguments (qual_wfl))
9008 return 1;
c2952b01
APB
9009
9010 /* We might have to setup a new current class and a new this
9011 for the search of an inner class, relative to the type of
9012 a expression resolved as `decl'. The current values are
9013 saved and restored shortly after */
9014 saved_current = current_class;
9015 saved_this = current_this;
dba41d30
APB
9016 if (decl
9017 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9018 || from_qualified_this))
c2952b01 9019 {
dba41d30
APB
9020 /* If we still have `from_qualified_this', we have the form
9021 <T>.this.f() and we need to build <T>.this */
9022 if (from_qualified_this)
9023 {
9024 decl = build_access_to_thisn (current_class, type, 0);
9025 decl = java_complete_tree (decl);
9026 type = TREE_TYPE (TREE_TYPE (decl));
9027 }
c2952b01
APB
9028 current_class = type;
9029 current_this = decl;
dba41d30 9030 from_qualified_this = 0;
c2952b01
APB
9031 }
9032
89e09b9a
PB
9033 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9034 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9035 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9036 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9037 *where_found = patch_method_invocation (qual_wfl, decl, type,
9038 &is_static, &ret_decl);
e04a16fb 9039 if (*where_found == error_mark_node)
c2952b01
APB
9040 {
9041 RESTORE_THIS_AND_CURRENT_CLASS;
9042 return 1;
9043 }
e04a16fb
AG
9044 *type_found = type = QUAL_DECL_TYPE (*where_found);
9045
c2952b01
APB
9046 /* If we're creating an inner class instance, check for that
9047 an enclosing instance is in scope */
9048 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9049 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9050 {
9051 parse_error_context
165f37bc
APB
9052 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9053 lang_printable_name (type, 0),
9054 (!current_this ? "" :
9055 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9056 RESTORE_THIS_AND_CURRENT_CLASS;
9057 return 1;
9058 }
9059
9060 /* In case we had to change then to resolve a inner class
9061 instantiation using a primary qualified by a `new' */
9062 RESTORE_THIS_AND_CURRENT_CLASS;
9063
34d4df06
APB
9064 /* EH check. No check on access$<n> functions */
9065 if (location
9066 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9067 (DECL_NAME (current_function_decl)))
7705e9db
APB
9068 check_thrown_exceptions (location, ret_decl);
9069
e04a16fb
AG
9070 /* If the previous call was static and this one is too,
9071 build a compound expression to hold the two (because in
9072 that case, previous function calls aren't transported as
9073 forcoming function's argument. */
9074 if (previous_call_static && is_static)
9075 {
9076 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9077 TREE_SIDE_EFFECTS (decl) = 1;
9078 }
9079 else
9080 {
9081 previous_call_static = is_static;
9082 decl = *where_found;
9083 }
c2952b01 9084 from_type = 0;
e04a16fb
AG
9085 continue;
9086
d8fccff5 9087 case NEW_ARRAY_EXPR:
c2952b01 9088 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9089 *where_found = decl = java_complete_tree (qual_wfl);
9090 if (decl == error_mark_node)
9091 return 1;
9092 *type_found = type = QUAL_DECL_TYPE (decl);
9093 CLASS_LOADED_P (type) = 1;
9094 continue;
9095
e04a16fb
AG
9096 case CONVERT_EXPR:
9097 *where_found = decl = java_complete_tree (qual_wfl);
9098 if (decl == error_mark_node)
9099 return 1;
9100 *type_found = type = QUAL_DECL_TYPE (decl);
9101 from_cast = 1;
9102 continue;
9103
22eed1e6 9104 case CONDITIONAL_EXPR:
5e942c50 9105 case STRING_CST:
ac22f9cb 9106 case MODIFY_EXPR:
22eed1e6
APB
9107 *where_found = decl = java_complete_tree (qual_wfl);
9108 if (decl == error_mark_node)
9109 return 1;
9110 *type_found = type = QUAL_DECL_TYPE (decl);
9111 continue;
9112
e04a16fb
AG
9113 case ARRAY_REF:
9114 /* If the access to the function call is a non static field,
9115 build the code to access it. */
34f4db93 9116 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9117 {
9118 decl = maybe_access_field (decl, *where_found, type);
9119 if (decl == error_mark_node)
9120 return 1;
9121 }
9122 /* And code for the array reference expression */
9123 decl = java_complete_tree (qual_wfl);
9124 if (decl == error_mark_node)
9125 return 1;
9126 type = QUAL_DECL_TYPE (decl);
9127 continue;
0a2138e2 9128
37feda7d
APB
9129 case PLUS_EXPR:
9130 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9131 return 1;
9132 if ((type = patch_string (decl)))
9133 decl = type;
9134 *where_found = QUAL_RESOLUTION (q) = decl;
9135 *type_found = type = TREE_TYPE (decl);
9136 break;
9137
165f37bc
APB
9138 case CLASS_LITERAL:
9139 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9140 return 1;
9141 *where_found = QUAL_RESOLUTION (q) = decl;
9142 *type_found = type = TREE_TYPE (decl);
9143 break;
9144
0a2138e2
APB
9145 default:
9146 /* Fix for -Wall Just go to the next statement. Don't
9147 continue */
a3f406ce 9148 break;
e04a16fb
AG
9149 }
9150
9151 /* If we fall here, we weren't processing a (static) function call. */
9152 previous_call_static = 0;
9153
9154 /* It can be the keyword THIS */
9155 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9156 {
9157 if (!current_this)
9158 {
9159 parse_error_context
9160 (wfl, "Keyword `this' used outside allowed context");
9161 return 1;
9162 }
7e1376a1
BM
9163 if (ctxp->explicit_constructor_p
9164 && type == current_class)
f63991a8 9165 {
781b0558 9166 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9167 return 1;
9168 }
e04a16fb 9169 /* We have to generate code for intermediate acess */
c2952b01
APB
9170 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9171 {
9172 *where_found = decl = current_this;
9173 *type_found = type = QUAL_DECL_TYPE (decl);
9174 }
4dbf4496
APB
9175 /* We're trying to access the this from somewhere else. Make sure
9176 it's allowed before doing so. */
c2952b01
APB
9177 else
9178 {
4dbf4496
APB
9179 if (!enclosing_context_p (type, current_class))
9180 {
9181 char *p = xstrdup (lang_printable_name (type, 0));
9182 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9183 p, p,
9184 lang_printable_name (current_class, 0));
9185 free (p);
9186 return 1;
9187 }
c2952b01 9188 from_qualified_this = 1;
dba41d30
APB
9189 /* If there's nothing else after that, we need to
9190 produce something now, otherwise, the section of the
9191 code that needs to produce <T>.this will generate
9192 what is necessary. */
9193 if (!TREE_CHAIN (q))
9194 {
9195 decl = build_access_to_thisn (current_class, type, 0);
9196 *where_found = decl = java_complete_tree (decl);
9197 *type_found = type = TREE_TYPE (decl);
9198 }
c2952b01
APB
9199 }
9200
9201 from_type = 0;
e04a16fb
AG
9202 continue;
9203 }
9204
9205 /* 15.10.2 Accessing Superclass Members using SUPER */
9206 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9207 {
9208 tree node;
9209 /* Check on the restricted use of SUPER */
9210 if (METHOD_STATIC (current_function_decl)
9211 || current_class == object_type_node)
9212 {
9213 parse_error_context
9214 (wfl, "Keyword `super' used outside allowed context");
9215 return 1;
9216 }
9217 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9218 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9219 CLASSTYPE_SUPER (current_class),
9220 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9221 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9222 if (decl == error_mark_node)
9223 return 1;
e04a16fb
AG
9224 *type_found = type = QUAL_DECL_TYPE (decl);
9225 from_super = from_type = 1;
9226 continue;
9227 }
9228
9229 /* 15.13.1: Can't search for field name in packages, so we
9230 assume a variable/class name was meant. */
9231 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9232 {
5e942c50
APB
9233 tree name = resolve_package (wfl, &q);
9234 if (name)
9235 {
c2952b01 9236 tree list;
5e942c50 9237 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9238 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9239 out. We're going to search something inside it. */
9240 *type_found = type = TREE_TYPE (decl);
9241 layout_class (type);
9242 from_type = 1;
c2952b01 9243
dde1da72
APB
9244 /* Fix them all the way down, if any are left. */
9245 if (q)
c2952b01 9246 {
dde1da72
APB
9247 list = TREE_CHAIN (q);
9248 while (list)
9249 {
9250 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9251 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9252 list = TREE_CHAIN (list);
9253 }
c2952b01 9254 }
5e942c50 9255 }
e04a16fb 9256 else
5e942c50
APB
9257 {
9258 if (from_super || from_cast)
9259 parse_error_context
9260 ((from_cast ? qual_wfl : wfl),
9261 "No variable `%s' defined in class `%s'",
9262 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9263 lang_printable_name (type, 0));
9264 else
9265 parse_error_context
9266 (qual_wfl, "Undefined variable or class name: `%s'",
9267 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9268 return 1;
9269 }
e04a16fb
AG
9270 }
9271
9272 /* We have a type name. It's been already resolved when the
9273 expression was qualified. */
9274 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9275 {
9276 if (!(decl = QUAL_RESOLUTION (q)))
9277 return 1; /* Error reported already */
9278
c2952b01
APB
9279 /* Sneak preview. If next we see a `new', we're facing a
9280 qualification with resulted in a type being selected
9281 instead of a field. Report the error */
9282 if(TREE_CHAIN (q)
9283 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9284 {
9285 parse_error_context (qual_wfl, "Undefined variable `%s'",
9286 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9287 return 1;
9288 }
9289
e04a16fb
AG
9290 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9291 {
9292 parse_error_context
9293 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9294 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9295 GET_TYPE_NAME (type),
e04a16fb
AG
9296 IDENTIFIER_POINTER (DECL_NAME (decl)),
9297 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9298 return 1;
9299 }
5e942c50 9300 check_deprecation (qual_wfl, decl);
c2952b01 9301
e04a16fb
AG
9302 type = TREE_TYPE (decl);
9303 from_type = 1;
9304 }
9305 /* We resolve and expression name */
9306 else
9307 {
cd531a2e 9308 tree field_decl = NULL_TREE;
e04a16fb
AG
9309
9310 /* If there exists an early resolution, use it. That occurs
9311 only once and we know that there are more things to
9312 come. Don't do that when processing something after SUPER
9313 (we need more thing to be put in place below */
9314 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9315 {
9316 decl = QUAL_RESOLUTION (q);
c877974e 9317 if (!type)
5e942c50 9318 {
7f10c2e2
APB
9319 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9320 {
9321 if (current_this)
9322 *where_found = current_this;
9323 else
9324 {
9325 static_ref_err (qual_wfl, DECL_NAME (decl),
9326 current_class);
9327 return 1;
9328 }
f0f3a777
APB
9329 if (outer_field_access_p (current_class, decl))
9330 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9331 }
c877974e
APB
9332 else
9333 {
9334 *where_found = TREE_TYPE (decl);
9335 if (TREE_CODE (*where_found) == POINTER_TYPE)
9336 *where_found = TREE_TYPE (*where_found);
9337 }
5e942c50 9338 }
b67d701b 9339 }
e04a16fb
AG
9340
9341 /* We have to search for a field, knowing the type of its
9342 container. The flag FROM_TYPE indicates that we resolved
9343 the last member of the expression as a type name, which
5e942c50
APB
9344 means that for the resolution of this field, we'll look
9345 for other errors than if it was resolved as a member of
9346 an other field. */
e04a16fb
AG
9347 else
9348 {
9349 int is_static;
5e942c50
APB
9350 tree field_decl_type; /* For layout */
9351
e04a16fb
AG
9352 if (!from_type && !JREFERENCE_TYPE_P (type))
9353 {
9354 parse_error_context
9355 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9356 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9357 lang_printable_name (type, 0),
e04a16fb
AG
9358 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9359 return 1;
9360 }
9361
dc0b3eff
PB
9362 field_decl = lookup_field_wrapper (type,
9363 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9364
9365 /* Maybe what we're trying to access an inner class. */
9366 if (!field_decl)
9367 {
9368 tree ptr, inner_decl;
9369
9370 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9371 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9372 if (inner_decl)
9373 {
9374 check_inner_class_access (inner_decl, decl, qual_wfl);
9375 type = TREE_TYPE (inner_decl);
9376 decl = inner_decl;
9377 from_type = 1;
9378 continue;
9379 }
9380 }
9381
dc0b3eff 9382 if (field_decl == NULL_TREE)
e04a16fb
AG
9383 {
9384 parse_error_context
2aa11e97 9385 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9386 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9387 GET_TYPE_NAME (type));
e04a16fb
AG
9388 return 1;
9389 }
dc0b3eff
PB
9390 if (field_decl == error_mark_node)
9391 return 1;
5e942c50
APB
9392
9393 /* Layout the type of field_decl, since we may need
c877974e
APB
9394 it. Don't do primitive types or loaded classes. The
9395 situation of non primitive arrays may not handled
9396 properly here. FIXME */
5e942c50
APB
9397 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9398 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9399 else
9400 field_decl_type = TREE_TYPE (field_decl);
9401 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9402 && !CLASS_LOADED_P (field_decl_type)
9403 && !TYPE_ARRAY_P (field_decl_type))
9404 resolve_and_layout (field_decl_type, NULL_TREE);
9405 if (TYPE_ARRAY_P (field_decl_type))
9406 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9407
9408 /* Check on accessibility here */
9409 if (not_accessible_p (type, field_decl, from_super))
9410 {
9411 parse_error_context
9412 (qual_wfl,
9413 "Can't access %s field `%s.%s' from `%s'",
9414 java_accstring_lookup
9415 (get_access_flags_from_decl (field_decl)),
2aa11e97 9416 GET_TYPE_NAME (type),
e04a16fb
AG
9417 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9418 IDENTIFIER_POINTER
9419 (DECL_NAME (TYPE_NAME (current_class))));
9420 return 1;
9421 }
5e942c50 9422 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9423
9424 /* There are things to check when fields are accessed
9425 from type. There are no restrictions on a static
9426 declaration of the field when it is accessed from an
9427 interface */
9428 is_static = FIELD_STATIC (field_decl);
9429 if (!from_super && from_type
c2952b01
APB
9430 && !TYPE_INTERFACE_P (type)
9431 && !is_static
9432 && (current_function_decl
9433 && METHOD_STATIC (current_function_decl)))
e04a16fb 9434 {
7f10c2e2 9435 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9436 return 1;
9437 }
9438 from_cast = from_super = 0;
9439
c2952b01
APB
9440 /* It's an access from a type but it isn't static, we
9441 make it relative to `this'. */
9442 if (!is_static && from_type)
9443 decl = current_this;
9444
5e942c50
APB
9445 /* If we need to generate something to get a proper
9446 handle on what this field is accessed from, do it
9447 now. */
e04a16fb
AG
9448 if (!is_static)
9449 {
c583dd46 9450 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9451 if (decl == error_mark_node)
9452 return 1;
9453 }
9454
9455 /* We want to keep the location were found it, and the type
9456 we found. */
9457 *where_found = decl;
9458 *type_found = type;
9459
c2952b01
APB
9460 /* Generate the correct expression for field access from
9461 qualified this */
9462 if (from_qualified_this)
9463 {
9464 field_decl = build_outer_field_access (qual_wfl, field_decl);
9465 from_qualified_this = 0;
9466 }
9467
e04a16fb
AG
9468 /* This is the decl found and eventually the next one to
9469 search from */
9470 decl = field_decl;
9471 }
e04a16fb
AG
9472 from_type = 0;
9473 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9474
9475 /* Sneak preview. If decl is qualified by a `new', report
9476 the error here to be accurate on the peculiar construct */
9477 if (TREE_CHAIN (q)
9478 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9479 && !JREFERENCE_TYPE_P (type))
9480 {
9481 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9482 lang_printable_name (type, 0));
9483 return 1;
9484 }
e04a16fb 9485 }
dde1da72
APB
9486 /* `q' might have changed due to a after package resolution
9487 re-qualification */
9488 if (!q)
9489 break;
e04a16fb
AG
9490 }
9491 *found_decl = decl;
9492 return 0;
9493}
9494
9495/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9496 can't be accessed from REFERENCE (a record type). This should be
9497 used when decl is a field or a method.*/
e04a16fb 9498
be245ac0
KG
9499static int
9500not_accessible_p (reference, member, from_super)
e04a16fb
AG
9501 tree reference, member;
9502 int from_super;
9503{
9504 int access_flag = get_access_flags_from_decl (member);
9505
4dbf4496
APB
9506 /* Inner classes are processed by check_inner_class_access */
9507 if (INNER_CLASS_TYPE_P (reference))
9508 return 0;
9509
e04a16fb
AG
9510 /* Access always granted for members declared public */
9511 if (access_flag & ACC_PUBLIC)
9512 return 0;
9513
9514 /* Check access on protected members */
9515 if (access_flag & ACC_PROTECTED)
9516 {
9517 /* Access granted if it occurs from within the package
9518 containing the class in which the protected member is
9519 declared */
9520 if (class_in_current_package (DECL_CONTEXT (member)))
9521 return 0;
9522
9bbc7d9f
PB
9523 /* If accessed with the form `super.member', then access is granted */
9524 if (from_super)
9525 return 0;
e04a16fb 9526
9bbc7d9f 9527 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9528 member is declared or a subclass of it. Find the right
9529 context to perform the check */
9530 if (PURE_INNER_CLASS_TYPE_P (reference))
9531 {
9532 while (INNER_CLASS_TYPE_P (reference))
9533 {
9534 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9535 return 0;
9536 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9537 }
9538 }
473e7b07 9539 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9540 return 0;
e04a16fb
AG
9541 return 1;
9542 }
9543
9544 /* Check access on private members. Access is granted only if it
761491c8
APB
9545 occurs from within the class in which it is declared -- that does
9546 it for innerclasses too. */
e04a16fb 9547 if (access_flag & ACC_PRIVATE)
761491c8
APB
9548 {
9549 if (reference == DECL_CONTEXT (member))
9550 return 0;
9551 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9552 return 0;
9553 return 1;
9554 }
e04a16fb
AG
9555
9556 /* Default access are permitted only when occuring within the
9557 package in which the type (REFERENCE) is declared. In other words,
9558 REFERENCE is defined in the current package */
9559 if (ctxp->package)
9560 return !class_in_current_package (reference);
473e7b07 9561
e04a16fb
AG
9562 /* Otherwise, access is granted */
9563 return 0;
9564}
9565
5e942c50
APB
9566/* Test deprecated decl access. */
9567static void
9568check_deprecation (wfl, decl)
9569 tree wfl, decl;
9570{
49f48c71 9571 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9572 /* Complain if the field is deprecated and the file it was defined
9573 in isn't compiled at the same time the file which contains its
9574 use is */
9575 if (DECL_DEPRECATED (decl)
9576 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9577 {
9578 char the [20];
9579 switch (TREE_CODE (decl))
9580 {
9581 case FUNCTION_DECL:
9582 strcpy (the, "method");
9583 break;
9584 case FIELD_DECL:
9585 strcpy (the, "field");
9586 break;
9587 case TYPE_DECL:
9588 strcpy (the, "class");
9589 break;
15fdcfe9 9590 default:
400500c4 9591 abort ();
5e942c50
APB
9592 }
9593 parse_warning_context
9594 (wfl, "The %s `%s' in class `%s' has been deprecated",
9595 the, lang_printable_name (decl, 0),
9596 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9597 }
9598}
9599
e04a16fb
AG
9600/* Returns 1 if class was declared in the current package, 0 otherwise */
9601
9602static int
9603class_in_current_package (class)
9604 tree class;
9605{
9606 static tree cache = NULL_TREE;
9607 int qualified_flag;
9608 tree left;
9609
9610 if (cache == class)
9611 return 1;
9612
9613 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9614
9615 /* If the current package is empty and the name of CLASS is
9616 qualified, class isn't in the current package. If there is a
9617 current package and the name of the CLASS is not qualified, class
9618 isn't in the current package */
0a2138e2 9619 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9620 return 0;
9621
9622 /* If there is not package and the name of CLASS isn't qualified,
9623 they belong to the same unnamed package */
9624 if (!ctxp->package && !qualified_flag)
9625 return 1;
9626
9627 /* Compare the left part of the name of CLASS with the package name */
9628 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9629 if (ctxp->package == left)
9630 {
19e223db
MM
9631 static int initialized_p;
9632 /* Register CACHE with the garbage collector. */
9633 if (!initialized_p)
9634 {
9635 ggc_add_tree_root (&cache, 1);
9636 initialized_p = 1;
9637 }
9638
e04a16fb
AG
9639 cache = class;
9640 return 1;
9641 }
9642 return 0;
9643}
9644
9645/* This function may generate code to access DECL from WHERE. This is
9646 done only if certain conditions meet. */
9647
9648static tree
9649maybe_access_field (decl, where, type)
9650 tree decl, where, type;
9651{
5e942c50
APB
9652 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9653 && !FIELD_STATIC (decl))
e04a16fb 9654 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9655 (type ? type : DECL_CONTEXT (decl)),
9656 DECL_NAME (decl));
e04a16fb
AG
9657 return decl;
9658}
9659
15fdcfe9 9660/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9661 and according to the situation, PRIMARY and WHERE may be
9662 used. IS_STATIC is set to 1 if the invoked function is static. */
9663
9664static tree
89e09b9a 9665patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9666 tree patch, primary, where;
9667 int *is_static;
b9f7e36c 9668 tree *ret_decl;
e04a16fb
AG
9669{
9670 tree wfl = TREE_OPERAND (patch, 0);
9671 tree args = TREE_OPERAND (patch, 1);
9672 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9673 tree list;
22eed1e6 9674 int is_static_flag = 0;
89e09b9a 9675 int is_super_init = 0;
bccaf73a 9676 tree this_arg = NULL_TREE;
e04a16fb
AG
9677
9678 /* Should be overriden if everything goes well. Otherwise, if
9679 something fails, it should keep this value. It stop the
9680 evaluation of a bogus assignment. See java_complete_tree,
9681 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9682 evaluating an assignment */
9683 TREE_TYPE (patch) = error_mark_node;
9684
9685 /* Since lookup functions are messing with line numbers, save the
9686 context now. */
9687 java_parser_context_save_global ();
9688
9689 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9690
9691 /* Resolution of qualified name, excluding constructors */
9692 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9693 {
dde1da72 9694 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9695 /* Extract the last IDENTIFIER of the qualified
9696 expression. This is a wfl and we will use it's location
9697 data during error report. */
9698 identifier_wfl = cut_identifier_in_qualified (wfl);
9699 identifier = EXPR_WFL_NODE (identifier_wfl);
9700
9701 /* Given the context, IDENTIFIER is syntactically qualified
9702 as a MethodName. We need to qualify what's before */
9703 qualify_ambiguous_name (wfl);
dde1da72 9704 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9705
dde1da72
APB
9706 if (resolved == error_mark_node)
9707 PATCH_METHOD_RETURN_ERROR ();
9708
9709 type = GET_SKIP_TYPE (resolved);
9710 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9711
9712 if (JPRIMITIVE_TYPE_P (type))
9713 {
7e51098e
TT
9714 parse_error_context
9715 (identifier_wfl,
9716 "Can't invoke a method on primitive type `%s'",
9717 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9718 PATCH_METHOD_RETURN_ERROR ();
9719 }
9720
dde1da72
APB
9721 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9722 args = nreverse (args);
2c56429a 9723
e04a16fb 9724 /* We're resolving a call from a type */
dde1da72 9725 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9726 {
dde1da72 9727 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9728 {
9729 parse_error_context
781b0558
KG
9730 (identifier_wfl,
9731 "Can't make static reference to method `%s' in interface `%s'",
9732 IDENTIFIER_POINTER (identifier),
e04a16fb 9733 IDENTIFIER_POINTER (name));
b9f7e36c 9734 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9735 }
e04a16fb
AG
9736 if (list && !METHOD_STATIC (list))
9737 {
c2e3db92 9738 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9739 parse_error_context
9740 (identifier_wfl,
9741 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9742 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9743 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9744 free (fct_name);
b9f7e36c 9745 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9746 }
9747 }
e04a16fb 9748 else
dde1da72
APB
9749 this_arg = primary = resolved;
9750
5e942c50 9751 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9752 wfl = identifier_wfl;
9753 }
9754 /* Resolution of simple names, names generated after a primary: or
9755 constructors */
9756 else
9757 {
cd531a2e 9758 tree class_to_search = NULL_TREE;
c2952b01 9759 int lc; /* Looking for Constructor */
e04a16fb
AG
9760
9761 /* We search constructor in their target class */
9762 if (CALL_CONSTRUCTOR_P (patch))
9763 {
22eed1e6
APB
9764 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9765 class_to_search = EXPR_WFL_NODE (wfl);
9766 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9767 this_identifier_node)
9768 class_to_search = NULL_TREE;
9769 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9770 super_identifier_node)
e04a16fb 9771 {
89e09b9a 9772 is_super_init = 1;
22eed1e6
APB
9773 if (CLASSTYPE_SUPER (current_class))
9774 class_to_search =
9775 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9776 else
9777 {
781b0558 9778 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9779 PATCH_METHOD_RETURN_ERROR ();
9780 }
e04a16fb 9781 }
22eed1e6
APB
9782
9783 /* Class to search is NULL if we're searching the current one */
9784 if (class_to_search)
e04a16fb 9785 {
c2952b01
APB
9786 class_to_search = resolve_and_layout (class_to_search, wfl);
9787
22eed1e6
APB
9788 if (!class_to_search)
9789 {
9790 parse_error_context
9791 (wfl, "Class `%s' not found in type declaration",
9792 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9793 PATCH_METHOD_RETURN_ERROR ();
9794 }
9795
5e942c50
APB
9796 /* Can't instantiate an abstract class, but we can
9797 invoke it's constructor. It's use within the `new'
9798 context is denied here. */
9799 if (CLASS_ABSTRACT (class_to_search)
9800 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9801 {
9802 parse_error_context
781b0558
KG
9803 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9804 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9805 PATCH_METHOD_RETURN_ERROR ();
9806 }
c2952b01 9807
22eed1e6 9808 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9809 }
22eed1e6
APB
9810 else
9811 class_to_search = current_class;
e04a16fb
AG
9812 lc = 1;
9813 }
9814 /* This is a regular search in the local class, unless an
9815 alternate class is specified. */
9816 else
9817 {
9818 class_to_search = (where ? where : current_class);
9819 lc = 0;
9820 }
c2952b01 9821
e04a16fb
AG
9822 /* NAME is a simple identifier or comes from a primary. Search
9823 in the class whose declaration contain the method being
9824 invoked. */
c877974e 9825 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9826
c2952b01 9827 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9828 /* Don't continue if no method were found, as the next statement
9829 can't be executed then. */
b9f7e36c
APB
9830 if (!list)
9831 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9832
9833 /* Check for static reference if non static methods */
9834 if (check_for_static_method_reference (wfl, patch, list,
9835 class_to_search, primary))
b9f7e36c 9836 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9837
165f37bc
APB
9838 /* Check for inner classes creation from illegal contexts */
9839 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9840 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9841 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9842 {
9843 parse_error_context
9844 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9845 lang_printable_name (class_to_search, 0),
9846 (!current_this ? "" :
9847 "; an explicit one must be provided when creating this inner class"));
9848 PATCH_METHOD_RETURN_ERROR ();
9849 }
9850
22eed1e6
APB
9851 /* Non static methods are called with the current object extra
9852 argument. If patch a `new TYPE()', the argument is the value
9853 returned by the object allocator. If method is resolved as a
9854 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9855 args = nreverse (args);
bccaf73a 9856 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9857 {
9858 this_arg = primary ? primary : current_this;
9859
9860 /* If we're using an access method, things are different.
9861 There are two familly of cases:
9862
9863 1) We're not generating bytecodes:
9864
9865 - LIST is non static. It's invocation is transformed from
9866 x(a1,...,an) into this$<n>.x(a1,....an).
9867 - LIST is static. It's invocation is transformed from
9868 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9869
9870 2) We're generating bytecodes:
9871
9872 - LIST is non static. It's invocation is transformed from
9873 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9874 - LIST is static. It's invocation is transformed from
9875 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9876
9877 Of course, this$<n> can be abitrary complex, ranging from
9878 this$0 (the immediate outer context) to
9879 access$0(access$0(...(this$0))).
9880
9881 maybe_use_access_method returns a non zero value if the
dfb99c83 9882 this_arg has to be moved into the (then generated) stub
4dbf4496 9883 argument list. In the meantime, the selected function
dfb99c83 9884 might have be replaced by a generated stub. */
c2952b01 9885 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9886 {
9887 args = tree_cons (NULL_TREE, this_arg, args);
9888 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9889 }
c2952b01 9890 }
e04a16fb 9891 }
b67d701b 9892
e04a16fb
AG
9893 /* Merge point of all resolution schemes. If we have nothing, this
9894 is an error, already signaled */
b9f7e36c
APB
9895 if (!list)
9896 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9897
e04a16fb
AG
9898 /* Check accessibility, position the is_static flag, build and
9899 return the call */
9bbc7d9f 9900 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9901 {
c2e3db92 9902 char *fct_name = xstrdup (lang_printable_name (list, 0));
761491c8 9903 int ctor_p = DECL_CONSTRUCTOR_P (list);
e04a16fb 9904 parse_error_context
761491c8 9905 (wfl, "Can't access %s %s `%s%s.%s' from `%s'",
e04a16fb 9906 java_accstring_lookup (get_access_flags_from_decl (list)),
761491c8
APB
9907 (ctor_p ? "constructor" : "method"),
9908 (ctor_p ?
9909 "" : lang_printable_name_wls (TREE_TYPE (TREE_TYPE (list)), 0)),
5e942c50
APB
9910 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9911 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9912 free (fct_name);
b9f7e36c 9913 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9914 }
5e942c50 9915 check_deprecation (wfl, list);
22eed1e6 9916
c2952b01
APB
9917 /* If invoking a innerclass constructor, there are hidden parameters
9918 to pass */
9919 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9920 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9921 {
9922 /* And make sure we add the accessed local variables to be saved
9923 in field aliases. */
9924 args = build_alias_initializer_parameter_list
9925 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9926
da632f2c 9927 /* Secretly pass the current_this/primary as a second argument */
165f37bc 9928 if (primary || current_this)
f8b93ea7
APB
9929 {
9930 tree extra_arg;
9931 tree this_type = (current_this ?
9932 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9933 /* Method's (list) enclosing context */
9934 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9935 /* If we have a primary, use it. */
9936 if (primary)
9937 extra_arg = primary;
9938 /* The current `this' is an inner class but isn't a direct
9939 enclosing context for the inner class we're trying to
9940 create. Build an access to the proper enclosing context
9941 and use it. */
9942 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
9943 && this_type != TREE_TYPE (mec))
9944 {
9945
9946 extra_arg = build_access_to_thisn (current_class,
9947 TREE_TYPE (mec), 0);
9948 extra_arg = java_complete_tree (extra_arg);
9949 }
9950 /* Otherwise, just use the current `this' as an enclosing
9951 context. */
9952 else
9953 extra_arg = current_this;
9954 args = tree_cons (NULL_TREE, extra_arg, args);
9955 }
165f37bc
APB
9956 else
9957 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9958 }
9959
152de068
APB
9960 /* This handles the situation where a constructor invocation needs
9961 to have an enclosing context passed as a second parameter (the
9962 constructor is one of an inner class. We extract it from the
9963 current function. */
9964 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9965 {
9966 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9967 tree extra_arg;
9968
9969 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9970 {
9971 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9972 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9973 }
9974 else
9975 {
9976 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9977 extra_arg =
9978 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9979 extra_arg = java_complete_tree (extra_arg);
9980 }
9981 args = tree_cons (NULL_TREE, extra_arg, args);
9982 }
9983
22eed1e6 9984 is_static_flag = METHOD_STATIC (list);
dba41d30 9985 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 9986 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9987
c3f2a476
APB
9988 /* In the context of an explicit constructor invocation, we can't
9989 invoke any method relying on `this'. Exceptions are: we're
9990 invoking a static function, primary exists and is not the current
9991 this, we're creating a new object. */
22eed1e6 9992 if (ctxp->explicit_constructor_p
c3f2a476
APB
9993 && !is_static_flag
9994 && (!primary || primary == current_this)
9995 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9996 {
781b0558 9997 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9998 PATCH_METHOD_RETURN_ERROR ();
9999 }
e04a16fb 10000 java_parser_context_restore_global ();
22eed1e6
APB
10001 if (is_static)
10002 *is_static = is_static_flag;
b9f7e36c
APB
10003 /* Sometimes, we want the decl of the selected method. Such as for
10004 EH checking */
10005 if (ret_decl)
10006 *ret_decl = list;
89e09b9a
PB
10007 patch = patch_invoke (patch, list, args);
10008 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10009 {
c2952b01
APB
10010 tree finit_parms, finit_call;
10011
c00f0fb2 10012 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10013 finit_parms = build_alias_initializer_parameter_list
10014 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10015
c2952b01
APB
10016 finit_call =
10017 build_method_invocation (build_wfl_node (finit_identifier_node),
10018 finit_parms);
10019
10020 /* Generate the code used to initialize fields declared with an
10021 initialization statement and build a compound statement along
10022 with the super constructor invocation. */
89e09b9a
PB
10023 patch = build (COMPOUND_EXPR, void_type_node, patch,
10024 java_complete_tree (finit_call));
10025 CAN_COMPLETE_NORMALLY (patch) = 1;
10026 }
10027 return patch;
e04a16fb
AG
10028}
10029
10030/* Check that we're not trying to do a static reference to a method in
10031 non static method. Return 1 if it's the case, 0 otherwise. */
10032
10033static int
10034check_for_static_method_reference (wfl, node, method, where, primary)
10035 tree wfl, node, method, where, primary;
10036{
10037 if (METHOD_STATIC (current_function_decl)
10038 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10039 {
c2e3db92 10040 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10041 parse_error_context
10042 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10043 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10044 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10045 free (fct_name);
10046 return 1;
10047 }
10048 return 0;
10049}
10050
c2952b01
APB
10051/* Fix the invocation of *MDECL if necessary in the case of a
10052 invocation from an inner class. *THIS_ARG might be modified
10053 appropriately and an alternative access to *MDECL might be
10054 returned. */
10055
10056static int
10057maybe_use_access_method (is_super_init, mdecl, this_arg)
10058 int is_super_init;
10059 tree *mdecl, *this_arg;
10060{
10061 tree ctx;
10062 tree md = *mdecl, ta = *this_arg;
10063 int to_return = 0;
10064 int non_static_context = !METHOD_STATIC (md);
10065
10066 if (is_super_init
165f37bc
APB
10067 || DECL_CONTEXT (md) == current_class
10068 || !PURE_INNER_CLASS_TYPE_P (current_class)
10069 || DECL_FINIT_P (md))
c2952b01
APB
10070 return 0;
10071
10072 /* If we're calling a method found in an enclosing class, generate
10073 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10074 invoking a static method. Note that if MD's type is unrelated to
10075 CURRENT_CLASS, then the current this can be used. */
c2952b01 10076
2cb3951d 10077 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10078 {
10079 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10080 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10081 {
10082 ta = build_current_thisn (current_class);
10083 ta = build_wfl_node (ta);
10084 }
10085 else
10086 {
10087 tree type = ctx;
10088 while (type)
10089 {
10090 maybe_build_thisn_access_method (type);
4dbf4496 10091 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10092 {
10093 ta = build_access_to_thisn (ctx, type, 0);
10094 break;
10095 }
10096 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10097 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10098 }
10099 }
10100 ta = java_complete_tree (ta);
10101 }
10102
10103 /* We might have to use an access method to get to MD. We can
10104 break the method access rule as far as we're not generating
10105 bytecode */
10106 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10107 {
10108 md = build_outer_method_access_method (md);
10109 to_return = 1;
10110 }
10111
10112 *mdecl = md;
10113 *this_arg = ta;
10114
10115 /* Returnin a non zero value indicates we were doing a non static
10116 method invokation that is now a static invocation. It will have
10117 callee displace `this' to insert it in the regular argument
10118 list. */
10119 return (non_static_context && to_return);
10120}
10121
e04a16fb
AG
10122/* Patch an invoke expression METHOD and ARGS, based on its invocation
10123 mode. */
10124
10125static tree
89e09b9a 10126patch_invoke (patch, method, args)
e04a16fb 10127 tree patch, method, args;
e04a16fb
AG
10128{
10129 tree dtable, func;
0a2138e2 10130 tree original_call, t, ta;
e815887f 10131 tree cond = NULL_TREE;
e04a16fb 10132
5e942c50
APB
10133 /* Last step for args: convert build-in types. If we're dealing with
10134 a new TYPE() type call, the first argument to the constructor
e815887f 10135 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10136 `new' */
10137 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10138 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10139 t = TREE_CHAIN (t);
ac825856
APB
10140 for (ta = args; t != end_params_node && ta;
10141 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10142 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10143 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10144 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10145
10146 /* Resolve unresolved returned type isses */
10147 t = TREE_TYPE (TREE_TYPE (method));
10148 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10149 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10150
e8fc7396 10151 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10152 func = method;
10153 else
e04a16fb 10154 {
15fdcfe9 10155 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10156 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10157 {
10158 case INVOKE_VIRTUAL:
10159 dtable = invoke_build_dtable (0, args);
10160 func = build_invokevirtual (dtable, method);
10161 break;
b9f7e36c 10162
e815887f
TT
10163 case INVOKE_NONVIRTUAL:
10164 /* If the object for the method call is null, we throw an
10165 exception. We don't do this if the object is the current
10166 method's `this'. In other cases we just rely on an
10167 optimization pass to eliminate redundant checks. */
10168 if (TREE_VALUE (args) != current_this)
10169 {
10170 /* We use a SAVE_EXPR here to make sure we only evaluate
10171 the new `self' expression once. */
10172 tree save_arg = save_expr (TREE_VALUE (args));
10173 TREE_VALUE (args) = save_arg;
10174 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10175 null_pointer_node);
10176 }
10177 /* Fall through. */
10178
15fdcfe9
PB
10179 case INVOKE_SUPER:
10180 case INVOKE_STATIC:
10181 func = build_known_method_ref (method, TREE_TYPE (method),
10182 DECL_CONTEXT (method),
10183 signature, args);
10184 break;
e04a16fb 10185
15fdcfe9
PB
10186 case INVOKE_INTERFACE:
10187 dtable = invoke_build_dtable (1, args);
173f556c 10188 func = build_invokeinterface (dtable, method);
15fdcfe9 10189 break;
5e942c50 10190
15fdcfe9 10191 default:
400500c4 10192 abort ();
15fdcfe9
PB
10193 }
10194
10195 /* Ensure self_type is initialized, (invokestatic). FIXME */
10196 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10197 }
10198
e04a16fb
AG
10199 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10200 TREE_OPERAND (patch, 0) = func;
10201 TREE_OPERAND (patch, 1) = args;
10202 original_call = patch;
10203
e815887f 10204 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10205 returned value is the first argument to the constructor. We build
10206 a COMPOUND_EXPR and use saved expression so that the overall NEW
10207 expression value is a pointer to a newly created and initialized
10208 class. */
10209 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10210 {
10211 tree class = DECL_CONTEXT (method);
10212 tree c1, saved_new, size, new;
e8fc7396 10213 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10214 {
10215 TREE_TYPE (patch) = build_pointer_type (class);
10216 return patch;
10217 }
e04a16fb
AG
10218 if (!TYPE_SIZE (class))
10219 safe_layout_class (class);
10220 size = size_in_bytes (class);
10221 new = build (CALL_EXPR, promote_type (class),
10222 build_address_of (alloc_object_node),
10223 tree_cons (NULL_TREE, build_class_ref (class),
10224 build_tree_list (NULL_TREE,
10225 size_in_bytes (class))),
10226 NULL_TREE);
10227 saved_new = save_expr (new);
10228 c1 = build_tree_list (NULL_TREE, saved_new);
10229 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10230 TREE_OPERAND (original_call, 1) = c1;
10231 TREE_SET_CODE (original_call, CALL_EXPR);
10232 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10233 }
e815887f
TT
10234
10235 /* If COND is set, then we are building a check to see if the object
10236 is NULL. */
10237 if (cond != NULL_TREE)
10238 {
10239 /* We have to make the `then' branch a compound expression to
10240 make the types turn out right. This seems bizarre. */
10241 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10242 build (COMPOUND_EXPR, TREE_TYPE (patch),
10243 build (CALL_EXPR, void_type_node,
10244 build_address_of (soft_nullpointer_node),
10245 NULL_TREE, NULL_TREE),
10246 (FLOAT_TYPE_P (TREE_TYPE (patch))
10247 ? build_real (TREE_TYPE (patch), dconst0)
10248 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10249 integer_zero_node))),
10250 patch);
10251 TREE_SIDE_EFFECTS (patch) = 1;
10252 }
10253
e04a16fb
AG
10254 return patch;
10255}
10256
10257static int
10258invocation_mode (method, super)
10259 tree method;
10260 int super;
10261{
10262 int access = get_access_flags_from_decl (method);
10263
22eed1e6
APB
10264 if (super)
10265 return INVOKE_SUPER;
10266
e815887f 10267 if (access & ACC_STATIC)
e04a16fb
AG
10268 return INVOKE_STATIC;
10269
e815887f
TT
10270 /* We have to look for a constructor before we handle nonvirtual
10271 calls; otherwise the constructor will look nonvirtual. */
10272 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10273 return INVOKE_STATIC;
e815887f
TT
10274
10275 if (access & ACC_FINAL || access & ACC_PRIVATE)
10276 return INVOKE_NONVIRTUAL;
10277
10278 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10279 return INVOKE_NONVIRTUAL;
10280
e04a16fb
AG
10281 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10282 return INVOKE_INTERFACE;
22eed1e6 10283
e04a16fb
AG
10284 return INVOKE_VIRTUAL;
10285}
10286
b67d701b
PB
10287/* Retrieve a refined list of matching methods. It covers the step
10288 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10289
10290static tree
10291lookup_method_invoke (lc, cl, class, name, arg_list)
10292 int lc;
10293 tree cl;
10294 tree class, name, arg_list;
10295{
de4c7b02 10296 tree atl = end_params_node; /* Arg Type List */
c877974e 10297 tree method, signature, list, node;
49f48c71 10298 const char *candidates; /* Used for error report */
b5b8a0e7 10299 char *dup;
e04a16fb 10300
5e942c50 10301 /* Fix the arguments */
e04a16fb
AG
10302 for (node = arg_list; node; node = TREE_CHAIN (node))
10303 {
e3884b71 10304 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10305 /* Non primitive type may have to be resolved */
e3884b71 10306 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10307 resolve_and_layout (current_arg, NULL_TREE);
10308 /* And promoted */
b67d701b 10309 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10310 current_arg = promote_type (current_arg);
5e942c50 10311 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10312 }
e04a16fb 10313
c2952b01
APB
10314 /* Presto. If we're dealing with an anonymous class and a
10315 constructor call, generate the right constructor now, since we
10316 know the arguments' types. */
10317
10318 if (lc && ANONYMOUS_CLASS_P (class))
10319 craft_constructor (TYPE_NAME (class), atl);
10320
5e942c50
APB
10321 /* Find all candidates and then refine the list, searching for the
10322 most specific method. */
10323 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10324 list = find_most_specific_methods_list (list);
b67d701b
PB
10325 if (list && !TREE_CHAIN (list))
10326 return TREE_VALUE (list);
e04a16fb 10327
b67d701b
PB
10328 /* Issue an error. List candidates if any. Candidates are listed
10329 only if accessible (non accessible methods may end-up here for
10330 the sake of a better error report). */
10331 candidates = NULL;
10332 if (list)
e04a16fb 10333 {
e04a16fb 10334 tree current;
b67d701b 10335 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10336 for (current = list; current; current = TREE_CHAIN (current))
10337 {
b67d701b
PB
10338 tree cm = TREE_VALUE (current);
10339 char string [4096];
10340 if (!cm || not_accessible_p (class, cm, 0))
10341 continue;
b67d701b 10342 sprintf
22eed1e6
APB
10343 (string, " `%s' in `%s'%s",
10344 get_printable_method_name (cm),
b67d701b
PB
10345 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10346 (TREE_CHAIN (current) ? "\n" : ""));
10347 obstack_grow (&temporary_obstack, string, strlen (string));
10348 }
10349 obstack_1grow (&temporary_obstack, '\0');
10350 candidates = obstack_finish (&temporary_obstack);
10351 }
10352 /* Issue the error message */
c877974e
APB
10353 method = make_node (FUNCTION_TYPE);
10354 TYPE_ARG_TYPES (method) = atl;
b67d701b 10355 signature = build_java_argument_signature (method);
c63b98cd 10356 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10357 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10358 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10359 (lc ? dup : IDENTIFIER_POINTER (name)),
10360 IDENTIFIER_POINTER (signature), dup,
b67d701b 10361 (candidates ? candidates : ""));
b5b8a0e7 10362 free (dup);
b67d701b
PB
10363 return NULL_TREE;
10364}
10365
5e942c50
APB
10366/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10367 when we're looking for a constructor. */
b67d701b
PB
10368
10369static tree
5e942c50
APB
10370find_applicable_accessible_methods_list (lc, class, name, arglist)
10371 int lc;
b67d701b
PB
10372 tree class, name, arglist;
10373{
ad69b5b6
BM
10374 static struct hash_table t, *searched_classes = NULL;
10375 static int search_not_done = 0;
b67d701b
PB
10376 tree list = NULL_TREE, all_list = NULL_TREE;
10377
ad69b5b6
BM
10378 /* Check the hash table to determine if this class has been searched
10379 already. */
10380 if (searched_classes)
10381 {
10382 if (hash_lookup (searched_classes,
10383 (const hash_table_key) class, FALSE, NULL))
10384 return NULL;
10385 }
10386 else
10387 {
10388 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10389 java_hash_compare_tree_node);
10390 searched_classes = &t;
10391 }
10392
10393 search_not_done++;
10394 hash_lookup (searched_classes,
0c2b8145 10395 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10396
c2952b01
APB
10397 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10398 {
10399 load_class (class, 1);
10400 safe_layout_class (class);
10401 }
10402
1982388a 10403 /* Search interfaces */
9a7ab4b3
APB
10404 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10405 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10406 {
1982388a
APB
10407 int i, n;
10408 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10409 search_applicable_methods_list (lc, TYPE_METHODS (class),
10410 name, arglist, &list, &all_list);
1982388a 10411 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10412 for (i = 1; i < n; i++)
b67d701b 10413 {
de0b553f
APB
10414 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10415 tree rlist;
10416
de0b553f
APB
10417 rlist = find_applicable_accessible_methods_list (lc, t, name,
10418 arglist);
165f37bc 10419 list = chainon (rlist, list);
e04a16fb 10420 }
e04a16fb 10421 }
1982388a
APB
10422 /* Search classes */
10423 else
c2952b01 10424 {
165f37bc
APB
10425 tree sc = class;
10426 int seen_inner_class = 0;
c2952b01
APB
10427 search_applicable_methods_list (lc, TYPE_METHODS (class),
10428 name, arglist, &list, &all_list);
10429
493d561d
APB
10430 /* When looking finit$, we turn LC to 1 so that we only search
10431 in class. Note that we should have found something at
10432 this point. */
10433 if (ID_FINIT_P (name))
10434 {
10435 lc = 1;
10436 if (!list)
400500c4 10437 abort ();
493d561d
APB
10438 }
10439
165f37bc
APB
10440 /* We must search all interfaces of this class */
10441 if (!lc)
10442 {
10443 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10444 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10445 for (i = 1; i < n; i++)
10446 {
10447 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10448 if (t != object_type_node)
30a3caef
ZW
10449 {
10450 tree rlist
10451 = find_applicable_accessible_methods_list (lc, t,
10452 name, arglist);
10453 list = chainon (rlist, list);
10454 }
165f37bc 10455 }
165f37bc
APB
10456 }
10457
c2952b01
APB
10458 /* Search enclosing context of inner classes before looking
10459 ancestors up. */
10460 while (!lc && INNER_CLASS_TYPE_P (class))
10461 {
165f37bc
APB
10462 tree rlist;
10463 seen_inner_class = 1;
c2952b01 10464 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10465 rlist = find_applicable_accessible_methods_list (lc, class,
10466 name, arglist);
10467 list = chainon (rlist, list);
c2952b01 10468 }
165f37bc
APB
10469
10470 if (!lc && seen_inner_class
10471 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10472 class = CLASSTYPE_SUPER (sc);
10473 else
10474 class = sc;
10475
ad69b5b6
BM
10476 /* Search superclass */
10477 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10478 {
10479 tree rlist;
10480 class = CLASSTYPE_SUPER (class);
10481 rlist = find_applicable_accessible_methods_list (lc, class,
10482 name, arglist);
10483 list = chainon (rlist, list);
10484 }
10485 }
10486
10487 search_not_done--;
10488
10489 /* We're done. Reset the searched classes list and finally search
10490 java.lang.Object if it wasn't searched already. */
10491 if (!search_not_done)
10492 {
10493 if (!lc
10494 && TYPE_METHODS (object_type_node)
10495 && !hash_lookup (searched_classes,
10496 (const hash_table_key) object_type_node,
10497 FALSE, NULL))
10498 {
10499 search_applicable_methods_list (lc,
10500 TYPE_METHODS (object_type_node),
10501 name, arglist, &list, &all_list);
10502 }
10503 hash_table_free (searched_classes);
10504 searched_classes = NULL;
c2952b01 10505 }
1982388a 10506
b67d701b
PB
10507 /* Either return the list obtained or all selected (but
10508 inaccessible) methods for better error report. */
10509 return (!list ? all_list : list);
10510}
e04a16fb 10511
ad69b5b6 10512/* Effectively search for the appropriate method in method */
1982388a
APB
10513
10514static void
c2952b01 10515search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10516 int lc;
10517 tree method, name, arglist;
10518 tree *list, *all_list;
10519{
10520 for (; method; method = TREE_CHAIN (method))
10521 {
10522 /* When dealing with constructor, stop here, otherwise search
10523 other classes */
10524 if (lc && !DECL_CONSTRUCTOR_P (method))
10525 continue;
10526 else if (!lc && (DECL_CONSTRUCTOR_P (method)
c4faeb92 10527 || (DECL_NAME (method) != name)))
1982388a 10528 continue;
c7303e41 10529
1982388a
APB
10530 if (argument_types_convertible (method, arglist))
10531 {
10532 /* Retain accessible methods only */
10533 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10534 method, 0))
10535 *list = tree_cons (NULL_TREE, method, *list);
10536 else
10537 /* Also retain all selected method here */
10538 *all_list = tree_cons (NULL_TREE, method, *list);
10539 }
10540 }
ad69b5b6 10541}
1982388a 10542
b67d701b
PB
10543/* 15.11.2.2 Choose the Most Specific Method */
10544
10545static tree
10546find_most_specific_methods_list (list)
10547 tree list;
10548{
10549 int max = 0;
9a7ab4b3 10550 int abstract, candidates;
b67d701b
PB
10551 tree current, new_list = NULL_TREE;
10552 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10553 {
b67d701b
PB
10554 tree method;
10555 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10556
10557 for (method = list; method; method = TREE_CHAIN (method))
10558 {
0c2b8145 10559 tree method_v, current_v;
b67d701b
PB
10560 /* Don't test a method against itself */
10561 if (method == current)
10562 continue;
10563
0c2b8145
APB
10564 method_v = TREE_VALUE (method);
10565 current_v = TREE_VALUE (current);
10566
10567 /* Compare arguments and location where methods where declared */
10568 if (argument_types_convertible (method_v, current_v))
b67d701b 10569 {
0c2b8145
APB
10570 if (valid_method_invocation_conversion_p
10571 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10572 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10573 && enclosing_context_p (DECL_CONTEXT (method_v),
10574 DECL_CONTEXT (current_v))))
10575 {
10576 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10577 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10578 max = (v > max ? v : max);
10579 }
b67d701b
PB
10580 }
10581 }
e04a16fb
AG
10582 }
10583
b67d701b 10584 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10585 for (current = list, abstract = -1, candidates = -1;
10586 current; current = TREE_CHAIN (current))
b67d701b 10587 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10588 {
10589 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10590 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10591 candidates++;
10592 }
b67d701b 10593
165f37bc
APB
10594 /* If we have several and they're all abstract, just pick the
10595 closest one. */
9a7ab4b3
APB
10596 if (candidates > 0 && (candidates == abstract))
10597 {
10598 new_list = nreverse (new_list);
10599 TREE_CHAIN (new_list) = NULL_TREE;
10600 }
165f37bc 10601
cab8e2bd
BM
10602 /* We have several (we couldn't find a most specific), all but one
10603 are abstract, we pick the only non abstract one. */
10604 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10605 {
9a7ab4b3
APB
10606 for (current = new_list; current; current = TREE_CHAIN (current))
10607 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10608 {
10609 TREE_CHAIN (current) = NULL_TREE;
10610 new_list = current;
10611 }
165f37bc
APB
10612 }
10613
b67d701b
PB
10614 /* If we can't find one, lower expectations and try to gather multiple
10615 maximally specific methods */
165f37bc 10616 while (!new_list && max)
b67d701b
PB
10617 {
10618 while (--max > 0)
10619 {
10620 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10621 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10622 }
b67d701b
PB
10623 }
10624
10625 return new_list;
e04a16fb
AG
10626}
10627
b67d701b
PB
10628/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10629 converted by method invocation conversion (5.3) to the type of the
10630 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10631 to change less often than M1. */
e04a16fb 10632
b67d701b
PB
10633static int
10634argument_types_convertible (m1, m2_or_arglist)
10635 tree m1, m2_or_arglist;
e04a16fb 10636{
b67d701b
PB
10637 static tree m2_arg_value = NULL_TREE;
10638 static tree m2_arg_cache = NULL_TREE;
19e223db 10639 static int initialized_p;
e04a16fb 10640
b67d701b 10641 register tree m1_arg, m2_arg;
e04a16fb 10642
19e223db
MM
10643 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10644 collector. */
10645 if (!initialized_p)
10646 {
10647 ggc_add_tree_root (&m2_arg_value, 1);
10648 ggc_add_tree_root (&m2_arg_cache, 1);
10649 initialized_p = 1;
10650 }
10651
c2952b01 10652 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10653
b67d701b
PB
10654 if (m2_arg_value == m2_or_arglist)
10655 m2_arg = m2_arg_cache;
10656 else
10657 {
10658 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10659 argument types */
10660 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10661 {
10662 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10663 if (!METHOD_STATIC (m2_or_arglist))
10664 m2_arg = TREE_CHAIN (m2_arg);
10665 }
10666 else
10667 m2_arg = m2_or_arglist;
e04a16fb 10668
b67d701b
PB
10669 m2_arg_value = m2_or_arglist;
10670 m2_arg_cache = m2_arg;
10671 }
e04a16fb 10672
de4c7b02 10673 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10674 {
c877974e 10675 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10676 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10677 TREE_VALUE (m2_arg)))
10678 break;
10679 m1_arg = TREE_CHAIN (m1_arg);
10680 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10681 }
de4c7b02 10682 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10683}
10684
10685/* Qualification routines */
10686
10687static void
10688qualify_ambiguous_name (id)
10689 tree id;
10690{
cd531a2e
KG
10691 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10692 saved_current_class;
d8fccff5 10693 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10694 int code;
e04a16fb
AG
10695
10696 /* We first qualify the first element, then derive qualification of
10697 others based on the first one. If the first element is qualified
10698 by a resolution (field or type), this resolution is stored in the
10699 QUAL_RESOLUTION of the qual element being examined. We need to
10700 save the current_class since the use of SUPER might change the
10701 its value. */
10702 saved_current_class = current_class;
10703 qual = EXPR_WFL_QUALIFICATION (id);
10704 do {
10705
10706 /* Simple qualified expression feature a qual_wfl that is a
10707 WFL. Expression derived from a primary feature more complicated
10708 things like a CALL_EXPR. Expression from primary need to be
10709 worked out to extract the part on which the qualification will
10710 take place. */
10711 qual_wfl = QUAL_WFL (qual);
10712 switch (TREE_CODE (qual_wfl))
10713 {
10714 case CALL_EXPR:
10715 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10716 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10717 {
10718 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10719 qual_wfl = QUAL_WFL (qual);
10720 }
10721 break;
d8fccff5 10722 case NEW_ARRAY_EXPR:
c2952b01 10723 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10724 qual = TREE_CHAIN (qual);
1a6d4fb7 10725 again = new_array_found = 1;
d8fccff5 10726 continue;
e04a16fb 10727 case CONVERT_EXPR:
f2760b27
APB
10728 break;
10729 case NEW_CLASS_EXPR:
e04a16fb
AG
10730 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10731 break;
c583dd46
APB
10732 case ARRAY_REF:
10733 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10734 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10735 break;
8576f094
APB
10736 case STRING_CST:
10737 qual = TREE_CHAIN (qual);
10738 qual_wfl = QUAL_WFL (qual);
10739 break;
165f37bc
APB
10740 case CLASS_LITERAL:
10741 qual = TREE_CHAIN (qual);
10742 qual_wfl = QUAL_WFL (qual);
10743 break;
0a2138e2
APB
10744 default:
10745 /* Fix for -Wall. Just break doing nothing */
10746 break;
e04a16fb 10747 }
8576f094 10748
e04a16fb
AG
10749 ptr_type = current_class;
10750 again = 0;
8576f094
APB
10751 code = TREE_CODE (qual_wfl);
10752
10753 /* Pos evaluation: non WFL leading expression nodes */
10754 if (code == CONVERT_EXPR
10755 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10756 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10757
cd7c5840
APB
10758 else if (code == INTEGER_CST)
10759 name = qual_wfl;
10760
ac22f9cb 10761 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10762 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10763 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10764
c2952b01
APB
10765 else if (code == TREE_LIST)
10766 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10767
37feda7d
APB
10768 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10769 || code == PLUS_EXPR)
8576f094
APB
10770 {
10771 qual = TREE_CHAIN (qual);
10772 qual_wfl = QUAL_WFL (qual);
10773 again = 1;
10774 }
10775 else
f441f671
APB
10776 {
10777 name = EXPR_WFL_NODE (qual_wfl);
10778 if (!name)
10779 {
10780 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10781 again = 1;
10782 }
10783 }
10784
e04a16fb
AG
10785 /* If we have a THIS (from a primary), we set the context accordingly */
10786 if (name == this_identifier_node)
10787 {
6e22695a
APB
10788 /* This isn't really elegant. One more added irregularity
10789 before I start using COMPONENT_REF (hopefully very soon.) */
10790 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10791 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10792 EXPR_WITH_FILE_LOCATION
10793 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10794 this_identifier_node)
10795 {
10796 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10797 qual = EXPR_WFL_QUALIFICATION (qual);
10798 }
e04a16fb
AG
10799 qual = TREE_CHAIN (qual);
10800 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10801 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10802 again = 1;
10803 else
10804 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10805 this_found = 1;
10806 }
10807 /* If we have a SUPER, we set the context accordingly */
10808 if (name == super_identifier_node)
10809 {
10810 current_class = CLASSTYPE_SUPER (ptr_type);
10811 /* Check that there is such a thing as a super class. If not,
10812 return. The error will be caught later on, during the
10813 resolution */
10814 if (!current_class)
10815 {
10816 current_class = saved_current_class;
10817 return;
10818 }
10819 qual = TREE_CHAIN (qual);
10820 /* Do one more interation to set things up */
10821 super_found = again = 1;
10822 }
10823 } while (again);
10824
f2760b27
APB
10825 /* If name appears within the scope of a local variable declaration
10826 or parameter declaration, then it is an expression name. We don't
10827 carry this test out if we're in the context of the use of SUPER
10828 or THIS */
cd7c5840
APB
10829 if (!this_found && !super_found
10830 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10831 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10832 {
10833 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10834 QUAL_RESOLUTION (qual) = decl;
10835 }
10836
10837 /* If within the class/interface NAME was found to be used there
10838 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10839 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10840 address length, it is OK. */
10841 else if ((decl = lookup_field_wrapper (ptr_type, name))
10842 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10843 {
10844 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10845 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10846 }
10847
1a6d4fb7 10848 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10849 - NAME is a class/interface declared within the compilation
10850 unit containing NAME,
10851 - NAME is imported via a single-type-import declaration,
10852 - NAME is declared in an another compilation unit of the package
10853 of the compilation unit containing NAME,
10854 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10855 of the compilation unit containing NAME.
10856 - NAME is actually a STRING_CST. */
cd7c5840
APB
10857 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10858 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10859 {
10860 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10861 QUAL_RESOLUTION (qual) = decl;
10862 }
10863
f2760b27 10864 /* Method call, array references and cast are expression name */
9bbc7d9f 10865 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10866 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10867 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10868 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10869
10870 /* Check here that NAME isn't declared by more than one
10871 type-import-on-demand declaration of the compilation unit
10872 containing NAME. FIXME */
10873
10874 /* Otherwise, NAME is reclassified as a package name */
10875 else
10876 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10877
10878 /* Propagate the qualification accross other components of the
10879 qualified name */
10880 for (qual = TREE_CHAIN (qual); qual;
10881 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10882 {
10883 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10884 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10885 else
10886 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10887 }
10888
10889 /* Store the global qualification for the ambiguous part of ID back
10890 into ID fields */
10891 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10892 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10893 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10894 RESOLVE_TYPE_NAME_P (id) = 1;
10895 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10896 RESOLVE_PACKAGE_NAME_P (id) = 1;
10897
10898 /* Restore the current class */
10899 current_class = saved_current_class;
10900}
10901
10902static int
10903breakdown_qualified (left, right, source)
10904 tree *left, *right, source;
10905{
63ad61ed 10906 char *p, *base;
e04a16fb
AG
10907 int l = IDENTIFIER_LENGTH (source);
10908
63ad61ed
ZW
10909 base = alloca (l + 1);
10910 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10911
e04a16fb 10912 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10913 p = base + l - 1;
e04a16fb
AG
10914 while (*p != '.' && p != base)
10915 p--;
10916
10917 /* We didn't find a '.'. Return an error */
10918 if (p == base)
10919 return 1;
10920
10921 *p = '\0';
10922 if (right)
10923 *right = get_identifier (p+1);
63ad61ed 10924 *left = get_identifier (base);
e04a16fb
AG
10925
10926 return 0;
10927}
10928
e04a16fb 10929/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10930 local variable decls if present.
10931 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10932
10933static tree
10934java_complete_tree (node)
10935 tree node;
5b09b33e
PB
10936{
10937 node = java_complete_lhs (node);
c7303e41 10938 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 10939 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10940 && !flag_emit_xref)
5b09b33e
PB
10941 {
10942 tree value = DECL_INITIAL (node);
10943 DECL_INITIAL (node) = NULL_TREE;
10944 value = fold_constant_for_init (value, node);
10945 DECL_INITIAL (node) = value;
10946 if (value != NULL_TREE)
c2952b01
APB
10947 {
10948 /* fold_constant_for_init sometimes widen the original type
10949 of the constant (i.e. byte to int.) It's not desirable,
10950 especially if NODE is a function argument. */
10951 if (TREE_CODE (value) == INTEGER_CST
10952 && TREE_TYPE (node) != TREE_TYPE (value))
10953 return convert (TREE_TYPE (node), value);
10954 else
10955 return value;
10956 }
c7303e41
APB
10957 else
10958 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
10959 }
10960 return node;
10961}
10962
2aa11e97
APB
10963static tree
10964java_stabilize_reference (node)
10965 tree node;
10966{
10967 if (TREE_CODE (node) == COMPOUND_EXPR)
10968 {
10969 tree op0 = TREE_OPERAND (node, 0);
10970 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10971 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10972 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10973 return node;
10974 }
5cbdba64 10975 return stabilize_reference (node);
2aa11e97
APB
10976}
10977
5b09b33e
PB
10978/* Patch tree nodes in a function body. When a BLOCK is found, push
10979 local variable decls if present.
10980 Same as java_complete_tree, but does not resolve static finals to values. */
10981
10982static tree
10983java_complete_lhs (node)
10984 tree node;
e04a16fb 10985{
22eed1e6 10986 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10987 int flag;
e04a16fb
AG
10988
10989 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10990 worked out. */
e04a16fb
AG
10991 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10992 return node;
10993
10994 /* The switch block implements cases processing container nodes
10995 first. Contained nodes are always written back. Leaves come
10996 next and return a value. */
10997 switch (TREE_CODE (node))
10998 {
10999 case BLOCK:
11000
11001 /* 1- Block section.
11002 Set the local values on decl names so we can identify them
11003 faster when they're referenced. At that stage, identifiers
11004 are legal so we don't check for declaration errors. */
11005 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11006 {
11007 DECL_CONTEXT (cn) = current_function_decl;
11008 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11009 }
15fdcfe9
PB
11010 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11011 CAN_COMPLETE_NORMALLY (node) = 1;
11012 else
e04a16fb 11013 {
15fdcfe9
PB
11014 tree stmt = BLOCK_EXPR_BODY (node);
11015 tree *ptr;
11016 int error_seen = 0;
11017 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11018 {
c877974e
APB
11019 /* Re-order from (((A; B); C); ...; Z) to
11020 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11021 This makes it easier to scan the statements left-to-right
11022 without using recursion (which might overflow the stack
11023 if the block has many statements. */
11024 for (;;)
11025 {
11026 tree left = TREE_OPERAND (stmt, 0);
11027 if (TREE_CODE (left) != COMPOUND_EXPR)
11028 break;
11029 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11030 TREE_OPERAND (left, 1) = stmt;
11031 stmt = left;
11032 }
11033 BLOCK_EXPR_BODY (node) = stmt;
11034 }
11035
c877974e
APB
11036 /* Now do the actual complete, without deep recursion for
11037 long blocks. */
15fdcfe9 11038 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11039 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11040 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11041 {
11042 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11043 tree *next = &TREE_OPERAND (*ptr, 1);
11044 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11045 if (cur == empty_stmt_node)
11046 {
11047 /* Optimization; makes it easier to detect empty bodies.
11048 Most useful for <clinit> with all-constant initializer. */
11049 *ptr = *next;
11050 continue;
11051 }
15fdcfe9
PB
11052 if (TREE_CODE (cur) == ERROR_MARK)
11053 error_seen++;
11054 else if (! CAN_COMPLETE_NORMALLY (cur))
11055 {
11056 wfl_op2 = *next;
11057 for (;;)
11058 {
11059 if (TREE_CODE (wfl_op2) == BLOCK)
11060 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11061 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11062 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11063 else
11064 break;
11065 }
11066 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11067 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11068 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11069 }
11070 ptr = next;
11071 }
11072 *ptr = java_complete_tree (*ptr);
11073
11074 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11075 return error_mark_node;
15fdcfe9 11076 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11077 }
11078 /* Turn local bindings to null */
11079 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11080 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11081
11082 TREE_TYPE (node) = void_type_node;
11083 break;
11084
11085 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11086
b9f7e36c
APB
11087 case THROW_EXPR:
11088 wfl_op1 = TREE_OPERAND (node, 0);
11089 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11090 /* 14.19 A throw statement cannot complete normally. */
11091 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11092 return patch_throw_statement (node, wfl_op1);
11093
11094 case SYNCHRONIZED_EXPR:
11095 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11096 return patch_synchronized_statement (node, wfl_op1);
11097
b67d701b
PB
11098 case TRY_EXPR:
11099 return patch_try_statement (node);
11100
a7d8d81f
PB
11101 case TRY_FINALLY_EXPR:
11102 COMPLETE_CHECK_OP_0 (node);
11103 COMPLETE_CHECK_OP_1 (node);
11104 CAN_COMPLETE_NORMALLY (node)
11105 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11106 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11107 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11108 return node;
11109
5a005d9e
PB
11110 case CLEANUP_POINT_EXPR:
11111 COMPLETE_CHECK_OP_0 (node);
11112 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11113 CAN_COMPLETE_NORMALLY (node) =
11114 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11115 return node;
11116
11117 case WITH_CLEANUP_EXPR:
11118 COMPLETE_CHECK_OP_0 (node);
11119 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11120 CAN_COMPLETE_NORMALLY (node) =
11121 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11122 TREE_TYPE (node) = void_type_node;
11123 return node;
11124
e04a16fb
AG
11125 case LABELED_BLOCK_EXPR:
11126 PUSH_LABELED_BLOCK (node);
11127 if (LABELED_BLOCK_BODY (node))
11128 COMPLETE_CHECK_OP_1 (node);
11129 TREE_TYPE (node) = void_type_node;
11130 POP_LABELED_BLOCK ();
1fb89a4d
APB
11131
11132 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11133 {
11134 LABELED_BLOCK_BODY (node) = NULL_TREE;
11135 CAN_COMPLETE_NORMALLY (node) = 1;
11136 }
1fb89a4d 11137 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11138 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11139 return node;
11140
11141 case EXIT_BLOCK_EXPR:
11142 /* We don't complete operand 1, because it's the return value of
11143 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11144 return patch_bc_statement (node);
11145
15fdcfe9
PB
11146 case CASE_EXPR:
11147 cn = java_complete_tree (TREE_OPERAND (node, 0));
11148 if (cn == error_mark_node)
11149 return cn;
11150
8576f094
APB
11151 /* First, the case expression must be constant. Values of final
11152 fields are accepted. */
15fdcfe9 11153 cn = fold (cn);
8576f094
APB
11154 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11155 && JDECL_P (TREE_OPERAND (cn, 1))
11156 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11157 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11158 {
100f7cd8
APB
11159 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11160 TREE_OPERAND (cn, 1));
100f7cd8 11161 }
15fdcfe9 11162
ce6e9147 11163 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11164 {
11165 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11166 parse_error_context (node, "Constant expression required");
11167 return error_mark_node;
11168 }
11169
11170 nn = ctxp->current_loop;
11171
11172 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11173 if (!try_builtin_assignconv (NULL_TREE,
11174 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11175 {
11176 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11177 parse_error_context
11178 (wfl_operator,
11179 "Incompatible type for case. Can't convert `%s' to `int'",
11180 lang_printable_name (TREE_TYPE (cn), 0));
11181 return error_mark_node;
11182 }
11183
11184 cn = fold (convert (int_type_node, cn));
11185
11186 /* Multiple instance of a case label bearing the same
11187 value is checked during code generation. The case
11188 expression is allright so far. */
34d4df06
APB
11189 if (TREE_CODE (cn) == VAR_DECL)
11190 cn = DECL_INITIAL (cn);
15fdcfe9 11191 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11192 TREE_TYPE (node) = void_type_node;
15fdcfe9 11193 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11194 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11195 break;
11196
11197 case DEFAULT_EXPR:
11198 nn = ctxp->current_loop;
11199 /* Only one default label is allowed per switch statement */
11200 if (SWITCH_HAS_DEFAULT (nn))
11201 {
11202 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11203 parse_error_context (wfl_operator,
11204 "Duplicate case label: `default'");
11205 return error_mark_node;
11206 }
11207 else
11208 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11209 TREE_TYPE (node) = void_type_node;
10100cc7 11210 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11211 CAN_COMPLETE_NORMALLY (node) = 1;
11212 break;
11213
b67d701b 11214 case SWITCH_EXPR:
e04a16fb
AG
11215 case LOOP_EXPR:
11216 PUSH_LOOP (node);
11217 /* Check whether the loop was enclosed in a labeled
11218 statement. If not, create one, insert the loop in it and
11219 return the node */
11220 nn = patch_loop_statement (node);
b67d701b 11221
e04a16fb 11222 /* Anyways, walk the body of the loop */
b67d701b
PB
11223 if (TREE_CODE (node) == LOOP_EXPR)
11224 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11225 /* Switch statement: walk the switch expression and the cases */
11226 else
11227 node = patch_switch_statement (node);
11228
e7c7bcef 11229 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11230 nn = error_mark_node;
11231 else
15fdcfe9 11232 {
b635eb2f
PB
11233 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11234 /* If we returned something different, that's because we
11235 inserted a label. Pop the label too. */
11236 if (nn != node)
11237 {
11238 if (CAN_COMPLETE_NORMALLY (node))
11239 CAN_COMPLETE_NORMALLY (nn) = 1;
11240 POP_LABELED_BLOCK ();
11241 }
15fdcfe9 11242 }
e04a16fb
AG
11243 POP_LOOP ();
11244 return nn;
11245
11246 case EXIT_EXPR:
11247 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11248 return patch_exit_expr (node);
11249
11250 case COND_EXPR:
11251 /* Condition */
11252 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11253 if (TREE_OPERAND (node, 0) == error_mark_node)
11254 return error_mark_node;
11255 /* then-else branches */
11256 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11257 if (TREE_OPERAND (node, 1) == error_mark_node)
11258 return error_mark_node;
11259 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11260 if (TREE_OPERAND (node, 2) == error_mark_node)
11261 return error_mark_node;
11262 return patch_if_else_statement (node);
11263 break;
11264
22eed1e6
APB
11265 case CONDITIONAL_EXPR:
11266 /* Condition */
11267 wfl_op1 = TREE_OPERAND (node, 0);
11268 COMPLETE_CHECK_OP_0 (node);
11269 wfl_op2 = TREE_OPERAND (node, 1);
11270 COMPLETE_CHECK_OP_1 (node);
11271 wfl_op3 = TREE_OPERAND (node, 2);
11272 COMPLETE_CHECK_OP_2 (node);
11273 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11274
e04a16fb
AG
11275 /* 3- Expression section */
11276 case COMPOUND_EXPR:
15fdcfe9 11277 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11278 TREE_OPERAND (node, 0) = nn =
11279 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11280 if (wfl_op2 == empty_stmt_node)
11281 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11282 else
15fdcfe9 11283 {
dc0b3eff 11284 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11285 {
dc0b3eff
PB
11286 /* An unreachable condition in a do-while statement
11287 is *not* (technically) an unreachable statement. */
11288 nn = wfl_op2;
11289 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11290 nn = EXPR_WFL_NODE (nn);
11291 if (TREE_CODE (nn) != EXIT_EXPR)
11292 {
11293 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11294 parse_error_context (wfl_operator, "Unreachable statement");
11295 }
bccaf73a 11296 }
dc0b3eff
PB
11297 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11298 if (TREE_OPERAND (node, 1) == error_mark_node)
11299 return error_mark_node;
11300 CAN_COMPLETE_NORMALLY (node)
11301 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11302 }
e04a16fb
AG
11303 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11304 break;
11305
11306 case RETURN_EXPR:
15fdcfe9 11307 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11308 return patch_return (node);
11309
11310 case EXPR_WITH_FILE_LOCATION:
11311 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11312 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11313 {
5423609c 11314 tree wfl = node;
15fdcfe9 11315 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11316 if (node == error_mark_node)
11317 return node;
5423609c
APB
11318 /* Keep line number information somewhere were it doesn't
11319 disrupt the completion process. */
2c56429a 11320 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11321 {
11322 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11323 TREE_OPERAND (node, 1) = wfl;
11324 }
15fdcfe9
PB
11325 CAN_COMPLETE_NORMALLY (node) = 1;
11326 }
e04a16fb
AG
11327 else
11328 {
5b09b33e
PB
11329 tree body;
11330 int save_lineno = lineno;
11331 lineno = EXPR_WFL_LINENO (node);
11332 body = java_complete_tree (EXPR_WFL_NODE (node));
11333 lineno = save_lineno;
15fdcfe9 11334 EXPR_WFL_NODE (node) = body;
dc0b3eff 11335 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11336 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
00b4575d 11337 if (body == empty_stmt_node || TREE_CONSTANT (body))
cd9643f7 11338 {
00b4575d 11339 /* Makes it easier to constant fold, detect empty bodies. */
cd9643f7
PB
11340 return body;
11341 }
dc0b3eff 11342 if (body == error_mark_node)
e04a16fb
AG
11343 {
11344 /* Its important for the evaluation of assignment that
11345 this mark on the TREE_TYPE is propagated. */
11346 TREE_TYPE (node) = error_mark_node;
11347 return error_mark_node;
11348 }
11349 else
11350 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11351
e04a16fb
AG
11352 }
11353 break;
11354
b67d701b 11355 case NEW_ARRAY_EXPR:
e04a16fb
AG
11356 /* Patch all the dimensions */
11357 flag = 0;
11358 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11359 {
11360 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11361 tree dim = convert (int_type_node,
11362 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11363 if (dim == error_mark_node)
11364 {
11365 flag = 1;
11366 continue;
11367 }
11368 else
11369 {
b9f7e36c 11370 TREE_VALUE (cn) = dim;
e04a16fb
AG
11371 /* Setup the location of the current dimension, for
11372 later error report. */
11373 TREE_PURPOSE (cn) =
11374 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11375 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11376 }
11377 }
11378 /* They complete the array creation expression, if no errors
11379 were found. */
15fdcfe9 11380 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11381 return (flag ? error_mark_node
11382 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11383
c2952b01
APB
11384 case NEW_ANONYMOUS_ARRAY_EXPR:
11385 /* Create the array type if necessary. */
11386 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11387 {
11388 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11389 if (!(type = resolve_type_during_patch (type)))
11390 return error_mark_node;
11391 type = build_array_from_name (type, NULL_TREE,
11392 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11393 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11394 }
11395 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11396 ANONYMOUS_ARRAY_INITIALIZER (node));
11397 if (node == error_mark_node)
11398 return error_mark_node;
11399 CAN_COMPLETE_NORMALLY (node) = 1;
11400 return node;
11401
b67d701b 11402 case NEW_CLASS_EXPR:
e04a16fb 11403 case CALL_EXPR:
b67d701b 11404 /* Complete function's argument(s) first */
e04a16fb
AG
11405 if (complete_function_arguments (node))
11406 return error_mark_node;
11407 else
b9f7e36c 11408 {
22eed1e6
APB
11409 tree decl, wfl = TREE_OPERAND (node, 0);
11410 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11411
c877974e 11412 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11413 NULL_TREE, 0, &decl);
c877974e
APB
11414 if (node == error_mark_node)
11415 return error_mark_node;
11416
11417 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11418 /* If we call this(...), register signature and positions */
11419 if (in_this)
11420 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11421 tree_cons (wfl, decl,
11422 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11423 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11424 return force_evaluation_order (node);
b9f7e36c 11425 }
e04a16fb
AG
11426
11427 case MODIFY_EXPR:
11428 /* Save potential wfls */
11429 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11430 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11431
cd9643f7
PB
11432 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11433 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11434 && DECL_INITIAL (nn) != NULL_TREE)
11435 {
100f7cd8
APB
11436 tree value;
11437
100f7cd8 11438 value = fold_constant_for_init (nn, nn);
c2952b01 11439
cd9643f7
PB
11440 if (value != NULL_TREE)
11441 {
11442 tree type = TREE_TYPE (value);
c2952b01
APB
11443 if (JPRIMITIVE_TYPE_P (type) ||
11444 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11445 return empty_stmt_node;
11446 }
629d4b4d
APB
11447 if (! flag_emit_class_files)
11448 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11449 if (CLASS_FINAL_VARIABLE_P (nn))
11450 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11451 }
e04a16fb 11452 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11453
e04a16fb
AG
11454 if (TREE_OPERAND (node, 0) == error_mark_node)
11455 return error_mark_node;
11456
5cbdba64
APB
11457 flag = COMPOUND_ASSIGN_P (wfl_op2);
11458 if (flag)
e04a16fb 11459 {
c2952b01
APB
11460 /* This might break when accessing outer field from inner
11461 class. TESTME, FIXME */
2aa11e97 11462 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11463
11464 /* Hand stablize the lhs on both places */
e04a16fb 11465 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11466 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11467 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11468
5cbdba64 11469 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11470 /* Now complete the RHS. We write it back later on. */
11471 nn = java_complete_tree (TREE_OPERAND (node, 1));
11472
642f15d1
APB
11473 if ((cn = patch_string (nn)))
11474 nn = cn;
11475
2aa11e97
APB
11476 /* The last part of the rewrite for E1 op= E2 is to have
11477 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11478 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11479 TREE_TYPE (lvalue), nn));
5cbdba64 11480
568aac9c
TT
11481 /* If the assignment is compound and has reference type,
11482 then ensure the LHS has type String and nothing else. */
11483 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11484 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11485 parse_error_context (wfl_op2,
11486 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11487 lang_printable_name (TREE_TYPE (lvalue), 0));
11488
5cbdba64 11489 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11490 }
11491
f8976021 11492 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11493 function to complete this RHS. Note that a NEW_ARRAY_INIT
11494 might have been already fully expanded if created as a result
11495 of processing an anonymous array initializer. We avoid doing
11496 the operation twice by testing whether the node already bears
11497 a type. */
11498 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11499 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11500 TREE_OPERAND (node, 1));
2aa11e97 11501 /* Otherwise we simply complete the RHS */
f8976021
APB
11502 else
11503 nn = java_complete_tree (TREE_OPERAND (node, 1));
11504
e04a16fb 11505 if (nn == error_mark_node)
c0d87ff6 11506 return error_mark_node;
2aa11e97
APB
11507
11508 /* Write back the RHS as we evaluated it. */
e04a16fb 11509 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11510
11511 /* In case we're handling = with a String as a RHS, we need to
11512 produce a String out of the RHS (it might still be a
11513 STRING_CST or a StringBuffer at this stage */
11514 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11515 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11516
11517 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11518 TREE_OPERAND (node, 1))))
11519 {
11520 /* We return error_mark_node if outer_field_access_fix
11521 detects we write into a final. */
11522 if (nn == error_mark_node)
11523 return error_mark_node;
11524 node = nn;
11525 }
11526 else
11527 {
11528 node = patch_assignment (node, wfl_op1, wfl_op2);
11529 /* Reorganize the tree if necessary. */
11530 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11531 || JSTRING_P (TREE_TYPE (node))))
11532 node = java_refold (node);
11533 }
11534
15fdcfe9
PB
11535 CAN_COMPLETE_NORMALLY (node) = 1;
11536 return node;
e04a16fb
AG
11537
11538 case MULT_EXPR:
11539 case PLUS_EXPR:
11540 case MINUS_EXPR:
11541 case LSHIFT_EXPR:
11542 case RSHIFT_EXPR:
11543 case URSHIFT_EXPR:
11544 case BIT_AND_EXPR:
11545 case BIT_XOR_EXPR:
11546 case BIT_IOR_EXPR:
11547 case TRUNC_MOD_EXPR:
c2952b01 11548 case TRUNC_DIV_EXPR:
e04a16fb
AG
11549 case RDIV_EXPR:
11550 case TRUTH_ANDIF_EXPR:
11551 case TRUTH_ORIF_EXPR:
11552 case EQ_EXPR:
11553 case NE_EXPR:
11554 case GT_EXPR:
11555 case GE_EXPR:
11556 case LT_EXPR:
11557 case LE_EXPR:
11558 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11559 knows how to handle those cases. */
11560 wfl_op1 = TREE_OPERAND (node, 0);
11561 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11562
15fdcfe9 11563 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11564 /* Don't complete string nodes if dealing with the PLUS operand. */
11565 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11566 {
11567 nn = java_complete_tree (wfl_op1);
11568 if (nn == error_mark_node)
11569 return error_mark_node;
48a840d9 11570
2aa11e97
APB
11571 TREE_OPERAND (node, 0) = nn;
11572 }
b67d701b 11573 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11574 {
11575 nn = java_complete_tree (wfl_op2);
11576 if (nn == error_mark_node)
11577 return error_mark_node;
48a840d9 11578
2aa11e97
APB
11579 TREE_OPERAND (node, 1) = nn;
11580 }
dc0b3eff 11581 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11582
5e942c50
APB
11583 case INSTANCEOF_EXPR:
11584 wfl_op1 = TREE_OPERAND (node, 0);
11585 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11586 if (flag_emit_xref)
11587 {
11588 TREE_TYPE (node) = boolean_type_node;
11589 return node;
11590 }
5e942c50
APB
11591 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11592
b67d701b 11593 case UNARY_PLUS_EXPR:
e04a16fb
AG
11594 case NEGATE_EXPR:
11595 case TRUTH_NOT_EXPR:
11596 case BIT_NOT_EXPR:
11597 case PREDECREMENT_EXPR:
11598 case PREINCREMENT_EXPR:
11599 case POSTDECREMENT_EXPR:
11600 case POSTINCREMENT_EXPR:
11601 case CONVERT_EXPR:
11602 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11603 how to handle those cases. */
11604 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11605 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11606 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11607 if (TREE_OPERAND (node, 0) == error_mark_node)
11608 return error_mark_node;
4a5f66c3
APB
11609 node = patch_unaryop (node, wfl_op1);
11610 CAN_COMPLETE_NORMALLY (node) = 1;
11611 break;
e04a16fb
AG
11612
11613 case ARRAY_REF:
11614 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11615 how to handle those cases. */
11616 wfl_op1 = TREE_OPERAND (node, 0);
11617 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11618 if (TREE_OPERAND (node, 0) == error_mark_node)
11619 return error_mark_node;
7f1d4866 11620 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11621 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11622 /* The same applies to wfl_op2 */
11623 wfl_op2 = TREE_OPERAND (node, 1);
11624 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11625 if (TREE_OPERAND (node, 1) == error_mark_node)
11626 return error_mark_node;
7f1d4866 11627 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11628 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11629 return patch_array_ref (node);
e04a16fb 11630
63a212ed
PB
11631 case RECORD_TYPE:
11632 return node;;
11633
11634 case COMPONENT_REF:
11635 /* The first step in the re-write of qualified name handling. FIXME.
11636 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11637 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11638 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11639 {
11640 tree name = TREE_OPERAND (node, 1);
11641 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11642 if (field == NULL_TREE)
11643 {
11644 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11645 return error_mark_node;
11646 }
11647 if (! FIELD_STATIC (field))
11648 {
11649 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11650 return error_mark_node;
11651 }
11652 return field;
11653 }
11654 else
400500c4 11655 abort ();
9bbc7d9f 11656 break;
9bbc7d9f 11657
b67d701b 11658 case THIS_EXPR:
e04a16fb
AG
11659 /* Can't use THIS in a static environment */
11660 if (!current_this)
11661 {
11662 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11663 parse_error_context (wfl_operator,
11664 "Keyword `this' used outside allowed context");
e04a16fb
AG
11665 TREE_TYPE (node) = error_mark_node;
11666 return error_mark_node;
11667 }
22eed1e6
APB
11668 if (ctxp->explicit_constructor_p)
11669 {
11670 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11671 parse_error_context
781b0558 11672 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11673 TREE_TYPE (node) = error_mark_node;
11674 return error_mark_node;
11675 }
e04a16fb 11676 return current_this;
c2952b01
APB
11677
11678 case CLASS_LITERAL:
11679 CAN_COMPLETE_NORMALLY (node) = 1;
11680 node = patch_incomplete_class_ref (node);
11681 if (node == error_mark_node)
11682 return error_mark_node;
11683 break;
11684
11685 case INSTANCE_INITIALIZERS_EXPR:
11686 in_instance_initializer++;
11687 node = java_complete_tree (TREE_OPERAND (node, 0));
11688 in_instance_initializer--;
11689 if (node != error_mark_node)
11690 TREE_TYPE (node) = void_type_node;
11691 else
11692 return error_mark_node;
11693 break;
e04a16fb 11694
e04a16fb 11695 default:
15fdcfe9 11696 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11697 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11698 and it's time to turn it into the appropriate String object */
11699 if ((nn = patch_string (node)))
11700 node = nn;
11701 else
400500c4 11702 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11703 }
11704 return node;
11705}
11706
11707/* Complete function call's argument. Return a non zero value is an
11708 error was found. */
11709
11710static int
11711complete_function_arguments (node)
11712 tree node;
11713{
11714 int flag = 0;
11715 tree cn;
11716
f63991a8 11717 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11718 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11719 {
b67d701b 11720 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11721 parm = java_complete_tree (wfl);
c2952b01 11722
e04a16fb
AG
11723 if (parm == error_mark_node)
11724 {
11725 flag = 1;
11726 continue;
11727 }
b67d701b
PB
11728 /* If have a string literal that we haven't transformed yet or a
11729 crafted string buffer, as a result of use of the the String
11730 `+' operator. Build `parm.toString()' and expand it. */
11731 if ((temp = patch_string (parm)))
b9f7e36c 11732 parm = temp;
5e942c50
APB
11733 /* Inline PRIMTYPE.TYPE read access */
11734 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11735
5e942c50 11736 TREE_VALUE (cn) = parm;
e04a16fb 11737 }
f63991a8 11738 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11739 return flag;
11740}
11741
11742/* Sometimes (for loops and variable initialized during their
11743 declaration), we want to wrap a statement around a WFL and turn it
11744 debugable. */
11745
11746static tree
11747build_debugable_stmt (location, stmt)
11748 int location;
11749 tree stmt;
11750{
11751 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11752 {
11753 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11754 EXPR_WFL_LINECOL (stmt) = location;
11755 }
11756 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11757 return stmt;
11758}
11759
11760static tree
11761build_expr_block (body, decls)
11762 tree body, decls;
11763{
11764 tree node = make_node (BLOCK);
11765 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11766 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11767 if (body)
11768 TREE_TYPE (node) = TREE_TYPE (body);
11769 TREE_SIDE_EFFECTS (node) = 1;
11770 return node;
11771}
11772
b67d701b
PB
11773/* Create a new function block and link it approriately to current
11774 function block chain */
e04a16fb
AG
11775
11776static tree
11777enter_block ()
11778{
b67d701b
PB
11779 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11780}
11781
11782/* Link block B supercontext to the previous block. The current
11783 function DECL is used as supercontext when enter_a_block is called
11784 for the first time for a given function. The current function body
11785 (DECL_FUNCTION_BODY) is set to be block B. */
11786
11787static tree
11788enter_a_block (b)
11789 tree b;
11790{
e04a16fb
AG
11791 tree fndecl = current_function_decl;
11792
f099f336
APB
11793 if (!fndecl) {
11794 BLOCK_SUPERCONTEXT (b) = current_static_block;
11795 current_static_block = b;
11796 }
11797
11798 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11799 {
11800 BLOCK_SUPERCONTEXT (b) = fndecl;
11801 DECL_FUNCTION_BODY (fndecl) = b;
11802 }
11803 else
11804 {
11805 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11806 DECL_FUNCTION_BODY (fndecl) = b;
11807 }
11808 return b;
11809}
11810
11811/* Exit a block by changing the current function body
11812 (DECL_FUNCTION_BODY) to the current block super context, only if
11813 the block being exited isn't the method's top level one. */
11814
11815static tree
11816exit_block ()
11817{
f099f336
APB
11818 tree b;
11819 if (current_function_decl)
11820 {
11821 b = DECL_FUNCTION_BODY (current_function_decl);
11822 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11823 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11824 }
11825 else
11826 {
11827 b = current_static_block;
e04a16fb 11828
f099f336
APB
11829 if (BLOCK_SUPERCONTEXT (b))
11830 current_static_block = BLOCK_SUPERCONTEXT (b);
11831 }
e04a16fb
AG
11832 return b;
11833}
11834
11835/* Lookup for NAME in the nested function's blocks, all the way up to
11836 the current toplevel one. It complies with Java's local variable
11837 scoping rules. */
11838
11839static tree
11840lookup_name_in_blocks (name)
11841 tree name;
11842{
f099f336 11843 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11844
11845 while (b != current_function_decl)
11846 {
11847 tree current;
11848
11849 /* Paranoid sanity check. To be removed */
11850 if (TREE_CODE (b) != BLOCK)
400500c4 11851 abort ();
e04a16fb
AG
11852
11853 for (current = BLOCK_EXPR_DECLS (b); current;
11854 current = TREE_CHAIN (current))
11855 if (DECL_NAME (current) == name)
11856 return current;
11857 b = BLOCK_SUPERCONTEXT (b);
11858 }
11859 return NULL_TREE;
11860}
11861
11862static void
11863maybe_absorb_scoping_blocks ()
11864{
f099f336 11865 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11866 {
11867 tree b = exit_block ();
11868 java_method_add_stmt (current_function_decl, b);
11869 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11870 }
11871}
11872
11873\f
11874/* This section of the source is reserved to build_* functions that
11875 are building incomplete tree nodes and the patch_* functions that
11876 are completing them. */
11877
c2952b01
APB
11878/* Wrap a non WFL node around a WFL. */
11879static tree
9a7ab4b3 11880build_wfl_wrap (node, location)
c2952b01 11881 tree node;
9a7ab4b3 11882 int location;
c2952b01
APB
11883{
11884 tree wfl, node_to_insert = node;
11885
11886 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11887 with the way we're processing SUPER. A THIS from a primary as a
11888 different form than a SUPER. Turn THIS into something symbolic */
11889 if (TREE_CODE (node) == THIS_EXPR)
11890 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11891 else
11892 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11893
9a7ab4b3 11894 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11895 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11896 return wfl;
11897}
11898
11899
9bbc7d9f 11900/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11901 we're currently dealing with the class java.lang.Object. */
11902
11903static tree
e920ebc9
APB
11904build_super_invocation (mdecl)
11905 tree mdecl;
22eed1e6 11906{
e920ebc9 11907 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11908 return empty_stmt_node;
22eed1e6
APB
11909 else
11910 {
9ee9b555 11911 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11912 tree a = NULL_TREE, t;
11913 /* If we're dealing with an anonymous class, pass the arguments
11914 of the crafted constructor along. */
11915 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11916 {
11917 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11918 for (; t != end_params_node; t = TREE_CHAIN (t))
11919 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11920 }
11921 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11922 }
11923}
11924
11925/* Build a SUPER/THIS qualified method invocation. */
11926
11927static tree
11928build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11929 int use_this;
11930 tree name, args;
11931 int lloc, rloc;
22eed1e6
APB
11932{
11933 tree invok;
11934 tree wfl =
9ee9b555 11935 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11936 EXPR_WFL_LINECOL (wfl) = lloc;
11937 invok = build_method_invocation (name, args);
11938 return make_qualified_primary (wfl, invok, rloc);
11939}
11940
b67d701b 11941/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11942
11943static tree
11944build_method_invocation (name, args)
11945 tree name;
11946 tree args;
11947{
11948 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11949 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11950 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11951 return call;
11952}
11953
11954/* Build an incomplete new xxx(...) node. */
11955
11956static tree
11957build_new_invocation (name, args)
11958 tree name, args;
11959{
11960 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11961 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11962 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11963 return call;
11964}
11965
11966/* Build an incomplete assignment expression. */
11967
11968static tree
11969build_assignment (op, op_location, lhs, rhs)
11970 int op, op_location;
11971 tree lhs, rhs;
11972{
11973 tree assignment;
11974 /* Build the corresponding binop if we deal with a Compound
11975 Assignment operator. Mark the binop sub-tree as part of a
11976 Compound Assignment expression */
11977 if (op != ASSIGN_TK)
11978 {
11979 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11980 COMPOUND_ASSIGN_P (rhs) = 1;
11981 }
11982 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11983 TREE_SIDE_EFFECTS (assignment) = 1;
11984 EXPR_WFL_LINECOL (assignment) = op_location;
11985 return assignment;
11986}
11987
11988/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11989
15fdcfe9 11990char *
e04a16fb
AG
11991print_int_node (node)
11992 tree node;
11993{
11994 static char buffer [80];
11995 if (TREE_CONSTANT_OVERFLOW (node))
11996 sprintf (buffer, "<overflow>");
11997
11998 if (TREE_INT_CST_HIGH (node) == 0)
11999 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12000 TREE_INT_CST_LOW (node));
12001 else if (TREE_INT_CST_HIGH (node) == -1
12002 && TREE_INT_CST_LOW (node) != 0)
12003 {
12004 buffer [0] = '-';
12005 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12006 -TREE_INT_CST_LOW (node));
12007 }
12008 else
12009 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12010 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12011
12012 return buffer;
12013}
12014
c7303e41
APB
12015\f
12016
12017/* This section of the code handle assignment check with FINAL
12018 variables. */
12019
12020static void
12021reset_static_final_variable_assignment_flag (class)
12022 tree class;
12023{
12024 tree field;
12025 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12026 if (CLASS_FINAL_VARIABLE_P (field))
12027 DECL_FIELD_FINAL_LIIC (field) = 0;
12028}
12029
12030/* Figure whether all final static variable have been initialized. */
12031
12032static void
12033check_static_final_variable_assignment_flag (class)
12034 tree class;
12035{
12036 tree field;
12037
12038 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12039 if (CLASS_FINAL_VARIABLE_P (field)
12040 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12041 parse_error_context
12042 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12043 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12044 IDENTIFIER_POINTER (DECL_NAME (field)));
12045}
12046
12047/* This function marks all final variable locally unassigned. */
12048
12049static void
12050reset_final_variable_local_assignment_flag (class)
12051 tree class;
12052{
12053 tree field;
12054 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12055 if (FINAL_VARIABLE_P (field))
12056 DECL_FIELD_FINAL_LIIC (field) = 0;
12057}
12058
12059/* Figure whether all final variables have beem initialized in MDECL
12060 and mark MDECL accordingly. */
12061
12062static void
12063check_final_variable_local_assignment_flag (class, mdecl)
12064 tree class;
12065 tree mdecl;
12066{
12067 tree field;
12068 int initialized = 0;
12069 int non_initialized = 0;
12070
12071 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12072 return;
12073
12074 /* First find out whether all final variables or no final variable
12075 are initialized in this ctor. We don't take into account final
12076 variable that have been initialized upon declaration. */
12077 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12078 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12079 {
12080 if (DECL_FIELD_FINAL_LIIC (field))
12081 initialized++;
12082 else
12083 non_initialized++;
12084 }
12085
12086 /* There were no non initialized variable and no initialized variable.
12087 This ctor is fine. */
12088 if (!non_initialized && !initialized)
12089 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12090 /* If no variables have been initialized, that fine. We'll check
12091 later whether this ctor calls a constructor which initializes
12092 them. We mark the ctor as not initializing all its finals. */
12093 else if (initialized == 0)
12094 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12095 /* If we have a mixed bag, then we have a problem. We need to report
12096 all the variables we're not initializing. */
12097 else if (initialized && non_initialized)
12098 {
12099 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12100 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12101 if (FIELD_FINAL (field)
12102 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12103 {
12104 parse_error_context
12105 (lookup_cl (mdecl),
12106 "Blank final variable `%s' may not have been initialized in this constructor",
12107 IDENTIFIER_POINTER (DECL_NAME (field)));
12108 DECL_FIELD_FINAL_IERR (field) = 1;
12109 }
12110 }
12111 /* Otherwise we know this ctor is initializing all its final
12112 variable. We mark it so. */
12113 else
12114 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12115}
12116
12117/* This function recurses in a simple what through STMT and stops when
12118 it finds a constructor call. It then verifies that the called
12119 constructor initialized its final properly. Return 1 upon success,
12120 0 or -1 otherwise. */
12121
12122static int
12123check_final_variable_indirect_assignment (stmt)
12124 tree stmt;
12125{
12126 int res;
12127 switch (TREE_CODE (stmt))
12128 {
12129 case EXPR_WITH_FILE_LOCATION:
12130 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12131 case COMPOUND_EXPR:
12132 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12133 if (res)
12134 return res;
12135 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12136 case SAVE_EXPR:
12137 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12138 case CALL_EXPR:
12139 {
12140 tree decl = TREE_OPERAND (stmt, 0);
12141 tree fbody;
12142
12143 if (TREE_CODE (decl) != FUNCTION_DECL)
12144 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12145 if (TREE_CODE (decl) != FUNCTION_DECL)
400500c4 12146 abort ();
c7303e41
APB
12147 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12148 return 1;
12149 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12150 return -1;
12151 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12152 if (fbody == error_mark_node)
12153 return -1;
12154 fbody = BLOCK_EXPR_BODY (fbody);
12155 return check_final_variable_indirect_assignment (fbody);
12156 }
12157 default:
12158 break;
12159 }
12160 return 0;
12161}
12162
12163/* This is the last chance to catch a final variable initialization
12164 problem. This routine will report an error if a final variable was
12165 never (globally) initialized and never reported as not having been
12166 initialized properly. */
12167
12168static void
12169check_final_variable_global_assignment_flag (class)
12170 tree class;
12171{
12172 tree field, mdecl;
12173 int nnctor = 0;
12174
12175 /* We go through all natural ctors and see whether they're
12176 initializing all their final variables or not. */
12177 current_function_decl = NULL_TREE; /* For the error report. */
12178 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12179 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12180 {
12181 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12182 {
12183 /* It doesn't. Maybe it calls a constructor that initializes
12184 them. find out. */
12185 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12186 if (fbody == error_mark_node)
12187 continue;
12188 fbody = BLOCK_EXPR_BODY (fbody);
12189 if (check_final_variable_indirect_assignment (fbody) == 1)
12190 {
12191 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12192 nnctor++;
12193 }
12194 else
12195 parse_error_context
12196 (lookup_cl (mdecl),
12197 "Final variable initialization error in this constructor");
12198 }
12199 else
12200 nnctor++;
12201 }
12202
12203 /* Finally we catch final variables that never were initialized */
12204 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12205 if (FINAL_VARIABLE_P (field)
12206 /* If the field wasn't initialized upon declaration */
12207 && !DECL_FIELD_FINAL_IUD (field)
12208 /* There wasn't natural ctor in which the field could have been
12209 initialized */
12210 && !nnctor
12211 /* If we never reported a problem with this field */
12212 && !DECL_FIELD_FINAL_IERR (field))
12213 {
12214 current_function_decl = NULL;
12215 parse_error_context
12216 (DECL_FIELD_FINAL_WFL (field),
12217 "Final variable `%s' hasn't been initialized upon its declaration",
12218 IDENTIFIER_POINTER (DECL_NAME (field)));
12219 }
12220
12221}
12222
7f1d4866
APB
12223/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12224 context. */
5e942c50
APB
12225
12226static int
12227check_final_assignment (lvalue, wfl)
12228 tree lvalue, wfl;
12229{
c7303e41
APB
12230 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12231 return 0;
12232
12233 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12234 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12235 lvalue = TREE_OPERAND (lvalue, 1);
12236
c7303e41
APB
12237 if (!FIELD_FINAL (lvalue))
12238 return 0;
12239
12240 /* Now the logic. We can modify a final VARIABLE:
12241 1) in finit$, (its declaration was followed by an initialization,)
12242 2) consistently in each natural ctor, if it wasn't initialized in
12243 finit$ or once in <clinit>. In any other cases, an error should be
12244 reported. */
12245 if (DECL_FINIT_P (current_function_decl))
5e942c50 12246 {
c7303e41
APB
12247 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12248 return 0;
5e942c50 12249 }
c7303e41
APB
12250
12251 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12252 /* Only if it wasn't given a value upon initialization */
12253 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12254 /* If it was never assigned a value in this constructor */
12255 && !DECL_FIELD_FINAL_LIIC (lvalue))
12256 {
12257 /* Turn the locally assigned flag on, it will be checked later
12258 on to point out at discrepancies. */
12259 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12260 if (DECL_CLINIT_P (current_function_decl))
12261 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12262 return 0;
12263 }
12264
12265 /* Other problems should be reported right away. */
12266 parse_error_context
12267 (wfl, "Can't %sassign a value to the final variable `%s'",
12268 (FIELD_STATIC (lvalue) ? "re" : ""),
12269 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12270
12271 /* Note that static field can be initialized once and only once. */
12272 if (FIELD_STATIC (lvalue))
12273 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12274
12275 return 1;
5e942c50
APB
12276}
12277
12278/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12279 read. This is needed to avoid circularities in the implementation
12280 of these fields in libjava. */
12281
12282static tree
12283maybe_build_primttype_type_ref (rhs, wfl)
12284 tree rhs, wfl;
12285{
12286 tree to_return = NULL_TREE;
12287 tree rhs_type = TREE_TYPE (rhs);
12288 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12289 {
12290 tree n = TREE_OPERAND (rhs, 1);
12291 if (TREE_CODE (n) == VAR_DECL
12292 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12293 && rhs_type == class_ptr_type
12294 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12295 {
49f48c71 12296 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12297 if (!strncmp (self_name, "java.lang.", 10))
12298 to_return = build_primtype_type_ref (self_name);
12299 }
12300 }
12301 return (to_return ? to_return : rhs );
12302}
12303
e04a16fb
AG
12304/* 15.25 Assignment operators. */
12305
12306static tree
12307patch_assignment (node, wfl_op1, wfl_op2)
12308 tree node;
12309 tree wfl_op1;
12310 tree wfl_op2;
12311{
0a2138e2 12312 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12313 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12314 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12315 int error_found = 0;
12316 int lvalue_from_array = 0;
12317
c2952b01 12318 /* Can't assign to a (blank) final. */
5e942c50
APB
12319 if (check_final_assignment (lvalue, wfl_op1))
12320 error_found = 1;
e04a16fb
AG
12321
12322 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12323
12324 /* Lhs can be a named variable */
34f4db93 12325 if (JDECL_P (lvalue))
e04a16fb 12326 {
e04a16fb
AG
12327 lhs_type = TREE_TYPE (lvalue);
12328 }
12329 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12330 comment on reason why */
12331 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12332 {
12333 lhs_type = TREE_TYPE (lvalue);
12334 lvalue_from_array = 1;
12335 }
12336 /* Or a field access */
12337 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12338 lhs_type = TREE_TYPE (lvalue);
12339 /* Or a function return slot */
12340 else if (TREE_CODE (lvalue) == RESULT_DECL)
12341 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12342 /* Otherwise, we might want to try to write into an optimized static
12343 final, this is an of a different nature, reported further on. */
12344 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12345 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12346 {
6632dcdd 12347 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12348 {
12349 /* What we should do instead is resetting the all the flags
12350 previously set, exchange lvalue for llvalue and continue. */
12351 error_found = 1;
12352 return error_mark_node;
12353 }
12354 else
12355 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12356 }
12357 else
e04a16fb
AG
12358 {
12359 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12360 error_found = 1;
12361 }
12362
12363 rhs_type = TREE_TYPE (rhs);
b67d701b 12364 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12365 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12366
b67d701b 12367 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12368 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12369 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12370
12371 /* 15.25.2 If we have a compound assignment, convert RHS into the
12372 type of the LHS */
12373 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12374 new_rhs = convert (lhs_type, rhs);
12375
12376 /* Explicit cast required. This is an error */
12377 if (!new_rhs)
12378 {
c2e3db92
KG
12379 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12380 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12381 tree wfl;
12382 char operation [32]; /* Max size known */
12383
12384 /* If the assignment is part of a declaration, we use the WFL of
12385 the declared variable to point out the error and call it a
12386 declaration problem. If the assignment is a genuine =
12387 operator, we call is a operator `=' problem, otherwise we
12388 call it an assignment problem. In both of these last cases,
12389 we use the WFL of the operator to indicate the error. */
12390
12391 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12392 {
12393 wfl = wfl_op1;
12394 strcpy (operation, "declaration");
12395 }
12396 else
12397 {
12398 wfl = wfl_operator;
12399 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12400 strcpy (operation, "assignment");
12401 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12402 strcpy (operation, "`return'");
12403 else
12404 strcpy (operation, "`='");
12405 }
12406
1ebadc60 12407 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12408 parse_error_context
12409 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12410 operation, t1, t2);
1ebadc60 12411 else
781b0558 12412 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12413 operation, t1, t2);
e04a16fb
AG
12414 free (t1); free (t2);
12415 error_found = 1;
12416 }
12417
c877974e
APB
12418 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12419 if (new_rhs)
12420 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12421
e04a16fb
AG
12422 if (error_found)
12423 return error_mark_node;
12424
2622b947
APB
12425 /* 10.10: Array Store Exception runtime check */
12426 if (!flag_emit_class_files
e8fc7396 12427 && !flag_emit_xref
2622b947 12428 && lvalue_from_array
afc390b1 12429 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12430 {
12431 tree check;
12432 tree base = lvalue;
12433
12434 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12435 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12436 base = TREE_OPERAND (lvalue, 0);
12437 else
12438 {
12439 if (flag_bounds_check)
12440 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12441 else
12442 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12443 }
12444
12445 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12446 new_rhs = save_expr (new_rhs);
2622b947
APB
12447 check = build (CALL_EXPR, void_type_node,
12448 build_address_of (soft_checkarraystore_node),
12449 tree_cons (NULL_TREE, base,
12450 build_tree_list (NULL_TREE, new_rhs)),
12451 NULL_TREE);
12452 TREE_SIDE_EFFECTS (check) = 1;
12453
12454 /* We have to decide on an insertion point */
12455 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12456 {
12457 tree t;
12458 if (flag_bounds_check)
12459 {
12460 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12461 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12462 build (COMPOUND_EXPR, void_type_node, t, check);
12463 }
12464 else
12465 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12466 check, TREE_OPERAND (lvalue, 1));
12467 }
12468 else
12469 {
12470 /* Make sure the bound check will happen before the store check */
12471 if (flag_bounds_check)
12472 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12473 build (COMPOUND_EXPR, void_type_node,
12474 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12475 else
12476 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12477 }
12478 }
22eed1e6 12479
34d4df06
APB
12480 /* Final locals can be used as case values in switch
12481 statement. Prepare them for this eventuality. */
12482 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12483 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12484 && TREE_CONSTANT (new_rhs)
12485 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12486 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12487 )
12488 {
12489 TREE_CONSTANT (lvalue) = 1;
12490 DECL_INITIAL (lvalue) = new_rhs;
12491 }
12492
e04a16fb
AG
12493 TREE_OPERAND (node, 0) = lvalue;
12494 TREE_OPERAND (node, 1) = new_rhs;
12495 TREE_TYPE (node) = lhs_type;
12496 return node;
12497}
12498
b67d701b
PB
12499/* Check that type SOURCE can be cast into type DEST. If the cast
12500 can't occur at all, return 0 otherwise 1. This function is used to
12501 produce accurate error messages on the reasons why an assignment
12502 failed. */
e04a16fb 12503
b67d701b
PB
12504static tree
12505try_reference_assignconv (lhs_type, rhs)
12506 tree lhs_type, rhs;
e04a16fb 12507{
b67d701b
PB
12508 tree new_rhs = NULL_TREE;
12509 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12510
b67d701b
PB
12511 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12512 {
12513 /* `null' may be assigned to any reference type */
12514 if (rhs == null_pointer_node)
12515 new_rhs = null_pointer_node;
12516 /* Try the reference assignment conversion */
12517 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12518 new_rhs = rhs;
12519 /* This is a magic assignment that we process differently */
12520 else if (rhs == soft_exceptioninfo_call_node)
12521 new_rhs = rhs;
12522 }
12523 return new_rhs;
12524}
12525
12526/* Check that RHS can be converted into LHS_TYPE by the assignment
12527 conversion (5.2), for the cases of RHS being a builtin type. Return
12528 NULL_TREE if the conversion fails or if because RHS isn't of a
12529 builtin type. Return a converted RHS if the conversion is possible. */
12530
12531static tree
12532try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12533 tree wfl_op1, lhs_type, rhs;
12534{
12535 tree new_rhs = NULL_TREE;
12536 tree rhs_type = TREE_TYPE (rhs);
12537
7e51098e
TT
12538 /* Handle boolean specially. */
12539 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12540 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12541 {
12542 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12543 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12544 new_rhs = rhs;
12545 }
12546
5e942c50 12547 /* Zero accepted everywhere */
7e51098e 12548 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12549 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12550 && JPRIMITIVE_TYPE_P (rhs_type))
12551 new_rhs = convert (lhs_type, rhs);
12552
b67d701b
PB
12553 /* 5.1.1 Try Identity Conversion,
12554 5.1.2 Try Widening Primitive Conversion */
5e942c50 12555 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12556 new_rhs = convert (lhs_type, rhs);
12557
12558 /* Try a narrowing primitive conversion (5.1.3):
12559 - expression is a constant expression of type int AND
12560 - variable is byte, short or char AND
12561 - The value of the expression is representable in the type of the
12562 variable */
12563 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12564 && (lhs_type == byte_type_node || lhs_type == char_type_node
12565 || lhs_type == short_type_node))
12566 {
12567 if (int_fits_type_p (rhs, lhs_type))
12568 new_rhs = convert (lhs_type, rhs);
12569 else if (wfl_op1) /* Might be called with a NULL */
12570 parse_warning_context
7e51098e 12571 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12572 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12573 /* Reported a warning that will turn into an error further
12574 down, so we don't return */
12575 }
12576
12577 return new_rhs;
12578}
12579
12580/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12581 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12582 0 is the conversion test fails. This implements parts the method
12583 invocation convertion (5.3). */
12584
12585static int
12586valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12587 tree lhs_type, rhs_type;
12588{
acd663ee 12589 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12590 if (lhs_type == rhs_type)
12591 return 1;
12592
7e51098e
TT
12593 /* Reject non primitive types and boolean conversions. */
12594 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12595 return 0;
12596
acd663ee
APB
12597 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12598 than a char can't be converted into a char. Short can't too, but
12599 the < test below takes care of that */
b67d701b
PB
12600 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12601 return 0;
12602
5e942c50
APB
12603 /* Accept all promoted type here. Note, we can't use <= in the test
12604 below, because we still need to bounce out assignments of short
12605 to char and the likes */
12606 if (lhs_type == int_type_node
12607 && (rhs_type == promoted_byte_type_node
12608 || rhs_type == promoted_short_type_node
12609 || rhs_type == promoted_char_type_node
12610 || rhs_type == promoted_boolean_type_node))
12611 return 1;
12612
acd663ee
APB
12613 /* From here, an integral is widened if its precision is smaller
12614 than the precision of the LHS or if the LHS is a floating point
12615 type, or the RHS is a float and the RHS a double. */
12616 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12617 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12618 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12619 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12620 return 1;
12621
12622 return 0;
e04a16fb
AG
12623}
12624
12625/* Check that something of SOURCE type can be assigned or cast to
12626 something of DEST type at runtime. Return 1 if the operation is
12627 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12628 were SOURCE is cast into DEST, which borrows a lot of the
12629 assignment check. */
12630
12631static int
12632valid_ref_assignconv_cast_p (source, dest, cast)
12633 tree source;
12634 tree dest;
12635 int cast;
12636{
09ed0f70
APB
12637 /* SOURCE or DEST might be null if not from a declared entity. */
12638 if (!source || !dest)
12639 return 0;
5e942c50
APB
12640 if (JNULLP_TYPE_P (source))
12641 return 1;
e04a16fb
AG
12642 if (TREE_CODE (source) == POINTER_TYPE)
12643 source = TREE_TYPE (source);
12644 if (TREE_CODE (dest) == POINTER_TYPE)
12645 dest = TREE_TYPE (dest);
c1eacb70
BM
12646
12647 /* If source and dest are being compiled from bytecode, they may need to
12648 be loaded. */
12649 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12650 {
12651 load_class (source, 1);
12652 safe_layout_class (source);
12653 }
12654 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12655 {
12656 load_class (dest, 1);
12657 safe_layout_class (dest);
12658 }
12659
e04a16fb
AG
12660 /* Case where SOURCE is a class type */
12661 if (TYPE_CLASS_P (source))
12662 {
12663 if (TYPE_CLASS_P (dest))
c2952b01
APB
12664 return (source == dest
12665 || inherits_from_p (source, dest)
c2952b01 12666 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12667 if (TYPE_INTERFACE_P (dest))
12668 {
12669 /* If doing a cast and SOURCE is final, the operation is
12670 always correct a compile time (because even if SOURCE
12671 does not implement DEST, a subclass of SOURCE might). */
12672 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12673 return 1;
12674 /* Otherwise, SOURCE must implement DEST */
12675 return interface_of_p (dest, source);
12676 }
12677 /* DEST is an array, cast permited if SOURCE is of Object type */
12678 return (cast && source == object_type_node ? 1 : 0);
12679 }
12680 if (TYPE_INTERFACE_P (source))
12681 {
12682 if (TYPE_CLASS_P (dest))
12683 {
12684 /* If not casting, DEST must be the Object type */
12685 if (!cast)
12686 return dest == object_type_node;
12687 /* We're doing a cast. The cast is always valid is class
12688 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12689 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12690 return 1;
12691 else
12692 return interface_of_p (source, dest);
12693 }
12694 if (TYPE_INTERFACE_P (dest))
12695 {
12696 /* If doing a cast, then if SOURCE and DEST contain method
12697 with the same signature but different return type, then
12698 this is a (compile time) error */
12699 if (cast)
12700 {
12701 tree method_source, method_dest;
12702 tree source_type;
0a2138e2 12703 tree source_sig;
e04a16fb
AG
12704 tree source_name;
12705 for (method_source = TYPE_METHODS (source); method_source;
12706 method_source = TREE_CHAIN (method_source))
12707 {
12708 source_sig =
12709 build_java_argument_signature (TREE_TYPE (method_source));
12710 source_type = TREE_TYPE (TREE_TYPE (method_source));
12711 source_name = DECL_NAME (method_source);
12712 for (method_dest = TYPE_METHODS (dest);
12713 method_dest; method_dest = TREE_CHAIN (method_dest))
12714 if (source_sig ==
12715 build_java_argument_signature (TREE_TYPE (method_dest))
12716 && source_name == DECL_NAME (method_dest)
12717 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12718 return 0;
12719 }
12720 return 1;
12721 }
12722 else
12723 return source == dest || interface_of_p (dest, source);
12724 }
ee17a290
TT
12725 else
12726 {
12727 /* Array */
12728 return (cast
12729 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12730 || (DECL_NAME (TYPE_NAME (source))
12731 == java_io_serializable)));
12732 }
e04a16fb
AG
12733 }
12734 if (TYPE_ARRAY_P (source))
12735 {
12736 if (TYPE_CLASS_P (dest))
12737 return dest == object_type_node;
09ed0f70 12738 /* Can't cast an array to an interface unless the interface is
ee17a290 12739 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12740 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12741 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12742 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12743 else /* Arrays */
12744 {
12745 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12746 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12747
b9f7e36c
APB
12748 /* In case of severe errors, they turn out null */
12749 if (!dest_element_type || !source_element_type)
12750 return 0;
e04a16fb
AG
12751 if (source_element_type == dest_element_type)
12752 return 1;
12753 return valid_ref_assignconv_cast_p (source_element_type,
12754 dest_element_type, cast);
12755 }
12756 return 0;
12757 }
12758 return 0;
12759}
12760
b67d701b
PB
12761static int
12762valid_cast_to_p (source, dest)
12763 tree source;
12764 tree dest;
12765{
12766 if (TREE_CODE (source) == POINTER_TYPE)
12767 source = TREE_TYPE (source);
12768 if (TREE_CODE (dest) == POINTER_TYPE)
12769 dest = TREE_TYPE (dest);
12770
12771 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12772 return valid_ref_assignconv_cast_p (source, dest, 1);
12773
12774 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12775 return 1;
12776
7e51098e
TT
12777 else if (TREE_CODE (source) == BOOLEAN_TYPE
12778 && TREE_CODE (dest) == BOOLEAN_TYPE)
12779 return 1;
12780
b67d701b
PB
12781 return 0;
12782}
12783
15fdcfe9
PB
12784static tree
12785do_unary_numeric_promotion (arg)
12786 tree arg;
12787{
12788 tree type = TREE_TYPE (arg);
7e51098e
TT
12789 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12790 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12791 arg = convert (int_type_node, arg);
12792 return arg;
12793}
12794
acd663ee
APB
12795/* Return a non zero value if SOURCE can be converted into DEST using
12796 the method invocation conversion rule (5.3). */
b67d701b
PB
12797static int
12798valid_method_invocation_conversion_p (dest, source)
12799 tree dest, source;
12800{
e3884b71 12801 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12802 && valid_builtin_assignconv_identity_widening_p (dest, source))
12803 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12804 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12805 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12806}
12807
e04a16fb
AG
12808/* Build an incomplete binop expression. */
12809
12810static tree
12811build_binop (op, op_location, op1, op2)
12812 enum tree_code op;
12813 int op_location;
12814 tree op1, op2;
12815{
5e942c50 12816 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12817 TREE_SIDE_EFFECTS (binop) = 1;
12818 /* Store the location of the operator, for better error report. The
12819 string of the operator will be rebuild based on the OP value. */
12820 EXPR_WFL_LINECOL (binop) = op_location;
12821 return binop;
12822}
12823
12824/* Build the string of the operator retained by NODE. If NODE is part
12825 of a compound expression, add an '=' at the end of the string. This
12826 function is called when an error needs to be reported on an
12827 operator. The string is returned as a pointer to a static character
12828 buffer. */
12829
12830static char *
12831operator_string (node)
12832 tree node;
12833{
12834#define BUILD_OPERATOR_STRING(S) \
12835 { \
12836 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12837 return buffer; \
12838 }
12839
12840 static char buffer [10];
12841 switch (TREE_CODE (node))
12842 {
12843 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12844 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12845 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12846 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12847 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12848 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12849 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12850 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12851 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12852 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12853 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12854 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12855 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12856 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12857 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12858 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12859 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12860 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12861 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12862 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12863 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12864 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12865 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12866 case PREINCREMENT_EXPR: /* Fall through */
12867 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12868 case PREDECREMENT_EXPR: /* Fall through */
12869 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12870 default:
400500c4
RK
12871 internal_error ("unregistered operator %s",
12872 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
12873 }
12874 return NULL;
12875#undef BUILD_OPERATOR_STRING
12876}
12877
5cbdba64
APB
12878/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12879
12880static int
12881java_decl_equiv (var_acc1, var_acc2)
12882 tree var_acc1, var_acc2;
12883{
12884 if (JDECL_P (var_acc1))
12885 return (var_acc1 == var_acc2);
12886
12887 return (TREE_CODE (var_acc1) == COMPONENT_REF
12888 && TREE_CODE (var_acc2) == COMPONENT_REF
12889 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12890 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12891 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12892}
12893
12894/* Return a non zero value if CODE is one of the operators that can be
12895 used in conjunction with the `=' operator in a compound assignment. */
12896
12897static int
12898binop_compound_p (code)
12899 enum tree_code code;
12900{
12901 int i;
12902 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12903 if (binop_lookup [i] == code)
12904 break;
12905
12906 return i < BINOP_COMPOUND_CANDIDATES;
12907}
12908
12909/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12910
12911static tree
12912java_refold (t)
12913 tree t;
12914{
12915 tree c, b, ns, decl;
12916
12917 if (TREE_CODE (t) != MODIFY_EXPR)
12918 return t;
12919
12920 c = TREE_OPERAND (t, 1);
12921 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12922 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12923 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12924 return t;
12925
12926 /* Now the left branch of the binary operator. */
12927 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12928 if (! (b && TREE_CODE (b) == NOP_EXPR
12929 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12930 return t;
12931
12932 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12933 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12934 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12935 return t;
12936
12937 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12938 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12939 /* It's got to be the an equivalent decl */
12940 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12941 {
12942 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12943 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12944 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12945 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12946 /* Change the right part of the BINOP_EXPR */
12947 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12948 }
12949
12950 return t;
12951}
12952
e04a16fb
AG
12953/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12954 errors but we modify NODE so that it contains the type computed
12955 according to the expression, when it's fixed. Otherwise, we write
12956 error_mark_node as the type. It allows us to further the analysis
12957 of remaining nodes and detects more errors in certain cases. */
12958
12959static tree
12960patch_binop (node, wfl_op1, wfl_op2)
12961 tree node;
12962 tree wfl_op1;
12963 tree wfl_op2;
12964{
12965 tree op1 = TREE_OPERAND (node, 0);
12966 tree op2 = TREE_OPERAND (node, 1);
12967 tree op1_type = TREE_TYPE (op1);
12968 tree op2_type = TREE_TYPE (op2);
48a840d9 12969 tree prom_type = NULL_TREE, cn;
e04a16fb 12970 int code = TREE_CODE (node);
b67d701b 12971
e04a16fb
AG
12972 /* If 1, tell the routine that we have to return error_mark_node
12973 after checking for the initialization of the RHS */
12974 int error_found = 0;
12975
e04a16fb
AG
12976 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12977
761491c8
APB
12978 /* If either op<n>_type are NULL, this might be early signs of an
12979 error situation, unless it's too early to tell (in case we're
12980 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
12981 correctly so the error can be later on reported accurately. */
12982 if (! (code == PLUS_EXPR || code == NE_EXPR
12983 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
12984 {
12985 tree n;
12986 if (! op1_type)
12987 {
12988 n = java_complete_tree (op1);
12989 op1_type = TREE_TYPE (n);
12990 }
12991 if (! op2_type)
12992 {
12993 n = java_complete_tree (op2);
12994 op2_type = TREE_TYPE (n);
12995 }
12996 }
12997
e04a16fb
AG
12998 switch (code)
12999 {
13000 /* 15.16 Multiplicative operators */
13001 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13002 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13003 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13004 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13005 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13006 {
7e51098e 13007 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13008 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13009 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13010 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13011 TREE_TYPE (node) = error_mark_node;
13012 error_found = 1;
13013 break;
13014 }
13015 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13016 /* Change the division operator if necessary */
13017 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13018 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13019
aa4759c1
AH
13020 if (TREE_CODE (prom_type) == INTEGER_TYPE
13021 && flag_use_divide_subroutine
13022 && ! flag_emit_class_files
13023 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13024 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13025
0b4d333e
APB
13026 /* This one is more complicated. FLOATs are processed by a
13027 function call to soft_fmod. Duplicate the value of the
13028 COMPOUND_ASSIGN_P flag. */
e04a16fb 13029 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13030 {
13031 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13032 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13033 TREE_SIDE_EFFECTS (mod)
13034 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13035 return mod;
13036 }
e04a16fb
AG
13037 break;
13038
13039 /* 15.17 Additive Operators */
13040 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13041
13042 /* Operation is valid if either one argument is a string
13043 constant, a String object or a StringBuffer crafted for the
13044 purpose of the a previous usage of the String concatenation
13045 operator */
13046
13047 if (TREE_CODE (op1) == STRING_CST
13048 || TREE_CODE (op2) == STRING_CST
13049 || JSTRING_TYPE_P (op1_type)
13050 || JSTRING_TYPE_P (op2_type)
13051 || IS_CRAFTED_STRING_BUFFER_P (op1)
13052 || IS_CRAFTED_STRING_BUFFER_P (op2))
13053 return build_string_concatenation (op1, op2);
13054
e04a16fb
AG
13055 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13056 Numeric Types */
7e51098e 13057 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13058 {
7e51098e 13059 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13060 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13061 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13062 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13063 TREE_TYPE (node) = error_mark_node;
13064 error_found = 1;
13065 break;
13066 }
13067 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13068 break;
13069
13070 /* 15.18 Shift Operators */
13071 case LSHIFT_EXPR:
13072 case RSHIFT_EXPR:
13073 case URSHIFT_EXPR:
13074 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13075 {
13076 if (!JINTEGRAL_TYPE_P (op1_type))
13077 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13078 else
1ebadc60 13079 {
7e51098e 13080 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13081 parse_error_context (wfl_operator,
781b0558 13082 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13083 operator_string (node),
13084 lang_printable_name (op2_type, 0));
13085 else
781b0558
KG
13086 parse_error_context (wfl_operator,
13087 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13088 operator_string (node),
13089 lang_printable_name (op2_type, 0));
13090 }
e04a16fb
AG
13091 TREE_TYPE (node) = error_mark_node;
13092 error_found = 1;
13093 break;
13094 }
13095
13096 /* Unary numeric promotion (5.6.1) is performed on each operand
13097 separatly */
15fdcfe9
PB
13098 op1 = do_unary_numeric_promotion (op1);
13099 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13100
13101 /* The type of the shift expression is the type of the promoted
13102 type of the left-hand operand */
13103 prom_type = TREE_TYPE (op1);
13104
c2952b01
APB
13105 /* Shift int only up to 0x1f and long up to 0x3f */
13106 if (prom_type == int_type_node)
13107 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13108 build_int_2 (0x1f, 0)));
13109 else
13110 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13111 build_int_2 (0x3f, 0)));
e04a16fb
AG
13112
13113 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13114 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13115 {
0b4d333e 13116 tree to_return;
73333a87
AH
13117 tree utype = unsigned_type (prom_type);
13118 op1 = convert (utype, op1);
e04a16fb 13119 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13120 TREE_OPERAND (node, 0) = op1;
13121 TREE_OPERAND (node, 1) = op2;
13122 TREE_TYPE (node) = utype;
0b4d333e
APB
13123 to_return = convert (prom_type, node);
13124 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13125 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13126 TREE_SIDE_EFFECTS (to_return)
13127 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13128 return to_return;
e04a16fb
AG
13129 }
13130 break;
5e942c50
APB
13131
13132 /* 15.19.1 Type Comparison Operator instaceof */
13133 case INSTANCEOF_EXPR:
13134
13135 TREE_TYPE (node) = boolean_type_node;
13136
13137 if (!(op2_type = resolve_type_during_patch (op2)))
13138 return error_mark_node;
13139
13140 /* The first operand must be a reference type or the null type */
13141 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13142 error_found = 1; /* Error reported further below */
13143
13144 /* The second operand must be a reference type */
13145 if (!JREFERENCE_TYPE_P (op2_type))
13146 {
13147 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13148 parse_error_context
13149 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13150 lang_printable_name (op2_type, 0));
13151 error_found = 1;
13152 }
13153
13154 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13155 {
13156 /* If the first operand is null, the result is always false */
13157 if (op1 == null_pointer_node)
13158 return boolean_false_node;
15fdcfe9
PB
13159 else if (flag_emit_class_files)
13160 {
13161 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13162 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13163 return node;
13164 }
5e942c50
APB
13165 /* Otherwise we have to invoke instance of to figure it out */
13166 else
67db0ce7 13167 return build_instanceof (op1, op2_type);
5e942c50
APB
13168 }
13169 /* There is no way the expression operand can be an instance of
13170 the type operand. This is a compile time error. */
13171 else
13172 {
c2e3db92 13173 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13174 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13175 parse_error_context
13176 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13177 t1, lang_printable_name (op2_type, 0));
13178 free (t1);
13179 error_found = 1;
13180 }
e04a16fb 13181
5e942c50 13182 break;
e04a16fb
AG
13183
13184 /* 15.21 Bitwise and Logical Operators */
13185 case BIT_AND_EXPR:
13186 case BIT_XOR_EXPR:
13187 case BIT_IOR_EXPR:
13188 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13189 /* Binary numeric promotion is performed on both operand and the
13190 expression retain that type */
13191 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13192
13193 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13194 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13195 /* The type of the bitwise operator expression is BOOLEAN */
13196 prom_type = boolean_type_node;
13197 else
13198 {
13199 if (!JINTEGRAL_TYPE_P (op1_type))
13200 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13201 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13202 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13203 TREE_TYPE (node) = error_mark_node;
13204 error_found = 1;
13205 /* Insert a break here if adding thing before the switch's
13206 break for this case */
13207 }
13208 break;
13209
13210 /* 15.22 Conditional-And Operator */
13211 case TRUTH_ANDIF_EXPR:
13212 /* 15.23 Conditional-Or Operator */
13213 case TRUTH_ORIF_EXPR:
13214 /* Operands must be of BOOLEAN type */
13215 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13216 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13217 {
13218 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13219 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13220 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13221 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13222 TREE_TYPE (node) = boolean_type_node;
13223 error_found = 1;
13224 break;
13225 }
13226 /* The type of the conditional operators is BOOLEAN */
13227 prom_type = boolean_type_node;
13228 break;
13229
13230 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13231 case LT_EXPR:
13232 case GT_EXPR:
13233 case LE_EXPR:
13234 case GE_EXPR:
13235 /* The type of each of the operands must be a primitive numeric
13236 type */
13237 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13238 {
13239 if (!JNUMERIC_TYPE_P (op1_type))
13240 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13241 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13242 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13243 TREE_TYPE (node) = boolean_type_node;
13244 error_found = 1;
13245 break;
13246 }
13247 /* Binary numeric promotion is performed on the operands */
13248 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13249 /* The type of the relation expression is always BOOLEAN */
13250 prom_type = boolean_type_node;
13251 break;
13252
13253 /* 15.20 Equality Operator */
13254 case EQ_EXPR:
13255 case NE_EXPR:
48a840d9
APB
13256 /* It's time for us to patch the strings. */
13257 if ((cn = patch_string (op1)))
13258 {
13259 op1 = cn;
13260 op1_type = TREE_TYPE (op1);
13261 }
13262 if ((cn = patch_string (op2)))
13263 {
13264 op2 = cn;
13265 op2_type = TREE_TYPE (op2);
13266 }
13267
e04a16fb
AG
13268 /* 15.20.1 Numerical Equality Operators == and != */
13269 /* Binary numeric promotion is performed on the operands */
5e942c50 13270 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13271 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13272
13273 /* 15.20.2 Boolean Equality Operators == and != */
13274 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13275 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13276 ; /* Nothing to do here */
13277
13278 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13279 /* Types have to be either references or the null type. If
13280 they're references, it must be possible to convert either
13281 type to the other by casting conversion. */
b9f7e36c
APB
13282 else if (op1 == null_pointer_node || op2 == null_pointer_node
13283 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13284 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13285 || valid_ref_assignconv_cast_p (op2_type,
13286 op1_type, 1))))
e04a16fb
AG
13287 ; /* Nothing to do here */
13288
13289 /* Else we have an error figure what can't be converted into
13290 what and report the error */
13291 else
13292 {
13293 char *t1;
c2e3db92 13294 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13295 parse_error_context
781b0558
KG
13296 (wfl_operator,
13297 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13298 operator_string (node), t1,
0a2138e2 13299 lang_printable_name (op2_type, 0));
e04a16fb
AG
13300 free (t1);
13301 TREE_TYPE (node) = boolean_type_node;
13302 error_found = 1;
13303 break;
13304 }
13305 prom_type = boolean_type_node;
13306 break;
13307 }
13308
e04a16fb
AG
13309 if (error_found)
13310 return error_mark_node;
13311
13312 TREE_OPERAND (node, 0) = op1;
13313 TREE_OPERAND (node, 1) = op2;
13314 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13315 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13316
ce6e9147
APB
13317 if (flag_emit_xref)
13318 return node;
13319
d1472141
PB
13320 /* fold does not respect side-effect order as required for Java but not C.
13321 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13322 * bytecode.
13323 */
13324 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13325 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13326 node = fold (node);
13327 return node;
e04a16fb
AG
13328}
13329
b67d701b
PB
13330/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13331 zero value, the value of CSTE comes after the valude of STRING */
13332
13333static tree
13334do_merge_string_cste (cste, string, string_len, after)
13335 tree cste;
49f48c71 13336 const char *string;
b67d701b
PB
13337 int string_len, after;
13338{
49f48c71 13339 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13340 int old_len = TREE_STRING_LENGTH (cste);
13341 int len = old_len + string_len;
520a57c8 13342 char *new = alloca (len+1);
354e99ce 13343
b67d701b
PB
13344 if (after)
13345 {
354e99ce
APB
13346 memcpy (new, string, string_len);
13347 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13348 }
13349 else
13350 {
354e99ce
APB
13351 memcpy (new, old, old_len);
13352 memcpy (&new [old_len], string, string_len);
b67d701b 13353 }
354e99ce 13354 new [len] = '\0';
520a57c8 13355 return build_string (len, new);
b67d701b
PB
13356}
13357
13358/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13359 new STRING_CST on success, NULL_TREE on failure */
13360
13361static tree
13362merge_string_cste (op1, op2, after)
13363 tree op1, op2;
13364 int after;
13365{
13366 /* Handle two string constants right away */
13367 if (TREE_CODE (op2) == STRING_CST)
13368 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13369 TREE_STRING_LENGTH (op2), after);
13370
13371 /* Reasonable integer constant can be treated right away */
13372 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13373 {
49f48c71
KG
13374 static const char *boolean_true = "true";
13375 static const char *boolean_false = "false";
13376 static const char *null_pointer = "null";
b67d701b 13377 char ch[3];
49f48c71 13378 const char *string;
b67d701b
PB
13379
13380 if (op2 == boolean_true_node)
13381 string = boolean_true;
13382 else if (op2 == boolean_false_node)
13383 string = boolean_false;
13384 else if (op2 == null_pointer_node)
13385 string = null_pointer;
13386 else if (TREE_TYPE (op2) == char_type_node)
13387 {
13388 ch[0] = (char )TREE_INT_CST_LOW (op2);
13389 ch[1] = '\0';
13390 string = ch;
13391 }
13392 else
13393 string = print_int_node (op2);
13394
13395 return do_merge_string_cste (op1, string, strlen (string), after);
13396 }
13397 return NULL_TREE;
13398}
13399
13400/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13401 has to be a STRING_CST and the other part must be a STRING_CST or a
13402 INTEGRAL constant. Return a new STRING_CST if the operation
13403 succeed, NULL_TREE otherwise.
13404
13405 If the case we want to optimize for space, we might want to return
13406 NULL_TREE for each invocation of this routine. FIXME */
13407
13408static tree
13409string_constant_concatenation (op1, op2)
13410 tree op1, op2;
13411{
13412 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13413 {
0a2138e2 13414 tree string, rest;
b67d701b
PB
13415 int invert;
13416
13417 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13418 rest = (string == op1 ? op2 : op1);
13419 invert = (string == op1 ? 0 : 1 );
13420
13421 /* Walk REST, only if it looks reasonable */
13422 if (TREE_CODE (rest) != STRING_CST
13423 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13424 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13425 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13426 {
13427 rest = java_complete_tree (rest);
13428 if (rest == error_mark_node)
13429 return error_mark_node;
13430 rest = fold (rest);
13431 }
13432 return merge_string_cste (string, rest, invert);
13433 }
13434 return NULL_TREE;
13435}
13436
13437/* Implement the `+' operator. Does static optimization if possible,
13438 otherwise create (if necessary) and append elements to a
13439 StringBuffer. The StringBuffer will be carried around until it is
13440 used for a function call or an assignment. Then toString() will be
13441 called on it to turn it into a String object. */
13442
13443static tree
13444build_string_concatenation (op1, op2)
13445 tree op1, op2;
13446{
13447 tree result;
dc0b3eff 13448 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13449
13450 if (flag_emit_xref)
13451 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13452
13453 /* Try to do some static optimization */
13454 if ((result = string_constant_concatenation (op1, op2)))
13455 return result;
13456
c0d87ff6
PB
13457 /* Discard empty strings on either side of the expression */
13458 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13459 {
13460 op1 = op2;
13461 op2 = NULL_TREE;
13462 }
c0d87ff6 13463 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13464 op2 = NULL_TREE;
b67d701b 13465
acd663ee 13466 /* If operands are string constant, turn then into object references */
b67d701b
PB
13467 if (TREE_CODE (op1) == STRING_CST)
13468 op1 = patch_string_cst (op1);
acd663ee 13469 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13470 op2 = patch_string_cst (op2);
13471
acd663ee
APB
13472 /* If either one of the constant is null and the other non null
13473 operand is a String object, return it. */
13474 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13475 return op1;
13476
b67d701b
PB
13477 /* If OP1 isn't already a StringBuffer, create and
13478 initialize a new one */
13479 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13480 {
13481 /* Two solutions here:
c52b5771
AG
13482 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13483 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13484 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13485 op1 = BUILD_STRING_BUFFER (op1);
13486 else
13487 {
13488 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13489 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13490 }
13491 }
13492
acd663ee
APB
13493 if (op2)
13494 {
13495 /* OP1 is no longer the last node holding a crafted StringBuffer */
13496 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13497 /* Create a node for `{new...,xxx}.append (op2)' */
13498 if (op2)
13499 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13500 }
13501
b67d701b
PB
13502 /* Mark the last node holding a crafted StringBuffer */
13503 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13504
13505 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13506 return op1;
13507}
13508
13509/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13510 StringBuffer. If no string were found to be patched, return
13511 NULL. */
13512
13513static tree
13514patch_string (node)
13515 tree node;
13516{
1179ebc2
APB
13517 if (node == error_mark_node)
13518 return error_mark_node;
b67d701b
PB
13519 if (TREE_CODE (node) == STRING_CST)
13520 return patch_string_cst (node);
13521 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13522 {
c877974e 13523 int saved = ctxp->explicit_constructor_p;
b67d701b 13524 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13525 tree ret;
13526 /* Temporary disable forbid the use of `this'. */
13527 ctxp->explicit_constructor_p = 0;
13528 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13529 /* String concatenation arguments must be evaluated in order too. */
13530 ret = force_evaluation_order (ret);
c877974e
APB
13531 /* Restore it at its previous value */
13532 ctxp->explicit_constructor_p = saved;
13533 return ret;
b67d701b
PB
13534 }
13535 return NULL_TREE;
13536}
13537
13538/* Build the internal representation of a string constant. */
13539
13540static tree
13541patch_string_cst (node)
13542 tree node;
13543{
13544 int location;
15fdcfe9
PB
13545 if (! flag_emit_class_files)
13546 {
15fdcfe9
PB
13547 node = get_identifier (TREE_STRING_POINTER (node));
13548 location = alloc_name_constant (CONSTANT_String, node);
13549 node = build_ref_from_constant_pool (location);
13550 }
cd9643f7 13551 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13552 TREE_CONSTANT (node) = 1;
13553 return node;
13554}
13555
13556/* Build an incomplete unary operator expression. */
e04a16fb
AG
13557
13558static tree
13559build_unaryop (op_token, op_location, op1)
13560 int op_token, op_location;
13561 tree op1;
13562{
13563 enum tree_code op;
13564 tree unaryop;
13565 switch (op_token)
13566 {
b67d701b 13567 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13568 case MINUS_TK: op = NEGATE_EXPR; break;
13569 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13570 case NOT_TK: op = BIT_NOT_EXPR; break;
400500c4 13571 default: abort ();
e04a16fb
AG
13572 }
13573
13574 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13575 TREE_SIDE_EFFECTS (unaryop) = 1;
13576 /* Store the location of the operator, for better error report. The
13577 string of the operator will be rebuild based on the OP value. */
13578 EXPR_WFL_LINECOL (unaryop) = op_location;
13579 return unaryop;
13580}
13581
13582/* Special case for the ++/-- operators, since they require an extra
13583 argument to build, which is set to NULL and patched
13584 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13585
13586static tree
13587build_incdec (op_token, op_location, op1, is_post_p)
13588 int op_token, op_location;
13589 tree op1;
13590 int is_post_p;
13591{
13592 static enum tree_code lookup [2][2] =
13593 {
13594 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13595 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13596 };
13597 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13598 NULL_TREE, op1, NULL_TREE);
13599 TREE_SIDE_EFFECTS (node) = 1;
13600 /* Store the location of the operator, for better error report. The
13601 string of the operator will be rebuild based on the OP value. */
13602 EXPR_WFL_LINECOL (node) = op_location;
13603 return node;
13604}
13605
13606/* Build an incomplete cast operator, based on the use of the
13607 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13608 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13609 though its type is already set. */
13610
13611static tree
13612build_cast (location, type, exp)
13613 int location;
13614 tree type, exp;
13615{
13616 tree node = build1 (CONVERT_EXPR, type, exp);
13617 EXPR_WFL_LINECOL (node) = location;
13618 return node;
13619}
13620
c2952b01
APB
13621/* Build an incomplete class reference operator. */
13622static tree
13623build_incomplete_class_ref (location, class_name)
13624 int location;
13625 tree class_name;
13626{
13627 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13628 EXPR_WFL_LINECOL (node) = location;
13629 return node;
13630}
13631
13632/* Complete an incomplete class reference operator. */
13633static tree
13634patch_incomplete_class_ref (node)
13635 tree node;
13636{
13637 tree type = TREE_OPERAND (node, 0);
13638 tree ref_type;
13639
13640 if (!(ref_type = resolve_type_during_patch (type)))
13641 return error_mark_node;
13642
165f37bc 13643 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13644 {
13645 /* A class referenced by `foo.class' is initialized. */
13646 return build_class_init (ref_type, build_class_ref (ref_type));
13647 }
165f37bc
APB
13648
13649 /* If we're emitting class files and we have to deal with non
13650 primitive types, we invoke (and consider generating) the
13651 synthetic static method `class$'. */
13652 if (!TYPE_DOT_CLASS (current_class))
13653 build_dot_class_method (current_class);
f0f3a777 13654 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13655 return java_complete_tree (ref_type);
c2952b01
APB
13656}
13657
e04a16fb
AG
13658/* 15.14 Unary operators. We return error_mark_node in case of error,
13659 but preserve the type of NODE if the type is fixed. */
13660
13661static tree
13662patch_unaryop (node, wfl_op)
13663 tree node;
13664 tree wfl_op;
13665{
13666 tree op = TREE_OPERAND (node, 0);
13667 tree op_type = TREE_TYPE (op);
ab3a6dd6 13668 tree prom_type = NULL_TREE, value, decl;
c2952b01 13669 int outer_field_flag = 0;
e04a16fb
AG
13670 int code = TREE_CODE (node);
13671 int error_found = 0;
13672
13673 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13674
13675 switch (code)
13676 {
13677 /* 15.13.2 Postfix Increment Operator ++ */
13678 case POSTINCREMENT_EXPR:
13679 /* 15.13.3 Postfix Increment Operator -- */
13680 case POSTDECREMENT_EXPR:
13681 /* 15.14.1 Prefix Increment Operator ++ */
13682 case PREINCREMENT_EXPR:
13683 /* 15.14.2 Prefix Decrement Operator -- */
13684 case PREDECREMENT_EXPR:
5cbdba64 13685 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13686 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13687 /* We might be trying to change an outer field accessed using
13688 access method. */
13689 if (outer_field_flag)
13690 {
13691 /* Retrieve the decl of the field we're trying to access. We
13692 do that by first retrieving the function we would call to
13693 access the field. It has been already verified that this
13694 field isn't final */
13695 if (flag_emit_class_files)
13696 decl = TREE_OPERAND (op, 0);
13697 else
13698 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13699 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13700 }
b3edebcf 13701 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13702 else if (!JDECL_P (decl)
b3edebcf
APB
13703 && TREE_CODE (decl) != COMPONENT_REF
13704 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13705 && TREE_CODE (decl) != INDIRECT_REF
13706 && !(TREE_CODE (decl) == COMPOUND_EXPR
13707 && TREE_OPERAND (decl, 1)
13708 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13709 {
5e942c50
APB
13710 tree lvalue;
13711 /* Before screaming, check that we're not in fact trying to
13712 increment a optimized static final access, in which case
13713 we issue an different error message. */
13714 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13715 && resolve_expression_name (wfl_op, &lvalue)
13716 && check_final_assignment (lvalue, wfl_op)))
13717 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13718 operator_string (node));
e04a16fb
AG
13719 TREE_TYPE (node) = error_mark_node;
13720 error_found = 1;
13721 }
c2952b01
APB
13722
13723 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13724 error_found = 1;
13725
e04a16fb
AG
13726 /* From now on, we know that op if a variable and that it has a
13727 valid wfl. We use wfl_op to locate errors related to the
13728 ++/-- operand. */
13729 else if (!JNUMERIC_TYPE_P (op_type))
13730 {
13731 parse_error_context
13732 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13733 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13734 TREE_TYPE (node) = error_mark_node;
13735 error_found = 1;
13736 }
13737 else
13738 {
4a5f66c3 13739 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13740 both operands, if really necessary */
13741 if (JINTEGRAL_TYPE_P (op_type))
13742 {
13743 value = build_int_2 (1, 0);
13744 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13745 }
13746 else
13747 {
13748 value = build_int_2 (1, 0);
13749 TREE_TYPE (node) =
13750 binary_numeric_promotion (op_type,
13751 TREE_TYPE (value), &op, &value);
13752 }
c2952b01
APB
13753
13754 /* We remember we might be accessing an outer field */
13755 if (outer_field_flag)
13756 {
13757 /* We re-generate an access to the field */
13758 value = build (PLUS_EXPR, TREE_TYPE (op),
13759 build_outer_field_access (wfl_op, decl), value);
13760
13761 /* And we patch the original access$() into a write
13762 with plus_op as a rhs */
13763 return outer_field_access_fix (node, op, value);
13764 }
13765
5cbdba64 13766 /* And write back into the node. */
4a5f66c3 13767 TREE_OPERAND (node, 0) = op;
e04a16fb 13768 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13769 /* Convert the overall back into its original type, if
13770 necessary, and return */
13771 if (JINTEGRAL_TYPE_P (op_type))
13772 return fold (node);
13773 else
13774 return fold (convert (op_type, node));
e04a16fb
AG
13775 }
13776 break;
13777
13778 /* 15.14.3 Unary Plus Operator + */
b67d701b 13779 case UNARY_PLUS_EXPR:
e04a16fb
AG
13780 /* 15.14.4 Unary Minus Operator - */
13781 case NEGATE_EXPR:
13782 if (!JNUMERIC_TYPE_P (op_type))
13783 {
13784 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13785 TREE_TYPE (node) = error_mark_node;
13786 error_found = 1;
13787 }
13788 /* Unary numeric promotion is performed on operand */
13789 else
13790 {
15fdcfe9
PB
13791 op = do_unary_numeric_promotion (op);
13792 prom_type = TREE_TYPE (op);
b67d701b 13793 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13794 return fold (op);
e04a16fb
AG
13795 }
13796 break;
13797
13798 /* 15.14.5 Bitwise Complement Operator ~ */
13799 case BIT_NOT_EXPR:
13800 if (!JINTEGRAL_TYPE_P (op_type))
13801 {
13802 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13803 TREE_TYPE (node) = error_mark_node;
13804 error_found = 1;
13805 }
13806 else
13807 {
15fdcfe9
PB
13808 op = do_unary_numeric_promotion (op);
13809 prom_type = TREE_TYPE (op);
e04a16fb
AG
13810 }
13811 break;
13812
13813 /* 15.14.6 Logical Complement Operator ! */
13814 case TRUTH_NOT_EXPR:
13815 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13816 {
13817 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13818 /* But the type is known. We will report an error if further
13819 attempt of a assignment is made with this rhs */
e04a16fb
AG
13820 TREE_TYPE (node) = boolean_type_node;
13821 error_found = 1;
13822 }
13823 else
13824 prom_type = boolean_type_node;
13825 break;
13826
13827 /* 15.15 Cast Expression */
13828 case CONVERT_EXPR:
0a2138e2 13829 value = patch_cast (node, wfl_operator);
e04a16fb 13830 if (value == error_mark_node)
c877974e
APB
13831 {
13832 /* If this cast is part of an assignment, we tell the code
13833 that deals with it not to complain about a mismatch,
13834 because things have been cast, anyways */
13835 TREE_TYPE (node) = error_mark_node;
13836 error_found = 1;
13837 }
13838 else
dc0b3eff
PB
13839 {
13840 value = fold (value);
13841 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13842 return value;
13843 }
e04a16fb
AG
13844 break;
13845 }
13846
e04a16fb
AG
13847 if (error_found)
13848 return error_mark_node;
4a5f66c3
APB
13849
13850 /* There are cases where node has been replaced by something else
13851 and we don't end up returning here: UNARY_PLUS_EXPR,
13852 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13853 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13854 TREE_TYPE (node) = prom_type;
dc0b3eff 13855 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13856 return fold (node);
13857}
13858
13859/* Generic type resolution that sometimes takes place during node
13860 patching. Returned the resolved type or generate an error
13861 message. Return the resolved type or NULL_TREE. */
13862
13863static tree
13864resolve_type_during_patch (type)
13865 tree type;
13866{
13867 if (unresolved_type_p (type, NULL))
13868 {
c0b00d37 13869 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13870 if (!type_decl)
13871 {
13872 parse_error_context (type,
13873 "Class `%s' not found in type declaration",
13874 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13875 return NULL_TREE;
13876 }
13877 else
5e942c50
APB
13878 {
13879 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13880 return TREE_TYPE (type_decl);
13881 }
e04a16fb
AG
13882 }
13883 return type;
13884}
13885/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13886 found. Otherwise NODE or something meant to replace it is returned. */
13887
13888static tree
19e223db 13889patch_cast (node, wfl_op)
e04a16fb 13890 tree node;
19e223db 13891 tree wfl_op;
e04a16fb
AG
13892{
13893 tree op = TREE_OPERAND (node, 0);
13894 tree op_type = TREE_TYPE (op);
13895 tree cast_type = TREE_TYPE (node);
13896 char *t1;
13897
13898 /* First resolve OP_TYPE if unresolved */
13899 if (!(cast_type = resolve_type_during_patch (cast_type)))
13900 return error_mark_node;
13901
13902 /* Check on cast that are proven correct at compile time */
13903 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13904 {
e04a16fb
AG
13905 /* Same type */
13906 if (cast_type == op_type)
13907 return node;
13908
0b4d333e
APB
13909 /* float and double type are converted to the original type main
13910 variant and then to the target type. */
13911 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13912 op = convert (integer_type_node, op);
13913
e04a16fb
AG
13914 /* Try widening/narowwing convertion. Potentially, things need
13915 to be worked out in gcc so we implement the extreme cases
13916 correctly. fold_convert() needs to be fixed. */
13917 return convert (cast_type, op);
13918 }
13919
0b4d333e
APB
13920 /* It's also valid to cast a boolean into a boolean */
13921 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13922 return node;
13923
5e942c50
APB
13924 /* null can be casted to references */
13925 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13926 return build_null_of_type (cast_type);
13927
e04a16fb
AG
13928 /* The remaining legal casts involve conversion between reference
13929 types. Check for their compile time correctness. */
13930 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13931 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13932 {
13933 TREE_TYPE (node) = promote_type (cast_type);
13934 /* Now, the case can be determined correct at compile time if
13935 OP_TYPE can be converted into CAST_TYPE by assignment
13936 conversion (5.2) */
13937
13938 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13939 {
13940 TREE_SET_CODE (node, NOP_EXPR);
13941 return node;
13942 }
13943
13944 if (flag_emit_class_files)
13945 {
13946 TREE_SET_CODE (node, CONVERT_EXPR);
13947 return node;
13948 }
e04a16fb
AG
13949
13950 /* The cast requires a run-time check */
13951 return build (CALL_EXPR, promote_type (cast_type),
13952 build_address_of (soft_checkcast_node),
13953 tree_cons (NULL_TREE, build_class_ref (cast_type),
13954 build_tree_list (NULL_TREE, op)),
13955 NULL_TREE);
13956 }
13957
13958 /* Any other casts are proven incorrect at compile time */
c2e3db92 13959 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13960 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13961 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13962 free (t1);
13963 return error_mark_node;
13964}
13965
5e942c50
APB
13966/* Build a null constant and give it the type TYPE. */
13967
13968static tree
13969build_null_of_type (type)
13970 tree type;
13971{
13972 tree node = build_int_2 (0, 0);
13973 TREE_TYPE (node) = promote_type (type);
13974 return node;
13975}
13976
e04a16fb
AG
13977/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13978 a list of indices. */
13979static tree
13980build_array_ref (location, array, index)
13981 int location;
13982 tree array, index;
13983{
13984 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13985 EXPR_WFL_LINECOL (node) = location;
13986 return node;
13987}
13988
13989/* 15.12 Array Access Expression */
13990
13991static tree
c877974e
APB
13992patch_array_ref (node)
13993 tree node;
e04a16fb
AG
13994{
13995 tree array = TREE_OPERAND (node, 0);
13996 tree array_type = TREE_TYPE (array);
13997 tree index = TREE_OPERAND (node, 1);
13998 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13999 int error_found = 0;
14000
14001 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14002
e04a16fb
AG
14003 if (TREE_CODE (array_type) == POINTER_TYPE)
14004 array_type = TREE_TYPE (array_type);
14005
14006 /* The array reference must be an array */
14007 if (!TYPE_ARRAY_P (array_type))
14008 {
14009 parse_error_context
781b0558
KG
14010 (wfl_operator,
14011 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14012 lang_printable_name (array_type, 0));
e04a16fb
AG
14013 TREE_TYPE (node) = error_mark_node;
14014 error_found = 1;
14015 }
14016
c2952b01 14017 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14018 type must be int */
15fdcfe9
PB
14019 index = do_unary_numeric_promotion (index);
14020 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14021 {
1ebadc60 14022 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14023 parse_error_context (wfl_operator,
14024 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14025 lang_printable_name (index_type, 0));
14026 else
781b0558
KG
14027 parse_error_context (wfl_operator,
14028 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14029 lang_printable_name (index_type, 0));
e04a16fb
AG
14030 TREE_TYPE (node) = error_mark_node;
14031 error_found = 1;
14032 }
14033
e04a16fb
AG
14034 if (error_found)
14035 return error_mark_node;
e04a16fb 14036
5e942c50 14037 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14038
7f1d4866 14039 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14040 {
15fdcfe9
PB
14041 TREE_OPERAND (node, 0) = array;
14042 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14043 }
14044 else
939d7216
PB
14045 {
14046 /* The save_expr is for correct evaluation order. It would be cleaner
14047 to use force_evaluation_order (see comment there), but that is
14048 difficult when we also have to deal with bounds checking. */
14049 if (TREE_SIDE_EFFECTS (index))
14050 array = save_expr (array);
14051 node = build_java_arrayaccess (array, array_type, index);
14052 if (TREE_SIDE_EFFECTS (index))
14053 node = build (COMPOUND_EXPR, array_type, array, node);
14054 }
e04a16fb
AG
14055 TREE_TYPE (node) = array_type;
14056 return node;
14057}
14058
14059/* 15.9 Array Creation Expressions */
14060
14061static tree
14062build_newarray_node (type, dims, extra_dims)
14063 tree type;
14064 tree dims;
14065 int extra_dims;
14066{
14067 tree node =
b67d701b 14068 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14069 build_int_2 (extra_dims, 0));
e04a16fb
AG
14070 return node;
14071}
14072
14073static tree
14074patch_newarray (node)
14075 tree node;
14076{
14077 tree type = TREE_OPERAND (node, 0);
14078 tree dims = TREE_OPERAND (node, 1);
14079 tree cdim, array_type;
14080 int error_found = 0;
14081 int ndims = 0;
14082 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14083
14084 /* Dimension types are verified. It's better for the types to be
14085 verified in order. */
14086 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14087 {
14088 int dim_error = 0;
14089 tree dim = TREE_VALUE (cdim);
14090
14091 /* Dim might have been saved during its evaluation */
dba41d30 14092 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14093
14094 /* The type of each specified dimension must be an integral type. */
14095 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14096 dim_error = 1;
14097
14098 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14099 promoted type must be int. */
14100 else
14101 {
15fdcfe9 14102 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14103 if (TREE_TYPE (dim) != int_type_node)
14104 dim_error = 1;
14105 }
14106
14107 /* Report errors on types here */
14108 if (dim_error)
14109 {
14110 parse_error_context
14111 (TREE_PURPOSE (cdim),
781b0558 14112 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14113 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14114 "Explicit cast needed to" : "Can't"),
0a2138e2 14115 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14116 error_found = 1;
14117 }
14118
e04a16fb
AG
14119 TREE_PURPOSE (cdim) = NULL_TREE;
14120 }
14121
14122 /* Resolve array base type if unresolved */
14123 if (!(type = resolve_type_during_patch (type)))
14124 error_found = 1;
14125
14126 if (error_found)
14127 {
14128 /* We don't want further evaluation of this bogus array creation
14129 operation */
14130 TREE_TYPE (node) = error_mark_node;
14131 return error_mark_node;
14132 }
14133
15fdcfe9
PB
14134 /* Set array_type to the actual (promoted) array type of the result. */
14135 if (TREE_CODE (type) == RECORD_TYPE)
14136 type = build_pointer_type (type);
14137 while (--xdims >= 0)
14138 {
14139 type = promote_type (build_java_array_type (type, -1));
14140 }
14141 dims = nreverse (dims);
14142 array_type = type;
14143 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14144 {
14145 type = array_type;
05bccae2
RK
14146 array_type
14147 = build_java_array_type (type,
14148 TREE_CODE (cdim) == INTEGER_CST
14149 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14150 : -1);
15fdcfe9
PB
14151 array_type = promote_type (array_type);
14152 }
14153 dims = nreverse (dims);
14154
e04a16fb
AG
14155 /* The node is transformed into a function call. Things are done
14156 differently according to the number of dimensions. If the number
14157 of dimension is equal to 1, then the nature of the base type
14158 (primitive or not) matters. */
15fdcfe9 14159 if (ndims == 1)
fdec99c6 14160 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14161
e04a16fb
AG
14162 /* Can't reuse what's already written in expr.c because it uses the
14163 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14164 return build (CALL_EXPR, array_type,
e04a16fb 14165 build_address_of (soft_multianewarray_node),
15fdcfe9 14166 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14167 tree_cons (NULL_TREE,
15fdcfe9 14168 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14169 NULL_TREE);
14170}
14171
f8976021
APB
14172/* 10.6 Array initializer. */
14173
14174/* Build a wfl for array element that don't have one, so we can
14175 pin-point errors. */
14176
14177static tree
14178maybe_build_array_element_wfl (node)
14179 tree node;
14180{
14181 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14182 return build_expr_wfl (NULL_TREE, ctxp->filename,
14183 ctxp->elc.line, ctxp->elc.prev_col);
14184 else
14185 return NULL_TREE;
14186}
14187
14188/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14189 identification of initialized arrays easier to detect during walk
14190 and expansion. */
14191
14192static tree
14193build_new_array_init (location, values)
14194 int location;
14195 tree values;
14196{
14197 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14198 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14199 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14200 return to_return;
14201}
14202
14203/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14204 occurred. Otherwise return NODE after having set its type
14205 appropriately. */
14206
14207static tree
14208patch_new_array_init (type, node)
14209 tree type, node;
f8976021
APB
14210{
14211 int error_seen = 0;
fdec99c6 14212 tree current, element_type;
f8976021 14213 HOST_WIDE_INT length;
fdec99c6
PB
14214 int all_constant = 1;
14215 tree init = TREE_OPERAND (node, 0);
f8976021 14216
fdec99c6
PB
14217 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14218 {
14219 parse_error_context (node,
14220 "Invalid array initializer for non-array type `%s'",
14221 lang_printable_name (type, 1));
14222 return error_mark_node;
14223 }
14224 type = TREE_TYPE (type);
14225 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14226
fdec99c6
PB
14227 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14228
14229 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14230 current; length++, current = TREE_CHAIN (current))
f8976021 14231 {
fdec99c6
PB
14232 tree elt = TREE_VALUE (current);
14233 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14234 {
fdec99c6 14235 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14236 elt = TREE_VALUE (current);
14237 /* When compiling to native code, STRING_CST is converted to
14238 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14239 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14240 all_constant = 0;
f8976021 14241 }
fdec99c6
PB
14242 else
14243 {
14244 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14245 TREE_PURPOSE (current) = NULL_TREE;
14246 all_constant = 0;
14247 }
9a7ab4b3
APB
14248 if (elt && TREE_CODE (elt) == TREE_LIST
14249 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14250 error_seen = 1;
f8976021
APB
14251 }
14252
14253 if (error_seen)
14254 return error_mark_node;
14255
14256 /* Create a new type. We can't reuse the one we have here by
14257 patching its dimension because it originally is of dimension -1
14258 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14259 type = build_java_array_type (element_type, length);
14260 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14261 TREE_TYPE (node) = promote_type (type);
14262 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14263 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14264 return node;
14265}
14266
14267/* Verify that one entry of the initializer element list can be
14268 assigned to the array base type. Report 1 if an error occurred, 0
14269 otherwise. */
14270
14271static int
14272array_constructor_check_entry (type, entry)
14273 tree type, entry;
14274{
14275 char *array_type_string = NULL; /* For error reports */
14276 tree value, type_value, new_value, wfl_value, patched;
14277 int error_seen = 0;
14278
14279 new_value = NULL_TREE;
14280 wfl_value = TREE_VALUE (entry);
14281
f8976021 14282 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14283 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14284 if ((patched = patch_string (value)))
14285 value = patched;
1179ebc2
APB
14286 if (value == error_mark_node)
14287 return 1;
f8976021 14288
f8976021
APB
14289 type_value = TREE_TYPE (value);
14290
1179ebc2 14291 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14292 constant overflow during narrowing. */
14293 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14294 new_value = try_builtin_assignconv (wfl_operator, type, value);
14295 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14296 type_value = promote_type (type);
100f7cd8 14297
f8976021
APB
14298 /* Check and report errors */
14299 if (!new_value)
14300 {
49f48c71 14301 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14302 "Can't" : "Explicit cast needed to");
14303 if (!array_type_string)
c2e3db92 14304 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14305 parse_error_context
14306 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14307 msg, lang_printable_name (type_value, 1), array_type_string);
14308 error_seen = 1;
14309 }
14310
14311 if (new_value)
14312 {
b8c5b1c6 14313 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14314 TREE_VALUE (entry) = new_value;
14315 }
14316
14317 if (array_type_string)
14318 free (array_type_string);
14319
14320 TREE_PURPOSE (entry) = NULL_TREE;
14321 return error_seen;
14322}
14323
e04a16fb
AG
14324static tree
14325build_this (location)
14326 int location;
14327{
9ee9b555 14328 tree node = build_wfl_node (this_identifier_node);
b67d701b 14329 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14330 EXPR_WFL_LINECOL (node) = location;
14331 return node;
14332}
14333
14334/* 14.15 The return statement. It builds a modify expression that
14335 assigns the returned value to the RESULT_DECL that hold the value
14336 to be returned. */
14337
14338static tree
14339build_return (location, op)
14340 int location;
14341 tree op;
14342{
14343 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14344 EXPR_WFL_LINECOL (node) = location;
b67d701b 14345 node = build_debugable_stmt (location, node);
e04a16fb
AG
14346 return node;
14347}
14348
14349static tree
14350patch_return (node)
14351 tree node;
14352{
14353 tree return_exp = TREE_OPERAND (node, 0);
14354 tree meth = current_function_decl;
14355 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14356 int error_found = 0;
14357
14358 TREE_TYPE (node) = error_mark_node;
14359 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14360
14361 /* It's invalid to have a return value within a function that is
14362 declared with the keyword void or that is a constructor */
14363 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14364 error_found = 1;
14365
f099f336 14366 /* It's invalid to use a return statement in a static block */
c2952b01 14367 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14368 error_found = 1;
14369
e04a16fb
AG
14370 /* It's invalid to have a no return value within a function that
14371 isn't declared with the keyword `void' */
14372 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14373 error_found = 2;
c2952b01
APB
14374
14375 if (in_instance_initializer)
14376 error_found = 1;
e04a16fb
AG
14377
14378 if (error_found)
14379 {
c2952b01 14380 if (in_instance_initializer)
f099f336 14381 parse_error_context (wfl_operator,
c2952b01
APB
14382 "`return' inside instance initializer");
14383
14384 else if (DECL_CLINIT_P (current_function_decl))
14385 parse_error_context (wfl_operator,
14386 "`return' inside static initializer");
f099f336
APB
14387
14388 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14389 {
c2e3db92 14390 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14391 parse_error_context (wfl_operator,
14392 "`return' with%s value from `%s %s'",
14393 (error_found == 1 ? "" : "out"),
14394 t, lang_printable_name (meth, 0));
14395 free (t);
14396 }
14397 else
14398 parse_error_context (wfl_operator,
14399 "`return' with value from constructor `%s'",
14400 lang_printable_name (meth, 0));
e04a16fb
AG
14401 return error_mark_node;
14402 }
14403
5e942c50
APB
14404 /* If we have a return_exp, build a modify expression and expand
14405 it. Note: at that point, the assignment is declared valid, but we
14406 may want to carry some more hacks */
e04a16fb
AG
14407 if (return_exp)
14408 {
5e942c50
APB
14409 tree exp = java_complete_tree (return_exp);
14410 tree modify, patched;
14411
14412 /* If the function returned value and EXP are booleans, EXP has
14413 to be converted into the type of DECL_RESULT, which is integer
14414 (see complete_start_java_method) */
14415 if (TREE_TYPE (exp) == boolean_type_node &&
14416 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14417 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14418
14419 /* `null' can be assigned to a function returning a reference */
14420 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14421 exp == null_pointer_node)
14422 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14423
14424 if ((patched = patch_string (exp)))
14425 exp = patched;
14426
14427 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14428 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14429 modify = java_complete_tree (modify);
5e942c50 14430
e04a16fb
AG
14431 if (modify != error_mark_node)
14432 {
14433 TREE_SIDE_EFFECTS (modify) = 1;
14434 TREE_OPERAND (node, 0) = modify;
14435 }
14436 else
14437 return error_mark_node;
14438 }
14439 TREE_TYPE (node) = void_type_node;
14440 TREE_SIDE_EFFECTS (node) = 1;
14441 return node;
14442}
14443
14444/* 14.8 The if Statement */
14445
14446static tree
14447build_if_else_statement (location, expression, if_body, else_body)
14448 int location;
14449 tree expression, if_body, else_body;
14450{
14451 tree node;
e04a16fb 14452 if (!else_body)
9bbc7d9f 14453 else_body = empty_stmt_node;
e04a16fb
AG
14454 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14455 EXPR_WFL_LINECOL (node) = location;
b67d701b 14456 node = build_debugable_stmt (location, node);
e04a16fb
AG
14457 return node;
14458}
14459
14460static tree
14461patch_if_else_statement (node)
14462 tree node;
14463{
14464 tree expression = TREE_OPERAND (node, 0);
14465
14466 TREE_TYPE (node) = error_mark_node;
14467 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14468
14469 /* The type of expression must be boolean */
b67d701b
PB
14470 if (TREE_TYPE (expression) != boolean_type_node
14471 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14472 {
14473 parse_error_context
14474 (wfl_operator,
14475 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14476 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14477 return error_mark_node;
14478 }
14479
14480 TREE_TYPE (node) = void_type_node;
14481 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14482 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14483 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14484 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14485 return node;
14486}
14487
14488/* 14.6 Labeled Statements */
14489
14490/* Action taken when a lableled statement is parsed. a new
14491 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14492 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14493
14494static tree
0a2138e2 14495build_labeled_block (location, label)
e04a16fb 14496 int location;
0a2138e2 14497 tree label;
e04a16fb 14498{
b635eb2f 14499 tree label_name ;
e04a16fb 14500 tree label_decl, node;
b635eb2f
PB
14501 if (label == NULL_TREE || label == continue_identifier_node)
14502 label_name = label;
14503 else
e04a16fb 14504 {
b635eb2f
PB
14505 label_name = merge_qualified_name (label_id, label);
14506 /* Issue an error if we try to reuse a label that was previously
14507 declared */
14508 if (IDENTIFIER_LOCAL_VALUE (label_name))
14509 {
14510 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14511 parse_error_context (wfl_operator,
14512 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14513 IDENTIFIER_POINTER (label));
14514 EXPR_WFL_LINECOL (wfl_operator) =
14515 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14516 parse_error_context (wfl_operator,
14517 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14518 IDENTIFIER_POINTER (label));
14519 java_error_count--;
14520 }
e04a16fb
AG
14521 }
14522
14523 label_decl = create_label_decl (label_name);
14524 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14525 EXPR_WFL_LINECOL (node) = location;
14526 TREE_SIDE_EFFECTS (node) = 1;
14527 return node;
14528}
14529
b67d701b 14530/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14531
14532static tree
b635eb2f 14533finish_labeled_statement (lbe, statement)
e04a16fb
AG
14534 tree lbe; /* Labeled block expr */
14535 tree statement;
14536{
14537 /* In anyways, tie the loop to its statement */
14538 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14539 pop_labeled_block ();
14540 POP_LABELED_BLOCK ();
e04a16fb
AG
14541 return lbe;
14542}
14543
14544/* 14.10, 14.11, 14.12 Loop Statements */
14545
14546/* Create an empty LOOP_EXPR and make it the last in the nested loop
14547 list. */
14548
14549static tree
14550build_new_loop (loop_body)
14551 tree loop_body;
14552{
14553 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14554 TREE_SIDE_EFFECTS (loop) = 1;
14555 PUSH_LOOP (loop);
14556 return loop;
14557}
14558
14559/* Create a loop body according to the following structure:
14560 COMPOUND_EXPR
14561 COMPOUND_EXPR (loop main body)
14562 EXIT_EXPR (this order is for while/for loops.
14563 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14564 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14565 BODY end of this labeled block)
14566 INCREMENT (if any)
14567
14568 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14569 after the body, like in the do-while loop.
14570
14571 To obtain a loop, the loop body structure described above is
14572 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14573
14574 LABELED_BLOCK_EXPR
14575 LABEL_DECL (use this label to exit the loop)
14576 LOOP_EXPR
14577 <structure described above> */
e04a16fb
AG
14578
14579static tree
14580build_loop_body (location, condition, reversed)
14581 int location;
14582 tree condition;
14583 int reversed;
14584{
0a2138e2 14585 tree first, second, body;
e04a16fb
AG
14586
14587 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14588 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14589 condition = build_debugable_stmt (location, condition);
14590 TREE_SIDE_EFFECTS (condition) = 1;
14591
b635eb2f 14592 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14593 first = (reversed ? body : condition);
14594 second = (reversed ? condition : body);
14595 return
14596 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14597 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14598}
14599
14600/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14601 their order) on the current loop. Unlink the current loop from the
14602 loop list. */
14603
14604static tree
b635eb2f 14605finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14606 int location;
14607 tree condition, body;
14608 int reversed;
14609{
14610 tree to_return = ctxp->current_loop;
14611 tree loop_body = LOOP_EXPR_BODY (to_return);
14612 if (condition)
14613 {
14614 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14615 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14616 The real EXIT_EXPR is one operand further. */
14617 EXPR_WFL_LINECOL (cnode) = location;
14618 /* This one is for accurate error reports */
14619 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14620 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14621 }
14622 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14623 POP_LOOP ();
14624 return to_return;
14625}
14626
b635eb2f 14627/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14628 loops feature the condition part */
14629
14630static tree
b635eb2f 14631finish_for_loop (location, condition, update, body)
e04a16fb
AG
14632 int location;
14633 tree condition, update, body;
14634{
14635 /* Put the condition and the loop body in place */
b635eb2f 14636 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14637 /* LOOP is the current loop which has been now popped of the loop
14638 stack. Install the update block */
14639 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14640 return loop;
14641}
14642
5cbdba64
APB
14643/* Try to find the loop a block might be related to. This comprises
14644 the case where the LOOP_EXPR is found as the second operand of a
14645 COMPOUND_EXPR, because the loop happens to have an initialization
14646 part, then expressed as the first operand of the COMPOUND_EXPR. If
14647 the search finds something, 1 is returned. Otherwise, 0 is
14648 returned. The search is assumed to start from a
14649 LABELED_BLOCK_EXPR's block. */
14650
14651static tree
14652search_loop (statement)
14653 tree statement;
14654{
14655 if (TREE_CODE (statement) == LOOP_EXPR)
14656 return statement;
14657
14658 if (TREE_CODE (statement) == BLOCK)
14659 statement = BLOCK_SUBBLOCKS (statement);
14660 else
14661 return NULL_TREE;
14662
14663 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14664 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14665 statement = TREE_OPERAND (statement, 1);
14666
14667 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14668 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14669}
14670
14671/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14672 returned otherwise. */
14673
14674static int
14675labeled_block_contains_loop_p (block, loop)
14676 tree block, loop;
14677{
14678 if (!block)
14679 return 0;
14680
14681 if (LABELED_BLOCK_BODY (block) == loop)
14682 return 1;
14683
c2952b01 14684 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14685 return 1;
14686
14687 return 0;
14688}
14689
e04a16fb 14690/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14691 insert LOOP as its body. */
e04a16fb
AG
14692
14693static tree
14694patch_loop_statement (loop)
14695 tree loop;
14696{
cd9643f7 14697 tree loop_label;
5cbdba64 14698
cd9643f7 14699 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14700 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14701 return loop;
14702
cd9643f7 14703 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14704 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14705 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14706 LABELED_BLOCK_BODY (loop_label) = loop;
14707 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14708 return loop_label;
e04a16fb
AG
14709}
14710
14711/* 14.13, 14.14: break and continue Statements */
14712
14713/* Build a break or a continue statement. a null NAME indicates an
14714 unlabeled break/continue statement. */
14715
14716static tree
14717build_bc_statement (location, is_break, name)
14718 int location, is_break;
14719 tree name;
14720{
14721 tree break_continue, label_block_expr = NULL_TREE;
14722
14723 if (name)
14724 {
14725 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14726 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14727 /* Null means that we don't have a target for this named
14728 break/continue. In this case, we make the target to be the
14729 label name, so that the error can be reported accuratly in
14730 patch_bc_statement. */
14731 label_block_expr = EXPR_WFL_NODE (name);
14732 }
14733 /* Unlabeled break/continue will be handled during the
14734 break/continue patch operation */
14735 break_continue
14736 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14737
14738 IS_BREAK_STMT_P (break_continue) = is_break;
14739 TREE_SIDE_EFFECTS (break_continue) = 1;
14740 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14741 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14742 return break_continue;
14743}
14744
14745/* Verification of a break/continue statement. */
14746
14747static tree
14748patch_bc_statement (node)
14749 tree node;
14750{
14751 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14752 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14753 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14754
e04a16fb 14755 /* Having an identifier here means that the target is unknown. */
b635eb2f 14756 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14757 {
14758 parse_error_context (wfl_operator, "No label definition found for `%s'",
14759 IDENTIFIER_POINTER (bc_label));
14760 return error_mark_node;
14761 }
b635eb2f 14762 if (! IS_BREAK_STMT_P (node))
e04a16fb 14763 {
b635eb2f
PB
14764 /* It's a continue statement. */
14765 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14766 {
b635eb2f
PB
14767 if (labeled_block == NULL_TREE)
14768 {
14769 if (bc_label == NULL_TREE)
14770 parse_error_context (wfl_operator,
14771 "`continue' must be in loop");
14772 else
1504b2b4
APB
14773 parse_error_context
14774 (wfl_operator, "continue label `%s' does not name a loop",
14775 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14776 return error_mark_node;
14777 }
14778 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14779 == continue_identifier_node)
14780 && (bc_label == NULL_TREE
14781 || TREE_CHAIN (labeled_block) == bc_label))
14782 {
14783 bc_label = labeled_block;
14784 break;
14785 }
e04a16fb 14786 }
e04a16fb 14787 }
b635eb2f 14788 else if (!bc_label)
34f4db93 14789 {
b635eb2f 14790 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14791 {
b635eb2f
PB
14792 if (labeled_block == NULL_TREE)
14793 {
14794 parse_error_context (wfl_operator,
14795 "`break' must be in loop or switch");
14796 return error_mark_node;
14797 }
14798 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14799 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14800 || search_loop (target_stmt))
b635eb2f
PB
14801 {
14802 bc_label = labeled_block;
14803 break;
14804 }
e04a16fb 14805 }
e04a16fb
AG
14806 }
14807
b635eb2f 14808 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14809 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14810
e04a16fb
AG
14811 /* Our break/continue don't return values. */
14812 TREE_TYPE (node) = void_type_node;
14813 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14814 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14815 sometimes, like after a if statement) */
14816 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14817 TREE_SIDE_EFFECTS (node) = 1;
14818 return node;
14819}
14820
14821/* Process the exit expression belonging to a loop. Its type must be
14822 boolean. */
14823
14824static tree
14825patch_exit_expr (node)
14826 tree node;
14827{
14828 tree expression = TREE_OPERAND (node, 0);
14829 TREE_TYPE (node) = error_mark_node;
14830 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14831
14832 /* The type of expression must be boolean */
14833 if (TREE_TYPE (expression) != boolean_type_node)
14834 {
14835 parse_error_context
14836 (wfl_operator,
781b0558 14837 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14838 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14839 return error_mark_node;
14840 }
14841 /* Now we know things are allright, invert the condition, fold and
14842 return */
14843 TREE_OPERAND (node, 0) =
14844 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14845
14846 if (! integer_zerop (TREE_OPERAND (node, 0))
14847 && ctxp->current_loop != NULL_TREE
14848 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14849 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14850 if (! integer_onep (TREE_OPERAND (node, 0)))
14851 CAN_COMPLETE_NORMALLY (node) = 1;
14852
14853
e04a16fb
AG
14854 TREE_TYPE (node) = void_type_node;
14855 return node;
14856}
b67d701b
PB
14857
14858/* 14.9 Switch statement */
14859
14860static tree
14861patch_switch_statement (node)
14862 tree node;
14863{
c877974e 14864 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14865
14866 /* Complete the switch expression */
14867 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14868 se_type = TREE_TYPE (se);
14869 /* The type of the switch expression must be char, byte, short or
14870 int */
2e0f0aff 14871 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14872 {
14873 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14874 parse_error_context (wfl_operator,
14875 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14876 lang_printable_name (se_type, 0));
b67d701b
PB
14877 /* This is what java_complete_tree will check */
14878 TREE_OPERAND (node, 0) = error_mark_node;
14879 return error_mark_node;
14880 }
14881
15fdcfe9 14882 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14883
14884 /* Ready to return */
15fdcfe9 14885 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14886 {
14887 TREE_TYPE (node) = error_mark_node;
14888 return error_mark_node;
14889 }
14890 TREE_TYPE (node) = void_type_node;
14891 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14892 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14893 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14894 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14895 return node;
14896}
14897
165f37bc 14898/* 14.18 The try/catch statements */
b67d701b 14899
b67d701b 14900static tree
a7d8d81f 14901build_try_statement (location, try_block, catches)
b67d701b 14902 int location;
a7d8d81f
PB
14903 tree try_block, catches;
14904{
14905 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14906 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14907 return node;
b67d701b
PB
14908}
14909
a7d8d81f
PB
14910static tree
14911build_try_finally_statement (location, try_block, finally)
14912 int location;
14913 tree try_block, finally;
b67d701b 14914{
a7d8d81f
PB
14915 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14916 EXPR_WFL_LINECOL (node) = location;
14917 return node;
b67d701b
PB
14918}
14919
14920static tree
14921patch_try_statement (node)
14922 tree node;
14923{
14924 int error_found = 0;
14925 tree try = TREE_OPERAND (node, 0);
14926 /* Exception handlers are considered in left to right order */
14927 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14928 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14929
14930 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14931 to process the next catch clause. We process the catch clause before
14932 the try block so that when processing the try block we can check thrown
14933 exceptions againts the caught type list. */
b67d701b
PB
14934 for (current = catch; current; current = TREE_CHAIN (current))
14935 {
14936 tree carg_decl, carg_type;
14937 tree sub_current, catch_block, catch_clause;
14938 int unreachable;
14939
b67d701b 14940 /* At this point, the structure of the catch clause is
b67d701b
PB
14941 CATCH_EXPR (catch node)
14942 BLOCK (with the decl of the parameter)
14943 COMPOUND_EXPR
7525cc04 14944 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14945 BLOCK (catch clause block)
a7d8d81f
PB
14946 */
14947 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14948 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14949 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14950
14951 /* Catch clauses can't have more than one parameter declared,
14952 but it's already enforced by the grammar. Make sure that the
14953 only parameter of the clause statement in of class Throwable
14954 or a subclass of Throwable, but that was done earlier. The
14955 catch clause parameter type has also been resolved. */
14956
14957 /* Just make sure that the catch clause parameter type inherits
14958 from java.lang.Throwable */
14959 if (!inherits_from_p (carg_type, throwable_type_node))
14960 {
14961 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14962 parse_error_context (wfl_operator,
781b0558 14963 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14964 lang_printable_name (carg_type, 0));
b67d701b
PB
14965 error_found = 1;
14966 continue;
14967 }
14968
14969 /* Partial check for unreachable catch statement: The catch
14970 clause is reachable iff is no earlier catch block A in
14971 the try statement such that the type of the catch
14972 clause's parameter is the same as or a subclass of the
14973 type of A's parameter */
14974 unreachable = 0;
14975 for (sub_current = catch;
14976 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14977 {
14978 tree sub_catch_clause, decl;
a7d8d81f 14979 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14980 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14981
14982 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14983 {
14984 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14985 parse_error_context
781b0558
KG
14986 (wfl_operator,
14987 "`catch' not reached because of the catch clause at line %d",
14988 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14989 unreachable = error_found = 1;
14990 break;
14991 }
14992 }
b67d701b
PB
14993 /* Complete the catch clause block */
14994 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14995 if (catch_block == error_mark_node)
14996 {
14997 error_found = 1;
14998 continue;
14999 }
15fdcfe9
PB
15000 if (CAN_COMPLETE_NORMALLY (catch_block))
15001 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15002 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15003
15004 if (unreachable)
15005 continue;
15006
15007 /* Things to do here: the exception must be thrown */
15008
15009 /* Link this type to the caught type list */
15010 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15011 }
15012
b9f7e36c
APB
15013 PUSH_EXCEPTIONS (caught_type_list);
15014 if ((try = java_complete_tree (try)) == error_mark_node)
15015 error_found = 1;
15fdcfe9
PB
15016 if (CAN_COMPLETE_NORMALLY (try))
15017 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15018 POP_EXCEPTIONS ();
15019
b67d701b
PB
15020 /* Verification ends here */
15021 if (error_found)
15022 return error_mark_node;
15023
15024 TREE_OPERAND (node, 0) = try;
15025 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15026 TREE_TYPE (node) = void_type_node;
15027 return node;
15028}
b9f7e36c
APB
15029
15030/* 14.17 The synchronized Statement */
15031
15032static tree
15033patch_synchronized_statement (node, wfl_op1)
15034 tree node, wfl_op1;
15035{
5a005d9e 15036 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15037 tree block = TREE_OPERAND (node, 1);
5a005d9e 15038
c7303e41 15039 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15040
15041 if (expr == error_mark_node)
15042 {
15043 block = java_complete_tree (block);
15044 return expr;
15045 }
b9f7e36c 15046
c7303e41
APB
15047 /* We might be trying to synchronize on a STRING_CST */
15048 if ((tmp = patch_string (expr)))
15049 expr = tmp;
15050
b9f7e36c 15051 /* The TYPE of expr must be a reference type */
5a005d9e 15052 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15053 {
15054 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15055 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15056 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15057 return error_mark_node;
15058 }
15059
ce6e9147
APB
15060 if (flag_emit_xref)
15061 {
15062 TREE_OPERAND (node, 0) = expr;
15063 TREE_OPERAND (node, 1) = java_complete_tree (block);
15064 CAN_COMPLETE_NORMALLY (node) = 1;
15065 return node;
15066 }
15067
b9f7e36c
APB
15068 /* Generate a try-finally for the synchronized statement, except
15069 that the handler that catches all throw exception calls
15070 _Jv_MonitorExit and then rethrow the exception.
15071 The synchronized statement is then implemented as:
15072 TRY
15073 {
15074 _Jv_MonitorEnter (expression)
15075 synchronized_block
15076 _Jv_MonitorExit (expression)
15077 }
15078 CATCH_ALL
15079 {
15080 e = _Jv_exception_info ();
15081 _Jv_MonitorExit (expression)
15082 Throw (e);
15083 } */
15084
5a005d9e
PB
15085 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15086 BUILD_MONITOR_ENTER (enter, expr_decl);
15087 BUILD_MONITOR_EXIT (exit, expr_decl);
15088 CAN_COMPLETE_NORMALLY (enter) = 1;
15089 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15090 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15091 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15092 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15093 build (COMPOUND_EXPR, NULL_TREE,
15094 build (WITH_CLEANUP_EXPR, NULL_TREE,
15095 build (COMPOUND_EXPR, NULL_TREE,
96847892 15096 assignment, enter),
5a005d9e
PB
15097 NULL_TREE, exit),
15098 block));
15099 node = build_expr_block (node, expr_decl);
15100
15101 return java_complete_tree (node);
b9f7e36c
APB
15102}
15103
15104/* 14.16 The throw Statement */
15105
15106static tree
15107patch_throw_statement (node, wfl_op1)
15108 tree node, wfl_op1;
15109{
15110 tree expr = TREE_OPERAND (node, 0);
15111 tree type = TREE_TYPE (expr);
15112 int unchecked_ok = 0, tryblock_throws_ok = 0;
15113
15114 /* Thrown expression must be assignable to java.lang.Throwable */
15115 if (!try_reference_assignconv (throwable_type_node, expr))
15116 {
15117 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15118 parse_error_context (wfl_operator,
15119 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15120 lang_printable_name (type, 0));
b9f7e36c
APB
15121 /* If the thrown expression was a reference, we further the
15122 compile-time check. */
15123 if (!JREFERENCE_TYPE_P (type))
15124 return error_mark_node;
15125 }
15126
15127 /* At least one of the following must be true */
15128
15129 /* The type of the throw expression is a not checked exception,
15130 i.e. is a unchecked expression. */
c877974e 15131 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15132
c2952b01
APB
15133 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15134 /* An instance can't throw a checked excetion unless that exception
15135 is explicitely declared in the `throws' clause of each
15136 constructor. This doesn't apply to anonymous classes, since they
15137 don't have declared constructors. */
15138 if (!unchecked_ok
15139 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15140 {
15141 tree current;
15142 for (current = TYPE_METHODS (current_class); current;
15143 current = TREE_CHAIN (current))
15144 if (DECL_CONSTRUCTOR_P (current)
15145 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15146 {
15147 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)",
15148 lang_printable_name (TREE_TYPE (expr), 0));
15149 return error_mark_node;
15150 }
15151 }
15152
b9f7e36c
APB
15153 /* Throw is contained in a try statement and at least one catch
15154 clause can receive the thrown expression or the current method is
15155 declared to throw such an exception. Or, the throw statement is
15156 contained in a method or constructor declaration and the type of
15157 the Expression is assignable to at least one type listed in the
15158 throws clause the declaration. */
b9f7e36c 15159 if (!unchecked_ok)
f099f336 15160 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15161 if (!(unchecked_ok || tryblock_throws_ok))
15162 {
15163 /* If there is a surrounding try block that has no matching
15164 clatch clause, report it first. A surrounding try block exits
15165 only if there is something after the list of checked
15166 exception thrown by the current function (if any). */
15167 if (IN_TRY_BLOCK_P ())
781b0558 15168 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 15169 lang_printable_name (type, 0));
b9f7e36c
APB
15170 /* If we have no surrounding try statement and the method doesn't have
15171 any throws, report it now. FIXME */
f099f336
APB
15172
15173 /* We report that the exception can't be throw from a try block
15174 in all circumstances but when the `throw' is inside a static
15175 block. */
b9f7e36c
APB
15176 else if (!EXCEPTIONS_P (currently_caught_type_list)
15177 && !tryblock_throws_ok)
f099f336 15178 {
c2952b01 15179 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15180 parse_error_context (wfl_operator,
15181 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15182 lang_printable_name (type, 0));
15183 else
781b0558
KG
15184 parse_error_context (wfl_operator,
15185 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15186 lang_printable_name (type, 0));
15187 }
b9f7e36c
APB
15188 /* Otherwise, the current method doesn't have the appropriate
15189 throws declaration */
15190 else
781b0558 15191 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15192 lang_printable_name (type, 0));
b9f7e36c
APB
15193 return error_mark_node;
15194 }
15195
ce6e9147 15196 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15197 BUILD_THROW (node, expr);
ce6e9147
APB
15198
15199 /* If doing xrefs, keep the location where the `throw' was seen. */
15200 if (flag_emit_xref)
15201 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15202 return node;
15203}
15204
15205/* Check that exception said to be thrown by method DECL can be
15206 effectively caught from where DECL is invoked. */
15207
15208static void
15209check_thrown_exceptions (location, decl)
15210 int location;
15211 tree decl;
15212{
15213 tree throws;
15214 /* For all the unchecked exceptions thrown by DECL */
15215 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15216 throws = TREE_CHAIN (throws))
0a2138e2 15217 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15218 {
3e78f871
PB
15219#if 1
15220 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15221 if (DECL_NAME (decl) == get_identifier ("clone"))
15222 continue;
15223#endif
b9f7e36c 15224 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15225 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15226 parse_error_context
15227 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15228 lang_printable_name (TREE_VALUE (throws), 0));
15229 else
15230 {
15231 parse_error_context
781b0558 15232 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15233 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15234 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15235 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15236 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15237 }
b9f7e36c
APB
15238 }
15239}
15240
c877974e 15241/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15242 try-catch blocks, OR is listed in the `throws' clause of the
15243 current method. */
15244
15245static int
0a2138e2 15246check_thrown_exceptions_do (exception)
b9f7e36c
APB
15247 tree exception;
15248{
15249 tree list = currently_caught_type_list;
c877974e 15250 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15251 /* First, all the nested try-catch-finally at that stage. The
15252 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15253 if (IS_UNCHECKED_EXCEPTION_P (exception))
15254 return 1;
b9f7e36c
APB
15255 while (list)
15256 {
15257 tree caught;
15258 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15259 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15260 return 1;
15261 list = TREE_CHAIN (list);
15262 }
15263 return 0;
15264}
15265
15266static void
15267purge_unchecked_exceptions (mdecl)
15268 tree mdecl;
15269{
15270 tree throws = DECL_FUNCTION_THROWS (mdecl);
15271 tree new = NULL_TREE;
15272
15273 while (throws)
15274 {
15275 tree next = TREE_CHAIN (throws);
c877974e 15276 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15277 {
15278 TREE_CHAIN (throws) = new;
15279 new = throws;
15280 }
15281 throws = next;
15282 }
15283 /* List is inverted here, but it doesn't matter */
15284 DECL_FUNCTION_THROWS (mdecl) = new;
15285}
22eed1e6
APB
15286
15287/* 15.24 Conditional Operator ?: */
15288
15289static tree
15290patch_conditional_expr (node, wfl_cond, wfl_op1)
15291 tree node, wfl_cond, wfl_op1;
15292{
15293 tree cond = TREE_OPERAND (node, 0);
15294 tree op1 = TREE_OPERAND (node, 1);
15295 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15296 tree resulting_type = NULL_TREE;
ac825856 15297 tree t1, t2, patched;
22eed1e6
APB
15298 int error_found = 0;
15299
ac825856
APB
15300 /* Operands of ?: might be StringBuffers crafted as a result of a
15301 string concatenation. Obtain a descent operand here. */
15302 if ((patched = patch_string (op1)))
15303 TREE_OPERAND (node, 1) = op1 = patched;
15304 if ((patched = patch_string (op2)))
15305 TREE_OPERAND (node, 2) = op2 = patched;
15306
15307 t1 = TREE_TYPE (op1);
15308 t2 = TREE_TYPE (op2);
15309
22eed1e6
APB
15310 /* The first expression must be a boolean */
15311 if (TREE_TYPE (cond) != boolean_type_node)
15312 {
15313 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15314 parse_error_context (wfl_operator,
15315 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15316 lang_printable_name (TREE_TYPE (cond), 0));
15317 error_found = 1;
15318 }
15319
15320 /* Second and third can be numeric, boolean (i.e. primitive),
15321 references or null. Anything else results in an error */
15322 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15323 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15324 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15325 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15326 error_found = 1;
15327
15328 /* Determine the type of the conditional expression. Same types are
15329 easy to deal with */
15330 else if (t1 == t2)
15331 resulting_type = t1;
15332
15333 /* There are different rules for numeric types */
15334 else if (JNUMERIC_TYPE_P (t1))
15335 {
15336 /* if byte/short found, the resulting type is short */
15337 if ((t1 == byte_type_node && t2 == short_type_node)
15338 || (t1 == short_type_node && t2 == byte_type_node))
15339 resulting_type = short_type_node;
15340
15341 /* If t1 is a constant int and t2 is of type byte, short or char
15342 and t1's value fits in t2, then the resulting type is t2 */
15343 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15344 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15345 resulting_type = t2;
15346
15347 /* If t2 is a constant int and t1 is of type byte, short or char
15348 and t2's value fits in t1, then the resulting type is t1 */
15349 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15350 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15351 resulting_type = t1;
15352
15353 /* Otherwise, binary numeric promotion is applied and the
15354 resulting type is the promoted type of operand 1 and 2 */
15355 else
93024893 15356 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15357 &TREE_OPERAND (node, 1),
15358 &TREE_OPERAND (node, 2));
15359 }
15360
15361 /* Cases of a reference and a null type */
15362 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15363 resulting_type = t1;
15364
15365 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15366 resulting_type = t2;
15367
15368 /* Last case: different reference types. If a type can be converted
15369 into the other one by assignment conversion, the latter
15370 determines the type of the expression */
15371 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15372 resulting_type = promote_type (t1);
15373
15374 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15375 resulting_type = promote_type (t2);
15376
15377 /* If we don't have any resulting type, we're in trouble */
15378 if (!resulting_type)
15379 {
c2e3db92 15380 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15381 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15382 parse_error_context (wfl_operator,
15383 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15384 t, lang_printable_name (t2, 0));
22eed1e6
APB
15385 free (t);
15386 error_found = 1;
15387 }
15388
15389 if (error_found)
15390 {
15391 TREE_TYPE (node) = error_mark_node;
15392 return error_mark_node;
15393 }
15394
15395 TREE_TYPE (node) = resulting_type;
15396 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15397 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15398 return node;
15399}
ac825856 15400
5b09b33e
PB
15401/* Try to constant fold NODE.
15402 If NODE is not a constant expression, return NULL_EXPR.
15403 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15404
15405static tree
15406fold_constant_for_init (node, context)
15407 tree node;
15408 tree context;
15409{
15410 tree op0, op1, val;
15411 enum tree_code code = TREE_CODE (node);
15412
ee97d354 15413 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15414 return node;
93024893 15415
5b09b33e
PB
15416 switch (code)
15417 {
5b09b33e
PB
15418 case PLUS_EXPR:
15419 case MINUS_EXPR:
bc3ca41b
PB
15420 case MULT_EXPR:
15421 case TRUNC_MOD_EXPR:
15422 case RDIV_EXPR:
5b09b33e
PB
15423 case LSHIFT_EXPR:
15424 case RSHIFT_EXPR:
15425 case URSHIFT_EXPR:
15426 case BIT_AND_EXPR:
15427 case BIT_XOR_EXPR:
15428 case BIT_IOR_EXPR:
5b09b33e
PB
15429 case TRUTH_ANDIF_EXPR:
15430 case TRUTH_ORIF_EXPR:
15431 case EQ_EXPR:
15432 case NE_EXPR:
15433 case GT_EXPR:
15434 case GE_EXPR:
15435 case LT_EXPR:
15436 case LE_EXPR:
15437 op0 = TREE_OPERAND (node, 0);
15438 op1 = TREE_OPERAND (node, 1);
15439 val = fold_constant_for_init (op0, context);
15440 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15441 return NULL_TREE;
15442 TREE_OPERAND (node, 0) = val;
15443 val = fold_constant_for_init (op1, context);
15444 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15445 return NULL_TREE;
15446 TREE_OPERAND (node, 1) = val;
15447 return patch_binop (node, op0, op1);
15448
15449 case UNARY_PLUS_EXPR:
15450 case NEGATE_EXPR:
15451 case TRUTH_NOT_EXPR:
15452 case BIT_NOT_EXPR:
15453 case CONVERT_EXPR:
15454 op0 = TREE_OPERAND (node, 0);
15455 val = fold_constant_for_init (op0, context);
15456 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15457 return NULL_TREE;
15458 TREE_OPERAND (node, 0) = val;
5a005d9e 15459 return patch_unaryop (node, op0);
5b09b33e
PB
15460 break;
15461
15462 case COND_EXPR:
15463 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15464 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15465 return NULL_TREE;
15466 TREE_OPERAND (node, 0) = val;
15467 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15468 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15469 return NULL_TREE;
15470 TREE_OPERAND (node, 1) = val;
15471 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15472 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15473 return NULL_TREE;
15474 TREE_OPERAND (node, 2) = val;
15475 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15476 : TREE_OPERAND (node, 2);
15477
15478 case VAR_DECL:
8576f094
APB
15479 case FIELD_DECL:
15480 if (! FIELD_FINAL (node)
5b09b33e
PB
15481 || DECL_INITIAL (node) == NULL_TREE)
15482 return NULL_TREE;
15483 val = DECL_INITIAL (node);
15484 /* Guard against infinite recursion. */
15485 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15486 val = fold_constant_for_init (val, node);
5b09b33e 15487 DECL_INITIAL (node) = val;
c7303e41
APB
15488 if (!val && CLASS_FINAL_VARIABLE_P (node))
15489 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15490 return val;
15491
15492 case EXPR_WITH_FILE_LOCATION:
15493 /* Compare java_complete_tree and resolve_expression_name. */
15494 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15495 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15496 {
15497 tree name = EXPR_WFL_NODE (node);
15498 tree decl;
15499 if (PRIMARY_P (node))
15500 return NULL_TREE;
15501 else if (! QUALIFIED_P (name))
15502 {
15503 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15504 if (decl == NULL_TREE
15505 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15506 return NULL_TREE;
15507 return fold_constant_for_init (decl, decl);
15508 }
15509 else
15510 {
5b09b33e
PB
15511 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15512 qualify_ambiguous_name (node);
15513 if (resolve_field_access (node, &decl, NULL)
15514 && decl != NULL_TREE)
15515 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15516 return NULL_TREE;
15517 }
15518 }
15519 else
15520 {
15521 op0 = TREE_OPERAND (node, 0);
15522 val = fold_constant_for_init (op0, context);
15523 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15524 return NULL_TREE;
15525 TREE_OPERAND (node, 0) = val;
15526 return val;
15527 }
15528
bc3ca41b
PB
15529#ifdef USE_COMPONENT_REF
15530 case IDENTIFIER:
15531 case COMPONENT_REF:
15532 ?;
15533#endif
15534
5b09b33e
PB
15535 default:
15536 return NULL_TREE;
15537 }
15538}
bc3ca41b
PB
15539
15540#ifdef USE_COMPONENT_REF
15541/* Context is 'T' for TypeName, 'P' for PackageName,
15542 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15543
15544tree
15545resolve_simple_name (name, context)
15546 tree name;
15547 int context;
15548{
15549}
15550
15551tree
15552resolve_qualified_name (name, context)
15553 tree name;
15554 int context;
15555{
15556}
15557#endif
f15b9af9
MM
15558
15559/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15560
15561static void
15562mark_parser_ctxt (p)
15563 void *p;
15564{
15565 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15566 int i;
15567
15568 if (!pc)
15569 return;
15570
15571#ifndef JC1_LITE
15572 for (i = 0; i < 11; ++i)
15573 ggc_mark_tree (pc->modifier_ctx[i]);
15574 ggc_mark_tree (pc->class_type);
15575 ggc_mark_tree (pc->function_decl);
15576 ggc_mark_tree (pc->package);
15577 ggc_mark_tree (pc->incomplete_class);
15578 ggc_mark_tree (pc->gclass_list);
15579 ggc_mark_tree (pc->class_list);
15580 ggc_mark_tree (pc->current_parsed_class);
15581 ggc_mark_tree (pc->current_parsed_class_un);
15582 ggc_mark_tree (pc->non_static_initialized);
15583 ggc_mark_tree (pc->static_initialized);
15584 ggc_mark_tree (pc->instance_initializers);
15585 ggc_mark_tree (pc->import_list);
15586 ggc_mark_tree (pc->import_demand_list);
15587 ggc_mark_tree (pc->current_loop);
15588 ggc_mark_tree (pc->current_labeled_block);
15589#endif /* JC1_LITE */
15590
15591 if (pc->next)
15592 mark_parser_ctxt (&pc->next);
15593}
This page took 2.900698 seconds and 5 git commands to generate.