]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
error.c (dump_template_parameter): Use parm to determine how to print default value.
[gcc.git] / gcc / java / parse.y
CommitLineData
e04a16fb
AG
1/* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
df32d2ce 3 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
e04a16fb
AG
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23Java and all Java-based marks are trademarks or registered trademarks
24of Sun Microsystems, Inc. in the United States and other countries.
25The Free Software Foundation is independent of Sun Microsystems, Inc. */
26
27/* This file parses java source code and issues a tree node image
28suitable for code generation (byte code and targeted CPU assembly
29language).
30
31The grammar conforms to the Java grammar described in "The Java(TM)
32Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
331996, ISBN 0-201-63451-1"
34
35The following modifications were brought to the original grammar:
36
37method_body: added the rule '| block SC_TK'
e04a16fb
AG
38static_initializer: added the rule 'static block SC_TK'.
39
40Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43statement_nsi: 'nsi' should be read no_short_if.
44
45Some rules have been modified to support JDK1.1 inner classes
46definitions and other extensions. */
47
48%{
e04a16fb 49#include "config.h"
36635152
GS
50#include "system.h"
51#include <dirent.h>
e04a16fb
AG
52#include "tree.h"
53#include "rtl.h"
54#include "obstack.h"
0a2138e2 55#include "toplev.h"
e04a16fb
AG
56#include "flags.h"
57#include "java-tree.h"
58#include "jcf.h"
59#include "lex.h"
60#include "parse.h"
61#include "zipfile.h"
5e942c50 62#include "convert.h"
63a212ed 63#include "buffer.h"
f099f336 64#include "xref.h"
b384405b 65#include "function.h"
138657ec 66#include "except.h"
0ae70c6a 67#include "defaults.h"
19e223db 68#include "ggc.h"
e04a16fb 69
c2952b01
APB
70#ifndef DIR_SEPARATOR
71#define DIR_SEPARATOR '/'
72#endif
73
82371d41 74/* Local function prototypes */
df32d2ce
KG
75static char *java_accstring_lookup PARAMS ((int));
76static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
78static tree create_class PARAMS ((int, tree, tree, tree));
79static tree create_interface PARAMS ((int, tree, tree));
c2952b01 80static void end_class_declaration PARAMS ((int));
df32d2ce
KG
81static tree find_field PARAMS ((tree, tree));
82static tree lookup_field_wrapper PARAMS ((tree, tree));
83static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84static void register_fields PARAMS ((int, tree, tree));
98a52c2c 85static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
86static int parser_check_super PARAMS ((tree, tree, tree));
87static int parser_check_super_interface PARAMS ((tree, tree, tree));
88static void check_modifiers_consistency PARAMS ((int));
89static tree lookup_cl PARAMS ((tree));
90static tree lookup_java_method2 PARAMS ((tree, tree, int));
91static tree method_header PARAMS ((int, tree, tree, tree));
92static void fix_method_argument_names PARAMS ((tree ,tree));
93static tree method_declarator PARAMS ((tree, tree));
94static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 95 ATTRIBUTE_PRINTF_2;
88f3c477
JM
96static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
97 ATTRIBUTE_PRINTF (2, 0);
df32d2ce
KG
98static void parse_ctor_invocation_error PARAMS ((void));
99static tree parse_jdk1_1_error PARAMS ((const char *));
100static void complete_class_report_errors PARAMS ((jdep *));
101static int process_imports PARAMS ((void));
102static void read_import_dir PARAMS ((tree));
103static int find_in_imports_on_demand PARAMS ((tree));
9a7ab4b3 104static void find_in_imports PARAMS ((tree));
c7303e41
APB
105static void check_static_final_variable_assignment_flag PARAMS ((tree));
106static void reset_static_final_variable_assignment_flag PARAMS ((tree));
107static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
108static void reset_final_variable_local_assignment_flag PARAMS ((tree));
109static int check_final_variable_indirect_assignment PARAMS ((tree));
110static void check_final_variable_global_assignment_flag PARAMS ((tree));
cf1748bf 111static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 112static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 113static void register_package PARAMS ((tree));
df32d2ce
KG
114static tree resolve_package PARAMS ((tree, tree *));
115static tree lookup_package_type PARAMS ((const char *, int));
116static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 117static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
118static void declare_local_variables PARAMS ((int, tree, tree));
119static void source_start_java_method PARAMS ((tree));
120static void source_end_java_method PARAMS ((void));
121static void expand_start_java_method PARAMS ((tree));
122static tree find_name_in_single_imports PARAMS ((tree));
123static void check_abstract_method_header PARAMS ((tree));
124static tree lookup_java_interface_method2 PARAMS ((tree, tree));
125static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 126static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 127static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 128 tree, tree, tree));
df32d2ce 129static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 130 int *, tree *));
df32d2ce
KG
131static int breakdown_qualified PARAMS ((tree *, tree *, tree));
132static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 133static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
134static tree resolve_no_layout PARAMS ((tree, tree));
135static int invocation_mode PARAMS ((tree, int));
136static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 137 tree, tree));
df32d2ce 138static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 139 tree *, tree *));
df32d2ce
KG
140static tree find_most_specific_methods_list PARAMS ((tree));
141static int argument_types_convertible PARAMS ((tree, tree));
142static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 143static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
144static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
145static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
146static tree obtain_incomplete_type PARAMS ((tree));
147static tree java_complete_lhs PARAMS ((tree));
148static tree java_complete_tree PARAMS ((tree));
c2952b01 149static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 150static int analyze_clinit_body PARAMS ((tree));
92d83515 151static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
152static void java_complete_expand_method PARAMS ((tree));
153static int unresolved_type_p PARAMS ((tree, tree *));
154static void create_jdep_list PARAMS ((struct parser_ctxt *));
155static tree build_expr_block PARAMS ((tree, tree));
156static tree enter_block PARAMS ((void));
157static tree enter_a_block PARAMS ((tree));
158static tree exit_block PARAMS ((void));
159static tree lookup_name_in_blocks PARAMS ((tree));
160static void maybe_absorb_scoping_blocks PARAMS ((void));
161static tree build_method_invocation PARAMS ((tree, tree));
162static tree build_new_invocation PARAMS ((tree, tree));
163static tree build_assignment PARAMS ((int, int, tree, tree));
164static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
165static int check_final_assignment PARAMS ((tree ,tree));
166static tree patch_assignment PARAMS ((tree, tree, tree ));
167static tree patch_binop PARAMS ((tree, tree, tree));
168static tree build_unaryop PARAMS ((int, int, tree));
169static tree build_incdec PARAMS ((int, int, tree, int));
170static tree patch_unaryop PARAMS ((tree, tree));
171static tree build_cast PARAMS ((int, tree, tree));
172static tree build_null_of_type PARAMS ((tree));
173static tree patch_cast PARAMS ((tree, tree));
174static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
175static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
176static int valid_cast_to_p PARAMS ((tree, tree));
177static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
178static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
179static tree try_reference_assignconv PARAMS ((tree, tree));
180static tree build_unresolved_array_type PARAMS ((tree));
181static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
182static tree build_array_ref PARAMS ((int, tree, tree));
183static tree patch_array_ref PARAMS ((tree));
184static tree make_qualified_name PARAMS ((tree, tree, int));
185static tree merge_qualified_name PARAMS ((tree, tree));
186static tree make_qualified_primary PARAMS ((tree, tree, int));
187static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 188 tree *, tree *));
df32d2ce 189static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
190static tree resolve_field_access PARAMS ((tree, tree *, tree *));
191static tree build_newarray_node PARAMS ((tree, tree, int));
192static tree patch_newarray PARAMS ((tree));
193static tree resolve_type_during_patch PARAMS ((tree));
194static tree build_this PARAMS ((int));
9a7ab4b3 195static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
196static tree build_return PARAMS ((int, tree));
197static tree patch_return PARAMS ((tree));
198static tree maybe_access_field PARAMS ((tree, tree, tree));
199static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
200static int check_for_static_method_reference PARAMS ((tree, tree, tree,
201 tree, tree));
df32d2ce
KG
202static int not_accessible_p PARAMS ((tree, tree, int));
203static void check_deprecation PARAMS ((tree, tree));
204static int class_in_current_package PARAMS ((tree));
205static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
206static tree patch_if_else_statement PARAMS ((tree));
207static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
208static tree add_stmt_to_block PARAMS ((tree, tree, tree));
209static tree patch_exit_expr PARAMS ((tree));
210static tree build_labeled_block PARAMS ((int, tree));
211static tree finish_labeled_statement PARAMS ((tree, tree));
212static tree build_bc_statement PARAMS ((int, int, tree));
213static tree patch_bc_statement PARAMS ((tree));
214static tree patch_loop_statement PARAMS ((tree));
215static tree build_new_loop PARAMS ((tree));
216static tree build_loop_body PARAMS ((int, tree, int));
217static tree finish_loop_body PARAMS ((int, tree, tree, int));
218static tree build_debugable_stmt PARAMS ((int, tree));
219static tree finish_for_loop PARAMS ((int, tree, tree, tree));
220static tree patch_switch_statement PARAMS ((tree));
221static tree string_constant_concatenation PARAMS ((tree, tree));
222static tree build_string_concatenation PARAMS ((tree, tree));
223static tree patch_string_cst PARAMS ((tree));
224static tree patch_string PARAMS ((tree));
225static tree build_try_statement PARAMS ((int, tree, tree));
226static tree build_try_finally_statement PARAMS ((int, tree, tree));
227static tree patch_try_statement PARAMS ((tree));
228static tree patch_synchronized_statement PARAMS ((tree, tree));
229static tree patch_throw_statement PARAMS ((tree, tree));
230static void check_thrown_exceptions PARAMS ((int, tree));
231static int check_thrown_exceptions_do PARAMS ((tree));
232static void purge_unchecked_exceptions PARAMS ((tree));
233static void check_throws_clauses PARAMS ((tree, tree, tree));
234static void finish_method_declaration PARAMS ((tree));
235static tree build_super_invocation PARAMS ((tree));
236static int verify_constructor_circularity PARAMS ((tree, tree));
237static char *constructor_circularity_msg PARAMS ((tree, tree));
238static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 239 int, int));
df32d2ce
KG
240static const char *get_printable_method_name PARAMS ((tree));
241static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
242static tree generate_finit PARAMS ((tree));
243static void add_instance_initializer PARAMS ((tree));
df32d2ce 244static void fix_constructors PARAMS ((tree));
c2952b01
APB
245static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
246 tree, int *));
247static void craft_constructor PARAMS ((tree, tree));
248static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
249static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
250static void start_artificial_method_body PARAMS ((tree));
251static void end_artificial_method_body PARAMS ((tree));
252static int check_method_redefinition PARAMS ((tree, tree));
253static int reset_method_name PARAMS ((tree));
165f37bc 254static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
255static void java_check_regular_methods PARAMS ((tree));
256static void java_check_abstract_methods PARAMS ((tree));
257static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
258static void unreachable_stmt_error PARAMS ((tree));
259static tree find_expr_with_wfl PARAMS ((tree));
260static void missing_return_error PARAMS ((tree));
261static tree build_new_array_init PARAMS ((int, tree));
262static tree patch_new_array_init PARAMS ((tree, tree));
263static tree maybe_build_array_element_wfl PARAMS ((tree));
264static int array_constructor_check_entry PARAMS ((tree, tree));
265static const char *purify_type_name PARAMS ((const char *));
266static tree fold_constant_for_init PARAMS ((tree, tree));
267static tree strip_out_static_field_access_decl PARAMS ((tree));
268static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
269static void static_ref_err PARAMS ((tree, tree, tree));
270static void parser_add_interface PARAMS ((tree, tree, tree));
271static void add_superinterfaces PARAMS ((tree, tree));
272static tree jdep_resolve_class PARAMS ((jdep *));
273static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
274static void java_complete_expand_classes PARAMS ((void));
275static void java_complete_expand_class PARAMS ((tree));
276static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
277static tree cut_identifier_in_qualified PARAMS ((tree));
278static tree java_stabilize_reference PARAMS ((tree));
279static tree do_unary_numeric_promotion PARAMS ((tree));
280static char * operator_string PARAMS ((tree));
281static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
282static tree merge_string_cste PARAMS ((tree, tree, int));
283static tree java_refold PARAMS ((tree));
284static int java_decl_equiv PARAMS ((tree, tree));
285static int binop_compound_p PARAMS ((enum tree_code));
286static tree search_loop PARAMS ((tree));
287static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 288static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
289static void java_check_abstract_method_definitions PARAMS ((tree));
290static void java_debug_context_do PARAMS ((int));
c2952b01
APB
291static void java_parser_context_push_initialized_field PARAMS ((void));
292static void java_parser_context_pop_initialized_field PARAMS ((void));
293static tree reorder_static_initialized PARAMS ((tree));
294static void java_parser_context_suspend PARAMS ((void));
295static void java_parser_context_resume PARAMS ((void));
c7303e41 296static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
297
298/* JDK 1.1 work. FIXME */
299
300static tree maybe_make_nested_class_name PARAMS ((tree));
301static void make_nested_class_name PARAMS ((tree));
302static void set_nested_class_simple_name_value PARAMS ((tree, int));
303static void link_nested_class_to_enclosing PARAMS ((void));
304static tree find_as_inner_class PARAMS ((tree, tree, tree));
305static tree find_as_inner_class_do PARAMS ((tree, tree));
306static int check_inner_class_redefinition PARAMS ((tree, tree));
307
308static tree build_thisn_assign PARAMS ((void));
309static tree build_current_thisn PARAMS ((tree));
310static tree build_access_to_thisn PARAMS ((tree, tree, int));
311static tree maybe_build_thisn_access_method PARAMS ((tree));
312
313static tree build_outer_field_access PARAMS ((tree, tree));
314static tree build_outer_field_access_methods PARAMS ((tree));
315static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
316 tree, tree));
317static tree build_outer_method_access_method PARAMS ((tree));
318static tree build_new_access_id PARAMS ((void));
319static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
320 tree, tree));
321
322static int outer_field_access_p PARAMS ((tree, tree));
323static int outer_field_expanded_access_p PARAMS ((tree, tree *,
324 tree *, tree *));
325static tree outer_field_access_fix PARAMS ((tree, tree, tree));
326static tree build_incomplete_class_ref PARAMS ((int, tree));
327static tree patch_incomplete_class_ref PARAMS ((tree));
328static tree create_anonymous_class PARAMS ((int, tree));
329static void patch_anonymous_class PARAMS ((tree, tree, tree));
330static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 331
165f37bc
APB
332static tree build_dot_class_method PARAMS ((tree));
333static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 334static void create_new_parser_context PARAMS ((int));
f15b9af9 335static void mark_parser_ctxt PARAMS ((void *));
165f37bc 336
e04a16fb
AG
337/* Number of error found so far. */
338int java_error_count;
339/* Number of warning found so far. */
340int java_warning_count;
ce6e9147
APB
341/* Tell when not to fold, when doing xrefs */
342int do_not_fold;
c2952b01 343/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 344const char *cyclic_inheritance_report;
f15b9af9 345
c2952b01
APB
346/* Tell when we're within an instance initializer */
347static int in_instance_initializer;
e04a16fb
AG
348
349/* The current parser context */
d4370213 350struct parser_ctxt *ctxp;
e04a16fb 351
d4370213 352/* List of things that were analyzed for which code will be generated */
b351b287
APB
353static struct parser_ctxt *ctxp_for_generation = NULL;
354
e04a16fb
AG
355/* binop_lookup maps token to tree_code. It is used where binary
356 operations are involved and required by the parser. RDIV_EXPR
357 covers both integral/floating point division. The code is changed
358 once the type of both operator is worked out. */
359
360static enum tree_code binop_lookup[19] =
361 {
362 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
363 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
364 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
365 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
366 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
367 };
368#define BINOP_LOOKUP(VALUE) \
6e2aa220 369 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 370
5cbdba64
APB
371/* This is the end index for binary operators that can also be used
372 in compound assignements. */
373#define BINOP_COMPOUND_CANDIDATES 11
374
e04a16fb 375/* The "$L" identifier we use to create labels. */
b67d701b
PB
376static tree label_id = NULL_TREE;
377
378/* The "StringBuffer" identifier used for the String `+' operator. */
379static tree wfl_string_buffer = NULL_TREE;
380
381/* The "append" identifier used for String `+' operator. */
382static tree wfl_append = NULL_TREE;
383
384/* The "toString" identifier used for String `+' operator. */
385static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
386
387/* The "java.lang" import qualified name. */
388static tree java_lang_id = NULL_TREE;
09ed0f70 389
c2952b01
APB
390/* The generated `inst$' identifier used for generated enclosing
391 instance/field access functions. */
392static tree inst_id = NULL_TREE;
393
09ed0f70
APB
394/* The "java.lang.Cloneable" qualified name. */
395static tree java_lang_cloneable = NULL_TREE;
f099f336 396
ee17a290
TT
397/* The "java.io.Serializable" qualified name. */
398static tree java_io_serializable = NULL_TREE;
399
f099f336
APB
400/* Context and flag for static blocks */
401static tree current_static_block = NULL_TREE;
402
c2952b01
APB
403/* The generated `write_parm_value$' identifier. */
404static tree wpv_id;
405
ee07f4f4
APB
406/* The list of all packages we've seen so far */
407static tree package_list = NULL_TREE;
2884c41e 408
19e223db
MM
409/* Hold THIS for the scope of the current public method decl. */
410static tree current_this;
411
412/* Hold a list of catch clauses list. The first element of this list is
413 the list of the catch clauses of the currently analysed try block. */
414static tree currently_caught_type_list;
415
2884c41e
KG
416/* Check modifiers. If one doesn't fit, retrieve it in its declaration
417 line and point it out. */
418/* Should point out the one that don't fit. ASCII/unicode, going
419 backward. FIXME */
420
421#define check_modifiers(__message, __value, __mask) do { \
422 if ((__value) & ~(__mask)) \
423 { \
424 int i, remainder = (__value) & ~(__mask); \
425 for (i = 0; i <= 10; i++) \
426 if ((1 << i) & remainder) \
427 parse_error_context (ctxp->modifier_ctx [i], (__message), \
428 java_accstring_lookup (1 << i)); \
429 } \
430} while (0)
ee07f4f4 431
e04a16fb
AG
432%}
433
434%union {
435 tree node;
436 int sub_token;
437 struct {
438 int token;
439 int location;
440 } operator;
441 int value;
442}
443
9ee9b555
KG
444%{
445#include "lex.c"
446%}
447
e04a16fb
AG
448%pure_parser
449
450/* Things defined here have to match the order of what's in the
451 binop_lookup table. */
452
453%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
454%token LS_TK SRS_TK ZRS_TK
455%token AND_TK XOR_TK OR_TK
456%token BOOL_AND_TK BOOL_OR_TK
457%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
458
459/* This maps to the same binop_lookup entry than the token above */
460
461%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
462%token REM_ASSIGN_TK
463%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
464%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
465
466
467/* Modifier TOKEN have to be kept in this order. Don't scramble it */
468
469%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
470%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
471%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
472%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 473%token STRICT_TK
e04a16fb
AG
474
475/* Keep those two in order, too */
476%token DECR_TK INCR_TK
477
478/* From now one, things can be in any order */
479
480%token DEFAULT_TK IF_TK THROW_TK
481%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
482%token THROWS_TK BREAK_TK IMPORT_TK
483%token ELSE_TK INSTANCEOF_TK RETURN_TK
484%token VOID_TK CATCH_TK INTERFACE_TK
485%token CASE_TK EXTENDS_TK FINALLY_TK
486%token SUPER_TK WHILE_TK CLASS_TK
487%token SWITCH_TK CONST_TK TRY_TK
488%token FOR_TK NEW_TK CONTINUE_TK
489%token GOTO_TK PACKAGE_TK THIS_TK
490
491%token BYTE_TK SHORT_TK INT_TK LONG_TK
492%token CHAR_TK INTEGRAL_TK
493
494%token FLOAT_TK DOUBLE_TK FP_TK
495
496%token ID_TK
497
498%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
499
500%token ASSIGN_ANY_TK ASSIGN_TK
501%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
502
503%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
504%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
505
c2952b01 506%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
507
508%type <node> super ID_TK identifier
509%type <node> name simple_name qualified_name
c2952b01 510%type <node> type_declaration compilation_unit
e04a16fb
AG
511 field_declaration method_declaration extends_interfaces
512 interfaces interface_type_list
c2952b01 513 class_member_declaration
e04a16fb
AG
514 import_declarations package_declaration
515 type_declarations interface_body
516 interface_member_declaration constant_declaration
517 interface_member_declarations interface_type
518 abstract_method_declaration interface_type_list
519%type <node> class_body_declaration class_member_declaration
520 static_initializer constructor_declaration block
22eed1e6 521%type <node> class_body_declarations constructor_header
e04a16fb
AG
522%type <node> class_or_interface_type class_type class_type_list
523 constructor_declarator explicit_constructor_invocation
b9f7e36c 524%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
525
526%type <node> variable_declarator_id variable_declarator
527 variable_declarators variable_initializer
22eed1e6 528 variable_initializers constructor_body
ac825856 529 array_initializer
e04a16fb 530
2e5eb5c5 531%type <node> class_body block_end constructor_block_end
e04a16fb
AG
532%type <node> statement statement_without_trailing_substatement
533 labeled_statement if_then_statement label_decl
534 if_then_else_statement while_statement for_statement
535 statement_nsi labeled_statement_nsi do_statement
536 if_then_else_statement_nsi while_statement_nsi
537 for_statement_nsi statement_expression_list for_init
538 for_update statement_expression expression_statement
539 primary_no_new_array expression primary
540 array_creation_expression array_type
541 class_instance_creation_expression field_access
542 method_invocation array_access something_dot_new
543 argument_list postfix_expression while_expression
544 post_increment_expression post_decrement_expression
545 unary_expression_not_plus_minus unary_expression
546 pre_increment_expression pre_decrement_expression
547 unary_expression_not_plus_minus cast_expression
548 multiplicative_expression additive_expression
549 shift_expression relational_expression
550 equality_expression and_expression
551 exclusive_or_expression inclusive_or_expression
552 conditional_and_expression conditional_or_expression
553 conditional_expression assignment_expression
554 left_hand_side assignment for_header for_begin
555 constant_expression do_statement_begin empty_statement
b67d701b 556 switch_statement synchronized_statement throw_statement
f8976021 557 try_statement switch_expression switch_block
15fdcfe9 558 catches catch_clause catch_clause_parameter finally
c2952b01 559 anonymous_class_creation
e04a16fb
AG
560%type <node> return_statement break_statement continue_statement
561
562%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
563%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
564%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
565%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
566%type <operator> ASSIGN_ANY_TK assignment_operator
567%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
568%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
569%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 570%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 571%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
572%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
573%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 574%type <operator> NEW_TK
e04a16fb
AG
575
576%type <node> method_body
577
578%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
579 STRING_LIT_TK NULL_TK VOID_TK
580
581%type <node> IF_TK WHILE_TK FOR_TK
582
583%type <node> formal_parameter_list formal_parameter
584 method_declarator method_header
585
c2952b01 586%type <node> primitive_type reference_type type
e04a16fb
AG
587 BOOLEAN_TK INTEGRAL_TK FP_TK
588
c2952b01 589/* Added or modified JDK 1.1 rule types */
c7303e41 590%type <node> type_literals
c2952b01 591
e04a16fb
AG
592%%
593/* 19.2 Production from 2.3: The Syntactic Grammar */
594goal:
19e223db
MM
595 {
596 /* Register static variables with the garbage
597 collector. */
598 ggc_add_tree_root (&label_id, 1);
599 ggc_add_tree_root (&wfl_string_buffer, 1);
600 ggc_add_tree_root (&wfl_append, 1);
601 ggc_add_tree_root (&wfl_to_string, 1);
602 ggc_add_tree_root (&java_lang_id, 1);
603 ggc_add_tree_root (&inst_id, 1);
604 ggc_add_tree_root (&java_lang_cloneable, 1);
605 ggc_add_tree_root (&java_io_serializable, 1);
606 ggc_add_tree_root (&current_static_block, 1);
607 ggc_add_tree_root (&wpv_id, 1);
608 ggc_add_tree_root (&package_list, 1);
609 ggc_add_tree_root (&current_this, 1);
610 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
611 ggc_add_root (&ctxp, 1,
612 sizeof (struct parser_ctxt *),
613 mark_parser_ctxt);
614 ggc_add_root (&ctxp_for_generation, 1,
615 sizeof (struct parser_ctxt *),
616 mark_parser_ctxt);
19e223db 617 }
e04a16fb
AG
618 compilation_unit
619 {}
620;
621
622/* 19.3 Productions from 3: Lexical structure */
623literal:
624 INT_LIT_TK
625| FP_LIT_TK
626| BOOL_LIT_TK
627| CHAR_LIT_TK
628| STRING_LIT_TK
629| NULL_TK
630;
631
632/* 19.4 Productions from 4: Types, Values and Variables */
633type:
634 primitive_type
635| reference_type
636;
637
638primitive_type:
639 INTEGRAL_TK
640| FP_TK
641| BOOLEAN_TK
642;
643
644reference_type:
645 class_or_interface_type
646| array_type
647;
648
649class_or_interface_type:
650 name
651;
652
653class_type:
654 class_or_interface_type /* Default rule */
655;
656
657interface_type:
658 class_or_interface_type
659;
660
661array_type:
c7303e41 662 primitive_type dims
e04a16fb 663 {
c7303e41
APB
664 int osb = pop_current_osb (ctxp);
665 tree t = build_java_array_type (($1), -1);
666 CLASS_LOADED_P (t) = 1;
667 while (--osb)
668 t = build_unresolved_array_type (t);
669 $$ = t;
670 }
671| name dims
672 {
673 int osb = pop_current_osb (ctxp);
674 tree t = $1;
675 while (osb--)
676 t = build_unresolved_array_type (t);
677 $$ = t;
e04a16fb 678 }
e04a16fb
AG
679;
680
681/* 19.5 Productions from 6: Names */
682name:
683 simple_name /* Default rule */
684| qualified_name /* Default rule */
685;
686
687simple_name:
688 identifier /* Default rule */
689;
690
691qualified_name:
692 name DOT_TK identifier
693 { $$ = make_qualified_name ($1, $3, $2.location); }
694;
695
696identifier:
697 ID_TK
698;
699
700/* 19.6: Production from 7: Packages */
701compilation_unit:
702 {$$ = NULL;}
703| package_declaration
704| import_declarations
705| type_declarations
706| package_declaration import_declarations
707| package_declaration type_declarations
708| import_declarations type_declarations
709| package_declaration import_declarations type_declarations
710;
711
712import_declarations:
713 import_declaration
714 {
715 $$ = NULL;
716 }
717| import_declarations import_declaration
718 {
719 $$ = NULL;
720 }
721;
722
723type_declarations:
724 type_declaration
725| type_declarations type_declaration
726;
727
728package_declaration:
729 PACKAGE_TK name SC_TK
ee07f4f4
APB
730 {
731 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 732 register_package (ctxp->package);
ee07f4f4 733 }
e04a16fb
AG
734| PACKAGE_TK error
735 {yyerror ("Missing name"); RECOVER;}
736| PACKAGE_TK name error
737 {yyerror ("';' expected"); RECOVER;}
738;
739
740import_declaration:
741 single_type_import_declaration
742| type_import_on_demand_declaration
743;
744
745single_type_import_declaration:
746 IMPORT_TK name SC_TK
747 {
9a7ab4b3 748 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 749 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 750 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
751 while (last != IDENTIFIER_POINTER (name))
752 {
753 if (last [0] == '.')
754 break;
755 last--;
756 }
757 last_name = get_identifier (++last);
758 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
759 {
760 tree err = find_name_in_single_imports (last_name);
761 if (err && err != name)
762 parse_error_context
763 ($2, "Ambiguous class: `%s' and `%s'",
764 IDENTIFIER_POINTER (name),
765 IDENTIFIER_POINTER (err));
5e942c50 766 else
9a7ab4b3 767 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
768 }
769 else
5e942c50 770 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
771 }
772| IMPORT_TK error
773 {yyerror ("Missing name"); RECOVER;}
774| IMPORT_TK name error
775 {yyerror ("';' expected"); RECOVER;}
776;
777
778type_import_on_demand_declaration:
779 IMPORT_TK name DOT_TK MULT_TK SC_TK
780 {
781 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
782 /* Don't import java.lang.* twice. */
783 if (name != java_lang_id)
784 {
ba179f9f 785 read_import_dir ($2);
9a7ab4b3
APB
786 ctxp->import_demand_list =
787 chainon (ctxp->import_demand_list,
788 build_tree_list ($2, NULL_TREE));
ba179f9f 789 }
e04a16fb
AG
790 }
791| IMPORT_TK name DOT_TK error
792 {yyerror ("'*' expected"); RECOVER;}
793| IMPORT_TK name DOT_TK MULT_TK error
794 {yyerror ("';' expected"); RECOVER;}
795;
796
797type_declaration:
798 class_declaration
c2952b01 799 { end_class_declaration (0); }
e04a16fb 800| interface_declaration
c2952b01 801 { end_class_declaration (0); }
5f1c312a 802| empty_statement
e04a16fb
AG
803| error
804 {
805 YYERROR_NOW;
806 yyerror ("Class or interface declaration expected");
807 }
808;
809
810/* 19.7 Shortened from the original:
811 modifiers: modifier | modifiers modifier
812 modifier: any of public... */
813modifiers:
814 MODIFIER_TK
815 {
816 $$ = (1 << $1);
817 }
818| modifiers MODIFIER_TK
819 {
820 int acc = (1 << $2);
821 if ($$ & acc)
822 parse_error_context
823 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
824 java_accstring_lookup (acc));
825 else
826 {
827 $$ |= acc;
828 }
829 }
830;
831
832/* 19.8.1 Production from $8.1: Class Declaration */
833class_declaration:
834 modifiers CLASS_TK identifier super interfaces
835 { create_class ($1, $3, $4, $5); }
836 class_body
e04a16fb
AG
837| CLASS_TK identifier super interfaces
838 { create_class (0, $2, $3, $4); }
839 class_body
e04a16fb
AG
840| modifiers CLASS_TK error
841 {yyerror ("Missing class name"); RECOVER;}
842| CLASS_TK error
843 {yyerror ("Missing class name"); RECOVER;}
844| CLASS_TK identifier error
0b4d333e
APB
845 {
846 if (!ctxp->class_err) yyerror ("'{' expected");
847 DRECOVER(class1);
848 }
e04a16fb
AG
849| modifiers CLASS_TK identifier error
850 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
851;
852
853super:
854 { $$ = NULL; }
855| EXTENDS_TK class_type
856 { $$ = $2; }
857| EXTENDS_TK class_type error
858 {yyerror ("'{' expected"); ctxp->class_err=1;}
859| EXTENDS_TK error
860 {yyerror ("Missing super class name"); ctxp->class_err=1;}
861;
862
863interfaces:
864 { $$ = NULL_TREE; }
865| IMPLEMENTS_TK interface_type_list
866 { $$ = $2; }
867| IMPLEMENTS_TK error
868 {
869 ctxp->class_err=1;
870 yyerror ("Missing interface name");
871 }
872;
873
874interface_type_list:
875 interface_type
876 {
877 ctxp->interface_number = 1;
878 $$ = build_tree_list ($1, NULL_TREE);
879 }
880| interface_type_list C_TK interface_type
881 {
882 ctxp->interface_number++;
883 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
884 }
885| interface_type_list C_TK error
886 {yyerror ("Missing interface name"); RECOVER;}
887;
888
889class_body:
890 OCB_TK CCB_TK
7f10c2e2
APB
891 {
892 /* Store the location of the `}' when doing xrefs */
893 if (flag_emit_xref)
c2952b01 894 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 895 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 896 $$ = GET_CPC ();
7f10c2e2 897 }
e04a16fb 898| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
899 {
900 /* Store the location of the `}' when doing xrefs */
901 if (flag_emit_xref)
c2952b01 902 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 903 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 904 $$ = GET_CPC ();
7f10c2e2 905 }
e04a16fb
AG
906;
907
908class_body_declarations:
909 class_body_declaration
910| class_body_declarations class_body_declaration
911;
912
913class_body_declaration:
914 class_member_declaration
915| static_initializer
916| constructor_declaration
917| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
918 {
919 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
920 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
921 }
e04a16fb
AG
922;
923
924class_member_declaration:
925 field_declaration
926| method_declaration
927| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
928 { end_class_declaration (1); }
929| interface_declaration /* Added, JDK1.1 inner interfaces */
930 { end_class_declaration (1); }
5f1c312a 931| empty_statement
e04a16fb
AG
932;
933
934/* 19.8.2 Productions from 8.3: Field Declarations */
935field_declaration:
936 type variable_declarators SC_TK
937 { register_fields (0, $1, $2); }
938| modifiers type variable_declarators SC_TK
939 {
e04a16fb
AG
940 check_modifiers
941 ("Illegal modifier `%s' for field declaration",
942 $1, FIELD_MODIFIERS);
943 check_modifiers_consistency ($1);
944 register_fields ($1, $2, $3);
945 }
946;
947
948variable_declarators:
949 /* Should we use build_decl_list () instead ? FIXME */
950 variable_declarator /* Default rule */
951| variable_declarators C_TK variable_declarator
952 { $$ = chainon ($1, $3); }
953| variable_declarators C_TK error
954 {yyerror ("Missing term"); RECOVER;}
955;
956
957variable_declarator:
958 variable_declarator_id
959 { $$ = build_tree_list ($1, NULL_TREE); }
960| variable_declarator_id ASSIGN_TK variable_initializer
961 {
962 if (java_error_count)
963 $3 = NULL_TREE;
964 $$ = build_tree_list
965 ($1, build_assignment ($2.token, $2.location, $1, $3));
966 }
967| variable_declarator_id ASSIGN_TK error
968 {
969 yyerror ("Missing variable initializer");
970 $$ = build_tree_list ($1, NULL_TREE);
971 RECOVER;
972 }
973| variable_declarator_id ASSIGN_TK variable_initializer error
974 {
975 yyerror ("';' expected");
976 $$ = build_tree_list ($1, NULL_TREE);
977 RECOVER;
978 }
979;
980
981variable_declarator_id:
982 identifier
983| variable_declarator_id OSB_TK CSB_TK
c583dd46 984 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
985| identifier error
986 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
987| variable_declarator_id OSB_TK error
29f8b718
APB
988 {
989 tree node = java_lval.node;
990 if (node && (TREE_CODE (node) == INTEGER_CST
991 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
992 yyerror ("Can't specify array dimension in a declaration");
993 else
994 yyerror ("']' expected");
995 DRECOVER(vdi);
996 }
e04a16fb
AG
997| variable_declarator_id CSB_TK error
998 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
999;
1000
1001variable_initializer:
1002 expression
1003| array_initializer
e04a16fb
AG
1004;
1005
1006/* 19.8.3 Productions from 8.4: Method Declarations */
1007method_declaration:
1008 method_header
1009 {
1010 current_function_decl = $1;
c2952b01
APB
1011 if (current_function_decl
1012 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1013 source_start_java_method (current_function_decl);
1014 else
1015 current_function_decl = NULL_TREE;
e04a16fb
AG
1016 }
1017 method_body
b635eb2f 1018 { finish_method_declaration ($3); }
e04a16fb
AG
1019| method_header error
1020 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1021;
1022
1023method_header:
1024 type method_declarator throws
b9f7e36c 1025 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1026| VOID_TK method_declarator throws
b9f7e36c 1027 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1028| modifiers type method_declarator throws
b9f7e36c 1029 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1030| modifiers VOID_TK method_declarator throws
b9f7e36c 1031 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1032| type error
efa0a23f
APB
1033 {
1034 yyerror ("Invalid method declaration, method name required");
1035 RECOVER;
1036 }
e04a16fb
AG
1037| modifiers type error
1038 {RECOVER;}
1039| VOID_TK error
1040 {yyerror ("Identifier expected"); RECOVER;}
1041| modifiers VOID_TK error
1042 {yyerror ("Identifier expected"); RECOVER;}
1043| modifiers error
1044 {
1045 yyerror ("Invalid method declaration, return type required");
1046 RECOVER;
1047 }
1048;
1049
1050method_declarator:
1051 identifier OP_TK CP_TK
c2952b01
APB
1052 {
1053 ctxp->formal_parameter_number = 0;
1054 $$ = method_declarator ($1, NULL_TREE);
1055 }
e04a16fb
AG
1056| identifier OP_TK formal_parameter_list CP_TK
1057 { $$ = method_declarator ($1, $3); }
1058| method_declarator OSB_TK CSB_TK
1059 {
1886c9d8
APB
1060 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1061 TREE_PURPOSE ($1) =
1062 build_unresolved_array_type (TREE_PURPOSE ($1));
1063 parse_warning_context
1064 (wfl_operator,
1065 "Discouraged form of returned type specification");
e04a16fb
AG
1066 }
1067| identifier OP_TK error
1068 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1069| method_declarator OSB_TK error
1070 {yyerror ("']' expected"); RECOVER;}
1071;
1072
1073formal_parameter_list:
1074 formal_parameter
1075 {
1076 ctxp->formal_parameter_number = 1;
1077 }
1078| formal_parameter_list C_TK formal_parameter
1079 {
1080 ctxp->formal_parameter_number += 1;
1081 $$ = chainon ($1, $3);
1082 }
1083| formal_parameter_list C_TK error
c2952b01 1084 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1085;
1086
1087formal_parameter:
1088 type variable_declarator_id
1089 {
1090 $$ = build_tree_list ($2, $1);
1091 }
18990de5 1092| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1093 {
5256aa37 1094 $$ = build_tree_list ($3, $2);
c2952b01 1095 ARG_FINAL_P ($$) = 1;
5256aa37 1096 }
e04a16fb 1097| type error
f8989a66
APB
1098 {
1099 yyerror ("Missing identifier"); RECOVER;
1100 $$ = NULL_TREE;
1101 }
18990de5 1102| final type error
e04a16fb 1103 {
e04a16fb 1104 yyerror ("Missing identifier"); RECOVER;
f8989a66 1105 $$ = NULL_TREE;
e04a16fb
AG
1106 }
1107;
1108
18990de5
JB
1109final:
1110 modifiers
1111 {
1112 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1113 $1, ACC_FINAL);
1114 if ($1 != ACC_FINAL)
1115 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1116 }
1117;
1118
e04a16fb 1119throws:
b9f7e36c 1120 { $$ = NULL_TREE; }
e04a16fb 1121| THROWS_TK class_type_list
b9f7e36c 1122 { $$ = $2; }
e04a16fb
AG
1123| THROWS_TK error
1124 {yyerror ("Missing class type term"); RECOVER;}
1125;
1126
1127class_type_list:
1128 class_type
c877974e 1129 { $$ = build_tree_list ($1, $1); }
e04a16fb 1130| class_type_list C_TK class_type
c877974e 1131 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1132| class_type_list C_TK error
1133 {yyerror ("Missing class type term"); RECOVER;}
1134;
1135
1136method_body:
1137 block
5f1c312a 1138| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1139;
1140
1141/* 19.8.4 Productions from 8.5: Static Initializers */
1142static_initializer:
1143 static block
1144 {
c2952b01
APB
1145 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1146 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1147 current_static_block = NULL_TREE;
e04a16fb 1148 }
e04a16fb
AG
1149;
1150
1151static: /* Test lval.sub_token here */
c2952b01 1152 modifiers
e04a16fb 1153 {
c2952b01
APB
1154 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1155 /* Can't have a static initializer in an innerclass */
1156 if ($1 | ACC_STATIC &&
1157 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1158 parse_error_context
1159 (MODIFIER_WFL (STATIC_TK),
1160 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1161 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1162 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1163 }
1164;
1165
1166/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1167constructor_declaration:
22eed1e6 1168 constructor_header
e04a16fb 1169 {
22eed1e6
APB
1170 current_function_decl = $1;
1171 source_start_java_method (current_function_decl);
e04a16fb 1172 }
22eed1e6 1173 constructor_body
b635eb2f 1174 { finish_method_declaration ($3); }
22eed1e6
APB
1175;
1176
1177constructor_header:
1178 constructor_declarator throws
1179 { $$ = method_header (0, NULL_TREE, $1, $2); }
1180| modifiers constructor_declarator throws
1181 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1182;
1183
1184constructor_declarator:
1185 simple_name OP_TK CP_TK
c2952b01
APB
1186 {
1187 ctxp->formal_parameter_number = 0;
1188 $$ = method_declarator ($1, NULL_TREE);
1189 }
e04a16fb 1190| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1191 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1192;
1193
1194constructor_body:
22eed1e6
APB
1195 /* Unlike regular method, we always need a complete (empty)
1196 body so we can safely perform all the required code
1197 addition (super invocation and field initialization) */
2e5eb5c5 1198 block_begin constructor_block_end
22eed1e6 1199 {
9bbc7d9f 1200 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1201 $$ = $2;
1202 }
2e5eb5c5 1203| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1204 { $$ = $3; }
2e5eb5c5 1205| block_begin block_statements constructor_block_end
22eed1e6 1206 { $$ = $3; }
2e5eb5c5 1207| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1208 { $$ = $4; }
e04a16fb
AG
1209;
1210
2e5eb5c5
APB
1211constructor_block_end:
1212 block_end
5f1c312a 1213;
2e5eb5c5 1214
e04a16fb
AG
1215/* Error recovery for that rule moved down expression_statement: rule. */
1216explicit_constructor_invocation:
1217 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1218 {
1219 $$ = build_method_invocation ($1, NULL_TREE);
1220 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1221 $$ = java_method_add_stmt (current_function_decl, $$);
1222 }
e04a16fb 1223| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1224 {
1225 $$ = build_method_invocation ($1, $3);
1226 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1227 $$ = java_method_add_stmt (current_function_decl, $$);
1228 }
e04a16fb
AG
1229 /* Added, JDK1.1 inner classes. Modified because the rule
1230 'primary' couldn't work. */
1231| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1232 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1233| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1234 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1235;
1236
1237this_or_super: /* Added, simplifies error diagnostics */
1238 THIS_TK
1239 {
9ee9b555 1240 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1241 EXPR_WFL_LINECOL (wfl) = $1.location;
1242 $$ = wfl;
1243 }
1244| SUPER_TK
1245 {
9ee9b555 1246 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1247 EXPR_WFL_LINECOL (wfl) = $1.location;
1248 $$ = wfl;
1249 }
1250;
1251
1252/* 19.9 Productions from 9: Interfaces */
1253/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1254interface_declaration:
1255 INTERFACE_TK identifier
1256 { create_interface (0, $2, NULL_TREE); }
1257 interface_body
e04a16fb
AG
1258| modifiers INTERFACE_TK identifier
1259 { create_interface ($1, $3, NULL_TREE); }
1260 interface_body
e04a16fb
AG
1261| INTERFACE_TK identifier extends_interfaces
1262 { create_interface (0, $2, $3); }
1263 interface_body
e04a16fb
AG
1264| modifiers INTERFACE_TK identifier extends_interfaces
1265 { create_interface ($1, $3, $4); }
1266 interface_body
e04a16fb 1267| INTERFACE_TK identifier error
0b4d333e 1268 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1269| modifiers INTERFACE_TK identifier error
0b4d333e 1270 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1271;
1272
1273extends_interfaces:
1274 EXTENDS_TK interface_type
1275 {
1276 ctxp->interface_number = 1;
1277 $$ = build_tree_list ($2, NULL_TREE);
1278 }
1279| extends_interfaces C_TK interface_type
1280 {
1281 ctxp->interface_number++;
1282 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1283 }
1284| EXTENDS_TK error
1285 {yyerror ("Invalid interface type"); RECOVER;}
1286| extends_interfaces C_TK error
1287 {yyerror ("Missing term"); RECOVER;}
1288;
1289
1290interface_body:
1291 OCB_TK CCB_TK
1292 { $$ = NULL_TREE; }
1293| OCB_TK interface_member_declarations CCB_TK
1294 { $$ = NULL_TREE; }
1295;
1296
1297interface_member_declarations:
1298 interface_member_declaration
1299| interface_member_declarations interface_member_declaration
1300;
1301
1302interface_member_declaration:
1303 constant_declaration
1304| abstract_method_declaration
1305| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1306 { end_class_declaration (1); }
1307| interface_declaration /* Added, JDK1.1 inner interfaces */
1308 { end_class_declaration (1); }
e04a16fb
AG
1309;
1310
1311constant_declaration:
1312 field_declaration
1313;
1314
1315abstract_method_declaration:
1316 method_header SC_TK
1317 {
1318 check_abstract_method_header ($1);
1319 current_function_decl = NULL_TREE; /* FIXME ? */
1320 }
1321| method_header error
1322 {yyerror ("';' expected"); RECOVER;}
1323;
1324
1325/* 19.10 Productions from 10: Arrays */
1326array_initializer:
1327 OCB_TK CCB_TK
1179ebc2 1328 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1329| OCB_TK variable_initializers CCB_TK
f8976021 1330 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1331| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1332 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1333;
1334
1335variable_initializers:
1336 variable_initializer
f8976021
APB
1337 {
1338 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1339 $1, NULL_TREE);
1340 }
e04a16fb 1341| variable_initializers C_TK variable_initializer
1179ebc2
APB
1342 {
1343 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1344 }
e04a16fb
AG
1345| variable_initializers C_TK error
1346 {yyerror ("Missing term"); RECOVER;}
1347;
1348
1349/* 19.11 Production from 14: Blocks and Statements */
1350block:
1351 OCB_TK CCB_TK
7f10c2e2
APB
1352 {
1353 /* Store the location of the `}' when doing xrefs */
1354 if (current_function_decl && flag_emit_xref)
1355 DECL_END_SOURCE_LINE (current_function_decl) =
1356 EXPR_WFL_ADD_COL ($2.location, 1);
1357 $$ = empty_stmt_node;
1358 }
22eed1e6
APB
1359| block_begin block_statements block_end
1360 { $$ = $3; }
1361;
1362
1363block_begin:
1364 OCB_TK
e04a16fb 1365 { enter_block (); }
22eed1e6
APB
1366;
1367
1368block_end:
e04a16fb
AG
1369 CCB_TK
1370 {
1371 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1372 /* Store the location of the `}' when doing xrefs */
1373 if (current_function_decl && flag_emit_xref)
1374 DECL_END_SOURCE_LINE (current_function_decl) =
1375 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1376 $$ = exit_block ();
c280e37a
APB
1377 if (!BLOCK_SUBBLOCKS ($$))
1378 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1379 }
1380;
1381
1382block_statements:
1383 block_statement
1384| block_statements block_statement
1385;
1386
1387block_statement:
1388 local_variable_declaration_statement
1389| statement
15fdcfe9 1390 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1391| class_declaration /* Added, JDK1.1 local classes */
1392 {
1393 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1394 end_class_declaration (1);
1395 }
e04a16fb
AG
1396;
1397
1398local_variable_declaration_statement:
1399 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1400;
1401
1402local_variable_declaration:
1403 type variable_declarators
1404 { declare_local_variables (0, $1, $2); }
a003f638 1405| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1406 { declare_local_variables ($1, $2, $3); }
1407;
1408
1409statement:
1410 statement_without_trailing_substatement
1411| labeled_statement
e04a16fb 1412| if_then_statement
e04a16fb 1413| if_then_else_statement
e04a16fb 1414| while_statement
e04a16fb 1415| for_statement
cd9643f7 1416 { $$ = exit_block (); }
e04a16fb
AG
1417;
1418
1419statement_nsi:
1420 statement_without_trailing_substatement
1421| labeled_statement_nsi
e04a16fb 1422| if_then_else_statement_nsi
e04a16fb 1423| while_statement_nsi
e04a16fb 1424| for_statement_nsi
9dd939b2 1425 { $$ = exit_block (); }
e04a16fb
AG
1426;
1427
1428statement_without_trailing_substatement:
1429 block
e04a16fb 1430| empty_statement
e04a16fb 1431| expression_statement
e04a16fb 1432| switch_statement
e04a16fb 1433| do_statement
e04a16fb 1434| break_statement
e04a16fb 1435| continue_statement
e04a16fb
AG
1436| return_statement
1437| synchronized_statement
e04a16fb 1438| throw_statement
e04a16fb 1439| try_statement
e04a16fb
AG
1440;
1441
1442empty_statement:
1443 SC_TK
5f1c312a
APB
1444 {
1445 if (flag_extraneous_semicolon)
1446 {
1447 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1448 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1449 }
1450 $$ = empty_stmt_node;
1451 }
e04a16fb
AG
1452;
1453
1454label_decl:
1455 identifier REL_CL_TK
1456 {
1457 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1458 EXPR_WFL_NODE ($1));
e04a16fb
AG
1459 pushlevel (2);
1460 push_labeled_block ($$);
1461 PUSH_LABELED_BLOCK ($$);
1462 }
1463;
1464
1465labeled_statement:
1466 label_decl statement
b635eb2f 1467 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1468| identifier error
1469 {yyerror ("':' expected"); RECOVER;}
1470;
1471
1472labeled_statement_nsi:
1473 label_decl statement_nsi
b635eb2f 1474 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1475;
1476
1477/* We concentrate here a bunch of error handling rules that we couldn't write
1478 earlier, because expression_statement catches a missing ';'. */
1479expression_statement:
1480 statement_expression SC_TK
1481 {
1482 /* We have a statement. Generate a WFL around it so
1483 we can debug it */
1484 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1485 /* We know we have a statement, so set the debug
1486 info to be eventually generate here. */
1487 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1488 }
1489| error SC_TK
1490 {
29f8b718 1491 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1492 DRECOVER (expr_stmt);
1493 }
1494| error OCB_TK
1495 {
29f8b718 1496 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1497 DRECOVER (expr_stmt);
1498 }
1499| error CCB_TK
1500 {
29f8b718 1501 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1502 DRECOVER (expr_stmt);
1503 }
1504| this_or_super OP_TK error
1505 {yyerror ("')' expected"); RECOVER;}
1506| this_or_super OP_TK CP_TK error
22eed1e6 1507 {
8119c720 1508 parse_ctor_invocation_error ();
22eed1e6
APB
1509 RECOVER;
1510 }
e04a16fb
AG
1511| this_or_super OP_TK argument_list error
1512 {yyerror ("')' expected"); RECOVER;}
1513| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1514 {
8119c720 1515 parse_ctor_invocation_error ();
22eed1e6
APB
1516 RECOVER;
1517 }
e04a16fb
AG
1518| name DOT_TK SUPER_TK error
1519 {yyerror ("'(' expected"); RECOVER;}
1520| name DOT_TK SUPER_TK OP_TK error
1521 {yyerror ("')' expected"); RECOVER;}
1522| name DOT_TK SUPER_TK OP_TK argument_list error
1523 {yyerror ("')' expected"); RECOVER;}
1524| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1525 {yyerror ("';' expected"); RECOVER;}
1526| name DOT_TK SUPER_TK OP_TK CP_TK error
1527 {yyerror ("';' expected"); RECOVER;}
1528;
1529
1530statement_expression:
1531 assignment
1532| pre_increment_expression
e04a16fb 1533| pre_decrement_expression
e04a16fb 1534| post_increment_expression
e04a16fb 1535| post_decrement_expression
e04a16fb
AG
1536| method_invocation
1537| class_instance_creation_expression
e04a16fb
AG
1538;
1539
1540if_then_statement:
1541 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1542 {
1543 $$ = build_if_else_statement ($2.location, $3,
1544 $5, NULL_TREE);
1545 }
e04a16fb
AG
1546| IF_TK error
1547 {yyerror ("'(' expected"); RECOVER;}
1548| IF_TK OP_TK error
1549 {yyerror ("Missing term"); RECOVER;}
1550| IF_TK OP_TK expression error
1551 {yyerror ("')' expected"); RECOVER;}
1552;
1553
1554if_then_else_statement:
1555 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1556 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1557;
1558
1559if_then_else_statement_nsi:
1560 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1561 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1562;
1563
1564switch_statement:
15fdcfe9
PB
1565 switch_expression
1566 {
1567 enter_block ();
1568 }
1569 switch_block
b67d701b 1570 {
15fdcfe9 1571 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1572 I.e. make the last statment also have its own
1573 COMPOUND_EXPR. */
15fdcfe9
PB
1574 maybe_absorb_scoping_blocks ();
1575 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1576 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1577 }
1578;
1579
1580switch_expression:
1581 SWITCH_TK OP_TK expression CP_TK
1582 {
1583 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1584 EXPR_WFL_LINECOL ($$) = $2.location;
1585 }
e04a16fb
AG
1586| SWITCH_TK error
1587 {yyerror ("'(' expected"); RECOVER;}
1588| SWITCH_TK OP_TK error
1589 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1590| SWITCH_TK OP_TK expression CP_TK error
1591 {yyerror ("'{' expected"); RECOVER;}
1592;
1593
f8976021
APB
1594/* Default assignment is there to avoid type node on switch_block
1595 node. */
1596
e04a16fb
AG
1597switch_block:
1598 OCB_TK CCB_TK
f8976021 1599 { $$ = NULL_TREE; }
e04a16fb 1600| OCB_TK switch_labels CCB_TK
f8976021 1601 { $$ = NULL_TREE; }
e04a16fb 1602| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1603 { $$ = NULL_TREE; }
e04a16fb 1604| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1605 { $$ = NULL_TREE; }
e04a16fb
AG
1606;
1607
1608switch_block_statement_groups:
1609 switch_block_statement_group
1610| switch_block_statement_groups switch_block_statement_group
1611;
1612
1613switch_block_statement_group:
15fdcfe9 1614 switch_labels block_statements
e04a16fb
AG
1615;
1616
e04a16fb
AG
1617switch_labels:
1618 switch_label
1619| switch_labels switch_label
1620;
1621
1622switch_label:
1623 CASE_TK constant_expression REL_CL_TK
b67d701b 1624 {
15fdcfe9
PB
1625 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1626 EXPR_WFL_LINECOL (lab) = $1.location;
1627 java_method_add_stmt (current_function_decl, lab);
b67d701b 1628 }
e04a16fb 1629| DEFAULT_TK REL_CL_TK
b67d701b 1630 {
ac39dac0 1631 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1632 EXPR_WFL_LINECOL (lab) = $1.location;
1633 java_method_add_stmt (current_function_decl, lab);
b67d701b 1634 }
e04a16fb
AG
1635| CASE_TK error
1636 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1637| CASE_TK constant_expression error
1638 {yyerror ("':' expected"); RECOVER;}
1639| DEFAULT_TK error
1640 {yyerror ("':' expected"); RECOVER;}
1641;
1642
1643while_expression:
1644 WHILE_TK OP_TK expression CP_TK
1645 {
1646 tree body = build_loop_body ($2.location, $3, 0);
1647 $$ = build_new_loop (body);
1648 }
1649;
1650
1651while_statement:
1652 while_expression statement
b635eb2f 1653 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1654| WHILE_TK error
1655 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1656| WHILE_TK OP_TK error
1657 {yyerror ("Missing term and ')' expected"); RECOVER;}
1658| WHILE_TK OP_TK expression error
1659 {yyerror ("')' expected"); RECOVER;}
1660;
1661
1662while_statement_nsi:
1663 while_expression statement_nsi
b635eb2f 1664 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1665;
1666
1667do_statement_begin:
1668 DO_TK
1669 {
1670 tree body = build_loop_body (0, NULL_TREE, 1);
1671 $$ = build_new_loop (body);
1672 }
1673 /* Need error handing here. FIXME */
1674;
1675
1676do_statement:
1677 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1678 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1679;
1680
1681for_statement:
1682 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1683 {
1684 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1685 $3 = build_wfl_node ($3);
1686 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1687 }
e04a16fb
AG
1688| for_begin SC_TK SC_TK for_update CP_TK statement
1689 {
b635eb2f 1690 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1691 /* We have not condition, so we get rid of the EXIT_EXPR */
1692 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1693 empty_stmt_node;
e04a16fb
AG
1694 }
1695| for_begin SC_TK error
1696 {yyerror ("Invalid control expression"); RECOVER;}
1697| for_begin SC_TK expression SC_TK error
1698 {yyerror ("Invalid update expression"); RECOVER;}
1699| for_begin SC_TK SC_TK error
1700 {yyerror ("Invalid update expression"); RECOVER;}
1701;
1702
1703for_statement_nsi:
1704 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1705 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1706| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1707 {
b635eb2f 1708 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1709 /* We have not condition, so we get rid of the EXIT_EXPR */
1710 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1711 empty_stmt_node;
e04a16fb
AG
1712 }
1713;
1714
1715for_header:
1716 FOR_TK OP_TK
1717 {
1718 /* This scope defined for local variable that may be
1719 defined within the scope of the for loop */
1720 enter_block ();
1721 }
1722| FOR_TK error
1723 {yyerror ("'(' expected"); DRECOVER(for_1);}
1724| FOR_TK OP_TK error
1725 {yyerror ("Invalid init statement"); RECOVER;}
1726;
1727
1728for_begin:
1729 for_header for_init
1730 {
1731 /* We now declare the loop body. The loop is
1732 declared as a for loop. */
1733 tree body = build_loop_body (0, NULL_TREE, 0);
1734 $$ = build_new_loop (body);
c2952b01 1735 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1736 /* The loop is added to the current block the for
1737 statement is defined within */
1738 java_method_add_stmt (current_function_decl, $$);
1739 }
1740;
1741for_init: /* Can be empty */
9bbc7d9f 1742 { $$ = empty_stmt_node; }
e04a16fb
AG
1743| statement_expression_list
1744 {
1745 /* Init statement recorded within the previously
1746 defined block scope */
1747 $$ = java_method_add_stmt (current_function_decl, $1);
1748 }
1749| local_variable_declaration
1750 {
1751 /* Local variable are recorded within the previously
1752 defined block scope */
1753 $$ = NULL_TREE;
1754 }
1755| statement_expression_list error
1756 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1757;
1758
1759for_update: /* Can be empty */
9bbc7d9f 1760 {$$ = empty_stmt_node;}
e04a16fb
AG
1761| statement_expression_list
1762 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1763;
1764
1765statement_expression_list:
1766 statement_expression
1767 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1768| statement_expression_list C_TK statement_expression
1769 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1770| statement_expression_list C_TK error
1771 {yyerror ("Missing term"); RECOVER;}
1772;
1773
1774break_statement:
1775 BREAK_TK SC_TK
1776 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1777| BREAK_TK identifier SC_TK
1778 { $$ = build_bc_statement ($1.location, 1, $2); }
1779| BREAK_TK error
1780 {yyerror ("Missing term"); RECOVER;}
1781| BREAK_TK identifier error
1782 {yyerror ("';' expected"); RECOVER;}
1783;
1784
1785continue_statement:
1786 CONTINUE_TK SC_TK
1787 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1788| CONTINUE_TK identifier SC_TK
1789 { $$ = build_bc_statement ($1.location, 0, $2); }
1790| CONTINUE_TK error
1791 {yyerror ("Missing term"); RECOVER;}
1792| CONTINUE_TK identifier error
1793 {yyerror ("';' expected"); RECOVER;}
1794;
1795
1796return_statement:
1797 RETURN_TK SC_TK
1798 { $$ = build_return ($1.location, NULL_TREE); }
1799| RETURN_TK expression SC_TK
1800 { $$ = build_return ($1.location, $2); }
1801| RETURN_TK error
1802 {yyerror ("Missing term"); RECOVER;}
1803| RETURN_TK expression error
1804 {yyerror ("';' expected"); RECOVER;}
1805;
1806
1807throw_statement:
1808 THROW_TK expression SC_TK
b9f7e36c
APB
1809 {
1810 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1811 EXPR_WFL_LINECOL ($$) = $1.location;
1812 }
e04a16fb
AG
1813| THROW_TK error
1814 {yyerror ("Missing term"); RECOVER;}
1815| THROW_TK expression error
1816 {yyerror ("';' expected"); RECOVER;}
1817;
1818
1819synchronized_statement:
1820 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1821 {
1822 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1823 EXPR_WFL_LINECOL ($$) =
1824 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1825 }
e04a16fb
AG
1826| synchronized OP_TK expression CP_TK error
1827 {yyerror ("'{' expected"); RECOVER;}
1828| synchronized error
1829 {yyerror ("'(' expected"); RECOVER;}
1830| synchronized OP_TK error CP_TK
1831 {yyerror ("Missing term"); RECOVER;}
1832| synchronized OP_TK error
1833 {yyerror ("Missing term"); RECOVER;}
1834;
1835
b9f7e36c 1836synchronized:
efa0a23f 1837 modifiers
e04a16fb 1838 {
781b0558
KG
1839 check_modifiers (
1840 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1841 $1, ACC_SYNCHRONIZED);
1842 if ($1 != ACC_SYNCHRONIZED)
1843 MODIFIER_WFL (SYNCHRONIZED_TK) =
1844 build_wfl_node (NULL_TREE);
e04a16fb
AG
1845 }
1846;
1847
1848try_statement:
1849 TRY_TK block catches
a7d8d81f 1850 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1851| TRY_TK block finally
a7d8d81f 1852 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1853| TRY_TK block catches finally
2aa11e97
APB
1854 { $$ = build_try_finally_statement
1855 ($1.location, build_try_statement ($1.location,
1856 $2, $3), $4);
1857 }
e04a16fb
AG
1858| TRY_TK error
1859 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1860;
1861
1862catches:
1863 catch_clause
1864| catches catch_clause
b67d701b
PB
1865 {
1866 TREE_CHAIN ($2) = $1;
1867 $$ = $2;
1868 }
e04a16fb
AG
1869;
1870
1871catch_clause:
b67d701b
PB
1872 catch_clause_parameter block
1873 {
1874 java_method_add_stmt (current_function_decl, $2);
1875 exit_block ();
1876 $$ = $1;
1877 }
1878
1879catch_clause_parameter:
1880 CATCH_TK OP_TK formal_parameter CP_TK
1881 {
1882 /* We add a block to define a scope for
1883 formal_parameter (CCBP). The formal parameter is
1884 declared initialized by the appropriate function
1885 call */
1886 tree ccpb = enter_block ();
1887 tree init = build_assignment (ASSIGN_TK, $2.location,
1888 TREE_PURPOSE ($3),
1889 soft_exceptioninfo_call_node);
1890 declare_local_variables (0, TREE_VALUE ($3),
1891 build_tree_list (TREE_PURPOSE ($3),
1892 init));
1893 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1894 EXPR_WFL_LINECOL ($$) = $1.location;
1895 }
e04a16fb 1896| CATCH_TK error
97f30284 1897 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1898| CATCH_TK OP_TK error
97f30284
APB
1899 {
1900 yyerror ("Missing term or ')' expected");
1901 RECOVER; $$ = NULL_TREE;
1902 }
b67d701b 1903| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1904 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1905;
1906
1907finally:
1908 FINALLY_TK block
a7d8d81f 1909 { $$ = $2; }
e04a16fb
AG
1910| FINALLY_TK error
1911 {yyerror ("'{' expected"); RECOVER; }
1912;
1913
1914/* 19.12 Production from 15: Expressions */
1915primary:
1916 primary_no_new_array
1917| array_creation_expression
1918;
1919
1920primary_no_new_array:
1921 literal
1922| THIS_TK
1923 { $$ = build_this ($1.location); }
1924| OP_TK expression CP_TK
1925 {$$ = $2;}
1926| class_instance_creation_expression
1927| field_access
1928| method_invocation
1929| array_access
c2952b01 1930| type_literals
e04a16fb
AG
1931 /* Added, JDK1.1 inner classes. Documentation is wrong
1932 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1933 exist. Used name: instead. */
e04a16fb 1934| name DOT_TK THIS_TK
c2952b01
APB
1935 {
1936 tree wfl = build_wfl_node (this_identifier_node);
1937 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1938 }
e04a16fb
AG
1939| OP_TK expression error
1940 {yyerror ("')' expected"); RECOVER;}
1941| name DOT_TK error
1942 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1943| primitive_type DOT_TK error
1944 {yyerror ("'class' expected" ); RECOVER;}
1945| VOID_TK DOT_TK error
1946 {yyerror ("'class' expected" ); RECOVER;}
1947;
1948
c2952b01
APB
1949type_literals:
1950 name DOT_TK CLASS_TK
1951 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1952| array_type DOT_TK CLASS_TK
c2952b01
APB
1953 { $$ = build_incomplete_class_ref ($2.location, $1); }
1954| primitive_type DOT_TK CLASS_TK
1955 { $$ = build_class_ref ($1); }
1956| VOID_TK DOT_TK CLASS_TK
1957 { $$ = build_class_ref (void_type_node); }
1958;
1959
e04a16fb
AG
1960class_instance_creation_expression:
1961 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1962 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1963| NEW_TK class_type OP_TK CP_TK
b67d701b 1964 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1965| anonymous_class_creation
e04a16fb
AG
1966 /* Added, JDK1.1 inner classes, modified to use name or
1967 primary instead of primary solely which couldn't work in
1968 all situations. */
1969| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1970 {
1971 tree ctor = build_new_invocation ($2, NULL_TREE);
1972 $$ = make_qualified_primary ($1, ctor,
1973 EXPR_WFL_LINECOL ($1));
1974 }
e04a16fb
AG
1975| something_dot_new identifier OP_TK CP_TK class_body
1976| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1977 {
1978 tree ctor = build_new_invocation ($2, $4);
1979 $$ = make_qualified_primary ($1, ctor,
1980 EXPR_WFL_LINECOL ($1));
1981 }
e04a16fb
AG
1982| something_dot_new identifier OP_TK argument_list CP_TK class_body
1983| NEW_TK error SC_TK
1984 {yyerror ("'(' expected"); DRECOVER(new_1);}
1985| NEW_TK class_type error
1986 {yyerror ("'(' expected"); RECOVER;}
1987| NEW_TK class_type OP_TK error
1988 {yyerror ("')' or term expected"); RECOVER;}
1989| NEW_TK class_type OP_TK argument_list error
1990 {yyerror ("')' expected"); RECOVER;}
1991| something_dot_new error
1992 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1993| something_dot_new identifier error
1994 {yyerror ("'(' expected"); RECOVER;}
1995;
1996
c2952b01
APB
1997/* Created after JDK1.1 rules originally added to
1998 class_instance_creation_expression, but modified to use
1999 'class_type' instead of 'TypeName' (type_name) which is mentionned
2000 in the documentation but doesn't exist. */
2001
2002anonymous_class_creation:
2003 NEW_TK class_type OP_TK argument_list CP_TK
2004 { create_anonymous_class ($1.location, $2); }
2005 class_body
2006 {
2007 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2008 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2009
2010 end_class_declaration (1);
2011
2012 /* Now we can craft the new expression */
2013 $$ = build_new_invocation (id, $4);
2014
2015 /* Note that we can't possibly be here if
2016 `class_type' is an interface (in which case the
2017 anonymous class extends Object and implements
2018 `class_type', hence its constructor can't have
2019 arguments.) */
2020
2021 /* Otherwise, the innerclass must feature a
2022 constructor matching `argument_list'. Anonymous
2023 classes are a bit special: it's impossible to
2024 define constructor for them, hence constructors
2025 must be generated following the hints provided by
2026 the `new' expression. Whether a super constructor
2027 of that nature exists or not is to be verified
2028 later on in verify_constructor_super.
2029
2030 It's during the expansion of a `new' statement
2031 refering to an anonymous class that a ctor will
2032 be generated for the anonymous class, with the
2033 right arguments. */
2034
2035 }
2036| NEW_TK class_type OP_TK CP_TK
2037 { create_anonymous_class ($1.location, $2); }
2038 class_body
2039 {
2040 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2041 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2042
2043 end_class_declaration (1);
2044
2045 /* Now we can craft the new expression. The
2046 statement doesn't need to be remember so that a
2047 constructor can be generated, since its signature
2048 is already known. */
2049 $$ = build_new_invocation (id, NULL_TREE);
2050 }
2051;
2052
e04a16fb
AG
2053something_dot_new: /* Added, not part of the specs. */
2054 name DOT_TK NEW_TK
c2952b01 2055 { $$ = $1; }
e04a16fb 2056| primary DOT_TK NEW_TK
c2952b01 2057 { $$ = $1; }
e04a16fb
AG
2058;
2059
2060argument_list:
2061 expression
2062 {
2063 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2064 ctxp->formal_parameter_number = 1;
2065 }
2066| argument_list C_TK expression
2067 {
2068 ctxp->formal_parameter_number += 1;
2069 $$ = tree_cons (NULL_TREE, $3, $1);
2070 }
2071| argument_list C_TK error
2072 {yyerror ("Missing term"); RECOVER;}
2073;
2074
2075array_creation_expression:
2076 NEW_TK primitive_type dim_exprs
2077 { $$ = build_newarray_node ($2, $3, 0); }
2078| NEW_TK class_or_interface_type dim_exprs
2079 { $$ = build_newarray_node ($2, $3, 0); }
2080| NEW_TK primitive_type dim_exprs dims
c7303e41 2081 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2082| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2083 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2084 /* Added, JDK1.1 anonymous array. Initial documentation rule
2085 modified */
2086| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2087 {
2088 char *sig;
c7303e41
APB
2089 int osb = pop_current_osb (ctxp);
2090 while (osb--)
c2952b01
APB
2091 obstack_1grow (&temporary_obstack, '[');
2092 sig = obstack_finish (&temporary_obstack);
2093 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2094 $2, get_identifier (sig), $4);
2095 }
e04a16fb 2096| NEW_TK primitive_type dims array_initializer
c2952b01 2097 {
c7303e41 2098 int osb = pop_current_osb (ctxp);
c2952b01 2099 tree type = $2;
c7303e41 2100 while (osb--)
c2952b01
APB
2101 type = build_java_array_type (type, -1);
2102 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2103 build_pointer_type (type), NULL_TREE, $4);
2104 }
e04a16fb
AG
2105| NEW_TK error CSB_TK
2106 {yyerror ("'[' expected"); DRECOVER ("]");}
2107| NEW_TK error OSB_TK
2108 {yyerror ("']' expected"); RECOVER;}
2109;
2110
2111dim_exprs:
2112 dim_expr
2113 { $$ = build_tree_list (NULL_TREE, $1); }
2114| dim_exprs dim_expr
2115 { $$ = tree_cons (NULL_TREE, $2, $$); }
2116;
2117
2118dim_expr:
2119 OSB_TK expression CSB_TK
2120 {
9a7ab4b3
APB
2121 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2122 {
2123 $2 = build_wfl_node ($2);
2124 TREE_TYPE ($2) = NULL_TREE;
2125 }
e04a16fb
AG
2126 EXPR_WFL_LINECOL ($2) = $1.location;
2127 $$ = $2;
2128 }
2129| OSB_TK expression error
2130 {yyerror ("']' expected"); RECOVER;}
2131| OSB_TK error
2132 {
2133 yyerror ("Missing term");
2134 yyerror ("']' expected");
2135 RECOVER;
2136 }
2137;
2138
2139dims:
2140 OSB_TK CSB_TK
ba179f9f
APB
2141 {
2142 int allocate = 0;
2143 /* If not initialized, allocate memory for the osb
2144 numbers stack */
2145 if (!ctxp->osb_limit)
2146 {
2147 allocate = ctxp->osb_limit = 32;
2148 ctxp->osb_depth = -1;
2149 }
c2952b01 2150 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2151 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2152 allocate = ctxp->osb_limit << 1;
2153
2154 if (allocate)
2155 {
2156 allocate *= sizeof (int);
2157 if (ctxp->osb_number)
2158 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2159 allocate);
2160 else
2161 ctxp->osb_number = (int *)xmalloc (allocate);
2162 }
2163 ctxp->osb_depth++;
2164 CURRENT_OSB (ctxp) = 1;
2165 }
e04a16fb 2166| dims OSB_TK CSB_TK
ba179f9f 2167 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2168| dims OSB_TK error
2169 { yyerror ("']' expected"); RECOVER;}
2170;
2171
2172field_access:
2173 primary DOT_TK identifier
2174 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2175 /* FIXME - REWRITE TO:
2176 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2177| SUPER_TK DOT_TK identifier
2178 {
6e22695a 2179 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2180 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2181 $$ = make_qualified_name (super_wfl, $3, $2.location);
2182 }
2183| SUPER_TK error
2184 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2185;
2186
2187method_invocation:
2188 name OP_TK CP_TK
2189 { $$ = build_method_invocation ($1, NULL_TREE); }
2190| name OP_TK argument_list CP_TK
2191 { $$ = build_method_invocation ($1, $3); }
2192| primary DOT_TK identifier OP_TK CP_TK
2193 {
22eed1e6
APB
2194 if (TREE_CODE ($1) == THIS_EXPR)
2195 $$ = build_this_super_qualified_invocation
2196 (1, $3, NULL_TREE, 0, $2.location);
2197 else
2198 {
2199 tree invok = build_method_invocation ($3, NULL_TREE);
2200 $$ = make_qualified_primary ($1, invok, $2.location);
2201 }
e04a16fb
AG
2202 }
2203| primary DOT_TK identifier OP_TK argument_list CP_TK
2204 {
22eed1e6
APB
2205 if (TREE_CODE ($1) == THIS_EXPR)
2206 $$ = build_this_super_qualified_invocation
2207 (1, $3, $5, 0, $2.location);
2208 else
2209 {
2210 tree invok = build_method_invocation ($3, $5);
2211 $$ = make_qualified_primary ($1, invok, $2.location);
2212 }
e04a16fb
AG
2213 }
2214| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2215 {
2216 $$ = build_this_super_qualified_invocation
2217 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2218 }
2219| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2220 {
22eed1e6
APB
2221 $$ = build_this_super_qualified_invocation
2222 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2223 }
2224 /* Screws up thing. I let it here until I'm convinced it can
2225 be removed. FIXME
2226| primary DOT_TK error
2227 {yyerror ("'(' expected"); DRECOVER(bad);} */
2228| SUPER_TK DOT_TK error CP_TK
2229 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2230| SUPER_TK DOT_TK error DOT_TK
2231 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232;
2233
2234array_access:
2235 name OSB_TK expression CSB_TK
2236 { $$ = build_array_ref ($2.location, $1, $3); }
2237| primary_no_new_array OSB_TK expression CSB_TK
2238 { $$ = build_array_ref ($2.location, $1, $3); }
2239| name OSB_TK error
2240 {
2241 yyerror ("Missing term and ']' expected");
2242 DRECOVER(array_access);
2243 }
2244| name OSB_TK expression error
2245 {
2246 yyerror ("']' expected");
2247 DRECOVER(array_access);
2248 }
2249| primary_no_new_array OSB_TK error
2250 {
2251 yyerror ("Missing term and ']' expected");
2252 DRECOVER(array_access);
2253 }
2254| primary_no_new_array OSB_TK expression error
2255 {
2256 yyerror ("']' expected");
2257 DRECOVER(array_access);
2258 }
2259;
2260
2261postfix_expression:
2262 primary
2263| name
2264| post_increment_expression
2265| post_decrement_expression
2266;
2267
2268post_increment_expression:
2269 postfix_expression INCR_TK
2270 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2271;
2272
2273post_decrement_expression:
2274 postfix_expression DECR_TK
2275 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2276;
2277
2278unary_expression:
2279 pre_increment_expression
2280| pre_decrement_expression
2281| PLUS_TK unary_expression
2282 {$$ = build_unaryop ($1.token, $1.location, $2); }
2283| MINUS_TK unary_expression
2284 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285| unary_expression_not_plus_minus
2286| PLUS_TK error
2287 {yyerror ("Missing term"); RECOVER}
2288| MINUS_TK error
2289 {yyerror ("Missing term"); RECOVER}
2290;
2291
2292pre_increment_expression:
2293 INCR_TK unary_expression
2294 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2295| INCR_TK error
2296 {yyerror ("Missing term"); RECOVER}
2297;
2298
2299pre_decrement_expression:
2300 DECR_TK unary_expression
2301 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2302| DECR_TK error
2303 {yyerror ("Missing term"); RECOVER}
2304;
2305
2306unary_expression_not_plus_minus:
2307 postfix_expression
2308| NOT_TK unary_expression
2309 {$$ = build_unaryop ($1.token, $1.location, $2); }
2310| NEG_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312| cast_expression
2313| NOT_TK error
2314 {yyerror ("Missing term"); RECOVER}
2315| NEG_TK error
2316 {yyerror ("Missing term"); RECOVER}
2317;
2318
2319cast_expression: /* Error handling here is potentially weak */
2320 OP_TK primitive_type dims CP_TK unary_expression
2321 {
2322 tree type = $2;
c7303e41
APB
2323 int osb = pop_current_osb (ctxp);
2324 while (osb--)
e04a16fb
AG
2325 type = build_java_array_type (type, -1);
2326 $$ = build_cast ($1.location, type, $5);
2327 }
2328| OP_TK primitive_type CP_TK unary_expression
2329 { $$ = build_cast ($1.location, $2, $4); }
2330| OP_TK expression CP_TK unary_expression_not_plus_minus
2331 { $$ = build_cast ($1.location, $2, $4); }
2332| OP_TK name dims CP_TK unary_expression_not_plus_minus
2333 {
49f48c71 2334 const char *ptr;
c7303e41
APB
2335 int osb = pop_current_osb (ctxp);
2336 while (osb--)
e04a16fb
AG
2337 obstack_1grow (&temporary_obstack, '[');
2338 obstack_grow0 (&temporary_obstack,
2339 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2340 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2341 ptr = obstack_finish (&temporary_obstack);
2342 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2343 $$ = build_cast ($1.location, $2, $5);
2344 }
2345| OP_TK primitive_type OSB_TK error
2346 {yyerror ("']' expected, invalid type expression");}
2347| OP_TK error
2348 {
29f8b718 2349 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2350 RECOVER;
2351 }
2352| OP_TK primitive_type dims CP_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354| OP_TK primitive_type CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356| OP_TK name dims CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358;
2359
2360multiplicative_expression:
2361 unary_expression
2362| multiplicative_expression MULT_TK unary_expression
2363 {
2364 $$ = build_binop (BINOP_LOOKUP ($2.token),
2365 $2.location, $1, $3);
2366 }
2367| multiplicative_expression DIV_TK unary_expression
2368 {
2369 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2370 $1, $3);
2371 }
2372| multiplicative_expression REM_TK unary_expression
2373 {
2374 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2375 $1, $3);
2376 }
2377| multiplicative_expression MULT_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| multiplicative_expression DIV_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381| multiplicative_expression REM_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383;
2384
2385additive_expression:
2386 multiplicative_expression
2387| additive_expression PLUS_TK multiplicative_expression
2388 {
2389 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2390 $1, $3);
2391 }
2392| additive_expression MINUS_TK multiplicative_expression
2393 {
2394 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2395 $1, $3);
2396 }
2397| additive_expression PLUS_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399| additive_expression MINUS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401;
2402
2403shift_expression:
2404 additive_expression
2405| shift_expression LS_TK additive_expression
2406 {
2407 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2408 $1, $3);
2409 }
2410| shift_expression SRS_TK additive_expression
2411 {
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2414 }
2415| shift_expression ZRS_TK additive_expression
2416 {
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2419 }
2420| shift_expression LS_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422| shift_expression SRS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424| shift_expression ZRS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426;
2427
2428relational_expression:
2429 shift_expression
2430| relational_expression LT_TK shift_expression
2431 {
2432 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2433 $1, $3);
2434 }
2435| relational_expression GT_TK shift_expression
2436 {
2437 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2438 $1, $3);
2439 }
2440| relational_expression LTE_TK shift_expression
2441 {
2442 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2443 $1, $3);
2444 }
2445| relational_expression GTE_TK shift_expression
2446 {
2447 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2448 $1, $3);
2449 }
2450| relational_expression INSTANCEOF_TK reference_type
5e942c50 2451 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2452| relational_expression LT_TK error
2453 {yyerror ("Missing term"); RECOVER;}
2454| relational_expression GT_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456| relational_expression LTE_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression GTE_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460| relational_expression INSTANCEOF_TK error
2461 {yyerror ("Invalid reference type"); RECOVER;}
2462;
2463
2464equality_expression:
2465 relational_expression
2466| equality_expression EQ_TK relational_expression
2467 {
2468 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2469 $1, $3);
2470 }
2471| equality_expression NEQ_TK relational_expression
2472 {
2473 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2474 $1, $3);
2475 }
2476| equality_expression EQ_TK error
2477 {yyerror ("Missing term"); RECOVER;}
2478| equality_expression NEQ_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2480;
2481
2482and_expression:
2483 equality_expression
2484| and_expression AND_TK equality_expression
2485 {
2486 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 $1, $3);
2488 }
2489| and_expression AND_TK error
2490 {yyerror ("Missing term"); RECOVER;}
2491;
2492
2493exclusive_or_expression:
2494 and_expression
2495| exclusive_or_expression XOR_TK and_expression
2496 {
2497 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2498 $1, $3);
2499 }
2500| exclusive_or_expression XOR_TK error
2501 {yyerror ("Missing term"); RECOVER;}
2502;
2503
2504inclusive_or_expression:
2505 exclusive_or_expression
2506| inclusive_or_expression OR_TK exclusive_or_expression
2507 {
2508 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2509 $1, $3);
2510 }
2511| inclusive_or_expression OR_TK error
2512 {yyerror ("Missing term"); RECOVER;}
2513;
2514
2515conditional_and_expression:
2516 inclusive_or_expression
2517| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2518 {
2519 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2520 $1, $3);
2521 }
2522| conditional_and_expression BOOL_AND_TK error
2523 {yyerror ("Missing term"); RECOVER;}
2524;
2525
2526conditional_or_expression:
2527 conditional_and_expression
2528| conditional_or_expression BOOL_OR_TK conditional_and_expression
2529 {
2530 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531 $1, $3);
2532 }
2533| conditional_or_expression BOOL_OR_TK error
2534 {yyerror ("Missing term"); RECOVER;}
2535;
2536
2537conditional_expression: /* Error handling here is weak */
2538 conditional_or_expression
2539| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2540 {
2541 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2542 EXPR_WFL_LINECOL ($$) = $2.location;
2543 }
e04a16fb
AG
2544| conditional_or_expression REL_QM_TK REL_CL_TK error
2545 {
2546 YYERROR_NOW;
2547 yyerror ("Missing term");
2548 DRECOVER (1);
2549 }
2550| conditional_or_expression REL_QM_TK error
2551 {yyerror ("Missing term"); DRECOVER (2);}
2552| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2553 {yyerror ("Missing term"); DRECOVER (3);}
2554;
2555
2556assignment_expression:
2557 conditional_expression
2558| assignment
2559;
2560
2561assignment:
2562 left_hand_side assignment_operator assignment_expression
2563 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2564| left_hand_side assignment_operator error
2565 {
29f8b718 2566 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2567 DRECOVER (assign);
2568 }
2569;
2570
2571left_hand_side:
2572 name
2573| field_access
2574| array_access
2575;
2576
2577assignment_operator:
2578 ASSIGN_ANY_TK
2579| ASSIGN_TK
2580;
2581
2582expression:
2583 assignment_expression
2584;
2585
2586constant_expression:
2587 expression
2588;
2589
2590%%
c7303e41
APB
2591
2592/* Helper function to retrieve an OSB count. Should be used when the
2593 `dims:' rule is being used. */
2594
2595static int
2596pop_current_osb (ctxp)
2597 struct parser_ctxt *ctxp;
2598{
2599 int to_return;
2600
2601 if (ctxp->osb_depth < 0)
2602 fatal ("osb stack underflow");
2603
2604 to_return = CURRENT_OSB (ctxp);
2605 ctxp->osb_depth--;
2606
2607 return to_return;
2608}
2609
e04a16fb
AG
2610\f
2611
c2952b01
APB
2612/* This section of the code deal with save/restoring parser contexts.
2613 Add mode documentation here. FIXME */
e04a16fb 2614
c2952b01
APB
2615/* Helper function. Create a new parser context. With
2616 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2617 context is copied, otherwise, the new context is zeroed. The newly
2618 created context becomes the current one. */
e04a16fb 2619
c2952b01
APB
2620static void
2621create_new_parser_context (copy_from_previous)
2622 int copy_from_previous;
e04a16fb 2623{
c2952b01 2624 struct parser_ctxt *new;
e04a16fb 2625
c2952b01
APB
2626 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2627 if (copy_from_previous)
2628 {
2629 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2630 new->saved_data_ctx = 1;
2631 }
2632 else
2e09e75a 2633 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2634
e04a16fb
AG
2635 new->next = ctxp;
2636 ctxp = new;
c2952b01
APB
2637}
2638
2639/* Create a new parser context and make it the current one. */
2640
2641void
2642java_push_parser_context ()
2643{
2644 create_new_parser_context (0);
e04a16fb 2645 if (ctxp->next)
5e942c50
APB
2646 {
2647 ctxp->incomplete_class = ctxp->next->incomplete_class;
2648 ctxp->gclass_list = ctxp->next->gclass_list;
2649 }
e04a16fb
AG
2650}
2651
c2952b01
APB
2652void
2653java_pop_parser_context (generate)
2654 int generate;
2655{
2656 tree current;
2657 struct parser_ctxt *toFree, *next;
2658
2659 if (!ctxp)
2660 return;
2661
2662 toFree = ctxp;
2663 next = ctxp->next;
2664 if (next)
2665 {
2666 next->incomplete_class = ctxp->incomplete_class;
2667 next->gclass_list = ctxp->gclass_list;
2668 lineno = ctxp->lineno;
19e223db 2669 current_class = ctxp->class_type;
c2952b01
APB
2670 }
2671
d19cbcb5
TT
2672 /* If the old and new lexers differ, then free the old one. */
2673 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2674 java_destroy_lexer (ctxp->lexer);
2675
c2952b01
APB
2676 /* Set the single import class file flag to 0 for the current list
2677 of imported things */
2678 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2679 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2680
2681 /* And restore those of the previous context */
2682 if ((ctxp = next)) /* Assignment is really meant here */
2683 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2684 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2685
2686 /* If we pushed a context to parse a class intended to be generated,
2687 we keep it so we can remember the class. What we could actually
2688 do is to just update a list of class names. */
2689 if (generate)
2690 {
2691 toFree->next = ctxp_for_generation;
2692 ctxp_for_generation = toFree;
2693 }
2694 else
2695 free (toFree);
2696}
2697
2698/* Create a parser context for the use of saving some global
2699 variables. */
2700
e04a16fb
AG
2701void
2702java_parser_context_save_global ()
2703{
22eed1e6
APB
2704 if (!ctxp)
2705 {
2706 java_push_parser_context ();
ee07f4f4
APB
2707 ctxp->saved_data_ctx = 1;
2708 }
c2952b01
APB
2709
2710 /* If this context already stores data, create a new one suitable
2711 for data storage. */
ee07f4f4 2712 else if (ctxp->saved_data)
c2952b01
APB
2713 create_new_parser_context (1);
2714
e04a16fb 2715 ctxp->lineno = lineno;
19e223db 2716 ctxp->class_type = current_class;
e04a16fb 2717 ctxp->filename = input_filename;
19e223db 2718 ctxp->function_decl = current_function_decl;
ee07f4f4 2719 ctxp->saved_data = 1;
e04a16fb
AG
2720}
2721
c2952b01
APB
2722/* Restore some global variables from the previous context. Make the
2723 previous context the current one. */
2724
e04a16fb
AG
2725void
2726java_parser_context_restore_global ()
2727{
e04a16fb 2728 lineno = ctxp->lineno;
19e223db 2729 current_class = ctxp->class_type;
e04a16fb 2730 input_filename = ctxp->filename;
dba41d30
APB
2731 if (wfl_operator)
2732 {
2733 tree s;
2734 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2735 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2736 }
19e223db 2737 current_function_decl = ctxp->function_decl;
c2952b01 2738 ctxp->saved_data = 0;
ee07f4f4
APB
2739 if (ctxp->saved_data_ctx)
2740 java_pop_parser_context (0);
e04a16fb
AG
2741}
2742
c2952b01
APB
2743/* Suspend vital data for the current class/function being parsed so
2744 that an other class can be parsed. Used to let local/anonymous
2745 classes be parsed. */
2746
2747static void
2748java_parser_context_suspend ()
e04a16fb 2749{
c2952b01 2750 /* This makes debugging through java_debug_context easier */
3b304f5b 2751 static const char *name = "<inner buffer context>";
e04a16fb 2752
c2952b01
APB
2753 /* Duplicate the previous context, use it to save the globals we're
2754 interested in */
2755 create_new_parser_context (1);
19e223db
MM
2756 ctxp->function_decl = current_function_decl;
2757 ctxp->class_type = current_class;
5e942c50 2758
c2952b01
APB
2759 /* Then create a new context which inherits all data from the
2760 previous one. This will be the new current context */
2761 create_new_parser_context (1);
2762
2763 /* Help debugging */
2764 ctxp->next->filename = name;
2765}
2766
2767/* Resume vital data for the current class/function being parsed so
2768 that an other class can be parsed. Used to let local/anonymous
2769 classes be parsed. The trick is the data storing file position
2770 informations must be restored to their current value, so parsing
2771 can resume as if no context was ever saved. */
2772
2773static void
2774java_parser_context_resume ()
2775{
2776 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2777 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2778 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2779
2780 /* We need to inherit the list of classes to complete/generate */
2781 restored->incomplete_class = old->incomplete_class;
2782 restored->gclass_list = old->gclass_list;
2783 restored->classd_list = old->classd_list;
2784 restored->class_list = old->class_list;
2785
2786 /* Restore the current class and function from the saver */
19e223db
MM
2787 current_class = saver->class_type;
2788 current_function_decl = saver->function_decl;
c2952b01
APB
2789
2790 /* Retrive the restored context */
2791 ctxp = restored;
2792
2793 /* Re-installed the data for the parsing to carry on */
2794 bcopy (&old->marker_begining, &ctxp->marker_begining,
2795 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2796
2797 /* Buffer context can now be discarded */
2798 free (saver);
2799 free (old);
2800}
2801
2802/* Add a new anchor node to which all statement(s) initializing static
2803 and non static initialized upon declaration field(s) will be
2804 linked. */
2805
2806static void
2807java_parser_context_push_initialized_field ()
2808{
2809 tree node;
2810
2811 node = build_tree_list (NULL_TREE, NULL_TREE);
2812 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2813 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2814
2815 node = build_tree_list (NULL_TREE, NULL_TREE);
2816 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2817 CPC_INITIALIZER_LIST (ctxp) = node;
2818
2819 node = build_tree_list (NULL_TREE, NULL_TREE);
2820 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2821 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2822}
2823
2824/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2825 remember them so we can use it to create and populate the finit$
c2952b01
APB
2826 or <clinit> functions. */
2827
2828static void
2829java_parser_context_pop_initialized_field ()
2830{
2831 tree stmts;
2832 tree class_type = TREE_TYPE (GET_CPC ());
2833
2834 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2835 {
c2952b01
APB
2836 stmts = CPC_INITIALIZER_STMT (ctxp);
2837 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2838 if (stmts && !java_error_count)
2839 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2840 }
2841
c2952b01
APB
2842 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2843 {
2844 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2845 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2846 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2847 /* Keep initialization in order to enforce 8.5 */
2848 if (stmts && !java_error_count)
2849 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2850 }
e04a16fb 2851
c2952b01
APB
2852 /* JDK 1.1 instance initializers */
2853 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2854 {
c2952b01
APB
2855 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2856 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2857 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2858 if (stmts && !java_error_count)
2859 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2860 }
c2952b01
APB
2861}
2862
2863static tree
2864reorder_static_initialized (list)
2865 tree list;
2866{
2867 /* We have to keep things in order. The alias initializer have to
2868 come first, then the initialized regular field, in reverse to
2869 keep them in lexical order. */
2870 tree marker, previous = NULL_TREE;
2871 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2872 if (TREE_CODE (marker) == TREE_LIST
2873 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2874 break;
2875
2876 /* No static initialized, the list is fine as is */
2877 if (!previous)
2878 list = TREE_CHAIN (marker);
2879
2880 /* No marker? reverse the whole list */
2881 else if (!marker)
2882 list = nreverse (list);
2883
2884 /* Otherwise, reverse what's after the marker and the new reordered
2885 sublist will replace the marker. */
b351b287 2886 else
c2952b01
APB
2887 {
2888 TREE_CHAIN (previous) = NULL_TREE;
2889 list = nreverse (list);
2890 list = chainon (TREE_CHAIN (marker), list);
2891 }
2892 return list;
e04a16fb
AG
2893}
2894
c2952b01
APB
2895/* Helper functions to dump the parser context stack. */
2896
2897#define TAB_CONTEXT(C) \
2898 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2899
2900static void
2901java_debug_context_do (tab)
2902 int tab;
2903{
ee07f4f4
APB
2904 struct parser_ctxt *copy = ctxp;
2905 while (copy)
2906 {
c2952b01 2907 TAB_CONTEXT (tab);
ee07f4f4 2908 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2909 TAB_CONTEXT (tab);
ee07f4f4 2910 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2911 TAB_CONTEXT (tab);
2912 fprintf (stderr, "lineno: %d\n", copy->lineno);
2913 TAB_CONTEXT (tab);
ee07f4f4
APB
2914 fprintf (stderr, "package: %s\n",
2915 (copy->package ?
2916 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2917 TAB_CONTEXT (tab);
ee07f4f4 2918 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2919 TAB_CONTEXT (tab);
ee07f4f4
APB
2920 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2921 copy = copy->next;
2922 tab += 2;
2923 }
ee07f4f4
APB
2924}
2925
c2952b01
APB
2926/* Dump the stacked up parser contexts. Intended to be called from a
2927 debugger. */
2928
ee07f4f4
APB
2929void
2930java_debug_context ()
2931{
2932 java_debug_context_do (0);
2933}
2934
c2952b01
APB
2935\f
2936
2937/* Flag for the error report routine to issue the error the first time
2938 it's called (overriding the default behavior which is to drop the
2939 first invocation and honor the second one, taking advantage of a
2940 richer context. */
2941static int force_error = 0;
ee07f4f4 2942
8119c720
APB
2943/* Reporting an constructor invocation error. */
2944static void
2945parse_ctor_invocation_error ()
2946{
2947 if (DECL_CONSTRUCTOR_P (current_function_decl))
2948 yyerror ("Constructor invocation must be first thing in a constructor");
2949 else
2950 yyerror ("Only constructors can invoke constructors");
2951}
2952
2953/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2954
2955static tree
2956parse_jdk1_1_error (msg)
49f48c71 2957 const char *msg;
b67d701b
PB
2958{
2959 sorry (": `%s' JDK1.1(TM) feature", msg);
2960 java_error_count++;
9bbc7d9f 2961 return empty_stmt_node;
b67d701b
PB
2962}
2963
e04a16fb
AG
2964static int do_warning = 0;
2965
2966void
2967yyerror (msg)
49f48c71 2968 const char *msg;
e04a16fb
AG
2969{
2970 static java_lc elc;
2971 static int prev_lineno;
49f48c71 2972 static const char *prev_msg;
e04a16fb 2973
0a2138e2 2974 int save_lineno;
e04a16fb
AG
2975 char *remainder, *code_from_source;
2976 extern struct obstack temporary_obstack;
2977
2978 if (!force_error && prev_lineno == lineno)
2979 return;
2980
2981 /* Save current error location but report latter, when the context is
2982 richer. */
2983 if (ctxp->java_error_flag == 0)
2984 {
2985 ctxp->java_error_flag = 1;
2986 elc = ctxp->elc;
2987 /* Do something to use the previous line if we're reaching the
2988 end of the file... */
2989#ifdef VERBOSE_SKELETON
2990 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2991#endif
2992 return;
2993 }
2994
2995 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2996 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2997 return;
2998
2999 ctxp->java_error_flag = 0;
3000 if (do_warning)
3001 java_warning_count++;
3002 else
3003 java_error_count++;
3004
807bc1db 3005 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
3006 {
3007 elc.col = ctxp->p_line->char_col-1;
3008 elc.line = ctxp->p_line->lineno;
3009 }
3010
3011 save_lineno = lineno;
3012 prev_lineno = lineno = elc.line;
3013 prev_msg = msg;
3014
3015 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3016 obstack_grow0 (&temporary_obstack,
3017 code_from_source, strlen (code_from_source));
3018 remainder = obstack_finish (&temporary_obstack);
3019 if (do_warning)
3020 warning ("%s.\n%s", msg, remainder);
3021 else
3022 error ("%s.\n%s", msg, remainder);
3023
3024 /* This allow us to cheaply avoid an extra 'Invalid expression
3025 statement' error report when errors have been already reported on
3026 the same line. This occurs when we report an error but don't have
3027 a synchronization point other than ';', which
3028 expression_statement is the only one to take care of. */
3029 ctxp->prevent_ese = lineno = save_lineno;
3030}
3031
3032static void
15fdcfe9 3033issue_warning_error_from_context (cl, msg, ap)
5e942c50 3034 tree cl;
d4476be2 3035 const char *msg;
15fdcfe9 3036 va_list ap;
5e942c50 3037{
3b304f5b 3038 const char *saved, *saved_input_filename;
15fdcfe9
PB
3039 char buffer [4096];
3040 vsprintf (buffer, msg, ap);
3041 force_error = 1;
5e942c50
APB
3042
3043 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3044 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3045 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3046
3047 /* We have a CL, that's a good reason for using it if it contains data */
3048 saved = ctxp->filename;
3049 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3050 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3051 saved_input_filename = input_filename;
3052 input_filename = ctxp->filename;
15fdcfe9
PB
3053 java_error (NULL);
3054 java_error (buffer);
5e942c50 3055 ctxp->filename = saved;
1886c9d8 3056 input_filename = saved_input_filename;
15fdcfe9 3057 force_error = 0;
5e942c50
APB
3058}
3059
e04a16fb
AG
3060/* Issue an error message at a current source line CL */
3061
15fdcfe9 3062void
df32d2ce 3063parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3064{
d4476be2 3065#ifndef ANSI_PROTOTYPES
e04a16fb 3066 tree cl;
d4476be2 3067 const char *msg;
e04a16fb 3068#endif
e04a16fb
AG
3069 va_list ap;
3070
3071 VA_START (ap, msg);
d4476be2 3072#ifndef ANSI_PROTOTYPES
e04a16fb 3073 cl = va_arg (ap, tree);
d4476be2 3074 msg = va_arg (ap, const char *);
e04a16fb 3075#endif
15fdcfe9
PB
3076 issue_warning_error_from_context (cl, msg, ap);
3077 va_end (ap);
e04a16fb
AG
3078}
3079
3080/* Issue a warning at a current source line CL */
3081
3082static void
df32d2ce 3083parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3084{
d4476be2 3085#ifndef ANSI_PROTOTYPES
e04a16fb 3086 tree cl;
d4476be2 3087 const char *msg;
e04a16fb 3088#endif
e04a16fb
AG
3089 va_list ap;
3090
3091 VA_START (ap, msg);
d4476be2 3092#ifndef ANSI_PROTOTYPES
e04a16fb 3093 cl = va_arg (ap, tree);
d4476be2 3094 msg = va_arg (ap, const char *);
e04a16fb 3095#endif
e04a16fb 3096
c877974e 3097 force_error = do_warning = 1;
15fdcfe9 3098 issue_warning_error_from_context (cl, msg, ap);
c877974e 3099 do_warning = force_error = 0;
15fdcfe9 3100 va_end (ap);
e04a16fb
AG
3101}
3102
82371d41
APB
3103static tree
3104find_expr_with_wfl (node)
3105 tree node;
3106{
3107 while (node)
3108 {
3109 char code;
3110 tree to_return;
3111
3112 switch (TREE_CODE (node))
3113 {
3114 case BLOCK:
c0d87ff6
PB
3115 node = BLOCK_EXPR_BODY (node);
3116 continue;
82371d41
APB
3117
3118 case COMPOUND_EXPR:
3119 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3120 if (to_return)
3121 return to_return;
c0d87ff6
PB
3122 node = TREE_OPERAND (node, 1);
3123 continue;
82371d41
APB
3124
3125 case LOOP_EXPR:
c0d87ff6
PB
3126 node = TREE_OPERAND (node, 0);
3127 continue;
82371d41
APB
3128
3129 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3130 node = TREE_OPERAND (node, 1);
3131 continue;
3132
82371d41
APB
3133 default:
3134 code = TREE_CODE_CLASS (TREE_CODE (node));
3135 if (((code == '1') || (code == '2') || (code == 'e'))
3136 && EXPR_WFL_LINECOL (node))
3137 return node;
ba179f9f 3138 return NULL_TREE;
82371d41
APB
3139 }
3140 }
3141 return NULL_TREE;
3142}
3143
3144/* Issue a missing return statement error. Uses METHOD to figure the
3145 last line of the method the error occurs in. */
3146
3147static void
3148missing_return_error (method)
3149 tree method;
3150{
3151 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3152 parse_error_context (wfl_operator, "Missing return statement");
3153}
3154
3155/* Issue an unreachable statement error. From NODE, find the next
3156 statement to report appropriately. */
3157static void
3158unreachable_stmt_error (node)
3159 tree node;
3160{
3161 /* Browse node to find the next expression node that has a WFL. Use
3162 the location to report the error */
3163 if (TREE_CODE (node) == COMPOUND_EXPR)
3164 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3165 else
3166 node = find_expr_with_wfl (node);
3167
3168 if (node)
3169 {
3170 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3171 parse_error_context (wfl_operator, "Unreachable statement");
3172 }
3173 else
3174 fatal ("Can't get valid statement - unreachable_stmt_error");
3175}
3176
c877974e 3177int
e04a16fb
AG
3178java_report_errors ()
3179{
3180 if (java_error_count)
3181 fprintf (stderr, "%d error%s",
3182 java_error_count, (java_error_count == 1 ? "" : "s"));
3183 if (java_warning_count)
3184 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3185 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3186 if (java_error_count || java_warning_count)
3187 putc ('\n', stderr);
c877974e 3188 return java_error_count;
e04a16fb
AG
3189}
3190
3191static char *
3192java_accstring_lookup (flags)
3193 int flags;
3194{
3195 static char buffer [80];
3196#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3197
3198 /* Access modifier looked-up first for easier report on forbidden
3199 access. */
3200 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3201 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3202 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3203 if (flags & ACC_STATIC) COPY_RETURN ("static");
3204 if (flags & ACC_FINAL) COPY_RETURN ("final");
3205 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3206 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3207 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3208 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3209 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3210 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3211
3212 buffer [0] = '\0';
3213 return buffer;
3214#undef COPY_RETURN
3215}
3216
b67d701b
PB
3217/* Issuing error messages upon redefinition of classes, interfaces or
3218 variables. */
3219
e04a16fb 3220static void
b67d701b 3221classitf_redefinition_error (context, id, decl, cl)
49f48c71 3222 const char *context;
e04a16fb
AG
3223 tree id, decl, cl;
3224{
3225 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3226 context, IDENTIFIER_POINTER (id),
3227 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3228 /* Here we should point out where its redefined. It's a unicode. FIXME */
3229}
3230
b67d701b
PB
3231static void
3232variable_redefinition_error (context, name, type, line)
3233 tree context, name, type;
3234 int line;
3235{
49f48c71 3236 const char *type_name;
b67d701b
PB
3237
3238 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3239 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3240 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3241 else
0a2138e2 3242 type_name = lang_printable_name (type, 0);
b67d701b
PB
3243
3244 parse_error_context (context,
781b0558 3245 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3246 IDENTIFIER_POINTER (name),
3247 type_name, IDENTIFIER_POINTER (name), line);
3248}
3249
c583dd46
APB
3250static tree
3251build_array_from_name (type, type_wfl, name, ret_name)
3252 tree type, type_wfl, name, *ret_name;
3253{
3254 int more_dims = 0;
49f48c71 3255 const char *string;
c583dd46
APB
3256
3257 /* Eventually get more dims */
3258 string = IDENTIFIER_POINTER (name);
3259 while (string [more_dims] == '[')
3260 more_dims++;
3261
3262 /* If we have, then craft a new type for this variable */
3263 if (more_dims)
3264 {
c0d87ff6 3265 name = get_identifier (&string [more_dims]);
c583dd46 3266
34f4db93
APB
3267 /* If we have a pointer, use its type */
3268 if (TREE_CODE (type) == POINTER_TYPE)
3269 type = TREE_TYPE (type);
c583dd46
APB
3270
3271 /* Building the first dimension of a primitive type uses this
3272 function */
3273 if (JPRIMITIVE_TYPE_P (type))
3274 {
3275 type = build_java_array_type (type, -1);
22eed1e6 3276 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3277 more_dims--;
3278 }
3279 /* Otherwise, if we have a WFL for this type, use it (the type
3280 is already an array on an unresolved type, and we just keep
3281 on adding dimensions) */
3282 else if (type_wfl)
3283 type = type_wfl;
3284
3285 /* Add all the dimensions */
3286 while (more_dims--)
3287 type = build_unresolved_array_type (type);
3288
3289 /* The type may have been incomplete in the first place */
3290 if (type_wfl)
3291 type = obtain_incomplete_type (type);
3292 }
3293
c2952b01
APB
3294 if (ret_name)
3295 *ret_name = name;
c583dd46
APB
3296 return type;
3297}
3298
e04a16fb
AG
3299/* Build something that the type identifier resolver will identify as
3300 being an array to an unresolved type. TYPE_WFL is a WFL on a
3301 identifier. */
3302
3303static tree
3304build_unresolved_array_type (type_or_wfl)
3305 tree type_or_wfl;
3306{
49f48c71 3307 const char *ptr;
e04a16fb 3308
1886c9d8 3309 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3310 just create a array type */
3311 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3312 {
3313 tree type = build_java_array_type (type_or_wfl, -1);
3314 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3315 return type;
3316 }
3317
3318 obstack_1grow (&temporary_obstack, '[');
3319 obstack_grow0 (&temporary_obstack,
3320 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3321 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3322 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3323 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3324 return type_or_wfl;
e04a16fb
AG
3325}
3326
e04a16fb
AG
3327static void
3328parser_add_interface (class_decl, interface_decl, wfl)
3329 tree class_decl, interface_decl, wfl;
3330{
3331 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3332 parse_error_context (wfl, "Interface `%s' repeated",
3333 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3334}
3335
3336/* Bulk of common class/interface checks. Return 1 if an error was
3337 encountered. TAG is 0 for a class, 1 for an interface. */
3338
3339static int
3340check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3341 int is_interface, flags;
3342 tree raw_name, qualified_name, decl, cl;
3343{
3344 tree node;
c2952b01
APB
3345 int sca = 0; /* Static class allowed */
3346 int icaf = 0; /* Inner class allowed flags */
3347 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3348
3349 if (!quiet_flag)
c2952b01
APB
3350 fprintf (stderr, " %s%s %s",
3351 (CPC_INNER_P () ? "inner" : ""),
3352 (is_interface ? "interface" : "class"),
e04a16fb
AG
3353 IDENTIFIER_POINTER (qualified_name));
3354
3355 /* Scope of an interface/class type name:
3356 - Can't be imported by a single type import
3357 - Can't already exists in the package */
3358 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3359 && (node = find_name_in_single_imports (raw_name))
3360 && !CPC_INNER_P ())
e04a16fb
AG
3361 {
3362 parse_error_context
3363 (cl, "%s name `%s' clashes with imported type `%s'",
3364 (is_interface ? "Interface" : "Class"),
3365 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3366 return 1;
3367 }
3368 if (decl && CLASS_COMPLETE_P (decl))
3369 {
b67d701b
PB
3370 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3371 qualified_name, decl, cl);
e04a16fb
AG
3372 return 1;
3373 }
3374
c2952b01
APB
3375 if (check_inner_class_redefinition (raw_name, cl))
3376 return 1;
3377
3378 /* If public, file name should match class/interface name, except
3379 when dealing with an inner class */
3380 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3381 {
49f48c71 3382 const char *f;
e04a16fb
AG
3383
3384 /* Contains OS dependent assumption on path separator. FIXME */
3385 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3386 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3387 f--)
3388 ;
847fe791 3389 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3390 f++;
3391 if (strncmp (IDENTIFIER_POINTER (raw_name),
3392 f , IDENTIFIER_LENGTH (raw_name)) ||
3393 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3394 parse_error_context
3395 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3396 (is_interface ? "interface" : "class"),
3397 IDENTIFIER_POINTER (qualified_name),
3398 IDENTIFIER_POINTER (raw_name));
3399 }
3400
c2952b01
APB
3401 /* Static classes can be declared only in top level classes. Note:
3402 once static, a inner class is a top level class. */
3403 if (flags & ACC_STATIC)
3404 {
3405 /* Catch the specific error of declaring an class inner class
3406 with no toplevel enclosing class. Prevent check_modifiers from
3407 complaining a second time */
3408 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3409 {
3410 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3411 IDENTIFIER_POINTER (qualified_name));
3412 sca = ACC_STATIC;
3413 }
3414 /* Else, in the context of a top-level class declaration, let
3415 `check_modifiers' do its job, otherwise, give it a go */
3416 else
3417 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3418 }
3419
a40d21da 3420 /* Inner classes can be declared private or protected
c2952b01
APB
3421 within their enclosing classes. */
3422 if (CPC_INNER_P ())
3423 {
3424 /* A class which is local to a block can't be public, private,
3425 protected or static. But it is created final, so allow this
3426 one. */
3427 if (current_function_decl)
3428 icaf = sca = uaaf = ACC_FINAL;
3429 else
3430 {
3431 check_modifiers_consistency (flags);
3432 icaf = ACC_PRIVATE|ACC_PROTECTED;
3433 }
3434 }
3435
a40d21da
APB
3436 if (is_interface)
3437 {
3438 if (CPC_INNER_P ())
3439 uaaf = INTERFACE_INNER_MODIFIERS;
3440 else
3441 uaaf = INTERFACE_MODIFIERS;
3442
3443 check_modifiers ("Illegal modifier `%s' for interface declaration",
3444 flags, uaaf);
3445 }
2884c41e 3446 else
a40d21da
APB
3447 check_modifiers ((current_function_decl ?
3448 "Illegal modifier `%s' for local class declaration" :
3449 "Illegal modifier `%s' for class declaration"),
c2952b01 3450 flags, uaaf|sca|icaf);
e04a16fb
AG
3451 return 0;
3452}
3453
c2952b01
APB
3454static void
3455make_nested_class_name (cpc_list)
3456 tree cpc_list;
3457{
3458 tree name;
3459
3460 if (!cpc_list)
3461 return;
3462 else
3463 make_nested_class_name (TREE_CHAIN (cpc_list));
3464
3465 /* Pick the qualified name when dealing with the first upmost
3466 enclosing class */
3467 name = (TREE_CHAIN (cpc_list) ?
3468 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3469 obstack_grow (&temporary_obstack,
3470 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3471 /* Why is NO_DOLLAR_IN_LABEL defined? */
3472#if 0
3473#ifdef NO_DOLLAR_IN_LABEL
3474 fatal ("make_nested_class_name: Can't use '$' as a separator "
3475 "for inner classes");
3476#endif
3477#endif
3478 obstack_1grow (&temporary_obstack, '$');
3479}
3480
3481/* Can't redefine a class already defined in an earlier scope. */
3482
3483static int
3484check_inner_class_redefinition (raw_name, cl)
3485 tree raw_name, cl;
3486{
3487 tree scope_list;
3488
3489 for (scope_list = GET_CPC_LIST (); scope_list;
3490 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3491 if (raw_name == GET_CPC_UN_NODE (scope_list))
3492 {
3493 parse_error_context
3494 (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3495 IDENTIFIER_POINTER (raw_name));
3496 return 1;
3497 }
3498 return 0;
3499}
3500
3501static tree
3502find_as_inner_class (enclosing, name, cl)
3503 tree enclosing, name, cl;
3504{
3505 tree qual, to_return;
3506 if (!enclosing)
3507 return NULL_TREE;
3508
3509 name = TYPE_NAME (name);
3510
3511 /* First search: within the scope of `enclosing', search for name */
3512 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3513 qual = EXPR_WFL_QUALIFICATION (cl);
3514 else if (cl)
3515 qual = build_tree_list (cl, NULL_TREE);
3516 else
3517 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3518
3519 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3520 return to_return;
3521
3522 /* We're dealing with a qualified name. Try to resolve thing until
3523 we get something that is an enclosing class. */
3524 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3525 {
3526 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3527
0c2b8145
APB
3528 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3529 qual = TREE_CHAIN (qual))
c2952b01
APB
3530 {
3531 acc = merge_qualified_name (acc,
3532 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3533 BUILD_PTR_FROM_NAME (ptr, acc);
3534 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3535 }
3536
3537 /* A NULL qual and a decl means that the search ended
3538 successfully?!? We have to do something then. FIXME */
3539
3540 if (decl)
3541 enclosing = decl;
3542 else
3543 qual = EXPR_WFL_QUALIFICATION (cl);
3544 }
3545 /* Otherwise, create a qual for the other part of the resolution. */
3546 else
3547 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3548
1e12ab9b 3549 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3550}
3551
3552/* We go inside the list of sub classes and try to find a way
3553 through. */
3554
3555static tree
3556find_as_inner_class_do (qual, enclosing)
3557 tree qual, enclosing;
3558{
3559 if (!qual)
3560 return NULL_TREE;
3561
3562 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3563 {
3564 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3565 tree next_enclosing = NULL_TREE;
3566 tree inner_list;
3567
3568 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3569 inner_list; inner_list = TREE_CHAIN (inner_list))
3570 {
3571 if (TREE_VALUE (inner_list) == name_to_match)
3572 {
3573 next_enclosing = TREE_PURPOSE (inner_list);
3574 break;
3575 }
3576 }
3577 enclosing = next_enclosing;
3578 }
3579
3580 return (!qual && enclosing ? enclosing : NULL_TREE);
3581}
3582
3583/* Reach all inner classes and tie their unqualified name to a
3584 DECL. */
3585
3586static void
3587set_nested_class_simple_name_value (outer, set)
3588 tree outer;
3589 int set;
3590{
3591 tree l;
3592
3593 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3594 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3595 TREE_PURPOSE (l) : NULL_TREE);
3596}
3597
3598static void
3599link_nested_class_to_enclosing ()
3600{
3601 if (GET_ENCLOSING_CPC ())
3602 {
3603 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3604 DECL_INNER_CLASS_LIST (enclosing) =
3605 tree_cons (GET_CPC (), GET_CPC_UN (),
3606 DECL_INNER_CLASS_LIST (enclosing));
3607 enclosing = enclosing;
3608 }
3609}
3610
3611static tree
3612maybe_make_nested_class_name (name)
3613 tree name;
3614{
3615 tree id = NULL_TREE;
3616
3617 if (CPC_INNER_P ())
3618 {
3619 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3620 obstack_grow0 (&temporary_obstack,
3621 IDENTIFIER_POINTER (name),
3622 IDENTIFIER_LENGTH (name));
c2952b01
APB
3623 id = get_identifier (obstack_finish (&temporary_obstack));
3624 if (ctxp->package)
3625 QUALIFIED_P (id) = 1;
3626 }
3627 return id;
3628}
3629
3630/* If DECL is NULL, create and push a new DECL, record the current
3631 line CL and do other maintenance things. */
3632
e04a16fb 3633static tree
c2952b01
APB
3634maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3635 tree decl, raw_name, qualified_name, cl;
e04a16fb 3636{
5e942c50 3637 if (!decl)
e04a16fb 3638 decl = push_class (make_class (), qualified_name);
c2952b01 3639
e04a16fb
AG
3640 /* Take care of the file and line business */
3641 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3642 /* If we're emiting xrefs, store the line/col number information */
3643 if (flag_emit_xref)
3644 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3645 else
3646 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3647 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3648 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3649 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3650
c2952b01
APB
3651 PUSH_CPC (decl, raw_name);
3652 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3653
e04a16fb
AG
3654 /* Link the declaration to the already seen ones */
3655 TREE_CHAIN (decl) = ctxp->class_list;
3656 ctxp->class_list = decl;
5e942c50 3657
23a79c61 3658 /* Create a new nodes in the global lists */
5e942c50 3659 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3660 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3661
e04a16fb
AG
3662 /* Install a new dependency list element */
3663 create_jdep_list (ctxp);
3664
3665 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3666 IDENTIFIER_POINTER (qualified_name)));
3667 return decl;
3668}
3669
3670static void
3671add_superinterfaces (decl, interface_list)
3672 tree decl, interface_list;
3673{
3674 tree node;
3675 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3676 takes care of ensuring that:
3677 - This is an accessible interface type,
3678 - Circularity detection.
3679 parser_add_interface is then called. If present but not defined,
3680 the check operation is delayed until the super interface gets
3681 defined. */
3682 for (node = interface_list; node; node = TREE_CHAIN (node))
3683 {
15fdcfe9 3684 tree current = TREE_PURPOSE (node);
5e942c50
APB
3685 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3686 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3687 {
5e942c50
APB
3688 if (!parser_check_super_interface (idecl, decl, current))
3689 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3690 }
3691 else
3692 register_incomplete_type (JDEP_INTERFACE,
3693 current, decl, NULL_TREE);
3694 }
3695}
3696
3697/* Create an interface in pass1 and return its decl. Return the
3698 interface's decl in pass 2. */
3699
3700static tree
3701create_interface (flags, id, super)
3702 int flags;
3703 tree id, super;
3704{
e04a16fb 3705 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3706 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3707 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3708
3709 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3710
3711 /* Basic checks: scope, redefinition, modifiers */
3712 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3713 {
3714 PUSH_ERROR ();
3715 return NULL_TREE;
3716 }
3717
3718 /* Suspend the current parsing context if we're parsing an inner
3719 interface */
3720 if (CPC_INNER_P ())
3721 java_parser_context_suspend ();
3722
3723 /* Push a new context for (static) initialized upon declaration fields */
3724 java_parser_context_push_initialized_field ();
e04a16fb
AG
3725
3726 /* Interface modifiers check
3727 - public/abstract allowed (already done at that point)
3728 - abstract is obsolete (comes first, it's a warning, or should be)
3729 - Can't use twice the same (checked in the modifier rule) */
c877974e 3730 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3731 parse_warning_context
3732 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3733 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3734
3735 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3736 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3737
3738 /* Set super info and mark the class a complete */
2aa11e97 3739 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3740 object_type_node, ctxp->interface_number);
3741 ctxp->interface_number = 0;
3742 CLASS_COMPLETE_P (decl) = 1;
3743 add_superinterfaces (decl, super);
3744
3745 return decl;
3746}
3747
c2952b01
APB
3748/* Anonymous class counter. Will be reset to 1 every time a non
3749 anonymous class gets created. */
3750static int anonymous_class_counter = 1;
3751
3752/* Patch anonymous class CLASS, by either extending or implementing
3753 DEP. */
3754
3755static void
3756patch_anonymous_class (type_decl, class_decl, wfl)
3757 tree type_decl, class_decl, wfl;
3758{
3759 tree class = TREE_TYPE (class_decl);
3760 tree type = TREE_TYPE (type_decl);
3761 tree binfo = TYPE_BINFO (class);
3762
3763 /* If it's an interface, implement it */
3764 if (CLASS_INTERFACE (type_decl))
3765 {
3766 tree s_binfo;
3767 int length;
3768
3769 if (parser_check_super_interface (type_decl, class_decl, wfl))
3770 return;
3771
3772 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3773 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3774 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3775 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3776 /* And add the interface */
3777 parser_add_interface (class_decl, type_decl, wfl);
3778 }
3779 /* Otherwise, it's a type we want to extend */
3780 else
3781 {
3782 if (parser_check_super (type_decl, class_decl, wfl))
3783 return;
3784 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3785 }
3786}
3787
3788static tree
3789create_anonymous_class (location, type_name)
3790 int location;
3791 tree type_name;
3792{
3793 char buffer [80];
3794 tree super = NULL_TREE, itf = NULL_TREE;
3795 tree id, type_decl, class;
3796
3797 /* The unqualified name of the anonymous class. It's just a number. */
3798 sprintf (buffer, "%d", anonymous_class_counter++);
3799 id = build_wfl_node (get_identifier (buffer));
3800 EXPR_WFL_LINECOL (id) = location;
3801
3802 /* We know about the type to extend/implement. We go ahead */
3803 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3804 {
3805 /* Create a class which either implements on extends the designated
3806 class. The class bears an innacessible name. */
3807 if (CLASS_INTERFACE (type_decl))
3808 {
3809 /* It's OK to modify it here. It's been already used and
3810 shouldn't be reused */
3811 ctxp->interface_number = 1;
3812 /* Interfaces should presented as a list of WFLs */
3813 itf = build_tree_list (type_name, NULL_TREE);
3814 }
3815 else
3816 super = type_name;
3817 }
3818
3819 class = create_class (ACC_FINAL, id, super, itf);
3820
3821 /* We didn't know anything about the stuff. We register a dependence. */
3822 if (!type_decl)
3823 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3824
3825 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3826 return class;
3827}
3828
a40d21da 3829/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3830 interface's decl in pass 2. */
3831
3832static tree
3833create_class (flags, id, super, interfaces)
3834 int flags;
3835 tree id, super, interfaces;
3836{
e04a16fb
AG
3837 tree raw_name = EXPR_WFL_NODE (id);
3838 tree class_id, decl;
9ee9b555 3839 tree super_decl_type;
e04a16fb 3840
98a52c2c 3841 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3842 decl = IDENTIFIER_CLASS_VALUE (class_id);
3843 EXPR_WFL_NODE (id) = class_id;
3844
3845 /* Basic check: scope, redefinition, modifiers */
3846 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3847 {
3848 PUSH_ERROR ();
3849 return NULL_TREE;
3850 }
3851
3852 /* Suspend the current parsing context if we're parsing an inner
3853 class or an anonymous class. */
3854 if (CPC_INNER_P ())
3855 java_parser_context_suspend ();
3856 /* Push a new context for (static) initialized upon declaration fields */
3857 java_parser_context_push_initialized_field ();
e04a16fb
AG
3858
3859 /* Class modifier check:
3860 - Allowed modifier (already done at that point)
3861 - abstract AND final forbidden
3862 - Public classes defined in the correct file */
3863 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3864 parse_error_context
3865 (id, "Class `%s' can't be declared both abstract and final",
3866 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3867
3868 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3869 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3870
3871 /* If SUPER exists, use it, otherwise use Object */
3872 if (super)
3873 {
3874 /* Can't extend java.lang.Object */
3875 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3876 {
3877 parse_error_context (id, "Can't extend `java.lang.Object'");
3878 return NULL_TREE;
3879 }
3880
2c3199bc
PB
3881 super_decl_type =
3882 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3883 }
3884 else if (TREE_TYPE (decl) != object_type_node)
3885 super_decl_type = object_type_node;
3886 /* We're defining java.lang.Object */
3887 else
3888 super_decl_type = NULL_TREE;
3889
6d003d5c
BM
3890 /* A class nested in an interface is implicitly static. */
3891 if (INNER_CLASS_DECL_P (decl)
3892 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3893 {
3894 flags |= ACC_STATIC;
3895 }
3896
3897 /* Set super info and mark the class as complete. */
e04a16fb
AG
3898 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3899 ctxp->interface_number);
3900 ctxp->interface_number = 0;
3901 CLASS_COMPLETE_P (decl) = 1;
3902 add_superinterfaces (decl, interfaces);
3903
c2952b01
APB
3904 /* Add the private this$<n> field, Replicate final locals still in
3905 scope as private final fields mangled like val$<local_name>.
3906 This doesn't not occur for top level (static) inner classes. */
3907 if (PURE_INNER_CLASS_DECL_P (decl))
3908 add_inner_class_fields (decl, current_function_decl);
3909
7f10c2e2
APB
3910 /* If doing xref, store the location at which the inherited class
3911 (if any) was seen. */
3912 if (flag_emit_xref && super)
3913 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3914
5e942c50
APB
3915 /* Eventually sets the @deprecated tag flag */
3916 CHECK_DEPRECATED (decl);
3917
165f37bc
APB
3918 /* Reset the anonymous class counter when declaring non inner classes */
3919 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3920 anonymous_class_counter = 1;
3921
e04a16fb
AG
3922 return decl;
3923}
3924
c2952b01 3925/* End a class declaration: register the statements used to create
c00f0fb2 3926 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3927 parser context if necessary. */
3928
3929static void
3930end_class_declaration (resume)
3931 int resume;
3932{
3933 /* If an error occured, context weren't pushed and won't need to be
3934 popped by a resume. */
3935 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3936
3937 java_parser_context_pop_initialized_field ();
3938 POP_CPC ();
3939 if (resume && no_error_occured)
3940 java_parser_context_resume ();
93220702
APB
3941
3942 /* We're ending a class declaration, this is a good time to reset
3943 the interface cout. Note that might have been already done in
3944 create_interface, but if at that time an inner class was being
3945 dealt with, the interface count was reset in a context created
3946 for the sake of handling inner classes declaration. */
3947 ctxp->interface_number = 0;
c2952b01
APB
3948}
3949
3950static void
3951add_inner_class_fields (class_decl, fct_decl)
3952 tree class_decl;
3953 tree fct_decl;
3954{
3955 tree block, marker, f;
3956
3957 f = add_field (TREE_TYPE (class_decl),
3958 build_current_thisn (TREE_TYPE (class_decl)),
3959 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3960 ACC_PRIVATE);
3961 FIELD_THISN (f) = 1;
3962
3963 if (!fct_decl)
3964 return;
3965
3966 for (block = GET_CURRENT_BLOCK (fct_decl);
3967 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3968 {
3969 tree decl;
3970 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3971 {
1f8f4a0b 3972 tree name, pname;
c2952b01
APB
3973 tree wfl, init, list;
3974
3975 /* Avoid non final arguments. */
c7303e41 3976 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3977 continue;
3978
3979 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3980 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3981 wfl = build_wfl_node (name);
3982 init = build_wfl_node (pname);
c2952b01 3983 /* Build an initialization for the field: it will be
c00f0fb2 3984 initialized by a parameter added to finit$, bearing a
c2952b01 3985 mangled name of the field itself (param$<n>.) The
c00f0fb2 3986 parameter is provided to finit$ by the constructor
c2952b01
APB
3987 invoking it (hence the constructor will also feature a
3988 hidden parameter, set to the value of the outer context
3989 local at the time the inner class is created.)
3990
3991 Note: we take into account all possible locals that can
3992 be accessed by the inner class. It's actually not trivial
3993 to minimize these aliases down to the ones really
3994 used. One way to do that would be to expand all regular
c00f0fb2 3995 methods first, then finit$ to get a picture of what's
c2952b01
APB
3996 used. It works with the exception that we would have to
3997 go back on all constructor invoked in regular methods to
3998 have their invokation reworked (to include the right amount
3999 of alias initializer parameters.)
4000
4001 The only real way around, I think, is a first pass to
4002 identify locals really used in the inner class. We leave
4003 the flag FIELD_LOCAL_ALIAS_USED around for that future
4004 use.
4005
4006 On the other hand, it only affect local inner classes,
c00f0fb2 4007 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4008 unecessary arguments. It's easy for a developper to keep
4009 this number of parameter down by using the `final'
4010 keyword only when necessary. For the time being, we can
4011 issue a warning on unecessary finals. FIXME */
4012 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4013 wfl, init);
4014
4015 /* Register the field. The TREE_LIST holding the part
4016 initialized/initializer will be marked ARG_FINAL_P so
4017 that the created field can be marked
4018 FIELD_LOCAL_ALIAS. */
4019 list = build_tree_list (wfl, init);
4020 ARG_FINAL_P (list) = 1;
4021 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4022 }
4023 }
4024
4025 if (!CPC_INITIALIZER_STMT (ctxp))
4026 return;
4027
4028 /* If we ever registered an alias field, insert and marker to
4029 remeber where the list ends. The second part of the list (the one
4030 featuring initialized fields) so it can be later reversed to
4031 enforce 8.5. The marker will be removed during that operation. */
4032 marker = build_tree_list (NULL_TREE, NULL_TREE);
4033 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4034 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4035}
4036
e04a16fb
AG
4037/* Can't use lookup_field () since we don't want to load the class and
4038 can't set the CLASS_LOADED_P flag */
4039
4040static tree
4041find_field (class, name)
4042 tree class;
4043 tree name;
4044{
4045 tree decl;
4046 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4047 {
4048 if (DECL_NAME (decl) == name)
4049 return decl;
4050 }
4051 return NULL_TREE;
4052}
4053
4054/* Wrap around lookup_field that doesn't potentially upset the value
4055 of CLASS */
4056
4057static tree
4058lookup_field_wrapper (class, name)
4059 tree class, name;
4060{
4061 tree type = class;
9a7ab4b3 4062 tree decl = NULL_TREE;
c877974e 4063 java_parser_context_save_global ();
f2760b27
APB
4064
4065 /* Last chance: if we're within the context of an inner class, we
4066 might be trying to access a local variable defined in an outer
4067 context. We try to look for it now. */
9a7ab4b3 4068 if (INNER_CLASS_TYPE_P (class))
f2760b27 4069 {
9a7ab4b3 4070 tree new_name;
1f8f4a0b 4071 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4072 decl = lookup_field (&type, new_name);
f2760b27
APB
4073 if (decl && decl != error_mark_node)
4074 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4075 }
9a7ab4b3
APB
4076 if (!decl || decl == error_mark_node)
4077 {
4078 type = class;
4079 decl = lookup_field (&type, name);
4080 }
f2760b27 4081
c877974e 4082 java_parser_context_restore_global ();
93024893 4083 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4084}
4085
4086/* Find duplicate field within the same class declarations and report
c583dd46
APB
4087 the error. Returns 1 if a duplicated field was found, 0
4088 otherwise. */
e04a16fb
AG
4089
4090static int
c583dd46 4091duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4092 tree new_field_name, new_type, cl;
e04a16fb
AG
4093{
4094 /* This might be modified to work with method decl as well */
c2952b01 4095 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4096 if (decl)
4097 {
c2e3db92 4098 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4099 ((TREE_CODE (new_type) == POINTER_TYPE
4100 && TREE_TYPE (new_type) == NULL_TREE) ?
4101 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4102 lang_printable_name (new_type, 1)));
c877974e
APB
4103 /* The type may not have been completed by the time we report
4104 the error */
c2e3db92 4105 char *t2 = xstrdup (purify_type_name
4a5f66c3 4106 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4107 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4108 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4109 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4110 parse_error_context
4111 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4112 t1, IDENTIFIER_POINTER (new_field_name),
4113 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4114 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4115 free (t1);
4116 free (t2);
c583dd46 4117 return 1;
e04a16fb 4118 }
c583dd46 4119 return 0;
e04a16fb
AG
4120}
4121
4122/* Field registration routine. If TYPE doesn't exist, field
4123 declarations are linked to the undefined TYPE dependency list, to
4124 be later resolved in java_complete_class () */
4125
4126static void
4127register_fields (flags, type, variable_list)
4128 int flags;
4129 tree type, variable_list;
4130{
c583dd46 4131 tree current, saved_type;
c2952b01 4132 tree class_type = NULL_TREE;
e04a16fb
AG
4133 int saved_lineno = lineno;
4134 int must_chain = 0;
4135 tree wfl = NULL_TREE;
4136
c2952b01
APB
4137 if (GET_CPC ())
4138 class_type = TREE_TYPE (GET_CPC ());
4139
4140 if (!class_type || class_type == error_mark_node)
4141 return;
4142
e04a16fb
AG
4143 /* If we're adding fields to interfaces, those fields are public,
4144 static, final */
4145 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4146 {
4147 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4148 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4149 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4150 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4151 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4152 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4153 check_modifiers ("Illegal interface member modifier `%s'", flags,
4154 INTERFACE_FIELD_MODIFIERS);
4155 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4156 }
4157
c583dd46
APB
4158 /* Obtain a suitable type for resolution, if necessary */
4159 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4160
4161 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4162 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4163
c583dd46
APB
4164 for (current = variable_list, saved_type = type; current;
4165 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4166 {
c877974e 4167 tree real_type;
c583dd46 4168 tree field_decl;
e04a16fb
AG
4169 tree cl = TREE_PURPOSE (current);
4170 tree init = TREE_VALUE (current);
4171 tree current_name = EXPR_WFL_NODE (cl);
4172
cf1748bf 4173 /* Can't declare non-final static fields in inner classes */
c2952b01 4174 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4175 && !(flags & ACC_FINAL))
c2952b01 4176 parse_error_context
cf1748bf 4177 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4178 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4179 lang_printable_name (class_type, 0));
4180
c583dd46
APB
4181 /* Process NAME, as it may specify extra dimension(s) for it */
4182 type = build_array_from_name (type, wfl, current_name, &current_name);
4183
c583dd46
APB
4184 /* Type adjustment. We may have just readjusted TYPE because
4185 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4186 a reference if we can and don't have one already. Also
4187 change the name if we have an init. */
4188 if (type != saved_type)
4189 {
1886c9d8 4190 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4191 if (init)
4192 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4193 }
e04a16fb 4194
c877974e
APB
4195 real_type = GET_REAL_TYPE (type);
4196 /* Check for redeclarations */
4197 if (duplicate_declaration_error_p (current_name, real_type, cl))
4198 continue;
4199
c583dd46 4200 /* Set lineno to the line the field was found and create a
5e942c50 4201 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4202 if (flag_emit_xref)
4203 lineno = EXPR_WFL_LINECOL (cl);
4204 else
4205 lineno = EXPR_WFL_LINENO (cl);
c877974e 4206 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4207 CHECK_DEPRECATED (field_decl);
c2952b01 4208
c7303e41
APB
4209 /* If the field denotes a final instance variable, then we
4210 allocate a LANG_DECL_SPECIFIC part to keep track of its
4211 initialization. We also mark whether the field was
4212 initialized upon it's declaration. We don't do that if the
4213 created field is an alias to a final local. */
4214 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4215 {
4216 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4217 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4218 if ((flags & ACC_STATIC) && init)
4219 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4220 }
4221
4222 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4223 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4224 hide parameters to this inner class finit$ and
4225 constructors. It also means that the field isn't final per
4226 say. */
c2952b01 4227 if (ARG_FINAL_P (current))
c7303e41
APB
4228 {
4229 FIELD_LOCAL_ALIAS (field_decl) = 1;
4230 FIELD_FINAL (field_decl) = 0;
4231 }
c583dd46
APB
4232
4233 /* Check if we must chain. */
4234 if (must_chain)
4235 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4236
c583dd46
APB
4237 /* If we have an initialization value tied to the field */
4238 if (init)
4239 {
4240 /* The field is declared static */
e04a16fb 4241 if (flags & ACC_STATIC)
e04a16fb 4242 {
7525cc04
APB
4243 /* We include the field and its initialization part into
4244 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4245 walked, field initializations will be processed and
4246 fields initialized with known constants will be taken
4247 out of <clinit> and have their DECL_INITIAL set
7525cc04 4248 appropriately. */
c2952b01
APB
4249 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4250 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4251 if (TREE_OPERAND (init, 1)
4252 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4253 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4254 }
5e942c50
APB
4255 /* A non-static field declared with an immediate initialization is
4256 to be initialized in <init>, if any. This field is remembered
4257 to be processed at the time of the generation of <init>. */
c583dd46
APB
4258 else
4259 {
c2952b01
APB
4260 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4261 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4262 }
5b09b33e 4263 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4264 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4265 }
4266 }
4267 lineno = saved_lineno;
4268}
4269
c00f0fb2
APB
4270/* Generate finit$, using the list of initialized fields to populate
4271 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4272 one(s) used to initialized the field(s) caching outer context
4273 local(s). */
22eed1e6 4274
c2952b01
APB
4275static tree
4276generate_finit (class_type)
4277 tree class_type;
22eed1e6 4278{
c2952b01
APB
4279 int count = 0;
4280 tree list = TYPE_FINIT_STMT_LIST (class_type);
4281 tree mdecl, current, parms;
4282
4283 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4284 class_type, NULL_TREE,
4285 &count);
4286 CRAFTED_PARAM_LIST_FIXUP (parms);
4287 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4288 finit_identifier_node, parms);
4289 fix_method_argument_names (parms, mdecl);
4290 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4291 mdecl, NULL_TREE);
4292 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4293 start_artificial_method_body (mdecl);
4294
c2952b01 4295 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4296 java_method_add_stmt (mdecl,
4297 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4298 current));
22eed1e6 4299 end_artificial_method_body (mdecl);
c2952b01 4300 return mdecl;
22eed1e6
APB
4301}
4302
e04a16fb 4303static void
c2952b01
APB
4304add_instance_initializer (mdecl)
4305 tree mdecl;
e04a16fb 4306{
c2952b01
APB
4307 tree current;
4308 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4309 tree compound = NULL_TREE;
e04a16fb 4310
c2952b01 4311 if (stmt_list)
e04a16fb 4312 {
c2952b01
APB
4313 for (current = stmt_list; current; current = TREE_CHAIN (current))
4314 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4315
c2952b01
APB
4316 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4317 NULL_TREE, compound));
4318 }
e04a16fb
AG
4319}
4320
4321/* Shared accros method_declarator and method_header to remember the
4322 patch stage that was reached during the declaration of the method.
4323 A method DECL is built differently is there is no patch
4324 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4325 pending on the currently defined method. */
4326
4327static int patch_stage;
4328
4329/* Check the method declaration and add the method to its current
4330 class. If the argument list is known to contain incomplete types,
4331 the method is partially added and the registration will be resume
22eed1e6
APB
4332 once the method arguments resolved. If TYPE is NULL, we're dealing
4333 with a constructor. */
e04a16fb
AG
4334
4335static tree
4336method_header (flags, type, mdecl, throws)
4337 int flags;
4338 tree type, mdecl, throws;
4339{
1886c9d8 4340 tree type_wfl = NULL_TREE;
79d13333 4341 tree meth_name = NULL_TREE;
c2952b01 4342 tree current, orig_arg, this_class = NULL;
34d4df06 4343 tree id, meth;
e04a16fb 4344 int saved_lineno;
1886c9d8 4345 int constructor_ok = 0, must_chain;
c2952b01 4346 int count;
34d4df06
APB
4347
4348 if (mdecl == error_mark_node)
4349 return error_mark_node;
4350 meth = TREE_VALUE (mdecl);
4351 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4352
4353 check_modifiers_consistency (flags);
79d13333 4354
c2952b01
APB
4355 if (GET_CPC ())
4356 this_class = TREE_TYPE (GET_CPC ());
4357
4358 if (!this_class || this_class == error_mark_node)
79d13333 4359 return NULL_TREE;
e04a16fb
AG
4360
4361 /* There are some forbidden modifiers for an abstract method and its
4362 class must be abstract as well. */
22eed1e6 4363 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4364 {
4365 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4366 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4367 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4368 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4369 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4370 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4371 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4372 parse_error_context
781b0558 4373 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4374 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4375 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4376 }
c2952b01 4377
22eed1e6
APB
4378 /* Things to be checked when declaring a constructor */
4379 if (!type)
4380 {
4381 int ec = java_error_count;
4382 /* 8.6: Constructor declarations: we might be trying to define a
4383 method without specifying a return type. */
c2952b01 4384 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4385 parse_error_context
4386 (id, "Invalid method declaration, return type required");
4387 /* 8.6.3: Constructor modifiers */
4388 else
4389 {
4390 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4391 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4392 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4393 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4394 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4395 }
4396 /* If we found error here, we don't consider it's OK to tread
4397 the method definition as a constructor, for the rest of this
4398 function */
4399 if (ec == java_error_count)
4400 constructor_ok = 1;
4401 }
e04a16fb
AG
4402
4403 /* Method declared within the scope of an interface are implicitly
4404 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4405 modifiers are checked right after. */
e04a16fb
AG
4406
4407 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4408 {
4409 /* If FLAGS isn't set because of a modifier, turn the
4410 corresponding modifier WFL to NULL so we issue a warning on
4411 the obsolete use of the modifier */
4412 if (!(flags & ACC_PUBLIC))
4413 MODIFIER_WFL (PUBLIC_TK) = NULL;
4414 if (!(flags & ACC_ABSTRACT))
4415 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4416 flags |= ACC_PUBLIC;
4417 flags |= ACC_ABSTRACT;
4418 }
4419
c2952b01
APB
4420 /* Inner class can't declare static methods */
4421 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4422 {
4423 parse_error_context
4424 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4425 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4426 lang_printable_name (this_class, 0));
4427 }
4428
e04a16fb
AG
4429 /* Modifiers context reset moved up, so abstract method declaration
4430 modifiers can be later checked. */
4431
22eed1e6
APB
4432 /* Set constructor returned type to void and method name to <init>,
4433 unless we found an error identifier the constructor (in which
4434 case we retain the original name) */
4435 if (!type)
4436 {
4437 type = void_type_node;
4438 if (constructor_ok)
4439 meth_name = init_identifier_node;
4440 }
4441 else
4442 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4443
1886c9d8
APB
4444 /* Do the returned type resolution and registration if necessary */
4445 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4446
4a5f66c3
APB
4447 if (meth_name)
4448 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4449 EXPR_WFL_NODE (id) = meth_name;
4450 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4451
4452 if (must_chain)
e04a16fb 4453 {
1886c9d8
APB
4454 patch_stage = JDEP_METHOD_RETURN;
4455 register_incomplete_type (patch_stage, type_wfl, id, type);
4456 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4457 }
4458 else
1886c9d8 4459 TREE_TYPE (meth) = type;
e04a16fb
AG
4460
4461 saved_lineno = lineno;
4462 /* When defining an abstract or interface method, the curly
4463 bracket at level 1 doesn't exist because there is no function
4464 body */
4465 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4466 EXPR_WFL_LINENO (id));
4467
5e942c50
APB
4468 /* Remember the original argument list */
4469 orig_arg = TYPE_ARG_TYPES (meth);
4470
e04a16fb
AG
4471 if (patch_stage) /* includes ret type and/or all args */
4472 {
4473 jdep *jdep;
4474 meth = add_method_1 (this_class, flags, meth_name, meth);
4475 /* Patch for the return type */
4476 if (patch_stage == JDEP_METHOD_RETURN)
4477 {
4478 jdep = CLASSD_LAST (ctxp->classd_list);
4479 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4480 }
4481 /* This is the stop JDEP. METH allows the function's signature
4482 to be computed. */
4483 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4484 }
4485 else
5e942c50
APB
4486 meth = add_method (this_class, flags, meth_name,
4487 build_java_signature (meth));
4488
c2952b01
APB
4489 /* Remember final parameters */
4490 MARK_FINAL_PARMS (meth, orig_arg);
4491
5e942c50
APB
4492 /* Fix the method argument list so we have the argument name
4493 information */
4494 fix_method_argument_names (orig_arg, meth);
4495
4496 /* Register the parameter number and re-install the current line
4497 number */
e04a16fb
AG
4498 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4499 lineno = saved_lineno;
b9f7e36c
APB
4500
4501 /* Register exception specified by the `throws' keyword for
4502 resolution and set the method decl appropriate field to the list.
4503 Note: the grammar ensures that what we get here are class
4504 types. */
4505 if (throws)
4506 {
4507 throws = nreverse (throws);
4508 for (current = throws; current; current = TREE_CHAIN (current))
4509 {
4510 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4511 NULL_TREE, NULL_TREE);
4512 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4513 &TREE_VALUE (current);
4514 }
4515 DECL_FUNCTION_THROWS (meth) = throws;
4516 }
4517
e04a16fb
AG
4518 /* We set the DECL_NAME to ID so we can track the location where
4519 the function was declared. This allow us to report
4520 redefinition error accurately. When method are verified,
4521 DECL_NAME is reinstalled properly (using the content of the
4522 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4523 when Object is being defined. Constructor <init> names will be
4524 reinstalled the same way. */
c2952b01 4525 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4526 DECL_NAME (meth) = id;
22eed1e6
APB
4527
4528 /* Set the flag if we correctly processed a constructor */
4529 if (constructor_ok)
c2952b01
APB
4530 {
4531 DECL_CONSTRUCTOR_P (meth) = 1;
4532 /* Compute and store the number of artificial parameters declared
4533 for this constructor */
4534 for (count = 0, current = TYPE_FIELDS (this_class); current;
4535 current = TREE_CHAIN (current))
4536 if (FIELD_LOCAL_ALIAS (current))
4537 count++;
4538 DECL_FUNCTION_NAP (meth) = count;
4539 }
22eed1e6 4540
5e942c50
APB
4541 /* Eventually set the @deprecated tag flag */
4542 CHECK_DEPRECATED (meth);
4543
7f10c2e2
APB
4544 /* If doing xref, store column and line number information instead
4545 of the line number only. */
4546 if (flag_emit_xref)
4547 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4548
e04a16fb
AG
4549 return meth;
4550}
4551
5e942c50
APB
4552static void
4553fix_method_argument_names (orig_arg, meth)
4554 tree orig_arg, meth;
4555{
4556 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4557 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4558 {
4559 TREE_PURPOSE (arg) = this_identifier_node;
4560 arg = TREE_CHAIN (arg);
4561 }
de4c7b02 4562 while (orig_arg != end_params_node)
5e942c50
APB
4563 {
4564 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4565 orig_arg = TREE_CHAIN (orig_arg);
4566 arg = TREE_CHAIN (arg);
4567 }
4568}
4569
22eed1e6
APB
4570/* Complete the method declaration with METHOD_BODY. */
4571
4572static void
b635eb2f 4573finish_method_declaration (method_body)
22eed1e6
APB
4574 tree method_body;
4575{
79d13333
APB
4576 int flags;
4577
4578 if (!current_function_decl)
4579 return;
4580
4581 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4582
4583 /* 8.4.5 Method Body */
4584 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4585 {
4586 tree wfl = DECL_NAME (current_function_decl);
4587 parse_error_context (wfl,
4588 "%s method `%s' can't have a body defined",
4589 (METHOD_NATIVE (current_function_decl) ?
4590 "Native" : "Abstract"),
4591 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4592 method_body = NULL_TREE;
4593 }
4594 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4595 {
4596 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4597 parse_error_context
4598 (wfl,
4599 "Non native and non abstract method `%s' must have a body defined",
4600 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4601 method_body = NULL_TREE;
4602 }
4603
2c56429a
APB
4604 if (flag_emit_class_files && method_body
4605 && TREE_CODE (method_body) == NOP_EXPR
4606 && TREE_TYPE (current_function_decl)
4607 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4608 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4609
22eed1e6
APB
4610 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4611 maybe_absorb_scoping_blocks ();
4612 /* Exit function's body */
4613 exit_block ();
4614 /* Merge last line of the function with first line, directly in the
4615 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4616 if (!flag_emit_xref)
4617 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4618
4619 /* Since function's argument's list are shared, reset the
4620 ARG_FINAL_P parameter that might have been set on some of this
4621 function parameters. */
4622 UNMARK_FINAL_PARMS (current_function_decl);
4623
f099f336
APB
4624 /* So we don't have an irrelevant function declaration context for
4625 the next static block we'll see. */
4626 current_function_decl = NULL_TREE;
22eed1e6
APB
4627}
4628
4629/* Build a an error message for constructor circularity errors. */
4630
4631static char *
4632constructor_circularity_msg (from, to)
4633 tree from, to;
4634{
4635 static char string [4096];
c2e3db92 4636 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4637 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4638 free (t);
4639 return string;
4640}
4641
4642/* Verify a circular call to METH. Return 1 if an error is found, 0
4643 otherwise. */
4644
4645static int
4646verify_constructor_circularity (meth, current)
4647 tree meth, current;
4648{
4649 static tree list = NULL_TREE;
19e223db 4650 static int initialized_p;
22eed1e6 4651 tree c;
19e223db
MM
4652
4653 /* If we haven't already registered LIST with the garbage collector,
4654 do so now. */
4655 if (!initialized_p)
4656 {
4657 ggc_add_tree_root (&list, 1);
4658 initialized_p = 1;
4659 }
4660
22eed1e6
APB
4661 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4662 {
4663 if (TREE_VALUE (c) == meth)
4664 {
4665 char *t;
4666 if (list)
4667 {
4668 tree liste;
4669 list = nreverse (list);
4670 for (liste = list; liste; liste = TREE_CHAIN (liste))
4671 {
4672 parse_error_context
c63b98cd 4673 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4674 constructor_circularity_msg
4675 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4676 java_error_count--;
4677 }
4678 }
c2e3db92 4679 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4680 parse_error_context (TREE_PURPOSE (c),
4681 "%s: recursive invocation of constructor `%s'",
4682 constructor_circularity_msg (current, meth), t);
4683 free (t);
4684 list = NULL_TREE;
4685 return 1;
4686 }
4687 }
4688 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4689 {
4690 list = tree_cons (c, current, list);
4691 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4692 return 1;
4693 list = TREE_CHAIN (list);
4694 }
4695 return 0;
4696}
4697
e04a16fb
AG
4698/* Check modifiers that can be declared but exclusively */
4699
4700static void
4701check_modifiers_consistency (flags)
4702 int flags;
4703{
4704 int acc_count = 0;
4705 tree cl = NULL_TREE;
4706
e0fc4118
TT
4707 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4708 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4709 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4710 if (acc_count > 1)
4711 parse_error_context
e0fc4118
TT
4712 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4713
4714 acc_count = 0;
4715 cl = NULL_TREE;
14d075d8
TT
4716 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4717 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4718 if (acc_count > 1)
4719 parse_error_context (cl,
4720 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4721}
4722
4723/* Check the methode header METH for abstract specifics features */
4724
4725static void
4726check_abstract_method_header (meth)
4727 tree meth;
4728{
4729 int flags = get_access_flags_from_decl (meth);
4730 /* DECL_NAME might still be a WFL node */
c877974e 4731 tree name = GET_METHOD_NAME (meth);
e04a16fb 4732
2884c41e
KG
4733 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4734 ACC_ABSTRACT, "abstract method",
4735 IDENTIFIER_POINTER (name));
4736 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4737 ACC_PUBLIC, "abstract method",
4738 IDENTIFIER_POINTER (name));
e04a16fb
AG
4739
4740 check_modifiers ("Illegal modifier `%s' for interface method",
4741 flags, INTERFACE_METHOD_MODIFIERS);
4742}
4743
4744/* Create a FUNCTION_TYPE node and start augmenting it with the
4745 declared function arguments. Arguments type that can't be resolved
4746 are left as they are, but the returned node is marked as containing
4747 incomplete types. */
4748
4749static tree
4750method_declarator (id, list)
4751 tree id, list;
4752{
4753 tree arg_types = NULL_TREE, current, node;
4754 tree meth = make_node (FUNCTION_TYPE);
4755 jdep *jdep;
e04a16fb
AG
4756
4757 patch_stage = JDEP_NO_PATCH;
c2952b01 4758
34d4df06
APB
4759 if (GET_CPC () == error_mark_node)
4760 return error_mark_node;
4761
c2952b01
APB
4762 /* If we're dealing with an inner class constructor, we hide the
4763 this$<n> decl in the name field of its parameter declaration. We
4764 also might have to hide the outer context local alias
4765 initializers. Not done when the class is a toplevel class. */
4766 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4767 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4768 {
4769 tree aliases_list, type, thisn;
4770 /* First the aliases, linked to the regular parameters */
4771 aliases_list =
4772 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4773 TREE_TYPE (GET_CPC ()),
4774 NULL_TREE, NULL);
4775 list = chainon (nreverse (aliases_list), list);
4776
4777 /* Then this$<n> */
4778 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4779 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4780 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4781 list);
4782 }
e04a16fb
AG
4783
4784 for (current = list; current; current = TREE_CHAIN (current))
4785 {
c583dd46 4786 int must_chain = 0;
e04a16fb
AG
4787 tree wfl_name = TREE_PURPOSE (current);
4788 tree type = TREE_VALUE (current);
4789 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4790 tree already, arg_node;
4791 tree type_wfl = NULL_TREE;
23a79c61 4792 tree real_type;
c583dd46
APB
4793
4794 /* Obtain a suitable type for resolution, if necessary */
4795 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4796
4797 /* Process NAME, as it may specify extra dimension(s) for it */
4798 type = build_array_from_name (type, type_wfl, name, &name);
4799 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4800
23a79c61
APB
4801 real_type = GET_REAL_TYPE (type);
4802 if (TREE_CODE (real_type) == RECORD_TYPE)
4803 {
4804 real_type = promote_type (real_type);
4805 if (TREE_CODE (type) == TREE_LIST)
4806 TREE_PURPOSE (type) = real_type;
4807 }
5e942c50 4808
e04a16fb
AG
4809 /* Check redefinition */
4810 for (already = arg_types; already; already = TREE_CHAIN (already))
4811 if (TREE_PURPOSE (already) == name)
4812 {
781b0558
KG
4813 parse_error_context
4814 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4815 IDENTIFIER_POINTER (name),
e04a16fb
AG
4816 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4817 break;
4818 }
4819
4820 /* If we've an incomplete argument type, we know there is a location
4821 to patch when the type get resolved, later. */
4822 jdep = NULL;
c583dd46 4823 if (must_chain)
e04a16fb 4824 {
c583dd46
APB
4825 patch_stage = JDEP_METHOD;
4826 type = register_incomplete_type (patch_stage,
4827 type_wfl, wfl_name, type);
4828 jdep = CLASSD_LAST (ctxp->classd_list);
4829 JDEP_MISC (jdep) = id;
e04a16fb 4830 }
c583dd46 4831
c2952b01 4832 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4833 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4834 /* Remeber arguments declared final. */
4835 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4836
e04a16fb
AG
4837 if (jdep)
4838 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4839 TREE_CHAIN (arg_node) = arg_types;
4840 arg_types = arg_node;
4841 }
de4c7b02 4842 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4843 node = build_tree_list (id, meth);
4844 return node;
4845}
4846
4847static int
4848unresolved_type_p (wfl, returned)
4849 tree wfl;
4850 tree *returned;
4851
4852{
4853 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4854 {
e04a16fb 4855 if (returned)
165f37bc
APB
4856 {
4857 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4858 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4859 *returned = TREE_TYPE (decl);
4860 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4861 *returned = TREE_TYPE (GET_CPC ());
4862 else
4863 *returned = NULL_TREE;
4864 }
e04a16fb
AG
4865 return 1;
4866 }
4867 if (returned)
4868 *returned = wfl;
4869 return 0;
4870}
4871
4872/* From NAME, build a qualified identifier node using the
4873 qualification from the current package definition. */
4874
4875static tree
98a52c2c 4876parser_qualified_classname (name)
e04a16fb
AG
4877 tree name;
4878{
c2952b01
APB
4879 tree nested_class_name;
4880
98a52c2c 4881 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4882 return nested_class_name;
4883
e04a16fb 4884 if (ctxp->package)
c2952b01 4885 return merge_qualified_name (ctxp->package, name);
e04a16fb 4886 else
c2952b01 4887 return name;
e04a16fb
AG
4888}
4889
4890/* Called once the type a interface extends is resolved. Returns 0 if
4891 everything is OK. */
4892
4893static int
4894parser_check_super_interface (super_decl, this_decl, this_wfl)
4895 tree super_decl, this_decl, this_wfl;
4896{
4897 tree super_type = TREE_TYPE (super_decl);
4898
4899 /* Has to be an interface */
c2952b01 4900 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4901 {
4902 parse_error_context
4903 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4904 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4905 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4906 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4907 "interface" : "class"),
4908 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4909 return 1;
4910 }
4911
4912 /* Check scope: same package OK, other package: OK if public */
4913 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4914 return 1;
4915
4916 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4917 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4918 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4919 return 0;
4920}
4921
4922/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4923 0 if everthing is OK. */
4924
4925static int
4926parser_check_super (super_decl, this_decl, wfl)
4927 tree super_decl, this_decl, wfl;
4928{
e04a16fb
AG
4929 tree super_type = TREE_TYPE (super_decl);
4930
4931 /* SUPER should be a CLASS (neither an array nor an interface) */
4932 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4933 {
4934 parse_error_context
4935 (wfl, "Class `%s' can't subclass %s `%s'",
4936 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4937 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4938 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4939 return 1;
4940 }
4941
4942 if (CLASS_FINAL (TYPE_NAME (super_type)))
4943 {
4944 parse_error_context (wfl, "Can't subclass final classes: %s",
4945 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4946 return 1;
4947 }
4948
4949 /* Check scope: same package OK, other package: OK if public */
4950 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4951 return 1;
4952
4953 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4954 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4955 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4956 return 0;
4957}
4958
4959/* Create a new dependency list and link it (in a LIFO manner) to the
4960 CTXP list of type dependency list. */
4961
4962static void
4963create_jdep_list (ctxp)
4964 struct parser_ctxt *ctxp;
4965{
23a79c61 4966 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4967 new->first = new->last = NULL;
4968 new->next = ctxp->classd_list;
4969 ctxp->classd_list = new;
4970}
4971
4972static jdeplist *
4973reverse_jdep_list (ctxp)
4974 struct parser_ctxt *ctxp;
4975{
4976 register jdeplist *prev = NULL, *current, *next;
4977 for (current = ctxp->classd_list; current; current = next)
4978 {
4979 next = current->next;
4980 current->next = prev;
4981 prev = current;
4982 }
4983 return prev;
4984}
4985
23a79c61
APB
4986/* Create a fake pointer based on the ID stored in
4987 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4988 registered again. */
e04a16fb
AG
4989
4990static tree
23a79c61
APB
4991obtain_incomplete_type (type_name)
4992 tree type_name;
e04a16fb 4993{
23a79c61
APB
4994 tree ptr, name;
4995
4996 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4997 name = EXPR_WFL_NODE (type_name);
4998 else if (INCOMPLETE_TYPE_P (type_name))
4999 name = TYPE_NAME (type_name);
5000 else
5001 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
5002
5003 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 5004 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
5005 break;
5006
5007 if (!ptr)
5008 {
78d21f92
PB
5009 BUILD_PTR_FROM_NAME (ptr, name);
5010 layout_type (ptr);
e04a16fb
AG
5011 TREE_CHAIN (ptr) = ctxp->incomplete_class;
5012 ctxp->incomplete_class = ptr;
5013 }
5014
5015 return ptr;
5016}
5017
5018/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5019 non NULL instead of computing a new fake type based on WFL. The new
5020 dependency is inserted in the current type dependency list, in FIFO
5021 manner. */
5022
5023static tree
5024register_incomplete_type (kind, wfl, decl, ptr)
5025 int kind;
5026 tree wfl, decl, ptr;
5027{
23a79c61 5028 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5029
e04a16fb
AG
5030 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5031 ptr = obtain_incomplete_type (wfl);
5032
5033 JDEP_KIND (new) = kind;
5034 JDEP_DECL (new) = decl;
5035 JDEP_SOLV (new) = ptr;
5036 JDEP_WFL (new) = wfl;
5037 JDEP_CHAIN (new) = NULL;
5038 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5039 /* For some dependencies, set the enclosing class of the current
5040 class to be the enclosing context */
8ac1de05
APB
5041 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5042 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5043 && GET_ENCLOSING_CPC ())
5044 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5045 else
324ed8fd 5046 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5047 JDEP_GET_PATCH (new) = (tree *)NULL;
5048
5049 JDEP_INSERT (ctxp->classd_list, new);
5050
5051 return ptr;
5052}
5053
5054void
5055java_check_circular_reference ()
5056{
5057 tree current;
5058 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5059 {
5060 tree type = TREE_TYPE (current);
e920ebc9 5061 if (CLASS_INTERFACE (current))
e04a16fb
AG
5062 {
5063 /* Check all interfaces this class extends */
5064 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5065 int n, i;
5066
5067 if (!basetype_vec)
5068 return;
5069 n = TREE_VEC_LENGTH (basetype_vec);
5070 for (i = 0; i < n; i++)
5071 {
5072 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5073 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5074 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5075 parse_error_context (lookup_cl (current),
5076 "Cyclic interface inheritance");
5077 }
5078 }
5079 else
5080 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5081 parse_error_context (lookup_cl (current),
c2952b01
APB
5082 "Cyclic class inheritance%s",
5083 (cyclic_inheritance_report ?
5084 cyclic_inheritance_report : ""));
5085 }
5086}
5087
5088/* Augment the parameter list PARM with parameters crafted to
5089 initialize outer context locals aliases. Through ARTIFICIAL, a
5090 count is kept of the number of crafted parameters. MODE governs
5091 what eventually gets created: something suitable for a function
5092 creation or a function invocation, either the constructor or
c00f0fb2 5093 finit$. */
c2952b01
APB
5094
5095static tree
5096build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5097 int mode;
5098 tree class_type, parm;
5099 int *artificial;
5100{
5101 tree field;
da632f2c
APB
5102 tree additional_parms = NULL_TREE;
5103
c2952b01
APB
5104 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5105 if (FIELD_LOCAL_ALIAS (field))
5106 {
63ad61ed 5107 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5108 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5109 tree mangled_id;
c2952b01
APB
5110
5111 switch (mode)
5112 {
5113 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5114 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5115 &buffer [4]);
5116 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5117 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5118 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5119 else
5120 value = TREE_TYPE (field);
5121 break;
5122
5123 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5124 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5125 &buffer [4]);
c2952b01
APB
5126 value = TREE_TYPE (field);
5127 break;
5128
5129 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5130 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5131 &buffer [4]);
c2952b01
APB
5132 /* Now, this is wrong. purpose should always be the NAME
5133 of something and value its matching value (decl, type,
5134 etc...) FIXME -- but there is a lot to fix. */
5135
5136 /* When invoked for this kind of operation, we already
5137 know whether a field is used or not. */
5138 purpose = TREE_TYPE (field);
1f8f4a0b 5139 value = build_wfl_node (mangled_id);
c2952b01
APB
5140 break;
5141
5142 case AIPL_FUNCTION_CTOR_INVOCATION:
5143 /* There are two case: the constructor invokation happends
5144 outside the local inner, in which case, locales from the outer
5145 context are directly used.
5146
5147 Otherwise, we fold to using the alias directly. */
5148 if (class_type == current_class)
5149 value = field;
5150 else
5151 {
5152 name = get_identifier (&buffer[4]);
5153 value = IDENTIFIER_LOCAL_VALUE (name);
5154 }
5155 break;
5156 }
da632f2c 5157 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5158 if (artificial)
5159 *artificial +=1;
5160 }
da632f2c
APB
5161 if (additional_parms)
5162 {
5163 if (ANONYMOUS_CLASS_P (class_type)
5164 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5165 additional_parms = nreverse (additional_parms);
5166 parm = chainon (additional_parms, parm);
5167 }
5168
5169 return parm;
c2952b01
APB
5170}
5171
5172/* Craft a constructor for CLASS_DECL -- what we should do when none
5173 where found. ARGS is non NULL when a special signature must be
5174 enforced. This is the case for anonymous classes. */
5175
5176static void
5177craft_constructor (class_decl, args)
5178 tree class_decl, args;
5179{
5180 tree class_type = TREE_TYPE (class_decl);
5181 tree parm = NULL_TREE;
5182 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5183 ACC_PUBLIC : 0);
5184 int i = 0, artificial = 0;
5185 tree decl, ctor_name;
5186 char buffer [80];
5187
c2952b01
APB
5188 /* The constructor name is <init> unless we're dealing with an
5189 anonymous class, in which case the name will be fixed after having
5190 be expanded. */
5191 if (ANONYMOUS_CLASS_P (class_type))
5192 ctor_name = DECL_NAME (class_decl);
5193 else
5194 ctor_name = init_identifier_node;
5195
5196 /* If we're dealing with an inner class constructor, we hide the
5197 this$<n> decl in the name field of its parameter declaration. */
5198 if (PURE_INNER_CLASS_TYPE_P (class_type))
5199 {
5200 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5201 parm = tree_cons (build_current_thisn (class_type),
5202 build_pointer_type (type), parm);
5203
5204 /* Some more arguments to be hidden here. The values of the local
5205 variables of the outer context that the inner class needs to see. */
5206 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5207 class_type, parm,
5208 &artificial);
5209 }
5210
5211 /* Then if there are any args to be enforced, enforce them now */
5212 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5213 {
5214 sprintf (buffer, "parm%d", i++);
5215 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5216 }
c2952b01
APB
5217
5218 CRAFTED_PARAM_LIST_FIXUP (parm);
5219 decl = create_artificial_method (class_type, flags, void_type_node,
5220 ctor_name, parm);
5221 fix_method_argument_names (parm, decl);
5222 /* Now, mark the artificial parameters. */
5223 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5224 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5225}
5226
c2952b01 5227
e920ebc9
APB
5228/* Fix the constructors. This will be called right after circular
5229 references have been checked. It is necessary to fix constructors
5230 early even if no code generation will take place for that class:
5231 some generated constructor might be required by the class whose
5232 compilation triggered this one to be simply loaded. */
5233
5234void
5235java_fix_constructors ()
5236{
5237 tree current;
5238
5239 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5240 {
e920ebc9
APB
5241 tree class_type = TREE_TYPE (current);
5242 int saw_ctor = 0;
c2952b01
APB
5243 tree decl;
5244
5245 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5246 continue;
e920ebc9
APB
5247
5248 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5249 {
5250 if (DECL_CONSTRUCTOR_P (decl))
5251 {
5252 fix_constructors (decl);
5253 saw_ctor = 1;
5254 }
5255 }
5256
c2952b01
APB
5257 /* Anonymous class constructor can't be generated that early. */
5258 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5259 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5260 }
5261}
5262
23a79c61
APB
5263/* safe_layout_class just makes sure that we can load a class without
5264 disrupting the current_class, input_file, lineno, etc, information
5265 about the class processed currently. */
5266
e04a16fb
AG
5267void
5268safe_layout_class (class)
5269 tree class;
5270{
5271 tree save_current_class = current_class;
3b304f5b 5272 const char *save_input_filename = input_filename;
e04a16fb 5273 int save_lineno = lineno;
5e942c50 5274
e04a16fb 5275 layout_class (class);
5e942c50 5276
e04a16fb
AG
5277 current_class = save_current_class;
5278 input_filename = save_input_filename;
5279 lineno = save_lineno;
5280 CLASS_LOADED_P (class) = 1;
5281}
5282
5283static tree
5284jdep_resolve_class (dep)
5285 jdep *dep;
5286{
5287 tree decl;
5288
23a79c61
APB
5289 if (JDEP_RESOLVED_P (dep))
5290 decl = JDEP_RESOLVED_DECL (dep);
5291 else
e04a16fb 5292 {
c2952b01 5293 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5294 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5295 JDEP_RESOLVED (dep, decl);
5296 }
23a79c61 5297
e04a16fb 5298 if (!decl)
23a79c61 5299 complete_class_report_errors (dep);
1e12ab9b 5300 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5301 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5302 return decl;
5303}
5304
5305/* Complete unsatisfied class declaration and their dependencies */
5306
5307void
5308java_complete_class ()
5309{
e04a16fb
AG
5310 tree cclass;
5311 jdeplist *cclassd;
5312 int error_found;
b67d701b 5313 tree type;
e04a16fb 5314
9a7ab4b3 5315 /* Process imports */
e04a16fb 5316 process_imports ();
e04a16fb
AG
5317
5318 /* Rever things so we have the right order */
5319 ctxp->class_list = nreverse (ctxp->class_list);
5320 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5321
e04a16fb
AG
5322 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5323 cclass && cclassd;
5324 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5325 {
5326 jdep *dep;
5327 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5328 {
5329 tree decl;
e04a16fb
AG
5330 if (!(decl = jdep_resolve_class (dep)))
5331 continue;
5332
5333 /* Now it's time to patch */
5334 switch (JDEP_KIND (dep))
5335 {
5336 case JDEP_SUPER:
5337 /* Simply patch super */
5338 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5339 continue;
5340 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5341 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5342 break;
5343
5344 case JDEP_FIELD:
5345 {
5346 /* We do part of the job done in add_field */
5347 tree field_decl = JDEP_DECL (dep);
5348 tree field_type = TREE_TYPE (decl);
e04a16fb 5349 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5350 field_type = promote_type (field_type);
e04a16fb 5351 TREE_TYPE (field_decl) = field_type;
5e942c50 5352 DECL_ALIGN (field_decl) = 0;
11cf4d18 5353 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5354 layout_decl (field_decl, 0);
e04a16fb
AG
5355 SOURCE_FRONTEND_DEBUG
5356 (("Completed field/var decl `%s' with `%s'",
5357 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5358 IDENTIFIER_POINTER (DECL_NAME (decl))));
5359 break;
5360 }
5361 case JDEP_METHOD: /* We start patching a method */
5362 case JDEP_METHOD_RETURN:
5363 error_found = 0;
5364 while (1)
5365 {
5366 if (decl)
5367 {
b67d701b
PB
5368 type = TREE_TYPE(decl);
5369 if (TREE_CODE (type) == RECORD_TYPE)
5370 type = promote_type (type);
e04a16fb
AG
5371 JDEP_APPLY_PATCH (dep, type);
5372 SOURCE_FRONTEND_DEBUG
5373 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5374 "Completing fct `%s' with ret type `%s'":
5375 "Completing arg `%s' with type `%s'"),
5376 IDENTIFIER_POINTER (EXPR_WFL_NODE
5377 (JDEP_DECL_WFL (dep))),
5378 IDENTIFIER_POINTER (DECL_NAME (decl))));
5379 }
5380 else
5381 error_found = 1;
5382 dep = JDEP_CHAIN (dep);
5383 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5384 break;
5385 else
5386 decl = jdep_resolve_class (dep);
5387 }
5388 if (!error_found)
5389 {
5390 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5391 /* Recompute and reset the signature, check first that
5392 all types are now defined. If they're not,
5393 dont build the signature. */
5394 if (check_method_types_complete (mdecl))
5395 {
5396 signature = build_java_signature (TREE_TYPE (mdecl));
5397 set_java_signature (TREE_TYPE (mdecl), signature);
5398 }
e04a16fb
AG
5399 }
5400 else
5401 continue;
5402 break;
5403
5404 case JDEP_INTERFACE:
5405 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5406 JDEP_WFL (dep)))
5407 continue;
5408 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5409 break;
5410
b67d701b 5411 case JDEP_PARM:
e04a16fb 5412 case JDEP_VARIABLE:
b67d701b
PB
5413 type = TREE_TYPE(decl);
5414 if (TREE_CODE (type) == RECORD_TYPE)
5415 type = promote_type (type);
5416 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5417 break;
5418
5419 case JDEP_TYPE:
5420 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5421 SOURCE_FRONTEND_DEBUG
5422 (("Completing a random type dependency on a '%s' node",
5423 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5424 break;
5425
b9f7e36c 5426 case JDEP_EXCEPTION:
c877974e
APB
5427 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5428 SOURCE_FRONTEND_DEBUG
5429 (("Completing `%s' `throws' argument node",
5430 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5431 break;
5432
c2952b01
APB
5433 case JDEP_ANONYMOUS:
5434 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5435 break;
5436
e04a16fb 5437 default:
0a2138e2
APB
5438 fatal ("Can't handle patch code %d - java_complete_class",
5439 JDEP_KIND (dep));
e04a16fb
AG
5440 }
5441 }
5442 }
e04a16fb
AG
5443 return;
5444}
5445
5446/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5447 array. */
5448
5449static tree
c2952b01
APB
5450resolve_class (enclosing, class_type, decl, cl)
5451 tree enclosing, class_type, decl, cl;
e04a16fb 5452{
49f48c71
KG
5453 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5454 const char *base = name;
78d21f92
PB
5455 tree resolved_type = TREE_TYPE (class_type);
5456 tree resolved_type_decl;
e04a16fb 5457
78d21f92
PB
5458 if (resolved_type != NULL_TREE)
5459 {
5460 tree resolved_type_decl = TYPE_NAME (resolved_type);
5461 if (resolved_type_decl == NULL_TREE
5462 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5463 {
5464 resolved_type_decl = build_decl (TYPE_DECL,
5465 TYPE_NAME (class_type),
5466 resolved_type);
5467 }
5468 return resolved_type_decl;
5469 }
5470
e04a16fb
AG
5471 /* 1- Check to see if we have an array. If true, find what we really
5472 want to resolve */
5473 while (name[0] == '[')
5474 name++;
5475 if (base != name)
34d4df06
APB
5476 {
5477 TYPE_NAME (class_type) = get_identifier (name);
5478 WFL_STRIP_BRACKET (cl, cl);
5479 }
e04a16fb
AG
5480
5481 /* 2- Resolve the bare type */
c2952b01
APB
5482 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5483 decl, cl)))
e04a16fb
AG
5484 return NULL_TREE;
5485 resolved_type = TREE_TYPE (resolved_type_decl);
5486
5487 /* 3- If we have and array, reconstruct the array down to its nesting */
5488 if (base != name)
5489 {
5490 while (base != name)
5491 {
5492 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5493 resolved_type = promote_type (resolved_type);
5494 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5495 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5496 name--;
5497 }
5498 /* Build a fake decl for this, since this is what is expected to
5499 be returned. */
5500 resolved_type_decl =
5501 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5502 /* Figure how those two things are important for error report. FIXME */
5503 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5504 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5505 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5506 }
78d21f92 5507 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5508 return resolved_type_decl;
5509}
5510
5511/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5512 are used to report error messages. */
5513
78d21f92 5514tree
c2952b01
APB
5515do_resolve_class (enclosing, class_type, decl, cl)
5516 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5517{
5518 tree new_class_decl;
e04a16fb
AG
5519
5520 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5521 it is changed by find_in_imports{_on_demand} and (but it doesn't
5522 really matter) qualify_and_find */
e04a16fb 5523
c2952b01
APB
5524 /* 0- Search in the current class as an inner class */
5525
5526 /* Maybe some code here should be added to load the class or
5527 something, at least if the class isn't an inner class and ended
5528 being loaded from class file. FIXME. */
a40d21da
APB
5529 while (enclosing)
5530 {
5531 tree name;
e1d565ab 5532 tree intermediate;
a40d21da
APB
5533
5534 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5535 return new_class_decl;
8ac1de05 5536
e1d565ab 5537 intermediate = enclosing;
0c2b8145 5538 /* Explore enclosing contexts. */
e1d565ab 5539 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5540 {
e1d565ab
BM
5541 intermediate = DECL_CONTEXT (intermediate);
5542 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5543 class_type, cl)))
5544 return new_class_decl;
5545 }
5546
a40d21da
APB
5547 /* Now go to the upper classes, bail out if necessary. */
5548 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5549 if (!enclosing || enclosing == object_type_node)
5550 break;
5551
5552 if (TREE_CODE (enclosing) == RECORD_TYPE)
5553 {
5554 enclosing = TYPE_NAME (enclosing);
5555 continue;
5556 }
5557
5558 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5559 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5560 else
5561 name = enclosing;
5562 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5563 }
c2952b01 5564
9a7ab4b3
APB
5565 /* 1- Check for the type in single imports. This will change
5566 TYPE_NAME() if something relevant is found */
5567 find_in_imports (class_type);
e04a16fb 5568
9a7ab4b3 5569 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5570 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5571 {
5572 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5573 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5574 load_class (TYPE_NAME (class_type), 0);
5575 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5576 }
5577
9a7ab4b3
APB
5578 /* 3- Search according to the current package definition */
5579 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5580 {
5581 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5582 TYPE_NAME (class_type))))
5583 return new_class_decl;
5584 }
5585
5586 /* 4- Check the import on demands. Don't allow bar.baz to be
5587 imported from foo.* */
5588 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5589 if (find_in_imports_on_demand (class_type))
5590 return NULL_TREE;
5591
5592 /* If found in find_in_imports_on_demant, the type has already been
5593 loaded. */
5594 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5595 return new_class_decl;
5596
5597 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5598 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5599 {
ee07f4f4 5600 tree package;
d6baf6f5
APB
5601
5602 /* If there is a current package (ctxp->package), it's the first
5603 element of package_list and we can skip it. */
5604 for (package = (ctxp->package ?
5605 TREE_CHAIN (package_list) : package_list);
5606 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5607 if ((new_class_decl = qualify_and_find (class_type,
5608 TREE_PURPOSE (package),
5609 TYPE_NAME (class_type))))
5610 return new_class_decl;
5611 }
5612
5613 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5614 load_class (TYPE_NAME (class_type), 0);
5615 if (check_pkg_class_access (TYPE_NAME (class_type),
5616 (cl ? cl : lookup_cl (decl))))
5617 return NULL_TREE;
5618
9a7ab4b3 5619 /* 6- Last call for a resolution */
e04a16fb
AG
5620 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5621}
5622
9a7ab4b3
APB
5623static tree
5624qualify_and_find (class_type, package, name)
5625 tree class_type, package, name;
5626{
5627 tree new_qualified = merge_qualified_name (package, name);
5628 tree new_class_decl;
5629
5630 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5631 load_class (new_qualified, 0);
5632 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5633 {
5634 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5635 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5636 load_class (new_qualified, 0);
5637 TYPE_NAME (class_type) = new_qualified;
5638 return IDENTIFIER_CLASS_VALUE (new_qualified);
5639 }
5640 return NULL_TREE;
5641}
5642
e04a16fb 5643/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5644 parsed class). Return a decl node. This function is meant to be
5645 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5646
5647static tree
c877974e
APB
5648resolve_and_layout (something, cl)
5649 tree something;
e04a16fb
AG
5650 tree cl;
5651{
34d4df06 5652 tree decl, decl_type;
c877974e 5653
23a79c61
APB
5654 /* Don't do that on the current class */
5655 if (something == current_class)
5656 return TYPE_NAME (current_class);
c877974e 5657
23a79c61 5658 /* Don't do anything for void and other primitive types */
c877974e
APB
5659 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5660 return NULL_TREE;
5661
23a79c61
APB
5662 /* Pointer types can be reall pointer types or fake pointers. When
5663 finding a real pointer, recheck for primitive types */
5664 if (TREE_CODE (something) == POINTER_TYPE)
5665 {
5666 if (TREE_TYPE (something))
5667 {
5668 something = TREE_TYPE (something);
5669 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5670 return NULL_TREE;
5671 }
5672 else
5673 something = TYPE_NAME (something);
5674 }
5675
5676 /* Don't do anything for arrays of primitive types */
5677 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5678 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5679 return NULL_TREE;
5680
c2952b01
APB
5681 /* Something might be a WFL */
5682 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5683 something = EXPR_WFL_NODE (something);
5684
5685 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5686 TYPE_DECL or a real TYPE */
5687 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5688 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5689 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5690
23a79c61
APB
5691 if (!(decl = resolve_no_layout (something, cl)))
5692 return NULL_TREE;
5693
5694 /* Resolve and layout if necessary */
34d4df06
APB
5695 decl_type = TREE_TYPE (decl);
5696 layout_class_methods (decl_type);
5697 /* Check methods */
5698 if (CLASS_FROM_SOURCE_P (decl_type))
5699 java_check_methods (decl);
5700 /* Layout the type if necessary */
5701 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5702 safe_layout_class (decl_type);
23a79c61 5703
e04a16fb
AG
5704 return decl;
5705}
5706
5707/* Resolve a class, returns its decl but doesn't perform any
5708 layout. The current parsing context is saved and restored */
5709
5710static tree
5711resolve_no_layout (name, cl)
5712 tree name, cl;
5713{
5714 tree ptr, decl;
5715 BUILD_PTR_FROM_NAME (ptr, name);
5716 java_parser_context_save_global ();
c2952b01 5717 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5718 java_parser_context_restore_global ();
5719
5720 return decl;
5721}
5722
23a79c61
APB
5723/* Called when reporting errors. Skip leader '[' in a complex array
5724 type description that failed to be resolved. */
e04a16fb 5725
49f48c71 5726static const char *
e04a16fb 5727purify_type_name (name)
49f48c71 5728 const char *name;
e04a16fb
AG
5729{
5730 while (*name && *name == '[')
5731 name++;
5732 return name;
5733}
5734
5735/* The type CURRENT refers to can't be found. We print error messages. */
5736
5737static void
5738complete_class_report_errors (dep)
5739 jdep *dep;
5740{
49f48c71 5741 const char *name;
23a79c61
APB
5742
5743 if (!JDEP_WFL (dep))
5744 return;
5745
5746 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5747 switch (JDEP_KIND (dep))
5748 {
5749 case JDEP_SUPER:
5750 parse_error_context
5751 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5752 purify_type_name (name),
e04a16fb
AG
5753 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5754 break;
5755 case JDEP_FIELD:
5756 parse_error_context
5757 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5758 purify_type_name (name),
e04a16fb
AG
5759 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5760 break;
5761 case JDEP_METHOD: /* Covers arguments */
5762 parse_error_context
781b0558 5763 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5764 purify_type_name (name),
e04a16fb
AG
5765 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5766 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5767 break;
5768 case JDEP_METHOD_RETURN: /* Covers return type */
5769 parse_error_context
781b0558 5770 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5771 purify_type_name (name),
e04a16fb
AG
5772 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5773 break;
5774 case JDEP_INTERFACE:
5775 parse_error_context
5776 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5777 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5778 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5779 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5780 break;
5781 case JDEP_VARIABLE:
5782 parse_error_context
781b0558 5783 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5784 purify_type_name (IDENTIFIER_POINTER
5785 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5786 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5787 break;
b9f7e36c
APB
5788 case JDEP_EXCEPTION: /* As specified by `throws' */
5789 parse_error_context
5790 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5791 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5792 break;
0a2138e2
APB
5793 default:
5794 /* Fix for -Wall. Just break doing nothing. The error will be
5795 caught later */
5796 break;
e04a16fb
AG
5797 }
5798}
5799
22eed1e6
APB
5800/* Return a static string containing the DECL prototype string. If
5801 DECL is a constructor, use the class name instead of the form
5802 <init> */
5803
49f48c71 5804static const char *
22eed1e6
APB
5805get_printable_method_name (decl)
5806 tree decl;
5807{
49f48c71 5808 const char *to_return;
9ee9b555 5809 tree name = NULL_TREE;
22eed1e6
APB
5810
5811 if (DECL_CONSTRUCTOR_P (decl))
5812 {
5813 name = DECL_NAME (decl);
5e942c50 5814 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5815 }
5816
5817 to_return = lang_printable_name (decl, 0);
5818 if (DECL_CONSTRUCTOR_P (decl))
5819 DECL_NAME (decl) = name;
5820
5821 return to_return;
5822}
5823
5e942c50
APB
5824/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5825 nevertheless needs to be verfied, 1 otherwise. */
5826
5827static int
5828reset_method_name (method)
5829 tree method;
5830{
c2952b01 5831 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5832 {
5833 /* NAME is just the plain name when Object is being defined */
5834 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5835 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5836 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5837 return 0;
5838 }
5839 else
5840 return 1;
5841}
5842
c877974e
APB
5843/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5844
5845tree
5846java_get_real_method_name (method_decl)
5847 tree method_decl;
5848{
5849 tree method_name = DECL_NAME (method_decl);
5850 if (DECL_CONSTRUCTOR_P (method_decl))
5851 return init_identifier_node;
82371d41
APB
5852
5853 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5854 and still can be a constructor. FIXME */
5855
23a79c61
APB
5856 /* Don't confuse method only bearing the name of their class as
5857 constructors */
82371d41
APB
5858 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5859 && ctxp
c2952b01 5860 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5861 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5862 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5863 return init_identifier_node;
5864 else
5865 return EXPR_WFL_NODE (method_name);
5866}
5867
22eed1e6
APB
5868/* Track method being redefined inside the same class. As a side
5869 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5870 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5871
e04a16fb
AG
5872static int
5873check_method_redefinition (class, method)
5874 tree class, method;
5875{
5876 tree redef, name;
5877 tree cl = DECL_NAME (method);
c3f2a476 5878 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5879 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5880 fixed and checked */
5e942c50
APB
5881
5882 /* Reset the method name before running the check. If it returns 1,
5883 the method doesn't need to be verified with respect to method
5884 redeclaration and we return 0 */
5885 if (reset_method_name (method))
e04a16fb 5886 return 0;
5e942c50
APB
5887
5888 name = DECL_NAME (method);
e04a16fb
AG
5889 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5890 {
c3f2a476 5891 if (redef == method)
e04a16fb 5892 break;
c3f2a476
APB
5893 if (DECL_NAME (redef) == name
5894 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5895 {
22eed1e6
APB
5896 parse_error_context
5897 (cl, "Duplicate %s declaration `%s'",
5898 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5899 get_printable_method_name (redef));
e04a16fb
AG
5900 return 1;
5901 }
5902 }
5903 return 0;
5904}
5905
1175b9b4
TT
5906/* Return 1 if check went ok, 0 otherwise. */
5907static int
d77613be
APB
5908check_abstract_method_definitions (do_interface, class_decl, type)
5909 int do_interface;
5910 tree class_decl, type;
5911{
5912 tree class = TREE_TYPE (class_decl);
5913 tree method, end_type;
1175b9b4 5914 int ok = 1;
d77613be
APB
5915
5916 end_type = (do_interface ? object_type_node : type);
5917 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5918 {
5919 tree other_super, other_method, method_sig, method_name;
5920 int found = 0;
165f37bc 5921 int end_type_reached = 0;
d77613be
APB
5922
5923 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5924 continue;
5925
5926 /* Now verify that somewhere in between TYPE and CLASS,
5927 abstract method METHOD gets a non abstract definition
5928 that is inherited by CLASS. */
5929
5930 method_sig = build_java_signature (TREE_TYPE (method));
5931 method_name = DECL_NAME (method);
5932 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5933 method_name = EXPR_WFL_NODE (method_name);
5934
165f37bc
APB
5935 other_super = class;
5936 do {
5937 if (other_super == end_type)
5938 end_type_reached = 1;
5939
5940 /* Method search */
5941 for (other_method = TYPE_METHODS (other_super); other_method;
5942 other_method = TREE_CHAIN (other_method))
5943 {
5944 tree s = build_java_signature (TREE_TYPE (other_method));
5945 tree other_name = DECL_NAME (other_method);
5946
5947 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5948 other_name = EXPR_WFL_NODE (other_name);
5949 if (!DECL_CLINIT_P (other_method)
5950 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5951 && method_name == other_name
5952 && method_sig == s
5953 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5954 {
5955 found = 1;
5956 break;
5957 }
5958 }
5959 other_super = CLASSTYPE_SUPER (other_super);
5960 } while (!end_type_reached);
5961
d77613be
APB
5962 /* Report that abstract METHOD didn't find an implementation
5963 that CLASS can use. */
5964 if (!found)
5965 {
c2e3db92 5966 char *t = xstrdup (lang_printable_name
d77613be
APB
5967 (TREE_TYPE (TREE_TYPE (method)), 0));
5968 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5969 tree saved_wfl = NULL_TREE;
5970
5971 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5972 {
5973 saved_wfl = DECL_NAME (method);
5974 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5975 }
5976
5977 parse_error_context
5978 (lookup_cl (class_decl),
781b0558 5979 "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract",
d77613be
APB
5980 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5981 t, lang_printable_name (method, 0),
5982 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5983 "interface" : "class"),
5984 IDENTIFIER_POINTER (ccn),
5985 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5986 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5987 ok = 0;
d77613be 5988 free (t);
1175b9b4 5989
d77613be
APB
5990 if (saved_wfl)
5991 DECL_NAME (method) = saved_wfl;
5992 }
5993 }
1175b9b4
TT
5994
5995 if (ok && do_interface)
5996 {
5997 /* Check for implemented interfaces. */
5998 int i;
5999 tree vector = TYPE_BINFO_BASETYPES (type);
6000 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6001 {
6002 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6003 ok = check_abstract_method_definitions (1, class_decl, super);
6004 }
6005 }
6006
6007 return ok;
d77613be
APB
6008}
6009
614eaae0 6010/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
6011 methods. */
6012
6013static void
6014java_check_abstract_method_definitions (class_decl)
6015 tree class_decl;
6016{
6017 tree class = TREE_TYPE (class_decl);
6018 tree super, vector;
6019 int i;
6020
6021 if (CLASS_ABSTRACT (class_decl))
6022 return;
6023
6024 /* Check for inherited types */
165f37bc
APB
6025 super = class;
6026 do {
6027 super = CLASSTYPE_SUPER (super);
6028 check_abstract_method_definitions (0, class_decl, super);
6029 } while (super != object_type_node);
d77613be
APB
6030
6031 /* Check for implemented interfaces. */
6032 vector = TYPE_BINFO_BASETYPES (class);
6033 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6034 {
6035 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6036 check_abstract_method_definitions (1, class_decl, super);
6037 }
6038}
6039
165f37bc
APB
6040/* Check all the types method DECL uses and return 1 if all of them
6041 are now complete, 0 otherwise. This is used to check whether its
6042 safe to build a method signature or not. */
6043
6044static int
6045check_method_types_complete (decl)
6046 tree decl;
6047{
6048 tree type = TREE_TYPE (decl);
6049 tree args;
6050
6051 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6052 return 0;
6053
6054 args = TYPE_ARG_TYPES (type);
6055 if (TREE_CODE (type) == METHOD_TYPE)
6056 args = TREE_CHAIN (args);
6057 for (; args != end_params_node; args = TREE_CHAIN (args))
6058 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6059 return 0;
6060
6061 return 1;
6062}
6063
34d4df06
APB
6064/* Visible interface to check methods contained in CLASS_DECL */
6065
6066void
6067java_check_methods (class_decl)
6068 tree class_decl;
6069{
6070 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6071 return;
6072
6073 if (CLASS_INTERFACE (class_decl))
6074 java_check_abstract_methods (class_decl);
6075 else
6076 java_check_regular_methods (class_decl);
6077
6078 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6079}
6080
d77613be
APB
6081/* Check all the methods of CLASS_DECL. Methods are first completed
6082 then checked according to regular method existance rules. If no
6083 constructor for CLASS_DECL were encountered, then build its
6084 declaration. */
e04a16fb
AG
6085
6086static void
6087java_check_regular_methods (class_decl)
6088 tree class_decl;
6089{
c2952b01 6090 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6091 tree method;
6092 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6093 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6094 tree mthrows;
6095
6096 /* It is not necessary to check methods defined in java.lang.Object */
6097 if (class == object_type_node)
6098 return;
e04a16fb 6099
23a79c61
APB
6100 if (!TYPE_NVIRTUALS (class))
6101 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6102
6103 /* Should take interfaces into account. FIXME */
6104 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6105 {
5e942c50 6106 tree sig;
e04a16fb
AG
6107 tree method_wfl = DECL_NAME (method);
6108 int aflags;
6109
5e942c50
APB
6110 /* If we previously found something and its name was saved,
6111 reinstall it now */
6112 if (found && saved_found_wfl)
ba179f9f
APB
6113 {
6114 DECL_NAME (found) = saved_found_wfl;
6115 saved_found_wfl = NULL_TREE;
6116 }
5e942c50 6117
e04a16fb
AG
6118 /* Check for redefinitions */
6119 if (check_method_redefinition (class, method))
6120 continue;
6121
22eed1e6
APB
6122 /* If we see one constructor a mark so we don't generate the
6123 default one. Also skip other verifications: constructors
6124 can't be inherited hence hiden or overriden */
6125 if (DECL_CONSTRUCTOR_P (method))
6126 {
6127 saw_constructor = 1;
6128 continue;
6129 }
6130
c877974e
APB
6131 /* We verify things thrown by the method. They must inherits from
6132 java.lang.Throwable */
6133 for (mthrows = DECL_FUNCTION_THROWS (method);
6134 mthrows; mthrows = TREE_CHAIN (mthrows))
6135 {
6136 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6137 parse_error_context
781b0558 6138 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6139 IDENTIFIER_POINTER
6140 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6141 }
6142
e04a16fb 6143 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6144 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6145
c2952b01
APB
6146 /* Inner class can't declare static methods */
6147 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6148 {
6149 char *t = xstrdup (lang_printable_name (class, 0));
6150 parse_error_context
6151 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6152 lang_printable_name (method, 0), t);
6153 free (t);
6154 }
6155
5e942c50 6156 /* Nothing overrides or it's a private method. */
aabd7048 6157 if (!found)
5e942c50 6158 continue;
aabd7048
PB
6159 if (METHOD_PRIVATE (found))
6160 {
6161 found = NULL_TREE;
6162 continue;
6163 }
5e942c50
APB
6164
6165 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6166 We set it temporarily for the sake of the error report. */
6167 saved_found_wfl = DECL_NAME (found);
6168 reset_method_name (found);
6169
614eaae0
APB
6170 /* If `found' is declared in an interface, make sure the
6171 modifier matches. */
6172 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6173 && clinit_identifier_node != DECL_NAME (found)
6174 && !METHOD_PUBLIC (method))
6175 {
6176 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6177 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6178 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6179 lang_printable_name (method, 0),
6180 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6181 }
6182
e04a16fb
AG
6183 /* Can't override a method with the same name and different return
6184 types. */
6185 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6186 {
614eaae0
APB
6187 char *t = xstrdup
6188 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6189 parse_error_context
7f10c2e2 6190 (method_wfl,
b9f7e36c 6191 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6192 lang_printable_name (found, 0), t,
b9f7e36c
APB
6193 IDENTIFIER_POINTER
6194 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6195 free (t);
6196 }
e04a16fb 6197
7f10c2e2
APB
6198 aflags = get_access_flags_from_decl (found);
6199 /* If the method has default, access in an other package, then
6200 issue a warning that the current method doesn't override the
6201 one that was found elsewhere. Do not issue this warning when
6202 the match was found in java.lang.Object. */
6203 if (DECL_CONTEXT (found) != object_type_node
a003f638 6204 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6205 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6206 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6207 && flag_not_overriding)
6208 {
6209 parse_warning_context
781b0558 6210 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
7f10c2e2
APB
6211 lang_printable_name (found, 0),
6212 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6213 IDENTIFIER_POINTER (DECL_NAME
6214 (TYPE_NAME (DECL_CONTEXT (found)))));
6215 continue;
6216 }
6217
e04a16fb
AG
6218 /* Can't override final. Can't override static. */
6219 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6220 {
6221 /* Static *can* override static */
6222 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6223 continue;
6224 parse_error_context
6225 (method_wfl,
6226 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6227 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6228 lang_printable_name (found, 0),
e04a16fb
AG
6229 (METHOD_FINAL (found) ? "final" : "static"),
6230 IDENTIFIER_POINTER
6231 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6232 continue;
6233 }
7f10c2e2 6234
e04a16fb
AG
6235 /* Static method can't override instance method. */
6236 if (METHOD_STATIC (method))
6237 {
6238 parse_error_context
6239 (method_wfl,
781b0558 6240 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6241 lang_printable_name (found, 0),
e04a16fb
AG
6242 IDENTIFIER_POINTER
6243 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6244 continue;
6245 }
5e942c50 6246
5e942c50
APB
6247 /* - Overriding/hiding public must be public
6248 - Overriding/hiding protected must be protected or public
6249 - If the overriden or hidden method has default (package)
6250 access, then the overriding or hiding method must not be
614eaae0
APB
6251 private; otherwise, a compile-time error occurs. If
6252 `found' belongs to an interface, things have been already
6253 taken care of. */
6254 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6255 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6256 || (METHOD_PROTECTED (found)
6257 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6258 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6259 && METHOD_PRIVATE (method))))
e04a16fb
AG
6260 {
6261 parse_error_context
6262 (method_wfl,
781b0558 6263 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6264 (METHOD_PUBLIC (method) ? "public" :
6265 (METHOD_PRIVATE (method) ? "private" : "protected")),
6266 IDENTIFIER_POINTER (DECL_NAME
6267 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6268 continue;
6269 }
6270
b9f7e36c
APB
6271 /* Overriding methods must have compatible `throws' clauses on checked
6272 exceptions, if any */
6273 check_throws_clauses (method, method_wfl, found);
6274
e04a16fb
AG
6275 /* Inheriting multiple methods with the same signature. FIXME */
6276 }
6277
5e942c50
APB
6278 /* Don't forget eventual pending found and saved_found_wfl. Take
6279 into account that we might have exited because we saw an
d77613be 6280 artificial method as the last entry. */
5e942c50
APB
6281
6282 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6283 DECL_NAME (found) = saved_found_wfl;
6284
23a79c61
APB
6285 if (!TYPE_NVIRTUALS (class))
6286 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6287
d77613be
APB
6288 /* Search for inherited abstract method not yet implemented in this
6289 class. */
6290 java_check_abstract_method_definitions (class_decl);
6291
22eed1e6 6292 if (!saw_constructor)
e920ebc9 6293 fatal ("No constructor found");
e04a16fb
AG
6294}
6295
b9f7e36c
APB
6296/* Return a non zero value if the `throws' clause of METHOD (if any)
6297 is incompatible with the `throws' clause of FOUND (if any). */
6298
6299static void
6300check_throws_clauses (method, method_wfl, found)
6301 tree method, method_wfl, found;
6302{
6303 tree mthrows, fthrows;
6304
c877974e
APB
6305 /* Can't check these things with class loaded from bytecode. FIXME */
6306 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6307 return;
6308
b9f7e36c
APB
6309 for (mthrows = DECL_FUNCTION_THROWS (method);
6310 mthrows; mthrows = TREE_CHAIN (mthrows))
6311 {
6312 /* We don't verify unchecked expressions */
c877974e 6313 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6314 continue;
6315 /* Checked expression must be compatible */
6316 for (fthrows = DECL_FUNCTION_THROWS (found);
6317 fthrows; fthrows = TREE_CHAIN (fthrows))
6318 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6319 break;
6320 if (!fthrows)
6321 {
6322 parse_error_context
781b0558 6323 (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'",
b9f7e36c 6324 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6325 lang_printable_name (found, 0),
b9f7e36c
APB
6326 IDENTIFIER_POINTER
6327 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6328 }
6329 }
6330}
6331
e04a16fb
AG
6332/* Check abstract method of interface INTERFACE */
6333
6334static void
5e942c50
APB
6335java_check_abstract_methods (interface_decl)
6336 tree interface_decl;
e04a16fb
AG
6337{
6338 int i, n;
6339 tree method, basetype_vec, found;
5e942c50 6340 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6341
6342 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6343 {
b9f7e36c 6344 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6345
6346 /* 2- Check for double definition inside the defining interface */
6347 if (check_method_redefinition (interface, method))
6348 continue;
6349
6350 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6351 the thrown exceptions (FIXME) */
e04a16fb
AG
6352 found = lookup_java_interface_method2 (interface, method);
6353 if (found)
6354 {
5e942c50
APB
6355 char *t;
6356 tree saved_found_wfl = DECL_NAME (found);
6357 reset_method_name (found);
c2e3db92 6358 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6359 parse_error_context
b9f7e36c 6360 (method_wfl,
5e942c50 6361 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6362 lang_printable_name (found, 0), t,
b9f7e36c
APB
6363 IDENTIFIER_POINTER
6364 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6365 free (t);
5e942c50 6366 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6367 continue;
e04a16fb
AG
6368 }
6369 }
6370
6371 /* 4- Inherited methods can't differ by their returned types */
6372 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6373 return;
6374 n = TREE_VEC_LENGTH (basetype_vec);
6375 for (i = 0; i < n; i++)
6376 {
6377 tree sub_interface_method, sub_interface;
6378 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6379 if (!vec_elt)
6380 continue;
6381 sub_interface = BINFO_TYPE (vec_elt);
6382 for (sub_interface_method = TYPE_METHODS (sub_interface);
6383 sub_interface_method;
6384 sub_interface_method = TREE_CHAIN (sub_interface_method))
6385 {
6386 found = lookup_java_interface_method2 (interface,
6387 sub_interface_method);
6388 if (found && (found != sub_interface_method))
5e942c50
APB
6389 {
6390 tree saved_found_wfl = DECL_NAME (found);
6391 reset_method_name (found);
6392 parse_error_context
6393 (lookup_cl (sub_interface_method),
781b0558 6394 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6395 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6396 lang_printable_name (found, 0),
6397 IDENTIFIER_POINTER
6398 (DECL_NAME (TYPE_NAME
6399 (DECL_CONTEXT (sub_interface_method)))),
6400 IDENTIFIER_POINTER
6401 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6402 DECL_NAME (found) = saved_found_wfl;
6403 }
e04a16fb
AG
6404 }
6405 }
6406}
6407
e04a16fb
AG
6408/* Lookup methods in interfaces using their name and partial
6409 signature. Return a matching method only if their types differ. */
6410
6411static tree
6412lookup_java_interface_method2 (class, method_decl)
6413 tree class, method_decl;
6414{
6415 int i, n;
6416 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6417
6418 if (!basetype_vec)
6419 return NULL_TREE;
6420
6421 n = TREE_VEC_LENGTH (basetype_vec);
6422 for (i = 0; i < n; i++)
6423 {
6424 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6425 if ((BINFO_TYPE (vec_elt) != object_type_node)
6426 && (to_return =
6427 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6428 return to_return;
6429 }
6430 for (i = 0; i < n; i++)
6431 {
6432 to_return = lookup_java_interface_method2
6433 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6434 if (to_return)
6435 return to_return;
6436 }
6437
6438 return NULL_TREE;
6439}
6440
6441/* Lookup method using their name and partial signature. Return a
6442 matching method only if their types differ. */
6443
6444static tree
6445lookup_java_method2 (clas, method_decl, do_interface)
6446 tree clas, method_decl;
6447 int do_interface;
6448{
5e942c50
APB
6449 tree method, method_signature, method_name, method_type, name;
6450
e04a16fb 6451 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6452 name = DECL_NAME (method_decl);
6453 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6454 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6455 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6456
6457 while (clas != NULL_TREE)
6458 {
6459 for (method = TYPE_METHODS (clas);
6460 method != NULL_TREE; method = TREE_CHAIN (method))
6461 {
6462 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6463 tree name = DECL_NAME (method);
6464 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6465 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6466 && method_sig == method_signature
6467 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6468 return method;
e04a16fb
AG
6469 }
6470 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6471 }
6472 return NULL_TREE;
6473}
6474
f441f671
APB
6475/* Return the line that matches DECL line number, and try its best to
6476 position the column number. Used during error reports. */
e04a16fb
AG
6477
6478static tree
6479lookup_cl (decl)
6480 tree decl;
6481{
6482 static tree cl = NULL_TREE;
f441f671 6483 char *line, *found;
e04a16fb
AG
6484
6485 if (!decl)
6486 return NULL_TREE;
6487
6488 if (cl == NULL_TREE)
19e223db
MM
6489 {
6490 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6491 ggc_add_tree_root (&cl, 1);
6492 }
e04a16fb
AG
6493
6494 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6495 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6496
dba41d30 6497 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6498 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6499
6500 found = strstr ((const char *)line,
6501 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6502 if (found)
6503 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6504
e04a16fb
AG
6505 return cl;
6506}
6507
6508/* Look for a simple name in the single-type import list */
6509
6510static tree
6511find_name_in_single_imports (name)
6512 tree name;
6513{
6514 tree node;
6515
6516 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6517 if (TREE_VALUE (node) == name)
6518 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6519
6520 return NULL_TREE;
6521}
6522
6523/* Process all single-type import. */
6524
6525static int
6526process_imports ()
6527{
6528 tree import;
6529 int error_found;
6530
6531 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6532 {
6533 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6534
6535 /* Don't load twice something already defined. */
6536 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6537 continue;
02ae6e2e
APB
6538
6539 while (1)
6540 {
6541 tree left;
6542
6543 QUALIFIED_P (to_be_found) = 1;
6544 load_class (to_be_found, 0);
6545 error_found =
6546 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6547
6548 /* We found it, we can bail out */
6549 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6550 break;
6551
6552 /* We haven't found it. Maybe we're trying to access an
6553 inner class. The only way for us to know is to try again
6554 after having dropped a qualifier. If we can't break it further,
6555 we have an error. */
6556 if (breakdown_qualified (&left, NULL, to_be_found))
6557 break;
6558
6559 to_be_found = left;
6560 }
e04a16fb
AG
6561 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6562 {
6563 parse_error_context (TREE_PURPOSE (import),
6564 "Class or interface `%s' not found in import",
6565 IDENTIFIER_POINTER (to_be_found));
6566 return 1;
6567 }
6568 if (error_found)
6569 return 1;
6570 }
6571 return 0;
6572}
6573
9a7ab4b3
APB
6574/* Possibly find and mark a class imported by a single-type import
6575 statement. */
e04a16fb 6576
9a7ab4b3 6577static void
e04a16fb
AG
6578find_in_imports (class_type)
6579 tree class_type;
6580{
6581 tree import;
6582
6583 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6584 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6585 {
6586 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6587 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6588 }
e04a16fb
AG
6589}
6590
e04a16fb 6591static int
63a212ed 6592note_possible_classname (name, len)
49f48c71 6593 const char *name;
63a212ed 6594 int len;
e04a16fb 6595{
63a212ed
PB
6596 tree node;
6597 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6598 len = len - 5;
6599 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6600 len = len - 6;
e04a16fb 6601 else
63a212ed
PB
6602 return 0;
6603 node = ident_subst (name, len, "", '/', '.', "");
6604 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6605 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6606 return 1;
e04a16fb
AG
6607}
6608
6609/* Read a import directory, gathering potential match for further type
6610 references. Indifferently reads a filesystem or a ZIP archive
6611 directory. */
6612
6613static void
6614read_import_dir (wfl)
6615 tree wfl;
6616{
63a212ed 6617 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6618 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6619 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6620 DIR *dirp = NULL;
d8fccff5 6621 JCF *saved_jcf = current_jcf;
e04a16fb 6622
63a212ed
PB
6623 int found = 0;
6624 int k;
6625 void *entry;
6626 struct buffer filename[1];
6627
6628
6629 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6630 return;
6631 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6632
6633 BUFFER_INIT (filename);
6634 buffer_grow (filename, package_length + 100);
6635
6636 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6637 {
49f48c71 6638 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6639 int entry_length = strlen (entry_name);
6640 if (jcf_path_is_zipfile (entry))
6641 {
6642 ZipFile *zipf;
6643 buffer_grow (filename, entry_length);
6644 memcpy (filename->data, entry_name, entry_length - 1);
6645 filename->data[entry_length-1] = '\0';
6646 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6647 if (zipf == NULL)
6648 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6649 else
6650 {
6651 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6652 BUFFER_RESET (filename);
6653 for (k = 0; k < package_length; k++)
6654 {
6655 char ch = package_name[k];
6656 *filename->ptr++ = ch == '.' ? '/' : ch;
6657 }
6658 *filename->ptr++ = '/';
6659
345137c7 6660 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6661 {
49f48c71 6662 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6663 int current_entry_len = zipd->filename_length;
6664
345137c7
TT
6665 if (current_entry_len >= BUFFER_LENGTH (filename)
6666 && strncmp (filename->data, current_entry,
6667 BUFFER_LENGTH (filename)) != 0)
63a212ed 6668 continue;
345137c7 6669 found |= note_possible_classname (current_entry,
63a212ed
PB
6670 current_entry_len);
6671 }
6672 }
6673 }
6674 else
6675 {
6676 BUFFER_RESET (filename);
6677 buffer_grow (filename, entry_length + package_length + 4);
6678 strcpy (filename->data, entry_name);
6679 filename->ptr = filename->data + entry_length;
6680 for (k = 0; k < package_length; k++)
6681 {
6682 char ch = package_name[k];
6683 *filename->ptr++ = ch == '.' ? '/' : ch;
6684 }
6685 *filename->ptr = '\0';
6686
6687 dirp = opendir (filename->data);
6688 if (dirp == NULL)
6689 continue;
6690 *filename->ptr++ = '/';
6691 for (;;)
6692 {
63a212ed 6693 int len;
49f48c71 6694 const char *d_name;
63a212ed
PB
6695 struct dirent *direntp = readdir (dirp);
6696 if (!direntp)
6697 break;
6698 d_name = direntp->d_name;
6699 len = strlen (direntp->d_name);
6700 buffer_grow (filename, len+1);
6701 strcpy (filename->ptr, d_name);
345137c7 6702 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6703 package_length+len+1);
6704 }
6705 if (dirp)
6706 closedir (dirp);
6707 }
6708 }
e04a16fb 6709
63a212ed 6710 free (filename->data);
e04a16fb 6711
63a212ed
PB
6712 /* Here we should have a unified way of retrieving an entry, to be
6713 indexed. */
6714 if (!found)
e04a16fb
AG
6715 {
6716 static int first = 1;
6717 if (first)
6718 {
781b0558 6719 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6720 java_error_count++;
6721 first = 0;
6722 }
6723 else
63a212ed
PB
6724 parse_error_context (wfl, "Package `%s' not found in import",
6725 package_name);
e04a16fb
AG
6726 current_jcf = saved_jcf;
6727 return;
6728 }
e04a16fb
AG
6729 current_jcf = saved_jcf;
6730}
6731
6732/* Possibly find a type in the import on demands specified
6733 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6734 entire list, to detected potential double definitions. */
6735
6736static int
6737find_in_imports_on_demand (class_type)
6738 tree class_type;
6739{
ab3a6dd6 6740 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6741 int seen_once = -1;
ab3a6dd6 6742 tree cl = NULL_TREE;
e04a16fb
AG
6743
6744 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6745 {
49f48c71 6746 const char *id_name;
e04a16fb
AG
6747 obstack_grow (&temporary_obstack,
6748 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6749 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6750 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6751 obstack_grow0 (&temporary_obstack,
6752 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6753 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6754 id_name = obstack_finish (&temporary_obstack);
6755
6756 node = maybe_get_identifier (id_name);
6757 if (node && IS_A_CLASSFILE_NAME (node))
6758 {
6759 if (seen_once < 0)
6760 {
6761 cl = TREE_PURPOSE (import);
6762 seen_once = 1;
6763 node_to_use = node;
6764 }
6765 else
6766 {
6767 seen_once++;
6768 parse_error_context
1e12ab9b
APB
6769 (TREE_PURPOSE (import),
6770 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6771 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6772 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6773 }
6774 }
6775 }
6776
6777 if (seen_once == 1)
6778 {
6779 /* Setup lineno so that it refers to the line of the import (in
6780 case we parse a class file and encounter errors */
6781 tree decl;
6782 int saved_lineno = lineno;
6783 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6784 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6785 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6786 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6787 /* If there is no DECL set for the class or if the class isn't
6788 loaded and not seen in source yet, the load */
6789 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6790 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6791 load_class (node_to_use, 0);
6792 lineno = saved_lineno;
6793 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6794 }
6795 else
6796 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6797}
6798
9a7ab4b3
APB
6799/* Add package NAME to the list of package encountered so far. To
6800 speed up class lookup in do_resolve_class, we make sure a
6801 particular package is added only once. */
6802
6803static void
6804register_package (name)
6805 tree name;
6806{
6807 static struct hash_table _pht, *pht = NULL;
6808
6809 if (!pht)
6810 {
6811 hash_table_init (&_pht, hash_newfunc,
6812 java_hash_hash_tree_node, java_hash_compare_tree_node);
6813 pht = &_pht;
6814 }
6815
6816 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6817 {
6818 package_list = chainon (package_list, build_tree_list (name, NULL));
6819 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6820 }
6821}
6822
5e942c50
APB
6823static tree
6824resolve_package (pkg, next)
6825 tree pkg, *next;
6826{
c2952b01 6827 tree current, acc;
5e942c50 6828 tree type_name = NULL_TREE;
49f48c71 6829 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6830
6831 /* The trick is to determine when the package name stops and were
6832 the name of something contained in the package starts. Then we
6833 return a fully qualified name of what we want to get. */
6834
6835 /* Do a quick search on well known package names */
6836 if (!strncmp (name, "java.lang.reflect", 17))
6837 {
6838 *next =
6839 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6840 type_name = lookup_package_type (name, 17);
6841 }
6842 else if (!strncmp (name, "java.lang", 9))
6843 {
6844 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6845 type_name = lookup_package_type (name, 9);
6846 }
5e942c50 6847
2c56429a
APB
6848 /* If we found something here, return */
6849 if (type_name)
6850 return type_name;
6851
6852 *next = EXPR_WFL_QUALIFICATION (pkg);
6853
6854 /* Try the current package. */
6855 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6856 IDENTIFIER_LENGTH (ctxp->package)))
6857 {
6858 type_name =
6859 lookup_package_type_and_set_next (name,
6860 IDENTIFIER_LENGTH (ctxp->package),
6861 next );
6862 if (type_name)
6863 return type_name;
6864 }
6865
6866 /* Search in imported package */
6867 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6868 {
6869 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6870 int len = IDENTIFIER_LENGTH (current_pkg_name);
6871 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6872 {
6873 tree left, dummy;
6874
6875 breakdown_qualified (&left, &dummy, current_pkg_name);
6876 len = IDENTIFIER_LENGTH (left);
6877 type_name = lookup_package_type_and_set_next (name, len, next);
6878 if (type_name)
6879 break;
6880 }
6881 }
6882
c2952b01
APB
6883 /* Try to progressively construct a type name */
6884 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6885 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6886 current; current = TREE_CHAIN (current))
6887 {
6888 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6889 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6890 {
6891 type_name = acc;
6b48deee
APB
6892 /* resolve_package should be used in a loop, hence we
6893 point at this one to naturally process the next one at
6894 the next iteration. */
6895 *next = current;
c2952b01
APB
6896 break;
6897 }
6898 }
2c56429a
APB
6899 return type_name;
6900}
6901
6902static tree
6903lookup_package_type_and_set_next (name, len, next)
49f48c71 6904 const char *name;
2c56429a
APB
6905 int len;
6906 tree *next;
6907{
49f48c71 6908 const char *ptr;
2c56429a
APB
6909 tree type_name = lookup_package_type (name, len);
6910
6911 if (!type_name)
6912 return NULL;
6913
6914 ptr = IDENTIFIER_POINTER (type_name);
6915 while (ptr && (ptr = strchr (ptr, '.')))
6916 {
6917 *next = TREE_CHAIN (*next);
6918 ptr++;
6919 }
5e942c50
APB
6920 return type_name;
6921}
6922
6923static tree
6924lookup_package_type (name, from)
49f48c71 6925 const char *name;
5e942c50
APB
6926 int from;
6927{
6928 char subname [128];
49f48c71 6929 const char *sub = &name[from+1];
5e942c50
APB
6930 while (*sub != '.' && *sub)
6931 sub++;
6932 strncpy (subname, name, sub-name);
6933 subname [sub-name] = '\0';
6934 return get_identifier (subname);
6935}
6936
cf1748bf 6937static void
4dbf4496
APB
6938check_inner_class_access (decl, enclosing_decl, cl)
6939 tree decl, enclosing_decl, cl;
cf1748bf 6940{
4dbf4496
APB
6941 int access = 0;
6942
cf1748bf 6943 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6944 as a result of processing a JDEP crafted by source_start_java_method
6945 for the purpose of patching its parm decl. But the error would
6946 have been already trapped when fixing the method's signature.
6947 DECL can also be NULL in case of earlier errors. */
6948 if (!decl || !cl)
cf1748bf
APB
6949 return;
6950
4dbf4496
APB
6951 /* We grant access to private and protected inner classes if the
6952 location from where we're trying to access DECL is an enclosing
6953 context for DECL or if both have a common enclosing context. */
6954 if (CLASS_PRIVATE (decl))
6955 access = 1;
6956 if (CLASS_PROTECTED (decl))
6957 access = 2;
6958 if (!access)
6959 return;
6960
6961 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6962 TREE_TYPE (decl))
6963 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6964 TREE_TYPE (decl)))
6965 return;
6966
6967 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6968 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6969 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6970 lang_printable_name (decl, 0));
6971}
6972
e04a16fb
AG
6973/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6974 access violations were found, 1 otherwise. */
6975
6976static int
6977check_pkg_class_access (class_name, cl)
6978 tree class_name;
6979 tree cl;
6980{
6981 tree type;
e04a16fb
AG
6982
6983 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6984 return 0;
6985
6986 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6987 return 0;
6988
6989 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6990 {
e28cd97b
APB
6991 /* Access to a private class within the same package is
6992 allowed. */
6993 tree l, r;
6994 breakdown_qualified (&l, &r, class_name);
6995 if (l == ctxp->package)
6996 return 0;
6997
e04a16fb 6998 parse_error_context
781b0558 6999 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
7000 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7001 IDENTIFIER_POINTER (class_name));
7002 return 1;
7003 }
7004 return 0;
7005}
7006
7007/* Local variable declaration. */
7008
7009static void
7010declare_local_variables (modifier, type, vlist)
7011 int modifier;
7012 tree type;
7013 tree vlist;
7014{
c583dd46
APB
7015 tree decl, current, saved_type;
7016 tree type_wfl = NULL_TREE;
e04a16fb 7017 int must_chain = 0;
c2952b01 7018 int final_p = 0;
e04a16fb 7019
2aa11e97 7020 /* Push a new block if statements were seen between the last time we
e04a16fb 7021 pushed a block and now. Keep a cound of block to close */
f099f336 7022 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 7023 {
f099f336 7024 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 7025 tree b = enter_block ();
f099f336 7026 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
7027 }
7028
7029 if (modifier)
7030 {
7031 int i;
7032 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 7033 if (modifier == ACC_FINAL)
c2952b01 7034 final_p = 1;
c877974e
APB
7035 else
7036 {
7037 parse_error_context
7038 (ctxp->modifier_ctx [i],
7039 "Only `final' is allowed as a local variables modifier");
7040 return;
7041 }
e04a16fb
AG
7042 }
7043
c583dd46
APB
7044 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7045 hold the TYPE value if a new incomplete has to be created (as
7046 opposed to being found already existing and reused). */
7047 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7048
7049 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 7050 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
7051
7052 /* Go through all the declared variables */
7053 for (current = vlist, saved_type = type; current;
7054 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 7055 {
c877974e 7056 tree other, real_type;
e04a16fb
AG
7057 tree wfl = TREE_PURPOSE (current);
7058 tree name = EXPR_WFL_NODE (wfl);
7059 tree init = TREE_VALUE (current);
e04a16fb 7060
c583dd46
APB
7061 /* Process NAME, as it may specify extra dimension(s) for it */
7062 type = build_array_from_name (type, type_wfl, name, &name);
7063
7064 /* Variable redefinition check */
7065 if ((other = lookup_name_in_blocks (name)))
7066 {
7067 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7068 DECL_SOURCE_LINE (other));
7069 continue;
7070 }
7071
7072 /* Type adjustment. We may have just readjusted TYPE because
7073 the variable specified more dimensions. Make sure we have
7074 a reference if we can and don't have one already. */
1886c9d8 7075 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7076
7077 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7078 /* Never layout this decl. This will be done when its scope
7079 will be entered */
c877974e 7080 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 7081 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 7082 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7083 BLOCK_CHAIN_DECL (decl);
7084
d4370213
APB
7085 /* If doing xreferencing, replace the line number with the WFL
7086 compound value */
7087 if (flag_emit_xref)
7088 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7089
e04a16fb
AG
7090 /* Don't try to use an INIT statement when an error was found */
7091 if (init && java_error_count)
7092 init = NULL_TREE;
c583dd46
APB
7093
7094 /* Add the initialization function to the current function's code */
7095 if (init)
e04a16fb 7096 {
c583dd46
APB
7097 /* Name might have been readjusted */
7098 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7099 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7100 java_method_add_stmt (current_function_decl,
7101 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7102 init));
7103 }
7104
7105 /* Setup dependency the type of the decl */
7106 if (must_chain)
7107 {
7108 jdep *dep;
7109 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7110 dep = CLASSD_LAST (ctxp->classd_list);
7111 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7112 }
7113 }
7114 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7115}
7116
7117/* Called during parsing. Build decls from argument list. */
7118
7119static void
7120source_start_java_method (fndecl)
7121 tree fndecl;
7122{
7123 tree tem;
7124 tree parm_decl;
7125 int i;
7126
79d13333
APB
7127 if (!fndecl)
7128 return;
7129
e04a16fb
AG
7130 current_function_decl = fndecl;
7131
7132 /* New scope for the function */
7133 enter_block ();
7134 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7135 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7136 {
7137 tree type = TREE_VALUE (tem);
7138 tree name = TREE_PURPOSE (tem);
7139
23a79c61
APB
7140 /* If type is incomplete. Create an incomplete decl and ask for
7141 the decl to be patched later */
e04a16fb
AG
7142 if (INCOMPLETE_TYPE_P (type))
7143 {
7144 jdep *jdep;
c877974e
APB
7145 tree real_type = GET_REAL_TYPE (type);
7146 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7147 type = obtain_incomplete_type (type);
e04a16fb
AG
7148 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7149 jdep = CLASSD_LAST (ctxp->classd_list);
7150 JDEP_MISC (jdep) = name;
7151 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7152 }
7153 else
7154 parm_decl = build_decl (PARM_DECL, name, type);
7155
c2952b01
APB
7156 /* Remember if a local variable was declared final (via its
7157 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7158 if (ARG_FINAL_P (tem))
c7303e41
APB
7159 {
7160 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7161 LOCAL_FINAL (parm_decl) = 1;
7162 }
c2952b01 7163
e04a16fb
AG
7164 BLOCK_CHAIN_DECL (parm_decl);
7165 }
7166 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7167 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7168 nreverse (tem);
7169 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7170 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7171}
7172
22eed1e6
APB
7173/* Called during parsing. Creates an artificial method declaration. */
7174
7175static tree
7176create_artificial_method (class, flags, type, name, args)
7177 tree class;
7178 int flags;
7179 tree type, name, args;
7180{
22eed1e6
APB
7181 tree mdecl;
7182
c2952b01 7183 java_parser_context_save_global ();
22eed1e6
APB
7184 lineno = 0;
7185 mdecl = make_node (FUNCTION_TYPE);
7186 TREE_TYPE (mdecl) = type;
7187 TYPE_ARG_TYPES (mdecl) = args;
7188 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7189 java_parser_context_restore_global ();
22eed1e6
APB
7190 DECL_ARTIFICIAL (mdecl) = 1;
7191 return mdecl;
7192}
7193
7194/* Starts the body if an artifical method. */
7195
7196static void
7197start_artificial_method_body (mdecl)
7198 tree mdecl;
7199{
7200 DECL_SOURCE_LINE (mdecl) = 1;
7201 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7202 source_start_java_method (mdecl);
7203 enter_block ();
7204}
7205
7206static void
7207end_artificial_method_body (mdecl)
7208 tree mdecl;
7209{
1e0cdc10
APB
7210 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7211 It has to be evaluated first. (if mdecl is current_function_decl,
7212 we have an undefined behavior if no temporary variable is used.) */
7213 tree b = exit_block ();
7214 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7215 exit_block ();
7216}
7217
e04a16fb
AG
7218/* Called during expansion. Push decls formerly built from argument
7219 list so they're usable during expansion. */
7220
7221static void
7222expand_start_java_method (fndecl)
7223 tree fndecl;
7224{
7225 tree tem, *ptr;
e04a16fb 7226
e04a16fb
AG
7227 current_function_decl = fndecl;
7228
c2952b01
APB
7229 if (! quiet_flag)
7230 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7231 announce_function (fndecl);
c2952b01
APB
7232 if (! quiet_flag)
7233 fprintf (stderr, "]");
7234
7235 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7236 ptr = &DECL_ARGUMENTS (fndecl);
7237 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7238 while (tem)
7239 {
7240 tree next = TREE_CHAIN (tem);
b67d701b 7241 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7242 if (PROMOTE_PROTOTYPES
7243 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7244 && INTEGRAL_TYPE_P (type))
7245 type = integer_type_node;
b67d701b 7246 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7247 layout_decl (tem, 0);
7248 pushdecl (tem);
e04a16fb
AG
7249 *ptr = tem;
7250 ptr = &TREE_CHAIN (tem);
7251 tem = next;
7252 }
7253 *ptr = NULL_TREE;
7254 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7255 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7256}
7257
7258/* Terminate a function and expand its body. */
7259
7260static void
7261source_end_java_method ()
7262{
7263 tree fndecl = current_function_decl;
138657ec 7264 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7265
79d13333
APB
7266 if (!fndecl)
7267 return;
7268
e04a16fb
AG
7269 java_parser_context_save_global ();
7270 lineno = ctxp->last_ccb_indent1;
7271
b67d701b
PB
7272 /* Set EH language codes */
7273 java_set_exception_lang_code ();
7274
5423609c
APB
7275 /* Turn function bodies with only a NOP expr null, so they don't get
7276 generated at all and we won't get warnings when using the -W
7277 -Wall flags. */
7278 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7279 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7280
e04a16fb
AG
7281 /* Generate function's code */
7282 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7283 && ! flag_emit_class_files
7284 && ! flag_emit_xref)
e04a16fb
AG
7285 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7286
7287 /* pop out of its parameters */
7288 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7289 poplevel (1, 0, 1);
7290 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7291
7292 /* Generate rtl for function exit. */
e8fc7396 7293 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7294 {
7295 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7296 /* Emit catch-finally clauses */
7297 emit_handlers ();
e04a16fb
AG
7298 expand_function_end (input_filename, lineno, 0);
7299
138657ec
AH
7300 /* FIXME: If the current method contains any exception handlers,
7301 force asynchronous_exceptions: this is necessary because signal
7302 handlers in libjava may throw exceptions. This is far from being
7303 a perfect solution, but it's better than doing nothing at all.*/
7304 if (catch_clauses)
7305 asynchronous_exceptions = 1;
7306
e04a16fb
AG
7307 /* Run the optimizers and output assembler code for this function. */
7308 rest_of_compilation (fndecl);
7309 }
7310
7311 current_function_decl = NULL_TREE;
e04a16fb 7312 java_parser_context_restore_global ();
138657ec 7313 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7314}
7315
7316/* Record EXPR in the current function block. Complements compound
7317 expression second operand if necessary. */
7318
7319tree
7320java_method_add_stmt (fndecl, expr)
7321 tree fndecl, expr;
7322{
b771925e
APB
7323 if (!GET_CURRENT_BLOCK (fndecl))
7324 return NULL_TREE;
f099f336 7325 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7326}
e04a16fb 7327
b67d701b
PB
7328static tree
7329add_stmt_to_block (b, type, stmt)
7330 tree b, type, stmt;
7331{
7332 tree body = BLOCK_EXPR_BODY (b), c;
7333
e04a16fb
AG
7334 if (java_error_count)
7335 return body;
b67d701b
PB
7336
7337 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7338 return body;
7339
b67d701b
PB
7340 BLOCK_EXPR_BODY (b) = c;
7341 TREE_SIDE_EFFECTS (c) = 1;
7342 return c;
e04a16fb
AG
7343}
7344
7345/* Add STMT to EXISTING if possible, otherwise create a new
7346 COMPOUND_EXPR and add STMT to it. */
7347
7348static tree
7349add_stmt_to_compound (existing, type, stmt)
7350 tree existing, type, stmt;
7351{
15fdcfe9
PB
7352 if (existing)
7353 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7354 else
15fdcfe9 7355 return stmt;
e04a16fb
AG
7356}
7357
1886c9d8
APB
7358void java_layout_seen_class_methods ()
7359{
7360 tree previous_list = all_class_list;
7361 tree end = NULL_TREE;
7362 tree current;
7363
7364 while (1)
7365 {
7366 for (current = previous_list;
7367 current != end; current = TREE_CHAIN (current))
7368 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7369
7370 if (previous_list != all_class_list)
7371 {
7372 end = previous_list;
7373 previous_list = all_class_list;
7374 }
7375 else
7376 break;
7377 }
7378}
7379
e04a16fb 7380void
c2952b01 7381java_reorder_fields ()
e04a16fb 7382{
c2952b01 7383 static tree stop_reordering = NULL_TREE;
19e223db 7384 static int initialized_p;
c2952b01 7385 tree current;
19e223db
MM
7386
7387 /* Register STOP_REORDERING with the garbage collector. */
7388 if (!initialized_p)
7389 {
7390 ggc_add_tree_root (&stop_reordering, 1);
7391 initialized_p = 1;
7392 }
7393
5e942c50 7394 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7395 {
5e942c50 7396 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7397
c2952b01
APB
7398 if (current_class == stop_reordering)
7399 break;
7400
c877974e
APB
7401 /* Reverse the fields, but leave the dummy field in front.
7402 Fields are already ordered for Object and Class */
7403 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7404 && current_class != class_type_node)
7405 {
23a79c61
APB
7406 /* If the dummy field is there, reverse the right fields and
7407 just layout the type for proper fields offset */
c877974e
APB
7408 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7409 {
7410 tree fields = TYPE_FIELDS (current_class);
7411 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7412 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7413 }
23a79c61
APB
7414 /* We don't have a dummy field, we need to layout the class,
7415 after having reversed the fields */
c877974e
APB
7416 else
7417 {
7418 TYPE_FIELDS (current_class) =
7419 nreverse (TYPE_FIELDS (current_class));
7420 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7421 }
7422 }
c2952b01
APB
7423 }
7424 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7425}
7426
7427/* Layout the methods of all classes loaded in one way on an
7428 other. Check methods of source parsed classes. Then reorder the
7429 fields and layout the classes or the type of all source parsed
7430 classes */
7431
7432void
7433java_layout_classes ()
7434{
7435 tree current;
7436 int save_error_count = java_error_count;
7437
7438 /* Layout the methods of all classes seen so far */
7439 java_layout_seen_class_methods ();
7440 java_parse_abort_on_error ();
7441 all_class_list = NULL_TREE;
7442
7443 /* Then check the methods of all parsed classes */
7444 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7445 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7446 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7447 java_parse_abort_on_error ();
7448
7449 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7450 {
7451 current_class = TREE_TYPE (TREE_VALUE (current));
7452 layout_class (current_class);
5e942c50 7453
c877974e
APB
7454 /* From now on, the class is considered completely loaded */
7455 CLASS_LOADED_P (current_class) = 1;
7456
5e942c50
APB
7457 /* Error reported by the caller */
7458 if (java_error_count)
7459 return;
e04a16fb 7460 }
23a79c61
APB
7461
7462 /* We might have reloaded classes durign the process of laying out
7463 classes for code generation. We must layout the methods of those
7464 late additions, as constructor checks might use them */
1886c9d8 7465 java_layout_seen_class_methods ();
23a79c61 7466 java_parse_abort_on_error ();
e04a16fb
AG
7467}
7468
c2952b01
APB
7469/* Expand methods in the current set of classes rememebered for
7470 generation. */
e04a16fb 7471
49f48c71 7472static void
c2952b01 7473java_complete_expand_classes ()
e04a16fb
AG
7474{
7475 tree current;
ce6e9147
APB
7476
7477 do_not_fold = flag_emit_xref;
c2952b01 7478
e04a16fb 7479 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7480 if (!INNER_CLASS_DECL_P (current))
7481 java_complete_expand_class (current);
7482}
e04a16fb 7483
c2952b01
APB
7484/* Expand the methods found in OUTER, starting first by OUTER's inner
7485 classes, if any. */
e04a16fb 7486
c2952b01
APB
7487static void
7488java_complete_expand_class (outer)
7489 tree outer;
7490{
7491 tree inner_list;
e04a16fb 7492
c2952b01 7493 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7494
c2952b01
APB
7495 /* We need to go after all inner classes and start expanding them,
7496 starting with most nested ones. We have to do that because nested
7497 classes might add functions to outer classes */
e04a16fb 7498
c2952b01
APB
7499 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7500 inner_list; inner_list = TREE_CHAIN (inner_list))
7501 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7502
c2952b01
APB
7503 java_complete_expand_methods (outer);
7504 set_nested_class_simple_name_value (outer, 0); /* Reset */
7505}
7506
7507/* Expand methods registered in CLASS_DECL. The general idea is that
7508 we expand regular methods first. This allows us get an estimate on
7509 how outer context local alias fields are really used so we can add
7510 to the constructor just enough code to initialize them properly (it
c00f0fb2 7511 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7512 constructors and then <clinit>. */
7513
7514static void
7515java_complete_expand_methods (class_decl)
7516 tree class_decl;
7517{
7518 tree clinit, finit, decl, first_decl;
7519
7520 current_class = TREE_TYPE (class_decl);
7521
c7303e41
APB
7522 /* Find whether the class has final variables */
7523 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7524 if (FIELD_FINAL (decl))
7525 {
7526 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7527 break;
7528 }
7529
c2952b01
APB
7530 /* Initialize a new constant pool */
7531 init_outgoing_cpool ();
7532
7533 /* Pre-expand <clinit> to figure whether we really need it or
7534 not. If we do need it, we pre-expand the static fields so they're
7535 ready to be used somewhere else. <clinit> will be fully expanded
7536 after we processed the constructors. */
7537 first_decl = TYPE_METHODS (current_class);
7538 clinit = maybe_generate_pre_expand_clinit (current_class);
7539
c00f0fb2 7540 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7541 try to use it.*/
7542 if (TYPE_FINIT_STMT_LIST (current_class))
7543 {
7544 finit = generate_finit (current_class);
7545 java_complete_expand_method (finit);
7546 }
7547
7548 /* Now do the constructors */
7549 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7550 {
7551 int no_body;
7552
7553 if (!DECL_CONSTRUCTOR_P (decl))
7554 continue;
7555
7556 no_body = !DECL_FUNCTION_BODY (decl);
7557 /* Don't generate debug info on line zero when expanding a
7558 generated constructor. */
7559 if (no_body)
7560 restore_line_number_status (1);
7561
c7303e41
APB
7562 /* Reset the final local variable assignment flags */
7563 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7564 reset_final_variable_local_assignment_flag (current_class);
7565
c2952b01 7566 java_complete_expand_method (decl);
c7303e41
APB
7567
7568 /* Check for missed out final variable assignment */
7569 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7570 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7571
7572 if (no_body)
7573 restore_line_number_status (0);
7574 }
7575
7576 /* First, do the ordinary methods. */
7577 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7578 {
7145d9fe
TT
7579 /* Skip abstract or native methods -- but do handle native
7580 methods when generating JNI stubs. */
7581 if (METHOD_ABSTRACT (decl)
7582 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7583 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7584 continue;
7145d9fe
TT
7585
7586 if (METHOD_NATIVE (decl))
7587 {
7588 tree body = build_jni_stub (decl);
7589 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7590 }
7591
c2952b01
APB
7592 java_complete_expand_method (decl);
7593 }
7594
7595 /* If there is indeed a <clinit>, fully expand it now */
7596 if (clinit)
7597 {
c7303e41
APB
7598 /* Reset the final local variable assignment flags */
7599 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7600 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7601 /* Prevent the use of `this' inside <clinit> */
7602 ctxp->explicit_constructor_p = 1;
7603 java_complete_expand_method (clinit);
7604 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7605 /* Check for missed out static final variable assignment */
7606 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7607 && !CLASS_INTERFACE (class_decl))
7608 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7609 }
c2952b01 7610
165f37bc
APB
7611 /* We might have generated a class$ that we now want to expand */
7612 if (TYPE_DOT_CLASS (current_class))
7613 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7614
c2952b01
APB
7615 /* Now verify constructor circularity (stop after the first one we
7616 prove wrong.) */
7617 if (!CLASS_INTERFACE (class_decl))
7618 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7619 if (DECL_CONSTRUCTOR_P (decl)
7620 && verify_constructor_circularity (decl, decl))
7621 break;
7622
c7303e41
APB
7623 /* Final check on the initialization of final variables. */
7624 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7625 {
7626 check_final_variable_global_assignment_flag (current_class);
7627 /* If we have an interface, check for uninitialized fields. */
7628 if (CLASS_INTERFACE (class_decl))
7629 check_static_final_variable_assignment_flag (current_class);
7630 }
7631
c2952b01
APB
7632 /* Save the constant pool. We'll need to restore it later. */
7633 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7634}
7635
c2952b01
APB
7636/* Attempt to create <clinit>. Pre-expand static fields so they can be
7637 safely used in some other methods/constructors. */
e920ebc9 7638
c2952b01
APB
7639static tree
7640maybe_generate_pre_expand_clinit (class_type)
7641 tree class_type;
e920ebc9 7642{
c2952b01
APB
7643 tree current, mdecl;
7644
7645 if (!TYPE_CLINIT_STMT_LIST (class_type))
7646 return NULL_TREE;
e920ebc9 7647
c2952b01
APB
7648 /* Go through all static fields and pre expand them */
7649 for (current = TYPE_FIELDS (class_type); current;
7650 current = TREE_CHAIN (current))
7651 if (FIELD_STATIC (current))
7652 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7653
7654 /* Then build the <clinit> method */
7655 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7656 clinit_identifier_node, end_params_node);
7657 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7658 mdecl, NULL_TREE);
7659 start_artificial_method_body (mdecl);
7660
7661 /* We process the list of assignment we produced as the result of
7662 the declaration of initialized static field and add them as
7663 statement to the <clinit> method. */
7664 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7665 current = TREE_CHAIN (current))
e920ebc9 7666 {
9a7ab4b3 7667 tree stmt = current;
c2952b01
APB
7668 /* We build the assignment expression that will initialize the
7669 field to its value. There are strict rules on static
7670 initializers (8.5). FIXME */
98a52c2c 7671 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7672 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7673 java_method_add_stmt (mdecl, stmt);
7674 }
e920ebc9 7675
c2952b01
APB
7676 end_artificial_method_body (mdecl);
7677
92d83515
APB
7678 /* Now we want to place <clinit> as the last method (because we need
7679 it at least for interface so that it doesn't interfere with the
7680 dispatch table based lookup. */
7681 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7682 {
92d83515
APB
7683 current = TREE_CHAIN (TYPE_METHODS (class_type));
7684 TYPE_METHODS (class_type) = current;
c2952b01
APB
7685
7686 while (TREE_CHAIN (current))
7687 current = TREE_CHAIN (current);
92d83515 7688
c2952b01
APB
7689 TREE_CHAIN (current) = mdecl;
7690 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7691 }
c2952b01
APB
7692
7693 return mdecl;
e920ebc9
APB
7694}
7695
dba41d30
APB
7696/* Analyzes a method body and look for something that isn't a
7697 MODIFY_EXPR. */
7698
7699static int
7700analyze_clinit_body (bbody)
7701 tree bbody;
7702{
7703 while (bbody)
7704 switch (TREE_CODE (bbody))
7705 {
7706 case BLOCK:
7707 bbody = BLOCK_EXPR_BODY (bbody);
7708 break;
7709
7710 case EXPR_WITH_FILE_LOCATION:
7711 bbody = EXPR_WFL_NODE (bbody);
7712 break;
7713
7714 case COMPOUND_EXPR:
7715 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7716 return 1;
7717 bbody = TREE_OPERAND (bbody, 1);
7718 break;
7719
7720 case MODIFY_EXPR:
7721 bbody = NULL_TREE;
7722 break;
7723
7724 default:
7725 bbody = NULL_TREE;
7726 return 1;
7727 }
7728 return 0;
7729}
7730
7731
92d83515
APB
7732/* See whether we could get rid of <clinit>. Criteria are: all static
7733 final fields have constant initial values and the body of <clinit>
7734 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7735
7736static int
7737maybe_yank_clinit (mdecl)
7738 tree mdecl;
7739{
7740 tree type, current;
7741 tree fbody, bbody;
99eaf8d4 7742 int found = 0;
92d83515
APB
7743
7744 if (!DECL_CLINIT_P (mdecl))
7745 return 0;
f0f3a777
APB
7746
7747 /* If the body isn't empty, then we keep <clinit>. Note that if
7748 we're emitting classfiles, this isn't enough not to rule it
7749 out. */
92d83515 7750 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7751 bbody = BLOCK_EXPR_BODY (fbody);
7752 if (bbody && bbody != error_mark_node)
92d83515 7753 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7754 else
7755 return 0;
f0f3a777 7756 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7757 return 0;
7758
7759 type = DECL_CONTEXT (mdecl);
7760 current = TYPE_FIELDS (type);
7761
7762 for (current = (current ? TREE_CHAIN (current) : current);
7763 current; current = TREE_CHAIN (current))
f0f3a777
APB
7764 {
7765 tree f_init;
7766
7767 /* We're not interested in non static field */
7768 if (!FIELD_STATIC (current))
7769 continue;
7770
7771 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7772 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7773 should generated an empty <clinit> so that SUID are computed
7774 correctly. */
7775 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7776 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7777 break;
7778
7779 f_init = DECL_INITIAL (current);
7780 /* If we're emitting native code, we want static final fields to
7781 have constant initializers. If we don't meet these
7782 conditions, we keep <clinit> */
7783 if (!flag_emit_class_files
7784 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7785 break;
7786 /* If we're emitting bytecode, we want static fields to have
7787 constant initializers or no initializer. If we don't meet
7788 these conditions, we keep <clinit> */
7789 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7790 break;
7791 }
92d83515 7792
99eaf8d4
APB
7793 /* Now we analyze the method body and look for something that
7794 isn't a MODIFY_EXPR */
7795 if (bbody == empty_stmt_node)
dba41d30
APB
7796 found = 0;
7797 else
7798 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7799
7800 if (current || found)
92d83515
APB
7801 return 0;
7802
7803 /* Get rid of <clinit> in the class' list of methods */
7804 if (TYPE_METHODS (type) == mdecl)
7805 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7806 else
7807 for (current = TYPE_METHODS (type); current;
7808 current = TREE_CHAIN (current))
7809 if (TREE_CHAIN (current) == mdecl)
7810 {
7811 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7812 break;
7813 }
7814
7815 return 1;
7816}
7817
7818
e04a16fb
AG
7819/* Complete and expand a method. */
7820
7821static void
7822java_complete_expand_method (mdecl)
7823 tree mdecl;
7824{
92d83515
APB
7825 int yank_clinit = 0;
7826
c2952b01 7827 current_function_decl = mdecl;
22eed1e6
APB
7828 /* Fix constructors before expanding them */
7829 if (DECL_CONSTRUCTOR_P (mdecl))
7830 fix_constructors (mdecl);
e04a16fb 7831
22eed1e6 7832 /* Expand functions that have a body */
e04a16fb
AG
7833 if (DECL_FUNCTION_BODY (mdecl))
7834 {
9bbc7d9f
PB
7835 tree fbody = DECL_FUNCTION_BODY (mdecl);
7836 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7837 tree exception_copy = NULL_TREE;
e04a16fb 7838 expand_start_java_method (mdecl);
939d7216 7839 build_result_decl (mdecl);
e04a16fb
AG
7840
7841 current_this
7842 = (!METHOD_STATIC (mdecl) ?
7843 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7844
ce6e9147
APB
7845 /* Purge the `throws' list of unchecked exceptions. If we're
7846 doing xref, save a copy of the list and re-install it
7847 later. */
7848 if (flag_emit_xref)
7849 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7850
b9f7e36c
APB
7851 purge_unchecked_exceptions (mdecl);
7852
7853 /* Install exceptions thrown with `throws' */
7854 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7855
9bbc7d9f 7856 if (block_body != NULL_TREE)
bc3ca41b
PB
7857 {
7858 block_body = java_complete_tree (block_body);
c2952b01 7859
7145d9fe 7860 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7861 check_for_initialization (block_body);
f099f336 7862 ctxp->explicit_constructor_p = 0;
bc3ca41b 7863 }
e803d3b2 7864
9bbc7d9f 7865 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7866
c2952b01
APB
7867 /* If we saw a return but couldn't evaluate it properly, we'll
7868 have an error_mark_node here. */
7869 if (block_body != error_mark_node
7870 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7871 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7872 && !flag_emit_xref)
82371d41 7873 missing_return_error (current_function_decl);
7525cc04 7874
92d83515
APB
7875 /* Check wether we could just get rid of clinit, now the picture
7876 is complete. */
7877 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7878 complete_start_java_method (mdecl);
7879
e04a16fb 7880 /* Don't go any further if we've found error(s) during the
92d83515
APB
7881 expansion */
7882 if (!java_error_count && !yank_clinit)
e04a16fb 7883 source_end_java_method ();
22eed1e6
APB
7884 else
7885 {
92d83515
APB
7886 if (java_error_count)
7887 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7888 poplevel (1, 0, 1);
7889 }
b9f7e36c
APB
7890
7891 /* Pop the exceptions and sanity check */
7892 POP_EXCEPTIONS();
7893 if (currently_caught_type_list)
7894 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7895
7896 if (flag_emit_xref)
7897 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7898 }
7899}
7900
c2952b01
APB
7901\f
7902
7903/* This section of the code deals with accessing enclosing context
7904 fields either directly by using the relevant access to this$<n> or
7905 by invoking an access method crafted for that purpose. */
7906
7907/* Build the necessary access from an inner class to an outer
7908 class. This routine could be optimized to cache previous result
7909 (decl, current_class and returned access). When an access method
7910 needs to be generated, it always takes the form of a read. It might
7911 be later turned into a write by calling outer_field_access_fix. */
7912
7913static tree
7914build_outer_field_access (id, decl)
7915 tree id, decl;
7916{
7917 tree access = NULL_TREE;
7918 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7919 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7920
5e18f6d6
APB
7921 /* If the immediate enclosing context of the current class is the
7922 field decl's class or inherits from it; build the access as
7923 `this$<n>.<field>'. Note that we will break the `private' barrier
7924 if we're not emitting bytecodes. */
7925 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7926 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7927 {
7928 tree thisn = build_current_thisn (current_class);
7929 access = make_qualified_primary (build_wfl_node (thisn),
7930 id, EXPR_WFL_LINECOL (id));
7931 }
7932 /* Otherwise, generate access methods to outer this and access the
7933 field (either using an access method or by direct access.) */
7934 else
7935 {
7936 int lc = EXPR_WFL_LINECOL (id);
7937
7938 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7939 get a hold to the enclosing instance we need, and then we
c2952b01 7940 build the field access. */
5e18f6d6 7941 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7942
7943 /* If the field is private and we're generating bytecode, then
7944 we generate an access method */
7945 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7946 {
7947 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7948 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7949 name, access, NULL_TREE);
7950 }
7951 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7952 Once again we break the `private' access rule from a foreign
7953 class. */
7954 else
7955 access = make_qualified_primary (access, id, lc);
7956 }
7957 return resolve_expression_name (access, NULL);
7958}
7959
7960/* Return a non zero value if NODE describes an outer field inner
7961 access. */
7962
7963static int
7964outer_field_access_p (type, decl)
7965 tree type, decl;
7966{
7967 if (!INNER_CLASS_TYPE_P (type)
7968 || TREE_CODE (decl) != FIELD_DECL
7969 || DECL_CONTEXT (decl) == type)
7970 return 0;
ee5f86dc
APB
7971
7972 /* If the inner class extends the declaration context of the field
7973 we're try to acces, then this isn't an outer field access */
7974 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7975 return 0;
c2952b01
APB
7976
7977 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7978 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7979 {
7980 if (type == DECL_CONTEXT (decl))
7981 return 1;
5e18f6d6 7982
c2952b01 7983 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7984 {
7985 /* Before we give up, see whether the field is inherited from
7986 the enclosing context we're considering. */
7987 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7988 return 1;
7989 break;
7990 }
c2952b01
APB
7991 }
7992
7993 return 0;
7994}
7995
7996/* Return a non zero value if NODE represents an outer field inner
7997 access that was been already expanded. As a side effect, it returns
7998 the name of the field being accessed and the argument passed to the
7999 access function, suitable for a regeneration of the access method
8000 call if necessary. */
8001
8002static int
8003outer_field_expanded_access_p (node, name, arg_type, arg)
8004 tree node, *name, *arg_type, *arg;
8005{
8006 int identified = 0;
8007
8008 if (TREE_CODE (node) != CALL_EXPR)
8009 return 0;
8010
8011 /* Well, gcj generates slightly different tree nodes when compiling
8012 to native or bytecodes. It's the case for function calls. */
8013
8014 if (flag_emit_class_files
8015 && TREE_CODE (node) == CALL_EXPR
8016 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8017 identified = 1;
8018 else if (!flag_emit_class_files)
8019 {
8020 node = TREE_OPERAND (node, 0);
8021
8022 if (node && TREE_OPERAND (node, 0)
8023 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8024 {
8025 node = TREE_OPERAND (node, 0);
8026 if (TREE_OPERAND (node, 0)
8027 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8028 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8029 (DECL_NAME (TREE_OPERAND (node, 0)))))
8030 identified = 1;
8031 }
8032 }
8033
8034 if (identified && name && arg_type && arg)
8035 {
8036 tree argument = TREE_OPERAND (node, 1);
8037 *name = DECL_NAME (TREE_OPERAND (node, 0));
8038 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8039 *arg = TREE_VALUE (argument);
8040 }
8041 return identified;
8042}
8043
8044/* Detect in NODE an outer field read access from an inner class and
8045 transform it into a write with RHS as an argument. This function is
8046 called from the java_complete_lhs when an assignment to a LHS can
8047 be identified. */
8048
8049static tree
8050outer_field_access_fix (wfl, node, rhs)
8051 tree wfl, node, rhs;
8052{
8053 tree name, arg_type, arg;
8054
8055 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8056 {
8057 /* At any rate, check whether we're trying to assign a value to
8058 a final. */
8059 tree accessed = (JDECL_P (node) ? node :
8060 (TREE_CODE (node) == COMPONENT_REF ?
8061 TREE_OPERAND (node, 1) : node));
8062 if (check_final_assignment (accessed, wfl))
8063 return error_mark_node;
8064
8065 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8066 arg_type, name, arg, rhs);
8067 return java_complete_tree (node);
8068 }
8069 return NULL_TREE;
8070}
8071
8072/* Construct the expression that calls an access method:
8073 <type>.access$<n>(<arg1> [, <arg2>]);
8074
8075 ARG2 can be NULL and will be omitted in that case. It will denote a
8076 read access. */
8077
8078static tree
8079build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8080 int lc;
8081 tree type, access_method_name, arg1, arg2;
8082{
8083 tree args, cn, access;
8084
8085 args = arg1 ? arg1 :
8086 build_wfl_node (build_current_thisn (current_class));
8087 args = build_tree_list (NULL_TREE, args);
8088
8089 if (arg2)
8090 args = tree_cons (NULL_TREE, arg2, args);
8091
8092 access = build_method_invocation (build_wfl_node (access_method_name), args);
8093 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8094 return make_qualified_primary (cn, access, lc);
8095}
8096
8097static tree
8098build_new_access_id ()
8099{
8100 static int access_n_counter = 1;
8101 char buffer [128];
8102
8103 sprintf (buffer, "access$%d", access_n_counter++);
8104 return get_identifier (buffer);
8105}
8106
8107/* Create the static access functions for the outer field DECL. We define a
8108 read:
8109 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8110 return inst$.field;
8111 }
8112 and a write access:
8113 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8114 TREE_TYPE (<field>) value$) {
8115 return inst$.field = value$;
8116 }
8117 We should have a usage flags on the DECL so we can lazily turn the ones
8118 we're using for code generation. FIXME.
8119*/
8120
8121static tree
8122build_outer_field_access_methods (decl)
8123 tree decl;
8124{
8125 tree id, args, stmt, mdecl;
8126
c7303e41 8127 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
8128 return FIELD_INNER_ACCESS (decl);
8129
c7303e41
APB
8130 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8131
c2952b01
APB
8132 /* Create the identifier and a function named after it. */
8133 id = build_new_access_id ();
8134
8135 /* The identifier is marked as bearing the name of a generated write
8136 access function for outer field accessed from inner classes. */
8137 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8138
8139 /* Create the read access */
8140 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8141 TREE_CHAIN (args) = end_params_node;
8142 stmt = make_qualified_primary (build_wfl_node (inst_id),
8143 build_wfl_node (DECL_NAME (decl)), 0);
8144 stmt = build_return (0, stmt);
8145 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8146 TREE_TYPE (decl), id, args, stmt);
8147 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8148
c7303e41
APB
8149 /* Create the write access method. No write access for final variable */
8150 if (!FIELD_FINAL (decl))
8151 {
8152 args = build_tree_list (inst_id,
8153 build_pointer_type (DECL_CONTEXT (decl)));
8154 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8155 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8156 stmt = make_qualified_primary (build_wfl_node (inst_id),
8157 build_wfl_node (DECL_NAME (decl)), 0);
8158 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8159 build_wfl_node (wpv_id)));
8160 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8161 TREE_TYPE (decl), id,
8162 args, stmt);
8163 }
c2952b01 8164 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8165
8166 /* Return the access name */
8167 return FIELD_INNER_ACCESS (decl) = id;
8168}
8169
8170/* Build an field access method NAME. */
8171
8172static tree
8173build_outer_field_access_method (class, type, name, args, body)
8174 tree class, type, name, args, body;
8175{
8176 tree saved_current_function_decl, mdecl;
8177
8178 /* Create the method */
8179 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8180 fix_method_argument_names (args, mdecl);
8181 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8182
8183 /* Attach the method body. */
8184 saved_current_function_decl = current_function_decl;
8185 start_artificial_method_body (mdecl);
8186 java_method_add_stmt (mdecl, body);
8187 end_artificial_method_body (mdecl);
8188 current_function_decl = saved_current_function_decl;
8189
8190 return mdecl;
8191}
8192
8193\f
8194/* This section deals with building access function necessary for
8195 certain kinds of method invocation from inner classes. */
8196
8197static tree
8198build_outer_method_access_method (decl)
8199 tree decl;
8200{
8201 tree saved_current_function_decl, mdecl;
8202 tree args = NULL_TREE, call_args = NULL_TREE;
8203 tree carg, id, body, class;
8204 char buffer [80];
8205 int parm_id_count = 0;
8206
8207 /* Test this abort with an access to a private field */
8208 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8209 abort ();
8210
8211 /* Check the cache first */
8212 if (DECL_FUNCTION_INNER_ACCESS (decl))
8213 return DECL_FUNCTION_INNER_ACCESS (decl);
8214
8215 class = DECL_CONTEXT (decl);
8216
8217 /* Obtain an access identifier and mark it */
8218 id = build_new_access_id ();
8219 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8220
c2952b01
APB
8221 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8222 /* Create the arguments, as much as the original */
8223 for (; carg && carg != end_params_node;
8224 carg = TREE_CHAIN (carg))
8225 {
8226 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8227 args = chainon (args, build_tree_list (get_identifier (buffer),
8228 TREE_VALUE (carg)));
8229 }
8230 args = chainon (args, end_params_node);
8231
8232 /* Create the method */
8233 mdecl = create_artificial_method (class, ACC_STATIC,
8234 TREE_TYPE (TREE_TYPE (decl)), id, args);
8235 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8236 /* There is a potential bug here. We should be able to use
8237 fix_method_argument_names, but then arg names get mixed up and
8238 eventually a constructor will have its this$0 altered and the
8239 outer context won't be assignment properly. The test case is
8240 stub.java FIXME */
8241 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8242
8243 /* Attach the method body. */
8244 saved_current_function_decl = current_function_decl;
8245 start_artificial_method_body (mdecl);
8246
8247 /* The actual method invocation uses the same args. When invoking a
8248 static methods that way, we don't want to skip the first
8249 argument. */
8250 carg = args;
8251 if (!METHOD_STATIC (decl))
8252 carg = TREE_CHAIN (carg);
8253 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8254 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8255 call_args);
8256
8257 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8258 call_args);
8259 if (!METHOD_STATIC (decl))
8260 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8261 body, 0);
8262 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8263 body = build_return (0, body);
8264 java_method_add_stmt (mdecl,body);
8265 end_artificial_method_body (mdecl);
8266 current_function_decl = saved_current_function_decl;
c2952b01
APB
8267
8268 /* Back tag the access function so it know what it accesses */
8269 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8270
8271 /* Tag the current method so it knows it has an access generated */
8272 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8273}
8274
8275\f
8276/* This section of the code deals with building expressions to access
8277 the enclosing instance of an inner class. The enclosing instance is
8278 kept in a generated field called this$<n>, with <n> being the
8279 inner class nesting level (starting from 0.) */
8280
dba41d30
APB
8281/* Build an access to a given this$<n>, always chaining access call to
8282 others. Access methods to this$<n> are build on the fly if
8283 necessary. This CAN'T be used to solely access this$<n-1> from
8284 this$<n> (which alway yield to special cases and optimization, see
8285 for example build_outer_field_access). */
c2952b01
APB
8286
8287static tree
8288build_access_to_thisn (from, to, lc)
8289 tree from, to;
8290 int lc;
8291{
8292 tree access = NULL_TREE;
8293
8294 while (from != to)
8295 {
c2952b01 8296 if (!access)
dba41d30
APB
8297 {
8298 access = build_current_thisn (from);
8299 access = build_wfl_node (access);
8300 }
8301 else
c2952b01 8302 {
dba41d30
APB
8303 tree access0_wfl, cn;
8304
8305 maybe_build_thisn_access_method (from);
8306 access0_wfl = build_wfl_node (access0_identifier_node);
8307 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8308 EXPR_WFL_LINECOL (access0_wfl) = lc;
8309 access = build_tree_list (NULL_TREE, access);
8310 access = build_method_invocation (access0_wfl, access);
8311 access = make_qualified_primary (cn, access, lc);
c2952b01 8312 }
5e18f6d6
APB
8313
8314 /* if FROM isn't an inter class, that's fine, we've done
8315 enough. What we're looking for can be accessed from there. */
8316 from = DECL_CONTEXT (TYPE_NAME (from));
8317 if (!from)
8318 break;
8319 from = TREE_TYPE (from);
c2952b01
APB
8320 }
8321 return access;
8322}
8323
8324/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8325 is returned if nothing needs to be generated. Otherwise, the method
152de068 8326 generated and a method decl is returned.
c2952b01
APB
8327
8328 NOTE: These generated methods should be declared in a class file
8329 attribute so that they can't be referred to directly. */
8330
8331static tree
8332maybe_build_thisn_access_method (type)
8333 tree type;
8334{
8335 tree mdecl, args, stmt, rtype;
8336 tree saved_current_function_decl;
8337
8338 /* If TYPE is a top-level class, no access method is required.
8339 If there already is such an access method, bail out. */
ee5f86dc 8340 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8341 return NULL_TREE;
8342
8343 /* We generate the method. The method looks like:
8344 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8345 */
c2952b01
APB
8346 args = build_tree_list (inst_id, build_pointer_type (type));
8347 TREE_CHAIN (args) = end_params_node;
8348 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8349 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8350 access0_identifier_node, args);
8351 fix_method_argument_names (args, mdecl);
8352 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8353 stmt = build_current_thisn (type);
8354 stmt = make_qualified_primary (build_wfl_node (inst_id),
8355 build_wfl_node (stmt), 0);
8356 stmt = build_return (0, stmt);
8357
8358 saved_current_function_decl = current_function_decl;
8359 start_artificial_method_body (mdecl);
8360 java_method_add_stmt (mdecl, stmt);
8361 end_artificial_method_body (mdecl);
8362 current_function_decl = saved_current_function_decl;
c2952b01
APB
8363
8364 CLASS_ACCESS0_GENERATED_P (type) = 1;
8365
8366 return mdecl;
8367}
8368
8369/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8370 the first level of innerclassing. this$1 for the next one, etc...
8371 This function can be invoked with TYPE to NULL, available and then
8372 has to count the parser context. */
8373
8374static tree
8375build_current_thisn (type)
8376 tree type;
8377{
8378 static int saved_i = -1;
8379 static tree saved_thisn = NULL_TREE;
19e223db
MM
8380 static tree saved_type = NULL_TREE;
8381 static int saved_type_i = 0;
8382 static int initialized_p;
c2952b01
APB
8383 tree decl;
8384 char buffer [80];
8385 int i = 0;
8386
19e223db
MM
8387 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8388 if (!initialized_p)
c2952b01 8389 {
19e223db
MM
8390 ggc_add_tree_root (&saved_thisn, 1);
8391 ggc_add_tree_root (&saved_type, 1);
8392 initialized_p = 1;
8393 }
c2952b01 8394
19e223db
MM
8395 if (type)
8396 {
c2952b01
APB
8397 if (type == saved_type)
8398 i = saved_type_i;
8399 else
8400 {
8401 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8402 decl; decl = DECL_CONTEXT (decl), i++)
8403 ;
8404
8405 saved_type = type;
8406 saved_type_i = i;
8407 }
8408 }
8409 else
8410 i = list_length (GET_CPC_LIST ())-2;
8411
8412 if (i == saved_i)
8413 return saved_thisn;
8414
8415 sprintf (buffer, "this$%d", i);
8416 saved_i = i;
8417 saved_thisn = get_identifier (buffer);
8418 return saved_thisn;
8419}
8420
8421/* Return the assignement to the hidden enclosing context `this$<n>'
8422 by the second incoming parameter to the innerclass constructor. The
8423 form used is `this.this$<n> = this$<n>;'. */
8424
8425static tree
8426build_thisn_assign ()
8427{
8428 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8429 {
8430 tree thisn = build_current_thisn (current_class);
8431 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8432 build_wfl_node (thisn), 0);
8433 tree rhs = build_wfl_node (thisn);
8434 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8435 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8436 }
8437 return NULL_TREE;
8438}
8439
8440\f
165f37bc
APB
8441/* Building the synthetic `class$' used to implement the `.class' 1.1
8442 extension for non primitive types. This method looks like:
8443
8444 static Class class$(String type) throws NoClassDefFoundError
8445 {
8446 try {return (java.lang.Class.forName (String));}
8447 catch (ClassNotFoundException e) {
8448 throw new NoClassDefFoundError(e.getMessage());}
8449 } */
8450
8451static tree
8452build_dot_class_method (class)
8453 tree class;
8454{
8455#define BWF(S) build_wfl_node (get_identifier ((S)))
8456#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8457 tree args, tmp, saved_current_function_decl, mdecl;
8458 tree stmt, throw_stmt, catch, catch_block, try_block;
8459 tree catch_clause_param;
8460 tree class_not_found_exception, no_class_def_found_error;
8461
8462 static tree get_message_wfl, type_parm_wfl;
8463
8464 if (!get_message_wfl)
8465 {
8466 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8467 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8468 ggc_add_tree_root (&get_message_wfl, 1);
8469 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8470 }
8471
8472 /* Build the arguments */
8473 args = build_tree_list (get_identifier ("type$"),
8474 build_pointer_type (string_type_node));
8475 TREE_CHAIN (args) = end_params_node;
8476
8477 /* Build the qualified name java.lang.Class.forName */
8478 tmp = MQN (MQN (MQN (BWF ("java"),
8479 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8480
8481 /* For things we have to catch and throw */
8482 class_not_found_exception =
8483 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8484 no_class_def_found_error =
8485 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8486 load_class (class_not_found_exception, 1);
8487 load_class (no_class_def_found_error, 1);
8488
8489 /* Create the "class$" function */
8490 mdecl = create_artificial_method (class, ACC_STATIC,
8491 build_pointer_type (class_type_node),
8492 get_identifier ("class$"), args);
8493 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8494 no_class_def_found_error);
8495
8496 /* We start by building the try block. We need to build:
8497 return (java.lang.Class.forName (type)); */
8498 stmt = build_method_invocation (tmp,
8499 build_tree_list (NULL_TREE, type_parm_wfl));
8500 stmt = build_return (0, stmt);
8501 /* Put it in a block. That's the try block */
8502 try_block = build_expr_block (stmt, NULL_TREE);
8503
8504 /* Now onto the catch block. We start by building the expression
8505 throwing a new exception:
8506 throw new NoClassDefFoundError (_.getMessage); */
8507 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8508 get_message_wfl, 0);
8509 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8510
8511 /* Build new NoClassDefFoundError (_.getMessage) */
8512 throw_stmt = build_new_invocation
8513 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8514 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8515
8516 /* Build the throw, (it's too early to use BUILD_THROW) */
8517 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8518
8519 /* Build the catch block to encapsulate all this. We begin by
8520 building an decl for the catch clause parameter and link it to
8521 newly created block, the catch block. */
8522 catch_clause_param =
8523 build_decl (VAR_DECL, wpv_id,
8524 build_pointer_type (class_not_found_exception));
8525 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8526
8527 /* We initialize the variable with the exception handler. */
8528 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8529 soft_exceptioninfo_call_node);
8530 add_stmt_to_block (catch_block, NULL_TREE, catch);
8531
8532 /* We add the statement throwing the new exception */
8533 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8534
8535 /* Build a catch expression for all this */
8536 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8537
8538 /* Build the try/catch sequence */
8539 stmt = build_try_statement (0, try_block, catch_block);
8540
8541 fix_method_argument_names (args, mdecl);
8542 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8543 saved_current_function_decl = current_function_decl;
8544 start_artificial_method_body (mdecl);
8545 java_method_add_stmt (mdecl, stmt);
8546 end_artificial_method_body (mdecl);
8547 current_function_decl = saved_current_function_decl;
8548 TYPE_DOT_CLASS (class) = mdecl;
8549
8550 return mdecl;
8551}
8552
8553static tree
f0f3a777
APB
8554build_dot_class_method_invocation (type)
8555 tree type;
165f37bc 8556{
f0f3a777
APB
8557 tree sig_id, s;
8558
8559 if (TYPE_ARRAY_P (type))
8560 sig_id = build_java_signature (type);
8561 else
8562 sig_id = DECL_NAME (TYPE_NAME (type));
8563
1f8f4a0b
MM
8564 s = build_string (IDENTIFIER_LENGTH (sig_id),
8565 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8566 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8567 build_tree_list (NULL_TREE, s));
8568}
8569
c2952b01
APB
8570/* This section of the code deals with constructor. */
8571
22eed1e6
APB
8572/* Craft a body for default constructor. Patch existing constructor
8573 bodies with call to super() and field initialization statements if
8574 necessary. */
8575
8576static void
8577fix_constructors (mdecl)
8578 tree mdecl;
8579{
8580 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8581 tree thisn_assign, compound = NULL_TREE;
8582 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8583
22eed1e6
APB
8584 if (!body)
8585 {
22eed1e6
APB
8586 /* It is an error for the compiler to generate a default
8587 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8588 takes no argument, or the same args for an anonymous class */
8589 if (verify_constructor_super (mdecl))
22eed1e6 8590 {
c2952b01
APB
8591 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8592 tree save = DECL_NAME (mdecl);
49f48c71 8593 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8594 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8595 parse_error_context
c2952b01
APB
8596 (lookup_cl (TYPE_NAME (class_type)),
8597 "No constructor matching `%s' found in class `%s'",
8598 lang_printable_name (mdecl, 0), n);
8599 DECL_NAME (mdecl) = save;
22eed1e6
APB
8600 }
8601
c2952b01
APB
8602 /* The constructor body must be crafted by hand. It's the
8603 constructor we defined when we realize we didn't have the
8604 CLASSNAME() constructor */
22eed1e6
APB
8605 start_artificial_method_body (mdecl);
8606
f0f3a777
APB
8607 /* Insert an assignment to the this$<n> hidden field, if
8608 necessary */
8609 if ((thisn_assign = build_thisn_assign ()))
8610 java_method_add_stmt (mdecl, thisn_assign);
8611
22eed1e6
APB
8612 /* We don't generate a super constructor invocation if we're
8613 compiling java.lang.Object. build_super_invocation takes care
8614 of that. */
e920ebc9 8615 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8616
c2952b01
APB
8617 /* Insert the instance initializer block right here, after the
8618 super invocation. */
8619 add_instance_initializer (mdecl);
8620
22eed1e6
APB
8621 end_artificial_method_body (mdecl);
8622 }
8623 /* Search for an explicit constructor invocation */
8624 else
8625 {
8626 int found = 0;
8627 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8628
8629 while (body)
8630 switch (TREE_CODE (body))
8631 {
8632 case CALL_EXPR:
8633 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8634 body = NULL_TREE;
8635 break;
8636 case COMPOUND_EXPR:
8637 case EXPR_WITH_FILE_LOCATION:
8638 body = TREE_OPERAND (body, 0);
8639 break;
8640 case BLOCK:
8641 body = BLOCK_EXPR_BODY (body);
8642 break;
8643 default:
8644 found = 0;
8645 body = NULL_TREE;
8646 }
8647 /* The constructor is missing an invocation of super() */
8648 if (!found)
8649 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8650 build_super_invocation (mdecl));
22eed1e6 8651
c2952b01
APB
8652 /* Generate the assignment to this$<n>, if necessary */
8653 if ((thisn_assign = build_thisn_assign ()))
8654 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8655
f0f3a777
APB
8656 /* Insert the instance initializer block right here, after the
8657 super invocation. */
8658 add_instance_initializer (mdecl);
8659
22eed1e6
APB
8660 /* Fix the constructor main block if we're adding extra stmts */
8661 if (compound)
8662 {
8663 compound = add_stmt_to_compound (compound, NULL_TREE,
8664 BLOCK_EXPR_BODY (main_block));
8665 BLOCK_EXPR_BODY (main_block) = compound;
8666 }
8667 }
8668}
8669
8670/* Browse constructors in the super class, searching for a constructor
8671 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8672 otherwise. If the current class is an anonymous inner class, look
8673 for something that has the same signature. */
22eed1e6
APB
8674
8675static int
c2952b01
APB
8676verify_constructor_super (mdecl)
8677 tree mdecl;
22eed1e6
APB
8678{
8679 tree class = CLASSTYPE_SUPER (current_class);
152de068 8680 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8681 tree sdecl;
8682
22eed1e6
APB
8683 if (!class)
8684 return 0;
8685
c2952b01 8686 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8687 {
c2952b01
APB
8688 tree mdecl_arg_type;
8689 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8690 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8691 if (DECL_CONSTRUCTOR_P (sdecl))
8692 {
cf1b2274 8693 tree m_arg_type;
152de068
APB
8694 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8695 if (super_inner)
8696 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8697 for (m_arg_type = mdecl_arg_type;
8698 (arg_type != end_params_node
8699 && m_arg_type != end_params_node);
c2952b01 8700 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8701 m_arg_type = TREE_CHAIN (m_arg_type))
8702 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8703 break;
8704
cf1b2274 8705 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8706 return 0;
8707 }
8708 }
8709 else
8710 {
8711 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8712 {
152de068
APB
8713 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8714 if (super_inner)
8715 arg = TREE_CHAIN (arg);
8716 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8717 return 0;
8718 }
8719 }
8720 return 1;
8721}
8722
22eed1e6 8723/* Generate code for all context remembered for code generation. */
b351b287
APB
8724
8725void
8726java_expand_classes ()
8727{
5423609c 8728 int save_error_count = 0;
c2952b01
APB
8729 static struct parser_ctxt *saved_ctxp = NULL;
8730
23a79c61
APB
8731 java_parse_abort_on_error ();
8732 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8733 return;
8734 java_layout_classes ();
8735 java_parse_abort_on_error ();
8736
c2952b01 8737 saved_ctxp = ctxp_for_generation;
b351b287
APB
8738 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8739 {
8740 ctxp = ctxp_for_generation;
8741 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8742 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8743 java_parse_abort_on_error ();
8744 }
c2952b01
APB
8745
8746 /* Find anonymous classes and expand their constructor, now they
8747 have been fixed. */
8748 for (ctxp_for_generation = saved_ctxp;
8749 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8750 {
8751 tree current;
8752 ctxp = ctxp_for_generation;
8753 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8754 {
8755 current_class = TREE_TYPE (current);
8756 if (ANONYMOUS_CLASS_P (current_class))
8757 {
8758 tree d;
8759 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8760 {
8761 if (DECL_CONSTRUCTOR_P (d))
8762 {
8763 restore_line_number_status (1);
8764 reset_method_name (d);
8765 java_complete_expand_method (d);
8766 restore_line_number_status (0);
8767 break; /* We now there are no other ones */
8768 }
8769 }
8770 }
8771 }
8772 }
8773
8774 /* If we've found error at that stage, don't try to generate
8775 anything, unless we're emitting xrefs or checking the syntax only
8776 (but not using -fsyntax-only for the purpose of generating
8777 bytecode. */
8778 if (java_error_count && !flag_emit_xref
8779 && (!flag_syntax_only && !flag_emit_class_files))
8780 return;
8781
8782 /* Now things are stable, go for generation of the class data. */
8783 for (ctxp_for_generation = saved_ctxp;
8784 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8785 {
8786 tree current;
8787 ctxp = ctxp_for_generation;
8788 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8789 {
8790 current_class = TREE_TYPE (current);
8791 outgoing_cpool = TYPE_CPOOL (current_class);
8792 if (flag_emit_class_files)
8793 write_classfile (current_class);
8794 if (flag_emit_xref)
8795 expand_xref (current_class);
8796 else if (! flag_syntax_only)
8797 finish_class ();
8798 }
8799 }
b351b287
APB
8800}
8801
e04a16fb
AG
8802/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8803 a tree list node containing RIGHT. Fore coming RIGHTs will be
8804 chained to this hook. LOCATION contains the location of the
8805 separating `.' operator. */
8806
8807static tree
8808make_qualified_primary (primary, right, location)
8809 tree primary, right;
8810 int location;
8811{
8812 tree wfl;
8813
c2952b01 8814 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8815 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8816 else
8817 {
8818 wfl = primary;
c2952b01
APB
8819 /* If wfl wasn't qualified, we build a first anchor */
8820 if (!EXPR_WFL_QUALIFICATION (wfl))
8821 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8822 }
8823
c2952b01 8824 /* And chain them */
e04a16fb
AG
8825 EXPR_WFL_LINECOL (right) = location;
8826 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8827 PRIMARY_P (wfl) = 1;
8828 return wfl;
8829}
8830
8831/* Simple merge of two name separated by a `.' */
8832
8833static tree
8834merge_qualified_name (left, right)
8835 tree left, right;
8836{
8837 tree node;
c2952b01
APB
8838 if (!left && !right)
8839 return NULL_TREE;
8840
8841 if (!left)
8842 return right;
8843
8844 if (!right)
8845 return left;
8846
e04a16fb
AG
8847 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8848 IDENTIFIER_LENGTH (left));
8849 obstack_1grow (&temporary_obstack, '.');
8850 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8851 IDENTIFIER_LENGTH (right));
8852 node = get_identifier (obstack_base (&temporary_obstack));
8853 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8854 QUALIFIED_P (node) = 1;
8855 return node;
8856}
8857
8858/* Merge the two parts of a qualified name into LEFT. Set the
8859 location information of the resulting node to LOCATION, usually
8860 inherited from the location information of the `.' operator. */
8861
8862static tree
8863make_qualified_name (left, right, location)
8864 tree left, right;
8865 int location;
8866{
bc3ca41b
PB
8867#ifdef USE_COMPONENT_REF
8868 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8869 EXPR_WFL_LINECOL (node) = location;
8870 return node;
8871#else
e04a16fb
AG
8872 tree left_id = EXPR_WFL_NODE (left);
8873 tree right_id = EXPR_WFL_NODE (right);
8874 tree wfl, merge;
8875
8876 merge = merge_qualified_name (left_id, right_id);
8877
8878 /* Left wasn't qualified and is now qualified */
8879 if (!QUALIFIED_P (left_id))
8880 {
8881 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8882 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8883 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8884 }
8885
8886 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8887 EXPR_WFL_LINECOL (wfl) = location;
8888 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8889
8890 EXPR_WFL_NODE (left) = merge;
8891 return left;
bc3ca41b 8892#endif
e04a16fb
AG
8893}
8894
8895/* Extract the last identifier component of the qualified in WFL. The
8896 last identifier is removed from the linked list */
8897
8898static tree
8899cut_identifier_in_qualified (wfl)
8900 tree wfl;
8901{
8902 tree q;
8903 tree previous = NULL_TREE;
8904 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8905 if (!TREE_CHAIN (q))
8906 {
8907 if (!previous)
781b0558 8908 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8909 TREE_CHAIN (previous) = NULL_TREE;
8910 return TREE_PURPOSE (q);
8911 }
8912}
8913
8914/* Resolve the expression name NAME. Return its decl. */
8915
8916static tree
5e942c50 8917resolve_expression_name (id, orig)
e04a16fb 8918 tree id;
5e942c50 8919 tree *orig;
e04a16fb
AG
8920{
8921 tree name = EXPR_WFL_NODE (id);
8922 tree decl;
8923
8924 /* 6.5.5.1: Simple expression names */
8925 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8926 {
8927 /* 15.13.1: NAME can appear within the scope of a local variable
8928 declaration */
8929 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8930 return decl;
8931
8932 /* 15.13.1: NAME can appear within a class declaration */
8933 else
8934 {
8935 decl = lookup_field_wrapper (current_class, name);
8936 if (decl)
8937 {
c2952b01 8938 tree access = NULL_TREE;
e04a16fb 8939 int fs = FIELD_STATIC (decl);
f2760b27
APB
8940
8941 /* If we're accessing an outer scope local alias, make
8942 sure we change the name of the field we're going to
8943 build access to. */
8944 if (FIELD_LOCAL_ALIAS_USED (decl))
8945 name = DECL_NAME (decl);
8946
e04a16fb
AG
8947 /* Instance variable (8.3.1.1) can't appear within
8948 static method, static initializer or initializer for
8949 a static variable. */
8950 if (!fs && METHOD_STATIC (current_function_decl))
8951 {
7f10c2e2 8952 static_ref_err (id, name, current_class);
e04a16fb
AG
8953 return error_mark_node;
8954 }
22eed1e6
APB
8955 /* Instance variables can't appear as an argument of
8956 an explicit constructor invocation */
8957 if (!fs && ctxp->explicit_constructor_p)
8958 {
8959 parse_error_context
781b0558 8960 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8961 return error_mark_node;
8962 }
5e942c50 8963
c2952b01
APB
8964 /* If we're processing an inner class and we're trying
8965 to access a field belonging to an outer class, build
8966 the access to the field */
8967 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8968 {
8969 if (CLASS_STATIC (TYPE_NAME (current_class)))
8970 {
8971 static_ref_err (id, DECL_NAME (decl), current_class);
8972 return error_mark_node;
8973 }
8974 return build_outer_field_access (id, decl);
8975 }
c2952b01 8976
5e942c50 8977 /* Otherwise build what it takes to access the field */
c2952b01
APB
8978 access = build_field_ref ((fs ? NULL_TREE : current_this),
8979 DECL_CONTEXT (decl), name);
e8fc7396 8980 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8981 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8982 /* We may be asked to save the real field access node */
8983 if (orig)
c2952b01 8984 *orig = access;
5e942c50 8985 /* And we return what we got */
c2952b01 8986 return access;
e04a16fb
AG
8987 }
8988 /* Fall down to error report on undefined variable */
8989 }
8990 }
8991 /* 6.5.5.2 Qualified Expression Names */
8992 else
8993 {
5e942c50
APB
8994 if (orig)
8995 *orig = NULL_TREE;
e04a16fb
AG
8996 qualify_ambiguous_name (id);
8997 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8998 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8999 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
9000 }
9001
9002 /* We've got an error here */
9003 parse_error_context (id, "Undefined variable `%s'",
9004 IDENTIFIER_POINTER (name));
9005
9006 return error_mark_node;
9007}
9008
7f10c2e2
APB
9009static void
9010static_ref_err (wfl, field_id, class_type)
9011 tree wfl, field_id, class_type;
9012{
9013 parse_error_context
9014 (wfl,
9015 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9016 IDENTIFIER_POINTER (field_id),
9017 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9018}
9019
e04a16fb
AG
9020/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
9021 We return something suitable to generate the field access. We also
9022 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9023 recipient's address can be null. */
9024
9025static tree
9026resolve_field_access (qual_wfl, field_decl, field_type)
9027 tree qual_wfl;
9028 tree *field_decl, *field_type;
9029{
9030 int is_static = 0;
9031 tree field_ref;
9032 tree decl, where_found, type_found;
9033
9034 if (resolve_qualified_expression_name (qual_wfl, &decl,
9035 &where_found, &type_found))
9036 return error_mark_node;
9037
9038 /* Resolve the LENGTH field of an array here */
9a7ab4b3 9039 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 9040 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 9041 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
9042 {
9043 tree length = build_java_array_length_access (where_found);
9044 field_ref =
9045 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
9046
9047 /* In case we're dealing with a static array, we need to
9048 initialize its class before the array length can be fetched.
9049 It's also a good time to create a DECL_RTL for the field if
9050 none already exists, otherwise if the field was declared in a
9051 class found in an external file and hasn't been (and won't
9052 be) accessed for its value, none will be created. */
9053 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9054 {
9055 build_static_field_ref (where_found);
9056 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9057 }
e04a16fb
AG
9058 }
9059 /* We might have been trying to resolve field.method(). In which
9060 case, the resolution is over and decl is the answer */
34f4db93 9061 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 9062 field_ref = decl;
34f4db93 9063 else if (JDECL_P (decl))
e04a16fb 9064 {
5e942c50
APB
9065 int static_final_found = 0;
9066 if (!type_found)
9067 type_found = DECL_CONTEXT (decl);
34f4db93 9068 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 9069 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 9070 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 9071 && DECL_INITIAL (decl))
5e942c50 9072 {
0c2b8145
APB
9073 /* When called on a FIELD_DECL of the right (primitive)
9074 type, java_complete_tree will try to substitue the decl
9075 for it's initial value. */
70541f45 9076 field_ref = java_complete_tree (decl);
5e942c50
APB
9077 static_final_found = 1;
9078 }
9079 else
7f10c2e2
APB
9080 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9081 NULL_TREE : where_found),
5e942c50 9082 type_found, DECL_NAME (decl));
e04a16fb
AG
9083 if (field_ref == error_mark_node)
9084 return error_mark_node;
e8fc7396
APB
9085 if (is_static && !static_final_found
9086 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 9087 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
9088 }
9089 else
9090 field_ref = decl;
9091
9092 if (field_decl)
9093 *field_decl = decl;
9094 if (field_type)
c877974e
APB
9095 *field_type = (QUAL_DECL_TYPE (decl) ?
9096 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
9097 return field_ref;
9098}
9099
e28cd97b
APB
9100/* If NODE is an access to f static field, strip out the class
9101 initialization part and return the field decl, otherwise, return
9102 NODE. */
9103
9104static tree
9105strip_out_static_field_access_decl (node)
9106 tree node;
9107{
9108 if (TREE_CODE (node) == COMPOUND_EXPR)
9109 {
9110 tree op1 = TREE_OPERAND (node, 1);
9111 if (TREE_CODE (op1) == COMPOUND_EXPR)
9112 {
9113 tree call = TREE_OPERAND (op1, 0);
9114 if (TREE_CODE (call) == CALL_EXPR
9115 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9116 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9117 == soft_initclass_node)
9118 return TREE_OPERAND (op1, 1);
9119 }
2f11d407
TT
9120 else if (JDECL_P (op1))
9121 return op1;
e28cd97b
APB
9122 }
9123 return node;
9124}
9125
e04a16fb
AG
9126/* 6.5.5.2: Qualified Expression Names */
9127
9128static int
9129resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9130 tree wfl;
9131 tree *found_decl, *type_found, *where_found;
9132{
9133 int from_type = 0; /* Field search initiated from a type */
c2952b01 9134 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9135 int previous_call_static = 0;
9136 int is_static;
9137 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9138 /* For certain for of inner class instantiation */
9139 tree saved_current, saved_this;
9140#define RESTORE_THIS_AND_CURRENT_CLASS \
9141 { current_class = saved_current; current_this = saved_this;}
9142
c877974e 9143 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9144
9145 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9146 {
9147 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9148 tree ret_decl; /* for EH checking */
9149 int location; /* for EH checking */
e04a16fb
AG
9150
9151 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9152 switch (TREE_CODE (qual_wfl))
9153 {
9154 case CALL_EXPR:
b67d701b 9155 case NEW_CLASS_EXPR:
e04a16fb
AG
9156 /* If the access to the function call is a non static field,
9157 build the code to access it. */
34f4db93 9158 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9159 {
ac825856
APB
9160 decl = maybe_access_field (decl, *where_found,
9161 DECL_CONTEXT (decl));
e04a16fb
AG
9162 if (decl == error_mark_node)
9163 return 1;
9164 }
c2952b01 9165
e04a16fb
AG
9166 /* And code for the function call */
9167 if (complete_function_arguments (qual_wfl))
9168 return 1;
c2952b01
APB
9169
9170 /* We might have to setup a new current class and a new this
9171 for the search of an inner class, relative to the type of
9172 a expression resolved as `decl'. The current values are
9173 saved and restored shortly after */
9174 saved_current = current_class;
9175 saved_this = current_this;
dba41d30
APB
9176 if (decl
9177 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9178 || from_qualified_this))
c2952b01 9179 {
dba41d30
APB
9180 /* If we still have `from_qualified_this', we have the form
9181 <T>.this.f() and we need to build <T>.this */
9182 if (from_qualified_this)
9183 {
9184 decl = build_access_to_thisn (current_class, type, 0);
9185 decl = java_complete_tree (decl);
9186 type = TREE_TYPE (TREE_TYPE (decl));
9187 }
c2952b01
APB
9188 current_class = type;
9189 current_this = decl;
dba41d30 9190 from_qualified_this = 0;
c2952b01
APB
9191 }
9192
89e09b9a
PB
9193 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9194 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9195 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9196 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9197 *where_found = patch_method_invocation (qual_wfl, decl, type,
9198 &is_static, &ret_decl);
e04a16fb 9199 if (*where_found == error_mark_node)
c2952b01
APB
9200 {
9201 RESTORE_THIS_AND_CURRENT_CLASS;
9202 return 1;
9203 }
e04a16fb
AG
9204 *type_found = type = QUAL_DECL_TYPE (*where_found);
9205
c2952b01
APB
9206 /* If we're creating an inner class instance, check for that
9207 an enclosing instance is in scope */
9208 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9209 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9210 {
9211 parse_error_context
165f37bc
APB
9212 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9213 lang_printable_name (type, 0),
9214 (!current_this ? "" :
9215 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9216 RESTORE_THIS_AND_CURRENT_CLASS;
9217 return 1;
9218 }
9219
9220 /* In case we had to change then to resolve a inner class
9221 instantiation using a primary qualified by a `new' */
9222 RESTORE_THIS_AND_CURRENT_CLASS;
9223
34d4df06
APB
9224 /* EH check. No check on access$<n> functions */
9225 if (location
9226 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9227 (DECL_NAME (current_function_decl)))
7705e9db
APB
9228 check_thrown_exceptions (location, ret_decl);
9229
e04a16fb
AG
9230 /* If the previous call was static and this one is too,
9231 build a compound expression to hold the two (because in
9232 that case, previous function calls aren't transported as
9233 forcoming function's argument. */
9234 if (previous_call_static && is_static)
9235 {
9236 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9237 TREE_SIDE_EFFECTS (decl) = 1;
9238 }
9239 else
9240 {
9241 previous_call_static = is_static;
9242 decl = *where_found;
9243 }
c2952b01 9244 from_type = 0;
e04a16fb
AG
9245 continue;
9246
d8fccff5 9247 case NEW_ARRAY_EXPR:
c2952b01 9248 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9249 *where_found = decl = java_complete_tree (qual_wfl);
9250 if (decl == error_mark_node)
9251 return 1;
9252 *type_found = type = QUAL_DECL_TYPE (decl);
9253 CLASS_LOADED_P (type) = 1;
9254 continue;
9255
e04a16fb
AG
9256 case CONVERT_EXPR:
9257 *where_found = decl = java_complete_tree (qual_wfl);
9258 if (decl == error_mark_node)
9259 return 1;
9260 *type_found = type = QUAL_DECL_TYPE (decl);
9261 from_cast = 1;
9262 continue;
9263
22eed1e6 9264 case CONDITIONAL_EXPR:
5e942c50 9265 case STRING_CST:
ac22f9cb 9266 case MODIFY_EXPR:
22eed1e6
APB
9267 *where_found = decl = java_complete_tree (qual_wfl);
9268 if (decl == error_mark_node)
9269 return 1;
9270 *type_found = type = QUAL_DECL_TYPE (decl);
9271 continue;
9272
e04a16fb
AG
9273 case ARRAY_REF:
9274 /* If the access to the function call is a non static field,
9275 build the code to access it. */
34f4db93 9276 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9277 {
9278 decl = maybe_access_field (decl, *where_found, type);
9279 if (decl == error_mark_node)
9280 return 1;
9281 }
9282 /* And code for the array reference expression */
9283 decl = java_complete_tree (qual_wfl);
9284 if (decl == error_mark_node)
9285 return 1;
9286 type = QUAL_DECL_TYPE (decl);
9287 continue;
0a2138e2 9288
37feda7d
APB
9289 case PLUS_EXPR:
9290 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9291 return 1;
9292 if ((type = patch_string (decl)))
9293 decl = type;
9294 *where_found = QUAL_RESOLUTION (q) = decl;
9295 *type_found = type = TREE_TYPE (decl);
9296 break;
9297
165f37bc
APB
9298 case CLASS_LITERAL:
9299 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9300 return 1;
9301 *where_found = QUAL_RESOLUTION (q) = decl;
9302 *type_found = type = TREE_TYPE (decl);
9303 break;
9304
0a2138e2
APB
9305 default:
9306 /* Fix for -Wall Just go to the next statement. Don't
9307 continue */
a3f406ce 9308 break;
e04a16fb
AG
9309 }
9310
9311 /* If we fall here, we weren't processing a (static) function call. */
9312 previous_call_static = 0;
9313
9314 /* It can be the keyword THIS */
9315 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9316 {
9317 if (!current_this)
9318 {
9319 parse_error_context
9320 (wfl, "Keyword `this' used outside allowed context");
9321 return 1;
9322 }
f63991a8
APB
9323 if (ctxp->explicit_constructor_p)
9324 {
781b0558 9325 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9326 return 1;
9327 }
e04a16fb 9328 /* We have to generate code for intermediate acess */
c2952b01
APB
9329 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9330 {
9331 *where_found = decl = current_this;
9332 *type_found = type = QUAL_DECL_TYPE (decl);
9333 }
4dbf4496
APB
9334 /* We're trying to access the this from somewhere else. Make sure
9335 it's allowed before doing so. */
c2952b01
APB
9336 else
9337 {
4dbf4496
APB
9338 if (!enclosing_context_p (type, current_class))
9339 {
9340 char *p = xstrdup (lang_printable_name (type, 0));
9341 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9342 p, p,
9343 lang_printable_name (current_class, 0));
9344 free (p);
9345 return 1;
9346 }
c2952b01 9347 from_qualified_this = 1;
dba41d30
APB
9348 /* If there's nothing else after that, we need to
9349 produce something now, otherwise, the section of the
9350 code that needs to produce <T>.this will generate
9351 what is necessary. */
9352 if (!TREE_CHAIN (q))
9353 {
9354 decl = build_access_to_thisn (current_class, type, 0);
9355 *where_found = decl = java_complete_tree (decl);
9356 *type_found = type = TREE_TYPE (decl);
9357 }
c2952b01
APB
9358 }
9359
9360 from_type = 0;
e04a16fb
AG
9361 continue;
9362 }
9363
9364 /* 15.10.2 Accessing Superclass Members using SUPER */
9365 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9366 {
9367 tree node;
9368 /* Check on the restricted use of SUPER */
9369 if (METHOD_STATIC (current_function_decl)
9370 || current_class == object_type_node)
9371 {
9372 parse_error_context
9373 (wfl, "Keyword `super' used outside allowed context");
9374 return 1;
9375 }
9376 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9377 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9378 CLASSTYPE_SUPER (current_class),
9379 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9380 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9381 if (decl == error_mark_node)
9382 return 1;
e04a16fb
AG
9383 *type_found = type = QUAL_DECL_TYPE (decl);
9384 from_super = from_type = 1;
9385 continue;
9386 }
9387
9388 /* 15.13.1: Can't search for field name in packages, so we
9389 assume a variable/class name was meant. */
9390 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9391 {
5e942c50
APB
9392 tree name = resolve_package (wfl, &q);
9393 if (name)
9394 {
c2952b01 9395 tree list;
5e942c50 9396 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9397 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9398 out. We're going to search something inside it. */
9399 *type_found = type = TREE_TYPE (decl);
9400 layout_class (type);
9401 from_type = 1;
c2952b01 9402
dde1da72
APB
9403 /* Fix them all the way down, if any are left. */
9404 if (q)
c2952b01 9405 {
dde1da72
APB
9406 list = TREE_CHAIN (q);
9407 while (list)
9408 {
9409 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9410 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9411 list = TREE_CHAIN (list);
9412 }
c2952b01 9413 }
5e942c50 9414 }
e04a16fb 9415 else
5e942c50
APB
9416 {
9417 if (from_super || from_cast)
9418 parse_error_context
9419 ((from_cast ? qual_wfl : wfl),
9420 "No variable `%s' defined in class `%s'",
9421 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9422 lang_printable_name (type, 0));
9423 else
9424 parse_error_context
9425 (qual_wfl, "Undefined variable or class name: `%s'",
9426 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9427 return 1;
9428 }
e04a16fb
AG
9429 }
9430
9431 /* We have a type name. It's been already resolved when the
9432 expression was qualified. */
9433 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9434 {
9435 if (!(decl = QUAL_RESOLUTION (q)))
9436 return 1; /* Error reported already */
9437
c2952b01
APB
9438 /* Sneak preview. If next we see a `new', we're facing a
9439 qualification with resulted in a type being selected
9440 instead of a field. Report the error */
9441 if(TREE_CHAIN (q)
9442 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9443 {
9444 parse_error_context (qual_wfl, "Undefined variable `%s'",
9445 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9446 return 1;
9447 }
9448
e04a16fb
AG
9449 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9450 {
9451 parse_error_context
9452 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9453 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9454 GET_TYPE_NAME (type),
e04a16fb
AG
9455 IDENTIFIER_POINTER (DECL_NAME (decl)),
9456 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9457 return 1;
9458 }
5e942c50 9459 check_deprecation (qual_wfl, decl);
c2952b01 9460
e04a16fb
AG
9461 type = TREE_TYPE (decl);
9462 from_type = 1;
9463 }
9464 /* We resolve and expression name */
9465 else
9466 {
cd531a2e 9467 tree field_decl = NULL_TREE;
e04a16fb
AG
9468
9469 /* If there exists an early resolution, use it. That occurs
9470 only once and we know that there are more things to
9471 come. Don't do that when processing something after SUPER
9472 (we need more thing to be put in place below */
9473 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9474 {
9475 decl = QUAL_RESOLUTION (q);
c877974e 9476 if (!type)
5e942c50 9477 {
7f10c2e2
APB
9478 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9479 {
9480 if (current_this)
9481 *where_found = current_this;
9482 else
9483 {
9484 static_ref_err (qual_wfl, DECL_NAME (decl),
9485 current_class);
9486 return 1;
9487 }
f0f3a777
APB
9488 if (outer_field_access_p (current_class, decl))
9489 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9490 }
c877974e
APB
9491 else
9492 {
9493 *where_found = TREE_TYPE (decl);
9494 if (TREE_CODE (*where_found) == POINTER_TYPE)
9495 *where_found = TREE_TYPE (*where_found);
9496 }
5e942c50 9497 }
b67d701b 9498 }
e04a16fb
AG
9499
9500 /* We have to search for a field, knowing the type of its
9501 container. The flag FROM_TYPE indicates that we resolved
9502 the last member of the expression as a type name, which
5e942c50
APB
9503 means that for the resolution of this field, we'll look
9504 for other errors than if it was resolved as a member of
9505 an other field. */
e04a16fb
AG
9506 else
9507 {
9508 int is_static;
5e942c50
APB
9509 tree field_decl_type; /* For layout */
9510
e04a16fb
AG
9511 if (!from_type && !JREFERENCE_TYPE_P (type))
9512 {
9513 parse_error_context
9514 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9515 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9516 lang_printable_name (type, 0),
e04a16fb
AG
9517 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9518 return 1;
9519 }
9520
dc0b3eff
PB
9521 field_decl = lookup_field_wrapper (type,
9522 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9523
9524 /* Maybe what we're trying to access an inner class. */
9525 if (!field_decl)
9526 {
9527 tree ptr, inner_decl;
9528
9529 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9530 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9531 if (inner_decl)
9532 {
9533 check_inner_class_access (inner_decl, decl, qual_wfl);
9534 type = TREE_TYPE (inner_decl);
9535 decl = inner_decl;
9536 from_type = 1;
9537 continue;
9538 }
9539 }
9540
dc0b3eff 9541 if (field_decl == NULL_TREE)
e04a16fb
AG
9542 {
9543 parse_error_context
2aa11e97 9544 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9545 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9546 GET_TYPE_NAME (type));
e04a16fb
AG
9547 return 1;
9548 }
dc0b3eff
PB
9549 if (field_decl == error_mark_node)
9550 return 1;
5e942c50
APB
9551
9552 /* Layout the type of field_decl, since we may need
c877974e
APB
9553 it. Don't do primitive types or loaded classes. The
9554 situation of non primitive arrays may not handled
9555 properly here. FIXME */
5e942c50
APB
9556 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9557 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9558 else
9559 field_decl_type = TREE_TYPE (field_decl);
9560 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9561 && !CLASS_LOADED_P (field_decl_type)
9562 && !TYPE_ARRAY_P (field_decl_type))
9563 resolve_and_layout (field_decl_type, NULL_TREE);
9564 if (TYPE_ARRAY_P (field_decl_type))
9565 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9566
9567 /* Check on accessibility here */
9568 if (not_accessible_p (type, field_decl, from_super))
9569 {
9570 parse_error_context
9571 (qual_wfl,
9572 "Can't access %s field `%s.%s' from `%s'",
9573 java_accstring_lookup
9574 (get_access_flags_from_decl (field_decl)),
2aa11e97 9575 GET_TYPE_NAME (type),
e04a16fb
AG
9576 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9577 IDENTIFIER_POINTER
9578 (DECL_NAME (TYPE_NAME (current_class))));
9579 return 1;
9580 }
5e942c50 9581 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9582
9583 /* There are things to check when fields are accessed
9584 from type. There are no restrictions on a static
9585 declaration of the field when it is accessed from an
9586 interface */
9587 is_static = FIELD_STATIC (field_decl);
9588 if (!from_super && from_type
c2952b01
APB
9589 && !TYPE_INTERFACE_P (type)
9590 && !is_static
9591 && (current_function_decl
9592 && METHOD_STATIC (current_function_decl)))
e04a16fb 9593 {
7f10c2e2 9594 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9595 return 1;
9596 }
9597 from_cast = from_super = 0;
9598
c2952b01
APB
9599 /* It's an access from a type but it isn't static, we
9600 make it relative to `this'. */
9601 if (!is_static && from_type)
9602 decl = current_this;
9603
5e942c50
APB
9604 /* If we need to generate something to get a proper
9605 handle on what this field is accessed from, do it
9606 now. */
e04a16fb
AG
9607 if (!is_static)
9608 {
c583dd46 9609 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9610 if (decl == error_mark_node)
9611 return 1;
9612 }
9613
9614 /* We want to keep the location were found it, and the type
9615 we found. */
9616 *where_found = decl;
9617 *type_found = type;
9618
c2952b01
APB
9619 /* Generate the correct expression for field access from
9620 qualified this */
9621 if (from_qualified_this)
9622 {
9623 field_decl = build_outer_field_access (qual_wfl, field_decl);
9624 from_qualified_this = 0;
9625 }
9626
e04a16fb
AG
9627 /* This is the decl found and eventually the next one to
9628 search from */
9629 decl = field_decl;
9630 }
e04a16fb
AG
9631 from_type = 0;
9632 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9633
9634 /* Sneak preview. If decl is qualified by a `new', report
9635 the error here to be accurate on the peculiar construct */
9636 if (TREE_CHAIN (q)
9637 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9638 && !JREFERENCE_TYPE_P (type))
9639 {
9640 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9641 lang_printable_name (type, 0));
9642 return 1;
9643 }
e04a16fb 9644 }
dde1da72
APB
9645 /* `q' might have changed due to a after package resolution
9646 re-qualification */
9647 if (!q)
9648 break;
e04a16fb
AG
9649 }
9650 *found_decl = decl;
9651 return 0;
9652}
9653
9654/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9655 can't be accessed from REFERENCE (a record type). This should be
9656 used when decl is a field or a method.*/
e04a16fb 9657
be245ac0
KG
9658static int
9659not_accessible_p (reference, member, from_super)
e04a16fb
AG
9660 tree reference, member;
9661 int from_super;
9662{
9663 int access_flag = get_access_flags_from_decl (member);
9664
4dbf4496
APB
9665 /* Inner classes are processed by check_inner_class_access */
9666 if (INNER_CLASS_TYPE_P (reference))
9667 return 0;
9668
e04a16fb
AG
9669 /* Access always granted for members declared public */
9670 if (access_flag & ACC_PUBLIC)
9671 return 0;
9672
9673 /* Check access on protected members */
9674 if (access_flag & ACC_PROTECTED)
9675 {
9676 /* Access granted if it occurs from within the package
9677 containing the class in which the protected member is
9678 declared */
9679 if (class_in_current_package (DECL_CONTEXT (member)))
9680 return 0;
9681
9bbc7d9f
PB
9682 /* If accessed with the form `super.member', then access is granted */
9683 if (from_super)
9684 return 0;
e04a16fb 9685
9bbc7d9f 9686 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9687 member is declared or a subclass of it. Find the right
9688 context to perform the check */
9689 if (PURE_INNER_CLASS_TYPE_P (reference))
9690 {
9691 while (INNER_CLASS_TYPE_P (reference))
9692 {
9693 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9694 return 0;
9695 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9696 }
9697 }
473e7b07 9698 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9699 return 0;
e04a16fb
AG
9700 return 1;
9701 }
9702
9703 /* Check access on private members. Access is granted only if it
473e7b07 9704 occurs from within the class in which it is declared. Exceptions
4dbf4496 9705 are accesses from inner-classes. */
e04a16fb 9706 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9707 return (current_class == DECL_CONTEXT (member) ? 0 :
9708 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9709
9710 /* Default access are permitted only when occuring within the
9711 package in which the type (REFERENCE) is declared. In other words,
9712 REFERENCE is defined in the current package */
9713 if (ctxp->package)
9714 return !class_in_current_package (reference);
473e7b07 9715
e04a16fb
AG
9716 /* Otherwise, access is granted */
9717 return 0;
9718}
9719
5e942c50
APB
9720/* Test deprecated decl access. */
9721static void
9722check_deprecation (wfl, decl)
9723 tree wfl, decl;
9724{
49f48c71 9725 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9726 /* Complain if the field is deprecated and the file it was defined
9727 in isn't compiled at the same time the file which contains its
9728 use is */
9729 if (DECL_DEPRECATED (decl)
9730 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9731 {
9732 char the [20];
9733 switch (TREE_CODE (decl))
9734 {
9735 case FUNCTION_DECL:
9736 strcpy (the, "method");
9737 break;
9738 case FIELD_DECL:
9739 strcpy (the, "field");
9740 break;
9741 case TYPE_DECL:
9742 strcpy (the, "class");
9743 break;
15fdcfe9
PB
9744 default:
9745 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9746 }
9747 parse_warning_context
9748 (wfl, "The %s `%s' in class `%s' has been deprecated",
9749 the, lang_printable_name (decl, 0),
9750 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9751 }
9752}
9753
e04a16fb
AG
9754/* Returns 1 if class was declared in the current package, 0 otherwise */
9755
9756static int
9757class_in_current_package (class)
9758 tree class;
9759{
9760 static tree cache = NULL_TREE;
9761 int qualified_flag;
9762 tree left;
9763
9764 if (cache == class)
9765 return 1;
9766
9767 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9768
9769 /* If the current package is empty and the name of CLASS is
9770 qualified, class isn't in the current package. If there is a
9771 current package and the name of the CLASS is not qualified, class
9772 isn't in the current package */
0a2138e2 9773 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9774 return 0;
9775
9776 /* If there is not package and the name of CLASS isn't qualified,
9777 they belong to the same unnamed package */
9778 if (!ctxp->package && !qualified_flag)
9779 return 1;
9780
9781 /* Compare the left part of the name of CLASS with the package name */
9782 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9783 if (ctxp->package == left)
9784 {
19e223db
MM
9785 static int initialized_p;
9786 /* Register CACHE with the garbage collector. */
9787 if (!initialized_p)
9788 {
9789 ggc_add_tree_root (&cache, 1);
9790 initialized_p = 1;
9791 }
9792
e04a16fb
AG
9793 cache = class;
9794 return 1;
9795 }
9796 return 0;
9797}
9798
9799/* This function may generate code to access DECL from WHERE. This is
9800 done only if certain conditions meet. */
9801
9802static tree
9803maybe_access_field (decl, where, type)
9804 tree decl, where, type;
9805{
5e942c50
APB
9806 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9807 && !FIELD_STATIC (decl))
e04a16fb 9808 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9809 (type ? type : DECL_CONTEXT (decl)),
9810 DECL_NAME (decl));
e04a16fb
AG
9811 return decl;
9812}
9813
15fdcfe9 9814/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9815 and according to the situation, PRIMARY and WHERE may be
9816 used. IS_STATIC is set to 1 if the invoked function is static. */
9817
9818static tree
89e09b9a 9819patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9820 tree patch, primary, where;
9821 int *is_static;
b9f7e36c 9822 tree *ret_decl;
e04a16fb
AG
9823{
9824 tree wfl = TREE_OPERAND (patch, 0);
9825 tree args = TREE_OPERAND (patch, 1);
9826 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9827 tree list;
22eed1e6 9828 int is_static_flag = 0;
89e09b9a 9829 int is_super_init = 0;
bccaf73a 9830 tree this_arg = NULL_TREE;
e04a16fb
AG
9831
9832 /* Should be overriden if everything goes well. Otherwise, if
9833 something fails, it should keep this value. It stop the
9834 evaluation of a bogus assignment. See java_complete_tree,
9835 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9836 evaluating an assignment */
9837 TREE_TYPE (patch) = error_mark_node;
9838
9839 /* Since lookup functions are messing with line numbers, save the
9840 context now. */
9841 java_parser_context_save_global ();
9842
9843 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9844
9845 /* Resolution of qualified name, excluding constructors */
9846 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9847 {
dde1da72 9848 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9849 /* Extract the last IDENTIFIER of the qualified
9850 expression. This is a wfl and we will use it's location
9851 data during error report. */
9852 identifier_wfl = cut_identifier_in_qualified (wfl);
9853 identifier = EXPR_WFL_NODE (identifier_wfl);
9854
9855 /* Given the context, IDENTIFIER is syntactically qualified
9856 as a MethodName. We need to qualify what's before */
9857 qualify_ambiguous_name (wfl);
dde1da72 9858 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9859
dde1da72
APB
9860 if (resolved == error_mark_node)
9861 PATCH_METHOD_RETURN_ERROR ();
9862
9863 type = GET_SKIP_TYPE (resolved);
9864 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9865
9866 if (JPRIMITIVE_TYPE_P (type))
9867 {
7e51098e
TT
9868 parse_error_context
9869 (identifier_wfl,
9870 "Can't invoke a method on primitive type `%s'",
9871 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9872 PATCH_METHOD_RETURN_ERROR ();
9873 }
9874
dde1da72
APB
9875 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9876 args = nreverse (args);
2c56429a 9877
e04a16fb 9878 /* We're resolving a call from a type */
dde1da72 9879 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9880 {
dde1da72 9881 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9882 {
9883 parse_error_context
781b0558
KG
9884 (identifier_wfl,
9885 "Can't make static reference to method `%s' in interface `%s'",
9886 IDENTIFIER_POINTER (identifier),
e04a16fb 9887 IDENTIFIER_POINTER (name));
b9f7e36c 9888 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9889 }
e04a16fb
AG
9890 if (list && !METHOD_STATIC (list))
9891 {
c2e3db92 9892 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9893 parse_error_context
9894 (identifier_wfl,
9895 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9896 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9897 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9898 free (fct_name);
b9f7e36c 9899 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9900 }
9901 }
e04a16fb 9902 else
dde1da72
APB
9903 this_arg = primary = resolved;
9904
5e942c50 9905 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9906 wfl = identifier_wfl;
9907 }
9908 /* Resolution of simple names, names generated after a primary: or
9909 constructors */
9910 else
9911 {
cd531a2e 9912 tree class_to_search = NULL_TREE;
c2952b01 9913 int lc; /* Looking for Constructor */
e04a16fb
AG
9914
9915 /* We search constructor in their target class */
9916 if (CALL_CONSTRUCTOR_P (patch))
9917 {
22eed1e6
APB
9918 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9919 class_to_search = EXPR_WFL_NODE (wfl);
9920 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9921 this_identifier_node)
9922 class_to_search = NULL_TREE;
9923 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9924 super_identifier_node)
e04a16fb 9925 {
89e09b9a 9926 is_super_init = 1;
22eed1e6
APB
9927 if (CLASSTYPE_SUPER (current_class))
9928 class_to_search =
9929 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9930 else
9931 {
781b0558 9932 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9933 PATCH_METHOD_RETURN_ERROR ();
9934 }
e04a16fb 9935 }
22eed1e6
APB
9936
9937 /* Class to search is NULL if we're searching the current one */
9938 if (class_to_search)
e04a16fb 9939 {
c2952b01
APB
9940 class_to_search = resolve_and_layout (class_to_search, wfl);
9941
22eed1e6
APB
9942 if (!class_to_search)
9943 {
9944 parse_error_context
9945 (wfl, "Class `%s' not found in type declaration",
9946 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9947 PATCH_METHOD_RETURN_ERROR ();
9948 }
9949
5e942c50
APB
9950 /* Can't instantiate an abstract class, but we can
9951 invoke it's constructor. It's use within the `new'
9952 context is denied here. */
9953 if (CLASS_ABSTRACT (class_to_search)
9954 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9955 {
9956 parse_error_context
781b0558
KG
9957 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9958 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9959 PATCH_METHOD_RETURN_ERROR ();
9960 }
c2952b01 9961
22eed1e6 9962 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9963 }
22eed1e6
APB
9964 else
9965 class_to_search = current_class;
e04a16fb
AG
9966 lc = 1;
9967 }
9968 /* This is a regular search in the local class, unless an
9969 alternate class is specified. */
9970 else
9971 {
9972 class_to_search = (where ? where : current_class);
9973 lc = 0;
9974 }
c2952b01 9975
e04a16fb
AG
9976 /* NAME is a simple identifier or comes from a primary. Search
9977 in the class whose declaration contain the method being
9978 invoked. */
c877974e 9979 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9980
c2952b01 9981 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9982 /* Don't continue if no method were found, as the next statement
9983 can't be executed then. */
b9f7e36c
APB
9984 if (!list)
9985 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9986
9987 /* Check for static reference if non static methods */
9988 if (check_for_static_method_reference (wfl, patch, list,
9989 class_to_search, primary))
b9f7e36c 9990 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9991
165f37bc
APB
9992 /* Check for inner classes creation from illegal contexts */
9993 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9994 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9995 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9996 {
9997 parse_error_context
9998 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9999 lang_printable_name (class_to_search, 0),
10000 (!current_this ? "" :
10001 "; an explicit one must be provided when creating this inner class"));
10002 PATCH_METHOD_RETURN_ERROR ();
10003 }
10004
22eed1e6
APB
10005 /* Non static methods are called with the current object extra
10006 argument. If patch a `new TYPE()', the argument is the value
10007 returned by the object allocator. If method is resolved as a
10008 primary, use the primary otherwise use the current THIS. */
b9f7e36c 10009 args = nreverse (args);
bccaf73a 10010 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
10011 {
10012 this_arg = primary ? primary : current_this;
10013
10014 /* If we're using an access method, things are different.
10015 There are two familly of cases:
10016
10017 1) We're not generating bytecodes:
10018
10019 - LIST is non static. It's invocation is transformed from
10020 x(a1,...,an) into this$<n>.x(a1,....an).
10021 - LIST is static. It's invocation is transformed from
10022 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10023
10024 2) We're generating bytecodes:
10025
10026 - LIST is non static. It's invocation is transformed from
10027 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10028 - LIST is static. It's invocation is transformed from
10029 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
10030
10031 Of course, this$<n> can be abitrary complex, ranging from
10032 this$0 (the immediate outer context) to
10033 access$0(access$0(...(this$0))).
10034
10035 maybe_use_access_method returns a non zero value if the
dfb99c83 10036 this_arg has to be moved into the (then generated) stub
4dbf4496 10037 argument list. In the meantime, the selected function
dfb99c83 10038 might have be replaced by a generated stub. */
c2952b01 10039 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
10040 {
10041 args = tree_cons (NULL_TREE, this_arg, args);
10042 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10043 }
c2952b01 10044 }
e04a16fb 10045 }
b67d701b 10046
e04a16fb
AG
10047 /* Merge point of all resolution schemes. If we have nothing, this
10048 is an error, already signaled */
b9f7e36c
APB
10049 if (!list)
10050 PATCH_METHOD_RETURN_ERROR ();
b67d701b 10051
e04a16fb
AG
10052 /* Check accessibility, position the is_static flag, build and
10053 return the call */
9bbc7d9f 10054 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 10055 {
c2e3db92 10056 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
10057 parse_error_context
10058 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
10059 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 10060 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
10061 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
10062 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 10063 free (fct_name);
b9f7e36c 10064 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 10065 }
5e942c50 10066 check_deprecation (wfl, list);
22eed1e6 10067
c2952b01
APB
10068 /* If invoking a innerclass constructor, there are hidden parameters
10069 to pass */
10070 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10071 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10072 {
10073 /* And make sure we add the accessed local variables to be saved
10074 in field aliases. */
10075 args = build_alias_initializer_parameter_list
10076 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10077
da632f2c 10078 /* Secretly pass the current_this/primary as a second argument */
165f37bc 10079 if (primary || current_this)
f8b93ea7
APB
10080 {
10081 tree extra_arg;
10082 tree this_type = (current_this ?
10083 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10084 /* Method's (list) enclosing context */
10085 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10086 /* If we have a primary, use it. */
10087 if (primary)
10088 extra_arg = primary;
10089 /* The current `this' is an inner class but isn't a direct
10090 enclosing context for the inner class we're trying to
10091 create. Build an access to the proper enclosing context
10092 and use it. */
10093 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10094 && this_type != TREE_TYPE (mec))
10095 {
10096
10097 extra_arg = build_access_to_thisn (current_class,
10098 TREE_TYPE (mec), 0);
10099 extra_arg = java_complete_tree (extra_arg);
10100 }
10101 /* Otherwise, just use the current `this' as an enclosing
10102 context. */
10103 else
10104 extra_arg = current_this;
10105 args = tree_cons (NULL_TREE, extra_arg, args);
10106 }
165f37bc
APB
10107 else
10108 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10109 }
10110
152de068
APB
10111 /* This handles the situation where a constructor invocation needs
10112 to have an enclosing context passed as a second parameter (the
10113 constructor is one of an inner class. We extract it from the
10114 current function. */
10115 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10116 {
10117 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10118 tree extra_arg;
10119
10120 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10121 {
10122 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10123 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10124 }
10125 else
10126 {
10127 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10128 extra_arg =
10129 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10130 extra_arg = java_complete_tree (extra_arg);
10131 }
10132 args = tree_cons (NULL_TREE, extra_arg, args);
10133 }
10134
22eed1e6 10135 is_static_flag = METHOD_STATIC (list);
dba41d30 10136 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10137 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10138
c3f2a476
APB
10139 /* In the context of an explicit constructor invocation, we can't
10140 invoke any method relying on `this'. Exceptions are: we're
10141 invoking a static function, primary exists and is not the current
10142 this, we're creating a new object. */
22eed1e6 10143 if (ctxp->explicit_constructor_p
c3f2a476
APB
10144 && !is_static_flag
10145 && (!primary || primary == current_this)
10146 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10147 {
781b0558 10148 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10149 PATCH_METHOD_RETURN_ERROR ();
10150 }
e04a16fb 10151 java_parser_context_restore_global ();
22eed1e6
APB
10152 if (is_static)
10153 *is_static = is_static_flag;
b9f7e36c
APB
10154 /* Sometimes, we want the decl of the selected method. Such as for
10155 EH checking */
10156 if (ret_decl)
10157 *ret_decl = list;
89e09b9a
PB
10158 patch = patch_invoke (patch, list, args);
10159 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10160 {
c2952b01
APB
10161 tree finit_parms, finit_call;
10162
c00f0fb2 10163 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10164 finit_parms = build_alias_initializer_parameter_list
10165 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10166
c2952b01
APB
10167 finit_call =
10168 build_method_invocation (build_wfl_node (finit_identifier_node),
10169 finit_parms);
10170
10171 /* Generate the code used to initialize fields declared with an
10172 initialization statement and build a compound statement along
10173 with the super constructor invocation. */
89e09b9a
PB
10174 patch = build (COMPOUND_EXPR, void_type_node, patch,
10175 java_complete_tree (finit_call));
10176 CAN_COMPLETE_NORMALLY (patch) = 1;
10177 }
10178 return patch;
e04a16fb
AG
10179}
10180
10181/* Check that we're not trying to do a static reference to a method in
10182 non static method. Return 1 if it's the case, 0 otherwise. */
10183
10184static int
10185check_for_static_method_reference (wfl, node, method, where, primary)
10186 tree wfl, node, method, where, primary;
10187{
10188 if (METHOD_STATIC (current_function_decl)
10189 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10190 {
c2e3db92 10191 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10192 parse_error_context
10193 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10194 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10195 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10196 free (fct_name);
10197 return 1;
10198 }
10199 return 0;
10200}
10201
c2952b01
APB
10202/* Fix the invocation of *MDECL if necessary in the case of a
10203 invocation from an inner class. *THIS_ARG might be modified
10204 appropriately and an alternative access to *MDECL might be
10205 returned. */
10206
10207static int
10208maybe_use_access_method (is_super_init, mdecl, this_arg)
10209 int is_super_init;
10210 tree *mdecl, *this_arg;
10211{
10212 tree ctx;
10213 tree md = *mdecl, ta = *this_arg;
10214 int to_return = 0;
10215 int non_static_context = !METHOD_STATIC (md);
10216
10217 if (is_super_init
165f37bc
APB
10218 || DECL_CONTEXT (md) == current_class
10219 || !PURE_INNER_CLASS_TYPE_P (current_class)
10220 || DECL_FINIT_P (md))
c2952b01
APB
10221 return 0;
10222
10223 /* If we're calling a method found in an enclosing class, generate
10224 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10225 invoking a static method. Note that if MD's type is unrelated to
10226 CURRENT_CLASS, then the current this can be used. */
c2952b01 10227
2cb3951d 10228 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10229 {
10230 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10231 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10232 {
10233 ta = build_current_thisn (current_class);
10234 ta = build_wfl_node (ta);
10235 }
10236 else
10237 {
10238 tree type = ctx;
10239 while (type)
10240 {
10241 maybe_build_thisn_access_method (type);
4dbf4496 10242 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10243 {
10244 ta = build_access_to_thisn (ctx, type, 0);
10245 break;
10246 }
10247 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10248 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10249 }
10250 }
10251 ta = java_complete_tree (ta);
10252 }
10253
10254 /* We might have to use an access method to get to MD. We can
10255 break the method access rule as far as we're not generating
10256 bytecode */
10257 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10258 {
10259 md = build_outer_method_access_method (md);
10260 to_return = 1;
10261 }
10262
10263 *mdecl = md;
10264 *this_arg = ta;
10265
10266 /* Returnin a non zero value indicates we were doing a non static
10267 method invokation that is now a static invocation. It will have
10268 callee displace `this' to insert it in the regular argument
10269 list. */
10270 return (non_static_context && to_return);
10271}
10272
e04a16fb
AG
10273/* Patch an invoke expression METHOD and ARGS, based on its invocation
10274 mode. */
10275
10276static tree
89e09b9a 10277patch_invoke (patch, method, args)
e04a16fb 10278 tree patch, method, args;
e04a16fb
AG
10279{
10280 tree dtable, func;
0a2138e2 10281 tree original_call, t, ta;
e815887f 10282 tree cond = NULL_TREE;
e04a16fb 10283
5e942c50
APB
10284 /* Last step for args: convert build-in types. If we're dealing with
10285 a new TYPE() type call, the first argument to the constructor
e815887f 10286 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10287 `new' */
10288 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10289 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10290 t = TREE_CHAIN (t);
ac825856
APB
10291 for (ta = args; t != end_params_node && ta;
10292 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10293 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10294 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10295 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10296
10297 /* Resolve unresolved returned type isses */
10298 t = TREE_TYPE (TREE_TYPE (method));
10299 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10300 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10301
e8fc7396 10302 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10303 func = method;
10304 else
e04a16fb 10305 {
15fdcfe9 10306 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10307 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10308 {
10309 case INVOKE_VIRTUAL:
10310 dtable = invoke_build_dtable (0, args);
10311 func = build_invokevirtual (dtable, method);
10312 break;
b9f7e36c 10313
e815887f
TT
10314 case INVOKE_NONVIRTUAL:
10315 /* If the object for the method call is null, we throw an
10316 exception. We don't do this if the object is the current
10317 method's `this'. In other cases we just rely on an
10318 optimization pass to eliminate redundant checks. */
10319 if (TREE_VALUE (args) != current_this)
10320 {
10321 /* We use a SAVE_EXPR here to make sure we only evaluate
10322 the new `self' expression once. */
10323 tree save_arg = save_expr (TREE_VALUE (args));
10324 TREE_VALUE (args) = save_arg;
10325 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10326 null_pointer_node);
10327 }
10328 /* Fall through. */
10329
15fdcfe9
PB
10330 case INVOKE_SUPER:
10331 case INVOKE_STATIC:
10332 func = build_known_method_ref (method, TREE_TYPE (method),
10333 DECL_CONTEXT (method),
10334 signature, args);
10335 break;
e04a16fb 10336
15fdcfe9
PB
10337 case INVOKE_INTERFACE:
10338 dtable = invoke_build_dtable (1, args);
173f556c 10339 func = build_invokeinterface (dtable, method);
15fdcfe9 10340 break;
5e942c50 10341
15fdcfe9 10342 default:
89e09b9a 10343 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10344 }
10345
10346 /* Ensure self_type is initialized, (invokestatic). FIXME */
10347 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10348 }
10349
e04a16fb
AG
10350 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10351 TREE_OPERAND (patch, 0) = func;
10352 TREE_OPERAND (patch, 1) = args;
10353 original_call = patch;
10354
e815887f 10355 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10356 returned value is the first argument to the constructor. We build
10357 a COMPOUND_EXPR and use saved expression so that the overall NEW
10358 expression value is a pointer to a newly created and initialized
10359 class. */
10360 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10361 {
10362 tree class = DECL_CONTEXT (method);
10363 tree c1, saved_new, size, new;
e8fc7396 10364 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10365 {
10366 TREE_TYPE (patch) = build_pointer_type (class);
10367 return patch;
10368 }
e04a16fb
AG
10369 if (!TYPE_SIZE (class))
10370 safe_layout_class (class);
10371 size = size_in_bytes (class);
10372 new = build (CALL_EXPR, promote_type (class),
10373 build_address_of (alloc_object_node),
10374 tree_cons (NULL_TREE, build_class_ref (class),
10375 build_tree_list (NULL_TREE,
10376 size_in_bytes (class))),
10377 NULL_TREE);
10378 saved_new = save_expr (new);
10379 c1 = build_tree_list (NULL_TREE, saved_new);
10380 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10381 TREE_OPERAND (original_call, 1) = c1;
10382 TREE_SET_CODE (original_call, CALL_EXPR);
10383 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10384 }
e815887f
TT
10385
10386 /* If COND is set, then we are building a check to see if the object
10387 is NULL. */
10388 if (cond != NULL_TREE)
10389 {
10390 /* We have to make the `then' branch a compound expression to
10391 make the types turn out right. This seems bizarre. */
10392 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10393 build (COMPOUND_EXPR, TREE_TYPE (patch),
10394 build (CALL_EXPR, void_type_node,
10395 build_address_of (soft_nullpointer_node),
10396 NULL_TREE, NULL_TREE),
10397 (FLOAT_TYPE_P (TREE_TYPE (patch))
10398 ? build_real (TREE_TYPE (patch), dconst0)
10399 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10400 integer_zero_node))),
10401 patch);
10402 TREE_SIDE_EFFECTS (patch) = 1;
10403 }
10404
e04a16fb
AG
10405 return patch;
10406}
10407
10408static int
10409invocation_mode (method, super)
10410 tree method;
10411 int super;
10412{
10413 int access = get_access_flags_from_decl (method);
10414
22eed1e6
APB
10415 if (super)
10416 return INVOKE_SUPER;
10417
e815887f 10418 if (access & ACC_STATIC)
e04a16fb
AG
10419 return INVOKE_STATIC;
10420
e815887f
TT
10421 /* We have to look for a constructor before we handle nonvirtual
10422 calls; otherwise the constructor will look nonvirtual. */
10423 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10424 return INVOKE_STATIC;
e815887f
TT
10425
10426 if (access & ACC_FINAL || access & ACC_PRIVATE)
10427 return INVOKE_NONVIRTUAL;
10428
10429 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10430 return INVOKE_NONVIRTUAL;
10431
e04a16fb
AG
10432 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10433 return INVOKE_INTERFACE;
22eed1e6 10434
e04a16fb
AG
10435 return INVOKE_VIRTUAL;
10436}
10437
b67d701b
PB
10438/* Retrieve a refined list of matching methods. It covers the step
10439 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10440
10441static tree
10442lookup_method_invoke (lc, cl, class, name, arg_list)
10443 int lc;
10444 tree cl;
10445 tree class, name, arg_list;
10446{
de4c7b02 10447 tree atl = end_params_node; /* Arg Type List */
c877974e 10448 tree method, signature, list, node;
49f48c71 10449 const char *candidates; /* Used for error report */
b5b8a0e7 10450 char *dup;
e04a16fb 10451
5e942c50 10452 /* Fix the arguments */
e04a16fb
AG
10453 for (node = arg_list; node; node = TREE_CHAIN (node))
10454 {
e3884b71 10455 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10456 /* Non primitive type may have to be resolved */
e3884b71 10457 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10458 resolve_and_layout (current_arg, NULL_TREE);
10459 /* And promoted */
b67d701b 10460 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10461 current_arg = promote_type (current_arg);
5e942c50 10462 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10463 }
e04a16fb 10464
c2952b01
APB
10465 /* Presto. If we're dealing with an anonymous class and a
10466 constructor call, generate the right constructor now, since we
10467 know the arguments' types. */
10468
10469 if (lc && ANONYMOUS_CLASS_P (class))
10470 craft_constructor (TYPE_NAME (class), atl);
10471
5e942c50
APB
10472 /* Find all candidates and then refine the list, searching for the
10473 most specific method. */
10474 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10475 list = find_most_specific_methods_list (list);
b67d701b
PB
10476 if (list && !TREE_CHAIN (list))
10477 return TREE_VALUE (list);
e04a16fb 10478
b67d701b
PB
10479 /* Issue an error. List candidates if any. Candidates are listed
10480 only if accessible (non accessible methods may end-up here for
10481 the sake of a better error report). */
10482 candidates = NULL;
10483 if (list)
e04a16fb 10484 {
e04a16fb 10485 tree current;
b67d701b 10486 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10487 for (current = list; current; current = TREE_CHAIN (current))
10488 {
b67d701b
PB
10489 tree cm = TREE_VALUE (current);
10490 char string [4096];
10491 if (!cm || not_accessible_p (class, cm, 0))
10492 continue;
b67d701b 10493 sprintf
22eed1e6
APB
10494 (string, " `%s' in `%s'%s",
10495 get_printable_method_name (cm),
b67d701b
PB
10496 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10497 (TREE_CHAIN (current) ? "\n" : ""));
10498 obstack_grow (&temporary_obstack, string, strlen (string));
10499 }
10500 obstack_1grow (&temporary_obstack, '\0');
10501 candidates = obstack_finish (&temporary_obstack);
10502 }
10503 /* Issue the error message */
c877974e
APB
10504 method = make_node (FUNCTION_TYPE);
10505 TYPE_ARG_TYPES (method) = atl;
b67d701b 10506 signature = build_java_argument_signature (method);
c63b98cd 10507 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10508 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10509 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10510 (lc ? dup : IDENTIFIER_POINTER (name)),
10511 IDENTIFIER_POINTER (signature), dup,
b67d701b 10512 (candidates ? candidates : ""));
b5b8a0e7 10513 free (dup);
b67d701b
PB
10514 return NULL_TREE;
10515}
10516
5e942c50
APB
10517/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10518 when we're looking for a constructor. */
b67d701b
PB
10519
10520static tree
5e942c50
APB
10521find_applicable_accessible_methods_list (lc, class, name, arglist)
10522 int lc;
b67d701b
PB
10523 tree class, name, arglist;
10524{
ad69b5b6
BM
10525 static struct hash_table t, *searched_classes = NULL;
10526 static int search_not_done = 0;
b67d701b
PB
10527 tree list = NULL_TREE, all_list = NULL_TREE;
10528
ad69b5b6
BM
10529 /* Check the hash table to determine if this class has been searched
10530 already. */
10531 if (searched_classes)
10532 {
10533 if (hash_lookup (searched_classes,
10534 (const hash_table_key) class, FALSE, NULL))
10535 return NULL;
10536 }
10537 else
10538 {
10539 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10540 java_hash_compare_tree_node);
10541 searched_classes = &t;
10542 }
10543
10544 search_not_done++;
10545 hash_lookup (searched_classes,
0c2b8145 10546 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10547
c2952b01
APB
10548 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10549 {
10550 load_class (class, 1);
10551 safe_layout_class (class);
10552 }
10553
1982388a 10554 /* Search interfaces */
9a7ab4b3
APB
10555 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10556 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10557 {
1982388a
APB
10558 int i, n;
10559 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10560 search_applicable_methods_list (lc, TYPE_METHODS (class),
10561 name, arglist, &list, &all_list);
1982388a 10562 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10563 for (i = 1; i < n; i++)
b67d701b 10564 {
de0b553f
APB
10565 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10566 tree rlist;
10567
de0b553f
APB
10568 rlist = find_applicable_accessible_methods_list (lc, t, name,
10569 arglist);
165f37bc 10570 list = chainon (rlist, list);
e04a16fb 10571 }
e04a16fb 10572 }
1982388a
APB
10573 /* Search classes */
10574 else
c2952b01 10575 {
165f37bc
APB
10576 tree sc = class;
10577 int seen_inner_class = 0;
c2952b01
APB
10578 search_applicable_methods_list (lc, TYPE_METHODS (class),
10579 name, arglist, &list, &all_list);
10580
165f37bc
APB
10581 /* We must search all interfaces of this class */
10582 if (!lc)
10583 {
10584 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10585 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10586 for (i = 1; i < n; i++)
10587 {
10588 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10589 if (t != object_type_node)
30a3caef
ZW
10590 {
10591 tree rlist
10592 = find_applicable_accessible_methods_list (lc, t,
10593 name, arglist);
10594 list = chainon (rlist, list);
10595 }
165f37bc 10596 }
165f37bc
APB
10597 }
10598
c2952b01
APB
10599 /* Search enclosing context of inner classes before looking
10600 ancestors up. */
10601 while (!lc && INNER_CLASS_TYPE_P (class))
10602 {
165f37bc
APB
10603 tree rlist;
10604 seen_inner_class = 1;
c2952b01 10605 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10606 rlist = find_applicable_accessible_methods_list (lc, class,
10607 name, arglist);
10608 list = chainon (rlist, list);
c2952b01 10609 }
165f37bc
APB
10610
10611 if (!lc && seen_inner_class
10612 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10613 class = CLASSTYPE_SUPER (sc);
10614 else
10615 class = sc;
10616
ad69b5b6
BM
10617 /* Search superclass */
10618 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10619 {
10620 tree rlist;
10621 class = CLASSTYPE_SUPER (class);
10622 rlist = find_applicable_accessible_methods_list (lc, class,
10623 name, arglist);
10624 list = chainon (rlist, list);
10625 }
10626 }
10627
10628 search_not_done--;
10629
10630 /* We're done. Reset the searched classes list and finally search
10631 java.lang.Object if it wasn't searched already. */
10632 if (!search_not_done)
10633 {
10634 if (!lc
10635 && TYPE_METHODS (object_type_node)
10636 && !hash_lookup (searched_classes,
10637 (const hash_table_key) object_type_node,
10638 FALSE, NULL))
10639 {
10640 search_applicable_methods_list (lc,
10641 TYPE_METHODS (object_type_node),
10642 name, arglist, &list, &all_list);
10643 }
10644 hash_table_free (searched_classes);
10645 searched_classes = NULL;
c2952b01 10646 }
1982388a 10647
b67d701b
PB
10648 /* Either return the list obtained or all selected (but
10649 inaccessible) methods for better error report. */
10650 return (!list ? all_list : list);
10651}
e04a16fb 10652
ad69b5b6 10653/* Effectively search for the appropriate method in method */
1982388a
APB
10654
10655static void
c2952b01 10656search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10657 int lc;
10658 tree method, name, arglist;
10659 tree *list, *all_list;
10660{
10661 for (; method; method = TREE_CHAIN (method))
10662 {
10663 /* When dealing with constructor, stop here, otherwise search
10664 other classes */
10665 if (lc && !DECL_CONSTRUCTOR_P (method))
10666 continue;
10667 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10668 || (GET_METHOD_NAME (method) != name)))
10669 continue;
c7303e41 10670
1982388a
APB
10671 if (argument_types_convertible (method, arglist))
10672 {
10673 /* Retain accessible methods only */
10674 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10675 method, 0))
10676 *list = tree_cons (NULL_TREE, method, *list);
10677 else
10678 /* Also retain all selected method here */
10679 *all_list = tree_cons (NULL_TREE, method, *list);
10680 }
10681 }
ad69b5b6 10682}
1982388a 10683
b67d701b
PB
10684/* 15.11.2.2 Choose the Most Specific Method */
10685
10686static tree
10687find_most_specific_methods_list (list)
10688 tree list;
10689{
10690 int max = 0;
9a7ab4b3 10691 int abstract, candidates;
b67d701b
PB
10692 tree current, new_list = NULL_TREE;
10693 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10694 {
b67d701b
PB
10695 tree method;
10696 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10697
10698 for (method = list; method; method = TREE_CHAIN (method))
10699 {
0c2b8145 10700 tree method_v, current_v;
b67d701b
PB
10701 /* Don't test a method against itself */
10702 if (method == current)
10703 continue;
10704
0c2b8145
APB
10705 method_v = TREE_VALUE (method);
10706 current_v = TREE_VALUE (current);
10707
10708 /* Compare arguments and location where methods where declared */
10709 if (argument_types_convertible (method_v, current_v))
b67d701b 10710 {
0c2b8145
APB
10711 if (valid_method_invocation_conversion_p
10712 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10713 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10714 && enclosing_context_p (DECL_CONTEXT (method_v),
10715 DECL_CONTEXT (current_v))))
10716 {
10717 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10718 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10719 max = (v > max ? v : max);
10720 }
b67d701b
PB
10721 }
10722 }
e04a16fb
AG
10723 }
10724
b67d701b 10725 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10726 for (current = list, abstract = -1, candidates = -1;
10727 current; current = TREE_CHAIN (current))
b67d701b 10728 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10729 {
10730 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10731 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10732 candidates++;
10733 }
b67d701b 10734
165f37bc
APB
10735 /* If we have several and they're all abstract, just pick the
10736 closest one. */
9a7ab4b3
APB
10737 if (candidates > 0 && (candidates == abstract))
10738 {
10739 new_list = nreverse (new_list);
10740 TREE_CHAIN (new_list) = NULL_TREE;
10741 }
165f37bc 10742
cab8e2bd
BM
10743 /* We have several (we couldn't find a most specific), all but one
10744 are abstract, we pick the only non abstract one. */
10745 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10746 {
9a7ab4b3
APB
10747 for (current = new_list; current; current = TREE_CHAIN (current))
10748 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10749 {
10750 TREE_CHAIN (current) = NULL_TREE;
10751 new_list = current;
10752 }
165f37bc
APB
10753 }
10754
b67d701b
PB
10755 /* If we can't find one, lower expectations and try to gather multiple
10756 maximally specific methods */
165f37bc 10757 while (!new_list && max)
b67d701b
PB
10758 {
10759 while (--max > 0)
10760 {
10761 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10762 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10763 }
b67d701b
PB
10764 }
10765
10766 return new_list;
e04a16fb
AG
10767}
10768
b67d701b
PB
10769/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10770 converted by method invocation conversion (5.3) to the type of the
10771 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10772 to change less often than M1. */
e04a16fb 10773
b67d701b
PB
10774static int
10775argument_types_convertible (m1, m2_or_arglist)
10776 tree m1, m2_or_arglist;
e04a16fb 10777{
b67d701b
PB
10778 static tree m2_arg_value = NULL_TREE;
10779 static tree m2_arg_cache = NULL_TREE;
19e223db 10780 static int initialized_p;
e04a16fb 10781
b67d701b 10782 register tree m1_arg, m2_arg;
e04a16fb 10783
19e223db
MM
10784 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10785 collector. */
10786 if (!initialized_p)
10787 {
10788 ggc_add_tree_root (&m2_arg_value, 1);
10789 ggc_add_tree_root (&m2_arg_cache, 1);
10790 initialized_p = 1;
10791 }
10792
c2952b01 10793 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10794
b67d701b
PB
10795 if (m2_arg_value == m2_or_arglist)
10796 m2_arg = m2_arg_cache;
10797 else
10798 {
10799 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10800 argument types */
10801 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10802 {
10803 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10804 if (!METHOD_STATIC (m2_or_arglist))
10805 m2_arg = TREE_CHAIN (m2_arg);
10806 }
10807 else
10808 m2_arg = m2_or_arglist;
e04a16fb 10809
b67d701b
PB
10810 m2_arg_value = m2_or_arglist;
10811 m2_arg_cache = m2_arg;
10812 }
e04a16fb 10813
de4c7b02 10814 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10815 {
c877974e 10816 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10817 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10818 TREE_VALUE (m2_arg)))
10819 break;
10820 m1_arg = TREE_CHAIN (m1_arg);
10821 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10822 }
de4c7b02 10823 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10824}
10825
10826/* Qualification routines */
10827
10828static void
10829qualify_ambiguous_name (id)
10830 tree id;
10831{
cd531a2e
KG
10832 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10833 saved_current_class;
d8fccff5 10834 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10835 int code;
e04a16fb
AG
10836
10837 /* We first qualify the first element, then derive qualification of
10838 others based on the first one. If the first element is qualified
10839 by a resolution (field or type), this resolution is stored in the
10840 QUAL_RESOLUTION of the qual element being examined. We need to
10841 save the current_class since the use of SUPER might change the
10842 its value. */
10843 saved_current_class = current_class;
10844 qual = EXPR_WFL_QUALIFICATION (id);
10845 do {
10846
10847 /* Simple qualified expression feature a qual_wfl that is a
10848 WFL. Expression derived from a primary feature more complicated
10849 things like a CALL_EXPR. Expression from primary need to be
10850 worked out to extract the part on which the qualification will
10851 take place. */
10852 qual_wfl = QUAL_WFL (qual);
10853 switch (TREE_CODE (qual_wfl))
10854 {
10855 case CALL_EXPR:
10856 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10857 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10858 {
10859 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10860 qual_wfl = QUAL_WFL (qual);
10861 }
10862 break;
d8fccff5 10863 case NEW_ARRAY_EXPR:
c2952b01 10864 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10865 qual = TREE_CHAIN (qual);
1a6d4fb7 10866 again = new_array_found = 1;
d8fccff5 10867 continue;
e04a16fb 10868 case CONVERT_EXPR:
f2760b27
APB
10869 break;
10870 case NEW_CLASS_EXPR:
e04a16fb
AG
10871 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10872 break;
c583dd46
APB
10873 case ARRAY_REF:
10874 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10875 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10876 break;
8576f094
APB
10877 case STRING_CST:
10878 qual = TREE_CHAIN (qual);
10879 qual_wfl = QUAL_WFL (qual);
10880 break;
165f37bc
APB
10881 case CLASS_LITERAL:
10882 qual = TREE_CHAIN (qual);
10883 qual_wfl = QUAL_WFL (qual);
10884 break;
0a2138e2
APB
10885 default:
10886 /* Fix for -Wall. Just break doing nothing */
10887 break;
e04a16fb 10888 }
8576f094 10889
e04a16fb
AG
10890 ptr_type = current_class;
10891 again = 0;
8576f094
APB
10892 code = TREE_CODE (qual_wfl);
10893
10894 /* Pos evaluation: non WFL leading expression nodes */
10895 if (code == CONVERT_EXPR
10896 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10897 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10898
cd7c5840
APB
10899 else if (code == INTEGER_CST)
10900 name = qual_wfl;
10901
ac22f9cb 10902 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10903 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10904 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10905
c2952b01
APB
10906 else if (code == TREE_LIST)
10907 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10908
37feda7d
APB
10909 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10910 || code == PLUS_EXPR)
8576f094
APB
10911 {
10912 qual = TREE_CHAIN (qual);
10913 qual_wfl = QUAL_WFL (qual);
10914 again = 1;
10915 }
10916 else
f441f671
APB
10917 {
10918 name = EXPR_WFL_NODE (qual_wfl);
10919 if (!name)
10920 {
10921 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10922 again = 1;
10923 }
10924 }
10925
e04a16fb
AG
10926 /* If we have a THIS (from a primary), we set the context accordingly */
10927 if (name == this_identifier_node)
10928 {
6e22695a
APB
10929 /* This isn't really elegant. One more added irregularity
10930 before I start using COMPONENT_REF (hopefully very soon.) */
10931 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10932 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10933 EXPR_WITH_FILE_LOCATION
10934 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10935 this_identifier_node)
10936 {
10937 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10938 qual = EXPR_WFL_QUALIFICATION (qual);
10939 }
e04a16fb
AG
10940 qual = TREE_CHAIN (qual);
10941 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10942 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10943 again = 1;
10944 else
10945 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10946 this_found = 1;
10947 }
10948 /* If we have a SUPER, we set the context accordingly */
10949 if (name == super_identifier_node)
10950 {
10951 current_class = CLASSTYPE_SUPER (ptr_type);
10952 /* Check that there is such a thing as a super class. If not,
10953 return. The error will be caught later on, during the
10954 resolution */
10955 if (!current_class)
10956 {
10957 current_class = saved_current_class;
10958 return;
10959 }
10960 qual = TREE_CHAIN (qual);
10961 /* Do one more interation to set things up */
10962 super_found = again = 1;
10963 }
10964 } while (again);
10965
f2760b27
APB
10966 /* If name appears within the scope of a local variable declaration
10967 or parameter declaration, then it is an expression name. We don't
10968 carry this test out if we're in the context of the use of SUPER
10969 or THIS */
cd7c5840
APB
10970 if (!this_found && !super_found
10971 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10972 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10973 {
10974 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10975 QUAL_RESOLUTION (qual) = decl;
10976 }
10977
10978 /* If within the class/interface NAME was found to be used there
10979 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10980 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10981 address length, it is OK. */
10982 else if ((decl = lookup_field_wrapper (ptr_type, name))
10983 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10984 {
10985 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10986 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10987 }
10988
1a6d4fb7 10989 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10990 - NAME is a class/interface declared within the compilation
10991 unit containing NAME,
10992 - NAME is imported via a single-type-import declaration,
10993 - NAME is declared in an another compilation unit of the package
10994 of the compilation unit containing NAME,
10995 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10996 of the compilation unit containing NAME.
10997 - NAME is actually a STRING_CST. */
cd7c5840
APB
10998 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10999 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
11000 {
11001 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11002 QUAL_RESOLUTION (qual) = decl;
11003 }
11004
f2760b27 11005 /* Method call, array references and cast are expression name */
9bbc7d9f 11006 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
11007 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11008 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
11009 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11010
11011 /* Check here that NAME isn't declared by more than one
11012 type-import-on-demand declaration of the compilation unit
11013 containing NAME. FIXME */
11014
11015 /* Otherwise, NAME is reclassified as a package name */
11016 else
11017 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11018
11019 /* Propagate the qualification accross other components of the
11020 qualified name */
11021 for (qual = TREE_CHAIN (qual); qual;
11022 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11023 {
11024 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11025 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11026 else
11027 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11028 }
11029
11030 /* Store the global qualification for the ambiguous part of ID back
11031 into ID fields */
11032 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11033 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11034 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11035 RESOLVE_TYPE_NAME_P (id) = 1;
11036 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11037 RESOLVE_PACKAGE_NAME_P (id) = 1;
11038
11039 /* Restore the current class */
11040 current_class = saved_current_class;
11041}
11042
11043static int
11044breakdown_qualified (left, right, source)
11045 tree *left, *right, source;
11046{
63ad61ed 11047 char *p, *base;
e04a16fb
AG
11048 int l = IDENTIFIER_LENGTH (source);
11049
63ad61ed
ZW
11050 base = alloca (l + 1);
11051 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11052
e04a16fb 11053 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 11054 p = base + l - 1;
e04a16fb
AG
11055 while (*p != '.' && p != base)
11056 p--;
11057
11058 /* We didn't find a '.'. Return an error */
11059 if (p == base)
11060 return 1;
11061
11062 *p = '\0';
11063 if (right)
11064 *right = get_identifier (p+1);
63ad61ed 11065 *left = get_identifier (base);
e04a16fb
AG
11066
11067 return 0;
11068}
11069
e04a16fb 11070/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11071 local variable decls if present.
11072 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11073
11074static tree
11075java_complete_tree (node)
11076 tree node;
5b09b33e
PB
11077{
11078 node = java_complete_lhs (node);
c7303e41 11079 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11080 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11081 && !flag_emit_xref)
5b09b33e
PB
11082 {
11083 tree value = DECL_INITIAL (node);
11084 DECL_INITIAL (node) = NULL_TREE;
11085 value = fold_constant_for_init (value, node);
11086 DECL_INITIAL (node) = value;
11087 if (value != NULL_TREE)
c2952b01
APB
11088 {
11089 /* fold_constant_for_init sometimes widen the original type
11090 of the constant (i.e. byte to int.) It's not desirable,
11091 especially if NODE is a function argument. */
11092 if (TREE_CODE (value) == INTEGER_CST
11093 && TREE_TYPE (node) != TREE_TYPE (value))
11094 return convert (TREE_TYPE (node), value);
11095 else
11096 return value;
11097 }
c7303e41
APB
11098 else
11099 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11100 }
11101 return node;
11102}
11103
2aa11e97
APB
11104static tree
11105java_stabilize_reference (node)
11106 tree node;
11107{
11108 if (TREE_CODE (node) == COMPOUND_EXPR)
11109 {
11110 tree op0 = TREE_OPERAND (node, 0);
11111 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11112 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11113 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11114 return node;
11115 }
5cbdba64 11116 return stabilize_reference (node);
2aa11e97
APB
11117}
11118
5b09b33e
PB
11119/* Patch tree nodes in a function body. When a BLOCK is found, push
11120 local variable decls if present.
11121 Same as java_complete_tree, but does not resolve static finals to values. */
11122
11123static tree
11124java_complete_lhs (node)
11125 tree node;
e04a16fb 11126{
22eed1e6 11127 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11128 int flag;
e04a16fb
AG
11129
11130 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11131 worked out. */
e04a16fb
AG
11132 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11133 return node;
11134
11135 /* The switch block implements cases processing container nodes
11136 first. Contained nodes are always written back. Leaves come
11137 next and return a value. */
11138 switch (TREE_CODE (node))
11139 {
11140 case BLOCK:
11141
11142 /* 1- Block section.
11143 Set the local values on decl names so we can identify them
11144 faster when they're referenced. At that stage, identifiers
11145 are legal so we don't check for declaration errors. */
11146 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11147 {
11148 DECL_CONTEXT (cn) = current_function_decl;
11149 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11150 }
15fdcfe9
PB
11151 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11152 CAN_COMPLETE_NORMALLY (node) = 1;
11153 else
e04a16fb 11154 {
15fdcfe9
PB
11155 tree stmt = BLOCK_EXPR_BODY (node);
11156 tree *ptr;
11157 int error_seen = 0;
11158 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11159 {
c877974e
APB
11160 /* Re-order from (((A; B); C); ...; Z) to
11161 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11162 This makes it easier to scan the statements left-to-right
11163 without using recursion (which might overflow the stack
11164 if the block has many statements. */
11165 for (;;)
11166 {
11167 tree left = TREE_OPERAND (stmt, 0);
11168 if (TREE_CODE (left) != COMPOUND_EXPR)
11169 break;
11170 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11171 TREE_OPERAND (left, 1) = stmt;
11172 stmt = left;
11173 }
11174 BLOCK_EXPR_BODY (node) = stmt;
11175 }
11176
c877974e
APB
11177 /* Now do the actual complete, without deep recursion for
11178 long blocks. */
15fdcfe9 11179 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11180 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11181 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11182 {
11183 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11184 tree *next = &TREE_OPERAND (*ptr, 1);
11185 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11186 if (cur == empty_stmt_node)
11187 {
11188 /* Optimization; makes it easier to detect empty bodies.
11189 Most useful for <clinit> with all-constant initializer. */
11190 *ptr = *next;
11191 continue;
11192 }
15fdcfe9
PB
11193 if (TREE_CODE (cur) == ERROR_MARK)
11194 error_seen++;
11195 else if (! CAN_COMPLETE_NORMALLY (cur))
11196 {
11197 wfl_op2 = *next;
11198 for (;;)
11199 {
11200 if (TREE_CODE (wfl_op2) == BLOCK)
11201 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11202 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11203 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11204 else
11205 break;
11206 }
11207 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11208 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11209 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11210 }
11211 ptr = next;
11212 }
11213 *ptr = java_complete_tree (*ptr);
11214
11215 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11216 return error_mark_node;
15fdcfe9 11217 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11218 }
11219 /* Turn local bindings to null */
11220 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11221 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11222
11223 TREE_TYPE (node) = void_type_node;
11224 break;
11225
11226 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11227
b9f7e36c
APB
11228 case THROW_EXPR:
11229 wfl_op1 = TREE_OPERAND (node, 0);
11230 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11231 /* 14.19 A throw statement cannot complete normally. */
11232 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11233 return patch_throw_statement (node, wfl_op1);
11234
11235 case SYNCHRONIZED_EXPR:
11236 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11237 return patch_synchronized_statement (node, wfl_op1);
11238
b67d701b
PB
11239 case TRY_EXPR:
11240 return patch_try_statement (node);
11241
a7d8d81f
PB
11242 case TRY_FINALLY_EXPR:
11243 COMPLETE_CHECK_OP_0 (node);
11244 COMPLETE_CHECK_OP_1 (node);
11245 CAN_COMPLETE_NORMALLY (node)
11246 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11247 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11248 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11249 return node;
11250
5a005d9e
PB
11251 case CLEANUP_POINT_EXPR:
11252 COMPLETE_CHECK_OP_0 (node);
11253 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11254 CAN_COMPLETE_NORMALLY (node) =
11255 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11256 return node;
11257
11258 case WITH_CLEANUP_EXPR:
11259 COMPLETE_CHECK_OP_0 (node);
11260 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11261 CAN_COMPLETE_NORMALLY (node) =
11262 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11263 TREE_TYPE (node) = void_type_node;
11264 return node;
11265
e04a16fb
AG
11266 case LABELED_BLOCK_EXPR:
11267 PUSH_LABELED_BLOCK (node);
11268 if (LABELED_BLOCK_BODY (node))
11269 COMPLETE_CHECK_OP_1 (node);
11270 TREE_TYPE (node) = void_type_node;
11271 POP_LABELED_BLOCK ();
1fb89a4d
APB
11272
11273 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11274 {
11275 LABELED_BLOCK_BODY (node) = NULL_TREE;
11276 CAN_COMPLETE_NORMALLY (node) = 1;
11277 }
1fb89a4d 11278 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11279 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11280 return node;
11281
11282 case EXIT_BLOCK_EXPR:
11283 /* We don't complete operand 1, because it's the return value of
11284 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11285 return patch_bc_statement (node);
11286
15fdcfe9
PB
11287 case CASE_EXPR:
11288 cn = java_complete_tree (TREE_OPERAND (node, 0));
11289 if (cn == error_mark_node)
11290 return cn;
11291
8576f094
APB
11292 /* First, the case expression must be constant. Values of final
11293 fields are accepted. */
15fdcfe9 11294 cn = fold (cn);
8576f094
APB
11295 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11296 && JDECL_P (TREE_OPERAND (cn, 1))
11297 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11298 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11299 {
100f7cd8
APB
11300 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11301 TREE_OPERAND (cn, 1));
100f7cd8 11302 }
15fdcfe9 11303
ce6e9147 11304 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11305 {
11306 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11307 parse_error_context (node, "Constant expression required");
11308 return error_mark_node;
11309 }
11310
11311 nn = ctxp->current_loop;
11312
11313 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11314 if (!try_builtin_assignconv (NULL_TREE,
11315 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11316 {
11317 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11318 parse_error_context
11319 (wfl_operator,
11320 "Incompatible type for case. Can't convert `%s' to `int'",
11321 lang_printable_name (TREE_TYPE (cn), 0));
11322 return error_mark_node;
11323 }
11324
11325 cn = fold (convert (int_type_node, cn));
11326
11327 /* Multiple instance of a case label bearing the same
11328 value is checked during code generation. The case
11329 expression is allright so far. */
34d4df06
APB
11330 if (TREE_CODE (cn) == VAR_DECL)
11331 cn = DECL_INITIAL (cn);
15fdcfe9 11332 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11333 TREE_TYPE (node) = void_type_node;
15fdcfe9 11334 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11335 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11336 break;
11337
11338 case DEFAULT_EXPR:
11339 nn = ctxp->current_loop;
11340 /* Only one default label is allowed per switch statement */
11341 if (SWITCH_HAS_DEFAULT (nn))
11342 {
11343 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11344 parse_error_context (wfl_operator,
11345 "Duplicate case label: `default'");
11346 return error_mark_node;
11347 }
11348 else
11349 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11350 TREE_TYPE (node) = void_type_node;
10100cc7 11351 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11352 CAN_COMPLETE_NORMALLY (node) = 1;
11353 break;
11354
b67d701b 11355 case SWITCH_EXPR:
e04a16fb
AG
11356 case LOOP_EXPR:
11357 PUSH_LOOP (node);
11358 /* Check whether the loop was enclosed in a labeled
11359 statement. If not, create one, insert the loop in it and
11360 return the node */
11361 nn = patch_loop_statement (node);
b67d701b 11362
e04a16fb 11363 /* Anyways, walk the body of the loop */
b67d701b
PB
11364 if (TREE_CODE (node) == LOOP_EXPR)
11365 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11366 /* Switch statement: walk the switch expression and the cases */
11367 else
11368 node = patch_switch_statement (node);
11369
e7c7bcef 11370 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11371 nn = error_mark_node;
11372 else
15fdcfe9 11373 {
b635eb2f
PB
11374 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11375 /* If we returned something different, that's because we
11376 inserted a label. Pop the label too. */
11377 if (nn != node)
11378 {
11379 if (CAN_COMPLETE_NORMALLY (node))
11380 CAN_COMPLETE_NORMALLY (nn) = 1;
11381 POP_LABELED_BLOCK ();
11382 }
15fdcfe9 11383 }
e04a16fb
AG
11384 POP_LOOP ();
11385 return nn;
11386
11387 case EXIT_EXPR:
11388 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11389 return patch_exit_expr (node);
11390
11391 case COND_EXPR:
11392 /* Condition */
11393 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11394 if (TREE_OPERAND (node, 0) == error_mark_node)
11395 return error_mark_node;
11396 /* then-else branches */
11397 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11398 if (TREE_OPERAND (node, 1) == error_mark_node)
11399 return error_mark_node;
11400 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11401 if (TREE_OPERAND (node, 2) == error_mark_node)
11402 return error_mark_node;
11403 return patch_if_else_statement (node);
11404 break;
11405
22eed1e6
APB
11406 case CONDITIONAL_EXPR:
11407 /* Condition */
11408 wfl_op1 = TREE_OPERAND (node, 0);
11409 COMPLETE_CHECK_OP_0 (node);
11410 wfl_op2 = TREE_OPERAND (node, 1);
11411 COMPLETE_CHECK_OP_1 (node);
11412 wfl_op3 = TREE_OPERAND (node, 2);
11413 COMPLETE_CHECK_OP_2 (node);
11414 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11415
e04a16fb
AG
11416 /* 3- Expression section */
11417 case COMPOUND_EXPR:
15fdcfe9 11418 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11419 TREE_OPERAND (node, 0) = nn =
11420 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11421 if (wfl_op2 == empty_stmt_node)
11422 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11423 else
15fdcfe9 11424 {
dc0b3eff 11425 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11426 {
dc0b3eff
PB
11427 /* An unreachable condition in a do-while statement
11428 is *not* (technically) an unreachable statement. */
11429 nn = wfl_op2;
11430 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11431 nn = EXPR_WFL_NODE (nn);
11432 if (TREE_CODE (nn) != EXIT_EXPR)
11433 {
11434 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11435 parse_error_context (wfl_operator, "Unreachable statement");
11436 }
bccaf73a 11437 }
dc0b3eff
PB
11438 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11439 if (TREE_OPERAND (node, 1) == error_mark_node)
11440 return error_mark_node;
11441 CAN_COMPLETE_NORMALLY (node)
11442 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11443 }
e04a16fb
AG
11444 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11445 break;
11446
11447 case RETURN_EXPR:
15fdcfe9 11448 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11449 return patch_return (node);
11450
11451 case EXPR_WITH_FILE_LOCATION:
11452 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11453 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11454 {
5423609c 11455 tree wfl = node;
15fdcfe9 11456 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11457 if (node == error_mark_node)
11458 return node;
5423609c
APB
11459 /* Keep line number information somewhere were it doesn't
11460 disrupt the completion process. */
2c56429a 11461 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11462 {
11463 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11464 TREE_OPERAND (node, 1) = wfl;
11465 }
15fdcfe9
PB
11466 CAN_COMPLETE_NORMALLY (node) = 1;
11467 }
e04a16fb
AG
11468 else
11469 {
5b09b33e
PB
11470 tree body;
11471 int save_lineno = lineno;
11472 lineno = EXPR_WFL_LINENO (node);
11473 body = java_complete_tree (EXPR_WFL_NODE (node));
11474 lineno = save_lineno;
15fdcfe9 11475 EXPR_WFL_NODE (node) = body;
dc0b3eff 11476 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11477 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11478 if (body == empty_stmt_node)
11479 {
11480 /* Optimization; makes it easier to detect empty bodies. */
11481 return body;
11482 }
dc0b3eff 11483 if (body == error_mark_node)
e04a16fb
AG
11484 {
11485 /* Its important for the evaluation of assignment that
11486 this mark on the TREE_TYPE is propagated. */
11487 TREE_TYPE (node) = error_mark_node;
11488 return error_mark_node;
11489 }
11490 else
11491 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11492
e04a16fb
AG
11493 }
11494 break;
11495
b67d701b 11496 case NEW_ARRAY_EXPR:
e04a16fb
AG
11497 /* Patch all the dimensions */
11498 flag = 0;
11499 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11500 {
11501 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11502 tree dim = convert (int_type_node,
11503 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11504 if (dim == error_mark_node)
11505 {
11506 flag = 1;
11507 continue;
11508 }
11509 else
11510 {
b9f7e36c 11511 TREE_VALUE (cn) = dim;
e04a16fb
AG
11512 /* Setup the location of the current dimension, for
11513 later error report. */
11514 TREE_PURPOSE (cn) =
11515 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11516 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11517 }
11518 }
11519 /* They complete the array creation expression, if no errors
11520 were found. */
15fdcfe9 11521 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11522 return (flag ? error_mark_node
11523 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11524
c2952b01
APB
11525 case NEW_ANONYMOUS_ARRAY_EXPR:
11526 /* Create the array type if necessary. */
11527 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11528 {
11529 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11530 if (!(type = resolve_type_during_patch (type)))
11531 return error_mark_node;
11532 type = build_array_from_name (type, NULL_TREE,
11533 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11534 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11535 }
11536 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11537 ANONYMOUS_ARRAY_INITIALIZER (node));
11538 if (node == error_mark_node)
11539 return error_mark_node;
11540 CAN_COMPLETE_NORMALLY (node) = 1;
11541 return node;
11542
b67d701b 11543 case NEW_CLASS_EXPR:
e04a16fb 11544 case CALL_EXPR:
b67d701b 11545 /* Complete function's argument(s) first */
e04a16fb
AG
11546 if (complete_function_arguments (node))
11547 return error_mark_node;
11548 else
b9f7e36c 11549 {
22eed1e6
APB
11550 tree decl, wfl = TREE_OPERAND (node, 0);
11551 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11552
c877974e 11553 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11554 NULL_TREE, 0, &decl);
c877974e
APB
11555 if (node == error_mark_node)
11556 return error_mark_node;
11557
11558 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11559 /* If we call this(...), register signature and positions */
11560 if (in_this)
11561 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11562 tree_cons (wfl, decl,
11563 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11564 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11565 return force_evaluation_order (node);
b9f7e36c 11566 }
e04a16fb
AG
11567
11568 case MODIFY_EXPR:
11569 /* Save potential wfls */
11570 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11571 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11572
cd9643f7
PB
11573 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11574 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11575 && DECL_INITIAL (nn) != NULL_TREE)
11576 {
100f7cd8
APB
11577 tree value;
11578
100f7cd8 11579 value = fold_constant_for_init (nn, nn);
c2952b01 11580
cd9643f7
PB
11581 if (value != NULL_TREE)
11582 {
11583 tree type = TREE_TYPE (value);
c2952b01
APB
11584 if (JPRIMITIVE_TYPE_P (type) ||
11585 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11586 return empty_stmt_node;
11587 }
629d4b4d
APB
11588 if (! flag_emit_class_files)
11589 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11590 if (CLASS_FINAL_VARIABLE_P (nn))
11591 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11592 }
e04a16fb 11593 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11594
e04a16fb
AG
11595 if (TREE_OPERAND (node, 0) == error_mark_node)
11596 return error_mark_node;
11597
5cbdba64
APB
11598 flag = COMPOUND_ASSIGN_P (wfl_op2);
11599 if (flag)
e04a16fb 11600 {
c2952b01
APB
11601 /* This might break when accessing outer field from inner
11602 class. TESTME, FIXME */
2aa11e97 11603 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11604
11605 /* Hand stablize the lhs on both places */
e04a16fb 11606 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11607 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11608 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11609
5cbdba64 11610 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11611 /* Now complete the RHS. We write it back later on. */
11612 nn = java_complete_tree (TREE_OPERAND (node, 1));
11613
642f15d1
APB
11614 if ((cn = patch_string (nn)))
11615 nn = cn;
11616
2aa11e97
APB
11617 /* The last part of the rewrite for E1 op= E2 is to have
11618 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11619 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11620 TREE_TYPE (lvalue), nn));
5cbdba64 11621
568aac9c
TT
11622 /* If the assignment is compound and has reference type,
11623 then ensure the LHS has type String and nothing else. */
11624 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11625 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11626 parse_error_context (wfl_op2,
11627 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11628 lang_printable_name (TREE_TYPE (lvalue), 0));
11629
5cbdba64 11630 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11631 }
11632
f8976021 11633 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11634 function to complete this RHS. Note that a NEW_ARRAY_INIT
11635 might have been already fully expanded if created as a result
11636 of processing an anonymous array initializer. We avoid doing
11637 the operation twice by testing whether the node already bears
11638 a type. */
11639 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11640 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11641 TREE_OPERAND (node, 1));
2aa11e97 11642 /* Otherwise we simply complete the RHS */
f8976021
APB
11643 else
11644 nn = java_complete_tree (TREE_OPERAND (node, 1));
11645
e04a16fb 11646 if (nn == error_mark_node)
c0d87ff6 11647 return error_mark_node;
2aa11e97
APB
11648
11649 /* Write back the RHS as we evaluated it. */
e04a16fb 11650 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11651
11652 /* In case we're handling = with a String as a RHS, we need to
11653 produce a String out of the RHS (it might still be a
11654 STRING_CST or a StringBuffer at this stage */
11655 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11656 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11657
11658 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11659 TREE_OPERAND (node, 1))))
11660 {
11661 /* We return error_mark_node if outer_field_access_fix
11662 detects we write into a final. */
11663 if (nn == error_mark_node)
11664 return error_mark_node;
11665 node = nn;
11666 }
11667 else
11668 {
11669 node = patch_assignment (node, wfl_op1, wfl_op2);
11670 /* Reorganize the tree if necessary. */
11671 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11672 || JSTRING_P (TREE_TYPE (node))))
11673 node = java_refold (node);
11674 }
11675
15fdcfe9
PB
11676 CAN_COMPLETE_NORMALLY (node) = 1;
11677 return node;
e04a16fb
AG
11678
11679 case MULT_EXPR:
11680 case PLUS_EXPR:
11681 case MINUS_EXPR:
11682 case LSHIFT_EXPR:
11683 case RSHIFT_EXPR:
11684 case URSHIFT_EXPR:
11685 case BIT_AND_EXPR:
11686 case BIT_XOR_EXPR:
11687 case BIT_IOR_EXPR:
11688 case TRUNC_MOD_EXPR:
c2952b01 11689 case TRUNC_DIV_EXPR:
e04a16fb
AG
11690 case RDIV_EXPR:
11691 case TRUTH_ANDIF_EXPR:
11692 case TRUTH_ORIF_EXPR:
11693 case EQ_EXPR:
11694 case NE_EXPR:
11695 case GT_EXPR:
11696 case GE_EXPR:
11697 case LT_EXPR:
11698 case LE_EXPR:
11699 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11700 knows how to handle those cases. */
11701 wfl_op1 = TREE_OPERAND (node, 0);
11702 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11703
15fdcfe9 11704 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11705 /* Don't complete string nodes if dealing with the PLUS operand. */
11706 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11707 {
11708 nn = java_complete_tree (wfl_op1);
11709 if (nn == error_mark_node)
11710 return error_mark_node;
48a840d9 11711
2aa11e97
APB
11712 TREE_OPERAND (node, 0) = nn;
11713 }
b67d701b 11714 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11715 {
11716 nn = java_complete_tree (wfl_op2);
11717 if (nn == error_mark_node)
11718 return error_mark_node;
48a840d9 11719
2aa11e97
APB
11720 TREE_OPERAND (node, 1) = nn;
11721 }
dc0b3eff 11722 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11723
5e942c50
APB
11724 case INSTANCEOF_EXPR:
11725 wfl_op1 = TREE_OPERAND (node, 0);
11726 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11727 if (flag_emit_xref)
11728 {
11729 TREE_TYPE (node) = boolean_type_node;
11730 return node;
11731 }
5e942c50
APB
11732 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11733
b67d701b 11734 case UNARY_PLUS_EXPR:
e04a16fb
AG
11735 case NEGATE_EXPR:
11736 case TRUTH_NOT_EXPR:
11737 case BIT_NOT_EXPR:
11738 case PREDECREMENT_EXPR:
11739 case PREINCREMENT_EXPR:
11740 case POSTDECREMENT_EXPR:
11741 case POSTINCREMENT_EXPR:
11742 case CONVERT_EXPR:
11743 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11744 how to handle those cases. */
11745 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11746 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11747 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11748 if (TREE_OPERAND (node, 0) == error_mark_node)
11749 return error_mark_node;
4a5f66c3
APB
11750 node = patch_unaryop (node, wfl_op1);
11751 CAN_COMPLETE_NORMALLY (node) = 1;
11752 break;
e04a16fb
AG
11753
11754 case ARRAY_REF:
11755 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11756 how to handle those cases. */
11757 wfl_op1 = TREE_OPERAND (node, 0);
11758 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11759 if (TREE_OPERAND (node, 0) == error_mark_node)
11760 return error_mark_node;
7f1d4866 11761 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11762 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11763 /* The same applies to wfl_op2 */
11764 wfl_op2 = TREE_OPERAND (node, 1);
11765 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11766 if (TREE_OPERAND (node, 1) == error_mark_node)
11767 return error_mark_node;
7f1d4866 11768 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11769 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11770 return patch_array_ref (node);
e04a16fb 11771
63a212ed
PB
11772 case RECORD_TYPE:
11773 return node;;
11774
11775 case COMPONENT_REF:
11776 /* The first step in the re-write of qualified name handling. FIXME.
11777 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11778 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11779 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11780 {
11781 tree name = TREE_OPERAND (node, 1);
11782 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11783 if (field == NULL_TREE)
11784 {
11785 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11786 return error_mark_node;
11787 }
11788 if (! FIELD_STATIC (field))
11789 {
11790 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11791 return error_mark_node;
11792 }
11793 return field;
11794 }
11795 else
11796 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11797 break;
9bbc7d9f 11798
b67d701b 11799 case THIS_EXPR:
e04a16fb
AG
11800 /* Can't use THIS in a static environment */
11801 if (!current_this)
11802 {
11803 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11804 parse_error_context (wfl_operator,
11805 "Keyword `this' used outside allowed context");
e04a16fb
AG
11806 TREE_TYPE (node) = error_mark_node;
11807 return error_mark_node;
11808 }
22eed1e6
APB
11809 if (ctxp->explicit_constructor_p)
11810 {
11811 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11812 parse_error_context
781b0558 11813 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11814 TREE_TYPE (node) = error_mark_node;
11815 return error_mark_node;
11816 }
e04a16fb 11817 return current_this;
c2952b01
APB
11818
11819 case CLASS_LITERAL:
11820 CAN_COMPLETE_NORMALLY (node) = 1;
11821 node = patch_incomplete_class_ref (node);
11822 if (node == error_mark_node)
11823 return error_mark_node;
11824 break;
11825
11826 case INSTANCE_INITIALIZERS_EXPR:
11827 in_instance_initializer++;
11828 node = java_complete_tree (TREE_OPERAND (node, 0));
11829 in_instance_initializer--;
11830 if (node != error_mark_node)
11831 TREE_TYPE (node) = void_type_node;
11832 else
11833 return error_mark_node;
11834 break;
e04a16fb 11835
e04a16fb 11836 default:
15fdcfe9 11837 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11838 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11839 and it's time to turn it into the appropriate String object */
11840 if ((nn = patch_string (node)))
11841 node = nn;
11842 else
11843 fatal ("No case for tree code `%s' - java_complete_tree\n",
11844 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11845 }
11846 return node;
11847}
11848
11849/* Complete function call's argument. Return a non zero value is an
11850 error was found. */
11851
11852static int
11853complete_function_arguments (node)
11854 tree node;
11855{
11856 int flag = 0;
11857 tree cn;
11858
f63991a8 11859 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11860 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11861 {
b67d701b 11862 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11863 parm = java_complete_tree (wfl);
c2952b01 11864
e04a16fb
AG
11865 if (parm == error_mark_node)
11866 {
11867 flag = 1;
11868 continue;
11869 }
b67d701b
PB
11870 /* If have a string literal that we haven't transformed yet or a
11871 crafted string buffer, as a result of use of the the String
11872 `+' operator. Build `parm.toString()' and expand it. */
11873 if ((temp = patch_string (parm)))
b9f7e36c 11874 parm = temp;
5e942c50
APB
11875 /* Inline PRIMTYPE.TYPE read access */
11876 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11877
5e942c50 11878 TREE_VALUE (cn) = parm;
e04a16fb 11879 }
f63991a8 11880 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11881 return flag;
11882}
11883
11884/* Sometimes (for loops and variable initialized during their
11885 declaration), we want to wrap a statement around a WFL and turn it
11886 debugable. */
11887
11888static tree
11889build_debugable_stmt (location, stmt)
11890 int location;
11891 tree stmt;
11892{
11893 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11894 {
11895 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11896 EXPR_WFL_LINECOL (stmt) = location;
11897 }
11898 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11899 return stmt;
11900}
11901
11902static tree
11903build_expr_block (body, decls)
11904 tree body, decls;
11905{
11906 tree node = make_node (BLOCK);
11907 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11908 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11909 if (body)
11910 TREE_TYPE (node) = TREE_TYPE (body);
11911 TREE_SIDE_EFFECTS (node) = 1;
11912 return node;
11913}
11914
b67d701b
PB
11915/* Create a new function block and link it approriately to current
11916 function block chain */
e04a16fb
AG
11917
11918static tree
11919enter_block ()
11920{
b67d701b
PB
11921 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11922}
11923
11924/* Link block B supercontext to the previous block. The current
11925 function DECL is used as supercontext when enter_a_block is called
11926 for the first time for a given function. The current function body
11927 (DECL_FUNCTION_BODY) is set to be block B. */
11928
11929static tree
11930enter_a_block (b)
11931 tree b;
11932{
e04a16fb
AG
11933 tree fndecl = current_function_decl;
11934
f099f336
APB
11935 if (!fndecl) {
11936 BLOCK_SUPERCONTEXT (b) = current_static_block;
11937 current_static_block = b;
11938 }
11939
11940 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11941 {
11942 BLOCK_SUPERCONTEXT (b) = fndecl;
11943 DECL_FUNCTION_BODY (fndecl) = b;
11944 }
11945 else
11946 {
11947 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11948 DECL_FUNCTION_BODY (fndecl) = b;
11949 }
11950 return b;
11951}
11952
11953/* Exit a block by changing the current function body
11954 (DECL_FUNCTION_BODY) to the current block super context, only if
11955 the block being exited isn't the method's top level one. */
11956
11957static tree
11958exit_block ()
11959{
f099f336
APB
11960 tree b;
11961 if (current_function_decl)
11962 {
11963 b = DECL_FUNCTION_BODY (current_function_decl);
11964 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11965 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11966 }
11967 else
11968 {
11969 b = current_static_block;
e04a16fb 11970
f099f336
APB
11971 if (BLOCK_SUPERCONTEXT (b))
11972 current_static_block = BLOCK_SUPERCONTEXT (b);
11973 }
e04a16fb
AG
11974 return b;
11975}
11976
11977/* Lookup for NAME in the nested function's blocks, all the way up to
11978 the current toplevel one. It complies with Java's local variable
11979 scoping rules. */
11980
11981static tree
11982lookup_name_in_blocks (name)
11983 tree name;
11984{
f099f336 11985 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11986
11987 while (b != current_function_decl)
11988 {
11989 tree current;
11990
11991 /* Paranoid sanity check. To be removed */
11992 if (TREE_CODE (b) != BLOCK)
11993 fatal ("non block expr function body - lookup_name_in_blocks");
11994
11995 for (current = BLOCK_EXPR_DECLS (b); current;
11996 current = TREE_CHAIN (current))
11997 if (DECL_NAME (current) == name)
11998 return current;
11999 b = BLOCK_SUPERCONTEXT (b);
12000 }
12001 return NULL_TREE;
12002}
12003
12004static void
12005maybe_absorb_scoping_blocks ()
12006{
f099f336 12007 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
12008 {
12009 tree b = exit_block ();
12010 java_method_add_stmt (current_function_decl, b);
12011 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12012 }
12013}
12014
12015\f
12016/* This section of the source is reserved to build_* functions that
12017 are building incomplete tree nodes and the patch_* functions that
12018 are completing them. */
12019
c2952b01
APB
12020/* Wrap a non WFL node around a WFL. */
12021static tree
9a7ab4b3 12022build_wfl_wrap (node, location)
c2952b01 12023 tree node;
9a7ab4b3 12024 int location;
c2952b01
APB
12025{
12026 tree wfl, node_to_insert = node;
12027
12028 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12029 with the way we're processing SUPER. A THIS from a primary as a
12030 different form than a SUPER. Turn THIS into something symbolic */
12031 if (TREE_CODE (node) == THIS_EXPR)
12032 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12033 else
12034 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12035
9a7ab4b3 12036 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
12037 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12038 return wfl;
12039}
12040
12041
9bbc7d9f 12042/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
12043 we're currently dealing with the class java.lang.Object. */
12044
12045static tree
e920ebc9
APB
12046build_super_invocation (mdecl)
12047 tree mdecl;
22eed1e6 12048{
e920ebc9 12049 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 12050 return empty_stmt_node;
22eed1e6
APB
12051 else
12052 {
9ee9b555 12053 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
12054 tree a = NULL_TREE, t;
12055 /* If we're dealing with an anonymous class, pass the arguments
12056 of the crafted constructor along. */
12057 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12058 {
12059 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12060 for (; t != end_params_node; t = TREE_CHAIN (t))
12061 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12062 }
12063 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12064 }
12065}
12066
12067/* Build a SUPER/THIS qualified method invocation. */
12068
12069static tree
12070build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12071 int use_this;
12072 tree name, args;
12073 int lloc, rloc;
22eed1e6
APB
12074{
12075 tree invok;
12076 tree wfl =
9ee9b555 12077 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12078 EXPR_WFL_LINECOL (wfl) = lloc;
12079 invok = build_method_invocation (name, args);
12080 return make_qualified_primary (wfl, invok, rloc);
12081}
12082
b67d701b 12083/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12084
12085static tree
12086build_method_invocation (name, args)
12087 tree name;
12088 tree args;
12089{
12090 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12091 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12092 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12093 return call;
12094}
12095
12096/* Build an incomplete new xxx(...) node. */
12097
12098static tree
12099build_new_invocation (name, args)
12100 tree name, args;
12101{
12102 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12103 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12104 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12105 return call;
12106}
12107
12108/* Build an incomplete assignment expression. */
12109
12110static tree
12111build_assignment (op, op_location, lhs, rhs)
12112 int op, op_location;
12113 tree lhs, rhs;
12114{
12115 tree assignment;
12116 /* Build the corresponding binop if we deal with a Compound
12117 Assignment operator. Mark the binop sub-tree as part of a
12118 Compound Assignment expression */
12119 if (op != ASSIGN_TK)
12120 {
12121 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12122 COMPOUND_ASSIGN_P (rhs) = 1;
12123 }
12124 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12125 TREE_SIDE_EFFECTS (assignment) = 1;
12126 EXPR_WFL_LINECOL (assignment) = op_location;
12127 return assignment;
12128}
12129
12130/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12131
15fdcfe9 12132char *
e04a16fb
AG
12133print_int_node (node)
12134 tree node;
12135{
12136 static char buffer [80];
12137 if (TREE_CONSTANT_OVERFLOW (node))
12138 sprintf (buffer, "<overflow>");
12139
12140 if (TREE_INT_CST_HIGH (node) == 0)
12141 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12142 TREE_INT_CST_LOW (node));
12143 else if (TREE_INT_CST_HIGH (node) == -1
12144 && TREE_INT_CST_LOW (node) != 0)
12145 {
12146 buffer [0] = '-';
12147 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12148 -TREE_INT_CST_LOW (node));
12149 }
12150 else
12151 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12152 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12153
12154 return buffer;
12155}
12156
c7303e41
APB
12157\f
12158
12159/* This section of the code handle assignment check with FINAL
12160 variables. */
12161
12162static void
12163reset_static_final_variable_assignment_flag (class)
12164 tree class;
12165{
12166 tree field;
12167 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12168 if (CLASS_FINAL_VARIABLE_P (field))
12169 DECL_FIELD_FINAL_LIIC (field) = 0;
12170}
12171
12172/* Figure whether all final static variable have been initialized. */
12173
12174static void
12175check_static_final_variable_assignment_flag (class)
12176 tree class;
12177{
12178 tree field;
12179
12180 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12181 if (CLASS_FINAL_VARIABLE_P (field)
12182 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12183 parse_error_context
12184 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12185 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12186 IDENTIFIER_POINTER (DECL_NAME (field)));
12187}
12188
12189/* This function marks all final variable locally unassigned. */
12190
12191static void
12192reset_final_variable_local_assignment_flag (class)
12193 tree class;
12194{
12195 tree field;
12196 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12197 if (FINAL_VARIABLE_P (field))
12198 DECL_FIELD_FINAL_LIIC (field) = 0;
12199}
12200
12201/* Figure whether all final variables have beem initialized in MDECL
12202 and mark MDECL accordingly. */
12203
12204static void
12205check_final_variable_local_assignment_flag (class, mdecl)
12206 tree class;
12207 tree mdecl;
12208{
12209 tree field;
12210 int initialized = 0;
12211 int non_initialized = 0;
12212
12213 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12214 return;
12215
12216 /* First find out whether all final variables or no final variable
12217 are initialized in this ctor. We don't take into account final
12218 variable that have been initialized upon declaration. */
12219 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12220 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12221 {
12222 if (DECL_FIELD_FINAL_LIIC (field))
12223 initialized++;
12224 else
12225 non_initialized++;
12226 }
12227
12228 /* There were no non initialized variable and no initialized variable.
12229 This ctor is fine. */
12230 if (!non_initialized && !initialized)
12231 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12232 /* If no variables have been initialized, that fine. We'll check
12233 later whether this ctor calls a constructor which initializes
12234 them. We mark the ctor as not initializing all its finals. */
12235 else if (initialized == 0)
12236 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12237 /* If we have a mixed bag, then we have a problem. We need to report
12238 all the variables we're not initializing. */
12239 else if (initialized && non_initialized)
12240 {
12241 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12242 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12243 if (FIELD_FINAL (field)
12244 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12245 {
12246 parse_error_context
12247 (lookup_cl (mdecl),
12248 "Blank final variable `%s' may not have been initialized in this constructor",
12249 IDENTIFIER_POINTER (DECL_NAME (field)));
12250 DECL_FIELD_FINAL_IERR (field) = 1;
12251 }
12252 }
12253 /* Otherwise we know this ctor is initializing all its final
12254 variable. We mark it so. */
12255 else
12256 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12257}
12258
12259/* This function recurses in a simple what through STMT and stops when
12260 it finds a constructor call. It then verifies that the called
12261 constructor initialized its final properly. Return 1 upon success,
12262 0 or -1 otherwise. */
12263
12264static int
12265check_final_variable_indirect_assignment (stmt)
12266 tree stmt;
12267{
12268 int res;
12269 switch (TREE_CODE (stmt))
12270 {
12271 case EXPR_WITH_FILE_LOCATION:
12272 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12273 case COMPOUND_EXPR:
12274 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12275 if (res)
12276 return res;
12277 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12278 case SAVE_EXPR:
12279 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12280 case CALL_EXPR:
12281 {
12282 tree decl = TREE_OPERAND (stmt, 0);
12283 tree fbody;
12284
12285 if (TREE_CODE (decl) != FUNCTION_DECL)
12286 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12287 if (TREE_CODE (decl) != FUNCTION_DECL)
12288 fatal ("Can't find FUNCTION_DECL in CALL_EXPR - check_final_variable_indirect_assignment");
12289 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12290 return 1;
12291 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12292 return -1;
12293 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12294 if (fbody == error_mark_node)
12295 return -1;
12296 fbody = BLOCK_EXPR_BODY (fbody);
12297 return check_final_variable_indirect_assignment (fbody);
12298 }
12299 default:
12300 break;
12301 }
12302 return 0;
12303}
12304
12305/* This is the last chance to catch a final variable initialization
12306 problem. This routine will report an error if a final variable was
12307 never (globally) initialized and never reported as not having been
12308 initialized properly. */
12309
12310static void
12311check_final_variable_global_assignment_flag (class)
12312 tree class;
12313{
12314 tree field, mdecl;
12315 int nnctor = 0;
12316
12317 /* We go through all natural ctors and see whether they're
12318 initializing all their final variables or not. */
12319 current_function_decl = NULL_TREE; /* For the error report. */
12320 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12321 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12322 {
12323 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12324 {
12325 /* It doesn't. Maybe it calls a constructor that initializes
12326 them. find out. */
12327 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12328 if (fbody == error_mark_node)
12329 continue;
12330 fbody = BLOCK_EXPR_BODY (fbody);
12331 if (check_final_variable_indirect_assignment (fbody) == 1)
12332 {
12333 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12334 nnctor++;
12335 }
12336 else
12337 parse_error_context
12338 (lookup_cl (mdecl),
12339 "Final variable initialization error in this constructor");
12340 }
12341 else
12342 nnctor++;
12343 }
12344
12345 /* Finally we catch final variables that never were initialized */
12346 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12347 if (FINAL_VARIABLE_P (field)
12348 /* If the field wasn't initialized upon declaration */
12349 && !DECL_FIELD_FINAL_IUD (field)
12350 /* There wasn't natural ctor in which the field could have been
12351 initialized */
12352 && !nnctor
12353 /* If we never reported a problem with this field */
12354 && !DECL_FIELD_FINAL_IERR (field))
12355 {
12356 current_function_decl = NULL;
12357 parse_error_context
12358 (DECL_FIELD_FINAL_WFL (field),
12359 "Final variable `%s' hasn't been initialized upon its declaration",
12360 IDENTIFIER_POINTER (DECL_NAME (field)));
12361 }
12362
12363}
12364
7f1d4866
APB
12365/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12366 context. */
5e942c50
APB
12367
12368static int
12369check_final_assignment (lvalue, wfl)
12370 tree lvalue, wfl;
12371{
c7303e41
APB
12372 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12373 return 0;
12374
12375 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12376 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12377 lvalue = TREE_OPERAND (lvalue, 1);
12378
c7303e41
APB
12379 if (!FIELD_FINAL (lvalue))
12380 return 0;
12381
12382 /* Now the logic. We can modify a final VARIABLE:
12383 1) in finit$, (its declaration was followed by an initialization,)
12384 2) consistently in each natural ctor, if it wasn't initialized in
12385 finit$ or once in <clinit>. In any other cases, an error should be
12386 reported. */
12387 if (DECL_FINIT_P (current_function_decl))
5e942c50 12388 {
c7303e41
APB
12389 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12390 return 0;
5e942c50 12391 }
c7303e41
APB
12392
12393 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12394 /* Only if it wasn't given a value upon initialization */
12395 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12396 /* If it was never assigned a value in this constructor */
12397 && !DECL_FIELD_FINAL_LIIC (lvalue))
12398 {
12399 /* Turn the locally assigned flag on, it will be checked later
12400 on to point out at discrepancies. */
12401 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12402 if (DECL_CLINIT_P (current_function_decl))
12403 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12404 return 0;
12405 }
12406
12407 /* Other problems should be reported right away. */
12408 parse_error_context
12409 (wfl, "Can't %sassign a value to the final variable `%s'",
12410 (FIELD_STATIC (lvalue) ? "re" : ""),
12411 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12412
12413 /* Note that static field can be initialized once and only once. */
12414 if (FIELD_STATIC (lvalue))
12415 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12416
12417 return 1;
5e942c50
APB
12418}
12419
12420/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12421 read. This is needed to avoid circularities in the implementation
12422 of these fields in libjava. */
12423
12424static tree
12425maybe_build_primttype_type_ref (rhs, wfl)
12426 tree rhs, wfl;
12427{
12428 tree to_return = NULL_TREE;
12429 tree rhs_type = TREE_TYPE (rhs);
12430 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12431 {
12432 tree n = TREE_OPERAND (rhs, 1);
12433 if (TREE_CODE (n) == VAR_DECL
12434 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12435 && rhs_type == class_ptr_type
12436 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12437 {
49f48c71 12438 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12439 if (!strncmp (self_name, "java.lang.", 10))
12440 to_return = build_primtype_type_ref (self_name);
12441 }
12442 }
12443 return (to_return ? to_return : rhs );
12444}
12445
e04a16fb
AG
12446/* 15.25 Assignment operators. */
12447
12448static tree
12449patch_assignment (node, wfl_op1, wfl_op2)
12450 tree node;
12451 tree wfl_op1;
12452 tree wfl_op2;
12453{
0a2138e2 12454 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12455 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12456 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12457 int error_found = 0;
12458 int lvalue_from_array = 0;
12459
c2952b01 12460 /* Can't assign to a (blank) final. */
5e942c50
APB
12461 if (check_final_assignment (lvalue, wfl_op1))
12462 error_found = 1;
e04a16fb
AG
12463
12464 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12465
12466 /* Lhs can be a named variable */
34f4db93 12467 if (JDECL_P (lvalue))
e04a16fb 12468 {
e04a16fb
AG
12469 lhs_type = TREE_TYPE (lvalue);
12470 }
12471 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12472 comment on reason why */
12473 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12474 {
12475 lhs_type = TREE_TYPE (lvalue);
12476 lvalue_from_array = 1;
12477 }
12478 /* Or a field access */
12479 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12480 lhs_type = TREE_TYPE (lvalue);
12481 /* Or a function return slot */
12482 else if (TREE_CODE (lvalue) == RESULT_DECL)
12483 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12484 /* Otherwise, we might want to try to write into an optimized static
12485 final, this is an of a different nature, reported further on. */
12486 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12487 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12488 {
6632dcdd 12489 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12490 {
12491 /* What we should do instead is resetting the all the flags
12492 previously set, exchange lvalue for llvalue and continue. */
12493 error_found = 1;
12494 return error_mark_node;
12495 }
12496 else
12497 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12498 }
12499 else
e04a16fb
AG
12500 {
12501 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12502 error_found = 1;
12503 }
12504
12505 rhs_type = TREE_TYPE (rhs);
b67d701b 12506 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12507 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12508
b67d701b 12509 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12510 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12511 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12512
12513 /* 15.25.2 If we have a compound assignment, convert RHS into the
12514 type of the LHS */
12515 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12516 new_rhs = convert (lhs_type, rhs);
12517
12518 /* Explicit cast required. This is an error */
12519 if (!new_rhs)
12520 {
c2e3db92
KG
12521 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12522 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12523 tree wfl;
12524 char operation [32]; /* Max size known */
12525
12526 /* If the assignment is part of a declaration, we use the WFL of
12527 the declared variable to point out the error and call it a
12528 declaration problem. If the assignment is a genuine =
12529 operator, we call is a operator `=' problem, otherwise we
12530 call it an assignment problem. In both of these last cases,
12531 we use the WFL of the operator to indicate the error. */
12532
12533 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12534 {
12535 wfl = wfl_op1;
12536 strcpy (operation, "declaration");
12537 }
12538 else
12539 {
12540 wfl = wfl_operator;
12541 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12542 strcpy (operation, "assignment");
12543 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12544 strcpy (operation, "`return'");
12545 else
12546 strcpy (operation, "`='");
12547 }
12548
1ebadc60 12549 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12550 parse_error_context
12551 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12552 operation, t1, t2);
1ebadc60 12553 else
781b0558 12554 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12555 operation, t1, t2);
e04a16fb
AG
12556 free (t1); free (t2);
12557 error_found = 1;
12558 }
12559
c877974e
APB
12560 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12561 if (new_rhs)
12562 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12563
e04a16fb
AG
12564 if (error_found)
12565 return error_mark_node;
12566
2622b947
APB
12567 /* 10.10: Array Store Exception runtime check */
12568 if (!flag_emit_class_files
e8fc7396 12569 && !flag_emit_xref
2622b947 12570 && lvalue_from_array
afc390b1 12571 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12572 {
12573 tree check;
12574 tree base = lvalue;
12575
12576 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12577 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12578 base = TREE_OPERAND (lvalue, 0);
12579 else
12580 {
12581 if (flag_bounds_check)
12582 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12583 else
12584 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12585 }
12586
12587 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12588 new_rhs = save_expr (new_rhs);
2622b947
APB
12589 check = build (CALL_EXPR, void_type_node,
12590 build_address_of (soft_checkarraystore_node),
12591 tree_cons (NULL_TREE, base,
12592 build_tree_list (NULL_TREE, new_rhs)),
12593 NULL_TREE);
12594 TREE_SIDE_EFFECTS (check) = 1;
12595
12596 /* We have to decide on an insertion point */
12597 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12598 {
12599 tree t;
12600 if (flag_bounds_check)
12601 {
12602 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12603 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12604 build (COMPOUND_EXPR, void_type_node, t, check);
12605 }
12606 else
12607 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12608 check, TREE_OPERAND (lvalue, 1));
12609 }
12610 else
12611 {
12612 /* Make sure the bound check will happen before the store check */
12613 if (flag_bounds_check)
12614 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12615 build (COMPOUND_EXPR, void_type_node,
12616 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12617 else
12618 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12619 }
12620 }
22eed1e6 12621
34d4df06
APB
12622 /* Final locals can be used as case values in switch
12623 statement. Prepare them for this eventuality. */
12624 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12625 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12626 && TREE_CONSTANT (new_rhs)
12627 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12628 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12629 )
12630 {
12631 TREE_CONSTANT (lvalue) = 1;
12632 DECL_INITIAL (lvalue) = new_rhs;
12633 }
12634
e04a16fb
AG
12635 TREE_OPERAND (node, 0) = lvalue;
12636 TREE_OPERAND (node, 1) = new_rhs;
12637 TREE_TYPE (node) = lhs_type;
12638 return node;
12639}
12640
b67d701b
PB
12641/* Check that type SOURCE can be cast into type DEST. If the cast
12642 can't occur at all, return 0 otherwise 1. This function is used to
12643 produce accurate error messages on the reasons why an assignment
12644 failed. */
e04a16fb 12645
b67d701b
PB
12646static tree
12647try_reference_assignconv (lhs_type, rhs)
12648 tree lhs_type, rhs;
e04a16fb 12649{
b67d701b
PB
12650 tree new_rhs = NULL_TREE;
12651 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12652
b67d701b
PB
12653 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12654 {
12655 /* `null' may be assigned to any reference type */
12656 if (rhs == null_pointer_node)
12657 new_rhs = null_pointer_node;
12658 /* Try the reference assignment conversion */
12659 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12660 new_rhs = rhs;
12661 /* This is a magic assignment that we process differently */
12662 else if (rhs == soft_exceptioninfo_call_node)
12663 new_rhs = rhs;
12664 }
12665 return new_rhs;
12666}
12667
12668/* Check that RHS can be converted into LHS_TYPE by the assignment
12669 conversion (5.2), for the cases of RHS being a builtin type. Return
12670 NULL_TREE if the conversion fails or if because RHS isn't of a
12671 builtin type. Return a converted RHS if the conversion is possible. */
12672
12673static tree
12674try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12675 tree wfl_op1, lhs_type, rhs;
12676{
12677 tree new_rhs = NULL_TREE;
12678 tree rhs_type = TREE_TYPE (rhs);
12679
7e51098e
TT
12680 /* Handle boolean specially. */
12681 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12682 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12683 {
12684 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12685 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12686 new_rhs = rhs;
12687 }
12688
5e942c50 12689 /* Zero accepted everywhere */
7e51098e 12690 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12691 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12692 && JPRIMITIVE_TYPE_P (rhs_type))
12693 new_rhs = convert (lhs_type, rhs);
12694
b67d701b
PB
12695 /* 5.1.1 Try Identity Conversion,
12696 5.1.2 Try Widening Primitive Conversion */
5e942c50 12697 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12698 new_rhs = convert (lhs_type, rhs);
12699
12700 /* Try a narrowing primitive conversion (5.1.3):
12701 - expression is a constant expression of type int AND
12702 - variable is byte, short or char AND
12703 - The value of the expression is representable in the type of the
12704 variable */
12705 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12706 && (lhs_type == byte_type_node || lhs_type == char_type_node
12707 || lhs_type == short_type_node))
12708 {
12709 if (int_fits_type_p (rhs, lhs_type))
12710 new_rhs = convert (lhs_type, rhs);
12711 else if (wfl_op1) /* Might be called with a NULL */
12712 parse_warning_context
7e51098e 12713 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12714 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12715 /* Reported a warning that will turn into an error further
12716 down, so we don't return */
12717 }
12718
12719 return new_rhs;
12720}
12721
12722/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12723 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12724 0 is the conversion test fails. This implements parts the method
12725 invocation convertion (5.3). */
12726
12727static int
12728valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12729 tree lhs_type, rhs_type;
12730{
acd663ee 12731 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12732 if (lhs_type == rhs_type)
12733 return 1;
12734
7e51098e
TT
12735 /* Reject non primitive types and boolean conversions. */
12736 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12737 return 0;
12738
acd663ee
APB
12739 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12740 than a char can't be converted into a char. Short can't too, but
12741 the < test below takes care of that */
b67d701b
PB
12742 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12743 return 0;
12744
5e942c50
APB
12745 /* Accept all promoted type here. Note, we can't use <= in the test
12746 below, because we still need to bounce out assignments of short
12747 to char and the likes */
12748 if (lhs_type == int_type_node
12749 && (rhs_type == promoted_byte_type_node
12750 || rhs_type == promoted_short_type_node
12751 || rhs_type == promoted_char_type_node
12752 || rhs_type == promoted_boolean_type_node))
12753 return 1;
12754
acd663ee
APB
12755 /* From here, an integral is widened if its precision is smaller
12756 than the precision of the LHS or if the LHS is a floating point
12757 type, or the RHS is a float and the RHS a double. */
12758 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12759 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12760 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12761 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12762 return 1;
12763
12764 return 0;
e04a16fb
AG
12765}
12766
12767/* Check that something of SOURCE type can be assigned or cast to
12768 something of DEST type at runtime. Return 1 if the operation is
12769 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12770 were SOURCE is cast into DEST, which borrows a lot of the
12771 assignment check. */
12772
12773static int
12774valid_ref_assignconv_cast_p (source, dest, cast)
12775 tree source;
12776 tree dest;
12777 int cast;
12778{
09ed0f70
APB
12779 /* SOURCE or DEST might be null if not from a declared entity. */
12780 if (!source || !dest)
12781 return 0;
5e942c50
APB
12782 if (JNULLP_TYPE_P (source))
12783 return 1;
e04a16fb
AG
12784 if (TREE_CODE (source) == POINTER_TYPE)
12785 source = TREE_TYPE (source);
12786 if (TREE_CODE (dest) == POINTER_TYPE)
12787 dest = TREE_TYPE (dest);
c1eacb70
BM
12788
12789 /* If source and dest are being compiled from bytecode, they may need to
12790 be loaded. */
12791 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12792 {
12793 load_class (source, 1);
12794 safe_layout_class (source);
12795 }
12796 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12797 {
12798 load_class (dest, 1);
12799 safe_layout_class (dest);
12800 }
12801
e04a16fb
AG
12802 /* Case where SOURCE is a class type */
12803 if (TYPE_CLASS_P (source))
12804 {
12805 if (TYPE_CLASS_P (dest))
c2952b01
APB
12806 return (source == dest
12807 || inherits_from_p (source, dest)
c2952b01 12808 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12809 if (TYPE_INTERFACE_P (dest))
12810 {
12811 /* If doing a cast and SOURCE is final, the operation is
12812 always correct a compile time (because even if SOURCE
12813 does not implement DEST, a subclass of SOURCE might). */
12814 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12815 return 1;
12816 /* Otherwise, SOURCE must implement DEST */
12817 return interface_of_p (dest, source);
12818 }
12819 /* DEST is an array, cast permited if SOURCE is of Object type */
12820 return (cast && source == object_type_node ? 1 : 0);
12821 }
12822 if (TYPE_INTERFACE_P (source))
12823 {
12824 if (TYPE_CLASS_P (dest))
12825 {
12826 /* If not casting, DEST must be the Object type */
12827 if (!cast)
12828 return dest == object_type_node;
12829 /* We're doing a cast. The cast is always valid is class
12830 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12831 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12832 return 1;
12833 else
12834 return interface_of_p (source, dest);
12835 }
12836 if (TYPE_INTERFACE_P (dest))
12837 {
12838 /* If doing a cast, then if SOURCE and DEST contain method
12839 with the same signature but different return type, then
12840 this is a (compile time) error */
12841 if (cast)
12842 {
12843 tree method_source, method_dest;
12844 tree source_type;
0a2138e2 12845 tree source_sig;
e04a16fb
AG
12846 tree source_name;
12847 for (method_source = TYPE_METHODS (source); method_source;
12848 method_source = TREE_CHAIN (method_source))
12849 {
12850 source_sig =
12851 build_java_argument_signature (TREE_TYPE (method_source));
12852 source_type = TREE_TYPE (TREE_TYPE (method_source));
12853 source_name = DECL_NAME (method_source);
12854 for (method_dest = TYPE_METHODS (dest);
12855 method_dest; method_dest = TREE_CHAIN (method_dest))
12856 if (source_sig ==
12857 build_java_argument_signature (TREE_TYPE (method_dest))
12858 && source_name == DECL_NAME (method_dest)
12859 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12860 return 0;
12861 }
12862 return 1;
12863 }
12864 else
12865 return source == dest || interface_of_p (dest, source);
12866 }
ee17a290
TT
12867 else
12868 {
12869 /* Array */
12870 return (cast
12871 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12872 || (DECL_NAME (TYPE_NAME (source))
12873 == java_io_serializable)));
12874 }
e04a16fb
AG
12875 }
12876 if (TYPE_ARRAY_P (source))
12877 {
12878 if (TYPE_CLASS_P (dest))
12879 return dest == object_type_node;
09ed0f70 12880 /* Can't cast an array to an interface unless the interface is
ee17a290 12881 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12882 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12883 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12884 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12885 else /* Arrays */
12886 {
12887 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12888 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12889
b9f7e36c
APB
12890 /* In case of severe errors, they turn out null */
12891 if (!dest_element_type || !source_element_type)
12892 return 0;
e04a16fb
AG
12893 if (source_element_type == dest_element_type)
12894 return 1;
12895 return valid_ref_assignconv_cast_p (source_element_type,
12896 dest_element_type, cast);
12897 }
12898 return 0;
12899 }
12900 return 0;
12901}
12902
b67d701b
PB
12903static int
12904valid_cast_to_p (source, dest)
12905 tree source;
12906 tree dest;
12907{
12908 if (TREE_CODE (source) == POINTER_TYPE)
12909 source = TREE_TYPE (source);
12910 if (TREE_CODE (dest) == POINTER_TYPE)
12911 dest = TREE_TYPE (dest);
12912
12913 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12914 return valid_ref_assignconv_cast_p (source, dest, 1);
12915
12916 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12917 return 1;
12918
7e51098e
TT
12919 else if (TREE_CODE (source) == BOOLEAN_TYPE
12920 && TREE_CODE (dest) == BOOLEAN_TYPE)
12921 return 1;
12922
b67d701b
PB
12923 return 0;
12924}
12925
15fdcfe9
PB
12926static tree
12927do_unary_numeric_promotion (arg)
12928 tree arg;
12929{
12930 tree type = TREE_TYPE (arg);
7e51098e
TT
12931 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12932 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12933 arg = convert (int_type_node, arg);
12934 return arg;
12935}
12936
acd663ee
APB
12937/* Return a non zero value if SOURCE can be converted into DEST using
12938 the method invocation conversion rule (5.3). */
b67d701b
PB
12939static int
12940valid_method_invocation_conversion_p (dest, source)
12941 tree dest, source;
12942{
e3884b71 12943 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12944 && valid_builtin_assignconv_identity_widening_p (dest, source))
12945 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12946 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12947 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12948}
12949
e04a16fb
AG
12950/* Build an incomplete binop expression. */
12951
12952static tree
12953build_binop (op, op_location, op1, op2)
12954 enum tree_code op;
12955 int op_location;
12956 tree op1, op2;
12957{
5e942c50 12958 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12959 TREE_SIDE_EFFECTS (binop) = 1;
12960 /* Store the location of the operator, for better error report. The
12961 string of the operator will be rebuild based on the OP value. */
12962 EXPR_WFL_LINECOL (binop) = op_location;
12963 return binop;
12964}
12965
12966/* Build the string of the operator retained by NODE. If NODE is part
12967 of a compound expression, add an '=' at the end of the string. This
12968 function is called when an error needs to be reported on an
12969 operator. The string is returned as a pointer to a static character
12970 buffer. */
12971
12972static char *
12973operator_string (node)
12974 tree node;
12975{
12976#define BUILD_OPERATOR_STRING(S) \
12977 { \
12978 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12979 return buffer; \
12980 }
12981
12982 static char buffer [10];
12983 switch (TREE_CODE (node))
12984 {
12985 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12986 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12987 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12988 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12989 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12990 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12991 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12992 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12993 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12994 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12995 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12996 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12997 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12998 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12999 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13000 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13001 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13002 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13003 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 13004 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
13005 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13006 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13007 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13008 case PREINCREMENT_EXPR: /* Fall through */
13009 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13010 case PREDECREMENT_EXPR: /* Fall through */
13011 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13012 default:
13013 fatal ("unregistered operator %s - operator_string",
13014 tree_code_name [TREE_CODE (node)]);
13015 }
13016 return NULL;
13017#undef BUILD_OPERATOR_STRING
13018}
13019
5cbdba64
APB
13020/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13021
13022static int
13023java_decl_equiv (var_acc1, var_acc2)
13024 tree var_acc1, var_acc2;
13025{
13026 if (JDECL_P (var_acc1))
13027 return (var_acc1 == var_acc2);
13028
13029 return (TREE_CODE (var_acc1) == COMPONENT_REF
13030 && TREE_CODE (var_acc2) == COMPONENT_REF
13031 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13032 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13033 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13034}
13035
13036/* Return a non zero value if CODE is one of the operators that can be
13037 used in conjunction with the `=' operator in a compound assignment. */
13038
13039static int
13040binop_compound_p (code)
13041 enum tree_code code;
13042{
13043 int i;
13044 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13045 if (binop_lookup [i] == code)
13046 break;
13047
13048 return i < BINOP_COMPOUND_CANDIDATES;
13049}
13050
13051/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13052
13053static tree
13054java_refold (t)
13055 tree t;
13056{
13057 tree c, b, ns, decl;
13058
13059 if (TREE_CODE (t) != MODIFY_EXPR)
13060 return t;
13061
13062 c = TREE_OPERAND (t, 1);
13063 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13064 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13065 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13066 return t;
13067
13068 /* Now the left branch of the binary operator. */
13069 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13070 if (! (b && TREE_CODE (b) == NOP_EXPR
13071 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13072 return t;
13073
13074 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13075 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13076 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13077 return t;
13078
13079 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13080 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13081 /* It's got to be the an equivalent decl */
13082 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13083 {
13084 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13085 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13086 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13087 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13088 /* Change the right part of the BINOP_EXPR */
13089 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13090 }
13091
13092 return t;
13093}
13094
e04a16fb
AG
13095/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13096 errors but we modify NODE so that it contains the type computed
13097 according to the expression, when it's fixed. Otherwise, we write
13098 error_mark_node as the type. It allows us to further the analysis
13099 of remaining nodes and detects more errors in certain cases. */
13100
13101static tree
13102patch_binop (node, wfl_op1, wfl_op2)
13103 tree node;
13104 tree wfl_op1;
13105 tree wfl_op2;
13106{
13107 tree op1 = TREE_OPERAND (node, 0);
13108 tree op2 = TREE_OPERAND (node, 1);
13109 tree op1_type = TREE_TYPE (op1);
13110 tree op2_type = TREE_TYPE (op2);
48a840d9 13111 tree prom_type = NULL_TREE, cn;
e04a16fb 13112 int code = TREE_CODE (node);
b67d701b 13113
e04a16fb
AG
13114 /* If 1, tell the routine that we have to return error_mark_node
13115 after checking for the initialization of the RHS */
13116 int error_found = 0;
13117
e04a16fb
AG
13118 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13119
e04a16fb
AG
13120 switch (code)
13121 {
13122 /* 15.16 Multiplicative operators */
13123 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13124 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13125 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13126 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13127 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13128 {
7e51098e 13129 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13130 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13131 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13132 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13133 TREE_TYPE (node) = error_mark_node;
13134 error_found = 1;
13135 break;
13136 }
13137 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13138 /* Change the division operator if necessary */
13139 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13140 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13141
aa4759c1
AH
13142 if (TREE_CODE (prom_type) == INTEGER_TYPE
13143 && flag_use_divide_subroutine
13144 && ! flag_emit_class_files
13145 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13146 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13147
0b4d333e
APB
13148 /* This one is more complicated. FLOATs are processed by a
13149 function call to soft_fmod. Duplicate the value of the
13150 COMPOUND_ASSIGN_P flag. */
e04a16fb 13151 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13152 {
13153 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13154 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13155 TREE_SIDE_EFFECTS (mod)
13156 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13157 return mod;
13158 }
e04a16fb
AG
13159 break;
13160
13161 /* 15.17 Additive Operators */
13162 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13163
13164 /* Operation is valid if either one argument is a string
13165 constant, a String object or a StringBuffer crafted for the
13166 purpose of the a previous usage of the String concatenation
13167 operator */
13168
13169 if (TREE_CODE (op1) == STRING_CST
13170 || TREE_CODE (op2) == STRING_CST
13171 || JSTRING_TYPE_P (op1_type)
13172 || JSTRING_TYPE_P (op2_type)
13173 || IS_CRAFTED_STRING_BUFFER_P (op1)
13174 || IS_CRAFTED_STRING_BUFFER_P (op2))
13175 return build_string_concatenation (op1, op2);
13176
e04a16fb
AG
13177 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13178 Numeric Types */
7e51098e 13179 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13180 {
7e51098e 13181 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13182 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13183 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13184 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13185 TREE_TYPE (node) = error_mark_node;
13186 error_found = 1;
13187 break;
13188 }
13189 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13190 break;
13191
13192 /* 15.18 Shift Operators */
13193 case LSHIFT_EXPR:
13194 case RSHIFT_EXPR:
13195 case URSHIFT_EXPR:
13196 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13197 {
13198 if (!JINTEGRAL_TYPE_P (op1_type))
13199 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13200 else
1ebadc60 13201 {
7e51098e 13202 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13203 parse_error_context (wfl_operator,
781b0558 13204 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13205 operator_string (node),
13206 lang_printable_name (op2_type, 0));
13207 else
781b0558
KG
13208 parse_error_context (wfl_operator,
13209 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13210 operator_string (node),
13211 lang_printable_name (op2_type, 0));
13212 }
e04a16fb
AG
13213 TREE_TYPE (node) = error_mark_node;
13214 error_found = 1;
13215 break;
13216 }
13217
13218 /* Unary numeric promotion (5.6.1) is performed on each operand
13219 separatly */
15fdcfe9
PB
13220 op1 = do_unary_numeric_promotion (op1);
13221 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13222
13223 /* The type of the shift expression is the type of the promoted
13224 type of the left-hand operand */
13225 prom_type = TREE_TYPE (op1);
13226
c2952b01
APB
13227 /* Shift int only up to 0x1f and long up to 0x3f */
13228 if (prom_type == int_type_node)
13229 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13230 build_int_2 (0x1f, 0)));
13231 else
13232 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13233 build_int_2 (0x3f, 0)));
e04a16fb
AG
13234
13235 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13236 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13237 {
0b4d333e 13238 tree to_return;
73333a87
AH
13239 tree utype = unsigned_type (prom_type);
13240 op1 = convert (utype, op1);
e04a16fb 13241 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13242 TREE_OPERAND (node, 0) = op1;
13243 TREE_OPERAND (node, 1) = op2;
13244 TREE_TYPE (node) = utype;
0b4d333e
APB
13245 to_return = convert (prom_type, node);
13246 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13247 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13248 TREE_SIDE_EFFECTS (to_return)
13249 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13250 return to_return;
e04a16fb
AG
13251 }
13252 break;
5e942c50
APB
13253
13254 /* 15.19.1 Type Comparison Operator instaceof */
13255 case INSTANCEOF_EXPR:
13256
13257 TREE_TYPE (node) = boolean_type_node;
13258
13259 if (!(op2_type = resolve_type_during_patch (op2)))
13260 return error_mark_node;
13261
13262 /* The first operand must be a reference type or the null type */
13263 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13264 error_found = 1; /* Error reported further below */
13265
13266 /* The second operand must be a reference type */
13267 if (!JREFERENCE_TYPE_P (op2_type))
13268 {
13269 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13270 parse_error_context
13271 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13272 lang_printable_name (op2_type, 0));
13273 error_found = 1;
13274 }
13275
13276 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13277 {
13278 /* If the first operand is null, the result is always false */
13279 if (op1 == null_pointer_node)
13280 return boolean_false_node;
15fdcfe9
PB
13281 else if (flag_emit_class_files)
13282 {
13283 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13284 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13285 return node;
13286 }
5e942c50
APB
13287 /* Otherwise we have to invoke instance of to figure it out */
13288 else
67db0ce7 13289 return build_instanceof (op1, op2_type);
5e942c50
APB
13290 }
13291 /* There is no way the expression operand can be an instance of
13292 the type operand. This is a compile time error. */
13293 else
13294 {
c2e3db92 13295 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13296 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13297 parse_error_context
13298 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13299 t1, lang_printable_name (op2_type, 0));
13300 free (t1);
13301 error_found = 1;
13302 }
e04a16fb 13303
5e942c50 13304 break;
e04a16fb
AG
13305
13306 /* 15.21 Bitwise and Logical Operators */
13307 case BIT_AND_EXPR:
13308 case BIT_XOR_EXPR:
13309 case BIT_IOR_EXPR:
13310 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13311 /* Binary numeric promotion is performed on both operand and the
13312 expression retain that type */
13313 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13314
13315 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13316 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13317 /* The type of the bitwise operator expression is BOOLEAN */
13318 prom_type = boolean_type_node;
13319 else
13320 {
13321 if (!JINTEGRAL_TYPE_P (op1_type))
13322 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13323 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13324 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13325 TREE_TYPE (node) = error_mark_node;
13326 error_found = 1;
13327 /* Insert a break here if adding thing before the switch's
13328 break for this case */
13329 }
13330 break;
13331
13332 /* 15.22 Conditional-And Operator */
13333 case TRUTH_ANDIF_EXPR:
13334 /* 15.23 Conditional-Or Operator */
13335 case TRUTH_ORIF_EXPR:
13336 /* Operands must be of BOOLEAN type */
13337 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13338 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13339 {
13340 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13341 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13342 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13343 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13344 TREE_TYPE (node) = boolean_type_node;
13345 error_found = 1;
13346 break;
13347 }
13348 /* The type of the conditional operators is BOOLEAN */
13349 prom_type = boolean_type_node;
13350 break;
13351
13352 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13353 case LT_EXPR:
13354 case GT_EXPR:
13355 case LE_EXPR:
13356 case GE_EXPR:
13357 /* The type of each of the operands must be a primitive numeric
13358 type */
13359 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13360 {
13361 if (!JNUMERIC_TYPE_P (op1_type))
13362 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13363 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13364 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13365 TREE_TYPE (node) = boolean_type_node;
13366 error_found = 1;
13367 break;
13368 }
13369 /* Binary numeric promotion is performed on the operands */
13370 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13371 /* The type of the relation expression is always BOOLEAN */
13372 prom_type = boolean_type_node;
13373 break;
13374
13375 /* 15.20 Equality Operator */
13376 case EQ_EXPR:
13377 case NE_EXPR:
48a840d9
APB
13378 /* It's time for us to patch the strings. */
13379 if ((cn = patch_string (op1)))
13380 {
13381 op1 = cn;
13382 op1_type = TREE_TYPE (op1);
13383 }
13384 if ((cn = patch_string (op2)))
13385 {
13386 op2 = cn;
13387 op2_type = TREE_TYPE (op2);
13388 }
13389
e04a16fb
AG
13390 /* 15.20.1 Numerical Equality Operators == and != */
13391 /* Binary numeric promotion is performed on the operands */
5e942c50 13392 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13393 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13394
13395 /* 15.20.2 Boolean Equality Operators == and != */
13396 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13397 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13398 ; /* Nothing to do here */
13399
13400 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13401 /* Types have to be either references or the null type. If
13402 they're references, it must be possible to convert either
13403 type to the other by casting conversion. */
b9f7e36c
APB
13404 else if (op1 == null_pointer_node || op2 == null_pointer_node
13405 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13406 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13407 || valid_ref_assignconv_cast_p (op2_type,
13408 op1_type, 1))))
e04a16fb
AG
13409 ; /* Nothing to do here */
13410
13411 /* Else we have an error figure what can't be converted into
13412 what and report the error */
13413 else
13414 {
13415 char *t1;
c2e3db92 13416 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13417 parse_error_context
781b0558
KG
13418 (wfl_operator,
13419 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13420 operator_string (node), t1,
0a2138e2 13421 lang_printable_name (op2_type, 0));
e04a16fb
AG
13422 free (t1);
13423 TREE_TYPE (node) = boolean_type_node;
13424 error_found = 1;
13425 break;
13426 }
13427 prom_type = boolean_type_node;
13428 break;
13429 }
13430
e04a16fb
AG
13431 if (error_found)
13432 return error_mark_node;
13433
13434 TREE_OPERAND (node, 0) = op1;
13435 TREE_OPERAND (node, 1) = op2;
13436 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13437 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13438
ce6e9147
APB
13439 if (flag_emit_xref)
13440 return node;
13441
d1472141
PB
13442 /* fold does not respect side-effect order as required for Java but not C.
13443 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13444 * bytecode.
13445 */
13446 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13447 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13448 node = fold (node);
13449 return node;
e04a16fb
AG
13450}
13451
b67d701b
PB
13452/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13453 zero value, the value of CSTE comes after the valude of STRING */
13454
13455static tree
13456do_merge_string_cste (cste, string, string_len, after)
13457 tree cste;
49f48c71 13458 const char *string;
b67d701b
PB
13459 int string_len, after;
13460{
49f48c71 13461 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13462 int old_len = TREE_STRING_LENGTH (cste);
13463 int len = old_len + string_len;
520a57c8 13464 char *new = alloca (len+1);
354e99ce 13465
b67d701b
PB
13466 if (after)
13467 {
354e99ce
APB
13468 memcpy (new, string, string_len);
13469 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13470 }
13471 else
13472 {
354e99ce
APB
13473 memcpy (new, old, old_len);
13474 memcpy (&new [old_len], string, string_len);
b67d701b 13475 }
354e99ce 13476 new [len] = '\0';
520a57c8 13477 return build_string (len, new);
b67d701b
PB
13478}
13479
13480/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13481 new STRING_CST on success, NULL_TREE on failure */
13482
13483static tree
13484merge_string_cste (op1, op2, after)
13485 tree op1, op2;
13486 int after;
13487{
13488 /* Handle two string constants right away */
13489 if (TREE_CODE (op2) == STRING_CST)
13490 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13491 TREE_STRING_LENGTH (op2), after);
13492
13493 /* Reasonable integer constant can be treated right away */
13494 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13495 {
49f48c71
KG
13496 static const char *boolean_true = "true";
13497 static const char *boolean_false = "false";
13498 static const char *null_pointer = "null";
b67d701b 13499 char ch[3];
49f48c71 13500 const char *string;
b67d701b
PB
13501
13502 if (op2 == boolean_true_node)
13503 string = boolean_true;
13504 else if (op2 == boolean_false_node)
13505 string = boolean_false;
13506 else if (op2 == null_pointer_node)
13507 string = null_pointer;
13508 else if (TREE_TYPE (op2) == char_type_node)
13509 {
13510 ch[0] = (char )TREE_INT_CST_LOW (op2);
13511 ch[1] = '\0';
13512 string = ch;
13513 }
13514 else
13515 string = print_int_node (op2);
13516
13517 return do_merge_string_cste (op1, string, strlen (string), after);
13518 }
13519 return NULL_TREE;
13520}
13521
13522/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13523 has to be a STRING_CST and the other part must be a STRING_CST or a
13524 INTEGRAL constant. Return a new STRING_CST if the operation
13525 succeed, NULL_TREE otherwise.
13526
13527 If the case we want to optimize for space, we might want to return
13528 NULL_TREE for each invocation of this routine. FIXME */
13529
13530static tree
13531string_constant_concatenation (op1, op2)
13532 tree op1, op2;
13533{
13534 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13535 {
0a2138e2 13536 tree string, rest;
b67d701b
PB
13537 int invert;
13538
13539 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13540 rest = (string == op1 ? op2 : op1);
13541 invert = (string == op1 ? 0 : 1 );
13542
13543 /* Walk REST, only if it looks reasonable */
13544 if (TREE_CODE (rest) != STRING_CST
13545 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13546 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13547 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13548 {
13549 rest = java_complete_tree (rest);
13550 if (rest == error_mark_node)
13551 return error_mark_node;
13552 rest = fold (rest);
13553 }
13554 return merge_string_cste (string, rest, invert);
13555 }
13556 return NULL_TREE;
13557}
13558
13559/* Implement the `+' operator. Does static optimization if possible,
13560 otherwise create (if necessary) and append elements to a
13561 StringBuffer. The StringBuffer will be carried around until it is
13562 used for a function call or an assignment. Then toString() will be
13563 called on it to turn it into a String object. */
13564
13565static tree
13566build_string_concatenation (op1, op2)
13567 tree op1, op2;
13568{
13569 tree result;
dc0b3eff 13570 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13571
13572 if (flag_emit_xref)
13573 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13574
13575 /* Try to do some static optimization */
13576 if ((result = string_constant_concatenation (op1, op2)))
13577 return result;
13578
c0d87ff6
PB
13579 /* Discard empty strings on either side of the expression */
13580 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13581 {
13582 op1 = op2;
13583 op2 = NULL_TREE;
13584 }
c0d87ff6 13585 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13586 op2 = NULL_TREE;
b67d701b 13587
acd663ee 13588 /* If operands are string constant, turn then into object references */
b67d701b
PB
13589 if (TREE_CODE (op1) == STRING_CST)
13590 op1 = patch_string_cst (op1);
acd663ee 13591 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13592 op2 = patch_string_cst (op2);
13593
acd663ee
APB
13594 /* If either one of the constant is null and the other non null
13595 operand is a String object, return it. */
13596 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13597 return op1;
13598
b67d701b
PB
13599 /* If OP1 isn't already a StringBuffer, create and
13600 initialize a new one */
13601 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13602 {
13603 /* Two solutions here:
c52b5771
AG
13604 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13605 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13606 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13607 op1 = BUILD_STRING_BUFFER (op1);
13608 else
13609 {
13610 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13611 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13612 }
13613 }
13614
acd663ee
APB
13615 if (op2)
13616 {
13617 /* OP1 is no longer the last node holding a crafted StringBuffer */
13618 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13619 /* Create a node for `{new...,xxx}.append (op2)' */
13620 if (op2)
13621 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13622 }
13623
b67d701b
PB
13624 /* Mark the last node holding a crafted StringBuffer */
13625 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13626
13627 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13628 return op1;
13629}
13630
13631/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13632 StringBuffer. If no string were found to be patched, return
13633 NULL. */
13634
13635static tree
13636patch_string (node)
13637 tree node;
13638{
1179ebc2
APB
13639 if (node == error_mark_node)
13640 return error_mark_node;
b67d701b
PB
13641 if (TREE_CODE (node) == STRING_CST)
13642 return patch_string_cst (node);
13643 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13644 {
c877974e 13645 int saved = ctxp->explicit_constructor_p;
b67d701b 13646 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13647 tree ret;
13648 /* Temporary disable forbid the use of `this'. */
13649 ctxp->explicit_constructor_p = 0;
13650 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13651 /* String concatenation arguments must be evaluated in order too. */
13652 ret = force_evaluation_order (ret);
c877974e
APB
13653 /* Restore it at its previous value */
13654 ctxp->explicit_constructor_p = saved;
13655 return ret;
b67d701b
PB
13656 }
13657 return NULL_TREE;
13658}
13659
13660/* Build the internal representation of a string constant. */
13661
13662static tree
13663patch_string_cst (node)
13664 tree node;
13665{
13666 int location;
15fdcfe9
PB
13667 if (! flag_emit_class_files)
13668 {
15fdcfe9
PB
13669 node = get_identifier (TREE_STRING_POINTER (node));
13670 location = alloc_name_constant (CONSTANT_String, node);
13671 node = build_ref_from_constant_pool (location);
13672 }
cd9643f7 13673 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13674 TREE_CONSTANT (node) = 1;
13675 return node;
13676}
13677
13678/* Build an incomplete unary operator expression. */
e04a16fb
AG
13679
13680static tree
13681build_unaryop (op_token, op_location, op1)
13682 int op_token, op_location;
13683 tree op1;
13684{
13685 enum tree_code op;
13686 tree unaryop;
13687 switch (op_token)
13688 {
b67d701b 13689 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13690 case MINUS_TK: op = NEGATE_EXPR; break;
13691 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13692 case NOT_TK: op = BIT_NOT_EXPR; break;
13693 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13694 op_token);
13695 }
13696
13697 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13698 TREE_SIDE_EFFECTS (unaryop) = 1;
13699 /* Store the location of the operator, for better error report. The
13700 string of the operator will be rebuild based on the OP value. */
13701 EXPR_WFL_LINECOL (unaryop) = op_location;
13702 return unaryop;
13703}
13704
13705/* Special case for the ++/-- operators, since they require an extra
13706 argument to build, which is set to NULL and patched
13707 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13708
13709static tree
13710build_incdec (op_token, op_location, op1, is_post_p)
13711 int op_token, op_location;
13712 tree op1;
13713 int is_post_p;
13714{
13715 static enum tree_code lookup [2][2] =
13716 {
13717 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13718 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13719 };
13720 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13721 NULL_TREE, op1, NULL_TREE);
13722 TREE_SIDE_EFFECTS (node) = 1;
13723 /* Store the location of the operator, for better error report. The
13724 string of the operator will be rebuild based on the OP value. */
13725 EXPR_WFL_LINECOL (node) = op_location;
13726 return node;
13727}
13728
13729/* Build an incomplete cast operator, based on the use of the
13730 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13731 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13732 though its type is already set. */
13733
13734static tree
13735build_cast (location, type, exp)
13736 int location;
13737 tree type, exp;
13738{
13739 tree node = build1 (CONVERT_EXPR, type, exp);
13740 EXPR_WFL_LINECOL (node) = location;
13741 return node;
13742}
13743
c2952b01
APB
13744/* Build an incomplete class reference operator. */
13745static tree
13746build_incomplete_class_ref (location, class_name)
13747 int location;
13748 tree class_name;
13749{
13750 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13751 EXPR_WFL_LINECOL (node) = location;
13752 return node;
13753}
13754
13755/* Complete an incomplete class reference operator. */
13756static tree
13757patch_incomplete_class_ref (node)
13758 tree node;
13759{
13760 tree type = TREE_OPERAND (node, 0);
13761 tree ref_type;
13762
13763 if (!(ref_type = resolve_type_during_patch (type)))
13764 return error_mark_node;
13765
165f37bc 13766 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13767 {
13768 /* A class referenced by `foo.class' is initialized. */
13769 return build_class_init (ref_type, build_class_ref (ref_type));
13770 }
165f37bc
APB
13771
13772 /* If we're emitting class files and we have to deal with non
13773 primitive types, we invoke (and consider generating) the
13774 synthetic static method `class$'. */
13775 if (!TYPE_DOT_CLASS (current_class))
13776 build_dot_class_method (current_class);
f0f3a777 13777 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13778 return java_complete_tree (ref_type);
c2952b01
APB
13779}
13780
e04a16fb
AG
13781/* 15.14 Unary operators. We return error_mark_node in case of error,
13782 but preserve the type of NODE if the type is fixed. */
13783
13784static tree
13785patch_unaryop (node, wfl_op)
13786 tree node;
13787 tree wfl_op;
13788{
13789 tree op = TREE_OPERAND (node, 0);
13790 tree op_type = TREE_TYPE (op);
ab3a6dd6 13791 tree prom_type = NULL_TREE, value, decl;
c2952b01 13792 int outer_field_flag = 0;
e04a16fb
AG
13793 int code = TREE_CODE (node);
13794 int error_found = 0;
13795
13796 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13797
13798 switch (code)
13799 {
13800 /* 15.13.2 Postfix Increment Operator ++ */
13801 case POSTINCREMENT_EXPR:
13802 /* 15.13.3 Postfix Increment Operator -- */
13803 case POSTDECREMENT_EXPR:
13804 /* 15.14.1 Prefix Increment Operator ++ */
13805 case PREINCREMENT_EXPR:
13806 /* 15.14.2 Prefix Decrement Operator -- */
13807 case PREDECREMENT_EXPR:
5cbdba64 13808 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13809 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13810 /* We might be trying to change an outer field accessed using
13811 access method. */
13812 if (outer_field_flag)
13813 {
13814 /* Retrieve the decl of the field we're trying to access. We
13815 do that by first retrieving the function we would call to
13816 access the field. It has been already verified that this
13817 field isn't final */
13818 if (flag_emit_class_files)
13819 decl = TREE_OPERAND (op, 0);
13820 else
13821 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13822 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13823 }
b3edebcf 13824 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13825 else if (!JDECL_P (decl)
b3edebcf
APB
13826 && TREE_CODE (decl) != COMPONENT_REF
13827 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13828 && TREE_CODE (decl) != INDIRECT_REF
13829 && !(TREE_CODE (decl) == COMPOUND_EXPR
13830 && TREE_OPERAND (decl, 1)
13831 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13832 {
5e942c50
APB
13833 tree lvalue;
13834 /* Before screaming, check that we're not in fact trying to
13835 increment a optimized static final access, in which case
13836 we issue an different error message. */
13837 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13838 && resolve_expression_name (wfl_op, &lvalue)
13839 && check_final_assignment (lvalue, wfl_op)))
13840 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13841 operator_string (node));
e04a16fb
AG
13842 TREE_TYPE (node) = error_mark_node;
13843 error_found = 1;
13844 }
c2952b01
APB
13845
13846 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13847 error_found = 1;
13848
e04a16fb
AG
13849 /* From now on, we know that op if a variable and that it has a
13850 valid wfl. We use wfl_op to locate errors related to the
13851 ++/-- operand. */
13852 else if (!JNUMERIC_TYPE_P (op_type))
13853 {
13854 parse_error_context
13855 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13856 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13857 TREE_TYPE (node) = error_mark_node;
13858 error_found = 1;
13859 }
13860 else
13861 {
4a5f66c3 13862 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13863 both operands, if really necessary */
13864 if (JINTEGRAL_TYPE_P (op_type))
13865 {
13866 value = build_int_2 (1, 0);
13867 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13868 }
13869 else
13870 {
13871 value = build_int_2 (1, 0);
13872 TREE_TYPE (node) =
13873 binary_numeric_promotion (op_type,
13874 TREE_TYPE (value), &op, &value);
13875 }
c2952b01
APB
13876
13877 /* We remember we might be accessing an outer field */
13878 if (outer_field_flag)
13879 {
13880 /* We re-generate an access to the field */
13881 value = build (PLUS_EXPR, TREE_TYPE (op),
13882 build_outer_field_access (wfl_op, decl), value);
13883
13884 /* And we patch the original access$() into a write
13885 with plus_op as a rhs */
13886 return outer_field_access_fix (node, op, value);
13887 }
13888
5cbdba64 13889 /* And write back into the node. */
4a5f66c3 13890 TREE_OPERAND (node, 0) = op;
e04a16fb 13891 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13892 /* Convert the overall back into its original type, if
13893 necessary, and return */
13894 if (JINTEGRAL_TYPE_P (op_type))
13895 return fold (node);
13896 else
13897 return fold (convert (op_type, node));
e04a16fb
AG
13898 }
13899 break;
13900
13901 /* 15.14.3 Unary Plus Operator + */
b67d701b 13902 case UNARY_PLUS_EXPR:
e04a16fb
AG
13903 /* 15.14.4 Unary Minus Operator - */
13904 case NEGATE_EXPR:
13905 if (!JNUMERIC_TYPE_P (op_type))
13906 {
13907 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13908 TREE_TYPE (node) = error_mark_node;
13909 error_found = 1;
13910 }
13911 /* Unary numeric promotion is performed on operand */
13912 else
13913 {
15fdcfe9
PB
13914 op = do_unary_numeric_promotion (op);
13915 prom_type = TREE_TYPE (op);
b67d701b 13916 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13917 return fold (op);
e04a16fb
AG
13918 }
13919 break;
13920
13921 /* 15.14.5 Bitwise Complement Operator ~ */
13922 case BIT_NOT_EXPR:
13923 if (!JINTEGRAL_TYPE_P (op_type))
13924 {
13925 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13926 TREE_TYPE (node) = error_mark_node;
13927 error_found = 1;
13928 }
13929 else
13930 {
15fdcfe9
PB
13931 op = do_unary_numeric_promotion (op);
13932 prom_type = TREE_TYPE (op);
e04a16fb
AG
13933 }
13934 break;
13935
13936 /* 15.14.6 Logical Complement Operator ! */
13937 case TRUTH_NOT_EXPR:
13938 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13939 {
13940 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13941 /* But the type is known. We will report an error if further
13942 attempt of a assignment is made with this rhs */
e04a16fb
AG
13943 TREE_TYPE (node) = boolean_type_node;
13944 error_found = 1;
13945 }
13946 else
13947 prom_type = boolean_type_node;
13948 break;
13949
13950 /* 15.15 Cast Expression */
13951 case CONVERT_EXPR:
0a2138e2 13952 value = patch_cast (node, wfl_operator);
e04a16fb 13953 if (value == error_mark_node)
c877974e
APB
13954 {
13955 /* If this cast is part of an assignment, we tell the code
13956 that deals with it not to complain about a mismatch,
13957 because things have been cast, anyways */
13958 TREE_TYPE (node) = error_mark_node;
13959 error_found = 1;
13960 }
13961 else
dc0b3eff
PB
13962 {
13963 value = fold (value);
13964 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13965 return value;
13966 }
e04a16fb
AG
13967 break;
13968 }
13969
e04a16fb
AG
13970 if (error_found)
13971 return error_mark_node;
4a5f66c3
APB
13972
13973 /* There are cases where node has been replaced by something else
13974 and we don't end up returning here: UNARY_PLUS_EXPR,
13975 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13976 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13977 TREE_TYPE (node) = prom_type;
dc0b3eff 13978 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13979 return fold (node);
13980}
13981
13982/* Generic type resolution that sometimes takes place during node
13983 patching. Returned the resolved type or generate an error
13984 message. Return the resolved type or NULL_TREE. */
13985
13986static tree
13987resolve_type_during_patch (type)
13988 tree type;
13989{
13990 if (unresolved_type_p (type, NULL))
13991 {
4142b247 13992 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13993 if (!type_decl)
13994 {
13995 parse_error_context (type,
13996 "Class `%s' not found in type declaration",
13997 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13998 return NULL_TREE;
13999 }
14000 else
5e942c50
APB
14001 {
14002 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
14003 return TREE_TYPE (type_decl);
14004 }
e04a16fb
AG
14005 }
14006 return type;
14007}
14008/* 5.5 Casting Conversion. error_mark_node is returned if an error is
14009 found. Otherwise NODE or something meant to replace it is returned. */
14010
14011static tree
19e223db 14012patch_cast (node, wfl_op)
e04a16fb 14013 tree node;
19e223db 14014 tree wfl_op;
e04a16fb
AG
14015{
14016 tree op = TREE_OPERAND (node, 0);
14017 tree op_type = TREE_TYPE (op);
14018 tree cast_type = TREE_TYPE (node);
14019 char *t1;
14020
14021 /* First resolve OP_TYPE if unresolved */
14022 if (!(cast_type = resolve_type_during_patch (cast_type)))
14023 return error_mark_node;
14024
14025 /* Check on cast that are proven correct at compile time */
14026 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14027 {
e04a16fb
AG
14028 /* Same type */
14029 if (cast_type == op_type)
14030 return node;
14031
0b4d333e
APB
14032 /* float and double type are converted to the original type main
14033 variant and then to the target type. */
14034 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14035 op = convert (integer_type_node, op);
14036
e04a16fb
AG
14037 /* Try widening/narowwing convertion. Potentially, things need
14038 to be worked out in gcc so we implement the extreme cases
14039 correctly. fold_convert() needs to be fixed. */
14040 return convert (cast_type, op);
14041 }
14042
0b4d333e
APB
14043 /* It's also valid to cast a boolean into a boolean */
14044 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14045 return node;
14046
5e942c50
APB
14047 /* null can be casted to references */
14048 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14049 return build_null_of_type (cast_type);
14050
e04a16fb
AG
14051 /* The remaining legal casts involve conversion between reference
14052 types. Check for their compile time correctness. */
14053 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14054 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14055 {
14056 TREE_TYPE (node) = promote_type (cast_type);
14057 /* Now, the case can be determined correct at compile time if
14058 OP_TYPE can be converted into CAST_TYPE by assignment
14059 conversion (5.2) */
14060
14061 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14062 {
14063 TREE_SET_CODE (node, NOP_EXPR);
14064 return node;
14065 }
14066
14067 if (flag_emit_class_files)
14068 {
14069 TREE_SET_CODE (node, CONVERT_EXPR);
14070 return node;
14071 }
e04a16fb
AG
14072
14073 /* The cast requires a run-time check */
14074 return build (CALL_EXPR, promote_type (cast_type),
14075 build_address_of (soft_checkcast_node),
14076 tree_cons (NULL_TREE, build_class_ref (cast_type),
14077 build_tree_list (NULL_TREE, op)),
14078 NULL_TREE);
14079 }
14080
14081 /* Any other casts are proven incorrect at compile time */
c2e3db92 14082 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14083 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14084 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14085 free (t1);
14086 return error_mark_node;
14087}
14088
5e942c50
APB
14089/* Build a null constant and give it the type TYPE. */
14090
14091static tree
14092build_null_of_type (type)
14093 tree type;
14094{
14095 tree node = build_int_2 (0, 0);
14096 TREE_TYPE (node) = promote_type (type);
14097 return node;
14098}
14099
e04a16fb
AG
14100/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14101 a list of indices. */
14102static tree
14103build_array_ref (location, array, index)
14104 int location;
14105 tree array, index;
14106{
14107 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14108 EXPR_WFL_LINECOL (node) = location;
14109 return node;
14110}
14111
14112/* 15.12 Array Access Expression */
14113
14114static tree
c877974e
APB
14115patch_array_ref (node)
14116 tree node;
e04a16fb
AG
14117{
14118 tree array = TREE_OPERAND (node, 0);
14119 tree array_type = TREE_TYPE (array);
14120 tree index = TREE_OPERAND (node, 1);
14121 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14122 int error_found = 0;
14123
14124 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14125
e04a16fb
AG
14126 if (TREE_CODE (array_type) == POINTER_TYPE)
14127 array_type = TREE_TYPE (array_type);
14128
14129 /* The array reference must be an array */
14130 if (!TYPE_ARRAY_P (array_type))
14131 {
14132 parse_error_context
781b0558
KG
14133 (wfl_operator,
14134 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14135 lang_printable_name (array_type, 0));
e04a16fb
AG
14136 TREE_TYPE (node) = error_mark_node;
14137 error_found = 1;
14138 }
14139
c2952b01 14140 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14141 type must be int */
15fdcfe9
PB
14142 index = do_unary_numeric_promotion (index);
14143 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14144 {
1ebadc60 14145 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14146 parse_error_context (wfl_operator,
14147 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14148 lang_printable_name (index_type, 0));
14149 else
781b0558
KG
14150 parse_error_context (wfl_operator,
14151 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14152 lang_printable_name (index_type, 0));
e04a16fb
AG
14153 TREE_TYPE (node) = error_mark_node;
14154 error_found = 1;
14155 }
14156
e04a16fb
AG
14157 if (error_found)
14158 return error_mark_node;
e04a16fb 14159
5e942c50 14160 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14161
7f1d4866 14162 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14163 {
15fdcfe9
PB
14164 TREE_OPERAND (node, 0) = array;
14165 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14166 }
14167 else
939d7216
PB
14168 {
14169 /* The save_expr is for correct evaluation order. It would be cleaner
14170 to use force_evaluation_order (see comment there), but that is
14171 difficult when we also have to deal with bounds checking. */
14172 if (TREE_SIDE_EFFECTS (index))
14173 array = save_expr (array);
14174 node = build_java_arrayaccess (array, array_type, index);
14175 if (TREE_SIDE_EFFECTS (index))
14176 node = build (COMPOUND_EXPR, array_type, array, node);
14177 }
e04a16fb
AG
14178 TREE_TYPE (node) = array_type;
14179 return node;
14180}
14181
14182/* 15.9 Array Creation Expressions */
14183
14184static tree
14185build_newarray_node (type, dims, extra_dims)
14186 tree type;
14187 tree dims;
14188 int extra_dims;
14189{
14190 tree node =
b67d701b 14191 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14192 build_int_2 (extra_dims, 0));
e04a16fb
AG
14193 return node;
14194}
14195
14196static tree
14197patch_newarray (node)
14198 tree node;
14199{
14200 tree type = TREE_OPERAND (node, 0);
14201 tree dims = TREE_OPERAND (node, 1);
14202 tree cdim, array_type;
14203 int error_found = 0;
14204 int ndims = 0;
14205 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14206
14207 /* Dimension types are verified. It's better for the types to be
14208 verified in order. */
14209 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14210 {
14211 int dim_error = 0;
14212 tree dim = TREE_VALUE (cdim);
14213
14214 /* Dim might have been saved during its evaluation */
dba41d30 14215 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14216
14217 /* The type of each specified dimension must be an integral type. */
14218 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14219 dim_error = 1;
14220
14221 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14222 promoted type must be int. */
14223 else
14224 {
15fdcfe9 14225 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14226 if (TREE_TYPE (dim) != int_type_node)
14227 dim_error = 1;
14228 }
14229
14230 /* Report errors on types here */
14231 if (dim_error)
14232 {
14233 parse_error_context
14234 (TREE_PURPOSE (cdim),
781b0558 14235 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14236 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14237 "Explicit cast needed to" : "Can't"),
0a2138e2 14238 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14239 error_found = 1;
14240 }
14241
e04a16fb
AG
14242 TREE_PURPOSE (cdim) = NULL_TREE;
14243 }
14244
14245 /* Resolve array base type if unresolved */
14246 if (!(type = resolve_type_during_patch (type)))
14247 error_found = 1;
14248
14249 if (error_found)
14250 {
14251 /* We don't want further evaluation of this bogus array creation
14252 operation */
14253 TREE_TYPE (node) = error_mark_node;
14254 return error_mark_node;
14255 }
14256
15fdcfe9
PB
14257 /* Set array_type to the actual (promoted) array type of the result. */
14258 if (TREE_CODE (type) == RECORD_TYPE)
14259 type = build_pointer_type (type);
14260 while (--xdims >= 0)
14261 {
14262 type = promote_type (build_java_array_type (type, -1));
14263 }
14264 dims = nreverse (dims);
14265 array_type = type;
14266 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14267 {
14268 type = array_type;
05bccae2
RK
14269 array_type
14270 = build_java_array_type (type,
14271 TREE_CODE (cdim) == INTEGER_CST
14272 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14273 : -1);
15fdcfe9
PB
14274 array_type = promote_type (array_type);
14275 }
14276 dims = nreverse (dims);
14277
e04a16fb
AG
14278 /* The node is transformed into a function call. Things are done
14279 differently according to the number of dimensions. If the number
14280 of dimension is equal to 1, then the nature of the base type
14281 (primitive or not) matters. */
15fdcfe9 14282 if (ndims == 1)
fdec99c6 14283 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14284
e04a16fb
AG
14285 /* Can't reuse what's already written in expr.c because it uses the
14286 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14287 return build (CALL_EXPR, array_type,
e04a16fb 14288 build_address_of (soft_multianewarray_node),
15fdcfe9 14289 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14290 tree_cons (NULL_TREE,
15fdcfe9 14291 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14292 NULL_TREE);
14293}
14294
f8976021
APB
14295/* 10.6 Array initializer. */
14296
14297/* Build a wfl for array element that don't have one, so we can
14298 pin-point errors. */
14299
14300static tree
14301maybe_build_array_element_wfl (node)
14302 tree node;
14303{
14304 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14305 return build_expr_wfl (NULL_TREE, ctxp->filename,
14306 ctxp->elc.line, ctxp->elc.prev_col);
14307 else
14308 return NULL_TREE;
14309}
14310
14311/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14312 identification of initialized arrays easier to detect during walk
14313 and expansion. */
14314
14315static tree
14316build_new_array_init (location, values)
14317 int location;
14318 tree values;
14319{
14320 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14321 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14322 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14323 return to_return;
14324}
14325
14326/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14327 occurred. Otherwise return NODE after having set its type
14328 appropriately. */
14329
14330static tree
14331patch_new_array_init (type, node)
14332 tree type, node;
f8976021
APB
14333{
14334 int error_seen = 0;
fdec99c6 14335 tree current, element_type;
f8976021 14336 HOST_WIDE_INT length;
fdec99c6
PB
14337 int all_constant = 1;
14338 tree init = TREE_OPERAND (node, 0);
f8976021 14339
fdec99c6
PB
14340 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14341 {
14342 parse_error_context (node,
14343 "Invalid array initializer for non-array type `%s'",
14344 lang_printable_name (type, 1));
14345 return error_mark_node;
14346 }
14347 type = TREE_TYPE (type);
14348 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14349
fdec99c6
PB
14350 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14351
14352 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14353 current; length++, current = TREE_CHAIN (current))
f8976021 14354 {
fdec99c6
PB
14355 tree elt = TREE_VALUE (current);
14356 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14357 {
fdec99c6 14358 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14359 elt = TREE_VALUE (current);
14360 /* When compiling to native code, STRING_CST is converted to
14361 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14362 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14363 all_constant = 0;
f8976021 14364 }
fdec99c6
PB
14365 else
14366 {
14367 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14368 TREE_PURPOSE (current) = NULL_TREE;
14369 all_constant = 0;
14370 }
9a7ab4b3
APB
14371 if (elt && TREE_CODE (elt) == TREE_LIST
14372 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14373 error_seen = 1;
f8976021
APB
14374 }
14375
14376 if (error_seen)
14377 return error_mark_node;
14378
14379 /* Create a new type. We can't reuse the one we have here by
14380 patching its dimension because it originally is of dimension -1
14381 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14382 type = build_java_array_type (element_type, length);
14383 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14384 TREE_TYPE (node) = promote_type (type);
14385 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14386 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14387 return node;
14388}
14389
14390/* Verify that one entry of the initializer element list can be
14391 assigned to the array base type. Report 1 if an error occurred, 0
14392 otherwise. */
14393
14394static int
14395array_constructor_check_entry (type, entry)
14396 tree type, entry;
14397{
14398 char *array_type_string = NULL; /* For error reports */
14399 tree value, type_value, new_value, wfl_value, patched;
14400 int error_seen = 0;
14401
14402 new_value = NULL_TREE;
14403 wfl_value = TREE_VALUE (entry);
14404
f8976021 14405 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14406 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14407 if ((patched = patch_string (value)))
14408 value = patched;
1179ebc2
APB
14409 if (value == error_mark_node)
14410 return 1;
f8976021 14411
f8976021
APB
14412 type_value = TREE_TYPE (value);
14413
1179ebc2 14414 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14415 constant overflow during narrowing. */
14416 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14417 new_value = try_builtin_assignconv (wfl_operator, type, value);
14418 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14419 type_value = promote_type (type);
100f7cd8 14420
f8976021
APB
14421 /* Check and report errors */
14422 if (!new_value)
14423 {
49f48c71 14424 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14425 "Can't" : "Explicit cast needed to");
14426 if (!array_type_string)
c2e3db92 14427 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14428 parse_error_context
14429 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14430 msg, lang_printable_name (type_value, 1), array_type_string);
14431 error_seen = 1;
14432 }
14433
14434 if (new_value)
14435 {
b8c5b1c6 14436 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14437 TREE_VALUE (entry) = new_value;
14438 }
14439
14440 if (array_type_string)
14441 free (array_type_string);
14442
14443 TREE_PURPOSE (entry) = NULL_TREE;
14444 return error_seen;
14445}
14446
e04a16fb
AG
14447static tree
14448build_this (location)
14449 int location;
14450{
9ee9b555 14451 tree node = build_wfl_node (this_identifier_node);
b67d701b 14452 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14453 EXPR_WFL_LINECOL (node) = location;
14454 return node;
14455}
14456
14457/* 14.15 The return statement. It builds a modify expression that
14458 assigns the returned value to the RESULT_DECL that hold the value
14459 to be returned. */
14460
14461static tree
14462build_return (location, op)
14463 int location;
14464 tree op;
14465{
14466 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14467 EXPR_WFL_LINECOL (node) = location;
b67d701b 14468 node = build_debugable_stmt (location, node);
e04a16fb
AG
14469 return node;
14470}
14471
14472static tree
14473patch_return (node)
14474 tree node;
14475{
14476 tree return_exp = TREE_OPERAND (node, 0);
14477 tree meth = current_function_decl;
14478 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14479 int error_found = 0;
14480
14481 TREE_TYPE (node) = error_mark_node;
14482 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14483
14484 /* It's invalid to have a return value within a function that is
14485 declared with the keyword void or that is a constructor */
14486 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14487 error_found = 1;
14488
f099f336 14489 /* It's invalid to use a return statement in a static block */
c2952b01 14490 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14491 error_found = 1;
14492
e04a16fb
AG
14493 /* It's invalid to have a no return value within a function that
14494 isn't declared with the keyword `void' */
14495 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14496 error_found = 2;
c2952b01
APB
14497
14498 if (in_instance_initializer)
14499 error_found = 1;
e04a16fb
AG
14500
14501 if (error_found)
14502 {
c2952b01 14503 if (in_instance_initializer)
f099f336 14504 parse_error_context (wfl_operator,
c2952b01
APB
14505 "`return' inside instance initializer");
14506
14507 else if (DECL_CLINIT_P (current_function_decl))
14508 parse_error_context (wfl_operator,
14509 "`return' inside static initializer");
f099f336
APB
14510
14511 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14512 {
c2e3db92 14513 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14514 parse_error_context (wfl_operator,
14515 "`return' with%s value from `%s %s'",
14516 (error_found == 1 ? "" : "out"),
14517 t, lang_printable_name (meth, 0));
14518 free (t);
14519 }
14520 else
14521 parse_error_context (wfl_operator,
14522 "`return' with value from constructor `%s'",
14523 lang_printable_name (meth, 0));
e04a16fb
AG
14524 return error_mark_node;
14525 }
14526
5e942c50
APB
14527 /* If we have a return_exp, build a modify expression and expand
14528 it. Note: at that point, the assignment is declared valid, but we
14529 may want to carry some more hacks */
e04a16fb
AG
14530 if (return_exp)
14531 {
5e942c50
APB
14532 tree exp = java_complete_tree (return_exp);
14533 tree modify, patched;
14534
14535 /* If the function returned value and EXP are booleans, EXP has
14536 to be converted into the type of DECL_RESULT, which is integer
14537 (see complete_start_java_method) */
14538 if (TREE_TYPE (exp) == boolean_type_node &&
14539 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14540 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14541
14542 /* `null' can be assigned to a function returning a reference */
14543 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14544 exp == null_pointer_node)
14545 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14546
14547 if ((patched = patch_string (exp)))
14548 exp = patched;
14549
14550 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14551 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14552 modify = java_complete_tree (modify);
5e942c50 14553
e04a16fb
AG
14554 if (modify != error_mark_node)
14555 {
14556 TREE_SIDE_EFFECTS (modify) = 1;
14557 TREE_OPERAND (node, 0) = modify;
14558 }
14559 else
14560 return error_mark_node;
14561 }
14562 TREE_TYPE (node) = void_type_node;
14563 TREE_SIDE_EFFECTS (node) = 1;
14564 return node;
14565}
14566
14567/* 14.8 The if Statement */
14568
14569static tree
14570build_if_else_statement (location, expression, if_body, else_body)
14571 int location;
14572 tree expression, if_body, else_body;
14573{
14574 tree node;
e04a16fb 14575 if (!else_body)
9bbc7d9f 14576 else_body = empty_stmt_node;
e04a16fb
AG
14577 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14578 EXPR_WFL_LINECOL (node) = location;
b67d701b 14579 node = build_debugable_stmt (location, node);
e04a16fb
AG
14580 return node;
14581}
14582
14583static tree
14584patch_if_else_statement (node)
14585 tree node;
14586{
14587 tree expression = TREE_OPERAND (node, 0);
14588
14589 TREE_TYPE (node) = error_mark_node;
14590 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14591
14592 /* The type of expression must be boolean */
b67d701b
PB
14593 if (TREE_TYPE (expression) != boolean_type_node
14594 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14595 {
14596 parse_error_context
14597 (wfl_operator,
14598 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14599 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14600 return error_mark_node;
14601 }
14602
14603 TREE_TYPE (node) = void_type_node;
14604 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14605 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14606 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14607 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14608 return node;
14609}
14610
14611/* 14.6 Labeled Statements */
14612
14613/* Action taken when a lableled statement is parsed. a new
14614 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14615 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14616
14617static tree
0a2138e2 14618build_labeled_block (location, label)
e04a16fb 14619 int location;
0a2138e2 14620 tree label;
e04a16fb 14621{
b635eb2f 14622 tree label_name ;
e04a16fb 14623 tree label_decl, node;
b635eb2f
PB
14624 if (label == NULL_TREE || label == continue_identifier_node)
14625 label_name = label;
14626 else
e04a16fb 14627 {
b635eb2f
PB
14628 label_name = merge_qualified_name (label_id, label);
14629 /* Issue an error if we try to reuse a label that was previously
14630 declared */
14631 if (IDENTIFIER_LOCAL_VALUE (label_name))
14632 {
14633 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14634 parse_error_context (wfl_operator,
14635 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14636 IDENTIFIER_POINTER (label));
14637 EXPR_WFL_LINECOL (wfl_operator) =
14638 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14639 parse_error_context (wfl_operator,
14640 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14641 IDENTIFIER_POINTER (label));
14642 java_error_count--;
14643 }
e04a16fb
AG
14644 }
14645
14646 label_decl = create_label_decl (label_name);
14647 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14648 EXPR_WFL_LINECOL (node) = location;
14649 TREE_SIDE_EFFECTS (node) = 1;
14650 return node;
14651}
14652
b67d701b 14653/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14654
14655static tree
b635eb2f 14656finish_labeled_statement (lbe, statement)
e04a16fb
AG
14657 tree lbe; /* Labeled block expr */
14658 tree statement;
14659{
14660 /* In anyways, tie the loop to its statement */
14661 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14662 pop_labeled_block ();
14663 POP_LABELED_BLOCK ();
e04a16fb
AG
14664 return lbe;
14665}
14666
14667/* 14.10, 14.11, 14.12 Loop Statements */
14668
14669/* Create an empty LOOP_EXPR and make it the last in the nested loop
14670 list. */
14671
14672static tree
14673build_new_loop (loop_body)
14674 tree loop_body;
14675{
14676 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14677 TREE_SIDE_EFFECTS (loop) = 1;
14678 PUSH_LOOP (loop);
14679 return loop;
14680}
14681
14682/* Create a loop body according to the following structure:
14683 COMPOUND_EXPR
14684 COMPOUND_EXPR (loop main body)
14685 EXIT_EXPR (this order is for while/for loops.
14686 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14687 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14688 BODY end of this labeled block)
14689 INCREMENT (if any)
14690
14691 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14692 after the body, like in the do-while loop.
14693
14694 To obtain a loop, the loop body structure described above is
14695 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14696
14697 LABELED_BLOCK_EXPR
14698 LABEL_DECL (use this label to exit the loop)
14699 LOOP_EXPR
14700 <structure described above> */
e04a16fb
AG
14701
14702static tree
14703build_loop_body (location, condition, reversed)
14704 int location;
14705 tree condition;
14706 int reversed;
14707{
0a2138e2 14708 tree first, second, body;
e04a16fb
AG
14709
14710 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14711 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14712 condition = build_debugable_stmt (location, condition);
14713 TREE_SIDE_EFFECTS (condition) = 1;
14714
b635eb2f 14715 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14716 first = (reversed ? body : condition);
14717 second = (reversed ? condition : body);
14718 return
14719 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14720 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14721}
14722
14723/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14724 their order) on the current loop. Unlink the current loop from the
14725 loop list. */
14726
14727static tree
b635eb2f 14728finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14729 int location;
14730 tree condition, body;
14731 int reversed;
14732{
14733 tree to_return = ctxp->current_loop;
14734 tree loop_body = LOOP_EXPR_BODY (to_return);
14735 if (condition)
14736 {
14737 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14738 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14739 The real EXIT_EXPR is one operand further. */
14740 EXPR_WFL_LINECOL (cnode) = location;
14741 /* This one is for accurate error reports */
14742 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14743 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14744 }
14745 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14746 POP_LOOP ();
14747 return to_return;
14748}
14749
b635eb2f 14750/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14751 loops feature the condition part */
14752
14753static tree
b635eb2f 14754finish_for_loop (location, condition, update, body)
e04a16fb
AG
14755 int location;
14756 tree condition, update, body;
14757{
14758 /* Put the condition and the loop body in place */
b635eb2f 14759 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14760 /* LOOP is the current loop which has been now popped of the loop
14761 stack. Install the update block */
14762 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14763 return loop;
14764}
14765
5cbdba64
APB
14766/* Try to find the loop a block might be related to. This comprises
14767 the case where the LOOP_EXPR is found as the second operand of a
14768 COMPOUND_EXPR, because the loop happens to have an initialization
14769 part, then expressed as the first operand of the COMPOUND_EXPR. If
14770 the search finds something, 1 is returned. Otherwise, 0 is
14771 returned. The search is assumed to start from a
14772 LABELED_BLOCK_EXPR's block. */
14773
14774static tree
14775search_loop (statement)
14776 tree statement;
14777{
14778 if (TREE_CODE (statement) == LOOP_EXPR)
14779 return statement;
14780
14781 if (TREE_CODE (statement) == BLOCK)
14782 statement = BLOCK_SUBBLOCKS (statement);
14783 else
14784 return NULL_TREE;
14785
14786 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14787 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14788 statement = TREE_OPERAND (statement, 1);
14789
14790 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14791 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14792}
14793
14794/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14795 returned otherwise. */
14796
14797static int
14798labeled_block_contains_loop_p (block, loop)
14799 tree block, loop;
14800{
14801 if (!block)
14802 return 0;
14803
14804 if (LABELED_BLOCK_BODY (block) == loop)
14805 return 1;
14806
c2952b01 14807 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14808 return 1;
14809
14810 return 0;
14811}
14812
e04a16fb 14813/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14814 insert LOOP as its body. */
e04a16fb
AG
14815
14816static tree
14817patch_loop_statement (loop)
14818 tree loop;
14819{
cd9643f7 14820 tree loop_label;
5cbdba64 14821
cd9643f7 14822 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14823 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14824 return loop;
14825
cd9643f7 14826 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14827 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14828 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14829 LABELED_BLOCK_BODY (loop_label) = loop;
14830 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14831 return loop_label;
e04a16fb
AG
14832}
14833
14834/* 14.13, 14.14: break and continue Statements */
14835
14836/* Build a break or a continue statement. a null NAME indicates an
14837 unlabeled break/continue statement. */
14838
14839static tree
14840build_bc_statement (location, is_break, name)
14841 int location, is_break;
14842 tree name;
14843{
14844 tree break_continue, label_block_expr = NULL_TREE;
14845
14846 if (name)
14847 {
14848 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14849 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14850 /* Null means that we don't have a target for this named
14851 break/continue. In this case, we make the target to be the
14852 label name, so that the error can be reported accuratly in
14853 patch_bc_statement. */
14854 label_block_expr = EXPR_WFL_NODE (name);
14855 }
14856 /* Unlabeled break/continue will be handled during the
14857 break/continue patch operation */
14858 break_continue
14859 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14860
14861 IS_BREAK_STMT_P (break_continue) = is_break;
14862 TREE_SIDE_EFFECTS (break_continue) = 1;
14863 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14864 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14865 return break_continue;
14866}
14867
14868/* Verification of a break/continue statement. */
14869
14870static tree
14871patch_bc_statement (node)
14872 tree node;
14873{
14874 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14875 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14876 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14877
e04a16fb 14878 /* Having an identifier here means that the target is unknown. */
b635eb2f 14879 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14880 {
14881 parse_error_context (wfl_operator, "No label definition found for `%s'",
14882 IDENTIFIER_POINTER (bc_label));
14883 return error_mark_node;
14884 }
b635eb2f 14885 if (! IS_BREAK_STMT_P (node))
e04a16fb 14886 {
b635eb2f
PB
14887 /* It's a continue statement. */
14888 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14889 {
b635eb2f
PB
14890 if (labeled_block == NULL_TREE)
14891 {
14892 if (bc_label == NULL_TREE)
14893 parse_error_context (wfl_operator,
14894 "`continue' must be in loop");
14895 else
1504b2b4
APB
14896 parse_error_context
14897 (wfl_operator, "continue label `%s' does not name a loop",
14898 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14899 return error_mark_node;
14900 }
14901 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14902 == continue_identifier_node)
14903 && (bc_label == NULL_TREE
14904 || TREE_CHAIN (labeled_block) == bc_label))
14905 {
14906 bc_label = labeled_block;
14907 break;
14908 }
e04a16fb 14909 }
e04a16fb 14910 }
b635eb2f 14911 else if (!bc_label)
34f4db93 14912 {
b635eb2f 14913 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14914 {
b635eb2f
PB
14915 if (labeled_block == NULL_TREE)
14916 {
14917 parse_error_context (wfl_operator,
14918 "`break' must be in loop or switch");
14919 return error_mark_node;
14920 }
14921 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14922 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14923 || search_loop (target_stmt))
b635eb2f
PB
14924 {
14925 bc_label = labeled_block;
14926 break;
14927 }
e04a16fb 14928 }
e04a16fb
AG
14929 }
14930
b635eb2f 14931 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14932 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14933
e04a16fb
AG
14934 /* Our break/continue don't return values. */
14935 TREE_TYPE (node) = void_type_node;
14936 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14937 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14938 sometimes, like after a if statement) */
14939 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14940 TREE_SIDE_EFFECTS (node) = 1;
14941 return node;
14942}
14943
14944/* Process the exit expression belonging to a loop. Its type must be
14945 boolean. */
14946
14947static tree
14948patch_exit_expr (node)
14949 tree node;
14950{
14951 tree expression = TREE_OPERAND (node, 0);
14952 TREE_TYPE (node) = error_mark_node;
14953 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14954
14955 /* The type of expression must be boolean */
14956 if (TREE_TYPE (expression) != boolean_type_node)
14957 {
14958 parse_error_context
14959 (wfl_operator,
781b0558 14960 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14961 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14962 return error_mark_node;
14963 }
14964 /* Now we know things are allright, invert the condition, fold and
14965 return */
14966 TREE_OPERAND (node, 0) =
14967 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14968
14969 if (! integer_zerop (TREE_OPERAND (node, 0))
14970 && ctxp->current_loop != NULL_TREE
14971 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14972 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14973 if (! integer_onep (TREE_OPERAND (node, 0)))
14974 CAN_COMPLETE_NORMALLY (node) = 1;
14975
14976
e04a16fb
AG
14977 TREE_TYPE (node) = void_type_node;
14978 return node;
14979}
b67d701b
PB
14980
14981/* 14.9 Switch statement */
14982
14983static tree
14984patch_switch_statement (node)
14985 tree node;
14986{
c877974e 14987 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14988
14989 /* Complete the switch expression */
14990 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14991 se_type = TREE_TYPE (se);
14992 /* The type of the switch expression must be char, byte, short or
14993 int */
2e0f0aff 14994 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14995 {
14996 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14997 parse_error_context (wfl_operator,
14998 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14999 lang_printable_name (se_type, 0));
b67d701b
PB
15000 /* This is what java_complete_tree will check */
15001 TREE_OPERAND (node, 0) = error_mark_node;
15002 return error_mark_node;
15003 }
15004
15fdcfe9 15005 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
15006
15007 /* Ready to return */
15fdcfe9 15008 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
15009 {
15010 TREE_TYPE (node) = error_mark_node;
15011 return error_mark_node;
15012 }
15013 TREE_TYPE (node) = void_type_node;
15014 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 15015 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
15016 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15017 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
15018 return node;
15019}
15020
165f37bc 15021/* 14.18 The try/catch statements */
b67d701b 15022
b67d701b 15023static tree
a7d8d81f 15024build_try_statement (location, try_block, catches)
b67d701b 15025 int location;
a7d8d81f
PB
15026 tree try_block, catches;
15027{
15028 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 15029 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 15030 return node;
b67d701b
PB
15031}
15032
a7d8d81f
PB
15033static tree
15034build_try_finally_statement (location, try_block, finally)
15035 int location;
15036 tree try_block, finally;
b67d701b 15037{
a7d8d81f
PB
15038 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15039 EXPR_WFL_LINECOL (node) = location;
15040 return node;
b67d701b
PB
15041}
15042
15043static tree
15044patch_try_statement (node)
15045 tree node;
15046{
15047 int error_found = 0;
15048 tree try = TREE_OPERAND (node, 0);
15049 /* Exception handlers are considered in left to right order */
15050 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15051 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15052
15053 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15054 to process the next catch clause. We process the catch clause before
15055 the try block so that when processing the try block we can check thrown
15056 exceptions againts the caught type list. */
b67d701b
PB
15057 for (current = catch; current; current = TREE_CHAIN (current))
15058 {
15059 tree carg_decl, carg_type;
15060 tree sub_current, catch_block, catch_clause;
15061 int unreachable;
15062
b67d701b 15063 /* At this point, the structure of the catch clause is
b67d701b
PB
15064 CATCH_EXPR (catch node)
15065 BLOCK (with the decl of the parameter)
15066 COMPOUND_EXPR
7525cc04 15067 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15068 BLOCK (catch clause block)
a7d8d81f
PB
15069 */
15070 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15071 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15072 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15073
15074 /* Catch clauses can't have more than one parameter declared,
15075 but it's already enforced by the grammar. Make sure that the
15076 only parameter of the clause statement in of class Throwable
15077 or a subclass of Throwable, but that was done earlier. The
15078 catch clause parameter type has also been resolved. */
15079
15080 /* Just make sure that the catch clause parameter type inherits
15081 from java.lang.Throwable */
15082 if (!inherits_from_p (carg_type, throwable_type_node))
15083 {
15084 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15085 parse_error_context (wfl_operator,
781b0558 15086 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15087 lang_printable_name (carg_type, 0));
b67d701b
PB
15088 error_found = 1;
15089 continue;
15090 }
15091
15092 /* Partial check for unreachable catch statement: The catch
15093 clause is reachable iff is no earlier catch block A in
15094 the try statement such that the type of the catch
15095 clause's parameter is the same as or a subclass of the
15096 type of A's parameter */
15097 unreachable = 0;
15098 for (sub_current = catch;
15099 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15100 {
15101 tree sub_catch_clause, decl;
a7d8d81f 15102 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15103 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15104
15105 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15106 {
15107 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15108 parse_error_context
781b0558
KG
15109 (wfl_operator,
15110 "`catch' not reached because of the catch clause at line %d",
15111 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15112 unreachable = error_found = 1;
15113 break;
15114 }
15115 }
b67d701b
PB
15116 /* Complete the catch clause block */
15117 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15118 if (catch_block == error_mark_node)
15119 {
15120 error_found = 1;
15121 continue;
15122 }
15fdcfe9
PB
15123 if (CAN_COMPLETE_NORMALLY (catch_block))
15124 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15125 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15126
15127 if (unreachable)
15128 continue;
15129
15130 /* Things to do here: the exception must be thrown */
15131
15132 /* Link this type to the caught type list */
15133 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15134 }
15135
b9f7e36c
APB
15136 PUSH_EXCEPTIONS (caught_type_list);
15137 if ((try = java_complete_tree (try)) == error_mark_node)
15138 error_found = 1;
15fdcfe9
PB
15139 if (CAN_COMPLETE_NORMALLY (try))
15140 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15141 POP_EXCEPTIONS ();
15142
b67d701b
PB
15143 /* Verification ends here */
15144 if (error_found)
15145 return error_mark_node;
15146
15147 TREE_OPERAND (node, 0) = try;
15148 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15149 TREE_TYPE (node) = void_type_node;
15150 return node;
15151}
b9f7e36c
APB
15152
15153/* 14.17 The synchronized Statement */
15154
15155static tree
15156patch_synchronized_statement (node, wfl_op1)
15157 tree node, wfl_op1;
15158{
5a005d9e 15159 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15160 tree block = TREE_OPERAND (node, 1);
5a005d9e 15161
c7303e41 15162 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15163
15164 if (expr == error_mark_node)
15165 {
15166 block = java_complete_tree (block);
15167 return expr;
15168 }
b9f7e36c 15169
c7303e41
APB
15170 /* We might be trying to synchronize on a STRING_CST */
15171 if ((tmp = patch_string (expr)))
15172 expr = tmp;
15173
b9f7e36c 15174 /* The TYPE of expr must be a reference type */
5a005d9e 15175 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15176 {
15177 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15178 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15179 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15180 return error_mark_node;
15181 }
15182
ce6e9147
APB
15183 if (flag_emit_xref)
15184 {
15185 TREE_OPERAND (node, 0) = expr;
15186 TREE_OPERAND (node, 1) = java_complete_tree (block);
15187 CAN_COMPLETE_NORMALLY (node) = 1;
15188 return node;
15189 }
15190
b9f7e36c
APB
15191 /* Generate a try-finally for the synchronized statement, except
15192 that the handler that catches all throw exception calls
15193 _Jv_MonitorExit and then rethrow the exception.
15194 The synchronized statement is then implemented as:
15195 TRY
15196 {
15197 _Jv_MonitorEnter (expression)
15198 synchronized_block
15199 _Jv_MonitorExit (expression)
15200 }
15201 CATCH_ALL
15202 {
15203 e = _Jv_exception_info ();
15204 _Jv_MonitorExit (expression)
15205 Throw (e);
15206 } */
15207
5a005d9e
PB
15208 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15209 BUILD_MONITOR_ENTER (enter, expr_decl);
15210 BUILD_MONITOR_EXIT (exit, expr_decl);
15211 CAN_COMPLETE_NORMALLY (enter) = 1;
15212 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15213 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15214 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15215 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15216 build (COMPOUND_EXPR, NULL_TREE,
15217 build (WITH_CLEANUP_EXPR, NULL_TREE,
15218 build (COMPOUND_EXPR, NULL_TREE,
96847892 15219 assignment, enter),
5a005d9e
PB
15220 NULL_TREE, exit),
15221 block));
15222 node = build_expr_block (node, expr_decl);
15223
15224 return java_complete_tree (node);
b9f7e36c
APB
15225}
15226
15227/* 14.16 The throw Statement */
15228
15229static tree
15230patch_throw_statement (node, wfl_op1)
15231 tree node, wfl_op1;
15232{
15233 tree expr = TREE_OPERAND (node, 0);
15234 tree type = TREE_TYPE (expr);
15235 int unchecked_ok = 0, tryblock_throws_ok = 0;
15236
15237 /* Thrown expression must be assignable to java.lang.Throwable */
15238 if (!try_reference_assignconv (throwable_type_node, expr))
15239 {
15240 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15241 parse_error_context (wfl_operator,
15242 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15243 lang_printable_name (type, 0));
b9f7e36c
APB
15244 /* If the thrown expression was a reference, we further the
15245 compile-time check. */
15246 if (!JREFERENCE_TYPE_P (type))
15247 return error_mark_node;
15248 }
15249
15250 /* At least one of the following must be true */
15251
15252 /* The type of the throw expression is a not checked exception,
15253 i.e. is a unchecked expression. */
c877974e 15254 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15255
c2952b01
APB
15256 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15257 /* An instance can't throw a checked excetion unless that exception
15258 is explicitely declared in the `throws' clause of each
15259 constructor. This doesn't apply to anonymous classes, since they
15260 don't have declared constructors. */
15261 if (!unchecked_ok
15262 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15263 {
15264 tree current;
15265 for (current = TYPE_METHODS (current_class); current;
15266 current = TREE_CHAIN (current))
15267 if (DECL_CONSTRUCTOR_P (current)
15268 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15269 {
15270 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)",
15271 lang_printable_name (TREE_TYPE (expr), 0));
15272 return error_mark_node;
15273 }
15274 }
15275
b9f7e36c
APB
15276 /* Throw is contained in a try statement and at least one catch
15277 clause can receive the thrown expression or the current method is
15278 declared to throw such an exception. Or, the throw statement is
15279 contained in a method or constructor declaration and the type of
15280 the Expression is assignable to at least one type listed in the
15281 throws clause the declaration. */
b9f7e36c 15282 if (!unchecked_ok)
f099f336 15283 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15284 if (!(unchecked_ok || tryblock_throws_ok))
15285 {
15286 /* If there is a surrounding try block that has no matching
15287 clatch clause, report it first. A surrounding try block exits
15288 only if there is something after the list of checked
15289 exception thrown by the current function (if any). */
15290 if (IN_TRY_BLOCK_P ())
781b0558 15291 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 15292 lang_printable_name (type, 0));
b9f7e36c
APB
15293 /* If we have no surrounding try statement and the method doesn't have
15294 any throws, report it now. FIXME */
f099f336
APB
15295
15296 /* We report that the exception can't be throw from a try block
15297 in all circumstances but when the `throw' is inside a static
15298 block. */
b9f7e36c
APB
15299 else if (!EXCEPTIONS_P (currently_caught_type_list)
15300 && !tryblock_throws_ok)
f099f336 15301 {
c2952b01 15302 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15303 parse_error_context (wfl_operator,
15304 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15305 lang_printable_name (type, 0));
15306 else
781b0558
KG
15307 parse_error_context (wfl_operator,
15308 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15309 lang_printable_name (type, 0));
15310 }
b9f7e36c
APB
15311 /* Otherwise, the current method doesn't have the appropriate
15312 throws declaration */
15313 else
781b0558 15314 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15315 lang_printable_name (type, 0));
b9f7e36c
APB
15316 return error_mark_node;
15317 }
15318
ce6e9147 15319 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15320 BUILD_THROW (node, expr);
ce6e9147
APB
15321
15322 /* If doing xrefs, keep the location where the `throw' was seen. */
15323 if (flag_emit_xref)
15324 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15325 return node;
15326}
15327
15328/* Check that exception said to be thrown by method DECL can be
15329 effectively caught from where DECL is invoked. */
15330
15331static void
15332check_thrown_exceptions (location, decl)
15333 int location;
15334 tree decl;
15335{
15336 tree throws;
15337 /* For all the unchecked exceptions thrown by DECL */
15338 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15339 throws = TREE_CHAIN (throws))
0a2138e2 15340 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15341 {
3e78f871
PB
15342#if 1
15343 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15344 if (DECL_NAME (decl) == get_identifier ("clone"))
15345 continue;
15346#endif
b9f7e36c 15347 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15348 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15349 parse_error_context
15350 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15351 lang_printable_name (TREE_VALUE (throws), 0));
15352 else
15353 {
15354 parse_error_context
781b0558 15355 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15356 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15357 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15358 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15359 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15360 }
b9f7e36c
APB
15361 }
15362}
15363
c877974e 15364/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15365 try-catch blocks, OR is listed in the `throws' clause of the
15366 current method. */
15367
15368static int
0a2138e2 15369check_thrown_exceptions_do (exception)
b9f7e36c
APB
15370 tree exception;
15371{
15372 tree list = currently_caught_type_list;
c877974e 15373 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15374 /* First, all the nested try-catch-finally at that stage. The
15375 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15376 if (IS_UNCHECKED_EXCEPTION_P (exception))
15377 return 1;
b9f7e36c
APB
15378 while (list)
15379 {
15380 tree caught;
15381 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15382 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15383 return 1;
15384 list = TREE_CHAIN (list);
15385 }
15386 return 0;
15387}
15388
15389static void
15390purge_unchecked_exceptions (mdecl)
15391 tree mdecl;
15392{
15393 tree throws = DECL_FUNCTION_THROWS (mdecl);
15394 tree new = NULL_TREE;
15395
15396 while (throws)
15397 {
15398 tree next = TREE_CHAIN (throws);
c877974e 15399 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15400 {
15401 TREE_CHAIN (throws) = new;
15402 new = throws;
15403 }
15404 throws = next;
15405 }
15406 /* List is inverted here, but it doesn't matter */
15407 DECL_FUNCTION_THROWS (mdecl) = new;
15408}
22eed1e6
APB
15409
15410/* 15.24 Conditional Operator ?: */
15411
15412static tree
15413patch_conditional_expr (node, wfl_cond, wfl_op1)
15414 tree node, wfl_cond, wfl_op1;
15415{
15416 tree cond = TREE_OPERAND (node, 0);
15417 tree op1 = TREE_OPERAND (node, 1);
15418 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15419 tree resulting_type = NULL_TREE;
ac825856 15420 tree t1, t2, patched;
22eed1e6
APB
15421 int error_found = 0;
15422
ac825856
APB
15423 /* Operands of ?: might be StringBuffers crafted as a result of a
15424 string concatenation. Obtain a descent operand here. */
15425 if ((patched = patch_string (op1)))
15426 TREE_OPERAND (node, 1) = op1 = patched;
15427 if ((patched = patch_string (op2)))
15428 TREE_OPERAND (node, 2) = op2 = patched;
15429
15430 t1 = TREE_TYPE (op1);
15431 t2 = TREE_TYPE (op2);
15432
22eed1e6
APB
15433 /* The first expression must be a boolean */
15434 if (TREE_TYPE (cond) != boolean_type_node)
15435 {
15436 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15437 parse_error_context (wfl_operator,
15438 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15439 lang_printable_name (TREE_TYPE (cond), 0));
15440 error_found = 1;
15441 }
15442
15443 /* Second and third can be numeric, boolean (i.e. primitive),
15444 references or null. Anything else results in an error */
15445 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15446 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15447 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15448 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15449 error_found = 1;
15450
15451 /* Determine the type of the conditional expression. Same types are
15452 easy to deal with */
15453 else if (t1 == t2)
15454 resulting_type = t1;
15455
15456 /* There are different rules for numeric types */
15457 else if (JNUMERIC_TYPE_P (t1))
15458 {
15459 /* if byte/short found, the resulting type is short */
15460 if ((t1 == byte_type_node && t2 == short_type_node)
15461 || (t1 == short_type_node && t2 == byte_type_node))
15462 resulting_type = short_type_node;
15463
15464 /* If t1 is a constant int and t2 is of type byte, short or char
15465 and t1's value fits in t2, then the resulting type is t2 */
15466 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15467 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15468 resulting_type = t2;
15469
15470 /* If t2 is a constant int and t1 is of type byte, short or char
15471 and t2's value fits in t1, then the resulting type is t1 */
15472 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15473 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15474 resulting_type = t1;
15475
15476 /* Otherwise, binary numeric promotion is applied and the
15477 resulting type is the promoted type of operand 1 and 2 */
15478 else
93024893 15479 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15480 &TREE_OPERAND (node, 1),
15481 &TREE_OPERAND (node, 2));
15482 }
15483
15484 /* Cases of a reference and a null type */
15485 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15486 resulting_type = t1;
15487
15488 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15489 resulting_type = t2;
15490
15491 /* Last case: different reference types. If a type can be converted
15492 into the other one by assignment conversion, the latter
15493 determines the type of the expression */
15494 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15495 resulting_type = promote_type (t1);
15496
15497 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15498 resulting_type = promote_type (t2);
15499
15500 /* If we don't have any resulting type, we're in trouble */
15501 if (!resulting_type)
15502 {
c2e3db92 15503 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15504 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15505 parse_error_context (wfl_operator,
15506 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15507 t, lang_printable_name (t2, 0));
22eed1e6
APB
15508 free (t);
15509 error_found = 1;
15510 }
15511
15512 if (error_found)
15513 {
15514 TREE_TYPE (node) = error_mark_node;
15515 return error_mark_node;
15516 }
15517
15518 TREE_TYPE (node) = resulting_type;
15519 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15520 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15521 return node;
15522}
ac825856 15523
5b09b33e
PB
15524/* Try to constant fold NODE.
15525 If NODE is not a constant expression, return NULL_EXPR.
15526 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15527
15528static tree
15529fold_constant_for_init (node, context)
15530 tree node;
15531 tree context;
15532{
15533 tree op0, op1, val;
15534 enum tree_code code = TREE_CODE (node);
15535
ee97d354 15536 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15537 return node;
93024893 15538
5b09b33e
PB
15539 switch (code)
15540 {
5b09b33e
PB
15541 case PLUS_EXPR:
15542 case MINUS_EXPR:
bc3ca41b
PB
15543 case MULT_EXPR:
15544 case TRUNC_MOD_EXPR:
15545 case RDIV_EXPR:
5b09b33e
PB
15546 case LSHIFT_EXPR:
15547 case RSHIFT_EXPR:
15548 case URSHIFT_EXPR:
15549 case BIT_AND_EXPR:
15550 case BIT_XOR_EXPR:
15551 case BIT_IOR_EXPR:
5b09b33e
PB
15552 case TRUTH_ANDIF_EXPR:
15553 case TRUTH_ORIF_EXPR:
15554 case EQ_EXPR:
15555 case NE_EXPR:
15556 case GT_EXPR:
15557 case GE_EXPR:
15558 case LT_EXPR:
15559 case LE_EXPR:
15560 op0 = TREE_OPERAND (node, 0);
15561 op1 = TREE_OPERAND (node, 1);
15562 val = fold_constant_for_init (op0, context);
15563 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15564 return NULL_TREE;
15565 TREE_OPERAND (node, 0) = val;
15566 val = fold_constant_for_init (op1, context);
15567 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15568 return NULL_TREE;
15569 TREE_OPERAND (node, 1) = val;
15570 return patch_binop (node, op0, op1);
15571
15572 case UNARY_PLUS_EXPR:
15573 case NEGATE_EXPR:
15574 case TRUTH_NOT_EXPR:
15575 case BIT_NOT_EXPR:
15576 case CONVERT_EXPR:
15577 op0 = TREE_OPERAND (node, 0);
15578 val = fold_constant_for_init (op0, context);
15579 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15580 return NULL_TREE;
15581 TREE_OPERAND (node, 0) = val;
5a005d9e 15582 return patch_unaryop (node, op0);
5b09b33e
PB
15583 break;
15584
15585 case COND_EXPR:
15586 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15587 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15588 return NULL_TREE;
15589 TREE_OPERAND (node, 0) = val;
15590 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15591 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15592 return NULL_TREE;
15593 TREE_OPERAND (node, 1) = val;
15594 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15595 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15596 return NULL_TREE;
15597 TREE_OPERAND (node, 2) = val;
15598 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15599 : TREE_OPERAND (node, 2);
15600
15601 case VAR_DECL:
8576f094
APB
15602 case FIELD_DECL:
15603 if (! FIELD_FINAL (node)
5b09b33e
PB
15604 || DECL_INITIAL (node) == NULL_TREE)
15605 return NULL_TREE;
15606 val = DECL_INITIAL (node);
15607 /* Guard against infinite recursion. */
15608 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15609 val = fold_constant_for_init (val, node);
5b09b33e 15610 DECL_INITIAL (node) = val;
c7303e41
APB
15611 if (!val && CLASS_FINAL_VARIABLE_P (node))
15612 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15613 return val;
15614
15615 case EXPR_WITH_FILE_LOCATION:
15616 /* Compare java_complete_tree and resolve_expression_name. */
15617 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15618 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15619 {
15620 tree name = EXPR_WFL_NODE (node);
15621 tree decl;
15622 if (PRIMARY_P (node))
15623 return NULL_TREE;
15624 else if (! QUALIFIED_P (name))
15625 {
15626 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15627 if (decl == NULL_TREE
15628 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15629 return NULL_TREE;
15630 return fold_constant_for_init (decl, decl);
15631 }
15632 else
15633 {
5b09b33e
PB
15634 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15635 qualify_ambiguous_name (node);
15636 if (resolve_field_access (node, &decl, NULL)
15637 && decl != NULL_TREE)
15638 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15639 return NULL_TREE;
15640 }
15641 }
15642 else
15643 {
15644 op0 = TREE_OPERAND (node, 0);
15645 val = fold_constant_for_init (op0, context);
15646 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15647 return NULL_TREE;
15648 TREE_OPERAND (node, 0) = val;
15649 return val;
15650 }
15651
bc3ca41b
PB
15652#ifdef USE_COMPONENT_REF
15653 case IDENTIFIER:
15654 case COMPONENT_REF:
15655 ?;
15656#endif
15657
5b09b33e
PB
15658 default:
15659 return NULL_TREE;
15660 }
15661}
bc3ca41b
PB
15662
15663#ifdef USE_COMPONENT_REF
15664/* Context is 'T' for TypeName, 'P' for PackageName,
15665 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15666
15667tree
15668resolve_simple_name (name, context)
15669 tree name;
15670 int context;
15671{
15672}
15673
15674tree
15675resolve_qualified_name (name, context)
15676 tree name;
15677 int context;
15678{
15679}
15680#endif
f15b9af9
MM
15681
15682/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15683
15684static void
15685mark_parser_ctxt (p)
15686 void *p;
15687{
15688 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15689 int i;
15690
15691 if (!pc)
15692 return;
15693
15694#ifndef JC1_LITE
15695 for (i = 0; i < 11; ++i)
15696 ggc_mark_tree (pc->modifier_ctx[i]);
15697 ggc_mark_tree (pc->class_type);
15698 ggc_mark_tree (pc->function_decl);
15699 ggc_mark_tree (pc->package);
15700 ggc_mark_tree (pc->incomplete_class);
15701 ggc_mark_tree (pc->gclass_list);
15702 ggc_mark_tree (pc->class_list);
15703 ggc_mark_tree (pc->current_parsed_class);
15704 ggc_mark_tree (pc->current_parsed_class_un);
15705 ggc_mark_tree (pc->non_static_initialized);
15706 ggc_mark_tree (pc->static_initialized);
15707 ggc_mark_tree (pc->instance_initializers);
15708 ggc_mark_tree (pc->import_list);
15709 ggc_mark_tree (pc->import_demand_list);
15710 ggc_mark_tree (pc->current_loop);
15711 ggc_mark_tree (pc->current_labeled_block);
15712#endif /* JC1_LITE */
15713
15714 if (pc->next)
15715 mark_parser_ctxt (&pc->next);
15716}
This page took 2.888299 seconds and 5 git commands to generate.