]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
invoke.texi: Use @table @samp instead of @itemize @minus for table of language standards.
[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)));
5e18f6d6 7840 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7841
5e18f6d6
APB
7842 /* If the immediate enclosing context of the current class is the
7843 field decl's class or inherits from it; build the access as
7844 `this$<n>.<field>'. Note that we will break the `private' barrier
7845 if we're not emitting bytecodes. */
7846 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7847 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7848 {
7849 tree thisn = build_current_thisn (current_class);
7850 access = make_qualified_primary (build_wfl_node (thisn),
7851 id, EXPR_WFL_LINECOL (id));
7852 }
7853 /* Otherwise, generate access methods to outer this and access the
7854 field (either using an access method or by direct access.) */
7855 else
7856 {
7857 int lc = EXPR_WFL_LINECOL (id);
7858
7859 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7860 get a hold to the enclosing instance we need, and then we
c2952b01 7861 build the field access. */
5e18f6d6 7862 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7863
7864 /* If the field is private and we're generating bytecode, then
7865 we generate an access method */
7866 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7867 {
7868 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7869 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7870 name, access, NULL_TREE);
7871 }
7872 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7873 Once again we break the `private' access rule from a foreign
7874 class. */
7875 else
7876 access = make_qualified_primary (access, id, lc);
7877 }
7878 return resolve_expression_name (access, NULL);
7879}
7880
7881/* Return a non zero value if NODE describes an outer field inner
7882 access. */
7883
7884static int
7885outer_field_access_p (type, decl)
7886 tree type, decl;
7887{
7888 if (!INNER_CLASS_TYPE_P (type)
7889 || TREE_CODE (decl) != FIELD_DECL
7890 || DECL_CONTEXT (decl) == type)
7891 return 0;
ee5f86dc
APB
7892
7893 /* If the inner class extends the declaration context of the field
7894 we're try to acces, then this isn't an outer field access */
7895 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7896 return 0;
c2952b01
APB
7897
7898 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7899 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7900 {
7901 if (type == DECL_CONTEXT (decl))
7902 return 1;
5e18f6d6 7903
c2952b01 7904 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7905 {
7906 /* Before we give up, see whether the field is inherited from
7907 the enclosing context we're considering. */
7908 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7909 return 1;
7910 break;
7911 }
c2952b01
APB
7912 }
7913
7914 return 0;
7915}
7916
7917/* Return a non zero value if NODE represents an outer field inner
7918 access that was been already expanded. As a side effect, it returns
7919 the name of the field being accessed and the argument passed to the
7920 access function, suitable for a regeneration of the access method
7921 call if necessary. */
7922
7923static int
7924outer_field_expanded_access_p (node, name, arg_type, arg)
7925 tree node, *name, *arg_type, *arg;
7926{
7927 int identified = 0;
7928
7929 if (TREE_CODE (node) != CALL_EXPR)
7930 return 0;
7931
7932 /* Well, gcj generates slightly different tree nodes when compiling
7933 to native or bytecodes. It's the case for function calls. */
7934
7935 if (flag_emit_class_files
7936 && TREE_CODE (node) == CALL_EXPR
7937 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7938 identified = 1;
7939 else if (!flag_emit_class_files)
7940 {
7941 node = TREE_OPERAND (node, 0);
7942
7943 if (node && TREE_OPERAND (node, 0)
7944 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7945 {
7946 node = TREE_OPERAND (node, 0);
7947 if (TREE_OPERAND (node, 0)
7948 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7949 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7950 (DECL_NAME (TREE_OPERAND (node, 0)))))
7951 identified = 1;
7952 }
7953 }
7954
7955 if (identified && name && arg_type && arg)
7956 {
7957 tree argument = TREE_OPERAND (node, 1);
7958 *name = DECL_NAME (TREE_OPERAND (node, 0));
7959 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7960 *arg = TREE_VALUE (argument);
7961 }
7962 return identified;
7963}
7964
7965/* Detect in NODE an outer field read access from an inner class and
7966 transform it into a write with RHS as an argument. This function is
7967 called from the java_complete_lhs when an assignment to a LHS can
7968 be identified. */
7969
7970static tree
7971outer_field_access_fix (wfl, node, rhs)
7972 tree wfl, node, rhs;
7973{
7974 tree name, arg_type, arg;
7975
7976 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7977 {
7978 /* At any rate, check whether we're trying to assign a value to
7979 a final. */
7980 tree accessed = (JDECL_P (node) ? node :
7981 (TREE_CODE (node) == COMPONENT_REF ?
7982 TREE_OPERAND (node, 1) : node));
7983 if (check_final_assignment (accessed, wfl))
7984 return error_mark_node;
7985
7986 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7987 arg_type, name, arg, rhs);
7988 return java_complete_tree (node);
7989 }
7990 return NULL_TREE;
7991}
7992
7993/* Construct the expression that calls an access method:
7994 <type>.access$<n>(<arg1> [, <arg2>]);
7995
7996 ARG2 can be NULL and will be omitted in that case. It will denote a
7997 read access. */
7998
7999static tree
8000build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8001 int lc;
8002 tree type, access_method_name, arg1, arg2;
8003{
8004 tree args, cn, access;
8005
8006 args = arg1 ? arg1 :
8007 build_wfl_node (build_current_thisn (current_class));
8008 args = build_tree_list (NULL_TREE, args);
8009
8010 if (arg2)
8011 args = tree_cons (NULL_TREE, arg2, args);
8012
8013 access = build_method_invocation (build_wfl_node (access_method_name), args);
8014 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8015 return make_qualified_primary (cn, access, lc);
8016}
8017
8018static tree
8019build_new_access_id ()
8020{
8021 static int access_n_counter = 1;
8022 char buffer [128];
8023
8024 sprintf (buffer, "access$%d", access_n_counter++);
8025 return get_identifier (buffer);
8026}
8027
8028/* Create the static access functions for the outer field DECL. We define a
8029 read:
8030 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8031 return inst$.field;
8032 }
8033 and a write access:
8034 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8035 TREE_TYPE (<field>) value$) {
8036 return inst$.field = value$;
8037 }
8038 We should have a usage flags on the DECL so we can lazily turn the ones
8039 we're using for code generation. FIXME.
8040*/
8041
8042static tree
8043build_outer_field_access_methods (decl)
8044 tree decl;
8045{
8046 tree id, args, stmt, mdecl;
8047
8048 /* Check point, to be removed. FIXME */
8049 if (FIELD_INNER_ACCESS (decl)
8050 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
8051 abort ();
8052
8053 if (FIELD_INNER_ACCESS (decl))
8054 return FIELD_INNER_ACCESS (decl);
8055
c2952b01
APB
8056 /* Create the identifier and a function named after it. */
8057 id = build_new_access_id ();
8058
8059 /* The identifier is marked as bearing the name of a generated write
8060 access function for outer field accessed from inner classes. */
8061 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8062
8063 /* Create the read access */
8064 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8065 TREE_CHAIN (args) = end_params_node;
8066 stmt = make_qualified_primary (build_wfl_node (inst_id),
8067 build_wfl_node (DECL_NAME (decl)), 0);
8068 stmt = build_return (0, stmt);
8069 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8070 TREE_TYPE (decl), id, args, stmt);
8071 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8072
8073 /* Create the write access method */
8074 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8075 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8076 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8077 stmt = make_qualified_primary (build_wfl_node (inst_id),
8078 build_wfl_node (DECL_NAME (decl)), 0);
8079 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8080 build_wfl_node (wpv_id)));
8081
8082 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8083 TREE_TYPE (decl), id, args, stmt);
8084 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8085
8086 /* Return the access name */
8087 return FIELD_INNER_ACCESS (decl) = id;
8088}
8089
8090/* Build an field access method NAME. */
8091
8092static tree
8093build_outer_field_access_method (class, type, name, args, body)
8094 tree class, type, name, args, body;
8095{
8096 tree saved_current_function_decl, mdecl;
8097
8098 /* Create the method */
8099 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8100 fix_method_argument_names (args, mdecl);
8101 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8102
8103 /* Attach the method body. */
8104 saved_current_function_decl = current_function_decl;
8105 start_artificial_method_body (mdecl);
8106 java_method_add_stmt (mdecl, body);
8107 end_artificial_method_body (mdecl);
8108 current_function_decl = saved_current_function_decl;
8109
8110 return mdecl;
8111}
8112
8113\f
8114/* This section deals with building access function necessary for
8115 certain kinds of method invocation from inner classes. */
8116
8117static tree
8118build_outer_method_access_method (decl)
8119 tree decl;
8120{
8121 tree saved_current_function_decl, mdecl;
8122 tree args = NULL_TREE, call_args = NULL_TREE;
8123 tree carg, id, body, class;
8124 char buffer [80];
8125 int parm_id_count = 0;
8126
8127 /* Test this abort with an access to a private field */
8128 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8129 abort ();
8130
8131 /* Check the cache first */
8132 if (DECL_FUNCTION_INNER_ACCESS (decl))
8133 return DECL_FUNCTION_INNER_ACCESS (decl);
8134
8135 class = DECL_CONTEXT (decl);
8136
8137 /* Obtain an access identifier and mark it */
8138 id = build_new_access_id ();
8139 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8140
c2952b01
APB
8141 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8142 /* Create the arguments, as much as the original */
8143 for (; carg && carg != end_params_node;
8144 carg = TREE_CHAIN (carg))
8145 {
8146 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8147 args = chainon (args, build_tree_list (get_identifier (buffer),
8148 TREE_VALUE (carg)));
8149 }
8150 args = chainon (args, end_params_node);
8151
8152 /* Create the method */
8153 mdecl = create_artificial_method (class, ACC_STATIC,
8154 TREE_TYPE (TREE_TYPE (decl)), id, args);
8155 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8156 /* There is a potential bug here. We should be able to use
8157 fix_method_argument_names, but then arg names get mixed up and
8158 eventually a constructor will have its this$0 altered and the
8159 outer context won't be assignment properly. The test case is
8160 stub.java FIXME */
8161 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8162
8163 /* Attach the method body. */
8164 saved_current_function_decl = current_function_decl;
8165 start_artificial_method_body (mdecl);
8166
8167 /* The actual method invocation uses the same args. When invoking a
8168 static methods that way, we don't want to skip the first
8169 argument. */
8170 carg = args;
8171 if (!METHOD_STATIC (decl))
8172 carg = TREE_CHAIN (carg);
8173 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8174 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8175 call_args);
8176
8177 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8178 call_args);
8179 if (!METHOD_STATIC (decl))
8180 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8181 body, 0);
8182 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8183 body = build_return (0, body);
8184 java_method_add_stmt (mdecl,body);
8185 end_artificial_method_body (mdecl);
8186 current_function_decl = saved_current_function_decl;
c2952b01
APB
8187
8188 /* Back tag the access function so it know what it accesses */
8189 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8190
8191 /* Tag the current method so it knows it has an access generated */
8192 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8193}
8194
8195\f
8196/* This section of the code deals with building expressions to access
8197 the enclosing instance of an inner class. The enclosing instance is
8198 kept in a generated field called this$<n>, with <n> being the
8199 inner class nesting level (starting from 0.) */
8200
dba41d30
APB
8201/* Build an access to a given this$<n>, always chaining access call to
8202 others. Access methods to this$<n> are build on the fly if
8203 necessary. This CAN'T be used to solely access this$<n-1> from
8204 this$<n> (which alway yield to special cases and optimization, see
8205 for example build_outer_field_access). */
c2952b01
APB
8206
8207static tree
8208build_access_to_thisn (from, to, lc)
8209 tree from, to;
8210 int lc;
8211{
8212 tree access = NULL_TREE;
8213
8214 while (from != to)
8215 {
c2952b01 8216 if (!access)
dba41d30
APB
8217 {
8218 access = build_current_thisn (from);
8219 access = build_wfl_node (access);
8220 }
8221 else
c2952b01 8222 {
dba41d30
APB
8223 tree access0_wfl, cn;
8224
8225 maybe_build_thisn_access_method (from);
8226 access0_wfl = build_wfl_node (access0_identifier_node);
8227 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8228 EXPR_WFL_LINECOL (access0_wfl) = lc;
8229 access = build_tree_list (NULL_TREE, access);
8230 access = build_method_invocation (access0_wfl, access);
8231 access = make_qualified_primary (cn, access, lc);
c2952b01 8232 }
5e18f6d6
APB
8233
8234 /* if FROM isn't an inter class, that's fine, we've done
8235 enough. What we're looking for can be accessed from there. */
8236 from = DECL_CONTEXT (TYPE_NAME (from));
8237 if (!from)
8238 break;
8239 from = TREE_TYPE (from);
c2952b01
APB
8240 }
8241 return access;
8242}
8243
8244/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8245 is returned if nothing needs to be generated. Otherwise, the method
152de068 8246 generated and a method decl is returned.
c2952b01
APB
8247
8248 NOTE: These generated methods should be declared in a class file
8249 attribute so that they can't be referred to directly. */
8250
8251static tree
8252maybe_build_thisn_access_method (type)
8253 tree type;
8254{
8255 tree mdecl, args, stmt, rtype;
8256 tree saved_current_function_decl;
8257
8258 /* If TYPE is a top-level class, no access method is required.
8259 If there already is such an access method, bail out. */
ee5f86dc 8260 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8261 return NULL_TREE;
8262
8263 /* We generate the method. The method looks like:
8264 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8265 */
c2952b01
APB
8266 args = build_tree_list (inst_id, build_pointer_type (type));
8267 TREE_CHAIN (args) = end_params_node;
8268 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8269 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8270 access0_identifier_node, args);
8271 fix_method_argument_names (args, mdecl);
8272 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8273 stmt = build_current_thisn (type);
8274 stmt = make_qualified_primary (build_wfl_node (inst_id),
8275 build_wfl_node (stmt), 0);
8276 stmt = build_return (0, stmt);
8277
8278 saved_current_function_decl = current_function_decl;
8279 start_artificial_method_body (mdecl);
8280 java_method_add_stmt (mdecl, stmt);
8281 end_artificial_method_body (mdecl);
8282 current_function_decl = saved_current_function_decl;
c2952b01
APB
8283
8284 CLASS_ACCESS0_GENERATED_P (type) = 1;
8285
8286 return mdecl;
8287}
8288
8289/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8290 the first level of innerclassing. this$1 for the next one, etc...
8291 This function can be invoked with TYPE to NULL, available and then
8292 has to count the parser context. */
8293
8294static tree
8295build_current_thisn (type)
8296 tree type;
8297{
8298 static int saved_i = -1;
8299 static tree saved_thisn = NULL_TREE;
19e223db
MM
8300 static tree saved_type = NULL_TREE;
8301 static int saved_type_i = 0;
8302 static int initialized_p;
c2952b01
APB
8303 tree decl;
8304 char buffer [80];
8305 int i = 0;
8306
19e223db
MM
8307 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8308 if (!initialized_p)
c2952b01 8309 {
19e223db
MM
8310 ggc_add_tree_root (&saved_thisn, 1);
8311 ggc_add_tree_root (&saved_type, 1);
8312 initialized_p = 1;
8313 }
c2952b01 8314
19e223db
MM
8315 if (type)
8316 {
c2952b01
APB
8317 if (type == saved_type)
8318 i = saved_type_i;
8319 else
8320 {
8321 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8322 decl; decl = DECL_CONTEXT (decl), i++)
8323 ;
8324
8325 saved_type = type;
8326 saved_type_i = i;
8327 }
8328 }
8329 else
8330 i = list_length (GET_CPC_LIST ())-2;
8331
8332 if (i == saved_i)
8333 return saved_thisn;
8334
8335 sprintf (buffer, "this$%d", i);
8336 saved_i = i;
8337 saved_thisn = get_identifier (buffer);
8338 return saved_thisn;
8339}
8340
8341/* Return the assignement to the hidden enclosing context `this$<n>'
8342 by the second incoming parameter to the innerclass constructor. The
8343 form used is `this.this$<n> = this$<n>;'. */
8344
8345static tree
8346build_thisn_assign ()
8347{
8348 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8349 {
8350 tree thisn = build_current_thisn (current_class);
8351 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8352 build_wfl_node (thisn), 0);
8353 tree rhs = build_wfl_node (thisn);
8354 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8355 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8356 }
8357 return NULL_TREE;
8358}
8359
8360\f
165f37bc
APB
8361/* Building the synthetic `class$' used to implement the `.class' 1.1
8362 extension for non primitive types. This method looks like:
8363
8364 static Class class$(String type) throws NoClassDefFoundError
8365 {
8366 try {return (java.lang.Class.forName (String));}
8367 catch (ClassNotFoundException e) {
8368 throw new NoClassDefFoundError(e.getMessage());}
8369 } */
8370
8371static tree
8372build_dot_class_method (class)
8373 tree class;
8374{
8375#define BWF(S) build_wfl_node (get_identifier ((S)))
8376#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8377 tree args, tmp, saved_current_function_decl, mdecl;
8378 tree stmt, throw_stmt, catch, catch_block, try_block;
8379 tree catch_clause_param;
8380 tree class_not_found_exception, no_class_def_found_error;
8381
8382 static tree get_message_wfl, type_parm_wfl;
8383
8384 if (!get_message_wfl)
8385 {
8386 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8387 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8388 ggc_add_tree_root (&get_message_wfl, 1);
8389 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8390 }
8391
8392 /* Build the arguments */
8393 args = build_tree_list (get_identifier ("type$"),
8394 build_pointer_type (string_type_node));
8395 TREE_CHAIN (args) = end_params_node;
8396
8397 /* Build the qualified name java.lang.Class.forName */
8398 tmp = MQN (MQN (MQN (BWF ("java"),
8399 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8400
8401 /* For things we have to catch and throw */
8402 class_not_found_exception =
8403 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8404 no_class_def_found_error =
8405 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8406 load_class (class_not_found_exception, 1);
8407 load_class (no_class_def_found_error, 1);
8408
8409 /* Create the "class$" function */
8410 mdecl = create_artificial_method (class, ACC_STATIC,
8411 build_pointer_type (class_type_node),
8412 get_identifier ("class$"), args);
8413 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8414 no_class_def_found_error);
8415
8416 /* We start by building the try block. We need to build:
8417 return (java.lang.Class.forName (type)); */
8418 stmt = build_method_invocation (tmp,
8419 build_tree_list (NULL_TREE, type_parm_wfl));
8420 stmt = build_return (0, stmt);
8421 /* Put it in a block. That's the try block */
8422 try_block = build_expr_block (stmt, NULL_TREE);
8423
8424 /* Now onto the catch block. We start by building the expression
8425 throwing a new exception:
8426 throw new NoClassDefFoundError (_.getMessage); */
8427 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8428 get_message_wfl, 0);
8429 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8430
8431 /* Build new NoClassDefFoundError (_.getMessage) */
8432 throw_stmt = build_new_invocation
8433 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8434 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8435
8436 /* Build the throw, (it's too early to use BUILD_THROW) */
8437 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8438
8439 /* Build the catch block to encapsulate all this. We begin by
8440 building an decl for the catch clause parameter and link it to
8441 newly created block, the catch block. */
8442 catch_clause_param =
8443 build_decl (VAR_DECL, wpv_id,
8444 build_pointer_type (class_not_found_exception));
8445 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8446
8447 /* We initialize the variable with the exception handler. */
8448 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8449 soft_exceptioninfo_call_node);
8450 add_stmt_to_block (catch_block, NULL_TREE, catch);
8451
8452 /* We add the statement throwing the new exception */
8453 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8454
8455 /* Build a catch expression for all this */
8456 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8457
8458 /* Build the try/catch sequence */
8459 stmt = build_try_statement (0, try_block, catch_block);
8460
8461 fix_method_argument_names (args, mdecl);
8462 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8463 saved_current_function_decl = current_function_decl;
8464 start_artificial_method_body (mdecl);
8465 java_method_add_stmt (mdecl, stmt);
8466 end_artificial_method_body (mdecl);
8467 current_function_decl = saved_current_function_decl;
8468 TYPE_DOT_CLASS (class) = mdecl;
8469
8470 return mdecl;
8471}
8472
8473static tree
f0f3a777
APB
8474build_dot_class_method_invocation (type)
8475 tree type;
165f37bc 8476{
f0f3a777
APB
8477 tree sig_id, s;
8478
8479 if (TYPE_ARRAY_P (type))
8480 sig_id = build_java_signature (type);
8481 else
8482 sig_id = DECL_NAME (TYPE_NAME (type));
8483
1f8f4a0b
MM
8484 s = build_string (IDENTIFIER_LENGTH (sig_id),
8485 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8486 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8487 build_tree_list (NULL_TREE, s));
8488}
8489
c2952b01
APB
8490/* This section of the code deals with constructor. */
8491
22eed1e6
APB
8492/* Craft a body for default constructor. Patch existing constructor
8493 bodies with call to super() and field initialization statements if
8494 necessary. */
8495
8496static void
8497fix_constructors (mdecl)
8498 tree mdecl;
8499{
8500 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8501 tree thisn_assign, compound = NULL_TREE;
8502 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8503
22eed1e6
APB
8504 if (!body)
8505 {
22eed1e6
APB
8506 /* It is an error for the compiler to generate a default
8507 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8508 takes no argument, or the same args for an anonymous class */
8509 if (verify_constructor_super (mdecl))
22eed1e6 8510 {
c2952b01
APB
8511 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8512 tree save = DECL_NAME (mdecl);
49f48c71 8513 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8514 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8515 parse_error_context
c2952b01
APB
8516 (lookup_cl (TYPE_NAME (class_type)),
8517 "No constructor matching `%s' found in class `%s'",
8518 lang_printable_name (mdecl, 0), n);
8519 DECL_NAME (mdecl) = save;
22eed1e6
APB
8520 }
8521
c2952b01
APB
8522 /* The constructor body must be crafted by hand. It's the
8523 constructor we defined when we realize we didn't have the
8524 CLASSNAME() constructor */
22eed1e6
APB
8525 start_artificial_method_body (mdecl);
8526
f0f3a777
APB
8527 /* Insert an assignment to the this$<n> hidden field, if
8528 necessary */
8529 if ((thisn_assign = build_thisn_assign ()))
8530 java_method_add_stmt (mdecl, thisn_assign);
8531
22eed1e6
APB
8532 /* We don't generate a super constructor invocation if we're
8533 compiling java.lang.Object. build_super_invocation takes care
8534 of that. */
e920ebc9 8535 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8536
c2952b01
APB
8537 /* Insert the instance initializer block right here, after the
8538 super invocation. */
8539 add_instance_initializer (mdecl);
8540
22eed1e6
APB
8541 end_artificial_method_body (mdecl);
8542 }
8543 /* Search for an explicit constructor invocation */
8544 else
8545 {
8546 int found = 0;
8547 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8548
8549 while (body)
8550 switch (TREE_CODE (body))
8551 {
8552 case CALL_EXPR:
8553 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8554 body = NULL_TREE;
8555 break;
8556 case COMPOUND_EXPR:
8557 case EXPR_WITH_FILE_LOCATION:
8558 body = TREE_OPERAND (body, 0);
8559 break;
8560 case BLOCK:
8561 body = BLOCK_EXPR_BODY (body);
8562 break;
8563 default:
8564 found = 0;
8565 body = NULL_TREE;
8566 }
8567 /* The constructor is missing an invocation of super() */
8568 if (!found)
8569 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8570 build_super_invocation (mdecl));
22eed1e6 8571
c2952b01
APB
8572 /* Generate the assignment to this$<n>, if necessary */
8573 if ((thisn_assign = build_thisn_assign ()))
8574 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8575
f0f3a777
APB
8576 /* Insert the instance initializer block right here, after the
8577 super invocation. */
8578 add_instance_initializer (mdecl);
8579
22eed1e6
APB
8580 /* Fix the constructor main block if we're adding extra stmts */
8581 if (compound)
8582 {
8583 compound = add_stmt_to_compound (compound, NULL_TREE,
8584 BLOCK_EXPR_BODY (main_block));
8585 BLOCK_EXPR_BODY (main_block) = compound;
8586 }
8587 }
8588}
8589
8590/* Browse constructors in the super class, searching for a constructor
8591 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8592 otherwise. If the current class is an anonymous inner class, look
8593 for something that has the same signature. */
22eed1e6
APB
8594
8595static int
c2952b01
APB
8596verify_constructor_super (mdecl)
8597 tree mdecl;
22eed1e6
APB
8598{
8599 tree class = CLASSTYPE_SUPER (current_class);
152de068 8600 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8601 tree sdecl;
8602
22eed1e6
APB
8603 if (!class)
8604 return 0;
8605
c2952b01 8606 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8607 {
c2952b01
APB
8608 tree mdecl_arg_type;
8609 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8610 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8611 if (DECL_CONSTRUCTOR_P (sdecl))
8612 {
cf1b2274 8613 tree m_arg_type;
152de068
APB
8614 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8615 if (super_inner)
8616 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8617 for (m_arg_type = mdecl_arg_type;
8618 (arg_type != end_params_node
8619 && m_arg_type != end_params_node);
c2952b01 8620 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8621 m_arg_type = TREE_CHAIN (m_arg_type))
8622 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8623 break;
8624
cf1b2274 8625 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8626 return 0;
8627 }
8628 }
8629 else
8630 {
8631 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8632 {
152de068
APB
8633 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8634 if (super_inner)
8635 arg = TREE_CHAIN (arg);
8636 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8637 return 0;
8638 }
8639 }
8640 return 1;
8641}
8642
22eed1e6 8643/* Generate code for all context remembered for code generation. */
b351b287
APB
8644
8645void
8646java_expand_classes ()
8647{
5423609c 8648 int save_error_count = 0;
c2952b01
APB
8649 static struct parser_ctxt *saved_ctxp = NULL;
8650
23a79c61
APB
8651 java_parse_abort_on_error ();
8652 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8653 return;
8654 java_layout_classes ();
8655 java_parse_abort_on_error ();
8656
c2952b01 8657 saved_ctxp = ctxp_for_generation;
b351b287
APB
8658 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8659 {
8660 ctxp = ctxp_for_generation;
8661 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8662 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8663 java_parse_abort_on_error ();
8664 }
c2952b01
APB
8665
8666 /* Find anonymous classes and expand their constructor, now they
8667 have been fixed. */
8668 for (ctxp_for_generation = saved_ctxp;
8669 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8670 {
8671 tree current;
8672 ctxp = ctxp_for_generation;
8673 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8674 {
8675 current_class = TREE_TYPE (current);
8676 if (ANONYMOUS_CLASS_P (current_class))
8677 {
8678 tree d;
8679 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8680 {
8681 if (DECL_CONSTRUCTOR_P (d))
8682 {
8683 restore_line_number_status (1);
8684 reset_method_name (d);
8685 java_complete_expand_method (d);
8686 restore_line_number_status (0);
8687 break; /* We now there are no other ones */
8688 }
8689 }
8690 }
8691 }
8692 }
8693
8694 /* If we've found error at that stage, don't try to generate
8695 anything, unless we're emitting xrefs or checking the syntax only
8696 (but not using -fsyntax-only for the purpose of generating
8697 bytecode. */
8698 if (java_error_count && !flag_emit_xref
8699 && (!flag_syntax_only && !flag_emit_class_files))
8700 return;
8701
8702 /* Now things are stable, go for generation of the class data. */
8703 for (ctxp_for_generation = saved_ctxp;
8704 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8705 {
8706 tree current;
8707 ctxp = ctxp_for_generation;
8708 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8709 {
8710 current_class = TREE_TYPE (current);
8711 outgoing_cpool = TYPE_CPOOL (current_class);
8712 if (flag_emit_class_files)
8713 write_classfile (current_class);
8714 if (flag_emit_xref)
8715 expand_xref (current_class);
8716 else if (! flag_syntax_only)
8717 finish_class ();
8718 }
8719 }
b351b287
APB
8720}
8721
e04a16fb
AG
8722/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8723 a tree list node containing RIGHT. Fore coming RIGHTs will be
8724 chained to this hook. LOCATION contains the location of the
8725 separating `.' operator. */
8726
8727static tree
8728make_qualified_primary (primary, right, location)
8729 tree primary, right;
8730 int location;
8731{
8732 tree wfl;
8733
c2952b01 8734 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8735 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8736 else
8737 {
8738 wfl = primary;
c2952b01
APB
8739 /* If wfl wasn't qualified, we build a first anchor */
8740 if (!EXPR_WFL_QUALIFICATION (wfl))
8741 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8742 }
8743
c2952b01 8744 /* And chain them */
e04a16fb
AG
8745 EXPR_WFL_LINECOL (right) = location;
8746 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8747 PRIMARY_P (wfl) = 1;
8748 return wfl;
8749}
8750
8751/* Simple merge of two name separated by a `.' */
8752
8753static tree
8754merge_qualified_name (left, right)
8755 tree left, right;
8756{
8757 tree node;
c2952b01
APB
8758 if (!left && !right)
8759 return NULL_TREE;
8760
8761 if (!left)
8762 return right;
8763
8764 if (!right)
8765 return left;
8766
e04a16fb
AG
8767 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8768 IDENTIFIER_LENGTH (left));
8769 obstack_1grow (&temporary_obstack, '.');
8770 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8771 IDENTIFIER_LENGTH (right));
8772 node = get_identifier (obstack_base (&temporary_obstack));
8773 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8774 QUALIFIED_P (node) = 1;
8775 return node;
8776}
8777
8778/* Merge the two parts of a qualified name into LEFT. Set the
8779 location information of the resulting node to LOCATION, usually
8780 inherited from the location information of the `.' operator. */
8781
8782static tree
8783make_qualified_name (left, right, location)
8784 tree left, right;
8785 int location;
8786{
bc3ca41b
PB
8787#ifdef USE_COMPONENT_REF
8788 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8789 EXPR_WFL_LINECOL (node) = location;
8790 return node;
8791#else
e04a16fb
AG
8792 tree left_id = EXPR_WFL_NODE (left);
8793 tree right_id = EXPR_WFL_NODE (right);
8794 tree wfl, merge;
8795
8796 merge = merge_qualified_name (left_id, right_id);
8797
8798 /* Left wasn't qualified and is now qualified */
8799 if (!QUALIFIED_P (left_id))
8800 {
8801 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8802 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8803 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8804 }
8805
8806 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8807 EXPR_WFL_LINECOL (wfl) = location;
8808 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8809
8810 EXPR_WFL_NODE (left) = merge;
8811 return left;
bc3ca41b 8812#endif
e04a16fb
AG
8813}
8814
8815/* Extract the last identifier component of the qualified in WFL. The
8816 last identifier is removed from the linked list */
8817
8818static tree
8819cut_identifier_in_qualified (wfl)
8820 tree wfl;
8821{
8822 tree q;
8823 tree previous = NULL_TREE;
8824 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8825 if (!TREE_CHAIN (q))
8826 {
8827 if (!previous)
781b0558 8828 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8829 TREE_CHAIN (previous) = NULL_TREE;
8830 return TREE_PURPOSE (q);
8831 }
8832}
8833
8834/* Resolve the expression name NAME. Return its decl. */
8835
8836static tree
5e942c50 8837resolve_expression_name (id, orig)
e04a16fb 8838 tree id;
5e942c50 8839 tree *orig;
e04a16fb
AG
8840{
8841 tree name = EXPR_WFL_NODE (id);
8842 tree decl;
8843
8844 /* 6.5.5.1: Simple expression names */
8845 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8846 {
8847 /* 15.13.1: NAME can appear within the scope of a local variable
8848 declaration */
8849 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8850 return decl;
8851
8852 /* 15.13.1: NAME can appear within a class declaration */
8853 else
8854 {
8855 decl = lookup_field_wrapper (current_class, name);
8856 if (decl)
8857 {
c2952b01 8858 tree access = NULL_TREE;
e04a16fb 8859 int fs = FIELD_STATIC (decl);
f2760b27
APB
8860
8861 /* If we're accessing an outer scope local alias, make
8862 sure we change the name of the field we're going to
8863 build access to. */
8864 if (FIELD_LOCAL_ALIAS_USED (decl))
8865 name = DECL_NAME (decl);
8866
e04a16fb
AG
8867 /* Instance variable (8.3.1.1) can't appear within
8868 static method, static initializer or initializer for
8869 a static variable. */
8870 if (!fs && METHOD_STATIC (current_function_decl))
8871 {
7f10c2e2 8872 static_ref_err (id, name, current_class);
e04a16fb
AG
8873 return error_mark_node;
8874 }
22eed1e6
APB
8875 /* Instance variables can't appear as an argument of
8876 an explicit constructor invocation */
8877 if (!fs && ctxp->explicit_constructor_p)
8878 {
8879 parse_error_context
781b0558 8880 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8881 return error_mark_node;
8882 }
5e942c50 8883
c2952b01
APB
8884 /* If we're processing an inner class and we're trying
8885 to access a field belonging to an outer class, build
8886 the access to the field */
8887 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8888 {
8889 if (CLASS_STATIC (TYPE_NAME (current_class)))
8890 {
8891 static_ref_err (id, DECL_NAME (decl), current_class);
8892 return error_mark_node;
8893 }
8894 return build_outer_field_access (id, decl);
8895 }
c2952b01 8896
5e942c50 8897 /* Otherwise build what it takes to access the field */
c2952b01
APB
8898 access = build_field_ref ((fs ? NULL_TREE : current_this),
8899 DECL_CONTEXT (decl), name);
e8fc7396 8900 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8901 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8902 /* We may be asked to save the real field access node */
8903 if (orig)
c2952b01 8904 *orig = access;
5e942c50 8905 /* And we return what we got */
c2952b01 8906 return access;
e04a16fb
AG
8907 }
8908 /* Fall down to error report on undefined variable */
8909 }
8910 }
8911 /* 6.5.5.2 Qualified Expression Names */
8912 else
8913 {
5e942c50
APB
8914 if (orig)
8915 *orig = NULL_TREE;
e04a16fb
AG
8916 qualify_ambiguous_name (id);
8917 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8918 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8919 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8920 }
8921
8922 /* We've got an error here */
8923 parse_error_context (id, "Undefined variable `%s'",
8924 IDENTIFIER_POINTER (name));
8925
8926 return error_mark_node;
8927}
8928
7f10c2e2
APB
8929static void
8930static_ref_err (wfl, field_id, class_type)
8931 tree wfl, field_id, class_type;
8932{
8933 parse_error_context
8934 (wfl,
8935 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8936 IDENTIFIER_POINTER (field_id),
8937 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8938}
8939
e04a16fb
AG
8940/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8941 We return something suitable to generate the field access. We also
8942 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8943 recipient's address can be null. */
8944
8945static tree
8946resolve_field_access (qual_wfl, field_decl, field_type)
8947 tree qual_wfl;
8948 tree *field_decl, *field_type;
8949{
8950 int is_static = 0;
8951 tree field_ref;
8952 tree decl, where_found, type_found;
8953
8954 if (resolve_qualified_expression_name (qual_wfl, &decl,
8955 &where_found, &type_found))
8956 return error_mark_node;
8957
8958 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8959 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8960 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8961 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8962 {
8963 tree length = build_java_array_length_access (where_found);
8964 field_ref =
8965 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8966
8967 /* In case we're dealing with a static array, we need to
8968 initialize its class before the array length can be fetched.
8969 It's also a good time to create a DECL_RTL for the field if
8970 none already exists, otherwise if the field was declared in a
8971 class found in an external file and hasn't been (and won't
8972 be) accessed for its value, none will be created. */
8973 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8974 {
8975 build_static_field_ref (where_found);
8976 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8977 }
e04a16fb
AG
8978 }
8979 /* We might have been trying to resolve field.method(). In which
8980 case, the resolution is over and decl is the answer */
34f4db93 8981 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8982 field_ref = decl;
34f4db93 8983 else if (JDECL_P (decl))
e04a16fb 8984 {
5e942c50
APB
8985 int static_final_found = 0;
8986 if (!type_found)
8987 type_found = DECL_CONTEXT (decl);
34f4db93 8988 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8989 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8990 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8991 && DECL_INITIAL (decl))
5e942c50 8992 {
0c2b8145
APB
8993 /* When called on a FIELD_DECL of the right (primitive)
8994 type, java_complete_tree will try to substitue the decl
8995 for it's initial value. */
70541f45 8996 field_ref = java_complete_tree (decl);
5e942c50
APB
8997 static_final_found = 1;
8998 }
8999 else
7f10c2e2
APB
9000 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9001 NULL_TREE : where_found),
5e942c50 9002 type_found, DECL_NAME (decl));
e04a16fb
AG
9003 if (field_ref == error_mark_node)
9004 return error_mark_node;
e8fc7396
APB
9005 if (is_static && !static_final_found
9006 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 9007 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
9008 }
9009 else
9010 field_ref = decl;
9011
9012 if (field_decl)
9013 *field_decl = decl;
9014 if (field_type)
c877974e
APB
9015 *field_type = (QUAL_DECL_TYPE (decl) ?
9016 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
9017 return field_ref;
9018}
9019
e28cd97b
APB
9020/* If NODE is an access to f static field, strip out the class
9021 initialization part and return the field decl, otherwise, return
9022 NODE. */
9023
9024static tree
9025strip_out_static_field_access_decl (node)
9026 tree node;
9027{
9028 if (TREE_CODE (node) == COMPOUND_EXPR)
9029 {
9030 tree op1 = TREE_OPERAND (node, 1);
9031 if (TREE_CODE (op1) == COMPOUND_EXPR)
9032 {
9033 tree call = TREE_OPERAND (op1, 0);
9034 if (TREE_CODE (call) == CALL_EXPR
9035 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9036 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9037 == soft_initclass_node)
9038 return TREE_OPERAND (op1, 1);
9039 }
2f11d407
TT
9040 else if (JDECL_P (op1))
9041 return op1;
e28cd97b
APB
9042 }
9043 return node;
9044}
9045
e04a16fb
AG
9046/* 6.5.5.2: Qualified Expression Names */
9047
9048static int
9049resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9050 tree wfl;
9051 tree *found_decl, *type_found, *where_found;
9052{
9053 int from_type = 0; /* Field search initiated from a type */
c2952b01 9054 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9055 int previous_call_static = 0;
9056 int is_static;
9057 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9058 /* For certain for of inner class instantiation */
9059 tree saved_current, saved_this;
9060#define RESTORE_THIS_AND_CURRENT_CLASS \
9061 { current_class = saved_current; current_this = saved_this;}
9062
c877974e 9063 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9064
9065 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9066 {
9067 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9068 tree ret_decl; /* for EH checking */
9069 int location; /* for EH checking */
e04a16fb
AG
9070
9071 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9072 switch (TREE_CODE (qual_wfl))
9073 {
9074 case CALL_EXPR:
b67d701b 9075 case NEW_CLASS_EXPR:
e04a16fb
AG
9076 /* If the access to the function call is a non static field,
9077 build the code to access it. */
34f4db93 9078 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9079 {
ac825856
APB
9080 decl = maybe_access_field (decl, *where_found,
9081 DECL_CONTEXT (decl));
e04a16fb
AG
9082 if (decl == error_mark_node)
9083 return 1;
9084 }
c2952b01 9085
e04a16fb
AG
9086 /* And code for the function call */
9087 if (complete_function_arguments (qual_wfl))
9088 return 1;
c2952b01
APB
9089
9090 /* We might have to setup a new current class and a new this
9091 for the search of an inner class, relative to the type of
9092 a expression resolved as `decl'. The current values are
9093 saved and restored shortly after */
9094 saved_current = current_class;
9095 saved_this = current_this;
dba41d30
APB
9096 if (decl
9097 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9098 || from_qualified_this))
c2952b01 9099 {
dba41d30
APB
9100 /* If we still have `from_qualified_this', we have the form
9101 <T>.this.f() and we need to build <T>.this */
9102 if (from_qualified_this)
9103 {
9104 decl = build_access_to_thisn (current_class, type, 0);
9105 decl = java_complete_tree (decl);
9106 type = TREE_TYPE (TREE_TYPE (decl));
9107 }
c2952b01
APB
9108 current_class = type;
9109 current_this = decl;
dba41d30 9110 from_qualified_this = 0;
c2952b01
APB
9111 }
9112
89e09b9a
PB
9113 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9114 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9115 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9116 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9117 *where_found = patch_method_invocation (qual_wfl, decl, type,
9118 &is_static, &ret_decl);
e04a16fb 9119 if (*where_found == error_mark_node)
c2952b01
APB
9120 {
9121 RESTORE_THIS_AND_CURRENT_CLASS;
9122 return 1;
9123 }
e04a16fb
AG
9124 *type_found = type = QUAL_DECL_TYPE (*where_found);
9125
c2952b01
APB
9126 /* If we're creating an inner class instance, check for that
9127 an enclosing instance is in scope */
9128 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9129 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9130 {
9131 parse_error_context
165f37bc
APB
9132 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9133 lang_printable_name (type, 0),
9134 (!current_this ? "" :
9135 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9136 RESTORE_THIS_AND_CURRENT_CLASS;
9137 return 1;
9138 }
9139
9140 /* In case we had to change then to resolve a inner class
9141 instantiation using a primary qualified by a `new' */
9142 RESTORE_THIS_AND_CURRENT_CLASS;
9143
34d4df06
APB
9144 /* EH check. No check on access$<n> functions */
9145 if (location
9146 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9147 (DECL_NAME (current_function_decl)))
7705e9db
APB
9148 check_thrown_exceptions (location, ret_decl);
9149
e04a16fb
AG
9150 /* If the previous call was static and this one is too,
9151 build a compound expression to hold the two (because in
9152 that case, previous function calls aren't transported as
9153 forcoming function's argument. */
9154 if (previous_call_static && is_static)
9155 {
9156 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9157 TREE_SIDE_EFFECTS (decl) = 1;
9158 }
9159 else
9160 {
9161 previous_call_static = is_static;
9162 decl = *where_found;
9163 }
c2952b01 9164 from_type = 0;
e04a16fb
AG
9165 continue;
9166
d8fccff5 9167 case NEW_ARRAY_EXPR:
c2952b01 9168 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9169 *where_found = decl = java_complete_tree (qual_wfl);
9170 if (decl == error_mark_node)
9171 return 1;
9172 *type_found = type = QUAL_DECL_TYPE (decl);
9173 CLASS_LOADED_P (type) = 1;
9174 continue;
9175
e04a16fb
AG
9176 case CONVERT_EXPR:
9177 *where_found = decl = java_complete_tree (qual_wfl);
9178 if (decl == error_mark_node)
9179 return 1;
9180 *type_found = type = QUAL_DECL_TYPE (decl);
9181 from_cast = 1;
9182 continue;
9183
22eed1e6 9184 case CONDITIONAL_EXPR:
5e942c50 9185 case STRING_CST:
ac22f9cb 9186 case MODIFY_EXPR:
22eed1e6
APB
9187 *where_found = decl = java_complete_tree (qual_wfl);
9188 if (decl == error_mark_node)
9189 return 1;
9190 *type_found = type = QUAL_DECL_TYPE (decl);
9191 continue;
9192
e04a16fb
AG
9193 case ARRAY_REF:
9194 /* If the access to the function call is a non static field,
9195 build the code to access it. */
34f4db93 9196 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9197 {
9198 decl = maybe_access_field (decl, *where_found, type);
9199 if (decl == error_mark_node)
9200 return 1;
9201 }
9202 /* And code for the array reference expression */
9203 decl = java_complete_tree (qual_wfl);
9204 if (decl == error_mark_node)
9205 return 1;
9206 type = QUAL_DECL_TYPE (decl);
9207 continue;
0a2138e2 9208
37feda7d
APB
9209 case PLUS_EXPR:
9210 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9211 return 1;
9212 if ((type = patch_string (decl)))
9213 decl = type;
9214 *where_found = QUAL_RESOLUTION (q) = decl;
9215 *type_found = type = TREE_TYPE (decl);
9216 break;
9217
165f37bc
APB
9218 case CLASS_LITERAL:
9219 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9220 return 1;
9221 *where_found = QUAL_RESOLUTION (q) = decl;
9222 *type_found = type = TREE_TYPE (decl);
9223 break;
9224
0a2138e2
APB
9225 default:
9226 /* Fix for -Wall Just go to the next statement. Don't
9227 continue */
a3f406ce 9228 break;
e04a16fb
AG
9229 }
9230
9231 /* If we fall here, we weren't processing a (static) function call. */
9232 previous_call_static = 0;
9233
9234 /* It can be the keyword THIS */
9235 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9236 {
9237 if (!current_this)
9238 {
9239 parse_error_context
9240 (wfl, "Keyword `this' used outside allowed context");
9241 return 1;
9242 }
f63991a8
APB
9243 if (ctxp->explicit_constructor_p)
9244 {
781b0558 9245 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9246 return 1;
9247 }
e04a16fb 9248 /* We have to generate code for intermediate acess */
c2952b01
APB
9249 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9250 {
9251 *where_found = decl = current_this;
9252 *type_found = type = QUAL_DECL_TYPE (decl);
9253 }
4dbf4496
APB
9254 /* We're trying to access the this from somewhere else. Make sure
9255 it's allowed before doing so. */
c2952b01
APB
9256 else
9257 {
4dbf4496
APB
9258 if (!enclosing_context_p (type, current_class))
9259 {
9260 char *p = xstrdup (lang_printable_name (type, 0));
9261 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9262 p, p,
9263 lang_printable_name (current_class, 0));
9264 free (p);
9265 return 1;
9266 }
c2952b01 9267 from_qualified_this = 1;
dba41d30
APB
9268 /* If there's nothing else after that, we need to
9269 produce something now, otherwise, the section of the
9270 code that needs to produce <T>.this will generate
9271 what is necessary. */
9272 if (!TREE_CHAIN (q))
9273 {
9274 decl = build_access_to_thisn (current_class, type, 0);
9275 *where_found = decl = java_complete_tree (decl);
9276 *type_found = type = TREE_TYPE (decl);
9277 }
c2952b01
APB
9278 }
9279
9280 from_type = 0;
e04a16fb
AG
9281 continue;
9282 }
9283
9284 /* 15.10.2 Accessing Superclass Members using SUPER */
9285 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9286 {
9287 tree node;
9288 /* Check on the restricted use of SUPER */
9289 if (METHOD_STATIC (current_function_decl)
9290 || current_class == object_type_node)
9291 {
9292 parse_error_context
9293 (wfl, "Keyword `super' used outside allowed context");
9294 return 1;
9295 }
9296 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9297 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9298 CLASSTYPE_SUPER (current_class),
9299 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9300 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9301 if (decl == error_mark_node)
9302 return 1;
e04a16fb
AG
9303 *type_found = type = QUAL_DECL_TYPE (decl);
9304 from_super = from_type = 1;
9305 continue;
9306 }
9307
9308 /* 15.13.1: Can't search for field name in packages, so we
9309 assume a variable/class name was meant. */
9310 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9311 {
5e942c50
APB
9312 tree name = resolve_package (wfl, &q);
9313 if (name)
9314 {
c2952b01 9315 tree list;
5e942c50 9316 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9317 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9318 out. We're going to search something inside it. */
9319 *type_found = type = TREE_TYPE (decl);
9320 layout_class (type);
9321 from_type = 1;
c2952b01 9322
dde1da72
APB
9323 /* Fix them all the way down, if any are left. */
9324 if (q)
c2952b01 9325 {
dde1da72
APB
9326 list = TREE_CHAIN (q);
9327 while (list)
9328 {
9329 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9330 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9331 list = TREE_CHAIN (list);
9332 }
c2952b01 9333 }
5e942c50 9334 }
e04a16fb 9335 else
5e942c50
APB
9336 {
9337 if (from_super || from_cast)
9338 parse_error_context
9339 ((from_cast ? qual_wfl : wfl),
9340 "No variable `%s' defined in class `%s'",
9341 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9342 lang_printable_name (type, 0));
9343 else
9344 parse_error_context
9345 (qual_wfl, "Undefined variable or class name: `%s'",
9346 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9347 return 1;
9348 }
e04a16fb
AG
9349 }
9350
9351 /* We have a type name. It's been already resolved when the
9352 expression was qualified. */
9353 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9354 {
9355 if (!(decl = QUAL_RESOLUTION (q)))
9356 return 1; /* Error reported already */
9357
c2952b01
APB
9358 /* Sneak preview. If next we see a `new', we're facing a
9359 qualification with resulted in a type being selected
9360 instead of a field. Report the error */
9361 if(TREE_CHAIN (q)
9362 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9363 {
9364 parse_error_context (qual_wfl, "Undefined variable `%s'",
9365 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9366 return 1;
9367 }
9368
e04a16fb
AG
9369 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9370 {
9371 parse_error_context
9372 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9373 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9374 GET_TYPE_NAME (type),
e04a16fb
AG
9375 IDENTIFIER_POINTER (DECL_NAME (decl)),
9376 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9377 return 1;
9378 }
5e942c50 9379 check_deprecation (qual_wfl, decl);
c2952b01 9380
e04a16fb
AG
9381 type = TREE_TYPE (decl);
9382 from_type = 1;
9383 }
9384 /* We resolve and expression name */
9385 else
9386 {
cd531a2e 9387 tree field_decl = NULL_TREE;
e04a16fb
AG
9388
9389 /* If there exists an early resolution, use it. That occurs
9390 only once and we know that there are more things to
9391 come. Don't do that when processing something after SUPER
9392 (we need more thing to be put in place below */
9393 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9394 {
9395 decl = QUAL_RESOLUTION (q);
c877974e 9396 if (!type)
5e942c50 9397 {
7f10c2e2
APB
9398 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9399 {
9400 if (current_this)
9401 *where_found = current_this;
9402 else
9403 {
9404 static_ref_err (qual_wfl, DECL_NAME (decl),
9405 current_class);
9406 return 1;
9407 }
f0f3a777
APB
9408 if (outer_field_access_p (current_class, decl))
9409 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9410 }
c877974e
APB
9411 else
9412 {
9413 *where_found = TREE_TYPE (decl);
9414 if (TREE_CODE (*where_found) == POINTER_TYPE)
9415 *where_found = TREE_TYPE (*where_found);
9416 }
5e942c50 9417 }
b67d701b 9418 }
e04a16fb
AG
9419
9420 /* We have to search for a field, knowing the type of its
9421 container. The flag FROM_TYPE indicates that we resolved
9422 the last member of the expression as a type name, which
5e942c50
APB
9423 means that for the resolution of this field, we'll look
9424 for other errors than if it was resolved as a member of
9425 an other field. */
e04a16fb
AG
9426 else
9427 {
9428 int is_static;
5e942c50
APB
9429 tree field_decl_type; /* For layout */
9430
e04a16fb
AG
9431 if (!from_type && !JREFERENCE_TYPE_P (type))
9432 {
9433 parse_error_context
9434 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9435 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9436 lang_printable_name (type, 0),
e04a16fb
AG
9437 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9438 return 1;
9439 }
9440
dc0b3eff
PB
9441 field_decl = lookup_field_wrapper (type,
9442 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9443
9444 /* Maybe what we're trying to access an inner class. */
9445 if (!field_decl)
9446 {
9447 tree ptr, inner_decl;
9448
9449 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9450 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9451 if (inner_decl)
9452 {
9453 check_inner_class_access (inner_decl, decl, qual_wfl);
9454 type = TREE_TYPE (inner_decl);
9455 decl = inner_decl;
9456 from_type = 1;
9457 continue;
9458 }
9459 }
9460
dc0b3eff 9461 if (field_decl == NULL_TREE)
e04a16fb
AG
9462 {
9463 parse_error_context
2aa11e97 9464 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9465 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9466 GET_TYPE_NAME (type));
e04a16fb
AG
9467 return 1;
9468 }
dc0b3eff
PB
9469 if (field_decl == error_mark_node)
9470 return 1;
5e942c50
APB
9471
9472 /* Layout the type of field_decl, since we may need
c877974e
APB
9473 it. Don't do primitive types or loaded classes. The
9474 situation of non primitive arrays may not handled
9475 properly here. FIXME */
5e942c50
APB
9476 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9477 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9478 else
9479 field_decl_type = TREE_TYPE (field_decl);
9480 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9481 && !CLASS_LOADED_P (field_decl_type)
9482 && !TYPE_ARRAY_P (field_decl_type))
9483 resolve_and_layout (field_decl_type, NULL_TREE);
9484 if (TYPE_ARRAY_P (field_decl_type))
9485 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9486
9487 /* Check on accessibility here */
9488 if (not_accessible_p (type, field_decl, from_super))
9489 {
9490 parse_error_context
9491 (qual_wfl,
9492 "Can't access %s field `%s.%s' from `%s'",
9493 java_accstring_lookup
9494 (get_access_flags_from_decl (field_decl)),
2aa11e97 9495 GET_TYPE_NAME (type),
e04a16fb
AG
9496 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9497 IDENTIFIER_POINTER
9498 (DECL_NAME (TYPE_NAME (current_class))));
9499 return 1;
9500 }
5e942c50 9501 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9502
9503 /* There are things to check when fields are accessed
9504 from type. There are no restrictions on a static
9505 declaration of the field when it is accessed from an
9506 interface */
9507 is_static = FIELD_STATIC (field_decl);
9508 if (!from_super && from_type
c2952b01
APB
9509 && !TYPE_INTERFACE_P (type)
9510 && !is_static
9511 && (current_function_decl
9512 && METHOD_STATIC (current_function_decl)))
e04a16fb 9513 {
7f10c2e2 9514 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9515 return 1;
9516 }
9517 from_cast = from_super = 0;
9518
c2952b01
APB
9519 /* It's an access from a type but it isn't static, we
9520 make it relative to `this'. */
9521 if (!is_static && from_type)
9522 decl = current_this;
9523
5e942c50
APB
9524 /* If we need to generate something to get a proper
9525 handle on what this field is accessed from, do it
9526 now. */
e04a16fb
AG
9527 if (!is_static)
9528 {
c583dd46 9529 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9530 if (decl == error_mark_node)
9531 return 1;
9532 }
9533
9534 /* We want to keep the location were found it, and the type
9535 we found. */
9536 *where_found = decl;
9537 *type_found = type;
9538
c2952b01
APB
9539 /* Generate the correct expression for field access from
9540 qualified this */
9541 if (from_qualified_this)
9542 {
9543 field_decl = build_outer_field_access (qual_wfl, field_decl);
9544 from_qualified_this = 0;
9545 }
9546
e04a16fb
AG
9547 /* This is the decl found and eventually the next one to
9548 search from */
9549 decl = field_decl;
9550 }
e04a16fb
AG
9551 from_type = 0;
9552 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9553
9554 /* Sneak preview. If decl is qualified by a `new', report
9555 the error here to be accurate on the peculiar construct */
9556 if (TREE_CHAIN (q)
9557 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9558 && !JREFERENCE_TYPE_P (type))
9559 {
9560 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9561 lang_printable_name (type, 0));
9562 return 1;
9563 }
e04a16fb 9564 }
dde1da72
APB
9565 /* `q' might have changed due to a after package resolution
9566 re-qualification */
9567 if (!q)
9568 break;
e04a16fb
AG
9569 }
9570 *found_decl = decl;
9571 return 0;
9572}
9573
9574/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9575 can't be accessed from REFERENCE (a record type). This should be
9576 used when decl is a field or a method.*/
e04a16fb 9577
be245ac0
KG
9578static int
9579not_accessible_p (reference, member, from_super)
e04a16fb
AG
9580 tree reference, member;
9581 int from_super;
9582{
9583 int access_flag = get_access_flags_from_decl (member);
9584
4dbf4496
APB
9585 /* Inner classes are processed by check_inner_class_access */
9586 if (INNER_CLASS_TYPE_P (reference))
9587 return 0;
9588
e04a16fb
AG
9589 /* Access always granted for members declared public */
9590 if (access_flag & ACC_PUBLIC)
9591 return 0;
9592
9593 /* Check access on protected members */
9594 if (access_flag & ACC_PROTECTED)
9595 {
9596 /* Access granted if it occurs from within the package
9597 containing the class in which the protected member is
9598 declared */
9599 if (class_in_current_package (DECL_CONTEXT (member)))
9600 return 0;
9601
9bbc7d9f
PB
9602 /* If accessed with the form `super.member', then access is granted */
9603 if (from_super)
9604 return 0;
e04a16fb 9605
9bbc7d9f 9606 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9607 member is declared or a subclass of it. Find the right
9608 context to perform the check */
9609 if (PURE_INNER_CLASS_TYPE_P (reference))
9610 {
9611 while (INNER_CLASS_TYPE_P (reference))
9612 {
9613 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9614 return 0;
9615 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9616 }
9617 }
473e7b07 9618 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9619 return 0;
e04a16fb
AG
9620 return 1;
9621 }
9622
9623 /* Check access on private members. Access is granted only if it
473e7b07 9624 occurs from within the class in which it is declared. Exceptions
4dbf4496 9625 are accesses from inner-classes. */
e04a16fb 9626 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9627 return (current_class == DECL_CONTEXT (member) ? 0 :
9628 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9629
9630 /* Default access are permitted only when occuring within the
9631 package in which the type (REFERENCE) is declared. In other words,
9632 REFERENCE is defined in the current package */
9633 if (ctxp->package)
9634 return !class_in_current_package (reference);
473e7b07 9635
e04a16fb
AG
9636 /* Otherwise, access is granted */
9637 return 0;
9638}
9639
5e942c50
APB
9640/* Test deprecated decl access. */
9641static void
9642check_deprecation (wfl, decl)
9643 tree wfl, decl;
9644{
49f48c71 9645 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9646 /* Complain if the field is deprecated and the file it was defined
9647 in isn't compiled at the same time the file which contains its
9648 use is */
9649 if (DECL_DEPRECATED (decl)
9650 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9651 {
9652 char the [20];
9653 switch (TREE_CODE (decl))
9654 {
9655 case FUNCTION_DECL:
9656 strcpy (the, "method");
9657 break;
9658 case FIELD_DECL:
9659 strcpy (the, "field");
9660 break;
9661 case TYPE_DECL:
9662 strcpy (the, "class");
9663 break;
15fdcfe9
PB
9664 default:
9665 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9666 }
9667 parse_warning_context
9668 (wfl, "The %s `%s' in class `%s' has been deprecated",
9669 the, lang_printable_name (decl, 0),
9670 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9671 }
9672}
9673
e04a16fb
AG
9674/* Returns 1 if class was declared in the current package, 0 otherwise */
9675
9676static int
9677class_in_current_package (class)
9678 tree class;
9679{
9680 static tree cache = NULL_TREE;
9681 int qualified_flag;
9682 tree left;
9683
9684 if (cache == class)
9685 return 1;
9686
9687 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9688
9689 /* If the current package is empty and the name of CLASS is
9690 qualified, class isn't in the current package. If there is a
9691 current package and the name of the CLASS is not qualified, class
9692 isn't in the current package */
0a2138e2 9693 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9694 return 0;
9695
9696 /* If there is not package and the name of CLASS isn't qualified,
9697 they belong to the same unnamed package */
9698 if (!ctxp->package && !qualified_flag)
9699 return 1;
9700
9701 /* Compare the left part of the name of CLASS with the package name */
9702 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9703 if (ctxp->package == left)
9704 {
19e223db
MM
9705 static int initialized_p;
9706 /* Register CACHE with the garbage collector. */
9707 if (!initialized_p)
9708 {
9709 ggc_add_tree_root (&cache, 1);
9710 initialized_p = 1;
9711 }
9712
e04a16fb
AG
9713 cache = class;
9714 return 1;
9715 }
9716 return 0;
9717}
9718
9719/* This function may generate code to access DECL from WHERE. This is
9720 done only if certain conditions meet. */
9721
9722static tree
9723maybe_access_field (decl, where, type)
9724 tree decl, where, type;
9725{
5e942c50
APB
9726 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9727 && !FIELD_STATIC (decl))
e04a16fb 9728 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9729 (type ? type : DECL_CONTEXT (decl)),
9730 DECL_NAME (decl));
e04a16fb
AG
9731 return decl;
9732}
9733
15fdcfe9 9734/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9735 and according to the situation, PRIMARY and WHERE may be
9736 used. IS_STATIC is set to 1 if the invoked function is static. */
9737
9738static tree
89e09b9a 9739patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9740 tree patch, primary, where;
9741 int *is_static;
b9f7e36c 9742 tree *ret_decl;
e04a16fb
AG
9743{
9744 tree wfl = TREE_OPERAND (patch, 0);
9745 tree args = TREE_OPERAND (patch, 1);
9746 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9747 tree list;
22eed1e6 9748 int is_static_flag = 0;
89e09b9a 9749 int is_super_init = 0;
bccaf73a 9750 tree this_arg = NULL_TREE;
e04a16fb
AG
9751
9752 /* Should be overriden if everything goes well. Otherwise, if
9753 something fails, it should keep this value. It stop the
9754 evaluation of a bogus assignment. See java_complete_tree,
9755 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9756 evaluating an assignment */
9757 TREE_TYPE (patch) = error_mark_node;
9758
9759 /* Since lookup functions are messing with line numbers, save the
9760 context now. */
9761 java_parser_context_save_global ();
9762
9763 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9764
9765 /* Resolution of qualified name, excluding constructors */
9766 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9767 {
dde1da72 9768 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9769 /* Extract the last IDENTIFIER of the qualified
9770 expression. This is a wfl and we will use it's location
9771 data during error report. */
9772 identifier_wfl = cut_identifier_in_qualified (wfl);
9773 identifier = EXPR_WFL_NODE (identifier_wfl);
9774
9775 /* Given the context, IDENTIFIER is syntactically qualified
9776 as a MethodName. We need to qualify what's before */
9777 qualify_ambiguous_name (wfl);
dde1da72 9778 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9779
dde1da72
APB
9780 if (resolved == error_mark_node)
9781 PATCH_METHOD_RETURN_ERROR ();
9782
9783 type = GET_SKIP_TYPE (resolved);
9784 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9785
9786 if (JPRIMITIVE_TYPE_P (type))
9787 {
7e51098e
TT
9788 parse_error_context
9789 (identifier_wfl,
9790 "Can't invoke a method on primitive type `%s'",
9791 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9792 PATCH_METHOD_RETURN_ERROR ();
9793 }
9794
dde1da72
APB
9795 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9796 args = nreverse (args);
2c56429a 9797
e04a16fb 9798 /* We're resolving a call from a type */
dde1da72 9799 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9800 {
dde1da72 9801 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9802 {
9803 parse_error_context
781b0558
KG
9804 (identifier_wfl,
9805 "Can't make static reference to method `%s' in interface `%s'",
9806 IDENTIFIER_POINTER (identifier),
e04a16fb 9807 IDENTIFIER_POINTER (name));
b9f7e36c 9808 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9809 }
e04a16fb
AG
9810 if (list && !METHOD_STATIC (list))
9811 {
c2e3db92 9812 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9813 parse_error_context
9814 (identifier_wfl,
9815 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9816 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9817 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9818 free (fct_name);
b9f7e36c 9819 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9820 }
9821 }
e04a16fb 9822 else
dde1da72
APB
9823 this_arg = primary = resolved;
9824
5e942c50 9825 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9826 wfl = identifier_wfl;
9827 }
9828 /* Resolution of simple names, names generated after a primary: or
9829 constructors */
9830 else
9831 {
cd531a2e 9832 tree class_to_search = NULL_TREE;
c2952b01 9833 int lc; /* Looking for Constructor */
e04a16fb
AG
9834
9835 /* We search constructor in their target class */
9836 if (CALL_CONSTRUCTOR_P (patch))
9837 {
22eed1e6
APB
9838 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9839 class_to_search = EXPR_WFL_NODE (wfl);
9840 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9841 this_identifier_node)
9842 class_to_search = NULL_TREE;
9843 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9844 super_identifier_node)
e04a16fb 9845 {
89e09b9a 9846 is_super_init = 1;
22eed1e6
APB
9847 if (CLASSTYPE_SUPER (current_class))
9848 class_to_search =
9849 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9850 else
9851 {
781b0558 9852 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9853 PATCH_METHOD_RETURN_ERROR ();
9854 }
e04a16fb 9855 }
22eed1e6
APB
9856
9857 /* Class to search is NULL if we're searching the current one */
9858 if (class_to_search)
e04a16fb 9859 {
c2952b01
APB
9860 class_to_search = resolve_and_layout (class_to_search, wfl);
9861
22eed1e6
APB
9862 if (!class_to_search)
9863 {
9864 parse_error_context
9865 (wfl, "Class `%s' not found in type declaration",
9866 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9867 PATCH_METHOD_RETURN_ERROR ();
9868 }
9869
5e942c50
APB
9870 /* Can't instantiate an abstract class, but we can
9871 invoke it's constructor. It's use within the `new'
9872 context is denied here. */
9873 if (CLASS_ABSTRACT (class_to_search)
9874 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9875 {
9876 parse_error_context
781b0558
KG
9877 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9878 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9879 PATCH_METHOD_RETURN_ERROR ();
9880 }
c2952b01 9881
22eed1e6 9882 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9883 }
22eed1e6
APB
9884 else
9885 class_to_search = current_class;
e04a16fb
AG
9886 lc = 1;
9887 }
9888 /* This is a regular search in the local class, unless an
9889 alternate class is specified. */
9890 else
9891 {
9892 class_to_search = (where ? where : current_class);
9893 lc = 0;
9894 }
c2952b01 9895
e04a16fb
AG
9896 /* NAME is a simple identifier or comes from a primary. Search
9897 in the class whose declaration contain the method being
9898 invoked. */
c877974e 9899 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9900
c2952b01 9901 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9902 /* Don't continue if no method were found, as the next statement
9903 can't be executed then. */
b9f7e36c
APB
9904 if (!list)
9905 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9906
9907 /* Check for static reference if non static methods */
9908 if (check_for_static_method_reference (wfl, patch, list,
9909 class_to_search, primary))
b9f7e36c 9910 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9911
165f37bc
APB
9912 /* Check for inner classes creation from illegal contexts */
9913 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9914 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9915 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9916 {
9917 parse_error_context
9918 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9919 lang_printable_name (class_to_search, 0),
9920 (!current_this ? "" :
9921 "; an explicit one must be provided when creating this inner class"));
9922 PATCH_METHOD_RETURN_ERROR ();
9923 }
9924
22eed1e6
APB
9925 /* Non static methods are called with the current object extra
9926 argument. If patch a `new TYPE()', the argument is the value
9927 returned by the object allocator. If method is resolved as a
9928 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9929 args = nreverse (args);
bccaf73a 9930 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9931 {
9932 this_arg = primary ? primary : current_this;
9933
9934 /* If we're using an access method, things are different.
9935 There are two familly of cases:
9936
9937 1) We're not generating bytecodes:
9938
9939 - LIST is non static. It's invocation is transformed from
9940 x(a1,...,an) into this$<n>.x(a1,....an).
9941 - LIST is static. It's invocation is transformed from
9942 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9943
9944 2) We're generating bytecodes:
9945
9946 - LIST is non static. It's invocation is transformed from
9947 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9948 - LIST is static. It's invocation is transformed from
9949 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9950
9951 Of course, this$<n> can be abitrary complex, ranging from
9952 this$0 (the immediate outer context) to
9953 access$0(access$0(...(this$0))).
9954
9955 maybe_use_access_method returns a non zero value if the
dfb99c83 9956 this_arg has to be moved into the (then generated) stub
4dbf4496 9957 argument list. In the meantime, the selected function
dfb99c83 9958 might have be replaced by a generated stub. */
c2952b01 9959 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9960 {
9961 args = tree_cons (NULL_TREE, this_arg, args);
9962 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9963 }
c2952b01 9964 }
e04a16fb 9965 }
b67d701b 9966
e04a16fb
AG
9967 /* Merge point of all resolution schemes. If we have nothing, this
9968 is an error, already signaled */
b9f7e36c
APB
9969 if (!list)
9970 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9971
e04a16fb
AG
9972 /* Check accessibility, position the is_static flag, build and
9973 return the call */
9bbc7d9f 9974 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9975 {
c2e3db92 9976 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9977 parse_error_context
9978 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9979 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9980 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9981 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9982 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9983 free (fct_name);
b9f7e36c 9984 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9985 }
5e942c50 9986 check_deprecation (wfl, list);
22eed1e6 9987
c2952b01
APB
9988 /* If invoking a innerclass constructor, there are hidden parameters
9989 to pass */
9990 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9991 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9992 {
9993 /* And make sure we add the accessed local variables to be saved
9994 in field aliases. */
9995 args = build_alias_initializer_parameter_list
9996 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9997
da632f2c 9998 /* Secretly pass the current_this/primary as a second argument */
165f37bc 9999 if (primary || current_this)
f8b93ea7
APB
10000 {
10001 tree extra_arg;
10002 tree this_type = (current_this ?
10003 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10004 /* Method's (list) enclosing context */
10005 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10006 /* If we have a primary, use it. */
10007 if (primary)
10008 extra_arg = primary;
10009 /* The current `this' is an inner class but isn't a direct
10010 enclosing context for the inner class we're trying to
10011 create. Build an access to the proper enclosing context
10012 and use it. */
10013 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10014 && this_type != TREE_TYPE (mec))
10015 {
10016
10017 extra_arg = build_access_to_thisn (current_class,
10018 TREE_TYPE (mec), 0);
10019 extra_arg = java_complete_tree (extra_arg);
10020 }
10021 /* Otherwise, just use the current `this' as an enclosing
10022 context. */
10023 else
10024 extra_arg = current_this;
10025 args = tree_cons (NULL_TREE, extra_arg, args);
10026 }
165f37bc
APB
10027 else
10028 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10029 }
10030
152de068
APB
10031 /* This handles the situation where a constructor invocation needs
10032 to have an enclosing context passed as a second parameter (the
10033 constructor is one of an inner class. We extract it from the
10034 current function. */
10035 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10036 {
10037 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10038 tree extra_arg;
10039
10040 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10041 {
10042 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10043 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10044 }
10045 else
10046 {
10047 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10048 extra_arg =
10049 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10050 extra_arg = java_complete_tree (extra_arg);
10051 }
10052 args = tree_cons (NULL_TREE, extra_arg, args);
10053 }
10054
22eed1e6 10055 is_static_flag = METHOD_STATIC (list);
dba41d30 10056 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10057 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10058
c3f2a476
APB
10059 /* In the context of an explicit constructor invocation, we can't
10060 invoke any method relying on `this'. Exceptions are: we're
10061 invoking a static function, primary exists and is not the current
10062 this, we're creating a new object. */
22eed1e6 10063 if (ctxp->explicit_constructor_p
c3f2a476
APB
10064 && !is_static_flag
10065 && (!primary || primary == current_this)
10066 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10067 {
781b0558 10068 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10069 PATCH_METHOD_RETURN_ERROR ();
10070 }
e04a16fb 10071 java_parser_context_restore_global ();
22eed1e6
APB
10072 if (is_static)
10073 *is_static = is_static_flag;
b9f7e36c
APB
10074 /* Sometimes, we want the decl of the selected method. Such as for
10075 EH checking */
10076 if (ret_decl)
10077 *ret_decl = list;
89e09b9a
PB
10078 patch = patch_invoke (patch, list, args);
10079 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10080 {
c2952b01
APB
10081 tree finit_parms, finit_call;
10082
c00f0fb2 10083 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10084 finit_parms = build_alias_initializer_parameter_list
10085 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10086
c2952b01
APB
10087 finit_call =
10088 build_method_invocation (build_wfl_node (finit_identifier_node),
10089 finit_parms);
10090
10091 /* Generate the code used to initialize fields declared with an
10092 initialization statement and build a compound statement along
10093 with the super constructor invocation. */
89e09b9a
PB
10094 patch = build (COMPOUND_EXPR, void_type_node, patch,
10095 java_complete_tree (finit_call));
10096 CAN_COMPLETE_NORMALLY (patch) = 1;
10097 }
10098 return patch;
e04a16fb
AG
10099}
10100
10101/* Check that we're not trying to do a static reference to a method in
10102 non static method. Return 1 if it's the case, 0 otherwise. */
10103
10104static int
10105check_for_static_method_reference (wfl, node, method, where, primary)
10106 tree wfl, node, method, where, primary;
10107{
10108 if (METHOD_STATIC (current_function_decl)
10109 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10110 {
c2e3db92 10111 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10112 parse_error_context
10113 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10114 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10115 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10116 free (fct_name);
10117 return 1;
10118 }
10119 return 0;
10120}
10121
c2952b01
APB
10122/* Fix the invocation of *MDECL if necessary in the case of a
10123 invocation from an inner class. *THIS_ARG might be modified
10124 appropriately and an alternative access to *MDECL might be
10125 returned. */
10126
10127static int
10128maybe_use_access_method (is_super_init, mdecl, this_arg)
10129 int is_super_init;
10130 tree *mdecl, *this_arg;
10131{
10132 tree ctx;
10133 tree md = *mdecl, ta = *this_arg;
10134 int to_return = 0;
10135 int non_static_context = !METHOD_STATIC (md);
10136
10137 if (is_super_init
165f37bc
APB
10138 || DECL_CONTEXT (md) == current_class
10139 || !PURE_INNER_CLASS_TYPE_P (current_class)
10140 || DECL_FINIT_P (md))
c2952b01
APB
10141 return 0;
10142
10143 /* If we're calling a method found in an enclosing class, generate
10144 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10145 invoking a static method. Note that if MD's type is unrelated to
10146 CURRENT_CLASS, then the current this can be used. */
c2952b01 10147
2cb3951d 10148 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10149 {
10150 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10151 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10152 {
10153 ta = build_current_thisn (current_class);
10154 ta = build_wfl_node (ta);
10155 }
10156 else
10157 {
10158 tree type = ctx;
10159 while (type)
10160 {
10161 maybe_build_thisn_access_method (type);
4dbf4496 10162 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10163 {
10164 ta = build_access_to_thisn (ctx, type, 0);
10165 break;
10166 }
10167 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10168 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10169 }
10170 }
10171 ta = java_complete_tree (ta);
10172 }
10173
10174 /* We might have to use an access method to get to MD. We can
10175 break the method access rule as far as we're not generating
10176 bytecode */
10177 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10178 {
10179 md = build_outer_method_access_method (md);
10180 to_return = 1;
10181 }
10182
10183 *mdecl = md;
10184 *this_arg = ta;
10185
10186 /* Returnin a non zero value indicates we were doing a non static
10187 method invokation that is now a static invocation. It will have
10188 callee displace `this' to insert it in the regular argument
10189 list. */
10190 return (non_static_context && to_return);
10191}
10192
e04a16fb
AG
10193/* Patch an invoke expression METHOD and ARGS, based on its invocation
10194 mode. */
10195
10196static tree
89e09b9a 10197patch_invoke (patch, method, args)
e04a16fb 10198 tree patch, method, args;
e04a16fb
AG
10199{
10200 tree dtable, func;
0a2138e2 10201 tree original_call, t, ta;
e815887f 10202 tree cond = NULL_TREE;
e04a16fb 10203
5e942c50
APB
10204 /* Last step for args: convert build-in types. If we're dealing with
10205 a new TYPE() type call, the first argument to the constructor
e815887f 10206 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10207 `new' */
10208 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10209 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10210 t = TREE_CHAIN (t);
ac825856
APB
10211 for (ta = args; t != end_params_node && ta;
10212 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10213 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10214 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10215 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10216
10217 /* Resolve unresolved returned type isses */
10218 t = TREE_TYPE (TREE_TYPE (method));
10219 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10220 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10221
e8fc7396 10222 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10223 func = method;
10224 else
e04a16fb 10225 {
15fdcfe9 10226 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10227 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10228 {
10229 case INVOKE_VIRTUAL:
10230 dtable = invoke_build_dtable (0, args);
10231 func = build_invokevirtual (dtable, method);
10232 break;
b9f7e36c 10233
e815887f
TT
10234 case INVOKE_NONVIRTUAL:
10235 /* If the object for the method call is null, we throw an
10236 exception. We don't do this if the object is the current
10237 method's `this'. In other cases we just rely on an
10238 optimization pass to eliminate redundant checks. */
10239 if (TREE_VALUE (args) != current_this)
10240 {
10241 /* We use a SAVE_EXPR here to make sure we only evaluate
10242 the new `self' expression once. */
10243 tree save_arg = save_expr (TREE_VALUE (args));
10244 TREE_VALUE (args) = save_arg;
10245 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10246 null_pointer_node);
10247 }
10248 /* Fall through. */
10249
15fdcfe9
PB
10250 case INVOKE_SUPER:
10251 case INVOKE_STATIC:
10252 func = build_known_method_ref (method, TREE_TYPE (method),
10253 DECL_CONTEXT (method),
10254 signature, args);
10255 break;
e04a16fb 10256
15fdcfe9
PB
10257 case INVOKE_INTERFACE:
10258 dtable = invoke_build_dtable (1, args);
173f556c 10259 func = build_invokeinterface (dtable, method);
15fdcfe9 10260 break;
5e942c50 10261
15fdcfe9 10262 default:
89e09b9a 10263 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10264 }
10265
10266 /* Ensure self_type is initialized, (invokestatic). FIXME */
10267 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10268 }
10269
e04a16fb
AG
10270 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10271 TREE_OPERAND (patch, 0) = func;
10272 TREE_OPERAND (patch, 1) = args;
10273 original_call = patch;
10274
e815887f 10275 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10276 returned value is the first argument to the constructor. We build
10277 a COMPOUND_EXPR and use saved expression so that the overall NEW
10278 expression value is a pointer to a newly created and initialized
10279 class. */
10280 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10281 {
10282 tree class = DECL_CONTEXT (method);
10283 tree c1, saved_new, size, new;
e8fc7396 10284 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10285 {
10286 TREE_TYPE (patch) = build_pointer_type (class);
10287 return patch;
10288 }
e04a16fb
AG
10289 if (!TYPE_SIZE (class))
10290 safe_layout_class (class);
10291 size = size_in_bytes (class);
10292 new = build (CALL_EXPR, promote_type (class),
10293 build_address_of (alloc_object_node),
10294 tree_cons (NULL_TREE, build_class_ref (class),
10295 build_tree_list (NULL_TREE,
10296 size_in_bytes (class))),
10297 NULL_TREE);
10298 saved_new = save_expr (new);
10299 c1 = build_tree_list (NULL_TREE, saved_new);
10300 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10301 TREE_OPERAND (original_call, 1) = c1;
10302 TREE_SET_CODE (original_call, CALL_EXPR);
10303 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10304 }
e815887f
TT
10305
10306 /* If COND is set, then we are building a check to see if the object
10307 is NULL. */
10308 if (cond != NULL_TREE)
10309 {
10310 /* We have to make the `then' branch a compound expression to
10311 make the types turn out right. This seems bizarre. */
10312 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10313 build (COMPOUND_EXPR, TREE_TYPE (patch),
10314 build (CALL_EXPR, void_type_node,
10315 build_address_of (soft_nullpointer_node),
10316 NULL_TREE, NULL_TREE),
10317 (FLOAT_TYPE_P (TREE_TYPE (patch))
10318 ? build_real (TREE_TYPE (patch), dconst0)
10319 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10320 integer_zero_node))),
10321 patch);
10322 TREE_SIDE_EFFECTS (patch) = 1;
10323 }
10324
e04a16fb
AG
10325 return patch;
10326}
10327
10328static int
10329invocation_mode (method, super)
10330 tree method;
10331 int super;
10332{
10333 int access = get_access_flags_from_decl (method);
10334
22eed1e6
APB
10335 if (super)
10336 return INVOKE_SUPER;
10337
e815887f 10338 if (access & ACC_STATIC)
e04a16fb
AG
10339 return INVOKE_STATIC;
10340
e815887f
TT
10341 /* We have to look for a constructor before we handle nonvirtual
10342 calls; otherwise the constructor will look nonvirtual. */
10343 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10344 return INVOKE_STATIC;
e815887f
TT
10345
10346 if (access & ACC_FINAL || access & ACC_PRIVATE)
10347 return INVOKE_NONVIRTUAL;
10348
10349 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10350 return INVOKE_NONVIRTUAL;
10351
e04a16fb
AG
10352 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10353 return INVOKE_INTERFACE;
22eed1e6 10354
e04a16fb
AG
10355 return INVOKE_VIRTUAL;
10356}
10357
b67d701b
PB
10358/* Retrieve a refined list of matching methods. It covers the step
10359 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10360
10361static tree
10362lookup_method_invoke (lc, cl, class, name, arg_list)
10363 int lc;
10364 tree cl;
10365 tree class, name, arg_list;
10366{
de4c7b02 10367 tree atl = end_params_node; /* Arg Type List */
c877974e 10368 tree method, signature, list, node;
49f48c71 10369 const char *candidates; /* Used for error report */
b5b8a0e7 10370 char *dup;
e04a16fb 10371
5e942c50 10372 /* Fix the arguments */
e04a16fb
AG
10373 for (node = arg_list; node; node = TREE_CHAIN (node))
10374 {
e3884b71 10375 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10376 /* Non primitive type may have to be resolved */
e3884b71 10377 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10378 resolve_and_layout (current_arg, NULL_TREE);
10379 /* And promoted */
b67d701b 10380 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10381 current_arg = promote_type (current_arg);
5e942c50 10382 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10383 }
e04a16fb 10384
c2952b01
APB
10385 /* Presto. If we're dealing with an anonymous class and a
10386 constructor call, generate the right constructor now, since we
10387 know the arguments' types. */
10388
10389 if (lc && ANONYMOUS_CLASS_P (class))
10390 craft_constructor (TYPE_NAME (class), atl);
10391
5e942c50
APB
10392 /* Find all candidates and then refine the list, searching for the
10393 most specific method. */
10394 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10395 list = find_most_specific_methods_list (list);
b67d701b
PB
10396 if (list && !TREE_CHAIN (list))
10397 return TREE_VALUE (list);
e04a16fb 10398
b67d701b
PB
10399 /* Issue an error. List candidates if any. Candidates are listed
10400 only if accessible (non accessible methods may end-up here for
10401 the sake of a better error report). */
10402 candidates = NULL;
10403 if (list)
e04a16fb 10404 {
e04a16fb 10405 tree current;
b67d701b 10406 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10407 for (current = list; current; current = TREE_CHAIN (current))
10408 {
b67d701b
PB
10409 tree cm = TREE_VALUE (current);
10410 char string [4096];
10411 if (!cm || not_accessible_p (class, cm, 0))
10412 continue;
b67d701b 10413 sprintf
22eed1e6
APB
10414 (string, " `%s' in `%s'%s",
10415 get_printable_method_name (cm),
b67d701b
PB
10416 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10417 (TREE_CHAIN (current) ? "\n" : ""));
10418 obstack_grow (&temporary_obstack, string, strlen (string));
10419 }
10420 obstack_1grow (&temporary_obstack, '\0');
10421 candidates = obstack_finish (&temporary_obstack);
10422 }
10423 /* Issue the error message */
c877974e
APB
10424 method = make_node (FUNCTION_TYPE);
10425 TYPE_ARG_TYPES (method) = atl;
b67d701b 10426 signature = build_java_argument_signature (method);
c63b98cd 10427 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10428 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10429 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10430 (lc ? dup : IDENTIFIER_POINTER (name)),
10431 IDENTIFIER_POINTER (signature), dup,
b67d701b 10432 (candidates ? candidates : ""));
b5b8a0e7 10433 free (dup);
b67d701b
PB
10434 return NULL_TREE;
10435}
10436
5e942c50
APB
10437/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10438 when we're looking for a constructor. */
b67d701b
PB
10439
10440static tree
5e942c50
APB
10441find_applicable_accessible_methods_list (lc, class, name, arglist)
10442 int lc;
b67d701b
PB
10443 tree class, name, arglist;
10444{
ad69b5b6
BM
10445 static struct hash_table t, *searched_classes = NULL;
10446 static int search_not_done = 0;
b67d701b
PB
10447 tree list = NULL_TREE, all_list = NULL_TREE;
10448
ad69b5b6
BM
10449 /* Check the hash table to determine if this class has been searched
10450 already. */
10451 if (searched_classes)
10452 {
10453 if (hash_lookup (searched_classes,
10454 (const hash_table_key) class, FALSE, NULL))
10455 return NULL;
10456 }
10457 else
10458 {
10459 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10460 java_hash_compare_tree_node);
10461 searched_classes = &t;
10462 }
10463
10464 search_not_done++;
10465 hash_lookup (searched_classes,
0c2b8145 10466 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10467
c2952b01
APB
10468 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10469 {
10470 load_class (class, 1);
10471 safe_layout_class (class);
10472 }
10473
1982388a 10474 /* Search interfaces */
9a7ab4b3
APB
10475 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10476 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10477 {
1982388a
APB
10478 int i, n;
10479 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10480 search_applicable_methods_list (lc, TYPE_METHODS (class),
10481 name, arglist, &list, &all_list);
1982388a 10482 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10483 for (i = 1; i < n; i++)
b67d701b 10484 {
de0b553f
APB
10485 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10486 tree rlist;
10487
de0b553f
APB
10488 rlist = find_applicable_accessible_methods_list (lc, t, name,
10489 arglist);
165f37bc 10490 list = chainon (rlist, list);
e04a16fb 10491 }
e04a16fb 10492 }
1982388a
APB
10493 /* Search classes */
10494 else
c2952b01 10495 {
165f37bc
APB
10496 tree sc = class;
10497 int seen_inner_class = 0;
c2952b01
APB
10498 search_applicable_methods_list (lc, TYPE_METHODS (class),
10499 name, arglist, &list, &all_list);
10500
165f37bc
APB
10501 /* We must search all interfaces of this class */
10502 if (!lc)
10503 {
10504 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10505 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10506 for (i = 1; i < n; i++)
10507 {
10508 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10509 if (t != object_type_node)
30a3caef
ZW
10510 {
10511 tree rlist
10512 = find_applicable_accessible_methods_list (lc, t,
10513 name, arglist);
10514 list = chainon (rlist, list);
10515 }
165f37bc 10516 }
165f37bc
APB
10517 }
10518
c2952b01
APB
10519 /* Search enclosing context of inner classes before looking
10520 ancestors up. */
10521 while (!lc && INNER_CLASS_TYPE_P (class))
10522 {
165f37bc
APB
10523 tree rlist;
10524 seen_inner_class = 1;
c2952b01 10525 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10526 rlist = find_applicable_accessible_methods_list (lc, class,
10527 name, arglist);
10528 list = chainon (rlist, list);
c2952b01 10529 }
165f37bc
APB
10530
10531 if (!lc && seen_inner_class
10532 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10533 class = CLASSTYPE_SUPER (sc);
10534 else
10535 class = sc;
10536
ad69b5b6
BM
10537 /* Search superclass */
10538 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10539 {
10540 tree rlist;
10541 class = CLASSTYPE_SUPER (class);
10542 rlist = find_applicable_accessible_methods_list (lc, class,
10543 name, arglist);
10544 list = chainon (rlist, list);
10545 }
10546 }
10547
10548 search_not_done--;
10549
10550 /* We're done. Reset the searched classes list and finally search
10551 java.lang.Object if it wasn't searched already. */
10552 if (!search_not_done)
10553 {
10554 if (!lc
10555 && TYPE_METHODS (object_type_node)
10556 && !hash_lookup (searched_classes,
10557 (const hash_table_key) object_type_node,
10558 FALSE, NULL))
10559 {
10560 search_applicable_methods_list (lc,
10561 TYPE_METHODS (object_type_node),
10562 name, arglist, &list, &all_list);
10563 }
10564 hash_table_free (searched_classes);
10565 searched_classes = NULL;
c2952b01 10566 }
1982388a 10567
b67d701b
PB
10568 /* Either return the list obtained or all selected (but
10569 inaccessible) methods for better error report. */
10570 return (!list ? all_list : list);
10571}
e04a16fb 10572
ad69b5b6 10573/* Effectively search for the appropriate method in method */
1982388a
APB
10574
10575static void
c2952b01 10576search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10577 int lc;
10578 tree method, name, arglist;
10579 tree *list, *all_list;
10580{
10581 for (; method; method = TREE_CHAIN (method))
10582 {
10583 /* When dealing with constructor, stop here, otherwise search
10584 other classes */
10585 if (lc && !DECL_CONSTRUCTOR_P (method))
10586 continue;
10587 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10588 || (GET_METHOD_NAME (method) != name)))
10589 continue;
10590
10591 if (argument_types_convertible (method, arglist))
10592 {
10593 /* Retain accessible methods only */
10594 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10595 method, 0))
10596 *list = tree_cons (NULL_TREE, method, *list);
10597 else
10598 /* Also retain all selected method here */
10599 *all_list = tree_cons (NULL_TREE, method, *list);
10600 }
10601 }
ad69b5b6 10602}
1982388a 10603
b67d701b
PB
10604/* 15.11.2.2 Choose the Most Specific Method */
10605
10606static tree
10607find_most_specific_methods_list (list)
10608 tree list;
10609{
10610 int max = 0;
9a7ab4b3 10611 int abstract, candidates;
b67d701b
PB
10612 tree current, new_list = NULL_TREE;
10613 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10614 {
b67d701b
PB
10615 tree method;
10616 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10617
10618 for (method = list; method; method = TREE_CHAIN (method))
10619 {
0c2b8145 10620 tree method_v, current_v;
b67d701b
PB
10621 /* Don't test a method against itself */
10622 if (method == current)
10623 continue;
10624
0c2b8145
APB
10625 method_v = TREE_VALUE (method);
10626 current_v = TREE_VALUE (current);
10627
10628 /* Compare arguments and location where methods where declared */
10629 if (argument_types_convertible (method_v, current_v))
b67d701b 10630 {
0c2b8145
APB
10631 if (valid_method_invocation_conversion_p
10632 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10633 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10634 && enclosing_context_p (DECL_CONTEXT (method_v),
10635 DECL_CONTEXT (current_v))))
10636 {
10637 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10638 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10639 max = (v > max ? v : max);
10640 }
b67d701b
PB
10641 }
10642 }
e04a16fb
AG
10643 }
10644
b67d701b 10645 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10646 for (current = list, abstract = -1, candidates = -1;
10647 current; current = TREE_CHAIN (current))
b67d701b 10648 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10649 {
10650 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10651 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10652 candidates++;
10653 }
b67d701b 10654
165f37bc
APB
10655 /* If we have several and they're all abstract, just pick the
10656 closest one. */
9a7ab4b3
APB
10657 if (candidates > 0 && (candidates == abstract))
10658 {
10659 new_list = nreverse (new_list);
10660 TREE_CHAIN (new_list) = NULL_TREE;
10661 }
165f37bc 10662
cab8e2bd
BM
10663 /* We have several (we couldn't find a most specific), all but one
10664 are abstract, we pick the only non abstract one. */
10665 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10666 {
9a7ab4b3
APB
10667 for (current = new_list; current; current = TREE_CHAIN (current))
10668 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10669 {
10670 TREE_CHAIN (current) = NULL_TREE;
10671 new_list = current;
10672 }
165f37bc
APB
10673 }
10674
b67d701b
PB
10675 /* If we can't find one, lower expectations and try to gather multiple
10676 maximally specific methods */
165f37bc 10677 while (!new_list && max)
b67d701b
PB
10678 {
10679 while (--max > 0)
10680 {
10681 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10682 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10683 }
b67d701b
PB
10684 }
10685
10686 return new_list;
e04a16fb
AG
10687}
10688
b67d701b
PB
10689/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10690 converted by method invocation conversion (5.3) to the type of the
10691 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10692 to change less often than M1. */
e04a16fb 10693
b67d701b
PB
10694static int
10695argument_types_convertible (m1, m2_or_arglist)
10696 tree m1, m2_or_arglist;
e04a16fb 10697{
b67d701b
PB
10698 static tree m2_arg_value = NULL_TREE;
10699 static tree m2_arg_cache = NULL_TREE;
19e223db 10700 static int initialized_p;
e04a16fb 10701
b67d701b 10702 register tree m1_arg, m2_arg;
e04a16fb 10703
19e223db
MM
10704 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10705 collector. */
10706 if (!initialized_p)
10707 {
10708 ggc_add_tree_root (&m2_arg_value, 1);
10709 ggc_add_tree_root (&m2_arg_cache, 1);
10710 initialized_p = 1;
10711 }
10712
c2952b01 10713 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10714
b67d701b
PB
10715 if (m2_arg_value == m2_or_arglist)
10716 m2_arg = m2_arg_cache;
10717 else
10718 {
10719 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10720 argument types */
10721 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10722 {
10723 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10724 if (!METHOD_STATIC (m2_or_arglist))
10725 m2_arg = TREE_CHAIN (m2_arg);
10726 }
10727 else
10728 m2_arg = m2_or_arglist;
e04a16fb 10729
b67d701b
PB
10730 m2_arg_value = m2_or_arglist;
10731 m2_arg_cache = m2_arg;
10732 }
e04a16fb 10733
de4c7b02 10734 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10735 {
c877974e 10736 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10737 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10738 TREE_VALUE (m2_arg)))
10739 break;
10740 m1_arg = TREE_CHAIN (m1_arg);
10741 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10742 }
de4c7b02 10743 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10744}
10745
10746/* Qualification routines */
10747
10748static void
10749qualify_ambiguous_name (id)
10750 tree id;
10751{
cd531a2e
KG
10752 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10753 saved_current_class;
d8fccff5 10754 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10755 int code;
e04a16fb
AG
10756
10757 /* We first qualify the first element, then derive qualification of
10758 others based on the first one. If the first element is qualified
10759 by a resolution (field or type), this resolution is stored in the
10760 QUAL_RESOLUTION of the qual element being examined. We need to
10761 save the current_class since the use of SUPER might change the
10762 its value. */
10763 saved_current_class = current_class;
10764 qual = EXPR_WFL_QUALIFICATION (id);
10765 do {
10766
10767 /* Simple qualified expression feature a qual_wfl that is a
10768 WFL. Expression derived from a primary feature more complicated
10769 things like a CALL_EXPR. Expression from primary need to be
10770 worked out to extract the part on which the qualification will
10771 take place. */
10772 qual_wfl = QUAL_WFL (qual);
10773 switch (TREE_CODE (qual_wfl))
10774 {
10775 case CALL_EXPR:
10776 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10777 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10778 {
10779 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10780 qual_wfl = QUAL_WFL (qual);
10781 }
10782 break;
d8fccff5 10783 case NEW_ARRAY_EXPR:
c2952b01 10784 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10785 qual = TREE_CHAIN (qual);
1a6d4fb7 10786 again = new_array_found = 1;
d8fccff5 10787 continue;
e04a16fb 10788 case CONVERT_EXPR:
f2760b27
APB
10789 break;
10790 case NEW_CLASS_EXPR:
e04a16fb
AG
10791 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10792 break;
c583dd46
APB
10793 case ARRAY_REF:
10794 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10795 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10796 break;
8576f094
APB
10797 case STRING_CST:
10798 qual = TREE_CHAIN (qual);
10799 qual_wfl = QUAL_WFL (qual);
10800 break;
165f37bc
APB
10801 case CLASS_LITERAL:
10802 qual = TREE_CHAIN (qual);
10803 qual_wfl = QUAL_WFL (qual);
10804 break;
0a2138e2
APB
10805 default:
10806 /* Fix for -Wall. Just break doing nothing */
10807 break;
e04a16fb 10808 }
8576f094 10809
e04a16fb
AG
10810 ptr_type = current_class;
10811 again = 0;
8576f094
APB
10812 code = TREE_CODE (qual_wfl);
10813
10814 /* Pos evaluation: non WFL leading expression nodes */
10815 if (code == CONVERT_EXPR
10816 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10817 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10818
cd7c5840
APB
10819 else if (code == INTEGER_CST)
10820 name = qual_wfl;
10821
ac22f9cb 10822 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10823 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10824 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10825
c2952b01
APB
10826 else if (code == TREE_LIST)
10827 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10828
37feda7d
APB
10829 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10830 || code == PLUS_EXPR)
8576f094
APB
10831 {
10832 qual = TREE_CHAIN (qual);
10833 qual_wfl = QUAL_WFL (qual);
10834 again = 1;
10835 }
10836 else
f441f671
APB
10837 {
10838 name = EXPR_WFL_NODE (qual_wfl);
10839 if (!name)
10840 {
10841 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10842 again = 1;
10843 }
10844 }
10845
e04a16fb
AG
10846 /* If we have a THIS (from a primary), we set the context accordingly */
10847 if (name == this_identifier_node)
10848 {
6e22695a
APB
10849 /* This isn't really elegant. One more added irregularity
10850 before I start using COMPONENT_REF (hopefully very soon.) */
10851 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10852 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10853 EXPR_WITH_FILE_LOCATION
10854 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10855 this_identifier_node)
10856 {
10857 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10858 qual = EXPR_WFL_QUALIFICATION (qual);
10859 }
e04a16fb
AG
10860 qual = TREE_CHAIN (qual);
10861 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10862 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10863 again = 1;
10864 else
10865 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10866 this_found = 1;
10867 }
10868 /* If we have a SUPER, we set the context accordingly */
10869 if (name == super_identifier_node)
10870 {
10871 current_class = CLASSTYPE_SUPER (ptr_type);
10872 /* Check that there is such a thing as a super class. If not,
10873 return. The error will be caught later on, during the
10874 resolution */
10875 if (!current_class)
10876 {
10877 current_class = saved_current_class;
10878 return;
10879 }
10880 qual = TREE_CHAIN (qual);
10881 /* Do one more interation to set things up */
10882 super_found = again = 1;
10883 }
10884 } while (again);
10885
f2760b27
APB
10886 /* If name appears within the scope of a local variable declaration
10887 or parameter declaration, then it is an expression name. We don't
10888 carry this test out if we're in the context of the use of SUPER
10889 or THIS */
cd7c5840
APB
10890 if (!this_found && !super_found
10891 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10892 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10893 {
10894 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10895 QUAL_RESOLUTION (qual) = decl;
10896 }
10897
10898 /* If within the class/interface NAME was found to be used there
10899 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10900 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10901 address length, it is OK. */
10902 else if ((decl = lookup_field_wrapper (ptr_type, name))
10903 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10904 {
10905 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10906 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10907 }
10908
1a6d4fb7 10909 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10910 - NAME is a class/interface declared within the compilation
10911 unit containing NAME,
10912 - NAME is imported via a single-type-import declaration,
10913 - NAME is declared in an another compilation unit of the package
10914 of the compilation unit containing NAME,
10915 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10916 of the compilation unit containing NAME.
10917 - NAME is actually a STRING_CST. */
cd7c5840
APB
10918 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10919 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10920 {
10921 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10922 QUAL_RESOLUTION (qual) = decl;
10923 }
10924
f2760b27 10925 /* Method call, array references and cast are expression name */
9bbc7d9f 10926 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10927 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10928 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10929 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10930
10931 /* Check here that NAME isn't declared by more than one
10932 type-import-on-demand declaration of the compilation unit
10933 containing NAME. FIXME */
10934
10935 /* Otherwise, NAME is reclassified as a package name */
10936 else
10937 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10938
10939 /* Propagate the qualification accross other components of the
10940 qualified name */
10941 for (qual = TREE_CHAIN (qual); qual;
10942 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10943 {
10944 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10945 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10946 else
10947 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10948 }
10949
10950 /* Store the global qualification for the ambiguous part of ID back
10951 into ID fields */
10952 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10953 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10954 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10955 RESOLVE_TYPE_NAME_P (id) = 1;
10956 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10957 RESOLVE_PACKAGE_NAME_P (id) = 1;
10958
10959 /* Restore the current class */
10960 current_class = saved_current_class;
10961}
10962
10963static int
10964breakdown_qualified (left, right, source)
10965 tree *left, *right, source;
10966{
63ad61ed 10967 char *p, *base;
e04a16fb
AG
10968 int l = IDENTIFIER_LENGTH (source);
10969
63ad61ed
ZW
10970 base = alloca (l + 1);
10971 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10972
e04a16fb 10973 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10974 p = base + l - 1;
e04a16fb
AG
10975 while (*p != '.' && p != base)
10976 p--;
10977
10978 /* We didn't find a '.'. Return an error */
10979 if (p == base)
10980 return 1;
10981
10982 *p = '\0';
10983 if (right)
10984 *right = get_identifier (p+1);
63ad61ed 10985 *left = get_identifier (base);
e04a16fb
AG
10986
10987 return 0;
10988}
10989
e04a16fb 10990/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10991 local variable decls if present.
10992 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10993
10994static tree
10995java_complete_tree (node)
10996 tree node;
5b09b33e
PB
10997{
10998 node = java_complete_lhs (node);
0c2b8145
APB
10999 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
11000 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11001 && !flag_emit_xref)
5b09b33e
PB
11002 {
11003 tree value = DECL_INITIAL (node);
11004 DECL_INITIAL (node) = NULL_TREE;
11005 value = fold_constant_for_init (value, node);
11006 DECL_INITIAL (node) = value;
11007 if (value != NULL_TREE)
c2952b01
APB
11008 {
11009 /* fold_constant_for_init sometimes widen the original type
11010 of the constant (i.e. byte to int.) It's not desirable,
11011 especially if NODE is a function argument. */
11012 if (TREE_CODE (value) == INTEGER_CST
11013 && TREE_TYPE (node) != TREE_TYPE (value))
11014 return convert (TREE_TYPE (node), value);
11015 else
11016 return value;
11017 }
5b09b33e
PB
11018 }
11019 return node;
11020}
11021
2aa11e97
APB
11022static tree
11023java_stabilize_reference (node)
11024 tree node;
11025{
11026 if (TREE_CODE (node) == COMPOUND_EXPR)
11027 {
11028 tree op0 = TREE_OPERAND (node, 0);
11029 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11030 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11031 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11032 return node;
11033 }
5cbdba64 11034 return stabilize_reference (node);
2aa11e97
APB
11035}
11036
5b09b33e
PB
11037/* Patch tree nodes in a function body. When a BLOCK is found, push
11038 local variable decls if present.
11039 Same as java_complete_tree, but does not resolve static finals to values. */
11040
11041static tree
11042java_complete_lhs (node)
11043 tree node;
e04a16fb 11044{
22eed1e6 11045 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11046 int flag;
e04a16fb
AG
11047
11048 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11049 worked out. */
e04a16fb
AG
11050 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11051 return node;
11052
11053 /* The switch block implements cases processing container nodes
11054 first. Contained nodes are always written back. Leaves come
11055 next and return a value. */
11056 switch (TREE_CODE (node))
11057 {
11058 case BLOCK:
11059
11060 /* 1- Block section.
11061 Set the local values on decl names so we can identify them
11062 faster when they're referenced. At that stage, identifiers
11063 are legal so we don't check for declaration errors. */
11064 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11065 {
11066 DECL_CONTEXT (cn) = current_function_decl;
11067 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11068 }
15fdcfe9
PB
11069 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11070 CAN_COMPLETE_NORMALLY (node) = 1;
11071 else
e04a16fb 11072 {
15fdcfe9
PB
11073 tree stmt = BLOCK_EXPR_BODY (node);
11074 tree *ptr;
11075 int error_seen = 0;
11076 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11077 {
c877974e
APB
11078 /* Re-order from (((A; B); C); ...; Z) to
11079 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11080 This makes it easier to scan the statements left-to-right
11081 without using recursion (which might overflow the stack
11082 if the block has many statements. */
11083 for (;;)
11084 {
11085 tree left = TREE_OPERAND (stmt, 0);
11086 if (TREE_CODE (left) != COMPOUND_EXPR)
11087 break;
11088 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11089 TREE_OPERAND (left, 1) = stmt;
11090 stmt = left;
11091 }
11092 BLOCK_EXPR_BODY (node) = stmt;
11093 }
11094
c877974e
APB
11095 /* Now do the actual complete, without deep recursion for
11096 long blocks. */
15fdcfe9 11097 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11098 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11099 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11100 {
11101 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11102 tree *next = &TREE_OPERAND (*ptr, 1);
11103 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11104 if (cur == empty_stmt_node)
11105 {
11106 /* Optimization; makes it easier to detect empty bodies.
11107 Most useful for <clinit> with all-constant initializer. */
11108 *ptr = *next;
11109 continue;
11110 }
15fdcfe9
PB
11111 if (TREE_CODE (cur) == ERROR_MARK)
11112 error_seen++;
11113 else if (! CAN_COMPLETE_NORMALLY (cur))
11114 {
11115 wfl_op2 = *next;
11116 for (;;)
11117 {
11118 if (TREE_CODE (wfl_op2) == BLOCK)
11119 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11120 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11121 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11122 else
11123 break;
11124 }
11125 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11126 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11127 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11128 }
11129 ptr = next;
11130 }
11131 *ptr = java_complete_tree (*ptr);
11132
11133 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11134 return error_mark_node;
15fdcfe9 11135 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11136 }
11137 /* Turn local bindings to null */
11138 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11139 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11140
11141 TREE_TYPE (node) = void_type_node;
11142 break;
11143
11144 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11145
b9f7e36c
APB
11146 case THROW_EXPR:
11147 wfl_op1 = TREE_OPERAND (node, 0);
11148 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11149 /* 14.19 A throw statement cannot complete normally. */
11150 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11151 return patch_throw_statement (node, wfl_op1);
11152
11153 case SYNCHRONIZED_EXPR:
11154 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11155 return patch_synchronized_statement (node, wfl_op1);
11156
b67d701b
PB
11157 case TRY_EXPR:
11158 return patch_try_statement (node);
11159
a7d8d81f
PB
11160 case TRY_FINALLY_EXPR:
11161 COMPLETE_CHECK_OP_0 (node);
11162 COMPLETE_CHECK_OP_1 (node);
11163 CAN_COMPLETE_NORMALLY (node)
11164 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11165 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11166 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11167 return node;
11168
5a005d9e
PB
11169 case CLEANUP_POINT_EXPR:
11170 COMPLETE_CHECK_OP_0 (node);
11171 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11172 CAN_COMPLETE_NORMALLY (node) =
11173 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11174 return node;
11175
11176 case WITH_CLEANUP_EXPR:
11177 COMPLETE_CHECK_OP_0 (node);
11178 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11179 CAN_COMPLETE_NORMALLY (node) =
11180 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11181 TREE_TYPE (node) = void_type_node;
11182 return node;
11183
e04a16fb
AG
11184 case LABELED_BLOCK_EXPR:
11185 PUSH_LABELED_BLOCK (node);
11186 if (LABELED_BLOCK_BODY (node))
11187 COMPLETE_CHECK_OP_1 (node);
11188 TREE_TYPE (node) = void_type_node;
11189 POP_LABELED_BLOCK ();
1fb89a4d
APB
11190
11191 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11192 {
11193 LABELED_BLOCK_BODY (node) = NULL_TREE;
11194 CAN_COMPLETE_NORMALLY (node) = 1;
11195 }
1fb89a4d 11196 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11197 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11198 return node;
11199
11200 case EXIT_BLOCK_EXPR:
11201 /* We don't complete operand 1, because it's the return value of
11202 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11203 return patch_bc_statement (node);
11204
15fdcfe9
PB
11205 case CASE_EXPR:
11206 cn = java_complete_tree (TREE_OPERAND (node, 0));
11207 if (cn == error_mark_node)
11208 return cn;
11209
8576f094
APB
11210 /* First, the case expression must be constant. Values of final
11211 fields are accepted. */
15fdcfe9 11212 cn = fold (cn);
8576f094
APB
11213 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11214 && JDECL_P (TREE_OPERAND (cn, 1))
11215 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11216 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11217 {
100f7cd8
APB
11218 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11219 TREE_OPERAND (cn, 1));
100f7cd8 11220 }
15fdcfe9 11221
ce6e9147 11222 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11223 {
11224 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11225 parse_error_context (node, "Constant expression required");
11226 return error_mark_node;
11227 }
11228
11229 nn = ctxp->current_loop;
11230
11231 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11232 if (!try_builtin_assignconv (NULL_TREE,
11233 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11234 {
11235 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11236 parse_error_context
11237 (wfl_operator,
11238 "Incompatible type for case. Can't convert `%s' to `int'",
11239 lang_printable_name (TREE_TYPE (cn), 0));
11240 return error_mark_node;
11241 }
11242
11243 cn = fold (convert (int_type_node, cn));
11244
11245 /* Multiple instance of a case label bearing the same
11246 value is checked during code generation. The case
11247 expression is allright so far. */
34d4df06
APB
11248 if (TREE_CODE (cn) == VAR_DECL)
11249 cn = DECL_INITIAL (cn);
15fdcfe9 11250 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11251 TREE_TYPE (node) = void_type_node;
15fdcfe9 11252 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11253 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11254 break;
11255
11256 case DEFAULT_EXPR:
11257 nn = ctxp->current_loop;
11258 /* Only one default label is allowed per switch statement */
11259 if (SWITCH_HAS_DEFAULT (nn))
11260 {
11261 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11262 parse_error_context (wfl_operator,
11263 "Duplicate case label: `default'");
11264 return error_mark_node;
11265 }
11266 else
11267 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11268 TREE_TYPE (node) = void_type_node;
10100cc7 11269 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11270 CAN_COMPLETE_NORMALLY (node) = 1;
11271 break;
11272
b67d701b 11273 case SWITCH_EXPR:
e04a16fb
AG
11274 case LOOP_EXPR:
11275 PUSH_LOOP (node);
11276 /* Check whether the loop was enclosed in a labeled
11277 statement. If not, create one, insert the loop in it and
11278 return the node */
11279 nn = patch_loop_statement (node);
b67d701b 11280
e04a16fb 11281 /* Anyways, walk the body of the loop */
b67d701b
PB
11282 if (TREE_CODE (node) == LOOP_EXPR)
11283 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11284 /* Switch statement: walk the switch expression and the cases */
11285 else
11286 node = patch_switch_statement (node);
11287
e7c7bcef 11288 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11289 nn = error_mark_node;
11290 else
15fdcfe9 11291 {
b635eb2f
PB
11292 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11293 /* If we returned something different, that's because we
11294 inserted a label. Pop the label too. */
11295 if (nn != node)
11296 {
11297 if (CAN_COMPLETE_NORMALLY (node))
11298 CAN_COMPLETE_NORMALLY (nn) = 1;
11299 POP_LABELED_BLOCK ();
11300 }
15fdcfe9 11301 }
e04a16fb
AG
11302 POP_LOOP ();
11303 return nn;
11304
11305 case EXIT_EXPR:
11306 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11307 return patch_exit_expr (node);
11308
11309 case COND_EXPR:
11310 /* Condition */
11311 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11312 if (TREE_OPERAND (node, 0) == error_mark_node)
11313 return error_mark_node;
11314 /* then-else branches */
11315 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11316 if (TREE_OPERAND (node, 1) == error_mark_node)
11317 return error_mark_node;
11318 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11319 if (TREE_OPERAND (node, 2) == error_mark_node)
11320 return error_mark_node;
11321 return patch_if_else_statement (node);
11322 break;
11323
22eed1e6
APB
11324 case CONDITIONAL_EXPR:
11325 /* Condition */
11326 wfl_op1 = TREE_OPERAND (node, 0);
11327 COMPLETE_CHECK_OP_0 (node);
11328 wfl_op2 = TREE_OPERAND (node, 1);
11329 COMPLETE_CHECK_OP_1 (node);
11330 wfl_op3 = TREE_OPERAND (node, 2);
11331 COMPLETE_CHECK_OP_2 (node);
11332 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11333
e04a16fb
AG
11334 /* 3- Expression section */
11335 case COMPOUND_EXPR:
15fdcfe9 11336 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11337 TREE_OPERAND (node, 0) = nn =
11338 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11339 if (wfl_op2 == empty_stmt_node)
11340 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11341 else
15fdcfe9 11342 {
dc0b3eff 11343 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11344 {
dc0b3eff
PB
11345 /* An unreachable condition in a do-while statement
11346 is *not* (technically) an unreachable statement. */
11347 nn = wfl_op2;
11348 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11349 nn = EXPR_WFL_NODE (nn);
11350 if (TREE_CODE (nn) != EXIT_EXPR)
11351 {
11352 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11353 parse_error_context (wfl_operator, "Unreachable statement");
11354 }
bccaf73a 11355 }
dc0b3eff
PB
11356 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11357 if (TREE_OPERAND (node, 1) == error_mark_node)
11358 return error_mark_node;
11359 CAN_COMPLETE_NORMALLY (node)
11360 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11361 }
e04a16fb
AG
11362 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11363 break;
11364
11365 case RETURN_EXPR:
15fdcfe9 11366 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11367 return patch_return (node);
11368
11369 case EXPR_WITH_FILE_LOCATION:
11370 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11371 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11372 {
5423609c 11373 tree wfl = node;
15fdcfe9 11374 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11375 if (node == error_mark_node)
11376 return node;
5423609c
APB
11377 /* Keep line number information somewhere were it doesn't
11378 disrupt the completion process. */
2c56429a 11379 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11380 {
11381 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11382 TREE_OPERAND (node, 1) = wfl;
11383 }
15fdcfe9
PB
11384 CAN_COMPLETE_NORMALLY (node) = 1;
11385 }
e04a16fb
AG
11386 else
11387 {
5b09b33e
PB
11388 tree body;
11389 int save_lineno = lineno;
11390 lineno = EXPR_WFL_LINENO (node);
11391 body = java_complete_tree (EXPR_WFL_NODE (node));
11392 lineno = save_lineno;
15fdcfe9 11393 EXPR_WFL_NODE (node) = body;
dc0b3eff 11394 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11395 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11396 if (body == empty_stmt_node)
11397 {
11398 /* Optimization; makes it easier to detect empty bodies. */
11399 return body;
11400 }
dc0b3eff 11401 if (body == error_mark_node)
e04a16fb
AG
11402 {
11403 /* Its important for the evaluation of assignment that
11404 this mark on the TREE_TYPE is propagated. */
11405 TREE_TYPE (node) = error_mark_node;
11406 return error_mark_node;
11407 }
11408 else
11409 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11410
e04a16fb
AG
11411 }
11412 break;
11413
b67d701b 11414 case NEW_ARRAY_EXPR:
e04a16fb
AG
11415 /* Patch all the dimensions */
11416 flag = 0;
11417 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11418 {
11419 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11420 tree dim = convert (int_type_node,
11421 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11422 if (dim == error_mark_node)
11423 {
11424 flag = 1;
11425 continue;
11426 }
11427 else
11428 {
b9f7e36c 11429 TREE_VALUE (cn) = dim;
e04a16fb
AG
11430 /* Setup the location of the current dimension, for
11431 later error report. */
11432 TREE_PURPOSE (cn) =
11433 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11434 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11435 }
11436 }
11437 /* They complete the array creation expression, if no errors
11438 were found. */
15fdcfe9 11439 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11440 return (flag ? error_mark_node
11441 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11442
c2952b01
APB
11443 case NEW_ANONYMOUS_ARRAY_EXPR:
11444 /* Create the array type if necessary. */
11445 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11446 {
11447 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11448 if (!(type = resolve_type_during_patch (type)))
11449 return error_mark_node;
11450 type = build_array_from_name (type, NULL_TREE,
11451 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11452 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11453 }
11454 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11455 ANONYMOUS_ARRAY_INITIALIZER (node));
11456 if (node == error_mark_node)
11457 return error_mark_node;
11458 CAN_COMPLETE_NORMALLY (node) = 1;
11459 return node;
11460
b67d701b 11461 case NEW_CLASS_EXPR:
e04a16fb 11462 case CALL_EXPR:
b67d701b 11463 /* Complete function's argument(s) first */
e04a16fb
AG
11464 if (complete_function_arguments (node))
11465 return error_mark_node;
11466 else
b9f7e36c 11467 {
22eed1e6
APB
11468 tree decl, wfl = TREE_OPERAND (node, 0);
11469 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11470
c877974e 11471 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11472 NULL_TREE, 0, &decl);
c877974e
APB
11473 if (node == error_mark_node)
11474 return error_mark_node;
11475
11476 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11477 /* If we call this(...), register signature and positions */
11478 if (in_this)
11479 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11480 tree_cons (wfl, decl,
11481 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11482 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11483 return force_evaluation_order (node);
b9f7e36c 11484 }
e04a16fb
AG
11485
11486 case MODIFY_EXPR:
11487 /* Save potential wfls */
11488 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11489 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11490
cd9643f7
PB
11491 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11492 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11493 && DECL_INITIAL (nn) != NULL_TREE)
11494 {
100f7cd8
APB
11495 tree value;
11496
100f7cd8 11497 value = fold_constant_for_init (nn, nn);
c2952b01 11498
cd9643f7
PB
11499 if (value != NULL_TREE)
11500 {
11501 tree type = TREE_TYPE (value);
c2952b01
APB
11502 if (JPRIMITIVE_TYPE_P (type) ||
11503 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11504 return empty_stmt_node;
11505 }
629d4b4d
APB
11506 if (! flag_emit_class_files)
11507 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11508 }
e04a16fb 11509 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11510
e04a16fb
AG
11511 if (TREE_OPERAND (node, 0) == error_mark_node)
11512 return error_mark_node;
11513
5cbdba64
APB
11514 flag = COMPOUND_ASSIGN_P (wfl_op2);
11515 if (flag)
e04a16fb 11516 {
c2952b01
APB
11517 /* This might break when accessing outer field from inner
11518 class. TESTME, FIXME */
2aa11e97 11519 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11520
11521 /* Hand stablize the lhs on both places */
e04a16fb 11522 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11523 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11524 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11525
5cbdba64 11526 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11527 /* Now complete the RHS. We write it back later on. */
11528 nn = java_complete_tree (TREE_OPERAND (node, 1));
11529
642f15d1
APB
11530 if ((cn = patch_string (nn)))
11531 nn = cn;
11532
2aa11e97
APB
11533 /* The last part of the rewrite for E1 op= E2 is to have
11534 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11535 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11536 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11537
11538 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11539 }
11540
f8976021 11541 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11542 function to complete this RHS. Note that a NEW_ARRAY_INIT
11543 might have been already fully expanded if created as a result
11544 of processing an anonymous array initializer. We avoid doing
11545 the operation twice by testing whether the node already bears
11546 a type. */
11547 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11548 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11549 TREE_OPERAND (node, 1));
2aa11e97 11550 /* Otherwise we simply complete the RHS */
f8976021
APB
11551 else
11552 nn = java_complete_tree (TREE_OPERAND (node, 1));
11553
e04a16fb 11554 if (nn == error_mark_node)
c0d87ff6 11555 return error_mark_node;
2aa11e97
APB
11556
11557 /* Write back the RHS as we evaluated it. */
e04a16fb 11558 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11559
11560 /* In case we're handling = with a String as a RHS, we need to
11561 produce a String out of the RHS (it might still be a
11562 STRING_CST or a StringBuffer at this stage */
11563 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11564 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11565
11566 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11567 TREE_OPERAND (node, 1))))
11568 {
11569 /* We return error_mark_node if outer_field_access_fix
11570 detects we write into a final. */
11571 if (nn == error_mark_node)
11572 return error_mark_node;
11573 node = nn;
11574 }
11575 else
11576 {
11577 node = patch_assignment (node, wfl_op1, wfl_op2);
11578 /* Reorganize the tree if necessary. */
11579 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11580 || JSTRING_P (TREE_TYPE (node))))
11581 node = java_refold (node);
11582 }
11583
15fdcfe9
PB
11584 CAN_COMPLETE_NORMALLY (node) = 1;
11585 return node;
e04a16fb
AG
11586
11587 case MULT_EXPR:
11588 case PLUS_EXPR:
11589 case MINUS_EXPR:
11590 case LSHIFT_EXPR:
11591 case RSHIFT_EXPR:
11592 case URSHIFT_EXPR:
11593 case BIT_AND_EXPR:
11594 case BIT_XOR_EXPR:
11595 case BIT_IOR_EXPR:
11596 case TRUNC_MOD_EXPR:
c2952b01 11597 case TRUNC_DIV_EXPR:
e04a16fb
AG
11598 case RDIV_EXPR:
11599 case TRUTH_ANDIF_EXPR:
11600 case TRUTH_ORIF_EXPR:
11601 case EQ_EXPR:
11602 case NE_EXPR:
11603 case GT_EXPR:
11604 case GE_EXPR:
11605 case LT_EXPR:
11606 case LE_EXPR:
11607 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11608 knows how to handle those cases. */
11609 wfl_op1 = TREE_OPERAND (node, 0);
11610 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11611
15fdcfe9 11612 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11613 /* Don't complete string nodes if dealing with the PLUS operand. */
11614 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11615 {
11616 nn = java_complete_tree (wfl_op1);
11617 if (nn == error_mark_node)
11618 return error_mark_node;
48a840d9 11619
2aa11e97
APB
11620 TREE_OPERAND (node, 0) = nn;
11621 }
b67d701b 11622 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11623 {
11624 nn = java_complete_tree (wfl_op2);
11625 if (nn == error_mark_node)
11626 return error_mark_node;
48a840d9 11627
2aa11e97
APB
11628 TREE_OPERAND (node, 1) = nn;
11629 }
dc0b3eff 11630 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11631
5e942c50
APB
11632 case INSTANCEOF_EXPR:
11633 wfl_op1 = TREE_OPERAND (node, 0);
11634 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11635 if (flag_emit_xref)
11636 {
11637 TREE_TYPE (node) = boolean_type_node;
11638 return node;
11639 }
5e942c50
APB
11640 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11641
b67d701b 11642 case UNARY_PLUS_EXPR:
e04a16fb
AG
11643 case NEGATE_EXPR:
11644 case TRUTH_NOT_EXPR:
11645 case BIT_NOT_EXPR:
11646 case PREDECREMENT_EXPR:
11647 case PREINCREMENT_EXPR:
11648 case POSTDECREMENT_EXPR:
11649 case POSTINCREMENT_EXPR:
11650 case CONVERT_EXPR:
11651 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11652 how to handle those cases. */
11653 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11654 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11655 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11656 if (TREE_OPERAND (node, 0) == error_mark_node)
11657 return error_mark_node;
4a5f66c3
APB
11658 node = patch_unaryop (node, wfl_op1);
11659 CAN_COMPLETE_NORMALLY (node) = 1;
11660 break;
e04a16fb
AG
11661
11662 case ARRAY_REF:
11663 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11664 how to handle those cases. */
11665 wfl_op1 = TREE_OPERAND (node, 0);
11666 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11667 if (TREE_OPERAND (node, 0) == error_mark_node)
11668 return error_mark_node;
7f1d4866 11669 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11670 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11671 /* The same applies to wfl_op2 */
11672 wfl_op2 = TREE_OPERAND (node, 1);
11673 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11674 if (TREE_OPERAND (node, 1) == error_mark_node)
11675 return error_mark_node;
7f1d4866 11676 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11677 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11678 return patch_array_ref (node);
e04a16fb 11679
63a212ed
PB
11680 case RECORD_TYPE:
11681 return node;;
11682
11683 case COMPONENT_REF:
11684 /* The first step in the re-write of qualified name handling. FIXME.
11685 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11686 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11687 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11688 {
11689 tree name = TREE_OPERAND (node, 1);
11690 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11691 if (field == NULL_TREE)
11692 {
11693 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11694 return error_mark_node;
11695 }
11696 if (! FIELD_STATIC (field))
11697 {
11698 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11699 return error_mark_node;
11700 }
11701 return field;
11702 }
11703 else
11704 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11705 break;
9bbc7d9f 11706
b67d701b 11707 case THIS_EXPR:
e04a16fb
AG
11708 /* Can't use THIS in a static environment */
11709 if (!current_this)
11710 {
11711 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11712 parse_error_context (wfl_operator,
11713 "Keyword `this' used outside allowed context");
e04a16fb
AG
11714 TREE_TYPE (node) = error_mark_node;
11715 return error_mark_node;
11716 }
22eed1e6
APB
11717 if (ctxp->explicit_constructor_p)
11718 {
11719 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11720 parse_error_context
781b0558 11721 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11722 TREE_TYPE (node) = error_mark_node;
11723 return error_mark_node;
11724 }
e04a16fb 11725 return current_this;
c2952b01
APB
11726
11727 case CLASS_LITERAL:
11728 CAN_COMPLETE_NORMALLY (node) = 1;
11729 node = patch_incomplete_class_ref (node);
11730 if (node == error_mark_node)
11731 return error_mark_node;
11732 break;
11733
11734 case INSTANCE_INITIALIZERS_EXPR:
11735 in_instance_initializer++;
11736 node = java_complete_tree (TREE_OPERAND (node, 0));
11737 in_instance_initializer--;
11738 if (node != error_mark_node)
11739 TREE_TYPE (node) = void_type_node;
11740 else
11741 return error_mark_node;
11742 break;
e04a16fb 11743
e04a16fb 11744 default:
15fdcfe9 11745 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11746 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11747 and it's time to turn it into the appropriate String object */
11748 if ((nn = patch_string (node)))
11749 node = nn;
11750 else
11751 fatal ("No case for tree code `%s' - java_complete_tree\n",
11752 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11753 }
11754 return node;
11755}
11756
11757/* Complete function call's argument. Return a non zero value is an
11758 error was found. */
11759
11760static int
11761complete_function_arguments (node)
11762 tree node;
11763{
11764 int flag = 0;
11765 tree cn;
11766
f63991a8 11767 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11768 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11769 {
b67d701b 11770 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11771 parm = java_complete_tree (wfl);
c2952b01 11772
e04a16fb
AG
11773 if (parm == error_mark_node)
11774 {
11775 flag = 1;
11776 continue;
11777 }
b67d701b
PB
11778 /* If have a string literal that we haven't transformed yet or a
11779 crafted string buffer, as a result of use of the the String
11780 `+' operator. Build `parm.toString()' and expand it. */
11781 if ((temp = patch_string (parm)))
b9f7e36c 11782 parm = temp;
5e942c50
APB
11783 /* Inline PRIMTYPE.TYPE read access */
11784 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11785
5e942c50 11786 TREE_VALUE (cn) = parm;
e04a16fb 11787 }
f63991a8 11788 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11789 return flag;
11790}
11791
11792/* Sometimes (for loops and variable initialized during their
11793 declaration), we want to wrap a statement around a WFL and turn it
11794 debugable. */
11795
11796static tree
11797build_debugable_stmt (location, stmt)
11798 int location;
11799 tree stmt;
11800{
11801 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11802 {
11803 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11804 EXPR_WFL_LINECOL (stmt) = location;
11805 }
11806 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11807 return stmt;
11808}
11809
11810static tree
11811build_expr_block (body, decls)
11812 tree body, decls;
11813{
11814 tree node = make_node (BLOCK);
11815 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11816 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11817 if (body)
11818 TREE_TYPE (node) = TREE_TYPE (body);
11819 TREE_SIDE_EFFECTS (node) = 1;
11820 return node;
11821}
11822
b67d701b
PB
11823/* Create a new function block and link it approriately to current
11824 function block chain */
e04a16fb
AG
11825
11826static tree
11827enter_block ()
11828{
b67d701b
PB
11829 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11830}
11831
11832/* Link block B supercontext to the previous block. The current
11833 function DECL is used as supercontext when enter_a_block is called
11834 for the first time for a given function. The current function body
11835 (DECL_FUNCTION_BODY) is set to be block B. */
11836
11837static tree
11838enter_a_block (b)
11839 tree b;
11840{
e04a16fb
AG
11841 tree fndecl = current_function_decl;
11842
f099f336
APB
11843 if (!fndecl) {
11844 BLOCK_SUPERCONTEXT (b) = current_static_block;
11845 current_static_block = b;
11846 }
11847
11848 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11849 {
11850 BLOCK_SUPERCONTEXT (b) = fndecl;
11851 DECL_FUNCTION_BODY (fndecl) = b;
11852 }
11853 else
11854 {
11855 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11856 DECL_FUNCTION_BODY (fndecl) = b;
11857 }
11858 return b;
11859}
11860
11861/* Exit a block by changing the current function body
11862 (DECL_FUNCTION_BODY) to the current block super context, only if
11863 the block being exited isn't the method's top level one. */
11864
11865static tree
11866exit_block ()
11867{
f099f336
APB
11868 tree b;
11869 if (current_function_decl)
11870 {
11871 b = DECL_FUNCTION_BODY (current_function_decl);
11872 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11873 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11874 }
11875 else
11876 {
11877 b = current_static_block;
e04a16fb 11878
f099f336
APB
11879 if (BLOCK_SUPERCONTEXT (b))
11880 current_static_block = BLOCK_SUPERCONTEXT (b);
11881 }
e04a16fb
AG
11882 return b;
11883}
11884
11885/* Lookup for NAME in the nested function's blocks, all the way up to
11886 the current toplevel one. It complies with Java's local variable
11887 scoping rules. */
11888
11889static tree
11890lookup_name_in_blocks (name)
11891 tree name;
11892{
f099f336 11893 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11894
11895 while (b != current_function_decl)
11896 {
11897 tree current;
11898
11899 /* Paranoid sanity check. To be removed */
11900 if (TREE_CODE (b) != BLOCK)
11901 fatal ("non block expr function body - lookup_name_in_blocks");
11902
11903 for (current = BLOCK_EXPR_DECLS (b); current;
11904 current = TREE_CHAIN (current))
11905 if (DECL_NAME (current) == name)
11906 return current;
11907 b = BLOCK_SUPERCONTEXT (b);
11908 }
11909 return NULL_TREE;
11910}
11911
11912static void
11913maybe_absorb_scoping_blocks ()
11914{
f099f336 11915 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11916 {
11917 tree b = exit_block ();
11918 java_method_add_stmt (current_function_decl, b);
11919 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11920 }
11921}
11922
11923\f
11924/* This section of the source is reserved to build_* functions that
11925 are building incomplete tree nodes and the patch_* functions that
11926 are completing them. */
11927
c2952b01
APB
11928/* Wrap a non WFL node around a WFL. */
11929static tree
9a7ab4b3 11930build_wfl_wrap (node, location)
c2952b01 11931 tree node;
9a7ab4b3 11932 int location;
c2952b01
APB
11933{
11934 tree wfl, node_to_insert = node;
11935
11936 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11937 with the way we're processing SUPER. A THIS from a primary as a
11938 different form than a SUPER. Turn THIS into something symbolic */
11939 if (TREE_CODE (node) == THIS_EXPR)
11940 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11941 else
11942 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11943
9a7ab4b3 11944 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11945 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11946 return wfl;
11947}
11948
11949
9bbc7d9f 11950/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11951 we're currently dealing with the class java.lang.Object. */
11952
11953static tree
e920ebc9
APB
11954build_super_invocation (mdecl)
11955 tree mdecl;
22eed1e6 11956{
e920ebc9 11957 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11958 return empty_stmt_node;
22eed1e6
APB
11959 else
11960 {
9ee9b555 11961 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11962 tree a = NULL_TREE, t;
11963 /* If we're dealing with an anonymous class, pass the arguments
11964 of the crafted constructor along. */
11965 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11966 {
11967 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11968 for (; t != end_params_node; t = TREE_CHAIN (t))
11969 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11970 }
11971 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11972 }
11973}
11974
11975/* Build a SUPER/THIS qualified method invocation. */
11976
11977static tree
11978build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11979 int use_this;
11980 tree name, args;
11981 int lloc, rloc;
22eed1e6
APB
11982{
11983 tree invok;
11984 tree wfl =
9ee9b555 11985 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11986 EXPR_WFL_LINECOL (wfl) = lloc;
11987 invok = build_method_invocation (name, args);
11988 return make_qualified_primary (wfl, invok, rloc);
11989}
11990
b67d701b 11991/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11992
11993static tree
11994build_method_invocation (name, args)
11995 tree name;
11996 tree args;
11997{
11998 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11999 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12000 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12001 return call;
12002}
12003
12004/* Build an incomplete new xxx(...) node. */
12005
12006static tree
12007build_new_invocation (name, args)
12008 tree name, args;
12009{
12010 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12011 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12012 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12013 return call;
12014}
12015
12016/* Build an incomplete assignment expression. */
12017
12018static tree
12019build_assignment (op, op_location, lhs, rhs)
12020 int op, op_location;
12021 tree lhs, rhs;
12022{
12023 tree assignment;
12024 /* Build the corresponding binop if we deal with a Compound
12025 Assignment operator. Mark the binop sub-tree as part of a
12026 Compound Assignment expression */
12027 if (op != ASSIGN_TK)
12028 {
12029 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12030 COMPOUND_ASSIGN_P (rhs) = 1;
12031 }
12032 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12033 TREE_SIDE_EFFECTS (assignment) = 1;
12034 EXPR_WFL_LINECOL (assignment) = op_location;
12035 return assignment;
12036}
12037
12038/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12039
15fdcfe9 12040char *
e04a16fb
AG
12041print_int_node (node)
12042 tree node;
12043{
12044 static char buffer [80];
12045 if (TREE_CONSTANT_OVERFLOW (node))
12046 sprintf (buffer, "<overflow>");
12047
12048 if (TREE_INT_CST_HIGH (node) == 0)
12049 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12050 TREE_INT_CST_LOW (node));
12051 else if (TREE_INT_CST_HIGH (node) == -1
12052 && TREE_INT_CST_LOW (node) != 0)
12053 {
12054 buffer [0] = '-';
12055 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12056 -TREE_INT_CST_LOW (node));
12057 }
12058 else
12059 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12060 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12061
12062 return buffer;
12063}
12064
7f1d4866
APB
12065/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12066 context. */
5e942c50
APB
12067
12068static int
12069check_final_assignment (lvalue, wfl)
12070 tree lvalue, wfl;
12071{
6632dcdd
APB
12072 if (TREE_CODE (lvalue) == COMPOUND_EXPR
12073 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12074 lvalue = TREE_OPERAND (lvalue, 1);
12075
bc2874c9
TT
12076 /* When generating class files, references to the `length' field
12077 look a bit different. */
12078 if ((flag_emit_class_files
12079 && TREE_CODE (lvalue) == COMPONENT_REF
12080 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
12081 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
12082 || (TREE_CODE (lvalue) == FIELD_DECL
12083 && FIELD_FINAL (lvalue)
12084 && !DECL_CLINIT_P (current_function_decl)
12085 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
12086 {
12087 parse_error_context
12088 (wfl, "Can't assign a value to the final variable `%s'",
12089 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12090 return 1;
12091 }
12092 return 0;
12093}
12094
12095/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12096 read. This is needed to avoid circularities in the implementation
12097 of these fields in libjava. */
12098
12099static tree
12100maybe_build_primttype_type_ref (rhs, wfl)
12101 tree rhs, wfl;
12102{
12103 tree to_return = NULL_TREE;
12104 tree rhs_type = TREE_TYPE (rhs);
12105 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12106 {
12107 tree n = TREE_OPERAND (rhs, 1);
12108 if (TREE_CODE (n) == VAR_DECL
12109 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12110 && rhs_type == class_ptr_type
12111 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12112 {
49f48c71 12113 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12114 if (!strncmp (self_name, "java.lang.", 10))
12115 to_return = build_primtype_type_ref (self_name);
12116 }
12117 }
12118 return (to_return ? to_return : rhs );
12119}
12120
e04a16fb
AG
12121/* 15.25 Assignment operators. */
12122
12123static tree
12124patch_assignment (node, wfl_op1, wfl_op2)
12125 tree node;
12126 tree wfl_op1;
12127 tree wfl_op2;
12128{
0a2138e2 12129 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12130 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12131 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12132 int error_found = 0;
12133 int lvalue_from_array = 0;
12134
c2952b01 12135 /* Can't assign to a (blank) final. */
5e942c50
APB
12136 if (check_final_assignment (lvalue, wfl_op1))
12137 error_found = 1;
e04a16fb
AG
12138
12139 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12140
12141 /* Lhs can be a named variable */
34f4db93 12142 if (JDECL_P (lvalue))
e04a16fb 12143 {
e04a16fb
AG
12144 lhs_type = TREE_TYPE (lvalue);
12145 }
12146 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12147 comment on reason why */
12148 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12149 {
12150 lhs_type = TREE_TYPE (lvalue);
12151 lvalue_from_array = 1;
12152 }
12153 /* Or a field access */
12154 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12155 lhs_type = TREE_TYPE (lvalue);
12156 /* Or a function return slot */
12157 else if (TREE_CODE (lvalue) == RESULT_DECL)
12158 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12159 /* Otherwise, we might want to try to write into an optimized static
12160 final, this is an of a different nature, reported further on. */
12161 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12162 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12163 {
6632dcdd 12164 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12165 {
12166 /* What we should do instead is resetting the all the flags
12167 previously set, exchange lvalue for llvalue and continue. */
12168 error_found = 1;
12169 return error_mark_node;
12170 }
12171 else
12172 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12173 }
12174 else
e04a16fb
AG
12175 {
12176 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12177 error_found = 1;
12178 }
12179
12180 rhs_type = TREE_TYPE (rhs);
b67d701b 12181 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12182 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12183
b67d701b 12184 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12185 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12186 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12187
12188 /* 15.25.2 If we have a compound assignment, convert RHS into the
12189 type of the LHS */
12190 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12191 new_rhs = convert (lhs_type, rhs);
12192
12193 /* Explicit cast required. This is an error */
12194 if (!new_rhs)
12195 {
c2e3db92
KG
12196 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12197 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12198 tree wfl;
12199 char operation [32]; /* Max size known */
12200
12201 /* If the assignment is part of a declaration, we use the WFL of
12202 the declared variable to point out the error and call it a
12203 declaration problem. If the assignment is a genuine =
12204 operator, we call is a operator `=' problem, otherwise we
12205 call it an assignment problem. In both of these last cases,
12206 we use the WFL of the operator to indicate the error. */
12207
12208 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12209 {
12210 wfl = wfl_op1;
12211 strcpy (operation, "declaration");
12212 }
12213 else
12214 {
12215 wfl = wfl_operator;
12216 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12217 strcpy (operation, "assignment");
12218 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12219 strcpy (operation, "`return'");
12220 else
12221 strcpy (operation, "`='");
12222 }
12223
1ebadc60 12224 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12225 parse_error_context
12226 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12227 operation, t1, t2);
1ebadc60 12228 else
781b0558 12229 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12230 operation, t1, t2);
e04a16fb
AG
12231 free (t1); free (t2);
12232 error_found = 1;
12233 }
12234
c877974e
APB
12235 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12236 if (new_rhs)
12237 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12238
e04a16fb
AG
12239 if (error_found)
12240 return error_mark_node;
12241
2622b947
APB
12242 /* 10.10: Array Store Exception runtime check */
12243 if (!flag_emit_class_files
e8fc7396 12244 && !flag_emit_xref
2622b947 12245 && lvalue_from_array
afc390b1 12246 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12247 {
12248 tree check;
12249 tree base = lvalue;
12250
12251 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12252 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12253 base = TREE_OPERAND (lvalue, 0);
12254 else
12255 {
12256 if (flag_bounds_check)
12257 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12258 else
12259 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12260 }
12261
12262 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12263 new_rhs = save_expr (new_rhs);
2622b947
APB
12264 check = build (CALL_EXPR, void_type_node,
12265 build_address_of (soft_checkarraystore_node),
12266 tree_cons (NULL_TREE, base,
12267 build_tree_list (NULL_TREE, new_rhs)),
12268 NULL_TREE);
12269 TREE_SIDE_EFFECTS (check) = 1;
12270
12271 /* We have to decide on an insertion point */
12272 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12273 {
12274 tree t;
12275 if (flag_bounds_check)
12276 {
12277 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12278 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12279 build (COMPOUND_EXPR, void_type_node, t, check);
12280 }
12281 else
12282 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12283 check, TREE_OPERAND (lvalue, 1));
12284 }
12285 else
12286 {
12287 /* Make sure the bound check will happen before the store check */
12288 if (flag_bounds_check)
12289 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12290 build (COMPOUND_EXPR, void_type_node,
12291 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12292 else
12293 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12294 }
12295 }
22eed1e6 12296
34d4df06
APB
12297 /* Final locals can be used as case values in switch
12298 statement. Prepare them for this eventuality. */
12299 if (TREE_CODE (lvalue) == VAR_DECL
12300 && LOCAL_FINAL (lvalue)
12301 && TREE_CONSTANT (new_rhs)
12302 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12303 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12304 )
12305 {
12306 TREE_CONSTANT (lvalue) = 1;
12307 DECL_INITIAL (lvalue) = new_rhs;
12308 }
12309
e04a16fb
AG
12310 TREE_OPERAND (node, 0) = lvalue;
12311 TREE_OPERAND (node, 1) = new_rhs;
12312 TREE_TYPE (node) = lhs_type;
12313 return node;
12314}
12315
b67d701b
PB
12316/* Check that type SOURCE can be cast into type DEST. If the cast
12317 can't occur at all, return 0 otherwise 1. This function is used to
12318 produce accurate error messages on the reasons why an assignment
12319 failed. */
e04a16fb 12320
b67d701b
PB
12321static tree
12322try_reference_assignconv (lhs_type, rhs)
12323 tree lhs_type, rhs;
e04a16fb 12324{
b67d701b
PB
12325 tree new_rhs = NULL_TREE;
12326 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12327
b67d701b
PB
12328 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12329 {
12330 /* `null' may be assigned to any reference type */
12331 if (rhs == null_pointer_node)
12332 new_rhs = null_pointer_node;
12333 /* Try the reference assignment conversion */
12334 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12335 new_rhs = rhs;
12336 /* This is a magic assignment that we process differently */
12337 else if (rhs == soft_exceptioninfo_call_node)
12338 new_rhs = rhs;
12339 }
12340 return new_rhs;
12341}
12342
12343/* Check that RHS can be converted into LHS_TYPE by the assignment
12344 conversion (5.2), for the cases of RHS being a builtin type. Return
12345 NULL_TREE if the conversion fails or if because RHS isn't of a
12346 builtin type. Return a converted RHS if the conversion is possible. */
12347
12348static tree
12349try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12350 tree wfl_op1, lhs_type, rhs;
12351{
12352 tree new_rhs = NULL_TREE;
12353 tree rhs_type = TREE_TYPE (rhs);
12354
7e51098e
TT
12355 /* Handle boolean specially. */
12356 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12357 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12358 {
12359 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12360 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12361 new_rhs = rhs;
12362 }
12363
5e942c50 12364 /* Zero accepted everywhere */
7e51098e 12365 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12366 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12367 && JPRIMITIVE_TYPE_P (rhs_type))
12368 new_rhs = convert (lhs_type, rhs);
12369
b67d701b
PB
12370 /* 5.1.1 Try Identity Conversion,
12371 5.1.2 Try Widening Primitive Conversion */
5e942c50 12372 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12373 new_rhs = convert (lhs_type, rhs);
12374
12375 /* Try a narrowing primitive conversion (5.1.3):
12376 - expression is a constant expression of type int AND
12377 - variable is byte, short or char AND
12378 - The value of the expression is representable in the type of the
12379 variable */
12380 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12381 && (lhs_type == byte_type_node || lhs_type == char_type_node
12382 || lhs_type == short_type_node))
12383 {
12384 if (int_fits_type_p (rhs, lhs_type))
12385 new_rhs = convert (lhs_type, rhs);
12386 else if (wfl_op1) /* Might be called with a NULL */
12387 parse_warning_context
7e51098e 12388 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12389 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12390 /* Reported a warning that will turn into an error further
12391 down, so we don't return */
12392 }
12393
12394 return new_rhs;
12395}
12396
12397/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12398 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12399 0 is the conversion test fails. This implements parts the method
12400 invocation convertion (5.3). */
12401
12402static int
12403valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12404 tree lhs_type, rhs_type;
12405{
acd663ee 12406 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12407 if (lhs_type == rhs_type)
12408 return 1;
12409
7e51098e
TT
12410 /* Reject non primitive types and boolean conversions. */
12411 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12412 return 0;
12413
acd663ee
APB
12414 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12415 than a char can't be converted into a char. Short can't too, but
12416 the < test below takes care of that */
b67d701b
PB
12417 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12418 return 0;
12419
5e942c50
APB
12420 /* Accept all promoted type here. Note, we can't use <= in the test
12421 below, because we still need to bounce out assignments of short
12422 to char and the likes */
12423 if (lhs_type == int_type_node
12424 && (rhs_type == promoted_byte_type_node
12425 || rhs_type == promoted_short_type_node
12426 || rhs_type == promoted_char_type_node
12427 || rhs_type == promoted_boolean_type_node))
12428 return 1;
12429
acd663ee
APB
12430 /* From here, an integral is widened if its precision is smaller
12431 than the precision of the LHS or if the LHS is a floating point
12432 type, or the RHS is a float and the RHS a double. */
12433 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12434 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12435 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12436 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12437 return 1;
12438
12439 return 0;
e04a16fb
AG
12440}
12441
12442/* Check that something of SOURCE type can be assigned or cast to
12443 something of DEST type at runtime. Return 1 if the operation is
12444 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12445 were SOURCE is cast into DEST, which borrows a lot of the
12446 assignment check. */
12447
12448static int
12449valid_ref_assignconv_cast_p (source, dest, cast)
12450 tree source;
12451 tree dest;
12452 int cast;
12453{
09ed0f70
APB
12454 /* SOURCE or DEST might be null if not from a declared entity. */
12455 if (!source || !dest)
12456 return 0;
5e942c50
APB
12457 if (JNULLP_TYPE_P (source))
12458 return 1;
e04a16fb
AG
12459 if (TREE_CODE (source) == POINTER_TYPE)
12460 source = TREE_TYPE (source);
12461 if (TREE_CODE (dest) == POINTER_TYPE)
12462 dest = TREE_TYPE (dest);
12463 /* Case where SOURCE is a class type */
12464 if (TYPE_CLASS_P (source))
12465 {
12466 if (TYPE_CLASS_P (dest))
c2952b01
APB
12467 return (source == dest
12468 || inherits_from_p (source, dest)
c2952b01 12469 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12470 if (TYPE_INTERFACE_P (dest))
12471 {
12472 /* If doing a cast and SOURCE is final, the operation is
12473 always correct a compile time (because even if SOURCE
12474 does not implement DEST, a subclass of SOURCE might). */
12475 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12476 return 1;
12477 /* Otherwise, SOURCE must implement DEST */
12478 return interface_of_p (dest, source);
12479 }
12480 /* DEST is an array, cast permited if SOURCE is of Object type */
12481 return (cast && source == object_type_node ? 1 : 0);
12482 }
12483 if (TYPE_INTERFACE_P (source))
12484 {
12485 if (TYPE_CLASS_P (dest))
12486 {
12487 /* If not casting, DEST must be the Object type */
12488 if (!cast)
12489 return dest == object_type_node;
12490 /* We're doing a cast. The cast is always valid is class
12491 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12492 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12493 return 1;
12494 else
12495 return interface_of_p (source, dest);
12496 }
12497 if (TYPE_INTERFACE_P (dest))
12498 {
12499 /* If doing a cast, then if SOURCE and DEST contain method
12500 with the same signature but different return type, then
12501 this is a (compile time) error */
12502 if (cast)
12503 {
12504 tree method_source, method_dest;
12505 tree source_type;
0a2138e2 12506 tree source_sig;
e04a16fb
AG
12507 tree source_name;
12508 for (method_source = TYPE_METHODS (source); method_source;
12509 method_source = TREE_CHAIN (method_source))
12510 {
12511 source_sig =
12512 build_java_argument_signature (TREE_TYPE (method_source));
12513 source_type = TREE_TYPE (TREE_TYPE (method_source));
12514 source_name = DECL_NAME (method_source);
12515 for (method_dest = TYPE_METHODS (dest);
12516 method_dest; method_dest = TREE_CHAIN (method_dest))
12517 if (source_sig ==
12518 build_java_argument_signature (TREE_TYPE (method_dest))
12519 && source_name == DECL_NAME (method_dest)
12520 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12521 return 0;
12522 }
12523 return 1;
12524 }
12525 else
12526 return source == dest || interface_of_p (dest, source);
12527 }
ee17a290
TT
12528 else
12529 {
12530 /* Array */
12531 return (cast
12532 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12533 || (DECL_NAME (TYPE_NAME (source))
12534 == java_io_serializable)));
12535 }
e04a16fb
AG
12536 }
12537 if (TYPE_ARRAY_P (source))
12538 {
12539 if (TYPE_CLASS_P (dest))
12540 return dest == object_type_node;
09ed0f70 12541 /* Can't cast an array to an interface unless the interface is
ee17a290 12542 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12543 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12544 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12545 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12546 else /* Arrays */
12547 {
12548 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12549 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12550
b9f7e36c
APB
12551 /* In case of severe errors, they turn out null */
12552 if (!dest_element_type || !source_element_type)
12553 return 0;
e04a16fb
AG
12554 if (source_element_type == dest_element_type)
12555 return 1;
12556 return valid_ref_assignconv_cast_p (source_element_type,
12557 dest_element_type, cast);
12558 }
12559 return 0;
12560 }
12561 return 0;
12562}
12563
b67d701b
PB
12564static int
12565valid_cast_to_p (source, dest)
12566 tree source;
12567 tree dest;
12568{
12569 if (TREE_CODE (source) == POINTER_TYPE)
12570 source = TREE_TYPE (source);
12571 if (TREE_CODE (dest) == POINTER_TYPE)
12572 dest = TREE_TYPE (dest);
12573
12574 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12575 return valid_ref_assignconv_cast_p (source, dest, 1);
12576
12577 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12578 return 1;
12579
7e51098e
TT
12580 else if (TREE_CODE (source) == BOOLEAN_TYPE
12581 && TREE_CODE (dest) == BOOLEAN_TYPE)
12582 return 1;
12583
b67d701b
PB
12584 return 0;
12585}
12586
15fdcfe9
PB
12587static tree
12588do_unary_numeric_promotion (arg)
12589 tree arg;
12590{
12591 tree type = TREE_TYPE (arg);
7e51098e
TT
12592 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12593 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12594 arg = convert (int_type_node, arg);
12595 return arg;
12596}
12597
acd663ee
APB
12598/* Return a non zero value if SOURCE can be converted into DEST using
12599 the method invocation conversion rule (5.3). */
b67d701b
PB
12600static int
12601valid_method_invocation_conversion_p (dest, source)
12602 tree dest, source;
12603{
e3884b71 12604 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12605 && valid_builtin_assignconv_identity_widening_p (dest, source))
12606 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12607 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12608 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12609}
12610
e04a16fb
AG
12611/* Build an incomplete binop expression. */
12612
12613static tree
12614build_binop (op, op_location, op1, op2)
12615 enum tree_code op;
12616 int op_location;
12617 tree op1, op2;
12618{
5e942c50 12619 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12620 TREE_SIDE_EFFECTS (binop) = 1;
12621 /* Store the location of the operator, for better error report. The
12622 string of the operator will be rebuild based on the OP value. */
12623 EXPR_WFL_LINECOL (binop) = op_location;
12624 return binop;
12625}
12626
12627/* Build the string of the operator retained by NODE. If NODE is part
12628 of a compound expression, add an '=' at the end of the string. This
12629 function is called when an error needs to be reported on an
12630 operator. The string is returned as a pointer to a static character
12631 buffer. */
12632
12633static char *
12634operator_string (node)
12635 tree node;
12636{
12637#define BUILD_OPERATOR_STRING(S) \
12638 { \
12639 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12640 return buffer; \
12641 }
12642
12643 static char buffer [10];
12644 switch (TREE_CODE (node))
12645 {
12646 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12647 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12648 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12649 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12650 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12651 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12652 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12653 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12654 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12655 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12656 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12657 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12658 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12659 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12660 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12661 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12662 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12663 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12664 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12665 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12666 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12667 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12668 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12669 case PREINCREMENT_EXPR: /* Fall through */
12670 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12671 case PREDECREMENT_EXPR: /* Fall through */
12672 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12673 default:
12674 fatal ("unregistered operator %s - operator_string",
12675 tree_code_name [TREE_CODE (node)]);
12676 }
12677 return NULL;
12678#undef BUILD_OPERATOR_STRING
12679}
12680
5cbdba64
APB
12681/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12682
12683static int
12684java_decl_equiv (var_acc1, var_acc2)
12685 tree var_acc1, var_acc2;
12686{
12687 if (JDECL_P (var_acc1))
12688 return (var_acc1 == var_acc2);
12689
12690 return (TREE_CODE (var_acc1) == COMPONENT_REF
12691 && TREE_CODE (var_acc2) == COMPONENT_REF
12692 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12693 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12694 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12695}
12696
12697/* Return a non zero value if CODE is one of the operators that can be
12698 used in conjunction with the `=' operator in a compound assignment. */
12699
12700static int
12701binop_compound_p (code)
12702 enum tree_code code;
12703{
12704 int i;
12705 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12706 if (binop_lookup [i] == code)
12707 break;
12708
12709 return i < BINOP_COMPOUND_CANDIDATES;
12710}
12711
12712/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12713
12714static tree
12715java_refold (t)
12716 tree t;
12717{
12718 tree c, b, ns, decl;
12719
12720 if (TREE_CODE (t) != MODIFY_EXPR)
12721 return t;
12722
12723 c = TREE_OPERAND (t, 1);
12724 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12725 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12726 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12727 return t;
12728
12729 /* Now the left branch of the binary operator. */
12730 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12731 if (! (b && TREE_CODE (b) == NOP_EXPR
12732 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12733 return t;
12734
12735 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12736 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12737 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12738 return t;
12739
12740 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12741 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12742 /* It's got to be the an equivalent decl */
12743 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12744 {
12745 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12746 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12747 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12748 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12749 /* Change the right part of the BINOP_EXPR */
12750 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12751 }
12752
12753 return t;
12754}
12755
e04a16fb
AG
12756/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12757 errors but we modify NODE so that it contains the type computed
12758 according to the expression, when it's fixed. Otherwise, we write
12759 error_mark_node as the type. It allows us to further the analysis
12760 of remaining nodes and detects more errors in certain cases. */
12761
12762static tree
12763patch_binop (node, wfl_op1, wfl_op2)
12764 tree node;
12765 tree wfl_op1;
12766 tree wfl_op2;
12767{
12768 tree op1 = TREE_OPERAND (node, 0);
12769 tree op2 = TREE_OPERAND (node, 1);
12770 tree op1_type = TREE_TYPE (op1);
12771 tree op2_type = TREE_TYPE (op2);
48a840d9 12772 tree prom_type = NULL_TREE, cn;
e04a16fb 12773 int code = TREE_CODE (node);
b67d701b 12774
e04a16fb
AG
12775 /* If 1, tell the routine that we have to return error_mark_node
12776 after checking for the initialization of the RHS */
12777 int error_found = 0;
12778
e04a16fb
AG
12779 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12780
e04a16fb
AG
12781 switch (code)
12782 {
12783 /* 15.16 Multiplicative operators */
12784 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12785 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12786 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 12787 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
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 /* Change the division operator if necessary */
12800 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12801 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12802
aa4759c1
AH
12803 if (TREE_CODE (prom_type) == INTEGER_TYPE
12804 && flag_use_divide_subroutine
12805 && ! flag_emit_class_files
12806 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12807 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12808
0b4d333e
APB
12809 /* This one is more complicated. FLOATs are processed by a
12810 function call to soft_fmod. Duplicate the value of the
12811 COMPOUND_ASSIGN_P flag. */
e04a16fb 12812 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12813 {
12814 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12815 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12816 TREE_SIDE_EFFECTS (mod)
12817 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12818 return mod;
12819 }
e04a16fb
AG
12820 break;
12821
12822 /* 15.17 Additive Operators */
12823 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12824
12825 /* Operation is valid if either one argument is a string
12826 constant, a String object or a StringBuffer crafted for the
12827 purpose of the a previous usage of the String concatenation
12828 operator */
12829
12830 if (TREE_CODE (op1) == STRING_CST
12831 || TREE_CODE (op2) == STRING_CST
12832 || JSTRING_TYPE_P (op1_type)
12833 || JSTRING_TYPE_P (op2_type)
12834 || IS_CRAFTED_STRING_BUFFER_P (op1)
12835 || IS_CRAFTED_STRING_BUFFER_P (op2))
12836 return build_string_concatenation (op1, op2);
12837
e04a16fb
AG
12838 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12839 Numeric Types */
7e51098e 12840 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 12841 {
7e51098e 12842 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 12843 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 12844 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
12845 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12846 TREE_TYPE (node) = error_mark_node;
12847 error_found = 1;
12848 break;
12849 }
12850 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12851 break;
12852
12853 /* 15.18 Shift Operators */
12854 case LSHIFT_EXPR:
12855 case RSHIFT_EXPR:
12856 case URSHIFT_EXPR:
12857 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12858 {
12859 if (!JINTEGRAL_TYPE_P (op1_type))
12860 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12861 else
1ebadc60 12862 {
7e51098e 12863 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 12864 parse_error_context (wfl_operator,
781b0558 12865 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12866 operator_string (node),
12867 lang_printable_name (op2_type, 0));
12868 else
781b0558
KG
12869 parse_error_context (wfl_operator,
12870 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12871 operator_string (node),
12872 lang_printable_name (op2_type, 0));
12873 }
e04a16fb
AG
12874 TREE_TYPE (node) = error_mark_node;
12875 error_found = 1;
12876 break;
12877 }
12878
12879 /* Unary numeric promotion (5.6.1) is performed on each operand
12880 separatly */
15fdcfe9
PB
12881 op1 = do_unary_numeric_promotion (op1);
12882 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12883
12884 /* The type of the shift expression is the type of the promoted
12885 type of the left-hand operand */
12886 prom_type = TREE_TYPE (op1);
12887
c2952b01
APB
12888 /* Shift int only up to 0x1f and long up to 0x3f */
12889 if (prom_type == int_type_node)
12890 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12891 build_int_2 (0x1f, 0)));
12892 else
12893 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12894 build_int_2 (0x3f, 0)));
e04a16fb
AG
12895
12896 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12897 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12898 {
0b4d333e 12899 tree to_return;
73333a87
AH
12900 tree utype = unsigned_type (prom_type);
12901 op1 = convert (utype, op1);
e04a16fb 12902 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12903 TREE_OPERAND (node, 0) = op1;
12904 TREE_OPERAND (node, 1) = op2;
12905 TREE_TYPE (node) = utype;
0b4d333e
APB
12906 to_return = convert (prom_type, node);
12907 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12908 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12909 TREE_SIDE_EFFECTS (to_return)
12910 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12911 return to_return;
e04a16fb
AG
12912 }
12913 break;
5e942c50
APB
12914
12915 /* 15.19.1 Type Comparison Operator instaceof */
12916 case INSTANCEOF_EXPR:
12917
12918 TREE_TYPE (node) = boolean_type_node;
12919
12920 if (!(op2_type = resolve_type_during_patch (op2)))
12921 return error_mark_node;
12922
12923 /* The first operand must be a reference type or the null type */
12924 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12925 error_found = 1; /* Error reported further below */
12926
12927 /* The second operand must be a reference type */
12928 if (!JREFERENCE_TYPE_P (op2_type))
12929 {
12930 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12931 parse_error_context
12932 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12933 lang_printable_name (op2_type, 0));
12934 error_found = 1;
12935 }
12936
12937 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12938 {
12939 /* If the first operand is null, the result is always false */
12940 if (op1 == null_pointer_node)
12941 return boolean_false_node;
15fdcfe9
PB
12942 else if (flag_emit_class_files)
12943 {
12944 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12945 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12946 return node;
12947 }
5e942c50
APB
12948 /* Otherwise we have to invoke instance of to figure it out */
12949 else
67db0ce7 12950 return build_instanceof (op1, op2_type);
5e942c50
APB
12951 }
12952 /* There is no way the expression operand can be an instance of
12953 the type operand. This is a compile time error. */
12954 else
12955 {
c2e3db92 12956 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12957 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12958 parse_error_context
12959 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12960 t1, lang_printable_name (op2_type, 0));
12961 free (t1);
12962 error_found = 1;
12963 }
e04a16fb 12964
5e942c50 12965 break;
e04a16fb
AG
12966
12967 /* 15.21 Bitwise and Logical Operators */
12968 case BIT_AND_EXPR:
12969 case BIT_XOR_EXPR:
12970 case BIT_IOR_EXPR:
12971 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12972 /* Binary numeric promotion is performed on both operand and the
12973 expression retain that type */
12974 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12975
12976 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12977 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12978 /* The type of the bitwise operator expression is BOOLEAN */
12979 prom_type = boolean_type_node;
12980 else
12981 {
12982 if (!JINTEGRAL_TYPE_P (op1_type))
12983 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12984 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12985 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12986 TREE_TYPE (node) = error_mark_node;
12987 error_found = 1;
12988 /* Insert a break here if adding thing before the switch's
12989 break for this case */
12990 }
12991 break;
12992
12993 /* 15.22 Conditional-And Operator */
12994 case TRUTH_ANDIF_EXPR:
12995 /* 15.23 Conditional-Or Operator */
12996 case TRUTH_ORIF_EXPR:
12997 /* Operands must be of BOOLEAN type */
12998 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12999 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13000 {
13001 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13002 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13003 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13004 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13005 TREE_TYPE (node) = boolean_type_node;
13006 error_found = 1;
13007 break;
13008 }
13009 /* The type of the conditional operators is BOOLEAN */
13010 prom_type = boolean_type_node;
13011 break;
13012
13013 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13014 case LT_EXPR:
13015 case GT_EXPR:
13016 case LE_EXPR:
13017 case GE_EXPR:
13018 /* The type of each of the operands must be a primitive numeric
13019 type */
13020 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13021 {
13022 if (!JNUMERIC_TYPE_P (op1_type))
13023 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13024 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13025 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13026 TREE_TYPE (node) = boolean_type_node;
13027 error_found = 1;
13028 break;
13029 }
13030 /* Binary numeric promotion is performed on the operands */
13031 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13032 /* The type of the relation expression is always BOOLEAN */
13033 prom_type = boolean_type_node;
13034 break;
13035
13036 /* 15.20 Equality Operator */
13037 case EQ_EXPR:
13038 case NE_EXPR:
48a840d9
APB
13039 /* It's time for us to patch the strings. */
13040 if ((cn = patch_string (op1)))
13041 {
13042 op1 = cn;
13043 op1_type = TREE_TYPE (op1);
13044 }
13045 if ((cn = patch_string (op2)))
13046 {
13047 op2 = cn;
13048 op2_type = TREE_TYPE (op2);
13049 }
13050
e04a16fb
AG
13051 /* 15.20.1 Numerical Equality Operators == and != */
13052 /* Binary numeric promotion is performed on the operands */
5e942c50 13053 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13054 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13055
13056 /* 15.20.2 Boolean Equality Operators == and != */
13057 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13058 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13059 ; /* Nothing to do here */
13060
13061 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13062 /* Types have to be either references or the null type. If
13063 they're references, it must be possible to convert either
13064 type to the other by casting conversion. */
b9f7e36c
APB
13065 else if (op1 == null_pointer_node || op2 == null_pointer_node
13066 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13067 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13068 || valid_ref_assignconv_cast_p (op2_type,
13069 op1_type, 1))))
e04a16fb
AG
13070 ; /* Nothing to do here */
13071
13072 /* Else we have an error figure what can't be converted into
13073 what and report the error */
13074 else
13075 {
13076 char *t1;
c2e3db92 13077 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13078 parse_error_context
781b0558
KG
13079 (wfl_operator,
13080 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13081 operator_string (node), t1,
0a2138e2 13082 lang_printable_name (op2_type, 0));
e04a16fb
AG
13083 free (t1);
13084 TREE_TYPE (node) = boolean_type_node;
13085 error_found = 1;
13086 break;
13087 }
13088 prom_type = boolean_type_node;
13089 break;
13090 }
13091
e04a16fb
AG
13092 if (error_found)
13093 return error_mark_node;
13094
13095 TREE_OPERAND (node, 0) = op1;
13096 TREE_OPERAND (node, 1) = op2;
13097 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13098 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13099
ce6e9147
APB
13100 if (flag_emit_xref)
13101 return node;
13102
d1472141
PB
13103 /* fold does not respect side-effect order as required for Java but not C.
13104 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13105 * bytecode.
13106 */
13107 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13108 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13109 node = fold (node);
13110 return node;
e04a16fb
AG
13111}
13112
b67d701b
PB
13113/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13114 zero value, the value of CSTE comes after the valude of STRING */
13115
13116static tree
13117do_merge_string_cste (cste, string, string_len, after)
13118 tree cste;
49f48c71 13119 const char *string;
b67d701b
PB
13120 int string_len, after;
13121{
49f48c71 13122 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13123 int old_len = TREE_STRING_LENGTH (cste);
13124 int len = old_len + string_len;
520a57c8 13125 char *new = alloca (len+1);
354e99ce 13126
b67d701b
PB
13127 if (after)
13128 {
354e99ce
APB
13129 memcpy (new, string, string_len);
13130 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13131 }
13132 else
13133 {
354e99ce
APB
13134 memcpy (new, old, old_len);
13135 memcpy (&new [old_len], string, string_len);
b67d701b 13136 }
354e99ce 13137 new [len] = '\0';
520a57c8 13138 return build_string (len, new);
b67d701b
PB
13139}
13140
13141/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13142 new STRING_CST on success, NULL_TREE on failure */
13143
13144static tree
13145merge_string_cste (op1, op2, after)
13146 tree op1, op2;
13147 int after;
13148{
13149 /* Handle two string constants right away */
13150 if (TREE_CODE (op2) == STRING_CST)
13151 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13152 TREE_STRING_LENGTH (op2), after);
13153
13154 /* Reasonable integer constant can be treated right away */
13155 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13156 {
49f48c71
KG
13157 static const char *boolean_true = "true";
13158 static const char *boolean_false = "false";
13159 static const char *null_pointer = "null";
b67d701b 13160 char ch[3];
49f48c71 13161 const char *string;
b67d701b
PB
13162
13163 if (op2 == boolean_true_node)
13164 string = boolean_true;
13165 else if (op2 == boolean_false_node)
13166 string = boolean_false;
13167 else if (op2 == null_pointer_node)
13168 string = null_pointer;
13169 else if (TREE_TYPE (op2) == char_type_node)
13170 {
13171 ch[0] = (char )TREE_INT_CST_LOW (op2);
13172 ch[1] = '\0';
13173 string = ch;
13174 }
13175 else
13176 string = print_int_node (op2);
13177
13178 return do_merge_string_cste (op1, string, strlen (string), after);
13179 }
13180 return NULL_TREE;
13181}
13182
13183/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13184 has to be a STRING_CST and the other part must be a STRING_CST or a
13185 INTEGRAL constant. Return a new STRING_CST if the operation
13186 succeed, NULL_TREE otherwise.
13187
13188 If the case we want to optimize for space, we might want to return
13189 NULL_TREE for each invocation of this routine. FIXME */
13190
13191static tree
13192string_constant_concatenation (op1, op2)
13193 tree op1, op2;
13194{
13195 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13196 {
0a2138e2 13197 tree string, rest;
b67d701b
PB
13198 int invert;
13199
13200 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13201 rest = (string == op1 ? op2 : op1);
13202 invert = (string == op1 ? 0 : 1 );
13203
13204 /* Walk REST, only if it looks reasonable */
13205 if (TREE_CODE (rest) != STRING_CST
13206 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13207 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13208 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13209 {
13210 rest = java_complete_tree (rest);
13211 if (rest == error_mark_node)
13212 return error_mark_node;
13213 rest = fold (rest);
13214 }
13215 return merge_string_cste (string, rest, invert);
13216 }
13217 return NULL_TREE;
13218}
13219
13220/* Implement the `+' operator. Does static optimization if possible,
13221 otherwise create (if necessary) and append elements to a
13222 StringBuffer. The StringBuffer will be carried around until it is
13223 used for a function call or an assignment. Then toString() will be
13224 called on it to turn it into a String object. */
13225
13226static tree
13227build_string_concatenation (op1, op2)
13228 tree op1, op2;
13229{
13230 tree result;
dc0b3eff 13231 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13232
13233 if (flag_emit_xref)
13234 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13235
13236 /* Try to do some static optimization */
13237 if ((result = string_constant_concatenation (op1, op2)))
13238 return result;
13239
c0d87ff6
PB
13240 /* Discard empty strings on either side of the expression */
13241 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13242 {
13243 op1 = op2;
13244 op2 = NULL_TREE;
13245 }
c0d87ff6 13246 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13247 op2 = NULL_TREE;
b67d701b 13248
acd663ee 13249 /* If operands are string constant, turn then into object references */
b67d701b
PB
13250 if (TREE_CODE (op1) == STRING_CST)
13251 op1 = patch_string_cst (op1);
acd663ee 13252 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13253 op2 = patch_string_cst (op2);
13254
acd663ee
APB
13255 /* If either one of the constant is null and the other non null
13256 operand is a String object, return it. */
13257 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13258 return op1;
13259
b67d701b
PB
13260 /* If OP1 isn't already a StringBuffer, create and
13261 initialize a new one */
13262 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13263 {
13264 /* Two solutions here:
c52b5771
AG
13265 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13266 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13267 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13268 op1 = BUILD_STRING_BUFFER (op1);
13269 else
13270 {
13271 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13272 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13273 }
13274 }
13275
acd663ee
APB
13276 if (op2)
13277 {
13278 /* OP1 is no longer the last node holding a crafted StringBuffer */
13279 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13280 /* Create a node for `{new...,xxx}.append (op2)' */
13281 if (op2)
13282 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13283 }
13284
b67d701b
PB
13285 /* Mark the last node holding a crafted StringBuffer */
13286 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13287
13288 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13289 return op1;
13290}
13291
13292/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13293 StringBuffer. If no string were found to be patched, return
13294 NULL. */
13295
13296static tree
13297patch_string (node)
13298 tree node;
13299{
1179ebc2
APB
13300 if (node == error_mark_node)
13301 return error_mark_node;
b67d701b
PB
13302 if (TREE_CODE (node) == STRING_CST)
13303 return patch_string_cst (node);
13304 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13305 {
c877974e 13306 int saved = ctxp->explicit_constructor_p;
b67d701b 13307 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13308 tree ret;
13309 /* Temporary disable forbid the use of `this'. */
13310 ctxp->explicit_constructor_p = 0;
13311 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13312 /* String concatenation arguments must be evaluated in order too. */
13313 ret = force_evaluation_order (ret);
c877974e
APB
13314 /* Restore it at its previous value */
13315 ctxp->explicit_constructor_p = saved;
13316 return ret;
b67d701b
PB
13317 }
13318 return NULL_TREE;
13319}
13320
13321/* Build the internal representation of a string constant. */
13322
13323static tree
13324patch_string_cst (node)
13325 tree node;
13326{
13327 int location;
15fdcfe9
PB
13328 if (! flag_emit_class_files)
13329 {
15fdcfe9
PB
13330 node = get_identifier (TREE_STRING_POINTER (node));
13331 location = alloc_name_constant (CONSTANT_String, node);
13332 node = build_ref_from_constant_pool (location);
13333 }
cd9643f7 13334 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13335 TREE_CONSTANT (node) = 1;
13336 return node;
13337}
13338
13339/* Build an incomplete unary operator expression. */
e04a16fb
AG
13340
13341static tree
13342build_unaryop (op_token, op_location, op1)
13343 int op_token, op_location;
13344 tree op1;
13345{
13346 enum tree_code op;
13347 tree unaryop;
13348 switch (op_token)
13349 {
b67d701b 13350 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13351 case MINUS_TK: op = NEGATE_EXPR; break;
13352 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13353 case NOT_TK: op = BIT_NOT_EXPR; break;
13354 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13355 op_token);
13356 }
13357
13358 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13359 TREE_SIDE_EFFECTS (unaryop) = 1;
13360 /* Store the location of the operator, for better error report. The
13361 string of the operator will be rebuild based on the OP value. */
13362 EXPR_WFL_LINECOL (unaryop) = op_location;
13363 return unaryop;
13364}
13365
13366/* Special case for the ++/-- operators, since they require an extra
13367 argument to build, which is set to NULL and patched
13368 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13369
13370static tree
13371build_incdec (op_token, op_location, op1, is_post_p)
13372 int op_token, op_location;
13373 tree op1;
13374 int is_post_p;
13375{
13376 static enum tree_code lookup [2][2] =
13377 {
13378 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13379 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13380 };
13381 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13382 NULL_TREE, op1, NULL_TREE);
13383 TREE_SIDE_EFFECTS (node) = 1;
13384 /* Store the location of the operator, for better error report. The
13385 string of the operator will be rebuild based on the OP value. */
13386 EXPR_WFL_LINECOL (node) = op_location;
13387 return node;
13388}
13389
13390/* Build an incomplete cast operator, based on the use of the
13391 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13392 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13393 though its type is already set. */
13394
13395static tree
13396build_cast (location, type, exp)
13397 int location;
13398 tree type, exp;
13399{
13400 tree node = build1 (CONVERT_EXPR, type, exp);
13401 EXPR_WFL_LINECOL (node) = location;
13402 return node;
13403}
13404
c2952b01
APB
13405/* Build an incomplete class reference operator. */
13406static tree
13407build_incomplete_class_ref (location, class_name)
13408 int location;
13409 tree class_name;
13410{
13411 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13412 EXPR_WFL_LINECOL (node) = location;
13413 return node;
13414}
13415
13416/* Complete an incomplete class reference operator. */
13417static tree
13418patch_incomplete_class_ref (node)
13419 tree node;
13420{
13421 tree type = TREE_OPERAND (node, 0);
13422 tree ref_type;
13423
13424 if (!(ref_type = resolve_type_during_patch (type)))
13425 return error_mark_node;
13426
165f37bc 13427 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13428 {
13429 /* A class referenced by `foo.class' is initialized. */
13430 return build_class_init (ref_type, build_class_ref (ref_type));
13431 }
165f37bc
APB
13432
13433 /* If we're emitting class files and we have to deal with non
13434 primitive types, we invoke (and consider generating) the
13435 synthetic static method `class$'. */
13436 if (!TYPE_DOT_CLASS (current_class))
13437 build_dot_class_method (current_class);
f0f3a777 13438 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13439 return java_complete_tree (ref_type);
c2952b01
APB
13440}
13441
e04a16fb
AG
13442/* 15.14 Unary operators. We return error_mark_node in case of error,
13443 but preserve the type of NODE if the type is fixed. */
13444
13445static tree
13446patch_unaryop (node, wfl_op)
13447 tree node;
13448 tree wfl_op;
13449{
13450 tree op = TREE_OPERAND (node, 0);
13451 tree op_type = TREE_TYPE (op);
ab3a6dd6 13452 tree prom_type = NULL_TREE, value, decl;
c2952b01 13453 int outer_field_flag = 0;
e04a16fb
AG
13454 int code = TREE_CODE (node);
13455 int error_found = 0;
13456
13457 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13458
13459 switch (code)
13460 {
13461 /* 15.13.2 Postfix Increment Operator ++ */
13462 case POSTINCREMENT_EXPR:
13463 /* 15.13.3 Postfix Increment Operator -- */
13464 case POSTDECREMENT_EXPR:
13465 /* 15.14.1 Prefix Increment Operator ++ */
13466 case PREINCREMENT_EXPR:
13467 /* 15.14.2 Prefix Decrement Operator -- */
13468 case PREDECREMENT_EXPR:
5cbdba64 13469 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13470 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13471 /* We might be trying to change an outer field accessed using
13472 access method. */
13473 if (outer_field_flag)
13474 {
13475 /* Retrieve the decl of the field we're trying to access. We
13476 do that by first retrieving the function we would call to
13477 access the field. It has been already verified that this
13478 field isn't final */
13479 if (flag_emit_class_files)
13480 decl = TREE_OPERAND (op, 0);
13481 else
13482 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13483 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13484 }
b3edebcf 13485 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13486 else if (!JDECL_P (decl)
b3edebcf
APB
13487 && TREE_CODE (decl) != COMPONENT_REF
13488 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13489 && TREE_CODE (decl) != INDIRECT_REF
13490 && !(TREE_CODE (decl) == COMPOUND_EXPR
13491 && TREE_OPERAND (decl, 1)
13492 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13493 {
5e942c50
APB
13494 tree lvalue;
13495 /* Before screaming, check that we're not in fact trying to
13496 increment a optimized static final access, in which case
13497 we issue an different error message. */
13498 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13499 && resolve_expression_name (wfl_op, &lvalue)
13500 && check_final_assignment (lvalue, wfl_op)))
13501 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13502 operator_string (node));
e04a16fb
AG
13503 TREE_TYPE (node) = error_mark_node;
13504 error_found = 1;
13505 }
c2952b01
APB
13506
13507 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13508 error_found = 1;
13509
e04a16fb
AG
13510 /* From now on, we know that op if a variable and that it has a
13511 valid wfl. We use wfl_op to locate errors related to the
13512 ++/-- operand. */
13513 else if (!JNUMERIC_TYPE_P (op_type))
13514 {
13515 parse_error_context
13516 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13517 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13518 TREE_TYPE (node) = error_mark_node;
13519 error_found = 1;
13520 }
13521 else
13522 {
4a5f66c3 13523 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13524 both operands, if really necessary */
13525 if (JINTEGRAL_TYPE_P (op_type))
13526 {
13527 value = build_int_2 (1, 0);
13528 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13529 }
13530 else
13531 {
13532 value = build_int_2 (1, 0);
13533 TREE_TYPE (node) =
13534 binary_numeric_promotion (op_type,
13535 TREE_TYPE (value), &op, &value);
13536 }
c2952b01
APB
13537
13538 /* We remember we might be accessing an outer field */
13539 if (outer_field_flag)
13540 {
13541 /* We re-generate an access to the field */
13542 value = build (PLUS_EXPR, TREE_TYPE (op),
13543 build_outer_field_access (wfl_op, decl), value);
13544
13545 /* And we patch the original access$() into a write
13546 with plus_op as a rhs */
13547 return outer_field_access_fix (node, op, value);
13548 }
13549
5cbdba64 13550 /* And write back into the node. */
4a5f66c3 13551 TREE_OPERAND (node, 0) = op;
e04a16fb 13552 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13553 /* Convert the overall back into its original type, if
13554 necessary, and return */
13555 if (JINTEGRAL_TYPE_P (op_type))
13556 return fold (node);
13557 else
13558 return fold (convert (op_type, node));
e04a16fb
AG
13559 }
13560 break;
13561
13562 /* 15.14.3 Unary Plus Operator + */
b67d701b 13563 case UNARY_PLUS_EXPR:
e04a16fb
AG
13564 /* 15.14.4 Unary Minus Operator - */
13565 case NEGATE_EXPR:
13566 if (!JNUMERIC_TYPE_P (op_type))
13567 {
13568 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13569 TREE_TYPE (node) = error_mark_node;
13570 error_found = 1;
13571 }
13572 /* Unary numeric promotion is performed on operand */
13573 else
13574 {
15fdcfe9
PB
13575 op = do_unary_numeric_promotion (op);
13576 prom_type = TREE_TYPE (op);
b67d701b 13577 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13578 return fold (op);
e04a16fb
AG
13579 }
13580 break;
13581
13582 /* 15.14.5 Bitwise Complement Operator ~ */
13583 case BIT_NOT_EXPR:
13584 if (!JINTEGRAL_TYPE_P (op_type))
13585 {
13586 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13587 TREE_TYPE (node) = error_mark_node;
13588 error_found = 1;
13589 }
13590 else
13591 {
15fdcfe9
PB
13592 op = do_unary_numeric_promotion (op);
13593 prom_type = TREE_TYPE (op);
e04a16fb
AG
13594 }
13595 break;
13596
13597 /* 15.14.6 Logical Complement Operator ! */
13598 case TRUTH_NOT_EXPR:
13599 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13600 {
13601 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13602 /* But the type is known. We will report an error if further
13603 attempt of a assignment is made with this rhs */
e04a16fb
AG
13604 TREE_TYPE (node) = boolean_type_node;
13605 error_found = 1;
13606 }
13607 else
13608 prom_type = boolean_type_node;
13609 break;
13610
13611 /* 15.15 Cast Expression */
13612 case CONVERT_EXPR:
0a2138e2 13613 value = patch_cast (node, wfl_operator);
e04a16fb 13614 if (value == error_mark_node)
c877974e
APB
13615 {
13616 /* If this cast is part of an assignment, we tell the code
13617 that deals with it not to complain about a mismatch,
13618 because things have been cast, anyways */
13619 TREE_TYPE (node) = error_mark_node;
13620 error_found = 1;
13621 }
13622 else
dc0b3eff
PB
13623 {
13624 value = fold (value);
13625 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13626 return value;
13627 }
e04a16fb
AG
13628 break;
13629 }
13630
e04a16fb
AG
13631 if (error_found)
13632 return error_mark_node;
4a5f66c3
APB
13633
13634 /* There are cases where node has been replaced by something else
13635 and we don't end up returning here: UNARY_PLUS_EXPR,
13636 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13637 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13638 TREE_TYPE (node) = prom_type;
dc0b3eff 13639 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13640 return fold (node);
13641}
13642
13643/* Generic type resolution that sometimes takes place during node
13644 patching. Returned the resolved type or generate an error
13645 message. Return the resolved type or NULL_TREE. */
13646
13647static tree
13648resolve_type_during_patch (type)
13649 tree type;
13650{
13651 if (unresolved_type_p (type, NULL))
13652 {
4142b247 13653 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13654 if (!type_decl)
13655 {
13656 parse_error_context (type,
13657 "Class `%s' not found in type declaration",
13658 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13659 return NULL_TREE;
13660 }
13661 else
5e942c50
APB
13662 {
13663 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13664 return TREE_TYPE (type_decl);
13665 }
e04a16fb
AG
13666 }
13667 return type;
13668}
13669/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13670 found. Otherwise NODE or something meant to replace it is returned. */
13671
13672static tree
19e223db 13673patch_cast (node, wfl_op)
e04a16fb 13674 tree node;
19e223db 13675 tree wfl_op;
e04a16fb
AG
13676{
13677 tree op = TREE_OPERAND (node, 0);
13678 tree op_type = TREE_TYPE (op);
13679 tree cast_type = TREE_TYPE (node);
13680 char *t1;
13681
13682 /* First resolve OP_TYPE if unresolved */
13683 if (!(cast_type = resolve_type_during_patch (cast_type)))
13684 return error_mark_node;
13685
13686 /* Check on cast that are proven correct at compile time */
13687 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13688 {
e04a16fb
AG
13689 /* Same type */
13690 if (cast_type == op_type)
13691 return node;
13692
0b4d333e
APB
13693 /* float and double type are converted to the original type main
13694 variant and then to the target type. */
13695 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13696 op = convert (integer_type_node, op);
13697
e04a16fb
AG
13698 /* Try widening/narowwing convertion. Potentially, things need
13699 to be worked out in gcc so we implement the extreme cases
13700 correctly. fold_convert() needs to be fixed. */
13701 return convert (cast_type, op);
13702 }
13703
0b4d333e
APB
13704 /* It's also valid to cast a boolean into a boolean */
13705 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13706 return node;
13707
5e942c50
APB
13708 /* null can be casted to references */
13709 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13710 return build_null_of_type (cast_type);
13711
e04a16fb
AG
13712 /* The remaining legal casts involve conversion between reference
13713 types. Check for their compile time correctness. */
13714 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13715 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13716 {
13717 TREE_TYPE (node) = promote_type (cast_type);
13718 /* Now, the case can be determined correct at compile time if
13719 OP_TYPE can be converted into CAST_TYPE by assignment
13720 conversion (5.2) */
13721
13722 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13723 {
13724 TREE_SET_CODE (node, NOP_EXPR);
13725 return node;
13726 }
13727
13728 if (flag_emit_class_files)
13729 {
13730 TREE_SET_CODE (node, CONVERT_EXPR);
13731 return node;
13732 }
e04a16fb
AG
13733
13734 /* The cast requires a run-time check */
13735 return build (CALL_EXPR, promote_type (cast_type),
13736 build_address_of (soft_checkcast_node),
13737 tree_cons (NULL_TREE, build_class_ref (cast_type),
13738 build_tree_list (NULL_TREE, op)),
13739 NULL_TREE);
13740 }
13741
13742 /* Any other casts are proven incorrect at compile time */
c2e3db92 13743 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13744 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13745 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13746 free (t1);
13747 return error_mark_node;
13748}
13749
5e942c50
APB
13750/* Build a null constant and give it the type TYPE. */
13751
13752static tree
13753build_null_of_type (type)
13754 tree type;
13755{
13756 tree node = build_int_2 (0, 0);
13757 TREE_TYPE (node) = promote_type (type);
13758 return node;
13759}
13760
e04a16fb
AG
13761/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13762 a list of indices. */
13763static tree
13764build_array_ref (location, array, index)
13765 int location;
13766 tree array, index;
13767{
13768 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13769 EXPR_WFL_LINECOL (node) = location;
13770 return node;
13771}
13772
13773/* 15.12 Array Access Expression */
13774
13775static tree
c877974e
APB
13776patch_array_ref (node)
13777 tree node;
e04a16fb
AG
13778{
13779 tree array = TREE_OPERAND (node, 0);
13780 tree array_type = TREE_TYPE (array);
13781 tree index = TREE_OPERAND (node, 1);
13782 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13783 int error_found = 0;
13784
13785 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13786
e04a16fb
AG
13787 if (TREE_CODE (array_type) == POINTER_TYPE)
13788 array_type = TREE_TYPE (array_type);
13789
13790 /* The array reference must be an array */
13791 if (!TYPE_ARRAY_P (array_type))
13792 {
13793 parse_error_context
781b0558
KG
13794 (wfl_operator,
13795 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13796 lang_printable_name (array_type, 0));
e04a16fb
AG
13797 TREE_TYPE (node) = error_mark_node;
13798 error_found = 1;
13799 }
13800
c2952b01 13801 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13802 type must be int */
15fdcfe9
PB
13803 index = do_unary_numeric_promotion (index);
13804 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13805 {
1ebadc60 13806 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13807 parse_error_context (wfl_operator,
13808 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13809 lang_printable_name (index_type, 0));
13810 else
781b0558
KG
13811 parse_error_context (wfl_operator,
13812 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13813 lang_printable_name (index_type, 0));
e04a16fb
AG
13814 TREE_TYPE (node) = error_mark_node;
13815 error_found = 1;
13816 }
13817
e04a16fb
AG
13818 if (error_found)
13819 return error_mark_node;
e04a16fb 13820
5e942c50 13821 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13822
7f1d4866 13823 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13824 {
15fdcfe9
PB
13825 TREE_OPERAND (node, 0) = array;
13826 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13827 }
13828 else
939d7216
PB
13829 {
13830 /* The save_expr is for correct evaluation order. It would be cleaner
13831 to use force_evaluation_order (see comment there), but that is
13832 difficult when we also have to deal with bounds checking. */
13833 if (TREE_SIDE_EFFECTS (index))
13834 array = save_expr (array);
13835 node = build_java_arrayaccess (array, array_type, index);
13836 if (TREE_SIDE_EFFECTS (index))
13837 node = build (COMPOUND_EXPR, array_type, array, node);
13838 }
e04a16fb
AG
13839 TREE_TYPE (node) = array_type;
13840 return node;
13841}
13842
13843/* 15.9 Array Creation Expressions */
13844
13845static tree
13846build_newarray_node (type, dims, extra_dims)
13847 tree type;
13848 tree dims;
13849 int extra_dims;
13850{
13851 tree node =
b67d701b 13852 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13853 build_int_2 (extra_dims, 0));
e04a16fb
AG
13854 return node;
13855}
13856
13857static tree
13858patch_newarray (node)
13859 tree node;
13860{
13861 tree type = TREE_OPERAND (node, 0);
13862 tree dims = TREE_OPERAND (node, 1);
13863 tree cdim, array_type;
13864 int error_found = 0;
13865 int ndims = 0;
13866 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13867
13868 /* Dimension types are verified. It's better for the types to be
13869 verified in order. */
13870 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13871 {
13872 int dim_error = 0;
13873 tree dim = TREE_VALUE (cdim);
13874
13875 /* Dim might have been saved during its evaluation */
dba41d30 13876 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
13877
13878 /* The type of each specified dimension must be an integral type. */
13879 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13880 dim_error = 1;
13881
13882 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13883 promoted type must be int. */
13884 else
13885 {
15fdcfe9 13886 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13887 if (TREE_TYPE (dim) != int_type_node)
13888 dim_error = 1;
13889 }
13890
13891 /* Report errors on types here */
13892 if (dim_error)
13893 {
13894 parse_error_context
13895 (TREE_PURPOSE (cdim),
781b0558 13896 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13897 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13898 "Explicit cast needed to" : "Can't"),
0a2138e2 13899 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13900 error_found = 1;
13901 }
13902
e04a16fb
AG
13903 TREE_PURPOSE (cdim) = NULL_TREE;
13904 }
13905
13906 /* Resolve array base type if unresolved */
13907 if (!(type = resolve_type_during_patch (type)))
13908 error_found = 1;
13909
13910 if (error_found)
13911 {
13912 /* We don't want further evaluation of this bogus array creation
13913 operation */
13914 TREE_TYPE (node) = error_mark_node;
13915 return error_mark_node;
13916 }
13917
15fdcfe9
PB
13918 /* Set array_type to the actual (promoted) array type of the result. */
13919 if (TREE_CODE (type) == RECORD_TYPE)
13920 type = build_pointer_type (type);
13921 while (--xdims >= 0)
13922 {
13923 type = promote_type (build_java_array_type (type, -1));
13924 }
13925 dims = nreverse (dims);
13926 array_type = type;
13927 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13928 {
13929 type = array_type;
05bccae2
RK
13930 array_type
13931 = build_java_array_type (type,
13932 TREE_CODE (cdim) == INTEGER_CST
13933 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13934 : -1);
15fdcfe9
PB
13935 array_type = promote_type (array_type);
13936 }
13937 dims = nreverse (dims);
13938
e04a16fb
AG
13939 /* The node is transformed into a function call. Things are done
13940 differently according to the number of dimensions. If the number
13941 of dimension is equal to 1, then the nature of the base type
13942 (primitive or not) matters. */
15fdcfe9 13943 if (ndims == 1)
fdec99c6 13944 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13945
e04a16fb
AG
13946 /* Can't reuse what's already written in expr.c because it uses the
13947 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13948 return build (CALL_EXPR, array_type,
e04a16fb 13949 build_address_of (soft_multianewarray_node),
15fdcfe9 13950 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13951 tree_cons (NULL_TREE,
15fdcfe9 13952 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13953 NULL_TREE);
13954}
13955
f8976021
APB
13956/* 10.6 Array initializer. */
13957
13958/* Build a wfl for array element that don't have one, so we can
13959 pin-point errors. */
13960
13961static tree
13962maybe_build_array_element_wfl (node)
13963 tree node;
13964{
13965 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13966 return build_expr_wfl (NULL_TREE, ctxp->filename,
13967 ctxp->elc.line, ctxp->elc.prev_col);
13968 else
13969 return NULL_TREE;
13970}
13971
13972/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13973 identification of initialized arrays easier to detect during walk
13974 and expansion. */
13975
13976static tree
13977build_new_array_init (location, values)
13978 int location;
13979 tree values;
13980{
13981 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13982 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13983 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13984 return to_return;
13985}
13986
13987/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13988 occurred. Otherwise return NODE after having set its type
13989 appropriately. */
13990
13991static tree
13992patch_new_array_init (type, node)
13993 tree type, node;
f8976021
APB
13994{
13995 int error_seen = 0;
fdec99c6 13996 tree current, element_type;
f8976021 13997 HOST_WIDE_INT length;
fdec99c6
PB
13998 int all_constant = 1;
13999 tree init = TREE_OPERAND (node, 0);
f8976021 14000
fdec99c6
PB
14001 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14002 {
14003 parse_error_context (node,
14004 "Invalid array initializer for non-array type `%s'",
14005 lang_printable_name (type, 1));
14006 return error_mark_node;
14007 }
14008 type = TREE_TYPE (type);
14009 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14010
fdec99c6
PB
14011 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14012
14013 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14014 current; length++, current = TREE_CHAIN (current))
f8976021 14015 {
fdec99c6
PB
14016 tree elt = TREE_VALUE (current);
14017 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14018 {
fdec99c6 14019 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14020 elt = TREE_VALUE (current);
14021 /* When compiling to native code, STRING_CST is converted to
14022 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14023 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14024 all_constant = 0;
f8976021 14025 }
fdec99c6
PB
14026 else
14027 {
14028 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14029 TREE_PURPOSE (current) = NULL_TREE;
14030 all_constant = 0;
14031 }
9a7ab4b3
APB
14032 if (elt && TREE_CODE (elt) == TREE_LIST
14033 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14034 error_seen = 1;
f8976021
APB
14035 }
14036
14037 if (error_seen)
14038 return error_mark_node;
14039
14040 /* Create a new type. We can't reuse the one we have here by
14041 patching its dimension because it originally is of dimension -1
14042 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14043 type = build_java_array_type (element_type, length);
14044 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14045 TREE_TYPE (node) = promote_type (type);
14046 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14047 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14048 return node;
14049}
14050
14051/* Verify that one entry of the initializer element list can be
14052 assigned to the array base type. Report 1 if an error occurred, 0
14053 otherwise. */
14054
14055static int
14056array_constructor_check_entry (type, entry)
14057 tree type, entry;
14058{
14059 char *array_type_string = NULL; /* For error reports */
14060 tree value, type_value, new_value, wfl_value, patched;
14061 int error_seen = 0;
14062
14063 new_value = NULL_TREE;
14064 wfl_value = TREE_VALUE (entry);
14065
f8976021 14066 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14067 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14068 if ((patched = patch_string (value)))
14069 value = patched;
1179ebc2
APB
14070 if (value == error_mark_node)
14071 return 1;
f8976021 14072
f8976021
APB
14073 type_value = TREE_TYPE (value);
14074
1179ebc2 14075 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14076 constant overflow during narrowing. */
14077 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14078 new_value = try_builtin_assignconv (wfl_operator, type, value);
14079 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14080 type_value = promote_type (type);
100f7cd8 14081
f8976021
APB
14082 /* Check and report errors */
14083 if (!new_value)
14084 {
49f48c71 14085 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14086 "Can't" : "Explicit cast needed to");
14087 if (!array_type_string)
c2e3db92 14088 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14089 parse_error_context
14090 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14091 msg, lang_printable_name (type_value, 1), array_type_string);
14092 error_seen = 1;
14093 }
14094
14095 if (new_value)
14096 {
b8c5b1c6 14097 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14098 TREE_VALUE (entry) = new_value;
14099 }
14100
14101 if (array_type_string)
14102 free (array_type_string);
14103
14104 TREE_PURPOSE (entry) = NULL_TREE;
14105 return error_seen;
14106}
14107
e04a16fb
AG
14108static tree
14109build_this (location)
14110 int location;
14111{
9ee9b555 14112 tree node = build_wfl_node (this_identifier_node);
b67d701b 14113 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14114 EXPR_WFL_LINECOL (node) = location;
14115 return node;
14116}
14117
14118/* 14.15 The return statement. It builds a modify expression that
14119 assigns the returned value to the RESULT_DECL that hold the value
14120 to be returned. */
14121
14122static tree
14123build_return (location, op)
14124 int location;
14125 tree op;
14126{
14127 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14128 EXPR_WFL_LINECOL (node) = location;
b67d701b 14129 node = build_debugable_stmt (location, node);
e04a16fb
AG
14130 return node;
14131}
14132
14133static tree
14134patch_return (node)
14135 tree node;
14136{
14137 tree return_exp = TREE_OPERAND (node, 0);
14138 tree meth = current_function_decl;
14139 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14140 int error_found = 0;
14141
14142 TREE_TYPE (node) = error_mark_node;
14143 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14144
14145 /* It's invalid to have a return value within a function that is
14146 declared with the keyword void or that is a constructor */
14147 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14148 error_found = 1;
14149
f099f336 14150 /* It's invalid to use a return statement in a static block */
c2952b01 14151 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14152 error_found = 1;
14153
e04a16fb
AG
14154 /* It's invalid to have a no return value within a function that
14155 isn't declared with the keyword `void' */
14156 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14157 error_found = 2;
c2952b01
APB
14158
14159 if (in_instance_initializer)
14160 error_found = 1;
e04a16fb
AG
14161
14162 if (error_found)
14163 {
c2952b01 14164 if (in_instance_initializer)
f099f336 14165 parse_error_context (wfl_operator,
c2952b01
APB
14166 "`return' inside instance initializer");
14167
14168 else if (DECL_CLINIT_P (current_function_decl))
14169 parse_error_context (wfl_operator,
14170 "`return' inside static initializer");
f099f336
APB
14171
14172 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14173 {
c2e3db92 14174 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14175 parse_error_context (wfl_operator,
14176 "`return' with%s value from `%s %s'",
14177 (error_found == 1 ? "" : "out"),
14178 t, lang_printable_name (meth, 0));
14179 free (t);
14180 }
14181 else
14182 parse_error_context (wfl_operator,
14183 "`return' with value from constructor `%s'",
14184 lang_printable_name (meth, 0));
e04a16fb
AG
14185 return error_mark_node;
14186 }
14187
5e942c50
APB
14188 /* If we have a return_exp, build a modify expression and expand
14189 it. Note: at that point, the assignment is declared valid, but we
14190 may want to carry some more hacks */
e04a16fb
AG
14191 if (return_exp)
14192 {
5e942c50
APB
14193 tree exp = java_complete_tree (return_exp);
14194 tree modify, patched;
14195
14196 /* If the function returned value and EXP are booleans, EXP has
14197 to be converted into the type of DECL_RESULT, which is integer
14198 (see complete_start_java_method) */
14199 if (TREE_TYPE (exp) == boolean_type_node &&
14200 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14201 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14202
14203 /* `null' can be assigned to a function returning a reference */
14204 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14205 exp == null_pointer_node)
14206 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14207
14208 if ((patched = patch_string (exp)))
14209 exp = patched;
14210
14211 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14212 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14213 modify = java_complete_tree (modify);
5e942c50 14214
e04a16fb
AG
14215 if (modify != error_mark_node)
14216 {
14217 TREE_SIDE_EFFECTS (modify) = 1;
14218 TREE_OPERAND (node, 0) = modify;
14219 }
14220 else
14221 return error_mark_node;
14222 }
14223 TREE_TYPE (node) = void_type_node;
14224 TREE_SIDE_EFFECTS (node) = 1;
14225 return node;
14226}
14227
14228/* 14.8 The if Statement */
14229
14230static tree
14231build_if_else_statement (location, expression, if_body, else_body)
14232 int location;
14233 tree expression, if_body, else_body;
14234{
14235 tree node;
e04a16fb 14236 if (!else_body)
9bbc7d9f 14237 else_body = empty_stmt_node;
e04a16fb
AG
14238 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14239 EXPR_WFL_LINECOL (node) = location;
b67d701b 14240 node = build_debugable_stmt (location, node);
e04a16fb
AG
14241 return node;
14242}
14243
14244static tree
14245patch_if_else_statement (node)
14246 tree node;
14247{
14248 tree expression = TREE_OPERAND (node, 0);
14249
14250 TREE_TYPE (node) = error_mark_node;
14251 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14252
14253 /* The type of expression must be boolean */
b67d701b
PB
14254 if (TREE_TYPE (expression) != boolean_type_node
14255 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14256 {
14257 parse_error_context
14258 (wfl_operator,
14259 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14260 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14261 return error_mark_node;
14262 }
14263
14264 TREE_TYPE (node) = void_type_node;
14265 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14266 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14267 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14268 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14269 return node;
14270}
14271
14272/* 14.6 Labeled Statements */
14273
14274/* Action taken when a lableled statement is parsed. a new
14275 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14276 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14277
14278static tree
0a2138e2 14279build_labeled_block (location, label)
e04a16fb 14280 int location;
0a2138e2 14281 tree label;
e04a16fb 14282{
b635eb2f 14283 tree label_name ;
e04a16fb 14284 tree label_decl, node;
b635eb2f
PB
14285 if (label == NULL_TREE || label == continue_identifier_node)
14286 label_name = label;
14287 else
e04a16fb 14288 {
b635eb2f
PB
14289 label_name = merge_qualified_name (label_id, label);
14290 /* Issue an error if we try to reuse a label that was previously
14291 declared */
14292 if (IDENTIFIER_LOCAL_VALUE (label_name))
14293 {
14294 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14295 parse_error_context (wfl_operator,
14296 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14297 IDENTIFIER_POINTER (label));
14298 EXPR_WFL_LINECOL (wfl_operator) =
14299 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14300 parse_error_context (wfl_operator,
14301 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14302 IDENTIFIER_POINTER (label));
14303 java_error_count--;
14304 }
e04a16fb
AG
14305 }
14306
14307 label_decl = create_label_decl (label_name);
14308 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14309 EXPR_WFL_LINECOL (node) = location;
14310 TREE_SIDE_EFFECTS (node) = 1;
14311 return node;
14312}
14313
b67d701b 14314/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14315
14316static tree
b635eb2f 14317finish_labeled_statement (lbe, statement)
e04a16fb
AG
14318 tree lbe; /* Labeled block expr */
14319 tree statement;
14320{
14321 /* In anyways, tie the loop to its statement */
14322 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14323 pop_labeled_block ();
14324 POP_LABELED_BLOCK ();
e04a16fb
AG
14325 return lbe;
14326}
14327
14328/* 14.10, 14.11, 14.12 Loop Statements */
14329
14330/* Create an empty LOOP_EXPR and make it the last in the nested loop
14331 list. */
14332
14333static tree
14334build_new_loop (loop_body)
14335 tree loop_body;
14336{
14337 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14338 TREE_SIDE_EFFECTS (loop) = 1;
14339 PUSH_LOOP (loop);
14340 return loop;
14341}
14342
14343/* Create a loop body according to the following structure:
14344 COMPOUND_EXPR
14345 COMPOUND_EXPR (loop main body)
14346 EXIT_EXPR (this order is for while/for loops.
14347 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14348 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14349 BODY end of this labeled block)
14350 INCREMENT (if any)
14351
14352 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14353 after the body, like in the do-while loop.
14354
14355 To obtain a loop, the loop body structure described above is
14356 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14357
14358 LABELED_BLOCK_EXPR
14359 LABEL_DECL (use this label to exit the loop)
14360 LOOP_EXPR
14361 <structure described above> */
e04a16fb
AG
14362
14363static tree
14364build_loop_body (location, condition, reversed)
14365 int location;
14366 tree condition;
14367 int reversed;
14368{
0a2138e2 14369 tree first, second, body;
e04a16fb
AG
14370
14371 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14372 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14373 condition = build_debugable_stmt (location, condition);
14374 TREE_SIDE_EFFECTS (condition) = 1;
14375
b635eb2f 14376 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14377 first = (reversed ? body : condition);
14378 second = (reversed ? condition : body);
14379 return
14380 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14381 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14382}
14383
14384/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14385 their order) on the current loop. Unlink the current loop from the
14386 loop list. */
14387
14388static tree
b635eb2f 14389finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14390 int location;
14391 tree condition, body;
14392 int reversed;
14393{
14394 tree to_return = ctxp->current_loop;
14395 tree loop_body = LOOP_EXPR_BODY (to_return);
14396 if (condition)
14397 {
14398 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14399 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14400 The real EXIT_EXPR is one operand further. */
14401 EXPR_WFL_LINECOL (cnode) = location;
14402 /* This one is for accurate error reports */
14403 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14404 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14405 }
14406 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14407 POP_LOOP ();
14408 return to_return;
14409}
14410
b635eb2f 14411/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14412 loops feature the condition part */
14413
14414static tree
b635eb2f 14415finish_for_loop (location, condition, update, body)
e04a16fb
AG
14416 int location;
14417 tree condition, update, body;
14418{
14419 /* Put the condition and the loop body in place */
b635eb2f 14420 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14421 /* LOOP is the current loop which has been now popped of the loop
14422 stack. Install the update block */
14423 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14424 return loop;
14425}
14426
5cbdba64
APB
14427/* Try to find the loop a block might be related to. This comprises
14428 the case where the LOOP_EXPR is found as the second operand of a
14429 COMPOUND_EXPR, because the loop happens to have an initialization
14430 part, then expressed as the first operand of the COMPOUND_EXPR. If
14431 the search finds something, 1 is returned. Otherwise, 0 is
14432 returned. The search is assumed to start from a
14433 LABELED_BLOCK_EXPR's block. */
14434
14435static tree
14436search_loop (statement)
14437 tree statement;
14438{
14439 if (TREE_CODE (statement) == LOOP_EXPR)
14440 return statement;
14441
14442 if (TREE_CODE (statement) == BLOCK)
14443 statement = BLOCK_SUBBLOCKS (statement);
14444 else
14445 return NULL_TREE;
14446
14447 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14448 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14449 statement = TREE_OPERAND (statement, 1);
14450
14451 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14452 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14453}
14454
14455/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14456 returned otherwise. */
14457
14458static int
14459labeled_block_contains_loop_p (block, loop)
14460 tree block, loop;
14461{
14462 if (!block)
14463 return 0;
14464
14465 if (LABELED_BLOCK_BODY (block) == loop)
14466 return 1;
14467
c2952b01 14468 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14469 return 1;
14470
14471 return 0;
14472}
14473
e04a16fb 14474/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14475 insert LOOP as its body. */
e04a16fb
AG
14476
14477static tree
14478patch_loop_statement (loop)
14479 tree loop;
14480{
cd9643f7 14481 tree loop_label;
5cbdba64 14482
cd9643f7 14483 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14484 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14485 return loop;
14486
cd9643f7 14487 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14488 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14489 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14490 LABELED_BLOCK_BODY (loop_label) = loop;
14491 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14492 return loop_label;
e04a16fb
AG
14493}
14494
14495/* 14.13, 14.14: break and continue Statements */
14496
14497/* Build a break or a continue statement. a null NAME indicates an
14498 unlabeled break/continue statement. */
14499
14500static tree
14501build_bc_statement (location, is_break, name)
14502 int location, is_break;
14503 tree name;
14504{
14505 tree break_continue, label_block_expr = NULL_TREE;
14506
14507 if (name)
14508 {
14509 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14510 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14511 /* Null means that we don't have a target for this named
14512 break/continue. In this case, we make the target to be the
14513 label name, so that the error can be reported accuratly in
14514 patch_bc_statement. */
14515 label_block_expr = EXPR_WFL_NODE (name);
14516 }
14517 /* Unlabeled break/continue will be handled during the
14518 break/continue patch operation */
14519 break_continue
14520 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14521
14522 IS_BREAK_STMT_P (break_continue) = is_break;
14523 TREE_SIDE_EFFECTS (break_continue) = 1;
14524 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14525 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14526 return break_continue;
14527}
14528
14529/* Verification of a break/continue statement. */
14530
14531static tree
14532patch_bc_statement (node)
14533 tree node;
14534{
14535 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14536 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14537 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14538
e04a16fb 14539 /* Having an identifier here means that the target is unknown. */
b635eb2f 14540 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14541 {
14542 parse_error_context (wfl_operator, "No label definition found for `%s'",
14543 IDENTIFIER_POINTER (bc_label));
14544 return error_mark_node;
14545 }
b635eb2f 14546 if (! IS_BREAK_STMT_P (node))
e04a16fb 14547 {
b635eb2f
PB
14548 /* It's a continue statement. */
14549 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14550 {
b635eb2f
PB
14551 if (labeled_block == NULL_TREE)
14552 {
14553 if (bc_label == NULL_TREE)
14554 parse_error_context (wfl_operator,
14555 "`continue' must be in loop");
14556 else
1504b2b4
APB
14557 parse_error_context
14558 (wfl_operator, "continue label `%s' does not name a loop",
14559 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14560 return error_mark_node;
14561 }
14562 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14563 == continue_identifier_node)
14564 && (bc_label == NULL_TREE
14565 || TREE_CHAIN (labeled_block) == bc_label))
14566 {
14567 bc_label = labeled_block;
14568 break;
14569 }
e04a16fb 14570 }
e04a16fb 14571 }
b635eb2f 14572 else if (!bc_label)
34f4db93 14573 {
b635eb2f 14574 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14575 {
b635eb2f
PB
14576 if (labeled_block == NULL_TREE)
14577 {
14578 parse_error_context (wfl_operator,
14579 "`break' must be in loop or switch");
14580 return error_mark_node;
14581 }
14582 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14583 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14584 || search_loop (target_stmt))
b635eb2f
PB
14585 {
14586 bc_label = labeled_block;
14587 break;
14588 }
e04a16fb 14589 }
e04a16fb
AG
14590 }
14591
b635eb2f 14592 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14593 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14594
e04a16fb
AG
14595 /* Our break/continue don't return values. */
14596 TREE_TYPE (node) = void_type_node;
14597 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14598 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14599 sometimes, like after a if statement) */
14600 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14601 TREE_SIDE_EFFECTS (node) = 1;
14602 return node;
14603}
14604
14605/* Process the exit expression belonging to a loop. Its type must be
14606 boolean. */
14607
14608static tree
14609patch_exit_expr (node)
14610 tree node;
14611{
14612 tree expression = TREE_OPERAND (node, 0);
14613 TREE_TYPE (node) = error_mark_node;
14614 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14615
14616 /* The type of expression must be boolean */
14617 if (TREE_TYPE (expression) != boolean_type_node)
14618 {
14619 parse_error_context
14620 (wfl_operator,
781b0558 14621 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14622 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14623 return error_mark_node;
14624 }
14625 /* Now we know things are allright, invert the condition, fold and
14626 return */
14627 TREE_OPERAND (node, 0) =
14628 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14629
14630 if (! integer_zerop (TREE_OPERAND (node, 0))
14631 && ctxp->current_loop != NULL_TREE
14632 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14633 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14634 if (! integer_onep (TREE_OPERAND (node, 0)))
14635 CAN_COMPLETE_NORMALLY (node) = 1;
14636
14637
e04a16fb
AG
14638 TREE_TYPE (node) = void_type_node;
14639 return node;
14640}
b67d701b
PB
14641
14642/* 14.9 Switch statement */
14643
14644static tree
14645patch_switch_statement (node)
14646 tree node;
14647{
c877974e 14648 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14649
14650 /* Complete the switch expression */
14651 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14652 se_type = TREE_TYPE (se);
14653 /* The type of the switch expression must be char, byte, short or
14654 int */
2e0f0aff 14655 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14656 {
14657 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14658 parse_error_context (wfl_operator,
14659 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14660 lang_printable_name (se_type, 0));
b67d701b
PB
14661 /* This is what java_complete_tree will check */
14662 TREE_OPERAND (node, 0) = error_mark_node;
14663 return error_mark_node;
14664 }
14665
15fdcfe9 14666 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14667
14668 /* Ready to return */
15fdcfe9 14669 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14670 {
14671 TREE_TYPE (node) = error_mark_node;
14672 return error_mark_node;
14673 }
14674 TREE_TYPE (node) = void_type_node;
14675 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14676 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14677 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14678 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14679 return node;
14680}
14681
165f37bc 14682/* 14.18 The try/catch statements */
b67d701b 14683
b67d701b 14684static tree
a7d8d81f 14685build_try_statement (location, try_block, catches)
b67d701b 14686 int location;
a7d8d81f
PB
14687 tree try_block, catches;
14688{
14689 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14690 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14691 return node;
b67d701b
PB
14692}
14693
a7d8d81f
PB
14694static tree
14695build_try_finally_statement (location, try_block, finally)
14696 int location;
14697 tree try_block, finally;
b67d701b 14698{
a7d8d81f
PB
14699 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14700 EXPR_WFL_LINECOL (node) = location;
14701 return node;
b67d701b
PB
14702}
14703
14704static tree
14705patch_try_statement (node)
14706 tree node;
14707{
14708 int error_found = 0;
14709 tree try = TREE_OPERAND (node, 0);
14710 /* Exception handlers are considered in left to right order */
14711 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14712 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14713
14714 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14715 to process the next catch clause. We process the catch clause before
14716 the try block so that when processing the try block we can check thrown
14717 exceptions againts the caught type list. */
b67d701b
PB
14718 for (current = catch; current; current = TREE_CHAIN (current))
14719 {
14720 tree carg_decl, carg_type;
14721 tree sub_current, catch_block, catch_clause;
14722 int unreachable;
14723
b67d701b 14724 /* At this point, the structure of the catch clause is
b67d701b
PB
14725 CATCH_EXPR (catch node)
14726 BLOCK (with the decl of the parameter)
14727 COMPOUND_EXPR
7525cc04 14728 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14729 BLOCK (catch clause block)
a7d8d81f
PB
14730 */
14731 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14732 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14733 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14734
14735 /* Catch clauses can't have more than one parameter declared,
14736 but it's already enforced by the grammar. Make sure that the
14737 only parameter of the clause statement in of class Throwable
14738 or a subclass of Throwable, but that was done earlier. The
14739 catch clause parameter type has also been resolved. */
14740
14741 /* Just make sure that the catch clause parameter type inherits
14742 from java.lang.Throwable */
14743 if (!inherits_from_p (carg_type, throwable_type_node))
14744 {
14745 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14746 parse_error_context (wfl_operator,
781b0558 14747 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14748 lang_printable_name (carg_type, 0));
b67d701b
PB
14749 error_found = 1;
14750 continue;
14751 }
14752
14753 /* Partial check for unreachable catch statement: The catch
14754 clause is reachable iff is no earlier catch block A in
14755 the try statement such that the type of the catch
14756 clause's parameter is the same as or a subclass of the
14757 type of A's parameter */
14758 unreachable = 0;
14759 for (sub_current = catch;
14760 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14761 {
14762 tree sub_catch_clause, decl;
a7d8d81f 14763 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14764 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14765
14766 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14767 {
14768 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14769 parse_error_context
781b0558
KG
14770 (wfl_operator,
14771 "`catch' not reached because of the catch clause at line %d",
14772 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14773 unreachable = error_found = 1;
14774 break;
14775 }
14776 }
b67d701b
PB
14777 /* Complete the catch clause block */
14778 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14779 if (catch_block == error_mark_node)
14780 {
14781 error_found = 1;
14782 continue;
14783 }
15fdcfe9
PB
14784 if (CAN_COMPLETE_NORMALLY (catch_block))
14785 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14786 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14787
14788 if (unreachable)
14789 continue;
14790
14791 /* Things to do here: the exception must be thrown */
14792
14793 /* Link this type to the caught type list */
14794 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14795 }
14796
b9f7e36c
APB
14797 PUSH_EXCEPTIONS (caught_type_list);
14798 if ((try = java_complete_tree (try)) == error_mark_node)
14799 error_found = 1;
15fdcfe9
PB
14800 if (CAN_COMPLETE_NORMALLY (try))
14801 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14802 POP_EXCEPTIONS ();
14803
b67d701b
PB
14804 /* Verification ends here */
14805 if (error_found)
14806 return error_mark_node;
14807
14808 TREE_OPERAND (node, 0) = try;
14809 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14810 TREE_TYPE (node) = void_type_node;
14811 return node;
14812}
b9f7e36c
APB
14813
14814/* 14.17 The synchronized Statement */
14815
14816static tree
14817patch_synchronized_statement (node, wfl_op1)
14818 tree node, wfl_op1;
14819{
5a005d9e 14820 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14821 tree block = TREE_OPERAND (node, 1);
5a005d9e 14822
d8fccff5 14823 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14824
14825 if (expr == error_mark_node)
14826 {
14827 block = java_complete_tree (block);
14828 return expr;
14829 }
b9f7e36c
APB
14830
14831 /* The TYPE of expr must be a reference type */
5a005d9e 14832 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14833 {
14834 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14835 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14836 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14837 return error_mark_node;
14838 }
14839
ce6e9147
APB
14840 if (flag_emit_xref)
14841 {
14842 TREE_OPERAND (node, 0) = expr;
14843 TREE_OPERAND (node, 1) = java_complete_tree (block);
14844 CAN_COMPLETE_NORMALLY (node) = 1;
14845 return node;
14846 }
14847
b9f7e36c
APB
14848 /* Generate a try-finally for the synchronized statement, except
14849 that the handler that catches all throw exception calls
14850 _Jv_MonitorExit and then rethrow the exception.
14851 The synchronized statement is then implemented as:
14852 TRY
14853 {
14854 _Jv_MonitorEnter (expression)
14855 synchronized_block
14856 _Jv_MonitorExit (expression)
14857 }
14858 CATCH_ALL
14859 {
14860 e = _Jv_exception_info ();
14861 _Jv_MonitorExit (expression)
14862 Throw (e);
14863 } */
14864
5a005d9e
PB
14865 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14866 BUILD_MONITOR_ENTER (enter, expr_decl);
14867 BUILD_MONITOR_EXIT (exit, expr_decl);
14868 CAN_COMPLETE_NORMALLY (enter) = 1;
14869 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14870 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14871 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14872 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14873 build (COMPOUND_EXPR, NULL_TREE,
14874 build (WITH_CLEANUP_EXPR, NULL_TREE,
14875 build (COMPOUND_EXPR, NULL_TREE,
96847892 14876 assignment, enter),
5a005d9e
PB
14877 NULL_TREE, exit),
14878 block));
14879 node = build_expr_block (node, expr_decl);
14880
14881 return java_complete_tree (node);
b9f7e36c
APB
14882}
14883
14884/* 14.16 The throw Statement */
14885
14886static tree
14887patch_throw_statement (node, wfl_op1)
14888 tree node, wfl_op1;
14889{
14890 tree expr = TREE_OPERAND (node, 0);
14891 tree type = TREE_TYPE (expr);
14892 int unchecked_ok = 0, tryblock_throws_ok = 0;
14893
14894 /* Thrown expression must be assignable to java.lang.Throwable */
14895 if (!try_reference_assignconv (throwable_type_node, expr))
14896 {
14897 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14898 parse_error_context (wfl_operator,
14899 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14900 lang_printable_name (type, 0));
b9f7e36c
APB
14901 /* If the thrown expression was a reference, we further the
14902 compile-time check. */
14903 if (!JREFERENCE_TYPE_P (type))
14904 return error_mark_node;
14905 }
14906
14907 /* At least one of the following must be true */
14908
14909 /* The type of the throw expression is a not checked exception,
14910 i.e. is a unchecked expression. */
c877974e 14911 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14912
c2952b01
APB
14913 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14914 /* An instance can't throw a checked excetion unless that exception
14915 is explicitely declared in the `throws' clause of each
14916 constructor. This doesn't apply to anonymous classes, since they
14917 don't have declared constructors. */
14918 if (!unchecked_ok
14919 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14920 {
14921 tree current;
14922 for (current = TYPE_METHODS (current_class); current;
14923 current = TREE_CHAIN (current))
14924 if (DECL_CONSTRUCTOR_P (current)
14925 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14926 {
14927 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)",
14928 lang_printable_name (TREE_TYPE (expr), 0));
14929 return error_mark_node;
14930 }
14931 }
14932
b9f7e36c
APB
14933 /* Throw is contained in a try statement and at least one catch
14934 clause can receive the thrown expression or the current method is
14935 declared to throw such an exception. Or, the throw statement is
14936 contained in a method or constructor declaration and the type of
14937 the Expression is assignable to at least one type listed in the
14938 throws clause the declaration. */
b9f7e36c 14939 if (!unchecked_ok)
f099f336 14940 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14941 if (!(unchecked_ok || tryblock_throws_ok))
14942 {
14943 /* If there is a surrounding try block that has no matching
14944 clatch clause, report it first. A surrounding try block exits
14945 only if there is something after the list of checked
14946 exception thrown by the current function (if any). */
14947 if (IN_TRY_BLOCK_P ())
781b0558 14948 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 14949 lang_printable_name (type, 0));
b9f7e36c
APB
14950 /* If we have no surrounding try statement and the method doesn't have
14951 any throws, report it now. FIXME */
f099f336
APB
14952
14953 /* We report that the exception can't be throw from a try block
14954 in all circumstances but when the `throw' is inside a static
14955 block. */
b9f7e36c
APB
14956 else if (!EXCEPTIONS_P (currently_caught_type_list)
14957 && !tryblock_throws_ok)
f099f336 14958 {
c2952b01 14959 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14960 parse_error_context (wfl_operator,
14961 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14962 lang_printable_name (type, 0));
14963 else
781b0558
KG
14964 parse_error_context (wfl_operator,
14965 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14966 lang_printable_name (type, 0));
14967 }
b9f7e36c
APB
14968 /* Otherwise, the current method doesn't have the appropriate
14969 throws declaration */
14970 else
781b0558 14971 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14972 lang_printable_name (type, 0));
b9f7e36c
APB
14973 return error_mark_node;
14974 }
14975
ce6e9147 14976 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14977 BUILD_THROW (node, expr);
ce6e9147
APB
14978
14979 /* If doing xrefs, keep the location where the `throw' was seen. */
14980 if (flag_emit_xref)
14981 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14982 return node;
14983}
14984
14985/* Check that exception said to be thrown by method DECL can be
14986 effectively caught from where DECL is invoked. */
14987
14988static void
14989check_thrown_exceptions (location, decl)
14990 int location;
14991 tree decl;
14992{
14993 tree throws;
14994 /* For all the unchecked exceptions thrown by DECL */
14995 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14996 throws = TREE_CHAIN (throws))
0a2138e2 14997 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14998 {
3e78f871
PB
14999#if 1
15000 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15001 if (DECL_NAME (decl) == get_identifier ("clone"))
15002 continue;
15003#endif
b9f7e36c 15004 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15005 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15006 parse_error_context
15007 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15008 lang_printable_name (TREE_VALUE (throws), 0));
15009 else
15010 {
15011 parse_error_context
781b0558 15012 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15013 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15014 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15015 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15016 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15017 }
b9f7e36c
APB
15018 }
15019}
15020
c877974e 15021/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15022 try-catch blocks, OR is listed in the `throws' clause of the
15023 current method. */
15024
15025static int
0a2138e2 15026check_thrown_exceptions_do (exception)
b9f7e36c
APB
15027 tree exception;
15028{
15029 tree list = currently_caught_type_list;
c877974e 15030 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15031 /* First, all the nested try-catch-finally at that stage. The
15032 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15033 if (IS_UNCHECKED_EXCEPTION_P (exception))
15034 return 1;
b9f7e36c
APB
15035 while (list)
15036 {
15037 tree caught;
15038 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15039 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15040 return 1;
15041 list = TREE_CHAIN (list);
15042 }
15043 return 0;
15044}
15045
15046static void
15047purge_unchecked_exceptions (mdecl)
15048 tree mdecl;
15049{
15050 tree throws = DECL_FUNCTION_THROWS (mdecl);
15051 tree new = NULL_TREE;
15052
15053 while (throws)
15054 {
15055 tree next = TREE_CHAIN (throws);
c877974e 15056 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15057 {
15058 TREE_CHAIN (throws) = new;
15059 new = throws;
15060 }
15061 throws = next;
15062 }
15063 /* List is inverted here, but it doesn't matter */
15064 DECL_FUNCTION_THROWS (mdecl) = new;
15065}
22eed1e6
APB
15066
15067/* 15.24 Conditional Operator ?: */
15068
15069static tree
15070patch_conditional_expr (node, wfl_cond, wfl_op1)
15071 tree node, wfl_cond, wfl_op1;
15072{
15073 tree cond = TREE_OPERAND (node, 0);
15074 tree op1 = TREE_OPERAND (node, 1);
15075 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15076 tree resulting_type = NULL_TREE;
ac825856 15077 tree t1, t2, patched;
22eed1e6
APB
15078 int error_found = 0;
15079
ac825856
APB
15080 /* Operands of ?: might be StringBuffers crafted as a result of a
15081 string concatenation. Obtain a descent operand here. */
15082 if ((patched = patch_string (op1)))
15083 TREE_OPERAND (node, 1) = op1 = patched;
15084 if ((patched = patch_string (op2)))
15085 TREE_OPERAND (node, 2) = op2 = patched;
15086
15087 t1 = TREE_TYPE (op1);
15088 t2 = TREE_TYPE (op2);
15089
22eed1e6
APB
15090 /* The first expression must be a boolean */
15091 if (TREE_TYPE (cond) != boolean_type_node)
15092 {
15093 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15094 parse_error_context (wfl_operator,
15095 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15096 lang_printable_name (TREE_TYPE (cond), 0));
15097 error_found = 1;
15098 }
15099
15100 /* Second and third can be numeric, boolean (i.e. primitive),
15101 references or null. Anything else results in an error */
15102 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15103 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15104 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15105 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15106 error_found = 1;
15107
15108 /* Determine the type of the conditional expression. Same types are
15109 easy to deal with */
15110 else if (t1 == t2)
15111 resulting_type = t1;
15112
15113 /* There are different rules for numeric types */
15114 else if (JNUMERIC_TYPE_P (t1))
15115 {
15116 /* if byte/short found, the resulting type is short */
15117 if ((t1 == byte_type_node && t2 == short_type_node)
15118 || (t1 == short_type_node && t2 == byte_type_node))
15119 resulting_type = short_type_node;
15120
15121 /* If t1 is a constant int and t2 is of type byte, short or char
15122 and t1's value fits in t2, then the resulting type is t2 */
15123 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15124 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15125 resulting_type = t2;
15126
15127 /* If t2 is a constant int and t1 is of type byte, short or char
15128 and t2's value fits in t1, then the resulting type is t1 */
15129 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15130 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15131 resulting_type = t1;
15132
15133 /* Otherwise, binary numeric promotion is applied and the
15134 resulting type is the promoted type of operand 1 and 2 */
15135 else
93024893 15136 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15137 &TREE_OPERAND (node, 1),
15138 &TREE_OPERAND (node, 2));
15139 }
15140
15141 /* Cases of a reference and a null type */
15142 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15143 resulting_type = t1;
15144
15145 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15146 resulting_type = t2;
15147
15148 /* Last case: different reference types. If a type can be converted
15149 into the other one by assignment conversion, the latter
15150 determines the type of the expression */
15151 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15152 resulting_type = promote_type (t1);
15153
15154 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15155 resulting_type = promote_type (t2);
15156
15157 /* If we don't have any resulting type, we're in trouble */
15158 if (!resulting_type)
15159 {
c2e3db92 15160 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15161 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15162 parse_error_context (wfl_operator,
15163 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15164 t, lang_printable_name (t2, 0));
22eed1e6
APB
15165 free (t);
15166 error_found = 1;
15167 }
15168
15169 if (error_found)
15170 {
15171 TREE_TYPE (node) = error_mark_node;
15172 return error_mark_node;
15173 }
15174
15175 TREE_TYPE (node) = resulting_type;
15176 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15177 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15178 return node;
15179}
ac825856 15180
5b09b33e
PB
15181/* Try to constant fold NODE.
15182 If NODE is not a constant expression, return NULL_EXPR.
15183 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15184
15185static tree
15186fold_constant_for_init (node, context)
15187 tree node;
15188 tree context;
15189{
15190 tree op0, op1, val;
15191 enum tree_code code = TREE_CODE (node);
15192
ee97d354 15193 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15194 return node;
93024893 15195
5b09b33e
PB
15196 switch (code)
15197 {
5b09b33e
PB
15198 case PLUS_EXPR:
15199 case MINUS_EXPR:
bc3ca41b
PB
15200 case MULT_EXPR:
15201 case TRUNC_MOD_EXPR:
15202 case RDIV_EXPR:
5b09b33e
PB
15203 case LSHIFT_EXPR:
15204 case RSHIFT_EXPR:
15205 case URSHIFT_EXPR:
15206 case BIT_AND_EXPR:
15207 case BIT_XOR_EXPR:
15208 case BIT_IOR_EXPR:
5b09b33e
PB
15209 case TRUTH_ANDIF_EXPR:
15210 case TRUTH_ORIF_EXPR:
15211 case EQ_EXPR:
15212 case NE_EXPR:
15213 case GT_EXPR:
15214 case GE_EXPR:
15215 case LT_EXPR:
15216 case LE_EXPR:
15217 op0 = TREE_OPERAND (node, 0);
15218 op1 = TREE_OPERAND (node, 1);
15219 val = fold_constant_for_init (op0, context);
15220 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15221 return NULL_TREE;
15222 TREE_OPERAND (node, 0) = val;
15223 val = fold_constant_for_init (op1, context);
15224 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15225 return NULL_TREE;
15226 TREE_OPERAND (node, 1) = val;
15227 return patch_binop (node, op0, op1);
15228
15229 case UNARY_PLUS_EXPR:
15230 case NEGATE_EXPR:
15231 case TRUTH_NOT_EXPR:
15232 case BIT_NOT_EXPR:
15233 case CONVERT_EXPR:
15234 op0 = TREE_OPERAND (node, 0);
15235 val = fold_constant_for_init (op0, context);
15236 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15237 return NULL_TREE;
15238 TREE_OPERAND (node, 0) = val;
5a005d9e 15239 return patch_unaryop (node, op0);
5b09b33e
PB
15240 break;
15241
15242 case COND_EXPR:
15243 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15244 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15245 return NULL_TREE;
15246 TREE_OPERAND (node, 0) = val;
15247 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15248 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15249 return NULL_TREE;
15250 TREE_OPERAND (node, 1) = val;
15251 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15252 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15253 return NULL_TREE;
15254 TREE_OPERAND (node, 2) = val;
15255 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15256 : TREE_OPERAND (node, 2);
15257
15258 case VAR_DECL:
8576f094
APB
15259 case FIELD_DECL:
15260 if (! FIELD_FINAL (node)
5b09b33e
PB
15261 || DECL_INITIAL (node) == NULL_TREE)
15262 return NULL_TREE;
15263 val = DECL_INITIAL (node);
15264 /* Guard against infinite recursion. */
15265 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15266 val = fold_constant_for_init (val, node);
5b09b33e
PB
15267 DECL_INITIAL (node) = val;
15268 return val;
15269
15270 case EXPR_WITH_FILE_LOCATION:
15271 /* Compare java_complete_tree and resolve_expression_name. */
15272 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15273 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15274 {
15275 tree name = EXPR_WFL_NODE (node);
15276 tree decl;
15277 if (PRIMARY_P (node))
15278 return NULL_TREE;
15279 else if (! QUALIFIED_P (name))
15280 {
15281 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15282 if (decl == NULL_TREE
15283 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15284 return NULL_TREE;
15285 return fold_constant_for_init (decl, decl);
15286 }
15287 else
15288 {
5b09b33e
PB
15289 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15290 qualify_ambiguous_name (node);
15291 if (resolve_field_access (node, &decl, NULL)
15292 && decl != NULL_TREE)
15293 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15294 return NULL_TREE;
15295 }
15296 }
15297 else
15298 {
15299 op0 = TREE_OPERAND (node, 0);
15300 val = fold_constant_for_init (op0, context);
15301 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15302 return NULL_TREE;
15303 TREE_OPERAND (node, 0) = val;
15304 return val;
15305 }
15306
bc3ca41b
PB
15307#ifdef USE_COMPONENT_REF
15308 case IDENTIFIER:
15309 case COMPONENT_REF:
15310 ?;
15311#endif
15312
5b09b33e
PB
15313 default:
15314 return NULL_TREE;
15315 }
15316}
bc3ca41b
PB
15317
15318#ifdef USE_COMPONENT_REF
15319/* Context is 'T' for TypeName, 'P' for PackageName,
15320 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15321
15322tree
15323resolve_simple_name (name, context)
15324 tree name;
15325 int context;
15326{
15327}
15328
15329tree
15330resolve_qualified_name (name, context)
15331 tree name;
15332 int context;
15333{
15334}
15335#endif
f15b9af9
MM
15336
15337/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15338
15339static void
15340mark_parser_ctxt (p)
15341 void *p;
15342{
15343 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15344 int i;
15345
15346 if (!pc)
15347 return;
15348
15349#ifndef JC1_LITE
15350 for (i = 0; i < 11; ++i)
15351 ggc_mark_tree (pc->modifier_ctx[i]);
15352 ggc_mark_tree (pc->class_type);
15353 ggc_mark_tree (pc->function_decl);
15354 ggc_mark_tree (pc->package);
15355 ggc_mark_tree (pc->incomplete_class);
15356 ggc_mark_tree (pc->gclass_list);
15357 ggc_mark_tree (pc->class_list);
15358 ggc_mark_tree (pc->current_parsed_class);
15359 ggc_mark_tree (pc->current_parsed_class_un);
15360 ggc_mark_tree (pc->non_static_initialized);
15361 ggc_mark_tree (pc->static_initialized);
15362 ggc_mark_tree (pc->instance_initializers);
15363 ggc_mark_tree (pc->import_list);
15364 ggc_mark_tree (pc->import_demand_list);
15365 ggc_mark_tree (pc->current_loop);
15366 ggc_mark_tree (pc->current_labeled_block);
15367#endif /* JC1_LITE */
15368
15369 if (pc->next)
15370 mark_parser_ctxt (&pc->next);
15371}
This page took 2.686984 seconds and 5 git commands to generate.