]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
reload1.c (move2add_note_store): Update reg_set_luid even if base reg remains the...
[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.
df32d2ce 3 Copyright (C) 1997, 1998, 1999, 2000 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"
0ae70c6a 67#include "defaults.h"
19e223db 68#include "ggc.h"
e04a16fb 69
c2952b01
APB
70#ifndef DIR_SEPARATOR
71#define DIR_SEPARATOR '/'
72#endif
73
82371d41 74/* Local function prototypes */
df32d2ce
KG
75static char *java_accstring_lookup PARAMS ((int));
76static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
78static tree create_class PARAMS ((int, tree, tree, tree));
79static tree create_interface PARAMS ((int, tree, tree));
c2952b01 80static void end_class_declaration PARAMS ((int));
df32d2ce
KG
81static tree find_field PARAMS ((tree, tree));
82static tree lookup_field_wrapper PARAMS ((tree, tree));
83static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84static void register_fields PARAMS ((int, tree, tree));
98a52c2c 85static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
86static int parser_check_super PARAMS ((tree, tree, tree));
87static int parser_check_super_interface PARAMS ((tree, tree, tree));
88static void check_modifiers_consistency PARAMS ((int));
89static tree lookup_cl PARAMS ((tree));
90static tree lookup_java_method2 PARAMS ((tree, tree, int));
91static tree method_header PARAMS ((int, tree, tree, tree));
92static void fix_method_argument_names PARAMS ((tree ,tree));
93static tree method_declarator PARAMS ((tree, tree));
94static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 95 ATTRIBUTE_PRINTF_2;
88f3c477
JM
96static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
97 ATTRIBUTE_PRINTF (2, 0);
df32d2ce
KG
98static void parse_ctor_invocation_error PARAMS ((void));
99static tree parse_jdk1_1_error PARAMS ((const char *));
100static void complete_class_report_errors PARAMS ((jdep *));
101static int process_imports PARAMS ((void));
102static void read_import_dir PARAMS ((tree));
103static int find_in_imports_on_demand PARAMS ((tree));
9a7ab4b3 104static void find_in_imports PARAMS ((tree));
c7303e41
APB
105static void check_static_final_variable_assignment_flag PARAMS ((tree));
106static void reset_static_final_variable_assignment_flag PARAMS ((tree));
107static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
108static void reset_final_variable_local_assignment_flag PARAMS ((tree));
109static int check_final_variable_indirect_assignment PARAMS ((tree));
110static void check_final_variable_global_assignment_flag PARAMS ((tree));
cf1748bf 111static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 112static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 113static void register_package PARAMS ((tree));
df32d2ce
KG
114static tree resolve_package PARAMS ((tree, tree *));
115static tree lookup_package_type PARAMS ((const char *, int));
116static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 117static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
118static void declare_local_variables PARAMS ((int, tree, tree));
119static void source_start_java_method PARAMS ((tree));
120static void source_end_java_method PARAMS ((void));
121static void expand_start_java_method PARAMS ((tree));
122static tree find_name_in_single_imports PARAMS ((tree));
123static void check_abstract_method_header PARAMS ((tree));
124static tree lookup_java_interface_method2 PARAMS ((tree, tree));
125static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 126static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 127static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 128 tree, tree, tree));
df32d2ce 129static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 130 int *, tree *));
df32d2ce
KG
131static int breakdown_qualified PARAMS ((tree *, tree *, tree));
132static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 133static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
134static tree resolve_no_layout PARAMS ((tree, tree));
135static int invocation_mode PARAMS ((tree, int));
136static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 137 tree, tree));
df32d2ce 138static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 139 tree *, tree *));
df32d2ce
KG
140static tree find_most_specific_methods_list PARAMS ((tree));
141static int argument_types_convertible PARAMS ((tree, tree));
142static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 143static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
144static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
145static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
146static tree obtain_incomplete_type PARAMS ((tree));
147static tree java_complete_lhs PARAMS ((tree));
148static tree java_complete_tree PARAMS ((tree));
c2952b01 149static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 150static int analyze_clinit_body PARAMS ((tree));
92d83515 151static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
152static void java_complete_expand_method PARAMS ((tree));
153static int unresolved_type_p PARAMS ((tree, tree *));
154static void create_jdep_list PARAMS ((struct parser_ctxt *));
155static tree build_expr_block PARAMS ((tree, tree));
156static tree enter_block PARAMS ((void));
157static tree enter_a_block PARAMS ((tree));
158static tree exit_block PARAMS ((void));
159static tree lookup_name_in_blocks PARAMS ((tree));
160static void maybe_absorb_scoping_blocks PARAMS ((void));
161static tree build_method_invocation PARAMS ((tree, tree));
162static tree build_new_invocation PARAMS ((tree, tree));
163static tree build_assignment PARAMS ((int, int, tree, tree));
164static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
165static int check_final_assignment PARAMS ((tree ,tree));
166static tree patch_assignment PARAMS ((tree, tree, tree ));
167static tree patch_binop PARAMS ((tree, tree, tree));
168static tree build_unaryop PARAMS ((int, int, tree));
169static tree build_incdec PARAMS ((int, int, tree, int));
170static tree patch_unaryop PARAMS ((tree, tree));
171static tree build_cast PARAMS ((int, tree, tree));
172static tree build_null_of_type PARAMS ((tree));
173static tree patch_cast PARAMS ((tree, tree));
174static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
175static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
176static int valid_cast_to_p PARAMS ((tree, tree));
177static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
178static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
179static tree try_reference_assignconv PARAMS ((tree, tree));
180static tree build_unresolved_array_type PARAMS ((tree));
181static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
182static tree build_array_ref PARAMS ((int, tree, tree));
183static tree patch_array_ref PARAMS ((tree));
184static tree make_qualified_name PARAMS ((tree, tree, int));
185static tree merge_qualified_name PARAMS ((tree, tree));
186static tree make_qualified_primary PARAMS ((tree, tree, int));
187static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 188 tree *, tree *));
df32d2ce 189static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
190static tree resolve_field_access PARAMS ((tree, tree *, tree *));
191static tree build_newarray_node PARAMS ((tree, tree, int));
192static tree patch_newarray PARAMS ((tree));
193static tree resolve_type_during_patch PARAMS ((tree));
194static tree build_this PARAMS ((int));
9a7ab4b3 195static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
196static tree build_return PARAMS ((int, tree));
197static tree patch_return PARAMS ((tree));
198static tree maybe_access_field PARAMS ((tree, tree, tree));
199static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
200static int check_for_static_method_reference PARAMS ((tree, tree, tree,
201 tree, tree));
df32d2ce
KG
202static int not_accessible_p PARAMS ((tree, tree, int));
203static void check_deprecation PARAMS ((tree, tree));
204static int class_in_current_package PARAMS ((tree));
205static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
206static tree patch_if_else_statement PARAMS ((tree));
207static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
208static tree add_stmt_to_block PARAMS ((tree, tree, tree));
209static tree patch_exit_expr PARAMS ((tree));
210static tree build_labeled_block PARAMS ((int, tree));
211static tree finish_labeled_statement PARAMS ((tree, tree));
212static tree build_bc_statement PARAMS ((int, int, tree));
213static tree patch_bc_statement PARAMS ((tree));
214static tree patch_loop_statement PARAMS ((tree));
215static tree build_new_loop PARAMS ((tree));
216static tree build_loop_body PARAMS ((int, tree, int));
217static tree finish_loop_body PARAMS ((int, tree, tree, int));
218static tree build_debugable_stmt PARAMS ((int, tree));
219static tree finish_for_loop PARAMS ((int, tree, tree, tree));
220static tree patch_switch_statement PARAMS ((tree));
221static tree string_constant_concatenation PARAMS ((tree, tree));
222static tree build_string_concatenation PARAMS ((tree, tree));
223static tree patch_string_cst PARAMS ((tree));
224static tree patch_string PARAMS ((tree));
225static tree build_try_statement PARAMS ((int, tree, tree));
226static tree build_try_finally_statement PARAMS ((int, tree, tree));
227static tree patch_try_statement PARAMS ((tree));
228static tree patch_synchronized_statement PARAMS ((tree, tree));
229static tree patch_throw_statement PARAMS ((tree, tree));
230static void check_thrown_exceptions PARAMS ((int, tree));
231static int check_thrown_exceptions_do PARAMS ((tree));
232static void purge_unchecked_exceptions PARAMS ((tree));
233static void check_throws_clauses PARAMS ((tree, tree, tree));
234static void finish_method_declaration PARAMS ((tree));
235static tree build_super_invocation PARAMS ((tree));
236static int verify_constructor_circularity PARAMS ((tree, tree));
237static char *constructor_circularity_msg PARAMS ((tree, tree));
238static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 239 int, int));
df32d2ce
KG
240static const char *get_printable_method_name PARAMS ((tree));
241static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
242static tree generate_finit PARAMS ((tree));
243static void add_instance_initializer PARAMS ((tree));
df32d2ce 244static void fix_constructors PARAMS ((tree));
c2952b01
APB
245static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
246 tree, int *));
247static void craft_constructor PARAMS ((tree, tree));
248static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
249static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
250static void start_artificial_method_body PARAMS ((tree));
251static void end_artificial_method_body PARAMS ((tree));
252static int check_method_redefinition PARAMS ((tree, tree));
253static int reset_method_name PARAMS ((tree));
165f37bc 254static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
255static void java_check_regular_methods PARAMS ((tree));
256static void java_check_abstract_methods PARAMS ((tree));
257static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
258static void unreachable_stmt_error PARAMS ((tree));
259static tree find_expr_with_wfl PARAMS ((tree));
260static void missing_return_error PARAMS ((tree));
261static tree build_new_array_init PARAMS ((int, tree));
262static tree patch_new_array_init PARAMS ((tree, tree));
263static tree maybe_build_array_element_wfl PARAMS ((tree));
264static int array_constructor_check_entry PARAMS ((tree, tree));
265static const char *purify_type_name PARAMS ((const char *));
266static tree fold_constant_for_init PARAMS ((tree, tree));
267static tree strip_out_static_field_access_decl PARAMS ((tree));
268static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
269static void static_ref_err PARAMS ((tree, tree, tree));
270static void parser_add_interface PARAMS ((tree, tree, tree));
271static void add_superinterfaces PARAMS ((tree, tree));
272static tree jdep_resolve_class PARAMS ((jdep *));
273static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
274static void java_complete_expand_classes PARAMS ((void));
275static void java_complete_expand_class PARAMS ((tree));
276static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
277static tree cut_identifier_in_qualified PARAMS ((tree));
278static tree java_stabilize_reference PARAMS ((tree));
279static tree do_unary_numeric_promotion PARAMS ((tree));
280static char * operator_string PARAMS ((tree));
281static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
282static tree merge_string_cste PARAMS ((tree, tree, int));
283static tree java_refold PARAMS ((tree));
284static int java_decl_equiv PARAMS ((tree, tree));
285static int binop_compound_p PARAMS ((enum tree_code));
286static tree search_loop PARAMS ((tree));
287static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 288static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
289static void java_check_abstract_method_definitions PARAMS ((tree));
290static void java_debug_context_do PARAMS ((int));
c2952b01
APB
291static void java_parser_context_push_initialized_field PARAMS ((void));
292static void java_parser_context_pop_initialized_field PARAMS ((void));
293static tree reorder_static_initialized PARAMS ((tree));
294static void java_parser_context_suspend PARAMS ((void));
295static void java_parser_context_resume PARAMS ((void));
c7303e41 296static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
297
298/* JDK 1.1 work. FIXME */
299
300static tree maybe_make_nested_class_name PARAMS ((tree));
301static void make_nested_class_name PARAMS ((tree));
302static void set_nested_class_simple_name_value PARAMS ((tree, int));
303static void link_nested_class_to_enclosing PARAMS ((void));
304static tree find_as_inner_class PARAMS ((tree, tree, tree));
305static tree find_as_inner_class_do PARAMS ((tree, tree));
306static int check_inner_class_redefinition PARAMS ((tree, tree));
307
308static tree build_thisn_assign PARAMS ((void));
309static tree build_current_thisn PARAMS ((tree));
310static tree build_access_to_thisn PARAMS ((tree, tree, int));
311static tree maybe_build_thisn_access_method PARAMS ((tree));
312
313static tree build_outer_field_access PARAMS ((tree, tree));
314static tree build_outer_field_access_methods PARAMS ((tree));
315static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
316 tree, tree));
317static tree build_outer_method_access_method PARAMS ((tree));
318static tree build_new_access_id PARAMS ((void));
319static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
320 tree, tree));
321
322static int outer_field_access_p PARAMS ((tree, tree));
323static int outer_field_expanded_access_p PARAMS ((tree, tree *,
324 tree *, tree *));
325static tree outer_field_access_fix PARAMS ((tree, tree, tree));
326static tree build_incomplete_class_ref PARAMS ((int, tree));
327static tree patch_incomplete_class_ref PARAMS ((tree));
328static tree create_anonymous_class PARAMS ((int, tree));
329static void patch_anonymous_class PARAMS ((tree, tree, tree));
330static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 331
165f37bc
APB
332static tree build_dot_class_method PARAMS ((tree));
333static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 334static void create_new_parser_context PARAMS ((int));
f15b9af9 335static void mark_parser_ctxt PARAMS ((void *));
165f37bc 336
e04a16fb
AG
337/* Number of error found so far. */
338int java_error_count;
339/* Number of warning found so far. */
340int java_warning_count;
ce6e9147
APB
341/* Tell when not to fold, when doing xrefs */
342int do_not_fold;
c2952b01 343/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 344const char *cyclic_inheritance_report;
f15b9af9 345
c2952b01
APB
346/* Tell when we're within an instance initializer */
347static int in_instance_initializer;
e04a16fb
AG
348
349/* The current parser context */
d4370213 350struct parser_ctxt *ctxp;
e04a16fb 351
d4370213 352/* List of things that were analyzed for which code will be generated */
b351b287
APB
353static struct parser_ctxt *ctxp_for_generation = NULL;
354
e04a16fb
AG
355/* binop_lookup maps token to tree_code. It is used where binary
356 operations are involved and required by the parser. RDIV_EXPR
357 covers both integral/floating point division. The code is changed
358 once the type of both operator is worked out. */
359
360static enum tree_code binop_lookup[19] =
361 {
362 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
363 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
364 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
365 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
366 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
367 };
368#define BINOP_LOOKUP(VALUE) \
6e2aa220 369 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 370
5cbdba64
APB
371/* This is the end index for binary operators that can also be used
372 in compound assignements. */
373#define BINOP_COMPOUND_CANDIDATES 11
374
e04a16fb 375/* The "$L" identifier we use to create labels. */
b67d701b
PB
376static tree label_id = NULL_TREE;
377
378/* The "StringBuffer" identifier used for the String `+' operator. */
379static tree wfl_string_buffer = NULL_TREE;
380
381/* The "append" identifier used for String `+' operator. */
382static tree wfl_append = NULL_TREE;
383
384/* The "toString" identifier used for String `+' operator. */
385static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
386
387/* The "java.lang" import qualified name. */
388static tree java_lang_id = NULL_TREE;
09ed0f70 389
c2952b01
APB
390/* The generated `inst$' identifier used for generated enclosing
391 instance/field access functions. */
392static tree inst_id = NULL_TREE;
393
09ed0f70
APB
394/* The "java.lang.Cloneable" qualified name. */
395static tree java_lang_cloneable = NULL_TREE;
f099f336 396
ee17a290
TT
397/* The "java.io.Serializable" qualified name. */
398static tree java_io_serializable = NULL_TREE;
399
f099f336
APB
400/* Context and flag for static blocks */
401static tree current_static_block = NULL_TREE;
402
c2952b01
APB
403/* The generated `write_parm_value$' identifier. */
404static tree wpv_id;
405
ee07f4f4
APB
406/* The list of all packages we've seen so far */
407static tree package_list = NULL_TREE;
2884c41e 408
19e223db
MM
409/* Hold THIS for the scope of the current public method decl. */
410static tree current_this;
411
412/* Hold a list of catch clauses list. The first element of this list is
413 the list of the catch clauses of the currently analysed try block. */
414static tree currently_caught_type_list;
415
2884c41e
KG
416/* Check modifiers. If one doesn't fit, retrieve it in its declaration
417 line and point it out. */
418/* Should point out the one that don't fit. ASCII/unicode, going
419 backward. FIXME */
420
421#define check_modifiers(__message, __value, __mask) do { \
422 if ((__value) & ~(__mask)) \
423 { \
424 int i, remainder = (__value) & ~(__mask); \
425 for (i = 0; i <= 10; i++) \
426 if ((1 << i) & remainder) \
427 parse_error_context (ctxp->modifier_ctx [i], (__message), \
428 java_accstring_lookup (1 << i)); \
429 } \
430} while (0)
ee07f4f4 431
e04a16fb
AG
432%}
433
434%union {
435 tree node;
436 int sub_token;
437 struct {
438 int token;
439 int location;
440 } operator;
441 int value;
442}
443
9ee9b555
KG
444%{
445#include "lex.c"
446%}
447
e04a16fb
AG
448%pure_parser
449
450/* Things defined here have to match the order of what's in the
451 binop_lookup table. */
452
453%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
454%token LS_TK SRS_TK ZRS_TK
455%token AND_TK XOR_TK OR_TK
456%token BOOL_AND_TK BOOL_OR_TK
457%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
458
459/* This maps to the same binop_lookup entry than the token above */
460
461%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
462%token REM_ASSIGN_TK
463%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
464%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
465
466
467/* Modifier TOKEN have to be kept in this order. Don't scramble it */
468
469%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
470%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
471%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
472%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 473%token STRICT_TK
e04a16fb
AG
474
475/* Keep those two in order, too */
476%token DECR_TK INCR_TK
477
478/* From now one, things can be in any order */
479
480%token DEFAULT_TK IF_TK THROW_TK
481%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
482%token THROWS_TK BREAK_TK IMPORT_TK
483%token ELSE_TK INSTANCEOF_TK RETURN_TK
484%token VOID_TK CATCH_TK INTERFACE_TK
485%token CASE_TK EXTENDS_TK FINALLY_TK
486%token SUPER_TK WHILE_TK CLASS_TK
487%token SWITCH_TK CONST_TK TRY_TK
488%token FOR_TK NEW_TK CONTINUE_TK
489%token GOTO_TK PACKAGE_TK THIS_TK
490
491%token BYTE_TK SHORT_TK INT_TK LONG_TK
492%token CHAR_TK INTEGRAL_TK
493
494%token FLOAT_TK DOUBLE_TK FP_TK
495
496%token ID_TK
497
498%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
499
500%token ASSIGN_ANY_TK ASSIGN_TK
501%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
502
503%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
504%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
505
c2952b01 506%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
507
508%type <node> super ID_TK identifier
509%type <node> name simple_name qualified_name
c2952b01 510%type <node> type_declaration compilation_unit
e04a16fb
AG
511 field_declaration method_declaration extends_interfaces
512 interfaces interface_type_list
c2952b01 513 class_member_declaration
e04a16fb
AG
514 import_declarations package_declaration
515 type_declarations interface_body
516 interface_member_declaration constant_declaration
517 interface_member_declarations interface_type
518 abstract_method_declaration interface_type_list
519%type <node> class_body_declaration class_member_declaration
520 static_initializer constructor_declaration block
22eed1e6 521%type <node> class_body_declarations constructor_header
e04a16fb
AG
522%type <node> class_or_interface_type class_type class_type_list
523 constructor_declarator explicit_constructor_invocation
b9f7e36c 524%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
525
526%type <node> variable_declarator_id variable_declarator
527 variable_declarators variable_initializer
22eed1e6 528 variable_initializers constructor_body
ac825856 529 array_initializer
e04a16fb 530
2e5eb5c5 531%type <node> class_body block_end constructor_block_end
e04a16fb
AG
532%type <node> statement statement_without_trailing_substatement
533 labeled_statement if_then_statement label_decl
534 if_then_else_statement while_statement for_statement
535 statement_nsi labeled_statement_nsi do_statement
536 if_then_else_statement_nsi while_statement_nsi
537 for_statement_nsi statement_expression_list for_init
538 for_update statement_expression expression_statement
539 primary_no_new_array expression primary
540 array_creation_expression array_type
541 class_instance_creation_expression field_access
542 method_invocation array_access something_dot_new
543 argument_list postfix_expression while_expression
544 post_increment_expression post_decrement_expression
545 unary_expression_not_plus_minus unary_expression
546 pre_increment_expression pre_decrement_expression
547 unary_expression_not_plus_minus cast_expression
548 multiplicative_expression additive_expression
549 shift_expression relational_expression
550 equality_expression and_expression
551 exclusive_or_expression inclusive_or_expression
552 conditional_and_expression conditional_or_expression
553 conditional_expression assignment_expression
554 left_hand_side assignment for_header for_begin
555 constant_expression do_statement_begin empty_statement
b67d701b 556 switch_statement synchronized_statement throw_statement
f8976021 557 try_statement switch_expression switch_block
15fdcfe9 558 catches catch_clause catch_clause_parameter finally
c2952b01 559 anonymous_class_creation
e04a16fb
AG
560%type <node> return_statement break_statement continue_statement
561
562%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
563%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
564%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
565%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
566%type <operator> ASSIGN_ANY_TK assignment_operator
567%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
568%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
569%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 570%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 571%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
572%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
573%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 574%type <operator> NEW_TK
e04a16fb
AG
575
576%type <node> method_body
577
578%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
579 STRING_LIT_TK NULL_TK VOID_TK
580
581%type <node> IF_TK WHILE_TK FOR_TK
582
583%type <node> formal_parameter_list formal_parameter
584 method_declarator method_header
585
c2952b01 586%type <node> primitive_type reference_type type
e04a16fb
AG
587 BOOLEAN_TK INTEGRAL_TK FP_TK
588
c2952b01 589/* Added or modified JDK 1.1 rule types */
c7303e41 590%type <node> type_literals
c2952b01 591
e04a16fb
AG
592%%
593/* 19.2 Production from 2.3: The Syntactic Grammar */
594goal:
19e223db
MM
595 {
596 /* Register static variables with the garbage
597 collector. */
598 ggc_add_tree_root (&label_id, 1);
599 ggc_add_tree_root (&wfl_string_buffer, 1);
600 ggc_add_tree_root (&wfl_append, 1);
601 ggc_add_tree_root (&wfl_to_string, 1);
602 ggc_add_tree_root (&java_lang_id, 1);
603 ggc_add_tree_root (&inst_id, 1);
604 ggc_add_tree_root (&java_lang_cloneable, 1);
605 ggc_add_tree_root (&java_io_serializable, 1);
606 ggc_add_tree_root (&current_static_block, 1);
607 ggc_add_tree_root (&wpv_id, 1);
608 ggc_add_tree_root (&package_list, 1);
609 ggc_add_tree_root (&current_this, 1);
610 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
611 ggc_add_root (&ctxp, 1,
612 sizeof (struct parser_ctxt *),
613 mark_parser_ctxt);
614 ggc_add_root (&ctxp_for_generation, 1,
615 sizeof (struct parser_ctxt *),
616 mark_parser_ctxt);
19e223db 617 }
e04a16fb
AG
618 compilation_unit
619 {}
620;
621
622/* 19.3 Productions from 3: Lexical structure */
623literal:
624 INT_LIT_TK
625| FP_LIT_TK
626| BOOL_LIT_TK
627| CHAR_LIT_TK
628| STRING_LIT_TK
629| NULL_TK
630;
631
632/* 19.4 Productions from 4: Types, Values and Variables */
633type:
634 primitive_type
635| reference_type
636;
637
638primitive_type:
639 INTEGRAL_TK
640| FP_TK
641| BOOLEAN_TK
642;
643
644reference_type:
645 class_or_interface_type
646| array_type
647;
648
649class_or_interface_type:
650 name
651;
652
653class_type:
654 class_or_interface_type /* Default rule */
655;
656
657interface_type:
658 class_or_interface_type
659;
660
661array_type:
c7303e41 662 primitive_type dims
e04a16fb 663 {
c7303e41
APB
664 int osb = pop_current_osb (ctxp);
665 tree t = build_java_array_type (($1), -1);
666 CLASS_LOADED_P (t) = 1;
667 while (--osb)
668 t = build_unresolved_array_type (t);
669 $$ = t;
670 }
671| name dims
672 {
673 int osb = pop_current_osb (ctxp);
674 tree t = $1;
675 while (osb--)
676 t = build_unresolved_array_type (t);
677 $$ = t;
e04a16fb 678 }
e04a16fb
AG
679;
680
681/* 19.5 Productions from 6: Names */
682name:
683 simple_name /* Default rule */
684| qualified_name /* Default rule */
685;
686
687simple_name:
688 identifier /* Default rule */
689;
690
691qualified_name:
692 name DOT_TK identifier
693 { $$ = make_qualified_name ($1, $3, $2.location); }
694;
695
696identifier:
697 ID_TK
698;
699
700/* 19.6: Production from 7: Packages */
701compilation_unit:
702 {$$ = NULL;}
703| package_declaration
704| import_declarations
705| type_declarations
706| package_declaration import_declarations
707| package_declaration type_declarations
708| import_declarations type_declarations
709| package_declaration import_declarations type_declarations
710;
711
712import_declarations:
713 import_declaration
714 {
715 $$ = NULL;
716 }
717| import_declarations import_declaration
718 {
719 $$ = NULL;
720 }
721;
722
723type_declarations:
724 type_declaration
725| type_declarations type_declaration
726;
727
728package_declaration:
729 PACKAGE_TK name SC_TK
ee07f4f4
APB
730 {
731 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 732 register_package (ctxp->package);
ee07f4f4 733 }
e04a16fb
AG
734| PACKAGE_TK error
735 {yyerror ("Missing name"); RECOVER;}
736| PACKAGE_TK name error
737 {yyerror ("';' expected"); RECOVER;}
738;
739
740import_declaration:
741 single_type_import_declaration
742| type_import_on_demand_declaration
743;
744
745single_type_import_declaration:
746 IMPORT_TK name SC_TK
747 {
9a7ab4b3 748 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 749 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 750 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
751 while (last != IDENTIFIER_POINTER (name))
752 {
753 if (last [0] == '.')
754 break;
755 last--;
756 }
757 last_name = get_identifier (++last);
758 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
759 {
760 tree err = find_name_in_single_imports (last_name);
761 if (err && err != name)
762 parse_error_context
763 ($2, "Ambiguous class: `%s' and `%s'",
764 IDENTIFIER_POINTER (name),
765 IDENTIFIER_POINTER (err));
5e942c50 766 else
9a7ab4b3 767 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
768 }
769 else
5e942c50 770 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
771 }
772| IMPORT_TK error
773 {yyerror ("Missing name"); RECOVER;}
774| IMPORT_TK name error
775 {yyerror ("';' expected"); RECOVER;}
776;
777
778type_import_on_demand_declaration:
779 IMPORT_TK name DOT_TK MULT_TK SC_TK
780 {
781 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
782 /* Don't import java.lang.* twice. */
783 if (name != java_lang_id)
784 {
ba179f9f 785 read_import_dir ($2);
9a7ab4b3
APB
786 ctxp->import_demand_list =
787 chainon (ctxp->import_demand_list,
788 build_tree_list ($2, NULL_TREE));
ba179f9f 789 }
e04a16fb
AG
790 }
791| IMPORT_TK name DOT_TK error
792 {yyerror ("'*' expected"); RECOVER;}
793| IMPORT_TK name DOT_TK MULT_TK error
794 {yyerror ("';' expected"); RECOVER;}
795;
796
797type_declaration:
798 class_declaration
c2952b01 799 { end_class_declaration (0); }
e04a16fb 800| interface_declaration
c2952b01 801 { end_class_declaration (0); }
5f1c312a 802| empty_statement
e04a16fb
AG
803| error
804 {
805 YYERROR_NOW;
806 yyerror ("Class or interface declaration expected");
807 }
808;
809
810/* 19.7 Shortened from the original:
811 modifiers: modifier | modifiers modifier
812 modifier: any of public... */
813modifiers:
814 MODIFIER_TK
815 {
816 $$ = (1 << $1);
817 }
818| modifiers MODIFIER_TK
819 {
820 int acc = (1 << $2);
821 if ($$ & acc)
822 parse_error_context
823 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
824 java_accstring_lookup (acc));
825 else
826 {
827 $$ |= acc;
828 }
829 }
830;
831
832/* 19.8.1 Production from $8.1: Class Declaration */
833class_declaration:
834 modifiers CLASS_TK identifier super interfaces
835 { create_class ($1, $3, $4, $5); }
836 class_body
e04a16fb
AG
837| CLASS_TK identifier super interfaces
838 { create_class (0, $2, $3, $4); }
839 class_body
e04a16fb
AG
840| modifiers CLASS_TK error
841 {yyerror ("Missing class name"); RECOVER;}
842| CLASS_TK error
843 {yyerror ("Missing class name"); RECOVER;}
844| CLASS_TK identifier error
0b4d333e
APB
845 {
846 if (!ctxp->class_err) yyerror ("'{' expected");
847 DRECOVER(class1);
848 }
e04a16fb
AG
849| modifiers CLASS_TK identifier error
850 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
851;
852
853super:
854 { $$ = NULL; }
855| EXTENDS_TK class_type
856 { $$ = $2; }
857| EXTENDS_TK class_type error
858 {yyerror ("'{' expected"); ctxp->class_err=1;}
859| EXTENDS_TK error
860 {yyerror ("Missing super class name"); ctxp->class_err=1;}
861;
862
863interfaces:
864 { $$ = NULL_TREE; }
865| IMPLEMENTS_TK interface_type_list
866 { $$ = $2; }
867| IMPLEMENTS_TK error
868 {
869 ctxp->class_err=1;
870 yyerror ("Missing interface name");
871 }
872;
873
874interface_type_list:
875 interface_type
876 {
877 ctxp->interface_number = 1;
878 $$ = build_tree_list ($1, NULL_TREE);
879 }
880| interface_type_list C_TK interface_type
881 {
882 ctxp->interface_number++;
883 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
884 }
885| interface_type_list C_TK error
886 {yyerror ("Missing interface name"); RECOVER;}
887;
888
889class_body:
890 OCB_TK CCB_TK
7f10c2e2
APB
891 {
892 /* Store the location of the `}' when doing xrefs */
893 if (flag_emit_xref)
c2952b01 894 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 895 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 896 $$ = GET_CPC ();
7f10c2e2 897 }
e04a16fb 898| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
899 {
900 /* Store the location of the `}' when doing xrefs */
901 if (flag_emit_xref)
c2952b01 902 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 903 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 904 $$ = GET_CPC ();
7f10c2e2 905 }
e04a16fb
AG
906;
907
908class_body_declarations:
909 class_body_declaration
910| class_body_declarations class_body_declaration
911;
912
913class_body_declaration:
914 class_member_declaration
915| static_initializer
916| constructor_declaration
917| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
918 {
919 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
920 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
921 }
e04a16fb
AG
922;
923
924class_member_declaration:
925 field_declaration
926| method_declaration
927| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
928 { end_class_declaration (1); }
929| interface_declaration /* Added, JDK1.1 inner interfaces */
930 { end_class_declaration (1); }
5f1c312a 931| empty_statement
e04a16fb
AG
932;
933
934/* 19.8.2 Productions from 8.3: Field Declarations */
935field_declaration:
936 type variable_declarators SC_TK
937 { register_fields (0, $1, $2); }
938| modifiers type variable_declarators SC_TK
939 {
e04a16fb
AG
940 check_modifiers
941 ("Illegal modifier `%s' for field declaration",
942 $1, FIELD_MODIFIERS);
943 check_modifiers_consistency ($1);
944 register_fields ($1, $2, $3);
945 }
946;
947
948variable_declarators:
949 /* Should we use build_decl_list () instead ? FIXME */
950 variable_declarator /* Default rule */
951| variable_declarators C_TK variable_declarator
952 { $$ = chainon ($1, $3); }
953| variable_declarators C_TK error
954 {yyerror ("Missing term"); RECOVER;}
955;
956
957variable_declarator:
958 variable_declarator_id
959 { $$ = build_tree_list ($1, NULL_TREE); }
960| variable_declarator_id ASSIGN_TK variable_initializer
961 {
962 if (java_error_count)
963 $3 = NULL_TREE;
964 $$ = build_tree_list
965 ($1, build_assignment ($2.token, $2.location, $1, $3));
966 }
967| variable_declarator_id ASSIGN_TK error
968 {
969 yyerror ("Missing variable initializer");
970 $$ = build_tree_list ($1, NULL_TREE);
971 RECOVER;
972 }
973| variable_declarator_id ASSIGN_TK variable_initializer error
974 {
975 yyerror ("';' expected");
976 $$ = build_tree_list ($1, NULL_TREE);
977 RECOVER;
978 }
979;
980
981variable_declarator_id:
982 identifier
983| variable_declarator_id OSB_TK CSB_TK
c583dd46 984 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
985| identifier error
986 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
987| variable_declarator_id OSB_TK error
29f8b718
APB
988 {
989 tree node = java_lval.node;
990 if (node && (TREE_CODE (node) == INTEGER_CST
991 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
992 yyerror ("Can't specify array dimension in a declaration");
993 else
994 yyerror ("']' expected");
995 DRECOVER(vdi);
996 }
e04a16fb
AG
997| variable_declarator_id CSB_TK error
998 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
999;
1000
1001variable_initializer:
1002 expression
1003| array_initializer
e04a16fb
AG
1004;
1005
1006/* 19.8.3 Productions from 8.4: Method Declarations */
1007method_declaration:
1008 method_header
1009 {
1010 current_function_decl = $1;
c2952b01
APB
1011 if (current_function_decl
1012 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1013 source_start_java_method (current_function_decl);
1014 else
1015 current_function_decl = NULL_TREE;
e04a16fb
AG
1016 }
1017 method_body
b635eb2f 1018 { finish_method_declaration ($3); }
e04a16fb
AG
1019| method_header error
1020 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1021;
1022
1023method_header:
1024 type method_declarator throws
b9f7e36c 1025 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1026| VOID_TK method_declarator throws
b9f7e36c 1027 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1028| modifiers type method_declarator throws
b9f7e36c 1029 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1030| modifiers VOID_TK method_declarator throws
b9f7e36c 1031 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1032| type error
efa0a23f
APB
1033 {
1034 yyerror ("Invalid method declaration, method name required");
1035 RECOVER;
1036 }
e04a16fb
AG
1037| modifiers type error
1038 {RECOVER;}
1039| VOID_TK error
1040 {yyerror ("Identifier expected"); RECOVER;}
1041| modifiers VOID_TK error
1042 {yyerror ("Identifier expected"); RECOVER;}
1043| modifiers error
1044 {
1045 yyerror ("Invalid method declaration, return type required");
1046 RECOVER;
1047 }
1048;
1049
1050method_declarator:
1051 identifier OP_TK CP_TK
c2952b01
APB
1052 {
1053 ctxp->formal_parameter_number = 0;
1054 $$ = method_declarator ($1, NULL_TREE);
1055 }
e04a16fb
AG
1056| identifier OP_TK formal_parameter_list CP_TK
1057 { $$ = method_declarator ($1, $3); }
1058| method_declarator OSB_TK CSB_TK
1059 {
1886c9d8
APB
1060 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1061 TREE_PURPOSE ($1) =
1062 build_unresolved_array_type (TREE_PURPOSE ($1));
1063 parse_warning_context
1064 (wfl_operator,
1065 "Discouraged form of returned type specification");
e04a16fb
AG
1066 }
1067| identifier OP_TK error
1068 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1069| method_declarator OSB_TK error
1070 {yyerror ("']' expected"); RECOVER;}
1071;
1072
1073formal_parameter_list:
1074 formal_parameter
1075 {
1076 ctxp->formal_parameter_number = 1;
1077 }
1078| formal_parameter_list C_TK formal_parameter
1079 {
1080 ctxp->formal_parameter_number += 1;
1081 $$ = chainon ($1, $3);
1082 }
1083| formal_parameter_list C_TK error
c2952b01 1084 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1085;
1086
1087formal_parameter:
1088 type variable_declarator_id
1089 {
1090 $$ = build_tree_list ($2, $1);
1091 }
18990de5 1092| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1093 {
5256aa37 1094 $$ = build_tree_list ($3, $2);
c2952b01 1095 ARG_FINAL_P ($$) = 1;
5256aa37 1096 }
e04a16fb 1097| type error
f8989a66
APB
1098 {
1099 yyerror ("Missing identifier"); RECOVER;
1100 $$ = NULL_TREE;
1101 }
18990de5 1102| final type error
e04a16fb 1103 {
e04a16fb 1104 yyerror ("Missing identifier"); RECOVER;
f8989a66 1105 $$ = NULL_TREE;
e04a16fb
AG
1106 }
1107;
1108
18990de5
JB
1109final:
1110 modifiers
1111 {
1112 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1113 $1, ACC_FINAL);
1114 if ($1 != ACC_FINAL)
1115 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1116 }
1117;
1118
e04a16fb 1119throws:
b9f7e36c 1120 { $$ = NULL_TREE; }
e04a16fb 1121| THROWS_TK class_type_list
b9f7e36c 1122 { $$ = $2; }
e04a16fb
AG
1123| THROWS_TK error
1124 {yyerror ("Missing class type term"); RECOVER;}
1125;
1126
1127class_type_list:
1128 class_type
c877974e 1129 { $$ = build_tree_list ($1, $1); }
e04a16fb 1130| class_type_list C_TK class_type
c877974e 1131 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1132| class_type_list C_TK error
1133 {yyerror ("Missing class type term"); RECOVER;}
1134;
1135
1136method_body:
1137 block
5f1c312a 1138| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1139;
1140
1141/* 19.8.4 Productions from 8.5: Static Initializers */
1142static_initializer:
1143 static block
1144 {
c2952b01
APB
1145 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1146 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1147 current_static_block = NULL_TREE;
e04a16fb 1148 }
e04a16fb
AG
1149;
1150
1151static: /* Test lval.sub_token here */
c2952b01 1152 modifiers
e04a16fb 1153 {
c2952b01
APB
1154 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1155 /* Can't have a static initializer in an innerclass */
1156 if ($1 | ACC_STATIC &&
1157 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1158 parse_error_context
1159 (MODIFIER_WFL (STATIC_TK),
1160 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1161 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1162 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1163 }
1164;
1165
1166/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1167constructor_declaration:
22eed1e6 1168 constructor_header
e04a16fb 1169 {
22eed1e6
APB
1170 current_function_decl = $1;
1171 source_start_java_method (current_function_decl);
e04a16fb 1172 }
22eed1e6 1173 constructor_body
b635eb2f 1174 { finish_method_declaration ($3); }
22eed1e6
APB
1175;
1176
1177constructor_header:
1178 constructor_declarator throws
1179 { $$ = method_header (0, NULL_TREE, $1, $2); }
1180| modifiers constructor_declarator throws
1181 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1182;
1183
1184constructor_declarator:
1185 simple_name OP_TK CP_TK
c2952b01
APB
1186 {
1187 ctxp->formal_parameter_number = 0;
1188 $$ = method_declarator ($1, NULL_TREE);
1189 }
e04a16fb 1190| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1191 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1192;
1193
1194constructor_body:
22eed1e6
APB
1195 /* Unlike regular method, we always need a complete (empty)
1196 body so we can safely perform all the required code
1197 addition (super invocation and field initialization) */
2e5eb5c5 1198 block_begin constructor_block_end
22eed1e6 1199 {
9bbc7d9f 1200 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1201 $$ = $2;
1202 }
2e5eb5c5 1203| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1204 { $$ = $3; }
2e5eb5c5 1205| block_begin block_statements constructor_block_end
22eed1e6 1206 { $$ = $3; }
2e5eb5c5 1207| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1208 { $$ = $4; }
e04a16fb
AG
1209;
1210
2e5eb5c5
APB
1211constructor_block_end:
1212 block_end
5f1c312a 1213;
2e5eb5c5 1214
e04a16fb
AG
1215/* Error recovery for that rule moved down expression_statement: rule. */
1216explicit_constructor_invocation:
1217 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1218 {
1219 $$ = build_method_invocation ($1, NULL_TREE);
1220 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1221 $$ = java_method_add_stmt (current_function_decl, $$);
1222 }
e04a16fb 1223| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1224 {
1225 $$ = build_method_invocation ($1, $3);
1226 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1227 $$ = java_method_add_stmt (current_function_decl, $$);
1228 }
e04a16fb
AG
1229 /* Added, JDK1.1 inner classes. Modified because the rule
1230 'primary' couldn't work. */
1231| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1232 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1233| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1234 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1235;
1236
1237this_or_super: /* Added, simplifies error diagnostics */
1238 THIS_TK
1239 {
9ee9b555 1240 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1241 EXPR_WFL_LINECOL (wfl) = $1.location;
1242 $$ = wfl;
1243 }
1244| SUPER_TK
1245 {
9ee9b555 1246 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1247 EXPR_WFL_LINECOL (wfl) = $1.location;
1248 $$ = wfl;
1249 }
1250;
1251
1252/* 19.9 Productions from 9: Interfaces */
1253/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1254interface_declaration:
1255 INTERFACE_TK identifier
1256 { create_interface (0, $2, NULL_TREE); }
1257 interface_body
e04a16fb
AG
1258| modifiers INTERFACE_TK identifier
1259 { create_interface ($1, $3, NULL_TREE); }
1260 interface_body
e04a16fb
AG
1261| INTERFACE_TK identifier extends_interfaces
1262 { create_interface (0, $2, $3); }
1263 interface_body
e04a16fb
AG
1264| modifiers INTERFACE_TK identifier extends_interfaces
1265 { create_interface ($1, $3, $4); }
1266 interface_body
e04a16fb 1267| INTERFACE_TK identifier error
0b4d333e 1268 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1269| modifiers INTERFACE_TK identifier error
0b4d333e 1270 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1271;
1272
1273extends_interfaces:
1274 EXTENDS_TK interface_type
1275 {
1276 ctxp->interface_number = 1;
1277 $$ = build_tree_list ($2, NULL_TREE);
1278 }
1279| extends_interfaces C_TK interface_type
1280 {
1281 ctxp->interface_number++;
1282 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1283 }
1284| EXTENDS_TK error
1285 {yyerror ("Invalid interface type"); RECOVER;}
1286| extends_interfaces C_TK error
1287 {yyerror ("Missing term"); RECOVER;}
1288;
1289
1290interface_body:
1291 OCB_TK CCB_TK
1292 { $$ = NULL_TREE; }
1293| OCB_TK interface_member_declarations CCB_TK
1294 { $$ = NULL_TREE; }
1295;
1296
1297interface_member_declarations:
1298 interface_member_declaration
1299| interface_member_declarations interface_member_declaration
1300;
1301
1302interface_member_declaration:
1303 constant_declaration
1304| abstract_method_declaration
1305| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1306 { end_class_declaration (1); }
1307| interface_declaration /* Added, JDK1.1 inner interfaces */
1308 { end_class_declaration (1); }
e04a16fb
AG
1309;
1310
1311constant_declaration:
1312 field_declaration
1313;
1314
1315abstract_method_declaration:
1316 method_header SC_TK
1317 {
1318 check_abstract_method_header ($1);
1319 current_function_decl = NULL_TREE; /* FIXME ? */
1320 }
1321| method_header error
1322 {yyerror ("';' expected"); RECOVER;}
1323;
1324
1325/* 19.10 Productions from 10: Arrays */
1326array_initializer:
1327 OCB_TK CCB_TK
1179ebc2 1328 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1329| OCB_TK variable_initializers CCB_TK
f8976021 1330 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1331| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1332 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1333;
1334
1335variable_initializers:
1336 variable_initializer
f8976021
APB
1337 {
1338 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1339 $1, NULL_TREE);
1340 }
e04a16fb 1341| variable_initializers C_TK variable_initializer
1179ebc2
APB
1342 {
1343 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1344 }
e04a16fb
AG
1345| variable_initializers C_TK error
1346 {yyerror ("Missing term"); RECOVER;}
1347;
1348
1349/* 19.11 Production from 14: Blocks and Statements */
1350block:
1351 OCB_TK CCB_TK
7f10c2e2
APB
1352 {
1353 /* Store the location of the `}' when doing xrefs */
1354 if (current_function_decl && flag_emit_xref)
1355 DECL_END_SOURCE_LINE (current_function_decl) =
1356 EXPR_WFL_ADD_COL ($2.location, 1);
1357 $$ = empty_stmt_node;
1358 }
22eed1e6
APB
1359| block_begin block_statements block_end
1360 { $$ = $3; }
1361;
1362
1363block_begin:
1364 OCB_TK
e04a16fb 1365 { enter_block (); }
22eed1e6
APB
1366;
1367
1368block_end:
e04a16fb
AG
1369 CCB_TK
1370 {
1371 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1372 /* Store the location of the `}' when doing xrefs */
1373 if (current_function_decl && flag_emit_xref)
1374 DECL_END_SOURCE_LINE (current_function_decl) =
1375 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1376 $$ = exit_block ();
c280e37a
APB
1377 if (!BLOCK_SUBBLOCKS ($$))
1378 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1379 }
1380;
1381
1382block_statements:
1383 block_statement
1384| block_statements block_statement
1385;
1386
1387block_statement:
1388 local_variable_declaration_statement
1389| statement
15fdcfe9 1390 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1391| class_declaration /* Added, JDK1.1 local classes */
1392 {
1393 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1394 end_class_declaration (1);
1395 }
e04a16fb
AG
1396;
1397
1398local_variable_declaration_statement:
1399 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1400;
1401
1402local_variable_declaration:
1403 type variable_declarators
1404 { declare_local_variables (0, $1, $2); }
a003f638 1405| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1406 { declare_local_variables ($1, $2, $3); }
1407;
1408
1409statement:
1410 statement_without_trailing_substatement
1411| labeled_statement
e04a16fb 1412| if_then_statement
e04a16fb 1413| if_then_else_statement
e04a16fb 1414| while_statement
e04a16fb 1415| for_statement
cd9643f7 1416 { $$ = exit_block (); }
e04a16fb
AG
1417;
1418
1419statement_nsi:
1420 statement_without_trailing_substatement
1421| labeled_statement_nsi
e04a16fb 1422| if_then_else_statement_nsi
e04a16fb 1423| while_statement_nsi
e04a16fb 1424| for_statement_nsi
9dd939b2 1425 { $$ = exit_block (); }
e04a16fb
AG
1426;
1427
1428statement_without_trailing_substatement:
1429 block
e04a16fb 1430| empty_statement
e04a16fb 1431| expression_statement
e04a16fb 1432| switch_statement
e04a16fb 1433| do_statement
e04a16fb 1434| break_statement
e04a16fb 1435| continue_statement
e04a16fb
AG
1436| return_statement
1437| synchronized_statement
e04a16fb 1438| throw_statement
e04a16fb 1439| try_statement
e04a16fb
AG
1440;
1441
1442empty_statement:
1443 SC_TK
5f1c312a
APB
1444 {
1445 if (flag_extraneous_semicolon)
1446 {
1447 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1448 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1449 }
1450 $$ = empty_stmt_node;
1451 }
e04a16fb
AG
1452;
1453
1454label_decl:
1455 identifier REL_CL_TK
1456 {
1457 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1458 EXPR_WFL_NODE ($1));
e04a16fb
AG
1459 pushlevel (2);
1460 push_labeled_block ($$);
1461 PUSH_LABELED_BLOCK ($$);
1462 }
1463;
1464
1465labeled_statement:
1466 label_decl statement
b635eb2f 1467 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1468| identifier error
1469 {yyerror ("':' expected"); RECOVER;}
1470;
1471
1472labeled_statement_nsi:
1473 label_decl statement_nsi
b635eb2f 1474 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1475;
1476
1477/* We concentrate here a bunch of error handling rules that we couldn't write
1478 earlier, because expression_statement catches a missing ';'. */
1479expression_statement:
1480 statement_expression SC_TK
1481 {
1482 /* We have a statement. Generate a WFL around it so
1483 we can debug it */
1484 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1485 /* We know we have a statement, so set the debug
1486 info to be eventually generate here. */
1487 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1488 }
1489| error SC_TK
1490 {
29f8b718 1491 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1492 DRECOVER (expr_stmt);
1493 }
1494| error OCB_TK
1495 {
29f8b718 1496 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1497 DRECOVER (expr_stmt);
1498 }
1499| error CCB_TK
1500 {
29f8b718 1501 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1502 DRECOVER (expr_stmt);
1503 }
1504| this_or_super OP_TK error
1505 {yyerror ("')' expected"); RECOVER;}
1506| this_or_super OP_TK CP_TK error
22eed1e6 1507 {
8119c720 1508 parse_ctor_invocation_error ();
22eed1e6
APB
1509 RECOVER;
1510 }
e04a16fb
AG
1511| this_or_super OP_TK argument_list error
1512 {yyerror ("')' expected"); RECOVER;}
1513| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1514 {
8119c720 1515 parse_ctor_invocation_error ();
22eed1e6
APB
1516 RECOVER;
1517 }
e04a16fb
AG
1518| name DOT_TK SUPER_TK error
1519 {yyerror ("'(' expected"); RECOVER;}
1520| name DOT_TK SUPER_TK OP_TK error
1521 {yyerror ("')' expected"); RECOVER;}
1522| name DOT_TK SUPER_TK OP_TK argument_list error
1523 {yyerror ("')' expected"); RECOVER;}
1524| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1525 {yyerror ("';' expected"); RECOVER;}
1526| name DOT_TK SUPER_TK OP_TK CP_TK error
1527 {yyerror ("';' expected"); RECOVER;}
1528;
1529
1530statement_expression:
1531 assignment
1532| pre_increment_expression
e04a16fb 1533| pre_decrement_expression
e04a16fb 1534| post_increment_expression
e04a16fb 1535| post_decrement_expression
e04a16fb
AG
1536| method_invocation
1537| class_instance_creation_expression
e04a16fb
AG
1538;
1539
1540if_then_statement:
1541 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1542 {
1543 $$ = build_if_else_statement ($2.location, $3,
1544 $5, NULL_TREE);
1545 }
e04a16fb
AG
1546| IF_TK error
1547 {yyerror ("'(' expected"); RECOVER;}
1548| IF_TK OP_TK error
1549 {yyerror ("Missing term"); RECOVER;}
1550| IF_TK OP_TK expression error
1551 {yyerror ("')' expected"); RECOVER;}
1552;
1553
1554if_then_else_statement:
1555 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1556 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1557;
1558
1559if_then_else_statement_nsi:
1560 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1561 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1562;
1563
1564switch_statement:
15fdcfe9
PB
1565 switch_expression
1566 {
1567 enter_block ();
1568 }
1569 switch_block
b67d701b 1570 {
15fdcfe9 1571 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1572 I.e. make the last statment also have its own
1573 COMPOUND_EXPR. */
15fdcfe9
PB
1574 maybe_absorb_scoping_blocks ();
1575 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1576 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1577 }
1578;
1579
1580switch_expression:
1581 SWITCH_TK OP_TK expression CP_TK
1582 {
1583 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1584 EXPR_WFL_LINECOL ($$) = $2.location;
1585 }
e04a16fb
AG
1586| SWITCH_TK error
1587 {yyerror ("'(' expected"); RECOVER;}
1588| SWITCH_TK OP_TK error
1589 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1590| SWITCH_TK OP_TK expression CP_TK error
1591 {yyerror ("'{' expected"); RECOVER;}
1592;
1593
f8976021
APB
1594/* Default assignment is there to avoid type node on switch_block
1595 node. */
1596
e04a16fb
AG
1597switch_block:
1598 OCB_TK CCB_TK
f8976021 1599 { $$ = NULL_TREE; }
e04a16fb 1600| OCB_TK switch_labels CCB_TK
f8976021 1601 { $$ = NULL_TREE; }
e04a16fb 1602| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1603 { $$ = NULL_TREE; }
e04a16fb 1604| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1605 { $$ = NULL_TREE; }
e04a16fb
AG
1606;
1607
1608switch_block_statement_groups:
1609 switch_block_statement_group
1610| switch_block_statement_groups switch_block_statement_group
1611;
1612
1613switch_block_statement_group:
15fdcfe9 1614 switch_labels block_statements
e04a16fb
AG
1615;
1616
e04a16fb
AG
1617switch_labels:
1618 switch_label
1619| switch_labels switch_label
1620;
1621
1622switch_label:
1623 CASE_TK constant_expression REL_CL_TK
b67d701b 1624 {
15fdcfe9
PB
1625 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1626 EXPR_WFL_LINECOL (lab) = $1.location;
1627 java_method_add_stmt (current_function_decl, lab);
b67d701b 1628 }
e04a16fb 1629| DEFAULT_TK REL_CL_TK
b67d701b 1630 {
ac39dac0 1631 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1632 EXPR_WFL_LINECOL (lab) = $1.location;
1633 java_method_add_stmt (current_function_decl, lab);
b67d701b 1634 }
e04a16fb
AG
1635| CASE_TK error
1636 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1637| CASE_TK constant_expression error
1638 {yyerror ("':' expected"); RECOVER;}
1639| DEFAULT_TK error
1640 {yyerror ("':' expected"); RECOVER;}
1641;
1642
1643while_expression:
1644 WHILE_TK OP_TK expression CP_TK
1645 {
1646 tree body = build_loop_body ($2.location, $3, 0);
1647 $$ = build_new_loop (body);
1648 }
1649;
1650
1651while_statement:
1652 while_expression statement
b635eb2f 1653 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1654| WHILE_TK error
1655 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1656| WHILE_TK OP_TK error
1657 {yyerror ("Missing term and ')' expected"); RECOVER;}
1658| WHILE_TK OP_TK expression error
1659 {yyerror ("')' expected"); RECOVER;}
1660;
1661
1662while_statement_nsi:
1663 while_expression statement_nsi
b635eb2f 1664 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1665;
1666
1667do_statement_begin:
1668 DO_TK
1669 {
1670 tree body = build_loop_body (0, NULL_TREE, 1);
1671 $$ = build_new_loop (body);
1672 }
1673 /* Need error handing here. FIXME */
1674;
1675
1676do_statement:
1677 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1678 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1679;
1680
1681for_statement:
1682 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1683 {
1684 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1685 $3 = build_wfl_node ($3);
1686 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1687 }
e04a16fb
AG
1688| for_begin SC_TK SC_TK for_update CP_TK statement
1689 {
b635eb2f 1690 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1691 /* We have not condition, so we get rid of the EXIT_EXPR */
1692 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1693 empty_stmt_node;
e04a16fb
AG
1694 }
1695| for_begin SC_TK error
1696 {yyerror ("Invalid control expression"); RECOVER;}
1697| for_begin SC_TK expression SC_TK error
1698 {yyerror ("Invalid update expression"); RECOVER;}
1699| for_begin SC_TK SC_TK error
1700 {yyerror ("Invalid update expression"); RECOVER;}
1701;
1702
1703for_statement_nsi:
1704 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1705 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1706| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1707 {
b635eb2f 1708 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1709 /* We have not condition, so we get rid of the EXIT_EXPR */
1710 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1711 empty_stmt_node;
e04a16fb
AG
1712 }
1713;
1714
1715for_header:
1716 FOR_TK OP_TK
1717 {
1718 /* This scope defined for local variable that may be
1719 defined within the scope of the for loop */
1720 enter_block ();
1721 }
1722| FOR_TK error
1723 {yyerror ("'(' expected"); DRECOVER(for_1);}
1724| FOR_TK OP_TK error
1725 {yyerror ("Invalid init statement"); RECOVER;}
1726;
1727
1728for_begin:
1729 for_header for_init
1730 {
1731 /* We now declare the loop body. The loop is
1732 declared as a for loop. */
1733 tree body = build_loop_body (0, NULL_TREE, 0);
1734 $$ = build_new_loop (body);
c2952b01 1735 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1736 /* The loop is added to the current block the for
1737 statement is defined within */
1738 java_method_add_stmt (current_function_decl, $$);
1739 }
1740;
1741for_init: /* Can be empty */
9bbc7d9f 1742 { $$ = empty_stmt_node; }
e04a16fb
AG
1743| statement_expression_list
1744 {
1745 /* Init statement recorded within the previously
1746 defined block scope */
1747 $$ = java_method_add_stmt (current_function_decl, $1);
1748 }
1749| local_variable_declaration
1750 {
1751 /* Local variable are recorded within the previously
1752 defined block scope */
1753 $$ = NULL_TREE;
1754 }
1755| statement_expression_list error
1756 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1757;
1758
1759for_update: /* Can be empty */
9bbc7d9f 1760 {$$ = empty_stmt_node;}
e04a16fb
AG
1761| statement_expression_list
1762 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1763;
1764
1765statement_expression_list:
1766 statement_expression
1767 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1768| statement_expression_list C_TK statement_expression
1769 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1770| statement_expression_list C_TK error
1771 {yyerror ("Missing term"); RECOVER;}
1772;
1773
1774break_statement:
1775 BREAK_TK SC_TK
1776 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1777| BREAK_TK identifier SC_TK
1778 { $$ = build_bc_statement ($1.location, 1, $2); }
1779| BREAK_TK error
1780 {yyerror ("Missing term"); RECOVER;}
1781| BREAK_TK identifier error
1782 {yyerror ("';' expected"); RECOVER;}
1783;
1784
1785continue_statement:
1786 CONTINUE_TK SC_TK
1787 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1788| CONTINUE_TK identifier SC_TK
1789 { $$ = build_bc_statement ($1.location, 0, $2); }
1790| CONTINUE_TK error
1791 {yyerror ("Missing term"); RECOVER;}
1792| CONTINUE_TK identifier error
1793 {yyerror ("';' expected"); RECOVER;}
1794;
1795
1796return_statement:
1797 RETURN_TK SC_TK
1798 { $$ = build_return ($1.location, NULL_TREE); }
1799| RETURN_TK expression SC_TK
1800 { $$ = build_return ($1.location, $2); }
1801| RETURN_TK error
1802 {yyerror ("Missing term"); RECOVER;}
1803| RETURN_TK expression error
1804 {yyerror ("';' expected"); RECOVER;}
1805;
1806
1807throw_statement:
1808 THROW_TK expression SC_TK
b9f7e36c
APB
1809 {
1810 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1811 EXPR_WFL_LINECOL ($$) = $1.location;
1812 }
e04a16fb
AG
1813| THROW_TK error
1814 {yyerror ("Missing term"); RECOVER;}
1815| THROW_TK expression error
1816 {yyerror ("';' expected"); RECOVER;}
1817;
1818
1819synchronized_statement:
1820 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1821 {
1822 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1823 EXPR_WFL_LINECOL ($$) =
1824 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1825 }
e04a16fb
AG
1826| synchronized OP_TK expression CP_TK error
1827 {yyerror ("'{' expected"); RECOVER;}
1828| synchronized error
1829 {yyerror ("'(' expected"); RECOVER;}
1830| synchronized OP_TK error CP_TK
1831 {yyerror ("Missing term"); RECOVER;}
1832| synchronized OP_TK error
1833 {yyerror ("Missing term"); RECOVER;}
1834;
1835
b9f7e36c 1836synchronized:
efa0a23f 1837 modifiers
e04a16fb 1838 {
781b0558
KG
1839 check_modifiers (
1840 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1841 $1, ACC_SYNCHRONIZED);
1842 if ($1 != ACC_SYNCHRONIZED)
1843 MODIFIER_WFL (SYNCHRONIZED_TK) =
1844 build_wfl_node (NULL_TREE);
e04a16fb
AG
1845 }
1846;
1847
1848try_statement:
1849 TRY_TK block catches
a7d8d81f 1850 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1851| TRY_TK block finally
a7d8d81f 1852 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1853| TRY_TK block catches finally
2aa11e97
APB
1854 { $$ = build_try_finally_statement
1855 ($1.location, build_try_statement ($1.location,
1856 $2, $3), $4);
1857 }
e04a16fb
AG
1858| TRY_TK error
1859 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1860;
1861
1862catches:
1863 catch_clause
1864| catches catch_clause
b67d701b
PB
1865 {
1866 TREE_CHAIN ($2) = $1;
1867 $$ = $2;
1868 }
e04a16fb
AG
1869;
1870
1871catch_clause:
b67d701b
PB
1872 catch_clause_parameter block
1873 {
1874 java_method_add_stmt (current_function_decl, $2);
1875 exit_block ();
1876 $$ = $1;
1877 }
1878
1879catch_clause_parameter:
1880 CATCH_TK OP_TK formal_parameter CP_TK
1881 {
1882 /* We add a block to define a scope for
1883 formal_parameter (CCBP). The formal parameter is
1884 declared initialized by the appropriate function
1885 call */
1886 tree ccpb = enter_block ();
1887 tree init = build_assignment (ASSIGN_TK, $2.location,
1888 TREE_PURPOSE ($3),
1889 soft_exceptioninfo_call_node);
1890 declare_local_variables (0, TREE_VALUE ($3),
1891 build_tree_list (TREE_PURPOSE ($3),
1892 init));
1893 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1894 EXPR_WFL_LINECOL ($$) = $1.location;
1895 }
e04a16fb 1896| CATCH_TK error
97f30284 1897 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1898| CATCH_TK OP_TK error
97f30284
APB
1899 {
1900 yyerror ("Missing term or ')' expected");
1901 RECOVER; $$ = NULL_TREE;
1902 }
b67d701b 1903| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1904 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1905;
1906
1907finally:
1908 FINALLY_TK block
a7d8d81f 1909 { $$ = $2; }
e04a16fb
AG
1910| FINALLY_TK error
1911 {yyerror ("'{' expected"); RECOVER; }
1912;
1913
1914/* 19.12 Production from 15: Expressions */
1915primary:
1916 primary_no_new_array
1917| array_creation_expression
1918;
1919
1920primary_no_new_array:
1921 literal
1922| THIS_TK
1923 { $$ = build_this ($1.location); }
1924| OP_TK expression CP_TK
1925 {$$ = $2;}
1926| class_instance_creation_expression
1927| field_access
1928| method_invocation
1929| array_access
c2952b01 1930| type_literals
e04a16fb
AG
1931 /* Added, JDK1.1 inner classes. Documentation is wrong
1932 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1933 exist. Used name: instead. */
e04a16fb 1934| name DOT_TK THIS_TK
c2952b01
APB
1935 {
1936 tree wfl = build_wfl_node (this_identifier_node);
1937 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1938 }
e04a16fb
AG
1939| OP_TK expression error
1940 {yyerror ("')' expected"); RECOVER;}
1941| name DOT_TK error
1942 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1943| primitive_type DOT_TK error
1944 {yyerror ("'class' expected" ); RECOVER;}
1945| VOID_TK DOT_TK error
1946 {yyerror ("'class' expected" ); RECOVER;}
1947;
1948
c2952b01
APB
1949type_literals:
1950 name DOT_TK CLASS_TK
1951 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1952| array_type DOT_TK CLASS_TK
c2952b01
APB
1953 { $$ = build_incomplete_class_ref ($2.location, $1); }
1954| primitive_type DOT_TK CLASS_TK
1955 { $$ = build_class_ref ($1); }
1956| VOID_TK DOT_TK CLASS_TK
1957 { $$ = build_class_ref (void_type_node); }
1958;
1959
e04a16fb
AG
1960class_instance_creation_expression:
1961 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1962 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1963| NEW_TK class_type OP_TK CP_TK
b67d701b 1964 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1965| anonymous_class_creation
e04a16fb
AG
1966 /* Added, JDK1.1 inner classes, modified to use name or
1967 primary instead of primary solely which couldn't work in
1968 all situations. */
1969| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1970 {
1971 tree ctor = build_new_invocation ($2, NULL_TREE);
1972 $$ = make_qualified_primary ($1, ctor,
1973 EXPR_WFL_LINECOL ($1));
1974 }
e04a16fb
AG
1975| something_dot_new identifier OP_TK CP_TK class_body
1976| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1977 {
1978 tree ctor = build_new_invocation ($2, $4);
1979 $$ = make_qualified_primary ($1, ctor,
1980 EXPR_WFL_LINECOL ($1));
1981 }
e04a16fb
AG
1982| something_dot_new identifier OP_TK argument_list CP_TK class_body
1983| NEW_TK error SC_TK
1984 {yyerror ("'(' expected"); DRECOVER(new_1);}
1985| NEW_TK class_type error
1986 {yyerror ("'(' expected"); RECOVER;}
1987| NEW_TK class_type OP_TK error
1988 {yyerror ("')' or term expected"); RECOVER;}
1989| NEW_TK class_type OP_TK argument_list error
1990 {yyerror ("')' expected"); RECOVER;}
1991| something_dot_new error
1992 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1993| something_dot_new identifier error
1994 {yyerror ("'(' expected"); RECOVER;}
1995;
1996
c2952b01
APB
1997/* Created after JDK1.1 rules originally added to
1998 class_instance_creation_expression, but modified to use
1999 'class_type' instead of 'TypeName' (type_name) which is mentionned
2000 in the documentation but doesn't exist. */
2001
2002anonymous_class_creation:
2003 NEW_TK class_type OP_TK argument_list CP_TK
2004 { create_anonymous_class ($1.location, $2); }
2005 class_body
2006 {
2007 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2008 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2009
2010 end_class_declaration (1);
2011
2012 /* Now we can craft the new expression */
2013 $$ = build_new_invocation (id, $4);
2014
2015 /* Note that we can't possibly be here if
2016 `class_type' is an interface (in which case the
2017 anonymous class extends Object and implements
2018 `class_type', hence its constructor can't have
2019 arguments.) */
2020
2021 /* Otherwise, the innerclass must feature a
2022 constructor matching `argument_list'. Anonymous
2023 classes are a bit special: it's impossible to
2024 define constructor for them, hence constructors
2025 must be generated following the hints provided by
2026 the `new' expression. Whether a super constructor
2027 of that nature exists or not is to be verified
2028 later on in verify_constructor_super.
2029
2030 It's during the expansion of a `new' statement
2031 refering to an anonymous class that a ctor will
2032 be generated for the anonymous class, with the
2033 right arguments. */
2034
2035 }
2036| NEW_TK class_type OP_TK CP_TK
2037 { create_anonymous_class ($1.location, $2); }
2038 class_body
2039 {
2040 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2041 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2042
2043 end_class_declaration (1);
2044
2045 /* Now we can craft the new expression. The
2046 statement doesn't need to be remember so that a
2047 constructor can be generated, since its signature
2048 is already known. */
2049 $$ = build_new_invocation (id, NULL_TREE);
2050 }
2051;
2052
e04a16fb
AG
2053something_dot_new: /* Added, not part of the specs. */
2054 name DOT_TK NEW_TK
c2952b01 2055 { $$ = $1; }
e04a16fb 2056| primary DOT_TK NEW_TK
c2952b01 2057 { $$ = $1; }
e04a16fb
AG
2058;
2059
2060argument_list:
2061 expression
2062 {
2063 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2064 ctxp->formal_parameter_number = 1;
2065 }
2066| argument_list C_TK expression
2067 {
2068 ctxp->formal_parameter_number += 1;
2069 $$ = tree_cons (NULL_TREE, $3, $1);
2070 }
2071| argument_list C_TK error
2072 {yyerror ("Missing term"); RECOVER;}
2073;
2074
2075array_creation_expression:
2076 NEW_TK primitive_type dim_exprs
2077 { $$ = build_newarray_node ($2, $3, 0); }
2078| NEW_TK class_or_interface_type dim_exprs
2079 { $$ = build_newarray_node ($2, $3, 0); }
2080| NEW_TK primitive_type dim_exprs dims
c7303e41 2081 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2082| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2083 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2084 /* Added, JDK1.1 anonymous array. Initial documentation rule
2085 modified */
2086| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2087 {
2088 char *sig;
c7303e41
APB
2089 int osb = pop_current_osb (ctxp);
2090 while (osb--)
c2952b01
APB
2091 obstack_1grow (&temporary_obstack, '[');
2092 sig = obstack_finish (&temporary_obstack);
2093 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2094 $2, get_identifier (sig), $4);
2095 }
e04a16fb 2096| NEW_TK primitive_type dims array_initializer
c2952b01 2097 {
c7303e41 2098 int osb = pop_current_osb (ctxp);
c2952b01 2099 tree type = $2;
c7303e41 2100 while (osb--)
c2952b01
APB
2101 type = build_java_array_type (type, -1);
2102 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2103 build_pointer_type (type), NULL_TREE, $4);
2104 }
e04a16fb
AG
2105| NEW_TK error CSB_TK
2106 {yyerror ("'[' expected"); DRECOVER ("]");}
2107| NEW_TK error OSB_TK
2108 {yyerror ("']' expected"); RECOVER;}
2109;
2110
2111dim_exprs:
2112 dim_expr
2113 { $$ = build_tree_list (NULL_TREE, $1); }
2114| dim_exprs dim_expr
2115 { $$ = tree_cons (NULL_TREE, $2, $$); }
2116;
2117
2118dim_expr:
2119 OSB_TK expression CSB_TK
2120 {
9a7ab4b3
APB
2121 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2122 {
2123 $2 = build_wfl_node ($2);
2124 TREE_TYPE ($2) = NULL_TREE;
2125 }
e04a16fb
AG
2126 EXPR_WFL_LINECOL ($2) = $1.location;
2127 $$ = $2;
2128 }
2129| OSB_TK expression error
2130 {yyerror ("']' expected"); RECOVER;}
2131| OSB_TK error
2132 {
2133 yyerror ("Missing term");
2134 yyerror ("']' expected");
2135 RECOVER;
2136 }
2137;
2138
2139dims:
2140 OSB_TK CSB_TK
ba179f9f
APB
2141 {
2142 int allocate = 0;
2143 /* If not initialized, allocate memory for the osb
2144 numbers stack */
2145 if (!ctxp->osb_limit)
2146 {
2147 allocate = ctxp->osb_limit = 32;
2148 ctxp->osb_depth = -1;
2149 }
c2952b01 2150 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2151 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2152 allocate = ctxp->osb_limit << 1;
2153
2154 if (allocate)
2155 {
2156 allocate *= sizeof (int);
2157 if (ctxp->osb_number)
2158 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2159 allocate);
2160 else
2161 ctxp->osb_number = (int *)xmalloc (allocate);
2162 }
2163 ctxp->osb_depth++;
2164 CURRENT_OSB (ctxp) = 1;
2165 }
e04a16fb 2166| dims OSB_TK CSB_TK
ba179f9f 2167 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2168| dims OSB_TK error
2169 { yyerror ("']' expected"); RECOVER;}
2170;
2171
2172field_access:
2173 primary DOT_TK identifier
2174 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2175 /* FIXME - REWRITE TO:
2176 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2177| SUPER_TK DOT_TK identifier
2178 {
6e22695a 2179 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2180 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2181 $$ = make_qualified_name (super_wfl, $3, $2.location);
2182 }
2183| SUPER_TK error
2184 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2185;
2186
2187method_invocation:
2188 name OP_TK CP_TK
2189 { $$ = build_method_invocation ($1, NULL_TREE); }
2190| name OP_TK argument_list CP_TK
2191 { $$ = build_method_invocation ($1, $3); }
2192| primary DOT_TK identifier OP_TK CP_TK
2193 {
22eed1e6
APB
2194 if (TREE_CODE ($1) == THIS_EXPR)
2195 $$ = build_this_super_qualified_invocation
2196 (1, $3, NULL_TREE, 0, $2.location);
2197 else
2198 {
2199 tree invok = build_method_invocation ($3, NULL_TREE);
2200 $$ = make_qualified_primary ($1, invok, $2.location);
2201 }
e04a16fb
AG
2202 }
2203| primary DOT_TK identifier OP_TK argument_list CP_TK
2204 {
22eed1e6
APB
2205 if (TREE_CODE ($1) == THIS_EXPR)
2206 $$ = build_this_super_qualified_invocation
2207 (1, $3, $5, 0, $2.location);
2208 else
2209 {
2210 tree invok = build_method_invocation ($3, $5);
2211 $$ = make_qualified_primary ($1, invok, $2.location);
2212 }
e04a16fb
AG
2213 }
2214| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2215 {
2216 $$ = build_this_super_qualified_invocation
2217 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2218 }
2219| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2220 {
22eed1e6
APB
2221 $$ = build_this_super_qualified_invocation
2222 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2223 }
2224 /* Screws up thing. I let it here until I'm convinced it can
2225 be removed. FIXME
2226| primary DOT_TK error
2227 {yyerror ("'(' expected"); DRECOVER(bad);} */
2228| SUPER_TK DOT_TK error CP_TK
2229 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2230| SUPER_TK DOT_TK error DOT_TK
2231 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232;
2233
2234array_access:
2235 name OSB_TK expression CSB_TK
2236 { $$ = build_array_ref ($2.location, $1, $3); }
2237| primary_no_new_array OSB_TK expression CSB_TK
2238 { $$ = build_array_ref ($2.location, $1, $3); }
2239| name OSB_TK error
2240 {
2241 yyerror ("Missing term and ']' expected");
2242 DRECOVER(array_access);
2243 }
2244| name OSB_TK expression error
2245 {
2246 yyerror ("']' expected");
2247 DRECOVER(array_access);
2248 }
2249| primary_no_new_array OSB_TK error
2250 {
2251 yyerror ("Missing term and ']' expected");
2252 DRECOVER(array_access);
2253 }
2254| primary_no_new_array OSB_TK expression error
2255 {
2256 yyerror ("']' expected");
2257 DRECOVER(array_access);
2258 }
2259;
2260
2261postfix_expression:
2262 primary
2263| name
2264| post_increment_expression
2265| post_decrement_expression
2266;
2267
2268post_increment_expression:
2269 postfix_expression INCR_TK
2270 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2271;
2272
2273post_decrement_expression:
2274 postfix_expression DECR_TK
2275 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2276;
2277
2278unary_expression:
2279 pre_increment_expression
2280| pre_decrement_expression
2281| PLUS_TK unary_expression
2282 {$$ = build_unaryop ($1.token, $1.location, $2); }
2283| MINUS_TK unary_expression
2284 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285| unary_expression_not_plus_minus
2286| PLUS_TK error
2287 {yyerror ("Missing term"); RECOVER}
2288| MINUS_TK error
2289 {yyerror ("Missing term"); RECOVER}
2290;
2291
2292pre_increment_expression:
2293 INCR_TK unary_expression
2294 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2295| INCR_TK error
2296 {yyerror ("Missing term"); RECOVER}
2297;
2298
2299pre_decrement_expression:
2300 DECR_TK unary_expression
2301 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2302| DECR_TK error
2303 {yyerror ("Missing term"); RECOVER}
2304;
2305
2306unary_expression_not_plus_minus:
2307 postfix_expression
2308| NOT_TK unary_expression
2309 {$$ = build_unaryop ($1.token, $1.location, $2); }
2310| NEG_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312| cast_expression
2313| NOT_TK error
2314 {yyerror ("Missing term"); RECOVER}
2315| NEG_TK error
2316 {yyerror ("Missing term"); RECOVER}
2317;
2318
2319cast_expression: /* Error handling here is potentially weak */
2320 OP_TK primitive_type dims CP_TK unary_expression
2321 {
2322 tree type = $2;
c7303e41
APB
2323 int osb = pop_current_osb (ctxp);
2324 while (osb--)
e04a16fb
AG
2325 type = build_java_array_type (type, -1);
2326 $$ = build_cast ($1.location, type, $5);
2327 }
2328| OP_TK primitive_type CP_TK unary_expression
2329 { $$ = build_cast ($1.location, $2, $4); }
2330| OP_TK expression CP_TK unary_expression_not_plus_minus
2331 { $$ = build_cast ($1.location, $2, $4); }
2332| OP_TK name dims CP_TK unary_expression_not_plus_minus
2333 {
49f48c71 2334 const char *ptr;
c7303e41
APB
2335 int osb = pop_current_osb (ctxp);
2336 while (osb--)
e04a16fb
AG
2337 obstack_1grow (&temporary_obstack, '[');
2338 obstack_grow0 (&temporary_obstack,
2339 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2340 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2341 ptr = obstack_finish (&temporary_obstack);
2342 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2343 $$ = build_cast ($1.location, $2, $5);
2344 }
2345| OP_TK primitive_type OSB_TK error
2346 {yyerror ("']' expected, invalid type expression");}
2347| OP_TK error
2348 {
29f8b718 2349 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2350 RECOVER;
2351 }
2352| OP_TK primitive_type dims CP_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354| OP_TK primitive_type CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356| OP_TK name dims CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358;
2359
2360multiplicative_expression:
2361 unary_expression
2362| multiplicative_expression MULT_TK unary_expression
2363 {
2364 $$ = build_binop (BINOP_LOOKUP ($2.token),
2365 $2.location, $1, $3);
2366 }
2367| multiplicative_expression DIV_TK unary_expression
2368 {
2369 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2370 $1, $3);
2371 }
2372| multiplicative_expression REM_TK unary_expression
2373 {
2374 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2375 $1, $3);
2376 }
2377| multiplicative_expression MULT_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| multiplicative_expression DIV_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381| multiplicative_expression REM_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383;
2384
2385additive_expression:
2386 multiplicative_expression
2387| additive_expression PLUS_TK multiplicative_expression
2388 {
2389 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2390 $1, $3);
2391 }
2392| additive_expression MINUS_TK multiplicative_expression
2393 {
2394 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2395 $1, $3);
2396 }
2397| additive_expression PLUS_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399| additive_expression MINUS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401;
2402
2403shift_expression:
2404 additive_expression
2405| shift_expression LS_TK additive_expression
2406 {
2407 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2408 $1, $3);
2409 }
2410| shift_expression SRS_TK additive_expression
2411 {
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2414 }
2415| shift_expression ZRS_TK additive_expression
2416 {
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2419 }
2420| shift_expression LS_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422| shift_expression SRS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424| shift_expression ZRS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426;
2427
2428relational_expression:
2429 shift_expression
2430| relational_expression LT_TK shift_expression
2431 {
2432 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2433 $1, $3);
2434 }
2435| relational_expression GT_TK shift_expression
2436 {
2437 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2438 $1, $3);
2439 }
2440| relational_expression LTE_TK shift_expression
2441 {
2442 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2443 $1, $3);
2444 }
2445| relational_expression GTE_TK shift_expression
2446 {
2447 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2448 $1, $3);
2449 }
2450| relational_expression INSTANCEOF_TK reference_type
5e942c50 2451 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2452| relational_expression LT_TK error
2453 {yyerror ("Missing term"); RECOVER;}
2454| relational_expression GT_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456| relational_expression LTE_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression GTE_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460| relational_expression INSTANCEOF_TK error
2461 {yyerror ("Invalid reference type"); RECOVER;}
2462;
2463
2464equality_expression:
2465 relational_expression
2466| equality_expression EQ_TK relational_expression
2467 {
2468 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2469 $1, $3);
2470 }
2471| equality_expression NEQ_TK relational_expression
2472 {
2473 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2474 $1, $3);
2475 }
2476| equality_expression EQ_TK error
2477 {yyerror ("Missing term"); RECOVER;}
2478| equality_expression NEQ_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2480;
2481
2482and_expression:
2483 equality_expression
2484| and_expression AND_TK equality_expression
2485 {
2486 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 $1, $3);
2488 }
2489| and_expression AND_TK error
2490 {yyerror ("Missing term"); RECOVER;}
2491;
2492
2493exclusive_or_expression:
2494 and_expression
2495| exclusive_or_expression XOR_TK and_expression
2496 {
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2499 }
2500| exclusive_or_expression XOR_TK error
2501 {yyerror ("Missing term"); RECOVER;}
2502;
2503
2504inclusive_or_expression:
2505 exclusive_or_expression
2506| inclusive_or_expression OR_TK exclusive_or_expression
2507 {
2508 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2509 $1, $3);
2510 }
2511| inclusive_or_expression OR_TK error
2512 {yyerror ("Missing term"); RECOVER;}
2513;
2514
2515conditional_and_expression:
2516 inclusive_or_expression
2517| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2518 {
2519 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2520 $1, $3);
2521 }
2522| conditional_and_expression BOOL_AND_TK error
2523 {yyerror ("Missing term"); RECOVER;}
2524;
2525
2526conditional_or_expression:
2527 conditional_and_expression
2528| conditional_or_expression BOOL_OR_TK conditional_and_expression
2529 {
2530 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531 $1, $3);
2532 }
2533| conditional_or_expression BOOL_OR_TK error
2534 {yyerror ("Missing term"); RECOVER;}
2535;
2536
2537conditional_expression: /* Error handling here is weak */
2538 conditional_or_expression
2539| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2540 {
2541 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2542 EXPR_WFL_LINECOL ($$) = $2.location;
2543 }
e04a16fb
AG
2544| conditional_or_expression REL_QM_TK REL_CL_TK error
2545 {
2546 YYERROR_NOW;
2547 yyerror ("Missing term");
2548 DRECOVER (1);
2549 }
2550| conditional_or_expression REL_QM_TK error
2551 {yyerror ("Missing term"); DRECOVER (2);}
2552| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2553 {yyerror ("Missing term"); DRECOVER (3);}
2554;
2555
2556assignment_expression:
2557 conditional_expression
2558| assignment
2559;
2560
2561assignment:
2562 left_hand_side assignment_operator assignment_expression
2563 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2564| left_hand_side assignment_operator error
2565 {
29f8b718 2566 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2567 DRECOVER (assign);
2568 }
2569;
2570
2571left_hand_side:
2572 name
2573| field_access
2574| array_access
2575;
2576
2577assignment_operator:
2578 ASSIGN_ANY_TK
2579| ASSIGN_TK
2580;
2581
2582expression:
2583 assignment_expression
2584;
2585
2586constant_expression:
2587 expression
2588;
2589
2590%%
c7303e41
APB
2591
2592/* Helper function to retrieve an OSB count. Should be used when the
2593 `dims:' rule is being used. */
2594
2595static int
2596pop_current_osb (ctxp)
2597 struct parser_ctxt *ctxp;
2598{
2599 int to_return;
2600
2601 if (ctxp->osb_depth < 0)
2602 fatal ("osb stack underflow");
2603
2604 to_return = CURRENT_OSB (ctxp);
2605 ctxp->osb_depth--;
2606
2607 return to_return;
2608}
2609
e04a16fb
AG
2610\f
2611
c2952b01
APB
2612/* This section of the code deal with save/restoring parser contexts.
2613 Add mode documentation here. FIXME */
e04a16fb 2614
c2952b01
APB
2615/* Helper function. Create a new parser context. With
2616 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2617 context is copied, otherwise, the new context is zeroed. The newly
2618 created context becomes the current one. */
e04a16fb 2619
c2952b01
APB
2620static void
2621create_new_parser_context (copy_from_previous)
2622 int copy_from_previous;
e04a16fb 2623{
c2952b01 2624 struct parser_ctxt *new;
e04a16fb 2625
c2952b01
APB
2626 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2627 if (copy_from_previous)
2628 {
2629 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2630 new->saved_data_ctx = 1;
2631 }
2632 else
2e09e75a 2633 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2634
e04a16fb
AG
2635 new->next = ctxp;
2636 ctxp = new;
c2952b01
APB
2637}
2638
2639/* Create a new parser context and make it the current one. */
2640
2641void
2642java_push_parser_context ()
2643{
2644 create_new_parser_context (0);
e04a16fb 2645 if (ctxp->next)
5e942c50
APB
2646 {
2647 ctxp->incomplete_class = ctxp->next->incomplete_class;
2648 ctxp->gclass_list = ctxp->next->gclass_list;
2649 }
e04a16fb
AG
2650}
2651
c2952b01
APB
2652void
2653java_pop_parser_context (generate)
2654 int generate;
2655{
2656 tree current;
2657 struct parser_ctxt *toFree, *next;
2658
2659 if (!ctxp)
2660 return;
2661
2662 toFree = ctxp;
2663 next = ctxp->next;
2664 if (next)
2665 {
2666 next->incomplete_class = ctxp->incomplete_class;
2667 next->gclass_list = ctxp->gclass_list;
2668 lineno = ctxp->lineno;
19e223db 2669 current_class = ctxp->class_type;
c2952b01
APB
2670 }
2671
d19cbcb5
TT
2672 /* If the old and new lexers differ, then free the old one. */
2673 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2674 java_destroy_lexer (ctxp->lexer);
2675
c2952b01
APB
2676 /* Set the single import class file flag to 0 for the current list
2677 of imported things */
2678 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2679 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2680
2681 /* And restore those of the previous context */
2682 if ((ctxp = next)) /* Assignment is really meant here */
2683 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2684 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2685
2686 /* If we pushed a context to parse a class intended to be generated,
2687 we keep it so we can remember the class. What we could actually
2688 do is to just update a list of class names. */
2689 if (generate)
2690 {
2691 toFree->next = ctxp_for_generation;
2692 ctxp_for_generation = toFree;
2693 }
2694 else
2695 free (toFree);
2696}
2697
2698/* Create a parser context for the use of saving some global
2699 variables. */
2700
e04a16fb
AG
2701void
2702java_parser_context_save_global ()
2703{
22eed1e6
APB
2704 if (!ctxp)
2705 {
2706 java_push_parser_context ();
ee07f4f4
APB
2707 ctxp->saved_data_ctx = 1;
2708 }
c2952b01
APB
2709
2710 /* If this context already stores data, create a new one suitable
2711 for data storage. */
ee07f4f4 2712 else if (ctxp->saved_data)
c2952b01
APB
2713 create_new_parser_context (1);
2714
e04a16fb 2715 ctxp->lineno = lineno;
19e223db 2716 ctxp->class_type = current_class;
e04a16fb 2717 ctxp->filename = input_filename;
19e223db 2718 ctxp->function_decl = current_function_decl;
ee07f4f4 2719 ctxp->saved_data = 1;
e04a16fb
AG
2720}
2721
c2952b01
APB
2722/* Restore some global variables from the previous context. Make the
2723 previous context the current one. */
2724
e04a16fb
AG
2725void
2726java_parser_context_restore_global ()
2727{
e04a16fb 2728 lineno = ctxp->lineno;
19e223db 2729 current_class = ctxp->class_type;
e04a16fb 2730 input_filename = ctxp->filename;
dba41d30
APB
2731 if (wfl_operator)
2732 {
2733 tree s;
2734 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2735 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2736 }
19e223db 2737 current_function_decl = ctxp->function_decl;
c2952b01 2738 ctxp->saved_data = 0;
ee07f4f4
APB
2739 if (ctxp->saved_data_ctx)
2740 java_pop_parser_context (0);
e04a16fb
AG
2741}
2742
c2952b01
APB
2743/* Suspend vital data for the current class/function being parsed so
2744 that an other class can be parsed. Used to let local/anonymous
2745 classes be parsed. */
2746
2747static void
2748java_parser_context_suspend ()
e04a16fb 2749{
c2952b01 2750 /* This makes debugging through java_debug_context easier */
3b304f5b 2751 static const char *name = "<inner buffer context>";
e04a16fb 2752
c2952b01
APB
2753 /* Duplicate the previous context, use it to save the globals we're
2754 interested in */
2755 create_new_parser_context (1);
19e223db
MM
2756 ctxp->function_decl = current_function_decl;
2757 ctxp->class_type = current_class;
5e942c50 2758
c2952b01
APB
2759 /* Then create a new context which inherits all data from the
2760 previous one. This will be the new current context */
2761 create_new_parser_context (1);
2762
2763 /* Help debugging */
2764 ctxp->next->filename = name;
2765}
2766
2767/* Resume vital data for the current class/function being parsed so
2768 that an other class can be parsed. Used to let local/anonymous
2769 classes be parsed. The trick is the data storing file position
2770 informations must be restored to their current value, so parsing
2771 can resume as if no context was ever saved. */
2772
2773static void
2774java_parser_context_resume ()
2775{
2776 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2777 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2778 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2779
2780 /* We need to inherit the list of classes to complete/generate */
2781 restored->incomplete_class = old->incomplete_class;
2782 restored->gclass_list = old->gclass_list;
2783 restored->classd_list = old->classd_list;
2784 restored->class_list = old->class_list;
2785
2786 /* Restore the current class and function from the saver */
19e223db
MM
2787 current_class = saver->class_type;
2788 current_function_decl = saver->function_decl;
c2952b01
APB
2789
2790 /* Retrive the restored context */
2791 ctxp = restored;
2792
2793 /* Re-installed the data for the parsing to carry on */
2794 bcopy (&old->marker_begining, &ctxp->marker_begining,
2795 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2796
2797 /* Buffer context can now be discarded */
2798 free (saver);
2799 free (old);
2800}
2801
2802/* Add a new anchor node to which all statement(s) initializing static
2803 and non static initialized upon declaration field(s) will be
2804 linked. */
2805
2806static void
2807java_parser_context_push_initialized_field ()
2808{
2809 tree node;
2810
2811 node = build_tree_list (NULL_TREE, NULL_TREE);
2812 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2813 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2814
2815 node = build_tree_list (NULL_TREE, NULL_TREE);
2816 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2817 CPC_INITIALIZER_LIST (ctxp) = node;
2818
2819 node = build_tree_list (NULL_TREE, NULL_TREE);
2820 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2821 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2822}
2823
2824/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2825 remember them so we can use it to create and populate the finit$
c2952b01
APB
2826 or <clinit> functions. */
2827
2828static void
2829java_parser_context_pop_initialized_field ()
2830{
2831 tree stmts;
2832 tree class_type = TREE_TYPE (GET_CPC ());
2833
2834 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2835 {
c2952b01
APB
2836 stmts = CPC_INITIALIZER_STMT (ctxp);
2837 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2838 if (stmts && !java_error_count)
2839 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2840 }
2841
c2952b01
APB
2842 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2843 {
2844 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2845 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2846 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2847 /* Keep initialization in order to enforce 8.5 */
2848 if (stmts && !java_error_count)
2849 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2850 }
e04a16fb 2851
c2952b01
APB
2852 /* JDK 1.1 instance initializers */
2853 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2854 {
c2952b01
APB
2855 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2856 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2857 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2858 if (stmts && !java_error_count)
2859 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2860 }
c2952b01
APB
2861}
2862
2863static tree
2864reorder_static_initialized (list)
2865 tree list;
2866{
2867 /* We have to keep things in order. The alias initializer have to
2868 come first, then the initialized regular field, in reverse to
2869 keep them in lexical order. */
2870 tree marker, previous = NULL_TREE;
2871 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2872 if (TREE_CODE (marker) == TREE_LIST
2873 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2874 break;
2875
2876 /* No static initialized, the list is fine as is */
2877 if (!previous)
2878 list = TREE_CHAIN (marker);
2879
2880 /* No marker? reverse the whole list */
2881 else if (!marker)
2882 list = nreverse (list);
2883
2884 /* Otherwise, reverse what's after the marker and the new reordered
2885 sublist will replace the marker. */
b351b287 2886 else
c2952b01
APB
2887 {
2888 TREE_CHAIN (previous) = NULL_TREE;
2889 list = nreverse (list);
2890 list = chainon (TREE_CHAIN (marker), list);
2891 }
2892 return list;
e04a16fb
AG
2893}
2894
c2952b01
APB
2895/* Helper functions to dump the parser context stack. */
2896
2897#define TAB_CONTEXT(C) \
2898 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2899
2900static void
2901java_debug_context_do (tab)
2902 int tab;
2903{
ee07f4f4
APB
2904 struct parser_ctxt *copy = ctxp;
2905 while (copy)
2906 {
c2952b01 2907 TAB_CONTEXT (tab);
ee07f4f4 2908 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2909 TAB_CONTEXT (tab);
ee07f4f4 2910 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2911 TAB_CONTEXT (tab);
2912 fprintf (stderr, "lineno: %d\n", copy->lineno);
2913 TAB_CONTEXT (tab);
ee07f4f4
APB
2914 fprintf (stderr, "package: %s\n",
2915 (copy->package ?
2916 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2917 TAB_CONTEXT (tab);
ee07f4f4 2918 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2919 TAB_CONTEXT (tab);
ee07f4f4
APB
2920 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2921 copy = copy->next;
2922 tab += 2;
2923 }
ee07f4f4
APB
2924}
2925
c2952b01
APB
2926/* Dump the stacked up parser contexts. Intended to be called from a
2927 debugger. */
2928
ee07f4f4
APB
2929void
2930java_debug_context ()
2931{
2932 java_debug_context_do (0);
2933}
2934
c2952b01
APB
2935\f
2936
2937/* Flag for the error report routine to issue the error the first time
2938 it's called (overriding the default behavior which is to drop the
2939 first invocation and honor the second one, taking advantage of a
2940 richer context. */
2941static int force_error = 0;
ee07f4f4 2942
8119c720
APB
2943/* Reporting an constructor invocation error. */
2944static void
2945parse_ctor_invocation_error ()
2946{
2947 if (DECL_CONSTRUCTOR_P (current_function_decl))
2948 yyerror ("Constructor invocation must be first thing in a constructor");
2949 else
2950 yyerror ("Only constructors can invoke constructors");
2951}
2952
2953/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2954
2955static tree
2956parse_jdk1_1_error (msg)
49f48c71 2957 const char *msg;
b67d701b
PB
2958{
2959 sorry (": `%s' JDK1.1(TM) feature", msg);
2960 java_error_count++;
9bbc7d9f 2961 return empty_stmt_node;
b67d701b
PB
2962}
2963
e04a16fb
AG
2964static int do_warning = 0;
2965
2966void
2967yyerror (msg)
49f48c71 2968 const char *msg;
e04a16fb
AG
2969{
2970 static java_lc elc;
2971 static int prev_lineno;
49f48c71 2972 static const char *prev_msg;
e04a16fb 2973
0a2138e2 2974 int save_lineno;
e04a16fb
AG
2975 char *remainder, *code_from_source;
2976 extern struct obstack temporary_obstack;
2977
2978 if (!force_error && prev_lineno == lineno)
2979 return;
2980
2981 /* Save current error location but report latter, when the context is
2982 richer. */
2983 if (ctxp->java_error_flag == 0)
2984 {
2985 ctxp->java_error_flag = 1;
2986 elc = ctxp->elc;
2987 /* Do something to use the previous line if we're reaching the
2988 end of the file... */
2989#ifdef VERBOSE_SKELETON
2990 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2991#endif
2992 return;
2993 }
2994
2995 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2996 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2997 return;
2998
2999 ctxp->java_error_flag = 0;
3000 if (do_warning)
3001 java_warning_count++;
3002 else
3003 java_error_count++;
3004
807bc1db 3005 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
3006 {
3007 elc.col = ctxp->p_line->char_col-1;
3008 elc.line = ctxp->p_line->lineno;
3009 }
3010
3011 save_lineno = lineno;
3012 prev_lineno = lineno = elc.line;
3013 prev_msg = msg;
3014
3015 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3016 obstack_grow0 (&temporary_obstack,
3017 code_from_source, strlen (code_from_source));
3018 remainder = obstack_finish (&temporary_obstack);
3019 if (do_warning)
3020 warning ("%s.\n%s", msg, remainder);
3021 else
3022 error ("%s.\n%s", msg, remainder);
3023
3024 /* This allow us to cheaply avoid an extra 'Invalid expression
3025 statement' error report when errors have been already reported on
3026 the same line. This occurs when we report an error but don't have
3027 a synchronization point other than ';', which
3028 expression_statement is the only one to take care of. */
3029 ctxp->prevent_ese = lineno = save_lineno;
3030}
3031
3032static void
15fdcfe9 3033issue_warning_error_from_context (cl, msg, ap)
5e942c50 3034 tree cl;
d4476be2 3035 const char *msg;
15fdcfe9 3036 va_list ap;
5e942c50 3037{
3b304f5b 3038 const char *saved, *saved_input_filename;
15fdcfe9
PB
3039 char buffer [4096];
3040 vsprintf (buffer, msg, ap);
3041 force_error = 1;
5e942c50
APB
3042
3043 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3044 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3045 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3046
3047 /* We have a CL, that's a good reason for using it if it contains data */
3048 saved = ctxp->filename;
3049 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3050 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3051 saved_input_filename = input_filename;
3052 input_filename = ctxp->filename;
15fdcfe9
PB
3053 java_error (NULL);
3054 java_error (buffer);
5e942c50 3055 ctxp->filename = saved;
1886c9d8 3056 input_filename = saved_input_filename;
15fdcfe9 3057 force_error = 0;
5e942c50
APB
3058}
3059
e04a16fb
AG
3060/* Issue an error message at a current source line CL */
3061
15fdcfe9 3062void
df32d2ce 3063parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3064{
d4476be2 3065#ifndef ANSI_PROTOTYPES
e04a16fb 3066 tree cl;
d4476be2 3067 const char *msg;
e04a16fb 3068#endif
e04a16fb
AG
3069 va_list ap;
3070
3071 VA_START (ap, msg);
d4476be2 3072#ifndef ANSI_PROTOTYPES
e04a16fb 3073 cl = va_arg (ap, tree);
d4476be2 3074 msg = va_arg (ap, const char *);
e04a16fb 3075#endif
15fdcfe9
PB
3076 issue_warning_error_from_context (cl, msg, ap);
3077 va_end (ap);
e04a16fb
AG
3078}
3079
3080/* Issue a warning at a current source line CL */
3081
3082static void
df32d2ce 3083parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3084{
d4476be2 3085#ifndef ANSI_PROTOTYPES
e04a16fb 3086 tree cl;
d4476be2 3087 const char *msg;
e04a16fb 3088#endif
e04a16fb
AG
3089 va_list ap;
3090
3091 VA_START (ap, msg);
d4476be2 3092#ifndef ANSI_PROTOTYPES
e04a16fb 3093 cl = va_arg (ap, tree);
d4476be2 3094 msg = va_arg (ap, const char *);
e04a16fb 3095#endif
e04a16fb 3096
c877974e 3097 force_error = do_warning = 1;
15fdcfe9 3098 issue_warning_error_from_context (cl, msg, ap);
c877974e 3099 do_warning = force_error = 0;
15fdcfe9 3100 va_end (ap);
e04a16fb
AG
3101}
3102
82371d41
APB
3103static tree
3104find_expr_with_wfl (node)
3105 tree node;
3106{
3107 while (node)
3108 {
3109 char code;
3110 tree to_return;
3111
3112 switch (TREE_CODE (node))
3113 {
3114 case BLOCK:
c0d87ff6
PB
3115 node = BLOCK_EXPR_BODY (node);
3116 continue;
82371d41
APB
3117
3118 case COMPOUND_EXPR:
3119 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3120 if (to_return)
3121 return to_return;
c0d87ff6
PB
3122 node = TREE_OPERAND (node, 1);
3123 continue;
82371d41
APB
3124
3125 case LOOP_EXPR:
c0d87ff6
PB
3126 node = TREE_OPERAND (node, 0);
3127 continue;
82371d41
APB
3128
3129 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3130 node = TREE_OPERAND (node, 1);
3131 continue;
3132
82371d41
APB
3133 default:
3134 code = TREE_CODE_CLASS (TREE_CODE (node));
3135 if (((code == '1') || (code == '2') || (code == 'e'))
3136 && EXPR_WFL_LINECOL (node))
3137 return node;
ba179f9f 3138 return NULL_TREE;
82371d41
APB
3139 }
3140 }
3141 return NULL_TREE;
3142}
3143
3144/* Issue a missing return statement error. Uses METHOD to figure the
3145 last line of the method the error occurs in. */
3146
3147static void
3148missing_return_error (method)
3149 tree method;
3150{
3151 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3152 parse_error_context (wfl_operator, "Missing return statement");
3153}
3154
3155/* Issue an unreachable statement error. From NODE, find the next
3156 statement to report appropriately. */
3157static void
3158unreachable_stmt_error (node)
3159 tree node;
3160{
3161 /* Browse node to find the next expression node that has a WFL. Use
3162 the location to report the error */
3163 if (TREE_CODE (node) == COMPOUND_EXPR)
3164 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3165 else
3166 node = find_expr_with_wfl (node);
3167
3168 if (node)
3169 {
3170 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3171 parse_error_context (wfl_operator, "Unreachable statement");
3172 }
3173 else
3174 fatal ("Can't get valid statement - unreachable_stmt_error");
3175}
3176
c877974e 3177int
e04a16fb
AG
3178java_report_errors ()
3179{
3180 if (java_error_count)
3181 fprintf (stderr, "%d error%s",
3182 java_error_count, (java_error_count == 1 ? "" : "s"));
3183 if (java_warning_count)
3184 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3185 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3186 if (java_error_count || java_warning_count)
3187 putc ('\n', stderr);
c877974e 3188 return java_error_count;
e04a16fb
AG
3189}
3190
3191static char *
3192java_accstring_lookup (flags)
3193 int flags;
3194{
3195 static char buffer [80];
3196#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3197
3198 /* Access modifier looked-up first for easier report on forbidden
3199 access. */
3200 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3201 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3202 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3203 if (flags & ACC_STATIC) COPY_RETURN ("static");
3204 if (flags & ACC_FINAL) COPY_RETURN ("final");
3205 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3206 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3207 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3208 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3209 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3210 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3211
3212 buffer [0] = '\0';
3213 return buffer;
3214#undef COPY_RETURN
3215}
3216
b67d701b
PB
3217/* Issuing error messages upon redefinition of classes, interfaces or
3218 variables. */
3219
e04a16fb 3220static void
b67d701b 3221classitf_redefinition_error (context, id, decl, cl)
49f48c71 3222 const char *context;
e04a16fb
AG
3223 tree id, decl, cl;
3224{
3225 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3226 context, IDENTIFIER_POINTER (id),
3227 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3228 /* Here we should point out where its redefined. It's a unicode. FIXME */
3229}
3230
b67d701b
PB
3231static void
3232variable_redefinition_error (context, name, type, line)
3233 tree context, name, type;
3234 int line;
3235{
49f48c71 3236 const char *type_name;
b67d701b
PB
3237
3238 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3239 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3240 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3241 else
0a2138e2 3242 type_name = lang_printable_name (type, 0);
b67d701b
PB
3243
3244 parse_error_context (context,
781b0558 3245 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3246 IDENTIFIER_POINTER (name),
3247 type_name, IDENTIFIER_POINTER (name), line);
3248}
3249
c583dd46
APB
3250static tree
3251build_array_from_name (type, type_wfl, name, ret_name)
3252 tree type, type_wfl, name, *ret_name;
3253{
3254 int more_dims = 0;
49f48c71 3255 const char *string;
c583dd46
APB
3256
3257 /* Eventually get more dims */
3258 string = IDENTIFIER_POINTER (name);
3259 while (string [more_dims] == '[')
3260 more_dims++;
3261
3262 /* If we have, then craft a new type for this variable */
3263 if (more_dims)
3264 {
c0d87ff6 3265 name = get_identifier (&string [more_dims]);
c583dd46 3266
34f4db93
APB
3267 /* If we have a pointer, use its type */
3268 if (TREE_CODE (type) == POINTER_TYPE)
3269 type = TREE_TYPE (type);
c583dd46
APB
3270
3271 /* Building the first dimension of a primitive type uses this
3272 function */
3273 if (JPRIMITIVE_TYPE_P (type))
3274 {
3275 type = build_java_array_type (type, -1);
22eed1e6 3276 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3277 more_dims--;
3278 }
3279 /* Otherwise, if we have a WFL for this type, use it (the type
3280 is already an array on an unresolved type, and we just keep
3281 on adding dimensions) */
3282 else if (type_wfl)
3283 type = type_wfl;
3284
3285 /* Add all the dimensions */
3286 while (more_dims--)
3287 type = build_unresolved_array_type (type);
3288
3289 /* The type may have been incomplete in the first place */
3290 if (type_wfl)
3291 type = obtain_incomplete_type (type);
3292 }
3293
c2952b01
APB
3294 if (ret_name)
3295 *ret_name = name;
c583dd46
APB
3296 return type;
3297}
3298
e04a16fb
AG
3299/* Build something that the type identifier resolver will identify as
3300 being an array to an unresolved type. TYPE_WFL is a WFL on a
3301 identifier. */
3302
3303static tree
3304build_unresolved_array_type (type_or_wfl)
3305 tree type_or_wfl;
3306{
49f48c71 3307 const char *ptr;
e04a16fb 3308
1886c9d8 3309 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3310 just create a array type */
3311 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3312 {
3313 tree type = build_java_array_type (type_or_wfl, -1);
3314 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3315 return type;
3316 }
3317
3318 obstack_1grow (&temporary_obstack, '[');
3319 obstack_grow0 (&temporary_obstack,
3320 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3321 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3322 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3323 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3324 return type_or_wfl;
e04a16fb
AG
3325}
3326
e04a16fb
AG
3327static void
3328parser_add_interface (class_decl, interface_decl, wfl)
3329 tree class_decl, interface_decl, wfl;
3330{
3331 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3332 parse_error_context (wfl, "Interface `%s' repeated",
3333 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3334}
3335
3336/* Bulk of common class/interface checks. Return 1 if an error was
3337 encountered. TAG is 0 for a class, 1 for an interface. */
3338
3339static int
3340check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3341 int is_interface, flags;
3342 tree raw_name, qualified_name, decl, cl;
3343{
3344 tree node;
c2952b01
APB
3345 int sca = 0; /* Static class allowed */
3346 int icaf = 0; /* Inner class allowed flags */
3347 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3348
3349 if (!quiet_flag)
c2952b01
APB
3350 fprintf (stderr, " %s%s %s",
3351 (CPC_INNER_P () ? "inner" : ""),
3352 (is_interface ? "interface" : "class"),
e04a16fb
AG
3353 IDENTIFIER_POINTER (qualified_name));
3354
3355 /* Scope of an interface/class type name:
3356 - Can't be imported by a single type import
3357 - Can't already exists in the package */
3358 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3359 && (node = find_name_in_single_imports (raw_name))
3360 && !CPC_INNER_P ())
e04a16fb
AG
3361 {
3362 parse_error_context
3363 (cl, "%s name `%s' clashes with imported type `%s'",
3364 (is_interface ? "Interface" : "Class"),
3365 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3366 return 1;
3367 }
3368 if (decl && CLASS_COMPLETE_P (decl))
3369 {
b67d701b
PB
3370 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3371 qualified_name, decl, cl);
e04a16fb
AG
3372 return 1;
3373 }
3374
c2952b01
APB
3375 if (check_inner_class_redefinition (raw_name, cl))
3376 return 1;
3377
3378 /* If public, file name should match class/interface name, except
3379 when dealing with an inner class */
3380 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3381 {
49f48c71 3382 const char *f;
e04a16fb
AG
3383
3384 /* Contains OS dependent assumption on path separator. FIXME */
3385 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3386 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3387 f--)
3388 ;
847fe791 3389 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3390 f++;
3391 if (strncmp (IDENTIFIER_POINTER (raw_name),
3392 f , IDENTIFIER_LENGTH (raw_name)) ||
3393 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3394 parse_error_context
3395 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3396 (is_interface ? "interface" : "class"),
3397 IDENTIFIER_POINTER (qualified_name),
3398 IDENTIFIER_POINTER (raw_name));
3399 }
3400
c2952b01
APB
3401 /* Static classes can be declared only in top level classes. Note:
3402 once static, a inner class is a top level class. */
3403 if (flags & ACC_STATIC)
3404 {
3405 /* Catch the specific error of declaring an class inner class
3406 with no toplevel enclosing class. Prevent check_modifiers from
3407 complaining a second time */
3408 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3409 {
3410 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3411 IDENTIFIER_POINTER (qualified_name));
3412 sca = ACC_STATIC;
3413 }
3414 /* Else, in the context of a top-level class declaration, let
3415 `check_modifiers' do its job, otherwise, give it a go */
3416 else
3417 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3418 }
3419
a40d21da 3420 /* Inner classes can be declared private or protected
c2952b01
APB
3421 within their enclosing classes. */
3422 if (CPC_INNER_P ())
3423 {
3424 /* A class which is local to a block can't be public, private,
3425 protected or static. But it is created final, so allow this
3426 one. */
3427 if (current_function_decl)
3428 icaf = sca = uaaf = ACC_FINAL;
3429 else
3430 {
3431 check_modifiers_consistency (flags);
3432 icaf = ACC_PRIVATE|ACC_PROTECTED;
3433 }
3434 }
3435
a40d21da
APB
3436 if (is_interface)
3437 {
3438 if (CPC_INNER_P ())
3439 uaaf = INTERFACE_INNER_MODIFIERS;
3440 else
3441 uaaf = INTERFACE_MODIFIERS;
3442
3443 check_modifiers ("Illegal modifier `%s' for interface declaration",
3444 flags, uaaf);
3445 }
2884c41e 3446 else
a40d21da
APB
3447 check_modifiers ((current_function_decl ?
3448 "Illegal modifier `%s' for local class declaration" :
3449 "Illegal modifier `%s' for class declaration"),
c2952b01 3450 flags, uaaf|sca|icaf);
e04a16fb
AG
3451 return 0;
3452}
3453
c2952b01
APB
3454static void
3455make_nested_class_name (cpc_list)
3456 tree cpc_list;
3457{
3458 tree name;
3459
3460 if (!cpc_list)
3461 return;
3462 else
3463 make_nested_class_name (TREE_CHAIN (cpc_list));
3464
3465 /* Pick the qualified name when dealing with the first upmost
3466 enclosing class */
3467 name = (TREE_CHAIN (cpc_list) ?
3468 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3469 obstack_grow (&temporary_obstack,
3470 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3471 /* Why is NO_DOLLAR_IN_LABEL defined? */
3472#if 0
3473#ifdef NO_DOLLAR_IN_LABEL
3474 fatal ("make_nested_class_name: Can't use '$' as a separator "
3475 "for inner classes");
3476#endif
3477#endif
3478 obstack_1grow (&temporary_obstack, '$');
3479}
3480
3481/* Can't redefine a class already defined in an earlier scope. */
3482
3483static int
3484check_inner_class_redefinition (raw_name, cl)
3485 tree raw_name, cl;
3486{
3487 tree scope_list;
3488
3489 for (scope_list = GET_CPC_LIST (); scope_list;
3490 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3491 if (raw_name == GET_CPC_UN_NODE (scope_list))
3492 {
3493 parse_error_context
3494 (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",
3495 IDENTIFIER_POINTER (raw_name));
3496 return 1;
3497 }
3498 return 0;
3499}
3500
3501static tree
3502find_as_inner_class (enclosing, name, cl)
3503 tree enclosing, name, cl;
3504{
3505 tree qual, to_return;
3506 if (!enclosing)
3507 return NULL_TREE;
3508
3509 name = TYPE_NAME (name);
3510
3511 /* First search: within the scope of `enclosing', search for name */
3512 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3513 qual = EXPR_WFL_QUALIFICATION (cl);
3514 else if (cl)
3515 qual = build_tree_list (cl, NULL_TREE);
3516 else
3517 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3518
3519 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3520 return to_return;
3521
3522 /* We're dealing with a qualified name. Try to resolve thing until
3523 we get something that is an enclosing class. */
3524 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3525 {
3526 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3527
0c2b8145
APB
3528 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3529 qual = TREE_CHAIN (qual))
c2952b01
APB
3530 {
3531 acc = merge_qualified_name (acc,
3532 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3533 BUILD_PTR_FROM_NAME (ptr, acc);
3534 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3535 }
3536
3537 /* A NULL qual and a decl means that the search ended
3538 successfully?!? We have to do something then. FIXME */
3539
3540 if (decl)
3541 enclosing = decl;
3542 else
3543 qual = EXPR_WFL_QUALIFICATION (cl);
3544 }
3545 /* Otherwise, create a qual for the other part of the resolution. */
3546 else
3547 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3548
1e12ab9b 3549 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3550}
3551
3552/* We go inside the list of sub classes and try to find a way
3553 through. */
3554
3555static tree
3556find_as_inner_class_do (qual, enclosing)
3557 tree qual, enclosing;
3558{
3559 if (!qual)
3560 return NULL_TREE;
3561
3562 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3563 {
3564 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3565 tree next_enclosing = NULL_TREE;
3566 tree inner_list;
3567
3568 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3569 inner_list; inner_list = TREE_CHAIN (inner_list))
3570 {
3571 if (TREE_VALUE (inner_list) == name_to_match)
3572 {
3573 next_enclosing = TREE_PURPOSE (inner_list);
3574 break;
3575 }
3576 }
3577 enclosing = next_enclosing;
3578 }
3579
3580 return (!qual && enclosing ? enclosing : NULL_TREE);
3581}
3582
3583/* Reach all inner classes and tie their unqualified name to a
3584 DECL. */
3585
3586static void
3587set_nested_class_simple_name_value (outer, set)
3588 tree outer;
3589 int set;
3590{
3591 tree l;
3592
3593 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3594 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3595 TREE_PURPOSE (l) : NULL_TREE);
3596}
3597
3598static void
3599link_nested_class_to_enclosing ()
3600{
3601 if (GET_ENCLOSING_CPC ())
3602 {
3603 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3604 DECL_INNER_CLASS_LIST (enclosing) =
3605 tree_cons (GET_CPC (), GET_CPC_UN (),
3606 DECL_INNER_CLASS_LIST (enclosing));
3607 enclosing = enclosing;
3608 }
3609}
3610
3611static tree
3612maybe_make_nested_class_name (name)
3613 tree name;
3614{
3615 tree id = NULL_TREE;
3616
3617 if (CPC_INNER_P ())
3618 {
3619 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3620 obstack_grow0 (&temporary_obstack,
3621 IDENTIFIER_POINTER (name),
3622 IDENTIFIER_LENGTH (name));
c2952b01
APB
3623 id = get_identifier (obstack_finish (&temporary_obstack));
3624 if (ctxp->package)
3625 QUALIFIED_P (id) = 1;
3626 }
3627 return id;
3628}
3629
3630/* If DECL is NULL, create and push a new DECL, record the current
3631 line CL and do other maintenance things. */
3632
e04a16fb 3633static tree
c2952b01
APB
3634maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3635 tree decl, raw_name, qualified_name, cl;
e04a16fb 3636{
5e942c50 3637 if (!decl)
e04a16fb 3638 decl = push_class (make_class (), qualified_name);
c2952b01 3639
e04a16fb
AG
3640 /* Take care of the file and line business */
3641 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3642 /* If we're emiting xrefs, store the line/col number information */
3643 if (flag_emit_xref)
3644 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3645 else
3646 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3647 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3648 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3649 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3650
c2952b01
APB
3651 PUSH_CPC (decl, raw_name);
3652 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3653
e04a16fb
AG
3654 /* Link the declaration to the already seen ones */
3655 TREE_CHAIN (decl) = ctxp->class_list;
3656 ctxp->class_list = decl;
5e942c50 3657
23a79c61 3658 /* Create a new nodes in the global lists */
5e942c50 3659 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3660 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3661
e04a16fb
AG
3662 /* Install a new dependency list element */
3663 create_jdep_list (ctxp);
3664
3665 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3666 IDENTIFIER_POINTER (qualified_name)));
3667 return decl;
3668}
3669
3670static void
3671add_superinterfaces (decl, interface_list)
3672 tree decl, interface_list;
3673{
3674 tree node;
3675 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3676 takes care of ensuring that:
3677 - This is an accessible interface type,
3678 - Circularity detection.
3679 parser_add_interface is then called. If present but not defined,
3680 the check operation is delayed until the super interface gets
3681 defined. */
3682 for (node = interface_list; node; node = TREE_CHAIN (node))
3683 {
15fdcfe9 3684 tree current = TREE_PURPOSE (node);
5e942c50
APB
3685 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3686 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3687 {
5e942c50
APB
3688 if (!parser_check_super_interface (idecl, decl, current))
3689 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3690 }
3691 else
3692 register_incomplete_type (JDEP_INTERFACE,
3693 current, decl, NULL_TREE);
3694 }
3695}
3696
3697/* Create an interface in pass1 and return its decl. Return the
3698 interface's decl in pass 2. */
3699
3700static tree
3701create_interface (flags, id, super)
3702 int flags;
3703 tree id, super;
3704{
e04a16fb 3705 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3706 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3707 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3708
3709 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3710
3711 /* Basic checks: scope, redefinition, modifiers */
3712 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3713 {
3714 PUSH_ERROR ();
3715 return NULL_TREE;
3716 }
3717
3718 /* Suspend the current parsing context if we're parsing an inner
3719 interface */
3720 if (CPC_INNER_P ())
3721 java_parser_context_suspend ();
3722
3723 /* Push a new context for (static) initialized upon declaration fields */
3724 java_parser_context_push_initialized_field ();
e04a16fb
AG
3725
3726 /* Interface modifiers check
3727 - public/abstract allowed (already done at that point)
3728 - abstract is obsolete (comes first, it's a warning, or should be)
3729 - Can't use twice the same (checked in the modifier rule) */
c877974e 3730 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3731 parse_warning_context
3732 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3733 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3734
3735 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3736 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3737
3738 /* Set super info and mark the class a complete */
2aa11e97 3739 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3740 object_type_node, ctxp->interface_number);
3741 ctxp->interface_number = 0;
3742 CLASS_COMPLETE_P (decl) = 1;
3743 add_superinterfaces (decl, super);
3744
3745 return decl;
3746}
3747
c2952b01
APB
3748/* Anonymous class counter. Will be reset to 1 every time a non
3749 anonymous class gets created. */
3750static int anonymous_class_counter = 1;
3751
3752/* Patch anonymous class CLASS, by either extending or implementing
3753 DEP. */
3754
3755static void
3756patch_anonymous_class (type_decl, class_decl, wfl)
3757 tree type_decl, class_decl, wfl;
3758{
3759 tree class = TREE_TYPE (class_decl);
3760 tree type = TREE_TYPE (type_decl);
3761 tree binfo = TYPE_BINFO (class);
3762
3763 /* If it's an interface, implement it */
3764 if (CLASS_INTERFACE (type_decl))
3765 {
3766 tree s_binfo;
3767 int length;
3768
3769 if (parser_check_super_interface (type_decl, class_decl, wfl))
3770 return;
3771
3772 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3773 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3774 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3775 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3776 /* And add the interface */
3777 parser_add_interface (class_decl, type_decl, wfl);
3778 }
3779 /* Otherwise, it's a type we want to extend */
3780 else
3781 {
3782 if (parser_check_super (type_decl, class_decl, wfl))
3783 return;
3784 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3785 }
3786}
3787
3788static tree
3789create_anonymous_class (location, type_name)
3790 int location;
3791 tree type_name;
3792{
3793 char buffer [80];
3794 tree super = NULL_TREE, itf = NULL_TREE;
3795 tree id, type_decl, class;
3796
3797 /* The unqualified name of the anonymous class. It's just a number. */
3798 sprintf (buffer, "%d", anonymous_class_counter++);
3799 id = build_wfl_node (get_identifier (buffer));
3800 EXPR_WFL_LINECOL (id) = location;
3801
3802 /* We know about the type to extend/implement. We go ahead */
3803 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3804 {
3805 /* Create a class which either implements on extends the designated
3806 class. The class bears an innacessible name. */
3807 if (CLASS_INTERFACE (type_decl))
3808 {
3809 /* It's OK to modify it here. It's been already used and
3810 shouldn't be reused */
3811 ctxp->interface_number = 1;
3812 /* Interfaces should presented as a list of WFLs */
3813 itf = build_tree_list (type_name, NULL_TREE);
3814 }
3815 else
3816 super = type_name;
3817 }
3818
3819 class = create_class (ACC_FINAL, id, super, itf);
3820
3821 /* We didn't know anything about the stuff. We register a dependence. */
3822 if (!type_decl)
3823 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3824
3825 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3826 return class;
3827}
3828
a40d21da 3829/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3830 interface's decl in pass 2. */
3831
3832static tree
3833create_class (flags, id, super, interfaces)
3834 int flags;
3835 tree id, super, interfaces;
3836{
e04a16fb
AG
3837 tree raw_name = EXPR_WFL_NODE (id);
3838 tree class_id, decl;
9ee9b555 3839 tree super_decl_type;
e04a16fb 3840
98a52c2c 3841 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3842 decl = IDENTIFIER_CLASS_VALUE (class_id);
3843 EXPR_WFL_NODE (id) = class_id;
3844
3845 /* Basic check: scope, redefinition, modifiers */
3846 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3847 {
3848 PUSH_ERROR ();
3849 return NULL_TREE;
3850 }
3851
3852 /* Suspend the current parsing context if we're parsing an inner
3853 class or an anonymous class. */
3854 if (CPC_INNER_P ())
3855 java_parser_context_suspend ();
3856 /* Push a new context for (static) initialized upon declaration fields */
3857 java_parser_context_push_initialized_field ();
e04a16fb
AG
3858
3859 /* Class modifier check:
3860 - Allowed modifier (already done at that point)
3861 - abstract AND final forbidden
3862 - Public classes defined in the correct file */
3863 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3864 parse_error_context
3865 (id, "Class `%s' can't be declared both abstract and final",
3866 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3867
3868 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3869 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3870
3871 /* If SUPER exists, use it, otherwise use Object */
3872 if (super)
3873 {
3874 /* Can't extend java.lang.Object */
3875 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3876 {
3877 parse_error_context (id, "Can't extend `java.lang.Object'");
3878 return NULL_TREE;
3879 }
3880
2c3199bc
PB
3881 super_decl_type =
3882 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3883 }
3884 else if (TREE_TYPE (decl) != object_type_node)
3885 super_decl_type = object_type_node;
3886 /* We're defining java.lang.Object */
3887 else
3888 super_decl_type = NULL_TREE;
3889
6d003d5c
BM
3890 /* A class nested in an interface is implicitly static. */
3891 if (INNER_CLASS_DECL_P (decl)
3892 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3893 {
3894 flags |= ACC_STATIC;
3895 }
3896
3897 /* Set super info and mark the class as complete. */
e04a16fb
AG
3898 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3899 ctxp->interface_number);
3900 ctxp->interface_number = 0;
3901 CLASS_COMPLETE_P (decl) = 1;
3902 add_superinterfaces (decl, interfaces);
3903
c2952b01
APB
3904 /* Add the private this$<n> field, Replicate final locals still in
3905 scope as private final fields mangled like val$<local_name>.
3906 This doesn't not occur for top level (static) inner classes. */
3907 if (PURE_INNER_CLASS_DECL_P (decl))
3908 add_inner_class_fields (decl, current_function_decl);
3909
7f10c2e2
APB
3910 /* If doing xref, store the location at which the inherited class
3911 (if any) was seen. */
3912 if (flag_emit_xref && super)
3913 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3914
5e942c50
APB
3915 /* Eventually sets the @deprecated tag flag */
3916 CHECK_DEPRECATED (decl);
3917
165f37bc
APB
3918 /* Reset the anonymous class counter when declaring non inner classes */
3919 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3920 anonymous_class_counter = 1;
3921
e04a16fb
AG
3922 return decl;
3923}
3924
c2952b01 3925/* End a class declaration: register the statements used to create
c00f0fb2 3926 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3927 parser context if necessary. */
3928
3929static void
3930end_class_declaration (resume)
3931 int resume;
3932{
3933 /* If an error occured, context weren't pushed and won't need to be
3934 popped by a resume. */
3935 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3936
3937 java_parser_context_pop_initialized_field ();
3938 POP_CPC ();
3939 if (resume && no_error_occured)
3940 java_parser_context_resume ();
93220702
APB
3941
3942 /* We're ending a class declaration, this is a good time to reset
3943 the interface cout. Note that might have been already done in
3944 create_interface, but if at that time an inner class was being
3945 dealt with, the interface count was reset in a context created
3946 for the sake of handling inner classes declaration. */
3947 ctxp->interface_number = 0;
c2952b01
APB
3948}
3949
3950static void
3951add_inner_class_fields (class_decl, fct_decl)
3952 tree class_decl;
3953 tree fct_decl;
3954{
3955 tree block, marker, f;
3956
3957 f = add_field (TREE_TYPE (class_decl),
3958 build_current_thisn (TREE_TYPE (class_decl)),
3959 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3960 ACC_PRIVATE);
3961 FIELD_THISN (f) = 1;
3962
3963 if (!fct_decl)
3964 return;
3965
3966 for (block = GET_CURRENT_BLOCK (fct_decl);
3967 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3968 {
3969 tree decl;
3970 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3971 {
1f8f4a0b 3972 tree name, pname;
c2952b01
APB
3973 tree wfl, init, list;
3974
3975 /* Avoid non final arguments. */
c7303e41 3976 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3977 continue;
3978
3979 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3980 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3981 wfl = build_wfl_node (name);
3982 init = build_wfl_node (pname);
c2952b01 3983 /* Build an initialization for the field: it will be
c00f0fb2 3984 initialized by a parameter added to finit$, bearing a
c2952b01 3985 mangled name of the field itself (param$<n>.) The
c00f0fb2 3986 parameter is provided to finit$ by the constructor
c2952b01
APB
3987 invoking it (hence the constructor will also feature a
3988 hidden parameter, set to the value of the outer context
3989 local at the time the inner class is created.)
3990
3991 Note: we take into account all possible locals that can
3992 be accessed by the inner class. It's actually not trivial
3993 to minimize these aliases down to the ones really
3994 used. One way to do that would be to expand all regular
c00f0fb2 3995 methods first, then finit$ to get a picture of what's
c2952b01
APB
3996 used. It works with the exception that we would have to
3997 go back on all constructor invoked in regular methods to
3998 have their invokation reworked (to include the right amount
3999 of alias initializer parameters.)
4000
4001 The only real way around, I think, is a first pass to
4002 identify locals really used in the inner class. We leave
4003 the flag FIELD_LOCAL_ALIAS_USED around for that future
4004 use.
4005
4006 On the other hand, it only affect local inner classes,
c00f0fb2 4007 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4008 unecessary arguments. It's easy for a developper to keep
4009 this number of parameter down by using the `final'
4010 keyword only when necessary. For the time being, we can
4011 issue a warning on unecessary finals. FIXME */
4012 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4013 wfl, init);
4014
4015 /* Register the field. The TREE_LIST holding the part
4016 initialized/initializer will be marked ARG_FINAL_P so
4017 that the created field can be marked
4018 FIELD_LOCAL_ALIAS. */
4019 list = build_tree_list (wfl, init);
4020 ARG_FINAL_P (list) = 1;
4021 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4022 }
4023 }
4024
4025 if (!CPC_INITIALIZER_STMT (ctxp))
4026 return;
4027
4028 /* If we ever registered an alias field, insert and marker to
4029 remeber where the list ends. The second part of the list (the one
4030 featuring initialized fields) so it can be later reversed to
4031 enforce 8.5. The marker will be removed during that operation. */
4032 marker = build_tree_list (NULL_TREE, NULL_TREE);
4033 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4034 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4035}
4036
e04a16fb
AG
4037/* Can't use lookup_field () since we don't want to load the class and
4038 can't set the CLASS_LOADED_P flag */
4039
4040static tree
4041find_field (class, name)
4042 tree class;
4043 tree name;
4044{
4045 tree decl;
4046 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4047 {
4048 if (DECL_NAME (decl) == name)
4049 return decl;
4050 }
4051 return NULL_TREE;
4052}
4053
4054/* Wrap around lookup_field that doesn't potentially upset the value
4055 of CLASS */
4056
4057static tree
4058lookup_field_wrapper (class, name)
4059 tree class, name;
4060{
4061 tree type = class;
9a7ab4b3 4062 tree decl = NULL_TREE;
c877974e 4063 java_parser_context_save_global ();
f2760b27
APB
4064
4065 /* Last chance: if we're within the context of an inner class, we
4066 might be trying to access a local variable defined in an outer
4067 context. We try to look for it now. */
9a7ab4b3 4068 if (INNER_CLASS_TYPE_P (class))
f2760b27 4069 {
9a7ab4b3 4070 tree new_name;
1f8f4a0b 4071 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4072 decl = lookup_field (&type, new_name);
f2760b27
APB
4073 if (decl && decl != error_mark_node)
4074 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4075 }
9a7ab4b3
APB
4076 if (!decl || decl == error_mark_node)
4077 {
4078 type = class;
4079 decl = lookup_field (&type, name);
4080 }
f2760b27 4081
c877974e 4082 java_parser_context_restore_global ();
93024893 4083 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4084}
4085
4086/* Find duplicate field within the same class declarations and report
c583dd46
APB
4087 the error. Returns 1 if a duplicated field was found, 0
4088 otherwise. */
e04a16fb
AG
4089
4090static int
c583dd46 4091duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4092 tree new_field_name, new_type, cl;
e04a16fb
AG
4093{
4094 /* This might be modified to work with method decl as well */
c2952b01 4095 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4096 if (decl)
4097 {
c2e3db92 4098 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4099 ((TREE_CODE (new_type) == POINTER_TYPE
4100 && TREE_TYPE (new_type) == NULL_TREE) ?
4101 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4102 lang_printable_name (new_type, 1)));
c877974e
APB
4103 /* The type may not have been completed by the time we report
4104 the error */
c2e3db92 4105 char *t2 = xstrdup (purify_type_name
4a5f66c3 4106 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4107 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4108 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4109 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4110 parse_error_context
4111 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4112 t1, IDENTIFIER_POINTER (new_field_name),
4113 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4114 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4115 free (t1);
4116 free (t2);
c583dd46 4117 return 1;
e04a16fb 4118 }
c583dd46 4119 return 0;
e04a16fb
AG
4120}
4121
4122/* Field registration routine. If TYPE doesn't exist, field
4123 declarations are linked to the undefined TYPE dependency list, to
4124 be later resolved in java_complete_class () */
4125
4126static void
4127register_fields (flags, type, variable_list)
4128 int flags;
4129 tree type, variable_list;
4130{
c583dd46 4131 tree current, saved_type;
c2952b01 4132 tree class_type = NULL_TREE;
e04a16fb
AG
4133 int saved_lineno = lineno;
4134 int must_chain = 0;
4135 tree wfl = NULL_TREE;
4136
c2952b01
APB
4137 if (GET_CPC ())
4138 class_type = TREE_TYPE (GET_CPC ());
4139
4140 if (!class_type || class_type == error_mark_node)
4141 return;
4142
e04a16fb
AG
4143 /* If we're adding fields to interfaces, those fields are public,
4144 static, final */
4145 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4146 {
4147 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4148 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4149 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4150 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4151 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4152 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4153 check_modifiers ("Illegal interface member modifier `%s'", flags,
4154 INTERFACE_FIELD_MODIFIERS);
4155 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4156 }
4157
c583dd46
APB
4158 /* Obtain a suitable type for resolution, if necessary */
4159 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4160
4161 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4162 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4163
c583dd46
APB
4164 for (current = variable_list, saved_type = type; current;
4165 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4166 {
c877974e 4167 tree real_type;
c583dd46 4168 tree field_decl;
e04a16fb
AG
4169 tree cl = TREE_PURPOSE (current);
4170 tree init = TREE_VALUE (current);
4171 tree current_name = EXPR_WFL_NODE (cl);
4172
cf1748bf 4173 /* Can't declare non-final static fields in inner classes */
c2952b01 4174 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4175 && !(flags & ACC_FINAL))
c2952b01 4176 parse_error_context
cf1748bf 4177 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4178 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4179 lang_printable_name (class_type, 0));
4180
c583dd46
APB
4181 /* Process NAME, as it may specify extra dimension(s) for it */
4182 type = build_array_from_name (type, wfl, current_name, &current_name);
4183
c583dd46
APB
4184 /* Type adjustment. We may have just readjusted TYPE because
4185 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4186 a reference if we can and don't have one already. Also
4187 change the name if we have an init. */
4188 if (type != saved_type)
4189 {
1886c9d8 4190 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4191 if (init)
4192 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4193 }
e04a16fb 4194
c877974e
APB
4195 real_type = GET_REAL_TYPE (type);
4196 /* Check for redeclarations */
4197 if (duplicate_declaration_error_p (current_name, real_type, cl))
4198 continue;
4199
c583dd46 4200 /* Set lineno to the line the field was found and create a
5e942c50 4201 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4202 if (flag_emit_xref)
4203 lineno = EXPR_WFL_LINECOL (cl);
4204 else
4205 lineno = EXPR_WFL_LINENO (cl);
c877974e 4206 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4207 CHECK_DEPRECATED (field_decl);
c2952b01 4208
c7303e41
APB
4209 /* If the field denotes a final instance variable, then we
4210 allocate a LANG_DECL_SPECIFIC part to keep track of its
4211 initialization. We also mark whether the field was
4212 initialized upon it's declaration. We don't do that if the
4213 created field is an alias to a final local. */
4214 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4215 {
4216 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4217 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4218 if ((flags & ACC_STATIC) && init)
4219 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4220 }
4221
4222 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4223 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4224 hide parameters to this inner class finit$ and
4225 constructors. It also means that the field isn't final per
4226 say. */
c2952b01 4227 if (ARG_FINAL_P (current))
c7303e41
APB
4228 {
4229 FIELD_LOCAL_ALIAS (field_decl) = 1;
4230 FIELD_FINAL (field_decl) = 0;
4231 }
c583dd46
APB
4232
4233 /* Check if we must chain. */
4234 if (must_chain)
4235 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4236
c583dd46
APB
4237 /* If we have an initialization value tied to the field */
4238 if (init)
4239 {
4240 /* The field is declared static */
e04a16fb 4241 if (flags & ACC_STATIC)
e04a16fb 4242 {
7525cc04
APB
4243 /* We include the field and its initialization part into
4244 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4245 walked, field initializations will be processed and
4246 fields initialized with known constants will be taken
4247 out of <clinit> and have their DECL_INITIAL set
7525cc04 4248 appropriately. */
c2952b01
APB
4249 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4250 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4251 if (TREE_OPERAND (init, 1)
4252 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4253 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4254 }
5e942c50
APB
4255 /* A non-static field declared with an immediate initialization is
4256 to be initialized in <init>, if any. This field is remembered
4257 to be processed at the time of the generation of <init>. */
c583dd46
APB
4258 else
4259 {
c2952b01
APB
4260 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4261 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4262 }
5b09b33e 4263 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4264 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4265 }
4266 }
4267 lineno = saved_lineno;
4268}
4269
c00f0fb2
APB
4270/* Generate finit$, using the list of initialized fields to populate
4271 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4272 one(s) used to initialized the field(s) caching outer context
4273 local(s). */
22eed1e6 4274
c2952b01
APB
4275static tree
4276generate_finit (class_type)
4277 tree class_type;
22eed1e6 4278{
c2952b01
APB
4279 int count = 0;
4280 tree list = TYPE_FINIT_STMT_LIST (class_type);
4281 tree mdecl, current, parms;
4282
4283 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4284 class_type, NULL_TREE,
4285 &count);
4286 CRAFTED_PARAM_LIST_FIXUP (parms);
4287 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4288 finit_identifier_node, parms);
4289 fix_method_argument_names (parms, mdecl);
4290 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4291 mdecl, NULL_TREE);
4292 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4293 start_artificial_method_body (mdecl);
4294
c2952b01 4295 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4296 java_method_add_stmt (mdecl,
4297 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4298 current));
22eed1e6 4299 end_artificial_method_body (mdecl);
c2952b01 4300 return mdecl;
22eed1e6
APB
4301}
4302
e04a16fb 4303static void
c2952b01
APB
4304add_instance_initializer (mdecl)
4305 tree mdecl;
e04a16fb 4306{
c2952b01
APB
4307 tree current;
4308 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4309 tree compound = NULL_TREE;
e04a16fb 4310
c2952b01 4311 if (stmt_list)
e04a16fb 4312 {
c2952b01
APB
4313 for (current = stmt_list; current; current = TREE_CHAIN (current))
4314 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4315
c2952b01
APB
4316 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4317 NULL_TREE, compound));
4318 }
e04a16fb
AG
4319}
4320
4321/* Shared accros method_declarator and method_header to remember the
4322 patch stage that was reached during the declaration of the method.
4323 A method DECL is built differently is there is no patch
4324 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4325 pending on the currently defined method. */
4326
4327static int patch_stage;
4328
4329/* Check the method declaration and add the method to its current
4330 class. If the argument list is known to contain incomplete types,
4331 the method is partially added and the registration will be resume
22eed1e6
APB
4332 once the method arguments resolved. If TYPE is NULL, we're dealing
4333 with a constructor. */
e04a16fb
AG
4334
4335static tree
4336method_header (flags, type, mdecl, throws)
4337 int flags;
4338 tree type, mdecl, throws;
4339{
1886c9d8 4340 tree type_wfl = NULL_TREE;
79d13333 4341 tree meth_name = NULL_TREE;
c2952b01 4342 tree current, orig_arg, this_class = NULL;
34d4df06 4343 tree id, meth;
e04a16fb 4344 int saved_lineno;
1886c9d8 4345 int constructor_ok = 0, must_chain;
c2952b01 4346 int count;
34d4df06
APB
4347
4348 if (mdecl == error_mark_node)
4349 return error_mark_node;
4350 meth = TREE_VALUE (mdecl);
4351 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4352
4353 check_modifiers_consistency (flags);
79d13333 4354
c2952b01
APB
4355 if (GET_CPC ())
4356 this_class = TREE_TYPE (GET_CPC ());
4357
4358 if (!this_class || this_class == error_mark_node)
79d13333 4359 return NULL_TREE;
e04a16fb
AG
4360
4361 /* There are some forbidden modifiers for an abstract method and its
4362 class must be abstract as well. */
22eed1e6 4363 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4364 {
4365 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4366 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4367 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4368 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4369 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4370 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4371 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4372 parse_error_context
781b0558 4373 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4374 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4375 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4376 }
c2952b01 4377
22eed1e6
APB
4378 /* Things to be checked when declaring a constructor */
4379 if (!type)
4380 {
4381 int ec = java_error_count;
4382 /* 8.6: Constructor declarations: we might be trying to define a
4383 method without specifying a return type. */
c2952b01 4384 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4385 parse_error_context
4386 (id, "Invalid method declaration, return type required");
4387 /* 8.6.3: Constructor modifiers */
4388 else
4389 {
4390 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4391 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4392 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4393 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4394 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4395 }
4396 /* If we found error here, we don't consider it's OK to tread
4397 the method definition as a constructor, for the rest of this
4398 function */
4399 if (ec == java_error_count)
4400 constructor_ok = 1;
4401 }
e04a16fb
AG
4402
4403 /* Method declared within the scope of an interface are implicitly
4404 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4405 modifiers are checked right after. */
e04a16fb
AG
4406
4407 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4408 {
4409 /* If FLAGS isn't set because of a modifier, turn the
4410 corresponding modifier WFL to NULL so we issue a warning on
4411 the obsolete use of the modifier */
4412 if (!(flags & ACC_PUBLIC))
4413 MODIFIER_WFL (PUBLIC_TK) = NULL;
4414 if (!(flags & ACC_ABSTRACT))
4415 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4416 flags |= ACC_PUBLIC;
4417 flags |= ACC_ABSTRACT;
4418 }
4419
c2952b01
APB
4420 /* Inner class can't declare static methods */
4421 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4422 {
4423 parse_error_context
4424 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4425 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4426 lang_printable_name (this_class, 0));
4427 }
4428
e04a16fb
AG
4429 /* Modifiers context reset moved up, so abstract method declaration
4430 modifiers can be later checked. */
4431
22eed1e6
APB
4432 /* Set constructor returned type to void and method name to <init>,
4433 unless we found an error identifier the constructor (in which
4434 case we retain the original name) */
4435 if (!type)
4436 {
4437 type = void_type_node;
4438 if (constructor_ok)
4439 meth_name = init_identifier_node;
4440 }
4441 else
4442 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4443
1886c9d8
APB
4444 /* Do the returned type resolution and registration if necessary */
4445 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4446
4a5f66c3
APB
4447 if (meth_name)
4448 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4449 EXPR_WFL_NODE (id) = meth_name;
4450 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4451
4452 if (must_chain)
e04a16fb 4453 {
1886c9d8
APB
4454 patch_stage = JDEP_METHOD_RETURN;
4455 register_incomplete_type (patch_stage, type_wfl, id, type);
4456 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4457 }
4458 else
1886c9d8 4459 TREE_TYPE (meth) = type;
e04a16fb
AG
4460
4461 saved_lineno = lineno;
4462 /* When defining an abstract or interface method, the curly
4463 bracket at level 1 doesn't exist because there is no function
4464 body */
4465 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4466 EXPR_WFL_LINENO (id));
4467
5e942c50
APB
4468 /* Remember the original argument list */
4469 orig_arg = TYPE_ARG_TYPES (meth);
4470
e04a16fb
AG
4471 if (patch_stage) /* includes ret type and/or all args */
4472 {
4473 jdep *jdep;
4474 meth = add_method_1 (this_class, flags, meth_name, meth);
4475 /* Patch for the return type */
4476 if (patch_stage == JDEP_METHOD_RETURN)
4477 {
4478 jdep = CLASSD_LAST (ctxp->classd_list);
4479 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4480 }
4481 /* This is the stop JDEP. METH allows the function's signature
4482 to be computed. */
4483 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4484 }
4485 else
5e942c50
APB
4486 meth = add_method (this_class, flags, meth_name,
4487 build_java_signature (meth));
4488
c2952b01
APB
4489 /* Remember final parameters */
4490 MARK_FINAL_PARMS (meth, orig_arg);
4491
5e942c50
APB
4492 /* Fix the method argument list so we have the argument name
4493 information */
4494 fix_method_argument_names (orig_arg, meth);
4495
4496 /* Register the parameter number and re-install the current line
4497 number */
e04a16fb
AG
4498 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4499 lineno = saved_lineno;
b9f7e36c
APB
4500
4501 /* Register exception specified by the `throws' keyword for
4502 resolution and set the method decl appropriate field to the list.
4503 Note: the grammar ensures that what we get here are class
4504 types. */
4505 if (throws)
4506 {
4507 throws = nreverse (throws);
4508 for (current = throws; current; current = TREE_CHAIN (current))
4509 {
4510 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4511 NULL_TREE, NULL_TREE);
4512 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4513 &TREE_VALUE (current);
4514 }
4515 DECL_FUNCTION_THROWS (meth) = throws;
4516 }
4517
e04a16fb
AG
4518 /* We set the DECL_NAME to ID so we can track the location where
4519 the function was declared. This allow us to report
4520 redefinition error accurately. When method are verified,
4521 DECL_NAME is reinstalled properly (using the content of the
4522 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4523 when Object is being defined. Constructor <init> names will be
4524 reinstalled the same way. */
c2952b01 4525 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4526 DECL_NAME (meth) = id;
22eed1e6
APB
4527
4528 /* Set the flag if we correctly processed a constructor */
4529 if (constructor_ok)
c2952b01
APB
4530 {
4531 DECL_CONSTRUCTOR_P (meth) = 1;
4532 /* Compute and store the number of artificial parameters declared
4533 for this constructor */
4534 for (count = 0, current = TYPE_FIELDS (this_class); current;
4535 current = TREE_CHAIN (current))
4536 if (FIELD_LOCAL_ALIAS (current))
4537 count++;
4538 DECL_FUNCTION_NAP (meth) = count;
4539 }
22eed1e6 4540
5e942c50
APB
4541 /* Eventually set the @deprecated tag flag */
4542 CHECK_DEPRECATED (meth);
4543
7f10c2e2
APB
4544 /* If doing xref, store column and line number information instead
4545 of the line number only. */
4546 if (flag_emit_xref)
4547 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4548
e04a16fb
AG
4549 return meth;
4550}
4551
5e942c50
APB
4552static void
4553fix_method_argument_names (orig_arg, meth)
4554 tree orig_arg, meth;
4555{
4556 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4557 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4558 {
4559 TREE_PURPOSE (arg) = this_identifier_node;
4560 arg = TREE_CHAIN (arg);
4561 }
de4c7b02 4562 while (orig_arg != end_params_node)
5e942c50
APB
4563 {
4564 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4565 orig_arg = TREE_CHAIN (orig_arg);
4566 arg = TREE_CHAIN (arg);
4567 }
4568}
4569
22eed1e6
APB
4570/* Complete the method declaration with METHOD_BODY. */
4571
4572static void
b635eb2f 4573finish_method_declaration (method_body)
22eed1e6
APB
4574 tree method_body;
4575{
79d13333
APB
4576 int flags;
4577
4578 if (!current_function_decl)
4579 return;
4580
4581 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4582
4583 /* 8.4.5 Method Body */
4584 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4585 {
4586 tree wfl = DECL_NAME (current_function_decl);
4587 parse_error_context (wfl,
4588 "%s method `%s' can't have a body defined",
4589 (METHOD_NATIVE (current_function_decl) ?
4590 "Native" : "Abstract"),
4591 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4592 method_body = NULL_TREE;
4593 }
4594 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4595 {
4596 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4597 parse_error_context
4598 (wfl,
4599 "Non native and non abstract method `%s' must have a body defined",
4600 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4601 method_body = NULL_TREE;
4602 }
4603
2c56429a
APB
4604 if (flag_emit_class_files && method_body
4605 && TREE_CODE (method_body) == NOP_EXPR
4606 && TREE_TYPE (current_function_decl)
4607 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4608 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4609
22eed1e6
APB
4610 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4611 maybe_absorb_scoping_blocks ();
4612 /* Exit function's body */
4613 exit_block ();
4614 /* Merge last line of the function with first line, directly in the
4615 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4616 if (!flag_emit_xref)
4617 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4618
4619 /* Since function's argument's list are shared, reset the
4620 ARG_FINAL_P parameter that might have been set on some of this
4621 function parameters. */
4622 UNMARK_FINAL_PARMS (current_function_decl);
4623
f099f336
APB
4624 /* So we don't have an irrelevant function declaration context for
4625 the next static block we'll see. */
4626 current_function_decl = NULL_TREE;
22eed1e6
APB
4627}
4628
4629/* Build a an error message for constructor circularity errors. */
4630
4631static char *
4632constructor_circularity_msg (from, to)
4633 tree from, to;
4634{
4635 static char string [4096];
c2e3db92 4636 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4637 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4638 free (t);
4639 return string;
4640}
4641
4642/* Verify a circular call to METH. Return 1 if an error is found, 0
4643 otherwise. */
4644
4645static int
4646verify_constructor_circularity (meth, current)
4647 tree meth, current;
4648{
4649 static tree list = NULL_TREE;
19e223db 4650 static int initialized_p;
22eed1e6 4651 tree c;
19e223db
MM
4652
4653 /* If we haven't already registered LIST with the garbage collector,
4654 do so now. */
4655 if (!initialized_p)
4656 {
4657 ggc_add_tree_root (&list, 1);
4658 initialized_p = 1;
4659 }
4660
22eed1e6
APB
4661 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4662 {
4663 if (TREE_VALUE (c) == meth)
4664 {
4665 char *t;
4666 if (list)
4667 {
4668 tree liste;
4669 list = nreverse (list);
4670 for (liste = list; liste; liste = TREE_CHAIN (liste))
4671 {
4672 parse_error_context
c63b98cd 4673 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4674 constructor_circularity_msg
4675 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4676 java_error_count--;
4677 }
4678 }
c2e3db92 4679 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4680 parse_error_context (TREE_PURPOSE (c),
4681 "%s: recursive invocation of constructor `%s'",
4682 constructor_circularity_msg (current, meth), t);
4683 free (t);
4684 list = NULL_TREE;
4685 return 1;
4686 }
4687 }
4688 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4689 {
4690 list = tree_cons (c, current, list);
4691 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4692 return 1;
4693 list = TREE_CHAIN (list);
4694 }
4695 return 0;
4696}
4697
e04a16fb
AG
4698/* Check modifiers that can be declared but exclusively */
4699
4700static void
4701check_modifiers_consistency (flags)
4702 int flags;
4703{
4704 int acc_count = 0;
4705 tree cl = NULL_TREE;
4706
e0fc4118
TT
4707 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4708 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4709 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4710 if (acc_count > 1)
4711 parse_error_context
e0fc4118
TT
4712 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4713
4714 acc_count = 0;
4715 cl = NULL_TREE;
14d075d8
TT
4716 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4717 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4718 if (acc_count > 1)
4719 parse_error_context (cl,
4720 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4721}
4722
4723/* Check the methode header METH for abstract specifics features */
4724
4725static void
4726check_abstract_method_header (meth)
4727 tree meth;
4728{
4729 int flags = get_access_flags_from_decl (meth);
4730 /* DECL_NAME might still be a WFL node */
c877974e 4731 tree name = GET_METHOD_NAME (meth);
e04a16fb 4732
2884c41e
KG
4733 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4734 ACC_ABSTRACT, "abstract method",
4735 IDENTIFIER_POINTER (name));
4736 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4737 ACC_PUBLIC, "abstract method",
4738 IDENTIFIER_POINTER (name));
e04a16fb
AG
4739
4740 check_modifiers ("Illegal modifier `%s' for interface method",
4741 flags, INTERFACE_METHOD_MODIFIERS);
4742}
4743
4744/* Create a FUNCTION_TYPE node and start augmenting it with the
4745 declared function arguments. Arguments type that can't be resolved
4746 are left as they are, but the returned node is marked as containing
4747 incomplete types. */
4748
4749static tree
4750method_declarator (id, list)
4751 tree id, list;
4752{
4753 tree arg_types = NULL_TREE, current, node;
4754 tree meth = make_node (FUNCTION_TYPE);
4755 jdep *jdep;
e04a16fb
AG
4756
4757 patch_stage = JDEP_NO_PATCH;
c2952b01 4758
34d4df06
APB
4759 if (GET_CPC () == error_mark_node)
4760 return error_mark_node;
4761
c2952b01
APB
4762 /* If we're dealing with an inner class constructor, we hide the
4763 this$<n> decl in the name field of its parameter declaration. We
4764 also might have to hide the outer context local alias
4765 initializers. Not done when the class is a toplevel class. */
4766 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4767 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4768 {
4769 tree aliases_list, type, thisn;
4770 /* First the aliases, linked to the regular parameters */
4771 aliases_list =
4772 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4773 TREE_TYPE (GET_CPC ()),
4774 NULL_TREE, NULL);
4775 list = chainon (nreverse (aliases_list), list);
4776
4777 /* Then this$<n> */
4778 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4779 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4780 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4781 list);
4782 }
e04a16fb
AG
4783
4784 for (current = list; current; current = TREE_CHAIN (current))
4785 {
c583dd46 4786 int must_chain = 0;
e04a16fb
AG
4787 tree wfl_name = TREE_PURPOSE (current);
4788 tree type = TREE_VALUE (current);
4789 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4790 tree already, arg_node;
4791 tree type_wfl = NULL_TREE;
23a79c61 4792 tree real_type;
c583dd46
APB
4793
4794 /* Obtain a suitable type for resolution, if necessary */
4795 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4796
4797 /* Process NAME, as it may specify extra dimension(s) for it */
4798 type = build_array_from_name (type, type_wfl, name, &name);
4799 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4800
23a79c61
APB
4801 real_type = GET_REAL_TYPE (type);
4802 if (TREE_CODE (real_type) == RECORD_TYPE)
4803 {
4804 real_type = promote_type (real_type);
4805 if (TREE_CODE (type) == TREE_LIST)
4806 TREE_PURPOSE (type) = real_type;
4807 }
5e942c50 4808
e04a16fb
AG
4809 /* Check redefinition */
4810 for (already = arg_types; already; already = TREE_CHAIN (already))
4811 if (TREE_PURPOSE (already) == name)
4812 {
781b0558
KG
4813 parse_error_context
4814 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4815 IDENTIFIER_POINTER (name),
e04a16fb
AG
4816 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4817 break;
4818 }
4819
4820 /* If we've an incomplete argument type, we know there is a location
4821 to patch when the type get resolved, later. */
4822 jdep = NULL;
c583dd46 4823 if (must_chain)
e04a16fb 4824 {
c583dd46
APB
4825 patch_stage = JDEP_METHOD;
4826 type = register_incomplete_type (patch_stage,
4827 type_wfl, wfl_name, type);
4828 jdep = CLASSD_LAST (ctxp->classd_list);
4829 JDEP_MISC (jdep) = id;
e04a16fb 4830 }
c583dd46 4831
c2952b01 4832 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4833 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4834 /* Remeber arguments declared final. */
4835 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4836
e04a16fb
AG
4837 if (jdep)
4838 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4839 TREE_CHAIN (arg_node) = arg_types;
4840 arg_types = arg_node;
4841 }
de4c7b02 4842 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4843 node = build_tree_list (id, meth);
4844 return node;
4845}
4846
4847static int
4848unresolved_type_p (wfl, returned)
4849 tree wfl;
4850 tree *returned;
4851
4852{
4853 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4854 {
e04a16fb 4855 if (returned)
165f37bc
APB
4856 {
4857 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4858 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4859 *returned = TREE_TYPE (decl);
4860 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4861 *returned = TREE_TYPE (GET_CPC ());
4862 else
4863 *returned = NULL_TREE;
4864 }
e04a16fb
AG
4865 return 1;
4866 }
4867 if (returned)
4868 *returned = wfl;
4869 return 0;
4870}
4871
4872/* From NAME, build a qualified identifier node using the
4873 qualification from the current package definition. */
4874
4875static tree
98a52c2c 4876parser_qualified_classname (name)
e04a16fb
AG
4877 tree name;
4878{
c2952b01
APB
4879 tree nested_class_name;
4880
98a52c2c 4881 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4882 return nested_class_name;
4883
e04a16fb 4884 if (ctxp->package)
c2952b01 4885 return merge_qualified_name (ctxp->package, name);
e04a16fb 4886 else
c2952b01 4887 return name;
e04a16fb
AG
4888}
4889
4890/* Called once the type a interface extends is resolved. Returns 0 if
4891 everything is OK. */
4892
4893static int
4894parser_check_super_interface (super_decl, this_decl, this_wfl)
4895 tree super_decl, this_decl, this_wfl;
4896{
4897 tree super_type = TREE_TYPE (super_decl);
4898
4899 /* Has to be an interface */
c2952b01 4900 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4901 {
4902 parse_error_context
4903 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4904 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4905 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4906 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4907 "interface" : "class"),
4908 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4909 return 1;
4910 }
4911
4912 /* Check scope: same package OK, other package: OK if public */
4913 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4914 return 1;
4915
4916 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4917 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4918 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4919 return 0;
4920}
4921
4922/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4923 0 if everthing is OK. */
4924
4925static int
4926parser_check_super (super_decl, this_decl, wfl)
4927 tree super_decl, this_decl, wfl;
4928{
e04a16fb
AG
4929 tree super_type = TREE_TYPE (super_decl);
4930
4931 /* SUPER should be a CLASS (neither an array nor an interface) */
4932 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4933 {
4934 parse_error_context
4935 (wfl, "Class `%s' can't subclass %s `%s'",
4936 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4937 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4938 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4939 return 1;
4940 }
4941
4942 if (CLASS_FINAL (TYPE_NAME (super_type)))
4943 {
4944 parse_error_context (wfl, "Can't subclass final classes: %s",
4945 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4946 return 1;
4947 }
4948
4949 /* Check scope: same package OK, other package: OK if public */
4950 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4951 return 1;
4952
4953 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4954 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4955 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4956 return 0;
4957}
4958
4959/* Create a new dependency list and link it (in a LIFO manner) to the
4960 CTXP list of type dependency list. */
4961
4962static void
4963create_jdep_list (ctxp)
4964 struct parser_ctxt *ctxp;
4965{
23a79c61 4966 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4967 new->first = new->last = NULL;
4968 new->next = ctxp->classd_list;
4969 ctxp->classd_list = new;
4970}
4971
4972static jdeplist *
4973reverse_jdep_list (ctxp)
4974 struct parser_ctxt *ctxp;
4975{
4976 register jdeplist *prev = NULL, *current, *next;
4977 for (current = ctxp->classd_list; current; current = next)
4978 {
4979 next = current->next;
4980 current->next = prev;
4981 prev = current;
4982 }
4983 return prev;
4984}
4985
23a79c61
APB
4986/* Create a fake pointer based on the ID stored in
4987 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4988 registered again. */
e04a16fb
AG
4989
4990static tree
23a79c61
APB
4991obtain_incomplete_type (type_name)
4992 tree type_name;
e04a16fb 4993{
23a79c61
APB
4994 tree ptr, name;
4995
4996 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4997 name = EXPR_WFL_NODE (type_name);
4998 else if (INCOMPLETE_TYPE_P (type_name))
4999 name = TYPE_NAME (type_name);
5000 else
5001 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
5002
5003 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 5004 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
5005 break;
5006
5007 if (!ptr)
5008 {
78d21f92
PB
5009 BUILD_PTR_FROM_NAME (ptr, name);
5010 layout_type (ptr);
e04a16fb
AG
5011 TREE_CHAIN (ptr) = ctxp->incomplete_class;
5012 ctxp->incomplete_class = ptr;
5013 }
5014
5015 return ptr;
5016}
5017
5018/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5019 non NULL instead of computing a new fake type based on WFL. The new
5020 dependency is inserted in the current type dependency list, in FIFO
5021 manner. */
5022
5023static tree
5024register_incomplete_type (kind, wfl, decl, ptr)
5025 int kind;
5026 tree wfl, decl, ptr;
5027{
23a79c61 5028 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5029
e04a16fb
AG
5030 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5031 ptr = obtain_incomplete_type (wfl);
5032
5033 JDEP_KIND (new) = kind;
5034 JDEP_DECL (new) = decl;
5035 JDEP_SOLV (new) = ptr;
5036 JDEP_WFL (new) = wfl;
5037 JDEP_CHAIN (new) = NULL;
5038 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5039 /* For some dependencies, set the enclosing class of the current
5040 class to be the enclosing context */
8ac1de05
APB
5041 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5042 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5043 && GET_ENCLOSING_CPC ())
5044 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5045 else
324ed8fd 5046 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5047 JDEP_GET_PATCH (new) = (tree *)NULL;
5048
5049 JDEP_INSERT (ctxp->classd_list, new);
5050
5051 return ptr;
5052}
5053
5054void
5055java_check_circular_reference ()
5056{
5057 tree current;
5058 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5059 {
5060 tree type = TREE_TYPE (current);
e920ebc9 5061 if (CLASS_INTERFACE (current))
e04a16fb
AG
5062 {
5063 /* Check all interfaces this class extends */
5064 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5065 int n, i;
5066
5067 if (!basetype_vec)
5068 return;
5069 n = TREE_VEC_LENGTH (basetype_vec);
5070 for (i = 0; i < n; i++)
5071 {
5072 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5073 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5074 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5075 parse_error_context (lookup_cl (current),
5076 "Cyclic interface inheritance");
5077 }
5078 }
5079 else
5080 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5081 parse_error_context (lookup_cl (current),
c2952b01
APB
5082 "Cyclic class inheritance%s",
5083 (cyclic_inheritance_report ?
5084 cyclic_inheritance_report : ""));
5085 }
5086}
5087
5088/* Augment the parameter list PARM with parameters crafted to
5089 initialize outer context locals aliases. Through ARTIFICIAL, a
5090 count is kept of the number of crafted parameters. MODE governs
5091 what eventually gets created: something suitable for a function
5092 creation or a function invocation, either the constructor or
c00f0fb2 5093 finit$. */
c2952b01
APB
5094
5095static tree
5096build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5097 int mode;
5098 tree class_type, parm;
5099 int *artificial;
5100{
5101 tree field;
da632f2c
APB
5102 tree additional_parms = NULL_TREE;
5103
c2952b01
APB
5104 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5105 if (FIELD_LOCAL_ALIAS (field))
5106 {
63ad61ed 5107 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5108 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5109 tree mangled_id;
c2952b01
APB
5110
5111 switch (mode)
5112 {
5113 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5114 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5115 &buffer [4]);
5116 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5117 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5118 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5119 else
5120 value = TREE_TYPE (field);
5121 break;
5122
5123 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5124 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5125 &buffer [4]);
c2952b01
APB
5126 value = TREE_TYPE (field);
5127 break;
5128
5129 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5130 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5131 &buffer [4]);
c2952b01
APB
5132 /* Now, this is wrong. purpose should always be the NAME
5133 of something and value its matching value (decl, type,
5134 etc...) FIXME -- but there is a lot to fix. */
5135
5136 /* When invoked for this kind of operation, we already
5137 know whether a field is used or not. */
5138 purpose = TREE_TYPE (field);
1f8f4a0b 5139 value = build_wfl_node (mangled_id);
c2952b01
APB
5140 break;
5141
5142 case AIPL_FUNCTION_CTOR_INVOCATION:
5143 /* There are two case: the constructor invokation happends
5144 outside the local inner, in which case, locales from the outer
5145 context are directly used.
5146
5147 Otherwise, we fold to using the alias directly. */
5148 if (class_type == current_class)
5149 value = field;
5150 else
5151 {
5152 name = get_identifier (&buffer[4]);
5153 value = IDENTIFIER_LOCAL_VALUE (name);
5154 }
5155 break;
5156 }
da632f2c 5157 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5158 if (artificial)
5159 *artificial +=1;
5160 }
da632f2c
APB
5161 if (additional_parms)
5162 {
5163 if (ANONYMOUS_CLASS_P (class_type)
5164 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5165 additional_parms = nreverse (additional_parms);
5166 parm = chainon (additional_parms, parm);
5167 }
5168
5169 return parm;
c2952b01
APB
5170}
5171
5172/* Craft a constructor for CLASS_DECL -- what we should do when none
5173 where found. ARGS is non NULL when a special signature must be
5174 enforced. This is the case for anonymous classes. */
5175
5176static void
5177craft_constructor (class_decl, args)
5178 tree class_decl, args;
5179{
5180 tree class_type = TREE_TYPE (class_decl);
5181 tree parm = NULL_TREE;
5182 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5183 ACC_PUBLIC : 0);
5184 int i = 0, artificial = 0;
5185 tree decl, ctor_name;
5186 char buffer [80];
5187
c2952b01
APB
5188 /* The constructor name is <init> unless we're dealing with an
5189 anonymous class, in which case the name will be fixed after having
5190 be expanded. */
5191 if (ANONYMOUS_CLASS_P (class_type))
5192 ctor_name = DECL_NAME (class_decl);
5193 else
5194 ctor_name = init_identifier_node;
5195
5196 /* If we're dealing with an inner class constructor, we hide the
5197 this$<n> decl in the name field of its parameter declaration. */
5198 if (PURE_INNER_CLASS_TYPE_P (class_type))
5199 {
5200 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5201 parm = tree_cons (build_current_thisn (class_type),
5202 build_pointer_type (type), parm);
5203
5204 /* Some more arguments to be hidden here. The values of the local
5205 variables of the outer context that the inner class needs to see. */
5206 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5207 class_type, parm,
5208 &artificial);
5209 }
5210
5211 /* Then if there are any args to be enforced, enforce them now */
5212 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5213 {
5214 sprintf (buffer, "parm%d", i++);
5215 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5216 }
c2952b01
APB
5217
5218 CRAFTED_PARAM_LIST_FIXUP (parm);
5219 decl = create_artificial_method (class_type, flags, void_type_node,
5220 ctor_name, parm);
5221 fix_method_argument_names (parm, decl);
5222 /* Now, mark the artificial parameters. */
5223 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5224 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5225}
5226
c2952b01 5227
e920ebc9
APB
5228/* Fix the constructors. This will be called right after circular
5229 references have been checked. It is necessary to fix constructors
5230 early even if no code generation will take place for that class:
5231 some generated constructor might be required by the class whose
5232 compilation triggered this one to be simply loaded. */
5233
5234void
5235java_fix_constructors ()
5236{
5237 tree current;
5238
5239 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5240 {
e920ebc9
APB
5241 tree class_type = TREE_TYPE (current);
5242 int saw_ctor = 0;
c2952b01
APB
5243 tree decl;
5244
5245 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5246 continue;
e920ebc9
APB
5247
5248 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5249 {
5250 if (DECL_CONSTRUCTOR_P (decl))
5251 {
5252 fix_constructors (decl);
5253 saw_ctor = 1;
5254 }
5255 }
5256
c2952b01
APB
5257 /* Anonymous class constructor can't be generated that early. */
5258 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5259 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5260 }
5261}
5262
23a79c61
APB
5263/* safe_layout_class just makes sure that we can load a class without
5264 disrupting the current_class, input_file, lineno, etc, information
5265 about the class processed currently. */
5266
e04a16fb
AG
5267void
5268safe_layout_class (class)
5269 tree class;
5270{
5271 tree save_current_class = current_class;
3b304f5b 5272 const char *save_input_filename = input_filename;
e04a16fb 5273 int save_lineno = lineno;
5e942c50 5274
e04a16fb 5275 layout_class (class);
5e942c50 5276
e04a16fb
AG
5277 current_class = save_current_class;
5278 input_filename = save_input_filename;
5279 lineno = save_lineno;
5280 CLASS_LOADED_P (class) = 1;
5281}
5282
5283static tree
5284jdep_resolve_class (dep)
5285 jdep *dep;
5286{
5287 tree decl;
5288
23a79c61
APB
5289 if (JDEP_RESOLVED_P (dep))
5290 decl = JDEP_RESOLVED_DECL (dep);
5291 else
e04a16fb 5292 {
c2952b01 5293 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5294 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5295 JDEP_RESOLVED (dep, decl);
5296 }
23a79c61 5297
e04a16fb 5298 if (!decl)
23a79c61 5299 complete_class_report_errors (dep);
1e12ab9b 5300 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5301 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5302 return decl;
5303}
5304
5305/* Complete unsatisfied class declaration and their dependencies */
5306
5307void
5308java_complete_class ()
5309{
e04a16fb
AG
5310 tree cclass;
5311 jdeplist *cclassd;
5312 int error_found;
b67d701b 5313 tree type;
e04a16fb 5314
9a7ab4b3 5315 /* Process imports */
e04a16fb 5316 process_imports ();
e04a16fb
AG
5317
5318 /* Rever things so we have the right order */
5319 ctxp->class_list = nreverse (ctxp->class_list);
5320 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5321
e04a16fb
AG
5322 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5323 cclass && cclassd;
5324 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5325 {
5326 jdep *dep;
5327 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5328 {
5329 tree decl;
e04a16fb
AG
5330 if (!(decl = jdep_resolve_class (dep)))
5331 continue;
5332
5333 /* Now it's time to patch */
5334 switch (JDEP_KIND (dep))
5335 {
5336 case JDEP_SUPER:
5337 /* Simply patch super */
5338 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5339 continue;
5340 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5341 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5342 break;
5343
5344 case JDEP_FIELD:
5345 {
5346 /* We do part of the job done in add_field */
5347 tree field_decl = JDEP_DECL (dep);
5348 tree field_type = TREE_TYPE (decl);
e04a16fb 5349 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5350 field_type = promote_type (field_type);
e04a16fb 5351 TREE_TYPE (field_decl) = field_type;
5e942c50 5352 DECL_ALIGN (field_decl) = 0;
11cf4d18 5353 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5354 layout_decl (field_decl, 0);
e04a16fb
AG
5355 SOURCE_FRONTEND_DEBUG
5356 (("Completed field/var decl `%s' with `%s'",
5357 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5358 IDENTIFIER_POINTER (DECL_NAME (decl))));
5359 break;
5360 }
5361 case JDEP_METHOD: /* We start patching a method */
5362 case JDEP_METHOD_RETURN:
5363 error_found = 0;
5364 while (1)
5365 {
5366 if (decl)
5367 {
b67d701b
PB
5368 type = TREE_TYPE(decl);
5369 if (TREE_CODE (type) == RECORD_TYPE)
5370 type = promote_type (type);
e04a16fb
AG
5371 JDEP_APPLY_PATCH (dep, type);
5372 SOURCE_FRONTEND_DEBUG
5373 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5374 "Completing fct `%s' with ret type `%s'":
5375 "Completing arg `%s' with type `%s'"),
5376 IDENTIFIER_POINTER (EXPR_WFL_NODE
5377 (JDEP_DECL_WFL (dep))),
5378 IDENTIFIER_POINTER (DECL_NAME (decl))));
5379 }
5380 else
5381 error_found = 1;
5382 dep = JDEP_CHAIN (dep);
5383 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5384 break;
5385 else
5386 decl = jdep_resolve_class (dep);
5387 }
5388 if (!error_found)
5389 {
5390 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5391 /* Recompute and reset the signature, check first that
5392 all types are now defined. If they're not,
5393 dont build the signature. */
5394 if (check_method_types_complete (mdecl))
5395 {
5396 signature = build_java_signature (TREE_TYPE (mdecl));
5397 set_java_signature (TREE_TYPE (mdecl), signature);
5398 }
e04a16fb
AG
5399 }
5400 else
5401 continue;
5402 break;
5403
5404 case JDEP_INTERFACE:
5405 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5406 JDEP_WFL (dep)))
5407 continue;
5408 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5409 break;
5410
b67d701b 5411 case JDEP_PARM:
e04a16fb 5412 case JDEP_VARIABLE:
b67d701b
PB
5413 type = TREE_TYPE(decl);
5414 if (TREE_CODE (type) == RECORD_TYPE)
5415 type = promote_type (type);
5416 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5417 break;
5418
5419 case JDEP_TYPE:
5420 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5421 SOURCE_FRONTEND_DEBUG
5422 (("Completing a random type dependency on a '%s' node",
5423 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5424 break;
5425
b9f7e36c 5426 case JDEP_EXCEPTION:
c877974e
APB
5427 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5428 SOURCE_FRONTEND_DEBUG
5429 (("Completing `%s' `throws' argument node",
5430 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5431 break;
5432
c2952b01
APB
5433 case JDEP_ANONYMOUS:
5434 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5435 break;
5436
e04a16fb 5437 default:
0a2138e2
APB
5438 fatal ("Can't handle patch code %d - java_complete_class",
5439 JDEP_KIND (dep));
e04a16fb
AG
5440 }
5441 }
5442 }
e04a16fb
AG
5443 return;
5444}
5445
5446/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5447 array. */
5448
5449static tree
c2952b01
APB
5450resolve_class (enclosing, class_type, decl, cl)
5451 tree enclosing, class_type, decl, cl;
e04a16fb 5452{
49f48c71
KG
5453 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5454 const char *base = name;
78d21f92
PB
5455 tree resolved_type = TREE_TYPE (class_type);
5456 tree resolved_type_decl;
e04a16fb 5457
78d21f92
PB
5458 if (resolved_type != NULL_TREE)
5459 {
5460 tree resolved_type_decl = TYPE_NAME (resolved_type);
5461 if (resolved_type_decl == NULL_TREE
5462 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5463 {
5464 resolved_type_decl = build_decl (TYPE_DECL,
5465 TYPE_NAME (class_type),
5466 resolved_type);
5467 }
5468 return resolved_type_decl;
5469 }
5470
e04a16fb
AG
5471 /* 1- Check to see if we have an array. If true, find what we really
5472 want to resolve */
5473 while (name[0] == '[')
5474 name++;
5475 if (base != name)
34d4df06
APB
5476 {
5477 TYPE_NAME (class_type) = get_identifier (name);
5478 WFL_STRIP_BRACKET (cl, cl);
5479 }
e04a16fb
AG
5480
5481 /* 2- Resolve the bare type */
c2952b01
APB
5482 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5483 decl, cl)))
e04a16fb
AG
5484 return NULL_TREE;
5485 resolved_type = TREE_TYPE (resolved_type_decl);
5486
5487 /* 3- If we have and array, reconstruct the array down to its nesting */
5488 if (base != name)
5489 {
5490 while (base != name)
5491 {
5492 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5493 resolved_type = promote_type (resolved_type);
5494 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5495 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5496 name--;
5497 }
5498 /* Build a fake decl for this, since this is what is expected to
5499 be returned. */
5500 resolved_type_decl =
5501 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5502 /* Figure how those two things are important for error report. FIXME */
5503 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5504 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5505 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5506 }
78d21f92 5507 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5508 return resolved_type_decl;
5509}
5510
5511/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5512 are used to report error messages. */
5513
78d21f92 5514tree
c2952b01
APB
5515do_resolve_class (enclosing, class_type, decl, cl)
5516 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5517{
5518 tree new_class_decl;
e04a16fb
AG
5519
5520 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5521 it is changed by find_in_imports{_on_demand} and (but it doesn't
5522 really matter) qualify_and_find */
e04a16fb 5523
c2952b01
APB
5524 /* 0- Search in the current class as an inner class */
5525
5526 /* Maybe some code here should be added to load the class or
5527 something, at least if the class isn't an inner class and ended
5528 being loaded from class file. FIXME. */
a40d21da
APB
5529 while (enclosing)
5530 {
5531 tree name;
e1d565ab 5532 tree intermediate;
a40d21da
APB
5533
5534 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5535 return new_class_decl;
8ac1de05 5536
e1d565ab 5537 intermediate = enclosing;
0c2b8145 5538 /* Explore enclosing contexts. */
e1d565ab 5539 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5540 {
e1d565ab
BM
5541 intermediate = DECL_CONTEXT (intermediate);
5542 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5543 class_type, cl)))
5544 return new_class_decl;
5545 }
5546
a40d21da
APB
5547 /* Now go to the upper classes, bail out if necessary. */
5548 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5549 if (!enclosing || enclosing == object_type_node)
5550 break;
5551
5552 if (TREE_CODE (enclosing) == RECORD_TYPE)
5553 {
5554 enclosing = TYPE_NAME (enclosing);
5555 continue;
5556 }
5557
5558 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5559 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5560 else
5561 name = enclosing;
5562 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5563 }
c2952b01 5564
9a7ab4b3
APB
5565 /* 1- Check for the type in single imports. This will change
5566 TYPE_NAME() if something relevant is found */
5567 find_in_imports (class_type);
e04a16fb 5568
9a7ab4b3 5569 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5570 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5571 {
5572 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5573 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5574 load_class (TYPE_NAME (class_type), 0);
5575 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5576 }
5577
9a7ab4b3
APB
5578 /* 3- Search according to the current package definition */
5579 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5580 {
5581 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5582 TYPE_NAME (class_type))))
5583 return new_class_decl;
5584 }
5585
5586 /* 4- Check the import on demands. Don't allow bar.baz to be
5587 imported from foo.* */
5588 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5589 if (find_in_imports_on_demand (class_type))
5590 return NULL_TREE;
5591
5592 /* If found in find_in_imports_on_demant, the type has already been
5593 loaded. */
5594 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5595 return new_class_decl;
5596
5597 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5598 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5599 {
ee07f4f4 5600 tree package;
d6baf6f5
APB
5601
5602 /* If there is a current package (ctxp->package), it's the first
5603 element of package_list and we can skip it. */
5604 for (package = (ctxp->package ?
5605 TREE_CHAIN (package_list) : package_list);
5606 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5607 if ((new_class_decl = qualify_and_find (class_type,
5608 TREE_PURPOSE (package),
5609 TYPE_NAME (class_type))))
5610 return new_class_decl;
5611 }
5612
5613 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5614 load_class (TYPE_NAME (class_type), 0);
5615 if (check_pkg_class_access (TYPE_NAME (class_type),
5616 (cl ? cl : lookup_cl (decl))))
5617 return NULL_TREE;
5618
9a7ab4b3 5619 /* 6- Last call for a resolution */
e04a16fb
AG
5620 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5621}
5622
9a7ab4b3
APB
5623static tree
5624qualify_and_find (class_type, package, name)
5625 tree class_type, package, name;
5626{
5627 tree new_qualified = merge_qualified_name (package, name);
5628 tree new_class_decl;
5629
5630 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5631 load_class (new_qualified, 0);
5632 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5633 {
5634 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5635 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5636 load_class (new_qualified, 0);
5637 TYPE_NAME (class_type) = new_qualified;
5638 return IDENTIFIER_CLASS_VALUE (new_qualified);
5639 }
5640 return NULL_TREE;
5641}
5642
e04a16fb 5643/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5644 parsed class). Return a decl node. This function is meant to be
5645 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5646
5647static tree
c877974e
APB
5648resolve_and_layout (something, cl)
5649 tree something;
e04a16fb
AG
5650 tree cl;
5651{
34d4df06 5652 tree decl, decl_type;
c877974e 5653
23a79c61
APB
5654 /* Don't do that on the current class */
5655 if (something == current_class)
5656 return TYPE_NAME (current_class);
c877974e 5657
23a79c61 5658 /* Don't do anything for void and other primitive types */
c877974e
APB
5659 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5660 return NULL_TREE;
5661
23a79c61
APB
5662 /* Pointer types can be reall pointer types or fake pointers. When
5663 finding a real pointer, recheck for primitive types */
5664 if (TREE_CODE (something) == POINTER_TYPE)
5665 {
5666 if (TREE_TYPE (something))
5667 {
5668 something = TREE_TYPE (something);
5669 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5670 return NULL_TREE;
5671 }
5672 else
5673 something = TYPE_NAME (something);
5674 }
5675
5676 /* Don't do anything for arrays of primitive types */
5677 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5678 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5679 return NULL_TREE;
5680
c2952b01
APB
5681 /* Something might be a WFL */
5682 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5683 something = EXPR_WFL_NODE (something);
5684
5685 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5686 TYPE_DECL or a real TYPE */
5687 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5688 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5689 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5690
23a79c61
APB
5691 if (!(decl = resolve_no_layout (something, cl)))
5692 return NULL_TREE;
5693
5694 /* Resolve and layout if necessary */
34d4df06
APB
5695 decl_type = TREE_TYPE (decl);
5696 layout_class_methods (decl_type);
5697 /* Check methods */
5698 if (CLASS_FROM_SOURCE_P (decl_type))
5699 java_check_methods (decl);
5700 /* Layout the type if necessary */
5701 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5702 safe_layout_class (decl_type);
23a79c61 5703
e04a16fb
AG
5704 return decl;
5705}
5706
5707/* Resolve a class, returns its decl but doesn't perform any
5708 layout. The current parsing context is saved and restored */
5709
5710static tree
5711resolve_no_layout (name, cl)
5712 tree name, cl;
5713{
5714 tree ptr, decl;
5715 BUILD_PTR_FROM_NAME (ptr, name);
5716 java_parser_context_save_global ();
c2952b01 5717 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5718 java_parser_context_restore_global ();
5719
5720 return decl;
5721}
5722
23a79c61
APB
5723/* Called when reporting errors. Skip leader '[' in a complex array
5724 type description that failed to be resolved. */
e04a16fb 5725
49f48c71 5726static const char *
e04a16fb 5727purify_type_name (name)
49f48c71 5728 const char *name;
e04a16fb
AG
5729{
5730 while (*name && *name == '[')
5731 name++;
5732 return name;
5733}
5734
5735/* The type CURRENT refers to can't be found. We print error messages. */
5736
5737static void
5738complete_class_report_errors (dep)
5739 jdep *dep;
5740{
49f48c71 5741 const char *name;
23a79c61
APB
5742
5743 if (!JDEP_WFL (dep))
5744 return;
5745
5746 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5747 switch (JDEP_KIND (dep))
5748 {
5749 case JDEP_SUPER:
5750 parse_error_context
5751 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5752 purify_type_name (name),
e04a16fb
AG
5753 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5754 break;
5755 case JDEP_FIELD:
5756 parse_error_context
5757 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5758 purify_type_name (name),
e04a16fb
AG
5759 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5760 break;
5761 case JDEP_METHOD: /* Covers arguments */
5762 parse_error_context
781b0558 5763 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5764 purify_type_name (name),
e04a16fb
AG
5765 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5766 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5767 break;
5768 case JDEP_METHOD_RETURN: /* Covers return type */
5769 parse_error_context
781b0558 5770 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5771 purify_type_name (name),
e04a16fb
AG
5772 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5773 break;
5774 case JDEP_INTERFACE:
5775 parse_error_context
5776 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5777 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5778 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5779 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5780 break;
5781 case JDEP_VARIABLE:
5782 parse_error_context
781b0558 5783 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5784 purify_type_name (IDENTIFIER_POINTER
5785 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5786 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5787 break;
b9f7e36c
APB
5788 case JDEP_EXCEPTION: /* As specified by `throws' */
5789 parse_error_context
5790 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5791 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5792 break;
0a2138e2
APB
5793 default:
5794 /* Fix for -Wall. Just break doing nothing. The error will be
5795 caught later */
5796 break;
e04a16fb
AG
5797 }
5798}
5799
22eed1e6
APB
5800/* Return a static string containing the DECL prototype string. If
5801 DECL is a constructor, use the class name instead of the form
5802 <init> */
5803
49f48c71 5804static const char *
22eed1e6
APB
5805get_printable_method_name (decl)
5806 tree decl;
5807{
49f48c71 5808 const char *to_return;
9ee9b555 5809 tree name = NULL_TREE;
22eed1e6
APB
5810
5811 if (DECL_CONSTRUCTOR_P (decl))
5812 {
5813 name = DECL_NAME (decl);
5e942c50 5814 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5815 }
5816
5817 to_return = lang_printable_name (decl, 0);
5818 if (DECL_CONSTRUCTOR_P (decl))
5819 DECL_NAME (decl) = name;
5820
5821 return to_return;
5822}
5823
5e942c50
APB
5824/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5825 nevertheless needs to be verfied, 1 otherwise. */
5826
5827static int
5828reset_method_name (method)
5829 tree method;
5830{
c2952b01 5831 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5832 {
5833 /* NAME is just the plain name when Object is being defined */
5834 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5835 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5836 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5837 return 0;
5838 }
5839 else
5840 return 1;
5841}
5842
c877974e
APB
5843/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5844
5845tree
5846java_get_real_method_name (method_decl)
5847 tree method_decl;
5848{
5849 tree method_name = DECL_NAME (method_decl);
5850 if (DECL_CONSTRUCTOR_P (method_decl))
5851 return init_identifier_node;
82371d41
APB
5852
5853 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5854 and still can be a constructor. FIXME */
5855
23a79c61
APB
5856 /* Don't confuse method only bearing the name of their class as
5857 constructors */
82371d41
APB
5858 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5859 && ctxp
c2952b01 5860 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5861 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5862 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5863 return init_identifier_node;
5864 else
5865 return EXPR_WFL_NODE (method_name);
5866}
5867
22eed1e6
APB
5868/* Track method being redefined inside the same class. As a side
5869 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5870 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5871
e04a16fb
AG
5872static int
5873check_method_redefinition (class, method)
5874 tree class, method;
5875{
5876 tree redef, name;
5877 tree cl = DECL_NAME (method);
c3f2a476 5878 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5879 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5880 fixed and checked */
5e942c50
APB
5881
5882 /* Reset the method name before running the check. If it returns 1,
5883 the method doesn't need to be verified with respect to method
5884 redeclaration and we return 0 */
5885 if (reset_method_name (method))
e04a16fb 5886 return 0;
5e942c50
APB
5887
5888 name = DECL_NAME (method);
e04a16fb
AG
5889 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5890 {
c3f2a476 5891 if (redef == method)
e04a16fb 5892 break;
c3f2a476
APB
5893 if (DECL_NAME (redef) == name
5894 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5895 {
22eed1e6
APB
5896 parse_error_context
5897 (cl, "Duplicate %s declaration `%s'",
5898 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5899 get_printable_method_name (redef));
e04a16fb
AG
5900 return 1;
5901 }
5902 }
5903 return 0;
5904}
5905
1175b9b4
TT
5906/* Return 1 if check went ok, 0 otherwise. */
5907static int
d77613be
APB
5908check_abstract_method_definitions (do_interface, class_decl, type)
5909 int do_interface;
5910 tree class_decl, type;
5911{
5912 tree class = TREE_TYPE (class_decl);
5913 tree method, end_type;
1175b9b4 5914 int ok = 1;
d77613be
APB
5915
5916 end_type = (do_interface ? object_type_node : type);
5917 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5918 {
5919 tree other_super, other_method, method_sig, method_name;
5920 int found = 0;
165f37bc 5921 int end_type_reached = 0;
d77613be
APB
5922
5923 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5924 continue;
5925
5926 /* Now verify that somewhere in between TYPE and CLASS,
5927 abstract method METHOD gets a non abstract definition
5928 that is inherited by CLASS. */
5929
5930 method_sig = build_java_signature (TREE_TYPE (method));
5931 method_name = DECL_NAME (method);
5932 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5933 method_name = EXPR_WFL_NODE (method_name);
5934
165f37bc
APB
5935 other_super = class;
5936 do {
5937 if (other_super == end_type)
5938 end_type_reached = 1;
5939
5940 /* Method search */
5941 for (other_method = TYPE_METHODS (other_super); other_method;
5942 other_method = TREE_CHAIN (other_method))
5943 {
5944 tree s = build_java_signature (TREE_TYPE (other_method));
5945 tree other_name = DECL_NAME (other_method);
5946
5947 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5948 other_name = EXPR_WFL_NODE (other_name);
5949 if (!DECL_CLINIT_P (other_method)
5950 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5951 && method_name == other_name
5952 && method_sig == s
5953 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5954 {
5955 found = 1;
5956 break;
5957 }
5958 }
5959 other_super = CLASSTYPE_SUPER (other_super);
5960 } while (!end_type_reached);
5961
d77613be
APB
5962 /* Report that abstract METHOD didn't find an implementation
5963 that CLASS can use. */
5964 if (!found)
5965 {
c2e3db92 5966 char *t = xstrdup (lang_printable_name
d77613be
APB
5967 (TREE_TYPE (TREE_TYPE (method)), 0));
5968 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5969 tree saved_wfl = NULL_TREE;
5970
5971 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5972 {
5973 saved_wfl = DECL_NAME (method);
5974 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5975 }
5976
5977 parse_error_context
5978 (lookup_cl (class_decl),
781b0558 5979 "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
5980 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5981 t, lang_printable_name (method, 0),
5982 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5983 "interface" : "class"),
5984 IDENTIFIER_POINTER (ccn),
5985 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5986 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5987 ok = 0;
d77613be 5988 free (t);
1175b9b4 5989
d77613be
APB
5990 if (saved_wfl)
5991 DECL_NAME (method) = saved_wfl;
5992 }
5993 }
1175b9b4
TT
5994
5995 if (ok && do_interface)
5996 {
5997 /* Check for implemented interfaces. */
5998 int i;
5999 tree vector = TYPE_BINFO_BASETYPES (type);
6000 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6001 {
6002 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6003 ok = check_abstract_method_definitions (1, class_decl, super);
6004 }
6005 }
6006
6007 return ok;
d77613be
APB
6008}
6009
614eaae0 6010/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
6011 methods. */
6012
6013static void
6014java_check_abstract_method_definitions (class_decl)
6015 tree class_decl;
6016{
6017 tree class = TREE_TYPE (class_decl);
6018 tree super, vector;
6019 int i;
6020
6021 if (CLASS_ABSTRACT (class_decl))
6022 return;
6023
6024 /* Check for inherited types */
165f37bc
APB
6025 super = class;
6026 do {
6027 super = CLASSTYPE_SUPER (super);
6028 check_abstract_method_definitions (0, class_decl, super);
6029 } while (super != object_type_node);
d77613be
APB
6030
6031 /* Check for implemented interfaces. */
6032 vector = TYPE_BINFO_BASETYPES (class);
6033 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6034 {
6035 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6036 check_abstract_method_definitions (1, class_decl, super);
6037 }
6038}
6039
165f37bc
APB
6040/* Check all the types method DECL uses and return 1 if all of them
6041 are now complete, 0 otherwise. This is used to check whether its
6042 safe to build a method signature or not. */
6043
6044static int
6045check_method_types_complete (decl)
6046 tree decl;
6047{
6048 tree type = TREE_TYPE (decl);
6049 tree args;
6050
6051 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6052 return 0;
6053
6054 args = TYPE_ARG_TYPES (type);
6055 if (TREE_CODE (type) == METHOD_TYPE)
6056 args = TREE_CHAIN (args);
6057 for (; args != end_params_node; args = TREE_CHAIN (args))
6058 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6059 return 0;
6060
6061 return 1;
6062}
6063
34d4df06
APB
6064/* Visible interface to check methods contained in CLASS_DECL */
6065
6066void
6067java_check_methods (class_decl)
6068 tree class_decl;
6069{
6070 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6071 return;
6072
6073 if (CLASS_INTERFACE (class_decl))
6074 java_check_abstract_methods (class_decl);
6075 else
6076 java_check_regular_methods (class_decl);
6077
6078 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6079}
6080
d77613be
APB
6081/* Check all the methods of CLASS_DECL. Methods are first completed
6082 then checked according to regular method existance rules. If no
6083 constructor for CLASS_DECL were encountered, then build its
6084 declaration. */
e04a16fb
AG
6085
6086static void
6087java_check_regular_methods (class_decl)
6088 tree class_decl;
6089{
c2952b01 6090 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6091 tree method;
6092 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6093 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6094 tree mthrows;
6095
6096 /* It is not necessary to check methods defined in java.lang.Object */
6097 if (class == object_type_node)
6098 return;
e04a16fb 6099
23a79c61
APB
6100 if (!TYPE_NVIRTUALS (class))
6101 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6102
6103 /* Should take interfaces into account. FIXME */
6104 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6105 {
5e942c50 6106 tree sig;
e04a16fb
AG
6107 tree method_wfl = DECL_NAME (method);
6108 int aflags;
6109
5e942c50
APB
6110 /* If we previously found something and its name was saved,
6111 reinstall it now */
6112 if (found && saved_found_wfl)
ba179f9f
APB
6113 {
6114 DECL_NAME (found) = saved_found_wfl;
6115 saved_found_wfl = NULL_TREE;
6116 }
5e942c50 6117
e04a16fb
AG
6118 /* Check for redefinitions */
6119 if (check_method_redefinition (class, method))
6120 continue;
6121
22eed1e6
APB
6122 /* If we see one constructor a mark so we don't generate the
6123 default one. Also skip other verifications: constructors
6124 can't be inherited hence hiden or overriden */
6125 if (DECL_CONSTRUCTOR_P (method))
6126 {
6127 saw_constructor = 1;
6128 continue;
6129 }
6130
c877974e
APB
6131 /* We verify things thrown by the method. They must inherits from
6132 java.lang.Throwable */
6133 for (mthrows = DECL_FUNCTION_THROWS (method);
6134 mthrows; mthrows = TREE_CHAIN (mthrows))
6135 {
6136 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6137 parse_error_context
781b0558 6138 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6139 IDENTIFIER_POINTER
6140 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6141 }
6142
e04a16fb 6143 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6144 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6145
c2952b01
APB
6146 /* Inner class can't declare static methods */
6147 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6148 {
6149 char *t = xstrdup (lang_printable_name (class, 0));
6150 parse_error_context
6151 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6152 lang_printable_name (method, 0), t);
6153 free (t);
6154 }
6155
5e942c50 6156 /* Nothing overrides or it's a private method. */
aabd7048 6157 if (!found)
5e942c50 6158 continue;
aabd7048
PB
6159 if (METHOD_PRIVATE (found))
6160 {
6161 found = NULL_TREE;
6162 continue;
6163 }
5e942c50
APB
6164
6165 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6166 We set it temporarily for the sake of the error report. */
6167 saved_found_wfl = DECL_NAME (found);
6168 reset_method_name (found);
6169
614eaae0
APB
6170 /* If `found' is declared in an interface, make sure the
6171 modifier matches. */
6172 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6173 && clinit_identifier_node != DECL_NAME (found)
6174 && !METHOD_PUBLIC (method))
6175 {
6176 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6177 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6178 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6179 lang_printable_name (method, 0),
6180 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6181 }
6182
e04a16fb
AG
6183 /* Can't override a method with the same name and different return
6184 types. */
6185 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6186 {
614eaae0
APB
6187 char *t = xstrdup
6188 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6189 parse_error_context
7f10c2e2 6190 (method_wfl,
b9f7e36c 6191 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6192 lang_printable_name (found, 0), t,
b9f7e36c
APB
6193 IDENTIFIER_POINTER
6194 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6195 free (t);
6196 }
e04a16fb 6197
7f10c2e2
APB
6198 aflags = get_access_flags_from_decl (found);
6199 /* If the method has default, access in an other package, then
6200 issue a warning that the current method doesn't override the
6201 one that was found elsewhere. Do not issue this warning when
6202 the match was found in java.lang.Object. */
6203 if (DECL_CONTEXT (found) != object_type_node
a003f638 6204 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6205 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6206 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6207 && flag_not_overriding)
6208 {
6209 parse_warning_context
781b0558 6210 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
7f10c2e2
APB
6211 lang_printable_name (found, 0),
6212 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6213 IDENTIFIER_POINTER (DECL_NAME
6214 (TYPE_NAME (DECL_CONTEXT (found)))));
6215 continue;
6216 }
6217
e04a16fb
AG
6218 /* Can't override final. Can't override static. */
6219 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6220 {
6221 /* Static *can* override static */
6222 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6223 continue;
6224 parse_error_context
6225 (method_wfl,
6226 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6227 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6228 lang_printable_name (found, 0),
e04a16fb
AG
6229 (METHOD_FINAL (found) ? "final" : "static"),
6230 IDENTIFIER_POINTER
6231 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6232 continue;
6233 }
7f10c2e2 6234
e04a16fb
AG
6235 /* Static method can't override instance method. */
6236 if (METHOD_STATIC (method))
6237 {
6238 parse_error_context
6239 (method_wfl,
781b0558 6240 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6241 lang_printable_name (found, 0),
e04a16fb
AG
6242 IDENTIFIER_POINTER
6243 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6244 continue;
6245 }
5e942c50 6246
5e942c50
APB
6247 /* - Overriding/hiding public must be public
6248 - Overriding/hiding protected must be protected or public
6249 - If the overriden or hidden method has default (package)
6250 access, then the overriding or hiding method must not be
614eaae0
APB
6251 private; otherwise, a compile-time error occurs. If
6252 `found' belongs to an interface, things have been already
6253 taken care of. */
6254 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6255 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6256 || (METHOD_PROTECTED (found)
6257 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6258 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6259 && METHOD_PRIVATE (method))))
e04a16fb
AG
6260 {
6261 parse_error_context
6262 (method_wfl,
781b0558 6263 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6264 (METHOD_PUBLIC (method) ? "public" :
6265 (METHOD_PRIVATE (method) ? "private" : "protected")),
6266 IDENTIFIER_POINTER (DECL_NAME
6267 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6268 continue;
6269 }
6270
b9f7e36c
APB
6271 /* Overriding methods must have compatible `throws' clauses on checked
6272 exceptions, if any */
6273 check_throws_clauses (method, method_wfl, found);
6274
e04a16fb
AG
6275 /* Inheriting multiple methods with the same signature. FIXME */
6276 }
6277
5e942c50
APB
6278 /* Don't forget eventual pending found and saved_found_wfl. Take
6279 into account that we might have exited because we saw an
d77613be 6280 artificial method as the last entry. */
5e942c50
APB
6281
6282 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6283 DECL_NAME (found) = saved_found_wfl;
6284
23a79c61
APB
6285 if (!TYPE_NVIRTUALS (class))
6286 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6287
d77613be
APB
6288 /* Search for inherited abstract method not yet implemented in this
6289 class. */
6290 java_check_abstract_method_definitions (class_decl);
6291
22eed1e6 6292 if (!saw_constructor)
e920ebc9 6293 fatal ("No constructor found");
e04a16fb
AG
6294}
6295
b9f7e36c
APB
6296/* Return a non zero value if the `throws' clause of METHOD (if any)
6297 is incompatible with the `throws' clause of FOUND (if any). */
6298
6299static void
6300check_throws_clauses (method, method_wfl, found)
6301 tree method, method_wfl, found;
6302{
6303 tree mthrows, fthrows;
6304
c877974e
APB
6305 /* Can't check these things with class loaded from bytecode. FIXME */
6306 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6307 return;
6308
b9f7e36c
APB
6309 for (mthrows = DECL_FUNCTION_THROWS (method);
6310 mthrows; mthrows = TREE_CHAIN (mthrows))
6311 {
6312 /* We don't verify unchecked expressions */
c877974e 6313 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6314 continue;
6315 /* Checked expression must be compatible */
6316 for (fthrows = DECL_FUNCTION_THROWS (found);
6317 fthrows; fthrows = TREE_CHAIN (fthrows))
6318 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6319 break;
6320 if (!fthrows)
6321 {
6322 parse_error_context
781b0558 6323 (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 6324 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6325 lang_printable_name (found, 0),
b9f7e36c
APB
6326 IDENTIFIER_POINTER
6327 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6328 }
6329 }
6330}
6331
e04a16fb
AG
6332/* Check abstract method of interface INTERFACE */
6333
6334static void
5e942c50
APB
6335java_check_abstract_methods (interface_decl)
6336 tree interface_decl;
e04a16fb
AG
6337{
6338 int i, n;
6339 tree method, basetype_vec, found;
5e942c50 6340 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6341
6342 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6343 {
b9f7e36c 6344 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6345
6346 /* 2- Check for double definition inside the defining interface */
6347 if (check_method_redefinition (interface, method))
6348 continue;
6349
6350 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6351 the thrown exceptions (FIXME) */
e04a16fb
AG
6352 found = lookup_java_interface_method2 (interface, method);
6353 if (found)
6354 {
5e942c50
APB
6355 char *t;
6356 tree saved_found_wfl = DECL_NAME (found);
6357 reset_method_name (found);
c2e3db92 6358 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6359 parse_error_context
b9f7e36c 6360 (method_wfl,
5e942c50 6361 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6362 lang_printable_name (found, 0), t,
b9f7e36c
APB
6363 IDENTIFIER_POINTER
6364 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6365 free (t);
5e942c50 6366 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6367 continue;
e04a16fb
AG
6368 }
6369 }
6370
6371 /* 4- Inherited methods can't differ by their returned types */
6372 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6373 return;
6374 n = TREE_VEC_LENGTH (basetype_vec);
6375 for (i = 0; i < n; i++)
6376 {
6377 tree sub_interface_method, sub_interface;
6378 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6379 if (!vec_elt)
6380 continue;
6381 sub_interface = BINFO_TYPE (vec_elt);
6382 for (sub_interface_method = TYPE_METHODS (sub_interface);
6383 sub_interface_method;
6384 sub_interface_method = TREE_CHAIN (sub_interface_method))
6385 {
6386 found = lookup_java_interface_method2 (interface,
6387 sub_interface_method);
6388 if (found && (found != sub_interface_method))
5e942c50
APB
6389 {
6390 tree saved_found_wfl = DECL_NAME (found);
6391 reset_method_name (found);
6392 parse_error_context
6393 (lookup_cl (sub_interface_method),
781b0558 6394 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6395 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6396 lang_printable_name (found, 0),
6397 IDENTIFIER_POINTER
6398 (DECL_NAME (TYPE_NAME
6399 (DECL_CONTEXT (sub_interface_method)))),
6400 IDENTIFIER_POINTER
6401 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6402 DECL_NAME (found) = saved_found_wfl;
6403 }
e04a16fb
AG
6404 }
6405 }
6406}
6407
e04a16fb
AG
6408/* Lookup methods in interfaces using their name and partial
6409 signature. Return a matching method only if their types differ. */
6410
6411static tree
6412lookup_java_interface_method2 (class, method_decl)
6413 tree class, method_decl;
6414{
6415 int i, n;
6416 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6417
6418 if (!basetype_vec)
6419 return NULL_TREE;
6420
6421 n = TREE_VEC_LENGTH (basetype_vec);
6422 for (i = 0; i < n; i++)
6423 {
6424 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6425 if ((BINFO_TYPE (vec_elt) != object_type_node)
6426 && (to_return =
6427 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6428 return to_return;
6429 }
6430 for (i = 0; i < n; i++)
6431 {
6432 to_return = lookup_java_interface_method2
6433 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6434 if (to_return)
6435 return to_return;
6436 }
6437
6438 return NULL_TREE;
6439}
6440
6441/* Lookup method using their name and partial signature. Return a
6442 matching method only if their types differ. */
6443
6444static tree
6445lookup_java_method2 (clas, method_decl, do_interface)
6446 tree clas, method_decl;
6447 int do_interface;
6448{
5e942c50
APB
6449 tree method, method_signature, method_name, method_type, name;
6450
e04a16fb 6451 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6452 name = DECL_NAME (method_decl);
6453 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6454 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6455 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6456
6457 while (clas != NULL_TREE)
6458 {
6459 for (method = TYPE_METHODS (clas);
6460 method != NULL_TREE; method = TREE_CHAIN (method))
6461 {
6462 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6463 tree name = DECL_NAME (method);
6464 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6465 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6466 && method_sig == method_signature
6467 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6468 return method;
e04a16fb
AG
6469 }
6470 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6471 }
6472 return NULL_TREE;
6473}
6474
f441f671
APB
6475/* Return the line that matches DECL line number, and try its best to
6476 position the column number. Used during error reports. */
e04a16fb
AG
6477
6478static tree
6479lookup_cl (decl)
6480 tree decl;
6481{
6482 static tree cl = NULL_TREE;
f441f671 6483 char *line, *found;
e04a16fb
AG
6484
6485 if (!decl)
6486 return NULL_TREE;
6487
6488 if (cl == NULL_TREE)
19e223db
MM
6489 {
6490 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6491 ggc_add_tree_root (&cl, 1);
6492 }
e04a16fb
AG
6493
6494 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6495 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6496
dba41d30 6497 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6498 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6499
6500 found = strstr ((const char *)line,
6501 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6502 if (found)
6503 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6504
e04a16fb
AG
6505 return cl;
6506}
6507
6508/* Look for a simple name in the single-type import list */
6509
6510static tree
6511find_name_in_single_imports (name)
6512 tree name;
6513{
6514 tree node;
6515
6516 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6517 if (TREE_VALUE (node) == name)
6518 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6519
6520 return NULL_TREE;
6521}
6522
6523/* Process all single-type import. */
6524
6525static int
6526process_imports ()
6527{
6528 tree import;
6529 int error_found;
6530
6531 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6532 {
6533 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6534
6535 /* Don't load twice something already defined. */
6536 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6537 continue;
02ae6e2e
APB
6538
6539 while (1)
6540 {
6541 tree left;
6542
6543 QUALIFIED_P (to_be_found) = 1;
6544 load_class (to_be_found, 0);
6545 error_found =
6546 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6547
6548 /* We found it, we can bail out */
6549 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6550 break;
6551
6552 /* We haven't found it. Maybe we're trying to access an
6553 inner class. The only way for us to know is to try again
6554 after having dropped a qualifier. If we can't break it further,
6555 we have an error. */
6556 if (breakdown_qualified (&left, NULL, to_be_found))
6557 break;
6558
6559 to_be_found = left;
6560 }
e04a16fb
AG
6561 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6562 {
6563 parse_error_context (TREE_PURPOSE (import),
6564 "Class or interface `%s' not found in import",
6565 IDENTIFIER_POINTER (to_be_found));
6566 return 1;
6567 }
6568 if (error_found)
6569 return 1;
6570 }
6571 return 0;
6572}
6573
9a7ab4b3
APB
6574/* Possibly find and mark a class imported by a single-type import
6575 statement. */
e04a16fb 6576
9a7ab4b3 6577static void
e04a16fb
AG
6578find_in_imports (class_type)
6579 tree class_type;
6580{
6581 tree import;
6582
6583 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6584 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6585 {
6586 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6587 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6588 }
e04a16fb
AG
6589}
6590
e04a16fb 6591static int
63a212ed 6592note_possible_classname (name, len)
49f48c71 6593 const char *name;
63a212ed 6594 int len;
e04a16fb 6595{
63a212ed
PB
6596 tree node;
6597 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6598 len = len - 5;
6599 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6600 len = len - 6;
e04a16fb 6601 else
63a212ed
PB
6602 return 0;
6603 node = ident_subst (name, len, "", '/', '.', "");
6604 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6605 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6606 return 1;
e04a16fb
AG
6607}
6608
6609/* Read a import directory, gathering potential match for further type
6610 references. Indifferently reads a filesystem or a ZIP archive
6611 directory. */
6612
6613static void
6614read_import_dir (wfl)
6615 tree wfl;
6616{
63a212ed 6617 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6618 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6619 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6620 DIR *dirp = NULL;
d8fccff5 6621 JCF *saved_jcf = current_jcf;
e04a16fb 6622
63a212ed
PB
6623 int found = 0;
6624 int k;
6625 void *entry;
6626 struct buffer filename[1];
6627
6628
6629 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6630 return;
6631 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6632
6633 BUFFER_INIT (filename);
6634 buffer_grow (filename, package_length + 100);
6635
6636 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6637 {
49f48c71 6638 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6639 int entry_length = strlen (entry_name);
6640 if (jcf_path_is_zipfile (entry))
6641 {
6642 ZipFile *zipf;
6643 buffer_grow (filename, entry_length);
6644 memcpy (filename->data, entry_name, entry_length - 1);
6645 filename->data[entry_length-1] = '\0';
6646 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6647 if (zipf == NULL)
6648 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6649 else
6650 {
6651 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6652 BUFFER_RESET (filename);
6653 for (k = 0; k < package_length; k++)
6654 {
6655 char ch = package_name[k];
6656 *filename->ptr++ = ch == '.' ? '/' : ch;
6657 }
6658 *filename->ptr++ = '/';
6659
345137c7 6660 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6661 {
49f48c71 6662 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6663 int current_entry_len = zipd->filename_length;
6664
345137c7
TT
6665 if (current_entry_len >= BUFFER_LENGTH (filename)
6666 && strncmp (filename->data, current_entry,
6667 BUFFER_LENGTH (filename)) != 0)
63a212ed 6668 continue;
345137c7 6669 found |= note_possible_classname (current_entry,
63a212ed
PB
6670 current_entry_len);
6671 }
6672 }
6673 }
6674 else
6675 {
6676 BUFFER_RESET (filename);
6677 buffer_grow (filename, entry_length + package_length + 4);
6678 strcpy (filename->data, entry_name);
6679 filename->ptr = filename->data + entry_length;
6680 for (k = 0; k < package_length; k++)
6681 {
6682 char ch = package_name[k];
6683 *filename->ptr++ = ch == '.' ? '/' : ch;
6684 }
6685 *filename->ptr = '\0';
6686
6687 dirp = opendir (filename->data);
6688 if (dirp == NULL)
6689 continue;
6690 *filename->ptr++ = '/';
6691 for (;;)
6692 {
63a212ed 6693 int len;
49f48c71 6694 const char *d_name;
63a212ed
PB
6695 struct dirent *direntp = readdir (dirp);
6696 if (!direntp)
6697 break;
6698 d_name = direntp->d_name;
6699 len = strlen (direntp->d_name);
6700 buffer_grow (filename, len+1);
6701 strcpy (filename->ptr, d_name);
345137c7 6702 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6703 package_length+len+1);
6704 }
6705 if (dirp)
6706 closedir (dirp);
6707 }
6708 }
e04a16fb 6709
63a212ed 6710 free (filename->data);
e04a16fb 6711
63a212ed
PB
6712 /* Here we should have a unified way of retrieving an entry, to be
6713 indexed. */
6714 if (!found)
e04a16fb
AG
6715 {
6716 static int first = 1;
6717 if (first)
6718 {
781b0558 6719 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6720 java_error_count++;
6721 first = 0;
6722 }
6723 else
63a212ed
PB
6724 parse_error_context (wfl, "Package `%s' not found in import",
6725 package_name);
e04a16fb
AG
6726 current_jcf = saved_jcf;
6727 return;
6728 }
e04a16fb
AG
6729 current_jcf = saved_jcf;
6730}
6731
6732/* Possibly find a type in the import on demands specified
6733 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6734 entire list, to detected potential double definitions. */
6735
6736static int
6737find_in_imports_on_demand (class_type)
6738 tree class_type;
6739{
ab3a6dd6 6740 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6741 int seen_once = -1;
ab3a6dd6 6742 tree cl = NULL_TREE;
e04a16fb
AG
6743
6744 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6745 {
49f48c71 6746 const char *id_name;
e04a16fb
AG
6747 obstack_grow (&temporary_obstack,
6748 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6749 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6750 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6751 obstack_grow0 (&temporary_obstack,
6752 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6753 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6754 id_name = obstack_finish (&temporary_obstack);
6755
6756 node = maybe_get_identifier (id_name);
6757 if (node && IS_A_CLASSFILE_NAME (node))
6758 {
6759 if (seen_once < 0)
6760 {
6761 cl = TREE_PURPOSE (import);
6762 seen_once = 1;
6763 node_to_use = node;
6764 }
6765 else
6766 {
6767 seen_once++;
6768 parse_error_context
1e12ab9b
APB
6769 (TREE_PURPOSE (import),
6770 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6771 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6772 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6773 }
6774 }
6775 }
6776
6777 if (seen_once == 1)
6778 {
6779 /* Setup lineno so that it refers to the line of the import (in
6780 case we parse a class file and encounter errors */
6781 tree decl;
6782 int saved_lineno = lineno;
6783 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6784 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6785 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6786 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6787 /* If there is no DECL set for the class or if the class isn't
6788 loaded and not seen in source yet, the load */
6789 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6790 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6791 load_class (node_to_use, 0);
6792 lineno = saved_lineno;
6793 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6794 }
6795 else
6796 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6797}
6798
9a7ab4b3
APB
6799/* Add package NAME to the list of package encountered so far. To
6800 speed up class lookup in do_resolve_class, we make sure a
6801 particular package is added only once. */
6802
6803static void
6804register_package (name)
6805 tree name;
6806{
6807 static struct hash_table _pht, *pht = NULL;
6808
6809 if (!pht)
6810 {
6811 hash_table_init (&_pht, hash_newfunc,
6812 java_hash_hash_tree_node, java_hash_compare_tree_node);
6813 pht = &_pht;
6814 }
6815
6816 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6817 {
6818 package_list = chainon (package_list, build_tree_list (name, NULL));
6819 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6820 }
6821}
6822
5e942c50
APB
6823static tree
6824resolve_package (pkg, next)
6825 tree pkg, *next;
6826{
c2952b01 6827 tree current, acc;
5e942c50 6828 tree type_name = NULL_TREE;
49f48c71 6829 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6830
6831 /* The trick is to determine when the package name stops and were
6832 the name of something contained in the package starts. Then we
6833 return a fully qualified name of what we want to get. */
6834
6835 /* Do a quick search on well known package names */
6836 if (!strncmp (name, "java.lang.reflect", 17))
6837 {
6838 *next =
6839 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6840 type_name = lookup_package_type (name, 17);
6841 }
6842 else if (!strncmp (name, "java.lang", 9))
6843 {
6844 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6845 type_name = lookup_package_type (name, 9);
6846 }
5e942c50 6847
2c56429a
APB
6848 /* If we found something here, return */
6849 if (type_name)
6850 return type_name;
6851
6852 *next = EXPR_WFL_QUALIFICATION (pkg);
6853
6854 /* Try the current package. */
6855 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6856 IDENTIFIER_LENGTH (ctxp->package)))
6857 {
6858 type_name =
6859 lookup_package_type_and_set_next (name,
6860 IDENTIFIER_LENGTH (ctxp->package),
6861 next );
6862 if (type_name)
6863 return type_name;
6864 }
6865
6866 /* Search in imported package */
6867 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6868 {
6869 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6870 int len = IDENTIFIER_LENGTH (current_pkg_name);
6871 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6872 {
6873 tree left, dummy;
6874
6875 breakdown_qualified (&left, &dummy, current_pkg_name);
6876 len = IDENTIFIER_LENGTH (left);
6877 type_name = lookup_package_type_and_set_next (name, len, next);
6878 if (type_name)
6879 break;
6880 }
6881 }
6882
c2952b01
APB
6883 /* Try to progressively construct a type name */
6884 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6885 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6886 current; current = TREE_CHAIN (current))
6887 {
6888 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6889 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6890 {
6891 type_name = acc;
6b48deee
APB
6892 /* resolve_package should be used in a loop, hence we
6893 point at this one to naturally process the next one at
6894 the next iteration. */
6895 *next = current;
c2952b01
APB
6896 break;
6897 }
6898 }
2c56429a
APB
6899 return type_name;
6900}
6901
6902static tree
6903lookup_package_type_and_set_next (name, len, next)
49f48c71 6904 const char *name;
2c56429a
APB
6905 int len;
6906 tree *next;
6907{
49f48c71 6908 const char *ptr;
2c56429a
APB
6909 tree type_name = lookup_package_type (name, len);
6910
6911 if (!type_name)
6912 return NULL;
6913
6914 ptr = IDENTIFIER_POINTER (type_name);
6915 while (ptr && (ptr = strchr (ptr, '.')))
6916 {
6917 *next = TREE_CHAIN (*next);
6918 ptr++;
6919 }
5e942c50
APB
6920 return type_name;
6921}
6922
6923static tree
6924lookup_package_type (name, from)
49f48c71 6925 const char *name;
5e942c50
APB
6926 int from;
6927{
6928 char subname [128];
49f48c71 6929 const char *sub = &name[from+1];
5e942c50
APB
6930 while (*sub != '.' && *sub)
6931 sub++;
6932 strncpy (subname, name, sub-name);
6933 subname [sub-name] = '\0';
6934 return get_identifier (subname);
6935}
6936
cf1748bf 6937static void
4dbf4496
APB
6938check_inner_class_access (decl, enclosing_decl, cl)
6939 tree decl, enclosing_decl, cl;
cf1748bf 6940{
4dbf4496
APB
6941 int access = 0;
6942
cf1748bf 6943 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6944 as a result of processing a JDEP crafted by source_start_java_method
6945 for the purpose of patching its parm decl. But the error would
6946 have been already trapped when fixing the method's signature.
6947 DECL can also be NULL in case of earlier errors. */
6948 if (!decl || !cl)
cf1748bf
APB
6949 return;
6950
4dbf4496
APB
6951 /* We grant access to private and protected inner classes if the
6952 location from where we're trying to access DECL is an enclosing
6953 context for DECL or if both have a common enclosing context. */
6954 if (CLASS_PRIVATE (decl))
6955 access = 1;
6956 if (CLASS_PROTECTED (decl))
6957 access = 2;
6958 if (!access)
6959 return;
6960
6961 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6962 TREE_TYPE (decl))
6963 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6964 TREE_TYPE (decl)))
6965 return;
6966
6967 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6968 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6969 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6970 lang_printable_name (decl, 0));
6971}
6972
e04a16fb
AG
6973/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6974 access violations were found, 1 otherwise. */
6975
6976static int
6977check_pkg_class_access (class_name, cl)
6978 tree class_name;
6979 tree cl;
6980{
6981 tree type;
e04a16fb
AG
6982
6983 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6984 return 0;
6985
6986 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6987 return 0;
6988
6989 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6990 {
e28cd97b
APB
6991 /* Access to a private class within the same package is
6992 allowed. */
6993 tree l, r;
6994 breakdown_qualified (&l, &r, class_name);
6995 if (l == ctxp->package)
6996 return 0;
6997
e04a16fb 6998 parse_error_context
781b0558 6999 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
7000 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7001 IDENTIFIER_POINTER (class_name));
7002 return 1;
7003 }
7004 return 0;
7005}
7006
7007/* Local variable declaration. */
7008
7009static void
7010declare_local_variables (modifier, type, vlist)
7011 int modifier;
7012 tree type;
7013 tree vlist;
7014{
c583dd46
APB
7015 tree decl, current, saved_type;
7016 tree type_wfl = NULL_TREE;
e04a16fb 7017 int must_chain = 0;
c2952b01 7018 int final_p = 0;
e04a16fb 7019
2aa11e97 7020 /* Push a new block if statements were seen between the last time we
e04a16fb 7021 pushed a block and now. Keep a cound of block to close */
f099f336 7022 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 7023 {
f099f336 7024 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 7025 tree b = enter_block ();
f099f336 7026 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
7027 }
7028
7029 if (modifier)
7030 {
7031 int i;
7032 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 7033 if (modifier == ACC_FINAL)
c2952b01 7034 final_p = 1;
c877974e
APB
7035 else
7036 {
7037 parse_error_context
7038 (ctxp->modifier_ctx [i],
7039 "Only `final' is allowed as a local variables modifier");
7040 return;
7041 }
e04a16fb
AG
7042 }
7043
c583dd46
APB
7044 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7045 hold the TYPE value if a new incomplete has to be created (as
7046 opposed to being found already existing and reused). */
7047 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7048
7049 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 7050 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
7051
7052 /* Go through all the declared variables */
7053 for (current = vlist, saved_type = type; current;
7054 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 7055 {
c877974e 7056 tree other, real_type;
e04a16fb
AG
7057 tree wfl = TREE_PURPOSE (current);
7058 tree name = EXPR_WFL_NODE (wfl);
7059 tree init = TREE_VALUE (current);
e04a16fb 7060
c583dd46
APB
7061 /* Process NAME, as it may specify extra dimension(s) for it */
7062 type = build_array_from_name (type, type_wfl, name, &name);
7063
7064 /* Variable redefinition check */
7065 if ((other = lookup_name_in_blocks (name)))
7066 {
7067 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7068 DECL_SOURCE_LINE (other));
7069 continue;
7070 }
7071
7072 /* Type adjustment. We may have just readjusted TYPE because
7073 the variable specified more dimensions. Make sure we have
7074 a reference if we can and don't have one already. */
1886c9d8 7075 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7076
7077 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7078 /* Never layout this decl. This will be done when its scope
7079 will be entered */
c877974e 7080 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 7081 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 7082 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7083 BLOCK_CHAIN_DECL (decl);
7084
d4370213
APB
7085 /* If doing xreferencing, replace the line number with the WFL
7086 compound value */
7087 if (flag_emit_xref)
7088 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7089
e04a16fb
AG
7090 /* Don't try to use an INIT statement when an error was found */
7091 if (init && java_error_count)
7092 init = NULL_TREE;
c583dd46
APB
7093
7094 /* Add the initialization function to the current function's code */
7095 if (init)
e04a16fb 7096 {
c583dd46
APB
7097 /* Name might have been readjusted */
7098 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7099 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7100 java_method_add_stmt (current_function_decl,
7101 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7102 init));
7103 }
7104
7105 /* Setup dependency the type of the decl */
7106 if (must_chain)
7107 {
7108 jdep *dep;
7109 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7110 dep = CLASSD_LAST (ctxp->classd_list);
7111 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7112 }
7113 }
7114 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7115}
7116
7117/* Called during parsing. Build decls from argument list. */
7118
7119static void
7120source_start_java_method (fndecl)
7121 tree fndecl;
7122{
7123 tree tem;
7124 tree parm_decl;
7125 int i;
7126
79d13333
APB
7127 if (!fndecl)
7128 return;
7129
e04a16fb
AG
7130 current_function_decl = fndecl;
7131
7132 /* New scope for the function */
7133 enter_block ();
7134 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7135 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7136 {
7137 tree type = TREE_VALUE (tem);
7138 tree name = TREE_PURPOSE (tem);
7139
23a79c61
APB
7140 /* If type is incomplete. Create an incomplete decl and ask for
7141 the decl to be patched later */
e04a16fb
AG
7142 if (INCOMPLETE_TYPE_P (type))
7143 {
7144 jdep *jdep;
c877974e
APB
7145 tree real_type = GET_REAL_TYPE (type);
7146 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7147 type = obtain_incomplete_type (type);
e04a16fb
AG
7148 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7149 jdep = CLASSD_LAST (ctxp->classd_list);
7150 JDEP_MISC (jdep) = name;
7151 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7152 }
7153 else
7154 parm_decl = build_decl (PARM_DECL, name, type);
7155
c2952b01
APB
7156 /* Remember if a local variable was declared final (via its
7157 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7158 if (ARG_FINAL_P (tem))
c7303e41
APB
7159 {
7160 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7161 LOCAL_FINAL (parm_decl) = 1;
7162 }
c2952b01 7163
e04a16fb
AG
7164 BLOCK_CHAIN_DECL (parm_decl);
7165 }
7166 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7167 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7168 nreverse (tem);
7169 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7170 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7171}
7172
22eed1e6
APB
7173/* Called during parsing. Creates an artificial method declaration. */
7174
7175static tree
7176create_artificial_method (class, flags, type, name, args)
7177 tree class;
7178 int flags;
7179 tree type, name, args;
7180{
22eed1e6
APB
7181 tree mdecl;
7182
c2952b01 7183 java_parser_context_save_global ();
22eed1e6
APB
7184 lineno = 0;
7185 mdecl = make_node (FUNCTION_TYPE);
7186 TREE_TYPE (mdecl) = type;
7187 TYPE_ARG_TYPES (mdecl) = args;
7188 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7189 java_parser_context_restore_global ();
22eed1e6
APB
7190 DECL_ARTIFICIAL (mdecl) = 1;
7191 return mdecl;
7192}
7193
7194/* Starts the body if an artifical method. */
7195
7196static void
7197start_artificial_method_body (mdecl)
7198 tree mdecl;
7199{
7200 DECL_SOURCE_LINE (mdecl) = 1;
7201 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7202 source_start_java_method (mdecl);
7203 enter_block ();
7204}
7205
7206static void
7207end_artificial_method_body (mdecl)
7208 tree mdecl;
7209{
1e0cdc10
APB
7210 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7211 It has to be evaluated first. (if mdecl is current_function_decl,
7212 we have an undefined behavior if no temporary variable is used.) */
7213 tree b = exit_block ();
7214 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7215 exit_block ();
7216}
7217
e04a16fb
AG
7218/* Called during expansion. Push decls formerly built from argument
7219 list so they're usable during expansion. */
7220
7221static void
7222expand_start_java_method (fndecl)
7223 tree fndecl;
7224{
7225 tree tem, *ptr;
e04a16fb 7226
e04a16fb
AG
7227 current_function_decl = fndecl;
7228
c2952b01
APB
7229 if (! quiet_flag)
7230 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7231 announce_function (fndecl);
c2952b01
APB
7232 if (! quiet_flag)
7233 fprintf (stderr, "]");
7234
7235 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7236 ptr = &DECL_ARGUMENTS (fndecl);
7237 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7238 while (tem)
7239 {
7240 tree next = TREE_CHAIN (tem);
b67d701b 7241 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7242 if (PROMOTE_PROTOTYPES
7243 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7244 && INTEGRAL_TYPE_P (type))
7245 type = integer_type_node;
b67d701b 7246 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7247 layout_decl (tem, 0);
7248 pushdecl (tem);
e04a16fb
AG
7249 *ptr = tem;
7250 ptr = &TREE_CHAIN (tem);
7251 tem = next;
7252 }
7253 *ptr = NULL_TREE;
7254 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7255 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7256}
7257
7258/* Terminate a function and expand its body. */
7259
7260static void
7261source_end_java_method ()
7262{
7263 tree fndecl = current_function_decl;
138657ec 7264 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7265
79d13333
APB
7266 if (!fndecl)
7267 return;
7268
e04a16fb
AG
7269 java_parser_context_save_global ();
7270 lineno = ctxp->last_ccb_indent1;
7271
b67d701b
PB
7272 /* Set EH language codes */
7273 java_set_exception_lang_code ();
7274
5423609c
APB
7275 /* Turn function bodies with only a NOP expr null, so they don't get
7276 generated at all and we won't get warnings when using the -W
7277 -Wall flags. */
7278 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7279 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7280
e04a16fb
AG
7281 /* Generate function's code */
7282 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7283 && ! flag_emit_class_files
7284 && ! flag_emit_xref)
e04a16fb
AG
7285 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7286
7287 /* pop out of its parameters */
7288 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7289 poplevel (1, 0, 1);
7290 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7291
7292 /* Generate rtl for function exit. */
e8fc7396 7293 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7294 {
7295 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7296 /* Emit catch-finally clauses */
7297 emit_handlers ();
e04a16fb
AG
7298 expand_function_end (input_filename, lineno, 0);
7299
138657ec
AH
7300 /* FIXME: If the current method contains any exception handlers,
7301 force asynchronous_exceptions: this is necessary because signal
7302 handlers in libjava may throw exceptions. This is far from being
7303 a perfect solution, but it's better than doing nothing at all.*/
7304 if (catch_clauses)
7305 asynchronous_exceptions = 1;
7306
e04a16fb
AG
7307 /* Run the optimizers and output assembler code for this function. */
7308 rest_of_compilation (fndecl);
7309 }
7310
7311 current_function_decl = NULL_TREE;
e04a16fb 7312 java_parser_context_restore_global ();
138657ec 7313 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7314}
7315
7316/* Record EXPR in the current function block. Complements compound
7317 expression second operand if necessary. */
7318
7319tree
7320java_method_add_stmt (fndecl, expr)
7321 tree fndecl, expr;
7322{
b771925e
APB
7323 if (!GET_CURRENT_BLOCK (fndecl))
7324 return NULL_TREE;
f099f336 7325 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7326}
e04a16fb 7327
b67d701b
PB
7328static tree
7329add_stmt_to_block (b, type, stmt)
7330 tree b, type, stmt;
7331{
7332 tree body = BLOCK_EXPR_BODY (b), c;
7333
e04a16fb
AG
7334 if (java_error_count)
7335 return body;
b67d701b
PB
7336
7337 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7338 return body;
7339
b67d701b
PB
7340 BLOCK_EXPR_BODY (b) = c;
7341 TREE_SIDE_EFFECTS (c) = 1;
7342 return c;
e04a16fb
AG
7343}
7344
7345/* Add STMT to EXISTING if possible, otherwise create a new
7346 COMPOUND_EXPR and add STMT to it. */
7347
7348static tree
7349add_stmt_to_compound (existing, type, stmt)
7350 tree existing, type, stmt;
7351{
15fdcfe9
PB
7352 if (existing)
7353 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7354 else
15fdcfe9 7355 return stmt;
e04a16fb
AG
7356}
7357
1886c9d8
APB
7358void java_layout_seen_class_methods ()
7359{
7360 tree previous_list = all_class_list;
7361 tree end = NULL_TREE;
7362 tree current;
7363
7364 while (1)
7365 {
7366 for (current = previous_list;
7367 current != end; current = TREE_CHAIN (current))
7368 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7369
7370 if (previous_list != all_class_list)
7371 {
7372 end = previous_list;
7373 previous_list = all_class_list;
7374 }
7375 else
7376 break;
7377 }
7378}
7379
e04a16fb 7380void
c2952b01 7381java_reorder_fields ()
e04a16fb 7382{
c2952b01 7383 static tree stop_reordering = NULL_TREE;
19e223db 7384 static int initialized_p;
c2952b01 7385 tree current;
19e223db
MM
7386
7387 /* Register STOP_REORDERING with the garbage collector. */
7388 if (!initialized_p)
7389 {
7390 ggc_add_tree_root (&stop_reordering, 1);
7391 initialized_p = 1;
7392 }
7393
5e942c50 7394 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7395 {
5e942c50 7396 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7397
c2952b01
APB
7398 if (current_class == stop_reordering)
7399 break;
7400
c877974e
APB
7401 /* Reverse the fields, but leave the dummy field in front.
7402 Fields are already ordered for Object and Class */
7403 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7404 && current_class != class_type_node)
7405 {
23a79c61
APB
7406 /* If the dummy field is there, reverse the right fields and
7407 just layout the type for proper fields offset */
c877974e
APB
7408 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7409 {
7410 tree fields = TYPE_FIELDS (current_class);
7411 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7412 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7413 }
23a79c61
APB
7414 /* We don't have a dummy field, we need to layout the class,
7415 after having reversed the fields */
c877974e
APB
7416 else
7417 {
7418 TYPE_FIELDS (current_class) =
7419 nreverse (TYPE_FIELDS (current_class));
7420 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7421 }
7422 }
c2952b01
APB
7423 }
7424 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7425}
7426
7427/* Layout the methods of all classes loaded in one way on an
7428 other. Check methods of source parsed classes. Then reorder the
7429 fields and layout the classes or the type of all source parsed
7430 classes */
7431
7432void
7433java_layout_classes ()
7434{
7435 tree current;
7436 int save_error_count = java_error_count;
7437
7438 /* Layout the methods of all classes seen so far */
7439 java_layout_seen_class_methods ();
7440 java_parse_abort_on_error ();
7441 all_class_list = NULL_TREE;
7442
7443 /* Then check the methods of all parsed classes */
7444 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7445 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7446 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7447 java_parse_abort_on_error ();
7448
7449 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7450 {
7451 current_class = TREE_TYPE (TREE_VALUE (current));
7452 layout_class (current_class);
5e942c50 7453
c877974e
APB
7454 /* From now on, the class is considered completely loaded */
7455 CLASS_LOADED_P (current_class) = 1;
7456
5e942c50
APB
7457 /* Error reported by the caller */
7458 if (java_error_count)
7459 return;
e04a16fb 7460 }
23a79c61
APB
7461
7462 /* We might have reloaded classes durign the process of laying out
7463 classes for code generation. We must layout the methods of those
7464 late additions, as constructor checks might use them */
1886c9d8 7465 java_layout_seen_class_methods ();
23a79c61 7466 java_parse_abort_on_error ();
e04a16fb
AG
7467}
7468
c2952b01
APB
7469/* Expand methods in the current set of classes rememebered for
7470 generation. */
e04a16fb 7471
49f48c71 7472static void
c2952b01 7473java_complete_expand_classes ()
e04a16fb
AG
7474{
7475 tree current;
ce6e9147
APB
7476
7477 do_not_fold = flag_emit_xref;
c2952b01 7478
e04a16fb 7479 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7480 if (!INNER_CLASS_DECL_P (current))
7481 java_complete_expand_class (current);
7482}
e04a16fb 7483
c2952b01
APB
7484/* Expand the methods found in OUTER, starting first by OUTER's inner
7485 classes, if any. */
e04a16fb 7486
c2952b01
APB
7487static void
7488java_complete_expand_class (outer)
7489 tree outer;
7490{
7491 tree inner_list;
e04a16fb 7492
c2952b01 7493 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7494
c2952b01
APB
7495 /* We need to go after all inner classes and start expanding them,
7496 starting with most nested ones. We have to do that because nested
7497 classes might add functions to outer classes */
e04a16fb 7498
c2952b01
APB
7499 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7500 inner_list; inner_list = TREE_CHAIN (inner_list))
7501 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7502
c2952b01
APB
7503 java_complete_expand_methods (outer);
7504 set_nested_class_simple_name_value (outer, 0); /* Reset */
7505}
7506
7507/* Expand methods registered in CLASS_DECL. The general idea is that
7508 we expand regular methods first. This allows us get an estimate on
7509 how outer context local alias fields are really used so we can add
7510 to the constructor just enough code to initialize them properly (it
c00f0fb2 7511 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7512 constructors and then <clinit>. */
7513
7514static void
7515java_complete_expand_methods (class_decl)
7516 tree class_decl;
7517{
7518 tree clinit, finit, decl, first_decl;
7519
7520 current_class = TREE_TYPE (class_decl);
7521
c7303e41
APB
7522 /* Find whether the class has final variables */
7523 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7524 if (FIELD_FINAL (decl))
7525 {
7526 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7527 break;
7528 }
7529
c2952b01
APB
7530 /* Initialize a new constant pool */
7531 init_outgoing_cpool ();
7532
7533 /* Pre-expand <clinit> to figure whether we really need it or
7534 not. If we do need it, we pre-expand the static fields so they're
7535 ready to be used somewhere else. <clinit> will be fully expanded
7536 after we processed the constructors. */
7537 first_decl = TYPE_METHODS (current_class);
7538 clinit = maybe_generate_pre_expand_clinit (current_class);
7539
c00f0fb2 7540 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7541 try to use it.*/
7542 if (TYPE_FINIT_STMT_LIST (current_class))
7543 {
7544 finit = generate_finit (current_class);
7545 java_complete_expand_method (finit);
7546 }
7547
7548 /* Now do the constructors */
7549 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7550 {
7551 int no_body;
7552
7553 if (!DECL_CONSTRUCTOR_P (decl))
7554 continue;
7555
7556 no_body = !DECL_FUNCTION_BODY (decl);
7557 /* Don't generate debug info on line zero when expanding a
7558 generated constructor. */
7559 if (no_body)
7560 restore_line_number_status (1);
7561
c7303e41
APB
7562 /* Reset the final local variable assignment flags */
7563 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7564 reset_final_variable_local_assignment_flag (current_class);
7565
c2952b01 7566 java_complete_expand_method (decl);
c7303e41
APB
7567
7568 /* Check for missed out final variable assignment */
7569 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7570 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7571
7572 if (no_body)
7573 restore_line_number_status (0);
7574 }
7575
7576 /* First, do the ordinary methods. */
7577 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7578 {
7145d9fe
TT
7579 /* Skip abstract or native methods -- but do handle native
7580 methods when generating JNI stubs. */
7581 if (METHOD_ABSTRACT (decl)
7582 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7583 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7584 continue;
7145d9fe
TT
7585
7586 if (METHOD_NATIVE (decl))
7587 {
7588 tree body = build_jni_stub (decl);
7589 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7590 }
7591
c2952b01
APB
7592 java_complete_expand_method (decl);
7593 }
7594
7595 /* If there is indeed a <clinit>, fully expand it now */
7596 if (clinit)
7597 {
c7303e41
APB
7598 /* Reset the final local variable assignment flags */
7599 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7600 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7601 /* Prevent the use of `this' inside <clinit> */
7602 ctxp->explicit_constructor_p = 1;
7603 java_complete_expand_method (clinit);
7604 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7605 /* Check for missed out static final variable assignment */
7606 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7607 && !CLASS_INTERFACE (class_decl))
7608 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7609 }
c2952b01 7610
165f37bc
APB
7611 /* We might have generated a class$ that we now want to expand */
7612 if (TYPE_DOT_CLASS (current_class))
7613 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7614
c2952b01
APB
7615 /* Now verify constructor circularity (stop after the first one we
7616 prove wrong.) */
7617 if (!CLASS_INTERFACE (class_decl))
7618 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7619 if (DECL_CONSTRUCTOR_P (decl)
7620 && verify_constructor_circularity (decl, decl))
7621 break;
7622
c7303e41
APB
7623 /* Final check on the initialization of final variables. */
7624 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7625 {
7626 check_final_variable_global_assignment_flag (current_class);
7627 /* If we have an interface, check for uninitialized fields. */
7628 if (CLASS_INTERFACE (class_decl))
7629 check_static_final_variable_assignment_flag (current_class);
7630 }
7631
c2952b01
APB
7632 /* Save the constant pool. We'll need to restore it later. */
7633 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7634}
7635
c2952b01
APB
7636/* Attempt to create <clinit>. Pre-expand static fields so they can be
7637 safely used in some other methods/constructors. */
e920ebc9 7638
c2952b01
APB
7639static tree
7640maybe_generate_pre_expand_clinit (class_type)
7641 tree class_type;
e920ebc9 7642{
c2952b01
APB
7643 tree current, mdecl;
7644
7645 if (!TYPE_CLINIT_STMT_LIST (class_type))
7646 return NULL_TREE;
e920ebc9 7647
c2952b01
APB
7648 /* Go through all static fields and pre expand them */
7649 for (current = TYPE_FIELDS (class_type); current;
7650 current = TREE_CHAIN (current))
7651 if (FIELD_STATIC (current))
7652 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7653
7654 /* Then build the <clinit> method */
7655 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7656 clinit_identifier_node, end_params_node);
7657 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7658 mdecl, NULL_TREE);
7659 start_artificial_method_body (mdecl);
7660
7661 /* We process the list of assignment we produced as the result of
7662 the declaration of initialized static field and add them as
7663 statement to the <clinit> method. */
7664 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7665 current = TREE_CHAIN (current))
e920ebc9 7666 {
9a7ab4b3 7667 tree stmt = current;
c2952b01
APB
7668 /* We build the assignment expression that will initialize the
7669 field to its value. There are strict rules on static
7670 initializers (8.5). FIXME */
98a52c2c 7671 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7672 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7673 java_method_add_stmt (mdecl, stmt);
7674 }
e920ebc9 7675
c2952b01
APB
7676 end_artificial_method_body (mdecl);
7677
92d83515
APB
7678 /* Now we want to place <clinit> as the last method (because we need
7679 it at least for interface so that it doesn't interfere with the
7680 dispatch table based lookup. */
7681 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7682 {
92d83515
APB
7683 current = TREE_CHAIN (TYPE_METHODS (class_type));
7684 TYPE_METHODS (class_type) = current;
c2952b01
APB
7685
7686 while (TREE_CHAIN (current))
7687 current = TREE_CHAIN (current);
92d83515 7688
c2952b01
APB
7689 TREE_CHAIN (current) = mdecl;
7690 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7691 }
c2952b01
APB
7692
7693 return mdecl;
e920ebc9
APB
7694}
7695
dba41d30
APB
7696/* Analyzes a method body and look for something that isn't a
7697 MODIFY_EXPR. */
7698
7699static int
7700analyze_clinit_body (bbody)
7701 tree bbody;
7702{
7703 while (bbody)
7704 switch (TREE_CODE (bbody))
7705 {
7706 case BLOCK:
7707 bbody = BLOCK_EXPR_BODY (bbody);
7708 break;
7709
7710 case EXPR_WITH_FILE_LOCATION:
7711 bbody = EXPR_WFL_NODE (bbody);
7712 break;
7713
7714 case COMPOUND_EXPR:
7715 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7716 return 1;
7717 bbody = TREE_OPERAND (bbody, 1);
7718 break;
7719
7720 case MODIFY_EXPR:
7721 bbody = NULL_TREE;
7722 break;
7723
7724 default:
7725 bbody = NULL_TREE;
7726 return 1;
7727 }
7728 return 0;
7729}
7730
7731
92d83515
APB
7732/* See whether we could get rid of <clinit>. Criteria are: all static
7733 final fields have constant initial values and the body of <clinit>
7734 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7735
7736static int
7737maybe_yank_clinit (mdecl)
7738 tree mdecl;
7739{
7740 tree type, current;
7741 tree fbody, bbody;
99eaf8d4 7742 int found = 0;
92d83515
APB
7743
7744 if (!DECL_CLINIT_P (mdecl))
7745 return 0;
f0f3a777
APB
7746
7747 /* If the body isn't empty, then we keep <clinit>. Note that if
7748 we're emitting classfiles, this isn't enough not to rule it
7749 out. */
92d83515 7750 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7751 bbody = BLOCK_EXPR_BODY (fbody);
7752 if (bbody && bbody != error_mark_node)
92d83515 7753 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7754 else
7755 return 0;
f0f3a777 7756 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7757 return 0;
7758
7759 type = DECL_CONTEXT (mdecl);
7760 current = TYPE_FIELDS (type);
7761
7762 for (current = (current ? TREE_CHAIN (current) : current);
7763 current; current = TREE_CHAIN (current))
f0f3a777
APB
7764 {
7765 tree f_init;
7766
7767 /* We're not interested in non static field */
7768 if (!FIELD_STATIC (current))
7769 continue;
7770
7771 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7772 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7773 should generated an empty <clinit> so that SUID are computed
7774 correctly. */
7775 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7776 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7777 break;
7778
7779 f_init = DECL_INITIAL (current);
7780 /* If we're emitting native code, we want static final fields to
7781 have constant initializers. If we don't meet these
7782 conditions, we keep <clinit> */
7783 if (!flag_emit_class_files
7784 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7785 break;
7786 /* If we're emitting bytecode, we want static fields to have
7787 constant initializers or no initializer. If we don't meet
7788 these conditions, we keep <clinit> */
7789 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7790 break;
7791 }
92d83515 7792
99eaf8d4
APB
7793 /* Now we analyze the method body and look for something that
7794 isn't a MODIFY_EXPR */
7795 if (bbody == empty_stmt_node)
dba41d30
APB
7796 found = 0;
7797 else
7798 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7799
7800 if (current || found)
92d83515
APB
7801 return 0;
7802
7803 /* Get rid of <clinit> in the class' list of methods */
7804 if (TYPE_METHODS (type) == mdecl)
7805 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7806 else
7807 for (current = TYPE_METHODS (type); current;
7808 current = TREE_CHAIN (current))
7809 if (TREE_CHAIN (current) == mdecl)
7810 {
7811 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7812 break;
7813 }
7814
7815 return 1;
7816}
7817
7818
e04a16fb
AG
7819/* Complete and expand a method. */
7820
7821static void
7822java_complete_expand_method (mdecl)
7823 tree mdecl;
7824{
92d83515
APB
7825 int yank_clinit = 0;
7826
c2952b01 7827 current_function_decl = mdecl;
22eed1e6
APB
7828 /* Fix constructors before expanding them */
7829 if (DECL_CONSTRUCTOR_P (mdecl))
7830 fix_constructors (mdecl);
e04a16fb 7831
22eed1e6 7832 /* Expand functions that have a body */
e04a16fb
AG
7833 if (DECL_FUNCTION_BODY (mdecl))
7834 {
9bbc7d9f
PB
7835 tree fbody = DECL_FUNCTION_BODY (mdecl);
7836 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7837 tree exception_copy = NULL_TREE;
e04a16fb 7838 expand_start_java_method (mdecl);
939d7216 7839 build_result_decl (mdecl);
e04a16fb
AG
7840
7841 current_this
7842 = (!METHOD_STATIC (mdecl) ?
7843 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7844
ce6e9147
APB
7845 /* Purge the `throws' list of unchecked exceptions. If we're
7846 doing xref, save a copy of the list and re-install it
7847 later. */
7848 if (flag_emit_xref)
7849 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7850
b9f7e36c
APB
7851 purge_unchecked_exceptions (mdecl);
7852
7853 /* Install exceptions thrown with `throws' */
7854 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7855
9bbc7d9f 7856 if (block_body != NULL_TREE)
bc3ca41b
PB
7857 {
7858 block_body = java_complete_tree (block_body);
c2952b01 7859
7145d9fe 7860 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7861 check_for_initialization (block_body);
f099f336 7862 ctxp->explicit_constructor_p = 0;
bc3ca41b 7863 }
e803d3b2 7864
9bbc7d9f 7865 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7866
c2952b01
APB
7867 /* If we saw a return but couldn't evaluate it properly, we'll
7868 have an error_mark_node here. */
7869 if (block_body != error_mark_node
7870 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7871 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7872 && !flag_emit_xref)
82371d41 7873 missing_return_error (current_function_decl);
7525cc04 7874
92d83515
APB
7875 /* Check wether we could just get rid of clinit, now the picture
7876 is complete. */
7877 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7878 complete_start_java_method (mdecl);
7879
e04a16fb 7880 /* Don't go any further if we've found error(s) during the
92d83515
APB
7881 expansion */
7882 if (!java_error_count && !yank_clinit)
e04a16fb 7883 source_end_java_method ();
22eed1e6
APB
7884 else
7885 {
92d83515
APB
7886 if (java_error_count)
7887 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7888 poplevel (1, 0, 1);
7889 }
b9f7e36c
APB
7890
7891 /* Pop the exceptions and sanity check */
7892 POP_EXCEPTIONS();
7893 if (currently_caught_type_list)
7894 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7895
7896 if (flag_emit_xref)
7897 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7898 }
7899}
7900
c2952b01
APB
7901\f
7902
7903/* This section of the code deals with accessing enclosing context
7904 fields either directly by using the relevant access to this$<n> or
7905 by invoking an access method crafted for that purpose. */
7906
7907/* Build the necessary access from an inner class to an outer
7908 class. This routine could be optimized to cache previous result
7909 (decl, current_class and returned access). When an access method
7910 needs to be generated, it always takes the form of a read. It might
7911 be later turned into a write by calling outer_field_access_fix. */
7912
7913static tree
7914build_outer_field_access (id, decl)
7915 tree id, decl;
7916{
7917 tree access = NULL_TREE;
7918 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7919 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7920
5e18f6d6
APB
7921 /* If the immediate enclosing context of the current class is the
7922 field decl's class or inherits from it; build the access as
7923 `this$<n>.<field>'. Note that we will break the `private' barrier
7924 if we're not emitting bytecodes. */
7925 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7926 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7927 {
7928 tree thisn = build_current_thisn (current_class);
7929 access = make_qualified_primary (build_wfl_node (thisn),
7930 id, EXPR_WFL_LINECOL (id));
7931 }
7932 /* Otherwise, generate access methods to outer this and access the
7933 field (either using an access method or by direct access.) */
7934 else
7935 {
7936 int lc = EXPR_WFL_LINECOL (id);
7937
7938 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7939 get a hold to the enclosing instance we need, and then we
c2952b01 7940 build the field access. */
5e18f6d6 7941 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7942
7943 /* If the field is private and we're generating bytecode, then
7944 we generate an access method */
7945 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7946 {
7947 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7948 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7949 name, access, NULL_TREE);
7950 }
7951 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7952 Once again we break the `private' access rule from a foreign
7953 class. */
7954 else
7955 access = make_qualified_primary (access, id, lc);
7956 }
7957 return resolve_expression_name (access, NULL);
7958}
7959
7960/* Return a non zero value if NODE describes an outer field inner
7961 access. */
7962
7963static int
7964outer_field_access_p (type, decl)
7965 tree type, decl;
7966{
7967 if (!INNER_CLASS_TYPE_P (type)
7968 || TREE_CODE (decl) != FIELD_DECL
7969 || DECL_CONTEXT (decl) == type)
7970 return 0;
ee5f86dc
APB
7971
7972 /* If the inner class extends the declaration context of the field
7973 we're try to acces, then this isn't an outer field access */
7974 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7975 return 0;
c2952b01
APB
7976
7977 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7978 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7979 {
7980 if (type == DECL_CONTEXT (decl))
7981 return 1;
5e18f6d6 7982
c2952b01 7983 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7984 {
7985 /* Before we give up, see whether the field is inherited from
7986 the enclosing context we're considering. */
7987 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7988 return 1;
7989 break;
7990 }
c2952b01
APB
7991 }
7992
7993 return 0;
7994}
7995
7996/* Return a non zero value if NODE represents an outer field inner
7997 access that was been already expanded. As a side effect, it returns
7998 the name of the field being accessed and the argument passed to the
7999 access function, suitable for a regeneration of the access method
8000 call if necessary. */
8001
8002static int
8003outer_field_expanded_access_p (node, name, arg_type, arg)
8004 tree node, *name, *arg_type, *arg;
8005{
8006 int identified = 0;
8007
8008 if (TREE_CODE (node) != CALL_EXPR)
8009 return 0;
8010
8011 /* Well, gcj generates slightly different tree nodes when compiling
8012 to native or bytecodes. It's the case for function calls. */
8013
8014 if (flag_emit_class_files
8015 && TREE_CODE (node) == CALL_EXPR
8016 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8017 identified = 1;
8018 else if (!flag_emit_class_files)
8019 {
8020 node = TREE_OPERAND (node, 0);
8021
8022 if (node && TREE_OPERAND (node, 0)
8023 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8024 {
8025 node = TREE_OPERAND (node, 0);
8026 if (TREE_OPERAND (node, 0)
8027 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8028 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8029 (DECL_NAME (TREE_OPERAND (node, 0)))))
8030 identified = 1;
8031 }
8032 }
8033
8034 if (identified && name && arg_type && arg)
8035 {
8036 tree argument = TREE_OPERAND (node, 1);
8037 *name = DECL_NAME (TREE_OPERAND (node, 0));
8038 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8039 *arg = TREE_VALUE (argument);
8040 }
8041 return identified;
8042}
8043
8044/* Detect in NODE an outer field read access from an inner class and
8045 transform it into a write with RHS as an argument. This function is
8046 called from the java_complete_lhs when an assignment to a LHS can
8047 be identified. */
8048
8049static tree
8050outer_field_access_fix (wfl, node, rhs)
8051 tree wfl, node, rhs;
8052{
8053 tree name, arg_type, arg;
8054
8055 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8056 {
8057 /* At any rate, check whether we're trying to assign a value to
8058 a final. */
8059 tree accessed = (JDECL_P (node) ? node :
8060 (TREE_CODE (node) == COMPONENT_REF ?
8061 TREE_OPERAND (node, 1) : node));
8062 if (check_final_assignment (accessed, wfl))
8063 return error_mark_node;
8064
8065 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8066 arg_type, name, arg, rhs);
8067 return java_complete_tree (node);
8068 }
8069 return NULL_TREE;
8070}
8071
8072/* Construct the expression that calls an access method:
8073 <type>.access$<n>(<arg1> [, <arg2>]);
8074
8075 ARG2 can be NULL and will be omitted in that case. It will denote a
8076 read access. */
8077
8078static tree
8079build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8080 int lc;
8081 tree type, access_method_name, arg1, arg2;
8082{
8083 tree args, cn, access;
8084
8085 args = arg1 ? arg1 :
8086 build_wfl_node (build_current_thisn (current_class));
8087 args = build_tree_list (NULL_TREE, args);
8088
8089 if (arg2)
8090 args = tree_cons (NULL_TREE, arg2, args);
8091
8092 access = build_method_invocation (build_wfl_node (access_method_name), args);
8093 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8094 return make_qualified_primary (cn, access, lc);
8095}
8096
8097static tree
8098build_new_access_id ()
8099{
8100 static int access_n_counter = 1;
8101 char buffer [128];
8102
8103 sprintf (buffer, "access$%d", access_n_counter++);
8104 return get_identifier (buffer);
8105}
8106
8107/* Create the static access functions for the outer field DECL. We define a
8108 read:
8109 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8110 return inst$.field;
8111 }
8112 and a write access:
8113 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8114 TREE_TYPE (<field>) value$) {
8115 return inst$.field = value$;
8116 }
8117 We should have a usage flags on the DECL so we can lazily turn the ones
8118 we're using for code generation. FIXME.
8119*/
8120
8121static tree
8122build_outer_field_access_methods (decl)
8123 tree decl;
8124{
8125 tree id, args, stmt, mdecl;
8126
c7303e41 8127 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
8128 return FIELD_INNER_ACCESS (decl);
8129
c7303e41
APB
8130 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8131
c2952b01
APB
8132 /* Create the identifier and a function named after it. */
8133 id = build_new_access_id ();
8134
8135 /* The identifier is marked as bearing the name of a generated write
8136 access function for outer field accessed from inner classes. */
8137 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8138
8139 /* Create the read access */
8140 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8141 TREE_CHAIN (args) = end_params_node;
8142 stmt = make_qualified_primary (build_wfl_node (inst_id),
8143 build_wfl_node (DECL_NAME (decl)), 0);
8144 stmt = build_return (0, stmt);
8145 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8146 TREE_TYPE (decl), id, args, stmt);
8147 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8148
c7303e41
APB
8149 /* Create the write access method. No write access for final variable */
8150 if (!FIELD_FINAL (decl))
8151 {
8152 args = build_tree_list (inst_id,
8153 build_pointer_type (DECL_CONTEXT (decl)));
8154 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8155 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8156 stmt = make_qualified_primary (build_wfl_node (inst_id),
8157 build_wfl_node (DECL_NAME (decl)), 0);
8158 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8159 build_wfl_node (wpv_id)));
8160 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8161 TREE_TYPE (decl), id,
8162 args, stmt);
8163 }
c2952b01 8164 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8165
8166 /* Return the access name */
8167 return FIELD_INNER_ACCESS (decl) = id;
8168}
8169
8170/* Build an field access method NAME. */
8171
8172static tree
8173build_outer_field_access_method (class, type, name, args, body)
8174 tree class, type, name, args, body;
8175{
8176 tree saved_current_function_decl, mdecl;
8177
8178 /* Create the method */
8179 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8180 fix_method_argument_names (args, mdecl);
8181 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8182
8183 /* Attach the method body. */
8184 saved_current_function_decl = current_function_decl;
8185 start_artificial_method_body (mdecl);
8186 java_method_add_stmt (mdecl, body);
8187 end_artificial_method_body (mdecl);
8188 current_function_decl = saved_current_function_decl;
8189
8190 return mdecl;
8191}
8192
8193\f
8194/* This section deals with building access function necessary for
8195 certain kinds of method invocation from inner classes. */
8196
8197static tree
8198build_outer_method_access_method (decl)
8199 tree decl;
8200{
8201 tree saved_current_function_decl, mdecl;
8202 tree args = NULL_TREE, call_args = NULL_TREE;
8203 tree carg, id, body, class;
8204 char buffer [80];
8205 int parm_id_count = 0;
8206
8207 /* Test this abort with an access to a private field */
8208 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8209 abort ();
8210
8211 /* Check the cache first */
8212 if (DECL_FUNCTION_INNER_ACCESS (decl))
8213 return DECL_FUNCTION_INNER_ACCESS (decl);
8214
8215 class = DECL_CONTEXT (decl);
8216
8217 /* Obtain an access identifier and mark it */
8218 id = build_new_access_id ();
8219 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8220
c2952b01
APB
8221 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8222 /* Create the arguments, as much as the original */
8223 for (; carg && carg != end_params_node;
8224 carg = TREE_CHAIN (carg))
8225 {
8226 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8227 args = chainon (args, build_tree_list (get_identifier (buffer),
8228 TREE_VALUE (carg)));
8229 }
8230 args = chainon (args, end_params_node);
8231
8232 /* Create the method */
8233 mdecl = create_artificial_method (class, ACC_STATIC,
8234 TREE_TYPE (TREE_TYPE (decl)), id, args);
8235 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8236 /* There is a potential bug here. We should be able to use
8237 fix_method_argument_names, but then arg names get mixed up and
8238 eventually a constructor will have its this$0 altered and the
8239 outer context won't be assignment properly. The test case is
8240 stub.java FIXME */
8241 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8242
8243 /* Attach the method body. */
8244 saved_current_function_decl = current_function_decl;
8245 start_artificial_method_body (mdecl);
8246
8247 /* The actual method invocation uses the same args. When invoking a
8248 static methods that way, we don't want to skip the first
8249 argument. */
8250 carg = args;
8251 if (!METHOD_STATIC (decl))
8252 carg = TREE_CHAIN (carg);
8253 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8254 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8255 call_args);
8256
8257 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8258 call_args);
8259 if (!METHOD_STATIC (decl))
8260 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8261 body, 0);
8262 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8263 body = build_return (0, body);
8264 java_method_add_stmt (mdecl,body);
8265 end_artificial_method_body (mdecl);
8266 current_function_decl = saved_current_function_decl;
c2952b01
APB
8267
8268 /* Back tag the access function so it know what it accesses */
8269 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8270
8271 /* Tag the current method so it knows it has an access generated */
8272 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8273}
8274
8275\f
8276/* This section of the code deals with building expressions to access
8277 the enclosing instance of an inner class. The enclosing instance is
8278 kept in a generated field called this$<n>, with <n> being the
8279 inner class nesting level (starting from 0.) */
8280
dba41d30
APB
8281/* Build an access to a given this$<n>, always chaining access call to
8282 others. Access methods to this$<n> are build on the fly if
8283 necessary. This CAN'T be used to solely access this$<n-1> from
8284 this$<n> (which alway yield to special cases and optimization, see
8285 for example build_outer_field_access). */
c2952b01
APB
8286
8287static tree
8288build_access_to_thisn (from, to, lc)
8289 tree from, to;
8290 int lc;
8291{
8292 tree access = NULL_TREE;
8293
8294 while (from != to)
8295 {
c2952b01 8296 if (!access)
dba41d30
APB
8297 {
8298 access = build_current_thisn (from);
8299 access = build_wfl_node (access);
8300 }
8301 else
c2952b01 8302 {
dba41d30
APB
8303 tree access0_wfl, cn;
8304
8305 maybe_build_thisn_access_method (from);
8306 access0_wfl = build_wfl_node (access0_identifier_node);
8307 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8308 EXPR_WFL_LINECOL (access0_wfl) = lc;
8309 access = build_tree_list (NULL_TREE, access);
8310 access = build_method_invocation (access0_wfl, access);
8311 access = make_qualified_primary (cn, access, lc);
c2952b01 8312 }
5e18f6d6
APB
8313
8314 /* if FROM isn't an inter class, that's fine, we've done
8315 enough. What we're looking for can be accessed from there. */
8316 from = DECL_CONTEXT (TYPE_NAME (from));
8317 if (!from)
8318 break;
8319 from = TREE_TYPE (from);
c2952b01
APB
8320 }
8321 return access;
8322}
8323
8324/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8325 is returned if nothing needs to be generated. Otherwise, the method
152de068 8326 generated and a method decl is returned.
c2952b01
APB
8327
8328 NOTE: These generated methods should be declared in a class file
8329 attribute so that they can't be referred to directly. */
8330
8331static tree
8332maybe_build_thisn_access_method (type)
8333 tree type;
8334{
8335 tree mdecl, args, stmt, rtype;
8336 tree saved_current_function_decl;
8337
8338 /* If TYPE is a top-level class, no access method is required.
8339 If there already is such an access method, bail out. */
ee5f86dc 8340 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8341 return NULL_TREE;
8342
8343 /* We generate the method. The method looks like:
8344 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8345 */
c2952b01
APB
8346 args = build_tree_list (inst_id, build_pointer_type (type));
8347 TREE_CHAIN (args) = end_params_node;
8348 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8349 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8350 access0_identifier_node, args);
8351 fix_method_argument_names (args, mdecl);
8352 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8353 stmt = build_current_thisn (type);
8354 stmt = make_qualified_primary (build_wfl_node (inst_id),
8355 build_wfl_node (stmt), 0);
8356 stmt = build_return (0, stmt);
8357
8358 saved_current_function_decl = current_function_decl;
8359 start_artificial_method_body (mdecl);
8360 java_method_add_stmt (mdecl, stmt);
8361 end_artificial_method_body (mdecl);
8362 current_function_decl = saved_current_function_decl;
c2952b01
APB
8363
8364 CLASS_ACCESS0_GENERATED_P (type) = 1;
8365
8366 return mdecl;
8367}
8368
8369/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8370 the first level of innerclassing. this$1 for the next one, etc...
8371 This function can be invoked with TYPE to NULL, available and then
8372 has to count the parser context. */
8373
8374static tree
8375build_current_thisn (type)
8376 tree type;
8377{
8378 static int saved_i = -1;
8379 static tree saved_thisn = NULL_TREE;
19e223db
MM
8380 static tree saved_type = NULL_TREE;
8381 static int saved_type_i = 0;
8382 static int initialized_p;
c2952b01
APB
8383 tree decl;
8384 char buffer [80];
8385 int i = 0;
8386
19e223db
MM
8387 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8388 if (!initialized_p)
c2952b01 8389 {
19e223db
MM
8390 ggc_add_tree_root (&saved_thisn, 1);
8391 ggc_add_tree_root (&saved_type, 1);
8392 initialized_p = 1;
8393 }
c2952b01 8394
19e223db
MM
8395 if (type)
8396 {
c2952b01
APB
8397 if (type == saved_type)
8398 i = saved_type_i;
8399 else
8400 {
8401 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8402 decl; decl = DECL_CONTEXT (decl), i++)
8403 ;
8404
8405 saved_type = type;
8406 saved_type_i = i;
8407 }
8408 }
8409 else
8410 i = list_length (GET_CPC_LIST ())-2;
8411
8412 if (i == saved_i)
8413 return saved_thisn;
8414
8415 sprintf (buffer, "this$%d", i);
8416 saved_i = i;
8417 saved_thisn = get_identifier (buffer);
8418 return saved_thisn;
8419}
8420
8421/* Return the assignement to the hidden enclosing context `this$<n>'
8422 by the second incoming parameter to the innerclass constructor. The
8423 form used is `this.this$<n> = this$<n>;'. */
8424
8425static tree
8426build_thisn_assign ()
8427{
8428 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8429 {
8430 tree thisn = build_current_thisn (current_class);
8431 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8432 build_wfl_node (thisn), 0);
8433 tree rhs = build_wfl_node (thisn);
8434 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8435 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8436 }
8437 return NULL_TREE;
8438}
8439
8440\f
165f37bc
APB
8441/* Building the synthetic `class$' used to implement the `.class' 1.1
8442 extension for non primitive types. This method looks like:
8443
8444 static Class class$(String type) throws NoClassDefFoundError
8445 {
8446 try {return (java.lang.Class.forName (String));}
8447 catch (ClassNotFoundException e) {
8448 throw new NoClassDefFoundError(e.getMessage());}
8449 } */
8450
8451static tree
8452build_dot_class_method (class)
8453 tree class;
8454{
8455#define BWF(S) build_wfl_node (get_identifier ((S)))
8456#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8457 tree args, tmp, saved_current_function_decl, mdecl;
8458 tree stmt, throw_stmt, catch, catch_block, try_block;
8459 tree catch_clause_param;
8460 tree class_not_found_exception, no_class_def_found_error;
8461
8462 static tree get_message_wfl, type_parm_wfl;
8463
8464 if (!get_message_wfl)
8465 {
8466 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8467 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8468 ggc_add_tree_root (&get_message_wfl, 1);
8469 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8470 }
8471
8472 /* Build the arguments */
8473 args = build_tree_list (get_identifier ("type$"),
8474 build_pointer_type (string_type_node));
8475 TREE_CHAIN (args) = end_params_node;
8476
8477 /* Build the qualified name java.lang.Class.forName */
8478 tmp = MQN (MQN (MQN (BWF ("java"),
8479 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8480
8481 /* For things we have to catch and throw */
8482 class_not_found_exception =
8483 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8484 no_class_def_found_error =
8485 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8486 load_class (class_not_found_exception, 1);
8487 load_class (no_class_def_found_error, 1);
8488
8489 /* Create the "class$" function */
8490 mdecl = create_artificial_method (class, ACC_STATIC,
8491 build_pointer_type (class_type_node),
8492 get_identifier ("class$"), args);
8493 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8494 no_class_def_found_error);
8495
8496 /* We start by building the try block. We need to build:
8497 return (java.lang.Class.forName (type)); */
8498 stmt = build_method_invocation (tmp,
8499 build_tree_list (NULL_TREE, type_parm_wfl));
8500 stmt = build_return (0, stmt);
8501 /* Put it in a block. That's the try block */
8502 try_block = build_expr_block (stmt, NULL_TREE);
8503
8504 /* Now onto the catch block. We start by building the expression
8505 throwing a new exception:
8506 throw new NoClassDefFoundError (_.getMessage); */
8507 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8508 get_message_wfl, 0);
8509 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8510
8511 /* Build new NoClassDefFoundError (_.getMessage) */
8512 throw_stmt = build_new_invocation
8513 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8514 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8515
8516 /* Build the throw, (it's too early to use BUILD_THROW) */
8517 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8518
8519 /* Build the catch block to encapsulate all this. We begin by
8520 building an decl for the catch clause parameter and link it to
8521 newly created block, the catch block. */
8522 catch_clause_param =
8523 build_decl (VAR_DECL, wpv_id,
8524 build_pointer_type (class_not_found_exception));
8525 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8526
8527 /* We initialize the variable with the exception handler. */
8528 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8529 soft_exceptioninfo_call_node);
8530 add_stmt_to_block (catch_block, NULL_TREE, catch);
8531
8532 /* We add the statement throwing the new exception */
8533 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8534
8535 /* Build a catch expression for all this */
8536 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8537
8538 /* Build the try/catch sequence */
8539 stmt = build_try_statement (0, try_block, catch_block);
8540
8541 fix_method_argument_names (args, mdecl);
8542 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8543 saved_current_function_decl = current_function_decl;
8544 start_artificial_method_body (mdecl);
8545 java_method_add_stmt (mdecl, stmt);
8546 end_artificial_method_body (mdecl);
8547 current_function_decl = saved_current_function_decl;
8548 TYPE_DOT_CLASS (class) = mdecl;
8549
8550 return mdecl;
8551}
8552
8553static tree
f0f3a777
APB
8554build_dot_class_method_invocation (type)
8555 tree type;
165f37bc 8556{
f0f3a777
APB
8557 tree sig_id, s;
8558
8559 if (TYPE_ARRAY_P (type))
8560 sig_id = build_java_signature (type);
8561 else
8562 sig_id = DECL_NAME (TYPE_NAME (type));
8563
1f8f4a0b
MM
8564 s = build_string (IDENTIFIER_LENGTH (sig_id),
8565 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8566 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8567 build_tree_list (NULL_TREE, s));
8568}
8569
c2952b01
APB
8570/* This section of the code deals with constructor. */
8571
22eed1e6
APB
8572/* Craft a body for default constructor. Patch existing constructor
8573 bodies with call to super() and field initialization statements if
8574 necessary. */
8575
8576static void
8577fix_constructors (mdecl)
8578 tree mdecl;
8579{
8580 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8581 tree thisn_assign, compound = NULL_TREE;
8582 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8583
22eed1e6
APB
8584 if (!body)
8585 {
22eed1e6
APB
8586 /* It is an error for the compiler to generate a default
8587 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8588 takes no argument, or the same args for an anonymous class */
8589 if (verify_constructor_super (mdecl))
22eed1e6 8590 {
c2952b01
APB
8591 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8592 tree save = DECL_NAME (mdecl);
49f48c71 8593 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8594 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8595 parse_error_context
c2952b01
APB
8596 (lookup_cl (TYPE_NAME (class_type)),
8597 "No constructor matching `%s' found in class `%s'",
8598 lang_printable_name (mdecl, 0), n);
8599 DECL_NAME (mdecl) = save;
22eed1e6
APB
8600 }
8601
c2952b01
APB
8602 /* The constructor body must be crafted by hand. It's the
8603 constructor we defined when we realize we didn't have the
8604 CLASSNAME() constructor */
22eed1e6
APB
8605 start_artificial_method_body (mdecl);
8606
f0f3a777
APB
8607 /* Insert an assignment to the this$<n> hidden field, if
8608 necessary */
8609 if ((thisn_assign = build_thisn_assign ()))
8610 java_method_add_stmt (mdecl, thisn_assign);
8611
22eed1e6
APB
8612 /* We don't generate a super constructor invocation if we're
8613 compiling java.lang.Object. build_super_invocation takes care
8614 of that. */
e920ebc9 8615 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8616
c2952b01
APB
8617 /* Insert the instance initializer block right here, after the
8618 super invocation. */
8619 add_instance_initializer (mdecl);
8620
22eed1e6
APB
8621 end_artificial_method_body (mdecl);
8622 }
8623 /* Search for an explicit constructor invocation */
8624 else
8625 {
8626 int found = 0;
8627 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8628
8629 while (body)
8630 switch (TREE_CODE (body))
8631 {
8632 case CALL_EXPR:
8633 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8634 body = NULL_TREE;
8635 break;
8636 case COMPOUND_EXPR:
8637 case EXPR_WITH_FILE_LOCATION:
8638 body = TREE_OPERAND (body, 0);
8639 break;
8640 case BLOCK:
8641 body = BLOCK_EXPR_BODY (body);
8642 break;
8643 default:
8644 found = 0;
8645 body = NULL_TREE;
8646 }
8647 /* The constructor is missing an invocation of super() */
8648 if (!found)
8649 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8650 build_super_invocation (mdecl));
22eed1e6 8651
c2952b01
APB
8652 /* Generate the assignment to this$<n>, if necessary */
8653 if ((thisn_assign = build_thisn_assign ()))
8654 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8655
f0f3a777
APB
8656 /* Insert the instance initializer block right here, after the
8657 super invocation. */
8658 add_instance_initializer (mdecl);
8659
22eed1e6
APB
8660 /* Fix the constructor main block if we're adding extra stmts */
8661 if (compound)
8662 {
8663 compound = add_stmt_to_compound (compound, NULL_TREE,
8664 BLOCK_EXPR_BODY (main_block));
8665 BLOCK_EXPR_BODY (main_block) = compound;
8666 }
8667 }
8668}
8669
8670/* Browse constructors in the super class, searching for a constructor
8671 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8672 otherwise. If the current class is an anonymous inner class, look
8673 for something that has the same signature. */
22eed1e6
APB
8674
8675static int
c2952b01
APB
8676verify_constructor_super (mdecl)
8677 tree mdecl;
22eed1e6
APB
8678{
8679 tree class = CLASSTYPE_SUPER (current_class);
152de068 8680 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8681 tree sdecl;
8682
22eed1e6
APB
8683 if (!class)
8684 return 0;
8685
c2952b01 8686 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8687 {
c2952b01
APB
8688 tree mdecl_arg_type;
8689 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8690 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8691 if (DECL_CONSTRUCTOR_P (sdecl))
8692 {
cf1b2274 8693 tree m_arg_type;
152de068
APB
8694 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8695 if (super_inner)
8696 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8697 for (m_arg_type = mdecl_arg_type;
8698 (arg_type != end_params_node
8699 && m_arg_type != end_params_node);
c2952b01 8700 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8701 m_arg_type = TREE_CHAIN (m_arg_type))
8702 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8703 break;
8704
cf1b2274 8705 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8706 return 0;
8707 }
8708 }
8709 else
8710 {
8711 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8712 {
152de068
APB
8713 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8714 if (super_inner)
8715 arg = TREE_CHAIN (arg);
8716 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8717 return 0;
8718 }
8719 }
8720 return 1;
8721}
8722
22eed1e6 8723/* Generate code for all context remembered for code generation. */
b351b287
APB
8724
8725void
8726java_expand_classes ()
8727{
5423609c 8728 int save_error_count = 0;
c2952b01
APB
8729 static struct parser_ctxt *saved_ctxp = NULL;
8730
23a79c61
APB
8731 java_parse_abort_on_error ();
8732 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8733 return;
8734 java_layout_classes ();
8735 java_parse_abort_on_error ();
8736
c2952b01 8737 saved_ctxp = ctxp_for_generation;
b351b287
APB
8738 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8739 {
8740 ctxp = ctxp_for_generation;
8741 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8742 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8743 java_parse_abort_on_error ();
8744 }
c2952b01
APB
8745
8746 /* Find anonymous classes and expand their constructor, now they
8747 have been fixed. */
8748 for (ctxp_for_generation = saved_ctxp;
8749 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8750 {
8751 tree current;
8752 ctxp = ctxp_for_generation;
8753 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8754 {
8755 current_class = TREE_TYPE (current);
8756 if (ANONYMOUS_CLASS_P (current_class))
8757 {
8758 tree d;
8759 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8760 {
8761 if (DECL_CONSTRUCTOR_P (d))
8762 {
8763 restore_line_number_status (1);
8764 reset_method_name (d);
8765 java_complete_expand_method (d);
8766 restore_line_number_status (0);
8767 break; /* We now there are no other ones */
8768 }
8769 }
8770 }
8771 }
8772 }
8773
8774 /* If we've found error at that stage, don't try to generate
8775 anything, unless we're emitting xrefs or checking the syntax only
8776 (but not using -fsyntax-only for the purpose of generating
8777 bytecode. */
8778 if (java_error_count && !flag_emit_xref
8779 && (!flag_syntax_only && !flag_emit_class_files))
8780 return;
8781
8782 /* Now things are stable, go for generation of the class data. */
8783 for (ctxp_for_generation = saved_ctxp;
8784 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8785 {
8786 tree current;
8787 ctxp = ctxp_for_generation;
8788 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8789 {
8790 current_class = TREE_TYPE (current);
8791 outgoing_cpool = TYPE_CPOOL (current_class);
8792 if (flag_emit_class_files)
8793 write_classfile (current_class);
8794 if (flag_emit_xref)
8795 expand_xref (current_class);
8796 else if (! flag_syntax_only)
8797 finish_class ();
8798 }
8799 }
b351b287
APB
8800}
8801
e04a16fb
AG
8802/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8803 a tree list node containing RIGHT. Fore coming RIGHTs will be
8804 chained to this hook. LOCATION contains the location of the
8805 separating `.' operator. */
8806
8807static tree
8808make_qualified_primary (primary, right, location)
8809 tree primary, right;
8810 int location;
8811{
8812 tree wfl;
8813
c2952b01 8814 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8815 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8816 else
8817 {
8818 wfl = primary;
c2952b01
APB
8819 /* If wfl wasn't qualified, we build a first anchor */
8820 if (!EXPR_WFL_QUALIFICATION (wfl))
8821 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8822 }
8823
c2952b01 8824 /* And chain them */
e04a16fb
AG
8825 EXPR_WFL_LINECOL (right) = location;
8826 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8827 PRIMARY_P (wfl) = 1;
8828 return wfl;
8829}
8830
8831/* Simple merge of two name separated by a `.' */
8832
8833static tree
8834merge_qualified_name (left, right)
8835 tree left, right;
8836{
8837 tree node;
c2952b01
APB
8838 if (!left && !right)
8839 return NULL_TREE;
8840
8841 if (!left)
8842 return right;
8843
8844 if (!right)
8845 return left;
8846
e04a16fb
AG
8847 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8848 IDENTIFIER_LENGTH (left));
8849 obstack_1grow (&temporary_obstack, '.');
8850 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8851 IDENTIFIER_LENGTH (right));
8852 node = get_identifier (obstack_base (&temporary_obstack));
8853 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8854 QUALIFIED_P (node) = 1;
8855 return node;
8856}
8857
8858/* Merge the two parts of a qualified name into LEFT. Set the
8859 location information of the resulting node to LOCATION, usually
8860 inherited from the location information of the `.' operator. */
8861
8862static tree
8863make_qualified_name (left, right, location)
8864 tree left, right;
8865 int location;
8866{
bc3ca41b
PB
8867#ifdef USE_COMPONENT_REF
8868 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8869 EXPR_WFL_LINECOL (node) = location;
8870 return node;
8871#else
e04a16fb
AG
8872 tree left_id = EXPR_WFL_NODE (left);
8873 tree right_id = EXPR_WFL_NODE (right);
8874 tree wfl, merge;
8875
8876 merge = merge_qualified_name (left_id, right_id);
8877
8878 /* Left wasn't qualified and is now qualified */
8879 if (!QUALIFIED_P (left_id))
8880 {
8881 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8882 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8883 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8884 }
8885
8886 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8887 EXPR_WFL_LINECOL (wfl) = location;
8888 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8889
8890 EXPR_WFL_NODE (left) = merge;
8891 return left;
bc3ca41b 8892#endif
e04a16fb
AG
8893}
8894
8895/* Extract the last identifier component of the qualified in WFL. The
8896 last identifier is removed from the linked list */
8897
8898static tree
8899cut_identifier_in_qualified (wfl)
8900 tree wfl;
8901{
8902 tree q;
8903 tree previous = NULL_TREE;
8904 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8905 if (!TREE_CHAIN (q))
8906 {
8907 if (!previous)
781b0558 8908 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8909 TREE_CHAIN (previous) = NULL_TREE;
8910 return TREE_PURPOSE (q);
8911 }
8912}
8913
8914/* Resolve the expression name NAME. Return its decl. */
8915
8916static tree
5e942c50 8917resolve_expression_name (id, orig)
e04a16fb 8918 tree id;
5e942c50 8919 tree *orig;
e04a16fb
AG
8920{
8921 tree name = EXPR_WFL_NODE (id);
8922 tree decl;
8923
8924 /* 6.5.5.1: Simple expression names */
8925 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8926 {
8927 /* 15.13.1: NAME can appear within the scope of a local variable
8928 declaration */
8929 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8930 return decl;
8931
8932 /* 15.13.1: NAME can appear within a class declaration */
8933 else
8934 {
8935 decl = lookup_field_wrapper (current_class, name);
8936 if (decl)
8937 {
c2952b01 8938 tree access = NULL_TREE;
e04a16fb 8939 int fs = FIELD_STATIC (decl);
f2760b27
APB
8940
8941 /* If we're accessing an outer scope local alias, make
8942 sure we change the name of the field we're going to
8943 build access to. */
8944 if (FIELD_LOCAL_ALIAS_USED (decl))
8945 name = DECL_NAME (decl);
8946
e04a16fb
AG
8947 /* Instance variable (8.3.1.1) can't appear within
8948 static method, static initializer or initializer for
8949 a static variable. */
8950 if (!fs && METHOD_STATIC (current_function_decl))
8951 {
7f10c2e2 8952 static_ref_err (id, name, current_class);
e04a16fb
AG
8953 return error_mark_node;
8954 }
22eed1e6
APB
8955 /* Instance variables can't appear as an argument of
8956 an explicit constructor invocation */
8957 if (!fs && ctxp->explicit_constructor_p)
8958 {
8959 parse_error_context
781b0558 8960 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8961 return error_mark_node;
8962 }
5e942c50 8963
c2952b01
APB
8964 /* If we're processing an inner class and we're trying
8965 to access a field belonging to an outer class, build
8966 the access to the field */
8967 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8968 {
8969 if (CLASS_STATIC (TYPE_NAME (current_class)))
8970 {
8971 static_ref_err (id, DECL_NAME (decl), current_class);
8972 return error_mark_node;
8973 }
8974 return build_outer_field_access (id, decl);
8975 }
c2952b01 8976
5e942c50 8977 /* Otherwise build what it takes to access the field */
c2952b01
APB
8978 access = build_field_ref ((fs ? NULL_TREE : current_this),
8979 DECL_CONTEXT (decl), name);
e8fc7396 8980 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8981 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8982 /* We may be asked to save the real field access node */
8983 if (orig)
c2952b01 8984 *orig = access;
5e942c50 8985 /* And we return what we got */
c2952b01 8986 return access;
e04a16fb
AG
8987 }
8988 /* Fall down to error report on undefined variable */
8989 }
8990 }
8991 /* 6.5.5.2 Qualified Expression Names */
8992 else
8993 {
5e942c50
APB
8994 if (orig)
8995 *orig = NULL_TREE;
e04a16fb
AG
8996 qualify_ambiguous_name (id);
8997 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8998 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8999 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
9000 }
9001
9002 /* We've got an error here */
9003 parse_error_context (id, "Undefined variable `%s'",
9004 IDENTIFIER_POINTER (name));
9005
9006 return error_mark_node;
9007}
9008
7f10c2e2
APB
9009static void
9010static_ref_err (wfl, field_id, class_type)
9011 tree wfl, field_id, class_type;
9012{
9013 parse_error_context
9014 (wfl,
9015 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9016 IDENTIFIER_POINTER (field_id),
9017 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9018}
9019
e04a16fb
AG
9020/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
9021 We return something suitable to generate the field access. We also
9022 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9023 recipient's address can be null. */
9024
9025static tree
9026resolve_field_access (qual_wfl, field_decl, field_type)
9027 tree qual_wfl;
9028 tree *field_decl, *field_type;
9029{
9030 int is_static = 0;
9031 tree field_ref;
9032 tree decl, where_found, type_found;
9033
9034 if (resolve_qualified_expression_name (qual_wfl, &decl,
9035 &where_found, &type_found))
9036 return error_mark_node;
9037
9038 /* Resolve the LENGTH field of an array here */
9a7ab4b3 9039 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 9040 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 9041 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
9042 {
9043 tree length = build_java_array_length_access (where_found);
9044 field_ref =
9045 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
9046
9047 /* In case we're dealing with a static array, we need to
9048 initialize its class before the array length can be fetched.
9049 It's also a good time to create a DECL_RTL for the field if
9050 none already exists, otherwise if the field was declared in a
9051 class found in an external file and hasn't been (and won't
9052 be) accessed for its value, none will be created. */
9053 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9054 {
9055 build_static_field_ref (where_found);
9056 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9057 }
e04a16fb
AG
9058 }
9059 /* We might have been trying to resolve field.method(). In which
9060 case, the resolution is over and decl is the answer */
34f4db93 9061 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 9062 field_ref = decl;
34f4db93 9063 else if (JDECL_P (decl))
e04a16fb 9064 {
5e942c50
APB
9065 int static_final_found = 0;
9066 if (!type_found)
9067 type_found = DECL_CONTEXT (decl);
34f4db93 9068 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 9069 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 9070 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 9071 && DECL_INITIAL (decl))
5e942c50 9072 {
0c2b8145
APB
9073 /* When called on a FIELD_DECL of the right (primitive)
9074 type, java_complete_tree will try to substitue the decl
9075 for it's initial value. */
70541f45 9076 field_ref = java_complete_tree (decl);
5e942c50
APB
9077 static_final_found = 1;
9078 }
9079 else
7f10c2e2
APB
9080 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9081 NULL_TREE : where_found),
5e942c50 9082 type_found, DECL_NAME (decl));
e04a16fb
AG
9083 if (field_ref == error_mark_node)
9084 return error_mark_node;
e8fc7396
APB
9085 if (is_static && !static_final_found
9086 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 9087 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
9088 }
9089 else
9090 field_ref = decl;
9091
9092 if (field_decl)
9093 *field_decl = decl;
9094 if (field_type)
c877974e
APB
9095 *field_type = (QUAL_DECL_TYPE (decl) ?
9096 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
9097 return field_ref;
9098}
9099
e28cd97b
APB
9100/* If NODE is an access to f static field, strip out the class
9101 initialization part and return the field decl, otherwise, return
9102 NODE. */
9103
9104static tree
9105strip_out_static_field_access_decl (node)
9106 tree node;
9107{
9108 if (TREE_CODE (node) == COMPOUND_EXPR)
9109 {
9110 tree op1 = TREE_OPERAND (node, 1);
9111 if (TREE_CODE (op1) == COMPOUND_EXPR)
9112 {
9113 tree call = TREE_OPERAND (op1, 0);
9114 if (TREE_CODE (call) == CALL_EXPR
9115 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9116 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9117 == soft_initclass_node)
9118 return TREE_OPERAND (op1, 1);
9119 }
2f11d407
TT
9120 else if (JDECL_P (op1))
9121 return op1;
e28cd97b
APB
9122 }
9123 return node;
9124}
9125
e04a16fb
AG
9126/* 6.5.5.2: Qualified Expression Names */
9127
9128static int
9129resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9130 tree wfl;
9131 tree *found_decl, *type_found, *where_found;
9132{
9133 int from_type = 0; /* Field search initiated from a type */
c2952b01 9134 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9135 int previous_call_static = 0;
9136 int is_static;
9137 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9138 /* For certain for of inner class instantiation */
9139 tree saved_current, saved_this;
9140#define RESTORE_THIS_AND_CURRENT_CLASS \
9141 { current_class = saved_current; current_this = saved_this;}
9142
c877974e 9143 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9144
9145 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9146 {
9147 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9148 tree ret_decl; /* for EH checking */
9149 int location; /* for EH checking */
e04a16fb
AG
9150
9151 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9152 switch (TREE_CODE (qual_wfl))
9153 {
9154 case CALL_EXPR:
b67d701b 9155 case NEW_CLASS_EXPR:
e04a16fb
AG
9156 /* If the access to the function call is a non static field,
9157 build the code to access it. */
34f4db93 9158 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9159 {
ac825856
APB
9160 decl = maybe_access_field (decl, *where_found,
9161 DECL_CONTEXT (decl));
e04a16fb
AG
9162 if (decl == error_mark_node)
9163 return 1;
9164 }
c2952b01 9165
e04a16fb
AG
9166 /* And code for the function call */
9167 if (complete_function_arguments (qual_wfl))
9168 return 1;
c2952b01
APB
9169
9170 /* We might have to setup a new current class and a new this
9171 for the search of an inner class, relative to the type of
9172 a expression resolved as `decl'. The current values are
9173 saved and restored shortly after */
9174 saved_current = current_class;
9175 saved_this = current_this;
dba41d30
APB
9176 if (decl
9177 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9178 || from_qualified_this))
c2952b01 9179 {
dba41d30
APB
9180 /* If we still have `from_qualified_this', we have the form
9181 <T>.this.f() and we need to build <T>.this */
9182 if (from_qualified_this)
9183 {
9184 decl = build_access_to_thisn (current_class, type, 0);
9185 decl = java_complete_tree (decl);
9186 type = TREE_TYPE (TREE_TYPE (decl));
9187 }
c2952b01
APB
9188 current_class = type;
9189 current_this = decl;
dba41d30 9190 from_qualified_this = 0;
c2952b01
APB
9191 }
9192
89e09b9a
PB
9193 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9194 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9195 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9196 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9197 *where_found = patch_method_invocation (qual_wfl, decl, type,
9198 &is_static, &ret_decl);
e04a16fb 9199 if (*where_found == error_mark_node)
c2952b01
APB
9200 {
9201 RESTORE_THIS_AND_CURRENT_CLASS;
9202 return 1;
9203 }
e04a16fb
AG
9204 *type_found = type = QUAL_DECL_TYPE (*where_found);
9205
c2952b01
APB
9206 /* If we're creating an inner class instance, check for that
9207 an enclosing instance is in scope */
9208 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9209 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9210 {
9211 parse_error_context
165f37bc
APB
9212 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9213 lang_printable_name (type, 0),
9214 (!current_this ? "" :
9215 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9216 RESTORE_THIS_AND_CURRENT_CLASS;
9217 return 1;
9218 }
9219
9220 /* In case we had to change then to resolve a inner class
9221 instantiation using a primary qualified by a `new' */
9222 RESTORE_THIS_AND_CURRENT_CLASS;
9223
34d4df06
APB
9224 /* EH check. No check on access$<n> functions */
9225 if (location
9226 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9227 (DECL_NAME (current_function_decl)))
7705e9db
APB
9228 check_thrown_exceptions (location, ret_decl);
9229
e04a16fb
AG
9230 /* If the previous call was static and this one is too,
9231 build a compound expression to hold the two (because in
9232 that case, previous function calls aren't transported as
9233 forcoming function's argument. */
9234 if (previous_call_static && is_static)
9235 {
9236 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9237 TREE_SIDE_EFFECTS (decl) = 1;
9238 }
9239 else
9240 {
9241 previous_call_static = is_static;
9242 decl = *where_found;
9243 }
c2952b01 9244 from_type = 0;
e04a16fb
AG
9245 continue;
9246
d8fccff5 9247 case NEW_ARRAY_EXPR:
c2952b01 9248 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9249 *where_found = decl = java_complete_tree (qual_wfl);
9250 if (decl == error_mark_node)
9251 return 1;
9252 *type_found = type = QUAL_DECL_TYPE (decl);
9253 CLASS_LOADED_P (type) = 1;
9254 continue;
9255
e04a16fb
AG
9256 case CONVERT_EXPR:
9257 *where_found = decl = java_complete_tree (qual_wfl);
9258 if (decl == error_mark_node)
9259 return 1;
9260 *type_found = type = QUAL_DECL_TYPE (decl);
9261 from_cast = 1;
9262 continue;
9263
22eed1e6 9264 case CONDITIONAL_EXPR:
5e942c50 9265 case STRING_CST:
ac22f9cb 9266 case MODIFY_EXPR:
22eed1e6
APB
9267 *where_found = decl = java_complete_tree (qual_wfl);
9268 if (decl == error_mark_node)
9269 return 1;
9270 *type_found = type = QUAL_DECL_TYPE (decl);
9271 continue;
9272
e04a16fb
AG
9273 case ARRAY_REF:
9274 /* If the access to the function call is a non static field,
9275 build the code to access it. */
34f4db93 9276 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9277 {
9278 decl = maybe_access_field (decl, *where_found, type);
9279 if (decl == error_mark_node)
9280 return 1;
9281 }
9282 /* And code for the array reference expression */
9283 decl = java_complete_tree (qual_wfl);
9284 if (decl == error_mark_node)
9285 return 1;
9286 type = QUAL_DECL_TYPE (decl);
9287 continue;
0a2138e2 9288
37feda7d
APB
9289 case PLUS_EXPR:
9290 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9291 return 1;
9292 if ((type = patch_string (decl)))
9293 decl = type;
9294 *where_found = QUAL_RESOLUTION (q) = decl;
9295 *type_found = type = TREE_TYPE (decl);
9296 break;
9297
165f37bc
APB
9298 case CLASS_LITERAL:
9299 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9300 return 1;
9301 *where_found = QUAL_RESOLUTION (q) = decl;
9302 *type_found = type = TREE_TYPE (decl);
9303 break;
9304
0a2138e2
APB
9305 default:
9306 /* Fix for -Wall Just go to the next statement. Don't
9307 continue */
a3f406ce 9308 break;
e04a16fb
AG
9309 }
9310
9311 /* If we fall here, we weren't processing a (static) function call. */
9312 previous_call_static = 0;
9313
9314 /* It can be the keyword THIS */
9315 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9316 {
9317 if (!current_this)
9318 {
9319 parse_error_context
9320 (wfl, "Keyword `this' used outside allowed context");
9321 return 1;
9322 }
f63991a8
APB
9323 if (ctxp->explicit_constructor_p)
9324 {
781b0558 9325 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9326 return 1;
9327 }
e04a16fb 9328 /* We have to generate code for intermediate acess */
c2952b01
APB
9329 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9330 {
9331 *where_found = decl = current_this;
9332 *type_found = type = QUAL_DECL_TYPE (decl);
9333 }
4dbf4496
APB
9334 /* We're trying to access the this from somewhere else. Make sure
9335 it's allowed before doing so. */
c2952b01
APB
9336 else
9337 {
4dbf4496
APB
9338 if (!enclosing_context_p (type, current_class))
9339 {
9340 char *p = xstrdup (lang_printable_name (type, 0));
9341 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9342 p, p,
9343 lang_printable_name (current_class, 0));
9344 free (p);
9345 return 1;
9346 }
c2952b01 9347 from_qualified_this = 1;
dba41d30
APB
9348 /* If there's nothing else after that, we need to
9349 produce something now, otherwise, the section of the
9350 code that needs to produce <T>.this will generate
9351 what is necessary. */
9352 if (!TREE_CHAIN (q))
9353 {
9354 decl = build_access_to_thisn (current_class, type, 0);
9355 *where_found = decl = java_complete_tree (decl);
9356 *type_found = type = TREE_TYPE (decl);
9357 }
c2952b01
APB
9358 }
9359
9360 from_type = 0;
e04a16fb
AG
9361 continue;
9362 }
9363
9364 /* 15.10.2 Accessing Superclass Members using SUPER */
9365 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9366 {
9367 tree node;
9368 /* Check on the restricted use of SUPER */
9369 if (METHOD_STATIC (current_function_decl)
9370 || current_class == object_type_node)
9371 {
9372 parse_error_context
9373 (wfl, "Keyword `super' used outside allowed context");
9374 return 1;
9375 }
9376 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9377 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9378 CLASSTYPE_SUPER (current_class),
9379 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9380 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9381 if (decl == error_mark_node)
9382 return 1;
e04a16fb
AG
9383 *type_found = type = QUAL_DECL_TYPE (decl);
9384 from_super = from_type = 1;
9385 continue;
9386 }
9387
9388 /* 15.13.1: Can't search for field name in packages, so we
9389 assume a variable/class name was meant. */
9390 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9391 {
5e942c50
APB
9392 tree name = resolve_package (wfl, &q);
9393 if (name)
9394 {
c2952b01 9395 tree list;
5e942c50 9396 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9397 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9398 out. We're going to search something inside it. */
9399 *type_found = type = TREE_TYPE (decl);
9400 layout_class (type);
9401 from_type = 1;
c2952b01 9402
dde1da72
APB
9403 /* Fix them all the way down, if any are left. */
9404 if (q)
c2952b01 9405 {
dde1da72
APB
9406 list = TREE_CHAIN (q);
9407 while (list)
9408 {
9409 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9410 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9411 list = TREE_CHAIN (list);
9412 }
c2952b01 9413 }
5e942c50 9414 }
e04a16fb 9415 else
5e942c50
APB
9416 {
9417 if (from_super || from_cast)
9418 parse_error_context
9419 ((from_cast ? qual_wfl : wfl),
9420 "No variable `%s' defined in class `%s'",
9421 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9422 lang_printable_name (type, 0));
9423 else
9424 parse_error_context
9425 (qual_wfl, "Undefined variable or class name: `%s'",
9426 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9427 return 1;
9428 }
e04a16fb
AG
9429 }
9430
9431 /* We have a type name. It's been already resolved when the
9432 expression was qualified. */
9433 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9434 {
9435 if (!(decl = QUAL_RESOLUTION (q)))
9436 return 1; /* Error reported already */
9437
c2952b01
APB
9438 /* Sneak preview. If next we see a `new', we're facing a
9439 qualification with resulted in a type being selected
9440 instead of a field. Report the error */
9441 if(TREE_CHAIN (q)
9442 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9443 {
9444 parse_error_context (qual_wfl, "Undefined variable `%s'",
9445 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9446 return 1;
9447 }
9448
e04a16fb
AG
9449 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9450 {
9451 parse_error_context
9452 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9453 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9454 GET_TYPE_NAME (type),
e04a16fb
AG
9455 IDENTIFIER_POINTER (DECL_NAME (decl)),
9456 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9457 return 1;
9458 }
5e942c50 9459 check_deprecation (qual_wfl, decl);
c2952b01 9460
e04a16fb
AG
9461 type = TREE_TYPE (decl);
9462 from_type = 1;
9463 }
9464 /* We resolve and expression name */
9465 else
9466 {
cd531a2e 9467 tree field_decl = NULL_TREE;
e04a16fb
AG
9468
9469 /* If there exists an early resolution, use it. That occurs
9470 only once and we know that there are more things to
9471 come. Don't do that when processing something after SUPER
9472 (we need more thing to be put in place below */
9473 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9474 {
9475 decl = QUAL_RESOLUTION (q);
c877974e 9476 if (!type)
5e942c50 9477 {
7f10c2e2
APB
9478 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9479 {
9480 if (current_this)
9481 *where_found = current_this;
9482 else
9483 {
9484 static_ref_err (qual_wfl, DECL_NAME (decl),
9485 current_class);
9486 return 1;
9487 }
f0f3a777
APB
9488 if (outer_field_access_p (current_class, decl))
9489 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9490 }
c877974e
APB
9491 else
9492 {
9493 *where_found = TREE_TYPE (decl);
9494 if (TREE_CODE (*where_found) == POINTER_TYPE)
9495 *where_found = TREE_TYPE (*where_found);
9496 }
5e942c50 9497 }
b67d701b 9498 }
e04a16fb
AG
9499
9500 /* We have to search for a field, knowing the type of its
9501 container. The flag FROM_TYPE indicates that we resolved
9502 the last member of the expression as a type name, which
5e942c50
APB
9503 means that for the resolution of this field, we'll look
9504 for other errors than if it was resolved as a member of
9505 an other field. */
e04a16fb
AG
9506 else
9507 {
9508 int is_static;
5e942c50
APB
9509 tree field_decl_type; /* For layout */
9510
e04a16fb
AG
9511 if (!from_type && !JREFERENCE_TYPE_P (type))
9512 {
9513 parse_error_context
9514 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9515 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9516 lang_printable_name (type, 0),
e04a16fb
AG
9517 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9518 return 1;
9519 }
9520
dc0b3eff
PB
9521 field_decl = lookup_field_wrapper (type,
9522 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9523
9524 /* Maybe what we're trying to access an inner class. */
9525 if (!field_decl)
9526 {
9527 tree ptr, inner_decl;
9528
9529 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9530 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9531 if (inner_decl)
9532 {
9533 check_inner_class_access (inner_decl, decl, qual_wfl);
9534 type = TREE_TYPE (inner_decl);
9535 decl = inner_decl;
9536 from_type = 1;
9537 continue;
9538 }
9539 }
9540
dc0b3eff 9541 if (field_decl == NULL_TREE)
e04a16fb
AG
9542 {
9543 parse_error_context
2aa11e97 9544 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9545 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9546 GET_TYPE_NAME (type));
e04a16fb
AG
9547 return 1;
9548 }
dc0b3eff
PB
9549 if (field_decl == error_mark_node)
9550 return 1;
5e942c50
APB
9551
9552 /* Layout the type of field_decl, since we may need
c877974e
APB
9553 it. Don't do primitive types or loaded classes. The
9554 situation of non primitive arrays may not handled
9555 properly here. FIXME */
5e942c50
APB
9556 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9557 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9558 else
9559 field_decl_type = TREE_TYPE (field_decl);
9560 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9561 && !CLASS_LOADED_P (field_decl_type)
9562 && !TYPE_ARRAY_P (field_decl_type))
9563 resolve_and_layout (field_decl_type, NULL_TREE);
9564 if (TYPE_ARRAY_P (field_decl_type))
9565 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9566
9567 /* Check on accessibility here */
9568 if (not_accessible_p (type, field_decl, from_super))
9569 {
9570 parse_error_context
9571 (qual_wfl,
9572 "Can't access %s field `%s.%s' from `%s'",
9573 java_accstring_lookup
9574 (get_access_flags_from_decl (field_decl)),
2aa11e97 9575 GET_TYPE_NAME (type),
e04a16fb
AG
9576 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9577 IDENTIFIER_POINTER
9578 (DECL_NAME (TYPE_NAME (current_class))));
9579 return 1;
9580 }
5e942c50 9581 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9582
9583 /* There are things to check when fields are accessed
9584 from type. There are no restrictions on a static
9585 declaration of the field when it is accessed from an
9586 interface */
9587 is_static = FIELD_STATIC (field_decl);
9588 if (!from_super && from_type
c2952b01
APB
9589 && !TYPE_INTERFACE_P (type)
9590 && !is_static
9591 && (current_function_decl
9592 && METHOD_STATIC (current_function_decl)))
e04a16fb 9593 {
7f10c2e2 9594 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9595 return 1;
9596 }
9597 from_cast = from_super = 0;
9598
c2952b01
APB
9599 /* It's an access from a type but it isn't static, we
9600 make it relative to `this'. */
9601 if (!is_static && from_type)
9602 decl = current_this;
9603
5e942c50
APB
9604 /* If we need to generate something to get a proper
9605 handle on what this field is accessed from, do it
9606 now. */
e04a16fb
AG
9607 if (!is_static)
9608 {
c583dd46 9609 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9610 if (decl == error_mark_node)
9611 return 1;
9612 }
9613
9614 /* We want to keep the location were found it, and the type
9615 we found. */
9616 *where_found = decl;
9617 *type_found = type;
9618
c2952b01
APB
9619 /* Generate the correct expression for field access from
9620 qualified this */
9621 if (from_qualified_this)
9622 {
9623 field_decl = build_outer_field_access (qual_wfl, field_decl);
9624 from_qualified_this = 0;
9625 }
9626
e04a16fb
AG
9627 /* This is the decl found and eventually the next one to
9628 search from */
9629 decl = field_decl;
9630 }
e04a16fb
AG
9631 from_type = 0;
9632 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9633
9634 /* Sneak preview. If decl is qualified by a `new', report
9635 the error here to be accurate on the peculiar construct */
9636 if (TREE_CHAIN (q)
9637 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9638 && !JREFERENCE_TYPE_P (type))
9639 {
9640 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9641 lang_printable_name (type, 0));
9642 return 1;
9643 }
e04a16fb 9644 }
dde1da72
APB
9645 /* `q' might have changed due to a after package resolution
9646 re-qualification */
9647 if (!q)
9648 break;
e04a16fb
AG
9649 }
9650 *found_decl = decl;
9651 return 0;
9652}
9653
9654/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9655 can't be accessed from REFERENCE (a record type). This should be
9656 used when decl is a field or a method.*/
e04a16fb 9657
be245ac0
KG
9658static int
9659not_accessible_p (reference, member, from_super)
e04a16fb
AG
9660 tree reference, member;
9661 int from_super;
9662{
9663 int access_flag = get_access_flags_from_decl (member);
9664
4dbf4496
APB
9665 /* Inner classes are processed by check_inner_class_access */
9666 if (INNER_CLASS_TYPE_P (reference))
9667 return 0;
9668
e04a16fb
AG
9669 /* Access always granted for members declared public */
9670 if (access_flag & ACC_PUBLIC)
9671 return 0;
9672
9673 /* Check access on protected members */
9674 if (access_flag & ACC_PROTECTED)
9675 {
9676 /* Access granted if it occurs from within the package
9677 containing the class in which the protected member is
9678 declared */
9679 if (class_in_current_package (DECL_CONTEXT (member)))
9680 return 0;
9681
9bbc7d9f
PB
9682 /* If accessed with the form `super.member', then access is granted */
9683 if (from_super)
9684 return 0;
e04a16fb 9685
9bbc7d9f 9686 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9687 member is declared or a subclass of it. Find the right
9688 context to perform the check */
9689 if (PURE_INNER_CLASS_TYPE_P (reference))
9690 {
9691 while (INNER_CLASS_TYPE_P (reference))
9692 {
9693 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9694 return 0;
9695 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9696 }
9697 }
473e7b07 9698 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9699 return 0;
e04a16fb
AG
9700 return 1;
9701 }
9702
9703 /* Check access on private members. Access is granted only if it
761491c8
APB
9704 occurs from within the class in which it is declared -- that does
9705 it for innerclasses too. */
e04a16fb 9706 if (access_flag & ACC_PRIVATE)
761491c8
APB
9707 {
9708 if (reference == DECL_CONTEXT (member))
9709 return 0;
9710 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9711 return 0;
9712 return 1;
9713 }
e04a16fb
AG
9714
9715 /* Default access are permitted only when occuring within the
9716 package in which the type (REFERENCE) is declared. In other words,
9717 REFERENCE is defined in the current package */
9718 if (ctxp->package)
9719 return !class_in_current_package (reference);
473e7b07 9720
e04a16fb
AG
9721 /* Otherwise, access is granted */
9722 return 0;
9723}
9724
5e942c50
APB
9725/* Test deprecated decl access. */
9726static void
9727check_deprecation (wfl, decl)
9728 tree wfl, decl;
9729{
49f48c71 9730 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9731 /* Complain if the field is deprecated and the file it was defined
9732 in isn't compiled at the same time the file which contains its
9733 use is */
9734 if (DECL_DEPRECATED (decl)
9735 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9736 {
9737 char the [20];
9738 switch (TREE_CODE (decl))
9739 {
9740 case FUNCTION_DECL:
9741 strcpy (the, "method");
9742 break;
9743 case FIELD_DECL:
9744 strcpy (the, "field");
9745 break;
9746 case TYPE_DECL:
9747 strcpy (the, "class");
9748 break;
15fdcfe9
PB
9749 default:
9750 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9751 }
9752 parse_warning_context
9753 (wfl, "The %s `%s' in class `%s' has been deprecated",
9754 the, lang_printable_name (decl, 0),
9755 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9756 }
9757}
9758
e04a16fb
AG
9759/* Returns 1 if class was declared in the current package, 0 otherwise */
9760
9761static int
9762class_in_current_package (class)
9763 tree class;
9764{
9765 static tree cache = NULL_TREE;
9766 int qualified_flag;
9767 tree left;
9768
9769 if (cache == class)
9770 return 1;
9771
9772 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9773
9774 /* If the current package is empty and the name of CLASS is
9775 qualified, class isn't in the current package. If there is a
9776 current package and the name of the CLASS is not qualified, class
9777 isn't in the current package */
0a2138e2 9778 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9779 return 0;
9780
9781 /* If there is not package and the name of CLASS isn't qualified,
9782 they belong to the same unnamed package */
9783 if (!ctxp->package && !qualified_flag)
9784 return 1;
9785
9786 /* Compare the left part of the name of CLASS with the package name */
9787 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9788 if (ctxp->package == left)
9789 {
19e223db
MM
9790 static int initialized_p;
9791 /* Register CACHE with the garbage collector. */
9792 if (!initialized_p)
9793 {
9794 ggc_add_tree_root (&cache, 1);
9795 initialized_p = 1;
9796 }
9797
e04a16fb
AG
9798 cache = class;
9799 return 1;
9800 }
9801 return 0;
9802}
9803
9804/* This function may generate code to access DECL from WHERE. This is
9805 done only if certain conditions meet. */
9806
9807static tree
9808maybe_access_field (decl, where, type)
9809 tree decl, where, type;
9810{
5e942c50
APB
9811 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9812 && !FIELD_STATIC (decl))
e04a16fb 9813 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9814 (type ? type : DECL_CONTEXT (decl)),
9815 DECL_NAME (decl));
e04a16fb
AG
9816 return decl;
9817}
9818
15fdcfe9 9819/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9820 and according to the situation, PRIMARY and WHERE may be
9821 used. IS_STATIC is set to 1 if the invoked function is static. */
9822
9823static tree
89e09b9a 9824patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9825 tree patch, primary, where;
9826 int *is_static;
b9f7e36c 9827 tree *ret_decl;
e04a16fb
AG
9828{
9829 tree wfl = TREE_OPERAND (patch, 0);
9830 tree args = TREE_OPERAND (patch, 1);
9831 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9832 tree list;
22eed1e6 9833 int is_static_flag = 0;
89e09b9a 9834 int is_super_init = 0;
bccaf73a 9835 tree this_arg = NULL_TREE;
e04a16fb
AG
9836
9837 /* Should be overriden if everything goes well. Otherwise, if
9838 something fails, it should keep this value. It stop the
9839 evaluation of a bogus assignment. See java_complete_tree,
9840 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9841 evaluating an assignment */
9842 TREE_TYPE (patch) = error_mark_node;
9843
9844 /* Since lookup functions are messing with line numbers, save the
9845 context now. */
9846 java_parser_context_save_global ();
9847
9848 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9849
9850 /* Resolution of qualified name, excluding constructors */
9851 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9852 {
dde1da72 9853 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9854 /* Extract the last IDENTIFIER of the qualified
9855 expression. This is a wfl and we will use it's location
9856 data during error report. */
9857 identifier_wfl = cut_identifier_in_qualified (wfl);
9858 identifier = EXPR_WFL_NODE (identifier_wfl);
9859
9860 /* Given the context, IDENTIFIER is syntactically qualified
9861 as a MethodName. We need to qualify what's before */
9862 qualify_ambiguous_name (wfl);
dde1da72 9863 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9864
dde1da72
APB
9865 if (resolved == error_mark_node)
9866 PATCH_METHOD_RETURN_ERROR ();
9867
9868 type = GET_SKIP_TYPE (resolved);
9869 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9870
9871 if (JPRIMITIVE_TYPE_P (type))
9872 {
7e51098e
TT
9873 parse_error_context
9874 (identifier_wfl,
9875 "Can't invoke a method on primitive type `%s'",
9876 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9877 PATCH_METHOD_RETURN_ERROR ();
9878 }
9879
dde1da72
APB
9880 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9881 args = nreverse (args);
2c56429a 9882
e04a16fb 9883 /* We're resolving a call from a type */
dde1da72 9884 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9885 {
dde1da72 9886 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9887 {
9888 parse_error_context
781b0558
KG
9889 (identifier_wfl,
9890 "Can't make static reference to method `%s' in interface `%s'",
9891 IDENTIFIER_POINTER (identifier),
e04a16fb 9892 IDENTIFIER_POINTER (name));
b9f7e36c 9893 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9894 }
e04a16fb
AG
9895 if (list && !METHOD_STATIC (list))
9896 {
c2e3db92 9897 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9898 parse_error_context
9899 (identifier_wfl,
9900 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9901 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9902 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9903 free (fct_name);
b9f7e36c 9904 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9905 }
9906 }
e04a16fb 9907 else
dde1da72
APB
9908 this_arg = primary = resolved;
9909
5e942c50 9910 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9911 wfl = identifier_wfl;
9912 }
9913 /* Resolution of simple names, names generated after a primary: or
9914 constructors */
9915 else
9916 {
cd531a2e 9917 tree class_to_search = NULL_TREE;
c2952b01 9918 int lc; /* Looking for Constructor */
e04a16fb
AG
9919
9920 /* We search constructor in their target class */
9921 if (CALL_CONSTRUCTOR_P (patch))
9922 {
22eed1e6
APB
9923 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9924 class_to_search = EXPR_WFL_NODE (wfl);
9925 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9926 this_identifier_node)
9927 class_to_search = NULL_TREE;
9928 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9929 super_identifier_node)
e04a16fb 9930 {
89e09b9a 9931 is_super_init = 1;
22eed1e6
APB
9932 if (CLASSTYPE_SUPER (current_class))
9933 class_to_search =
9934 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9935 else
9936 {
781b0558 9937 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9938 PATCH_METHOD_RETURN_ERROR ();
9939 }
e04a16fb 9940 }
22eed1e6
APB
9941
9942 /* Class to search is NULL if we're searching the current one */
9943 if (class_to_search)
e04a16fb 9944 {
c2952b01
APB
9945 class_to_search = resolve_and_layout (class_to_search, wfl);
9946
22eed1e6
APB
9947 if (!class_to_search)
9948 {
9949 parse_error_context
9950 (wfl, "Class `%s' not found in type declaration",
9951 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9952 PATCH_METHOD_RETURN_ERROR ();
9953 }
9954
5e942c50
APB
9955 /* Can't instantiate an abstract class, but we can
9956 invoke it's constructor. It's use within the `new'
9957 context is denied here. */
9958 if (CLASS_ABSTRACT (class_to_search)
9959 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9960 {
9961 parse_error_context
781b0558
KG
9962 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9963 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9964 PATCH_METHOD_RETURN_ERROR ();
9965 }
c2952b01 9966
22eed1e6 9967 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9968 }
22eed1e6
APB
9969 else
9970 class_to_search = current_class;
e04a16fb
AG
9971 lc = 1;
9972 }
9973 /* This is a regular search in the local class, unless an
9974 alternate class is specified. */
9975 else
9976 {
9977 class_to_search = (where ? where : current_class);
9978 lc = 0;
9979 }
c2952b01 9980
e04a16fb
AG
9981 /* NAME is a simple identifier or comes from a primary. Search
9982 in the class whose declaration contain the method being
9983 invoked. */
c877974e 9984 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9985
c2952b01 9986 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9987 /* Don't continue if no method were found, as the next statement
9988 can't be executed then. */
b9f7e36c
APB
9989 if (!list)
9990 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9991
9992 /* Check for static reference if non static methods */
9993 if (check_for_static_method_reference (wfl, patch, list,
9994 class_to_search, primary))
b9f7e36c 9995 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9996
165f37bc
APB
9997 /* Check for inner classes creation from illegal contexts */
9998 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9999 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10000 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
10001 {
10002 parse_error_context
10003 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10004 lang_printable_name (class_to_search, 0),
10005 (!current_this ? "" :
10006 "; an explicit one must be provided when creating this inner class"));
10007 PATCH_METHOD_RETURN_ERROR ();
10008 }
10009
22eed1e6
APB
10010 /* Non static methods are called with the current object extra
10011 argument. If patch a `new TYPE()', the argument is the value
10012 returned by the object allocator. If method is resolved as a
10013 primary, use the primary otherwise use the current THIS. */
b9f7e36c 10014 args = nreverse (args);
bccaf73a 10015 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
10016 {
10017 this_arg = primary ? primary : current_this;
10018
10019 /* If we're using an access method, things are different.
10020 There are two familly of cases:
10021
10022 1) We're not generating bytecodes:
10023
10024 - LIST is non static. It's invocation is transformed from
10025 x(a1,...,an) into this$<n>.x(a1,....an).
10026 - LIST is static. It's invocation is transformed from
10027 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10028
10029 2) We're generating bytecodes:
10030
10031 - LIST is non static. It's invocation is transformed from
10032 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10033 - LIST is static. It's invocation is transformed from
10034 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
10035
10036 Of course, this$<n> can be abitrary complex, ranging from
10037 this$0 (the immediate outer context) to
10038 access$0(access$0(...(this$0))).
10039
10040 maybe_use_access_method returns a non zero value if the
dfb99c83 10041 this_arg has to be moved into the (then generated) stub
4dbf4496 10042 argument list. In the meantime, the selected function
dfb99c83 10043 might have be replaced by a generated stub. */
c2952b01 10044 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
10045 {
10046 args = tree_cons (NULL_TREE, this_arg, args);
10047 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10048 }
c2952b01 10049 }
e04a16fb 10050 }
b67d701b 10051
e04a16fb
AG
10052 /* Merge point of all resolution schemes. If we have nothing, this
10053 is an error, already signaled */
b9f7e36c
APB
10054 if (!list)
10055 PATCH_METHOD_RETURN_ERROR ();
b67d701b 10056
e04a16fb
AG
10057 /* Check accessibility, position the is_static flag, build and
10058 return the call */
9bbc7d9f 10059 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 10060 {
c2e3db92 10061 char *fct_name = xstrdup (lang_printable_name (list, 0));
761491c8 10062 int ctor_p = DECL_CONSTRUCTOR_P (list);
e04a16fb 10063 parse_error_context
761491c8 10064 (wfl, "Can't access %s %s `%s%s.%s' from `%s'",
e04a16fb 10065 java_accstring_lookup (get_access_flags_from_decl (list)),
761491c8
APB
10066 (ctor_p ? "constructor" : "method"),
10067 (ctor_p ?
10068 "" : lang_printable_name_wls (TREE_TYPE (TREE_TYPE (list)), 0)),
5e942c50
APB
10069 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
10070 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 10071 free (fct_name);
b9f7e36c 10072 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 10073 }
5e942c50 10074 check_deprecation (wfl, list);
22eed1e6 10075
c2952b01
APB
10076 /* If invoking a innerclass constructor, there are hidden parameters
10077 to pass */
10078 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10079 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10080 {
10081 /* And make sure we add the accessed local variables to be saved
10082 in field aliases. */
10083 args = build_alias_initializer_parameter_list
10084 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10085
da632f2c 10086 /* Secretly pass the current_this/primary as a second argument */
165f37bc 10087 if (primary || current_this)
f8b93ea7
APB
10088 {
10089 tree extra_arg;
10090 tree this_type = (current_this ?
10091 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10092 /* Method's (list) enclosing context */
10093 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10094 /* If we have a primary, use it. */
10095 if (primary)
10096 extra_arg = primary;
10097 /* The current `this' is an inner class but isn't a direct
10098 enclosing context for the inner class we're trying to
10099 create. Build an access to the proper enclosing context
10100 and use it. */
10101 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10102 && this_type != TREE_TYPE (mec))
10103 {
10104
10105 extra_arg = build_access_to_thisn (current_class,
10106 TREE_TYPE (mec), 0);
10107 extra_arg = java_complete_tree (extra_arg);
10108 }
10109 /* Otherwise, just use the current `this' as an enclosing
10110 context. */
10111 else
10112 extra_arg = current_this;
10113 args = tree_cons (NULL_TREE, extra_arg, args);
10114 }
165f37bc
APB
10115 else
10116 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10117 }
10118
152de068
APB
10119 /* This handles the situation where a constructor invocation needs
10120 to have an enclosing context passed as a second parameter (the
10121 constructor is one of an inner class. We extract it from the
10122 current function. */
10123 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10124 {
10125 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10126 tree extra_arg;
10127
10128 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10129 {
10130 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10131 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10132 }
10133 else
10134 {
10135 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10136 extra_arg =
10137 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10138 extra_arg = java_complete_tree (extra_arg);
10139 }
10140 args = tree_cons (NULL_TREE, extra_arg, args);
10141 }
10142
22eed1e6 10143 is_static_flag = METHOD_STATIC (list);
dba41d30 10144 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10145 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10146
c3f2a476
APB
10147 /* In the context of an explicit constructor invocation, we can't
10148 invoke any method relying on `this'. Exceptions are: we're
10149 invoking a static function, primary exists and is not the current
10150 this, we're creating a new object. */
22eed1e6 10151 if (ctxp->explicit_constructor_p
c3f2a476
APB
10152 && !is_static_flag
10153 && (!primary || primary == current_this)
10154 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10155 {
781b0558 10156 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10157 PATCH_METHOD_RETURN_ERROR ();
10158 }
e04a16fb 10159 java_parser_context_restore_global ();
22eed1e6
APB
10160 if (is_static)
10161 *is_static = is_static_flag;
b9f7e36c
APB
10162 /* Sometimes, we want the decl of the selected method. Such as for
10163 EH checking */
10164 if (ret_decl)
10165 *ret_decl = list;
89e09b9a
PB
10166 patch = patch_invoke (patch, list, args);
10167 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10168 {
c2952b01
APB
10169 tree finit_parms, finit_call;
10170
c00f0fb2 10171 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10172 finit_parms = build_alias_initializer_parameter_list
10173 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10174
c2952b01
APB
10175 finit_call =
10176 build_method_invocation (build_wfl_node (finit_identifier_node),
10177 finit_parms);
10178
10179 /* Generate the code used to initialize fields declared with an
10180 initialization statement and build a compound statement along
10181 with the super constructor invocation. */
89e09b9a
PB
10182 patch = build (COMPOUND_EXPR, void_type_node, patch,
10183 java_complete_tree (finit_call));
10184 CAN_COMPLETE_NORMALLY (patch) = 1;
10185 }
10186 return patch;
e04a16fb
AG
10187}
10188
10189/* Check that we're not trying to do a static reference to a method in
10190 non static method. Return 1 if it's the case, 0 otherwise. */
10191
10192static int
10193check_for_static_method_reference (wfl, node, method, where, primary)
10194 tree wfl, node, method, where, primary;
10195{
10196 if (METHOD_STATIC (current_function_decl)
10197 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10198 {
c2e3db92 10199 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10200 parse_error_context
10201 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10202 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10203 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10204 free (fct_name);
10205 return 1;
10206 }
10207 return 0;
10208}
10209
c2952b01
APB
10210/* Fix the invocation of *MDECL if necessary in the case of a
10211 invocation from an inner class. *THIS_ARG might be modified
10212 appropriately and an alternative access to *MDECL might be
10213 returned. */
10214
10215static int
10216maybe_use_access_method (is_super_init, mdecl, this_arg)
10217 int is_super_init;
10218 tree *mdecl, *this_arg;
10219{
10220 tree ctx;
10221 tree md = *mdecl, ta = *this_arg;
10222 int to_return = 0;
10223 int non_static_context = !METHOD_STATIC (md);
10224
10225 if (is_super_init
165f37bc
APB
10226 || DECL_CONTEXT (md) == current_class
10227 || !PURE_INNER_CLASS_TYPE_P (current_class)
10228 || DECL_FINIT_P (md))
c2952b01
APB
10229 return 0;
10230
10231 /* If we're calling a method found in an enclosing class, generate
10232 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10233 invoking a static method. Note that if MD's type is unrelated to
10234 CURRENT_CLASS, then the current this can be used. */
c2952b01 10235
2cb3951d 10236 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10237 {
10238 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10239 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10240 {
10241 ta = build_current_thisn (current_class);
10242 ta = build_wfl_node (ta);
10243 }
10244 else
10245 {
10246 tree type = ctx;
10247 while (type)
10248 {
10249 maybe_build_thisn_access_method (type);
4dbf4496 10250 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10251 {
10252 ta = build_access_to_thisn (ctx, type, 0);
10253 break;
10254 }
10255 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10256 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10257 }
10258 }
10259 ta = java_complete_tree (ta);
10260 }
10261
10262 /* We might have to use an access method to get to MD. We can
10263 break the method access rule as far as we're not generating
10264 bytecode */
10265 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10266 {
10267 md = build_outer_method_access_method (md);
10268 to_return = 1;
10269 }
10270
10271 *mdecl = md;
10272 *this_arg = ta;
10273
10274 /* Returnin a non zero value indicates we were doing a non static
10275 method invokation that is now a static invocation. It will have
10276 callee displace `this' to insert it in the regular argument
10277 list. */
10278 return (non_static_context && to_return);
10279}
10280
e04a16fb
AG
10281/* Patch an invoke expression METHOD and ARGS, based on its invocation
10282 mode. */
10283
10284static tree
89e09b9a 10285patch_invoke (patch, method, args)
e04a16fb 10286 tree patch, method, args;
e04a16fb
AG
10287{
10288 tree dtable, func;
0a2138e2 10289 tree original_call, t, ta;
e815887f 10290 tree cond = NULL_TREE;
e04a16fb 10291
5e942c50
APB
10292 /* Last step for args: convert build-in types. If we're dealing with
10293 a new TYPE() type call, the first argument to the constructor
e815887f 10294 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10295 `new' */
10296 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10297 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10298 t = TREE_CHAIN (t);
ac825856
APB
10299 for (ta = args; t != end_params_node && ta;
10300 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10301 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10302 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10303 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10304
10305 /* Resolve unresolved returned type isses */
10306 t = TREE_TYPE (TREE_TYPE (method));
10307 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10308 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10309
e8fc7396 10310 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10311 func = method;
10312 else
e04a16fb 10313 {
15fdcfe9 10314 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10315 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10316 {
10317 case INVOKE_VIRTUAL:
10318 dtable = invoke_build_dtable (0, args);
10319 func = build_invokevirtual (dtable, method);
10320 break;
b9f7e36c 10321
e815887f
TT
10322 case INVOKE_NONVIRTUAL:
10323 /* If the object for the method call is null, we throw an
10324 exception. We don't do this if the object is the current
10325 method's `this'. In other cases we just rely on an
10326 optimization pass to eliminate redundant checks. */
10327 if (TREE_VALUE (args) != current_this)
10328 {
10329 /* We use a SAVE_EXPR here to make sure we only evaluate
10330 the new `self' expression once. */
10331 tree save_arg = save_expr (TREE_VALUE (args));
10332 TREE_VALUE (args) = save_arg;
10333 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10334 null_pointer_node);
10335 }
10336 /* Fall through. */
10337
15fdcfe9
PB
10338 case INVOKE_SUPER:
10339 case INVOKE_STATIC:
10340 func = build_known_method_ref (method, TREE_TYPE (method),
10341 DECL_CONTEXT (method),
10342 signature, args);
10343 break;
e04a16fb 10344
15fdcfe9
PB
10345 case INVOKE_INTERFACE:
10346 dtable = invoke_build_dtable (1, args);
173f556c 10347 func = build_invokeinterface (dtable, method);
15fdcfe9 10348 break;
5e942c50 10349
15fdcfe9 10350 default:
89e09b9a 10351 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10352 }
10353
10354 /* Ensure self_type is initialized, (invokestatic). FIXME */
10355 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10356 }
10357
e04a16fb
AG
10358 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10359 TREE_OPERAND (patch, 0) = func;
10360 TREE_OPERAND (patch, 1) = args;
10361 original_call = patch;
10362
e815887f 10363 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10364 returned value is the first argument to the constructor. We build
10365 a COMPOUND_EXPR and use saved expression so that the overall NEW
10366 expression value is a pointer to a newly created and initialized
10367 class. */
10368 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10369 {
10370 tree class = DECL_CONTEXT (method);
10371 tree c1, saved_new, size, new;
e8fc7396 10372 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10373 {
10374 TREE_TYPE (patch) = build_pointer_type (class);
10375 return patch;
10376 }
e04a16fb
AG
10377 if (!TYPE_SIZE (class))
10378 safe_layout_class (class);
10379 size = size_in_bytes (class);
10380 new = build (CALL_EXPR, promote_type (class),
10381 build_address_of (alloc_object_node),
10382 tree_cons (NULL_TREE, build_class_ref (class),
10383 build_tree_list (NULL_TREE,
10384 size_in_bytes (class))),
10385 NULL_TREE);
10386 saved_new = save_expr (new);
10387 c1 = build_tree_list (NULL_TREE, saved_new);
10388 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10389 TREE_OPERAND (original_call, 1) = c1;
10390 TREE_SET_CODE (original_call, CALL_EXPR);
10391 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10392 }
e815887f
TT
10393
10394 /* If COND is set, then we are building a check to see if the object
10395 is NULL. */
10396 if (cond != NULL_TREE)
10397 {
10398 /* We have to make the `then' branch a compound expression to
10399 make the types turn out right. This seems bizarre. */
10400 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10401 build (COMPOUND_EXPR, TREE_TYPE (patch),
10402 build (CALL_EXPR, void_type_node,
10403 build_address_of (soft_nullpointer_node),
10404 NULL_TREE, NULL_TREE),
10405 (FLOAT_TYPE_P (TREE_TYPE (patch))
10406 ? build_real (TREE_TYPE (patch), dconst0)
10407 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10408 integer_zero_node))),
10409 patch);
10410 TREE_SIDE_EFFECTS (patch) = 1;
10411 }
10412
e04a16fb
AG
10413 return patch;
10414}
10415
10416static int
10417invocation_mode (method, super)
10418 tree method;
10419 int super;
10420{
10421 int access = get_access_flags_from_decl (method);
10422
22eed1e6
APB
10423 if (super)
10424 return INVOKE_SUPER;
10425
e815887f 10426 if (access & ACC_STATIC)
e04a16fb
AG
10427 return INVOKE_STATIC;
10428
e815887f
TT
10429 /* We have to look for a constructor before we handle nonvirtual
10430 calls; otherwise the constructor will look nonvirtual. */
10431 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10432 return INVOKE_STATIC;
e815887f
TT
10433
10434 if (access & ACC_FINAL || access & ACC_PRIVATE)
10435 return INVOKE_NONVIRTUAL;
10436
10437 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10438 return INVOKE_NONVIRTUAL;
10439
e04a16fb
AG
10440 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10441 return INVOKE_INTERFACE;
22eed1e6 10442
e04a16fb
AG
10443 return INVOKE_VIRTUAL;
10444}
10445
b67d701b
PB
10446/* Retrieve a refined list of matching methods. It covers the step
10447 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10448
10449static tree
10450lookup_method_invoke (lc, cl, class, name, arg_list)
10451 int lc;
10452 tree cl;
10453 tree class, name, arg_list;
10454{
de4c7b02 10455 tree atl = end_params_node; /* Arg Type List */
c877974e 10456 tree method, signature, list, node;
49f48c71 10457 const char *candidates; /* Used for error report */
b5b8a0e7 10458 char *dup;
e04a16fb 10459
5e942c50 10460 /* Fix the arguments */
e04a16fb
AG
10461 for (node = arg_list; node; node = TREE_CHAIN (node))
10462 {
e3884b71 10463 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10464 /* Non primitive type may have to be resolved */
e3884b71 10465 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10466 resolve_and_layout (current_arg, NULL_TREE);
10467 /* And promoted */
b67d701b 10468 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10469 current_arg = promote_type (current_arg);
5e942c50 10470 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10471 }
e04a16fb 10472
c2952b01
APB
10473 /* Presto. If we're dealing with an anonymous class and a
10474 constructor call, generate the right constructor now, since we
10475 know the arguments' types. */
10476
10477 if (lc && ANONYMOUS_CLASS_P (class))
10478 craft_constructor (TYPE_NAME (class), atl);
10479
5e942c50
APB
10480 /* Find all candidates and then refine the list, searching for the
10481 most specific method. */
10482 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10483 list = find_most_specific_methods_list (list);
b67d701b
PB
10484 if (list && !TREE_CHAIN (list))
10485 return TREE_VALUE (list);
e04a16fb 10486
b67d701b
PB
10487 /* Issue an error. List candidates if any. Candidates are listed
10488 only if accessible (non accessible methods may end-up here for
10489 the sake of a better error report). */
10490 candidates = NULL;
10491 if (list)
e04a16fb 10492 {
e04a16fb 10493 tree current;
b67d701b 10494 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10495 for (current = list; current; current = TREE_CHAIN (current))
10496 {
b67d701b
PB
10497 tree cm = TREE_VALUE (current);
10498 char string [4096];
10499 if (!cm || not_accessible_p (class, cm, 0))
10500 continue;
b67d701b 10501 sprintf
22eed1e6
APB
10502 (string, " `%s' in `%s'%s",
10503 get_printable_method_name (cm),
b67d701b
PB
10504 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10505 (TREE_CHAIN (current) ? "\n" : ""));
10506 obstack_grow (&temporary_obstack, string, strlen (string));
10507 }
10508 obstack_1grow (&temporary_obstack, '\0');
10509 candidates = obstack_finish (&temporary_obstack);
10510 }
10511 /* Issue the error message */
c877974e
APB
10512 method = make_node (FUNCTION_TYPE);
10513 TYPE_ARG_TYPES (method) = atl;
b67d701b 10514 signature = build_java_argument_signature (method);
c63b98cd 10515 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10516 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10517 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10518 (lc ? dup : IDENTIFIER_POINTER (name)),
10519 IDENTIFIER_POINTER (signature), dup,
b67d701b 10520 (candidates ? candidates : ""));
b5b8a0e7 10521 free (dup);
b67d701b
PB
10522 return NULL_TREE;
10523}
10524
5e942c50
APB
10525/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10526 when we're looking for a constructor. */
b67d701b
PB
10527
10528static tree
5e942c50
APB
10529find_applicable_accessible_methods_list (lc, class, name, arglist)
10530 int lc;
b67d701b
PB
10531 tree class, name, arglist;
10532{
ad69b5b6
BM
10533 static struct hash_table t, *searched_classes = NULL;
10534 static int search_not_done = 0;
b67d701b
PB
10535 tree list = NULL_TREE, all_list = NULL_TREE;
10536
ad69b5b6
BM
10537 /* Check the hash table to determine if this class has been searched
10538 already. */
10539 if (searched_classes)
10540 {
10541 if (hash_lookup (searched_classes,
10542 (const hash_table_key) class, FALSE, NULL))
10543 return NULL;
10544 }
10545 else
10546 {
10547 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10548 java_hash_compare_tree_node);
10549 searched_classes = &t;
10550 }
10551
10552 search_not_done++;
10553 hash_lookup (searched_classes,
0c2b8145 10554 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10555
c2952b01
APB
10556 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10557 {
10558 load_class (class, 1);
10559 safe_layout_class (class);
10560 }
10561
1982388a 10562 /* Search interfaces */
9a7ab4b3
APB
10563 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10564 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10565 {
1982388a
APB
10566 int i, n;
10567 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10568 search_applicable_methods_list (lc, TYPE_METHODS (class),
10569 name, arglist, &list, &all_list);
1982388a 10570 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10571 for (i = 1; i < n; i++)
b67d701b 10572 {
de0b553f
APB
10573 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10574 tree rlist;
10575
de0b553f
APB
10576 rlist = find_applicable_accessible_methods_list (lc, t, name,
10577 arglist);
165f37bc 10578 list = chainon (rlist, list);
e04a16fb 10579 }
e04a16fb 10580 }
1982388a
APB
10581 /* Search classes */
10582 else
c2952b01 10583 {
165f37bc
APB
10584 tree sc = class;
10585 int seen_inner_class = 0;
c2952b01
APB
10586 search_applicable_methods_list (lc, TYPE_METHODS (class),
10587 name, arglist, &list, &all_list);
10588
165f37bc
APB
10589 /* We must search all interfaces of this class */
10590 if (!lc)
10591 {
10592 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10593 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10594 for (i = 1; i < n; i++)
10595 {
10596 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10597 if (t != object_type_node)
30a3caef
ZW
10598 {
10599 tree rlist
10600 = find_applicable_accessible_methods_list (lc, t,
10601 name, arglist);
10602 list = chainon (rlist, list);
10603 }
165f37bc 10604 }
165f37bc
APB
10605 }
10606
c2952b01
APB
10607 /* Search enclosing context of inner classes before looking
10608 ancestors up. */
10609 while (!lc && INNER_CLASS_TYPE_P (class))
10610 {
165f37bc
APB
10611 tree rlist;
10612 seen_inner_class = 1;
c2952b01 10613 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10614 rlist = find_applicable_accessible_methods_list (lc, class,
10615 name, arglist);
10616 list = chainon (rlist, list);
c2952b01 10617 }
165f37bc
APB
10618
10619 if (!lc && seen_inner_class
10620 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10621 class = CLASSTYPE_SUPER (sc);
10622 else
10623 class = sc;
10624
ad69b5b6
BM
10625 /* Search superclass */
10626 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10627 {
10628 tree rlist;
10629 class = CLASSTYPE_SUPER (class);
10630 rlist = find_applicable_accessible_methods_list (lc, class,
10631 name, arglist);
10632 list = chainon (rlist, list);
10633 }
10634 }
10635
10636 search_not_done--;
10637
10638 /* We're done. Reset the searched classes list and finally search
10639 java.lang.Object if it wasn't searched already. */
10640 if (!search_not_done)
10641 {
10642 if (!lc
10643 && TYPE_METHODS (object_type_node)
10644 && !hash_lookup (searched_classes,
10645 (const hash_table_key) object_type_node,
10646 FALSE, NULL))
10647 {
10648 search_applicable_methods_list (lc,
10649 TYPE_METHODS (object_type_node),
10650 name, arglist, &list, &all_list);
10651 }
10652 hash_table_free (searched_classes);
10653 searched_classes = NULL;
c2952b01 10654 }
1982388a 10655
b67d701b
PB
10656 /* Either return the list obtained or all selected (but
10657 inaccessible) methods for better error report. */
10658 return (!list ? all_list : list);
10659}
e04a16fb 10660
ad69b5b6 10661/* Effectively search for the appropriate method in method */
1982388a
APB
10662
10663static void
c2952b01 10664search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10665 int lc;
10666 tree method, name, arglist;
10667 tree *list, *all_list;
10668{
10669 for (; method; method = TREE_CHAIN (method))
10670 {
10671 /* When dealing with constructor, stop here, otherwise search
10672 other classes */
10673 if (lc && !DECL_CONSTRUCTOR_P (method))
10674 continue;
10675 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10676 || (GET_METHOD_NAME (method) != name)))
10677 continue;
c7303e41 10678
1982388a
APB
10679 if (argument_types_convertible (method, arglist))
10680 {
10681 /* Retain accessible methods only */
10682 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10683 method, 0))
10684 *list = tree_cons (NULL_TREE, method, *list);
10685 else
10686 /* Also retain all selected method here */
10687 *all_list = tree_cons (NULL_TREE, method, *list);
10688 }
10689 }
ad69b5b6 10690}
1982388a 10691
b67d701b
PB
10692/* 15.11.2.2 Choose the Most Specific Method */
10693
10694static tree
10695find_most_specific_methods_list (list)
10696 tree list;
10697{
10698 int max = 0;
9a7ab4b3 10699 int abstract, candidates;
b67d701b
PB
10700 tree current, new_list = NULL_TREE;
10701 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10702 {
b67d701b
PB
10703 tree method;
10704 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10705
10706 for (method = list; method; method = TREE_CHAIN (method))
10707 {
0c2b8145 10708 tree method_v, current_v;
b67d701b
PB
10709 /* Don't test a method against itself */
10710 if (method == current)
10711 continue;
10712
0c2b8145
APB
10713 method_v = TREE_VALUE (method);
10714 current_v = TREE_VALUE (current);
10715
10716 /* Compare arguments and location where methods where declared */
10717 if (argument_types_convertible (method_v, current_v))
b67d701b 10718 {
0c2b8145
APB
10719 if (valid_method_invocation_conversion_p
10720 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10721 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10722 && enclosing_context_p (DECL_CONTEXT (method_v),
10723 DECL_CONTEXT (current_v))))
10724 {
10725 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10726 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10727 max = (v > max ? v : max);
10728 }
b67d701b
PB
10729 }
10730 }
e04a16fb
AG
10731 }
10732
b67d701b 10733 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10734 for (current = list, abstract = -1, candidates = -1;
10735 current; current = TREE_CHAIN (current))
b67d701b 10736 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10737 {
10738 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10739 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10740 candidates++;
10741 }
b67d701b 10742
165f37bc
APB
10743 /* If we have several and they're all abstract, just pick the
10744 closest one. */
9a7ab4b3
APB
10745 if (candidates > 0 && (candidates == abstract))
10746 {
10747 new_list = nreverse (new_list);
10748 TREE_CHAIN (new_list) = NULL_TREE;
10749 }
165f37bc 10750
cab8e2bd
BM
10751 /* We have several (we couldn't find a most specific), all but one
10752 are abstract, we pick the only non abstract one. */
10753 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10754 {
9a7ab4b3
APB
10755 for (current = new_list; current; current = TREE_CHAIN (current))
10756 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10757 {
10758 TREE_CHAIN (current) = NULL_TREE;
10759 new_list = current;
10760 }
165f37bc
APB
10761 }
10762
b67d701b
PB
10763 /* If we can't find one, lower expectations and try to gather multiple
10764 maximally specific methods */
165f37bc 10765 while (!new_list && max)
b67d701b
PB
10766 {
10767 while (--max > 0)
10768 {
10769 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10770 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10771 }
b67d701b
PB
10772 }
10773
10774 return new_list;
e04a16fb
AG
10775}
10776
b67d701b
PB
10777/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10778 converted by method invocation conversion (5.3) to the type of the
10779 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10780 to change less often than M1. */
e04a16fb 10781
b67d701b
PB
10782static int
10783argument_types_convertible (m1, m2_or_arglist)
10784 tree m1, m2_or_arglist;
e04a16fb 10785{
b67d701b
PB
10786 static tree m2_arg_value = NULL_TREE;
10787 static tree m2_arg_cache = NULL_TREE;
19e223db 10788 static int initialized_p;
e04a16fb 10789
b67d701b 10790 register tree m1_arg, m2_arg;
e04a16fb 10791
19e223db
MM
10792 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10793 collector. */
10794 if (!initialized_p)
10795 {
10796 ggc_add_tree_root (&m2_arg_value, 1);
10797 ggc_add_tree_root (&m2_arg_cache, 1);
10798 initialized_p = 1;
10799 }
10800
c2952b01 10801 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10802
b67d701b
PB
10803 if (m2_arg_value == m2_or_arglist)
10804 m2_arg = m2_arg_cache;
10805 else
10806 {
10807 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10808 argument types */
10809 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10810 {
10811 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10812 if (!METHOD_STATIC (m2_or_arglist))
10813 m2_arg = TREE_CHAIN (m2_arg);
10814 }
10815 else
10816 m2_arg = m2_or_arglist;
e04a16fb 10817
b67d701b
PB
10818 m2_arg_value = m2_or_arglist;
10819 m2_arg_cache = m2_arg;
10820 }
e04a16fb 10821
de4c7b02 10822 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10823 {
c877974e 10824 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10825 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10826 TREE_VALUE (m2_arg)))
10827 break;
10828 m1_arg = TREE_CHAIN (m1_arg);
10829 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10830 }
de4c7b02 10831 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10832}
10833
10834/* Qualification routines */
10835
10836static void
10837qualify_ambiguous_name (id)
10838 tree id;
10839{
cd531a2e
KG
10840 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10841 saved_current_class;
d8fccff5 10842 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10843 int code;
e04a16fb
AG
10844
10845 /* We first qualify the first element, then derive qualification of
10846 others based on the first one. If the first element is qualified
10847 by a resolution (field or type), this resolution is stored in the
10848 QUAL_RESOLUTION of the qual element being examined. We need to
10849 save the current_class since the use of SUPER might change the
10850 its value. */
10851 saved_current_class = current_class;
10852 qual = EXPR_WFL_QUALIFICATION (id);
10853 do {
10854
10855 /* Simple qualified expression feature a qual_wfl that is a
10856 WFL. Expression derived from a primary feature more complicated
10857 things like a CALL_EXPR. Expression from primary need to be
10858 worked out to extract the part on which the qualification will
10859 take place. */
10860 qual_wfl = QUAL_WFL (qual);
10861 switch (TREE_CODE (qual_wfl))
10862 {
10863 case CALL_EXPR:
10864 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10865 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10866 {
10867 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10868 qual_wfl = QUAL_WFL (qual);
10869 }
10870 break;
d8fccff5 10871 case NEW_ARRAY_EXPR:
c2952b01 10872 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10873 qual = TREE_CHAIN (qual);
1a6d4fb7 10874 again = new_array_found = 1;
d8fccff5 10875 continue;
e04a16fb 10876 case CONVERT_EXPR:
f2760b27
APB
10877 break;
10878 case NEW_CLASS_EXPR:
e04a16fb
AG
10879 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10880 break;
c583dd46
APB
10881 case ARRAY_REF:
10882 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10883 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10884 break;
8576f094
APB
10885 case STRING_CST:
10886 qual = TREE_CHAIN (qual);
10887 qual_wfl = QUAL_WFL (qual);
10888 break;
165f37bc
APB
10889 case CLASS_LITERAL:
10890 qual = TREE_CHAIN (qual);
10891 qual_wfl = QUAL_WFL (qual);
10892 break;
0a2138e2
APB
10893 default:
10894 /* Fix for -Wall. Just break doing nothing */
10895 break;
e04a16fb 10896 }
8576f094 10897
e04a16fb
AG
10898 ptr_type = current_class;
10899 again = 0;
8576f094
APB
10900 code = TREE_CODE (qual_wfl);
10901
10902 /* Pos evaluation: non WFL leading expression nodes */
10903 if (code == CONVERT_EXPR
10904 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10905 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10906
cd7c5840
APB
10907 else if (code == INTEGER_CST)
10908 name = qual_wfl;
10909
ac22f9cb 10910 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10911 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10912 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10913
c2952b01
APB
10914 else if (code == TREE_LIST)
10915 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10916
37feda7d
APB
10917 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10918 || code == PLUS_EXPR)
8576f094
APB
10919 {
10920 qual = TREE_CHAIN (qual);
10921 qual_wfl = QUAL_WFL (qual);
10922 again = 1;
10923 }
10924 else
f441f671
APB
10925 {
10926 name = EXPR_WFL_NODE (qual_wfl);
10927 if (!name)
10928 {
10929 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10930 again = 1;
10931 }
10932 }
10933
e04a16fb
AG
10934 /* If we have a THIS (from a primary), we set the context accordingly */
10935 if (name == this_identifier_node)
10936 {
6e22695a
APB
10937 /* This isn't really elegant. One more added irregularity
10938 before I start using COMPONENT_REF (hopefully very soon.) */
10939 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10940 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10941 EXPR_WITH_FILE_LOCATION
10942 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10943 this_identifier_node)
10944 {
10945 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10946 qual = EXPR_WFL_QUALIFICATION (qual);
10947 }
e04a16fb
AG
10948 qual = TREE_CHAIN (qual);
10949 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10950 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10951 again = 1;
10952 else
10953 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10954 this_found = 1;
10955 }
10956 /* If we have a SUPER, we set the context accordingly */
10957 if (name == super_identifier_node)
10958 {
10959 current_class = CLASSTYPE_SUPER (ptr_type);
10960 /* Check that there is such a thing as a super class. If not,
10961 return. The error will be caught later on, during the
10962 resolution */
10963 if (!current_class)
10964 {
10965 current_class = saved_current_class;
10966 return;
10967 }
10968 qual = TREE_CHAIN (qual);
10969 /* Do one more interation to set things up */
10970 super_found = again = 1;
10971 }
10972 } while (again);
10973
f2760b27
APB
10974 /* If name appears within the scope of a local variable declaration
10975 or parameter declaration, then it is an expression name. We don't
10976 carry this test out if we're in the context of the use of SUPER
10977 or THIS */
cd7c5840
APB
10978 if (!this_found && !super_found
10979 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10980 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10981 {
10982 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10983 QUAL_RESOLUTION (qual) = decl;
10984 }
10985
10986 /* If within the class/interface NAME was found to be used there
10987 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10988 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10989 address length, it is OK. */
10990 else if ((decl = lookup_field_wrapper (ptr_type, name))
10991 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10992 {
10993 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10994 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10995 }
10996
1a6d4fb7 10997 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10998 - NAME is a class/interface declared within the compilation
10999 unit containing NAME,
11000 - NAME is imported via a single-type-import declaration,
11001 - NAME is declared in an another compilation unit of the package
11002 of the compilation unit containing NAME,
11003 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
11004 of the compilation unit containing NAME.
11005 - NAME is actually a STRING_CST. */
cd7c5840
APB
11006 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
11007 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
11008 {
11009 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11010 QUAL_RESOLUTION (qual) = decl;
11011 }
11012
f2760b27 11013 /* Method call, array references and cast are expression name */
9bbc7d9f 11014 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
11015 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11016 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
11017 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11018
11019 /* Check here that NAME isn't declared by more than one
11020 type-import-on-demand declaration of the compilation unit
11021 containing NAME. FIXME */
11022
11023 /* Otherwise, NAME is reclassified as a package name */
11024 else
11025 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11026
11027 /* Propagate the qualification accross other components of the
11028 qualified name */
11029 for (qual = TREE_CHAIN (qual); qual;
11030 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11031 {
11032 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11033 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11034 else
11035 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11036 }
11037
11038 /* Store the global qualification for the ambiguous part of ID back
11039 into ID fields */
11040 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11041 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11042 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11043 RESOLVE_TYPE_NAME_P (id) = 1;
11044 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11045 RESOLVE_PACKAGE_NAME_P (id) = 1;
11046
11047 /* Restore the current class */
11048 current_class = saved_current_class;
11049}
11050
11051static int
11052breakdown_qualified (left, right, source)
11053 tree *left, *right, source;
11054{
63ad61ed 11055 char *p, *base;
e04a16fb
AG
11056 int l = IDENTIFIER_LENGTH (source);
11057
63ad61ed
ZW
11058 base = alloca (l + 1);
11059 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11060
e04a16fb 11061 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 11062 p = base + l - 1;
e04a16fb
AG
11063 while (*p != '.' && p != base)
11064 p--;
11065
11066 /* We didn't find a '.'. Return an error */
11067 if (p == base)
11068 return 1;
11069
11070 *p = '\0';
11071 if (right)
11072 *right = get_identifier (p+1);
63ad61ed 11073 *left = get_identifier (base);
e04a16fb
AG
11074
11075 return 0;
11076}
11077
e04a16fb 11078/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11079 local variable decls if present.
11080 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11081
11082static tree
11083java_complete_tree (node)
11084 tree node;
5b09b33e
PB
11085{
11086 node = java_complete_lhs (node);
c7303e41 11087 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11088 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11089 && !flag_emit_xref)
5b09b33e
PB
11090 {
11091 tree value = DECL_INITIAL (node);
11092 DECL_INITIAL (node) = NULL_TREE;
11093 value = fold_constant_for_init (value, node);
11094 DECL_INITIAL (node) = value;
11095 if (value != NULL_TREE)
c2952b01
APB
11096 {
11097 /* fold_constant_for_init sometimes widen the original type
11098 of the constant (i.e. byte to int.) It's not desirable,
11099 especially if NODE is a function argument. */
11100 if (TREE_CODE (value) == INTEGER_CST
11101 && TREE_TYPE (node) != TREE_TYPE (value))
11102 return convert (TREE_TYPE (node), value);
11103 else
11104 return value;
11105 }
c7303e41
APB
11106 else
11107 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11108 }
11109 return node;
11110}
11111
2aa11e97
APB
11112static tree
11113java_stabilize_reference (node)
11114 tree node;
11115{
11116 if (TREE_CODE (node) == COMPOUND_EXPR)
11117 {
11118 tree op0 = TREE_OPERAND (node, 0);
11119 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11120 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11121 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11122 return node;
11123 }
5cbdba64 11124 return stabilize_reference (node);
2aa11e97
APB
11125}
11126
5b09b33e
PB
11127/* Patch tree nodes in a function body. When a BLOCK is found, push
11128 local variable decls if present.
11129 Same as java_complete_tree, but does not resolve static finals to values. */
11130
11131static tree
11132java_complete_lhs (node)
11133 tree node;
e04a16fb 11134{
22eed1e6 11135 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11136 int flag;
e04a16fb
AG
11137
11138 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11139 worked out. */
e04a16fb
AG
11140 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11141 return node;
11142
11143 /* The switch block implements cases processing container nodes
11144 first. Contained nodes are always written back. Leaves come
11145 next and return a value. */
11146 switch (TREE_CODE (node))
11147 {
11148 case BLOCK:
11149
11150 /* 1- Block section.
11151 Set the local values on decl names so we can identify them
11152 faster when they're referenced. At that stage, identifiers
11153 are legal so we don't check for declaration errors. */
11154 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11155 {
11156 DECL_CONTEXT (cn) = current_function_decl;
11157 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11158 }
15fdcfe9
PB
11159 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11160 CAN_COMPLETE_NORMALLY (node) = 1;
11161 else
e04a16fb 11162 {
15fdcfe9
PB
11163 tree stmt = BLOCK_EXPR_BODY (node);
11164 tree *ptr;
11165 int error_seen = 0;
11166 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11167 {
c877974e
APB
11168 /* Re-order from (((A; B); C); ...; Z) to
11169 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11170 This makes it easier to scan the statements left-to-right
11171 without using recursion (which might overflow the stack
11172 if the block has many statements. */
11173 for (;;)
11174 {
11175 tree left = TREE_OPERAND (stmt, 0);
11176 if (TREE_CODE (left) != COMPOUND_EXPR)
11177 break;
11178 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11179 TREE_OPERAND (left, 1) = stmt;
11180 stmt = left;
11181 }
11182 BLOCK_EXPR_BODY (node) = stmt;
11183 }
11184
c877974e
APB
11185 /* Now do the actual complete, without deep recursion for
11186 long blocks. */
15fdcfe9 11187 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11188 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11189 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11190 {
11191 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11192 tree *next = &TREE_OPERAND (*ptr, 1);
11193 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11194 if (cur == empty_stmt_node)
11195 {
11196 /* Optimization; makes it easier to detect empty bodies.
11197 Most useful for <clinit> with all-constant initializer. */
11198 *ptr = *next;
11199 continue;
11200 }
15fdcfe9
PB
11201 if (TREE_CODE (cur) == ERROR_MARK)
11202 error_seen++;
11203 else if (! CAN_COMPLETE_NORMALLY (cur))
11204 {
11205 wfl_op2 = *next;
11206 for (;;)
11207 {
11208 if (TREE_CODE (wfl_op2) == BLOCK)
11209 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11210 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11211 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11212 else
11213 break;
11214 }
11215 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11216 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11217 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11218 }
11219 ptr = next;
11220 }
11221 *ptr = java_complete_tree (*ptr);
11222
11223 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11224 return error_mark_node;
15fdcfe9 11225 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11226 }
11227 /* Turn local bindings to null */
11228 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11229 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11230
11231 TREE_TYPE (node) = void_type_node;
11232 break;
11233
11234 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11235
b9f7e36c
APB
11236 case THROW_EXPR:
11237 wfl_op1 = TREE_OPERAND (node, 0);
11238 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11239 /* 14.19 A throw statement cannot complete normally. */
11240 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11241 return patch_throw_statement (node, wfl_op1);
11242
11243 case SYNCHRONIZED_EXPR:
11244 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11245 return patch_synchronized_statement (node, wfl_op1);
11246
b67d701b
PB
11247 case TRY_EXPR:
11248 return patch_try_statement (node);
11249
a7d8d81f
PB
11250 case TRY_FINALLY_EXPR:
11251 COMPLETE_CHECK_OP_0 (node);
11252 COMPLETE_CHECK_OP_1 (node);
11253 CAN_COMPLETE_NORMALLY (node)
11254 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11255 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11256 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11257 return node;
11258
5a005d9e
PB
11259 case CLEANUP_POINT_EXPR:
11260 COMPLETE_CHECK_OP_0 (node);
11261 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11262 CAN_COMPLETE_NORMALLY (node) =
11263 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11264 return node;
11265
11266 case WITH_CLEANUP_EXPR:
11267 COMPLETE_CHECK_OP_0 (node);
11268 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11269 CAN_COMPLETE_NORMALLY (node) =
11270 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11271 TREE_TYPE (node) = void_type_node;
11272 return node;
11273
e04a16fb
AG
11274 case LABELED_BLOCK_EXPR:
11275 PUSH_LABELED_BLOCK (node);
11276 if (LABELED_BLOCK_BODY (node))
11277 COMPLETE_CHECK_OP_1 (node);
11278 TREE_TYPE (node) = void_type_node;
11279 POP_LABELED_BLOCK ();
1fb89a4d
APB
11280
11281 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11282 {
11283 LABELED_BLOCK_BODY (node) = NULL_TREE;
11284 CAN_COMPLETE_NORMALLY (node) = 1;
11285 }
1fb89a4d 11286 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11287 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11288 return node;
11289
11290 case EXIT_BLOCK_EXPR:
11291 /* We don't complete operand 1, because it's the return value of
11292 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11293 return patch_bc_statement (node);
11294
15fdcfe9
PB
11295 case CASE_EXPR:
11296 cn = java_complete_tree (TREE_OPERAND (node, 0));
11297 if (cn == error_mark_node)
11298 return cn;
11299
8576f094
APB
11300 /* First, the case expression must be constant. Values of final
11301 fields are accepted. */
15fdcfe9 11302 cn = fold (cn);
8576f094
APB
11303 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11304 && JDECL_P (TREE_OPERAND (cn, 1))
11305 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11306 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11307 {
100f7cd8
APB
11308 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11309 TREE_OPERAND (cn, 1));
100f7cd8 11310 }
15fdcfe9 11311
ce6e9147 11312 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11313 {
11314 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11315 parse_error_context (node, "Constant expression required");
11316 return error_mark_node;
11317 }
11318
11319 nn = ctxp->current_loop;
11320
11321 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11322 if (!try_builtin_assignconv (NULL_TREE,
11323 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11324 {
11325 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11326 parse_error_context
11327 (wfl_operator,
11328 "Incompatible type for case. Can't convert `%s' to `int'",
11329 lang_printable_name (TREE_TYPE (cn), 0));
11330 return error_mark_node;
11331 }
11332
11333 cn = fold (convert (int_type_node, cn));
11334
11335 /* Multiple instance of a case label bearing the same
11336 value is checked during code generation. The case
11337 expression is allright so far. */
34d4df06
APB
11338 if (TREE_CODE (cn) == VAR_DECL)
11339 cn = DECL_INITIAL (cn);
15fdcfe9 11340 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11341 TREE_TYPE (node) = void_type_node;
15fdcfe9 11342 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11343 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11344 break;
11345
11346 case DEFAULT_EXPR:
11347 nn = ctxp->current_loop;
11348 /* Only one default label is allowed per switch statement */
11349 if (SWITCH_HAS_DEFAULT (nn))
11350 {
11351 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11352 parse_error_context (wfl_operator,
11353 "Duplicate case label: `default'");
11354 return error_mark_node;
11355 }
11356 else
11357 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11358 TREE_TYPE (node) = void_type_node;
10100cc7 11359 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11360 CAN_COMPLETE_NORMALLY (node) = 1;
11361 break;
11362
b67d701b 11363 case SWITCH_EXPR:
e04a16fb
AG
11364 case LOOP_EXPR:
11365 PUSH_LOOP (node);
11366 /* Check whether the loop was enclosed in a labeled
11367 statement. If not, create one, insert the loop in it and
11368 return the node */
11369 nn = patch_loop_statement (node);
b67d701b 11370
e04a16fb 11371 /* Anyways, walk the body of the loop */
b67d701b
PB
11372 if (TREE_CODE (node) == LOOP_EXPR)
11373 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11374 /* Switch statement: walk the switch expression and the cases */
11375 else
11376 node = patch_switch_statement (node);
11377
e7c7bcef 11378 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11379 nn = error_mark_node;
11380 else
15fdcfe9 11381 {
b635eb2f
PB
11382 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11383 /* If we returned something different, that's because we
11384 inserted a label. Pop the label too. */
11385 if (nn != node)
11386 {
11387 if (CAN_COMPLETE_NORMALLY (node))
11388 CAN_COMPLETE_NORMALLY (nn) = 1;
11389 POP_LABELED_BLOCK ();
11390 }
15fdcfe9 11391 }
e04a16fb
AG
11392 POP_LOOP ();
11393 return nn;
11394
11395 case EXIT_EXPR:
11396 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11397 return patch_exit_expr (node);
11398
11399 case COND_EXPR:
11400 /* Condition */
11401 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11402 if (TREE_OPERAND (node, 0) == error_mark_node)
11403 return error_mark_node;
11404 /* then-else branches */
11405 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11406 if (TREE_OPERAND (node, 1) == error_mark_node)
11407 return error_mark_node;
11408 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11409 if (TREE_OPERAND (node, 2) == error_mark_node)
11410 return error_mark_node;
11411 return patch_if_else_statement (node);
11412 break;
11413
22eed1e6
APB
11414 case CONDITIONAL_EXPR:
11415 /* Condition */
11416 wfl_op1 = TREE_OPERAND (node, 0);
11417 COMPLETE_CHECK_OP_0 (node);
11418 wfl_op2 = TREE_OPERAND (node, 1);
11419 COMPLETE_CHECK_OP_1 (node);
11420 wfl_op3 = TREE_OPERAND (node, 2);
11421 COMPLETE_CHECK_OP_2 (node);
11422 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11423
e04a16fb
AG
11424 /* 3- Expression section */
11425 case COMPOUND_EXPR:
15fdcfe9 11426 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11427 TREE_OPERAND (node, 0) = nn =
11428 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11429 if (wfl_op2 == empty_stmt_node)
11430 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11431 else
15fdcfe9 11432 {
dc0b3eff 11433 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11434 {
dc0b3eff
PB
11435 /* An unreachable condition in a do-while statement
11436 is *not* (technically) an unreachable statement. */
11437 nn = wfl_op2;
11438 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11439 nn = EXPR_WFL_NODE (nn);
11440 if (TREE_CODE (nn) != EXIT_EXPR)
11441 {
11442 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11443 parse_error_context (wfl_operator, "Unreachable statement");
11444 }
bccaf73a 11445 }
dc0b3eff
PB
11446 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11447 if (TREE_OPERAND (node, 1) == error_mark_node)
11448 return error_mark_node;
11449 CAN_COMPLETE_NORMALLY (node)
11450 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11451 }
e04a16fb
AG
11452 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11453 break;
11454
11455 case RETURN_EXPR:
15fdcfe9 11456 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11457 return patch_return (node);
11458
11459 case EXPR_WITH_FILE_LOCATION:
11460 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11461 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11462 {
5423609c 11463 tree wfl = node;
15fdcfe9 11464 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11465 if (node == error_mark_node)
11466 return node;
5423609c
APB
11467 /* Keep line number information somewhere were it doesn't
11468 disrupt the completion process. */
2c56429a 11469 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11470 {
11471 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11472 TREE_OPERAND (node, 1) = wfl;
11473 }
15fdcfe9
PB
11474 CAN_COMPLETE_NORMALLY (node) = 1;
11475 }
e04a16fb
AG
11476 else
11477 {
5b09b33e
PB
11478 tree body;
11479 int save_lineno = lineno;
11480 lineno = EXPR_WFL_LINENO (node);
11481 body = java_complete_tree (EXPR_WFL_NODE (node));
11482 lineno = save_lineno;
15fdcfe9 11483 EXPR_WFL_NODE (node) = body;
dc0b3eff 11484 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11485 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11486 if (body == empty_stmt_node)
11487 {
11488 /* Optimization; makes it easier to detect empty bodies. */
11489 return body;
11490 }
dc0b3eff 11491 if (body == error_mark_node)
e04a16fb
AG
11492 {
11493 /* Its important for the evaluation of assignment that
11494 this mark on the TREE_TYPE is propagated. */
11495 TREE_TYPE (node) = error_mark_node;
11496 return error_mark_node;
11497 }
11498 else
11499 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11500
e04a16fb
AG
11501 }
11502 break;
11503
b67d701b 11504 case NEW_ARRAY_EXPR:
e04a16fb
AG
11505 /* Patch all the dimensions */
11506 flag = 0;
11507 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11508 {
11509 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11510 tree dim = convert (int_type_node,
11511 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11512 if (dim == error_mark_node)
11513 {
11514 flag = 1;
11515 continue;
11516 }
11517 else
11518 {
b9f7e36c 11519 TREE_VALUE (cn) = dim;
e04a16fb
AG
11520 /* Setup the location of the current dimension, for
11521 later error report. */
11522 TREE_PURPOSE (cn) =
11523 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11524 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11525 }
11526 }
11527 /* They complete the array creation expression, if no errors
11528 were found. */
15fdcfe9 11529 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11530 return (flag ? error_mark_node
11531 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11532
c2952b01
APB
11533 case NEW_ANONYMOUS_ARRAY_EXPR:
11534 /* Create the array type if necessary. */
11535 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11536 {
11537 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11538 if (!(type = resolve_type_during_patch (type)))
11539 return error_mark_node;
11540 type = build_array_from_name (type, NULL_TREE,
11541 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11542 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11543 }
11544 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11545 ANONYMOUS_ARRAY_INITIALIZER (node));
11546 if (node == error_mark_node)
11547 return error_mark_node;
11548 CAN_COMPLETE_NORMALLY (node) = 1;
11549 return node;
11550
b67d701b 11551 case NEW_CLASS_EXPR:
e04a16fb 11552 case CALL_EXPR:
b67d701b 11553 /* Complete function's argument(s) first */
e04a16fb
AG
11554 if (complete_function_arguments (node))
11555 return error_mark_node;
11556 else
b9f7e36c 11557 {
22eed1e6
APB
11558 tree decl, wfl = TREE_OPERAND (node, 0);
11559 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11560
c877974e 11561 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11562 NULL_TREE, 0, &decl);
c877974e
APB
11563 if (node == error_mark_node)
11564 return error_mark_node;
11565
11566 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11567 /* If we call this(...), register signature and positions */
11568 if (in_this)
11569 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11570 tree_cons (wfl, decl,
11571 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11572 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11573 return force_evaluation_order (node);
b9f7e36c 11574 }
e04a16fb
AG
11575
11576 case MODIFY_EXPR:
11577 /* Save potential wfls */
11578 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11579 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11580
cd9643f7
PB
11581 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11582 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11583 && DECL_INITIAL (nn) != NULL_TREE)
11584 {
100f7cd8
APB
11585 tree value;
11586
100f7cd8 11587 value = fold_constant_for_init (nn, nn);
c2952b01 11588
cd9643f7
PB
11589 if (value != NULL_TREE)
11590 {
11591 tree type = TREE_TYPE (value);
c2952b01
APB
11592 if (JPRIMITIVE_TYPE_P (type) ||
11593 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11594 return empty_stmt_node;
11595 }
629d4b4d
APB
11596 if (! flag_emit_class_files)
11597 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11598 if (CLASS_FINAL_VARIABLE_P (nn))
11599 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11600 }
e04a16fb 11601 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11602
e04a16fb
AG
11603 if (TREE_OPERAND (node, 0) == error_mark_node)
11604 return error_mark_node;
11605
5cbdba64
APB
11606 flag = COMPOUND_ASSIGN_P (wfl_op2);
11607 if (flag)
e04a16fb 11608 {
c2952b01
APB
11609 /* This might break when accessing outer field from inner
11610 class. TESTME, FIXME */
2aa11e97 11611 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11612
11613 /* Hand stablize the lhs on both places */
e04a16fb 11614 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11615 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11616 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11617
5cbdba64 11618 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11619 /* Now complete the RHS. We write it back later on. */
11620 nn = java_complete_tree (TREE_OPERAND (node, 1));
11621
642f15d1
APB
11622 if ((cn = patch_string (nn)))
11623 nn = cn;
11624
2aa11e97
APB
11625 /* The last part of the rewrite for E1 op= E2 is to have
11626 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11627 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11628 TREE_TYPE (lvalue), nn));
5cbdba64 11629
568aac9c
TT
11630 /* If the assignment is compound and has reference type,
11631 then ensure the LHS has type String and nothing else. */
11632 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11633 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11634 parse_error_context (wfl_op2,
11635 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11636 lang_printable_name (TREE_TYPE (lvalue), 0));
11637
5cbdba64 11638 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11639 }
11640
f8976021 11641 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11642 function to complete this RHS. Note that a NEW_ARRAY_INIT
11643 might have been already fully expanded if created as a result
11644 of processing an anonymous array initializer. We avoid doing
11645 the operation twice by testing whether the node already bears
11646 a type. */
11647 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11648 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11649 TREE_OPERAND (node, 1));
2aa11e97 11650 /* Otherwise we simply complete the RHS */
f8976021
APB
11651 else
11652 nn = java_complete_tree (TREE_OPERAND (node, 1));
11653
e04a16fb 11654 if (nn == error_mark_node)
c0d87ff6 11655 return error_mark_node;
2aa11e97
APB
11656
11657 /* Write back the RHS as we evaluated it. */
e04a16fb 11658 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11659
11660 /* In case we're handling = with a String as a RHS, we need to
11661 produce a String out of the RHS (it might still be a
11662 STRING_CST or a StringBuffer at this stage */
11663 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11664 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11665
11666 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11667 TREE_OPERAND (node, 1))))
11668 {
11669 /* We return error_mark_node if outer_field_access_fix
11670 detects we write into a final. */
11671 if (nn == error_mark_node)
11672 return error_mark_node;
11673 node = nn;
11674 }
11675 else
11676 {
11677 node = patch_assignment (node, wfl_op1, wfl_op2);
11678 /* Reorganize the tree if necessary. */
11679 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11680 || JSTRING_P (TREE_TYPE (node))))
11681 node = java_refold (node);
11682 }
11683
15fdcfe9
PB
11684 CAN_COMPLETE_NORMALLY (node) = 1;
11685 return node;
e04a16fb
AG
11686
11687 case MULT_EXPR:
11688 case PLUS_EXPR:
11689 case MINUS_EXPR:
11690 case LSHIFT_EXPR:
11691 case RSHIFT_EXPR:
11692 case URSHIFT_EXPR:
11693 case BIT_AND_EXPR:
11694 case BIT_XOR_EXPR:
11695 case BIT_IOR_EXPR:
11696 case TRUNC_MOD_EXPR:
c2952b01 11697 case TRUNC_DIV_EXPR:
e04a16fb
AG
11698 case RDIV_EXPR:
11699 case TRUTH_ANDIF_EXPR:
11700 case TRUTH_ORIF_EXPR:
11701 case EQ_EXPR:
11702 case NE_EXPR:
11703 case GT_EXPR:
11704 case GE_EXPR:
11705 case LT_EXPR:
11706 case LE_EXPR:
11707 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11708 knows how to handle those cases. */
11709 wfl_op1 = TREE_OPERAND (node, 0);
11710 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11711
15fdcfe9 11712 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11713 /* Don't complete string nodes if dealing with the PLUS operand. */
11714 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11715 {
11716 nn = java_complete_tree (wfl_op1);
11717 if (nn == error_mark_node)
11718 return error_mark_node;
48a840d9 11719
2aa11e97
APB
11720 TREE_OPERAND (node, 0) = nn;
11721 }
b67d701b 11722 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11723 {
11724 nn = java_complete_tree (wfl_op2);
11725 if (nn == error_mark_node)
11726 return error_mark_node;
48a840d9 11727
2aa11e97
APB
11728 TREE_OPERAND (node, 1) = nn;
11729 }
dc0b3eff 11730 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11731
5e942c50
APB
11732 case INSTANCEOF_EXPR:
11733 wfl_op1 = TREE_OPERAND (node, 0);
11734 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11735 if (flag_emit_xref)
11736 {
11737 TREE_TYPE (node) = boolean_type_node;
11738 return node;
11739 }
5e942c50
APB
11740 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11741
b67d701b 11742 case UNARY_PLUS_EXPR:
e04a16fb
AG
11743 case NEGATE_EXPR:
11744 case TRUTH_NOT_EXPR:
11745 case BIT_NOT_EXPR:
11746 case PREDECREMENT_EXPR:
11747 case PREINCREMENT_EXPR:
11748 case POSTDECREMENT_EXPR:
11749 case POSTINCREMENT_EXPR:
11750 case CONVERT_EXPR:
11751 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11752 how to handle those cases. */
11753 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11754 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11755 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11756 if (TREE_OPERAND (node, 0) == error_mark_node)
11757 return error_mark_node;
4a5f66c3
APB
11758 node = patch_unaryop (node, wfl_op1);
11759 CAN_COMPLETE_NORMALLY (node) = 1;
11760 break;
e04a16fb
AG
11761
11762 case ARRAY_REF:
11763 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11764 how to handle those cases. */
11765 wfl_op1 = TREE_OPERAND (node, 0);
11766 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11767 if (TREE_OPERAND (node, 0) == error_mark_node)
11768 return error_mark_node;
7f1d4866 11769 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11770 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11771 /* The same applies to wfl_op2 */
11772 wfl_op2 = TREE_OPERAND (node, 1);
11773 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11774 if (TREE_OPERAND (node, 1) == error_mark_node)
11775 return error_mark_node;
7f1d4866 11776 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11777 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11778 return patch_array_ref (node);
e04a16fb 11779
63a212ed
PB
11780 case RECORD_TYPE:
11781 return node;;
11782
11783 case COMPONENT_REF:
11784 /* The first step in the re-write of qualified name handling. FIXME.
11785 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11786 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11787 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11788 {
11789 tree name = TREE_OPERAND (node, 1);
11790 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11791 if (field == NULL_TREE)
11792 {
11793 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11794 return error_mark_node;
11795 }
11796 if (! FIELD_STATIC (field))
11797 {
11798 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11799 return error_mark_node;
11800 }
11801 return field;
11802 }
11803 else
11804 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11805 break;
9bbc7d9f 11806
b67d701b 11807 case THIS_EXPR:
e04a16fb
AG
11808 /* Can't use THIS in a static environment */
11809 if (!current_this)
11810 {
11811 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11812 parse_error_context (wfl_operator,
11813 "Keyword `this' used outside allowed context");
e04a16fb
AG
11814 TREE_TYPE (node) = error_mark_node;
11815 return error_mark_node;
11816 }
22eed1e6
APB
11817 if (ctxp->explicit_constructor_p)
11818 {
11819 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11820 parse_error_context
781b0558 11821 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11822 TREE_TYPE (node) = error_mark_node;
11823 return error_mark_node;
11824 }
e04a16fb 11825 return current_this;
c2952b01
APB
11826
11827 case CLASS_LITERAL:
11828 CAN_COMPLETE_NORMALLY (node) = 1;
11829 node = patch_incomplete_class_ref (node);
11830 if (node == error_mark_node)
11831 return error_mark_node;
11832 break;
11833
11834 case INSTANCE_INITIALIZERS_EXPR:
11835 in_instance_initializer++;
11836 node = java_complete_tree (TREE_OPERAND (node, 0));
11837 in_instance_initializer--;
11838 if (node != error_mark_node)
11839 TREE_TYPE (node) = void_type_node;
11840 else
11841 return error_mark_node;
11842 break;
e04a16fb 11843
e04a16fb 11844 default:
15fdcfe9 11845 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11846 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11847 and it's time to turn it into the appropriate String object */
11848 if ((nn = patch_string (node)))
11849 node = nn;
11850 else
11851 fatal ("No case for tree code `%s' - java_complete_tree\n",
11852 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11853 }
11854 return node;
11855}
11856
11857/* Complete function call's argument. Return a non zero value is an
11858 error was found. */
11859
11860static int
11861complete_function_arguments (node)
11862 tree node;
11863{
11864 int flag = 0;
11865 tree cn;
11866
f63991a8 11867 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11868 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11869 {
b67d701b 11870 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11871 parm = java_complete_tree (wfl);
c2952b01 11872
e04a16fb
AG
11873 if (parm == error_mark_node)
11874 {
11875 flag = 1;
11876 continue;
11877 }
b67d701b
PB
11878 /* If have a string literal that we haven't transformed yet or a
11879 crafted string buffer, as a result of use of the the String
11880 `+' operator. Build `parm.toString()' and expand it. */
11881 if ((temp = patch_string (parm)))
b9f7e36c 11882 parm = temp;
5e942c50
APB
11883 /* Inline PRIMTYPE.TYPE read access */
11884 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11885
5e942c50 11886 TREE_VALUE (cn) = parm;
e04a16fb 11887 }
f63991a8 11888 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11889 return flag;
11890}
11891
11892/* Sometimes (for loops and variable initialized during their
11893 declaration), we want to wrap a statement around a WFL and turn it
11894 debugable. */
11895
11896static tree
11897build_debugable_stmt (location, stmt)
11898 int location;
11899 tree stmt;
11900{
11901 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11902 {
11903 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11904 EXPR_WFL_LINECOL (stmt) = location;
11905 }
11906 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11907 return stmt;
11908}
11909
11910static tree
11911build_expr_block (body, decls)
11912 tree body, decls;
11913{
11914 tree node = make_node (BLOCK);
11915 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11916 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11917 if (body)
11918 TREE_TYPE (node) = TREE_TYPE (body);
11919 TREE_SIDE_EFFECTS (node) = 1;
11920 return node;
11921}
11922
b67d701b
PB
11923/* Create a new function block and link it approriately to current
11924 function block chain */
e04a16fb
AG
11925
11926static tree
11927enter_block ()
11928{
b67d701b
PB
11929 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11930}
11931
11932/* Link block B supercontext to the previous block. The current
11933 function DECL is used as supercontext when enter_a_block is called
11934 for the first time for a given function. The current function body
11935 (DECL_FUNCTION_BODY) is set to be block B. */
11936
11937static tree
11938enter_a_block (b)
11939 tree b;
11940{
e04a16fb
AG
11941 tree fndecl = current_function_decl;
11942
f099f336
APB
11943 if (!fndecl) {
11944 BLOCK_SUPERCONTEXT (b) = current_static_block;
11945 current_static_block = b;
11946 }
11947
11948 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11949 {
11950 BLOCK_SUPERCONTEXT (b) = fndecl;
11951 DECL_FUNCTION_BODY (fndecl) = b;
11952 }
11953 else
11954 {
11955 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11956 DECL_FUNCTION_BODY (fndecl) = b;
11957 }
11958 return b;
11959}
11960
11961/* Exit a block by changing the current function body
11962 (DECL_FUNCTION_BODY) to the current block super context, only if
11963 the block being exited isn't the method's top level one. */
11964
11965static tree
11966exit_block ()
11967{
f099f336
APB
11968 tree b;
11969 if (current_function_decl)
11970 {
11971 b = DECL_FUNCTION_BODY (current_function_decl);
11972 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11973 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11974 }
11975 else
11976 {
11977 b = current_static_block;
e04a16fb 11978
f099f336
APB
11979 if (BLOCK_SUPERCONTEXT (b))
11980 current_static_block = BLOCK_SUPERCONTEXT (b);
11981 }
e04a16fb
AG
11982 return b;
11983}
11984
11985/* Lookup for NAME in the nested function's blocks, all the way up to
11986 the current toplevel one. It complies with Java's local variable
11987 scoping rules. */
11988
11989static tree
11990lookup_name_in_blocks (name)
11991 tree name;
11992{
f099f336 11993 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11994
11995 while (b != current_function_decl)
11996 {
11997 tree current;
11998
11999 /* Paranoid sanity check. To be removed */
12000 if (TREE_CODE (b) != BLOCK)
12001 fatal ("non block expr function body - lookup_name_in_blocks");
12002
12003 for (current = BLOCK_EXPR_DECLS (b); current;
12004 current = TREE_CHAIN (current))
12005 if (DECL_NAME (current) == name)
12006 return current;
12007 b = BLOCK_SUPERCONTEXT (b);
12008 }
12009 return NULL_TREE;
12010}
12011
12012static void
12013maybe_absorb_scoping_blocks ()
12014{
f099f336 12015 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
12016 {
12017 tree b = exit_block ();
12018 java_method_add_stmt (current_function_decl, b);
12019 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12020 }
12021}
12022
12023\f
12024/* This section of the source is reserved to build_* functions that
12025 are building incomplete tree nodes and the patch_* functions that
12026 are completing them. */
12027
c2952b01
APB
12028/* Wrap a non WFL node around a WFL. */
12029static tree
9a7ab4b3 12030build_wfl_wrap (node, location)
c2952b01 12031 tree node;
9a7ab4b3 12032 int location;
c2952b01
APB
12033{
12034 tree wfl, node_to_insert = node;
12035
12036 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12037 with the way we're processing SUPER. A THIS from a primary as a
12038 different form than a SUPER. Turn THIS into something symbolic */
12039 if (TREE_CODE (node) == THIS_EXPR)
12040 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12041 else
12042 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12043
9a7ab4b3 12044 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
12045 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12046 return wfl;
12047}
12048
12049
9bbc7d9f 12050/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
12051 we're currently dealing with the class java.lang.Object. */
12052
12053static tree
e920ebc9
APB
12054build_super_invocation (mdecl)
12055 tree mdecl;
22eed1e6 12056{
e920ebc9 12057 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 12058 return empty_stmt_node;
22eed1e6
APB
12059 else
12060 {
9ee9b555 12061 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
12062 tree a = NULL_TREE, t;
12063 /* If we're dealing with an anonymous class, pass the arguments
12064 of the crafted constructor along. */
12065 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12066 {
12067 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12068 for (; t != end_params_node; t = TREE_CHAIN (t))
12069 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12070 }
12071 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12072 }
12073}
12074
12075/* Build a SUPER/THIS qualified method invocation. */
12076
12077static tree
12078build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12079 int use_this;
12080 tree name, args;
12081 int lloc, rloc;
22eed1e6
APB
12082{
12083 tree invok;
12084 tree wfl =
9ee9b555 12085 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12086 EXPR_WFL_LINECOL (wfl) = lloc;
12087 invok = build_method_invocation (name, args);
12088 return make_qualified_primary (wfl, invok, rloc);
12089}
12090
b67d701b 12091/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12092
12093static tree
12094build_method_invocation (name, args)
12095 tree name;
12096 tree args;
12097{
12098 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12099 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12100 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12101 return call;
12102}
12103
12104/* Build an incomplete new xxx(...) node. */
12105
12106static tree
12107build_new_invocation (name, args)
12108 tree name, args;
12109{
12110 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12111 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12112 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12113 return call;
12114}
12115
12116/* Build an incomplete assignment expression. */
12117
12118static tree
12119build_assignment (op, op_location, lhs, rhs)
12120 int op, op_location;
12121 tree lhs, rhs;
12122{
12123 tree assignment;
12124 /* Build the corresponding binop if we deal with a Compound
12125 Assignment operator. Mark the binop sub-tree as part of a
12126 Compound Assignment expression */
12127 if (op != ASSIGN_TK)
12128 {
12129 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12130 COMPOUND_ASSIGN_P (rhs) = 1;
12131 }
12132 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12133 TREE_SIDE_EFFECTS (assignment) = 1;
12134 EXPR_WFL_LINECOL (assignment) = op_location;
12135 return assignment;
12136}
12137
12138/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12139
15fdcfe9 12140char *
e04a16fb
AG
12141print_int_node (node)
12142 tree node;
12143{
12144 static char buffer [80];
12145 if (TREE_CONSTANT_OVERFLOW (node))
12146 sprintf (buffer, "<overflow>");
12147
12148 if (TREE_INT_CST_HIGH (node) == 0)
12149 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12150 TREE_INT_CST_LOW (node));
12151 else if (TREE_INT_CST_HIGH (node) == -1
12152 && TREE_INT_CST_LOW (node) != 0)
12153 {
12154 buffer [0] = '-';
12155 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12156 -TREE_INT_CST_LOW (node));
12157 }
12158 else
12159 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12160 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12161
12162 return buffer;
12163}
12164
c7303e41
APB
12165\f
12166
12167/* This section of the code handle assignment check with FINAL
12168 variables. */
12169
12170static void
12171reset_static_final_variable_assignment_flag (class)
12172 tree class;
12173{
12174 tree field;
12175 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12176 if (CLASS_FINAL_VARIABLE_P (field))
12177 DECL_FIELD_FINAL_LIIC (field) = 0;
12178}
12179
12180/* Figure whether all final static variable have been initialized. */
12181
12182static void
12183check_static_final_variable_assignment_flag (class)
12184 tree class;
12185{
12186 tree field;
12187
12188 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12189 if (CLASS_FINAL_VARIABLE_P (field)
12190 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12191 parse_error_context
12192 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12193 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12194 IDENTIFIER_POINTER (DECL_NAME (field)));
12195}
12196
12197/* This function marks all final variable locally unassigned. */
12198
12199static void
12200reset_final_variable_local_assignment_flag (class)
12201 tree class;
12202{
12203 tree field;
12204 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12205 if (FINAL_VARIABLE_P (field))
12206 DECL_FIELD_FINAL_LIIC (field) = 0;
12207}
12208
12209/* Figure whether all final variables have beem initialized in MDECL
12210 and mark MDECL accordingly. */
12211
12212static void
12213check_final_variable_local_assignment_flag (class, mdecl)
12214 tree class;
12215 tree mdecl;
12216{
12217 tree field;
12218 int initialized = 0;
12219 int non_initialized = 0;
12220
12221 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12222 return;
12223
12224 /* First find out whether all final variables or no final variable
12225 are initialized in this ctor. We don't take into account final
12226 variable that have been initialized upon declaration. */
12227 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12228 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12229 {
12230 if (DECL_FIELD_FINAL_LIIC (field))
12231 initialized++;
12232 else
12233 non_initialized++;
12234 }
12235
12236 /* There were no non initialized variable and no initialized variable.
12237 This ctor is fine. */
12238 if (!non_initialized && !initialized)
12239 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12240 /* If no variables have been initialized, that fine. We'll check
12241 later whether this ctor calls a constructor which initializes
12242 them. We mark the ctor as not initializing all its finals. */
12243 else if (initialized == 0)
12244 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12245 /* If we have a mixed bag, then we have a problem. We need to report
12246 all the variables we're not initializing. */
12247 else if (initialized && non_initialized)
12248 {
12249 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12250 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12251 if (FIELD_FINAL (field)
12252 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12253 {
12254 parse_error_context
12255 (lookup_cl (mdecl),
12256 "Blank final variable `%s' may not have been initialized in this constructor",
12257 IDENTIFIER_POINTER (DECL_NAME (field)));
12258 DECL_FIELD_FINAL_IERR (field) = 1;
12259 }
12260 }
12261 /* Otherwise we know this ctor is initializing all its final
12262 variable. We mark it so. */
12263 else
12264 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12265}
12266
12267/* This function recurses in a simple what through STMT and stops when
12268 it finds a constructor call. It then verifies that the called
12269 constructor initialized its final properly. Return 1 upon success,
12270 0 or -1 otherwise. */
12271
12272static int
12273check_final_variable_indirect_assignment (stmt)
12274 tree stmt;
12275{
12276 int res;
12277 switch (TREE_CODE (stmt))
12278 {
12279 case EXPR_WITH_FILE_LOCATION:
12280 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12281 case COMPOUND_EXPR:
12282 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12283 if (res)
12284 return res;
12285 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12286 case SAVE_EXPR:
12287 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12288 case CALL_EXPR:
12289 {
12290 tree decl = TREE_OPERAND (stmt, 0);
12291 tree fbody;
12292
12293 if (TREE_CODE (decl) != FUNCTION_DECL)
12294 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12295 if (TREE_CODE (decl) != FUNCTION_DECL)
12296 fatal ("Can't find FUNCTION_DECL in CALL_EXPR - check_final_variable_indirect_assignment");
12297 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12298 return 1;
12299 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12300 return -1;
12301 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12302 if (fbody == error_mark_node)
12303 return -1;
12304 fbody = BLOCK_EXPR_BODY (fbody);
12305 return check_final_variable_indirect_assignment (fbody);
12306 }
12307 default:
12308 break;
12309 }
12310 return 0;
12311}
12312
12313/* This is the last chance to catch a final variable initialization
12314 problem. This routine will report an error if a final variable was
12315 never (globally) initialized and never reported as not having been
12316 initialized properly. */
12317
12318static void
12319check_final_variable_global_assignment_flag (class)
12320 tree class;
12321{
12322 tree field, mdecl;
12323 int nnctor = 0;
12324
12325 /* We go through all natural ctors and see whether they're
12326 initializing all their final variables or not. */
12327 current_function_decl = NULL_TREE; /* For the error report. */
12328 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12329 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12330 {
12331 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12332 {
12333 /* It doesn't. Maybe it calls a constructor that initializes
12334 them. find out. */
12335 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12336 if (fbody == error_mark_node)
12337 continue;
12338 fbody = BLOCK_EXPR_BODY (fbody);
12339 if (check_final_variable_indirect_assignment (fbody) == 1)
12340 {
12341 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12342 nnctor++;
12343 }
12344 else
12345 parse_error_context
12346 (lookup_cl (mdecl),
12347 "Final variable initialization error in this constructor");
12348 }
12349 else
12350 nnctor++;
12351 }
12352
12353 /* Finally we catch final variables that never were initialized */
12354 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12355 if (FINAL_VARIABLE_P (field)
12356 /* If the field wasn't initialized upon declaration */
12357 && !DECL_FIELD_FINAL_IUD (field)
12358 /* There wasn't natural ctor in which the field could have been
12359 initialized */
12360 && !nnctor
12361 /* If we never reported a problem with this field */
12362 && !DECL_FIELD_FINAL_IERR (field))
12363 {
12364 current_function_decl = NULL;
12365 parse_error_context
12366 (DECL_FIELD_FINAL_WFL (field),
12367 "Final variable `%s' hasn't been initialized upon its declaration",
12368 IDENTIFIER_POINTER (DECL_NAME (field)));
12369 }
12370
12371}
12372
7f1d4866
APB
12373/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12374 context. */
5e942c50
APB
12375
12376static int
12377check_final_assignment (lvalue, wfl)
12378 tree lvalue, wfl;
12379{
c7303e41
APB
12380 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12381 return 0;
12382
12383 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12384 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12385 lvalue = TREE_OPERAND (lvalue, 1);
12386
c7303e41
APB
12387 if (!FIELD_FINAL (lvalue))
12388 return 0;
12389
12390 /* Now the logic. We can modify a final VARIABLE:
12391 1) in finit$, (its declaration was followed by an initialization,)
12392 2) consistently in each natural ctor, if it wasn't initialized in
12393 finit$ or once in <clinit>. In any other cases, an error should be
12394 reported. */
12395 if (DECL_FINIT_P (current_function_decl))
5e942c50 12396 {
c7303e41
APB
12397 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12398 return 0;
5e942c50 12399 }
c7303e41
APB
12400
12401 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12402 /* Only if it wasn't given a value upon initialization */
12403 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12404 /* If it was never assigned a value in this constructor */
12405 && !DECL_FIELD_FINAL_LIIC (lvalue))
12406 {
12407 /* Turn the locally assigned flag on, it will be checked later
12408 on to point out at discrepancies. */
12409 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12410 if (DECL_CLINIT_P (current_function_decl))
12411 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12412 return 0;
12413 }
12414
12415 /* Other problems should be reported right away. */
12416 parse_error_context
12417 (wfl, "Can't %sassign a value to the final variable `%s'",
12418 (FIELD_STATIC (lvalue) ? "re" : ""),
12419 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12420
12421 /* Note that static field can be initialized once and only once. */
12422 if (FIELD_STATIC (lvalue))
12423 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12424
12425 return 1;
5e942c50
APB
12426}
12427
12428/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12429 read. This is needed to avoid circularities in the implementation
12430 of these fields in libjava. */
12431
12432static tree
12433maybe_build_primttype_type_ref (rhs, wfl)
12434 tree rhs, wfl;
12435{
12436 tree to_return = NULL_TREE;
12437 tree rhs_type = TREE_TYPE (rhs);
12438 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12439 {
12440 tree n = TREE_OPERAND (rhs, 1);
12441 if (TREE_CODE (n) == VAR_DECL
12442 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12443 && rhs_type == class_ptr_type
12444 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12445 {
49f48c71 12446 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12447 if (!strncmp (self_name, "java.lang.", 10))
12448 to_return = build_primtype_type_ref (self_name);
12449 }
12450 }
12451 return (to_return ? to_return : rhs );
12452}
12453
e04a16fb
AG
12454/* 15.25 Assignment operators. */
12455
12456static tree
12457patch_assignment (node, wfl_op1, wfl_op2)
12458 tree node;
12459 tree wfl_op1;
12460 tree wfl_op2;
12461{
0a2138e2 12462 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12463 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12464 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12465 int error_found = 0;
12466 int lvalue_from_array = 0;
12467
c2952b01 12468 /* Can't assign to a (blank) final. */
5e942c50
APB
12469 if (check_final_assignment (lvalue, wfl_op1))
12470 error_found = 1;
e04a16fb
AG
12471
12472 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12473
12474 /* Lhs can be a named variable */
34f4db93 12475 if (JDECL_P (lvalue))
e04a16fb 12476 {
e04a16fb
AG
12477 lhs_type = TREE_TYPE (lvalue);
12478 }
12479 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12480 comment on reason why */
12481 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12482 {
12483 lhs_type = TREE_TYPE (lvalue);
12484 lvalue_from_array = 1;
12485 }
12486 /* Or a field access */
12487 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12488 lhs_type = TREE_TYPE (lvalue);
12489 /* Or a function return slot */
12490 else if (TREE_CODE (lvalue) == RESULT_DECL)
12491 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12492 /* Otherwise, we might want to try to write into an optimized static
12493 final, this is an of a different nature, reported further on. */
12494 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12495 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12496 {
6632dcdd 12497 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12498 {
12499 /* What we should do instead is resetting the all the flags
12500 previously set, exchange lvalue for llvalue and continue. */
12501 error_found = 1;
12502 return error_mark_node;
12503 }
12504 else
12505 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12506 }
12507 else
e04a16fb
AG
12508 {
12509 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12510 error_found = 1;
12511 }
12512
12513 rhs_type = TREE_TYPE (rhs);
b67d701b 12514 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12515 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12516
b67d701b 12517 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12518 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12519 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12520
12521 /* 15.25.2 If we have a compound assignment, convert RHS into the
12522 type of the LHS */
12523 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12524 new_rhs = convert (lhs_type, rhs);
12525
12526 /* Explicit cast required. This is an error */
12527 if (!new_rhs)
12528 {
c2e3db92
KG
12529 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12530 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12531 tree wfl;
12532 char operation [32]; /* Max size known */
12533
12534 /* If the assignment is part of a declaration, we use the WFL of
12535 the declared variable to point out the error and call it a
12536 declaration problem. If the assignment is a genuine =
12537 operator, we call is a operator `=' problem, otherwise we
12538 call it an assignment problem. In both of these last cases,
12539 we use the WFL of the operator to indicate the error. */
12540
12541 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12542 {
12543 wfl = wfl_op1;
12544 strcpy (operation, "declaration");
12545 }
12546 else
12547 {
12548 wfl = wfl_operator;
12549 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12550 strcpy (operation, "assignment");
12551 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12552 strcpy (operation, "`return'");
12553 else
12554 strcpy (operation, "`='");
12555 }
12556
1ebadc60 12557 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12558 parse_error_context
12559 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12560 operation, t1, t2);
1ebadc60 12561 else
781b0558 12562 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12563 operation, t1, t2);
e04a16fb
AG
12564 free (t1); free (t2);
12565 error_found = 1;
12566 }
12567
c877974e
APB
12568 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12569 if (new_rhs)
12570 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12571
e04a16fb
AG
12572 if (error_found)
12573 return error_mark_node;
12574
2622b947
APB
12575 /* 10.10: Array Store Exception runtime check */
12576 if (!flag_emit_class_files
e8fc7396 12577 && !flag_emit_xref
2622b947 12578 && lvalue_from_array
afc390b1 12579 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12580 {
12581 tree check;
12582 tree base = lvalue;
12583
12584 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12585 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12586 base = TREE_OPERAND (lvalue, 0);
12587 else
12588 {
12589 if (flag_bounds_check)
12590 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12591 else
12592 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12593 }
12594
12595 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12596 new_rhs = save_expr (new_rhs);
2622b947
APB
12597 check = build (CALL_EXPR, void_type_node,
12598 build_address_of (soft_checkarraystore_node),
12599 tree_cons (NULL_TREE, base,
12600 build_tree_list (NULL_TREE, new_rhs)),
12601 NULL_TREE);
12602 TREE_SIDE_EFFECTS (check) = 1;
12603
12604 /* We have to decide on an insertion point */
12605 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12606 {
12607 tree t;
12608 if (flag_bounds_check)
12609 {
12610 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12611 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12612 build (COMPOUND_EXPR, void_type_node, t, check);
12613 }
12614 else
12615 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12616 check, TREE_OPERAND (lvalue, 1));
12617 }
12618 else
12619 {
12620 /* Make sure the bound check will happen before the store check */
12621 if (flag_bounds_check)
12622 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12623 build (COMPOUND_EXPR, void_type_node,
12624 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12625 else
12626 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12627 }
12628 }
22eed1e6 12629
34d4df06
APB
12630 /* Final locals can be used as case values in switch
12631 statement. Prepare them for this eventuality. */
12632 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12633 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12634 && TREE_CONSTANT (new_rhs)
12635 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12636 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12637 )
12638 {
12639 TREE_CONSTANT (lvalue) = 1;
12640 DECL_INITIAL (lvalue) = new_rhs;
12641 }
12642
e04a16fb
AG
12643 TREE_OPERAND (node, 0) = lvalue;
12644 TREE_OPERAND (node, 1) = new_rhs;
12645 TREE_TYPE (node) = lhs_type;
12646 return node;
12647}
12648
b67d701b
PB
12649/* Check that type SOURCE can be cast into type DEST. If the cast
12650 can't occur at all, return 0 otherwise 1. This function is used to
12651 produce accurate error messages on the reasons why an assignment
12652 failed. */
e04a16fb 12653
b67d701b
PB
12654static tree
12655try_reference_assignconv (lhs_type, rhs)
12656 tree lhs_type, rhs;
e04a16fb 12657{
b67d701b
PB
12658 tree new_rhs = NULL_TREE;
12659 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12660
b67d701b
PB
12661 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12662 {
12663 /* `null' may be assigned to any reference type */
12664 if (rhs == null_pointer_node)
12665 new_rhs = null_pointer_node;
12666 /* Try the reference assignment conversion */
12667 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12668 new_rhs = rhs;
12669 /* This is a magic assignment that we process differently */
12670 else if (rhs == soft_exceptioninfo_call_node)
12671 new_rhs = rhs;
12672 }
12673 return new_rhs;
12674}
12675
12676/* Check that RHS can be converted into LHS_TYPE by the assignment
12677 conversion (5.2), for the cases of RHS being a builtin type. Return
12678 NULL_TREE if the conversion fails or if because RHS isn't of a
12679 builtin type. Return a converted RHS if the conversion is possible. */
12680
12681static tree
12682try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12683 tree wfl_op1, lhs_type, rhs;
12684{
12685 tree new_rhs = NULL_TREE;
12686 tree rhs_type = TREE_TYPE (rhs);
12687
7e51098e
TT
12688 /* Handle boolean specially. */
12689 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12690 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12691 {
12692 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12693 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12694 new_rhs = rhs;
12695 }
12696
5e942c50 12697 /* Zero accepted everywhere */
7e51098e 12698 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12699 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12700 && JPRIMITIVE_TYPE_P (rhs_type))
12701 new_rhs = convert (lhs_type, rhs);
12702
b67d701b
PB
12703 /* 5.1.1 Try Identity Conversion,
12704 5.1.2 Try Widening Primitive Conversion */
5e942c50 12705 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12706 new_rhs = convert (lhs_type, rhs);
12707
12708 /* Try a narrowing primitive conversion (5.1.3):
12709 - expression is a constant expression of type int AND
12710 - variable is byte, short or char AND
12711 - The value of the expression is representable in the type of the
12712 variable */
12713 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12714 && (lhs_type == byte_type_node || lhs_type == char_type_node
12715 || lhs_type == short_type_node))
12716 {
12717 if (int_fits_type_p (rhs, lhs_type))
12718 new_rhs = convert (lhs_type, rhs);
12719 else if (wfl_op1) /* Might be called with a NULL */
12720 parse_warning_context
7e51098e 12721 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12722 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12723 /* Reported a warning that will turn into an error further
12724 down, so we don't return */
12725 }
12726
12727 return new_rhs;
12728}
12729
12730/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12731 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12732 0 is the conversion test fails. This implements parts the method
12733 invocation convertion (5.3). */
12734
12735static int
12736valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12737 tree lhs_type, rhs_type;
12738{
acd663ee 12739 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12740 if (lhs_type == rhs_type)
12741 return 1;
12742
7e51098e
TT
12743 /* Reject non primitive types and boolean conversions. */
12744 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12745 return 0;
12746
acd663ee
APB
12747 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12748 than a char can't be converted into a char. Short can't too, but
12749 the < test below takes care of that */
b67d701b
PB
12750 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12751 return 0;
12752
5e942c50
APB
12753 /* Accept all promoted type here. Note, we can't use <= in the test
12754 below, because we still need to bounce out assignments of short
12755 to char and the likes */
12756 if (lhs_type == int_type_node
12757 && (rhs_type == promoted_byte_type_node
12758 || rhs_type == promoted_short_type_node
12759 || rhs_type == promoted_char_type_node
12760 || rhs_type == promoted_boolean_type_node))
12761 return 1;
12762
acd663ee
APB
12763 /* From here, an integral is widened if its precision is smaller
12764 than the precision of the LHS or if the LHS is a floating point
12765 type, or the RHS is a float and the RHS a double. */
12766 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12767 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12768 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12769 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12770 return 1;
12771
12772 return 0;
e04a16fb
AG
12773}
12774
12775/* Check that something of SOURCE type can be assigned or cast to
12776 something of DEST type at runtime. Return 1 if the operation is
12777 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12778 were SOURCE is cast into DEST, which borrows a lot of the
12779 assignment check. */
12780
12781static int
12782valid_ref_assignconv_cast_p (source, dest, cast)
12783 tree source;
12784 tree dest;
12785 int cast;
12786{
09ed0f70
APB
12787 /* SOURCE or DEST might be null if not from a declared entity. */
12788 if (!source || !dest)
12789 return 0;
5e942c50
APB
12790 if (JNULLP_TYPE_P (source))
12791 return 1;
e04a16fb
AG
12792 if (TREE_CODE (source) == POINTER_TYPE)
12793 source = TREE_TYPE (source);
12794 if (TREE_CODE (dest) == POINTER_TYPE)
12795 dest = TREE_TYPE (dest);
c1eacb70
BM
12796
12797 /* If source and dest are being compiled from bytecode, they may need to
12798 be loaded. */
12799 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12800 {
12801 load_class (source, 1);
12802 safe_layout_class (source);
12803 }
12804 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12805 {
12806 load_class (dest, 1);
12807 safe_layout_class (dest);
12808 }
12809
e04a16fb
AG
12810 /* Case where SOURCE is a class type */
12811 if (TYPE_CLASS_P (source))
12812 {
12813 if (TYPE_CLASS_P (dest))
c2952b01
APB
12814 return (source == dest
12815 || inherits_from_p (source, dest)
c2952b01 12816 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12817 if (TYPE_INTERFACE_P (dest))
12818 {
12819 /* If doing a cast and SOURCE is final, the operation is
12820 always correct a compile time (because even if SOURCE
12821 does not implement DEST, a subclass of SOURCE might). */
12822 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12823 return 1;
12824 /* Otherwise, SOURCE must implement DEST */
12825 return interface_of_p (dest, source);
12826 }
12827 /* DEST is an array, cast permited if SOURCE is of Object type */
12828 return (cast && source == object_type_node ? 1 : 0);
12829 }
12830 if (TYPE_INTERFACE_P (source))
12831 {
12832 if (TYPE_CLASS_P (dest))
12833 {
12834 /* If not casting, DEST must be the Object type */
12835 if (!cast)
12836 return dest == object_type_node;
12837 /* We're doing a cast. The cast is always valid is class
12838 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12839 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12840 return 1;
12841 else
12842 return interface_of_p (source, dest);
12843 }
12844 if (TYPE_INTERFACE_P (dest))
12845 {
12846 /* If doing a cast, then if SOURCE and DEST contain method
12847 with the same signature but different return type, then
12848 this is a (compile time) error */
12849 if (cast)
12850 {
12851 tree method_source, method_dest;
12852 tree source_type;
0a2138e2 12853 tree source_sig;
e04a16fb
AG
12854 tree source_name;
12855 for (method_source = TYPE_METHODS (source); method_source;
12856 method_source = TREE_CHAIN (method_source))
12857 {
12858 source_sig =
12859 build_java_argument_signature (TREE_TYPE (method_source));
12860 source_type = TREE_TYPE (TREE_TYPE (method_source));
12861 source_name = DECL_NAME (method_source);
12862 for (method_dest = TYPE_METHODS (dest);
12863 method_dest; method_dest = TREE_CHAIN (method_dest))
12864 if (source_sig ==
12865 build_java_argument_signature (TREE_TYPE (method_dest))
12866 && source_name == DECL_NAME (method_dest)
12867 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12868 return 0;
12869 }
12870 return 1;
12871 }
12872 else
12873 return source == dest || interface_of_p (dest, source);
12874 }
ee17a290
TT
12875 else
12876 {
12877 /* Array */
12878 return (cast
12879 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12880 || (DECL_NAME (TYPE_NAME (source))
12881 == java_io_serializable)));
12882 }
e04a16fb
AG
12883 }
12884 if (TYPE_ARRAY_P (source))
12885 {
12886 if (TYPE_CLASS_P (dest))
12887 return dest == object_type_node;
09ed0f70 12888 /* Can't cast an array to an interface unless the interface is
ee17a290 12889 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12890 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12891 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12892 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12893 else /* Arrays */
12894 {
12895 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12896 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12897
b9f7e36c
APB
12898 /* In case of severe errors, they turn out null */
12899 if (!dest_element_type || !source_element_type)
12900 return 0;
e04a16fb
AG
12901 if (source_element_type == dest_element_type)
12902 return 1;
12903 return valid_ref_assignconv_cast_p (source_element_type,
12904 dest_element_type, cast);
12905 }
12906 return 0;
12907 }
12908 return 0;
12909}
12910
b67d701b
PB
12911static int
12912valid_cast_to_p (source, dest)
12913 tree source;
12914 tree dest;
12915{
12916 if (TREE_CODE (source) == POINTER_TYPE)
12917 source = TREE_TYPE (source);
12918 if (TREE_CODE (dest) == POINTER_TYPE)
12919 dest = TREE_TYPE (dest);
12920
12921 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12922 return valid_ref_assignconv_cast_p (source, dest, 1);
12923
12924 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12925 return 1;
12926
7e51098e
TT
12927 else if (TREE_CODE (source) == BOOLEAN_TYPE
12928 && TREE_CODE (dest) == BOOLEAN_TYPE)
12929 return 1;
12930
b67d701b
PB
12931 return 0;
12932}
12933
15fdcfe9
PB
12934static tree
12935do_unary_numeric_promotion (arg)
12936 tree arg;
12937{
12938 tree type = TREE_TYPE (arg);
7e51098e
TT
12939 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12940 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12941 arg = convert (int_type_node, arg);
12942 return arg;
12943}
12944
acd663ee
APB
12945/* Return a non zero value if SOURCE can be converted into DEST using
12946 the method invocation conversion rule (5.3). */
b67d701b
PB
12947static int
12948valid_method_invocation_conversion_p (dest, source)
12949 tree dest, source;
12950{
e3884b71 12951 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12952 && valid_builtin_assignconv_identity_widening_p (dest, source))
12953 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12954 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12955 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12956}
12957
e04a16fb
AG
12958/* Build an incomplete binop expression. */
12959
12960static tree
12961build_binop (op, op_location, op1, op2)
12962 enum tree_code op;
12963 int op_location;
12964 tree op1, op2;
12965{
5e942c50 12966 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12967 TREE_SIDE_EFFECTS (binop) = 1;
12968 /* Store the location of the operator, for better error report. The
12969 string of the operator will be rebuild based on the OP value. */
12970 EXPR_WFL_LINECOL (binop) = op_location;
12971 return binop;
12972}
12973
12974/* Build the string of the operator retained by NODE. If NODE is part
12975 of a compound expression, add an '=' at the end of the string. This
12976 function is called when an error needs to be reported on an
12977 operator. The string is returned as a pointer to a static character
12978 buffer. */
12979
12980static char *
12981operator_string (node)
12982 tree node;
12983{
12984#define BUILD_OPERATOR_STRING(S) \
12985 { \
12986 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12987 return buffer; \
12988 }
12989
12990 static char buffer [10];
12991 switch (TREE_CODE (node))
12992 {
12993 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12994 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12995 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12996 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12997 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12998 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12999 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13000 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13001 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13002 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13003 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13004 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13005 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13006 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13007 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13008 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13009 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13010 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13011 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 13012 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
13013 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13014 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13015 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13016 case PREINCREMENT_EXPR: /* Fall through */
13017 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13018 case PREDECREMENT_EXPR: /* Fall through */
13019 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13020 default:
13021 fatal ("unregistered operator %s - operator_string",
13022 tree_code_name [TREE_CODE (node)]);
13023 }
13024 return NULL;
13025#undef BUILD_OPERATOR_STRING
13026}
13027
5cbdba64
APB
13028/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13029
13030static int
13031java_decl_equiv (var_acc1, var_acc2)
13032 tree var_acc1, var_acc2;
13033{
13034 if (JDECL_P (var_acc1))
13035 return (var_acc1 == var_acc2);
13036
13037 return (TREE_CODE (var_acc1) == COMPONENT_REF
13038 && TREE_CODE (var_acc2) == COMPONENT_REF
13039 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13040 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13041 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13042}
13043
13044/* Return a non zero value if CODE is one of the operators that can be
13045 used in conjunction with the `=' operator in a compound assignment. */
13046
13047static int
13048binop_compound_p (code)
13049 enum tree_code code;
13050{
13051 int i;
13052 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13053 if (binop_lookup [i] == code)
13054 break;
13055
13056 return i < BINOP_COMPOUND_CANDIDATES;
13057}
13058
13059/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13060
13061static tree
13062java_refold (t)
13063 tree t;
13064{
13065 tree c, b, ns, decl;
13066
13067 if (TREE_CODE (t) != MODIFY_EXPR)
13068 return t;
13069
13070 c = TREE_OPERAND (t, 1);
13071 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13072 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13073 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13074 return t;
13075
13076 /* Now the left branch of the binary operator. */
13077 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13078 if (! (b && TREE_CODE (b) == NOP_EXPR
13079 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13080 return t;
13081
13082 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13083 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13084 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13085 return t;
13086
13087 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13088 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13089 /* It's got to be the an equivalent decl */
13090 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13091 {
13092 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13093 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13094 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13095 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13096 /* Change the right part of the BINOP_EXPR */
13097 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13098 }
13099
13100 return t;
13101}
13102
e04a16fb
AG
13103/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13104 errors but we modify NODE so that it contains the type computed
13105 according to the expression, when it's fixed. Otherwise, we write
13106 error_mark_node as the type. It allows us to further the analysis
13107 of remaining nodes and detects more errors in certain cases. */
13108
13109static tree
13110patch_binop (node, wfl_op1, wfl_op2)
13111 tree node;
13112 tree wfl_op1;
13113 tree wfl_op2;
13114{
13115 tree op1 = TREE_OPERAND (node, 0);
13116 tree op2 = TREE_OPERAND (node, 1);
13117 tree op1_type = TREE_TYPE (op1);
13118 tree op2_type = TREE_TYPE (op2);
48a840d9 13119 tree prom_type = NULL_TREE, cn;
e04a16fb 13120 int code = TREE_CODE (node);
b67d701b 13121
e04a16fb
AG
13122 /* If 1, tell the routine that we have to return error_mark_node
13123 after checking for the initialization of the RHS */
13124 int error_found = 0;
13125
e04a16fb
AG
13126 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13127
761491c8
APB
13128 /* If either op<n>_type are NULL, this might be early signs of an
13129 error situation, unless it's too early to tell (in case we're
13130 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13131 correctly so the error can be later on reported accurately. */
13132 if (! (code == PLUS_EXPR || code == NE_EXPR
13133 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13134 {
13135 tree n;
13136 if (! op1_type)
13137 {
13138 n = java_complete_tree (op1);
13139 op1_type = TREE_TYPE (n);
13140 }
13141 if (! op2_type)
13142 {
13143 n = java_complete_tree (op2);
13144 op2_type = TREE_TYPE (n);
13145 }
13146 }
13147
e04a16fb
AG
13148 switch (code)
13149 {
13150 /* 15.16 Multiplicative operators */
13151 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13152 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13153 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13154 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13155 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13156 {
7e51098e 13157 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13158 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13159 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13160 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13161 TREE_TYPE (node) = error_mark_node;
13162 error_found = 1;
13163 break;
13164 }
13165 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13166 /* Change the division operator if necessary */
13167 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13168 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13169
aa4759c1
AH
13170 if (TREE_CODE (prom_type) == INTEGER_TYPE
13171 && flag_use_divide_subroutine
13172 && ! flag_emit_class_files
13173 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13174 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13175
0b4d333e
APB
13176 /* This one is more complicated. FLOATs are processed by a
13177 function call to soft_fmod. Duplicate the value of the
13178 COMPOUND_ASSIGN_P flag. */
e04a16fb 13179 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13180 {
13181 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13182 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13183 TREE_SIDE_EFFECTS (mod)
13184 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13185 return mod;
13186 }
e04a16fb
AG
13187 break;
13188
13189 /* 15.17 Additive Operators */
13190 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13191
13192 /* Operation is valid if either one argument is a string
13193 constant, a String object or a StringBuffer crafted for the
13194 purpose of the a previous usage of the String concatenation
13195 operator */
13196
13197 if (TREE_CODE (op1) == STRING_CST
13198 || TREE_CODE (op2) == STRING_CST
13199 || JSTRING_TYPE_P (op1_type)
13200 || JSTRING_TYPE_P (op2_type)
13201 || IS_CRAFTED_STRING_BUFFER_P (op1)
13202 || IS_CRAFTED_STRING_BUFFER_P (op2))
13203 return build_string_concatenation (op1, op2);
13204
e04a16fb
AG
13205 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13206 Numeric Types */
7e51098e 13207 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13208 {
7e51098e 13209 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13210 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13211 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13212 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13213 TREE_TYPE (node) = error_mark_node;
13214 error_found = 1;
13215 break;
13216 }
13217 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13218 break;
13219
13220 /* 15.18 Shift Operators */
13221 case LSHIFT_EXPR:
13222 case RSHIFT_EXPR:
13223 case URSHIFT_EXPR:
13224 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13225 {
13226 if (!JINTEGRAL_TYPE_P (op1_type))
13227 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13228 else
1ebadc60 13229 {
7e51098e 13230 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13231 parse_error_context (wfl_operator,
781b0558 13232 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13233 operator_string (node),
13234 lang_printable_name (op2_type, 0));
13235 else
781b0558
KG
13236 parse_error_context (wfl_operator,
13237 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13238 operator_string (node),
13239 lang_printable_name (op2_type, 0));
13240 }
e04a16fb
AG
13241 TREE_TYPE (node) = error_mark_node;
13242 error_found = 1;
13243 break;
13244 }
13245
13246 /* Unary numeric promotion (5.6.1) is performed on each operand
13247 separatly */
15fdcfe9
PB
13248 op1 = do_unary_numeric_promotion (op1);
13249 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13250
13251 /* The type of the shift expression is the type of the promoted
13252 type of the left-hand operand */
13253 prom_type = TREE_TYPE (op1);
13254
c2952b01
APB
13255 /* Shift int only up to 0x1f and long up to 0x3f */
13256 if (prom_type == int_type_node)
13257 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13258 build_int_2 (0x1f, 0)));
13259 else
13260 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13261 build_int_2 (0x3f, 0)));
e04a16fb
AG
13262
13263 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13264 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13265 {
0b4d333e 13266 tree to_return;
73333a87
AH
13267 tree utype = unsigned_type (prom_type);
13268 op1 = convert (utype, op1);
e04a16fb 13269 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13270 TREE_OPERAND (node, 0) = op1;
13271 TREE_OPERAND (node, 1) = op2;
13272 TREE_TYPE (node) = utype;
0b4d333e
APB
13273 to_return = convert (prom_type, node);
13274 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13275 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13276 TREE_SIDE_EFFECTS (to_return)
13277 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13278 return to_return;
e04a16fb
AG
13279 }
13280 break;
5e942c50
APB
13281
13282 /* 15.19.1 Type Comparison Operator instaceof */
13283 case INSTANCEOF_EXPR:
13284
13285 TREE_TYPE (node) = boolean_type_node;
13286
13287 if (!(op2_type = resolve_type_during_patch (op2)))
13288 return error_mark_node;
13289
13290 /* The first operand must be a reference type or the null type */
13291 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13292 error_found = 1; /* Error reported further below */
13293
13294 /* The second operand must be a reference type */
13295 if (!JREFERENCE_TYPE_P (op2_type))
13296 {
13297 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13298 parse_error_context
13299 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13300 lang_printable_name (op2_type, 0));
13301 error_found = 1;
13302 }
13303
13304 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13305 {
13306 /* If the first operand is null, the result is always false */
13307 if (op1 == null_pointer_node)
13308 return boolean_false_node;
15fdcfe9
PB
13309 else if (flag_emit_class_files)
13310 {
13311 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13312 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13313 return node;
13314 }
5e942c50
APB
13315 /* Otherwise we have to invoke instance of to figure it out */
13316 else
67db0ce7 13317 return build_instanceof (op1, op2_type);
5e942c50
APB
13318 }
13319 /* There is no way the expression operand can be an instance of
13320 the type operand. This is a compile time error. */
13321 else
13322 {
c2e3db92 13323 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13324 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13325 parse_error_context
13326 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13327 t1, lang_printable_name (op2_type, 0));
13328 free (t1);
13329 error_found = 1;
13330 }
e04a16fb 13331
5e942c50 13332 break;
e04a16fb
AG
13333
13334 /* 15.21 Bitwise and Logical Operators */
13335 case BIT_AND_EXPR:
13336 case BIT_XOR_EXPR:
13337 case BIT_IOR_EXPR:
13338 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13339 /* Binary numeric promotion is performed on both operand and the
13340 expression retain that type */
13341 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13342
13343 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13344 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13345 /* The type of the bitwise operator expression is BOOLEAN */
13346 prom_type = boolean_type_node;
13347 else
13348 {
13349 if (!JINTEGRAL_TYPE_P (op1_type))
13350 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13351 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13352 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13353 TREE_TYPE (node) = error_mark_node;
13354 error_found = 1;
13355 /* Insert a break here if adding thing before the switch's
13356 break for this case */
13357 }
13358 break;
13359
13360 /* 15.22 Conditional-And Operator */
13361 case TRUTH_ANDIF_EXPR:
13362 /* 15.23 Conditional-Or Operator */
13363 case TRUTH_ORIF_EXPR:
13364 /* Operands must be of BOOLEAN type */
13365 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13366 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13367 {
13368 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13369 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13370 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13371 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13372 TREE_TYPE (node) = boolean_type_node;
13373 error_found = 1;
13374 break;
13375 }
13376 /* The type of the conditional operators is BOOLEAN */
13377 prom_type = boolean_type_node;
13378 break;
13379
13380 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13381 case LT_EXPR:
13382 case GT_EXPR:
13383 case LE_EXPR:
13384 case GE_EXPR:
13385 /* The type of each of the operands must be a primitive numeric
13386 type */
13387 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13388 {
13389 if (!JNUMERIC_TYPE_P (op1_type))
13390 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13391 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13392 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13393 TREE_TYPE (node) = boolean_type_node;
13394 error_found = 1;
13395 break;
13396 }
13397 /* Binary numeric promotion is performed on the operands */
13398 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13399 /* The type of the relation expression is always BOOLEAN */
13400 prom_type = boolean_type_node;
13401 break;
13402
13403 /* 15.20 Equality Operator */
13404 case EQ_EXPR:
13405 case NE_EXPR:
48a840d9
APB
13406 /* It's time for us to patch the strings. */
13407 if ((cn = patch_string (op1)))
13408 {
13409 op1 = cn;
13410 op1_type = TREE_TYPE (op1);
13411 }
13412 if ((cn = patch_string (op2)))
13413 {
13414 op2 = cn;
13415 op2_type = TREE_TYPE (op2);
13416 }
13417
e04a16fb
AG
13418 /* 15.20.1 Numerical Equality Operators == and != */
13419 /* Binary numeric promotion is performed on the operands */
5e942c50 13420 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13421 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13422
13423 /* 15.20.2 Boolean Equality Operators == and != */
13424 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13425 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13426 ; /* Nothing to do here */
13427
13428 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13429 /* Types have to be either references or the null type. If
13430 they're references, it must be possible to convert either
13431 type to the other by casting conversion. */
b9f7e36c
APB
13432 else if (op1 == null_pointer_node || op2 == null_pointer_node
13433 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13434 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13435 || valid_ref_assignconv_cast_p (op2_type,
13436 op1_type, 1))))
e04a16fb
AG
13437 ; /* Nothing to do here */
13438
13439 /* Else we have an error figure what can't be converted into
13440 what and report the error */
13441 else
13442 {
13443 char *t1;
c2e3db92 13444 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13445 parse_error_context
781b0558
KG
13446 (wfl_operator,
13447 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13448 operator_string (node), t1,
0a2138e2 13449 lang_printable_name (op2_type, 0));
e04a16fb
AG
13450 free (t1);
13451 TREE_TYPE (node) = boolean_type_node;
13452 error_found = 1;
13453 break;
13454 }
13455 prom_type = boolean_type_node;
13456 break;
13457 }
13458
e04a16fb
AG
13459 if (error_found)
13460 return error_mark_node;
13461
13462 TREE_OPERAND (node, 0) = op1;
13463 TREE_OPERAND (node, 1) = op2;
13464 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13465 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13466
ce6e9147
APB
13467 if (flag_emit_xref)
13468 return node;
13469
d1472141
PB
13470 /* fold does not respect side-effect order as required for Java but not C.
13471 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13472 * bytecode.
13473 */
13474 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13475 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13476 node = fold (node);
13477 return node;
e04a16fb
AG
13478}
13479
b67d701b
PB
13480/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13481 zero value, the value of CSTE comes after the valude of STRING */
13482
13483static tree
13484do_merge_string_cste (cste, string, string_len, after)
13485 tree cste;
49f48c71 13486 const char *string;
b67d701b
PB
13487 int string_len, after;
13488{
49f48c71 13489 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13490 int old_len = TREE_STRING_LENGTH (cste);
13491 int len = old_len + string_len;
520a57c8 13492 char *new = alloca (len+1);
354e99ce 13493
b67d701b
PB
13494 if (after)
13495 {
354e99ce
APB
13496 memcpy (new, string, string_len);
13497 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13498 }
13499 else
13500 {
354e99ce
APB
13501 memcpy (new, old, old_len);
13502 memcpy (&new [old_len], string, string_len);
b67d701b 13503 }
354e99ce 13504 new [len] = '\0';
520a57c8 13505 return build_string (len, new);
b67d701b
PB
13506}
13507
13508/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13509 new STRING_CST on success, NULL_TREE on failure */
13510
13511static tree
13512merge_string_cste (op1, op2, after)
13513 tree op1, op2;
13514 int after;
13515{
13516 /* Handle two string constants right away */
13517 if (TREE_CODE (op2) == STRING_CST)
13518 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13519 TREE_STRING_LENGTH (op2), after);
13520
13521 /* Reasonable integer constant can be treated right away */
13522 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13523 {
49f48c71
KG
13524 static const char *boolean_true = "true";
13525 static const char *boolean_false = "false";
13526 static const char *null_pointer = "null";
b67d701b 13527 char ch[3];
49f48c71 13528 const char *string;
b67d701b
PB
13529
13530 if (op2 == boolean_true_node)
13531 string = boolean_true;
13532 else if (op2 == boolean_false_node)
13533 string = boolean_false;
13534 else if (op2 == null_pointer_node)
13535 string = null_pointer;
13536 else if (TREE_TYPE (op2) == char_type_node)
13537 {
13538 ch[0] = (char )TREE_INT_CST_LOW (op2);
13539 ch[1] = '\0';
13540 string = ch;
13541 }
13542 else
13543 string = print_int_node (op2);
13544
13545 return do_merge_string_cste (op1, string, strlen (string), after);
13546 }
13547 return NULL_TREE;
13548}
13549
13550/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13551 has to be a STRING_CST and the other part must be a STRING_CST or a
13552 INTEGRAL constant. Return a new STRING_CST if the operation
13553 succeed, NULL_TREE otherwise.
13554
13555 If the case we want to optimize for space, we might want to return
13556 NULL_TREE for each invocation of this routine. FIXME */
13557
13558static tree
13559string_constant_concatenation (op1, op2)
13560 tree op1, op2;
13561{
13562 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13563 {
0a2138e2 13564 tree string, rest;
b67d701b
PB
13565 int invert;
13566
13567 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13568 rest = (string == op1 ? op2 : op1);
13569 invert = (string == op1 ? 0 : 1 );
13570
13571 /* Walk REST, only if it looks reasonable */
13572 if (TREE_CODE (rest) != STRING_CST
13573 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13574 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13575 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13576 {
13577 rest = java_complete_tree (rest);
13578 if (rest == error_mark_node)
13579 return error_mark_node;
13580 rest = fold (rest);
13581 }
13582 return merge_string_cste (string, rest, invert);
13583 }
13584 return NULL_TREE;
13585}
13586
13587/* Implement the `+' operator. Does static optimization if possible,
13588 otherwise create (if necessary) and append elements to a
13589 StringBuffer. The StringBuffer will be carried around until it is
13590 used for a function call or an assignment. Then toString() will be
13591 called on it to turn it into a String object. */
13592
13593static tree
13594build_string_concatenation (op1, op2)
13595 tree op1, op2;
13596{
13597 tree result;
dc0b3eff 13598 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13599
13600 if (flag_emit_xref)
13601 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13602
13603 /* Try to do some static optimization */
13604 if ((result = string_constant_concatenation (op1, op2)))
13605 return result;
13606
c0d87ff6
PB
13607 /* Discard empty strings on either side of the expression */
13608 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13609 {
13610 op1 = op2;
13611 op2 = NULL_TREE;
13612 }
c0d87ff6 13613 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13614 op2 = NULL_TREE;
b67d701b 13615
acd663ee 13616 /* If operands are string constant, turn then into object references */
b67d701b
PB
13617 if (TREE_CODE (op1) == STRING_CST)
13618 op1 = patch_string_cst (op1);
acd663ee 13619 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13620 op2 = patch_string_cst (op2);
13621
acd663ee
APB
13622 /* If either one of the constant is null and the other non null
13623 operand is a String object, return it. */
13624 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13625 return op1;
13626
b67d701b
PB
13627 /* If OP1 isn't already a StringBuffer, create and
13628 initialize a new one */
13629 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13630 {
13631 /* Two solutions here:
c52b5771
AG
13632 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13633 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13634 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13635 op1 = BUILD_STRING_BUFFER (op1);
13636 else
13637 {
13638 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13639 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13640 }
13641 }
13642
acd663ee
APB
13643 if (op2)
13644 {
13645 /* OP1 is no longer the last node holding a crafted StringBuffer */
13646 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13647 /* Create a node for `{new...,xxx}.append (op2)' */
13648 if (op2)
13649 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13650 }
13651
b67d701b
PB
13652 /* Mark the last node holding a crafted StringBuffer */
13653 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13654
13655 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13656 return op1;
13657}
13658
13659/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13660 StringBuffer. If no string were found to be patched, return
13661 NULL. */
13662
13663static tree
13664patch_string (node)
13665 tree node;
13666{
1179ebc2
APB
13667 if (node == error_mark_node)
13668 return error_mark_node;
b67d701b
PB
13669 if (TREE_CODE (node) == STRING_CST)
13670 return patch_string_cst (node);
13671 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13672 {
c877974e 13673 int saved = ctxp->explicit_constructor_p;
b67d701b 13674 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13675 tree ret;
13676 /* Temporary disable forbid the use of `this'. */
13677 ctxp->explicit_constructor_p = 0;
13678 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13679 /* String concatenation arguments must be evaluated in order too. */
13680 ret = force_evaluation_order (ret);
c877974e
APB
13681 /* Restore it at its previous value */
13682 ctxp->explicit_constructor_p = saved;
13683 return ret;
b67d701b
PB
13684 }
13685 return NULL_TREE;
13686}
13687
13688/* Build the internal representation of a string constant. */
13689
13690static tree
13691patch_string_cst (node)
13692 tree node;
13693{
13694 int location;
15fdcfe9
PB
13695 if (! flag_emit_class_files)
13696 {
15fdcfe9
PB
13697 node = get_identifier (TREE_STRING_POINTER (node));
13698 location = alloc_name_constant (CONSTANT_String, node);
13699 node = build_ref_from_constant_pool (location);
13700 }
cd9643f7 13701 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13702 TREE_CONSTANT (node) = 1;
13703 return node;
13704}
13705
13706/* Build an incomplete unary operator expression. */
e04a16fb
AG
13707
13708static tree
13709build_unaryop (op_token, op_location, op1)
13710 int op_token, op_location;
13711 tree op1;
13712{
13713 enum tree_code op;
13714 tree unaryop;
13715 switch (op_token)
13716 {
b67d701b 13717 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13718 case MINUS_TK: op = NEGATE_EXPR; break;
13719 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13720 case NOT_TK: op = BIT_NOT_EXPR; break;
13721 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13722 op_token);
13723 }
13724
13725 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13726 TREE_SIDE_EFFECTS (unaryop) = 1;
13727 /* Store the location of the operator, for better error report. The
13728 string of the operator will be rebuild based on the OP value. */
13729 EXPR_WFL_LINECOL (unaryop) = op_location;
13730 return unaryop;
13731}
13732
13733/* Special case for the ++/-- operators, since they require an extra
13734 argument to build, which is set to NULL and patched
13735 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13736
13737static tree
13738build_incdec (op_token, op_location, op1, is_post_p)
13739 int op_token, op_location;
13740 tree op1;
13741 int is_post_p;
13742{
13743 static enum tree_code lookup [2][2] =
13744 {
13745 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13746 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13747 };
13748 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13749 NULL_TREE, op1, NULL_TREE);
13750 TREE_SIDE_EFFECTS (node) = 1;
13751 /* Store the location of the operator, for better error report. The
13752 string of the operator will be rebuild based on the OP value. */
13753 EXPR_WFL_LINECOL (node) = op_location;
13754 return node;
13755}
13756
13757/* Build an incomplete cast operator, based on the use of the
13758 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13759 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13760 though its type is already set. */
13761
13762static tree
13763build_cast (location, type, exp)
13764 int location;
13765 tree type, exp;
13766{
13767 tree node = build1 (CONVERT_EXPR, type, exp);
13768 EXPR_WFL_LINECOL (node) = location;
13769 return node;
13770}
13771
c2952b01
APB
13772/* Build an incomplete class reference operator. */
13773static tree
13774build_incomplete_class_ref (location, class_name)
13775 int location;
13776 tree class_name;
13777{
13778 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13779 EXPR_WFL_LINECOL (node) = location;
13780 return node;
13781}
13782
13783/* Complete an incomplete class reference operator. */
13784static tree
13785patch_incomplete_class_ref (node)
13786 tree node;
13787{
13788 tree type = TREE_OPERAND (node, 0);
13789 tree ref_type;
13790
13791 if (!(ref_type = resolve_type_during_patch (type)))
13792 return error_mark_node;
13793
165f37bc 13794 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13795 {
13796 /* A class referenced by `foo.class' is initialized. */
13797 return build_class_init (ref_type, build_class_ref (ref_type));
13798 }
165f37bc
APB
13799
13800 /* If we're emitting class files and we have to deal with non
13801 primitive types, we invoke (and consider generating) the
13802 synthetic static method `class$'. */
13803 if (!TYPE_DOT_CLASS (current_class))
13804 build_dot_class_method (current_class);
f0f3a777 13805 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13806 return java_complete_tree (ref_type);
c2952b01
APB
13807}
13808
e04a16fb
AG
13809/* 15.14 Unary operators. We return error_mark_node in case of error,
13810 but preserve the type of NODE if the type is fixed. */
13811
13812static tree
13813patch_unaryop (node, wfl_op)
13814 tree node;
13815 tree wfl_op;
13816{
13817 tree op = TREE_OPERAND (node, 0);
13818 tree op_type = TREE_TYPE (op);
ab3a6dd6 13819 tree prom_type = NULL_TREE, value, decl;
c2952b01 13820 int outer_field_flag = 0;
e04a16fb
AG
13821 int code = TREE_CODE (node);
13822 int error_found = 0;
13823
13824 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13825
13826 switch (code)
13827 {
13828 /* 15.13.2 Postfix Increment Operator ++ */
13829 case POSTINCREMENT_EXPR:
13830 /* 15.13.3 Postfix Increment Operator -- */
13831 case POSTDECREMENT_EXPR:
13832 /* 15.14.1 Prefix Increment Operator ++ */
13833 case PREINCREMENT_EXPR:
13834 /* 15.14.2 Prefix Decrement Operator -- */
13835 case PREDECREMENT_EXPR:
5cbdba64 13836 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13837 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13838 /* We might be trying to change an outer field accessed using
13839 access method. */
13840 if (outer_field_flag)
13841 {
13842 /* Retrieve the decl of the field we're trying to access. We
13843 do that by first retrieving the function we would call to
13844 access the field. It has been already verified that this
13845 field isn't final */
13846 if (flag_emit_class_files)
13847 decl = TREE_OPERAND (op, 0);
13848 else
13849 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13850 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13851 }
b3edebcf 13852 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13853 else if (!JDECL_P (decl)
b3edebcf
APB
13854 && TREE_CODE (decl) != COMPONENT_REF
13855 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13856 && TREE_CODE (decl) != INDIRECT_REF
13857 && !(TREE_CODE (decl) == COMPOUND_EXPR
13858 && TREE_OPERAND (decl, 1)
13859 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13860 {
5e942c50
APB
13861 tree lvalue;
13862 /* Before screaming, check that we're not in fact trying to
13863 increment a optimized static final access, in which case
13864 we issue an different error message. */
13865 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13866 && resolve_expression_name (wfl_op, &lvalue)
13867 && check_final_assignment (lvalue, wfl_op)))
13868 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13869 operator_string (node));
e04a16fb
AG
13870 TREE_TYPE (node) = error_mark_node;
13871 error_found = 1;
13872 }
c2952b01
APB
13873
13874 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13875 error_found = 1;
13876
e04a16fb
AG
13877 /* From now on, we know that op if a variable and that it has a
13878 valid wfl. We use wfl_op to locate errors related to the
13879 ++/-- operand. */
13880 else if (!JNUMERIC_TYPE_P (op_type))
13881 {
13882 parse_error_context
13883 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13884 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13885 TREE_TYPE (node) = error_mark_node;
13886 error_found = 1;
13887 }
13888 else
13889 {
4a5f66c3 13890 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13891 both operands, if really necessary */
13892 if (JINTEGRAL_TYPE_P (op_type))
13893 {
13894 value = build_int_2 (1, 0);
13895 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13896 }
13897 else
13898 {
13899 value = build_int_2 (1, 0);
13900 TREE_TYPE (node) =
13901 binary_numeric_promotion (op_type,
13902 TREE_TYPE (value), &op, &value);
13903 }
c2952b01
APB
13904
13905 /* We remember we might be accessing an outer field */
13906 if (outer_field_flag)
13907 {
13908 /* We re-generate an access to the field */
13909 value = build (PLUS_EXPR, TREE_TYPE (op),
13910 build_outer_field_access (wfl_op, decl), value);
13911
13912 /* And we patch the original access$() into a write
13913 with plus_op as a rhs */
13914 return outer_field_access_fix (node, op, value);
13915 }
13916
5cbdba64 13917 /* And write back into the node. */
4a5f66c3 13918 TREE_OPERAND (node, 0) = op;
e04a16fb 13919 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13920 /* Convert the overall back into its original type, if
13921 necessary, and return */
13922 if (JINTEGRAL_TYPE_P (op_type))
13923 return fold (node);
13924 else
13925 return fold (convert (op_type, node));
e04a16fb
AG
13926 }
13927 break;
13928
13929 /* 15.14.3 Unary Plus Operator + */
b67d701b 13930 case UNARY_PLUS_EXPR:
e04a16fb
AG
13931 /* 15.14.4 Unary Minus Operator - */
13932 case NEGATE_EXPR:
13933 if (!JNUMERIC_TYPE_P (op_type))
13934 {
13935 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13936 TREE_TYPE (node) = error_mark_node;
13937 error_found = 1;
13938 }
13939 /* Unary numeric promotion is performed on operand */
13940 else
13941 {
15fdcfe9
PB
13942 op = do_unary_numeric_promotion (op);
13943 prom_type = TREE_TYPE (op);
b67d701b 13944 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13945 return fold (op);
e04a16fb
AG
13946 }
13947 break;
13948
13949 /* 15.14.5 Bitwise Complement Operator ~ */
13950 case BIT_NOT_EXPR:
13951 if (!JINTEGRAL_TYPE_P (op_type))
13952 {
13953 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13954 TREE_TYPE (node) = error_mark_node;
13955 error_found = 1;
13956 }
13957 else
13958 {
15fdcfe9
PB
13959 op = do_unary_numeric_promotion (op);
13960 prom_type = TREE_TYPE (op);
e04a16fb
AG
13961 }
13962 break;
13963
13964 /* 15.14.6 Logical Complement Operator ! */
13965 case TRUTH_NOT_EXPR:
13966 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13967 {
13968 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13969 /* But the type is known. We will report an error if further
13970 attempt of a assignment is made with this rhs */
e04a16fb
AG
13971 TREE_TYPE (node) = boolean_type_node;
13972 error_found = 1;
13973 }
13974 else
13975 prom_type = boolean_type_node;
13976 break;
13977
13978 /* 15.15 Cast Expression */
13979 case CONVERT_EXPR:
0a2138e2 13980 value = patch_cast (node, wfl_operator);
e04a16fb 13981 if (value == error_mark_node)
c877974e
APB
13982 {
13983 /* If this cast is part of an assignment, we tell the code
13984 that deals with it not to complain about a mismatch,
13985 because things have been cast, anyways */
13986 TREE_TYPE (node) = error_mark_node;
13987 error_found = 1;
13988 }
13989 else
dc0b3eff
PB
13990 {
13991 value = fold (value);
13992 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13993 return value;
13994 }
e04a16fb
AG
13995 break;
13996 }
13997
e04a16fb
AG
13998 if (error_found)
13999 return error_mark_node;
4a5f66c3
APB
14000
14001 /* There are cases where node has been replaced by something else
14002 and we don't end up returning here: UNARY_PLUS_EXPR,
14003 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 14004 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 14005 TREE_TYPE (node) = prom_type;
dc0b3eff 14006 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
14007 return fold (node);
14008}
14009
14010/* Generic type resolution that sometimes takes place during node
14011 patching. Returned the resolved type or generate an error
14012 message. Return the resolved type or NULL_TREE. */
14013
14014static tree
14015resolve_type_during_patch (type)
14016 tree type;
14017{
14018 if (unresolved_type_p (type, NULL))
14019 {
4142b247 14020 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
14021 if (!type_decl)
14022 {
14023 parse_error_context (type,
14024 "Class `%s' not found in type declaration",
14025 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14026 return NULL_TREE;
14027 }
14028 else
5e942c50
APB
14029 {
14030 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
14031 return TREE_TYPE (type_decl);
14032 }
e04a16fb
AG
14033 }
14034 return type;
14035}
14036/* 5.5 Casting Conversion. error_mark_node is returned if an error is
14037 found. Otherwise NODE or something meant to replace it is returned. */
14038
14039static tree
19e223db 14040patch_cast (node, wfl_op)
e04a16fb 14041 tree node;
19e223db 14042 tree wfl_op;
e04a16fb
AG
14043{
14044 tree op = TREE_OPERAND (node, 0);
14045 tree op_type = TREE_TYPE (op);
14046 tree cast_type = TREE_TYPE (node);
14047 char *t1;
14048
14049 /* First resolve OP_TYPE if unresolved */
14050 if (!(cast_type = resolve_type_during_patch (cast_type)))
14051 return error_mark_node;
14052
14053 /* Check on cast that are proven correct at compile time */
14054 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14055 {
e04a16fb
AG
14056 /* Same type */
14057 if (cast_type == op_type)
14058 return node;
14059
0b4d333e
APB
14060 /* float and double type are converted to the original type main
14061 variant and then to the target type. */
14062 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14063 op = convert (integer_type_node, op);
14064
e04a16fb
AG
14065 /* Try widening/narowwing convertion. Potentially, things need
14066 to be worked out in gcc so we implement the extreme cases
14067 correctly. fold_convert() needs to be fixed. */
14068 return convert (cast_type, op);
14069 }
14070
0b4d333e
APB
14071 /* It's also valid to cast a boolean into a boolean */
14072 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14073 return node;
14074
5e942c50
APB
14075 /* null can be casted to references */
14076 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14077 return build_null_of_type (cast_type);
14078
e04a16fb
AG
14079 /* The remaining legal casts involve conversion between reference
14080 types. Check for their compile time correctness. */
14081 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14082 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14083 {
14084 TREE_TYPE (node) = promote_type (cast_type);
14085 /* Now, the case can be determined correct at compile time if
14086 OP_TYPE can be converted into CAST_TYPE by assignment
14087 conversion (5.2) */
14088
14089 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14090 {
14091 TREE_SET_CODE (node, NOP_EXPR);
14092 return node;
14093 }
14094
14095 if (flag_emit_class_files)
14096 {
14097 TREE_SET_CODE (node, CONVERT_EXPR);
14098 return node;
14099 }
e04a16fb
AG
14100
14101 /* The cast requires a run-time check */
14102 return build (CALL_EXPR, promote_type (cast_type),
14103 build_address_of (soft_checkcast_node),
14104 tree_cons (NULL_TREE, build_class_ref (cast_type),
14105 build_tree_list (NULL_TREE, op)),
14106 NULL_TREE);
14107 }
14108
14109 /* Any other casts are proven incorrect at compile time */
c2e3db92 14110 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14111 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14112 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14113 free (t1);
14114 return error_mark_node;
14115}
14116
5e942c50
APB
14117/* Build a null constant and give it the type TYPE. */
14118
14119static tree
14120build_null_of_type (type)
14121 tree type;
14122{
14123 tree node = build_int_2 (0, 0);
14124 TREE_TYPE (node) = promote_type (type);
14125 return node;
14126}
14127
e04a16fb
AG
14128/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14129 a list of indices. */
14130static tree
14131build_array_ref (location, array, index)
14132 int location;
14133 tree array, index;
14134{
14135 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14136 EXPR_WFL_LINECOL (node) = location;
14137 return node;
14138}
14139
14140/* 15.12 Array Access Expression */
14141
14142static tree
c877974e
APB
14143patch_array_ref (node)
14144 tree node;
e04a16fb
AG
14145{
14146 tree array = TREE_OPERAND (node, 0);
14147 tree array_type = TREE_TYPE (array);
14148 tree index = TREE_OPERAND (node, 1);
14149 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14150 int error_found = 0;
14151
14152 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14153
e04a16fb
AG
14154 if (TREE_CODE (array_type) == POINTER_TYPE)
14155 array_type = TREE_TYPE (array_type);
14156
14157 /* The array reference must be an array */
14158 if (!TYPE_ARRAY_P (array_type))
14159 {
14160 parse_error_context
781b0558
KG
14161 (wfl_operator,
14162 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14163 lang_printable_name (array_type, 0));
e04a16fb
AG
14164 TREE_TYPE (node) = error_mark_node;
14165 error_found = 1;
14166 }
14167
c2952b01 14168 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14169 type must be int */
15fdcfe9
PB
14170 index = do_unary_numeric_promotion (index);
14171 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14172 {
1ebadc60 14173 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14174 parse_error_context (wfl_operator,
14175 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14176 lang_printable_name (index_type, 0));
14177 else
781b0558
KG
14178 parse_error_context (wfl_operator,
14179 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14180 lang_printable_name (index_type, 0));
e04a16fb
AG
14181 TREE_TYPE (node) = error_mark_node;
14182 error_found = 1;
14183 }
14184
e04a16fb
AG
14185 if (error_found)
14186 return error_mark_node;
e04a16fb 14187
5e942c50 14188 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14189
7f1d4866 14190 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14191 {
15fdcfe9
PB
14192 TREE_OPERAND (node, 0) = array;
14193 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14194 }
14195 else
939d7216
PB
14196 {
14197 /* The save_expr is for correct evaluation order. It would be cleaner
14198 to use force_evaluation_order (see comment there), but that is
14199 difficult when we also have to deal with bounds checking. */
14200 if (TREE_SIDE_EFFECTS (index))
14201 array = save_expr (array);
14202 node = build_java_arrayaccess (array, array_type, index);
14203 if (TREE_SIDE_EFFECTS (index))
14204 node = build (COMPOUND_EXPR, array_type, array, node);
14205 }
e04a16fb
AG
14206 TREE_TYPE (node) = array_type;
14207 return node;
14208}
14209
14210/* 15.9 Array Creation Expressions */
14211
14212static tree
14213build_newarray_node (type, dims, extra_dims)
14214 tree type;
14215 tree dims;
14216 int extra_dims;
14217{
14218 tree node =
b67d701b 14219 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14220 build_int_2 (extra_dims, 0));
e04a16fb
AG
14221 return node;
14222}
14223
14224static tree
14225patch_newarray (node)
14226 tree node;
14227{
14228 tree type = TREE_OPERAND (node, 0);
14229 tree dims = TREE_OPERAND (node, 1);
14230 tree cdim, array_type;
14231 int error_found = 0;
14232 int ndims = 0;
14233 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14234
14235 /* Dimension types are verified. It's better for the types to be
14236 verified in order. */
14237 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14238 {
14239 int dim_error = 0;
14240 tree dim = TREE_VALUE (cdim);
14241
14242 /* Dim might have been saved during its evaluation */
dba41d30 14243 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14244
14245 /* The type of each specified dimension must be an integral type. */
14246 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14247 dim_error = 1;
14248
14249 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14250 promoted type must be int. */
14251 else
14252 {
15fdcfe9 14253 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14254 if (TREE_TYPE (dim) != int_type_node)
14255 dim_error = 1;
14256 }
14257
14258 /* Report errors on types here */
14259 if (dim_error)
14260 {
14261 parse_error_context
14262 (TREE_PURPOSE (cdim),
781b0558 14263 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14264 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14265 "Explicit cast needed to" : "Can't"),
0a2138e2 14266 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14267 error_found = 1;
14268 }
14269
e04a16fb
AG
14270 TREE_PURPOSE (cdim) = NULL_TREE;
14271 }
14272
14273 /* Resolve array base type if unresolved */
14274 if (!(type = resolve_type_during_patch (type)))
14275 error_found = 1;
14276
14277 if (error_found)
14278 {
14279 /* We don't want further evaluation of this bogus array creation
14280 operation */
14281 TREE_TYPE (node) = error_mark_node;
14282 return error_mark_node;
14283 }
14284
15fdcfe9
PB
14285 /* Set array_type to the actual (promoted) array type of the result. */
14286 if (TREE_CODE (type) == RECORD_TYPE)
14287 type = build_pointer_type (type);
14288 while (--xdims >= 0)
14289 {
14290 type = promote_type (build_java_array_type (type, -1));
14291 }
14292 dims = nreverse (dims);
14293 array_type = type;
14294 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14295 {
14296 type = array_type;
05bccae2
RK
14297 array_type
14298 = build_java_array_type (type,
14299 TREE_CODE (cdim) == INTEGER_CST
14300 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14301 : -1);
15fdcfe9
PB
14302 array_type = promote_type (array_type);
14303 }
14304 dims = nreverse (dims);
14305
e04a16fb
AG
14306 /* The node is transformed into a function call. Things are done
14307 differently according to the number of dimensions. If the number
14308 of dimension is equal to 1, then the nature of the base type
14309 (primitive or not) matters. */
15fdcfe9 14310 if (ndims == 1)
fdec99c6 14311 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14312
e04a16fb
AG
14313 /* Can't reuse what's already written in expr.c because it uses the
14314 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14315 return build (CALL_EXPR, array_type,
e04a16fb 14316 build_address_of (soft_multianewarray_node),
15fdcfe9 14317 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14318 tree_cons (NULL_TREE,
15fdcfe9 14319 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14320 NULL_TREE);
14321}
14322
f8976021
APB
14323/* 10.6 Array initializer. */
14324
14325/* Build a wfl for array element that don't have one, so we can
14326 pin-point errors. */
14327
14328static tree
14329maybe_build_array_element_wfl (node)
14330 tree node;
14331{
14332 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14333 return build_expr_wfl (NULL_TREE, ctxp->filename,
14334 ctxp->elc.line, ctxp->elc.prev_col);
14335 else
14336 return NULL_TREE;
14337}
14338
14339/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14340 identification of initialized arrays easier to detect during walk
14341 and expansion. */
14342
14343static tree
14344build_new_array_init (location, values)
14345 int location;
14346 tree values;
14347{
14348 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14349 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14350 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14351 return to_return;
14352}
14353
14354/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14355 occurred. Otherwise return NODE after having set its type
14356 appropriately. */
14357
14358static tree
14359patch_new_array_init (type, node)
14360 tree type, node;
f8976021
APB
14361{
14362 int error_seen = 0;
fdec99c6 14363 tree current, element_type;
f8976021 14364 HOST_WIDE_INT length;
fdec99c6
PB
14365 int all_constant = 1;
14366 tree init = TREE_OPERAND (node, 0);
f8976021 14367
fdec99c6
PB
14368 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14369 {
14370 parse_error_context (node,
14371 "Invalid array initializer for non-array type `%s'",
14372 lang_printable_name (type, 1));
14373 return error_mark_node;
14374 }
14375 type = TREE_TYPE (type);
14376 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14377
fdec99c6
PB
14378 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14379
14380 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14381 current; length++, current = TREE_CHAIN (current))
f8976021 14382 {
fdec99c6
PB
14383 tree elt = TREE_VALUE (current);
14384 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14385 {
fdec99c6 14386 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14387 elt = TREE_VALUE (current);
14388 /* When compiling to native code, STRING_CST is converted to
14389 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14390 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14391 all_constant = 0;
f8976021 14392 }
fdec99c6
PB
14393 else
14394 {
14395 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14396 TREE_PURPOSE (current) = NULL_TREE;
14397 all_constant = 0;
14398 }
9a7ab4b3
APB
14399 if (elt && TREE_CODE (elt) == TREE_LIST
14400 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14401 error_seen = 1;
f8976021
APB
14402 }
14403
14404 if (error_seen)
14405 return error_mark_node;
14406
14407 /* Create a new type. We can't reuse the one we have here by
14408 patching its dimension because it originally is of dimension -1
14409 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14410 type = build_java_array_type (element_type, length);
14411 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14412 TREE_TYPE (node) = promote_type (type);
14413 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14414 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14415 return node;
14416}
14417
14418/* Verify that one entry of the initializer element list can be
14419 assigned to the array base type. Report 1 if an error occurred, 0
14420 otherwise. */
14421
14422static int
14423array_constructor_check_entry (type, entry)
14424 tree type, entry;
14425{
14426 char *array_type_string = NULL; /* For error reports */
14427 tree value, type_value, new_value, wfl_value, patched;
14428 int error_seen = 0;
14429
14430 new_value = NULL_TREE;
14431 wfl_value = TREE_VALUE (entry);
14432
f8976021 14433 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14434 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14435 if ((patched = patch_string (value)))
14436 value = patched;
1179ebc2
APB
14437 if (value == error_mark_node)
14438 return 1;
f8976021 14439
f8976021
APB
14440 type_value = TREE_TYPE (value);
14441
1179ebc2 14442 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14443 constant overflow during narrowing. */
14444 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14445 new_value = try_builtin_assignconv (wfl_operator, type, value);
14446 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14447 type_value = promote_type (type);
100f7cd8 14448
f8976021
APB
14449 /* Check and report errors */
14450 if (!new_value)
14451 {
49f48c71 14452 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14453 "Can't" : "Explicit cast needed to");
14454 if (!array_type_string)
c2e3db92 14455 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14456 parse_error_context
14457 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14458 msg, lang_printable_name (type_value, 1), array_type_string);
14459 error_seen = 1;
14460 }
14461
14462 if (new_value)
14463 {
b8c5b1c6 14464 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14465 TREE_VALUE (entry) = new_value;
14466 }
14467
14468 if (array_type_string)
14469 free (array_type_string);
14470
14471 TREE_PURPOSE (entry) = NULL_TREE;
14472 return error_seen;
14473}
14474
e04a16fb
AG
14475static tree
14476build_this (location)
14477 int location;
14478{
9ee9b555 14479 tree node = build_wfl_node (this_identifier_node);
b67d701b 14480 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14481 EXPR_WFL_LINECOL (node) = location;
14482 return node;
14483}
14484
14485/* 14.15 The return statement. It builds a modify expression that
14486 assigns the returned value to the RESULT_DECL that hold the value
14487 to be returned. */
14488
14489static tree
14490build_return (location, op)
14491 int location;
14492 tree op;
14493{
14494 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14495 EXPR_WFL_LINECOL (node) = location;
b67d701b 14496 node = build_debugable_stmt (location, node);
e04a16fb
AG
14497 return node;
14498}
14499
14500static tree
14501patch_return (node)
14502 tree node;
14503{
14504 tree return_exp = TREE_OPERAND (node, 0);
14505 tree meth = current_function_decl;
14506 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14507 int error_found = 0;
14508
14509 TREE_TYPE (node) = error_mark_node;
14510 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14511
14512 /* It's invalid to have a return value within a function that is
14513 declared with the keyword void or that is a constructor */
14514 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14515 error_found = 1;
14516
f099f336 14517 /* It's invalid to use a return statement in a static block */
c2952b01 14518 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14519 error_found = 1;
14520
e04a16fb
AG
14521 /* It's invalid to have a no return value within a function that
14522 isn't declared with the keyword `void' */
14523 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14524 error_found = 2;
c2952b01
APB
14525
14526 if (in_instance_initializer)
14527 error_found = 1;
e04a16fb
AG
14528
14529 if (error_found)
14530 {
c2952b01 14531 if (in_instance_initializer)
f099f336 14532 parse_error_context (wfl_operator,
c2952b01
APB
14533 "`return' inside instance initializer");
14534
14535 else if (DECL_CLINIT_P (current_function_decl))
14536 parse_error_context (wfl_operator,
14537 "`return' inside static initializer");
f099f336
APB
14538
14539 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14540 {
c2e3db92 14541 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14542 parse_error_context (wfl_operator,
14543 "`return' with%s value from `%s %s'",
14544 (error_found == 1 ? "" : "out"),
14545 t, lang_printable_name (meth, 0));
14546 free (t);
14547 }
14548 else
14549 parse_error_context (wfl_operator,
14550 "`return' with value from constructor `%s'",
14551 lang_printable_name (meth, 0));
e04a16fb
AG
14552 return error_mark_node;
14553 }
14554
5e942c50
APB
14555 /* If we have a return_exp, build a modify expression and expand
14556 it. Note: at that point, the assignment is declared valid, but we
14557 may want to carry some more hacks */
e04a16fb
AG
14558 if (return_exp)
14559 {
5e942c50
APB
14560 tree exp = java_complete_tree (return_exp);
14561 tree modify, patched;
14562
14563 /* If the function returned value and EXP are booleans, EXP has
14564 to be converted into the type of DECL_RESULT, which is integer
14565 (see complete_start_java_method) */
14566 if (TREE_TYPE (exp) == boolean_type_node &&
14567 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14568 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14569
14570 /* `null' can be assigned to a function returning a reference */
14571 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14572 exp == null_pointer_node)
14573 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14574
14575 if ((patched = patch_string (exp)))
14576 exp = patched;
14577
14578 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14579 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14580 modify = java_complete_tree (modify);
5e942c50 14581
e04a16fb
AG
14582 if (modify != error_mark_node)
14583 {
14584 TREE_SIDE_EFFECTS (modify) = 1;
14585 TREE_OPERAND (node, 0) = modify;
14586 }
14587 else
14588 return error_mark_node;
14589 }
14590 TREE_TYPE (node) = void_type_node;
14591 TREE_SIDE_EFFECTS (node) = 1;
14592 return node;
14593}
14594
14595/* 14.8 The if Statement */
14596
14597static tree
14598build_if_else_statement (location, expression, if_body, else_body)
14599 int location;
14600 tree expression, if_body, else_body;
14601{
14602 tree node;
e04a16fb 14603 if (!else_body)
9bbc7d9f 14604 else_body = empty_stmt_node;
e04a16fb
AG
14605 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14606 EXPR_WFL_LINECOL (node) = location;
b67d701b 14607 node = build_debugable_stmt (location, node);
e04a16fb
AG
14608 return node;
14609}
14610
14611static tree
14612patch_if_else_statement (node)
14613 tree node;
14614{
14615 tree expression = TREE_OPERAND (node, 0);
14616
14617 TREE_TYPE (node) = error_mark_node;
14618 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14619
14620 /* The type of expression must be boolean */
b67d701b
PB
14621 if (TREE_TYPE (expression) != boolean_type_node
14622 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14623 {
14624 parse_error_context
14625 (wfl_operator,
14626 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14627 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14628 return error_mark_node;
14629 }
14630
14631 TREE_TYPE (node) = void_type_node;
14632 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14633 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14634 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14635 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14636 return node;
14637}
14638
14639/* 14.6 Labeled Statements */
14640
14641/* Action taken when a lableled statement is parsed. a new
14642 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14643 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14644
14645static tree
0a2138e2 14646build_labeled_block (location, label)
e04a16fb 14647 int location;
0a2138e2 14648 tree label;
e04a16fb 14649{
b635eb2f 14650 tree label_name ;
e04a16fb 14651 tree label_decl, node;
b635eb2f
PB
14652 if (label == NULL_TREE || label == continue_identifier_node)
14653 label_name = label;
14654 else
e04a16fb 14655 {
b635eb2f
PB
14656 label_name = merge_qualified_name (label_id, label);
14657 /* Issue an error if we try to reuse a label that was previously
14658 declared */
14659 if (IDENTIFIER_LOCAL_VALUE (label_name))
14660 {
14661 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14662 parse_error_context (wfl_operator,
14663 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14664 IDENTIFIER_POINTER (label));
14665 EXPR_WFL_LINECOL (wfl_operator) =
14666 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14667 parse_error_context (wfl_operator,
14668 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14669 IDENTIFIER_POINTER (label));
14670 java_error_count--;
14671 }
e04a16fb
AG
14672 }
14673
14674 label_decl = create_label_decl (label_name);
14675 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14676 EXPR_WFL_LINECOL (node) = location;
14677 TREE_SIDE_EFFECTS (node) = 1;
14678 return node;
14679}
14680
b67d701b 14681/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14682
14683static tree
b635eb2f 14684finish_labeled_statement (lbe, statement)
e04a16fb
AG
14685 tree lbe; /* Labeled block expr */
14686 tree statement;
14687{
14688 /* In anyways, tie the loop to its statement */
14689 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14690 pop_labeled_block ();
14691 POP_LABELED_BLOCK ();
e04a16fb
AG
14692 return lbe;
14693}
14694
14695/* 14.10, 14.11, 14.12 Loop Statements */
14696
14697/* Create an empty LOOP_EXPR and make it the last in the nested loop
14698 list. */
14699
14700static tree
14701build_new_loop (loop_body)
14702 tree loop_body;
14703{
14704 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14705 TREE_SIDE_EFFECTS (loop) = 1;
14706 PUSH_LOOP (loop);
14707 return loop;
14708}
14709
14710/* Create a loop body according to the following structure:
14711 COMPOUND_EXPR
14712 COMPOUND_EXPR (loop main body)
14713 EXIT_EXPR (this order is for while/for loops.
14714 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14715 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14716 BODY end of this labeled block)
14717 INCREMENT (if any)
14718
14719 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14720 after the body, like in the do-while loop.
14721
14722 To obtain a loop, the loop body structure described above is
14723 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14724
14725 LABELED_BLOCK_EXPR
14726 LABEL_DECL (use this label to exit the loop)
14727 LOOP_EXPR
14728 <structure described above> */
e04a16fb
AG
14729
14730static tree
14731build_loop_body (location, condition, reversed)
14732 int location;
14733 tree condition;
14734 int reversed;
14735{
0a2138e2 14736 tree first, second, body;
e04a16fb
AG
14737
14738 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14739 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14740 condition = build_debugable_stmt (location, condition);
14741 TREE_SIDE_EFFECTS (condition) = 1;
14742
b635eb2f 14743 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14744 first = (reversed ? body : condition);
14745 second = (reversed ? condition : body);
14746 return
14747 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14748 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14749}
14750
14751/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14752 their order) on the current loop. Unlink the current loop from the
14753 loop list. */
14754
14755static tree
b635eb2f 14756finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14757 int location;
14758 tree condition, body;
14759 int reversed;
14760{
14761 tree to_return = ctxp->current_loop;
14762 tree loop_body = LOOP_EXPR_BODY (to_return);
14763 if (condition)
14764 {
14765 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14766 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14767 The real EXIT_EXPR is one operand further. */
14768 EXPR_WFL_LINECOL (cnode) = location;
14769 /* This one is for accurate error reports */
14770 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14771 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14772 }
14773 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14774 POP_LOOP ();
14775 return to_return;
14776}
14777
b635eb2f 14778/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14779 loops feature the condition part */
14780
14781static tree
b635eb2f 14782finish_for_loop (location, condition, update, body)
e04a16fb
AG
14783 int location;
14784 tree condition, update, body;
14785{
14786 /* Put the condition and the loop body in place */
b635eb2f 14787 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14788 /* LOOP is the current loop which has been now popped of the loop
14789 stack. Install the update block */
14790 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14791 return loop;
14792}
14793
5cbdba64
APB
14794/* Try to find the loop a block might be related to. This comprises
14795 the case where the LOOP_EXPR is found as the second operand of a
14796 COMPOUND_EXPR, because the loop happens to have an initialization
14797 part, then expressed as the first operand of the COMPOUND_EXPR. If
14798 the search finds something, 1 is returned. Otherwise, 0 is
14799 returned. The search is assumed to start from a
14800 LABELED_BLOCK_EXPR's block. */
14801
14802static tree
14803search_loop (statement)
14804 tree statement;
14805{
14806 if (TREE_CODE (statement) == LOOP_EXPR)
14807 return statement;
14808
14809 if (TREE_CODE (statement) == BLOCK)
14810 statement = BLOCK_SUBBLOCKS (statement);
14811 else
14812 return NULL_TREE;
14813
14814 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14815 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14816 statement = TREE_OPERAND (statement, 1);
14817
14818 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14819 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14820}
14821
14822/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14823 returned otherwise. */
14824
14825static int
14826labeled_block_contains_loop_p (block, loop)
14827 tree block, loop;
14828{
14829 if (!block)
14830 return 0;
14831
14832 if (LABELED_BLOCK_BODY (block) == loop)
14833 return 1;
14834
c2952b01 14835 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14836 return 1;
14837
14838 return 0;
14839}
14840
e04a16fb 14841/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14842 insert LOOP as its body. */
e04a16fb
AG
14843
14844static tree
14845patch_loop_statement (loop)
14846 tree loop;
14847{
cd9643f7 14848 tree loop_label;
5cbdba64 14849
cd9643f7 14850 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14851 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14852 return loop;
14853
cd9643f7 14854 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14855 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14856 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14857 LABELED_BLOCK_BODY (loop_label) = loop;
14858 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14859 return loop_label;
e04a16fb
AG
14860}
14861
14862/* 14.13, 14.14: break and continue Statements */
14863
14864/* Build a break or a continue statement. a null NAME indicates an
14865 unlabeled break/continue statement. */
14866
14867static tree
14868build_bc_statement (location, is_break, name)
14869 int location, is_break;
14870 tree name;
14871{
14872 tree break_continue, label_block_expr = NULL_TREE;
14873
14874 if (name)
14875 {
14876 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14877 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14878 /* Null means that we don't have a target for this named
14879 break/continue. In this case, we make the target to be the
14880 label name, so that the error can be reported accuratly in
14881 patch_bc_statement. */
14882 label_block_expr = EXPR_WFL_NODE (name);
14883 }
14884 /* Unlabeled break/continue will be handled during the
14885 break/continue patch operation */
14886 break_continue
14887 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14888
14889 IS_BREAK_STMT_P (break_continue) = is_break;
14890 TREE_SIDE_EFFECTS (break_continue) = 1;
14891 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14892 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14893 return break_continue;
14894}
14895
14896/* Verification of a break/continue statement. */
14897
14898static tree
14899patch_bc_statement (node)
14900 tree node;
14901{
14902 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14903 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14904 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14905
e04a16fb 14906 /* Having an identifier here means that the target is unknown. */
b635eb2f 14907 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14908 {
14909 parse_error_context (wfl_operator, "No label definition found for `%s'",
14910 IDENTIFIER_POINTER (bc_label));
14911 return error_mark_node;
14912 }
b635eb2f 14913 if (! IS_BREAK_STMT_P (node))
e04a16fb 14914 {
b635eb2f
PB
14915 /* It's a continue statement. */
14916 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14917 {
b635eb2f
PB
14918 if (labeled_block == NULL_TREE)
14919 {
14920 if (bc_label == NULL_TREE)
14921 parse_error_context (wfl_operator,
14922 "`continue' must be in loop");
14923 else
1504b2b4
APB
14924 parse_error_context
14925 (wfl_operator, "continue label `%s' does not name a loop",
14926 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14927 return error_mark_node;
14928 }
14929 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14930 == continue_identifier_node)
14931 && (bc_label == NULL_TREE
14932 || TREE_CHAIN (labeled_block) == bc_label))
14933 {
14934 bc_label = labeled_block;
14935 break;
14936 }
e04a16fb 14937 }
e04a16fb 14938 }
b635eb2f 14939 else if (!bc_label)
34f4db93 14940 {
b635eb2f 14941 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14942 {
b635eb2f
PB
14943 if (labeled_block == NULL_TREE)
14944 {
14945 parse_error_context (wfl_operator,
14946 "`break' must be in loop or switch");
14947 return error_mark_node;
14948 }
14949 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14950 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14951 || search_loop (target_stmt))
b635eb2f
PB
14952 {
14953 bc_label = labeled_block;
14954 break;
14955 }
e04a16fb 14956 }
e04a16fb
AG
14957 }
14958
b635eb2f 14959 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14960 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14961
e04a16fb
AG
14962 /* Our break/continue don't return values. */
14963 TREE_TYPE (node) = void_type_node;
14964 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14965 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14966 sometimes, like after a if statement) */
14967 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14968 TREE_SIDE_EFFECTS (node) = 1;
14969 return node;
14970}
14971
14972/* Process the exit expression belonging to a loop. Its type must be
14973 boolean. */
14974
14975static tree
14976patch_exit_expr (node)
14977 tree node;
14978{
14979 tree expression = TREE_OPERAND (node, 0);
14980 TREE_TYPE (node) = error_mark_node;
14981 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14982
14983 /* The type of expression must be boolean */
14984 if (TREE_TYPE (expression) != boolean_type_node)
14985 {
14986 parse_error_context
14987 (wfl_operator,
781b0558 14988 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14989 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14990 return error_mark_node;
14991 }
14992 /* Now we know things are allright, invert the condition, fold and
14993 return */
14994 TREE_OPERAND (node, 0) =
14995 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14996
14997 if (! integer_zerop (TREE_OPERAND (node, 0))
14998 && ctxp->current_loop != NULL_TREE
14999 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15000 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15001 if (! integer_onep (TREE_OPERAND (node, 0)))
15002 CAN_COMPLETE_NORMALLY (node) = 1;
15003
15004
e04a16fb
AG
15005 TREE_TYPE (node) = void_type_node;
15006 return node;
15007}
b67d701b
PB
15008
15009/* 14.9 Switch statement */
15010
15011static tree
15012patch_switch_statement (node)
15013 tree node;
15014{
c877974e 15015 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
15016
15017 /* Complete the switch expression */
15018 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15019 se_type = TREE_TYPE (se);
15020 /* The type of the switch expression must be char, byte, short or
15021 int */
2e0f0aff 15022 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
15023 {
15024 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
15025 parse_error_context (wfl_operator,
15026 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 15027 lang_printable_name (se_type, 0));
b67d701b
PB
15028 /* This is what java_complete_tree will check */
15029 TREE_OPERAND (node, 0) = error_mark_node;
15030 return error_mark_node;
15031 }
15032
15fdcfe9 15033 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
15034
15035 /* Ready to return */
15fdcfe9 15036 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
15037 {
15038 TREE_TYPE (node) = error_mark_node;
15039 return error_mark_node;
15040 }
15041 TREE_TYPE (node) = void_type_node;
15042 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 15043 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
15044 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15045 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
15046 return node;
15047}
15048
165f37bc 15049/* 14.18 The try/catch statements */
b67d701b 15050
b67d701b 15051static tree
a7d8d81f 15052build_try_statement (location, try_block, catches)
b67d701b 15053 int location;
a7d8d81f
PB
15054 tree try_block, catches;
15055{
15056 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 15057 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 15058 return node;
b67d701b
PB
15059}
15060
a7d8d81f
PB
15061static tree
15062build_try_finally_statement (location, try_block, finally)
15063 int location;
15064 tree try_block, finally;
b67d701b 15065{
a7d8d81f
PB
15066 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15067 EXPR_WFL_LINECOL (node) = location;
15068 return node;
b67d701b
PB
15069}
15070
15071static tree
15072patch_try_statement (node)
15073 tree node;
15074{
15075 int error_found = 0;
15076 tree try = TREE_OPERAND (node, 0);
15077 /* Exception handlers are considered in left to right order */
15078 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15079 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15080
15081 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15082 to process the next catch clause. We process the catch clause before
15083 the try block so that when processing the try block we can check thrown
15084 exceptions againts the caught type list. */
b67d701b
PB
15085 for (current = catch; current; current = TREE_CHAIN (current))
15086 {
15087 tree carg_decl, carg_type;
15088 tree sub_current, catch_block, catch_clause;
15089 int unreachable;
15090
b67d701b 15091 /* At this point, the structure of the catch clause is
b67d701b
PB
15092 CATCH_EXPR (catch node)
15093 BLOCK (with the decl of the parameter)
15094 COMPOUND_EXPR
7525cc04 15095 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15096 BLOCK (catch clause block)
a7d8d81f
PB
15097 */
15098 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15099 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15100 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15101
15102 /* Catch clauses can't have more than one parameter declared,
15103 but it's already enforced by the grammar. Make sure that the
15104 only parameter of the clause statement in of class Throwable
15105 or a subclass of Throwable, but that was done earlier. The
15106 catch clause parameter type has also been resolved. */
15107
15108 /* Just make sure that the catch clause parameter type inherits
15109 from java.lang.Throwable */
15110 if (!inherits_from_p (carg_type, throwable_type_node))
15111 {
15112 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15113 parse_error_context (wfl_operator,
781b0558 15114 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15115 lang_printable_name (carg_type, 0));
b67d701b
PB
15116 error_found = 1;
15117 continue;
15118 }
15119
15120 /* Partial check for unreachable catch statement: The catch
15121 clause is reachable iff is no earlier catch block A in
15122 the try statement such that the type of the catch
15123 clause's parameter is the same as or a subclass of the
15124 type of A's parameter */
15125 unreachable = 0;
15126 for (sub_current = catch;
15127 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15128 {
15129 tree sub_catch_clause, decl;
a7d8d81f 15130 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15131 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15132
15133 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15134 {
15135 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15136 parse_error_context
781b0558
KG
15137 (wfl_operator,
15138 "`catch' not reached because of the catch clause at line %d",
15139 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15140 unreachable = error_found = 1;
15141 break;
15142 }
15143 }
b67d701b
PB
15144 /* Complete the catch clause block */
15145 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15146 if (catch_block == error_mark_node)
15147 {
15148 error_found = 1;
15149 continue;
15150 }
15fdcfe9
PB
15151 if (CAN_COMPLETE_NORMALLY (catch_block))
15152 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15153 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15154
15155 if (unreachable)
15156 continue;
15157
15158 /* Things to do here: the exception must be thrown */
15159
15160 /* Link this type to the caught type list */
15161 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15162 }
15163
b9f7e36c
APB
15164 PUSH_EXCEPTIONS (caught_type_list);
15165 if ((try = java_complete_tree (try)) == error_mark_node)
15166 error_found = 1;
15fdcfe9
PB
15167 if (CAN_COMPLETE_NORMALLY (try))
15168 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15169 POP_EXCEPTIONS ();
15170
b67d701b
PB
15171 /* Verification ends here */
15172 if (error_found)
15173 return error_mark_node;
15174
15175 TREE_OPERAND (node, 0) = try;
15176 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15177 TREE_TYPE (node) = void_type_node;
15178 return node;
15179}
b9f7e36c
APB
15180
15181/* 14.17 The synchronized Statement */
15182
15183static tree
15184patch_synchronized_statement (node, wfl_op1)
15185 tree node, wfl_op1;
15186{
5a005d9e 15187 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15188 tree block = TREE_OPERAND (node, 1);
5a005d9e 15189
c7303e41 15190 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15191
15192 if (expr == error_mark_node)
15193 {
15194 block = java_complete_tree (block);
15195 return expr;
15196 }
b9f7e36c 15197
c7303e41
APB
15198 /* We might be trying to synchronize on a STRING_CST */
15199 if ((tmp = patch_string (expr)))
15200 expr = tmp;
15201
b9f7e36c 15202 /* The TYPE of expr must be a reference type */
5a005d9e 15203 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15204 {
15205 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15206 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15207 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15208 return error_mark_node;
15209 }
15210
ce6e9147
APB
15211 if (flag_emit_xref)
15212 {
15213 TREE_OPERAND (node, 0) = expr;
15214 TREE_OPERAND (node, 1) = java_complete_tree (block);
15215 CAN_COMPLETE_NORMALLY (node) = 1;
15216 return node;
15217 }
15218
b9f7e36c
APB
15219 /* Generate a try-finally for the synchronized statement, except
15220 that the handler that catches all throw exception calls
15221 _Jv_MonitorExit and then rethrow the exception.
15222 The synchronized statement is then implemented as:
15223 TRY
15224 {
15225 _Jv_MonitorEnter (expression)
15226 synchronized_block
15227 _Jv_MonitorExit (expression)
15228 }
15229 CATCH_ALL
15230 {
15231 e = _Jv_exception_info ();
15232 _Jv_MonitorExit (expression)
15233 Throw (e);
15234 } */
15235
5a005d9e
PB
15236 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15237 BUILD_MONITOR_ENTER (enter, expr_decl);
15238 BUILD_MONITOR_EXIT (exit, expr_decl);
15239 CAN_COMPLETE_NORMALLY (enter) = 1;
15240 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15241 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15242 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15243 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15244 build (COMPOUND_EXPR, NULL_TREE,
15245 build (WITH_CLEANUP_EXPR, NULL_TREE,
15246 build (COMPOUND_EXPR, NULL_TREE,
96847892 15247 assignment, enter),
5a005d9e
PB
15248 NULL_TREE, exit),
15249 block));
15250 node = build_expr_block (node, expr_decl);
15251
15252 return java_complete_tree (node);
b9f7e36c
APB
15253}
15254
15255/* 14.16 The throw Statement */
15256
15257static tree
15258patch_throw_statement (node, wfl_op1)
15259 tree node, wfl_op1;
15260{
15261 tree expr = TREE_OPERAND (node, 0);
15262 tree type = TREE_TYPE (expr);
15263 int unchecked_ok = 0, tryblock_throws_ok = 0;
15264
15265 /* Thrown expression must be assignable to java.lang.Throwable */
15266 if (!try_reference_assignconv (throwable_type_node, expr))
15267 {
15268 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15269 parse_error_context (wfl_operator,
15270 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15271 lang_printable_name (type, 0));
b9f7e36c
APB
15272 /* If the thrown expression was a reference, we further the
15273 compile-time check. */
15274 if (!JREFERENCE_TYPE_P (type))
15275 return error_mark_node;
15276 }
15277
15278 /* At least one of the following must be true */
15279
15280 /* The type of the throw expression is a not checked exception,
15281 i.e. is a unchecked expression. */
c877974e 15282 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15283
c2952b01
APB
15284 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15285 /* An instance can't throw a checked excetion unless that exception
15286 is explicitely declared in the `throws' clause of each
15287 constructor. This doesn't apply to anonymous classes, since they
15288 don't have declared constructors. */
15289 if (!unchecked_ok
15290 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15291 {
15292 tree current;
15293 for (current = TYPE_METHODS (current_class); current;
15294 current = TREE_CHAIN (current))
15295 if (DECL_CONSTRUCTOR_P (current)
15296 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15297 {
15298 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)",
15299 lang_printable_name (TREE_TYPE (expr), 0));
15300 return error_mark_node;
15301 }
15302 }
15303
b9f7e36c
APB
15304 /* Throw is contained in a try statement and at least one catch
15305 clause can receive the thrown expression or the current method is
15306 declared to throw such an exception. Or, the throw statement is
15307 contained in a method or constructor declaration and the type of
15308 the Expression is assignable to at least one type listed in the
15309 throws clause the declaration. */
b9f7e36c 15310 if (!unchecked_ok)
f099f336 15311 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15312 if (!(unchecked_ok || tryblock_throws_ok))
15313 {
15314 /* If there is a surrounding try block that has no matching
15315 clatch clause, report it first. A surrounding try block exits
15316 only if there is something after the list of checked
15317 exception thrown by the current function (if any). */
15318 if (IN_TRY_BLOCK_P ())
781b0558 15319 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 15320 lang_printable_name (type, 0));
b9f7e36c
APB
15321 /* If we have no surrounding try statement and the method doesn't have
15322 any throws, report it now. FIXME */
f099f336
APB
15323
15324 /* We report that the exception can't be throw from a try block
15325 in all circumstances but when the `throw' is inside a static
15326 block. */
b9f7e36c
APB
15327 else if (!EXCEPTIONS_P (currently_caught_type_list)
15328 && !tryblock_throws_ok)
f099f336 15329 {
c2952b01 15330 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15331 parse_error_context (wfl_operator,
15332 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15333 lang_printable_name (type, 0));
15334 else
781b0558
KG
15335 parse_error_context (wfl_operator,
15336 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15337 lang_printable_name (type, 0));
15338 }
b9f7e36c
APB
15339 /* Otherwise, the current method doesn't have the appropriate
15340 throws declaration */
15341 else
781b0558 15342 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15343 lang_printable_name (type, 0));
b9f7e36c
APB
15344 return error_mark_node;
15345 }
15346
ce6e9147 15347 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15348 BUILD_THROW (node, expr);
ce6e9147
APB
15349
15350 /* If doing xrefs, keep the location where the `throw' was seen. */
15351 if (flag_emit_xref)
15352 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15353 return node;
15354}
15355
15356/* Check that exception said to be thrown by method DECL can be
15357 effectively caught from where DECL is invoked. */
15358
15359static void
15360check_thrown_exceptions (location, decl)
15361 int location;
15362 tree decl;
15363{
15364 tree throws;
15365 /* For all the unchecked exceptions thrown by DECL */
15366 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15367 throws = TREE_CHAIN (throws))
0a2138e2 15368 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15369 {
3e78f871
PB
15370#if 1
15371 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15372 if (DECL_NAME (decl) == get_identifier ("clone"))
15373 continue;
15374#endif
b9f7e36c 15375 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15376 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15377 parse_error_context
15378 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15379 lang_printable_name (TREE_VALUE (throws), 0));
15380 else
15381 {
15382 parse_error_context
781b0558 15383 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15384 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15385 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15386 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15387 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15388 }
b9f7e36c
APB
15389 }
15390}
15391
c877974e 15392/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15393 try-catch blocks, OR is listed in the `throws' clause of the
15394 current method. */
15395
15396static int
0a2138e2 15397check_thrown_exceptions_do (exception)
b9f7e36c
APB
15398 tree exception;
15399{
15400 tree list = currently_caught_type_list;
c877974e 15401 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15402 /* First, all the nested try-catch-finally at that stage. The
15403 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15404 if (IS_UNCHECKED_EXCEPTION_P (exception))
15405 return 1;
b9f7e36c
APB
15406 while (list)
15407 {
15408 tree caught;
15409 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15410 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15411 return 1;
15412 list = TREE_CHAIN (list);
15413 }
15414 return 0;
15415}
15416
15417static void
15418purge_unchecked_exceptions (mdecl)
15419 tree mdecl;
15420{
15421 tree throws = DECL_FUNCTION_THROWS (mdecl);
15422 tree new = NULL_TREE;
15423
15424 while (throws)
15425 {
15426 tree next = TREE_CHAIN (throws);
c877974e 15427 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15428 {
15429 TREE_CHAIN (throws) = new;
15430 new = throws;
15431 }
15432 throws = next;
15433 }
15434 /* List is inverted here, but it doesn't matter */
15435 DECL_FUNCTION_THROWS (mdecl) = new;
15436}
22eed1e6
APB
15437
15438/* 15.24 Conditional Operator ?: */
15439
15440static tree
15441patch_conditional_expr (node, wfl_cond, wfl_op1)
15442 tree node, wfl_cond, wfl_op1;
15443{
15444 tree cond = TREE_OPERAND (node, 0);
15445 tree op1 = TREE_OPERAND (node, 1);
15446 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15447 tree resulting_type = NULL_TREE;
ac825856 15448 tree t1, t2, patched;
22eed1e6
APB
15449 int error_found = 0;
15450
ac825856
APB
15451 /* Operands of ?: might be StringBuffers crafted as a result of a
15452 string concatenation. Obtain a descent operand here. */
15453 if ((patched = patch_string (op1)))
15454 TREE_OPERAND (node, 1) = op1 = patched;
15455 if ((patched = patch_string (op2)))
15456 TREE_OPERAND (node, 2) = op2 = patched;
15457
15458 t1 = TREE_TYPE (op1);
15459 t2 = TREE_TYPE (op2);
15460
22eed1e6
APB
15461 /* The first expression must be a boolean */
15462 if (TREE_TYPE (cond) != boolean_type_node)
15463 {
15464 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15465 parse_error_context (wfl_operator,
15466 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15467 lang_printable_name (TREE_TYPE (cond), 0));
15468 error_found = 1;
15469 }
15470
15471 /* Second and third can be numeric, boolean (i.e. primitive),
15472 references or null. Anything else results in an error */
15473 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15474 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15475 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15476 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15477 error_found = 1;
15478
15479 /* Determine the type of the conditional expression. Same types are
15480 easy to deal with */
15481 else if (t1 == t2)
15482 resulting_type = t1;
15483
15484 /* There are different rules for numeric types */
15485 else if (JNUMERIC_TYPE_P (t1))
15486 {
15487 /* if byte/short found, the resulting type is short */
15488 if ((t1 == byte_type_node && t2 == short_type_node)
15489 || (t1 == short_type_node && t2 == byte_type_node))
15490 resulting_type = short_type_node;
15491
15492 /* If t1 is a constant int and t2 is of type byte, short or char
15493 and t1's value fits in t2, then the resulting type is t2 */
15494 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15495 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15496 resulting_type = t2;
15497
15498 /* If t2 is a constant int and t1 is of type byte, short or char
15499 and t2's value fits in t1, then the resulting type is t1 */
15500 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15501 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15502 resulting_type = t1;
15503
15504 /* Otherwise, binary numeric promotion is applied and the
15505 resulting type is the promoted type of operand 1 and 2 */
15506 else
93024893 15507 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15508 &TREE_OPERAND (node, 1),
15509 &TREE_OPERAND (node, 2));
15510 }
15511
15512 /* Cases of a reference and a null type */
15513 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15514 resulting_type = t1;
15515
15516 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15517 resulting_type = t2;
15518
15519 /* Last case: different reference types. If a type can be converted
15520 into the other one by assignment conversion, the latter
15521 determines the type of the expression */
15522 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15523 resulting_type = promote_type (t1);
15524
15525 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15526 resulting_type = promote_type (t2);
15527
15528 /* If we don't have any resulting type, we're in trouble */
15529 if (!resulting_type)
15530 {
c2e3db92 15531 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15532 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15533 parse_error_context (wfl_operator,
15534 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15535 t, lang_printable_name (t2, 0));
22eed1e6
APB
15536 free (t);
15537 error_found = 1;
15538 }
15539
15540 if (error_found)
15541 {
15542 TREE_TYPE (node) = error_mark_node;
15543 return error_mark_node;
15544 }
15545
15546 TREE_TYPE (node) = resulting_type;
15547 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15548 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15549 return node;
15550}
ac825856 15551
5b09b33e
PB
15552/* Try to constant fold NODE.
15553 If NODE is not a constant expression, return NULL_EXPR.
15554 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15555
15556static tree
15557fold_constant_for_init (node, context)
15558 tree node;
15559 tree context;
15560{
15561 tree op0, op1, val;
15562 enum tree_code code = TREE_CODE (node);
15563
ee97d354 15564 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15565 return node;
93024893 15566
5b09b33e
PB
15567 switch (code)
15568 {
5b09b33e
PB
15569 case PLUS_EXPR:
15570 case MINUS_EXPR:
bc3ca41b
PB
15571 case MULT_EXPR:
15572 case TRUNC_MOD_EXPR:
15573 case RDIV_EXPR:
5b09b33e
PB
15574 case LSHIFT_EXPR:
15575 case RSHIFT_EXPR:
15576 case URSHIFT_EXPR:
15577 case BIT_AND_EXPR:
15578 case BIT_XOR_EXPR:
15579 case BIT_IOR_EXPR:
5b09b33e
PB
15580 case TRUTH_ANDIF_EXPR:
15581 case TRUTH_ORIF_EXPR:
15582 case EQ_EXPR:
15583 case NE_EXPR:
15584 case GT_EXPR:
15585 case GE_EXPR:
15586 case LT_EXPR:
15587 case LE_EXPR:
15588 op0 = TREE_OPERAND (node, 0);
15589 op1 = TREE_OPERAND (node, 1);
15590 val = fold_constant_for_init (op0, context);
15591 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15592 return NULL_TREE;
15593 TREE_OPERAND (node, 0) = val;
15594 val = fold_constant_for_init (op1, context);
15595 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15596 return NULL_TREE;
15597 TREE_OPERAND (node, 1) = val;
15598 return patch_binop (node, op0, op1);
15599
15600 case UNARY_PLUS_EXPR:
15601 case NEGATE_EXPR:
15602 case TRUTH_NOT_EXPR:
15603 case BIT_NOT_EXPR:
15604 case CONVERT_EXPR:
15605 op0 = TREE_OPERAND (node, 0);
15606 val = fold_constant_for_init (op0, context);
15607 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15608 return NULL_TREE;
15609 TREE_OPERAND (node, 0) = val;
5a005d9e 15610 return patch_unaryop (node, op0);
5b09b33e
PB
15611 break;
15612
15613 case COND_EXPR:
15614 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15615 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15616 return NULL_TREE;
15617 TREE_OPERAND (node, 0) = val;
15618 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15619 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15620 return NULL_TREE;
15621 TREE_OPERAND (node, 1) = val;
15622 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15623 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15624 return NULL_TREE;
15625 TREE_OPERAND (node, 2) = val;
15626 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15627 : TREE_OPERAND (node, 2);
15628
15629 case VAR_DECL:
8576f094
APB
15630 case FIELD_DECL:
15631 if (! FIELD_FINAL (node)
5b09b33e
PB
15632 || DECL_INITIAL (node) == NULL_TREE)
15633 return NULL_TREE;
15634 val = DECL_INITIAL (node);
15635 /* Guard against infinite recursion. */
15636 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15637 val = fold_constant_for_init (val, node);
5b09b33e 15638 DECL_INITIAL (node) = val;
c7303e41
APB
15639 if (!val && CLASS_FINAL_VARIABLE_P (node))
15640 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15641 return val;
15642
15643 case EXPR_WITH_FILE_LOCATION:
15644 /* Compare java_complete_tree and resolve_expression_name. */
15645 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15646 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15647 {
15648 tree name = EXPR_WFL_NODE (node);
15649 tree decl;
15650 if (PRIMARY_P (node))
15651 return NULL_TREE;
15652 else if (! QUALIFIED_P (name))
15653 {
15654 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15655 if (decl == NULL_TREE
15656 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15657 return NULL_TREE;
15658 return fold_constant_for_init (decl, decl);
15659 }
15660 else
15661 {
5b09b33e
PB
15662 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15663 qualify_ambiguous_name (node);
15664 if (resolve_field_access (node, &decl, NULL)
15665 && decl != NULL_TREE)
15666 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15667 return NULL_TREE;
15668 }
15669 }
15670 else
15671 {
15672 op0 = TREE_OPERAND (node, 0);
15673 val = fold_constant_for_init (op0, context);
15674 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15675 return NULL_TREE;
15676 TREE_OPERAND (node, 0) = val;
15677 return val;
15678 }
15679
bc3ca41b
PB
15680#ifdef USE_COMPONENT_REF
15681 case IDENTIFIER:
15682 case COMPONENT_REF:
15683 ?;
15684#endif
15685
5b09b33e
PB
15686 default:
15687 return NULL_TREE;
15688 }
15689}
bc3ca41b
PB
15690
15691#ifdef USE_COMPONENT_REF
15692/* Context is 'T' for TypeName, 'P' for PackageName,
15693 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15694
15695tree
15696resolve_simple_name (name, context)
15697 tree name;
15698 int context;
15699{
15700}
15701
15702tree
15703resolve_qualified_name (name, context)
15704 tree name;
15705 int context;
15706{
15707}
15708#endif
f15b9af9
MM
15709
15710/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15711
15712static void
15713mark_parser_ctxt (p)
15714 void *p;
15715{
15716 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15717 int i;
15718
15719 if (!pc)
15720 return;
15721
15722#ifndef JC1_LITE
15723 for (i = 0; i < 11; ++i)
15724 ggc_mark_tree (pc->modifier_ctx[i]);
15725 ggc_mark_tree (pc->class_type);
15726 ggc_mark_tree (pc->function_decl);
15727 ggc_mark_tree (pc->package);
15728 ggc_mark_tree (pc->incomplete_class);
15729 ggc_mark_tree (pc->gclass_list);
15730 ggc_mark_tree (pc->class_list);
15731 ggc_mark_tree (pc->current_parsed_class);
15732 ggc_mark_tree (pc->current_parsed_class_un);
15733 ggc_mark_tree (pc->non_static_initialized);
15734 ggc_mark_tree (pc->static_initialized);
15735 ggc_mark_tree (pc->instance_initializers);
15736 ggc_mark_tree (pc->import_list);
15737 ggc_mark_tree (pc->import_demand_list);
15738 ggc_mark_tree (pc->current_loop);
15739 ggc_mark_tree (pc->current_labeled_block);
15740#endif /* JC1_LITE */
15741
15742 if (pc->next)
15743 mark_parser_ctxt (&pc->next);
15744}
This page took 2.869174 seconds and 5 git commands to generate.