]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
regrename.c (scan_rtx_reg): Terminate the chain rather than abort on mark_read with...
[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));
cf1748bf 105static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 106static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 107static void register_package PARAMS ((tree));
df32d2ce
KG
108static tree resolve_package PARAMS ((tree, tree *));
109static tree lookup_package_type PARAMS ((const char *, int));
110static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 111static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
112static void declare_local_variables PARAMS ((int, tree, tree));
113static void source_start_java_method PARAMS ((tree));
114static void source_end_java_method PARAMS ((void));
115static void expand_start_java_method PARAMS ((tree));
116static tree find_name_in_single_imports PARAMS ((tree));
117static void check_abstract_method_header PARAMS ((tree));
118static tree lookup_java_interface_method2 PARAMS ((tree, tree));
119static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 120static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 121static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 122 tree, tree, tree));
df32d2ce 123static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 124 int *, tree *));
df32d2ce
KG
125static int breakdown_qualified PARAMS ((tree *, tree *, tree));
126static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 127static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
128static tree resolve_no_layout PARAMS ((tree, tree));
129static int invocation_mode PARAMS ((tree, int));
130static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 131 tree, tree));
df32d2ce 132static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 133 tree *, tree *));
df32d2ce
KG
134static tree find_most_specific_methods_list PARAMS ((tree));
135static int argument_types_convertible PARAMS ((tree, tree));
136static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 137static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
138static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
139static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
140static tree obtain_incomplete_type PARAMS ((tree));
141static tree java_complete_lhs PARAMS ((tree));
142static tree java_complete_tree PARAMS ((tree));
c2952b01 143static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 144static int analyze_clinit_body PARAMS ((tree));
92d83515 145static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
146static void java_complete_expand_method PARAMS ((tree));
147static int unresolved_type_p PARAMS ((tree, tree *));
148static void create_jdep_list PARAMS ((struct parser_ctxt *));
149static tree build_expr_block PARAMS ((tree, tree));
150static tree enter_block PARAMS ((void));
151static tree enter_a_block PARAMS ((tree));
152static tree exit_block PARAMS ((void));
153static tree lookup_name_in_blocks PARAMS ((tree));
154static void maybe_absorb_scoping_blocks PARAMS ((void));
155static tree build_method_invocation PARAMS ((tree, tree));
156static tree build_new_invocation PARAMS ((tree, tree));
157static tree build_assignment PARAMS ((int, int, tree, tree));
158static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
159static int check_final_assignment PARAMS ((tree ,tree));
160static tree patch_assignment PARAMS ((tree, tree, tree ));
161static tree patch_binop PARAMS ((tree, tree, tree));
162static tree build_unaryop PARAMS ((int, int, tree));
163static tree build_incdec PARAMS ((int, int, tree, int));
164static tree patch_unaryop PARAMS ((tree, tree));
165static tree build_cast PARAMS ((int, tree, tree));
166static tree build_null_of_type PARAMS ((tree));
167static tree patch_cast PARAMS ((tree, tree));
168static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
169static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
170static int valid_cast_to_p PARAMS ((tree, tree));
171static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
172static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
173static tree try_reference_assignconv PARAMS ((tree, tree));
174static tree build_unresolved_array_type PARAMS ((tree));
175static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
176static tree build_array_ref PARAMS ((int, tree, tree));
177static tree patch_array_ref PARAMS ((tree));
178static tree make_qualified_name PARAMS ((tree, tree, int));
179static tree merge_qualified_name PARAMS ((tree, tree));
180static tree make_qualified_primary PARAMS ((tree, tree, int));
181static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 182 tree *, tree *));
df32d2ce 183static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
184static tree resolve_field_access PARAMS ((tree, tree *, tree *));
185static tree build_newarray_node PARAMS ((tree, tree, int));
186static tree patch_newarray PARAMS ((tree));
187static tree resolve_type_during_patch PARAMS ((tree));
188static tree build_this PARAMS ((int));
9a7ab4b3 189static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
190static tree build_return PARAMS ((int, tree));
191static tree patch_return PARAMS ((tree));
192static tree maybe_access_field PARAMS ((tree, tree, tree));
193static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
194static int check_for_static_method_reference PARAMS ((tree, tree, tree,
195 tree, tree));
df32d2ce
KG
196static int not_accessible_p PARAMS ((tree, tree, int));
197static void check_deprecation PARAMS ((tree, tree));
198static int class_in_current_package PARAMS ((tree));
199static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
200static tree patch_if_else_statement PARAMS ((tree));
201static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
202static tree add_stmt_to_block PARAMS ((tree, tree, tree));
203static tree patch_exit_expr PARAMS ((tree));
204static tree build_labeled_block PARAMS ((int, tree));
205static tree finish_labeled_statement PARAMS ((tree, tree));
206static tree build_bc_statement PARAMS ((int, int, tree));
207static tree patch_bc_statement PARAMS ((tree));
208static tree patch_loop_statement PARAMS ((tree));
209static tree build_new_loop PARAMS ((tree));
210static tree build_loop_body PARAMS ((int, tree, int));
211static tree finish_loop_body PARAMS ((int, tree, tree, int));
212static tree build_debugable_stmt PARAMS ((int, tree));
213static tree finish_for_loop PARAMS ((int, tree, tree, tree));
214static tree patch_switch_statement PARAMS ((tree));
215static tree string_constant_concatenation PARAMS ((tree, tree));
216static tree build_string_concatenation PARAMS ((tree, tree));
217static tree patch_string_cst PARAMS ((tree));
218static tree patch_string PARAMS ((tree));
219static tree build_try_statement PARAMS ((int, tree, tree));
220static tree build_try_finally_statement PARAMS ((int, tree, tree));
221static tree patch_try_statement PARAMS ((tree));
222static tree patch_synchronized_statement PARAMS ((tree, tree));
223static tree patch_throw_statement PARAMS ((tree, tree));
224static void check_thrown_exceptions PARAMS ((int, tree));
225static int check_thrown_exceptions_do PARAMS ((tree));
226static void purge_unchecked_exceptions PARAMS ((tree));
227static void check_throws_clauses PARAMS ((tree, tree, tree));
228static void finish_method_declaration PARAMS ((tree));
229static tree build_super_invocation PARAMS ((tree));
230static int verify_constructor_circularity PARAMS ((tree, tree));
231static char *constructor_circularity_msg PARAMS ((tree, tree));
232static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 233 int, int));
df32d2ce
KG
234static const char *get_printable_method_name PARAMS ((tree));
235static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
236static tree generate_finit PARAMS ((tree));
237static void add_instance_initializer PARAMS ((tree));
df32d2ce 238static void fix_constructors PARAMS ((tree));
c2952b01
APB
239static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
240 tree, int *));
241static void craft_constructor PARAMS ((tree, tree));
242static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
243static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
244static void start_artificial_method_body PARAMS ((tree));
245static void end_artificial_method_body PARAMS ((tree));
246static int check_method_redefinition PARAMS ((tree, tree));
247static int reset_method_name PARAMS ((tree));
165f37bc 248static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
249static void java_check_regular_methods PARAMS ((tree));
250static void java_check_abstract_methods PARAMS ((tree));
251static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
252static void unreachable_stmt_error PARAMS ((tree));
253static tree find_expr_with_wfl PARAMS ((tree));
254static void missing_return_error PARAMS ((tree));
255static tree build_new_array_init PARAMS ((int, tree));
256static tree patch_new_array_init PARAMS ((tree, tree));
257static tree maybe_build_array_element_wfl PARAMS ((tree));
258static int array_constructor_check_entry PARAMS ((tree, tree));
259static const char *purify_type_name PARAMS ((const char *));
260static tree fold_constant_for_init PARAMS ((tree, tree));
261static tree strip_out_static_field_access_decl PARAMS ((tree));
262static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
263static void static_ref_err PARAMS ((tree, tree, tree));
264static void parser_add_interface PARAMS ((tree, tree, tree));
265static void add_superinterfaces PARAMS ((tree, tree));
266static tree jdep_resolve_class PARAMS ((jdep *));
267static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
268static void java_complete_expand_classes PARAMS ((void));
269static void java_complete_expand_class PARAMS ((tree));
270static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
271static tree cut_identifier_in_qualified PARAMS ((tree));
272static tree java_stabilize_reference PARAMS ((tree));
273static tree do_unary_numeric_promotion PARAMS ((tree));
274static char * operator_string PARAMS ((tree));
275static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
276static tree merge_string_cste PARAMS ((tree, tree, int));
277static tree java_refold PARAMS ((tree));
278static int java_decl_equiv PARAMS ((tree, tree));
279static int binop_compound_p PARAMS ((enum tree_code));
280static tree search_loop PARAMS ((tree));
281static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 282static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
283static void java_check_abstract_method_definitions PARAMS ((tree));
284static void java_debug_context_do PARAMS ((int));
c2952b01
APB
285static void java_parser_context_push_initialized_field PARAMS ((void));
286static void java_parser_context_pop_initialized_field PARAMS ((void));
287static tree reorder_static_initialized PARAMS ((tree));
288static void java_parser_context_suspend PARAMS ((void));
289static void java_parser_context_resume PARAMS ((void));
290
291/* JDK 1.1 work. FIXME */
292
293static tree maybe_make_nested_class_name PARAMS ((tree));
294static void make_nested_class_name PARAMS ((tree));
295static void set_nested_class_simple_name_value PARAMS ((tree, int));
296static void link_nested_class_to_enclosing PARAMS ((void));
297static tree find_as_inner_class PARAMS ((tree, tree, tree));
298static tree find_as_inner_class_do PARAMS ((tree, tree));
299static int check_inner_class_redefinition PARAMS ((tree, tree));
300
301static tree build_thisn_assign PARAMS ((void));
302static tree build_current_thisn PARAMS ((tree));
303static tree build_access_to_thisn PARAMS ((tree, tree, int));
304static tree maybe_build_thisn_access_method PARAMS ((tree));
305
306static tree build_outer_field_access PARAMS ((tree, tree));
307static tree build_outer_field_access_methods PARAMS ((tree));
308static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
309 tree, tree));
310static tree build_outer_method_access_method PARAMS ((tree));
311static tree build_new_access_id PARAMS ((void));
312static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
313 tree, tree));
314
315static int outer_field_access_p PARAMS ((tree, tree));
316static int outer_field_expanded_access_p PARAMS ((tree, tree *,
317 tree *, tree *));
318static tree outer_field_access_fix PARAMS ((tree, tree, tree));
319static tree build_incomplete_class_ref PARAMS ((int, tree));
320static tree patch_incomplete_class_ref PARAMS ((tree));
321static tree create_anonymous_class PARAMS ((int, tree));
322static void patch_anonymous_class PARAMS ((tree, tree, tree));
323static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 324
165f37bc
APB
325static tree build_dot_class_method PARAMS ((tree));
326static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 327static void create_new_parser_context PARAMS ((int));
f15b9af9 328static void mark_parser_ctxt PARAMS ((void *));
165f37bc 329
e04a16fb
AG
330/* Number of error found so far. */
331int java_error_count;
332/* Number of warning found so far. */
333int java_warning_count;
ce6e9147
APB
334/* Tell when not to fold, when doing xrefs */
335int do_not_fold;
c2952b01 336/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 337const char *cyclic_inheritance_report;
f15b9af9 338
c2952b01
APB
339/* Tell when we're within an instance initializer */
340static int in_instance_initializer;
e04a16fb
AG
341
342/* The current parser context */
d4370213 343struct parser_ctxt *ctxp;
e04a16fb 344
d4370213 345/* List of things that were analyzed for which code will be generated */
b351b287
APB
346static struct parser_ctxt *ctxp_for_generation = NULL;
347
e04a16fb
AG
348/* binop_lookup maps token to tree_code. It is used where binary
349 operations are involved and required by the parser. RDIV_EXPR
350 covers both integral/floating point division. The code is changed
351 once the type of both operator is worked out. */
352
353static enum tree_code binop_lookup[19] =
354 {
355 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
356 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
357 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
358 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
359 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
360 };
361#define BINOP_LOOKUP(VALUE) \
6e2aa220 362 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 363
5cbdba64
APB
364/* This is the end index for binary operators that can also be used
365 in compound assignements. */
366#define BINOP_COMPOUND_CANDIDATES 11
367
e04a16fb 368/* The "$L" identifier we use to create labels. */
b67d701b
PB
369static tree label_id = NULL_TREE;
370
371/* The "StringBuffer" identifier used for the String `+' operator. */
372static tree wfl_string_buffer = NULL_TREE;
373
374/* The "append" identifier used for String `+' operator. */
375static tree wfl_append = NULL_TREE;
376
377/* The "toString" identifier used for String `+' operator. */
378static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
379
380/* The "java.lang" import qualified name. */
381static tree java_lang_id = NULL_TREE;
09ed0f70 382
c2952b01
APB
383/* The generated `inst$' identifier used for generated enclosing
384 instance/field access functions. */
385static tree inst_id = NULL_TREE;
386
09ed0f70
APB
387/* The "java.lang.Cloneable" qualified name. */
388static tree java_lang_cloneable = NULL_TREE;
f099f336 389
ee17a290
TT
390/* The "java.io.Serializable" qualified name. */
391static tree java_io_serializable = NULL_TREE;
392
f099f336
APB
393/* Context and flag for static blocks */
394static tree current_static_block = NULL_TREE;
395
c2952b01
APB
396/* The generated `write_parm_value$' identifier. */
397static tree wpv_id;
398
ee07f4f4
APB
399/* The list of all packages we've seen so far */
400static tree package_list = NULL_TREE;
2884c41e 401
19e223db
MM
402/* Hold THIS for the scope of the current public method decl. */
403static tree current_this;
404
405/* Hold a list of catch clauses list. The first element of this list is
406 the list of the catch clauses of the currently analysed try block. */
407static tree currently_caught_type_list;
408
2884c41e
KG
409/* Check modifiers. If one doesn't fit, retrieve it in its declaration
410 line and point it out. */
411/* Should point out the one that don't fit. ASCII/unicode, going
412 backward. FIXME */
413
414#define check_modifiers(__message, __value, __mask) do { \
415 if ((__value) & ~(__mask)) \
416 { \
417 int i, remainder = (__value) & ~(__mask); \
418 for (i = 0; i <= 10; i++) \
419 if ((1 << i) & remainder) \
420 parse_error_context (ctxp->modifier_ctx [i], (__message), \
421 java_accstring_lookup (1 << i)); \
422 } \
423} while (0)
ee07f4f4 424
e04a16fb
AG
425%}
426
427%union {
428 tree node;
429 int sub_token;
430 struct {
431 int token;
432 int location;
433 } operator;
434 int value;
435}
436
9ee9b555
KG
437%{
438#include "lex.c"
439%}
440
e04a16fb
AG
441%pure_parser
442
443/* Things defined here have to match the order of what's in the
444 binop_lookup table. */
445
446%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
447%token LS_TK SRS_TK ZRS_TK
448%token AND_TK XOR_TK OR_TK
449%token BOOL_AND_TK BOOL_OR_TK
450%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
451
452/* This maps to the same binop_lookup entry than the token above */
453
454%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
455%token REM_ASSIGN_TK
456%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
457%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
458
459
460/* Modifier TOKEN have to be kept in this order. Don't scramble it */
461
462%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
463%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
464%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
465%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 466%token STRICT_TK
e04a16fb
AG
467
468/* Keep those two in order, too */
469%token DECR_TK INCR_TK
470
471/* From now one, things can be in any order */
472
473%token DEFAULT_TK IF_TK THROW_TK
474%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
475%token THROWS_TK BREAK_TK IMPORT_TK
476%token ELSE_TK INSTANCEOF_TK RETURN_TK
477%token VOID_TK CATCH_TK INTERFACE_TK
478%token CASE_TK EXTENDS_TK FINALLY_TK
479%token SUPER_TK WHILE_TK CLASS_TK
480%token SWITCH_TK CONST_TK TRY_TK
481%token FOR_TK NEW_TK CONTINUE_TK
482%token GOTO_TK PACKAGE_TK THIS_TK
483
484%token BYTE_TK SHORT_TK INT_TK LONG_TK
485%token CHAR_TK INTEGRAL_TK
486
487%token FLOAT_TK DOUBLE_TK FP_TK
488
489%token ID_TK
490
491%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
492
493%token ASSIGN_ANY_TK ASSIGN_TK
494%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
495
496%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
497%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
498
c2952b01 499%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
500
501%type <node> super ID_TK identifier
502%type <node> name simple_name qualified_name
c2952b01 503%type <node> type_declaration compilation_unit
e04a16fb
AG
504 field_declaration method_declaration extends_interfaces
505 interfaces interface_type_list
c2952b01 506 class_member_declaration
e04a16fb
AG
507 import_declarations package_declaration
508 type_declarations interface_body
509 interface_member_declaration constant_declaration
510 interface_member_declarations interface_type
511 abstract_method_declaration interface_type_list
512%type <node> class_body_declaration class_member_declaration
513 static_initializer constructor_declaration block
22eed1e6 514%type <node> class_body_declarations constructor_header
e04a16fb
AG
515%type <node> class_or_interface_type class_type class_type_list
516 constructor_declarator explicit_constructor_invocation
b9f7e36c 517%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
518
519%type <node> variable_declarator_id variable_declarator
520 variable_declarators variable_initializer
22eed1e6 521 variable_initializers constructor_body
ac825856 522 array_initializer
e04a16fb 523
2e5eb5c5 524%type <node> class_body block_end constructor_block_end
e04a16fb
AG
525%type <node> statement statement_without_trailing_substatement
526 labeled_statement if_then_statement label_decl
527 if_then_else_statement while_statement for_statement
528 statement_nsi labeled_statement_nsi do_statement
529 if_then_else_statement_nsi while_statement_nsi
530 for_statement_nsi statement_expression_list for_init
531 for_update statement_expression expression_statement
532 primary_no_new_array expression primary
533 array_creation_expression array_type
534 class_instance_creation_expression field_access
535 method_invocation array_access something_dot_new
536 argument_list postfix_expression while_expression
537 post_increment_expression post_decrement_expression
538 unary_expression_not_plus_minus unary_expression
539 pre_increment_expression pre_decrement_expression
540 unary_expression_not_plus_minus cast_expression
541 multiplicative_expression additive_expression
542 shift_expression relational_expression
543 equality_expression and_expression
544 exclusive_or_expression inclusive_or_expression
545 conditional_and_expression conditional_or_expression
546 conditional_expression assignment_expression
547 left_hand_side assignment for_header for_begin
548 constant_expression do_statement_begin empty_statement
b67d701b 549 switch_statement synchronized_statement throw_statement
f8976021 550 try_statement switch_expression switch_block
15fdcfe9 551 catches catch_clause catch_clause_parameter finally
c2952b01 552 anonymous_class_creation
e04a16fb
AG
553%type <node> return_statement break_statement continue_statement
554
555%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
556%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
557%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
558%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
559%type <operator> ASSIGN_ANY_TK assignment_operator
560%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
561%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
562%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 563%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 564%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
565%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
566%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 567%type <operator> NEW_TK
e04a16fb
AG
568
569%type <node> method_body
570
571%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
572 STRING_LIT_TK NULL_TK VOID_TK
573
574%type <node> IF_TK WHILE_TK FOR_TK
575
576%type <node> formal_parameter_list formal_parameter
577 method_declarator method_header
578
c2952b01 579%type <node> primitive_type reference_type type
e04a16fb
AG
580 BOOLEAN_TK INTEGRAL_TK FP_TK
581
c2952b01
APB
582/* Added or modified JDK 1.1 rule types */
583%type <node> type_literals array_type_literal
584
e04a16fb
AG
585%%
586/* 19.2 Production from 2.3: The Syntactic Grammar */
587goal:
19e223db
MM
588 {
589 /* Register static variables with the garbage
590 collector. */
591 ggc_add_tree_root (&label_id, 1);
592 ggc_add_tree_root (&wfl_string_buffer, 1);
593 ggc_add_tree_root (&wfl_append, 1);
594 ggc_add_tree_root (&wfl_to_string, 1);
595 ggc_add_tree_root (&java_lang_id, 1);
596 ggc_add_tree_root (&inst_id, 1);
597 ggc_add_tree_root (&java_lang_cloneable, 1);
598 ggc_add_tree_root (&java_io_serializable, 1);
599 ggc_add_tree_root (&current_static_block, 1);
600 ggc_add_tree_root (&wpv_id, 1);
601 ggc_add_tree_root (&package_list, 1);
602 ggc_add_tree_root (&current_this, 1);
603 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
604 ggc_add_root (&ctxp, 1,
605 sizeof (struct parser_ctxt *),
606 mark_parser_ctxt);
607 ggc_add_root (&ctxp_for_generation, 1,
608 sizeof (struct parser_ctxt *),
609 mark_parser_ctxt);
19e223db 610 }
e04a16fb
AG
611 compilation_unit
612 {}
613;
614
615/* 19.3 Productions from 3: Lexical structure */
616literal:
617 INT_LIT_TK
618| FP_LIT_TK
619| BOOL_LIT_TK
620| CHAR_LIT_TK
621| STRING_LIT_TK
622| NULL_TK
623;
624
625/* 19.4 Productions from 4: Types, Values and Variables */
626type:
627 primitive_type
628| reference_type
629;
630
631primitive_type:
632 INTEGRAL_TK
633| FP_TK
634| BOOLEAN_TK
635;
636
637reference_type:
638 class_or_interface_type
639| array_type
640;
641
642class_or_interface_type:
643 name
644;
645
646class_type:
647 class_or_interface_type /* Default rule */
648;
649
650interface_type:
651 class_or_interface_type
652;
653
654array_type:
655 primitive_type OSB_TK CSB_TK
656 {
657 $$ = build_java_array_type ($1, -1);
658 CLASS_LOADED_P ($$) = 1;
659 }
660| name OSB_TK CSB_TK
661 { $$ = build_unresolved_array_type ($1); }
662| array_type OSB_TK CSB_TK
663 { $$ = build_unresolved_array_type ($1); }
664| primitive_type OSB_TK error
665 {RULE ("']' expected"); RECOVER;}
666| array_type OSB_TK error
667 {RULE ("']' expected"); RECOVER;}
668;
669
670/* 19.5 Productions from 6: Names */
671name:
672 simple_name /* Default rule */
673| qualified_name /* Default rule */
674;
675
676simple_name:
677 identifier /* Default rule */
678;
679
680qualified_name:
681 name DOT_TK identifier
682 { $$ = make_qualified_name ($1, $3, $2.location); }
683;
684
685identifier:
686 ID_TK
687;
688
689/* 19.6: Production from 7: Packages */
690compilation_unit:
691 {$$ = NULL;}
692| package_declaration
693| import_declarations
694| type_declarations
695| package_declaration import_declarations
696| package_declaration type_declarations
697| import_declarations type_declarations
698| package_declaration import_declarations type_declarations
699;
700
701import_declarations:
702 import_declaration
703 {
704 $$ = NULL;
705 }
706| import_declarations import_declaration
707 {
708 $$ = NULL;
709 }
710;
711
712type_declarations:
713 type_declaration
714| type_declarations type_declaration
715;
716
717package_declaration:
718 PACKAGE_TK name SC_TK
ee07f4f4
APB
719 {
720 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 721 register_package (ctxp->package);
ee07f4f4 722 }
e04a16fb
AG
723| PACKAGE_TK error
724 {yyerror ("Missing name"); RECOVER;}
725| PACKAGE_TK name error
726 {yyerror ("';' expected"); RECOVER;}
727;
728
729import_declaration:
730 single_type_import_declaration
731| type_import_on_demand_declaration
732;
733
734single_type_import_declaration:
735 IMPORT_TK name SC_TK
736 {
9a7ab4b3 737 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 738 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 739 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
740 while (last != IDENTIFIER_POINTER (name))
741 {
742 if (last [0] == '.')
743 break;
744 last--;
745 }
746 last_name = get_identifier (++last);
747 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
748 {
749 tree err = find_name_in_single_imports (last_name);
750 if (err && err != name)
751 parse_error_context
752 ($2, "Ambiguous class: `%s' and `%s'",
753 IDENTIFIER_POINTER (name),
754 IDENTIFIER_POINTER (err));
5e942c50 755 else
9a7ab4b3 756 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
757 }
758 else
5e942c50 759 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
760 }
761| IMPORT_TK error
762 {yyerror ("Missing name"); RECOVER;}
763| IMPORT_TK name error
764 {yyerror ("';' expected"); RECOVER;}
765;
766
767type_import_on_demand_declaration:
768 IMPORT_TK name DOT_TK MULT_TK SC_TK
769 {
770 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
771 /* Don't import java.lang.* twice. */
772 if (name != java_lang_id)
773 {
ba179f9f 774 read_import_dir ($2);
9a7ab4b3
APB
775 ctxp->import_demand_list =
776 chainon (ctxp->import_demand_list,
777 build_tree_list ($2, NULL_TREE));
ba179f9f 778 }
e04a16fb
AG
779 }
780| IMPORT_TK name DOT_TK error
781 {yyerror ("'*' expected"); RECOVER;}
782| IMPORT_TK name DOT_TK MULT_TK error
783 {yyerror ("';' expected"); RECOVER;}
784;
785
786type_declaration:
787 class_declaration
c2952b01 788 { end_class_declaration (0); }
e04a16fb 789| interface_declaration
c2952b01 790 { end_class_declaration (0); }
5f1c312a 791| empty_statement
e04a16fb
AG
792| error
793 {
794 YYERROR_NOW;
795 yyerror ("Class or interface declaration expected");
796 }
797;
798
799/* 19.7 Shortened from the original:
800 modifiers: modifier | modifiers modifier
801 modifier: any of public... */
802modifiers:
803 MODIFIER_TK
804 {
805 $$ = (1 << $1);
806 }
807| modifiers MODIFIER_TK
808 {
809 int acc = (1 << $2);
810 if ($$ & acc)
811 parse_error_context
812 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
813 java_accstring_lookup (acc));
814 else
815 {
816 $$ |= acc;
817 }
818 }
819;
820
821/* 19.8.1 Production from $8.1: Class Declaration */
822class_declaration:
823 modifiers CLASS_TK identifier super interfaces
824 { create_class ($1, $3, $4, $5); }
825 class_body
e04a16fb
AG
826| CLASS_TK identifier super interfaces
827 { create_class (0, $2, $3, $4); }
828 class_body
e04a16fb
AG
829| modifiers CLASS_TK error
830 {yyerror ("Missing class name"); RECOVER;}
831| CLASS_TK error
832 {yyerror ("Missing class name"); RECOVER;}
833| CLASS_TK identifier error
0b4d333e
APB
834 {
835 if (!ctxp->class_err) yyerror ("'{' expected");
836 DRECOVER(class1);
837 }
e04a16fb
AG
838| modifiers CLASS_TK identifier error
839 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
840;
841
842super:
843 { $$ = NULL; }
844| EXTENDS_TK class_type
845 { $$ = $2; }
846| EXTENDS_TK class_type error
847 {yyerror ("'{' expected"); ctxp->class_err=1;}
848| EXTENDS_TK error
849 {yyerror ("Missing super class name"); ctxp->class_err=1;}
850;
851
852interfaces:
853 { $$ = NULL_TREE; }
854| IMPLEMENTS_TK interface_type_list
855 { $$ = $2; }
856| IMPLEMENTS_TK error
857 {
858 ctxp->class_err=1;
859 yyerror ("Missing interface name");
860 }
861;
862
863interface_type_list:
864 interface_type
865 {
866 ctxp->interface_number = 1;
867 $$ = build_tree_list ($1, NULL_TREE);
868 }
869| interface_type_list C_TK interface_type
870 {
871 ctxp->interface_number++;
872 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
873 }
874| interface_type_list C_TK error
875 {yyerror ("Missing interface name"); RECOVER;}
876;
877
878class_body:
879 OCB_TK CCB_TK
7f10c2e2
APB
880 {
881 /* Store the location of the `}' when doing xrefs */
882 if (flag_emit_xref)
c2952b01 883 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 884 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 885 $$ = GET_CPC ();
7f10c2e2 886 }
e04a16fb 887| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
888 {
889 /* Store the location of the `}' when doing xrefs */
890 if (flag_emit_xref)
c2952b01 891 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 892 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 893 $$ = GET_CPC ();
7f10c2e2 894 }
e04a16fb
AG
895;
896
897class_body_declarations:
898 class_body_declaration
899| class_body_declarations class_body_declaration
900;
901
902class_body_declaration:
903 class_member_declaration
904| static_initializer
905| constructor_declaration
906| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
907 {
908 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
909 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
910 }
e04a16fb
AG
911;
912
913class_member_declaration:
914 field_declaration
915| method_declaration
916| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
917 { end_class_declaration (1); }
918| interface_declaration /* Added, JDK1.1 inner interfaces */
919 { end_class_declaration (1); }
5f1c312a 920| empty_statement
e04a16fb
AG
921;
922
923/* 19.8.2 Productions from 8.3: Field Declarations */
924field_declaration:
925 type variable_declarators SC_TK
926 { register_fields (0, $1, $2); }
927| modifiers type variable_declarators SC_TK
928 {
e04a16fb
AG
929 check_modifiers
930 ("Illegal modifier `%s' for field declaration",
931 $1, FIELD_MODIFIERS);
932 check_modifiers_consistency ($1);
933 register_fields ($1, $2, $3);
934 }
935;
936
937variable_declarators:
938 /* Should we use build_decl_list () instead ? FIXME */
939 variable_declarator /* Default rule */
940| variable_declarators C_TK variable_declarator
941 { $$ = chainon ($1, $3); }
942| variable_declarators C_TK error
943 {yyerror ("Missing term"); RECOVER;}
944;
945
946variable_declarator:
947 variable_declarator_id
948 { $$ = build_tree_list ($1, NULL_TREE); }
949| variable_declarator_id ASSIGN_TK variable_initializer
950 {
951 if (java_error_count)
952 $3 = NULL_TREE;
953 $$ = build_tree_list
954 ($1, build_assignment ($2.token, $2.location, $1, $3));
955 }
956| variable_declarator_id ASSIGN_TK error
957 {
958 yyerror ("Missing variable initializer");
959 $$ = build_tree_list ($1, NULL_TREE);
960 RECOVER;
961 }
962| variable_declarator_id ASSIGN_TK variable_initializer error
963 {
964 yyerror ("';' expected");
965 $$ = build_tree_list ($1, NULL_TREE);
966 RECOVER;
967 }
968;
969
970variable_declarator_id:
971 identifier
972| variable_declarator_id OSB_TK CSB_TK
c583dd46 973 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
974| identifier error
975 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
976| variable_declarator_id OSB_TK error
29f8b718
APB
977 {
978 tree node = java_lval.node;
979 if (node && (TREE_CODE (node) == INTEGER_CST
980 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
981 yyerror ("Can't specify array dimension in a declaration");
982 else
983 yyerror ("']' expected");
984 DRECOVER(vdi);
985 }
e04a16fb
AG
986| variable_declarator_id CSB_TK error
987 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
988;
989
990variable_initializer:
991 expression
992| array_initializer
e04a16fb
AG
993;
994
995/* 19.8.3 Productions from 8.4: Method Declarations */
996method_declaration:
997 method_header
998 {
999 current_function_decl = $1;
c2952b01
APB
1000 if (current_function_decl
1001 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1002 source_start_java_method (current_function_decl);
1003 else
1004 current_function_decl = NULL_TREE;
e04a16fb
AG
1005 }
1006 method_body
b635eb2f 1007 { finish_method_declaration ($3); }
e04a16fb
AG
1008| method_header error
1009 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1010;
1011
1012method_header:
1013 type method_declarator throws
b9f7e36c 1014 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1015| VOID_TK method_declarator throws
b9f7e36c 1016 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1017| modifiers type method_declarator throws
b9f7e36c 1018 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1019| modifiers VOID_TK method_declarator throws
b9f7e36c 1020 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1021| type error
efa0a23f
APB
1022 {
1023 yyerror ("Invalid method declaration, method name required");
1024 RECOVER;
1025 }
e04a16fb
AG
1026| modifiers type error
1027 {RECOVER;}
1028| VOID_TK error
1029 {yyerror ("Identifier expected"); RECOVER;}
1030| modifiers VOID_TK error
1031 {yyerror ("Identifier expected"); RECOVER;}
1032| modifiers error
1033 {
1034 yyerror ("Invalid method declaration, return type required");
1035 RECOVER;
1036 }
1037;
1038
1039method_declarator:
1040 identifier OP_TK CP_TK
c2952b01
APB
1041 {
1042 ctxp->formal_parameter_number = 0;
1043 $$ = method_declarator ($1, NULL_TREE);
1044 }
e04a16fb
AG
1045| identifier OP_TK formal_parameter_list CP_TK
1046 { $$ = method_declarator ($1, $3); }
1047| method_declarator OSB_TK CSB_TK
1048 {
1886c9d8
APB
1049 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1050 TREE_PURPOSE ($1) =
1051 build_unresolved_array_type (TREE_PURPOSE ($1));
1052 parse_warning_context
1053 (wfl_operator,
1054 "Discouraged form of returned type specification");
e04a16fb
AG
1055 }
1056| identifier OP_TK error
1057 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1058| method_declarator OSB_TK error
1059 {yyerror ("']' expected"); RECOVER;}
1060;
1061
1062formal_parameter_list:
1063 formal_parameter
1064 {
1065 ctxp->formal_parameter_number = 1;
1066 }
1067| formal_parameter_list C_TK formal_parameter
1068 {
1069 ctxp->formal_parameter_number += 1;
1070 $$ = chainon ($1, $3);
1071 }
1072| formal_parameter_list C_TK error
c2952b01 1073 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1074;
1075
1076formal_parameter:
1077 type variable_declarator_id
1078 {
1079 $$ = build_tree_list ($2, $1);
1080 }
18990de5 1081| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1082 {
5256aa37 1083 $$ = build_tree_list ($3, $2);
c2952b01 1084 ARG_FINAL_P ($$) = 1;
5256aa37 1085 }
e04a16fb 1086| type error
f8989a66
APB
1087 {
1088 yyerror ("Missing identifier"); RECOVER;
1089 $$ = NULL_TREE;
1090 }
18990de5 1091| final type error
e04a16fb 1092 {
e04a16fb 1093 yyerror ("Missing identifier"); RECOVER;
f8989a66 1094 $$ = NULL_TREE;
e04a16fb
AG
1095 }
1096;
1097
18990de5
JB
1098final:
1099 modifiers
1100 {
1101 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1102 $1, ACC_FINAL);
1103 if ($1 != ACC_FINAL)
1104 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1105 }
1106;
1107
e04a16fb 1108throws:
b9f7e36c 1109 { $$ = NULL_TREE; }
e04a16fb 1110| THROWS_TK class_type_list
b9f7e36c 1111 { $$ = $2; }
e04a16fb
AG
1112| THROWS_TK error
1113 {yyerror ("Missing class type term"); RECOVER;}
1114;
1115
1116class_type_list:
1117 class_type
c877974e 1118 { $$ = build_tree_list ($1, $1); }
e04a16fb 1119| class_type_list C_TK class_type
c877974e 1120 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1121| class_type_list C_TK error
1122 {yyerror ("Missing class type term"); RECOVER;}
1123;
1124
1125method_body:
1126 block
5f1c312a 1127| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1128;
1129
1130/* 19.8.4 Productions from 8.5: Static Initializers */
1131static_initializer:
1132 static block
1133 {
c2952b01
APB
1134 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1135 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1136 current_static_block = NULL_TREE;
e04a16fb 1137 }
e04a16fb
AG
1138;
1139
1140static: /* Test lval.sub_token here */
c2952b01 1141 modifiers
e04a16fb 1142 {
c2952b01
APB
1143 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1144 /* Can't have a static initializer in an innerclass */
1145 if ($1 | ACC_STATIC &&
1146 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1147 parse_error_context
1148 (MODIFIER_WFL (STATIC_TK),
1149 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1150 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1151 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1152 }
1153;
1154
1155/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1156constructor_declaration:
22eed1e6 1157 constructor_header
e04a16fb 1158 {
22eed1e6
APB
1159 current_function_decl = $1;
1160 source_start_java_method (current_function_decl);
e04a16fb 1161 }
22eed1e6 1162 constructor_body
b635eb2f 1163 { finish_method_declaration ($3); }
22eed1e6
APB
1164;
1165
1166constructor_header:
1167 constructor_declarator throws
1168 { $$ = method_header (0, NULL_TREE, $1, $2); }
1169| modifiers constructor_declarator throws
1170 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1171;
1172
1173constructor_declarator:
1174 simple_name OP_TK CP_TK
c2952b01
APB
1175 {
1176 ctxp->formal_parameter_number = 0;
1177 $$ = method_declarator ($1, NULL_TREE);
1178 }
e04a16fb 1179| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1180 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1181;
1182
1183constructor_body:
22eed1e6
APB
1184 /* Unlike regular method, we always need a complete (empty)
1185 body so we can safely perform all the required code
1186 addition (super invocation and field initialization) */
2e5eb5c5 1187 block_begin constructor_block_end
22eed1e6 1188 {
9bbc7d9f 1189 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1190 $$ = $2;
1191 }
2e5eb5c5 1192| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1193 { $$ = $3; }
2e5eb5c5 1194| block_begin block_statements constructor_block_end
22eed1e6 1195 { $$ = $3; }
2e5eb5c5 1196| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1197 { $$ = $4; }
e04a16fb
AG
1198;
1199
2e5eb5c5
APB
1200constructor_block_end:
1201 block_end
5f1c312a 1202;
2e5eb5c5 1203
e04a16fb
AG
1204/* Error recovery for that rule moved down expression_statement: rule. */
1205explicit_constructor_invocation:
1206 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1207 {
1208 $$ = build_method_invocation ($1, NULL_TREE);
1209 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1210 $$ = java_method_add_stmt (current_function_decl, $$);
1211 }
e04a16fb 1212| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1213 {
1214 $$ = build_method_invocation ($1, $3);
1215 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1216 $$ = java_method_add_stmt (current_function_decl, $$);
1217 }
e04a16fb
AG
1218 /* Added, JDK1.1 inner classes. Modified because the rule
1219 'primary' couldn't work. */
1220| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1221 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1222| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1223 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1224;
1225
1226this_or_super: /* Added, simplifies error diagnostics */
1227 THIS_TK
1228 {
9ee9b555 1229 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1230 EXPR_WFL_LINECOL (wfl) = $1.location;
1231 $$ = wfl;
1232 }
1233| SUPER_TK
1234 {
9ee9b555 1235 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1236 EXPR_WFL_LINECOL (wfl) = $1.location;
1237 $$ = wfl;
1238 }
1239;
1240
1241/* 19.9 Productions from 9: Interfaces */
1242/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1243interface_declaration:
1244 INTERFACE_TK identifier
1245 { create_interface (0, $2, NULL_TREE); }
1246 interface_body
e04a16fb
AG
1247| modifiers INTERFACE_TK identifier
1248 { create_interface ($1, $3, NULL_TREE); }
1249 interface_body
e04a16fb
AG
1250| INTERFACE_TK identifier extends_interfaces
1251 { create_interface (0, $2, $3); }
1252 interface_body
e04a16fb
AG
1253| modifiers INTERFACE_TK identifier extends_interfaces
1254 { create_interface ($1, $3, $4); }
1255 interface_body
e04a16fb 1256| INTERFACE_TK identifier error
0b4d333e 1257 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1258| modifiers INTERFACE_TK identifier error
0b4d333e 1259 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1260;
1261
1262extends_interfaces:
1263 EXTENDS_TK interface_type
1264 {
1265 ctxp->interface_number = 1;
1266 $$ = build_tree_list ($2, NULL_TREE);
1267 }
1268| extends_interfaces C_TK interface_type
1269 {
1270 ctxp->interface_number++;
1271 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1272 }
1273| EXTENDS_TK error
1274 {yyerror ("Invalid interface type"); RECOVER;}
1275| extends_interfaces C_TK error
1276 {yyerror ("Missing term"); RECOVER;}
1277;
1278
1279interface_body:
1280 OCB_TK CCB_TK
1281 { $$ = NULL_TREE; }
1282| OCB_TK interface_member_declarations CCB_TK
1283 { $$ = NULL_TREE; }
1284;
1285
1286interface_member_declarations:
1287 interface_member_declaration
1288| interface_member_declarations interface_member_declaration
1289;
1290
1291interface_member_declaration:
1292 constant_declaration
1293| abstract_method_declaration
1294| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1295 { end_class_declaration (1); }
1296| interface_declaration /* Added, JDK1.1 inner interfaces */
1297 { end_class_declaration (1); }
e04a16fb
AG
1298;
1299
1300constant_declaration:
1301 field_declaration
1302;
1303
1304abstract_method_declaration:
1305 method_header SC_TK
1306 {
1307 check_abstract_method_header ($1);
1308 current_function_decl = NULL_TREE; /* FIXME ? */
1309 }
1310| method_header error
1311 {yyerror ("';' expected"); RECOVER;}
1312;
1313
1314/* 19.10 Productions from 10: Arrays */
1315array_initializer:
1316 OCB_TK CCB_TK
1179ebc2 1317 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1318| OCB_TK variable_initializers CCB_TK
f8976021 1319 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1320| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1321 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1322;
1323
1324variable_initializers:
1325 variable_initializer
f8976021
APB
1326 {
1327 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1328 $1, NULL_TREE);
1329 }
e04a16fb 1330| variable_initializers C_TK variable_initializer
1179ebc2
APB
1331 {
1332 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1333 }
e04a16fb
AG
1334| variable_initializers C_TK error
1335 {yyerror ("Missing term"); RECOVER;}
1336;
1337
1338/* 19.11 Production from 14: Blocks and Statements */
1339block:
1340 OCB_TK CCB_TK
7f10c2e2
APB
1341 {
1342 /* Store the location of the `}' when doing xrefs */
1343 if (current_function_decl && flag_emit_xref)
1344 DECL_END_SOURCE_LINE (current_function_decl) =
1345 EXPR_WFL_ADD_COL ($2.location, 1);
1346 $$ = empty_stmt_node;
1347 }
22eed1e6
APB
1348| block_begin block_statements block_end
1349 { $$ = $3; }
1350;
1351
1352block_begin:
1353 OCB_TK
e04a16fb 1354 { enter_block (); }
22eed1e6
APB
1355;
1356
1357block_end:
e04a16fb
AG
1358 CCB_TK
1359 {
1360 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1361 /* Store the location of the `}' when doing xrefs */
1362 if (current_function_decl && flag_emit_xref)
1363 DECL_END_SOURCE_LINE (current_function_decl) =
1364 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1365 $$ = exit_block ();
c280e37a
APB
1366 if (!BLOCK_SUBBLOCKS ($$))
1367 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1368 }
1369;
1370
1371block_statements:
1372 block_statement
1373| block_statements block_statement
1374;
1375
1376block_statement:
1377 local_variable_declaration_statement
1378| statement
15fdcfe9 1379 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1380| class_declaration /* Added, JDK1.1 local classes */
1381 {
1382 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1383 end_class_declaration (1);
1384 }
e04a16fb
AG
1385;
1386
1387local_variable_declaration_statement:
1388 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1389;
1390
1391local_variable_declaration:
1392 type variable_declarators
1393 { declare_local_variables (0, $1, $2); }
a003f638 1394| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1395 { declare_local_variables ($1, $2, $3); }
1396;
1397
1398statement:
1399 statement_without_trailing_substatement
1400| labeled_statement
e04a16fb 1401| if_then_statement
e04a16fb 1402| if_then_else_statement
e04a16fb 1403| while_statement
e04a16fb 1404| for_statement
cd9643f7 1405 { $$ = exit_block (); }
e04a16fb
AG
1406;
1407
1408statement_nsi:
1409 statement_without_trailing_substatement
1410| labeled_statement_nsi
e04a16fb 1411| if_then_else_statement_nsi
e04a16fb 1412| while_statement_nsi
e04a16fb 1413| for_statement_nsi
9dd939b2 1414 { $$ = exit_block (); }
e04a16fb
AG
1415;
1416
1417statement_without_trailing_substatement:
1418 block
e04a16fb 1419| empty_statement
e04a16fb 1420| expression_statement
e04a16fb 1421| switch_statement
e04a16fb 1422| do_statement
e04a16fb 1423| break_statement
e04a16fb 1424| continue_statement
e04a16fb
AG
1425| return_statement
1426| synchronized_statement
e04a16fb 1427| throw_statement
e04a16fb 1428| try_statement
e04a16fb
AG
1429;
1430
1431empty_statement:
1432 SC_TK
5f1c312a
APB
1433 {
1434 if (flag_extraneous_semicolon)
1435 {
1436 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1437 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1438 }
1439 $$ = empty_stmt_node;
1440 }
e04a16fb
AG
1441;
1442
1443label_decl:
1444 identifier REL_CL_TK
1445 {
1446 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1447 EXPR_WFL_NODE ($1));
e04a16fb
AG
1448 pushlevel (2);
1449 push_labeled_block ($$);
1450 PUSH_LABELED_BLOCK ($$);
1451 }
1452;
1453
1454labeled_statement:
1455 label_decl statement
b635eb2f 1456 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1457| identifier error
1458 {yyerror ("':' expected"); RECOVER;}
1459;
1460
1461labeled_statement_nsi:
1462 label_decl statement_nsi
b635eb2f 1463 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1464;
1465
1466/* We concentrate here a bunch of error handling rules that we couldn't write
1467 earlier, because expression_statement catches a missing ';'. */
1468expression_statement:
1469 statement_expression SC_TK
1470 {
1471 /* We have a statement. Generate a WFL around it so
1472 we can debug it */
1473 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1474 /* We know we have a statement, so set the debug
1475 info to be eventually generate here. */
1476 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1477 }
1478| error SC_TK
1479 {
29f8b718 1480 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1481 DRECOVER (expr_stmt);
1482 }
1483| error OCB_TK
1484 {
29f8b718 1485 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1486 DRECOVER (expr_stmt);
1487 }
1488| error CCB_TK
1489 {
29f8b718 1490 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1491 DRECOVER (expr_stmt);
1492 }
1493| this_or_super OP_TK error
1494 {yyerror ("')' expected"); RECOVER;}
1495| this_or_super OP_TK CP_TK error
22eed1e6 1496 {
8119c720 1497 parse_ctor_invocation_error ();
22eed1e6
APB
1498 RECOVER;
1499 }
e04a16fb
AG
1500| this_or_super OP_TK argument_list error
1501 {yyerror ("')' expected"); RECOVER;}
1502| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1503 {
8119c720 1504 parse_ctor_invocation_error ();
22eed1e6
APB
1505 RECOVER;
1506 }
e04a16fb
AG
1507| name DOT_TK SUPER_TK error
1508 {yyerror ("'(' expected"); RECOVER;}
1509| name DOT_TK SUPER_TK OP_TK error
1510 {yyerror ("')' expected"); RECOVER;}
1511| name DOT_TK SUPER_TK OP_TK argument_list error
1512 {yyerror ("')' expected"); RECOVER;}
1513| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1514 {yyerror ("';' expected"); RECOVER;}
1515| name DOT_TK SUPER_TK OP_TK CP_TK error
1516 {yyerror ("';' expected"); RECOVER;}
1517;
1518
1519statement_expression:
1520 assignment
1521| pre_increment_expression
e04a16fb 1522| pre_decrement_expression
e04a16fb 1523| post_increment_expression
e04a16fb 1524| post_decrement_expression
e04a16fb
AG
1525| method_invocation
1526| class_instance_creation_expression
e04a16fb
AG
1527;
1528
1529if_then_statement:
1530 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1531 {
1532 $$ = build_if_else_statement ($2.location, $3,
1533 $5, NULL_TREE);
1534 }
e04a16fb
AG
1535| IF_TK error
1536 {yyerror ("'(' expected"); RECOVER;}
1537| IF_TK OP_TK error
1538 {yyerror ("Missing term"); RECOVER;}
1539| IF_TK OP_TK expression error
1540 {yyerror ("')' expected"); RECOVER;}
1541;
1542
1543if_then_else_statement:
1544 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1545 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1546;
1547
1548if_then_else_statement_nsi:
1549 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1550 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1551;
1552
1553switch_statement:
15fdcfe9
PB
1554 switch_expression
1555 {
1556 enter_block ();
1557 }
1558 switch_block
b67d701b 1559 {
15fdcfe9 1560 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1561 I.e. make the last statment also have its own
1562 COMPOUND_EXPR. */
15fdcfe9
PB
1563 maybe_absorb_scoping_blocks ();
1564 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1565 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1566 }
1567;
1568
1569switch_expression:
1570 SWITCH_TK OP_TK expression CP_TK
1571 {
1572 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1573 EXPR_WFL_LINECOL ($$) = $2.location;
1574 }
e04a16fb
AG
1575| SWITCH_TK error
1576 {yyerror ("'(' expected"); RECOVER;}
1577| SWITCH_TK OP_TK error
1578 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1579| SWITCH_TK OP_TK expression CP_TK error
1580 {yyerror ("'{' expected"); RECOVER;}
1581;
1582
f8976021
APB
1583/* Default assignment is there to avoid type node on switch_block
1584 node. */
1585
e04a16fb
AG
1586switch_block:
1587 OCB_TK CCB_TK
f8976021 1588 { $$ = NULL_TREE; }
e04a16fb 1589| OCB_TK switch_labels CCB_TK
f8976021 1590 { $$ = NULL_TREE; }
e04a16fb 1591| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1592 { $$ = NULL_TREE; }
e04a16fb 1593| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1594 { $$ = NULL_TREE; }
e04a16fb
AG
1595;
1596
1597switch_block_statement_groups:
1598 switch_block_statement_group
1599| switch_block_statement_groups switch_block_statement_group
1600;
1601
1602switch_block_statement_group:
15fdcfe9 1603 switch_labels block_statements
e04a16fb
AG
1604;
1605
e04a16fb
AG
1606switch_labels:
1607 switch_label
1608| switch_labels switch_label
1609;
1610
1611switch_label:
1612 CASE_TK constant_expression REL_CL_TK
b67d701b 1613 {
15fdcfe9
PB
1614 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1615 EXPR_WFL_LINECOL (lab) = $1.location;
1616 java_method_add_stmt (current_function_decl, lab);
b67d701b 1617 }
e04a16fb 1618| DEFAULT_TK REL_CL_TK
b67d701b 1619 {
15fdcfe9
PB
1620 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1621 EXPR_WFL_LINECOL (lab) = $1.location;
1622 java_method_add_stmt (current_function_decl, lab);
b67d701b 1623 }
e04a16fb
AG
1624| CASE_TK error
1625 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1626| CASE_TK constant_expression error
1627 {yyerror ("':' expected"); RECOVER;}
1628| DEFAULT_TK error
1629 {yyerror ("':' expected"); RECOVER;}
1630;
1631
1632while_expression:
1633 WHILE_TK OP_TK expression CP_TK
1634 {
1635 tree body = build_loop_body ($2.location, $3, 0);
1636 $$ = build_new_loop (body);
1637 }
1638;
1639
1640while_statement:
1641 while_expression statement
b635eb2f 1642 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1643| WHILE_TK error
1644 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1645| WHILE_TK OP_TK error
1646 {yyerror ("Missing term and ')' expected"); RECOVER;}
1647| WHILE_TK OP_TK expression error
1648 {yyerror ("')' expected"); RECOVER;}
1649;
1650
1651while_statement_nsi:
1652 while_expression statement_nsi
b635eb2f 1653 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1654;
1655
1656do_statement_begin:
1657 DO_TK
1658 {
1659 tree body = build_loop_body (0, NULL_TREE, 1);
1660 $$ = build_new_loop (body);
1661 }
1662 /* Need error handing here. FIXME */
1663;
1664
1665do_statement:
1666 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1667 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1668;
1669
1670for_statement:
1671 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1672 {
1673 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1674 $3 = build_wfl_node ($3);
1675 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1676 }
e04a16fb
AG
1677| for_begin SC_TK SC_TK for_update CP_TK statement
1678 {
b635eb2f 1679 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1680 /* We have not condition, so we get rid of the EXIT_EXPR */
1681 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1682 empty_stmt_node;
e04a16fb
AG
1683 }
1684| for_begin SC_TK error
1685 {yyerror ("Invalid control expression"); RECOVER;}
1686| for_begin SC_TK expression SC_TK error
1687 {yyerror ("Invalid update expression"); RECOVER;}
1688| for_begin SC_TK SC_TK error
1689 {yyerror ("Invalid update expression"); RECOVER;}
1690;
1691
1692for_statement_nsi:
1693 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1694 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1695| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1696 {
b635eb2f 1697 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1698 /* We have not condition, so we get rid of the EXIT_EXPR */
1699 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1700 empty_stmt_node;
e04a16fb
AG
1701 }
1702;
1703
1704for_header:
1705 FOR_TK OP_TK
1706 {
1707 /* This scope defined for local variable that may be
1708 defined within the scope of the for loop */
1709 enter_block ();
1710 }
1711| FOR_TK error
1712 {yyerror ("'(' expected"); DRECOVER(for_1);}
1713| FOR_TK OP_TK error
1714 {yyerror ("Invalid init statement"); RECOVER;}
1715;
1716
1717for_begin:
1718 for_header for_init
1719 {
1720 /* We now declare the loop body. The loop is
1721 declared as a for loop. */
1722 tree body = build_loop_body (0, NULL_TREE, 0);
1723 $$ = build_new_loop (body);
c2952b01 1724 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1725 /* The loop is added to the current block the for
1726 statement is defined within */
1727 java_method_add_stmt (current_function_decl, $$);
1728 }
1729;
1730for_init: /* Can be empty */
9bbc7d9f 1731 { $$ = empty_stmt_node; }
e04a16fb
AG
1732| statement_expression_list
1733 {
1734 /* Init statement recorded within the previously
1735 defined block scope */
1736 $$ = java_method_add_stmt (current_function_decl, $1);
1737 }
1738| local_variable_declaration
1739 {
1740 /* Local variable are recorded within the previously
1741 defined block scope */
1742 $$ = NULL_TREE;
1743 }
1744| statement_expression_list error
1745 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1746;
1747
1748for_update: /* Can be empty */
9bbc7d9f 1749 {$$ = empty_stmt_node;}
e04a16fb
AG
1750| statement_expression_list
1751 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1752;
1753
1754statement_expression_list:
1755 statement_expression
1756 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1757| statement_expression_list C_TK statement_expression
1758 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1759| statement_expression_list C_TK error
1760 {yyerror ("Missing term"); RECOVER;}
1761;
1762
1763break_statement:
1764 BREAK_TK SC_TK
1765 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1766| BREAK_TK identifier SC_TK
1767 { $$ = build_bc_statement ($1.location, 1, $2); }
1768| BREAK_TK error
1769 {yyerror ("Missing term"); RECOVER;}
1770| BREAK_TK identifier error
1771 {yyerror ("';' expected"); RECOVER;}
1772;
1773
1774continue_statement:
1775 CONTINUE_TK SC_TK
1776 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1777| CONTINUE_TK identifier SC_TK
1778 { $$ = build_bc_statement ($1.location, 0, $2); }
1779| CONTINUE_TK error
1780 {yyerror ("Missing term"); RECOVER;}
1781| CONTINUE_TK identifier error
1782 {yyerror ("';' expected"); RECOVER;}
1783;
1784
1785return_statement:
1786 RETURN_TK SC_TK
1787 { $$ = build_return ($1.location, NULL_TREE); }
1788| RETURN_TK expression SC_TK
1789 { $$ = build_return ($1.location, $2); }
1790| RETURN_TK error
1791 {yyerror ("Missing term"); RECOVER;}
1792| RETURN_TK expression error
1793 {yyerror ("';' expected"); RECOVER;}
1794;
1795
1796throw_statement:
1797 THROW_TK expression SC_TK
b9f7e36c
APB
1798 {
1799 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1800 EXPR_WFL_LINECOL ($$) = $1.location;
1801 }
e04a16fb
AG
1802| THROW_TK error
1803 {yyerror ("Missing term"); RECOVER;}
1804| THROW_TK expression error
1805 {yyerror ("';' expected"); RECOVER;}
1806;
1807
1808synchronized_statement:
1809 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1810 {
1811 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1812 EXPR_WFL_LINECOL ($$) =
1813 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1814 }
e04a16fb
AG
1815| synchronized OP_TK expression CP_TK error
1816 {yyerror ("'{' expected"); RECOVER;}
1817| synchronized error
1818 {yyerror ("'(' expected"); RECOVER;}
1819| synchronized OP_TK error CP_TK
1820 {yyerror ("Missing term"); RECOVER;}
1821| synchronized OP_TK error
1822 {yyerror ("Missing term"); RECOVER;}
1823;
1824
b9f7e36c 1825synchronized:
efa0a23f 1826 modifiers
e04a16fb 1827 {
781b0558
KG
1828 check_modifiers (
1829 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1830 $1, ACC_SYNCHRONIZED);
1831 if ($1 != ACC_SYNCHRONIZED)
1832 MODIFIER_WFL (SYNCHRONIZED_TK) =
1833 build_wfl_node (NULL_TREE);
e04a16fb
AG
1834 }
1835;
1836
1837try_statement:
1838 TRY_TK block catches
a7d8d81f 1839 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1840| TRY_TK block finally
a7d8d81f 1841 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1842| TRY_TK block catches finally
2aa11e97
APB
1843 { $$ = build_try_finally_statement
1844 ($1.location, build_try_statement ($1.location,
1845 $2, $3), $4);
1846 }
e04a16fb
AG
1847| TRY_TK error
1848 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1849;
1850
1851catches:
1852 catch_clause
1853| catches catch_clause
b67d701b
PB
1854 {
1855 TREE_CHAIN ($2) = $1;
1856 $$ = $2;
1857 }
e04a16fb
AG
1858;
1859
1860catch_clause:
b67d701b
PB
1861 catch_clause_parameter block
1862 {
1863 java_method_add_stmt (current_function_decl, $2);
1864 exit_block ();
1865 $$ = $1;
1866 }
1867
1868catch_clause_parameter:
1869 CATCH_TK OP_TK formal_parameter CP_TK
1870 {
1871 /* We add a block to define a scope for
1872 formal_parameter (CCBP). The formal parameter is
1873 declared initialized by the appropriate function
1874 call */
1875 tree ccpb = enter_block ();
1876 tree init = build_assignment (ASSIGN_TK, $2.location,
1877 TREE_PURPOSE ($3),
1878 soft_exceptioninfo_call_node);
1879 declare_local_variables (0, TREE_VALUE ($3),
1880 build_tree_list (TREE_PURPOSE ($3),
1881 init));
1882 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1883 EXPR_WFL_LINECOL ($$) = $1.location;
1884 }
e04a16fb 1885| CATCH_TK error
97f30284 1886 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1887| CATCH_TK OP_TK error
97f30284
APB
1888 {
1889 yyerror ("Missing term or ')' expected");
1890 RECOVER; $$ = NULL_TREE;
1891 }
b67d701b 1892| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1893 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1894;
1895
1896finally:
1897 FINALLY_TK block
a7d8d81f 1898 { $$ = $2; }
e04a16fb
AG
1899| FINALLY_TK error
1900 {yyerror ("'{' expected"); RECOVER; }
1901;
1902
1903/* 19.12 Production from 15: Expressions */
1904primary:
1905 primary_no_new_array
1906| array_creation_expression
1907;
1908
1909primary_no_new_array:
1910 literal
1911| THIS_TK
1912 { $$ = build_this ($1.location); }
1913| OP_TK expression CP_TK
1914 {$$ = $2;}
1915| class_instance_creation_expression
1916| field_access
1917| method_invocation
1918| array_access
c2952b01 1919| type_literals
e04a16fb
AG
1920 /* Added, JDK1.1 inner classes. Documentation is wrong
1921 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1922 exist. Used name: instead. */
e04a16fb 1923| name DOT_TK THIS_TK
c2952b01
APB
1924 {
1925 tree wfl = build_wfl_node (this_identifier_node);
1926 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1927 }
e04a16fb
AG
1928| OP_TK expression error
1929 {yyerror ("')' expected"); RECOVER;}
1930| name DOT_TK error
1931 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1932| primitive_type DOT_TK error
1933 {yyerror ("'class' expected" ); RECOVER;}
1934| VOID_TK DOT_TK error
1935 {yyerror ("'class' expected" ); RECOVER;}
1936;
1937
c2952b01
APB
1938/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1939 broke the rule down a bit. */
1940
1941array_type_literal:
1942 primitive_type OSB_TK CSB_TK
1943 {
1944 $$ = build_java_array_type ($1, -1);
1945 CLASS_LOADED_P ($$) = 1;
1946 }
1947| name OSB_TK CSB_TK
1948 { $$ = build_unresolved_array_type ($1); }
1949/* This triggers two reduce/reduce conflict between array_type_literal and
1950 dims. FIXME.
1951| array_type OSB_TK CSB_TK
1952 { $$ = build_unresolved_array_type ($1); }
1953*/
1954;
1955
1956type_literals:
1957 name DOT_TK CLASS_TK
1958 { $$ = build_incomplete_class_ref ($2.location, $1); }
1959| array_type_literal DOT_TK CLASS_TK
1960 { $$ = build_incomplete_class_ref ($2.location, $1); }
1961| primitive_type DOT_TK CLASS_TK
1962 { $$ = build_class_ref ($1); }
1963| VOID_TK DOT_TK CLASS_TK
1964 { $$ = build_class_ref (void_type_node); }
1965;
1966
e04a16fb
AG
1967class_instance_creation_expression:
1968 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1969 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1970| NEW_TK class_type OP_TK CP_TK
b67d701b 1971 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1972| anonymous_class_creation
e04a16fb
AG
1973 /* Added, JDK1.1 inner classes, modified to use name or
1974 primary instead of primary solely which couldn't work in
1975 all situations. */
1976| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1977 {
1978 tree ctor = build_new_invocation ($2, NULL_TREE);
1979 $$ = make_qualified_primary ($1, ctor,
1980 EXPR_WFL_LINECOL ($1));
1981 }
e04a16fb
AG
1982| something_dot_new identifier OP_TK CP_TK class_body
1983| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1984 {
1985 tree ctor = build_new_invocation ($2, $4);
1986 $$ = make_qualified_primary ($1, ctor,
1987 EXPR_WFL_LINECOL ($1));
1988 }
e04a16fb
AG
1989| something_dot_new identifier OP_TK argument_list CP_TK class_body
1990| NEW_TK error SC_TK
1991 {yyerror ("'(' expected"); DRECOVER(new_1);}
1992| NEW_TK class_type error
1993 {yyerror ("'(' expected"); RECOVER;}
1994| NEW_TK class_type OP_TK error
1995 {yyerror ("')' or term expected"); RECOVER;}
1996| NEW_TK class_type OP_TK argument_list error
1997 {yyerror ("')' expected"); RECOVER;}
1998| something_dot_new error
1999 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2000| something_dot_new identifier error
2001 {yyerror ("'(' expected"); RECOVER;}
2002;
2003
c2952b01
APB
2004/* Created after JDK1.1 rules originally added to
2005 class_instance_creation_expression, but modified to use
2006 'class_type' instead of 'TypeName' (type_name) which is mentionned
2007 in the documentation but doesn't exist. */
2008
2009anonymous_class_creation:
2010 NEW_TK class_type OP_TK argument_list CP_TK
2011 { create_anonymous_class ($1.location, $2); }
2012 class_body
2013 {
2014 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2015 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2016
2017 end_class_declaration (1);
2018
2019 /* Now we can craft the new expression */
2020 $$ = build_new_invocation (id, $4);
2021
2022 /* Note that we can't possibly be here if
2023 `class_type' is an interface (in which case the
2024 anonymous class extends Object and implements
2025 `class_type', hence its constructor can't have
2026 arguments.) */
2027
2028 /* Otherwise, the innerclass must feature a
2029 constructor matching `argument_list'. Anonymous
2030 classes are a bit special: it's impossible to
2031 define constructor for them, hence constructors
2032 must be generated following the hints provided by
2033 the `new' expression. Whether a super constructor
2034 of that nature exists or not is to be verified
2035 later on in verify_constructor_super.
2036
2037 It's during the expansion of a `new' statement
2038 refering to an anonymous class that a ctor will
2039 be generated for the anonymous class, with the
2040 right arguments. */
2041
2042 }
2043| NEW_TK class_type OP_TK CP_TK
2044 { create_anonymous_class ($1.location, $2); }
2045 class_body
2046 {
2047 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2048 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2049
2050 end_class_declaration (1);
2051
2052 /* Now we can craft the new expression. The
2053 statement doesn't need to be remember so that a
2054 constructor can be generated, since its signature
2055 is already known. */
2056 $$ = build_new_invocation (id, NULL_TREE);
2057 }
2058;
2059
e04a16fb
AG
2060something_dot_new: /* Added, not part of the specs. */
2061 name DOT_TK NEW_TK
c2952b01 2062 { $$ = $1; }
e04a16fb 2063| primary DOT_TK NEW_TK
c2952b01 2064 { $$ = $1; }
e04a16fb
AG
2065;
2066
2067argument_list:
2068 expression
2069 {
2070 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2071 ctxp->formal_parameter_number = 1;
2072 }
2073| argument_list C_TK expression
2074 {
2075 ctxp->formal_parameter_number += 1;
2076 $$ = tree_cons (NULL_TREE, $3, $1);
2077 }
2078| argument_list C_TK error
2079 {yyerror ("Missing term"); RECOVER;}
2080;
2081
2082array_creation_expression:
2083 NEW_TK primitive_type dim_exprs
2084 { $$ = build_newarray_node ($2, $3, 0); }
2085| NEW_TK class_or_interface_type dim_exprs
2086 { $$ = build_newarray_node ($2, $3, 0); }
2087| NEW_TK primitive_type dim_exprs dims
ba179f9f 2088 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2089| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2090 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2091 /* Added, JDK1.1 anonymous array. Initial documentation rule
2092 modified */
2093| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2094 {
2095 char *sig;
2096 while (CURRENT_OSB (ctxp)--)
2097 obstack_1grow (&temporary_obstack, '[');
2098 sig = obstack_finish (&temporary_obstack);
2099 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2100 $2, get_identifier (sig), $4);
2101 }
e04a16fb 2102| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2103 {
2104 tree type = $2;
2105 while (CURRENT_OSB (ctxp)--)
2106 type = build_java_array_type (type, -1);
2107 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2108 build_pointer_type (type), NULL_TREE, $4);
2109 }
e04a16fb
AG
2110| NEW_TK error CSB_TK
2111 {yyerror ("'[' expected"); DRECOVER ("]");}
2112| NEW_TK error OSB_TK
2113 {yyerror ("']' expected"); RECOVER;}
2114;
2115
2116dim_exprs:
2117 dim_expr
2118 { $$ = build_tree_list (NULL_TREE, $1); }
2119| dim_exprs dim_expr
2120 { $$ = tree_cons (NULL_TREE, $2, $$); }
2121;
2122
2123dim_expr:
2124 OSB_TK expression CSB_TK
2125 {
9a7ab4b3
APB
2126 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2127 {
2128 $2 = build_wfl_node ($2);
2129 TREE_TYPE ($2) = NULL_TREE;
2130 }
e04a16fb
AG
2131 EXPR_WFL_LINECOL ($2) = $1.location;
2132 $$ = $2;
2133 }
2134| OSB_TK expression error
2135 {yyerror ("']' expected"); RECOVER;}
2136| OSB_TK error
2137 {
2138 yyerror ("Missing term");
2139 yyerror ("']' expected");
2140 RECOVER;
2141 }
2142;
2143
2144dims:
2145 OSB_TK CSB_TK
ba179f9f
APB
2146 {
2147 int allocate = 0;
2148 /* If not initialized, allocate memory for the osb
2149 numbers stack */
2150 if (!ctxp->osb_limit)
2151 {
2152 allocate = ctxp->osb_limit = 32;
2153 ctxp->osb_depth = -1;
2154 }
c2952b01 2155 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2156 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2157 allocate = ctxp->osb_limit << 1;
2158
2159 if (allocate)
2160 {
2161 allocate *= sizeof (int);
2162 if (ctxp->osb_number)
2163 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2164 allocate);
2165 else
2166 ctxp->osb_number = (int *)xmalloc (allocate);
2167 }
2168 ctxp->osb_depth++;
2169 CURRENT_OSB (ctxp) = 1;
2170 }
e04a16fb 2171| dims OSB_TK CSB_TK
ba179f9f 2172 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2173| dims OSB_TK error
2174 { yyerror ("']' expected"); RECOVER;}
2175;
2176
2177field_access:
2178 primary DOT_TK identifier
2179 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2180 /* FIXME - REWRITE TO:
2181 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2182| SUPER_TK DOT_TK identifier
2183 {
6e22695a 2184 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2185 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2186 $$ = make_qualified_name (super_wfl, $3, $2.location);
2187 }
2188| SUPER_TK error
2189 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2190;
2191
2192method_invocation:
2193 name OP_TK CP_TK
2194 { $$ = build_method_invocation ($1, NULL_TREE); }
2195| name OP_TK argument_list CP_TK
2196 { $$ = build_method_invocation ($1, $3); }
2197| primary DOT_TK identifier OP_TK CP_TK
2198 {
22eed1e6
APB
2199 if (TREE_CODE ($1) == THIS_EXPR)
2200 $$ = build_this_super_qualified_invocation
2201 (1, $3, NULL_TREE, 0, $2.location);
2202 else
2203 {
2204 tree invok = build_method_invocation ($3, NULL_TREE);
2205 $$ = make_qualified_primary ($1, invok, $2.location);
2206 }
e04a16fb
AG
2207 }
2208| primary DOT_TK identifier OP_TK argument_list CP_TK
2209 {
22eed1e6
APB
2210 if (TREE_CODE ($1) == THIS_EXPR)
2211 $$ = build_this_super_qualified_invocation
2212 (1, $3, $5, 0, $2.location);
2213 else
2214 {
2215 tree invok = build_method_invocation ($3, $5);
2216 $$ = make_qualified_primary ($1, invok, $2.location);
2217 }
e04a16fb
AG
2218 }
2219| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2220 {
2221 $$ = build_this_super_qualified_invocation
2222 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2223 }
2224| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2225 {
22eed1e6
APB
2226 $$ = build_this_super_qualified_invocation
2227 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2228 }
2229 /* Screws up thing. I let it here until I'm convinced it can
2230 be removed. FIXME
2231| primary DOT_TK error
2232 {yyerror ("'(' expected"); DRECOVER(bad);} */
2233| SUPER_TK DOT_TK error CP_TK
2234 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2235| SUPER_TK DOT_TK error DOT_TK
2236 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2237;
2238
2239array_access:
2240 name OSB_TK expression CSB_TK
2241 { $$ = build_array_ref ($2.location, $1, $3); }
2242| primary_no_new_array OSB_TK expression CSB_TK
2243 { $$ = build_array_ref ($2.location, $1, $3); }
2244| name OSB_TK error
2245 {
2246 yyerror ("Missing term and ']' expected");
2247 DRECOVER(array_access);
2248 }
2249| name OSB_TK expression error
2250 {
2251 yyerror ("']' expected");
2252 DRECOVER(array_access);
2253 }
2254| primary_no_new_array OSB_TK error
2255 {
2256 yyerror ("Missing term and ']' expected");
2257 DRECOVER(array_access);
2258 }
2259| primary_no_new_array OSB_TK expression error
2260 {
2261 yyerror ("']' expected");
2262 DRECOVER(array_access);
2263 }
2264;
2265
2266postfix_expression:
2267 primary
2268| name
2269| post_increment_expression
2270| post_decrement_expression
2271;
2272
2273post_increment_expression:
2274 postfix_expression INCR_TK
2275 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2276;
2277
2278post_decrement_expression:
2279 postfix_expression DECR_TK
2280 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2281;
2282
2283unary_expression:
2284 pre_increment_expression
2285| pre_decrement_expression
2286| PLUS_TK unary_expression
2287 {$$ = build_unaryop ($1.token, $1.location, $2); }
2288| MINUS_TK unary_expression
2289 {$$ = build_unaryop ($1.token, $1.location, $2); }
2290| unary_expression_not_plus_minus
2291| PLUS_TK error
2292 {yyerror ("Missing term"); RECOVER}
2293| MINUS_TK error
2294 {yyerror ("Missing term"); RECOVER}
2295;
2296
2297pre_increment_expression:
2298 INCR_TK unary_expression
2299 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2300| INCR_TK error
2301 {yyerror ("Missing term"); RECOVER}
2302;
2303
2304pre_decrement_expression:
2305 DECR_TK unary_expression
2306 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2307| DECR_TK error
2308 {yyerror ("Missing term"); RECOVER}
2309;
2310
2311unary_expression_not_plus_minus:
2312 postfix_expression
2313| NOT_TK unary_expression
2314 {$$ = build_unaryop ($1.token, $1.location, $2); }
2315| NEG_TK unary_expression
2316 {$$ = build_unaryop ($1.token, $1.location, $2); }
2317| cast_expression
2318| NOT_TK error
2319 {yyerror ("Missing term"); RECOVER}
2320| NEG_TK error
2321 {yyerror ("Missing term"); RECOVER}
2322;
2323
2324cast_expression: /* Error handling here is potentially weak */
2325 OP_TK primitive_type dims CP_TK unary_expression
2326 {
2327 tree type = $2;
ba179f9f 2328 while (CURRENT_OSB (ctxp)--)
e04a16fb 2329 type = build_java_array_type (type, -1);
ba179f9f 2330 ctxp->osb_depth--;
e04a16fb
AG
2331 $$ = build_cast ($1.location, type, $5);
2332 }
2333| OP_TK primitive_type CP_TK unary_expression
2334 { $$ = build_cast ($1.location, $2, $4); }
2335| OP_TK expression CP_TK unary_expression_not_plus_minus
2336 { $$ = build_cast ($1.location, $2, $4); }
2337| OP_TK name dims CP_TK unary_expression_not_plus_minus
2338 {
49f48c71 2339 const char *ptr;
ba179f9f 2340 while (CURRENT_OSB (ctxp)--)
e04a16fb 2341 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2342 ctxp->osb_depth--;
e04a16fb
AG
2343 obstack_grow0 (&temporary_obstack,
2344 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2345 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2346 ptr = obstack_finish (&temporary_obstack);
2347 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2348 $$ = build_cast ($1.location, $2, $5);
2349 }
2350| OP_TK primitive_type OSB_TK error
2351 {yyerror ("']' expected, invalid type expression");}
2352| OP_TK error
2353 {
29f8b718 2354 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2355 RECOVER;
2356 }
2357| OP_TK primitive_type dims CP_TK error
2358 {yyerror ("Missing term"); RECOVER;}
2359| OP_TK primitive_type CP_TK error
2360 {yyerror ("Missing term"); RECOVER;}
2361| OP_TK name dims CP_TK error
2362 {yyerror ("Missing term"); RECOVER;}
2363;
2364
2365multiplicative_expression:
2366 unary_expression
2367| multiplicative_expression MULT_TK unary_expression
2368 {
2369 $$ = build_binop (BINOP_LOOKUP ($2.token),
2370 $2.location, $1, $3);
2371 }
2372| multiplicative_expression DIV_TK unary_expression
2373 {
2374 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2375 $1, $3);
2376 }
2377| multiplicative_expression REM_TK unary_expression
2378 {
2379 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2380 $1, $3);
2381 }
2382| multiplicative_expression MULT_TK error
2383 {yyerror ("Missing term"); RECOVER;}
2384| multiplicative_expression DIV_TK error
2385 {yyerror ("Missing term"); RECOVER;}
2386| multiplicative_expression REM_TK error
2387 {yyerror ("Missing term"); RECOVER;}
2388;
2389
2390additive_expression:
2391 multiplicative_expression
2392| additive_expression PLUS_TK multiplicative_expression
2393 {
2394 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2395 $1, $3);
2396 }
2397| additive_expression MINUS_TK multiplicative_expression
2398 {
2399 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2400 $1, $3);
2401 }
2402| additive_expression PLUS_TK error
2403 {yyerror ("Missing term"); RECOVER;}
2404| additive_expression MINUS_TK error
2405 {yyerror ("Missing term"); RECOVER;}
2406;
2407
2408shift_expression:
2409 additive_expression
2410| shift_expression LS_TK additive_expression
2411 {
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2414 }
2415| shift_expression SRS_TK additive_expression
2416 {
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2419 }
2420| shift_expression ZRS_TK additive_expression
2421 {
2422 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2423 $1, $3);
2424 }
2425| shift_expression LS_TK error
2426 {yyerror ("Missing term"); RECOVER;}
2427| shift_expression SRS_TK error
2428 {yyerror ("Missing term"); RECOVER;}
2429| shift_expression ZRS_TK error
2430 {yyerror ("Missing term"); RECOVER;}
2431;
2432
2433relational_expression:
2434 shift_expression
2435| relational_expression LT_TK shift_expression
2436 {
2437 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2438 $1, $3);
2439 }
2440| relational_expression GT_TK shift_expression
2441 {
2442 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2443 $1, $3);
2444 }
2445| relational_expression LTE_TK shift_expression
2446 {
2447 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2448 $1, $3);
2449 }
2450| relational_expression GTE_TK shift_expression
2451 {
2452 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2453 $1, $3);
2454 }
2455| relational_expression INSTANCEOF_TK reference_type
5e942c50 2456 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2457| relational_expression LT_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459| relational_expression GT_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461| relational_expression LTE_TK error
2462 {yyerror ("Missing term"); RECOVER;}
2463| relational_expression GTE_TK error
2464 {yyerror ("Missing term"); RECOVER;}
2465| relational_expression INSTANCEOF_TK error
2466 {yyerror ("Invalid reference type"); RECOVER;}
2467;
2468
2469equality_expression:
2470 relational_expression
2471| equality_expression EQ_TK relational_expression
2472 {
2473 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2474 $1, $3);
2475 }
2476| equality_expression NEQ_TK relational_expression
2477 {
2478 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2479 $1, $3);
2480 }
2481| equality_expression EQ_TK error
2482 {yyerror ("Missing term"); RECOVER;}
2483| equality_expression NEQ_TK error
2484 {yyerror ("Missing term"); RECOVER;}
2485;
2486
2487and_expression:
2488 equality_expression
2489| and_expression AND_TK equality_expression
2490 {
2491 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2492 $1, $3);
2493 }
2494| and_expression AND_TK error
2495 {yyerror ("Missing term"); RECOVER;}
2496;
2497
2498exclusive_or_expression:
2499 and_expression
2500| exclusive_or_expression XOR_TK and_expression
2501 {
2502 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2503 $1, $3);
2504 }
2505| exclusive_or_expression XOR_TK error
2506 {yyerror ("Missing term"); RECOVER;}
2507;
2508
2509inclusive_or_expression:
2510 exclusive_or_expression
2511| inclusive_or_expression OR_TK exclusive_or_expression
2512 {
2513 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2514 $1, $3);
2515 }
2516| inclusive_or_expression OR_TK error
2517 {yyerror ("Missing term"); RECOVER;}
2518;
2519
2520conditional_and_expression:
2521 inclusive_or_expression
2522| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2523 {
2524 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2525 $1, $3);
2526 }
2527| conditional_and_expression BOOL_AND_TK error
2528 {yyerror ("Missing term"); RECOVER;}
2529;
2530
2531conditional_or_expression:
2532 conditional_and_expression
2533| conditional_or_expression BOOL_OR_TK conditional_and_expression
2534 {
2535 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2536 $1, $3);
2537 }
2538| conditional_or_expression BOOL_OR_TK error
2539 {yyerror ("Missing term"); RECOVER;}
2540;
2541
2542conditional_expression: /* Error handling here is weak */
2543 conditional_or_expression
2544| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2545 {
2546 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2547 EXPR_WFL_LINECOL ($$) = $2.location;
2548 }
e04a16fb
AG
2549| conditional_or_expression REL_QM_TK REL_CL_TK error
2550 {
2551 YYERROR_NOW;
2552 yyerror ("Missing term");
2553 DRECOVER (1);
2554 }
2555| conditional_or_expression REL_QM_TK error
2556 {yyerror ("Missing term"); DRECOVER (2);}
2557| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2558 {yyerror ("Missing term"); DRECOVER (3);}
2559;
2560
2561assignment_expression:
2562 conditional_expression
2563| assignment
2564;
2565
2566assignment:
2567 left_hand_side assignment_operator assignment_expression
2568 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2569| left_hand_side assignment_operator error
2570 {
29f8b718 2571 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2572 DRECOVER (assign);
2573 }
2574;
2575
2576left_hand_side:
2577 name
2578| field_access
2579| array_access
2580;
2581
2582assignment_operator:
2583 ASSIGN_ANY_TK
2584| ASSIGN_TK
2585;
2586
2587expression:
2588 assignment_expression
2589;
2590
2591constant_expression:
2592 expression
2593;
2594
2595%%
2596\f
2597
c2952b01
APB
2598/* This section of the code deal with save/restoring parser contexts.
2599 Add mode documentation here. FIXME */
e04a16fb 2600
c2952b01
APB
2601/* Helper function. Create a new parser context. With
2602 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2603 context is copied, otherwise, the new context is zeroed. The newly
2604 created context becomes the current one. */
e04a16fb 2605
c2952b01
APB
2606static void
2607create_new_parser_context (copy_from_previous)
2608 int copy_from_previous;
e04a16fb 2609{
c2952b01 2610 struct parser_ctxt *new;
e04a16fb 2611
c2952b01
APB
2612 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2613 if (copy_from_previous)
2614 {
2615 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2616 new->saved_data_ctx = 1;
2617 }
2618 else
2e09e75a 2619 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2620
e04a16fb
AG
2621 new->next = ctxp;
2622 ctxp = new;
c2952b01
APB
2623}
2624
2625/* Create a new parser context and make it the current one. */
2626
2627void
2628java_push_parser_context ()
2629{
2630 create_new_parser_context (0);
e04a16fb 2631 if (ctxp->next)
5e942c50
APB
2632 {
2633 ctxp->incomplete_class = ctxp->next->incomplete_class;
2634 ctxp->gclass_list = ctxp->next->gclass_list;
2635 }
e04a16fb
AG
2636}
2637
c2952b01
APB
2638void
2639java_pop_parser_context (generate)
2640 int generate;
2641{
2642 tree current;
2643 struct parser_ctxt *toFree, *next;
2644
2645 if (!ctxp)
2646 return;
2647
2648 toFree = ctxp;
2649 next = ctxp->next;
2650 if (next)
2651 {
2652 next->incomplete_class = ctxp->incomplete_class;
2653 next->gclass_list = ctxp->gclass_list;
2654 lineno = ctxp->lineno;
19e223db 2655 current_class = ctxp->class_type;
c2952b01
APB
2656 }
2657
d19cbcb5
TT
2658 /* If the old and new lexers differ, then free the old one. */
2659 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2660 java_destroy_lexer (ctxp->lexer);
2661
c2952b01
APB
2662 /* Set the single import class file flag to 0 for the current list
2663 of imported things */
2664 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2665 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2666
2667 /* And restore those of the previous context */
2668 if ((ctxp = next)) /* Assignment is really meant here */
2669 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2670 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2671
2672 /* If we pushed a context to parse a class intended to be generated,
2673 we keep it so we can remember the class. What we could actually
2674 do is to just update a list of class names. */
2675 if (generate)
2676 {
2677 toFree->next = ctxp_for_generation;
2678 ctxp_for_generation = toFree;
2679 }
2680 else
2681 free (toFree);
2682}
2683
2684/* Create a parser context for the use of saving some global
2685 variables. */
2686
e04a16fb
AG
2687void
2688java_parser_context_save_global ()
2689{
22eed1e6
APB
2690 if (!ctxp)
2691 {
2692 java_push_parser_context ();
ee07f4f4
APB
2693 ctxp->saved_data_ctx = 1;
2694 }
c2952b01
APB
2695
2696 /* If this context already stores data, create a new one suitable
2697 for data storage. */
ee07f4f4 2698 else if (ctxp->saved_data)
c2952b01
APB
2699 create_new_parser_context (1);
2700
e04a16fb 2701 ctxp->lineno = lineno;
19e223db 2702 ctxp->class_type = current_class;
e04a16fb 2703 ctxp->filename = input_filename;
19e223db 2704 ctxp->function_decl = current_function_decl;
ee07f4f4 2705 ctxp->saved_data = 1;
e04a16fb
AG
2706}
2707
c2952b01
APB
2708/* Restore some global variables from the previous context. Make the
2709 previous context the current one. */
2710
e04a16fb
AG
2711void
2712java_parser_context_restore_global ()
2713{
e04a16fb 2714 lineno = ctxp->lineno;
19e223db 2715 current_class = ctxp->class_type;
e04a16fb 2716 input_filename = ctxp->filename;
dba41d30
APB
2717 if (wfl_operator)
2718 {
2719 tree s;
2720 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2721 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2722 }
19e223db 2723 current_function_decl = ctxp->function_decl;
c2952b01 2724 ctxp->saved_data = 0;
ee07f4f4
APB
2725 if (ctxp->saved_data_ctx)
2726 java_pop_parser_context (0);
e04a16fb
AG
2727}
2728
c2952b01
APB
2729/* Suspend vital data for the current class/function being parsed so
2730 that an other class can be parsed. Used to let local/anonymous
2731 classes be parsed. */
2732
2733static void
2734java_parser_context_suspend ()
e04a16fb 2735{
c2952b01 2736 /* This makes debugging through java_debug_context easier */
3b304f5b 2737 static const char *name = "<inner buffer context>";
e04a16fb 2738
c2952b01
APB
2739 /* Duplicate the previous context, use it to save the globals we're
2740 interested in */
2741 create_new_parser_context (1);
19e223db
MM
2742 ctxp->function_decl = current_function_decl;
2743 ctxp->class_type = current_class;
5e942c50 2744
c2952b01
APB
2745 /* Then create a new context which inherits all data from the
2746 previous one. This will be the new current context */
2747 create_new_parser_context (1);
2748
2749 /* Help debugging */
2750 ctxp->next->filename = name;
2751}
2752
2753/* Resume vital data for the current class/function being parsed so
2754 that an other class can be parsed. Used to let local/anonymous
2755 classes be parsed. The trick is the data storing file position
2756 informations must be restored to their current value, so parsing
2757 can resume as if no context was ever saved. */
2758
2759static void
2760java_parser_context_resume ()
2761{
2762 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2763 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2764 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2765
2766 /* We need to inherit the list of classes to complete/generate */
2767 restored->incomplete_class = old->incomplete_class;
2768 restored->gclass_list = old->gclass_list;
2769 restored->classd_list = old->classd_list;
2770 restored->class_list = old->class_list;
2771
2772 /* Restore the current class and function from the saver */
19e223db
MM
2773 current_class = saver->class_type;
2774 current_function_decl = saver->function_decl;
c2952b01
APB
2775
2776 /* Retrive the restored context */
2777 ctxp = restored;
2778
2779 /* Re-installed the data for the parsing to carry on */
2780 bcopy (&old->marker_begining, &ctxp->marker_begining,
2781 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2782
2783 /* Buffer context can now be discarded */
2784 free (saver);
2785 free (old);
2786}
2787
2788/* Add a new anchor node to which all statement(s) initializing static
2789 and non static initialized upon declaration field(s) will be
2790 linked. */
2791
2792static void
2793java_parser_context_push_initialized_field ()
2794{
2795 tree node;
2796
2797 node = build_tree_list (NULL_TREE, NULL_TREE);
2798 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2799 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2800
2801 node = build_tree_list (NULL_TREE, NULL_TREE);
2802 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2803 CPC_INITIALIZER_LIST (ctxp) = node;
2804
2805 node = build_tree_list (NULL_TREE, NULL_TREE);
2806 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2807 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2808}
2809
2810/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2811 remember them so we can use it to create and populate the finit$
c2952b01
APB
2812 or <clinit> functions. */
2813
2814static void
2815java_parser_context_pop_initialized_field ()
2816{
2817 tree stmts;
2818 tree class_type = TREE_TYPE (GET_CPC ());
2819
2820 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2821 {
c2952b01
APB
2822 stmts = CPC_INITIALIZER_STMT (ctxp);
2823 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2824 if (stmts && !java_error_count)
2825 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2826 }
2827
c2952b01
APB
2828 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2829 {
2830 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2831 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2832 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2833 /* Keep initialization in order to enforce 8.5 */
2834 if (stmts && !java_error_count)
2835 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2836 }
e04a16fb 2837
c2952b01
APB
2838 /* JDK 1.1 instance initializers */
2839 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2840 {
c2952b01
APB
2841 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2842 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2843 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2844 if (stmts && !java_error_count)
2845 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2846 }
c2952b01
APB
2847}
2848
2849static tree
2850reorder_static_initialized (list)
2851 tree list;
2852{
2853 /* We have to keep things in order. The alias initializer have to
2854 come first, then the initialized regular field, in reverse to
2855 keep them in lexical order. */
2856 tree marker, previous = NULL_TREE;
2857 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2858 if (TREE_CODE (marker) == TREE_LIST
2859 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2860 break;
2861
2862 /* No static initialized, the list is fine as is */
2863 if (!previous)
2864 list = TREE_CHAIN (marker);
2865
2866 /* No marker? reverse the whole list */
2867 else if (!marker)
2868 list = nreverse (list);
2869
2870 /* Otherwise, reverse what's after the marker and the new reordered
2871 sublist will replace the marker. */
b351b287 2872 else
c2952b01
APB
2873 {
2874 TREE_CHAIN (previous) = NULL_TREE;
2875 list = nreverse (list);
2876 list = chainon (TREE_CHAIN (marker), list);
2877 }
2878 return list;
e04a16fb
AG
2879}
2880
c2952b01
APB
2881/* Helper functions to dump the parser context stack. */
2882
2883#define TAB_CONTEXT(C) \
2884 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2885
2886static void
2887java_debug_context_do (tab)
2888 int tab;
2889{
ee07f4f4
APB
2890 struct parser_ctxt *copy = ctxp;
2891 while (copy)
2892 {
c2952b01 2893 TAB_CONTEXT (tab);
ee07f4f4 2894 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2895 TAB_CONTEXT (tab);
ee07f4f4 2896 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2897 TAB_CONTEXT (tab);
2898 fprintf (stderr, "lineno: %d\n", copy->lineno);
2899 TAB_CONTEXT (tab);
ee07f4f4
APB
2900 fprintf (stderr, "package: %s\n",
2901 (copy->package ?
2902 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2903 TAB_CONTEXT (tab);
ee07f4f4 2904 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2905 TAB_CONTEXT (tab);
ee07f4f4
APB
2906 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2907 copy = copy->next;
2908 tab += 2;
2909 }
ee07f4f4
APB
2910}
2911
c2952b01
APB
2912/* Dump the stacked up parser contexts. Intended to be called from a
2913 debugger. */
2914
ee07f4f4
APB
2915void
2916java_debug_context ()
2917{
2918 java_debug_context_do (0);
2919}
2920
c2952b01
APB
2921\f
2922
2923/* Flag for the error report routine to issue the error the first time
2924 it's called (overriding the default behavior which is to drop the
2925 first invocation and honor the second one, taking advantage of a
2926 richer context. */
2927static int force_error = 0;
ee07f4f4 2928
8119c720
APB
2929/* Reporting an constructor invocation error. */
2930static void
2931parse_ctor_invocation_error ()
2932{
2933 if (DECL_CONSTRUCTOR_P (current_function_decl))
2934 yyerror ("Constructor invocation must be first thing in a constructor");
2935 else
2936 yyerror ("Only constructors can invoke constructors");
2937}
2938
2939/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2940
2941static tree
2942parse_jdk1_1_error (msg)
49f48c71 2943 const char *msg;
b67d701b
PB
2944{
2945 sorry (": `%s' JDK1.1(TM) feature", msg);
2946 java_error_count++;
9bbc7d9f 2947 return empty_stmt_node;
b67d701b
PB
2948}
2949
e04a16fb
AG
2950static int do_warning = 0;
2951
2952void
2953yyerror (msg)
49f48c71 2954 const char *msg;
e04a16fb
AG
2955{
2956 static java_lc elc;
2957 static int prev_lineno;
49f48c71 2958 static const char *prev_msg;
e04a16fb 2959
0a2138e2 2960 int save_lineno;
e04a16fb
AG
2961 char *remainder, *code_from_source;
2962 extern struct obstack temporary_obstack;
2963
2964 if (!force_error && prev_lineno == lineno)
2965 return;
2966
2967 /* Save current error location but report latter, when the context is
2968 richer. */
2969 if (ctxp->java_error_flag == 0)
2970 {
2971 ctxp->java_error_flag = 1;
2972 elc = ctxp->elc;
2973 /* Do something to use the previous line if we're reaching the
2974 end of the file... */
2975#ifdef VERBOSE_SKELETON
2976 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2977#endif
2978 return;
2979 }
2980
2981 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2982 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2983 return;
2984
2985 ctxp->java_error_flag = 0;
2986 if (do_warning)
2987 java_warning_count++;
2988 else
2989 java_error_count++;
2990
807bc1db 2991 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2992 {
2993 elc.col = ctxp->p_line->char_col-1;
2994 elc.line = ctxp->p_line->lineno;
2995 }
2996
2997 save_lineno = lineno;
2998 prev_lineno = lineno = elc.line;
2999 prev_msg = msg;
3000
3001 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3002 obstack_grow0 (&temporary_obstack,
3003 code_from_source, strlen (code_from_source));
3004 remainder = obstack_finish (&temporary_obstack);
3005 if (do_warning)
3006 warning ("%s.\n%s", msg, remainder);
3007 else
3008 error ("%s.\n%s", msg, remainder);
3009
3010 /* This allow us to cheaply avoid an extra 'Invalid expression
3011 statement' error report when errors have been already reported on
3012 the same line. This occurs when we report an error but don't have
3013 a synchronization point other than ';', which
3014 expression_statement is the only one to take care of. */
3015 ctxp->prevent_ese = lineno = save_lineno;
3016}
3017
3018static void
15fdcfe9 3019issue_warning_error_from_context (cl, msg, ap)
5e942c50 3020 tree cl;
d4476be2 3021 const char *msg;
15fdcfe9 3022 va_list ap;
5e942c50 3023{
3b304f5b 3024 const char *saved, *saved_input_filename;
15fdcfe9
PB
3025 char buffer [4096];
3026 vsprintf (buffer, msg, ap);
3027 force_error = 1;
5e942c50
APB
3028
3029 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3030 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3031 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3032
3033 /* We have a CL, that's a good reason for using it if it contains data */
3034 saved = ctxp->filename;
3035 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3036 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3037 saved_input_filename = input_filename;
3038 input_filename = ctxp->filename;
15fdcfe9
PB
3039 java_error (NULL);
3040 java_error (buffer);
5e942c50 3041 ctxp->filename = saved;
1886c9d8 3042 input_filename = saved_input_filename;
15fdcfe9 3043 force_error = 0;
5e942c50
APB
3044}
3045
e04a16fb
AG
3046/* Issue an error message at a current source line CL */
3047
15fdcfe9 3048void
df32d2ce 3049parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3050{
d4476be2 3051#ifndef ANSI_PROTOTYPES
e04a16fb 3052 tree cl;
d4476be2 3053 const char *msg;
e04a16fb 3054#endif
e04a16fb
AG
3055 va_list ap;
3056
3057 VA_START (ap, msg);
d4476be2 3058#ifndef ANSI_PROTOTYPES
e04a16fb 3059 cl = va_arg (ap, tree);
d4476be2 3060 msg = va_arg (ap, const char *);
e04a16fb 3061#endif
15fdcfe9
PB
3062 issue_warning_error_from_context (cl, msg, ap);
3063 va_end (ap);
e04a16fb
AG
3064}
3065
3066/* Issue a warning at a current source line CL */
3067
3068static void
df32d2ce 3069parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3070{
d4476be2 3071#ifndef ANSI_PROTOTYPES
e04a16fb 3072 tree cl;
d4476be2 3073 const char *msg;
e04a16fb 3074#endif
e04a16fb
AG
3075 va_list ap;
3076
3077 VA_START (ap, msg);
d4476be2 3078#ifndef ANSI_PROTOTYPES
e04a16fb 3079 cl = va_arg (ap, tree);
d4476be2 3080 msg = va_arg (ap, const char *);
e04a16fb 3081#endif
e04a16fb 3082
c877974e 3083 force_error = do_warning = 1;
15fdcfe9 3084 issue_warning_error_from_context (cl, msg, ap);
c877974e 3085 do_warning = force_error = 0;
15fdcfe9 3086 va_end (ap);
e04a16fb
AG
3087}
3088
82371d41
APB
3089static tree
3090find_expr_with_wfl (node)
3091 tree node;
3092{
3093 while (node)
3094 {
3095 char code;
3096 tree to_return;
3097
3098 switch (TREE_CODE (node))
3099 {
3100 case BLOCK:
c0d87ff6
PB
3101 node = BLOCK_EXPR_BODY (node);
3102 continue;
82371d41
APB
3103
3104 case COMPOUND_EXPR:
3105 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3106 if (to_return)
3107 return to_return;
c0d87ff6
PB
3108 node = TREE_OPERAND (node, 1);
3109 continue;
82371d41
APB
3110
3111 case LOOP_EXPR:
c0d87ff6
PB
3112 node = TREE_OPERAND (node, 0);
3113 continue;
82371d41
APB
3114
3115 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3116 node = TREE_OPERAND (node, 1);
3117 continue;
3118
82371d41
APB
3119 default:
3120 code = TREE_CODE_CLASS (TREE_CODE (node));
3121 if (((code == '1') || (code == '2') || (code == 'e'))
3122 && EXPR_WFL_LINECOL (node))
3123 return node;
ba179f9f 3124 return NULL_TREE;
82371d41
APB
3125 }
3126 }
3127 return NULL_TREE;
3128}
3129
3130/* Issue a missing return statement error. Uses METHOD to figure the
3131 last line of the method the error occurs in. */
3132
3133static void
3134missing_return_error (method)
3135 tree method;
3136{
3137 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3138 parse_error_context (wfl_operator, "Missing return statement");
3139}
3140
3141/* Issue an unreachable statement error. From NODE, find the next
3142 statement to report appropriately. */
3143static void
3144unreachable_stmt_error (node)
3145 tree node;
3146{
3147 /* Browse node to find the next expression node that has a WFL. Use
3148 the location to report the error */
3149 if (TREE_CODE (node) == COMPOUND_EXPR)
3150 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3151 else
3152 node = find_expr_with_wfl (node);
3153
3154 if (node)
3155 {
3156 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3157 parse_error_context (wfl_operator, "Unreachable statement");
3158 }
3159 else
3160 fatal ("Can't get valid statement - unreachable_stmt_error");
3161}
3162
c877974e 3163int
e04a16fb
AG
3164java_report_errors ()
3165{
3166 if (java_error_count)
3167 fprintf (stderr, "%d error%s",
3168 java_error_count, (java_error_count == 1 ? "" : "s"));
3169 if (java_warning_count)
3170 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3171 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3172 if (java_error_count || java_warning_count)
3173 putc ('\n', stderr);
c877974e 3174 return java_error_count;
e04a16fb
AG
3175}
3176
3177static char *
3178java_accstring_lookup (flags)
3179 int flags;
3180{
3181 static char buffer [80];
3182#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3183
3184 /* Access modifier looked-up first for easier report on forbidden
3185 access. */
3186 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3187 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3188 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3189 if (flags & ACC_STATIC) COPY_RETURN ("static");
3190 if (flags & ACC_FINAL) COPY_RETURN ("final");
3191 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3192 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3193 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3194 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3195 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3196 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3197
3198 buffer [0] = '\0';
3199 return buffer;
3200#undef COPY_RETURN
3201}
3202
b67d701b
PB
3203/* Issuing error messages upon redefinition of classes, interfaces or
3204 variables. */
3205
e04a16fb 3206static void
b67d701b 3207classitf_redefinition_error (context, id, decl, cl)
49f48c71 3208 const char *context;
e04a16fb
AG
3209 tree id, decl, cl;
3210{
3211 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3212 context, IDENTIFIER_POINTER (id),
3213 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3214 /* Here we should point out where its redefined. It's a unicode. FIXME */
3215}
3216
b67d701b
PB
3217static void
3218variable_redefinition_error (context, name, type, line)
3219 tree context, name, type;
3220 int line;
3221{
49f48c71 3222 const char *type_name;
b67d701b
PB
3223
3224 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3225 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3226 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3227 else
0a2138e2 3228 type_name = lang_printable_name (type, 0);
b67d701b
PB
3229
3230 parse_error_context (context,
781b0558 3231 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3232 IDENTIFIER_POINTER (name),
3233 type_name, IDENTIFIER_POINTER (name), line);
3234}
3235
c583dd46
APB
3236static tree
3237build_array_from_name (type, type_wfl, name, ret_name)
3238 tree type, type_wfl, name, *ret_name;
3239{
3240 int more_dims = 0;
49f48c71 3241 const char *string;
c583dd46
APB
3242
3243 /* Eventually get more dims */
3244 string = IDENTIFIER_POINTER (name);
3245 while (string [more_dims] == '[')
3246 more_dims++;
3247
3248 /* If we have, then craft a new type for this variable */
3249 if (more_dims)
3250 {
c0d87ff6 3251 name = get_identifier (&string [more_dims]);
c583dd46 3252
34f4db93
APB
3253 /* If we have a pointer, use its type */
3254 if (TREE_CODE (type) == POINTER_TYPE)
3255 type = TREE_TYPE (type);
c583dd46
APB
3256
3257 /* Building the first dimension of a primitive type uses this
3258 function */
3259 if (JPRIMITIVE_TYPE_P (type))
3260 {
3261 type = build_java_array_type (type, -1);
22eed1e6 3262 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3263 more_dims--;
3264 }
3265 /* Otherwise, if we have a WFL for this type, use it (the type
3266 is already an array on an unresolved type, and we just keep
3267 on adding dimensions) */
3268 else if (type_wfl)
3269 type = type_wfl;
3270
3271 /* Add all the dimensions */
3272 while (more_dims--)
3273 type = build_unresolved_array_type (type);
3274
3275 /* The type may have been incomplete in the first place */
3276 if (type_wfl)
3277 type = obtain_incomplete_type (type);
3278 }
3279
c2952b01
APB
3280 if (ret_name)
3281 *ret_name = name;
c583dd46
APB
3282 return type;
3283}
3284
e04a16fb
AG
3285/* Build something that the type identifier resolver will identify as
3286 being an array to an unresolved type. TYPE_WFL is a WFL on a
3287 identifier. */
3288
3289static tree
3290build_unresolved_array_type (type_or_wfl)
3291 tree type_or_wfl;
3292{
49f48c71 3293 const char *ptr;
e04a16fb 3294
1886c9d8 3295 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3296 just create a array type */
3297 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3298 {
3299 tree type = build_java_array_type (type_or_wfl, -1);
3300 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3301 return type;
3302 }
3303
3304 obstack_1grow (&temporary_obstack, '[');
3305 obstack_grow0 (&temporary_obstack,
3306 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3307 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3308 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3309 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3310 return type_or_wfl;
e04a16fb
AG
3311}
3312
e04a16fb
AG
3313static void
3314parser_add_interface (class_decl, interface_decl, wfl)
3315 tree class_decl, interface_decl, wfl;
3316{
3317 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3318 parse_error_context (wfl, "Interface `%s' repeated",
3319 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3320}
3321
3322/* Bulk of common class/interface checks. Return 1 if an error was
3323 encountered. TAG is 0 for a class, 1 for an interface. */
3324
3325static int
3326check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3327 int is_interface, flags;
3328 tree raw_name, qualified_name, decl, cl;
3329{
3330 tree node;
c2952b01
APB
3331 int sca = 0; /* Static class allowed */
3332 int icaf = 0; /* Inner class allowed flags */
3333 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3334
3335 if (!quiet_flag)
c2952b01
APB
3336 fprintf (stderr, " %s%s %s",
3337 (CPC_INNER_P () ? "inner" : ""),
3338 (is_interface ? "interface" : "class"),
e04a16fb
AG
3339 IDENTIFIER_POINTER (qualified_name));
3340
3341 /* Scope of an interface/class type name:
3342 - Can't be imported by a single type import
3343 - Can't already exists in the package */
3344 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3345 && (node = find_name_in_single_imports (raw_name))
3346 && !CPC_INNER_P ())
e04a16fb
AG
3347 {
3348 parse_error_context
3349 (cl, "%s name `%s' clashes with imported type `%s'",
3350 (is_interface ? "Interface" : "Class"),
3351 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3352 return 1;
3353 }
3354 if (decl && CLASS_COMPLETE_P (decl))
3355 {
b67d701b
PB
3356 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3357 qualified_name, decl, cl);
e04a16fb
AG
3358 return 1;
3359 }
3360
c2952b01
APB
3361 if (check_inner_class_redefinition (raw_name, cl))
3362 return 1;
3363
3364 /* If public, file name should match class/interface name, except
3365 when dealing with an inner class */
3366 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3367 {
49f48c71 3368 const char *f;
e04a16fb
AG
3369
3370 /* Contains OS dependent assumption on path separator. FIXME */
3371 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3372 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3373 f--)
3374 ;
847fe791 3375 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3376 f++;
3377 if (strncmp (IDENTIFIER_POINTER (raw_name),
3378 f , IDENTIFIER_LENGTH (raw_name)) ||
3379 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3380 parse_error_context
3381 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3382 (is_interface ? "interface" : "class"),
3383 IDENTIFIER_POINTER (qualified_name),
3384 IDENTIFIER_POINTER (raw_name));
3385 }
3386
c2952b01
APB
3387 /* Static classes can be declared only in top level classes. Note:
3388 once static, a inner class is a top level class. */
3389 if (flags & ACC_STATIC)
3390 {
3391 /* Catch the specific error of declaring an class inner class
3392 with no toplevel enclosing class. Prevent check_modifiers from
3393 complaining a second time */
3394 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3395 {
3396 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3397 IDENTIFIER_POINTER (qualified_name));
3398 sca = ACC_STATIC;
3399 }
3400 /* Else, in the context of a top-level class declaration, let
3401 `check_modifiers' do its job, otherwise, give it a go */
3402 else
3403 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3404 }
3405
a40d21da 3406 /* Inner classes can be declared private or protected
c2952b01
APB
3407 within their enclosing classes. */
3408 if (CPC_INNER_P ())
3409 {
3410 /* A class which is local to a block can't be public, private,
3411 protected or static. But it is created final, so allow this
3412 one. */
3413 if (current_function_decl)
3414 icaf = sca = uaaf = ACC_FINAL;
3415 else
3416 {
3417 check_modifiers_consistency (flags);
3418 icaf = ACC_PRIVATE|ACC_PROTECTED;
3419 }
3420 }
3421
a40d21da
APB
3422 if (is_interface)
3423 {
3424 if (CPC_INNER_P ())
3425 uaaf = INTERFACE_INNER_MODIFIERS;
3426 else
3427 uaaf = INTERFACE_MODIFIERS;
3428
3429 check_modifiers ("Illegal modifier `%s' for interface declaration",
3430 flags, uaaf);
3431 }
2884c41e 3432 else
a40d21da
APB
3433 check_modifiers ((current_function_decl ?
3434 "Illegal modifier `%s' for local class declaration" :
3435 "Illegal modifier `%s' for class declaration"),
c2952b01 3436 flags, uaaf|sca|icaf);
e04a16fb
AG
3437 return 0;
3438}
3439
c2952b01
APB
3440static void
3441make_nested_class_name (cpc_list)
3442 tree cpc_list;
3443{
3444 tree name;
3445
3446 if (!cpc_list)
3447 return;
3448 else
3449 make_nested_class_name (TREE_CHAIN (cpc_list));
3450
3451 /* Pick the qualified name when dealing with the first upmost
3452 enclosing class */
3453 name = (TREE_CHAIN (cpc_list) ?
3454 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3455 obstack_grow (&temporary_obstack,
3456 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3457 /* Why is NO_DOLLAR_IN_LABEL defined? */
3458#if 0
3459#ifdef NO_DOLLAR_IN_LABEL
3460 fatal ("make_nested_class_name: Can't use '$' as a separator "
3461 "for inner classes");
3462#endif
3463#endif
3464 obstack_1grow (&temporary_obstack, '$');
3465}
3466
3467/* Can't redefine a class already defined in an earlier scope. */
3468
3469static int
3470check_inner_class_redefinition (raw_name, cl)
3471 tree raw_name, cl;
3472{
3473 tree scope_list;
3474
3475 for (scope_list = GET_CPC_LIST (); scope_list;
3476 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3477 if (raw_name == GET_CPC_UN_NODE (scope_list))
3478 {
3479 parse_error_context
3480 (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",
3481 IDENTIFIER_POINTER (raw_name));
3482 return 1;
3483 }
3484 return 0;
3485}
3486
3487static tree
3488find_as_inner_class (enclosing, name, cl)
3489 tree enclosing, name, cl;
3490{
3491 tree qual, to_return;
3492 if (!enclosing)
3493 return NULL_TREE;
3494
3495 name = TYPE_NAME (name);
3496
3497 /* First search: within the scope of `enclosing', search for name */
3498 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3499 qual = EXPR_WFL_QUALIFICATION (cl);
3500 else if (cl)
3501 qual = build_tree_list (cl, NULL_TREE);
3502 else
3503 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3504
3505 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3506 return to_return;
3507
3508 /* We're dealing with a qualified name. Try to resolve thing until
3509 we get something that is an enclosing class. */
3510 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3511 {
3512 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3513
0c2b8145
APB
3514 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3515 qual = TREE_CHAIN (qual))
c2952b01
APB
3516 {
3517 acc = merge_qualified_name (acc,
3518 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3519 BUILD_PTR_FROM_NAME (ptr, acc);
3520 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3521 }
3522
3523 /* A NULL qual and a decl means that the search ended
3524 successfully?!? We have to do something then. FIXME */
3525
3526 if (decl)
3527 enclosing = decl;
3528 else
3529 qual = EXPR_WFL_QUALIFICATION (cl);
3530 }
3531 /* Otherwise, create a qual for the other part of the resolution. */
3532 else
3533 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3534
1e12ab9b 3535 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3536}
3537
3538/* We go inside the list of sub classes and try to find a way
3539 through. */
3540
3541static tree
3542find_as_inner_class_do (qual, enclosing)
3543 tree qual, enclosing;
3544{
3545 if (!qual)
3546 return NULL_TREE;
3547
3548 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3549 {
3550 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3551 tree next_enclosing = NULL_TREE;
3552 tree inner_list;
3553
3554 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3555 inner_list; inner_list = TREE_CHAIN (inner_list))
3556 {
3557 if (TREE_VALUE (inner_list) == name_to_match)
3558 {
3559 next_enclosing = TREE_PURPOSE (inner_list);
3560 break;
3561 }
3562 }
3563 enclosing = next_enclosing;
3564 }
3565
3566 return (!qual && enclosing ? enclosing : NULL_TREE);
3567}
3568
3569/* Reach all inner classes and tie their unqualified name to a
3570 DECL. */
3571
3572static void
3573set_nested_class_simple_name_value (outer, set)
3574 tree outer;
3575 int set;
3576{
3577 tree l;
3578
3579 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3580 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3581 TREE_PURPOSE (l) : NULL_TREE);
3582}
3583
3584static void
3585link_nested_class_to_enclosing ()
3586{
3587 if (GET_ENCLOSING_CPC ())
3588 {
3589 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3590 DECL_INNER_CLASS_LIST (enclosing) =
3591 tree_cons (GET_CPC (), GET_CPC_UN (),
3592 DECL_INNER_CLASS_LIST (enclosing));
3593 enclosing = enclosing;
3594 }
3595}
3596
3597static tree
3598maybe_make_nested_class_name (name)
3599 tree name;
3600{
3601 tree id = NULL_TREE;
3602
3603 if (CPC_INNER_P ())
3604 {
3605 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3606 obstack_grow0 (&temporary_obstack,
3607 IDENTIFIER_POINTER (name),
3608 IDENTIFIER_LENGTH (name));
c2952b01
APB
3609 id = get_identifier (obstack_finish (&temporary_obstack));
3610 if (ctxp->package)
3611 QUALIFIED_P (id) = 1;
3612 }
3613 return id;
3614}
3615
3616/* If DECL is NULL, create and push a new DECL, record the current
3617 line CL and do other maintenance things. */
3618
e04a16fb 3619static tree
c2952b01
APB
3620maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3621 tree decl, raw_name, qualified_name, cl;
e04a16fb 3622{
5e942c50 3623 if (!decl)
e04a16fb 3624 decl = push_class (make_class (), qualified_name);
c2952b01 3625
e04a16fb
AG
3626 /* Take care of the file and line business */
3627 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3628 /* If we're emiting xrefs, store the line/col number information */
3629 if (flag_emit_xref)
3630 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3631 else
3632 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3633 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3634 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3635 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3636
c2952b01
APB
3637 PUSH_CPC (decl, raw_name);
3638 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3639
e04a16fb
AG
3640 /* Link the declaration to the already seen ones */
3641 TREE_CHAIN (decl) = ctxp->class_list;
3642 ctxp->class_list = decl;
5e942c50 3643
23a79c61 3644 /* Create a new nodes in the global lists */
5e942c50 3645 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3646 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3647
e04a16fb
AG
3648 /* Install a new dependency list element */
3649 create_jdep_list (ctxp);
3650
3651 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3652 IDENTIFIER_POINTER (qualified_name)));
3653 return decl;
3654}
3655
3656static void
3657add_superinterfaces (decl, interface_list)
3658 tree decl, interface_list;
3659{
3660 tree node;
3661 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3662 takes care of ensuring that:
3663 - This is an accessible interface type,
3664 - Circularity detection.
3665 parser_add_interface is then called. If present but not defined,
3666 the check operation is delayed until the super interface gets
3667 defined. */
3668 for (node = interface_list; node; node = TREE_CHAIN (node))
3669 {
15fdcfe9 3670 tree current = TREE_PURPOSE (node);
5e942c50
APB
3671 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3672 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3673 {
5e942c50
APB
3674 if (!parser_check_super_interface (idecl, decl, current))
3675 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3676 }
3677 else
3678 register_incomplete_type (JDEP_INTERFACE,
3679 current, decl, NULL_TREE);
3680 }
3681}
3682
3683/* Create an interface in pass1 and return its decl. Return the
3684 interface's decl in pass 2. */
3685
3686static tree
3687create_interface (flags, id, super)
3688 int flags;
3689 tree id, super;
3690{
e04a16fb 3691 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3692 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3693 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3694
3695 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3696
3697 /* Basic checks: scope, redefinition, modifiers */
3698 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3699 {
3700 PUSH_ERROR ();
3701 return NULL_TREE;
3702 }
3703
3704 /* Suspend the current parsing context if we're parsing an inner
3705 interface */
3706 if (CPC_INNER_P ())
3707 java_parser_context_suspend ();
3708
3709 /* Push a new context for (static) initialized upon declaration fields */
3710 java_parser_context_push_initialized_field ();
e04a16fb
AG
3711
3712 /* Interface modifiers check
3713 - public/abstract allowed (already done at that point)
3714 - abstract is obsolete (comes first, it's a warning, or should be)
3715 - Can't use twice the same (checked in the modifier rule) */
c877974e 3716 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3717 parse_warning_context
3718 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3719 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3720
3721 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3722 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3723
3724 /* Set super info and mark the class a complete */
2aa11e97 3725 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3726 object_type_node, ctxp->interface_number);
3727 ctxp->interface_number = 0;
3728 CLASS_COMPLETE_P (decl) = 1;
3729 add_superinterfaces (decl, super);
3730
3731 return decl;
3732}
3733
c2952b01
APB
3734/* Anonymous class counter. Will be reset to 1 every time a non
3735 anonymous class gets created. */
3736static int anonymous_class_counter = 1;
3737
3738/* Patch anonymous class CLASS, by either extending or implementing
3739 DEP. */
3740
3741static void
3742patch_anonymous_class (type_decl, class_decl, wfl)
3743 tree type_decl, class_decl, wfl;
3744{
3745 tree class = TREE_TYPE (class_decl);
3746 tree type = TREE_TYPE (type_decl);
3747 tree binfo = TYPE_BINFO (class);
3748
3749 /* If it's an interface, implement it */
3750 if (CLASS_INTERFACE (type_decl))
3751 {
3752 tree s_binfo;
3753 int length;
3754
3755 if (parser_check_super_interface (type_decl, class_decl, wfl))
3756 return;
3757
3758 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3759 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3760 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3761 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3762 /* And add the interface */
3763 parser_add_interface (class_decl, type_decl, wfl);
3764 }
3765 /* Otherwise, it's a type we want to extend */
3766 else
3767 {
3768 if (parser_check_super (type_decl, class_decl, wfl))
3769 return;
3770 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3771 }
3772}
3773
3774static tree
3775create_anonymous_class (location, type_name)
3776 int location;
3777 tree type_name;
3778{
3779 char buffer [80];
3780 tree super = NULL_TREE, itf = NULL_TREE;
3781 tree id, type_decl, class;
3782
3783 /* The unqualified name of the anonymous class. It's just a number. */
3784 sprintf (buffer, "%d", anonymous_class_counter++);
3785 id = build_wfl_node (get_identifier (buffer));
3786 EXPR_WFL_LINECOL (id) = location;
3787
3788 /* We know about the type to extend/implement. We go ahead */
3789 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3790 {
3791 /* Create a class which either implements on extends the designated
3792 class. The class bears an innacessible name. */
3793 if (CLASS_INTERFACE (type_decl))
3794 {
3795 /* It's OK to modify it here. It's been already used and
3796 shouldn't be reused */
3797 ctxp->interface_number = 1;
3798 /* Interfaces should presented as a list of WFLs */
3799 itf = build_tree_list (type_name, NULL_TREE);
3800 }
3801 else
3802 super = type_name;
3803 }
3804
3805 class = create_class (ACC_FINAL, id, super, itf);
3806
3807 /* We didn't know anything about the stuff. We register a dependence. */
3808 if (!type_decl)
3809 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3810
3811 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3812 return class;
3813}
3814
a40d21da 3815/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3816 interface's decl in pass 2. */
3817
3818static tree
3819create_class (flags, id, super, interfaces)
3820 int flags;
3821 tree id, super, interfaces;
3822{
e04a16fb
AG
3823 tree raw_name = EXPR_WFL_NODE (id);
3824 tree class_id, decl;
9ee9b555 3825 tree super_decl_type;
e04a16fb 3826
98a52c2c 3827 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3828 decl = IDENTIFIER_CLASS_VALUE (class_id);
3829 EXPR_WFL_NODE (id) = class_id;
3830
3831 /* Basic check: scope, redefinition, modifiers */
3832 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3833 {
3834 PUSH_ERROR ();
3835 return NULL_TREE;
3836 }
3837
3838 /* Suspend the current parsing context if we're parsing an inner
3839 class or an anonymous class. */
3840 if (CPC_INNER_P ())
3841 java_parser_context_suspend ();
3842 /* Push a new context for (static) initialized upon declaration fields */
3843 java_parser_context_push_initialized_field ();
e04a16fb
AG
3844
3845 /* Class modifier check:
3846 - Allowed modifier (already done at that point)
3847 - abstract AND final forbidden
3848 - Public classes defined in the correct file */
3849 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3850 parse_error_context
3851 (id, "Class `%s' can't be declared both abstract and final",
3852 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3853
3854 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3855 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3856
3857 /* If SUPER exists, use it, otherwise use Object */
3858 if (super)
3859 {
3860 /* Can't extend java.lang.Object */
3861 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3862 {
3863 parse_error_context (id, "Can't extend `java.lang.Object'");
3864 return NULL_TREE;
3865 }
3866
2c3199bc
PB
3867 super_decl_type =
3868 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3869 }
3870 else if (TREE_TYPE (decl) != object_type_node)
3871 super_decl_type = object_type_node;
3872 /* We're defining java.lang.Object */
3873 else
3874 super_decl_type = NULL_TREE;
3875
3876 /* Set super info and mark the class a complete */
3877 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3878 ctxp->interface_number);
3879 ctxp->interface_number = 0;
3880 CLASS_COMPLETE_P (decl) = 1;
3881 add_superinterfaces (decl, interfaces);
3882
c2952b01
APB
3883 /* Add the private this$<n> field, Replicate final locals still in
3884 scope as private final fields mangled like val$<local_name>.
3885 This doesn't not occur for top level (static) inner classes. */
3886 if (PURE_INNER_CLASS_DECL_P (decl))
3887 add_inner_class_fields (decl, current_function_decl);
3888
7f10c2e2
APB
3889 /* If doing xref, store the location at which the inherited class
3890 (if any) was seen. */
3891 if (flag_emit_xref && super)
3892 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3893
5e942c50
APB
3894 /* Eventually sets the @deprecated tag flag */
3895 CHECK_DEPRECATED (decl);
3896
165f37bc
APB
3897 /* Reset the anonymous class counter when declaring non inner classes */
3898 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3899 anonymous_class_counter = 1;
3900
e04a16fb
AG
3901 return decl;
3902}
3903
c2952b01 3904/* End a class declaration: register the statements used to create
c00f0fb2 3905 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3906 parser context if necessary. */
3907
3908static void
3909end_class_declaration (resume)
3910 int resume;
3911{
3912 /* If an error occured, context weren't pushed and won't need to be
3913 popped by a resume. */
3914 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3915
3916 java_parser_context_pop_initialized_field ();
3917 POP_CPC ();
3918 if (resume && no_error_occured)
3919 java_parser_context_resume ();
93220702
APB
3920
3921 /* We're ending a class declaration, this is a good time to reset
3922 the interface cout. Note that might have been already done in
3923 create_interface, but if at that time an inner class was being
3924 dealt with, the interface count was reset in a context created
3925 for the sake of handling inner classes declaration. */
3926 ctxp->interface_number = 0;
c2952b01
APB
3927}
3928
3929static void
3930add_inner_class_fields (class_decl, fct_decl)
3931 tree class_decl;
3932 tree fct_decl;
3933{
3934 tree block, marker, f;
3935
3936 f = add_field (TREE_TYPE (class_decl),
3937 build_current_thisn (TREE_TYPE (class_decl)),
3938 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3939 ACC_PRIVATE);
3940 FIELD_THISN (f) = 1;
3941
3942 if (!fct_decl)
3943 return;
3944
3945 for (block = GET_CURRENT_BLOCK (fct_decl);
3946 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3947 {
3948 tree decl;
3949 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3950 {
1f8f4a0b 3951 tree name, pname;
c2952b01
APB
3952 tree wfl, init, list;
3953
3954 /* Avoid non final arguments. */
3955 if (!LOCAL_FINAL (decl))
3956 continue;
3957
3958 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3959 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3960 wfl = build_wfl_node (name);
3961 init = build_wfl_node (pname);
c2952b01 3962 /* Build an initialization for the field: it will be
c00f0fb2 3963 initialized by a parameter added to finit$, bearing a
c2952b01 3964 mangled name of the field itself (param$<n>.) The
c00f0fb2 3965 parameter is provided to finit$ by the constructor
c2952b01
APB
3966 invoking it (hence the constructor will also feature a
3967 hidden parameter, set to the value of the outer context
3968 local at the time the inner class is created.)
3969
3970 Note: we take into account all possible locals that can
3971 be accessed by the inner class. It's actually not trivial
3972 to minimize these aliases down to the ones really
3973 used. One way to do that would be to expand all regular
c00f0fb2 3974 methods first, then finit$ to get a picture of what's
c2952b01
APB
3975 used. It works with the exception that we would have to
3976 go back on all constructor invoked in regular methods to
3977 have their invokation reworked (to include the right amount
3978 of alias initializer parameters.)
3979
3980 The only real way around, I think, is a first pass to
3981 identify locals really used in the inner class. We leave
3982 the flag FIELD_LOCAL_ALIAS_USED around for that future
3983 use.
3984
3985 On the other hand, it only affect local inner classes,
c00f0fb2 3986 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3987 unecessary arguments. It's easy for a developper to keep
3988 this number of parameter down by using the `final'
3989 keyword only when necessary. For the time being, we can
3990 issue a warning on unecessary finals. FIXME */
3991 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3992 wfl, init);
3993
3994 /* Register the field. The TREE_LIST holding the part
3995 initialized/initializer will be marked ARG_FINAL_P so
3996 that the created field can be marked
3997 FIELD_LOCAL_ALIAS. */
3998 list = build_tree_list (wfl, init);
3999 ARG_FINAL_P (list) = 1;
4000 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4001 }
4002 }
4003
4004 if (!CPC_INITIALIZER_STMT (ctxp))
4005 return;
4006
4007 /* If we ever registered an alias field, insert and marker to
4008 remeber where the list ends. The second part of the list (the one
4009 featuring initialized fields) so it can be later reversed to
4010 enforce 8.5. The marker will be removed during that operation. */
4011 marker = build_tree_list (NULL_TREE, NULL_TREE);
4012 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4013 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4014}
4015
e04a16fb
AG
4016/* Can't use lookup_field () since we don't want to load the class and
4017 can't set the CLASS_LOADED_P flag */
4018
4019static tree
4020find_field (class, name)
4021 tree class;
4022 tree name;
4023{
4024 tree decl;
4025 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4026 {
4027 if (DECL_NAME (decl) == name)
4028 return decl;
4029 }
4030 return NULL_TREE;
4031}
4032
4033/* Wrap around lookup_field that doesn't potentially upset the value
4034 of CLASS */
4035
4036static tree
4037lookup_field_wrapper (class, name)
4038 tree class, name;
4039{
4040 tree type = class;
9a7ab4b3 4041 tree decl = NULL_TREE;
c877974e 4042 java_parser_context_save_global ();
f2760b27
APB
4043
4044 /* Last chance: if we're within the context of an inner class, we
4045 might be trying to access a local variable defined in an outer
4046 context. We try to look for it now. */
9a7ab4b3 4047 if (INNER_CLASS_TYPE_P (class))
f2760b27 4048 {
9a7ab4b3 4049 tree new_name;
1f8f4a0b 4050 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4051 decl = lookup_field (&type, new_name);
f2760b27
APB
4052 if (decl && decl != error_mark_node)
4053 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4054 }
9a7ab4b3
APB
4055 if (!decl || decl == error_mark_node)
4056 {
4057 type = class;
4058 decl = lookup_field (&type, name);
4059 }
f2760b27 4060
c877974e 4061 java_parser_context_restore_global ();
93024893 4062 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4063}
4064
4065/* Find duplicate field within the same class declarations and report
c583dd46
APB
4066 the error. Returns 1 if a duplicated field was found, 0
4067 otherwise. */
e04a16fb
AG
4068
4069static int
c583dd46 4070duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4071 tree new_field_name, new_type, cl;
e04a16fb
AG
4072{
4073 /* This might be modified to work with method decl as well */
c2952b01 4074 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4075 if (decl)
4076 {
c2e3db92 4077 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4078 ((TREE_CODE (new_type) == POINTER_TYPE
4079 && TREE_TYPE (new_type) == NULL_TREE) ?
4080 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4081 lang_printable_name (new_type, 1)));
c877974e
APB
4082 /* The type may not have been completed by the time we report
4083 the error */
c2e3db92 4084 char *t2 = xstrdup (purify_type_name
4a5f66c3 4085 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4086 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4087 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4088 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4089 parse_error_context
4090 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4091 t1, IDENTIFIER_POINTER (new_field_name),
4092 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4093 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4094 free (t1);
4095 free (t2);
c583dd46 4096 return 1;
e04a16fb 4097 }
c583dd46 4098 return 0;
e04a16fb
AG
4099}
4100
4101/* Field registration routine. If TYPE doesn't exist, field
4102 declarations are linked to the undefined TYPE dependency list, to
4103 be later resolved in java_complete_class () */
4104
4105static void
4106register_fields (flags, type, variable_list)
4107 int flags;
4108 tree type, variable_list;
4109{
c583dd46 4110 tree current, saved_type;
c2952b01 4111 tree class_type = NULL_TREE;
e04a16fb
AG
4112 int saved_lineno = lineno;
4113 int must_chain = 0;
4114 tree wfl = NULL_TREE;
4115
c2952b01
APB
4116 if (GET_CPC ())
4117 class_type = TREE_TYPE (GET_CPC ());
4118
4119 if (!class_type || class_type == error_mark_node)
4120 return;
4121
e04a16fb
AG
4122 /* If we're adding fields to interfaces, those fields are public,
4123 static, final */
4124 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4125 {
4126 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4127 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4128 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4129 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4130 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4131 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4132 check_modifiers ("Illegal interface member modifier `%s'", flags,
4133 INTERFACE_FIELD_MODIFIERS);
4134 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4135 }
4136
c583dd46
APB
4137 /* Obtain a suitable type for resolution, if necessary */
4138 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4139
4140 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4141 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4142
c583dd46
APB
4143 for (current = variable_list, saved_type = type; current;
4144 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4145 {
c877974e 4146 tree real_type;
c583dd46 4147 tree field_decl;
e04a16fb
AG
4148 tree cl = TREE_PURPOSE (current);
4149 tree init = TREE_VALUE (current);
4150 tree current_name = EXPR_WFL_NODE (cl);
4151
cf1748bf 4152 /* Can't declare non-final static fields in inner classes */
c2952b01 4153 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4154 && !(flags & ACC_FINAL))
c2952b01 4155 parse_error_context
cf1748bf 4156 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4157 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4158 lang_printable_name (class_type, 0));
4159
c583dd46
APB
4160 /* Process NAME, as it may specify extra dimension(s) for it */
4161 type = build_array_from_name (type, wfl, current_name, &current_name);
4162
c583dd46
APB
4163 /* Type adjustment. We may have just readjusted TYPE because
4164 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4165 a reference if we can and don't have one already. Also
4166 change the name if we have an init. */
4167 if (type != saved_type)
4168 {
1886c9d8 4169 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4170 if (init)
4171 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4172 }
e04a16fb 4173
c877974e
APB
4174 real_type = GET_REAL_TYPE (type);
4175 /* Check for redeclarations */
4176 if (duplicate_declaration_error_p (current_name, real_type, cl))
4177 continue;
4178
c583dd46 4179 /* Set lineno to the line the field was found and create a
5e942c50 4180 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4181 if (flag_emit_xref)
4182 lineno = EXPR_WFL_LINECOL (cl);
4183 else
4184 lineno = EXPR_WFL_LINENO (cl);
c877974e 4185 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4186 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4187
4188 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4189 mark the created field FIELD_LOCAL_ALIAS, so that we can
c00f0fb2 4190 hide parameters to this inner class finit$ and constructors. */
c2952b01
APB
4191 if (ARG_FINAL_P (current))
4192 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4193
4194 /* Check if we must chain. */
4195 if (must_chain)
4196 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4197
c583dd46
APB
4198 /* If we have an initialization value tied to the field */
4199 if (init)
4200 {
4201 /* The field is declared static */
e04a16fb 4202 if (flags & ACC_STATIC)
e04a16fb 4203 {
7525cc04
APB
4204 /* We include the field and its initialization part into
4205 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4206 walked, field initializations will be processed and
4207 fields initialized with known constants will be taken
4208 out of <clinit> and have their DECL_INITIAL set
7525cc04 4209 appropriately. */
c2952b01
APB
4210 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4211 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4212 if (TREE_OPERAND (init, 1)
4213 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4214 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4215 }
5e942c50
APB
4216 /* A non-static field declared with an immediate initialization is
4217 to be initialized in <init>, if any. This field is remembered
4218 to be processed at the time of the generation of <init>. */
c583dd46
APB
4219 else
4220 {
c2952b01
APB
4221 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4222 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4223 }
5b09b33e 4224 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4225 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4226 }
4227 }
4228 lineno = saved_lineno;
4229}
4230
c00f0fb2
APB
4231/* Generate finit$, using the list of initialized fields to populate
4232 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4233 one(s) used to initialized the field(s) caching outer context
4234 local(s). */
22eed1e6 4235
c2952b01
APB
4236static tree
4237generate_finit (class_type)
4238 tree class_type;
22eed1e6 4239{
c2952b01
APB
4240 int count = 0;
4241 tree list = TYPE_FINIT_STMT_LIST (class_type);
4242 tree mdecl, current, parms;
4243
4244 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4245 class_type, NULL_TREE,
4246 &count);
4247 CRAFTED_PARAM_LIST_FIXUP (parms);
4248 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4249 finit_identifier_node, parms);
4250 fix_method_argument_names (parms, mdecl);
4251 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4252 mdecl, NULL_TREE);
4253 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4254 start_artificial_method_body (mdecl);
4255
c2952b01 4256 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4257 java_method_add_stmt (mdecl,
4258 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4259 current));
22eed1e6 4260 end_artificial_method_body (mdecl);
c2952b01 4261 return mdecl;
22eed1e6
APB
4262}
4263
e04a16fb 4264static void
c2952b01
APB
4265add_instance_initializer (mdecl)
4266 tree mdecl;
e04a16fb 4267{
c2952b01
APB
4268 tree current;
4269 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4270 tree compound = NULL_TREE;
e04a16fb 4271
c2952b01 4272 if (stmt_list)
e04a16fb 4273 {
c2952b01
APB
4274 for (current = stmt_list; current; current = TREE_CHAIN (current))
4275 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4276
c2952b01
APB
4277 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4278 NULL_TREE, compound));
4279 }
e04a16fb
AG
4280}
4281
4282/* Shared accros method_declarator and method_header to remember the
4283 patch stage that was reached during the declaration of the method.
4284 A method DECL is built differently is there is no patch
4285 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4286 pending on the currently defined method. */
4287
4288static int patch_stage;
4289
4290/* Check the method declaration and add the method to its current
4291 class. If the argument list is known to contain incomplete types,
4292 the method is partially added and the registration will be resume
22eed1e6
APB
4293 once the method arguments resolved. If TYPE is NULL, we're dealing
4294 with a constructor. */
e04a16fb
AG
4295
4296static tree
4297method_header (flags, type, mdecl, throws)
4298 int flags;
4299 tree type, mdecl, throws;
4300{
1886c9d8 4301 tree type_wfl = NULL_TREE;
79d13333 4302 tree meth_name = NULL_TREE;
c2952b01 4303 tree current, orig_arg, this_class = NULL;
34d4df06 4304 tree id, meth;
e04a16fb 4305 int saved_lineno;
1886c9d8 4306 int constructor_ok = 0, must_chain;
c2952b01 4307 int count;
34d4df06
APB
4308
4309 if (mdecl == error_mark_node)
4310 return error_mark_node;
4311 meth = TREE_VALUE (mdecl);
4312 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4313
4314 check_modifiers_consistency (flags);
79d13333 4315
c2952b01
APB
4316 if (GET_CPC ())
4317 this_class = TREE_TYPE (GET_CPC ());
4318
4319 if (!this_class || this_class == error_mark_node)
79d13333 4320 return NULL_TREE;
e04a16fb
AG
4321
4322 /* There are some forbidden modifiers for an abstract method and its
4323 class must be abstract as well. */
22eed1e6 4324 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4325 {
4326 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4327 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4328 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4329 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4330 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4331 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4332 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4333 parse_error_context
781b0558 4334 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4335 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4336 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4337 }
c2952b01 4338
22eed1e6
APB
4339 /* Things to be checked when declaring a constructor */
4340 if (!type)
4341 {
4342 int ec = java_error_count;
4343 /* 8.6: Constructor declarations: we might be trying to define a
4344 method without specifying a return type. */
c2952b01 4345 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4346 parse_error_context
4347 (id, "Invalid method declaration, return type required");
4348 /* 8.6.3: Constructor modifiers */
4349 else
4350 {
4351 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4352 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4353 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4354 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4355 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4356 }
4357 /* If we found error here, we don't consider it's OK to tread
4358 the method definition as a constructor, for the rest of this
4359 function */
4360 if (ec == java_error_count)
4361 constructor_ok = 1;
4362 }
e04a16fb
AG
4363
4364 /* Method declared within the scope of an interface are implicitly
4365 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4366 modifiers are checked right after. */
e04a16fb
AG
4367
4368 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4369 {
4370 /* If FLAGS isn't set because of a modifier, turn the
4371 corresponding modifier WFL to NULL so we issue a warning on
4372 the obsolete use of the modifier */
4373 if (!(flags & ACC_PUBLIC))
4374 MODIFIER_WFL (PUBLIC_TK) = NULL;
4375 if (!(flags & ACC_ABSTRACT))
4376 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4377 flags |= ACC_PUBLIC;
4378 flags |= ACC_ABSTRACT;
4379 }
4380
c2952b01
APB
4381 /* Inner class can't declare static methods */
4382 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4383 {
4384 parse_error_context
4385 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4386 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4387 lang_printable_name (this_class, 0));
4388 }
4389
e04a16fb
AG
4390 /* Modifiers context reset moved up, so abstract method declaration
4391 modifiers can be later checked. */
4392
22eed1e6
APB
4393 /* Set constructor returned type to void and method name to <init>,
4394 unless we found an error identifier the constructor (in which
4395 case we retain the original name) */
4396 if (!type)
4397 {
4398 type = void_type_node;
4399 if (constructor_ok)
4400 meth_name = init_identifier_node;
4401 }
4402 else
4403 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4404
1886c9d8
APB
4405 /* Do the returned type resolution and registration if necessary */
4406 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4407
4a5f66c3
APB
4408 if (meth_name)
4409 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4410 EXPR_WFL_NODE (id) = meth_name;
4411 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4412
4413 if (must_chain)
e04a16fb 4414 {
1886c9d8
APB
4415 patch_stage = JDEP_METHOD_RETURN;
4416 register_incomplete_type (patch_stage, type_wfl, id, type);
4417 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4418 }
4419 else
1886c9d8 4420 TREE_TYPE (meth) = type;
e04a16fb
AG
4421
4422 saved_lineno = lineno;
4423 /* When defining an abstract or interface method, the curly
4424 bracket at level 1 doesn't exist because there is no function
4425 body */
4426 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4427 EXPR_WFL_LINENO (id));
4428
5e942c50
APB
4429 /* Remember the original argument list */
4430 orig_arg = TYPE_ARG_TYPES (meth);
4431
e04a16fb
AG
4432 if (patch_stage) /* includes ret type and/or all args */
4433 {
4434 jdep *jdep;
4435 meth = add_method_1 (this_class, flags, meth_name, meth);
4436 /* Patch for the return type */
4437 if (patch_stage == JDEP_METHOD_RETURN)
4438 {
4439 jdep = CLASSD_LAST (ctxp->classd_list);
4440 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4441 }
4442 /* This is the stop JDEP. METH allows the function's signature
4443 to be computed. */
4444 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4445 }
4446 else
5e942c50
APB
4447 meth = add_method (this_class, flags, meth_name,
4448 build_java_signature (meth));
4449
c2952b01
APB
4450 /* Remember final parameters */
4451 MARK_FINAL_PARMS (meth, orig_arg);
4452
5e942c50
APB
4453 /* Fix the method argument list so we have the argument name
4454 information */
4455 fix_method_argument_names (orig_arg, meth);
4456
4457 /* Register the parameter number and re-install the current line
4458 number */
e04a16fb
AG
4459 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4460 lineno = saved_lineno;
b9f7e36c
APB
4461
4462 /* Register exception specified by the `throws' keyword for
4463 resolution and set the method decl appropriate field to the list.
4464 Note: the grammar ensures that what we get here are class
4465 types. */
4466 if (throws)
4467 {
4468 throws = nreverse (throws);
4469 for (current = throws; current; current = TREE_CHAIN (current))
4470 {
4471 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4472 NULL_TREE, NULL_TREE);
4473 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4474 &TREE_VALUE (current);
4475 }
4476 DECL_FUNCTION_THROWS (meth) = throws;
4477 }
4478
e04a16fb
AG
4479 /* We set the DECL_NAME to ID so we can track the location where
4480 the function was declared. This allow us to report
4481 redefinition error accurately. When method are verified,
4482 DECL_NAME is reinstalled properly (using the content of the
4483 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4484 when Object is being defined. Constructor <init> names will be
4485 reinstalled the same way. */
c2952b01 4486 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4487 DECL_NAME (meth) = id;
22eed1e6
APB
4488
4489 /* Set the flag if we correctly processed a constructor */
4490 if (constructor_ok)
c2952b01
APB
4491 {
4492 DECL_CONSTRUCTOR_P (meth) = 1;
4493 /* Compute and store the number of artificial parameters declared
4494 for this constructor */
4495 for (count = 0, current = TYPE_FIELDS (this_class); current;
4496 current = TREE_CHAIN (current))
4497 if (FIELD_LOCAL_ALIAS (current))
4498 count++;
4499 DECL_FUNCTION_NAP (meth) = count;
4500 }
22eed1e6 4501
5e942c50
APB
4502 /* Eventually set the @deprecated tag flag */
4503 CHECK_DEPRECATED (meth);
4504
7f10c2e2
APB
4505 /* If doing xref, store column and line number information instead
4506 of the line number only. */
4507 if (flag_emit_xref)
4508 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4509
e04a16fb
AG
4510 return meth;
4511}
4512
5e942c50
APB
4513static void
4514fix_method_argument_names (orig_arg, meth)
4515 tree orig_arg, meth;
4516{
4517 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4518 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4519 {
4520 TREE_PURPOSE (arg) = this_identifier_node;
4521 arg = TREE_CHAIN (arg);
4522 }
de4c7b02 4523 while (orig_arg != end_params_node)
5e942c50
APB
4524 {
4525 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4526 orig_arg = TREE_CHAIN (orig_arg);
4527 arg = TREE_CHAIN (arg);
4528 }
4529}
4530
22eed1e6
APB
4531/* Complete the method declaration with METHOD_BODY. */
4532
4533static void
b635eb2f 4534finish_method_declaration (method_body)
22eed1e6
APB
4535 tree method_body;
4536{
79d13333
APB
4537 int flags;
4538
4539 if (!current_function_decl)
4540 return;
4541
4542 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4543
4544 /* 8.4.5 Method Body */
4545 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4546 {
4547 tree wfl = DECL_NAME (current_function_decl);
4548 parse_error_context (wfl,
4549 "%s method `%s' can't have a body defined",
4550 (METHOD_NATIVE (current_function_decl) ?
4551 "Native" : "Abstract"),
4552 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4553 method_body = NULL_TREE;
4554 }
4555 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4556 {
4557 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4558 parse_error_context
4559 (wfl,
4560 "Non native and non abstract method `%s' must have a body defined",
4561 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4562 method_body = NULL_TREE;
4563 }
4564
2c56429a
APB
4565 if (flag_emit_class_files && method_body
4566 && TREE_CODE (method_body) == NOP_EXPR
4567 && TREE_TYPE (current_function_decl)
4568 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4569 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4570
22eed1e6
APB
4571 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4572 maybe_absorb_scoping_blocks ();
4573 /* Exit function's body */
4574 exit_block ();
4575 /* Merge last line of the function with first line, directly in the
4576 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4577 if (!flag_emit_xref)
4578 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4579
4580 /* Since function's argument's list are shared, reset the
4581 ARG_FINAL_P parameter that might have been set on some of this
4582 function parameters. */
4583 UNMARK_FINAL_PARMS (current_function_decl);
4584
f099f336
APB
4585 /* So we don't have an irrelevant function declaration context for
4586 the next static block we'll see. */
4587 current_function_decl = NULL_TREE;
22eed1e6
APB
4588}
4589
4590/* Build a an error message for constructor circularity errors. */
4591
4592static char *
4593constructor_circularity_msg (from, to)
4594 tree from, to;
4595{
4596 static char string [4096];
c2e3db92 4597 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4598 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4599 free (t);
4600 return string;
4601}
4602
4603/* Verify a circular call to METH. Return 1 if an error is found, 0
4604 otherwise. */
4605
4606static int
4607verify_constructor_circularity (meth, current)
4608 tree meth, current;
4609{
4610 static tree list = NULL_TREE;
19e223db 4611 static int initialized_p;
22eed1e6 4612 tree c;
19e223db
MM
4613
4614 /* If we haven't already registered LIST with the garbage collector,
4615 do so now. */
4616 if (!initialized_p)
4617 {
4618 ggc_add_tree_root (&list, 1);
4619 initialized_p = 1;
4620 }
4621
22eed1e6
APB
4622 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4623 {
4624 if (TREE_VALUE (c) == meth)
4625 {
4626 char *t;
4627 if (list)
4628 {
4629 tree liste;
4630 list = nreverse (list);
4631 for (liste = list; liste; liste = TREE_CHAIN (liste))
4632 {
4633 parse_error_context
c63b98cd 4634 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4635 constructor_circularity_msg
4636 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4637 java_error_count--;
4638 }
4639 }
c2e3db92 4640 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4641 parse_error_context (TREE_PURPOSE (c),
4642 "%s: recursive invocation of constructor `%s'",
4643 constructor_circularity_msg (current, meth), t);
4644 free (t);
4645 list = NULL_TREE;
4646 return 1;
4647 }
4648 }
4649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4650 {
4651 list = tree_cons (c, current, list);
4652 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4653 return 1;
4654 list = TREE_CHAIN (list);
4655 }
4656 return 0;
4657}
4658
e04a16fb
AG
4659/* Check modifiers that can be declared but exclusively */
4660
4661static void
4662check_modifiers_consistency (flags)
4663 int flags;
4664{
4665 int acc_count = 0;
4666 tree cl = NULL_TREE;
4667
e0fc4118
TT
4668 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4669 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4670 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4671 if (acc_count > 1)
4672 parse_error_context
e0fc4118
TT
4673 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4674
4675 acc_count = 0;
4676 cl = NULL_TREE;
14d075d8
TT
4677 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4678 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4679 if (acc_count > 1)
4680 parse_error_context (cl,
4681 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4682}
4683
4684/* Check the methode header METH for abstract specifics features */
4685
4686static void
4687check_abstract_method_header (meth)
4688 tree meth;
4689{
4690 int flags = get_access_flags_from_decl (meth);
4691 /* DECL_NAME might still be a WFL node */
c877974e 4692 tree name = GET_METHOD_NAME (meth);
e04a16fb 4693
2884c41e
KG
4694 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4695 ACC_ABSTRACT, "abstract method",
4696 IDENTIFIER_POINTER (name));
4697 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4698 ACC_PUBLIC, "abstract method",
4699 IDENTIFIER_POINTER (name));
e04a16fb
AG
4700
4701 check_modifiers ("Illegal modifier `%s' for interface method",
4702 flags, INTERFACE_METHOD_MODIFIERS);
4703}
4704
4705/* Create a FUNCTION_TYPE node and start augmenting it with the
4706 declared function arguments. Arguments type that can't be resolved
4707 are left as they are, but the returned node is marked as containing
4708 incomplete types. */
4709
4710static tree
4711method_declarator (id, list)
4712 tree id, list;
4713{
4714 tree arg_types = NULL_TREE, current, node;
4715 tree meth = make_node (FUNCTION_TYPE);
4716 jdep *jdep;
e04a16fb
AG
4717
4718 patch_stage = JDEP_NO_PATCH;
c2952b01 4719
34d4df06
APB
4720 if (GET_CPC () == error_mark_node)
4721 return error_mark_node;
4722
c2952b01
APB
4723 /* If we're dealing with an inner class constructor, we hide the
4724 this$<n> decl in the name field of its parameter declaration. We
4725 also might have to hide the outer context local alias
4726 initializers. Not done when the class is a toplevel class. */
4727 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4728 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4729 {
4730 tree aliases_list, type, thisn;
4731 /* First the aliases, linked to the regular parameters */
4732 aliases_list =
4733 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4734 TREE_TYPE (GET_CPC ()),
4735 NULL_TREE, NULL);
4736 list = chainon (nreverse (aliases_list), list);
4737
4738 /* Then this$<n> */
4739 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4740 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4741 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4742 list);
4743 }
e04a16fb
AG
4744
4745 for (current = list; current; current = TREE_CHAIN (current))
4746 {
c583dd46 4747 int must_chain = 0;
e04a16fb
AG
4748 tree wfl_name = TREE_PURPOSE (current);
4749 tree type = TREE_VALUE (current);
4750 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4751 tree already, arg_node;
4752 tree type_wfl = NULL_TREE;
23a79c61 4753 tree real_type;
c583dd46
APB
4754
4755 /* Obtain a suitable type for resolution, if necessary */
4756 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4757
4758 /* Process NAME, as it may specify extra dimension(s) for it */
4759 type = build_array_from_name (type, type_wfl, name, &name);
4760 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4761
23a79c61
APB
4762 real_type = GET_REAL_TYPE (type);
4763 if (TREE_CODE (real_type) == RECORD_TYPE)
4764 {
4765 real_type = promote_type (real_type);
4766 if (TREE_CODE (type) == TREE_LIST)
4767 TREE_PURPOSE (type) = real_type;
4768 }
5e942c50 4769
e04a16fb
AG
4770 /* Check redefinition */
4771 for (already = arg_types; already; already = TREE_CHAIN (already))
4772 if (TREE_PURPOSE (already) == name)
4773 {
781b0558
KG
4774 parse_error_context
4775 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4776 IDENTIFIER_POINTER (name),
e04a16fb
AG
4777 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4778 break;
4779 }
4780
4781 /* If we've an incomplete argument type, we know there is a location
4782 to patch when the type get resolved, later. */
4783 jdep = NULL;
c583dd46 4784 if (must_chain)
e04a16fb 4785 {
c583dd46
APB
4786 patch_stage = JDEP_METHOD;
4787 type = register_incomplete_type (patch_stage,
4788 type_wfl, wfl_name, type);
4789 jdep = CLASSD_LAST (ctxp->classd_list);
4790 JDEP_MISC (jdep) = id;
e04a16fb 4791 }
c583dd46 4792
c2952b01 4793 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4794 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4795 /* Remeber arguments declared final. */
4796 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4797
e04a16fb
AG
4798 if (jdep)
4799 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4800 TREE_CHAIN (arg_node) = arg_types;
4801 arg_types = arg_node;
4802 }
de4c7b02 4803 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4804 node = build_tree_list (id, meth);
4805 return node;
4806}
4807
4808static int
4809unresolved_type_p (wfl, returned)
4810 tree wfl;
4811 tree *returned;
4812
4813{
4814 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4815 {
e04a16fb 4816 if (returned)
165f37bc
APB
4817 {
4818 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4819 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4820 *returned = TREE_TYPE (decl);
4821 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4822 *returned = TREE_TYPE (GET_CPC ());
4823 else
4824 *returned = NULL_TREE;
4825 }
e04a16fb
AG
4826 return 1;
4827 }
4828 if (returned)
4829 *returned = wfl;
4830 return 0;
4831}
4832
4833/* From NAME, build a qualified identifier node using the
4834 qualification from the current package definition. */
4835
4836static tree
98a52c2c 4837parser_qualified_classname (name)
e04a16fb
AG
4838 tree name;
4839{
c2952b01
APB
4840 tree nested_class_name;
4841
98a52c2c 4842 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4843 return nested_class_name;
4844
e04a16fb 4845 if (ctxp->package)
c2952b01 4846 return merge_qualified_name (ctxp->package, name);
e04a16fb 4847 else
c2952b01 4848 return name;
e04a16fb
AG
4849}
4850
4851/* Called once the type a interface extends is resolved. Returns 0 if
4852 everything is OK. */
4853
4854static int
4855parser_check_super_interface (super_decl, this_decl, this_wfl)
4856 tree super_decl, this_decl, this_wfl;
4857{
4858 tree super_type = TREE_TYPE (super_decl);
4859
4860 /* Has to be an interface */
c2952b01 4861 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4862 {
4863 parse_error_context
4864 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4865 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4866 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4867 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4868 "interface" : "class"),
4869 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4870 return 1;
4871 }
4872
4873 /* Check scope: same package OK, other package: OK if public */
4874 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4875 return 1;
4876
4877 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4878 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4879 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4880 return 0;
4881}
4882
4883/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4884 0 if everthing is OK. */
4885
4886static int
4887parser_check_super (super_decl, this_decl, wfl)
4888 tree super_decl, this_decl, wfl;
4889{
e04a16fb
AG
4890 tree super_type = TREE_TYPE (super_decl);
4891
4892 /* SUPER should be a CLASS (neither an array nor an interface) */
4893 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4894 {
4895 parse_error_context
4896 (wfl, "Class `%s' can't subclass %s `%s'",
4897 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4898 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4899 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4900 return 1;
4901 }
4902
4903 if (CLASS_FINAL (TYPE_NAME (super_type)))
4904 {
4905 parse_error_context (wfl, "Can't subclass final classes: %s",
4906 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4907 return 1;
4908 }
4909
4910 /* Check scope: same package OK, other package: OK if public */
4911 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4912 return 1;
4913
4914 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4915 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4916 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4917 return 0;
4918}
4919
4920/* Create a new dependency list and link it (in a LIFO manner) to the
4921 CTXP list of type dependency list. */
4922
4923static void
4924create_jdep_list (ctxp)
4925 struct parser_ctxt *ctxp;
4926{
23a79c61 4927 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4928 new->first = new->last = NULL;
4929 new->next = ctxp->classd_list;
4930 ctxp->classd_list = new;
4931}
4932
4933static jdeplist *
4934reverse_jdep_list (ctxp)
4935 struct parser_ctxt *ctxp;
4936{
4937 register jdeplist *prev = NULL, *current, *next;
4938 for (current = ctxp->classd_list; current; current = next)
4939 {
4940 next = current->next;
4941 current->next = prev;
4942 prev = current;
4943 }
4944 return prev;
4945}
4946
23a79c61
APB
4947/* Create a fake pointer based on the ID stored in
4948 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4949 registered again. */
e04a16fb
AG
4950
4951static tree
23a79c61
APB
4952obtain_incomplete_type (type_name)
4953 tree type_name;
e04a16fb 4954{
23a79c61
APB
4955 tree ptr, name;
4956
4957 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4958 name = EXPR_WFL_NODE (type_name);
4959 else if (INCOMPLETE_TYPE_P (type_name))
4960 name = TYPE_NAME (type_name);
4961 else
4962 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4963
4964 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4965 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4966 break;
4967
4968 if (!ptr)
4969 {
78d21f92
PB
4970 BUILD_PTR_FROM_NAME (ptr, name);
4971 layout_type (ptr);
e04a16fb
AG
4972 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4973 ctxp->incomplete_class = ptr;
4974 }
4975
4976 return ptr;
4977}
4978
4979/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4980 non NULL instead of computing a new fake type based on WFL. The new
4981 dependency is inserted in the current type dependency list, in FIFO
4982 manner. */
4983
4984static tree
4985register_incomplete_type (kind, wfl, decl, ptr)
4986 int kind;
4987 tree wfl, decl, ptr;
4988{
23a79c61 4989 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4990
e04a16fb
AG
4991 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4992 ptr = obtain_incomplete_type (wfl);
4993
4994 JDEP_KIND (new) = kind;
4995 JDEP_DECL (new) = decl;
4996 JDEP_SOLV (new) = ptr;
4997 JDEP_WFL (new) = wfl;
4998 JDEP_CHAIN (new) = NULL;
4999 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5000 /* For some dependencies, set the enclosing class of the current
5001 class to be the enclosing context */
8ac1de05
APB
5002 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5003 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5004 && GET_ENCLOSING_CPC ())
5005 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5006 else
324ed8fd 5007 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5008 JDEP_GET_PATCH (new) = (tree *)NULL;
5009
5010 JDEP_INSERT (ctxp->classd_list, new);
5011
5012 return ptr;
5013}
5014
5015void
5016java_check_circular_reference ()
5017{
5018 tree current;
5019 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5020 {
5021 tree type = TREE_TYPE (current);
e920ebc9 5022 if (CLASS_INTERFACE (current))
e04a16fb
AG
5023 {
5024 /* Check all interfaces this class extends */
5025 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5026 int n, i;
5027
5028 if (!basetype_vec)
5029 return;
5030 n = TREE_VEC_LENGTH (basetype_vec);
5031 for (i = 0; i < n; i++)
5032 {
5033 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5034 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5035 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5036 parse_error_context (lookup_cl (current),
5037 "Cyclic interface inheritance");
5038 }
5039 }
5040 else
5041 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5042 parse_error_context (lookup_cl (current),
c2952b01
APB
5043 "Cyclic class inheritance%s",
5044 (cyclic_inheritance_report ?
5045 cyclic_inheritance_report : ""));
5046 }
5047}
5048
5049/* Augment the parameter list PARM with parameters crafted to
5050 initialize outer context locals aliases. Through ARTIFICIAL, a
5051 count is kept of the number of crafted parameters. MODE governs
5052 what eventually gets created: something suitable for a function
5053 creation or a function invocation, either the constructor or
c00f0fb2 5054 finit$. */
c2952b01
APB
5055
5056static tree
5057build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5058 int mode;
5059 tree class_type, parm;
5060 int *artificial;
5061{
5062 tree field;
da632f2c
APB
5063 tree additional_parms = NULL_TREE;
5064
c2952b01
APB
5065 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5066 if (FIELD_LOCAL_ALIAS (field))
5067 {
63ad61ed 5068 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5069 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5070 tree mangled_id;
c2952b01
APB
5071
5072 switch (mode)
5073 {
5074 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5075 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5076 &buffer [4]);
5077 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5078 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5079 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5080 else
5081 value = TREE_TYPE (field);
5082 break;
5083
5084 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5085 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5086 &buffer [4]);
c2952b01
APB
5087 value = TREE_TYPE (field);
5088 break;
5089
5090 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5091 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5092 &buffer [4]);
c2952b01
APB
5093 /* Now, this is wrong. purpose should always be the NAME
5094 of something and value its matching value (decl, type,
5095 etc...) FIXME -- but there is a lot to fix. */
5096
5097 /* When invoked for this kind of operation, we already
5098 know whether a field is used or not. */
5099 purpose = TREE_TYPE (field);
1f8f4a0b 5100 value = build_wfl_node (mangled_id);
c2952b01
APB
5101 break;
5102
5103 case AIPL_FUNCTION_CTOR_INVOCATION:
5104 /* There are two case: the constructor invokation happends
5105 outside the local inner, in which case, locales from the outer
5106 context are directly used.
5107
5108 Otherwise, we fold to using the alias directly. */
5109 if (class_type == current_class)
5110 value = field;
5111 else
5112 {
5113 name = get_identifier (&buffer[4]);
5114 value = IDENTIFIER_LOCAL_VALUE (name);
5115 }
5116 break;
5117 }
da632f2c 5118 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5119 if (artificial)
5120 *artificial +=1;
5121 }
da632f2c
APB
5122 if (additional_parms)
5123 {
5124 if (ANONYMOUS_CLASS_P (class_type)
5125 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5126 additional_parms = nreverse (additional_parms);
5127 parm = chainon (additional_parms, parm);
5128 }
5129
5130 return parm;
c2952b01
APB
5131}
5132
5133/* Craft a constructor for CLASS_DECL -- what we should do when none
5134 where found. ARGS is non NULL when a special signature must be
5135 enforced. This is the case for anonymous classes. */
5136
5137static void
5138craft_constructor (class_decl, args)
5139 tree class_decl, args;
5140{
5141 tree class_type = TREE_TYPE (class_decl);
5142 tree parm = NULL_TREE;
5143 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5144 ACC_PUBLIC : 0);
5145 int i = 0, artificial = 0;
5146 tree decl, ctor_name;
5147 char buffer [80];
5148
c2952b01
APB
5149 /* The constructor name is <init> unless we're dealing with an
5150 anonymous class, in which case the name will be fixed after having
5151 be expanded. */
5152 if (ANONYMOUS_CLASS_P (class_type))
5153 ctor_name = DECL_NAME (class_decl);
5154 else
5155 ctor_name = init_identifier_node;
5156
5157 /* If we're dealing with an inner class constructor, we hide the
5158 this$<n> decl in the name field of its parameter declaration. */
5159 if (PURE_INNER_CLASS_TYPE_P (class_type))
5160 {
5161 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5162 parm = tree_cons (build_current_thisn (class_type),
5163 build_pointer_type (type), parm);
5164
5165 /* Some more arguments to be hidden here. The values of the local
5166 variables of the outer context that the inner class needs to see. */
5167 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5168 class_type, parm,
5169 &artificial);
5170 }
5171
5172 /* Then if there are any args to be enforced, enforce them now */
5173 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5174 {
5175 sprintf (buffer, "parm%d", i++);
5176 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5177 }
c2952b01
APB
5178
5179 CRAFTED_PARAM_LIST_FIXUP (parm);
5180 decl = create_artificial_method (class_type, flags, void_type_node,
5181 ctor_name, parm);
5182 fix_method_argument_names (parm, decl);
5183 /* Now, mark the artificial parameters. */
5184 DECL_FUNCTION_NAP (decl) = artificial;
c2952b01 5185 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5186}
5187
c2952b01 5188
e920ebc9
APB
5189/* Fix the constructors. This will be called right after circular
5190 references have been checked. It is necessary to fix constructors
5191 early even if no code generation will take place for that class:
5192 some generated constructor might be required by the class whose
5193 compilation triggered this one to be simply loaded. */
5194
5195void
5196java_fix_constructors ()
5197{
5198 tree current;
5199
5200 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5201 {
e920ebc9
APB
5202 tree class_type = TREE_TYPE (current);
5203 int saw_ctor = 0;
c2952b01
APB
5204 tree decl;
5205
5206 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5207 continue;
e920ebc9
APB
5208
5209 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5210 {
5211 if (DECL_CONSTRUCTOR_P (decl))
5212 {
5213 fix_constructors (decl);
5214 saw_ctor = 1;
5215 }
5216 }
5217
c2952b01
APB
5218 /* Anonymous class constructor can't be generated that early. */
5219 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5220 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5221 }
5222}
5223
23a79c61
APB
5224/* safe_layout_class just makes sure that we can load a class without
5225 disrupting the current_class, input_file, lineno, etc, information
5226 about the class processed currently. */
5227
e04a16fb
AG
5228void
5229safe_layout_class (class)
5230 tree class;
5231{
5232 tree save_current_class = current_class;
3b304f5b 5233 const char *save_input_filename = input_filename;
e04a16fb 5234 int save_lineno = lineno;
5e942c50 5235
e04a16fb 5236 layout_class (class);
5e942c50 5237
e04a16fb
AG
5238 current_class = save_current_class;
5239 input_filename = save_input_filename;
5240 lineno = save_lineno;
5241 CLASS_LOADED_P (class) = 1;
5242}
5243
5244static tree
5245jdep_resolve_class (dep)
5246 jdep *dep;
5247{
5248 tree decl;
5249
23a79c61
APB
5250 if (JDEP_RESOLVED_P (dep))
5251 decl = JDEP_RESOLVED_DECL (dep);
5252 else
e04a16fb 5253 {
c2952b01 5254 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5255 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5256 JDEP_RESOLVED (dep, decl);
5257 }
23a79c61 5258
e04a16fb 5259 if (!decl)
23a79c61 5260 complete_class_report_errors (dep);
1e12ab9b 5261 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5262 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5263 return decl;
5264}
5265
5266/* Complete unsatisfied class declaration and their dependencies */
5267
5268void
5269java_complete_class ()
5270{
e04a16fb
AG
5271 tree cclass;
5272 jdeplist *cclassd;
5273 int error_found;
b67d701b 5274 tree type;
e04a16fb 5275
9a7ab4b3 5276 /* Process imports */
e04a16fb 5277 process_imports ();
e04a16fb
AG
5278
5279 /* Rever things so we have the right order */
5280 ctxp->class_list = nreverse (ctxp->class_list);
5281 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5282
e04a16fb
AG
5283 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5284 cclass && cclassd;
5285 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5286 {
5287 jdep *dep;
5288 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5289 {
5290 tree decl;
e04a16fb
AG
5291 if (!(decl = jdep_resolve_class (dep)))
5292 continue;
5293
5294 /* Now it's time to patch */
5295 switch (JDEP_KIND (dep))
5296 {
5297 case JDEP_SUPER:
5298 /* Simply patch super */
5299 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5300 continue;
5301 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5302 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5303 break;
5304
5305 case JDEP_FIELD:
5306 {
5307 /* We do part of the job done in add_field */
5308 tree field_decl = JDEP_DECL (dep);
5309 tree field_type = TREE_TYPE (decl);
e04a16fb 5310 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5311 field_type = promote_type (field_type);
e04a16fb 5312 TREE_TYPE (field_decl) = field_type;
5e942c50 5313 DECL_ALIGN (field_decl) = 0;
11cf4d18 5314 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5315 layout_decl (field_decl, 0);
e04a16fb
AG
5316 SOURCE_FRONTEND_DEBUG
5317 (("Completed field/var decl `%s' with `%s'",
5318 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5319 IDENTIFIER_POINTER (DECL_NAME (decl))));
5320 break;
5321 }
5322 case JDEP_METHOD: /* We start patching a method */
5323 case JDEP_METHOD_RETURN:
5324 error_found = 0;
5325 while (1)
5326 {
5327 if (decl)
5328 {
b67d701b
PB
5329 type = TREE_TYPE(decl);
5330 if (TREE_CODE (type) == RECORD_TYPE)
5331 type = promote_type (type);
e04a16fb
AG
5332 JDEP_APPLY_PATCH (dep, type);
5333 SOURCE_FRONTEND_DEBUG
5334 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5335 "Completing fct `%s' with ret type `%s'":
5336 "Completing arg `%s' with type `%s'"),
5337 IDENTIFIER_POINTER (EXPR_WFL_NODE
5338 (JDEP_DECL_WFL (dep))),
5339 IDENTIFIER_POINTER (DECL_NAME (decl))));
5340 }
5341 else
5342 error_found = 1;
5343 dep = JDEP_CHAIN (dep);
5344 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5345 break;
5346 else
5347 decl = jdep_resolve_class (dep);
5348 }
5349 if (!error_found)
5350 {
5351 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5352 /* Recompute and reset the signature, check first that
5353 all types are now defined. If they're not,
5354 dont build the signature. */
5355 if (check_method_types_complete (mdecl))
5356 {
5357 signature = build_java_signature (TREE_TYPE (mdecl));
5358 set_java_signature (TREE_TYPE (mdecl), signature);
5359 }
e04a16fb
AG
5360 }
5361 else
5362 continue;
5363 break;
5364
5365 case JDEP_INTERFACE:
5366 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5367 JDEP_WFL (dep)))
5368 continue;
5369 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5370 break;
5371
b67d701b 5372 case JDEP_PARM:
e04a16fb 5373 case JDEP_VARIABLE:
b67d701b
PB
5374 type = TREE_TYPE(decl);
5375 if (TREE_CODE (type) == RECORD_TYPE)
5376 type = promote_type (type);
5377 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5378 break;
5379
5380 case JDEP_TYPE:
5381 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5382 SOURCE_FRONTEND_DEBUG
5383 (("Completing a random type dependency on a '%s' node",
5384 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5385 break;
5386
b9f7e36c 5387 case JDEP_EXCEPTION:
c877974e
APB
5388 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5389 SOURCE_FRONTEND_DEBUG
5390 (("Completing `%s' `throws' argument node",
5391 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5392 break;
5393
c2952b01
APB
5394 case JDEP_ANONYMOUS:
5395 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5396 break;
5397
e04a16fb 5398 default:
0a2138e2
APB
5399 fatal ("Can't handle patch code %d - java_complete_class",
5400 JDEP_KIND (dep));
e04a16fb
AG
5401 }
5402 }
5403 }
e04a16fb
AG
5404 return;
5405}
5406
5407/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5408 array. */
5409
5410static tree
c2952b01
APB
5411resolve_class (enclosing, class_type, decl, cl)
5412 tree enclosing, class_type, decl, cl;
e04a16fb 5413{
49f48c71
KG
5414 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5415 const char *base = name;
78d21f92
PB
5416 tree resolved_type = TREE_TYPE (class_type);
5417 tree resolved_type_decl;
e04a16fb 5418
78d21f92
PB
5419 if (resolved_type != NULL_TREE)
5420 {
5421 tree resolved_type_decl = TYPE_NAME (resolved_type);
5422 if (resolved_type_decl == NULL_TREE
5423 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5424 {
5425 resolved_type_decl = build_decl (TYPE_DECL,
5426 TYPE_NAME (class_type),
5427 resolved_type);
5428 }
5429 return resolved_type_decl;
5430 }
5431
e04a16fb
AG
5432 /* 1- Check to see if we have an array. If true, find what we really
5433 want to resolve */
5434 while (name[0] == '[')
5435 name++;
5436 if (base != name)
34d4df06
APB
5437 {
5438 TYPE_NAME (class_type) = get_identifier (name);
5439 WFL_STRIP_BRACKET (cl, cl);
5440 }
e04a16fb
AG
5441
5442 /* 2- Resolve the bare type */
c2952b01
APB
5443 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5444 decl, cl)))
e04a16fb
AG
5445 return NULL_TREE;
5446 resolved_type = TREE_TYPE (resolved_type_decl);
5447
5448 /* 3- If we have and array, reconstruct the array down to its nesting */
5449 if (base != name)
5450 {
5451 while (base != name)
5452 {
5453 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5454 resolved_type = promote_type (resolved_type);
5455 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5456 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5457 name--;
5458 }
5459 /* Build a fake decl for this, since this is what is expected to
5460 be returned. */
5461 resolved_type_decl =
5462 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5463 /* Figure how those two things are important for error report. FIXME */
5464 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5465 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5466 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5467 }
78d21f92 5468 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5469 return resolved_type_decl;
5470}
5471
5472/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5473 are used to report error messages. */
5474
78d21f92 5475tree
c2952b01
APB
5476do_resolve_class (enclosing, class_type, decl, cl)
5477 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5478{
5479 tree new_class_decl;
e04a16fb
AG
5480
5481 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5482 it is changed by find_in_imports{_on_demand} and (but it doesn't
5483 really matter) qualify_and_find */
e04a16fb 5484
c2952b01
APB
5485 /* 0- Search in the current class as an inner class */
5486
5487 /* Maybe some code here should be added to load the class or
5488 something, at least if the class isn't an inner class and ended
5489 being loaded from class file. FIXME. */
a40d21da
APB
5490 while (enclosing)
5491 {
5492 tree name;
e1d565ab 5493 tree intermediate;
a40d21da
APB
5494
5495 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5496 return new_class_decl;
8ac1de05 5497
e1d565ab 5498 intermediate = enclosing;
0c2b8145 5499 /* Explore enclosing contexts. */
e1d565ab 5500 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5501 {
e1d565ab
BM
5502 intermediate = DECL_CONTEXT (intermediate);
5503 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5504 class_type, cl)))
5505 return new_class_decl;
5506 }
5507
a40d21da
APB
5508 /* Now go to the upper classes, bail out if necessary. */
5509 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5510 if (!enclosing || enclosing == object_type_node)
5511 break;
5512
5513 if (TREE_CODE (enclosing) == RECORD_TYPE)
5514 {
5515 enclosing = TYPE_NAME (enclosing);
5516 continue;
5517 }
5518
5519 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5520 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5521 else
5522 name = enclosing;
5523 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5524 }
c2952b01 5525
9a7ab4b3
APB
5526 /* 1- Check for the type in single imports. This will change
5527 TYPE_NAME() if something relevant is found */
5528 find_in_imports (class_type);
e04a16fb 5529
9a7ab4b3 5530 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5531 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5532 {
5533 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5534 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5535 load_class (TYPE_NAME (class_type), 0);
5536 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5537 }
5538
9a7ab4b3
APB
5539 /* 3- Search according to the current package definition */
5540 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5541 {
5542 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5543 TYPE_NAME (class_type))))
5544 return new_class_decl;
5545 }
5546
5547 /* 4- Check the import on demands. Don't allow bar.baz to be
5548 imported from foo.* */
5549 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5550 if (find_in_imports_on_demand (class_type))
5551 return NULL_TREE;
5552
5553 /* If found in find_in_imports_on_demant, the type has already been
5554 loaded. */
5555 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5556 return new_class_decl;
5557
5558 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5559 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5560 {
ee07f4f4 5561 tree package;
d6baf6f5
APB
5562
5563 /* If there is a current package (ctxp->package), it's the first
5564 element of package_list and we can skip it. */
5565 for (package = (ctxp->package ?
5566 TREE_CHAIN (package_list) : package_list);
5567 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5568 if ((new_class_decl = qualify_and_find (class_type,
5569 TREE_PURPOSE (package),
5570 TYPE_NAME (class_type))))
5571 return new_class_decl;
5572 }
5573
5574 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5575 load_class (TYPE_NAME (class_type), 0);
5576 if (check_pkg_class_access (TYPE_NAME (class_type),
5577 (cl ? cl : lookup_cl (decl))))
5578 return NULL_TREE;
5579
9a7ab4b3 5580 /* 6- Last call for a resolution */
e04a16fb
AG
5581 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5582}
5583
9a7ab4b3
APB
5584static tree
5585qualify_and_find (class_type, package, name)
5586 tree class_type, package, name;
5587{
5588 tree new_qualified = merge_qualified_name (package, name);
5589 tree new_class_decl;
5590
5591 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5592 load_class (new_qualified, 0);
5593 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5594 {
5595 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5596 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5597 load_class (new_qualified, 0);
5598 TYPE_NAME (class_type) = new_qualified;
5599 return IDENTIFIER_CLASS_VALUE (new_qualified);
5600 }
5601 return NULL_TREE;
5602}
5603
e04a16fb 5604/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5605 parsed class). Return a decl node. This function is meant to be
5606 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5607
5608static tree
c877974e
APB
5609resolve_and_layout (something, cl)
5610 tree something;
e04a16fb
AG
5611 tree cl;
5612{
34d4df06 5613 tree decl, decl_type;
c877974e 5614
23a79c61
APB
5615 /* Don't do that on the current class */
5616 if (something == current_class)
5617 return TYPE_NAME (current_class);
c877974e 5618
23a79c61 5619 /* Don't do anything for void and other primitive types */
c877974e
APB
5620 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5621 return NULL_TREE;
5622
23a79c61
APB
5623 /* Pointer types can be reall pointer types or fake pointers. When
5624 finding a real pointer, recheck for primitive types */
5625 if (TREE_CODE (something) == POINTER_TYPE)
5626 {
5627 if (TREE_TYPE (something))
5628 {
5629 something = TREE_TYPE (something);
5630 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5631 return NULL_TREE;
5632 }
5633 else
5634 something = TYPE_NAME (something);
5635 }
5636
5637 /* Don't do anything for arrays of primitive types */
5638 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5639 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5640 return NULL_TREE;
5641
c2952b01
APB
5642 /* Something might be a WFL */
5643 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5644 something = EXPR_WFL_NODE (something);
5645
5646 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5647 TYPE_DECL or a real TYPE */
5648 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5649 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5650 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5651
23a79c61
APB
5652 if (!(decl = resolve_no_layout (something, cl)))
5653 return NULL_TREE;
5654
5655 /* Resolve and layout if necessary */
34d4df06
APB
5656 decl_type = TREE_TYPE (decl);
5657 layout_class_methods (decl_type);
5658 /* Check methods */
5659 if (CLASS_FROM_SOURCE_P (decl_type))
5660 java_check_methods (decl);
5661 /* Layout the type if necessary */
5662 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5663 safe_layout_class (decl_type);
23a79c61 5664
e04a16fb
AG
5665 return decl;
5666}
5667
5668/* Resolve a class, returns its decl but doesn't perform any
5669 layout. The current parsing context is saved and restored */
5670
5671static tree
5672resolve_no_layout (name, cl)
5673 tree name, cl;
5674{
5675 tree ptr, decl;
5676 BUILD_PTR_FROM_NAME (ptr, name);
5677 java_parser_context_save_global ();
c2952b01 5678 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5679 java_parser_context_restore_global ();
5680
5681 return decl;
5682}
5683
23a79c61
APB
5684/* Called when reporting errors. Skip leader '[' in a complex array
5685 type description that failed to be resolved. */
e04a16fb 5686
49f48c71 5687static const char *
e04a16fb 5688purify_type_name (name)
49f48c71 5689 const char *name;
e04a16fb
AG
5690{
5691 while (*name && *name == '[')
5692 name++;
5693 return name;
5694}
5695
5696/* The type CURRENT refers to can't be found. We print error messages. */
5697
5698static void
5699complete_class_report_errors (dep)
5700 jdep *dep;
5701{
49f48c71 5702 const char *name;
23a79c61
APB
5703
5704 if (!JDEP_WFL (dep))
5705 return;
5706
5707 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5708 switch (JDEP_KIND (dep))
5709 {
5710 case JDEP_SUPER:
5711 parse_error_context
5712 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5713 purify_type_name (name),
e04a16fb
AG
5714 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5715 break;
5716 case JDEP_FIELD:
5717 parse_error_context
5718 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5719 purify_type_name (name),
e04a16fb
AG
5720 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5721 break;
5722 case JDEP_METHOD: /* Covers arguments */
5723 parse_error_context
781b0558 5724 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5725 purify_type_name (name),
e04a16fb
AG
5726 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5727 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5728 break;
5729 case JDEP_METHOD_RETURN: /* Covers return type */
5730 parse_error_context
781b0558 5731 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5732 purify_type_name (name),
e04a16fb
AG
5733 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5734 break;
5735 case JDEP_INTERFACE:
5736 parse_error_context
5737 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5738 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5739 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5740 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5741 break;
5742 case JDEP_VARIABLE:
5743 parse_error_context
781b0558 5744 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5745 purify_type_name (IDENTIFIER_POINTER
5746 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5747 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5748 break;
b9f7e36c
APB
5749 case JDEP_EXCEPTION: /* As specified by `throws' */
5750 parse_error_context
5751 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5753 break;
0a2138e2
APB
5754 default:
5755 /* Fix for -Wall. Just break doing nothing. The error will be
5756 caught later */
5757 break;
e04a16fb
AG
5758 }
5759}
5760
22eed1e6
APB
5761/* Return a static string containing the DECL prototype string. If
5762 DECL is a constructor, use the class name instead of the form
5763 <init> */
5764
49f48c71 5765static const char *
22eed1e6
APB
5766get_printable_method_name (decl)
5767 tree decl;
5768{
49f48c71 5769 const char *to_return;
9ee9b555 5770 tree name = NULL_TREE;
22eed1e6
APB
5771
5772 if (DECL_CONSTRUCTOR_P (decl))
5773 {
5774 name = DECL_NAME (decl);
5e942c50 5775 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5776 }
5777
5778 to_return = lang_printable_name (decl, 0);
5779 if (DECL_CONSTRUCTOR_P (decl))
5780 DECL_NAME (decl) = name;
5781
5782 return to_return;
5783}
5784
5e942c50
APB
5785/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5786 nevertheless needs to be verfied, 1 otherwise. */
5787
5788static int
5789reset_method_name (method)
5790 tree method;
5791{
c2952b01 5792 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5793 {
5794 /* NAME is just the plain name when Object is being defined */
5795 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5796 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5797 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5798 return 0;
5799 }
5800 else
5801 return 1;
5802}
5803
c877974e
APB
5804/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5805
5806tree
5807java_get_real_method_name (method_decl)
5808 tree method_decl;
5809{
5810 tree method_name = DECL_NAME (method_decl);
5811 if (DECL_CONSTRUCTOR_P (method_decl))
5812 return init_identifier_node;
82371d41
APB
5813
5814 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5815 and still can be a constructor. FIXME */
5816
23a79c61
APB
5817 /* Don't confuse method only bearing the name of their class as
5818 constructors */
82371d41
APB
5819 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5820 && ctxp
c2952b01 5821 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5822 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5823 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5824 return init_identifier_node;
5825 else
5826 return EXPR_WFL_NODE (method_name);
5827}
5828
22eed1e6
APB
5829/* Track method being redefined inside the same class. As a side
5830 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5831 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5832
e04a16fb
AG
5833static int
5834check_method_redefinition (class, method)
5835 tree class, method;
5836{
5837 tree redef, name;
5838 tree cl = DECL_NAME (method);
c3f2a476 5839 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5840 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5841 fixed and checked */
5e942c50
APB
5842
5843 /* Reset the method name before running the check. If it returns 1,
5844 the method doesn't need to be verified with respect to method
5845 redeclaration and we return 0 */
5846 if (reset_method_name (method))
e04a16fb 5847 return 0;
5e942c50
APB
5848
5849 name = DECL_NAME (method);
e04a16fb
AG
5850 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5851 {
c3f2a476 5852 if (redef == method)
e04a16fb 5853 break;
c3f2a476
APB
5854 if (DECL_NAME (redef) == name
5855 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5856 {
22eed1e6
APB
5857 parse_error_context
5858 (cl, "Duplicate %s declaration `%s'",
5859 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5860 get_printable_method_name (redef));
e04a16fb
AG
5861 return 1;
5862 }
5863 }
5864 return 0;
5865}
5866
1175b9b4
TT
5867/* Return 1 if check went ok, 0 otherwise. */
5868static int
d77613be
APB
5869check_abstract_method_definitions (do_interface, class_decl, type)
5870 int do_interface;
5871 tree class_decl, type;
5872{
5873 tree class = TREE_TYPE (class_decl);
5874 tree method, end_type;
1175b9b4 5875 int ok = 1;
d77613be
APB
5876
5877 end_type = (do_interface ? object_type_node : type);
5878 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5879 {
5880 tree other_super, other_method, method_sig, method_name;
5881 int found = 0;
165f37bc 5882 int end_type_reached = 0;
d77613be
APB
5883
5884 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5885 continue;
5886
5887 /* Now verify that somewhere in between TYPE and CLASS,
5888 abstract method METHOD gets a non abstract definition
5889 that is inherited by CLASS. */
5890
5891 method_sig = build_java_signature (TREE_TYPE (method));
5892 method_name = DECL_NAME (method);
5893 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5894 method_name = EXPR_WFL_NODE (method_name);
5895
165f37bc
APB
5896 other_super = class;
5897 do {
5898 if (other_super == end_type)
5899 end_type_reached = 1;
5900
5901 /* Method search */
5902 for (other_method = TYPE_METHODS (other_super); other_method;
5903 other_method = TREE_CHAIN (other_method))
5904 {
5905 tree s = build_java_signature (TREE_TYPE (other_method));
5906 tree other_name = DECL_NAME (other_method);
5907
5908 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5909 other_name = EXPR_WFL_NODE (other_name);
5910 if (!DECL_CLINIT_P (other_method)
5911 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5912 && method_name == other_name
5913 && method_sig == s
5914 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5915 {
5916 found = 1;
5917 break;
5918 }
5919 }
5920 other_super = CLASSTYPE_SUPER (other_super);
5921 } while (!end_type_reached);
5922
d77613be
APB
5923 /* Report that abstract METHOD didn't find an implementation
5924 that CLASS can use. */
5925 if (!found)
5926 {
c2e3db92 5927 char *t = xstrdup (lang_printable_name
d77613be
APB
5928 (TREE_TYPE (TREE_TYPE (method)), 0));
5929 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5930 tree saved_wfl = NULL_TREE;
5931
5932 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5933 {
5934 saved_wfl = DECL_NAME (method);
5935 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5936 }
5937
5938 parse_error_context
5939 (lookup_cl (class_decl),
781b0558 5940 "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
5941 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5942 t, lang_printable_name (method, 0),
5943 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5944 "interface" : "class"),
5945 IDENTIFIER_POINTER (ccn),
5946 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5947 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5948 ok = 0;
d77613be 5949 free (t);
1175b9b4 5950
d77613be
APB
5951 if (saved_wfl)
5952 DECL_NAME (method) = saved_wfl;
5953 }
5954 }
1175b9b4
TT
5955
5956 if (ok && do_interface)
5957 {
5958 /* Check for implemented interfaces. */
5959 int i;
5960 tree vector = TYPE_BINFO_BASETYPES (type);
5961 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5962 {
5963 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5964 ok = check_abstract_method_definitions (1, class_decl, super);
5965 }
5966 }
5967
5968 return ok;
d77613be
APB
5969}
5970
614eaae0 5971/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5972 methods. */
5973
5974static void
5975java_check_abstract_method_definitions (class_decl)
5976 tree class_decl;
5977{
5978 tree class = TREE_TYPE (class_decl);
5979 tree super, vector;
5980 int i;
5981
5982 if (CLASS_ABSTRACT (class_decl))
5983 return;
5984
5985 /* Check for inherited types */
165f37bc
APB
5986 super = class;
5987 do {
5988 super = CLASSTYPE_SUPER (super);
5989 check_abstract_method_definitions (0, class_decl, super);
5990 } while (super != object_type_node);
d77613be
APB
5991
5992 /* Check for implemented interfaces. */
5993 vector = TYPE_BINFO_BASETYPES (class);
5994 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5995 {
5996 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5997 check_abstract_method_definitions (1, class_decl, super);
5998 }
5999}
6000
165f37bc
APB
6001/* Check all the types method DECL uses and return 1 if all of them
6002 are now complete, 0 otherwise. This is used to check whether its
6003 safe to build a method signature or not. */
6004
6005static int
6006check_method_types_complete (decl)
6007 tree decl;
6008{
6009 tree type = TREE_TYPE (decl);
6010 tree args;
6011
6012 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6013 return 0;
6014
6015 args = TYPE_ARG_TYPES (type);
6016 if (TREE_CODE (type) == METHOD_TYPE)
6017 args = TREE_CHAIN (args);
6018 for (; args != end_params_node; args = TREE_CHAIN (args))
6019 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6020 return 0;
6021
6022 return 1;
6023}
6024
34d4df06
APB
6025/* Visible interface to check methods contained in CLASS_DECL */
6026
6027void
6028java_check_methods (class_decl)
6029 tree class_decl;
6030{
6031 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6032 return;
6033
6034 if (CLASS_INTERFACE (class_decl))
6035 java_check_abstract_methods (class_decl);
6036 else
6037 java_check_regular_methods (class_decl);
6038
6039 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6040}
6041
d77613be
APB
6042/* Check all the methods of CLASS_DECL. Methods are first completed
6043 then checked according to regular method existance rules. If no
6044 constructor for CLASS_DECL were encountered, then build its
6045 declaration. */
e04a16fb
AG
6046
6047static void
6048java_check_regular_methods (class_decl)
6049 tree class_decl;
6050{
c2952b01 6051 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6052 tree method;
6053 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6054 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6055 tree mthrows;
6056
6057 /* It is not necessary to check methods defined in java.lang.Object */
6058 if (class == object_type_node)
6059 return;
e04a16fb 6060
23a79c61
APB
6061 if (!TYPE_NVIRTUALS (class))
6062 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6063
6064 /* Should take interfaces into account. FIXME */
6065 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6066 {
5e942c50 6067 tree sig;
e04a16fb
AG
6068 tree method_wfl = DECL_NAME (method);
6069 int aflags;
6070
5e942c50
APB
6071 /* If we previously found something and its name was saved,
6072 reinstall it now */
6073 if (found && saved_found_wfl)
ba179f9f
APB
6074 {
6075 DECL_NAME (found) = saved_found_wfl;
6076 saved_found_wfl = NULL_TREE;
6077 }
5e942c50 6078
e04a16fb
AG
6079 /* Check for redefinitions */
6080 if (check_method_redefinition (class, method))
6081 continue;
6082
22eed1e6
APB
6083 /* If we see one constructor a mark so we don't generate the
6084 default one. Also skip other verifications: constructors
6085 can't be inherited hence hiden or overriden */
6086 if (DECL_CONSTRUCTOR_P (method))
6087 {
6088 saw_constructor = 1;
6089 continue;
6090 }
6091
c877974e
APB
6092 /* We verify things thrown by the method. They must inherits from
6093 java.lang.Throwable */
6094 for (mthrows = DECL_FUNCTION_THROWS (method);
6095 mthrows; mthrows = TREE_CHAIN (mthrows))
6096 {
6097 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6098 parse_error_context
781b0558 6099 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6100 IDENTIFIER_POINTER
6101 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6102 }
6103
e04a16fb 6104 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6105 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6106
c2952b01
APB
6107 /* Inner class can't declare static methods */
6108 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6109 {
6110 char *t = xstrdup (lang_printable_name (class, 0));
6111 parse_error_context
6112 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6113 lang_printable_name (method, 0), t);
6114 free (t);
6115 }
6116
5e942c50 6117 /* Nothing overrides or it's a private method. */
aabd7048 6118 if (!found)
5e942c50 6119 continue;
aabd7048
PB
6120 if (METHOD_PRIVATE (found))
6121 {
6122 found = NULL_TREE;
6123 continue;
6124 }
5e942c50
APB
6125
6126 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6127 We set it temporarily for the sake of the error report. */
6128 saved_found_wfl = DECL_NAME (found);
6129 reset_method_name (found);
6130
614eaae0
APB
6131 /* If `found' is declared in an interface, make sure the
6132 modifier matches. */
6133 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6134 && clinit_identifier_node != DECL_NAME (found)
6135 && !METHOD_PUBLIC (method))
6136 {
6137 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6138 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6139 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6140 lang_printable_name (method, 0),
6141 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6142 }
6143
e04a16fb
AG
6144 /* Can't override a method with the same name and different return
6145 types. */
6146 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6147 {
614eaae0
APB
6148 char *t = xstrdup
6149 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6150 parse_error_context
7f10c2e2 6151 (method_wfl,
b9f7e36c 6152 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6153 lang_printable_name (found, 0), t,
b9f7e36c
APB
6154 IDENTIFIER_POINTER
6155 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6156 free (t);
6157 }
e04a16fb 6158
7f10c2e2
APB
6159 aflags = get_access_flags_from_decl (found);
6160 /* If the method has default, access in an other package, then
6161 issue a warning that the current method doesn't override the
6162 one that was found elsewhere. Do not issue this warning when
6163 the match was found in java.lang.Object. */
6164 if (DECL_CONTEXT (found) != object_type_node
a003f638 6165 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6166 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6167 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6168 && flag_not_overriding)
6169 {
6170 parse_warning_context
781b0558 6171 (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
6172 lang_printable_name (found, 0),
6173 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6174 IDENTIFIER_POINTER (DECL_NAME
6175 (TYPE_NAME (DECL_CONTEXT (found)))));
6176 continue;
6177 }
6178
e04a16fb
AG
6179 /* Can't override final. Can't override static. */
6180 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6181 {
6182 /* Static *can* override static */
6183 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6184 continue;
6185 parse_error_context
6186 (method_wfl,
6187 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6188 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6189 lang_printable_name (found, 0),
e04a16fb
AG
6190 (METHOD_FINAL (found) ? "final" : "static"),
6191 IDENTIFIER_POINTER
6192 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6193 continue;
6194 }
7f10c2e2 6195
e04a16fb
AG
6196 /* Static method can't override instance method. */
6197 if (METHOD_STATIC (method))
6198 {
6199 parse_error_context
6200 (method_wfl,
781b0558 6201 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6202 lang_printable_name (found, 0),
e04a16fb
AG
6203 IDENTIFIER_POINTER
6204 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6205 continue;
6206 }
5e942c50 6207
5e942c50
APB
6208 /* - Overriding/hiding public must be public
6209 - Overriding/hiding protected must be protected or public
6210 - If the overriden or hidden method has default (package)
6211 access, then the overriding or hiding method must not be
614eaae0
APB
6212 private; otherwise, a compile-time error occurs. If
6213 `found' belongs to an interface, things have been already
6214 taken care of. */
6215 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6216 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6217 || (METHOD_PROTECTED (found)
6218 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6219 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6220 && METHOD_PRIVATE (method))))
e04a16fb
AG
6221 {
6222 parse_error_context
6223 (method_wfl,
781b0558 6224 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6225 (METHOD_PUBLIC (method) ? "public" :
6226 (METHOD_PRIVATE (method) ? "private" : "protected")),
6227 IDENTIFIER_POINTER (DECL_NAME
6228 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6229 continue;
6230 }
6231
b9f7e36c
APB
6232 /* Overriding methods must have compatible `throws' clauses on checked
6233 exceptions, if any */
6234 check_throws_clauses (method, method_wfl, found);
6235
e04a16fb
AG
6236 /* Inheriting multiple methods with the same signature. FIXME */
6237 }
6238
5e942c50
APB
6239 /* Don't forget eventual pending found and saved_found_wfl. Take
6240 into account that we might have exited because we saw an
d77613be 6241 artificial method as the last entry. */
5e942c50
APB
6242
6243 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6244 DECL_NAME (found) = saved_found_wfl;
6245
23a79c61
APB
6246 if (!TYPE_NVIRTUALS (class))
6247 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6248
d77613be
APB
6249 /* Search for inherited abstract method not yet implemented in this
6250 class. */
6251 java_check_abstract_method_definitions (class_decl);
6252
22eed1e6 6253 if (!saw_constructor)
e920ebc9 6254 fatal ("No constructor found");
e04a16fb
AG
6255}
6256
b9f7e36c
APB
6257/* Return a non zero value if the `throws' clause of METHOD (if any)
6258 is incompatible with the `throws' clause of FOUND (if any). */
6259
6260static void
6261check_throws_clauses (method, method_wfl, found)
6262 tree method, method_wfl, found;
6263{
6264 tree mthrows, fthrows;
6265
c877974e
APB
6266 /* Can't check these things with class loaded from bytecode. FIXME */
6267 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6268 return;
6269
b9f7e36c
APB
6270 for (mthrows = DECL_FUNCTION_THROWS (method);
6271 mthrows; mthrows = TREE_CHAIN (mthrows))
6272 {
6273 /* We don't verify unchecked expressions */
c877974e 6274 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6275 continue;
6276 /* Checked expression must be compatible */
6277 for (fthrows = DECL_FUNCTION_THROWS (found);
6278 fthrows; fthrows = TREE_CHAIN (fthrows))
6279 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6280 break;
6281 if (!fthrows)
6282 {
6283 parse_error_context
781b0558 6284 (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 6285 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6286 lang_printable_name (found, 0),
b9f7e36c
APB
6287 IDENTIFIER_POINTER
6288 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6289 }
6290 }
6291}
6292
e04a16fb
AG
6293/* Check abstract method of interface INTERFACE */
6294
6295static void
5e942c50
APB
6296java_check_abstract_methods (interface_decl)
6297 tree interface_decl;
e04a16fb
AG
6298{
6299 int i, n;
6300 tree method, basetype_vec, found;
5e942c50 6301 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6302
6303 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6304 {
b9f7e36c 6305 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6306
6307 /* 2- Check for double definition inside the defining interface */
6308 if (check_method_redefinition (interface, method))
6309 continue;
6310
6311 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6312 the thrown exceptions (FIXME) */
e04a16fb
AG
6313 found = lookup_java_interface_method2 (interface, method);
6314 if (found)
6315 {
5e942c50
APB
6316 char *t;
6317 tree saved_found_wfl = DECL_NAME (found);
6318 reset_method_name (found);
c2e3db92 6319 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6320 parse_error_context
b9f7e36c 6321 (method_wfl,
5e942c50 6322 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6323 lang_printable_name (found, 0), t,
b9f7e36c
APB
6324 IDENTIFIER_POINTER
6325 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6326 free (t);
5e942c50 6327 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6328 continue;
e04a16fb
AG
6329 }
6330 }
6331
6332 /* 4- Inherited methods can't differ by their returned types */
6333 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6334 return;
6335 n = TREE_VEC_LENGTH (basetype_vec);
6336 for (i = 0; i < n; i++)
6337 {
6338 tree sub_interface_method, sub_interface;
6339 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6340 if (!vec_elt)
6341 continue;
6342 sub_interface = BINFO_TYPE (vec_elt);
6343 for (sub_interface_method = TYPE_METHODS (sub_interface);
6344 sub_interface_method;
6345 sub_interface_method = TREE_CHAIN (sub_interface_method))
6346 {
6347 found = lookup_java_interface_method2 (interface,
6348 sub_interface_method);
6349 if (found && (found != sub_interface_method))
5e942c50
APB
6350 {
6351 tree saved_found_wfl = DECL_NAME (found);
6352 reset_method_name (found);
6353 parse_error_context
6354 (lookup_cl (sub_interface_method),
781b0558 6355 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6356 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6357 lang_printable_name (found, 0),
6358 IDENTIFIER_POINTER
6359 (DECL_NAME (TYPE_NAME
6360 (DECL_CONTEXT (sub_interface_method)))),
6361 IDENTIFIER_POINTER
6362 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6363 DECL_NAME (found) = saved_found_wfl;
6364 }
e04a16fb
AG
6365 }
6366 }
6367}
6368
e04a16fb
AG
6369/* Lookup methods in interfaces using their name and partial
6370 signature. Return a matching method only if their types differ. */
6371
6372static tree
6373lookup_java_interface_method2 (class, method_decl)
6374 tree class, method_decl;
6375{
6376 int i, n;
6377 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6378
6379 if (!basetype_vec)
6380 return NULL_TREE;
6381
6382 n = TREE_VEC_LENGTH (basetype_vec);
6383 for (i = 0; i < n; i++)
6384 {
6385 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6386 if ((BINFO_TYPE (vec_elt) != object_type_node)
6387 && (to_return =
6388 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6389 return to_return;
6390 }
6391 for (i = 0; i < n; i++)
6392 {
6393 to_return = lookup_java_interface_method2
6394 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6395 if (to_return)
6396 return to_return;
6397 }
6398
6399 return NULL_TREE;
6400}
6401
6402/* Lookup method using their name and partial signature. Return a
6403 matching method only if their types differ. */
6404
6405static tree
6406lookup_java_method2 (clas, method_decl, do_interface)
6407 tree clas, method_decl;
6408 int do_interface;
6409{
5e942c50
APB
6410 tree method, method_signature, method_name, method_type, name;
6411
e04a16fb 6412 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6413 name = DECL_NAME (method_decl);
6414 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6415 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6416 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6417
6418 while (clas != NULL_TREE)
6419 {
6420 for (method = TYPE_METHODS (clas);
6421 method != NULL_TREE; method = TREE_CHAIN (method))
6422 {
6423 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6424 tree name = DECL_NAME (method);
6425 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6426 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6427 && method_sig == method_signature
6428 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6429 return method;
e04a16fb
AG
6430 }
6431 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6432 }
6433 return NULL_TREE;
6434}
6435
f441f671
APB
6436/* Return the line that matches DECL line number, and try its best to
6437 position the column number. Used during error reports. */
e04a16fb
AG
6438
6439static tree
6440lookup_cl (decl)
6441 tree decl;
6442{
6443 static tree cl = NULL_TREE;
f441f671 6444 char *line, *found;
e04a16fb
AG
6445
6446 if (!decl)
6447 return NULL_TREE;
6448
6449 if (cl == NULL_TREE)
19e223db
MM
6450 {
6451 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6452 ggc_add_tree_root (&cl, 1);
6453 }
e04a16fb
AG
6454
6455 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6456 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6457
dba41d30 6458 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6459 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6460
6461 found = strstr ((const char *)line,
6462 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6463 if (found)
6464 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6465
e04a16fb
AG
6466 return cl;
6467}
6468
6469/* Look for a simple name in the single-type import list */
6470
6471static tree
6472find_name_in_single_imports (name)
6473 tree name;
6474{
6475 tree node;
6476
6477 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6478 if (TREE_VALUE (node) == name)
6479 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6480
6481 return NULL_TREE;
6482}
6483
6484/* Process all single-type import. */
6485
6486static int
6487process_imports ()
6488{
6489 tree import;
6490 int error_found;
6491
6492 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6493 {
6494 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6495
6496 /* Don't load twice something already defined. */
6497 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6498 continue;
02ae6e2e
APB
6499
6500 while (1)
6501 {
6502 tree left;
6503
6504 QUALIFIED_P (to_be_found) = 1;
6505 load_class (to_be_found, 0);
6506 error_found =
6507 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6508
6509 /* We found it, we can bail out */
6510 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6511 break;
6512
6513 /* We haven't found it. Maybe we're trying to access an
6514 inner class. The only way for us to know is to try again
6515 after having dropped a qualifier. If we can't break it further,
6516 we have an error. */
6517 if (breakdown_qualified (&left, NULL, to_be_found))
6518 break;
6519
6520 to_be_found = left;
6521 }
e04a16fb
AG
6522 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6523 {
6524 parse_error_context (TREE_PURPOSE (import),
6525 "Class or interface `%s' not found in import",
6526 IDENTIFIER_POINTER (to_be_found));
6527 return 1;
6528 }
6529 if (error_found)
6530 return 1;
6531 }
6532 return 0;
6533}
6534
9a7ab4b3
APB
6535/* Possibly find and mark a class imported by a single-type import
6536 statement. */
e04a16fb 6537
9a7ab4b3 6538static void
e04a16fb
AG
6539find_in_imports (class_type)
6540 tree class_type;
6541{
6542 tree import;
6543
6544 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6545 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6546 {
6547 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6548 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6549 }
e04a16fb
AG
6550}
6551
e04a16fb 6552static int
63a212ed 6553note_possible_classname (name, len)
49f48c71 6554 const char *name;
63a212ed 6555 int len;
e04a16fb 6556{
63a212ed
PB
6557 tree node;
6558 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6559 len = len - 5;
6560 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6561 len = len - 6;
e04a16fb 6562 else
63a212ed
PB
6563 return 0;
6564 node = ident_subst (name, len, "", '/', '.', "");
6565 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6566 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6567 return 1;
e04a16fb
AG
6568}
6569
6570/* Read a import directory, gathering potential match for further type
6571 references. Indifferently reads a filesystem or a ZIP archive
6572 directory. */
6573
6574static void
6575read_import_dir (wfl)
6576 tree wfl;
6577{
63a212ed 6578 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6579 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6580 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6581 DIR *dirp = NULL;
d8fccff5 6582 JCF *saved_jcf = current_jcf;
e04a16fb 6583
63a212ed
PB
6584 int found = 0;
6585 int k;
6586 void *entry;
6587 struct buffer filename[1];
6588
6589
6590 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6591 return;
6592 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6593
6594 BUFFER_INIT (filename);
6595 buffer_grow (filename, package_length + 100);
6596
6597 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6598 {
49f48c71 6599 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6600 int entry_length = strlen (entry_name);
6601 if (jcf_path_is_zipfile (entry))
6602 {
6603 ZipFile *zipf;
6604 buffer_grow (filename, entry_length);
6605 memcpy (filename->data, entry_name, entry_length - 1);
6606 filename->data[entry_length-1] = '\0';
6607 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6608 if (zipf == NULL)
6609 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6610 else
6611 {
6612 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6613 BUFFER_RESET (filename);
6614 for (k = 0; k < package_length; k++)
6615 {
6616 char ch = package_name[k];
6617 *filename->ptr++ = ch == '.' ? '/' : ch;
6618 }
6619 *filename->ptr++ = '/';
6620
345137c7 6621 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6622 {
49f48c71 6623 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6624 int current_entry_len = zipd->filename_length;
6625
345137c7
TT
6626 if (current_entry_len >= BUFFER_LENGTH (filename)
6627 && strncmp (filename->data, current_entry,
6628 BUFFER_LENGTH (filename)) != 0)
63a212ed 6629 continue;
345137c7 6630 found |= note_possible_classname (current_entry,
63a212ed
PB
6631 current_entry_len);
6632 }
6633 }
6634 }
6635 else
6636 {
6637 BUFFER_RESET (filename);
6638 buffer_grow (filename, entry_length + package_length + 4);
6639 strcpy (filename->data, entry_name);
6640 filename->ptr = filename->data + entry_length;
6641 for (k = 0; k < package_length; k++)
6642 {
6643 char ch = package_name[k];
6644 *filename->ptr++ = ch == '.' ? '/' : ch;
6645 }
6646 *filename->ptr = '\0';
6647
6648 dirp = opendir (filename->data);
6649 if (dirp == NULL)
6650 continue;
6651 *filename->ptr++ = '/';
6652 for (;;)
6653 {
63a212ed 6654 int len;
49f48c71 6655 const char *d_name;
63a212ed
PB
6656 struct dirent *direntp = readdir (dirp);
6657 if (!direntp)
6658 break;
6659 d_name = direntp->d_name;
6660 len = strlen (direntp->d_name);
6661 buffer_grow (filename, len+1);
6662 strcpy (filename->ptr, d_name);
345137c7 6663 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6664 package_length+len+1);
6665 }
6666 if (dirp)
6667 closedir (dirp);
6668 }
6669 }
e04a16fb 6670
63a212ed 6671 free (filename->data);
e04a16fb 6672
63a212ed
PB
6673 /* Here we should have a unified way of retrieving an entry, to be
6674 indexed. */
6675 if (!found)
e04a16fb
AG
6676 {
6677 static int first = 1;
6678 if (first)
6679 {
781b0558 6680 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6681 java_error_count++;
6682 first = 0;
6683 }
6684 else
63a212ed
PB
6685 parse_error_context (wfl, "Package `%s' not found in import",
6686 package_name);
e04a16fb
AG
6687 current_jcf = saved_jcf;
6688 return;
6689 }
e04a16fb
AG
6690 current_jcf = saved_jcf;
6691}
6692
6693/* Possibly find a type in the import on demands specified
6694 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6695 entire list, to detected potential double definitions. */
6696
6697static int
6698find_in_imports_on_demand (class_type)
6699 tree class_type;
6700{
ab3a6dd6 6701 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6702 int seen_once = -1;
ab3a6dd6 6703 tree cl = NULL_TREE;
e04a16fb
AG
6704
6705 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6706 {
49f48c71 6707 const char *id_name;
e04a16fb
AG
6708 obstack_grow (&temporary_obstack,
6709 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6710 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6711 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6712 obstack_grow0 (&temporary_obstack,
6713 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6714 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6715 id_name = obstack_finish (&temporary_obstack);
6716
6717 node = maybe_get_identifier (id_name);
6718 if (node && IS_A_CLASSFILE_NAME (node))
6719 {
6720 if (seen_once < 0)
6721 {
6722 cl = TREE_PURPOSE (import);
6723 seen_once = 1;
6724 node_to_use = node;
6725 }
6726 else
6727 {
6728 seen_once++;
6729 parse_error_context
1e12ab9b
APB
6730 (TREE_PURPOSE (import),
6731 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6732 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6733 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6734 }
6735 }
6736 }
6737
6738 if (seen_once == 1)
6739 {
6740 /* Setup lineno so that it refers to the line of the import (in
6741 case we parse a class file and encounter errors */
6742 tree decl;
6743 int saved_lineno = lineno;
6744 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6745 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6746 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6747 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6748 /* If there is no DECL set for the class or if the class isn't
6749 loaded and not seen in source yet, the load */
6750 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6751 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6752 load_class (node_to_use, 0);
6753 lineno = saved_lineno;
6754 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6755 }
6756 else
6757 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6758}
6759
9a7ab4b3
APB
6760/* Add package NAME to the list of package encountered so far. To
6761 speed up class lookup in do_resolve_class, we make sure a
6762 particular package is added only once. */
6763
6764static void
6765register_package (name)
6766 tree name;
6767{
6768 static struct hash_table _pht, *pht = NULL;
6769
6770 if (!pht)
6771 {
6772 hash_table_init (&_pht, hash_newfunc,
6773 java_hash_hash_tree_node, java_hash_compare_tree_node);
6774 pht = &_pht;
6775 }
6776
6777 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6778 {
6779 package_list = chainon (package_list, build_tree_list (name, NULL));
6780 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6781 }
6782}
6783
5e942c50
APB
6784static tree
6785resolve_package (pkg, next)
6786 tree pkg, *next;
6787{
c2952b01 6788 tree current, acc;
5e942c50 6789 tree type_name = NULL_TREE;
49f48c71 6790 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6791
6792 /* The trick is to determine when the package name stops and were
6793 the name of something contained in the package starts. Then we
6794 return a fully qualified name of what we want to get. */
6795
6796 /* Do a quick search on well known package names */
6797 if (!strncmp (name, "java.lang.reflect", 17))
6798 {
6799 *next =
6800 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6801 type_name = lookup_package_type (name, 17);
6802 }
6803 else if (!strncmp (name, "java.lang", 9))
6804 {
6805 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6806 type_name = lookup_package_type (name, 9);
6807 }
5e942c50 6808
2c56429a
APB
6809 /* If we found something here, return */
6810 if (type_name)
6811 return type_name;
6812
6813 *next = EXPR_WFL_QUALIFICATION (pkg);
6814
6815 /* Try the current package. */
6816 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6817 IDENTIFIER_LENGTH (ctxp->package)))
6818 {
6819 type_name =
6820 lookup_package_type_and_set_next (name,
6821 IDENTIFIER_LENGTH (ctxp->package),
6822 next );
6823 if (type_name)
6824 return type_name;
6825 }
6826
6827 /* Search in imported package */
6828 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6829 {
6830 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6831 int len = IDENTIFIER_LENGTH (current_pkg_name);
6832 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6833 {
6834 tree left, dummy;
6835
6836 breakdown_qualified (&left, &dummy, current_pkg_name);
6837 len = IDENTIFIER_LENGTH (left);
6838 type_name = lookup_package_type_and_set_next (name, len, next);
6839 if (type_name)
6840 break;
6841 }
6842 }
6843
c2952b01
APB
6844 /* Try to progressively construct a type name */
6845 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6846 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6847 current; current = TREE_CHAIN (current))
6848 {
6849 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6850 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6851 {
6852 type_name = acc;
6b48deee
APB
6853 /* resolve_package should be used in a loop, hence we
6854 point at this one to naturally process the next one at
6855 the next iteration. */
6856 *next = current;
c2952b01
APB
6857 break;
6858 }
6859 }
2c56429a
APB
6860 return type_name;
6861}
6862
6863static tree
6864lookup_package_type_and_set_next (name, len, next)
49f48c71 6865 const char *name;
2c56429a
APB
6866 int len;
6867 tree *next;
6868{
49f48c71 6869 const char *ptr;
2c56429a
APB
6870 tree type_name = lookup_package_type (name, len);
6871
6872 if (!type_name)
6873 return NULL;
6874
6875 ptr = IDENTIFIER_POINTER (type_name);
6876 while (ptr && (ptr = strchr (ptr, '.')))
6877 {
6878 *next = TREE_CHAIN (*next);
6879 ptr++;
6880 }
5e942c50
APB
6881 return type_name;
6882}
6883
6884static tree
6885lookup_package_type (name, from)
49f48c71 6886 const char *name;
5e942c50
APB
6887 int from;
6888{
6889 char subname [128];
49f48c71 6890 const char *sub = &name[from+1];
5e942c50
APB
6891 while (*sub != '.' && *sub)
6892 sub++;
6893 strncpy (subname, name, sub-name);
6894 subname [sub-name] = '\0';
6895 return get_identifier (subname);
6896}
6897
cf1748bf 6898static void
4dbf4496
APB
6899check_inner_class_access (decl, enclosing_decl, cl)
6900 tree decl, enclosing_decl, cl;
cf1748bf 6901{
4dbf4496
APB
6902 int access = 0;
6903
cf1748bf 6904 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6905 as a result of processing a JDEP crafted by source_start_java_method
6906 for the purpose of patching its parm decl. But the error would
6907 have been already trapped when fixing the method's signature.
6908 DECL can also be NULL in case of earlier errors. */
6909 if (!decl || !cl)
cf1748bf
APB
6910 return;
6911
4dbf4496
APB
6912 /* We grant access to private and protected inner classes if the
6913 location from where we're trying to access DECL is an enclosing
6914 context for DECL or if both have a common enclosing context. */
6915 if (CLASS_PRIVATE (decl))
6916 access = 1;
6917 if (CLASS_PROTECTED (decl))
6918 access = 2;
6919 if (!access)
6920 return;
6921
6922 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6923 TREE_TYPE (decl))
6924 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6925 TREE_TYPE (decl)))
6926 return;
6927
6928 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6929 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6930 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6931 lang_printable_name (decl, 0));
6932}
6933
e04a16fb
AG
6934/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6935 access violations were found, 1 otherwise. */
6936
6937static int
6938check_pkg_class_access (class_name, cl)
6939 tree class_name;
6940 tree cl;
6941{
6942 tree type;
e04a16fb
AG
6943
6944 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6945 return 0;
6946
6947 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6948 return 0;
6949
6950 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6951 {
e28cd97b
APB
6952 /* Access to a private class within the same package is
6953 allowed. */
6954 tree l, r;
6955 breakdown_qualified (&l, &r, class_name);
6956 if (l == ctxp->package)
6957 return 0;
6958
e04a16fb 6959 parse_error_context
781b0558 6960 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6961 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6962 IDENTIFIER_POINTER (class_name));
6963 return 1;
6964 }
6965 return 0;
6966}
6967
6968/* Local variable declaration. */
6969
6970static void
6971declare_local_variables (modifier, type, vlist)
6972 int modifier;
6973 tree type;
6974 tree vlist;
6975{
c583dd46
APB
6976 tree decl, current, saved_type;
6977 tree type_wfl = NULL_TREE;
e04a16fb 6978 int must_chain = 0;
c2952b01 6979 int final_p = 0;
e04a16fb 6980
2aa11e97 6981 /* Push a new block if statements were seen between the last time we
e04a16fb 6982 pushed a block and now. Keep a cound of block to close */
f099f336 6983 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6984 {
f099f336 6985 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6986 tree b = enter_block ();
f099f336 6987 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6988 }
6989
6990 if (modifier)
6991 {
6992 int i;
6993 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6994 if (modifier == ACC_FINAL)
c2952b01 6995 final_p = 1;
c877974e
APB
6996 else
6997 {
6998 parse_error_context
6999 (ctxp->modifier_ctx [i],
7000 "Only `final' is allowed as a local variables modifier");
7001 return;
7002 }
e04a16fb
AG
7003 }
7004
c583dd46
APB
7005 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7006 hold the TYPE value if a new incomplete has to be created (as
7007 opposed to being found already existing and reused). */
7008 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7009
7010 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 7011 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
7012
7013 /* Go through all the declared variables */
7014 for (current = vlist, saved_type = type; current;
7015 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 7016 {
c877974e 7017 tree other, real_type;
e04a16fb
AG
7018 tree wfl = TREE_PURPOSE (current);
7019 tree name = EXPR_WFL_NODE (wfl);
7020 tree init = TREE_VALUE (current);
e04a16fb 7021
c583dd46
APB
7022 /* Process NAME, as it may specify extra dimension(s) for it */
7023 type = build_array_from_name (type, type_wfl, name, &name);
7024
7025 /* Variable redefinition check */
7026 if ((other = lookup_name_in_blocks (name)))
7027 {
7028 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7029 DECL_SOURCE_LINE (other));
7030 continue;
7031 }
7032
7033 /* Type adjustment. We may have just readjusted TYPE because
7034 the variable specified more dimensions. Make sure we have
7035 a reference if we can and don't have one already. */
1886c9d8 7036 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7037
7038 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7039 /* Never layout this decl. This will be done when its scope
7040 will be entered */
c877974e 7041 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 7042 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7043 BLOCK_CHAIN_DECL (decl);
7044
d4370213
APB
7045 /* If doing xreferencing, replace the line number with the WFL
7046 compound value */
7047 if (flag_emit_xref)
7048 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7049
e04a16fb
AG
7050 /* Don't try to use an INIT statement when an error was found */
7051 if (init && java_error_count)
7052 init = NULL_TREE;
c583dd46
APB
7053
7054 /* Add the initialization function to the current function's code */
7055 if (init)
e04a16fb 7056 {
c583dd46
APB
7057 /* Name might have been readjusted */
7058 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7059 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7060 java_method_add_stmt (current_function_decl,
7061 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7062 init));
7063 }
7064
7065 /* Setup dependency the type of the decl */
7066 if (must_chain)
7067 {
7068 jdep *dep;
7069 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7070 dep = CLASSD_LAST (ctxp->classd_list);
7071 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7072 }
7073 }
7074 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7075}
7076
7077/* Called during parsing. Build decls from argument list. */
7078
7079static void
7080source_start_java_method (fndecl)
7081 tree fndecl;
7082{
7083 tree tem;
7084 tree parm_decl;
7085 int i;
7086
79d13333
APB
7087 if (!fndecl)
7088 return;
7089
e04a16fb
AG
7090 current_function_decl = fndecl;
7091
7092 /* New scope for the function */
7093 enter_block ();
7094 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7095 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7096 {
7097 tree type = TREE_VALUE (tem);
7098 tree name = TREE_PURPOSE (tem);
7099
23a79c61
APB
7100 /* If type is incomplete. Create an incomplete decl and ask for
7101 the decl to be patched later */
e04a16fb
AG
7102 if (INCOMPLETE_TYPE_P (type))
7103 {
7104 jdep *jdep;
c877974e
APB
7105 tree real_type = GET_REAL_TYPE (type);
7106 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7107 type = obtain_incomplete_type (type);
e04a16fb
AG
7108 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7109 jdep = CLASSD_LAST (ctxp->classd_list);
7110 JDEP_MISC (jdep) = name;
7111 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7112 }
7113 else
7114 parm_decl = build_decl (PARM_DECL, name, type);
7115
c2952b01
APB
7116 /* Remember if a local variable was declared final (via its
7117 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7118 if (ARG_FINAL_P (tem))
7119 LOCAL_FINAL (parm_decl) = 1;
7120
e04a16fb
AG
7121 BLOCK_CHAIN_DECL (parm_decl);
7122 }
7123 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7124 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7125 nreverse (tem);
7126 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7127 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7128}
7129
22eed1e6
APB
7130/* Called during parsing. Creates an artificial method declaration. */
7131
7132static tree
7133create_artificial_method (class, flags, type, name, args)
7134 tree class;
7135 int flags;
7136 tree type, name, args;
7137{
22eed1e6
APB
7138 tree mdecl;
7139
c2952b01 7140 java_parser_context_save_global ();
22eed1e6
APB
7141 lineno = 0;
7142 mdecl = make_node (FUNCTION_TYPE);
7143 TREE_TYPE (mdecl) = type;
7144 TYPE_ARG_TYPES (mdecl) = args;
7145 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7146 java_parser_context_restore_global ();
22eed1e6
APB
7147 DECL_ARTIFICIAL (mdecl) = 1;
7148 return mdecl;
7149}
7150
7151/* Starts the body if an artifical method. */
7152
7153static void
7154start_artificial_method_body (mdecl)
7155 tree mdecl;
7156{
7157 DECL_SOURCE_LINE (mdecl) = 1;
7158 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7159 source_start_java_method (mdecl);
7160 enter_block ();
7161}
7162
7163static void
7164end_artificial_method_body (mdecl)
7165 tree mdecl;
7166{
7167 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7168 exit_block ();
7169}
7170
e04a16fb
AG
7171/* Called during expansion. Push decls formerly built from argument
7172 list so they're usable during expansion. */
7173
7174static void
7175expand_start_java_method (fndecl)
7176 tree fndecl;
7177{
7178 tree tem, *ptr;
e04a16fb 7179
e04a16fb
AG
7180 current_function_decl = fndecl;
7181
c2952b01
APB
7182 if (! quiet_flag)
7183 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7184 announce_function (fndecl);
c2952b01
APB
7185 if (! quiet_flag)
7186 fprintf (stderr, "]");
7187
7188 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7189 ptr = &DECL_ARGUMENTS (fndecl);
7190 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7191 while (tem)
7192 {
7193 tree next = TREE_CHAIN (tem);
b67d701b 7194 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7195 if (PROMOTE_PROTOTYPES
7196 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7197 && INTEGRAL_TYPE_P (type))
7198 type = integer_type_node;
b67d701b 7199 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7200 layout_decl (tem, 0);
7201 pushdecl (tem);
e04a16fb
AG
7202 *ptr = tem;
7203 ptr = &TREE_CHAIN (tem);
7204 tem = next;
7205 }
7206 *ptr = NULL_TREE;
7207 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7208 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7209}
7210
7211/* Terminate a function and expand its body. */
7212
7213static void
7214source_end_java_method ()
7215{
7216 tree fndecl = current_function_decl;
138657ec 7217 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7218
79d13333
APB
7219 if (!fndecl)
7220 return;
7221
e04a16fb
AG
7222 java_parser_context_save_global ();
7223 lineno = ctxp->last_ccb_indent1;
7224
b67d701b
PB
7225 /* Set EH language codes */
7226 java_set_exception_lang_code ();
7227
5423609c
APB
7228 /* Turn function bodies with only a NOP expr null, so they don't get
7229 generated at all and we won't get warnings when using the -W
7230 -Wall flags. */
7231 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7232 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7233
e04a16fb
AG
7234 /* Generate function's code */
7235 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7236 && ! flag_emit_class_files
7237 && ! flag_emit_xref)
e04a16fb
AG
7238 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7239
7240 /* pop out of its parameters */
7241 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7242 poplevel (1, 0, 1);
7243 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7244
7245 /* Generate rtl for function exit. */
e8fc7396 7246 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7247 {
7248 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7249 /* Emit catch-finally clauses */
7250 emit_handlers ();
e04a16fb
AG
7251 expand_function_end (input_filename, lineno, 0);
7252
138657ec
AH
7253 /* FIXME: If the current method contains any exception handlers,
7254 force asynchronous_exceptions: this is necessary because signal
7255 handlers in libjava may throw exceptions. This is far from being
7256 a perfect solution, but it's better than doing nothing at all.*/
7257 if (catch_clauses)
7258 asynchronous_exceptions = 1;
7259
e04a16fb
AG
7260 /* Run the optimizers and output assembler code for this function. */
7261 rest_of_compilation (fndecl);
7262 }
7263
7264 current_function_decl = NULL_TREE;
e04a16fb 7265 java_parser_context_restore_global ();
138657ec 7266 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7267}
7268
7269/* Record EXPR in the current function block. Complements compound
7270 expression second operand if necessary. */
7271
7272tree
7273java_method_add_stmt (fndecl, expr)
7274 tree fndecl, expr;
7275{
b771925e
APB
7276 if (!GET_CURRENT_BLOCK (fndecl))
7277 return NULL_TREE;
f099f336 7278 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7279}
e04a16fb 7280
b67d701b
PB
7281static tree
7282add_stmt_to_block (b, type, stmt)
7283 tree b, type, stmt;
7284{
7285 tree body = BLOCK_EXPR_BODY (b), c;
7286
e04a16fb
AG
7287 if (java_error_count)
7288 return body;
b67d701b
PB
7289
7290 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7291 return body;
7292
b67d701b
PB
7293 BLOCK_EXPR_BODY (b) = c;
7294 TREE_SIDE_EFFECTS (c) = 1;
7295 return c;
e04a16fb
AG
7296}
7297
7298/* Add STMT to EXISTING if possible, otherwise create a new
7299 COMPOUND_EXPR and add STMT to it. */
7300
7301static tree
7302add_stmt_to_compound (existing, type, stmt)
7303 tree existing, type, stmt;
7304{
15fdcfe9
PB
7305 if (existing)
7306 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7307 else
15fdcfe9 7308 return stmt;
e04a16fb
AG
7309}
7310
1886c9d8
APB
7311void java_layout_seen_class_methods ()
7312{
7313 tree previous_list = all_class_list;
7314 tree end = NULL_TREE;
7315 tree current;
7316
7317 while (1)
7318 {
7319 for (current = previous_list;
7320 current != end; current = TREE_CHAIN (current))
7321 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7322
7323 if (previous_list != all_class_list)
7324 {
7325 end = previous_list;
7326 previous_list = all_class_list;
7327 }
7328 else
7329 break;
7330 }
7331}
7332
e04a16fb 7333void
c2952b01 7334java_reorder_fields ()
e04a16fb 7335{
c2952b01 7336 static tree stop_reordering = NULL_TREE;
19e223db 7337 static int initialized_p;
c2952b01 7338 tree current;
19e223db
MM
7339
7340 /* Register STOP_REORDERING with the garbage collector. */
7341 if (!initialized_p)
7342 {
7343 ggc_add_tree_root (&stop_reordering, 1);
7344 initialized_p = 1;
7345 }
7346
5e942c50 7347 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7348 {
5e942c50 7349 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7350
c2952b01
APB
7351 if (current_class == stop_reordering)
7352 break;
7353
c877974e
APB
7354 /* Reverse the fields, but leave the dummy field in front.
7355 Fields are already ordered for Object and Class */
7356 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7357 && current_class != class_type_node)
7358 {
23a79c61
APB
7359 /* If the dummy field is there, reverse the right fields and
7360 just layout the type for proper fields offset */
c877974e
APB
7361 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7362 {
7363 tree fields = TYPE_FIELDS (current_class);
7364 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7365 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7366 }
23a79c61
APB
7367 /* We don't have a dummy field, we need to layout the class,
7368 after having reversed the fields */
c877974e
APB
7369 else
7370 {
7371 TYPE_FIELDS (current_class) =
7372 nreverse (TYPE_FIELDS (current_class));
7373 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7374 }
7375 }
c2952b01
APB
7376 }
7377 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7378}
7379
7380/* Layout the methods of all classes loaded in one way on an
7381 other. Check methods of source parsed classes. Then reorder the
7382 fields and layout the classes or the type of all source parsed
7383 classes */
7384
7385void
7386java_layout_classes ()
7387{
7388 tree current;
7389 int save_error_count = java_error_count;
7390
7391 /* Layout the methods of all classes seen so far */
7392 java_layout_seen_class_methods ();
7393 java_parse_abort_on_error ();
7394 all_class_list = NULL_TREE;
7395
7396 /* Then check the methods of all parsed classes */
7397 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7398 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7399 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7400 java_parse_abort_on_error ();
7401
7402 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7403 {
7404 current_class = TREE_TYPE (TREE_VALUE (current));
7405 layout_class (current_class);
5e942c50 7406
c877974e
APB
7407 /* From now on, the class is considered completely loaded */
7408 CLASS_LOADED_P (current_class) = 1;
7409
5e942c50
APB
7410 /* Error reported by the caller */
7411 if (java_error_count)
7412 return;
e04a16fb 7413 }
23a79c61
APB
7414
7415 /* We might have reloaded classes durign the process of laying out
7416 classes for code generation. We must layout the methods of those
7417 late additions, as constructor checks might use them */
1886c9d8 7418 java_layout_seen_class_methods ();
23a79c61 7419 java_parse_abort_on_error ();
e04a16fb
AG
7420}
7421
c2952b01
APB
7422/* Expand methods in the current set of classes rememebered for
7423 generation. */
e04a16fb 7424
49f48c71 7425static void
c2952b01 7426java_complete_expand_classes ()
e04a16fb
AG
7427{
7428 tree current;
ce6e9147
APB
7429
7430 do_not_fold = flag_emit_xref;
c2952b01 7431
e04a16fb 7432 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7433 if (!INNER_CLASS_DECL_P (current))
7434 java_complete_expand_class (current);
7435}
e04a16fb 7436
c2952b01
APB
7437/* Expand the methods found in OUTER, starting first by OUTER's inner
7438 classes, if any. */
e04a16fb 7439
c2952b01
APB
7440static void
7441java_complete_expand_class (outer)
7442 tree outer;
7443{
7444 tree inner_list;
e04a16fb 7445
c2952b01 7446 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7447
c2952b01
APB
7448 /* We need to go after all inner classes and start expanding them,
7449 starting with most nested ones. We have to do that because nested
7450 classes might add functions to outer classes */
e04a16fb 7451
c2952b01
APB
7452 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7453 inner_list; inner_list = TREE_CHAIN (inner_list))
7454 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7455
c2952b01
APB
7456 java_complete_expand_methods (outer);
7457 set_nested_class_simple_name_value (outer, 0); /* Reset */
7458}
7459
7460/* Expand methods registered in CLASS_DECL. The general idea is that
7461 we expand regular methods first. This allows us get an estimate on
7462 how outer context local alias fields are really used so we can add
7463 to the constructor just enough code to initialize them properly (it
c00f0fb2 7464 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7465 constructors and then <clinit>. */
7466
7467static void
7468java_complete_expand_methods (class_decl)
7469 tree class_decl;
7470{
7471 tree clinit, finit, decl, first_decl;
7472
7473 current_class = TREE_TYPE (class_decl);
7474
7475 /* Initialize a new constant pool */
7476 init_outgoing_cpool ();
7477
7478 /* Pre-expand <clinit> to figure whether we really need it or
7479 not. If we do need it, we pre-expand the static fields so they're
7480 ready to be used somewhere else. <clinit> will be fully expanded
7481 after we processed the constructors. */
7482 first_decl = TYPE_METHODS (current_class);
7483 clinit = maybe_generate_pre_expand_clinit (current_class);
7484
c00f0fb2 7485 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7486 try to use it.*/
7487 if (TYPE_FINIT_STMT_LIST (current_class))
7488 {
7489 finit = generate_finit (current_class);
7490 java_complete_expand_method (finit);
7491 }
7492
7493 /* Now do the constructors */
7494 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7495 {
7496 int no_body;
7497
7498 if (!DECL_CONSTRUCTOR_P (decl))
7499 continue;
7500
7501 no_body = !DECL_FUNCTION_BODY (decl);
7502 /* Don't generate debug info on line zero when expanding a
7503 generated constructor. */
7504 if (no_body)
7505 restore_line_number_status (1);
7506
7507 java_complete_expand_method (decl);
7508
7509 if (no_body)
7510 restore_line_number_status (0);
7511 }
7512
7513 /* First, do the ordinary methods. */
7514 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7515 {
7145d9fe
TT
7516 /* Skip abstract or native methods -- but do handle native
7517 methods when generating JNI stubs. */
7518 if (METHOD_ABSTRACT (decl)
7519 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7520 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7521 continue;
7145d9fe
TT
7522
7523 if (METHOD_NATIVE (decl))
7524 {
7525 tree body = build_jni_stub (decl);
7526 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7527 }
7528
c2952b01
APB
7529 java_complete_expand_method (decl);
7530 }
7531
7532 /* If there is indeed a <clinit>, fully expand it now */
7533 if (clinit)
7534 {
7535 /* Prevent the use of `this' inside <clinit> */
7536 ctxp->explicit_constructor_p = 1;
7537 java_complete_expand_method (clinit);
7538 ctxp->explicit_constructor_p = 0;
e04a16fb 7539 }
c2952b01 7540
165f37bc
APB
7541 /* We might have generated a class$ that we now want to expand */
7542 if (TYPE_DOT_CLASS (current_class))
7543 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7544
c2952b01
APB
7545 /* Now verify constructor circularity (stop after the first one we
7546 prove wrong.) */
7547 if (!CLASS_INTERFACE (class_decl))
7548 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7549 if (DECL_CONSTRUCTOR_P (decl)
7550 && verify_constructor_circularity (decl, decl))
7551 break;
7552
7553 /* Save the constant pool. We'll need to restore it later. */
7554 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7555}
7556
c2952b01
APB
7557/* Attempt to create <clinit>. Pre-expand static fields so they can be
7558 safely used in some other methods/constructors. */
e920ebc9 7559
c2952b01
APB
7560static tree
7561maybe_generate_pre_expand_clinit (class_type)
7562 tree class_type;
e920ebc9 7563{
c2952b01
APB
7564 tree current, mdecl;
7565
7566 if (!TYPE_CLINIT_STMT_LIST (class_type))
7567 return NULL_TREE;
e920ebc9 7568
c2952b01
APB
7569 /* Go through all static fields and pre expand them */
7570 for (current = TYPE_FIELDS (class_type); current;
7571 current = TREE_CHAIN (current))
7572 if (FIELD_STATIC (current))
7573 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7574
7575 /* Then build the <clinit> method */
7576 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7577 clinit_identifier_node, end_params_node);
7578 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7579 mdecl, NULL_TREE);
7580 start_artificial_method_body (mdecl);
7581
7582 /* We process the list of assignment we produced as the result of
7583 the declaration of initialized static field and add them as
7584 statement to the <clinit> method. */
7585 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7586 current = TREE_CHAIN (current))
e920ebc9 7587 {
9a7ab4b3 7588 tree stmt = current;
c2952b01
APB
7589 /* We build the assignment expression that will initialize the
7590 field to its value. There are strict rules on static
7591 initializers (8.5). FIXME */
98a52c2c 7592 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7593 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7594 java_method_add_stmt (mdecl, stmt);
7595 }
e920ebc9 7596
c2952b01
APB
7597 end_artificial_method_body (mdecl);
7598
92d83515
APB
7599 /* Now we want to place <clinit> as the last method (because we need
7600 it at least for interface so that it doesn't interfere with the
7601 dispatch table based lookup. */
7602 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7603 {
92d83515
APB
7604 current = TREE_CHAIN (TYPE_METHODS (class_type));
7605 TYPE_METHODS (class_type) = current;
c2952b01
APB
7606
7607 while (TREE_CHAIN (current))
7608 current = TREE_CHAIN (current);
92d83515 7609
c2952b01
APB
7610 TREE_CHAIN (current) = mdecl;
7611 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7612 }
c2952b01
APB
7613
7614 return mdecl;
e920ebc9
APB
7615}
7616
dba41d30
APB
7617/* Analyzes a method body and look for something that isn't a
7618 MODIFY_EXPR. */
7619
7620static int
7621analyze_clinit_body (bbody)
7622 tree bbody;
7623{
7624 while (bbody)
7625 switch (TREE_CODE (bbody))
7626 {
7627 case BLOCK:
7628 bbody = BLOCK_EXPR_BODY (bbody);
7629 break;
7630
7631 case EXPR_WITH_FILE_LOCATION:
7632 bbody = EXPR_WFL_NODE (bbody);
7633 break;
7634
7635 case COMPOUND_EXPR:
7636 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7637 return 1;
7638 bbody = TREE_OPERAND (bbody, 1);
7639 break;
7640
7641 case MODIFY_EXPR:
7642 bbody = NULL_TREE;
7643 break;
7644
7645 default:
7646 bbody = NULL_TREE;
7647 return 1;
7648 }
7649 return 0;
7650}
7651
7652
92d83515
APB
7653/* See whether we could get rid of <clinit>. Criteria are: all static
7654 final fields have constant initial values and the body of <clinit>
7655 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7656
7657static int
7658maybe_yank_clinit (mdecl)
7659 tree mdecl;
7660{
7661 tree type, current;
7662 tree fbody, bbody;
99eaf8d4 7663 int found = 0;
92d83515
APB
7664
7665 if (!DECL_CLINIT_P (mdecl))
7666 return 0;
f0f3a777
APB
7667
7668 /* If the body isn't empty, then we keep <clinit>. Note that if
7669 we're emitting classfiles, this isn't enough not to rule it
7670 out. */
92d83515 7671 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7672 bbody = BLOCK_EXPR_BODY (fbody);
7673 if (bbody && bbody != error_mark_node)
92d83515 7674 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7675 else
7676 return 0;
f0f3a777 7677 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7678 return 0;
7679
7680 type = DECL_CONTEXT (mdecl);
7681 current = TYPE_FIELDS (type);
7682
7683 for (current = (current ? TREE_CHAIN (current) : current);
7684 current; current = TREE_CHAIN (current))
f0f3a777
APB
7685 {
7686 tree f_init;
7687
7688 /* We're not interested in non static field */
7689 if (!FIELD_STATIC (current))
7690 continue;
7691
7692 /* Anything that isn't String or a basic type is ruled out -- or
7693 if we now how to deal with it (when doing things natively) we
7694 should generated an empty <clinit> so that SUID are computed
7695 correctly. */
7696 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7697 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7698 break;
7699
7700 f_init = DECL_INITIAL (current);
7701 /* If we're emitting native code, we want static final fields to
7702 have constant initializers. If we don't meet these
7703 conditions, we keep <clinit> */
7704 if (!flag_emit_class_files
7705 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7706 break;
7707 /* If we're emitting bytecode, we want static fields to have
7708 constant initializers or no initializer. If we don't meet
7709 these conditions, we keep <clinit> */
7710 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7711 break;
7712 }
92d83515 7713
99eaf8d4
APB
7714 /* Now we analyze the method body and look for something that
7715 isn't a MODIFY_EXPR */
7716 if (bbody == empty_stmt_node)
dba41d30
APB
7717 found = 0;
7718 else
7719 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7720
7721 if (current || found)
92d83515
APB
7722 return 0;
7723
7724 /* Get rid of <clinit> in the class' list of methods */
7725 if (TYPE_METHODS (type) == mdecl)
7726 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7727 else
7728 for (current = TYPE_METHODS (type); current;
7729 current = TREE_CHAIN (current))
7730 if (TREE_CHAIN (current) == mdecl)
7731 {
7732 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7733 break;
7734 }
7735
7736 return 1;
7737}
7738
7739
e04a16fb
AG
7740/* Complete and expand a method. */
7741
7742static void
7743java_complete_expand_method (mdecl)
7744 tree mdecl;
7745{
92d83515
APB
7746 int yank_clinit = 0;
7747
c2952b01 7748 current_function_decl = mdecl;
22eed1e6
APB
7749 /* Fix constructors before expanding them */
7750 if (DECL_CONSTRUCTOR_P (mdecl))
7751 fix_constructors (mdecl);
e04a16fb 7752
22eed1e6 7753 /* Expand functions that have a body */
e04a16fb
AG
7754 if (DECL_FUNCTION_BODY (mdecl))
7755 {
9bbc7d9f
PB
7756 tree fbody = DECL_FUNCTION_BODY (mdecl);
7757 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7758 tree exception_copy = NULL_TREE;
e04a16fb 7759 expand_start_java_method (mdecl);
939d7216 7760 build_result_decl (mdecl);
e04a16fb
AG
7761
7762 current_this
7763 = (!METHOD_STATIC (mdecl) ?
7764 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7765
ce6e9147
APB
7766 /* Purge the `throws' list of unchecked exceptions. If we're
7767 doing xref, save a copy of the list and re-install it
7768 later. */
7769 if (flag_emit_xref)
7770 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7771
b9f7e36c
APB
7772 purge_unchecked_exceptions (mdecl);
7773
7774 /* Install exceptions thrown with `throws' */
7775 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7776
9bbc7d9f 7777 if (block_body != NULL_TREE)
bc3ca41b
PB
7778 {
7779 block_body = java_complete_tree (block_body);
c2952b01 7780
7145d9fe 7781 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7782 check_for_initialization (block_body);
f099f336 7783 ctxp->explicit_constructor_p = 0;
bc3ca41b 7784 }
e803d3b2 7785
9bbc7d9f 7786 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7787
c2952b01
APB
7788 /* If we saw a return but couldn't evaluate it properly, we'll
7789 have an error_mark_node here. */
7790 if (block_body != error_mark_node
7791 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7792 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7793 && !flag_emit_xref)
82371d41 7794 missing_return_error (current_function_decl);
7525cc04 7795
92d83515
APB
7796 /* Check wether we could just get rid of clinit, now the picture
7797 is complete. */
7798 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7799 complete_start_java_method (mdecl);
7800
e04a16fb 7801 /* Don't go any further if we've found error(s) during the
92d83515
APB
7802 expansion */
7803 if (!java_error_count && !yank_clinit)
e04a16fb 7804 source_end_java_method ();
22eed1e6
APB
7805 else
7806 {
92d83515
APB
7807 if (java_error_count)
7808 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7809 poplevel (1, 0, 1);
7810 }
b9f7e36c
APB
7811
7812 /* Pop the exceptions and sanity check */
7813 POP_EXCEPTIONS();
7814 if (currently_caught_type_list)
7815 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7816
7817 if (flag_emit_xref)
7818 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7819 }
7820}
7821
c2952b01
APB
7822\f
7823
7824/* This section of the code deals with accessing enclosing context
7825 fields either directly by using the relevant access to this$<n> or
7826 by invoking an access method crafted for that purpose. */
7827
7828/* Build the necessary access from an inner class to an outer
7829 class. This routine could be optimized to cache previous result
7830 (decl, current_class and returned access). When an access method
7831 needs to be generated, it always takes the form of a read. It might
7832 be later turned into a write by calling outer_field_access_fix. */
7833
7834static tree
7835build_outer_field_access (id, decl)
7836 tree id, decl;
7837{
7838 tree access = NULL_TREE;
7839 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7840
7841 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7842 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7843 the `private' barrier if we're not emitting bytecodes. */
7844 if (ctx == DECL_CONTEXT (decl)
7845 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7846 {
7847 tree thisn = build_current_thisn (current_class);
7848 access = make_qualified_primary (build_wfl_node (thisn),
7849 id, EXPR_WFL_LINECOL (id));
7850 }
7851 /* Otherwise, generate access methods to outer this and access the
7852 field (either using an access method or by direct access.) */
7853 else
7854 {
7855 int lc = EXPR_WFL_LINECOL (id);
7856
7857 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7858 get a hold to the enclosing instance we need, and then we
c2952b01 7859 build the field access. */
dba41d30 7860 access = build_access_to_thisn (current_class, DECL_CONTEXT (decl), lc);
c2952b01
APB
7861
7862 /* If the field is private and we're generating bytecode, then
7863 we generate an access method */
7864 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7865 {
7866 tree name = build_outer_field_access_methods (decl);
7867 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7868 name, access, NULL_TREE);
7869 }
7870 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7871 Once again we break the `private' access rule from a foreign
7872 class. */
7873 else
7874 access = make_qualified_primary (access, id, lc);
7875 }
7876 return resolve_expression_name (access, NULL);
7877}
7878
7879/* Return a non zero value if NODE describes an outer field inner
7880 access. */
7881
7882static int
7883outer_field_access_p (type, decl)
7884 tree type, decl;
7885{
7886 if (!INNER_CLASS_TYPE_P (type)
7887 || TREE_CODE (decl) != FIELD_DECL
7888 || DECL_CONTEXT (decl) == type)
7889 return 0;
7890
7891 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7892 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7893 {
7894 if (type == DECL_CONTEXT (decl))
7895 return 1;
7896 if (!DECL_CONTEXT (TYPE_NAME (type)))
7897 break;
7898 }
7899
7900 return 0;
7901}
7902
7903/* Return a non zero value if NODE represents an outer field inner
7904 access that was been already expanded. As a side effect, it returns
7905 the name of the field being accessed and the argument passed to the
7906 access function, suitable for a regeneration of the access method
7907 call if necessary. */
7908
7909static int
7910outer_field_expanded_access_p (node, name, arg_type, arg)
7911 tree node, *name, *arg_type, *arg;
7912{
7913 int identified = 0;
7914
7915 if (TREE_CODE (node) != CALL_EXPR)
7916 return 0;
7917
7918 /* Well, gcj generates slightly different tree nodes when compiling
7919 to native or bytecodes. It's the case for function calls. */
7920
7921 if (flag_emit_class_files
7922 && TREE_CODE (node) == CALL_EXPR
7923 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7924 identified = 1;
7925 else if (!flag_emit_class_files)
7926 {
7927 node = TREE_OPERAND (node, 0);
7928
7929 if (node && TREE_OPERAND (node, 0)
7930 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7931 {
7932 node = TREE_OPERAND (node, 0);
7933 if (TREE_OPERAND (node, 0)
7934 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7935 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7936 (DECL_NAME (TREE_OPERAND (node, 0)))))
7937 identified = 1;
7938 }
7939 }
7940
7941 if (identified && name && arg_type && arg)
7942 {
7943 tree argument = TREE_OPERAND (node, 1);
7944 *name = DECL_NAME (TREE_OPERAND (node, 0));
7945 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7946 *arg = TREE_VALUE (argument);
7947 }
7948 return identified;
7949}
7950
7951/* Detect in NODE an outer field read access from an inner class and
7952 transform it into a write with RHS as an argument. This function is
7953 called from the java_complete_lhs when an assignment to a LHS can
7954 be identified. */
7955
7956static tree
7957outer_field_access_fix (wfl, node, rhs)
7958 tree wfl, node, rhs;
7959{
7960 tree name, arg_type, arg;
7961
7962 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7963 {
7964 /* At any rate, check whether we're trying to assign a value to
7965 a final. */
7966 tree accessed = (JDECL_P (node) ? node :
7967 (TREE_CODE (node) == COMPONENT_REF ?
7968 TREE_OPERAND (node, 1) : node));
7969 if (check_final_assignment (accessed, wfl))
7970 return error_mark_node;
7971
7972 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7973 arg_type, name, arg, rhs);
7974 return java_complete_tree (node);
7975 }
7976 return NULL_TREE;
7977}
7978
7979/* Construct the expression that calls an access method:
7980 <type>.access$<n>(<arg1> [, <arg2>]);
7981
7982 ARG2 can be NULL and will be omitted in that case. It will denote a
7983 read access. */
7984
7985static tree
7986build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7987 int lc;
7988 tree type, access_method_name, arg1, arg2;
7989{
7990 tree args, cn, access;
7991
7992 args = arg1 ? arg1 :
7993 build_wfl_node (build_current_thisn (current_class));
7994 args = build_tree_list (NULL_TREE, args);
7995
7996 if (arg2)
7997 args = tree_cons (NULL_TREE, arg2, args);
7998
7999 access = build_method_invocation (build_wfl_node (access_method_name), args);
8000 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8001 return make_qualified_primary (cn, access, lc);
8002}
8003
8004static tree
8005build_new_access_id ()
8006{
8007 static int access_n_counter = 1;
8008 char buffer [128];
8009
8010 sprintf (buffer, "access$%d", access_n_counter++);
8011 return get_identifier (buffer);
8012}
8013
8014/* Create the static access functions for the outer field DECL. We define a
8015 read:
8016 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8017 return inst$.field;
8018 }
8019 and a write access:
8020 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8021 TREE_TYPE (<field>) value$) {
8022 return inst$.field = value$;
8023 }
8024 We should have a usage flags on the DECL so we can lazily turn the ones
8025 we're using for code generation. FIXME.
8026*/
8027
8028static tree
8029build_outer_field_access_methods (decl)
8030 tree decl;
8031{
8032 tree id, args, stmt, mdecl;
8033
8034 /* Check point, to be removed. FIXME */
8035 if (FIELD_INNER_ACCESS (decl)
8036 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
8037 abort ();
8038
8039 if (FIELD_INNER_ACCESS (decl))
8040 return FIELD_INNER_ACCESS (decl);
8041
c2952b01
APB
8042 /* Create the identifier and a function named after it. */
8043 id = build_new_access_id ();
8044
8045 /* The identifier is marked as bearing the name of a generated write
8046 access function for outer field accessed from inner classes. */
8047 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8048
8049 /* Create the read access */
8050 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8051 TREE_CHAIN (args) = end_params_node;
8052 stmt = make_qualified_primary (build_wfl_node (inst_id),
8053 build_wfl_node (DECL_NAME (decl)), 0);
8054 stmt = build_return (0, stmt);
8055 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8056 TREE_TYPE (decl), id, args, stmt);
8057 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8058
8059 /* Create the write access method */
8060 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8061 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8062 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8063 stmt = make_qualified_primary (build_wfl_node (inst_id),
8064 build_wfl_node (DECL_NAME (decl)), 0);
8065 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8066 build_wfl_node (wpv_id)));
8067
8068 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8069 TREE_TYPE (decl), id, args, stmt);
8070 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8071
8072 /* Return the access name */
8073 return FIELD_INNER_ACCESS (decl) = id;
8074}
8075
8076/* Build an field access method NAME. */
8077
8078static tree
8079build_outer_field_access_method (class, type, name, args, body)
8080 tree class, type, name, args, body;
8081{
8082 tree saved_current_function_decl, mdecl;
8083
8084 /* Create the method */
8085 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8086 fix_method_argument_names (args, mdecl);
8087 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8088
8089 /* Attach the method body. */
8090 saved_current_function_decl = current_function_decl;
8091 start_artificial_method_body (mdecl);
8092 java_method_add_stmt (mdecl, body);
8093 end_artificial_method_body (mdecl);
8094 current_function_decl = saved_current_function_decl;
8095
8096 return mdecl;
8097}
8098
8099\f
8100/* This section deals with building access function necessary for
8101 certain kinds of method invocation from inner classes. */
8102
8103static tree
8104build_outer_method_access_method (decl)
8105 tree decl;
8106{
8107 tree saved_current_function_decl, mdecl;
8108 tree args = NULL_TREE, call_args = NULL_TREE;
8109 tree carg, id, body, class;
8110 char buffer [80];
8111 int parm_id_count = 0;
8112
8113 /* Test this abort with an access to a private field */
8114 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8115 abort ();
8116
8117 /* Check the cache first */
8118 if (DECL_FUNCTION_INNER_ACCESS (decl))
8119 return DECL_FUNCTION_INNER_ACCESS (decl);
8120
8121 class = DECL_CONTEXT (decl);
8122
8123 /* Obtain an access identifier and mark it */
8124 id = build_new_access_id ();
8125 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8126
c2952b01
APB
8127 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8128 /* Create the arguments, as much as the original */
8129 for (; carg && carg != end_params_node;
8130 carg = TREE_CHAIN (carg))
8131 {
8132 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8133 args = chainon (args, build_tree_list (get_identifier (buffer),
8134 TREE_VALUE (carg)));
8135 }
8136 args = chainon (args, end_params_node);
8137
8138 /* Create the method */
8139 mdecl = create_artificial_method (class, ACC_STATIC,
8140 TREE_TYPE (TREE_TYPE (decl)), id, args);
8141 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8142 /* There is a potential bug here. We should be able to use
8143 fix_method_argument_names, but then arg names get mixed up and
8144 eventually a constructor will have its this$0 altered and the
8145 outer context won't be assignment properly. The test case is
8146 stub.java FIXME */
8147 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8148
8149 /* Attach the method body. */
8150 saved_current_function_decl = current_function_decl;
8151 start_artificial_method_body (mdecl);
8152
8153 /* The actual method invocation uses the same args. When invoking a
8154 static methods that way, we don't want to skip the first
8155 argument. */
8156 carg = args;
8157 if (!METHOD_STATIC (decl))
8158 carg = TREE_CHAIN (carg);
8159 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8160 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8161 call_args);
8162
8163 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8164 call_args);
8165 if (!METHOD_STATIC (decl))
8166 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8167 body, 0);
8168 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8169 body = build_return (0, body);
8170 java_method_add_stmt (mdecl,body);
8171 end_artificial_method_body (mdecl);
8172 current_function_decl = saved_current_function_decl;
c2952b01
APB
8173
8174 /* Back tag the access function so it know what it accesses */
8175 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8176
8177 /* Tag the current method so it knows it has an access generated */
8178 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8179}
8180
8181\f
8182/* This section of the code deals with building expressions to access
8183 the enclosing instance of an inner class. The enclosing instance is
8184 kept in a generated field called this$<n>, with <n> being the
8185 inner class nesting level (starting from 0.) */
8186
dba41d30
APB
8187/* Build an access to a given this$<n>, always chaining access call to
8188 others. Access methods to this$<n> are build on the fly if
8189 necessary. This CAN'T be used to solely access this$<n-1> from
8190 this$<n> (which alway yield to special cases and optimization, see
8191 for example build_outer_field_access). */
c2952b01
APB
8192
8193static tree
8194build_access_to_thisn (from, to, lc)
8195 tree from, to;
8196 int lc;
8197{
8198 tree access = NULL_TREE;
8199
8200 while (from != to)
8201 {
c2952b01 8202 if (!access)
dba41d30
APB
8203 {
8204 access = build_current_thisn (from);
8205 access = build_wfl_node (access);
8206 }
8207 else
c2952b01 8208 {
dba41d30
APB
8209 tree access0_wfl, cn;
8210
8211 maybe_build_thisn_access_method (from);
8212 access0_wfl = build_wfl_node (access0_identifier_node);
8213 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8214 EXPR_WFL_LINECOL (access0_wfl) = lc;
8215 access = build_tree_list (NULL_TREE, access);
8216 access = build_method_invocation (access0_wfl, access);
8217 access = make_qualified_primary (cn, access, lc);
c2952b01 8218 }
c2952b01
APB
8219
8220 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8221 }
8222 return access;
8223}
8224
8225/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8226 is returned if nothing needs to be generated. Otherwise, the method
152de068 8227 generated and a method decl is returned.
c2952b01
APB
8228
8229 NOTE: These generated methods should be declared in a class file
8230 attribute so that they can't be referred to directly. */
8231
8232static tree
8233maybe_build_thisn_access_method (type)
8234 tree type;
8235{
8236 tree mdecl, args, stmt, rtype;
8237 tree saved_current_function_decl;
8238
8239 /* If TYPE is a top-level class, no access method is required.
8240 If there already is such an access method, bail out. */
8241 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8242 return NULL_TREE;
8243
8244 /* We generate the method. The method looks like:
8245 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8246 */
c2952b01
APB
8247 args = build_tree_list (inst_id, build_pointer_type (type));
8248 TREE_CHAIN (args) = end_params_node;
8249 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8250 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8251 access0_identifier_node, args);
8252 fix_method_argument_names (args, mdecl);
8253 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8254 stmt = build_current_thisn (type);
8255 stmt = make_qualified_primary (build_wfl_node (inst_id),
8256 build_wfl_node (stmt), 0);
8257 stmt = build_return (0, stmt);
8258
8259 saved_current_function_decl = current_function_decl;
8260 start_artificial_method_body (mdecl);
8261 java_method_add_stmt (mdecl, stmt);
8262 end_artificial_method_body (mdecl);
8263 current_function_decl = saved_current_function_decl;
c2952b01
APB
8264
8265 CLASS_ACCESS0_GENERATED_P (type) = 1;
8266
8267 return mdecl;
8268}
8269
8270/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8271 the first level of innerclassing. this$1 for the next one, etc...
8272 This function can be invoked with TYPE to NULL, available and then
8273 has to count the parser context. */
8274
8275static tree
8276build_current_thisn (type)
8277 tree type;
8278{
8279 static int saved_i = -1;
8280 static tree saved_thisn = NULL_TREE;
19e223db
MM
8281 static tree saved_type = NULL_TREE;
8282 static int saved_type_i = 0;
8283 static int initialized_p;
c2952b01
APB
8284 tree decl;
8285 char buffer [80];
8286 int i = 0;
8287
19e223db
MM
8288 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8289 if (!initialized_p)
c2952b01 8290 {
19e223db
MM
8291 ggc_add_tree_root (&saved_thisn, 1);
8292 ggc_add_tree_root (&saved_type, 1);
8293 initialized_p = 1;
8294 }
c2952b01 8295
19e223db
MM
8296 if (type)
8297 {
c2952b01
APB
8298 if (type == saved_type)
8299 i = saved_type_i;
8300 else
8301 {
8302 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8303 decl; decl = DECL_CONTEXT (decl), i++)
8304 ;
8305
8306 saved_type = type;
8307 saved_type_i = i;
8308 }
8309 }
8310 else
8311 i = list_length (GET_CPC_LIST ())-2;
8312
8313 if (i == saved_i)
8314 return saved_thisn;
8315
8316 sprintf (buffer, "this$%d", i);
8317 saved_i = i;
8318 saved_thisn = get_identifier (buffer);
8319 return saved_thisn;
8320}
8321
8322/* Return the assignement to the hidden enclosing context `this$<n>'
8323 by the second incoming parameter to the innerclass constructor. The
8324 form used is `this.this$<n> = this$<n>;'. */
8325
8326static tree
8327build_thisn_assign ()
8328{
8329 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8330 {
8331 tree thisn = build_current_thisn (current_class);
8332 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8333 build_wfl_node (thisn), 0);
8334 tree rhs = build_wfl_node (thisn);
8335 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8336 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8337 }
8338 return NULL_TREE;
8339}
8340
8341\f
165f37bc
APB
8342/* Building the synthetic `class$' used to implement the `.class' 1.1
8343 extension for non primitive types. This method looks like:
8344
8345 static Class class$(String type) throws NoClassDefFoundError
8346 {
8347 try {return (java.lang.Class.forName (String));}
8348 catch (ClassNotFoundException e) {
8349 throw new NoClassDefFoundError(e.getMessage());}
8350 } */
8351
8352static tree
8353build_dot_class_method (class)
8354 tree class;
8355{
8356#define BWF(S) build_wfl_node (get_identifier ((S)))
8357#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8358 tree args, tmp, saved_current_function_decl, mdecl;
8359 tree stmt, throw_stmt, catch, catch_block, try_block;
8360 tree catch_clause_param;
8361 tree class_not_found_exception, no_class_def_found_error;
8362
8363 static tree get_message_wfl, type_parm_wfl;
8364
8365 if (!get_message_wfl)
8366 {
8367 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8368 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8369 ggc_add_tree_root (&get_message_wfl, 1);
8370 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8371 }
8372
8373 /* Build the arguments */
8374 args = build_tree_list (get_identifier ("type$"),
8375 build_pointer_type (string_type_node));
8376 TREE_CHAIN (args) = end_params_node;
8377
8378 /* Build the qualified name java.lang.Class.forName */
8379 tmp = MQN (MQN (MQN (BWF ("java"),
8380 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8381
8382 /* For things we have to catch and throw */
8383 class_not_found_exception =
8384 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8385 no_class_def_found_error =
8386 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8387 load_class (class_not_found_exception, 1);
8388 load_class (no_class_def_found_error, 1);
8389
8390 /* Create the "class$" function */
8391 mdecl = create_artificial_method (class, ACC_STATIC,
8392 build_pointer_type (class_type_node),
8393 get_identifier ("class$"), args);
8394 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8395 no_class_def_found_error);
8396
8397 /* We start by building the try block. We need to build:
8398 return (java.lang.Class.forName (type)); */
8399 stmt = build_method_invocation (tmp,
8400 build_tree_list (NULL_TREE, type_parm_wfl));
8401 stmt = build_return (0, stmt);
8402 /* Put it in a block. That's the try block */
8403 try_block = build_expr_block (stmt, NULL_TREE);
8404
8405 /* Now onto the catch block. We start by building the expression
8406 throwing a new exception:
8407 throw new NoClassDefFoundError (_.getMessage); */
8408 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8409 get_message_wfl, 0);
8410 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8411
8412 /* Build new NoClassDefFoundError (_.getMessage) */
8413 throw_stmt = build_new_invocation
8414 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8415 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8416
8417 /* Build the throw, (it's too early to use BUILD_THROW) */
8418 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8419
8420 /* Build the catch block to encapsulate all this. We begin by
8421 building an decl for the catch clause parameter and link it to
8422 newly created block, the catch block. */
8423 catch_clause_param =
8424 build_decl (VAR_DECL, wpv_id,
8425 build_pointer_type (class_not_found_exception));
8426 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8427
8428 /* We initialize the variable with the exception handler. */
8429 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8430 soft_exceptioninfo_call_node);
8431 add_stmt_to_block (catch_block, NULL_TREE, catch);
8432
8433 /* We add the statement throwing the new exception */
8434 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8435
8436 /* Build a catch expression for all this */
8437 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8438
8439 /* Build the try/catch sequence */
8440 stmt = build_try_statement (0, try_block, catch_block);
8441
8442 fix_method_argument_names (args, mdecl);
8443 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8444 saved_current_function_decl = current_function_decl;
8445 start_artificial_method_body (mdecl);
8446 java_method_add_stmt (mdecl, stmt);
8447 end_artificial_method_body (mdecl);
8448 current_function_decl = saved_current_function_decl;
8449 TYPE_DOT_CLASS (class) = mdecl;
8450
8451 return mdecl;
8452}
8453
8454static tree
f0f3a777
APB
8455build_dot_class_method_invocation (type)
8456 tree type;
165f37bc 8457{
f0f3a777
APB
8458 tree sig_id, s;
8459
8460 if (TYPE_ARRAY_P (type))
8461 sig_id = build_java_signature (type);
8462 else
8463 sig_id = DECL_NAME (TYPE_NAME (type));
8464
1f8f4a0b
MM
8465 s = build_string (IDENTIFIER_LENGTH (sig_id),
8466 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8467 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8468 build_tree_list (NULL_TREE, s));
8469}
8470
c2952b01
APB
8471/* This section of the code deals with constructor. */
8472
22eed1e6
APB
8473/* Craft a body for default constructor. Patch existing constructor
8474 bodies with call to super() and field initialization statements if
8475 necessary. */
8476
8477static void
8478fix_constructors (mdecl)
8479 tree mdecl;
8480{
8481 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8482 tree thisn_assign, compound = NULL_TREE;
8483 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8484
22eed1e6
APB
8485 if (!body)
8486 {
22eed1e6
APB
8487 /* It is an error for the compiler to generate a default
8488 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8489 takes no argument, or the same args for an anonymous class */
8490 if (verify_constructor_super (mdecl))
22eed1e6 8491 {
c2952b01
APB
8492 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8493 tree save = DECL_NAME (mdecl);
49f48c71 8494 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8495 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8496 parse_error_context
c2952b01
APB
8497 (lookup_cl (TYPE_NAME (class_type)),
8498 "No constructor matching `%s' found in class `%s'",
8499 lang_printable_name (mdecl, 0), n);
8500 DECL_NAME (mdecl) = save;
22eed1e6
APB
8501 }
8502
c2952b01
APB
8503 /* The constructor body must be crafted by hand. It's the
8504 constructor we defined when we realize we didn't have the
8505 CLASSNAME() constructor */
22eed1e6
APB
8506 start_artificial_method_body (mdecl);
8507
f0f3a777
APB
8508 /* Insert an assignment to the this$<n> hidden field, if
8509 necessary */
8510 if ((thisn_assign = build_thisn_assign ()))
8511 java_method_add_stmt (mdecl, thisn_assign);
8512
22eed1e6
APB
8513 /* We don't generate a super constructor invocation if we're
8514 compiling java.lang.Object. build_super_invocation takes care
8515 of that. */
e920ebc9 8516 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8517
c2952b01
APB
8518 /* Insert the instance initializer block right here, after the
8519 super invocation. */
8520 add_instance_initializer (mdecl);
8521
22eed1e6
APB
8522 end_artificial_method_body (mdecl);
8523 }
8524 /* Search for an explicit constructor invocation */
8525 else
8526 {
8527 int found = 0;
8528 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8529
8530 while (body)
8531 switch (TREE_CODE (body))
8532 {
8533 case CALL_EXPR:
8534 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8535 body = NULL_TREE;
8536 break;
8537 case COMPOUND_EXPR:
8538 case EXPR_WITH_FILE_LOCATION:
8539 body = TREE_OPERAND (body, 0);
8540 break;
8541 case BLOCK:
8542 body = BLOCK_EXPR_BODY (body);
8543 break;
8544 default:
8545 found = 0;
8546 body = NULL_TREE;
8547 }
8548 /* The constructor is missing an invocation of super() */
8549 if (!found)
8550 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8551 build_super_invocation (mdecl));
22eed1e6 8552
c2952b01
APB
8553 /* Generate the assignment to this$<n>, if necessary */
8554 if ((thisn_assign = build_thisn_assign ()))
8555 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8556
f0f3a777
APB
8557 /* Insert the instance initializer block right here, after the
8558 super invocation. */
8559 add_instance_initializer (mdecl);
8560
22eed1e6
APB
8561 /* Fix the constructor main block if we're adding extra stmts */
8562 if (compound)
8563 {
8564 compound = add_stmt_to_compound (compound, NULL_TREE,
8565 BLOCK_EXPR_BODY (main_block));
8566 BLOCK_EXPR_BODY (main_block) = compound;
8567 }
8568 }
8569}
8570
8571/* Browse constructors in the super class, searching for a constructor
8572 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8573 otherwise. If the current class is an anonymous inner class, look
8574 for something that has the same signature. */
22eed1e6
APB
8575
8576static int
c2952b01
APB
8577verify_constructor_super (mdecl)
8578 tree mdecl;
22eed1e6
APB
8579{
8580 tree class = CLASSTYPE_SUPER (current_class);
152de068 8581 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8582 tree sdecl;
8583
22eed1e6
APB
8584 if (!class)
8585 return 0;
8586
c2952b01 8587 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8588 {
c2952b01
APB
8589 tree mdecl_arg_type;
8590 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8591 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8592 if (DECL_CONSTRUCTOR_P (sdecl))
8593 {
cf1b2274 8594 tree m_arg_type;
152de068
APB
8595 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8596 if (super_inner)
8597 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8598 for (m_arg_type = mdecl_arg_type;
8599 (arg_type != end_params_node
8600 && m_arg_type != end_params_node);
c2952b01 8601 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8602 m_arg_type = TREE_CHAIN (m_arg_type))
8603 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8604 break;
8605
cf1b2274 8606 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8607 return 0;
8608 }
8609 }
8610 else
8611 {
8612 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8613 {
152de068
APB
8614 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8615 if (super_inner)
8616 arg = TREE_CHAIN (arg);
8617 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8618 return 0;
8619 }
8620 }
8621 return 1;
8622}
8623
22eed1e6 8624/* Generate code for all context remembered for code generation. */
b351b287
APB
8625
8626void
8627java_expand_classes ()
8628{
5423609c 8629 int save_error_count = 0;
c2952b01
APB
8630 static struct parser_ctxt *saved_ctxp = NULL;
8631
23a79c61
APB
8632 java_parse_abort_on_error ();
8633 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8634 return;
8635 java_layout_classes ();
8636 java_parse_abort_on_error ();
8637
c2952b01 8638 saved_ctxp = ctxp_for_generation;
b351b287
APB
8639 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8640 {
8641 ctxp = ctxp_for_generation;
8642 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8643 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8644 java_parse_abort_on_error ();
8645 }
c2952b01
APB
8646
8647 /* Find anonymous classes and expand their constructor, now they
8648 have been fixed. */
8649 for (ctxp_for_generation = saved_ctxp;
8650 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8651 {
8652 tree current;
8653 ctxp = ctxp_for_generation;
8654 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8655 {
8656 current_class = TREE_TYPE (current);
8657 if (ANONYMOUS_CLASS_P (current_class))
8658 {
8659 tree d;
8660 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8661 {
8662 if (DECL_CONSTRUCTOR_P (d))
8663 {
8664 restore_line_number_status (1);
8665 reset_method_name (d);
8666 java_complete_expand_method (d);
8667 restore_line_number_status (0);
8668 break; /* We now there are no other ones */
8669 }
8670 }
8671 }
8672 }
8673 }
8674
8675 /* If we've found error at that stage, don't try to generate
8676 anything, unless we're emitting xrefs or checking the syntax only
8677 (but not using -fsyntax-only for the purpose of generating
8678 bytecode. */
8679 if (java_error_count && !flag_emit_xref
8680 && (!flag_syntax_only && !flag_emit_class_files))
8681 return;
8682
8683 /* Now things are stable, go for generation of the class data. */
8684 for (ctxp_for_generation = saved_ctxp;
8685 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8686 {
8687 tree current;
8688 ctxp = ctxp_for_generation;
8689 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8690 {
8691 current_class = TREE_TYPE (current);
8692 outgoing_cpool = TYPE_CPOOL (current_class);
8693 if (flag_emit_class_files)
8694 write_classfile (current_class);
8695 if (flag_emit_xref)
8696 expand_xref (current_class);
8697 else if (! flag_syntax_only)
8698 finish_class ();
8699 }
8700 }
b351b287
APB
8701}
8702
e04a16fb
AG
8703/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8704 a tree list node containing RIGHT. Fore coming RIGHTs will be
8705 chained to this hook. LOCATION contains the location of the
8706 separating `.' operator. */
8707
8708static tree
8709make_qualified_primary (primary, right, location)
8710 tree primary, right;
8711 int location;
8712{
8713 tree wfl;
8714
c2952b01 8715 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8716 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8717 else
8718 {
8719 wfl = primary;
c2952b01
APB
8720 /* If wfl wasn't qualified, we build a first anchor */
8721 if (!EXPR_WFL_QUALIFICATION (wfl))
8722 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8723 }
8724
c2952b01 8725 /* And chain them */
e04a16fb
AG
8726 EXPR_WFL_LINECOL (right) = location;
8727 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8728 PRIMARY_P (wfl) = 1;
8729 return wfl;
8730}
8731
8732/* Simple merge of two name separated by a `.' */
8733
8734static tree
8735merge_qualified_name (left, right)
8736 tree left, right;
8737{
8738 tree node;
c2952b01
APB
8739 if (!left && !right)
8740 return NULL_TREE;
8741
8742 if (!left)
8743 return right;
8744
8745 if (!right)
8746 return left;
8747
e04a16fb
AG
8748 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8749 IDENTIFIER_LENGTH (left));
8750 obstack_1grow (&temporary_obstack, '.');
8751 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8752 IDENTIFIER_LENGTH (right));
8753 node = get_identifier (obstack_base (&temporary_obstack));
8754 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8755 QUALIFIED_P (node) = 1;
8756 return node;
8757}
8758
8759/* Merge the two parts of a qualified name into LEFT. Set the
8760 location information of the resulting node to LOCATION, usually
8761 inherited from the location information of the `.' operator. */
8762
8763static tree
8764make_qualified_name (left, right, location)
8765 tree left, right;
8766 int location;
8767{
bc3ca41b
PB
8768#ifdef USE_COMPONENT_REF
8769 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8770 EXPR_WFL_LINECOL (node) = location;
8771 return node;
8772#else
e04a16fb
AG
8773 tree left_id = EXPR_WFL_NODE (left);
8774 tree right_id = EXPR_WFL_NODE (right);
8775 tree wfl, merge;
8776
8777 merge = merge_qualified_name (left_id, right_id);
8778
8779 /* Left wasn't qualified and is now qualified */
8780 if (!QUALIFIED_P (left_id))
8781 {
8782 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8783 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8784 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8785 }
8786
8787 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8788 EXPR_WFL_LINECOL (wfl) = location;
8789 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8790
8791 EXPR_WFL_NODE (left) = merge;
8792 return left;
bc3ca41b 8793#endif
e04a16fb
AG
8794}
8795
8796/* Extract the last identifier component of the qualified in WFL. The
8797 last identifier is removed from the linked list */
8798
8799static tree
8800cut_identifier_in_qualified (wfl)
8801 tree wfl;
8802{
8803 tree q;
8804 tree previous = NULL_TREE;
8805 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8806 if (!TREE_CHAIN (q))
8807 {
8808 if (!previous)
781b0558 8809 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8810 TREE_CHAIN (previous) = NULL_TREE;
8811 return TREE_PURPOSE (q);
8812 }
8813}
8814
8815/* Resolve the expression name NAME. Return its decl. */
8816
8817static tree
5e942c50 8818resolve_expression_name (id, orig)
e04a16fb 8819 tree id;
5e942c50 8820 tree *orig;
e04a16fb
AG
8821{
8822 tree name = EXPR_WFL_NODE (id);
8823 tree decl;
8824
8825 /* 6.5.5.1: Simple expression names */
8826 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8827 {
8828 /* 15.13.1: NAME can appear within the scope of a local variable
8829 declaration */
8830 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8831 return decl;
8832
8833 /* 15.13.1: NAME can appear within a class declaration */
8834 else
8835 {
8836 decl = lookup_field_wrapper (current_class, name);
8837 if (decl)
8838 {
c2952b01 8839 tree access = NULL_TREE;
e04a16fb 8840 int fs = FIELD_STATIC (decl);
f2760b27
APB
8841
8842 /* If we're accessing an outer scope local alias, make
8843 sure we change the name of the field we're going to
8844 build access to. */
8845 if (FIELD_LOCAL_ALIAS_USED (decl))
8846 name = DECL_NAME (decl);
8847
e04a16fb
AG
8848 /* Instance variable (8.3.1.1) can't appear within
8849 static method, static initializer or initializer for
8850 a static variable. */
8851 if (!fs && METHOD_STATIC (current_function_decl))
8852 {
7f10c2e2 8853 static_ref_err (id, name, current_class);
e04a16fb
AG
8854 return error_mark_node;
8855 }
22eed1e6
APB
8856 /* Instance variables can't appear as an argument of
8857 an explicit constructor invocation */
8858 if (!fs && ctxp->explicit_constructor_p)
8859 {
8860 parse_error_context
781b0558 8861 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8862 return error_mark_node;
8863 }
5e942c50 8864
c2952b01
APB
8865 /* If we're processing an inner class and we're trying
8866 to access a field belonging to an outer class, build
8867 the access to the field */
8868 if (!fs && outer_field_access_p (current_class, decl))
8869 return build_outer_field_access (id, decl);
8870
5e942c50 8871 /* Otherwise build what it takes to access the field */
c2952b01
APB
8872 access = build_field_ref ((fs ? NULL_TREE : current_this),
8873 DECL_CONTEXT (decl), name);
e8fc7396 8874 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8875 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8876 /* We may be asked to save the real field access node */
8877 if (orig)
c2952b01 8878 *orig = access;
5e942c50 8879 /* And we return what we got */
c2952b01 8880 return access;
e04a16fb
AG
8881 }
8882 /* Fall down to error report on undefined variable */
8883 }
8884 }
8885 /* 6.5.5.2 Qualified Expression Names */
8886 else
8887 {
5e942c50
APB
8888 if (orig)
8889 *orig = NULL_TREE;
e04a16fb
AG
8890 qualify_ambiguous_name (id);
8891 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8892 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8893 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8894 }
8895
8896 /* We've got an error here */
8897 parse_error_context (id, "Undefined variable `%s'",
8898 IDENTIFIER_POINTER (name));
8899
8900 return error_mark_node;
8901}
8902
7f10c2e2
APB
8903static void
8904static_ref_err (wfl, field_id, class_type)
8905 tree wfl, field_id, class_type;
8906{
8907 parse_error_context
8908 (wfl,
8909 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8910 IDENTIFIER_POINTER (field_id),
8911 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8912}
8913
e04a16fb
AG
8914/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8915 We return something suitable to generate the field access. We also
8916 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8917 recipient's address can be null. */
8918
8919static tree
8920resolve_field_access (qual_wfl, field_decl, field_type)
8921 tree qual_wfl;
8922 tree *field_decl, *field_type;
8923{
8924 int is_static = 0;
8925 tree field_ref;
8926 tree decl, where_found, type_found;
8927
8928 if (resolve_qualified_expression_name (qual_wfl, &decl,
8929 &where_found, &type_found))
8930 return error_mark_node;
8931
8932 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8933 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8934 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8935 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8936 {
8937 tree length = build_java_array_length_access (where_found);
8938 field_ref =
8939 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8940
8941 /* In case we're dealing with a static array, we need to
8942 initialize its class before the array length can be fetched.
8943 It's also a good time to create a DECL_RTL for the field if
8944 none already exists, otherwise if the field was declared in a
8945 class found in an external file and hasn't been (and won't
8946 be) accessed for its value, none will be created. */
8947 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8948 {
8949 build_static_field_ref (where_found);
8950 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8951 }
e04a16fb
AG
8952 }
8953 /* We might have been trying to resolve field.method(). In which
8954 case, the resolution is over and decl is the answer */
34f4db93 8955 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8956 field_ref = decl;
34f4db93 8957 else if (JDECL_P (decl))
e04a16fb 8958 {
5e942c50
APB
8959 int static_final_found = 0;
8960 if (!type_found)
8961 type_found = DECL_CONTEXT (decl);
34f4db93 8962 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8963 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8964 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8965 && DECL_INITIAL (decl))
5e942c50 8966 {
0c2b8145
APB
8967 /* When called on a FIELD_DECL of the right (primitive)
8968 type, java_complete_tree will try to substitue the decl
8969 for it's initial value. */
70541f45 8970 field_ref = java_complete_tree (decl);
5e942c50
APB
8971 static_final_found = 1;
8972 }
8973 else
7f10c2e2
APB
8974 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8975 NULL_TREE : where_found),
5e942c50 8976 type_found, DECL_NAME (decl));
e04a16fb
AG
8977 if (field_ref == error_mark_node)
8978 return error_mark_node;
e8fc7396
APB
8979 if (is_static && !static_final_found
8980 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8981 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8982 }
8983 else
8984 field_ref = decl;
8985
8986 if (field_decl)
8987 *field_decl = decl;
8988 if (field_type)
c877974e
APB
8989 *field_type = (QUAL_DECL_TYPE (decl) ?
8990 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8991 return field_ref;
8992}
8993
e28cd97b
APB
8994/* If NODE is an access to f static field, strip out the class
8995 initialization part and return the field decl, otherwise, return
8996 NODE. */
8997
8998static tree
8999strip_out_static_field_access_decl (node)
9000 tree node;
9001{
9002 if (TREE_CODE (node) == COMPOUND_EXPR)
9003 {
9004 tree op1 = TREE_OPERAND (node, 1);
9005 if (TREE_CODE (op1) == COMPOUND_EXPR)
9006 {
9007 tree call = TREE_OPERAND (op1, 0);
9008 if (TREE_CODE (call) == CALL_EXPR
9009 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9010 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9011 == soft_initclass_node)
9012 return TREE_OPERAND (op1, 1);
9013 }
2f11d407
TT
9014 else if (JDECL_P (op1))
9015 return op1;
e28cd97b
APB
9016 }
9017 return node;
9018}
9019
e04a16fb
AG
9020/* 6.5.5.2: Qualified Expression Names */
9021
9022static int
9023resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9024 tree wfl;
9025 tree *found_decl, *type_found, *where_found;
9026{
9027 int from_type = 0; /* Field search initiated from a type */
c2952b01 9028 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9029 int previous_call_static = 0;
9030 int is_static;
9031 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9032 /* For certain for of inner class instantiation */
9033 tree saved_current, saved_this;
9034#define RESTORE_THIS_AND_CURRENT_CLASS \
9035 { current_class = saved_current; current_this = saved_this;}
9036
c877974e 9037 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9038
9039 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9040 {
9041 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9042 tree ret_decl; /* for EH checking */
9043 int location; /* for EH checking */
e04a16fb
AG
9044
9045 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9046 switch (TREE_CODE (qual_wfl))
9047 {
9048 case CALL_EXPR:
b67d701b 9049 case NEW_CLASS_EXPR:
e04a16fb
AG
9050 /* If the access to the function call is a non static field,
9051 build the code to access it. */
34f4db93 9052 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9053 {
ac825856
APB
9054 decl = maybe_access_field (decl, *where_found,
9055 DECL_CONTEXT (decl));
e04a16fb
AG
9056 if (decl == error_mark_node)
9057 return 1;
9058 }
c2952b01 9059
e04a16fb
AG
9060 /* And code for the function call */
9061 if (complete_function_arguments (qual_wfl))
9062 return 1;
c2952b01
APB
9063
9064 /* We might have to setup a new current class and a new this
9065 for the search of an inner class, relative to the type of
9066 a expression resolved as `decl'. The current values are
9067 saved and restored shortly after */
9068 saved_current = current_class;
9069 saved_this = current_this;
dba41d30
APB
9070 if (decl
9071 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9072 || from_qualified_this))
c2952b01 9073 {
dba41d30
APB
9074 /* If we still have `from_qualified_this', we have the form
9075 <T>.this.f() and we need to build <T>.this */
9076 if (from_qualified_this)
9077 {
9078 decl = build_access_to_thisn (current_class, type, 0);
9079 decl = java_complete_tree (decl);
9080 type = TREE_TYPE (TREE_TYPE (decl));
9081 }
c2952b01
APB
9082 current_class = type;
9083 current_this = decl;
dba41d30 9084 from_qualified_this = 0;
c2952b01
APB
9085 }
9086
89e09b9a
PB
9087 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9088 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9089 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9090 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9091 *where_found = patch_method_invocation (qual_wfl, decl, type,
9092 &is_static, &ret_decl);
e04a16fb 9093 if (*where_found == error_mark_node)
c2952b01
APB
9094 {
9095 RESTORE_THIS_AND_CURRENT_CLASS;
9096 return 1;
9097 }
e04a16fb
AG
9098 *type_found = type = QUAL_DECL_TYPE (*where_found);
9099
c2952b01
APB
9100 /* If we're creating an inner class instance, check for that
9101 an enclosing instance is in scope */
9102 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9103 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9104 {
9105 parse_error_context
165f37bc
APB
9106 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9107 lang_printable_name (type, 0),
9108 (!current_this ? "" :
9109 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9110 RESTORE_THIS_AND_CURRENT_CLASS;
9111 return 1;
9112 }
9113
9114 /* In case we had to change then to resolve a inner class
9115 instantiation using a primary qualified by a `new' */
9116 RESTORE_THIS_AND_CURRENT_CLASS;
9117
34d4df06
APB
9118 /* EH check. No check on access$<n> functions */
9119 if (location
9120 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9121 (DECL_NAME (current_function_decl)))
7705e9db
APB
9122 check_thrown_exceptions (location, ret_decl);
9123
e04a16fb
AG
9124 /* If the previous call was static and this one is too,
9125 build a compound expression to hold the two (because in
9126 that case, previous function calls aren't transported as
9127 forcoming function's argument. */
9128 if (previous_call_static && is_static)
9129 {
9130 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9131 TREE_SIDE_EFFECTS (decl) = 1;
9132 }
9133 else
9134 {
9135 previous_call_static = is_static;
9136 decl = *where_found;
9137 }
c2952b01 9138 from_type = 0;
e04a16fb
AG
9139 continue;
9140
d8fccff5 9141 case NEW_ARRAY_EXPR:
c2952b01 9142 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9143 *where_found = decl = java_complete_tree (qual_wfl);
9144 if (decl == error_mark_node)
9145 return 1;
9146 *type_found = type = QUAL_DECL_TYPE (decl);
9147 CLASS_LOADED_P (type) = 1;
9148 continue;
9149
e04a16fb
AG
9150 case CONVERT_EXPR:
9151 *where_found = decl = java_complete_tree (qual_wfl);
9152 if (decl == error_mark_node)
9153 return 1;
9154 *type_found = type = QUAL_DECL_TYPE (decl);
9155 from_cast = 1;
9156 continue;
9157
22eed1e6 9158 case CONDITIONAL_EXPR:
5e942c50 9159 case STRING_CST:
ac22f9cb 9160 case MODIFY_EXPR:
22eed1e6
APB
9161 *where_found = decl = java_complete_tree (qual_wfl);
9162 if (decl == error_mark_node)
9163 return 1;
9164 *type_found = type = QUAL_DECL_TYPE (decl);
9165 continue;
9166
e04a16fb
AG
9167 case ARRAY_REF:
9168 /* If the access to the function call is a non static field,
9169 build the code to access it. */
34f4db93 9170 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9171 {
9172 decl = maybe_access_field (decl, *where_found, type);
9173 if (decl == error_mark_node)
9174 return 1;
9175 }
9176 /* And code for the array reference expression */
9177 decl = java_complete_tree (qual_wfl);
9178 if (decl == error_mark_node)
9179 return 1;
9180 type = QUAL_DECL_TYPE (decl);
9181 continue;
0a2138e2 9182
37feda7d
APB
9183 case PLUS_EXPR:
9184 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9185 return 1;
9186 if ((type = patch_string (decl)))
9187 decl = type;
9188 *where_found = QUAL_RESOLUTION (q) = decl;
9189 *type_found = type = TREE_TYPE (decl);
9190 break;
9191
165f37bc
APB
9192 case CLASS_LITERAL:
9193 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9194 return 1;
9195 *where_found = QUAL_RESOLUTION (q) = decl;
9196 *type_found = type = TREE_TYPE (decl);
9197 break;
9198
0a2138e2
APB
9199 default:
9200 /* Fix for -Wall Just go to the next statement. Don't
9201 continue */
a3f406ce 9202 break;
e04a16fb
AG
9203 }
9204
9205 /* If we fall here, we weren't processing a (static) function call. */
9206 previous_call_static = 0;
9207
9208 /* It can be the keyword THIS */
9209 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9210 {
9211 if (!current_this)
9212 {
9213 parse_error_context
9214 (wfl, "Keyword `this' used outside allowed context");
9215 return 1;
9216 }
f63991a8
APB
9217 if (ctxp->explicit_constructor_p)
9218 {
781b0558 9219 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9220 return 1;
9221 }
e04a16fb 9222 /* We have to generate code for intermediate acess */
c2952b01
APB
9223 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9224 {
9225 *where_found = decl = current_this;
9226 *type_found = type = QUAL_DECL_TYPE (decl);
9227 }
4dbf4496
APB
9228 /* We're trying to access the this from somewhere else. Make sure
9229 it's allowed before doing so. */
c2952b01
APB
9230 else
9231 {
4dbf4496
APB
9232 if (!enclosing_context_p (type, current_class))
9233 {
9234 char *p = xstrdup (lang_printable_name (type, 0));
9235 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9236 p, p,
9237 lang_printable_name (current_class, 0));
9238 free (p);
9239 return 1;
9240 }
c2952b01 9241 from_qualified_this = 1;
dba41d30
APB
9242 /* If there's nothing else after that, we need to
9243 produce something now, otherwise, the section of the
9244 code that needs to produce <T>.this will generate
9245 what is necessary. */
9246 if (!TREE_CHAIN (q))
9247 {
9248 decl = build_access_to_thisn (current_class, type, 0);
9249 *where_found = decl = java_complete_tree (decl);
9250 *type_found = type = TREE_TYPE (decl);
9251 }
c2952b01
APB
9252 }
9253
9254 from_type = 0;
e04a16fb
AG
9255 continue;
9256 }
9257
9258 /* 15.10.2 Accessing Superclass Members using SUPER */
9259 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9260 {
9261 tree node;
9262 /* Check on the restricted use of SUPER */
9263 if (METHOD_STATIC (current_function_decl)
9264 || current_class == object_type_node)
9265 {
9266 parse_error_context
9267 (wfl, "Keyword `super' used outside allowed context");
9268 return 1;
9269 }
9270 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9271 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9272 CLASSTYPE_SUPER (current_class),
9273 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9274 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9275 if (decl == error_mark_node)
9276 return 1;
e04a16fb
AG
9277 *type_found = type = QUAL_DECL_TYPE (decl);
9278 from_super = from_type = 1;
9279 continue;
9280 }
9281
9282 /* 15.13.1: Can't search for field name in packages, so we
9283 assume a variable/class name was meant. */
9284 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9285 {
5e942c50
APB
9286 tree name = resolve_package (wfl, &q);
9287 if (name)
9288 {
c2952b01 9289 tree list;
5e942c50 9290 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9291 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9292 out. We're going to search something inside it. */
9293 *type_found = type = TREE_TYPE (decl);
9294 layout_class (type);
9295 from_type = 1;
c2952b01 9296
dde1da72
APB
9297 /* Fix them all the way down, if any are left. */
9298 if (q)
c2952b01 9299 {
dde1da72
APB
9300 list = TREE_CHAIN (q);
9301 while (list)
9302 {
9303 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9304 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9305 list = TREE_CHAIN (list);
9306 }
c2952b01 9307 }
5e942c50 9308 }
e04a16fb 9309 else
5e942c50
APB
9310 {
9311 if (from_super || from_cast)
9312 parse_error_context
9313 ((from_cast ? qual_wfl : wfl),
9314 "No variable `%s' defined in class `%s'",
9315 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9316 lang_printable_name (type, 0));
9317 else
9318 parse_error_context
9319 (qual_wfl, "Undefined variable or class name: `%s'",
9320 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9321 return 1;
9322 }
e04a16fb
AG
9323 }
9324
9325 /* We have a type name. It's been already resolved when the
9326 expression was qualified. */
9327 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9328 {
9329 if (!(decl = QUAL_RESOLUTION (q)))
9330 return 1; /* Error reported already */
9331
c2952b01
APB
9332 /* Sneak preview. If next we see a `new', we're facing a
9333 qualification with resulted in a type being selected
9334 instead of a field. Report the error */
9335 if(TREE_CHAIN (q)
9336 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9337 {
9338 parse_error_context (qual_wfl, "Undefined variable `%s'",
9339 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9340 return 1;
9341 }
9342
e04a16fb
AG
9343 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9344 {
9345 parse_error_context
9346 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9347 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9348 GET_TYPE_NAME (type),
e04a16fb
AG
9349 IDENTIFIER_POINTER (DECL_NAME (decl)),
9350 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9351 return 1;
9352 }
5e942c50 9353 check_deprecation (qual_wfl, decl);
c2952b01 9354
e04a16fb
AG
9355 type = TREE_TYPE (decl);
9356 from_type = 1;
9357 }
9358 /* We resolve and expression name */
9359 else
9360 {
cd531a2e 9361 tree field_decl = NULL_TREE;
e04a16fb
AG
9362
9363 /* If there exists an early resolution, use it. That occurs
9364 only once and we know that there are more things to
9365 come. Don't do that when processing something after SUPER
9366 (we need more thing to be put in place below */
9367 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9368 {
9369 decl = QUAL_RESOLUTION (q);
c877974e 9370 if (!type)
5e942c50 9371 {
7f10c2e2
APB
9372 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9373 {
9374 if (current_this)
9375 *where_found = current_this;
9376 else
9377 {
9378 static_ref_err (qual_wfl, DECL_NAME (decl),
9379 current_class);
9380 return 1;
9381 }
f0f3a777
APB
9382 if (outer_field_access_p (current_class, decl))
9383 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9384 }
c877974e
APB
9385 else
9386 {
9387 *where_found = TREE_TYPE (decl);
9388 if (TREE_CODE (*where_found) == POINTER_TYPE)
9389 *where_found = TREE_TYPE (*where_found);
9390 }
5e942c50 9391 }
b67d701b 9392 }
e04a16fb
AG
9393
9394 /* We have to search for a field, knowing the type of its
9395 container. The flag FROM_TYPE indicates that we resolved
9396 the last member of the expression as a type name, which
5e942c50
APB
9397 means that for the resolution of this field, we'll look
9398 for other errors than if it was resolved as a member of
9399 an other field. */
e04a16fb
AG
9400 else
9401 {
9402 int is_static;
5e942c50
APB
9403 tree field_decl_type; /* For layout */
9404
e04a16fb
AG
9405 if (!from_type && !JREFERENCE_TYPE_P (type))
9406 {
9407 parse_error_context
9408 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9409 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9410 lang_printable_name (type, 0),
e04a16fb
AG
9411 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9412 return 1;
9413 }
9414
dc0b3eff
PB
9415 field_decl = lookup_field_wrapper (type,
9416 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9417
9418 /* Maybe what we're trying to access an inner class. */
9419 if (!field_decl)
9420 {
9421 tree ptr, inner_decl;
9422
9423 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9424 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9425 if (inner_decl)
9426 {
9427 check_inner_class_access (inner_decl, decl, qual_wfl);
9428 type = TREE_TYPE (inner_decl);
9429 decl = inner_decl;
9430 from_type = 1;
9431 continue;
9432 }
9433 }
9434
dc0b3eff 9435 if (field_decl == NULL_TREE)
e04a16fb
AG
9436 {
9437 parse_error_context
2aa11e97 9438 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9439 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9440 GET_TYPE_NAME (type));
e04a16fb
AG
9441 return 1;
9442 }
dc0b3eff
PB
9443 if (field_decl == error_mark_node)
9444 return 1;
5e942c50
APB
9445
9446 /* Layout the type of field_decl, since we may need
c877974e
APB
9447 it. Don't do primitive types or loaded classes. The
9448 situation of non primitive arrays may not handled
9449 properly here. FIXME */
5e942c50
APB
9450 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9451 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9452 else
9453 field_decl_type = TREE_TYPE (field_decl);
9454 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9455 && !CLASS_LOADED_P (field_decl_type)
9456 && !TYPE_ARRAY_P (field_decl_type))
9457 resolve_and_layout (field_decl_type, NULL_TREE);
9458 if (TYPE_ARRAY_P (field_decl_type))
9459 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9460
9461 /* Check on accessibility here */
9462 if (not_accessible_p (type, field_decl, from_super))
9463 {
9464 parse_error_context
9465 (qual_wfl,
9466 "Can't access %s field `%s.%s' from `%s'",
9467 java_accstring_lookup
9468 (get_access_flags_from_decl (field_decl)),
2aa11e97 9469 GET_TYPE_NAME (type),
e04a16fb
AG
9470 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9471 IDENTIFIER_POINTER
9472 (DECL_NAME (TYPE_NAME (current_class))));
9473 return 1;
9474 }
5e942c50 9475 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9476
9477 /* There are things to check when fields are accessed
9478 from type. There are no restrictions on a static
9479 declaration of the field when it is accessed from an
9480 interface */
9481 is_static = FIELD_STATIC (field_decl);
9482 if (!from_super && from_type
c2952b01
APB
9483 && !TYPE_INTERFACE_P (type)
9484 && !is_static
9485 && (current_function_decl
9486 && METHOD_STATIC (current_function_decl)))
e04a16fb 9487 {
7f10c2e2 9488 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9489 return 1;
9490 }
9491 from_cast = from_super = 0;
9492
c2952b01
APB
9493 /* It's an access from a type but it isn't static, we
9494 make it relative to `this'. */
9495 if (!is_static && from_type)
9496 decl = current_this;
9497
5e942c50
APB
9498 /* If we need to generate something to get a proper
9499 handle on what this field is accessed from, do it
9500 now. */
e04a16fb
AG
9501 if (!is_static)
9502 {
c583dd46 9503 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9504 if (decl == error_mark_node)
9505 return 1;
9506 }
9507
9508 /* We want to keep the location were found it, and the type
9509 we found. */
9510 *where_found = decl;
9511 *type_found = type;
9512
c2952b01
APB
9513 /* Generate the correct expression for field access from
9514 qualified this */
9515 if (from_qualified_this)
9516 {
9517 field_decl = build_outer_field_access (qual_wfl, field_decl);
9518 from_qualified_this = 0;
9519 }
9520
e04a16fb
AG
9521 /* This is the decl found and eventually the next one to
9522 search from */
9523 decl = field_decl;
9524 }
e04a16fb
AG
9525 from_type = 0;
9526 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9527
9528 /* Sneak preview. If decl is qualified by a `new', report
9529 the error here to be accurate on the peculiar construct */
9530 if (TREE_CHAIN (q)
9531 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9532 && !JREFERENCE_TYPE_P (type))
9533 {
9534 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9535 lang_printable_name (type, 0));
9536 return 1;
9537 }
e04a16fb 9538 }
dde1da72
APB
9539 /* `q' might have changed due to a after package resolution
9540 re-qualification */
9541 if (!q)
9542 break;
e04a16fb
AG
9543 }
9544 *found_decl = decl;
9545 return 0;
9546}
9547
9548/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9549 can't be accessed from REFERENCE (a record type). This should be
9550 used when decl is a field or a method.*/
e04a16fb 9551
be245ac0
KG
9552static int
9553not_accessible_p (reference, member, from_super)
e04a16fb
AG
9554 tree reference, member;
9555 int from_super;
9556{
9557 int access_flag = get_access_flags_from_decl (member);
9558
4dbf4496
APB
9559 /* Inner classes are processed by check_inner_class_access */
9560 if (INNER_CLASS_TYPE_P (reference))
9561 return 0;
9562
e04a16fb
AG
9563 /* Access always granted for members declared public */
9564 if (access_flag & ACC_PUBLIC)
9565 return 0;
9566
9567 /* Check access on protected members */
9568 if (access_flag & ACC_PROTECTED)
9569 {
9570 /* Access granted if it occurs from within the package
9571 containing the class in which the protected member is
9572 declared */
9573 if (class_in_current_package (DECL_CONTEXT (member)))
9574 return 0;
9575
9bbc7d9f
PB
9576 /* If accessed with the form `super.member', then access is granted */
9577 if (from_super)
9578 return 0;
e04a16fb 9579
9bbc7d9f 9580 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9581 member is declared or a subclass of it. Find the right
9582 context to perform the check */
9583 if (PURE_INNER_CLASS_TYPE_P (reference))
9584 {
9585 while (INNER_CLASS_TYPE_P (reference))
9586 {
9587 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9588 return 0;
9589 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9590 }
9591 }
473e7b07 9592 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9593 return 0;
e04a16fb
AG
9594 return 1;
9595 }
9596
9597 /* Check access on private members. Access is granted only if it
473e7b07 9598 occurs from within the class in which it is declared. Exceptions
4dbf4496 9599 are accesses from inner-classes. */
e04a16fb 9600 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9601 return (current_class == DECL_CONTEXT (member) ? 0 :
9602 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9603
9604 /* Default access are permitted only when occuring within the
9605 package in which the type (REFERENCE) is declared. In other words,
9606 REFERENCE is defined in the current package */
9607 if (ctxp->package)
9608 return !class_in_current_package (reference);
473e7b07 9609
e04a16fb
AG
9610 /* Otherwise, access is granted */
9611 return 0;
9612}
9613
5e942c50
APB
9614/* Test deprecated decl access. */
9615static void
9616check_deprecation (wfl, decl)
9617 tree wfl, decl;
9618{
49f48c71 9619 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9620 /* Complain if the field is deprecated and the file it was defined
9621 in isn't compiled at the same time the file which contains its
9622 use is */
9623 if (DECL_DEPRECATED (decl)
9624 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9625 {
9626 char the [20];
9627 switch (TREE_CODE (decl))
9628 {
9629 case FUNCTION_DECL:
9630 strcpy (the, "method");
9631 break;
9632 case FIELD_DECL:
9633 strcpy (the, "field");
9634 break;
9635 case TYPE_DECL:
9636 strcpy (the, "class");
9637 break;
15fdcfe9
PB
9638 default:
9639 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9640 }
9641 parse_warning_context
9642 (wfl, "The %s `%s' in class `%s' has been deprecated",
9643 the, lang_printable_name (decl, 0),
9644 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9645 }
9646}
9647
e04a16fb
AG
9648/* Returns 1 if class was declared in the current package, 0 otherwise */
9649
9650static int
9651class_in_current_package (class)
9652 tree class;
9653{
9654 static tree cache = NULL_TREE;
9655 int qualified_flag;
9656 tree left;
9657
9658 if (cache == class)
9659 return 1;
9660
9661 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9662
9663 /* If the current package is empty and the name of CLASS is
9664 qualified, class isn't in the current package. If there is a
9665 current package and the name of the CLASS is not qualified, class
9666 isn't in the current package */
0a2138e2 9667 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9668 return 0;
9669
9670 /* If there is not package and the name of CLASS isn't qualified,
9671 they belong to the same unnamed package */
9672 if (!ctxp->package && !qualified_flag)
9673 return 1;
9674
9675 /* Compare the left part of the name of CLASS with the package name */
9676 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9677 if (ctxp->package == left)
9678 {
19e223db
MM
9679 static int initialized_p;
9680 /* Register CACHE with the garbage collector. */
9681 if (!initialized_p)
9682 {
9683 ggc_add_tree_root (&cache, 1);
9684 initialized_p = 1;
9685 }
9686
e04a16fb
AG
9687 cache = class;
9688 return 1;
9689 }
9690 return 0;
9691}
9692
9693/* This function may generate code to access DECL from WHERE. This is
9694 done only if certain conditions meet. */
9695
9696static tree
9697maybe_access_field (decl, where, type)
9698 tree decl, where, type;
9699{
5e942c50
APB
9700 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9701 && !FIELD_STATIC (decl))
e04a16fb 9702 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9703 (type ? type : DECL_CONTEXT (decl)),
9704 DECL_NAME (decl));
e04a16fb
AG
9705 return decl;
9706}
9707
15fdcfe9 9708/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9709 and according to the situation, PRIMARY and WHERE may be
9710 used. IS_STATIC is set to 1 if the invoked function is static. */
9711
9712static tree
89e09b9a 9713patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9714 tree patch, primary, where;
9715 int *is_static;
b9f7e36c 9716 tree *ret_decl;
e04a16fb
AG
9717{
9718 tree wfl = TREE_OPERAND (patch, 0);
9719 tree args = TREE_OPERAND (patch, 1);
9720 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9721 tree list;
22eed1e6 9722 int is_static_flag = 0;
89e09b9a 9723 int is_super_init = 0;
bccaf73a 9724 tree this_arg = NULL_TREE;
e04a16fb
AG
9725
9726 /* Should be overriden if everything goes well. Otherwise, if
9727 something fails, it should keep this value. It stop the
9728 evaluation of a bogus assignment. See java_complete_tree,
9729 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9730 evaluating an assignment */
9731 TREE_TYPE (patch) = error_mark_node;
9732
9733 /* Since lookup functions are messing with line numbers, save the
9734 context now. */
9735 java_parser_context_save_global ();
9736
9737 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9738
9739 /* Resolution of qualified name, excluding constructors */
9740 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9741 {
dde1da72 9742 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9743 /* Extract the last IDENTIFIER of the qualified
9744 expression. This is a wfl and we will use it's location
9745 data during error report. */
9746 identifier_wfl = cut_identifier_in_qualified (wfl);
9747 identifier = EXPR_WFL_NODE (identifier_wfl);
9748
9749 /* Given the context, IDENTIFIER is syntactically qualified
9750 as a MethodName. We need to qualify what's before */
9751 qualify_ambiguous_name (wfl);
dde1da72 9752 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9753
dde1da72
APB
9754 if (resolved == error_mark_node)
9755 PATCH_METHOD_RETURN_ERROR ();
9756
9757 type = GET_SKIP_TYPE (resolved);
9758 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9759
9760 if (JPRIMITIVE_TYPE_P (type))
9761 {
7e51098e
TT
9762 parse_error_context
9763 (identifier_wfl,
9764 "Can't invoke a method on primitive type `%s'",
9765 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9766 PATCH_METHOD_RETURN_ERROR ();
9767 }
9768
dde1da72
APB
9769 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9770 args = nreverse (args);
2c56429a 9771
e04a16fb 9772 /* We're resolving a call from a type */
dde1da72 9773 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9774 {
dde1da72 9775 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9776 {
9777 parse_error_context
781b0558
KG
9778 (identifier_wfl,
9779 "Can't make static reference to method `%s' in interface `%s'",
9780 IDENTIFIER_POINTER (identifier),
e04a16fb 9781 IDENTIFIER_POINTER (name));
b9f7e36c 9782 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9783 }
e04a16fb
AG
9784 if (list && !METHOD_STATIC (list))
9785 {
c2e3db92 9786 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9787 parse_error_context
9788 (identifier_wfl,
9789 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9790 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9791 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9792 free (fct_name);
b9f7e36c 9793 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9794 }
9795 }
e04a16fb 9796 else
dde1da72
APB
9797 this_arg = primary = resolved;
9798
5e942c50 9799 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9800 wfl = identifier_wfl;
9801 }
9802 /* Resolution of simple names, names generated after a primary: or
9803 constructors */
9804 else
9805 {
cd531a2e 9806 tree class_to_search = NULL_TREE;
c2952b01 9807 int lc; /* Looking for Constructor */
e04a16fb
AG
9808
9809 /* We search constructor in their target class */
9810 if (CALL_CONSTRUCTOR_P (patch))
9811 {
22eed1e6
APB
9812 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9813 class_to_search = EXPR_WFL_NODE (wfl);
9814 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9815 this_identifier_node)
9816 class_to_search = NULL_TREE;
9817 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9818 super_identifier_node)
e04a16fb 9819 {
89e09b9a 9820 is_super_init = 1;
22eed1e6
APB
9821 if (CLASSTYPE_SUPER (current_class))
9822 class_to_search =
9823 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9824 else
9825 {
781b0558 9826 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9827 PATCH_METHOD_RETURN_ERROR ();
9828 }
e04a16fb 9829 }
22eed1e6
APB
9830
9831 /* Class to search is NULL if we're searching the current one */
9832 if (class_to_search)
e04a16fb 9833 {
c2952b01
APB
9834 class_to_search = resolve_and_layout (class_to_search, wfl);
9835
22eed1e6
APB
9836 if (!class_to_search)
9837 {
9838 parse_error_context
9839 (wfl, "Class `%s' not found in type declaration",
9840 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9841 PATCH_METHOD_RETURN_ERROR ();
9842 }
9843
5e942c50
APB
9844 /* Can't instantiate an abstract class, but we can
9845 invoke it's constructor. It's use within the `new'
9846 context is denied here. */
9847 if (CLASS_ABSTRACT (class_to_search)
9848 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9849 {
9850 parse_error_context
781b0558
KG
9851 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9852 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9853 PATCH_METHOD_RETURN_ERROR ();
9854 }
c2952b01 9855
22eed1e6 9856 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9857 }
22eed1e6
APB
9858 else
9859 class_to_search = current_class;
e04a16fb
AG
9860 lc = 1;
9861 }
9862 /* This is a regular search in the local class, unless an
9863 alternate class is specified. */
9864 else
9865 {
9866 class_to_search = (where ? where : current_class);
9867 lc = 0;
9868 }
c2952b01 9869
e04a16fb
AG
9870 /* NAME is a simple identifier or comes from a primary. Search
9871 in the class whose declaration contain the method being
9872 invoked. */
c877974e 9873 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9874
c2952b01 9875 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9876 /* Don't continue if no method were found, as the next statement
9877 can't be executed then. */
b9f7e36c
APB
9878 if (!list)
9879 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9880
9881 /* Check for static reference if non static methods */
9882 if (check_for_static_method_reference (wfl, patch, list,
9883 class_to_search, primary))
b9f7e36c 9884 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9885
165f37bc
APB
9886 /* Check for inner classes creation from illegal contexts */
9887 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9888 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9889 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9890 {
9891 parse_error_context
9892 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9893 lang_printable_name (class_to_search, 0),
9894 (!current_this ? "" :
9895 "; an explicit one must be provided when creating this inner class"));
9896 PATCH_METHOD_RETURN_ERROR ();
9897 }
9898
22eed1e6
APB
9899 /* Non static methods are called with the current object extra
9900 argument. If patch a `new TYPE()', the argument is the value
9901 returned by the object allocator. If method is resolved as a
9902 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9903 args = nreverse (args);
bccaf73a 9904 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9905 {
9906 this_arg = primary ? primary : current_this;
9907
9908 /* If we're using an access method, things are different.
9909 There are two familly of cases:
9910
9911 1) We're not generating bytecodes:
9912
9913 - LIST is non static. It's invocation is transformed from
9914 x(a1,...,an) into this$<n>.x(a1,....an).
9915 - LIST is static. It's invocation is transformed from
9916 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9917
9918 2) We're generating bytecodes:
9919
9920 - LIST is non static. It's invocation is transformed from
9921 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9922 - LIST is static. It's invocation is transformed from
9923 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9924
9925 Of course, this$<n> can be abitrary complex, ranging from
9926 this$0 (the immediate outer context) to
9927 access$0(access$0(...(this$0))).
9928
9929 maybe_use_access_method returns a non zero value if the
dfb99c83 9930 this_arg has to be moved into the (then generated) stub
4dbf4496 9931 argument list. In the meantime, the selected function
dfb99c83 9932 might have be replaced by a generated stub. */
c2952b01 9933 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9934 {
9935 args = tree_cons (NULL_TREE, this_arg, args);
9936 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9937 }
c2952b01 9938 }
e04a16fb 9939 }
b67d701b 9940
e04a16fb
AG
9941 /* Merge point of all resolution schemes. If we have nothing, this
9942 is an error, already signaled */
b9f7e36c
APB
9943 if (!list)
9944 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9945
e04a16fb
AG
9946 /* Check accessibility, position the is_static flag, build and
9947 return the call */
9bbc7d9f 9948 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9949 {
c2e3db92 9950 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9951 parse_error_context
9952 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9953 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9954 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9955 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9956 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9957 free (fct_name);
b9f7e36c 9958 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9959 }
5e942c50 9960 check_deprecation (wfl, list);
22eed1e6 9961
c2952b01
APB
9962 /* If invoking a innerclass constructor, there are hidden parameters
9963 to pass */
9964 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9965 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9966 {
9967 /* And make sure we add the accessed local variables to be saved
9968 in field aliases. */
9969 args = build_alias_initializer_parameter_list
9970 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9971
da632f2c 9972 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9973 if (primary || current_this)
9974 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9975 else
9976 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9977 }
9978
152de068
APB
9979 /* This handles the situation where a constructor invocation needs
9980 to have an enclosing context passed as a second parameter (the
9981 constructor is one of an inner class. We extract it from the
9982 current function. */
9983 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9984 {
9985 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9986 tree extra_arg;
9987
9988 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9989 {
9990 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9991 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9992 }
9993 else
9994 {
9995 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9996 extra_arg =
9997 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9998 extra_arg = java_complete_tree (extra_arg);
9999 }
10000 args = tree_cons (NULL_TREE, extra_arg, args);
10001 }
10002
22eed1e6 10003 is_static_flag = METHOD_STATIC (list);
dba41d30 10004 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10005 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10006
c3f2a476
APB
10007 /* In the context of an explicit constructor invocation, we can't
10008 invoke any method relying on `this'. Exceptions are: we're
10009 invoking a static function, primary exists and is not the current
10010 this, we're creating a new object. */
22eed1e6 10011 if (ctxp->explicit_constructor_p
c3f2a476
APB
10012 && !is_static_flag
10013 && (!primary || primary == current_this)
10014 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10015 {
781b0558 10016 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10017 PATCH_METHOD_RETURN_ERROR ();
10018 }
e04a16fb 10019 java_parser_context_restore_global ();
22eed1e6
APB
10020 if (is_static)
10021 *is_static = is_static_flag;
b9f7e36c
APB
10022 /* Sometimes, we want the decl of the selected method. Such as for
10023 EH checking */
10024 if (ret_decl)
10025 *ret_decl = list;
89e09b9a
PB
10026 patch = patch_invoke (patch, list, args);
10027 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10028 {
c2952b01
APB
10029 tree finit_parms, finit_call;
10030
c00f0fb2 10031 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10032 finit_parms = build_alias_initializer_parameter_list
10033 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10034
c2952b01
APB
10035 finit_call =
10036 build_method_invocation (build_wfl_node (finit_identifier_node),
10037 finit_parms);
10038
10039 /* Generate the code used to initialize fields declared with an
10040 initialization statement and build a compound statement along
10041 with the super constructor invocation. */
89e09b9a
PB
10042 patch = build (COMPOUND_EXPR, void_type_node, patch,
10043 java_complete_tree (finit_call));
10044 CAN_COMPLETE_NORMALLY (patch) = 1;
10045 }
10046 return patch;
e04a16fb
AG
10047}
10048
10049/* Check that we're not trying to do a static reference to a method in
10050 non static method. Return 1 if it's the case, 0 otherwise. */
10051
10052static int
10053check_for_static_method_reference (wfl, node, method, where, primary)
10054 tree wfl, node, method, where, primary;
10055{
10056 if (METHOD_STATIC (current_function_decl)
10057 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10058 {
c2e3db92 10059 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10060 parse_error_context
10061 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10062 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10063 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10064 free (fct_name);
10065 return 1;
10066 }
10067 return 0;
10068}
10069
c2952b01
APB
10070/* Fix the invocation of *MDECL if necessary in the case of a
10071 invocation from an inner class. *THIS_ARG might be modified
10072 appropriately and an alternative access to *MDECL might be
10073 returned. */
10074
10075static int
10076maybe_use_access_method (is_super_init, mdecl, this_arg)
10077 int is_super_init;
10078 tree *mdecl, *this_arg;
10079{
10080 tree ctx;
10081 tree md = *mdecl, ta = *this_arg;
10082 int to_return = 0;
10083 int non_static_context = !METHOD_STATIC (md);
10084
10085 if (is_super_init
165f37bc
APB
10086 || DECL_CONTEXT (md) == current_class
10087 || !PURE_INNER_CLASS_TYPE_P (current_class)
10088 || DECL_FINIT_P (md))
c2952b01
APB
10089 return 0;
10090
10091 /* If we're calling a method found in an enclosing class, generate
10092 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10093 invoking a static method. Note that if MD's type is unrelated to
10094 CURRENT_CLASS, then the current this can be used. */
c2952b01 10095
2cb3951d 10096 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10097 {
10098 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10099 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10100 {
10101 ta = build_current_thisn (current_class);
10102 ta = build_wfl_node (ta);
10103 }
10104 else
10105 {
10106 tree type = ctx;
10107 while (type)
10108 {
10109 maybe_build_thisn_access_method (type);
4dbf4496 10110 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10111 {
10112 ta = build_access_to_thisn (ctx, type, 0);
10113 break;
10114 }
10115 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10116 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10117 }
10118 }
10119 ta = java_complete_tree (ta);
10120 }
10121
10122 /* We might have to use an access method to get to MD. We can
10123 break the method access rule as far as we're not generating
10124 bytecode */
10125 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10126 {
10127 md = build_outer_method_access_method (md);
10128 to_return = 1;
10129 }
10130
10131 *mdecl = md;
10132 *this_arg = ta;
10133
10134 /* Returnin a non zero value indicates we were doing a non static
10135 method invokation that is now a static invocation. It will have
10136 callee displace `this' to insert it in the regular argument
10137 list. */
10138 return (non_static_context && to_return);
10139}
10140
e04a16fb
AG
10141/* Patch an invoke expression METHOD and ARGS, based on its invocation
10142 mode. */
10143
10144static tree
89e09b9a 10145patch_invoke (patch, method, args)
e04a16fb 10146 tree patch, method, args;
e04a16fb
AG
10147{
10148 tree dtable, func;
0a2138e2 10149 tree original_call, t, ta;
e815887f 10150 tree cond = NULL_TREE;
e04a16fb 10151
5e942c50
APB
10152 /* Last step for args: convert build-in types. If we're dealing with
10153 a new TYPE() type call, the first argument to the constructor
e815887f 10154 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10155 `new' */
10156 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10157 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10158 t = TREE_CHAIN (t);
ac825856
APB
10159 for (ta = args; t != end_params_node && ta;
10160 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10161 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10162 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10163 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10164
10165 /* Resolve unresolved returned type isses */
10166 t = TREE_TYPE (TREE_TYPE (method));
10167 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10168 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10169
e8fc7396 10170 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10171 func = method;
10172 else
e04a16fb 10173 {
15fdcfe9 10174 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10175 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10176 {
10177 case INVOKE_VIRTUAL:
10178 dtable = invoke_build_dtable (0, args);
10179 func = build_invokevirtual (dtable, method);
10180 break;
b9f7e36c 10181
e815887f
TT
10182 case INVOKE_NONVIRTUAL:
10183 /* If the object for the method call is null, we throw an
10184 exception. We don't do this if the object is the current
10185 method's `this'. In other cases we just rely on an
10186 optimization pass to eliminate redundant checks. */
10187 if (TREE_VALUE (args) != current_this)
10188 {
10189 /* We use a SAVE_EXPR here to make sure we only evaluate
10190 the new `self' expression once. */
10191 tree save_arg = save_expr (TREE_VALUE (args));
10192 TREE_VALUE (args) = save_arg;
10193 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10194 null_pointer_node);
10195 }
10196 /* Fall through. */
10197
15fdcfe9
PB
10198 case INVOKE_SUPER:
10199 case INVOKE_STATIC:
10200 func = build_known_method_ref (method, TREE_TYPE (method),
10201 DECL_CONTEXT (method),
10202 signature, args);
10203 break;
e04a16fb 10204
15fdcfe9
PB
10205 case INVOKE_INTERFACE:
10206 dtable = invoke_build_dtable (1, args);
173f556c 10207 func = build_invokeinterface (dtable, method);
15fdcfe9 10208 break;
5e942c50 10209
15fdcfe9 10210 default:
89e09b9a 10211 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10212 }
10213
10214 /* Ensure self_type is initialized, (invokestatic). FIXME */
10215 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10216 }
10217
e04a16fb
AG
10218 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10219 TREE_OPERAND (patch, 0) = func;
10220 TREE_OPERAND (patch, 1) = args;
10221 original_call = patch;
10222
e815887f 10223 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10224 returned value is the first argument to the constructor. We build
10225 a COMPOUND_EXPR and use saved expression so that the overall NEW
10226 expression value is a pointer to a newly created and initialized
10227 class. */
10228 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10229 {
10230 tree class = DECL_CONTEXT (method);
10231 tree c1, saved_new, size, new;
e8fc7396 10232 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10233 {
10234 TREE_TYPE (patch) = build_pointer_type (class);
10235 return patch;
10236 }
e04a16fb
AG
10237 if (!TYPE_SIZE (class))
10238 safe_layout_class (class);
10239 size = size_in_bytes (class);
10240 new = build (CALL_EXPR, promote_type (class),
10241 build_address_of (alloc_object_node),
10242 tree_cons (NULL_TREE, build_class_ref (class),
10243 build_tree_list (NULL_TREE,
10244 size_in_bytes (class))),
10245 NULL_TREE);
10246 saved_new = save_expr (new);
10247 c1 = build_tree_list (NULL_TREE, saved_new);
10248 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10249 TREE_OPERAND (original_call, 1) = c1;
10250 TREE_SET_CODE (original_call, CALL_EXPR);
10251 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10252 }
e815887f
TT
10253
10254 /* If COND is set, then we are building a check to see if the object
10255 is NULL. */
10256 if (cond != NULL_TREE)
10257 {
10258 /* We have to make the `then' branch a compound expression to
10259 make the types turn out right. This seems bizarre. */
10260 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10261 build (COMPOUND_EXPR, TREE_TYPE (patch),
10262 build (CALL_EXPR, void_type_node,
10263 build_address_of (soft_nullpointer_node),
10264 NULL_TREE, NULL_TREE),
10265 (FLOAT_TYPE_P (TREE_TYPE (patch))
10266 ? build_real (TREE_TYPE (patch), dconst0)
10267 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10268 integer_zero_node))),
10269 patch);
10270 TREE_SIDE_EFFECTS (patch) = 1;
10271 }
10272
e04a16fb
AG
10273 return patch;
10274}
10275
10276static int
10277invocation_mode (method, super)
10278 tree method;
10279 int super;
10280{
10281 int access = get_access_flags_from_decl (method);
10282
22eed1e6
APB
10283 if (super)
10284 return INVOKE_SUPER;
10285
e815887f 10286 if (access & ACC_STATIC)
e04a16fb
AG
10287 return INVOKE_STATIC;
10288
e815887f
TT
10289 /* We have to look for a constructor before we handle nonvirtual
10290 calls; otherwise the constructor will look nonvirtual. */
10291 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10292 return INVOKE_STATIC;
e815887f
TT
10293
10294 if (access & ACC_FINAL || access & ACC_PRIVATE)
10295 return INVOKE_NONVIRTUAL;
10296
10297 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10298 return INVOKE_NONVIRTUAL;
10299
e04a16fb
AG
10300 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10301 return INVOKE_INTERFACE;
22eed1e6 10302
e04a16fb
AG
10303 return INVOKE_VIRTUAL;
10304}
10305
b67d701b
PB
10306/* Retrieve a refined list of matching methods. It covers the step
10307 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10308
10309static tree
10310lookup_method_invoke (lc, cl, class, name, arg_list)
10311 int lc;
10312 tree cl;
10313 tree class, name, arg_list;
10314{
de4c7b02 10315 tree atl = end_params_node; /* Arg Type List */
c877974e 10316 tree method, signature, list, node;
49f48c71 10317 const char *candidates; /* Used for error report */
b5b8a0e7 10318 char *dup;
e04a16fb 10319
5e942c50 10320 /* Fix the arguments */
e04a16fb
AG
10321 for (node = arg_list; node; node = TREE_CHAIN (node))
10322 {
e3884b71 10323 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10324 /* Non primitive type may have to be resolved */
e3884b71 10325 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10326 resolve_and_layout (current_arg, NULL_TREE);
10327 /* And promoted */
b67d701b 10328 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10329 current_arg = promote_type (current_arg);
5e942c50 10330 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10331 }
e04a16fb 10332
c2952b01
APB
10333 /* Presto. If we're dealing with an anonymous class and a
10334 constructor call, generate the right constructor now, since we
10335 know the arguments' types. */
10336
10337 if (lc && ANONYMOUS_CLASS_P (class))
10338 craft_constructor (TYPE_NAME (class), atl);
10339
5e942c50
APB
10340 /* Find all candidates and then refine the list, searching for the
10341 most specific method. */
10342 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10343 list = find_most_specific_methods_list (list);
b67d701b
PB
10344 if (list && !TREE_CHAIN (list))
10345 return TREE_VALUE (list);
e04a16fb 10346
b67d701b
PB
10347 /* Issue an error. List candidates if any. Candidates are listed
10348 only if accessible (non accessible methods may end-up here for
10349 the sake of a better error report). */
10350 candidates = NULL;
10351 if (list)
e04a16fb 10352 {
e04a16fb 10353 tree current;
b67d701b 10354 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10355 for (current = list; current; current = TREE_CHAIN (current))
10356 {
b67d701b
PB
10357 tree cm = TREE_VALUE (current);
10358 char string [4096];
10359 if (!cm || not_accessible_p (class, cm, 0))
10360 continue;
b67d701b 10361 sprintf
22eed1e6
APB
10362 (string, " `%s' in `%s'%s",
10363 get_printable_method_name (cm),
b67d701b
PB
10364 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10365 (TREE_CHAIN (current) ? "\n" : ""));
10366 obstack_grow (&temporary_obstack, string, strlen (string));
10367 }
10368 obstack_1grow (&temporary_obstack, '\0');
10369 candidates = obstack_finish (&temporary_obstack);
10370 }
10371 /* Issue the error message */
c877974e
APB
10372 method = make_node (FUNCTION_TYPE);
10373 TYPE_ARG_TYPES (method) = atl;
b67d701b 10374 signature = build_java_argument_signature (method);
c63b98cd 10375 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10376 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10377 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10378 (lc ? dup : IDENTIFIER_POINTER (name)),
10379 IDENTIFIER_POINTER (signature), dup,
b67d701b 10380 (candidates ? candidates : ""));
b5b8a0e7 10381 free (dup);
b67d701b
PB
10382 return NULL_TREE;
10383}
10384
5e942c50
APB
10385/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10386 when we're looking for a constructor. */
b67d701b
PB
10387
10388static tree
5e942c50
APB
10389find_applicable_accessible_methods_list (lc, class, name, arglist)
10390 int lc;
b67d701b
PB
10391 tree class, name, arglist;
10392{
ad69b5b6
BM
10393 static struct hash_table t, *searched_classes = NULL;
10394 static int search_not_done = 0;
b67d701b
PB
10395 tree list = NULL_TREE, all_list = NULL_TREE;
10396
ad69b5b6
BM
10397 /* Check the hash table to determine if this class has been searched
10398 already. */
10399 if (searched_classes)
10400 {
10401 if (hash_lookup (searched_classes,
10402 (const hash_table_key) class, FALSE, NULL))
10403 return NULL;
10404 }
10405 else
10406 {
10407 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10408 java_hash_compare_tree_node);
10409 searched_classes = &t;
10410 }
10411
10412 search_not_done++;
10413 hash_lookup (searched_classes,
0c2b8145 10414 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10415
c2952b01
APB
10416 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10417 {
10418 load_class (class, 1);
10419 safe_layout_class (class);
10420 }
10421
1982388a 10422 /* Search interfaces */
9a7ab4b3
APB
10423 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10424 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10425 {
1982388a
APB
10426 int i, n;
10427 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10428 search_applicable_methods_list (lc, TYPE_METHODS (class),
10429 name, arglist, &list, &all_list);
1982388a 10430 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10431 for (i = 1; i < n; i++)
b67d701b 10432 {
de0b553f
APB
10433 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10434 tree rlist;
10435
de0b553f
APB
10436 rlist = find_applicable_accessible_methods_list (lc, t, name,
10437 arglist);
165f37bc 10438 list = chainon (rlist, list);
e04a16fb 10439 }
e04a16fb 10440 }
1982388a
APB
10441 /* Search classes */
10442 else
c2952b01 10443 {
165f37bc
APB
10444 tree sc = class;
10445 int seen_inner_class = 0;
c2952b01
APB
10446 search_applicable_methods_list (lc, TYPE_METHODS (class),
10447 name, arglist, &list, &all_list);
10448
165f37bc
APB
10449 /* We must search all interfaces of this class */
10450 if (!lc)
10451 {
10452 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10453 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10454 for (i = 1; i < n; i++)
10455 {
10456 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10457 if (t != object_type_node)
30a3caef
ZW
10458 {
10459 tree rlist
10460 = find_applicable_accessible_methods_list (lc, t,
10461 name, arglist);
10462 list = chainon (rlist, list);
10463 }
165f37bc 10464 }
165f37bc
APB
10465 }
10466
c2952b01
APB
10467 /* Search enclosing context of inner classes before looking
10468 ancestors up. */
10469 while (!lc && INNER_CLASS_TYPE_P (class))
10470 {
165f37bc
APB
10471 tree rlist;
10472 seen_inner_class = 1;
c2952b01 10473 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10474 rlist = find_applicable_accessible_methods_list (lc, class,
10475 name, arglist);
10476 list = chainon (rlist, list);
c2952b01 10477 }
165f37bc
APB
10478
10479 if (!lc && seen_inner_class
10480 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10481 class = CLASSTYPE_SUPER (sc);
10482 else
10483 class = sc;
10484
ad69b5b6
BM
10485 /* Search superclass */
10486 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10487 {
10488 tree rlist;
10489 class = CLASSTYPE_SUPER (class);
10490 rlist = find_applicable_accessible_methods_list (lc, class,
10491 name, arglist);
10492 list = chainon (rlist, list);
10493 }
10494 }
10495
10496 search_not_done--;
10497
10498 /* We're done. Reset the searched classes list and finally search
10499 java.lang.Object if it wasn't searched already. */
10500 if (!search_not_done)
10501 {
10502 if (!lc
10503 && TYPE_METHODS (object_type_node)
10504 && !hash_lookup (searched_classes,
10505 (const hash_table_key) object_type_node,
10506 FALSE, NULL))
10507 {
10508 search_applicable_methods_list (lc,
10509 TYPE_METHODS (object_type_node),
10510 name, arglist, &list, &all_list);
10511 }
10512 hash_table_free (searched_classes);
10513 searched_classes = NULL;
c2952b01 10514 }
1982388a 10515
b67d701b
PB
10516 /* Either return the list obtained or all selected (but
10517 inaccessible) methods for better error report. */
10518 return (!list ? all_list : list);
10519}
e04a16fb 10520
ad69b5b6 10521/* Effectively search for the appropriate method in method */
1982388a
APB
10522
10523static void
c2952b01 10524search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10525 int lc;
10526 tree method, name, arglist;
10527 tree *list, *all_list;
10528{
10529 for (; method; method = TREE_CHAIN (method))
10530 {
10531 /* When dealing with constructor, stop here, otherwise search
10532 other classes */
10533 if (lc && !DECL_CONSTRUCTOR_P (method))
10534 continue;
10535 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10536 || (GET_METHOD_NAME (method) != name)))
10537 continue;
10538
10539 if (argument_types_convertible (method, arglist))
10540 {
10541 /* Retain accessible methods only */
10542 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10543 method, 0))
10544 *list = tree_cons (NULL_TREE, method, *list);
10545 else
10546 /* Also retain all selected method here */
10547 *all_list = tree_cons (NULL_TREE, method, *list);
10548 }
10549 }
ad69b5b6 10550}
1982388a 10551
b67d701b
PB
10552/* 15.11.2.2 Choose the Most Specific Method */
10553
10554static tree
10555find_most_specific_methods_list (list)
10556 tree list;
10557{
10558 int max = 0;
9a7ab4b3 10559 int abstract, candidates;
b67d701b
PB
10560 tree current, new_list = NULL_TREE;
10561 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10562 {
b67d701b
PB
10563 tree method;
10564 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10565
10566 for (method = list; method; method = TREE_CHAIN (method))
10567 {
0c2b8145 10568 tree method_v, current_v;
b67d701b
PB
10569 /* Don't test a method against itself */
10570 if (method == current)
10571 continue;
10572
0c2b8145
APB
10573 method_v = TREE_VALUE (method);
10574 current_v = TREE_VALUE (current);
10575
10576 /* Compare arguments and location where methods where declared */
10577 if (argument_types_convertible (method_v, current_v))
b67d701b 10578 {
0c2b8145
APB
10579 if (valid_method_invocation_conversion_p
10580 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10581 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10582 && enclosing_context_p (DECL_CONTEXT (method_v),
10583 DECL_CONTEXT (current_v))))
10584 {
10585 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10586 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10587 max = (v > max ? v : max);
10588 }
b67d701b
PB
10589 }
10590 }
e04a16fb
AG
10591 }
10592
b67d701b 10593 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10594 for (current = list, abstract = -1, candidates = -1;
10595 current; current = TREE_CHAIN (current))
b67d701b 10596 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10597 {
10598 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10599 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10600 candidates++;
10601 }
b67d701b 10602
165f37bc
APB
10603 /* If we have several and they're all abstract, just pick the
10604 closest one. */
9a7ab4b3
APB
10605 if (candidates > 0 && (candidates == abstract))
10606 {
10607 new_list = nreverse (new_list);
10608 TREE_CHAIN (new_list) = NULL_TREE;
10609 }
165f37bc 10610
cab8e2bd
BM
10611 /* We have several (we couldn't find a most specific), all but one
10612 are abstract, we pick the only non abstract one. */
10613 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10614 {
9a7ab4b3
APB
10615 for (current = new_list; current; current = TREE_CHAIN (current))
10616 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10617 {
10618 TREE_CHAIN (current) = NULL_TREE;
10619 new_list = current;
10620 }
165f37bc
APB
10621 }
10622
b67d701b
PB
10623 /* If we can't find one, lower expectations and try to gather multiple
10624 maximally specific methods */
165f37bc 10625 while (!new_list && max)
b67d701b
PB
10626 {
10627 while (--max > 0)
10628 {
10629 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10630 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10631 }
b67d701b
PB
10632 }
10633
10634 return new_list;
e04a16fb
AG
10635}
10636
b67d701b
PB
10637/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10638 converted by method invocation conversion (5.3) to the type of the
10639 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10640 to change less often than M1. */
e04a16fb 10641
b67d701b
PB
10642static int
10643argument_types_convertible (m1, m2_or_arglist)
10644 tree m1, m2_or_arglist;
e04a16fb 10645{
b67d701b
PB
10646 static tree m2_arg_value = NULL_TREE;
10647 static tree m2_arg_cache = NULL_TREE;
19e223db 10648 static int initialized_p;
e04a16fb 10649
b67d701b 10650 register tree m1_arg, m2_arg;
e04a16fb 10651
19e223db
MM
10652 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10653 collector. */
10654 if (!initialized_p)
10655 {
10656 ggc_add_tree_root (&m2_arg_value, 1);
10657 ggc_add_tree_root (&m2_arg_cache, 1);
10658 initialized_p = 1;
10659 }
10660
c2952b01 10661 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10662
b67d701b
PB
10663 if (m2_arg_value == m2_or_arglist)
10664 m2_arg = m2_arg_cache;
10665 else
10666 {
10667 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10668 argument types */
10669 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10670 {
10671 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10672 if (!METHOD_STATIC (m2_or_arglist))
10673 m2_arg = TREE_CHAIN (m2_arg);
10674 }
10675 else
10676 m2_arg = m2_or_arglist;
e04a16fb 10677
b67d701b
PB
10678 m2_arg_value = m2_or_arglist;
10679 m2_arg_cache = m2_arg;
10680 }
e04a16fb 10681
de4c7b02 10682 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10683 {
c877974e 10684 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10685 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10686 TREE_VALUE (m2_arg)))
10687 break;
10688 m1_arg = TREE_CHAIN (m1_arg);
10689 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10690 }
de4c7b02 10691 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10692}
10693
10694/* Qualification routines */
10695
10696static void
10697qualify_ambiguous_name (id)
10698 tree id;
10699{
cd531a2e
KG
10700 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10701 saved_current_class;
d8fccff5 10702 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10703 int code;
e04a16fb
AG
10704
10705 /* We first qualify the first element, then derive qualification of
10706 others based on the first one. If the first element is qualified
10707 by a resolution (field or type), this resolution is stored in the
10708 QUAL_RESOLUTION of the qual element being examined. We need to
10709 save the current_class since the use of SUPER might change the
10710 its value. */
10711 saved_current_class = current_class;
10712 qual = EXPR_WFL_QUALIFICATION (id);
10713 do {
10714
10715 /* Simple qualified expression feature a qual_wfl that is a
10716 WFL. Expression derived from a primary feature more complicated
10717 things like a CALL_EXPR. Expression from primary need to be
10718 worked out to extract the part on which the qualification will
10719 take place. */
10720 qual_wfl = QUAL_WFL (qual);
10721 switch (TREE_CODE (qual_wfl))
10722 {
10723 case CALL_EXPR:
10724 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10725 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10726 {
10727 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10728 qual_wfl = QUAL_WFL (qual);
10729 }
10730 break;
d8fccff5 10731 case NEW_ARRAY_EXPR:
c2952b01 10732 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10733 qual = TREE_CHAIN (qual);
1a6d4fb7 10734 again = new_array_found = 1;
d8fccff5 10735 continue;
e04a16fb 10736 case CONVERT_EXPR:
f2760b27
APB
10737 break;
10738 case NEW_CLASS_EXPR:
e04a16fb
AG
10739 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10740 break;
c583dd46
APB
10741 case ARRAY_REF:
10742 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10743 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10744 break;
8576f094
APB
10745 case STRING_CST:
10746 qual = TREE_CHAIN (qual);
10747 qual_wfl = QUAL_WFL (qual);
10748 break;
165f37bc
APB
10749 case CLASS_LITERAL:
10750 qual = TREE_CHAIN (qual);
10751 qual_wfl = QUAL_WFL (qual);
10752 break;
0a2138e2
APB
10753 default:
10754 /* Fix for -Wall. Just break doing nothing */
10755 break;
e04a16fb 10756 }
8576f094 10757
e04a16fb
AG
10758 ptr_type = current_class;
10759 again = 0;
8576f094
APB
10760 code = TREE_CODE (qual_wfl);
10761
10762 /* Pos evaluation: non WFL leading expression nodes */
10763 if (code == CONVERT_EXPR
10764 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10765 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10766
cd7c5840
APB
10767 else if (code == INTEGER_CST)
10768 name = qual_wfl;
10769
ac22f9cb 10770 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10771 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10772 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10773
c2952b01
APB
10774 else if (code == TREE_LIST)
10775 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10776
37feda7d
APB
10777 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10778 || code == PLUS_EXPR)
8576f094
APB
10779 {
10780 qual = TREE_CHAIN (qual);
10781 qual_wfl = QUAL_WFL (qual);
10782 again = 1;
10783 }
10784 else
f441f671
APB
10785 {
10786 name = EXPR_WFL_NODE (qual_wfl);
10787 if (!name)
10788 {
10789 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10790 again = 1;
10791 }
10792 }
10793
e04a16fb
AG
10794 /* If we have a THIS (from a primary), we set the context accordingly */
10795 if (name == this_identifier_node)
10796 {
6e22695a
APB
10797 /* This isn't really elegant. One more added irregularity
10798 before I start using COMPONENT_REF (hopefully very soon.) */
10799 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10800 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10801 EXPR_WITH_FILE_LOCATION
10802 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10803 this_identifier_node)
10804 {
10805 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10806 qual = EXPR_WFL_QUALIFICATION (qual);
10807 }
e04a16fb
AG
10808 qual = TREE_CHAIN (qual);
10809 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10810 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10811 again = 1;
10812 else
10813 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10814 this_found = 1;
10815 }
10816 /* If we have a SUPER, we set the context accordingly */
10817 if (name == super_identifier_node)
10818 {
10819 current_class = CLASSTYPE_SUPER (ptr_type);
10820 /* Check that there is such a thing as a super class. If not,
10821 return. The error will be caught later on, during the
10822 resolution */
10823 if (!current_class)
10824 {
10825 current_class = saved_current_class;
10826 return;
10827 }
10828 qual = TREE_CHAIN (qual);
10829 /* Do one more interation to set things up */
10830 super_found = again = 1;
10831 }
10832 } while (again);
10833
f2760b27
APB
10834 /* If name appears within the scope of a local variable declaration
10835 or parameter declaration, then it is an expression name. We don't
10836 carry this test out if we're in the context of the use of SUPER
10837 or THIS */
cd7c5840
APB
10838 if (!this_found && !super_found
10839 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10840 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10841 {
10842 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10843 QUAL_RESOLUTION (qual) = decl;
10844 }
10845
10846 /* If within the class/interface NAME was found to be used there
10847 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10848 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10849 address length, it is OK. */
10850 else if ((decl = lookup_field_wrapper (ptr_type, name))
10851 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10852 {
10853 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10854 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10855 }
10856
1a6d4fb7 10857 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10858 - NAME is a class/interface declared within the compilation
10859 unit containing NAME,
10860 - NAME is imported via a single-type-import declaration,
10861 - NAME is declared in an another compilation unit of the package
10862 of the compilation unit containing NAME,
10863 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10864 of the compilation unit containing NAME.
10865 - NAME is actually a STRING_CST. */
cd7c5840
APB
10866 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10867 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10868 {
10869 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10870 QUAL_RESOLUTION (qual) = decl;
10871 }
10872
f2760b27 10873 /* Method call, array references and cast are expression name */
9bbc7d9f 10874 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10875 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10876 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10877 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10878
10879 /* Check here that NAME isn't declared by more than one
10880 type-import-on-demand declaration of the compilation unit
10881 containing NAME. FIXME */
10882
10883 /* Otherwise, NAME is reclassified as a package name */
10884 else
10885 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10886
10887 /* Propagate the qualification accross other components of the
10888 qualified name */
10889 for (qual = TREE_CHAIN (qual); qual;
10890 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10891 {
10892 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10893 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10894 else
10895 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10896 }
10897
10898 /* Store the global qualification for the ambiguous part of ID back
10899 into ID fields */
10900 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10901 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10902 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10903 RESOLVE_TYPE_NAME_P (id) = 1;
10904 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10905 RESOLVE_PACKAGE_NAME_P (id) = 1;
10906
10907 /* Restore the current class */
10908 current_class = saved_current_class;
10909}
10910
10911static int
10912breakdown_qualified (left, right, source)
10913 tree *left, *right, source;
10914{
63ad61ed 10915 char *p, *base;
e04a16fb
AG
10916 int l = IDENTIFIER_LENGTH (source);
10917
63ad61ed
ZW
10918 base = alloca (l + 1);
10919 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10920
e04a16fb 10921 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10922 p = base + l - 1;
e04a16fb
AG
10923 while (*p != '.' && p != base)
10924 p--;
10925
10926 /* We didn't find a '.'. Return an error */
10927 if (p == base)
10928 return 1;
10929
10930 *p = '\0';
10931 if (right)
10932 *right = get_identifier (p+1);
63ad61ed 10933 *left = get_identifier (base);
e04a16fb
AG
10934
10935 return 0;
10936}
10937
e04a16fb 10938/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10939 local variable decls if present.
10940 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10941
10942static tree
10943java_complete_tree (node)
10944 tree node;
5b09b33e
PB
10945{
10946 node = java_complete_lhs (node);
0c2b8145
APB
10947 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10948 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10949 && !flag_emit_xref)
5b09b33e
PB
10950 {
10951 tree value = DECL_INITIAL (node);
10952 DECL_INITIAL (node) = NULL_TREE;
10953 value = fold_constant_for_init (value, node);
10954 DECL_INITIAL (node) = value;
10955 if (value != NULL_TREE)
c2952b01
APB
10956 {
10957 /* fold_constant_for_init sometimes widen the original type
10958 of the constant (i.e. byte to int.) It's not desirable,
10959 especially if NODE is a function argument. */
10960 if (TREE_CODE (value) == INTEGER_CST
10961 && TREE_TYPE (node) != TREE_TYPE (value))
10962 return convert (TREE_TYPE (node), value);
10963 else
10964 return value;
10965 }
5b09b33e
PB
10966 }
10967 return node;
10968}
10969
2aa11e97
APB
10970static tree
10971java_stabilize_reference (node)
10972 tree node;
10973{
10974 if (TREE_CODE (node) == COMPOUND_EXPR)
10975 {
10976 tree op0 = TREE_OPERAND (node, 0);
10977 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10978 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10979 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10980 return node;
10981 }
5cbdba64 10982 return stabilize_reference (node);
2aa11e97
APB
10983}
10984
5b09b33e
PB
10985/* Patch tree nodes in a function body. When a BLOCK is found, push
10986 local variable decls if present.
10987 Same as java_complete_tree, but does not resolve static finals to values. */
10988
10989static tree
10990java_complete_lhs (node)
10991 tree node;
e04a16fb 10992{
22eed1e6 10993 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10994 int flag;
e04a16fb
AG
10995
10996 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10997 worked out. */
e04a16fb
AG
10998 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10999 return node;
11000
11001 /* The switch block implements cases processing container nodes
11002 first. Contained nodes are always written back. Leaves come
11003 next and return a value. */
11004 switch (TREE_CODE (node))
11005 {
11006 case BLOCK:
11007
11008 /* 1- Block section.
11009 Set the local values on decl names so we can identify them
11010 faster when they're referenced. At that stage, identifiers
11011 are legal so we don't check for declaration errors. */
11012 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11013 {
11014 DECL_CONTEXT (cn) = current_function_decl;
11015 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11016 }
15fdcfe9
PB
11017 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11018 CAN_COMPLETE_NORMALLY (node) = 1;
11019 else
e04a16fb 11020 {
15fdcfe9
PB
11021 tree stmt = BLOCK_EXPR_BODY (node);
11022 tree *ptr;
11023 int error_seen = 0;
11024 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11025 {
c877974e
APB
11026 /* Re-order from (((A; B); C); ...; Z) to
11027 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11028 This makes it easier to scan the statements left-to-right
11029 without using recursion (which might overflow the stack
11030 if the block has many statements. */
11031 for (;;)
11032 {
11033 tree left = TREE_OPERAND (stmt, 0);
11034 if (TREE_CODE (left) != COMPOUND_EXPR)
11035 break;
11036 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11037 TREE_OPERAND (left, 1) = stmt;
11038 stmt = left;
11039 }
11040 BLOCK_EXPR_BODY (node) = stmt;
11041 }
11042
c877974e
APB
11043 /* Now do the actual complete, without deep recursion for
11044 long blocks. */
15fdcfe9 11045 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11046 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11047 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11048 {
11049 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11050 tree *next = &TREE_OPERAND (*ptr, 1);
11051 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11052 if (cur == empty_stmt_node)
11053 {
11054 /* Optimization; makes it easier to detect empty bodies.
11055 Most useful for <clinit> with all-constant initializer. */
11056 *ptr = *next;
11057 continue;
11058 }
15fdcfe9
PB
11059 if (TREE_CODE (cur) == ERROR_MARK)
11060 error_seen++;
11061 else if (! CAN_COMPLETE_NORMALLY (cur))
11062 {
11063 wfl_op2 = *next;
11064 for (;;)
11065 {
11066 if (TREE_CODE (wfl_op2) == BLOCK)
11067 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11068 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11069 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11070 else
11071 break;
11072 }
11073 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11074 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11075 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11076 }
11077 ptr = next;
11078 }
11079 *ptr = java_complete_tree (*ptr);
11080
11081 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11082 return error_mark_node;
15fdcfe9 11083 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11084 }
11085 /* Turn local bindings to null */
11086 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11087 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11088
11089 TREE_TYPE (node) = void_type_node;
11090 break;
11091
11092 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11093
b9f7e36c
APB
11094 case THROW_EXPR:
11095 wfl_op1 = TREE_OPERAND (node, 0);
11096 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11097 /* 14.19 A throw statement cannot complete normally. */
11098 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11099 return patch_throw_statement (node, wfl_op1);
11100
11101 case SYNCHRONIZED_EXPR:
11102 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11103 return patch_synchronized_statement (node, wfl_op1);
11104
b67d701b
PB
11105 case TRY_EXPR:
11106 return patch_try_statement (node);
11107
a7d8d81f
PB
11108 case TRY_FINALLY_EXPR:
11109 COMPLETE_CHECK_OP_0 (node);
11110 COMPLETE_CHECK_OP_1 (node);
11111 CAN_COMPLETE_NORMALLY (node)
11112 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11113 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11114 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11115 return node;
11116
5a005d9e
PB
11117 case CLEANUP_POINT_EXPR:
11118 COMPLETE_CHECK_OP_0 (node);
11119 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11120 CAN_COMPLETE_NORMALLY (node) =
11121 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11122 return node;
11123
11124 case WITH_CLEANUP_EXPR:
11125 COMPLETE_CHECK_OP_0 (node);
11126 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11127 CAN_COMPLETE_NORMALLY (node) =
11128 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11129 TREE_TYPE (node) = void_type_node;
11130 return node;
11131
e04a16fb
AG
11132 case LABELED_BLOCK_EXPR:
11133 PUSH_LABELED_BLOCK (node);
11134 if (LABELED_BLOCK_BODY (node))
11135 COMPLETE_CHECK_OP_1 (node);
11136 TREE_TYPE (node) = void_type_node;
11137 POP_LABELED_BLOCK ();
1fb89a4d
APB
11138
11139 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11140 {
11141 LABELED_BLOCK_BODY (node) = NULL_TREE;
11142 CAN_COMPLETE_NORMALLY (node) = 1;
11143 }
1fb89a4d 11144 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11145 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11146 return node;
11147
11148 case EXIT_BLOCK_EXPR:
11149 /* We don't complete operand 1, because it's the return value of
11150 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11151 return patch_bc_statement (node);
11152
15fdcfe9
PB
11153 case CASE_EXPR:
11154 cn = java_complete_tree (TREE_OPERAND (node, 0));
11155 if (cn == error_mark_node)
11156 return cn;
11157
8576f094
APB
11158 /* First, the case expression must be constant. Values of final
11159 fields are accepted. */
15fdcfe9 11160 cn = fold (cn);
8576f094
APB
11161 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11162 && JDECL_P (TREE_OPERAND (cn, 1))
11163 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11164 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11165 {
100f7cd8
APB
11166 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11167 TREE_OPERAND (cn, 1));
100f7cd8 11168 }
15fdcfe9 11169
ce6e9147 11170 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11171 {
11172 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11173 parse_error_context (node, "Constant expression required");
11174 return error_mark_node;
11175 }
11176
11177 nn = ctxp->current_loop;
11178
11179 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11180 if (!try_builtin_assignconv (NULL_TREE,
11181 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11182 {
11183 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11184 parse_error_context
11185 (wfl_operator,
11186 "Incompatible type for case. Can't convert `%s' to `int'",
11187 lang_printable_name (TREE_TYPE (cn), 0));
11188 return error_mark_node;
11189 }
11190
11191 cn = fold (convert (int_type_node, cn));
11192
11193 /* Multiple instance of a case label bearing the same
11194 value is checked during code generation. The case
11195 expression is allright so far. */
34d4df06
APB
11196 if (TREE_CODE (cn) == VAR_DECL)
11197 cn = DECL_INITIAL (cn);
15fdcfe9 11198 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11199 TREE_TYPE (node) = void_type_node;
15fdcfe9 11200 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11201 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11202 break;
11203
11204 case DEFAULT_EXPR:
11205 nn = ctxp->current_loop;
11206 /* Only one default label is allowed per switch statement */
11207 if (SWITCH_HAS_DEFAULT (nn))
11208 {
11209 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11210 parse_error_context (wfl_operator,
11211 "Duplicate case label: `default'");
11212 return error_mark_node;
11213 }
11214 else
11215 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11216 TREE_TYPE (node) = void_type_node;
10100cc7 11217 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11218 CAN_COMPLETE_NORMALLY (node) = 1;
11219 break;
11220
b67d701b 11221 case SWITCH_EXPR:
e04a16fb
AG
11222 case LOOP_EXPR:
11223 PUSH_LOOP (node);
11224 /* Check whether the loop was enclosed in a labeled
11225 statement. If not, create one, insert the loop in it and
11226 return the node */
11227 nn = patch_loop_statement (node);
b67d701b 11228
e04a16fb 11229 /* Anyways, walk the body of the loop */
b67d701b
PB
11230 if (TREE_CODE (node) == LOOP_EXPR)
11231 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11232 /* Switch statement: walk the switch expression and the cases */
11233 else
11234 node = patch_switch_statement (node);
11235
e7c7bcef 11236 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11237 nn = error_mark_node;
11238 else
15fdcfe9 11239 {
b635eb2f
PB
11240 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11241 /* If we returned something different, that's because we
11242 inserted a label. Pop the label too. */
11243 if (nn != node)
11244 {
11245 if (CAN_COMPLETE_NORMALLY (node))
11246 CAN_COMPLETE_NORMALLY (nn) = 1;
11247 POP_LABELED_BLOCK ();
11248 }
15fdcfe9 11249 }
e04a16fb
AG
11250 POP_LOOP ();
11251 return nn;
11252
11253 case EXIT_EXPR:
11254 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11255 return patch_exit_expr (node);
11256
11257 case COND_EXPR:
11258 /* Condition */
11259 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11260 if (TREE_OPERAND (node, 0) == error_mark_node)
11261 return error_mark_node;
11262 /* then-else branches */
11263 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11264 if (TREE_OPERAND (node, 1) == error_mark_node)
11265 return error_mark_node;
11266 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11267 if (TREE_OPERAND (node, 2) == error_mark_node)
11268 return error_mark_node;
11269 return patch_if_else_statement (node);
11270 break;
11271
22eed1e6
APB
11272 case CONDITIONAL_EXPR:
11273 /* Condition */
11274 wfl_op1 = TREE_OPERAND (node, 0);
11275 COMPLETE_CHECK_OP_0 (node);
11276 wfl_op2 = TREE_OPERAND (node, 1);
11277 COMPLETE_CHECK_OP_1 (node);
11278 wfl_op3 = TREE_OPERAND (node, 2);
11279 COMPLETE_CHECK_OP_2 (node);
11280 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11281
e04a16fb
AG
11282 /* 3- Expression section */
11283 case COMPOUND_EXPR:
15fdcfe9 11284 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11285 TREE_OPERAND (node, 0) = nn =
11286 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11287 if (wfl_op2 == empty_stmt_node)
11288 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11289 else
15fdcfe9 11290 {
dc0b3eff 11291 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11292 {
dc0b3eff
PB
11293 /* An unreachable condition in a do-while statement
11294 is *not* (technically) an unreachable statement. */
11295 nn = wfl_op2;
11296 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11297 nn = EXPR_WFL_NODE (nn);
11298 if (TREE_CODE (nn) != EXIT_EXPR)
11299 {
11300 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11301 parse_error_context (wfl_operator, "Unreachable statement");
11302 }
bccaf73a 11303 }
dc0b3eff
PB
11304 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11305 if (TREE_OPERAND (node, 1) == error_mark_node)
11306 return error_mark_node;
11307 CAN_COMPLETE_NORMALLY (node)
11308 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11309 }
e04a16fb
AG
11310 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11311 break;
11312
11313 case RETURN_EXPR:
15fdcfe9 11314 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11315 return patch_return (node);
11316
11317 case EXPR_WITH_FILE_LOCATION:
11318 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11319 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11320 {
5423609c 11321 tree wfl = node;
15fdcfe9 11322 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11323 if (node == error_mark_node)
11324 return node;
5423609c
APB
11325 /* Keep line number information somewhere were it doesn't
11326 disrupt the completion process. */
2c56429a 11327 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11328 {
11329 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11330 TREE_OPERAND (node, 1) = wfl;
11331 }
15fdcfe9
PB
11332 CAN_COMPLETE_NORMALLY (node) = 1;
11333 }
e04a16fb
AG
11334 else
11335 {
5b09b33e
PB
11336 tree body;
11337 int save_lineno = lineno;
11338 lineno = EXPR_WFL_LINENO (node);
11339 body = java_complete_tree (EXPR_WFL_NODE (node));
11340 lineno = save_lineno;
15fdcfe9 11341 EXPR_WFL_NODE (node) = body;
dc0b3eff 11342 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11343 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11344 if (body == empty_stmt_node)
11345 {
11346 /* Optimization; makes it easier to detect empty bodies. */
11347 return body;
11348 }
dc0b3eff 11349 if (body == error_mark_node)
e04a16fb
AG
11350 {
11351 /* Its important for the evaluation of assignment that
11352 this mark on the TREE_TYPE is propagated. */
11353 TREE_TYPE (node) = error_mark_node;
11354 return error_mark_node;
11355 }
11356 else
11357 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11358
e04a16fb
AG
11359 }
11360 break;
11361
b67d701b 11362 case NEW_ARRAY_EXPR:
e04a16fb
AG
11363 /* Patch all the dimensions */
11364 flag = 0;
11365 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11366 {
11367 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11368 tree dim = convert (int_type_node,
11369 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11370 if (dim == error_mark_node)
11371 {
11372 flag = 1;
11373 continue;
11374 }
11375 else
11376 {
b9f7e36c 11377 TREE_VALUE (cn) = dim;
e04a16fb
AG
11378 /* Setup the location of the current dimension, for
11379 later error report. */
11380 TREE_PURPOSE (cn) =
11381 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11382 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11383 }
11384 }
11385 /* They complete the array creation expression, if no errors
11386 were found. */
15fdcfe9 11387 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11388 return (flag ? error_mark_node
11389 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11390
c2952b01
APB
11391 case NEW_ANONYMOUS_ARRAY_EXPR:
11392 /* Create the array type if necessary. */
11393 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11394 {
11395 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11396 if (!(type = resolve_type_during_patch (type)))
11397 return error_mark_node;
11398 type = build_array_from_name (type, NULL_TREE,
11399 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11400 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11401 }
11402 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11403 ANONYMOUS_ARRAY_INITIALIZER (node));
11404 if (node == error_mark_node)
11405 return error_mark_node;
11406 CAN_COMPLETE_NORMALLY (node) = 1;
11407 return node;
11408
b67d701b 11409 case NEW_CLASS_EXPR:
e04a16fb 11410 case CALL_EXPR:
b67d701b 11411 /* Complete function's argument(s) first */
e04a16fb
AG
11412 if (complete_function_arguments (node))
11413 return error_mark_node;
11414 else
b9f7e36c 11415 {
22eed1e6
APB
11416 tree decl, wfl = TREE_OPERAND (node, 0);
11417 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11418
c877974e 11419 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11420 NULL_TREE, 0, &decl);
c877974e
APB
11421 if (node == error_mark_node)
11422 return error_mark_node;
11423
11424 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11425 /* If we call this(...), register signature and positions */
11426 if (in_this)
11427 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11428 tree_cons (wfl, decl,
11429 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11430 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11431 return force_evaluation_order (node);
b9f7e36c 11432 }
e04a16fb
AG
11433
11434 case MODIFY_EXPR:
11435 /* Save potential wfls */
11436 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11437 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11438
cd9643f7
PB
11439 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11440 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11441 && DECL_INITIAL (nn) != NULL_TREE)
11442 {
100f7cd8
APB
11443 tree value;
11444
100f7cd8 11445 value = fold_constant_for_init (nn, nn);
c2952b01 11446
cd9643f7
PB
11447 if (value != NULL_TREE)
11448 {
11449 tree type = TREE_TYPE (value);
c2952b01
APB
11450 if (JPRIMITIVE_TYPE_P (type) ||
11451 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11452 return empty_stmt_node;
11453 }
629d4b4d
APB
11454 if (! flag_emit_class_files)
11455 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11456 }
e04a16fb 11457 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11458
e04a16fb
AG
11459 if (TREE_OPERAND (node, 0) == error_mark_node)
11460 return error_mark_node;
11461
5cbdba64
APB
11462 flag = COMPOUND_ASSIGN_P (wfl_op2);
11463 if (flag)
e04a16fb 11464 {
c2952b01
APB
11465 /* This might break when accessing outer field from inner
11466 class. TESTME, FIXME */
2aa11e97 11467 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11468
11469 /* Hand stablize the lhs on both places */
e04a16fb 11470 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11471 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11472 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11473
5cbdba64 11474 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11475 /* Now complete the RHS. We write it back later on. */
11476 nn = java_complete_tree (TREE_OPERAND (node, 1));
11477
642f15d1
APB
11478 if ((cn = patch_string (nn)))
11479 nn = cn;
11480
2aa11e97
APB
11481 /* The last part of the rewrite for E1 op= E2 is to have
11482 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11483 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11484 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11485
11486 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11487 }
11488
f8976021 11489 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11490 function to complete this RHS. Note that a NEW_ARRAY_INIT
11491 might have been already fully expanded if created as a result
11492 of processing an anonymous array initializer. We avoid doing
11493 the operation twice by testing whether the node already bears
11494 a type. */
11495 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11496 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11497 TREE_OPERAND (node, 1));
2aa11e97 11498 /* Otherwise we simply complete the RHS */
f8976021
APB
11499 else
11500 nn = java_complete_tree (TREE_OPERAND (node, 1));
11501
e04a16fb 11502 if (nn == error_mark_node)
c0d87ff6 11503 return error_mark_node;
2aa11e97
APB
11504
11505 /* Write back the RHS as we evaluated it. */
e04a16fb 11506 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11507
11508 /* In case we're handling = with a String as a RHS, we need to
11509 produce a String out of the RHS (it might still be a
11510 STRING_CST or a StringBuffer at this stage */
11511 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11512 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11513
11514 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11515 TREE_OPERAND (node, 1))))
11516 {
11517 /* We return error_mark_node if outer_field_access_fix
11518 detects we write into a final. */
11519 if (nn == error_mark_node)
11520 return error_mark_node;
11521 node = nn;
11522 }
11523 else
11524 {
11525 node = patch_assignment (node, wfl_op1, wfl_op2);
11526 /* Reorganize the tree if necessary. */
11527 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11528 || JSTRING_P (TREE_TYPE (node))))
11529 node = java_refold (node);
11530 }
11531
15fdcfe9
PB
11532 CAN_COMPLETE_NORMALLY (node) = 1;
11533 return node;
e04a16fb
AG
11534
11535 case MULT_EXPR:
11536 case PLUS_EXPR:
11537 case MINUS_EXPR:
11538 case LSHIFT_EXPR:
11539 case RSHIFT_EXPR:
11540 case URSHIFT_EXPR:
11541 case BIT_AND_EXPR:
11542 case BIT_XOR_EXPR:
11543 case BIT_IOR_EXPR:
11544 case TRUNC_MOD_EXPR:
c2952b01 11545 case TRUNC_DIV_EXPR:
e04a16fb
AG
11546 case RDIV_EXPR:
11547 case TRUTH_ANDIF_EXPR:
11548 case TRUTH_ORIF_EXPR:
11549 case EQ_EXPR:
11550 case NE_EXPR:
11551 case GT_EXPR:
11552 case GE_EXPR:
11553 case LT_EXPR:
11554 case LE_EXPR:
11555 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11556 knows how to handle those cases. */
11557 wfl_op1 = TREE_OPERAND (node, 0);
11558 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11559
15fdcfe9 11560 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11561 /* Don't complete string nodes if dealing with the PLUS operand. */
11562 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11563 {
11564 nn = java_complete_tree (wfl_op1);
11565 if (nn == error_mark_node)
11566 return error_mark_node;
48a840d9 11567
2aa11e97
APB
11568 TREE_OPERAND (node, 0) = nn;
11569 }
b67d701b 11570 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11571 {
11572 nn = java_complete_tree (wfl_op2);
11573 if (nn == error_mark_node)
11574 return error_mark_node;
48a840d9 11575
2aa11e97
APB
11576 TREE_OPERAND (node, 1) = nn;
11577 }
dc0b3eff 11578 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11579
5e942c50
APB
11580 case INSTANCEOF_EXPR:
11581 wfl_op1 = TREE_OPERAND (node, 0);
11582 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11583 if (flag_emit_xref)
11584 {
11585 TREE_TYPE (node) = boolean_type_node;
11586 return node;
11587 }
5e942c50
APB
11588 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11589
b67d701b 11590 case UNARY_PLUS_EXPR:
e04a16fb
AG
11591 case NEGATE_EXPR:
11592 case TRUTH_NOT_EXPR:
11593 case BIT_NOT_EXPR:
11594 case PREDECREMENT_EXPR:
11595 case PREINCREMENT_EXPR:
11596 case POSTDECREMENT_EXPR:
11597 case POSTINCREMENT_EXPR:
11598 case CONVERT_EXPR:
11599 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11600 how to handle those cases. */
11601 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11602 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11603 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11604 if (TREE_OPERAND (node, 0) == error_mark_node)
11605 return error_mark_node;
4a5f66c3
APB
11606 node = patch_unaryop (node, wfl_op1);
11607 CAN_COMPLETE_NORMALLY (node) = 1;
11608 break;
e04a16fb
AG
11609
11610 case ARRAY_REF:
11611 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11612 how to handle those cases. */
11613 wfl_op1 = TREE_OPERAND (node, 0);
11614 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11615 if (TREE_OPERAND (node, 0) == error_mark_node)
11616 return error_mark_node;
7f1d4866 11617 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11618 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11619 /* The same applies to wfl_op2 */
11620 wfl_op2 = TREE_OPERAND (node, 1);
11621 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11622 if (TREE_OPERAND (node, 1) == error_mark_node)
11623 return error_mark_node;
7f1d4866 11624 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11625 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11626 return patch_array_ref (node);
e04a16fb 11627
63a212ed
PB
11628 case RECORD_TYPE:
11629 return node;;
11630
11631 case COMPONENT_REF:
11632 /* The first step in the re-write of qualified name handling. FIXME.
11633 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11634 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11635 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11636 {
11637 tree name = TREE_OPERAND (node, 1);
11638 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11639 if (field == NULL_TREE)
11640 {
11641 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11642 return error_mark_node;
11643 }
11644 if (! FIELD_STATIC (field))
11645 {
11646 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11647 return error_mark_node;
11648 }
11649 return field;
11650 }
11651 else
11652 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11653 break;
9bbc7d9f 11654
b67d701b 11655 case THIS_EXPR:
e04a16fb
AG
11656 /* Can't use THIS in a static environment */
11657 if (!current_this)
11658 {
11659 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11660 parse_error_context (wfl_operator,
11661 "Keyword `this' used outside allowed context");
e04a16fb
AG
11662 TREE_TYPE (node) = error_mark_node;
11663 return error_mark_node;
11664 }
22eed1e6
APB
11665 if (ctxp->explicit_constructor_p)
11666 {
11667 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11668 parse_error_context
781b0558 11669 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11670 TREE_TYPE (node) = error_mark_node;
11671 return error_mark_node;
11672 }
e04a16fb 11673 return current_this;
c2952b01
APB
11674
11675 case CLASS_LITERAL:
11676 CAN_COMPLETE_NORMALLY (node) = 1;
11677 node = patch_incomplete_class_ref (node);
11678 if (node == error_mark_node)
11679 return error_mark_node;
11680 break;
11681
11682 case INSTANCE_INITIALIZERS_EXPR:
11683 in_instance_initializer++;
11684 node = java_complete_tree (TREE_OPERAND (node, 0));
11685 in_instance_initializer--;
11686 if (node != error_mark_node)
11687 TREE_TYPE (node) = void_type_node;
11688 else
11689 return error_mark_node;
11690 break;
e04a16fb 11691
e04a16fb 11692 default:
15fdcfe9 11693 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11694 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11695 and it's time to turn it into the appropriate String object */
11696 if ((nn = patch_string (node)))
11697 node = nn;
11698 else
11699 fatal ("No case for tree code `%s' - java_complete_tree\n",
11700 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11701 }
11702 return node;
11703}
11704
11705/* Complete function call's argument. Return a non zero value is an
11706 error was found. */
11707
11708static int
11709complete_function_arguments (node)
11710 tree node;
11711{
11712 int flag = 0;
11713 tree cn;
11714
f63991a8 11715 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11716 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11717 {
b67d701b 11718 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11719 parm = java_complete_tree (wfl);
c2952b01 11720
e04a16fb
AG
11721 if (parm == error_mark_node)
11722 {
11723 flag = 1;
11724 continue;
11725 }
b67d701b
PB
11726 /* If have a string literal that we haven't transformed yet or a
11727 crafted string buffer, as a result of use of the the String
11728 `+' operator. Build `parm.toString()' and expand it. */
11729 if ((temp = patch_string (parm)))
b9f7e36c 11730 parm = temp;
5e942c50
APB
11731 /* Inline PRIMTYPE.TYPE read access */
11732 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11733
5e942c50 11734 TREE_VALUE (cn) = parm;
e04a16fb 11735 }
f63991a8 11736 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11737 return flag;
11738}
11739
11740/* Sometimes (for loops and variable initialized during their
11741 declaration), we want to wrap a statement around a WFL and turn it
11742 debugable. */
11743
11744static tree
11745build_debugable_stmt (location, stmt)
11746 int location;
11747 tree stmt;
11748{
11749 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11750 {
11751 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11752 EXPR_WFL_LINECOL (stmt) = location;
11753 }
11754 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11755 return stmt;
11756}
11757
11758static tree
11759build_expr_block (body, decls)
11760 tree body, decls;
11761{
11762 tree node = make_node (BLOCK);
11763 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11764 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11765 if (body)
11766 TREE_TYPE (node) = TREE_TYPE (body);
11767 TREE_SIDE_EFFECTS (node) = 1;
11768 return node;
11769}
11770
b67d701b
PB
11771/* Create a new function block and link it approriately to current
11772 function block chain */
e04a16fb
AG
11773
11774static tree
11775enter_block ()
11776{
b67d701b
PB
11777 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11778}
11779
11780/* Link block B supercontext to the previous block. The current
11781 function DECL is used as supercontext when enter_a_block is called
11782 for the first time for a given function. The current function body
11783 (DECL_FUNCTION_BODY) is set to be block B. */
11784
11785static tree
11786enter_a_block (b)
11787 tree b;
11788{
e04a16fb
AG
11789 tree fndecl = current_function_decl;
11790
f099f336
APB
11791 if (!fndecl) {
11792 BLOCK_SUPERCONTEXT (b) = current_static_block;
11793 current_static_block = b;
11794 }
11795
11796 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11797 {
11798 BLOCK_SUPERCONTEXT (b) = fndecl;
11799 DECL_FUNCTION_BODY (fndecl) = b;
11800 }
11801 else
11802 {
11803 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11804 DECL_FUNCTION_BODY (fndecl) = b;
11805 }
11806 return b;
11807}
11808
11809/* Exit a block by changing the current function body
11810 (DECL_FUNCTION_BODY) to the current block super context, only if
11811 the block being exited isn't the method's top level one. */
11812
11813static tree
11814exit_block ()
11815{
f099f336
APB
11816 tree b;
11817 if (current_function_decl)
11818 {
11819 b = DECL_FUNCTION_BODY (current_function_decl);
11820 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11821 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11822 }
11823 else
11824 {
11825 b = current_static_block;
e04a16fb 11826
f099f336
APB
11827 if (BLOCK_SUPERCONTEXT (b))
11828 current_static_block = BLOCK_SUPERCONTEXT (b);
11829 }
e04a16fb
AG
11830 return b;
11831}
11832
11833/* Lookup for NAME in the nested function's blocks, all the way up to
11834 the current toplevel one. It complies with Java's local variable
11835 scoping rules. */
11836
11837static tree
11838lookup_name_in_blocks (name)
11839 tree name;
11840{
f099f336 11841 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11842
11843 while (b != current_function_decl)
11844 {
11845 tree current;
11846
11847 /* Paranoid sanity check. To be removed */
11848 if (TREE_CODE (b) != BLOCK)
11849 fatal ("non block expr function body - lookup_name_in_blocks");
11850
11851 for (current = BLOCK_EXPR_DECLS (b); current;
11852 current = TREE_CHAIN (current))
11853 if (DECL_NAME (current) == name)
11854 return current;
11855 b = BLOCK_SUPERCONTEXT (b);
11856 }
11857 return NULL_TREE;
11858}
11859
11860static void
11861maybe_absorb_scoping_blocks ()
11862{
f099f336 11863 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11864 {
11865 tree b = exit_block ();
11866 java_method_add_stmt (current_function_decl, b);
11867 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11868 }
11869}
11870
11871\f
11872/* This section of the source is reserved to build_* functions that
11873 are building incomplete tree nodes and the patch_* functions that
11874 are completing them. */
11875
c2952b01
APB
11876/* Wrap a non WFL node around a WFL. */
11877static tree
9a7ab4b3 11878build_wfl_wrap (node, location)
c2952b01 11879 tree node;
9a7ab4b3 11880 int location;
c2952b01
APB
11881{
11882 tree wfl, node_to_insert = node;
11883
11884 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11885 with the way we're processing SUPER. A THIS from a primary as a
11886 different form than a SUPER. Turn THIS into something symbolic */
11887 if (TREE_CODE (node) == THIS_EXPR)
11888 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11889 else
11890 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11891
9a7ab4b3 11892 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11893 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11894 return wfl;
11895}
11896
11897
9bbc7d9f 11898/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11899 we're currently dealing with the class java.lang.Object. */
11900
11901static tree
e920ebc9
APB
11902build_super_invocation (mdecl)
11903 tree mdecl;
22eed1e6 11904{
e920ebc9 11905 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11906 return empty_stmt_node;
22eed1e6
APB
11907 else
11908 {
9ee9b555 11909 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11910 tree a = NULL_TREE, t;
11911 /* If we're dealing with an anonymous class, pass the arguments
11912 of the crafted constructor along. */
11913 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11914 {
11915 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11916 for (; t != end_params_node; t = TREE_CHAIN (t))
11917 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11918 }
11919 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11920 }
11921}
11922
11923/* Build a SUPER/THIS qualified method invocation. */
11924
11925static tree
11926build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11927 int use_this;
11928 tree name, args;
11929 int lloc, rloc;
22eed1e6
APB
11930{
11931 tree invok;
11932 tree wfl =
9ee9b555 11933 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11934 EXPR_WFL_LINECOL (wfl) = lloc;
11935 invok = build_method_invocation (name, args);
11936 return make_qualified_primary (wfl, invok, rloc);
11937}
11938
b67d701b 11939/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11940
11941static tree
11942build_method_invocation (name, args)
11943 tree name;
11944 tree args;
11945{
11946 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11947 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11948 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11949 return call;
11950}
11951
11952/* Build an incomplete new xxx(...) node. */
11953
11954static tree
11955build_new_invocation (name, args)
11956 tree name, args;
11957{
11958 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11959 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11960 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11961 return call;
11962}
11963
11964/* Build an incomplete assignment expression. */
11965
11966static tree
11967build_assignment (op, op_location, lhs, rhs)
11968 int op, op_location;
11969 tree lhs, rhs;
11970{
11971 tree assignment;
11972 /* Build the corresponding binop if we deal with a Compound
11973 Assignment operator. Mark the binop sub-tree as part of a
11974 Compound Assignment expression */
11975 if (op != ASSIGN_TK)
11976 {
11977 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11978 COMPOUND_ASSIGN_P (rhs) = 1;
11979 }
11980 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11981 TREE_SIDE_EFFECTS (assignment) = 1;
11982 EXPR_WFL_LINECOL (assignment) = op_location;
11983 return assignment;
11984}
11985
11986/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11987
15fdcfe9 11988char *
e04a16fb
AG
11989print_int_node (node)
11990 tree node;
11991{
11992 static char buffer [80];
11993 if (TREE_CONSTANT_OVERFLOW (node))
11994 sprintf (buffer, "<overflow>");
11995
11996 if (TREE_INT_CST_HIGH (node) == 0)
11997 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11998 TREE_INT_CST_LOW (node));
11999 else if (TREE_INT_CST_HIGH (node) == -1
12000 && TREE_INT_CST_LOW (node) != 0)
12001 {
12002 buffer [0] = '-';
12003 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12004 -TREE_INT_CST_LOW (node));
12005 }
12006 else
12007 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12008 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12009
12010 return buffer;
12011}
12012
7f1d4866
APB
12013/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12014 context. */
5e942c50
APB
12015
12016static int
12017check_final_assignment (lvalue, wfl)
12018 tree lvalue, wfl;
12019{
6632dcdd
APB
12020 if (TREE_CODE (lvalue) == COMPOUND_EXPR
12021 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12022 lvalue = TREE_OPERAND (lvalue, 1);
12023
bc2874c9
TT
12024 /* When generating class files, references to the `length' field
12025 look a bit different. */
12026 if ((flag_emit_class_files
12027 && TREE_CODE (lvalue) == COMPONENT_REF
12028 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
12029 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
12030 || (TREE_CODE (lvalue) == FIELD_DECL
12031 && FIELD_FINAL (lvalue)
12032 && !DECL_CLINIT_P (current_function_decl)
12033 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
12034 {
12035 parse_error_context
12036 (wfl, "Can't assign a value to the final variable `%s'",
12037 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12038 return 1;
12039 }
12040 return 0;
12041}
12042
12043/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12044 read. This is needed to avoid circularities in the implementation
12045 of these fields in libjava. */
12046
12047static tree
12048maybe_build_primttype_type_ref (rhs, wfl)
12049 tree rhs, wfl;
12050{
12051 tree to_return = NULL_TREE;
12052 tree rhs_type = TREE_TYPE (rhs);
12053 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12054 {
12055 tree n = TREE_OPERAND (rhs, 1);
12056 if (TREE_CODE (n) == VAR_DECL
12057 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12058 && rhs_type == class_ptr_type
12059 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12060 {
49f48c71 12061 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12062 if (!strncmp (self_name, "java.lang.", 10))
12063 to_return = build_primtype_type_ref (self_name);
12064 }
12065 }
12066 return (to_return ? to_return : rhs );
12067}
12068
e04a16fb
AG
12069/* 15.25 Assignment operators. */
12070
12071static tree
12072patch_assignment (node, wfl_op1, wfl_op2)
12073 tree node;
12074 tree wfl_op1;
12075 tree wfl_op2;
12076{
0a2138e2 12077 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12078 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12079 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12080 int error_found = 0;
12081 int lvalue_from_array = 0;
12082
c2952b01 12083 /* Can't assign to a (blank) final. */
5e942c50
APB
12084 if (check_final_assignment (lvalue, wfl_op1))
12085 error_found = 1;
e04a16fb
AG
12086
12087 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12088
12089 /* Lhs can be a named variable */
34f4db93 12090 if (JDECL_P (lvalue))
e04a16fb 12091 {
e04a16fb
AG
12092 lhs_type = TREE_TYPE (lvalue);
12093 }
12094 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12095 comment on reason why */
12096 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12097 {
12098 lhs_type = TREE_TYPE (lvalue);
12099 lvalue_from_array = 1;
12100 }
12101 /* Or a field access */
12102 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12103 lhs_type = TREE_TYPE (lvalue);
12104 /* Or a function return slot */
12105 else if (TREE_CODE (lvalue) == RESULT_DECL)
12106 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12107 /* Otherwise, we might want to try to write into an optimized static
12108 final, this is an of a different nature, reported further on. */
12109 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12110 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12111 {
6632dcdd 12112 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12113 {
12114 /* What we should do instead is resetting the all the flags
12115 previously set, exchange lvalue for llvalue and continue. */
12116 error_found = 1;
12117 return error_mark_node;
12118 }
12119 else
12120 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12121 }
12122 else
e04a16fb
AG
12123 {
12124 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12125 error_found = 1;
12126 }
12127
12128 rhs_type = TREE_TYPE (rhs);
b67d701b 12129 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12130 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12131
b67d701b 12132 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12133 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12134 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12135
12136 /* 15.25.2 If we have a compound assignment, convert RHS into the
12137 type of the LHS */
12138 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12139 new_rhs = convert (lhs_type, rhs);
12140
12141 /* Explicit cast required. This is an error */
12142 if (!new_rhs)
12143 {
c2e3db92
KG
12144 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12145 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12146 tree wfl;
12147 char operation [32]; /* Max size known */
12148
12149 /* If the assignment is part of a declaration, we use the WFL of
12150 the declared variable to point out the error and call it a
12151 declaration problem. If the assignment is a genuine =
12152 operator, we call is a operator `=' problem, otherwise we
12153 call it an assignment problem. In both of these last cases,
12154 we use the WFL of the operator to indicate the error. */
12155
12156 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12157 {
12158 wfl = wfl_op1;
12159 strcpy (operation, "declaration");
12160 }
12161 else
12162 {
12163 wfl = wfl_operator;
12164 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12165 strcpy (operation, "assignment");
12166 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12167 strcpy (operation, "`return'");
12168 else
12169 strcpy (operation, "`='");
12170 }
12171
1ebadc60 12172 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12173 parse_error_context
12174 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12175 operation, t1, t2);
1ebadc60 12176 else
781b0558 12177 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12178 operation, t1, t2);
e04a16fb
AG
12179 free (t1); free (t2);
12180 error_found = 1;
12181 }
12182
c877974e
APB
12183 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12184 if (new_rhs)
12185 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12186
e04a16fb
AG
12187 if (error_found)
12188 return error_mark_node;
12189
2622b947
APB
12190 /* 10.10: Array Store Exception runtime check */
12191 if (!flag_emit_class_files
e8fc7396 12192 && !flag_emit_xref
2622b947 12193 && lvalue_from_array
afc390b1 12194 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12195 {
12196 tree check;
12197 tree base = lvalue;
12198
12199 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12200 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12201 base = TREE_OPERAND (lvalue, 0);
12202 else
12203 {
12204 if (flag_bounds_check)
12205 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12206 else
12207 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12208 }
12209
12210 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12211 new_rhs = save_expr (new_rhs);
2622b947
APB
12212 check = build (CALL_EXPR, void_type_node,
12213 build_address_of (soft_checkarraystore_node),
12214 tree_cons (NULL_TREE, base,
12215 build_tree_list (NULL_TREE, new_rhs)),
12216 NULL_TREE);
12217 TREE_SIDE_EFFECTS (check) = 1;
12218
12219 /* We have to decide on an insertion point */
12220 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12221 {
12222 tree t;
12223 if (flag_bounds_check)
12224 {
12225 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12226 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12227 build (COMPOUND_EXPR, void_type_node, t, check);
12228 }
12229 else
12230 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12231 check, TREE_OPERAND (lvalue, 1));
12232 }
12233 else
12234 {
12235 /* Make sure the bound check will happen before the store check */
12236 if (flag_bounds_check)
12237 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12238 build (COMPOUND_EXPR, void_type_node,
12239 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12240 else
12241 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12242 }
12243 }
22eed1e6 12244
34d4df06
APB
12245 /* Final locals can be used as case values in switch
12246 statement. Prepare them for this eventuality. */
12247 if (TREE_CODE (lvalue) == VAR_DECL
12248 && LOCAL_FINAL (lvalue)
12249 && TREE_CONSTANT (new_rhs)
12250 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12251 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12252 )
12253 {
12254 TREE_CONSTANT (lvalue) = 1;
12255 DECL_INITIAL (lvalue) = new_rhs;
12256 }
12257
e04a16fb
AG
12258 TREE_OPERAND (node, 0) = lvalue;
12259 TREE_OPERAND (node, 1) = new_rhs;
12260 TREE_TYPE (node) = lhs_type;
12261 return node;
12262}
12263
b67d701b
PB
12264/* Check that type SOURCE can be cast into type DEST. If the cast
12265 can't occur at all, return 0 otherwise 1. This function is used to
12266 produce accurate error messages on the reasons why an assignment
12267 failed. */
e04a16fb 12268
b67d701b
PB
12269static tree
12270try_reference_assignconv (lhs_type, rhs)
12271 tree lhs_type, rhs;
e04a16fb 12272{
b67d701b
PB
12273 tree new_rhs = NULL_TREE;
12274 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12275
b67d701b
PB
12276 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12277 {
12278 /* `null' may be assigned to any reference type */
12279 if (rhs == null_pointer_node)
12280 new_rhs = null_pointer_node;
12281 /* Try the reference assignment conversion */
12282 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12283 new_rhs = rhs;
12284 /* This is a magic assignment that we process differently */
12285 else if (rhs == soft_exceptioninfo_call_node)
12286 new_rhs = rhs;
12287 }
12288 return new_rhs;
12289}
12290
12291/* Check that RHS can be converted into LHS_TYPE by the assignment
12292 conversion (5.2), for the cases of RHS being a builtin type. Return
12293 NULL_TREE if the conversion fails or if because RHS isn't of a
12294 builtin type. Return a converted RHS if the conversion is possible. */
12295
12296static tree
12297try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12298 tree wfl_op1, lhs_type, rhs;
12299{
12300 tree new_rhs = NULL_TREE;
12301 tree rhs_type = TREE_TYPE (rhs);
12302
7e51098e
TT
12303 /* Handle boolean specially. */
12304 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12305 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12306 {
12307 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12308 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12309 new_rhs = rhs;
12310 }
12311
5e942c50 12312 /* Zero accepted everywhere */
7e51098e 12313 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12314 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12315 && JPRIMITIVE_TYPE_P (rhs_type))
12316 new_rhs = convert (lhs_type, rhs);
12317
b67d701b
PB
12318 /* 5.1.1 Try Identity Conversion,
12319 5.1.2 Try Widening Primitive Conversion */
5e942c50 12320 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12321 new_rhs = convert (lhs_type, rhs);
12322
12323 /* Try a narrowing primitive conversion (5.1.3):
12324 - expression is a constant expression of type int AND
12325 - variable is byte, short or char AND
12326 - The value of the expression is representable in the type of the
12327 variable */
12328 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12329 && (lhs_type == byte_type_node || lhs_type == char_type_node
12330 || lhs_type == short_type_node))
12331 {
12332 if (int_fits_type_p (rhs, lhs_type))
12333 new_rhs = convert (lhs_type, rhs);
12334 else if (wfl_op1) /* Might be called with a NULL */
12335 parse_warning_context
7e51098e 12336 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12337 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12338 /* Reported a warning that will turn into an error further
12339 down, so we don't return */
12340 }
12341
12342 return new_rhs;
12343}
12344
12345/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12346 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12347 0 is the conversion test fails. This implements parts the method
12348 invocation convertion (5.3). */
12349
12350static int
12351valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12352 tree lhs_type, rhs_type;
12353{
acd663ee 12354 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12355 if (lhs_type == rhs_type)
12356 return 1;
12357
7e51098e
TT
12358 /* Reject non primitive types and boolean conversions. */
12359 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12360 return 0;
12361
acd663ee
APB
12362 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12363 than a char can't be converted into a char. Short can't too, but
12364 the < test below takes care of that */
b67d701b
PB
12365 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12366 return 0;
12367
5e942c50
APB
12368 /* Accept all promoted type here. Note, we can't use <= in the test
12369 below, because we still need to bounce out assignments of short
12370 to char and the likes */
12371 if (lhs_type == int_type_node
12372 && (rhs_type == promoted_byte_type_node
12373 || rhs_type == promoted_short_type_node
12374 || rhs_type == promoted_char_type_node
12375 || rhs_type == promoted_boolean_type_node))
12376 return 1;
12377
acd663ee
APB
12378 /* From here, an integral is widened if its precision is smaller
12379 than the precision of the LHS or if the LHS is a floating point
12380 type, or the RHS is a float and the RHS a double. */
12381 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12382 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12383 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12384 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12385 return 1;
12386
12387 return 0;
e04a16fb
AG
12388}
12389
12390/* Check that something of SOURCE type can be assigned or cast to
12391 something of DEST type at runtime. Return 1 if the operation is
12392 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12393 were SOURCE is cast into DEST, which borrows a lot of the
12394 assignment check. */
12395
12396static int
12397valid_ref_assignconv_cast_p (source, dest, cast)
12398 tree source;
12399 tree dest;
12400 int cast;
12401{
09ed0f70
APB
12402 /* SOURCE or DEST might be null if not from a declared entity. */
12403 if (!source || !dest)
12404 return 0;
5e942c50
APB
12405 if (JNULLP_TYPE_P (source))
12406 return 1;
e04a16fb
AG
12407 if (TREE_CODE (source) == POINTER_TYPE)
12408 source = TREE_TYPE (source);
12409 if (TREE_CODE (dest) == POINTER_TYPE)
12410 dest = TREE_TYPE (dest);
12411 /* Case where SOURCE is a class type */
12412 if (TYPE_CLASS_P (source))
12413 {
12414 if (TYPE_CLASS_P (dest))
c2952b01
APB
12415 return (source == dest
12416 || inherits_from_p (source, dest)
c2952b01 12417 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12418 if (TYPE_INTERFACE_P (dest))
12419 {
12420 /* If doing a cast and SOURCE is final, the operation is
12421 always correct a compile time (because even if SOURCE
12422 does not implement DEST, a subclass of SOURCE might). */
12423 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12424 return 1;
12425 /* Otherwise, SOURCE must implement DEST */
12426 return interface_of_p (dest, source);
12427 }
12428 /* DEST is an array, cast permited if SOURCE is of Object type */
12429 return (cast && source == object_type_node ? 1 : 0);
12430 }
12431 if (TYPE_INTERFACE_P (source))
12432 {
12433 if (TYPE_CLASS_P (dest))
12434 {
12435 /* If not casting, DEST must be the Object type */
12436 if (!cast)
12437 return dest == object_type_node;
12438 /* We're doing a cast. The cast is always valid is class
12439 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12440 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12441 return 1;
12442 else
12443 return interface_of_p (source, dest);
12444 }
12445 if (TYPE_INTERFACE_P (dest))
12446 {
12447 /* If doing a cast, then if SOURCE and DEST contain method
12448 with the same signature but different return type, then
12449 this is a (compile time) error */
12450 if (cast)
12451 {
12452 tree method_source, method_dest;
12453 tree source_type;
0a2138e2 12454 tree source_sig;
e04a16fb
AG
12455 tree source_name;
12456 for (method_source = TYPE_METHODS (source); method_source;
12457 method_source = TREE_CHAIN (method_source))
12458 {
12459 source_sig =
12460 build_java_argument_signature (TREE_TYPE (method_source));
12461 source_type = TREE_TYPE (TREE_TYPE (method_source));
12462 source_name = DECL_NAME (method_source);
12463 for (method_dest = TYPE_METHODS (dest);
12464 method_dest; method_dest = TREE_CHAIN (method_dest))
12465 if (source_sig ==
12466 build_java_argument_signature (TREE_TYPE (method_dest))
12467 && source_name == DECL_NAME (method_dest)
12468 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12469 return 0;
12470 }
12471 return 1;
12472 }
12473 else
12474 return source == dest || interface_of_p (dest, source);
12475 }
ee17a290
TT
12476 else
12477 {
12478 /* Array */
12479 return (cast
12480 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12481 || (DECL_NAME (TYPE_NAME (source))
12482 == java_io_serializable)));
12483 }
e04a16fb
AG
12484 }
12485 if (TYPE_ARRAY_P (source))
12486 {
12487 if (TYPE_CLASS_P (dest))
12488 return dest == object_type_node;
09ed0f70 12489 /* Can't cast an array to an interface unless the interface is
ee17a290 12490 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12491 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12492 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12493 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12494 else /* Arrays */
12495 {
12496 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12497 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12498
b9f7e36c
APB
12499 /* In case of severe errors, they turn out null */
12500 if (!dest_element_type || !source_element_type)
12501 return 0;
e04a16fb
AG
12502 if (source_element_type == dest_element_type)
12503 return 1;
12504 return valid_ref_assignconv_cast_p (source_element_type,
12505 dest_element_type, cast);
12506 }
12507 return 0;
12508 }
12509 return 0;
12510}
12511
b67d701b
PB
12512static int
12513valid_cast_to_p (source, dest)
12514 tree source;
12515 tree dest;
12516{
12517 if (TREE_CODE (source) == POINTER_TYPE)
12518 source = TREE_TYPE (source);
12519 if (TREE_CODE (dest) == POINTER_TYPE)
12520 dest = TREE_TYPE (dest);
12521
12522 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12523 return valid_ref_assignconv_cast_p (source, dest, 1);
12524
12525 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12526 return 1;
12527
7e51098e
TT
12528 else if (TREE_CODE (source) == BOOLEAN_TYPE
12529 && TREE_CODE (dest) == BOOLEAN_TYPE)
12530 return 1;
12531
b67d701b
PB
12532 return 0;
12533}
12534
15fdcfe9
PB
12535static tree
12536do_unary_numeric_promotion (arg)
12537 tree arg;
12538{
12539 tree type = TREE_TYPE (arg);
7e51098e
TT
12540 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12541 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12542 arg = convert (int_type_node, arg);
12543 return arg;
12544}
12545
acd663ee
APB
12546/* Return a non zero value if SOURCE can be converted into DEST using
12547 the method invocation conversion rule (5.3). */
b67d701b
PB
12548static int
12549valid_method_invocation_conversion_p (dest, source)
12550 tree dest, source;
12551{
e3884b71 12552 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12553 && valid_builtin_assignconv_identity_widening_p (dest, source))
12554 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12555 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12556 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12557}
12558
e04a16fb
AG
12559/* Build an incomplete binop expression. */
12560
12561static tree
12562build_binop (op, op_location, op1, op2)
12563 enum tree_code op;
12564 int op_location;
12565 tree op1, op2;
12566{
5e942c50 12567 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12568 TREE_SIDE_EFFECTS (binop) = 1;
12569 /* Store the location of the operator, for better error report. The
12570 string of the operator will be rebuild based on the OP value. */
12571 EXPR_WFL_LINECOL (binop) = op_location;
12572 return binop;
12573}
12574
12575/* Build the string of the operator retained by NODE. If NODE is part
12576 of a compound expression, add an '=' at the end of the string. This
12577 function is called when an error needs to be reported on an
12578 operator. The string is returned as a pointer to a static character
12579 buffer. */
12580
12581static char *
12582operator_string (node)
12583 tree node;
12584{
12585#define BUILD_OPERATOR_STRING(S) \
12586 { \
12587 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12588 return buffer; \
12589 }
12590
12591 static char buffer [10];
12592 switch (TREE_CODE (node))
12593 {
12594 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12595 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12596 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12597 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12598 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12599 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12600 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12601 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12602 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12603 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12604 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12605 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12606 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12607 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12608 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12609 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12610 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12611 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12612 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12613 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12614 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12615 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12616 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12617 case PREINCREMENT_EXPR: /* Fall through */
12618 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12619 case PREDECREMENT_EXPR: /* Fall through */
12620 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12621 default:
12622 fatal ("unregistered operator %s - operator_string",
12623 tree_code_name [TREE_CODE (node)]);
12624 }
12625 return NULL;
12626#undef BUILD_OPERATOR_STRING
12627}
12628
5cbdba64
APB
12629/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12630
12631static int
12632java_decl_equiv (var_acc1, var_acc2)
12633 tree var_acc1, var_acc2;
12634{
12635 if (JDECL_P (var_acc1))
12636 return (var_acc1 == var_acc2);
12637
12638 return (TREE_CODE (var_acc1) == COMPONENT_REF
12639 && TREE_CODE (var_acc2) == COMPONENT_REF
12640 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12641 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12642 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12643}
12644
12645/* Return a non zero value if CODE is one of the operators that can be
12646 used in conjunction with the `=' operator in a compound assignment. */
12647
12648static int
12649binop_compound_p (code)
12650 enum tree_code code;
12651{
12652 int i;
12653 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12654 if (binop_lookup [i] == code)
12655 break;
12656
12657 return i < BINOP_COMPOUND_CANDIDATES;
12658}
12659
12660/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12661
12662static tree
12663java_refold (t)
12664 tree t;
12665{
12666 tree c, b, ns, decl;
12667
12668 if (TREE_CODE (t) != MODIFY_EXPR)
12669 return t;
12670
12671 c = TREE_OPERAND (t, 1);
12672 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12673 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12674 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12675 return t;
12676
12677 /* Now the left branch of the binary operator. */
12678 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12679 if (! (b && TREE_CODE (b) == NOP_EXPR
12680 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12681 return t;
12682
12683 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12684 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12685 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12686 return t;
12687
12688 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12689 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12690 /* It's got to be the an equivalent decl */
12691 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12692 {
12693 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12694 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12695 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12696 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12697 /* Change the right part of the BINOP_EXPR */
12698 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12699 }
12700
12701 return t;
12702}
12703
e04a16fb
AG
12704/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12705 errors but we modify NODE so that it contains the type computed
12706 according to the expression, when it's fixed. Otherwise, we write
12707 error_mark_node as the type. It allows us to further the analysis
12708 of remaining nodes and detects more errors in certain cases. */
12709
12710static tree
12711patch_binop (node, wfl_op1, wfl_op2)
12712 tree node;
12713 tree wfl_op1;
12714 tree wfl_op2;
12715{
12716 tree op1 = TREE_OPERAND (node, 0);
12717 tree op2 = TREE_OPERAND (node, 1);
12718 tree op1_type = TREE_TYPE (op1);
12719 tree op2_type = TREE_TYPE (op2);
48a840d9 12720 tree prom_type = NULL_TREE, cn;
e04a16fb 12721 int code = TREE_CODE (node);
b67d701b 12722
e04a16fb
AG
12723 /* If 1, tell the routine that we have to return error_mark_node
12724 after checking for the initialization of the RHS */
12725 int error_found = 0;
12726
e04a16fb
AG
12727 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12728
e04a16fb
AG
12729 switch (code)
12730 {
12731 /* 15.16 Multiplicative operators */
12732 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12733 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12734 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 12735 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 12736 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 12737 {
7e51098e 12738 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 12739 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 12740 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
12741 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12742 TREE_TYPE (node) = error_mark_node;
12743 error_found = 1;
12744 break;
12745 }
12746 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12747 /* Change the division operator if necessary */
12748 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12749 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12750
aa4759c1
AH
12751 if (TREE_CODE (prom_type) == INTEGER_TYPE
12752 && flag_use_divide_subroutine
12753 && ! flag_emit_class_files
12754 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12755 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12756
0b4d333e
APB
12757 /* This one is more complicated. FLOATs are processed by a
12758 function call to soft_fmod. Duplicate the value of the
12759 COMPOUND_ASSIGN_P flag. */
e04a16fb 12760 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12761 {
12762 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12763 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12764 TREE_SIDE_EFFECTS (mod)
12765 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12766 return mod;
12767 }
e04a16fb
AG
12768 break;
12769
12770 /* 15.17 Additive Operators */
12771 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12772
12773 /* Operation is valid if either one argument is a string
12774 constant, a String object or a StringBuffer crafted for the
12775 purpose of the a previous usage of the String concatenation
12776 operator */
12777
12778 if (TREE_CODE (op1) == STRING_CST
12779 || TREE_CODE (op2) == STRING_CST
12780 || JSTRING_TYPE_P (op1_type)
12781 || JSTRING_TYPE_P (op2_type)
12782 || IS_CRAFTED_STRING_BUFFER_P (op1)
12783 || IS_CRAFTED_STRING_BUFFER_P (op2))
12784 return build_string_concatenation (op1, op2);
12785
e04a16fb
AG
12786 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12787 Numeric Types */
7e51098e 12788 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 12789 {
7e51098e 12790 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 12791 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 12792 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
12793 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12794 TREE_TYPE (node) = error_mark_node;
12795 error_found = 1;
12796 break;
12797 }
12798 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12799 break;
12800
12801 /* 15.18 Shift Operators */
12802 case LSHIFT_EXPR:
12803 case RSHIFT_EXPR:
12804 case URSHIFT_EXPR:
12805 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12806 {
12807 if (!JINTEGRAL_TYPE_P (op1_type))
12808 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12809 else
1ebadc60 12810 {
7e51098e 12811 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 12812 parse_error_context (wfl_operator,
781b0558 12813 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12814 operator_string (node),
12815 lang_printable_name (op2_type, 0));
12816 else
781b0558
KG
12817 parse_error_context (wfl_operator,
12818 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12819 operator_string (node),
12820 lang_printable_name (op2_type, 0));
12821 }
e04a16fb
AG
12822 TREE_TYPE (node) = error_mark_node;
12823 error_found = 1;
12824 break;
12825 }
12826
12827 /* Unary numeric promotion (5.6.1) is performed on each operand
12828 separatly */
15fdcfe9
PB
12829 op1 = do_unary_numeric_promotion (op1);
12830 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12831
12832 /* The type of the shift expression is the type of the promoted
12833 type of the left-hand operand */
12834 prom_type = TREE_TYPE (op1);
12835
c2952b01
APB
12836 /* Shift int only up to 0x1f and long up to 0x3f */
12837 if (prom_type == int_type_node)
12838 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12839 build_int_2 (0x1f, 0)));
12840 else
12841 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12842 build_int_2 (0x3f, 0)));
e04a16fb
AG
12843
12844 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12845 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12846 {
0b4d333e 12847 tree to_return;
73333a87
AH
12848 tree utype = unsigned_type (prom_type);
12849 op1 = convert (utype, op1);
e04a16fb 12850 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12851 TREE_OPERAND (node, 0) = op1;
12852 TREE_OPERAND (node, 1) = op2;
12853 TREE_TYPE (node) = utype;
0b4d333e
APB
12854 to_return = convert (prom_type, node);
12855 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12856 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12857 TREE_SIDE_EFFECTS (to_return)
12858 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12859 return to_return;
e04a16fb
AG
12860 }
12861 break;
5e942c50
APB
12862
12863 /* 15.19.1 Type Comparison Operator instaceof */
12864 case INSTANCEOF_EXPR:
12865
12866 TREE_TYPE (node) = boolean_type_node;
12867
12868 if (!(op2_type = resolve_type_during_patch (op2)))
12869 return error_mark_node;
12870
12871 /* The first operand must be a reference type or the null type */
12872 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12873 error_found = 1; /* Error reported further below */
12874
12875 /* The second operand must be a reference type */
12876 if (!JREFERENCE_TYPE_P (op2_type))
12877 {
12878 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12879 parse_error_context
12880 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12881 lang_printable_name (op2_type, 0));
12882 error_found = 1;
12883 }
12884
12885 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12886 {
12887 /* If the first operand is null, the result is always false */
12888 if (op1 == null_pointer_node)
12889 return boolean_false_node;
15fdcfe9
PB
12890 else if (flag_emit_class_files)
12891 {
12892 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12893 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12894 return node;
12895 }
5e942c50
APB
12896 /* Otherwise we have to invoke instance of to figure it out */
12897 else
67db0ce7 12898 return build_instanceof (op1, op2_type);
5e942c50
APB
12899 }
12900 /* There is no way the expression operand can be an instance of
12901 the type operand. This is a compile time error. */
12902 else
12903 {
c2e3db92 12904 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12905 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12906 parse_error_context
12907 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12908 t1, lang_printable_name (op2_type, 0));
12909 free (t1);
12910 error_found = 1;
12911 }
e04a16fb 12912
5e942c50 12913 break;
e04a16fb
AG
12914
12915 /* 15.21 Bitwise and Logical Operators */
12916 case BIT_AND_EXPR:
12917 case BIT_XOR_EXPR:
12918 case BIT_IOR_EXPR:
12919 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12920 /* Binary numeric promotion is performed on both operand and the
12921 expression retain that type */
12922 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12923
12924 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12925 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12926 /* The type of the bitwise operator expression is BOOLEAN */
12927 prom_type = boolean_type_node;
12928 else
12929 {
12930 if (!JINTEGRAL_TYPE_P (op1_type))
12931 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12932 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12933 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12934 TREE_TYPE (node) = error_mark_node;
12935 error_found = 1;
12936 /* Insert a break here if adding thing before the switch's
12937 break for this case */
12938 }
12939 break;
12940
12941 /* 15.22 Conditional-And Operator */
12942 case TRUTH_ANDIF_EXPR:
12943 /* 15.23 Conditional-Or Operator */
12944 case TRUTH_ORIF_EXPR:
12945 /* Operands must be of BOOLEAN type */
12946 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12947 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12948 {
12949 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12950 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12951 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12952 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12953 TREE_TYPE (node) = boolean_type_node;
12954 error_found = 1;
12955 break;
12956 }
12957 /* The type of the conditional operators is BOOLEAN */
12958 prom_type = boolean_type_node;
12959 break;
12960
12961 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12962 case LT_EXPR:
12963 case GT_EXPR:
12964 case LE_EXPR:
12965 case GE_EXPR:
12966 /* The type of each of the operands must be a primitive numeric
12967 type */
12968 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12969 {
12970 if (!JNUMERIC_TYPE_P (op1_type))
12971 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12972 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12973 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12974 TREE_TYPE (node) = boolean_type_node;
12975 error_found = 1;
12976 break;
12977 }
12978 /* Binary numeric promotion is performed on the operands */
12979 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12980 /* The type of the relation expression is always BOOLEAN */
12981 prom_type = boolean_type_node;
12982 break;
12983
12984 /* 15.20 Equality Operator */
12985 case EQ_EXPR:
12986 case NE_EXPR:
48a840d9
APB
12987 /* It's time for us to patch the strings. */
12988 if ((cn = patch_string (op1)))
12989 {
12990 op1 = cn;
12991 op1_type = TREE_TYPE (op1);
12992 }
12993 if ((cn = patch_string (op2)))
12994 {
12995 op2 = cn;
12996 op2_type = TREE_TYPE (op2);
12997 }
12998
e04a16fb
AG
12999 /* 15.20.1 Numerical Equality Operators == and != */
13000 /* Binary numeric promotion is performed on the operands */
5e942c50 13001 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13002 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13003
13004 /* 15.20.2 Boolean Equality Operators == and != */
13005 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13006 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13007 ; /* Nothing to do here */
13008
13009 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13010 /* Types have to be either references or the null type. If
13011 they're references, it must be possible to convert either
13012 type to the other by casting conversion. */
b9f7e36c
APB
13013 else if (op1 == null_pointer_node || op2 == null_pointer_node
13014 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13015 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13016 || valid_ref_assignconv_cast_p (op2_type,
13017 op1_type, 1))))
e04a16fb
AG
13018 ; /* Nothing to do here */
13019
13020 /* Else we have an error figure what can't be converted into
13021 what and report the error */
13022 else
13023 {
13024 char *t1;
c2e3db92 13025 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13026 parse_error_context
781b0558
KG
13027 (wfl_operator,
13028 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13029 operator_string (node), t1,
0a2138e2 13030 lang_printable_name (op2_type, 0));
e04a16fb
AG
13031 free (t1);
13032 TREE_TYPE (node) = boolean_type_node;
13033 error_found = 1;
13034 break;
13035 }
13036 prom_type = boolean_type_node;
13037 break;
13038 }
13039
e04a16fb
AG
13040 if (error_found)
13041 return error_mark_node;
13042
13043 TREE_OPERAND (node, 0) = op1;
13044 TREE_OPERAND (node, 1) = op2;
13045 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13046 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13047
ce6e9147
APB
13048 if (flag_emit_xref)
13049 return node;
13050
d1472141
PB
13051 /* fold does not respect side-effect order as required for Java but not C.
13052 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13053 * bytecode.
13054 */
13055 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13056 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13057 node = fold (node);
13058 return node;
e04a16fb
AG
13059}
13060
b67d701b
PB
13061/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13062 zero value, the value of CSTE comes after the valude of STRING */
13063
13064static tree
13065do_merge_string_cste (cste, string, string_len, after)
13066 tree cste;
49f48c71 13067 const char *string;
b67d701b
PB
13068 int string_len, after;
13069{
49f48c71 13070 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13071 int old_len = TREE_STRING_LENGTH (cste);
13072 int len = old_len + string_len;
520a57c8 13073 char *new = alloca (len+1);
354e99ce 13074
b67d701b
PB
13075 if (after)
13076 {
354e99ce
APB
13077 memcpy (new, string, string_len);
13078 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13079 }
13080 else
13081 {
354e99ce
APB
13082 memcpy (new, old, old_len);
13083 memcpy (&new [old_len], string, string_len);
b67d701b 13084 }
354e99ce 13085 new [len] = '\0';
520a57c8 13086 return build_string (len, new);
b67d701b
PB
13087}
13088
13089/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13090 new STRING_CST on success, NULL_TREE on failure */
13091
13092static tree
13093merge_string_cste (op1, op2, after)
13094 tree op1, op2;
13095 int after;
13096{
13097 /* Handle two string constants right away */
13098 if (TREE_CODE (op2) == STRING_CST)
13099 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13100 TREE_STRING_LENGTH (op2), after);
13101
13102 /* Reasonable integer constant can be treated right away */
13103 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13104 {
49f48c71
KG
13105 static const char *boolean_true = "true";
13106 static const char *boolean_false = "false";
13107 static const char *null_pointer = "null";
b67d701b 13108 char ch[3];
49f48c71 13109 const char *string;
b67d701b
PB
13110
13111 if (op2 == boolean_true_node)
13112 string = boolean_true;
13113 else if (op2 == boolean_false_node)
13114 string = boolean_false;
13115 else if (op2 == null_pointer_node)
13116 string = null_pointer;
13117 else if (TREE_TYPE (op2) == char_type_node)
13118 {
13119 ch[0] = (char )TREE_INT_CST_LOW (op2);
13120 ch[1] = '\0';
13121 string = ch;
13122 }
13123 else
13124 string = print_int_node (op2);
13125
13126 return do_merge_string_cste (op1, string, strlen (string), after);
13127 }
13128 return NULL_TREE;
13129}
13130
13131/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13132 has to be a STRING_CST and the other part must be a STRING_CST or a
13133 INTEGRAL constant. Return a new STRING_CST if the operation
13134 succeed, NULL_TREE otherwise.
13135
13136 If the case we want to optimize for space, we might want to return
13137 NULL_TREE for each invocation of this routine. FIXME */
13138
13139static tree
13140string_constant_concatenation (op1, op2)
13141 tree op1, op2;
13142{
13143 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13144 {
0a2138e2 13145 tree string, rest;
b67d701b
PB
13146 int invert;
13147
13148 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13149 rest = (string == op1 ? op2 : op1);
13150 invert = (string == op1 ? 0 : 1 );
13151
13152 /* Walk REST, only if it looks reasonable */
13153 if (TREE_CODE (rest) != STRING_CST
13154 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13155 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13156 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13157 {
13158 rest = java_complete_tree (rest);
13159 if (rest == error_mark_node)
13160 return error_mark_node;
13161 rest = fold (rest);
13162 }
13163 return merge_string_cste (string, rest, invert);
13164 }
13165 return NULL_TREE;
13166}
13167
13168/* Implement the `+' operator. Does static optimization if possible,
13169 otherwise create (if necessary) and append elements to a
13170 StringBuffer. The StringBuffer will be carried around until it is
13171 used for a function call or an assignment. Then toString() will be
13172 called on it to turn it into a String object. */
13173
13174static tree
13175build_string_concatenation (op1, op2)
13176 tree op1, op2;
13177{
13178 tree result;
dc0b3eff 13179 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13180
13181 if (flag_emit_xref)
13182 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13183
13184 /* Try to do some static optimization */
13185 if ((result = string_constant_concatenation (op1, op2)))
13186 return result;
13187
c0d87ff6
PB
13188 /* Discard empty strings on either side of the expression */
13189 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13190 {
13191 op1 = op2;
13192 op2 = NULL_TREE;
13193 }
c0d87ff6 13194 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13195 op2 = NULL_TREE;
b67d701b 13196
acd663ee 13197 /* If operands are string constant, turn then into object references */
b67d701b
PB
13198 if (TREE_CODE (op1) == STRING_CST)
13199 op1 = patch_string_cst (op1);
acd663ee 13200 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13201 op2 = patch_string_cst (op2);
13202
acd663ee
APB
13203 /* If either one of the constant is null and the other non null
13204 operand is a String object, return it. */
13205 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13206 return op1;
13207
b67d701b
PB
13208 /* If OP1 isn't already a StringBuffer, create and
13209 initialize a new one */
13210 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13211 {
13212 /* Two solutions here:
c52b5771
AG
13213 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13214 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13215 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13216 op1 = BUILD_STRING_BUFFER (op1);
13217 else
13218 {
13219 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13220 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13221 }
13222 }
13223
acd663ee
APB
13224 if (op2)
13225 {
13226 /* OP1 is no longer the last node holding a crafted StringBuffer */
13227 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13228 /* Create a node for `{new...,xxx}.append (op2)' */
13229 if (op2)
13230 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13231 }
13232
b67d701b
PB
13233 /* Mark the last node holding a crafted StringBuffer */
13234 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13235
13236 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13237 return op1;
13238}
13239
13240/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13241 StringBuffer. If no string were found to be patched, return
13242 NULL. */
13243
13244static tree
13245patch_string (node)
13246 tree node;
13247{
1179ebc2
APB
13248 if (node == error_mark_node)
13249 return error_mark_node;
b67d701b
PB
13250 if (TREE_CODE (node) == STRING_CST)
13251 return patch_string_cst (node);
13252 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13253 {
c877974e 13254 int saved = ctxp->explicit_constructor_p;
b67d701b 13255 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13256 tree ret;
13257 /* Temporary disable forbid the use of `this'. */
13258 ctxp->explicit_constructor_p = 0;
13259 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13260 /* String concatenation arguments must be evaluated in order too. */
13261 ret = force_evaluation_order (ret);
c877974e
APB
13262 /* Restore it at its previous value */
13263 ctxp->explicit_constructor_p = saved;
13264 return ret;
b67d701b
PB
13265 }
13266 return NULL_TREE;
13267}
13268
13269/* Build the internal representation of a string constant. */
13270
13271static tree
13272patch_string_cst (node)
13273 tree node;
13274{
13275 int location;
15fdcfe9
PB
13276 if (! flag_emit_class_files)
13277 {
15fdcfe9
PB
13278 node = get_identifier (TREE_STRING_POINTER (node));
13279 location = alloc_name_constant (CONSTANT_String, node);
13280 node = build_ref_from_constant_pool (location);
13281 }
cd9643f7 13282 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13283 TREE_CONSTANT (node) = 1;
13284 return node;
13285}
13286
13287/* Build an incomplete unary operator expression. */
e04a16fb
AG
13288
13289static tree
13290build_unaryop (op_token, op_location, op1)
13291 int op_token, op_location;
13292 tree op1;
13293{
13294 enum tree_code op;
13295 tree unaryop;
13296 switch (op_token)
13297 {
b67d701b 13298 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13299 case MINUS_TK: op = NEGATE_EXPR; break;
13300 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13301 case NOT_TK: op = BIT_NOT_EXPR; break;
13302 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13303 op_token);
13304 }
13305
13306 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13307 TREE_SIDE_EFFECTS (unaryop) = 1;
13308 /* Store the location of the operator, for better error report. The
13309 string of the operator will be rebuild based on the OP value. */
13310 EXPR_WFL_LINECOL (unaryop) = op_location;
13311 return unaryop;
13312}
13313
13314/* Special case for the ++/-- operators, since they require an extra
13315 argument to build, which is set to NULL and patched
13316 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13317
13318static tree
13319build_incdec (op_token, op_location, op1, is_post_p)
13320 int op_token, op_location;
13321 tree op1;
13322 int is_post_p;
13323{
13324 static enum tree_code lookup [2][2] =
13325 {
13326 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13327 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13328 };
13329 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13330 NULL_TREE, op1, NULL_TREE);
13331 TREE_SIDE_EFFECTS (node) = 1;
13332 /* Store the location of the operator, for better error report. The
13333 string of the operator will be rebuild based on the OP value. */
13334 EXPR_WFL_LINECOL (node) = op_location;
13335 return node;
13336}
13337
13338/* Build an incomplete cast operator, based on the use of the
13339 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13340 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13341 though its type is already set. */
13342
13343static tree
13344build_cast (location, type, exp)
13345 int location;
13346 tree type, exp;
13347{
13348 tree node = build1 (CONVERT_EXPR, type, exp);
13349 EXPR_WFL_LINECOL (node) = location;
13350 return node;
13351}
13352
c2952b01
APB
13353/* Build an incomplete class reference operator. */
13354static tree
13355build_incomplete_class_ref (location, class_name)
13356 int location;
13357 tree class_name;
13358{
13359 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13360 EXPR_WFL_LINECOL (node) = location;
13361 return node;
13362}
13363
13364/* Complete an incomplete class reference operator. */
13365static tree
13366patch_incomplete_class_ref (node)
13367 tree node;
13368{
13369 tree type = TREE_OPERAND (node, 0);
13370 tree ref_type;
13371
13372 if (!(ref_type = resolve_type_during_patch (type)))
13373 return error_mark_node;
13374
165f37bc 13375 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13376 {
13377 /* A class referenced by `foo.class' is initialized. */
13378 return build_class_init (ref_type, build_class_ref (ref_type));
13379 }
165f37bc
APB
13380
13381 /* If we're emitting class files and we have to deal with non
13382 primitive types, we invoke (and consider generating) the
13383 synthetic static method `class$'. */
13384 if (!TYPE_DOT_CLASS (current_class))
13385 build_dot_class_method (current_class);
f0f3a777 13386 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13387 return java_complete_tree (ref_type);
c2952b01
APB
13388}
13389
e04a16fb
AG
13390/* 15.14 Unary operators. We return error_mark_node in case of error,
13391 but preserve the type of NODE if the type is fixed. */
13392
13393static tree
13394patch_unaryop (node, wfl_op)
13395 tree node;
13396 tree wfl_op;
13397{
13398 tree op = TREE_OPERAND (node, 0);
13399 tree op_type = TREE_TYPE (op);
ab3a6dd6 13400 tree prom_type = NULL_TREE, value, decl;
c2952b01 13401 int outer_field_flag = 0;
e04a16fb
AG
13402 int code = TREE_CODE (node);
13403 int error_found = 0;
13404
13405 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13406
13407 switch (code)
13408 {
13409 /* 15.13.2 Postfix Increment Operator ++ */
13410 case POSTINCREMENT_EXPR:
13411 /* 15.13.3 Postfix Increment Operator -- */
13412 case POSTDECREMENT_EXPR:
13413 /* 15.14.1 Prefix Increment Operator ++ */
13414 case PREINCREMENT_EXPR:
13415 /* 15.14.2 Prefix Decrement Operator -- */
13416 case PREDECREMENT_EXPR:
5cbdba64 13417 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13418 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13419 /* We might be trying to change an outer field accessed using
13420 access method. */
13421 if (outer_field_flag)
13422 {
13423 /* Retrieve the decl of the field we're trying to access. We
13424 do that by first retrieving the function we would call to
13425 access the field. It has been already verified that this
13426 field isn't final */
13427 if (flag_emit_class_files)
13428 decl = TREE_OPERAND (op, 0);
13429 else
13430 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13431 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13432 }
b3edebcf 13433 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13434 else if (!JDECL_P (decl)
b3edebcf
APB
13435 && TREE_CODE (decl) != COMPONENT_REF
13436 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13437 && TREE_CODE (decl) != INDIRECT_REF
13438 && !(TREE_CODE (decl) == COMPOUND_EXPR
13439 && TREE_OPERAND (decl, 1)
13440 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13441 {
5e942c50
APB
13442 tree lvalue;
13443 /* Before screaming, check that we're not in fact trying to
13444 increment a optimized static final access, in which case
13445 we issue an different error message. */
13446 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13447 && resolve_expression_name (wfl_op, &lvalue)
13448 && check_final_assignment (lvalue, wfl_op)))
13449 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13450 operator_string (node));
e04a16fb
AG
13451 TREE_TYPE (node) = error_mark_node;
13452 error_found = 1;
13453 }
c2952b01
APB
13454
13455 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13456 error_found = 1;
13457
e04a16fb
AG
13458 /* From now on, we know that op if a variable and that it has a
13459 valid wfl. We use wfl_op to locate errors related to the
13460 ++/-- operand. */
13461 else if (!JNUMERIC_TYPE_P (op_type))
13462 {
13463 parse_error_context
13464 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13465 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13466 TREE_TYPE (node) = error_mark_node;
13467 error_found = 1;
13468 }
13469 else
13470 {
4a5f66c3 13471 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13472 both operands, if really necessary */
13473 if (JINTEGRAL_TYPE_P (op_type))
13474 {
13475 value = build_int_2 (1, 0);
13476 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13477 }
13478 else
13479 {
13480 value = build_int_2 (1, 0);
13481 TREE_TYPE (node) =
13482 binary_numeric_promotion (op_type,
13483 TREE_TYPE (value), &op, &value);
13484 }
c2952b01
APB
13485
13486 /* We remember we might be accessing an outer field */
13487 if (outer_field_flag)
13488 {
13489 /* We re-generate an access to the field */
13490 value = build (PLUS_EXPR, TREE_TYPE (op),
13491 build_outer_field_access (wfl_op, decl), value);
13492
13493 /* And we patch the original access$() into a write
13494 with plus_op as a rhs */
13495 return outer_field_access_fix (node, op, value);
13496 }
13497
5cbdba64 13498 /* And write back into the node. */
4a5f66c3 13499 TREE_OPERAND (node, 0) = op;
e04a16fb 13500 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13501 /* Convert the overall back into its original type, if
13502 necessary, and return */
13503 if (JINTEGRAL_TYPE_P (op_type))
13504 return fold (node);
13505 else
13506 return fold (convert (op_type, node));
e04a16fb
AG
13507 }
13508 break;
13509
13510 /* 15.14.3 Unary Plus Operator + */
b67d701b 13511 case UNARY_PLUS_EXPR:
e04a16fb
AG
13512 /* 15.14.4 Unary Minus Operator - */
13513 case NEGATE_EXPR:
13514 if (!JNUMERIC_TYPE_P (op_type))
13515 {
13516 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13517 TREE_TYPE (node) = error_mark_node;
13518 error_found = 1;
13519 }
13520 /* Unary numeric promotion is performed on operand */
13521 else
13522 {
15fdcfe9
PB
13523 op = do_unary_numeric_promotion (op);
13524 prom_type = TREE_TYPE (op);
b67d701b 13525 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13526 return fold (op);
e04a16fb
AG
13527 }
13528 break;
13529
13530 /* 15.14.5 Bitwise Complement Operator ~ */
13531 case BIT_NOT_EXPR:
13532 if (!JINTEGRAL_TYPE_P (op_type))
13533 {
13534 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13535 TREE_TYPE (node) = error_mark_node;
13536 error_found = 1;
13537 }
13538 else
13539 {
15fdcfe9
PB
13540 op = do_unary_numeric_promotion (op);
13541 prom_type = TREE_TYPE (op);
e04a16fb
AG
13542 }
13543 break;
13544
13545 /* 15.14.6 Logical Complement Operator ! */
13546 case TRUTH_NOT_EXPR:
13547 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13548 {
13549 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13550 /* But the type is known. We will report an error if further
13551 attempt of a assignment is made with this rhs */
e04a16fb
AG
13552 TREE_TYPE (node) = boolean_type_node;
13553 error_found = 1;
13554 }
13555 else
13556 prom_type = boolean_type_node;
13557 break;
13558
13559 /* 15.15 Cast Expression */
13560 case CONVERT_EXPR:
0a2138e2 13561 value = patch_cast (node, wfl_operator);
e04a16fb 13562 if (value == error_mark_node)
c877974e
APB
13563 {
13564 /* If this cast is part of an assignment, we tell the code
13565 that deals with it not to complain about a mismatch,
13566 because things have been cast, anyways */
13567 TREE_TYPE (node) = error_mark_node;
13568 error_found = 1;
13569 }
13570 else
dc0b3eff
PB
13571 {
13572 value = fold (value);
13573 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13574 return value;
13575 }
e04a16fb
AG
13576 break;
13577 }
13578
e04a16fb
AG
13579 if (error_found)
13580 return error_mark_node;
4a5f66c3
APB
13581
13582 /* There are cases where node has been replaced by something else
13583 and we don't end up returning here: UNARY_PLUS_EXPR,
13584 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13585 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13586 TREE_TYPE (node) = prom_type;
dc0b3eff 13587 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13588 return fold (node);
13589}
13590
13591/* Generic type resolution that sometimes takes place during node
13592 patching. Returned the resolved type or generate an error
13593 message. Return the resolved type or NULL_TREE. */
13594
13595static tree
13596resolve_type_during_patch (type)
13597 tree type;
13598{
13599 if (unresolved_type_p (type, NULL))
13600 {
4142b247 13601 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13602 if (!type_decl)
13603 {
13604 parse_error_context (type,
13605 "Class `%s' not found in type declaration",
13606 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13607 return NULL_TREE;
13608 }
13609 else
5e942c50
APB
13610 {
13611 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13612 return TREE_TYPE (type_decl);
13613 }
e04a16fb
AG
13614 }
13615 return type;
13616}
13617/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13618 found. Otherwise NODE or something meant to replace it is returned. */
13619
13620static tree
19e223db 13621patch_cast (node, wfl_op)
e04a16fb 13622 tree node;
19e223db 13623 tree wfl_op;
e04a16fb
AG
13624{
13625 tree op = TREE_OPERAND (node, 0);
13626 tree op_type = TREE_TYPE (op);
13627 tree cast_type = TREE_TYPE (node);
13628 char *t1;
13629
13630 /* First resolve OP_TYPE if unresolved */
13631 if (!(cast_type = resolve_type_during_patch (cast_type)))
13632 return error_mark_node;
13633
13634 /* Check on cast that are proven correct at compile time */
13635 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13636 {
e04a16fb
AG
13637 /* Same type */
13638 if (cast_type == op_type)
13639 return node;
13640
0b4d333e
APB
13641 /* float and double type are converted to the original type main
13642 variant and then to the target type. */
13643 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13644 op = convert (integer_type_node, op);
13645
e04a16fb
AG
13646 /* Try widening/narowwing convertion. Potentially, things need
13647 to be worked out in gcc so we implement the extreme cases
13648 correctly. fold_convert() needs to be fixed. */
13649 return convert (cast_type, op);
13650 }
13651
0b4d333e
APB
13652 /* It's also valid to cast a boolean into a boolean */
13653 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13654 return node;
13655
5e942c50
APB
13656 /* null can be casted to references */
13657 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13658 return build_null_of_type (cast_type);
13659
e04a16fb
AG
13660 /* The remaining legal casts involve conversion between reference
13661 types. Check for their compile time correctness. */
13662 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13663 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13664 {
13665 TREE_TYPE (node) = promote_type (cast_type);
13666 /* Now, the case can be determined correct at compile time if
13667 OP_TYPE can be converted into CAST_TYPE by assignment
13668 conversion (5.2) */
13669
13670 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13671 {
13672 TREE_SET_CODE (node, NOP_EXPR);
13673 return node;
13674 }
13675
13676 if (flag_emit_class_files)
13677 {
13678 TREE_SET_CODE (node, CONVERT_EXPR);
13679 return node;
13680 }
e04a16fb
AG
13681
13682 /* The cast requires a run-time check */
13683 return build (CALL_EXPR, promote_type (cast_type),
13684 build_address_of (soft_checkcast_node),
13685 tree_cons (NULL_TREE, build_class_ref (cast_type),
13686 build_tree_list (NULL_TREE, op)),
13687 NULL_TREE);
13688 }
13689
13690 /* Any other casts are proven incorrect at compile time */
c2e3db92 13691 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13692 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13693 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13694 free (t1);
13695 return error_mark_node;
13696}
13697
5e942c50
APB
13698/* Build a null constant and give it the type TYPE. */
13699
13700static tree
13701build_null_of_type (type)
13702 tree type;
13703{
13704 tree node = build_int_2 (0, 0);
13705 TREE_TYPE (node) = promote_type (type);
13706 return node;
13707}
13708
e04a16fb
AG
13709/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13710 a list of indices. */
13711static tree
13712build_array_ref (location, array, index)
13713 int location;
13714 tree array, index;
13715{
13716 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13717 EXPR_WFL_LINECOL (node) = location;
13718 return node;
13719}
13720
13721/* 15.12 Array Access Expression */
13722
13723static tree
c877974e
APB
13724patch_array_ref (node)
13725 tree node;
e04a16fb
AG
13726{
13727 tree array = TREE_OPERAND (node, 0);
13728 tree array_type = TREE_TYPE (array);
13729 tree index = TREE_OPERAND (node, 1);
13730 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13731 int error_found = 0;
13732
13733 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13734
e04a16fb
AG
13735 if (TREE_CODE (array_type) == POINTER_TYPE)
13736 array_type = TREE_TYPE (array_type);
13737
13738 /* The array reference must be an array */
13739 if (!TYPE_ARRAY_P (array_type))
13740 {
13741 parse_error_context
781b0558
KG
13742 (wfl_operator,
13743 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13744 lang_printable_name (array_type, 0));
e04a16fb
AG
13745 TREE_TYPE (node) = error_mark_node;
13746 error_found = 1;
13747 }
13748
c2952b01 13749 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13750 type must be int */
15fdcfe9
PB
13751 index = do_unary_numeric_promotion (index);
13752 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13753 {
1ebadc60 13754 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13755 parse_error_context (wfl_operator,
13756 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13757 lang_printable_name (index_type, 0));
13758 else
781b0558
KG
13759 parse_error_context (wfl_operator,
13760 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13761 lang_printable_name (index_type, 0));
e04a16fb
AG
13762 TREE_TYPE (node) = error_mark_node;
13763 error_found = 1;
13764 }
13765
e04a16fb
AG
13766 if (error_found)
13767 return error_mark_node;
e04a16fb 13768
5e942c50 13769 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13770
7f1d4866 13771 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13772 {
15fdcfe9
PB
13773 TREE_OPERAND (node, 0) = array;
13774 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13775 }
13776 else
939d7216
PB
13777 {
13778 /* The save_expr is for correct evaluation order. It would be cleaner
13779 to use force_evaluation_order (see comment there), but that is
13780 difficult when we also have to deal with bounds checking. */
13781 if (TREE_SIDE_EFFECTS (index))
13782 array = save_expr (array);
13783 node = build_java_arrayaccess (array, array_type, index);
13784 if (TREE_SIDE_EFFECTS (index))
13785 node = build (COMPOUND_EXPR, array_type, array, node);
13786 }
e04a16fb
AG
13787 TREE_TYPE (node) = array_type;
13788 return node;
13789}
13790
13791/* 15.9 Array Creation Expressions */
13792
13793static tree
13794build_newarray_node (type, dims, extra_dims)
13795 tree type;
13796 tree dims;
13797 int extra_dims;
13798{
13799 tree node =
b67d701b 13800 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13801 build_int_2 (extra_dims, 0));
e04a16fb
AG
13802 return node;
13803}
13804
13805static tree
13806patch_newarray (node)
13807 tree node;
13808{
13809 tree type = TREE_OPERAND (node, 0);
13810 tree dims = TREE_OPERAND (node, 1);
13811 tree cdim, array_type;
13812 int error_found = 0;
13813 int ndims = 0;
13814 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13815
13816 /* Dimension types are verified. It's better for the types to be
13817 verified in order. */
13818 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13819 {
13820 int dim_error = 0;
13821 tree dim = TREE_VALUE (cdim);
13822
13823 /* Dim might have been saved during its evaluation */
dba41d30 13824 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
13825
13826 /* The type of each specified dimension must be an integral type. */
13827 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13828 dim_error = 1;
13829
13830 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13831 promoted type must be int. */
13832 else
13833 {
15fdcfe9 13834 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13835 if (TREE_TYPE (dim) != int_type_node)
13836 dim_error = 1;
13837 }
13838
13839 /* Report errors on types here */
13840 if (dim_error)
13841 {
13842 parse_error_context
13843 (TREE_PURPOSE (cdim),
781b0558 13844 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13845 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13846 "Explicit cast needed to" : "Can't"),
0a2138e2 13847 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13848 error_found = 1;
13849 }
13850
e04a16fb
AG
13851 TREE_PURPOSE (cdim) = NULL_TREE;
13852 }
13853
13854 /* Resolve array base type if unresolved */
13855 if (!(type = resolve_type_during_patch (type)))
13856 error_found = 1;
13857
13858 if (error_found)
13859 {
13860 /* We don't want further evaluation of this bogus array creation
13861 operation */
13862 TREE_TYPE (node) = error_mark_node;
13863 return error_mark_node;
13864 }
13865
15fdcfe9
PB
13866 /* Set array_type to the actual (promoted) array type of the result. */
13867 if (TREE_CODE (type) == RECORD_TYPE)
13868 type = build_pointer_type (type);
13869 while (--xdims >= 0)
13870 {
13871 type = promote_type (build_java_array_type (type, -1));
13872 }
13873 dims = nreverse (dims);
13874 array_type = type;
13875 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13876 {
13877 type = array_type;
05bccae2
RK
13878 array_type
13879 = build_java_array_type (type,
13880 TREE_CODE (cdim) == INTEGER_CST
13881 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13882 : -1);
15fdcfe9
PB
13883 array_type = promote_type (array_type);
13884 }
13885 dims = nreverse (dims);
13886
e04a16fb
AG
13887 /* The node is transformed into a function call. Things are done
13888 differently according to the number of dimensions. If the number
13889 of dimension is equal to 1, then the nature of the base type
13890 (primitive or not) matters. */
15fdcfe9 13891 if (ndims == 1)
fdec99c6 13892 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13893
e04a16fb
AG
13894 /* Can't reuse what's already written in expr.c because it uses the
13895 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13896 return build (CALL_EXPR, array_type,
e04a16fb 13897 build_address_of (soft_multianewarray_node),
15fdcfe9 13898 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13899 tree_cons (NULL_TREE,
15fdcfe9 13900 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13901 NULL_TREE);
13902}
13903
f8976021
APB
13904/* 10.6 Array initializer. */
13905
13906/* Build a wfl for array element that don't have one, so we can
13907 pin-point errors. */
13908
13909static tree
13910maybe_build_array_element_wfl (node)
13911 tree node;
13912{
13913 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13914 return build_expr_wfl (NULL_TREE, ctxp->filename,
13915 ctxp->elc.line, ctxp->elc.prev_col);
13916 else
13917 return NULL_TREE;
13918}
13919
13920/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13921 identification of initialized arrays easier to detect during walk
13922 and expansion. */
13923
13924static tree
13925build_new_array_init (location, values)
13926 int location;
13927 tree values;
13928{
13929 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13930 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13931 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13932 return to_return;
13933}
13934
13935/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13936 occurred. Otherwise return NODE after having set its type
13937 appropriately. */
13938
13939static tree
13940patch_new_array_init (type, node)
13941 tree type, node;
f8976021
APB
13942{
13943 int error_seen = 0;
fdec99c6 13944 tree current, element_type;
f8976021 13945 HOST_WIDE_INT length;
fdec99c6
PB
13946 int all_constant = 1;
13947 tree init = TREE_OPERAND (node, 0);
f8976021 13948
fdec99c6
PB
13949 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13950 {
13951 parse_error_context (node,
13952 "Invalid array initializer for non-array type `%s'",
13953 lang_printable_name (type, 1));
13954 return error_mark_node;
13955 }
13956 type = TREE_TYPE (type);
13957 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13958
fdec99c6
PB
13959 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13960
13961 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13962 current; length++, current = TREE_CHAIN (current))
f8976021 13963 {
fdec99c6
PB
13964 tree elt = TREE_VALUE (current);
13965 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13966 {
fdec99c6 13967 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13968 elt = TREE_VALUE (current);
13969 /* When compiling to native code, STRING_CST is converted to
13970 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13971 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13972 all_constant = 0;
f8976021 13973 }
fdec99c6
PB
13974 else
13975 {
13976 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13977 TREE_PURPOSE (current) = NULL_TREE;
13978 all_constant = 0;
13979 }
9a7ab4b3
APB
13980 if (elt && TREE_CODE (elt) == TREE_LIST
13981 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13982 error_seen = 1;
f8976021
APB
13983 }
13984
13985 if (error_seen)
13986 return error_mark_node;
13987
13988 /* Create a new type. We can't reuse the one we have here by
13989 patching its dimension because it originally is of dimension -1
13990 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13991 type = build_java_array_type (element_type, length);
13992 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13993 TREE_TYPE (node) = promote_type (type);
13994 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13995 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13996 return node;
13997}
13998
13999/* Verify that one entry of the initializer element list can be
14000 assigned to the array base type. Report 1 if an error occurred, 0
14001 otherwise. */
14002
14003static int
14004array_constructor_check_entry (type, entry)
14005 tree type, entry;
14006{
14007 char *array_type_string = NULL; /* For error reports */
14008 tree value, type_value, new_value, wfl_value, patched;
14009 int error_seen = 0;
14010
14011 new_value = NULL_TREE;
14012 wfl_value = TREE_VALUE (entry);
14013
f8976021 14014 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14015 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14016 if ((patched = patch_string (value)))
14017 value = patched;
1179ebc2
APB
14018 if (value == error_mark_node)
14019 return 1;
f8976021 14020
f8976021
APB
14021 type_value = TREE_TYPE (value);
14022
1179ebc2 14023 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14024 constant overflow during narrowing. */
14025 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14026 new_value = try_builtin_assignconv (wfl_operator, type, value);
14027 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14028 type_value = promote_type (type);
100f7cd8 14029
f8976021
APB
14030 /* Check and report errors */
14031 if (!new_value)
14032 {
49f48c71 14033 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14034 "Can't" : "Explicit cast needed to");
14035 if (!array_type_string)
c2e3db92 14036 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14037 parse_error_context
14038 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14039 msg, lang_printable_name (type_value, 1), array_type_string);
14040 error_seen = 1;
14041 }
14042
14043 if (new_value)
14044 {
b8c5b1c6 14045 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14046 TREE_VALUE (entry) = new_value;
14047 }
14048
14049 if (array_type_string)
14050 free (array_type_string);
14051
14052 TREE_PURPOSE (entry) = NULL_TREE;
14053 return error_seen;
14054}
14055
e04a16fb
AG
14056static tree
14057build_this (location)
14058 int location;
14059{
9ee9b555 14060 tree node = build_wfl_node (this_identifier_node);
b67d701b 14061 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14062 EXPR_WFL_LINECOL (node) = location;
14063 return node;
14064}
14065
14066/* 14.15 The return statement. It builds a modify expression that
14067 assigns the returned value to the RESULT_DECL that hold the value
14068 to be returned. */
14069
14070static tree
14071build_return (location, op)
14072 int location;
14073 tree op;
14074{
14075 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14076 EXPR_WFL_LINECOL (node) = location;
b67d701b 14077 node = build_debugable_stmt (location, node);
e04a16fb
AG
14078 return node;
14079}
14080
14081static tree
14082patch_return (node)
14083 tree node;
14084{
14085 tree return_exp = TREE_OPERAND (node, 0);
14086 tree meth = current_function_decl;
14087 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14088 int error_found = 0;
14089
14090 TREE_TYPE (node) = error_mark_node;
14091 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14092
14093 /* It's invalid to have a return value within a function that is
14094 declared with the keyword void or that is a constructor */
14095 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14096 error_found = 1;
14097
f099f336 14098 /* It's invalid to use a return statement in a static block */
c2952b01 14099 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14100 error_found = 1;
14101
e04a16fb
AG
14102 /* It's invalid to have a no return value within a function that
14103 isn't declared with the keyword `void' */
14104 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14105 error_found = 2;
c2952b01
APB
14106
14107 if (in_instance_initializer)
14108 error_found = 1;
e04a16fb
AG
14109
14110 if (error_found)
14111 {
c2952b01 14112 if (in_instance_initializer)
f099f336 14113 parse_error_context (wfl_operator,
c2952b01
APB
14114 "`return' inside instance initializer");
14115
14116 else if (DECL_CLINIT_P (current_function_decl))
14117 parse_error_context (wfl_operator,
14118 "`return' inside static initializer");
f099f336
APB
14119
14120 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14121 {
c2e3db92 14122 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14123 parse_error_context (wfl_operator,
14124 "`return' with%s value from `%s %s'",
14125 (error_found == 1 ? "" : "out"),
14126 t, lang_printable_name (meth, 0));
14127 free (t);
14128 }
14129 else
14130 parse_error_context (wfl_operator,
14131 "`return' with value from constructor `%s'",
14132 lang_printable_name (meth, 0));
e04a16fb
AG
14133 return error_mark_node;
14134 }
14135
5e942c50
APB
14136 /* If we have a return_exp, build a modify expression and expand
14137 it. Note: at that point, the assignment is declared valid, but we
14138 may want to carry some more hacks */
e04a16fb
AG
14139 if (return_exp)
14140 {
5e942c50
APB
14141 tree exp = java_complete_tree (return_exp);
14142 tree modify, patched;
14143
14144 /* If the function returned value and EXP are booleans, EXP has
14145 to be converted into the type of DECL_RESULT, which is integer
14146 (see complete_start_java_method) */
14147 if (TREE_TYPE (exp) == boolean_type_node &&
14148 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14149 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14150
14151 /* `null' can be assigned to a function returning a reference */
14152 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14153 exp == null_pointer_node)
14154 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14155
14156 if ((patched = patch_string (exp)))
14157 exp = patched;
14158
14159 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14160 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14161 modify = java_complete_tree (modify);
5e942c50 14162
e04a16fb
AG
14163 if (modify != error_mark_node)
14164 {
14165 TREE_SIDE_EFFECTS (modify) = 1;
14166 TREE_OPERAND (node, 0) = modify;
14167 }
14168 else
14169 return error_mark_node;
14170 }
14171 TREE_TYPE (node) = void_type_node;
14172 TREE_SIDE_EFFECTS (node) = 1;
14173 return node;
14174}
14175
14176/* 14.8 The if Statement */
14177
14178static tree
14179build_if_else_statement (location, expression, if_body, else_body)
14180 int location;
14181 tree expression, if_body, else_body;
14182{
14183 tree node;
e04a16fb 14184 if (!else_body)
9bbc7d9f 14185 else_body = empty_stmt_node;
e04a16fb
AG
14186 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14187 EXPR_WFL_LINECOL (node) = location;
b67d701b 14188 node = build_debugable_stmt (location, node);
e04a16fb
AG
14189 return node;
14190}
14191
14192static tree
14193patch_if_else_statement (node)
14194 tree node;
14195{
14196 tree expression = TREE_OPERAND (node, 0);
14197
14198 TREE_TYPE (node) = error_mark_node;
14199 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14200
14201 /* The type of expression must be boolean */
b67d701b
PB
14202 if (TREE_TYPE (expression) != boolean_type_node
14203 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14204 {
14205 parse_error_context
14206 (wfl_operator,
14207 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14208 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14209 return error_mark_node;
14210 }
14211
14212 TREE_TYPE (node) = void_type_node;
14213 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14214 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14215 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14216 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14217 return node;
14218}
14219
14220/* 14.6 Labeled Statements */
14221
14222/* Action taken when a lableled statement is parsed. a new
14223 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14224 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14225
14226static tree
0a2138e2 14227build_labeled_block (location, label)
e04a16fb 14228 int location;
0a2138e2 14229 tree label;
e04a16fb 14230{
b635eb2f 14231 tree label_name ;
e04a16fb 14232 tree label_decl, node;
b635eb2f
PB
14233 if (label == NULL_TREE || label == continue_identifier_node)
14234 label_name = label;
14235 else
e04a16fb 14236 {
b635eb2f
PB
14237 label_name = merge_qualified_name (label_id, label);
14238 /* Issue an error if we try to reuse a label that was previously
14239 declared */
14240 if (IDENTIFIER_LOCAL_VALUE (label_name))
14241 {
14242 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14243 parse_error_context (wfl_operator,
14244 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14245 IDENTIFIER_POINTER (label));
14246 EXPR_WFL_LINECOL (wfl_operator) =
14247 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14248 parse_error_context (wfl_operator,
14249 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14250 IDENTIFIER_POINTER (label));
14251 java_error_count--;
14252 }
e04a16fb
AG
14253 }
14254
14255 label_decl = create_label_decl (label_name);
14256 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14257 EXPR_WFL_LINECOL (node) = location;
14258 TREE_SIDE_EFFECTS (node) = 1;
14259 return node;
14260}
14261
b67d701b 14262/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14263
14264static tree
b635eb2f 14265finish_labeled_statement (lbe, statement)
e04a16fb
AG
14266 tree lbe; /* Labeled block expr */
14267 tree statement;
14268{
14269 /* In anyways, tie the loop to its statement */
14270 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14271 pop_labeled_block ();
14272 POP_LABELED_BLOCK ();
e04a16fb
AG
14273 return lbe;
14274}
14275
14276/* 14.10, 14.11, 14.12 Loop Statements */
14277
14278/* Create an empty LOOP_EXPR and make it the last in the nested loop
14279 list. */
14280
14281static tree
14282build_new_loop (loop_body)
14283 tree loop_body;
14284{
14285 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14286 TREE_SIDE_EFFECTS (loop) = 1;
14287 PUSH_LOOP (loop);
14288 return loop;
14289}
14290
14291/* Create a loop body according to the following structure:
14292 COMPOUND_EXPR
14293 COMPOUND_EXPR (loop main body)
14294 EXIT_EXPR (this order is for while/for loops.
14295 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14296 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14297 BODY end of this labeled block)
14298 INCREMENT (if any)
14299
14300 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14301 after the body, like in the do-while loop.
14302
14303 To obtain a loop, the loop body structure described above is
14304 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14305
14306 LABELED_BLOCK_EXPR
14307 LABEL_DECL (use this label to exit the loop)
14308 LOOP_EXPR
14309 <structure described above> */
e04a16fb
AG
14310
14311static tree
14312build_loop_body (location, condition, reversed)
14313 int location;
14314 tree condition;
14315 int reversed;
14316{
0a2138e2 14317 tree first, second, body;
e04a16fb
AG
14318
14319 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14320 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14321 condition = build_debugable_stmt (location, condition);
14322 TREE_SIDE_EFFECTS (condition) = 1;
14323
b635eb2f 14324 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14325 first = (reversed ? body : condition);
14326 second = (reversed ? condition : body);
14327 return
14328 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14329 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14330}
14331
14332/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14333 their order) on the current loop. Unlink the current loop from the
14334 loop list. */
14335
14336static tree
b635eb2f 14337finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14338 int location;
14339 tree condition, body;
14340 int reversed;
14341{
14342 tree to_return = ctxp->current_loop;
14343 tree loop_body = LOOP_EXPR_BODY (to_return);
14344 if (condition)
14345 {
14346 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14347 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14348 The real EXIT_EXPR is one operand further. */
14349 EXPR_WFL_LINECOL (cnode) = location;
14350 /* This one is for accurate error reports */
14351 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14352 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14353 }
14354 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14355 POP_LOOP ();
14356 return to_return;
14357}
14358
b635eb2f 14359/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14360 loops feature the condition part */
14361
14362static tree
b635eb2f 14363finish_for_loop (location, condition, update, body)
e04a16fb
AG
14364 int location;
14365 tree condition, update, body;
14366{
14367 /* Put the condition and the loop body in place */
b635eb2f 14368 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14369 /* LOOP is the current loop which has been now popped of the loop
14370 stack. Install the update block */
14371 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14372 return loop;
14373}
14374
5cbdba64
APB
14375/* Try to find the loop a block might be related to. This comprises
14376 the case where the LOOP_EXPR is found as the second operand of a
14377 COMPOUND_EXPR, because the loop happens to have an initialization
14378 part, then expressed as the first operand of the COMPOUND_EXPR. If
14379 the search finds something, 1 is returned. Otherwise, 0 is
14380 returned. The search is assumed to start from a
14381 LABELED_BLOCK_EXPR's block. */
14382
14383static tree
14384search_loop (statement)
14385 tree statement;
14386{
14387 if (TREE_CODE (statement) == LOOP_EXPR)
14388 return statement;
14389
14390 if (TREE_CODE (statement) == BLOCK)
14391 statement = BLOCK_SUBBLOCKS (statement);
14392 else
14393 return NULL_TREE;
14394
14395 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14396 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14397 statement = TREE_OPERAND (statement, 1);
14398
14399 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14400 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14401}
14402
14403/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14404 returned otherwise. */
14405
14406static int
14407labeled_block_contains_loop_p (block, loop)
14408 tree block, loop;
14409{
14410 if (!block)
14411 return 0;
14412
14413 if (LABELED_BLOCK_BODY (block) == loop)
14414 return 1;
14415
c2952b01 14416 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14417 return 1;
14418
14419 return 0;
14420}
14421
e04a16fb 14422/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14423 insert LOOP as its body. */
e04a16fb
AG
14424
14425static tree
14426patch_loop_statement (loop)
14427 tree loop;
14428{
cd9643f7 14429 tree loop_label;
5cbdba64 14430
cd9643f7 14431 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14432 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14433 return loop;
14434
cd9643f7 14435 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14436 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14437 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14438 LABELED_BLOCK_BODY (loop_label) = loop;
14439 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14440 return loop_label;
e04a16fb
AG
14441}
14442
14443/* 14.13, 14.14: break and continue Statements */
14444
14445/* Build a break or a continue statement. a null NAME indicates an
14446 unlabeled break/continue statement. */
14447
14448static tree
14449build_bc_statement (location, is_break, name)
14450 int location, is_break;
14451 tree name;
14452{
14453 tree break_continue, label_block_expr = NULL_TREE;
14454
14455 if (name)
14456 {
14457 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14458 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14459 /* Null means that we don't have a target for this named
14460 break/continue. In this case, we make the target to be the
14461 label name, so that the error can be reported accuratly in
14462 patch_bc_statement. */
14463 label_block_expr = EXPR_WFL_NODE (name);
14464 }
14465 /* Unlabeled break/continue will be handled during the
14466 break/continue patch operation */
14467 break_continue
14468 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14469
14470 IS_BREAK_STMT_P (break_continue) = is_break;
14471 TREE_SIDE_EFFECTS (break_continue) = 1;
14472 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14473 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14474 return break_continue;
14475}
14476
14477/* Verification of a break/continue statement. */
14478
14479static tree
14480patch_bc_statement (node)
14481 tree node;
14482{
14483 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14484 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14485 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14486
e04a16fb 14487 /* Having an identifier here means that the target is unknown. */
b635eb2f 14488 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14489 {
14490 parse_error_context (wfl_operator, "No label definition found for `%s'",
14491 IDENTIFIER_POINTER (bc_label));
14492 return error_mark_node;
14493 }
b635eb2f 14494 if (! IS_BREAK_STMT_P (node))
e04a16fb 14495 {
b635eb2f
PB
14496 /* It's a continue statement. */
14497 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14498 {
b635eb2f
PB
14499 if (labeled_block == NULL_TREE)
14500 {
14501 if (bc_label == NULL_TREE)
14502 parse_error_context (wfl_operator,
14503 "`continue' must be in loop");
14504 else
1504b2b4
APB
14505 parse_error_context
14506 (wfl_operator, "continue label `%s' does not name a loop",
14507 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14508 return error_mark_node;
14509 }
14510 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14511 == continue_identifier_node)
14512 && (bc_label == NULL_TREE
14513 || TREE_CHAIN (labeled_block) == bc_label))
14514 {
14515 bc_label = labeled_block;
14516 break;
14517 }
e04a16fb 14518 }
e04a16fb 14519 }
b635eb2f 14520 else if (!bc_label)
34f4db93 14521 {
b635eb2f 14522 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14523 {
b635eb2f
PB
14524 if (labeled_block == NULL_TREE)
14525 {
14526 parse_error_context (wfl_operator,
14527 "`break' must be in loop or switch");
14528 return error_mark_node;
14529 }
14530 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14531 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14532 || search_loop (target_stmt))
b635eb2f
PB
14533 {
14534 bc_label = labeled_block;
14535 break;
14536 }
e04a16fb 14537 }
e04a16fb
AG
14538 }
14539
b635eb2f 14540 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14541 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14542
e04a16fb
AG
14543 /* Our break/continue don't return values. */
14544 TREE_TYPE (node) = void_type_node;
14545 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14546 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14547 sometimes, like after a if statement) */
14548 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14549 TREE_SIDE_EFFECTS (node) = 1;
14550 return node;
14551}
14552
14553/* Process the exit expression belonging to a loop. Its type must be
14554 boolean. */
14555
14556static tree
14557patch_exit_expr (node)
14558 tree node;
14559{
14560 tree expression = TREE_OPERAND (node, 0);
14561 TREE_TYPE (node) = error_mark_node;
14562 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14563
14564 /* The type of expression must be boolean */
14565 if (TREE_TYPE (expression) != boolean_type_node)
14566 {
14567 parse_error_context
14568 (wfl_operator,
781b0558 14569 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14570 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14571 return error_mark_node;
14572 }
14573 /* Now we know things are allright, invert the condition, fold and
14574 return */
14575 TREE_OPERAND (node, 0) =
14576 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14577
14578 if (! integer_zerop (TREE_OPERAND (node, 0))
14579 && ctxp->current_loop != NULL_TREE
14580 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14581 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14582 if (! integer_onep (TREE_OPERAND (node, 0)))
14583 CAN_COMPLETE_NORMALLY (node) = 1;
14584
14585
e04a16fb
AG
14586 TREE_TYPE (node) = void_type_node;
14587 return node;
14588}
b67d701b
PB
14589
14590/* 14.9 Switch statement */
14591
14592static tree
14593patch_switch_statement (node)
14594 tree node;
14595{
c877974e 14596 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14597
14598 /* Complete the switch expression */
14599 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14600 se_type = TREE_TYPE (se);
14601 /* The type of the switch expression must be char, byte, short or
14602 int */
2e0f0aff 14603 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14604 {
14605 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14606 parse_error_context (wfl_operator,
14607 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14608 lang_printable_name (se_type, 0));
b67d701b
PB
14609 /* This is what java_complete_tree will check */
14610 TREE_OPERAND (node, 0) = error_mark_node;
14611 return error_mark_node;
14612 }
14613
15fdcfe9 14614 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14615
14616 /* Ready to return */
15fdcfe9 14617 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14618 {
14619 TREE_TYPE (node) = error_mark_node;
14620 return error_mark_node;
14621 }
14622 TREE_TYPE (node) = void_type_node;
14623 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14624 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14625 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14626 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14627 return node;
14628}
14629
165f37bc 14630/* 14.18 The try/catch statements */
b67d701b 14631
b67d701b 14632static tree
a7d8d81f 14633build_try_statement (location, try_block, catches)
b67d701b 14634 int location;
a7d8d81f
PB
14635 tree try_block, catches;
14636{
14637 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14638 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14639 return node;
b67d701b
PB
14640}
14641
a7d8d81f
PB
14642static tree
14643build_try_finally_statement (location, try_block, finally)
14644 int location;
14645 tree try_block, finally;
b67d701b 14646{
a7d8d81f
PB
14647 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14648 EXPR_WFL_LINECOL (node) = location;
14649 return node;
b67d701b
PB
14650}
14651
14652static tree
14653patch_try_statement (node)
14654 tree node;
14655{
14656 int error_found = 0;
14657 tree try = TREE_OPERAND (node, 0);
14658 /* Exception handlers are considered in left to right order */
14659 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14660 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14661
14662 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14663 to process the next catch clause. We process the catch clause before
14664 the try block so that when processing the try block we can check thrown
14665 exceptions againts the caught type list. */
b67d701b
PB
14666 for (current = catch; current; current = TREE_CHAIN (current))
14667 {
14668 tree carg_decl, carg_type;
14669 tree sub_current, catch_block, catch_clause;
14670 int unreachable;
14671
b67d701b 14672 /* At this point, the structure of the catch clause is
b67d701b
PB
14673 CATCH_EXPR (catch node)
14674 BLOCK (with the decl of the parameter)
14675 COMPOUND_EXPR
7525cc04 14676 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14677 BLOCK (catch clause block)
a7d8d81f
PB
14678 */
14679 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14680 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14681 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14682
14683 /* Catch clauses can't have more than one parameter declared,
14684 but it's already enforced by the grammar. Make sure that the
14685 only parameter of the clause statement in of class Throwable
14686 or a subclass of Throwable, but that was done earlier. The
14687 catch clause parameter type has also been resolved. */
14688
14689 /* Just make sure that the catch clause parameter type inherits
14690 from java.lang.Throwable */
14691 if (!inherits_from_p (carg_type, throwable_type_node))
14692 {
14693 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14694 parse_error_context (wfl_operator,
781b0558 14695 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14696 lang_printable_name (carg_type, 0));
b67d701b
PB
14697 error_found = 1;
14698 continue;
14699 }
14700
14701 /* Partial check for unreachable catch statement: The catch
14702 clause is reachable iff is no earlier catch block A in
14703 the try statement such that the type of the catch
14704 clause's parameter is the same as or a subclass of the
14705 type of A's parameter */
14706 unreachable = 0;
14707 for (sub_current = catch;
14708 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14709 {
14710 tree sub_catch_clause, decl;
a7d8d81f 14711 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14712 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14713
14714 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14715 {
14716 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14717 parse_error_context
781b0558
KG
14718 (wfl_operator,
14719 "`catch' not reached because of the catch clause at line %d",
14720 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14721 unreachable = error_found = 1;
14722 break;
14723 }
14724 }
b67d701b
PB
14725 /* Complete the catch clause block */
14726 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14727 if (catch_block == error_mark_node)
14728 {
14729 error_found = 1;
14730 continue;
14731 }
15fdcfe9
PB
14732 if (CAN_COMPLETE_NORMALLY (catch_block))
14733 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14734 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14735
14736 if (unreachable)
14737 continue;
14738
14739 /* Things to do here: the exception must be thrown */
14740
14741 /* Link this type to the caught type list */
14742 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14743 }
14744
b9f7e36c
APB
14745 PUSH_EXCEPTIONS (caught_type_list);
14746 if ((try = java_complete_tree (try)) == error_mark_node)
14747 error_found = 1;
15fdcfe9
PB
14748 if (CAN_COMPLETE_NORMALLY (try))
14749 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14750 POP_EXCEPTIONS ();
14751
b67d701b
PB
14752 /* Verification ends here */
14753 if (error_found)
14754 return error_mark_node;
14755
14756 TREE_OPERAND (node, 0) = try;
14757 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14758 TREE_TYPE (node) = void_type_node;
14759 return node;
14760}
b9f7e36c
APB
14761
14762/* 14.17 The synchronized Statement */
14763
14764static tree
14765patch_synchronized_statement (node, wfl_op1)
14766 tree node, wfl_op1;
14767{
5a005d9e 14768 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14769 tree block = TREE_OPERAND (node, 1);
5a005d9e 14770
d8fccff5 14771 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14772
14773 if (expr == error_mark_node)
14774 {
14775 block = java_complete_tree (block);
14776 return expr;
14777 }
b9f7e36c
APB
14778
14779 /* The TYPE of expr must be a reference type */
5a005d9e 14780 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14781 {
14782 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14783 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14784 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14785 return error_mark_node;
14786 }
14787
ce6e9147
APB
14788 if (flag_emit_xref)
14789 {
14790 TREE_OPERAND (node, 0) = expr;
14791 TREE_OPERAND (node, 1) = java_complete_tree (block);
14792 CAN_COMPLETE_NORMALLY (node) = 1;
14793 return node;
14794 }
14795
b9f7e36c
APB
14796 /* Generate a try-finally for the synchronized statement, except
14797 that the handler that catches all throw exception calls
14798 _Jv_MonitorExit and then rethrow the exception.
14799 The synchronized statement is then implemented as:
14800 TRY
14801 {
14802 _Jv_MonitorEnter (expression)
14803 synchronized_block
14804 _Jv_MonitorExit (expression)
14805 }
14806 CATCH_ALL
14807 {
14808 e = _Jv_exception_info ();
14809 _Jv_MonitorExit (expression)
14810 Throw (e);
14811 } */
14812
5a005d9e
PB
14813 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14814 BUILD_MONITOR_ENTER (enter, expr_decl);
14815 BUILD_MONITOR_EXIT (exit, expr_decl);
14816 CAN_COMPLETE_NORMALLY (enter) = 1;
14817 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14818 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14819 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14820 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14821 build (COMPOUND_EXPR, NULL_TREE,
14822 build (WITH_CLEANUP_EXPR, NULL_TREE,
14823 build (COMPOUND_EXPR, NULL_TREE,
96847892 14824 assignment, enter),
5a005d9e
PB
14825 NULL_TREE, exit),
14826 block));
14827 node = build_expr_block (node, expr_decl);
14828
14829 return java_complete_tree (node);
b9f7e36c
APB
14830}
14831
14832/* 14.16 The throw Statement */
14833
14834static tree
14835patch_throw_statement (node, wfl_op1)
14836 tree node, wfl_op1;
14837{
14838 tree expr = TREE_OPERAND (node, 0);
14839 tree type = TREE_TYPE (expr);
14840 int unchecked_ok = 0, tryblock_throws_ok = 0;
14841
14842 /* Thrown expression must be assignable to java.lang.Throwable */
14843 if (!try_reference_assignconv (throwable_type_node, expr))
14844 {
14845 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14846 parse_error_context (wfl_operator,
14847 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14848 lang_printable_name (type, 0));
b9f7e36c
APB
14849 /* If the thrown expression was a reference, we further the
14850 compile-time check. */
14851 if (!JREFERENCE_TYPE_P (type))
14852 return error_mark_node;
14853 }
14854
14855 /* At least one of the following must be true */
14856
14857 /* The type of the throw expression is a not checked exception,
14858 i.e. is a unchecked expression. */
c877974e 14859 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14860
c2952b01
APB
14861 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14862 /* An instance can't throw a checked excetion unless that exception
14863 is explicitely declared in the `throws' clause of each
14864 constructor. This doesn't apply to anonymous classes, since they
14865 don't have declared constructors. */
14866 if (!unchecked_ok
14867 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14868 {
14869 tree current;
14870 for (current = TYPE_METHODS (current_class); current;
14871 current = TREE_CHAIN (current))
14872 if (DECL_CONSTRUCTOR_P (current)
14873 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14874 {
14875 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)",
14876 lang_printable_name (TREE_TYPE (expr), 0));
14877 return error_mark_node;
14878 }
14879 }
14880
b9f7e36c
APB
14881 /* Throw is contained in a try statement and at least one catch
14882 clause can receive the thrown expression or the current method is
14883 declared to throw such an exception. Or, the throw statement is
14884 contained in a method or constructor declaration and the type of
14885 the Expression is assignable to at least one type listed in the
14886 throws clause the declaration. */
b9f7e36c 14887 if (!unchecked_ok)
f099f336 14888 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14889 if (!(unchecked_ok || tryblock_throws_ok))
14890 {
14891 /* If there is a surrounding try block that has no matching
14892 clatch clause, report it first. A surrounding try block exits
14893 only if there is something after the list of checked
14894 exception thrown by the current function (if any). */
14895 if (IN_TRY_BLOCK_P ())
781b0558 14896 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 14897 lang_printable_name (type, 0));
b9f7e36c
APB
14898 /* If we have no surrounding try statement and the method doesn't have
14899 any throws, report it now. FIXME */
f099f336
APB
14900
14901 /* We report that the exception can't be throw from a try block
14902 in all circumstances but when the `throw' is inside a static
14903 block. */
b9f7e36c
APB
14904 else if (!EXCEPTIONS_P (currently_caught_type_list)
14905 && !tryblock_throws_ok)
f099f336 14906 {
c2952b01 14907 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14908 parse_error_context (wfl_operator,
14909 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14910 lang_printable_name (type, 0));
14911 else
781b0558
KG
14912 parse_error_context (wfl_operator,
14913 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14914 lang_printable_name (type, 0));
14915 }
b9f7e36c
APB
14916 /* Otherwise, the current method doesn't have the appropriate
14917 throws declaration */
14918 else
781b0558 14919 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14920 lang_printable_name (type, 0));
b9f7e36c
APB
14921 return error_mark_node;
14922 }
14923
ce6e9147 14924 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14925 BUILD_THROW (node, expr);
ce6e9147
APB
14926
14927 /* If doing xrefs, keep the location where the `throw' was seen. */
14928 if (flag_emit_xref)
14929 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14930 return node;
14931}
14932
14933/* Check that exception said to be thrown by method DECL can be
14934 effectively caught from where DECL is invoked. */
14935
14936static void
14937check_thrown_exceptions (location, decl)
14938 int location;
14939 tree decl;
14940{
14941 tree throws;
14942 /* For all the unchecked exceptions thrown by DECL */
14943 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14944 throws = TREE_CHAIN (throws))
0a2138e2 14945 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14946 {
3e78f871
PB
14947#if 1
14948 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14949 if (DECL_NAME (decl) == get_identifier ("clone"))
14950 continue;
14951#endif
b9f7e36c 14952 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14953 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14954 parse_error_context
14955 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14956 lang_printable_name (TREE_VALUE (throws), 0));
14957 else
14958 {
14959 parse_error_context
781b0558 14960 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14961 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14962 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14963 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14964 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14965 }
b9f7e36c
APB
14966 }
14967}
14968
c877974e 14969/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14970 try-catch blocks, OR is listed in the `throws' clause of the
14971 current method. */
14972
14973static int
0a2138e2 14974check_thrown_exceptions_do (exception)
b9f7e36c
APB
14975 tree exception;
14976{
14977 tree list = currently_caught_type_list;
c877974e 14978 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14979 /* First, all the nested try-catch-finally at that stage. The
14980 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14981 if (IS_UNCHECKED_EXCEPTION_P (exception))
14982 return 1;
b9f7e36c
APB
14983 while (list)
14984 {
14985 tree caught;
14986 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14987 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14988 return 1;
14989 list = TREE_CHAIN (list);
14990 }
14991 return 0;
14992}
14993
14994static void
14995purge_unchecked_exceptions (mdecl)
14996 tree mdecl;
14997{
14998 tree throws = DECL_FUNCTION_THROWS (mdecl);
14999 tree new = NULL_TREE;
15000
15001 while (throws)
15002 {
15003 tree next = TREE_CHAIN (throws);
c877974e 15004 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15005 {
15006 TREE_CHAIN (throws) = new;
15007 new = throws;
15008 }
15009 throws = next;
15010 }
15011 /* List is inverted here, but it doesn't matter */
15012 DECL_FUNCTION_THROWS (mdecl) = new;
15013}
22eed1e6
APB
15014
15015/* 15.24 Conditional Operator ?: */
15016
15017static tree
15018patch_conditional_expr (node, wfl_cond, wfl_op1)
15019 tree node, wfl_cond, wfl_op1;
15020{
15021 tree cond = TREE_OPERAND (node, 0);
15022 tree op1 = TREE_OPERAND (node, 1);
15023 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15024 tree resulting_type = NULL_TREE;
ac825856 15025 tree t1, t2, patched;
22eed1e6
APB
15026 int error_found = 0;
15027
ac825856
APB
15028 /* Operands of ?: might be StringBuffers crafted as a result of a
15029 string concatenation. Obtain a descent operand here. */
15030 if ((patched = patch_string (op1)))
15031 TREE_OPERAND (node, 1) = op1 = patched;
15032 if ((patched = patch_string (op2)))
15033 TREE_OPERAND (node, 2) = op2 = patched;
15034
15035 t1 = TREE_TYPE (op1);
15036 t2 = TREE_TYPE (op2);
15037
22eed1e6
APB
15038 /* The first expression must be a boolean */
15039 if (TREE_TYPE (cond) != boolean_type_node)
15040 {
15041 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15042 parse_error_context (wfl_operator,
15043 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15044 lang_printable_name (TREE_TYPE (cond), 0));
15045 error_found = 1;
15046 }
15047
15048 /* Second and third can be numeric, boolean (i.e. primitive),
15049 references or null. Anything else results in an error */
15050 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15051 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15052 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15053 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15054 error_found = 1;
15055
15056 /* Determine the type of the conditional expression. Same types are
15057 easy to deal with */
15058 else if (t1 == t2)
15059 resulting_type = t1;
15060
15061 /* There are different rules for numeric types */
15062 else if (JNUMERIC_TYPE_P (t1))
15063 {
15064 /* if byte/short found, the resulting type is short */
15065 if ((t1 == byte_type_node && t2 == short_type_node)
15066 || (t1 == short_type_node && t2 == byte_type_node))
15067 resulting_type = short_type_node;
15068
15069 /* If t1 is a constant int and t2 is of type byte, short or char
15070 and t1's value fits in t2, then the resulting type is t2 */
15071 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15072 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15073 resulting_type = t2;
15074
15075 /* If t2 is a constant int and t1 is of type byte, short or char
15076 and t2's value fits in t1, then the resulting type is t1 */
15077 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15078 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15079 resulting_type = t1;
15080
15081 /* Otherwise, binary numeric promotion is applied and the
15082 resulting type is the promoted type of operand 1 and 2 */
15083 else
93024893 15084 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15085 &TREE_OPERAND (node, 1),
15086 &TREE_OPERAND (node, 2));
15087 }
15088
15089 /* Cases of a reference and a null type */
15090 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15091 resulting_type = t1;
15092
15093 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15094 resulting_type = t2;
15095
15096 /* Last case: different reference types. If a type can be converted
15097 into the other one by assignment conversion, the latter
15098 determines the type of the expression */
15099 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15100 resulting_type = promote_type (t1);
15101
15102 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15103 resulting_type = promote_type (t2);
15104
15105 /* If we don't have any resulting type, we're in trouble */
15106 if (!resulting_type)
15107 {
c2e3db92 15108 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15109 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15110 parse_error_context (wfl_operator,
15111 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15112 t, lang_printable_name (t2, 0));
22eed1e6
APB
15113 free (t);
15114 error_found = 1;
15115 }
15116
15117 if (error_found)
15118 {
15119 TREE_TYPE (node) = error_mark_node;
15120 return error_mark_node;
15121 }
15122
15123 TREE_TYPE (node) = resulting_type;
15124 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15125 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15126 return node;
15127}
ac825856 15128
5b09b33e
PB
15129/* Try to constant fold NODE.
15130 If NODE is not a constant expression, return NULL_EXPR.
15131 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15132
15133static tree
15134fold_constant_for_init (node, context)
15135 tree node;
15136 tree context;
15137{
15138 tree op0, op1, val;
15139 enum tree_code code = TREE_CODE (node);
15140
ee97d354 15141 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15142 return node;
93024893 15143
5b09b33e
PB
15144 switch (code)
15145 {
5b09b33e
PB
15146 case PLUS_EXPR:
15147 case MINUS_EXPR:
bc3ca41b
PB
15148 case MULT_EXPR:
15149 case TRUNC_MOD_EXPR:
15150 case RDIV_EXPR:
5b09b33e
PB
15151 case LSHIFT_EXPR:
15152 case RSHIFT_EXPR:
15153 case URSHIFT_EXPR:
15154 case BIT_AND_EXPR:
15155 case BIT_XOR_EXPR:
15156 case BIT_IOR_EXPR:
5b09b33e
PB
15157 case TRUTH_ANDIF_EXPR:
15158 case TRUTH_ORIF_EXPR:
15159 case EQ_EXPR:
15160 case NE_EXPR:
15161 case GT_EXPR:
15162 case GE_EXPR:
15163 case LT_EXPR:
15164 case LE_EXPR:
15165 op0 = TREE_OPERAND (node, 0);
15166 op1 = TREE_OPERAND (node, 1);
15167 val = fold_constant_for_init (op0, context);
15168 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15169 return NULL_TREE;
15170 TREE_OPERAND (node, 0) = val;
15171 val = fold_constant_for_init (op1, context);
15172 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15173 return NULL_TREE;
15174 TREE_OPERAND (node, 1) = val;
15175 return patch_binop (node, op0, op1);
15176
15177 case UNARY_PLUS_EXPR:
15178 case NEGATE_EXPR:
15179 case TRUTH_NOT_EXPR:
15180 case BIT_NOT_EXPR:
15181 case CONVERT_EXPR:
15182 op0 = TREE_OPERAND (node, 0);
15183 val = fold_constant_for_init (op0, context);
15184 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15185 return NULL_TREE;
15186 TREE_OPERAND (node, 0) = val;
5a005d9e 15187 return patch_unaryop (node, op0);
5b09b33e
PB
15188 break;
15189
15190 case COND_EXPR:
15191 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15192 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15193 return NULL_TREE;
15194 TREE_OPERAND (node, 0) = val;
15195 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15196 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15197 return NULL_TREE;
15198 TREE_OPERAND (node, 1) = val;
15199 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15200 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15201 return NULL_TREE;
15202 TREE_OPERAND (node, 2) = val;
15203 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15204 : TREE_OPERAND (node, 2);
15205
15206 case VAR_DECL:
8576f094
APB
15207 case FIELD_DECL:
15208 if (! FIELD_FINAL (node)
5b09b33e
PB
15209 || DECL_INITIAL (node) == NULL_TREE)
15210 return NULL_TREE;
15211 val = DECL_INITIAL (node);
15212 /* Guard against infinite recursion. */
15213 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15214 val = fold_constant_for_init (val, node);
5b09b33e
PB
15215 DECL_INITIAL (node) = val;
15216 return val;
15217
15218 case EXPR_WITH_FILE_LOCATION:
15219 /* Compare java_complete_tree and resolve_expression_name. */
15220 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15221 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15222 {
15223 tree name = EXPR_WFL_NODE (node);
15224 tree decl;
15225 if (PRIMARY_P (node))
15226 return NULL_TREE;
15227 else if (! QUALIFIED_P (name))
15228 {
15229 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15230 if (decl == NULL_TREE
15231 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15232 return NULL_TREE;
15233 return fold_constant_for_init (decl, decl);
15234 }
15235 else
15236 {
5b09b33e
PB
15237 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15238 qualify_ambiguous_name (node);
15239 if (resolve_field_access (node, &decl, NULL)
15240 && decl != NULL_TREE)
15241 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15242 return NULL_TREE;
15243 }
15244 }
15245 else
15246 {
15247 op0 = TREE_OPERAND (node, 0);
15248 val = fold_constant_for_init (op0, context);
15249 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15250 return NULL_TREE;
15251 TREE_OPERAND (node, 0) = val;
15252 return val;
15253 }
15254
bc3ca41b
PB
15255#ifdef USE_COMPONENT_REF
15256 case IDENTIFIER:
15257 case COMPONENT_REF:
15258 ?;
15259#endif
15260
5b09b33e
PB
15261 default:
15262 return NULL_TREE;
15263 }
15264}
bc3ca41b
PB
15265
15266#ifdef USE_COMPONENT_REF
15267/* Context is 'T' for TypeName, 'P' for PackageName,
15268 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15269
15270tree
15271resolve_simple_name (name, context)
15272 tree name;
15273 int context;
15274{
15275}
15276
15277tree
15278resolve_qualified_name (name, context)
15279 tree name;
15280 int context;
15281{
15282}
15283#endif
f15b9af9
MM
15284
15285/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15286
15287static void
15288mark_parser_ctxt (p)
15289 void *p;
15290{
15291 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15292 int i;
15293
15294 if (!pc)
15295 return;
15296
15297#ifndef JC1_LITE
15298 for (i = 0; i < 11; ++i)
15299 ggc_mark_tree (pc->modifier_ctx[i]);
15300 ggc_mark_tree (pc->class_type);
15301 ggc_mark_tree (pc->function_decl);
15302 ggc_mark_tree (pc->package);
15303 ggc_mark_tree (pc->incomplete_class);
15304 ggc_mark_tree (pc->gclass_list);
15305 ggc_mark_tree (pc->class_list);
15306 ggc_mark_tree (pc->current_parsed_class);
15307 ggc_mark_tree (pc->current_parsed_class_un);
15308 ggc_mark_tree (pc->non_static_initialized);
15309 ggc_mark_tree (pc->static_initialized);
15310 ggc_mark_tree (pc->instance_initializers);
15311 ggc_mark_tree (pc->import_list);
15312 ggc_mark_tree (pc->import_demand_list);
15313 ggc_mark_tree (pc->current_loop);
15314 ggc_mark_tree (pc->current_labeled_block);
15315#endif /* JC1_LITE */
15316
15317 if (pc->next)
15318 mark_parser_ctxt (&pc->next);
15319}
This page took 2.756296 seconds and 5 git commands to generate.