]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
gcc.c (record_temp_file, [...]): Make non-static, so they can be called from java...
[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.
dc08e603 3 Copyright (C) 1997, 1998, 1999, 2000, 2001 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"
19e223db 67#include "ggc.h"
e04a16fb 68
c2952b01
APB
69#ifndef DIR_SEPARATOR
70#define DIR_SEPARATOR '/'
71#endif
72
82371d41 73/* Local function prototypes */
df32d2ce
KG
74static char *java_accstring_lookup PARAMS ((int));
75static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
77static tree create_class PARAMS ((int, tree, tree, tree));
78static tree create_interface PARAMS ((int, tree, tree));
c2952b01 79static void end_class_declaration PARAMS ((int));
df32d2ce
KG
80static tree find_field PARAMS ((tree, tree));
81static tree lookup_field_wrapper PARAMS ((tree, tree));
82static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83static void register_fields PARAMS ((int, tree, tree));
98a52c2c 84static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
85static int parser_check_super PARAMS ((tree, tree, tree));
86static int parser_check_super_interface PARAMS ((tree, tree, tree));
87static void check_modifiers_consistency PARAMS ((int));
88static tree lookup_cl PARAMS ((tree));
89static tree lookup_java_method2 PARAMS ((tree, tree, int));
90static tree method_header PARAMS ((int, tree, tree, tree));
91static void fix_method_argument_names PARAMS ((tree ,tree));
92static tree method_declarator PARAMS ((tree, tree));
93static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 94 ATTRIBUTE_PRINTF_2;
88f3c477
JM
95static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
df32d2ce
KG
97static void parse_ctor_invocation_error PARAMS ((void));
98static tree parse_jdk1_1_error PARAMS ((const char *));
99static void complete_class_report_errors PARAMS ((jdep *));
100static int process_imports PARAMS ((void));
101static void read_import_dir PARAMS ((tree));
102static int find_in_imports_on_demand PARAMS ((tree));
9a7ab4b3 103static void find_in_imports PARAMS ((tree));
c7303e41
APB
104static void check_static_final_variable_assignment_flag PARAMS ((tree));
105static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108static int check_final_variable_indirect_assignment PARAMS ((tree));
109static void check_final_variable_global_assignment_flag PARAMS ((tree));
cf1748bf 110static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 111static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 112static void register_package PARAMS ((tree));
df32d2ce
KG
113static tree resolve_package PARAMS ((tree, tree *));
114static tree lookup_package_type PARAMS ((const char *, int));
c2952b01 115static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
116static void declare_local_variables PARAMS ((int, tree, tree));
117static void source_start_java_method PARAMS ((tree));
118static void source_end_java_method PARAMS ((void));
119static void expand_start_java_method PARAMS ((tree));
120static tree find_name_in_single_imports PARAMS ((tree));
121static void check_abstract_method_header PARAMS ((tree));
122static tree lookup_java_interface_method2 PARAMS ((tree, tree));
123static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 124static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 125static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 126 tree, tree, tree));
e101152f 127static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
89e09b9a 128 int *, tree *));
df32d2ce 129static int breakdown_qualified PARAMS ((tree *, tree *, tree));
a648f4e4 130static int in_same_package PARAMS ((tree, tree));
df32d2ce 131static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 132static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
133static tree resolve_no_layout PARAMS ((tree, tree));
134static int invocation_mode PARAMS ((tree, int));
135static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 136 tree, tree));
df32d2ce 137static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 138 tree *, tree *));
df32d2ce
KG
139static tree find_most_specific_methods_list PARAMS ((tree));
140static int argument_types_convertible PARAMS ((tree, tree));
141static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 142static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
143static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
144static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
145static tree obtain_incomplete_type PARAMS ((tree));
146static tree java_complete_lhs PARAMS ((tree));
147static tree java_complete_tree PARAMS ((tree));
c2952b01 148static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 149static int analyze_clinit_body PARAMS ((tree));
92d83515 150static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
151static void java_complete_expand_method PARAMS ((tree));
152static int unresolved_type_p PARAMS ((tree, tree *));
153static void create_jdep_list PARAMS ((struct parser_ctxt *));
154static tree build_expr_block PARAMS ((tree, tree));
155static tree enter_block PARAMS ((void));
156static tree enter_a_block PARAMS ((tree));
157static tree exit_block PARAMS ((void));
158static tree lookup_name_in_blocks PARAMS ((tree));
159static void maybe_absorb_scoping_blocks PARAMS ((void));
160static tree build_method_invocation PARAMS ((tree, tree));
161static tree build_new_invocation PARAMS ((tree, tree));
162static tree build_assignment PARAMS ((int, int, tree, tree));
163static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
164static int check_final_assignment PARAMS ((tree ,tree));
165static tree patch_assignment PARAMS ((tree, tree, tree ));
166static tree patch_binop PARAMS ((tree, tree, tree));
167static tree build_unaryop PARAMS ((int, int, tree));
168static tree build_incdec PARAMS ((int, int, tree, int));
169static tree patch_unaryop PARAMS ((tree, tree));
170static tree build_cast PARAMS ((int, tree, tree));
171static tree build_null_of_type PARAMS ((tree));
172static tree patch_cast PARAMS ((tree, tree));
173static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
174static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
175static int valid_cast_to_p PARAMS ((tree, tree));
176static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
177static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
178static tree try_reference_assignconv PARAMS ((tree, tree));
179static tree build_unresolved_array_type PARAMS ((tree));
180static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
181static tree build_array_ref PARAMS ((int, tree, tree));
182static tree patch_array_ref PARAMS ((tree));
183static tree make_qualified_name PARAMS ((tree, tree, int));
184static tree merge_qualified_name PARAMS ((tree, tree));
185static tree make_qualified_primary PARAMS ((tree, tree, int));
186static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 187 tree *, tree *));
df32d2ce 188static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
189static tree resolve_field_access PARAMS ((tree, tree *, tree *));
190static tree build_newarray_node PARAMS ((tree, tree, int));
191static tree patch_newarray PARAMS ((tree));
192static tree resolve_type_during_patch PARAMS ((tree));
193static tree build_this PARAMS ((int));
9a7ab4b3 194static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
195static tree build_return PARAMS ((int, tree));
196static tree patch_return PARAMS ((tree));
197static tree maybe_access_field PARAMS ((tree, tree, tree));
198static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
199static int check_for_static_method_reference PARAMS ((tree, tree, tree,
200 tree, tree));
e101152f 201static int not_accessible_p PARAMS ((tree, tree, tree, int));
df32d2ce
KG
202static void check_deprecation PARAMS ((tree, tree));
203static int class_in_current_package PARAMS ((tree));
204static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
205static tree patch_if_else_statement PARAMS ((tree));
206static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
207static tree add_stmt_to_block PARAMS ((tree, tree, tree));
208static tree patch_exit_expr PARAMS ((tree));
209static tree build_labeled_block PARAMS ((int, tree));
210static tree finish_labeled_statement PARAMS ((tree, tree));
211static tree build_bc_statement PARAMS ((int, int, tree));
212static tree patch_bc_statement PARAMS ((tree));
213static tree patch_loop_statement PARAMS ((tree));
214static tree build_new_loop PARAMS ((tree));
215static tree build_loop_body PARAMS ((int, tree, int));
216static tree finish_loop_body PARAMS ((int, tree, tree, int));
217static tree build_debugable_stmt PARAMS ((int, tree));
218static tree finish_for_loop PARAMS ((int, tree, tree, tree));
219static tree patch_switch_statement PARAMS ((tree));
220static tree string_constant_concatenation PARAMS ((tree, tree));
221static tree build_string_concatenation PARAMS ((tree, tree));
222static tree patch_string_cst PARAMS ((tree));
223static tree patch_string PARAMS ((tree));
224static tree build_try_statement PARAMS ((int, tree, tree));
225static tree build_try_finally_statement PARAMS ((int, tree, tree));
226static tree patch_try_statement PARAMS ((tree));
227static tree patch_synchronized_statement PARAMS ((tree, tree));
228static tree patch_throw_statement PARAMS ((tree, tree));
229static void check_thrown_exceptions PARAMS ((int, tree));
230static int check_thrown_exceptions_do PARAMS ((tree));
231static void purge_unchecked_exceptions PARAMS ((tree));
232static void check_throws_clauses PARAMS ((tree, tree, tree));
233static void finish_method_declaration PARAMS ((tree));
234static tree build_super_invocation PARAMS ((tree));
235static int verify_constructor_circularity PARAMS ((tree, tree));
236static char *constructor_circularity_msg PARAMS ((tree, tree));
237static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 238 int, int));
df32d2ce
KG
239static const char *get_printable_method_name PARAMS ((tree));
240static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
241static tree generate_finit PARAMS ((tree));
242static void add_instance_initializer PARAMS ((tree));
df32d2ce 243static void fix_constructors PARAMS ((tree));
c2952b01
APB
244static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
245 tree, int *));
246static void craft_constructor PARAMS ((tree, tree));
247static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
248static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
249static void start_artificial_method_body PARAMS ((tree));
250static void end_artificial_method_body PARAMS ((tree));
251static int check_method_redefinition PARAMS ((tree, tree));
165f37bc 252static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
253static void java_check_regular_methods PARAMS ((tree));
254static void java_check_abstract_methods PARAMS ((tree));
255static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
256static void unreachable_stmt_error PARAMS ((tree));
257static tree find_expr_with_wfl PARAMS ((tree));
258static void missing_return_error PARAMS ((tree));
259static tree build_new_array_init PARAMS ((int, tree));
260static tree patch_new_array_init PARAMS ((tree, tree));
261static tree maybe_build_array_element_wfl PARAMS ((tree));
262static int array_constructor_check_entry PARAMS ((tree, tree));
263static const char *purify_type_name PARAMS ((const char *));
264static tree fold_constant_for_init PARAMS ((tree, tree));
265static tree strip_out_static_field_access_decl PARAMS ((tree));
266static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
267static void static_ref_err PARAMS ((tree, tree, tree));
268static void parser_add_interface PARAMS ((tree, tree, tree));
269static void add_superinterfaces PARAMS ((tree, tree));
270static tree jdep_resolve_class PARAMS ((jdep *));
271static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
272static void java_complete_expand_classes PARAMS ((void));
273static void java_complete_expand_class PARAMS ((tree));
274static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
275static tree cut_identifier_in_qualified PARAMS ((tree));
276static tree java_stabilize_reference PARAMS ((tree));
277static tree do_unary_numeric_promotion PARAMS ((tree));
278static char * operator_string PARAMS ((tree));
279static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
280static tree merge_string_cste PARAMS ((tree, tree, int));
281static tree java_refold PARAMS ((tree));
282static int java_decl_equiv PARAMS ((tree, tree));
283static int binop_compound_p PARAMS ((enum tree_code));
284static tree search_loop PARAMS ((tree));
285static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 286static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
287static void java_check_abstract_method_definitions PARAMS ((tree));
288static void java_debug_context_do PARAMS ((int));
c2952b01
APB
289static void java_parser_context_push_initialized_field PARAMS ((void));
290static void java_parser_context_pop_initialized_field PARAMS ((void));
291static tree reorder_static_initialized PARAMS ((tree));
292static void java_parser_context_suspend PARAMS ((void));
293static void java_parser_context_resume PARAMS ((void));
c7303e41 294static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
295
296/* JDK 1.1 work. FIXME */
297
298static tree maybe_make_nested_class_name PARAMS ((tree));
299static void make_nested_class_name PARAMS ((tree));
300static void set_nested_class_simple_name_value PARAMS ((tree, int));
301static void link_nested_class_to_enclosing PARAMS ((void));
302static tree find_as_inner_class PARAMS ((tree, tree, tree));
303static tree find_as_inner_class_do PARAMS ((tree, tree));
304static int check_inner_class_redefinition PARAMS ((tree, tree));
305
306static tree build_thisn_assign PARAMS ((void));
307static tree build_current_thisn PARAMS ((tree));
308static tree build_access_to_thisn PARAMS ((tree, tree, int));
309static tree maybe_build_thisn_access_method PARAMS ((tree));
310
311static tree build_outer_field_access PARAMS ((tree, tree));
312static tree build_outer_field_access_methods PARAMS ((tree));
313static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
314 tree, tree));
315static tree build_outer_method_access_method PARAMS ((tree));
316static tree build_new_access_id PARAMS ((void));
317static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
318 tree, tree));
319
320static int outer_field_access_p PARAMS ((tree, tree));
321static int outer_field_expanded_access_p PARAMS ((tree, tree *,
322 tree *, tree *));
323static tree outer_field_access_fix PARAMS ((tree, tree, tree));
324static tree build_incomplete_class_ref PARAMS ((int, tree));
325static tree patch_incomplete_class_ref PARAMS ((tree));
326static tree create_anonymous_class PARAMS ((int, tree));
327static void patch_anonymous_class PARAMS ((tree, tree, tree));
328static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 329
165f37bc
APB
330static tree build_dot_class_method PARAMS ((tree));
331static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 332static void create_new_parser_context PARAMS ((int));
f15b9af9 333static void mark_parser_ctxt PARAMS ((void *));
165f37bc 334
e04a16fb
AG
335/* Number of error found so far. */
336int java_error_count;
337/* Number of warning found so far. */
338int java_warning_count;
ce6e9147
APB
339/* Tell when not to fold, when doing xrefs */
340int do_not_fold;
c2952b01 341/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 342const char *cyclic_inheritance_report;
f15b9af9 343
c2952b01
APB
344/* Tell when we're within an instance initializer */
345static int in_instance_initializer;
e04a16fb
AG
346
347/* The current parser context */
d4370213 348struct parser_ctxt *ctxp;
e04a16fb 349
d4370213 350/* List of things that were analyzed for which code will be generated */
b351b287
APB
351static struct parser_ctxt *ctxp_for_generation = NULL;
352
e04a16fb
AG
353/* binop_lookup maps token to tree_code. It is used where binary
354 operations are involved and required by the parser. RDIV_EXPR
355 covers both integral/floating point division. The code is changed
356 once the type of both operator is worked out. */
357
358static enum tree_code binop_lookup[19] =
359 {
360 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
361 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
362 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
363 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
364 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
365 };
366#define BINOP_LOOKUP(VALUE) \
6e2aa220 367 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 368
5cbdba64
APB
369/* This is the end index for binary operators that can also be used
370 in compound assignements. */
371#define BINOP_COMPOUND_CANDIDATES 11
372
e04a16fb 373/* The "$L" identifier we use to create labels. */
b67d701b
PB
374static tree label_id = NULL_TREE;
375
376/* The "StringBuffer" identifier used for the String `+' operator. */
377static tree wfl_string_buffer = NULL_TREE;
378
379/* The "append" identifier used for String `+' operator. */
380static tree wfl_append = NULL_TREE;
381
382/* The "toString" identifier used for String `+' operator. */
383static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
384
385/* The "java.lang" import qualified name. */
386static tree java_lang_id = NULL_TREE;
09ed0f70 387
c2952b01
APB
388/* The generated `inst$' identifier used for generated enclosing
389 instance/field access functions. */
390static tree inst_id = NULL_TREE;
391
09ed0f70
APB
392/* The "java.lang.Cloneable" qualified name. */
393static tree java_lang_cloneable = NULL_TREE;
f099f336 394
ee17a290
TT
395/* The "java.io.Serializable" qualified name. */
396static tree java_io_serializable = NULL_TREE;
397
f099f336
APB
398/* Context and flag for static blocks */
399static tree current_static_block = NULL_TREE;
400
c2952b01
APB
401/* The generated `write_parm_value$' identifier. */
402static tree wpv_id;
403
ee07f4f4
APB
404/* The list of all packages we've seen so far */
405static tree package_list = NULL_TREE;
2884c41e 406
19e223db
MM
407/* Hold THIS for the scope of the current public method decl. */
408static tree current_this;
409
410/* Hold a list of catch clauses list. The first element of this list is
411 the list of the catch clauses of the currently analysed try block. */
412static tree currently_caught_type_list;
413
2884c41e
KG
414/* Check modifiers. If one doesn't fit, retrieve it in its declaration
415 line and point it out. */
416/* Should point out the one that don't fit. ASCII/unicode, going
417 backward. FIXME */
418
419#define check_modifiers(__message, __value, __mask) do { \
420 if ((__value) & ~(__mask)) \
421 { \
422 int i, remainder = (__value) & ~(__mask); \
423 for (i = 0; i <= 10; i++) \
424 if ((1 << i) & remainder) \
425 parse_error_context (ctxp->modifier_ctx [i], (__message), \
426 java_accstring_lookup (1 << i)); \
427 } \
428} while (0)
ee07f4f4 429
e04a16fb
AG
430%}
431
432%union {
433 tree node;
434 int sub_token;
435 struct {
436 int token;
437 int location;
438 } operator;
439 int value;
440}
441
9ee9b555
KG
442%{
443#include "lex.c"
444%}
445
e04a16fb
AG
446%pure_parser
447
448/* Things defined here have to match the order of what's in the
449 binop_lookup table. */
450
451%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
452%token LS_TK SRS_TK ZRS_TK
453%token AND_TK XOR_TK OR_TK
454%token BOOL_AND_TK BOOL_OR_TK
455%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
456
457/* This maps to the same binop_lookup entry than the token above */
458
459%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
460%token REM_ASSIGN_TK
461%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
462%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
463
464
465/* Modifier TOKEN have to be kept in this order. Don't scramble it */
466
467%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
468%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
469%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
470%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 471%token STRICT_TK
e04a16fb
AG
472
473/* Keep those two in order, too */
474%token DECR_TK INCR_TK
475
476/* From now one, things can be in any order */
477
478%token DEFAULT_TK IF_TK THROW_TK
479%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
480%token THROWS_TK BREAK_TK IMPORT_TK
481%token ELSE_TK INSTANCEOF_TK RETURN_TK
482%token VOID_TK CATCH_TK INTERFACE_TK
483%token CASE_TK EXTENDS_TK FINALLY_TK
484%token SUPER_TK WHILE_TK CLASS_TK
485%token SWITCH_TK CONST_TK TRY_TK
486%token FOR_TK NEW_TK CONTINUE_TK
487%token GOTO_TK PACKAGE_TK THIS_TK
488
489%token BYTE_TK SHORT_TK INT_TK LONG_TK
490%token CHAR_TK INTEGRAL_TK
491
492%token FLOAT_TK DOUBLE_TK FP_TK
493
494%token ID_TK
495
496%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
497
498%token ASSIGN_ANY_TK ASSIGN_TK
499%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
500
501%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
502%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
503
c2952b01 504%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
505
506%type <node> super ID_TK identifier
507%type <node> name simple_name qualified_name
c2952b01 508%type <node> type_declaration compilation_unit
e04a16fb
AG
509 field_declaration method_declaration extends_interfaces
510 interfaces interface_type_list
c2952b01 511 class_member_declaration
e04a16fb
AG
512 import_declarations package_declaration
513 type_declarations interface_body
514 interface_member_declaration constant_declaration
515 interface_member_declarations interface_type
516 abstract_method_declaration interface_type_list
517%type <node> class_body_declaration class_member_declaration
518 static_initializer constructor_declaration block
22eed1e6 519%type <node> class_body_declarations constructor_header
e04a16fb
AG
520%type <node> class_or_interface_type class_type class_type_list
521 constructor_declarator explicit_constructor_invocation
b9f7e36c 522%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
523
524%type <node> variable_declarator_id variable_declarator
525 variable_declarators variable_initializer
22eed1e6 526 variable_initializers constructor_body
ac825856 527 array_initializer
e04a16fb 528
2e5eb5c5 529%type <node> class_body block_end constructor_block_end
e04a16fb
AG
530%type <node> statement statement_without_trailing_substatement
531 labeled_statement if_then_statement label_decl
532 if_then_else_statement while_statement for_statement
533 statement_nsi labeled_statement_nsi do_statement
534 if_then_else_statement_nsi while_statement_nsi
535 for_statement_nsi statement_expression_list for_init
536 for_update statement_expression expression_statement
537 primary_no_new_array expression primary
538 array_creation_expression array_type
539 class_instance_creation_expression field_access
540 method_invocation array_access something_dot_new
541 argument_list postfix_expression while_expression
542 post_increment_expression post_decrement_expression
543 unary_expression_not_plus_minus unary_expression
544 pre_increment_expression pre_decrement_expression
545 unary_expression_not_plus_minus cast_expression
546 multiplicative_expression additive_expression
547 shift_expression relational_expression
548 equality_expression and_expression
549 exclusive_or_expression inclusive_or_expression
550 conditional_and_expression conditional_or_expression
551 conditional_expression assignment_expression
552 left_hand_side assignment for_header for_begin
553 constant_expression do_statement_begin empty_statement
b67d701b 554 switch_statement synchronized_statement throw_statement
f8976021 555 try_statement switch_expression switch_block
15fdcfe9 556 catches catch_clause catch_clause_parameter finally
c2952b01 557 anonymous_class_creation
e04a16fb
AG
558%type <node> return_statement break_statement continue_statement
559
560%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
561%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
562%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
563%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
564%type <operator> ASSIGN_ANY_TK assignment_operator
565%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
566%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
567%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 568%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 569%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
570%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
571%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 572%type <operator> NEW_TK
e04a16fb
AG
573
574%type <node> method_body
575
576%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
577 STRING_LIT_TK NULL_TK VOID_TK
578
579%type <node> IF_TK WHILE_TK FOR_TK
580
581%type <node> formal_parameter_list formal_parameter
582 method_declarator method_header
583
c2952b01 584%type <node> primitive_type reference_type type
e04a16fb
AG
585 BOOLEAN_TK INTEGRAL_TK FP_TK
586
c2952b01 587/* Added or modified JDK 1.1 rule types */
c7303e41 588%type <node> type_literals
c2952b01 589
e04a16fb
AG
590%%
591/* 19.2 Production from 2.3: The Syntactic Grammar */
592goal:
19e223db
MM
593 {
594 /* Register static variables with the garbage
595 collector. */
596 ggc_add_tree_root (&label_id, 1);
597 ggc_add_tree_root (&wfl_string_buffer, 1);
598 ggc_add_tree_root (&wfl_append, 1);
599 ggc_add_tree_root (&wfl_to_string, 1);
600 ggc_add_tree_root (&java_lang_id, 1);
601 ggc_add_tree_root (&inst_id, 1);
602 ggc_add_tree_root (&java_lang_cloneable, 1);
603 ggc_add_tree_root (&java_io_serializable, 1);
604 ggc_add_tree_root (&current_static_block, 1);
605 ggc_add_tree_root (&wpv_id, 1);
606 ggc_add_tree_root (&package_list, 1);
607 ggc_add_tree_root (&current_this, 1);
608 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
609 ggc_add_root (&ctxp, 1,
610 sizeof (struct parser_ctxt *),
611 mark_parser_ctxt);
612 ggc_add_root (&ctxp_for_generation, 1,
613 sizeof (struct parser_ctxt *),
614 mark_parser_ctxt);
19e223db 615 }
e04a16fb
AG
616 compilation_unit
617 {}
618;
619
620/* 19.3 Productions from 3: Lexical structure */
621literal:
622 INT_LIT_TK
623| FP_LIT_TK
624| BOOL_LIT_TK
625| CHAR_LIT_TK
626| STRING_LIT_TK
627| NULL_TK
628;
629
630/* 19.4 Productions from 4: Types, Values and Variables */
631type:
632 primitive_type
633| reference_type
634;
635
636primitive_type:
637 INTEGRAL_TK
638| FP_TK
639| BOOLEAN_TK
640;
641
642reference_type:
643 class_or_interface_type
644| array_type
645;
646
647class_or_interface_type:
648 name
649;
650
651class_type:
652 class_or_interface_type /* Default rule */
653;
654
655interface_type:
656 class_or_interface_type
657;
658
659array_type:
c7303e41 660 primitive_type dims
e04a16fb 661 {
c7303e41
APB
662 int osb = pop_current_osb (ctxp);
663 tree t = build_java_array_type (($1), -1);
664 CLASS_LOADED_P (t) = 1;
665 while (--osb)
666 t = build_unresolved_array_type (t);
667 $$ = t;
668 }
669| name dims
670 {
671 int osb = pop_current_osb (ctxp);
672 tree t = $1;
673 while (osb--)
674 t = build_unresolved_array_type (t);
675 $$ = t;
e04a16fb 676 }
e04a16fb
AG
677;
678
679/* 19.5 Productions from 6: Names */
680name:
681 simple_name /* Default rule */
682| qualified_name /* Default rule */
683;
684
685simple_name:
686 identifier /* Default rule */
687;
688
689qualified_name:
690 name DOT_TK identifier
691 { $$ = make_qualified_name ($1, $3, $2.location); }
692;
693
694identifier:
695 ID_TK
696;
697
698/* 19.6: Production from 7: Packages */
699compilation_unit:
700 {$$ = NULL;}
701| package_declaration
702| import_declarations
703| type_declarations
704| package_declaration import_declarations
705| package_declaration type_declarations
706| import_declarations type_declarations
707| package_declaration import_declarations type_declarations
708;
709
710import_declarations:
711 import_declaration
712 {
713 $$ = NULL;
714 }
715| import_declarations import_declaration
716 {
717 $$ = NULL;
718 }
719;
720
721type_declarations:
722 type_declaration
723| type_declarations type_declaration
724;
725
726package_declaration:
727 PACKAGE_TK name SC_TK
ee07f4f4
APB
728 {
729 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 730 register_package (ctxp->package);
ee07f4f4 731 }
e04a16fb
AG
732| PACKAGE_TK error
733 {yyerror ("Missing name"); RECOVER;}
734| PACKAGE_TK name error
735 {yyerror ("';' expected"); RECOVER;}
736;
737
738import_declaration:
739 single_type_import_declaration
740| type_import_on_demand_declaration
741;
742
743single_type_import_declaration:
744 IMPORT_TK name SC_TK
745 {
9a7ab4b3 746 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 747 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 748 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
749 while (last != IDENTIFIER_POINTER (name))
750 {
751 if (last [0] == '.')
752 break;
753 last--;
754 }
755 last_name = get_identifier (++last);
756 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
757 {
758 tree err = find_name_in_single_imports (last_name);
759 if (err && err != name)
760 parse_error_context
761 ($2, "Ambiguous class: `%s' and `%s'",
762 IDENTIFIER_POINTER (name),
763 IDENTIFIER_POINTER (err));
5e942c50 764 else
9a7ab4b3 765 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
766 }
767 else
5e942c50 768 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
769 }
770| IMPORT_TK error
771 {yyerror ("Missing name"); RECOVER;}
772| IMPORT_TK name error
773 {yyerror ("';' expected"); RECOVER;}
774;
775
776type_import_on_demand_declaration:
777 IMPORT_TK name DOT_TK MULT_TK SC_TK
778 {
779 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
780 /* Don't import java.lang.* twice. */
781 if (name != java_lang_id)
782 {
ba179f9f 783 read_import_dir ($2);
9a7ab4b3
APB
784 ctxp->import_demand_list =
785 chainon (ctxp->import_demand_list,
786 build_tree_list ($2, NULL_TREE));
ba179f9f 787 }
e04a16fb
AG
788 }
789| IMPORT_TK name DOT_TK error
790 {yyerror ("'*' expected"); RECOVER;}
791| IMPORT_TK name DOT_TK MULT_TK error
792 {yyerror ("';' expected"); RECOVER;}
793;
794
795type_declaration:
796 class_declaration
c2952b01 797 { end_class_declaration (0); }
e04a16fb 798| interface_declaration
c2952b01 799 { end_class_declaration (0); }
5f1c312a 800| empty_statement
e04a16fb
AG
801| error
802 {
803 YYERROR_NOW;
804 yyerror ("Class or interface declaration expected");
805 }
806;
807
808/* 19.7 Shortened from the original:
809 modifiers: modifier | modifiers modifier
810 modifier: any of public... */
811modifiers:
812 MODIFIER_TK
813 {
814 $$ = (1 << $1);
815 }
816| modifiers MODIFIER_TK
817 {
818 int acc = (1 << $2);
819 if ($$ & acc)
820 parse_error_context
821 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
822 java_accstring_lookup (acc));
823 else
824 {
825 $$ |= acc;
826 }
827 }
828;
829
830/* 19.8.1 Production from $8.1: Class Declaration */
831class_declaration:
832 modifiers CLASS_TK identifier super interfaces
833 { create_class ($1, $3, $4, $5); }
834 class_body
e04a16fb
AG
835| CLASS_TK identifier super interfaces
836 { create_class (0, $2, $3, $4); }
837 class_body
e04a16fb
AG
838| modifiers CLASS_TK error
839 {yyerror ("Missing class name"); RECOVER;}
840| CLASS_TK error
841 {yyerror ("Missing class name"); RECOVER;}
842| CLASS_TK identifier error
0b4d333e
APB
843 {
844 if (!ctxp->class_err) yyerror ("'{' expected");
845 DRECOVER(class1);
846 }
e04a16fb
AG
847| modifiers CLASS_TK identifier error
848 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
849;
850
851super:
852 { $$ = NULL; }
853| EXTENDS_TK class_type
854 { $$ = $2; }
855| EXTENDS_TK class_type error
856 {yyerror ("'{' expected"); ctxp->class_err=1;}
857| EXTENDS_TK error
858 {yyerror ("Missing super class name"); ctxp->class_err=1;}
859;
860
861interfaces:
862 { $$ = NULL_TREE; }
863| IMPLEMENTS_TK interface_type_list
864 { $$ = $2; }
865| IMPLEMENTS_TK error
866 {
867 ctxp->class_err=1;
868 yyerror ("Missing interface name");
869 }
870;
871
872interface_type_list:
873 interface_type
874 {
875 ctxp->interface_number = 1;
876 $$ = build_tree_list ($1, NULL_TREE);
877 }
878| interface_type_list C_TK interface_type
879 {
880 ctxp->interface_number++;
881 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
882 }
883| interface_type_list C_TK error
884 {yyerror ("Missing interface name"); RECOVER;}
885;
886
887class_body:
888 OCB_TK CCB_TK
7f10c2e2
APB
889 {
890 /* Store the location of the `}' when doing xrefs */
891 if (flag_emit_xref)
c2952b01 892 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 893 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 894 $$ = GET_CPC ();
7f10c2e2 895 }
e04a16fb 896| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
897 {
898 /* Store the location of the `}' when doing xrefs */
899 if (flag_emit_xref)
c2952b01 900 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 901 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 902 $$ = GET_CPC ();
7f10c2e2 903 }
e04a16fb
AG
904;
905
906class_body_declarations:
907 class_body_declaration
908| class_body_declarations class_body_declaration
909;
910
911class_body_declaration:
912 class_member_declaration
913| static_initializer
914| constructor_declaration
915| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
916 {
917 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
918 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
919 }
e04a16fb
AG
920;
921
922class_member_declaration:
923 field_declaration
924| method_declaration
925| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
926 { end_class_declaration (1); }
927| interface_declaration /* Added, JDK1.1 inner interfaces */
928 { end_class_declaration (1); }
5f1c312a 929| empty_statement
e04a16fb
AG
930;
931
932/* 19.8.2 Productions from 8.3: Field Declarations */
933field_declaration:
934 type variable_declarators SC_TK
935 { register_fields (0, $1, $2); }
936| modifiers type variable_declarators SC_TK
937 {
e04a16fb
AG
938 check_modifiers
939 ("Illegal modifier `%s' for field declaration",
940 $1, FIELD_MODIFIERS);
941 check_modifiers_consistency ($1);
942 register_fields ($1, $2, $3);
943 }
944;
945
946variable_declarators:
947 /* Should we use build_decl_list () instead ? FIXME */
948 variable_declarator /* Default rule */
949| variable_declarators C_TK variable_declarator
950 { $$ = chainon ($1, $3); }
951| variable_declarators C_TK error
952 {yyerror ("Missing term"); RECOVER;}
953;
954
955variable_declarator:
956 variable_declarator_id
957 { $$ = build_tree_list ($1, NULL_TREE); }
958| variable_declarator_id ASSIGN_TK variable_initializer
959 {
960 if (java_error_count)
961 $3 = NULL_TREE;
962 $$ = build_tree_list
963 ($1, build_assignment ($2.token, $2.location, $1, $3));
964 }
965| variable_declarator_id ASSIGN_TK error
966 {
967 yyerror ("Missing variable initializer");
968 $$ = build_tree_list ($1, NULL_TREE);
969 RECOVER;
970 }
971| variable_declarator_id ASSIGN_TK variable_initializer error
972 {
973 yyerror ("';' expected");
974 $$ = build_tree_list ($1, NULL_TREE);
975 RECOVER;
976 }
977;
978
979variable_declarator_id:
980 identifier
981| variable_declarator_id OSB_TK CSB_TK
c583dd46 982 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
983| identifier error
984 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
985| variable_declarator_id OSB_TK error
29f8b718
APB
986 {
987 tree node = java_lval.node;
988 if (node && (TREE_CODE (node) == INTEGER_CST
989 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
990 yyerror ("Can't specify array dimension in a declaration");
991 else
992 yyerror ("']' expected");
993 DRECOVER(vdi);
994 }
e04a16fb
AG
995| variable_declarator_id CSB_TK error
996 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
997;
998
999variable_initializer:
1000 expression
1001| array_initializer
e04a16fb
AG
1002;
1003
1004/* 19.8.3 Productions from 8.4: Method Declarations */
1005method_declaration:
1006 method_header
1007 {
1008 current_function_decl = $1;
c2952b01
APB
1009 if (current_function_decl
1010 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1011 source_start_java_method (current_function_decl);
1012 else
1013 current_function_decl = NULL_TREE;
e04a16fb
AG
1014 }
1015 method_body
b635eb2f 1016 { finish_method_declaration ($3); }
e04a16fb
AG
1017| method_header error
1018 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1019;
1020
1021method_header:
1022 type method_declarator throws
b9f7e36c 1023 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1024| VOID_TK method_declarator throws
b9f7e36c 1025 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1026| modifiers type method_declarator throws
b9f7e36c 1027 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1028| modifiers VOID_TK method_declarator throws
b9f7e36c 1029 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1030| type error
efa0a23f
APB
1031 {
1032 yyerror ("Invalid method declaration, method name required");
1033 RECOVER;
1034 }
e04a16fb
AG
1035| modifiers type error
1036 {RECOVER;}
1037| VOID_TK error
1038 {yyerror ("Identifier expected"); RECOVER;}
1039| modifiers VOID_TK error
1040 {yyerror ("Identifier expected"); RECOVER;}
1041| modifiers error
1042 {
1043 yyerror ("Invalid method declaration, return type required");
1044 RECOVER;
1045 }
1046;
1047
1048method_declarator:
1049 identifier OP_TK CP_TK
c2952b01
APB
1050 {
1051 ctxp->formal_parameter_number = 0;
1052 $$ = method_declarator ($1, NULL_TREE);
1053 }
e04a16fb
AG
1054| identifier OP_TK formal_parameter_list CP_TK
1055 { $$ = method_declarator ($1, $3); }
1056| method_declarator OSB_TK CSB_TK
1057 {
1886c9d8
APB
1058 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1059 TREE_PURPOSE ($1) =
1060 build_unresolved_array_type (TREE_PURPOSE ($1));
1061 parse_warning_context
1062 (wfl_operator,
1063 "Discouraged form of returned type specification");
e04a16fb
AG
1064 }
1065| identifier OP_TK error
1066 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1067| method_declarator OSB_TK error
1068 {yyerror ("']' expected"); RECOVER;}
1069;
1070
1071formal_parameter_list:
1072 formal_parameter
1073 {
1074 ctxp->formal_parameter_number = 1;
1075 }
1076| formal_parameter_list C_TK formal_parameter
1077 {
1078 ctxp->formal_parameter_number += 1;
1079 $$ = chainon ($1, $3);
1080 }
1081| formal_parameter_list C_TK error
c2952b01 1082 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1083;
1084
1085formal_parameter:
1086 type variable_declarator_id
1087 {
1088 $$ = build_tree_list ($2, $1);
1089 }
18990de5 1090| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1091 {
5256aa37 1092 $$ = build_tree_list ($3, $2);
c2952b01 1093 ARG_FINAL_P ($$) = 1;
5256aa37 1094 }
e04a16fb 1095| type error
f8989a66
APB
1096 {
1097 yyerror ("Missing identifier"); RECOVER;
1098 $$ = NULL_TREE;
1099 }
18990de5 1100| final type error
e04a16fb 1101 {
e04a16fb 1102 yyerror ("Missing identifier"); RECOVER;
f8989a66 1103 $$ = NULL_TREE;
e04a16fb
AG
1104 }
1105;
1106
18990de5
JB
1107final:
1108 modifiers
1109 {
1110 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1111 $1, ACC_FINAL);
1112 if ($1 != ACC_FINAL)
1113 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1114 }
1115;
1116
e04a16fb 1117throws:
b9f7e36c 1118 { $$ = NULL_TREE; }
e04a16fb 1119| THROWS_TK class_type_list
b9f7e36c 1120 { $$ = $2; }
e04a16fb
AG
1121| THROWS_TK error
1122 {yyerror ("Missing class type term"); RECOVER;}
1123;
1124
1125class_type_list:
1126 class_type
c877974e 1127 { $$ = build_tree_list ($1, $1); }
e04a16fb 1128| class_type_list C_TK class_type
c877974e 1129 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1130| class_type_list C_TK error
1131 {yyerror ("Missing class type term"); RECOVER;}
1132;
1133
1134method_body:
1135 block
5f1c312a 1136| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1137;
1138
1139/* 19.8.4 Productions from 8.5: Static Initializers */
1140static_initializer:
1141 static block
1142 {
c2952b01
APB
1143 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1144 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1145 current_static_block = NULL_TREE;
e04a16fb 1146 }
e04a16fb
AG
1147;
1148
1149static: /* Test lval.sub_token here */
c2952b01 1150 modifiers
e04a16fb 1151 {
c2952b01
APB
1152 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1153 /* Can't have a static initializer in an innerclass */
1154 if ($1 | ACC_STATIC &&
1155 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1156 parse_error_context
1157 (MODIFIER_WFL (STATIC_TK),
1158 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1159 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1160 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1161 }
1162;
1163
1164/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1165constructor_declaration:
22eed1e6 1166 constructor_header
e04a16fb 1167 {
22eed1e6
APB
1168 current_function_decl = $1;
1169 source_start_java_method (current_function_decl);
e04a16fb 1170 }
22eed1e6 1171 constructor_body
b635eb2f 1172 { finish_method_declaration ($3); }
22eed1e6
APB
1173;
1174
1175constructor_header:
1176 constructor_declarator throws
1177 { $$ = method_header (0, NULL_TREE, $1, $2); }
1178| modifiers constructor_declarator throws
1179 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1180;
1181
1182constructor_declarator:
1183 simple_name OP_TK CP_TK
c2952b01
APB
1184 {
1185 ctxp->formal_parameter_number = 0;
1186 $$ = method_declarator ($1, NULL_TREE);
1187 }
e04a16fb 1188| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1189 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1190;
1191
1192constructor_body:
22eed1e6
APB
1193 /* Unlike regular method, we always need a complete (empty)
1194 body so we can safely perform all the required code
1195 addition (super invocation and field initialization) */
2e5eb5c5 1196 block_begin constructor_block_end
22eed1e6 1197 {
9bbc7d9f 1198 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1199 $$ = $2;
1200 }
2e5eb5c5 1201| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1202 { $$ = $3; }
2e5eb5c5 1203| block_begin block_statements constructor_block_end
22eed1e6 1204 { $$ = $3; }
2e5eb5c5 1205| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1206 { $$ = $4; }
e04a16fb
AG
1207;
1208
2e5eb5c5
APB
1209constructor_block_end:
1210 block_end
5f1c312a 1211;
2e5eb5c5 1212
e04a16fb
AG
1213/* Error recovery for that rule moved down expression_statement: rule. */
1214explicit_constructor_invocation:
1215 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1216 {
1217 $$ = build_method_invocation ($1, NULL_TREE);
1218 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1219 $$ = java_method_add_stmt (current_function_decl, $$);
1220 }
e04a16fb 1221| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1222 {
1223 $$ = build_method_invocation ($1, $3);
1224 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1225 $$ = java_method_add_stmt (current_function_decl, $$);
1226 }
e04a16fb
AG
1227 /* Added, JDK1.1 inner classes. Modified because the rule
1228 'primary' couldn't work. */
1229| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1230 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1231| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1232 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1233;
1234
1235this_or_super: /* Added, simplifies error diagnostics */
1236 THIS_TK
1237 {
9ee9b555 1238 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1239 EXPR_WFL_LINECOL (wfl) = $1.location;
1240 $$ = wfl;
1241 }
1242| SUPER_TK
1243 {
9ee9b555 1244 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1245 EXPR_WFL_LINECOL (wfl) = $1.location;
1246 $$ = wfl;
1247 }
1248;
1249
1250/* 19.9 Productions from 9: Interfaces */
1251/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1252interface_declaration:
1253 INTERFACE_TK identifier
1254 { create_interface (0, $2, NULL_TREE); }
1255 interface_body
e04a16fb
AG
1256| modifiers INTERFACE_TK identifier
1257 { create_interface ($1, $3, NULL_TREE); }
1258 interface_body
e04a16fb
AG
1259| INTERFACE_TK identifier extends_interfaces
1260 { create_interface (0, $2, $3); }
1261 interface_body
e04a16fb
AG
1262| modifiers INTERFACE_TK identifier extends_interfaces
1263 { create_interface ($1, $3, $4); }
1264 interface_body
e04a16fb 1265| INTERFACE_TK identifier error
0b4d333e 1266 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1267| modifiers INTERFACE_TK identifier error
0b4d333e 1268 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1269;
1270
1271extends_interfaces:
1272 EXTENDS_TK interface_type
1273 {
1274 ctxp->interface_number = 1;
1275 $$ = build_tree_list ($2, NULL_TREE);
1276 }
1277| extends_interfaces C_TK interface_type
1278 {
1279 ctxp->interface_number++;
1280 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1281 }
1282| EXTENDS_TK error
1283 {yyerror ("Invalid interface type"); RECOVER;}
1284| extends_interfaces C_TK error
1285 {yyerror ("Missing term"); RECOVER;}
1286;
1287
1288interface_body:
1289 OCB_TK CCB_TK
1290 { $$ = NULL_TREE; }
1291| OCB_TK interface_member_declarations CCB_TK
1292 { $$ = NULL_TREE; }
1293;
1294
1295interface_member_declarations:
1296 interface_member_declaration
1297| interface_member_declarations interface_member_declaration
1298;
1299
1300interface_member_declaration:
1301 constant_declaration
1302| abstract_method_declaration
1303| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1304 { end_class_declaration (1); }
1305| interface_declaration /* Added, JDK1.1 inner interfaces */
1306 { end_class_declaration (1); }
e04a16fb
AG
1307;
1308
1309constant_declaration:
1310 field_declaration
1311;
1312
1313abstract_method_declaration:
1314 method_header SC_TK
1315 {
1316 check_abstract_method_header ($1);
1317 current_function_decl = NULL_TREE; /* FIXME ? */
1318 }
1319| method_header error
1320 {yyerror ("';' expected"); RECOVER;}
1321;
1322
1323/* 19.10 Productions from 10: Arrays */
1324array_initializer:
1325 OCB_TK CCB_TK
1179ebc2 1326 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1327| OCB_TK variable_initializers CCB_TK
f8976021 1328 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1329| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1330 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1331;
1332
1333variable_initializers:
1334 variable_initializer
f8976021
APB
1335 {
1336 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1337 $1, NULL_TREE);
1338 }
e04a16fb 1339| variable_initializers C_TK variable_initializer
1179ebc2
APB
1340 {
1341 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1342 }
e04a16fb
AG
1343| variable_initializers C_TK error
1344 {yyerror ("Missing term"); RECOVER;}
1345;
1346
1347/* 19.11 Production from 14: Blocks and Statements */
1348block:
1349 OCB_TK CCB_TK
7f10c2e2
APB
1350 {
1351 /* Store the location of the `}' when doing xrefs */
1352 if (current_function_decl && flag_emit_xref)
1353 DECL_END_SOURCE_LINE (current_function_decl) =
1354 EXPR_WFL_ADD_COL ($2.location, 1);
1355 $$ = empty_stmt_node;
1356 }
22eed1e6
APB
1357| block_begin block_statements block_end
1358 { $$ = $3; }
1359;
1360
1361block_begin:
1362 OCB_TK
e04a16fb 1363 { enter_block (); }
22eed1e6
APB
1364;
1365
1366block_end:
e04a16fb
AG
1367 CCB_TK
1368 {
1369 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1370 /* Store the location of the `}' when doing xrefs */
1371 if (current_function_decl && flag_emit_xref)
1372 DECL_END_SOURCE_LINE (current_function_decl) =
1373 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1374 $$ = exit_block ();
c280e37a
APB
1375 if (!BLOCK_SUBBLOCKS ($$))
1376 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1377 }
1378;
1379
1380block_statements:
1381 block_statement
1382| block_statements block_statement
1383;
1384
1385block_statement:
1386 local_variable_declaration_statement
1387| statement
15fdcfe9 1388 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1389| class_declaration /* Added, JDK1.1 local classes */
1390 {
1391 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1392 end_class_declaration (1);
1393 }
e04a16fb
AG
1394;
1395
1396local_variable_declaration_statement:
1397 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1398;
1399
1400local_variable_declaration:
1401 type variable_declarators
1402 { declare_local_variables (0, $1, $2); }
a003f638 1403| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1404 { declare_local_variables ($1, $2, $3); }
1405;
1406
1407statement:
1408 statement_without_trailing_substatement
1409| labeled_statement
e04a16fb 1410| if_then_statement
e04a16fb 1411| if_then_else_statement
e04a16fb 1412| while_statement
e04a16fb 1413| for_statement
cd9643f7 1414 { $$ = exit_block (); }
e04a16fb
AG
1415;
1416
1417statement_nsi:
1418 statement_without_trailing_substatement
1419| labeled_statement_nsi
e04a16fb 1420| if_then_else_statement_nsi
e04a16fb 1421| while_statement_nsi
e04a16fb 1422| for_statement_nsi
9dd939b2 1423 { $$ = exit_block (); }
e04a16fb
AG
1424;
1425
1426statement_without_trailing_substatement:
1427 block
e04a16fb 1428| empty_statement
e04a16fb 1429| expression_statement
e04a16fb 1430| switch_statement
e04a16fb 1431| do_statement
e04a16fb 1432| break_statement
e04a16fb 1433| continue_statement
e04a16fb
AG
1434| return_statement
1435| synchronized_statement
e04a16fb 1436| throw_statement
e04a16fb 1437| try_statement
e04a16fb
AG
1438;
1439
1440empty_statement:
1441 SC_TK
5f1c312a
APB
1442 {
1443 if (flag_extraneous_semicolon)
1444 {
1445 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1446 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1447 }
1448 $$ = empty_stmt_node;
1449 }
e04a16fb
AG
1450;
1451
1452label_decl:
1453 identifier REL_CL_TK
1454 {
1455 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1456 EXPR_WFL_NODE ($1));
e04a16fb
AG
1457 pushlevel (2);
1458 push_labeled_block ($$);
1459 PUSH_LABELED_BLOCK ($$);
1460 }
1461;
1462
1463labeled_statement:
1464 label_decl statement
b635eb2f 1465 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1466| identifier error
1467 {yyerror ("':' expected"); RECOVER;}
1468;
1469
1470labeled_statement_nsi:
1471 label_decl statement_nsi
b635eb2f 1472 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1473;
1474
1475/* We concentrate here a bunch of error handling rules that we couldn't write
1476 earlier, because expression_statement catches a missing ';'. */
1477expression_statement:
1478 statement_expression SC_TK
1479 {
1480 /* We have a statement. Generate a WFL around it so
1481 we can debug it */
1482 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1483 /* We know we have a statement, so set the debug
1484 info to be eventually generate here. */
1485 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1486 }
1487| error SC_TK
1488 {
29f8b718 1489 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1490 DRECOVER (expr_stmt);
1491 }
1492| error OCB_TK
1493 {
29f8b718 1494 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1495 DRECOVER (expr_stmt);
1496 }
1497| error CCB_TK
1498 {
29f8b718 1499 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1500 DRECOVER (expr_stmt);
1501 }
1502| this_or_super OP_TK error
1503 {yyerror ("')' expected"); RECOVER;}
1504| this_or_super OP_TK CP_TK error
22eed1e6 1505 {
8119c720 1506 parse_ctor_invocation_error ();
22eed1e6
APB
1507 RECOVER;
1508 }
e04a16fb
AG
1509| this_or_super OP_TK argument_list error
1510 {yyerror ("')' expected"); RECOVER;}
1511| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1512 {
8119c720 1513 parse_ctor_invocation_error ();
22eed1e6
APB
1514 RECOVER;
1515 }
e04a16fb
AG
1516| name DOT_TK SUPER_TK error
1517 {yyerror ("'(' expected"); RECOVER;}
1518| name DOT_TK SUPER_TK OP_TK error
1519 {yyerror ("')' expected"); RECOVER;}
1520| name DOT_TK SUPER_TK OP_TK argument_list error
1521 {yyerror ("')' expected"); RECOVER;}
1522| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1523 {yyerror ("';' expected"); RECOVER;}
1524| name DOT_TK SUPER_TK OP_TK CP_TK error
1525 {yyerror ("';' expected"); RECOVER;}
1526;
1527
1528statement_expression:
1529 assignment
1530| pre_increment_expression
e04a16fb 1531| pre_decrement_expression
e04a16fb 1532| post_increment_expression
e04a16fb 1533| post_decrement_expression
e04a16fb
AG
1534| method_invocation
1535| class_instance_creation_expression
e04a16fb
AG
1536;
1537
1538if_then_statement:
1539 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1540 {
1541 $$ = build_if_else_statement ($2.location, $3,
1542 $5, NULL_TREE);
1543 }
e04a16fb
AG
1544| IF_TK error
1545 {yyerror ("'(' expected"); RECOVER;}
1546| IF_TK OP_TK error
1547 {yyerror ("Missing term"); RECOVER;}
1548| IF_TK OP_TK expression error
1549 {yyerror ("')' expected"); RECOVER;}
1550;
1551
1552if_then_else_statement:
1553 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1554 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1555;
1556
1557if_then_else_statement_nsi:
1558 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1559 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1560;
1561
1562switch_statement:
15fdcfe9
PB
1563 switch_expression
1564 {
1565 enter_block ();
1566 }
1567 switch_block
b67d701b 1568 {
15fdcfe9 1569 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1570 I.e. make the last statment also have its own
1571 COMPOUND_EXPR. */
15fdcfe9
PB
1572 maybe_absorb_scoping_blocks ();
1573 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1574 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1575 }
1576;
1577
1578switch_expression:
1579 SWITCH_TK OP_TK expression CP_TK
1580 {
1581 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1582 EXPR_WFL_LINECOL ($$) = $2.location;
1583 }
e04a16fb
AG
1584| SWITCH_TK error
1585 {yyerror ("'(' expected"); RECOVER;}
1586| SWITCH_TK OP_TK error
1587 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1588| SWITCH_TK OP_TK expression CP_TK error
1589 {yyerror ("'{' expected"); RECOVER;}
1590;
1591
f8976021
APB
1592/* Default assignment is there to avoid type node on switch_block
1593 node. */
1594
e04a16fb
AG
1595switch_block:
1596 OCB_TK CCB_TK
f8976021 1597 { $$ = NULL_TREE; }
e04a16fb 1598| OCB_TK switch_labels CCB_TK
f8976021 1599 { $$ = NULL_TREE; }
e04a16fb 1600| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1601 { $$ = NULL_TREE; }
e04a16fb 1602| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1603 { $$ = NULL_TREE; }
e04a16fb
AG
1604;
1605
1606switch_block_statement_groups:
1607 switch_block_statement_group
1608| switch_block_statement_groups switch_block_statement_group
1609;
1610
1611switch_block_statement_group:
15fdcfe9 1612 switch_labels block_statements
e04a16fb
AG
1613;
1614
e04a16fb
AG
1615switch_labels:
1616 switch_label
1617| switch_labels switch_label
1618;
1619
1620switch_label:
1621 CASE_TK constant_expression REL_CL_TK
b67d701b 1622 {
15fdcfe9
PB
1623 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1624 EXPR_WFL_LINECOL (lab) = $1.location;
1625 java_method_add_stmt (current_function_decl, lab);
b67d701b 1626 }
e04a16fb 1627| DEFAULT_TK REL_CL_TK
b67d701b 1628 {
ac39dac0 1629 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1630 EXPR_WFL_LINECOL (lab) = $1.location;
1631 java_method_add_stmt (current_function_decl, lab);
b67d701b 1632 }
e04a16fb
AG
1633| CASE_TK error
1634 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1635| CASE_TK constant_expression error
1636 {yyerror ("':' expected"); RECOVER;}
1637| DEFAULT_TK error
1638 {yyerror ("':' expected"); RECOVER;}
1639;
1640
1641while_expression:
1642 WHILE_TK OP_TK expression CP_TK
1643 {
1644 tree body = build_loop_body ($2.location, $3, 0);
1645 $$ = build_new_loop (body);
1646 }
1647;
1648
1649while_statement:
1650 while_expression statement
b635eb2f 1651 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1652| WHILE_TK error
1653 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1654| WHILE_TK OP_TK error
1655 {yyerror ("Missing term and ')' expected"); RECOVER;}
1656| WHILE_TK OP_TK expression error
1657 {yyerror ("')' expected"); RECOVER;}
1658;
1659
1660while_statement_nsi:
1661 while_expression statement_nsi
b635eb2f 1662 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1663;
1664
1665do_statement_begin:
1666 DO_TK
1667 {
1668 tree body = build_loop_body (0, NULL_TREE, 1);
1669 $$ = build_new_loop (body);
1670 }
1671 /* Need error handing here. FIXME */
1672;
1673
1674do_statement:
1675 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1676 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1677;
1678
1679for_statement:
1680 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1681 {
1682 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1683 $3 = build_wfl_node ($3);
1684 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1685 }
e04a16fb
AG
1686| for_begin SC_TK SC_TK for_update CP_TK statement
1687 {
b635eb2f 1688 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1689 /* We have not condition, so we get rid of the EXIT_EXPR */
1690 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1691 empty_stmt_node;
e04a16fb
AG
1692 }
1693| for_begin SC_TK error
1694 {yyerror ("Invalid control expression"); RECOVER;}
1695| for_begin SC_TK expression SC_TK error
1696 {yyerror ("Invalid update expression"); RECOVER;}
1697| for_begin SC_TK SC_TK error
1698 {yyerror ("Invalid update expression"); RECOVER;}
1699;
1700
1701for_statement_nsi:
1702 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1703 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1704| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1705 {
b635eb2f 1706 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1707 /* We have not condition, so we get rid of the EXIT_EXPR */
1708 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1709 empty_stmt_node;
e04a16fb
AG
1710 }
1711;
1712
1713for_header:
1714 FOR_TK OP_TK
1715 {
1716 /* This scope defined for local variable that may be
1717 defined within the scope of the for loop */
1718 enter_block ();
1719 }
1720| FOR_TK error
1721 {yyerror ("'(' expected"); DRECOVER(for_1);}
1722| FOR_TK OP_TK error
1723 {yyerror ("Invalid init statement"); RECOVER;}
1724;
1725
1726for_begin:
1727 for_header for_init
1728 {
1729 /* We now declare the loop body. The loop is
1730 declared as a for loop. */
1731 tree body = build_loop_body (0, NULL_TREE, 0);
1732 $$ = build_new_loop (body);
c2952b01 1733 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1734 /* The loop is added to the current block the for
1735 statement is defined within */
1736 java_method_add_stmt (current_function_decl, $$);
1737 }
1738;
1739for_init: /* Can be empty */
9bbc7d9f 1740 { $$ = empty_stmt_node; }
e04a16fb
AG
1741| statement_expression_list
1742 {
1743 /* Init statement recorded within the previously
1744 defined block scope */
1745 $$ = java_method_add_stmt (current_function_decl, $1);
1746 }
1747| local_variable_declaration
1748 {
1749 /* Local variable are recorded within the previously
1750 defined block scope */
1751 $$ = NULL_TREE;
1752 }
1753| statement_expression_list error
1754 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1755;
1756
1757for_update: /* Can be empty */
9bbc7d9f 1758 {$$ = empty_stmt_node;}
e04a16fb
AG
1759| statement_expression_list
1760 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1761;
1762
1763statement_expression_list:
1764 statement_expression
1765 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1766| statement_expression_list C_TK statement_expression
1767 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1768| statement_expression_list C_TK error
1769 {yyerror ("Missing term"); RECOVER;}
1770;
1771
1772break_statement:
1773 BREAK_TK SC_TK
1774 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1775| BREAK_TK identifier SC_TK
1776 { $$ = build_bc_statement ($1.location, 1, $2); }
1777| BREAK_TK error
1778 {yyerror ("Missing term"); RECOVER;}
1779| BREAK_TK identifier error
1780 {yyerror ("';' expected"); RECOVER;}
1781;
1782
1783continue_statement:
1784 CONTINUE_TK SC_TK
1785 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1786| CONTINUE_TK identifier SC_TK
1787 { $$ = build_bc_statement ($1.location, 0, $2); }
1788| CONTINUE_TK error
1789 {yyerror ("Missing term"); RECOVER;}
1790| CONTINUE_TK identifier error
1791 {yyerror ("';' expected"); RECOVER;}
1792;
1793
1794return_statement:
1795 RETURN_TK SC_TK
1796 { $$ = build_return ($1.location, NULL_TREE); }
1797| RETURN_TK expression SC_TK
1798 { $$ = build_return ($1.location, $2); }
1799| RETURN_TK error
1800 {yyerror ("Missing term"); RECOVER;}
1801| RETURN_TK expression error
1802 {yyerror ("';' expected"); RECOVER;}
1803;
1804
1805throw_statement:
1806 THROW_TK expression SC_TK
b9f7e36c
APB
1807 {
1808 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1809 EXPR_WFL_LINECOL ($$) = $1.location;
1810 }
e04a16fb
AG
1811| THROW_TK error
1812 {yyerror ("Missing term"); RECOVER;}
1813| THROW_TK expression error
1814 {yyerror ("';' expected"); RECOVER;}
1815;
1816
1817synchronized_statement:
1818 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1819 {
1820 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1821 EXPR_WFL_LINECOL ($$) =
1822 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1823 }
e04a16fb
AG
1824| synchronized OP_TK expression CP_TK error
1825 {yyerror ("'{' expected"); RECOVER;}
1826| synchronized error
1827 {yyerror ("'(' expected"); RECOVER;}
1828| synchronized OP_TK error CP_TK
1829 {yyerror ("Missing term"); RECOVER;}
1830| synchronized OP_TK error
1831 {yyerror ("Missing term"); RECOVER;}
1832;
1833
b9f7e36c 1834synchronized:
efa0a23f 1835 modifiers
e04a16fb 1836 {
781b0558
KG
1837 check_modifiers (
1838 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1839 $1, ACC_SYNCHRONIZED);
1840 if ($1 != ACC_SYNCHRONIZED)
1841 MODIFIER_WFL (SYNCHRONIZED_TK) =
1842 build_wfl_node (NULL_TREE);
e04a16fb
AG
1843 }
1844;
1845
1846try_statement:
1847 TRY_TK block catches
a7d8d81f 1848 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1849| TRY_TK block finally
a7d8d81f 1850 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1851| TRY_TK block catches finally
2aa11e97
APB
1852 { $$ = build_try_finally_statement
1853 ($1.location, build_try_statement ($1.location,
1854 $2, $3), $4);
1855 }
e04a16fb
AG
1856| TRY_TK error
1857 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1858;
1859
1860catches:
1861 catch_clause
1862| catches catch_clause
b67d701b
PB
1863 {
1864 TREE_CHAIN ($2) = $1;
1865 $$ = $2;
1866 }
e04a16fb
AG
1867;
1868
1869catch_clause:
b67d701b
PB
1870 catch_clause_parameter block
1871 {
1872 java_method_add_stmt (current_function_decl, $2);
1873 exit_block ();
1874 $$ = $1;
1875 }
1876
1877catch_clause_parameter:
1878 CATCH_TK OP_TK formal_parameter CP_TK
1879 {
1880 /* We add a block to define a scope for
1881 formal_parameter (CCBP). The formal parameter is
1882 declared initialized by the appropriate function
1883 call */
1884 tree ccpb = enter_block ();
1885 tree init = build_assignment (ASSIGN_TK, $2.location,
1886 TREE_PURPOSE ($3),
1887 soft_exceptioninfo_call_node);
1888 declare_local_variables (0, TREE_VALUE ($3),
1889 build_tree_list (TREE_PURPOSE ($3),
1890 init));
1891 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1892 EXPR_WFL_LINECOL ($$) = $1.location;
1893 }
e04a16fb 1894| CATCH_TK error
97f30284 1895 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1896| CATCH_TK OP_TK error
97f30284
APB
1897 {
1898 yyerror ("Missing term or ')' expected");
1899 RECOVER; $$ = NULL_TREE;
1900 }
b67d701b 1901| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1902 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1903;
1904
1905finally:
1906 FINALLY_TK block
a7d8d81f 1907 { $$ = $2; }
e04a16fb
AG
1908| FINALLY_TK error
1909 {yyerror ("'{' expected"); RECOVER; }
1910;
1911
1912/* 19.12 Production from 15: Expressions */
1913primary:
1914 primary_no_new_array
1915| array_creation_expression
1916;
1917
1918primary_no_new_array:
1919 literal
1920| THIS_TK
1921 { $$ = build_this ($1.location); }
1922| OP_TK expression CP_TK
1923 {$$ = $2;}
1924| class_instance_creation_expression
1925| field_access
1926| method_invocation
1927| array_access
c2952b01 1928| type_literals
e04a16fb
AG
1929 /* Added, JDK1.1 inner classes. Documentation is wrong
1930 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1931 exist. Used name: instead. */
e04a16fb 1932| name DOT_TK THIS_TK
c2952b01
APB
1933 {
1934 tree wfl = build_wfl_node (this_identifier_node);
1935 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1936 }
e04a16fb
AG
1937| OP_TK expression error
1938 {yyerror ("')' expected"); RECOVER;}
1939| name DOT_TK error
1940 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1941| primitive_type DOT_TK error
1942 {yyerror ("'class' expected" ); RECOVER;}
1943| VOID_TK DOT_TK error
1944 {yyerror ("'class' expected" ); RECOVER;}
1945;
1946
c2952b01
APB
1947type_literals:
1948 name DOT_TK CLASS_TK
1949 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1950| array_type DOT_TK CLASS_TK
c2952b01
APB
1951 { $$ = build_incomplete_class_ref ($2.location, $1); }
1952| primitive_type DOT_TK CLASS_TK
1953 { $$ = build_class_ref ($1); }
1954| VOID_TK DOT_TK CLASS_TK
1955 { $$ = build_class_ref (void_type_node); }
1956;
1957
e04a16fb
AG
1958class_instance_creation_expression:
1959 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1960 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1961| NEW_TK class_type OP_TK CP_TK
b67d701b 1962 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1963| anonymous_class_creation
e04a16fb
AG
1964 /* Added, JDK1.1 inner classes, modified to use name or
1965 primary instead of primary solely which couldn't work in
1966 all situations. */
1967| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1968 {
1969 tree ctor = build_new_invocation ($2, NULL_TREE);
1970 $$ = make_qualified_primary ($1, ctor,
1971 EXPR_WFL_LINECOL ($1));
1972 }
e04a16fb
AG
1973| something_dot_new identifier OP_TK CP_TK class_body
1974| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1975 {
1976 tree ctor = build_new_invocation ($2, $4);
1977 $$ = make_qualified_primary ($1, ctor,
1978 EXPR_WFL_LINECOL ($1));
1979 }
e04a16fb
AG
1980| something_dot_new identifier OP_TK argument_list CP_TK class_body
1981| NEW_TK error SC_TK
1982 {yyerror ("'(' expected"); DRECOVER(new_1);}
1983| NEW_TK class_type error
1984 {yyerror ("'(' expected"); RECOVER;}
1985| NEW_TK class_type OP_TK error
1986 {yyerror ("')' or term expected"); RECOVER;}
1987| NEW_TK class_type OP_TK argument_list error
1988 {yyerror ("')' expected"); RECOVER;}
1989| something_dot_new error
1990 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1991| something_dot_new identifier error
1992 {yyerror ("'(' expected"); RECOVER;}
1993;
1994
c2952b01
APB
1995/* Created after JDK1.1 rules originally added to
1996 class_instance_creation_expression, but modified to use
1997 'class_type' instead of 'TypeName' (type_name) which is mentionned
1998 in the documentation but doesn't exist. */
1999
2000anonymous_class_creation:
2001 NEW_TK class_type OP_TK argument_list CP_TK
2002 { create_anonymous_class ($1.location, $2); }
2003 class_body
2004 {
2005 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2006 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2007
2008 end_class_declaration (1);
2009
2010 /* Now we can craft the new expression */
2011 $$ = build_new_invocation (id, $4);
2012
2013 /* Note that we can't possibly be here if
2014 `class_type' is an interface (in which case the
2015 anonymous class extends Object and implements
2016 `class_type', hence its constructor can't have
2017 arguments.) */
2018
2019 /* Otherwise, the innerclass must feature a
2020 constructor matching `argument_list'. Anonymous
2021 classes are a bit special: it's impossible to
2022 define constructor for them, hence constructors
2023 must be generated following the hints provided by
2024 the `new' expression. Whether a super constructor
2025 of that nature exists or not is to be verified
2026 later on in verify_constructor_super.
2027
2028 It's during the expansion of a `new' statement
2029 refering to an anonymous class that a ctor will
2030 be generated for the anonymous class, with the
2031 right arguments. */
2032
2033 }
2034| NEW_TK class_type OP_TK CP_TK
2035 { create_anonymous_class ($1.location, $2); }
2036 class_body
2037 {
2038 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2039 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2040
2041 end_class_declaration (1);
2042
2043 /* Now we can craft the new expression. The
2044 statement doesn't need to be remember so that a
2045 constructor can be generated, since its signature
2046 is already known. */
2047 $$ = build_new_invocation (id, NULL_TREE);
2048 }
2049;
2050
e04a16fb
AG
2051something_dot_new: /* Added, not part of the specs. */
2052 name DOT_TK NEW_TK
c2952b01 2053 { $$ = $1; }
e04a16fb 2054| primary DOT_TK NEW_TK
c2952b01 2055 { $$ = $1; }
e04a16fb
AG
2056;
2057
2058argument_list:
2059 expression
2060 {
2061 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2062 ctxp->formal_parameter_number = 1;
2063 }
2064| argument_list C_TK expression
2065 {
2066 ctxp->formal_parameter_number += 1;
2067 $$ = tree_cons (NULL_TREE, $3, $1);
2068 }
2069| argument_list C_TK error
2070 {yyerror ("Missing term"); RECOVER;}
2071;
2072
2073array_creation_expression:
2074 NEW_TK primitive_type dim_exprs
2075 { $$ = build_newarray_node ($2, $3, 0); }
2076| NEW_TK class_or_interface_type dim_exprs
2077 { $$ = build_newarray_node ($2, $3, 0); }
2078| NEW_TK primitive_type dim_exprs dims
c7303e41 2079 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2080| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2081 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2082 /* Added, JDK1.1 anonymous array. Initial documentation rule
2083 modified */
2084| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2085 {
2086 char *sig;
c7303e41
APB
2087 int osb = pop_current_osb (ctxp);
2088 while (osb--)
c2952b01
APB
2089 obstack_1grow (&temporary_obstack, '[');
2090 sig = obstack_finish (&temporary_obstack);
2091 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2092 $2, get_identifier (sig), $4);
2093 }
e04a16fb 2094| NEW_TK primitive_type dims array_initializer
c2952b01 2095 {
c7303e41 2096 int osb = pop_current_osb (ctxp);
c2952b01 2097 tree type = $2;
c7303e41 2098 while (osb--)
c2952b01
APB
2099 type = build_java_array_type (type, -1);
2100 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2101 build_pointer_type (type), NULL_TREE, $4);
2102 }
e04a16fb
AG
2103| NEW_TK error CSB_TK
2104 {yyerror ("'[' expected"); DRECOVER ("]");}
2105| NEW_TK error OSB_TK
2106 {yyerror ("']' expected"); RECOVER;}
2107;
2108
2109dim_exprs:
2110 dim_expr
2111 { $$ = build_tree_list (NULL_TREE, $1); }
2112| dim_exprs dim_expr
2113 { $$ = tree_cons (NULL_TREE, $2, $$); }
2114;
2115
2116dim_expr:
2117 OSB_TK expression CSB_TK
2118 {
9a7ab4b3
APB
2119 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2120 {
2121 $2 = build_wfl_node ($2);
2122 TREE_TYPE ($2) = NULL_TREE;
2123 }
e04a16fb
AG
2124 EXPR_WFL_LINECOL ($2) = $1.location;
2125 $$ = $2;
2126 }
2127| OSB_TK expression error
2128 {yyerror ("']' expected"); RECOVER;}
2129| OSB_TK error
2130 {
2131 yyerror ("Missing term");
2132 yyerror ("']' expected");
2133 RECOVER;
2134 }
2135;
2136
2137dims:
2138 OSB_TK CSB_TK
ba179f9f
APB
2139 {
2140 int allocate = 0;
2141 /* If not initialized, allocate memory for the osb
2142 numbers stack */
2143 if (!ctxp->osb_limit)
2144 {
2145 allocate = ctxp->osb_limit = 32;
2146 ctxp->osb_depth = -1;
2147 }
c2952b01 2148 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2149 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2150 allocate = ctxp->osb_limit << 1;
2151
2152 if (allocate)
2153 {
2154 allocate *= sizeof (int);
2155 if (ctxp->osb_number)
2156 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2157 allocate);
2158 else
2159 ctxp->osb_number = (int *)xmalloc (allocate);
2160 }
2161 ctxp->osb_depth++;
2162 CURRENT_OSB (ctxp) = 1;
2163 }
e04a16fb 2164| dims OSB_TK CSB_TK
ba179f9f 2165 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2166| dims OSB_TK error
2167 { yyerror ("']' expected"); RECOVER;}
2168;
2169
2170field_access:
2171 primary DOT_TK identifier
2172 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2173 /* FIXME - REWRITE TO:
2174 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2175| SUPER_TK DOT_TK identifier
2176 {
6e22695a 2177 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2178 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2179 $$ = make_qualified_name (super_wfl, $3, $2.location);
2180 }
2181| SUPER_TK error
2182 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2183;
2184
2185method_invocation:
2186 name OP_TK CP_TK
2187 { $$ = build_method_invocation ($1, NULL_TREE); }
2188| name OP_TK argument_list CP_TK
2189 { $$ = build_method_invocation ($1, $3); }
2190| primary DOT_TK identifier OP_TK CP_TK
2191 {
22eed1e6
APB
2192 if (TREE_CODE ($1) == THIS_EXPR)
2193 $$ = build_this_super_qualified_invocation
2194 (1, $3, NULL_TREE, 0, $2.location);
2195 else
2196 {
2197 tree invok = build_method_invocation ($3, NULL_TREE);
2198 $$ = make_qualified_primary ($1, invok, $2.location);
2199 }
e04a16fb
AG
2200 }
2201| primary DOT_TK identifier OP_TK argument_list CP_TK
2202 {
22eed1e6
APB
2203 if (TREE_CODE ($1) == THIS_EXPR)
2204 $$ = build_this_super_qualified_invocation
2205 (1, $3, $5, 0, $2.location);
2206 else
2207 {
2208 tree invok = build_method_invocation ($3, $5);
2209 $$ = make_qualified_primary ($1, invok, $2.location);
2210 }
e04a16fb
AG
2211 }
2212| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2213 {
2214 $$ = build_this_super_qualified_invocation
2215 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2216 }
2217| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2218 {
22eed1e6
APB
2219 $$ = build_this_super_qualified_invocation
2220 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2221 }
2222 /* Screws up thing. I let it here until I'm convinced it can
2223 be removed. FIXME
2224| primary DOT_TK error
2225 {yyerror ("'(' expected"); DRECOVER(bad);} */
2226| SUPER_TK DOT_TK error CP_TK
2227 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2228| SUPER_TK DOT_TK error DOT_TK
2229 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2230;
2231
2232array_access:
2233 name OSB_TK expression CSB_TK
2234 { $$ = build_array_ref ($2.location, $1, $3); }
2235| primary_no_new_array OSB_TK expression CSB_TK
2236 { $$ = build_array_ref ($2.location, $1, $3); }
2237| name OSB_TK error
2238 {
2239 yyerror ("Missing term and ']' expected");
2240 DRECOVER(array_access);
2241 }
2242| name OSB_TK expression error
2243 {
2244 yyerror ("']' expected");
2245 DRECOVER(array_access);
2246 }
2247| primary_no_new_array OSB_TK error
2248 {
2249 yyerror ("Missing term and ']' expected");
2250 DRECOVER(array_access);
2251 }
2252| primary_no_new_array OSB_TK expression error
2253 {
2254 yyerror ("']' expected");
2255 DRECOVER(array_access);
2256 }
2257;
2258
2259postfix_expression:
2260 primary
2261| name
2262| post_increment_expression
2263| post_decrement_expression
2264;
2265
2266post_increment_expression:
2267 postfix_expression INCR_TK
2268 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2269;
2270
2271post_decrement_expression:
2272 postfix_expression DECR_TK
2273 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2274;
2275
2276unary_expression:
2277 pre_increment_expression
2278| pre_decrement_expression
2279| PLUS_TK unary_expression
2280 {$$ = build_unaryop ($1.token, $1.location, $2); }
2281| MINUS_TK unary_expression
2282 {$$ = build_unaryop ($1.token, $1.location, $2); }
2283| unary_expression_not_plus_minus
2284| PLUS_TK error
2285 {yyerror ("Missing term"); RECOVER}
2286| MINUS_TK error
2287 {yyerror ("Missing term"); RECOVER}
2288;
2289
2290pre_increment_expression:
2291 INCR_TK unary_expression
2292 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2293| INCR_TK error
2294 {yyerror ("Missing term"); RECOVER}
2295;
2296
2297pre_decrement_expression:
2298 DECR_TK unary_expression
2299 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2300| DECR_TK error
2301 {yyerror ("Missing term"); RECOVER}
2302;
2303
2304unary_expression_not_plus_minus:
2305 postfix_expression
2306| NOT_TK unary_expression
2307 {$$ = build_unaryop ($1.token, $1.location, $2); }
2308| NEG_TK unary_expression
2309 {$$ = build_unaryop ($1.token, $1.location, $2); }
2310| cast_expression
2311| NOT_TK error
2312 {yyerror ("Missing term"); RECOVER}
2313| NEG_TK error
2314 {yyerror ("Missing term"); RECOVER}
2315;
2316
2317cast_expression: /* Error handling here is potentially weak */
2318 OP_TK primitive_type dims CP_TK unary_expression
2319 {
2320 tree type = $2;
c7303e41
APB
2321 int osb = pop_current_osb (ctxp);
2322 while (osb--)
e04a16fb
AG
2323 type = build_java_array_type (type, -1);
2324 $$ = build_cast ($1.location, type, $5);
2325 }
2326| OP_TK primitive_type CP_TK unary_expression
2327 { $$ = build_cast ($1.location, $2, $4); }
2328| OP_TK expression CP_TK unary_expression_not_plus_minus
2329 { $$ = build_cast ($1.location, $2, $4); }
2330| OP_TK name dims CP_TK unary_expression_not_plus_minus
2331 {
49f48c71 2332 const char *ptr;
c7303e41
APB
2333 int osb = pop_current_osb (ctxp);
2334 while (osb--)
e04a16fb
AG
2335 obstack_1grow (&temporary_obstack, '[');
2336 obstack_grow0 (&temporary_obstack,
2337 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2338 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2339 ptr = obstack_finish (&temporary_obstack);
2340 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2341 $$ = build_cast ($1.location, $2, $5);
2342 }
2343| OP_TK primitive_type OSB_TK error
2344 {yyerror ("']' expected, invalid type expression");}
2345| OP_TK error
2346 {
29f8b718 2347 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2348 RECOVER;
2349 }
2350| OP_TK primitive_type dims CP_TK error
2351 {yyerror ("Missing term"); RECOVER;}
2352| OP_TK primitive_type CP_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354| OP_TK name dims CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356;
2357
2358multiplicative_expression:
2359 unary_expression
2360| multiplicative_expression MULT_TK unary_expression
2361 {
2362 $$ = build_binop (BINOP_LOOKUP ($2.token),
2363 $2.location, $1, $3);
2364 }
2365| multiplicative_expression DIV_TK unary_expression
2366 {
2367 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2368 $1, $3);
2369 }
2370| multiplicative_expression REM_TK unary_expression
2371 {
2372 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2373 $1, $3);
2374 }
2375| multiplicative_expression MULT_TK error
2376 {yyerror ("Missing term"); RECOVER;}
2377| multiplicative_expression DIV_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| multiplicative_expression REM_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381;
2382
2383additive_expression:
2384 multiplicative_expression
2385| additive_expression PLUS_TK multiplicative_expression
2386 {
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2388 $1, $3);
2389 }
2390| additive_expression MINUS_TK multiplicative_expression
2391 {
2392 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2393 $1, $3);
2394 }
2395| additive_expression PLUS_TK error
2396 {yyerror ("Missing term"); RECOVER;}
2397| additive_expression MINUS_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399;
2400
2401shift_expression:
2402 additive_expression
2403| shift_expression LS_TK additive_expression
2404 {
2405 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2406 $1, $3);
2407 }
2408| shift_expression SRS_TK additive_expression
2409 {
2410 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2411 $1, $3);
2412 }
2413| shift_expression ZRS_TK additive_expression
2414 {
2415 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2416 $1, $3);
2417 }
2418| shift_expression LS_TK error
2419 {yyerror ("Missing term"); RECOVER;}
2420| shift_expression SRS_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422| shift_expression ZRS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424;
2425
2426relational_expression:
2427 shift_expression
2428| relational_expression LT_TK shift_expression
2429 {
2430 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2431 $1, $3);
2432 }
2433| relational_expression GT_TK shift_expression
2434 {
2435 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2436 $1, $3);
2437 }
2438| relational_expression LTE_TK shift_expression
2439 {
2440 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2441 $1, $3);
2442 }
2443| relational_expression GTE_TK shift_expression
2444 {
2445 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2446 $1, $3);
2447 }
2448| relational_expression INSTANCEOF_TK reference_type
5e942c50 2449 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2450| relational_expression LT_TK error
2451 {yyerror ("Missing term"); RECOVER;}
2452| relational_expression GT_TK error
2453 {yyerror ("Missing term"); RECOVER;}
2454| relational_expression LTE_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456| relational_expression GTE_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression INSTANCEOF_TK error
2459 {yyerror ("Invalid reference type"); RECOVER;}
2460;
2461
2462equality_expression:
2463 relational_expression
2464| equality_expression EQ_TK relational_expression
2465 {
2466 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 $1, $3);
2468 }
2469| equality_expression NEQ_TK relational_expression
2470 {
2471 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 $1, $3);
2473 }
2474| equality_expression EQ_TK error
2475 {yyerror ("Missing term"); RECOVER;}
2476| equality_expression NEQ_TK error
2477 {yyerror ("Missing term"); RECOVER;}
2478;
2479
2480and_expression:
2481 equality_expression
2482| and_expression AND_TK equality_expression
2483 {
2484 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2485 $1, $3);
2486 }
2487| and_expression AND_TK error
2488 {yyerror ("Missing term"); RECOVER;}
2489;
2490
2491exclusive_or_expression:
2492 and_expression
2493| exclusive_or_expression XOR_TK and_expression
2494 {
2495 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2496 $1, $3);
2497 }
2498| exclusive_or_expression XOR_TK error
2499 {yyerror ("Missing term"); RECOVER;}
2500;
2501
2502inclusive_or_expression:
2503 exclusive_or_expression
2504| inclusive_or_expression OR_TK exclusive_or_expression
2505 {
2506 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2507 $1, $3);
2508 }
2509| inclusive_or_expression OR_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2511;
2512
2513conditional_and_expression:
2514 inclusive_or_expression
2515| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2516 {
2517 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2518 $1, $3);
2519 }
2520| conditional_and_expression BOOL_AND_TK error
2521 {yyerror ("Missing term"); RECOVER;}
2522;
2523
2524conditional_or_expression:
2525 conditional_and_expression
2526| conditional_or_expression BOOL_OR_TK conditional_and_expression
2527 {
2528 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2529 $1, $3);
2530 }
2531| conditional_or_expression BOOL_OR_TK error
2532 {yyerror ("Missing term"); RECOVER;}
2533;
2534
2535conditional_expression: /* Error handling here is weak */
2536 conditional_or_expression
2537| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2538 {
2539 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2540 EXPR_WFL_LINECOL ($$) = $2.location;
2541 }
e04a16fb
AG
2542| conditional_or_expression REL_QM_TK REL_CL_TK error
2543 {
2544 YYERROR_NOW;
2545 yyerror ("Missing term");
2546 DRECOVER (1);
2547 }
2548| conditional_or_expression REL_QM_TK error
2549 {yyerror ("Missing term"); DRECOVER (2);}
2550| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2551 {yyerror ("Missing term"); DRECOVER (3);}
2552;
2553
2554assignment_expression:
2555 conditional_expression
2556| assignment
2557;
2558
2559assignment:
2560 left_hand_side assignment_operator assignment_expression
2561 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2562| left_hand_side assignment_operator error
2563 {
29f8b718 2564 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2565 DRECOVER (assign);
2566 }
2567;
2568
2569left_hand_side:
2570 name
2571| field_access
2572| array_access
2573;
2574
2575assignment_operator:
2576 ASSIGN_ANY_TK
2577| ASSIGN_TK
2578;
2579
2580expression:
2581 assignment_expression
2582;
2583
2584constant_expression:
2585 expression
2586;
2587
2588%%
c7303e41
APB
2589
2590/* Helper function to retrieve an OSB count. Should be used when the
2591 `dims:' rule is being used. */
2592
2593static int
2594pop_current_osb (ctxp)
2595 struct parser_ctxt *ctxp;
2596{
2597 int to_return;
2598
2599 if (ctxp->osb_depth < 0)
400500c4 2600 abort ();
c7303e41
APB
2601
2602 to_return = CURRENT_OSB (ctxp);
2603 ctxp->osb_depth--;
2604
2605 return to_return;
2606}
2607
e04a16fb
AG
2608\f
2609
c2952b01
APB
2610/* This section of the code deal with save/restoring parser contexts.
2611 Add mode documentation here. FIXME */
e04a16fb 2612
c2952b01
APB
2613/* Helper function. Create a new parser context. With
2614 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2615 context is copied, otherwise, the new context is zeroed. The newly
2616 created context becomes the current one. */
e04a16fb 2617
c2952b01
APB
2618static void
2619create_new_parser_context (copy_from_previous)
2620 int copy_from_previous;
e04a16fb 2621{
c2952b01 2622 struct parser_ctxt *new;
e04a16fb 2623
c2952b01
APB
2624 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2625 if (copy_from_previous)
2626 {
2627 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2628 new->saved_data_ctx = 1;
2629 }
2630 else
2e09e75a 2631 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2632
e04a16fb
AG
2633 new->next = ctxp;
2634 ctxp = new;
c2952b01
APB
2635}
2636
2637/* Create a new parser context and make it the current one. */
2638
2639void
2640java_push_parser_context ()
2641{
2642 create_new_parser_context (0);
e04a16fb 2643 if (ctxp->next)
5e942c50
APB
2644 {
2645 ctxp->incomplete_class = ctxp->next->incomplete_class;
2646 ctxp->gclass_list = ctxp->next->gclass_list;
2647 }
e04a16fb
AG
2648}
2649
c2952b01
APB
2650void
2651java_pop_parser_context (generate)
2652 int generate;
2653{
2654 tree current;
2655 struct parser_ctxt *toFree, *next;
2656
2657 if (!ctxp)
2658 return;
2659
2660 toFree = ctxp;
2661 next = ctxp->next;
2662 if (next)
2663 {
2664 next->incomplete_class = ctxp->incomplete_class;
2665 next->gclass_list = ctxp->gclass_list;
2666 lineno = ctxp->lineno;
19e223db 2667 current_class = ctxp->class_type;
c2952b01
APB
2668 }
2669
d19cbcb5
TT
2670 /* If the old and new lexers differ, then free the old one. */
2671 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2672 java_destroy_lexer (ctxp->lexer);
2673
c2952b01
APB
2674 /* Set the single import class file flag to 0 for the current list
2675 of imported things */
2676 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2677 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2678
2679 /* And restore those of the previous context */
2680 if ((ctxp = next)) /* Assignment is really meant here */
2681 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2682 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2683
2684 /* If we pushed a context to parse a class intended to be generated,
2685 we keep it so we can remember the class. What we could actually
2686 do is to just update a list of class names. */
2687 if (generate)
2688 {
2689 toFree->next = ctxp_for_generation;
2690 ctxp_for_generation = toFree;
2691 }
2692 else
2693 free (toFree);
2694}
2695
2696/* Create a parser context for the use of saving some global
2697 variables. */
2698
e04a16fb
AG
2699void
2700java_parser_context_save_global ()
2701{
22eed1e6
APB
2702 if (!ctxp)
2703 {
2704 java_push_parser_context ();
ee07f4f4
APB
2705 ctxp->saved_data_ctx = 1;
2706 }
c2952b01
APB
2707
2708 /* If this context already stores data, create a new one suitable
2709 for data storage. */
ee07f4f4 2710 else if (ctxp->saved_data)
c2952b01
APB
2711 create_new_parser_context (1);
2712
e04a16fb 2713 ctxp->lineno = lineno;
19e223db 2714 ctxp->class_type = current_class;
e04a16fb 2715 ctxp->filename = input_filename;
19e223db 2716 ctxp->function_decl = current_function_decl;
ee07f4f4 2717 ctxp->saved_data = 1;
e04a16fb
AG
2718}
2719
c2952b01
APB
2720/* Restore some global variables from the previous context. Make the
2721 previous context the current one. */
2722
e04a16fb
AG
2723void
2724java_parser_context_restore_global ()
2725{
e04a16fb 2726 lineno = ctxp->lineno;
19e223db 2727 current_class = ctxp->class_type;
e04a16fb 2728 input_filename = ctxp->filename;
dba41d30
APB
2729 if (wfl_operator)
2730 {
2731 tree s;
2732 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2733 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2734 }
19e223db 2735 current_function_decl = ctxp->function_decl;
c2952b01 2736 ctxp->saved_data = 0;
ee07f4f4
APB
2737 if (ctxp->saved_data_ctx)
2738 java_pop_parser_context (0);
e04a16fb
AG
2739}
2740
c2952b01
APB
2741/* Suspend vital data for the current class/function being parsed so
2742 that an other class can be parsed. Used to let local/anonymous
2743 classes be parsed. */
2744
2745static void
2746java_parser_context_suspend ()
e04a16fb 2747{
c2952b01 2748 /* This makes debugging through java_debug_context easier */
3b304f5b 2749 static const char *name = "<inner buffer context>";
e04a16fb 2750
c2952b01
APB
2751 /* Duplicate the previous context, use it to save the globals we're
2752 interested in */
2753 create_new_parser_context (1);
19e223db
MM
2754 ctxp->function_decl = current_function_decl;
2755 ctxp->class_type = current_class;
5e942c50 2756
c2952b01
APB
2757 /* Then create a new context which inherits all data from the
2758 previous one. This will be the new current context */
2759 create_new_parser_context (1);
2760
2761 /* Help debugging */
2762 ctxp->next->filename = name;
2763}
2764
2765/* Resume vital data for the current class/function being parsed so
2766 that an other class can be parsed. Used to let local/anonymous
2767 classes be parsed. The trick is the data storing file position
2768 informations must be restored to their current value, so parsing
2769 can resume as if no context was ever saved. */
2770
2771static void
2772java_parser_context_resume ()
2773{
2774 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2775 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2776 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2777
2778 /* We need to inherit the list of classes to complete/generate */
2779 restored->incomplete_class = old->incomplete_class;
2780 restored->gclass_list = old->gclass_list;
2781 restored->classd_list = old->classd_list;
2782 restored->class_list = old->class_list;
2783
2784 /* Restore the current class and function from the saver */
19e223db
MM
2785 current_class = saver->class_type;
2786 current_function_decl = saver->function_decl;
c2952b01
APB
2787
2788 /* Retrive the restored context */
2789 ctxp = restored;
2790
2791 /* Re-installed the data for the parsing to carry on */
2792 bcopy (&old->marker_begining, &ctxp->marker_begining,
2793 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2794
2795 /* Buffer context can now be discarded */
2796 free (saver);
2797 free (old);
2798}
2799
2800/* Add a new anchor node to which all statement(s) initializing static
2801 and non static initialized upon declaration field(s) will be
2802 linked. */
2803
2804static void
2805java_parser_context_push_initialized_field ()
2806{
2807 tree node;
2808
2809 node = build_tree_list (NULL_TREE, NULL_TREE);
2810 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2811 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2812
2813 node = build_tree_list (NULL_TREE, NULL_TREE);
2814 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2815 CPC_INITIALIZER_LIST (ctxp) = node;
2816
2817 node = build_tree_list (NULL_TREE, NULL_TREE);
2818 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2819 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2820}
2821
2822/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2823 remember them so we can use it to create and populate the finit$
c2952b01
APB
2824 or <clinit> functions. */
2825
2826static void
2827java_parser_context_pop_initialized_field ()
2828{
2829 tree stmts;
2830 tree class_type = TREE_TYPE (GET_CPC ());
2831
2832 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2833 {
c2952b01
APB
2834 stmts = CPC_INITIALIZER_STMT (ctxp);
2835 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2836 if (stmts && !java_error_count)
2837 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2838 }
2839
c2952b01
APB
2840 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2841 {
2842 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2843 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2844 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2845 /* Keep initialization in order to enforce 8.5 */
2846 if (stmts && !java_error_count)
2847 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2848 }
e04a16fb 2849
c2952b01
APB
2850 /* JDK 1.1 instance initializers */
2851 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2852 {
c2952b01
APB
2853 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2854 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2855 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2856 if (stmts && !java_error_count)
2857 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2858 }
c2952b01
APB
2859}
2860
2861static tree
2862reorder_static_initialized (list)
2863 tree list;
2864{
2865 /* We have to keep things in order. The alias initializer have to
2866 come first, then the initialized regular field, in reverse to
2867 keep them in lexical order. */
2868 tree marker, previous = NULL_TREE;
2869 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2870 if (TREE_CODE (marker) == TREE_LIST
2871 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2872 break;
2873
2874 /* No static initialized, the list is fine as is */
2875 if (!previous)
2876 list = TREE_CHAIN (marker);
2877
2878 /* No marker? reverse the whole list */
2879 else if (!marker)
2880 list = nreverse (list);
2881
2882 /* Otherwise, reverse what's after the marker and the new reordered
2883 sublist will replace the marker. */
b351b287 2884 else
c2952b01
APB
2885 {
2886 TREE_CHAIN (previous) = NULL_TREE;
2887 list = nreverse (list);
2888 list = chainon (TREE_CHAIN (marker), list);
2889 }
2890 return list;
e04a16fb
AG
2891}
2892
c2952b01
APB
2893/* Helper functions to dump the parser context stack. */
2894
2895#define TAB_CONTEXT(C) \
2896 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2897
2898static void
2899java_debug_context_do (tab)
2900 int tab;
2901{
ee07f4f4
APB
2902 struct parser_ctxt *copy = ctxp;
2903 while (copy)
2904 {
c2952b01 2905 TAB_CONTEXT (tab);
ee07f4f4 2906 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2907 TAB_CONTEXT (tab);
ee07f4f4 2908 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2909 TAB_CONTEXT (tab);
2910 fprintf (stderr, "lineno: %d\n", copy->lineno);
2911 TAB_CONTEXT (tab);
ee07f4f4
APB
2912 fprintf (stderr, "package: %s\n",
2913 (copy->package ?
2914 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2915 TAB_CONTEXT (tab);
ee07f4f4 2916 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2917 TAB_CONTEXT (tab);
ee07f4f4
APB
2918 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2919 copy = copy->next;
2920 tab += 2;
2921 }
ee07f4f4
APB
2922}
2923
c2952b01
APB
2924/* Dump the stacked up parser contexts. Intended to be called from a
2925 debugger. */
2926
ee07f4f4
APB
2927void
2928java_debug_context ()
2929{
2930 java_debug_context_do (0);
2931}
2932
c2952b01
APB
2933\f
2934
2935/* Flag for the error report routine to issue the error the first time
2936 it's called (overriding the default behavior which is to drop the
2937 first invocation and honor the second one, taking advantage of a
2938 richer context. */
2939static int force_error = 0;
ee07f4f4 2940
8119c720
APB
2941/* Reporting an constructor invocation error. */
2942static void
2943parse_ctor_invocation_error ()
2944{
2945 if (DECL_CONSTRUCTOR_P (current_function_decl))
2946 yyerror ("Constructor invocation must be first thing in a constructor");
2947 else
2948 yyerror ("Only constructors can invoke constructors");
2949}
2950
2951/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2952
2953static tree
2954parse_jdk1_1_error (msg)
49f48c71 2955 const char *msg;
b67d701b
PB
2956{
2957 sorry (": `%s' JDK1.1(TM) feature", msg);
2958 java_error_count++;
9bbc7d9f 2959 return empty_stmt_node;
b67d701b
PB
2960}
2961
e04a16fb
AG
2962static int do_warning = 0;
2963
2964void
2965yyerror (msg)
49f48c71 2966 const char *msg;
e04a16fb
AG
2967{
2968 static java_lc elc;
2969 static int prev_lineno;
49f48c71 2970 static const char *prev_msg;
e04a16fb 2971
0a2138e2 2972 int save_lineno;
e04a16fb 2973 char *remainder, *code_from_source;
e04a16fb
AG
2974
2975 if (!force_error && prev_lineno == lineno)
2976 return;
2977
2978 /* Save current error location but report latter, when the context is
2979 richer. */
2980 if (ctxp->java_error_flag == 0)
2981 {
2982 ctxp->java_error_flag = 1;
2983 elc = ctxp->elc;
2984 /* Do something to use the previous line if we're reaching the
2985 end of the file... */
2986#ifdef VERBOSE_SKELETON
2987 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2988#endif
2989 return;
2990 }
2991
2992 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2993 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2994 return;
2995
2996 ctxp->java_error_flag = 0;
2997 if (do_warning)
2998 java_warning_count++;
2999 else
3000 java_error_count++;
3001
807bc1db 3002 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
3003 {
3004 elc.col = ctxp->p_line->char_col-1;
3005 elc.line = ctxp->p_line->lineno;
3006 }
3007
3008 save_lineno = lineno;
3009 prev_lineno = lineno = elc.line;
3010 prev_msg = msg;
3011
3012 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3013 obstack_grow0 (&temporary_obstack,
3014 code_from_source, strlen (code_from_source));
3015 remainder = obstack_finish (&temporary_obstack);
3016 if (do_warning)
3017 warning ("%s.\n%s", msg, remainder);
3018 else
3019 error ("%s.\n%s", msg, remainder);
3020
3021 /* This allow us to cheaply avoid an extra 'Invalid expression
3022 statement' error report when errors have been already reported on
3023 the same line. This occurs when we report an error but don't have
3024 a synchronization point other than ';', which
3025 expression_statement is the only one to take care of. */
3026 ctxp->prevent_ese = lineno = save_lineno;
3027}
3028
3029static void
15fdcfe9 3030issue_warning_error_from_context (cl, msg, ap)
5e942c50 3031 tree cl;
d4476be2 3032 const char *msg;
15fdcfe9 3033 va_list ap;
5e942c50 3034{
3b304f5b 3035 const char *saved, *saved_input_filename;
15fdcfe9
PB
3036 char buffer [4096];
3037 vsprintf (buffer, msg, ap);
3038 force_error = 1;
5e942c50
APB
3039
3040 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3041 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3042 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3043
3044 /* We have a CL, that's a good reason for using it if it contains data */
3045 saved = ctxp->filename;
3046 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3047 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3048 saved_input_filename = input_filename;
3049 input_filename = ctxp->filename;
15fdcfe9
PB
3050 java_error (NULL);
3051 java_error (buffer);
5e942c50 3052 ctxp->filename = saved;
1886c9d8 3053 input_filename = saved_input_filename;
15fdcfe9 3054 force_error = 0;
5e942c50
APB
3055}
3056
e04a16fb
AG
3057/* Issue an error message at a current source line CL */
3058
15fdcfe9 3059void
df32d2ce 3060parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3061{
d4476be2 3062#ifndef ANSI_PROTOTYPES
e04a16fb 3063 tree cl;
d4476be2 3064 const char *msg;
e04a16fb 3065#endif
e04a16fb
AG
3066 va_list ap;
3067
3068 VA_START (ap, msg);
d4476be2 3069#ifndef ANSI_PROTOTYPES
e04a16fb 3070 cl = va_arg (ap, tree);
d4476be2 3071 msg = va_arg (ap, const char *);
e04a16fb 3072#endif
15fdcfe9
PB
3073 issue_warning_error_from_context (cl, msg, ap);
3074 va_end (ap);
e04a16fb
AG
3075}
3076
3077/* Issue a warning at a current source line CL */
3078
3079static void
df32d2ce 3080parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3081{
d4476be2 3082#ifndef ANSI_PROTOTYPES
e04a16fb 3083 tree cl;
d4476be2 3084 const char *msg;
e04a16fb 3085#endif
e04a16fb
AG
3086 va_list ap;
3087
3088 VA_START (ap, msg);
d4476be2 3089#ifndef ANSI_PROTOTYPES
e04a16fb 3090 cl = va_arg (ap, tree);
d4476be2 3091 msg = va_arg (ap, const char *);
e04a16fb 3092#endif
e04a16fb 3093
c877974e 3094 force_error = do_warning = 1;
15fdcfe9 3095 issue_warning_error_from_context (cl, msg, ap);
c877974e 3096 do_warning = force_error = 0;
15fdcfe9 3097 va_end (ap);
e04a16fb
AG
3098}
3099
82371d41
APB
3100static tree
3101find_expr_with_wfl (node)
3102 tree node;
3103{
3104 while (node)
3105 {
3106 char code;
3107 tree to_return;
3108
3109 switch (TREE_CODE (node))
3110 {
3111 case BLOCK:
c0d87ff6
PB
3112 node = BLOCK_EXPR_BODY (node);
3113 continue;
82371d41
APB
3114
3115 case COMPOUND_EXPR:
3116 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3117 if (to_return)
3118 return to_return;
c0d87ff6
PB
3119 node = TREE_OPERAND (node, 1);
3120 continue;
82371d41
APB
3121
3122 case LOOP_EXPR:
c0d87ff6
PB
3123 node = TREE_OPERAND (node, 0);
3124 continue;
82371d41
APB
3125
3126 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3127 node = TREE_OPERAND (node, 1);
3128 continue;
3129
82371d41
APB
3130 default:
3131 code = TREE_CODE_CLASS (TREE_CODE (node));
3132 if (((code == '1') || (code == '2') || (code == 'e'))
3133 && EXPR_WFL_LINECOL (node))
3134 return node;
ba179f9f 3135 return NULL_TREE;
82371d41
APB
3136 }
3137 }
3138 return NULL_TREE;
3139}
3140
3141/* Issue a missing return statement error. Uses METHOD to figure the
3142 last line of the method the error occurs in. */
3143
3144static void
3145missing_return_error (method)
3146 tree method;
3147{
3148 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3149 parse_error_context (wfl_operator, "Missing return statement");
3150}
3151
3152/* Issue an unreachable statement error. From NODE, find the next
3153 statement to report appropriately. */
3154static void
3155unreachable_stmt_error (node)
3156 tree node;
3157{
3158 /* Browse node to find the next expression node that has a WFL. Use
3159 the location to report the error */
3160 if (TREE_CODE (node) == COMPOUND_EXPR)
3161 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3162 else
3163 node = find_expr_with_wfl (node);
3164
3165 if (node)
3166 {
3167 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3168 parse_error_context (wfl_operator, "Unreachable statement");
3169 }
3170 else
400500c4 3171 abort ();
82371d41
APB
3172}
3173
c877974e 3174int
e04a16fb
AG
3175java_report_errors ()
3176{
3177 if (java_error_count)
3178 fprintf (stderr, "%d error%s",
3179 java_error_count, (java_error_count == 1 ? "" : "s"));
3180 if (java_warning_count)
3181 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3182 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3183 if (java_error_count || java_warning_count)
3184 putc ('\n', stderr);
c877974e 3185 return java_error_count;
e04a16fb
AG
3186}
3187
3188static char *
3189java_accstring_lookup (flags)
3190 int flags;
3191{
3192 static char buffer [80];
3193#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3194
3195 /* Access modifier looked-up first for easier report on forbidden
3196 access. */
3197 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3198 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3199 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3200 if (flags & ACC_STATIC) COPY_RETURN ("static");
3201 if (flags & ACC_FINAL) COPY_RETURN ("final");
3202 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3203 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3204 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3205 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3206 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3207 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3208
3209 buffer [0] = '\0';
3210 return buffer;
3211#undef COPY_RETURN
3212}
3213
b67d701b
PB
3214/* Issuing error messages upon redefinition of classes, interfaces or
3215 variables. */
3216
e04a16fb 3217static void
b67d701b 3218classitf_redefinition_error (context, id, decl, cl)
49f48c71 3219 const char *context;
e04a16fb
AG
3220 tree id, decl, cl;
3221{
3222 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3223 context, IDENTIFIER_POINTER (id),
3224 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3225 /* Here we should point out where its redefined. It's a unicode. FIXME */
3226}
3227
b67d701b
PB
3228static void
3229variable_redefinition_error (context, name, type, line)
3230 tree context, name, type;
3231 int line;
3232{
49f48c71 3233 const char *type_name;
b67d701b
PB
3234
3235 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3236 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3237 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3238 else
0a2138e2 3239 type_name = lang_printable_name (type, 0);
b67d701b
PB
3240
3241 parse_error_context (context,
781b0558 3242 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3243 IDENTIFIER_POINTER (name),
3244 type_name, IDENTIFIER_POINTER (name), line);
3245}
3246
c583dd46
APB
3247static tree
3248build_array_from_name (type, type_wfl, name, ret_name)
3249 tree type, type_wfl, name, *ret_name;
3250{
3251 int more_dims = 0;
49f48c71 3252 const char *string;
c583dd46
APB
3253
3254 /* Eventually get more dims */
3255 string = IDENTIFIER_POINTER (name);
3256 while (string [more_dims] == '[')
3257 more_dims++;
3258
3259 /* If we have, then craft a new type for this variable */
3260 if (more_dims)
3261 {
c0d87ff6 3262 name = get_identifier (&string [more_dims]);
c583dd46 3263
34f4db93
APB
3264 /* If we have a pointer, use its type */
3265 if (TREE_CODE (type) == POINTER_TYPE)
3266 type = TREE_TYPE (type);
c583dd46
APB
3267
3268 /* Building the first dimension of a primitive type uses this
3269 function */
3270 if (JPRIMITIVE_TYPE_P (type))
3271 {
3272 type = build_java_array_type (type, -1);
22eed1e6 3273 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3274 more_dims--;
3275 }
3276 /* Otherwise, if we have a WFL for this type, use it (the type
3277 is already an array on an unresolved type, and we just keep
3278 on adding dimensions) */
3279 else if (type_wfl)
3280 type = type_wfl;
3281
3282 /* Add all the dimensions */
3283 while (more_dims--)
3284 type = build_unresolved_array_type (type);
3285
3286 /* The type may have been incomplete in the first place */
3287 if (type_wfl)
3288 type = obtain_incomplete_type (type);
3289 }
3290
c2952b01
APB
3291 if (ret_name)
3292 *ret_name = name;
c583dd46
APB
3293 return type;
3294}
3295
e04a16fb
AG
3296/* Build something that the type identifier resolver will identify as
3297 being an array to an unresolved type. TYPE_WFL is a WFL on a
3298 identifier. */
3299
3300static tree
3301build_unresolved_array_type (type_or_wfl)
3302 tree type_or_wfl;
3303{
49f48c71 3304 const char *ptr;
e04a16fb 3305
1886c9d8 3306 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3307 just create a array type */
3308 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3309 {
3310 tree type = build_java_array_type (type_or_wfl, -1);
3311 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3312 return type;
3313 }
3314
3315 obstack_1grow (&temporary_obstack, '[');
3316 obstack_grow0 (&temporary_obstack,
3317 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3318 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3319 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3320 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3321 return type_or_wfl;
e04a16fb
AG
3322}
3323
e04a16fb
AG
3324static void
3325parser_add_interface (class_decl, interface_decl, wfl)
3326 tree class_decl, interface_decl, wfl;
3327{
3328 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3329 parse_error_context (wfl, "Interface `%s' repeated",
3330 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3331}
3332
3333/* Bulk of common class/interface checks. Return 1 if an error was
3334 encountered. TAG is 0 for a class, 1 for an interface. */
3335
3336static int
3337check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3338 int is_interface, flags;
3339 tree raw_name, qualified_name, decl, cl;
3340{
3341 tree node;
c2952b01
APB
3342 int sca = 0; /* Static class allowed */
3343 int icaf = 0; /* Inner class allowed flags */
3344 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3345
3346 if (!quiet_flag)
c2952b01
APB
3347 fprintf (stderr, " %s%s %s",
3348 (CPC_INNER_P () ? "inner" : ""),
3349 (is_interface ? "interface" : "class"),
e04a16fb
AG
3350 IDENTIFIER_POINTER (qualified_name));
3351
3352 /* Scope of an interface/class type name:
3353 - Can't be imported by a single type import
3354 - Can't already exists in the package */
3355 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3356 && (node = find_name_in_single_imports (raw_name))
3357 && !CPC_INNER_P ())
e04a16fb
AG
3358 {
3359 parse_error_context
3360 (cl, "%s name `%s' clashes with imported type `%s'",
3361 (is_interface ? "Interface" : "Class"),
3362 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3363 return 1;
3364 }
3365 if (decl && CLASS_COMPLETE_P (decl))
3366 {
b67d701b
PB
3367 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3368 qualified_name, decl, cl);
e04a16fb
AG
3369 return 1;
3370 }
3371
c2952b01
APB
3372 if (check_inner_class_redefinition (raw_name, cl))
3373 return 1;
3374
3375 /* If public, file name should match class/interface name, except
3376 when dealing with an inner class */
3377 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3378 {
49f48c71 3379 const char *f;
e04a16fb
AG
3380
3381 /* Contains OS dependent assumption on path separator. FIXME */
3382 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3383 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3384 f--)
3385 ;
847fe791 3386 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3387 f++;
3388 if (strncmp (IDENTIFIER_POINTER (raw_name),
3389 f , IDENTIFIER_LENGTH (raw_name)) ||
3390 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3391 parse_error_context
3392 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3393 (is_interface ? "interface" : "class"),
3394 IDENTIFIER_POINTER (qualified_name),
3395 IDENTIFIER_POINTER (raw_name));
3396 }
3397
c2952b01
APB
3398 /* Static classes can be declared only in top level classes. Note:
3399 once static, a inner class is a top level class. */
3400 if (flags & ACC_STATIC)
3401 {
3402 /* Catch the specific error of declaring an class inner class
3403 with no toplevel enclosing class. Prevent check_modifiers from
3404 complaining a second time */
3405 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3406 {
3407 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3408 IDENTIFIER_POINTER (qualified_name));
3409 sca = ACC_STATIC;
3410 }
3411 /* Else, in the context of a top-level class declaration, let
3412 `check_modifiers' do its job, otherwise, give it a go */
3413 else
3414 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3415 }
3416
a40d21da 3417 /* Inner classes can be declared private or protected
c2952b01
APB
3418 within their enclosing classes. */
3419 if (CPC_INNER_P ())
3420 {
3421 /* A class which is local to a block can't be public, private,
3422 protected or static. But it is created final, so allow this
3423 one. */
3424 if (current_function_decl)
3425 icaf = sca = uaaf = ACC_FINAL;
3426 else
3427 {
3428 check_modifiers_consistency (flags);
3429 icaf = ACC_PRIVATE|ACC_PROTECTED;
3430 }
3431 }
3432
a40d21da
APB
3433 if (is_interface)
3434 {
3435 if (CPC_INNER_P ())
3436 uaaf = INTERFACE_INNER_MODIFIERS;
3437 else
3438 uaaf = INTERFACE_MODIFIERS;
3439
3440 check_modifiers ("Illegal modifier `%s' for interface declaration",
3441 flags, uaaf);
3442 }
2884c41e 3443 else
a40d21da
APB
3444 check_modifiers ((current_function_decl ?
3445 "Illegal modifier `%s' for local class declaration" :
3446 "Illegal modifier `%s' for class declaration"),
c2952b01 3447 flags, uaaf|sca|icaf);
e04a16fb
AG
3448 return 0;
3449}
3450
c2952b01
APB
3451static void
3452make_nested_class_name (cpc_list)
3453 tree cpc_list;
3454{
3455 tree name;
3456
3457 if (!cpc_list)
3458 return;
3459 else
3460 make_nested_class_name (TREE_CHAIN (cpc_list));
3461
3462 /* Pick the qualified name when dealing with the first upmost
3463 enclosing class */
3464 name = (TREE_CHAIN (cpc_list) ?
3465 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3466 obstack_grow (&temporary_obstack,
3467 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3468 /* Why is NO_DOLLAR_IN_LABEL defined? */
3469#if 0
3470#ifdef NO_DOLLAR_IN_LABEL
400500c4 3471 internal_error ("Can't use '$' as a separator for inner classes");
c2952b01
APB
3472#endif
3473#endif
3474 obstack_1grow (&temporary_obstack, '$');
3475}
3476
3477/* Can't redefine a class already defined in an earlier scope. */
3478
3479static int
3480check_inner_class_redefinition (raw_name, cl)
3481 tree raw_name, cl;
3482{
3483 tree scope_list;
3484
3485 for (scope_list = GET_CPC_LIST (); scope_list;
3486 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3487 if (raw_name == GET_CPC_UN_NODE (scope_list))
3488 {
3489 parse_error_context
3490 (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",
3491 IDENTIFIER_POINTER (raw_name));
3492 return 1;
3493 }
3494 return 0;
3495}
3496
3497static tree
3498find_as_inner_class (enclosing, name, cl)
3499 tree enclosing, name, cl;
3500{
3501 tree qual, to_return;
3502 if (!enclosing)
3503 return NULL_TREE;
3504
3505 name = TYPE_NAME (name);
3506
3507 /* First search: within the scope of `enclosing', search for name */
3508 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3509 qual = EXPR_WFL_QUALIFICATION (cl);
3510 else if (cl)
3511 qual = build_tree_list (cl, NULL_TREE);
3512 else
3513 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3514
3515 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3516 return to_return;
3517
3518 /* We're dealing with a qualified name. Try to resolve thing until
3519 we get something that is an enclosing class. */
3520 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3521 {
3522 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3523
0c2b8145
APB
3524 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3525 qual = TREE_CHAIN (qual))
c2952b01
APB
3526 {
3527 acc = merge_qualified_name (acc,
3528 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3529 BUILD_PTR_FROM_NAME (ptr, acc);
3530 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3531 }
3532
3533 /* A NULL qual and a decl means that the search ended
3534 successfully?!? We have to do something then. FIXME */
3535
3536 if (decl)
3537 enclosing = decl;
3538 else
3539 qual = EXPR_WFL_QUALIFICATION (cl);
3540 }
3541 /* Otherwise, create a qual for the other part of the resolution. */
3542 else
3543 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3544
1e12ab9b 3545 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3546}
3547
3548/* We go inside the list of sub classes and try to find a way
3549 through. */
3550
3551static tree
3552find_as_inner_class_do (qual, enclosing)
3553 tree qual, enclosing;
3554{
3555 if (!qual)
3556 return NULL_TREE;
3557
3558 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3559 {
3560 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3561 tree next_enclosing = NULL_TREE;
3562 tree inner_list;
3563
3564 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3565 inner_list; inner_list = TREE_CHAIN (inner_list))
3566 {
3567 if (TREE_VALUE (inner_list) == name_to_match)
3568 {
3569 next_enclosing = TREE_PURPOSE (inner_list);
3570 break;
3571 }
3572 }
3573 enclosing = next_enclosing;
3574 }
3575
3576 return (!qual && enclosing ? enclosing : NULL_TREE);
3577}
3578
3579/* Reach all inner classes and tie their unqualified name to a
3580 DECL. */
3581
3582static void
3583set_nested_class_simple_name_value (outer, set)
3584 tree outer;
3585 int set;
3586{
3587 tree l;
3588
3589 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3590 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3591 TREE_PURPOSE (l) : NULL_TREE);
3592}
3593
3594static void
3595link_nested_class_to_enclosing ()
3596{
3597 if (GET_ENCLOSING_CPC ())
3598 {
3599 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3600 DECL_INNER_CLASS_LIST (enclosing) =
3601 tree_cons (GET_CPC (), GET_CPC_UN (),
3602 DECL_INNER_CLASS_LIST (enclosing));
3603 enclosing = enclosing;
3604 }
3605}
3606
3607static tree
3608maybe_make_nested_class_name (name)
3609 tree name;
3610{
3611 tree id = NULL_TREE;
3612
3613 if (CPC_INNER_P ())
3614 {
3615 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3616 obstack_grow0 (&temporary_obstack,
3617 IDENTIFIER_POINTER (name),
3618 IDENTIFIER_LENGTH (name));
c2952b01
APB
3619 id = get_identifier (obstack_finish (&temporary_obstack));
3620 if (ctxp->package)
3621 QUALIFIED_P (id) = 1;
3622 }
3623 return id;
3624}
3625
3626/* If DECL is NULL, create and push a new DECL, record the current
3627 line CL and do other maintenance things. */
3628
e04a16fb 3629static tree
c2952b01
APB
3630maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3631 tree decl, raw_name, qualified_name, cl;
e04a16fb 3632{
5e942c50 3633 if (!decl)
e04a16fb 3634 decl = push_class (make_class (), qualified_name);
c2952b01 3635
e04a16fb
AG
3636 /* Take care of the file and line business */
3637 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3638 /* If we're emiting xrefs, store the line/col number information */
3639 if (flag_emit_xref)
3640 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3641 else
3642 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3643 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3644 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3645 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3646
c2952b01
APB
3647 PUSH_CPC (decl, raw_name);
3648 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3649
e04a16fb
AG
3650 /* Link the declaration to the already seen ones */
3651 TREE_CHAIN (decl) = ctxp->class_list;
3652 ctxp->class_list = decl;
5e942c50 3653
23a79c61 3654 /* Create a new nodes in the global lists */
5e942c50 3655 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3656 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3657
e04a16fb
AG
3658 /* Install a new dependency list element */
3659 create_jdep_list (ctxp);
3660
3661 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3662 IDENTIFIER_POINTER (qualified_name)));
3663 return decl;
3664}
3665
3666static void
3667add_superinterfaces (decl, interface_list)
3668 tree decl, interface_list;
3669{
3670 tree node;
3671 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3672 takes care of ensuring that:
3673 - This is an accessible interface type,
3674 - Circularity detection.
3675 parser_add_interface is then called. If present but not defined,
3676 the check operation is delayed until the super interface gets
3677 defined. */
3678 for (node = interface_list; node; node = TREE_CHAIN (node))
3679 {
15fdcfe9 3680 tree current = TREE_PURPOSE (node);
5e942c50
APB
3681 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3682 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3683 {
5e942c50
APB
3684 if (!parser_check_super_interface (idecl, decl, current))
3685 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3686 }
3687 else
3688 register_incomplete_type (JDEP_INTERFACE,
3689 current, decl, NULL_TREE);
3690 }
3691}
3692
3693/* Create an interface in pass1 and return its decl. Return the
3694 interface's decl in pass 2. */
3695
3696static tree
3697create_interface (flags, id, super)
3698 int flags;
3699 tree id, super;
3700{
e04a16fb 3701 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3702 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3703 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3704
3705 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3706
3707 /* Basic checks: scope, redefinition, modifiers */
3708 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3709 {
3710 PUSH_ERROR ();
3711 return NULL_TREE;
3712 }
3713
3714 /* Suspend the current parsing context if we're parsing an inner
3715 interface */
3716 if (CPC_INNER_P ())
3717 java_parser_context_suspend ();
3718
3719 /* Push a new context for (static) initialized upon declaration fields */
3720 java_parser_context_push_initialized_field ();
e04a16fb
AG
3721
3722 /* Interface modifiers check
3723 - public/abstract allowed (already done at that point)
3724 - abstract is obsolete (comes first, it's a warning, or should be)
3725 - Can't use twice the same (checked in the modifier rule) */
c877974e 3726 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3727 parse_warning_context
3728 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3729 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3730
3731 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3732 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3733
3734 /* Set super info and mark the class a complete */
2aa11e97 3735 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3736 object_type_node, ctxp->interface_number);
3737 ctxp->interface_number = 0;
3738 CLASS_COMPLETE_P (decl) = 1;
3739 add_superinterfaces (decl, super);
3740
3741 return decl;
3742}
3743
c2952b01
APB
3744/* Anonymous class counter. Will be reset to 1 every time a non
3745 anonymous class gets created. */
3746static int anonymous_class_counter = 1;
3747
3748/* Patch anonymous class CLASS, by either extending or implementing
3749 DEP. */
3750
3751static void
3752patch_anonymous_class (type_decl, class_decl, wfl)
3753 tree type_decl, class_decl, wfl;
3754{
3755 tree class = TREE_TYPE (class_decl);
3756 tree type = TREE_TYPE (type_decl);
3757 tree binfo = TYPE_BINFO (class);
3758
3759 /* If it's an interface, implement it */
3760 if (CLASS_INTERFACE (type_decl))
3761 {
3762 tree s_binfo;
3763 int length;
3764
3765 if (parser_check_super_interface (type_decl, class_decl, wfl))
3766 return;
3767
3768 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3769 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3770 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3771 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3772 /* And add the interface */
3773 parser_add_interface (class_decl, type_decl, wfl);
3774 }
3775 /* Otherwise, it's a type we want to extend */
3776 else
3777 {
3778 if (parser_check_super (type_decl, class_decl, wfl))
3779 return;
3780 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3781 }
3782}
3783
3784static tree
3785create_anonymous_class (location, type_name)
3786 int location;
3787 tree type_name;
3788{
3789 char buffer [80];
3790 tree super = NULL_TREE, itf = NULL_TREE;
3791 tree id, type_decl, class;
3792
3793 /* The unqualified name of the anonymous class. It's just a number. */
3794 sprintf (buffer, "%d", anonymous_class_counter++);
3795 id = build_wfl_node (get_identifier (buffer));
3796 EXPR_WFL_LINECOL (id) = location;
3797
3798 /* We know about the type to extend/implement. We go ahead */
3799 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3800 {
3801 /* Create a class which either implements on extends the designated
3802 class. The class bears an innacessible name. */
3803 if (CLASS_INTERFACE (type_decl))
3804 {
3805 /* It's OK to modify it here. It's been already used and
3806 shouldn't be reused */
3807 ctxp->interface_number = 1;
3808 /* Interfaces should presented as a list of WFLs */
3809 itf = build_tree_list (type_name, NULL_TREE);
3810 }
3811 else
3812 super = type_name;
3813 }
3814
3815 class = create_class (ACC_FINAL, id, super, itf);
3816
3817 /* We didn't know anything about the stuff. We register a dependence. */
3818 if (!type_decl)
3819 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3820
3821 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3822 return class;
3823}
3824
a40d21da 3825/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3826 interface's decl in pass 2. */
3827
3828static tree
3829create_class (flags, id, super, interfaces)
3830 int flags;
3831 tree id, super, interfaces;
3832{
e04a16fb
AG
3833 tree raw_name = EXPR_WFL_NODE (id);
3834 tree class_id, decl;
9ee9b555 3835 tree super_decl_type;
e04a16fb 3836
98a52c2c 3837 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3838 decl = IDENTIFIER_CLASS_VALUE (class_id);
3839 EXPR_WFL_NODE (id) = class_id;
3840
3841 /* Basic check: scope, redefinition, modifiers */
3842 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3843 {
3844 PUSH_ERROR ();
3845 return NULL_TREE;
3846 }
3847
3848 /* Suspend the current parsing context if we're parsing an inner
3849 class or an anonymous class. */
3850 if (CPC_INNER_P ())
3851 java_parser_context_suspend ();
3852 /* Push a new context for (static) initialized upon declaration fields */
3853 java_parser_context_push_initialized_field ();
e04a16fb
AG
3854
3855 /* Class modifier check:
3856 - Allowed modifier (already done at that point)
3857 - abstract AND final forbidden
3858 - Public classes defined in the correct file */
3859 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3860 parse_error_context
3861 (id, "Class `%s' can't be declared both abstract and final",
3862 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3863
3864 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3865 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3866
3867 /* If SUPER exists, use it, otherwise use Object */
3868 if (super)
3869 {
3870 /* Can't extend java.lang.Object */
3871 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3872 {
3873 parse_error_context (id, "Can't extend `java.lang.Object'");
3874 return NULL_TREE;
3875 }
3876
2c3199bc
PB
3877 super_decl_type =
3878 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3879 }
3880 else if (TREE_TYPE (decl) != object_type_node)
3881 super_decl_type = object_type_node;
3882 /* We're defining java.lang.Object */
3883 else
3884 super_decl_type = NULL_TREE;
3885
6d003d5c
BM
3886 /* A class nested in an interface is implicitly static. */
3887 if (INNER_CLASS_DECL_P (decl)
3888 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3889 {
3890 flags |= ACC_STATIC;
3891 }
3892
3893 /* Set super info and mark the class as complete. */
e04a16fb
AG
3894 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3895 ctxp->interface_number);
3896 ctxp->interface_number = 0;
3897 CLASS_COMPLETE_P (decl) = 1;
3898 add_superinterfaces (decl, interfaces);
3899
c2952b01
APB
3900 /* Add the private this$<n> field, Replicate final locals still in
3901 scope as private final fields mangled like val$<local_name>.
3902 This doesn't not occur for top level (static) inner classes. */
3903 if (PURE_INNER_CLASS_DECL_P (decl))
3904 add_inner_class_fields (decl, current_function_decl);
3905
7f10c2e2
APB
3906 /* If doing xref, store the location at which the inherited class
3907 (if any) was seen. */
3908 if (flag_emit_xref && super)
3909 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3910
5e942c50
APB
3911 /* Eventually sets the @deprecated tag flag */
3912 CHECK_DEPRECATED (decl);
3913
165f37bc
APB
3914 /* Reset the anonymous class counter when declaring non inner classes */
3915 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3916 anonymous_class_counter = 1;
3917
e04a16fb
AG
3918 return decl;
3919}
3920
c2952b01 3921/* End a class declaration: register the statements used to create
c00f0fb2 3922 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3923 parser context if necessary. */
3924
3925static void
3926end_class_declaration (resume)
3927 int resume;
3928{
3929 /* If an error occured, context weren't pushed and won't need to be
3930 popped by a resume. */
3931 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3932
3933 java_parser_context_pop_initialized_field ();
3934 POP_CPC ();
3935 if (resume && no_error_occured)
3936 java_parser_context_resume ();
93220702
APB
3937
3938 /* We're ending a class declaration, this is a good time to reset
3939 the interface cout. Note that might have been already done in
3940 create_interface, but if at that time an inner class was being
3941 dealt with, the interface count was reset in a context created
3942 for the sake of handling inner classes declaration. */
3943 ctxp->interface_number = 0;
c2952b01
APB
3944}
3945
3946static void
3947add_inner_class_fields (class_decl, fct_decl)
3948 tree class_decl;
3949 tree fct_decl;
3950{
3951 tree block, marker, f;
3952
3953 f = add_field (TREE_TYPE (class_decl),
3954 build_current_thisn (TREE_TYPE (class_decl)),
3955 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3956 ACC_PRIVATE);
3957 FIELD_THISN (f) = 1;
3958
3959 if (!fct_decl)
3960 return;
3961
3962 for (block = GET_CURRENT_BLOCK (fct_decl);
3963 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3964 {
3965 tree decl;
3966 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3967 {
1f8f4a0b 3968 tree name, pname;
c2952b01
APB
3969 tree wfl, init, list;
3970
3971 /* Avoid non final arguments. */
c7303e41 3972 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3973 continue;
3974
3975 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3976 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3977 wfl = build_wfl_node (name);
3978 init = build_wfl_node (pname);
c2952b01 3979 /* Build an initialization for the field: it will be
c00f0fb2 3980 initialized by a parameter added to finit$, bearing a
c2952b01 3981 mangled name of the field itself (param$<n>.) The
c00f0fb2 3982 parameter is provided to finit$ by the constructor
c2952b01
APB
3983 invoking it (hence the constructor will also feature a
3984 hidden parameter, set to the value of the outer context
3985 local at the time the inner class is created.)
3986
3987 Note: we take into account all possible locals that can
3988 be accessed by the inner class. It's actually not trivial
3989 to minimize these aliases down to the ones really
3990 used. One way to do that would be to expand all regular
c00f0fb2 3991 methods first, then finit$ to get a picture of what's
c2952b01
APB
3992 used. It works with the exception that we would have to
3993 go back on all constructor invoked in regular methods to
3994 have their invokation reworked (to include the right amount
3995 of alias initializer parameters.)
3996
3997 The only real way around, I think, is a first pass to
3998 identify locals really used in the inner class. We leave
3999 the flag FIELD_LOCAL_ALIAS_USED around for that future
4000 use.
4001
4002 On the other hand, it only affect local inner classes,
c00f0fb2 4003 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4004 unecessary arguments. It's easy for a developper to keep
4005 this number of parameter down by using the `final'
4006 keyword only when necessary. For the time being, we can
4007 issue a warning on unecessary finals. FIXME */
4008 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4009 wfl, init);
4010
4011 /* Register the field. The TREE_LIST holding the part
4012 initialized/initializer will be marked ARG_FINAL_P so
4013 that the created field can be marked
4014 FIELD_LOCAL_ALIAS. */
4015 list = build_tree_list (wfl, init);
4016 ARG_FINAL_P (list) = 1;
4017 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4018 }
4019 }
4020
4021 if (!CPC_INITIALIZER_STMT (ctxp))
4022 return;
4023
4024 /* If we ever registered an alias field, insert and marker to
4025 remeber where the list ends. The second part of the list (the one
4026 featuring initialized fields) so it can be later reversed to
4027 enforce 8.5. The marker will be removed during that operation. */
4028 marker = build_tree_list (NULL_TREE, NULL_TREE);
4029 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4030 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4031}
4032
e04a16fb
AG
4033/* Can't use lookup_field () since we don't want to load the class and
4034 can't set the CLASS_LOADED_P flag */
4035
4036static tree
4037find_field (class, name)
4038 tree class;
4039 tree name;
4040{
4041 tree decl;
4042 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4043 {
4044 if (DECL_NAME (decl) == name)
4045 return decl;
4046 }
4047 return NULL_TREE;
4048}
4049
4050/* Wrap around lookup_field that doesn't potentially upset the value
4051 of CLASS */
4052
4053static tree
4054lookup_field_wrapper (class, name)
4055 tree class, name;
4056{
4057 tree type = class;
9a7ab4b3 4058 tree decl = NULL_TREE;
c877974e 4059 java_parser_context_save_global ();
f2760b27
APB
4060
4061 /* Last chance: if we're within the context of an inner class, we
4062 might be trying to access a local variable defined in an outer
4063 context. We try to look for it now. */
9a7ab4b3 4064 if (INNER_CLASS_TYPE_P (class))
f2760b27 4065 {
9a7ab4b3 4066 tree new_name;
1f8f4a0b 4067 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4068 decl = lookup_field (&type, new_name);
f2760b27
APB
4069 if (decl && decl != error_mark_node)
4070 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4071 }
9a7ab4b3
APB
4072 if (!decl || decl == error_mark_node)
4073 {
4074 type = class;
4075 decl = lookup_field (&type, name);
4076 }
f2760b27 4077
c877974e 4078 java_parser_context_restore_global ();
93024893 4079 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4080}
4081
4082/* Find duplicate field within the same class declarations and report
c583dd46
APB
4083 the error. Returns 1 if a duplicated field was found, 0
4084 otherwise. */
e04a16fb
AG
4085
4086static int
c583dd46 4087duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4088 tree new_field_name, new_type, cl;
e04a16fb
AG
4089{
4090 /* This might be modified to work with method decl as well */
c2952b01 4091 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4092 if (decl)
4093 {
c2e3db92 4094 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4095 ((TREE_CODE (new_type) == POINTER_TYPE
4096 && TREE_TYPE (new_type) == NULL_TREE) ?
4097 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4098 lang_printable_name (new_type, 1)));
c877974e
APB
4099 /* The type may not have been completed by the time we report
4100 the error */
c2e3db92 4101 char *t2 = xstrdup (purify_type_name
4a5f66c3 4102 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4103 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4104 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4105 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4106 parse_error_context
4107 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4108 t1, IDENTIFIER_POINTER (new_field_name),
4109 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4110 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4111 free (t1);
4112 free (t2);
c583dd46 4113 return 1;
e04a16fb 4114 }
c583dd46 4115 return 0;
e04a16fb
AG
4116}
4117
4118/* Field registration routine. If TYPE doesn't exist, field
4119 declarations are linked to the undefined TYPE dependency list, to
4120 be later resolved in java_complete_class () */
4121
4122static void
4123register_fields (flags, type, variable_list)
4124 int flags;
4125 tree type, variable_list;
4126{
c583dd46 4127 tree current, saved_type;
c2952b01 4128 tree class_type = NULL_TREE;
e04a16fb
AG
4129 int saved_lineno = lineno;
4130 int must_chain = 0;
4131 tree wfl = NULL_TREE;
4132
c2952b01
APB
4133 if (GET_CPC ())
4134 class_type = TREE_TYPE (GET_CPC ());
4135
4136 if (!class_type || class_type == error_mark_node)
4137 return;
4138
e04a16fb
AG
4139 /* If we're adding fields to interfaces, those fields are public,
4140 static, final */
4141 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4142 {
4143 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4144 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4145 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4146 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4147 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4148 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4149 check_modifiers ("Illegal interface member modifier `%s'", flags,
4150 INTERFACE_FIELD_MODIFIERS);
4151 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4152 }
4153
c583dd46
APB
4154 /* Obtain a suitable type for resolution, if necessary */
4155 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4156
4157 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4158 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4159
c583dd46
APB
4160 for (current = variable_list, saved_type = type; current;
4161 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4162 {
c877974e 4163 tree real_type;
c583dd46 4164 tree field_decl;
e04a16fb
AG
4165 tree cl = TREE_PURPOSE (current);
4166 tree init = TREE_VALUE (current);
4167 tree current_name = EXPR_WFL_NODE (cl);
4168
cf1748bf 4169 /* Can't declare non-final static fields in inner classes */
c2952b01 4170 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4171 && !(flags & ACC_FINAL))
c2952b01 4172 parse_error_context
cf1748bf 4173 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4174 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4175 lang_printable_name (class_type, 0));
4176
c583dd46
APB
4177 /* Process NAME, as it may specify extra dimension(s) for it */
4178 type = build_array_from_name (type, wfl, current_name, &current_name);
4179
c583dd46
APB
4180 /* Type adjustment. We may have just readjusted TYPE because
4181 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4182 a reference if we can and don't have one already. Also
4183 change the name if we have an init. */
4184 if (type != saved_type)
4185 {
1886c9d8 4186 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4187 if (init)
4188 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4189 }
e04a16fb 4190
c877974e
APB
4191 real_type = GET_REAL_TYPE (type);
4192 /* Check for redeclarations */
4193 if (duplicate_declaration_error_p (current_name, real_type, cl))
4194 continue;
4195
c583dd46 4196 /* Set lineno to the line the field was found and create a
5e942c50 4197 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4198 if (flag_emit_xref)
4199 lineno = EXPR_WFL_LINECOL (cl);
4200 else
4201 lineno = EXPR_WFL_LINENO (cl);
c877974e 4202 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4203 CHECK_DEPRECATED (field_decl);
c2952b01 4204
c7303e41
APB
4205 /* If the field denotes a final instance variable, then we
4206 allocate a LANG_DECL_SPECIFIC part to keep track of its
4207 initialization. We also mark whether the field was
4208 initialized upon it's declaration. We don't do that if the
4209 created field is an alias to a final local. */
4210 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4211 {
4212 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4213 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4214 if ((flags & ACC_STATIC) && init)
4215 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4216 }
4217
4218 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4219 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4220 hide parameters to this inner class finit$ and
4221 constructors. It also means that the field isn't final per
4222 say. */
c2952b01 4223 if (ARG_FINAL_P (current))
c7303e41
APB
4224 {
4225 FIELD_LOCAL_ALIAS (field_decl) = 1;
4226 FIELD_FINAL (field_decl) = 0;
4227 }
c583dd46
APB
4228
4229 /* Check if we must chain. */
4230 if (must_chain)
4231 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4232
c583dd46
APB
4233 /* If we have an initialization value tied to the field */
4234 if (init)
4235 {
4236 /* The field is declared static */
e04a16fb 4237 if (flags & ACC_STATIC)
e04a16fb 4238 {
7525cc04
APB
4239 /* We include the field and its initialization part into
4240 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4241 walked, field initializations will be processed and
4242 fields initialized with known constants will be taken
4243 out of <clinit> and have their DECL_INITIAL set
7525cc04 4244 appropriately. */
c2952b01
APB
4245 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4246 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4247 if (TREE_OPERAND (init, 1)
4248 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4249 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4250 }
5e942c50
APB
4251 /* A non-static field declared with an immediate initialization is
4252 to be initialized in <init>, if any. This field is remembered
4253 to be processed at the time of the generation of <init>. */
c583dd46
APB
4254 else
4255 {
c2952b01
APB
4256 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4257 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4258 }
5b09b33e 4259 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4260 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4261 }
4262 }
4263 lineno = saved_lineno;
4264}
4265
c00f0fb2
APB
4266/* Generate finit$, using the list of initialized fields to populate
4267 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4268 one(s) used to initialized the field(s) caching outer context
4269 local(s). */
22eed1e6 4270
c2952b01
APB
4271static tree
4272generate_finit (class_type)
4273 tree class_type;
22eed1e6 4274{
c2952b01
APB
4275 int count = 0;
4276 tree list = TYPE_FINIT_STMT_LIST (class_type);
4277 tree mdecl, current, parms;
4278
4279 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4280 class_type, NULL_TREE,
4281 &count);
4282 CRAFTED_PARAM_LIST_FIXUP (parms);
4283 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4284 finit_identifier_node, parms);
4285 fix_method_argument_names (parms, mdecl);
4286 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4287 mdecl, NULL_TREE);
4288 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4289 start_artificial_method_body (mdecl);
4290
c2952b01 4291 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4292 java_method_add_stmt (mdecl,
4293 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4294 current));
22eed1e6 4295 end_artificial_method_body (mdecl);
c2952b01 4296 return mdecl;
22eed1e6
APB
4297}
4298
e04a16fb 4299static void
c2952b01
APB
4300add_instance_initializer (mdecl)
4301 tree mdecl;
e04a16fb 4302{
c2952b01
APB
4303 tree current;
4304 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4305 tree compound = NULL_TREE;
e04a16fb 4306
c2952b01 4307 if (stmt_list)
e04a16fb 4308 {
c2952b01
APB
4309 for (current = stmt_list; current; current = TREE_CHAIN (current))
4310 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4311
c2952b01
APB
4312 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4313 NULL_TREE, compound));
4314 }
e04a16fb
AG
4315}
4316
4317/* Shared accros method_declarator and method_header to remember the
4318 patch stage that was reached during the declaration of the method.
4319 A method DECL is built differently is there is no patch
4320 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4321 pending on the currently defined method. */
4322
4323static int patch_stage;
4324
4325/* Check the method declaration and add the method to its current
4326 class. If the argument list is known to contain incomplete types,
4327 the method is partially added and the registration will be resume
22eed1e6
APB
4328 once the method arguments resolved. If TYPE is NULL, we're dealing
4329 with a constructor. */
e04a16fb
AG
4330
4331static tree
4332method_header (flags, type, mdecl, throws)
4333 int flags;
4334 tree type, mdecl, throws;
4335{
1886c9d8 4336 tree type_wfl = NULL_TREE;
79d13333 4337 tree meth_name = NULL_TREE;
c2952b01 4338 tree current, orig_arg, this_class = NULL;
34d4df06 4339 tree id, meth;
e04a16fb 4340 int saved_lineno;
1886c9d8 4341 int constructor_ok = 0, must_chain;
c2952b01 4342 int count;
34d4df06
APB
4343
4344 if (mdecl == error_mark_node)
4345 return error_mark_node;
4346 meth = TREE_VALUE (mdecl);
4347 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4348
4349 check_modifiers_consistency (flags);
79d13333 4350
c2952b01
APB
4351 if (GET_CPC ())
4352 this_class = TREE_TYPE (GET_CPC ());
4353
4354 if (!this_class || this_class == error_mark_node)
79d13333 4355 return NULL_TREE;
e04a16fb
AG
4356
4357 /* There are some forbidden modifiers for an abstract method and its
4358 class must be abstract as well. */
22eed1e6 4359 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4360 {
4361 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4362 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4363 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4364 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4365 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4366 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4367 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4368 parse_error_context
781b0558 4369 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4370 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4371 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4372 }
c2952b01 4373
22eed1e6
APB
4374 /* Things to be checked when declaring a constructor */
4375 if (!type)
4376 {
4377 int ec = java_error_count;
4378 /* 8.6: Constructor declarations: we might be trying to define a
4379 method without specifying a return type. */
c2952b01 4380 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4381 parse_error_context
4382 (id, "Invalid method declaration, return type required");
4383 /* 8.6.3: Constructor modifiers */
4384 else
4385 {
4386 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4387 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4388 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4389 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4390 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4391 }
4392 /* If we found error here, we don't consider it's OK to tread
4393 the method definition as a constructor, for the rest of this
4394 function */
4395 if (ec == java_error_count)
4396 constructor_ok = 1;
4397 }
e04a16fb
AG
4398
4399 /* Method declared within the scope of an interface are implicitly
4400 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4401 modifiers are checked right after. */
e04a16fb
AG
4402
4403 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4404 {
4405 /* If FLAGS isn't set because of a modifier, turn the
4406 corresponding modifier WFL to NULL so we issue a warning on
4407 the obsolete use of the modifier */
4408 if (!(flags & ACC_PUBLIC))
4409 MODIFIER_WFL (PUBLIC_TK) = NULL;
4410 if (!(flags & ACC_ABSTRACT))
4411 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4412 flags |= ACC_PUBLIC;
4413 flags |= ACC_ABSTRACT;
4414 }
4415
c2952b01
APB
4416 /* Inner class can't declare static methods */
4417 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4418 {
4419 parse_error_context
4420 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4421 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4422 lang_printable_name (this_class, 0));
4423 }
4424
e04a16fb
AG
4425 /* Modifiers context reset moved up, so abstract method declaration
4426 modifiers can be later checked. */
4427
22eed1e6
APB
4428 /* Set constructor returned type to void and method name to <init>,
4429 unless we found an error identifier the constructor (in which
4430 case we retain the original name) */
4431 if (!type)
4432 {
4433 type = void_type_node;
4434 if (constructor_ok)
4435 meth_name = init_identifier_node;
4436 }
4437 else
4438 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4439
1886c9d8
APB
4440 /* Do the returned type resolution and registration if necessary */
4441 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4442
4a5f66c3
APB
4443 if (meth_name)
4444 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4445 EXPR_WFL_NODE (id) = meth_name;
4446 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4447
4448 if (must_chain)
e04a16fb 4449 {
1886c9d8
APB
4450 patch_stage = JDEP_METHOD_RETURN;
4451 register_incomplete_type (patch_stage, type_wfl, id, type);
4452 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4453 }
4454 else
1886c9d8 4455 TREE_TYPE (meth) = type;
e04a16fb
AG
4456
4457 saved_lineno = lineno;
4458 /* When defining an abstract or interface method, the curly
4459 bracket at level 1 doesn't exist because there is no function
4460 body */
4461 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4462 EXPR_WFL_LINENO (id));
4463
5e942c50
APB
4464 /* Remember the original argument list */
4465 orig_arg = TYPE_ARG_TYPES (meth);
4466
e04a16fb
AG
4467 if (patch_stage) /* includes ret type and/or all args */
4468 {
4469 jdep *jdep;
4470 meth = add_method_1 (this_class, flags, meth_name, meth);
4471 /* Patch for the return type */
4472 if (patch_stage == JDEP_METHOD_RETURN)
4473 {
4474 jdep = CLASSD_LAST (ctxp->classd_list);
4475 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4476 }
4477 /* This is the stop JDEP. METH allows the function's signature
4478 to be computed. */
4479 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4480 }
4481 else
5e942c50
APB
4482 meth = add_method (this_class, flags, meth_name,
4483 build_java_signature (meth));
4484
c2952b01
APB
4485 /* Remember final parameters */
4486 MARK_FINAL_PARMS (meth, orig_arg);
4487
5e942c50
APB
4488 /* Fix the method argument list so we have the argument name
4489 information */
4490 fix_method_argument_names (orig_arg, meth);
4491
4492 /* Register the parameter number and re-install the current line
4493 number */
e04a16fb
AG
4494 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4495 lineno = saved_lineno;
b9f7e36c
APB
4496
4497 /* Register exception specified by the `throws' keyword for
4498 resolution and set the method decl appropriate field to the list.
4499 Note: the grammar ensures that what we get here are class
4500 types. */
4501 if (throws)
4502 {
4503 throws = nreverse (throws);
4504 for (current = throws; current; current = TREE_CHAIN (current))
4505 {
4506 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4507 NULL_TREE, NULL_TREE);
4508 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4509 &TREE_VALUE (current);
4510 }
4511 DECL_FUNCTION_THROWS (meth) = throws;
4512 }
4513
c4faeb92
APB
4514 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4515 DECL_FUNCTION_WFL (meth) = id;
4516
22eed1e6
APB
4517 /* Set the flag if we correctly processed a constructor */
4518 if (constructor_ok)
c2952b01
APB
4519 {
4520 DECL_CONSTRUCTOR_P (meth) = 1;
4521 /* Compute and store the number of artificial parameters declared
4522 for this constructor */
4523 for (count = 0, current = TYPE_FIELDS (this_class); current;
4524 current = TREE_CHAIN (current))
4525 if (FIELD_LOCAL_ALIAS (current))
4526 count++;
4527 DECL_FUNCTION_NAP (meth) = count;
4528 }
22eed1e6 4529
5e942c50
APB
4530 /* Eventually set the @deprecated tag flag */
4531 CHECK_DEPRECATED (meth);
4532
7f10c2e2
APB
4533 /* If doing xref, store column and line number information instead
4534 of the line number only. */
4535 if (flag_emit_xref)
4536 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4537
e04a16fb
AG
4538 return meth;
4539}
4540
5e942c50
APB
4541static void
4542fix_method_argument_names (orig_arg, meth)
4543 tree orig_arg, meth;
4544{
4545 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4546 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4547 {
4548 TREE_PURPOSE (arg) = this_identifier_node;
4549 arg = TREE_CHAIN (arg);
4550 }
de4c7b02 4551 while (orig_arg != end_params_node)
5e942c50
APB
4552 {
4553 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4554 orig_arg = TREE_CHAIN (orig_arg);
4555 arg = TREE_CHAIN (arg);
4556 }
4557}
4558
22eed1e6
APB
4559/* Complete the method declaration with METHOD_BODY. */
4560
4561static void
b635eb2f 4562finish_method_declaration (method_body)
22eed1e6
APB
4563 tree method_body;
4564{
79d13333
APB
4565 int flags;
4566
4567 if (!current_function_decl)
4568 return;
4569
4570 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4571
4572 /* 8.4.5 Method Body */
4573 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4574 {
c0b00d37
APB
4575 tree name = DECL_NAME (current_function_decl);
4576 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
5256aa37
APB
4577 "%s method `%s' can't have a body defined",
4578 (METHOD_NATIVE (current_function_decl) ?
4579 "Native" : "Abstract"),
c0b00d37 4580 IDENTIFIER_POINTER (name));
5256aa37
APB
4581 method_body = NULL_TREE;
4582 }
4583 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4584 {
c0b00d37 4585 tree name = DECL_NAME (current_function_decl);
781b0558 4586 parse_error_context
c0b00d37 4587 (DECL_FUNCTION_WFL (current_function_decl),
781b0558 4588 "Non native and non abstract method `%s' must have a body defined",
c0b00d37 4589 IDENTIFIER_POINTER (name));
5256aa37
APB
4590 method_body = NULL_TREE;
4591 }
4592
2c56429a
APB
4593 if (flag_emit_class_files && method_body
4594 && TREE_CODE (method_body) == NOP_EXPR
4595 && TREE_TYPE (current_function_decl)
4596 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4597 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4598
22eed1e6
APB
4599 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4600 maybe_absorb_scoping_blocks ();
4601 /* Exit function's body */
4602 exit_block ();
4603 /* Merge last line of the function with first line, directly in the
4604 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4605 if (!flag_emit_xref)
4606 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4607
4608 /* Since function's argument's list are shared, reset the
4609 ARG_FINAL_P parameter that might have been set on some of this
4610 function parameters. */
4611 UNMARK_FINAL_PARMS (current_function_decl);
4612
f099f336
APB
4613 /* So we don't have an irrelevant function declaration context for
4614 the next static block we'll see. */
4615 current_function_decl = NULL_TREE;
22eed1e6
APB
4616}
4617
4618/* Build a an error message for constructor circularity errors. */
4619
4620static char *
4621constructor_circularity_msg (from, to)
4622 tree from, to;
4623{
4624 static char string [4096];
c2e3db92 4625 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4626 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4627 free (t);
4628 return string;
4629}
4630
4631/* Verify a circular call to METH. Return 1 if an error is found, 0
4632 otherwise. */
4633
4634static int
4635verify_constructor_circularity (meth, current)
4636 tree meth, current;
4637{
4638 static tree list = NULL_TREE;
19e223db 4639 static int initialized_p;
22eed1e6 4640 tree c;
19e223db
MM
4641
4642 /* If we haven't already registered LIST with the garbage collector,
4643 do so now. */
4644 if (!initialized_p)
4645 {
4646 ggc_add_tree_root (&list, 1);
4647 initialized_p = 1;
4648 }
4649
22eed1e6
APB
4650 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4651 {
4652 if (TREE_VALUE (c) == meth)
4653 {
4654 char *t;
4655 if (list)
4656 {
4657 tree liste;
4658 list = nreverse (list);
4659 for (liste = list; liste; liste = TREE_CHAIN (liste))
4660 {
4661 parse_error_context
c63b98cd 4662 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4663 constructor_circularity_msg
4664 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4665 java_error_count--;
4666 }
4667 }
c2e3db92 4668 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4669 parse_error_context (TREE_PURPOSE (c),
4670 "%s: recursive invocation of constructor `%s'",
4671 constructor_circularity_msg (current, meth), t);
4672 free (t);
4673 list = NULL_TREE;
4674 return 1;
4675 }
4676 }
4677 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4678 {
4679 list = tree_cons (c, current, list);
4680 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4681 return 1;
4682 list = TREE_CHAIN (list);
4683 }
4684 return 0;
4685}
4686
e04a16fb
AG
4687/* Check modifiers that can be declared but exclusively */
4688
4689static void
4690check_modifiers_consistency (flags)
4691 int flags;
4692{
4693 int acc_count = 0;
4694 tree cl = NULL_TREE;
4695
e0fc4118
TT
4696 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4698 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4699 if (acc_count > 1)
4700 parse_error_context
e0fc4118
TT
4701 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4702
4703 acc_count = 0;
4704 cl = NULL_TREE;
14d075d8
TT
4705 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4706 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4707 if (acc_count > 1)
4708 parse_error_context (cl,
4709 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4710}
4711
4712/* Check the methode header METH for abstract specifics features */
4713
4714static void
4715check_abstract_method_header (meth)
4716 tree meth;
4717{
4718 int flags = get_access_flags_from_decl (meth);
e04a16fb 4719
2884c41e
KG
4720 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4721 ACC_ABSTRACT, "abstract method",
c4faeb92 4722 IDENTIFIER_POINTER (DECL_NAME (meth)));
2884c41e
KG
4723 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4724 ACC_PUBLIC, "abstract method",
c4faeb92 4725 IDENTIFIER_POINTER (DECL_NAME (meth)));
e04a16fb
AG
4726
4727 check_modifiers ("Illegal modifier `%s' for interface method",
4728 flags, INTERFACE_METHOD_MODIFIERS);
4729}
4730
4731/* Create a FUNCTION_TYPE node and start augmenting it with the
4732 declared function arguments. Arguments type that can't be resolved
4733 are left as they are, but the returned node is marked as containing
4734 incomplete types. */
4735
4736static tree
4737method_declarator (id, list)
4738 tree id, list;
4739{
4740 tree arg_types = NULL_TREE, current, node;
4741 tree meth = make_node (FUNCTION_TYPE);
4742 jdep *jdep;
e04a16fb
AG
4743
4744 patch_stage = JDEP_NO_PATCH;
c2952b01 4745
34d4df06
APB
4746 if (GET_CPC () == error_mark_node)
4747 return error_mark_node;
4748
c2952b01
APB
4749 /* If we're dealing with an inner class constructor, we hide the
4750 this$<n> decl in the name field of its parameter declaration. We
4751 also might have to hide the outer context local alias
4752 initializers. Not done when the class is a toplevel class. */
4753 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4754 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4755 {
4756 tree aliases_list, type, thisn;
4757 /* First the aliases, linked to the regular parameters */
4758 aliases_list =
4759 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4760 TREE_TYPE (GET_CPC ()),
4761 NULL_TREE, NULL);
4762 list = chainon (nreverse (aliases_list), list);
4763
4764 /* Then this$<n> */
4765 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4766 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4767 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4768 list);
4769 }
e04a16fb
AG
4770
4771 for (current = list; current; current = TREE_CHAIN (current))
4772 {
c583dd46 4773 int must_chain = 0;
e04a16fb
AG
4774 tree wfl_name = TREE_PURPOSE (current);
4775 tree type = TREE_VALUE (current);
4776 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4777 tree already, arg_node;
4778 tree type_wfl = NULL_TREE;
23a79c61 4779 tree real_type;
c583dd46
APB
4780
4781 /* Obtain a suitable type for resolution, if necessary */
4782 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4783
4784 /* Process NAME, as it may specify extra dimension(s) for it */
4785 type = build_array_from_name (type, type_wfl, name, &name);
4786 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4787
23a79c61
APB
4788 real_type = GET_REAL_TYPE (type);
4789 if (TREE_CODE (real_type) == RECORD_TYPE)
4790 {
4791 real_type = promote_type (real_type);
4792 if (TREE_CODE (type) == TREE_LIST)
4793 TREE_PURPOSE (type) = real_type;
4794 }
5e942c50 4795
e04a16fb
AG
4796 /* Check redefinition */
4797 for (already = arg_types; already; already = TREE_CHAIN (already))
4798 if (TREE_PURPOSE (already) == name)
4799 {
781b0558
KG
4800 parse_error_context
4801 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4802 IDENTIFIER_POINTER (name),
e04a16fb
AG
4803 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4804 break;
4805 }
4806
4807 /* If we've an incomplete argument type, we know there is a location
4808 to patch when the type get resolved, later. */
4809 jdep = NULL;
c583dd46 4810 if (must_chain)
e04a16fb 4811 {
c583dd46
APB
4812 patch_stage = JDEP_METHOD;
4813 type = register_incomplete_type (patch_stage,
4814 type_wfl, wfl_name, type);
4815 jdep = CLASSD_LAST (ctxp->classd_list);
4816 JDEP_MISC (jdep) = id;
e04a16fb 4817 }
c583dd46 4818
c2952b01 4819 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4820 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4821 /* Remeber arguments declared final. */
4822 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4823
e04a16fb
AG
4824 if (jdep)
4825 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4826 TREE_CHAIN (arg_node) = arg_types;
4827 arg_types = arg_node;
4828 }
de4c7b02 4829 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4830 node = build_tree_list (id, meth);
4831 return node;
4832}
4833
4834static int
4835unresolved_type_p (wfl, returned)
4836 tree wfl;
4837 tree *returned;
4838
4839{
4840 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4841 {
e04a16fb 4842 if (returned)
165f37bc
APB
4843 {
4844 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4845 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4846 *returned = TREE_TYPE (decl);
4847 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4848 *returned = TREE_TYPE (GET_CPC ());
4849 else
4850 *returned = NULL_TREE;
4851 }
e04a16fb
AG
4852 return 1;
4853 }
4854 if (returned)
4855 *returned = wfl;
4856 return 0;
4857}
4858
4859/* From NAME, build a qualified identifier node using the
4860 qualification from the current package definition. */
4861
4862static tree
98a52c2c 4863parser_qualified_classname (name)
e04a16fb
AG
4864 tree name;
4865{
c2952b01
APB
4866 tree nested_class_name;
4867
98a52c2c 4868 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4869 return nested_class_name;
4870
e04a16fb 4871 if (ctxp->package)
c2952b01 4872 return merge_qualified_name (ctxp->package, name);
e04a16fb 4873 else
c2952b01 4874 return name;
e04a16fb
AG
4875}
4876
4877/* Called once the type a interface extends is resolved. Returns 0 if
4878 everything is OK. */
4879
4880static int
4881parser_check_super_interface (super_decl, this_decl, this_wfl)
4882 tree super_decl, this_decl, this_wfl;
4883{
4884 tree super_type = TREE_TYPE (super_decl);
4885
4886 /* Has to be an interface */
c2952b01 4887 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4888 {
4889 parse_error_context
4890 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4891 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4892 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4893 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4894 "interface" : "class"),
4895 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4896 return 1;
4897 }
4898
a648f4e4
BM
4899 /* Check top-level interface access. Inner classes are subject to member
4900 access rules (6.6.1). */
4901 if (! INNER_CLASS_P (super_type)
4902 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
e04a16fb
AG
4903 return 1;
4904
4905 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4906 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4907 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4908 return 0;
4909}
4910
4911/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4912 0 if everthing is OK. */
4913
4914static int
4915parser_check_super (super_decl, this_decl, wfl)
4916 tree super_decl, this_decl, wfl;
4917{
e04a16fb
AG
4918 tree super_type = TREE_TYPE (super_decl);
4919
4920 /* SUPER should be a CLASS (neither an array nor an interface) */
4921 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4922 {
4923 parse_error_context
4924 (wfl, "Class `%s' can't subclass %s `%s'",
4925 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4926 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4927 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4928 return 1;
4929 }
4930
4931 if (CLASS_FINAL (TYPE_NAME (super_type)))
4932 {
4933 parse_error_context (wfl, "Can't subclass final classes: %s",
4934 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4935 return 1;
4936 }
4937
a648f4e4
BM
4938 /* Check top-level class scope. Inner classes are subject to member access
4939 rules (6.6.1). */
4940 if (! INNER_CLASS_P (super_type)
4941 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
e04a16fb
AG
4942 return 1;
4943
4944 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4945 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4946 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4947 return 0;
4948}
4949
4950/* Create a new dependency list and link it (in a LIFO manner) to the
4951 CTXP list of type dependency list. */
4952
4953static void
4954create_jdep_list (ctxp)
4955 struct parser_ctxt *ctxp;
4956{
23a79c61 4957 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4958 new->first = new->last = NULL;
4959 new->next = ctxp->classd_list;
4960 ctxp->classd_list = new;
4961}
4962
4963static jdeplist *
4964reverse_jdep_list (ctxp)
4965 struct parser_ctxt *ctxp;
4966{
4967 register jdeplist *prev = NULL, *current, *next;
4968 for (current = ctxp->classd_list; current; current = next)
4969 {
4970 next = current->next;
4971 current->next = prev;
4972 prev = current;
4973 }
4974 return prev;
4975}
4976
23a79c61
APB
4977/* Create a fake pointer based on the ID stored in
4978 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4979 registered again. */
e04a16fb
AG
4980
4981static tree
23a79c61
APB
4982obtain_incomplete_type (type_name)
4983 tree type_name;
e04a16fb 4984{
23a79c61
APB
4985 tree ptr, name;
4986
4987 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4988 name = EXPR_WFL_NODE (type_name);
4989 else if (INCOMPLETE_TYPE_P (type_name))
4990 name = TYPE_NAME (type_name);
4991 else
400500c4 4992 abort ();
e04a16fb
AG
4993
4994 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4995 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4996 break;
4997
4998 if (!ptr)
4999 {
78d21f92
PB
5000 BUILD_PTR_FROM_NAME (ptr, name);
5001 layout_type (ptr);
e04a16fb
AG
5002 TREE_CHAIN (ptr) = ctxp->incomplete_class;
5003 ctxp->incomplete_class = ptr;
5004 }
5005
5006 return ptr;
5007}
5008
5009/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5010 non NULL instead of computing a new fake type based on WFL. The new
5011 dependency is inserted in the current type dependency list, in FIFO
5012 manner. */
5013
5014static tree
5015register_incomplete_type (kind, wfl, decl, ptr)
5016 int kind;
5017 tree wfl, decl, ptr;
5018{
23a79c61 5019 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5020
e04a16fb
AG
5021 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5022 ptr = obtain_incomplete_type (wfl);
5023
5024 JDEP_KIND (new) = kind;
5025 JDEP_DECL (new) = decl;
5026 JDEP_SOLV (new) = ptr;
5027 JDEP_WFL (new) = wfl;
5028 JDEP_CHAIN (new) = NULL;
5029 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5030 /* For some dependencies, set the enclosing class of the current
5031 class to be the enclosing context */
8ac1de05
APB
5032 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5033 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5034 && GET_ENCLOSING_CPC ())
5035 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5036 else
324ed8fd 5037 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5038 JDEP_GET_PATCH (new) = (tree *)NULL;
5039
5040 JDEP_INSERT (ctxp->classd_list, new);
5041
5042 return ptr;
5043}
5044
5045void
5046java_check_circular_reference ()
5047{
5048 tree current;
5049 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5050 {
5051 tree type = TREE_TYPE (current);
e920ebc9 5052 if (CLASS_INTERFACE (current))
e04a16fb
AG
5053 {
5054 /* Check all interfaces this class extends */
5055 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5056 int n, i;
5057
5058 if (!basetype_vec)
5059 return;
5060 n = TREE_VEC_LENGTH (basetype_vec);
5061 for (i = 0; i < n; i++)
5062 {
5063 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5064 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5065 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5066 parse_error_context (lookup_cl (current),
5067 "Cyclic interface inheritance");
5068 }
5069 }
5070 else
5071 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5072 parse_error_context (lookup_cl (current),
c2952b01
APB
5073 "Cyclic class inheritance%s",
5074 (cyclic_inheritance_report ?
5075 cyclic_inheritance_report : ""));
5076 }
5077}
5078
5079/* Augment the parameter list PARM with parameters crafted to
5080 initialize outer context locals aliases. Through ARTIFICIAL, a
5081 count is kept of the number of crafted parameters. MODE governs
5082 what eventually gets created: something suitable for a function
5083 creation or a function invocation, either the constructor or
c00f0fb2 5084 finit$. */
c2952b01
APB
5085
5086static tree
5087build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5088 int mode;
5089 tree class_type, parm;
5090 int *artificial;
5091{
5092 tree field;
da632f2c
APB
5093 tree additional_parms = NULL_TREE;
5094
c2952b01
APB
5095 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5096 if (FIELD_LOCAL_ALIAS (field))
5097 {
63ad61ed 5098 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5099 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5100 tree mangled_id;
c2952b01
APB
5101
5102 switch (mode)
5103 {
5104 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5105 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5106 &buffer [4]);
5107 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5108 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5109 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5110 else
5111 value = TREE_TYPE (field);
5112 break;
5113
5114 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5115 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5116 &buffer [4]);
c2952b01
APB
5117 value = TREE_TYPE (field);
5118 break;
5119
5120 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5121 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5122 &buffer [4]);
c2952b01
APB
5123 /* Now, this is wrong. purpose should always be the NAME
5124 of something and value its matching value (decl, type,
5125 etc...) FIXME -- but there is a lot to fix. */
5126
5127 /* When invoked for this kind of operation, we already
5128 know whether a field is used or not. */
5129 purpose = TREE_TYPE (field);
1f8f4a0b 5130 value = build_wfl_node (mangled_id);
c2952b01
APB
5131 break;
5132
5133 case AIPL_FUNCTION_CTOR_INVOCATION:
5134 /* There are two case: the constructor invokation happends
5135 outside the local inner, in which case, locales from the outer
5136 context are directly used.
5137
5138 Otherwise, we fold to using the alias directly. */
5139 if (class_type == current_class)
5140 value = field;
5141 else
5142 {
5143 name = get_identifier (&buffer[4]);
5144 value = IDENTIFIER_LOCAL_VALUE (name);
5145 }
5146 break;
5147 }
da632f2c 5148 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5149 if (artificial)
5150 *artificial +=1;
5151 }
da632f2c
APB
5152 if (additional_parms)
5153 {
5154 if (ANONYMOUS_CLASS_P (class_type)
5155 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5156 additional_parms = nreverse (additional_parms);
5157 parm = chainon (additional_parms, parm);
5158 }
5159
5160 return parm;
c2952b01
APB
5161}
5162
5163/* Craft a constructor for CLASS_DECL -- what we should do when none
5164 where found. ARGS is non NULL when a special signature must be
5165 enforced. This is the case for anonymous classes. */
5166
5167static void
5168craft_constructor (class_decl, args)
5169 tree class_decl, args;
5170{
5171 tree class_type = TREE_TYPE (class_decl);
5172 tree parm = NULL_TREE;
5173 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5174 ACC_PUBLIC : 0);
5175 int i = 0, artificial = 0;
5176 tree decl, ctor_name;
5177 char buffer [80];
5178
c2952b01
APB
5179 /* The constructor name is <init> unless we're dealing with an
5180 anonymous class, in which case the name will be fixed after having
5181 be expanded. */
5182 if (ANONYMOUS_CLASS_P (class_type))
5183 ctor_name = DECL_NAME (class_decl);
5184 else
5185 ctor_name = init_identifier_node;
5186
5187 /* If we're dealing with an inner class constructor, we hide the
5188 this$<n> decl in the name field of its parameter declaration. */
5189 if (PURE_INNER_CLASS_TYPE_P (class_type))
5190 {
5191 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5192 parm = tree_cons (build_current_thisn (class_type),
5193 build_pointer_type (type), parm);
5194
5195 /* Some more arguments to be hidden here. The values of the local
5196 variables of the outer context that the inner class needs to see. */
5197 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5198 class_type, parm,
5199 &artificial);
5200 }
5201
5202 /* Then if there are any args to be enforced, enforce them now */
5203 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5204 {
5205 sprintf (buffer, "parm%d", i++);
5206 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5207 }
c2952b01
APB
5208
5209 CRAFTED_PARAM_LIST_FIXUP (parm);
5210 decl = create_artificial_method (class_type, flags, void_type_node,
5211 ctor_name, parm);
5212 fix_method_argument_names (parm, decl);
5213 /* Now, mark the artificial parameters. */
5214 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5215 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5216}
5217
c2952b01 5218
e920ebc9
APB
5219/* Fix the constructors. This will be called right after circular
5220 references have been checked. It is necessary to fix constructors
5221 early even if no code generation will take place for that class:
5222 some generated constructor might be required by the class whose
5223 compilation triggered this one to be simply loaded. */
5224
5225void
5226java_fix_constructors ()
5227{
5228 tree current;
5229
5230 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5231 {
e920ebc9
APB
5232 tree class_type = TREE_TYPE (current);
5233 int saw_ctor = 0;
c2952b01
APB
5234 tree decl;
5235
5236 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5237 continue;
e920ebc9
APB
5238
5239 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5240 {
5241 if (DECL_CONSTRUCTOR_P (decl))
5242 {
5243 fix_constructors (decl);
5244 saw_ctor = 1;
5245 }
5246 }
5247
c2952b01
APB
5248 /* Anonymous class constructor can't be generated that early. */
5249 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5250 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5251 }
5252}
5253
23a79c61
APB
5254/* safe_layout_class just makes sure that we can load a class without
5255 disrupting the current_class, input_file, lineno, etc, information
5256 about the class processed currently. */
5257
e04a16fb
AG
5258void
5259safe_layout_class (class)
5260 tree class;
5261{
5262 tree save_current_class = current_class;
3b304f5b 5263 const char *save_input_filename = input_filename;
e04a16fb 5264 int save_lineno = lineno;
5e942c50 5265
e04a16fb 5266 layout_class (class);
5e942c50 5267
e04a16fb
AG
5268 current_class = save_current_class;
5269 input_filename = save_input_filename;
5270 lineno = save_lineno;
5271 CLASS_LOADED_P (class) = 1;
5272}
5273
5274static tree
5275jdep_resolve_class (dep)
5276 jdep *dep;
5277{
5278 tree decl;
5279
23a79c61
APB
5280 if (JDEP_RESOLVED_P (dep))
5281 decl = JDEP_RESOLVED_DECL (dep);
5282 else
e04a16fb 5283 {
c2952b01 5284 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5285 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5286 JDEP_RESOLVED (dep, decl);
5287 }
23a79c61 5288
e04a16fb 5289 if (!decl)
23a79c61 5290 complete_class_report_errors (dep);
1e12ab9b 5291 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5292 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5293 return decl;
5294}
5295
5296/* Complete unsatisfied class declaration and their dependencies */
5297
5298void
5299java_complete_class ()
5300{
e04a16fb
AG
5301 tree cclass;
5302 jdeplist *cclassd;
5303 int error_found;
b67d701b 5304 tree type;
e04a16fb 5305
9a7ab4b3 5306 /* Process imports */
e04a16fb 5307 process_imports ();
e04a16fb
AG
5308
5309 /* Rever things so we have the right order */
5310 ctxp->class_list = nreverse (ctxp->class_list);
5311 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5312
e04a16fb
AG
5313 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5314 cclass && cclassd;
5315 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5316 {
5317 jdep *dep;
5318 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5319 {
5320 tree decl;
e04a16fb
AG
5321 if (!(decl = jdep_resolve_class (dep)))
5322 continue;
5323
5324 /* Now it's time to patch */
5325 switch (JDEP_KIND (dep))
5326 {
5327 case JDEP_SUPER:
5328 /* Simply patch super */
5329 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5330 continue;
5331 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5332 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5333 break;
5334
5335 case JDEP_FIELD:
5336 {
5337 /* We do part of the job done in add_field */
5338 tree field_decl = JDEP_DECL (dep);
5339 tree field_type = TREE_TYPE (decl);
e04a16fb 5340 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5341 field_type = promote_type (field_type);
e04a16fb 5342 TREE_TYPE (field_decl) = field_type;
5e942c50 5343 DECL_ALIGN (field_decl) = 0;
11cf4d18 5344 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5345 layout_decl (field_decl, 0);
e04a16fb
AG
5346 SOURCE_FRONTEND_DEBUG
5347 (("Completed field/var decl `%s' with `%s'",
5348 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5349 IDENTIFIER_POINTER (DECL_NAME (decl))));
5350 break;
5351 }
5352 case JDEP_METHOD: /* We start patching a method */
5353 case JDEP_METHOD_RETURN:
5354 error_found = 0;
5355 while (1)
5356 {
5357 if (decl)
5358 {
b67d701b
PB
5359 type = TREE_TYPE(decl);
5360 if (TREE_CODE (type) == RECORD_TYPE)
5361 type = promote_type (type);
e04a16fb
AG
5362 JDEP_APPLY_PATCH (dep, type);
5363 SOURCE_FRONTEND_DEBUG
5364 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5365 "Completing fct `%s' with ret type `%s'":
5366 "Completing arg `%s' with type `%s'"),
5367 IDENTIFIER_POINTER (EXPR_WFL_NODE
5368 (JDEP_DECL_WFL (dep))),
5369 IDENTIFIER_POINTER (DECL_NAME (decl))));
5370 }
5371 else
5372 error_found = 1;
5373 dep = JDEP_CHAIN (dep);
5374 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5375 break;
5376 else
5377 decl = jdep_resolve_class (dep);
5378 }
5379 if (!error_found)
5380 {
5381 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5382 /* Recompute and reset the signature, check first that
5383 all types are now defined. If they're not,
5384 dont build the signature. */
5385 if (check_method_types_complete (mdecl))
5386 {
5387 signature = build_java_signature (TREE_TYPE (mdecl));
5388 set_java_signature (TREE_TYPE (mdecl), signature);
5389 }
e04a16fb
AG
5390 }
5391 else
5392 continue;
5393 break;
5394
5395 case JDEP_INTERFACE:
5396 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5397 JDEP_WFL (dep)))
5398 continue;
5399 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5400 break;
5401
b67d701b 5402 case JDEP_PARM:
e04a16fb 5403 case JDEP_VARIABLE:
b67d701b
PB
5404 type = TREE_TYPE(decl);
5405 if (TREE_CODE (type) == RECORD_TYPE)
5406 type = promote_type (type);
5407 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5408 break;
5409
5410 case JDEP_TYPE:
5411 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5412 SOURCE_FRONTEND_DEBUG
5413 (("Completing a random type dependency on a '%s' node",
5414 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5415 break;
5416
b9f7e36c 5417 case JDEP_EXCEPTION:
c877974e
APB
5418 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5419 SOURCE_FRONTEND_DEBUG
5420 (("Completing `%s' `throws' argument node",
5421 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5422 break;
5423
c2952b01
APB
5424 case JDEP_ANONYMOUS:
5425 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5426 break;
5427
e04a16fb 5428 default:
400500c4 5429 abort ();
e04a16fb
AG
5430 }
5431 }
5432 }
e04a16fb
AG
5433 return;
5434}
5435
5436/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5437 array. */
5438
5439static tree
c2952b01
APB
5440resolve_class (enclosing, class_type, decl, cl)
5441 tree enclosing, class_type, decl, cl;
e04a16fb 5442{
49f48c71
KG
5443 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5444 const char *base = name;
78d21f92
PB
5445 tree resolved_type = TREE_TYPE (class_type);
5446 tree resolved_type_decl;
e04a16fb 5447
78d21f92
PB
5448 if (resolved_type != NULL_TREE)
5449 {
5450 tree resolved_type_decl = TYPE_NAME (resolved_type);
5451 if (resolved_type_decl == NULL_TREE
5452 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5453 {
5454 resolved_type_decl = build_decl (TYPE_DECL,
5455 TYPE_NAME (class_type),
5456 resolved_type);
5457 }
5458 return resolved_type_decl;
5459 }
5460
e04a16fb
AG
5461 /* 1- Check to see if we have an array. If true, find what we really
5462 want to resolve */
5463 while (name[0] == '[')
5464 name++;
5465 if (base != name)
34d4df06
APB
5466 {
5467 TYPE_NAME (class_type) = get_identifier (name);
5468 WFL_STRIP_BRACKET (cl, cl);
5469 }
e04a16fb
AG
5470
5471 /* 2- Resolve the bare type */
c2952b01
APB
5472 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5473 decl, cl)))
e04a16fb
AG
5474 return NULL_TREE;
5475 resolved_type = TREE_TYPE (resolved_type_decl);
5476
5477 /* 3- If we have and array, reconstruct the array down to its nesting */
5478 if (base != name)
5479 {
5480 while (base != name)
5481 {
5482 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5483 resolved_type = promote_type (resolved_type);
5484 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5485 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5486 name--;
5487 }
e101152f
APB
5488 /* A TYPE_NAME that is a TYPE_DECL was set in
5489 build_java_array_type, return it. */
5490 resolved_type_decl = TYPE_NAME (resolved_type);
e04a16fb 5491 }
78d21f92 5492 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5493 return resolved_type_decl;
5494}
5495
5496/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5497 are used to report error messages. */
5498
78d21f92 5499tree
c2952b01
APB
5500do_resolve_class (enclosing, class_type, decl, cl)
5501 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5502{
5503 tree new_class_decl;
e04a16fb
AG
5504
5505 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5506 it is changed by find_in_imports{_on_demand} and (but it doesn't
5507 really matter) qualify_and_find */
e04a16fb 5508
c2952b01
APB
5509 /* 0- Search in the current class as an inner class */
5510
5511 /* Maybe some code here should be added to load the class or
5512 something, at least if the class isn't an inner class and ended
5513 being loaded from class file. FIXME. */
a40d21da
APB
5514 while (enclosing)
5515 {
e1d565ab 5516 tree intermediate;
a40d21da
APB
5517
5518 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5519 return new_class_decl;
8ac1de05 5520
e1d565ab 5521 intermediate = enclosing;
0c2b8145 5522 /* Explore enclosing contexts. */
e1d565ab 5523 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5524 {
e1d565ab
BM
5525 intermediate = DECL_CONTEXT (intermediate);
5526 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5527 class_type, cl)))
5528 return new_class_decl;
5529 }
5530
a40d21da
APB
5531 /* Now go to the upper classes, bail out if necessary. */
5532 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5533 if (!enclosing || enclosing == object_type_node)
a648f4e4 5534 break;
a40d21da 5535
a648f4e4
BM
5536 if (TREE_CODE (enclosing) == POINTER_TYPE)
5537 enclosing = do_resolve_class (NULL, enclosing, NULL, NULL);
a40d21da 5538 else
a648f4e4 5539 enclosing = TYPE_NAME (enclosing);
a40d21da 5540 }
c2952b01 5541
9a7ab4b3
APB
5542 /* 1- Check for the type in single imports. This will change
5543 TYPE_NAME() if something relevant is found */
5544 find_in_imports (class_type);
e04a16fb 5545
9a7ab4b3 5546 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5547 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5548 {
5549 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5550 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5551 load_class (TYPE_NAME (class_type), 0);
5552 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5553 }
5554
9a7ab4b3
APB
5555 /* 3- Search according to the current package definition */
5556 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5557 {
5558 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5559 TYPE_NAME (class_type))))
5560 return new_class_decl;
5561 }
5562
5563 /* 4- Check the import on demands. Don't allow bar.baz to be
5564 imported from foo.* */
5565 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5566 if (find_in_imports_on_demand (class_type))
5567 return NULL_TREE;
5568
5569 /* If found in find_in_imports_on_demant, the type has already been
5570 loaded. */
5571 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5572 return new_class_decl;
5573
5574 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5575 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5576 {
ee07f4f4 5577 tree package;
d6baf6f5
APB
5578
5579 /* If there is a current package (ctxp->package), it's the first
5580 element of package_list and we can skip it. */
5581 for (package = (ctxp->package ?
5582 TREE_CHAIN (package_list) : package_list);
5583 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5584 if ((new_class_decl = qualify_and_find (class_type,
5585 TREE_PURPOSE (package),
5586 TYPE_NAME (class_type))))
5587 return new_class_decl;
5588 }
5589
5590 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb 5591 load_class (TYPE_NAME (class_type), 0);
a648f4e4
BM
5592
5593 if (!cl)
5594 cl = lookup_cl (decl);
5595
5596 /* If we don't have a value for CL, then we're being called recursively.
5597 We can't check package access just yet, but it will be taken care of
5598 by the caller. */
5599 if (cl)
5600 {
5601 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5602 return NULL_TREE;
5603 }
5604
9a7ab4b3 5605 /* 6- Last call for a resolution */
e04a16fb
AG
5606 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5607}
5608
9a7ab4b3
APB
5609static tree
5610qualify_and_find (class_type, package, name)
5611 tree class_type, package, name;
5612{
5613 tree new_qualified = merge_qualified_name (package, name);
5614 tree new_class_decl;
5615
5616 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5617 load_class (new_qualified, 0);
5618 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5619 {
5620 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5621 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5622 load_class (new_qualified, 0);
5623 TYPE_NAME (class_type) = new_qualified;
5624 return IDENTIFIER_CLASS_VALUE (new_qualified);
5625 }
5626 return NULL_TREE;
5627}
5628
e04a16fb 5629/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5630 parsed class). Return a decl node. This function is meant to be
5631 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5632
5633static tree
c877974e
APB
5634resolve_and_layout (something, cl)
5635 tree something;
e04a16fb
AG
5636 tree cl;
5637{
34d4df06 5638 tree decl, decl_type;
c877974e 5639
23a79c61
APB
5640 /* Don't do that on the current class */
5641 if (something == current_class)
5642 return TYPE_NAME (current_class);
c877974e 5643
23a79c61 5644 /* Don't do anything for void and other primitive types */
c877974e
APB
5645 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5646 return NULL_TREE;
5647
23a79c61
APB
5648 /* Pointer types can be reall pointer types or fake pointers. When
5649 finding a real pointer, recheck for primitive types */
5650 if (TREE_CODE (something) == POINTER_TYPE)
5651 {
5652 if (TREE_TYPE (something))
5653 {
5654 something = TREE_TYPE (something);
5655 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5656 return NULL_TREE;
5657 }
5658 else
5659 something = TYPE_NAME (something);
5660 }
5661
5662 /* Don't do anything for arrays of primitive types */
5663 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5664 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5665 return NULL_TREE;
5666
c2952b01
APB
5667 /* Something might be a WFL */
5668 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5669 something = EXPR_WFL_NODE (something);
5670
5671 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5672 TYPE_DECL or a real TYPE */
5673 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5674 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5675 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5676
23a79c61
APB
5677 if (!(decl = resolve_no_layout (something, cl)))
5678 return NULL_TREE;
5679
5680 /* Resolve and layout if necessary */
34d4df06
APB
5681 decl_type = TREE_TYPE (decl);
5682 layout_class_methods (decl_type);
5683 /* Check methods */
5684 if (CLASS_FROM_SOURCE_P (decl_type))
5685 java_check_methods (decl);
5686 /* Layout the type if necessary */
5687 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5688 safe_layout_class (decl_type);
23a79c61 5689
e04a16fb
AG
5690 return decl;
5691}
5692
5693/* Resolve a class, returns its decl but doesn't perform any
5694 layout. The current parsing context is saved and restored */
5695
5696static tree
5697resolve_no_layout (name, cl)
5698 tree name, cl;
5699{
5700 tree ptr, decl;
5701 BUILD_PTR_FROM_NAME (ptr, name);
5702 java_parser_context_save_global ();
c2952b01 5703 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5704 java_parser_context_restore_global ();
5705
5706 return decl;
5707}
5708
23a79c61
APB
5709/* Called when reporting errors. Skip leader '[' in a complex array
5710 type description that failed to be resolved. */
e04a16fb 5711
49f48c71 5712static const char *
e04a16fb 5713purify_type_name (name)
49f48c71 5714 const char *name;
e04a16fb
AG
5715{
5716 while (*name && *name == '[')
5717 name++;
5718 return name;
5719}
5720
5721/* The type CURRENT refers to can't be found. We print error messages. */
5722
5723static void
5724complete_class_report_errors (dep)
5725 jdep *dep;
5726{
49f48c71 5727 const char *name;
23a79c61
APB
5728
5729 if (!JDEP_WFL (dep))
5730 return;
5731
5732 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5733 switch (JDEP_KIND (dep))
5734 {
5735 case JDEP_SUPER:
5736 parse_error_context
5737 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5738 purify_type_name (name),
e04a16fb
AG
5739 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5740 break;
5741 case JDEP_FIELD:
5742 parse_error_context
5743 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5744 purify_type_name (name),
e04a16fb
AG
5745 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5746 break;
5747 case JDEP_METHOD: /* Covers arguments */
5748 parse_error_context
781b0558 5749 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5750 purify_type_name (name),
e04a16fb
AG
5751 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5753 break;
5754 case JDEP_METHOD_RETURN: /* Covers return type */
5755 parse_error_context
781b0558 5756 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5757 purify_type_name (name),
e04a16fb
AG
5758 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5759 break;
5760 case JDEP_INTERFACE:
5761 parse_error_context
5762 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5763 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5764 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5765 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5766 break;
5767 case JDEP_VARIABLE:
5768 parse_error_context
781b0558 5769 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5770 purify_type_name (IDENTIFIER_POINTER
5771 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5772 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5773 break;
b9f7e36c
APB
5774 case JDEP_EXCEPTION: /* As specified by `throws' */
5775 parse_error_context
5776 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5777 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5778 break;
0a2138e2
APB
5779 default:
5780 /* Fix for -Wall. Just break doing nothing. The error will be
5781 caught later */
5782 break;
e04a16fb
AG
5783 }
5784}
5785
22eed1e6
APB
5786/* Return a static string containing the DECL prototype string. If
5787 DECL is a constructor, use the class name instead of the form
5788 <init> */
5789
49f48c71 5790static const char *
22eed1e6
APB
5791get_printable_method_name (decl)
5792 tree decl;
5793{
49f48c71 5794 const char *to_return;
9ee9b555 5795 tree name = NULL_TREE;
22eed1e6
APB
5796
5797 if (DECL_CONSTRUCTOR_P (decl))
5798 {
5799 name = DECL_NAME (decl);
5e942c50 5800 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5801 }
5802
5803 to_return = lang_printable_name (decl, 0);
5804 if (DECL_CONSTRUCTOR_P (decl))
5805 DECL_NAME (decl) = name;
5806
5807 return to_return;
5808}
5809
5810/* Track method being redefined inside the same class. As a side
5811 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5812 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5813
e04a16fb
AG
5814static int
5815check_method_redefinition (class, method)
5816 tree class, method;
5817{
c4faeb92 5818 tree redef, sig;
5e942c50 5819
c4faeb92
APB
5820 /* There's no need to verify <clinit> and finit$ */
5821 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
e04a16fb 5822 return 0;
5e942c50 5823
c4faeb92 5824 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
e04a16fb
AG
5825 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5826 {
c3f2a476 5827 if (redef == method)
e04a16fb 5828 break;
c4faeb92
APB
5829 if (DECL_NAME (redef) == DECL_NAME (method)
5830 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5831 && !DECL_ARTIFICIAL (method))
e04a16fb 5832 {
22eed1e6 5833 parse_error_context
c4faeb92 5834 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
22eed1e6
APB
5835 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5836 get_printable_method_name (redef));
e04a16fb
AG
5837 return 1;
5838 }
5839 }
5840 return 0;
5841}
5842
1175b9b4
TT
5843/* Return 1 if check went ok, 0 otherwise. */
5844static int
d77613be
APB
5845check_abstract_method_definitions (do_interface, class_decl, type)
5846 int do_interface;
5847 tree class_decl, type;
5848{
5849 tree class = TREE_TYPE (class_decl);
5850 tree method, end_type;
1175b9b4 5851 int ok = 1;
d77613be
APB
5852
5853 end_type = (do_interface ? object_type_node : type);
5854 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5855 {
5856 tree other_super, other_method, method_sig, method_name;
5857 int found = 0;
165f37bc 5858 int end_type_reached = 0;
d77613be
APB
5859
5860 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5861 continue;
5862
5863 /* Now verify that somewhere in between TYPE and CLASS,
5864 abstract method METHOD gets a non abstract definition
5865 that is inherited by CLASS. */
5866
5867 method_sig = build_java_signature (TREE_TYPE (method));
5868 method_name = DECL_NAME (method);
5869 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5870 method_name = EXPR_WFL_NODE (method_name);
5871
165f37bc
APB
5872 other_super = class;
5873 do {
5874 if (other_super == end_type)
5875 end_type_reached = 1;
5876
5877 /* Method search */
5878 for (other_method = TYPE_METHODS (other_super); other_method;
5879 other_method = TREE_CHAIN (other_method))
5880 {
5881 tree s = build_java_signature (TREE_TYPE (other_method));
5882 tree other_name = DECL_NAME (other_method);
5883
5884 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5885 other_name = EXPR_WFL_NODE (other_name);
5886 if (!DECL_CLINIT_P (other_method)
5887 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5888 && method_name == other_name
5889 && method_sig == s
5890 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5891 {
5892 found = 1;
5893 break;
5894 }
5895 }
5896 other_super = CLASSTYPE_SUPER (other_super);
5897 } while (!end_type_reached);
5898
d77613be
APB
5899 /* Report that abstract METHOD didn't find an implementation
5900 that CLASS can use. */
5901 if (!found)
5902 {
c2e3db92 5903 char *t = xstrdup (lang_printable_name
d77613be
APB
5904 (TREE_TYPE (TREE_TYPE (method)), 0));
5905 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
d77613be
APB
5906
5907 parse_error_context
5908 (lookup_cl (class_decl),
781b0558 5909 "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
5910 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5911 t, lang_printable_name (method, 0),
5912 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5913 "interface" : "class"),
5914 IDENTIFIER_POINTER (ccn),
5915 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5916 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5917 ok = 0;
d77613be 5918 free (t);
d77613be
APB
5919 }
5920 }
1175b9b4
TT
5921
5922 if (ok && do_interface)
5923 {
5924 /* Check for implemented interfaces. */
5925 int i;
5926 tree vector = TYPE_BINFO_BASETYPES (type);
5927 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5928 {
5929 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5930 ok = check_abstract_method_definitions (1, class_decl, super);
5931 }
5932 }
5933
5934 return ok;
d77613be
APB
5935}
5936
614eaae0 5937/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5938 methods. */
5939
5940static void
5941java_check_abstract_method_definitions (class_decl)
5942 tree class_decl;
5943{
5944 tree class = TREE_TYPE (class_decl);
5945 tree super, vector;
5946 int i;
5947
5948 if (CLASS_ABSTRACT (class_decl))
5949 return;
5950
5951 /* Check for inherited types */
165f37bc
APB
5952 super = class;
5953 do {
5954 super = CLASSTYPE_SUPER (super);
5955 check_abstract_method_definitions (0, class_decl, super);
5956 } while (super != object_type_node);
d77613be
APB
5957
5958 /* Check for implemented interfaces. */
5959 vector = TYPE_BINFO_BASETYPES (class);
5960 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5961 {
5962 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5963 check_abstract_method_definitions (1, class_decl, super);
5964 }
5965}
5966
165f37bc
APB
5967/* Check all the types method DECL uses and return 1 if all of them
5968 are now complete, 0 otherwise. This is used to check whether its
5969 safe to build a method signature or not. */
5970
5971static int
5972check_method_types_complete (decl)
5973 tree decl;
5974{
5975 tree type = TREE_TYPE (decl);
5976 tree args;
5977
5978 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5979 return 0;
5980
5981 args = TYPE_ARG_TYPES (type);
5982 if (TREE_CODE (type) == METHOD_TYPE)
5983 args = TREE_CHAIN (args);
5984 for (; args != end_params_node; args = TREE_CHAIN (args))
5985 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5986 return 0;
5987
5988 return 1;
5989}
5990
34d4df06
APB
5991/* Visible interface to check methods contained in CLASS_DECL */
5992
5993void
5994java_check_methods (class_decl)
5995 tree class_decl;
5996{
5997 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5998 return;
5999
6000 if (CLASS_INTERFACE (class_decl))
6001 java_check_abstract_methods (class_decl);
6002 else
6003 java_check_regular_methods (class_decl);
6004
6005 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6006}
6007
d77613be
APB
6008/* Check all the methods of CLASS_DECL. Methods are first completed
6009 then checked according to regular method existance rules. If no
6010 constructor for CLASS_DECL were encountered, then build its
6011 declaration. */
e04a16fb
AG
6012
6013static void
6014java_check_regular_methods (class_decl)
6015 tree class_decl;
6016{
c2952b01 6017 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6018 tree method;
6019 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
c4faeb92 6020 tree found = NULL_TREE;
c877974e
APB
6021 tree mthrows;
6022
6023 /* It is not necessary to check methods defined in java.lang.Object */
6024 if (class == object_type_node)
6025 return;
e04a16fb 6026
23a79c61
APB
6027 if (!TYPE_NVIRTUALS (class))
6028 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6029
6030 /* Should take interfaces into account. FIXME */
6031 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6032 {
5e942c50 6033 tree sig;
c4faeb92 6034 tree method_wfl = DECL_FUNCTION_WFL (method);
e04a16fb
AG
6035 int aflags;
6036
e04a16fb
AG
6037 /* Check for redefinitions */
6038 if (check_method_redefinition (class, method))
6039 continue;
6040
22eed1e6
APB
6041 /* If we see one constructor a mark so we don't generate the
6042 default one. Also skip other verifications: constructors
6043 can't be inherited hence hiden or overriden */
6044 if (DECL_CONSTRUCTOR_P (method))
6045 {
6046 saw_constructor = 1;
6047 continue;
6048 }
6049
c877974e
APB
6050 /* We verify things thrown by the method. They must inherits from
6051 java.lang.Throwable */
6052 for (mthrows = DECL_FUNCTION_THROWS (method);
6053 mthrows; mthrows = TREE_CHAIN (mthrows))
6054 {
6055 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6056 parse_error_context
781b0558 6057 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6058 IDENTIFIER_POINTER
6059 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6060 }
6061
e04a16fb 6062 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6063 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6064
c2952b01
APB
6065 /* Inner class can't declare static methods */
6066 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6067 {
6068 char *t = xstrdup (lang_printable_name (class, 0));
6069 parse_error_context
6070 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6071 lang_printable_name (method, 0), t);
6072 free (t);
6073 }
6074
5e942c50 6075 /* Nothing overrides or it's a private method. */
aabd7048 6076 if (!found)
5e942c50 6077 continue;
aabd7048
PB
6078 if (METHOD_PRIVATE (found))
6079 {
6080 found = NULL_TREE;
6081 continue;
6082 }
5e942c50 6083
614eaae0
APB
6084 /* If `found' is declared in an interface, make sure the
6085 modifier matches. */
6086 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6087 && clinit_identifier_node != DECL_NAME (found)
6088 && !METHOD_PUBLIC (method))
6089 {
6090 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6091 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6092 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6093 lang_printable_name (method, 0),
6094 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6095 }
6096
e04a16fb
AG
6097 /* Can't override a method with the same name and different return
6098 types. */
6099 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6100 {
614eaae0
APB
6101 char *t = xstrdup
6102 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6103 parse_error_context
7f10c2e2 6104 (method_wfl,
b9f7e36c 6105 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6106 lang_printable_name (found, 0), t,
b9f7e36c
APB
6107 IDENTIFIER_POINTER
6108 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6109 free (t);
6110 }
e04a16fb 6111
7f10c2e2 6112 aflags = get_access_flags_from_decl (found);
7f10c2e2 6113
e04a16fb
AG
6114 /* Can't override final. Can't override static. */
6115 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6116 {
6117 /* Static *can* override static */
6118 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6119 continue;
6120 parse_error_context
6121 (method_wfl,
6122 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6123 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6124 lang_printable_name (found, 0),
e04a16fb
AG
6125 (METHOD_FINAL (found) ? "final" : "static"),
6126 IDENTIFIER_POINTER
6127 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6128 continue;
6129 }
7f10c2e2 6130
e04a16fb
AG
6131 /* Static method can't override instance method. */
6132 if (METHOD_STATIC (method))
6133 {
6134 parse_error_context
6135 (method_wfl,
781b0558 6136 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6137 lang_printable_name (found, 0),
e04a16fb
AG
6138 IDENTIFIER_POINTER
6139 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6140 continue;
6141 }
5e942c50 6142
5e942c50
APB
6143 /* - Overriding/hiding public must be public
6144 - Overriding/hiding protected must be protected or public
6145 - If the overriden or hidden method has default (package)
6146 access, then the overriding or hiding method must not be
614eaae0
APB
6147 private; otherwise, a compile-time error occurs. If
6148 `found' belongs to an interface, things have been already
6149 taken care of. */
6150 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6151 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6152 || (METHOD_PROTECTED (found)
6153 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6154 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6155 && METHOD_PRIVATE (method))))
e04a16fb
AG
6156 {
6157 parse_error_context
6158 (method_wfl,
781b0558 6159 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6160 (METHOD_PUBLIC (method) ? "public" :
6161 (METHOD_PRIVATE (method) ? "private" : "protected")),
6162 IDENTIFIER_POINTER (DECL_NAME
6163 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6164 continue;
6165 }
6166
b9f7e36c
APB
6167 /* Overriding methods must have compatible `throws' clauses on checked
6168 exceptions, if any */
6169 check_throws_clauses (method, method_wfl, found);
6170
e04a16fb
AG
6171 /* Inheriting multiple methods with the same signature. FIXME */
6172 }
6173
23a79c61
APB
6174 if (!TYPE_NVIRTUALS (class))
6175 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6176
d77613be
APB
6177 /* Search for inherited abstract method not yet implemented in this
6178 class. */
6179 java_check_abstract_method_definitions (class_decl);
6180
22eed1e6 6181 if (!saw_constructor)
400500c4 6182 abort ();
e04a16fb
AG
6183}
6184
b9f7e36c
APB
6185/* Return a non zero value if the `throws' clause of METHOD (if any)
6186 is incompatible with the `throws' clause of FOUND (if any). */
6187
6188static void
6189check_throws_clauses (method, method_wfl, found)
6190 tree method, method_wfl, found;
6191{
6192 tree mthrows, fthrows;
6193
c877974e
APB
6194 /* Can't check these things with class loaded from bytecode. FIXME */
6195 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6196 return;
6197
b9f7e36c
APB
6198 for (mthrows = DECL_FUNCTION_THROWS (method);
6199 mthrows; mthrows = TREE_CHAIN (mthrows))
6200 {
6201 /* We don't verify unchecked expressions */
c877974e 6202 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6203 continue;
6204 /* Checked expression must be compatible */
6205 for (fthrows = DECL_FUNCTION_THROWS (found);
6206 fthrows; fthrows = TREE_CHAIN (fthrows))
6207 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6208 break;
6209 if (!fthrows)
6210 {
6211 parse_error_context
781b0558 6212 (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 6213 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6214 lang_printable_name (found, 0),
b9f7e36c
APB
6215 IDENTIFIER_POINTER
6216 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6217 }
6218 }
6219}
6220
e04a16fb
AG
6221/* Check abstract method of interface INTERFACE */
6222
6223static void
5e942c50
APB
6224java_check_abstract_methods (interface_decl)
6225 tree interface_decl;
e04a16fb
AG
6226{
6227 int i, n;
6228 tree method, basetype_vec, found;
5e942c50 6229 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6230
6231 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6232 {
e04a16fb
AG
6233 /* 2- Check for double definition inside the defining interface */
6234 if (check_method_redefinition (interface, method))
6235 continue;
6236
6237 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6238 the thrown exceptions (FIXME) */
e04a16fb
AG
6239 found = lookup_java_interface_method2 (interface, method);
6240 if (found)
6241 {
5e942c50 6242 char *t;
c2e3db92 6243 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6244 parse_error_context
c4faeb92 6245 (DECL_FUNCTION_WFL (found),
5e942c50 6246 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6247 lang_printable_name (found, 0), t,
b9f7e36c
APB
6248 IDENTIFIER_POINTER
6249 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6250 free (t);
c63b98cd 6251 continue;
e04a16fb
AG
6252 }
6253 }
6254
6255 /* 4- Inherited methods can't differ by their returned types */
6256 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6257 return;
6258 n = TREE_VEC_LENGTH (basetype_vec);
6259 for (i = 0; i < n; i++)
6260 {
6261 tree sub_interface_method, sub_interface;
6262 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6263 if (!vec_elt)
6264 continue;
6265 sub_interface = BINFO_TYPE (vec_elt);
6266 for (sub_interface_method = TYPE_METHODS (sub_interface);
6267 sub_interface_method;
6268 sub_interface_method = TREE_CHAIN (sub_interface_method))
6269 {
6270 found = lookup_java_interface_method2 (interface,
6271 sub_interface_method);
6272 if (found && (found != sub_interface_method))
5e942c50 6273 {
5e942c50
APB
6274 parse_error_context
6275 (lookup_cl (sub_interface_method),
781b0558 6276 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6277 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6278 lang_printable_name (found, 0),
6279 IDENTIFIER_POINTER
6280 (DECL_NAME (TYPE_NAME
6281 (DECL_CONTEXT (sub_interface_method)))),
6282 IDENTIFIER_POINTER
6283 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5e942c50 6284 }
e04a16fb
AG
6285 }
6286 }
6287}
6288
e04a16fb
AG
6289/* Lookup methods in interfaces using their name and partial
6290 signature. Return a matching method only if their types differ. */
6291
6292static tree
6293lookup_java_interface_method2 (class, method_decl)
6294 tree class, method_decl;
6295{
6296 int i, n;
6297 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6298
6299 if (!basetype_vec)
6300 return NULL_TREE;
6301
6302 n = TREE_VEC_LENGTH (basetype_vec);
6303 for (i = 0; i < n; i++)
6304 {
6305 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6306 if ((BINFO_TYPE (vec_elt) != object_type_node)
6307 && (to_return =
6308 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6309 return to_return;
6310 }
6311 for (i = 0; i < n; i++)
6312 {
6313 to_return = lookup_java_interface_method2
6314 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6315 if (to_return)
6316 return to_return;
6317 }
6318
6319 return NULL_TREE;
6320}
6321
6322/* Lookup method using their name and partial signature. Return a
6323 matching method only if their types differ. */
6324
6325static tree
6326lookup_java_method2 (clas, method_decl, do_interface)
6327 tree clas, method_decl;
6328 int do_interface;
6329{
5e942c50
APB
6330 tree method, method_signature, method_name, method_type, name;
6331
e04a16fb 6332 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6333 name = DECL_NAME (method_decl);
6334 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6335 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6336 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6337
6338 while (clas != NULL_TREE)
6339 {
6340 for (method = TYPE_METHODS (clas);
6341 method != NULL_TREE; method = TREE_CHAIN (method))
6342 {
6343 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6344 tree name = DECL_NAME (method);
6345 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6346 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6347 && method_sig == method_signature
6348 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6349 return method;
e04a16fb
AG
6350 }
6351 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6352 }
6353 return NULL_TREE;
6354}
6355
f441f671
APB
6356/* Return the line that matches DECL line number, and try its best to
6357 position the column number. Used during error reports. */
e04a16fb
AG
6358
6359static tree
6360lookup_cl (decl)
6361 tree decl;
6362{
6363 static tree cl = NULL_TREE;
f441f671 6364 char *line, *found;
e04a16fb
AG
6365
6366 if (!decl)
6367 return NULL_TREE;
6368
6369 if (cl == NULL_TREE)
19e223db
MM
6370 {
6371 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6372 ggc_add_tree_root (&cl, 1);
6373 }
e04a16fb
AG
6374
6375 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6376 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6377
dba41d30 6378 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6379 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6380
6381 found = strstr ((const char *)line,
6382 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6383 if (found)
6384 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6385
e04a16fb
AG
6386 return cl;
6387}
6388
6389/* Look for a simple name in the single-type import list */
6390
6391static tree
6392find_name_in_single_imports (name)
6393 tree name;
6394{
6395 tree node;
6396
6397 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6398 if (TREE_VALUE (node) == name)
6399 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6400
6401 return NULL_TREE;
6402}
6403
6404/* Process all single-type import. */
6405
6406static int
6407process_imports ()
6408{
6409 tree import;
6410 int error_found;
6411
6412 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6413 {
6414 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
1ebb5e73
APB
6415 char *original_name;
6416
6417 obstack_grow0 (&temporary_obstack,
6418 IDENTIFIER_POINTER (to_be_found),
6419 IDENTIFIER_LENGTH (to_be_found));
6420 original_name = obstack_finish (&temporary_obstack);
e04a16fb
AG
6421
6422 /* Don't load twice something already defined. */
6423 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6424 continue;
02ae6e2e
APB
6425
6426 while (1)
6427 {
6428 tree left;
6429
6430 QUALIFIED_P (to_be_found) = 1;
6431 load_class (to_be_found, 0);
6432 error_found =
6433 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6434
6435 /* We found it, we can bail out */
6436 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6437 break;
6438
6439 /* We haven't found it. Maybe we're trying to access an
6440 inner class. The only way for us to know is to try again
6441 after having dropped a qualifier. If we can't break it further,
6442 we have an error. */
6443 if (breakdown_qualified (&left, NULL, to_be_found))
6444 break;
6445
6446 to_be_found = left;
6447 }
e04a16fb
AG
6448 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6449 {
6450 parse_error_context (TREE_PURPOSE (import),
6451 "Class or interface `%s' not found in import",
1ebb5e73
APB
6452 original_name);
6453 error_found = 1;
e04a16fb 6454 }
1ebb5e73
APB
6455
6456 obstack_free (&temporary_obstack, original_name);
e04a16fb
AG
6457 if (error_found)
6458 return 1;
6459 }
6460 return 0;
6461}
6462
9a7ab4b3
APB
6463/* Possibly find and mark a class imported by a single-type import
6464 statement. */
e04a16fb 6465
9a7ab4b3 6466static void
e04a16fb
AG
6467find_in_imports (class_type)
6468 tree class_type;
6469{
6470 tree import;
6471
6472 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6473 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6474 {
6475 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6476 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6477 }
e04a16fb
AG
6478}
6479
e04a16fb 6480static int
63a212ed 6481note_possible_classname (name, len)
49f48c71 6482 const char *name;
63a212ed 6483 int len;
e04a16fb 6484{
63a212ed
PB
6485 tree node;
6486 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6487 len = len - 5;
6488 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6489 len = len - 6;
e04a16fb 6490 else
63a212ed
PB
6491 return 0;
6492 node = ident_subst (name, len, "", '/', '.', "");
6493 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6494 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6495 return 1;
e04a16fb
AG
6496}
6497
6498/* Read a import directory, gathering potential match for further type
6499 references. Indifferently reads a filesystem or a ZIP archive
6500 directory. */
6501
6502static void
6503read_import_dir (wfl)
6504 tree wfl;
6505{
63a212ed 6506 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6507 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6508 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6509 DIR *dirp = NULL;
d8fccff5 6510 JCF *saved_jcf = current_jcf;
e04a16fb 6511
63a212ed
PB
6512 int found = 0;
6513 int k;
6514 void *entry;
6515 struct buffer filename[1];
6516
6517
6518 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6519 return;
6520 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6521
6522 BUFFER_INIT (filename);
6523 buffer_grow (filename, package_length + 100);
6524
6525 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6526 {
49f48c71 6527 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6528 int entry_length = strlen (entry_name);
6529 if (jcf_path_is_zipfile (entry))
6530 {
6531 ZipFile *zipf;
6532 buffer_grow (filename, entry_length);
6533 memcpy (filename->data, entry_name, entry_length - 1);
6534 filename->data[entry_length-1] = '\0';
6535 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6536 if (zipf == NULL)
6537 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6538 else
6539 {
6540 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6541 BUFFER_RESET (filename);
6542 for (k = 0; k < package_length; k++)
6543 {
6544 char ch = package_name[k];
6545 *filename->ptr++ = ch == '.' ? '/' : ch;
6546 }
6547 *filename->ptr++ = '/';
6548
345137c7 6549 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6550 {
49f48c71 6551 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6552 int current_entry_len = zipd->filename_length;
6553
345137c7
TT
6554 if (current_entry_len >= BUFFER_LENGTH (filename)
6555 && strncmp (filename->data, current_entry,
6556 BUFFER_LENGTH (filename)) != 0)
63a212ed 6557 continue;
345137c7 6558 found |= note_possible_classname (current_entry,
63a212ed
PB
6559 current_entry_len);
6560 }
6561 }
6562 }
6563 else
6564 {
6565 BUFFER_RESET (filename);
6566 buffer_grow (filename, entry_length + package_length + 4);
6567 strcpy (filename->data, entry_name);
6568 filename->ptr = filename->data + entry_length;
6569 for (k = 0; k < package_length; k++)
6570 {
6571 char ch = package_name[k];
6572 *filename->ptr++ = ch == '.' ? '/' : ch;
6573 }
6574 *filename->ptr = '\0';
6575
6576 dirp = opendir (filename->data);
6577 if (dirp == NULL)
6578 continue;
6579 *filename->ptr++ = '/';
6580 for (;;)
6581 {
63a212ed 6582 int len;
49f48c71 6583 const char *d_name;
63a212ed
PB
6584 struct dirent *direntp = readdir (dirp);
6585 if (!direntp)
6586 break;
6587 d_name = direntp->d_name;
6588 len = strlen (direntp->d_name);
6589 buffer_grow (filename, len+1);
6590 strcpy (filename->ptr, d_name);
345137c7 6591 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6592 package_length+len+1);
6593 }
6594 if (dirp)
6595 closedir (dirp);
6596 }
6597 }
e04a16fb 6598
63a212ed 6599 free (filename->data);
e04a16fb 6600
63a212ed
PB
6601 /* Here we should have a unified way of retrieving an entry, to be
6602 indexed. */
6603 if (!found)
e04a16fb
AG
6604 {
6605 static int first = 1;
6606 if (first)
6607 {
781b0558 6608 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6609 java_error_count++;
6610 first = 0;
6611 }
6612 else
63a212ed
PB
6613 parse_error_context (wfl, "Package `%s' not found in import",
6614 package_name);
e04a16fb
AG
6615 current_jcf = saved_jcf;
6616 return;
6617 }
e04a16fb
AG
6618 current_jcf = saved_jcf;
6619}
6620
6621/* Possibly find a type in the import on demands specified
6622 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6623 entire list, to detected potential double definitions. */
6624
6625static int
6626find_in_imports_on_demand (class_type)
6627 tree class_type;
6628{
ab3a6dd6 6629 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6630 int seen_once = -1;
ab3a6dd6 6631 tree cl = NULL_TREE;
e04a16fb
AG
6632
6633 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6634 {
49f48c71 6635 const char *id_name;
e04a16fb
AG
6636 obstack_grow (&temporary_obstack,
6637 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6638 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6639 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6640 obstack_grow0 (&temporary_obstack,
6641 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6642 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6643 id_name = obstack_finish (&temporary_obstack);
6644
6645 node = maybe_get_identifier (id_name);
6646 if (node && IS_A_CLASSFILE_NAME (node))
6647 {
6648 if (seen_once < 0)
6649 {
6650 cl = TREE_PURPOSE (import);
6651 seen_once = 1;
6652 node_to_use = node;
6653 }
6654 else
6655 {
6656 seen_once++;
6657 parse_error_context
1e12ab9b
APB
6658 (TREE_PURPOSE (import),
6659 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6660 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6661 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6662 }
6663 }
6664 }
6665
6666 if (seen_once == 1)
6667 {
6668 /* Setup lineno so that it refers to the line of the import (in
6669 case we parse a class file and encounter errors */
6670 tree decl;
6671 int saved_lineno = lineno;
6672 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6673 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6674 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6675 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6676 /* If there is no DECL set for the class or if the class isn't
6677 loaded and not seen in source yet, the load */
6678 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6679 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
a648f4e4
BM
6680 {
6681 load_class (node_to_use, 0);
6682 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6683 }
e04a16fb 6684 lineno = saved_lineno;
a648f4e4
BM
6685 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6686 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6687 else
6688 /* 6.6.1: Inner classes are subject to member access rules. */
6689 return 0;
e04a16fb
AG
6690 }
6691 else
6692 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6693}
6694
9a7ab4b3
APB
6695/* Add package NAME to the list of package encountered so far. To
6696 speed up class lookup in do_resolve_class, we make sure a
6697 particular package is added only once. */
6698
6699static void
6700register_package (name)
6701 tree name;
6702{
6703 static struct hash_table _pht, *pht = NULL;
6704
6705 if (!pht)
6706 {
6707 hash_table_init (&_pht, hash_newfunc,
6708 java_hash_hash_tree_node, java_hash_compare_tree_node);
6709 pht = &_pht;
6710 }
6711
6712 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6713 {
6714 package_list = chainon (package_list, build_tree_list (name, NULL));
6715 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6716 }
6717}
6718
5e942c50
APB
6719static tree
6720resolve_package (pkg, next)
6721 tree pkg, *next;
6722{
c2952b01 6723 tree current, acc;
5e942c50 6724 tree type_name = NULL_TREE;
49f48c71 6725 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6726
6727 /* The trick is to determine when the package name stops and were
6728 the name of something contained in the package starts. Then we
6729 return a fully qualified name of what we want to get. */
6730
6731 /* Do a quick search on well known package names */
6732 if (!strncmp (name, "java.lang.reflect", 17))
6733 {
6734 *next =
6735 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6736 type_name = lookup_package_type (name, 17);
6737 }
6738 else if (!strncmp (name, "java.lang", 9))
6739 {
6740 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6741 type_name = lookup_package_type (name, 9);
6742 }
5e942c50 6743
2c56429a
APB
6744 /* If we found something here, return */
6745 if (type_name)
6746 return type_name;
6747
6748 *next = EXPR_WFL_QUALIFICATION (pkg);
6749
c2952b01
APB
6750 /* Try to progressively construct a type name */
6751 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6752 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6753 current; current = TREE_CHAIN (current))
6754 {
6755 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6756 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6757 {
6758 type_name = acc;
6b48deee
APB
6759 /* resolve_package should be used in a loop, hence we
6760 point at this one to naturally process the next one at
6761 the next iteration. */
6762 *next = current;
c2952b01
APB
6763 break;
6764 }
6765 }
2c56429a
APB
6766 return type_name;
6767}
6768
5e942c50
APB
6769static tree
6770lookup_package_type (name, from)
49f48c71 6771 const char *name;
5e942c50
APB
6772 int from;
6773{
6774 char subname [128];
49f48c71 6775 const char *sub = &name[from+1];
5e942c50
APB
6776 while (*sub != '.' && *sub)
6777 sub++;
6778 strncpy (subname, name, sub-name);
6779 subname [sub-name] = '\0';
6780 return get_identifier (subname);
6781}
6782
a648f4e4
BM
6783/* Check accessibility of inner classes according to member access rules.
6784 DECL is the inner class, ENCLOSING_DECL is the class from which the
6785 access is being attempted. */
6786
cf1748bf 6787static void
4dbf4496
APB
6788check_inner_class_access (decl, enclosing_decl, cl)
6789 tree decl, enclosing_decl, cl;
cf1748bf 6790{
a648f4e4 6791 const char *access;
064a552c 6792 tree enclosing_decl_type;
4dbf4496 6793
cf1748bf 6794 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6795 as a result of processing a JDEP crafted by source_start_java_method
6796 for the purpose of patching its parm decl. But the error would
6797 have been already trapped when fixing the method's signature.
6798 DECL can also be NULL in case of earlier errors. */
6799 if (!decl || !cl)
cf1748bf
APB
6800 return;
6801
064a552c 6802 enclosing_decl_type = TREE_TYPE (enclosing_decl);
a648f4e4 6803
4dbf4496 6804 if (CLASS_PRIVATE (decl))
a648f4e4
BM
6805 {
6806 /* Access is permitted only within the body of the top-level
6807 class in which DECL is declared. */
6808 tree top_level = decl;
6809 while (DECL_CONTEXT (top_level))
6810 top_level = DECL_CONTEXT (top_level);
6811 while (DECL_CONTEXT (enclosing_decl))
6812 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6813 if (top_level == enclosing_decl)
6814 return;
6815 access = "private";
6816 }
6817 else if (CLASS_PROTECTED (decl))
6818 {
6819 tree decl_context;
6820 /* Access is permitted from within the same package... */
6821 if (in_same_package (decl, enclosing_decl))
6822 return;
4dbf4496 6823
a648f4e4
BM
6824 /* ... or from within the body of a subtype of the context in which
6825 DECL is declared. */
6826 decl_context = DECL_CONTEXT (decl);
6827 while (enclosing_decl)
6828 {
6829 if (CLASS_INTERFACE (decl))
6830 {
6831 if (interface_of_p (TREE_TYPE (decl_context),
6832 enclosing_decl_type))
6833 return;
6834 }
6835 else
6836 {
6837 /* Eww. The order of the arguments is different!! */
6838 if (inherits_from_p (enclosing_decl_type,
6839 TREE_TYPE (decl_context)))
6840 return;
6841 }
6842 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6843 }
6844 access = "protected";
6845 }
6846 else if (! CLASS_PUBLIC (decl))
6847 {
6848 /* Access is permitted only from within the same package as DECL. */
6849 if (in_same_package (decl, enclosing_decl))
6850 return;
6851 access = "non-public";
6852 }
6853 else
6854 /* Class is public. */
4dbf4496
APB
6855 return;
6856
a648f4e4 6857 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
cf1748bf 6858 (CLASS_INTERFACE (decl) ? "interface" : "class"),
a648f4e4 6859 lang_printable_name (decl, 0), access);
cf1748bf
APB
6860}
6861
a648f4e4
BM
6862/* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6863 package, it must be PUBLIC. Return 0 if no access violations were found,
6864 1 otherwise. */
e04a16fb
AG
6865
6866static int
6867check_pkg_class_access (class_name, cl)
6868 tree class_name;
6869 tree cl;
6870{
6871 tree type;
e04a16fb 6872
a648f4e4 6873 if (!IDENTIFIER_CLASS_VALUE (class_name))
e04a16fb
AG
6874 return 0;
6875
6876 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6877 return 0;
6878
6879 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6880 {
e28cd97b
APB
6881 /* Access to a private class within the same package is
6882 allowed. */
6883 tree l, r;
6884 breakdown_qualified (&l, &r, class_name);
a648f4e4
BM
6885 if (!QUALIFIED_P (class_name) && !ctxp->package)
6886 /* Both in the empty package. */
6887 return 0;
e28cd97b 6888 if (l == ctxp->package)
a648f4e4 6889 /* Both in the same package. */
e28cd97b
APB
6890 return 0;
6891
e04a16fb 6892 parse_error_context
781b0558 6893 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6894 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6895 IDENTIFIER_POINTER (class_name));
6896 return 1;
6897 }
6898 return 0;
6899}
6900
6901/* Local variable declaration. */
6902
6903static void
6904declare_local_variables (modifier, type, vlist)
6905 int modifier;
6906 tree type;
6907 tree vlist;
6908{
c583dd46
APB
6909 tree decl, current, saved_type;
6910 tree type_wfl = NULL_TREE;
e04a16fb 6911 int must_chain = 0;
c2952b01 6912 int final_p = 0;
e04a16fb 6913
2aa11e97 6914 /* Push a new block if statements were seen between the last time we
e04a16fb 6915 pushed a block and now. Keep a cound of block to close */
f099f336 6916 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6917 {
f099f336 6918 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6919 tree b = enter_block ();
f099f336 6920 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6921 }
6922
6923 if (modifier)
6924 {
6925 int i;
6926 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6927 if (modifier == ACC_FINAL)
c2952b01 6928 final_p = 1;
c877974e
APB
6929 else
6930 {
6931 parse_error_context
6932 (ctxp->modifier_ctx [i],
6933 "Only `final' is allowed as a local variables modifier");
6934 return;
6935 }
e04a16fb
AG
6936 }
6937
c583dd46
APB
6938 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6939 hold the TYPE value if a new incomplete has to be created (as
6940 opposed to being found already existing and reused). */
6941 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6942
6943 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6944 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6945
6946 /* Go through all the declared variables */
6947 for (current = vlist, saved_type = type; current;
6948 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6949 {
c877974e 6950 tree other, real_type;
e04a16fb
AG
6951 tree wfl = TREE_PURPOSE (current);
6952 tree name = EXPR_WFL_NODE (wfl);
6953 tree init = TREE_VALUE (current);
e04a16fb 6954
c583dd46
APB
6955 /* Process NAME, as it may specify extra dimension(s) for it */
6956 type = build_array_from_name (type, type_wfl, name, &name);
6957
6958 /* Variable redefinition check */
6959 if ((other = lookup_name_in_blocks (name)))
6960 {
6961 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6962 DECL_SOURCE_LINE (other));
6963 continue;
6964 }
6965
6966 /* Type adjustment. We may have just readjusted TYPE because
6967 the variable specified more dimensions. Make sure we have
6968 a reference if we can and don't have one already. */
1886c9d8 6969 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6970
6971 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6972 /* Never layout this decl. This will be done when its scope
6973 will be entered */
c877974e 6974 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 6975 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 6976 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6977 BLOCK_CHAIN_DECL (decl);
6978
d4370213
APB
6979 /* If doing xreferencing, replace the line number with the WFL
6980 compound value */
6981 if (flag_emit_xref)
6982 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6983
e04a16fb
AG
6984 /* Don't try to use an INIT statement when an error was found */
6985 if (init && java_error_count)
6986 init = NULL_TREE;
c583dd46
APB
6987
6988 /* Add the initialization function to the current function's code */
6989 if (init)
e04a16fb 6990 {
c583dd46
APB
6991 /* Name might have been readjusted */
6992 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6993 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6994 java_method_add_stmt (current_function_decl,
6995 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6996 init));
6997 }
6998
6999 /* Setup dependency the type of the decl */
7000 if (must_chain)
7001 {
7002 jdep *dep;
7003 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7004 dep = CLASSD_LAST (ctxp->classd_list);
7005 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7006 }
7007 }
7008 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7009}
7010
7011/* Called during parsing. Build decls from argument list. */
7012
7013static void
7014source_start_java_method (fndecl)
7015 tree fndecl;
7016{
7017 tree tem;
7018 tree parm_decl;
7019 int i;
7020
79d13333
APB
7021 if (!fndecl)
7022 return;
7023
e04a16fb
AG
7024 current_function_decl = fndecl;
7025
7026 /* New scope for the function */
7027 enter_block ();
7028 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7029 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7030 {
7031 tree type = TREE_VALUE (tem);
7032 tree name = TREE_PURPOSE (tem);
7033
23a79c61
APB
7034 /* If type is incomplete. Create an incomplete decl and ask for
7035 the decl to be patched later */
e04a16fb
AG
7036 if (INCOMPLETE_TYPE_P (type))
7037 {
7038 jdep *jdep;
c877974e
APB
7039 tree real_type = GET_REAL_TYPE (type);
7040 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7041 type = obtain_incomplete_type (type);
e04a16fb
AG
7042 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7043 jdep = CLASSD_LAST (ctxp->classd_list);
7044 JDEP_MISC (jdep) = name;
7045 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7046 }
7047 else
7048 parm_decl = build_decl (PARM_DECL, name, type);
7049
c2952b01
APB
7050 /* Remember if a local variable was declared final (via its
7051 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7052 if (ARG_FINAL_P (tem))
c7303e41
APB
7053 {
7054 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7055 LOCAL_FINAL (parm_decl) = 1;
7056 }
c2952b01 7057
e04a16fb
AG
7058 BLOCK_CHAIN_DECL (parm_decl);
7059 }
7060 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7061 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7062 nreverse (tem);
7063 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7064 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7065}
7066
22eed1e6
APB
7067/* Called during parsing. Creates an artificial method declaration. */
7068
7069static tree
7070create_artificial_method (class, flags, type, name, args)
7071 tree class;
7072 int flags;
7073 tree type, name, args;
7074{
22eed1e6
APB
7075 tree mdecl;
7076
c2952b01 7077 java_parser_context_save_global ();
22eed1e6
APB
7078 lineno = 0;
7079 mdecl = make_node (FUNCTION_TYPE);
7080 TREE_TYPE (mdecl) = type;
7081 TYPE_ARG_TYPES (mdecl) = args;
7082 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7083 java_parser_context_restore_global ();
22eed1e6
APB
7084 DECL_ARTIFICIAL (mdecl) = 1;
7085 return mdecl;
7086}
7087
7088/* Starts the body if an artifical method. */
7089
7090static void
7091start_artificial_method_body (mdecl)
7092 tree mdecl;
7093{
7094 DECL_SOURCE_LINE (mdecl) = 1;
7095 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7096 source_start_java_method (mdecl);
7097 enter_block ();
7098}
7099
7100static void
7101end_artificial_method_body (mdecl)
7102 tree mdecl;
7103{
1e0cdc10
APB
7104 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7105 It has to be evaluated first. (if mdecl is current_function_decl,
7106 we have an undefined behavior if no temporary variable is used.) */
7107 tree b = exit_block ();
7108 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7109 exit_block ();
7110}
7111
e04a16fb
AG
7112/* Called during expansion. Push decls formerly built from argument
7113 list so they're usable during expansion. */
7114
7115static void
7116expand_start_java_method (fndecl)
7117 tree fndecl;
7118{
7119 tree tem, *ptr;
e04a16fb 7120
e04a16fb
AG
7121 current_function_decl = fndecl;
7122
c2952b01
APB
7123 if (! quiet_flag)
7124 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7125 announce_function (fndecl);
c2952b01
APB
7126 if (! quiet_flag)
7127 fprintf (stderr, "]");
7128
7129 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7130 ptr = &DECL_ARGUMENTS (fndecl);
7131 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7132 while (tem)
7133 {
7134 tree next = TREE_CHAIN (tem);
b67d701b 7135 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7136 if (PROMOTE_PROTOTYPES
7137 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7138 && INTEGRAL_TYPE_P (type))
7139 type = integer_type_node;
b67d701b 7140 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7141 layout_decl (tem, 0);
7142 pushdecl (tem);
e04a16fb
AG
7143 *ptr = tem;
7144 ptr = &TREE_CHAIN (tem);
7145 tem = next;
7146 }
7147 *ptr = NULL_TREE;
7148 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7149 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7150}
7151
7152/* Terminate a function and expand its body. */
7153
7154static void
7155source_end_java_method ()
7156{
7157 tree fndecl = current_function_decl;
138657ec 7158 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7159
79d13333
APB
7160 if (!fndecl)
7161 return;
7162
e04a16fb
AG
7163 java_parser_context_save_global ();
7164 lineno = ctxp->last_ccb_indent1;
7165
b67d701b
PB
7166 /* Set EH language codes */
7167 java_set_exception_lang_code ();
7168
5423609c
APB
7169 /* Turn function bodies with only a NOP expr null, so they don't get
7170 generated at all and we won't get warnings when using the -W
7171 -Wall flags. */
7172 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7173 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7174
e04a16fb
AG
7175 /* Generate function's code */
7176 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7177 && ! flag_emit_class_files
7178 && ! flag_emit_xref)
e04a16fb
AG
7179 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7180
7181 /* pop out of its parameters */
7182 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7183 poplevel (1, 0, 1);
7184 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7185
7186 /* Generate rtl for function exit. */
e8fc7396 7187 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7188 {
7189 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7190 /* Emit catch-finally clauses */
7191 emit_handlers ();
e04a16fb
AG
7192 expand_function_end (input_filename, lineno, 0);
7193
138657ec
AH
7194 /* FIXME: If the current method contains any exception handlers,
7195 force asynchronous_exceptions: this is necessary because signal
7196 handlers in libjava may throw exceptions. This is far from being
7197 a perfect solution, but it's better than doing nothing at all.*/
7198 if (catch_clauses)
7199 asynchronous_exceptions = 1;
7200
e04a16fb
AG
7201 /* Run the optimizers and output assembler code for this function. */
7202 rest_of_compilation (fndecl);
7203 }
7204
7205 current_function_decl = NULL_TREE;
e04a16fb 7206 java_parser_context_restore_global ();
138657ec 7207 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7208}
7209
7210/* Record EXPR in the current function block. Complements compound
7211 expression second operand if necessary. */
7212
7213tree
7214java_method_add_stmt (fndecl, expr)
7215 tree fndecl, expr;
7216{
b771925e
APB
7217 if (!GET_CURRENT_BLOCK (fndecl))
7218 return NULL_TREE;
f099f336 7219 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7220}
e04a16fb 7221
b67d701b
PB
7222static tree
7223add_stmt_to_block (b, type, stmt)
7224 tree b, type, stmt;
7225{
7226 tree body = BLOCK_EXPR_BODY (b), c;
7227
e04a16fb
AG
7228 if (java_error_count)
7229 return body;
b67d701b
PB
7230
7231 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7232 return body;
7233
b67d701b
PB
7234 BLOCK_EXPR_BODY (b) = c;
7235 TREE_SIDE_EFFECTS (c) = 1;
7236 return c;
e04a16fb
AG
7237}
7238
7239/* Add STMT to EXISTING if possible, otherwise create a new
7240 COMPOUND_EXPR and add STMT to it. */
7241
7242static tree
7243add_stmt_to_compound (existing, type, stmt)
7244 tree existing, type, stmt;
7245{
15fdcfe9
PB
7246 if (existing)
7247 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7248 else
15fdcfe9 7249 return stmt;
e04a16fb
AG
7250}
7251
1886c9d8
APB
7252void java_layout_seen_class_methods ()
7253{
7254 tree previous_list = all_class_list;
7255 tree end = NULL_TREE;
7256 tree current;
7257
7258 while (1)
7259 {
7260 for (current = previous_list;
7261 current != end; current = TREE_CHAIN (current))
7262 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7263
7264 if (previous_list != all_class_list)
7265 {
7266 end = previous_list;
7267 previous_list = all_class_list;
7268 }
7269 else
7270 break;
7271 }
7272}
7273
e04a16fb 7274void
c2952b01 7275java_reorder_fields ()
e04a16fb 7276{
c2952b01 7277 static tree stop_reordering = NULL_TREE;
19e223db 7278 static int initialized_p;
c2952b01 7279 tree current;
19e223db
MM
7280
7281 /* Register STOP_REORDERING with the garbage collector. */
7282 if (!initialized_p)
7283 {
7284 ggc_add_tree_root (&stop_reordering, 1);
7285 initialized_p = 1;
7286 }
7287
5e942c50 7288 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7289 {
5e942c50 7290 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7291
c2952b01
APB
7292 if (current_class == stop_reordering)
7293 break;
7294
c877974e
APB
7295 /* Reverse the fields, but leave the dummy field in front.
7296 Fields are already ordered for Object and Class */
7297 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7298 && current_class != class_type_node)
7299 {
23a79c61
APB
7300 /* If the dummy field is there, reverse the right fields and
7301 just layout the type for proper fields offset */
c877974e
APB
7302 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7303 {
7304 tree fields = TYPE_FIELDS (current_class);
7305 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7306 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7307 }
23a79c61
APB
7308 /* We don't have a dummy field, we need to layout the class,
7309 after having reversed the fields */
c877974e
APB
7310 else
7311 {
7312 TYPE_FIELDS (current_class) =
7313 nreverse (TYPE_FIELDS (current_class));
7314 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7315 }
7316 }
c2952b01
APB
7317 }
7318 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7319}
7320
7321/* Layout the methods of all classes loaded in one way on an
7322 other. Check methods of source parsed classes. Then reorder the
7323 fields and layout the classes or the type of all source parsed
7324 classes */
7325
7326void
7327java_layout_classes ()
7328{
7329 tree current;
7330 int save_error_count = java_error_count;
7331
7332 /* Layout the methods of all classes seen so far */
7333 java_layout_seen_class_methods ();
7334 java_parse_abort_on_error ();
7335 all_class_list = NULL_TREE;
7336
7337 /* Then check the methods of all parsed classes */
7338 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7339 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7340 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7341 java_parse_abort_on_error ();
7342
7343 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7344 {
7345 current_class = TREE_TYPE (TREE_VALUE (current));
7346 layout_class (current_class);
5e942c50 7347
c877974e
APB
7348 /* From now on, the class is considered completely loaded */
7349 CLASS_LOADED_P (current_class) = 1;
7350
5e942c50
APB
7351 /* Error reported by the caller */
7352 if (java_error_count)
7353 return;
e04a16fb 7354 }
23a79c61
APB
7355
7356 /* We might have reloaded classes durign the process of laying out
7357 classes for code generation. We must layout the methods of those
7358 late additions, as constructor checks might use them */
1886c9d8 7359 java_layout_seen_class_methods ();
23a79c61 7360 java_parse_abort_on_error ();
e04a16fb
AG
7361}
7362
c2952b01
APB
7363/* Expand methods in the current set of classes rememebered for
7364 generation. */
e04a16fb 7365
49f48c71 7366static void
c2952b01 7367java_complete_expand_classes ()
e04a16fb
AG
7368{
7369 tree current;
ce6e9147
APB
7370
7371 do_not_fold = flag_emit_xref;
c2952b01 7372
e04a16fb 7373 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7374 if (!INNER_CLASS_DECL_P (current))
7375 java_complete_expand_class (current);
7376}
e04a16fb 7377
c2952b01
APB
7378/* Expand the methods found in OUTER, starting first by OUTER's inner
7379 classes, if any. */
e04a16fb 7380
c2952b01
APB
7381static void
7382java_complete_expand_class (outer)
7383 tree outer;
7384{
7385 tree inner_list;
e04a16fb 7386
c2952b01 7387 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7388
c2952b01
APB
7389 /* We need to go after all inner classes and start expanding them,
7390 starting with most nested ones. We have to do that because nested
7391 classes might add functions to outer classes */
e04a16fb 7392
c2952b01
APB
7393 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7394 inner_list; inner_list = TREE_CHAIN (inner_list))
7395 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7396
c2952b01
APB
7397 java_complete_expand_methods (outer);
7398 set_nested_class_simple_name_value (outer, 0); /* Reset */
7399}
7400
7401/* Expand methods registered in CLASS_DECL. The general idea is that
7402 we expand regular methods first. This allows us get an estimate on
7403 how outer context local alias fields are really used so we can add
7404 to the constructor just enough code to initialize them properly (it
c00f0fb2 7405 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7406 constructors and then <clinit>. */
7407
7408static void
7409java_complete_expand_methods (class_decl)
7410 tree class_decl;
7411{
7412 tree clinit, finit, decl, first_decl;
7413
7414 current_class = TREE_TYPE (class_decl);
7415
c7303e41
APB
7416 /* Find whether the class has final variables */
7417 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7418 if (FIELD_FINAL (decl))
7419 {
7420 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7421 break;
7422 }
7423
c2952b01
APB
7424 /* Initialize a new constant pool */
7425 init_outgoing_cpool ();
7426
7427 /* Pre-expand <clinit> to figure whether we really need it or
7428 not. If we do need it, we pre-expand the static fields so they're
7429 ready to be used somewhere else. <clinit> will be fully expanded
7430 after we processed the constructors. */
7431 first_decl = TYPE_METHODS (current_class);
7432 clinit = maybe_generate_pre_expand_clinit (current_class);
7433
c00f0fb2 7434 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7435 try to use it.*/
7436 if (TYPE_FINIT_STMT_LIST (current_class))
7437 {
7438 finit = generate_finit (current_class);
7439 java_complete_expand_method (finit);
7440 }
7441
7442 /* Now do the constructors */
7443 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7444 {
7445 int no_body;
7446
7447 if (!DECL_CONSTRUCTOR_P (decl))
7448 continue;
7449
7450 no_body = !DECL_FUNCTION_BODY (decl);
7451 /* Don't generate debug info on line zero when expanding a
7452 generated constructor. */
7453 if (no_body)
7454 restore_line_number_status (1);
7455
c7303e41
APB
7456 /* Reset the final local variable assignment flags */
7457 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7458 reset_final_variable_local_assignment_flag (current_class);
7459
c2952b01 7460 java_complete_expand_method (decl);
c7303e41
APB
7461
7462 /* Check for missed out final variable assignment */
7463 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7464 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7465
7466 if (no_body)
7467 restore_line_number_status (0);
7468 }
7469
7470 /* First, do the ordinary methods. */
7471 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7472 {
7145d9fe
TT
7473 /* Skip abstract or native methods -- but do handle native
7474 methods when generating JNI stubs. */
7475 if (METHOD_ABSTRACT (decl)
7476 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7477 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7478 continue;
7145d9fe
TT
7479
7480 if (METHOD_NATIVE (decl))
7481 {
7482 tree body = build_jni_stub (decl);
7483 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7484 }
7485
c2952b01
APB
7486 java_complete_expand_method (decl);
7487 }
7488
7489 /* If there is indeed a <clinit>, fully expand it now */
7490 if (clinit)
7491 {
c7303e41
APB
7492 /* Reset the final local variable assignment flags */
7493 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7494 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7495 /* Prevent the use of `this' inside <clinit> */
7496 ctxp->explicit_constructor_p = 1;
7497 java_complete_expand_method (clinit);
7498 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7499 /* Check for missed out static final variable assignment */
7500 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7501 && !CLASS_INTERFACE (class_decl))
7502 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7503 }
c2952b01 7504
165f37bc
APB
7505 /* We might have generated a class$ that we now want to expand */
7506 if (TYPE_DOT_CLASS (current_class))
7507 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7508
c2952b01
APB
7509 /* Now verify constructor circularity (stop after the first one we
7510 prove wrong.) */
7511 if (!CLASS_INTERFACE (class_decl))
7512 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7513 if (DECL_CONSTRUCTOR_P (decl)
7514 && verify_constructor_circularity (decl, decl))
7515 break;
7516
c7303e41
APB
7517 /* Final check on the initialization of final variables. */
7518 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7519 {
7520 check_final_variable_global_assignment_flag (current_class);
7521 /* If we have an interface, check for uninitialized fields. */
7522 if (CLASS_INTERFACE (class_decl))
7523 check_static_final_variable_assignment_flag (current_class);
7524 }
7525
c2952b01
APB
7526 /* Save the constant pool. We'll need to restore it later. */
7527 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7528}
7529
c2952b01
APB
7530/* Attempt to create <clinit>. Pre-expand static fields so they can be
7531 safely used in some other methods/constructors. */
e920ebc9 7532
c2952b01
APB
7533static tree
7534maybe_generate_pre_expand_clinit (class_type)
7535 tree class_type;
e920ebc9 7536{
c2952b01
APB
7537 tree current, mdecl;
7538
7539 if (!TYPE_CLINIT_STMT_LIST (class_type))
7540 return NULL_TREE;
e920ebc9 7541
c2952b01
APB
7542 /* Go through all static fields and pre expand them */
7543 for (current = TYPE_FIELDS (class_type); current;
7544 current = TREE_CHAIN (current))
7545 if (FIELD_STATIC (current))
7546 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7547
7548 /* Then build the <clinit> method */
7549 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7550 clinit_identifier_node, end_params_node);
7551 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7552 mdecl, NULL_TREE);
7553 start_artificial_method_body (mdecl);
7554
7555 /* We process the list of assignment we produced as the result of
7556 the declaration of initialized static field and add them as
7557 statement to the <clinit> method. */
7558 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7559 current = TREE_CHAIN (current))
e920ebc9 7560 {
9a7ab4b3 7561 tree stmt = current;
c2952b01
APB
7562 /* We build the assignment expression that will initialize the
7563 field to its value. There are strict rules on static
7564 initializers (8.5). FIXME */
98a52c2c 7565 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7566 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7567 java_method_add_stmt (mdecl, stmt);
7568 }
e920ebc9 7569
c2952b01
APB
7570 end_artificial_method_body (mdecl);
7571
92d83515
APB
7572 /* Now we want to place <clinit> as the last method (because we need
7573 it at least for interface so that it doesn't interfere with the
7574 dispatch table based lookup. */
7575 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7576 {
92d83515
APB
7577 current = TREE_CHAIN (TYPE_METHODS (class_type));
7578 TYPE_METHODS (class_type) = current;
c2952b01
APB
7579
7580 while (TREE_CHAIN (current))
7581 current = TREE_CHAIN (current);
92d83515 7582
c2952b01
APB
7583 TREE_CHAIN (current) = mdecl;
7584 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7585 }
c2952b01
APB
7586
7587 return mdecl;
e920ebc9
APB
7588}
7589
dba41d30
APB
7590/* Analyzes a method body and look for something that isn't a
7591 MODIFY_EXPR. */
7592
7593static int
7594analyze_clinit_body (bbody)
7595 tree bbody;
7596{
7597 while (bbody)
7598 switch (TREE_CODE (bbody))
7599 {
7600 case BLOCK:
7601 bbody = BLOCK_EXPR_BODY (bbody);
7602 break;
7603
7604 case EXPR_WITH_FILE_LOCATION:
7605 bbody = EXPR_WFL_NODE (bbody);
7606 break;
7607
7608 case COMPOUND_EXPR:
7609 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7610 return 1;
7611 bbody = TREE_OPERAND (bbody, 1);
7612 break;
7613
7614 case MODIFY_EXPR:
7615 bbody = NULL_TREE;
7616 break;
7617
7618 default:
7619 bbody = NULL_TREE;
7620 return 1;
7621 }
7622 return 0;
7623}
7624
7625
92d83515
APB
7626/* See whether we could get rid of <clinit>. Criteria are: all static
7627 final fields have constant initial values and the body of <clinit>
7628 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7629
7630static int
7631maybe_yank_clinit (mdecl)
7632 tree mdecl;
7633{
7634 tree type, current;
7635 tree fbody, bbody;
99eaf8d4 7636 int found = 0;
92d83515
APB
7637
7638 if (!DECL_CLINIT_P (mdecl))
7639 return 0;
f0f3a777
APB
7640
7641 /* If the body isn't empty, then we keep <clinit>. Note that if
7642 we're emitting classfiles, this isn't enough not to rule it
7643 out. */
92d83515 7644 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7645 bbody = BLOCK_EXPR_BODY (fbody);
7646 if (bbody && bbody != error_mark_node)
92d83515 7647 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7648 else
7649 return 0;
f0f3a777 7650 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7651 return 0;
7652
7653 type = DECL_CONTEXT (mdecl);
7654 current = TYPE_FIELDS (type);
7655
7656 for (current = (current ? TREE_CHAIN (current) : current);
7657 current; current = TREE_CHAIN (current))
f0f3a777
APB
7658 {
7659 tree f_init;
7660
7661 /* We're not interested in non static field */
7662 if (!FIELD_STATIC (current))
7663 continue;
7664
7665 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7666 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7667 should generated an empty <clinit> so that SUID are computed
7668 correctly. */
7669 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7670 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7671 break;
7672
7673 f_init = DECL_INITIAL (current);
7674 /* If we're emitting native code, we want static final fields to
7675 have constant initializers. If we don't meet these
7676 conditions, we keep <clinit> */
7677 if (!flag_emit_class_files
7678 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7679 break;
7680 /* If we're emitting bytecode, we want static fields to have
7681 constant initializers or no initializer. If we don't meet
7682 these conditions, we keep <clinit> */
7683 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7684 break;
7685 }
92d83515 7686
99eaf8d4
APB
7687 /* Now we analyze the method body and look for something that
7688 isn't a MODIFY_EXPR */
7689 if (bbody == empty_stmt_node)
dba41d30
APB
7690 found = 0;
7691 else
7692 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7693
7694 if (current || found)
92d83515
APB
7695 return 0;
7696
7697 /* Get rid of <clinit> in the class' list of methods */
7698 if (TYPE_METHODS (type) == mdecl)
7699 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7700 else
7701 for (current = TYPE_METHODS (type); current;
7702 current = TREE_CHAIN (current))
7703 if (TREE_CHAIN (current) == mdecl)
7704 {
7705 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7706 break;
7707 }
7708
7709 return 1;
7710}
7711
7712
e04a16fb
AG
7713/* Complete and expand a method. */
7714
7715static void
7716java_complete_expand_method (mdecl)
7717 tree mdecl;
7718{
92d83515
APB
7719 int yank_clinit = 0;
7720
c2952b01 7721 current_function_decl = mdecl;
22eed1e6
APB
7722 /* Fix constructors before expanding them */
7723 if (DECL_CONSTRUCTOR_P (mdecl))
7724 fix_constructors (mdecl);
e04a16fb 7725
22eed1e6 7726 /* Expand functions that have a body */
e04a16fb
AG
7727 if (DECL_FUNCTION_BODY (mdecl))
7728 {
9bbc7d9f
PB
7729 tree fbody = DECL_FUNCTION_BODY (mdecl);
7730 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7731 tree exception_copy = NULL_TREE;
e04a16fb 7732 expand_start_java_method (mdecl);
939d7216 7733 build_result_decl (mdecl);
e04a16fb
AG
7734
7735 current_this
7736 = (!METHOD_STATIC (mdecl) ?
7737 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7738
ce6e9147
APB
7739 /* Purge the `throws' list of unchecked exceptions. If we're
7740 doing xref, save a copy of the list and re-install it
7741 later. */
7742 if (flag_emit_xref)
7743 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7744
b9f7e36c
APB
7745 purge_unchecked_exceptions (mdecl);
7746
7747 /* Install exceptions thrown with `throws' */
7748 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7749
9bbc7d9f 7750 if (block_body != NULL_TREE)
bc3ca41b
PB
7751 {
7752 block_body = java_complete_tree (block_body);
c2952b01 7753
7145d9fe 7754 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7755 check_for_initialization (block_body);
f099f336 7756 ctxp->explicit_constructor_p = 0;
bc3ca41b 7757 }
e803d3b2 7758
9bbc7d9f 7759 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7760
c2952b01
APB
7761 /* If we saw a return but couldn't evaluate it properly, we'll
7762 have an error_mark_node here. */
7763 if (block_body != error_mark_node
7764 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7765 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7766 && !flag_emit_xref)
82371d41 7767 missing_return_error (current_function_decl);
7525cc04 7768
92d83515
APB
7769 /* Check wether we could just get rid of clinit, now the picture
7770 is complete. */
7771 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7772 complete_start_java_method (mdecl);
7773
e04a16fb 7774 /* Don't go any further if we've found error(s) during the
92d83515
APB
7775 expansion */
7776 if (!java_error_count && !yank_clinit)
e04a16fb 7777 source_end_java_method ();
22eed1e6
APB
7778 else
7779 {
92d83515
APB
7780 if (java_error_count)
7781 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7782 poplevel (1, 0, 1);
7783 }
b9f7e36c
APB
7784
7785 /* Pop the exceptions and sanity check */
7786 POP_EXCEPTIONS();
7787 if (currently_caught_type_list)
400500c4 7788 abort ();
ce6e9147
APB
7789
7790 if (flag_emit_xref)
7791 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7792 }
7793}
7794
c2952b01
APB
7795\f
7796
7797/* This section of the code deals with accessing enclosing context
7798 fields either directly by using the relevant access to this$<n> or
7799 by invoking an access method crafted for that purpose. */
7800
7801/* Build the necessary access from an inner class to an outer
7802 class. This routine could be optimized to cache previous result
7803 (decl, current_class and returned access). When an access method
7804 needs to be generated, it always takes the form of a read. It might
7805 be later turned into a write by calling outer_field_access_fix. */
7806
7807static tree
7808build_outer_field_access (id, decl)
7809 tree id, decl;
7810{
7811 tree access = NULL_TREE;
7812 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7813 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7814
5e18f6d6
APB
7815 /* If the immediate enclosing context of the current class is the
7816 field decl's class or inherits from it; build the access as
7817 `this$<n>.<field>'. Note that we will break the `private' barrier
7818 if we're not emitting bytecodes. */
7819 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7820 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7821 {
7822 tree thisn = build_current_thisn (current_class);
7823 access = make_qualified_primary (build_wfl_node (thisn),
7824 id, EXPR_WFL_LINECOL (id));
7825 }
7826 /* Otherwise, generate access methods to outer this and access the
7827 field (either using an access method or by direct access.) */
7828 else
7829 {
7830 int lc = EXPR_WFL_LINECOL (id);
7831
7832 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7833 get a hold to the enclosing instance we need, and then we
c2952b01 7834 build the field access. */
5e18f6d6 7835 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7836
7837 /* If the field is private and we're generating bytecode, then
7838 we generate an access method */
7839 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7840 {
7841 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7842 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7843 name, access, NULL_TREE);
7844 }
7845 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7846 Once again we break the `private' access rule from a foreign
7847 class. */
7848 else
7849 access = make_qualified_primary (access, id, lc);
7850 }
7851 return resolve_expression_name (access, NULL);
7852}
7853
7854/* Return a non zero value if NODE describes an outer field inner
7855 access. */
7856
7857static int
7858outer_field_access_p (type, decl)
7859 tree type, decl;
7860{
7861 if (!INNER_CLASS_TYPE_P (type)
7862 || TREE_CODE (decl) != FIELD_DECL
7863 || DECL_CONTEXT (decl) == type)
7864 return 0;
ee5f86dc
APB
7865
7866 /* If the inner class extends the declaration context of the field
7867 we're try to acces, then this isn't an outer field access */
7868 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7869 return 0;
c2952b01
APB
7870
7871 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7872 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7873 {
7874 if (type == DECL_CONTEXT (decl))
7875 return 1;
5e18f6d6 7876
c2952b01 7877 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7878 {
7879 /* Before we give up, see whether the field is inherited from
7880 the enclosing context we're considering. */
7881 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7882 return 1;
7883 break;
7884 }
c2952b01
APB
7885 }
7886
7887 return 0;
7888}
7889
7890/* Return a non zero value if NODE represents an outer field inner
7891 access that was been already expanded. As a side effect, it returns
7892 the name of the field being accessed and the argument passed to the
7893 access function, suitable for a regeneration of the access method
7894 call if necessary. */
7895
7896static int
7897outer_field_expanded_access_p (node, name, arg_type, arg)
7898 tree node, *name, *arg_type, *arg;
7899{
7900 int identified = 0;
7901
7902 if (TREE_CODE (node) != CALL_EXPR)
7903 return 0;
7904
7905 /* Well, gcj generates slightly different tree nodes when compiling
7906 to native or bytecodes. It's the case for function calls. */
7907
7908 if (flag_emit_class_files
7909 && TREE_CODE (node) == CALL_EXPR
7910 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7911 identified = 1;
7912 else if (!flag_emit_class_files)
7913 {
7914 node = TREE_OPERAND (node, 0);
7915
7916 if (node && TREE_OPERAND (node, 0)
7917 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7918 {
7919 node = TREE_OPERAND (node, 0);
7920 if (TREE_OPERAND (node, 0)
7921 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7922 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7923 (DECL_NAME (TREE_OPERAND (node, 0)))))
7924 identified = 1;
7925 }
7926 }
7927
7928 if (identified && name && arg_type && arg)
7929 {
7930 tree argument = TREE_OPERAND (node, 1);
7931 *name = DECL_NAME (TREE_OPERAND (node, 0));
7932 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7933 *arg = TREE_VALUE (argument);
7934 }
7935 return identified;
7936}
7937
7938/* Detect in NODE an outer field read access from an inner class and
7939 transform it into a write with RHS as an argument. This function is
7940 called from the java_complete_lhs when an assignment to a LHS can
7941 be identified. */
7942
7943static tree
7944outer_field_access_fix (wfl, node, rhs)
7945 tree wfl, node, rhs;
7946{
7947 tree name, arg_type, arg;
7948
7949 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7950 {
7951 /* At any rate, check whether we're trying to assign a value to
7952 a final. */
7953 tree accessed = (JDECL_P (node) ? node :
7954 (TREE_CODE (node) == COMPONENT_REF ?
7955 TREE_OPERAND (node, 1) : node));
7956 if (check_final_assignment (accessed, wfl))
7957 return error_mark_node;
7958
7959 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7960 arg_type, name, arg, rhs);
7961 return java_complete_tree (node);
7962 }
7963 return NULL_TREE;
7964}
7965
7966/* Construct the expression that calls an access method:
7967 <type>.access$<n>(<arg1> [, <arg2>]);
7968
7969 ARG2 can be NULL and will be omitted in that case. It will denote a
7970 read access. */
7971
7972static tree
7973build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7974 int lc;
7975 tree type, access_method_name, arg1, arg2;
7976{
7977 tree args, cn, access;
7978
7979 args = arg1 ? arg1 :
7980 build_wfl_node (build_current_thisn (current_class));
7981 args = build_tree_list (NULL_TREE, args);
7982
7983 if (arg2)
7984 args = tree_cons (NULL_TREE, arg2, args);
7985
7986 access = build_method_invocation (build_wfl_node (access_method_name), args);
7987 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7988 return make_qualified_primary (cn, access, lc);
7989}
7990
7991static tree
7992build_new_access_id ()
7993{
7994 static int access_n_counter = 1;
7995 char buffer [128];
7996
7997 sprintf (buffer, "access$%d", access_n_counter++);
7998 return get_identifier (buffer);
7999}
8000
8001/* Create the static access functions for the outer field DECL. We define a
8002 read:
8003 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8004 return inst$.field;
8005 }
8006 and a write access:
8007 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8008 TREE_TYPE (<field>) value$) {
8009 return inst$.field = value$;
8010 }
8011 We should have a usage flags on the DECL so we can lazily turn the ones
8012 we're using for code generation. FIXME.
8013*/
8014
8015static tree
8016build_outer_field_access_methods (decl)
8017 tree decl;
8018{
8019 tree id, args, stmt, mdecl;
8020
c7303e41 8021 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
8022 return FIELD_INNER_ACCESS (decl);
8023
c7303e41
APB
8024 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8025
c2952b01
APB
8026 /* Create the identifier and a function named after it. */
8027 id = build_new_access_id ();
8028
8029 /* The identifier is marked as bearing the name of a generated write
8030 access function for outer field accessed from inner classes. */
8031 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8032
8033 /* Create the read access */
8034 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8035 TREE_CHAIN (args) = end_params_node;
8036 stmt = make_qualified_primary (build_wfl_node (inst_id),
8037 build_wfl_node (DECL_NAME (decl)), 0);
8038 stmt = build_return (0, stmt);
8039 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8040 TREE_TYPE (decl), id, args, stmt);
8041 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8042
c7303e41
APB
8043 /* Create the write access method. No write access for final variable */
8044 if (!FIELD_FINAL (decl))
8045 {
8046 args = build_tree_list (inst_id,
8047 build_pointer_type (DECL_CONTEXT (decl)));
8048 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8049 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8050 stmt = make_qualified_primary (build_wfl_node (inst_id),
8051 build_wfl_node (DECL_NAME (decl)), 0);
8052 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8053 build_wfl_node (wpv_id)));
8054 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8055 TREE_TYPE (decl), id,
8056 args, stmt);
8057 }
c2952b01 8058 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8059
8060 /* Return the access name */
8061 return FIELD_INNER_ACCESS (decl) = id;
8062}
8063
8064/* Build an field access method NAME. */
8065
8066static tree
8067build_outer_field_access_method (class, type, name, args, body)
8068 tree class, type, name, args, body;
8069{
8070 tree saved_current_function_decl, mdecl;
8071
8072 /* Create the method */
8073 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8074 fix_method_argument_names (args, mdecl);
8075 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8076
8077 /* Attach the method body. */
8078 saved_current_function_decl = current_function_decl;
8079 start_artificial_method_body (mdecl);
8080 java_method_add_stmt (mdecl, body);
8081 end_artificial_method_body (mdecl);
8082 current_function_decl = saved_current_function_decl;
8083
8084 return mdecl;
8085}
8086
8087\f
8088/* This section deals with building access function necessary for
8089 certain kinds of method invocation from inner classes. */
8090
8091static tree
8092build_outer_method_access_method (decl)
8093 tree decl;
8094{
8095 tree saved_current_function_decl, mdecl;
8096 tree args = NULL_TREE, call_args = NULL_TREE;
8097 tree carg, id, body, class;
8098 char buffer [80];
8099 int parm_id_count = 0;
8100
8101 /* Test this abort with an access to a private field */
8102 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8103 abort ();
8104
8105 /* Check the cache first */
8106 if (DECL_FUNCTION_INNER_ACCESS (decl))
8107 return DECL_FUNCTION_INNER_ACCESS (decl);
8108
8109 class = DECL_CONTEXT (decl);
8110
8111 /* Obtain an access identifier and mark it */
8112 id = build_new_access_id ();
8113 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8114
c2952b01
APB
8115 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8116 /* Create the arguments, as much as the original */
8117 for (; carg && carg != end_params_node;
8118 carg = TREE_CHAIN (carg))
8119 {
8120 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8121 args = chainon (args, build_tree_list (get_identifier (buffer),
8122 TREE_VALUE (carg)));
8123 }
8124 args = chainon (args, end_params_node);
8125
8126 /* Create the method */
8127 mdecl = create_artificial_method (class, ACC_STATIC,
8128 TREE_TYPE (TREE_TYPE (decl)), id, args);
8129 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8130 /* There is a potential bug here. We should be able to use
8131 fix_method_argument_names, but then arg names get mixed up and
8132 eventually a constructor will have its this$0 altered and the
8133 outer context won't be assignment properly. The test case is
8134 stub.java FIXME */
8135 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8136
8137 /* Attach the method body. */
8138 saved_current_function_decl = current_function_decl;
8139 start_artificial_method_body (mdecl);
8140
8141 /* The actual method invocation uses the same args. When invoking a
8142 static methods that way, we don't want to skip the first
8143 argument. */
8144 carg = args;
8145 if (!METHOD_STATIC (decl))
8146 carg = TREE_CHAIN (carg);
8147 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8148 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8149 call_args);
8150
8151 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8152 call_args);
8153 if (!METHOD_STATIC (decl))
8154 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8155 body, 0);
8156 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8157 body = build_return (0, body);
8158 java_method_add_stmt (mdecl,body);
8159 end_artificial_method_body (mdecl);
8160 current_function_decl = saved_current_function_decl;
c2952b01
APB
8161
8162 /* Back tag the access function so it know what it accesses */
8163 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8164
8165 /* Tag the current method so it knows it has an access generated */
8166 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8167}
8168
8169\f
8170/* This section of the code deals with building expressions to access
8171 the enclosing instance of an inner class. The enclosing instance is
8172 kept in a generated field called this$<n>, with <n> being the
8173 inner class nesting level (starting from 0.) */
8174
dba41d30
APB
8175/* Build an access to a given this$<n>, always chaining access call to
8176 others. Access methods to this$<n> are build on the fly if
8177 necessary. This CAN'T be used to solely access this$<n-1> from
8178 this$<n> (which alway yield to special cases and optimization, see
8179 for example build_outer_field_access). */
c2952b01
APB
8180
8181static tree
8182build_access_to_thisn (from, to, lc)
8183 tree from, to;
8184 int lc;
8185{
8186 tree access = NULL_TREE;
8187
8188 while (from != to)
8189 {
c2952b01 8190 if (!access)
dba41d30
APB
8191 {
8192 access = build_current_thisn (from);
8193 access = build_wfl_node (access);
8194 }
8195 else
c2952b01 8196 {
dba41d30
APB
8197 tree access0_wfl, cn;
8198
8199 maybe_build_thisn_access_method (from);
8200 access0_wfl = build_wfl_node (access0_identifier_node);
8201 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8202 EXPR_WFL_LINECOL (access0_wfl) = lc;
8203 access = build_tree_list (NULL_TREE, access);
8204 access = build_method_invocation (access0_wfl, access);
8205 access = make_qualified_primary (cn, access, lc);
c2952b01 8206 }
5e18f6d6
APB
8207
8208 /* if FROM isn't an inter class, that's fine, we've done
8209 enough. What we're looking for can be accessed from there. */
8210 from = DECL_CONTEXT (TYPE_NAME (from));
8211 if (!from)
8212 break;
8213 from = TREE_TYPE (from);
c2952b01
APB
8214 }
8215 return access;
8216}
8217
8218/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8219 is returned if nothing needs to be generated. Otherwise, the method
152de068 8220 generated and a method decl is returned.
c2952b01
APB
8221
8222 NOTE: These generated methods should be declared in a class file
8223 attribute so that they can't be referred to directly. */
8224
8225static tree
8226maybe_build_thisn_access_method (type)
8227 tree type;
8228{
8229 tree mdecl, args, stmt, rtype;
8230 tree saved_current_function_decl;
8231
8232 /* If TYPE is a top-level class, no access method is required.
8233 If there already is such an access method, bail out. */
ee5f86dc 8234 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8235 return NULL_TREE;
8236
8237 /* We generate the method. The method looks like:
8238 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8239 */
c2952b01
APB
8240 args = build_tree_list (inst_id, build_pointer_type (type));
8241 TREE_CHAIN (args) = end_params_node;
8242 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8243 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8244 access0_identifier_node, args);
8245 fix_method_argument_names (args, mdecl);
8246 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8247 stmt = build_current_thisn (type);
8248 stmt = make_qualified_primary (build_wfl_node (inst_id),
8249 build_wfl_node (stmt), 0);
8250 stmt = build_return (0, stmt);
8251
8252 saved_current_function_decl = current_function_decl;
8253 start_artificial_method_body (mdecl);
8254 java_method_add_stmt (mdecl, stmt);
8255 end_artificial_method_body (mdecl);
8256 current_function_decl = saved_current_function_decl;
c2952b01
APB
8257
8258 CLASS_ACCESS0_GENERATED_P (type) = 1;
8259
8260 return mdecl;
8261}
8262
8263/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8264 the first level of innerclassing. this$1 for the next one, etc...
8265 This function can be invoked with TYPE to NULL, available and then
8266 has to count the parser context. */
8267
8268static tree
8269build_current_thisn (type)
8270 tree type;
8271{
8272 static int saved_i = -1;
8273 static tree saved_thisn = NULL_TREE;
19e223db
MM
8274 static tree saved_type = NULL_TREE;
8275 static int saved_type_i = 0;
8276 static int initialized_p;
c2952b01
APB
8277 tree decl;
8278 char buffer [80];
8279 int i = 0;
8280
19e223db
MM
8281 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8282 if (!initialized_p)
c2952b01 8283 {
19e223db
MM
8284 ggc_add_tree_root (&saved_thisn, 1);
8285 ggc_add_tree_root (&saved_type, 1);
8286 initialized_p = 1;
8287 }
c2952b01 8288
19e223db
MM
8289 if (type)
8290 {
c2952b01
APB
8291 if (type == saved_type)
8292 i = saved_type_i;
8293 else
8294 {
8295 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8296 decl; decl = DECL_CONTEXT (decl), i++)
8297 ;
8298
8299 saved_type = type;
8300 saved_type_i = i;
8301 }
8302 }
8303 else
8304 i = list_length (GET_CPC_LIST ())-2;
8305
8306 if (i == saved_i)
8307 return saved_thisn;
8308
8309 sprintf (buffer, "this$%d", i);
8310 saved_i = i;
8311 saved_thisn = get_identifier (buffer);
8312 return saved_thisn;
8313}
8314
8315/* Return the assignement to the hidden enclosing context `this$<n>'
8316 by the second incoming parameter to the innerclass constructor. The
8317 form used is `this.this$<n> = this$<n>;'. */
8318
8319static tree
8320build_thisn_assign ()
8321{
8322 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8323 {
8324 tree thisn = build_current_thisn (current_class);
8325 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8326 build_wfl_node (thisn), 0);
8327 tree rhs = build_wfl_node (thisn);
8328 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8329 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8330 }
8331 return NULL_TREE;
8332}
8333
8334\f
165f37bc
APB
8335/* Building the synthetic `class$' used to implement the `.class' 1.1
8336 extension for non primitive types. This method looks like:
8337
8338 static Class class$(String type) throws NoClassDefFoundError
8339 {
8340 try {return (java.lang.Class.forName (String));}
8341 catch (ClassNotFoundException e) {
8342 throw new NoClassDefFoundError(e.getMessage());}
8343 } */
8344
8345static tree
8346build_dot_class_method (class)
8347 tree class;
8348{
8349#define BWF(S) build_wfl_node (get_identifier ((S)))
8350#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8351 tree args, tmp, saved_current_function_decl, mdecl;
8352 tree stmt, throw_stmt, catch, catch_block, try_block;
8353 tree catch_clause_param;
8354 tree class_not_found_exception, no_class_def_found_error;
8355
8356 static tree get_message_wfl, type_parm_wfl;
8357
8358 if (!get_message_wfl)
8359 {
8360 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8361 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8362 ggc_add_tree_root (&get_message_wfl, 1);
8363 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8364 }
8365
8366 /* Build the arguments */
8367 args = build_tree_list (get_identifier ("type$"),
8368 build_pointer_type (string_type_node));
8369 TREE_CHAIN (args) = end_params_node;
8370
8371 /* Build the qualified name java.lang.Class.forName */
8372 tmp = MQN (MQN (MQN (BWF ("java"),
8373 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8374
8375 /* For things we have to catch and throw */
8376 class_not_found_exception =
8377 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8378 no_class_def_found_error =
8379 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8380 load_class (class_not_found_exception, 1);
8381 load_class (no_class_def_found_error, 1);
8382
8383 /* Create the "class$" function */
8384 mdecl = create_artificial_method (class, ACC_STATIC,
8385 build_pointer_type (class_type_node),
94807d33 8386 classdollar_identifier_node, args);
165f37bc
APB
8387 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8388 no_class_def_found_error);
8389
8390 /* We start by building the try block. We need to build:
8391 return (java.lang.Class.forName (type)); */
8392 stmt = build_method_invocation (tmp,
8393 build_tree_list (NULL_TREE, type_parm_wfl));
8394 stmt = build_return (0, stmt);
8395 /* Put it in a block. That's the try block */
8396 try_block = build_expr_block (stmt, NULL_TREE);
8397
8398 /* Now onto the catch block. We start by building the expression
8399 throwing a new exception:
8400 throw new NoClassDefFoundError (_.getMessage); */
8401 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8402 get_message_wfl, 0);
8403 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8404
8405 /* Build new NoClassDefFoundError (_.getMessage) */
8406 throw_stmt = build_new_invocation
8407 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8408 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8409
8410 /* Build the throw, (it's too early to use BUILD_THROW) */
8411 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8412
8413 /* Build the catch block to encapsulate all this. We begin by
8414 building an decl for the catch clause parameter and link it to
8415 newly created block, the catch block. */
8416 catch_clause_param =
8417 build_decl (VAR_DECL, wpv_id,
8418 build_pointer_type (class_not_found_exception));
8419 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8420
8421 /* We initialize the variable with the exception handler. */
8422 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8423 soft_exceptioninfo_call_node);
8424 add_stmt_to_block (catch_block, NULL_TREE, catch);
8425
8426 /* We add the statement throwing the new exception */
8427 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8428
8429 /* Build a catch expression for all this */
8430 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8431
8432 /* Build the try/catch sequence */
8433 stmt = build_try_statement (0, try_block, catch_block);
8434
8435 fix_method_argument_names (args, mdecl);
8436 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8437 saved_current_function_decl = current_function_decl;
8438 start_artificial_method_body (mdecl);
8439 java_method_add_stmt (mdecl, stmt);
8440 end_artificial_method_body (mdecl);
8441 current_function_decl = saved_current_function_decl;
8442 TYPE_DOT_CLASS (class) = mdecl;
8443
8444 return mdecl;
8445}
8446
8447static tree
f0f3a777
APB
8448build_dot_class_method_invocation (type)
8449 tree type;
165f37bc 8450{
f0f3a777
APB
8451 tree sig_id, s;
8452
8453 if (TYPE_ARRAY_P (type))
8454 sig_id = build_java_signature (type);
8455 else
8456 sig_id = DECL_NAME (TYPE_NAME (type));
8457
1f8f4a0b
MM
8458 s = build_string (IDENTIFIER_LENGTH (sig_id),
8459 IDENTIFIER_POINTER (sig_id));
94807d33 8460 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
165f37bc
APB
8461 build_tree_list (NULL_TREE, s));
8462}
8463
c2952b01
APB
8464/* This section of the code deals with constructor. */
8465
22eed1e6
APB
8466/* Craft a body for default constructor. Patch existing constructor
8467 bodies with call to super() and field initialization statements if
8468 necessary. */
8469
8470static void
8471fix_constructors (mdecl)
8472 tree mdecl;
8473{
8474 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8475 tree thisn_assign, compound = NULL_TREE;
8476 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8477
22eed1e6
APB
8478 if (!body)
8479 {
22eed1e6
APB
8480 /* It is an error for the compiler to generate a default
8481 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8482 takes no argument, or the same args for an anonymous class */
8483 if (verify_constructor_super (mdecl))
22eed1e6 8484 {
c2952b01
APB
8485 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8486 tree save = DECL_NAME (mdecl);
49f48c71 8487 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8488 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8489 parse_error_context
c2952b01
APB
8490 (lookup_cl (TYPE_NAME (class_type)),
8491 "No constructor matching `%s' found in class `%s'",
8492 lang_printable_name (mdecl, 0), n);
8493 DECL_NAME (mdecl) = save;
22eed1e6
APB
8494 }
8495
c2952b01
APB
8496 /* The constructor body must be crafted by hand. It's the
8497 constructor we defined when we realize we didn't have the
8498 CLASSNAME() constructor */
22eed1e6
APB
8499 start_artificial_method_body (mdecl);
8500
f0f3a777
APB
8501 /* Insert an assignment to the this$<n> hidden field, if
8502 necessary */
8503 if ((thisn_assign = build_thisn_assign ()))
8504 java_method_add_stmt (mdecl, thisn_assign);
8505
22eed1e6
APB
8506 /* We don't generate a super constructor invocation if we're
8507 compiling java.lang.Object. build_super_invocation takes care
8508 of that. */
e920ebc9 8509 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8510
c2952b01
APB
8511 /* Insert the instance initializer block right here, after the
8512 super invocation. */
8513 add_instance_initializer (mdecl);
8514
22eed1e6
APB
8515 end_artificial_method_body (mdecl);
8516 }
8517 /* Search for an explicit constructor invocation */
8518 else
8519 {
8520 int found = 0;
8521 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8522
8523 while (body)
8524 switch (TREE_CODE (body))
8525 {
8526 case CALL_EXPR:
8527 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8528 body = NULL_TREE;
8529 break;
8530 case COMPOUND_EXPR:
8531 case EXPR_WITH_FILE_LOCATION:
8532 body = TREE_OPERAND (body, 0);
8533 break;
8534 case BLOCK:
8535 body = BLOCK_EXPR_BODY (body);
8536 break;
8537 default:
8538 found = 0;
8539 body = NULL_TREE;
8540 }
8541 /* The constructor is missing an invocation of super() */
8542 if (!found)
8543 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8544 build_super_invocation (mdecl));
22eed1e6 8545
c2952b01
APB
8546 /* Generate the assignment to this$<n>, if necessary */
8547 if ((thisn_assign = build_thisn_assign ()))
8548 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8549
f0f3a777
APB
8550 /* Insert the instance initializer block right here, after the
8551 super invocation. */
8552 add_instance_initializer (mdecl);
8553
22eed1e6
APB
8554 /* Fix the constructor main block if we're adding extra stmts */
8555 if (compound)
8556 {
8557 compound = add_stmt_to_compound (compound, NULL_TREE,
8558 BLOCK_EXPR_BODY (main_block));
8559 BLOCK_EXPR_BODY (main_block) = compound;
8560 }
8561 }
8562}
8563
8564/* Browse constructors in the super class, searching for a constructor
8565 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8566 otherwise. If the current class is an anonymous inner class, look
8567 for something that has the same signature. */
22eed1e6
APB
8568
8569static int
c2952b01
APB
8570verify_constructor_super (mdecl)
8571 tree mdecl;
22eed1e6
APB
8572{
8573 tree class = CLASSTYPE_SUPER (current_class);
152de068 8574 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8575 tree sdecl;
8576
22eed1e6
APB
8577 if (!class)
8578 return 0;
8579
c2952b01 8580 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8581 {
c2952b01
APB
8582 tree mdecl_arg_type;
8583 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8584 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8585 if (DECL_CONSTRUCTOR_P (sdecl))
8586 {
cf1b2274 8587 tree m_arg_type;
152de068
APB
8588 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8589 if (super_inner)
8590 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8591 for (m_arg_type = mdecl_arg_type;
8592 (arg_type != end_params_node
8593 && m_arg_type != end_params_node);
c2952b01 8594 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8595 m_arg_type = TREE_CHAIN (m_arg_type))
8596 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8597 break;
8598
cf1b2274 8599 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8600 return 0;
8601 }
8602 }
8603 else
8604 {
8605 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8606 {
152de068
APB
8607 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8608 if (super_inner)
8609 arg = TREE_CHAIN (arg);
8610 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8611 return 0;
8612 }
8613 }
8614 return 1;
8615}
8616
22eed1e6 8617/* Generate code for all context remembered for code generation. */
b351b287
APB
8618
8619void
8620java_expand_classes ()
8621{
5423609c 8622 int save_error_count = 0;
c2952b01
APB
8623 static struct parser_ctxt *saved_ctxp = NULL;
8624
23a79c61
APB
8625 java_parse_abort_on_error ();
8626 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8627 return;
8628 java_layout_classes ();
8629 java_parse_abort_on_error ();
8630
c2952b01 8631 saved_ctxp = ctxp_for_generation;
b351b287
APB
8632 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8633 {
8634 ctxp = ctxp_for_generation;
8635 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8636 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8637 java_parse_abort_on_error ();
8638 }
c2952b01
APB
8639
8640 /* Find anonymous classes and expand their constructor, now they
8641 have been fixed. */
8642 for (ctxp_for_generation = saved_ctxp;
8643 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8644 {
8645 tree current;
8646 ctxp = ctxp_for_generation;
8647 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8648 {
8649 current_class = TREE_TYPE (current);
8650 if (ANONYMOUS_CLASS_P (current_class))
8651 {
8652 tree d;
8653 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8654 {
8655 if (DECL_CONSTRUCTOR_P (d))
8656 {
8657 restore_line_number_status (1);
c2952b01
APB
8658 java_complete_expand_method (d);
8659 restore_line_number_status (0);
8660 break; /* We now there are no other ones */
8661 }
8662 }
8663 }
8664 }
8665 }
8666
8667 /* If we've found error at that stage, don't try to generate
8668 anything, unless we're emitting xrefs or checking the syntax only
8669 (but not using -fsyntax-only for the purpose of generating
8670 bytecode. */
8671 if (java_error_count && !flag_emit_xref
8672 && (!flag_syntax_only && !flag_emit_class_files))
8673 return;
8674
8675 /* Now things are stable, go for generation of the class data. */
8676 for (ctxp_for_generation = saved_ctxp;
8677 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8678 {
8679 tree current;
8680 ctxp = ctxp_for_generation;
8681 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8682 {
8683 current_class = TREE_TYPE (current);
8684 outgoing_cpool = TYPE_CPOOL (current_class);
8685 if (flag_emit_class_files)
8686 write_classfile (current_class);
8687 if (flag_emit_xref)
8688 expand_xref (current_class);
8689 else if (! flag_syntax_only)
8690 finish_class ();
8691 }
8692 }
b351b287
APB
8693}
8694
e04a16fb
AG
8695/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8696 a tree list node containing RIGHT. Fore coming RIGHTs will be
8697 chained to this hook. LOCATION contains the location of the
8698 separating `.' operator. */
8699
8700static tree
8701make_qualified_primary (primary, right, location)
8702 tree primary, right;
8703 int location;
8704{
8705 tree wfl;
8706
c2952b01 8707 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8708 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8709 else
8710 {
8711 wfl = primary;
c2952b01
APB
8712 /* If wfl wasn't qualified, we build a first anchor */
8713 if (!EXPR_WFL_QUALIFICATION (wfl))
8714 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8715 }
8716
c2952b01 8717 /* And chain them */
e04a16fb
AG
8718 EXPR_WFL_LINECOL (right) = location;
8719 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8720 PRIMARY_P (wfl) = 1;
8721 return wfl;
8722}
8723
8724/* Simple merge of two name separated by a `.' */
8725
8726static tree
8727merge_qualified_name (left, right)
8728 tree left, right;
8729{
8730 tree node;
c2952b01
APB
8731 if (!left && !right)
8732 return NULL_TREE;
8733
8734 if (!left)
8735 return right;
8736
8737 if (!right)
8738 return left;
8739
e04a16fb
AG
8740 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8741 IDENTIFIER_LENGTH (left));
8742 obstack_1grow (&temporary_obstack, '.');
8743 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8744 IDENTIFIER_LENGTH (right));
8745 node = get_identifier (obstack_base (&temporary_obstack));
8746 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8747 QUALIFIED_P (node) = 1;
8748 return node;
8749}
8750
8751/* Merge the two parts of a qualified name into LEFT. Set the
8752 location information of the resulting node to LOCATION, usually
8753 inherited from the location information of the `.' operator. */
8754
8755static tree
8756make_qualified_name (left, right, location)
8757 tree left, right;
8758 int location;
8759{
bc3ca41b
PB
8760#ifdef USE_COMPONENT_REF
8761 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8762 EXPR_WFL_LINECOL (node) = location;
8763 return node;
8764#else
e04a16fb
AG
8765 tree left_id = EXPR_WFL_NODE (left);
8766 tree right_id = EXPR_WFL_NODE (right);
8767 tree wfl, merge;
8768
8769 merge = merge_qualified_name (left_id, right_id);
8770
8771 /* Left wasn't qualified and is now qualified */
8772 if (!QUALIFIED_P (left_id))
8773 {
8774 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8775 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8776 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8777 }
8778
8779 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8780 EXPR_WFL_LINECOL (wfl) = location;
8781 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8782
8783 EXPR_WFL_NODE (left) = merge;
8784 return left;
bc3ca41b 8785#endif
e04a16fb
AG
8786}
8787
8788/* Extract the last identifier component of the qualified in WFL. The
8789 last identifier is removed from the linked list */
8790
8791static tree
8792cut_identifier_in_qualified (wfl)
8793 tree wfl;
8794{
8795 tree q;
8796 tree previous = NULL_TREE;
8797 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8798 if (!TREE_CHAIN (q))
8799 {
8800 if (!previous)
400500c4
RK
8801 /* Operating on a non qualified qualified WFL. */
8802 abort ();
8803
e04a16fb
AG
8804 TREE_CHAIN (previous) = NULL_TREE;
8805 return TREE_PURPOSE (q);
8806 }
8807}
8808
8809/* Resolve the expression name NAME. Return its decl. */
8810
8811static tree
5e942c50 8812resolve_expression_name (id, orig)
e04a16fb 8813 tree id;
5e942c50 8814 tree *orig;
e04a16fb
AG
8815{
8816 tree name = EXPR_WFL_NODE (id);
8817 tree decl;
8818
8819 /* 6.5.5.1: Simple expression names */
8820 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8821 {
8822 /* 15.13.1: NAME can appear within the scope of a local variable
8823 declaration */
8824 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8825 return decl;
8826
8827 /* 15.13.1: NAME can appear within a class declaration */
8828 else
8829 {
8830 decl = lookup_field_wrapper (current_class, name);
8831 if (decl)
8832 {
c2952b01 8833 tree access = NULL_TREE;
e04a16fb 8834 int fs = FIELD_STATIC (decl);
f2760b27
APB
8835
8836 /* If we're accessing an outer scope local alias, make
8837 sure we change the name of the field we're going to
8838 build access to. */
8839 if (FIELD_LOCAL_ALIAS_USED (decl))
8840 name = DECL_NAME (decl);
8841
e04a16fb
AG
8842 /* Instance variable (8.3.1.1) can't appear within
8843 static method, static initializer or initializer for
8844 a static variable. */
8845 if (!fs && METHOD_STATIC (current_function_decl))
8846 {
7f10c2e2 8847 static_ref_err (id, name, current_class);
e04a16fb
AG
8848 return error_mark_node;
8849 }
22eed1e6
APB
8850 /* Instance variables can't appear as an argument of
8851 an explicit constructor invocation */
7e1376a1
BM
8852 if (!fs && ctxp->explicit_constructor_p
8853 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
22eed1e6
APB
8854 {
8855 parse_error_context
781b0558 8856 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8857 return error_mark_node;
8858 }
5e942c50 8859
c2952b01
APB
8860 /* If we're processing an inner class and we're trying
8861 to access a field belonging to an outer class, build
8862 the access to the field */
8863 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8864 {
8865 if (CLASS_STATIC (TYPE_NAME (current_class)))
8866 {
8867 static_ref_err (id, DECL_NAME (decl), current_class);
8868 return error_mark_node;
8869 }
8870 return build_outer_field_access (id, decl);
8871 }
c2952b01 8872
5e942c50 8873 /* Otherwise build what it takes to access the field */
c2952b01
APB
8874 access = build_field_ref ((fs ? NULL_TREE : current_this),
8875 DECL_CONTEXT (decl), name);
e8fc7396 8876 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8877 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8878 /* We may be asked to save the real field access node */
8879 if (orig)
c2952b01 8880 *orig = access;
5e942c50 8881 /* And we return what we got */
c2952b01 8882 return access;
e04a16fb
AG
8883 }
8884 /* Fall down to error report on undefined variable */
8885 }
8886 }
8887 /* 6.5.5.2 Qualified Expression Names */
8888 else
8889 {
5e942c50
APB
8890 if (orig)
8891 *orig = NULL_TREE;
e04a16fb
AG
8892 qualify_ambiguous_name (id);
8893 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8894 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8895 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8896 }
8897
8898 /* We've got an error here */
8899 parse_error_context (id, "Undefined variable `%s'",
8900 IDENTIFIER_POINTER (name));
8901
8902 return error_mark_node;
8903}
8904
7f10c2e2
APB
8905static void
8906static_ref_err (wfl, field_id, class_type)
8907 tree wfl, field_id, class_type;
8908{
8909 parse_error_context
8910 (wfl,
8911 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8912 IDENTIFIER_POINTER (field_id),
8913 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8914}
8915
e04a16fb
AG
8916/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8917 We return something suitable to generate the field access. We also
8918 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8919 recipient's address can be null. */
8920
8921static tree
8922resolve_field_access (qual_wfl, field_decl, field_type)
8923 tree qual_wfl;
8924 tree *field_decl, *field_type;
8925{
8926 int is_static = 0;
8927 tree field_ref;
8928 tree decl, where_found, type_found;
8929
8930 if (resolve_qualified_expression_name (qual_wfl, &decl,
8931 &where_found, &type_found))
8932 return error_mark_node;
8933
8934 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8935 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8936 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8937 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8938 {
8939 tree length = build_java_array_length_access (where_found);
8940 field_ref =
8941 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8942
8943 /* In case we're dealing with a static array, we need to
8944 initialize its class before the array length can be fetched.
8945 It's also a good time to create a DECL_RTL for the field if
8946 none already exists, otherwise if the field was declared in a
8947 class found in an external file and hasn't been (and won't
8948 be) accessed for its value, none will be created. */
8949 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8950 {
8951 build_static_field_ref (where_found);
8952 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8953 }
e04a16fb
AG
8954 }
8955 /* We might have been trying to resolve field.method(). In which
8956 case, the resolution is over and decl is the answer */
34f4db93 8957 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8958 field_ref = decl;
34f4db93 8959 else if (JDECL_P (decl))
e04a16fb 8960 {
5e942c50
APB
8961 int static_final_found = 0;
8962 if (!type_found)
8963 type_found = DECL_CONTEXT (decl);
34f4db93 8964 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 8965 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 8966 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8967 && DECL_INITIAL (decl))
5e942c50 8968 {
0c2b8145
APB
8969 /* When called on a FIELD_DECL of the right (primitive)
8970 type, java_complete_tree will try to substitue the decl
8971 for it's initial value. */
70541f45 8972 field_ref = java_complete_tree (decl);
5e942c50
APB
8973 static_final_found = 1;
8974 }
8975 else
7f10c2e2
APB
8976 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8977 NULL_TREE : where_found),
5e942c50 8978 type_found, DECL_NAME (decl));
e04a16fb
AG
8979 if (field_ref == error_mark_node)
8980 return error_mark_node;
e8fc7396
APB
8981 if (is_static && !static_final_found
8982 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8983 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8984 }
8985 else
8986 field_ref = decl;
8987
8988 if (field_decl)
8989 *field_decl = decl;
8990 if (field_type)
c877974e
APB
8991 *field_type = (QUAL_DECL_TYPE (decl) ?
8992 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8993 return field_ref;
8994}
8995
e28cd97b
APB
8996/* If NODE is an access to f static field, strip out the class
8997 initialization part and return the field decl, otherwise, return
8998 NODE. */
8999
9000static tree
9001strip_out_static_field_access_decl (node)
9002 tree node;
9003{
9004 if (TREE_CODE (node) == COMPOUND_EXPR)
9005 {
9006 tree op1 = TREE_OPERAND (node, 1);
9007 if (TREE_CODE (op1) == COMPOUND_EXPR)
9008 {
9009 tree call = TREE_OPERAND (op1, 0);
9010 if (TREE_CODE (call) == CALL_EXPR
9011 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9012 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9013 == soft_initclass_node)
9014 return TREE_OPERAND (op1, 1);
9015 }
2f11d407
TT
9016 else if (JDECL_P (op1))
9017 return op1;
e28cd97b
APB
9018 }
9019 return node;
9020}
9021
e04a16fb
AG
9022/* 6.5.5.2: Qualified Expression Names */
9023
9024static int
9025resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9026 tree wfl;
9027 tree *found_decl, *type_found, *where_found;
9028{
9029 int from_type = 0; /* Field search initiated from a type */
c2952b01 9030 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9031 int previous_call_static = 0;
9032 int is_static;
9033 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9034 /* For certain for of inner class instantiation */
9035 tree saved_current, saved_this;
9036#define RESTORE_THIS_AND_CURRENT_CLASS \
9037 { current_class = saved_current; current_this = saved_this;}
9038
c877974e 9039 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9040
9041 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9042 {
9043 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9044 tree ret_decl; /* for EH checking */
9045 int location; /* for EH checking */
e04a16fb
AG
9046
9047 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9048 switch (TREE_CODE (qual_wfl))
9049 {
9050 case CALL_EXPR:
b67d701b 9051 case NEW_CLASS_EXPR:
e04a16fb
AG
9052 /* If the access to the function call is a non static field,
9053 build the code to access it. */
34f4db93 9054 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9055 {
ac825856
APB
9056 decl = maybe_access_field (decl, *where_found,
9057 DECL_CONTEXT (decl));
e04a16fb
AG
9058 if (decl == error_mark_node)
9059 return 1;
9060 }
c2952b01 9061
e04a16fb
AG
9062 /* And code for the function call */
9063 if (complete_function_arguments (qual_wfl))
9064 return 1;
c2952b01
APB
9065
9066 /* We might have to setup a new current class and a new this
9067 for the search of an inner class, relative to the type of
9068 a expression resolved as `decl'. The current values are
9069 saved and restored shortly after */
9070 saved_current = current_class;
9071 saved_this = current_this;
dba41d30
APB
9072 if (decl
9073 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9074 || from_qualified_this))
c2952b01 9075 {
dba41d30
APB
9076 /* If we still have `from_qualified_this', we have the form
9077 <T>.this.f() and we need to build <T>.this */
9078 if (from_qualified_this)
9079 {
9080 decl = build_access_to_thisn (current_class, type, 0);
9081 decl = java_complete_tree (decl);
9082 type = TREE_TYPE (TREE_TYPE (decl));
9083 }
c2952b01
APB
9084 current_class = type;
9085 current_this = decl;
dba41d30 9086 from_qualified_this = 0;
c2952b01
APB
9087 }
9088
89e09b9a
PB
9089 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9090 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9091 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9092 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
e101152f
APB
9093 *where_found = patch_method_invocation (qual_wfl, decl, type,
9094 from_super,
7705e9db 9095 &is_static, &ret_decl);
e04a16fb 9096 if (*where_found == error_mark_node)
c2952b01
APB
9097 {
9098 RESTORE_THIS_AND_CURRENT_CLASS;
9099 return 1;
9100 }
e04a16fb
AG
9101 *type_found = type = QUAL_DECL_TYPE (*where_found);
9102
c2952b01
APB
9103 /* If we're creating an inner class instance, check for that
9104 an enclosing instance is in scope */
9105 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9106 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9107 {
9108 parse_error_context
165f37bc
APB
9109 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9110 lang_printable_name (type, 0),
9111 (!current_this ? "" :
9112 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9113 RESTORE_THIS_AND_CURRENT_CLASS;
9114 return 1;
9115 }
9116
9117 /* In case we had to change then to resolve a inner class
9118 instantiation using a primary qualified by a `new' */
9119 RESTORE_THIS_AND_CURRENT_CLASS;
9120
34d4df06
APB
9121 /* EH check. No check on access$<n> functions */
9122 if (location
9123 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9124 (DECL_NAME (current_function_decl)))
7705e9db
APB
9125 check_thrown_exceptions (location, ret_decl);
9126
e04a16fb
AG
9127 /* If the previous call was static and this one is too,
9128 build a compound expression to hold the two (because in
9129 that case, previous function calls aren't transported as
9130 forcoming function's argument. */
9131 if (previous_call_static && is_static)
9132 {
9133 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9134 TREE_SIDE_EFFECTS (decl) = 1;
9135 }
9136 else
9137 {
9138 previous_call_static = is_static;
9139 decl = *where_found;
9140 }
c2952b01 9141 from_type = 0;
e04a16fb
AG
9142 continue;
9143
d8fccff5 9144 case NEW_ARRAY_EXPR:
c2952b01 9145 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9146 *where_found = decl = java_complete_tree (qual_wfl);
9147 if (decl == error_mark_node)
9148 return 1;
9149 *type_found = type = QUAL_DECL_TYPE (decl);
9150 CLASS_LOADED_P (type) = 1;
9151 continue;
9152
e04a16fb
AG
9153 case CONVERT_EXPR:
9154 *where_found = decl = java_complete_tree (qual_wfl);
9155 if (decl == error_mark_node)
9156 return 1;
9157 *type_found = type = QUAL_DECL_TYPE (decl);
9158 from_cast = 1;
9159 continue;
9160
22eed1e6 9161 case CONDITIONAL_EXPR:
5e942c50 9162 case STRING_CST:
ac22f9cb 9163 case MODIFY_EXPR:
22eed1e6
APB
9164 *where_found = decl = java_complete_tree (qual_wfl);
9165 if (decl == error_mark_node)
9166 return 1;
9167 *type_found = type = QUAL_DECL_TYPE (decl);
9168 continue;
9169
e04a16fb
AG
9170 case ARRAY_REF:
9171 /* If the access to the function call is a non static field,
9172 build the code to access it. */
34f4db93 9173 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9174 {
9175 decl = maybe_access_field (decl, *where_found, type);
9176 if (decl == error_mark_node)
9177 return 1;
9178 }
9179 /* And code for the array reference expression */
9180 decl = java_complete_tree (qual_wfl);
9181 if (decl == error_mark_node)
9182 return 1;
9183 type = QUAL_DECL_TYPE (decl);
9184 continue;
0a2138e2 9185
37feda7d
APB
9186 case PLUS_EXPR:
9187 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9188 return 1;
9189 if ((type = patch_string (decl)))
9190 decl = type;
9191 *where_found = QUAL_RESOLUTION (q) = decl;
9192 *type_found = type = TREE_TYPE (decl);
9193 break;
9194
165f37bc
APB
9195 case CLASS_LITERAL:
9196 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9197 return 1;
9198 *where_found = QUAL_RESOLUTION (q) = decl;
9199 *type_found = type = TREE_TYPE (decl);
9200 break;
9201
0a2138e2
APB
9202 default:
9203 /* Fix for -Wall Just go to the next statement. Don't
9204 continue */
a3f406ce 9205 break;
e04a16fb
AG
9206 }
9207
9208 /* If we fall here, we weren't processing a (static) function call. */
9209 previous_call_static = 0;
9210
9211 /* It can be the keyword THIS */
9212 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9213 {
9214 if (!current_this)
9215 {
9216 parse_error_context
9217 (wfl, "Keyword `this' used outside allowed context");
9218 return 1;
9219 }
7e1376a1
BM
9220 if (ctxp->explicit_constructor_p
9221 && type == current_class)
f63991a8 9222 {
781b0558 9223 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9224 return 1;
9225 }
e04a16fb 9226 /* We have to generate code for intermediate acess */
c2952b01
APB
9227 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9228 {
9229 *where_found = decl = current_this;
9230 *type_found = type = QUAL_DECL_TYPE (decl);
9231 }
4dbf4496
APB
9232 /* We're trying to access the this from somewhere else. Make sure
9233 it's allowed before doing so. */
c2952b01
APB
9234 else
9235 {
4dbf4496
APB
9236 if (!enclosing_context_p (type, current_class))
9237 {
9238 char *p = xstrdup (lang_printable_name (type, 0));
9239 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9240 p, p,
9241 lang_printable_name (current_class, 0));
9242 free (p);
9243 return 1;
9244 }
c2952b01 9245 from_qualified_this = 1;
dba41d30
APB
9246 /* If there's nothing else after that, we need to
9247 produce something now, otherwise, the section of the
9248 code that needs to produce <T>.this will generate
9249 what is necessary. */
9250 if (!TREE_CHAIN (q))
9251 {
9252 decl = build_access_to_thisn (current_class, type, 0);
9253 *where_found = decl = java_complete_tree (decl);
9254 *type_found = type = TREE_TYPE (decl);
9255 }
c2952b01
APB
9256 }
9257
9258 from_type = 0;
e04a16fb
AG
9259 continue;
9260 }
9261
9262 /* 15.10.2 Accessing Superclass Members using SUPER */
9263 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9264 {
9265 tree node;
9266 /* Check on the restricted use of SUPER */
9267 if (METHOD_STATIC (current_function_decl)
9268 || current_class == object_type_node)
9269 {
9270 parse_error_context
9271 (wfl, "Keyword `super' used outside allowed context");
9272 return 1;
9273 }
9274 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9275 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9276 CLASSTYPE_SUPER (current_class),
9277 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9278 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9279 if (decl == error_mark_node)
9280 return 1;
e04a16fb
AG
9281 *type_found = type = QUAL_DECL_TYPE (decl);
9282 from_super = from_type = 1;
9283 continue;
9284 }
9285
9286 /* 15.13.1: Can't search for field name in packages, so we
9287 assume a variable/class name was meant. */
9288 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9289 {
5e942c50
APB
9290 tree name = resolve_package (wfl, &q);
9291 if (name)
9292 {
c2952b01 9293 tree list;
5e942c50 9294 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9295 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9296 out. We're going to search something inside it. */
9297 *type_found = type = TREE_TYPE (decl);
9298 layout_class (type);
9299 from_type = 1;
c2952b01 9300
dde1da72
APB
9301 /* Fix them all the way down, if any are left. */
9302 if (q)
c2952b01 9303 {
dde1da72
APB
9304 list = TREE_CHAIN (q);
9305 while (list)
9306 {
9307 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9308 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9309 list = TREE_CHAIN (list);
9310 }
c2952b01 9311 }
5e942c50 9312 }
e04a16fb 9313 else
5e942c50
APB
9314 {
9315 if (from_super || from_cast)
9316 parse_error_context
9317 ((from_cast ? qual_wfl : wfl),
9318 "No variable `%s' defined in class `%s'",
9319 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9320 lang_printable_name (type, 0));
9321 else
9322 parse_error_context
9323 (qual_wfl, "Undefined variable or class name: `%s'",
9324 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9325 return 1;
9326 }
e04a16fb
AG
9327 }
9328
9329 /* We have a type name. It's been already resolved when the
9330 expression was qualified. */
9331 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9332 {
9333 if (!(decl = QUAL_RESOLUTION (q)))
9334 return 1; /* Error reported already */
9335
c2952b01
APB
9336 /* Sneak preview. If next we see a `new', we're facing a
9337 qualification with resulted in a type being selected
9338 instead of a field. Report the error */
9339 if(TREE_CHAIN (q)
9340 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9341 {
9342 parse_error_context (qual_wfl, "Undefined variable `%s'",
9343 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9344 return 1;
9345 }
9346
e101152f 9347 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
e04a16fb
AG
9348 {
9349 parse_error_context
9350 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9351 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9352 GET_TYPE_NAME (type),
e04a16fb
AG
9353 IDENTIFIER_POINTER (DECL_NAME (decl)),
9354 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9355 return 1;
9356 }
5e942c50 9357 check_deprecation (qual_wfl, decl);
c2952b01 9358
e04a16fb
AG
9359 type = TREE_TYPE (decl);
9360 from_type = 1;
9361 }
9362 /* We resolve and expression name */
9363 else
9364 {
cd531a2e 9365 tree field_decl = NULL_TREE;
e04a16fb
AG
9366
9367 /* If there exists an early resolution, use it. That occurs
9368 only once and we know that there are more things to
9369 come. Don't do that when processing something after SUPER
9370 (we need more thing to be put in place below */
9371 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9372 {
9373 decl = QUAL_RESOLUTION (q);
c877974e 9374 if (!type)
5e942c50 9375 {
7f10c2e2
APB
9376 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9377 {
9378 if (current_this)
9379 *where_found = current_this;
9380 else
9381 {
9382 static_ref_err (qual_wfl, DECL_NAME (decl),
9383 current_class);
9384 return 1;
9385 }
f0f3a777
APB
9386 if (outer_field_access_p (current_class, decl))
9387 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9388 }
c877974e
APB
9389 else
9390 {
9391 *where_found = TREE_TYPE (decl);
9392 if (TREE_CODE (*where_found) == POINTER_TYPE)
9393 *where_found = TREE_TYPE (*where_found);
9394 }
5e942c50 9395 }
b67d701b 9396 }
e04a16fb
AG
9397
9398 /* We have to search for a field, knowing the type of its
9399 container. The flag FROM_TYPE indicates that we resolved
9400 the last member of the expression as a type name, which
5e942c50
APB
9401 means that for the resolution of this field, we'll look
9402 for other errors than if it was resolved as a member of
9403 an other field. */
e04a16fb
AG
9404 else
9405 {
9406 int is_static;
5e942c50
APB
9407 tree field_decl_type; /* For layout */
9408
e04a16fb
AG
9409 if (!from_type && !JREFERENCE_TYPE_P (type))
9410 {
9411 parse_error_context
9412 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9413 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9414 lang_printable_name (type, 0),
e04a16fb
AG
9415 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9416 return 1;
9417 }
9418
dc0b3eff
PB
9419 field_decl = lookup_field_wrapper (type,
9420 EXPR_WFL_NODE (qual_wfl));
4dbf4496 9421
863cd85a
APB
9422 /* Maybe what we're trying to access to is an inner
9423 class, only if decl is a TYPE_DECL. */
9424 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
4dbf4496
APB
9425 {
9426 tree ptr, inner_decl;
9427
9428 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9429 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9430 if (inner_decl)
9431 {
9432 check_inner_class_access (inner_decl, decl, qual_wfl);
9433 type = TREE_TYPE (inner_decl);
9434 decl = inner_decl;
9435 from_type = 1;
9436 continue;
9437 }
9438 }
9439
dc0b3eff 9440 if (field_decl == NULL_TREE)
e04a16fb
AG
9441 {
9442 parse_error_context
2aa11e97 9443 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9444 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9445 GET_TYPE_NAME (type));
e04a16fb
AG
9446 return 1;
9447 }
dc0b3eff
PB
9448 if (field_decl == error_mark_node)
9449 return 1;
5e942c50
APB
9450
9451 /* Layout the type of field_decl, since we may need
c877974e
APB
9452 it. Don't do primitive types or loaded classes. The
9453 situation of non primitive arrays may not handled
9454 properly here. FIXME */
5e942c50
APB
9455 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9456 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9457 else
9458 field_decl_type = TREE_TYPE (field_decl);
9459 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9460 && !CLASS_LOADED_P (field_decl_type)
9461 && !TYPE_ARRAY_P (field_decl_type))
9462 resolve_and_layout (field_decl_type, NULL_TREE);
9463 if (TYPE_ARRAY_P (field_decl_type))
9464 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9465
9466 /* Check on accessibility here */
e101152f
APB
9467 if (not_accessible_p (current_class, field_decl,
9468 TREE_TYPE (decl), from_super))
e04a16fb
AG
9469 {
9470 parse_error_context
9471 (qual_wfl,
9472 "Can't access %s field `%s.%s' from `%s'",
9473 java_accstring_lookup
9474 (get_access_flags_from_decl (field_decl)),
2aa11e97 9475 GET_TYPE_NAME (type),
e04a16fb
AG
9476 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9477 IDENTIFIER_POINTER
9478 (DECL_NAME (TYPE_NAME (current_class))));
9479 return 1;
9480 }
5e942c50 9481 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9482
9483 /* There are things to check when fields are accessed
9484 from type. There are no restrictions on a static
9485 declaration of the field when it is accessed from an
9486 interface */
9487 is_static = FIELD_STATIC (field_decl);
9488 if (!from_super && from_type
c2952b01
APB
9489 && !TYPE_INTERFACE_P (type)
9490 && !is_static
9491 && (current_function_decl
9492 && METHOD_STATIC (current_function_decl)))
e04a16fb 9493 {
7f10c2e2 9494 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9495 return 1;
9496 }
9497 from_cast = from_super = 0;
9498
c2952b01
APB
9499 /* It's an access from a type but it isn't static, we
9500 make it relative to `this'. */
9501 if (!is_static && from_type)
9502 decl = current_this;
9503
5e942c50
APB
9504 /* If we need to generate something to get a proper
9505 handle on what this field is accessed from, do it
9506 now. */
e04a16fb
AG
9507 if (!is_static)
9508 {
c583dd46 9509 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9510 if (decl == error_mark_node)
9511 return 1;
9512 }
9513
9514 /* We want to keep the location were found it, and the type
9515 we found. */
9516 *where_found = decl;
9517 *type_found = type;
9518
c2952b01
APB
9519 /* Generate the correct expression for field access from
9520 qualified this */
9521 if (from_qualified_this)
9522 {
9523 field_decl = build_outer_field_access (qual_wfl, field_decl);
9524 from_qualified_this = 0;
9525 }
9526
e04a16fb
AG
9527 /* This is the decl found and eventually the next one to
9528 search from */
9529 decl = field_decl;
9530 }
e04a16fb
AG
9531 from_type = 0;
9532 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9533
9534 /* Sneak preview. If decl is qualified by a `new', report
9535 the error here to be accurate on the peculiar construct */
9536 if (TREE_CHAIN (q)
9537 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9538 && !JREFERENCE_TYPE_P (type))
9539 {
9540 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9541 lang_printable_name (type, 0));
9542 return 1;
9543 }
e04a16fb 9544 }
dde1da72
APB
9545 /* `q' might have changed due to a after package resolution
9546 re-qualification */
9547 if (!q)
9548 break;
e04a16fb
AG
9549 }
9550 *found_decl = decl;
9551 return 0;
9552}
9553
9554/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
e101152f
APB
9555 can't be accessed from REFERENCE (a record type). If MEMBER
9556 features a protected access, we then use WHERE which, if non null,
9557 holds the type of MEMBER's access that is checked against
9558 6.6.2.1. This function should be used when decl is a field or a
9559 method. */
e04a16fb 9560
be245ac0 9561static int
e101152f 9562not_accessible_p (reference, member, where, from_super)
e04a16fb 9563 tree reference, member;
e101152f 9564 tree where;
e04a16fb
AG
9565 int from_super;
9566{
9567 int access_flag = get_access_flags_from_decl (member);
9568
4dbf4496
APB
9569 /* Inner classes are processed by check_inner_class_access */
9570 if (INNER_CLASS_TYPE_P (reference))
9571 return 0;
9572
e04a16fb
AG
9573 /* Access always granted for members declared public */
9574 if (access_flag & ACC_PUBLIC)
9575 return 0;
9576
9577 /* Check access on protected members */
9578 if (access_flag & ACC_PROTECTED)
9579 {
9580 /* Access granted if it occurs from within the package
9581 containing the class in which the protected member is
9582 declared */
9583 if (class_in_current_package (DECL_CONTEXT (member)))
9584 return 0;
9585
9bbc7d9f
PB
9586 /* If accessed with the form `super.member', then access is granted */
9587 if (from_super)
9588 return 0;
e04a16fb 9589
e101152f
APB
9590 /* If where is active, access was made through a
9591 qualifier. Access is granted if the type of the qualifier is
9592 or is a sublass of the type the access made from (6.6.2.1.) */
9593 if (where && !inherits_from_p (where, reference))
9594 return 1;
9595
9bbc7d9f 9596 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9597 member is declared or a subclass of it. Find the right
9598 context to perform the check */
9599 if (PURE_INNER_CLASS_TYPE_P (reference))
9600 {
9601 while (INNER_CLASS_TYPE_P (reference))
9602 {
9603 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9604 return 0;
9605 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9606 }
9607 }
473e7b07 9608 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9609 return 0;
e04a16fb
AG
9610 return 1;
9611 }
9612
9613 /* Check access on private members. Access is granted only if it
761491c8
APB
9614 occurs from within the class in which it is declared -- that does
9615 it for innerclasses too. */
e04a16fb 9616 if (access_flag & ACC_PRIVATE)
761491c8
APB
9617 {
9618 if (reference == DECL_CONTEXT (member))
9619 return 0;
9620 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9621 return 0;
9622 return 1;
9623 }
e04a16fb
AG
9624
9625 /* Default access are permitted only when occuring within the
9626 package in which the type (REFERENCE) is declared. In other words,
9627 REFERENCE is defined in the current package */
9628 if (ctxp->package)
9629 return !class_in_current_package (reference);
473e7b07 9630
e04a16fb
AG
9631 /* Otherwise, access is granted */
9632 return 0;
9633}
9634
5e942c50
APB
9635/* Test deprecated decl access. */
9636static void
9637check_deprecation (wfl, decl)
9638 tree wfl, decl;
9639{
49f48c71 9640 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9641 /* Complain if the field is deprecated and the file it was defined
9642 in isn't compiled at the same time the file which contains its
9643 use is */
9644 if (DECL_DEPRECATED (decl)
9645 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9646 {
9647 char the [20];
9648 switch (TREE_CODE (decl))
9649 {
9650 case FUNCTION_DECL:
9651 strcpy (the, "method");
9652 break;
9653 case FIELD_DECL:
9654 strcpy (the, "field");
9655 break;
9656 case TYPE_DECL:
9657 strcpy (the, "class");
9658 break;
15fdcfe9 9659 default:
400500c4 9660 abort ();
5e942c50
APB
9661 }
9662 parse_warning_context
9663 (wfl, "The %s `%s' in class `%s' has been deprecated",
9664 the, lang_printable_name (decl, 0),
9665 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9666 }
9667}
9668
e04a16fb
AG
9669/* Returns 1 if class was declared in the current package, 0 otherwise */
9670
9671static int
9672class_in_current_package (class)
9673 tree class;
9674{
9675 static tree cache = NULL_TREE;
9676 int qualified_flag;
9677 tree left;
9678
9679 if (cache == class)
9680 return 1;
9681
9682 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9683
9684 /* If the current package is empty and the name of CLASS is
9685 qualified, class isn't in the current package. If there is a
9686 current package and the name of the CLASS is not qualified, class
9687 isn't in the current package */
0a2138e2 9688 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9689 return 0;
9690
9691 /* If there is not package and the name of CLASS isn't qualified,
9692 they belong to the same unnamed package */
9693 if (!ctxp->package && !qualified_flag)
9694 return 1;
9695
9696 /* Compare the left part of the name of CLASS with the package name */
9697 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9698 if (ctxp->package == left)
9699 {
19e223db
MM
9700 static int initialized_p;
9701 /* Register CACHE with the garbage collector. */
9702 if (!initialized_p)
9703 {
9704 ggc_add_tree_root (&cache, 1);
9705 initialized_p = 1;
9706 }
9707
e04a16fb
AG
9708 cache = class;
9709 return 1;
9710 }
9711 return 0;
9712}
9713
9714/* This function may generate code to access DECL from WHERE. This is
9715 done only if certain conditions meet. */
9716
9717static tree
9718maybe_access_field (decl, where, type)
9719 tree decl, where, type;
9720{
5e942c50
APB
9721 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9722 && !FIELD_STATIC (decl))
e04a16fb 9723 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9724 (type ? type : DECL_CONTEXT (decl)),
9725 DECL_NAME (decl));
e04a16fb
AG
9726 return decl;
9727}
9728
15fdcfe9 9729/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9730 and according to the situation, PRIMARY and WHERE may be
9731 used. IS_STATIC is set to 1 if the invoked function is static. */
9732
9733static tree
e101152f
APB
9734patch_method_invocation (patch, primary, where, from_super,
9735 is_static, ret_decl)
e04a16fb 9736 tree patch, primary, where;
e101152f 9737 int from_super;
e04a16fb 9738 int *is_static;
b9f7e36c 9739 tree *ret_decl;
e04a16fb
AG
9740{
9741 tree wfl = TREE_OPERAND (patch, 0);
9742 tree args = TREE_OPERAND (patch, 1);
9743 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9744 tree list;
22eed1e6 9745 int is_static_flag = 0;
89e09b9a 9746 int is_super_init = 0;
bccaf73a 9747 tree this_arg = NULL_TREE;
35ab11f0 9748 int is_array_clone_call = 0;
e04a16fb
AG
9749
9750 /* Should be overriden if everything goes well. Otherwise, if
9751 something fails, it should keep this value. It stop the
9752 evaluation of a bogus assignment. See java_complete_tree,
9753 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9754 evaluating an assignment */
9755 TREE_TYPE (patch) = error_mark_node;
9756
9757 /* Since lookup functions are messing with line numbers, save the
9758 context now. */
9759 java_parser_context_save_global ();
9760
9761 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9762
9763 /* Resolution of qualified name, excluding constructors */
9764 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9765 {
dde1da72 9766 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9767 /* Extract the last IDENTIFIER of the qualified
9768 expression. This is a wfl and we will use it's location
9769 data during error report. */
9770 identifier_wfl = cut_identifier_in_qualified (wfl);
9771 identifier = EXPR_WFL_NODE (identifier_wfl);
9772
9773 /* Given the context, IDENTIFIER is syntactically qualified
9774 as a MethodName. We need to qualify what's before */
9775 qualify_ambiguous_name (wfl);
dde1da72 9776 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9777
dde1da72
APB
9778 if (resolved == error_mark_node)
9779 PATCH_METHOD_RETURN_ERROR ();
9780
9781 type = GET_SKIP_TYPE (resolved);
9782 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9783
9784 if (JPRIMITIVE_TYPE_P (type))
9785 {
7e51098e
TT
9786 parse_error_context
9787 (identifier_wfl,
9788 "Can't invoke a method on primitive type `%s'",
9789 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9790 PATCH_METHOD_RETURN_ERROR ();
9791 }
9792
dde1da72
APB
9793 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9794 args = nreverse (args);
2c56429a 9795
e04a16fb 9796 /* We're resolving a call from a type */
dde1da72 9797 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9798 {
dde1da72 9799 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9800 {
9801 parse_error_context
781b0558
KG
9802 (identifier_wfl,
9803 "Can't make static reference to method `%s' in interface `%s'",
9804 IDENTIFIER_POINTER (identifier),
e04a16fb 9805 IDENTIFIER_POINTER (name));
b9f7e36c 9806 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9807 }
e04a16fb
AG
9808 if (list && !METHOD_STATIC (list))
9809 {
c2e3db92 9810 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9811 parse_error_context
9812 (identifier_wfl,
9813 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9814 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9815 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9816 free (fct_name);
b9f7e36c 9817 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9818 }
9819 }
e04a16fb 9820 else
dde1da72
APB
9821 this_arg = primary = resolved;
9822
35ab11f0
BM
9823 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9824 is_array_clone_call = 1;
9825
5e942c50 9826 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9827 wfl = identifier_wfl;
9828 }
9829 /* Resolution of simple names, names generated after a primary: or
9830 constructors */
9831 else
9832 {
cd531a2e 9833 tree class_to_search = NULL_TREE;
c2952b01 9834 int lc; /* Looking for Constructor */
e04a16fb
AG
9835
9836 /* We search constructor in their target class */
9837 if (CALL_CONSTRUCTOR_P (patch))
9838 {
22eed1e6
APB
9839 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9840 class_to_search = EXPR_WFL_NODE (wfl);
9841 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9842 this_identifier_node)
9843 class_to_search = NULL_TREE;
9844 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9845 super_identifier_node)
e04a16fb 9846 {
89e09b9a 9847 is_super_init = 1;
22eed1e6
APB
9848 if (CLASSTYPE_SUPER (current_class))
9849 class_to_search =
9850 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9851 else
9852 {
781b0558 9853 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9854 PATCH_METHOD_RETURN_ERROR ();
9855 }
e04a16fb 9856 }
22eed1e6
APB
9857
9858 /* Class to search is NULL if we're searching the current one */
9859 if (class_to_search)
e04a16fb 9860 {
c2952b01
APB
9861 class_to_search = resolve_and_layout (class_to_search, wfl);
9862
22eed1e6
APB
9863 if (!class_to_search)
9864 {
9865 parse_error_context
9866 (wfl, "Class `%s' not found in type declaration",
9867 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9868 PATCH_METHOD_RETURN_ERROR ();
9869 }
9870
5e942c50
APB
9871 /* Can't instantiate an abstract class, but we can
9872 invoke it's constructor. It's use within the `new'
9873 context is denied here. */
9874 if (CLASS_ABSTRACT (class_to_search)
9875 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9876 {
9877 parse_error_context
781b0558
KG
9878 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9879 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9880 PATCH_METHOD_RETURN_ERROR ();
9881 }
c2952b01 9882
22eed1e6 9883 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9884 }
22eed1e6
APB
9885 else
9886 class_to_search = current_class;
e04a16fb
AG
9887 lc = 1;
9888 }
9889 /* This is a regular search in the local class, unless an
9890 alternate class is specified. */
9891 else
9892 {
9893 class_to_search = (where ? where : current_class);
9894 lc = 0;
9895 }
c2952b01 9896
e04a16fb
AG
9897 /* NAME is a simple identifier or comes from a primary. Search
9898 in the class whose declaration contain the method being
9899 invoked. */
c877974e 9900 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9901
c2952b01 9902 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9903 /* Don't continue if no method were found, as the next statement
9904 can't be executed then. */
b9f7e36c
APB
9905 if (!list)
9906 PATCH_METHOD_RETURN_ERROR ();
35ab11f0
BM
9907
9908 if (TYPE_ARRAY_P (class_to_search)
9909 && DECL_NAME (list) == get_identifier ("clone"))
9910 is_array_clone_call = 1;
e04a16fb
AG
9911
9912 /* Check for static reference if non static methods */
9913 if (check_for_static_method_reference (wfl, patch, list,
9914 class_to_search, primary))
b9f7e36c 9915 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9916
165f37bc
APB
9917 /* Check for inner classes creation from illegal contexts */
9918 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9919 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9920 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9921 {
9922 parse_error_context
9923 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9924 lang_printable_name (class_to_search, 0),
9925 (!current_this ? "" :
9926 "; an explicit one must be provided when creating this inner class"));
9927 PATCH_METHOD_RETURN_ERROR ();
9928 }
9929
22eed1e6
APB
9930 /* Non static methods are called with the current object extra
9931 argument. If patch a `new TYPE()', the argument is the value
9932 returned by the object allocator. If method is resolved as a
9933 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9934 args = nreverse (args);
bccaf73a 9935 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9936 {
9937 this_arg = primary ? primary : current_this;
9938
9939 /* If we're using an access method, things are different.
9940 There are two familly of cases:
9941
9942 1) We're not generating bytecodes:
9943
9944 - LIST is non static. It's invocation is transformed from
9945 x(a1,...,an) into this$<n>.x(a1,....an).
9946 - LIST is static. It's invocation is transformed from
9947 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9948
9949 2) We're generating bytecodes:
9950
9951 - LIST is non static. It's invocation is transformed from
9952 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9953 - LIST is static. It's invocation is transformed from
9954 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9955
9956 Of course, this$<n> can be abitrary complex, ranging from
9957 this$0 (the immediate outer context) to
9958 access$0(access$0(...(this$0))).
9959
9960 maybe_use_access_method returns a non zero value if the
dfb99c83 9961 this_arg has to be moved into the (then generated) stub
4dbf4496 9962 argument list. In the meantime, the selected function
dfb99c83 9963 might have be replaced by a generated stub. */
c2952b01 9964 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9965 {
9966 args = tree_cons (NULL_TREE, this_arg, args);
9967 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9968 }
c2952b01 9969 }
e04a16fb 9970 }
b67d701b 9971
e04a16fb
AG
9972 /* Merge point of all resolution schemes. If we have nothing, this
9973 is an error, already signaled */
b9f7e36c
APB
9974 if (!list)
9975 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9976
e04a16fb
AG
9977 /* Check accessibility, position the is_static flag, build and
9978 return the call */
e101152f
APB
9979 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9980 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
35ab11f0
BM
9981 NULL_TREE), from_super)
9982 /* Calls to clone() on array types are permitted as a special-case. */
9983 && !is_array_clone_call)
e101152f
APB
9984 {
9985 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9986 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9987 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9988 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9989 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9990 ? "constructor" : "method");
9991 /* FIXME: WFL yields the wrong message here but I don't know
9992 what else to use. */
9993 parse_error_context (wfl,
9994 "Can't access %s %s `%s.%s' from `%s'",
9995 access, what, klass, fct_name, refklass);
b9f7e36c 9996 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9997 }
5e942c50 9998 check_deprecation (wfl, list);
22eed1e6 9999
c2952b01
APB
10000 /* If invoking a innerclass constructor, there are hidden parameters
10001 to pass */
10002 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10003 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10004 {
10005 /* And make sure we add the accessed local variables to be saved
10006 in field aliases. */
10007 args = build_alias_initializer_parameter_list
10008 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10009
da632f2c 10010 /* Secretly pass the current_this/primary as a second argument */
165f37bc 10011 if (primary || current_this)
f8b93ea7
APB
10012 {
10013 tree extra_arg;
10014 tree this_type = (current_this ?
10015 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10016 /* Method's (list) enclosing context */
10017 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10018 /* If we have a primary, use it. */
10019 if (primary)
10020 extra_arg = primary;
10021 /* The current `this' is an inner class but isn't a direct
10022 enclosing context for the inner class we're trying to
10023 create. Build an access to the proper enclosing context
10024 and use it. */
10025 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10026 && this_type != TREE_TYPE (mec))
10027 {
10028
10029 extra_arg = build_access_to_thisn (current_class,
10030 TREE_TYPE (mec), 0);
10031 extra_arg = java_complete_tree (extra_arg);
10032 }
10033 /* Otherwise, just use the current `this' as an enclosing
10034 context. */
10035 else
10036 extra_arg = current_this;
10037 args = tree_cons (NULL_TREE, extra_arg, args);
10038 }
165f37bc
APB
10039 else
10040 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10041 }
10042
152de068
APB
10043 /* This handles the situation where a constructor invocation needs
10044 to have an enclosing context passed as a second parameter (the
10045 constructor is one of an inner class. We extract it from the
10046 current function. */
10047 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10048 {
10049 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10050 tree extra_arg;
10051
10052 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10053 {
10054 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10055 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10056 }
10057 else
10058 {
10059 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10060 extra_arg =
10061 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10062 extra_arg = java_complete_tree (extra_arg);
10063 }
10064 args = tree_cons (NULL_TREE, extra_arg, args);
10065 }
10066
22eed1e6 10067 is_static_flag = METHOD_STATIC (list);
dba41d30 10068 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10069 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10070
c3f2a476
APB
10071 /* In the context of an explicit constructor invocation, we can't
10072 invoke any method relying on `this'. Exceptions are: we're
10073 invoking a static function, primary exists and is not the current
10074 this, we're creating a new object. */
22eed1e6 10075 if (ctxp->explicit_constructor_p
c3f2a476
APB
10076 && !is_static_flag
10077 && (!primary || primary == current_this)
10078 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10079 {
781b0558 10080 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10081 PATCH_METHOD_RETURN_ERROR ();
10082 }
e04a16fb 10083 java_parser_context_restore_global ();
22eed1e6
APB
10084 if (is_static)
10085 *is_static = is_static_flag;
b9f7e36c
APB
10086 /* Sometimes, we want the decl of the selected method. Such as for
10087 EH checking */
10088 if (ret_decl)
10089 *ret_decl = list;
89e09b9a
PB
10090 patch = patch_invoke (patch, list, args);
10091 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10092 {
c2952b01
APB
10093 tree finit_parms, finit_call;
10094
c00f0fb2 10095 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10096 finit_parms = build_alias_initializer_parameter_list
10097 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10098
c2952b01
APB
10099 finit_call =
10100 build_method_invocation (build_wfl_node (finit_identifier_node),
10101 finit_parms);
10102
10103 /* Generate the code used to initialize fields declared with an
10104 initialization statement and build a compound statement along
10105 with the super constructor invocation. */
89e09b9a
PB
10106 patch = build (COMPOUND_EXPR, void_type_node, patch,
10107 java_complete_tree (finit_call));
10108 CAN_COMPLETE_NORMALLY (patch) = 1;
10109 }
10110 return patch;
e04a16fb
AG
10111}
10112
10113/* Check that we're not trying to do a static reference to a method in
10114 non static method. Return 1 if it's the case, 0 otherwise. */
10115
10116static int
10117check_for_static_method_reference (wfl, node, method, where, primary)
10118 tree wfl, node, method, where, primary;
10119{
10120 if (METHOD_STATIC (current_function_decl)
10121 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10122 {
c2e3db92 10123 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10124 parse_error_context
10125 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10126 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10127 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10128 free (fct_name);
10129 return 1;
10130 }
10131 return 0;
10132}
10133
c2952b01
APB
10134/* Fix the invocation of *MDECL if necessary in the case of a
10135 invocation from an inner class. *THIS_ARG might be modified
10136 appropriately and an alternative access to *MDECL might be
10137 returned. */
10138
10139static int
10140maybe_use_access_method (is_super_init, mdecl, this_arg)
10141 int is_super_init;
10142 tree *mdecl, *this_arg;
10143{
10144 tree ctx;
10145 tree md = *mdecl, ta = *this_arg;
10146 int to_return = 0;
10147 int non_static_context = !METHOD_STATIC (md);
10148
10149 if (is_super_init
165f37bc
APB
10150 || DECL_CONTEXT (md) == current_class
10151 || !PURE_INNER_CLASS_TYPE_P (current_class)
10152 || DECL_FINIT_P (md))
c2952b01
APB
10153 return 0;
10154
10155 /* If we're calling a method found in an enclosing class, generate
10156 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10157 invoking a static method. Note that if MD's type is unrelated to
10158 CURRENT_CLASS, then the current this can be used. */
c2952b01 10159
2cb3951d 10160 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10161 {
10162 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10163 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10164 {
10165 ta = build_current_thisn (current_class);
10166 ta = build_wfl_node (ta);
10167 }
10168 else
10169 {
10170 tree type = ctx;
10171 while (type)
10172 {
10173 maybe_build_thisn_access_method (type);
4dbf4496 10174 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10175 {
10176 ta = build_access_to_thisn (ctx, type, 0);
10177 break;
10178 }
10179 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10180 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10181 }
10182 }
10183 ta = java_complete_tree (ta);
10184 }
10185
10186 /* We might have to use an access method to get to MD. We can
10187 break the method access rule as far as we're not generating
10188 bytecode */
10189 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10190 {
10191 md = build_outer_method_access_method (md);
10192 to_return = 1;
10193 }
10194
10195 *mdecl = md;
10196 *this_arg = ta;
10197
10198 /* Returnin a non zero value indicates we were doing a non static
10199 method invokation that is now a static invocation. It will have
10200 callee displace `this' to insert it in the regular argument
10201 list. */
10202 return (non_static_context && to_return);
10203}
10204
e04a16fb
AG
10205/* Patch an invoke expression METHOD and ARGS, based on its invocation
10206 mode. */
10207
10208static tree
89e09b9a 10209patch_invoke (patch, method, args)
e04a16fb 10210 tree patch, method, args;
e04a16fb
AG
10211{
10212 tree dtable, func;
0a2138e2 10213 tree original_call, t, ta;
e815887f 10214 tree cond = NULL_TREE;
e04a16fb 10215
5e942c50
APB
10216 /* Last step for args: convert build-in types. If we're dealing with
10217 a new TYPE() type call, the first argument to the constructor
e815887f 10218 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10219 `new' */
10220 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10221 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10222 t = TREE_CHAIN (t);
ac825856
APB
10223 for (ta = args; t != end_params_node && ta;
10224 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10225 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10226 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10227 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10228
10229 /* Resolve unresolved returned type isses */
10230 t = TREE_TYPE (TREE_TYPE (method));
10231 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10232 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10233
e8fc7396 10234 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10235 func = method;
10236 else
e04a16fb 10237 {
15fdcfe9 10238 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10239 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10240 {
10241 case INVOKE_VIRTUAL:
10242 dtable = invoke_build_dtable (0, args);
10243 func = build_invokevirtual (dtable, method);
10244 break;
b9f7e36c 10245
e815887f
TT
10246 case INVOKE_NONVIRTUAL:
10247 /* If the object for the method call is null, we throw an
10248 exception. We don't do this if the object is the current
10249 method's `this'. In other cases we just rely on an
10250 optimization pass to eliminate redundant checks. */
10251 if (TREE_VALUE (args) != current_this)
10252 {
10253 /* We use a SAVE_EXPR here to make sure we only evaluate
10254 the new `self' expression once. */
10255 tree save_arg = save_expr (TREE_VALUE (args));
10256 TREE_VALUE (args) = save_arg;
10257 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10258 null_pointer_node);
10259 }
10260 /* Fall through. */
10261
15fdcfe9
PB
10262 case INVOKE_SUPER:
10263 case INVOKE_STATIC:
10264 func = build_known_method_ref (method, TREE_TYPE (method),
10265 DECL_CONTEXT (method),
10266 signature, args);
10267 break;
e04a16fb 10268
15fdcfe9
PB
10269 case INVOKE_INTERFACE:
10270 dtable = invoke_build_dtable (1, args);
173f556c 10271 func = build_invokeinterface (dtable, method);
15fdcfe9 10272 break;
5e942c50 10273
15fdcfe9 10274 default:
400500c4 10275 abort ();
15fdcfe9
PB
10276 }
10277
10278 /* Ensure self_type is initialized, (invokestatic). FIXME */
10279 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10280 }
10281
e04a16fb
AG
10282 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10283 TREE_OPERAND (patch, 0) = func;
10284 TREE_OPERAND (patch, 1) = args;
10285 original_call = patch;
10286
e815887f 10287 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10288 returned value is the first argument to the constructor. We build
10289 a COMPOUND_EXPR and use saved expression so that the overall NEW
10290 expression value is a pointer to a newly created and initialized
10291 class. */
10292 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10293 {
10294 tree class = DECL_CONTEXT (method);
10295 tree c1, saved_new, size, new;
e8fc7396 10296 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10297 {
10298 TREE_TYPE (patch) = build_pointer_type (class);
10299 return patch;
10300 }
e04a16fb
AG
10301 if (!TYPE_SIZE (class))
10302 safe_layout_class (class);
10303 size = size_in_bytes (class);
10304 new = build (CALL_EXPR, promote_type (class),
10305 build_address_of (alloc_object_node),
10306 tree_cons (NULL_TREE, build_class_ref (class),
10307 build_tree_list (NULL_TREE,
10308 size_in_bytes (class))),
10309 NULL_TREE);
10310 saved_new = save_expr (new);
10311 c1 = build_tree_list (NULL_TREE, saved_new);
10312 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10313 TREE_OPERAND (original_call, 1) = c1;
10314 TREE_SET_CODE (original_call, CALL_EXPR);
10315 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10316 }
e815887f
TT
10317
10318 /* If COND is set, then we are building a check to see if the object
10319 is NULL. */
10320 if (cond != NULL_TREE)
10321 {
10322 /* We have to make the `then' branch a compound expression to
10323 make the types turn out right. This seems bizarre. */
10324 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10325 build (COMPOUND_EXPR, TREE_TYPE (patch),
10326 build (CALL_EXPR, void_type_node,
10327 build_address_of (soft_nullpointer_node),
10328 NULL_TREE, NULL_TREE),
10329 (FLOAT_TYPE_P (TREE_TYPE (patch))
10330 ? build_real (TREE_TYPE (patch), dconst0)
10331 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10332 integer_zero_node))),
10333 patch);
10334 TREE_SIDE_EFFECTS (patch) = 1;
10335 }
10336
e04a16fb
AG
10337 return patch;
10338}
10339
10340static int
10341invocation_mode (method, super)
10342 tree method;
10343 int super;
10344{
10345 int access = get_access_flags_from_decl (method);
10346
22eed1e6
APB
10347 if (super)
10348 return INVOKE_SUPER;
10349
e815887f 10350 if (access & ACC_STATIC)
e04a16fb
AG
10351 return INVOKE_STATIC;
10352
e815887f
TT
10353 /* We have to look for a constructor before we handle nonvirtual
10354 calls; otherwise the constructor will look nonvirtual. */
10355 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10356 return INVOKE_STATIC;
e815887f
TT
10357
10358 if (access & ACC_FINAL || access & ACC_PRIVATE)
10359 return INVOKE_NONVIRTUAL;
10360
10361 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10362 return INVOKE_NONVIRTUAL;
10363
e04a16fb
AG
10364 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10365 return INVOKE_INTERFACE;
22eed1e6 10366
e04a16fb
AG
10367 return INVOKE_VIRTUAL;
10368}
10369
b67d701b
PB
10370/* Retrieve a refined list of matching methods. It covers the step
10371 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10372
10373static tree
10374lookup_method_invoke (lc, cl, class, name, arg_list)
10375 int lc;
10376 tree cl;
10377 tree class, name, arg_list;
10378{
de4c7b02 10379 tree atl = end_params_node; /* Arg Type List */
c877974e 10380 tree method, signature, list, node;
49f48c71 10381 const char *candidates; /* Used for error report */
b5b8a0e7 10382 char *dup;
e04a16fb 10383
5e942c50 10384 /* Fix the arguments */
e04a16fb
AG
10385 for (node = arg_list; node; node = TREE_CHAIN (node))
10386 {
e3884b71 10387 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10388 /* Non primitive type may have to be resolved */
e3884b71 10389 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10390 resolve_and_layout (current_arg, NULL_TREE);
10391 /* And promoted */
b67d701b 10392 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10393 current_arg = promote_type (current_arg);
5e942c50 10394 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10395 }
e04a16fb 10396
c2952b01
APB
10397 /* Presto. If we're dealing with an anonymous class and a
10398 constructor call, generate the right constructor now, since we
10399 know the arguments' types. */
10400
10401 if (lc && ANONYMOUS_CLASS_P (class))
10402 craft_constructor (TYPE_NAME (class), atl);
10403
5e942c50
APB
10404 /* Find all candidates and then refine the list, searching for the
10405 most specific method. */
10406 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10407 list = find_most_specific_methods_list (list);
b67d701b
PB
10408 if (list && !TREE_CHAIN (list))
10409 return TREE_VALUE (list);
e04a16fb 10410
b67d701b
PB
10411 /* Issue an error. List candidates if any. Candidates are listed
10412 only if accessible (non accessible methods may end-up here for
10413 the sake of a better error report). */
10414 candidates = NULL;
10415 if (list)
e04a16fb 10416 {
e04a16fb 10417 tree current;
b67d701b 10418 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10419 for (current = list; current; current = TREE_CHAIN (current))
10420 {
b67d701b
PB
10421 tree cm = TREE_VALUE (current);
10422 char string [4096];
e101152f 10423 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
b67d701b 10424 continue;
b67d701b 10425 sprintf
22eed1e6
APB
10426 (string, " `%s' in `%s'%s",
10427 get_printable_method_name (cm),
b67d701b
PB
10428 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10429 (TREE_CHAIN (current) ? "\n" : ""));
10430 obstack_grow (&temporary_obstack, string, strlen (string));
10431 }
10432 obstack_1grow (&temporary_obstack, '\0');
10433 candidates = obstack_finish (&temporary_obstack);
10434 }
10435 /* Issue the error message */
c877974e
APB
10436 method = make_node (FUNCTION_TYPE);
10437 TYPE_ARG_TYPES (method) = atl;
b67d701b 10438 signature = build_java_argument_signature (method);
c63b98cd 10439 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10440 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10441 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10442 (lc ? dup : IDENTIFIER_POINTER (name)),
10443 IDENTIFIER_POINTER (signature), dup,
b67d701b 10444 (candidates ? candidates : ""));
b5b8a0e7 10445 free (dup);
b67d701b
PB
10446 return NULL_TREE;
10447}
10448
5e942c50
APB
10449/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10450 when we're looking for a constructor. */
b67d701b
PB
10451
10452static tree
5e942c50
APB
10453find_applicable_accessible_methods_list (lc, class, name, arglist)
10454 int lc;
b67d701b
PB
10455 tree class, name, arglist;
10456{
ad69b5b6
BM
10457 static struct hash_table t, *searched_classes = NULL;
10458 static int search_not_done = 0;
b67d701b
PB
10459 tree list = NULL_TREE, all_list = NULL_TREE;
10460
ad69b5b6
BM
10461 /* Check the hash table to determine if this class has been searched
10462 already. */
10463 if (searched_classes)
10464 {
10465 if (hash_lookup (searched_classes,
10466 (const hash_table_key) class, FALSE, NULL))
10467 return NULL;
10468 }
10469 else
10470 {
10471 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10472 java_hash_compare_tree_node);
10473 searched_classes = &t;
10474 }
10475
10476 search_not_done++;
10477 hash_lookup (searched_classes,
0c2b8145 10478 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10479
c2952b01
APB
10480 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10481 {
10482 load_class (class, 1);
10483 safe_layout_class (class);
10484 }
10485
1982388a 10486 /* Search interfaces */
9a7ab4b3
APB
10487 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10488 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10489 {
1982388a
APB
10490 int i, n;
10491 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10492 search_applicable_methods_list (lc, TYPE_METHODS (class),
10493 name, arglist, &list, &all_list);
1982388a 10494 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10495 for (i = 1; i < n; i++)
b67d701b 10496 {
de0b553f
APB
10497 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10498 tree rlist;
10499
de0b553f
APB
10500 rlist = find_applicable_accessible_methods_list (lc, t, name,
10501 arglist);
165f37bc 10502 list = chainon (rlist, list);
e04a16fb 10503 }
e04a16fb 10504 }
1982388a
APB
10505 /* Search classes */
10506 else
c2952b01 10507 {
165f37bc
APB
10508 tree sc = class;
10509 int seen_inner_class = 0;
c2952b01
APB
10510 search_applicable_methods_list (lc, TYPE_METHODS (class),
10511 name, arglist, &list, &all_list);
10512
94807d33
APB
10513 /* When looking finit$ or class$, we turn LC to 1 so that we
10514 only search in class. Note that we should have found
10515 something at this point. */
10516 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
493d561d
APB
10517 {
10518 lc = 1;
10519 if (!list)
400500c4 10520 abort ();
493d561d
APB
10521 }
10522
165f37bc
APB
10523 /* We must search all interfaces of this class */
10524 if (!lc)
10525 {
10526 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10527 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10528 for (i = 1; i < n; i++)
10529 {
10530 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10531 if (t != object_type_node)
30a3caef
ZW
10532 {
10533 tree rlist
10534 = find_applicable_accessible_methods_list (lc, t,
10535 name, arglist);
10536 list = chainon (rlist, list);
10537 }
165f37bc 10538 }
165f37bc
APB
10539 }
10540
c2952b01
APB
10541 /* Search enclosing context of inner classes before looking
10542 ancestors up. */
10543 while (!lc && INNER_CLASS_TYPE_P (class))
10544 {
165f37bc
APB
10545 tree rlist;
10546 seen_inner_class = 1;
c2952b01 10547 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10548 rlist = find_applicable_accessible_methods_list (lc, class,
10549 name, arglist);
10550 list = chainon (rlist, list);
c2952b01 10551 }
165f37bc
APB
10552
10553 if (!lc && seen_inner_class
10554 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10555 class = CLASSTYPE_SUPER (sc);
10556 else
10557 class = sc;
10558
ad69b5b6
BM
10559 /* Search superclass */
10560 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10561 {
10562 tree rlist;
10563 class = CLASSTYPE_SUPER (class);
10564 rlist = find_applicable_accessible_methods_list (lc, class,
10565 name, arglist);
10566 list = chainon (rlist, list);
10567 }
10568 }
10569
10570 search_not_done--;
10571
10572 /* We're done. Reset the searched classes list and finally search
10573 java.lang.Object if it wasn't searched already. */
10574 if (!search_not_done)
10575 {
10576 if (!lc
10577 && TYPE_METHODS (object_type_node)
10578 && !hash_lookup (searched_classes,
10579 (const hash_table_key) object_type_node,
10580 FALSE, NULL))
10581 {
10582 search_applicable_methods_list (lc,
10583 TYPE_METHODS (object_type_node),
10584 name, arglist, &list, &all_list);
10585 }
10586 hash_table_free (searched_classes);
10587 searched_classes = NULL;
c2952b01 10588 }
1982388a 10589
b67d701b
PB
10590 /* Either return the list obtained or all selected (but
10591 inaccessible) methods for better error report. */
10592 return (!list ? all_list : list);
10593}
e04a16fb 10594
ad69b5b6 10595/* Effectively search for the appropriate method in method */
1982388a
APB
10596
10597static void
c2952b01 10598search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10599 int lc;
10600 tree method, name, arglist;
10601 tree *list, *all_list;
10602{
10603 for (; method; method = TREE_CHAIN (method))
10604 {
10605 /* When dealing with constructor, stop here, otherwise search
10606 other classes */
10607 if (lc && !DECL_CONSTRUCTOR_P (method))
10608 continue;
10609 else if (!lc && (DECL_CONSTRUCTOR_P (method)
c4faeb92 10610 || (DECL_NAME (method) != name)))
1982388a 10611 continue;
c7303e41 10612
1982388a
APB
10613 if (argument_types_convertible (method, arglist))
10614 {
10615 /* Retain accessible methods only */
10616 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
e101152f 10617 method, NULL_TREE, 0))
1982388a
APB
10618 *list = tree_cons (NULL_TREE, method, *list);
10619 else
10620 /* Also retain all selected method here */
10621 *all_list = tree_cons (NULL_TREE, method, *list);
10622 }
10623 }
ad69b5b6 10624}
1982388a 10625
b67d701b
PB
10626/* 15.11.2.2 Choose the Most Specific Method */
10627
10628static tree
10629find_most_specific_methods_list (list)
10630 tree list;
10631{
10632 int max = 0;
9a7ab4b3 10633 int abstract, candidates;
b67d701b
PB
10634 tree current, new_list = NULL_TREE;
10635 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10636 {
b67d701b
PB
10637 tree method;
10638 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10639
10640 for (method = list; method; method = TREE_CHAIN (method))
10641 {
0c2b8145 10642 tree method_v, current_v;
b67d701b
PB
10643 /* Don't test a method against itself */
10644 if (method == current)
10645 continue;
10646
0c2b8145
APB
10647 method_v = TREE_VALUE (method);
10648 current_v = TREE_VALUE (current);
10649
10650 /* Compare arguments and location where methods where declared */
10651 if (argument_types_convertible (method_v, current_v))
b67d701b 10652 {
0c2b8145
APB
10653 if (valid_method_invocation_conversion_p
10654 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10655 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10656 && enclosing_context_p (DECL_CONTEXT (method_v),
10657 DECL_CONTEXT (current_v))))
10658 {
10659 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10660 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10661 max = (v > max ? v : max);
10662 }
b67d701b
PB
10663 }
10664 }
e04a16fb
AG
10665 }
10666
b67d701b 10667 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10668 for (current = list, abstract = -1, candidates = -1;
10669 current; current = TREE_CHAIN (current))
b67d701b 10670 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10671 {
10672 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10673 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10674 candidates++;
10675 }
b67d701b 10676
165f37bc
APB
10677 /* If we have several and they're all abstract, just pick the
10678 closest one. */
9a7ab4b3
APB
10679 if (candidates > 0 && (candidates == abstract))
10680 {
10681 new_list = nreverse (new_list);
10682 TREE_CHAIN (new_list) = NULL_TREE;
10683 }
165f37bc 10684
cab8e2bd
BM
10685 /* We have several (we couldn't find a most specific), all but one
10686 are abstract, we pick the only non abstract one. */
10687 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10688 {
9a7ab4b3
APB
10689 for (current = new_list; current; current = TREE_CHAIN (current))
10690 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10691 {
10692 TREE_CHAIN (current) = NULL_TREE;
10693 new_list = current;
10694 }
165f37bc
APB
10695 }
10696
b67d701b
PB
10697 /* If we can't find one, lower expectations and try to gather multiple
10698 maximally specific methods */
165f37bc 10699 while (!new_list && max)
b67d701b
PB
10700 {
10701 while (--max > 0)
10702 {
10703 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10704 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10705 }
b67d701b
PB
10706 }
10707
10708 return new_list;
e04a16fb
AG
10709}
10710
b67d701b
PB
10711/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10712 converted by method invocation conversion (5.3) to the type of the
10713 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10714 to change less often than M1. */
e04a16fb 10715
b67d701b
PB
10716static int
10717argument_types_convertible (m1, m2_or_arglist)
10718 tree m1, m2_or_arglist;
e04a16fb 10719{
b67d701b
PB
10720 static tree m2_arg_value = NULL_TREE;
10721 static tree m2_arg_cache = NULL_TREE;
19e223db 10722 static int initialized_p;
e04a16fb 10723
b67d701b 10724 register tree m1_arg, m2_arg;
e04a16fb 10725
19e223db
MM
10726 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10727 collector. */
10728 if (!initialized_p)
10729 {
10730 ggc_add_tree_root (&m2_arg_value, 1);
10731 ggc_add_tree_root (&m2_arg_cache, 1);
10732 initialized_p = 1;
10733 }
10734
c2952b01 10735 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10736
b67d701b
PB
10737 if (m2_arg_value == m2_or_arglist)
10738 m2_arg = m2_arg_cache;
10739 else
10740 {
10741 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10742 argument types */
10743 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10744 {
10745 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10746 if (!METHOD_STATIC (m2_or_arglist))
10747 m2_arg = TREE_CHAIN (m2_arg);
10748 }
10749 else
10750 m2_arg = m2_or_arglist;
e04a16fb 10751
b67d701b
PB
10752 m2_arg_value = m2_or_arglist;
10753 m2_arg_cache = m2_arg;
10754 }
e04a16fb 10755
de4c7b02 10756 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10757 {
c877974e 10758 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10759 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10760 TREE_VALUE (m2_arg)))
10761 break;
10762 m1_arg = TREE_CHAIN (m1_arg);
10763 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10764 }
de4c7b02 10765 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10766}
10767
10768/* Qualification routines */
10769
10770static void
10771qualify_ambiguous_name (id)
10772 tree id;
10773{
cd531a2e
KG
10774 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10775 saved_current_class;
d8fccff5 10776 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10777 int code;
e04a16fb
AG
10778
10779 /* We first qualify the first element, then derive qualification of
10780 others based on the first one. If the first element is qualified
10781 by a resolution (field or type), this resolution is stored in the
10782 QUAL_RESOLUTION of the qual element being examined. We need to
10783 save the current_class since the use of SUPER might change the
10784 its value. */
10785 saved_current_class = current_class;
10786 qual = EXPR_WFL_QUALIFICATION (id);
10787 do {
10788
10789 /* Simple qualified expression feature a qual_wfl that is a
10790 WFL. Expression derived from a primary feature more complicated
10791 things like a CALL_EXPR. Expression from primary need to be
10792 worked out to extract the part on which the qualification will
10793 take place. */
10794 qual_wfl = QUAL_WFL (qual);
10795 switch (TREE_CODE (qual_wfl))
10796 {
10797 case CALL_EXPR:
10798 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10799 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10800 {
10801 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10802 qual_wfl = QUAL_WFL (qual);
10803 }
10804 break;
d8fccff5 10805 case NEW_ARRAY_EXPR:
c2952b01 10806 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10807 qual = TREE_CHAIN (qual);
1a6d4fb7 10808 again = new_array_found = 1;
d8fccff5 10809 continue;
e04a16fb 10810 case CONVERT_EXPR:
f2760b27
APB
10811 break;
10812 case NEW_CLASS_EXPR:
e04a16fb
AG
10813 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10814 break;
c583dd46
APB
10815 case ARRAY_REF:
10816 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10817 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10818 break;
8576f094
APB
10819 case STRING_CST:
10820 qual = TREE_CHAIN (qual);
10821 qual_wfl = QUAL_WFL (qual);
10822 break;
165f37bc
APB
10823 case CLASS_LITERAL:
10824 qual = TREE_CHAIN (qual);
10825 qual_wfl = QUAL_WFL (qual);
10826 break;
0a2138e2
APB
10827 default:
10828 /* Fix for -Wall. Just break doing nothing */
10829 break;
e04a16fb 10830 }
8576f094 10831
e04a16fb
AG
10832 ptr_type = current_class;
10833 again = 0;
8576f094
APB
10834 code = TREE_CODE (qual_wfl);
10835
10836 /* Pos evaluation: non WFL leading expression nodes */
10837 if (code == CONVERT_EXPR
10838 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10839 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10840
cd7c5840
APB
10841 else if (code == INTEGER_CST)
10842 name = qual_wfl;
10843
ac22f9cb 10844 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10845 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10846 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10847
c2952b01
APB
10848 else if (code == TREE_LIST)
10849 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10850
37feda7d
APB
10851 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10852 || code == PLUS_EXPR)
8576f094
APB
10853 {
10854 qual = TREE_CHAIN (qual);
10855 qual_wfl = QUAL_WFL (qual);
10856 again = 1;
10857 }
10858 else
f441f671
APB
10859 {
10860 name = EXPR_WFL_NODE (qual_wfl);
10861 if (!name)
10862 {
10863 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10864 again = 1;
10865 }
10866 }
10867
e04a16fb
AG
10868 /* If we have a THIS (from a primary), we set the context accordingly */
10869 if (name == this_identifier_node)
10870 {
6e22695a
APB
10871 /* This isn't really elegant. One more added irregularity
10872 before I start using COMPONENT_REF (hopefully very soon.) */
10873 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10874 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10875 EXPR_WITH_FILE_LOCATION
10876 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10877 this_identifier_node)
10878 {
10879 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10880 qual = EXPR_WFL_QUALIFICATION (qual);
10881 }
e04a16fb
AG
10882 qual = TREE_CHAIN (qual);
10883 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10884 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10885 again = 1;
10886 else
10887 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10888 this_found = 1;
10889 }
10890 /* If we have a SUPER, we set the context accordingly */
10891 if (name == super_identifier_node)
10892 {
10893 current_class = CLASSTYPE_SUPER (ptr_type);
10894 /* Check that there is such a thing as a super class. If not,
10895 return. The error will be caught later on, during the
10896 resolution */
10897 if (!current_class)
10898 {
10899 current_class = saved_current_class;
10900 return;
10901 }
10902 qual = TREE_CHAIN (qual);
10903 /* Do one more interation to set things up */
10904 super_found = again = 1;
10905 }
10906 } while (again);
10907
f2760b27
APB
10908 /* If name appears within the scope of a local variable declaration
10909 or parameter declaration, then it is an expression name. We don't
10910 carry this test out if we're in the context of the use of SUPER
10911 or THIS */
cd7c5840
APB
10912 if (!this_found && !super_found
10913 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10914 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10915 {
10916 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10917 QUAL_RESOLUTION (qual) = decl;
10918 }
10919
10920 /* If within the class/interface NAME was found to be used there
10921 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10922 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10923 address length, it is OK. */
10924 else if ((decl = lookup_field_wrapper (ptr_type, name))
10925 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10926 {
10927 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10928 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10929 }
10930
1a6d4fb7 10931 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10932 - NAME is a class/interface declared within the compilation
10933 unit containing NAME,
10934 - NAME is imported via a single-type-import declaration,
10935 - NAME is declared in an another compilation unit of the package
10936 of the compilation unit containing NAME,
10937 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10938 of the compilation unit containing NAME.
10939 - NAME is actually a STRING_CST. */
cd7c5840
APB
10940 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10941 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10942 {
10943 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10944 QUAL_RESOLUTION (qual) = decl;
10945 }
10946
f2760b27 10947 /* Method call, array references and cast are expression name */
9bbc7d9f 10948 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10949 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10950 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10951 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10952
10953 /* Check here that NAME isn't declared by more than one
10954 type-import-on-demand declaration of the compilation unit
10955 containing NAME. FIXME */
10956
10957 /* Otherwise, NAME is reclassified as a package name */
10958 else
10959 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10960
10961 /* Propagate the qualification accross other components of the
10962 qualified name */
10963 for (qual = TREE_CHAIN (qual); qual;
10964 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10965 {
10966 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10967 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10968 else
10969 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10970 }
10971
10972 /* Store the global qualification for the ambiguous part of ID back
10973 into ID fields */
10974 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10975 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10976 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10977 RESOLVE_TYPE_NAME_P (id) = 1;
10978 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10979 RESOLVE_PACKAGE_NAME_P (id) = 1;
10980
10981 /* Restore the current class */
10982 current_class = saved_current_class;
10983}
10984
10985static int
10986breakdown_qualified (left, right, source)
10987 tree *left, *right, source;
10988{
63ad61ed 10989 char *p, *base;
e04a16fb
AG
10990 int l = IDENTIFIER_LENGTH (source);
10991
63ad61ed
ZW
10992 base = alloca (l + 1);
10993 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10994
e04a16fb 10995 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10996 p = base + l - 1;
e04a16fb
AG
10997 while (*p != '.' && p != base)
10998 p--;
10999
11000 /* We didn't find a '.'. Return an error */
11001 if (p == base)
11002 return 1;
11003
11004 *p = '\0';
11005 if (right)
11006 *right = get_identifier (p+1);
63ad61ed 11007 *left = get_identifier (base);
e04a16fb
AG
11008
11009 return 0;
11010}
11011
a648f4e4
BM
11012/* Return TRUE if two classes are from the same package. */
11013
11014static int
11015in_same_package (name1, name2)
11016 tree name1, name2;
11017{
11018 tree tmp;
11019 tree pkg1;
11020 tree pkg2;
11021
11022 if (TREE_CODE (name1) == TYPE_DECL)
11023 name1 = DECL_NAME (name1);
11024 if (TREE_CODE (name2) == TYPE_DECL)
11025 name2 = DECL_NAME (name2);
11026
11027 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11028 /* One in empty package. */
11029 return 0;
11030
11031 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11032 /* Both in empty package. */
11033 return 1;
11034
11035 breakdown_qualified (&pkg1, &tmp, name1);
11036 breakdown_qualified (&pkg2, &tmp, name2);
11037
11038 return (pkg1 == pkg2);
11039}
11040
e04a16fb 11041/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11042 local variable decls if present.
11043 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11044
11045static tree
11046java_complete_tree (node)
11047 tree node;
5b09b33e
PB
11048{
11049 node = java_complete_lhs (node);
c7303e41 11050 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11051 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11052 && !flag_emit_xref)
5b09b33e
PB
11053 {
11054 tree value = DECL_INITIAL (node);
11055 DECL_INITIAL (node) = NULL_TREE;
11056 value = fold_constant_for_init (value, node);
11057 DECL_INITIAL (node) = value;
11058 if (value != NULL_TREE)
c2952b01
APB
11059 {
11060 /* fold_constant_for_init sometimes widen the original type
11061 of the constant (i.e. byte to int.) It's not desirable,
11062 especially if NODE is a function argument. */
11063 if (TREE_CODE (value) == INTEGER_CST
11064 && TREE_TYPE (node) != TREE_TYPE (value))
11065 return convert (TREE_TYPE (node), value);
11066 else
11067 return value;
11068 }
c7303e41
APB
11069 else
11070 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11071 }
11072 return node;
11073}
11074
2aa11e97
APB
11075static tree
11076java_stabilize_reference (node)
11077 tree node;
11078{
11079 if (TREE_CODE (node) == COMPOUND_EXPR)
11080 {
11081 tree op0 = TREE_OPERAND (node, 0);
11082 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11083 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11084 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11085 return node;
11086 }
5cbdba64 11087 return stabilize_reference (node);
2aa11e97
APB
11088}
11089
5b09b33e
PB
11090/* Patch tree nodes in a function body. When a BLOCK is found, push
11091 local variable decls if present.
11092 Same as java_complete_tree, but does not resolve static finals to values. */
11093
11094static tree
11095java_complete_lhs (node)
11096 tree node;
e04a16fb 11097{
22eed1e6 11098 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11099 int flag;
e04a16fb
AG
11100
11101 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11102 worked out. */
e04a16fb
AG
11103 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11104 return node;
11105
11106 /* The switch block implements cases processing container nodes
11107 first. Contained nodes are always written back. Leaves come
11108 next and return a value. */
11109 switch (TREE_CODE (node))
11110 {
11111 case BLOCK:
11112
11113 /* 1- Block section.
11114 Set the local values on decl names so we can identify them
11115 faster when they're referenced. At that stage, identifiers
11116 are legal so we don't check for declaration errors. */
11117 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11118 {
11119 DECL_CONTEXT (cn) = current_function_decl;
11120 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11121 }
15fdcfe9
PB
11122 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11123 CAN_COMPLETE_NORMALLY (node) = 1;
11124 else
e04a16fb 11125 {
15fdcfe9
PB
11126 tree stmt = BLOCK_EXPR_BODY (node);
11127 tree *ptr;
11128 int error_seen = 0;
11129 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11130 {
c877974e
APB
11131 /* Re-order from (((A; B); C); ...; Z) to
11132 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11133 This makes it easier to scan the statements left-to-right
11134 without using recursion (which might overflow the stack
11135 if the block has many statements. */
11136 for (;;)
11137 {
11138 tree left = TREE_OPERAND (stmt, 0);
11139 if (TREE_CODE (left) != COMPOUND_EXPR)
11140 break;
11141 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11142 TREE_OPERAND (left, 1) = stmt;
11143 stmt = left;
11144 }
11145 BLOCK_EXPR_BODY (node) = stmt;
11146 }
11147
c877974e
APB
11148 /* Now do the actual complete, without deep recursion for
11149 long blocks. */
15fdcfe9 11150 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11151 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11152 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11153 {
11154 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11155 tree *next = &TREE_OPERAND (*ptr, 1);
11156 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11157 if (cur == empty_stmt_node)
11158 {
11159 /* Optimization; makes it easier to detect empty bodies.
11160 Most useful for <clinit> with all-constant initializer. */
11161 *ptr = *next;
11162 continue;
11163 }
15fdcfe9
PB
11164 if (TREE_CODE (cur) == ERROR_MARK)
11165 error_seen++;
11166 else if (! CAN_COMPLETE_NORMALLY (cur))
11167 {
11168 wfl_op2 = *next;
11169 for (;;)
11170 {
11171 if (TREE_CODE (wfl_op2) == BLOCK)
11172 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11173 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11174 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11175 else
11176 break;
11177 }
11178 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11179 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11180 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11181 }
11182 ptr = next;
11183 }
11184 *ptr = java_complete_tree (*ptr);
11185
11186 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11187 return error_mark_node;
15fdcfe9 11188 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11189 }
11190 /* Turn local bindings to null */
11191 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11192 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11193
11194 TREE_TYPE (node) = void_type_node;
11195 break;
11196
11197 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11198
b9f7e36c
APB
11199 case THROW_EXPR:
11200 wfl_op1 = TREE_OPERAND (node, 0);
11201 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11202 /* 14.19 A throw statement cannot complete normally. */
11203 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11204 return patch_throw_statement (node, wfl_op1);
11205
11206 case SYNCHRONIZED_EXPR:
11207 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11208 return patch_synchronized_statement (node, wfl_op1);
11209
b67d701b
PB
11210 case TRY_EXPR:
11211 return patch_try_statement (node);
11212
a7d8d81f
PB
11213 case TRY_FINALLY_EXPR:
11214 COMPLETE_CHECK_OP_0 (node);
11215 COMPLETE_CHECK_OP_1 (node);
11216 CAN_COMPLETE_NORMALLY (node)
11217 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11218 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11219 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11220 return node;
11221
5a005d9e
PB
11222 case CLEANUP_POINT_EXPR:
11223 COMPLETE_CHECK_OP_0 (node);
11224 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11225 CAN_COMPLETE_NORMALLY (node) =
11226 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11227 return node;
11228
11229 case WITH_CLEANUP_EXPR:
11230 COMPLETE_CHECK_OP_0 (node);
11231 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11232 CAN_COMPLETE_NORMALLY (node) =
11233 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11234 TREE_TYPE (node) = void_type_node;
11235 return node;
11236
e04a16fb
AG
11237 case LABELED_BLOCK_EXPR:
11238 PUSH_LABELED_BLOCK (node);
11239 if (LABELED_BLOCK_BODY (node))
11240 COMPLETE_CHECK_OP_1 (node);
11241 TREE_TYPE (node) = void_type_node;
11242 POP_LABELED_BLOCK ();
1fb89a4d
APB
11243
11244 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11245 {
11246 LABELED_BLOCK_BODY (node) = NULL_TREE;
11247 CAN_COMPLETE_NORMALLY (node) = 1;
11248 }
1fb89a4d 11249 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11250 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11251 return node;
11252
11253 case EXIT_BLOCK_EXPR:
11254 /* We don't complete operand 1, because it's the return value of
11255 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11256 return patch_bc_statement (node);
11257
15fdcfe9
PB
11258 case CASE_EXPR:
11259 cn = java_complete_tree (TREE_OPERAND (node, 0));
11260 if (cn == error_mark_node)
11261 return cn;
11262
8576f094
APB
11263 /* First, the case expression must be constant. Values of final
11264 fields are accepted. */
15fdcfe9 11265 cn = fold (cn);
8576f094
APB
11266 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11267 && JDECL_P (TREE_OPERAND (cn, 1))
11268 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11269 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11270 {
100f7cd8
APB
11271 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11272 TREE_OPERAND (cn, 1));
100f7cd8 11273 }
15fdcfe9 11274
ce6e9147 11275 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11276 {
11277 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11278 parse_error_context (node, "Constant expression required");
11279 return error_mark_node;
11280 }
11281
11282 nn = ctxp->current_loop;
11283
11284 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11285 if (!try_builtin_assignconv (NULL_TREE,
11286 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11287 {
11288 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11289 parse_error_context
11290 (wfl_operator,
11291 "Incompatible type for case. Can't convert `%s' to `int'",
11292 lang_printable_name (TREE_TYPE (cn), 0));
11293 return error_mark_node;
11294 }
11295
11296 cn = fold (convert (int_type_node, cn));
11297
11298 /* Multiple instance of a case label bearing the same
11299 value is checked during code generation. The case
11300 expression is allright so far. */
34d4df06
APB
11301 if (TREE_CODE (cn) == VAR_DECL)
11302 cn = DECL_INITIAL (cn);
15fdcfe9 11303 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11304 TREE_TYPE (node) = void_type_node;
15fdcfe9 11305 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11306 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11307 break;
11308
11309 case DEFAULT_EXPR:
11310 nn = ctxp->current_loop;
11311 /* Only one default label is allowed per switch statement */
11312 if (SWITCH_HAS_DEFAULT (nn))
11313 {
11314 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11315 parse_error_context (wfl_operator,
11316 "Duplicate case label: `default'");
11317 return error_mark_node;
11318 }
11319 else
11320 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11321 TREE_TYPE (node) = void_type_node;
10100cc7 11322 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11323 CAN_COMPLETE_NORMALLY (node) = 1;
11324 break;
11325
b67d701b 11326 case SWITCH_EXPR:
e04a16fb
AG
11327 case LOOP_EXPR:
11328 PUSH_LOOP (node);
11329 /* Check whether the loop was enclosed in a labeled
11330 statement. If not, create one, insert the loop in it and
11331 return the node */
11332 nn = patch_loop_statement (node);
b67d701b 11333
e04a16fb 11334 /* Anyways, walk the body of the loop */
b67d701b
PB
11335 if (TREE_CODE (node) == LOOP_EXPR)
11336 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11337 /* Switch statement: walk the switch expression and the cases */
11338 else
11339 node = patch_switch_statement (node);
11340
e7c7bcef 11341 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11342 nn = error_mark_node;
11343 else
15fdcfe9 11344 {
b635eb2f
PB
11345 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11346 /* If we returned something different, that's because we
11347 inserted a label. Pop the label too. */
11348 if (nn != node)
11349 {
11350 if (CAN_COMPLETE_NORMALLY (node))
11351 CAN_COMPLETE_NORMALLY (nn) = 1;
11352 POP_LABELED_BLOCK ();
11353 }
15fdcfe9 11354 }
e04a16fb
AG
11355 POP_LOOP ();
11356 return nn;
11357
11358 case EXIT_EXPR:
11359 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11360 return patch_exit_expr (node);
11361
11362 case COND_EXPR:
11363 /* Condition */
11364 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11365 if (TREE_OPERAND (node, 0) == error_mark_node)
11366 return error_mark_node;
11367 /* then-else branches */
11368 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11369 if (TREE_OPERAND (node, 1) == error_mark_node)
11370 return error_mark_node;
11371 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11372 if (TREE_OPERAND (node, 2) == error_mark_node)
11373 return error_mark_node;
11374 return patch_if_else_statement (node);
11375 break;
11376
22eed1e6
APB
11377 case CONDITIONAL_EXPR:
11378 /* Condition */
11379 wfl_op1 = TREE_OPERAND (node, 0);
11380 COMPLETE_CHECK_OP_0 (node);
11381 wfl_op2 = TREE_OPERAND (node, 1);
11382 COMPLETE_CHECK_OP_1 (node);
11383 wfl_op3 = TREE_OPERAND (node, 2);
11384 COMPLETE_CHECK_OP_2 (node);
11385 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11386
e04a16fb
AG
11387 /* 3- Expression section */
11388 case COMPOUND_EXPR:
15fdcfe9 11389 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11390 TREE_OPERAND (node, 0) = nn =
11391 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11392 if (wfl_op2 == empty_stmt_node)
11393 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11394 else
15fdcfe9 11395 {
dc0b3eff 11396 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11397 {
dc0b3eff
PB
11398 /* An unreachable condition in a do-while statement
11399 is *not* (technically) an unreachable statement. */
11400 nn = wfl_op2;
11401 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11402 nn = EXPR_WFL_NODE (nn);
11403 if (TREE_CODE (nn) != EXIT_EXPR)
11404 {
11405 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11406 parse_error_context (wfl_operator, "Unreachable statement");
11407 }
bccaf73a 11408 }
dc0b3eff
PB
11409 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11410 if (TREE_OPERAND (node, 1) == error_mark_node)
11411 return error_mark_node;
11412 CAN_COMPLETE_NORMALLY (node)
11413 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11414 }
e04a16fb
AG
11415 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11416 break;
11417
11418 case RETURN_EXPR:
15fdcfe9 11419 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11420 return patch_return (node);
11421
11422 case EXPR_WITH_FILE_LOCATION:
11423 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11424 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11425 {
5423609c 11426 tree wfl = node;
15fdcfe9 11427 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11428 if (node == error_mark_node)
11429 return node;
5423609c
APB
11430 /* Keep line number information somewhere were it doesn't
11431 disrupt the completion process. */
2c56429a 11432 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11433 {
11434 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11435 TREE_OPERAND (node, 1) = wfl;
11436 }
15fdcfe9
PB
11437 CAN_COMPLETE_NORMALLY (node) = 1;
11438 }
e04a16fb
AG
11439 else
11440 {
5b09b33e
PB
11441 tree body;
11442 int save_lineno = lineno;
11443 lineno = EXPR_WFL_LINENO (node);
11444 body = java_complete_tree (EXPR_WFL_NODE (node));
11445 lineno = save_lineno;
15fdcfe9 11446 EXPR_WFL_NODE (node) = body;
dc0b3eff 11447 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11448 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
00b4575d 11449 if (body == empty_stmt_node || TREE_CONSTANT (body))
cd9643f7 11450 {
00b4575d 11451 /* Makes it easier to constant fold, detect empty bodies. */
cd9643f7
PB
11452 return body;
11453 }
dc0b3eff 11454 if (body == error_mark_node)
e04a16fb
AG
11455 {
11456 /* Its important for the evaluation of assignment that
11457 this mark on the TREE_TYPE is propagated. */
11458 TREE_TYPE (node) = error_mark_node;
11459 return error_mark_node;
11460 }
11461 else
11462 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11463
e04a16fb
AG
11464 }
11465 break;
11466
b67d701b 11467 case NEW_ARRAY_EXPR:
e04a16fb
AG
11468 /* Patch all the dimensions */
11469 flag = 0;
11470 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11471 {
11472 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11473 tree dim = convert (int_type_node,
11474 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11475 if (dim == error_mark_node)
11476 {
11477 flag = 1;
11478 continue;
11479 }
11480 else
11481 {
b9f7e36c 11482 TREE_VALUE (cn) = dim;
e04a16fb
AG
11483 /* Setup the location of the current dimension, for
11484 later error report. */
11485 TREE_PURPOSE (cn) =
11486 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11487 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11488 }
11489 }
11490 /* They complete the array creation expression, if no errors
11491 were found. */
15fdcfe9 11492 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11493 return (flag ? error_mark_node
11494 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11495
c2952b01
APB
11496 case NEW_ANONYMOUS_ARRAY_EXPR:
11497 /* Create the array type if necessary. */
11498 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11499 {
11500 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11501 if (!(type = resolve_type_during_patch (type)))
11502 return error_mark_node;
11503 type = build_array_from_name (type, NULL_TREE,
11504 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11505 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11506 }
11507 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11508 ANONYMOUS_ARRAY_INITIALIZER (node));
11509 if (node == error_mark_node)
11510 return error_mark_node;
11511 CAN_COMPLETE_NORMALLY (node) = 1;
11512 return node;
11513
b67d701b 11514 case NEW_CLASS_EXPR:
e04a16fb 11515 case CALL_EXPR:
b67d701b 11516 /* Complete function's argument(s) first */
e04a16fb
AG
11517 if (complete_function_arguments (node))
11518 return error_mark_node;
11519 else
b9f7e36c 11520 {
22eed1e6
APB
11521 tree decl, wfl = TREE_OPERAND (node, 0);
11522 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
e101152f
APB
11523 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11524 super_identifier_node);
22eed1e6 11525
e101152f
APB
11526 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11527 from_super, 0, &decl);
c877974e
APB
11528 if (node == error_mark_node)
11529 return error_mark_node;
11530
11531 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11532 /* If we call this(...), register signature and positions */
11533 if (in_this)
11534 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11535 tree_cons (wfl, decl,
11536 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11537 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11538 return force_evaluation_order (node);
b9f7e36c 11539 }
e04a16fb
AG
11540
11541 case MODIFY_EXPR:
11542 /* Save potential wfls */
11543 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11544 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11545
cd9643f7
PB
11546 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11547 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11548 && DECL_INITIAL (nn) != NULL_TREE)
11549 {
100f7cd8
APB
11550 tree value;
11551
100f7cd8 11552 value = fold_constant_for_init (nn, nn);
c2952b01 11553
cd9643f7
PB
11554 if (value != NULL_TREE)
11555 {
11556 tree type = TREE_TYPE (value);
c2952b01
APB
11557 if (JPRIMITIVE_TYPE_P (type) ||
11558 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11559 return empty_stmt_node;
11560 }
629d4b4d
APB
11561 if (! flag_emit_class_files)
11562 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11563 if (CLASS_FINAL_VARIABLE_P (nn))
11564 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11565 }
e04a16fb 11566 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11567
e04a16fb
AG
11568 if (TREE_OPERAND (node, 0) == error_mark_node)
11569 return error_mark_node;
11570
5cbdba64
APB
11571 flag = COMPOUND_ASSIGN_P (wfl_op2);
11572 if (flag)
e04a16fb 11573 {
c2952b01
APB
11574 /* This might break when accessing outer field from inner
11575 class. TESTME, FIXME */
2aa11e97 11576 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb 11577
e101152f 11578 /* Hand stabilize the lhs on both places */
e04a16fb 11579 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11580 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11581 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11582
5cbdba64 11583 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11584 /* Now complete the RHS. We write it back later on. */
11585 nn = java_complete_tree (TREE_OPERAND (node, 1));
11586
642f15d1
APB
11587 if ((cn = patch_string (nn)))
11588 nn = cn;
11589
2aa11e97
APB
11590 /* The last part of the rewrite for E1 op= E2 is to have
11591 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11592 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11593 TREE_TYPE (lvalue), nn));
5cbdba64 11594
568aac9c
TT
11595 /* If the assignment is compound and has reference type,
11596 then ensure the LHS has type String and nothing else. */
11597 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11598 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11599 parse_error_context (wfl_op2,
11600 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11601 lang_printable_name (TREE_TYPE (lvalue), 0));
11602
5cbdba64 11603 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11604 }
11605
f8976021 11606 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11607 function to complete this RHS. Note that a NEW_ARRAY_INIT
11608 might have been already fully expanded if created as a result
11609 of processing an anonymous array initializer. We avoid doing
11610 the operation twice by testing whether the node already bears
11611 a type. */
11612 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11613 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11614 TREE_OPERAND (node, 1));
2aa11e97 11615 /* Otherwise we simply complete the RHS */
f8976021
APB
11616 else
11617 nn = java_complete_tree (TREE_OPERAND (node, 1));
11618
e04a16fb 11619 if (nn == error_mark_node)
c0d87ff6 11620 return error_mark_node;
2aa11e97
APB
11621
11622 /* Write back the RHS as we evaluated it. */
e04a16fb 11623 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11624
11625 /* In case we're handling = with a String as a RHS, we need to
11626 produce a String out of the RHS (it might still be a
11627 STRING_CST or a StringBuffer at this stage */
11628 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11629 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11630
11631 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11632 TREE_OPERAND (node, 1))))
11633 {
11634 /* We return error_mark_node if outer_field_access_fix
11635 detects we write into a final. */
11636 if (nn == error_mark_node)
11637 return error_mark_node;
11638 node = nn;
11639 }
11640 else
11641 {
11642 node = patch_assignment (node, wfl_op1, wfl_op2);
11643 /* Reorganize the tree if necessary. */
11644 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11645 || JSTRING_P (TREE_TYPE (node))))
11646 node = java_refold (node);
11647 }
11648
15fdcfe9
PB
11649 CAN_COMPLETE_NORMALLY (node) = 1;
11650 return node;
e04a16fb
AG
11651
11652 case MULT_EXPR:
11653 case PLUS_EXPR:
11654 case MINUS_EXPR:
11655 case LSHIFT_EXPR:
11656 case RSHIFT_EXPR:
11657 case URSHIFT_EXPR:
11658 case BIT_AND_EXPR:
11659 case BIT_XOR_EXPR:
11660 case BIT_IOR_EXPR:
11661 case TRUNC_MOD_EXPR:
c2952b01 11662 case TRUNC_DIV_EXPR:
e04a16fb
AG
11663 case RDIV_EXPR:
11664 case TRUTH_ANDIF_EXPR:
11665 case TRUTH_ORIF_EXPR:
11666 case EQ_EXPR:
11667 case NE_EXPR:
11668 case GT_EXPR:
11669 case GE_EXPR:
11670 case LT_EXPR:
11671 case LE_EXPR:
11672 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11673 knows how to handle those cases. */
11674 wfl_op1 = TREE_OPERAND (node, 0);
11675 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11676
15fdcfe9 11677 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11678 /* Don't complete string nodes if dealing with the PLUS operand. */
11679 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11680 {
11681 nn = java_complete_tree (wfl_op1);
11682 if (nn == error_mark_node)
11683 return error_mark_node;
48a840d9 11684
2aa11e97
APB
11685 TREE_OPERAND (node, 0) = nn;
11686 }
b67d701b 11687 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11688 {
11689 nn = java_complete_tree (wfl_op2);
11690 if (nn == error_mark_node)
11691 return error_mark_node;
48a840d9 11692
2aa11e97
APB
11693 TREE_OPERAND (node, 1) = nn;
11694 }
dc0b3eff 11695 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11696
5e942c50
APB
11697 case INSTANCEOF_EXPR:
11698 wfl_op1 = TREE_OPERAND (node, 0);
11699 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11700 if (flag_emit_xref)
11701 {
11702 TREE_TYPE (node) = boolean_type_node;
11703 return node;
11704 }
5e942c50
APB
11705 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11706
b67d701b 11707 case UNARY_PLUS_EXPR:
e04a16fb
AG
11708 case NEGATE_EXPR:
11709 case TRUTH_NOT_EXPR:
11710 case BIT_NOT_EXPR:
11711 case PREDECREMENT_EXPR:
11712 case PREINCREMENT_EXPR:
11713 case POSTDECREMENT_EXPR:
11714 case POSTINCREMENT_EXPR:
11715 case CONVERT_EXPR:
11716 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11717 how to handle those cases. */
11718 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11719 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11720 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11721 if (TREE_OPERAND (node, 0) == error_mark_node)
11722 return error_mark_node;
4a5f66c3
APB
11723 node = patch_unaryop (node, wfl_op1);
11724 CAN_COMPLETE_NORMALLY (node) = 1;
11725 break;
e04a16fb
AG
11726
11727 case ARRAY_REF:
11728 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11729 how to handle those cases. */
11730 wfl_op1 = TREE_OPERAND (node, 0);
11731 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11732 if (TREE_OPERAND (node, 0) == error_mark_node)
11733 return error_mark_node;
7f1d4866 11734 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11735 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11736 /* The same applies to wfl_op2 */
11737 wfl_op2 = TREE_OPERAND (node, 1);
11738 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11739 if (TREE_OPERAND (node, 1) == error_mark_node)
11740 return error_mark_node;
7f1d4866 11741 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11742 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11743 return patch_array_ref (node);
e04a16fb 11744
63a212ed
PB
11745 case RECORD_TYPE:
11746 return node;;
11747
11748 case COMPONENT_REF:
11749 /* The first step in the re-write of qualified name handling. FIXME.
11750 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11751 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11752 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11753 {
11754 tree name = TREE_OPERAND (node, 1);
11755 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11756 if (field == NULL_TREE)
11757 {
11758 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11759 return error_mark_node;
11760 }
11761 if (! FIELD_STATIC (field))
11762 {
11763 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11764 return error_mark_node;
11765 }
11766 return field;
11767 }
11768 else
400500c4 11769 abort ();
9bbc7d9f 11770 break;
9bbc7d9f 11771
b67d701b 11772 case THIS_EXPR:
e04a16fb
AG
11773 /* Can't use THIS in a static environment */
11774 if (!current_this)
11775 {
11776 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11777 parse_error_context (wfl_operator,
11778 "Keyword `this' used outside allowed context");
e04a16fb
AG
11779 TREE_TYPE (node) = error_mark_node;
11780 return error_mark_node;
11781 }
22eed1e6
APB
11782 if (ctxp->explicit_constructor_p)
11783 {
11784 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11785 parse_error_context
781b0558 11786 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11787 TREE_TYPE (node) = error_mark_node;
11788 return error_mark_node;
11789 }
e04a16fb 11790 return current_this;
c2952b01
APB
11791
11792 case CLASS_LITERAL:
11793 CAN_COMPLETE_NORMALLY (node) = 1;
11794 node = patch_incomplete_class_ref (node);
11795 if (node == error_mark_node)
11796 return error_mark_node;
11797 break;
11798
11799 case INSTANCE_INITIALIZERS_EXPR:
11800 in_instance_initializer++;
11801 node = java_complete_tree (TREE_OPERAND (node, 0));
11802 in_instance_initializer--;
11803 if (node != error_mark_node)
11804 TREE_TYPE (node) = void_type_node;
11805 else
11806 return error_mark_node;
11807 break;
e04a16fb 11808
e04a16fb 11809 default:
15fdcfe9 11810 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11811 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11812 and it's time to turn it into the appropriate String object */
11813 if ((nn = patch_string (node)))
11814 node = nn;
11815 else
400500c4 11816 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11817 }
11818 return node;
11819}
11820
11821/* Complete function call's argument. Return a non zero value is an
11822 error was found. */
11823
11824static int
11825complete_function_arguments (node)
11826 tree node;
11827{
11828 int flag = 0;
11829 tree cn;
11830
f63991a8 11831 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11832 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11833 {
b67d701b 11834 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11835 parm = java_complete_tree (wfl);
c2952b01 11836
e04a16fb
AG
11837 if (parm == error_mark_node)
11838 {
11839 flag = 1;
11840 continue;
11841 }
b67d701b
PB
11842 /* If have a string literal that we haven't transformed yet or a
11843 crafted string buffer, as a result of use of the the String
11844 `+' operator. Build `parm.toString()' and expand it. */
11845 if ((temp = patch_string (parm)))
b9f7e36c 11846 parm = temp;
5e942c50
APB
11847 /* Inline PRIMTYPE.TYPE read access */
11848 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11849
5e942c50 11850 TREE_VALUE (cn) = parm;
e04a16fb 11851 }
f63991a8 11852 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11853 return flag;
11854}
11855
11856/* Sometimes (for loops and variable initialized during their
11857 declaration), we want to wrap a statement around a WFL and turn it
11858 debugable. */
11859
11860static tree
11861build_debugable_stmt (location, stmt)
11862 int location;
11863 tree stmt;
11864{
11865 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11866 {
11867 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11868 EXPR_WFL_LINECOL (stmt) = location;
11869 }
11870 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11871 return stmt;
11872}
11873
11874static tree
11875build_expr_block (body, decls)
11876 tree body, decls;
11877{
11878 tree node = make_node (BLOCK);
11879 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11880 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11881 if (body)
11882 TREE_TYPE (node) = TREE_TYPE (body);
11883 TREE_SIDE_EFFECTS (node) = 1;
11884 return node;
11885}
11886
b67d701b
PB
11887/* Create a new function block and link it approriately to current
11888 function block chain */
e04a16fb
AG
11889
11890static tree
11891enter_block ()
11892{
b67d701b
PB
11893 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11894}
11895
11896/* Link block B supercontext to the previous block. The current
11897 function DECL is used as supercontext when enter_a_block is called
11898 for the first time for a given function. The current function body
11899 (DECL_FUNCTION_BODY) is set to be block B. */
11900
11901static tree
11902enter_a_block (b)
11903 tree b;
11904{
e04a16fb
AG
11905 tree fndecl = current_function_decl;
11906
f099f336
APB
11907 if (!fndecl) {
11908 BLOCK_SUPERCONTEXT (b) = current_static_block;
11909 current_static_block = b;
11910 }
11911
11912 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11913 {
11914 BLOCK_SUPERCONTEXT (b) = fndecl;
11915 DECL_FUNCTION_BODY (fndecl) = b;
11916 }
11917 else
11918 {
11919 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11920 DECL_FUNCTION_BODY (fndecl) = b;
11921 }
11922 return b;
11923}
11924
11925/* Exit a block by changing the current function body
11926 (DECL_FUNCTION_BODY) to the current block super context, only if
11927 the block being exited isn't the method's top level one. */
11928
11929static tree
11930exit_block ()
11931{
f099f336
APB
11932 tree b;
11933 if (current_function_decl)
11934 {
11935 b = DECL_FUNCTION_BODY (current_function_decl);
11936 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11937 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11938 }
11939 else
11940 {
11941 b = current_static_block;
e04a16fb 11942
f099f336
APB
11943 if (BLOCK_SUPERCONTEXT (b))
11944 current_static_block = BLOCK_SUPERCONTEXT (b);
11945 }
e04a16fb
AG
11946 return b;
11947}
11948
11949/* Lookup for NAME in the nested function's blocks, all the way up to
11950 the current toplevel one. It complies with Java's local variable
11951 scoping rules. */
11952
11953static tree
11954lookup_name_in_blocks (name)
11955 tree name;
11956{
f099f336 11957 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11958
11959 while (b != current_function_decl)
11960 {
11961 tree current;
11962
11963 /* Paranoid sanity check. To be removed */
11964 if (TREE_CODE (b) != BLOCK)
400500c4 11965 abort ();
e04a16fb
AG
11966
11967 for (current = BLOCK_EXPR_DECLS (b); current;
11968 current = TREE_CHAIN (current))
11969 if (DECL_NAME (current) == name)
11970 return current;
11971 b = BLOCK_SUPERCONTEXT (b);
11972 }
11973 return NULL_TREE;
11974}
11975
11976static void
11977maybe_absorb_scoping_blocks ()
11978{
f099f336 11979 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11980 {
11981 tree b = exit_block ();
11982 java_method_add_stmt (current_function_decl, b);
11983 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11984 }
11985}
11986
11987\f
11988/* This section of the source is reserved to build_* functions that
11989 are building incomplete tree nodes and the patch_* functions that
11990 are completing them. */
11991
c2952b01
APB
11992/* Wrap a non WFL node around a WFL. */
11993static tree
9a7ab4b3 11994build_wfl_wrap (node, location)
c2952b01 11995 tree node;
9a7ab4b3 11996 int location;
c2952b01
APB
11997{
11998 tree wfl, node_to_insert = node;
11999
12000 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12001 with the way we're processing SUPER. A THIS from a primary as a
12002 different form than a SUPER. Turn THIS into something symbolic */
12003 if (TREE_CODE (node) == THIS_EXPR)
12004 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12005 else
12006 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12007
9a7ab4b3 12008 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
12009 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12010 return wfl;
12011}
12012
12013
9bbc7d9f 12014/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
12015 we're currently dealing with the class java.lang.Object. */
12016
12017static tree
e920ebc9
APB
12018build_super_invocation (mdecl)
12019 tree mdecl;
22eed1e6 12020{
e920ebc9 12021 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 12022 return empty_stmt_node;
22eed1e6
APB
12023 else
12024 {
9ee9b555 12025 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
12026 tree a = NULL_TREE, t;
12027 /* If we're dealing with an anonymous class, pass the arguments
12028 of the crafted constructor along. */
12029 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12030 {
12031 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12032 for (; t != end_params_node; t = TREE_CHAIN (t))
12033 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12034 }
12035 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12036 }
12037}
12038
12039/* Build a SUPER/THIS qualified method invocation. */
12040
12041static tree
12042build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12043 int use_this;
12044 tree name, args;
12045 int lloc, rloc;
22eed1e6
APB
12046{
12047 tree invok;
12048 tree wfl =
9ee9b555 12049 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12050 EXPR_WFL_LINECOL (wfl) = lloc;
12051 invok = build_method_invocation (name, args);
12052 return make_qualified_primary (wfl, invok, rloc);
12053}
12054
b67d701b 12055/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12056
12057static tree
12058build_method_invocation (name, args)
12059 tree name;
12060 tree args;
12061{
12062 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12063 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12064 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12065 return call;
12066}
12067
12068/* Build an incomplete new xxx(...) node. */
12069
12070static tree
12071build_new_invocation (name, args)
12072 tree name, args;
12073{
12074 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12075 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12076 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12077 return call;
12078}
12079
12080/* Build an incomplete assignment expression. */
12081
12082static tree
12083build_assignment (op, op_location, lhs, rhs)
12084 int op, op_location;
12085 tree lhs, rhs;
12086{
12087 tree assignment;
12088 /* Build the corresponding binop if we deal with a Compound
12089 Assignment operator. Mark the binop sub-tree as part of a
12090 Compound Assignment expression */
12091 if (op != ASSIGN_TK)
12092 {
12093 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12094 COMPOUND_ASSIGN_P (rhs) = 1;
12095 }
12096 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12097 TREE_SIDE_EFFECTS (assignment) = 1;
12098 EXPR_WFL_LINECOL (assignment) = op_location;
12099 return assignment;
12100}
12101
12102/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12103
15fdcfe9 12104char *
e04a16fb
AG
12105print_int_node (node)
12106 tree node;
12107{
12108 static char buffer [80];
12109 if (TREE_CONSTANT_OVERFLOW (node))
12110 sprintf (buffer, "<overflow>");
12111
12112 if (TREE_INT_CST_HIGH (node) == 0)
12113 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12114 TREE_INT_CST_LOW (node));
12115 else if (TREE_INT_CST_HIGH (node) == -1
12116 && TREE_INT_CST_LOW (node) != 0)
12117 {
12118 buffer [0] = '-';
12119 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12120 -TREE_INT_CST_LOW (node));
12121 }
12122 else
12123 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12124 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12125
12126 return buffer;
12127}
12128
c7303e41
APB
12129\f
12130
12131/* This section of the code handle assignment check with FINAL
12132 variables. */
12133
12134static void
12135reset_static_final_variable_assignment_flag (class)
12136 tree class;
12137{
12138 tree field;
12139 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12140 if (CLASS_FINAL_VARIABLE_P (field))
12141 DECL_FIELD_FINAL_LIIC (field) = 0;
12142}
12143
12144/* Figure whether all final static variable have been initialized. */
12145
12146static void
12147check_static_final_variable_assignment_flag (class)
12148 tree class;
12149{
12150 tree field;
12151
12152 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12153 if (CLASS_FINAL_VARIABLE_P (field)
12154 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12155 parse_error_context
12156 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12157 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12158 IDENTIFIER_POINTER (DECL_NAME (field)));
12159}
12160
12161/* This function marks all final variable locally unassigned. */
12162
12163static void
12164reset_final_variable_local_assignment_flag (class)
12165 tree class;
12166{
12167 tree field;
12168 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12169 if (FINAL_VARIABLE_P (field))
12170 DECL_FIELD_FINAL_LIIC (field) = 0;
12171}
12172
12173/* Figure whether all final variables have beem initialized in MDECL
12174 and mark MDECL accordingly. */
12175
12176static void
12177check_final_variable_local_assignment_flag (class, mdecl)
12178 tree class;
12179 tree mdecl;
12180{
12181 tree field;
12182 int initialized = 0;
12183 int non_initialized = 0;
12184
12185 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12186 return;
12187
12188 /* First find out whether all final variables or no final variable
12189 are initialized in this ctor. We don't take into account final
12190 variable that have been initialized upon declaration. */
12191 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12192 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12193 {
12194 if (DECL_FIELD_FINAL_LIIC (field))
12195 initialized++;
12196 else
12197 non_initialized++;
12198 }
12199
12200 /* There were no non initialized variable and no initialized variable.
12201 This ctor is fine. */
12202 if (!non_initialized && !initialized)
12203 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12204 /* If no variables have been initialized, that fine. We'll check
12205 later whether this ctor calls a constructor which initializes
12206 them. We mark the ctor as not initializing all its finals. */
12207 else if (initialized == 0)
12208 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12209 /* If we have a mixed bag, then we have a problem. We need to report
12210 all the variables we're not initializing. */
12211 else if (initialized && non_initialized)
12212 {
12213 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12214 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12215 if (FIELD_FINAL (field)
12216 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12217 {
12218 parse_error_context
12219 (lookup_cl (mdecl),
12220 "Blank final variable `%s' may not have been initialized in this constructor",
12221 IDENTIFIER_POINTER (DECL_NAME (field)));
12222 DECL_FIELD_FINAL_IERR (field) = 1;
12223 }
12224 }
12225 /* Otherwise we know this ctor is initializing all its final
12226 variable. We mark it so. */
12227 else
12228 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12229}
12230
12231/* This function recurses in a simple what through STMT and stops when
12232 it finds a constructor call. It then verifies that the called
12233 constructor initialized its final properly. Return 1 upon success,
12234 0 or -1 otherwise. */
12235
12236static int
12237check_final_variable_indirect_assignment (stmt)
12238 tree stmt;
12239{
12240 int res;
12241 switch (TREE_CODE (stmt))
12242 {
12243 case EXPR_WITH_FILE_LOCATION:
12244 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12245 case COMPOUND_EXPR:
12246 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12247 if (res)
12248 return res;
12249 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12250 case SAVE_EXPR:
12251 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12252 case CALL_EXPR:
12253 {
12254 tree decl = TREE_OPERAND (stmt, 0);
12255 tree fbody;
12256
12257 if (TREE_CODE (decl) != FUNCTION_DECL)
12258 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12259 if (TREE_CODE (decl) != FUNCTION_DECL)
400500c4 12260 abort ();
c7303e41
APB
12261 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12262 return 1;
12263 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12264 return -1;
12265 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12266 if (fbody == error_mark_node)
12267 return -1;
12268 fbody = BLOCK_EXPR_BODY (fbody);
12269 return check_final_variable_indirect_assignment (fbody);
12270 }
12271 default:
12272 break;
12273 }
12274 return 0;
12275}
12276
12277/* This is the last chance to catch a final variable initialization
12278 problem. This routine will report an error if a final variable was
12279 never (globally) initialized and never reported as not having been
12280 initialized properly. */
12281
12282static void
12283check_final_variable_global_assignment_flag (class)
12284 tree class;
12285{
12286 tree field, mdecl;
12287 int nnctor = 0;
12288
12289 /* We go through all natural ctors and see whether they're
12290 initializing all their final variables or not. */
12291 current_function_decl = NULL_TREE; /* For the error report. */
12292 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12293 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12294 {
12295 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12296 {
12297 /* It doesn't. Maybe it calls a constructor that initializes
12298 them. find out. */
12299 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12300 if (fbody == error_mark_node)
12301 continue;
12302 fbody = BLOCK_EXPR_BODY (fbody);
12303 if (check_final_variable_indirect_assignment (fbody) == 1)
12304 {
12305 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12306 nnctor++;
12307 }
12308 else
12309 parse_error_context
12310 (lookup_cl (mdecl),
12311 "Final variable initialization error in this constructor");
12312 }
12313 else
12314 nnctor++;
12315 }
12316
12317 /* Finally we catch final variables that never were initialized */
12318 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12319 if (FINAL_VARIABLE_P (field)
12320 /* If the field wasn't initialized upon declaration */
12321 && !DECL_FIELD_FINAL_IUD (field)
12322 /* There wasn't natural ctor in which the field could have been
12323 initialized */
12324 && !nnctor
12325 /* If we never reported a problem with this field */
12326 && !DECL_FIELD_FINAL_IERR (field))
12327 {
12328 current_function_decl = NULL;
12329 parse_error_context
12330 (DECL_FIELD_FINAL_WFL (field),
12331 "Final variable `%s' hasn't been initialized upon its declaration",
12332 IDENTIFIER_POINTER (DECL_NAME (field)));
12333 }
12334
12335}
12336
7f1d4866
APB
12337/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12338 context. */
5e942c50
APB
12339
12340static int
12341check_final_assignment (lvalue, wfl)
12342 tree lvalue, wfl;
12343{
c7303e41
APB
12344 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12345 return 0;
12346
12347 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12348 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12349 lvalue = TREE_OPERAND (lvalue, 1);
12350
c7303e41
APB
12351 if (!FIELD_FINAL (lvalue))
12352 return 0;
12353
12354 /* Now the logic. We can modify a final VARIABLE:
12355 1) in finit$, (its declaration was followed by an initialization,)
12356 2) consistently in each natural ctor, if it wasn't initialized in
12357 finit$ or once in <clinit>. In any other cases, an error should be
12358 reported. */
12359 if (DECL_FINIT_P (current_function_decl))
5e942c50 12360 {
c7303e41
APB
12361 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12362 return 0;
5e942c50 12363 }
c7303e41
APB
12364
12365 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12366 /* Only if it wasn't given a value upon initialization */
12367 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12368 /* If it was never assigned a value in this constructor */
12369 && !DECL_FIELD_FINAL_LIIC (lvalue))
12370 {
12371 /* Turn the locally assigned flag on, it will be checked later
12372 on to point out at discrepancies. */
12373 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12374 if (DECL_CLINIT_P (current_function_decl))
12375 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12376 return 0;
12377 }
12378
12379 /* Other problems should be reported right away. */
12380 parse_error_context
12381 (wfl, "Can't %sassign a value to the final variable `%s'",
12382 (FIELD_STATIC (lvalue) ? "re" : ""),
12383 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12384
12385 /* Note that static field can be initialized once and only once. */
12386 if (FIELD_STATIC (lvalue))
12387 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12388
12389 return 1;
5e942c50
APB
12390}
12391
12392/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12393 read. This is needed to avoid circularities in the implementation
12394 of these fields in libjava. */
12395
12396static tree
12397maybe_build_primttype_type_ref (rhs, wfl)
12398 tree rhs, wfl;
12399{
12400 tree to_return = NULL_TREE;
12401 tree rhs_type = TREE_TYPE (rhs);
12402 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12403 {
12404 tree n = TREE_OPERAND (rhs, 1);
12405 if (TREE_CODE (n) == VAR_DECL
12406 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12407 && rhs_type == class_ptr_type
12408 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12409 {
49f48c71 12410 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12411 if (!strncmp (self_name, "java.lang.", 10))
12412 to_return = build_primtype_type_ref (self_name);
12413 }
12414 }
12415 return (to_return ? to_return : rhs );
12416}
12417
e04a16fb
AG
12418/* 15.25 Assignment operators. */
12419
12420static tree
12421patch_assignment (node, wfl_op1, wfl_op2)
12422 tree node;
12423 tree wfl_op1;
12424 tree wfl_op2;
12425{
0a2138e2 12426 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12427 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12428 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12429 int error_found = 0;
12430 int lvalue_from_array = 0;
12431
c2952b01 12432 /* Can't assign to a (blank) final. */
5e942c50
APB
12433 if (check_final_assignment (lvalue, wfl_op1))
12434 error_found = 1;
e04a16fb
AG
12435
12436 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12437
12438 /* Lhs can be a named variable */
34f4db93 12439 if (JDECL_P (lvalue))
e04a16fb 12440 {
e04a16fb
AG
12441 lhs_type = TREE_TYPE (lvalue);
12442 }
12443 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12444 comment on reason why */
12445 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12446 {
12447 lhs_type = TREE_TYPE (lvalue);
12448 lvalue_from_array = 1;
12449 }
12450 /* Or a field access */
12451 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12452 lhs_type = TREE_TYPE (lvalue);
12453 /* Or a function return slot */
12454 else if (TREE_CODE (lvalue) == RESULT_DECL)
12455 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12456 /* Otherwise, we might want to try to write into an optimized static
12457 final, this is an of a different nature, reported further on. */
12458 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12459 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12460 {
6632dcdd 12461 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12462 {
12463 /* What we should do instead is resetting the all the flags
12464 previously set, exchange lvalue for llvalue and continue. */
12465 error_found = 1;
12466 return error_mark_node;
12467 }
12468 else
12469 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12470 }
12471 else
e04a16fb
AG
12472 {
12473 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12474 error_found = 1;
12475 }
12476
12477 rhs_type = TREE_TYPE (rhs);
b67d701b 12478 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12479 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12480
b67d701b 12481 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12482 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12483 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12484
12485 /* 15.25.2 If we have a compound assignment, convert RHS into the
12486 type of the LHS */
12487 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12488 new_rhs = convert (lhs_type, rhs);
12489
12490 /* Explicit cast required. This is an error */
12491 if (!new_rhs)
12492 {
c2e3db92
KG
12493 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12494 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12495 tree wfl;
12496 char operation [32]; /* Max size known */
12497
12498 /* If the assignment is part of a declaration, we use the WFL of
12499 the declared variable to point out the error and call it a
12500 declaration problem. If the assignment is a genuine =
12501 operator, we call is a operator `=' problem, otherwise we
12502 call it an assignment problem. In both of these last cases,
12503 we use the WFL of the operator to indicate the error. */
12504
12505 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12506 {
12507 wfl = wfl_op1;
12508 strcpy (operation, "declaration");
12509 }
12510 else
12511 {
12512 wfl = wfl_operator;
12513 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12514 strcpy (operation, "assignment");
12515 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12516 strcpy (operation, "`return'");
12517 else
12518 strcpy (operation, "`='");
12519 }
12520
1ebadc60 12521 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12522 parse_error_context
12523 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12524 operation, t1, t2);
1ebadc60 12525 else
781b0558 12526 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12527 operation, t1, t2);
e04a16fb
AG
12528 free (t1); free (t2);
12529 error_found = 1;
12530 }
12531
c877974e
APB
12532 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12533 if (new_rhs)
12534 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12535
e04a16fb
AG
12536 if (error_found)
12537 return error_mark_node;
12538
2622b947
APB
12539 /* 10.10: Array Store Exception runtime check */
12540 if (!flag_emit_class_files
e8fc7396 12541 && !flag_emit_xref
2622b947 12542 && lvalue_from_array
afc390b1 12543 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12544 {
12545 tree check;
12546 tree base = lvalue;
12547
12548 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12549 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12550 base = TREE_OPERAND (lvalue, 0);
12551 else
12552 {
12553 if (flag_bounds_check)
12554 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12555 else
12556 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12557 }
12558
12559 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12560 new_rhs = save_expr (new_rhs);
2622b947
APB
12561 check = build (CALL_EXPR, void_type_node,
12562 build_address_of (soft_checkarraystore_node),
12563 tree_cons (NULL_TREE, base,
12564 build_tree_list (NULL_TREE, new_rhs)),
12565 NULL_TREE);
12566 TREE_SIDE_EFFECTS (check) = 1;
12567
12568 /* We have to decide on an insertion point */
12569 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12570 {
12571 tree t;
12572 if (flag_bounds_check)
12573 {
12574 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12575 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12576 build (COMPOUND_EXPR, void_type_node, t, check);
12577 }
12578 else
12579 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12580 check, TREE_OPERAND (lvalue, 1));
12581 }
12582 else
12583 {
12584 /* Make sure the bound check will happen before the store check */
12585 if (flag_bounds_check)
12586 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12587 build (COMPOUND_EXPR, void_type_node,
12588 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12589 else
12590 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12591 }
12592 }
22eed1e6 12593
34d4df06
APB
12594 /* Final locals can be used as case values in switch
12595 statement. Prepare them for this eventuality. */
12596 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12597 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12598 && TREE_CONSTANT (new_rhs)
12599 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12600 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12601 )
12602 {
12603 TREE_CONSTANT (lvalue) = 1;
12604 DECL_INITIAL (lvalue) = new_rhs;
12605 }
12606
e04a16fb
AG
12607 TREE_OPERAND (node, 0) = lvalue;
12608 TREE_OPERAND (node, 1) = new_rhs;
12609 TREE_TYPE (node) = lhs_type;
12610 return node;
12611}
12612
b67d701b
PB
12613/* Check that type SOURCE can be cast into type DEST. If the cast
12614 can't occur at all, return 0 otherwise 1. This function is used to
12615 produce accurate error messages on the reasons why an assignment
12616 failed. */
e04a16fb 12617
b67d701b
PB
12618static tree
12619try_reference_assignconv (lhs_type, rhs)
12620 tree lhs_type, rhs;
e04a16fb 12621{
b67d701b
PB
12622 tree new_rhs = NULL_TREE;
12623 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12624
b67d701b
PB
12625 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12626 {
12627 /* `null' may be assigned to any reference type */
12628 if (rhs == null_pointer_node)
12629 new_rhs = null_pointer_node;
12630 /* Try the reference assignment conversion */
12631 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12632 new_rhs = rhs;
12633 /* This is a magic assignment that we process differently */
12634 else if (rhs == soft_exceptioninfo_call_node)
12635 new_rhs = rhs;
12636 }
12637 return new_rhs;
12638}
12639
12640/* Check that RHS can be converted into LHS_TYPE by the assignment
12641 conversion (5.2), for the cases of RHS being a builtin type. Return
12642 NULL_TREE if the conversion fails or if because RHS isn't of a
12643 builtin type. Return a converted RHS if the conversion is possible. */
12644
12645static tree
12646try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12647 tree wfl_op1, lhs_type, rhs;
12648{
12649 tree new_rhs = NULL_TREE;
12650 tree rhs_type = TREE_TYPE (rhs);
12651
7e51098e
TT
12652 /* Handle boolean specially. */
12653 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12654 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12655 {
12656 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12657 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12658 new_rhs = rhs;
12659 }
12660
5e942c50 12661 /* Zero accepted everywhere */
7e51098e 12662 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12663 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12664 && JPRIMITIVE_TYPE_P (rhs_type))
12665 new_rhs = convert (lhs_type, rhs);
12666
b67d701b
PB
12667 /* 5.1.1 Try Identity Conversion,
12668 5.1.2 Try Widening Primitive Conversion */
5e942c50 12669 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12670 new_rhs = convert (lhs_type, rhs);
12671
12672 /* Try a narrowing primitive conversion (5.1.3):
12673 - expression is a constant expression of type int AND
12674 - variable is byte, short or char AND
12675 - The value of the expression is representable in the type of the
12676 variable */
12677 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12678 && (lhs_type == byte_type_node || lhs_type == char_type_node
12679 || lhs_type == short_type_node))
12680 {
12681 if (int_fits_type_p (rhs, lhs_type))
12682 new_rhs = convert (lhs_type, rhs);
12683 else if (wfl_op1) /* Might be called with a NULL */
12684 parse_warning_context
7e51098e 12685 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12686 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12687 /* Reported a warning that will turn into an error further
12688 down, so we don't return */
12689 }
12690
12691 return new_rhs;
12692}
12693
12694/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12695 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12696 0 is the conversion test fails. This implements parts the method
12697 invocation convertion (5.3). */
12698
12699static int
12700valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12701 tree lhs_type, rhs_type;
12702{
acd663ee 12703 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12704 if (lhs_type == rhs_type)
12705 return 1;
12706
7e51098e
TT
12707 /* Reject non primitive types and boolean conversions. */
12708 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12709 return 0;
12710
acd663ee
APB
12711 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12712 than a char can't be converted into a char. Short can't too, but
12713 the < test below takes care of that */
b67d701b
PB
12714 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12715 return 0;
12716
5e942c50
APB
12717 /* Accept all promoted type here. Note, we can't use <= in the test
12718 below, because we still need to bounce out assignments of short
12719 to char and the likes */
12720 if (lhs_type == int_type_node
12721 && (rhs_type == promoted_byte_type_node
12722 || rhs_type == promoted_short_type_node
12723 || rhs_type == promoted_char_type_node
12724 || rhs_type == promoted_boolean_type_node))
12725 return 1;
12726
acd663ee
APB
12727 /* From here, an integral is widened if its precision is smaller
12728 than the precision of the LHS or if the LHS is a floating point
12729 type, or the RHS is a float and the RHS a double. */
12730 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12731 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12732 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12733 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12734 return 1;
12735
12736 return 0;
e04a16fb
AG
12737}
12738
12739/* Check that something of SOURCE type can be assigned or cast to
12740 something of DEST type at runtime. Return 1 if the operation is
12741 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12742 were SOURCE is cast into DEST, which borrows a lot of the
12743 assignment check. */
12744
12745static int
12746valid_ref_assignconv_cast_p (source, dest, cast)
12747 tree source;
12748 tree dest;
12749 int cast;
12750{
09ed0f70
APB
12751 /* SOURCE or DEST might be null if not from a declared entity. */
12752 if (!source || !dest)
12753 return 0;
5e942c50
APB
12754 if (JNULLP_TYPE_P (source))
12755 return 1;
e04a16fb
AG
12756 if (TREE_CODE (source) == POINTER_TYPE)
12757 source = TREE_TYPE (source);
12758 if (TREE_CODE (dest) == POINTER_TYPE)
12759 dest = TREE_TYPE (dest);
c1eacb70
BM
12760
12761 /* If source and dest are being compiled from bytecode, they may need to
12762 be loaded. */
12763 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12764 {
12765 load_class (source, 1);
12766 safe_layout_class (source);
12767 }
12768 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12769 {
12770 load_class (dest, 1);
12771 safe_layout_class (dest);
12772 }
12773
e04a16fb
AG
12774 /* Case where SOURCE is a class type */
12775 if (TYPE_CLASS_P (source))
12776 {
12777 if (TYPE_CLASS_P (dest))
c2952b01
APB
12778 return (source == dest
12779 || inherits_from_p (source, dest)
c2952b01 12780 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12781 if (TYPE_INTERFACE_P (dest))
12782 {
12783 /* If doing a cast and SOURCE is final, the operation is
12784 always correct a compile time (because even if SOURCE
12785 does not implement DEST, a subclass of SOURCE might). */
12786 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12787 return 1;
12788 /* Otherwise, SOURCE must implement DEST */
12789 return interface_of_p (dest, source);
12790 }
12791 /* DEST is an array, cast permited if SOURCE is of Object type */
12792 return (cast && source == object_type_node ? 1 : 0);
12793 }
12794 if (TYPE_INTERFACE_P (source))
12795 {
12796 if (TYPE_CLASS_P (dest))
12797 {
12798 /* If not casting, DEST must be the Object type */
12799 if (!cast)
12800 return dest == object_type_node;
12801 /* We're doing a cast. The cast is always valid is class
12802 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12803 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12804 return 1;
12805 else
12806 return interface_of_p (source, dest);
12807 }
12808 if (TYPE_INTERFACE_P (dest))
12809 {
12810 /* If doing a cast, then if SOURCE and DEST contain method
12811 with the same signature but different return type, then
12812 this is a (compile time) error */
12813 if (cast)
12814 {
12815 tree method_source, method_dest;
12816 tree source_type;
0a2138e2 12817 tree source_sig;
e04a16fb
AG
12818 tree source_name;
12819 for (method_source = TYPE_METHODS (source); method_source;
12820 method_source = TREE_CHAIN (method_source))
12821 {
12822 source_sig =
12823 build_java_argument_signature (TREE_TYPE (method_source));
12824 source_type = TREE_TYPE (TREE_TYPE (method_source));
12825 source_name = DECL_NAME (method_source);
12826 for (method_dest = TYPE_METHODS (dest);
12827 method_dest; method_dest = TREE_CHAIN (method_dest))
12828 if (source_sig ==
12829 build_java_argument_signature (TREE_TYPE (method_dest))
12830 && source_name == DECL_NAME (method_dest)
12831 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12832 return 0;
12833 }
12834 return 1;
12835 }
12836 else
12837 return source == dest || interface_of_p (dest, source);
12838 }
ee17a290
TT
12839 else
12840 {
12841 /* Array */
12842 return (cast
12843 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12844 || (DECL_NAME (TYPE_NAME (source))
12845 == java_io_serializable)));
12846 }
e04a16fb
AG
12847 }
12848 if (TYPE_ARRAY_P (source))
12849 {
12850 if (TYPE_CLASS_P (dest))
12851 return dest == object_type_node;
09ed0f70 12852 /* Can't cast an array to an interface unless the interface is
ee17a290 12853 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12854 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12855 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12856 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12857 else /* Arrays */
12858 {
12859 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12860 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12861
b9f7e36c
APB
12862 /* In case of severe errors, they turn out null */
12863 if (!dest_element_type || !source_element_type)
12864 return 0;
e04a16fb
AG
12865 if (source_element_type == dest_element_type)
12866 return 1;
12867 return valid_ref_assignconv_cast_p (source_element_type,
12868 dest_element_type, cast);
12869 }
12870 return 0;
12871 }
12872 return 0;
12873}
12874
b67d701b
PB
12875static int
12876valid_cast_to_p (source, dest)
12877 tree source;
12878 tree dest;
12879{
12880 if (TREE_CODE (source) == POINTER_TYPE)
12881 source = TREE_TYPE (source);
12882 if (TREE_CODE (dest) == POINTER_TYPE)
12883 dest = TREE_TYPE (dest);
12884
12885 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12886 return valid_ref_assignconv_cast_p (source, dest, 1);
12887
12888 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12889 return 1;
12890
7e51098e
TT
12891 else if (TREE_CODE (source) == BOOLEAN_TYPE
12892 && TREE_CODE (dest) == BOOLEAN_TYPE)
12893 return 1;
12894
b67d701b
PB
12895 return 0;
12896}
12897
15fdcfe9
PB
12898static tree
12899do_unary_numeric_promotion (arg)
12900 tree arg;
12901{
12902 tree type = TREE_TYPE (arg);
7e51098e
TT
12903 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12904 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12905 arg = convert (int_type_node, arg);
12906 return arg;
12907}
12908
acd663ee
APB
12909/* Return a non zero value if SOURCE can be converted into DEST using
12910 the method invocation conversion rule (5.3). */
b67d701b
PB
12911static int
12912valid_method_invocation_conversion_p (dest, source)
12913 tree dest, source;
12914{
e3884b71 12915 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12916 && valid_builtin_assignconv_identity_widening_p (dest, source))
12917 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12918 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12919 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12920}
12921
e04a16fb
AG
12922/* Build an incomplete binop expression. */
12923
12924static tree
12925build_binop (op, op_location, op1, op2)
12926 enum tree_code op;
12927 int op_location;
12928 tree op1, op2;
12929{
5e942c50 12930 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12931 TREE_SIDE_EFFECTS (binop) = 1;
12932 /* Store the location of the operator, for better error report. The
12933 string of the operator will be rebuild based on the OP value. */
12934 EXPR_WFL_LINECOL (binop) = op_location;
12935 return binop;
12936}
12937
12938/* Build the string of the operator retained by NODE. If NODE is part
12939 of a compound expression, add an '=' at the end of the string. This
12940 function is called when an error needs to be reported on an
12941 operator. The string is returned as a pointer to a static character
12942 buffer. */
12943
12944static char *
12945operator_string (node)
12946 tree node;
12947{
12948#define BUILD_OPERATOR_STRING(S) \
12949 { \
12950 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12951 return buffer; \
12952 }
12953
12954 static char buffer [10];
12955 switch (TREE_CODE (node))
12956 {
12957 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12958 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12959 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12960 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12961 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12962 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12963 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12964 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12965 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12966 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12967 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12968 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12969 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12970 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12971 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12972 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12973 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12974 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12975 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12976 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12977 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12978 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12979 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12980 case PREINCREMENT_EXPR: /* Fall through */
12981 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12982 case PREDECREMENT_EXPR: /* Fall through */
12983 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12984 default:
400500c4
RK
12985 internal_error ("unregistered operator %s",
12986 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
12987 }
12988 return NULL;
12989#undef BUILD_OPERATOR_STRING
12990}
12991
5cbdba64
APB
12992/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12993
12994static int
12995java_decl_equiv (var_acc1, var_acc2)
12996 tree var_acc1, var_acc2;
12997{
12998 if (JDECL_P (var_acc1))
12999 return (var_acc1 == var_acc2);
13000
13001 return (TREE_CODE (var_acc1) == COMPONENT_REF
13002 && TREE_CODE (var_acc2) == COMPONENT_REF
13003 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13004 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13005 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13006}
13007
13008/* Return a non zero value if CODE is one of the operators that can be
13009 used in conjunction with the `=' operator in a compound assignment. */
13010
13011static int
13012binop_compound_p (code)
13013 enum tree_code code;
13014{
13015 int i;
13016 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13017 if (binop_lookup [i] == code)
13018 break;
13019
13020 return i < BINOP_COMPOUND_CANDIDATES;
13021}
13022
13023/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13024
13025static tree
13026java_refold (t)
13027 tree t;
13028{
13029 tree c, b, ns, decl;
13030
13031 if (TREE_CODE (t) != MODIFY_EXPR)
13032 return t;
13033
13034 c = TREE_OPERAND (t, 1);
13035 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13036 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13037 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13038 return t;
13039
13040 /* Now the left branch of the binary operator. */
13041 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13042 if (! (b && TREE_CODE (b) == NOP_EXPR
13043 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13044 return t;
13045
13046 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13047 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13048 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13049 return t;
13050
13051 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13052 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13053 /* It's got to be the an equivalent decl */
13054 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13055 {
13056 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13057 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13058 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13059 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13060 /* Change the right part of the BINOP_EXPR */
13061 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13062 }
13063
13064 return t;
13065}
13066
e04a16fb
AG
13067/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13068 errors but we modify NODE so that it contains the type computed
13069 according to the expression, when it's fixed. Otherwise, we write
13070 error_mark_node as the type. It allows us to further the analysis
13071 of remaining nodes and detects more errors in certain cases. */
13072
13073static tree
13074patch_binop (node, wfl_op1, wfl_op2)
13075 tree node;
13076 tree wfl_op1;
13077 tree wfl_op2;
13078{
13079 tree op1 = TREE_OPERAND (node, 0);
13080 tree op2 = TREE_OPERAND (node, 1);
13081 tree op1_type = TREE_TYPE (op1);
13082 tree op2_type = TREE_TYPE (op2);
48a840d9 13083 tree prom_type = NULL_TREE, cn;
e04a16fb 13084 int code = TREE_CODE (node);
b67d701b 13085
e04a16fb
AG
13086 /* If 1, tell the routine that we have to return error_mark_node
13087 after checking for the initialization of the RHS */
13088 int error_found = 0;
13089
e04a16fb
AG
13090 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13091
761491c8
APB
13092 /* If either op<n>_type are NULL, this might be early signs of an
13093 error situation, unless it's too early to tell (in case we're
13094 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13095 correctly so the error can be later on reported accurately. */
13096 if (! (code == PLUS_EXPR || code == NE_EXPR
13097 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13098 {
13099 tree n;
13100 if (! op1_type)
13101 {
13102 n = java_complete_tree (op1);
13103 op1_type = TREE_TYPE (n);
13104 }
13105 if (! op2_type)
13106 {
13107 n = java_complete_tree (op2);
13108 op2_type = TREE_TYPE (n);
13109 }
13110 }
13111
e04a16fb
AG
13112 switch (code)
13113 {
13114 /* 15.16 Multiplicative operators */
13115 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13116 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13117 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13118 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13119 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13120 {
7e51098e 13121 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13122 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13123 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13124 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13125 TREE_TYPE (node) = error_mark_node;
13126 error_found = 1;
13127 break;
13128 }
13129 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13130 /* Change the division operator if necessary */
13131 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13132 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13133
aa4759c1
AH
13134 if (TREE_CODE (prom_type) == INTEGER_TYPE
13135 && flag_use_divide_subroutine
13136 && ! flag_emit_class_files
13137 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13138 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13139
0b4d333e
APB
13140 /* This one is more complicated. FLOATs are processed by a
13141 function call to soft_fmod. Duplicate the value of the
13142 COMPOUND_ASSIGN_P flag. */
e04a16fb 13143 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13144 {
13145 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13146 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13147 TREE_SIDE_EFFECTS (mod)
13148 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13149 return mod;
13150 }
e04a16fb
AG
13151 break;
13152
13153 /* 15.17 Additive Operators */
13154 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13155
13156 /* Operation is valid if either one argument is a string
13157 constant, a String object or a StringBuffer crafted for the
13158 purpose of the a previous usage of the String concatenation
13159 operator */
13160
13161 if (TREE_CODE (op1) == STRING_CST
13162 || TREE_CODE (op2) == STRING_CST
13163 || JSTRING_TYPE_P (op1_type)
13164 || JSTRING_TYPE_P (op2_type)
13165 || IS_CRAFTED_STRING_BUFFER_P (op1)
13166 || IS_CRAFTED_STRING_BUFFER_P (op2))
13167 return build_string_concatenation (op1, op2);
13168
e04a16fb
AG
13169 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13170 Numeric Types */
7e51098e 13171 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13172 {
7e51098e 13173 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13174 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13175 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13176 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13177 TREE_TYPE (node) = error_mark_node;
13178 error_found = 1;
13179 break;
13180 }
13181 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13182 break;
13183
13184 /* 15.18 Shift Operators */
13185 case LSHIFT_EXPR:
13186 case RSHIFT_EXPR:
13187 case URSHIFT_EXPR:
13188 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13189 {
13190 if (!JINTEGRAL_TYPE_P (op1_type))
13191 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13192 else
1ebadc60 13193 {
7e51098e 13194 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13195 parse_error_context (wfl_operator,
781b0558 13196 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13197 operator_string (node),
13198 lang_printable_name (op2_type, 0));
13199 else
781b0558
KG
13200 parse_error_context (wfl_operator,
13201 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13202 operator_string (node),
13203 lang_printable_name (op2_type, 0));
13204 }
e04a16fb
AG
13205 TREE_TYPE (node) = error_mark_node;
13206 error_found = 1;
13207 break;
13208 }
13209
13210 /* Unary numeric promotion (5.6.1) is performed on each operand
13211 separatly */
15fdcfe9
PB
13212 op1 = do_unary_numeric_promotion (op1);
13213 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13214
13215 /* The type of the shift expression is the type of the promoted
13216 type of the left-hand operand */
13217 prom_type = TREE_TYPE (op1);
13218
c2952b01
APB
13219 /* Shift int only up to 0x1f and long up to 0x3f */
13220 if (prom_type == int_type_node)
13221 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13222 build_int_2 (0x1f, 0)));
13223 else
13224 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13225 build_int_2 (0x3f, 0)));
e04a16fb
AG
13226
13227 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13228 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13229 {
0b4d333e 13230 tree to_return;
73333a87
AH
13231 tree utype = unsigned_type (prom_type);
13232 op1 = convert (utype, op1);
e04a16fb 13233 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13234 TREE_OPERAND (node, 0) = op1;
13235 TREE_OPERAND (node, 1) = op2;
13236 TREE_TYPE (node) = utype;
0b4d333e
APB
13237 to_return = convert (prom_type, node);
13238 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13239 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13240 TREE_SIDE_EFFECTS (to_return)
13241 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13242 return to_return;
e04a16fb
AG
13243 }
13244 break;
5e942c50
APB
13245
13246 /* 15.19.1 Type Comparison Operator instaceof */
13247 case INSTANCEOF_EXPR:
13248
13249 TREE_TYPE (node) = boolean_type_node;
13250
13251 if (!(op2_type = resolve_type_during_patch (op2)))
13252 return error_mark_node;
13253
13254 /* The first operand must be a reference type or the null type */
13255 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13256 error_found = 1; /* Error reported further below */
13257
13258 /* The second operand must be a reference type */
13259 if (!JREFERENCE_TYPE_P (op2_type))
13260 {
13261 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13262 parse_error_context
13263 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13264 lang_printable_name (op2_type, 0));
13265 error_found = 1;
13266 }
13267
13268 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13269 {
13270 /* If the first operand is null, the result is always false */
13271 if (op1 == null_pointer_node)
13272 return boolean_false_node;
15fdcfe9
PB
13273 else if (flag_emit_class_files)
13274 {
13275 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13276 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13277 return node;
13278 }
5e942c50
APB
13279 /* Otherwise we have to invoke instance of to figure it out */
13280 else
67db0ce7 13281 return build_instanceof (op1, op2_type);
5e942c50
APB
13282 }
13283 /* There is no way the expression operand can be an instance of
13284 the type operand. This is a compile time error. */
13285 else
13286 {
c2e3db92 13287 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13288 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13289 parse_error_context
13290 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13291 t1, lang_printable_name (op2_type, 0));
13292 free (t1);
13293 error_found = 1;
13294 }
e04a16fb 13295
5e942c50 13296 break;
e04a16fb
AG
13297
13298 /* 15.21 Bitwise and Logical Operators */
13299 case BIT_AND_EXPR:
13300 case BIT_XOR_EXPR:
13301 case BIT_IOR_EXPR:
13302 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13303 /* Binary numeric promotion is performed on both operand and the
13304 expression retain that type */
13305 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13306
13307 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13308 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13309 /* The type of the bitwise operator expression is BOOLEAN */
13310 prom_type = boolean_type_node;
13311 else
13312 {
13313 if (!JINTEGRAL_TYPE_P (op1_type))
13314 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13315 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13316 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13317 TREE_TYPE (node) = error_mark_node;
13318 error_found = 1;
13319 /* Insert a break here if adding thing before the switch's
13320 break for this case */
13321 }
13322 break;
13323
13324 /* 15.22 Conditional-And Operator */
13325 case TRUTH_ANDIF_EXPR:
13326 /* 15.23 Conditional-Or Operator */
13327 case TRUTH_ORIF_EXPR:
13328 /* Operands must be of BOOLEAN type */
13329 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13330 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13331 {
13332 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13333 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13334 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13335 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13336 TREE_TYPE (node) = boolean_type_node;
13337 error_found = 1;
13338 break;
13339 }
13340 /* The type of the conditional operators is BOOLEAN */
13341 prom_type = boolean_type_node;
13342 break;
13343
13344 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13345 case LT_EXPR:
13346 case GT_EXPR:
13347 case LE_EXPR:
13348 case GE_EXPR:
13349 /* The type of each of the operands must be a primitive numeric
13350 type */
13351 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13352 {
13353 if (!JNUMERIC_TYPE_P (op1_type))
13354 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13355 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13356 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13357 TREE_TYPE (node) = boolean_type_node;
13358 error_found = 1;
13359 break;
13360 }
13361 /* Binary numeric promotion is performed on the operands */
13362 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13363 /* The type of the relation expression is always BOOLEAN */
13364 prom_type = boolean_type_node;
13365 break;
13366
13367 /* 15.20 Equality Operator */
13368 case EQ_EXPR:
13369 case NE_EXPR:
48a840d9
APB
13370 /* It's time for us to patch the strings. */
13371 if ((cn = patch_string (op1)))
13372 {
13373 op1 = cn;
13374 op1_type = TREE_TYPE (op1);
13375 }
13376 if ((cn = patch_string (op2)))
13377 {
13378 op2 = cn;
13379 op2_type = TREE_TYPE (op2);
13380 }
13381
e04a16fb
AG
13382 /* 15.20.1 Numerical Equality Operators == and != */
13383 /* Binary numeric promotion is performed on the operands */
5e942c50 13384 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13385 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13386
13387 /* 15.20.2 Boolean Equality Operators == and != */
13388 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13389 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13390 ; /* Nothing to do here */
13391
13392 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13393 /* Types have to be either references or the null type. If
13394 they're references, it must be possible to convert either
13395 type to the other by casting conversion. */
b9f7e36c
APB
13396 else if (op1 == null_pointer_node || op2 == null_pointer_node
13397 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13398 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13399 || valid_ref_assignconv_cast_p (op2_type,
13400 op1_type, 1))))
e04a16fb
AG
13401 ; /* Nothing to do here */
13402
13403 /* Else we have an error figure what can't be converted into
13404 what and report the error */
13405 else
13406 {
13407 char *t1;
c2e3db92 13408 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13409 parse_error_context
781b0558
KG
13410 (wfl_operator,
13411 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13412 operator_string (node), t1,
0a2138e2 13413 lang_printable_name (op2_type, 0));
e04a16fb
AG
13414 free (t1);
13415 TREE_TYPE (node) = boolean_type_node;
13416 error_found = 1;
13417 break;
13418 }
13419 prom_type = boolean_type_node;
13420 break;
13421 }
13422
e04a16fb
AG
13423 if (error_found)
13424 return error_mark_node;
13425
13426 TREE_OPERAND (node, 0) = op1;
13427 TREE_OPERAND (node, 1) = op2;
13428 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13429 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13430
ce6e9147
APB
13431 if (flag_emit_xref)
13432 return node;
13433
d1472141
PB
13434 /* fold does not respect side-effect order as required for Java but not C.
13435 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13436 * bytecode.
13437 */
13438 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13439 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13440 node = fold (node);
13441 return node;
e04a16fb
AG
13442}
13443
b67d701b
PB
13444/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13445 zero value, the value of CSTE comes after the valude of STRING */
13446
13447static tree
13448do_merge_string_cste (cste, string, string_len, after)
13449 tree cste;
49f48c71 13450 const char *string;
b67d701b
PB
13451 int string_len, after;
13452{
49f48c71 13453 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13454 int old_len = TREE_STRING_LENGTH (cste);
13455 int len = old_len + string_len;
520a57c8 13456 char *new = alloca (len+1);
354e99ce 13457
b67d701b
PB
13458 if (after)
13459 {
354e99ce
APB
13460 memcpy (new, string, string_len);
13461 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13462 }
13463 else
13464 {
354e99ce
APB
13465 memcpy (new, old, old_len);
13466 memcpy (&new [old_len], string, string_len);
b67d701b 13467 }
354e99ce 13468 new [len] = '\0';
520a57c8 13469 return build_string (len, new);
b67d701b
PB
13470}
13471
13472/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13473 new STRING_CST on success, NULL_TREE on failure */
13474
13475static tree
13476merge_string_cste (op1, op2, after)
13477 tree op1, op2;
13478 int after;
13479{
13480 /* Handle two string constants right away */
13481 if (TREE_CODE (op2) == STRING_CST)
13482 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13483 TREE_STRING_LENGTH (op2), after);
13484
13485 /* Reasonable integer constant can be treated right away */
13486 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13487 {
49f48c71
KG
13488 static const char *boolean_true = "true";
13489 static const char *boolean_false = "false";
13490 static const char *null_pointer = "null";
b67d701b 13491 char ch[3];
49f48c71 13492 const char *string;
b67d701b
PB
13493
13494 if (op2 == boolean_true_node)
13495 string = boolean_true;
13496 else if (op2 == boolean_false_node)
13497 string = boolean_false;
13498 else if (op2 == null_pointer_node)
13499 string = null_pointer;
13500 else if (TREE_TYPE (op2) == char_type_node)
13501 {
13502 ch[0] = (char )TREE_INT_CST_LOW (op2);
13503 ch[1] = '\0';
13504 string = ch;
13505 }
13506 else
13507 string = print_int_node (op2);
13508
13509 return do_merge_string_cste (op1, string, strlen (string), after);
13510 }
13511 return NULL_TREE;
13512}
13513
13514/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13515 has to be a STRING_CST and the other part must be a STRING_CST or a
13516 INTEGRAL constant. Return a new STRING_CST if the operation
13517 succeed, NULL_TREE otherwise.
13518
13519 If the case we want to optimize for space, we might want to return
13520 NULL_TREE for each invocation of this routine. FIXME */
13521
13522static tree
13523string_constant_concatenation (op1, op2)
13524 tree op1, op2;
13525{
13526 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13527 {
0a2138e2 13528 tree string, rest;
b67d701b
PB
13529 int invert;
13530
13531 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13532 rest = (string == op1 ? op2 : op1);
13533 invert = (string == op1 ? 0 : 1 );
13534
13535 /* Walk REST, only if it looks reasonable */
13536 if (TREE_CODE (rest) != STRING_CST
13537 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13538 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13539 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13540 {
13541 rest = java_complete_tree (rest);
13542 if (rest == error_mark_node)
13543 return error_mark_node;
13544 rest = fold (rest);
13545 }
13546 return merge_string_cste (string, rest, invert);
13547 }
13548 return NULL_TREE;
13549}
13550
13551/* Implement the `+' operator. Does static optimization if possible,
13552 otherwise create (if necessary) and append elements to a
13553 StringBuffer. The StringBuffer will be carried around until it is
13554 used for a function call or an assignment. Then toString() will be
13555 called on it to turn it into a String object. */
13556
13557static tree
13558build_string_concatenation (op1, op2)
13559 tree op1, op2;
13560{
13561 tree result;
dc0b3eff 13562 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13563
13564 if (flag_emit_xref)
13565 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13566
13567 /* Try to do some static optimization */
13568 if ((result = string_constant_concatenation (op1, op2)))
13569 return result;
13570
c0d87ff6
PB
13571 /* Discard empty strings on either side of the expression */
13572 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13573 {
13574 op1 = op2;
13575 op2 = NULL_TREE;
13576 }
c0d87ff6 13577 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13578 op2 = NULL_TREE;
b67d701b 13579
acd663ee 13580 /* If operands are string constant, turn then into object references */
b67d701b
PB
13581 if (TREE_CODE (op1) == STRING_CST)
13582 op1 = patch_string_cst (op1);
acd663ee 13583 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13584 op2 = patch_string_cst (op2);
13585
acd663ee
APB
13586 /* If either one of the constant is null and the other non null
13587 operand is a String object, return it. */
13588 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13589 return op1;
13590
b67d701b
PB
13591 /* If OP1 isn't already a StringBuffer, create and
13592 initialize a new one */
13593 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13594 {
13595 /* Two solutions here:
c52b5771
AG
13596 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13597 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13598 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13599 op1 = BUILD_STRING_BUFFER (op1);
13600 else
13601 {
13602 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13603 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13604 }
13605 }
13606
acd663ee
APB
13607 if (op2)
13608 {
13609 /* OP1 is no longer the last node holding a crafted StringBuffer */
13610 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13611 /* Create a node for `{new...,xxx}.append (op2)' */
13612 if (op2)
13613 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13614 }
13615
b67d701b
PB
13616 /* Mark the last node holding a crafted StringBuffer */
13617 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13618
13619 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13620 return op1;
13621}
13622
13623/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13624 StringBuffer. If no string were found to be patched, return
13625 NULL. */
13626
13627static tree
13628patch_string (node)
13629 tree node;
13630{
1179ebc2
APB
13631 if (node == error_mark_node)
13632 return error_mark_node;
b67d701b
PB
13633 if (TREE_CODE (node) == STRING_CST)
13634 return patch_string_cst (node);
13635 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13636 {
c877974e 13637 int saved = ctxp->explicit_constructor_p;
b67d701b 13638 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13639 tree ret;
13640 /* Temporary disable forbid the use of `this'. */
13641 ctxp->explicit_constructor_p = 0;
13642 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13643 /* String concatenation arguments must be evaluated in order too. */
13644 ret = force_evaluation_order (ret);
c877974e
APB
13645 /* Restore it at its previous value */
13646 ctxp->explicit_constructor_p = saved;
13647 return ret;
b67d701b
PB
13648 }
13649 return NULL_TREE;
13650}
13651
13652/* Build the internal representation of a string constant. */
13653
13654static tree
13655patch_string_cst (node)
13656 tree node;
13657{
13658 int location;
15fdcfe9
PB
13659 if (! flag_emit_class_files)
13660 {
15fdcfe9
PB
13661 node = get_identifier (TREE_STRING_POINTER (node));
13662 location = alloc_name_constant (CONSTANT_String, node);
13663 node = build_ref_from_constant_pool (location);
13664 }
cd9643f7 13665 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13666 TREE_CONSTANT (node) = 1;
13667 return node;
13668}
13669
13670/* Build an incomplete unary operator expression. */
e04a16fb
AG
13671
13672static tree
13673build_unaryop (op_token, op_location, op1)
13674 int op_token, op_location;
13675 tree op1;
13676{
13677 enum tree_code op;
13678 tree unaryop;
13679 switch (op_token)
13680 {
b67d701b 13681 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13682 case MINUS_TK: op = NEGATE_EXPR; break;
13683 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13684 case NOT_TK: op = BIT_NOT_EXPR; break;
400500c4 13685 default: abort ();
e04a16fb
AG
13686 }
13687
13688 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13689 TREE_SIDE_EFFECTS (unaryop) = 1;
13690 /* Store the location of the operator, for better error report. The
13691 string of the operator will be rebuild based on the OP value. */
13692 EXPR_WFL_LINECOL (unaryop) = op_location;
13693 return unaryop;
13694}
13695
13696/* Special case for the ++/-- operators, since they require an extra
13697 argument to build, which is set to NULL and patched
13698 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13699
13700static tree
13701build_incdec (op_token, op_location, op1, is_post_p)
13702 int op_token, op_location;
13703 tree op1;
13704 int is_post_p;
13705{
13706 static enum tree_code lookup [2][2] =
13707 {
13708 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13709 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13710 };
13711 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13712 NULL_TREE, op1, NULL_TREE);
13713 TREE_SIDE_EFFECTS (node) = 1;
13714 /* Store the location of the operator, for better error report. The
13715 string of the operator will be rebuild based on the OP value. */
13716 EXPR_WFL_LINECOL (node) = op_location;
13717 return node;
13718}
13719
13720/* Build an incomplete cast operator, based on the use of the
13721 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13722 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13723 though its type is already set. */
13724
13725static tree
13726build_cast (location, type, exp)
13727 int location;
13728 tree type, exp;
13729{
13730 tree node = build1 (CONVERT_EXPR, type, exp);
13731 EXPR_WFL_LINECOL (node) = location;
13732 return node;
13733}
13734
c2952b01
APB
13735/* Build an incomplete class reference operator. */
13736static tree
13737build_incomplete_class_ref (location, class_name)
13738 int location;
13739 tree class_name;
13740{
13741 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13742 EXPR_WFL_LINECOL (node) = location;
13743 return node;
13744}
13745
13746/* Complete an incomplete class reference operator. */
13747static tree
13748patch_incomplete_class_ref (node)
13749 tree node;
13750{
13751 tree type = TREE_OPERAND (node, 0);
13752 tree ref_type;
13753
13754 if (!(ref_type = resolve_type_during_patch (type)))
13755 return error_mark_node;
13756
165f37bc 13757 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13758 {
13759 /* A class referenced by `foo.class' is initialized. */
13760 return build_class_init (ref_type, build_class_ref (ref_type));
13761 }
165f37bc
APB
13762
13763 /* If we're emitting class files and we have to deal with non
13764 primitive types, we invoke (and consider generating) the
13765 synthetic static method `class$'. */
13766 if (!TYPE_DOT_CLASS (current_class))
13767 build_dot_class_method (current_class);
f0f3a777 13768 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13769 return java_complete_tree (ref_type);
c2952b01
APB
13770}
13771
e04a16fb
AG
13772/* 15.14 Unary operators. We return error_mark_node in case of error,
13773 but preserve the type of NODE if the type is fixed. */
13774
13775static tree
13776patch_unaryop (node, wfl_op)
13777 tree node;
13778 tree wfl_op;
13779{
13780 tree op = TREE_OPERAND (node, 0);
13781 tree op_type = TREE_TYPE (op);
ab3a6dd6 13782 tree prom_type = NULL_TREE, value, decl;
c2952b01 13783 int outer_field_flag = 0;
e04a16fb
AG
13784 int code = TREE_CODE (node);
13785 int error_found = 0;
13786
13787 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13788
13789 switch (code)
13790 {
13791 /* 15.13.2 Postfix Increment Operator ++ */
13792 case POSTINCREMENT_EXPR:
13793 /* 15.13.3 Postfix Increment Operator -- */
13794 case POSTDECREMENT_EXPR:
13795 /* 15.14.1 Prefix Increment Operator ++ */
13796 case PREINCREMENT_EXPR:
13797 /* 15.14.2 Prefix Decrement Operator -- */
13798 case PREDECREMENT_EXPR:
5cbdba64 13799 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13800 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13801 /* We might be trying to change an outer field accessed using
13802 access method. */
13803 if (outer_field_flag)
13804 {
13805 /* Retrieve the decl of the field we're trying to access. We
13806 do that by first retrieving the function we would call to
13807 access the field. It has been already verified that this
13808 field isn't final */
13809 if (flag_emit_class_files)
13810 decl = TREE_OPERAND (op, 0);
13811 else
13812 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13813 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13814 }
b3edebcf 13815 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13816 else if (!JDECL_P (decl)
b3edebcf
APB
13817 && TREE_CODE (decl) != COMPONENT_REF
13818 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13819 && TREE_CODE (decl) != INDIRECT_REF
13820 && !(TREE_CODE (decl) == COMPOUND_EXPR
13821 && TREE_OPERAND (decl, 1)
13822 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13823 {
5e942c50
APB
13824 tree lvalue;
13825 /* Before screaming, check that we're not in fact trying to
13826 increment a optimized static final access, in which case
13827 we issue an different error message. */
13828 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13829 && resolve_expression_name (wfl_op, &lvalue)
13830 && check_final_assignment (lvalue, wfl_op)))
13831 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13832 operator_string (node));
e04a16fb
AG
13833 TREE_TYPE (node) = error_mark_node;
13834 error_found = 1;
13835 }
c2952b01
APB
13836
13837 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13838 error_found = 1;
13839
e04a16fb
AG
13840 /* From now on, we know that op if a variable and that it has a
13841 valid wfl. We use wfl_op to locate errors related to the
13842 ++/-- operand. */
13843 else if (!JNUMERIC_TYPE_P (op_type))
13844 {
13845 parse_error_context
13846 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13847 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13848 TREE_TYPE (node) = error_mark_node;
13849 error_found = 1;
13850 }
13851 else
13852 {
4a5f66c3 13853 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13854 both operands, if really necessary */
13855 if (JINTEGRAL_TYPE_P (op_type))
13856 {
13857 value = build_int_2 (1, 0);
13858 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13859 }
13860 else
13861 {
13862 value = build_int_2 (1, 0);
13863 TREE_TYPE (node) =
13864 binary_numeric_promotion (op_type,
13865 TREE_TYPE (value), &op, &value);
13866 }
c2952b01
APB
13867
13868 /* We remember we might be accessing an outer field */
13869 if (outer_field_flag)
13870 {
13871 /* We re-generate an access to the field */
13872 value = build (PLUS_EXPR, TREE_TYPE (op),
13873 build_outer_field_access (wfl_op, decl), value);
13874
13875 /* And we patch the original access$() into a write
13876 with plus_op as a rhs */
13877 return outer_field_access_fix (node, op, value);
13878 }
13879
5cbdba64 13880 /* And write back into the node. */
4a5f66c3 13881 TREE_OPERAND (node, 0) = op;
e04a16fb 13882 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13883 /* Convert the overall back into its original type, if
13884 necessary, and return */
13885 if (JINTEGRAL_TYPE_P (op_type))
13886 return fold (node);
13887 else
13888 return fold (convert (op_type, node));
e04a16fb
AG
13889 }
13890 break;
13891
13892 /* 15.14.3 Unary Plus Operator + */
b67d701b 13893 case UNARY_PLUS_EXPR:
e04a16fb
AG
13894 /* 15.14.4 Unary Minus Operator - */
13895 case NEGATE_EXPR:
13896 if (!JNUMERIC_TYPE_P (op_type))
13897 {
13898 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13899 TREE_TYPE (node) = error_mark_node;
13900 error_found = 1;
13901 }
13902 /* Unary numeric promotion is performed on operand */
13903 else
13904 {
15fdcfe9
PB
13905 op = do_unary_numeric_promotion (op);
13906 prom_type = TREE_TYPE (op);
b67d701b 13907 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13908 return fold (op);
e04a16fb
AG
13909 }
13910 break;
13911
13912 /* 15.14.5 Bitwise Complement Operator ~ */
13913 case BIT_NOT_EXPR:
13914 if (!JINTEGRAL_TYPE_P (op_type))
13915 {
13916 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13917 TREE_TYPE (node) = error_mark_node;
13918 error_found = 1;
13919 }
13920 else
13921 {
15fdcfe9
PB
13922 op = do_unary_numeric_promotion (op);
13923 prom_type = TREE_TYPE (op);
e04a16fb
AG
13924 }
13925 break;
13926
13927 /* 15.14.6 Logical Complement Operator ! */
13928 case TRUTH_NOT_EXPR:
13929 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13930 {
13931 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13932 /* But the type is known. We will report an error if further
13933 attempt of a assignment is made with this rhs */
e04a16fb
AG
13934 TREE_TYPE (node) = boolean_type_node;
13935 error_found = 1;
13936 }
13937 else
13938 prom_type = boolean_type_node;
13939 break;
13940
13941 /* 15.15 Cast Expression */
13942 case CONVERT_EXPR:
0a2138e2 13943 value = patch_cast (node, wfl_operator);
e04a16fb 13944 if (value == error_mark_node)
c877974e
APB
13945 {
13946 /* If this cast is part of an assignment, we tell the code
13947 that deals with it not to complain about a mismatch,
13948 because things have been cast, anyways */
13949 TREE_TYPE (node) = error_mark_node;
13950 error_found = 1;
13951 }
13952 else
dc0b3eff
PB
13953 {
13954 value = fold (value);
13955 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13956 return value;
13957 }
e04a16fb
AG
13958 break;
13959 }
13960
e04a16fb
AG
13961 if (error_found)
13962 return error_mark_node;
4a5f66c3
APB
13963
13964 /* There are cases where node has been replaced by something else
13965 and we don't end up returning here: UNARY_PLUS_EXPR,
13966 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13967 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13968 TREE_TYPE (node) = prom_type;
dc0b3eff 13969 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13970 return fold (node);
13971}
13972
13973/* Generic type resolution that sometimes takes place during node
13974 patching. Returned the resolved type or generate an error
13975 message. Return the resolved type or NULL_TREE. */
13976
13977static tree
13978resolve_type_during_patch (type)
13979 tree type;
13980{
13981 if (unresolved_type_p (type, NULL))
13982 {
c0b00d37 13983 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13984 if (!type_decl)
13985 {
13986 parse_error_context (type,
13987 "Class `%s' not found in type declaration",
13988 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13989 return NULL_TREE;
13990 }
13991 else
5e942c50
APB
13992 {
13993 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13994 return TREE_TYPE (type_decl);
13995 }
e04a16fb
AG
13996 }
13997 return type;
13998}
13999/* 5.5 Casting Conversion. error_mark_node is returned if an error is
14000 found. Otherwise NODE or something meant to replace it is returned. */
14001
14002static tree
19e223db 14003patch_cast (node, wfl_op)
e04a16fb 14004 tree node;
19e223db 14005 tree wfl_op;
e04a16fb
AG
14006{
14007 tree op = TREE_OPERAND (node, 0);
14008 tree op_type = TREE_TYPE (op);
14009 tree cast_type = TREE_TYPE (node);
14010 char *t1;
14011
14012 /* First resolve OP_TYPE if unresolved */
14013 if (!(cast_type = resolve_type_during_patch (cast_type)))
14014 return error_mark_node;
14015
14016 /* Check on cast that are proven correct at compile time */
14017 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14018 {
e04a16fb
AG
14019 /* Same type */
14020 if (cast_type == op_type)
14021 return node;
14022
0b4d333e
APB
14023 /* float and double type are converted to the original type main
14024 variant and then to the target type. */
14025 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14026 op = convert (integer_type_node, op);
14027
e04a16fb
AG
14028 /* Try widening/narowwing convertion. Potentially, things need
14029 to be worked out in gcc so we implement the extreme cases
14030 correctly. fold_convert() needs to be fixed. */
14031 return convert (cast_type, op);
14032 }
14033
0b4d333e
APB
14034 /* It's also valid to cast a boolean into a boolean */
14035 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14036 return node;
14037
5e942c50
APB
14038 /* null can be casted to references */
14039 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14040 return build_null_of_type (cast_type);
14041
e04a16fb
AG
14042 /* The remaining legal casts involve conversion between reference
14043 types. Check for their compile time correctness. */
14044 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14045 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14046 {
14047 TREE_TYPE (node) = promote_type (cast_type);
14048 /* Now, the case can be determined correct at compile time if
14049 OP_TYPE can be converted into CAST_TYPE by assignment
14050 conversion (5.2) */
14051
14052 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14053 {
14054 TREE_SET_CODE (node, NOP_EXPR);
14055 return node;
14056 }
14057
14058 if (flag_emit_class_files)
14059 {
14060 TREE_SET_CODE (node, CONVERT_EXPR);
14061 return node;
14062 }
e04a16fb
AG
14063
14064 /* The cast requires a run-time check */
14065 return build (CALL_EXPR, promote_type (cast_type),
14066 build_address_of (soft_checkcast_node),
14067 tree_cons (NULL_TREE, build_class_ref (cast_type),
14068 build_tree_list (NULL_TREE, op)),
14069 NULL_TREE);
14070 }
14071
14072 /* Any other casts are proven incorrect at compile time */
c2e3db92 14073 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14074 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14075 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14076 free (t1);
14077 return error_mark_node;
14078}
14079
5e942c50
APB
14080/* Build a null constant and give it the type TYPE. */
14081
14082static tree
14083build_null_of_type (type)
14084 tree type;
14085{
14086 tree node = build_int_2 (0, 0);
14087 TREE_TYPE (node) = promote_type (type);
14088 return node;
14089}
14090
e04a16fb
AG
14091/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14092 a list of indices. */
14093static tree
14094build_array_ref (location, array, index)
14095 int location;
14096 tree array, index;
14097{
14098 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14099 EXPR_WFL_LINECOL (node) = location;
14100 return node;
14101}
14102
14103/* 15.12 Array Access Expression */
14104
14105static tree
c877974e
APB
14106patch_array_ref (node)
14107 tree node;
e04a16fb
AG
14108{
14109 tree array = TREE_OPERAND (node, 0);
14110 tree array_type = TREE_TYPE (array);
14111 tree index = TREE_OPERAND (node, 1);
14112 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14113 int error_found = 0;
14114
14115 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14116
e04a16fb
AG
14117 if (TREE_CODE (array_type) == POINTER_TYPE)
14118 array_type = TREE_TYPE (array_type);
14119
14120 /* The array reference must be an array */
14121 if (!TYPE_ARRAY_P (array_type))
14122 {
14123 parse_error_context
781b0558
KG
14124 (wfl_operator,
14125 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14126 lang_printable_name (array_type, 0));
e04a16fb
AG
14127 TREE_TYPE (node) = error_mark_node;
14128 error_found = 1;
14129 }
14130
c2952b01 14131 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14132 type must be int */
15fdcfe9
PB
14133 index = do_unary_numeric_promotion (index);
14134 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14135 {
1ebadc60 14136 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14137 parse_error_context (wfl_operator,
14138 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14139 lang_printable_name (index_type, 0));
14140 else
781b0558
KG
14141 parse_error_context (wfl_operator,
14142 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14143 lang_printable_name (index_type, 0));
e04a16fb
AG
14144 TREE_TYPE (node) = error_mark_node;
14145 error_found = 1;
14146 }
14147
e04a16fb
AG
14148 if (error_found)
14149 return error_mark_node;
e04a16fb 14150
5e942c50 14151 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14152
7f1d4866 14153 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14154 {
15fdcfe9
PB
14155 TREE_OPERAND (node, 0) = array;
14156 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14157 }
14158 else
939d7216
PB
14159 {
14160 /* The save_expr is for correct evaluation order. It would be cleaner
14161 to use force_evaluation_order (see comment there), but that is
14162 difficult when we also have to deal with bounds checking. */
14163 if (TREE_SIDE_EFFECTS (index))
14164 array = save_expr (array);
14165 node = build_java_arrayaccess (array, array_type, index);
14166 if (TREE_SIDE_EFFECTS (index))
14167 node = build (COMPOUND_EXPR, array_type, array, node);
14168 }
e04a16fb
AG
14169 TREE_TYPE (node) = array_type;
14170 return node;
14171}
14172
14173/* 15.9 Array Creation Expressions */
14174
14175static tree
14176build_newarray_node (type, dims, extra_dims)
14177 tree type;
14178 tree dims;
14179 int extra_dims;
14180{
14181 tree node =
b67d701b 14182 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14183 build_int_2 (extra_dims, 0));
e04a16fb
AG
14184 return node;
14185}
14186
14187static tree
14188patch_newarray (node)
14189 tree node;
14190{
14191 tree type = TREE_OPERAND (node, 0);
14192 tree dims = TREE_OPERAND (node, 1);
14193 tree cdim, array_type;
14194 int error_found = 0;
14195 int ndims = 0;
14196 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14197
14198 /* Dimension types are verified. It's better for the types to be
14199 verified in order. */
14200 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14201 {
14202 int dim_error = 0;
14203 tree dim = TREE_VALUE (cdim);
14204
14205 /* Dim might have been saved during its evaluation */
dba41d30 14206 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14207
14208 /* The type of each specified dimension must be an integral type. */
14209 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14210 dim_error = 1;
14211
14212 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14213 promoted type must be int. */
14214 else
14215 {
15fdcfe9 14216 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14217 if (TREE_TYPE (dim) != int_type_node)
14218 dim_error = 1;
14219 }
14220
14221 /* Report errors on types here */
14222 if (dim_error)
14223 {
14224 parse_error_context
14225 (TREE_PURPOSE (cdim),
781b0558 14226 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14227 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14228 "Explicit cast needed to" : "Can't"),
0a2138e2 14229 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14230 error_found = 1;
14231 }
14232
e04a16fb
AG
14233 TREE_PURPOSE (cdim) = NULL_TREE;
14234 }
14235
14236 /* Resolve array base type if unresolved */
14237 if (!(type = resolve_type_during_patch (type)))
14238 error_found = 1;
14239
14240 if (error_found)
14241 {
14242 /* We don't want further evaluation of this bogus array creation
14243 operation */
14244 TREE_TYPE (node) = error_mark_node;
14245 return error_mark_node;
14246 }
14247
15fdcfe9
PB
14248 /* Set array_type to the actual (promoted) array type of the result. */
14249 if (TREE_CODE (type) == RECORD_TYPE)
14250 type = build_pointer_type (type);
14251 while (--xdims >= 0)
14252 {
14253 type = promote_type (build_java_array_type (type, -1));
14254 }
14255 dims = nreverse (dims);
14256 array_type = type;
14257 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14258 {
14259 type = array_type;
05bccae2
RK
14260 array_type
14261 = build_java_array_type (type,
14262 TREE_CODE (cdim) == INTEGER_CST
14263 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14264 : -1);
15fdcfe9
PB
14265 array_type = promote_type (array_type);
14266 }
14267 dims = nreverse (dims);
14268
e04a16fb
AG
14269 /* The node is transformed into a function call. Things are done
14270 differently according to the number of dimensions. If the number
14271 of dimension is equal to 1, then the nature of the base type
14272 (primitive or not) matters. */
15fdcfe9 14273 if (ndims == 1)
fdec99c6 14274 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14275
e04a16fb
AG
14276 /* Can't reuse what's already written in expr.c because it uses the
14277 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14278 return build (CALL_EXPR, array_type,
e04a16fb 14279 build_address_of (soft_multianewarray_node),
15fdcfe9 14280 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14281 tree_cons (NULL_TREE,
15fdcfe9 14282 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14283 NULL_TREE);
14284}
14285
f8976021
APB
14286/* 10.6 Array initializer. */
14287
14288/* Build a wfl for array element that don't have one, so we can
14289 pin-point errors. */
14290
14291static tree
14292maybe_build_array_element_wfl (node)
14293 tree node;
14294{
14295 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14296 return build_expr_wfl (NULL_TREE, ctxp->filename,
14297 ctxp->elc.line, ctxp->elc.prev_col);
14298 else
14299 return NULL_TREE;
14300}
14301
14302/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14303 identification of initialized arrays easier to detect during walk
14304 and expansion. */
14305
14306static tree
14307build_new_array_init (location, values)
14308 int location;
14309 tree values;
14310{
14311 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14312 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14313 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14314 return to_return;
14315}
14316
14317/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14318 occurred. Otherwise return NODE after having set its type
14319 appropriately. */
14320
14321static tree
14322patch_new_array_init (type, node)
14323 tree type, node;
f8976021
APB
14324{
14325 int error_seen = 0;
fdec99c6 14326 tree current, element_type;
f8976021 14327 HOST_WIDE_INT length;
fdec99c6
PB
14328 int all_constant = 1;
14329 tree init = TREE_OPERAND (node, 0);
f8976021 14330
fdec99c6
PB
14331 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14332 {
14333 parse_error_context (node,
14334 "Invalid array initializer for non-array type `%s'",
14335 lang_printable_name (type, 1));
14336 return error_mark_node;
14337 }
14338 type = TREE_TYPE (type);
14339 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14340
fdec99c6
PB
14341 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14342
14343 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14344 current; length++, current = TREE_CHAIN (current))
f8976021 14345 {
fdec99c6
PB
14346 tree elt = TREE_VALUE (current);
14347 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14348 {
fdec99c6 14349 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14350 elt = TREE_VALUE (current);
14351 /* When compiling to native code, STRING_CST is converted to
14352 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14353 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14354 all_constant = 0;
f8976021 14355 }
fdec99c6
PB
14356 else
14357 {
14358 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14359 TREE_PURPOSE (current) = NULL_TREE;
14360 all_constant = 0;
14361 }
9a7ab4b3
APB
14362 if (elt && TREE_CODE (elt) == TREE_LIST
14363 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14364 error_seen = 1;
f8976021
APB
14365 }
14366
14367 if (error_seen)
14368 return error_mark_node;
14369
14370 /* Create a new type. We can't reuse the one we have here by
14371 patching its dimension because it originally is of dimension -1
14372 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14373 type = build_java_array_type (element_type, length);
14374 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14375 TREE_TYPE (node) = promote_type (type);
14376 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14377 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14378 return node;
14379}
14380
14381/* Verify that one entry of the initializer element list can be
14382 assigned to the array base type. Report 1 if an error occurred, 0
14383 otherwise. */
14384
14385static int
14386array_constructor_check_entry (type, entry)
14387 tree type, entry;
14388{
14389 char *array_type_string = NULL; /* For error reports */
14390 tree value, type_value, new_value, wfl_value, patched;
14391 int error_seen = 0;
14392
14393 new_value = NULL_TREE;
14394 wfl_value = TREE_VALUE (entry);
14395
f8976021 14396 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14397 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14398 if ((patched = patch_string (value)))
14399 value = patched;
1179ebc2
APB
14400 if (value == error_mark_node)
14401 return 1;
f8976021 14402
f8976021
APB
14403 type_value = TREE_TYPE (value);
14404
1179ebc2 14405 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14406 constant overflow during narrowing. */
14407 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14408 new_value = try_builtin_assignconv (wfl_operator, type, value);
14409 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14410 type_value = promote_type (type);
100f7cd8 14411
f8976021
APB
14412 /* Check and report errors */
14413 if (!new_value)
14414 {
49f48c71 14415 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14416 "Can't" : "Explicit cast needed to");
14417 if (!array_type_string)
c2e3db92 14418 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14419 parse_error_context
14420 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14421 msg, lang_printable_name (type_value, 1), array_type_string);
14422 error_seen = 1;
14423 }
14424
14425 if (new_value)
14426 {
b8c5b1c6 14427 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14428 TREE_VALUE (entry) = new_value;
14429 }
14430
14431 if (array_type_string)
14432 free (array_type_string);
14433
14434 TREE_PURPOSE (entry) = NULL_TREE;
14435 return error_seen;
14436}
14437
e04a16fb
AG
14438static tree
14439build_this (location)
14440 int location;
14441{
9ee9b555 14442 tree node = build_wfl_node (this_identifier_node);
b67d701b 14443 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14444 EXPR_WFL_LINECOL (node) = location;
14445 return node;
14446}
14447
14448/* 14.15 The return statement. It builds a modify expression that
14449 assigns the returned value to the RESULT_DECL that hold the value
14450 to be returned. */
14451
14452static tree
14453build_return (location, op)
14454 int location;
14455 tree op;
14456{
14457 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14458 EXPR_WFL_LINECOL (node) = location;
b67d701b 14459 node = build_debugable_stmt (location, node);
e04a16fb
AG
14460 return node;
14461}
14462
14463static tree
14464patch_return (node)
14465 tree node;
14466{
14467 tree return_exp = TREE_OPERAND (node, 0);
14468 tree meth = current_function_decl;
14469 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14470 int error_found = 0;
14471
14472 TREE_TYPE (node) = error_mark_node;
14473 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14474
14475 /* It's invalid to have a return value within a function that is
14476 declared with the keyword void or that is a constructor */
14477 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14478 error_found = 1;
14479
f099f336 14480 /* It's invalid to use a return statement in a static block */
c2952b01 14481 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14482 error_found = 1;
14483
e04a16fb
AG
14484 /* It's invalid to have a no return value within a function that
14485 isn't declared with the keyword `void' */
14486 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14487 error_found = 2;
c2952b01
APB
14488
14489 if (in_instance_initializer)
14490 error_found = 1;
e04a16fb
AG
14491
14492 if (error_found)
14493 {
c2952b01 14494 if (in_instance_initializer)
f099f336 14495 parse_error_context (wfl_operator,
c2952b01
APB
14496 "`return' inside instance initializer");
14497
14498 else if (DECL_CLINIT_P (current_function_decl))
14499 parse_error_context (wfl_operator,
14500 "`return' inside static initializer");
f099f336
APB
14501
14502 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14503 {
c2e3db92 14504 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14505 parse_error_context (wfl_operator,
14506 "`return' with%s value from `%s %s'",
14507 (error_found == 1 ? "" : "out"),
14508 t, lang_printable_name (meth, 0));
14509 free (t);
14510 }
14511 else
14512 parse_error_context (wfl_operator,
14513 "`return' with value from constructor `%s'",
14514 lang_printable_name (meth, 0));
e04a16fb
AG
14515 return error_mark_node;
14516 }
14517
5e942c50
APB
14518 /* If we have a return_exp, build a modify expression and expand
14519 it. Note: at that point, the assignment is declared valid, but we
14520 may want to carry some more hacks */
e04a16fb
AG
14521 if (return_exp)
14522 {
5e942c50
APB
14523 tree exp = java_complete_tree (return_exp);
14524 tree modify, patched;
14525
14526 /* If the function returned value and EXP are booleans, EXP has
14527 to be converted into the type of DECL_RESULT, which is integer
14528 (see complete_start_java_method) */
14529 if (TREE_TYPE (exp) == boolean_type_node &&
14530 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14531 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14532
14533 /* `null' can be assigned to a function returning a reference */
14534 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14535 exp == null_pointer_node)
14536 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14537
14538 if ((patched = patch_string (exp)))
14539 exp = patched;
14540
14541 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14542 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14543 modify = java_complete_tree (modify);
5e942c50 14544
e04a16fb
AG
14545 if (modify != error_mark_node)
14546 {
14547 TREE_SIDE_EFFECTS (modify) = 1;
14548 TREE_OPERAND (node, 0) = modify;
14549 }
14550 else
14551 return error_mark_node;
14552 }
14553 TREE_TYPE (node) = void_type_node;
14554 TREE_SIDE_EFFECTS (node) = 1;
14555 return node;
14556}
14557
14558/* 14.8 The if Statement */
14559
14560static tree
14561build_if_else_statement (location, expression, if_body, else_body)
14562 int location;
14563 tree expression, if_body, else_body;
14564{
14565 tree node;
e04a16fb 14566 if (!else_body)
9bbc7d9f 14567 else_body = empty_stmt_node;
e04a16fb
AG
14568 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14569 EXPR_WFL_LINECOL (node) = location;
b67d701b 14570 node = build_debugable_stmt (location, node);
e04a16fb
AG
14571 return node;
14572}
14573
14574static tree
14575patch_if_else_statement (node)
14576 tree node;
14577{
14578 tree expression = TREE_OPERAND (node, 0);
14579
14580 TREE_TYPE (node) = error_mark_node;
14581 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14582
14583 /* The type of expression must be boolean */
b67d701b
PB
14584 if (TREE_TYPE (expression) != boolean_type_node
14585 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14586 {
14587 parse_error_context
14588 (wfl_operator,
14589 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14590 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14591 return error_mark_node;
14592 }
14593
14594 TREE_TYPE (node) = void_type_node;
14595 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14596 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14597 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14598 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14599 return node;
14600}
14601
14602/* 14.6 Labeled Statements */
14603
14604/* Action taken when a lableled statement is parsed. a new
14605 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14606 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14607
14608static tree
0a2138e2 14609build_labeled_block (location, label)
e04a16fb 14610 int location;
0a2138e2 14611 tree label;
e04a16fb 14612{
b635eb2f 14613 tree label_name ;
e04a16fb 14614 tree label_decl, node;
b635eb2f
PB
14615 if (label == NULL_TREE || label == continue_identifier_node)
14616 label_name = label;
14617 else
e04a16fb 14618 {
b635eb2f
PB
14619 label_name = merge_qualified_name (label_id, label);
14620 /* Issue an error if we try to reuse a label that was previously
14621 declared */
14622 if (IDENTIFIER_LOCAL_VALUE (label_name))
14623 {
14624 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14625 parse_error_context (wfl_operator,
14626 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14627 IDENTIFIER_POINTER (label));
14628 EXPR_WFL_LINECOL (wfl_operator) =
14629 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14630 parse_error_context (wfl_operator,
14631 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14632 IDENTIFIER_POINTER (label));
14633 java_error_count--;
14634 }
e04a16fb
AG
14635 }
14636
14637 label_decl = create_label_decl (label_name);
14638 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14639 EXPR_WFL_LINECOL (node) = location;
14640 TREE_SIDE_EFFECTS (node) = 1;
14641 return node;
14642}
14643
b67d701b 14644/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14645
14646static tree
b635eb2f 14647finish_labeled_statement (lbe, statement)
e04a16fb
AG
14648 tree lbe; /* Labeled block expr */
14649 tree statement;
14650{
14651 /* In anyways, tie the loop to its statement */
14652 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14653 pop_labeled_block ();
14654 POP_LABELED_BLOCK ();
e04a16fb
AG
14655 return lbe;
14656}
14657
14658/* 14.10, 14.11, 14.12 Loop Statements */
14659
14660/* Create an empty LOOP_EXPR and make it the last in the nested loop
14661 list. */
14662
14663static tree
14664build_new_loop (loop_body)
14665 tree loop_body;
14666{
14667 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14668 TREE_SIDE_EFFECTS (loop) = 1;
14669 PUSH_LOOP (loop);
14670 return loop;
14671}
14672
14673/* Create a loop body according to the following structure:
14674 COMPOUND_EXPR
14675 COMPOUND_EXPR (loop main body)
14676 EXIT_EXPR (this order is for while/for loops.
14677 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14678 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14679 BODY end of this labeled block)
14680 INCREMENT (if any)
14681
14682 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14683 after the body, like in the do-while loop.
14684
14685 To obtain a loop, the loop body structure described above is
14686 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14687
14688 LABELED_BLOCK_EXPR
14689 LABEL_DECL (use this label to exit the loop)
14690 LOOP_EXPR
14691 <structure described above> */
e04a16fb
AG
14692
14693static tree
14694build_loop_body (location, condition, reversed)
14695 int location;
14696 tree condition;
14697 int reversed;
14698{
0a2138e2 14699 tree first, second, body;
e04a16fb
AG
14700
14701 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14702 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14703 condition = build_debugable_stmt (location, condition);
14704 TREE_SIDE_EFFECTS (condition) = 1;
14705
b635eb2f 14706 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14707 first = (reversed ? body : condition);
14708 second = (reversed ? condition : body);
14709 return
14710 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14711 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14712}
14713
14714/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14715 their order) on the current loop. Unlink the current loop from the
14716 loop list. */
14717
14718static tree
b635eb2f 14719finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14720 int location;
14721 tree condition, body;
14722 int reversed;
14723{
14724 tree to_return = ctxp->current_loop;
14725 tree loop_body = LOOP_EXPR_BODY (to_return);
14726 if (condition)
14727 {
14728 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14729 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14730 The real EXIT_EXPR is one operand further. */
14731 EXPR_WFL_LINECOL (cnode) = location;
14732 /* This one is for accurate error reports */
14733 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14734 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14735 }
14736 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14737 POP_LOOP ();
14738 return to_return;
14739}
14740
b635eb2f 14741/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14742 loops feature the condition part */
14743
14744static tree
b635eb2f 14745finish_for_loop (location, condition, update, body)
e04a16fb
AG
14746 int location;
14747 tree condition, update, body;
14748{
14749 /* Put the condition and the loop body in place */
b635eb2f 14750 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14751 /* LOOP is the current loop which has been now popped of the loop
14752 stack. Install the update block */
14753 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14754 return loop;
14755}
14756
5cbdba64
APB
14757/* Try to find the loop a block might be related to. This comprises
14758 the case where the LOOP_EXPR is found as the second operand of a
14759 COMPOUND_EXPR, because the loop happens to have an initialization
14760 part, then expressed as the first operand of the COMPOUND_EXPR. If
14761 the search finds something, 1 is returned. Otherwise, 0 is
14762 returned. The search is assumed to start from a
14763 LABELED_BLOCK_EXPR's block. */
14764
14765static tree
14766search_loop (statement)
14767 tree statement;
14768{
14769 if (TREE_CODE (statement) == LOOP_EXPR)
14770 return statement;
14771
14772 if (TREE_CODE (statement) == BLOCK)
14773 statement = BLOCK_SUBBLOCKS (statement);
14774 else
14775 return NULL_TREE;
14776
14777 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14778 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14779 statement = TREE_OPERAND (statement, 1);
14780
14781 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14782 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14783}
14784
14785/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14786 returned otherwise. */
14787
14788static int
14789labeled_block_contains_loop_p (block, loop)
14790 tree block, loop;
14791{
14792 if (!block)
14793 return 0;
14794
14795 if (LABELED_BLOCK_BODY (block) == loop)
14796 return 1;
14797
c2952b01 14798 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14799 return 1;
14800
14801 return 0;
14802}
14803
e04a16fb 14804/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14805 insert LOOP as its body. */
e04a16fb
AG
14806
14807static tree
14808patch_loop_statement (loop)
14809 tree loop;
14810{
cd9643f7 14811 tree loop_label;
5cbdba64 14812
cd9643f7 14813 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14814 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14815 return loop;
14816
cd9643f7 14817 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14818 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14819 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14820 LABELED_BLOCK_BODY (loop_label) = loop;
14821 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14822 return loop_label;
e04a16fb
AG
14823}
14824
14825/* 14.13, 14.14: break and continue Statements */
14826
14827/* Build a break or a continue statement. a null NAME indicates an
14828 unlabeled break/continue statement. */
14829
14830static tree
14831build_bc_statement (location, is_break, name)
14832 int location, is_break;
14833 tree name;
14834{
14835 tree break_continue, label_block_expr = NULL_TREE;
14836
14837 if (name)
14838 {
14839 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14840 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14841 /* Null means that we don't have a target for this named
14842 break/continue. In this case, we make the target to be the
14843 label name, so that the error can be reported accuratly in
14844 patch_bc_statement. */
14845 label_block_expr = EXPR_WFL_NODE (name);
14846 }
14847 /* Unlabeled break/continue will be handled during the
14848 break/continue patch operation */
14849 break_continue
14850 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14851
14852 IS_BREAK_STMT_P (break_continue) = is_break;
14853 TREE_SIDE_EFFECTS (break_continue) = 1;
14854 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14855 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14856 return break_continue;
14857}
14858
14859/* Verification of a break/continue statement. */
14860
14861static tree
14862patch_bc_statement (node)
14863 tree node;
14864{
14865 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14866 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14867 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14868
e04a16fb 14869 /* Having an identifier here means that the target is unknown. */
b635eb2f 14870 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14871 {
14872 parse_error_context (wfl_operator, "No label definition found for `%s'",
14873 IDENTIFIER_POINTER (bc_label));
14874 return error_mark_node;
14875 }
b635eb2f 14876 if (! IS_BREAK_STMT_P (node))
e04a16fb 14877 {
b635eb2f
PB
14878 /* It's a continue statement. */
14879 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14880 {
b635eb2f
PB
14881 if (labeled_block == NULL_TREE)
14882 {
14883 if (bc_label == NULL_TREE)
14884 parse_error_context (wfl_operator,
14885 "`continue' must be in loop");
14886 else
1504b2b4
APB
14887 parse_error_context
14888 (wfl_operator, "continue label `%s' does not name a loop",
14889 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14890 return error_mark_node;
14891 }
14892 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14893 == continue_identifier_node)
14894 && (bc_label == NULL_TREE
14895 || TREE_CHAIN (labeled_block) == bc_label))
14896 {
14897 bc_label = labeled_block;
14898 break;
14899 }
e04a16fb 14900 }
e04a16fb 14901 }
b635eb2f 14902 else if (!bc_label)
34f4db93 14903 {
b635eb2f 14904 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14905 {
b635eb2f
PB
14906 if (labeled_block == NULL_TREE)
14907 {
14908 parse_error_context (wfl_operator,
14909 "`break' must be in loop or switch");
14910 return error_mark_node;
14911 }
14912 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14913 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14914 || search_loop (target_stmt))
b635eb2f
PB
14915 {
14916 bc_label = labeled_block;
14917 break;
14918 }
e04a16fb 14919 }
e04a16fb
AG
14920 }
14921
b635eb2f 14922 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14923 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14924
e04a16fb
AG
14925 /* Our break/continue don't return values. */
14926 TREE_TYPE (node) = void_type_node;
14927 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14928 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14929 sometimes, like after a if statement) */
14930 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14931 TREE_SIDE_EFFECTS (node) = 1;
14932 return node;
14933}
14934
14935/* Process the exit expression belonging to a loop. Its type must be
14936 boolean. */
14937
14938static tree
14939patch_exit_expr (node)
14940 tree node;
14941{
14942 tree expression = TREE_OPERAND (node, 0);
14943 TREE_TYPE (node) = error_mark_node;
14944 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14945
14946 /* The type of expression must be boolean */
14947 if (TREE_TYPE (expression) != boolean_type_node)
14948 {
14949 parse_error_context
14950 (wfl_operator,
781b0558 14951 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14952 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14953 return error_mark_node;
14954 }
14955 /* Now we know things are allright, invert the condition, fold and
14956 return */
14957 TREE_OPERAND (node, 0) =
14958 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14959
14960 if (! integer_zerop (TREE_OPERAND (node, 0))
14961 && ctxp->current_loop != NULL_TREE
14962 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14963 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14964 if (! integer_onep (TREE_OPERAND (node, 0)))
14965 CAN_COMPLETE_NORMALLY (node) = 1;
14966
14967
e04a16fb
AG
14968 TREE_TYPE (node) = void_type_node;
14969 return node;
14970}
b67d701b
PB
14971
14972/* 14.9 Switch statement */
14973
14974static tree
14975patch_switch_statement (node)
14976 tree node;
14977{
c877974e 14978 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14979
14980 /* Complete the switch expression */
14981 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14982 se_type = TREE_TYPE (se);
14983 /* The type of the switch expression must be char, byte, short or
14984 int */
2e0f0aff 14985 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14986 {
14987 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14988 parse_error_context (wfl_operator,
14989 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14990 lang_printable_name (se_type, 0));
b67d701b
PB
14991 /* This is what java_complete_tree will check */
14992 TREE_OPERAND (node, 0) = error_mark_node;
14993 return error_mark_node;
14994 }
14995
15fdcfe9 14996 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14997
14998 /* Ready to return */
15fdcfe9 14999 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
15000 {
15001 TREE_TYPE (node) = error_mark_node;
15002 return error_mark_node;
15003 }
15004 TREE_TYPE (node) = void_type_node;
15005 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 15006 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
15007 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15008 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
15009 return node;
15010}
15011
165f37bc 15012/* 14.18 The try/catch statements */
b67d701b 15013
b67d701b 15014static tree
a7d8d81f 15015build_try_statement (location, try_block, catches)
b67d701b 15016 int location;
a7d8d81f
PB
15017 tree try_block, catches;
15018{
15019 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 15020 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 15021 return node;
b67d701b
PB
15022}
15023
a7d8d81f
PB
15024static tree
15025build_try_finally_statement (location, try_block, finally)
15026 int location;
15027 tree try_block, finally;
b67d701b 15028{
a7d8d81f
PB
15029 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15030 EXPR_WFL_LINECOL (node) = location;
15031 return node;
b67d701b
PB
15032}
15033
15034static tree
15035patch_try_statement (node)
15036 tree node;
15037{
15038 int error_found = 0;
15039 tree try = TREE_OPERAND (node, 0);
15040 /* Exception handlers are considered in left to right order */
15041 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15042 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15043
15044 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15045 to process the next catch clause. We process the catch clause before
15046 the try block so that when processing the try block we can check thrown
15047 exceptions againts the caught type list. */
b67d701b
PB
15048 for (current = catch; current; current = TREE_CHAIN (current))
15049 {
15050 tree carg_decl, carg_type;
15051 tree sub_current, catch_block, catch_clause;
15052 int unreachable;
15053
b67d701b 15054 /* At this point, the structure of the catch clause is
b67d701b
PB
15055 CATCH_EXPR (catch node)
15056 BLOCK (with the decl of the parameter)
15057 COMPOUND_EXPR
7525cc04 15058 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15059 BLOCK (catch clause block)
a7d8d81f
PB
15060 */
15061 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15062 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15063 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15064
15065 /* Catch clauses can't have more than one parameter declared,
15066 but it's already enforced by the grammar. Make sure that the
15067 only parameter of the clause statement in of class Throwable
15068 or a subclass of Throwable, but that was done earlier. The
15069 catch clause parameter type has also been resolved. */
15070
15071 /* Just make sure that the catch clause parameter type inherits
15072 from java.lang.Throwable */
15073 if (!inherits_from_p (carg_type, throwable_type_node))
15074 {
15075 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15076 parse_error_context (wfl_operator,
781b0558 15077 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15078 lang_printable_name (carg_type, 0));
b67d701b
PB
15079 error_found = 1;
15080 continue;
15081 }
15082
15083 /* Partial check for unreachable catch statement: The catch
15084 clause is reachable iff is no earlier catch block A in
15085 the try statement such that the type of the catch
15086 clause's parameter is the same as or a subclass of the
15087 type of A's parameter */
15088 unreachable = 0;
15089 for (sub_current = catch;
15090 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15091 {
15092 tree sub_catch_clause, decl;
a7d8d81f 15093 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15094 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15095
15096 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15097 {
15098 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15099 parse_error_context
781b0558
KG
15100 (wfl_operator,
15101 "`catch' not reached because of the catch clause at line %d",
15102 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15103 unreachable = error_found = 1;
15104 break;
15105 }
15106 }
b67d701b
PB
15107 /* Complete the catch clause block */
15108 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15109 if (catch_block == error_mark_node)
15110 {
15111 error_found = 1;
15112 continue;
15113 }
15fdcfe9
PB
15114 if (CAN_COMPLETE_NORMALLY (catch_block))
15115 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15116 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15117
15118 if (unreachable)
15119 continue;
15120
15121 /* Things to do here: the exception must be thrown */
15122
15123 /* Link this type to the caught type list */
15124 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15125 }
15126
b9f7e36c
APB
15127 PUSH_EXCEPTIONS (caught_type_list);
15128 if ((try = java_complete_tree (try)) == error_mark_node)
15129 error_found = 1;
15fdcfe9
PB
15130 if (CAN_COMPLETE_NORMALLY (try))
15131 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15132 POP_EXCEPTIONS ();
15133
b67d701b
PB
15134 /* Verification ends here */
15135 if (error_found)
15136 return error_mark_node;
15137
15138 TREE_OPERAND (node, 0) = try;
15139 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15140 TREE_TYPE (node) = void_type_node;
15141 return node;
15142}
b9f7e36c
APB
15143
15144/* 14.17 The synchronized Statement */
15145
15146static tree
15147patch_synchronized_statement (node, wfl_op1)
15148 tree node, wfl_op1;
15149{
5a005d9e 15150 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15151 tree block = TREE_OPERAND (node, 1);
5a005d9e 15152
c7303e41 15153 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15154
15155 if (expr == error_mark_node)
15156 {
15157 block = java_complete_tree (block);
15158 return expr;
15159 }
b9f7e36c 15160
c7303e41
APB
15161 /* We might be trying to synchronize on a STRING_CST */
15162 if ((tmp = patch_string (expr)))
15163 expr = tmp;
15164
b9f7e36c 15165 /* The TYPE of expr must be a reference type */
5a005d9e 15166 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15167 {
15168 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15169 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15170 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15171 return error_mark_node;
15172 }
15173
ce6e9147
APB
15174 if (flag_emit_xref)
15175 {
15176 TREE_OPERAND (node, 0) = expr;
15177 TREE_OPERAND (node, 1) = java_complete_tree (block);
15178 CAN_COMPLETE_NORMALLY (node) = 1;
15179 return node;
15180 }
15181
b9f7e36c
APB
15182 /* Generate a try-finally for the synchronized statement, except
15183 that the handler that catches all throw exception calls
15184 _Jv_MonitorExit and then rethrow the exception.
15185 The synchronized statement is then implemented as:
15186 TRY
15187 {
15188 _Jv_MonitorEnter (expression)
15189 synchronized_block
15190 _Jv_MonitorExit (expression)
15191 }
15192 CATCH_ALL
15193 {
15194 e = _Jv_exception_info ();
15195 _Jv_MonitorExit (expression)
15196 Throw (e);
15197 } */
15198
5a005d9e
PB
15199 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15200 BUILD_MONITOR_ENTER (enter, expr_decl);
15201 BUILD_MONITOR_EXIT (exit, expr_decl);
15202 CAN_COMPLETE_NORMALLY (enter) = 1;
15203 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15204 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15205 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15206 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15207 build (COMPOUND_EXPR, NULL_TREE,
15208 build (WITH_CLEANUP_EXPR, NULL_TREE,
15209 build (COMPOUND_EXPR, NULL_TREE,
96847892 15210 assignment, enter),
5a005d9e
PB
15211 NULL_TREE, exit),
15212 block));
15213 node = build_expr_block (node, expr_decl);
15214
15215 return java_complete_tree (node);
b9f7e36c
APB
15216}
15217
15218/* 14.16 The throw Statement */
15219
15220static tree
15221patch_throw_statement (node, wfl_op1)
15222 tree node, wfl_op1;
15223{
15224 tree expr = TREE_OPERAND (node, 0);
15225 tree type = TREE_TYPE (expr);
15226 int unchecked_ok = 0, tryblock_throws_ok = 0;
15227
15228 /* Thrown expression must be assignable to java.lang.Throwable */
15229 if (!try_reference_assignconv (throwable_type_node, expr))
15230 {
15231 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15232 parse_error_context (wfl_operator,
15233 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15234 lang_printable_name (type, 0));
b9f7e36c
APB
15235 /* If the thrown expression was a reference, we further the
15236 compile-time check. */
15237 if (!JREFERENCE_TYPE_P (type))
15238 return error_mark_node;
15239 }
15240
15241 /* At least one of the following must be true */
15242
15243 /* The type of the throw expression is a not checked exception,
15244 i.e. is a unchecked expression. */
c877974e 15245 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15246
c2952b01
APB
15247 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15248 /* An instance can't throw a checked excetion unless that exception
15249 is explicitely declared in the `throws' clause of each
15250 constructor. This doesn't apply to anonymous classes, since they
15251 don't have declared constructors. */
15252 if (!unchecked_ok
15253 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15254 {
15255 tree current;
15256 for (current = TYPE_METHODS (current_class); current;
15257 current = TREE_CHAIN (current))
15258 if (DECL_CONSTRUCTOR_P (current)
15259 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15260 {
15261 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)",
15262 lang_printable_name (TREE_TYPE (expr), 0));
15263 return error_mark_node;
15264 }
15265 }
15266
b9f7e36c
APB
15267 /* Throw is contained in a try statement and at least one catch
15268 clause can receive the thrown expression or the current method is
15269 declared to throw such an exception. Or, the throw statement is
15270 contained in a method or constructor declaration and the type of
15271 the Expression is assignable to at least one type listed in the
15272 throws clause the declaration. */
b9f7e36c 15273 if (!unchecked_ok)
f099f336 15274 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15275 if (!(unchecked_ok || tryblock_throws_ok))
15276 {
15277 /* If there is a surrounding try block that has no matching
15278 clatch clause, report it first. A surrounding try block exits
15279 only if there is something after the list of checked
15280 exception thrown by the current function (if any). */
15281 if (IN_TRY_BLOCK_P ())
781b0558 15282 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 15283 lang_printable_name (type, 0));
b9f7e36c
APB
15284 /* If we have no surrounding try statement and the method doesn't have
15285 any throws, report it now. FIXME */
f099f336
APB
15286
15287 /* We report that the exception can't be throw from a try block
15288 in all circumstances but when the `throw' is inside a static
15289 block. */
b9f7e36c
APB
15290 else if (!EXCEPTIONS_P (currently_caught_type_list)
15291 && !tryblock_throws_ok)
f099f336 15292 {
c2952b01 15293 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15294 parse_error_context (wfl_operator,
15295 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15296 lang_printable_name (type, 0));
15297 else
781b0558
KG
15298 parse_error_context (wfl_operator,
15299 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15300 lang_printable_name (type, 0));
15301 }
b9f7e36c
APB
15302 /* Otherwise, the current method doesn't have the appropriate
15303 throws declaration */
15304 else
781b0558 15305 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15306 lang_printable_name (type, 0));
b9f7e36c
APB
15307 return error_mark_node;
15308 }
15309
ce6e9147 15310 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15311 BUILD_THROW (node, expr);
ce6e9147
APB
15312
15313 /* If doing xrefs, keep the location where the `throw' was seen. */
15314 if (flag_emit_xref)
15315 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15316 return node;
15317}
15318
15319/* Check that exception said to be thrown by method DECL can be
15320 effectively caught from where DECL is invoked. */
15321
15322static void
15323check_thrown_exceptions (location, decl)
15324 int location;
15325 tree decl;
15326{
15327 tree throws;
15328 /* For all the unchecked exceptions thrown by DECL */
15329 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15330 throws = TREE_CHAIN (throws))
0a2138e2 15331 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15332 {
3e78f871
PB
15333#if 1
15334 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15335 if (DECL_NAME (decl) == get_identifier ("clone"))
15336 continue;
15337#endif
b9f7e36c 15338 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15339 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15340 parse_error_context
15341 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15342 lang_printable_name (TREE_VALUE (throws), 0));
15343 else
15344 {
15345 parse_error_context
781b0558 15346 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15347 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15348 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15349 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15350 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15351 }
b9f7e36c
APB
15352 }
15353}
15354
c877974e 15355/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15356 try-catch blocks, OR is listed in the `throws' clause of the
15357 current method. */
15358
15359static int
0a2138e2 15360check_thrown_exceptions_do (exception)
b9f7e36c
APB
15361 tree exception;
15362{
15363 tree list = currently_caught_type_list;
c877974e 15364 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15365 /* First, all the nested try-catch-finally at that stage. The
15366 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15367 if (IS_UNCHECKED_EXCEPTION_P (exception))
15368 return 1;
b9f7e36c
APB
15369 while (list)
15370 {
15371 tree caught;
15372 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15373 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15374 return 1;
15375 list = TREE_CHAIN (list);
15376 }
15377 return 0;
15378}
15379
15380static void
15381purge_unchecked_exceptions (mdecl)
15382 tree mdecl;
15383{
15384 tree throws = DECL_FUNCTION_THROWS (mdecl);
15385 tree new = NULL_TREE;
15386
15387 while (throws)
15388 {
15389 tree next = TREE_CHAIN (throws);
c877974e 15390 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15391 {
15392 TREE_CHAIN (throws) = new;
15393 new = throws;
15394 }
15395 throws = next;
15396 }
15397 /* List is inverted here, but it doesn't matter */
15398 DECL_FUNCTION_THROWS (mdecl) = new;
15399}
22eed1e6
APB
15400
15401/* 15.24 Conditional Operator ?: */
15402
15403static tree
15404patch_conditional_expr (node, wfl_cond, wfl_op1)
15405 tree node, wfl_cond, wfl_op1;
15406{
15407 tree cond = TREE_OPERAND (node, 0);
15408 tree op1 = TREE_OPERAND (node, 1);
15409 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15410 tree resulting_type = NULL_TREE;
ac825856 15411 tree t1, t2, patched;
22eed1e6
APB
15412 int error_found = 0;
15413
ac825856
APB
15414 /* Operands of ?: might be StringBuffers crafted as a result of a
15415 string concatenation. Obtain a descent operand here. */
15416 if ((patched = patch_string (op1)))
15417 TREE_OPERAND (node, 1) = op1 = patched;
15418 if ((patched = patch_string (op2)))
15419 TREE_OPERAND (node, 2) = op2 = patched;
15420
15421 t1 = TREE_TYPE (op1);
15422 t2 = TREE_TYPE (op2);
15423
22eed1e6
APB
15424 /* The first expression must be a boolean */
15425 if (TREE_TYPE (cond) != boolean_type_node)
15426 {
15427 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15428 parse_error_context (wfl_operator,
15429 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15430 lang_printable_name (TREE_TYPE (cond), 0));
15431 error_found = 1;
15432 }
15433
15434 /* Second and third can be numeric, boolean (i.e. primitive),
15435 references or null. Anything else results in an error */
15436 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15437 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15438 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15439 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15440 error_found = 1;
15441
15442 /* Determine the type of the conditional expression. Same types are
15443 easy to deal with */
15444 else if (t1 == t2)
15445 resulting_type = t1;
15446
15447 /* There are different rules for numeric types */
15448 else if (JNUMERIC_TYPE_P (t1))
15449 {
15450 /* if byte/short found, the resulting type is short */
15451 if ((t1 == byte_type_node && t2 == short_type_node)
15452 || (t1 == short_type_node && t2 == byte_type_node))
15453 resulting_type = short_type_node;
15454
15455 /* If t1 is a constant int and t2 is of type byte, short or char
15456 and t1's value fits in t2, then the resulting type is t2 */
15457 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15458 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15459 resulting_type = t2;
15460
15461 /* If t2 is a constant int and t1 is of type byte, short or char
15462 and t2's value fits in t1, then the resulting type is t1 */
15463 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15464 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15465 resulting_type = t1;
15466
15467 /* Otherwise, binary numeric promotion is applied and the
15468 resulting type is the promoted type of operand 1 and 2 */
15469 else
93024893 15470 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15471 &TREE_OPERAND (node, 1),
15472 &TREE_OPERAND (node, 2));
15473 }
15474
15475 /* Cases of a reference and a null type */
15476 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15477 resulting_type = t1;
15478
15479 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15480 resulting_type = t2;
15481
15482 /* Last case: different reference types. If a type can be converted
15483 into the other one by assignment conversion, the latter
15484 determines the type of the expression */
15485 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15486 resulting_type = promote_type (t1);
15487
15488 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15489 resulting_type = promote_type (t2);
15490
15491 /* If we don't have any resulting type, we're in trouble */
15492 if (!resulting_type)
15493 {
c2e3db92 15494 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15495 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15496 parse_error_context (wfl_operator,
15497 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15498 t, lang_printable_name (t2, 0));
22eed1e6
APB
15499 free (t);
15500 error_found = 1;
15501 }
15502
15503 if (error_found)
15504 {
15505 TREE_TYPE (node) = error_mark_node;
15506 return error_mark_node;
15507 }
15508
15509 TREE_TYPE (node) = resulting_type;
15510 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15511 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15512 return node;
15513}
ac825856 15514
5b09b33e
PB
15515/* Try to constant fold NODE.
15516 If NODE is not a constant expression, return NULL_EXPR.
15517 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15518
15519static tree
15520fold_constant_for_init (node, context)
15521 tree node;
15522 tree context;
15523{
15524 tree op0, op1, val;
15525 enum tree_code code = TREE_CODE (node);
15526
ee97d354 15527 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15528 return node;
93024893 15529
5b09b33e
PB
15530 switch (code)
15531 {
5b09b33e
PB
15532 case PLUS_EXPR:
15533 case MINUS_EXPR:
bc3ca41b
PB
15534 case MULT_EXPR:
15535 case TRUNC_MOD_EXPR:
15536 case RDIV_EXPR:
5b09b33e
PB
15537 case LSHIFT_EXPR:
15538 case RSHIFT_EXPR:
15539 case URSHIFT_EXPR:
15540 case BIT_AND_EXPR:
15541 case BIT_XOR_EXPR:
15542 case BIT_IOR_EXPR:
5b09b33e
PB
15543 case TRUTH_ANDIF_EXPR:
15544 case TRUTH_ORIF_EXPR:
15545 case EQ_EXPR:
15546 case NE_EXPR:
15547 case GT_EXPR:
15548 case GE_EXPR:
15549 case LT_EXPR:
15550 case LE_EXPR:
15551 op0 = TREE_OPERAND (node, 0);
15552 op1 = TREE_OPERAND (node, 1);
15553 val = fold_constant_for_init (op0, context);
15554 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15555 return NULL_TREE;
15556 TREE_OPERAND (node, 0) = val;
15557 val = fold_constant_for_init (op1, context);
15558 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15559 return NULL_TREE;
15560 TREE_OPERAND (node, 1) = val;
15561 return patch_binop (node, op0, op1);
15562
15563 case UNARY_PLUS_EXPR:
15564 case NEGATE_EXPR:
15565 case TRUTH_NOT_EXPR:
15566 case BIT_NOT_EXPR:
15567 case CONVERT_EXPR:
15568 op0 = TREE_OPERAND (node, 0);
15569 val = fold_constant_for_init (op0, context);
15570 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15571 return NULL_TREE;
15572 TREE_OPERAND (node, 0) = val;
5a005d9e 15573 return patch_unaryop (node, op0);
5b09b33e
PB
15574 break;
15575
15576 case COND_EXPR:
15577 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15578 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15579 return NULL_TREE;
15580 TREE_OPERAND (node, 0) = val;
15581 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15582 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15583 return NULL_TREE;
15584 TREE_OPERAND (node, 1) = val;
15585 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15586 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15587 return NULL_TREE;
15588 TREE_OPERAND (node, 2) = val;
15589 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15590 : TREE_OPERAND (node, 2);
15591
15592 case VAR_DECL:
8576f094
APB
15593 case FIELD_DECL:
15594 if (! FIELD_FINAL (node)
5b09b33e
PB
15595 || DECL_INITIAL (node) == NULL_TREE)
15596 return NULL_TREE;
15597 val = DECL_INITIAL (node);
15598 /* Guard against infinite recursion. */
15599 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15600 val = fold_constant_for_init (val, node);
5b09b33e 15601 DECL_INITIAL (node) = val;
c7303e41
APB
15602 if (!val && CLASS_FINAL_VARIABLE_P (node))
15603 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15604 return val;
15605
15606 case EXPR_WITH_FILE_LOCATION:
15607 /* Compare java_complete_tree and resolve_expression_name. */
15608 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15609 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15610 {
15611 tree name = EXPR_WFL_NODE (node);
15612 tree decl;
15613 if (PRIMARY_P (node))
15614 return NULL_TREE;
15615 else if (! QUALIFIED_P (name))
15616 {
15617 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15618 if (decl == NULL_TREE
15619 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15620 return NULL_TREE;
15621 return fold_constant_for_init (decl, decl);
15622 }
15623 else
15624 {
5b09b33e
PB
15625 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15626 qualify_ambiguous_name (node);
15627 if (resolve_field_access (node, &decl, NULL)
15628 && decl != NULL_TREE)
15629 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15630 return NULL_TREE;
15631 }
15632 }
15633 else
15634 {
15635 op0 = TREE_OPERAND (node, 0);
15636 val = fold_constant_for_init (op0, context);
15637 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15638 return NULL_TREE;
15639 TREE_OPERAND (node, 0) = val;
15640 return val;
15641 }
15642
bc3ca41b
PB
15643#ifdef USE_COMPONENT_REF
15644 case IDENTIFIER:
15645 case COMPONENT_REF:
15646 ?;
15647#endif
15648
5b09b33e
PB
15649 default:
15650 return NULL_TREE;
15651 }
15652}
bc3ca41b
PB
15653
15654#ifdef USE_COMPONENT_REF
15655/* Context is 'T' for TypeName, 'P' for PackageName,
15656 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15657
15658tree
15659resolve_simple_name (name, context)
15660 tree name;
15661 int context;
15662{
15663}
15664
15665tree
15666resolve_qualified_name (name, context)
15667 tree name;
15668 int context;
15669{
15670}
15671#endif
f15b9af9
MM
15672
15673/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15674
15675static void
15676mark_parser_ctxt (p)
15677 void *p;
15678{
15679 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15680 int i;
15681
15682 if (!pc)
15683 return;
15684
15685#ifndef JC1_LITE
15686 for (i = 0; i < 11; ++i)
15687 ggc_mark_tree (pc->modifier_ctx[i]);
15688 ggc_mark_tree (pc->class_type);
15689 ggc_mark_tree (pc->function_decl);
15690 ggc_mark_tree (pc->package);
15691 ggc_mark_tree (pc->incomplete_class);
15692 ggc_mark_tree (pc->gclass_list);
15693 ggc_mark_tree (pc->class_list);
15694 ggc_mark_tree (pc->current_parsed_class);
15695 ggc_mark_tree (pc->current_parsed_class_un);
15696 ggc_mark_tree (pc->non_static_initialized);
15697 ggc_mark_tree (pc->static_initialized);
15698 ggc_mark_tree (pc->instance_initializers);
15699 ggc_mark_tree (pc->import_list);
15700 ggc_mark_tree (pc->import_demand_list);
15701 ggc_mark_tree (pc->current_loop);
15702 ggc_mark_tree (pc->current_labeled_block);
15703#endif /* JC1_LITE */
15704
15705 if (pc->next)
15706 mark_parser_ctxt (&pc->next);
15707}
This page took 2.76203 seconds and 5 git commands to generate.