]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
Window.java (addNotify): Remove peer casting hack now that gcj/312 is fixed.
[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 {
15fdcfe9
PB
1631 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
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
3890 /* Set super info and mark the class a complete */
3891 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3892 ctxp->interface_number);
3893 ctxp->interface_number = 0;
3894 CLASS_COMPLETE_P (decl) = 1;
3895 add_superinterfaces (decl, interfaces);
3896
c2952b01
APB
3897 /* Add the private this$<n> field, Replicate final locals still in
3898 scope as private final fields mangled like val$<local_name>.
3899 This doesn't not occur for top level (static) inner classes. */
3900 if (PURE_INNER_CLASS_DECL_P (decl))
3901 add_inner_class_fields (decl, current_function_decl);
3902
7f10c2e2
APB
3903 /* If doing xref, store the location at which the inherited class
3904 (if any) was seen. */
3905 if (flag_emit_xref && super)
3906 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3907
5e942c50
APB
3908 /* Eventually sets the @deprecated tag flag */
3909 CHECK_DEPRECATED (decl);
3910
165f37bc
APB
3911 /* Reset the anonymous class counter when declaring non inner classes */
3912 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3913 anonymous_class_counter = 1;
3914
e04a16fb
AG
3915 return decl;
3916}
3917
c2952b01 3918/* End a class declaration: register the statements used to create
c00f0fb2 3919 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3920 parser context if necessary. */
3921
3922static void
3923end_class_declaration (resume)
3924 int resume;
3925{
3926 /* If an error occured, context weren't pushed and won't need to be
3927 popped by a resume. */
3928 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3929
3930 java_parser_context_pop_initialized_field ();
3931 POP_CPC ();
3932 if (resume && no_error_occured)
3933 java_parser_context_resume ();
93220702
APB
3934
3935 /* We're ending a class declaration, this is a good time to reset
3936 the interface cout. Note that might have been already done in
3937 create_interface, but if at that time an inner class was being
3938 dealt with, the interface count was reset in a context created
3939 for the sake of handling inner classes declaration. */
3940 ctxp->interface_number = 0;
c2952b01
APB
3941}
3942
3943static void
3944add_inner_class_fields (class_decl, fct_decl)
3945 tree class_decl;
3946 tree fct_decl;
3947{
3948 tree block, marker, f;
3949
3950 f = add_field (TREE_TYPE (class_decl),
3951 build_current_thisn (TREE_TYPE (class_decl)),
3952 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3953 ACC_PRIVATE);
3954 FIELD_THISN (f) = 1;
3955
3956 if (!fct_decl)
3957 return;
3958
3959 for (block = GET_CURRENT_BLOCK (fct_decl);
3960 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3961 {
3962 tree decl;
3963 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3964 {
1f8f4a0b 3965 tree name, pname;
c2952b01
APB
3966 tree wfl, init, list;
3967
3968 /* Avoid non final arguments. */
c7303e41 3969 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3970 continue;
3971
3972 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3973 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3974 wfl = build_wfl_node (name);
3975 init = build_wfl_node (pname);
c2952b01 3976 /* Build an initialization for the field: it will be
c00f0fb2 3977 initialized by a parameter added to finit$, bearing a
c2952b01 3978 mangled name of the field itself (param$<n>.) The
c00f0fb2 3979 parameter is provided to finit$ by the constructor
c2952b01
APB
3980 invoking it (hence the constructor will also feature a
3981 hidden parameter, set to the value of the outer context
3982 local at the time the inner class is created.)
3983
3984 Note: we take into account all possible locals that can
3985 be accessed by the inner class. It's actually not trivial
3986 to minimize these aliases down to the ones really
3987 used. One way to do that would be to expand all regular
c00f0fb2 3988 methods first, then finit$ to get a picture of what's
c2952b01
APB
3989 used. It works with the exception that we would have to
3990 go back on all constructor invoked in regular methods to
3991 have their invokation reworked (to include the right amount
3992 of alias initializer parameters.)
3993
3994 The only real way around, I think, is a first pass to
3995 identify locals really used in the inner class. We leave
3996 the flag FIELD_LOCAL_ALIAS_USED around for that future
3997 use.
3998
3999 On the other hand, it only affect local inner classes,
c00f0fb2 4000 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4001 unecessary arguments. It's easy for a developper to keep
4002 this number of parameter down by using the `final'
4003 keyword only when necessary. For the time being, we can
4004 issue a warning on unecessary finals. FIXME */
4005 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4006 wfl, init);
4007
4008 /* Register the field. The TREE_LIST holding the part
4009 initialized/initializer will be marked ARG_FINAL_P so
4010 that the created field can be marked
4011 FIELD_LOCAL_ALIAS. */
4012 list = build_tree_list (wfl, init);
4013 ARG_FINAL_P (list) = 1;
4014 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4015 }
4016 }
4017
4018 if (!CPC_INITIALIZER_STMT (ctxp))
4019 return;
4020
4021 /* If we ever registered an alias field, insert and marker to
4022 remeber where the list ends. The second part of the list (the one
4023 featuring initialized fields) so it can be later reversed to
4024 enforce 8.5. The marker will be removed during that operation. */
4025 marker = build_tree_list (NULL_TREE, NULL_TREE);
4026 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4027 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4028}
4029
e04a16fb
AG
4030/* Can't use lookup_field () since we don't want to load the class and
4031 can't set the CLASS_LOADED_P flag */
4032
4033static tree
4034find_field (class, name)
4035 tree class;
4036 tree name;
4037{
4038 tree decl;
4039 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4040 {
4041 if (DECL_NAME (decl) == name)
4042 return decl;
4043 }
4044 return NULL_TREE;
4045}
4046
4047/* Wrap around lookup_field that doesn't potentially upset the value
4048 of CLASS */
4049
4050static tree
4051lookup_field_wrapper (class, name)
4052 tree class, name;
4053{
4054 tree type = class;
9a7ab4b3 4055 tree decl = NULL_TREE;
c877974e 4056 java_parser_context_save_global ();
f2760b27
APB
4057
4058 /* Last chance: if we're within the context of an inner class, we
4059 might be trying to access a local variable defined in an outer
4060 context. We try to look for it now. */
9a7ab4b3 4061 if (INNER_CLASS_TYPE_P (class))
f2760b27 4062 {
9a7ab4b3 4063 tree new_name;
1f8f4a0b 4064 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4065 decl = lookup_field (&type, new_name);
f2760b27
APB
4066 if (decl && decl != error_mark_node)
4067 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4068 }
9a7ab4b3
APB
4069 if (!decl || decl == error_mark_node)
4070 {
4071 type = class;
4072 decl = lookup_field (&type, name);
4073 }
f2760b27 4074
c877974e 4075 java_parser_context_restore_global ();
93024893 4076 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4077}
4078
4079/* Find duplicate field within the same class declarations and report
c583dd46
APB
4080 the error. Returns 1 if a duplicated field was found, 0
4081 otherwise. */
e04a16fb
AG
4082
4083static int
c583dd46 4084duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4085 tree new_field_name, new_type, cl;
e04a16fb
AG
4086{
4087 /* This might be modified to work with method decl as well */
c2952b01 4088 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4089 if (decl)
4090 {
c2e3db92 4091 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4092 ((TREE_CODE (new_type) == POINTER_TYPE
4093 && TREE_TYPE (new_type) == NULL_TREE) ?
4094 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4095 lang_printable_name (new_type, 1)));
c877974e
APB
4096 /* The type may not have been completed by the time we report
4097 the error */
c2e3db92 4098 char *t2 = xstrdup (purify_type_name
4a5f66c3 4099 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4100 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4101 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4102 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4103 parse_error_context
4104 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4105 t1, IDENTIFIER_POINTER (new_field_name),
4106 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4107 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4108 free (t1);
4109 free (t2);
c583dd46 4110 return 1;
e04a16fb 4111 }
c583dd46 4112 return 0;
e04a16fb
AG
4113}
4114
4115/* Field registration routine. If TYPE doesn't exist, field
4116 declarations are linked to the undefined TYPE dependency list, to
4117 be later resolved in java_complete_class () */
4118
4119static void
4120register_fields (flags, type, variable_list)
4121 int flags;
4122 tree type, variable_list;
4123{
c583dd46 4124 tree current, saved_type;
c2952b01 4125 tree class_type = NULL_TREE;
e04a16fb
AG
4126 int saved_lineno = lineno;
4127 int must_chain = 0;
4128 tree wfl = NULL_TREE;
4129
c2952b01
APB
4130 if (GET_CPC ())
4131 class_type = TREE_TYPE (GET_CPC ());
4132
4133 if (!class_type || class_type == error_mark_node)
4134 return;
4135
e04a16fb
AG
4136 /* If we're adding fields to interfaces, those fields are public,
4137 static, final */
4138 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4139 {
4140 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4141 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4142 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4143 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4144 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4145 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4146 check_modifiers ("Illegal interface member modifier `%s'", flags,
4147 INTERFACE_FIELD_MODIFIERS);
4148 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4149 }
4150
c583dd46
APB
4151 /* Obtain a suitable type for resolution, if necessary */
4152 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4153
4154 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4155 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4156
c583dd46
APB
4157 for (current = variable_list, saved_type = type; current;
4158 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4159 {
c877974e 4160 tree real_type;
c583dd46 4161 tree field_decl;
e04a16fb
AG
4162 tree cl = TREE_PURPOSE (current);
4163 tree init = TREE_VALUE (current);
4164 tree current_name = EXPR_WFL_NODE (cl);
4165
cf1748bf 4166 /* Can't declare non-final static fields in inner classes */
c2952b01 4167 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4168 && !(flags & ACC_FINAL))
c2952b01 4169 parse_error_context
cf1748bf 4170 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4171 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4172 lang_printable_name (class_type, 0));
4173
c583dd46
APB
4174 /* Process NAME, as it may specify extra dimension(s) for it */
4175 type = build_array_from_name (type, wfl, current_name, &current_name);
4176
c583dd46
APB
4177 /* Type adjustment. We may have just readjusted TYPE because
4178 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4179 a reference if we can and don't have one already. Also
4180 change the name if we have an init. */
4181 if (type != saved_type)
4182 {
1886c9d8 4183 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4184 if (init)
4185 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4186 }
e04a16fb 4187
c877974e
APB
4188 real_type = GET_REAL_TYPE (type);
4189 /* Check for redeclarations */
4190 if (duplicate_declaration_error_p (current_name, real_type, cl))
4191 continue;
4192
c583dd46 4193 /* Set lineno to the line the field was found and create a
5e942c50 4194 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4195 if (flag_emit_xref)
4196 lineno = EXPR_WFL_LINECOL (cl);
4197 else
4198 lineno = EXPR_WFL_LINENO (cl);
c877974e 4199 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4200 CHECK_DEPRECATED (field_decl);
c2952b01 4201
c7303e41
APB
4202 /* If the field denotes a final instance variable, then we
4203 allocate a LANG_DECL_SPECIFIC part to keep track of its
4204 initialization. We also mark whether the field was
4205 initialized upon it's declaration. We don't do that if the
4206 created field is an alias to a final local. */
4207 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4208 {
4209 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4210 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4211 if ((flags & ACC_STATIC) && init)
4212 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4213 }
4214
4215 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4216 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4217 hide parameters to this inner class finit$ and
4218 constructors. It also means that the field isn't final per
4219 say. */
c2952b01 4220 if (ARG_FINAL_P (current))
c7303e41
APB
4221 {
4222 FIELD_LOCAL_ALIAS (field_decl) = 1;
4223 FIELD_FINAL (field_decl) = 0;
4224 }
c583dd46
APB
4225
4226 /* Check if we must chain. */
4227 if (must_chain)
4228 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4229
c583dd46
APB
4230 /* If we have an initialization value tied to the field */
4231 if (init)
4232 {
4233 /* The field is declared static */
e04a16fb 4234 if (flags & ACC_STATIC)
e04a16fb 4235 {
7525cc04
APB
4236 /* We include the field and its initialization part into
4237 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4238 walked, field initializations will be processed and
4239 fields initialized with known constants will be taken
4240 out of <clinit> and have their DECL_INITIAL set
7525cc04 4241 appropriately. */
c2952b01
APB
4242 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4243 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4244 if (TREE_OPERAND (init, 1)
4245 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4246 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4247 }
5e942c50
APB
4248 /* A non-static field declared with an immediate initialization is
4249 to be initialized in <init>, if any. This field is remembered
4250 to be processed at the time of the generation of <init>. */
c583dd46
APB
4251 else
4252 {
c2952b01
APB
4253 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4254 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4255 }
5b09b33e 4256 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4257 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4258 }
4259 }
4260 lineno = saved_lineno;
4261}
4262
c00f0fb2
APB
4263/* Generate finit$, using the list of initialized fields to populate
4264 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4265 one(s) used to initialized the field(s) caching outer context
4266 local(s). */
22eed1e6 4267
c2952b01
APB
4268static tree
4269generate_finit (class_type)
4270 tree class_type;
22eed1e6 4271{
c2952b01
APB
4272 int count = 0;
4273 tree list = TYPE_FINIT_STMT_LIST (class_type);
4274 tree mdecl, current, parms;
4275
4276 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4277 class_type, NULL_TREE,
4278 &count);
4279 CRAFTED_PARAM_LIST_FIXUP (parms);
4280 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4281 finit_identifier_node, parms);
4282 fix_method_argument_names (parms, mdecl);
4283 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4284 mdecl, NULL_TREE);
4285 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4286 start_artificial_method_body (mdecl);
4287
c2952b01 4288 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4289 java_method_add_stmt (mdecl,
4290 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4291 current));
22eed1e6 4292 end_artificial_method_body (mdecl);
c2952b01 4293 return mdecl;
22eed1e6
APB
4294}
4295
e04a16fb 4296static void
c2952b01
APB
4297add_instance_initializer (mdecl)
4298 tree mdecl;
e04a16fb 4299{
c2952b01
APB
4300 tree current;
4301 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4302 tree compound = NULL_TREE;
e04a16fb 4303
c2952b01 4304 if (stmt_list)
e04a16fb 4305 {
c2952b01
APB
4306 for (current = stmt_list; current; current = TREE_CHAIN (current))
4307 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4308
c2952b01
APB
4309 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4310 NULL_TREE, compound));
4311 }
e04a16fb
AG
4312}
4313
4314/* Shared accros method_declarator and method_header to remember the
4315 patch stage that was reached during the declaration of the method.
4316 A method DECL is built differently is there is no patch
4317 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4318 pending on the currently defined method. */
4319
4320static int patch_stage;
4321
4322/* Check the method declaration and add the method to its current
4323 class. If the argument list is known to contain incomplete types,
4324 the method is partially added and the registration will be resume
22eed1e6
APB
4325 once the method arguments resolved. If TYPE is NULL, we're dealing
4326 with a constructor. */
e04a16fb
AG
4327
4328static tree
4329method_header (flags, type, mdecl, throws)
4330 int flags;
4331 tree type, mdecl, throws;
4332{
1886c9d8 4333 tree type_wfl = NULL_TREE;
79d13333 4334 tree meth_name = NULL_TREE;
c2952b01 4335 tree current, orig_arg, this_class = NULL;
34d4df06 4336 tree id, meth;
e04a16fb 4337 int saved_lineno;
1886c9d8 4338 int constructor_ok = 0, must_chain;
c2952b01 4339 int count;
34d4df06
APB
4340
4341 if (mdecl == error_mark_node)
4342 return error_mark_node;
4343 meth = TREE_VALUE (mdecl);
4344 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4345
4346 check_modifiers_consistency (flags);
79d13333 4347
c2952b01
APB
4348 if (GET_CPC ())
4349 this_class = TREE_TYPE (GET_CPC ());
4350
4351 if (!this_class || this_class == error_mark_node)
79d13333 4352 return NULL_TREE;
e04a16fb
AG
4353
4354 /* There are some forbidden modifiers for an abstract method and its
4355 class must be abstract as well. */
22eed1e6 4356 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4357 {
4358 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4359 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4360 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4361 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4362 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4363 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4364 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4365 parse_error_context
781b0558 4366 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4367 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4368 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4369 }
c2952b01 4370
22eed1e6
APB
4371 /* Things to be checked when declaring a constructor */
4372 if (!type)
4373 {
4374 int ec = java_error_count;
4375 /* 8.6: Constructor declarations: we might be trying to define a
4376 method without specifying a return type. */
c2952b01 4377 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4378 parse_error_context
4379 (id, "Invalid method declaration, return type required");
4380 /* 8.6.3: Constructor modifiers */
4381 else
4382 {
4383 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4384 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4385 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4386 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4387 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4388 }
4389 /* If we found error here, we don't consider it's OK to tread
4390 the method definition as a constructor, for the rest of this
4391 function */
4392 if (ec == java_error_count)
4393 constructor_ok = 1;
4394 }
e04a16fb
AG
4395
4396 /* Method declared within the scope of an interface are implicitly
4397 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4398 modifiers are checked right after. */
e04a16fb
AG
4399
4400 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4401 {
4402 /* If FLAGS isn't set because of a modifier, turn the
4403 corresponding modifier WFL to NULL so we issue a warning on
4404 the obsolete use of the modifier */
4405 if (!(flags & ACC_PUBLIC))
4406 MODIFIER_WFL (PUBLIC_TK) = NULL;
4407 if (!(flags & ACC_ABSTRACT))
4408 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4409 flags |= ACC_PUBLIC;
4410 flags |= ACC_ABSTRACT;
4411 }
4412
c2952b01
APB
4413 /* Inner class can't declare static methods */
4414 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4415 {
4416 parse_error_context
4417 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4418 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4419 lang_printable_name (this_class, 0));
4420 }
4421
e04a16fb
AG
4422 /* Modifiers context reset moved up, so abstract method declaration
4423 modifiers can be later checked. */
4424
22eed1e6
APB
4425 /* Set constructor returned type to void and method name to <init>,
4426 unless we found an error identifier the constructor (in which
4427 case we retain the original name) */
4428 if (!type)
4429 {
4430 type = void_type_node;
4431 if (constructor_ok)
4432 meth_name = init_identifier_node;
4433 }
4434 else
4435 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4436
1886c9d8
APB
4437 /* Do the returned type resolution and registration if necessary */
4438 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4439
4a5f66c3
APB
4440 if (meth_name)
4441 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4442 EXPR_WFL_NODE (id) = meth_name;
4443 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4444
4445 if (must_chain)
e04a16fb 4446 {
1886c9d8
APB
4447 patch_stage = JDEP_METHOD_RETURN;
4448 register_incomplete_type (patch_stage, type_wfl, id, type);
4449 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4450 }
4451 else
1886c9d8 4452 TREE_TYPE (meth) = type;
e04a16fb
AG
4453
4454 saved_lineno = lineno;
4455 /* When defining an abstract or interface method, the curly
4456 bracket at level 1 doesn't exist because there is no function
4457 body */
4458 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4459 EXPR_WFL_LINENO (id));
4460
5e942c50
APB
4461 /* Remember the original argument list */
4462 orig_arg = TYPE_ARG_TYPES (meth);
4463
e04a16fb
AG
4464 if (patch_stage) /* includes ret type and/or all args */
4465 {
4466 jdep *jdep;
4467 meth = add_method_1 (this_class, flags, meth_name, meth);
4468 /* Patch for the return type */
4469 if (patch_stage == JDEP_METHOD_RETURN)
4470 {
4471 jdep = CLASSD_LAST (ctxp->classd_list);
4472 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4473 }
4474 /* This is the stop JDEP. METH allows the function's signature
4475 to be computed. */
4476 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4477 }
4478 else
5e942c50
APB
4479 meth = add_method (this_class, flags, meth_name,
4480 build_java_signature (meth));
4481
c2952b01
APB
4482 /* Remember final parameters */
4483 MARK_FINAL_PARMS (meth, orig_arg);
4484
5e942c50
APB
4485 /* Fix the method argument list so we have the argument name
4486 information */
4487 fix_method_argument_names (orig_arg, meth);
4488
4489 /* Register the parameter number and re-install the current line
4490 number */
e04a16fb
AG
4491 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4492 lineno = saved_lineno;
b9f7e36c
APB
4493
4494 /* Register exception specified by the `throws' keyword for
4495 resolution and set the method decl appropriate field to the list.
4496 Note: the grammar ensures that what we get here are class
4497 types. */
4498 if (throws)
4499 {
4500 throws = nreverse (throws);
4501 for (current = throws; current; current = TREE_CHAIN (current))
4502 {
4503 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4504 NULL_TREE, NULL_TREE);
4505 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4506 &TREE_VALUE (current);
4507 }
4508 DECL_FUNCTION_THROWS (meth) = throws;
4509 }
4510
e04a16fb
AG
4511 /* We set the DECL_NAME to ID so we can track the location where
4512 the function was declared. This allow us to report
4513 redefinition error accurately. When method are verified,
4514 DECL_NAME is reinstalled properly (using the content of the
4515 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4516 when Object is being defined. Constructor <init> names will be
4517 reinstalled the same way. */
c2952b01 4518 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4519 DECL_NAME (meth) = id;
22eed1e6
APB
4520
4521 /* Set the flag if we correctly processed a constructor */
4522 if (constructor_ok)
c2952b01
APB
4523 {
4524 DECL_CONSTRUCTOR_P (meth) = 1;
4525 /* Compute and store the number of artificial parameters declared
4526 for this constructor */
4527 for (count = 0, current = TYPE_FIELDS (this_class); current;
4528 current = TREE_CHAIN (current))
4529 if (FIELD_LOCAL_ALIAS (current))
4530 count++;
4531 DECL_FUNCTION_NAP (meth) = count;
4532 }
22eed1e6 4533
5e942c50
APB
4534 /* Eventually set the @deprecated tag flag */
4535 CHECK_DEPRECATED (meth);
4536
7f10c2e2
APB
4537 /* If doing xref, store column and line number information instead
4538 of the line number only. */
4539 if (flag_emit_xref)
4540 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4541
e04a16fb
AG
4542 return meth;
4543}
4544
5e942c50
APB
4545static void
4546fix_method_argument_names (orig_arg, meth)
4547 tree orig_arg, meth;
4548{
4549 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4550 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4551 {
4552 TREE_PURPOSE (arg) = this_identifier_node;
4553 arg = TREE_CHAIN (arg);
4554 }
de4c7b02 4555 while (orig_arg != end_params_node)
5e942c50
APB
4556 {
4557 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4558 orig_arg = TREE_CHAIN (orig_arg);
4559 arg = TREE_CHAIN (arg);
4560 }
4561}
4562
22eed1e6
APB
4563/* Complete the method declaration with METHOD_BODY. */
4564
4565static void
b635eb2f 4566finish_method_declaration (method_body)
22eed1e6
APB
4567 tree method_body;
4568{
79d13333
APB
4569 int flags;
4570
4571 if (!current_function_decl)
4572 return;
4573
4574 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4575
4576 /* 8.4.5 Method Body */
4577 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4578 {
4579 tree wfl = DECL_NAME (current_function_decl);
4580 parse_error_context (wfl,
4581 "%s method `%s' can't have a body defined",
4582 (METHOD_NATIVE (current_function_decl) ?
4583 "Native" : "Abstract"),
4584 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4585 method_body = NULL_TREE;
4586 }
4587 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4588 {
4589 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4590 parse_error_context
4591 (wfl,
4592 "Non native and non abstract method `%s' must have a body defined",
4593 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4594 method_body = NULL_TREE;
4595 }
4596
2c56429a
APB
4597 if (flag_emit_class_files && method_body
4598 && TREE_CODE (method_body) == NOP_EXPR
4599 && TREE_TYPE (current_function_decl)
4600 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4601 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4602
22eed1e6
APB
4603 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4604 maybe_absorb_scoping_blocks ();
4605 /* Exit function's body */
4606 exit_block ();
4607 /* Merge last line of the function with first line, directly in the
4608 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4609 if (!flag_emit_xref)
4610 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4611
4612 /* Since function's argument's list are shared, reset the
4613 ARG_FINAL_P parameter that might have been set on some of this
4614 function parameters. */
4615 UNMARK_FINAL_PARMS (current_function_decl);
4616
f099f336
APB
4617 /* So we don't have an irrelevant function declaration context for
4618 the next static block we'll see. */
4619 current_function_decl = NULL_TREE;
22eed1e6
APB
4620}
4621
4622/* Build a an error message for constructor circularity errors. */
4623
4624static char *
4625constructor_circularity_msg (from, to)
4626 tree from, to;
4627{
4628 static char string [4096];
c2e3db92 4629 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4630 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4631 free (t);
4632 return string;
4633}
4634
4635/* Verify a circular call to METH. Return 1 if an error is found, 0
4636 otherwise. */
4637
4638static int
4639verify_constructor_circularity (meth, current)
4640 tree meth, current;
4641{
4642 static tree list = NULL_TREE;
19e223db 4643 static int initialized_p;
22eed1e6 4644 tree c;
19e223db
MM
4645
4646 /* If we haven't already registered LIST with the garbage collector,
4647 do so now. */
4648 if (!initialized_p)
4649 {
4650 ggc_add_tree_root (&list, 1);
4651 initialized_p = 1;
4652 }
4653
22eed1e6
APB
4654 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4655 {
4656 if (TREE_VALUE (c) == meth)
4657 {
4658 char *t;
4659 if (list)
4660 {
4661 tree liste;
4662 list = nreverse (list);
4663 for (liste = list; liste; liste = TREE_CHAIN (liste))
4664 {
4665 parse_error_context
c63b98cd 4666 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4667 constructor_circularity_msg
4668 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4669 java_error_count--;
4670 }
4671 }
c2e3db92 4672 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4673 parse_error_context (TREE_PURPOSE (c),
4674 "%s: recursive invocation of constructor `%s'",
4675 constructor_circularity_msg (current, meth), t);
4676 free (t);
4677 list = NULL_TREE;
4678 return 1;
4679 }
4680 }
4681 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4682 {
4683 list = tree_cons (c, current, list);
4684 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4685 return 1;
4686 list = TREE_CHAIN (list);
4687 }
4688 return 0;
4689}
4690
e04a16fb
AG
4691/* Check modifiers that can be declared but exclusively */
4692
4693static void
4694check_modifiers_consistency (flags)
4695 int flags;
4696{
4697 int acc_count = 0;
4698 tree cl = NULL_TREE;
4699
e0fc4118
TT
4700 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4701 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4702 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4703 if (acc_count > 1)
4704 parse_error_context
e0fc4118
TT
4705 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4706
4707 acc_count = 0;
4708 cl = NULL_TREE;
14d075d8
TT
4709 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4710 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4711 if (acc_count > 1)
4712 parse_error_context (cl,
4713 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4714}
4715
4716/* Check the methode header METH for abstract specifics features */
4717
4718static void
4719check_abstract_method_header (meth)
4720 tree meth;
4721{
4722 int flags = get_access_flags_from_decl (meth);
4723 /* DECL_NAME might still be a WFL node */
c877974e 4724 tree name = GET_METHOD_NAME (meth);
e04a16fb 4725
2884c41e
KG
4726 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4727 ACC_ABSTRACT, "abstract method",
4728 IDENTIFIER_POINTER (name));
4729 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4730 ACC_PUBLIC, "abstract method",
4731 IDENTIFIER_POINTER (name));
e04a16fb
AG
4732
4733 check_modifiers ("Illegal modifier `%s' for interface method",
4734 flags, INTERFACE_METHOD_MODIFIERS);
4735}
4736
4737/* Create a FUNCTION_TYPE node and start augmenting it with the
4738 declared function arguments. Arguments type that can't be resolved
4739 are left as they are, but the returned node is marked as containing
4740 incomplete types. */
4741
4742static tree
4743method_declarator (id, list)
4744 tree id, list;
4745{
4746 tree arg_types = NULL_TREE, current, node;
4747 tree meth = make_node (FUNCTION_TYPE);
4748 jdep *jdep;
e04a16fb
AG
4749
4750 patch_stage = JDEP_NO_PATCH;
c2952b01 4751
34d4df06
APB
4752 if (GET_CPC () == error_mark_node)
4753 return error_mark_node;
4754
c2952b01
APB
4755 /* If we're dealing with an inner class constructor, we hide the
4756 this$<n> decl in the name field of its parameter declaration. We
4757 also might have to hide the outer context local alias
4758 initializers. Not done when the class is a toplevel class. */
4759 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4760 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4761 {
4762 tree aliases_list, type, thisn;
4763 /* First the aliases, linked to the regular parameters */
4764 aliases_list =
4765 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4766 TREE_TYPE (GET_CPC ()),
4767 NULL_TREE, NULL);
4768 list = chainon (nreverse (aliases_list), list);
4769
4770 /* Then this$<n> */
4771 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4772 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4773 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4774 list);
4775 }
e04a16fb
AG
4776
4777 for (current = list; current; current = TREE_CHAIN (current))
4778 {
c583dd46 4779 int must_chain = 0;
e04a16fb
AG
4780 tree wfl_name = TREE_PURPOSE (current);
4781 tree type = TREE_VALUE (current);
4782 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4783 tree already, arg_node;
4784 tree type_wfl = NULL_TREE;
23a79c61 4785 tree real_type;
c583dd46
APB
4786
4787 /* Obtain a suitable type for resolution, if necessary */
4788 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4789
4790 /* Process NAME, as it may specify extra dimension(s) for it */
4791 type = build_array_from_name (type, type_wfl, name, &name);
4792 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4793
23a79c61
APB
4794 real_type = GET_REAL_TYPE (type);
4795 if (TREE_CODE (real_type) == RECORD_TYPE)
4796 {
4797 real_type = promote_type (real_type);
4798 if (TREE_CODE (type) == TREE_LIST)
4799 TREE_PURPOSE (type) = real_type;
4800 }
5e942c50 4801
e04a16fb
AG
4802 /* Check redefinition */
4803 for (already = arg_types; already; already = TREE_CHAIN (already))
4804 if (TREE_PURPOSE (already) == name)
4805 {
781b0558
KG
4806 parse_error_context
4807 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4808 IDENTIFIER_POINTER (name),
e04a16fb
AG
4809 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4810 break;
4811 }
4812
4813 /* If we've an incomplete argument type, we know there is a location
4814 to patch when the type get resolved, later. */
4815 jdep = NULL;
c583dd46 4816 if (must_chain)
e04a16fb 4817 {
c583dd46
APB
4818 patch_stage = JDEP_METHOD;
4819 type = register_incomplete_type (patch_stage,
4820 type_wfl, wfl_name, type);
4821 jdep = CLASSD_LAST (ctxp->classd_list);
4822 JDEP_MISC (jdep) = id;
e04a16fb 4823 }
c583dd46 4824
c2952b01 4825 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4826 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4827 /* Remeber arguments declared final. */
4828 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4829
e04a16fb
AG
4830 if (jdep)
4831 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4832 TREE_CHAIN (arg_node) = arg_types;
4833 arg_types = arg_node;
4834 }
de4c7b02 4835 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4836 node = build_tree_list (id, meth);
4837 return node;
4838}
4839
4840static int
4841unresolved_type_p (wfl, returned)
4842 tree wfl;
4843 tree *returned;
4844
4845{
4846 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4847 {
e04a16fb 4848 if (returned)
165f37bc
APB
4849 {
4850 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4851 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4852 *returned = TREE_TYPE (decl);
4853 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4854 *returned = TREE_TYPE (GET_CPC ());
4855 else
4856 *returned = NULL_TREE;
4857 }
e04a16fb
AG
4858 return 1;
4859 }
4860 if (returned)
4861 *returned = wfl;
4862 return 0;
4863}
4864
4865/* From NAME, build a qualified identifier node using the
4866 qualification from the current package definition. */
4867
4868static tree
98a52c2c 4869parser_qualified_classname (name)
e04a16fb
AG
4870 tree name;
4871{
c2952b01
APB
4872 tree nested_class_name;
4873
98a52c2c 4874 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4875 return nested_class_name;
4876
e04a16fb 4877 if (ctxp->package)
c2952b01 4878 return merge_qualified_name (ctxp->package, name);
e04a16fb 4879 else
c2952b01 4880 return name;
e04a16fb
AG
4881}
4882
4883/* Called once the type a interface extends is resolved. Returns 0 if
4884 everything is OK. */
4885
4886static int
4887parser_check_super_interface (super_decl, this_decl, this_wfl)
4888 tree super_decl, this_decl, this_wfl;
4889{
4890 tree super_type = TREE_TYPE (super_decl);
4891
4892 /* Has to be an interface */
c2952b01 4893 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4894 {
4895 parse_error_context
4896 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4897 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4898 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4899 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4900 "interface" : "class"),
4901 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4902 return 1;
4903 }
4904
4905 /* Check scope: same package OK, other package: OK if public */
4906 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4907 return 1;
4908
4909 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4910 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4911 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4912 return 0;
4913}
4914
4915/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4916 0 if everthing is OK. */
4917
4918static int
4919parser_check_super (super_decl, this_decl, wfl)
4920 tree super_decl, this_decl, wfl;
4921{
e04a16fb
AG
4922 tree super_type = TREE_TYPE (super_decl);
4923
4924 /* SUPER should be a CLASS (neither an array nor an interface) */
4925 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4926 {
4927 parse_error_context
4928 (wfl, "Class `%s' can't subclass %s `%s'",
4929 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4930 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4931 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4932 return 1;
4933 }
4934
4935 if (CLASS_FINAL (TYPE_NAME (super_type)))
4936 {
4937 parse_error_context (wfl, "Can't subclass final classes: %s",
4938 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4939 return 1;
4940 }
4941
4942 /* Check scope: same package OK, other package: OK if public */
4943 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4944 return 1;
4945
4946 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4947 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4948 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4949 return 0;
4950}
4951
4952/* Create a new dependency list and link it (in a LIFO manner) to the
4953 CTXP list of type dependency list. */
4954
4955static void
4956create_jdep_list (ctxp)
4957 struct parser_ctxt *ctxp;
4958{
23a79c61 4959 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4960 new->first = new->last = NULL;
4961 new->next = ctxp->classd_list;
4962 ctxp->classd_list = new;
4963}
4964
4965static jdeplist *
4966reverse_jdep_list (ctxp)
4967 struct parser_ctxt *ctxp;
4968{
4969 register jdeplist *prev = NULL, *current, *next;
4970 for (current = ctxp->classd_list; current; current = next)
4971 {
4972 next = current->next;
4973 current->next = prev;
4974 prev = current;
4975 }
4976 return prev;
4977}
4978
23a79c61
APB
4979/* Create a fake pointer based on the ID stored in
4980 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4981 registered again. */
e04a16fb
AG
4982
4983static tree
23a79c61
APB
4984obtain_incomplete_type (type_name)
4985 tree type_name;
e04a16fb 4986{
23a79c61
APB
4987 tree ptr, name;
4988
4989 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4990 name = EXPR_WFL_NODE (type_name);
4991 else if (INCOMPLETE_TYPE_P (type_name))
4992 name = TYPE_NAME (type_name);
4993 else
4994 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4995
4996 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4997 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4998 break;
4999
5000 if (!ptr)
5001 {
78d21f92
PB
5002 BUILD_PTR_FROM_NAME (ptr, name);
5003 layout_type (ptr);
e04a16fb
AG
5004 TREE_CHAIN (ptr) = ctxp->incomplete_class;
5005 ctxp->incomplete_class = ptr;
5006 }
5007
5008 return ptr;
5009}
5010
5011/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5012 non NULL instead of computing a new fake type based on WFL. The new
5013 dependency is inserted in the current type dependency list, in FIFO
5014 manner. */
5015
5016static tree
5017register_incomplete_type (kind, wfl, decl, ptr)
5018 int kind;
5019 tree wfl, decl, ptr;
5020{
23a79c61 5021 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5022
e04a16fb
AG
5023 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5024 ptr = obtain_incomplete_type (wfl);
5025
5026 JDEP_KIND (new) = kind;
5027 JDEP_DECL (new) = decl;
5028 JDEP_SOLV (new) = ptr;
5029 JDEP_WFL (new) = wfl;
5030 JDEP_CHAIN (new) = NULL;
5031 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5032 /* For some dependencies, set the enclosing class of the current
5033 class to be the enclosing context */
8ac1de05
APB
5034 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5035 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5036 && GET_ENCLOSING_CPC ())
5037 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5038 else
324ed8fd 5039 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5040 JDEP_GET_PATCH (new) = (tree *)NULL;
5041
5042 JDEP_INSERT (ctxp->classd_list, new);
5043
5044 return ptr;
5045}
5046
5047void
5048java_check_circular_reference ()
5049{
5050 tree current;
5051 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5052 {
5053 tree type = TREE_TYPE (current);
e920ebc9 5054 if (CLASS_INTERFACE (current))
e04a16fb
AG
5055 {
5056 /* Check all interfaces this class extends */
5057 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5058 int n, i;
5059
5060 if (!basetype_vec)
5061 return;
5062 n = TREE_VEC_LENGTH (basetype_vec);
5063 for (i = 0; i < n; i++)
5064 {
5065 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5066 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5067 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5068 parse_error_context (lookup_cl (current),
5069 "Cyclic interface inheritance");
5070 }
5071 }
5072 else
5073 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5074 parse_error_context (lookup_cl (current),
c2952b01
APB
5075 "Cyclic class inheritance%s",
5076 (cyclic_inheritance_report ?
5077 cyclic_inheritance_report : ""));
5078 }
5079}
5080
5081/* Augment the parameter list PARM with parameters crafted to
5082 initialize outer context locals aliases. Through ARTIFICIAL, a
5083 count is kept of the number of crafted parameters. MODE governs
5084 what eventually gets created: something suitable for a function
5085 creation or a function invocation, either the constructor or
c00f0fb2 5086 finit$. */
c2952b01
APB
5087
5088static tree
5089build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5090 int mode;
5091 tree class_type, parm;
5092 int *artificial;
5093{
5094 tree field;
da632f2c
APB
5095 tree additional_parms = NULL_TREE;
5096
c2952b01
APB
5097 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5098 if (FIELD_LOCAL_ALIAS (field))
5099 {
63ad61ed 5100 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5101 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5102 tree mangled_id;
c2952b01
APB
5103
5104 switch (mode)
5105 {
5106 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5107 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5108 &buffer [4]);
5109 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5110 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5111 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5112 else
5113 value = TREE_TYPE (field);
5114 break;
5115
5116 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5117 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5118 &buffer [4]);
c2952b01
APB
5119 value = TREE_TYPE (field);
5120 break;
5121
5122 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5123 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5124 &buffer [4]);
c2952b01
APB
5125 /* Now, this is wrong. purpose should always be the NAME
5126 of something and value its matching value (decl, type,
5127 etc...) FIXME -- but there is a lot to fix. */
5128
5129 /* When invoked for this kind of operation, we already
5130 know whether a field is used or not. */
5131 purpose = TREE_TYPE (field);
1f8f4a0b 5132 value = build_wfl_node (mangled_id);
c2952b01
APB
5133 break;
5134
5135 case AIPL_FUNCTION_CTOR_INVOCATION:
5136 /* There are two case: the constructor invokation happends
5137 outside the local inner, in which case, locales from the outer
5138 context are directly used.
5139
5140 Otherwise, we fold to using the alias directly. */
5141 if (class_type == current_class)
5142 value = field;
5143 else
5144 {
5145 name = get_identifier (&buffer[4]);
5146 value = IDENTIFIER_LOCAL_VALUE (name);
5147 }
5148 break;
5149 }
da632f2c 5150 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5151 if (artificial)
5152 *artificial +=1;
5153 }
da632f2c
APB
5154 if (additional_parms)
5155 {
5156 if (ANONYMOUS_CLASS_P (class_type)
5157 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5158 additional_parms = nreverse (additional_parms);
5159 parm = chainon (additional_parms, parm);
5160 }
5161
5162 return parm;
c2952b01
APB
5163}
5164
5165/* Craft a constructor for CLASS_DECL -- what we should do when none
5166 where found. ARGS is non NULL when a special signature must be
5167 enforced. This is the case for anonymous classes. */
5168
5169static void
5170craft_constructor (class_decl, args)
5171 tree class_decl, args;
5172{
5173 tree class_type = TREE_TYPE (class_decl);
5174 tree parm = NULL_TREE;
5175 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5176 ACC_PUBLIC : 0);
5177 int i = 0, artificial = 0;
5178 tree decl, ctor_name;
5179 char buffer [80];
5180
c2952b01
APB
5181 /* The constructor name is <init> unless we're dealing with an
5182 anonymous class, in which case the name will be fixed after having
5183 be expanded. */
5184 if (ANONYMOUS_CLASS_P (class_type))
5185 ctor_name = DECL_NAME (class_decl);
5186 else
5187 ctor_name = init_identifier_node;
5188
5189 /* If we're dealing with an inner class constructor, we hide the
5190 this$<n> decl in the name field of its parameter declaration. */
5191 if (PURE_INNER_CLASS_TYPE_P (class_type))
5192 {
5193 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5194 parm = tree_cons (build_current_thisn (class_type),
5195 build_pointer_type (type), parm);
5196
5197 /* Some more arguments to be hidden here. The values of the local
5198 variables of the outer context that the inner class needs to see. */
5199 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5200 class_type, parm,
5201 &artificial);
5202 }
5203
5204 /* Then if there are any args to be enforced, enforce them now */
5205 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5206 {
5207 sprintf (buffer, "parm%d", i++);
5208 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5209 }
c2952b01
APB
5210
5211 CRAFTED_PARAM_LIST_FIXUP (parm);
5212 decl = create_artificial_method (class_type, flags, void_type_node,
5213 ctor_name, parm);
5214 fix_method_argument_names (parm, decl);
5215 /* Now, mark the artificial parameters. */
5216 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5217 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5218}
5219
c2952b01 5220
e920ebc9
APB
5221/* Fix the constructors. This will be called right after circular
5222 references have been checked. It is necessary to fix constructors
5223 early even if no code generation will take place for that class:
5224 some generated constructor might be required by the class whose
5225 compilation triggered this one to be simply loaded. */
5226
5227void
5228java_fix_constructors ()
5229{
5230 tree current;
5231
5232 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5233 {
e920ebc9
APB
5234 tree class_type = TREE_TYPE (current);
5235 int saw_ctor = 0;
c2952b01
APB
5236 tree decl;
5237
5238 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5239 continue;
e920ebc9
APB
5240
5241 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5242 {
5243 if (DECL_CONSTRUCTOR_P (decl))
5244 {
5245 fix_constructors (decl);
5246 saw_ctor = 1;
5247 }
5248 }
5249
c2952b01
APB
5250 /* Anonymous class constructor can't be generated that early. */
5251 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5252 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5253 }
5254}
5255
23a79c61
APB
5256/* safe_layout_class just makes sure that we can load a class without
5257 disrupting the current_class, input_file, lineno, etc, information
5258 about the class processed currently. */
5259
e04a16fb
AG
5260void
5261safe_layout_class (class)
5262 tree class;
5263{
5264 tree save_current_class = current_class;
3b304f5b 5265 const char *save_input_filename = input_filename;
e04a16fb 5266 int save_lineno = lineno;
5e942c50 5267
e04a16fb 5268 layout_class (class);
5e942c50 5269
e04a16fb
AG
5270 current_class = save_current_class;
5271 input_filename = save_input_filename;
5272 lineno = save_lineno;
5273 CLASS_LOADED_P (class) = 1;
5274}
5275
5276static tree
5277jdep_resolve_class (dep)
5278 jdep *dep;
5279{
5280 tree decl;
5281
23a79c61
APB
5282 if (JDEP_RESOLVED_P (dep))
5283 decl = JDEP_RESOLVED_DECL (dep);
5284 else
e04a16fb 5285 {
c2952b01 5286 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5287 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5288 JDEP_RESOLVED (dep, decl);
5289 }
23a79c61 5290
e04a16fb 5291 if (!decl)
23a79c61 5292 complete_class_report_errors (dep);
1e12ab9b 5293 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5294 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5295 return decl;
5296}
5297
5298/* Complete unsatisfied class declaration and their dependencies */
5299
5300void
5301java_complete_class ()
5302{
e04a16fb
AG
5303 tree cclass;
5304 jdeplist *cclassd;
5305 int error_found;
b67d701b 5306 tree type;
e04a16fb 5307
9a7ab4b3 5308 /* Process imports */
e04a16fb 5309 process_imports ();
e04a16fb
AG
5310
5311 /* Rever things so we have the right order */
5312 ctxp->class_list = nreverse (ctxp->class_list);
5313 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5314
e04a16fb
AG
5315 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5316 cclass && cclassd;
5317 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5318 {
5319 jdep *dep;
5320 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5321 {
5322 tree decl;
e04a16fb
AG
5323 if (!(decl = jdep_resolve_class (dep)))
5324 continue;
5325
5326 /* Now it's time to patch */
5327 switch (JDEP_KIND (dep))
5328 {
5329 case JDEP_SUPER:
5330 /* Simply patch super */
5331 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5332 continue;
5333 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5334 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5335 break;
5336
5337 case JDEP_FIELD:
5338 {
5339 /* We do part of the job done in add_field */
5340 tree field_decl = JDEP_DECL (dep);
5341 tree field_type = TREE_TYPE (decl);
e04a16fb 5342 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5343 field_type = promote_type (field_type);
e04a16fb 5344 TREE_TYPE (field_decl) = field_type;
5e942c50 5345 DECL_ALIGN (field_decl) = 0;
11cf4d18 5346 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5347 layout_decl (field_decl, 0);
e04a16fb
AG
5348 SOURCE_FRONTEND_DEBUG
5349 (("Completed field/var decl `%s' with `%s'",
5350 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5351 IDENTIFIER_POINTER (DECL_NAME (decl))));
5352 break;
5353 }
5354 case JDEP_METHOD: /* We start patching a method */
5355 case JDEP_METHOD_RETURN:
5356 error_found = 0;
5357 while (1)
5358 {
5359 if (decl)
5360 {
b67d701b
PB
5361 type = TREE_TYPE(decl);
5362 if (TREE_CODE (type) == RECORD_TYPE)
5363 type = promote_type (type);
e04a16fb
AG
5364 JDEP_APPLY_PATCH (dep, type);
5365 SOURCE_FRONTEND_DEBUG
5366 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5367 "Completing fct `%s' with ret type `%s'":
5368 "Completing arg `%s' with type `%s'"),
5369 IDENTIFIER_POINTER (EXPR_WFL_NODE
5370 (JDEP_DECL_WFL (dep))),
5371 IDENTIFIER_POINTER (DECL_NAME (decl))));
5372 }
5373 else
5374 error_found = 1;
5375 dep = JDEP_CHAIN (dep);
5376 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5377 break;
5378 else
5379 decl = jdep_resolve_class (dep);
5380 }
5381 if (!error_found)
5382 {
5383 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5384 /* Recompute and reset the signature, check first that
5385 all types are now defined. If they're not,
5386 dont build the signature. */
5387 if (check_method_types_complete (mdecl))
5388 {
5389 signature = build_java_signature (TREE_TYPE (mdecl));
5390 set_java_signature (TREE_TYPE (mdecl), signature);
5391 }
e04a16fb
AG
5392 }
5393 else
5394 continue;
5395 break;
5396
5397 case JDEP_INTERFACE:
5398 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5399 JDEP_WFL (dep)))
5400 continue;
5401 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5402 break;
5403
b67d701b 5404 case JDEP_PARM:
e04a16fb 5405 case JDEP_VARIABLE:
b67d701b
PB
5406 type = TREE_TYPE(decl);
5407 if (TREE_CODE (type) == RECORD_TYPE)
5408 type = promote_type (type);
5409 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5410 break;
5411
5412 case JDEP_TYPE:
5413 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5414 SOURCE_FRONTEND_DEBUG
5415 (("Completing a random type dependency on a '%s' node",
5416 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5417 break;
5418
b9f7e36c 5419 case JDEP_EXCEPTION:
c877974e
APB
5420 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5421 SOURCE_FRONTEND_DEBUG
5422 (("Completing `%s' `throws' argument node",
5423 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5424 break;
5425
c2952b01
APB
5426 case JDEP_ANONYMOUS:
5427 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5428 break;
5429
e04a16fb 5430 default:
0a2138e2
APB
5431 fatal ("Can't handle patch code %d - java_complete_class",
5432 JDEP_KIND (dep));
e04a16fb
AG
5433 }
5434 }
5435 }
e04a16fb
AG
5436 return;
5437}
5438
5439/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5440 array. */
5441
5442static tree
c2952b01
APB
5443resolve_class (enclosing, class_type, decl, cl)
5444 tree enclosing, class_type, decl, cl;
e04a16fb 5445{
49f48c71
KG
5446 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5447 const char *base = name;
78d21f92
PB
5448 tree resolved_type = TREE_TYPE (class_type);
5449 tree resolved_type_decl;
e04a16fb 5450
78d21f92
PB
5451 if (resolved_type != NULL_TREE)
5452 {
5453 tree resolved_type_decl = TYPE_NAME (resolved_type);
5454 if (resolved_type_decl == NULL_TREE
5455 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5456 {
5457 resolved_type_decl = build_decl (TYPE_DECL,
5458 TYPE_NAME (class_type),
5459 resolved_type);
5460 }
5461 return resolved_type_decl;
5462 }
5463
e04a16fb
AG
5464 /* 1- Check to see if we have an array. If true, find what we really
5465 want to resolve */
5466 while (name[0] == '[')
5467 name++;
5468 if (base != name)
34d4df06
APB
5469 {
5470 TYPE_NAME (class_type) = get_identifier (name);
5471 WFL_STRIP_BRACKET (cl, cl);
5472 }
e04a16fb
AG
5473
5474 /* 2- Resolve the bare type */
c2952b01
APB
5475 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5476 decl, cl)))
e04a16fb
AG
5477 return NULL_TREE;
5478 resolved_type = TREE_TYPE (resolved_type_decl);
5479
5480 /* 3- If we have and array, reconstruct the array down to its nesting */
5481 if (base != name)
5482 {
5483 while (base != name)
5484 {
5485 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5486 resolved_type = promote_type (resolved_type);
5487 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5488 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5489 name--;
5490 }
5491 /* Build a fake decl for this, since this is what is expected to
5492 be returned. */
5493 resolved_type_decl =
5494 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5495 /* Figure how those two things are important for error report. FIXME */
5496 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5497 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5498 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5499 }
78d21f92 5500 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5501 return resolved_type_decl;
5502}
5503
5504/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5505 are used to report error messages. */
5506
78d21f92 5507tree
c2952b01
APB
5508do_resolve_class (enclosing, class_type, decl, cl)
5509 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5510{
5511 tree new_class_decl;
e04a16fb
AG
5512
5513 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5514 it is changed by find_in_imports{_on_demand} and (but it doesn't
5515 really matter) qualify_and_find */
e04a16fb 5516
c2952b01
APB
5517 /* 0- Search in the current class as an inner class */
5518
5519 /* Maybe some code here should be added to load the class or
5520 something, at least if the class isn't an inner class and ended
5521 being loaded from class file. FIXME. */
a40d21da
APB
5522 while (enclosing)
5523 {
5524 tree name;
e1d565ab 5525 tree intermediate;
a40d21da
APB
5526
5527 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5528 return new_class_decl;
8ac1de05 5529
e1d565ab 5530 intermediate = enclosing;
0c2b8145 5531 /* Explore enclosing contexts. */
e1d565ab 5532 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5533 {
e1d565ab
BM
5534 intermediate = DECL_CONTEXT (intermediate);
5535 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5536 class_type, cl)))
5537 return new_class_decl;
5538 }
5539
a40d21da
APB
5540 /* Now go to the upper classes, bail out if necessary. */
5541 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5542 if (!enclosing || enclosing == object_type_node)
5543 break;
5544
5545 if (TREE_CODE (enclosing) == RECORD_TYPE)
5546 {
5547 enclosing = TYPE_NAME (enclosing);
5548 continue;
5549 }
5550
5551 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5552 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5553 else
5554 name = enclosing;
5555 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5556 }
c2952b01 5557
9a7ab4b3
APB
5558 /* 1- Check for the type in single imports. This will change
5559 TYPE_NAME() if something relevant is found */
5560 find_in_imports (class_type);
e04a16fb 5561
9a7ab4b3 5562 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5563 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5564 {
5565 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5566 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5567 load_class (TYPE_NAME (class_type), 0);
5568 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5569 }
5570
9a7ab4b3
APB
5571 /* 3- Search according to the current package definition */
5572 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5573 {
5574 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5575 TYPE_NAME (class_type))))
5576 return new_class_decl;
5577 }
5578
5579 /* 4- Check the import on demands. Don't allow bar.baz to be
5580 imported from foo.* */
5581 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5582 if (find_in_imports_on_demand (class_type))
5583 return NULL_TREE;
5584
5585 /* If found in find_in_imports_on_demant, the type has already been
5586 loaded. */
5587 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5588 return new_class_decl;
5589
5590 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5591 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5592 {
ee07f4f4 5593 tree package;
d6baf6f5
APB
5594
5595 /* If there is a current package (ctxp->package), it's the first
5596 element of package_list and we can skip it. */
5597 for (package = (ctxp->package ?
5598 TREE_CHAIN (package_list) : package_list);
5599 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5600 if ((new_class_decl = qualify_and_find (class_type,
5601 TREE_PURPOSE (package),
5602 TYPE_NAME (class_type))))
5603 return new_class_decl;
5604 }
5605
5606 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5607 load_class (TYPE_NAME (class_type), 0);
5608 if (check_pkg_class_access (TYPE_NAME (class_type),
5609 (cl ? cl : lookup_cl (decl))))
5610 return NULL_TREE;
5611
9a7ab4b3 5612 /* 6- Last call for a resolution */
e04a16fb
AG
5613 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5614}
5615
9a7ab4b3
APB
5616static tree
5617qualify_and_find (class_type, package, name)
5618 tree class_type, package, name;
5619{
5620 tree new_qualified = merge_qualified_name (package, name);
5621 tree new_class_decl;
5622
5623 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5624 load_class (new_qualified, 0);
5625 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5626 {
5627 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5628 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5629 load_class (new_qualified, 0);
5630 TYPE_NAME (class_type) = new_qualified;
5631 return IDENTIFIER_CLASS_VALUE (new_qualified);
5632 }
5633 return NULL_TREE;
5634}
5635
e04a16fb 5636/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5637 parsed class). Return a decl node. This function is meant to be
5638 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5639
5640static tree
c877974e
APB
5641resolve_and_layout (something, cl)
5642 tree something;
e04a16fb
AG
5643 tree cl;
5644{
34d4df06 5645 tree decl, decl_type;
c877974e 5646
23a79c61
APB
5647 /* Don't do that on the current class */
5648 if (something == current_class)
5649 return TYPE_NAME (current_class);
c877974e 5650
23a79c61 5651 /* Don't do anything for void and other primitive types */
c877974e
APB
5652 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5653 return NULL_TREE;
5654
23a79c61
APB
5655 /* Pointer types can be reall pointer types or fake pointers. When
5656 finding a real pointer, recheck for primitive types */
5657 if (TREE_CODE (something) == POINTER_TYPE)
5658 {
5659 if (TREE_TYPE (something))
5660 {
5661 something = TREE_TYPE (something);
5662 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5663 return NULL_TREE;
5664 }
5665 else
5666 something = TYPE_NAME (something);
5667 }
5668
5669 /* Don't do anything for arrays of primitive types */
5670 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5671 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5672 return NULL_TREE;
5673
c2952b01
APB
5674 /* Something might be a WFL */
5675 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5676 something = EXPR_WFL_NODE (something);
5677
5678 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5679 TYPE_DECL or a real TYPE */
5680 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5681 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5682 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5683
23a79c61
APB
5684 if (!(decl = resolve_no_layout (something, cl)))
5685 return NULL_TREE;
5686
5687 /* Resolve and layout if necessary */
34d4df06
APB
5688 decl_type = TREE_TYPE (decl);
5689 layout_class_methods (decl_type);
5690 /* Check methods */
5691 if (CLASS_FROM_SOURCE_P (decl_type))
5692 java_check_methods (decl);
5693 /* Layout the type if necessary */
5694 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5695 safe_layout_class (decl_type);
23a79c61 5696
e04a16fb
AG
5697 return decl;
5698}
5699
5700/* Resolve a class, returns its decl but doesn't perform any
5701 layout. The current parsing context is saved and restored */
5702
5703static tree
5704resolve_no_layout (name, cl)
5705 tree name, cl;
5706{
5707 tree ptr, decl;
5708 BUILD_PTR_FROM_NAME (ptr, name);
5709 java_parser_context_save_global ();
c2952b01 5710 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5711 java_parser_context_restore_global ();
5712
5713 return decl;
5714}
5715
23a79c61
APB
5716/* Called when reporting errors. Skip leader '[' in a complex array
5717 type description that failed to be resolved. */
e04a16fb 5718
49f48c71 5719static const char *
e04a16fb 5720purify_type_name (name)
49f48c71 5721 const char *name;
e04a16fb
AG
5722{
5723 while (*name && *name == '[')
5724 name++;
5725 return name;
5726}
5727
5728/* The type CURRENT refers to can't be found. We print error messages. */
5729
5730static void
5731complete_class_report_errors (dep)
5732 jdep *dep;
5733{
49f48c71 5734 const char *name;
23a79c61
APB
5735
5736 if (!JDEP_WFL (dep))
5737 return;
5738
5739 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5740 switch (JDEP_KIND (dep))
5741 {
5742 case JDEP_SUPER:
5743 parse_error_context
5744 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5745 purify_type_name (name),
e04a16fb
AG
5746 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5747 break;
5748 case JDEP_FIELD:
5749 parse_error_context
5750 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5751 purify_type_name (name),
e04a16fb
AG
5752 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5753 break;
5754 case JDEP_METHOD: /* Covers arguments */
5755 parse_error_context
781b0558 5756 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5757 purify_type_name (name),
e04a16fb
AG
5758 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5759 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5760 break;
5761 case JDEP_METHOD_RETURN: /* Covers return type */
5762 parse_error_context
781b0558 5763 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5764 purify_type_name (name),
e04a16fb
AG
5765 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5766 break;
5767 case JDEP_INTERFACE:
5768 parse_error_context
5769 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5770 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5771 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5772 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5773 break;
5774 case JDEP_VARIABLE:
5775 parse_error_context
781b0558 5776 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5777 purify_type_name (IDENTIFIER_POINTER
5778 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5779 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5780 break;
b9f7e36c
APB
5781 case JDEP_EXCEPTION: /* As specified by `throws' */
5782 parse_error_context
5783 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5784 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5785 break;
0a2138e2
APB
5786 default:
5787 /* Fix for -Wall. Just break doing nothing. The error will be
5788 caught later */
5789 break;
e04a16fb
AG
5790 }
5791}
5792
22eed1e6
APB
5793/* Return a static string containing the DECL prototype string. If
5794 DECL is a constructor, use the class name instead of the form
5795 <init> */
5796
49f48c71 5797static const char *
22eed1e6
APB
5798get_printable_method_name (decl)
5799 tree decl;
5800{
49f48c71 5801 const char *to_return;
9ee9b555 5802 tree name = NULL_TREE;
22eed1e6
APB
5803
5804 if (DECL_CONSTRUCTOR_P (decl))
5805 {
5806 name = DECL_NAME (decl);
5e942c50 5807 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5808 }
5809
5810 to_return = lang_printable_name (decl, 0);
5811 if (DECL_CONSTRUCTOR_P (decl))
5812 DECL_NAME (decl) = name;
5813
5814 return to_return;
5815}
5816
5e942c50
APB
5817/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5818 nevertheless needs to be verfied, 1 otherwise. */
5819
5820static int
5821reset_method_name (method)
5822 tree method;
5823{
c2952b01 5824 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5825 {
5826 /* NAME is just the plain name when Object is being defined */
5827 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5828 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5829 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5830 return 0;
5831 }
5832 else
5833 return 1;
5834}
5835
c877974e
APB
5836/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5837
5838tree
5839java_get_real_method_name (method_decl)
5840 tree method_decl;
5841{
5842 tree method_name = DECL_NAME (method_decl);
5843 if (DECL_CONSTRUCTOR_P (method_decl))
5844 return init_identifier_node;
82371d41
APB
5845
5846 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5847 and still can be a constructor. FIXME */
5848
23a79c61
APB
5849 /* Don't confuse method only bearing the name of their class as
5850 constructors */
82371d41
APB
5851 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5852 && ctxp
c2952b01 5853 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5854 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5855 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5856 return init_identifier_node;
5857 else
5858 return EXPR_WFL_NODE (method_name);
5859}
5860
22eed1e6
APB
5861/* Track method being redefined inside the same class. As a side
5862 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5863 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5864
e04a16fb
AG
5865static int
5866check_method_redefinition (class, method)
5867 tree class, method;
5868{
5869 tree redef, name;
5870 tree cl = DECL_NAME (method);
c3f2a476 5871 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5872 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5873 fixed and checked */
5e942c50
APB
5874
5875 /* Reset the method name before running the check. If it returns 1,
5876 the method doesn't need to be verified with respect to method
5877 redeclaration and we return 0 */
5878 if (reset_method_name (method))
e04a16fb 5879 return 0;
5e942c50
APB
5880
5881 name = DECL_NAME (method);
e04a16fb
AG
5882 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5883 {
c3f2a476 5884 if (redef == method)
e04a16fb 5885 break;
c3f2a476
APB
5886 if (DECL_NAME (redef) == name
5887 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5888 {
22eed1e6
APB
5889 parse_error_context
5890 (cl, "Duplicate %s declaration `%s'",
5891 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5892 get_printable_method_name (redef));
e04a16fb
AG
5893 return 1;
5894 }
5895 }
5896 return 0;
5897}
5898
1175b9b4
TT
5899/* Return 1 if check went ok, 0 otherwise. */
5900static int
d77613be
APB
5901check_abstract_method_definitions (do_interface, class_decl, type)
5902 int do_interface;
5903 tree class_decl, type;
5904{
5905 tree class = TREE_TYPE (class_decl);
5906 tree method, end_type;
1175b9b4 5907 int ok = 1;
d77613be
APB
5908
5909 end_type = (do_interface ? object_type_node : type);
5910 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5911 {
5912 tree other_super, other_method, method_sig, method_name;
5913 int found = 0;
165f37bc 5914 int end_type_reached = 0;
d77613be
APB
5915
5916 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5917 continue;
5918
5919 /* Now verify that somewhere in between TYPE and CLASS,
5920 abstract method METHOD gets a non abstract definition
5921 that is inherited by CLASS. */
5922
5923 method_sig = build_java_signature (TREE_TYPE (method));
5924 method_name = DECL_NAME (method);
5925 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5926 method_name = EXPR_WFL_NODE (method_name);
5927
165f37bc
APB
5928 other_super = class;
5929 do {
5930 if (other_super == end_type)
5931 end_type_reached = 1;
5932
5933 /* Method search */
5934 for (other_method = TYPE_METHODS (other_super); other_method;
5935 other_method = TREE_CHAIN (other_method))
5936 {
5937 tree s = build_java_signature (TREE_TYPE (other_method));
5938 tree other_name = DECL_NAME (other_method);
5939
5940 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5941 other_name = EXPR_WFL_NODE (other_name);
5942 if (!DECL_CLINIT_P (other_method)
5943 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5944 && method_name == other_name
5945 && method_sig == s
5946 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5947 {
5948 found = 1;
5949 break;
5950 }
5951 }
5952 other_super = CLASSTYPE_SUPER (other_super);
5953 } while (!end_type_reached);
5954
d77613be
APB
5955 /* Report that abstract METHOD didn't find an implementation
5956 that CLASS can use. */
5957 if (!found)
5958 {
c2e3db92 5959 char *t = xstrdup (lang_printable_name
d77613be
APB
5960 (TREE_TYPE (TREE_TYPE (method)), 0));
5961 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5962 tree saved_wfl = NULL_TREE;
5963
5964 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5965 {
5966 saved_wfl = DECL_NAME (method);
5967 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5968 }
5969
5970 parse_error_context
5971 (lookup_cl (class_decl),
781b0558 5972 "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
5973 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5974 t, lang_printable_name (method, 0),
5975 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5976 "interface" : "class"),
5977 IDENTIFIER_POINTER (ccn),
5978 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5979 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5980 ok = 0;
d77613be 5981 free (t);
1175b9b4 5982
d77613be
APB
5983 if (saved_wfl)
5984 DECL_NAME (method) = saved_wfl;
5985 }
5986 }
1175b9b4
TT
5987
5988 if (ok && do_interface)
5989 {
5990 /* Check for implemented interfaces. */
5991 int i;
5992 tree vector = TYPE_BINFO_BASETYPES (type);
5993 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5994 {
5995 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5996 ok = check_abstract_method_definitions (1, class_decl, super);
5997 }
5998 }
5999
6000 return ok;
d77613be
APB
6001}
6002
614eaae0 6003/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
6004 methods. */
6005
6006static void
6007java_check_abstract_method_definitions (class_decl)
6008 tree class_decl;
6009{
6010 tree class = TREE_TYPE (class_decl);
6011 tree super, vector;
6012 int i;
6013
6014 if (CLASS_ABSTRACT (class_decl))
6015 return;
6016
6017 /* Check for inherited types */
165f37bc
APB
6018 super = class;
6019 do {
6020 super = CLASSTYPE_SUPER (super);
6021 check_abstract_method_definitions (0, class_decl, super);
6022 } while (super != object_type_node);
d77613be
APB
6023
6024 /* Check for implemented interfaces. */
6025 vector = TYPE_BINFO_BASETYPES (class);
6026 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6027 {
6028 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6029 check_abstract_method_definitions (1, class_decl, super);
6030 }
6031}
6032
165f37bc
APB
6033/* Check all the types method DECL uses and return 1 if all of them
6034 are now complete, 0 otherwise. This is used to check whether its
6035 safe to build a method signature or not. */
6036
6037static int
6038check_method_types_complete (decl)
6039 tree decl;
6040{
6041 tree type = TREE_TYPE (decl);
6042 tree args;
6043
6044 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6045 return 0;
6046
6047 args = TYPE_ARG_TYPES (type);
6048 if (TREE_CODE (type) == METHOD_TYPE)
6049 args = TREE_CHAIN (args);
6050 for (; args != end_params_node; args = TREE_CHAIN (args))
6051 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6052 return 0;
6053
6054 return 1;
6055}
6056
34d4df06
APB
6057/* Visible interface to check methods contained in CLASS_DECL */
6058
6059void
6060java_check_methods (class_decl)
6061 tree class_decl;
6062{
6063 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6064 return;
6065
6066 if (CLASS_INTERFACE (class_decl))
6067 java_check_abstract_methods (class_decl);
6068 else
6069 java_check_regular_methods (class_decl);
6070
6071 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6072}
6073
d77613be
APB
6074/* Check all the methods of CLASS_DECL. Methods are first completed
6075 then checked according to regular method existance rules. If no
6076 constructor for CLASS_DECL were encountered, then build its
6077 declaration. */
e04a16fb
AG
6078
6079static void
6080java_check_regular_methods (class_decl)
6081 tree class_decl;
6082{
c2952b01 6083 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6084 tree method;
6085 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6086 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6087 tree mthrows;
6088
6089 /* It is not necessary to check methods defined in java.lang.Object */
6090 if (class == object_type_node)
6091 return;
e04a16fb 6092
23a79c61
APB
6093 if (!TYPE_NVIRTUALS (class))
6094 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6095
6096 /* Should take interfaces into account. FIXME */
6097 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6098 {
5e942c50 6099 tree sig;
e04a16fb
AG
6100 tree method_wfl = DECL_NAME (method);
6101 int aflags;
6102
5e942c50
APB
6103 /* If we previously found something and its name was saved,
6104 reinstall it now */
6105 if (found && saved_found_wfl)
ba179f9f
APB
6106 {
6107 DECL_NAME (found) = saved_found_wfl;
6108 saved_found_wfl = NULL_TREE;
6109 }
5e942c50 6110
e04a16fb
AG
6111 /* Check for redefinitions */
6112 if (check_method_redefinition (class, method))
6113 continue;
6114
22eed1e6
APB
6115 /* If we see one constructor a mark so we don't generate the
6116 default one. Also skip other verifications: constructors
6117 can't be inherited hence hiden or overriden */
6118 if (DECL_CONSTRUCTOR_P (method))
6119 {
6120 saw_constructor = 1;
6121 continue;
6122 }
6123
c877974e
APB
6124 /* We verify things thrown by the method. They must inherits from
6125 java.lang.Throwable */
6126 for (mthrows = DECL_FUNCTION_THROWS (method);
6127 mthrows; mthrows = TREE_CHAIN (mthrows))
6128 {
6129 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6130 parse_error_context
781b0558 6131 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6132 IDENTIFIER_POINTER
6133 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6134 }
6135
e04a16fb 6136 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6137 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6138
c2952b01
APB
6139 /* Inner class can't declare static methods */
6140 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6141 {
6142 char *t = xstrdup (lang_printable_name (class, 0));
6143 parse_error_context
6144 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6145 lang_printable_name (method, 0), t);
6146 free (t);
6147 }
6148
5e942c50 6149 /* Nothing overrides or it's a private method. */
aabd7048 6150 if (!found)
5e942c50 6151 continue;
aabd7048
PB
6152 if (METHOD_PRIVATE (found))
6153 {
6154 found = NULL_TREE;
6155 continue;
6156 }
5e942c50
APB
6157
6158 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6159 We set it temporarily for the sake of the error report. */
6160 saved_found_wfl = DECL_NAME (found);
6161 reset_method_name (found);
6162
614eaae0
APB
6163 /* If `found' is declared in an interface, make sure the
6164 modifier matches. */
6165 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6166 && clinit_identifier_node != DECL_NAME (found)
6167 && !METHOD_PUBLIC (method))
6168 {
6169 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6170 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6171 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6172 lang_printable_name (method, 0),
6173 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6174 }
6175
e04a16fb
AG
6176 /* Can't override a method with the same name and different return
6177 types. */
6178 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6179 {
614eaae0
APB
6180 char *t = xstrdup
6181 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6182 parse_error_context
7f10c2e2 6183 (method_wfl,
b9f7e36c 6184 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6185 lang_printable_name (found, 0), t,
b9f7e36c
APB
6186 IDENTIFIER_POINTER
6187 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6188 free (t);
6189 }
e04a16fb 6190
7f10c2e2
APB
6191 aflags = get_access_flags_from_decl (found);
6192 /* If the method has default, access in an other package, then
6193 issue a warning that the current method doesn't override the
6194 one that was found elsewhere. Do not issue this warning when
6195 the match was found in java.lang.Object. */
6196 if (DECL_CONTEXT (found) != object_type_node
a003f638 6197 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6198 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6199 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6200 && flag_not_overriding)
6201 {
6202 parse_warning_context
781b0558 6203 (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
6204 lang_printable_name (found, 0),
6205 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6206 IDENTIFIER_POINTER (DECL_NAME
6207 (TYPE_NAME (DECL_CONTEXT (found)))));
6208 continue;
6209 }
6210
e04a16fb
AG
6211 /* Can't override final. Can't override static. */
6212 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6213 {
6214 /* Static *can* override static */
6215 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6216 continue;
6217 parse_error_context
6218 (method_wfl,
6219 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6220 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6221 lang_printable_name (found, 0),
e04a16fb
AG
6222 (METHOD_FINAL (found) ? "final" : "static"),
6223 IDENTIFIER_POINTER
6224 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6225 continue;
6226 }
7f10c2e2 6227
e04a16fb
AG
6228 /* Static method can't override instance method. */
6229 if (METHOD_STATIC (method))
6230 {
6231 parse_error_context
6232 (method_wfl,
781b0558 6233 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6234 lang_printable_name (found, 0),
e04a16fb
AG
6235 IDENTIFIER_POINTER
6236 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6237 continue;
6238 }
5e942c50 6239
5e942c50
APB
6240 /* - Overriding/hiding public must be public
6241 - Overriding/hiding protected must be protected or public
6242 - If the overriden or hidden method has default (package)
6243 access, then the overriding or hiding method must not be
614eaae0
APB
6244 private; otherwise, a compile-time error occurs. If
6245 `found' belongs to an interface, things have been already
6246 taken care of. */
6247 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6248 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6249 || (METHOD_PROTECTED (found)
6250 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6251 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6252 && METHOD_PRIVATE (method))))
e04a16fb
AG
6253 {
6254 parse_error_context
6255 (method_wfl,
781b0558 6256 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6257 (METHOD_PUBLIC (method) ? "public" :
6258 (METHOD_PRIVATE (method) ? "private" : "protected")),
6259 IDENTIFIER_POINTER (DECL_NAME
6260 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6261 continue;
6262 }
6263
b9f7e36c
APB
6264 /* Overriding methods must have compatible `throws' clauses on checked
6265 exceptions, if any */
6266 check_throws_clauses (method, method_wfl, found);
6267
e04a16fb
AG
6268 /* Inheriting multiple methods with the same signature. FIXME */
6269 }
6270
5e942c50
APB
6271 /* Don't forget eventual pending found and saved_found_wfl. Take
6272 into account that we might have exited because we saw an
d77613be 6273 artificial method as the last entry. */
5e942c50
APB
6274
6275 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6276 DECL_NAME (found) = saved_found_wfl;
6277
23a79c61
APB
6278 if (!TYPE_NVIRTUALS (class))
6279 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6280
d77613be
APB
6281 /* Search for inherited abstract method not yet implemented in this
6282 class. */
6283 java_check_abstract_method_definitions (class_decl);
6284
22eed1e6 6285 if (!saw_constructor)
e920ebc9 6286 fatal ("No constructor found");
e04a16fb
AG
6287}
6288
b9f7e36c
APB
6289/* Return a non zero value if the `throws' clause of METHOD (if any)
6290 is incompatible with the `throws' clause of FOUND (if any). */
6291
6292static void
6293check_throws_clauses (method, method_wfl, found)
6294 tree method, method_wfl, found;
6295{
6296 tree mthrows, fthrows;
6297
c877974e
APB
6298 /* Can't check these things with class loaded from bytecode. FIXME */
6299 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6300 return;
6301
b9f7e36c
APB
6302 for (mthrows = DECL_FUNCTION_THROWS (method);
6303 mthrows; mthrows = TREE_CHAIN (mthrows))
6304 {
6305 /* We don't verify unchecked expressions */
c877974e 6306 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6307 continue;
6308 /* Checked expression must be compatible */
6309 for (fthrows = DECL_FUNCTION_THROWS (found);
6310 fthrows; fthrows = TREE_CHAIN (fthrows))
6311 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6312 break;
6313 if (!fthrows)
6314 {
6315 parse_error_context
781b0558 6316 (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 6317 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6318 lang_printable_name (found, 0),
b9f7e36c
APB
6319 IDENTIFIER_POINTER
6320 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6321 }
6322 }
6323}
6324
e04a16fb
AG
6325/* Check abstract method of interface INTERFACE */
6326
6327static void
5e942c50
APB
6328java_check_abstract_methods (interface_decl)
6329 tree interface_decl;
e04a16fb
AG
6330{
6331 int i, n;
6332 tree method, basetype_vec, found;
5e942c50 6333 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6334
6335 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6336 {
b9f7e36c 6337 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6338
6339 /* 2- Check for double definition inside the defining interface */
6340 if (check_method_redefinition (interface, method))
6341 continue;
6342
6343 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6344 the thrown exceptions (FIXME) */
e04a16fb
AG
6345 found = lookup_java_interface_method2 (interface, method);
6346 if (found)
6347 {
5e942c50
APB
6348 char *t;
6349 tree saved_found_wfl = DECL_NAME (found);
6350 reset_method_name (found);
c2e3db92 6351 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6352 parse_error_context
b9f7e36c 6353 (method_wfl,
5e942c50 6354 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6355 lang_printable_name (found, 0), t,
b9f7e36c
APB
6356 IDENTIFIER_POINTER
6357 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6358 free (t);
5e942c50 6359 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6360 continue;
e04a16fb
AG
6361 }
6362 }
6363
6364 /* 4- Inherited methods can't differ by their returned types */
6365 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6366 return;
6367 n = TREE_VEC_LENGTH (basetype_vec);
6368 for (i = 0; i < n; i++)
6369 {
6370 tree sub_interface_method, sub_interface;
6371 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6372 if (!vec_elt)
6373 continue;
6374 sub_interface = BINFO_TYPE (vec_elt);
6375 for (sub_interface_method = TYPE_METHODS (sub_interface);
6376 sub_interface_method;
6377 sub_interface_method = TREE_CHAIN (sub_interface_method))
6378 {
6379 found = lookup_java_interface_method2 (interface,
6380 sub_interface_method);
6381 if (found && (found != sub_interface_method))
5e942c50
APB
6382 {
6383 tree saved_found_wfl = DECL_NAME (found);
6384 reset_method_name (found);
6385 parse_error_context
6386 (lookup_cl (sub_interface_method),
781b0558 6387 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6388 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6389 lang_printable_name (found, 0),
6390 IDENTIFIER_POINTER
6391 (DECL_NAME (TYPE_NAME
6392 (DECL_CONTEXT (sub_interface_method)))),
6393 IDENTIFIER_POINTER
6394 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6395 DECL_NAME (found) = saved_found_wfl;
6396 }
e04a16fb
AG
6397 }
6398 }
6399}
6400
e04a16fb
AG
6401/* Lookup methods in interfaces using their name and partial
6402 signature. Return a matching method only if their types differ. */
6403
6404static tree
6405lookup_java_interface_method2 (class, method_decl)
6406 tree class, method_decl;
6407{
6408 int i, n;
6409 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6410
6411 if (!basetype_vec)
6412 return NULL_TREE;
6413
6414 n = TREE_VEC_LENGTH (basetype_vec);
6415 for (i = 0; i < n; i++)
6416 {
6417 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6418 if ((BINFO_TYPE (vec_elt) != object_type_node)
6419 && (to_return =
6420 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6421 return to_return;
6422 }
6423 for (i = 0; i < n; i++)
6424 {
6425 to_return = lookup_java_interface_method2
6426 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6427 if (to_return)
6428 return to_return;
6429 }
6430
6431 return NULL_TREE;
6432}
6433
6434/* Lookup method using their name and partial signature. Return a
6435 matching method only if their types differ. */
6436
6437static tree
6438lookup_java_method2 (clas, method_decl, do_interface)
6439 tree clas, method_decl;
6440 int do_interface;
6441{
5e942c50
APB
6442 tree method, method_signature, method_name, method_type, name;
6443
e04a16fb 6444 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6445 name = DECL_NAME (method_decl);
6446 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6447 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6448 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6449
6450 while (clas != NULL_TREE)
6451 {
6452 for (method = TYPE_METHODS (clas);
6453 method != NULL_TREE; method = TREE_CHAIN (method))
6454 {
6455 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6456 tree name = DECL_NAME (method);
6457 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6458 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6459 && method_sig == method_signature
6460 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6461 return method;
e04a16fb
AG
6462 }
6463 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6464 }
6465 return NULL_TREE;
6466}
6467
f441f671
APB
6468/* Return the line that matches DECL line number, and try its best to
6469 position the column number. Used during error reports. */
e04a16fb
AG
6470
6471static tree
6472lookup_cl (decl)
6473 tree decl;
6474{
6475 static tree cl = NULL_TREE;
f441f671 6476 char *line, *found;
e04a16fb
AG
6477
6478 if (!decl)
6479 return NULL_TREE;
6480
6481 if (cl == NULL_TREE)
19e223db
MM
6482 {
6483 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6484 ggc_add_tree_root (&cl, 1);
6485 }
e04a16fb
AG
6486
6487 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6488 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6489
dba41d30 6490 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6491 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6492
6493 found = strstr ((const char *)line,
6494 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6495 if (found)
6496 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6497
e04a16fb
AG
6498 return cl;
6499}
6500
6501/* Look for a simple name in the single-type import list */
6502
6503static tree
6504find_name_in_single_imports (name)
6505 tree name;
6506{
6507 tree node;
6508
6509 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6510 if (TREE_VALUE (node) == name)
6511 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6512
6513 return NULL_TREE;
6514}
6515
6516/* Process all single-type import. */
6517
6518static int
6519process_imports ()
6520{
6521 tree import;
6522 int error_found;
6523
6524 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6525 {
6526 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6527
6528 /* Don't load twice something already defined. */
6529 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6530 continue;
02ae6e2e
APB
6531
6532 while (1)
6533 {
6534 tree left;
6535
6536 QUALIFIED_P (to_be_found) = 1;
6537 load_class (to_be_found, 0);
6538 error_found =
6539 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6540
6541 /* We found it, we can bail out */
6542 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6543 break;
6544
6545 /* We haven't found it. Maybe we're trying to access an
6546 inner class. The only way for us to know is to try again
6547 after having dropped a qualifier. If we can't break it further,
6548 we have an error. */
6549 if (breakdown_qualified (&left, NULL, to_be_found))
6550 break;
6551
6552 to_be_found = left;
6553 }
e04a16fb
AG
6554 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6555 {
6556 parse_error_context (TREE_PURPOSE (import),
6557 "Class or interface `%s' not found in import",
6558 IDENTIFIER_POINTER (to_be_found));
6559 return 1;
6560 }
6561 if (error_found)
6562 return 1;
6563 }
6564 return 0;
6565}
6566
9a7ab4b3
APB
6567/* Possibly find and mark a class imported by a single-type import
6568 statement. */
e04a16fb 6569
9a7ab4b3 6570static void
e04a16fb
AG
6571find_in_imports (class_type)
6572 tree class_type;
6573{
6574 tree import;
6575
6576 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6577 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6578 {
6579 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6580 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6581 }
e04a16fb
AG
6582}
6583
e04a16fb 6584static int
63a212ed 6585note_possible_classname (name, len)
49f48c71 6586 const char *name;
63a212ed 6587 int len;
e04a16fb 6588{
63a212ed
PB
6589 tree node;
6590 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6591 len = len - 5;
6592 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6593 len = len - 6;
e04a16fb 6594 else
63a212ed
PB
6595 return 0;
6596 node = ident_subst (name, len, "", '/', '.', "");
6597 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6598 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6599 return 1;
e04a16fb
AG
6600}
6601
6602/* Read a import directory, gathering potential match for further type
6603 references. Indifferently reads a filesystem or a ZIP archive
6604 directory. */
6605
6606static void
6607read_import_dir (wfl)
6608 tree wfl;
6609{
63a212ed 6610 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6611 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6612 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6613 DIR *dirp = NULL;
d8fccff5 6614 JCF *saved_jcf = current_jcf;
e04a16fb 6615
63a212ed
PB
6616 int found = 0;
6617 int k;
6618 void *entry;
6619 struct buffer filename[1];
6620
6621
6622 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6623 return;
6624 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6625
6626 BUFFER_INIT (filename);
6627 buffer_grow (filename, package_length + 100);
6628
6629 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6630 {
49f48c71 6631 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6632 int entry_length = strlen (entry_name);
6633 if (jcf_path_is_zipfile (entry))
6634 {
6635 ZipFile *zipf;
6636 buffer_grow (filename, entry_length);
6637 memcpy (filename->data, entry_name, entry_length - 1);
6638 filename->data[entry_length-1] = '\0';
6639 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6640 if (zipf == NULL)
6641 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6642 else
6643 {
6644 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6645 BUFFER_RESET (filename);
6646 for (k = 0; k < package_length; k++)
6647 {
6648 char ch = package_name[k];
6649 *filename->ptr++ = ch == '.' ? '/' : ch;
6650 }
6651 *filename->ptr++ = '/';
6652
345137c7 6653 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6654 {
49f48c71 6655 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6656 int current_entry_len = zipd->filename_length;
6657
345137c7
TT
6658 if (current_entry_len >= BUFFER_LENGTH (filename)
6659 && strncmp (filename->data, current_entry,
6660 BUFFER_LENGTH (filename)) != 0)
63a212ed 6661 continue;
345137c7 6662 found |= note_possible_classname (current_entry,
63a212ed
PB
6663 current_entry_len);
6664 }
6665 }
6666 }
6667 else
6668 {
6669 BUFFER_RESET (filename);
6670 buffer_grow (filename, entry_length + package_length + 4);
6671 strcpy (filename->data, entry_name);
6672 filename->ptr = filename->data + entry_length;
6673 for (k = 0; k < package_length; k++)
6674 {
6675 char ch = package_name[k];
6676 *filename->ptr++ = ch == '.' ? '/' : ch;
6677 }
6678 *filename->ptr = '\0';
6679
6680 dirp = opendir (filename->data);
6681 if (dirp == NULL)
6682 continue;
6683 *filename->ptr++ = '/';
6684 for (;;)
6685 {
63a212ed 6686 int len;
49f48c71 6687 const char *d_name;
63a212ed
PB
6688 struct dirent *direntp = readdir (dirp);
6689 if (!direntp)
6690 break;
6691 d_name = direntp->d_name;
6692 len = strlen (direntp->d_name);
6693 buffer_grow (filename, len+1);
6694 strcpy (filename->ptr, d_name);
345137c7 6695 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6696 package_length+len+1);
6697 }
6698 if (dirp)
6699 closedir (dirp);
6700 }
6701 }
e04a16fb 6702
63a212ed 6703 free (filename->data);
e04a16fb 6704
63a212ed
PB
6705 /* Here we should have a unified way of retrieving an entry, to be
6706 indexed. */
6707 if (!found)
e04a16fb
AG
6708 {
6709 static int first = 1;
6710 if (first)
6711 {
781b0558 6712 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6713 java_error_count++;
6714 first = 0;
6715 }
6716 else
63a212ed
PB
6717 parse_error_context (wfl, "Package `%s' not found in import",
6718 package_name);
e04a16fb
AG
6719 current_jcf = saved_jcf;
6720 return;
6721 }
e04a16fb
AG
6722 current_jcf = saved_jcf;
6723}
6724
6725/* Possibly find a type in the import on demands specified
6726 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6727 entire list, to detected potential double definitions. */
6728
6729static int
6730find_in_imports_on_demand (class_type)
6731 tree class_type;
6732{
ab3a6dd6 6733 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6734 int seen_once = -1;
ab3a6dd6 6735 tree cl = NULL_TREE;
e04a16fb
AG
6736
6737 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6738 {
49f48c71 6739 const char *id_name;
e04a16fb
AG
6740 obstack_grow (&temporary_obstack,
6741 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6742 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6743 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6744 obstack_grow0 (&temporary_obstack,
6745 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6746 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6747 id_name = obstack_finish (&temporary_obstack);
6748
6749 node = maybe_get_identifier (id_name);
6750 if (node && IS_A_CLASSFILE_NAME (node))
6751 {
6752 if (seen_once < 0)
6753 {
6754 cl = TREE_PURPOSE (import);
6755 seen_once = 1;
6756 node_to_use = node;
6757 }
6758 else
6759 {
6760 seen_once++;
6761 parse_error_context
1e12ab9b
APB
6762 (TREE_PURPOSE (import),
6763 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6764 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6765 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6766 }
6767 }
6768 }
6769
6770 if (seen_once == 1)
6771 {
6772 /* Setup lineno so that it refers to the line of the import (in
6773 case we parse a class file and encounter errors */
6774 tree decl;
6775 int saved_lineno = lineno;
6776 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6777 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6778 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6779 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6780 /* If there is no DECL set for the class or if the class isn't
6781 loaded and not seen in source yet, the load */
6782 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6783 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6784 load_class (node_to_use, 0);
6785 lineno = saved_lineno;
6786 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6787 }
6788 else
6789 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6790}
6791
9a7ab4b3
APB
6792/* Add package NAME to the list of package encountered so far. To
6793 speed up class lookup in do_resolve_class, we make sure a
6794 particular package is added only once. */
6795
6796static void
6797register_package (name)
6798 tree name;
6799{
6800 static struct hash_table _pht, *pht = NULL;
6801
6802 if (!pht)
6803 {
6804 hash_table_init (&_pht, hash_newfunc,
6805 java_hash_hash_tree_node, java_hash_compare_tree_node);
6806 pht = &_pht;
6807 }
6808
6809 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6810 {
6811 package_list = chainon (package_list, build_tree_list (name, NULL));
6812 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6813 }
6814}
6815
5e942c50
APB
6816static tree
6817resolve_package (pkg, next)
6818 tree pkg, *next;
6819{
c2952b01 6820 tree current, acc;
5e942c50 6821 tree type_name = NULL_TREE;
49f48c71 6822 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6823
6824 /* The trick is to determine when the package name stops and were
6825 the name of something contained in the package starts. Then we
6826 return a fully qualified name of what we want to get. */
6827
6828 /* Do a quick search on well known package names */
6829 if (!strncmp (name, "java.lang.reflect", 17))
6830 {
6831 *next =
6832 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6833 type_name = lookup_package_type (name, 17);
6834 }
6835 else if (!strncmp (name, "java.lang", 9))
6836 {
6837 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6838 type_name = lookup_package_type (name, 9);
6839 }
5e942c50 6840
2c56429a
APB
6841 /* If we found something here, return */
6842 if (type_name)
6843 return type_name;
6844
6845 *next = EXPR_WFL_QUALIFICATION (pkg);
6846
6847 /* Try the current package. */
6848 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6849 IDENTIFIER_LENGTH (ctxp->package)))
6850 {
6851 type_name =
6852 lookup_package_type_and_set_next (name,
6853 IDENTIFIER_LENGTH (ctxp->package),
6854 next );
6855 if (type_name)
6856 return type_name;
6857 }
6858
6859 /* Search in imported package */
6860 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6861 {
6862 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6863 int len = IDENTIFIER_LENGTH (current_pkg_name);
6864 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6865 {
6866 tree left, dummy;
6867
6868 breakdown_qualified (&left, &dummy, current_pkg_name);
6869 len = IDENTIFIER_LENGTH (left);
6870 type_name = lookup_package_type_and_set_next (name, len, next);
6871 if (type_name)
6872 break;
6873 }
6874 }
6875
c2952b01
APB
6876 /* Try to progressively construct a type name */
6877 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6878 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6879 current; current = TREE_CHAIN (current))
6880 {
6881 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6882 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6883 {
6884 type_name = acc;
6b48deee
APB
6885 /* resolve_package should be used in a loop, hence we
6886 point at this one to naturally process the next one at
6887 the next iteration. */
6888 *next = current;
c2952b01
APB
6889 break;
6890 }
6891 }
2c56429a
APB
6892 return type_name;
6893}
6894
6895static tree
6896lookup_package_type_and_set_next (name, len, next)
49f48c71 6897 const char *name;
2c56429a
APB
6898 int len;
6899 tree *next;
6900{
49f48c71 6901 const char *ptr;
2c56429a
APB
6902 tree type_name = lookup_package_type (name, len);
6903
6904 if (!type_name)
6905 return NULL;
6906
6907 ptr = IDENTIFIER_POINTER (type_name);
6908 while (ptr && (ptr = strchr (ptr, '.')))
6909 {
6910 *next = TREE_CHAIN (*next);
6911 ptr++;
6912 }
5e942c50
APB
6913 return type_name;
6914}
6915
6916static tree
6917lookup_package_type (name, from)
49f48c71 6918 const char *name;
5e942c50
APB
6919 int from;
6920{
6921 char subname [128];
49f48c71 6922 const char *sub = &name[from+1];
5e942c50
APB
6923 while (*sub != '.' && *sub)
6924 sub++;
6925 strncpy (subname, name, sub-name);
6926 subname [sub-name] = '\0';
6927 return get_identifier (subname);
6928}
6929
cf1748bf 6930static void
4dbf4496
APB
6931check_inner_class_access (decl, enclosing_decl, cl)
6932 tree decl, enclosing_decl, cl;
cf1748bf 6933{
4dbf4496
APB
6934 int access = 0;
6935
cf1748bf 6936 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6937 as a result of processing a JDEP crafted by source_start_java_method
6938 for the purpose of patching its parm decl. But the error would
6939 have been already trapped when fixing the method's signature.
6940 DECL can also be NULL in case of earlier errors. */
6941 if (!decl || !cl)
cf1748bf
APB
6942 return;
6943
4dbf4496
APB
6944 /* We grant access to private and protected inner classes if the
6945 location from where we're trying to access DECL is an enclosing
6946 context for DECL or if both have a common enclosing context. */
6947 if (CLASS_PRIVATE (decl))
6948 access = 1;
6949 if (CLASS_PROTECTED (decl))
6950 access = 2;
6951 if (!access)
6952 return;
6953
6954 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6955 TREE_TYPE (decl))
6956 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6957 TREE_TYPE (decl)))
6958 return;
6959
6960 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6961 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6962 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6963 lang_printable_name (decl, 0));
6964}
6965
e04a16fb
AG
6966/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6967 access violations were found, 1 otherwise. */
6968
6969static int
6970check_pkg_class_access (class_name, cl)
6971 tree class_name;
6972 tree cl;
6973{
6974 tree type;
e04a16fb
AG
6975
6976 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6977 return 0;
6978
6979 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6980 return 0;
6981
6982 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6983 {
e28cd97b
APB
6984 /* Access to a private class within the same package is
6985 allowed. */
6986 tree l, r;
6987 breakdown_qualified (&l, &r, class_name);
6988 if (l == ctxp->package)
6989 return 0;
6990
e04a16fb 6991 parse_error_context
781b0558 6992 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6993 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6994 IDENTIFIER_POINTER (class_name));
6995 return 1;
6996 }
6997 return 0;
6998}
6999
7000/* Local variable declaration. */
7001
7002static void
7003declare_local_variables (modifier, type, vlist)
7004 int modifier;
7005 tree type;
7006 tree vlist;
7007{
c583dd46
APB
7008 tree decl, current, saved_type;
7009 tree type_wfl = NULL_TREE;
e04a16fb 7010 int must_chain = 0;
c2952b01 7011 int final_p = 0;
e04a16fb 7012
2aa11e97 7013 /* Push a new block if statements were seen between the last time we
e04a16fb 7014 pushed a block and now. Keep a cound of block to close */
f099f336 7015 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 7016 {
f099f336 7017 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 7018 tree b = enter_block ();
f099f336 7019 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
7020 }
7021
7022 if (modifier)
7023 {
7024 int i;
7025 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 7026 if (modifier == ACC_FINAL)
c2952b01 7027 final_p = 1;
c877974e
APB
7028 else
7029 {
7030 parse_error_context
7031 (ctxp->modifier_ctx [i],
7032 "Only `final' is allowed as a local variables modifier");
7033 return;
7034 }
e04a16fb
AG
7035 }
7036
c583dd46
APB
7037 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7038 hold the TYPE value if a new incomplete has to be created (as
7039 opposed to being found already existing and reused). */
7040 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7041
7042 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 7043 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
7044
7045 /* Go through all the declared variables */
7046 for (current = vlist, saved_type = type; current;
7047 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 7048 {
c877974e 7049 tree other, real_type;
e04a16fb
AG
7050 tree wfl = TREE_PURPOSE (current);
7051 tree name = EXPR_WFL_NODE (wfl);
7052 tree init = TREE_VALUE (current);
e04a16fb 7053
c583dd46
APB
7054 /* Process NAME, as it may specify extra dimension(s) for it */
7055 type = build_array_from_name (type, type_wfl, name, &name);
7056
7057 /* Variable redefinition check */
7058 if ((other = lookup_name_in_blocks (name)))
7059 {
7060 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7061 DECL_SOURCE_LINE (other));
7062 continue;
7063 }
7064
7065 /* Type adjustment. We may have just readjusted TYPE because
7066 the variable specified more dimensions. Make sure we have
7067 a reference if we can and don't have one already. */
1886c9d8 7068 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7069
7070 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7071 /* Never layout this decl. This will be done when its scope
7072 will be entered */
c877974e 7073 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 7074 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 7075 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7076 BLOCK_CHAIN_DECL (decl);
7077
d4370213
APB
7078 /* If doing xreferencing, replace the line number with the WFL
7079 compound value */
7080 if (flag_emit_xref)
7081 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7082
e04a16fb
AG
7083 /* Don't try to use an INIT statement when an error was found */
7084 if (init && java_error_count)
7085 init = NULL_TREE;
c583dd46
APB
7086
7087 /* Add the initialization function to the current function's code */
7088 if (init)
e04a16fb 7089 {
c583dd46
APB
7090 /* Name might have been readjusted */
7091 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7092 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7093 java_method_add_stmt (current_function_decl,
7094 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7095 init));
7096 }
7097
7098 /* Setup dependency the type of the decl */
7099 if (must_chain)
7100 {
7101 jdep *dep;
7102 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7103 dep = CLASSD_LAST (ctxp->classd_list);
7104 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7105 }
7106 }
7107 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7108}
7109
7110/* Called during parsing. Build decls from argument list. */
7111
7112static void
7113source_start_java_method (fndecl)
7114 tree fndecl;
7115{
7116 tree tem;
7117 tree parm_decl;
7118 int i;
7119
79d13333
APB
7120 if (!fndecl)
7121 return;
7122
e04a16fb
AG
7123 current_function_decl = fndecl;
7124
7125 /* New scope for the function */
7126 enter_block ();
7127 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7128 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7129 {
7130 tree type = TREE_VALUE (tem);
7131 tree name = TREE_PURPOSE (tem);
7132
23a79c61
APB
7133 /* If type is incomplete. Create an incomplete decl and ask for
7134 the decl to be patched later */
e04a16fb
AG
7135 if (INCOMPLETE_TYPE_P (type))
7136 {
7137 jdep *jdep;
c877974e
APB
7138 tree real_type = GET_REAL_TYPE (type);
7139 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7140 type = obtain_incomplete_type (type);
e04a16fb
AG
7141 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7142 jdep = CLASSD_LAST (ctxp->classd_list);
7143 JDEP_MISC (jdep) = name;
7144 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7145 }
7146 else
7147 parm_decl = build_decl (PARM_DECL, name, type);
7148
c2952b01
APB
7149 /* Remember if a local variable was declared final (via its
7150 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7151 if (ARG_FINAL_P (tem))
c7303e41
APB
7152 {
7153 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7154 LOCAL_FINAL (parm_decl) = 1;
7155 }
c2952b01 7156
e04a16fb
AG
7157 BLOCK_CHAIN_DECL (parm_decl);
7158 }
7159 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7160 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7161 nreverse (tem);
7162 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7163 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7164}
7165
22eed1e6
APB
7166/* Called during parsing. Creates an artificial method declaration. */
7167
7168static tree
7169create_artificial_method (class, flags, type, name, args)
7170 tree class;
7171 int flags;
7172 tree type, name, args;
7173{
22eed1e6
APB
7174 tree mdecl;
7175
c2952b01 7176 java_parser_context_save_global ();
22eed1e6
APB
7177 lineno = 0;
7178 mdecl = make_node (FUNCTION_TYPE);
7179 TREE_TYPE (mdecl) = type;
7180 TYPE_ARG_TYPES (mdecl) = args;
7181 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7182 java_parser_context_restore_global ();
22eed1e6
APB
7183 DECL_ARTIFICIAL (mdecl) = 1;
7184 return mdecl;
7185}
7186
7187/* Starts the body if an artifical method. */
7188
7189static void
7190start_artificial_method_body (mdecl)
7191 tree mdecl;
7192{
7193 DECL_SOURCE_LINE (mdecl) = 1;
7194 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7195 source_start_java_method (mdecl);
7196 enter_block ();
7197}
7198
7199static void
7200end_artificial_method_body (mdecl)
7201 tree mdecl;
7202{
1e0cdc10
APB
7203 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7204 It has to be evaluated first. (if mdecl is current_function_decl,
7205 we have an undefined behavior if no temporary variable is used.) */
7206 tree b = exit_block ();
7207 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7208 exit_block ();
7209}
7210
e04a16fb
AG
7211/* Called during expansion. Push decls formerly built from argument
7212 list so they're usable during expansion. */
7213
7214static void
7215expand_start_java_method (fndecl)
7216 tree fndecl;
7217{
7218 tree tem, *ptr;
e04a16fb 7219
e04a16fb
AG
7220 current_function_decl = fndecl;
7221
c2952b01
APB
7222 if (! quiet_flag)
7223 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7224 announce_function (fndecl);
c2952b01
APB
7225 if (! quiet_flag)
7226 fprintf (stderr, "]");
7227
7228 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7229 ptr = &DECL_ARGUMENTS (fndecl);
7230 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7231 while (tem)
7232 {
7233 tree next = TREE_CHAIN (tem);
b67d701b 7234 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7235 if (PROMOTE_PROTOTYPES
7236 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7237 && INTEGRAL_TYPE_P (type))
7238 type = integer_type_node;
b67d701b 7239 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7240 layout_decl (tem, 0);
7241 pushdecl (tem);
e04a16fb
AG
7242 *ptr = tem;
7243 ptr = &TREE_CHAIN (tem);
7244 tem = next;
7245 }
7246 *ptr = NULL_TREE;
7247 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7248 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7249}
7250
7251/* Terminate a function and expand its body. */
7252
7253static void
7254source_end_java_method ()
7255{
7256 tree fndecl = current_function_decl;
138657ec 7257 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7258
79d13333
APB
7259 if (!fndecl)
7260 return;
7261
e04a16fb
AG
7262 java_parser_context_save_global ();
7263 lineno = ctxp->last_ccb_indent1;
7264
b67d701b
PB
7265 /* Set EH language codes */
7266 java_set_exception_lang_code ();
7267
5423609c
APB
7268 /* Turn function bodies with only a NOP expr null, so they don't get
7269 generated at all and we won't get warnings when using the -W
7270 -Wall flags. */
7271 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7272 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7273
e04a16fb
AG
7274 /* Generate function's code */
7275 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7276 && ! flag_emit_class_files
7277 && ! flag_emit_xref)
e04a16fb
AG
7278 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7279
7280 /* pop out of its parameters */
7281 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7282 poplevel (1, 0, 1);
7283 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7284
7285 /* Generate rtl for function exit. */
e8fc7396 7286 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7287 {
7288 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7289 /* Emit catch-finally clauses */
7290 emit_handlers ();
e04a16fb
AG
7291 expand_function_end (input_filename, lineno, 0);
7292
138657ec
AH
7293 /* FIXME: If the current method contains any exception handlers,
7294 force asynchronous_exceptions: this is necessary because signal
7295 handlers in libjava may throw exceptions. This is far from being
7296 a perfect solution, but it's better than doing nothing at all.*/
7297 if (catch_clauses)
7298 asynchronous_exceptions = 1;
7299
e04a16fb
AG
7300 /* Run the optimizers and output assembler code for this function. */
7301 rest_of_compilation (fndecl);
7302 }
7303
7304 current_function_decl = NULL_TREE;
e04a16fb 7305 java_parser_context_restore_global ();
138657ec 7306 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7307}
7308
7309/* Record EXPR in the current function block. Complements compound
7310 expression second operand if necessary. */
7311
7312tree
7313java_method_add_stmt (fndecl, expr)
7314 tree fndecl, expr;
7315{
b771925e
APB
7316 if (!GET_CURRENT_BLOCK (fndecl))
7317 return NULL_TREE;
f099f336 7318 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7319}
e04a16fb 7320
b67d701b
PB
7321static tree
7322add_stmt_to_block (b, type, stmt)
7323 tree b, type, stmt;
7324{
7325 tree body = BLOCK_EXPR_BODY (b), c;
7326
e04a16fb
AG
7327 if (java_error_count)
7328 return body;
b67d701b
PB
7329
7330 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7331 return body;
7332
b67d701b
PB
7333 BLOCK_EXPR_BODY (b) = c;
7334 TREE_SIDE_EFFECTS (c) = 1;
7335 return c;
e04a16fb
AG
7336}
7337
7338/* Add STMT to EXISTING if possible, otherwise create a new
7339 COMPOUND_EXPR and add STMT to it. */
7340
7341static tree
7342add_stmt_to_compound (existing, type, stmt)
7343 tree existing, type, stmt;
7344{
15fdcfe9
PB
7345 if (existing)
7346 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7347 else
15fdcfe9 7348 return stmt;
e04a16fb
AG
7349}
7350
1886c9d8
APB
7351void java_layout_seen_class_methods ()
7352{
7353 tree previous_list = all_class_list;
7354 tree end = NULL_TREE;
7355 tree current;
7356
7357 while (1)
7358 {
7359 for (current = previous_list;
7360 current != end; current = TREE_CHAIN (current))
7361 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7362
7363 if (previous_list != all_class_list)
7364 {
7365 end = previous_list;
7366 previous_list = all_class_list;
7367 }
7368 else
7369 break;
7370 }
7371}
7372
e04a16fb 7373void
c2952b01 7374java_reorder_fields ()
e04a16fb 7375{
c2952b01 7376 static tree stop_reordering = NULL_TREE;
19e223db 7377 static int initialized_p;
c2952b01 7378 tree current;
19e223db
MM
7379
7380 /* Register STOP_REORDERING with the garbage collector. */
7381 if (!initialized_p)
7382 {
7383 ggc_add_tree_root (&stop_reordering, 1);
7384 initialized_p = 1;
7385 }
7386
5e942c50 7387 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7388 {
5e942c50 7389 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7390
c2952b01
APB
7391 if (current_class == stop_reordering)
7392 break;
7393
c877974e
APB
7394 /* Reverse the fields, but leave the dummy field in front.
7395 Fields are already ordered for Object and Class */
7396 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7397 && current_class != class_type_node)
7398 {
23a79c61
APB
7399 /* If the dummy field is there, reverse the right fields and
7400 just layout the type for proper fields offset */
c877974e
APB
7401 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7402 {
7403 tree fields = TYPE_FIELDS (current_class);
7404 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7405 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7406 }
23a79c61
APB
7407 /* We don't have a dummy field, we need to layout the class,
7408 after having reversed the fields */
c877974e
APB
7409 else
7410 {
7411 TYPE_FIELDS (current_class) =
7412 nreverse (TYPE_FIELDS (current_class));
7413 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7414 }
7415 }
c2952b01
APB
7416 }
7417 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7418}
7419
7420/* Layout the methods of all classes loaded in one way on an
7421 other. Check methods of source parsed classes. Then reorder the
7422 fields and layout the classes or the type of all source parsed
7423 classes */
7424
7425void
7426java_layout_classes ()
7427{
7428 tree current;
7429 int save_error_count = java_error_count;
7430
7431 /* Layout the methods of all classes seen so far */
7432 java_layout_seen_class_methods ();
7433 java_parse_abort_on_error ();
7434 all_class_list = NULL_TREE;
7435
7436 /* Then check the methods of all parsed classes */
7437 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7438 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7439 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7440 java_parse_abort_on_error ();
7441
7442 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7443 {
7444 current_class = TREE_TYPE (TREE_VALUE (current));
7445 layout_class (current_class);
5e942c50 7446
c877974e
APB
7447 /* From now on, the class is considered completely loaded */
7448 CLASS_LOADED_P (current_class) = 1;
7449
5e942c50
APB
7450 /* Error reported by the caller */
7451 if (java_error_count)
7452 return;
e04a16fb 7453 }
23a79c61
APB
7454
7455 /* We might have reloaded classes durign the process of laying out
7456 classes for code generation. We must layout the methods of those
7457 late additions, as constructor checks might use them */
1886c9d8 7458 java_layout_seen_class_methods ();
23a79c61 7459 java_parse_abort_on_error ();
e04a16fb
AG
7460}
7461
c2952b01
APB
7462/* Expand methods in the current set of classes rememebered for
7463 generation. */
e04a16fb 7464
49f48c71 7465static void
c2952b01 7466java_complete_expand_classes ()
e04a16fb
AG
7467{
7468 tree current;
ce6e9147
APB
7469
7470 do_not_fold = flag_emit_xref;
c2952b01 7471
e04a16fb 7472 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7473 if (!INNER_CLASS_DECL_P (current))
7474 java_complete_expand_class (current);
7475}
e04a16fb 7476
c2952b01
APB
7477/* Expand the methods found in OUTER, starting first by OUTER's inner
7478 classes, if any. */
e04a16fb 7479
c2952b01
APB
7480static void
7481java_complete_expand_class (outer)
7482 tree outer;
7483{
7484 tree inner_list;
e04a16fb 7485
c2952b01 7486 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7487
c2952b01
APB
7488 /* We need to go after all inner classes and start expanding them,
7489 starting with most nested ones. We have to do that because nested
7490 classes might add functions to outer classes */
e04a16fb 7491
c2952b01
APB
7492 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7493 inner_list; inner_list = TREE_CHAIN (inner_list))
7494 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7495
c2952b01
APB
7496 java_complete_expand_methods (outer);
7497 set_nested_class_simple_name_value (outer, 0); /* Reset */
7498}
7499
7500/* Expand methods registered in CLASS_DECL. The general idea is that
7501 we expand regular methods first. This allows us get an estimate on
7502 how outer context local alias fields are really used so we can add
7503 to the constructor just enough code to initialize them properly (it
c00f0fb2 7504 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7505 constructors and then <clinit>. */
7506
7507static void
7508java_complete_expand_methods (class_decl)
7509 tree class_decl;
7510{
7511 tree clinit, finit, decl, first_decl;
7512
7513 current_class = TREE_TYPE (class_decl);
7514
c7303e41
APB
7515 /* Find whether the class has final variables */
7516 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7517 if (FIELD_FINAL (decl))
7518 {
7519 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7520 break;
7521 }
7522
c2952b01
APB
7523 /* Initialize a new constant pool */
7524 init_outgoing_cpool ();
7525
7526 /* Pre-expand <clinit> to figure whether we really need it or
7527 not. If we do need it, we pre-expand the static fields so they're
7528 ready to be used somewhere else. <clinit> will be fully expanded
7529 after we processed the constructors. */
7530 first_decl = TYPE_METHODS (current_class);
7531 clinit = maybe_generate_pre_expand_clinit (current_class);
7532
c00f0fb2 7533 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7534 try to use it.*/
7535 if (TYPE_FINIT_STMT_LIST (current_class))
7536 {
7537 finit = generate_finit (current_class);
7538 java_complete_expand_method (finit);
7539 }
7540
7541 /* Now do the constructors */
7542 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7543 {
7544 int no_body;
7545
7546 if (!DECL_CONSTRUCTOR_P (decl))
7547 continue;
7548
7549 no_body = !DECL_FUNCTION_BODY (decl);
7550 /* Don't generate debug info on line zero when expanding a
7551 generated constructor. */
7552 if (no_body)
7553 restore_line_number_status (1);
7554
c7303e41
APB
7555 /* Reset the final local variable assignment flags */
7556 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7557 reset_final_variable_local_assignment_flag (current_class);
7558
c2952b01 7559 java_complete_expand_method (decl);
c7303e41
APB
7560
7561 /* Check for missed out final variable assignment */
7562 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7563 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7564
7565 if (no_body)
7566 restore_line_number_status (0);
7567 }
7568
7569 /* First, do the ordinary methods. */
7570 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7571 {
7145d9fe
TT
7572 /* Skip abstract or native methods -- but do handle native
7573 methods when generating JNI stubs. */
7574 if (METHOD_ABSTRACT (decl)
7575 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7576 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7577 continue;
7145d9fe
TT
7578
7579 if (METHOD_NATIVE (decl))
7580 {
7581 tree body = build_jni_stub (decl);
7582 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7583 }
7584
c2952b01
APB
7585 java_complete_expand_method (decl);
7586 }
7587
7588 /* If there is indeed a <clinit>, fully expand it now */
7589 if (clinit)
7590 {
c7303e41
APB
7591 /* Reset the final local variable assignment flags */
7592 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7593 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7594 /* Prevent the use of `this' inside <clinit> */
7595 ctxp->explicit_constructor_p = 1;
7596 java_complete_expand_method (clinit);
7597 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7598 /* Check for missed out static final variable assignment */
7599 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7600 && !CLASS_INTERFACE (class_decl))
7601 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7602 }
c2952b01 7603
165f37bc
APB
7604 /* We might have generated a class$ that we now want to expand */
7605 if (TYPE_DOT_CLASS (current_class))
7606 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7607
c2952b01
APB
7608 /* Now verify constructor circularity (stop after the first one we
7609 prove wrong.) */
7610 if (!CLASS_INTERFACE (class_decl))
7611 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7612 if (DECL_CONSTRUCTOR_P (decl)
7613 && verify_constructor_circularity (decl, decl))
7614 break;
7615
c7303e41
APB
7616 /* Final check on the initialization of final variables. */
7617 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7618 {
7619 check_final_variable_global_assignment_flag (current_class);
7620 /* If we have an interface, check for uninitialized fields. */
7621 if (CLASS_INTERFACE (class_decl))
7622 check_static_final_variable_assignment_flag (current_class);
7623 }
7624
c2952b01
APB
7625 /* Save the constant pool. We'll need to restore it later. */
7626 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7627}
7628
c2952b01
APB
7629/* Attempt to create <clinit>. Pre-expand static fields so they can be
7630 safely used in some other methods/constructors. */
e920ebc9 7631
c2952b01
APB
7632static tree
7633maybe_generate_pre_expand_clinit (class_type)
7634 tree class_type;
e920ebc9 7635{
c2952b01
APB
7636 tree current, mdecl;
7637
7638 if (!TYPE_CLINIT_STMT_LIST (class_type))
7639 return NULL_TREE;
e920ebc9 7640
c2952b01
APB
7641 /* Go through all static fields and pre expand them */
7642 for (current = TYPE_FIELDS (class_type); current;
7643 current = TREE_CHAIN (current))
7644 if (FIELD_STATIC (current))
7645 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7646
7647 /* Then build the <clinit> method */
7648 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7649 clinit_identifier_node, end_params_node);
7650 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7651 mdecl, NULL_TREE);
7652 start_artificial_method_body (mdecl);
7653
7654 /* We process the list of assignment we produced as the result of
7655 the declaration of initialized static field and add them as
7656 statement to the <clinit> method. */
7657 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7658 current = TREE_CHAIN (current))
e920ebc9 7659 {
9a7ab4b3 7660 tree stmt = current;
c2952b01
APB
7661 /* We build the assignment expression that will initialize the
7662 field to its value. There are strict rules on static
7663 initializers (8.5). FIXME */
98a52c2c 7664 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7665 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7666 java_method_add_stmt (mdecl, stmt);
7667 }
e920ebc9 7668
c2952b01
APB
7669 end_artificial_method_body (mdecl);
7670
92d83515
APB
7671 /* Now we want to place <clinit> as the last method (because we need
7672 it at least for interface so that it doesn't interfere with the
7673 dispatch table based lookup. */
7674 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7675 {
92d83515
APB
7676 current = TREE_CHAIN (TYPE_METHODS (class_type));
7677 TYPE_METHODS (class_type) = current;
c2952b01
APB
7678
7679 while (TREE_CHAIN (current))
7680 current = TREE_CHAIN (current);
92d83515 7681
c2952b01
APB
7682 TREE_CHAIN (current) = mdecl;
7683 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7684 }
c2952b01
APB
7685
7686 return mdecl;
e920ebc9
APB
7687}
7688
dba41d30
APB
7689/* Analyzes a method body and look for something that isn't a
7690 MODIFY_EXPR. */
7691
7692static int
7693analyze_clinit_body (bbody)
7694 tree bbody;
7695{
7696 while (bbody)
7697 switch (TREE_CODE (bbody))
7698 {
7699 case BLOCK:
7700 bbody = BLOCK_EXPR_BODY (bbody);
7701 break;
7702
7703 case EXPR_WITH_FILE_LOCATION:
7704 bbody = EXPR_WFL_NODE (bbody);
7705 break;
7706
7707 case COMPOUND_EXPR:
7708 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7709 return 1;
7710 bbody = TREE_OPERAND (bbody, 1);
7711 break;
7712
7713 case MODIFY_EXPR:
7714 bbody = NULL_TREE;
7715 break;
7716
7717 default:
7718 bbody = NULL_TREE;
7719 return 1;
7720 }
7721 return 0;
7722}
7723
7724
92d83515
APB
7725/* See whether we could get rid of <clinit>. Criteria are: all static
7726 final fields have constant initial values and the body of <clinit>
7727 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7728
7729static int
7730maybe_yank_clinit (mdecl)
7731 tree mdecl;
7732{
7733 tree type, current;
7734 tree fbody, bbody;
99eaf8d4 7735 int found = 0;
92d83515
APB
7736
7737 if (!DECL_CLINIT_P (mdecl))
7738 return 0;
f0f3a777
APB
7739
7740 /* If the body isn't empty, then we keep <clinit>. Note that if
7741 we're emitting classfiles, this isn't enough not to rule it
7742 out. */
92d83515 7743 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7744 bbody = BLOCK_EXPR_BODY (fbody);
7745 if (bbody && bbody != error_mark_node)
92d83515 7746 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7747 else
7748 return 0;
f0f3a777 7749 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7750 return 0;
7751
7752 type = DECL_CONTEXT (mdecl);
7753 current = TYPE_FIELDS (type);
7754
7755 for (current = (current ? TREE_CHAIN (current) : current);
7756 current; current = TREE_CHAIN (current))
f0f3a777
APB
7757 {
7758 tree f_init;
7759
7760 /* We're not interested in non static field */
7761 if (!FIELD_STATIC (current))
7762 continue;
7763
7764 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7765 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7766 should generated an empty <clinit> so that SUID are computed
7767 correctly. */
7768 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7769 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7770 break;
7771
7772 f_init = DECL_INITIAL (current);
7773 /* If we're emitting native code, we want static final fields to
7774 have constant initializers. If we don't meet these
7775 conditions, we keep <clinit> */
7776 if (!flag_emit_class_files
7777 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7778 break;
7779 /* If we're emitting bytecode, we want static fields to have
7780 constant initializers or no initializer. If we don't meet
7781 these conditions, we keep <clinit> */
7782 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7783 break;
7784 }
92d83515 7785
99eaf8d4
APB
7786 /* Now we analyze the method body and look for something that
7787 isn't a MODIFY_EXPR */
7788 if (bbody == empty_stmt_node)
dba41d30
APB
7789 found = 0;
7790 else
7791 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7792
7793 if (current || found)
92d83515
APB
7794 return 0;
7795
7796 /* Get rid of <clinit> in the class' list of methods */
7797 if (TYPE_METHODS (type) == mdecl)
7798 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7799 else
7800 for (current = TYPE_METHODS (type); current;
7801 current = TREE_CHAIN (current))
7802 if (TREE_CHAIN (current) == mdecl)
7803 {
7804 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7805 break;
7806 }
7807
7808 return 1;
7809}
7810
7811
e04a16fb
AG
7812/* Complete and expand a method. */
7813
7814static void
7815java_complete_expand_method (mdecl)
7816 tree mdecl;
7817{
92d83515
APB
7818 int yank_clinit = 0;
7819
c2952b01 7820 current_function_decl = mdecl;
22eed1e6
APB
7821 /* Fix constructors before expanding them */
7822 if (DECL_CONSTRUCTOR_P (mdecl))
7823 fix_constructors (mdecl);
e04a16fb 7824
22eed1e6 7825 /* Expand functions that have a body */
e04a16fb
AG
7826 if (DECL_FUNCTION_BODY (mdecl))
7827 {
9bbc7d9f
PB
7828 tree fbody = DECL_FUNCTION_BODY (mdecl);
7829 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7830 tree exception_copy = NULL_TREE;
e04a16fb 7831 expand_start_java_method (mdecl);
939d7216 7832 build_result_decl (mdecl);
e04a16fb
AG
7833
7834 current_this
7835 = (!METHOD_STATIC (mdecl) ?
7836 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7837
ce6e9147
APB
7838 /* Purge the `throws' list of unchecked exceptions. If we're
7839 doing xref, save a copy of the list and re-install it
7840 later. */
7841 if (flag_emit_xref)
7842 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7843
b9f7e36c
APB
7844 purge_unchecked_exceptions (mdecl);
7845
7846 /* Install exceptions thrown with `throws' */
7847 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7848
9bbc7d9f 7849 if (block_body != NULL_TREE)
bc3ca41b
PB
7850 {
7851 block_body = java_complete_tree (block_body);
c2952b01 7852
7145d9fe 7853 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7854 check_for_initialization (block_body);
f099f336 7855 ctxp->explicit_constructor_p = 0;
bc3ca41b 7856 }
e803d3b2 7857
9bbc7d9f 7858 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7859
c2952b01
APB
7860 /* If we saw a return but couldn't evaluate it properly, we'll
7861 have an error_mark_node here. */
7862 if (block_body != error_mark_node
7863 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7864 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7865 && !flag_emit_xref)
82371d41 7866 missing_return_error (current_function_decl);
7525cc04 7867
92d83515
APB
7868 /* Check wether we could just get rid of clinit, now the picture
7869 is complete. */
7870 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7871 complete_start_java_method (mdecl);
7872
e04a16fb 7873 /* Don't go any further if we've found error(s) during the
92d83515
APB
7874 expansion */
7875 if (!java_error_count && !yank_clinit)
e04a16fb 7876 source_end_java_method ();
22eed1e6
APB
7877 else
7878 {
92d83515
APB
7879 if (java_error_count)
7880 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7881 poplevel (1, 0, 1);
7882 }
b9f7e36c
APB
7883
7884 /* Pop the exceptions and sanity check */
7885 POP_EXCEPTIONS();
7886 if (currently_caught_type_list)
7887 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7888
7889 if (flag_emit_xref)
7890 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7891 }
7892}
7893
c2952b01
APB
7894\f
7895
7896/* This section of the code deals with accessing enclosing context
7897 fields either directly by using the relevant access to this$<n> or
7898 by invoking an access method crafted for that purpose. */
7899
7900/* Build the necessary access from an inner class to an outer
7901 class. This routine could be optimized to cache previous result
7902 (decl, current_class and returned access). When an access method
7903 needs to be generated, it always takes the form of a read. It might
7904 be later turned into a write by calling outer_field_access_fix. */
7905
7906static tree
7907build_outer_field_access (id, decl)
7908 tree id, decl;
7909{
7910 tree access = NULL_TREE;
7911 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7912 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7913
5e18f6d6
APB
7914 /* If the immediate enclosing context of the current class is the
7915 field decl's class or inherits from it; build the access as
7916 `this$<n>.<field>'. Note that we will break the `private' barrier
7917 if we're not emitting bytecodes. */
7918 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7919 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7920 {
7921 tree thisn = build_current_thisn (current_class);
7922 access = make_qualified_primary (build_wfl_node (thisn),
7923 id, EXPR_WFL_LINECOL (id));
7924 }
7925 /* Otherwise, generate access methods to outer this and access the
7926 field (either using an access method or by direct access.) */
7927 else
7928 {
7929 int lc = EXPR_WFL_LINECOL (id);
7930
7931 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7932 get a hold to the enclosing instance we need, and then we
c2952b01 7933 build the field access. */
5e18f6d6 7934 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7935
7936 /* If the field is private and we're generating bytecode, then
7937 we generate an access method */
7938 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7939 {
7940 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7941 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7942 name, access, NULL_TREE);
7943 }
7944 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7945 Once again we break the `private' access rule from a foreign
7946 class. */
7947 else
7948 access = make_qualified_primary (access, id, lc);
7949 }
7950 return resolve_expression_name (access, NULL);
7951}
7952
7953/* Return a non zero value if NODE describes an outer field inner
7954 access. */
7955
7956static int
7957outer_field_access_p (type, decl)
7958 tree type, decl;
7959{
7960 if (!INNER_CLASS_TYPE_P (type)
7961 || TREE_CODE (decl) != FIELD_DECL
7962 || DECL_CONTEXT (decl) == type)
7963 return 0;
ee5f86dc
APB
7964
7965 /* If the inner class extends the declaration context of the field
7966 we're try to acces, then this isn't an outer field access */
7967 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7968 return 0;
c2952b01
APB
7969
7970 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7971 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7972 {
7973 if (type == DECL_CONTEXT (decl))
7974 return 1;
5e18f6d6 7975
c2952b01 7976 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7977 {
7978 /* Before we give up, see whether the field is inherited from
7979 the enclosing context we're considering. */
7980 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7981 return 1;
7982 break;
7983 }
c2952b01
APB
7984 }
7985
7986 return 0;
7987}
7988
7989/* Return a non zero value if NODE represents an outer field inner
7990 access that was been already expanded. As a side effect, it returns
7991 the name of the field being accessed and the argument passed to the
7992 access function, suitable for a regeneration of the access method
7993 call if necessary. */
7994
7995static int
7996outer_field_expanded_access_p (node, name, arg_type, arg)
7997 tree node, *name, *arg_type, *arg;
7998{
7999 int identified = 0;
8000
8001 if (TREE_CODE (node) != CALL_EXPR)
8002 return 0;
8003
8004 /* Well, gcj generates slightly different tree nodes when compiling
8005 to native or bytecodes. It's the case for function calls. */
8006
8007 if (flag_emit_class_files
8008 && TREE_CODE (node) == CALL_EXPR
8009 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8010 identified = 1;
8011 else if (!flag_emit_class_files)
8012 {
8013 node = TREE_OPERAND (node, 0);
8014
8015 if (node && TREE_OPERAND (node, 0)
8016 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8017 {
8018 node = TREE_OPERAND (node, 0);
8019 if (TREE_OPERAND (node, 0)
8020 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8021 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8022 (DECL_NAME (TREE_OPERAND (node, 0)))))
8023 identified = 1;
8024 }
8025 }
8026
8027 if (identified && name && arg_type && arg)
8028 {
8029 tree argument = TREE_OPERAND (node, 1);
8030 *name = DECL_NAME (TREE_OPERAND (node, 0));
8031 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8032 *arg = TREE_VALUE (argument);
8033 }
8034 return identified;
8035}
8036
8037/* Detect in NODE an outer field read access from an inner class and
8038 transform it into a write with RHS as an argument. This function is
8039 called from the java_complete_lhs when an assignment to a LHS can
8040 be identified. */
8041
8042static tree
8043outer_field_access_fix (wfl, node, rhs)
8044 tree wfl, node, rhs;
8045{
8046 tree name, arg_type, arg;
8047
8048 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8049 {
8050 /* At any rate, check whether we're trying to assign a value to
8051 a final. */
8052 tree accessed = (JDECL_P (node) ? node :
8053 (TREE_CODE (node) == COMPONENT_REF ?
8054 TREE_OPERAND (node, 1) : node));
8055 if (check_final_assignment (accessed, wfl))
8056 return error_mark_node;
8057
8058 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8059 arg_type, name, arg, rhs);
8060 return java_complete_tree (node);
8061 }
8062 return NULL_TREE;
8063}
8064
8065/* Construct the expression that calls an access method:
8066 <type>.access$<n>(<arg1> [, <arg2>]);
8067
8068 ARG2 can be NULL and will be omitted in that case. It will denote a
8069 read access. */
8070
8071static tree
8072build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8073 int lc;
8074 tree type, access_method_name, arg1, arg2;
8075{
8076 tree args, cn, access;
8077
8078 args = arg1 ? arg1 :
8079 build_wfl_node (build_current_thisn (current_class));
8080 args = build_tree_list (NULL_TREE, args);
8081
8082 if (arg2)
8083 args = tree_cons (NULL_TREE, arg2, args);
8084
8085 access = build_method_invocation (build_wfl_node (access_method_name), args);
8086 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8087 return make_qualified_primary (cn, access, lc);
8088}
8089
8090static tree
8091build_new_access_id ()
8092{
8093 static int access_n_counter = 1;
8094 char buffer [128];
8095
8096 sprintf (buffer, "access$%d", access_n_counter++);
8097 return get_identifier (buffer);
8098}
8099
8100/* Create the static access functions for the outer field DECL. We define a
8101 read:
8102 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8103 return inst$.field;
8104 }
8105 and a write access:
8106 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8107 TREE_TYPE (<field>) value$) {
8108 return inst$.field = value$;
8109 }
8110 We should have a usage flags on the DECL so we can lazily turn the ones
8111 we're using for code generation. FIXME.
8112*/
8113
8114static tree
8115build_outer_field_access_methods (decl)
8116 tree decl;
8117{
8118 tree id, args, stmt, mdecl;
8119
c7303e41 8120 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
8121 return FIELD_INNER_ACCESS (decl);
8122
c7303e41
APB
8123 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8124
c2952b01
APB
8125 /* Create the identifier and a function named after it. */
8126 id = build_new_access_id ();
8127
8128 /* The identifier is marked as bearing the name of a generated write
8129 access function for outer field accessed from inner classes. */
8130 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8131
8132 /* Create the read access */
8133 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8134 TREE_CHAIN (args) = end_params_node;
8135 stmt = make_qualified_primary (build_wfl_node (inst_id),
8136 build_wfl_node (DECL_NAME (decl)), 0);
8137 stmt = build_return (0, stmt);
8138 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8139 TREE_TYPE (decl), id, args, stmt);
8140 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8141
c7303e41
APB
8142 /* Create the write access method. No write access for final variable */
8143 if (!FIELD_FINAL (decl))
8144 {
8145 args = build_tree_list (inst_id,
8146 build_pointer_type (DECL_CONTEXT (decl)));
8147 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8148 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8149 stmt = make_qualified_primary (build_wfl_node (inst_id),
8150 build_wfl_node (DECL_NAME (decl)), 0);
8151 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8152 build_wfl_node (wpv_id)));
8153 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8154 TREE_TYPE (decl), id,
8155 args, stmt);
8156 }
c2952b01 8157 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8158
8159 /* Return the access name */
8160 return FIELD_INNER_ACCESS (decl) = id;
8161}
8162
8163/* Build an field access method NAME. */
8164
8165static tree
8166build_outer_field_access_method (class, type, name, args, body)
8167 tree class, type, name, args, body;
8168{
8169 tree saved_current_function_decl, mdecl;
8170
8171 /* Create the method */
8172 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8173 fix_method_argument_names (args, mdecl);
8174 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8175
8176 /* Attach the method body. */
8177 saved_current_function_decl = current_function_decl;
8178 start_artificial_method_body (mdecl);
8179 java_method_add_stmt (mdecl, body);
8180 end_artificial_method_body (mdecl);
8181 current_function_decl = saved_current_function_decl;
8182
8183 return mdecl;
8184}
8185
8186\f
8187/* This section deals with building access function necessary for
8188 certain kinds of method invocation from inner classes. */
8189
8190static tree
8191build_outer_method_access_method (decl)
8192 tree decl;
8193{
8194 tree saved_current_function_decl, mdecl;
8195 tree args = NULL_TREE, call_args = NULL_TREE;
8196 tree carg, id, body, class;
8197 char buffer [80];
8198 int parm_id_count = 0;
8199
8200 /* Test this abort with an access to a private field */
8201 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8202 abort ();
8203
8204 /* Check the cache first */
8205 if (DECL_FUNCTION_INNER_ACCESS (decl))
8206 return DECL_FUNCTION_INNER_ACCESS (decl);
8207
8208 class = DECL_CONTEXT (decl);
8209
8210 /* Obtain an access identifier and mark it */
8211 id = build_new_access_id ();
8212 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8213
c2952b01
APB
8214 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8215 /* Create the arguments, as much as the original */
8216 for (; carg && carg != end_params_node;
8217 carg = TREE_CHAIN (carg))
8218 {
8219 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8220 args = chainon (args, build_tree_list (get_identifier (buffer),
8221 TREE_VALUE (carg)));
8222 }
8223 args = chainon (args, end_params_node);
8224
8225 /* Create the method */
8226 mdecl = create_artificial_method (class, ACC_STATIC,
8227 TREE_TYPE (TREE_TYPE (decl)), id, args);
8228 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8229 /* There is a potential bug here. We should be able to use
8230 fix_method_argument_names, but then arg names get mixed up and
8231 eventually a constructor will have its this$0 altered and the
8232 outer context won't be assignment properly. The test case is
8233 stub.java FIXME */
8234 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8235
8236 /* Attach the method body. */
8237 saved_current_function_decl = current_function_decl;
8238 start_artificial_method_body (mdecl);
8239
8240 /* The actual method invocation uses the same args. When invoking a
8241 static methods that way, we don't want to skip the first
8242 argument. */
8243 carg = args;
8244 if (!METHOD_STATIC (decl))
8245 carg = TREE_CHAIN (carg);
8246 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8247 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8248 call_args);
8249
8250 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8251 call_args);
8252 if (!METHOD_STATIC (decl))
8253 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8254 body, 0);
8255 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8256 body = build_return (0, body);
8257 java_method_add_stmt (mdecl,body);
8258 end_artificial_method_body (mdecl);
8259 current_function_decl = saved_current_function_decl;
c2952b01
APB
8260
8261 /* Back tag the access function so it know what it accesses */
8262 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8263
8264 /* Tag the current method so it knows it has an access generated */
8265 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8266}
8267
8268\f
8269/* This section of the code deals with building expressions to access
8270 the enclosing instance of an inner class. The enclosing instance is
8271 kept in a generated field called this$<n>, with <n> being the
8272 inner class nesting level (starting from 0.) */
8273
dba41d30
APB
8274/* Build an access to a given this$<n>, always chaining access call to
8275 others. Access methods to this$<n> are build on the fly if
8276 necessary. This CAN'T be used to solely access this$<n-1> from
8277 this$<n> (which alway yield to special cases and optimization, see
8278 for example build_outer_field_access). */
c2952b01
APB
8279
8280static tree
8281build_access_to_thisn (from, to, lc)
8282 tree from, to;
8283 int lc;
8284{
8285 tree access = NULL_TREE;
8286
8287 while (from != to)
8288 {
c2952b01 8289 if (!access)
dba41d30
APB
8290 {
8291 access = build_current_thisn (from);
8292 access = build_wfl_node (access);
8293 }
8294 else
c2952b01 8295 {
dba41d30
APB
8296 tree access0_wfl, cn;
8297
8298 maybe_build_thisn_access_method (from);
8299 access0_wfl = build_wfl_node (access0_identifier_node);
8300 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8301 EXPR_WFL_LINECOL (access0_wfl) = lc;
8302 access = build_tree_list (NULL_TREE, access);
8303 access = build_method_invocation (access0_wfl, access);
8304 access = make_qualified_primary (cn, access, lc);
c2952b01 8305 }
5e18f6d6
APB
8306
8307 /* if FROM isn't an inter class, that's fine, we've done
8308 enough. What we're looking for can be accessed from there. */
8309 from = DECL_CONTEXT (TYPE_NAME (from));
8310 if (!from)
8311 break;
8312 from = TREE_TYPE (from);
c2952b01
APB
8313 }
8314 return access;
8315}
8316
8317/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8318 is returned if nothing needs to be generated. Otherwise, the method
152de068 8319 generated and a method decl is returned.
c2952b01
APB
8320
8321 NOTE: These generated methods should be declared in a class file
8322 attribute so that they can't be referred to directly. */
8323
8324static tree
8325maybe_build_thisn_access_method (type)
8326 tree type;
8327{
8328 tree mdecl, args, stmt, rtype;
8329 tree saved_current_function_decl;
8330
8331 /* If TYPE is a top-level class, no access method is required.
8332 If there already is such an access method, bail out. */
ee5f86dc 8333 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8334 return NULL_TREE;
8335
8336 /* We generate the method. The method looks like:
8337 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8338 */
c2952b01
APB
8339 args = build_tree_list (inst_id, build_pointer_type (type));
8340 TREE_CHAIN (args) = end_params_node;
8341 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8342 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8343 access0_identifier_node, args);
8344 fix_method_argument_names (args, mdecl);
8345 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8346 stmt = build_current_thisn (type);
8347 stmt = make_qualified_primary (build_wfl_node (inst_id),
8348 build_wfl_node (stmt), 0);
8349 stmt = build_return (0, stmt);
8350
8351 saved_current_function_decl = current_function_decl;
8352 start_artificial_method_body (mdecl);
8353 java_method_add_stmt (mdecl, stmt);
8354 end_artificial_method_body (mdecl);
8355 current_function_decl = saved_current_function_decl;
c2952b01
APB
8356
8357 CLASS_ACCESS0_GENERATED_P (type) = 1;
8358
8359 return mdecl;
8360}
8361
8362/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8363 the first level of innerclassing. this$1 for the next one, etc...
8364 This function can be invoked with TYPE to NULL, available and then
8365 has to count the parser context. */
8366
8367static tree
8368build_current_thisn (type)
8369 tree type;
8370{
8371 static int saved_i = -1;
8372 static tree saved_thisn = NULL_TREE;
19e223db
MM
8373 static tree saved_type = NULL_TREE;
8374 static int saved_type_i = 0;
8375 static int initialized_p;
c2952b01
APB
8376 tree decl;
8377 char buffer [80];
8378 int i = 0;
8379
19e223db
MM
8380 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8381 if (!initialized_p)
c2952b01 8382 {
19e223db
MM
8383 ggc_add_tree_root (&saved_thisn, 1);
8384 ggc_add_tree_root (&saved_type, 1);
8385 initialized_p = 1;
8386 }
c2952b01 8387
19e223db
MM
8388 if (type)
8389 {
c2952b01
APB
8390 if (type == saved_type)
8391 i = saved_type_i;
8392 else
8393 {
8394 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8395 decl; decl = DECL_CONTEXT (decl), i++)
8396 ;
8397
8398 saved_type = type;
8399 saved_type_i = i;
8400 }
8401 }
8402 else
8403 i = list_length (GET_CPC_LIST ())-2;
8404
8405 if (i == saved_i)
8406 return saved_thisn;
8407
8408 sprintf (buffer, "this$%d", i);
8409 saved_i = i;
8410 saved_thisn = get_identifier (buffer);
8411 return saved_thisn;
8412}
8413
8414/* Return the assignement to the hidden enclosing context `this$<n>'
8415 by the second incoming parameter to the innerclass constructor. The
8416 form used is `this.this$<n> = this$<n>;'. */
8417
8418static tree
8419build_thisn_assign ()
8420{
8421 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8422 {
8423 tree thisn = build_current_thisn (current_class);
8424 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8425 build_wfl_node (thisn), 0);
8426 tree rhs = build_wfl_node (thisn);
8427 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8428 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8429 }
8430 return NULL_TREE;
8431}
8432
8433\f
165f37bc
APB
8434/* Building the synthetic `class$' used to implement the `.class' 1.1
8435 extension for non primitive types. This method looks like:
8436
8437 static Class class$(String type) throws NoClassDefFoundError
8438 {
8439 try {return (java.lang.Class.forName (String));}
8440 catch (ClassNotFoundException e) {
8441 throw new NoClassDefFoundError(e.getMessage());}
8442 } */
8443
8444static tree
8445build_dot_class_method (class)
8446 tree class;
8447{
8448#define BWF(S) build_wfl_node (get_identifier ((S)))
8449#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8450 tree args, tmp, saved_current_function_decl, mdecl;
8451 tree stmt, throw_stmt, catch, catch_block, try_block;
8452 tree catch_clause_param;
8453 tree class_not_found_exception, no_class_def_found_error;
8454
8455 static tree get_message_wfl, type_parm_wfl;
8456
8457 if (!get_message_wfl)
8458 {
8459 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8460 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8461 ggc_add_tree_root (&get_message_wfl, 1);
8462 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8463 }
8464
8465 /* Build the arguments */
8466 args = build_tree_list (get_identifier ("type$"),
8467 build_pointer_type (string_type_node));
8468 TREE_CHAIN (args) = end_params_node;
8469
8470 /* Build the qualified name java.lang.Class.forName */
8471 tmp = MQN (MQN (MQN (BWF ("java"),
8472 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8473
8474 /* For things we have to catch and throw */
8475 class_not_found_exception =
8476 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8477 no_class_def_found_error =
8478 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8479 load_class (class_not_found_exception, 1);
8480 load_class (no_class_def_found_error, 1);
8481
8482 /* Create the "class$" function */
8483 mdecl = create_artificial_method (class, ACC_STATIC,
8484 build_pointer_type (class_type_node),
8485 get_identifier ("class$"), args);
8486 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8487 no_class_def_found_error);
8488
8489 /* We start by building the try block. We need to build:
8490 return (java.lang.Class.forName (type)); */
8491 stmt = build_method_invocation (tmp,
8492 build_tree_list (NULL_TREE, type_parm_wfl));
8493 stmt = build_return (0, stmt);
8494 /* Put it in a block. That's the try block */
8495 try_block = build_expr_block (stmt, NULL_TREE);
8496
8497 /* Now onto the catch block. We start by building the expression
8498 throwing a new exception:
8499 throw new NoClassDefFoundError (_.getMessage); */
8500 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8501 get_message_wfl, 0);
8502 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8503
8504 /* Build new NoClassDefFoundError (_.getMessage) */
8505 throw_stmt = build_new_invocation
8506 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8507 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8508
8509 /* Build the throw, (it's too early to use BUILD_THROW) */
8510 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8511
8512 /* Build the catch block to encapsulate all this. We begin by
8513 building an decl for the catch clause parameter and link it to
8514 newly created block, the catch block. */
8515 catch_clause_param =
8516 build_decl (VAR_DECL, wpv_id,
8517 build_pointer_type (class_not_found_exception));
8518 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8519
8520 /* We initialize the variable with the exception handler. */
8521 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8522 soft_exceptioninfo_call_node);
8523 add_stmt_to_block (catch_block, NULL_TREE, catch);
8524
8525 /* We add the statement throwing the new exception */
8526 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8527
8528 /* Build a catch expression for all this */
8529 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8530
8531 /* Build the try/catch sequence */
8532 stmt = build_try_statement (0, try_block, catch_block);
8533
8534 fix_method_argument_names (args, mdecl);
8535 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8536 saved_current_function_decl = current_function_decl;
8537 start_artificial_method_body (mdecl);
8538 java_method_add_stmt (mdecl, stmt);
8539 end_artificial_method_body (mdecl);
8540 current_function_decl = saved_current_function_decl;
8541 TYPE_DOT_CLASS (class) = mdecl;
8542
8543 return mdecl;
8544}
8545
8546static tree
f0f3a777
APB
8547build_dot_class_method_invocation (type)
8548 tree type;
165f37bc 8549{
f0f3a777
APB
8550 tree sig_id, s;
8551
8552 if (TYPE_ARRAY_P (type))
8553 sig_id = build_java_signature (type);
8554 else
8555 sig_id = DECL_NAME (TYPE_NAME (type));
8556
1f8f4a0b
MM
8557 s = build_string (IDENTIFIER_LENGTH (sig_id),
8558 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8559 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8560 build_tree_list (NULL_TREE, s));
8561}
8562
c2952b01
APB
8563/* This section of the code deals with constructor. */
8564
22eed1e6
APB
8565/* Craft a body for default constructor. Patch existing constructor
8566 bodies with call to super() and field initialization statements if
8567 necessary. */
8568
8569static void
8570fix_constructors (mdecl)
8571 tree mdecl;
8572{
8573 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8574 tree thisn_assign, compound = NULL_TREE;
8575 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8576
22eed1e6
APB
8577 if (!body)
8578 {
22eed1e6
APB
8579 /* It is an error for the compiler to generate a default
8580 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8581 takes no argument, or the same args for an anonymous class */
8582 if (verify_constructor_super (mdecl))
22eed1e6 8583 {
c2952b01
APB
8584 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8585 tree save = DECL_NAME (mdecl);
49f48c71 8586 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8587 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8588 parse_error_context
c2952b01
APB
8589 (lookup_cl (TYPE_NAME (class_type)),
8590 "No constructor matching `%s' found in class `%s'",
8591 lang_printable_name (mdecl, 0), n);
8592 DECL_NAME (mdecl) = save;
22eed1e6
APB
8593 }
8594
c2952b01
APB
8595 /* The constructor body must be crafted by hand. It's the
8596 constructor we defined when we realize we didn't have the
8597 CLASSNAME() constructor */
22eed1e6
APB
8598 start_artificial_method_body (mdecl);
8599
f0f3a777
APB
8600 /* Insert an assignment to the this$<n> hidden field, if
8601 necessary */
8602 if ((thisn_assign = build_thisn_assign ()))
8603 java_method_add_stmt (mdecl, thisn_assign);
8604
22eed1e6
APB
8605 /* We don't generate a super constructor invocation if we're
8606 compiling java.lang.Object. build_super_invocation takes care
8607 of that. */
e920ebc9 8608 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8609
c2952b01
APB
8610 /* Insert the instance initializer block right here, after the
8611 super invocation. */
8612 add_instance_initializer (mdecl);
8613
22eed1e6
APB
8614 end_artificial_method_body (mdecl);
8615 }
8616 /* Search for an explicit constructor invocation */
8617 else
8618 {
8619 int found = 0;
8620 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8621
8622 while (body)
8623 switch (TREE_CODE (body))
8624 {
8625 case CALL_EXPR:
8626 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8627 body = NULL_TREE;
8628 break;
8629 case COMPOUND_EXPR:
8630 case EXPR_WITH_FILE_LOCATION:
8631 body = TREE_OPERAND (body, 0);
8632 break;
8633 case BLOCK:
8634 body = BLOCK_EXPR_BODY (body);
8635 break;
8636 default:
8637 found = 0;
8638 body = NULL_TREE;
8639 }
8640 /* The constructor is missing an invocation of super() */
8641 if (!found)
8642 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8643 build_super_invocation (mdecl));
22eed1e6 8644
c2952b01
APB
8645 /* Generate the assignment to this$<n>, if necessary */
8646 if ((thisn_assign = build_thisn_assign ()))
8647 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8648
f0f3a777
APB
8649 /* Insert the instance initializer block right here, after the
8650 super invocation. */
8651 add_instance_initializer (mdecl);
8652
22eed1e6
APB
8653 /* Fix the constructor main block if we're adding extra stmts */
8654 if (compound)
8655 {
8656 compound = add_stmt_to_compound (compound, NULL_TREE,
8657 BLOCK_EXPR_BODY (main_block));
8658 BLOCK_EXPR_BODY (main_block) = compound;
8659 }
8660 }
8661}
8662
8663/* Browse constructors in the super class, searching for a constructor
8664 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8665 otherwise. If the current class is an anonymous inner class, look
8666 for something that has the same signature. */
22eed1e6
APB
8667
8668static int
c2952b01
APB
8669verify_constructor_super (mdecl)
8670 tree mdecl;
22eed1e6
APB
8671{
8672 tree class = CLASSTYPE_SUPER (current_class);
152de068 8673 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8674 tree sdecl;
8675
22eed1e6
APB
8676 if (!class)
8677 return 0;
8678
c2952b01 8679 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8680 {
c2952b01
APB
8681 tree mdecl_arg_type;
8682 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8683 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8684 if (DECL_CONSTRUCTOR_P (sdecl))
8685 {
cf1b2274 8686 tree m_arg_type;
152de068
APB
8687 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8688 if (super_inner)
8689 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8690 for (m_arg_type = mdecl_arg_type;
8691 (arg_type != end_params_node
8692 && m_arg_type != end_params_node);
c2952b01 8693 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8694 m_arg_type = TREE_CHAIN (m_arg_type))
8695 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8696 break;
8697
cf1b2274 8698 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8699 return 0;
8700 }
8701 }
8702 else
8703 {
8704 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8705 {
152de068
APB
8706 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8707 if (super_inner)
8708 arg = TREE_CHAIN (arg);
8709 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8710 return 0;
8711 }
8712 }
8713 return 1;
8714}
8715
22eed1e6 8716/* Generate code for all context remembered for code generation. */
b351b287
APB
8717
8718void
8719java_expand_classes ()
8720{
5423609c 8721 int save_error_count = 0;
c2952b01
APB
8722 static struct parser_ctxt *saved_ctxp = NULL;
8723
23a79c61
APB
8724 java_parse_abort_on_error ();
8725 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8726 return;
8727 java_layout_classes ();
8728 java_parse_abort_on_error ();
8729
c2952b01 8730 saved_ctxp = ctxp_for_generation;
b351b287
APB
8731 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8732 {
8733 ctxp = ctxp_for_generation;
8734 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8735 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8736 java_parse_abort_on_error ();
8737 }
c2952b01
APB
8738
8739 /* Find anonymous classes and expand their constructor, now they
8740 have been fixed. */
8741 for (ctxp_for_generation = saved_ctxp;
8742 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8743 {
8744 tree current;
8745 ctxp = ctxp_for_generation;
8746 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8747 {
8748 current_class = TREE_TYPE (current);
8749 if (ANONYMOUS_CLASS_P (current_class))
8750 {
8751 tree d;
8752 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8753 {
8754 if (DECL_CONSTRUCTOR_P (d))
8755 {
8756 restore_line_number_status (1);
8757 reset_method_name (d);
8758 java_complete_expand_method (d);
8759 restore_line_number_status (0);
8760 break; /* We now there are no other ones */
8761 }
8762 }
8763 }
8764 }
8765 }
8766
8767 /* If we've found error at that stage, don't try to generate
8768 anything, unless we're emitting xrefs or checking the syntax only
8769 (but not using -fsyntax-only for the purpose of generating
8770 bytecode. */
8771 if (java_error_count && !flag_emit_xref
8772 && (!flag_syntax_only && !flag_emit_class_files))
8773 return;
8774
8775 /* Now things are stable, go for generation of the class data. */
8776 for (ctxp_for_generation = saved_ctxp;
8777 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8778 {
8779 tree current;
8780 ctxp = ctxp_for_generation;
8781 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8782 {
8783 current_class = TREE_TYPE (current);
8784 outgoing_cpool = TYPE_CPOOL (current_class);
8785 if (flag_emit_class_files)
8786 write_classfile (current_class);
8787 if (flag_emit_xref)
8788 expand_xref (current_class);
8789 else if (! flag_syntax_only)
8790 finish_class ();
8791 }
8792 }
b351b287
APB
8793}
8794
e04a16fb
AG
8795/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8796 a tree list node containing RIGHT. Fore coming RIGHTs will be
8797 chained to this hook. LOCATION contains the location of the
8798 separating `.' operator. */
8799
8800static tree
8801make_qualified_primary (primary, right, location)
8802 tree primary, right;
8803 int location;
8804{
8805 tree wfl;
8806
c2952b01 8807 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8808 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8809 else
8810 {
8811 wfl = primary;
c2952b01
APB
8812 /* If wfl wasn't qualified, we build a first anchor */
8813 if (!EXPR_WFL_QUALIFICATION (wfl))
8814 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8815 }
8816
c2952b01 8817 /* And chain them */
e04a16fb
AG
8818 EXPR_WFL_LINECOL (right) = location;
8819 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8820 PRIMARY_P (wfl) = 1;
8821 return wfl;
8822}
8823
8824/* Simple merge of two name separated by a `.' */
8825
8826static tree
8827merge_qualified_name (left, right)
8828 tree left, right;
8829{
8830 tree node;
c2952b01
APB
8831 if (!left && !right)
8832 return NULL_TREE;
8833
8834 if (!left)
8835 return right;
8836
8837 if (!right)
8838 return left;
8839
e04a16fb
AG
8840 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8841 IDENTIFIER_LENGTH (left));
8842 obstack_1grow (&temporary_obstack, '.');
8843 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8844 IDENTIFIER_LENGTH (right));
8845 node = get_identifier (obstack_base (&temporary_obstack));
8846 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8847 QUALIFIED_P (node) = 1;
8848 return node;
8849}
8850
8851/* Merge the two parts of a qualified name into LEFT. Set the
8852 location information of the resulting node to LOCATION, usually
8853 inherited from the location information of the `.' operator. */
8854
8855static tree
8856make_qualified_name (left, right, location)
8857 tree left, right;
8858 int location;
8859{
bc3ca41b
PB
8860#ifdef USE_COMPONENT_REF
8861 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8862 EXPR_WFL_LINECOL (node) = location;
8863 return node;
8864#else
e04a16fb
AG
8865 tree left_id = EXPR_WFL_NODE (left);
8866 tree right_id = EXPR_WFL_NODE (right);
8867 tree wfl, merge;
8868
8869 merge = merge_qualified_name (left_id, right_id);
8870
8871 /* Left wasn't qualified and is now qualified */
8872 if (!QUALIFIED_P (left_id))
8873 {
8874 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8875 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8876 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8877 }
8878
8879 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8880 EXPR_WFL_LINECOL (wfl) = location;
8881 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8882
8883 EXPR_WFL_NODE (left) = merge;
8884 return left;
bc3ca41b 8885#endif
e04a16fb
AG
8886}
8887
8888/* Extract the last identifier component of the qualified in WFL. The
8889 last identifier is removed from the linked list */
8890
8891static tree
8892cut_identifier_in_qualified (wfl)
8893 tree wfl;
8894{
8895 tree q;
8896 tree previous = NULL_TREE;
8897 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8898 if (!TREE_CHAIN (q))
8899 {
8900 if (!previous)
781b0558 8901 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8902 TREE_CHAIN (previous) = NULL_TREE;
8903 return TREE_PURPOSE (q);
8904 }
8905}
8906
8907/* Resolve the expression name NAME. Return its decl. */
8908
8909static tree
5e942c50 8910resolve_expression_name (id, orig)
e04a16fb 8911 tree id;
5e942c50 8912 tree *orig;
e04a16fb
AG
8913{
8914 tree name = EXPR_WFL_NODE (id);
8915 tree decl;
8916
8917 /* 6.5.5.1: Simple expression names */
8918 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8919 {
8920 /* 15.13.1: NAME can appear within the scope of a local variable
8921 declaration */
8922 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8923 return decl;
8924
8925 /* 15.13.1: NAME can appear within a class declaration */
8926 else
8927 {
8928 decl = lookup_field_wrapper (current_class, name);
8929 if (decl)
8930 {
c2952b01 8931 tree access = NULL_TREE;
e04a16fb 8932 int fs = FIELD_STATIC (decl);
f2760b27
APB
8933
8934 /* If we're accessing an outer scope local alias, make
8935 sure we change the name of the field we're going to
8936 build access to. */
8937 if (FIELD_LOCAL_ALIAS_USED (decl))
8938 name = DECL_NAME (decl);
8939
e04a16fb
AG
8940 /* Instance variable (8.3.1.1) can't appear within
8941 static method, static initializer or initializer for
8942 a static variable. */
8943 if (!fs && METHOD_STATIC (current_function_decl))
8944 {
7f10c2e2 8945 static_ref_err (id, name, current_class);
e04a16fb
AG
8946 return error_mark_node;
8947 }
22eed1e6
APB
8948 /* Instance variables can't appear as an argument of
8949 an explicit constructor invocation */
8950 if (!fs && ctxp->explicit_constructor_p)
8951 {
8952 parse_error_context
781b0558 8953 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8954 return error_mark_node;
8955 }
5e942c50 8956
c2952b01
APB
8957 /* If we're processing an inner class and we're trying
8958 to access a field belonging to an outer class, build
8959 the access to the field */
8960 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8961 {
8962 if (CLASS_STATIC (TYPE_NAME (current_class)))
8963 {
8964 static_ref_err (id, DECL_NAME (decl), current_class);
8965 return error_mark_node;
8966 }
8967 return build_outer_field_access (id, decl);
8968 }
c2952b01 8969
5e942c50 8970 /* Otherwise build what it takes to access the field */
c2952b01
APB
8971 access = build_field_ref ((fs ? NULL_TREE : current_this),
8972 DECL_CONTEXT (decl), name);
e8fc7396 8973 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8974 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8975 /* We may be asked to save the real field access node */
8976 if (orig)
c2952b01 8977 *orig = access;
5e942c50 8978 /* And we return what we got */
c2952b01 8979 return access;
e04a16fb
AG
8980 }
8981 /* Fall down to error report on undefined variable */
8982 }
8983 }
8984 /* 6.5.5.2 Qualified Expression Names */
8985 else
8986 {
5e942c50
APB
8987 if (orig)
8988 *orig = NULL_TREE;
e04a16fb
AG
8989 qualify_ambiguous_name (id);
8990 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8991 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8992 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8993 }
8994
8995 /* We've got an error here */
8996 parse_error_context (id, "Undefined variable `%s'",
8997 IDENTIFIER_POINTER (name));
8998
8999 return error_mark_node;
9000}
9001
7f10c2e2
APB
9002static void
9003static_ref_err (wfl, field_id, class_type)
9004 tree wfl, field_id, class_type;
9005{
9006 parse_error_context
9007 (wfl,
9008 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9009 IDENTIFIER_POINTER (field_id),
9010 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9011}
9012
e04a16fb
AG
9013/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
9014 We return something suitable to generate the field access. We also
9015 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9016 recipient's address can be null. */
9017
9018static tree
9019resolve_field_access (qual_wfl, field_decl, field_type)
9020 tree qual_wfl;
9021 tree *field_decl, *field_type;
9022{
9023 int is_static = 0;
9024 tree field_ref;
9025 tree decl, where_found, type_found;
9026
9027 if (resolve_qualified_expression_name (qual_wfl, &decl,
9028 &where_found, &type_found))
9029 return error_mark_node;
9030
9031 /* Resolve the LENGTH field of an array here */
9a7ab4b3 9032 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 9033 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 9034 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
9035 {
9036 tree length = build_java_array_length_access (where_found);
9037 field_ref =
9038 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
9039
9040 /* In case we're dealing with a static array, we need to
9041 initialize its class before the array length can be fetched.
9042 It's also a good time to create a DECL_RTL for the field if
9043 none already exists, otherwise if the field was declared in a
9044 class found in an external file and hasn't been (and won't
9045 be) accessed for its value, none will be created. */
9046 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9047 {
9048 build_static_field_ref (where_found);
9049 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9050 }
e04a16fb
AG
9051 }
9052 /* We might have been trying to resolve field.method(). In which
9053 case, the resolution is over and decl is the answer */
34f4db93 9054 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 9055 field_ref = decl;
34f4db93 9056 else if (JDECL_P (decl))
e04a16fb 9057 {
5e942c50
APB
9058 int static_final_found = 0;
9059 if (!type_found)
9060 type_found = DECL_CONTEXT (decl);
34f4db93 9061 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 9062 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 9063 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 9064 && DECL_INITIAL (decl))
5e942c50 9065 {
0c2b8145
APB
9066 /* When called on a FIELD_DECL of the right (primitive)
9067 type, java_complete_tree will try to substitue the decl
9068 for it's initial value. */
70541f45 9069 field_ref = java_complete_tree (decl);
5e942c50
APB
9070 static_final_found = 1;
9071 }
9072 else
7f10c2e2
APB
9073 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9074 NULL_TREE : where_found),
5e942c50 9075 type_found, DECL_NAME (decl));
e04a16fb
AG
9076 if (field_ref == error_mark_node)
9077 return error_mark_node;
e8fc7396
APB
9078 if (is_static && !static_final_found
9079 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 9080 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
9081 }
9082 else
9083 field_ref = decl;
9084
9085 if (field_decl)
9086 *field_decl = decl;
9087 if (field_type)
c877974e
APB
9088 *field_type = (QUAL_DECL_TYPE (decl) ?
9089 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
9090 return field_ref;
9091}
9092
e28cd97b
APB
9093/* If NODE is an access to f static field, strip out the class
9094 initialization part and return the field decl, otherwise, return
9095 NODE. */
9096
9097static tree
9098strip_out_static_field_access_decl (node)
9099 tree node;
9100{
9101 if (TREE_CODE (node) == COMPOUND_EXPR)
9102 {
9103 tree op1 = TREE_OPERAND (node, 1);
9104 if (TREE_CODE (op1) == COMPOUND_EXPR)
9105 {
9106 tree call = TREE_OPERAND (op1, 0);
9107 if (TREE_CODE (call) == CALL_EXPR
9108 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9109 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9110 == soft_initclass_node)
9111 return TREE_OPERAND (op1, 1);
9112 }
2f11d407
TT
9113 else if (JDECL_P (op1))
9114 return op1;
e28cd97b
APB
9115 }
9116 return node;
9117}
9118
e04a16fb
AG
9119/* 6.5.5.2: Qualified Expression Names */
9120
9121static int
9122resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9123 tree wfl;
9124 tree *found_decl, *type_found, *where_found;
9125{
9126 int from_type = 0; /* Field search initiated from a type */
c2952b01 9127 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9128 int previous_call_static = 0;
9129 int is_static;
9130 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9131 /* For certain for of inner class instantiation */
9132 tree saved_current, saved_this;
9133#define RESTORE_THIS_AND_CURRENT_CLASS \
9134 { current_class = saved_current; current_this = saved_this;}
9135
c877974e 9136 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9137
9138 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9139 {
9140 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9141 tree ret_decl; /* for EH checking */
9142 int location; /* for EH checking */
e04a16fb
AG
9143
9144 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9145 switch (TREE_CODE (qual_wfl))
9146 {
9147 case CALL_EXPR:
b67d701b 9148 case NEW_CLASS_EXPR:
e04a16fb
AG
9149 /* If the access to the function call is a non static field,
9150 build the code to access it. */
34f4db93 9151 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9152 {
ac825856
APB
9153 decl = maybe_access_field (decl, *where_found,
9154 DECL_CONTEXT (decl));
e04a16fb
AG
9155 if (decl == error_mark_node)
9156 return 1;
9157 }
c2952b01 9158
e04a16fb
AG
9159 /* And code for the function call */
9160 if (complete_function_arguments (qual_wfl))
9161 return 1;
c2952b01
APB
9162
9163 /* We might have to setup a new current class and a new this
9164 for the search of an inner class, relative to the type of
9165 a expression resolved as `decl'. The current values are
9166 saved and restored shortly after */
9167 saved_current = current_class;
9168 saved_this = current_this;
dba41d30
APB
9169 if (decl
9170 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9171 || from_qualified_this))
c2952b01 9172 {
dba41d30
APB
9173 /* If we still have `from_qualified_this', we have the form
9174 <T>.this.f() and we need to build <T>.this */
9175 if (from_qualified_this)
9176 {
9177 decl = build_access_to_thisn (current_class, type, 0);
9178 decl = java_complete_tree (decl);
9179 type = TREE_TYPE (TREE_TYPE (decl));
9180 }
c2952b01
APB
9181 current_class = type;
9182 current_this = decl;
dba41d30 9183 from_qualified_this = 0;
c2952b01
APB
9184 }
9185
89e09b9a
PB
9186 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9187 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9188 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9189 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9190 *where_found = patch_method_invocation (qual_wfl, decl, type,
9191 &is_static, &ret_decl);
e04a16fb 9192 if (*where_found == error_mark_node)
c2952b01
APB
9193 {
9194 RESTORE_THIS_AND_CURRENT_CLASS;
9195 return 1;
9196 }
e04a16fb
AG
9197 *type_found = type = QUAL_DECL_TYPE (*where_found);
9198
c2952b01
APB
9199 /* If we're creating an inner class instance, check for that
9200 an enclosing instance is in scope */
9201 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9202 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9203 {
9204 parse_error_context
165f37bc
APB
9205 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9206 lang_printable_name (type, 0),
9207 (!current_this ? "" :
9208 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9209 RESTORE_THIS_AND_CURRENT_CLASS;
9210 return 1;
9211 }
9212
9213 /* In case we had to change then to resolve a inner class
9214 instantiation using a primary qualified by a `new' */
9215 RESTORE_THIS_AND_CURRENT_CLASS;
9216
34d4df06
APB
9217 /* EH check. No check on access$<n> functions */
9218 if (location
9219 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9220 (DECL_NAME (current_function_decl)))
7705e9db
APB
9221 check_thrown_exceptions (location, ret_decl);
9222
e04a16fb
AG
9223 /* If the previous call was static and this one is too,
9224 build a compound expression to hold the two (because in
9225 that case, previous function calls aren't transported as
9226 forcoming function's argument. */
9227 if (previous_call_static && is_static)
9228 {
9229 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9230 TREE_SIDE_EFFECTS (decl) = 1;
9231 }
9232 else
9233 {
9234 previous_call_static = is_static;
9235 decl = *where_found;
9236 }
c2952b01 9237 from_type = 0;
e04a16fb
AG
9238 continue;
9239
d8fccff5 9240 case NEW_ARRAY_EXPR:
c2952b01 9241 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9242 *where_found = decl = java_complete_tree (qual_wfl);
9243 if (decl == error_mark_node)
9244 return 1;
9245 *type_found = type = QUAL_DECL_TYPE (decl);
9246 CLASS_LOADED_P (type) = 1;
9247 continue;
9248
e04a16fb
AG
9249 case CONVERT_EXPR:
9250 *where_found = decl = java_complete_tree (qual_wfl);
9251 if (decl == error_mark_node)
9252 return 1;
9253 *type_found = type = QUAL_DECL_TYPE (decl);
9254 from_cast = 1;
9255 continue;
9256
22eed1e6 9257 case CONDITIONAL_EXPR:
5e942c50 9258 case STRING_CST:
ac22f9cb 9259 case MODIFY_EXPR:
22eed1e6
APB
9260 *where_found = decl = java_complete_tree (qual_wfl);
9261 if (decl == error_mark_node)
9262 return 1;
9263 *type_found = type = QUAL_DECL_TYPE (decl);
9264 continue;
9265
e04a16fb
AG
9266 case ARRAY_REF:
9267 /* If the access to the function call is a non static field,
9268 build the code to access it. */
34f4db93 9269 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9270 {
9271 decl = maybe_access_field (decl, *where_found, type);
9272 if (decl == error_mark_node)
9273 return 1;
9274 }
9275 /* And code for the array reference expression */
9276 decl = java_complete_tree (qual_wfl);
9277 if (decl == error_mark_node)
9278 return 1;
9279 type = QUAL_DECL_TYPE (decl);
9280 continue;
0a2138e2 9281
37feda7d
APB
9282 case PLUS_EXPR:
9283 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9284 return 1;
9285 if ((type = patch_string (decl)))
9286 decl = type;
9287 *where_found = QUAL_RESOLUTION (q) = decl;
9288 *type_found = type = TREE_TYPE (decl);
9289 break;
9290
165f37bc
APB
9291 case CLASS_LITERAL:
9292 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9293 return 1;
9294 *where_found = QUAL_RESOLUTION (q) = decl;
9295 *type_found = type = TREE_TYPE (decl);
9296 break;
9297
0a2138e2
APB
9298 default:
9299 /* Fix for -Wall Just go to the next statement. Don't
9300 continue */
a3f406ce 9301 break;
e04a16fb
AG
9302 }
9303
9304 /* If we fall here, we weren't processing a (static) function call. */
9305 previous_call_static = 0;
9306
9307 /* It can be the keyword THIS */
9308 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9309 {
9310 if (!current_this)
9311 {
9312 parse_error_context
9313 (wfl, "Keyword `this' used outside allowed context");
9314 return 1;
9315 }
f63991a8
APB
9316 if (ctxp->explicit_constructor_p)
9317 {
781b0558 9318 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9319 return 1;
9320 }
e04a16fb 9321 /* We have to generate code for intermediate acess */
c2952b01
APB
9322 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9323 {
9324 *where_found = decl = current_this;
9325 *type_found = type = QUAL_DECL_TYPE (decl);
9326 }
4dbf4496
APB
9327 /* We're trying to access the this from somewhere else. Make sure
9328 it's allowed before doing so. */
c2952b01
APB
9329 else
9330 {
4dbf4496
APB
9331 if (!enclosing_context_p (type, current_class))
9332 {
9333 char *p = xstrdup (lang_printable_name (type, 0));
9334 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9335 p, p,
9336 lang_printable_name (current_class, 0));
9337 free (p);
9338 return 1;
9339 }
c2952b01 9340 from_qualified_this = 1;
dba41d30
APB
9341 /* If there's nothing else after that, we need to
9342 produce something now, otherwise, the section of the
9343 code that needs to produce <T>.this will generate
9344 what is necessary. */
9345 if (!TREE_CHAIN (q))
9346 {
9347 decl = build_access_to_thisn (current_class, type, 0);
9348 *where_found = decl = java_complete_tree (decl);
9349 *type_found = type = TREE_TYPE (decl);
9350 }
c2952b01
APB
9351 }
9352
9353 from_type = 0;
e04a16fb
AG
9354 continue;
9355 }
9356
9357 /* 15.10.2 Accessing Superclass Members using SUPER */
9358 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9359 {
9360 tree node;
9361 /* Check on the restricted use of SUPER */
9362 if (METHOD_STATIC (current_function_decl)
9363 || current_class == object_type_node)
9364 {
9365 parse_error_context
9366 (wfl, "Keyword `super' used outside allowed context");
9367 return 1;
9368 }
9369 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9370 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9371 CLASSTYPE_SUPER (current_class),
9372 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9373 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9374 if (decl == error_mark_node)
9375 return 1;
e04a16fb
AG
9376 *type_found = type = QUAL_DECL_TYPE (decl);
9377 from_super = from_type = 1;
9378 continue;
9379 }
9380
9381 /* 15.13.1: Can't search for field name in packages, so we
9382 assume a variable/class name was meant. */
9383 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9384 {
5e942c50
APB
9385 tree name = resolve_package (wfl, &q);
9386 if (name)
9387 {
c2952b01 9388 tree list;
5e942c50 9389 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9390 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9391 out. We're going to search something inside it. */
9392 *type_found = type = TREE_TYPE (decl);
9393 layout_class (type);
9394 from_type = 1;
c2952b01 9395
dde1da72
APB
9396 /* Fix them all the way down, if any are left. */
9397 if (q)
c2952b01 9398 {
dde1da72
APB
9399 list = TREE_CHAIN (q);
9400 while (list)
9401 {
9402 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9403 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9404 list = TREE_CHAIN (list);
9405 }
c2952b01 9406 }
5e942c50 9407 }
e04a16fb 9408 else
5e942c50
APB
9409 {
9410 if (from_super || from_cast)
9411 parse_error_context
9412 ((from_cast ? qual_wfl : wfl),
9413 "No variable `%s' defined in class `%s'",
9414 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9415 lang_printable_name (type, 0));
9416 else
9417 parse_error_context
9418 (qual_wfl, "Undefined variable or class name: `%s'",
9419 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9420 return 1;
9421 }
e04a16fb
AG
9422 }
9423
9424 /* We have a type name. It's been already resolved when the
9425 expression was qualified. */
9426 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9427 {
9428 if (!(decl = QUAL_RESOLUTION (q)))
9429 return 1; /* Error reported already */
9430
c2952b01
APB
9431 /* Sneak preview. If next we see a `new', we're facing a
9432 qualification with resulted in a type being selected
9433 instead of a field. Report the error */
9434 if(TREE_CHAIN (q)
9435 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9436 {
9437 parse_error_context (qual_wfl, "Undefined variable `%s'",
9438 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9439 return 1;
9440 }
9441
e04a16fb
AG
9442 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9443 {
9444 parse_error_context
9445 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9446 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9447 GET_TYPE_NAME (type),
e04a16fb
AG
9448 IDENTIFIER_POINTER (DECL_NAME (decl)),
9449 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9450 return 1;
9451 }
5e942c50 9452 check_deprecation (qual_wfl, decl);
c2952b01 9453
e04a16fb
AG
9454 type = TREE_TYPE (decl);
9455 from_type = 1;
9456 }
9457 /* We resolve and expression name */
9458 else
9459 {
cd531a2e 9460 tree field_decl = NULL_TREE;
e04a16fb
AG
9461
9462 /* If there exists an early resolution, use it. That occurs
9463 only once and we know that there are more things to
9464 come. Don't do that when processing something after SUPER
9465 (we need more thing to be put in place below */
9466 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9467 {
9468 decl = QUAL_RESOLUTION (q);
c877974e 9469 if (!type)
5e942c50 9470 {
7f10c2e2
APB
9471 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9472 {
9473 if (current_this)
9474 *where_found = current_this;
9475 else
9476 {
9477 static_ref_err (qual_wfl, DECL_NAME (decl),
9478 current_class);
9479 return 1;
9480 }
f0f3a777
APB
9481 if (outer_field_access_p (current_class, decl))
9482 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9483 }
c877974e
APB
9484 else
9485 {
9486 *where_found = TREE_TYPE (decl);
9487 if (TREE_CODE (*where_found) == POINTER_TYPE)
9488 *where_found = TREE_TYPE (*where_found);
9489 }
5e942c50 9490 }
b67d701b 9491 }
e04a16fb
AG
9492
9493 /* We have to search for a field, knowing the type of its
9494 container. The flag FROM_TYPE indicates that we resolved
9495 the last member of the expression as a type name, which
5e942c50
APB
9496 means that for the resolution of this field, we'll look
9497 for other errors than if it was resolved as a member of
9498 an other field. */
e04a16fb
AG
9499 else
9500 {
9501 int is_static;
5e942c50
APB
9502 tree field_decl_type; /* For layout */
9503
e04a16fb
AG
9504 if (!from_type && !JREFERENCE_TYPE_P (type))
9505 {
9506 parse_error_context
9507 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9508 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9509 lang_printable_name (type, 0),
e04a16fb
AG
9510 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9511 return 1;
9512 }
9513
dc0b3eff
PB
9514 field_decl = lookup_field_wrapper (type,
9515 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9516
9517 /* Maybe what we're trying to access an inner class. */
9518 if (!field_decl)
9519 {
9520 tree ptr, inner_decl;
9521
9522 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9523 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9524 if (inner_decl)
9525 {
9526 check_inner_class_access (inner_decl, decl, qual_wfl);
9527 type = TREE_TYPE (inner_decl);
9528 decl = inner_decl;
9529 from_type = 1;
9530 continue;
9531 }
9532 }
9533
dc0b3eff 9534 if (field_decl == NULL_TREE)
e04a16fb
AG
9535 {
9536 parse_error_context
2aa11e97 9537 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9538 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9539 GET_TYPE_NAME (type));
e04a16fb
AG
9540 return 1;
9541 }
dc0b3eff
PB
9542 if (field_decl == error_mark_node)
9543 return 1;
5e942c50
APB
9544
9545 /* Layout the type of field_decl, since we may need
c877974e
APB
9546 it. Don't do primitive types or loaded classes. The
9547 situation of non primitive arrays may not handled
9548 properly here. FIXME */
5e942c50
APB
9549 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9550 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9551 else
9552 field_decl_type = TREE_TYPE (field_decl);
9553 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9554 && !CLASS_LOADED_P (field_decl_type)
9555 && !TYPE_ARRAY_P (field_decl_type))
9556 resolve_and_layout (field_decl_type, NULL_TREE);
9557 if (TYPE_ARRAY_P (field_decl_type))
9558 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9559
9560 /* Check on accessibility here */
9561 if (not_accessible_p (type, field_decl, from_super))
9562 {
9563 parse_error_context
9564 (qual_wfl,
9565 "Can't access %s field `%s.%s' from `%s'",
9566 java_accstring_lookup
9567 (get_access_flags_from_decl (field_decl)),
2aa11e97 9568 GET_TYPE_NAME (type),
e04a16fb
AG
9569 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9570 IDENTIFIER_POINTER
9571 (DECL_NAME (TYPE_NAME (current_class))));
9572 return 1;
9573 }
5e942c50 9574 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9575
9576 /* There are things to check when fields are accessed
9577 from type. There are no restrictions on a static
9578 declaration of the field when it is accessed from an
9579 interface */
9580 is_static = FIELD_STATIC (field_decl);
9581 if (!from_super && from_type
c2952b01
APB
9582 && !TYPE_INTERFACE_P (type)
9583 && !is_static
9584 && (current_function_decl
9585 && METHOD_STATIC (current_function_decl)))
e04a16fb 9586 {
7f10c2e2 9587 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9588 return 1;
9589 }
9590 from_cast = from_super = 0;
9591
c2952b01
APB
9592 /* It's an access from a type but it isn't static, we
9593 make it relative to `this'. */
9594 if (!is_static && from_type)
9595 decl = current_this;
9596
5e942c50
APB
9597 /* If we need to generate something to get a proper
9598 handle on what this field is accessed from, do it
9599 now. */
e04a16fb
AG
9600 if (!is_static)
9601 {
c583dd46 9602 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9603 if (decl == error_mark_node)
9604 return 1;
9605 }
9606
9607 /* We want to keep the location were found it, and the type
9608 we found. */
9609 *where_found = decl;
9610 *type_found = type;
9611
c2952b01
APB
9612 /* Generate the correct expression for field access from
9613 qualified this */
9614 if (from_qualified_this)
9615 {
9616 field_decl = build_outer_field_access (qual_wfl, field_decl);
9617 from_qualified_this = 0;
9618 }
9619
e04a16fb
AG
9620 /* This is the decl found and eventually the next one to
9621 search from */
9622 decl = field_decl;
9623 }
e04a16fb
AG
9624 from_type = 0;
9625 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9626
9627 /* Sneak preview. If decl is qualified by a `new', report
9628 the error here to be accurate on the peculiar construct */
9629 if (TREE_CHAIN (q)
9630 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9631 && !JREFERENCE_TYPE_P (type))
9632 {
9633 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9634 lang_printable_name (type, 0));
9635 return 1;
9636 }
e04a16fb 9637 }
dde1da72
APB
9638 /* `q' might have changed due to a after package resolution
9639 re-qualification */
9640 if (!q)
9641 break;
e04a16fb
AG
9642 }
9643 *found_decl = decl;
9644 return 0;
9645}
9646
9647/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9648 can't be accessed from REFERENCE (a record type). This should be
9649 used when decl is a field or a method.*/
e04a16fb 9650
be245ac0
KG
9651static int
9652not_accessible_p (reference, member, from_super)
e04a16fb
AG
9653 tree reference, member;
9654 int from_super;
9655{
9656 int access_flag = get_access_flags_from_decl (member);
9657
4dbf4496
APB
9658 /* Inner classes are processed by check_inner_class_access */
9659 if (INNER_CLASS_TYPE_P (reference))
9660 return 0;
9661
e04a16fb
AG
9662 /* Access always granted for members declared public */
9663 if (access_flag & ACC_PUBLIC)
9664 return 0;
9665
9666 /* Check access on protected members */
9667 if (access_flag & ACC_PROTECTED)
9668 {
9669 /* Access granted if it occurs from within the package
9670 containing the class in which the protected member is
9671 declared */
9672 if (class_in_current_package (DECL_CONTEXT (member)))
9673 return 0;
9674
9bbc7d9f
PB
9675 /* If accessed with the form `super.member', then access is granted */
9676 if (from_super)
9677 return 0;
e04a16fb 9678
9bbc7d9f 9679 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9680 member is declared or a subclass of it. Find the right
9681 context to perform the check */
9682 if (PURE_INNER_CLASS_TYPE_P (reference))
9683 {
9684 while (INNER_CLASS_TYPE_P (reference))
9685 {
9686 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9687 return 0;
9688 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9689 }
9690 }
473e7b07 9691 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9692 return 0;
e04a16fb
AG
9693 return 1;
9694 }
9695
9696 /* Check access on private members. Access is granted only if it
473e7b07 9697 occurs from within the class in which it is declared. Exceptions
4dbf4496 9698 are accesses from inner-classes. */
e04a16fb 9699 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9700 return (current_class == DECL_CONTEXT (member) ? 0 :
9701 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9702
9703 /* Default access are permitted only when occuring within the
9704 package in which the type (REFERENCE) is declared. In other words,
9705 REFERENCE is defined in the current package */
9706 if (ctxp->package)
9707 return !class_in_current_package (reference);
473e7b07 9708
e04a16fb
AG
9709 /* Otherwise, access is granted */
9710 return 0;
9711}
9712
5e942c50
APB
9713/* Test deprecated decl access. */
9714static void
9715check_deprecation (wfl, decl)
9716 tree wfl, decl;
9717{
49f48c71 9718 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9719 /* Complain if the field is deprecated and the file it was defined
9720 in isn't compiled at the same time the file which contains its
9721 use is */
9722 if (DECL_DEPRECATED (decl)
9723 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9724 {
9725 char the [20];
9726 switch (TREE_CODE (decl))
9727 {
9728 case FUNCTION_DECL:
9729 strcpy (the, "method");
9730 break;
9731 case FIELD_DECL:
9732 strcpy (the, "field");
9733 break;
9734 case TYPE_DECL:
9735 strcpy (the, "class");
9736 break;
15fdcfe9
PB
9737 default:
9738 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9739 }
9740 parse_warning_context
9741 (wfl, "The %s `%s' in class `%s' has been deprecated",
9742 the, lang_printable_name (decl, 0),
9743 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9744 }
9745}
9746
e04a16fb
AG
9747/* Returns 1 if class was declared in the current package, 0 otherwise */
9748
9749static int
9750class_in_current_package (class)
9751 tree class;
9752{
9753 static tree cache = NULL_TREE;
9754 int qualified_flag;
9755 tree left;
9756
9757 if (cache == class)
9758 return 1;
9759
9760 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9761
9762 /* If the current package is empty and the name of CLASS is
9763 qualified, class isn't in the current package. If there is a
9764 current package and the name of the CLASS is not qualified, class
9765 isn't in the current package */
0a2138e2 9766 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9767 return 0;
9768
9769 /* If there is not package and the name of CLASS isn't qualified,
9770 they belong to the same unnamed package */
9771 if (!ctxp->package && !qualified_flag)
9772 return 1;
9773
9774 /* Compare the left part of the name of CLASS with the package name */
9775 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9776 if (ctxp->package == left)
9777 {
19e223db
MM
9778 static int initialized_p;
9779 /* Register CACHE with the garbage collector. */
9780 if (!initialized_p)
9781 {
9782 ggc_add_tree_root (&cache, 1);
9783 initialized_p = 1;
9784 }
9785
e04a16fb
AG
9786 cache = class;
9787 return 1;
9788 }
9789 return 0;
9790}
9791
9792/* This function may generate code to access DECL from WHERE. This is
9793 done only if certain conditions meet. */
9794
9795static tree
9796maybe_access_field (decl, where, type)
9797 tree decl, where, type;
9798{
5e942c50
APB
9799 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9800 && !FIELD_STATIC (decl))
e04a16fb 9801 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9802 (type ? type : DECL_CONTEXT (decl)),
9803 DECL_NAME (decl));
e04a16fb
AG
9804 return decl;
9805}
9806
15fdcfe9 9807/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9808 and according to the situation, PRIMARY and WHERE may be
9809 used. IS_STATIC is set to 1 if the invoked function is static. */
9810
9811static tree
89e09b9a 9812patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9813 tree patch, primary, where;
9814 int *is_static;
b9f7e36c 9815 tree *ret_decl;
e04a16fb
AG
9816{
9817 tree wfl = TREE_OPERAND (patch, 0);
9818 tree args = TREE_OPERAND (patch, 1);
9819 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9820 tree list;
22eed1e6 9821 int is_static_flag = 0;
89e09b9a 9822 int is_super_init = 0;
bccaf73a 9823 tree this_arg = NULL_TREE;
e04a16fb
AG
9824
9825 /* Should be overriden if everything goes well. Otherwise, if
9826 something fails, it should keep this value. It stop the
9827 evaluation of a bogus assignment. See java_complete_tree,
9828 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9829 evaluating an assignment */
9830 TREE_TYPE (patch) = error_mark_node;
9831
9832 /* Since lookup functions are messing with line numbers, save the
9833 context now. */
9834 java_parser_context_save_global ();
9835
9836 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9837
9838 /* Resolution of qualified name, excluding constructors */
9839 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9840 {
dde1da72 9841 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9842 /* Extract the last IDENTIFIER of the qualified
9843 expression. This is a wfl and we will use it's location
9844 data during error report. */
9845 identifier_wfl = cut_identifier_in_qualified (wfl);
9846 identifier = EXPR_WFL_NODE (identifier_wfl);
9847
9848 /* Given the context, IDENTIFIER is syntactically qualified
9849 as a MethodName. We need to qualify what's before */
9850 qualify_ambiguous_name (wfl);
dde1da72 9851 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9852
dde1da72
APB
9853 if (resolved == error_mark_node)
9854 PATCH_METHOD_RETURN_ERROR ();
9855
9856 type = GET_SKIP_TYPE (resolved);
9857 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9858
9859 if (JPRIMITIVE_TYPE_P (type))
9860 {
7e51098e
TT
9861 parse_error_context
9862 (identifier_wfl,
9863 "Can't invoke a method on primitive type `%s'",
9864 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9865 PATCH_METHOD_RETURN_ERROR ();
9866 }
9867
dde1da72
APB
9868 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9869 args = nreverse (args);
2c56429a 9870
e04a16fb 9871 /* We're resolving a call from a type */
dde1da72 9872 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9873 {
dde1da72 9874 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9875 {
9876 parse_error_context
781b0558
KG
9877 (identifier_wfl,
9878 "Can't make static reference to method `%s' in interface `%s'",
9879 IDENTIFIER_POINTER (identifier),
e04a16fb 9880 IDENTIFIER_POINTER (name));
b9f7e36c 9881 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9882 }
e04a16fb
AG
9883 if (list && !METHOD_STATIC (list))
9884 {
c2e3db92 9885 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9886 parse_error_context
9887 (identifier_wfl,
9888 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9889 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9890 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9891 free (fct_name);
b9f7e36c 9892 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9893 }
9894 }
e04a16fb 9895 else
dde1da72
APB
9896 this_arg = primary = resolved;
9897
5e942c50 9898 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9899 wfl = identifier_wfl;
9900 }
9901 /* Resolution of simple names, names generated after a primary: or
9902 constructors */
9903 else
9904 {
cd531a2e 9905 tree class_to_search = NULL_TREE;
c2952b01 9906 int lc; /* Looking for Constructor */
e04a16fb
AG
9907
9908 /* We search constructor in their target class */
9909 if (CALL_CONSTRUCTOR_P (patch))
9910 {
22eed1e6
APB
9911 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9912 class_to_search = EXPR_WFL_NODE (wfl);
9913 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9914 this_identifier_node)
9915 class_to_search = NULL_TREE;
9916 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9917 super_identifier_node)
e04a16fb 9918 {
89e09b9a 9919 is_super_init = 1;
22eed1e6
APB
9920 if (CLASSTYPE_SUPER (current_class))
9921 class_to_search =
9922 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9923 else
9924 {
781b0558 9925 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9926 PATCH_METHOD_RETURN_ERROR ();
9927 }
e04a16fb 9928 }
22eed1e6
APB
9929
9930 /* Class to search is NULL if we're searching the current one */
9931 if (class_to_search)
e04a16fb 9932 {
c2952b01
APB
9933 class_to_search = resolve_and_layout (class_to_search, wfl);
9934
22eed1e6
APB
9935 if (!class_to_search)
9936 {
9937 parse_error_context
9938 (wfl, "Class `%s' not found in type declaration",
9939 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9940 PATCH_METHOD_RETURN_ERROR ();
9941 }
9942
5e942c50
APB
9943 /* Can't instantiate an abstract class, but we can
9944 invoke it's constructor. It's use within the `new'
9945 context is denied here. */
9946 if (CLASS_ABSTRACT (class_to_search)
9947 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9948 {
9949 parse_error_context
781b0558
KG
9950 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9951 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9952 PATCH_METHOD_RETURN_ERROR ();
9953 }
c2952b01 9954
22eed1e6 9955 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9956 }
22eed1e6
APB
9957 else
9958 class_to_search = current_class;
e04a16fb
AG
9959 lc = 1;
9960 }
9961 /* This is a regular search in the local class, unless an
9962 alternate class is specified. */
9963 else
9964 {
9965 class_to_search = (where ? where : current_class);
9966 lc = 0;
9967 }
c2952b01 9968
e04a16fb
AG
9969 /* NAME is a simple identifier or comes from a primary. Search
9970 in the class whose declaration contain the method being
9971 invoked. */
c877974e 9972 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9973
c2952b01 9974 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9975 /* Don't continue if no method were found, as the next statement
9976 can't be executed then. */
b9f7e36c
APB
9977 if (!list)
9978 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9979
9980 /* Check for static reference if non static methods */
9981 if (check_for_static_method_reference (wfl, patch, list,
9982 class_to_search, primary))
b9f7e36c 9983 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9984
165f37bc
APB
9985 /* Check for inner classes creation from illegal contexts */
9986 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9987 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9988 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9989 {
9990 parse_error_context
9991 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9992 lang_printable_name (class_to_search, 0),
9993 (!current_this ? "" :
9994 "; an explicit one must be provided when creating this inner class"));
9995 PATCH_METHOD_RETURN_ERROR ();
9996 }
9997
22eed1e6
APB
9998 /* Non static methods are called with the current object extra
9999 argument. If patch a `new TYPE()', the argument is the value
10000 returned by the object allocator. If method is resolved as a
10001 primary, use the primary otherwise use the current THIS. */
b9f7e36c 10002 args = nreverse (args);
bccaf73a 10003 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
10004 {
10005 this_arg = primary ? primary : current_this;
10006
10007 /* If we're using an access method, things are different.
10008 There are two familly of cases:
10009
10010 1) We're not generating bytecodes:
10011
10012 - LIST is non static. It's invocation is transformed from
10013 x(a1,...,an) into this$<n>.x(a1,....an).
10014 - LIST is static. It's invocation is transformed from
10015 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10016
10017 2) We're generating bytecodes:
10018
10019 - LIST is non static. It's invocation is transformed from
10020 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10021 - LIST is static. It's invocation is transformed from
10022 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
10023
10024 Of course, this$<n> can be abitrary complex, ranging from
10025 this$0 (the immediate outer context) to
10026 access$0(access$0(...(this$0))).
10027
10028 maybe_use_access_method returns a non zero value if the
dfb99c83 10029 this_arg has to be moved into the (then generated) stub
4dbf4496 10030 argument list. In the meantime, the selected function
dfb99c83 10031 might have be replaced by a generated stub. */
c2952b01 10032 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
10033 {
10034 args = tree_cons (NULL_TREE, this_arg, args);
10035 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10036 }
c2952b01 10037 }
e04a16fb 10038 }
b67d701b 10039
e04a16fb
AG
10040 /* Merge point of all resolution schemes. If we have nothing, this
10041 is an error, already signaled */
b9f7e36c
APB
10042 if (!list)
10043 PATCH_METHOD_RETURN_ERROR ();
b67d701b 10044
e04a16fb
AG
10045 /* Check accessibility, position the is_static flag, build and
10046 return the call */
9bbc7d9f 10047 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 10048 {
c2e3db92 10049 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
10050 parse_error_context
10051 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
10052 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 10053 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
10054 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
10055 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 10056 free (fct_name);
b9f7e36c 10057 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 10058 }
5e942c50 10059 check_deprecation (wfl, list);
22eed1e6 10060
c2952b01
APB
10061 /* If invoking a innerclass constructor, there are hidden parameters
10062 to pass */
10063 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10064 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10065 {
10066 /* And make sure we add the accessed local variables to be saved
10067 in field aliases. */
10068 args = build_alias_initializer_parameter_list
10069 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10070
da632f2c 10071 /* Secretly pass the current_this/primary as a second argument */
165f37bc 10072 if (primary || current_this)
f8b93ea7
APB
10073 {
10074 tree extra_arg;
10075 tree this_type = (current_this ?
10076 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10077 /* Method's (list) enclosing context */
10078 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10079 /* If we have a primary, use it. */
10080 if (primary)
10081 extra_arg = primary;
10082 /* The current `this' is an inner class but isn't a direct
10083 enclosing context for the inner class we're trying to
10084 create. Build an access to the proper enclosing context
10085 and use it. */
10086 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10087 && this_type != TREE_TYPE (mec))
10088 {
10089
10090 extra_arg = build_access_to_thisn (current_class,
10091 TREE_TYPE (mec), 0);
10092 extra_arg = java_complete_tree (extra_arg);
10093 }
10094 /* Otherwise, just use the current `this' as an enclosing
10095 context. */
10096 else
10097 extra_arg = current_this;
10098 args = tree_cons (NULL_TREE, extra_arg, args);
10099 }
165f37bc
APB
10100 else
10101 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10102 }
10103
152de068
APB
10104 /* This handles the situation where a constructor invocation needs
10105 to have an enclosing context passed as a second parameter (the
10106 constructor is one of an inner class. We extract it from the
10107 current function. */
10108 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10109 {
10110 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10111 tree extra_arg;
10112
10113 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10114 {
10115 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10116 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10117 }
10118 else
10119 {
10120 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10121 extra_arg =
10122 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10123 extra_arg = java_complete_tree (extra_arg);
10124 }
10125 args = tree_cons (NULL_TREE, extra_arg, args);
10126 }
10127
22eed1e6 10128 is_static_flag = METHOD_STATIC (list);
dba41d30 10129 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10130 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10131
c3f2a476
APB
10132 /* In the context of an explicit constructor invocation, we can't
10133 invoke any method relying on `this'. Exceptions are: we're
10134 invoking a static function, primary exists and is not the current
10135 this, we're creating a new object. */
22eed1e6 10136 if (ctxp->explicit_constructor_p
c3f2a476
APB
10137 && !is_static_flag
10138 && (!primary || primary == current_this)
10139 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10140 {
781b0558 10141 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10142 PATCH_METHOD_RETURN_ERROR ();
10143 }
e04a16fb 10144 java_parser_context_restore_global ();
22eed1e6
APB
10145 if (is_static)
10146 *is_static = is_static_flag;
b9f7e36c
APB
10147 /* Sometimes, we want the decl of the selected method. Such as for
10148 EH checking */
10149 if (ret_decl)
10150 *ret_decl = list;
89e09b9a
PB
10151 patch = patch_invoke (patch, list, args);
10152 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10153 {
c2952b01
APB
10154 tree finit_parms, finit_call;
10155
c00f0fb2 10156 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10157 finit_parms = build_alias_initializer_parameter_list
10158 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10159
c2952b01
APB
10160 finit_call =
10161 build_method_invocation (build_wfl_node (finit_identifier_node),
10162 finit_parms);
10163
10164 /* Generate the code used to initialize fields declared with an
10165 initialization statement and build a compound statement along
10166 with the super constructor invocation. */
89e09b9a
PB
10167 patch = build (COMPOUND_EXPR, void_type_node, patch,
10168 java_complete_tree (finit_call));
10169 CAN_COMPLETE_NORMALLY (patch) = 1;
10170 }
10171 return patch;
e04a16fb
AG
10172}
10173
10174/* Check that we're not trying to do a static reference to a method in
10175 non static method. Return 1 if it's the case, 0 otherwise. */
10176
10177static int
10178check_for_static_method_reference (wfl, node, method, where, primary)
10179 tree wfl, node, method, where, primary;
10180{
10181 if (METHOD_STATIC (current_function_decl)
10182 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10183 {
c2e3db92 10184 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10185 parse_error_context
10186 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10187 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10188 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10189 free (fct_name);
10190 return 1;
10191 }
10192 return 0;
10193}
10194
c2952b01
APB
10195/* Fix the invocation of *MDECL if necessary in the case of a
10196 invocation from an inner class. *THIS_ARG might be modified
10197 appropriately and an alternative access to *MDECL might be
10198 returned. */
10199
10200static int
10201maybe_use_access_method (is_super_init, mdecl, this_arg)
10202 int is_super_init;
10203 tree *mdecl, *this_arg;
10204{
10205 tree ctx;
10206 tree md = *mdecl, ta = *this_arg;
10207 int to_return = 0;
10208 int non_static_context = !METHOD_STATIC (md);
10209
10210 if (is_super_init
165f37bc
APB
10211 || DECL_CONTEXT (md) == current_class
10212 || !PURE_INNER_CLASS_TYPE_P (current_class)
10213 || DECL_FINIT_P (md))
c2952b01
APB
10214 return 0;
10215
10216 /* If we're calling a method found in an enclosing class, generate
10217 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10218 invoking a static method. Note that if MD's type is unrelated to
10219 CURRENT_CLASS, then the current this can be used. */
c2952b01 10220
2cb3951d 10221 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10222 {
10223 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10224 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10225 {
10226 ta = build_current_thisn (current_class);
10227 ta = build_wfl_node (ta);
10228 }
10229 else
10230 {
10231 tree type = ctx;
10232 while (type)
10233 {
10234 maybe_build_thisn_access_method (type);
4dbf4496 10235 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10236 {
10237 ta = build_access_to_thisn (ctx, type, 0);
10238 break;
10239 }
10240 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10241 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10242 }
10243 }
10244 ta = java_complete_tree (ta);
10245 }
10246
10247 /* We might have to use an access method to get to MD. We can
10248 break the method access rule as far as we're not generating
10249 bytecode */
10250 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10251 {
10252 md = build_outer_method_access_method (md);
10253 to_return = 1;
10254 }
10255
10256 *mdecl = md;
10257 *this_arg = ta;
10258
10259 /* Returnin a non zero value indicates we were doing a non static
10260 method invokation that is now a static invocation. It will have
10261 callee displace `this' to insert it in the regular argument
10262 list. */
10263 return (non_static_context && to_return);
10264}
10265
e04a16fb
AG
10266/* Patch an invoke expression METHOD and ARGS, based on its invocation
10267 mode. */
10268
10269static tree
89e09b9a 10270patch_invoke (patch, method, args)
e04a16fb 10271 tree patch, method, args;
e04a16fb
AG
10272{
10273 tree dtable, func;
0a2138e2 10274 tree original_call, t, ta;
e815887f 10275 tree cond = NULL_TREE;
e04a16fb 10276
5e942c50
APB
10277 /* Last step for args: convert build-in types. If we're dealing with
10278 a new TYPE() type call, the first argument to the constructor
e815887f 10279 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10280 `new' */
10281 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10282 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10283 t = TREE_CHAIN (t);
ac825856
APB
10284 for (ta = args; t != end_params_node && ta;
10285 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10286 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10287 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10288 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10289
10290 /* Resolve unresolved returned type isses */
10291 t = TREE_TYPE (TREE_TYPE (method));
10292 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10293 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10294
e8fc7396 10295 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10296 func = method;
10297 else
e04a16fb 10298 {
15fdcfe9 10299 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10300 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10301 {
10302 case INVOKE_VIRTUAL:
10303 dtable = invoke_build_dtable (0, args);
10304 func = build_invokevirtual (dtable, method);
10305 break;
b9f7e36c 10306
e815887f
TT
10307 case INVOKE_NONVIRTUAL:
10308 /* If the object for the method call is null, we throw an
10309 exception. We don't do this if the object is the current
10310 method's `this'. In other cases we just rely on an
10311 optimization pass to eliminate redundant checks. */
10312 if (TREE_VALUE (args) != current_this)
10313 {
10314 /* We use a SAVE_EXPR here to make sure we only evaluate
10315 the new `self' expression once. */
10316 tree save_arg = save_expr (TREE_VALUE (args));
10317 TREE_VALUE (args) = save_arg;
10318 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10319 null_pointer_node);
10320 }
10321 /* Fall through. */
10322
15fdcfe9
PB
10323 case INVOKE_SUPER:
10324 case INVOKE_STATIC:
10325 func = build_known_method_ref (method, TREE_TYPE (method),
10326 DECL_CONTEXT (method),
10327 signature, args);
10328 break;
e04a16fb 10329
15fdcfe9
PB
10330 case INVOKE_INTERFACE:
10331 dtable = invoke_build_dtable (1, args);
173f556c 10332 func = build_invokeinterface (dtable, method);
15fdcfe9 10333 break;
5e942c50 10334
15fdcfe9 10335 default:
89e09b9a 10336 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10337 }
10338
10339 /* Ensure self_type is initialized, (invokestatic). FIXME */
10340 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10341 }
10342
e04a16fb
AG
10343 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10344 TREE_OPERAND (patch, 0) = func;
10345 TREE_OPERAND (patch, 1) = args;
10346 original_call = patch;
10347
e815887f 10348 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10349 returned value is the first argument to the constructor. We build
10350 a COMPOUND_EXPR and use saved expression so that the overall NEW
10351 expression value is a pointer to a newly created and initialized
10352 class. */
10353 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10354 {
10355 tree class = DECL_CONTEXT (method);
10356 tree c1, saved_new, size, new;
e8fc7396 10357 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10358 {
10359 TREE_TYPE (patch) = build_pointer_type (class);
10360 return patch;
10361 }
e04a16fb
AG
10362 if (!TYPE_SIZE (class))
10363 safe_layout_class (class);
10364 size = size_in_bytes (class);
10365 new = build (CALL_EXPR, promote_type (class),
10366 build_address_of (alloc_object_node),
10367 tree_cons (NULL_TREE, build_class_ref (class),
10368 build_tree_list (NULL_TREE,
10369 size_in_bytes (class))),
10370 NULL_TREE);
10371 saved_new = save_expr (new);
10372 c1 = build_tree_list (NULL_TREE, saved_new);
10373 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10374 TREE_OPERAND (original_call, 1) = c1;
10375 TREE_SET_CODE (original_call, CALL_EXPR);
10376 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10377 }
e815887f
TT
10378
10379 /* If COND is set, then we are building a check to see if the object
10380 is NULL. */
10381 if (cond != NULL_TREE)
10382 {
10383 /* We have to make the `then' branch a compound expression to
10384 make the types turn out right. This seems bizarre. */
10385 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10386 build (COMPOUND_EXPR, TREE_TYPE (patch),
10387 build (CALL_EXPR, void_type_node,
10388 build_address_of (soft_nullpointer_node),
10389 NULL_TREE, NULL_TREE),
10390 (FLOAT_TYPE_P (TREE_TYPE (patch))
10391 ? build_real (TREE_TYPE (patch), dconst0)
10392 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10393 integer_zero_node))),
10394 patch);
10395 TREE_SIDE_EFFECTS (patch) = 1;
10396 }
10397
e04a16fb
AG
10398 return patch;
10399}
10400
10401static int
10402invocation_mode (method, super)
10403 tree method;
10404 int super;
10405{
10406 int access = get_access_flags_from_decl (method);
10407
22eed1e6
APB
10408 if (super)
10409 return INVOKE_SUPER;
10410
e815887f 10411 if (access & ACC_STATIC)
e04a16fb
AG
10412 return INVOKE_STATIC;
10413
e815887f
TT
10414 /* We have to look for a constructor before we handle nonvirtual
10415 calls; otherwise the constructor will look nonvirtual. */
10416 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10417 return INVOKE_STATIC;
e815887f
TT
10418
10419 if (access & ACC_FINAL || access & ACC_PRIVATE)
10420 return INVOKE_NONVIRTUAL;
10421
10422 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10423 return INVOKE_NONVIRTUAL;
10424
e04a16fb
AG
10425 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10426 return INVOKE_INTERFACE;
22eed1e6 10427
e04a16fb
AG
10428 return INVOKE_VIRTUAL;
10429}
10430
b67d701b
PB
10431/* Retrieve a refined list of matching methods. It covers the step
10432 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10433
10434static tree
10435lookup_method_invoke (lc, cl, class, name, arg_list)
10436 int lc;
10437 tree cl;
10438 tree class, name, arg_list;
10439{
de4c7b02 10440 tree atl = end_params_node; /* Arg Type List */
c877974e 10441 tree method, signature, list, node;
49f48c71 10442 const char *candidates; /* Used for error report */
b5b8a0e7 10443 char *dup;
e04a16fb 10444
5e942c50 10445 /* Fix the arguments */
e04a16fb
AG
10446 for (node = arg_list; node; node = TREE_CHAIN (node))
10447 {
e3884b71 10448 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10449 /* Non primitive type may have to be resolved */
e3884b71 10450 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10451 resolve_and_layout (current_arg, NULL_TREE);
10452 /* And promoted */
b67d701b 10453 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10454 current_arg = promote_type (current_arg);
5e942c50 10455 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10456 }
e04a16fb 10457
c2952b01
APB
10458 /* Presto. If we're dealing with an anonymous class and a
10459 constructor call, generate the right constructor now, since we
10460 know the arguments' types. */
10461
10462 if (lc && ANONYMOUS_CLASS_P (class))
10463 craft_constructor (TYPE_NAME (class), atl);
10464
5e942c50
APB
10465 /* Find all candidates and then refine the list, searching for the
10466 most specific method. */
10467 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10468 list = find_most_specific_methods_list (list);
b67d701b
PB
10469 if (list && !TREE_CHAIN (list))
10470 return TREE_VALUE (list);
e04a16fb 10471
b67d701b
PB
10472 /* Issue an error. List candidates if any. Candidates are listed
10473 only if accessible (non accessible methods may end-up here for
10474 the sake of a better error report). */
10475 candidates = NULL;
10476 if (list)
e04a16fb 10477 {
e04a16fb 10478 tree current;
b67d701b 10479 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10480 for (current = list; current; current = TREE_CHAIN (current))
10481 {
b67d701b
PB
10482 tree cm = TREE_VALUE (current);
10483 char string [4096];
10484 if (!cm || not_accessible_p (class, cm, 0))
10485 continue;
b67d701b 10486 sprintf
22eed1e6
APB
10487 (string, " `%s' in `%s'%s",
10488 get_printable_method_name (cm),
b67d701b
PB
10489 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10490 (TREE_CHAIN (current) ? "\n" : ""));
10491 obstack_grow (&temporary_obstack, string, strlen (string));
10492 }
10493 obstack_1grow (&temporary_obstack, '\0');
10494 candidates = obstack_finish (&temporary_obstack);
10495 }
10496 /* Issue the error message */
c877974e
APB
10497 method = make_node (FUNCTION_TYPE);
10498 TYPE_ARG_TYPES (method) = atl;
b67d701b 10499 signature = build_java_argument_signature (method);
c63b98cd 10500 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10501 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10502 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10503 (lc ? dup : IDENTIFIER_POINTER (name)),
10504 IDENTIFIER_POINTER (signature), dup,
b67d701b 10505 (candidates ? candidates : ""));
b5b8a0e7 10506 free (dup);
b67d701b
PB
10507 return NULL_TREE;
10508}
10509
5e942c50
APB
10510/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10511 when we're looking for a constructor. */
b67d701b
PB
10512
10513static tree
5e942c50
APB
10514find_applicable_accessible_methods_list (lc, class, name, arglist)
10515 int lc;
b67d701b
PB
10516 tree class, name, arglist;
10517{
ad69b5b6
BM
10518 static struct hash_table t, *searched_classes = NULL;
10519 static int search_not_done = 0;
b67d701b
PB
10520 tree list = NULL_TREE, all_list = NULL_TREE;
10521
ad69b5b6
BM
10522 /* Check the hash table to determine if this class has been searched
10523 already. */
10524 if (searched_classes)
10525 {
10526 if (hash_lookup (searched_classes,
10527 (const hash_table_key) class, FALSE, NULL))
10528 return NULL;
10529 }
10530 else
10531 {
10532 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10533 java_hash_compare_tree_node);
10534 searched_classes = &t;
10535 }
10536
10537 search_not_done++;
10538 hash_lookup (searched_classes,
0c2b8145 10539 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10540
c2952b01
APB
10541 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10542 {
10543 load_class (class, 1);
10544 safe_layout_class (class);
10545 }
10546
1982388a 10547 /* Search interfaces */
9a7ab4b3
APB
10548 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10549 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10550 {
1982388a
APB
10551 int i, n;
10552 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10553 search_applicable_methods_list (lc, TYPE_METHODS (class),
10554 name, arglist, &list, &all_list);
1982388a 10555 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10556 for (i = 1; i < n; i++)
b67d701b 10557 {
de0b553f
APB
10558 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10559 tree rlist;
10560
de0b553f
APB
10561 rlist = find_applicable_accessible_methods_list (lc, t, name,
10562 arglist);
165f37bc 10563 list = chainon (rlist, list);
e04a16fb 10564 }
e04a16fb 10565 }
1982388a
APB
10566 /* Search classes */
10567 else
c2952b01 10568 {
165f37bc
APB
10569 tree sc = class;
10570 int seen_inner_class = 0;
c2952b01
APB
10571 search_applicable_methods_list (lc, TYPE_METHODS (class),
10572 name, arglist, &list, &all_list);
10573
165f37bc
APB
10574 /* We must search all interfaces of this class */
10575 if (!lc)
10576 {
10577 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10578 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10579 for (i = 1; i < n; i++)
10580 {
10581 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10582 if (t != object_type_node)
30a3caef
ZW
10583 {
10584 tree rlist
10585 = find_applicable_accessible_methods_list (lc, t,
10586 name, arglist);
10587 list = chainon (rlist, list);
10588 }
165f37bc 10589 }
165f37bc
APB
10590 }
10591
c2952b01
APB
10592 /* Search enclosing context of inner classes before looking
10593 ancestors up. */
10594 while (!lc && INNER_CLASS_TYPE_P (class))
10595 {
165f37bc
APB
10596 tree rlist;
10597 seen_inner_class = 1;
c2952b01 10598 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10599 rlist = find_applicable_accessible_methods_list (lc, class,
10600 name, arglist);
10601 list = chainon (rlist, list);
c2952b01 10602 }
165f37bc
APB
10603
10604 if (!lc && seen_inner_class
10605 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10606 class = CLASSTYPE_SUPER (sc);
10607 else
10608 class = sc;
10609
ad69b5b6
BM
10610 /* Search superclass */
10611 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10612 {
10613 tree rlist;
10614 class = CLASSTYPE_SUPER (class);
10615 rlist = find_applicable_accessible_methods_list (lc, class,
10616 name, arglist);
10617 list = chainon (rlist, list);
10618 }
10619 }
10620
10621 search_not_done--;
10622
10623 /* We're done. Reset the searched classes list and finally search
10624 java.lang.Object if it wasn't searched already. */
10625 if (!search_not_done)
10626 {
10627 if (!lc
10628 && TYPE_METHODS (object_type_node)
10629 && !hash_lookup (searched_classes,
10630 (const hash_table_key) object_type_node,
10631 FALSE, NULL))
10632 {
10633 search_applicable_methods_list (lc,
10634 TYPE_METHODS (object_type_node),
10635 name, arglist, &list, &all_list);
10636 }
10637 hash_table_free (searched_classes);
10638 searched_classes = NULL;
c2952b01 10639 }
1982388a 10640
b67d701b
PB
10641 /* Either return the list obtained or all selected (but
10642 inaccessible) methods for better error report. */
10643 return (!list ? all_list : list);
10644}
e04a16fb 10645
ad69b5b6 10646/* Effectively search for the appropriate method in method */
1982388a
APB
10647
10648static void
c2952b01 10649search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10650 int lc;
10651 tree method, name, arglist;
10652 tree *list, *all_list;
10653{
10654 for (; method; method = TREE_CHAIN (method))
10655 {
10656 /* When dealing with constructor, stop here, otherwise search
10657 other classes */
10658 if (lc && !DECL_CONSTRUCTOR_P (method))
10659 continue;
10660 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10661 || (GET_METHOD_NAME (method) != name)))
10662 continue;
c7303e41 10663
1982388a
APB
10664 if (argument_types_convertible (method, arglist))
10665 {
10666 /* Retain accessible methods only */
10667 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10668 method, 0))
10669 *list = tree_cons (NULL_TREE, method, *list);
10670 else
10671 /* Also retain all selected method here */
10672 *all_list = tree_cons (NULL_TREE, method, *list);
10673 }
10674 }
ad69b5b6 10675}
1982388a 10676
b67d701b
PB
10677/* 15.11.2.2 Choose the Most Specific Method */
10678
10679static tree
10680find_most_specific_methods_list (list)
10681 tree list;
10682{
10683 int max = 0;
9a7ab4b3 10684 int abstract, candidates;
b67d701b
PB
10685 tree current, new_list = NULL_TREE;
10686 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10687 {
b67d701b
PB
10688 tree method;
10689 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10690
10691 for (method = list; method; method = TREE_CHAIN (method))
10692 {
0c2b8145 10693 tree method_v, current_v;
b67d701b
PB
10694 /* Don't test a method against itself */
10695 if (method == current)
10696 continue;
10697
0c2b8145
APB
10698 method_v = TREE_VALUE (method);
10699 current_v = TREE_VALUE (current);
10700
10701 /* Compare arguments and location where methods where declared */
10702 if (argument_types_convertible (method_v, current_v))
b67d701b 10703 {
0c2b8145
APB
10704 if (valid_method_invocation_conversion_p
10705 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10706 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10707 && enclosing_context_p (DECL_CONTEXT (method_v),
10708 DECL_CONTEXT (current_v))))
10709 {
10710 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10711 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10712 max = (v > max ? v : max);
10713 }
b67d701b
PB
10714 }
10715 }
e04a16fb
AG
10716 }
10717
b67d701b 10718 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10719 for (current = list, abstract = -1, candidates = -1;
10720 current; current = TREE_CHAIN (current))
b67d701b 10721 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10722 {
10723 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10724 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10725 candidates++;
10726 }
b67d701b 10727
165f37bc
APB
10728 /* If we have several and they're all abstract, just pick the
10729 closest one. */
9a7ab4b3
APB
10730 if (candidates > 0 && (candidates == abstract))
10731 {
10732 new_list = nreverse (new_list);
10733 TREE_CHAIN (new_list) = NULL_TREE;
10734 }
165f37bc 10735
cab8e2bd
BM
10736 /* We have several (we couldn't find a most specific), all but one
10737 are abstract, we pick the only non abstract one. */
10738 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10739 {
9a7ab4b3
APB
10740 for (current = new_list; current; current = TREE_CHAIN (current))
10741 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10742 {
10743 TREE_CHAIN (current) = NULL_TREE;
10744 new_list = current;
10745 }
165f37bc
APB
10746 }
10747
b67d701b
PB
10748 /* If we can't find one, lower expectations and try to gather multiple
10749 maximally specific methods */
165f37bc 10750 while (!new_list && max)
b67d701b
PB
10751 {
10752 while (--max > 0)
10753 {
10754 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10755 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10756 }
b67d701b
PB
10757 }
10758
10759 return new_list;
e04a16fb
AG
10760}
10761
b67d701b
PB
10762/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10763 converted by method invocation conversion (5.3) to the type of the
10764 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10765 to change less often than M1. */
e04a16fb 10766
b67d701b
PB
10767static int
10768argument_types_convertible (m1, m2_or_arglist)
10769 tree m1, m2_or_arglist;
e04a16fb 10770{
b67d701b
PB
10771 static tree m2_arg_value = NULL_TREE;
10772 static tree m2_arg_cache = NULL_TREE;
19e223db 10773 static int initialized_p;
e04a16fb 10774
b67d701b 10775 register tree m1_arg, m2_arg;
e04a16fb 10776
19e223db
MM
10777 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10778 collector. */
10779 if (!initialized_p)
10780 {
10781 ggc_add_tree_root (&m2_arg_value, 1);
10782 ggc_add_tree_root (&m2_arg_cache, 1);
10783 initialized_p = 1;
10784 }
10785
c2952b01 10786 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10787
b67d701b
PB
10788 if (m2_arg_value == m2_or_arglist)
10789 m2_arg = m2_arg_cache;
10790 else
10791 {
10792 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10793 argument types */
10794 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10795 {
10796 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10797 if (!METHOD_STATIC (m2_or_arglist))
10798 m2_arg = TREE_CHAIN (m2_arg);
10799 }
10800 else
10801 m2_arg = m2_or_arglist;
e04a16fb 10802
b67d701b
PB
10803 m2_arg_value = m2_or_arglist;
10804 m2_arg_cache = m2_arg;
10805 }
e04a16fb 10806
de4c7b02 10807 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10808 {
c877974e 10809 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10810 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10811 TREE_VALUE (m2_arg)))
10812 break;
10813 m1_arg = TREE_CHAIN (m1_arg);
10814 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10815 }
de4c7b02 10816 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10817}
10818
10819/* Qualification routines */
10820
10821static void
10822qualify_ambiguous_name (id)
10823 tree id;
10824{
cd531a2e
KG
10825 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10826 saved_current_class;
d8fccff5 10827 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10828 int code;
e04a16fb
AG
10829
10830 /* We first qualify the first element, then derive qualification of
10831 others based on the first one. If the first element is qualified
10832 by a resolution (field or type), this resolution is stored in the
10833 QUAL_RESOLUTION of the qual element being examined. We need to
10834 save the current_class since the use of SUPER might change the
10835 its value. */
10836 saved_current_class = current_class;
10837 qual = EXPR_WFL_QUALIFICATION (id);
10838 do {
10839
10840 /* Simple qualified expression feature a qual_wfl that is a
10841 WFL. Expression derived from a primary feature more complicated
10842 things like a CALL_EXPR. Expression from primary need to be
10843 worked out to extract the part on which the qualification will
10844 take place. */
10845 qual_wfl = QUAL_WFL (qual);
10846 switch (TREE_CODE (qual_wfl))
10847 {
10848 case CALL_EXPR:
10849 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10850 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10851 {
10852 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10853 qual_wfl = QUAL_WFL (qual);
10854 }
10855 break;
d8fccff5 10856 case NEW_ARRAY_EXPR:
c2952b01 10857 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10858 qual = TREE_CHAIN (qual);
1a6d4fb7 10859 again = new_array_found = 1;
d8fccff5 10860 continue;
e04a16fb 10861 case CONVERT_EXPR:
f2760b27
APB
10862 break;
10863 case NEW_CLASS_EXPR:
e04a16fb
AG
10864 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10865 break;
c583dd46
APB
10866 case ARRAY_REF:
10867 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10868 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10869 break;
8576f094
APB
10870 case STRING_CST:
10871 qual = TREE_CHAIN (qual);
10872 qual_wfl = QUAL_WFL (qual);
10873 break;
165f37bc
APB
10874 case CLASS_LITERAL:
10875 qual = TREE_CHAIN (qual);
10876 qual_wfl = QUAL_WFL (qual);
10877 break;
0a2138e2
APB
10878 default:
10879 /* Fix for -Wall. Just break doing nothing */
10880 break;
e04a16fb 10881 }
8576f094 10882
e04a16fb
AG
10883 ptr_type = current_class;
10884 again = 0;
8576f094
APB
10885 code = TREE_CODE (qual_wfl);
10886
10887 /* Pos evaluation: non WFL leading expression nodes */
10888 if (code == CONVERT_EXPR
10889 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10890 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10891
cd7c5840
APB
10892 else if (code == INTEGER_CST)
10893 name = qual_wfl;
10894
ac22f9cb 10895 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10896 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10897 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10898
c2952b01
APB
10899 else if (code == TREE_LIST)
10900 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10901
37feda7d
APB
10902 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10903 || code == PLUS_EXPR)
8576f094
APB
10904 {
10905 qual = TREE_CHAIN (qual);
10906 qual_wfl = QUAL_WFL (qual);
10907 again = 1;
10908 }
10909 else
f441f671
APB
10910 {
10911 name = EXPR_WFL_NODE (qual_wfl);
10912 if (!name)
10913 {
10914 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10915 again = 1;
10916 }
10917 }
10918
e04a16fb
AG
10919 /* If we have a THIS (from a primary), we set the context accordingly */
10920 if (name == this_identifier_node)
10921 {
6e22695a
APB
10922 /* This isn't really elegant. One more added irregularity
10923 before I start using COMPONENT_REF (hopefully very soon.) */
10924 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10925 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10926 EXPR_WITH_FILE_LOCATION
10927 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10928 this_identifier_node)
10929 {
10930 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10931 qual = EXPR_WFL_QUALIFICATION (qual);
10932 }
e04a16fb
AG
10933 qual = TREE_CHAIN (qual);
10934 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10935 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10936 again = 1;
10937 else
10938 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10939 this_found = 1;
10940 }
10941 /* If we have a SUPER, we set the context accordingly */
10942 if (name == super_identifier_node)
10943 {
10944 current_class = CLASSTYPE_SUPER (ptr_type);
10945 /* Check that there is such a thing as a super class. If not,
10946 return. The error will be caught later on, during the
10947 resolution */
10948 if (!current_class)
10949 {
10950 current_class = saved_current_class;
10951 return;
10952 }
10953 qual = TREE_CHAIN (qual);
10954 /* Do one more interation to set things up */
10955 super_found = again = 1;
10956 }
10957 } while (again);
10958
f2760b27
APB
10959 /* If name appears within the scope of a local variable declaration
10960 or parameter declaration, then it is an expression name. We don't
10961 carry this test out if we're in the context of the use of SUPER
10962 or THIS */
cd7c5840
APB
10963 if (!this_found && !super_found
10964 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10965 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10966 {
10967 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10968 QUAL_RESOLUTION (qual) = decl;
10969 }
10970
10971 /* If within the class/interface NAME was found to be used there
10972 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10973 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10974 address length, it is OK. */
10975 else if ((decl = lookup_field_wrapper (ptr_type, name))
10976 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10977 {
10978 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10979 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10980 }
10981
1a6d4fb7 10982 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10983 - NAME is a class/interface declared within the compilation
10984 unit containing NAME,
10985 - NAME is imported via a single-type-import declaration,
10986 - NAME is declared in an another compilation unit of the package
10987 of the compilation unit containing NAME,
10988 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10989 of the compilation unit containing NAME.
10990 - NAME is actually a STRING_CST. */
cd7c5840
APB
10991 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10992 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10993 {
10994 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10995 QUAL_RESOLUTION (qual) = decl;
10996 }
10997
f2760b27 10998 /* Method call, array references and cast are expression name */
9bbc7d9f 10999 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
11000 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11001 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
11002 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11003
11004 /* Check here that NAME isn't declared by more than one
11005 type-import-on-demand declaration of the compilation unit
11006 containing NAME. FIXME */
11007
11008 /* Otherwise, NAME is reclassified as a package name */
11009 else
11010 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11011
11012 /* Propagate the qualification accross other components of the
11013 qualified name */
11014 for (qual = TREE_CHAIN (qual); qual;
11015 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11016 {
11017 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11018 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11019 else
11020 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11021 }
11022
11023 /* Store the global qualification for the ambiguous part of ID back
11024 into ID fields */
11025 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11026 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11027 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11028 RESOLVE_TYPE_NAME_P (id) = 1;
11029 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11030 RESOLVE_PACKAGE_NAME_P (id) = 1;
11031
11032 /* Restore the current class */
11033 current_class = saved_current_class;
11034}
11035
11036static int
11037breakdown_qualified (left, right, source)
11038 tree *left, *right, source;
11039{
63ad61ed 11040 char *p, *base;
e04a16fb
AG
11041 int l = IDENTIFIER_LENGTH (source);
11042
63ad61ed
ZW
11043 base = alloca (l + 1);
11044 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11045
e04a16fb 11046 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 11047 p = base + l - 1;
e04a16fb
AG
11048 while (*p != '.' && p != base)
11049 p--;
11050
11051 /* We didn't find a '.'. Return an error */
11052 if (p == base)
11053 return 1;
11054
11055 *p = '\0';
11056 if (right)
11057 *right = get_identifier (p+1);
63ad61ed 11058 *left = get_identifier (base);
e04a16fb
AG
11059
11060 return 0;
11061}
11062
e04a16fb 11063/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11064 local variable decls if present.
11065 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11066
11067static tree
11068java_complete_tree (node)
11069 tree node;
5b09b33e
PB
11070{
11071 node = java_complete_lhs (node);
c7303e41 11072 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11073 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11074 && !flag_emit_xref)
5b09b33e
PB
11075 {
11076 tree value = DECL_INITIAL (node);
11077 DECL_INITIAL (node) = NULL_TREE;
11078 value = fold_constant_for_init (value, node);
11079 DECL_INITIAL (node) = value;
11080 if (value != NULL_TREE)
c2952b01
APB
11081 {
11082 /* fold_constant_for_init sometimes widen the original type
11083 of the constant (i.e. byte to int.) It's not desirable,
11084 especially if NODE is a function argument. */
11085 if (TREE_CODE (value) == INTEGER_CST
11086 && TREE_TYPE (node) != TREE_TYPE (value))
11087 return convert (TREE_TYPE (node), value);
11088 else
11089 return value;
11090 }
c7303e41
APB
11091 else
11092 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11093 }
11094 return node;
11095}
11096
2aa11e97
APB
11097static tree
11098java_stabilize_reference (node)
11099 tree node;
11100{
11101 if (TREE_CODE (node) == COMPOUND_EXPR)
11102 {
11103 tree op0 = TREE_OPERAND (node, 0);
11104 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11105 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11106 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11107 return node;
11108 }
5cbdba64 11109 return stabilize_reference (node);
2aa11e97
APB
11110}
11111
5b09b33e
PB
11112/* Patch tree nodes in a function body. When a BLOCK is found, push
11113 local variable decls if present.
11114 Same as java_complete_tree, but does not resolve static finals to values. */
11115
11116static tree
11117java_complete_lhs (node)
11118 tree node;
e04a16fb 11119{
22eed1e6 11120 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11121 int flag;
e04a16fb
AG
11122
11123 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11124 worked out. */
e04a16fb
AG
11125 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11126 return node;
11127
11128 /* The switch block implements cases processing container nodes
11129 first. Contained nodes are always written back. Leaves come
11130 next and return a value. */
11131 switch (TREE_CODE (node))
11132 {
11133 case BLOCK:
11134
11135 /* 1- Block section.
11136 Set the local values on decl names so we can identify them
11137 faster when they're referenced. At that stage, identifiers
11138 are legal so we don't check for declaration errors. */
11139 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11140 {
11141 DECL_CONTEXT (cn) = current_function_decl;
11142 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11143 }
15fdcfe9
PB
11144 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11145 CAN_COMPLETE_NORMALLY (node) = 1;
11146 else
e04a16fb 11147 {
15fdcfe9
PB
11148 tree stmt = BLOCK_EXPR_BODY (node);
11149 tree *ptr;
11150 int error_seen = 0;
11151 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11152 {
c877974e
APB
11153 /* Re-order from (((A; B); C); ...; Z) to
11154 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11155 This makes it easier to scan the statements left-to-right
11156 without using recursion (which might overflow the stack
11157 if the block has many statements. */
11158 for (;;)
11159 {
11160 tree left = TREE_OPERAND (stmt, 0);
11161 if (TREE_CODE (left) != COMPOUND_EXPR)
11162 break;
11163 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11164 TREE_OPERAND (left, 1) = stmt;
11165 stmt = left;
11166 }
11167 BLOCK_EXPR_BODY (node) = stmt;
11168 }
11169
c877974e
APB
11170 /* Now do the actual complete, without deep recursion for
11171 long blocks. */
15fdcfe9 11172 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11173 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11174 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11175 {
11176 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11177 tree *next = &TREE_OPERAND (*ptr, 1);
11178 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11179 if (cur == empty_stmt_node)
11180 {
11181 /* Optimization; makes it easier to detect empty bodies.
11182 Most useful for <clinit> with all-constant initializer. */
11183 *ptr = *next;
11184 continue;
11185 }
15fdcfe9
PB
11186 if (TREE_CODE (cur) == ERROR_MARK)
11187 error_seen++;
11188 else if (! CAN_COMPLETE_NORMALLY (cur))
11189 {
11190 wfl_op2 = *next;
11191 for (;;)
11192 {
11193 if (TREE_CODE (wfl_op2) == BLOCK)
11194 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11195 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11196 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11197 else
11198 break;
11199 }
11200 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11201 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11202 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11203 }
11204 ptr = next;
11205 }
11206 *ptr = java_complete_tree (*ptr);
11207
11208 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11209 return error_mark_node;
15fdcfe9 11210 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11211 }
11212 /* Turn local bindings to null */
11213 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11214 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11215
11216 TREE_TYPE (node) = void_type_node;
11217 break;
11218
11219 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11220
b9f7e36c
APB
11221 case THROW_EXPR:
11222 wfl_op1 = TREE_OPERAND (node, 0);
11223 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11224 /* 14.19 A throw statement cannot complete normally. */
11225 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11226 return patch_throw_statement (node, wfl_op1);
11227
11228 case SYNCHRONIZED_EXPR:
11229 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11230 return patch_synchronized_statement (node, wfl_op1);
11231
b67d701b
PB
11232 case TRY_EXPR:
11233 return patch_try_statement (node);
11234
a7d8d81f
PB
11235 case TRY_FINALLY_EXPR:
11236 COMPLETE_CHECK_OP_0 (node);
11237 COMPLETE_CHECK_OP_1 (node);
11238 CAN_COMPLETE_NORMALLY (node)
11239 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11240 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11241 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11242 return node;
11243
5a005d9e
PB
11244 case CLEANUP_POINT_EXPR:
11245 COMPLETE_CHECK_OP_0 (node);
11246 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11247 CAN_COMPLETE_NORMALLY (node) =
11248 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11249 return node;
11250
11251 case WITH_CLEANUP_EXPR:
11252 COMPLETE_CHECK_OP_0 (node);
11253 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11254 CAN_COMPLETE_NORMALLY (node) =
11255 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11256 TREE_TYPE (node) = void_type_node;
11257 return node;
11258
e04a16fb
AG
11259 case LABELED_BLOCK_EXPR:
11260 PUSH_LABELED_BLOCK (node);
11261 if (LABELED_BLOCK_BODY (node))
11262 COMPLETE_CHECK_OP_1 (node);
11263 TREE_TYPE (node) = void_type_node;
11264 POP_LABELED_BLOCK ();
1fb89a4d
APB
11265
11266 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11267 {
11268 LABELED_BLOCK_BODY (node) = NULL_TREE;
11269 CAN_COMPLETE_NORMALLY (node) = 1;
11270 }
1fb89a4d 11271 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11272 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11273 return node;
11274
11275 case EXIT_BLOCK_EXPR:
11276 /* We don't complete operand 1, because it's the return value of
11277 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11278 return patch_bc_statement (node);
11279
15fdcfe9
PB
11280 case CASE_EXPR:
11281 cn = java_complete_tree (TREE_OPERAND (node, 0));
11282 if (cn == error_mark_node)
11283 return cn;
11284
8576f094
APB
11285 /* First, the case expression must be constant. Values of final
11286 fields are accepted. */
15fdcfe9 11287 cn = fold (cn);
8576f094
APB
11288 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11289 && JDECL_P (TREE_OPERAND (cn, 1))
11290 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11291 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11292 {
100f7cd8
APB
11293 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11294 TREE_OPERAND (cn, 1));
100f7cd8 11295 }
15fdcfe9 11296
ce6e9147 11297 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11298 {
11299 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11300 parse_error_context (node, "Constant expression required");
11301 return error_mark_node;
11302 }
11303
11304 nn = ctxp->current_loop;
11305
11306 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11307 if (!try_builtin_assignconv (NULL_TREE,
11308 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11309 {
11310 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11311 parse_error_context
11312 (wfl_operator,
11313 "Incompatible type for case. Can't convert `%s' to `int'",
11314 lang_printable_name (TREE_TYPE (cn), 0));
11315 return error_mark_node;
11316 }
11317
11318 cn = fold (convert (int_type_node, cn));
11319
11320 /* Multiple instance of a case label bearing the same
11321 value is checked during code generation. The case
11322 expression is allright so far. */
34d4df06
APB
11323 if (TREE_CODE (cn) == VAR_DECL)
11324 cn = DECL_INITIAL (cn);
15fdcfe9 11325 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11326 TREE_TYPE (node) = void_type_node;
15fdcfe9 11327 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11328 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11329 break;
11330
11331 case DEFAULT_EXPR:
11332 nn = ctxp->current_loop;
11333 /* Only one default label is allowed per switch statement */
11334 if (SWITCH_HAS_DEFAULT (nn))
11335 {
11336 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11337 parse_error_context (wfl_operator,
11338 "Duplicate case label: `default'");
11339 return error_mark_node;
11340 }
11341 else
11342 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11343 TREE_TYPE (node) = void_type_node;
10100cc7 11344 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11345 CAN_COMPLETE_NORMALLY (node) = 1;
11346 break;
11347
b67d701b 11348 case SWITCH_EXPR:
e04a16fb
AG
11349 case LOOP_EXPR:
11350 PUSH_LOOP (node);
11351 /* Check whether the loop was enclosed in a labeled
11352 statement. If not, create one, insert the loop in it and
11353 return the node */
11354 nn = patch_loop_statement (node);
b67d701b 11355
e04a16fb 11356 /* Anyways, walk the body of the loop */
b67d701b
PB
11357 if (TREE_CODE (node) == LOOP_EXPR)
11358 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11359 /* Switch statement: walk the switch expression and the cases */
11360 else
11361 node = patch_switch_statement (node);
11362
e7c7bcef 11363 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11364 nn = error_mark_node;
11365 else
15fdcfe9 11366 {
b635eb2f
PB
11367 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11368 /* If we returned something different, that's because we
11369 inserted a label. Pop the label too. */
11370 if (nn != node)
11371 {
11372 if (CAN_COMPLETE_NORMALLY (node))
11373 CAN_COMPLETE_NORMALLY (nn) = 1;
11374 POP_LABELED_BLOCK ();
11375 }
15fdcfe9 11376 }
e04a16fb
AG
11377 POP_LOOP ();
11378 return nn;
11379
11380 case EXIT_EXPR:
11381 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11382 return patch_exit_expr (node);
11383
11384 case COND_EXPR:
11385 /* Condition */
11386 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11387 if (TREE_OPERAND (node, 0) == error_mark_node)
11388 return error_mark_node;
11389 /* then-else branches */
11390 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11391 if (TREE_OPERAND (node, 1) == error_mark_node)
11392 return error_mark_node;
11393 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11394 if (TREE_OPERAND (node, 2) == error_mark_node)
11395 return error_mark_node;
11396 return patch_if_else_statement (node);
11397 break;
11398
22eed1e6
APB
11399 case CONDITIONAL_EXPR:
11400 /* Condition */
11401 wfl_op1 = TREE_OPERAND (node, 0);
11402 COMPLETE_CHECK_OP_0 (node);
11403 wfl_op2 = TREE_OPERAND (node, 1);
11404 COMPLETE_CHECK_OP_1 (node);
11405 wfl_op3 = TREE_OPERAND (node, 2);
11406 COMPLETE_CHECK_OP_2 (node);
11407 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11408
e04a16fb
AG
11409 /* 3- Expression section */
11410 case COMPOUND_EXPR:
15fdcfe9 11411 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11412 TREE_OPERAND (node, 0) = nn =
11413 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11414 if (wfl_op2 == empty_stmt_node)
11415 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11416 else
15fdcfe9 11417 {
dc0b3eff 11418 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11419 {
dc0b3eff
PB
11420 /* An unreachable condition in a do-while statement
11421 is *not* (technically) an unreachable statement. */
11422 nn = wfl_op2;
11423 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11424 nn = EXPR_WFL_NODE (nn);
11425 if (TREE_CODE (nn) != EXIT_EXPR)
11426 {
11427 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11428 parse_error_context (wfl_operator, "Unreachable statement");
11429 }
bccaf73a 11430 }
dc0b3eff
PB
11431 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11432 if (TREE_OPERAND (node, 1) == error_mark_node)
11433 return error_mark_node;
11434 CAN_COMPLETE_NORMALLY (node)
11435 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11436 }
e04a16fb
AG
11437 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11438 break;
11439
11440 case RETURN_EXPR:
15fdcfe9 11441 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11442 return patch_return (node);
11443
11444 case EXPR_WITH_FILE_LOCATION:
11445 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11446 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11447 {
5423609c 11448 tree wfl = node;
15fdcfe9 11449 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11450 if (node == error_mark_node)
11451 return node;
5423609c
APB
11452 /* Keep line number information somewhere were it doesn't
11453 disrupt the completion process. */
2c56429a 11454 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11455 {
11456 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11457 TREE_OPERAND (node, 1) = wfl;
11458 }
15fdcfe9
PB
11459 CAN_COMPLETE_NORMALLY (node) = 1;
11460 }
e04a16fb
AG
11461 else
11462 {
5b09b33e
PB
11463 tree body;
11464 int save_lineno = lineno;
11465 lineno = EXPR_WFL_LINENO (node);
11466 body = java_complete_tree (EXPR_WFL_NODE (node));
11467 lineno = save_lineno;
15fdcfe9 11468 EXPR_WFL_NODE (node) = body;
dc0b3eff 11469 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11470 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11471 if (body == empty_stmt_node)
11472 {
11473 /* Optimization; makes it easier to detect empty bodies. */
11474 return body;
11475 }
dc0b3eff 11476 if (body == error_mark_node)
e04a16fb
AG
11477 {
11478 /* Its important for the evaluation of assignment that
11479 this mark on the TREE_TYPE is propagated. */
11480 TREE_TYPE (node) = error_mark_node;
11481 return error_mark_node;
11482 }
11483 else
11484 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11485
e04a16fb
AG
11486 }
11487 break;
11488
b67d701b 11489 case NEW_ARRAY_EXPR:
e04a16fb
AG
11490 /* Patch all the dimensions */
11491 flag = 0;
11492 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11493 {
11494 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11495 tree dim = convert (int_type_node,
11496 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11497 if (dim == error_mark_node)
11498 {
11499 flag = 1;
11500 continue;
11501 }
11502 else
11503 {
b9f7e36c 11504 TREE_VALUE (cn) = dim;
e04a16fb
AG
11505 /* Setup the location of the current dimension, for
11506 later error report. */
11507 TREE_PURPOSE (cn) =
11508 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11509 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11510 }
11511 }
11512 /* They complete the array creation expression, if no errors
11513 were found. */
15fdcfe9 11514 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11515 return (flag ? error_mark_node
11516 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11517
c2952b01
APB
11518 case NEW_ANONYMOUS_ARRAY_EXPR:
11519 /* Create the array type if necessary. */
11520 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11521 {
11522 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11523 if (!(type = resolve_type_during_patch (type)))
11524 return error_mark_node;
11525 type = build_array_from_name (type, NULL_TREE,
11526 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11527 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11528 }
11529 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11530 ANONYMOUS_ARRAY_INITIALIZER (node));
11531 if (node == error_mark_node)
11532 return error_mark_node;
11533 CAN_COMPLETE_NORMALLY (node) = 1;
11534 return node;
11535
b67d701b 11536 case NEW_CLASS_EXPR:
e04a16fb 11537 case CALL_EXPR:
b67d701b 11538 /* Complete function's argument(s) first */
e04a16fb
AG
11539 if (complete_function_arguments (node))
11540 return error_mark_node;
11541 else
b9f7e36c 11542 {
22eed1e6
APB
11543 tree decl, wfl = TREE_OPERAND (node, 0);
11544 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11545
c877974e 11546 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11547 NULL_TREE, 0, &decl);
c877974e
APB
11548 if (node == error_mark_node)
11549 return error_mark_node;
11550
11551 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11552 /* If we call this(...), register signature and positions */
11553 if (in_this)
11554 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11555 tree_cons (wfl, decl,
11556 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11557 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11558 return force_evaluation_order (node);
b9f7e36c 11559 }
e04a16fb
AG
11560
11561 case MODIFY_EXPR:
11562 /* Save potential wfls */
11563 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11564 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11565
cd9643f7
PB
11566 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11567 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11568 && DECL_INITIAL (nn) != NULL_TREE)
11569 {
100f7cd8
APB
11570 tree value;
11571
100f7cd8 11572 value = fold_constant_for_init (nn, nn);
c2952b01 11573
cd9643f7
PB
11574 if (value != NULL_TREE)
11575 {
11576 tree type = TREE_TYPE (value);
c2952b01
APB
11577 if (JPRIMITIVE_TYPE_P (type) ||
11578 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11579 return empty_stmt_node;
11580 }
629d4b4d
APB
11581 if (! flag_emit_class_files)
11582 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11583 if (CLASS_FINAL_VARIABLE_P (nn))
11584 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11585 }
e04a16fb 11586 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11587
e04a16fb
AG
11588 if (TREE_OPERAND (node, 0) == error_mark_node)
11589 return error_mark_node;
11590
5cbdba64
APB
11591 flag = COMPOUND_ASSIGN_P (wfl_op2);
11592 if (flag)
e04a16fb 11593 {
c2952b01
APB
11594 /* This might break when accessing outer field from inner
11595 class. TESTME, FIXME */
2aa11e97 11596 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11597
11598 /* Hand stablize the lhs on both places */
e04a16fb 11599 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11600 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11601 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11602
5cbdba64 11603 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11604 /* Now complete the RHS. We write it back later on. */
11605 nn = java_complete_tree (TREE_OPERAND (node, 1));
11606
642f15d1
APB
11607 if ((cn = patch_string (nn)))
11608 nn = cn;
11609
2aa11e97
APB
11610 /* The last part of the rewrite for E1 op= E2 is to have
11611 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11612 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11613 TREE_TYPE (lvalue), nn));
5cbdba64 11614
568aac9c
TT
11615 /* If the assignment is compound and has reference type,
11616 then ensure the LHS has type String and nothing else. */
11617 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11618 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11619 parse_error_context (wfl_op2,
11620 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11621 lang_printable_name (TREE_TYPE (lvalue), 0));
11622
5cbdba64 11623 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11624 }
11625
f8976021 11626 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11627 function to complete this RHS. Note that a NEW_ARRAY_INIT
11628 might have been already fully expanded if created as a result
11629 of processing an anonymous array initializer. We avoid doing
11630 the operation twice by testing whether the node already bears
11631 a type. */
11632 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11633 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11634 TREE_OPERAND (node, 1));
2aa11e97 11635 /* Otherwise we simply complete the RHS */
f8976021
APB
11636 else
11637 nn = java_complete_tree (TREE_OPERAND (node, 1));
11638
e04a16fb 11639 if (nn == error_mark_node)
c0d87ff6 11640 return error_mark_node;
2aa11e97
APB
11641
11642 /* Write back the RHS as we evaluated it. */
e04a16fb 11643 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11644
11645 /* In case we're handling = with a String as a RHS, we need to
11646 produce a String out of the RHS (it might still be a
11647 STRING_CST or a StringBuffer at this stage */
11648 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11649 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11650
11651 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11652 TREE_OPERAND (node, 1))))
11653 {
11654 /* We return error_mark_node if outer_field_access_fix
11655 detects we write into a final. */
11656 if (nn == error_mark_node)
11657 return error_mark_node;
11658 node = nn;
11659 }
11660 else
11661 {
11662 node = patch_assignment (node, wfl_op1, wfl_op2);
11663 /* Reorganize the tree if necessary. */
11664 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11665 || JSTRING_P (TREE_TYPE (node))))
11666 node = java_refold (node);
11667 }
11668
15fdcfe9
PB
11669 CAN_COMPLETE_NORMALLY (node) = 1;
11670 return node;
e04a16fb
AG
11671
11672 case MULT_EXPR:
11673 case PLUS_EXPR:
11674 case MINUS_EXPR:
11675 case LSHIFT_EXPR:
11676 case RSHIFT_EXPR:
11677 case URSHIFT_EXPR:
11678 case BIT_AND_EXPR:
11679 case BIT_XOR_EXPR:
11680 case BIT_IOR_EXPR:
11681 case TRUNC_MOD_EXPR:
c2952b01 11682 case TRUNC_DIV_EXPR:
e04a16fb
AG
11683 case RDIV_EXPR:
11684 case TRUTH_ANDIF_EXPR:
11685 case TRUTH_ORIF_EXPR:
11686 case EQ_EXPR:
11687 case NE_EXPR:
11688 case GT_EXPR:
11689 case GE_EXPR:
11690 case LT_EXPR:
11691 case LE_EXPR:
11692 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11693 knows how to handle those cases. */
11694 wfl_op1 = TREE_OPERAND (node, 0);
11695 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11696
15fdcfe9 11697 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11698 /* Don't complete string nodes if dealing with the PLUS operand. */
11699 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11700 {
11701 nn = java_complete_tree (wfl_op1);
11702 if (nn == error_mark_node)
11703 return error_mark_node;
48a840d9 11704
2aa11e97
APB
11705 TREE_OPERAND (node, 0) = nn;
11706 }
b67d701b 11707 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11708 {
11709 nn = java_complete_tree (wfl_op2);
11710 if (nn == error_mark_node)
11711 return error_mark_node;
48a840d9 11712
2aa11e97
APB
11713 TREE_OPERAND (node, 1) = nn;
11714 }
dc0b3eff 11715 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11716
5e942c50
APB
11717 case INSTANCEOF_EXPR:
11718 wfl_op1 = TREE_OPERAND (node, 0);
11719 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11720 if (flag_emit_xref)
11721 {
11722 TREE_TYPE (node) = boolean_type_node;
11723 return node;
11724 }
5e942c50
APB
11725 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11726
b67d701b 11727 case UNARY_PLUS_EXPR:
e04a16fb
AG
11728 case NEGATE_EXPR:
11729 case TRUTH_NOT_EXPR:
11730 case BIT_NOT_EXPR:
11731 case PREDECREMENT_EXPR:
11732 case PREINCREMENT_EXPR:
11733 case POSTDECREMENT_EXPR:
11734 case POSTINCREMENT_EXPR:
11735 case CONVERT_EXPR:
11736 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11737 how to handle those cases. */
11738 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11739 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11740 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11741 if (TREE_OPERAND (node, 0) == error_mark_node)
11742 return error_mark_node;
4a5f66c3
APB
11743 node = patch_unaryop (node, wfl_op1);
11744 CAN_COMPLETE_NORMALLY (node) = 1;
11745 break;
e04a16fb
AG
11746
11747 case ARRAY_REF:
11748 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11749 how to handle those cases. */
11750 wfl_op1 = TREE_OPERAND (node, 0);
11751 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11752 if (TREE_OPERAND (node, 0) == error_mark_node)
11753 return error_mark_node;
7f1d4866 11754 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11755 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11756 /* The same applies to wfl_op2 */
11757 wfl_op2 = TREE_OPERAND (node, 1);
11758 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11759 if (TREE_OPERAND (node, 1) == error_mark_node)
11760 return error_mark_node;
7f1d4866 11761 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11762 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11763 return patch_array_ref (node);
e04a16fb 11764
63a212ed
PB
11765 case RECORD_TYPE:
11766 return node;;
11767
11768 case COMPONENT_REF:
11769 /* The first step in the re-write of qualified name handling. FIXME.
11770 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11771 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11772 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11773 {
11774 tree name = TREE_OPERAND (node, 1);
11775 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11776 if (field == NULL_TREE)
11777 {
11778 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11779 return error_mark_node;
11780 }
11781 if (! FIELD_STATIC (field))
11782 {
11783 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11784 return error_mark_node;
11785 }
11786 return field;
11787 }
11788 else
11789 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11790 break;
9bbc7d9f 11791
b67d701b 11792 case THIS_EXPR:
e04a16fb
AG
11793 /* Can't use THIS in a static environment */
11794 if (!current_this)
11795 {
11796 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11797 parse_error_context (wfl_operator,
11798 "Keyword `this' used outside allowed context");
e04a16fb
AG
11799 TREE_TYPE (node) = error_mark_node;
11800 return error_mark_node;
11801 }
22eed1e6
APB
11802 if (ctxp->explicit_constructor_p)
11803 {
11804 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11805 parse_error_context
781b0558 11806 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11807 TREE_TYPE (node) = error_mark_node;
11808 return error_mark_node;
11809 }
e04a16fb 11810 return current_this;
c2952b01
APB
11811
11812 case CLASS_LITERAL:
11813 CAN_COMPLETE_NORMALLY (node) = 1;
11814 node = patch_incomplete_class_ref (node);
11815 if (node == error_mark_node)
11816 return error_mark_node;
11817 break;
11818
11819 case INSTANCE_INITIALIZERS_EXPR:
11820 in_instance_initializer++;
11821 node = java_complete_tree (TREE_OPERAND (node, 0));
11822 in_instance_initializer--;
11823 if (node != error_mark_node)
11824 TREE_TYPE (node) = void_type_node;
11825 else
11826 return error_mark_node;
11827 break;
e04a16fb 11828
e04a16fb 11829 default:
15fdcfe9 11830 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11831 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11832 and it's time to turn it into the appropriate String object */
11833 if ((nn = patch_string (node)))
11834 node = nn;
11835 else
11836 fatal ("No case for tree code `%s' - java_complete_tree\n",
11837 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11838 }
11839 return node;
11840}
11841
11842/* Complete function call's argument. Return a non zero value is an
11843 error was found. */
11844
11845static int
11846complete_function_arguments (node)
11847 tree node;
11848{
11849 int flag = 0;
11850 tree cn;
11851
f63991a8 11852 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11853 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11854 {
b67d701b 11855 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11856 parm = java_complete_tree (wfl);
c2952b01 11857
e04a16fb
AG
11858 if (parm == error_mark_node)
11859 {
11860 flag = 1;
11861 continue;
11862 }
b67d701b
PB
11863 /* If have a string literal that we haven't transformed yet or a
11864 crafted string buffer, as a result of use of the the String
11865 `+' operator. Build `parm.toString()' and expand it. */
11866 if ((temp = patch_string (parm)))
b9f7e36c 11867 parm = temp;
5e942c50
APB
11868 /* Inline PRIMTYPE.TYPE read access */
11869 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11870
5e942c50 11871 TREE_VALUE (cn) = parm;
e04a16fb 11872 }
f63991a8 11873 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11874 return flag;
11875}
11876
11877/* Sometimes (for loops and variable initialized during their
11878 declaration), we want to wrap a statement around a WFL and turn it
11879 debugable. */
11880
11881static tree
11882build_debugable_stmt (location, stmt)
11883 int location;
11884 tree stmt;
11885{
11886 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11887 {
11888 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11889 EXPR_WFL_LINECOL (stmt) = location;
11890 }
11891 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11892 return stmt;
11893}
11894
11895static tree
11896build_expr_block (body, decls)
11897 tree body, decls;
11898{
11899 tree node = make_node (BLOCK);
11900 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11901 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11902 if (body)
11903 TREE_TYPE (node) = TREE_TYPE (body);
11904 TREE_SIDE_EFFECTS (node) = 1;
11905 return node;
11906}
11907
b67d701b
PB
11908/* Create a new function block and link it approriately to current
11909 function block chain */
e04a16fb
AG
11910
11911static tree
11912enter_block ()
11913{
b67d701b
PB
11914 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11915}
11916
11917/* Link block B supercontext to the previous block. The current
11918 function DECL is used as supercontext when enter_a_block is called
11919 for the first time for a given function. The current function body
11920 (DECL_FUNCTION_BODY) is set to be block B. */
11921
11922static tree
11923enter_a_block (b)
11924 tree b;
11925{
e04a16fb
AG
11926 tree fndecl = current_function_decl;
11927
f099f336
APB
11928 if (!fndecl) {
11929 BLOCK_SUPERCONTEXT (b) = current_static_block;
11930 current_static_block = b;
11931 }
11932
11933 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11934 {
11935 BLOCK_SUPERCONTEXT (b) = fndecl;
11936 DECL_FUNCTION_BODY (fndecl) = b;
11937 }
11938 else
11939 {
11940 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11941 DECL_FUNCTION_BODY (fndecl) = b;
11942 }
11943 return b;
11944}
11945
11946/* Exit a block by changing the current function body
11947 (DECL_FUNCTION_BODY) to the current block super context, only if
11948 the block being exited isn't the method's top level one. */
11949
11950static tree
11951exit_block ()
11952{
f099f336
APB
11953 tree b;
11954 if (current_function_decl)
11955 {
11956 b = DECL_FUNCTION_BODY (current_function_decl);
11957 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11958 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11959 }
11960 else
11961 {
11962 b = current_static_block;
e04a16fb 11963
f099f336
APB
11964 if (BLOCK_SUPERCONTEXT (b))
11965 current_static_block = BLOCK_SUPERCONTEXT (b);
11966 }
e04a16fb
AG
11967 return b;
11968}
11969
11970/* Lookup for NAME in the nested function's blocks, all the way up to
11971 the current toplevel one. It complies with Java's local variable
11972 scoping rules. */
11973
11974static tree
11975lookup_name_in_blocks (name)
11976 tree name;
11977{
f099f336 11978 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11979
11980 while (b != current_function_decl)
11981 {
11982 tree current;
11983
11984 /* Paranoid sanity check. To be removed */
11985 if (TREE_CODE (b) != BLOCK)
11986 fatal ("non block expr function body - lookup_name_in_blocks");
11987
11988 for (current = BLOCK_EXPR_DECLS (b); current;
11989 current = TREE_CHAIN (current))
11990 if (DECL_NAME (current) == name)
11991 return current;
11992 b = BLOCK_SUPERCONTEXT (b);
11993 }
11994 return NULL_TREE;
11995}
11996
11997static void
11998maybe_absorb_scoping_blocks ()
11999{
f099f336 12000 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
12001 {
12002 tree b = exit_block ();
12003 java_method_add_stmt (current_function_decl, b);
12004 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12005 }
12006}
12007
12008\f
12009/* This section of the source is reserved to build_* functions that
12010 are building incomplete tree nodes and the patch_* functions that
12011 are completing them. */
12012
c2952b01
APB
12013/* Wrap a non WFL node around a WFL. */
12014static tree
9a7ab4b3 12015build_wfl_wrap (node, location)
c2952b01 12016 tree node;
9a7ab4b3 12017 int location;
c2952b01
APB
12018{
12019 tree wfl, node_to_insert = node;
12020
12021 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12022 with the way we're processing SUPER. A THIS from a primary as a
12023 different form than a SUPER. Turn THIS into something symbolic */
12024 if (TREE_CODE (node) == THIS_EXPR)
12025 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12026 else
12027 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12028
9a7ab4b3 12029 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
12030 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12031 return wfl;
12032}
12033
12034
9bbc7d9f 12035/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
12036 we're currently dealing with the class java.lang.Object. */
12037
12038static tree
e920ebc9
APB
12039build_super_invocation (mdecl)
12040 tree mdecl;
22eed1e6 12041{
e920ebc9 12042 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 12043 return empty_stmt_node;
22eed1e6
APB
12044 else
12045 {
9ee9b555 12046 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
12047 tree a = NULL_TREE, t;
12048 /* If we're dealing with an anonymous class, pass the arguments
12049 of the crafted constructor along. */
12050 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12051 {
12052 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12053 for (; t != end_params_node; t = TREE_CHAIN (t))
12054 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12055 }
12056 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12057 }
12058}
12059
12060/* Build a SUPER/THIS qualified method invocation. */
12061
12062static tree
12063build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12064 int use_this;
12065 tree name, args;
12066 int lloc, rloc;
22eed1e6
APB
12067{
12068 tree invok;
12069 tree wfl =
9ee9b555 12070 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12071 EXPR_WFL_LINECOL (wfl) = lloc;
12072 invok = build_method_invocation (name, args);
12073 return make_qualified_primary (wfl, invok, rloc);
12074}
12075
b67d701b 12076/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12077
12078static tree
12079build_method_invocation (name, args)
12080 tree name;
12081 tree args;
12082{
12083 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12084 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12085 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12086 return call;
12087}
12088
12089/* Build an incomplete new xxx(...) node. */
12090
12091static tree
12092build_new_invocation (name, args)
12093 tree name, args;
12094{
12095 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12096 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12097 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12098 return call;
12099}
12100
12101/* Build an incomplete assignment expression. */
12102
12103static tree
12104build_assignment (op, op_location, lhs, rhs)
12105 int op, op_location;
12106 tree lhs, rhs;
12107{
12108 tree assignment;
12109 /* Build the corresponding binop if we deal with a Compound
12110 Assignment operator. Mark the binop sub-tree as part of a
12111 Compound Assignment expression */
12112 if (op != ASSIGN_TK)
12113 {
12114 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12115 COMPOUND_ASSIGN_P (rhs) = 1;
12116 }
12117 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12118 TREE_SIDE_EFFECTS (assignment) = 1;
12119 EXPR_WFL_LINECOL (assignment) = op_location;
12120 return assignment;
12121}
12122
12123/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12124
15fdcfe9 12125char *
e04a16fb
AG
12126print_int_node (node)
12127 tree node;
12128{
12129 static char buffer [80];
12130 if (TREE_CONSTANT_OVERFLOW (node))
12131 sprintf (buffer, "<overflow>");
12132
12133 if (TREE_INT_CST_HIGH (node) == 0)
12134 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12135 TREE_INT_CST_LOW (node));
12136 else if (TREE_INT_CST_HIGH (node) == -1
12137 && TREE_INT_CST_LOW (node) != 0)
12138 {
12139 buffer [0] = '-';
12140 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12141 -TREE_INT_CST_LOW (node));
12142 }
12143 else
12144 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12145 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12146
12147 return buffer;
12148}
12149
c7303e41
APB
12150\f
12151
12152/* This section of the code handle assignment check with FINAL
12153 variables. */
12154
12155static void
12156reset_static_final_variable_assignment_flag (class)
12157 tree class;
12158{
12159 tree field;
12160 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12161 if (CLASS_FINAL_VARIABLE_P (field))
12162 DECL_FIELD_FINAL_LIIC (field) = 0;
12163}
12164
12165/* Figure whether all final static variable have been initialized. */
12166
12167static void
12168check_static_final_variable_assignment_flag (class)
12169 tree class;
12170{
12171 tree field;
12172
12173 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12174 if (CLASS_FINAL_VARIABLE_P (field)
12175 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12176 parse_error_context
12177 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12178 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12179 IDENTIFIER_POINTER (DECL_NAME (field)));
12180}
12181
12182/* This function marks all final variable locally unassigned. */
12183
12184static void
12185reset_final_variable_local_assignment_flag (class)
12186 tree class;
12187{
12188 tree field;
12189 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12190 if (FINAL_VARIABLE_P (field))
12191 DECL_FIELD_FINAL_LIIC (field) = 0;
12192}
12193
12194/* Figure whether all final variables have beem initialized in MDECL
12195 and mark MDECL accordingly. */
12196
12197static void
12198check_final_variable_local_assignment_flag (class, mdecl)
12199 tree class;
12200 tree mdecl;
12201{
12202 tree field;
12203 int initialized = 0;
12204 int non_initialized = 0;
12205
12206 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12207 return;
12208
12209 /* First find out whether all final variables or no final variable
12210 are initialized in this ctor. We don't take into account final
12211 variable that have been initialized upon declaration. */
12212 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12213 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12214 {
12215 if (DECL_FIELD_FINAL_LIIC (field))
12216 initialized++;
12217 else
12218 non_initialized++;
12219 }
12220
12221 /* There were no non initialized variable and no initialized variable.
12222 This ctor is fine. */
12223 if (!non_initialized && !initialized)
12224 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12225 /* If no variables have been initialized, that fine. We'll check
12226 later whether this ctor calls a constructor which initializes
12227 them. We mark the ctor as not initializing all its finals. */
12228 else if (initialized == 0)
12229 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12230 /* If we have a mixed bag, then we have a problem. We need to report
12231 all the variables we're not initializing. */
12232 else if (initialized && non_initialized)
12233 {
12234 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12235 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12236 if (FIELD_FINAL (field)
12237 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12238 {
12239 parse_error_context
12240 (lookup_cl (mdecl),
12241 "Blank final variable `%s' may not have been initialized in this constructor",
12242 IDENTIFIER_POINTER (DECL_NAME (field)));
12243 DECL_FIELD_FINAL_IERR (field) = 1;
12244 }
12245 }
12246 /* Otherwise we know this ctor is initializing all its final
12247 variable. We mark it so. */
12248 else
12249 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12250}
12251
12252/* This function recurses in a simple what through STMT and stops when
12253 it finds a constructor call. It then verifies that the called
12254 constructor initialized its final properly. Return 1 upon success,
12255 0 or -1 otherwise. */
12256
12257static int
12258check_final_variable_indirect_assignment (stmt)
12259 tree stmt;
12260{
12261 int res;
12262 switch (TREE_CODE (stmt))
12263 {
12264 case EXPR_WITH_FILE_LOCATION:
12265 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12266 case COMPOUND_EXPR:
12267 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12268 if (res)
12269 return res;
12270 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12271 case SAVE_EXPR:
12272 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12273 case CALL_EXPR:
12274 {
12275 tree decl = TREE_OPERAND (stmt, 0);
12276 tree fbody;
12277
12278 if (TREE_CODE (decl) != FUNCTION_DECL)
12279 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12280 if (TREE_CODE (decl) != FUNCTION_DECL)
12281 fatal ("Can't find FUNCTION_DECL in CALL_EXPR - check_final_variable_indirect_assignment");
12282 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12283 return 1;
12284 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12285 return -1;
12286 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12287 if (fbody == error_mark_node)
12288 return -1;
12289 fbody = BLOCK_EXPR_BODY (fbody);
12290 return check_final_variable_indirect_assignment (fbody);
12291 }
12292 default:
12293 break;
12294 }
12295 return 0;
12296}
12297
12298/* This is the last chance to catch a final variable initialization
12299 problem. This routine will report an error if a final variable was
12300 never (globally) initialized and never reported as not having been
12301 initialized properly. */
12302
12303static void
12304check_final_variable_global_assignment_flag (class)
12305 tree class;
12306{
12307 tree field, mdecl;
12308 int nnctor = 0;
12309
12310 /* We go through all natural ctors and see whether they're
12311 initializing all their final variables or not. */
12312 current_function_decl = NULL_TREE; /* For the error report. */
12313 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12314 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12315 {
12316 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12317 {
12318 /* It doesn't. Maybe it calls a constructor that initializes
12319 them. find out. */
12320 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12321 if (fbody == error_mark_node)
12322 continue;
12323 fbody = BLOCK_EXPR_BODY (fbody);
12324 if (check_final_variable_indirect_assignment (fbody) == 1)
12325 {
12326 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12327 nnctor++;
12328 }
12329 else
12330 parse_error_context
12331 (lookup_cl (mdecl),
12332 "Final variable initialization error in this constructor");
12333 }
12334 else
12335 nnctor++;
12336 }
12337
12338 /* Finally we catch final variables that never were initialized */
12339 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12340 if (FINAL_VARIABLE_P (field)
12341 /* If the field wasn't initialized upon declaration */
12342 && !DECL_FIELD_FINAL_IUD (field)
12343 /* There wasn't natural ctor in which the field could have been
12344 initialized */
12345 && !nnctor
12346 /* If we never reported a problem with this field */
12347 && !DECL_FIELD_FINAL_IERR (field))
12348 {
12349 current_function_decl = NULL;
12350 parse_error_context
12351 (DECL_FIELD_FINAL_WFL (field),
12352 "Final variable `%s' hasn't been initialized upon its declaration",
12353 IDENTIFIER_POINTER (DECL_NAME (field)));
12354 }
12355
12356}
12357
7f1d4866
APB
12358/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12359 context. */
5e942c50
APB
12360
12361static int
12362check_final_assignment (lvalue, wfl)
12363 tree lvalue, wfl;
12364{
c7303e41
APB
12365 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12366 return 0;
12367
12368 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12369 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12370 lvalue = TREE_OPERAND (lvalue, 1);
12371
c7303e41
APB
12372 if (!FIELD_FINAL (lvalue))
12373 return 0;
12374
12375 /* Now the logic. We can modify a final VARIABLE:
12376 1) in finit$, (its declaration was followed by an initialization,)
12377 2) consistently in each natural ctor, if it wasn't initialized in
12378 finit$ or once in <clinit>. In any other cases, an error should be
12379 reported. */
12380 if (DECL_FINIT_P (current_function_decl))
5e942c50 12381 {
c7303e41
APB
12382 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12383 return 0;
5e942c50 12384 }
c7303e41
APB
12385
12386 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12387 /* Only if it wasn't given a value upon initialization */
12388 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12389 /* If it was never assigned a value in this constructor */
12390 && !DECL_FIELD_FINAL_LIIC (lvalue))
12391 {
12392 /* Turn the locally assigned flag on, it will be checked later
12393 on to point out at discrepancies. */
12394 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12395 if (DECL_CLINIT_P (current_function_decl))
12396 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12397 return 0;
12398 }
12399
12400 /* Other problems should be reported right away. */
12401 parse_error_context
12402 (wfl, "Can't %sassign a value to the final variable `%s'",
12403 (FIELD_STATIC (lvalue) ? "re" : ""),
12404 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12405
12406 /* Note that static field can be initialized once and only once. */
12407 if (FIELD_STATIC (lvalue))
12408 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12409
12410 return 1;
5e942c50
APB
12411}
12412
12413/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12414 read. This is needed to avoid circularities in the implementation
12415 of these fields in libjava. */
12416
12417static tree
12418maybe_build_primttype_type_ref (rhs, wfl)
12419 tree rhs, wfl;
12420{
12421 tree to_return = NULL_TREE;
12422 tree rhs_type = TREE_TYPE (rhs);
12423 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12424 {
12425 tree n = TREE_OPERAND (rhs, 1);
12426 if (TREE_CODE (n) == VAR_DECL
12427 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12428 && rhs_type == class_ptr_type
12429 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12430 {
49f48c71 12431 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12432 if (!strncmp (self_name, "java.lang.", 10))
12433 to_return = build_primtype_type_ref (self_name);
12434 }
12435 }
12436 return (to_return ? to_return : rhs );
12437}
12438
e04a16fb
AG
12439/* 15.25 Assignment operators. */
12440
12441static tree
12442patch_assignment (node, wfl_op1, wfl_op2)
12443 tree node;
12444 tree wfl_op1;
12445 tree wfl_op2;
12446{
0a2138e2 12447 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12448 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12449 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12450 int error_found = 0;
12451 int lvalue_from_array = 0;
12452
c2952b01 12453 /* Can't assign to a (blank) final. */
5e942c50
APB
12454 if (check_final_assignment (lvalue, wfl_op1))
12455 error_found = 1;
e04a16fb
AG
12456
12457 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12458
12459 /* Lhs can be a named variable */
34f4db93 12460 if (JDECL_P (lvalue))
e04a16fb 12461 {
e04a16fb
AG
12462 lhs_type = TREE_TYPE (lvalue);
12463 }
12464 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12465 comment on reason why */
12466 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12467 {
12468 lhs_type = TREE_TYPE (lvalue);
12469 lvalue_from_array = 1;
12470 }
12471 /* Or a field access */
12472 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12473 lhs_type = TREE_TYPE (lvalue);
12474 /* Or a function return slot */
12475 else if (TREE_CODE (lvalue) == RESULT_DECL)
12476 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12477 /* Otherwise, we might want to try to write into an optimized static
12478 final, this is an of a different nature, reported further on. */
12479 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12480 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12481 {
6632dcdd 12482 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12483 {
12484 /* What we should do instead is resetting the all the flags
12485 previously set, exchange lvalue for llvalue and continue. */
12486 error_found = 1;
12487 return error_mark_node;
12488 }
12489 else
12490 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12491 }
12492 else
e04a16fb
AG
12493 {
12494 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12495 error_found = 1;
12496 }
12497
12498 rhs_type = TREE_TYPE (rhs);
b67d701b 12499 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12500 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12501
b67d701b 12502 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12503 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12504 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12505
12506 /* 15.25.2 If we have a compound assignment, convert RHS into the
12507 type of the LHS */
12508 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12509 new_rhs = convert (lhs_type, rhs);
12510
12511 /* Explicit cast required. This is an error */
12512 if (!new_rhs)
12513 {
c2e3db92
KG
12514 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12515 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12516 tree wfl;
12517 char operation [32]; /* Max size known */
12518
12519 /* If the assignment is part of a declaration, we use the WFL of
12520 the declared variable to point out the error and call it a
12521 declaration problem. If the assignment is a genuine =
12522 operator, we call is a operator `=' problem, otherwise we
12523 call it an assignment problem. In both of these last cases,
12524 we use the WFL of the operator to indicate the error. */
12525
12526 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12527 {
12528 wfl = wfl_op1;
12529 strcpy (operation, "declaration");
12530 }
12531 else
12532 {
12533 wfl = wfl_operator;
12534 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12535 strcpy (operation, "assignment");
12536 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12537 strcpy (operation, "`return'");
12538 else
12539 strcpy (operation, "`='");
12540 }
12541
1ebadc60 12542 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12543 parse_error_context
12544 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12545 operation, t1, t2);
1ebadc60 12546 else
781b0558 12547 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12548 operation, t1, t2);
e04a16fb
AG
12549 free (t1); free (t2);
12550 error_found = 1;
12551 }
12552
c877974e
APB
12553 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12554 if (new_rhs)
12555 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12556
e04a16fb
AG
12557 if (error_found)
12558 return error_mark_node;
12559
2622b947
APB
12560 /* 10.10: Array Store Exception runtime check */
12561 if (!flag_emit_class_files
e8fc7396 12562 && !flag_emit_xref
2622b947 12563 && lvalue_from_array
afc390b1 12564 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12565 {
12566 tree check;
12567 tree base = lvalue;
12568
12569 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12570 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12571 base = TREE_OPERAND (lvalue, 0);
12572 else
12573 {
12574 if (flag_bounds_check)
12575 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12576 else
12577 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12578 }
12579
12580 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12581 new_rhs = save_expr (new_rhs);
2622b947
APB
12582 check = build (CALL_EXPR, void_type_node,
12583 build_address_of (soft_checkarraystore_node),
12584 tree_cons (NULL_TREE, base,
12585 build_tree_list (NULL_TREE, new_rhs)),
12586 NULL_TREE);
12587 TREE_SIDE_EFFECTS (check) = 1;
12588
12589 /* We have to decide on an insertion point */
12590 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12591 {
12592 tree t;
12593 if (flag_bounds_check)
12594 {
12595 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12596 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12597 build (COMPOUND_EXPR, void_type_node, t, check);
12598 }
12599 else
12600 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12601 check, TREE_OPERAND (lvalue, 1));
12602 }
12603 else
12604 {
12605 /* Make sure the bound check will happen before the store check */
12606 if (flag_bounds_check)
12607 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12608 build (COMPOUND_EXPR, void_type_node,
12609 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12610 else
12611 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12612 }
12613 }
22eed1e6 12614
34d4df06
APB
12615 /* Final locals can be used as case values in switch
12616 statement. Prepare them for this eventuality. */
12617 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12618 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12619 && TREE_CONSTANT (new_rhs)
12620 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12621 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12622 )
12623 {
12624 TREE_CONSTANT (lvalue) = 1;
12625 DECL_INITIAL (lvalue) = new_rhs;
12626 }
12627
e04a16fb
AG
12628 TREE_OPERAND (node, 0) = lvalue;
12629 TREE_OPERAND (node, 1) = new_rhs;
12630 TREE_TYPE (node) = lhs_type;
12631 return node;
12632}
12633
b67d701b
PB
12634/* Check that type SOURCE can be cast into type DEST. If the cast
12635 can't occur at all, return 0 otherwise 1. This function is used to
12636 produce accurate error messages on the reasons why an assignment
12637 failed. */
e04a16fb 12638
b67d701b
PB
12639static tree
12640try_reference_assignconv (lhs_type, rhs)
12641 tree lhs_type, rhs;
e04a16fb 12642{
b67d701b
PB
12643 tree new_rhs = NULL_TREE;
12644 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12645
b67d701b
PB
12646 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12647 {
12648 /* `null' may be assigned to any reference type */
12649 if (rhs == null_pointer_node)
12650 new_rhs = null_pointer_node;
12651 /* Try the reference assignment conversion */
12652 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12653 new_rhs = rhs;
12654 /* This is a magic assignment that we process differently */
12655 else if (rhs == soft_exceptioninfo_call_node)
12656 new_rhs = rhs;
12657 }
12658 return new_rhs;
12659}
12660
12661/* Check that RHS can be converted into LHS_TYPE by the assignment
12662 conversion (5.2), for the cases of RHS being a builtin type. Return
12663 NULL_TREE if the conversion fails or if because RHS isn't of a
12664 builtin type. Return a converted RHS if the conversion is possible. */
12665
12666static tree
12667try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12668 tree wfl_op1, lhs_type, rhs;
12669{
12670 tree new_rhs = NULL_TREE;
12671 tree rhs_type = TREE_TYPE (rhs);
12672
7e51098e
TT
12673 /* Handle boolean specially. */
12674 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12675 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12676 {
12677 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12678 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12679 new_rhs = rhs;
12680 }
12681
5e942c50 12682 /* Zero accepted everywhere */
7e51098e 12683 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12684 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12685 && JPRIMITIVE_TYPE_P (rhs_type))
12686 new_rhs = convert (lhs_type, rhs);
12687
b67d701b
PB
12688 /* 5.1.1 Try Identity Conversion,
12689 5.1.2 Try Widening Primitive Conversion */
5e942c50 12690 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12691 new_rhs = convert (lhs_type, rhs);
12692
12693 /* Try a narrowing primitive conversion (5.1.3):
12694 - expression is a constant expression of type int AND
12695 - variable is byte, short or char AND
12696 - The value of the expression is representable in the type of the
12697 variable */
12698 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12699 && (lhs_type == byte_type_node || lhs_type == char_type_node
12700 || lhs_type == short_type_node))
12701 {
12702 if (int_fits_type_p (rhs, lhs_type))
12703 new_rhs = convert (lhs_type, rhs);
12704 else if (wfl_op1) /* Might be called with a NULL */
12705 parse_warning_context
7e51098e 12706 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12707 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12708 /* Reported a warning that will turn into an error further
12709 down, so we don't return */
12710 }
12711
12712 return new_rhs;
12713}
12714
12715/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12716 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12717 0 is the conversion test fails. This implements parts the method
12718 invocation convertion (5.3). */
12719
12720static int
12721valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12722 tree lhs_type, rhs_type;
12723{
acd663ee 12724 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12725 if (lhs_type == rhs_type)
12726 return 1;
12727
7e51098e
TT
12728 /* Reject non primitive types and boolean conversions. */
12729 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12730 return 0;
12731
acd663ee
APB
12732 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12733 than a char can't be converted into a char. Short can't too, but
12734 the < test below takes care of that */
b67d701b
PB
12735 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12736 return 0;
12737
5e942c50
APB
12738 /* Accept all promoted type here. Note, we can't use <= in the test
12739 below, because we still need to bounce out assignments of short
12740 to char and the likes */
12741 if (lhs_type == int_type_node
12742 && (rhs_type == promoted_byte_type_node
12743 || rhs_type == promoted_short_type_node
12744 || rhs_type == promoted_char_type_node
12745 || rhs_type == promoted_boolean_type_node))
12746 return 1;
12747
acd663ee
APB
12748 /* From here, an integral is widened if its precision is smaller
12749 than the precision of the LHS or if the LHS is a floating point
12750 type, or the RHS is a float and the RHS a double. */
12751 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12752 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12753 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12754 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12755 return 1;
12756
12757 return 0;
e04a16fb
AG
12758}
12759
12760/* Check that something of SOURCE type can be assigned or cast to
12761 something of DEST type at runtime. Return 1 if the operation is
12762 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12763 were SOURCE is cast into DEST, which borrows a lot of the
12764 assignment check. */
12765
12766static int
12767valid_ref_assignconv_cast_p (source, dest, cast)
12768 tree source;
12769 tree dest;
12770 int cast;
12771{
09ed0f70
APB
12772 /* SOURCE or DEST might be null if not from a declared entity. */
12773 if (!source || !dest)
12774 return 0;
5e942c50
APB
12775 if (JNULLP_TYPE_P (source))
12776 return 1;
e04a16fb
AG
12777 if (TREE_CODE (source) == POINTER_TYPE)
12778 source = TREE_TYPE (source);
12779 if (TREE_CODE (dest) == POINTER_TYPE)
12780 dest = TREE_TYPE (dest);
c1eacb70
BM
12781
12782 /* If source and dest are being compiled from bytecode, they may need to
12783 be loaded. */
12784 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12785 {
12786 load_class (source, 1);
12787 safe_layout_class (source);
12788 }
12789 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12790 {
12791 load_class (dest, 1);
12792 safe_layout_class (dest);
12793 }
12794
e04a16fb
AG
12795 /* Case where SOURCE is a class type */
12796 if (TYPE_CLASS_P (source))
12797 {
12798 if (TYPE_CLASS_P (dest))
c2952b01
APB
12799 return (source == dest
12800 || inherits_from_p (source, dest)
c2952b01 12801 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12802 if (TYPE_INTERFACE_P (dest))
12803 {
12804 /* If doing a cast and SOURCE is final, the operation is
12805 always correct a compile time (because even if SOURCE
12806 does not implement DEST, a subclass of SOURCE might). */
12807 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12808 return 1;
12809 /* Otherwise, SOURCE must implement DEST */
12810 return interface_of_p (dest, source);
12811 }
12812 /* DEST is an array, cast permited if SOURCE is of Object type */
12813 return (cast && source == object_type_node ? 1 : 0);
12814 }
12815 if (TYPE_INTERFACE_P (source))
12816 {
12817 if (TYPE_CLASS_P (dest))
12818 {
12819 /* If not casting, DEST must be the Object type */
12820 if (!cast)
12821 return dest == object_type_node;
12822 /* We're doing a cast. The cast is always valid is class
12823 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12824 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12825 return 1;
12826 else
12827 return interface_of_p (source, dest);
12828 }
12829 if (TYPE_INTERFACE_P (dest))
12830 {
12831 /* If doing a cast, then if SOURCE and DEST contain method
12832 with the same signature but different return type, then
12833 this is a (compile time) error */
12834 if (cast)
12835 {
12836 tree method_source, method_dest;
12837 tree source_type;
0a2138e2 12838 tree source_sig;
e04a16fb
AG
12839 tree source_name;
12840 for (method_source = TYPE_METHODS (source); method_source;
12841 method_source = TREE_CHAIN (method_source))
12842 {
12843 source_sig =
12844 build_java_argument_signature (TREE_TYPE (method_source));
12845 source_type = TREE_TYPE (TREE_TYPE (method_source));
12846 source_name = DECL_NAME (method_source);
12847 for (method_dest = TYPE_METHODS (dest);
12848 method_dest; method_dest = TREE_CHAIN (method_dest))
12849 if (source_sig ==
12850 build_java_argument_signature (TREE_TYPE (method_dest))
12851 && source_name == DECL_NAME (method_dest)
12852 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12853 return 0;
12854 }
12855 return 1;
12856 }
12857 else
12858 return source == dest || interface_of_p (dest, source);
12859 }
ee17a290
TT
12860 else
12861 {
12862 /* Array */
12863 return (cast
12864 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12865 || (DECL_NAME (TYPE_NAME (source))
12866 == java_io_serializable)));
12867 }
e04a16fb
AG
12868 }
12869 if (TYPE_ARRAY_P (source))
12870 {
12871 if (TYPE_CLASS_P (dest))
12872 return dest == object_type_node;
09ed0f70 12873 /* Can't cast an array to an interface unless the interface is
ee17a290 12874 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12875 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12876 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12877 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12878 else /* Arrays */
12879 {
12880 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12881 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12882
b9f7e36c
APB
12883 /* In case of severe errors, they turn out null */
12884 if (!dest_element_type || !source_element_type)
12885 return 0;
e04a16fb
AG
12886 if (source_element_type == dest_element_type)
12887 return 1;
12888 return valid_ref_assignconv_cast_p (source_element_type,
12889 dest_element_type, cast);
12890 }
12891 return 0;
12892 }
12893 return 0;
12894}
12895
b67d701b
PB
12896static int
12897valid_cast_to_p (source, dest)
12898 tree source;
12899 tree dest;
12900{
12901 if (TREE_CODE (source) == POINTER_TYPE)
12902 source = TREE_TYPE (source);
12903 if (TREE_CODE (dest) == POINTER_TYPE)
12904 dest = TREE_TYPE (dest);
12905
12906 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12907 return valid_ref_assignconv_cast_p (source, dest, 1);
12908
12909 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12910 return 1;
12911
7e51098e
TT
12912 else if (TREE_CODE (source) == BOOLEAN_TYPE
12913 && TREE_CODE (dest) == BOOLEAN_TYPE)
12914 return 1;
12915
b67d701b
PB
12916 return 0;
12917}
12918
15fdcfe9
PB
12919static tree
12920do_unary_numeric_promotion (arg)
12921 tree arg;
12922{
12923 tree type = TREE_TYPE (arg);
7e51098e
TT
12924 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12925 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12926 arg = convert (int_type_node, arg);
12927 return arg;
12928}
12929
acd663ee
APB
12930/* Return a non zero value if SOURCE can be converted into DEST using
12931 the method invocation conversion rule (5.3). */
b67d701b
PB
12932static int
12933valid_method_invocation_conversion_p (dest, source)
12934 tree dest, source;
12935{
e3884b71 12936 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12937 && valid_builtin_assignconv_identity_widening_p (dest, source))
12938 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12939 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12940 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12941}
12942
e04a16fb
AG
12943/* Build an incomplete binop expression. */
12944
12945static tree
12946build_binop (op, op_location, op1, op2)
12947 enum tree_code op;
12948 int op_location;
12949 tree op1, op2;
12950{
5e942c50 12951 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12952 TREE_SIDE_EFFECTS (binop) = 1;
12953 /* Store the location of the operator, for better error report. The
12954 string of the operator will be rebuild based on the OP value. */
12955 EXPR_WFL_LINECOL (binop) = op_location;
12956 return binop;
12957}
12958
12959/* Build the string of the operator retained by NODE. If NODE is part
12960 of a compound expression, add an '=' at the end of the string. This
12961 function is called when an error needs to be reported on an
12962 operator. The string is returned as a pointer to a static character
12963 buffer. */
12964
12965static char *
12966operator_string (node)
12967 tree node;
12968{
12969#define BUILD_OPERATOR_STRING(S) \
12970 { \
12971 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12972 return buffer; \
12973 }
12974
12975 static char buffer [10];
12976 switch (TREE_CODE (node))
12977 {
12978 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12979 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12980 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12981 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12982 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12983 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12984 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12985 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12986 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12987 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12988 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12989 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12990 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12991 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12992 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12993 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12994 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12995 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12996 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12997 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12998 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12999 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13000 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13001 case PREINCREMENT_EXPR: /* Fall through */
13002 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13003 case PREDECREMENT_EXPR: /* Fall through */
13004 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13005 default:
13006 fatal ("unregistered operator %s - operator_string",
13007 tree_code_name [TREE_CODE (node)]);
13008 }
13009 return NULL;
13010#undef BUILD_OPERATOR_STRING
13011}
13012
5cbdba64
APB
13013/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13014
13015static int
13016java_decl_equiv (var_acc1, var_acc2)
13017 tree var_acc1, var_acc2;
13018{
13019 if (JDECL_P (var_acc1))
13020 return (var_acc1 == var_acc2);
13021
13022 return (TREE_CODE (var_acc1) == COMPONENT_REF
13023 && TREE_CODE (var_acc2) == COMPONENT_REF
13024 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13025 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13026 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13027}
13028
13029/* Return a non zero value if CODE is one of the operators that can be
13030 used in conjunction with the `=' operator in a compound assignment. */
13031
13032static int
13033binop_compound_p (code)
13034 enum tree_code code;
13035{
13036 int i;
13037 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13038 if (binop_lookup [i] == code)
13039 break;
13040
13041 return i < BINOP_COMPOUND_CANDIDATES;
13042}
13043
13044/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13045
13046static tree
13047java_refold (t)
13048 tree t;
13049{
13050 tree c, b, ns, decl;
13051
13052 if (TREE_CODE (t) != MODIFY_EXPR)
13053 return t;
13054
13055 c = TREE_OPERAND (t, 1);
13056 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13057 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13058 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13059 return t;
13060
13061 /* Now the left branch of the binary operator. */
13062 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13063 if (! (b && TREE_CODE (b) == NOP_EXPR
13064 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13065 return t;
13066
13067 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13068 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13069 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13070 return t;
13071
13072 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13073 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13074 /* It's got to be the an equivalent decl */
13075 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13076 {
13077 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13078 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13079 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13080 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13081 /* Change the right part of the BINOP_EXPR */
13082 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13083 }
13084
13085 return t;
13086}
13087
e04a16fb
AG
13088/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13089 errors but we modify NODE so that it contains the type computed
13090 according to the expression, when it's fixed. Otherwise, we write
13091 error_mark_node as the type. It allows us to further the analysis
13092 of remaining nodes and detects more errors in certain cases. */
13093
13094static tree
13095patch_binop (node, wfl_op1, wfl_op2)
13096 tree node;
13097 tree wfl_op1;
13098 tree wfl_op2;
13099{
13100 tree op1 = TREE_OPERAND (node, 0);
13101 tree op2 = TREE_OPERAND (node, 1);
13102 tree op1_type = TREE_TYPE (op1);
13103 tree op2_type = TREE_TYPE (op2);
48a840d9 13104 tree prom_type = NULL_TREE, cn;
e04a16fb 13105 int code = TREE_CODE (node);
b67d701b 13106
e04a16fb
AG
13107 /* If 1, tell the routine that we have to return error_mark_node
13108 after checking for the initialization of the RHS */
13109 int error_found = 0;
13110
e04a16fb
AG
13111 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13112
e04a16fb
AG
13113 switch (code)
13114 {
13115 /* 15.16 Multiplicative operators */
13116 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13117 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13118 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13119 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13120 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13121 {
7e51098e 13122 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13123 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13124 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13125 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13126 TREE_TYPE (node) = error_mark_node;
13127 error_found = 1;
13128 break;
13129 }
13130 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13131 /* Change the division operator if necessary */
13132 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13133 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13134
aa4759c1
AH
13135 if (TREE_CODE (prom_type) == INTEGER_TYPE
13136 && flag_use_divide_subroutine
13137 && ! flag_emit_class_files
13138 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13139 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13140
0b4d333e
APB
13141 /* This one is more complicated. FLOATs are processed by a
13142 function call to soft_fmod. Duplicate the value of the
13143 COMPOUND_ASSIGN_P flag. */
e04a16fb 13144 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13145 {
13146 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13147 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13148 TREE_SIDE_EFFECTS (mod)
13149 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13150 return mod;
13151 }
e04a16fb
AG
13152 break;
13153
13154 /* 15.17 Additive Operators */
13155 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13156
13157 /* Operation is valid if either one argument is a string
13158 constant, a String object or a StringBuffer crafted for the
13159 purpose of the a previous usage of the String concatenation
13160 operator */
13161
13162 if (TREE_CODE (op1) == STRING_CST
13163 || TREE_CODE (op2) == STRING_CST
13164 || JSTRING_TYPE_P (op1_type)
13165 || JSTRING_TYPE_P (op2_type)
13166 || IS_CRAFTED_STRING_BUFFER_P (op1)
13167 || IS_CRAFTED_STRING_BUFFER_P (op2))
13168 return build_string_concatenation (op1, op2);
13169
e04a16fb
AG
13170 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13171 Numeric Types */
7e51098e 13172 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13173 {
7e51098e 13174 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13175 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13176 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13177 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13178 TREE_TYPE (node) = error_mark_node;
13179 error_found = 1;
13180 break;
13181 }
13182 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13183 break;
13184
13185 /* 15.18 Shift Operators */
13186 case LSHIFT_EXPR:
13187 case RSHIFT_EXPR:
13188 case URSHIFT_EXPR:
13189 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13190 {
13191 if (!JINTEGRAL_TYPE_P (op1_type))
13192 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13193 else
1ebadc60 13194 {
7e51098e 13195 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13196 parse_error_context (wfl_operator,
781b0558 13197 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13198 operator_string (node),
13199 lang_printable_name (op2_type, 0));
13200 else
781b0558
KG
13201 parse_error_context (wfl_operator,
13202 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13203 operator_string (node),
13204 lang_printable_name (op2_type, 0));
13205 }
e04a16fb
AG
13206 TREE_TYPE (node) = error_mark_node;
13207 error_found = 1;
13208 break;
13209 }
13210
13211 /* Unary numeric promotion (5.6.1) is performed on each operand
13212 separatly */
15fdcfe9
PB
13213 op1 = do_unary_numeric_promotion (op1);
13214 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13215
13216 /* The type of the shift expression is the type of the promoted
13217 type of the left-hand operand */
13218 prom_type = TREE_TYPE (op1);
13219
c2952b01
APB
13220 /* Shift int only up to 0x1f and long up to 0x3f */
13221 if (prom_type == int_type_node)
13222 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13223 build_int_2 (0x1f, 0)));
13224 else
13225 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13226 build_int_2 (0x3f, 0)));
e04a16fb
AG
13227
13228 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13229 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13230 {
0b4d333e 13231 tree to_return;
73333a87
AH
13232 tree utype = unsigned_type (prom_type);
13233 op1 = convert (utype, op1);
e04a16fb 13234 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13235 TREE_OPERAND (node, 0) = op1;
13236 TREE_OPERAND (node, 1) = op2;
13237 TREE_TYPE (node) = utype;
0b4d333e
APB
13238 to_return = convert (prom_type, node);
13239 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13240 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13241 TREE_SIDE_EFFECTS (to_return)
13242 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13243 return to_return;
e04a16fb
AG
13244 }
13245 break;
5e942c50
APB
13246
13247 /* 15.19.1 Type Comparison Operator instaceof */
13248 case INSTANCEOF_EXPR:
13249
13250 TREE_TYPE (node) = boolean_type_node;
13251
13252 if (!(op2_type = resolve_type_during_patch (op2)))
13253 return error_mark_node;
13254
13255 /* The first operand must be a reference type or the null type */
13256 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13257 error_found = 1; /* Error reported further below */
13258
13259 /* The second operand must be a reference type */
13260 if (!JREFERENCE_TYPE_P (op2_type))
13261 {
13262 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13263 parse_error_context
13264 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13265 lang_printable_name (op2_type, 0));
13266 error_found = 1;
13267 }
13268
13269 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13270 {
13271 /* If the first operand is null, the result is always false */
13272 if (op1 == null_pointer_node)
13273 return boolean_false_node;
15fdcfe9
PB
13274 else if (flag_emit_class_files)
13275 {
13276 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13277 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13278 return node;
13279 }
5e942c50
APB
13280 /* Otherwise we have to invoke instance of to figure it out */
13281 else
67db0ce7 13282 return build_instanceof (op1, op2_type);
5e942c50
APB
13283 }
13284 /* There is no way the expression operand can be an instance of
13285 the type operand. This is a compile time error. */
13286 else
13287 {
c2e3db92 13288 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13289 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13290 parse_error_context
13291 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13292 t1, lang_printable_name (op2_type, 0));
13293 free (t1);
13294 error_found = 1;
13295 }
e04a16fb 13296
5e942c50 13297 break;
e04a16fb
AG
13298
13299 /* 15.21 Bitwise and Logical Operators */
13300 case BIT_AND_EXPR:
13301 case BIT_XOR_EXPR:
13302 case BIT_IOR_EXPR:
13303 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13304 /* Binary numeric promotion is performed on both operand and the
13305 expression retain that type */
13306 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13307
13308 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13309 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13310 /* The type of the bitwise operator expression is BOOLEAN */
13311 prom_type = boolean_type_node;
13312 else
13313 {
13314 if (!JINTEGRAL_TYPE_P (op1_type))
13315 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13316 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13317 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13318 TREE_TYPE (node) = error_mark_node;
13319 error_found = 1;
13320 /* Insert a break here if adding thing before the switch's
13321 break for this case */
13322 }
13323 break;
13324
13325 /* 15.22 Conditional-And Operator */
13326 case TRUTH_ANDIF_EXPR:
13327 /* 15.23 Conditional-Or Operator */
13328 case TRUTH_ORIF_EXPR:
13329 /* Operands must be of BOOLEAN type */
13330 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13331 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13332 {
13333 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13334 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13335 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13336 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13337 TREE_TYPE (node) = boolean_type_node;
13338 error_found = 1;
13339 break;
13340 }
13341 /* The type of the conditional operators is BOOLEAN */
13342 prom_type = boolean_type_node;
13343 break;
13344
13345 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13346 case LT_EXPR:
13347 case GT_EXPR:
13348 case LE_EXPR:
13349 case GE_EXPR:
13350 /* The type of each of the operands must be a primitive numeric
13351 type */
13352 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13353 {
13354 if (!JNUMERIC_TYPE_P (op1_type))
13355 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13356 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13357 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13358 TREE_TYPE (node) = boolean_type_node;
13359 error_found = 1;
13360 break;
13361 }
13362 /* Binary numeric promotion is performed on the operands */
13363 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13364 /* The type of the relation expression is always BOOLEAN */
13365 prom_type = boolean_type_node;
13366 break;
13367
13368 /* 15.20 Equality Operator */
13369 case EQ_EXPR:
13370 case NE_EXPR:
48a840d9
APB
13371 /* It's time for us to patch the strings. */
13372 if ((cn = patch_string (op1)))
13373 {
13374 op1 = cn;
13375 op1_type = TREE_TYPE (op1);
13376 }
13377 if ((cn = patch_string (op2)))
13378 {
13379 op2 = cn;
13380 op2_type = TREE_TYPE (op2);
13381 }
13382
e04a16fb
AG
13383 /* 15.20.1 Numerical Equality Operators == and != */
13384 /* Binary numeric promotion is performed on the operands */
5e942c50 13385 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13386 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13387
13388 /* 15.20.2 Boolean Equality Operators == and != */
13389 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13390 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13391 ; /* Nothing to do here */
13392
13393 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13394 /* Types have to be either references or the null type. If
13395 they're references, it must be possible to convert either
13396 type to the other by casting conversion. */
b9f7e36c
APB
13397 else if (op1 == null_pointer_node || op2 == null_pointer_node
13398 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13399 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13400 || valid_ref_assignconv_cast_p (op2_type,
13401 op1_type, 1))))
e04a16fb
AG
13402 ; /* Nothing to do here */
13403
13404 /* Else we have an error figure what can't be converted into
13405 what and report the error */
13406 else
13407 {
13408 char *t1;
c2e3db92 13409 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13410 parse_error_context
781b0558
KG
13411 (wfl_operator,
13412 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13413 operator_string (node), t1,
0a2138e2 13414 lang_printable_name (op2_type, 0));
e04a16fb
AG
13415 free (t1);
13416 TREE_TYPE (node) = boolean_type_node;
13417 error_found = 1;
13418 break;
13419 }
13420 prom_type = boolean_type_node;
13421 break;
13422 }
13423
e04a16fb
AG
13424 if (error_found)
13425 return error_mark_node;
13426
13427 TREE_OPERAND (node, 0) = op1;
13428 TREE_OPERAND (node, 1) = op2;
13429 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13430 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13431
ce6e9147
APB
13432 if (flag_emit_xref)
13433 return node;
13434
d1472141
PB
13435 /* fold does not respect side-effect order as required for Java but not C.
13436 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13437 * bytecode.
13438 */
13439 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13440 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13441 node = fold (node);
13442 return node;
e04a16fb
AG
13443}
13444
b67d701b
PB
13445/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13446 zero value, the value of CSTE comes after the valude of STRING */
13447
13448static tree
13449do_merge_string_cste (cste, string, string_len, after)
13450 tree cste;
49f48c71 13451 const char *string;
b67d701b
PB
13452 int string_len, after;
13453{
49f48c71 13454 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13455 int old_len = TREE_STRING_LENGTH (cste);
13456 int len = old_len + string_len;
520a57c8 13457 char *new = alloca (len+1);
354e99ce 13458
b67d701b
PB
13459 if (after)
13460 {
354e99ce
APB
13461 memcpy (new, string, string_len);
13462 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13463 }
13464 else
13465 {
354e99ce
APB
13466 memcpy (new, old, old_len);
13467 memcpy (&new [old_len], string, string_len);
b67d701b 13468 }
354e99ce 13469 new [len] = '\0';
520a57c8 13470 return build_string (len, new);
b67d701b
PB
13471}
13472
13473/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13474 new STRING_CST on success, NULL_TREE on failure */
13475
13476static tree
13477merge_string_cste (op1, op2, after)
13478 tree op1, op2;
13479 int after;
13480{
13481 /* Handle two string constants right away */
13482 if (TREE_CODE (op2) == STRING_CST)
13483 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13484 TREE_STRING_LENGTH (op2), after);
13485
13486 /* Reasonable integer constant can be treated right away */
13487 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13488 {
49f48c71
KG
13489 static const char *boolean_true = "true";
13490 static const char *boolean_false = "false";
13491 static const char *null_pointer = "null";
b67d701b 13492 char ch[3];
49f48c71 13493 const char *string;
b67d701b
PB
13494
13495 if (op2 == boolean_true_node)
13496 string = boolean_true;
13497 else if (op2 == boolean_false_node)
13498 string = boolean_false;
13499 else if (op2 == null_pointer_node)
13500 string = null_pointer;
13501 else if (TREE_TYPE (op2) == char_type_node)
13502 {
13503 ch[0] = (char )TREE_INT_CST_LOW (op2);
13504 ch[1] = '\0';
13505 string = ch;
13506 }
13507 else
13508 string = print_int_node (op2);
13509
13510 return do_merge_string_cste (op1, string, strlen (string), after);
13511 }
13512 return NULL_TREE;
13513}
13514
13515/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13516 has to be a STRING_CST and the other part must be a STRING_CST or a
13517 INTEGRAL constant. Return a new STRING_CST if the operation
13518 succeed, NULL_TREE otherwise.
13519
13520 If the case we want to optimize for space, we might want to return
13521 NULL_TREE for each invocation of this routine. FIXME */
13522
13523static tree
13524string_constant_concatenation (op1, op2)
13525 tree op1, op2;
13526{
13527 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13528 {
0a2138e2 13529 tree string, rest;
b67d701b
PB
13530 int invert;
13531
13532 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13533 rest = (string == op1 ? op2 : op1);
13534 invert = (string == op1 ? 0 : 1 );
13535
13536 /* Walk REST, only if it looks reasonable */
13537 if (TREE_CODE (rest) != STRING_CST
13538 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13539 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13540 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13541 {
13542 rest = java_complete_tree (rest);
13543 if (rest == error_mark_node)
13544 return error_mark_node;
13545 rest = fold (rest);
13546 }
13547 return merge_string_cste (string, rest, invert);
13548 }
13549 return NULL_TREE;
13550}
13551
13552/* Implement the `+' operator. Does static optimization if possible,
13553 otherwise create (if necessary) and append elements to a
13554 StringBuffer. The StringBuffer will be carried around until it is
13555 used for a function call or an assignment. Then toString() will be
13556 called on it to turn it into a String object. */
13557
13558static tree
13559build_string_concatenation (op1, op2)
13560 tree op1, op2;
13561{
13562 tree result;
dc0b3eff 13563 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13564
13565 if (flag_emit_xref)
13566 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13567
13568 /* Try to do some static optimization */
13569 if ((result = string_constant_concatenation (op1, op2)))
13570 return result;
13571
c0d87ff6
PB
13572 /* Discard empty strings on either side of the expression */
13573 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13574 {
13575 op1 = op2;
13576 op2 = NULL_TREE;
13577 }
c0d87ff6 13578 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13579 op2 = NULL_TREE;
b67d701b 13580
acd663ee 13581 /* If operands are string constant, turn then into object references */
b67d701b
PB
13582 if (TREE_CODE (op1) == STRING_CST)
13583 op1 = patch_string_cst (op1);
acd663ee 13584 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13585 op2 = patch_string_cst (op2);
13586
acd663ee
APB
13587 /* If either one of the constant is null and the other non null
13588 operand is a String object, return it. */
13589 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13590 return op1;
13591
b67d701b
PB
13592 /* If OP1 isn't already a StringBuffer, create and
13593 initialize a new one */
13594 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13595 {
13596 /* Two solutions here:
c52b5771
AG
13597 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13598 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13599 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13600 op1 = BUILD_STRING_BUFFER (op1);
13601 else
13602 {
13603 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13604 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13605 }
13606 }
13607
acd663ee
APB
13608 if (op2)
13609 {
13610 /* OP1 is no longer the last node holding a crafted StringBuffer */
13611 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13612 /* Create a node for `{new...,xxx}.append (op2)' */
13613 if (op2)
13614 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13615 }
13616
b67d701b
PB
13617 /* Mark the last node holding a crafted StringBuffer */
13618 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13619
13620 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13621 return op1;
13622}
13623
13624/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13625 StringBuffer. If no string were found to be patched, return
13626 NULL. */
13627
13628static tree
13629patch_string (node)
13630 tree node;
13631{
1179ebc2
APB
13632 if (node == error_mark_node)
13633 return error_mark_node;
b67d701b
PB
13634 if (TREE_CODE (node) == STRING_CST)
13635 return patch_string_cst (node);
13636 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13637 {
c877974e 13638 int saved = ctxp->explicit_constructor_p;
b67d701b 13639 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13640 tree ret;
13641 /* Temporary disable forbid the use of `this'. */
13642 ctxp->explicit_constructor_p = 0;
13643 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13644 /* String concatenation arguments must be evaluated in order too. */
13645 ret = force_evaluation_order (ret);
c877974e
APB
13646 /* Restore it at its previous value */
13647 ctxp->explicit_constructor_p = saved;
13648 return ret;
b67d701b
PB
13649 }
13650 return NULL_TREE;
13651}
13652
13653/* Build the internal representation of a string constant. */
13654
13655static tree
13656patch_string_cst (node)
13657 tree node;
13658{
13659 int location;
15fdcfe9
PB
13660 if (! flag_emit_class_files)
13661 {
15fdcfe9
PB
13662 node = get_identifier (TREE_STRING_POINTER (node));
13663 location = alloc_name_constant (CONSTANT_String, node);
13664 node = build_ref_from_constant_pool (location);
13665 }
cd9643f7 13666 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13667 TREE_CONSTANT (node) = 1;
13668 return node;
13669}
13670
13671/* Build an incomplete unary operator expression. */
e04a16fb
AG
13672
13673static tree
13674build_unaryop (op_token, op_location, op1)
13675 int op_token, op_location;
13676 tree op1;
13677{
13678 enum tree_code op;
13679 tree unaryop;
13680 switch (op_token)
13681 {
b67d701b 13682 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13683 case MINUS_TK: op = NEGATE_EXPR; break;
13684 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13685 case NOT_TK: op = BIT_NOT_EXPR; break;
13686 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13687 op_token);
13688 }
13689
13690 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13691 TREE_SIDE_EFFECTS (unaryop) = 1;
13692 /* Store the location of the operator, for better error report. The
13693 string of the operator will be rebuild based on the OP value. */
13694 EXPR_WFL_LINECOL (unaryop) = op_location;
13695 return unaryop;
13696}
13697
13698/* Special case for the ++/-- operators, since they require an extra
13699 argument to build, which is set to NULL and patched
13700 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13701
13702static tree
13703build_incdec (op_token, op_location, op1, is_post_p)
13704 int op_token, op_location;
13705 tree op1;
13706 int is_post_p;
13707{
13708 static enum tree_code lookup [2][2] =
13709 {
13710 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13711 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13712 };
13713 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13714 NULL_TREE, op1, NULL_TREE);
13715 TREE_SIDE_EFFECTS (node) = 1;
13716 /* Store the location of the operator, for better error report. The
13717 string of the operator will be rebuild based on the OP value. */
13718 EXPR_WFL_LINECOL (node) = op_location;
13719 return node;
13720}
13721
13722/* Build an incomplete cast operator, based on the use of the
13723 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13724 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13725 though its type is already set. */
13726
13727static tree
13728build_cast (location, type, exp)
13729 int location;
13730 tree type, exp;
13731{
13732 tree node = build1 (CONVERT_EXPR, type, exp);
13733 EXPR_WFL_LINECOL (node) = location;
13734 return node;
13735}
13736
c2952b01
APB
13737/* Build an incomplete class reference operator. */
13738static tree
13739build_incomplete_class_ref (location, class_name)
13740 int location;
13741 tree class_name;
13742{
13743 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13744 EXPR_WFL_LINECOL (node) = location;
13745 return node;
13746}
13747
13748/* Complete an incomplete class reference operator. */
13749static tree
13750patch_incomplete_class_ref (node)
13751 tree node;
13752{
13753 tree type = TREE_OPERAND (node, 0);
13754 tree ref_type;
13755
13756 if (!(ref_type = resolve_type_during_patch (type)))
13757 return error_mark_node;
13758
165f37bc 13759 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13760 {
13761 /* A class referenced by `foo.class' is initialized. */
13762 return build_class_init (ref_type, build_class_ref (ref_type));
13763 }
165f37bc
APB
13764
13765 /* If we're emitting class files and we have to deal with non
13766 primitive types, we invoke (and consider generating) the
13767 synthetic static method `class$'. */
13768 if (!TYPE_DOT_CLASS (current_class))
13769 build_dot_class_method (current_class);
f0f3a777 13770 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13771 return java_complete_tree (ref_type);
c2952b01
APB
13772}
13773
e04a16fb
AG
13774/* 15.14 Unary operators. We return error_mark_node in case of error,
13775 but preserve the type of NODE if the type is fixed. */
13776
13777static tree
13778patch_unaryop (node, wfl_op)
13779 tree node;
13780 tree wfl_op;
13781{
13782 tree op = TREE_OPERAND (node, 0);
13783 tree op_type = TREE_TYPE (op);
ab3a6dd6 13784 tree prom_type = NULL_TREE, value, decl;
c2952b01 13785 int outer_field_flag = 0;
e04a16fb
AG
13786 int code = TREE_CODE (node);
13787 int error_found = 0;
13788
13789 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13790
13791 switch (code)
13792 {
13793 /* 15.13.2 Postfix Increment Operator ++ */
13794 case POSTINCREMENT_EXPR:
13795 /* 15.13.3 Postfix Increment Operator -- */
13796 case POSTDECREMENT_EXPR:
13797 /* 15.14.1 Prefix Increment Operator ++ */
13798 case PREINCREMENT_EXPR:
13799 /* 15.14.2 Prefix Decrement Operator -- */
13800 case PREDECREMENT_EXPR:
5cbdba64 13801 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13802 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13803 /* We might be trying to change an outer field accessed using
13804 access method. */
13805 if (outer_field_flag)
13806 {
13807 /* Retrieve the decl of the field we're trying to access. We
13808 do that by first retrieving the function we would call to
13809 access the field. It has been already verified that this
13810 field isn't final */
13811 if (flag_emit_class_files)
13812 decl = TREE_OPERAND (op, 0);
13813 else
13814 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13815 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13816 }
b3edebcf 13817 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13818 else if (!JDECL_P (decl)
b3edebcf
APB
13819 && TREE_CODE (decl) != COMPONENT_REF
13820 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13821 && TREE_CODE (decl) != INDIRECT_REF
13822 && !(TREE_CODE (decl) == COMPOUND_EXPR
13823 && TREE_OPERAND (decl, 1)
13824 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13825 {
5e942c50
APB
13826 tree lvalue;
13827 /* Before screaming, check that we're not in fact trying to
13828 increment a optimized static final access, in which case
13829 we issue an different error message. */
13830 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13831 && resolve_expression_name (wfl_op, &lvalue)
13832 && check_final_assignment (lvalue, wfl_op)))
13833 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13834 operator_string (node));
e04a16fb
AG
13835 TREE_TYPE (node) = error_mark_node;
13836 error_found = 1;
13837 }
c2952b01
APB
13838
13839 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13840 error_found = 1;
13841
e04a16fb
AG
13842 /* From now on, we know that op if a variable and that it has a
13843 valid wfl. We use wfl_op to locate errors related to the
13844 ++/-- operand. */
13845 else if (!JNUMERIC_TYPE_P (op_type))
13846 {
13847 parse_error_context
13848 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13849 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13850 TREE_TYPE (node) = error_mark_node;
13851 error_found = 1;
13852 }
13853 else
13854 {
4a5f66c3 13855 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13856 both operands, if really necessary */
13857 if (JINTEGRAL_TYPE_P (op_type))
13858 {
13859 value = build_int_2 (1, 0);
13860 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13861 }
13862 else
13863 {
13864 value = build_int_2 (1, 0);
13865 TREE_TYPE (node) =
13866 binary_numeric_promotion (op_type,
13867 TREE_TYPE (value), &op, &value);
13868 }
c2952b01
APB
13869
13870 /* We remember we might be accessing an outer field */
13871 if (outer_field_flag)
13872 {
13873 /* We re-generate an access to the field */
13874 value = build (PLUS_EXPR, TREE_TYPE (op),
13875 build_outer_field_access (wfl_op, decl), value);
13876
13877 /* And we patch the original access$() into a write
13878 with plus_op as a rhs */
13879 return outer_field_access_fix (node, op, value);
13880 }
13881
5cbdba64 13882 /* And write back into the node. */
4a5f66c3 13883 TREE_OPERAND (node, 0) = op;
e04a16fb 13884 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13885 /* Convert the overall back into its original type, if
13886 necessary, and return */
13887 if (JINTEGRAL_TYPE_P (op_type))
13888 return fold (node);
13889 else
13890 return fold (convert (op_type, node));
e04a16fb
AG
13891 }
13892 break;
13893
13894 /* 15.14.3 Unary Plus Operator + */
b67d701b 13895 case UNARY_PLUS_EXPR:
e04a16fb
AG
13896 /* 15.14.4 Unary Minus Operator - */
13897 case NEGATE_EXPR:
13898 if (!JNUMERIC_TYPE_P (op_type))
13899 {
13900 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13901 TREE_TYPE (node) = error_mark_node;
13902 error_found = 1;
13903 }
13904 /* Unary numeric promotion is performed on operand */
13905 else
13906 {
15fdcfe9
PB
13907 op = do_unary_numeric_promotion (op);
13908 prom_type = TREE_TYPE (op);
b67d701b 13909 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13910 return fold (op);
e04a16fb
AG
13911 }
13912 break;
13913
13914 /* 15.14.5 Bitwise Complement Operator ~ */
13915 case BIT_NOT_EXPR:
13916 if (!JINTEGRAL_TYPE_P (op_type))
13917 {
13918 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13919 TREE_TYPE (node) = error_mark_node;
13920 error_found = 1;
13921 }
13922 else
13923 {
15fdcfe9
PB
13924 op = do_unary_numeric_promotion (op);
13925 prom_type = TREE_TYPE (op);
e04a16fb
AG
13926 }
13927 break;
13928
13929 /* 15.14.6 Logical Complement Operator ! */
13930 case TRUTH_NOT_EXPR:
13931 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13932 {
13933 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13934 /* But the type is known. We will report an error if further
13935 attempt of a assignment is made with this rhs */
e04a16fb
AG
13936 TREE_TYPE (node) = boolean_type_node;
13937 error_found = 1;
13938 }
13939 else
13940 prom_type = boolean_type_node;
13941 break;
13942
13943 /* 15.15 Cast Expression */
13944 case CONVERT_EXPR:
0a2138e2 13945 value = patch_cast (node, wfl_operator);
e04a16fb 13946 if (value == error_mark_node)
c877974e
APB
13947 {
13948 /* If this cast is part of an assignment, we tell the code
13949 that deals with it not to complain about a mismatch,
13950 because things have been cast, anyways */
13951 TREE_TYPE (node) = error_mark_node;
13952 error_found = 1;
13953 }
13954 else
dc0b3eff
PB
13955 {
13956 value = fold (value);
13957 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13958 return value;
13959 }
e04a16fb
AG
13960 break;
13961 }
13962
e04a16fb
AG
13963 if (error_found)
13964 return error_mark_node;
4a5f66c3
APB
13965
13966 /* There are cases where node has been replaced by something else
13967 and we don't end up returning here: UNARY_PLUS_EXPR,
13968 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13969 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13970 TREE_TYPE (node) = prom_type;
dc0b3eff 13971 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13972 return fold (node);
13973}
13974
13975/* Generic type resolution that sometimes takes place during node
13976 patching. Returned the resolved type or generate an error
13977 message. Return the resolved type or NULL_TREE. */
13978
13979static tree
13980resolve_type_during_patch (type)
13981 tree type;
13982{
13983 if (unresolved_type_p (type, NULL))
13984 {
4142b247 13985 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13986 if (!type_decl)
13987 {
13988 parse_error_context (type,
13989 "Class `%s' not found in type declaration",
13990 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13991 return NULL_TREE;
13992 }
13993 else
5e942c50
APB
13994 {
13995 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13996 return TREE_TYPE (type_decl);
13997 }
e04a16fb
AG
13998 }
13999 return type;
14000}
14001/* 5.5 Casting Conversion. error_mark_node is returned if an error is
14002 found. Otherwise NODE or something meant to replace it is returned. */
14003
14004static tree
19e223db 14005patch_cast (node, wfl_op)
e04a16fb 14006 tree node;
19e223db 14007 tree wfl_op;
e04a16fb
AG
14008{
14009 tree op = TREE_OPERAND (node, 0);
14010 tree op_type = TREE_TYPE (op);
14011 tree cast_type = TREE_TYPE (node);
14012 char *t1;
14013
14014 /* First resolve OP_TYPE if unresolved */
14015 if (!(cast_type = resolve_type_during_patch (cast_type)))
14016 return error_mark_node;
14017
14018 /* Check on cast that are proven correct at compile time */
14019 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14020 {
e04a16fb
AG
14021 /* Same type */
14022 if (cast_type == op_type)
14023 return node;
14024
0b4d333e
APB
14025 /* float and double type are converted to the original type main
14026 variant and then to the target type. */
14027 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14028 op = convert (integer_type_node, op);
14029
e04a16fb
AG
14030 /* Try widening/narowwing convertion. Potentially, things need
14031 to be worked out in gcc so we implement the extreme cases
14032 correctly. fold_convert() needs to be fixed. */
14033 return convert (cast_type, op);
14034 }
14035
0b4d333e
APB
14036 /* It's also valid to cast a boolean into a boolean */
14037 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14038 return node;
14039
5e942c50
APB
14040 /* null can be casted to references */
14041 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14042 return build_null_of_type (cast_type);
14043
e04a16fb
AG
14044 /* The remaining legal casts involve conversion between reference
14045 types. Check for their compile time correctness. */
14046 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14047 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14048 {
14049 TREE_TYPE (node) = promote_type (cast_type);
14050 /* Now, the case can be determined correct at compile time if
14051 OP_TYPE can be converted into CAST_TYPE by assignment
14052 conversion (5.2) */
14053
14054 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14055 {
14056 TREE_SET_CODE (node, NOP_EXPR);
14057 return node;
14058 }
14059
14060 if (flag_emit_class_files)
14061 {
14062 TREE_SET_CODE (node, CONVERT_EXPR);
14063 return node;
14064 }
e04a16fb
AG
14065
14066 /* The cast requires a run-time check */
14067 return build (CALL_EXPR, promote_type (cast_type),
14068 build_address_of (soft_checkcast_node),
14069 tree_cons (NULL_TREE, build_class_ref (cast_type),
14070 build_tree_list (NULL_TREE, op)),
14071 NULL_TREE);
14072 }
14073
14074 /* Any other casts are proven incorrect at compile time */
c2e3db92 14075 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14076 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14077 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14078 free (t1);
14079 return error_mark_node;
14080}
14081
5e942c50
APB
14082/* Build a null constant and give it the type TYPE. */
14083
14084static tree
14085build_null_of_type (type)
14086 tree type;
14087{
14088 tree node = build_int_2 (0, 0);
14089 TREE_TYPE (node) = promote_type (type);
14090 return node;
14091}
14092
e04a16fb
AG
14093/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14094 a list of indices. */
14095static tree
14096build_array_ref (location, array, index)
14097 int location;
14098 tree array, index;
14099{
14100 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14101 EXPR_WFL_LINECOL (node) = location;
14102 return node;
14103}
14104
14105/* 15.12 Array Access Expression */
14106
14107static tree
c877974e
APB
14108patch_array_ref (node)
14109 tree node;
e04a16fb
AG
14110{
14111 tree array = TREE_OPERAND (node, 0);
14112 tree array_type = TREE_TYPE (array);
14113 tree index = TREE_OPERAND (node, 1);
14114 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14115 int error_found = 0;
14116
14117 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14118
e04a16fb
AG
14119 if (TREE_CODE (array_type) == POINTER_TYPE)
14120 array_type = TREE_TYPE (array_type);
14121
14122 /* The array reference must be an array */
14123 if (!TYPE_ARRAY_P (array_type))
14124 {
14125 parse_error_context
781b0558
KG
14126 (wfl_operator,
14127 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14128 lang_printable_name (array_type, 0));
e04a16fb
AG
14129 TREE_TYPE (node) = error_mark_node;
14130 error_found = 1;
14131 }
14132
c2952b01 14133 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14134 type must be int */
15fdcfe9
PB
14135 index = do_unary_numeric_promotion (index);
14136 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14137 {
1ebadc60 14138 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14139 parse_error_context (wfl_operator,
14140 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14141 lang_printable_name (index_type, 0));
14142 else
781b0558
KG
14143 parse_error_context (wfl_operator,
14144 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14145 lang_printable_name (index_type, 0));
e04a16fb
AG
14146 TREE_TYPE (node) = error_mark_node;
14147 error_found = 1;
14148 }
14149
e04a16fb
AG
14150 if (error_found)
14151 return error_mark_node;
e04a16fb 14152
5e942c50 14153 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14154
7f1d4866 14155 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14156 {
15fdcfe9
PB
14157 TREE_OPERAND (node, 0) = array;
14158 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14159 }
14160 else
939d7216
PB
14161 {
14162 /* The save_expr is for correct evaluation order. It would be cleaner
14163 to use force_evaluation_order (see comment there), but that is
14164 difficult when we also have to deal with bounds checking. */
14165 if (TREE_SIDE_EFFECTS (index))
14166 array = save_expr (array);
14167 node = build_java_arrayaccess (array, array_type, index);
14168 if (TREE_SIDE_EFFECTS (index))
14169 node = build (COMPOUND_EXPR, array_type, array, node);
14170 }
e04a16fb
AG
14171 TREE_TYPE (node) = array_type;
14172 return node;
14173}
14174
14175/* 15.9 Array Creation Expressions */
14176
14177static tree
14178build_newarray_node (type, dims, extra_dims)
14179 tree type;
14180 tree dims;
14181 int extra_dims;
14182{
14183 tree node =
b67d701b 14184 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14185 build_int_2 (extra_dims, 0));
e04a16fb
AG
14186 return node;
14187}
14188
14189static tree
14190patch_newarray (node)
14191 tree node;
14192{
14193 tree type = TREE_OPERAND (node, 0);
14194 tree dims = TREE_OPERAND (node, 1);
14195 tree cdim, array_type;
14196 int error_found = 0;
14197 int ndims = 0;
14198 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14199
14200 /* Dimension types are verified. It's better for the types to be
14201 verified in order. */
14202 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14203 {
14204 int dim_error = 0;
14205 tree dim = TREE_VALUE (cdim);
14206
14207 /* Dim might have been saved during its evaluation */
dba41d30 14208 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14209
14210 /* The type of each specified dimension must be an integral type. */
14211 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14212 dim_error = 1;
14213
14214 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14215 promoted type must be int. */
14216 else
14217 {
15fdcfe9 14218 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14219 if (TREE_TYPE (dim) != int_type_node)
14220 dim_error = 1;
14221 }
14222
14223 /* Report errors on types here */
14224 if (dim_error)
14225 {
14226 parse_error_context
14227 (TREE_PURPOSE (cdim),
781b0558 14228 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14229 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14230 "Explicit cast needed to" : "Can't"),
0a2138e2 14231 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14232 error_found = 1;
14233 }
14234
e04a16fb
AG
14235 TREE_PURPOSE (cdim) = NULL_TREE;
14236 }
14237
14238 /* Resolve array base type if unresolved */
14239 if (!(type = resolve_type_during_patch (type)))
14240 error_found = 1;
14241
14242 if (error_found)
14243 {
14244 /* We don't want further evaluation of this bogus array creation
14245 operation */
14246 TREE_TYPE (node) = error_mark_node;
14247 return error_mark_node;
14248 }
14249
15fdcfe9
PB
14250 /* Set array_type to the actual (promoted) array type of the result. */
14251 if (TREE_CODE (type) == RECORD_TYPE)
14252 type = build_pointer_type (type);
14253 while (--xdims >= 0)
14254 {
14255 type = promote_type (build_java_array_type (type, -1));
14256 }
14257 dims = nreverse (dims);
14258 array_type = type;
14259 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14260 {
14261 type = array_type;
05bccae2
RK
14262 array_type
14263 = build_java_array_type (type,
14264 TREE_CODE (cdim) == INTEGER_CST
14265 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14266 : -1);
15fdcfe9
PB
14267 array_type = promote_type (array_type);
14268 }
14269 dims = nreverse (dims);
14270
e04a16fb
AG
14271 /* The node is transformed into a function call. Things are done
14272 differently according to the number of dimensions. If the number
14273 of dimension is equal to 1, then the nature of the base type
14274 (primitive or not) matters. */
15fdcfe9 14275 if (ndims == 1)
fdec99c6 14276 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14277
e04a16fb
AG
14278 /* Can't reuse what's already written in expr.c because it uses the
14279 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14280 return build (CALL_EXPR, array_type,
e04a16fb 14281 build_address_of (soft_multianewarray_node),
15fdcfe9 14282 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14283 tree_cons (NULL_TREE,
15fdcfe9 14284 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14285 NULL_TREE);
14286}
14287
f8976021
APB
14288/* 10.6 Array initializer. */
14289
14290/* Build a wfl for array element that don't have one, so we can
14291 pin-point errors. */
14292
14293static tree
14294maybe_build_array_element_wfl (node)
14295 tree node;
14296{
14297 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14298 return build_expr_wfl (NULL_TREE, ctxp->filename,
14299 ctxp->elc.line, ctxp->elc.prev_col);
14300 else
14301 return NULL_TREE;
14302}
14303
14304/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14305 identification of initialized arrays easier to detect during walk
14306 and expansion. */
14307
14308static tree
14309build_new_array_init (location, values)
14310 int location;
14311 tree values;
14312{
14313 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14314 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14315 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14316 return to_return;
14317}
14318
14319/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14320 occurred. Otherwise return NODE after having set its type
14321 appropriately. */
14322
14323static tree
14324patch_new_array_init (type, node)
14325 tree type, node;
f8976021
APB
14326{
14327 int error_seen = 0;
fdec99c6 14328 tree current, element_type;
f8976021 14329 HOST_WIDE_INT length;
fdec99c6
PB
14330 int all_constant = 1;
14331 tree init = TREE_OPERAND (node, 0);
f8976021 14332
fdec99c6
PB
14333 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14334 {
14335 parse_error_context (node,
14336 "Invalid array initializer for non-array type `%s'",
14337 lang_printable_name (type, 1));
14338 return error_mark_node;
14339 }
14340 type = TREE_TYPE (type);
14341 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14342
fdec99c6
PB
14343 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14344
14345 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14346 current; length++, current = TREE_CHAIN (current))
f8976021 14347 {
fdec99c6
PB
14348 tree elt = TREE_VALUE (current);
14349 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14350 {
fdec99c6 14351 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14352 elt = TREE_VALUE (current);
14353 /* When compiling to native code, STRING_CST is converted to
14354 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14355 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14356 all_constant = 0;
f8976021 14357 }
fdec99c6
PB
14358 else
14359 {
14360 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14361 TREE_PURPOSE (current) = NULL_TREE;
14362 all_constant = 0;
14363 }
9a7ab4b3
APB
14364 if (elt && TREE_CODE (elt) == TREE_LIST
14365 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14366 error_seen = 1;
f8976021
APB
14367 }
14368
14369 if (error_seen)
14370 return error_mark_node;
14371
14372 /* Create a new type. We can't reuse the one we have here by
14373 patching its dimension because it originally is of dimension -1
14374 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14375 type = build_java_array_type (element_type, length);
14376 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14377 TREE_TYPE (node) = promote_type (type);
14378 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14379 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14380 return node;
14381}
14382
14383/* Verify that one entry of the initializer element list can be
14384 assigned to the array base type. Report 1 if an error occurred, 0
14385 otherwise. */
14386
14387static int
14388array_constructor_check_entry (type, entry)
14389 tree type, entry;
14390{
14391 char *array_type_string = NULL; /* For error reports */
14392 tree value, type_value, new_value, wfl_value, patched;
14393 int error_seen = 0;
14394
14395 new_value = NULL_TREE;
14396 wfl_value = TREE_VALUE (entry);
14397
f8976021 14398 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14399 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14400 if ((patched = patch_string (value)))
14401 value = patched;
1179ebc2
APB
14402 if (value == error_mark_node)
14403 return 1;
f8976021 14404
f8976021
APB
14405 type_value = TREE_TYPE (value);
14406
1179ebc2 14407 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14408 constant overflow during narrowing. */
14409 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14410 new_value = try_builtin_assignconv (wfl_operator, type, value);
14411 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14412 type_value = promote_type (type);
100f7cd8 14413
f8976021
APB
14414 /* Check and report errors */
14415 if (!new_value)
14416 {
49f48c71 14417 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14418 "Can't" : "Explicit cast needed to");
14419 if (!array_type_string)
c2e3db92 14420 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14421 parse_error_context
14422 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14423 msg, lang_printable_name (type_value, 1), array_type_string);
14424 error_seen = 1;
14425 }
14426
14427 if (new_value)
14428 {
b8c5b1c6 14429 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14430 TREE_VALUE (entry) = new_value;
14431 }
14432
14433 if (array_type_string)
14434 free (array_type_string);
14435
14436 TREE_PURPOSE (entry) = NULL_TREE;
14437 return error_seen;
14438}
14439
e04a16fb
AG
14440static tree
14441build_this (location)
14442 int location;
14443{
9ee9b555 14444 tree node = build_wfl_node (this_identifier_node);
b67d701b 14445 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14446 EXPR_WFL_LINECOL (node) = location;
14447 return node;
14448}
14449
14450/* 14.15 The return statement. It builds a modify expression that
14451 assigns the returned value to the RESULT_DECL that hold the value
14452 to be returned. */
14453
14454static tree
14455build_return (location, op)
14456 int location;
14457 tree op;
14458{
14459 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14460 EXPR_WFL_LINECOL (node) = location;
b67d701b 14461 node = build_debugable_stmt (location, node);
e04a16fb
AG
14462 return node;
14463}
14464
14465static tree
14466patch_return (node)
14467 tree node;
14468{
14469 tree return_exp = TREE_OPERAND (node, 0);
14470 tree meth = current_function_decl;
14471 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14472 int error_found = 0;
14473
14474 TREE_TYPE (node) = error_mark_node;
14475 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14476
14477 /* It's invalid to have a return value within a function that is
14478 declared with the keyword void or that is a constructor */
14479 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14480 error_found = 1;
14481
f099f336 14482 /* It's invalid to use a return statement in a static block */
c2952b01 14483 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14484 error_found = 1;
14485
e04a16fb
AG
14486 /* It's invalid to have a no return value within a function that
14487 isn't declared with the keyword `void' */
14488 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14489 error_found = 2;
c2952b01
APB
14490
14491 if (in_instance_initializer)
14492 error_found = 1;
e04a16fb
AG
14493
14494 if (error_found)
14495 {
c2952b01 14496 if (in_instance_initializer)
f099f336 14497 parse_error_context (wfl_operator,
c2952b01
APB
14498 "`return' inside instance initializer");
14499
14500 else if (DECL_CLINIT_P (current_function_decl))
14501 parse_error_context (wfl_operator,
14502 "`return' inside static initializer");
f099f336
APB
14503
14504 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14505 {
c2e3db92 14506 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14507 parse_error_context (wfl_operator,
14508 "`return' with%s value from `%s %s'",
14509 (error_found == 1 ? "" : "out"),
14510 t, lang_printable_name (meth, 0));
14511 free (t);
14512 }
14513 else
14514 parse_error_context (wfl_operator,
14515 "`return' with value from constructor `%s'",
14516 lang_printable_name (meth, 0));
e04a16fb
AG
14517 return error_mark_node;
14518 }
14519
5e942c50
APB
14520 /* If we have a return_exp, build a modify expression and expand
14521 it. Note: at that point, the assignment is declared valid, but we
14522 may want to carry some more hacks */
e04a16fb
AG
14523 if (return_exp)
14524 {
5e942c50
APB
14525 tree exp = java_complete_tree (return_exp);
14526 tree modify, patched;
14527
14528 /* If the function returned value and EXP are booleans, EXP has
14529 to be converted into the type of DECL_RESULT, which is integer
14530 (see complete_start_java_method) */
14531 if (TREE_TYPE (exp) == boolean_type_node &&
14532 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14533 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14534
14535 /* `null' can be assigned to a function returning a reference */
14536 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14537 exp == null_pointer_node)
14538 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14539
14540 if ((patched = patch_string (exp)))
14541 exp = patched;
14542
14543 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14544 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14545 modify = java_complete_tree (modify);
5e942c50 14546
e04a16fb
AG
14547 if (modify != error_mark_node)
14548 {
14549 TREE_SIDE_EFFECTS (modify) = 1;
14550 TREE_OPERAND (node, 0) = modify;
14551 }
14552 else
14553 return error_mark_node;
14554 }
14555 TREE_TYPE (node) = void_type_node;
14556 TREE_SIDE_EFFECTS (node) = 1;
14557 return node;
14558}
14559
14560/* 14.8 The if Statement */
14561
14562static tree
14563build_if_else_statement (location, expression, if_body, else_body)
14564 int location;
14565 tree expression, if_body, else_body;
14566{
14567 tree node;
e04a16fb 14568 if (!else_body)
9bbc7d9f 14569 else_body = empty_stmt_node;
e04a16fb
AG
14570 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14571 EXPR_WFL_LINECOL (node) = location;
b67d701b 14572 node = build_debugable_stmt (location, node);
e04a16fb
AG
14573 return node;
14574}
14575
14576static tree
14577patch_if_else_statement (node)
14578 tree node;
14579{
14580 tree expression = TREE_OPERAND (node, 0);
14581
14582 TREE_TYPE (node) = error_mark_node;
14583 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14584
14585 /* The type of expression must be boolean */
b67d701b
PB
14586 if (TREE_TYPE (expression) != boolean_type_node
14587 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14588 {
14589 parse_error_context
14590 (wfl_operator,
14591 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14592 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14593 return error_mark_node;
14594 }
14595
14596 TREE_TYPE (node) = void_type_node;
14597 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14598 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14599 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14600 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14601 return node;
14602}
14603
14604/* 14.6 Labeled Statements */
14605
14606/* Action taken when a lableled statement is parsed. a new
14607 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14608 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14609
14610static tree
0a2138e2 14611build_labeled_block (location, label)
e04a16fb 14612 int location;
0a2138e2 14613 tree label;
e04a16fb 14614{
b635eb2f 14615 tree label_name ;
e04a16fb 14616 tree label_decl, node;
b635eb2f
PB
14617 if (label == NULL_TREE || label == continue_identifier_node)
14618 label_name = label;
14619 else
e04a16fb 14620 {
b635eb2f
PB
14621 label_name = merge_qualified_name (label_id, label);
14622 /* Issue an error if we try to reuse a label that was previously
14623 declared */
14624 if (IDENTIFIER_LOCAL_VALUE (label_name))
14625 {
14626 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14627 parse_error_context (wfl_operator,
14628 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14629 IDENTIFIER_POINTER (label));
14630 EXPR_WFL_LINECOL (wfl_operator) =
14631 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14632 parse_error_context (wfl_operator,
14633 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14634 IDENTIFIER_POINTER (label));
14635 java_error_count--;
14636 }
e04a16fb
AG
14637 }
14638
14639 label_decl = create_label_decl (label_name);
14640 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14641 EXPR_WFL_LINECOL (node) = location;
14642 TREE_SIDE_EFFECTS (node) = 1;
14643 return node;
14644}
14645
b67d701b 14646/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14647
14648static tree
b635eb2f 14649finish_labeled_statement (lbe, statement)
e04a16fb
AG
14650 tree lbe; /* Labeled block expr */
14651 tree statement;
14652{
14653 /* In anyways, tie the loop to its statement */
14654 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14655 pop_labeled_block ();
14656 POP_LABELED_BLOCK ();
e04a16fb
AG
14657 return lbe;
14658}
14659
14660/* 14.10, 14.11, 14.12 Loop Statements */
14661
14662/* Create an empty LOOP_EXPR and make it the last in the nested loop
14663 list. */
14664
14665static tree
14666build_new_loop (loop_body)
14667 tree loop_body;
14668{
14669 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14670 TREE_SIDE_EFFECTS (loop) = 1;
14671 PUSH_LOOP (loop);
14672 return loop;
14673}
14674
14675/* Create a loop body according to the following structure:
14676 COMPOUND_EXPR
14677 COMPOUND_EXPR (loop main body)
14678 EXIT_EXPR (this order is for while/for loops.
14679 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14680 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14681 BODY end of this labeled block)
14682 INCREMENT (if any)
14683
14684 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14685 after the body, like in the do-while loop.
14686
14687 To obtain a loop, the loop body structure described above is
14688 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14689
14690 LABELED_BLOCK_EXPR
14691 LABEL_DECL (use this label to exit the loop)
14692 LOOP_EXPR
14693 <structure described above> */
e04a16fb
AG
14694
14695static tree
14696build_loop_body (location, condition, reversed)
14697 int location;
14698 tree condition;
14699 int reversed;
14700{
0a2138e2 14701 tree first, second, body;
e04a16fb
AG
14702
14703 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14704 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14705 condition = build_debugable_stmt (location, condition);
14706 TREE_SIDE_EFFECTS (condition) = 1;
14707
b635eb2f 14708 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14709 first = (reversed ? body : condition);
14710 second = (reversed ? condition : body);
14711 return
14712 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14713 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14714}
14715
14716/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14717 their order) on the current loop. Unlink the current loop from the
14718 loop list. */
14719
14720static tree
b635eb2f 14721finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14722 int location;
14723 tree condition, body;
14724 int reversed;
14725{
14726 tree to_return = ctxp->current_loop;
14727 tree loop_body = LOOP_EXPR_BODY (to_return);
14728 if (condition)
14729 {
14730 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14731 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14732 The real EXIT_EXPR is one operand further. */
14733 EXPR_WFL_LINECOL (cnode) = location;
14734 /* This one is for accurate error reports */
14735 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14736 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14737 }
14738 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14739 POP_LOOP ();
14740 return to_return;
14741}
14742
b635eb2f 14743/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14744 loops feature the condition part */
14745
14746static tree
b635eb2f 14747finish_for_loop (location, condition, update, body)
e04a16fb
AG
14748 int location;
14749 tree condition, update, body;
14750{
14751 /* Put the condition and the loop body in place */
b635eb2f 14752 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14753 /* LOOP is the current loop which has been now popped of the loop
14754 stack. Install the update block */
14755 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14756 return loop;
14757}
14758
5cbdba64
APB
14759/* Try to find the loop a block might be related to. This comprises
14760 the case where the LOOP_EXPR is found as the second operand of a
14761 COMPOUND_EXPR, because the loop happens to have an initialization
14762 part, then expressed as the first operand of the COMPOUND_EXPR. If
14763 the search finds something, 1 is returned. Otherwise, 0 is
14764 returned. The search is assumed to start from a
14765 LABELED_BLOCK_EXPR's block. */
14766
14767static tree
14768search_loop (statement)
14769 tree statement;
14770{
14771 if (TREE_CODE (statement) == LOOP_EXPR)
14772 return statement;
14773
14774 if (TREE_CODE (statement) == BLOCK)
14775 statement = BLOCK_SUBBLOCKS (statement);
14776 else
14777 return NULL_TREE;
14778
14779 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14780 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14781 statement = TREE_OPERAND (statement, 1);
14782
14783 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14784 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14785}
14786
14787/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14788 returned otherwise. */
14789
14790static int
14791labeled_block_contains_loop_p (block, loop)
14792 tree block, loop;
14793{
14794 if (!block)
14795 return 0;
14796
14797 if (LABELED_BLOCK_BODY (block) == loop)
14798 return 1;
14799
c2952b01 14800 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14801 return 1;
14802
14803 return 0;
14804}
14805
e04a16fb 14806/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14807 insert LOOP as its body. */
e04a16fb
AG
14808
14809static tree
14810patch_loop_statement (loop)
14811 tree loop;
14812{
cd9643f7 14813 tree loop_label;
5cbdba64 14814
cd9643f7 14815 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14816 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14817 return loop;
14818
cd9643f7 14819 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14820 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14821 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14822 LABELED_BLOCK_BODY (loop_label) = loop;
14823 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14824 return loop_label;
e04a16fb
AG
14825}
14826
14827/* 14.13, 14.14: break and continue Statements */
14828
14829/* Build a break or a continue statement. a null NAME indicates an
14830 unlabeled break/continue statement. */
14831
14832static tree
14833build_bc_statement (location, is_break, name)
14834 int location, is_break;
14835 tree name;
14836{
14837 tree break_continue, label_block_expr = NULL_TREE;
14838
14839 if (name)
14840 {
14841 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14842 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14843 /* Null means that we don't have a target for this named
14844 break/continue. In this case, we make the target to be the
14845 label name, so that the error can be reported accuratly in
14846 patch_bc_statement. */
14847 label_block_expr = EXPR_WFL_NODE (name);
14848 }
14849 /* Unlabeled break/continue will be handled during the
14850 break/continue patch operation */
14851 break_continue
14852 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14853
14854 IS_BREAK_STMT_P (break_continue) = is_break;
14855 TREE_SIDE_EFFECTS (break_continue) = 1;
14856 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14857 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14858 return break_continue;
14859}
14860
14861/* Verification of a break/continue statement. */
14862
14863static tree
14864patch_bc_statement (node)
14865 tree node;
14866{
14867 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14868 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14869 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14870
e04a16fb 14871 /* Having an identifier here means that the target is unknown. */
b635eb2f 14872 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14873 {
14874 parse_error_context (wfl_operator, "No label definition found for `%s'",
14875 IDENTIFIER_POINTER (bc_label));
14876 return error_mark_node;
14877 }
b635eb2f 14878 if (! IS_BREAK_STMT_P (node))
e04a16fb 14879 {
b635eb2f
PB
14880 /* It's a continue statement. */
14881 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14882 {
b635eb2f
PB
14883 if (labeled_block == NULL_TREE)
14884 {
14885 if (bc_label == NULL_TREE)
14886 parse_error_context (wfl_operator,
14887 "`continue' must be in loop");
14888 else
1504b2b4
APB
14889 parse_error_context
14890 (wfl_operator, "continue label `%s' does not name a loop",
14891 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14892 return error_mark_node;
14893 }
14894 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14895 == continue_identifier_node)
14896 && (bc_label == NULL_TREE
14897 || TREE_CHAIN (labeled_block) == bc_label))
14898 {
14899 bc_label = labeled_block;
14900 break;
14901 }
e04a16fb 14902 }
e04a16fb 14903 }
b635eb2f 14904 else if (!bc_label)
34f4db93 14905 {
b635eb2f 14906 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14907 {
b635eb2f
PB
14908 if (labeled_block == NULL_TREE)
14909 {
14910 parse_error_context (wfl_operator,
14911 "`break' must be in loop or switch");
14912 return error_mark_node;
14913 }
14914 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14915 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14916 || search_loop (target_stmt))
b635eb2f
PB
14917 {
14918 bc_label = labeled_block;
14919 break;
14920 }
e04a16fb 14921 }
e04a16fb
AG
14922 }
14923
b635eb2f 14924 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14925 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14926
e04a16fb
AG
14927 /* Our break/continue don't return values. */
14928 TREE_TYPE (node) = void_type_node;
14929 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14930 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14931 sometimes, like after a if statement) */
14932 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14933 TREE_SIDE_EFFECTS (node) = 1;
14934 return node;
14935}
14936
14937/* Process the exit expression belonging to a loop. Its type must be
14938 boolean. */
14939
14940static tree
14941patch_exit_expr (node)
14942 tree node;
14943{
14944 tree expression = TREE_OPERAND (node, 0);
14945 TREE_TYPE (node) = error_mark_node;
14946 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14947
14948 /* The type of expression must be boolean */
14949 if (TREE_TYPE (expression) != boolean_type_node)
14950 {
14951 parse_error_context
14952 (wfl_operator,
781b0558 14953 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14954 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14955 return error_mark_node;
14956 }
14957 /* Now we know things are allright, invert the condition, fold and
14958 return */
14959 TREE_OPERAND (node, 0) =
14960 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14961
14962 if (! integer_zerop (TREE_OPERAND (node, 0))
14963 && ctxp->current_loop != NULL_TREE
14964 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14965 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14966 if (! integer_onep (TREE_OPERAND (node, 0)))
14967 CAN_COMPLETE_NORMALLY (node) = 1;
14968
14969
e04a16fb
AG
14970 TREE_TYPE (node) = void_type_node;
14971 return node;
14972}
b67d701b
PB
14973
14974/* 14.9 Switch statement */
14975
14976static tree
14977patch_switch_statement (node)
14978 tree node;
14979{
c877974e 14980 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14981
14982 /* Complete the switch expression */
14983 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14984 se_type = TREE_TYPE (se);
14985 /* The type of the switch expression must be char, byte, short or
14986 int */
2e0f0aff 14987 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14988 {
14989 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14990 parse_error_context (wfl_operator,
14991 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14992 lang_printable_name (se_type, 0));
b67d701b
PB
14993 /* This is what java_complete_tree will check */
14994 TREE_OPERAND (node, 0) = error_mark_node;
14995 return error_mark_node;
14996 }
14997
15fdcfe9 14998 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14999
15000 /* Ready to return */
15fdcfe9 15001 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
15002 {
15003 TREE_TYPE (node) = error_mark_node;
15004 return error_mark_node;
15005 }
15006 TREE_TYPE (node) = void_type_node;
15007 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 15008 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
15009 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15010 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
15011 return node;
15012}
15013
165f37bc 15014/* 14.18 The try/catch statements */
b67d701b 15015
b67d701b 15016static tree
a7d8d81f 15017build_try_statement (location, try_block, catches)
b67d701b 15018 int location;
a7d8d81f
PB
15019 tree try_block, catches;
15020{
15021 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 15022 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 15023 return node;
b67d701b
PB
15024}
15025
a7d8d81f
PB
15026static tree
15027build_try_finally_statement (location, try_block, finally)
15028 int location;
15029 tree try_block, finally;
b67d701b 15030{
a7d8d81f
PB
15031 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15032 EXPR_WFL_LINECOL (node) = location;
15033 return node;
b67d701b
PB
15034}
15035
15036static tree
15037patch_try_statement (node)
15038 tree node;
15039{
15040 int error_found = 0;
15041 tree try = TREE_OPERAND (node, 0);
15042 /* Exception handlers are considered in left to right order */
15043 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15044 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15045
15046 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15047 to process the next catch clause. We process the catch clause before
15048 the try block so that when processing the try block we can check thrown
15049 exceptions againts the caught type list. */
b67d701b
PB
15050 for (current = catch; current; current = TREE_CHAIN (current))
15051 {
15052 tree carg_decl, carg_type;
15053 tree sub_current, catch_block, catch_clause;
15054 int unreachable;
15055
b67d701b 15056 /* At this point, the structure of the catch clause is
b67d701b
PB
15057 CATCH_EXPR (catch node)
15058 BLOCK (with the decl of the parameter)
15059 COMPOUND_EXPR
7525cc04 15060 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15061 BLOCK (catch clause block)
a7d8d81f
PB
15062 */
15063 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15064 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15065 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15066
15067 /* Catch clauses can't have more than one parameter declared,
15068 but it's already enforced by the grammar. Make sure that the
15069 only parameter of the clause statement in of class Throwable
15070 or a subclass of Throwable, but that was done earlier. The
15071 catch clause parameter type has also been resolved. */
15072
15073 /* Just make sure that the catch clause parameter type inherits
15074 from java.lang.Throwable */
15075 if (!inherits_from_p (carg_type, throwable_type_node))
15076 {
15077 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15078 parse_error_context (wfl_operator,
781b0558 15079 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15080 lang_printable_name (carg_type, 0));
b67d701b
PB
15081 error_found = 1;
15082 continue;
15083 }
15084
15085 /* Partial check for unreachable catch statement: The catch
15086 clause is reachable iff is no earlier catch block A in
15087 the try statement such that the type of the catch
15088 clause's parameter is the same as or a subclass of the
15089 type of A's parameter */
15090 unreachable = 0;
15091 for (sub_current = catch;
15092 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15093 {
15094 tree sub_catch_clause, decl;
a7d8d81f 15095 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15096 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15097
15098 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15099 {
15100 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15101 parse_error_context
781b0558
KG
15102 (wfl_operator,
15103 "`catch' not reached because of the catch clause at line %d",
15104 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15105 unreachable = error_found = 1;
15106 break;
15107 }
15108 }
b67d701b
PB
15109 /* Complete the catch clause block */
15110 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15111 if (catch_block == error_mark_node)
15112 {
15113 error_found = 1;
15114 continue;
15115 }
15fdcfe9
PB
15116 if (CAN_COMPLETE_NORMALLY (catch_block))
15117 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15118 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15119
15120 if (unreachable)
15121 continue;
15122
15123 /* Things to do here: the exception must be thrown */
15124
15125 /* Link this type to the caught type list */
15126 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15127 }
15128
b9f7e36c
APB
15129 PUSH_EXCEPTIONS (caught_type_list);
15130 if ((try = java_complete_tree (try)) == error_mark_node)
15131 error_found = 1;
15fdcfe9
PB
15132 if (CAN_COMPLETE_NORMALLY (try))
15133 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15134 POP_EXCEPTIONS ();
15135
b67d701b
PB
15136 /* Verification ends here */
15137 if (error_found)
15138 return error_mark_node;
15139
15140 TREE_OPERAND (node, 0) = try;
15141 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15142 TREE_TYPE (node) = void_type_node;
15143 return node;
15144}
b9f7e36c
APB
15145
15146/* 14.17 The synchronized Statement */
15147
15148static tree
15149patch_synchronized_statement (node, wfl_op1)
15150 tree node, wfl_op1;
15151{
5a005d9e 15152 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15153 tree block = TREE_OPERAND (node, 1);
5a005d9e 15154
c7303e41 15155 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15156
15157 if (expr == error_mark_node)
15158 {
15159 block = java_complete_tree (block);
15160 return expr;
15161 }
b9f7e36c 15162
c7303e41
APB
15163 /* We might be trying to synchronize on a STRING_CST */
15164 if ((tmp = patch_string (expr)))
15165 expr = tmp;
15166
b9f7e36c 15167 /* The TYPE of expr must be a reference type */
5a005d9e 15168 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15169 {
15170 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15171 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15172 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15173 return error_mark_node;
15174 }
15175
ce6e9147
APB
15176 if (flag_emit_xref)
15177 {
15178 TREE_OPERAND (node, 0) = expr;
15179 TREE_OPERAND (node, 1) = java_complete_tree (block);
15180 CAN_COMPLETE_NORMALLY (node) = 1;
15181 return node;
15182 }
15183
b9f7e36c
APB
15184 /* Generate a try-finally for the synchronized statement, except
15185 that the handler that catches all throw exception calls
15186 _Jv_MonitorExit and then rethrow the exception.
15187 The synchronized statement is then implemented as:
15188 TRY
15189 {
15190 _Jv_MonitorEnter (expression)
15191 synchronized_block
15192 _Jv_MonitorExit (expression)
15193 }
15194 CATCH_ALL
15195 {
15196 e = _Jv_exception_info ();
15197 _Jv_MonitorExit (expression)
15198 Throw (e);
15199 } */
15200
5a005d9e
PB
15201 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15202 BUILD_MONITOR_ENTER (enter, expr_decl);
15203 BUILD_MONITOR_EXIT (exit, expr_decl);
15204 CAN_COMPLETE_NORMALLY (enter) = 1;
15205 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15206 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15207 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15208 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15209 build (COMPOUND_EXPR, NULL_TREE,
15210 build (WITH_CLEANUP_EXPR, NULL_TREE,
15211 build (COMPOUND_EXPR, NULL_TREE,
96847892 15212 assignment, enter),
5a005d9e
PB
15213 NULL_TREE, exit),
15214 block));
15215 node = build_expr_block (node, expr_decl);
15216
15217 return java_complete_tree (node);
b9f7e36c
APB
15218}
15219
15220/* 14.16 The throw Statement */
15221
15222static tree
15223patch_throw_statement (node, wfl_op1)
15224 tree node, wfl_op1;
15225{
15226 tree expr = TREE_OPERAND (node, 0);
15227 tree type = TREE_TYPE (expr);
15228 int unchecked_ok = 0, tryblock_throws_ok = 0;
15229
15230 /* Thrown expression must be assignable to java.lang.Throwable */
15231 if (!try_reference_assignconv (throwable_type_node, expr))
15232 {
15233 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15234 parse_error_context (wfl_operator,
15235 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15236 lang_printable_name (type, 0));
b9f7e36c
APB
15237 /* If the thrown expression was a reference, we further the
15238 compile-time check. */
15239 if (!JREFERENCE_TYPE_P (type))
15240 return error_mark_node;
15241 }
15242
15243 /* At least one of the following must be true */
15244
15245 /* The type of the throw expression is a not checked exception,
15246 i.e. is a unchecked expression. */
c877974e 15247 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15248
c2952b01
APB
15249 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15250 /* An instance can't throw a checked excetion unless that exception
15251 is explicitely declared in the `throws' clause of each
15252 constructor. This doesn't apply to anonymous classes, since they
15253 don't have declared constructors. */
15254 if (!unchecked_ok
15255 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15256 {
15257 tree current;
15258 for (current = TYPE_METHODS (current_class); current;
15259 current = TREE_CHAIN (current))
15260 if (DECL_CONSTRUCTOR_P (current)
15261 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15262 {
15263 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)",
15264 lang_printable_name (TREE_TYPE (expr), 0));
15265 return error_mark_node;
15266 }
15267 }
15268
b9f7e36c
APB
15269 /* Throw is contained in a try statement and at least one catch
15270 clause can receive the thrown expression or the current method is
15271 declared to throw such an exception. Or, the throw statement is
15272 contained in a method or constructor declaration and the type of
15273 the Expression is assignable to at least one type listed in the
15274 throws clause the declaration. */
b9f7e36c 15275 if (!unchecked_ok)
f099f336 15276 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15277 if (!(unchecked_ok || tryblock_throws_ok))
15278 {
15279 /* If there is a surrounding try block that has no matching
15280 clatch clause, report it first. A surrounding try block exits
15281 only if there is something after the list of checked
15282 exception thrown by the current function (if any). */
15283 if (IN_TRY_BLOCK_P ())
781b0558 15284 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 15285 lang_printable_name (type, 0));
b9f7e36c
APB
15286 /* If we have no surrounding try statement and the method doesn't have
15287 any throws, report it now. FIXME */
f099f336
APB
15288
15289 /* We report that the exception can't be throw from a try block
15290 in all circumstances but when the `throw' is inside a static
15291 block. */
b9f7e36c
APB
15292 else if (!EXCEPTIONS_P (currently_caught_type_list)
15293 && !tryblock_throws_ok)
f099f336 15294 {
c2952b01 15295 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15296 parse_error_context (wfl_operator,
15297 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15298 lang_printable_name (type, 0));
15299 else
781b0558
KG
15300 parse_error_context (wfl_operator,
15301 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15302 lang_printable_name (type, 0));
15303 }
b9f7e36c
APB
15304 /* Otherwise, the current method doesn't have the appropriate
15305 throws declaration */
15306 else
781b0558 15307 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15308 lang_printable_name (type, 0));
b9f7e36c
APB
15309 return error_mark_node;
15310 }
15311
ce6e9147 15312 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15313 BUILD_THROW (node, expr);
ce6e9147
APB
15314
15315 /* If doing xrefs, keep the location where the `throw' was seen. */
15316 if (flag_emit_xref)
15317 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15318 return node;
15319}
15320
15321/* Check that exception said to be thrown by method DECL can be
15322 effectively caught from where DECL is invoked. */
15323
15324static void
15325check_thrown_exceptions (location, decl)
15326 int location;
15327 tree decl;
15328{
15329 tree throws;
15330 /* For all the unchecked exceptions thrown by DECL */
15331 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15332 throws = TREE_CHAIN (throws))
0a2138e2 15333 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15334 {
3e78f871
PB
15335#if 1
15336 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15337 if (DECL_NAME (decl) == get_identifier ("clone"))
15338 continue;
15339#endif
b9f7e36c 15340 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15341 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15342 parse_error_context
15343 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15344 lang_printable_name (TREE_VALUE (throws), 0));
15345 else
15346 {
15347 parse_error_context
781b0558 15348 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15349 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15350 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15351 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15352 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15353 }
b9f7e36c
APB
15354 }
15355}
15356
c877974e 15357/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15358 try-catch blocks, OR is listed in the `throws' clause of the
15359 current method. */
15360
15361static int
0a2138e2 15362check_thrown_exceptions_do (exception)
b9f7e36c
APB
15363 tree exception;
15364{
15365 tree list = currently_caught_type_list;
c877974e 15366 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15367 /* First, all the nested try-catch-finally at that stage. The
15368 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15369 if (IS_UNCHECKED_EXCEPTION_P (exception))
15370 return 1;
b9f7e36c
APB
15371 while (list)
15372 {
15373 tree caught;
15374 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15375 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15376 return 1;
15377 list = TREE_CHAIN (list);
15378 }
15379 return 0;
15380}
15381
15382static void
15383purge_unchecked_exceptions (mdecl)
15384 tree mdecl;
15385{
15386 tree throws = DECL_FUNCTION_THROWS (mdecl);
15387 tree new = NULL_TREE;
15388
15389 while (throws)
15390 {
15391 tree next = TREE_CHAIN (throws);
c877974e 15392 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15393 {
15394 TREE_CHAIN (throws) = new;
15395 new = throws;
15396 }
15397 throws = next;
15398 }
15399 /* List is inverted here, but it doesn't matter */
15400 DECL_FUNCTION_THROWS (mdecl) = new;
15401}
22eed1e6
APB
15402
15403/* 15.24 Conditional Operator ?: */
15404
15405static tree
15406patch_conditional_expr (node, wfl_cond, wfl_op1)
15407 tree node, wfl_cond, wfl_op1;
15408{
15409 tree cond = TREE_OPERAND (node, 0);
15410 tree op1 = TREE_OPERAND (node, 1);
15411 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15412 tree resulting_type = NULL_TREE;
ac825856 15413 tree t1, t2, patched;
22eed1e6
APB
15414 int error_found = 0;
15415
ac825856
APB
15416 /* Operands of ?: might be StringBuffers crafted as a result of a
15417 string concatenation. Obtain a descent operand here. */
15418 if ((patched = patch_string (op1)))
15419 TREE_OPERAND (node, 1) = op1 = patched;
15420 if ((patched = patch_string (op2)))
15421 TREE_OPERAND (node, 2) = op2 = patched;
15422
15423 t1 = TREE_TYPE (op1);
15424 t2 = TREE_TYPE (op2);
15425
22eed1e6
APB
15426 /* The first expression must be a boolean */
15427 if (TREE_TYPE (cond) != boolean_type_node)
15428 {
15429 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15430 parse_error_context (wfl_operator,
15431 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15432 lang_printable_name (TREE_TYPE (cond), 0));
15433 error_found = 1;
15434 }
15435
15436 /* Second and third can be numeric, boolean (i.e. primitive),
15437 references or null. Anything else results in an error */
15438 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15439 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15440 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15441 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15442 error_found = 1;
15443
15444 /* Determine the type of the conditional expression. Same types are
15445 easy to deal with */
15446 else if (t1 == t2)
15447 resulting_type = t1;
15448
15449 /* There are different rules for numeric types */
15450 else if (JNUMERIC_TYPE_P (t1))
15451 {
15452 /* if byte/short found, the resulting type is short */
15453 if ((t1 == byte_type_node && t2 == short_type_node)
15454 || (t1 == short_type_node && t2 == byte_type_node))
15455 resulting_type = short_type_node;
15456
15457 /* If t1 is a constant int and t2 is of type byte, short or char
15458 and t1's value fits in t2, then the resulting type is t2 */
15459 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15460 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15461 resulting_type = t2;
15462
15463 /* If t2 is a constant int and t1 is of type byte, short or char
15464 and t2's value fits in t1, then the resulting type is t1 */
15465 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15466 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15467 resulting_type = t1;
15468
15469 /* Otherwise, binary numeric promotion is applied and the
15470 resulting type is the promoted type of operand 1 and 2 */
15471 else
93024893 15472 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15473 &TREE_OPERAND (node, 1),
15474 &TREE_OPERAND (node, 2));
15475 }
15476
15477 /* Cases of a reference and a null type */
15478 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15479 resulting_type = t1;
15480
15481 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15482 resulting_type = t2;
15483
15484 /* Last case: different reference types. If a type can be converted
15485 into the other one by assignment conversion, the latter
15486 determines the type of the expression */
15487 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15488 resulting_type = promote_type (t1);
15489
15490 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15491 resulting_type = promote_type (t2);
15492
15493 /* If we don't have any resulting type, we're in trouble */
15494 if (!resulting_type)
15495 {
c2e3db92 15496 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15497 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15498 parse_error_context (wfl_operator,
15499 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15500 t, lang_printable_name (t2, 0));
22eed1e6
APB
15501 free (t);
15502 error_found = 1;
15503 }
15504
15505 if (error_found)
15506 {
15507 TREE_TYPE (node) = error_mark_node;
15508 return error_mark_node;
15509 }
15510
15511 TREE_TYPE (node) = resulting_type;
15512 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15513 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15514 return node;
15515}
ac825856 15516
5b09b33e
PB
15517/* Try to constant fold NODE.
15518 If NODE is not a constant expression, return NULL_EXPR.
15519 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15520
15521static tree
15522fold_constant_for_init (node, context)
15523 tree node;
15524 tree context;
15525{
15526 tree op0, op1, val;
15527 enum tree_code code = TREE_CODE (node);
15528
ee97d354 15529 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15530 return node;
93024893 15531
5b09b33e
PB
15532 switch (code)
15533 {
5b09b33e
PB
15534 case PLUS_EXPR:
15535 case MINUS_EXPR:
bc3ca41b
PB
15536 case MULT_EXPR:
15537 case TRUNC_MOD_EXPR:
15538 case RDIV_EXPR:
5b09b33e
PB
15539 case LSHIFT_EXPR:
15540 case RSHIFT_EXPR:
15541 case URSHIFT_EXPR:
15542 case BIT_AND_EXPR:
15543 case BIT_XOR_EXPR:
15544 case BIT_IOR_EXPR:
5b09b33e
PB
15545 case TRUTH_ANDIF_EXPR:
15546 case TRUTH_ORIF_EXPR:
15547 case EQ_EXPR:
15548 case NE_EXPR:
15549 case GT_EXPR:
15550 case GE_EXPR:
15551 case LT_EXPR:
15552 case LE_EXPR:
15553 op0 = TREE_OPERAND (node, 0);
15554 op1 = TREE_OPERAND (node, 1);
15555 val = fold_constant_for_init (op0, context);
15556 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15557 return NULL_TREE;
15558 TREE_OPERAND (node, 0) = val;
15559 val = fold_constant_for_init (op1, context);
15560 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15561 return NULL_TREE;
15562 TREE_OPERAND (node, 1) = val;
15563 return patch_binop (node, op0, op1);
15564
15565 case UNARY_PLUS_EXPR:
15566 case NEGATE_EXPR:
15567 case TRUTH_NOT_EXPR:
15568 case BIT_NOT_EXPR:
15569 case CONVERT_EXPR:
15570 op0 = TREE_OPERAND (node, 0);
15571 val = fold_constant_for_init (op0, context);
15572 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15573 return NULL_TREE;
15574 TREE_OPERAND (node, 0) = val;
5a005d9e 15575 return patch_unaryop (node, op0);
5b09b33e
PB
15576 break;
15577
15578 case COND_EXPR:
15579 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15580 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15581 return NULL_TREE;
15582 TREE_OPERAND (node, 0) = val;
15583 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15584 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15585 return NULL_TREE;
15586 TREE_OPERAND (node, 1) = val;
15587 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15588 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15589 return NULL_TREE;
15590 TREE_OPERAND (node, 2) = val;
15591 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15592 : TREE_OPERAND (node, 2);
15593
15594 case VAR_DECL:
8576f094
APB
15595 case FIELD_DECL:
15596 if (! FIELD_FINAL (node)
5b09b33e
PB
15597 || DECL_INITIAL (node) == NULL_TREE)
15598 return NULL_TREE;
15599 val = DECL_INITIAL (node);
15600 /* Guard against infinite recursion. */
15601 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15602 val = fold_constant_for_init (val, node);
5b09b33e 15603 DECL_INITIAL (node) = val;
c7303e41
APB
15604 if (!val && CLASS_FINAL_VARIABLE_P (node))
15605 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15606 return val;
15607
15608 case EXPR_WITH_FILE_LOCATION:
15609 /* Compare java_complete_tree and resolve_expression_name. */
15610 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15611 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15612 {
15613 tree name = EXPR_WFL_NODE (node);
15614 tree decl;
15615 if (PRIMARY_P (node))
15616 return NULL_TREE;
15617 else if (! QUALIFIED_P (name))
15618 {
15619 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15620 if (decl == NULL_TREE
15621 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15622 return NULL_TREE;
15623 return fold_constant_for_init (decl, decl);
15624 }
15625 else
15626 {
5b09b33e
PB
15627 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15628 qualify_ambiguous_name (node);
15629 if (resolve_field_access (node, &decl, NULL)
15630 && decl != NULL_TREE)
15631 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15632 return NULL_TREE;
15633 }
15634 }
15635 else
15636 {
15637 op0 = TREE_OPERAND (node, 0);
15638 val = fold_constant_for_init (op0, context);
15639 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15640 return NULL_TREE;
15641 TREE_OPERAND (node, 0) = val;
15642 return val;
15643 }
15644
bc3ca41b
PB
15645#ifdef USE_COMPONENT_REF
15646 case IDENTIFIER:
15647 case COMPONENT_REF:
15648 ?;
15649#endif
15650
5b09b33e
PB
15651 default:
15652 return NULL_TREE;
15653 }
15654}
bc3ca41b
PB
15655
15656#ifdef USE_COMPONENT_REF
15657/* Context is 'T' for TypeName, 'P' for PackageName,
15658 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15659
15660tree
15661resolve_simple_name (name, context)
15662 tree name;
15663 int context;
15664{
15665}
15666
15667tree
15668resolve_qualified_name (name, context)
15669 tree name;
15670 int context;
15671{
15672}
15673#endif
f15b9af9
MM
15674
15675/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15676
15677static void
15678mark_parser_ctxt (p)
15679 void *p;
15680{
15681 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15682 int i;
15683
15684 if (!pc)
15685 return;
15686
15687#ifndef JC1_LITE
15688 for (i = 0; i < 11; ++i)
15689 ggc_mark_tree (pc->modifier_ctx[i]);
15690 ggc_mark_tree (pc->class_type);
15691 ggc_mark_tree (pc->function_decl);
15692 ggc_mark_tree (pc->package);
15693 ggc_mark_tree (pc->incomplete_class);
15694 ggc_mark_tree (pc->gclass_list);
15695 ggc_mark_tree (pc->class_list);
15696 ggc_mark_tree (pc->current_parsed_class);
15697 ggc_mark_tree (pc->current_parsed_class_un);
15698 ggc_mark_tree (pc->non_static_initialized);
15699 ggc_mark_tree (pc->static_initialized);
15700 ggc_mark_tree (pc->instance_initializers);
15701 ggc_mark_tree (pc->import_list);
15702 ggc_mark_tree (pc->import_demand_list);
15703 ggc_mark_tree (pc->current_loop);
15704 ggc_mark_tree (pc->current_labeled_block);
15705#endif /* JC1_LITE */
15706
15707 if (pc->next)
15708 mark_parser_ctxt (&pc->next);
15709}
This page took 2.736546 seconds and 5 git commands to generate.