]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
jcf-parse.c (yyparse): Set/reset input_filename for source file.
[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));
df32d2ce
KG
119static tree find_name_in_single_imports PARAMS ((tree));
120static void check_abstract_method_header PARAMS ((tree));
121static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 123static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 124static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 125 tree, tree, tree));
e101152f 126static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
89e09b9a 127 int *, tree *));
df32d2ce 128static int breakdown_qualified PARAMS ((tree *, tree *, tree));
a648f4e4 129static int in_same_package PARAMS ((tree, tree));
df32d2ce 130static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 131static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
132static tree resolve_no_layout PARAMS ((tree, tree));
133static int invocation_mode PARAMS ((tree, int));
134static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 135 tree, tree));
df32d2ce 136static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 137 tree *, tree *));
df32d2ce
KG
138static tree find_most_specific_methods_list PARAMS ((tree));
139static int argument_types_convertible PARAMS ((tree, tree));
140static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 141static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
142static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144static tree obtain_incomplete_type PARAMS ((tree));
145static tree java_complete_lhs PARAMS ((tree));
146static tree java_complete_tree PARAMS ((tree));
c2952b01 147static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
dba41d30 148static int analyze_clinit_body PARAMS ((tree));
92d83515 149static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
150static void java_complete_expand_method PARAMS ((tree));
151static int unresolved_type_p PARAMS ((tree, tree *));
152static void create_jdep_list PARAMS ((struct parser_ctxt *));
153static tree build_expr_block PARAMS ((tree, tree));
154static tree enter_block PARAMS ((void));
df32d2ce
KG
155static tree exit_block PARAMS ((void));
156static tree lookup_name_in_blocks PARAMS ((tree));
157static void maybe_absorb_scoping_blocks PARAMS ((void));
158static tree build_method_invocation PARAMS ((tree, tree));
159static tree build_new_invocation PARAMS ((tree, tree));
160static tree build_assignment PARAMS ((int, int, tree, tree));
161static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162static int check_final_assignment PARAMS ((tree ,tree));
163static tree patch_assignment PARAMS ((tree, tree, tree ));
164static tree patch_binop PARAMS ((tree, tree, tree));
165static tree build_unaryop PARAMS ((int, int, tree));
166static tree build_incdec PARAMS ((int, int, tree, int));
167static tree patch_unaryop PARAMS ((tree, tree));
168static tree build_cast PARAMS ((int, tree, tree));
169static tree build_null_of_type PARAMS ((tree));
170static tree patch_cast PARAMS ((tree, tree));
171static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173static int valid_cast_to_p PARAMS ((tree, tree));
174static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176static tree try_reference_assignconv PARAMS ((tree, tree));
177static tree build_unresolved_array_type PARAMS ((tree));
178static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179static tree build_array_ref PARAMS ((int, tree, tree));
180static tree patch_array_ref PARAMS ((tree));
181static tree make_qualified_name PARAMS ((tree, tree, int));
182static tree merge_qualified_name PARAMS ((tree, tree));
183static tree make_qualified_primary PARAMS ((tree, tree, int));
184static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 185 tree *, tree *));
df32d2ce 186static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
187static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188static tree build_newarray_node PARAMS ((tree, tree, int));
189static tree patch_newarray PARAMS ((tree));
190static tree resolve_type_during_patch PARAMS ((tree));
191static tree build_this PARAMS ((int));
9a7ab4b3 192static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
193static tree build_return PARAMS ((int, tree));
194static tree patch_return PARAMS ((tree));
195static tree maybe_access_field PARAMS ((tree, tree, tree));
196static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
197static int check_for_static_method_reference PARAMS ((tree, tree, tree,
198 tree, tree));
e101152f 199static int not_accessible_p PARAMS ((tree, tree, tree, int));
df32d2ce
KG
200static void check_deprecation PARAMS ((tree, tree));
201static int class_in_current_package PARAMS ((tree));
202static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203static tree patch_if_else_statement PARAMS ((tree));
204static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206static tree patch_exit_expr PARAMS ((tree));
207static tree build_labeled_block PARAMS ((int, tree));
208static tree finish_labeled_statement PARAMS ((tree, tree));
209static tree build_bc_statement PARAMS ((int, int, tree));
210static tree patch_bc_statement PARAMS ((tree));
211static tree patch_loop_statement PARAMS ((tree));
212static tree build_new_loop PARAMS ((tree));
213static tree build_loop_body PARAMS ((int, tree, int));
214static tree finish_loop_body PARAMS ((int, tree, tree, int));
215static tree build_debugable_stmt PARAMS ((int, tree));
216static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217static tree patch_switch_statement PARAMS ((tree));
218static tree string_constant_concatenation PARAMS ((tree, tree));
219static tree build_string_concatenation PARAMS ((tree, tree));
220static tree patch_string_cst PARAMS ((tree));
221static tree patch_string PARAMS ((tree));
222static tree build_try_statement PARAMS ((int, tree, tree));
223static tree build_try_finally_statement PARAMS ((int, tree, tree));
224static tree patch_try_statement PARAMS ((tree));
225static tree patch_synchronized_statement PARAMS ((tree, tree));
226static tree patch_throw_statement PARAMS ((tree, tree));
227static void check_thrown_exceptions PARAMS ((int, tree));
228static int check_thrown_exceptions_do PARAMS ((tree));
229static void purge_unchecked_exceptions PARAMS ((tree));
230static void check_throws_clauses PARAMS ((tree, tree, tree));
231static void finish_method_declaration PARAMS ((tree));
232static tree build_super_invocation PARAMS ((tree));
233static int verify_constructor_circularity PARAMS ((tree, tree));
234static char *constructor_circularity_msg PARAMS ((tree, tree));
235static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 236 int, int));
df32d2ce
KG
237static const char *get_printable_method_name PARAMS ((tree));
238static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
239static tree generate_finit PARAMS ((tree));
240static void add_instance_initializer PARAMS ((tree));
df32d2ce 241static void fix_constructors PARAMS ((tree));
c2952b01
APB
242static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
243 tree, int *));
244static void craft_constructor PARAMS ((tree, tree));
245static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
246static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
247static void start_artificial_method_body PARAMS ((tree));
248static void end_artificial_method_body PARAMS ((tree));
249static int check_method_redefinition PARAMS ((tree, tree));
165f37bc 250static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
251static void java_check_regular_methods PARAMS ((tree));
252static void java_check_abstract_methods PARAMS ((tree));
253static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
254static void unreachable_stmt_error PARAMS ((tree));
255static tree find_expr_with_wfl PARAMS ((tree));
256static void missing_return_error PARAMS ((tree));
257static tree build_new_array_init PARAMS ((int, tree));
258static tree patch_new_array_init PARAMS ((tree, tree));
259static tree maybe_build_array_element_wfl PARAMS ((tree));
260static int array_constructor_check_entry PARAMS ((tree, tree));
261static const char *purify_type_name PARAMS ((const char *));
262static tree fold_constant_for_init PARAMS ((tree, tree));
263static tree strip_out_static_field_access_decl PARAMS ((tree));
264static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
265static void static_ref_err PARAMS ((tree, tree, tree));
266static void parser_add_interface PARAMS ((tree, tree, tree));
267static void add_superinterfaces PARAMS ((tree, tree));
268static tree jdep_resolve_class PARAMS ((jdep *));
269static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
270static void java_complete_expand_classes PARAMS ((void));
271static void java_complete_expand_class PARAMS ((tree));
272static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
273static tree cut_identifier_in_qualified PARAMS ((tree));
274static tree java_stabilize_reference PARAMS ((tree));
275static tree do_unary_numeric_promotion PARAMS ((tree));
276static char * operator_string PARAMS ((tree));
277static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
278static tree merge_string_cste PARAMS ((tree, tree, int));
279static tree java_refold PARAMS ((tree));
280static int java_decl_equiv PARAMS ((tree, tree));
281static int binop_compound_p PARAMS ((enum tree_code));
282static tree search_loop PARAMS ((tree));
283static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 284static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
285static void java_check_abstract_method_definitions PARAMS ((tree));
286static void java_debug_context_do PARAMS ((int));
c2952b01
APB
287static void java_parser_context_push_initialized_field PARAMS ((void));
288static void java_parser_context_pop_initialized_field PARAMS ((void));
289static tree reorder_static_initialized PARAMS ((tree));
290static void java_parser_context_suspend PARAMS ((void));
291static void java_parser_context_resume PARAMS ((void));
c7303e41 292static int pop_current_osb PARAMS ((struct parser_ctxt *));
c2952b01
APB
293
294/* JDK 1.1 work. FIXME */
295
296static tree maybe_make_nested_class_name PARAMS ((tree));
297static void make_nested_class_name PARAMS ((tree));
298static void set_nested_class_simple_name_value PARAMS ((tree, int));
299static void link_nested_class_to_enclosing PARAMS ((void));
300static tree find_as_inner_class PARAMS ((tree, tree, tree));
301static tree find_as_inner_class_do PARAMS ((tree, tree));
302static int check_inner_class_redefinition PARAMS ((tree, tree));
303
304static tree build_thisn_assign PARAMS ((void));
305static tree build_current_thisn PARAMS ((tree));
306static tree build_access_to_thisn PARAMS ((tree, tree, int));
307static tree maybe_build_thisn_access_method PARAMS ((tree));
308
309static tree build_outer_field_access PARAMS ((tree, tree));
310static tree build_outer_field_access_methods PARAMS ((tree));
311static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
312 tree, tree));
313static tree build_outer_method_access_method PARAMS ((tree));
314static tree build_new_access_id PARAMS ((void));
315static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
316 tree, tree));
317
318static int outer_field_access_p PARAMS ((tree, tree));
319static int outer_field_expanded_access_p PARAMS ((tree, tree *,
320 tree *, tree *));
321static tree outer_field_access_fix PARAMS ((tree, tree, tree));
322static tree build_incomplete_class_ref PARAMS ((int, tree));
323static tree patch_incomplete_class_ref PARAMS ((tree));
324static tree create_anonymous_class PARAMS ((int, tree));
325static void patch_anonymous_class PARAMS ((tree, tree, tree));
326static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 327
165f37bc
APB
328static tree build_dot_class_method PARAMS ((tree));
329static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 330static void create_new_parser_context PARAMS ((int));
f15b9af9 331static void mark_parser_ctxt PARAMS ((void *));
165f37bc 332
e04a16fb
AG
333/* Number of error found so far. */
334int java_error_count;
335/* Number of warning found so far. */
336int java_warning_count;
ce6e9147
APB
337/* Tell when not to fold, when doing xrefs */
338int do_not_fold;
c2952b01 339/* Cyclic inheritance report, as it can be set by layout_class */
7e9355c6 340const char *cyclic_inheritance_report;
f15b9af9 341
c2952b01
APB
342/* Tell when we're within an instance initializer */
343static int in_instance_initializer;
e04a16fb
AG
344
345/* The current parser context */
d4370213 346struct parser_ctxt *ctxp;
e04a16fb 347
d4370213 348/* List of things that were analyzed for which code will be generated */
fea2d5da 349struct parser_ctxt *ctxp_for_generation = NULL;
b351b287 350
e04a16fb
AG
351/* binop_lookup maps token to tree_code. It is used where binary
352 operations are involved and required by the parser. RDIV_EXPR
353 covers both integral/floating point division. The code is changed
354 once the type of both operator is worked out. */
355
356static enum tree_code binop_lookup[19] =
357 {
358 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
359 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
360 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
361 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
362 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
363 };
364#define BINOP_LOOKUP(VALUE) \
6e2aa220 365 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 366
5cbdba64
APB
367/* This is the end index for binary operators that can also be used
368 in compound assignements. */
369#define BINOP_COMPOUND_CANDIDATES 11
370
e04a16fb 371/* The "$L" identifier we use to create labels. */
b67d701b
PB
372static tree label_id = NULL_TREE;
373
374/* The "StringBuffer" identifier used for the String `+' operator. */
375static tree wfl_string_buffer = NULL_TREE;
376
377/* The "append" identifier used for String `+' operator. */
378static tree wfl_append = NULL_TREE;
379
380/* The "toString" identifier used for String `+' operator. */
381static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
382
383/* The "java.lang" import qualified name. */
384static tree java_lang_id = NULL_TREE;
09ed0f70 385
c2952b01
APB
386/* The generated `inst$' identifier used for generated enclosing
387 instance/field access functions. */
388static tree inst_id = NULL_TREE;
389
09ed0f70
APB
390/* The "java.lang.Cloneable" qualified name. */
391static tree java_lang_cloneable = NULL_TREE;
f099f336 392
ee17a290
TT
393/* The "java.io.Serializable" qualified name. */
394static tree java_io_serializable = NULL_TREE;
395
f099f336
APB
396/* Context and flag for static blocks */
397static tree current_static_block = NULL_TREE;
398
c2952b01
APB
399/* The generated `write_parm_value$' identifier. */
400static tree wpv_id;
401
ee07f4f4
APB
402/* The list of all packages we've seen so far */
403static tree package_list = NULL_TREE;
2884c41e 404
19e223db
MM
405/* Hold THIS for the scope of the current public method decl. */
406static tree current_this;
407
408/* Hold a list of catch clauses list. The first element of this list is
409 the list of the catch clauses of the currently analysed try block. */
410static tree currently_caught_type_list;
411
fea2d5da
PB
412static tree src_parse_roots[2] = { NULL_TREE, NULL_TREE };
413
414/* All classes seen from source code */
415#define gclass_list src_parse_roots[0]
416
417/* List of non-complete classes */
418#define incomplete_class_list src_parse_roots[1]
419
2884c41e
KG
420/* Check modifiers. If one doesn't fit, retrieve it in its declaration
421 line and point it out. */
422/* Should point out the one that don't fit. ASCII/unicode, going
423 backward. FIXME */
424
425#define check_modifiers(__message, __value, __mask) do { \
426 if ((__value) & ~(__mask)) \
427 { \
428 int i, remainder = (__value) & ~(__mask); \
429 for (i = 0; i <= 10; i++) \
430 if ((1 << i) & remainder) \
431 parse_error_context (ctxp->modifier_ctx [i], (__message), \
432 java_accstring_lookup (1 << i)); \
433 } \
434} while (0)
ee07f4f4 435
e04a16fb
AG
436%}
437
438%union {
439 tree node;
440 int sub_token;
441 struct {
442 int token;
443 int location;
444 } operator;
445 int value;
446}
447
9ee9b555
KG
448%{
449#include "lex.c"
450%}
451
e04a16fb
AG
452%pure_parser
453
454/* Things defined here have to match the order of what's in the
455 binop_lookup table. */
456
457%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
458%token LS_TK SRS_TK ZRS_TK
459%token AND_TK XOR_TK OR_TK
460%token BOOL_AND_TK BOOL_OR_TK
461%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
462
463/* This maps to the same binop_lookup entry than the token above */
464
465%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
466%token REM_ASSIGN_TK
467%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
468%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
469
470
471/* Modifier TOKEN have to be kept in this order. Don't scramble it */
472
473%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
474%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
475%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
476%token PAD_TK ABSTRACT_TK MODIFIER_TK
d828bc42 477%token STRICT_TK
e04a16fb
AG
478
479/* Keep those two in order, too */
480%token DECR_TK INCR_TK
481
482/* From now one, things can be in any order */
483
484%token DEFAULT_TK IF_TK THROW_TK
485%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
486%token THROWS_TK BREAK_TK IMPORT_TK
487%token ELSE_TK INSTANCEOF_TK RETURN_TK
488%token VOID_TK CATCH_TK INTERFACE_TK
489%token CASE_TK EXTENDS_TK FINALLY_TK
490%token SUPER_TK WHILE_TK CLASS_TK
491%token SWITCH_TK CONST_TK TRY_TK
492%token FOR_TK NEW_TK CONTINUE_TK
493%token GOTO_TK PACKAGE_TK THIS_TK
494
495%token BYTE_TK SHORT_TK INT_TK LONG_TK
496%token CHAR_TK INTEGRAL_TK
497
498%token FLOAT_TK DOUBLE_TK FP_TK
499
500%token ID_TK
501
502%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
503
504%token ASSIGN_ANY_TK ASSIGN_TK
505%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
506
507%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
508%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
509
c2952b01 510%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
511
512%type <node> super ID_TK identifier
513%type <node> name simple_name qualified_name
c2952b01 514%type <node> type_declaration compilation_unit
e04a16fb
AG
515 field_declaration method_declaration extends_interfaces
516 interfaces interface_type_list
c2952b01 517 class_member_declaration
e04a16fb
AG
518 import_declarations package_declaration
519 type_declarations interface_body
520 interface_member_declaration constant_declaration
521 interface_member_declarations interface_type
522 abstract_method_declaration interface_type_list
523%type <node> class_body_declaration class_member_declaration
524 static_initializer constructor_declaration block
22eed1e6 525%type <node> class_body_declarations constructor_header
e04a16fb
AG
526%type <node> class_or_interface_type class_type class_type_list
527 constructor_declarator explicit_constructor_invocation
b9f7e36c 528%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
529
530%type <node> variable_declarator_id variable_declarator
531 variable_declarators variable_initializer
22eed1e6 532 variable_initializers constructor_body
ac825856 533 array_initializer
e04a16fb 534
2e5eb5c5 535%type <node> class_body block_end constructor_block_end
e04a16fb
AG
536%type <node> statement statement_without_trailing_substatement
537 labeled_statement if_then_statement label_decl
538 if_then_else_statement while_statement for_statement
539 statement_nsi labeled_statement_nsi do_statement
540 if_then_else_statement_nsi while_statement_nsi
541 for_statement_nsi statement_expression_list for_init
542 for_update statement_expression expression_statement
543 primary_no_new_array expression primary
544 array_creation_expression array_type
545 class_instance_creation_expression field_access
546 method_invocation array_access something_dot_new
547 argument_list postfix_expression while_expression
548 post_increment_expression post_decrement_expression
549 unary_expression_not_plus_minus unary_expression
550 pre_increment_expression pre_decrement_expression
551 unary_expression_not_plus_minus cast_expression
552 multiplicative_expression additive_expression
553 shift_expression relational_expression
554 equality_expression and_expression
555 exclusive_or_expression inclusive_or_expression
556 conditional_and_expression conditional_or_expression
557 conditional_expression assignment_expression
558 left_hand_side assignment for_header for_begin
559 constant_expression do_statement_begin empty_statement
b67d701b 560 switch_statement synchronized_statement throw_statement
f8976021 561 try_statement switch_expression switch_block
15fdcfe9 562 catches catch_clause catch_clause_parameter finally
c2952b01 563 anonymous_class_creation
e04a16fb
AG
564%type <node> return_statement break_statement continue_statement
565
566%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
567%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
568%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
569%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
570%type <operator> ASSIGN_ANY_TK assignment_operator
571%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
572%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
573%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 574%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 575%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
576%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
577%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 578%type <operator> NEW_TK
e04a16fb
AG
579
580%type <node> method_body
581
582%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
583 STRING_LIT_TK NULL_TK VOID_TK
584
585%type <node> IF_TK WHILE_TK FOR_TK
586
587%type <node> formal_parameter_list formal_parameter
588 method_declarator method_header
589
c2952b01 590%type <node> primitive_type reference_type type
e04a16fb
AG
591 BOOLEAN_TK INTEGRAL_TK FP_TK
592
c2952b01 593/* Added or modified JDK 1.1 rule types */
c7303e41 594%type <node> type_literals
c2952b01 595
e04a16fb
AG
596%%
597/* 19.2 Production from 2.3: The Syntactic Grammar */
598goal:
19e223db
MM
599 {
600 /* Register static variables with the garbage
601 collector. */
602 ggc_add_tree_root (&label_id, 1);
603 ggc_add_tree_root (&wfl_string_buffer, 1);
604 ggc_add_tree_root (&wfl_append, 1);
605 ggc_add_tree_root (&wfl_to_string, 1);
606 ggc_add_tree_root (&java_lang_id, 1);
607 ggc_add_tree_root (&inst_id, 1);
608 ggc_add_tree_root (&java_lang_cloneable, 1);
609 ggc_add_tree_root (&java_io_serializable, 1);
610 ggc_add_tree_root (&current_static_block, 1);
611 ggc_add_tree_root (&wpv_id, 1);
612 ggc_add_tree_root (&package_list, 1);
613 ggc_add_tree_root (&current_this, 1);
614 ggc_add_tree_root (&currently_caught_type_list, 1);
f15b9af9
MM
615 ggc_add_root (&ctxp, 1,
616 sizeof (struct parser_ctxt *),
617 mark_parser_ctxt);
618 ggc_add_root (&ctxp_for_generation, 1,
619 sizeof (struct parser_ctxt *),
620 mark_parser_ctxt);
19e223db 621 }
e04a16fb
AG
622 compilation_unit
623 {}
624;
625
626/* 19.3 Productions from 3: Lexical structure */
627literal:
628 INT_LIT_TK
629| FP_LIT_TK
630| BOOL_LIT_TK
631| CHAR_LIT_TK
632| STRING_LIT_TK
633| NULL_TK
634;
635
636/* 19.4 Productions from 4: Types, Values and Variables */
637type:
638 primitive_type
639| reference_type
640;
641
642primitive_type:
643 INTEGRAL_TK
644| FP_TK
645| BOOLEAN_TK
646;
647
648reference_type:
649 class_or_interface_type
650| array_type
651;
652
653class_or_interface_type:
654 name
655;
656
657class_type:
658 class_or_interface_type /* Default rule */
659;
660
661interface_type:
662 class_or_interface_type
663;
664
665array_type:
c7303e41 666 primitive_type dims
e04a16fb 667 {
c7303e41
APB
668 int osb = pop_current_osb (ctxp);
669 tree t = build_java_array_type (($1), -1);
670 CLASS_LOADED_P (t) = 1;
671 while (--osb)
672 t = build_unresolved_array_type (t);
673 $$ = t;
674 }
675| name dims
676 {
677 int osb = pop_current_osb (ctxp);
678 tree t = $1;
679 while (osb--)
680 t = build_unresolved_array_type (t);
681 $$ = t;
e04a16fb 682 }
e04a16fb
AG
683;
684
685/* 19.5 Productions from 6: Names */
686name:
687 simple_name /* Default rule */
688| qualified_name /* Default rule */
689;
690
691simple_name:
692 identifier /* Default rule */
693;
694
695qualified_name:
696 name DOT_TK identifier
697 { $$ = make_qualified_name ($1, $3, $2.location); }
698;
699
700identifier:
701 ID_TK
702;
703
704/* 19.6: Production from 7: Packages */
705compilation_unit:
706 {$$ = NULL;}
707| package_declaration
708| import_declarations
709| type_declarations
710| package_declaration import_declarations
711| package_declaration type_declarations
712| import_declarations type_declarations
713| package_declaration import_declarations type_declarations
714;
715
716import_declarations:
717 import_declaration
718 {
719 $$ = NULL;
720 }
721| import_declarations import_declaration
722 {
723 $$ = NULL;
724 }
725;
726
727type_declarations:
728 type_declaration
729| type_declarations type_declaration
730;
731
732package_declaration:
733 PACKAGE_TK name SC_TK
ee07f4f4
APB
734 {
735 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 736 register_package (ctxp->package);
ee07f4f4 737 }
e04a16fb
AG
738| PACKAGE_TK error
739 {yyerror ("Missing name"); RECOVER;}
740| PACKAGE_TK name error
741 {yyerror ("';' expected"); RECOVER;}
742;
743
744import_declaration:
745 single_type_import_declaration
746| type_import_on_demand_declaration
747;
748
749single_type_import_declaration:
750 IMPORT_TK name SC_TK
751 {
9a7ab4b3 752 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 753 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 754 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
755 while (last != IDENTIFIER_POINTER (name))
756 {
757 if (last [0] == '.')
758 break;
759 last--;
760 }
761 last_name = get_identifier (++last);
762 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
763 {
764 tree err = find_name_in_single_imports (last_name);
765 if (err && err != name)
766 parse_error_context
767 ($2, "Ambiguous class: `%s' and `%s'",
768 IDENTIFIER_POINTER (name),
769 IDENTIFIER_POINTER (err));
5e942c50 770 else
9a7ab4b3 771 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
772 }
773 else
5e942c50 774 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
775 }
776| IMPORT_TK error
777 {yyerror ("Missing name"); RECOVER;}
778| IMPORT_TK name error
779 {yyerror ("';' expected"); RECOVER;}
780;
781
782type_import_on_demand_declaration:
783 IMPORT_TK name DOT_TK MULT_TK SC_TK
784 {
785 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
786 /* Don't import java.lang.* twice. */
787 if (name != java_lang_id)
788 {
ba179f9f 789 read_import_dir ($2);
9a7ab4b3
APB
790 ctxp->import_demand_list =
791 chainon (ctxp->import_demand_list,
792 build_tree_list ($2, NULL_TREE));
ba179f9f 793 }
e04a16fb
AG
794 }
795| IMPORT_TK name DOT_TK error
796 {yyerror ("'*' expected"); RECOVER;}
797| IMPORT_TK name DOT_TK MULT_TK error
798 {yyerror ("';' expected"); RECOVER;}
799;
800
801type_declaration:
802 class_declaration
c2952b01 803 { end_class_declaration (0); }
e04a16fb 804| interface_declaration
c2952b01 805 { end_class_declaration (0); }
5f1c312a 806| empty_statement
e04a16fb
AG
807| error
808 {
809 YYERROR_NOW;
810 yyerror ("Class or interface declaration expected");
811 }
812;
813
814/* 19.7 Shortened from the original:
815 modifiers: modifier | modifiers modifier
816 modifier: any of public... */
817modifiers:
818 MODIFIER_TK
819 {
820 $$ = (1 << $1);
821 }
822| modifiers MODIFIER_TK
823 {
824 int acc = (1 << $2);
825 if ($$ & acc)
826 parse_error_context
827 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
828 java_accstring_lookup (acc));
829 else
830 {
831 $$ |= acc;
832 }
833 }
834;
835
836/* 19.8.1 Production from $8.1: Class Declaration */
837class_declaration:
838 modifiers CLASS_TK identifier super interfaces
839 { create_class ($1, $3, $4, $5); }
840 class_body
e04a16fb
AG
841| CLASS_TK identifier super interfaces
842 { create_class (0, $2, $3, $4); }
843 class_body
e04a16fb
AG
844| modifiers CLASS_TK error
845 {yyerror ("Missing class name"); RECOVER;}
846| CLASS_TK error
847 {yyerror ("Missing class name"); RECOVER;}
848| CLASS_TK identifier error
0b4d333e
APB
849 {
850 if (!ctxp->class_err) yyerror ("'{' expected");
851 DRECOVER(class1);
852 }
e04a16fb
AG
853| modifiers CLASS_TK identifier error
854 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
855;
856
857super:
858 { $$ = NULL; }
859| EXTENDS_TK class_type
860 { $$ = $2; }
861| EXTENDS_TK class_type error
862 {yyerror ("'{' expected"); ctxp->class_err=1;}
863| EXTENDS_TK error
864 {yyerror ("Missing super class name"); ctxp->class_err=1;}
865;
866
867interfaces:
868 { $$ = NULL_TREE; }
869| IMPLEMENTS_TK interface_type_list
870 { $$ = $2; }
871| IMPLEMENTS_TK error
872 {
873 ctxp->class_err=1;
874 yyerror ("Missing interface name");
875 }
876;
877
878interface_type_list:
879 interface_type
880 {
881 ctxp->interface_number = 1;
882 $$ = build_tree_list ($1, NULL_TREE);
883 }
884| interface_type_list C_TK interface_type
885 {
886 ctxp->interface_number++;
887 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
888 }
889| interface_type_list C_TK error
890 {yyerror ("Missing interface name"); RECOVER;}
891;
892
893class_body:
894 OCB_TK CCB_TK
7f10c2e2
APB
895 {
896 /* Store the location of the `}' when doing xrefs */
897 if (flag_emit_xref)
c2952b01 898 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 899 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 900 $$ = GET_CPC ();
7f10c2e2 901 }
e04a16fb 902| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
903 {
904 /* Store the location of the `}' when doing xrefs */
905 if (flag_emit_xref)
c2952b01 906 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 907 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 908 $$ = GET_CPC ();
7f10c2e2 909 }
e04a16fb
AG
910;
911
912class_body_declarations:
913 class_body_declaration
914| class_body_declarations class_body_declaration
915;
916
917class_body_declaration:
918 class_member_declaration
919| static_initializer
920| constructor_declaration
921| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
922 {
923 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
924 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
925 }
e04a16fb
AG
926;
927
928class_member_declaration:
929 field_declaration
930| method_declaration
931| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
932 { end_class_declaration (1); }
933| interface_declaration /* Added, JDK1.1 inner interfaces */
934 { end_class_declaration (1); }
5f1c312a 935| empty_statement
e04a16fb
AG
936;
937
938/* 19.8.2 Productions from 8.3: Field Declarations */
939field_declaration:
940 type variable_declarators SC_TK
941 { register_fields (0, $1, $2); }
942| modifiers type variable_declarators SC_TK
943 {
e04a16fb
AG
944 check_modifiers
945 ("Illegal modifier `%s' for field declaration",
946 $1, FIELD_MODIFIERS);
947 check_modifiers_consistency ($1);
948 register_fields ($1, $2, $3);
949 }
950;
951
952variable_declarators:
953 /* Should we use build_decl_list () instead ? FIXME */
954 variable_declarator /* Default rule */
955| variable_declarators C_TK variable_declarator
956 { $$ = chainon ($1, $3); }
957| variable_declarators C_TK error
958 {yyerror ("Missing term"); RECOVER;}
959;
960
961variable_declarator:
962 variable_declarator_id
963 { $$ = build_tree_list ($1, NULL_TREE); }
964| variable_declarator_id ASSIGN_TK variable_initializer
965 {
966 if (java_error_count)
967 $3 = NULL_TREE;
968 $$ = build_tree_list
969 ($1, build_assignment ($2.token, $2.location, $1, $3));
970 }
971| variable_declarator_id ASSIGN_TK error
972 {
973 yyerror ("Missing variable initializer");
974 $$ = build_tree_list ($1, NULL_TREE);
975 RECOVER;
976 }
977| variable_declarator_id ASSIGN_TK variable_initializer error
978 {
979 yyerror ("';' expected");
980 $$ = build_tree_list ($1, NULL_TREE);
981 RECOVER;
982 }
983;
984
985variable_declarator_id:
986 identifier
987| variable_declarator_id OSB_TK CSB_TK
c583dd46 988 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
989| identifier error
990 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
991| variable_declarator_id OSB_TK error
29f8b718
APB
992 {
993 tree node = java_lval.node;
994 if (node && (TREE_CODE (node) == INTEGER_CST
995 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
996 yyerror ("Can't specify array dimension in a declaration");
997 else
998 yyerror ("']' expected");
999 DRECOVER(vdi);
1000 }
e04a16fb
AG
1001| variable_declarator_id CSB_TK error
1002 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1003;
1004
1005variable_initializer:
1006 expression
1007| array_initializer
e04a16fb
AG
1008;
1009
1010/* 19.8.3 Productions from 8.4: Method Declarations */
1011method_declaration:
1012 method_header
1013 {
1014 current_function_decl = $1;
c2952b01
APB
1015 if (current_function_decl
1016 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1017 source_start_java_method (current_function_decl);
1018 else
1019 current_function_decl = NULL_TREE;
e04a16fb
AG
1020 }
1021 method_body
b635eb2f 1022 { finish_method_declaration ($3); }
e04a16fb
AG
1023| method_header error
1024 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1025;
1026
1027method_header:
1028 type method_declarator throws
b9f7e36c 1029 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1030| VOID_TK method_declarator throws
b9f7e36c 1031 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1032| modifiers type method_declarator throws
b9f7e36c 1033 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1034| modifiers VOID_TK method_declarator throws
b9f7e36c 1035 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1036| type error
efa0a23f
APB
1037 {
1038 yyerror ("Invalid method declaration, method name required");
1039 RECOVER;
1040 }
e04a16fb
AG
1041| modifiers type error
1042 {RECOVER;}
1043| VOID_TK error
1044 {yyerror ("Identifier expected"); RECOVER;}
1045| modifiers VOID_TK error
1046 {yyerror ("Identifier expected"); RECOVER;}
1047| modifiers error
1048 {
1049 yyerror ("Invalid method declaration, return type required");
1050 RECOVER;
1051 }
1052;
1053
1054method_declarator:
1055 identifier OP_TK CP_TK
c2952b01
APB
1056 {
1057 ctxp->formal_parameter_number = 0;
1058 $$ = method_declarator ($1, NULL_TREE);
1059 }
e04a16fb
AG
1060| identifier OP_TK formal_parameter_list CP_TK
1061 { $$ = method_declarator ($1, $3); }
1062| method_declarator OSB_TK CSB_TK
1063 {
1886c9d8
APB
1064 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1065 TREE_PURPOSE ($1) =
1066 build_unresolved_array_type (TREE_PURPOSE ($1));
1067 parse_warning_context
1068 (wfl_operator,
1069 "Discouraged form of returned type specification");
e04a16fb
AG
1070 }
1071| identifier OP_TK error
1072 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1073| method_declarator OSB_TK error
1074 {yyerror ("']' expected"); RECOVER;}
1075;
1076
1077formal_parameter_list:
1078 formal_parameter
1079 {
1080 ctxp->formal_parameter_number = 1;
1081 }
1082| formal_parameter_list C_TK formal_parameter
1083 {
1084 ctxp->formal_parameter_number += 1;
1085 $$ = chainon ($1, $3);
1086 }
1087| formal_parameter_list C_TK error
c2952b01 1088 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1089;
1090
1091formal_parameter:
1092 type variable_declarator_id
1093 {
1094 $$ = build_tree_list ($2, $1);
1095 }
18990de5 1096| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1097 {
5256aa37 1098 $$ = build_tree_list ($3, $2);
c2952b01 1099 ARG_FINAL_P ($$) = 1;
5256aa37 1100 }
e04a16fb 1101| type error
f8989a66
APB
1102 {
1103 yyerror ("Missing identifier"); RECOVER;
1104 $$ = NULL_TREE;
1105 }
18990de5 1106| final type error
e04a16fb 1107 {
e04a16fb 1108 yyerror ("Missing identifier"); RECOVER;
f8989a66 1109 $$ = NULL_TREE;
e04a16fb
AG
1110 }
1111;
1112
18990de5
JB
1113final:
1114 modifiers
1115 {
1116 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1117 $1, ACC_FINAL);
1118 if ($1 != ACC_FINAL)
1119 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1120 }
1121;
1122
e04a16fb 1123throws:
b9f7e36c 1124 { $$ = NULL_TREE; }
e04a16fb 1125| THROWS_TK class_type_list
b9f7e36c 1126 { $$ = $2; }
e04a16fb
AG
1127| THROWS_TK error
1128 {yyerror ("Missing class type term"); RECOVER;}
1129;
1130
1131class_type_list:
1132 class_type
c877974e 1133 { $$ = build_tree_list ($1, $1); }
e04a16fb 1134| class_type_list C_TK class_type
c877974e 1135 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1136| class_type_list C_TK error
1137 {yyerror ("Missing class type term"); RECOVER;}
1138;
1139
1140method_body:
1141 block
5f1c312a 1142| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1143;
1144
1145/* 19.8.4 Productions from 8.5: Static Initializers */
1146static_initializer:
1147 static block
1148 {
c2952b01
APB
1149 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1150 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
dba41d30 1151 current_static_block = NULL_TREE;
e04a16fb 1152 }
e04a16fb
AG
1153;
1154
1155static: /* Test lval.sub_token here */
c2952b01 1156 modifiers
e04a16fb 1157 {
c2952b01
APB
1158 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1159 /* Can't have a static initializer in an innerclass */
1160 if ($1 | ACC_STATIC &&
1161 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1162 parse_error_context
1163 (MODIFIER_WFL (STATIC_TK),
1164 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1165 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1166 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1167 }
1168;
1169
1170/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1171constructor_declaration:
22eed1e6 1172 constructor_header
e04a16fb 1173 {
22eed1e6
APB
1174 current_function_decl = $1;
1175 source_start_java_method (current_function_decl);
e04a16fb 1176 }
22eed1e6 1177 constructor_body
b635eb2f 1178 { finish_method_declaration ($3); }
22eed1e6
APB
1179;
1180
1181constructor_header:
1182 constructor_declarator throws
1183 { $$ = method_header (0, NULL_TREE, $1, $2); }
1184| modifiers constructor_declarator throws
1185 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1186;
1187
1188constructor_declarator:
1189 simple_name OP_TK CP_TK
c2952b01
APB
1190 {
1191 ctxp->formal_parameter_number = 0;
1192 $$ = method_declarator ($1, NULL_TREE);
1193 }
e04a16fb 1194| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1195 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1196;
1197
1198constructor_body:
22eed1e6
APB
1199 /* Unlike regular method, we always need a complete (empty)
1200 body so we can safely perform all the required code
1201 addition (super invocation and field initialization) */
2e5eb5c5 1202 block_begin constructor_block_end
22eed1e6 1203 {
9bbc7d9f 1204 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1205 $$ = $2;
1206 }
2e5eb5c5 1207| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1208 { $$ = $3; }
2e5eb5c5 1209| block_begin block_statements constructor_block_end
22eed1e6 1210 { $$ = $3; }
2e5eb5c5 1211| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1212 { $$ = $4; }
e04a16fb
AG
1213;
1214
2e5eb5c5
APB
1215constructor_block_end:
1216 block_end
5f1c312a 1217;
2e5eb5c5 1218
e04a16fb
AG
1219/* Error recovery for that rule moved down expression_statement: rule. */
1220explicit_constructor_invocation:
1221 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1222 {
1223 $$ = build_method_invocation ($1, NULL_TREE);
1224 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1225 $$ = java_method_add_stmt (current_function_decl, $$);
1226 }
e04a16fb 1227| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1228 {
1229 $$ = build_method_invocation ($1, $3);
1230 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1231 $$ = java_method_add_stmt (current_function_decl, $$);
1232 }
e04a16fb
AG
1233 /* Added, JDK1.1 inner classes. Modified because the rule
1234 'primary' couldn't work. */
1235| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1236 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1237| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1238 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1239;
1240
1241this_or_super: /* Added, simplifies error diagnostics */
1242 THIS_TK
1243 {
9ee9b555 1244 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1245 EXPR_WFL_LINECOL (wfl) = $1.location;
1246 $$ = wfl;
1247 }
1248| SUPER_TK
1249 {
9ee9b555 1250 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1251 EXPR_WFL_LINECOL (wfl) = $1.location;
1252 $$ = wfl;
1253 }
1254;
1255
1256/* 19.9 Productions from 9: Interfaces */
1257/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1258interface_declaration:
1259 INTERFACE_TK identifier
1260 { create_interface (0, $2, NULL_TREE); }
1261 interface_body
e04a16fb
AG
1262| modifiers INTERFACE_TK identifier
1263 { create_interface ($1, $3, NULL_TREE); }
1264 interface_body
e04a16fb
AG
1265| INTERFACE_TK identifier extends_interfaces
1266 { create_interface (0, $2, $3); }
1267 interface_body
e04a16fb
AG
1268| modifiers INTERFACE_TK identifier extends_interfaces
1269 { create_interface ($1, $3, $4); }
1270 interface_body
e04a16fb 1271| INTERFACE_TK identifier error
0b4d333e 1272 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1273| modifiers INTERFACE_TK identifier error
0b4d333e 1274 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1275;
1276
1277extends_interfaces:
1278 EXTENDS_TK interface_type
1279 {
1280 ctxp->interface_number = 1;
1281 $$ = build_tree_list ($2, NULL_TREE);
1282 }
1283| extends_interfaces C_TK interface_type
1284 {
1285 ctxp->interface_number++;
1286 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1287 }
1288| EXTENDS_TK error
1289 {yyerror ("Invalid interface type"); RECOVER;}
1290| extends_interfaces C_TK error
1291 {yyerror ("Missing term"); RECOVER;}
1292;
1293
1294interface_body:
1295 OCB_TK CCB_TK
1296 { $$ = NULL_TREE; }
1297| OCB_TK interface_member_declarations CCB_TK
1298 { $$ = NULL_TREE; }
1299;
1300
1301interface_member_declarations:
1302 interface_member_declaration
1303| interface_member_declarations interface_member_declaration
1304;
1305
1306interface_member_declaration:
1307 constant_declaration
1308| abstract_method_declaration
1309| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1310 { end_class_declaration (1); }
1311| interface_declaration /* Added, JDK1.1 inner interfaces */
1312 { end_class_declaration (1); }
e04a16fb
AG
1313;
1314
1315constant_declaration:
1316 field_declaration
1317;
1318
1319abstract_method_declaration:
1320 method_header SC_TK
1321 {
1322 check_abstract_method_header ($1);
1323 current_function_decl = NULL_TREE; /* FIXME ? */
1324 }
1325| method_header error
1326 {yyerror ("';' expected"); RECOVER;}
1327;
1328
1329/* 19.10 Productions from 10: Arrays */
1330array_initializer:
1331 OCB_TK CCB_TK
1179ebc2 1332 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1333| OCB_TK variable_initializers CCB_TK
f8976021 1334 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1335| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1336 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1337;
1338
1339variable_initializers:
1340 variable_initializer
f8976021
APB
1341 {
1342 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1343 $1, NULL_TREE);
1344 }
e04a16fb 1345| variable_initializers C_TK variable_initializer
1179ebc2
APB
1346 {
1347 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1348 }
e04a16fb
AG
1349| variable_initializers C_TK error
1350 {yyerror ("Missing term"); RECOVER;}
1351;
1352
1353/* 19.11 Production from 14: Blocks and Statements */
1354block:
1355 OCB_TK CCB_TK
7f10c2e2
APB
1356 {
1357 /* Store the location of the `}' when doing xrefs */
1358 if (current_function_decl && flag_emit_xref)
1359 DECL_END_SOURCE_LINE (current_function_decl) =
1360 EXPR_WFL_ADD_COL ($2.location, 1);
1361 $$ = empty_stmt_node;
1362 }
22eed1e6
APB
1363| block_begin block_statements block_end
1364 { $$ = $3; }
1365;
1366
1367block_begin:
1368 OCB_TK
e04a16fb 1369 { enter_block (); }
22eed1e6
APB
1370;
1371
1372block_end:
e04a16fb
AG
1373 CCB_TK
1374 {
1375 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1376 /* Store the location of the `}' when doing xrefs */
1377 if (current_function_decl && flag_emit_xref)
1378 DECL_END_SOURCE_LINE (current_function_decl) =
1379 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1380 $$ = exit_block ();
c280e37a
APB
1381 if (!BLOCK_SUBBLOCKS ($$))
1382 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1383 }
1384;
1385
1386block_statements:
1387 block_statement
1388| block_statements block_statement
1389;
1390
1391block_statement:
1392 local_variable_declaration_statement
1393| statement
15fdcfe9 1394 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1395| class_declaration /* Added, JDK1.1 local classes */
1396 {
1397 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1398 end_class_declaration (1);
1399 }
e04a16fb
AG
1400;
1401
1402local_variable_declaration_statement:
1403 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1404;
1405
1406local_variable_declaration:
1407 type variable_declarators
1408 { declare_local_variables (0, $1, $2); }
a003f638 1409| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1410 { declare_local_variables ($1, $2, $3); }
1411;
1412
1413statement:
1414 statement_without_trailing_substatement
1415| labeled_statement
e04a16fb 1416| if_then_statement
e04a16fb 1417| if_then_else_statement
e04a16fb 1418| while_statement
e04a16fb 1419| for_statement
cd9643f7 1420 { $$ = exit_block (); }
e04a16fb
AG
1421;
1422
1423statement_nsi:
1424 statement_without_trailing_substatement
1425| labeled_statement_nsi
e04a16fb 1426| if_then_else_statement_nsi
e04a16fb 1427| while_statement_nsi
e04a16fb 1428| for_statement_nsi
9dd939b2 1429 { $$ = exit_block (); }
e04a16fb
AG
1430;
1431
1432statement_without_trailing_substatement:
1433 block
e04a16fb 1434| empty_statement
e04a16fb 1435| expression_statement
e04a16fb 1436| switch_statement
e04a16fb 1437| do_statement
e04a16fb 1438| break_statement
e04a16fb 1439| continue_statement
e04a16fb
AG
1440| return_statement
1441| synchronized_statement
e04a16fb 1442| throw_statement
e04a16fb 1443| try_statement
e04a16fb
AG
1444;
1445
1446empty_statement:
1447 SC_TK
5f1c312a
APB
1448 {
1449 if (flag_extraneous_semicolon)
1450 {
1451 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1452 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1453 }
1454 $$ = empty_stmt_node;
1455 }
e04a16fb
AG
1456;
1457
1458label_decl:
1459 identifier REL_CL_TK
1460 {
1461 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1462 EXPR_WFL_NODE ($1));
e04a16fb
AG
1463 pushlevel (2);
1464 push_labeled_block ($$);
1465 PUSH_LABELED_BLOCK ($$);
1466 }
1467;
1468
1469labeled_statement:
1470 label_decl statement
b635eb2f 1471 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1472| identifier error
1473 {yyerror ("':' expected"); RECOVER;}
1474;
1475
1476labeled_statement_nsi:
1477 label_decl statement_nsi
b635eb2f 1478 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1479;
1480
1481/* We concentrate here a bunch of error handling rules that we couldn't write
1482 earlier, because expression_statement catches a missing ';'. */
1483expression_statement:
1484 statement_expression SC_TK
1485 {
1486 /* We have a statement. Generate a WFL around it so
1487 we can debug it */
1488 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1489 /* We know we have a statement, so set the debug
1490 info to be eventually generate here. */
1491 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1492 }
1493| error SC_TK
1494 {
29f8b718 1495 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1496 DRECOVER (expr_stmt);
1497 }
1498| error OCB_TK
1499 {
29f8b718 1500 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1501 DRECOVER (expr_stmt);
1502 }
1503| error CCB_TK
1504 {
29f8b718 1505 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1506 DRECOVER (expr_stmt);
1507 }
1508| this_or_super OP_TK error
1509 {yyerror ("')' expected"); RECOVER;}
1510| this_or_super OP_TK CP_TK error
22eed1e6 1511 {
8119c720 1512 parse_ctor_invocation_error ();
22eed1e6
APB
1513 RECOVER;
1514 }
e04a16fb
AG
1515| this_or_super OP_TK argument_list error
1516 {yyerror ("')' expected"); RECOVER;}
1517| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1518 {
8119c720 1519 parse_ctor_invocation_error ();
22eed1e6
APB
1520 RECOVER;
1521 }
e04a16fb
AG
1522| name DOT_TK SUPER_TK error
1523 {yyerror ("'(' expected"); RECOVER;}
1524| name DOT_TK SUPER_TK OP_TK error
1525 {yyerror ("')' expected"); RECOVER;}
1526| name DOT_TK SUPER_TK OP_TK argument_list error
1527 {yyerror ("')' expected"); RECOVER;}
1528| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1529 {yyerror ("';' expected"); RECOVER;}
1530| name DOT_TK SUPER_TK OP_TK CP_TK error
1531 {yyerror ("';' expected"); RECOVER;}
1532;
1533
1534statement_expression:
1535 assignment
1536| pre_increment_expression
e04a16fb 1537| pre_decrement_expression
e04a16fb 1538| post_increment_expression
e04a16fb 1539| post_decrement_expression
e04a16fb
AG
1540| method_invocation
1541| class_instance_creation_expression
e04a16fb
AG
1542;
1543
1544if_then_statement:
1545 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1546 {
1547 $$ = build_if_else_statement ($2.location, $3,
1548 $5, NULL_TREE);
1549 }
e04a16fb
AG
1550| IF_TK error
1551 {yyerror ("'(' expected"); RECOVER;}
1552| IF_TK OP_TK error
1553 {yyerror ("Missing term"); RECOVER;}
1554| IF_TK OP_TK expression error
1555 {yyerror ("')' expected"); RECOVER;}
1556;
1557
1558if_then_else_statement:
1559 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1560 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1561;
1562
1563if_then_else_statement_nsi:
1564 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1565 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1566;
1567
1568switch_statement:
15fdcfe9
PB
1569 switch_expression
1570 {
1571 enter_block ();
1572 }
1573 switch_block
b67d701b 1574 {
15fdcfe9 1575 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1576 I.e. make the last statment also have its own
1577 COMPOUND_EXPR. */
15fdcfe9
PB
1578 maybe_absorb_scoping_blocks ();
1579 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1580 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1581 }
1582;
1583
1584switch_expression:
1585 SWITCH_TK OP_TK expression CP_TK
1586 {
1587 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1588 EXPR_WFL_LINECOL ($$) = $2.location;
1589 }
e04a16fb
AG
1590| SWITCH_TK error
1591 {yyerror ("'(' expected"); RECOVER;}
1592| SWITCH_TK OP_TK error
1593 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1594| SWITCH_TK OP_TK expression CP_TK error
1595 {yyerror ("'{' expected"); RECOVER;}
1596;
1597
f8976021
APB
1598/* Default assignment is there to avoid type node on switch_block
1599 node. */
1600
e04a16fb
AG
1601switch_block:
1602 OCB_TK CCB_TK
f8976021 1603 { $$ = NULL_TREE; }
e04a16fb 1604| OCB_TK switch_labels CCB_TK
f8976021 1605 { $$ = NULL_TREE; }
e04a16fb 1606| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1607 { $$ = NULL_TREE; }
e04a16fb 1608| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1609 { $$ = NULL_TREE; }
e04a16fb
AG
1610;
1611
1612switch_block_statement_groups:
1613 switch_block_statement_group
1614| switch_block_statement_groups switch_block_statement_group
1615;
1616
1617switch_block_statement_group:
15fdcfe9 1618 switch_labels block_statements
e04a16fb
AG
1619;
1620
e04a16fb
AG
1621switch_labels:
1622 switch_label
1623| switch_labels switch_label
1624;
1625
1626switch_label:
1627 CASE_TK constant_expression REL_CL_TK
b67d701b 1628 {
15fdcfe9
PB
1629 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1630 EXPR_WFL_LINECOL (lab) = $1.location;
1631 java_method_add_stmt (current_function_decl, lab);
b67d701b 1632 }
e04a16fb 1633| DEFAULT_TK REL_CL_TK
b67d701b 1634 {
ac39dac0 1635 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
15fdcfe9
PB
1636 EXPR_WFL_LINECOL (lab) = $1.location;
1637 java_method_add_stmt (current_function_decl, lab);
b67d701b 1638 }
e04a16fb
AG
1639| CASE_TK error
1640 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1641| CASE_TK constant_expression error
1642 {yyerror ("':' expected"); RECOVER;}
1643| DEFAULT_TK error
1644 {yyerror ("':' expected"); RECOVER;}
1645;
1646
1647while_expression:
1648 WHILE_TK OP_TK expression CP_TK
1649 {
1650 tree body = build_loop_body ($2.location, $3, 0);
1651 $$ = build_new_loop (body);
1652 }
1653;
1654
1655while_statement:
1656 while_expression statement
b635eb2f 1657 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1658| WHILE_TK error
1659 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1660| WHILE_TK OP_TK error
1661 {yyerror ("Missing term and ')' expected"); RECOVER;}
1662| WHILE_TK OP_TK expression error
1663 {yyerror ("')' expected"); RECOVER;}
1664;
1665
1666while_statement_nsi:
1667 while_expression statement_nsi
b635eb2f 1668 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1669;
1670
1671do_statement_begin:
1672 DO_TK
1673 {
1674 tree body = build_loop_body (0, NULL_TREE, 1);
1675 $$ = build_new_loop (body);
1676 }
1677 /* Need error handing here. FIXME */
1678;
1679
1680do_statement:
1681 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1682 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1683;
1684
1685for_statement:
1686 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1687 {
1688 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1689 $3 = build_wfl_node ($3);
1690 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1691 }
e04a16fb
AG
1692| for_begin SC_TK SC_TK for_update CP_TK statement
1693 {
b635eb2f 1694 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1695 /* We have not condition, so we get rid of the EXIT_EXPR */
1696 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1697 empty_stmt_node;
e04a16fb
AG
1698 }
1699| for_begin SC_TK error
1700 {yyerror ("Invalid control expression"); RECOVER;}
1701| for_begin SC_TK expression SC_TK error
1702 {yyerror ("Invalid update expression"); RECOVER;}
1703| for_begin SC_TK SC_TK error
1704 {yyerror ("Invalid update expression"); RECOVER;}
1705;
1706
1707for_statement_nsi:
1708 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1709 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1710| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1711 {
b635eb2f 1712 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1713 /* We have not condition, so we get rid of the EXIT_EXPR */
1714 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1715 empty_stmt_node;
e04a16fb
AG
1716 }
1717;
1718
1719for_header:
1720 FOR_TK OP_TK
1721 {
1722 /* This scope defined for local variable that may be
1723 defined within the scope of the for loop */
1724 enter_block ();
1725 }
1726| FOR_TK error
1727 {yyerror ("'(' expected"); DRECOVER(for_1);}
1728| FOR_TK OP_TK error
1729 {yyerror ("Invalid init statement"); RECOVER;}
1730;
1731
1732for_begin:
1733 for_header for_init
1734 {
1735 /* We now declare the loop body. The loop is
1736 declared as a for loop. */
1737 tree body = build_loop_body (0, NULL_TREE, 0);
1738 $$ = build_new_loop (body);
c2952b01 1739 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1740 /* The loop is added to the current block the for
1741 statement is defined within */
1742 java_method_add_stmt (current_function_decl, $$);
1743 }
1744;
1745for_init: /* Can be empty */
9bbc7d9f 1746 { $$ = empty_stmt_node; }
e04a16fb
AG
1747| statement_expression_list
1748 {
1749 /* Init statement recorded within the previously
1750 defined block scope */
1751 $$ = java_method_add_stmt (current_function_decl, $1);
1752 }
1753| local_variable_declaration
1754 {
1755 /* Local variable are recorded within the previously
1756 defined block scope */
1757 $$ = NULL_TREE;
1758 }
1759| statement_expression_list error
1760 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1761;
1762
1763for_update: /* Can be empty */
9bbc7d9f 1764 {$$ = empty_stmt_node;}
e04a16fb
AG
1765| statement_expression_list
1766 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1767;
1768
1769statement_expression_list:
1770 statement_expression
1771 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1772| statement_expression_list C_TK statement_expression
1773 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1774| statement_expression_list C_TK error
1775 {yyerror ("Missing term"); RECOVER;}
1776;
1777
1778break_statement:
1779 BREAK_TK SC_TK
1780 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1781| BREAK_TK identifier SC_TK
1782 { $$ = build_bc_statement ($1.location, 1, $2); }
1783| BREAK_TK error
1784 {yyerror ("Missing term"); RECOVER;}
1785| BREAK_TK identifier error
1786 {yyerror ("';' expected"); RECOVER;}
1787;
1788
1789continue_statement:
1790 CONTINUE_TK SC_TK
1791 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1792| CONTINUE_TK identifier SC_TK
1793 { $$ = build_bc_statement ($1.location, 0, $2); }
1794| CONTINUE_TK error
1795 {yyerror ("Missing term"); RECOVER;}
1796| CONTINUE_TK identifier error
1797 {yyerror ("';' expected"); RECOVER;}
1798;
1799
1800return_statement:
1801 RETURN_TK SC_TK
1802 { $$ = build_return ($1.location, NULL_TREE); }
1803| RETURN_TK expression SC_TK
1804 { $$ = build_return ($1.location, $2); }
1805| RETURN_TK error
1806 {yyerror ("Missing term"); RECOVER;}
1807| RETURN_TK expression error
1808 {yyerror ("';' expected"); RECOVER;}
1809;
1810
1811throw_statement:
1812 THROW_TK expression SC_TK
b9f7e36c
APB
1813 {
1814 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1815 EXPR_WFL_LINECOL ($$) = $1.location;
1816 }
e04a16fb
AG
1817| THROW_TK error
1818 {yyerror ("Missing term"); RECOVER;}
1819| THROW_TK expression error
1820 {yyerror ("';' expected"); RECOVER;}
1821;
1822
1823synchronized_statement:
1824 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1825 {
1826 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1827 EXPR_WFL_LINECOL ($$) =
1828 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1829 }
e04a16fb
AG
1830| synchronized OP_TK expression CP_TK error
1831 {yyerror ("'{' expected"); RECOVER;}
1832| synchronized error
1833 {yyerror ("'(' expected"); RECOVER;}
1834| synchronized OP_TK error CP_TK
1835 {yyerror ("Missing term"); RECOVER;}
1836| synchronized OP_TK error
1837 {yyerror ("Missing term"); RECOVER;}
1838;
1839
b9f7e36c 1840synchronized:
efa0a23f 1841 modifiers
e04a16fb 1842 {
781b0558
KG
1843 check_modifiers (
1844 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1845 $1, ACC_SYNCHRONIZED);
1846 if ($1 != ACC_SYNCHRONIZED)
1847 MODIFIER_WFL (SYNCHRONIZED_TK) =
1848 build_wfl_node (NULL_TREE);
e04a16fb
AG
1849 }
1850;
1851
1852try_statement:
1853 TRY_TK block catches
a7d8d81f 1854 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1855| TRY_TK block finally
a7d8d81f 1856 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1857| TRY_TK block catches finally
2aa11e97
APB
1858 { $$ = build_try_finally_statement
1859 ($1.location, build_try_statement ($1.location,
1860 $2, $3), $4);
1861 }
e04a16fb
AG
1862| TRY_TK error
1863 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1864;
1865
1866catches:
1867 catch_clause
1868| catches catch_clause
b67d701b
PB
1869 {
1870 TREE_CHAIN ($2) = $1;
1871 $$ = $2;
1872 }
e04a16fb
AG
1873;
1874
1875catch_clause:
b67d701b
PB
1876 catch_clause_parameter block
1877 {
1878 java_method_add_stmt (current_function_decl, $2);
1879 exit_block ();
1880 $$ = $1;
1881 }
1882
1883catch_clause_parameter:
1884 CATCH_TK OP_TK formal_parameter CP_TK
1885 {
1886 /* We add a block to define a scope for
1887 formal_parameter (CCBP). The formal parameter is
1888 declared initialized by the appropriate function
1889 call */
1890 tree ccpb = enter_block ();
1891 tree init = build_assignment (ASSIGN_TK, $2.location,
1892 TREE_PURPOSE ($3),
1893 soft_exceptioninfo_call_node);
1894 declare_local_variables (0, TREE_VALUE ($3),
1895 build_tree_list (TREE_PURPOSE ($3),
1896 init));
1897 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1898 EXPR_WFL_LINECOL ($$) = $1.location;
1899 }
e04a16fb 1900| CATCH_TK error
97f30284 1901 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1902| CATCH_TK OP_TK error
97f30284
APB
1903 {
1904 yyerror ("Missing term or ')' expected");
1905 RECOVER; $$ = NULL_TREE;
1906 }
b67d701b 1907| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1908 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1909;
1910
1911finally:
1912 FINALLY_TK block
a7d8d81f 1913 { $$ = $2; }
e04a16fb
AG
1914| FINALLY_TK error
1915 {yyerror ("'{' expected"); RECOVER; }
1916;
1917
1918/* 19.12 Production from 15: Expressions */
1919primary:
1920 primary_no_new_array
1921| array_creation_expression
1922;
1923
1924primary_no_new_array:
1925 literal
1926| THIS_TK
1927 { $$ = build_this ($1.location); }
1928| OP_TK expression CP_TK
1929 {$$ = $2;}
1930| class_instance_creation_expression
1931| field_access
1932| method_invocation
1933| array_access
c2952b01 1934| type_literals
e04a16fb
AG
1935 /* Added, JDK1.1 inner classes. Documentation is wrong
1936 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1937 exist. Used name: instead. */
e04a16fb 1938| name DOT_TK THIS_TK
c2952b01
APB
1939 {
1940 tree wfl = build_wfl_node (this_identifier_node);
1941 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1942 }
e04a16fb
AG
1943| OP_TK expression error
1944 {yyerror ("')' expected"); RECOVER;}
1945| name DOT_TK error
1946 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1947| primitive_type DOT_TK error
1948 {yyerror ("'class' expected" ); RECOVER;}
1949| VOID_TK DOT_TK error
1950 {yyerror ("'class' expected" ); RECOVER;}
1951;
1952
c2952b01
APB
1953type_literals:
1954 name DOT_TK CLASS_TK
1955 { $$ = build_incomplete_class_ref ($2.location, $1); }
c7303e41 1956| array_type DOT_TK CLASS_TK
c2952b01
APB
1957 { $$ = build_incomplete_class_ref ($2.location, $1); }
1958| primitive_type DOT_TK CLASS_TK
1959 { $$ = build_class_ref ($1); }
1960| VOID_TK DOT_TK CLASS_TK
1961 { $$ = build_class_ref (void_type_node); }
1962;
1963
e04a16fb
AG
1964class_instance_creation_expression:
1965 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1966 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1967| NEW_TK class_type OP_TK CP_TK
b67d701b 1968 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1969| anonymous_class_creation
e04a16fb
AG
1970 /* Added, JDK1.1 inner classes, modified to use name or
1971 primary instead of primary solely which couldn't work in
1972 all situations. */
1973| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1974 {
1975 tree ctor = build_new_invocation ($2, NULL_TREE);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1978 }
e04a16fb
AG
1979| something_dot_new identifier OP_TK CP_TK class_body
1980| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1981 {
1982 tree ctor = build_new_invocation ($2, $4);
1983 $$ = make_qualified_primary ($1, ctor,
1984 EXPR_WFL_LINECOL ($1));
1985 }
e04a16fb
AG
1986| something_dot_new identifier OP_TK argument_list CP_TK class_body
1987| NEW_TK error SC_TK
1988 {yyerror ("'(' expected"); DRECOVER(new_1);}
1989| NEW_TK class_type error
1990 {yyerror ("'(' expected"); RECOVER;}
1991| NEW_TK class_type OP_TK error
1992 {yyerror ("')' or term expected"); RECOVER;}
1993| NEW_TK class_type OP_TK argument_list error
1994 {yyerror ("')' expected"); RECOVER;}
1995| something_dot_new error
1996 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1997| something_dot_new identifier error
1998 {yyerror ("'(' expected"); RECOVER;}
1999;
2000
c2952b01
APB
2001/* Created after JDK1.1 rules originally added to
2002 class_instance_creation_expression, but modified to use
2003 'class_type' instead of 'TypeName' (type_name) which is mentionned
2004 in the documentation but doesn't exist. */
2005
2006anonymous_class_creation:
2007 NEW_TK class_type OP_TK argument_list CP_TK
2008 { create_anonymous_class ($1.location, $2); }
2009 class_body
2010 {
2011 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2012 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2013
2014 end_class_declaration (1);
2015
2016 /* Now we can craft the new expression */
2017 $$ = build_new_invocation (id, $4);
2018
2019 /* Note that we can't possibly be here if
2020 `class_type' is an interface (in which case the
2021 anonymous class extends Object and implements
2022 `class_type', hence its constructor can't have
2023 arguments.) */
2024
2025 /* Otherwise, the innerclass must feature a
2026 constructor matching `argument_list'. Anonymous
2027 classes are a bit special: it's impossible to
2028 define constructor for them, hence constructors
2029 must be generated following the hints provided by
2030 the `new' expression. Whether a super constructor
2031 of that nature exists or not is to be verified
2032 later on in verify_constructor_super.
2033
2034 It's during the expansion of a `new' statement
2035 refering to an anonymous class that a ctor will
2036 be generated for the anonymous class, with the
2037 right arguments. */
2038
2039 }
2040| NEW_TK class_type OP_TK CP_TK
2041 { create_anonymous_class ($1.location, $2); }
2042 class_body
2043 {
2044 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2045 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2046
2047 end_class_declaration (1);
2048
2049 /* Now we can craft the new expression. The
2050 statement doesn't need to be remember so that a
2051 constructor can be generated, since its signature
2052 is already known. */
2053 $$ = build_new_invocation (id, NULL_TREE);
2054 }
2055;
2056
e04a16fb
AG
2057something_dot_new: /* Added, not part of the specs. */
2058 name DOT_TK NEW_TK
c2952b01 2059 { $$ = $1; }
e04a16fb 2060| primary DOT_TK NEW_TK
c2952b01 2061 { $$ = $1; }
e04a16fb
AG
2062;
2063
2064argument_list:
2065 expression
2066 {
2067 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2068 ctxp->formal_parameter_number = 1;
2069 }
2070| argument_list C_TK expression
2071 {
2072 ctxp->formal_parameter_number += 1;
2073 $$ = tree_cons (NULL_TREE, $3, $1);
2074 }
2075| argument_list C_TK error
2076 {yyerror ("Missing term"); RECOVER;}
2077;
2078
2079array_creation_expression:
2080 NEW_TK primitive_type dim_exprs
2081 { $$ = build_newarray_node ($2, $3, 0); }
2082| NEW_TK class_or_interface_type dim_exprs
2083 { $$ = build_newarray_node ($2, $3, 0); }
2084| NEW_TK primitive_type dim_exprs dims
c7303e41 2085 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb 2086| NEW_TK class_or_interface_type dim_exprs dims
c7303e41 2087 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
e04a16fb
AG
2088 /* Added, JDK1.1 anonymous array. Initial documentation rule
2089 modified */
2090| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2091 {
2092 char *sig;
c7303e41
APB
2093 int osb = pop_current_osb (ctxp);
2094 while (osb--)
c2952b01
APB
2095 obstack_1grow (&temporary_obstack, '[');
2096 sig = obstack_finish (&temporary_obstack);
2097 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2098 $2, get_identifier (sig), $4);
2099 }
e04a16fb 2100| NEW_TK primitive_type dims array_initializer
c2952b01 2101 {
c7303e41 2102 int osb = pop_current_osb (ctxp);
c2952b01 2103 tree type = $2;
c7303e41 2104 while (osb--)
c2952b01
APB
2105 type = build_java_array_type (type, -1);
2106 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2107 build_pointer_type (type), NULL_TREE, $4);
2108 }
e04a16fb
AG
2109| NEW_TK error CSB_TK
2110 {yyerror ("'[' expected"); DRECOVER ("]");}
2111| NEW_TK error OSB_TK
2112 {yyerror ("']' expected"); RECOVER;}
2113;
2114
2115dim_exprs:
2116 dim_expr
2117 { $$ = build_tree_list (NULL_TREE, $1); }
2118| dim_exprs dim_expr
2119 { $$ = tree_cons (NULL_TREE, $2, $$); }
2120;
2121
2122dim_expr:
2123 OSB_TK expression CSB_TK
2124 {
9a7ab4b3
APB
2125 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2126 {
2127 $2 = build_wfl_node ($2);
2128 TREE_TYPE ($2) = NULL_TREE;
2129 }
e04a16fb
AG
2130 EXPR_WFL_LINECOL ($2) = $1.location;
2131 $$ = $2;
2132 }
2133| OSB_TK expression error
2134 {yyerror ("']' expected"); RECOVER;}
2135| OSB_TK error
2136 {
2137 yyerror ("Missing term");
2138 yyerror ("']' expected");
2139 RECOVER;
2140 }
2141;
2142
2143dims:
2144 OSB_TK CSB_TK
ba179f9f
APB
2145 {
2146 int allocate = 0;
2147 /* If not initialized, allocate memory for the osb
2148 numbers stack */
2149 if (!ctxp->osb_limit)
2150 {
2151 allocate = ctxp->osb_limit = 32;
2152 ctxp->osb_depth = -1;
2153 }
c2952b01 2154 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2155 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2156 allocate = ctxp->osb_limit << 1;
2157
2158 if (allocate)
2159 {
2160 allocate *= sizeof (int);
2161 if (ctxp->osb_number)
2162 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2163 allocate);
2164 else
2165 ctxp->osb_number = (int *)xmalloc (allocate);
2166 }
2167 ctxp->osb_depth++;
2168 CURRENT_OSB (ctxp) = 1;
2169 }
e04a16fb 2170| dims OSB_TK CSB_TK
ba179f9f 2171 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2172| dims OSB_TK error
2173 { yyerror ("']' expected"); RECOVER;}
2174;
2175
2176field_access:
2177 primary DOT_TK identifier
2178 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2179 /* FIXME - REWRITE TO:
2180 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2181| SUPER_TK DOT_TK identifier
2182 {
6e22695a 2183 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2184 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2185 $$ = make_qualified_name (super_wfl, $3, $2.location);
2186 }
2187| SUPER_TK error
2188 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2189;
2190
2191method_invocation:
2192 name OP_TK CP_TK
2193 { $$ = build_method_invocation ($1, NULL_TREE); }
2194| name OP_TK argument_list CP_TK
2195 { $$ = build_method_invocation ($1, $3); }
2196| primary DOT_TK identifier OP_TK CP_TK
2197 {
22eed1e6
APB
2198 if (TREE_CODE ($1) == THIS_EXPR)
2199 $$ = build_this_super_qualified_invocation
2200 (1, $3, NULL_TREE, 0, $2.location);
2201 else
2202 {
2203 tree invok = build_method_invocation ($3, NULL_TREE);
2204 $$ = make_qualified_primary ($1, invok, $2.location);
2205 }
e04a16fb
AG
2206 }
2207| primary DOT_TK identifier OP_TK argument_list CP_TK
2208 {
22eed1e6
APB
2209 if (TREE_CODE ($1) == THIS_EXPR)
2210 $$ = build_this_super_qualified_invocation
2211 (1, $3, $5, 0, $2.location);
2212 else
2213 {
2214 tree invok = build_method_invocation ($3, $5);
2215 $$ = make_qualified_primary ($1, invok, $2.location);
2216 }
e04a16fb
AG
2217 }
2218| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2219 {
2220 $$ = build_this_super_qualified_invocation
2221 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2222 }
2223| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2224 {
22eed1e6
APB
2225 $$ = build_this_super_qualified_invocation
2226 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2227 }
2228 /* Screws up thing. I let it here until I'm convinced it can
2229 be removed. FIXME
2230| primary DOT_TK error
2231 {yyerror ("'(' expected"); DRECOVER(bad);} */
2232| SUPER_TK DOT_TK error CP_TK
2233 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2234| SUPER_TK DOT_TK error DOT_TK
2235 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2236;
2237
2238array_access:
2239 name OSB_TK expression CSB_TK
2240 { $$ = build_array_ref ($2.location, $1, $3); }
2241| primary_no_new_array OSB_TK expression CSB_TK
2242 { $$ = build_array_ref ($2.location, $1, $3); }
2243| name OSB_TK error
2244 {
2245 yyerror ("Missing term and ']' expected");
2246 DRECOVER(array_access);
2247 }
2248| name OSB_TK expression error
2249 {
2250 yyerror ("']' expected");
2251 DRECOVER(array_access);
2252 }
2253| primary_no_new_array OSB_TK error
2254 {
2255 yyerror ("Missing term and ']' expected");
2256 DRECOVER(array_access);
2257 }
2258| primary_no_new_array OSB_TK expression error
2259 {
2260 yyerror ("']' expected");
2261 DRECOVER(array_access);
2262 }
2263;
2264
2265postfix_expression:
2266 primary
2267| name
2268| post_increment_expression
2269| post_decrement_expression
2270;
2271
2272post_increment_expression:
2273 postfix_expression INCR_TK
2274 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2275;
2276
2277post_decrement_expression:
2278 postfix_expression DECR_TK
2279 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2280;
2281
2282unary_expression:
2283 pre_increment_expression
2284| pre_decrement_expression
2285| PLUS_TK unary_expression
2286 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287| MINUS_TK unary_expression
2288 {$$ = build_unaryop ($1.token, $1.location, $2); }
2289| unary_expression_not_plus_minus
2290| PLUS_TK error
2291 {yyerror ("Missing term"); RECOVER}
2292| MINUS_TK error
2293 {yyerror ("Missing term"); RECOVER}
2294;
2295
2296pre_increment_expression:
2297 INCR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2299| INCR_TK error
2300 {yyerror ("Missing term"); RECOVER}
2301;
2302
2303pre_decrement_expression:
2304 DECR_TK unary_expression
2305 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2306| DECR_TK error
2307 {yyerror ("Missing term"); RECOVER}
2308;
2309
2310unary_expression_not_plus_minus:
2311 postfix_expression
2312| NOT_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2314| NEG_TK unary_expression
2315 {$$ = build_unaryop ($1.token, $1.location, $2); }
2316| cast_expression
2317| NOT_TK error
2318 {yyerror ("Missing term"); RECOVER}
2319| NEG_TK error
2320 {yyerror ("Missing term"); RECOVER}
2321;
2322
2323cast_expression: /* Error handling here is potentially weak */
2324 OP_TK primitive_type dims CP_TK unary_expression
2325 {
2326 tree type = $2;
c7303e41
APB
2327 int osb = pop_current_osb (ctxp);
2328 while (osb--)
e04a16fb
AG
2329 type = build_java_array_type (type, -1);
2330 $$ = build_cast ($1.location, type, $5);
2331 }
2332| OP_TK primitive_type CP_TK unary_expression
2333 { $$ = build_cast ($1.location, $2, $4); }
2334| OP_TK expression CP_TK unary_expression_not_plus_minus
2335 { $$ = build_cast ($1.location, $2, $4); }
2336| OP_TK name dims CP_TK unary_expression_not_plus_minus
2337 {
49f48c71 2338 const char *ptr;
c7303e41
APB
2339 int osb = pop_current_osb (ctxp);
2340 while (osb--)
e04a16fb
AG
2341 obstack_1grow (&temporary_obstack, '[');
2342 obstack_grow0 (&temporary_obstack,
2343 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2344 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2345 ptr = obstack_finish (&temporary_obstack);
2346 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2347 $$ = build_cast ($1.location, $2, $5);
2348 }
2349| OP_TK primitive_type OSB_TK error
2350 {yyerror ("']' expected, invalid type expression");}
2351| OP_TK error
2352 {
29f8b718 2353 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2354 RECOVER;
2355 }
2356| OP_TK primitive_type dims CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358| OP_TK primitive_type CP_TK error
2359 {yyerror ("Missing term"); RECOVER;}
2360| OP_TK name dims CP_TK error
2361 {yyerror ("Missing term"); RECOVER;}
2362;
2363
2364multiplicative_expression:
2365 unary_expression
2366| multiplicative_expression MULT_TK unary_expression
2367 {
2368 $$ = build_binop (BINOP_LOOKUP ($2.token),
2369 $2.location, $1, $3);
2370 }
2371| multiplicative_expression DIV_TK unary_expression
2372 {
2373 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 $1, $3);
2375 }
2376| multiplicative_expression REM_TK unary_expression
2377 {
2378 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2379 $1, $3);
2380 }
2381| multiplicative_expression MULT_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383| multiplicative_expression DIV_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2385| multiplicative_expression REM_TK error
2386 {yyerror ("Missing term"); RECOVER;}
2387;
2388
2389additive_expression:
2390 multiplicative_expression
2391| additive_expression PLUS_TK multiplicative_expression
2392 {
2393 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 $1, $3);
2395 }
2396| additive_expression MINUS_TK multiplicative_expression
2397 {
2398 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 $1, $3);
2400 }
2401| additive_expression PLUS_TK error
2402 {yyerror ("Missing term"); RECOVER;}
2403| additive_expression MINUS_TK error
2404 {yyerror ("Missing term"); RECOVER;}
2405;
2406
2407shift_expression:
2408 additive_expression
2409| shift_expression LS_TK additive_expression
2410 {
2411 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 $1, $3);
2413 }
2414| shift_expression SRS_TK additive_expression
2415 {
2416 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 $1, $3);
2418 }
2419| shift_expression ZRS_TK additive_expression
2420 {
2421 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2422 $1, $3);
2423 }
2424| shift_expression LS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426| shift_expression SRS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428| shift_expression ZRS_TK error
2429 {yyerror ("Missing term"); RECOVER;}
2430;
2431
2432relational_expression:
2433 shift_expression
2434| relational_expression LT_TK shift_expression
2435 {
2436 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 $1, $3);
2438 }
2439| relational_expression GT_TK shift_expression
2440 {
2441 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 $1, $3);
2443 }
2444| relational_expression LTE_TK shift_expression
2445 {
2446 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 $1, $3);
2448 }
2449| relational_expression GTE_TK shift_expression
2450 {
2451 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 $1, $3);
2453 }
2454| relational_expression INSTANCEOF_TK reference_type
5e942c50 2455 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2456| relational_expression LT_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression GT_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460| relational_expression LTE_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2462| relational_expression GTE_TK error
2463 {yyerror ("Missing term"); RECOVER;}
2464| relational_expression INSTANCEOF_TK error
2465 {yyerror ("Invalid reference type"); RECOVER;}
2466;
2467
2468equality_expression:
2469 relational_expression
2470| equality_expression EQ_TK relational_expression
2471 {
2472 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 $1, $3);
2474 }
2475| equality_expression NEQ_TK relational_expression
2476 {
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2479 }
2480| equality_expression EQ_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482| equality_expression NEQ_TK error
2483 {yyerror ("Missing term"); RECOVER;}
2484;
2485
2486and_expression:
2487 equality_expression
2488| and_expression AND_TK equality_expression
2489 {
2490 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491 $1, $3);
2492 }
2493| and_expression AND_TK error
2494 {yyerror ("Missing term"); RECOVER;}
2495;
2496
2497exclusive_or_expression:
2498 and_expression
2499| exclusive_or_expression XOR_TK and_expression
2500 {
2501 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502 $1, $3);
2503 }
2504| exclusive_or_expression XOR_TK error
2505 {yyerror ("Missing term"); RECOVER;}
2506;
2507
2508inclusive_or_expression:
2509 exclusive_or_expression
2510| inclusive_or_expression OR_TK exclusive_or_expression
2511 {
2512 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2513 $1, $3);
2514 }
2515| inclusive_or_expression OR_TK error
2516 {yyerror ("Missing term"); RECOVER;}
2517;
2518
2519conditional_and_expression:
2520 inclusive_or_expression
2521| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2522 {
2523 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524 $1, $3);
2525 }
2526| conditional_and_expression BOOL_AND_TK error
2527 {yyerror ("Missing term"); RECOVER;}
2528;
2529
2530conditional_or_expression:
2531 conditional_and_expression
2532| conditional_or_expression BOOL_OR_TK conditional_and_expression
2533 {
2534 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 $1, $3);
2536 }
2537| conditional_or_expression BOOL_OR_TK error
2538 {yyerror ("Missing term"); RECOVER;}
2539;
2540
2541conditional_expression: /* Error handling here is weak */
2542 conditional_or_expression
2543| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2544 {
2545 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2546 EXPR_WFL_LINECOL ($$) = $2.location;
2547 }
e04a16fb
AG
2548| conditional_or_expression REL_QM_TK REL_CL_TK error
2549 {
2550 YYERROR_NOW;
2551 yyerror ("Missing term");
2552 DRECOVER (1);
2553 }
2554| conditional_or_expression REL_QM_TK error
2555 {yyerror ("Missing term"); DRECOVER (2);}
2556| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2557 {yyerror ("Missing term"); DRECOVER (3);}
2558;
2559
2560assignment_expression:
2561 conditional_expression
2562| assignment
2563;
2564
2565assignment:
2566 left_hand_side assignment_operator assignment_expression
2567 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2568| left_hand_side assignment_operator error
2569 {
29f8b718 2570 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2571 DRECOVER (assign);
2572 }
2573;
2574
2575left_hand_side:
2576 name
2577| field_access
2578| array_access
2579;
2580
2581assignment_operator:
2582 ASSIGN_ANY_TK
2583| ASSIGN_TK
2584;
2585
2586expression:
2587 assignment_expression
2588;
2589
2590constant_expression:
2591 expression
2592;
2593
2594%%
c7303e41
APB
2595
2596/* Helper function to retrieve an OSB count. Should be used when the
2597 `dims:' rule is being used. */
2598
2599static int
2600pop_current_osb (ctxp)
2601 struct parser_ctxt *ctxp;
2602{
2603 int to_return;
2604
2605 if (ctxp->osb_depth < 0)
400500c4 2606 abort ();
c7303e41
APB
2607
2608 to_return = CURRENT_OSB (ctxp);
2609 ctxp->osb_depth--;
2610
2611 return to_return;
2612}
2613
e04a16fb
AG
2614\f
2615
c2952b01
APB
2616/* This section of the code deal with save/restoring parser contexts.
2617 Add mode documentation here. FIXME */
e04a16fb 2618
c2952b01
APB
2619/* Helper function. Create a new parser context. With
2620 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2621 context is copied, otherwise, the new context is zeroed. The newly
2622 created context becomes the current one. */
e04a16fb 2623
c2952b01
APB
2624static void
2625create_new_parser_context (copy_from_previous)
2626 int copy_from_previous;
e04a16fb 2627{
c2952b01 2628 struct parser_ctxt *new;
e04a16fb 2629
c2952b01
APB
2630 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2631 if (copy_from_previous)
2632 {
2633 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2634 new->saved_data_ctx = 1;
2635 }
2636 else
2e09e75a 2637 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
c2952b01 2638
e04a16fb
AG
2639 new->next = ctxp;
2640 ctxp = new;
c2952b01
APB
2641}
2642
2643/* Create a new parser context and make it the current one. */
2644
2645void
2646java_push_parser_context ()
2647{
2648 create_new_parser_context (0);
e04a16fb
AG
2649}
2650
c2952b01
APB
2651void
2652java_pop_parser_context (generate)
2653 int generate;
2654{
2655 tree current;
2656 struct parser_ctxt *toFree, *next;
2657
2658 if (!ctxp)
2659 return;
2660
2661 toFree = ctxp;
2662 next = ctxp->next;
2663 if (next)
2664 {
c2952b01 2665 lineno = ctxp->lineno;
19e223db 2666 current_class = ctxp->class_type;
c2952b01
APB
2667 }
2668
d19cbcb5
TT
2669 /* If the old and new lexers differ, then free the old one. */
2670 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2671 java_destroy_lexer (ctxp->lexer);
2672
c2952b01
APB
2673 /* Set the single import class file flag to 0 for the current list
2674 of imported things */
2675 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2676 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2677
2678 /* And restore those of the previous context */
2679 if ((ctxp = next)) /* Assignment is really meant here */
2680 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2681 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2682
2683 /* If we pushed a context to parse a class intended to be generated,
2684 we keep it so we can remember the class. What we could actually
2685 do is to just update a list of class names. */
2686 if (generate)
2687 {
2688 toFree->next = ctxp_for_generation;
2689 ctxp_for_generation = toFree;
2690 }
2691 else
2692 free (toFree);
2693}
2694
2695/* Create a parser context for the use of saving some global
2696 variables. */
2697
e04a16fb
AG
2698void
2699java_parser_context_save_global ()
2700{
22eed1e6
APB
2701 if (!ctxp)
2702 {
2703 java_push_parser_context ();
ee07f4f4
APB
2704 ctxp->saved_data_ctx = 1;
2705 }
c2952b01
APB
2706
2707 /* If this context already stores data, create a new one suitable
2708 for data storage. */
ee07f4f4 2709 else if (ctxp->saved_data)
c2952b01
APB
2710 create_new_parser_context (1);
2711
e04a16fb 2712 ctxp->lineno = lineno;
19e223db 2713 ctxp->class_type = current_class;
e04a16fb 2714 ctxp->filename = input_filename;
19e223db 2715 ctxp->function_decl = current_function_decl;
ee07f4f4 2716 ctxp->saved_data = 1;
e04a16fb
AG
2717}
2718
c2952b01
APB
2719/* Restore some global variables from the previous context. Make the
2720 previous context the current one. */
2721
e04a16fb
AG
2722void
2723java_parser_context_restore_global ()
2724{
e04a16fb 2725 lineno = ctxp->lineno;
19e223db 2726 current_class = ctxp->class_type;
e04a16fb 2727 input_filename = ctxp->filename;
dba41d30
APB
2728 if (wfl_operator)
2729 {
2730 tree s;
2731 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2732 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2733 }
19e223db 2734 current_function_decl = ctxp->function_decl;
c2952b01 2735 ctxp->saved_data = 0;
ee07f4f4
APB
2736 if (ctxp->saved_data_ctx)
2737 java_pop_parser_context (0);
e04a16fb
AG
2738}
2739
c2952b01
APB
2740/* Suspend vital data for the current class/function being parsed so
2741 that an other class can be parsed. Used to let local/anonymous
2742 classes be parsed. */
2743
2744static void
2745java_parser_context_suspend ()
e04a16fb 2746{
c2952b01 2747 /* This makes debugging through java_debug_context easier */
3b304f5b 2748 static const char *name = "<inner buffer context>";
e04a16fb 2749
c2952b01
APB
2750 /* Duplicate the previous context, use it to save the globals we're
2751 interested in */
2752 create_new_parser_context (1);
19e223db
MM
2753 ctxp->function_decl = current_function_decl;
2754 ctxp->class_type = current_class;
5e942c50 2755
c2952b01
APB
2756 /* Then create a new context which inherits all data from the
2757 previous one. This will be the new current context */
2758 create_new_parser_context (1);
2759
2760 /* Help debugging */
2761 ctxp->next->filename = name;
2762}
2763
2764/* Resume vital data for the current class/function being parsed so
2765 that an other class can be parsed. Used to let local/anonymous
2766 classes be parsed. The trick is the data storing file position
2767 informations must be restored to their current value, so parsing
2768 can resume as if no context was ever saved. */
2769
2770static void
2771java_parser_context_resume ()
2772{
2773 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2774 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2775 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2776
2777 /* We need to inherit the list of classes to complete/generate */
c2952b01
APB
2778 restored->classd_list = old->classd_list;
2779 restored->class_list = old->class_list;
2780
2781 /* Restore the current class and function from the saver */
19e223db
MM
2782 current_class = saver->class_type;
2783 current_function_decl = saver->function_decl;
c2952b01
APB
2784
2785 /* Retrive the restored context */
2786 ctxp = restored;
2787
2788 /* Re-installed the data for the parsing to carry on */
2789 bcopy (&old->marker_begining, &ctxp->marker_begining,
2790 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2791
2792 /* Buffer context can now be discarded */
2793 free (saver);
2794 free (old);
2795}
2796
2797/* Add a new anchor node to which all statement(s) initializing static
2798 and non static initialized upon declaration field(s) will be
2799 linked. */
2800
2801static void
2802java_parser_context_push_initialized_field ()
2803{
2804 tree node;
2805
2806 node = build_tree_list (NULL_TREE, NULL_TREE);
2807 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2808 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2809
2810 node = build_tree_list (NULL_TREE, NULL_TREE);
2811 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2812 CPC_INITIALIZER_LIST (ctxp) = node;
2813
2814 node = build_tree_list (NULL_TREE, NULL_TREE);
2815 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2816 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2817}
2818
2819/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2820 remember them so we can use it to create and populate the finit$
c2952b01
APB
2821 or <clinit> functions. */
2822
2823static void
2824java_parser_context_pop_initialized_field ()
2825{
2826 tree stmts;
2827 tree class_type = TREE_TYPE (GET_CPC ());
2828
2829 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2830 {
c2952b01
APB
2831 stmts = CPC_INITIALIZER_STMT (ctxp);
2832 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2833 if (stmts && !java_error_count)
2834 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2835 }
2836
c2952b01
APB
2837 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2838 {
2839 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2840 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2841 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2842 /* Keep initialization in order to enforce 8.5 */
2843 if (stmts && !java_error_count)
2844 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2845 }
e04a16fb 2846
c2952b01
APB
2847 /* JDK 1.1 instance initializers */
2848 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2849 {
c2952b01
APB
2850 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2851 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2852 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2853 if (stmts && !java_error_count)
2854 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2855 }
c2952b01
APB
2856}
2857
2858static tree
2859reorder_static_initialized (list)
2860 tree list;
2861{
2862 /* We have to keep things in order. The alias initializer have to
2863 come first, then the initialized regular field, in reverse to
2864 keep them in lexical order. */
2865 tree marker, previous = NULL_TREE;
2866 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2867 if (TREE_CODE (marker) == TREE_LIST
2868 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2869 break;
2870
2871 /* No static initialized, the list is fine as is */
2872 if (!previous)
2873 list = TREE_CHAIN (marker);
2874
2875 /* No marker? reverse the whole list */
2876 else if (!marker)
2877 list = nreverse (list);
2878
2879 /* Otherwise, reverse what's after the marker and the new reordered
2880 sublist will replace the marker. */
b351b287 2881 else
c2952b01
APB
2882 {
2883 TREE_CHAIN (previous) = NULL_TREE;
2884 list = nreverse (list);
2885 list = chainon (TREE_CHAIN (marker), list);
2886 }
2887 return list;
e04a16fb
AG
2888}
2889
c2952b01
APB
2890/* Helper functions to dump the parser context stack. */
2891
2892#define TAB_CONTEXT(C) \
2893 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2894
2895static void
2896java_debug_context_do (tab)
2897 int tab;
2898{
ee07f4f4
APB
2899 struct parser_ctxt *copy = ctxp;
2900 while (copy)
2901 {
c2952b01 2902 TAB_CONTEXT (tab);
ee07f4f4 2903 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2904 TAB_CONTEXT (tab);
ee07f4f4 2905 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2906 TAB_CONTEXT (tab);
2907 fprintf (stderr, "lineno: %d\n", copy->lineno);
2908 TAB_CONTEXT (tab);
ee07f4f4
APB
2909 fprintf (stderr, "package: %s\n",
2910 (copy->package ?
2911 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2912 TAB_CONTEXT (tab);
ee07f4f4 2913 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2914 TAB_CONTEXT (tab);
ee07f4f4
APB
2915 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2916 copy = copy->next;
2917 tab += 2;
2918 }
ee07f4f4
APB
2919}
2920
c2952b01
APB
2921/* Dump the stacked up parser contexts. Intended to be called from a
2922 debugger. */
2923
ee07f4f4
APB
2924void
2925java_debug_context ()
2926{
2927 java_debug_context_do (0);
2928}
2929
c2952b01
APB
2930\f
2931
2932/* Flag for the error report routine to issue the error the first time
2933 it's called (overriding the default behavior which is to drop the
2934 first invocation and honor the second one, taking advantage of a
2935 richer context. */
2936static int force_error = 0;
ee07f4f4 2937
8119c720
APB
2938/* Reporting an constructor invocation error. */
2939static void
2940parse_ctor_invocation_error ()
2941{
2942 if (DECL_CONSTRUCTOR_P (current_function_decl))
2943 yyerror ("Constructor invocation must be first thing in a constructor");
2944 else
2945 yyerror ("Only constructors can invoke constructors");
2946}
2947
2948/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2949
2950static tree
2951parse_jdk1_1_error (msg)
49f48c71 2952 const char *msg;
b67d701b
PB
2953{
2954 sorry (": `%s' JDK1.1(TM) feature", msg);
2955 java_error_count++;
9bbc7d9f 2956 return empty_stmt_node;
b67d701b
PB
2957}
2958
e04a16fb
AG
2959static int do_warning = 0;
2960
2961void
2962yyerror (msg)
49f48c71 2963 const char *msg;
e04a16fb
AG
2964{
2965 static java_lc elc;
2966 static int prev_lineno;
49f48c71 2967 static const char *prev_msg;
e04a16fb 2968
0a2138e2 2969 int save_lineno;
e04a16fb 2970 char *remainder, *code_from_source;
e04a16fb
AG
2971
2972 if (!force_error && prev_lineno == lineno)
2973 return;
2974
2975 /* Save current error location but report latter, when the context is
2976 richer. */
2977 if (ctxp->java_error_flag == 0)
2978 {
2979 ctxp->java_error_flag = 1;
2980 elc = ctxp->elc;
2981 /* Do something to use the previous line if we're reaching the
2982 end of the file... */
2983#ifdef VERBOSE_SKELETON
2984 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2985#endif
2986 return;
2987 }
2988
2989 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2990 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2991 return;
2992
2993 ctxp->java_error_flag = 0;
2994 if (do_warning)
2995 java_warning_count++;
2996 else
2997 java_error_count++;
2998
807bc1db 2999 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
3000 {
3001 elc.col = ctxp->p_line->char_col-1;
3002 elc.line = ctxp->p_line->lineno;
3003 }
3004
3005 save_lineno = lineno;
3006 prev_lineno = lineno = elc.line;
3007 prev_msg = msg;
3008
3009 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3010 obstack_grow0 (&temporary_obstack,
3011 code_from_source, strlen (code_from_source));
3012 remainder = obstack_finish (&temporary_obstack);
3013 if (do_warning)
3014 warning ("%s.\n%s", msg, remainder);
3015 else
3016 error ("%s.\n%s", msg, remainder);
3017
3018 /* This allow us to cheaply avoid an extra 'Invalid expression
3019 statement' error report when errors have been already reported on
3020 the same line. This occurs when we report an error but don't have
3021 a synchronization point other than ';', which
3022 expression_statement is the only one to take care of. */
3023 ctxp->prevent_ese = lineno = save_lineno;
3024}
3025
3026static void
15fdcfe9 3027issue_warning_error_from_context (cl, msg, ap)
5e942c50 3028 tree cl;
d4476be2 3029 const char *msg;
15fdcfe9 3030 va_list ap;
5e942c50 3031{
3b304f5b 3032 const char *saved, *saved_input_filename;
15fdcfe9
PB
3033 char buffer [4096];
3034 vsprintf (buffer, msg, ap);
3035 force_error = 1;
5e942c50
APB
3036
3037 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3038 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3039 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3040
3041 /* We have a CL, that's a good reason for using it if it contains data */
3042 saved = ctxp->filename;
3043 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3044 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3045 saved_input_filename = input_filename;
3046 input_filename = ctxp->filename;
15fdcfe9
PB
3047 java_error (NULL);
3048 java_error (buffer);
5e942c50 3049 ctxp->filename = saved;
1886c9d8 3050 input_filename = saved_input_filename;
15fdcfe9 3051 force_error = 0;
5e942c50
APB
3052}
3053
e04a16fb
AG
3054/* Issue an error message at a current source line CL */
3055
15fdcfe9 3056void
df32d2ce 3057parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3058{
d4476be2 3059#ifndef ANSI_PROTOTYPES
e04a16fb 3060 tree cl;
d4476be2 3061 const char *msg;
e04a16fb 3062#endif
e04a16fb
AG
3063 va_list ap;
3064
3065 VA_START (ap, msg);
d4476be2 3066#ifndef ANSI_PROTOTYPES
e04a16fb 3067 cl = va_arg (ap, tree);
d4476be2 3068 msg = va_arg (ap, const char *);
e04a16fb 3069#endif
15fdcfe9
PB
3070 issue_warning_error_from_context (cl, msg, ap);
3071 va_end (ap);
e04a16fb
AG
3072}
3073
3074/* Issue a warning at a current source line CL */
3075
3076static void
df32d2ce 3077parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3078{
d4476be2 3079#ifndef ANSI_PROTOTYPES
e04a16fb 3080 tree cl;
d4476be2 3081 const char *msg;
e04a16fb 3082#endif
e04a16fb
AG
3083 va_list ap;
3084
3085 VA_START (ap, msg);
d4476be2 3086#ifndef ANSI_PROTOTYPES
e04a16fb 3087 cl = va_arg (ap, tree);
d4476be2 3088 msg = va_arg (ap, const char *);
e04a16fb 3089#endif
e04a16fb 3090
c877974e 3091 force_error = do_warning = 1;
15fdcfe9 3092 issue_warning_error_from_context (cl, msg, ap);
c877974e 3093 do_warning = force_error = 0;
15fdcfe9 3094 va_end (ap);
e04a16fb
AG
3095}
3096
82371d41
APB
3097static tree
3098find_expr_with_wfl (node)
3099 tree node;
3100{
3101 while (node)
3102 {
3103 char code;
3104 tree to_return;
3105
3106 switch (TREE_CODE (node))
3107 {
3108 case BLOCK:
c0d87ff6
PB
3109 node = BLOCK_EXPR_BODY (node);
3110 continue;
82371d41
APB
3111
3112 case COMPOUND_EXPR:
3113 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3114 if (to_return)
3115 return to_return;
c0d87ff6
PB
3116 node = TREE_OPERAND (node, 1);
3117 continue;
82371d41
APB
3118
3119 case LOOP_EXPR:
c0d87ff6
PB
3120 node = TREE_OPERAND (node, 0);
3121 continue;
82371d41
APB
3122
3123 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3124 node = TREE_OPERAND (node, 1);
3125 continue;
3126
82371d41
APB
3127 default:
3128 code = TREE_CODE_CLASS (TREE_CODE (node));
3129 if (((code == '1') || (code == '2') || (code == 'e'))
3130 && EXPR_WFL_LINECOL (node))
3131 return node;
ba179f9f 3132 return NULL_TREE;
82371d41
APB
3133 }
3134 }
3135 return NULL_TREE;
3136}
3137
3138/* Issue a missing return statement error. Uses METHOD to figure the
3139 last line of the method the error occurs in. */
3140
3141static void
3142missing_return_error (method)
3143 tree method;
3144{
3145 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3146 parse_error_context (wfl_operator, "Missing return statement");
3147}
3148
3149/* Issue an unreachable statement error. From NODE, find the next
3150 statement to report appropriately. */
3151static void
3152unreachable_stmt_error (node)
3153 tree node;
3154{
3155 /* Browse node to find the next expression node that has a WFL. Use
3156 the location to report the error */
3157 if (TREE_CODE (node) == COMPOUND_EXPR)
3158 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3159 else
3160 node = find_expr_with_wfl (node);
3161
3162 if (node)
3163 {
3164 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3165 parse_error_context (wfl_operator, "Unreachable statement");
3166 }
3167 else
400500c4 3168 abort ();
82371d41
APB
3169}
3170
c877974e 3171int
e04a16fb
AG
3172java_report_errors ()
3173{
3174 if (java_error_count)
3175 fprintf (stderr, "%d error%s",
3176 java_error_count, (java_error_count == 1 ? "" : "s"));
3177 if (java_warning_count)
3178 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3179 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3180 if (java_error_count || java_warning_count)
3181 putc ('\n', stderr);
c877974e 3182 return java_error_count;
e04a16fb
AG
3183}
3184
3185static char *
3186java_accstring_lookup (flags)
3187 int flags;
3188{
3189 static char buffer [80];
3190#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3191
3192 /* Access modifier looked-up first for easier report on forbidden
3193 access. */
3194 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3195 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3196 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3197 if (flags & ACC_STATIC) COPY_RETURN ("static");
3198 if (flags & ACC_FINAL) COPY_RETURN ("final");
3199 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3200 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3201 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3202 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3203 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3204 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3205
3206 buffer [0] = '\0';
3207 return buffer;
3208#undef COPY_RETURN
3209}
3210
b67d701b
PB
3211/* Issuing error messages upon redefinition of classes, interfaces or
3212 variables. */
3213
e04a16fb 3214static void
b67d701b 3215classitf_redefinition_error (context, id, decl, cl)
49f48c71 3216 const char *context;
e04a16fb
AG
3217 tree id, decl, cl;
3218{
3219 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3220 context, IDENTIFIER_POINTER (id),
3221 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3222 /* Here we should point out where its redefined. It's a unicode. FIXME */
3223}
3224
b67d701b
PB
3225static void
3226variable_redefinition_error (context, name, type, line)
3227 tree context, name, type;
3228 int line;
3229{
49f48c71 3230 const char *type_name;
b67d701b
PB
3231
3232 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3233 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3234 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3235 else
0a2138e2 3236 type_name = lang_printable_name (type, 0);
b67d701b
PB
3237
3238 parse_error_context (context,
781b0558 3239 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3240 IDENTIFIER_POINTER (name),
3241 type_name, IDENTIFIER_POINTER (name), line);
3242}
3243
c583dd46
APB
3244static tree
3245build_array_from_name (type, type_wfl, name, ret_name)
3246 tree type, type_wfl, name, *ret_name;
3247{
3248 int more_dims = 0;
49f48c71 3249 const char *string;
c583dd46
APB
3250
3251 /* Eventually get more dims */
3252 string = IDENTIFIER_POINTER (name);
3253 while (string [more_dims] == '[')
3254 more_dims++;
3255
3256 /* If we have, then craft a new type for this variable */
3257 if (more_dims)
3258 {
c0d87ff6 3259 name = get_identifier (&string [more_dims]);
c583dd46 3260
34f4db93
APB
3261 /* If we have a pointer, use its type */
3262 if (TREE_CODE (type) == POINTER_TYPE)
3263 type = TREE_TYPE (type);
c583dd46
APB
3264
3265 /* Building the first dimension of a primitive type uses this
3266 function */
3267 if (JPRIMITIVE_TYPE_P (type))
3268 {
3269 type = build_java_array_type (type, -1);
22eed1e6 3270 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3271 more_dims--;
3272 }
3273 /* Otherwise, if we have a WFL for this type, use it (the type
3274 is already an array on an unresolved type, and we just keep
3275 on adding dimensions) */
3276 else if (type_wfl)
3277 type = type_wfl;
3278
3279 /* Add all the dimensions */
3280 while (more_dims--)
3281 type = build_unresolved_array_type (type);
3282
3283 /* The type may have been incomplete in the first place */
3284 if (type_wfl)
3285 type = obtain_incomplete_type (type);
3286 }
3287
c2952b01
APB
3288 if (ret_name)
3289 *ret_name = name;
c583dd46
APB
3290 return type;
3291}
3292
e04a16fb
AG
3293/* Build something that the type identifier resolver will identify as
3294 being an array to an unresolved type. TYPE_WFL is a WFL on a
3295 identifier. */
3296
3297static tree
3298build_unresolved_array_type (type_or_wfl)
3299 tree type_or_wfl;
3300{
49f48c71 3301 const char *ptr;
e04a16fb 3302
1886c9d8 3303 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3304 just create a array type */
3305 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3306 {
3307 tree type = build_java_array_type (type_or_wfl, -1);
3308 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3309 return type;
3310 }
3311
3312 obstack_1grow (&temporary_obstack, '[');
3313 obstack_grow0 (&temporary_obstack,
3314 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3315 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3316 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3317 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3318 return type_or_wfl;
e04a16fb
AG
3319}
3320
e04a16fb
AG
3321static void
3322parser_add_interface (class_decl, interface_decl, wfl)
3323 tree class_decl, interface_decl, wfl;
3324{
3325 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3326 parse_error_context (wfl, "Interface `%s' repeated",
3327 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3328}
3329
3330/* Bulk of common class/interface checks. Return 1 if an error was
3331 encountered. TAG is 0 for a class, 1 for an interface. */
3332
3333static int
3334check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3335 int is_interface, flags;
3336 tree raw_name, qualified_name, decl, cl;
3337{
3338 tree node;
c2952b01
APB
3339 int sca = 0; /* Static class allowed */
3340 int icaf = 0; /* Inner class allowed flags */
3341 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3342
3343 if (!quiet_flag)
c2952b01
APB
3344 fprintf (stderr, " %s%s %s",
3345 (CPC_INNER_P () ? "inner" : ""),
3346 (is_interface ? "interface" : "class"),
e04a16fb
AG
3347 IDENTIFIER_POINTER (qualified_name));
3348
3349 /* Scope of an interface/class type name:
3350 - Can't be imported by a single type import
3351 - Can't already exists in the package */
3352 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3353 && (node = find_name_in_single_imports (raw_name))
3354 && !CPC_INNER_P ())
e04a16fb
AG
3355 {
3356 parse_error_context
3357 (cl, "%s name `%s' clashes with imported type `%s'",
3358 (is_interface ? "Interface" : "Class"),
3359 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3360 return 1;
3361 }
3362 if (decl && CLASS_COMPLETE_P (decl))
3363 {
b67d701b
PB
3364 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3365 qualified_name, decl, cl);
e04a16fb
AG
3366 return 1;
3367 }
3368
c2952b01
APB
3369 if (check_inner_class_redefinition (raw_name, cl))
3370 return 1;
3371
3372 /* If public, file name should match class/interface name, except
3373 when dealing with an inner class */
3374 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3375 {
49f48c71 3376 const char *f;
e04a16fb
AG
3377
3378 /* Contains OS dependent assumption on path separator. FIXME */
3379 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3380 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3381 f--)
3382 ;
847fe791 3383 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3384 f++;
3385 if (strncmp (IDENTIFIER_POINTER (raw_name),
3386 f , IDENTIFIER_LENGTH (raw_name)) ||
3387 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3388 parse_error_context
3389 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3390 (is_interface ? "interface" : "class"),
3391 IDENTIFIER_POINTER (qualified_name),
3392 IDENTIFIER_POINTER (raw_name));
3393 }
3394
c2952b01
APB
3395 /* Static classes can be declared only in top level classes. Note:
3396 once static, a inner class is a top level class. */
3397 if (flags & ACC_STATIC)
3398 {
3399 /* Catch the specific error of declaring an class inner class
3400 with no toplevel enclosing class. Prevent check_modifiers from
3401 complaining a second time */
3402 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3403 {
3404 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3405 IDENTIFIER_POINTER (qualified_name));
3406 sca = ACC_STATIC;
3407 }
3408 /* Else, in the context of a top-level class declaration, let
3409 `check_modifiers' do its job, otherwise, give it a go */
3410 else
3411 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3412 }
3413
a40d21da 3414 /* Inner classes can be declared private or protected
c2952b01
APB
3415 within their enclosing classes. */
3416 if (CPC_INNER_P ())
3417 {
3418 /* A class which is local to a block can't be public, private,
3419 protected or static. But it is created final, so allow this
3420 one. */
3421 if (current_function_decl)
3422 icaf = sca = uaaf = ACC_FINAL;
3423 else
3424 {
3425 check_modifiers_consistency (flags);
3426 icaf = ACC_PRIVATE|ACC_PROTECTED;
3427 }
3428 }
3429
a40d21da
APB
3430 if (is_interface)
3431 {
3432 if (CPC_INNER_P ())
3433 uaaf = INTERFACE_INNER_MODIFIERS;
3434 else
3435 uaaf = INTERFACE_MODIFIERS;
3436
3437 check_modifiers ("Illegal modifier `%s' for interface declaration",
3438 flags, uaaf);
3439 }
2884c41e 3440 else
a40d21da
APB
3441 check_modifiers ((current_function_decl ?
3442 "Illegal modifier `%s' for local class declaration" :
3443 "Illegal modifier `%s' for class declaration"),
c2952b01 3444 flags, uaaf|sca|icaf);
e04a16fb
AG
3445 return 0;
3446}
3447
c2952b01
APB
3448static void
3449make_nested_class_name (cpc_list)
3450 tree cpc_list;
3451{
3452 tree name;
3453
3454 if (!cpc_list)
3455 return;
3456 else
3457 make_nested_class_name (TREE_CHAIN (cpc_list));
3458
3459 /* Pick the qualified name when dealing with the first upmost
3460 enclosing class */
3461 name = (TREE_CHAIN (cpc_list) ?
3462 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3463 obstack_grow (&temporary_obstack,
3464 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3465 /* Why is NO_DOLLAR_IN_LABEL defined? */
3466#if 0
3467#ifdef NO_DOLLAR_IN_LABEL
400500c4 3468 internal_error ("Can't use '$' as a separator for inner classes");
c2952b01
APB
3469#endif
3470#endif
3471 obstack_1grow (&temporary_obstack, '$');
3472}
3473
3474/* Can't redefine a class already defined in an earlier scope. */
3475
3476static int
3477check_inner_class_redefinition (raw_name, cl)
3478 tree raw_name, cl;
3479{
3480 tree scope_list;
3481
3482 for (scope_list = GET_CPC_LIST (); scope_list;
3483 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3484 if (raw_name == GET_CPC_UN_NODE (scope_list))
3485 {
3486 parse_error_context
3487 (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",
3488 IDENTIFIER_POINTER (raw_name));
3489 return 1;
3490 }
3491 return 0;
3492}
3493
3494static tree
3495find_as_inner_class (enclosing, name, cl)
3496 tree enclosing, name, cl;
3497{
3498 tree qual, to_return;
3499 if (!enclosing)
3500 return NULL_TREE;
3501
3502 name = TYPE_NAME (name);
3503
3504 /* First search: within the scope of `enclosing', search for name */
3505 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3506 qual = EXPR_WFL_QUALIFICATION (cl);
3507 else if (cl)
3508 qual = build_tree_list (cl, NULL_TREE);
3509 else
3510 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3511
3512 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3513 return to_return;
3514
3515 /* We're dealing with a qualified name. Try to resolve thing until
3516 we get something that is an enclosing class. */
3517 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3518 {
3519 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3520
0c2b8145
APB
3521 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3522 qual = TREE_CHAIN (qual))
c2952b01
APB
3523 {
3524 acc = merge_qualified_name (acc,
3525 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3526 BUILD_PTR_FROM_NAME (ptr, acc);
3527 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3528 }
3529
3530 /* A NULL qual and a decl means that the search ended
3531 successfully?!? We have to do something then. FIXME */
3532
3533 if (decl)
3534 enclosing = decl;
3535 else
3536 qual = EXPR_WFL_QUALIFICATION (cl);
3537 }
3538 /* Otherwise, create a qual for the other part of the resolution. */
3539 else
3540 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3541
1e12ab9b 3542 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3543}
3544
3545/* We go inside the list of sub classes and try to find a way
3546 through. */
3547
3548static tree
3549find_as_inner_class_do (qual, enclosing)
3550 tree qual, enclosing;
3551{
3552 if (!qual)
3553 return NULL_TREE;
3554
3555 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3556 {
3557 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3558 tree next_enclosing = NULL_TREE;
3559 tree inner_list;
3560
3561 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3562 inner_list; inner_list = TREE_CHAIN (inner_list))
3563 {
3564 if (TREE_VALUE (inner_list) == name_to_match)
3565 {
3566 next_enclosing = TREE_PURPOSE (inner_list);
3567 break;
3568 }
3569 }
3570 enclosing = next_enclosing;
3571 }
3572
3573 return (!qual && enclosing ? enclosing : NULL_TREE);
3574}
3575
3576/* Reach all inner classes and tie their unqualified name to a
3577 DECL. */
3578
3579static void
3580set_nested_class_simple_name_value (outer, set)
3581 tree outer;
3582 int set;
3583{
3584 tree l;
3585
3586 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3587 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3588 TREE_PURPOSE (l) : NULL_TREE);
3589}
3590
3591static void
3592link_nested_class_to_enclosing ()
3593{
3594 if (GET_ENCLOSING_CPC ())
3595 {
3596 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3597 DECL_INNER_CLASS_LIST (enclosing) =
3598 tree_cons (GET_CPC (), GET_CPC_UN (),
3599 DECL_INNER_CLASS_LIST (enclosing));
3600 enclosing = enclosing;
3601 }
3602}
3603
3604static tree
3605maybe_make_nested_class_name (name)
3606 tree name;
3607{
3608 tree id = NULL_TREE;
3609
3610 if (CPC_INNER_P ())
3611 {
3612 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3613 obstack_grow0 (&temporary_obstack,
3614 IDENTIFIER_POINTER (name),
3615 IDENTIFIER_LENGTH (name));
c2952b01
APB
3616 id = get_identifier (obstack_finish (&temporary_obstack));
3617 if (ctxp->package)
3618 QUALIFIED_P (id) = 1;
3619 }
3620 return id;
3621}
3622
3623/* If DECL is NULL, create and push a new DECL, record the current
3624 line CL and do other maintenance things. */
3625
e04a16fb 3626static tree
c2952b01
APB
3627maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3628 tree decl, raw_name, qualified_name, cl;
e04a16fb 3629{
5e942c50 3630 if (!decl)
e04a16fb 3631 decl = push_class (make_class (), qualified_name);
c2952b01 3632
e04a16fb
AG
3633 /* Take care of the file and line business */
3634 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3635 /* If we're emiting xrefs, store the line/col number information */
3636 if (flag_emit_xref)
3637 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3638 else
3639 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3640 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3641 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3642 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3643
c2952b01
APB
3644 PUSH_CPC (decl, raw_name);
3645 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3646
e04a16fb
AG
3647 /* Link the declaration to the already seen ones */
3648 TREE_CHAIN (decl) = ctxp->class_list;
3649 ctxp->class_list = decl;
5e942c50 3650
23a79c61 3651 /* Create a new nodes in the global lists */
fea2d5da 3652 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
23a79c61 3653 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3654
e04a16fb
AG
3655 /* Install a new dependency list element */
3656 create_jdep_list (ctxp);
3657
3658 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3659 IDENTIFIER_POINTER (qualified_name)));
3660 return decl;
3661}
3662
3663static void
3664add_superinterfaces (decl, interface_list)
3665 tree decl, interface_list;
3666{
3667 tree node;
3668 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3669 takes care of ensuring that:
3670 - This is an accessible interface type,
3671 - Circularity detection.
3672 parser_add_interface is then called. If present but not defined,
3673 the check operation is delayed until the super interface gets
3674 defined. */
3675 for (node = interface_list; node; node = TREE_CHAIN (node))
3676 {
15fdcfe9 3677 tree current = TREE_PURPOSE (node);
5e942c50
APB
3678 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3679 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3680 {
5e942c50
APB
3681 if (!parser_check_super_interface (idecl, decl, current))
3682 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3683 }
3684 else
3685 register_incomplete_type (JDEP_INTERFACE,
3686 current, decl, NULL_TREE);
3687 }
3688}
3689
3690/* Create an interface in pass1 and return its decl. Return the
3691 interface's decl in pass 2. */
3692
3693static tree
3694create_interface (flags, id, super)
3695 int flags;
3696 tree id, super;
3697{
e04a16fb 3698 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3699 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3700 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3701
3702 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3703
3704 /* Basic checks: scope, redefinition, modifiers */
3705 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3706 {
3707 PUSH_ERROR ();
3708 return NULL_TREE;
3709 }
3710
3711 /* Suspend the current parsing context if we're parsing an inner
3712 interface */
3713 if (CPC_INNER_P ())
3714 java_parser_context_suspend ();
3715
3716 /* Push a new context for (static) initialized upon declaration fields */
3717 java_parser_context_push_initialized_field ();
e04a16fb
AG
3718
3719 /* Interface modifiers check
3720 - public/abstract allowed (already done at that point)
3721 - abstract is obsolete (comes first, it's a warning, or should be)
3722 - Can't use twice the same (checked in the modifier rule) */
c877974e 3723 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3724 parse_warning_context
3725 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3726 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3727
3728 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3729 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3730
3731 /* Set super info and mark the class a complete */
2aa11e97 3732 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3733 object_type_node, ctxp->interface_number);
3734 ctxp->interface_number = 0;
3735 CLASS_COMPLETE_P (decl) = 1;
3736 add_superinterfaces (decl, super);
3737
3738 return decl;
3739}
3740
c2952b01
APB
3741/* Anonymous class counter. Will be reset to 1 every time a non
3742 anonymous class gets created. */
3743static int anonymous_class_counter = 1;
3744
3745/* Patch anonymous class CLASS, by either extending or implementing
3746 DEP. */
3747
3748static void
3749patch_anonymous_class (type_decl, class_decl, wfl)
3750 tree type_decl, class_decl, wfl;
3751{
3752 tree class = TREE_TYPE (class_decl);
3753 tree type = TREE_TYPE (type_decl);
3754 tree binfo = TYPE_BINFO (class);
3755
3756 /* If it's an interface, implement it */
3757 if (CLASS_INTERFACE (type_decl))
3758 {
3759 tree s_binfo;
3760 int length;
3761
3762 if (parser_check_super_interface (type_decl, class_decl, wfl))
3763 return;
3764
3765 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3766 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3767 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3768 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3769 /* And add the interface */
3770 parser_add_interface (class_decl, type_decl, wfl);
3771 }
3772 /* Otherwise, it's a type we want to extend */
3773 else
3774 {
3775 if (parser_check_super (type_decl, class_decl, wfl))
3776 return;
3777 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3778 }
3779}
3780
3781static tree
3782create_anonymous_class (location, type_name)
3783 int location;
3784 tree type_name;
3785{
3786 char buffer [80];
3787 tree super = NULL_TREE, itf = NULL_TREE;
3788 tree id, type_decl, class;
3789
3790 /* The unqualified name of the anonymous class. It's just a number. */
3791 sprintf (buffer, "%d", anonymous_class_counter++);
3792 id = build_wfl_node (get_identifier (buffer));
3793 EXPR_WFL_LINECOL (id) = location;
3794
3795 /* We know about the type to extend/implement. We go ahead */
3796 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3797 {
3798 /* Create a class which either implements on extends the designated
3799 class. The class bears an innacessible name. */
3800 if (CLASS_INTERFACE (type_decl))
3801 {
3802 /* It's OK to modify it here. It's been already used and
3803 shouldn't be reused */
3804 ctxp->interface_number = 1;
3805 /* Interfaces should presented as a list of WFLs */
3806 itf = build_tree_list (type_name, NULL_TREE);
3807 }
3808 else
3809 super = type_name;
3810 }
3811
3812 class = create_class (ACC_FINAL, id, super, itf);
3813
3814 /* We didn't know anything about the stuff. We register a dependence. */
3815 if (!type_decl)
3816 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3817
3818 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3819 return class;
3820}
3821
a40d21da 3822/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3823 interface's decl in pass 2. */
3824
3825static tree
3826create_class (flags, id, super, interfaces)
3827 int flags;
3828 tree id, super, interfaces;
3829{
e04a16fb
AG
3830 tree raw_name = EXPR_WFL_NODE (id);
3831 tree class_id, decl;
9ee9b555 3832 tree super_decl_type;
e04a16fb 3833
98a52c2c 3834 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3835 decl = IDENTIFIER_CLASS_VALUE (class_id);
3836 EXPR_WFL_NODE (id) = class_id;
3837
3838 /* Basic check: scope, redefinition, modifiers */
3839 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3840 {
3841 PUSH_ERROR ();
3842 return NULL_TREE;
3843 }
3844
3845 /* Suspend the current parsing context if we're parsing an inner
3846 class or an anonymous class. */
3847 if (CPC_INNER_P ())
3848 java_parser_context_suspend ();
3849 /* Push a new context for (static) initialized upon declaration fields */
3850 java_parser_context_push_initialized_field ();
e04a16fb
AG
3851
3852 /* Class modifier check:
3853 - Allowed modifier (already done at that point)
3854 - abstract AND final forbidden
3855 - Public classes defined in the correct file */
3856 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3857 parse_error_context
3858 (id, "Class `%s' can't be declared both abstract and final",
3859 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3860
3861 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3862 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3863
3864 /* If SUPER exists, use it, otherwise use Object */
3865 if (super)
3866 {
3867 /* Can't extend java.lang.Object */
3868 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3869 {
3870 parse_error_context (id, "Can't extend `java.lang.Object'");
3871 return NULL_TREE;
3872 }
3873
2c3199bc
PB
3874 super_decl_type =
3875 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3876 }
3877 else if (TREE_TYPE (decl) != object_type_node)
3878 super_decl_type = object_type_node;
3879 /* We're defining java.lang.Object */
3880 else
3881 super_decl_type = NULL_TREE;
3882
6d003d5c
BM
3883 /* A class nested in an interface is implicitly static. */
3884 if (INNER_CLASS_DECL_P (decl)
3885 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3886 {
3887 flags |= ACC_STATIC;
3888 }
3889
3890 /* Set super info and mark the class as complete. */
e04a16fb
AG
3891 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3892 ctxp->interface_number);
3893 ctxp->interface_number = 0;
3894 CLASS_COMPLETE_P (decl) = 1;
3895 add_superinterfaces (decl, interfaces);
3896
c2952b01
APB
3897 /* Add the private this$<n> field, Replicate final locals still in
3898 scope as private final fields mangled like val$<local_name>.
3899 This doesn't not occur for top level (static) inner classes. */
3900 if (PURE_INNER_CLASS_DECL_P (decl))
3901 add_inner_class_fields (decl, current_function_decl);
3902
7f10c2e2
APB
3903 /* If doing xref, store the location at which the inherited class
3904 (if any) was seen. */
3905 if (flag_emit_xref && super)
3906 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3907
5e942c50
APB
3908 /* Eventually sets the @deprecated tag flag */
3909 CHECK_DEPRECATED (decl);
3910
165f37bc
APB
3911 /* Reset the anonymous class counter when declaring non inner classes */
3912 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3913 anonymous_class_counter = 1;
3914
e04a16fb
AG
3915 return decl;
3916}
3917
c2952b01 3918/* End a class declaration: register the statements used to create
c00f0fb2 3919 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3920 parser context if necessary. */
3921
3922static void
3923end_class_declaration (resume)
3924 int resume;
3925{
3926 /* If an error occured, context weren't pushed and won't need to be
3927 popped by a resume. */
3928 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3929
3930 java_parser_context_pop_initialized_field ();
3931 POP_CPC ();
3932 if (resume && no_error_occured)
3933 java_parser_context_resume ();
93220702
APB
3934
3935 /* We're ending a class declaration, this is a good time to reset
3936 the interface cout. Note that might have been already done in
3937 create_interface, but if at that time an inner class was being
3938 dealt with, the interface count was reset in a context created
3939 for the sake of handling inner classes declaration. */
3940 ctxp->interface_number = 0;
c2952b01
APB
3941}
3942
3943static void
3944add_inner_class_fields (class_decl, fct_decl)
3945 tree class_decl;
3946 tree fct_decl;
3947{
3948 tree block, marker, f;
3949
3950 f = add_field (TREE_TYPE (class_decl),
3951 build_current_thisn (TREE_TYPE (class_decl)),
3952 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3953 ACC_PRIVATE);
3954 FIELD_THISN (f) = 1;
3955
3956 if (!fct_decl)
3957 return;
3958
3959 for (block = GET_CURRENT_BLOCK (fct_decl);
3960 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3961 {
3962 tree decl;
3963 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3964 {
1f8f4a0b 3965 tree name, pname;
c2952b01
APB
3966 tree wfl, init, list;
3967
3968 /* Avoid non final arguments. */
c7303e41 3969 if (!LOCAL_FINAL_P (decl))
c2952b01
APB
3970 continue;
3971
3972 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3973 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3974 wfl = build_wfl_node (name);
3975 init = build_wfl_node (pname);
c2952b01 3976 /* Build an initialization for the field: it will be
c00f0fb2 3977 initialized by a parameter added to finit$, bearing a
c2952b01 3978 mangled name of the field itself (param$<n>.) The
c00f0fb2 3979 parameter is provided to finit$ by the constructor
c2952b01
APB
3980 invoking it (hence the constructor will also feature a
3981 hidden parameter, set to the value of the outer context
3982 local at the time the inner class is created.)
3983
3984 Note: we take into account all possible locals that can
3985 be accessed by the inner class. It's actually not trivial
3986 to minimize these aliases down to the ones really
3987 used. One way to do that would be to expand all regular
c00f0fb2 3988 methods first, then finit$ to get a picture of what's
c2952b01
APB
3989 used. It works with the exception that we would have to
3990 go back on all constructor invoked in regular methods to
3991 have their invokation reworked (to include the right amount
3992 of alias initializer parameters.)
3993
3994 The only real way around, I think, is a first pass to
3995 identify locals really used in the inner class. We leave
3996 the flag FIELD_LOCAL_ALIAS_USED around for that future
3997 use.
3998
3999 On the other hand, it only affect local inner classes,
c00f0fb2 4000 whose constructors (and finit$ call) will be featuring
c2952b01
APB
4001 unecessary arguments. It's easy for a developper to keep
4002 this number of parameter down by using the `final'
4003 keyword only when necessary. For the time being, we can
4004 issue a warning on unecessary finals. FIXME */
4005 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4006 wfl, init);
4007
4008 /* Register the field. The TREE_LIST holding the part
4009 initialized/initializer will be marked ARG_FINAL_P so
4010 that the created field can be marked
4011 FIELD_LOCAL_ALIAS. */
4012 list = build_tree_list (wfl, init);
4013 ARG_FINAL_P (list) = 1;
4014 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4015 }
4016 }
4017
4018 if (!CPC_INITIALIZER_STMT (ctxp))
4019 return;
4020
4021 /* If we ever registered an alias field, insert and marker to
4022 remeber where the list ends. The second part of the list (the one
4023 featuring initialized fields) so it can be later reversed to
4024 enforce 8.5. The marker will be removed during that operation. */
4025 marker = build_tree_list (NULL_TREE, NULL_TREE);
4026 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4027 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4028}
4029
e04a16fb
AG
4030/* Can't use lookup_field () since we don't want to load the class and
4031 can't set the CLASS_LOADED_P flag */
4032
4033static tree
4034find_field (class, name)
4035 tree class;
4036 tree name;
4037{
4038 tree decl;
4039 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4040 {
4041 if (DECL_NAME (decl) == name)
4042 return decl;
4043 }
4044 return NULL_TREE;
4045}
4046
4047/* Wrap around lookup_field that doesn't potentially upset the value
4048 of CLASS */
4049
4050static tree
4051lookup_field_wrapper (class, name)
4052 tree class, name;
4053{
4054 tree type = class;
9a7ab4b3 4055 tree decl = NULL_TREE;
c877974e 4056 java_parser_context_save_global ();
f2760b27
APB
4057
4058 /* Last chance: if we're within the context of an inner class, we
4059 might be trying to access a local variable defined in an outer
4060 context. We try to look for it now. */
9a7ab4b3 4061 if (INNER_CLASS_TYPE_P (class))
f2760b27 4062 {
9a7ab4b3 4063 tree new_name;
1f8f4a0b 4064 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4065 decl = lookup_field (&type, new_name);
f2760b27
APB
4066 if (decl && decl != error_mark_node)
4067 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4068 }
9a7ab4b3
APB
4069 if (!decl || decl == error_mark_node)
4070 {
4071 type = class;
4072 decl = lookup_field (&type, name);
4073 }
f2760b27 4074
c877974e 4075 java_parser_context_restore_global ();
93024893 4076 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4077}
4078
4079/* Find duplicate field within the same class declarations and report
c583dd46
APB
4080 the error. Returns 1 if a duplicated field was found, 0
4081 otherwise. */
e04a16fb
AG
4082
4083static int
c583dd46 4084duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4085 tree new_field_name, new_type, cl;
e04a16fb
AG
4086{
4087 /* This might be modified to work with method decl as well */
c2952b01 4088 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4089 if (decl)
4090 {
c2e3db92 4091 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4092 ((TREE_CODE (new_type) == POINTER_TYPE
4093 && TREE_TYPE (new_type) == NULL_TREE) ?
4094 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4095 lang_printable_name (new_type, 1)));
c877974e
APB
4096 /* The type may not have been completed by the time we report
4097 the error */
c2e3db92 4098 char *t2 = xstrdup (purify_type_name
4a5f66c3 4099 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4100 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4101 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4102 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4103 parse_error_context
4104 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4105 t1, IDENTIFIER_POINTER (new_field_name),
4106 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4107 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4108 free (t1);
4109 free (t2);
c583dd46 4110 return 1;
e04a16fb 4111 }
c583dd46 4112 return 0;
e04a16fb
AG
4113}
4114
4115/* Field registration routine. If TYPE doesn't exist, field
4116 declarations are linked to the undefined TYPE dependency list, to
4117 be later resolved in java_complete_class () */
4118
4119static void
4120register_fields (flags, type, variable_list)
4121 int flags;
4122 tree type, variable_list;
4123{
c583dd46 4124 tree current, saved_type;
c2952b01 4125 tree class_type = NULL_TREE;
e04a16fb
AG
4126 int saved_lineno = lineno;
4127 int must_chain = 0;
4128 tree wfl = NULL_TREE;
4129
c2952b01
APB
4130 if (GET_CPC ())
4131 class_type = TREE_TYPE (GET_CPC ());
4132
4133 if (!class_type || class_type == error_mark_node)
4134 return;
4135
e04a16fb
AG
4136 /* If we're adding fields to interfaces, those fields are public,
4137 static, final */
4138 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4139 {
4140 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4141 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4142 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4143 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4144 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4145 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4146 check_modifiers ("Illegal interface member modifier `%s'", flags,
4147 INTERFACE_FIELD_MODIFIERS);
4148 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4149 }
4150
c583dd46
APB
4151 /* Obtain a suitable type for resolution, if necessary */
4152 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4153
4154 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4155 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4156
c583dd46
APB
4157 for (current = variable_list, saved_type = type; current;
4158 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4159 {
c877974e 4160 tree real_type;
c583dd46 4161 tree field_decl;
e04a16fb
AG
4162 tree cl = TREE_PURPOSE (current);
4163 tree init = TREE_VALUE (current);
4164 tree current_name = EXPR_WFL_NODE (cl);
4165
cf1748bf 4166 /* Can't declare non-final static fields in inner classes */
c2952b01 4167 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4168 && !(flags & ACC_FINAL))
c2952b01 4169 parse_error_context
cf1748bf 4170 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4171 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4172 lang_printable_name (class_type, 0));
4173
c583dd46
APB
4174 /* Process NAME, as it may specify extra dimension(s) for it */
4175 type = build_array_from_name (type, wfl, current_name, &current_name);
4176
c583dd46
APB
4177 /* Type adjustment. We may have just readjusted TYPE because
4178 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4179 a reference if we can and don't have one already. Also
4180 change the name if we have an init. */
4181 if (type != saved_type)
4182 {
1886c9d8 4183 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4184 if (init)
4185 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4186 }
e04a16fb 4187
c877974e
APB
4188 real_type = GET_REAL_TYPE (type);
4189 /* Check for redeclarations */
4190 if (duplicate_declaration_error_p (current_name, real_type, cl))
4191 continue;
4192
c583dd46 4193 /* Set lineno to the line the field was found and create a
5e942c50 4194 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4195 if (flag_emit_xref)
4196 lineno = EXPR_WFL_LINECOL (cl);
4197 else
4198 lineno = EXPR_WFL_LINENO (cl);
c877974e 4199 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4200 CHECK_DEPRECATED (field_decl);
c2952b01 4201
c7303e41
APB
4202 /* If the field denotes a final instance variable, then we
4203 allocate a LANG_DECL_SPECIFIC part to keep track of its
4204 initialization. We also mark whether the field was
4205 initialized upon it's declaration. We don't do that if the
4206 created field is an alias to a final local. */
4207 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4208 {
4209 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4210 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4211 if ((flags & ACC_STATIC) && init)
4212 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4213 }
4214
4215 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4216 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4217 hide parameters to this inner class finit$ and
4218 constructors. It also means that the field isn't final per
4219 say. */
c2952b01 4220 if (ARG_FINAL_P (current))
c7303e41
APB
4221 {
4222 FIELD_LOCAL_ALIAS (field_decl) = 1;
4223 FIELD_FINAL (field_decl) = 0;
4224 }
c583dd46
APB
4225
4226 /* Check if we must chain. */
4227 if (must_chain)
4228 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4229
c583dd46
APB
4230 /* If we have an initialization value tied to the field */
4231 if (init)
4232 {
4233 /* The field is declared static */
e04a16fb 4234 if (flags & ACC_STATIC)
e04a16fb 4235 {
7525cc04
APB
4236 /* We include the field and its initialization part into
4237 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4238 walked, field initializations will be processed and
4239 fields initialized with known constants will be taken
4240 out of <clinit> and have their DECL_INITIAL set
7525cc04 4241 appropriately. */
c2952b01
APB
4242 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4243 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4244 if (TREE_OPERAND (init, 1)
4245 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4246 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4247 }
5e942c50
APB
4248 /* A non-static field declared with an immediate initialization is
4249 to be initialized in <init>, if any. This field is remembered
4250 to be processed at the time of the generation of <init>. */
c583dd46
APB
4251 else
4252 {
c2952b01
APB
4253 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4254 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4255 }
5b09b33e 4256 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4257 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4258 }
4259 }
4260 lineno = saved_lineno;
4261}
4262
c00f0fb2
APB
4263/* Generate finit$, using the list of initialized fields to populate
4264 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4265 one(s) used to initialized the field(s) caching outer context
4266 local(s). */
22eed1e6 4267
c2952b01
APB
4268static tree
4269generate_finit (class_type)
4270 tree class_type;
22eed1e6 4271{
c2952b01
APB
4272 int count = 0;
4273 tree list = TYPE_FINIT_STMT_LIST (class_type);
4274 tree mdecl, current, parms;
4275
4276 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4277 class_type, NULL_TREE,
4278 &count);
4279 CRAFTED_PARAM_LIST_FIXUP (parms);
4280 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4281 finit_identifier_node, parms);
4282 fix_method_argument_names (parms, mdecl);
4283 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4284 mdecl, NULL_TREE);
4285 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4286 start_artificial_method_body (mdecl);
4287
c2952b01 4288 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4289 java_method_add_stmt (mdecl,
4290 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4291 current));
22eed1e6 4292 end_artificial_method_body (mdecl);
c2952b01 4293 return mdecl;
22eed1e6
APB
4294}
4295
e04a16fb 4296static void
c2952b01
APB
4297add_instance_initializer (mdecl)
4298 tree mdecl;
e04a16fb 4299{
c2952b01
APB
4300 tree current;
4301 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4302 tree compound = NULL_TREE;
e04a16fb 4303
c2952b01 4304 if (stmt_list)
e04a16fb 4305 {
c2952b01
APB
4306 for (current = stmt_list; current; current = TREE_CHAIN (current))
4307 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4308
c2952b01
APB
4309 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4310 NULL_TREE, compound));
4311 }
e04a16fb
AG
4312}
4313
4314/* Shared accros method_declarator and method_header to remember the
4315 patch stage that was reached during the declaration of the method.
4316 A method DECL is built differently is there is no patch
4317 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4318 pending on the currently defined method. */
4319
4320static int patch_stage;
4321
4322/* Check the method declaration and add the method to its current
4323 class. If the argument list is known to contain incomplete types,
4324 the method is partially added and the registration will be resume
22eed1e6
APB
4325 once the method arguments resolved. If TYPE is NULL, we're dealing
4326 with a constructor. */
e04a16fb
AG
4327
4328static tree
4329method_header (flags, type, mdecl, throws)
4330 int flags;
4331 tree type, mdecl, throws;
4332{
1886c9d8 4333 tree type_wfl = NULL_TREE;
79d13333 4334 tree meth_name = NULL_TREE;
c2952b01 4335 tree current, orig_arg, this_class = NULL;
34d4df06 4336 tree id, meth;
e04a16fb 4337 int saved_lineno;
1886c9d8 4338 int constructor_ok = 0, must_chain;
c2952b01 4339 int count;
34d4df06
APB
4340
4341 if (mdecl == error_mark_node)
4342 return error_mark_node;
4343 meth = TREE_VALUE (mdecl);
4344 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4345
4346 check_modifiers_consistency (flags);
79d13333 4347
c2952b01
APB
4348 if (GET_CPC ())
4349 this_class = TREE_TYPE (GET_CPC ());
4350
4351 if (!this_class || this_class == error_mark_node)
79d13333 4352 return NULL_TREE;
e04a16fb
AG
4353
4354 /* There are some forbidden modifiers for an abstract method and its
4355 class must be abstract as well. */
22eed1e6 4356 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4357 {
4358 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4359 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4360 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4361 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4362 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4363 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4364 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4365 parse_error_context
781b0558 4366 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4367 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4368 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4369 }
c2952b01 4370
22eed1e6
APB
4371 /* Things to be checked when declaring a constructor */
4372 if (!type)
4373 {
4374 int ec = java_error_count;
4375 /* 8.6: Constructor declarations: we might be trying to define a
4376 method without specifying a return type. */
c2952b01 4377 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4378 parse_error_context
4379 (id, "Invalid method declaration, return type required");
4380 /* 8.6.3: Constructor modifiers */
4381 else
4382 {
4383 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4384 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4385 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4386 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4387 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4388 }
4389 /* If we found error here, we don't consider it's OK to tread
4390 the method definition as a constructor, for the rest of this
4391 function */
4392 if (ec == java_error_count)
4393 constructor_ok = 1;
4394 }
e04a16fb
AG
4395
4396 /* Method declared within the scope of an interface are implicitly
4397 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4398 modifiers are checked right after. */
e04a16fb
AG
4399
4400 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4401 {
4402 /* If FLAGS isn't set because of a modifier, turn the
4403 corresponding modifier WFL to NULL so we issue a warning on
4404 the obsolete use of the modifier */
4405 if (!(flags & ACC_PUBLIC))
4406 MODIFIER_WFL (PUBLIC_TK) = NULL;
4407 if (!(flags & ACC_ABSTRACT))
4408 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4409 flags |= ACC_PUBLIC;
4410 flags |= ACC_ABSTRACT;
4411 }
4412
c2952b01
APB
4413 /* Inner class can't declare static methods */
4414 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4415 {
4416 parse_error_context
4417 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4418 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4419 lang_printable_name (this_class, 0));
4420 }
4421
e04a16fb
AG
4422 /* Modifiers context reset moved up, so abstract method declaration
4423 modifiers can be later checked. */
4424
22eed1e6
APB
4425 /* Set constructor returned type to void and method name to <init>,
4426 unless we found an error identifier the constructor (in which
4427 case we retain the original name) */
4428 if (!type)
4429 {
4430 type = void_type_node;
4431 if (constructor_ok)
4432 meth_name = init_identifier_node;
4433 }
4434 else
4435 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4436
1886c9d8
APB
4437 /* Do the returned type resolution and registration if necessary */
4438 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4439
4a5f66c3
APB
4440 if (meth_name)
4441 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4442 EXPR_WFL_NODE (id) = meth_name;
4443 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4444
4445 if (must_chain)
e04a16fb 4446 {
1886c9d8
APB
4447 patch_stage = JDEP_METHOD_RETURN;
4448 register_incomplete_type (patch_stage, type_wfl, id, type);
4449 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4450 }
4451 else
1886c9d8 4452 TREE_TYPE (meth) = type;
e04a16fb
AG
4453
4454 saved_lineno = lineno;
4455 /* When defining an abstract or interface method, the curly
4456 bracket at level 1 doesn't exist because there is no function
4457 body */
4458 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4459 EXPR_WFL_LINENO (id));
4460
5e942c50
APB
4461 /* Remember the original argument list */
4462 orig_arg = TYPE_ARG_TYPES (meth);
4463
e04a16fb
AG
4464 if (patch_stage) /* includes ret type and/or all args */
4465 {
4466 jdep *jdep;
4467 meth = add_method_1 (this_class, flags, meth_name, meth);
4468 /* Patch for the return type */
4469 if (patch_stage == JDEP_METHOD_RETURN)
4470 {
4471 jdep = CLASSD_LAST (ctxp->classd_list);
4472 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4473 }
4474 /* This is the stop JDEP. METH allows the function's signature
4475 to be computed. */
4476 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4477 }
4478 else
5e942c50
APB
4479 meth = add_method (this_class, flags, meth_name,
4480 build_java_signature (meth));
4481
c2952b01
APB
4482 /* Remember final parameters */
4483 MARK_FINAL_PARMS (meth, orig_arg);
4484
5e942c50
APB
4485 /* Fix the method argument list so we have the argument name
4486 information */
4487 fix_method_argument_names (orig_arg, meth);
4488
4489 /* Register the parameter number and re-install the current line
4490 number */
e04a16fb
AG
4491 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4492 lineno = saved_lineno;
b9f7e36c
APB
4493
4494 /* Register exception specified by the `throws' keyword for
4495 resolution and set the method decl appropriate field to the list.
4496 Note: the grammar ensures that what we get here are class
4497 types. */
4498 if (throws)
4499 {
4500 throws = nreverse (throws);
4501 for (current = throws; current; current = TREE_CHAIN (current))
4502 {
4503 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4504 NULL_TREE, NULL_TREE);
4505 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4506 &TREE_VALUE (current);
4507 }
4508 DECL_FUNCTION_THROWS (meth) = throws;
4509 }
4510
c4faeb92
APB
4511 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4512 DECL_FUNCTION_WFL (meth) = id;
4513
22eed1e6
APB
4514 /* Set the flag if we correctly processed a constructor */
4515 if (constructor_ok)
c2952b01
APB
4516 {
4517 DECL_CONSTRUCTOR_P (meth) = 1;
4518 /* Compute and store the number of artificial parameters declared
4519 for this constructor */
4520 for (count = 0, current = TYPE_FIELDS (this_class); current;
4521 current = TREE_CHAIN (current))
4522 if (FIELD_LOCAL_ALIAS (current))
4523 count++;
4524 DECL_FUNCTION_NAP (meth) = count;
4525 }
22eed1e6 4526
5e942c50
APB
4527 /* Eventually set the @deprecated tag flag */
4528 CHECK_DEPRECATED (meth);
4529
7f10c2e2
APB
4530 /* If doing xref, store column and line number information instead
4531 of the line number only. */
4532 if (flag_emit_xref)
4533 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4534
e04a16fb
AG
4535 return meth;
4536}
4537
5e942c50
APB
4538static void
4539fix_method_argument_names (orig_arg, meth)
4540 tree orig_arg, meth;
4541{
4542 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4543 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4544 {
4545 TREE_PURPOSE (arg) = this_identifier_node;
4546 arg = TREE_CHAIN (arg);
4547 }
de4c7b02 4548 while (orig_arg != end_params_node)
5e942c50
APB
4549 {
4550 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4551 orig_arg = TREE_CHAIN (orig_arg);
4552 arg = TREE_CHAIN (arg);
4553 }
4554}
4555
22eed1e6
APB
4556/* Complete the method declaration with METHOD_BODY. */
4557
4558static void
b635eb2f 4559finish_method_declaration (method_body)
22eed1e6
APB
4560 tree method_body;
4561{
79d13333
APB
4562 int flags;
4563
4564 if (!current_function_decl)
4565 return;
4566
4567 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4568
4569 /* 8.4.5 Method Body */
4570 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4571 {
c0b00d37
APB
4572 tree name = DECL_NAME (current_function_decl);
4573 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
5256aa37
APB
4574 "%s method `%s' can't have a body defined",
4575 (METHOD_NATIVE (current_function_decl) ?
4576 "Native" : "Abstract"),
c0b00d37 4577 IDENTIFIER_POINTER (name));
5256aa37
APB
4578 method_body = NULL_TREE;
4579 }
4580 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4581 {
c0b00d37 4582 tree name = DECL_NAME (current_function_decl);
781b0558 4583 parse_error_context
c0b00d37 4584 (DECL_FUNCTION_WFL (current_function_decl),
781b0558 4585 "Non native and non abstract method `%s' must have a body defined",
c0b00d37 4586 IDENTIFIER_POINTER (name));
5256aa37
APB
4587 method_body = NULL_TREE;
4588 }
4589
2c56429a
APB
4590 if (flag_emit_class_files && method_body
4591 && TREE_CODE (method_body) == NOP_EXPR
4592 && TREE_TYPE (current_function_decl)
4593 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4594 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4595
22eed1e6
APB
4596 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4597 maybe_absorb_scoping_blocks ();
4598 /* Exit function's body */
4599 exit_block ();
4600 /* Merge last line of the function with first line, directly in the
4601 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4602 if (!flag_emit_xref)
4603 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4604
4605 /* Since function's argument's list are shared, reset the
4606 ARG_FINAL_P parameter that might have been set on some of this
4607 function parameters. */
4608 UNMARK_FINAL_PARMS (current_function_decl);
4609
f099f336
APB
4610 /* So we don't have an irrelevant function declaration context for
4611 the next static block we'll see. */
4612 current_function_decl = NULL_TREE;
22eed1e6
APB
4613}
4614
4615/* Build a an error message for constructor circularity errors. */
4616
4617static char *
4618constructor_circularity_msg (from, to)
4619 tree from, to;
4620{
4621 static char string [4096];
c2e3db92 4622 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4623 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4624 free (t);
4625 return string;
4626}
4627
4628/* Verify a circular call to METH. Return 1 if an error is found, 0
4629 otherwise. */
4630
4631static int
4632verify_constructor_circularity (meth, current)
4633 tree meth, current;
4634{
4635 static tree list = NULL_TREE;
19e223db 4636 static int initialized_p;
22eed1e6 4637 tree c;
19e223db
MM
4638
4639 /* If we haven't already registered LIST with the garbage collector,
4640 do so now. */
4641 if (!initialized_p)
4642 {
4643 ggc_add_tree_root (&list, 1);
4644 initialized_p = 1;
4645 }
4646
22eed1e6
APB
4647 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4648 {
4649 if (TREE_VALUE (c) == meth)
4650 {
4651 char *t;
4652 if (list)
4653 {
4654 tree liste;
4655 list = nreverse (list);
4656 for (liste = list; liste; liste = TREE_CHAIN (liste))
4657 {
4658 parse_error_context
c63b98cd 4659 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4660 constructor_circularity_msg
4661 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4662 java_error_count--;
4663 }
4664 }
c2e3db92 4665 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4666 parse_error_context (TREE_PURPOSE (c),
4667 "%s: recursive invocation of constructor `%s'",
4668 constructor_circularity_msg (current, meth), t);
4669 free (t);
4670 list = NULL_TREE;
4671 return 1;
4672 }
4673 }
4674 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4675 {
4676 list = tree_cons (c, current, list);
4677 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4678 return 1;
4679 list = TREE_CHAIN (list);
4680 }
4681 return 0;
4682}
4683
e04a16fb
AG
4684/* Check modifiers that can be declared but exclusively */
4685
4686static void
4687check_modifiers_consistency (flags)
4688 int flags;
4689{
4690 int acc_count = 0;
4691 tree cl = NULL_TREE;
4692
e0fc4118
TT
4693 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4694 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4695 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4696 if (acc_count > 1)
4697 parse_error_context
e0fc4118
TT
4698 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4699
4700 acc_count = 0;
4701 cl = NULL_TREE;
14d075d8
TT
4702 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4703 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4704 if (acc_count > 1)
4705 parse_error_context (cl,
4706 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4707}
4708
4709/* Check the methode header METH for abstract specifics features */
4710
4711static void
4712check_abstract_method_header (meth)
4713 tree meth;
4714{
4715 int flags = get_access_flags_from_decl (meth);
e04a16fb 4716
2884c41e
KG
4717 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4718 ACC_ABSTRACT, "abstract method",
c4faeb92 4719 IDENTIFIER_POINTER (DECL_NAME (meth)));
2884c41e
KG
4720 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4721 ACC_PUBLIC, "abstract method",
c4faeb92 4722 IDENTIFIER_POINTER (DECL_NAME (meth)));
e04a16fb
AG
4723
4724 check_modifiers ("Illegal modifier `%s' for interface method",
4725 flags, INTERFACE_METHOD_MODIFIERS);
4726}
4727
4728/* Create a FUNCTION_TYPE node and start augmenting it with the
4729 declared function arguments. Arguments type that can't be resolved
4730 are left as they are, but the returned node is marked as containing
4731 incomplete types. */
4732
4733static tree
4734method_declarator (id, list)
4735 tree id, list;
4736{
4737 tree arg_types = NULL_TREE, current, node;
4738 tree meth = make_node (FUNCTION_TYPE);
4739 jdep *jdep;
e04a16fb
AG
4740
4741 patch_stage = JDEP_NO_PATCH;
c2952b01 4742
34d4df06
APB
4743 if (GET_CPC () == error_mark_node)
4744 return error_mark_node;
4745
c2952b01
APB
4746 /* If we're dealing with an inner class constructor, we hide the
4747 this$<n> decl in the name field of its parameter declaration. We
4748 also might have to hide the outer context local alias
4749 initializers. Not done when the class is a toplevel class. */
4750 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4751 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4752 {
4753 tree aliases_list, type, thisn;
4754 /* First the aliases, linked to the regular parameters */
4755 aliases_list =
4756 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4757 TREE_TYPE (GET_CPC ()),
4758 NULL_TREE, NULL);
4759 list = chainon (nreverse (aliases_list), list);
4760
4761 /* Then this$<n> */
4762 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4763 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4764 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4765 list);
4766 }
e04a16fb
AG
4767
4768 for (current = list; current; current = TREE_CHAIN (current))
4769 {
c583dd46 4770 int must_chain = 0;
e04a16fb
AG
4771 tree wfl_name = TREE_PURPOSE (current);
4772 tree type = TREE_VALUE (current);
4773 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4774 tree already, arg_node;
4775 tree type_wfl = NULL_TREE;
23a79c61 4776 tree real_type;
c583dd46
APB
4777
4778 /* Obtain a suitable type for resolution, if necessary */
4779 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4780
4781 /* Process NAME, as it may specify extra dimension(s) for it */
4782 type = build_array_from_name (type, type_wfl, name, &name);
4783 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4784
23a79c61
APB
4785 real_type = GET_REAL_TYPE (type);
4786 if (TREE_CODE (real_type) == RECORD_TYPE)
4787 {
4788 real_type = promote_type (real_type);
4789 if (TREE_CODE (type) == TREE_LIST)
4790 TREE_PURPOSE (type) = real_type;
4791 }
5e942c50 4792
e04a16fb
AG
4793 /* Check redefinition */
4794 for (already = arg_types; already; already = TREE_CHAIN (already))
4795 if (TREE_PURPOSE (already) == name)
4796 {
781b0558
KG
4797 parse_error_context
4798 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4799 IDENTIFIER_POINTER (name),
e04a16fb
AG
4800 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4801 break;
4802 }
4803
4804 /* If we've an incomplete argument type, we know there is a location
4805 to patch when the type get resolved, later. */
4806 jdep = NULL;
c583dd46 4807 if (must_chain)
e04a16fb 4808 {
c583dd46
APB
4809 patch_stage = JDEP_METHOD;
4810 type = register_incomplete_type (patch_stage,
4811 type_wfl, wfl_name, type);
4812 jdep = CLASSD_LAST (ctxp->classd_list);
4813 JDEP_MISC (jdep) = id;
e04a16fb 4814 }
c583dd46 4815
c2952b01 4816 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4817 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4818 /* Remeber arguments declared final. */
4819 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4820
e04a16fb
AG
4821 if (jdep)
4822 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4823 TREE_CHAIN (arg_node) = arg_types;
4824 arg_types = arg_node;
4825 }
de4c7b02 4826 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4827 node = build_tree_list (id, meth);
4828 return node;
4829}
4830
4831static int
4832unresolved_type_p (wfl, returned)
4833 tree wfl;
4834 tree *returned;
4835
4836{
4837 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4838 {
e04a16fb 4839 if (returned)
165f37bc
APB
4840 {
4841 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4842 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4843 *returned = TREE_TYPE (decl);
4844 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4845 *returned = TREE_TYPE (GET_CPC ());
4846 else
4847 *returned = NULL_TREE;
4848 }
e04a16fb
AG
4849 return 1;
4850 }
4851 if (returned)
4852 *returned = wfl;
4853 return 0;
4854}
4855
4856/* From NAME, build a qualified identifier node using the
4857 qualification from the current package definition. */
4858
4859static tree
98a52c2c 4860parser_qualified_classname (name)
e04a16fb
AG
4861 tree name;
4862{
c2952b01
APB
4863 tree nested_class_name;
4864
98a52c2c 4865 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4866 return nested_class_name;
4867
e04a16fb 4868 if (ctxp->package)
c2952b01 4869 return merge_qualified_name (ctxp->package, name);
e04a16fb 4870 else
c2952b01 4871 return name;
e04a16fb
AG
4872}
4873
4874/* Called once the type a interface extends is resolved. Returns 0 if
4875 everything is OK. */
4876
4877static int
4878parser_check_super_interface (super_decl, this_decl, this_wfl)
4879 tree super_decl, this_decl, this_wfl;
4880{
4881 tree super_type = TREE_TYPE (super_decl);
4882
4883 /* Has to be an interface */
c2952b01 4884 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4885 {
4886 parse_error_context
4887 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4888 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4889 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4890 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4891 "interface" : "class"),
4892 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4893 return 1;
4894 }
4895
a648f4e4
BM
4896 /* Check top-level interface access. Inner classes are subject to member
4897 access rules (6.6.1). */
4898 if (! INNER_CLASS_P (super_type)
4899 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
e04a16fb
AG
4900 return 1;
4901
4902 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4903 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4904 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4905 return 0;
4906}
4907
4908/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4909 0 if everthing is OK. */
4910
4911static int
4912parser_check_super (super_decl, this_decl, wfl)
4913 tree super_decl, this_decl, wfl;
4914{
e04a16fb
AG
4915 tree super_type = TREE_TYPE (super_decl);
4916
4917 /* SUPER should be a CLASS (neither an array nor an interface) */
4918 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4919 {
4920 parse_error_context
4921 (wfl, "Class `%s' can't subclass %s `%s'",
4922 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4923 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4924 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4925 return 1;
4926 }
4927
4928 if (CLASS_FINAL (TYPE_NAME (super_type)))
4929 {
4930 parse_error_context (wfl, "Can't subclass final classes: %s",
4931 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4932 return 1;
4933 }
4934
a648f4e4
BM
4935 /* Check top-level class scope. Inner classes are subject to member access
4936 rules (6.6.1). */
4937 if (! INNER_CLASS_P (super_type)
4938 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
e04a16fb
AG
4939 return 1;
4940
4941 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4942 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4943 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4944 return 0;
4945}
4946
4947/* Create a new dependency list and link it (in a LIFO manner) to the
4948 CTXP list of type dependency list. */
4949
4950static void
4951create_jdep_list (ctxp)
4952 struct parser_ctxt *ctxp;
4953{
23a79c61 4954 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4955 new->first = new->last = NULL;
4956 new->next = ctxp->classd_list;
4957 ctxp->classd_list = new;
4958}
4959
4960static jdeplist *
4961reverse_jdep_list (ctxp)
4962 struct parser_ctxt *ctxp;
4963{
4964 register jdeplist *prev = NULL, *current, *next;
4965 for (current = ctxp->classd_list; current; current = next)
4966 {
4967 next = current->next;
4968 current->next = prev;
4969 prev = current;
4970 }
4971 return prev;
4972}
4973
23a79c61
APB
4974/* Create a fake pointer based on the ID stored in
4975 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4976 registered again. */
e04a16fb
AG
4977
4978static tree
23a79c61
APB
4979obtain_incomplete_type (type_name)
4980 tree type_name;
e04a16fb 4981{
23a79c61
APB
4982 tree ptr, name;
4983
4984 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4985 name = EXPR_WFL_NODE (type_name);
4986 else if (INCOMPLETE_TYPE_P (type_name))
4987 name = TYPE_NAME (type_name);
4988 else
400500c4 4989 abort ();
e04a16fb 4990
fea2d5da 4991 for (ptr = incomplete_class_list; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4992 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4993 break;
4994
4995 if (!ptr)
4996 {
78d21f92
PB
4997 BUILD_PTR_FROM_NAME (ptr, name);
4998 layout_type (ptr);
fea2d5da
PB
4999 TREE_CHAIN (ptr) = incomplete_class_list;
5000 incomplete_class_list = ptr;
e04a16fb
AG
5001 }
5002
5003 return ptr;
5004}
5005
5006/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5007 non NULL instead of computing a new fake type based on WFL. The new
5008 dependency is inserted in the current type dependency list, in FIFO
5009 manner. */
5010
5011static tree
5012register_incomplete_type (kind, wfl, decl, ptr)
5013 int kind;
5014 tree wfl, decl, ptr;
5015{
23a79c61 5016 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 5017
e04a16fb
AG
5018 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5019 ptr = obtain_incomplete_type (wfl);
5020
5021 JDEP_KIND (new) = kind;
5022 JDEP_DECL (new) = decl;
5023 JDEP_SOLV (new) = ptr;
5024 JDEP_WFL (new) = wfl;
5025 JDEP_CHAIN (new) = NULL;
5026 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
5027 /* For some dependencies, set the enclosing class of the current
5028 class to be the enclosing context */
8ac1de05
APB
5029 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5030 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
165f37bc
APB
5031 && GET_ENCLOSING_CPC ())
5032 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5033 else
324ed8fd 5034 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
5035 JDEP_GET_PATCH (new) = (tree *)NULL;
5036
5037 JDEP_INSERT (ctxp->classd_list, new);
5038
5039 return ptr;
5040}
5041
5042void
5043java_check_circular_reference ()
5044{
5045 tree current;
5046 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5047 {
5048 tree type = TREE_TYPE (current);
e920ebc9 5049 if (CLASS_INTERFACE (current))
e04a16fb
AG
5050 {
5051 /* Check all interfaces this class extends */
5052 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5053 int n, i;
5054
5055 if (!basetype_vec)
5056 return;
5057 n = TREE_VEC_LENGTH (basetype_vec);
5058 for (i = 0; i < n; i++)
5059 {
5060 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5061 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5062 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5063 parse_error_context (lookup_cl (current),
5064 "Cyclic interface inheritance");
5065 }
5066 }
5067 else
5068 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5069 parse_error_context (lookup_cl (current),
c2952b01
APB
5070 "Cyclic class inheritance%s",
5071 (cyclic_inheritance_report ?
5072 cyclic_inheritance_report : ""));
5073 }
5074}
5075
5076/* Augment the parameter list PARM with parameters crafted to
5077 initialize outer context locals aliases. Through ARTIFICIAL, a
5078 count is kept of the number of crafted parameters. MODE governs
5079 what eventually gets created: something suitable for a function
5080 creation or a function invocation, either the constructor or
c00f0fb2 5081 finit$. */
c2952b01
APB
5082
5083static tree
5084build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5085 int mode;
5086 tree class_type, parm;
5087 int *artificial;
5088{
5089 tree field;
da632f2c
APB
5090 tree additional_parms = NULL_TREE;
5091
c2952b01
APB
5092 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5093 if (FIELD_LOCAL_ALIAS (field))
5094 {
63ad61ed 5095 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5096 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5097 tree mangled_id;
c2952b01
APB
5098
5099 switch (mode)
5100 {
5101 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5102 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5103 &buffer [4]);
5104 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5105 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5106 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5107 else
5108 value = TREE_TYPE (field);
5109 break;
5110
5111 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5112 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5113 &buffer [4]);
c2952b01
APB
5114 value = TREE_TYPE (field);
5115 break;
5116
5117 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5118 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5119 &buffer [4]);
c2952b01
APB
5120 /* Now, this is wrong. purpose should always be the NAME
5121 of something and value its matching value (decl, type,
5122 etc...) FIXME -- but there is a lot to fix. */
5123
5124 /* When invoked for this kind of operation, we already
5125 know whether a field is used or not. */
5126 purpose = TREE_TYPE (field);
1f8f4a0b 5127 value = build_wfl_node (mangled_id);
c2952b01
APB
5128 break;
5129
5130 case AIPL_FUNCTION_CTOR_INVOCATION:
5131 /* There are two case: the constructor invokation happends
5132 outside the local inner, in which case, locales from the outer
5133 context are directly used.
5134
5135 Otherwise, we fold to using the alias directly. */
5136 if (class_type == current_class)
5137 value = field;
5138 else
5139 {
5140 name = get_identifier (&buffer[4]);
5141 value = IDENTIFIER_LOCAL_VALUE (name);
5142 }
5143 break;
5144 }
da632f2c 5145 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5146 if (artificial)
5147 *artificial +=1;
5148 }
da632f2c
APB
5149 if (additional_parms)
5150 {
5151 if (ANONYMOUS_CLASS_P (class_type)
5152 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5153 additional_parms = nreverse (additional_parms);
5154 parm = chainon (additional_parms, parm);
5155 }
5156
5157 return parm;
c2952b01
APB
5158}
5159
5160/* Craft a constructor for CLASS_DECL -- what we should do when none
5161 where found. ARGS is non NULL when a special signature must be
5162 enforced. This is the case for anonymous classes. */
5163
5164static void
5165craft_constructor (class_decl, args)
5166 tree class_decl, args;
5167{
5168 tree class_type = TREE_TYPE (class_decl);
5169 tree parm = NULL_TREE;
5170 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5171 ACC_PUBLIC : 0);
5172 int i = 0, artificial = 0;
5173 tree decl, ctor_name;
5174 char buffer [80];
5175
c2952b01
APB
5176 /* The constructor name is <init> unless we're dealing with an
5177 anonymous class, in which case the name will be fixed after having
5178 be expanded. */
5179 if (ANONYMOUS_CLASS_P (class_type))
5180 ctor_name = DECL_NAME (class_decl);
5181 else
5182 ctor_name = init_identifier_node;
5183
5184 /* If we're dealing with an inner class constructor, we hide the
5185 this$<n> decl in the name field of its parameter declaration. */
5186 if (PURE_INNER_CLASS_TYPE_P (class_type))
5187 {
5188 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5189 parm = tree_cons (build_current_thisn (class_type),
5190 build_pointer_type (type), parm);
5191
5192 /* Some more arguments to be hidden here. The values of the local
5193 variables of the outer context that the inner class needs to see. */
5194 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5195 class_type, parm,
5196 &artificial);
5197 }
5198
5199 /* Then if there are any args to be enforced, enforce them now */
5200 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5201 {
5202 sprintf (buffer, "parm%d", i++);
5203 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5204 }
c2952b01
APB
5205
5206 CRAFTED_PARAM_LIST_FIXUP (parm);
5207 decl = create_artificial_method (class_type, flags, void_type_node,
5208 ctor_name, parm);
5209 fix_method_argument_names (parm, decl);
5210 /* Now, mark the artificial parameters. */
5211 DECL_FUNCTION_NAP (decl) = artificial;
c7303e41 5212 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5213}
5214
c2952b01 5215
e920ebc9
APB
5216/* Fix the constructors. This will be called right after circular
5217 references have been checked. It is necessary to fix constructors
5218 early even if no code generation will take place for that class:
5219 some generated constructor might be required by the class whose
5220 compilation triggered this one to be simply loaded. */
5221
5222void
5223java_fix_constructors ()
5224{
5225 tree current;
5226
5227 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5228 {
e920ebc9
APB
5229 tree class_type = TREE_TYPE (current);
5230 int saw_ctor = 0;
c2952b01
APB
5231 tree decl;
5232
5233 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5234 continue;
e920ebc9
APB
5235
5236 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5237 {
5238 if (DECL_CONSTRUCTOR_P (decl))
5239 {
5240 fix_constructors (decl);
5241 saw_ctor = 1;
5242 }
5243 }
5244
c2952b01
APB
5245 /* Anonymous class constructor can't be generated that early. */
5246 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5247 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5248 }
5249}
5250
23a79c61
APB
5251/* safe_layout_class just makes sure that we can load a class without
5252 disrupting the current_class, input_file, lineno, etc, information
5253 about the class processed currently. */
5254
e04a16fb
AG
5255void
5256safe_layout_class (class)
5257 tree class;
5258{
5259 tree save_current_class = current_class;
3b304f5b 5260 const char *save_input_filename = input_filename;
e04a16fb 5261 int save_lineno = lineno;
5e942c50 5262
e04a16fb 5263 layout_class (class);
5e942c50 5264
e04a16fb
AG
5265 current_class = save_current_class;
5266 input_filename = save_input_filename;
5267 lineno = save_lineno;
5268 CLASS_LOADED_P (class) = 1;
5269}
5270
5271static tree
5272jdep_resolve_class (dep)
5273 jdep *dep;
5274{
5275 tree decl;
5276
23a79c61
APB
5277 if (JDEP_RESOLVED_P (dep))
5278 decl = JDEP_RESOLVED_DECL (dep);
5279 else
e04a16fb 5280 {
c2952b01 5281 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5282 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5283 JDEP_RESOLVED (dep, decl);
5284 }
23a79c61 5285
e04a16fb 5286 if (!decl)
23a79c61 5287 complete_class_report_errors (dep);
1e12ab9b 5288 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5289 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5290 return decl;
5291}
5292
5293/* Complete unsatisfied class declaration and their dependencies */
5294
5295void
5296java_complete_class ()
5297{
e04a16fb
AG
5298 tree cclass;
5299 jdeplist *cclassd;
5300 int error_found;
b67d701b 5301 tree type;
e04a16fb 5302
9a7ab4b3 5303 /* Process imports */
e04a16fb 5304 process_imports ();
e04a16fb
AG
5305
5306 /* Rever things so we have the right order */
5307 ctxp->class_list = nreverse (ctxp->class_list);
5308 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5309
e04a16fb
AG
5310 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5311 cclass && cclassd;
5312 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5313 {
5314 jdep *dep;
5315 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5316 {
5317 tree decl;
e04a16fb
AG
5318 if (!(decl = jdep_resolve_class (dep)))
5319 continue;
5320
5321 /* Now it's time to patch */
5322 switch (JDEP_KIND (dep))
5323 {
5324 case JDEP_SUPER:
5325 /* Simply patch super */
5326 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5327 continue;
5328 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5329 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5330 break;
5331
5332 case JDEP_FIELD:
5333 {
5334 /* We do part of the job done in add_field */
5335 tree field_decl = JDEP_DECL (dep);
5336 tree field_type = TREE_TYPE (decl);
e04a16fb 5337 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5338 field_type = promote_type (field_type);
e04a16fb 5339 TREE_TYPE (field_decl) = field_type;
5e942c50 5340 DECL_ALIGN (field_decl) = 0;
11cf4d18 5341 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5342 layout_decl (field_decl, 0);
e04a16fb
AG
5343 SOURCE_FRONTEND_DEBUG
5344 (("Completed field/var decl `%s' with `%s'",
5345 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5346 IDENTIFIER_POINTER (DECL_NAME (decl))));
5347 break;
5348 }
5349 case JDEP_METHOD: /* We start patching a method */
5350 case JDEP_METHOD_RETURN:
5351 error_found = 0;
5352 while (1)
5353 {
5354 if (decl)
5355 {
b67d701b
PB
5356 type = TREE_TYPE(decl);
5357 if (TREE_CODE (type) == RECORD_TYPE)
5358 type = promote_type (type);
e04a16fb
AG
5359 JDEP_APPLY_PATCH (dep, type);
5360 SOURCE_FRONTEND_DEBUG
5361 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5362 "Completing fct `%s' with ret type `%s'":
5363 "Completing arg `%s' with type `%s'"),
5364 IDENTIFIER_POINTER (EXPR_WFL_NODE
5365 (JDEP_DECL_WFL (dep))),
5366 IDENTIFIER_POINTER (DECL_NAME (decl))));
5367 }
5368 else
5369 error_found = 1;
5370 dep = JDEP_CHAIN (dep);
5371 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5372 break;
5373 else
5374 decl = jdep_resolve_class (dep);
5375 }
5376 if (!error_found)
5377 {
5378 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5379 /* Recompute and reset the signature, check first that
5380 all types are now defined. If they're not,
5381 dont build the signature. */
5382 if (check_method_types_complete (mdecl))
5383 {
5384 signature = build_java_signature (TREE_TYPE (mdecl));
5385 set_java_signature (TREE_TYPE (mdecl), signature);
5386 }
e04a16fb
AG
5387 }
5388 else
5389 continue;
5390 break;
5391
5392 case JDEP_INTERFACE:
5393 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5394 JDEP_WFL (dep)))
5395 continue;
5396 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5397 break;
5398
b67d701b 5399 case JDEP_PARM:
e04a16fb 5400 case JDEP_VARIABLE:
b67d701b
PB
5401 type = TREE_TYPE(decl);
5402 if (TREE_CODE (type) == RECORD_TYPE)
5403 type = promote_type (type);
5404 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5405 break;
5406
5407 case JDEP_TYPE:
5408 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5409 SOURCE_FRONTEND_DEBUG
5410 (("Completing a random type dependency on a '%s' node",
5411 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5412 break;
5413
b9f7e36c 5414 case JDEP_EXCEPTION:
c877974e
APB
5415 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5416 SOURCE_FRONTEND_DEBUG
5417 (("Completing `%s' `throws' argument node",
5418 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5419 break;
5420
c2952b01
APB
5421 case JDEP_ANONYMOUS:
5422 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5423 break;
5424
e04a16fb 5425 default:
400500c4 5426 abort ();
e04a16fb
AG
5427 }
5428 }
5429 }
e04a16fb
AG
5430 return;
5431}
5432
5433/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5434 array. */
5435
5436static tree
c2952b01
APB
5437resolve_class (enclosing, class_type, decl, cl)
5438 tree enclosing, class_type, decl, cl;
e04a16fb 5439{
49f48c71
KG
5440 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5441 const char *base = name;
78d21f92
PB
5442 tree resolved_type = TREE_TYPE (class_type);
5443 tree resolved_type_decl;
e04a16fb 5444
78d21f92
PB
5445 if (resolved_type != NULL_TREE)
5446 {
5447 tree resolved_type_decl = TYPE_NAME (resolved_type);
5448 if (resolved_type_decl == NULL_TREE
5449 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5450 {
5451 resolved_type_decl = build_decl (TYPE_DECL,
5452 TYPE_NAME (class_type),
5453 resolved_type);
5454 }
5455 return resolved_type_decl;
5456 }
5457
e04a16fb
AG
5458 /* 1- Check to see if we have an array. If true, find what we really
5459 want to resolve */
5460 while (name[0] == '[')
5461 name++;
5462 if (base != name)
34d4df06
APB
5463 {
5464 TYPE_NAME (class_type) = get_identifier (name);
5465 WFL_STRIP_BRACKET (cl, cl);
5466 }
e04a16fb
AG
5467
5468 /* 2- Resolve the bare type */
c2952b01
APB
5469 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5470 decl, cl)))
e04a16fb
AG
5471 return NULL_TREE;
5472 resolved_type = TREE_TYPE (resolved_type_decl);
5473
5474 /* 3- If we have and array, reconstruct the array down to its nesting */
5475 if (base != name)
5476 {
5477 while (base != name)
5478 {
5479 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5480 resolved_type = promote_type (resolved_type);
5481 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5482 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5483 name--;
5484 }
e101152f
APB
5485 /* A TYPE_NAME that is a TYPE_DECL was set in
5486 build_java_array_type, return it. */
5487 resolved_type_decl = TYPE_NAME (resolved_type);
e04a16fb 5488 }
78d21f92 5489 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5490 return resolved_type_decl;
5491}
5492
5493/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5494 are used to report error messages. */
5495
78d21f92 5496tree
c2952b01
APB
5497do_resolve_class (enclosing, class_type, decl, cl)
5498 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5499{
5500 tree new_class_decl;
e04a16fb
AG
5501
5502 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5503 it is changed by find_in_imports{_on_demand} and (but it doesn't
5504 really matter) qualify_and_find */
e04a16fb 5505
c2952b01
APB
5506 /* 0- Search in the current class as an inner class */
5507
5508 /* Maybe some code here should be added to load the class or
5509 something, at least if the class isn't an inner class and ended
5510 being loaded from class file. FIXME. */
a40d21da
APB
5511 while (enclosing)
5512 {
e1d565ab 5513 tree intermediate;
a40d21da
APB
5514
5515 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5516 return new_class_decl;
8ac1de05 5517
e1d565ab 5518 intermediate = enclosing;
0c2b8145 5519 /* Explore enclosing contexts. */
e1d565ab 5520 while (INNER_CLASS_DECL_P (intermediate))
0c2b8145 5521 {
e1d565ab
BM
5522 intermediate = DECL_CONTEXT (intermediate);
5523 if ((new_class_decl = find_as_inner_class (intermediate,
0c2b8145
APB
5524 class_type, cl)))
5525 return new_class_decl;
5526 }
5527
a40d21da
APB
5528 /* Now go to the upper classes, bail out if necessary. */
5529 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5530 if (!enclosing || enclosing == object_type_node)
a648f4e4 5531 break;
a40d21da 5532
a648f4e4
BM
5533 if (TREE_CODE (enclosing) == POINTER_TYPE)
5534 enclosing = do_resolve_class (NULL, enclosing, NULL, NULL);
a40d21da 5535 else
a648f4e4 5536 enclosing = TYPE_NAME (enclosing);
a40d21da 5537 }
c2952b01 5538
9a7ab4b3
APB
5539 /* 1- Check for the type in single imports. This will change
5540 TYPE_NAME() if something relevant is found */
5541 find_in_imports (class_type);
e04a16fb 5542
9a7ab4b3 5543 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5544 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5545 {
5546 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5547 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5548 load_class (TYPE_NAME (class_type), 0);
5549 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5550 }
5551
9a7ab4b3
APB
5552 /* 3- Search according to the current package definition */
5553 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5554 {
5555 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5556 TYPE_NAME (class_type))))
5557 return new_class_decl;
5558 }
5559
5560 /* 4- Check the import on demands. Don't allow bar.baz to be
5561 imported from foo.* */
5562 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5563 if (find_in_imports_on_demand (class_type))
5564 return NULL_TREE;
5565
5566 /* If found in find_in_imports_on_demant, the type has already been
5567 loaded. */
5568 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5569 return new_class_decl;
5570
5571 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5572 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5573 {
ee07f4f4 5574 tree package;
d6baf6f5
APB
5575
5576 /* If there is a current package (ctxp->package), it's the first
5577 element of package_list and we can skip it. */
5578 for (package = (ctxp->package ?
5579 TREE_CHAIN (package_list) : package_list);
5580 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5581 if ((new_class_decl = qualify_and_find (class_type,
5582 TREE_PURPOSE (package),
5583 TYPE_NAME (class_type))))
5584 return new_class_decl;
5585 }
5586
5587 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb 5588 load_class (TYPE_NAME (class_type), 0);
a648f4e4
BM
5589
5590 if (!cl)
5591 cl = lookup_cl (decl);
5592
5593 /* If we don't have a value for CL, then we're being called recursively.
5594 We can't check package access just yet, but it will be taken care of
5595 by the caller. */
5596 if (cl)
5597 {
5598 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5599 return NULL_TREE;
5600 }
5601
9a7ab4b3 5602 /* 6- Last call for a resolution */
e04a16fb
AG
5603 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5604}
5605
9a7ab4b3
APB
5606static tree
5607qualify_and_find (class_type, package, name)
5608 tree class_type, package, name;
5609{
5610 tree new_qualified = merge_qualified_name (package, name);
5611 tree new_class_decl;
5612
5613 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5614 load_class (new_qualified, 0);
5615 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5616 {
5617 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5618 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5619 load_class (new_qualified, 0);
5620 TYPE_NAME (class_type) = new_qualified;
5621 return IDENTIFIER_CLASS_VALUE (new_qualified);
5622 }
5623 return NULL_TREE;
5624}
5625
e04a16fb 5626/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5627 parsed class). Return a decl node. This function is meant to be
5628 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5629
5630static tree
c877974e
APB
5631resolve_and_layout (something, cl)
5632 tree something;
e04a16fb
AG
5633 tree cl;
5634{
34d4df06 5635 tree decl, decl_type;
c877974e 5636
23a79c61
APB
5637 /* Don't do that on the current class */
5638 if (something == current_class)
5639 return TYPE_NAME (current_class);
c877974e 5640
23a79c61 5641 /* Don't do anything for void and other primitive types */
c877974e
APB
5642 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5643 return NULL_TREE;
5644
23a79c61
APB
5645 /* Pointer types can be reall pointer types or fake pointers. When
5646 finding a real pointer, recheck for primitive types */
5647 if (TREE_CODE (something) == POINTER_TYPE)
5648 {
5649 if (TREE_TYPE (something))
5650 {
5651 something = TREE_TYPE (something);
5652 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5653 return NULL_TREE;
5654 }
5655 else
5656 something = TYPE_NAME (something);
5657 }
5658
5659 /* Don't do anything for arrays of primitive types */
5660 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5661 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5662 return NULL_TREE;
5663
c2952b01
APB
5664 /* Something might be a WFL */
5665 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5666 something = EXPR_WFL_NODE (something);
5667
5668 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5669 TYPE_DECL or a real TYPE */
5670 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5671 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5672 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5673
23a79c61
APB
5674 if (!(decl = resolve_no_layout (something, cl)))
5675 return NULL_TREE;
5676
5677 /* Resolve and layout if necessary */
34d4df06
APB
5678 decl_type = TREE_TYPE (decl);
5679 layout_class_methods (decl_type);
5680 /* Check methods */
5681 if (CLASS_FROM_SOURCE_P (decl_type))
5682 java_check_methods (decl);
5683 /* Layout the type if necessary */
5684 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5685 safe_layout_class (decl_type);
23a79c61 5686
e04a16fb
AG
5687 return decl;
5688}
5689
5690/* Resolve a class, returns its decl but doesn't perform any
5691 layout. The current parsing context is saved and restored */
5692
5693static tree
5694resolve_no_layout (name, cl)
5695 tree name, cl;
5696{
5697 tree ptr, decl;
5698 BUILD_PTR_FROM_NAME (ptr, name);
5699 java_parser_context_save_global ();
c2952b01 5700 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5701 java_parser_context_restore_global ();
5702
5703 return decl;
5704}
5705
23a79c61
APB
5706/* Called when reporting errors. Skip leader '[' in a complex array
5707 type description that failed to be resolved. */
e04a16fb 5708
49f48c71 5709static const char *
e04a16fb 5710purify_type_name (name)
49f48c71 5711 const char *name;
e04a16fb
AG
5712{
5713 while (*name && *name == '[')
5714 name++;
5715 return name;
5716}
5717
5718/* The type CURRENT refers to can't be found. We print error messages. */
5719
5720static void
5721complete_class_report_errors (dep)
5722 jdep *dep;
5723{
49f48c71 5724 const char *name;
23a79c61
APB
5725
5726 if (!JDEP_WFL (dep))
5727 return;
5728
5729 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5730 switch (JDEP_KIND (dep))
5731 {
5732 case JDEP_SUPER:
5733 parse_error_context
5734 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5735 purify_type_name (name),
e04a16fb
AG
5736 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5737 break;
5738 case JDEP_FIELD:
5739 parse_error_context
5740 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5741 purify_type_name (name),
e04a16fb
AG
5742 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5743 break;
5744 case JDEP_METHOD: /* Covers arguments */
5745 parse_error_context
781b0558 5746 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5747 purify_type_name (name),
e04a16fb
AG
5748 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5749 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5750 break;
5751 case JDEP_METHOD_RETURN: /* Covers return type */
5752 parse_error_context
781b0558 5753 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5754 purify_type_name (name),
e04a16fb
AG
5755 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5756 break;
5757 case JDEP_INTERFACE:
5758 parse_error_context
5759 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5760 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5761 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5762 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5763 break;
5764 case JDEP_VARIABLE:
5765 parse_error_context
781b0558 5766 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5767 purify_type_name (IDENTIFIER_POINTER
5768 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5769 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5770 break;
b9f7e36c
APB
5771 case JDEP_EXCEPTION: /* As specified by `throws' */
5772 parse_error_context
5773 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5774 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5775 break;
0a2138e2
APB
5776 default:
5777 /* Fix for -Wall. Just break doing nothing. The error will be
5778 caught later */
5779 break;
e04a16fb
AG
5780 }
5781}
5782
22eed1e6
APB
5783/* Return a static string containing the DECL prototype string. If
5784 DECL is a constructor, use the class name instead of the form
5785 <init> */
5786
49f48c71 5787static const char *
22eed1e6
APB
5788get_printable_method_name (decl)
5789 tree decl;
5790{
49f48c71 5791 const char *to_return;
9ee9b555 5792 tree name = NULL_TREE;
22eed1e6
APB
5793
5794 if (DECL_CONSTRUCTOR_P (decl))
5795 {
5796 name = DECL_NAME (decl);
5e942c50 5797 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5798 }
5799
5800 to_return = lang_printable_name (decl, 0);
5801 if (DECL_CONSTRUCTOR_P (decl))
5802 DECL_NAME (decl) = name;
5803
5804 return to_return;
5805}
5806
5807/* Track method being redefined inside the same class. As a side
5808 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5809 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5810
e04a16fb
AG
5811static int
5812check_method_redefinition (class, method)
5813 tree class, method;
5814{
c4faeb92 5815 tree redef, sig;
5e942c50 5816
c4faeb92
APB
5817 /* There's no need to verify <clinit> and finit$ */
5818 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
e04a16fb 5819 return 0;
5e942c50 5820
c4faeb92 5821 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
e04a16fb
AG
5822 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5823 {
c3f2a476 5824 if (redef == method)
e04a16fb 5825 break;
c4faeb92
APB
5826 if (DECL_NAME (redef) == DECL_NAME (method)
5827 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5828 && !DECL_ARTIFICIAL (method))
e04a16fb 5829 {
22eed1e6 5830 parse_error_context
c4faeb92 5831 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
22eed1e6
APB
5832 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5833 get_printable_method_name (redef));
e04a16fb
AG
5834 return 1;
5835 }
5836 }
5837 return 0;
5838}
5839
1175b9b4
TT
5840/* Return 1 if check went ok, 0 otherwise. */
5841static int
d77613be
APB
5842check_abstract_method_definitions (do_interface, class_decl, type)
5843 int do_interface;
5844 tree class_decl, type;
5845{
5846 tree class = TREE_TYPE (class_decl);
5847 tree method, end_type;
1175b9b4 5848 int ok = 1;
d77613be
APB
5849
5850 end_type = (do_interface ? object_type_node : type);
5851 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5852 {
5853 tree other_super, other_method, method_sig, method_name;
5854 int found = 0;
165f37bc 5855 int end_type_reached = 0;
d77613be
APB
5856
5857 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5858 continue;
5859
5860 /* Now verify that somewhere in between TYPE and CLASS,
5861 abstract method METHOD gets a non abstract definition
5862 that is inherited by CLASS. */
5863
5864 method_sig = build_java_signature (TREE_TYPE (method));
5865 method_name = DECL_NAME (method);
5866 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5867 method_name = EXPR_WFL_NODE (method_name);
5868
165f37bc
APB
5869 other_super = class;
5870 do {
5871 if (other_super == end_type)
5872 end_type_reached = 1;
5873
5874 /* Method search */
5875 for (other_method = TYPE_METHODS (other_super); other_method;
5876 other_method = TREE_CHAIN (other_method))
5877 {
5878 tree s = build_java_signature (TREE_TYPE (other_method));
5879 tree other_name = DECL_NAME (other_method);
5880
5881 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5882 other_name = EXPR_WFL_NODE (other_name);
5883 if (!DECL_CLINIT_P (other_method)
5884 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5885 && method_name == other_name
5886 && method_sig == s
5887 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5888 {
5889 found = 1;
5890 break;
5891 }
5892 }
5893 other_super = CLASSTYPE_SUPER (other_super);
5894 } while (!end_type_reached);
5895
d77613be
APB
5896 /* Report that abstract METHOD didn't find an implementation
5897 that CLASS can use. */
5898 if (!found)
5899 {
c2e3db92 5900 char *t = xstrdup (lang_printable_name
d77613be
APB
5901 (TREE_TYPE (TREE_TYPE (method)), 0));
5902 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
d77613be
APB
5903
5904 parse_error_context
5905 (lookup_cl (class_decl),
781b0558 5906 "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
5907 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5908 t, lang_printable_name (method, 0),
5909 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5910 "interface" : "class"),
5911 IDENTIFIER_POINTER (ccn),
5912 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5913 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5914 ok = 0;
d77613be 5915 free (t);
d77613be
APB
5916 }
5917 }
1175b9b4
TT
5918
5919 if (ok && do_interface)
5920 {
5921 /* Check for implemented interfaces. */
5922 int i;
5923 tree vector = TYPE_BINFO_BASETYPES (type);
5924 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5925 {
5926 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5927 ok = check_abstract_method_definitions (1, class_decl, super);
5928 }
5929 }
5930
5931 return ok;
d77613be
APB
5932}
5933
614eaae0 5934/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5935 methods. */
5936
5937static void
5938java_check_abstract_method_definitions (class_decl)
5939 tree class_decl;
5940{
5941 tree class = TREE_TYPE (class_decl);
5942 tree super, vector;
5943 int i;
5944
5945 if (CLASS_ABSTRACT (class_decl))
5946 return;
5947
5948 /* Check for inherited types */
165f37bc
APB
5949 super = class;
5950 do {
5951 super = CLASSTYPE_SUPER (super);
5952 check_abstract_method_definitions (0, class_decl, super);
5953 } while (super != object_type_node);
d77613be
APB
5954
5955 /* Check for implemented interfaces. */
5956 vector = TYPE_BINFO_BASETYPES (class);
5957 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5958 {
5959 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5960 check_abstract_method_definitions (1, class_decl, super);
5961 }
5962}
5963
165f37bc
APB
5964/* Check all the types method DECL uses and return 1 if all of them
5965 are now complete, 0 otherwise. This is used to check whether its
5966 safe to build a method signature or not. */
5967
5968static int
5969check_method_types_complete (decl)
5970 tree decl;
5971{
5972 tree type = TREE_TYPE (decl);
5973 tree args;
5974
5975 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5976 return 0;
5977
5978 args = TYPE_ARG_TYPES (type);
5979 if (TREE_CODE (type) == METHOD_TYPE)
5980 args = TREE_CHAIN (args);
5981 for (; args != end_params_node; args = TREE_CHAIN (args))
5982 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5983 return 0;
5984
5985 return 1;
5986}
5987
34d4df06
APB
5988/* Visible interface to check methods contained in CLASS_DECL */
5989
5990void
5991java_check_methods (class_decl)
5992 tree class_decl;
5993{
5994 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5995 return;
5996
5997 if (CLASS_INTERFACE (class_decl))
5998 java_check_abstract_methods (class_decl);
5999 else
6000 java_check_regular_methods (class_decl);
6001
6002 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6003}
6004
d77613be
APB
6005/* Check all the methods of CLASS_DECL. Methods are first completed
6006 then checked according to regular method existance rules. If no
6007 constructor for CLASS_DECL were encountered, then build its
6008 declaration. */
e04a16fb
AG
6009
6010static void
6011java_check_regular_methods (class_decl)
6012 tree class_decl;
6013{
c2952b01 6014 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6015 tree method;
6016 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
c4faeb92 6017 tree found = NULL_TREE;
c877974e
APB
6018 tree mthrows;
6019
6020 /* It is not necessary to check methods defined in java.lang.Object */
6021 if (class == object_type_node)
6022 return;
e04a16fb 6023
23a79c61
APB
6024 if (!TYPE_NVIRTUALS (class))
6025 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6026
6027 /* Should take interfaces into account. FIXME */
6028 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6029 {
5e942c50 6030 tree sig;
c4faeb92 6031 tree method_wfl = DECL_FUNCTION_WFL (method);
e04a16fb
AG
6032 int aflags;
6033
e04a16fb
AG
6034 /* Check for redefinitions */
6035 if (check_method_redefinition (class, method))
6036 continue;
6037
22eed1e6
APB
6038 /* If we see one constructor a mark so we don't generate the
6039 default one. Also skip other verifications: constructors
6040 can't be inherited hence hiden or overriden */
6041 if (DECL_CONSTRUCTOR_P (method))
6042 {
6043 saw_constructor = 1;
6044 continue;
6045 }
6046
c877974e
APB
6047 /* We verify things thrown by the method. They must inherits from
6048 java.lang.Throwable */
6049 for (mthrows = DECL_FUNCTION_THROWS (method);
6050 mthrows; mthrows = TREE_CHAIN (mthrows))
6051 {
6052 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6053 parse_error_context
781b0558 6054 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6055 IDENTIFIER_POINTER
6056 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6057 }
6058
e04a16fb 6059 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6060 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6061
c2952b01
APB
6062 /* Inner class can't declare static methods */
6063 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6064 {
6065 char *t = xstrdup (lang_printable_name (class, 0));
6066 parse_error_context
6067 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6068 lang_printable_name (method, 0), t);
6069 free (t);
6070 }
6071
5e942c50 6072 /* Nothing overrides or it's a private method. */
aabd7048 6073 if (!found)
5e942c50 6074 continue;
aabd7048
PB
6075 if (METHOD_PRIVATE (found))
6076 {
6077 found = NULL_TREE;
6078 continue;
6079 }
5e942c50 6080
614eaae0
APB
6081 /* If `found' is declared in an interface, make sure the
6082 modifier matches. */
6083 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6084 && clinit_identifier_node != DECL_NAME (found)
6085 && !METHOD_PUBLIC (method))
6086 {
6087 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6088 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6089 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6090 lang_printable_name (method, 0),
6091 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6092 }
6093
e04a16fb
AG
6094 /* Can't override a method with the same name and different return
6095 types. */
6096 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6097 {
614eaae0
APB
6098 char *t = xstrdup
6099 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6100 parse_error_context
7f10c2e2 6101 (method_wfl,
b9f7e36c 6102 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6103 lang_printable_name (found, 0), t,
b9f7e36c
APB
6104 IDENTIFIER_POINTER
6105 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6106 free (t);
6107 }
e04a16fb 6108
7f10c2e2 6109 aflags = get_access_flags_from_decl (found);
7f10c2e2 6110
e04a16fb
AG
6111 /* Can't override final. Can't override static. */
6112 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6113 {
6114 /* Static *can* override static */
6115 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6116 continue;
6117 parse_error_context
6118 (method_wfl,
6119 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6120 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6121 lang_printable_name (found, 0),
e04a16fb
AG
6122 (METHOD_FINAL (found) ? "final" : "static"),
6123 IDENTIFIER_POINTER
6124 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6125 continue;
6126 }
7f10c2e2 6127
e04a16fb
AG
6128 /* Static method can't override instance method. */
6129 if (METHOD_STATIC (method))
6130 {
6131 parse_error_context
6132 (method_wfl,
781b0558 6133 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6134 lang_printable_name (found, 0),
e04a16fb
AG
6135 IDENTIFIER_POINTER
6136 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6137 continue;
6138 }
5e942c50 6139
5e942c50
APB
6140 /* - Overriding/hiding public must be public
6141 - Overriding/hiding protected must be protected or public
6142 - If the overriden or hidden method has default (package)
6143 access, then the overriding or hiding method must not be
614eaae0
APB
6144 private; otherwise, a compile-time error occurs. If
6145 `found' belongs to an interface, things have been already
6146 taken care of. */
6147 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6148 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6149 || (METHOD_PROTECTED (found)
6150 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6151 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6152 && METHOD_PRIVATE (method))))
e04a16fb
AG
6153 {
6154 parse_error_context
6155 (method_wfl,
781b0558 6156 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6157 (METHOD_PUBLIC (method) ? "public" :
6158 (METHOD_PRIVATE (method) ? "private" : "protected")),
6159 IDENTIFIER_POINTER (DECL_NAME
6160 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6161 continue;
6162 }
6163
b9f7e36c
APB
6164 /* Overriding methods must have compatible `throws' clauses on checked
6165 exceptions, if any */
6166 check_throws_clauses (method, method_wfl, found);
6167
e04a16fb
AG
6168 /* Inheriting multiple methods with the same signature. FIXME */
6169 }
6170
23a79c61
APB
6171 if (!TYPE_NVIRTUALS (class))
6172 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6173
d77613be
APB
6174 /* Search for inherited abstract method not yet implemented in this
6175 class. */
6176 java_check_abstract_method_definitions (class_decl);
6177
22eed1e6 6178 if (!saw_constructor)
400500c4 6179 abort ();
e04a16fb
AG
6180}
6181
b9f7e36c
APB
6182/* Return a non zero value if the `throws' clause of METHOD (if any)
6183 is incompatible with the `throws' clause of FOUND (if any). */
6184
6185static void
6186check_throws_clauses (method, method_wfl, found)
6187 tree method, method_wfl, found;
6188{
6189 tree mthrows, fthrows;
6190
c877974e
APB
6191 /* Can't check these things with class loaded from bytecode. FIXME */
6192 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6193 return;
6194
b9f7e36c
APB
6195 for (mthrows = DECL_FUNCTION_THROWS (method);
6196 mthrows; mthrows = TREE_CHAIN (mthrows))
6197 {
6198 /* We don't verify unchecked expressions */
c877974e 6199 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6200 continue;
6201 /* Checked expression must be compatible */
6202 for (fthrows = DECL_FUNCTION_THROWS (found);
6203 fthrows; fthrows = TREE_CHAIN (fthrows))
6204 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6205 break;
6206 if (!fthrows)
6207 {
6208 parse_error_context
781b0558 6209 (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 6210 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6211 lang_printable_name (found, 0),
b9f7e36c
APB
6212 IDENTIFIER_POINTER
6213 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6214 }
6215 }
6216}
6217
e04a16fb
AG
6218/* Check abstract method of interface INTERFACE */
6219
6220static void
5e942c50
APB
6221java_check_abstract_methods (interface_decl)
6222 tree interface_decl;
e04a16fb
AG
6223{
6224 int i, n;
6225 tree method, basetype_vec, found;
5e942c50 6226 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6227
6228 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6229 {
e04a16fb
AG
6230 /* 2- Check for double definition inside the defining interface */
6231 if (check_method_redefinition (interface, method))
6232 continue;
6233
6234 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6235 the thrown exceptions (FIXME) */
e04a16fb
AG
6236 found = lookup_java_interface_method2 (interface, method);
6237 if (found)
6238 {
5e942c50 6239 char *t;
c2e3db92 6240 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6241 parse_error_context
c4faeb92 6242 (DECL_FUNCTION_WFL (found),
5e942c50 6243 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6244 lang_printable_name (found, 0), t,
b9f7e36c
APB
6245 IDENTIFIER_POINTER
6246 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6247 free (t);
c63b98cd 6248 continue;
e04a16fb
AG
6249 }
6250 }
6251
6252 /* 4- Inherited methods can't differ by their returned types */
6253 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6254 return;
6255 n = TREE_VEC_LENGTH (basetype_vec);
6256 for (i = 0; i < n; i++)
6257 {
6258 tree sub_interface_method, sub_interface;
6259 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6260 if (!vec_elt)
6261 continue;
6262 sub_interface = BINFO_TYPE (vec_elt);
6263 for (sub_interface_method = TYPE_METHODS (sub_interface);
6264 sub_interface_method;
6265 sub_interface_method = TREE_CHAIN (sub_interface_method))
6266 {
6267 found = lookup_java_interface_method2 (interface,
6268 sub_interface_method);
6269 if (found && (found != sub_interface_method))
5e942c50 6270 {
5e942c50
APB
6271 parse_error_context
6272 (lookup_cl (sub_interface_method),
781b0558 6273 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6274 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6275 lang_printable_name (found, 0),
6276 IDENTIFIER_POINTER
6277 (DECL_NAME (TYPE_NAME
6278 (DECL_CONTEXT (sub_interface_method)))),
6279 IDENTIFIER_POINTER
6280 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5e942c50 6281 }
e04a16fb
AG
6282 }
6283 }
6284}
6285
e04a16fb
AG
6286/* Lookup methods in interfaces using their name and partial
6287 signature. Return a matching method only if their types differ. */
6288
6289static tree
6290lookup_java_interface_method2 (class, method_decl)
6291 tree class, method_decl;
6292{
6293 int i, n;
6294 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6295
6296 if (!basetype_vec)
6297 return NULL_TREE;
6298
6299 n = TREE_VEC_LENGTH (basetype_vec);
6300 for (i = 0; i < n; i++)
6301 {
6302 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6303 if ((BINFO_TYPE (vec_elt) != object_type_node)
6304 && (to_return =
6305 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6306 return to_return;
6307 }
6308 for (i = 0; i < n; i++)
6309 {
6310 to_return = lookup_java_interface_method2
6311 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6312 if (to_return)
6313 return to_return;
6314 }
6315
6316 return NULL_TREE;
6317}
6318
6319/* Lookup method using their name and partial signature. Return a
6320 matching method only if their types differ. */
6321
6322static tree
6323lookup_java_method2 (clas, method_decl, do_interface)
6324 tree clas, method_decl;
6325 int do_interface;
6326{
5e942c50
APB
6327 tree method, method_signature, method_name, method_type, name;
6328
e04a16fb 6329 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6330 name = DECL_NAME (method_decl);
6331 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6332 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6333 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6334
6335 while (clas != NULL_TREE)
6336 {
6337 for (method = TYPE_METHODS (clas);
6338 method != NULL_TREE; method = TREE_CHAIN (method))
6339 {
6340 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6341 tree name = DECL_NAME (method);
6342 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6343 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6344 && method_sig == method_signature
6345 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6346 return method;
e04a16fb
AG
6347 }
6348 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6349 }
6350 return NULL_TREE;
6351}
6352
f441f671
APB
6353/* Return the line that matches DECL line number, and try its best to
6354 position the column number. Used during error reports. */
e04a16fb
AG
6355
6356static tree
6357lookup_cl (decl)
6358 tree decl;
6359{
6360 static tree cl = NULL_TREE;
f441f671 6361 char *line, *found;
e04a16fb
AG
6362
6363 if (!decl)
6364 return NULL_TREE;
6365
6366 if (cl == NULL_TREE)
19e223db
MM
6367 {
6368 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6369 ggc_add_tree_root (&cl, 1);
6370 }
e04a16fb
AG
6371
6372 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6373 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6374
dba41d30 6375 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
f441f671
APB
6376 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6377
6378 found = strstr ((const char *)line,
6379 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6380 if (found)
6381 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6382
e04a16fb
AG
6383 return cl;
6384}
6385
6386/* Look for a simple name in the single-type import list */
6387
6388static tree
6389find_name_in_single_imports (name)
6390 tree name;
6391{
6392 tree node;
6393
6394 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6395 if (TREE_VALUE (node) == name)
6396 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6397
6398 return NULL_TREE;
6399}
6400
6401/* Process all single-type import. */
6402
6403static int
6404process_imports ()
6405{
6406 tree import;
6407 int error_found;
6408
6409 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6410 {
6411 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
1ebb5e73
APB
6412 char *original_name;
6413
6414 obstack_grow0 (&temporary_obstack,
6415 IDENTIFIER_POINTER (to_be_found),
6416 IDENTIFIER_LENGTH (to_be_found));
6417 original_name = obstack_finish (&temporary_obstack);
e04a16fb
AG
6418
6419 /* Don't load twice something already defined. */
6420 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6421 continue;
02ae6e2e
APB
6422
6423 while (1)
6424 {
6425 tree left;
6426
6427 QUALIFIED_P (to_be_found) = 1;
6428 load_class (to_be_found, 0);
6429 error_found =
6430 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6431
6432 /* We found it, we can bail out */
6433 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6434 break;
6435
6436 /* We haven't found it. Maybe we're trying to access an
6437 inner class. The only way for us to know is to try again
6438 after having dropped a qualifier. If we can't break it further,
6439 we have an error. */
6440 if (breakdown_qualified (&left, NULL, to_be_found))
6441 break;
6442
6443 to_be_found = left;
6444 }
e04a16fb
AG
6445 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6446 {
6447 parse_error_context (TREE_PURPOSE (import),
6448 "Class or interface `%s' not found in import",
1ebb5e73
APB
6449 original_name);
6450 error_found = 1;
e04a16fb 6451 }
1ebb5e73
APB
6452
6453 obstack_free (&temporary_obstack, original_name);
e04a16fb
AG
6454 if (error_found)
6455 return 1;
6456 }
6457 return 0;
6458}
6459
9a7ab4b3
APB
6460/* Possibly find and mark a class imported by a single-type import
6461 statement. */
e04a16fb 6462
9a7ab4b3 6463static void
e04a16fb
AG
6464find_in_imports (class_type)
6465 tree class_type;
6466{
6467 tree import;
6468
6469 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6470 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6471 {
6472 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6473 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6474 }
e04a16fb
AG
6475}
6476
e04a16fb 6477static int
63a212ed 6478note_possible_classname (name, len)
49f48c71 6479 const char *name;
63a212ed 6480 int len;
e04a16fb 6481{
63a212ed
PB
6482 tree node;
6483 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6484 len = len - 5;
6485 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6486 len = len - 6;
e04a16fb 6487 else
63a212ed
PB
6488 return 0;
6489 node = ident_subst (name, len, "", '/', '.', "");
6490 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6491 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6492 return 1;
e04a16fb
AG
6493}
6494
6495/* Read a import directory, gathering potential match for further type
6496 references. Indifferently reads a filesystem or a ZIP archive
6497 directory. */
6498
6499static void
6500read_import_dir (wfl)
6501 tree wfl;
6502{
63a212ed 6503 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6504 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6505 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6506 DIR *dirp = NULL;
d8fccff5 6507 JCF *saved_jcf = current_jcf;
e04a16fb 6508
63a212ed
PB
6509 int found = 0;
6510 int k;
6511 void *entry;
6512 struct buffer filename[1];
6513
6514
6515 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6516 return;
6517 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6518
6519 BUFFER_INIT (filename);
6520 buffer_grow (filename, package_length + 100);
6521
6522 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6523 {
49f48c71 6524 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6525 int entry_length = strlen (entry_name);
6526 if (jcf_path_is_zipfile (entry))
6527 {
6528 ZipFile *zipf;
6529 buffer_grow (filename, entry_length);
6530 memcpy (filename->data, entry_name, entry_length - 1);
6531 filename->data[entry_length-1] = '\0';
6532 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6533 if (zipf == NULL)
6534 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6535 else
6536 {
6537 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6538 BUFFER_RESET (filename);
6539 for (k = 0; k < package_length; k++)
6540 {
6541 char ch = package_name[k];
6542 *filename->ptr++ = ch == '.' ? '/' : ch;
6543 }
6544 *filename->ptr++ = '/';
6545
345137c7 6546 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6547 {
49f48c71 6548 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6549 int current_entry_len = zipd->filename_length;
6550
345137c7
TT
6551 if (current_entry_len >= BUFFER_LENGTH (filename)
6552 && strncmp (filename->data, current_entry,
6553 BUFFER_LENGTH (filename)) != 0)
63a212ed 6554 continue;
345137c7 6555 found |= note_possible_classname (current_entry,
63a212ed
PB
6556 current_entry_len);
6557 }
6558 }
6559 }
6560 else
6561 {
6562 BUFFER_RESET (filename);
6563 buffer_grow (filename, entry_length + package_length + 4);
6564 strcpy (filename->data, entry_name);
6565 filename->ptr = filename->data + entry_length;
6566 for (k = 0; k < package_length; k++)
6567 {
6568 char ch = package_name[k];
6569 *filename->ptr++ = ch == '.' ? '/' : ch;
6570 }
6571 *filename->ptr = '\0';
6572
6573 dirp = opendir (filename->data);
6574 if (dirp == NULL)
6575 continue;
6576 *filename->ptr++ = '/';
6577 for (;;)
6578 {
63a212ed 6579 int len;
49f48c71 6580 const char *d_name;
63a212ed
PB
6581 struct dirent *direntp = readdir (dirp);
6582 if (!direntp)
6583 break;
6584 d_name = direntp->d_name;
6585 len = strlen (direntp->d_name);
6586 buffer_grow (filename, len+1);
6587 strcpy (filename->ptr, d_name);
345137c7 6588 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6589 package_length+len+1);
6590 }
6591 if (dirp)
6592 closedir (dirp);
6593 }
6594 }
e04a16fb 6595
63a212ed 6596 free (filename->data);
e04a16fb 6597
63a212ed
PB
6598 /* Here we should have a unified way of retrieving an entry, to be
6599 indexed. */
6600 if (!found)
e04a16fb
AG
6601 {
6602 static int first = 1;
6603 if (first)
6604 {
781b0558 6605 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6606 java_error_count++;
6607 first = 0;
6608 }
6609 else
63a212ed
PB
6610 parse_error_context (wfl, "Package `%s' not found in import",
6611 package_name);
e04a16fb
AG
6612 current_jcf = saved_jcf;
6613 return;
6614 }
e04a16fb
AG
6615 current_jcf = saved_jcf;
6616}
6617
6618/* Possibly find a type in the import on demands specified
6619 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6620 entire list, to detected potential double definitions. */
6621
6622static int
6623find_in_imports_on_demand (class_type)
6624 tree class_type;
6625{
ab3a6dd6 6626 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6627 int seen_once = -1;
ab3a6dd6 6628 tree cl = NULL_TREE;
e04a16fb
AG
6629
6630 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6631 {
49f48c71 6632 const char *id_name;
e04a16fb
AG
6633 obstack_grow (&temporary_obstack,
6634 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6635 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6636 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6637 obstack_grow0 (&temporary_obstack,
6638 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6639 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6640 id_name = obstack_finish (&temporary_obstack);
6641
6642 node = maybe_get_identifier (id_name);
6643 if (node && IS_A_CLASSFILE_NAME (node))
6644 {
6645 if (seen_once < 0)
6646 {
6647 cl = TREE_PURPOSE (import);
6648 seen_once = 1;
6649 node_to_use = node;
6650 }
6651 else
6652 {
6653 seen_once++;
6654 parse_error_context
1e12ab9b
APB
6655 (TREE_PURPOSE (import),
6656 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6657 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6658 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6659 }
6660 }
6661 }
6662
6663 if (seen_once == 1)
6664 {
6665 /* Setup lineno so that it refers to the line of the import (in
6666 case we parse a class file and encounter errors */
6667 tree decl;
6668 int saved_lineno = lineno;
6669 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6670 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6671 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6672 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6673 /* If there is no DECL set for the class or if the class isn't
6674 loaded and not seen in source yet, the load */
6675 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6676 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
a648f4e4
BM
6677 {
6678 load_class (node_to_use, 0);
6679 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6680 }
e04a16fb 6681 lineno = saved_lineno;
a648f4e4
BM
6682 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6683 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6684 else
6685 /* 6.6.1: Inner classes are subject to member access rules. */
6686 return 0;
e04a16fb
AG
6687 }
6688 else
6689 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6690}
6691
9a7ab4b3
APB
6692/* Add package NAME to the list of package encountered so far. To
6693 speed up class lookup in do_resolve_class, we make sure a
6694 particular package is added only once. */
6695
6696static void
6697register_package (name)
6698 tree name;
6699{
6700 static struct hash_table _pht, *pht = NULL;
6701
6702 if (!pht)
6703 {
6704 hash_table_init (&_pht, hash_newfunc,
6705 java_hash_hash_tree_node, java_hash_compare_tree_node);
6706 pht = &_pht;
6707 }
6708
6709 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6710 {
6711 package_list = chainon (package_list, build_tree_list (name, NULL));
6712 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6713 }
6714}
6715
5e942c50
APB
6716static tree
6717resolve_package (pkg, next)
6718 tree pkg, *next;
6719{
c2952b01 6720 tree current, acc;
5e942c50 6721 tree type_name = NULL_TREE;
49f48c71 6722 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6723
6724 /* The trick is to determine when the package name stops and were
6725 the name of something contained in the package starts. Then we
6726 return a fully qualified name of what we want to get. */
6727
6728 /* Do a quick search on well known package names */
6729 if (!strncmp (name, "java.lang.reflect", 17))
6730 {
6731 *next =
6732 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6733 type_name = lookup_package_type (name, 17);
6734 }
6735 else if (!strncmp (name, "java.lang", 9))
6736 {
6737 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6738 type_name = lookup_package_type (name, 9);
6739 }
5e942c50 6740
2c56429a
APB
6741 /* If we found something here, return */
6742 if (type_name)
6743 return type_name;
6744
6745 *next = EXPR_WFL_QUALIFICATION (pkg);
6746
c2952b01
APB
6747 /* Try to progressively construct a type name */
6748 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6749 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6750 current; current = TREE_CHAIN (current))
6751 {
6752 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6753 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6754 {
6755 type_name = acc;
6b48deee
APB
6756 /* resolve_package should be used in a loop, hence we
6757 point at this one to naturally process the next one at
6758 the next iteration. */
6759 *next = current;
c2952b01
APB
6760 break;
6761 }
6762 }
2c56429a
APB
6763 return type_name;
6764}
6765
5e942c50
APB
6766static tree
6767lookup_package_type (name, from)
49f48c71 6768 const char *name;
5e942c50
APB
6769 int from;
6770{
6771 char subname [128];
49f48c71 6772 const char *sub = &name[from+1];
5e942c50
APB
6773 while (*sub != '.' && *sub)
6774 sub++;
6775 strncpy (subname, name, sub-name);
6776 subname [sub-name] = '\0';
6777 return get_identifier (subname);
6778}
6779
a648f4e4
BM
6780/* Check accessibility of inner classes according to member access rules.
6781 DECL is the inner class, ENCLOSING_DECL is the class from which the
6782 access is being attempted. */
6783
cf1748bf 6784static void
4dbf4496
APB
6785check_inner_class_access (decl, enclosing_decl, cl)
6786 tree decl, enclosing_decl, cl;
cf1748bf 6787{
a648f4e4 6788 const char *access;
064a552c 6789 tree enclosing_decl_type;
4dbf4496 6790
cf1748bf 6791 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6792 as a result of processing a JDEP crafted by source_start_java_method
6793 for the purpose of patching its parm decl. But the error would
6794 have been already trapped when fixing the method's signature.
6795 DECL can also be NULL in case of earlier errors. */
6796 if (!decl || !cl)
cf1748bf
APB
6797 return;
6798
064a552c 6799 enclosing_decl_type = TREE_TYPE (enclosing_decl);
a648f4e4 6800
4dbf4496 6801 if (CLASS_PRIVATE (decl))
a648f4e4
BM
6802 {
6803 /* Access is permitted only within the body of the top-level
6804 class in which DECL is declared. */
6805 tree top_level = decl;
6806 while (DECL_CONTEXT (top_level))
6807 top_level = DECL_CONTEXT (top_level);
6808 while (DECL_CONTEXT (enclosing_decl))
6809 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6810 if (top_level == enclosing_decl)
6811 return;
6812 access = "private";
6813 }
6814 else if (CLASS_PROTECTED (decl))
6815 {
6816 tree decl_context;
6817 /* Access is permitted from within the same package... */
6818 if (in_same_package (decl, enclosing_decl))
6819 return;
4dbf4496 6820
a648f4e4
BM
6821 /* ... or from within the body of a subtype of the context in which
6822 DECL is declared. */
6823 decl_context = DECL_CONTEXT (decl);
6824 while (enclosing_decl)
6825 {
6826 if (CLASS_INTERFACE (decl))
6827 {
6828 if (interface_of_p (TREE_TYPE (decl_context),
6829 enclosing_decl_type))
6830 return;
6831 }
6832 else
6833 {
6834 /* Eww. The order of the arguments is different!! */
6835 if (inherits_from_p (enclosing_decl_type,
6836 TREE_TYPE (decl_context)))
6837 return;
6838 }
6839 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6840 }
6841 access = "protected";
6842 }
6843 else if (! CLASS_PUBLIC (decl))
6844 {
6845 /* Access is permitted only from within the same package as DECL. */
6846 if (in_same_package (decl, enclosing_decl))
6847 return;
6848 access = "non-public";
6849 }
6850 else
6851 /* Class is public. */
4dbf4496
APB
6852 return;
6853
a648f4e4 6854 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
cf1748bf 6855 (CLASS_INTERFACE (decl) ? "interface" : "class"),
a648f4e4 6856 lang_printable_name (decl, 0), access);
cf1748bf
APB
6857}
6858
a648f4e4
BM
6859/* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6860 package, it must be PUBLIC. Return 0 if no access violations were found,
6861 1 otherwise. */
e04a16fb
AG
6862
6863static int
6864check_pkg_class_access (class_name, cl)
6865 tree class_name;
6866 tree cl;
6867{
6868 tree type;
e04a16fb 6869
a648f4e4 6870 if (!IDENTIFIER_CLASS_VALUE (class_name))
e04a16fb
AG
6871 return 0;
6872
6873 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6874 return 0;
6875
6876 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6877 {
e28cd97b
APB
6878 /* Access to a private class within the same package is
6879 allowed. */
6880 tree l, r;
6881 breakdown_qualified (&l, &r, class_name);
a648f4e4
BM
6882 if (!QUALIFIED_P (class_name) && !ctxp->package)
6883 /* Both in the empty package. */
6884 return 0;
e28cd97b 6885 if (l == ctxp->package)
a648f4e4 6886 /* Both in the same package. */
e28cd97b
APB
6887 return 0;
6888
e04a16fb 6889 parse_error_context
781b0558 6890 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6891 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6892 IDENTIFIER_POINTER (class_name));
6893 return 1;
6894 }
6895 return 0;
6896}
6897
6898/* Local variable declaration. */
6899
6900static void
6901declare_local_variables (modifier, type, vlist)
6902 int modifier;
6903 tree type;
6904 tree vlist;
6905{
c583dd46
APB
6906 tree decl, current, saved_type;
6907 tree type_wfl = NULL_TREE;
e04a16fb 6908 int must_chain = 0;
c2952b01 6909 int final_p = 0;
e04a16fb 6910
2aa11e97 6911 /* Push a new block if statements were seen between the last time we
e04a16fb 6912 pushed a block and now. Keep a cound of block to close */
f099f336 6913 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6914 {
f099f336 6915 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6916 tree b = enter_block ();
f099f336 6917 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6918 }
6919
6920 if (modifier)
6921 {
6922 int i;
6923 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6924 if (modifier == ACC_FINAL)
c2952b01 6925 final_p = 1;
c877974e
APB
6926 else
6927 {
6928 parse_error_context
6929 (ctxp->modifier_ctx [i],
6930 "Only `final' is allowed as a local variables modifier");
6931 return;
6932 }
e04a16fb
AG
6933 }
6934
c583dd46
APB
6935 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6936 hold the TYPE value if a new incomplete has to be created (as
6937 opposed to being found already existing and reused). */
6938 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6939
6940 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6941 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6942
6943 /* Go through all the declared variables */
6944 for (current = vlist, saved_type = type; current;
6945 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6946 {
c877974e 6947 tree other, real_type;
e04a16fb
AG
6948 tree wfl = TREE_PURPOSE (current);
6949 tree name = EXPR_WFL_NODE (wfl);
6950 tree init = TREE_VALUE (current);
e04a16fb 6951
c583dd46
APB
6952 /* Process NAME, as it may specify extra dimension(s) for it */
6953 type = build_array_from_name (type, type_wfl, name, &name);
6954
6955 /* Variable redefinition check */
6956 if ((other = lookup_name_in_blocks (name)))
6957 {
6958 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6959 DECL_SOURCE_LINE (other));
6960 continue;
6961 }
6962
6963 /* Type adjustment. We may have just readjusted TYPE because
6964 the variable specified more dimensions. Make sure we have
6965 a reference if we can and don't have one already. */
1886c9d8 6966 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6967
6968 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6969 /* Never layout this decl. This will be done when its scope
6970 will be entered */
c877974e 6971 decl = build_decl (VAR_DECL, name, real_type);
c7303e41 6972 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
c2952b01 6973 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6974 BLOCK_CHAIN_DECL (decl);
6975
d4370213
APB
6976 /* If doing xreferencing, replace the line number with the WFL
6977 compound value */
6978 if (flag_emit_xref)
6979 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6980
e04a16fb
AG
6981 /* Don't try to use an INIT statement when an error was found */
6982 if (init && java_error_count)
6983 init = NULL_TREE;
c583dd46
APB
6984
6985 /* Add the initialization function to the current function's code */
6986 if (init)
e04a16fb 6987 {
c583dd46
APB
6988 /* Name might have been readjusted */
6989 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6990 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6991 java_method_add_stmt (current_function_decl,
6992 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6993 init));
6994 }
6995
6996 /* Setup dependency the type of the decl */
6997 if (must_chain)
6998 {
6999 jdep *dep;
7000 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7001 dep = CLASSD_LAST (ctxp->classd_list);
7002 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7003 }
7004 }
7005 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7006}
7007
7008/* Called during parsing. Build decls from argument list. */
7009
7010static void
7011source_start_java_method (fndecl)
7012 tree fndecl;
7013{
7014 tree tem;
7015 tree parm_decl;
7016 int i;
7017
79d13333
APB
7018 if (!fndecl)
7019 return;
7020
e04a16fb
AG
7021 current_function_decl = fndecl;
7022
7023 /* New scope for the function */
7024 enter_block ();
7025 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7026 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7027 {
7028 tree type = TREE_VALUE (tem);
7029 tree name = TREE_PURPOSE (tem);
7030
23a79c61
APB
7031 /* If type is incomplete. Create an incomplete decl and ask for
7032 the decl to be patched later */
e04a16fb
AG
7033 if (INCOMPLETE_TYPE_P (type))
7034 {
7035 jdep *jdep;
c877974e
APB
7036 tree real_type = GET_REAL_TYPE (type);
7037 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7038 type = obtain_incomplete_type (type);
e04a16fb
AG
7039 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7040 jdep = CLASSD_LAST (ctxp->classd_list);
7041 JDEP_MISC (jdep) = name;
7042 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7043 }
7044 else
7045 parm_decl = build_decl (PARM_DECL, name, type);
7046
c2952b01
APB
7047 /* Remember if a local variable was declared final (via its
7048 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7049 if (ARG_FINAL_P (tem))
c7303e41
APB
7050 {
7051 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7052 LOCAL_FINAL (parm_decl) = 1;
7053 }
c2952b01 7054
e04a16fb
AG
7055 BLOCK_CHAIN_DECL (parm_decl);
7056 }
7057 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7058 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7059 nreverse (tem);
7060 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7061 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7062}
7063
22eed1e6
APB
7064/* Called during parsing. Creates an artificial method declaration. */
7065
7066static tree
7067create_artificial_method (class, flags, type, name, args)
7068 tree class;
7069 int flags;
7070 tree type, name, args;
7071{
22eed1e6
APB
7072 tree mdecl;
7073
c2952b01 7074 java_parser_context_save_global ();
22eed1e6
APB
7075 lineno = 0;
7076 mdecl = make_node (FUNCTION_TYPE);
7077 TREE_TYPE (mdecl) = type;
7078 TYPE_ARG_TYPES (mdecl) = args;
7079 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7080 java_parser_context_restore_global ();
22eed1e6
APB
7081 DECL_ARTIFICIAL (mdecl) = 1;
7082 return mdecl;
7083}
7084
7085/* Starts the body if an artifical method. */
7086
7087static void
7088start_artificial_method_body (mdecl)
7089 tree mdecl;
7090{
7091 DECL_SOURCE_LINE (mdecl) = 1;
7092 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7093 source_start_java_method (mdecl);
7094 enter_block ();
7095}
7096
7097static void
7098end_artificial_method_body (mdecl)
7099 tree mdecl;
7100{
1e0cdc10
APB
7101 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7102 It has to be evaluated first. (if mdecl is current_function_decl,
7103 we have an undefined behavior if no temporary variable is used.) */
7104 tree b = exit_block ();
7105 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
22eed1e6
APB
7106 exit_block ();
7107}
7108
e04a16fb
AG
7109/* Terminate a function and expand its body. */
7110
7111static void
7112source_end_java_method ()
7113{
7114 tree fndecl = current_function_decl;
138657ec 7115 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7116
79d13333
APB
7117 if (!fndecl)
7118 return;
7119
e04a16fb
AG
7120 java_parser_context_save_global ();
7121 lineno = ctxp->last_ccb_indent1;
7122
b67d701b
PB
7123 /* Set EH language codes */
7124 java_set_exception_lang_code ();
7125
5423609c
APB
7126 /* Turn function bodies with only a NOP expr null, so they don't get
7127 generated at all and we won't get warnings when using the -W
7128 -Wall flags. */
7129 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7130 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7131
e04a16fb
AG
7132 /* Generate function's code */
7133 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7134 && ! flag_emit_class_files
7135 && ! flag_emit_xref)
e04a16fb
AG
7136 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7137
7138 /* pop out of its parameters */
7139 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7140 poplevel (1, 0, 1);
7141 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7142
7143 /* Generate rtl for function exit. */
e8fc7396 7144 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7145 {
7146 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7147 /* Emit catch-finally clauses */
7148 emit_handlers ();
e04a16fb
AG
7149 expand_function_end (input_filename, lineno, 0);
7150
138657ec
AH
7151 /* FIXME: If the current method contains any exception handlers,
7152 force asynchronous_exceptions: this is necessary because signal
7153 handlers in libjava may throw exceptions. This is far from being
7154 a perfect solution, but it's better than doing nothing at all.*/
7155 if (catch_clauses)
7156 asynchronous_exceptions = 1;
7157
e04a16fb
AG
7158 /* Run the optimizers and output assembler code for this function. */
7159 rest_of_compilation (fndecl);
7160 }
7161
7162 current_function_decl = NULL_TREE;
e04a16fb 7163 java_parser_context_restore_global ();
138657ec 7164 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7165}
7166
7167/* Record EXPR in the current function block. Complements compound
7168 expression second operand if necessary. */
7169
7170tree
7171java_method_add_stmt (fndecl, expr)
7172 tree fndecl, expr;
7173{
b771925e
APB
7174 if (!GET_CURRENT_BLOCK (fndecl))
7175 return NULL_TREE;
f099f336 7176 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7177}
e04a16fb 7178
b67d701b
PB
7179static tree
7180add_stmt_to_block (b, type, stmt)
7181 tree b, type, stmt;
7182{
7183 tree body = BLOCK_EXPR_BODY (b), c;
7184
e04a16fb
AG
7185 if (java_error_count)
7186 return body;
b67d701b
PB
7187
7188 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7189 return body;
7190
b67d701b
PB
7191 BLOCK_EXPR_BODY (b) = c;
7192 TREE_SIDE_EFFECTS (c) = 1;
7193 return c;
e04a16fb
AG
7194}
7195
7196/* Add STMT to EXISTING if possible, otherwise create a new
7197 COMPOUND_EXPR and add STMT to it. */
7198
7199static tree
7200add_stmt_to_compound (existing, type, stmt)
7201 tree existing, type, stmt;
7202{
15fdcfe9
PB
7203 if (existing)
7204 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7205 else
15fdcfe9 7206 return stmt;
e04a16fb
AG
7207}
7208
1886c9d8
APB
7209void java_layout_seen_class_methods ()
7210{
7211 tree previous_list = all_class_list;
7212 tree end = NULL_TREE;
7213 tree current;
7214
7215 while (1)
7216 {
7217 for (current = previous_list;
7218 current != end; current = TREE_CHAIN (current))
7219 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7220
7221 if (previous_list != all_class_list)
7222 {
7223 end = previous_list;
7224 previous_list = all_class_list;
7225 }
7226 else
7227 break;
7228 }
7229}
7230
e04a16fb 7231void
c2952b01 7232java_reorder_fields ()
e04a16fb 7233{
c2952b01 7234 static tree stop_reordering = NULL_TREE;
19e223db 7235 static int initialized_p;
c2952b01 7236 tree current;
19e223db
MM
7237
7238 /* Register STOP_REORDERING with the garbage collector. */
7239 if (!initialized_p)
7240 {
7241 ggc_add_tree_root (&stop_reordering, 1);
7242 initialized_p = 1;
7243 }
7244
fea2d5da 7245 for (current = gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7246 {
5e942c50 7247 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7248
c2952b01
APB
7249 if (current_class == stop_reordering)
7250 break;
7251
c877974e
APB
7252 /* Reverse the fields, but leave the dummy field in front.
7253 Fields are already ordered for Object and Class */
7254 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7255 && current_class != class_type_node)
7256 {
23a79c61
APB
7257 /* If the dummy field is there, reverse the right fields and
7258 just layout the type for proper fields offset */
c877974e
APB
7259 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7260 {
7261 tree fields = TYPE_FIELDS (current_class);
7262 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7263 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7264 }
23a79c61
APB
7265 /* We don't have a dummy field, we need to layout the class,
7266 after having reversed the fields */
c877974e
APB
7267 else
7268 {
7269 TYPE_FIELDS (current_class) =
7270 nreverse (TYPE_FIELDS (current_class));
7271 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7272 }
7273 }
c2952b01 7274 }
fea2d5da 7275 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
c2952b01
APB
7276}
7277
fea2d5da
PB
7278/* Layout the methods of all classes loaded in one way or another.
7279 Check methods of source parsed classes. Then reorder the
c2952b01
APB
7280 fields and layout the classes or the type of all source parsed
7281 classes */
7282
7283void
7284java_layout_classes ()
7285{
7286 tree current;
7287 int save_error_count = java_error_count;
7288
7289 /* Layout the methods of all classes seen so far */
7290 java_layout_seen_class_methods ();
7291 java_parse_abort_on_error ();
7292 all_class_list = NULL_TREE;
7293
7294 /* Then check the methods of all parsed classes */
fea2d5da 7295 for (current = gclass_list; current; current = TREE_CHAIN (current))
c2952b01 7296 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7297 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7298 java_parse_abort_on_error ();
7299
fea2d5da 7300 for (current = gclass_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7301 {
7302 current_class = TREE_TYPE (TREE_VALUE (current));
7303 layout_class (current_class);
5e942c50 7304
c877974e
APB
7305 /* From now on, the class is considered completely loaded */
7306 CLASS_LOADED_P (current_class) = 1;
7307
5e942c50
APB
7308 /* Error reported by the caller */
7309 if (java_error_count)
7310 return;
e04a16fb 7311 }
23a79c61
APB
7312
7313 /* We might have reloaded classes durign the process of laying out
7314 classes for code generation. We must layout the methods of those
7315 late additions, as constructor checks might use them */
1886c9d8 7316 java_layout_seen_class_methods ();
23a79c61 7317 java_parse_abort_on_error ();
e04a16fb
AG
7318}
7319
c2952b01
APB
7320/* Expand methods in the current set of classes rememebered for
7321 generation. */
e04a16fb 7322
49f48c71 7323static void
c2952b01 7324java_complete_expand_classes ()
e04a16fb
AG
7325{
7326 tree current;
ce6e9147
APB
7327
7328 do_not_fold = flag_emit_xref;
c2952b01 7329
e04a16fb 7330 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7331 if (!INNER_CLASS_DECL_P (current))
7332 java_complete_expand_class (current);
7333}
e04a16fb 7334
c2952b01
APB
7335/* Expand the methods found in OUTER, starting first by OUTER's inner
7336 classes, if any. */
e04a16fb 7337
c2952b01
APB
7338static void
7339java_complete_expand_class (outer)
7340 tree outer;
7341{
7342 tree inner_list;
e04a16fb 7343
c2952b01 7344 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7345
c2952b01
APB
7346 /* We need to go after all inner classes and start expanding them,
7347 starting with most nested ones. We have to do that because nested
7348 classes might add functions to outer classes */
e04a16fb 7349
c2952b01
APB
7350 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7351 inner_list; inner_list = TREE_CHAIN (inner_list))
7352 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7353
c2952b01
APB
7354 java_complete_expand_methods (outer);
7355 set_nested_class_simple_name_value (outer, 0); /* Reset */
7356}
7357
7358/* Expand methods registered in CLASS_DECL. The general idea is that
7359 we expand regular methods first. This allows us get an estimate on
7360 how outer context local alias fields are really used so we can add
7361 to the constructor just enough code to initialize them properly (it
c00f0fb2 7362 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7363 constructors and then <clinit>. */
7364
7365static void
7366java_complete_expand_methods (class_decl)
7367 tree class_decl;
7368{
7369 tree clinit, finit, decl, first_decl;
7370
7371 current_class = TREE_TYPE (class_decl);
7372
c7303e41
APB
7373 /* Find whether the class has final variables */
7374 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7375 if (FIELD_FINAL (decl))
7376 {
7377 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7378 break;
7379 }
7380
c2952b01
APB
7381 /* Initialize a new constant pool */
7382 init_outgoing_cpool ();
7383
7384 /* Pre-expand <clinit> to figure whether we really need it or
7385 not. If we do need it, we pre-expand the static fields so they're
7386 ready to be used somewhere else. <clinit> will be fully expanded
7387 after we processed the constructors. */
7388 first_decl = TYPE_METHODS (current_class);
7389 clinit = maybe_generate_pre_expand_clinit (current_class);
7390
c00f0fb2 7391 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7392 try to use it.*/
7393 if (TYPE_FINIT_STMT_LIST (current_class))
7394 {
7395 finit = generate_finit (current_class);
7396 java_complete_expand_method (finit);
7397 }
7398
7399 /* Now do the constructors */
7400 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7401 {
7402 int no_body;
7403
7404 if (!DECL_CONSTRUCTOR_P (decl))
7405 continue;
7406
7407 no_body = !DECL_FUNCTION_BODY (decl);
7408 /* Don't generate debug info on line zero when expanding a
7409 generated constructor. */
7410 if (no_body)
7411 restore_line_number_status (1);
7412
c7303e41
APB
7413 /* Reset the final local variable assignment flags */
7414 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7415 reset_final_variable_local_assignment_flag (current_class);
7416
c2952b01 7417 java_complete_expand_method (decl);
c7303e41
APB
7418
7419 /* Check for missed out final variable assignment */
7420 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7421 check_final_variable_local_assignment_flag (current_class, decl);
c2952b01
APB
7422
7423 if (no_body)
7424 restore_line_number_status (0);
7425 }
7426
7427 /* First, do the ordinary methods. */
7428 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7429 {
7145d9fe
TT
7430 /* Skip abstract or native methods -- but do handle native
7431 methods when generating JNI stubs. */
7432 if (METHOD_ABSTRACT (decl)
7433 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7434 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7435 continue;
7145d9fe
TT
7436
7437 if (METHOD_NATIVE (decl))
7438 {
7439 tree body = build_jni_stub (decl);
7440 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7441 }
7442
c2952b01
APB
7443 java_complete_expand_method (decl);
7444 }
7445
7446 /* If there is indeed a <clinit>, fully expand it now */
7447 if (clinit)
7448 {
c7303e41
APB
7449 /* Reset the final local variable assignment flags */
7450 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7451 reset_static_final_variable_assignment_flag (current_class);
c2952b01
APB
7452 /* Prevent the use of `this' inside <clinit> */
7453 ctxp->explicit_constructor_p = 1;
7454 java_complete_expand_method (clinit);
7455 ctxp->explicit_constructor_p = 0;
c7303e41
APB
7456 /* Check for missed out static final variable assignment */
7457 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7458 && !CLASS_INTERFACE (class_decl))
7459 check_static_final_variable_assignment_flag (current_class);
e04a16fb 7460 }
c2952b01 7461
165f37bc
APB
7462 /* We might have generated a class$ that we now want to expand */
7463 if (TYPE_DOT_CLASS (current_class))
7464 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7465
c2952b01
APB
7466 /* Now verify constructor circularity (stop after the first one we
7467 prove wrong.) */
7468 if (!CLASS_INTERFACE (class_decl))
7469 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7470 if (DECL_CONSTRUCTOR_P (decl)
7471 && verify_constructor_circularity (decl, decl))
7472 break;
7473
c7303e41
APB
7474 /* Final check on the initialization of final variables. */
7475 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7476 {
7477 check_final_variable_global_assignment_flag (current_class);
7478 /* If we have an interface, check for uninitialized fields. */
7479 if (CLASS_INTERFACE (class_decl))
7480 check_static_final_variable_assignment_flag (current_class);
7481 }
7482
c2952b01
APB
7483 /* Save the constant pool. We'll need to restore it later. */
7484 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7485}
7486
c2952b01
APB
7487/* Attempt to create <clinit>. Pre-expand static fields so they can be
7488 safely used in some other methods/constructors. */
e920ebc9 7489
c2952b01
APB
7490static tree
7491maybe_generate_pre_expand_clinit (class_type)
7492 tree class_type;
e920ebc9 7493{
c2952b01
APB
7494 tree current, mdecl;
7495
7496 if (!TYPE_CLINIT_STMT_LIST (class_type))
7497 return NULL_TREE;
e920ebc9 7498
c2952b01
APB
7499 /* Go through all static fields and pre expand them */
7500 for (current = TYPE_FIELDS (class_type); current;
7501 current = TREE_CHAIN (current))
7502 if (FIELD_STATIC (current))
7503 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7504
7505 /* Then build the <clinit> method */
7506 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7507 clinit_identifier_node, end_params_node);
7508 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7509 mdecl, NULL_TREE);
7510 start_artificial_method_body (mdecl);
7511
7512 /* We process the list of assignment we produced as the result of
7513 the declaration of initialized static field and add them as
7514 statement to the <clinit> method. */
7515 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7516 current = TREE_CHAIN (current))
e920ebc9 7517 {
9a7ab4b3 7518 tree stmt = current;
c2952b01
APB
7519 /* We build the assignment expression that will initialize the
7520 field to its value. There are strict rules on static
7521 initializers (8.5). FIXME */
98a52c2c 7522 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7523 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7524 java_method_add_stmt (mdecl, stmt);
7525 }
e920ebc9 7526
c2952b01
APB
7527 end_artificial_method_body (mdecl);
7528
92d83515
APB
7529 /* Now we want to place <clinit> as the last method (because we need
7530 it at least for interface so that it doesn't interfere with the
7531 dispatch table based lookup. */
7532 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7533 {
92d83515
APB
7534 current = TREE_CHAIN (TYPE_METHODS (class_type));
7535 TYPE_METHODS (class_type) = current;
c2952b01
APB
7536
7537 while (TREE_CHAIN (current))
7538 current = TREE_CHAIN (current);
92d83515 7539
c2952b01
APB
7540 TREE_CHAIN (current) = mdecl;
7541 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7542 }
c2952b01
APB
7543
7544 return mdecl;
e920ebc9
APB
7545}
7546
dba41d30
APB
7547/* Analyzes a method body and look for something that isn't a
7548 MODIFY_EXPR. */
7549
7550static int
7551analyze_clinit_body (bbody)
7552 tree bbody;
7553{
7554 while (bbody)
7555 switch (TREE_CODE (bbody))
7556 {
7557 case BLOCK:
7558 bbody = BLOCK_EXPR_BODY (bbody);
7559 break;
7560
7561 case EXPR_WITH_FILE_LOCATION:
7562 bbody = EXPR_WFL_NODE (bbody);
7563 break;
7564
7565 case COMPOUND_EXPR:
7566 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7567 return 1;
7568 bbody = TREE_OPERAND (bbody, 1);
7569 break;
7570
7571 case MODIFY_EXPR:
7572 bbody = NULL_TREE;
7573 break;
7574
7575 default:
7576 bbody = NULL_TREE;
7577 return 1;
7578 }
7579 return 0;
7580}
7581
7582
92d83515
APB
7583/* See whether we could get rid of <clinit>. Criteria are: all static
7584 final fields have constant initial values and the body of <clinit>
7585 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7586
7587static int
7588maybe_yank_clinit (mdecl)
7589 tree mdecl;
7590{
7591 tree type, current;
7592 tree fbody, bbody;
99eaf8d4 7593 int found = 0;
92d83515
APB
7594
7595 if (!DECL_CLINIT_P (mdecl))
7596 return 0;
f0f3a777
APB
7597
7598 /* If the body isn't empty, then we keep <clinit>. Note that if
7599 we're emitting classfiles, this isn't enough not to rule it
7600 out. */
92d83515 7601 fbody = DECL_FUNCTION_BODY (mdecl);
dba41d30
APB
7602 bbody = BLOCK_EXPR_BODY (fbody);
7603 if (bbody && bbody != error_mark_node)
92d83515 7604 bbody = BLOCK_EXPR_BODY (bbody);
dba41d30
APB
7605 else
7606 return 0;
f0f3a777 7607 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7608 return 0;
7609
7610 type = DECL_CONTEXT (mdecl);
7611 current = TYPE_FIELDS (type);
7612
7613 for (current = (current ? TREE_CHAIN (current) : current);
7614 current; current = TREE_CHAIN (current))
f0f3a777
APB
7615 {
7616 tree f_init;
7617
7618 /* We're not interested in non static field */
7619 if (!FIELD_STATIC (current))
7620 continue;
7621
7622 /* Anything that isn't String or a basic type is ruled out -- or
c7303e41 7623 if we know how to deal with it (when doing things natively) we
f0f3a777
APB
7624 should generated an empty <clinit> so that SUID are computed
7625 correctly. */
7626 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7627 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7628 break;
7629
7630 f_init = DECL_INITIAL (current);
7631 /* If we're emitting native code, we want static final fields to
7632 have constant initializers. If we don't meet these
7633 conditions, we keep <clinit> */
7634 if (!flag_emit_class_files
7635 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7636 break;
7637 /* If we're emitting bytecode, we want static fields to have
7638 constant initializers or no initializer. If we don't meet
7639 these conditions, we keep <clinit> */
7640 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7641 break;
7642 }
92d83515 7643
99eaf8d4
APB
7644 /* Now we analyze the method body and look for something that
7645 isn't a MODIFY_EXPR */
7646 if (bbody == empty_stmt_node)
dba41d30
APB
7647 found = 0;
7648 else
7649 found = analyze_clinit_body (bbody);
99eaf8d4
APB
7650
7651 if (current || found)
92d83515
APB
7652 return 0;
7653
7654 /* Get rid of <clinit> in the class' list of methods */
7655 if (TYPE_METHODS (type) == mdecl)
7656 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7657 else
7658 for (current = TYPE_METHODS (type); current;
7659 current = TREE_CHAIN (current))
7660 if (TREE_CHAIN (current) == mdecl)
7661 {
7662 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7663 break;
7664 }
7665
7666 return 1;
7667}
7668
7669
e04a16fb
AG
7670/* Complete and expand a method. */
7671
7672static void
7673java_complete_expand_method (mdecl)
7674 tree mdecl;
7675{
92d83515
APB
7676 int yank_clinit = 0;
7677
c2952b01 7678 current_function_decl = mdecl;
22eed1e6
APB
7679 /* Fix constructors before expanding them */
7680 if (DECL_CONSTRUCTOR_P (mdecl))
7681 fix_constructors (mdecl);
e04a16fb 7682
22eed1e6 7683 /* Expand functions that have a body */
e04a16fb
AG
7684 if (DECL_FUNCTION_BODY (mdecl))
7685 {
9bbc7d9f
PB
7686 tree fbody = DECL_FUNCTION_BODY (mdecl);
7687 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7688 tree exception_copy = NULL_TREE;
47a50de9
PB
7689 tree tem, *ptr;
7690
7691 current_function_decl = mdecl;
7692
7693 if (! quiet_flag)
7694 fprintf (stderr, " [%s.",
7695 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7696 announce_function (mdecl);
7697 if (! quiet_flag)
7698 fprintf (stderr, "]");
7699
7700 pushlevel (1); /* Prepare for a parameter push */
7701 ptr = &DECL_ARGUMENTS (mdecl);
7702 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7703 while (tem)
7704 {
7705 tree next = TREE_CHAIN (tem);
7706 tree type = TREE_TYPE (tem);
7707 if (PROMOTE_PROTOTYPES
7708 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7709 && INTEGRAL_TYPE_P (type))
7710 type = integer_type_node;
7711 DECL_ARG_TYPE (tem) = type;
7712 layout_decl (tem, 0);
7713 pushdecl (tem);
7714 *ptr = tem;
7715 ptr = &TREE_CHAIN (tem);
7716 tem = next;
7717 }
7718 *ptr = NULL_TREE;
7719 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7720 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7721
939d7216 7722 build_result_decl (mdecl);
e04a16fb
AG
7723
7724 current_this
7725 = (!METHOD_STATIC (mdecl) ?
7726 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7727
ce6e9147
APB
7728 /* Purge the `throws' list of unchecked exceptions. If we're
7729 doing xref, save a copy of the list and re-install it
7730 later. */
7731 if (flag_emit_xref)
7732 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7733
b9f7e36c
APB
7734 purge_unchecked_exceptions (mdecl);
7735
7736 /* Install exceptions thrown with `throws' */
7737 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7738
9bbc7d9f 7739 if (block_body != NULL_TREE)
bc3ca41b
PB
7740 {
7741 block_body = java_complete_tree (block_body);
c2952b01 7742
7145d9fe 7743 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7744 check_for_initialization (block_body);
f099f336 7745 ctxp->explicit_constructor_p = 0;
bc3ca41b 7746 }
e803d3b2 7747
9bbc7d9f 7748 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7749
c2952b01
APB
7750 /* If we saw a return but couldn't evaluate it properly, we'll
7751 have an error_mark_node here. */
7752 if (block_body != error_mark_node
7753 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7754 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7755 && !flag_emit_xref)
82371d41 7756 missing_return_error (current_function_decl);
7525cc04 7757
92d83515
APB
7758 /* Check wether we could just get rid of clinit, now the picture
7759 is complete. */
7760 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7761 complete_start_java_method (mdecl);
7762
e04a16fb 7763 /* Don't go any further if we've found error(s) during the
92d83515
APB
7764 expansion */
7765 if (!java_error_count && !yank_clinit)
e04a16fb 7766 source_end_java_method ();
22eed1e6
APB
7767 else
7768 {
92d83515
APB
7769 if (java_error_count)
7770 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7771 poplevel (1, 0, 1);
7772 }
b9f7e36c
APB
7773
7774 /* Pop the exceptions and sanity check */
7775 POP_EXCEPTIONS();
7776 if (currently_caught_type_list)
400500c4 7777 abort ();
ce6e9147
APB
7778
7779 if (flag_emit_xref)
7780 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7781 }
7782}
7783
c2952b01
APB
7784\f
7785
7786/* This section of the code deals with accessing enclosing context
7787 fields either directly by using the relevant access to this$<n> or
7788 by invoking an access method crafted for that purpose. */
7789
7790/* Build the necessary access from an inner class to an outer
7791 class. This routine could be optimized to cache previous result
7792 (decl, current_class and returned access). When an access method
7793 needs to be generated, it always takes the form of a read. It might
7794 be later turned into a write by calling outer_field_access_fix. */
7795
7796static tree
7797build_outer_field_access (id, decl)
7798 tree id, decl;
7799{
7800 tree access = NULL_TREE;
7801 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
5e18f6d6 7802 tree decl_ctx = DECL_CONTEXT (decl);
c2952b01 7803
5e18f6d6
APB
7804 /* If the immediate enclosing context of the current class is the
7805 field decl's class or inherits from it; build the access as
7806 `this$<n>.<field>'. Note that we will break the `private' barrier
7807 if we're not emitting bytecodes. */
7808 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
c2952b01
APB
7809 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7810 {
7811 tree thisn = build_current_thisn (current_class);
7812 access = make_qualified_primary (build_wfl_node (thisn),
7813 id, EXPR_WFL_LINECOL (id));
7814 }
7815 /* Otherwise, generate access methods to outer this and access the
7816 field (either using an access method or by direct access.) */
7817 else
7818 {
7819 int lc = EXPR_WFL_LINECOL (id);
7820
7821 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7822 get a hold to the enclosing instance we need, and then we
c2952b01 7823 build the field access. */
5e18f6d6 7824 access = build_access_to_thisn (current_class, decl_ctx, lc);
c2952b01
APB
7825
7826 /* If the field is private and we're generating bytecode, then
7827 we generate an access method */
7828 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7829 {
7830 tree name = build_outer_field_access_methods (decl);
5e18f6d6 7831 access = build_outer_field_access_expr (lc, decl_ctx,
c2952b01
APB
7832 name, access, NULL_TREE);
7833 }
7834 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7835 Once again we break the `private' access rule from a foreign
7836 class. */
7837 else
7838 access = make_qualified_primary (access, id, lc);
7839 }
7840 return resolve_expression_name (access, NULL);
7841}
7842
7843/* Return a non zero value if NODE describes an outer field inner
7844 access. */
7845
7846static int
7847outer_field_access_p (type, decl)
7848 tree type, decl;
7849{
7850 if (!INNER_CLASS_TYPE_P (type)
7851 || TREE_CODE (decl) != FIELD_DECL
7852 || DECL_CONTEXT (decl) == type)
7853 return 0;
ee5f86dc
APB
7854
7855 /* If the inner class extends the declaration context of the field
7856 we're try to acces, then this isn't an outer field access */
7857 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7858 return 0;
c2952b01
APB
7859
7860 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7861 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7862 {
7863 if (type == DECL_CONTEXT (decl))
7864 return 1;
5e18f6d6 7865
c2952b01 7866 if (!DECL_CONTEXT (TYPE_NAME (type)))
5e18f6d6
APB
7867 {
7868 /* Before we give up, see whether the field is inherited from
7869 the enclosing context we're considering. */
7870 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7871 return 1;
7872 break;
7873 }
c2952b01
APB
7874 }
7875
7876 return 0;
7877}
7878
7879/* Return a non zero value if NODE represents an outer field inner
7880 access that was been already expanded. As a side effect, it returns
7881 the name of the field being accessed and the argument passed to the
7882 access function, suitable for a regeneration of the access method
7883 call if necessary. */
7884
7885static int
7886outer_field_expanded_access_p (node, name, arg_type, arg)
7887 tree node, *name, *arg_type, *arg;
7888{
7889 int identified = 0;
7890
7891 if (TREE_CODE (node) != CALL_EXPR)
7892 return 0;
7893
7894 /* Well, gcj generates slightly different tree nodes when compiling
7895 to native or bytecodes. It's the case for function calls. */
7896
7897 if (flag_emit_class_files
7898 && TREE_CODE (node) == CALL_EXPR
7899 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7900 identified = 1;
7901 else if (!flag_emit_class_files)
7902 {
7903 node = TREE_OPERAND (node, 0);
7904
7905 if (node && TREE_OPERAND (node, 0)
7906 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7907 {
7908 node = TREE_OPERAND (node, 0);
7909 if (TREE_OPERAND (node, 0)
7910 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7911 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7912 (DECL_NAME (TREE_OPERAND (node, 0)))))
7913 identified = 1;
7914 }
7915 }
7916
7917 if (identified && name && arg_type && arg)
7918 {
7919 tree argument = TREE_OPERAND (node, 1);
7920 *name = DECL_NAME (TREE_OPERAND (node, 0));
7921 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7922 *arg = TREE_VALUE (argument);
7923 }
7924 return identified;
7925}
7926
7927/* Detect in NODE an outer field read access from an inner class and
7928 transform it into a write with RHS as an argument. This function is
7929 called from the java_complete_lhs when an assignment to a LHS can
7930 be identified. */
7931
7932static tree
7933outer_field_access_fix (wfl, node, rhs)
7934 tree wfl, node, rhs;
7935{
7936 tree name, arg_type, arg;
7937
7938 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7939 {
7940 /* At any rate, check whether we're trying to assign a value to
7941 a final. */
7942 tree accessed = (JDECL_P (node) ? node :
7943 (TREE_CODE (node) == COMPONENT_REF ?
7944 TREE_OPERAND (node, 1) : node));
7945 if (check_final_assignment (accessed, wfl))
7946 return error_mark_node;
7947
7948 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7949 arg_type, name, arg, rhs);
7950 return java_complete_tree (node);
7951 }
7952 return NULL_TREE;
7953}
7954
7955/* Construct the expression that calls an access method:
7956 <type>.access$<n>(<arg1> [, <arg2>]);
7957
7958 ARG2 can be NULL and will be omitted in that case. It will denote a
7959 read access. */
7960
7961static tree
7962build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7963 int lc;
7964 tree type, access_method_name, arg1, arg2;
7965{
7966 tree args, cn, access;
7967
7968 args = arg1 ? arg1 :
7969 build_wfl_node (build_current_thisn (current_class));
7970 args = build_tree_list (NULL_TREE, args);
7971
7972 if (arg2)
7973 args = tree_cons (NULL_TREE, arg2, args);
7974
7975 access = build_method_invocation (build_wfl_node (access_method_name), args);
7976 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7977 return make_qualified_primary (cn, access, lc);
7978}
7979
7980static tree
7981build_new_access_id ()
7982{
7983 static int access_n_counter = 1;
7984 char buffer [128];
7985
7986 sprintf (buffer, "access$%d", access_n_counter++);
7987 return get_identifier (buffer);
7988}
7989
7990/* Create the static access functions for the outer field DECL. We define a
7991 read:
7992 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7993 return inst$.field;
7994 }
7995 and a write access:
7996 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7997 TREE_TYPE (<field>) value$) {
7998 return inst$.field = value$;
7999 }
8000 We should have a usage flags on the DECL so we can lazily turn the ones
8001 we're using for code generation. FIXME.
8002*/
8003
8004static tree
8005build_outer_field_access_methods (decl)
8006 tree decl;
8007{
8008 tree id, args, stmt, mdecl;
8009
c7303e41 8010 if (FIELD_INNER_ACCESS_P (decl))
c2952b01
APB
8011 return FIELD_INNER_ACCESS (decl);
8012
c7303e41
APB
8013 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8014
c2952b01
APB
8015 /* Create the identifier and a function named after it. */
8016 id = build_new_access_id ();
8017
8018 /* The identifier is marked as bearing the name of a generated write
8019 access function for outer field accessed from inner classes. */
8020 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8021
8022 /* Create the read access */
8023 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8024 TREE_CHAIN (args) = end_params_node;
8025 stmt = make_qualified_primary (build_wfl_node (inst_id),
8026 build_wfl_node (DECL_NAME (decl)), 0);
8027 stmt = build_return (0, stmt);
8028 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8029 TREE_TYPE (decl), id, args, stmt);
8030 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8031
c7303e41
APB
8032 /* Create the write access method. No write access for final variable */
8033 if (!FIELD_FINAL (decl))
8034 {
8035 args = build_tree_list (inst_id,
8036 build_pointer_type (DECL_CONTEXT (decl)));
8037 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8038 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8039 stmt = make_qualified_primary (build_wfl_node (inst_id),
8040 build_wfl_node (DECL_NAME (decl)), 0);
8041 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8042 build_wfl_node (wpv_id)));
8043 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8044 TREE_TYPE (decl), id,
8045 args, stmt);
8046 }
c2952b01 8047 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8048
8049 /* Return the access name */
8050 return FIELD_INNER_ACCESS (decl) = id;
8051}
8052
8053/* Build an field access method NAME. */
8054
8055static tree
8056build_outer_field_access_method (class, type, name, args, body)
8057 tree class, type, name, args, body;
8058{
8059 tree saved_current_function_decl, mdecl;
8060
8061 /* Create the method */
8062 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8063 fix_method_argument_names (args, mdecl);
8064 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8065
8066 /* Attach the method body. */
8067 saved_current_function_decl = current_function_decl;
8068 start_artificial_method_body (mdecl);
8069 java_method_add_stmt (mdecl, body);
8070 end_artificial_method_body (mdecl);
8071 current_function_decl = saved_current_function_decl;
8072
8073 return mdecl;
8074}
8075
8076\f
8077/* This section deals with building access function necessary for
8078 certain kinds of method invocation from inner classes. */
8079
8080static tree
8081build_outer_method_access_method (decl)
8082 tree decl;
8083{
8084 tree saved_current_function_decl, mdecl;
8085 tree args = NULL_TREE, call_args = NULL_TREE;
8086 tree carg, id, body, class;
8087 char buffer [80];
8088 int parm_id_count = 0;
8089
8090 /* Test this abort with an access to a private field */
8091 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8092 abort ();
8093
8094 /* Check the cache first */
8095 if (DECL_FUNCTION_INNER_ACCESS (decl))
8096 return DECL_FUNCTION_INNER_ACCESS (decl);
8097
8098 class = DECL_CONTEXT (decl);
8099
8100 /* Obtain an access identifier and mark it */
8101 id = build_new_access_id ();
8102 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8103
c2952b01
APB
8104 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8105 /* Create the arguments, as much as the original */
8106 for (; carg && carg != end_params_node;
8107 carg = TREE_CHAIN (carg))
8108 {
8109 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8110 args = chainon (args, build_tree_list (get_identifier (buffer),
8111 TREE_VALUE (carg)));
8112 }
8113 args = chainon (args, end_params_node);
8114
8115 /* Create the method */
8116 mdecl = create_artificial_method (class, ACC_STATIC,
8117 TREE_TYPE (TREE_TYPE (decl)), id, args);
8118 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8119 /* There is a potential bug here. We should be able to use
8120 fix_method_argument_names, but then arg names get mixed up and
8121 eventually a constructor will have its this$0 altered and the
8122 outer context won't be assignment properly. The test case is
8123 stub.java FIXME */
8124 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8125
8126 /* Attach the method body. */
8127 saved_current_function_decl = current_function_decl;
8128 start_artificial_method_body (mdecl);
8129
8130 /* The actual method invocation uses the same args. When invoking a
8131 static methods that way, we don't want to skip the first
8132 argument. */
8133 carg = args;
8134 if (!METHOD_STATIC (decl))
8135 carg = TREE_CHAIN (carg);
8136 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8137 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8138 call_args);
8139
8140 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8141 call_args);
8142 if (!METHOD_STATIC (decl))
8143 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8144 body, 0);
8145 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8146 body = build_return (0, body);
8147 java_method_add_stmt (mdecl,body);
8148 end_artificial_method_body (mdecl);
8149 current_function_decl = saved_current_function_decl;
c2952b01
APB
8150
8151 /* Back tag the access function so it know what it accesses */
8152 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8153
8154 /* Tag the current method so it knows it has an access generated */
8155 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8156}
8157
8158\f
8159/* This section of the code deals with building expressions to access
8160 the enclosing instance of an inner class. The enclosing instance is
8161 kept in a generated field called this$<n>, with <n> being the
8162 inner class nesting level (starting from 0.) */
8163
dba41d30
APB
8164/* Build an access to a given this$<n>, always chaining access call to
8165 others. Access methods to this$<n> are build on the fly if
8166 necessary. This CAN'T be used to solely access this$<n-1> from
8167 this$<n> (which alway yield to special cases and optimization, see
8168 for example build_outer_field_access). */
c2952b01
APB
8169
8170static tree
8171build_access_to_thisn (from, to, lc)
8172 tree from, to;
8173 int lc;
8174{
8175 tree access = NULL_TREE;
8176
8177 while (from != to)
8178 {
c2952b01 8179 if (!access)
dba41d30
APB
8180 {
8181 access = build_current_thisn (from);
8182 access = build_wfl_node (access);
8183 }
8184 else
c2952b01 8185 {
dba41d30
APB
8186 tree access0_wfl, cn;
8187
8188 maybe_build_thisn_access_method (from);
8189 access0_wfl = build_wfl_node (access0_identifier_node);
8190 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8191 EXPR_WFL_LINECOL (access0_wfl) = lc;
8192 access = build_tree_list (NULL_TREE, access);
8193 access = build_method_invocation (access0_wfl, access);
8194 access = make_qualified_primary (cn, access, lc);
c2952b01 8195 }
5e18f6d6
APB
8196
8197 /* if FROM isn't an inter class, that's fine, we've done
8198 enough. What we're looking for can be accessed from there. */
8199 from = DECL_CONTEXT (TYPE_NAME (from));
8200 if (!from)
8201 break;
8202 from = TREE_TYPE (from);
c2952b01
APB
8203 }
8204 return access;
8205}
8206
8207/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8208 is returned if nothing needs to be generated. Otherwise, the method
152de068 8209 generated and a method decl is returned.
c2952b01
APB
8210
8211 NOTE: These generated methods should be declared in a class file
8212 attribute so that they can't be referred to directly. */
8213
8214static tree
8215maybe_build_thisn_access_method (type)
8216 tree type;
8217{
8218 tree mdecl, args, stmt, rtype;
8219 tree saved_current_function_decl;
8220
8221 /* If TYPE is a top-level class, no access method is required.
8222 If there already is such an access method, bail out. */
ee5f86dc 8223 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
c2952b01
APB
8224 return NULL_TREE;
8225
8226 /* We generate the method. The method looks like:
8227 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8228 */
c2952b01
APB
8229 args = build_tree_list (inst_id, build_pointer_type (type));
8230 TREE_CHAIN (args) = end_params_node;
8231 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8232 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8233 access0_identifier_node, args);
8234 fix_method_argument_names (args, mdecl);
8235 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8236 stmt = build_current_thisn (type);
8237 stmt = make_qualified_primary (build_wfl_node (inst_id),
8238 build_wfl_node (stmt), 0);
8239 stmt = build_return (0, stmt);
8240
8241 saved_current_function_decl = current_function_decl;
8242 start_artificial_method_body (mdecl);
8243 java_method_add_stmt (mdecl, stmt);
8244 end_artificial_method_body (mdecl);
8245 current_function_decl = saved_current_function_decl;
c2952b01
APB
8246
8247 CLASS_ACCESS0_GENERATED_P (type) = 1;
8248
8249 return mdecl;
8250}
8251
8252/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8253 the first level of innerclassing. this$1 for the next one, etc...
8254 This function can be invoked with TYPE to NULL, available and then
8255 has to count the parser context. */
8256
8257static tree
8258build_current_thisn (type)
8259 tree type;
8260{
8261 static int saved_i = -1;
8262 static tree saved_thisn = NULL_TREE;
19e223db
MM
8263 static tree saved_type = NULL_TREE;
8264 static int saved_type_i = 0;
8265 static int initialized_p;
c2952b01
APB
8266 tree decl;
8267 char buffer [80];
8268 int i = 0;
8269
19e223db
MM
8270 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8271 if (!initialized_p)
c2952b01 8272 {
19e223db
MM
8273 ggc_add_tree_root (&saved_thisn, 1);
8274 ggc_add_tree_root (&saved_type, 1);
8275 initialized_p = 1;
8276 }
c2952b01 8277
19e223db
MM
8278 if (type)
8279 {
c2952b01
APB
8280 if (type == saved_type)
8281 i = saved_type_i;
8282 else
8283 {
8284 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8285 decl; decl = DECL_CONTEXT (decl), i++)
8286 ;
8287
8288 saved_type = type;
8289 saved_type_i = i;
8290 }
8291 }
8292 else
8293 i = list_length (GET_CPC_LIST ())-2;
8294
8295 if (i == saved_i)
8296 return saved_thisn;
8297
8298 sprintf (buffer, "this$%d", i);
8299 saved_i = i;
8300 saved_thisn = get_identifier (buffer);
8301 return saved_thisn;
8302}
8303
8304/* Return the assignement to the hidden enclosing context `this$<n>'
8305 by the second incoming parameter to the innerclass constructor. The
8306 form used is `this.this$<n> = this$<n>;'. */
8307
8308static tree
8309build_thisn_assign ()
8310{
8311 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8312 {
8313 tree thisn = build_current_thisn (current_class);
8314 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8315 build_wfl_node (thisn), 0);
8316 tree rhs = build_wfl_node (thisn);
8317 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8318 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8319 }
8320 return NULL_TREE;
8321}
8322
8323\f
165f37bc
APB
8324/* Building the synthetic `class$' used to implement the `.class' 1.1
8325 extension for non primitive types. This method looks like:
8326
8327 static Class class$(String type) throws NoClassDefFoundError
8328 {
8329 try {return (java.lang.Class.forName (String));}
8330 catch (ClassNotFoundException e) {
8331 throw new NoClassDefFoundError(e.getMessage());}
8332 } */
8333
8334static tree
8335build_dot_class_method (class)
8336 tree class;
8337{
8338#define BWF(S) build_wfl_node (get_identifier ((S)))
8339#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8340 tree args, tmp, saved_current_function_decl, mdecl;
8341 tree stmt, throw_stmt, catch, catch_block, try_block;
8342 tree catch_clause_param;
8343 tree class_not_found_exception, no_class_def_found_error;
8344
8345 static tree get_message_wfl, type_parm_wfl;
8346
8347 if (!get_message_wfl)
8348 {
8349 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8350 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8351 ggc_add_tree_root (&get_message_wfl, 1);
8352 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8353 }
8354
8355 /* Build the arguments */
8356 args = build_tree_list (get_identifier ("type$"),
8357 build_pointer_type (string_type_node));
8358 TREE_CHAIN (args) = end_params_node;
8359
8360 /* Build the qualified name java.lang.Class.forName */
8361 tmp = MQN (MQN (MQN (BWF ("java"),
8362 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8363
8364 /* For things we have to catch and throw */
8365 class_not_found_exception =
8366 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8367 no_class_def_found_error =
8368 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8369 load_class (class_not_found_exception, 1);
8370 load_class (no_class_def_found_error, 1);
8371
8372 /* Create the "class$" function */
8373 mdecl = create_artificial_method (class, ACC_STATIC,
8374 build_pointer_type (class_type_node),
94807d33 8375 classdollar_identifier_node, args);
165f37bc
APB
8376 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8377 no_class_def_found_error);
8378
8379 /* We start by building the try block. We need to build:
8380 return (java.lang.Class.forName (type)); */
8381 stmt = build_method_invocation (tmp,
8382 build_tree_list (NULL_TREE, type_parm_wfl));
8383 stmt = build_return (0, stmt);
8384 /* Put it in a block. That's the try block */
8385 try_block = build_expr_block (stmt, NULL_TREE);
8386
8387 /* Now onto the catch block. We start by building the expression
8388 throwing a new exception:
8389 throw new NoClassDefFoundError (_.getMessage); */
8390 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8391 get_message_wfl, 0);
8392 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8393
8394 /* Build new NoClassDefFoundError (_.getMessage) */
8395 throw_stmt = build_new_invocation
8396 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8397 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8398
8399 /* Build the throw, (it's too early to use BUILD_THROW) */
8400 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8401
8402 /* Build the catch block to encapsulate all this. We begin by
8403 building an decl for the catch clause parameter and link it to
8404 newly created block, the catch block. */
8405 catch_clause_param =
8406 build_decl (VAR_DECL, wpv_id,
8407 build_pointer_type (class_not_found_exception));
8408 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8409
8410 /* We initialize the variable with the exception handler. */
8411 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8412 soft_exceptioninfo_call_node);
8413 add_stmt_to_block (catch_block, NULL_TREE, catch);
8414
8415 /* We add the statement throwing the new exception */
8416 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8417
8418 /* Build a catch expression for all this */
8419 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8420
8421 /* Build the try/catch sequence */
8422 stmt = build_try_statement (0, try_block, catch_block);
8423
8424 fix_method_argument_names (args, mdecl);
8425 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8426 saved_current_function_decl = current_function_decl;
8427 start_artificial_method_body (mdecl);
8428 java_method_add_stmt (mdecl, stmt);
8429 end_artificial_method_body (mdecl);
8430 current_function_decl = saved_current_function_decl;
8431 TYPE_DOT_CLASS (class) = mdecl;
8432
8433 return mdecl;
8434}
8435
8436static tree
f0f3a777
APB
8437build_dot_class_method_invocation (type)
8438 tree type;
165f37bc 8439{
f0f3a777
APB
8440 tree sig_id, s;
8441
8442 if (TYPE_ARRAY_P (type))
8443 sig_id = build_java_signature (type);
8444 else
8445 sig_id = DECL_NAME (TYPE_NAME (type));
8446
1f8f4a0b
MM
8447 s = build_string (IDENTIFIER_LENGTH (sig_id),
8448 IDENTIFIER_POINTER (sig_id));
94807d33 8449 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
165f37bc
APB
8450 build_tree_list (NULL_TREE, s));
8451}
8452
c2952b01
APB
8453/* This section of the code deals with constructor. */
8454
22eed1e6
APB
8455/* Craft a body for default constructor. Patch existing constructor
8456 bodies with call to super() and field initialization statements if
8457 necessary. */
8458
8459static void
8460fix_constructors (mdecl)
8461 tree mdecl;
8462{
8463 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8464 tree thisn_assign, compound = NULL_TREE;
8465 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8466
22eed1e6
APB
8467 if (!body)
8468 {
22eed1e6
APB
8469 /* It is an error for the compiler to generate a default
8470 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8471 takes no argument, or the same args for an anonymous class */
8472 if (verify_constructor_super (mdecl))
22eed1e6 8473 {
c2952b01
APB
8474 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8475 tree save = DECL_NAME (mdecl);
49f48c71 8476 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8477 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8478 parse_error_context
c2952b01
APB
8479 (lookup_cl (TYPE_NAME (class_type)),
8480 "No constructor matching `%s' found in class `%s'",
8481 lang_printable_name (mdecl, 0), n);
8482 DECL_NAME (mdecl) = save;
22eed1e6
APB
8483 }
8484
c2952b01
APB
8485 /* The constructor body must be crafted by hand. It's the
8486 constructor we defined when we realize we didn't have the
8487 CLASSNAME() constructor */
22eed1e6
APB
8488 start_artificial_method_body (mdecl);
8489
f0f3a777
APB
8490 /* Insert an assignment to the this$<n> hidden field, if
8491 necessary */
8492 if ((thisn_assign = build_thisn_assign ()))
8493 java_method_add_stmt (mdecl, thisn_assign);
8494
22eed1e6
APB
8495 /* We don't generate a super constructor invocation if we're
8496 compiling java.lang.Object. build_super_invocation takes care
8497 of that. */
e920ebc9 8498 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8499
c2952b01
APB
8500 /* Insert the instance initializer block right here, after the
8501 super invocation. */
8502 add_instance_initializer (mdecl);
8503
22eed1e6
APB
8504 end_artificial_method_body (mdecl);
8505 }
8506 /* Search for an explicit constructor invocation */
8507 else
8508 {
8509 int found = 0;
8510 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8511
8512 while (body)
8513 switch (TREE_CODE (body))
8514 {
8515 case CALL_EXPR:
8516 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8517 body = NULL_TREE;
8518 break;
8519 case COMPOUND_EXPR:
8520 case EXPR_WITH_FILE_LOCATION:
8521 body = TREE_OPERAND (body, 0);
8522 break;
8523 case BLOCK:
8524 body = BLOCK_EXPR_BODY (body);
8525 break;
8526 default:
8527 found = 0;
8528 body = NULL_TREE;
8529 }
8530 /* The constructor is missing an invocation of super() */
8531 if (!found)
8532 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8533 build_super_invocation (mdecl));
22eed1e6 8534
c2952b01
APB
8535 /* Generate the assignment to this$<n>, if necessary */
8536 if ((thisn_assign = build_thisn_assign ()))
8537 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8538
f0f3a777
APB
8539 /* Insert the instance initializer block right here, after the
8540 super invocation. */
8541 add_instance_initializer (mdecl);
8542
22eed1e6
APB
8543 /* Fix the constructor main block if we're adding extra stmts */
8544 if (compound)
8545 {
8546 compound = add_stmt_to_compound (compound, NULL_TREE,
8547 BLOCK_EXPR_BODY (main_block));
8548 BLOCK_EXPR_BODY (main_block) = compound;
8549 }
8550 }
8551}
8552
8553/* Browse constructors in the super class, searching for a constructor
8554 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8555 otherwise. If the current class is an anonymous inner class, look
8556 for something that has the same signature. */
22eed1e6
APB
8557
8558static int
c2952b01
APB
8559verify_constructor_super (mdecl)
8560 tree mdecl;
22eed1e6
APB
8561{
8562 tree class = CLASSTYPE_SUPER (current_class);
152de068 8563 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8564 tree sdecl;
8565
22eed1e6
APB
8566 if (!class)
8567 return 0;
8568
c2952b01 8569 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8570 {
c2952b01
APB
8571 tree mdecl_arg_type;
8572 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8573 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8574 if (DECL_CONSTRUCTOR_P (sdecl))
8575 {
cf1b2274 8576 tree m_arg_type;
152de068
APB
8577 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8578 if (super_inner)
8579 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8580 for (m_arg_type = mdecl_arg_type;
8581 (arg_type != end_params_node
8582 && m_arg_type != end_params_node);
c2952b01 8583 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8584 m_arg_type = TREE_CHAIN (m_arg_type))
8585 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8586 break;
8587
cf1b2274 8588 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8589 return 0;
8590 }
8591 }
8592 else
8593 {
8594 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8595 {
152de068
APB
8596 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8597 if (super_inner)
8598 arg = TREE_CHAIN (arg);
8599 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8600 return 0;
8601 }
8602 }
8603 return 1;
8604}
8605
22eed1e6 8606/* Generate code for all context remembered for code generation. */
b351b287
APB
8607
8608void
8609java_expand_classes ()
8610{
5423609c 8611 int save_error_count = 0;
c2952b01
APB
8612 static struct parser_ctxt *saved_ctxp = NULL;
8613
23a79c61
APB
8614 java_parse_abort_on_error ();
8615 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8616 return;
8617 java_layout_classes ();
8618 java_parse_abort_on_error ();
8619
c2952b01 8620 saved_ctxp = ctxp_for_generation;
b351b287
APB
8621 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8622 {
8623 ctxp = ctxp_for_generation;
3a2e5926 8624 input_filename = ctxp->filename;
b351b287 8625 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8626 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8627 java_parse_abort_on_error ();
8628 }
3a2e5926 8629 input_filename = main_input_filename;
c2952b01
APB
8630
8631 /* Find anonymous classes and expand their constructor, now they
8632 have been fixed. */
8633 for (ctxp_for_generation = saved_ctxp;
8634 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8635 {
8636 tree current;
8637 ctxp = ctxp_for_generation;
8638 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8639 {
8640 current_class = TREE_TYPE (current);
8641 if (ANONYMOUS_CLASS_P (current_class))
8642 {
8643 tree d;
8644 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8645 {
8646 if (DECL_CONSTRUCTOR_P (d))
8647 {
8648 restore_line_number_status (1);
c2952b01
APB
8649 java_complete_expand_method (d);
8650 restore_line_number_status (0);
8651 break; /* We now there are no other ones */
8652 }
8653 }
8654 }
8655 }
8656 }
8657
8658 /* If we've found error at that stage, don't try to generate
8659 anything, unless we're emitting xrefs or checking the syntax only
8660 (but not using -fsyntax-only for the purpose of generating
8661 bytecode. */
8662 if (java_error_count && !flag_emit_xref
8663 && (!flag_syntax_only && !flag_emit_class_files))
8664 return;
8665
8666 /* Now things are stable, go for generation of the class data. */
8667 for (ctxp_for_generation = saved_ctxp;
8668 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8669 {
8670 tree current;
8671 ctxp = ctxp_for_generation;
8672 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8673 {
8674 current_class = TREE_TYPE (current);
8675 outgoing_cpool = TYPE_CPOOL (current_class);
8676 if (flag_emit_class_files)
8677 write_classfile (current_class);
8678 if (flag_emit_xref)
8679 expand_xref (current_class);
8680 else if (! flag_syntax_only)
8681 finish_class ();
8682 }
8683 }
b351b287
APB
8684}
8685
e04a16fb
AG
8686/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8687 a tree list node containing RIGHT. Fore coming RIGHTs will be
8688 chained to this hook. LOCATION contains the location of the
8689 separating `.' operator. */
8690
8691static tree
8692make_qualified_primary (primary, right, location)
8693 tree primary, right;
8694 int location;
8695{
8696 tree wfl;
8697
c2952b01 8698 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8699 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8700 else
8701 {
8702 wfl = primary;
c2952b01
APB
8703 /* If wfl wasn't qualified, we build a first anchor */
8704 if (!EXPR_WFL_QUALIFICATION (wfl))
8705 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8706 }
8707
c2952b01 8708 /* And chain them */
e04a16fb
AG
8709 EXPR_WFL_LINECOL (right) = location;
8710 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8711 PRIMARY_P (wfl) = 1;
8712 return wfl;
8713}
8714
8715/* Simple merge of two name separated by a `.' */
8716
8717static tree
8718merge_qualified_name (left, right)
8719 tree left, right;
8720{
8721 tree node;
c2952b01
APB
8722 if (!left && !right)
8723 return NULL_TREE;
8724
8725 if (!left)
8726 return right;
8727
8728 if (!right)
8729 return left;
8730
e04a16fb
AG
8731 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8732 IDENTIFIER_LENGTH (left));
8733 obstack_1grow (&temporary_obstack, '.');
8734 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8735 IDENTIFIER_LENGTH (right));
8736 node = get_identifier (obstack_base (&temporary_obstack));
8737 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8738 QUALIFIED_P (node) = 1;
8739 return node;
8740}
8741
8742/* Merge the two parts of a qualified name into LEFT. Set the
8743 location information of the resulting node to LOCATION, usually
8744 inherited from the location information of the `.' operator. */
8745
8746static tree
8747make_qualified_name (left, right, location)
8748 tree left, right;
8749 int location;
8750{
bc3ca41b
PB
8751#ifdef USE_COMPONENT_REF
8752 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8753 EXPR_WFL_LINECOL (node) = location;
8754 return node;
8755#else
e04a16fb
AG
8756 tree left_id = EXPR_WFL_NODE (left);
8757 tree right_id = EXPR_WFL_NODE (right);
8758 tree wfl, merge;
8759
8760 merge = merge_qualified_name (left_id, right_id);
8761
8762 /* Left wasn't qualified and is now qualified */
8763 if (!QUALIFIED_P (left_id))
8764 {
8765 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8766 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8767 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8768 }
8769
8770 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8771 EXPR_WFL_LINECOL (wfl) = location;
8772 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8773
8774 EXPR_WFL_NODE (left) = merge;
8775 return left;
bc3ca41b 8776#endif
e04a16fb
AG
8777}
8778
8779/* Extract the last identifier component of the qualified in WFL. The
8780 last identifier is removed from the linked list */
8781
8782static tree
8783cut_identifier_in_qualified (wfl)
8784 tree wfl;
8785{
8786 tree q;
8787 tree previous = NULL_TREE;
8788 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8789 if (!TREE_CHAIN (q))
8790 {
8791 if (!previous)
400500c4
RK
8792 /* Operating on a non qualified qualified WFL. */
8793 abort ();
8794
e04a16fb
AG
8795 TREE_CHAIN (previous) = NULL_TREE;
8796 return TREE_PURPOSE (q);
8797 }
8798}
8799
8800/* Resolve the expression name NAME. Return its decl. */
8801
8802static tree
5e942c50 8803resolve_expression_name (id, orig)
e04a16fb 8804 tree id;
5e942c50 8805 tree *orig;
e04a16fb
AG
8806{
8807 tree name = EXPR_WFL_NODE (id);
8808 tree decl;
8809
8810 /* 6.5.5.1: Simple expression names */
8811 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8812 {
8813 /* 15.13.1: NAME can appear within the scope of a local variable
8814 declaration */
8815 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8816 return decl;
8817
8818 /* 15.13.1: NAME can appear within a class declaration */
8819 else
8820 {
8821 decl = lookup_field_wrapper (current_class, name);
8822 if (decl)
8823 {
c2952b01 8824 tree access = NULL_TREE;
e04a16fb 8825 int fs = FIELD_STATIC (decl);
f2760b27
APB
8826
8827 /* If we're accessing an outer scope local alias, make
8828 sure we change the name of the field we're going to
8829 build access to. */
8830 if (FIELD_LOCAL_ALIAS_USED (decl))
8831 name = DECL_NAME (decl);
8832
e04a16fb
AG
8833 /* Instance variable (8.3.1.1) can't appear within
8834 static method, static initializer or initializer for
8835 a static variable. */
8836 if (!fs && METHOD_STATIC (current_function_decl))
8837 {
7f10c2e2 8838 static_ref_err (id, name, current_class);
e04a16fb
AG
8839 return error_mark_node;
8840 }
22eed1e6
APB
8841 /* Instance variables can't appear as an argument of
8842 an explicit constructor invocation */
7e1376a1
BM
8843 if (!fs && ctxp->explicit_constructor_p
8844 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
22eed1e6
APB
8845 {
8846 parse_error_context
781b0558 8847 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8848 return error_mark_node;
8849 }
5e942c50 8850
c2952b01
APB
8851 /* If we're processing an inner class and we're trying
8852 to access a field belonging to an outer class, build
8853 the access to the field */
8854 if (!fs && outer_field_access_p (current_class, decl))
ee5f86dc
APB
8855 {
8856 if (CLASS_STATIC (TYPE_NAME (current_class)))
8857 {
8858 static_ref_err (id, DECL_NAME (decl), current_class);
8859 return error_mark_node;
8860 }
8861 return build_outer_field_access (id, decl);
8862 }
c2952b01 8863
5e942c50 8864 /* Otherwise build what it takes to access the field */
c2952b01
APB
8865 access = build_field_ref ((fs ? NULL_TREE : current_this),
8866 DECL_CONTEXT (decl), name);
e8fc7396 8867 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8868 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8869 /* We may be asked to save the real field access node */
8870 if (orig)
c2952b01 8871 *orig = access;
5e942c50 8872 /* And we return what we got */
c2952b01 8873 return access;
e04a16fb
AG
8874 }
8875 /* Fall down to error report on undefined variable */
8876 }
8877 }
8878 /* 6.5.5.2 Qualified Expression Names */
8879 else
8880 {
5e942c50
APB
8881 if (orig)
8882 *orig = NULL_TREE;
e04a16fb
AG
8883 qualify_ambiguous_name (id);
8884 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8885 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8886 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8887 }
8888
8889 /* We've got an error here */
8890 parse_error_context (id, "Undefined variable `%s'",
8891 IDENTIFIER_POINTER (name));
8892
8893 return error_mark_node;
8894}
8895
7f10c2e2
APB
8896static void
8897static_ref_err (wfl, field_id, class_type)
8898 tree wfl, field_id, class_type;
8899{
8900 parse_error_context
8901 (wfl,
8902 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8903 IDENTIFIER_POINTER (field_id),
8904 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8905}
8906
e04a16fb
AG
8907/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8908 We return something suitable to generate the field access. We also
8909 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8910 recipient's address can be null. */
8911
8912static tree
8913resolve_field_access (qual_wfl, field_decl, field_type)
8914 tree qual_wfl;
8915 tree *field_decl, *field_type;
8916{
8917 int is_static = 0;
8918 tree field_ref;
8919 tree decl, where_found, type_found;
8920
8921 if (resolve_qualified_expression_name (qual_wfl, &decl,
8922 &where_found, &type_found))
8923 return error_mark_node;
8924
8925 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8926 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8927 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8928 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8929 {
8930 tree length = build_java_array_length_access (where_found);
8931 field_ref =
8932 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8933
8934 /* In case we're dealing with a static array, we need to
8935 initialize its class before the array length can be fetched.
8936 It's also a good time to create a DECL_RTL for the field if
8937 none already exists, otherwise if the field was declared in a
8938 class found in an external file and hasn't been (and won't
8939 be) accessed for its value, none will be created. */
8940 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8941 {
8942 build_static_field_ref (where_found);
8943 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8944 }
e04a16fb
AG
8945 }
8946 /* We might have been trying to resolve field.method(). In which
8947 case, the resolution is over and decl is the answer */
34f4db93 8948 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8949 field_ref = decl;
34f4db93 8950 else if (JDECL_P (decl))
e04a16fb 8951 {
5e942c50
APB
8952 int static_final_found = 0;
8953 if (!type_found)
8954 type_found = DECL_CONTEXT (decl);
34f4db93 8955 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
c7303e41 8956 if (CLASS_FINAL_VARIABLE_P (decl)
5e942c50 8957 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8958 && DECL_INITIAL (decl))
5e942c50 8959 {
0c2b8145
APB
8960 /* When called on a FIELD_DECL of the right (primitive)
8961 type, java_complete_tree will try to substitue the decl
8962 for it's initial value. */
70541f45 8963 field_ref = java_complete_tree (decl);
5e942c50
APB
8964 static_final_found = 1;
8965 }
8966 else
7f10c2e2
APB
8967 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8968 NULL_TREE : where_found),
5e942c50 8969 type_found, DECL_NAME (decl));
e04a16fb
AG
8970 if (field_ref == error_mark_node)
8971 return error_mark_node;
e8fc7396
APB
8972 if (is_static && !static_final_found
8973 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8974 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8975 }
8976 else
8977 field_ref = decl;
8978
8979 if (field_decl)
8980 *field_decl = decl;
8981 if (field_type)
c877974e
APB
8982 *field_type = (QUAL_DECL_TYPE (decl) ?
8983 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8984 return field_ref;
8985}
8986
e28cd97b
APB
8987/* If NODE is an access to f static field, strip out the class
8988 initialization part and return the field decl, otherwise, return
8989 NODE. */
8990
8991static tree
8992strip_out_static_field_access_decl (node)
8993 tree node;
8994{
8995 if (TREE_CODE (node) == COMPOUND_EXPR)
8996 {
8997 tree op1 = TREE_OPERAND (node, 1);
8998 if (TREE_CODE (op1) == COMPOUND_EXPR)
8999 {
9000 tree call = TREE_OPERAND (op1, 0);
9001 if (TREE_CODE (call) == CALL_EXPR
9002 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9003 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9004 == soft_initclass_node)
9005 return TREE_OPERAND (op1, 1);
9006 }
2f11d407
TT
9007 else if (JDECL_P (op1))
9008 return op1;
e28cd97b
APB
9009 }
9010 return node;
9011}
9012
e04a16fb
AG
9013/* 6.5.5.2: Qualified Expression Names */
9014
9015static int
9016resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9017 tree wfl;
9018 tree *found_decl, *type_found, *where_found;
9019{
9020 int from_type = 0; /* Field search initiated from a type */
c2952b01 9021 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
9022 int previous_call_static = 0;
9023 int is_static;
9024 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
9025 /* For certain for of inner class instantiation */
9026 tree saved_current, saved_this;
9027#define RESTORE_THIS_AND_CURRENT_CLASS \
9028 { current_class = saved_current; current_this = saved_this;}
9029
c877974e 9030 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
9031
9032 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9033 {
9034 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
9035 tree ret_decl; /* for EH checking */
9036 int location; /* for EH checking */
e04a16fb
AG
9037
9038 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
9039 switch (TREE_CODE (qual_wfl))
9040 {
9041 case CALL_EXPR:
b67d701b 9042 case NEW_CLASS_EXPR:
e04a16fb
AG
9043 /* If the access to the function call is a non static field,
9044 build the code to access it. */
34f4db93 9045 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9046 {
ac825856
APB
9047 decl = maybe_access_field (decl, *where_found,
9048 DECL_CONTEXT (decl));
e04a16fb
AG
9049 if (decl == error_mark_node)
9050 return 1;
9051 }
c2952b01 9052
e04a16fb
AG
9053 /* And code for the function call */
9054 if (complete_function_arguments (qual_wfl))
9055 return 1;
c2952b01
APB
9056
9057 /* We might have to setup a new current class and a new this
9058 for the search of an inner class, relative to the type of
9059 a expression resolved as `decl'. The current values are
9060 saved and restored shortly after */
9061 saved_current = current_class;
9062 saved_this = current_this;
dba41d30
APB
9063 if (decl
9064 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9065 || from_qualified_this))
c2952b01 9066 {
dba41d30
APB
9067 /* If we still have `from_qualified_this', we have the form
9068 <T>.this.f() and we need to build <T>.this */
9069 if (from_qualified_this)
9070 {
9071 decl = build_access_to_thisn (current_class, type, 0);
9072 decl = java_complete_tree (decl);
9073 type = TREE_TYPE (TREE_TYPE (decl));
9074 }
c2952b01
APB
9075 current_class = type;
9076 current_this = decl;
dba41d30 9077 from_qualified_this = 0;
c2952b01
APB
9078 }
9079
89e09b9a
PB
9080 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9081 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9082 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9083 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
e101152f
APB
9084 *where_found = patch_method_invocation (qual_wfl, decl, type,
9085 from_super,
7705e9db 9086 &is_static, &ret_decl);
e04a16fb 9087 if (*where_found == error_mark_node)
c2952b01
APB
9088 {
9089 RESTORE_THIS_AND_CURRENT_CLASS;
9090 return 1;
9091 }
e04a16fb
AG
9092 *type_found = type = QUAL_DECL_TYPE (*where_found);
9093
c2952b01
APB
9094 /* If we're creating an inner class instance, check for that
9095 an enclosing instance is in scope */
9096 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9097 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9098 {
9099 parse_error_context
165f37bc
APB
9100 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9101 lang_printable_name (type, 0),
9102 (!current_this ? "" :
9103 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9104 RESTORE_THIS_AND_CURRENT_CLASS;
9105 return 1;
9106 }
9107
9108 /* In case we had to change then to resolve a inner class
9109 instantiation using a primary qualified by a `new' */
9110 RESTORE_THIS_AND_CURRENT_CLASS;
9111
34d4df06
APB
9112 /* EH check. No check on access$<n> functions */
9113 if (location
9114 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9115 (DECL_NAME (current_function_decl)))
7705e9db
APB
9116 check_thrown_exceptions (location, ret_decl);
9117
e04a16fb
AG
9118 /* If the previous call was static and this one is too,
9119 build a compound expression to hold the two (because in
9120 that case, previous function calls aren't transported as
9121 forcoming function's argument. */
9122 if (previous_call_static && is_static)
9123 {
9124 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9125 TREE_SIDE_EFFECTS (decl) = 1;
9126 }
9127 else
9128 {
9129 previous_call_static = is_static;
9130 decl = *where_found;
9131 }
c2952b01 9132 from_type = 0;
e04a16fb
AG
9133 continue;
9134
d8fccff5 9135 case NEW_ARRAY_EXPR:
c2952b01 9136 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9137 *where_found = decl = java_complete_tree (qual_wfl);
9138 if (decl == error_mark_node)
9139 return 1;
9140 *type_found = type = QUAL_DECL_TYPE (decl);
9141 CLASS_LOADED_P (type) = 1;
9142 continue;
9143
e04a16fb
AG
9144 case CONVERT_EXPR:
9145 *where_found = decl = java_complete_tree (qual_wfl);
9146 if (decl == error_mark_node)
9147 return 1;
9148 *type_found = type = QUAL_DECL_TYPE (decl);
9149 from_cast = 1;
9150 continue;
9151
22eed1e6 9152 case CONDITIONAL_EXPR:
5e942c50 9153 case STRING_CST:
ac22f9cb 9154 case MODIFY_EXPR:
22eed1e6
APB
9155 *where_found = decl = java_complete_tree (qual_wfl);
9156 if (decl == error_mark_node)
9157 return 1;
9158 *type_found = type = QUAL_DECL_TYPE (decl);
9159 continue;
9160
e04a16fb
AG
9161 case ARRAY_REF:
9162 /* If the access to the function call is a non static field,
9163 build the code to access it. */
34f4db93 9164 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9165 {
9166 decl = maybe_access_field (decl, *where_found, type);
9167 if (decl == error_mark_node)
9168 return 1;
9169 }
9170 /* And code for the array reference expression */
9171 decl = java_complete_tree (qual_wfl);
9172 if (decl == error_mark_node)
9173 return 1;
9174 type = QUAL_DECL_TYPE (decl);
9175 continue;
0a2138e2 9176
37feda7d
APB
9177 case PLUS_EXPR:
9178 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9179 return 1;
9180 if ((type = patch_string (decl)))
9181 decl = type;
9182 *where_found = QUAL_RESOLUTION (q) = decl;
9183 *type_found = type = TREE_TYPE (decl);
9184 break;
9185
165f37bc
APB
9186 case CLASS_LITERAL:
9187 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9188 return 1;
9189 *where_found = QUAL_RESOLUTION (q) = decl;
9190 *type_found = type = TREE_TYPE (decl);
9191 break;
9192
0a2138e2
APB
9193 default:
9194 /* Fix for -Wall Just go to the next statement. Don't
9195 continue */
a3f406ce 9196 break;
e04a16fb
AG
9197 }
9198
9199 /* If we fall here, we weren't processing a (static) function call. */
9200 previous_call_static = 0;
9201
9202 /* It can be the keyword THIS */
9203 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9204 {
9205 if (!current_this)
9206 {
9207 parse_error_context
9208 (wfl, "Keyword `this' used outside allowed context");
9209 return 1;
9210 }
7e1376a1
BM
9211 if (ctxp->explicit_constructor_p
9212 && type == current_class)
f63991a8 9213 {
781b0558 9214 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9215 return 1;
9216 }
e04a16fb 9217 /* We have to generate code for intermediate acess */
c2952b01
APB
9218 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9219 {
9220 *where_found = decl = current_this;
9221 *type_found = type = QUAL_DECL_TYPE (decl);
9222 }
4dbf4496
APB
9223 /* We're trying to access the this from somewhere else. Make sure
9224 it's allowed before doing so. */
c2952b01
APB
9225 else
9226 {
4dbf4496
APB
9227 if (!enclosing_context_p (type, current_class))
9228 {
9229 char *p = xstrdup (lang_printable_name (type, 0));
9230 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9231 p, p,
9232 lang_printable_name (current_class, 0));
9233 free (p);
9234 return 1;
9235 }
c2952b01 9236 from_qualified_this = 1;
dba41d30
APB
9237 /* If there's nothing else after that, we need to
9238 produce something now, otherwise, the section of the
9239 code that needs to produce <T>.this will generate
9240 what is necessary. */
9241 if (!TREE_CHAIN (q))
9242 {
9243 decl = build_access_to_thisn (current_class, type, 0);
9244 *where_found = decl = java_complete_tree (decl);
9245 *type_found = type = TREE_TYPE (decl);
9246 }
c2952b01
APB
9247 }
9248
9249 from_type = 0;
e04a16fb
AG
9250 continue;
9251 }
9252
9253 /* 15.10.2 Accessing Superclass Members using SUPER */
9254 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9255 {
9256 tree node;
9257 /* Check on the restricted use of SUPER */
9258 if (METHOD_STATIC (current_function_decl)
9259 || current_class == object_type_node)
9260 {
9261 parse_error_context
9262 (wfl, "Keyword `super' used outside allowed context");
9263 return 1;
9264 }
9265 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9266 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9267 CLASSTYPE_SUPER (current_class),
9268 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9269 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9270 if (decl == error_mark_node)
9271 return 1;
e04a16fb
AG
9272 *type_found = type = QUAL_DECL_TYPE (decl);
9273 from_super = from_type = 1;
9274 continue;
9275 }
9276
9277 /* 15.13.1: Can't search for field name in packages, so we
9278 assume a variable/class name was meant. */
9279 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9280 {
5e942c50
APB
9281 tree name = resolve_package (wfl, &q);
9282 if (name)
9283 {
c2952b01 9284 tree list;
5e942c50 9285 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9286 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9287 out. We're going to search something inside it. */
9288 *type_found = type = TREE_TYPE (decl);
9289 layout_class (type);
9290 from_type = 1;
c2952b01 9291
dde1da72
APB
9292 /* Fix them all the way down, if any are left. */
9293 if (q)
c2952b01 9294 {
dde1da72
APB
9295 list = TREE_CHAIN (q);
9296 while (list)
9297 {
9298 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9299 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9300 list = TREE_CHAIN (list);
9301 }
c2952b01 9302 }
5e942c50 9303 }
e04a16fb 9304 else
5e942c50
APB
9305 {
9306 if (from_super || from_cast)
9307 parse_error_context
9308 ((from_cast ? qual_wfl : wfl),
9309 "No variable `%s' defined in class `%s'",
9310 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9311 lang_printable_name (type, 0));
9312 else
9313 parse_error_context
9314 (qual_wfl, "Undefined variable or class name: `%s'",
9315 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9316 return 1;
9317 }
e04a16fb
AG
9318 }
9319
9320 /* We have a type name. It's been already resolved when the
9321 expression was qualified. */
9322 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9323 {
9324 if (!(decl = QUAL_RESOLUTION (q)))
9325 return 1; /* Error reported already */
9326
c2952b01
APB
9327 /* Sneak preview. If next we see a `new', we're facing a
9328 qualification with resulted in a type being selected
9329 instead of a field. Report the error */
9330 if(TREE_CHAIN (q)
9331 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9332 {
9333 parse_error_context (qual_wfl, "Undefined variable `%s'",
9334 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9335 return 1;
9336 }
9337
e101152f 9338 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
e04a16fb
AG
9339 {
9340 parse_error_context
9341 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9342 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9343 GET_TYPE_NAME (type),
e04a16fb
AG
9344 IDENTIFIER_POINTER (DECL_NAME (decl)),
9345 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9346 return 1;
9347 }
5e942c50 9348 check_deprecation (qual_wfl, decl);
c2952b01 9349
e04a16fb
AG
9350 type = TREE_TYPE (decl);
9351 from_type = 1;
9352 }
9353 /* We resolve and expression name */
9354 else
9355 {
cd531a2e 9356 tree field_decl = NULL_TREE;
e04a16fb
AG
9357
9358 /* If there exists an early resolution, use it. That occurs
9359 only once and we know that there are more things to
9360 come. Don't do that when processing something after SUPER
9361 (we need more thing to be put in place below */
9362 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9363 {
9364 decl = QUAL_RESOLUTION (q);
c877974e 9365 if (!type)
5e942c50 9366 {
7f10c2e2
APB
9367 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9368 {
9369 if (current_this)
9370 *where_found = current_this;
9371 else
9372 {
9373 static_ref_err (qual_wfl, DECL_NAME (decl),
9374 current_class);
9375 return 1;
9376 }
f0f3a777
APB
9377 if (outer_field_access_p (current_class, decl))
9378 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9379 }
c877974e
APB
9380 else
9381 {
9382 *where_found = TREE_TYPE (decl);
9383 if (TREE_CODE (*where_found) == POINTER_TYPE)
9384 *where_found = TREE_TYPE (*where_found);
9385 }
5e942c50 9386 }
b67d701b 9387 }
e04a16fb
AG
9388
9389 /* We have to search for a field, knowing the type of its
9390 container. The flag FROM_TYPE indicates that we resolved
9391 the last member of the expression as a type name, which
5e942c50
APB
9392 means that for the resolution of this field, we'll look
9393 for other errors than if it was resolved as a member of
9394 an other field. */
e04a16fb
AG
9395 else
9396 {
9397 int is_static;
5e942c50
APB
9398 tree field_decl_type; /* For layout */
9399
e04a16fb
AG
9400 if (!from_type && !JREFERENCE_TYPE_P (type))
9401 {
9402 parse_error_context
9403 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9404 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9405 lang_printable_name (type, 0),
e04a16fb
AG
9406 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9407 return 1;
9408 }
9409
dc0b3eff
PB
9410 field_decl = lookup_field_wrapper (type,
9411 EXPR_WFL_NODE (qual_wfl));
4dbf4496 9412
863cd85a
APB
9413 /* Maybe what we're trying to access to is an inner
9414 class, only if decl is a TYPE_DECL. */
9415 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
4dbf4496
APB
9416 {
9417 tree ptr, inner_decl;
9418
9419 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9420 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9421 if (inner_decl)
9422 {
9423 check_inner_class_access (inner_decl, decl, qual_wfl);
9424 type = TREE_TYPE (inner_decl);
9425 decl = inner_decl;
9426 from_type = 1;
9427 continue;
9428 }
9429 }
9430
dc0b3eff 9431 if (field_decl == NULL_TREE)
e04a16fb
AG
9432 {
9433 parse_error_context
2aa11e97 9434 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9435 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9436 GET_TYPE_NAME (type));
e04a16fb
AG
9437 return 1;
9438 }
dc0b3eff
PB
9439 if (field_decl == error_mark_node)
9440 return 1;
5e942c50
APB
9441
9442 /* Layout the type of field_decl, since we may need
c877974e
APB
9443 it. Don't do primitive types or loaded classes. The
9444 situation of non primitive arrays may not handled
9445 properly here. FIXME */
5e942c50
APB
9446 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9447 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9448 else
9449 field_decl_type = TREE_TYPE (field_decl);
9450 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9451 && !CLASS_LOADED_P (field_decl_type)
9452 && !TYPE_ARRAY_P (field_decl_type))
9453 resolve_and_layout (field_decl_type, NULL_TREE);
9454 if (TYPE_ARRAY_P (field_decl_type))
9455 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9456
9457 /* Check on accessibility here */
e101152f
APB
9458 if (not_accessible_p (current_class, field_decl,
9459 TREE_TYPE (decl), from_super))
e04a16fb
AG
9460 {
9461 parse_error_context
9462 (qual_wfl,
9463 "Can't access %s field `%s.%s' from `%s'",
9464 java_accstring_lookup
9465 (get_access_flags_from_decl (field_decl)),
2aa11e97 9466 GET_TYPE_NAME (type),
e04a16fb
AG
9467 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9468 IDENTIFIER_POINTER
9469 (DECL_NAME (TYPE_NAME (current_class))));
9470 return 1;
9471 }
5e942c50 9472 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9473
9474 /* There are things to check when fields are accessed
9475 from type. There are no restrictions on a static
9476 declaration of the field when it is accessed from an
9477 interface */
9478 is_static = FIELD_STATIC (field_decl);
9479 if (!from_super && from_type
c2952b01
APB
9480 && !TYPE_INTERFACE_P (type)
9481 && !is_static
9482 && (current_function_decl
9483 && METHOD_STATIC (current_function_decl)))
e04a16fb 9484 {
7f10c2e2 9485 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9486 return 1;
9487 }
9488 from_cast = from_super = 0;
9489
c2952b01
APB
9490 /* It's an access from a type but it isn't static, we
9491 make it relative to `this'. */
9492 if (!is_static && from_type)
9493 decl = current_this;
9494
5e942c50
APB
9495 /* If we need to generate something to get a proper
9496 handle on what this field is accessed from, do it
9497 now. */
e04a16fb
AG
9498 if (!is_static)
9499 {
c583dd46 9500 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9501 if (decl == error_mark_node)
9502 return 1;
9503 }
9504
9505 /* We want to keep the location were found it, and the type
9506 we found. */
9507 *where_found = decl;
9508 *type_found = type;
9509
c2952b01
APB
9510 /* Generate the correct expression for field access from
9511 qualified this */
9512 if (from_qualified_this)
9513 {
9514 field_decl = build_outer_field_access (qual_wfl, field_decl);
9515 from_qualified_this = 0;
9516 }
9517
e04a16fb
AG
9518 /* This is the decl found and eventually the next one to
9519 search from */
9520 decl = field_decl;
9521 }
e04a16fb
AG
9522 from_type = 0;
9523 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9524
9525 /* Sneak preview. If decl is qualified by a `new', report
9526 the error here to be accurate on the peculiar construct */
9527 if (TREE_CHAIN (q)
9528 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9529 && !JREFERENCE_TYPE_P (type))
9530 {
9531 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9532 lang_printable_name (type, 0));
9533 return 1;
9534 }
e04a16fb 9535 }
dde1da72
APB
9536 /* `q' might have changed due to a after package resolution
9537 re-qualification */
9538 if (!q)
9539 break;
e04a16fb
AG
9540 }
9541 *found_decl = decl;
9542 return 0;
9543}
9544
9545/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
e101152f
APB
9546 can't be accessed from REFERENCE (a record type). If MEMBER
9547 features a protected access, we then use WHERE which, if non null,
9548 holds the type of MEMBER's access that is checked against
9549 6.6.2.1. This function should be used when decl is a field or a
9550 method. */
e04a16fb 9551
be245ac0 9552static int
e101152f 9553not_accessible_p (reference, member, where, from_super)
e04a16fb 9554 tree reference, member;
e101152f 9555 tree where;
e04a16fb
AG
9556 int from_super;
9557{
9558 int access_flag = get_access_flags_from_decl (member);
9559
4dbf4496
APB
9560 /* Inner classes are processed by check_inner_class_access */
9561 if (INNER_CLASS_TYPE_P (reference))
9562 return 0;
9563
e04a16fb
AG
9564 /* Access always granted for members declared public */
9565 if (access_flag & ACC_PUBLIC)
9566 return 0;
9567
9568 /* Check access on protected members */
9569 if (access_flag & ACC_PROTECTED)
9570 {
9571 /* Access granted if it occurs from within the package
9572 containing the class in which the protected member is
9573 declared */
9574 if (class_in_current_package (DECL_CONTEXT (member)))
9575 return 0;
9576
9bbc7d9f
PB
9577 /* If accessed with the form `super.member', then access is granted */
9578 if (from_super)
9579 return 0;
e04a16fb 9580
e101152f
APB
9581 /* If where is active, access was made through a
9582 qualifier. Access is granted if the type of the qualifier is
9583 or is a sublass of the type the access made from (6.6.2.1.) */
9584 if (where && !inherits_from_p (where, reference))
9585 return 1;
9586
9bbc7d9f 9587 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9588 member is declared or a subclass of it. Find the right
9589 context to perform the check */
9590 if (PURE_INNER_CLASS_TYPE_P (reference))
9591 {
9592 while (INNER_CLASS_TYPE_P (reference))
9593 {
9594 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9595 return 0;
9596 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9597 }
9598 }
473e7b07 9599 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9600 return 0;
e04a16fb
AG
9601 return 1;
9602 }
9603
9604 /* Check access on private members. Access is granted only if it
761491c8
APB
9605 occurs from within the class in which it is declared -- that does
9606 it for innerclasses too. */
e04a16fb 9607 if (access_flag & ACC_PRIVATE)
761491c8
APB
9608 {
9609 if (reference == DECL_CONTEXT (member))
9610 return 0;
9611 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9612 return 0;
9613 return 1;
9614 }
e04a16fb
AG
9615
9616 /* Default access are permitted only when occuring within the
9617 package in which the type (REFERENCE) is declared. In other words,
9618 REFERENCE is defined in the current package */
9619 if (ctxp->package)
9620 return !class_in_current_package (reference);
473e7b07 9621
e04a16fb
AG
9622 /* Otherwise, access is granted */
9623 return 0;
9624}
9625
5e942c50
APB
9626/* Test deprecated decl access. */
9627static void
9628check_deprecation (wfl, decl)
9629 tree wfl, decl;
9630{
49f48c71 9631 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9632 /* Complain if the field is deprecated and the file it was defined
9633 in isn't compiled at the same time the file which contains its
9634 use is */
9635 if (DECL_DEPRECATED (decl)
9636 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9637 {
9638 char the [20];
9639 switch (TREE_CODE (decl))
9640 {
9641 case FUNCTION_DECL:
9642 strcpy (the, "method");
9643 break;
9644 case FIELD_DECL:
9645 strcpy (the, "field");
9646 break;
9647 case TYPE_DECL:
9648 strcpy (the, "class");
9649 break;
15fdcfe9 9650 default:
400500c4 9651 abort ();
5e942c50
APB
9652 }
9653 parse_warning_context
9654 (wfl, "The %s `%s' in class `%s' has been deprecated",
9655 the, lang_printable_name (decl, 0),
9656 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9657 }
9658}
9659
e04a16fb
AG
9660/* Returns 1 if class was declared in the current package, 0 otherwise */
9661
9662static int
9663class_in_current_package (class)
9664 tree class;
9665{
9666 static tree cache = NULL_TREE;
9667 int qualified_flag;
9668 tree left;
9669
9670 if (cache == class)
9671 return 1;
9672
9673 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9674
9675 /* If the current package is empty and the name of CLASS is
9676 qualified, class isn't in the current package. If there is a
9677 current package and the name of the CLASS is not qualified, class
9678 isn't in the current package */
0a2138e2 9679 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9680 return 0;
9681
9682 /* If there is not package and the name of CLASS isn't qualified,
9683 they belong to the same unnamed package */
9684 if (!ctxp->package && !qualified_flag)
9685 return 1;
9686
9687 /* Compare the left part of the name of CLASS with the package name */
9688 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9689 if (ctxp->package == left)
9690 {
19e223db
MM
9691 static int initialized_p;
9692 /* Register CACHE with the garbage collector. */
9693 if (!initialized_p)
9694 {
9695 ggc_add_tree_root (&cache, 1);
9696 initialized_p = 1;
9697 }
9698
e04a16fb
AG
9699 cache = class;
9700 return 1;
9701 }
9702 return 0;
9703}
9704
9705/* This function may generate code to access DECL from WHERE. This is
9706 done only if certain conditions meet. */
9707
9708static tree
9709maybe_access_field (decl, where, type)
9710 tree decl, where, type;
9711{
5e942c50
APB
9712 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9713 && !FIELD_STATIC (decl))
e04a16fb 9714 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9715 (type ? type : DECL_CONTEXT (decl)),
9716 DECL_NAME (decl));
e04a16fb
AG
9717 return decl;
9718}
9719
15fdcfe9 9720/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9721 and according to the situation, PRIMARY and WHERE may be
9722 used. IS_STATIC is set to 1 if the invoked function is static. */
9723
9724static tree
e101152f
APB
9725patch_method_invocation (patch, primary, where, from_super,
9726 is_static, ret_decl)
e04a16fb 9727 tree patch, primary, where;
e101152f 9728 int from_super;
e04a16fb 9729 int *is_static;
b9f7e36c 9730 tree *ret_decl;
e04a16fb
AG
9731{
9732 tree wfl = TREE_OPERAND (patch, 0);
9733 tree args = TREE_OPERAND (patch, 1);
9734 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9735 tree list;
22eed1e6 9736 int is_static_flag = 0;
89e09b9a 9737 int is_super_init = 0;
bccaf73a 9738 tree this_arg = NULL_TREE;
35ab11f0 9739 int is_array_clone_call = 0;
e04a16fb
AG
9740
9741 /* Should be overriden if everything goes well. Otherwise, if
9742 something fails, it should keep this value. It stop the
9743 evaluation of a bogus assignment. See java_complete_tree,
9744 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9745 evaluating an assignment */
9746 TREE_TYPE (patch) = error_mark_node;
9747
9748 /* Since lookup functions are messing with line numbers, save the
9749 context now. */
9750 java_parser_context_save_global ();
9751
9752 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9753
9754 /* Resolution of qualified name, excluding constructors */
9755 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9756 {
dde1da72 9757 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9758 /* Extract the last IDENTIFIER of the qualified
9759 expression. This is a wfl and we will use it's location
9760 data during error report. */
9761 identifier_wfl = cut_identifier_in_qualified (wfl);
9762 identifier = EXPR_WFL_NODE (identifier_wfl);
9763
9764 /* Given the context, IDENTIFIER is syntactically qualified
9765 as a MethodName. We need to qualify what's before */
9766 qualify_ambiguous_name (wfl);
dde1da72 9767 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9768
dde1da72
APB
9769 if (resolved == error_mark_node)
9770 PATCH_METHOD_RETURN_ERROR ();
9771
9772 type = GET_SKIP_TYPE (resolved);
9773 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9774
9775 if (JPRIMITIVE_TYPE_P (type))
9776 {
7e51098e
TT
9777 parse_error_context
9778 (identifier_wfl,
9779 "Can't invoke a method on primitive type `%s'",
9780 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9781 PATCH_METHOD_RETURN_ERROR ();
9782 }
9783
dde1da72
APB
9784 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9785 args = nreverse (args);
2c56429a 9786
e04a16fb 9787 /* We're resolving a call from a type */
dde1da72 9788 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9789 {
dde1da72 9790 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9791 {
9792 parse_error_context
781b0558
KG
9793 (identifier_wfl,
9794 "Can't make static reference to method `%s' in interface `%s'",
9795 IDENTIFIER_POINTER (identifier),
e04a16fb 9796 IDENTIFIER_POINTER (name));
b9f7e36c 9797 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9798 }
e04a16fb
AG
9799 if (list && !METHOD_STATIC (list))
9800 {
c2e3db92 9801 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9802 parse_error_context
9803 (identifier_wfl,
9804 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9805 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9806 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9807 free (fct_name);
b9f7e36c 9808 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9809 }
9810 }
e04a16fb 9811 else
dde1da72
APB
9812 this_arg = primary = resolved;
9813
35ab11f0
BM
9814 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9815 is_array_clone_call = 1;
9816
5e942c50 9817 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9818 wfl = identifier_wfl;
9819 }
9820 /* Resolution of simple names, names generated after a primary: or
9821 constructors */
9822 else
9823 {
cd531a2e 9824 tree class_to_search = NULL_TREE;
c2952b01 9825 int lc; /* Looking for Constructor */
e04a16fb
AG
9826
9827 /* We search constructor in their target class */
9828 if (CALL_CONSTRUCTOR_P (patch))
9829 {
22eed1e6
APB
9830 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9831 class_to_search = EXPR_WFL_NODE (wfl);
9832 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9833 this_identifier_node)
9834 class_to_search = NULL_TREE;
9835 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9836 super_identifier_node)
e04a16fb 9837 {
89e09b9a 9838 is_super_init = 1;
22eed1e6
APB
9839 if (CLASSTYPE_SUPER (current_class))
9840 class_to_search =
9841 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9842 else
9843 {
781b0558 9844 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9845 PATCH_METHOD_RETURN_ERROR ();
9846 }
e04a16fb 9847 }
22eed1e6
APB
9848
9849 /* Class to search is NULL if we're searching the current one */
9850 if (class_to_search)
e04a16fb 9851 {
c2952b01
APB
9852 class_to_search = resolve_and_layout (class_to_search, wfl);
9853
22eed1e6
APB
9854 if (!class_to_search)
9855 {
9856 parse_error_context
9857 (wfl, "Class `%s' not found in type declaration",
9858 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9859 PATCH_METHOD_RETURN_ERROR ();
9860 }
9861
5e942c50
APB
9862 /* Can't instantiate an abstract class, but we can
9863 invoke it's constructor. It's use within the `new'
9864 context is denied here. */
9865 if (CLASS_ABSTRACT (class_to_search)
9866 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9867 {
9868 parse_error_context
781b0558
KG
9869 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9870 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9871 PATCH_METHOD_RETURN_ERROR ();
9872 }
c2952b01 9873
22eed1e6 9874 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9875 }
22eed1e6
APB
9876 else
9877 class_to_search = current_class;
e04a16fb
AG
9878 lc = 1;
9879 }
9880 /* This is a regular search in the local class, unless an
9881 alternate class is specified. */
9882 else
9883 {
9884 class_to_search = (where ? where : current_class);
9885 lc = 0;
9886 }
c2952b01 9887
e04a16fb
AG
9888 /* NAME is a simple identifier or comes from a primary. Search
9889 in the class whose declaration contain the method being
9890 invoked. */
c877974e 9891 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9892
c2952b01 9893 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9894 /* Don't continue if no method were found, as the next statement
9895 can't be executed then. */
b9f7e36c
APB
9896 if (!list)
9897 PATCH_METHOD_RETURN_ERROR ();
35ab11f0
BM
9898
9899 if (TYPE_ARRAY_P (class_to_search)
9900 && DECL_NAME (list) == get_identifier ("clone"))
9901 is_array_clone_call = 1;
e04a16fb
AG
9902
9903 /* Check for static reference if non static methods */
9904 if (check_for_static_method_reference (wfl, patch, list,
9905 class_to_search, primary))
b9f7e36c 9906 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9907
165f37bc
APB
9908 /* Check for inner classes creation from illegal contexts */
9909 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9910 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9911 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9912 {
9913 parse_error_context
9914 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9915 lang_printable_name (class_to_search, 0),
9916 (!current_this ? "" :
9917 "; an explicit one must be provided when creating this inner class"));
9918 PATCH_METHOD_RETURN_ERROR ();
9919 }
9920
22eed1e6
APB
9921 /* Non static methods are called with the current object extra
9922 argument. If patch a `new TYPE()', the argument is the value
9923 returned by the object allocator. If method is resolved as a
9924 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9925 args = nreverse (args);
bccaf73a 9926 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9927 {
9928 this_arg = primary ? primary : current_this;
9929
9930 /* If we're using an access method, things are different.
9931 There are two familly of cases:
9932
9933 1) We're not generating bytecodes:
9934
9935 - LIST is non static. It's invocation is transformed from
9936 x(a1,...,an) into this$<n>.x(a1,....an).
9937 - LIST is static. It's invocation is transformed from
9938 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9939
9940 2) We're generating bytecodes:
9941
9942 - LIST is non static. It's invocation is transformed from
9943 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9944 - LIST is static. It's invocation is transformed from
9945 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9946
9947 Of course, this$<n> can be abitrary complex, ranging from
9948 this$0 (the immediate outer context) to
9949 access$0(access$0(...(this$0))).
9950
9951 maybe_use_access_method returns a non zero value if the
dfb99c83 9952 this_arg has to be moved into the (then generated) stub
4dbf4496 9953 argument list. In the meantime, the selected function
dfb99c83 9954 might have be replaced by a generated stub. */
c2952b01 9955 if (maybe_use_access_method (is_super_init, &list, &this_arg))
2cb3951d
APB
9956 {
9957 args = tree_cons (NULL_TREE, this_arg, args);
9958 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9959 }
c2952b01 9960 }
e04a16fb 9961 }
b67d701b 9962
e04a16fb
AG
9963 /* Merge point of all resolution schemes. If we have nothing, this
9964 is an error, already signaled */
b9f7e36c
APB
9965 if (!list)
9966 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9967
e04a16fb
AG
9968 /* Check accessibility, position the is_static flag, build and
9969 return the call */
e101152f
APB
9970 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9971 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
35ab11f0
BM
9972 NULL_TREE), from_super)
9973 /* Calls to clone() on array types are permitted as a special-case. */
9974 && !is_array_clone_call)
e101152f
APB
9975 {
9976 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9977 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9978 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9979 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9980 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9981 ? "constructor" : "method");
9982 /* FIXME: WFL yields the wrong message here but I don't know
9983 what else to use. */
9984 parse_error_context (wfl,
9985 "Can't access %s %s `%s.%s' from `%s'",
9986 access, what, klass, fct_name, refklass);
b9f7e36c 9987 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9988 }
5e942c50 9989 check_deprecation (wfl, list);
22eed1e6 9990
c2952b01
APB
9991 /* If invoking a innerclass constructor, there are hidden parameters
9992 to pass */
9993 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9994 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9995 {
9996 /* And make sure we add the accessed local variables to be saved
9997 in field aliases. */
9998 args = build_alias_initializer_parameter_list
9999 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10000
da632f2c 10001 /* Secretly pass the current_this/primary as a second argument */
165f37bc 10002 if (primary || current_this)
f8b93ea7
APB
10003 {
10004 tree extra_arg;
10005 tree this_type = (current_this ?
10006 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10007 /* Method's (list) enclosing context */
10008 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10009 /* If we have a primary, use it. */
10010 if (primary)
10011 extra_arg = primary;
10012 /* The current `this' is an inner class but isn't a direct
10013 enclosing context for the inner class we're trying to
10014 create. Build an access to the proper enclosing context
10015 and use it. */
10016 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10017 && this_type != TREE_TYPE (mec))
10018 {
10019
10020 extra_arg = build_access_to_thisn (current_class,
10021 TREE_TYPE (mec), 0);
10022 extra_arg = java_complete_tree (extra_arg);
10023 }
10024 /* Otherwise, just use the current `this' as an enclosing
10025 context. */
10026 else
10027 extra_arg = current_this;
10028 args = tree_cons (NULL_TREE, extra_arg, args);
10029 }
165f37bc
APB
10030 else
10031 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
10032 }
10033
152de068
APB
10034 /* This handles the situation where a constructor invocation needs
10035 to have an enclosing context passed as a second parameter (the
10036 constructor is one of an inner class. We extract it from the
10037 current function. */
10038 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10039 {
10040 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10041 tree extra_arg;
10042
10043 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10044 {
10045 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10046 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10047 }
10048 else
10049 {
10050 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10051 extra_arg =
10052 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10053 extra_arg = java_complete_tree (extra_arg);
10054 }
10055 args = tree_cons (NULL_TREE, extra_arg, args);
10056 }
10057
22eed1e6 10058 is_static_flag = METHOD_STATIC (list);
dba41d30 10059 if (! is_static_flag && this_arg != NULL_TREE)
bccaf73a 10060 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 10061
c3f2a476
APB
10062 /* In the context of an explicit constructor invocation, we can't
10063 invoke any method relying on `this'. Exceptions are: we're
10064 invoking a static function, primary exists and is not the current
10065 this, we're creating a new object. */
22eed1e6 10066 if (ctxp->explicit_constructor_p
c3f2a476
APB
10067 && !is_static_flag
10068 && (!primary || primary == current_this)
10069 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 10070 {
781b0558 10071 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
10072 PATCH_METHOD_RETURN_ERROR ();
10073 }
e04a16fb 10074 java_parser_context_restore_global ();
22eed1e6
APB
10075 if (is_static)
10076 *is_static = is_static_flag;
b9f7e36c
APB
10077 /* Sometimes, we want the decl of the selected method. Such as for
10078 EH checking */
10079 if (ret_decl)
10080 *ret_decl = list;
89e09b9a
PB
10081 patch = patch_invoke (patch, list, args);
10082 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10083 {
c2952b01
APB
10084 tree finit_parms, finit_call;
10085
c00f0fb2 10086 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
10087 finit_parms = build_alias_initializer_parameter_list
10088 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 10089
c2952b01
APB
10090 finit_call =
10091 build_method_invocation (build_wfl_node (finit_identifier_node),
10092 finit_parms);
10093
10094 /* Generate the code used to initialize fields declared with an
10095 initialization statement and build a compound statement along
10096 with the super constructor invocation. */
89e09b9a
PB
10097 patch = build (COMPOUND_EXPR, void_type_node, patch,
10098 java_complete_tree (finit_call));
10099 CAN_COMPLETE_NORMALLY (patch) = 1;
10100 }
10101 return patch;
e04a16fb
AG
10102}
10103
10104/* Check that we're not trying to do a static reference to a method in
10105 non static method. Return 1 if it's the case, 0 otherwise. */
10106
10107static int
10108check_for_static_method_reference (wfl, node, method, where, primary)
10109 tree wfl, node, method, where, primary;
10110{
10111 if (METHOD_STATIC (current_function_decl)
10112 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10113 {
c2e3db92 10114 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
10115 parse_error_context
10116 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 10117 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
10118 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10119 free (fct_name);
10120 return 1;
10121 }
10122 return 0;
10123}
10124
c2952b01
APB
10125/* Fix the invocation of *MDECL if necessary in the case of a
10126 invocation from an inner class. *THIS_ARG might be modified
10127 appropriately and an alternative access to *MDECL might be
10128 returned. */
10129
10130static int
10131maybe_use_access_method (is_super_init, mdecl, this_arg)
10132 int is_super_init;
10133 tree *mdecl, *this_arg;
10134{
10135 tree ctx;
10136 tree md = *mdecl, ta = *this_arg;
10137 int to_return = 0;
10138 int non_static_context = !METHOD_STATIC (md);
10139
10140 if (is_super_init
165f37bc
APB
10141 || DECL_CONTEXT (md) == current_class
10142 || !PURE_INNER_CLASS_TYPE_P (current_class)
10143 || DECL_FINIT_P (md))
c2952b01
APB
10144 return 0;
10145
10146 /* If we're calling a method found in an enclosing class, generate
10147 what it takes to retrieve the right this. Don't do that if we're
2cb3951d
APB
10148 invoking a static method. Note that if MD's type is unrelated to
10149 CURRENT_CLASS, then the current this can be used. */
c2952b01 10150
2cb3951d 10151 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
c2952b01
APB
10152 {
10153 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10154 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10155 {
10156 ta = build_current_thisn (current_class);
10157 ta = build_wfl_node (ta);
10158 }
10159 else
10160 {
10161 tree type = ctx;
10162 while (type)
10163 {
10164 maybe_build_thisn_access_method (type);
4dbf4496 10165 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10166 {
10167 ta = build_access_to_thisn (ctx, type, 0);
10168 break;
10169 }
10170 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10171 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10172 }
10173 }
10174 ta = java_complete_tree (ta);
10175 }
10176
10177 /* We might have to use an access method to get to MD. We can
10178 break the method access rule as far as we're not generating
10179 bytecode */
10180 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10181 {
10182 md = build_outer_method_access_method (md);
10183 to_return = 1;
10184 }
10185
10186 *mdecl = md;
10187 *this_arg = ta;
10188
10189 /* Returnin a non zero value indicates we were doing a non static
10190 method invokation that is now a static invocation. It will have
10191 callee displace `this' to insert it in the regular argument
10192 list. */
10193 return (non_static_context && to_return);
10194}
10195
e04a16fb
AG
10196/* Patch an invoke expression METHOD and ARGS, based on its invocation
10197 mode. */
10198
10199static tree
89e09b9a 10200patch_invoke (patch, method, args)
e04a16fb 10201 tree patch, method, args;
e04a16fb
AG
10202{
10203 tree dtable, func;
0a2138e2 10204 tree original_call, t, ta;
e815887f 10205 tree cond = NULL_TREE;
e04a16fb 10206
5e942c50
APB
10207 /* Last step for args: convert build-in types. If we're dealing with
10208 a new TYPE() type call, the first argument to the constructor
e815887f 10209 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10210 `new' */
10211 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10212 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10213 t = TREE_CHAIN (t);
ac825856
APB
10214 for (ta = args; t != end_params_node && ta;
10215 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10216 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10217 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10218 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10219
10220 /* Resolve unresolved returned type isses */
10221 t = TREE_TYPE (TREE_TYPE (method));
10222 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10223 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10224
e8fc7396 10225 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10226 func = method;
10227 else
e04a16fb 10228 {
15fdcfe9 10229 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10230 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10231 {
10232 case INVOKE_VIRTUAL:
10233 dtable = invoke_build_dtable (0, args);
10234 func = build_invokevirtual (dtable, method);
10235 break;
b9f7e36c 10236
e815887f
TT
10237 case INVOKE_NONVIRTUAL:
10238 /* If the object for the method call is null, we throw an
10239 exception. We don't do this if the object is the current
10240 method's `this'. In other cases we just rely on an
10241 optimization pass to eliminate redundant checks. */
10242 if (TREE_VALUE (args) != current_this)
10243 {
10244 /* We use a SAVE_EXPR here to make sure we only evaluate
10245 the new `self' expression once. */
10246 tree save_arg = save_expr (TREE_VALUE (args));
10247 TREE_VALUE (args) = save_arg;
10248 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10249 null_pointer_node);
10250 }
10251 /* Fall through. */
10252
15fdcfe9
PB
10253 case INVOKE_SUPER:
10254 case INVOKE_STATIC:
10255 func = build_known_method_ref (method, TREE_TYPE (method),
10256 DECL_CONTEXT (method),
10257 signature, args);
10258 break;
e04a16fb 10259
15fdcfe9
PB
10260 case INVOKE_INTERFACE:
10261 dtable = invoke_build_dtable (1, args);
173f556c 10262 func = build_invokeinterface (dtable, method);
15fdcfe9 10263 break;
5e942c50 10264
15fdcfe9 10265 default:
400500c4 10266 abort ();
15fdcfe9
PB
10267 }
10268
10269 /* Ensure self_type is initialized, (invokestatic). FIXME */
10270 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10271 }
10272
e04a16fb
AG
10273 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10274 TREE_OPERAND (patch, 0) = func;
10275 TREE_OPERAND (patch, 1) = args;
10276 original_call = patch;
10277
e815887f 10278 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10279 returned value is the first argument to the constructor. We build
10280 a COMPOUND_EXPR and use saved expression so that the overall NEW
10281 expression value is a pointer to a newly created and initialized
10282 class. */
10283 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10284 {
10285 tree class = DECL_CONTEXT (method);
10286 tree c1, saved_new, size, new;
e8fc7396 10287 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10288 {
10289 TREE_TYPE (patch) = build_pointer_type (class);
10290 return patch;
10291 }
e04a16fb
AG
10292 if (!TYPE_SIZE (class))
10293 safe_layout_class (class);
10294 size = size_in_bytes (class);
10295 new = build (CALL_EXPR, promote_type (class),
10296 build_address_of (alloc_object_node),
10297 tree_cons (NULL_TREE, build_class_ref (class),
10298 build_tree_list (NULL_TREE,
10299 size_in_bytes (class))),
10300 NULL_TREE);
10301 saved_new = save_expr (new);
10302 c1 = build_tree_list (NULL_TREE, saved_new);
10303 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10304 TREE_OPERAND (original_call, 1) = c1;
10305 TREE_SET_CODE (original_call, CALL_EXPR);
10306 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10307 }
e815887f
TT
10308
10309 /* If COND is set, then we are building a check to see if the object
10310 is NULL. */
10311 if (cond != NULL_TREE)
10312 {
10313 /* We have to make the `then' branch a compound expression to
10314 make the types turn out right. This seems bizarre. */
10315 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10316 build (COMPOUND_EXPR, TREE_TYPE (patch),
10317 build (CALL_EXPR, void_type_node,
10318 build_address_of (soft_nullpointer_node),
10319 NULL_TREE, NULL_TREE),
10320 (FLOAT_TYPE_P (TREE_TYPE (patch))
10321 ? build_real (TREE_TYPE (patch), dconst0)
10322 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10323 integer_zero_node))),
10324 patch);
10325 TREE_SIDE_EFFECTS (patch) = 1;
10326 }
10327
e04a16fb
AG
10328 return patch;
10329}
10330
10331static int
10332invocation_mode (method, super)
10333 tree method;
10334 int super;
10335{
10336 int access = get_access_flags_from_decl (method);
10337
22eed1e6
APB
10338 if (super)
10339 return INVOKE_SUPER;
10340
e815887f 10341 if (access & ACC_STATIC)
e04a16fb
AG
10342 return INVOKE_STATIC;
10343
e815887f
TT
10344 /* We have to look for a constructor before we handle nonvirtual
10345 calls; otherwise the constructor will look nonvirtual. */
10346 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10347 return INVOKE_STATIC;
e815887f
TT
10348
10349 if (access & ACC_FINAL || access & ACC_PRIVATE)
10350 return INVOKE_NONVIRTUAL;
10351
10352 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10353 return INVOKE_NONVIRTUAL;
10354
e04a16fb
AG
10355 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10356 return INVOKE_INTERFACE;
22eed1e6 10357
e04a16fb
AG
10358 return INVOKE_VIRTUAL;
10359}
10360
b67d701b
PB
10361/* Retrieve a refined list of matching methods. It covers the step
10362 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10363
10364static tree
10365lookup_method_invoke (lc, cl, class, name, arg_list)
10366 int lc;
10367 tree cl;
10368 tree class, name, arg_list;
10369{
de4c7b02 10370 tree atl = end_params_node; /* Arg Type List */
c877974e 10371 tree method, signature, list, node;
49f48c71 10372 const char *candidates; /* Used for error report */
b5b8a0e7 10373 char *dup;
e04a16fb 10374
5e942c50 10375 /* Fix the arguments */
e04a16fb
AG
10376 for (node = arg_list; node; node = TREE_CHAIN (node))
10377 {
e3884b71 10378 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10379 /* Non primitive type may have to be resolved */
e3884b71 10380 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10381 resolve_and_layout (current_arg, NULL_TREE);
10382 /* And promoted */
b67d701b 10383 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10384 current_arg = promote_type (current_arg);
5e942c50 10385 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10386 }
e04a16fb 10387
c2952b01
APB
10388 /* Presto. If we're dealing with an anonymous class and a
10389 constructor call, generate the right constructor now, since we
10390 know the arguments' types. */
10391
10392 if (lc && ANONYMOUS_CLASS_P (class))
10393 craft_constructor (TYPE_NAME (class), atl);
10394
5e942c50
APB
10395 /* Find all candidates and then refine the list, searching for the
10396 most specific method. */
10397 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10398 list = find_most_specific_methods_list (list);
b67d701b
PB
10399 if (list && !TREE_CHAIN (list))
10400 return TREE_VALUE (list);
e04a16fb 10401
b67d701b
PB
10402 /* Issue an error. List candidates if any. Candidates are listed
10403 only if accessible (non accessible methods may end-up here for
10404 the sake of a better error report). */
10405 candidates = NULL;
10406 if (list)
e04a16fb 10407 {
e04a16fb 10408 tree current;
b67d701b 10409 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10410 for (current = list; current; current = TREE_CHAIN (current))
10411 {
b67d701b
PB
10412 tree cm = TREE_VALUE (current);
10413 char string [4096];
e101152f 10414 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
b67d701b 10415 continue;
b67d701b 10416 sprintf
22eed1e6
APB
10417 (string, " `%s' in `%s'%s",
10418 get_printable_method_name (cm),
b67d701b
PB
10419 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10420 (TREE_CHAIN (current) ? "\n" : ""));
10421 obstack_grow (&temporary_obstack, string, strlen (string));
10422 }
10423 obstack_1grow (&temporary_obstack, '\0');
10424 candidates = obstack_finish (&temporary_obstack);
10425 }
10426 /* Issue the error message */
c877974e
APB
10427 method = make_node (FUNCTION_TYPE);
10428 TYPE_ARG_TYPES (method) = atl;
b67d701b 10429 signature = build_java_argument_signature (method);
c63b98cd 10430 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10431 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10432 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10433 (lc ? dup : IDENTIFIER_POINTER (name)),
10434 IDENTIFIER_POINTER (signature), dup,
b67d701b 10435 (candidates ? candidates : ""));
b5b8a0e7 10436 free (dup);
b67d701b
PB
10437 return NULL_TREE;
10438}
10439
5e942c50
APB
10440/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10441 when we're looking for a constructor. */
b67d701b
PB
10442
10443static tree
5e942c50
APB
10444find_applicable_accessible_methods_list (lc, class, name, arglist)
10445 int lc;
b67d701b
PB
10446 tree class, name, arglist;
10447{
ad69b5b6
BM
10448 static struct hash_table t, *searched_classes = NULL;
10449 static int search_not_done = 0;
b67d701b
PB
10450 tree list = NULL_TREE, all_list = NULL_TREE;
10451
ad69b5b6
BM
10452 /* Check the hash table to determine if this class has been searched
10453 already. */
10454 if (searched_classes)
10455 {
10456 if (hash_lookup (searched_classes,
10457 (const hash_table_key) class, FALSE, NULL))
10458 return NULL;
10459 }
10460 else
10461 {
10462 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10463 java_hash_compare_tree_node);
10464 searched_classes = &t;
10465 }
10466
10467 search_not_done++;
10468 hash_lookup (searched_classes,
0c2b8145 10469 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10470
c2952b01
APB
10471 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10472 {
10473 load_class (class, 1);
10474 safe_layout_class (class);
10475 }
10476
1982388a 10477 /* Search interfaces */
9a7ab4b3
APB
10478 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10479 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10480 {
1982388a
APB
10481 int i, n;
10482 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10483 search_applicable_methods_list (lc, TYPE_METHODS (class),
10484 name, arglist, &list, &all_list);
1982388a 10485 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10486 for (i = 1; i < n; i++)
b67d701b 10487 {
de0b553f
APB
10488 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10489 tree rlist;
10490
de0b553f
APB
10491 rlist = find_applicable_accessible_methods_list (lc, t, name,
10492 arglist);
165f37bc 10493 list = chainon (rlist, list);
e04a16fb 10494 }
e04a16fb 10495 }
1982388a
APB
10496 /* Search classes */
10497 else
c2952b01 10498 {
165f37bc
APB
10499 tree sc = class;
10500 int seen_inner_class = 0;
c2952b01
APB
10501 search_applicable_methods_list (lc, TYPE_METHODS (class),
10502 name, arglist, &list, &all_list);
10503
94807d33
APB
10504 /* When looking finit$ or class$, we turn LC to 1 so that we
10505 only search in class. Note that we should have found
10506 something at this point. */
10507 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
493d561d
APB
10508 {
10509 lc = 1;
10510 if (!list)
400500c4 10511 abort ();
493d561d
APB
10512 }
10513
165f37bc
APB
10514 /* We must search all interfaces of this class */
10515 if (!lc)
10516 {
10517 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10518 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10519 for (i = 1; i < n; i++)
10520 {
10521 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10522 if (t != object_type_node)
30a3caef
ZW
10523 {
10524 tree rlist
10525 = find_applicable_accessible_methods_list (lc, t,
10526 name, arglist);
10527 list = chainon (rlist, list);
10528 }
165f37bc 10529 }
165f37bc
APB
10530 }
10531
c2952b01
APB
10532 /* Search enclosing context of inner classes before looking
10533 ancestors up. */
10534 while (!lc && INNER_CLASS_TYPE_P (class))
10535 {
165f37bc
APB
10536 tree rlist;
10537 seen_inner_class = 1;
c2952b01 10538 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10539 rlist = find_applicable_accessible_methods_list (lc, class,
10540 name, arglist);
10541 list = chainon (rlist, list);
c2952b01 10542 }
165f37bc
APB
10543
10544 if (!lc && seen_inner_class
10545 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10546 class = CLASSTYPE_SUPER (sc);
10547 else
10548 class = sc;
10549
ad69b5b6
BM
10550 /* Search superclass */
10551 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10552 {
10553 tree rlist;
10554 class = CLASSTYPE_SUPER (class);
10555 rlist = find_applicable_accessible_methods_list (lc, class,
10556 name, arglist);
10557 list = chainon (rlist, list);
10558 }
10559 }
10560
10561 search_not_done--;
10562
10563 /* We're done. Reset the searched classes list and finally search
10564 java.lang.Object if it wasn't searched already. */
10565 if (!search_not_done)
10566 {
10567 if (!lc
10568 && TYPE_METHODS (object_type_node)
10569 && !hash_lookup (searched_classes,
10570 (const hash_table_key) object_type_node,
10571 FALSE, NULL))
10572 {
10573 search_applicable_methods_list (lc,
10574 TYPE_METHODS (object_type_node),
10575 name, arglist, &list, &all_list);
10576 }
10577 hash_table_free (searched_classes);
10578 searched_classes = NULL;
c2952b01 10579 }
1982388a 10580
b67d701b
PB
10581 /* Either return the list obtained or all selected (but
10582 inaccessible) methods for better error report. */
10583 return (!list ? all_list : list);
10584}
e04a16fb 10585
ad69b5b6 10586/* Effectively search for the appropriate method in method */
1982388a
APB
10587
10588static void
c2952b01 10589search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10590 int lc;
10591 tree method, name, arglist;
10592 tree *list, *all_list;
10593{
10594 for (; method; method = TREE_CHAIN (method))
10595 {
10596 /* When dealing with constructor, stop here, otherwise search
10597 other classes */
10598 if (lc && !DECL_CONSTRUCTOR_P (method))
10599 continue;
10600 else if (!lc && (DECL_CONSTRUCTOR_P (method)
c4faeb92 10601 || (DECL_NAME (method) != name)))
1982388a 10602 continue;
c7303e41 10603
1982388a
APB
10604 if (argument_types_convertible (method, arglist))
10605 {
10606 /* Retain accessible methods only */
10607 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
e101152f 10608 method, NULL_TREE, 0))
1982388a
APB
10609 *list = tree_cons (NULL_TREE, method, *list);
10610 else
10611 /* Also retain all selected method here */
10612 *all_list = tree_cons (NULL_TREE, method, *list);
10613 }
10614 }
ad69b5b6 10615}
1982388a 10616
b67d701b
PB
10617/* 15.11.2.2 Choose the Most Specific Method */
10618
10619static tree
10620find_most_specific_methods_list (list)
10621 tree list;
10622{
10623 int max = 0;
9a7ab4b3 10624 int abstract, candidates;
b67d701b
PB
10625 tree current, new_list = NULL_TREE;
10626 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10627 {
b67d701b
PB
10628 tree method;
10629 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10630
10631 for (method = list; method; method = TREE_CHAIN (method))
10632 {
0c2b8145 10633 tree method_v, current_v;
b67d701b
PB
10634 /* Don't test a method against itself */
10635 if (method == current)
10636 continue;
10637
0c2b8145
APB
10638 method_v = TREE_VALUE (method);
10639 current_v = TREE_VALUE (current);
10640
10641 /* Compare arguments and location where methods where declared */
10642 if (argument_types_convertible (method_v, current_v))
b67d701b 10643 {
0c2b8145
APB
10644 if (valid_method_invocation_conversion_p
10645 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10646 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10647 && enclosing_context_p (DECL_CONTEXT (method_v),
10648 DECL_CONTEXT (current_v))))
10649 {
10650 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10651 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10652 max = (v > max ? v : max);
10653 }
b67d701b
PB
10654 }
10655 }
e04a16fb
AG
10656 }
10657
b67d701b 10658 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10659 for (current = list, abstract = -1, candidates = -1;
10660 current; current = TREE_CHAIN (current))
b67d701b 10661 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10662 {
10663 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10664 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10665 candidates++;
10666 }
b67d701b 10667
165f37bc
APB
10668 /* If we have several and they're all abstract, just pick the
10669 closest one. */
9a7ab4b3
APB
10670 if (candidates > 0 && (candidates == abstract))
10671 {
10672 new_list = nreverse (new_list);
10673 TREE_CHAIN (new_list) = NULL_TREE;
10674 }
165f37bc 10675
cab8e2bd
BM
10676 /* We have several (we couldn't find a most specific), all but one
10677 are abstract, we pick the only non abstract one. */
10678 if (candidates > 0 && (candidates == abstract+1))
165f37bc 10679 {
9a7ab4b3
APB
10680 for (current = new_list; current; current = TREE_CHAIN (current))
10681 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10682 {
10683 TREE_CHAIN (current) = NULL_TREE;
10684 new_list = current;
10685 }
165f37bc
APB
10686 }
10687
b67d701b
PB
10688 /* If we can't find one, lower expectations and try to gather multiple
10689 maximally specific methods */
165f37bc 10690 while (!new_list && max)
b67d701b
PB
10691 {
10692 while (--max > 0)
10693 {
10694 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10695 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10696 }
b67d701b
PB
10697 }
10698
10699 return new_list;
e04a16fb
AG
10700}
10701
b67d701b
PB
10702/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10703 converted by method invocation conversion (5.3) to the type of the
10704 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10705 to change less often than M1. */
e04a16fb 10706
b67d701b
PB
10707static int
10708argument_types_convertible (m1, m2_or_arglist)
10709 tree m1, m2_or_arglist;
e04a16fb 10710{
b67d701b
PB
10711 static tree m2_arg_value = NULL_TREE;
10712 static tree m2_arg_cache = NULL_TREE;
19e223db 10713 static int initialized_p;
e04a16fb 10714
b67d701b 10715 register tree m1_arg, m2_arg;
e04a16fb 10716
19e223db
MM
10717 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10718 collector. */
10719 if (!initialized_p)
10720 {
10721 ggc_add_tree_root (&m2_arg_value, 1);
10722 ggc_add_tree_root (&m2_arg_cache, 1);
10723 initialized_p = 1;
10724 }
10725
c2952b01 10726 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10727
b67d701b
PB
10728 if (m2_arg_value == m2_or_arglist)
10729 m2_arg = m2_arg_cache;
10730 else
10731 {
10732 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10733 argument types */
10734 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10735 {
10736 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10737 if (!METHOD_STATIC (m2_or_arglist))
10738 m2_arg = TREE_CHAIN (m2_arg);
10739 }
10740 else
10741 m2_arg = m2_or_arglist;
e04a16fb 10742
b67d701b
PB
10743 m2_arg_value = m2_or_arglist;
10744 m2_arg_cache = m2_arg;
10745 }
e04a16fb 10746
de4c7b02 10747 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10748 {
c877974e 10749 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10750 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10751 TREE_VALUE (m2_arg)))
10752 break;
10753 m1_arg = TREE_CHAIN (m1_arg);
10754 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10755 }
de4c7b02 10756 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10757}
10758
10759/* Qualification routines */
10760
10761static void
10762qualify_ambiguous_name (id)
10763 tree id;
10764{
cd531a2e
KG
10765 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10766 saved_current_class;
d8fccff5 10767 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10768 int code;
e04a16fb
AG
10769
10770 /* We first qualify the first element, then derive qualification of
10771 others based on the first one. If the first element is qualified
10772 by a resolution (field or type), this resolution is stored in the
10773 QUAL_RESOLUTION of the qual element being examined. We need to
10774 save the current_class since the use of SUPER might change the
10775 its value. */
10776 saved_current_class = current_class;
10777 qual = EXPR_WFL_QUALIFICATION (id);
10778 do {
10779
10780 /* Simple qualified expression feature a qual_wfl that is a
10781 WFL. Expression derived from a primary feature more complicated
10782 things like a CALL_EXPR. Expression from primary need to be
10783 worked out to extract the part on which the qualification will
10784 take place. */
10785 qual_wfl = QUAL_WFL (qual);
10786 switch (TREE_CODE (qual_wfl))
10787 {
10788 case CALL_EXPR:
10789 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10790 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10791 {
10792 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10793 qual_wfl = QUAL_WFL (qual);
10794 }
10795 break;
d8fccff5 10796 case NEW_ARRAY_EXPR:
c2952b01 10797 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10798 qual = TREE_CHAIN (qual);
1a6d4fb7 10799 again = new_array_found = 1;
d8fccff5 10800 continue;
e04a16fb 10801 case CONVERT_EXPR:
f2760b27
APB
10802 break;
10803 case NEW_CLASS_EXPR:
e04a16fb
AG
10804 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10805 break;
c583dd46
APB
10806 case ARRAY_REF:
10807 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10808 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10809 break;
8576f094
APB
10810 case STRING_CST:
10811 qual = TREE_CHAIN (qual);
10812 qual_wfl = QUAL_WFL (qual);
10813 break;
165f37bc
APB
10814 case CLASS_LITERAL:
10815 qual = TREE_CHAIN (qual);
10816 qual_wfl = QUAL_WFL (qual);
10817 break;
0a2138e2
APB
10818 default:
10819 /* Fix for -Wall. Just break doing nothing */
10820 break;
e04a16fb 10821 }
8576f094 10822
e04a16fb
AG
10823 ptr_type = current_class;
10824 again = 0;
8576f094
APB
10825 code = TREE_CODE (qual_wfl);
10826
10827 /* Pos evaluation: non WFL leading expression nodes */
10828 if (code == CONVERT_EXPR
10829 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10830 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10831
cd7c5840
APB
10832 else if (code == INTEGER_CST)
10833 name = qual_wfl;
10834
ac22f9cb 10835 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10836 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10837 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10838
c2952b01
APB
10839 else if (code == TREE_LIST)
10840 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10841
37feda7d
APB
10842 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10843 || code == PLUS_EXPR)
8576f094
APB
10844 {
10845 qual = TREE_CHAIN (qual);
10846 qual_wfl = QUAL_WFL (qual);
10847 again = 1;
10848 }
10849 else
f441f671
APB
10850 {
10851 name = EXPR_WFL_NODE (qual_wfl);
10852 if (!name)
10853 {
10854 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10855 again = 1;
10856 }
10857 }
10858
e04a16fb
AG
10859 /* If we have a THIS (from a primary), we set the context accordingly */
10860 if (name == this_identifier_node)
10861 {
6e22695a
APB
10862 /* This isn't really elegant. One more added irregularity
10863 before I start using COMPONENT_REF (hopefully very soon.) */
10864 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10865 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10866 EXPR_WITH_FILE_LOCATION
10867 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10868 this_identifier_node)
10869 {
10870 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10871 qual = EXPR_WFL_QUALIFICATION (qual);
10872 }
e04a16fb
AG
10873 qual = TREE_CHAIN (qual);
10874 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10875 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10876 again = 1;
10877 else
10878 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10879 this_found = 1;
10880 }
10881 /* If we have a SUPER, we set the context accordingly */
10882 if (name == super_identifier_node)
10883 {
10884 current_class = CLASSTYPE_SUPER (ptr_type);
10885 /* Check that there is such a thing as a super class. If not,
10886 return. The error will be caught later on, during the
10887 resolution */
10888 if (!current_class)
10889 {
10890 current_class = saved_current_class;
10891 return;
10892 }
10893 qual = TREE_CHAIN (qual);
10894 /* Do one more interation to set things up */
10895 super_found = again = 1;
10896 }
10897 } while (again);
10898
f2760b27
APB
10899 /* If name appears within the scope of a local variable declaration
10900 or parameter declaration, then it is an expression name. We don't
10901 carry this test out if we're in the context of the use of SUPER
10902 or THIS */
cd7c5840
APB
10903 if (!this_found && !super_found
10904 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10905 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10906 {
10907 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10908 QUAL_RESOLUTION (qual) = decl;
10909 }
10910
10911 /* If within the class/interface NAME was found to be used there
10912 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10913 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10914 address length, it is OK. */
10915 else if ((decl = lookup_field_wrapper (ptr_type, name))
10916 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10917 {
10918 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10919 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10920 }
10921
1a6d4fb7 10922 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10923 - NAME is a class/interface declared within the compilation
10924 unit containing NAME,
10925 - NAME is imported via a single-type-import declaration,
10926 - NAME is declared in an another compilation unit of the package
10927 of the compilation unit containing NAME,
10928 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10929 of the compilation unit containing NAME.
10930 - NAME is actually a STRING_CST. */
cd7c5840
APB
10931 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10932 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10933 {
10934 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10935 QUAL_RESOLUTION (qual) = decl;
10936 }
10937
f2760b27 10938 /* Method call, array references and cast are expression name */
9bbc7d9f 10939 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10940 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10941 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10942 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10943
10944 /* Check here that NAME isn't declared by more than one
10945 type-import-on-demand declaration of the compilation unit
10946 containing NAME. FIXME */
10947
10948 /* Otherwise, NAME is reclassified as a package name */
10949 else
10950 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10951
10952 /* Propagate the qualification accross other components of the
10953 qualified name */
10954 for (qual = TREE_CHAIN (qual); qual;
10955 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10956 {
10957 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10958 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10959 else
10960 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10961 }
10962
10963 /* Store the global qualification for the ambiguous part of ID back
10964 into ID fields */
10965 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10966 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10967 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10968 RESOLVE_TYPE_NAME_P (id) = 1;
10969 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10970 RESOLVE_PACKAGE_NAME_P (id) = 1;
10971
10972 /* Restore the current class */
10973 current_class = saved_current_class;
10974}
10975
10976static int
10977breakdown_qualified (left, right, source)
10978 tree *left, *right, source;
10979{
63ad61ed 10980 char *p, *base;
e04a16fb
AG
10981 int l = IDENTIFIER_LENGTH (source);
10982
63ad61ed
ZW
10983 base = alloca (l + 1);
10984 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10985
e04a16fb 10986 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10987 p = base + l - 1;
e04a16fb
AG
10988 while (*p != '.' && p != base)
10989 p--;
10990
10991 /* We didn't find a '.'. Return an error */
10992 if (p == base)
10993 return 1;
10994
10995 *p = '\0';
10996 if (right)
10997 *right = get_identifier (p+1);
63ad61ed 10998 *left = get_identifier (base);
e04a16fb
AG
10999
11000 return 0;
11001}
11002
a648f4e4
BM
11003/* Return TRUE if two classes are from the same package. */
11004
11005static int
11006in_same_package (name1, name2)
11007 tree name1, name2;
11008{
11009 tree tmp;
11010 tree pkg1;
11011 tree pkg2;
11012
11013 if (TREE_CODE (name1) == TYPE_DECL)
11014 name1 = DECL_NAME (name1);
11015 if (TREE_CODE (name2) == TYPE_DECL)
11016 name2 = DECL_NAME (name2);
11017
11018 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11019 /* One in empty package. */
11020 return 0;
11021
11022 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11023 /* Both in empty package. */
11024 return 1;
11025
11026 breakdown_qualified (&pkg1, &tmp, name1);
11027 breakdown_qualified (&pkg2, &tmp, name2);
11028
11029 return (pkg1 == pkg2);
11030}
11031
e04a16fb 11032/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
11033 local variable decls if present.
11034 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
11035
11036static tree
11037java_complete_tree (node)
11038 tree node;
5b09b33e
PB
11039{
11040 node = java_complete_lhs (node);
c7303e41 11041 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
0c2b8145 11042 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 11043 && !flag_emit_xref)
5b09b33e
PB
11044 {
11045 tree value = DECL_INITIAL (node);
11046 DECL_INITIAL (node) = NULL_TREE;
11047 value = fold_constant_for_init (value, node);
11048 DECL_INITIAL (node) = value;
11049 if (value != NULL_TREE)
c2952b01
APB
11050 {
11051 /* fold_constant_for_init sometimes widen the original type
11052 of the constant (i.e. byte to int.) It's not desirable,
11053 especially if NODE is a function argument. */
11054 if (TREE_CODE (value) == INTEGER_CST
11055 && TREE_TYPE (node) != TREE_TYPE (value))
11056 return convert (TREE_TYPE (node), value);
11057 else
11058 return value;
11059 }
c7303e41
APB
11060 else
11061 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
11062 }
11063 return node;
11064}
11065
2aa11e97
APB
11066static tree
11067java_stabilize_reference (node)
11068 tree node;
11069{
11070 if (TREE_CODE (node) == COMPOUND_EXPR)
11071 {
11072 tree op0 = TREE_OPERAND (node, 0);
11073 tree op1 = TREE_OPERAND (node, 1);
642f15d1 11074 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
11075 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11076 return node;
11077 }
5cbdba64 11078 return stabilize_reference (node);
2aa11e97
APB
11079}
11080
5b09b33e
PB
11081/* Patch tree nodes in a function body. When a BLOCK is found, push
11082 local variable decls if present.
11083 Same as java_complete_tree, but does not resolve static finals to values. */
11084
11085static tree
11086java_complete_lhs (node)
11087 tree node;
e04a16fb 11088{
22eed1e6 11089 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 11090 int flag;
e04a16fb
AG
11091
11092 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 11093 worked out. */
e04a16fb
AG
11094 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11095 return node;
11096
11097 /* The switch block implements cases processing container nodes
11098 first. Contained nodes are always written back. Leaves come
11099 next and return a value. */
11100 switch (TREE_CODE (node))
11101 {
11102 case BLOCK:
11103
11104 /* 1- Block section.
11105 Set the local values on decl names so we can identify them
11106 faster when they're referenced. At that stage, identifiers
11107 are legal so we don't check for declaration errors. */
11108 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11109 {
11110 DECL_CONTEXT (cn) = current_function_decl;
11111 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 11112 }
15fdcfe9
PB
11113 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11114 CAN_COMPLETE_NORMALLY (node) = 1;
11115 else
e04a16fb 11116 {
15fdcfe9
PB
11117 tree stmt = BLOCK_EXPR_BODY (node);
11118 tree *ptr;
11119 int error_seen = 0;
11120 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11121 {
c877974e
APB
11122 /* Re-order from (((A; B); C); ...; Z) to
11123 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
11124 This makes it easier to scan the statements left-to-right
11125 without using recursion (which might overflow the stack
11126 if the block has many statements. */
11127 for (;;)
11128 {
11129 tree left = TREE_OPERAND (stmt, 0);
11130 if (TREE_CODE (left) != COMPOUND_EXPR)
11131 break;
11132 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11133 TREE_OPERAND (left, 1) = stmt;
11134 stmt = left;
11135 }
11136 BLOCK_EXPR_BODY (node) = stmt;
11137 }
11138
c877974e
APB
11139 /* Now do the actual complete, without deep recursion for
11140 long blocks. */
15fdcfe9 11141 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
11142 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11143 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
11144 {
11145 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11146 tree *next = &TREE_OPERAND (*ptr, 1);
11147 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
11148 if (cur == empty_stmt_node)
11149 {
11150 /* Optimization; makes it easier to detect empty bodies.
11151 Most useful for <clinit> with all-constant initializer. */
11152 *ptr = *next;
11153 continue;
11154 }
15fdcfe9
PB
11155 if (TREE_CODE (cur) == ERROR_MARK)
11156 error_seen++;
11157 else if (! CAN_COMPLETE_NORMALLY (cur))
11158 {
11159 wfl_op2 = *next;
11160 for (;;)
11161 {
11162 if (TREE_CODE (wfl_op2) == BLOCK)
11163 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11164 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11165 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11166 else
11167 break;
11168 }
11169 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 11170 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 11171 unreachable_stmt_error (*ptr);
15fdcfe9
PB
11172 }
11173 ptr = next;
11174 }
11175 *ptr = java_complete_tree (*ptr);
11176
11177 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11178 return error_mark_node;
15fdcfe9 11179 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11180 }
11181 /* Turn local bindings to null */
11182 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11183 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11184
11185 TREE_TYPE (node) = void_type_node;
11186 break;
11187
11188 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11189
b9f7e36c
APB
11190 case THROW_EXPR:
11191 wfl_op1 = TREE_OPERAND (node, 0);
11192 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11193 /* 14.19 A throw statement cannot complete normally. */
11194 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11195 return patch_throw_statement (node, wfl_op1);
11196
11197 case SYNCHRONIZED_EXPR:
11198 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11199 return patch_synchronized_statement (node, wfl_op1);
11200
b67d701b
PB
11201 case TRY_EXPR:
11202 return patch_try_statement (node);
11203
a7d8d81f
PB
11204 case TRY_FINALLY_EXPR:
11205 COMPLETE_CHECK_OP_0 (node);
11206 COMPLETE_CHECK_OP_1 (node);
11207 CAN_COMPLETE_NORMALLY (node)
11208 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11209 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11210 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11211 return node;
11212
5a005d9e
PB
11213 case CLEANUP_POINT_EXPR:
11214 COMPLETE_CHECK_OP_0 (node);
11215 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11216 CAN_COMPLETE_NORMALLY (node) =
11217 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11218 return node;
11219
11220 case WITH_CLEANUP_EXPR:
11221 COMPLETE_CHECK_OP_0 (node);
11222 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11223 CAN_COMPLETE_NORMALLY (node) =
11224 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11225 TREE_TYPE (node) = void_type_node;
11226 return node;
11227
e04a16fb
AG
11228 case LABELED_BLOCK_EXPR:
11229 PUSH_LABELED_BLOCK (node);
11230 if (LABELED_BLOCK_BODY (node))
11231 COMPLETE_CHECK_OP_1 (node);
11232 TREE_TYPE (node) = void_type_node;
11233 POP_LABELED_BLOCK ();
1fb89a4d
APB
11234
11235 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11236 {
11237 LABELED_BLOCK_BODY (node) = NULL_TREE;
11238 CAN_COMPLETE_NORMALLY (node) = 1;
11239 }
1fb89a4d 11240 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11241 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11242 return node;
11243
11244 case EXIT_BLOCK_EXPR:
11245 /* We don't complete operand 1, because it's the return value of
11246 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11247 return patch_bc_statement (node);
11248
15fdcfe9
PB
11249 case CASE_EXPR:
11250 cn = java_complete_tree (TREE_OPERAND (node, 0));
11251 if (cn == error_mark_node)
11252 return cn;
11253
8576f094
APB
11254 /* First, the case expression must be constant. Values of final
11255 fields are accepted. */
15fdcfe9 11256 cn = fold (cn);
8576f094
APB
11257 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11258 && JDECL_P (TREE_OPERAND (cn, 1))
11259 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11260 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11261 {
100f7cd8
APB
11262 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11263 TREE_OPERAND (cn, 1));
100f7cd8 11264 }
15fdcfe9 11265
ce6e9147 11266 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11267 {
11268 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11269 parse_error_context (node, "Constant expression required");
11270 return error_mark_node;
11271 }
11272
11273 nn = ctxp->current_loop;
11274
11275 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11276 if (!try_builtin_assignconv (NULL_TREE,
11277 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11278 {
11279 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11280 parse_error_context
11281 (wfl_operator,
11282 "Incompatible type for case. Can't convert `%s' to `int'",
11283 lang_printable_name (TREE_TYPE (cn), 0));
11284 return error_mark_node;
11285 }
11286
11287 cn = fold (convert (int_type_node, cn));
11288
11289 /* Multiple instance of a case label bearing the same
11290 value is checked during code generation. The case
11291 expression is allright so far. */
34d4df06
APB
11292 if (TREE_CODE (cn) == VAR_DECL)
11293 cn = DECL_INITIAL (cn);
15fdcfe9 11294 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11295 TREE_TYPE (node) = void_type_node;
15fdcfe9 11296 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11297 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11298 break;
11299
11300 case DEFAULT_EXPR:
11301 nn = ctxp->current_loop;
11302 /* Only one default label is allowed per switch statement */
11303 if (SWITCH_HAS_DEFAULT (nn))
11304 {
11305 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11306 parse_error_context (wfl_operator,
11307 "Duplicate case label: `default'");
11308 return error_mark_node;
11309 }
11310 else
11311 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11312 TREE_TYPE (node) = void_type_node;
10100cc7 11313 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11314 CAN_COMPLETE_NORMALLY (node) = 1;
11315 break;
11316
b67d701b 11317 case SWITCH_EXPR:
e04a16fb
AG
11318 case LOOP_EXPR:
11319 PUSH_LOOP (node);
11320 /* Check whether the loop was enclosed in a labeled
11321 statement. If not, create one, insert the loop in it and
11322 return the node */
11323 nn = patch_loop_statement (node);
b67d701b 11324
e04a16fb 11325 /* Anyways, walk the body of the loop */
b67d701b
PB
11326 if (TREE_CODE (node) == LOOP_EXPR)
11327 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11328 /* Switch statement: walk the switch expression and the cases */
11329 else
11330 node = patch_switch_statement (node);
11331
e7c7bcef 11332 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11333 nn = error_mark_node;
11334 else
15fdcfe9 11335 {
b635eb2f
PB
11336 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11337 /* If we returned something different, that's because we
11338 inserted a label. Pop the label too. */
11339 if (nn != node)
11340 {
11341 if (CAN_COMPLETE_NORMALLY (node))
11342 CAN_COMPLETE_NORMALLY (nn) = 1;
11343 POP_LABELED_BLOCK ();
11344 }
15fdcfe9 11345 }
e04a16fb
AG
11346 POP_LOOP ();
11347 return nn;
11348
11349 case EXIT_EXPR:
11350 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11351 return patch_exit_expr (node);
11352
11353 case COND_EXPR:
11354 /* Condition */
11355 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11356 if (TREE_OPERAND (node, 0) == error_mark_node)
11357 return error_mark_node;
11358 /* then-else branches */
11359 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11360 if (TREE_OPERAND (node, 1) == error_mark_node)
11361 return error_mark_node;
11362 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11363 if (TREE_OPERAND (node, 2) == error_mark_node)
11364 return error_mark_node;
11365 return patch_if_else_statement (node);
11366 break;
11367
22eed1e6
APB
11368 case CONDITIONAL_EXPR:
11369 /* Condition */
11370 wfl_op1 = TREE_OPERAND (node, 0);
11371 COMPLETE_CHECK_OP_0 (node);
11372 wfl_op2 = TREE_OPERAND (node, 1);
11373 COMPLETE_CHECK_OP_1 (node);
11374 wfl_op3 = TREE_OPERAND (node, 2);
11375 COMPLETE_CHECK_OP_2 (node);
11376 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11377
e04a16fb
AG
11378 /* 3- Expression section */
11379 case COMPOUND_EXPR:
15fdcfe9 11380 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11381 TREE_OPERAND (node, 0) = nn =
11382 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11383 if (wfl_op2 == empty_stmt_node)
11384 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11385 else
15fdcfe9 11386 {
dc0b3eff 11387 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11388 {
dc0b3eff
PB
11389 /* An unreachable condition in a do-while statement
11390 is *not* (technically) an unreachable statement. */
11391 nn = wfl_op2;
11392 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11393 nn = EXPR_WFL_NODE (nn);
11394 if (TREE_CODE (nn) != EXIT_EXPR)
11395 {
11396 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11397 parse_error_context (wfl_operator, "Unreachable statement");
11398 }
bccaf73a 11399 }
dc0b3eff
PB
11400 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11401 if (TREE_OPERAND (node, 1) == error_mark_node)
11402 return error_mark_node;
11403 CAN_COMPLETE_NORMALLY (node)
11404 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11405 }
e04a16fb
AG
11406 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11407 break;
11408
11409 case RETURN_EXPR:
15fdcfe9 11410 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11411 return patch_return (node);
11412
11413 case EXPR_WITH_FILE_LOCATION:
11414 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11415 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11416 {
5423609c 11417 tree wfl = node;
15fdcfe9 11418 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11419 if (node == error_mark_node)
11420 return node;
5423609c
APB
11421 /* Keep line number information somewhere were it doesn't
11422 disrupt the completion process. */
2c56429a 11423 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11424 {
11425 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11426 TREE_OPERAND (node, 1) = wfl;
11427 }
15fdcfe9
PB
11428 CAN_COMPLETE_NORMALLY (node) = 1;
11429 }
e04a16fb
AG
11430 else
11431 {
5b09b33e
PB
11432 tree body;
11433 int save_lineno = lineno;
11434 lineno = EXPR_WFL_LINENO (node);
11435 body = java_complete_tree (EXPR_WFL_NODE (node));
11436 lineno = save_lineno;
15fdcfe9 11437 EXPR_WFL_NODE (node) = body;
dc0b3eff 11438 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11439 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
00b4575d 11440 if (body == empty_stmt_node || TREE_CONSTANT (body))
cd9643f7 11441 {
00b4575d 11442 /* Makes it easier to constant fold, detect empty bodies. */
cd9643f7
PB
11443 return body;
11444 }
dc0b3eff 11445 if (body == error_mark_node)
e04a16fb
AG
11446 {
11447 /* Its important for the evaluation of assignment that
11448 this mark on the TREE_TYPE is propagated. */
11449 TREE_TYPE (node) = error_mark_node;
11450 return error_mark_node;
11451 }
11452 else
11453 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11454
e04a16fb
AG
11455 }
11456 break;
11457
b67d701b 11458 case NEW_ARRAY_EXPR:
e04a16fb
AG
11459 /* Patch all the dimensions */
11460 flag = 0;
11461 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11462 {
11463 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11464 tree dim = convert (int_type_node,
11465 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11466 if (dim == error_mark_node)
11467 {
11468 flag = 1;
11469 continue;
11470 }
11471 else
11472 {
b9f7e36c 11473 TREE_VALUE (cn) = dim;
e04a16fb
AG
11474 /* Setup the location of the current dimension, for
11475 later error report. */
11476 TREE_PURPOSE (cn) =
11477 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11478 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11479 }
11480 }
11481 /* They complete the array creation expression, if no errors
11482 were found. */
15fdcfe9 11483 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11484 return (flag ? error_mark_node
11485 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11486
c2952b01
APB
11487 case NEW_ANONYMOUS_ARRAY_EXPR:
11488 /* Create the array type if necessary. */
11489 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11490 {
11491 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11492 if (!(type = resolve_type_during_patch (type)))
11493 return error_mark_node;
11494 type = build_array_from_name (type, NULL_TREE,
11495 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11496 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11497 }
11498 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11499 ANONYMOUS_ARRAY_INITIALIZER (node));
11500 if (node == error_mark_node)
11501 return error_mark_node;
11502 CAN_COMPLETE_NORMALLY (node) = 1;
11503 return node;
11504
b67d701b 11505 case NEW_CLASS_EXPR:
e04a16fb 11506 case CALL_EXPR:
b67d701b 11507 /* Complete function's argument(s) first */
e04a16fb
AG
11508 if (complete_function_arguments (node))
11509 return error_mark_node;
11510 else
b9f7e36c 11511 {
22eed1e6
APB
11512 tree decl, wfl = TREE_OPERAND (node, 0);
11513 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
e101152f
APB
11514 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11515 super_identifier_node);
22eed1e6 11516
e101152f
APB
11517 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11518 from_super, 0, &decl);
c877974e
APB
11519 if (node == error_mark_node)
11520 return error_mark_node;
11521
11522 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11523 /* If we call this(...), register signature and positions */
11524 if (in_this)
11525 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11526 tree_cons (wfl, decl,
11527 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11528 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11529 return force_evaluation_order (node);
b9f7e36c 11530 }
e04a16fb
AG
11531
11532 case MODIFY_EXPR:
11533 /* Save potential wfls */
11534 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11535 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11536
cd9643f7
PB
11537 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11538 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11539 && DECL_INITIAL (nn) != NULL_TREE)
11540 {
100f7cd8
APB
11541 tree value;
11542
100f7cd8 11543 value = fold_constant_for_init (nn, nn);
c2952b01 11544
cd9643f7
PB
11545 if (value != NULL_TREE)
11546 {
11547 tree type = TREE_TYPE (value);
c2952b01
APB
11548 if (JPRIMITIVE_TYPE_P (type) ||
11549 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11550 return empty_stmt_node;
11551 }
629d4b4d
APB
11552 if (! flag_emit_class_files)
11553 DECL_INITIAL (nn) = NULL_TREE;
c7303e41
APB
11554 if (CLASS_FINAL_VARIABLE_P (nn))
11555 DECL_FIELD_FINAL_IUD (nn) = 0;
cd9643f7 11556 }
e04a16fb 11557 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11558
e04a16fb
AG
11559 if (TREE_OPERAND (node, 0) == error_mark_node)
11560 return error_mark_node;
11561
5cbdba64
APB
11562 flag = COMPOUND_ASSIGN_P (wfl_op2);
11563 if (flag)
e04a16fb 11564 {
c2952b01
APB
11565 /* This might break when accessing outer field from inner
11566 class. TESTME, FIXME */
2aa11e97 11567 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb 11568
e101152f 11569 /* Hand stabilize the lhs on both places */
e04a16fb 11570 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11571 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11572 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11573
5cbdba64 11574 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11575 /* Now complete the RHS. We write it back later on. */
11576 nn = java_complete_tree (TREE_OPERAND (node, 1));
11577
642f15d1
APB
11578 if ((cn = patch_string (nn)))
11579 nn = cn;
11580
2aa11e97
APB
11581 /* The last part of the rewrite for E1 op= E2 is to have
11582 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11583 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11584 TREE_TYPE (lvalue), nn));
5cbdba64 11585
568aac9c
TT
11586 /* If the assignment is compound and has reference type,
11587 then ensure the LHS has type String and nothing else. */
11588 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11589 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11590 parse_error_context (wfl_op2,
11591 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11592 lang_printable_name (TREE_TYPE (lvalue), 0));
11593
5cbdba64 11594 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11595 }
11596
f8976021 11597 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11598 function to complete this RHS. Note that a NEW_ARRAY_INIT
11599 might have been already fully expanded if created as a result
11600 of processing an anonymous array initializer. We avoid doing
11601 the operation twice by testing whether the node already bears
11602 a type. */
11603 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11604 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11605 TREE_OPERAND (node, 1));
2aa11e97 11606 /* Otherwise we simply complete the RHS */
f8976021
APB
11607 else
11608 nn = java_complete_tree (TREE_OPERAND (node, 1));
11609
e04a16fb 11610 if (nn == error_mark_node)
c0d87ff6 11611 return error_mark_node;
2aa11e97
APB
11612
11613 /* Write back the RHS as we evaluated it. */
e04a16fb 11614 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11615
11616 /* In case we're handling = with a String as a RHS, we need to
11617 produce a String out of the RHS (it might still be a
11618 STRING_CST or a StringBuffer at this stage */
11619 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11620 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11621
11622 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11623 TREE_OPERAND (node, 1))))
11624 {
11625 /* We return error_mark_node if outer_field_access_fix
11626 detects we write into a final. */
11627 if (nn == error_mark_node)
11628 return error_mark_node;
11629 node = nn;
11630 }
11631 else
11632 {
11633 node = patch_assignment (node, wfl_op1, wfl_op2);
11634 /* Reorganize the tree if necessary. */
11635 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11636 || JSTRING_P (TREE_TYPE (node))))
11637 node = java_refold (node);
11638 }
11639
15fdcfe9
PB
11640 CAN_COMPLETE_NORMALLY (node) = 1;
11641 return node;
e04a16fb
AG
11642
11643 case MULT_EXPR:
11644 case PLUS_EXPR:
11645 case MINUS_EXPR:
11646 case LSHIFT_EXPR:
11647 case RSHIFT_EXPR:
11648 case URSHIFT_EXPR:
11649 case BIT_AND_EXPR:
11650 case BIT_XOR_EXPR:
11651 case BIT_IOR_EXPR:
11652 case TRUNC_MOD_EXPR:
c2952b01 11653 case TRUNC_DIV_EXPR:
e04a16fb
AG
11654 case RDIV_EXPR:
11655 case TRUTH_ANDIF_EXPR:
11656 case TRUTH_ORIF_EXPR:
11657 case EQ_EXPR:
11658 case NE_EXPR:
11659 case GT_EXPR:
11660 case GE_EXPR:
11661 case LT_EXPR:
11662 case LE_EXPR:
11663 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11664 knows how to handle those cases. */
11665 wfl_op1 = TREE_OPERAND (node, 0);
11666 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11667
15fdcfe9 11668 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11669 /* Don't complete string nodes if dealing with the PLUS operand. */
11670 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11671 {
11672 nn = java_complete_tree (wfl_op1);
11673 if (nn == error_mark_node)
11674 return error_mark_node;
48a840d9 11675
2aa11e97
APB
11676 TREE_OPERAND (node, 0) = nn;
11677 }
b67d701b 11678 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11679 {
11680 nn = java_complete_tree (wfl_op2);
11681 if (nn == error_mark_node)
11682 return error_mark_node;
48a840d9 11683
2aa11e97
APB
11684 TREE_OPERAND (node, 1) = nn;
11685 }
dc0b3eff 11686 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11687
5e942c50
APB
11688 case INSTANCEOF_EXPR:
11689 wfl_op1 = TREE_OPERAND (node, 0);
11690 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11691 if (flag_emit_xref)
11692 {
11693 TREE_TYPE (node) = boolean_type_node;
11694 return node;
11695 }
5e942c50
APB
11696 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11697
b67d701b 11698 case UNARY_PLUS_EXPR:
e04a16fb
AG
11699 case NEGATE_EXPR:
11700 case TRUTH_NOT_EXPR:
11701 case BIT_NOT_EXPR:
11702 case PREDECREMENT_EXPR:
11703 case PREINCREMENT_EXPR:
11704 case POSTDECREMENT_EXPR:
11705 case POSTINCREMENT_EXPR:
11706 case CONVERT_EXPR:
11707 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11708 how to handle those cases. */
11709 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11710 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11711 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11712 if (TREE_OPERAND (node, 0) == error_mark_node)
11713 return error_mark_node;
4a5f66c3
APB
11714 node = patch_unaryop (node, wfl_op1);
11715 CAN_COMPLETE_NORMALLY (node) = 1;
11716 break;
e04a16fb
AG
11717
11718 case ARRAY_REF:
11719 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11720 how to handle those cases. */
11721 wfl_op1 = TREE_OPERAND (node, 0);
11722 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11723 if (TREE_OPERAND (node, 0) == error_mark_node)
11724 return error_mark_node;
7f1d4866 11725 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11726 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11727 /* The same applies to wfl_op2 */
11728 wfl_op2 = TREE_OPERAND (node, 1);
11729 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11730 if (TREE_OPERAND (node, 1) == error_mark_node)
11731 return error_mark_node;
7f1d4866 11732 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11733 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11734 return patch_array_ref (node);
e04a16fb 11735
63a212ed
PB
11736 case RECORD_TYPE:
11737 return node;;
11738
11739 case COMPONENT_REF:
11740 /* The first step in the re-write of qualified name handling. FIXME.
11741 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11742 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11743 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11744 {
11745 tree name = TREE_OPERAND (node, 1);
11746 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11747 if (field == NULL_TREE)
11748 {
11749 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11750 return error_mark_node;
11751 }
11752 if (! FIELD_STATIC (field))
11753 {
11754 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11755 return error_mark_node;
11756 }
11757 return field;
11758 }
11759 else
400500c4 11760 abort ();
9bbc7d9f 11761 break;
9bbc7d9f 11762
b67d701b 11763 case THIS_EXPR:
e04a16fb
AG
11764 /* Can't use THIS in a static environment */
11765 if (!current_this)
11766 {
11767 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11768 parse_error_context (wfl_operator,
11769 "Keyword `this' used outside allowed context");
e04a16fb
AG
11770 TREE_TYPE (node) = error_mark_node;
11771 return error_mark_node;
11772 }
22eed1e6
APB
11773 if (ctxp->explicit_constructor_p)
11774 {
11775 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11776 parse_error_context
781b0558 11777 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11778 TREE_TYPE (node) = error_mark_node;
11779 return error_mark_node;
11780 }
e04a16fb 11781 return current_this;
c2952b01
APB
11782
11783 case CLASS_LITERAL:
11784 CAN_COMPLETE_NORMALLY (node) = 1;
11785 node = patch_incomplete_class_ref (node);
11786 if (node == error_mark_node)
11787 return error_mark_node;
11788 break;
11789
11790 case INSTANCE_INITIALIZERS_EXPR:
11791 in_instance_initializer++;
11792 node = java_complete_tree (TREE_OPERAND (node, 0));
11793 in_instance_initializer--;
11794 if (node != error_mark_node)
11795 TREE_TYPE (node) = void_type_node;
11796 else
11797 return error_mark_node;
11798 break;
e04a16fb 11799
e04a16fb 11800 default:
15fdcfe9 11801 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11802 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11803 and it's time to turn it into the appropriate String object */
11804 if ((nn = patch_string (node)))
11805 node = nn;
11806 else
400500c4 11807 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11808 }
11809 return node;
11810}
11811
11812/* Complete function call's argument. Return a non zero value is an
11813 error was found. */
11814
11815static int
11816complete_function_arguments (node)
11817 tree node;
11818{
11819 int flag = 0;
11820 tree cn;
11821
f63991a8 11822 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11823 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11824 {
b67d701b 11825 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11826 parm = java_complete_tree (wfl);
c2952b01 11827
e04a16fb
AG
11828 if (parm == error_mark_node)
11829 {
11830 flag = 1;
11831 continue;
11832 }
b67d701b
PB
11833 /* If have a string literal that we haven't transformed yet or a
11834 crafted string buffer, as a result of use of the the String
11835 `+' operator. Build `parm.toString()' and expand it. */
11836 if ((temp = patch_string (parm)))
b9f7e36c 11837 parm = temp;
5e942c50
APB
11838 /* Inline PRIMTYPE.TYPE read access */
11839 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11840
5e942c50 11841 TREE_VALUE (cn) = parm;
e04a16fb 11842 }
f63991a8 11843 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11844 return flag;
11845}
11846
11847/* Sometimes (for loops and variable initialized during their
11848 declaration), we want to wrap a statement around a WFL and turn it
11849 debugable. */
11850
11851static tree
11852build_debugable_stmt (location, stmt)
11853 int location;
11854 tree stmt;
11855{
11856 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11857 {
11858 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11859 EXPR_WFL_LINECOL (stmt) = location;
11860 }
11861 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11862 return stmt;
11863}
11864
11865static tree
11866build_expr_block (body, decls)
11867 tree body, decls;
11868{
11869 tree node = make_node (BLOCK);
11870 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11871 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11872 if (body)
11873 TREE_TYPE (node) = TREE_TYPE (body);
11874 TREE_SIDE_EFFECTS (node) = 1;
11875 return node;
11876}
11877
b67d701b
PB
11878/* Create a new function block and link it approriately to current
11879 function block chain */
e04a16fb
AG
11880
11881static tree
11882enter_block ()
11883{
47a50de9 11884 tree b = build_expr_block (NULL_TREE, NULL_TREE);
b67d701b 11885
47a50de9
PB
11886 /* Link block B supercontext to the previous block. The current
11887 function DECL is used as supercontext when enter_a_block is called
11888 for the first time for a given function. The current function body
11889 (DECL_FUNCTION_BODY) is set to be block B. */
b67d701b 11890
e04a16fb
AG
11891 tree fndecl = current_function_decl;
11892
f099f336
APB
11893 if (!fndecl) {
11894 BLOCK_SUPERCONTEXT (b) = current_static_block;
11895 current_static_block = b;
11896 }
11897
11898 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11899 {
11900 BLOCK_SUPERCONTEXT (b) = fndecl;
11901 DECL_FUNCTION_BODY (fndecl) = b;
11902 }
11903 else
11904 {
11905 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11906 DECL_FUNCTION_BODY (fndecl) = b;
11907 }
11908 return b;
11909}
11910
11911/* Exit a block by changing the current function body
11912 (DECL_FUNCTION_BODY) to the current block super context, only if
11913 the block being exited isn't the method's top level one. */
11914
11915static tree
11916exit_block ()
11917{
f099f336
APB
11918 tree b;
11919 if (current_function_decl)
11920 {
11921 b = DECL_FUNCTION_BODY (current_function_decl);
11922 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11923 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11924 }
11925 else
11926 {
11927 b = current_static_block;
e04a16fb 11928
f099f336
APB
11929 if (BLOCK_SUPERCONTEXT (b))
11930 current_static_block = BLOCK_SUPERCONTEXT (b);
11931 }
e04a16fb
AG
11932 return b;
11933}
11934
11935/* Lookup for NAME in the nested function's blocks, all the way up to
11936 the current toplevel one. It complies with Java's local variable
11937 scoping rules. */
11938
11939static tree
11940lookup_name_in_blocks (name)
11941 tree name;
11942{
f099f336 11943 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11944
11945 while (b != current_function_decl)
11946 {
11947 tree current;
11948
11949 /* Paranoid sanity check. To be removed */
11950 if (TREE_CODE (b) != BLOCK)
400500c4 11951 abort ();
e04a16fb
AG
11952
11953 for (current = BLOCK_EXPR_DECLS (b); current;
11954 current = TREE_CHAIN (current))
11955 if (DECL_NAME (current) == name)
11956 return current;
11957 b = BLOCK_SUPERCONTEXT (b);
11958 }
11959 return NULL_TREE;
11960}
11961
11962static void
11963maybe_absorb_scoping_blocks ()
11964{
f099f336 11965 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11966 {
11967 tree b = exit_block ();
11968 java_method_add_stmt (current_function_decl, b);
11969 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11970 }
11971}
11972
11973\f
11974/* This section of the source is reserved to build_* functions that
11975 are building incomplete tree nodes and the patch_* functions that
11976 are completing them. */
11977
c2952b01
APB
11978/* Wrap a non WFL node around a WFL. */
11979static tree
9a7ab4b3 11980build_wfl_wrap (node, location)
c2952b01 11981 tree node;
9a7ab4b3 11982 int location;
c2952b01
APB
11983{
11984 tree wfl, node_to_insert = node;
11985
11986 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11987 with the way we're processing SUPER. A THIS from a primary as a
11988 different form than a SUPER. Turn THIS into something symbolic */
11989 if (TREE_CODE (node) == THIS_EXPR)
11990 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11991 else
11992 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11993
9a7ab4b3 11994 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11995 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11996 return wfl;
11997}
11998
11999
9bbc7d9f 12000/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
12001 we're currently dealing with the class java.lang.Object. */
12002
12003static tree
e920ebc9
APB
12004build_super_invocation (mdecl)
12005 tree mdecl;
22eed1e6 12006{
e920ebc9 12007 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 12008 return empty_stmt_node;
22eed1e6
APB
12009 else
12010 {
9ee9b555 12011 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
12012 tree a = NULL_TREE, t;
12013 /* If we're dealing with an anonymous class, pass the arguments
12014 of the crafted constructor along. */
12015 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12016 {
12017 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12018 for (; t != end_params_node; t = TREE_CHAIN (t))
12019 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12020 }
12021 return build_method_invocation (super_wfl, a);
22eed1e6
APB
12022 }
12023}
12024
12025/* Build a SUPER/THIS qualified method invocation. */
12026
12027static tree
12028build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12029 int use_this;
12030 tree name, args;
12031 int lloc, rloc;
22eed1e6
APB
12032{
12033 tree invok;
12034 tree wfl =
9ee9b555 12035 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
12036 EXPR_WFL_LINECOL (wfl) = lloc;
12037 invok = build_method_invocation (name, args);
12038 return make_qualified_primary (wfl, invok, rloc);
12039}
12040
b67d701b 12041/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
12042
12043static tree
12044build_method_invocation (name, args)
12045 tree name;
12046 tree args;
12047{
12048 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12049 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
12050 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12051 return call;
12052}
12053
12054/* Build an incomplete new xxx(...) node. */
12055
12056static tree
12057build_new_invocation (name, args)
12058 tree name, args;
12059{
12060 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12061 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
12062 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12063 return call;
12064}
12065
12066/* Build an incomplete assignment expression. */
12067
12068static tree
12069build_assignment (op, op_location, lhs, rhs)
12070 int op, op_location;
12071 tree lhs, rhs;
12072{
12073 tree assignment;
12074 /* Build the corresponding binop if we deal with a Compound
12075 Assignment operator. Mark the binop sub-tree as part of a
12076 Compound Assignment expression */
12077 if (op != ASSIGN_TK)
12078 {
12079 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12080 COMPOUND_ASSIGN_P (rhs) = 1;
12081 }
12082 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12083 TREE_SIDE_EFFECTS (assignment) = 1;
12084 EXPR_WFL_LINECOL (assignment) = op_location;
12085 return assignment;
12086}
12087
12088/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12089
15fdcfe9 12090char *
e04a16fb
AG
12091print_int_node (node)
12092 tree node;
12093{
12094 static char buffer [80];
12095 if (TREE_CONSTANT_OVERFLOW (node))
12096 sprintf (buffer, "<overflow>");
12097
12098 if (TREE_INT_CST_HIGH (node) == 0)
12099 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12100 TREE_INT_CST_LOW (node));
12101 else if (TREE_INT_CST_HIGH (node) == -1
12102 && TREE_INT_CST_LOW (node) != 0)
12103 {
12104 buffer [0] = '-';
12105 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12106 -TREE_INT_CST_LOW (node));
12107 }
12108 else
12109 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12110 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12111
12112 return buffer;
12113}
12114
c7303e41
APB
12115\f
12116
12117/* This section of the code handle assignment check with FINAL
12118 variables. */
12119
12120static void
12121reset_static_final_variable_assignment_flag (class)
12122 tree class;
12123{
12124 tree field;
12125 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12126 if (CLASS_FINAL_VARIABLE_P (field))
12127 DECL_FIELD_FINAL_LIIC (field) = 0;
12128}
12129
12130/* Figure whether all final static variable have been initialized. */
12131
12132static void
12133check_static_final_variable_assignment_flag (class)
12134 tree class;
12135{
12136 tree field;
12137
12138 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12139 if (CLASS_FINAL_VARIABLE_P (field)
12140 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12141 parse_error_context
12142 (DECL_FIELD_FINAL_WFL (field),
e9e42530 12143 "Blank static final variable `%s' may not have been initialized",
c7303e41
APB
12144 IDENTIFIER_POINTER (DECL_NAME (field)));
12145}
12146
12147/* This function marks all final variable locally unassigned. */
12148
12149static void
12150reset_final_variable_local_assignment_flag (class)
12151 tree class;
12152{
12153 tree field;
12154 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12155 if (FINAL_VARIABLE_P (field))
12156 DECL_FIELD_FINAL_LIIC (field) = 0;
12157}
12158
12159/* Figure whether all final variables have beem initialized in MDECL
12160 and mark MDECL accordingly. */
12161
12162static void
12163check_final_variable_local_assignment_flag (class, mdecl)
12164 tree class;
12165 tree mdecl;
12166{
12167 tree field;
12168 int initialized = 0;
12169 int non_initialized = 0;
12170
12171 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12172 return;
12173
12174 /* First find out whether all final variables or no final variable
12175 are initialized in this ctor. We don't take into account final
12176 variable that have been initialized upon declaration. */
12177 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12178 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12179 {
12180 if (DECL_FIELD_FINAL_LIIC (field))
12181 initialized++;
12182 else
12183 non_initialized++;
12184 }
12185
12186 /* There were no non initialized variable and no initialized variable.
12187 This ctor is fine. */
12188 if (!non_initialized && !initialized)
12189 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12190 /* If no variables have been initialized, that fine. We'll check
12191 later whether this ctor calls a constructor which initializes
12192 them. We mark the ctor as not initializing all its finals. */
12193 else if (initialized == 0)
12194 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12195 /* If we have a mixed bag, then we have a problem. We need to report
12196 all the variables we're not initializing. */
12197 else if (initialized && non_initialized)
12198 {
12199 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12200 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12201 if (FIELD_FINAL (field)
12202 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12203 {
12204 parse_error_context
12205 (lookup_cl (mdecl),
12206 "Blank final variable `%s' may not have been initialized in this constructor",
12207 IDENTIFIER_POINTER (DECL_NAME (field)));
12208 DECL_FIELD_FINAL_IERR (field) = 1;
12209 }
12210 }
12211 /* Otherwise we know this ctor is initializing all its final
12212 variable. We mark it so. */
12213 else
12214 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12215}
12216
12217/* This function recurses in a simple what through STMT and stops when
12218 it finds a constructor call. It then verifies that the called
12219 constructor initialized its final properly. Return 1 upon success,
12220 0 or -1 otherwise. */
12221
12222static int
12223check_final_variable_indirect_assignment (stmt)
12224 tree stmt;
12225{
12226 int res;
12227 switch (TREE_CODE (stmt))
12228 {
12229 case EXPR_WITH_FILE_LOCATION:
12230 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12231 case COMPOUND_EXPR:
12232 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12233 if (res)
12234 return res;
12235 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12236 case SAVE_EXPR:
12237 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12238 case CALL_EXPR:
12239 {
12240 tree decl = TREE_OPERAND (stmt, 0);
12241 tree fbody;
12242
12243 if (TREE_CODE (decl) != FUNCTION_DECL)
12244 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12245 if (TREE_CODE (decl) != FUNCTION_DECL)
400500c4 12246 abort ();
c7303e41
APB
12247 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12248 return 1;
12249 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12250 return -1;
12251 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12252 if (fbody == error_mark_node)
12253 return -1;
12254 fbody = BLOCK_EXPR_BODY (fbody);
12255 return check_final_variable_indirect_assignment (fbody);
12256 }
12257 default:
12258 break;
12259 }
12260 return 0;
12261}
12262
12263/* This is the last chance to catch a final variable initialization
12264 problem. This routine will report an error if a final variable was
12265 never (globally) initialized and never reported as not having been
12266 initialized properly. */
12267
12268static void
12269check_final_variable_global_assignment_flag (class)
12270 tree class;
12271{
12272 tree field, mdecl;
12273 int nnctor = 0;
12274
12275 /* We go through all natural ctors and see whether they're
12276 initializing all their final variables or not. */
12277 current_function_decl = NULL_TREE; /* For the error report. */
12278 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12279 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12280 {
12281 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12282 {
12283 /* It doesn't. Maybe it calls a constructor that initializes
12284 them. find out. */
12285 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12286 if (fbody == error_mark_node)
12287 continue;
12288 fbody = BLOCK_EXPR_BODY (fbody);
12289 if (check_final_variable_indirect_assignment (fbody) == 1)
12290 {
12291 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12292 nnctor++;
12293 }
12294 else
12295 parse_error_context
12296 (lookup_cl (mdecl),
12297 "Final variable initialization error in this constructor");
12298 }
12299 else
12300 nnctor++;
12301 }
12302
12303 /* Finally we catch final variables that never were initialized */
12304 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12305 if (FINAL_VARIABLE_P (field)
12306 /* If the field wasn't initialized upon declaration */
12307 && !DECL_FIELD_FINAL_IUD (field)
12308 /* There wasn't natural ctor in which the field could have been
12309 initialized */
12310 && !nnctor
12311 /* If we never reported a problem with this field */
12312 && !DECL_FIELD_FINAL_IERR (field))
12313 {
12314 current_function_decl = NULL;
12315 parse_error_context
12316 (DECL_FIELD_FINAL_WFL (field),
12317 "Final variable `%s' hasn't been initialized upon its declaration",
12318 IDENTIFIER_POINTER (DECL_NAME (field)));
12319 }
12320
12321}
12322
7f1d4866
APB
12323/* Return 1 if an assignment to a FINAL is attempted in a non suitable
12324 context. */
5e942c50
APB
12325
12326static int
12327check_final_assignment (lvalue, wfl)
12328 tree lvalue, wfl;
12329{
c7303e41
APB
12330 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12331 return 0;
12332
12333 if (TREE_CODE (lvalue) == COMPONENT_REF
6632dcdd
APB
12334 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12335 lvalue = TREE_OPERAND (lvalue, 1);
12336
c7303e41
APB
12337 if (!FIELD_FINAL (lvalue))
12338 return 0;
12339
12340 /* Now the logic. We can modify a final VARIABLE:
12341 1) in finit$, (its declaration was followed by an initialization,)
12342 2) consistently in each natural ctor, if it wasn't initialized in
12343 finit$ or once in <clinit>. In any other cases, an error should be
12344 reported. */
12345 if (DECL_FINIT_P (current_function_decl))
5e942c50 12346 {
c7303e41
APB
12347 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12348 return 0;
5e942c50 12349 }
c7303e41
APB
12350
12351 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12352 /* Only if it wasn't given a value upon initialization */
12353 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12354 /* If it was never assigned a value in this constructor */
12355 && !DECL_FIELD_FINAL_LIIC (lvalue))
12356 {
12357 /* Turn the locally assigned flag on, it will be checked later
12358 on to point out at discrepancies. */
12359 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12360 if (DECL_CLINIT_P (current_function_decl))
12361 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12362 return 0;
12363 }
12364
12365 /* Other problems should be reported right away. */
12366 parse_error_context
12367 (wfl, "Can't %sassign a value to the final variable `%s'",
12368 (FIELD_STATIC (lvalue) ? "re" : ""),
12369 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12370
12371 /* Note that static field can be initialized once and only once. */
12372 if (FIELD_STATIC (lvalue))
12373 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12374
12375 return 1;
5e942c50
APB
12376}
12377
12378/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12379 read. This is needed to avoid circularities in the implementation
12380 of these fields in libjava. */
12381
12382static tree
12383maybe_build_primttype_type_ref (rhs, wfl)
12384 tree rhs, wfl;
12385{
12386 tree to_return = NULL_TREE;
12387 tree rhs_type = TREE_TYPE (rhs);
12388 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12389 {
12390 tree n = TREE_OPERAND (rhs, 1);
12391 if (TREE_CODE (n) == VAR_DECL
12392 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
12393 && rhs_type == class_ptr_type
12394 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 12395 {
49f48c71 12396 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
12397 if (!strncmp (self_name, "java.lang.", 10))
12398 to_return = build_primtype_type_ref (self_name);
12399 }
12400 }
12401 return (to_return ? to_return : rhs );
12402}
12403
e04a16fb
AG
12404/* 15.25 Assignment operators. */
12405
12406static tree
12407patch_assignment (node, wfl_op1, wfl_op2)
12408 tree node;
12409 tree wfl_op1;
12410 tree wfl_op2;
12411{
0a2138e2 12412 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12413 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12414 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12415 int error_found = 0;
12416 int lvalue_from_array = 0;
12417
c2952b01 12418 /* Can't assign to a (blank) final. */
5e942c50
APB
12419 if (check_final_assignment (lvalue, wfl_op1))
12420 error_found = 1;
e04a16fb
AG
12421
12422 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12423
12424 /* Lhs can be a named variable */
34f4db93 12425 if (JDECL_P (lvalue))
e04a16fb 12426 {
e04a16fb
AG
12427 lhs_type = TREE_TYPE (lvalue);
12428 }
12429 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12430 comment on reason why */
12431 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12432 {
12433 lhs_type = TREE_TYPE (lvalue);
12434 lvalue_from_array = 1;
12435 }
12436 /* Or a field access */
12437 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12438 lhs_type = TREE_TYPE (lvalue);
12439 /* Or a function return slot */
12440 else if (TREE_CODE (lvalue) == RESULT_DECL)
12441 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12442 /* Otherwise, we might want to try to write into an optimized static
12443 final, this is an of a different nature, reported further on. */
12444 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12445 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12446 {
6632dcdd 12447 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12448 {
12449 /* What we should do instead is resetting the all the flags
12450 previously set, exchange lvalue for llvalue and continue. */
12451 error_found = 1;
12452 return error_mark_node;
12453 }
12454 else
12455 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12456 }
12457 else
e04a16fb
AG
12458 {
12459 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12460 error_found = 1;
12461 }
12462
12463 rhs_type = TREE_TYPE (rhs);
b67d701b 12464 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12465 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12466
b67d701b 12467 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12468 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12469 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12470
12471 /* 15.25.2 If we have a compound assignment, convert RHS into the
12472 type of the LHS */
12473 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12474 new_rhs = convert (lhs_type, rhs);
12475
12476 /* Explicit cast required. This is an error */
12477 if (!new_rhs)
12478 {
c2e3db92
KG
12479 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12480 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12481 tree wfl;
12482 char operation [32]; /* Max size known */
12483
12484 /* If the assignment is part of a declaration, we use the WFL of
12485 the declared variable to point out the error and call it a
12486 declaration problem. If the assignment is a genuine =
12487 operator, we call is a operator `=' problem, otherwise we
12488 call it an assignment problem. In both of these last cases,
12489 we use the WFL of the operator to indicate the error. */
12490
12491 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12492 {
12493 wfl = wfl_op1;
12494 strcpy (operation, "declaration");
12495 }
12496 else
12497 {
12498 wfl = wfl_operator;
12499 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12500 strcpy (operation, "assignment");
12501 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12502 strcpy (operation, "`return'");
12503 else
12504 strcpy (operation, "`='");
12505 }
12506
1ebadc60 12507 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12508 parse_error_context
12509 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12510 operation, t1, t2);
1ebadc60 12511 else
781b0558 12512 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12513 operation, t1, t2);
e04a16fb
AG
12514 free (t1); free (t2);
12515 error_found = 1;
12516 }
12517
c877974e
APB
12518 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12519 if (new_rhs)
12520 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12521
e04a16fb
AG
12522 if (error_found)
12523 return error_mark_node;
12524
2622b947
APB
12525 /* 10.10: Array Store Exception runtime check */
12526 if (!flag_emit_class_files
e8fc7396 12527 && !flag_emit_xref
2622b947 12528 && lvalue_from_array
afc390b1 12529 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12530 {
12531 tree check;
12532 tree base = lvalue;
12533
12534 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12535 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12536 base = TREE_OPERAND (lvalue, 0);
12537 else
12538 {
12539 if (flag_bounds_check)
12540 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12541 else
12542 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12543 }
12544
12545 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12546 new_rhs = save_expr (new_rhs);
2622b947
APB
12547 check = build (CALL_EXPR, void_type_node,
12548 build_address_of (soft_checkarraystore_node),
12549 tree_cons (NULL_TREE, base,
12550 build_tree_list (NULL_TREE, new_rhs)),
12551 NULL_TREE);
12552 TREE_SIDE_EFFECTS (check) = 1;
12553
12554 /* We have to decide on an insertion point */
12555 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12556 {
12557 tree t;
12558 if (flag_bounds_check)
12559 {
12560 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12561 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12562 build (COMPOUND_EXPR, void_type_node, t, check);
12563 }
12564 else
12565 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12566 check, TREE_OPERAND (lvalue, 1));
12567 }
12568 else
12569 {
12570 /* Make sure the bound check will happen before the store check */
12571 if (flag_bounds_check)
12572 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12573 build (COMPOUND_EXPR, void_type_node,
12574 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12575 else
12576 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12577 }
12578 }
22eed1e6 12579
34d4df06
APB
12580 /* Final locals can be used as case values in switch
12581 statement. Prepare them for this eventuality. */
12582 if (TREE_CODE (lvalue) == VAR_DECL
c7303e41 12583 && LOCAL_FINAL_P (lvalue)
34d4df06
APB
12584 && TREE_CONSTANT (new_rhs)
12585 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12586 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12587 )
12588 {
12589 TREE_CONSTANT (lvalue) = 1;
12590 DECL_INITIAL (lvalue) = new_rhs;
12591 }
12592
e04a16fb
AG
12593 TREE_OPERAND (node, 0) = lvalue;
12594 TREE_OPERAND (node, 1) = new_rhs;
12595 TREE_TYPE (node) = lhs_type;
12596 return node;
12597}
12598
b67d701b
PB
12599/* Check that type SOURCE can be cast into type DEST. If the cast
12600 can't occur at all, return 0 otherwise 1. This function is used to
12601 produce accurate error messages on the reasons why an assignment
12602 failed. */
e04a16fb 12603
b67d701b
PB
12604static tree
12605try_reference_assignconv (lhs_type, rhs)
12606 tree lhs_type, rhs;
e04a16fb 12607{
b67d701b
PB
12608 tree new_rhs = NULL_TREE;
12609 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12610
b67d701b
PB
12611 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12612 {
12613 /* `null' may be assigned to any reference type */
12614 if (rhs == null_pointer_node)
12615 new_rhs = null_pointer_node;
12616 /* Try the reference assignment conversion */
12617 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12618 new_rhs = rhs;
12619 /* This is a magic assignment that we process differently */
12620 else if (rhs == soft_exceptioninfo_call_node)
12621 new_rhs = rhs;
12622 }
12623 return new_rhs;
12624}
12625
12626/* Check that RHS can be converted into LHS_TYPE by the assignment
12627 conversion (5.2), for the cases of RHS being a builtin type. Return
12628 NULL_TREE if the conversion fails or if because RHS isn't of a
12629 builtin type. Return a converted RHS if the conversion is possible. */
12630
12631static tree
12632try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12633 tree wfl_op1, lhs_type, rhs;
12634{
12635 tree new_rhs = NULL_TREE;
12636 tree rhs_type = TREE_TYPE (rhs);
12637
7e51098e
TT
12638 /* Handle boolean specially. */
12639 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12640 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12641 {
12642 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12643 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12644 new_rhs = rhs;
12645 }
12646
5e942c50 12647 /* Zero accepted everywhere */
7e51098e 12648 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12649 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12650 && JPRIMITIVE_TYPE_P (rhs_type))
12651 new_rhs = convert (lhs_type, rhs);
12652
b67d701b
PB
12653 /* 5.1.1 Try Identity Conversion,
12654 5.1.2 Try Widening Primitive Conversion */
5e942c50 12655 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12656 new_rhs = convert (lhs_type, rhs);
12657
12658 /* Try a narrowing primitive conversion (5.1.3):
12659 - expression is a constant expression of type int AND
12660 - variable is byte, short or char AND
12661 - The value of the expression is representable in the type of the
12662 variable */
12663 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12664 && (lhs_type == byte_type_node || lhs_type == char_type_node
12665 || lhs_type == short_type_node))
12666 {
12667 if (int_fits_type_p (rhs, lhs_type))
12668 new_rhs = convert (lhs_type, rhs);
12669 else if (wfl_op1) /* Might be called with a NULL */
12670 parse_warning_context
7e51098e 12671 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12672 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12673 /* Reported a warning that will turn into an error further
12674 down, so we don't return */
12675 }
12676
12677 return new_rhs;
12678}
12679
12680/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12681 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12682 0 is the conversion test fails. This implements parts the method
12683 invocation convertion (5.3). */
12684
12685static int
12686valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12687 tree lhs_type, rhs_type;
12688{
acd663ee 12689 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12690 if (lhs_type == rhs_type)
12691 return 1;
12692
7e51098e
TT
12693 /* Reject non primitive types and boolean conversions. */
12694 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12695 return 0;
12696
acd663ee
APB
12697 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12698 than a char can't be converted into a char. Short can't too, but
12699 the < test below takes care of that */
b67d701b
PB
12700 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12701 return 0;
12702
5e942c50
APB
12703 /* Accept all promoted type here. Note, we can't use <= in the test
12704 below, because we still need to bounce out assignments of short
12705 to char and the likes */
12706 if (lhs_type == int_type_node
12707 && (rhs_type == promoted_byte_type_node
12708 || rhs_type == promoted_short_type_node
12709 || rhs_type == promoted_char_type_node
12710 || rhs_type == promoted_boolean_type_node))
12711 return 1;
12712
acd663ee
APB
12713 /* From here, an integral is widened if its precision is smaller
12714 than the precision of the LHS or if the LHS is a floating point
12715 type, or the RHS is a float and the RHS a double. */
12716 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12717 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12718 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12719 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12720 return 1;
12721
12722 return 0;
e04a16fb
AG
12723}
12724
12725/* Check that something of SOURCE type can be assigned or cast to
12726 something of DEST type at runtime. Return 1 if the operation is
12727 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12728 were SOURCE is cast into DEST, which borrows a lot of the
12729 assignment check. */
12730
12731static int
12732valid_ref_assignconv_cast_p (source, dest, cast)
12733 tree source;
12734 tree dest;
12735 int cast;
12736{
09ed0f70
APB
12737 /* SOURCE or DEST might be null if not from a declared entity. */
12738 if (!source || !dest)
12739 return 0;
5e942c50
APB
12740 if (JNULLP_TYPE_P (source))
12741 return 1;
e04a16fb
AG
12742 if (TREE_CODE (source) == POINTER_TYPE)
12743 source = TREE_TYPE (source);
12744 if (TREE_CODE (dest) == POINTER_TYPE)
12745 dest = TREE_TYPE (dest);
c1eacb70
BM
12746
12747 /* If source and dest are being compiled from bytecode, they may need to
12748 be loaded. */
12749 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12750 {
12751 load_class (source, 1);
12752 safe_layout_class (source);
12753 }
12754 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12755 {
12756 load_class (dest, 1);
12757 safe_layout_class (dest);
12758 }
12759
e04a16fb
AG
12760 /* Case where SOURCE is a class type */
12761 if (TYPE_CLASS_P (source))
12762 {
12763 if (TYPE_CLASS_P (dest))
c2952b01
APB
12764 return (source == dest
12765 || inherits_from_p (source, dest)
c2952b01 12766 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12767 if (TYPE_INTERFACE_P (dest))
12768 {
12769 /* If doing a cast and SOURCE is final, the operation is
12770 always correct a compile time (because even if SOURCE
12771 does not implement DEST, a subclass of SOURCE might). */
12772 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12773 return 1;
12774 /* Otherwise, SOURCE must implement DEST */
12775 return interface_of_p (dest, source);
12776 }
12777 /* DEST is an array, cast permited if SOURCE is of Object type */
12778 return (cast && source == object_type_node ? 1 : 0);
12779 }
12780 if (TYPE_INTERFACE_P (source))
12781 {
12782 if (TYPE_CLASS_P (dest))
12783 {
12784 /* If not casting, DEST must be the Object type */
12785 if (!cast)
12786 return dest == object_type_node;
12787 /* We're doing a cast. The cast is always valid is class
12788 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12789 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12790 return 1;
12791 else
12792 return interface_of_p (source, dest);
12793 }
12794 if (TYPE_INTERFACE_P (dest))
12795 {
12796 /* If doing a cast, then if SOURCE and DEST contain method
12797 with the same signature but different return type, then
12798 this is a (compile time) error */
12799 if (cast)
12800 {
12801 tree method_source, method_dest;
12802 tree source_type;
0a2138e2 12803 tree source_sig;
e04a16fb
AG
12804 tree source_name;
12805 for (method_source = TYPE_METHODS (source); method_source;
12806 method_source = TREE_CHAIN (method_source))
12807 {
12808 source_sig =
12809 build_java_argument_signature (TREE_TYPE (method_source));
12810 source_type = TREE_TYPE (TREE_TYPE (method_source));
12811 source_name = DECL_NAME (method_source);
12812 for (method_dest = TYPE_METHODS (dest);
12813 method_dest; method_dest = TREE_CHAIN (method_dest))
12814 if (source_sig ==
12815 build_java_argument_signature (TREE_TYPE (method_dest))
12816 && source_name == DECL_NAME (method_dest)
12817 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12818 return 0;
12819 }
12820 return 1;
12821 }
12822 else
12823 return source == dest || interface_of_p (dest, source);
12824 }
ee17a290
TT
12825 else
12826 {
12827 /* Array */
12828 return (cast
12829 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12830 || (DECL_NAME (TYPE_NAME (source))
12831 == java_io_serializable)));
12832 }
e04a16fb
AG
12833 }
12834 if (TYPE_ARRAY_P (source))
12835 {
12836 if (TYPE_CLASS_P (dest))
12837 return dest == object_type_node;
09ed0f70 12838 /* Can't cast an array to an interface unless the interface is
ee17a290 12839 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12840 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12841 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12842 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12843 else /* Arrays */
12844 {
12845 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12846 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12847
b9f7e36c
APB
12848 /* In case of severe errors, they turn out null */
12849 if (!dest_element_type || !source_element_type)
12850 return 0;
e04a16fb
AG
12851 if (source_element_type == dest_element_type)
12852 return 1;
12853 return valid_ref_assignconv_cast_p (source_element_type,
12854 dest_element_type, cast);
12855 }
12856 return 0;
12857 }
12858 return 0;
12859}
12860
b67d701b
PB
12861static int
12862valid_cast_to_p (source, dest)
12863 tree source;
12864 tree dest;
12865{
12866 if (TREE_CODE (source) == POINTER_TYPE)
12867 source = TREE_TYPE (source);
12868 if (TREE_CODE (dest) == POINTER_TYPE)
12869 dest = TREE_TYPE (dest);
12870
12871 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12872 return valid_ref_assignconv_cast_p (source, dest, 1);
12873
12874 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12875 return 1;
12876
7e51098e
TT
12877 else if (TREE_CODE (source) == BOOLEAN_TYPE
12878 && TREE_CODE (dest) == BOOLEAN_TYPE)
12879 return 1;
12880
b67d701b
PB
12881 return 0;
12882}
12883
15fdcfe9
PB
12884static tree
12885do_unary_numeric_promotion (arg)
12886 tree arg;
12887{
12888 tree type = TREE_TYPE (arg);
7e51098e
TT
12889 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12890 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12891 arg = convert (int_type_node, arg);
12892 return arg;
12893}
12894
acd663ee
APB
12895/* Return a non zero value if SOURCE can be converted into DEST using
12896 the method invocation conversion rule (5.3). */
b67d701b
PB
12897static int
12898valid_method_invocation_conversion_p (dest, source)
12899 tree dest, source;
12900{
e3884b71 12901 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12902 && valid_builtin_assignconv_identity_widening_p (dest, source))
12903 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12904 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12905 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12906}
12907
e04a16fb
AG
12908/* Build an incomplete binop expression. */
12909
12910static tree
12911build_binop (op, op_location, op1, op2)
12912 enum tree_code op;
12913 int op_location;
12914 tree op1, op2;
12915{
5e942c50 12916 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12917 TREE_SIDE_EFFECTS (binop) = 1;
12918 /* Store the location of the operator, for better error report. The
12919 string of the operator will be rebuild based on the OP value. */
12920 EXPR_WFL_LINECOL (binop) = op_location;
12921 return binop;
12922}
12923
12924/* Build the string of the operator retained by NODE. If NODE is part
12925 of a compound expression, add an '=' at the end of the string. This
12926 function is called when an error needs to be reported on an
12927 operator. The string is returned as a pointer to a static character
12928 buffer. */
12929
12930static char *
12931operator_string (node)
12932 tree node;
12933{
12934#define BUILD_OPERATOR_STRING(S) \
12935 { \
12936 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12937 return buffer; \
12938 }
12939
12940 static char buffer [10];
12941 switch (TREE_CODE (node))
12942 {
12943 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12944 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12945 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12946 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12947 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12948 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12949 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12950 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12951 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12952 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12953 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12954 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12955 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12956 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12957 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12958 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12959 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12960 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12961 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12962 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12963 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12964 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12965 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12966 case PREINCREMENT_EXPR: /* Fall through */
12967 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12968 case PREDECREMENT_EXPR: /* Fall through */
12969 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12970 default:
400500c4
RK
12971 internal_error ("unregistered operator %s",
12972 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
12973 }
12974 return NULL;
12975#undef BUILD_OPERATOR_STRING
12976}
12977
5cbdba64
APB
12978/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12979
12980static int
12981java_decl_equiv (var_acc1, var_acc2)
12982 tree var_acc1, var_acc2;
12983{
12984 if (JDECL_P (var_acc1))
12985 return (var_acc1 == var_acc2);
12986
12987 return (TREE_CODE (var_acc1) == COMPONENT_REF
12988 && TREE_CODE (var_acc2) == COMPONENT_REF
12989 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12990 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12991 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12992}
12993
12994/* Return a non zero value if CODE is one of the operators that can be
12995 used in conjunction with the `=' operator in a compound assignment. */
12996
12997static int
12998binop_compound_p (code)
12999 enum tree_code code;
13000{
13001 int i;
13002 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13003 if (binop_lookup [i] == code)
13004 break;
13005
13006 return i < BINOP_COMPOUND_CANDIDATES;
13007}
13008
13009/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13010
13011static tree
13012java_refold (t)
13013 tree t;
13014{
13015 tree c, b, ns, decl;
13016
13017 if (TREE_CODE (t) != MODIFY_EXPR)
13018 return t;
13019
13020 c = TREE_OPERAND (t, 1);
13021 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13022 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13023 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13024 return t;
13025
13026 /* Now the left branch of the binary operator. */
13027 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13028 if (! (b && TREE_CODE (b) == NOP_EXPR
13029 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13030 return t;
13031
13032 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13033 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13034 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13035 return t;
13036
13037 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13038 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13039 /* It's got to be the an equivalent decl */
13040 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13041 {
13042 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13043 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13044 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13045 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13046 /* Change the right part of the BINOP_EXPR */
13047 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13048 }
13049
13050 return t;
13051}
13052
e04a16fb
AG
13053/* Binary operators (15.16 up to 15.18). We return error_mark_node on
13054 errors but we modify NODE so that it contains the type computed
13055 according to the expression, when it's fixed. Otherwise, we write
13056 error_mark_node as the type. It allows us to further the analysis
13057 of remaining nodes and detects more errors in certain cases. */
13058
13059static tree
13060patch_binop (node, wfl_op1, wfl_op2)
13061 tree node;
13062 tree wfl_op1;
13063 tree wfl_op2;
13064{
13065 tree op1 = TREE_OPERAND (node, 0);
13066 tree op2 = TREE_OPERAND (node, 1);
13067 tree op1_type = TREE_TYPE (op1);
13068 tree op2_type = TREE_TYPE (op2);
48a840d9 13069 tree prom_type = NULL_TREE, cn;
e04a16fb 13070 int code = TREE_CODE (node);
b67d701b 13071
e04a16fb
AG
13072 /* If 1, tell the routine that we have to return error_mark_node
13073 after checking for the initialization of the RHS */
13074 int error_found = 0;
13075
e04a16fb
AG
13076 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13077
761491c8
APB
13078 /* If either op<n>_type are NULL, this might be early signs of an
13079 error situation, unless it's too early to tell (in case we're
13080 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13081 correctly so the error can be later on reported accurately. */
13082 if (! (code == PLUS_EXPR || code == NE_EXPR
13083 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13084 {
13085 tree n;
13086 if (! op1_type)
13087 {
13088 n = java_complete_tree (op1);
13089 op1_type = TREE_TYPE (n);
13090 }
13091 if (! op2_type)
13092 {
13093 n = java_complete_tree (op2);
13094 op2_type = TREE_TYPE (n);
13095 }
13096 }
13097
e04a16fb
AG
13098 switch (code)
13099 {
13100 /* 15.16 Multiplicative operators */
13101 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13102 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 13103 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 13104 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 13105 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13106 {
7e51098e 13107 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13108 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13109 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13110 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13111 TREE_TYPE (node) = error_mark_node;
13112 error_found = 1;
13113 break;
13114 }
13115 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13116 /* Change the division operator if necessary */
13117 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13118 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 13119
aa4759c1
AH
13120 if (TREE_CODE (prom_type) == INTEGER_TYPE
13121 && flag_use_divide_subroutine
13122 && ! flag_emit_class_files
13123 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13124 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13125
0b4d333e
APB
13126 /* This one is more complicated. FLOATs are processed by a
13127 function call to soft_fmod. Duplicate the value of the
13128 COMPOUND_ASSIGN_P flag. */
e04a16fb 13129 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
13130 {
13131 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13132 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13133 TREE_SIDE_EFFECTS (mod)
13134 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
13135 return mod;
13136 }
e04a16fb
AG
13137 break;
13138
13139 /* 15.17 Additive Operators */
13140 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
13141
13142 /* Operation is valid if either one argument is a string
13143 constant, a String object or a StringBuffer crafted for the
13144 purpose of the a previous usage of the String concatenation
13145 operator */
13146
13147 if (TREE_CODE (op1) == STRING_CST
13148 || TREE_CODE (op2) == STRING_CST
13149 || JSTRING_TYPE_P (op1_type)
13150 || JSTRING_TYPE_P (op2_type)
13151 || IS_CRAFTED_STRING_BUFFER_P (op1)
13152 || IS_CRAFTED_STRING_BUFFER_P (op2))
13153 return build_string_concatenation (op1, op2);
13154
e04a16fb
AG
13155 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13156 Numeric Types */
7e51098e 13157 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 13158 {
7e51098e 13159 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 13160 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 13161 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
13162 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13163 TREE_TYPE (node) = error_mark_node;
13164 error_found = 1;
13165 break;
13166 }
13167 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13168 break;
13169
13170 /* 15.18 Shift Operators */
13171 case LSHIFT_EXPR:
13172 case RSHIFT_EXPR:
13173 case URSHIFT_EXPR:
13174 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13175 {
13176 if (!JINTEGRAL_TYPE_P (op1_type))
13177 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13178 else
1ebadc60 13179 {
7e51098e 13180 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 13181 parse_error_context (wfl_operator,
781b0558 13182 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
13183 operator_string (node),
13184 lang_printable_name (op2_type, 0));
13185 else
781b0558
KG
13186 parse_error_context (wfl_operator,
13187 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
13188 operator_string (node),
13189 lang_printable_name (op2_type, 0));
13190 }
e04a16fb
AG
13191 TREE_TYPE (node) = error_mark_node;
13192 error_found = 1;
13193 break;
13194 }
13195
13196 /* Unary numeric promotion (5.6.1) is performed on each operand
13197 separatly */
15fdcfe9
PB
13198 op1 = do_unary_numeric_promotion (op1);
13199 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
13200
13201 /* The type of the shift expression is the type of the promoted
13202 type of the left-hand operand */
13203 prom_type = TREE_TYPE (op1);
13204
c2952b01
APB
13205 /* Shift int only up to 0x1f and long up to 0x3f */
13206 if (prom_type == int_type_node)
13207 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13208 build_int_2 (0x1f, 0)));
13209 else
13210 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13211 build_int_2 (0x3f, 0)));
e04a16fb
AG
13212
13213 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 13214 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 13215 {
0b4d333e 13216 tree to_return;
73333a87
AH
13217 tree utype = unsigned_type (prom_type);
13218 op1 = convert (utype, op1);
e04a16fb 13219 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
13220 TREE_OPERAND (node, 0) = op1;
13221 TREE_OPERAND (node, 1) = op2;
13222 TREE_TYPE (node) = utype;
0b4d333e
APB
13223 to_return = convert (prom_type, node);
13224 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13225 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
13226 TREE_SIDE_EFFECTS (to_return)
13227 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 13228 return to_return;
e04a16fb
AG
13229 }
13230 break;
5e942c50
APB
13231
13232 /* 15.19.1 Type Comparison Operator instaceof */
13233 case INSTANCEOF_EXPR:
13234
13235 TREE_TYPE (node) = boolean_type_node;
13236
13237 if (!(op2_type = resolve_type_during_patch (op2)))
13238 return error_mark_node;
13239
13240 /* The first operand must be a reference type or the null type */
13241 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13242 error_found = 1; /* Error reported further below */
13243
13244 /* The second operand must be a reference type */
13245 if (!JREFERENCE_TYPE_P (op2_type))
13246 {
13247 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13248 parse_error_context
13249 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13250 lang_printable_name (op2_type, 0));
13251 error_found = 1;
13252 }
13253
13254 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13255 {
13256 /* If the first operand is null, the result is always false */
13257 if (op1 == null_pointer_node)
13258 return boolean_false_node;
15fdcfe9
PB
13259 else if (flag_emit_class_files)
13260 {
13261 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 13262 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
13263 return node;
13264 }
5e942c50
APB
13265 /* Otherwise we have to invoke instance of to figure it out */
13266 else
67db0ce7 13267 return build_instanceof (op1, op2_type);
5e942c50
APB
13268 }
13269 /* There is no way the expression operand can be an instance of
13270 the type operand. This is a compile time error. */
13271 else
13272 {
c2e3db92 13273 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
13274 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13275 parse_error_context
13276 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13277 t1, lang_printable_name (op2_type, 0));
13278 free (t1);
13279 error_found = 1;
13280 }
e04a16fb 13281
5e942c50 13282 break;
e04a16fb
AG
13283
13284 /* 15.21 Bitwise and Logical Operators */
13285 case BIT_AND_EXPR:
13286 case BIT_XOR_EXPR:
13287 case BIT_IOR_EXPR:
13288 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13289 /* Binary numeric promotion is performed on both operand and the
13290 expression retain that type */
13291 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13292
13293 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13294 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13295 /* The type of the bitwise operator expression is BOOLEAN */
13296 prom_type = boolean_type_node;
13297 else
13298 {
13299 if (!JINTEGRAL_TYPE_P (op1_type))
13300 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13301 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13302 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13303 TREE_TYPE (node) = error_mark_node;
13304 error_found = 1;
13305 /* Insert a break here if adding thing before the switch's
13306 break for this case */
13307 }
13308 break;
13309
13310 /* 15.22 Conditional-And Operator */
13311 case TRUTH_ANDIF_EXPR:
13312 /* 15.23 Conditional-Or Operator */
13313 case TRUTH_ORIF_EXPR:
13314 /* Operands must be of BOOLEAN type */
13315 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13316 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13317 {
13318 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13319 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13320 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13321 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13322 TREE_TYPE (node) = boolean_type_node;
13323 error_found = 1;
13324 break;
13325 }
13326 /* The type of the conditional operators is BOOLEAN */
13327 prom_type = boolean_type_node;
13328 break;
13329
13330 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13331 case LT_EXPR:
13332 case GT_EXPR:
13333 case LE_EXPR:
13334 case GE_EXPR:
13335 /* The type of each of the operands must be a primitive numeric
13336 type */
13337 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13338 {
13339 if (!JNUMERIC_TYPE_P (op1_type))
13340 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13341 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13342 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13343 TREE_TYPE (node) = boolean_type_node;
13344 error_found = 1;
13345 break;
13346 }
13347 /* Binary numeric promotion is performed on the operands */
13348 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13349 /* The type of the relation expression is always BOOLEAN */
13350 prom_type = boolean_type_node;
13351 break;
13352
13353 /* 15.20 Equality Operator */
13354 case EQ_EXPR:
13355 case NE_EXPR:
48a840d9
APB
13356 /* It's time for us to patch the strings. */
13357 if ((cn = patch_string (op1)))
13358 {
13359 op1 = cn;
13360 op1_type = TREE_TYPE (op1);
13361 }
13362 if ((cn = patch_string (op2)))
13363 {
13364 op2 = cn;
13365 op2_type = TREE_TYPE (op2);
13366 }
13367
e04a16fb
AG
13368 /* 15.20.1 Numerical Equality Operators == and != */
13369 /* Binary numeric promotion is performed on the operands */
5e942c50 13370 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
13371 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13372
13373 /* 15.20.2 Boolean Equality Operators == and != */
13374 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13375 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13376 ; /* Nothing to do here */
13377
13378 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
13379 /* Types have to be either references or the null type. If
13380 they're references, it must be possible to convert either
13381 type to the other by casting conversion. */
b9f7e36c
APB
13382 else if (op1 == null_pointer_node || op2 == null_pointer_node
13383 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
13384 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13385 || valid_ref_assignconv_cast_p (op2_type,
13386 op1_type, 1))))
e04a16fb
AG
13387 ; /* Nothing to do here */
13388
13389 /* Else we have an error figure what can't be converted into
13390 what and report the error */
13391 else
13392 {
13393 char *t1;
c2e3db92 13394 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 13395 parse_error_context
781b0558
KG
13396 (wfl_operator,
13397 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13398 operator_string (node), t1,
0a2138e2 13399 lang_printable_name (op2_type, 0));
e04a16fb
AG
13400 free (t1);
13401 TREE_TYPE (node) = boolean_type_node;
13402 error_found = 1;
13403 break;
13404 }
13405 prom_type = boolean_type_node;
13406 break;
13407 }
13408
e04a16fb
AG
13409 if (error_found)
13410 return error_mark_node;
13411
13412 TREE_OPERAND (node, 0) = op1;
13413 TREE_OPERAND (node, 1) = op2;
13414 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
13415 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13416
ce6e9147
APB
13417 if (flag_emit_xref)
13418 return node;
13419
d1472141
PB
13420 /* fold does not respect side-effect order as required for Java but not C.
13421 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13422 * bytecode.
13423 */
13424 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13425 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
13426 node = fold (node);
13427 return node;
e04a16fb
AG
13428}
13429
b67d701b
PB
13430/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13431 zero value, the value of CSTE comes after the valude of STRING */
13432
13433static tree
13434do_merge_string_cste (cste, string, string_len, after)
13435 tree cste;
49f48c71 13436 const char *string;
b67d701b
PB
13437 int string_len, after;
13438{
49f48c71 13439 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
13440 int old_len = TREE_STRING_LENGTH (cste);
13441 int len = old_len + string_len;
520a57c8 13442 char *new = alloca (len+1);
354e99ce 13443
b67d701b
PB
13444 if (after)
13445 {
354e99ce
APB
13446 memcpy (new, string, string_len);
13447 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13448 }
13449 else
13450 {
354e99ce
APB
13451 memcpy (new, old, old_len);
13452 memcpy (&new [old_len], string, string_len);
b67d701b 13453 }
354e99ce 13454 new [len] = '\0';
520a57c8 13455 return build_string (len, new);
b67d701b
PB
13456}
13457
13458/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13459 new STRING_CST on success, NULL_TREE on failure */
13460
13461static tree
13462merge_string_cste (op1, op2, after)
13463 tree op1, op2;
13464 int after;
13465{
13466 /* Handle two string constants right away */
13467 if (TREE_CODE (op2) == STRING_CST)
13468 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13469 TREE_STRING_LENGTH (op2), after);
13470
13471 /* Reasonable integer constant can be treated right away */
13472 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13473 {
49f48c71
KG
13474 static const char *boolean_true = "true";
13475 static const char *boolean_false = "false";
13476 static const char *null_pointer = "null";
b67d701b 13477 char ch[3];
49f48c71 13478 const char *string;
b67d701b
PB
13479
13480 if (op2 == boolean_true_node)
13481 string = boolean_true;
13482 else if (op2 == boolean_false_node)
13483 string = boolean_false;
13484 else if (op2 == null_pointer_node)
13485 string = null_pointer;
13486 else if (TREE_TYPE (op2) == char_type_node)
13487 {
13488 ch[0] = (char )TREE_INT_CST_LOW (op2);
13489 ch[1] = '\0';
13490 string = ch;
13491 }
13492 else
13493 string = print_int_node (op2);
13494
13495 return do_merge_string_cste (op1, string, strlen (string), after);
13496 }
13497 return NULL_TREE;
13498}
13499
13500/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13501 has to be a STRING_CST and the other part must be a STRING_CST or a
13502 INTEGRAL constant. Return a new STRING_CST if the operation
13503 succeed, NULL_TREE otherwise.
13504
13505 If the case we want to optimize for space, we might want to return
13506 NULL_TREE for each invocation of this routine. FIXME */
13507
13508static tree
13509string_constant_concatenation (op1, op2)
13510 tree op1, op2;
13511{
13512 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13513 {
0a2138e2 13514 tree string, rest;
b67d701b
PB
13515 int invert;
13516
13517 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13518 rest = (string == op1 ? op2 : op1);
13519 invert = (string == op1 ? 0 : 1 );
13520
13521 /* Walk REST, only if it looks reasonable */
13522 if (TREE_CODE (rest) != STRING_CST
13523 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13524 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13525 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13526 {
13527 rest = java_complete_tree (rest);
13528 if (rest == error_mark_node)
13529 return error_mark_node;
13530 rest = fold (rest);
13531 }
13532 return merge_string_cste (string, rest, invert);
13533 }
13534 return NULL_TREE;
13535}
13536
13537/* Implement the `+' operator. Does static optimization if possible,
13538 otherwise create (if necessary) and append elements to a
13539 StringBuffer. The StringBuffer will be carried around until it is
13540 used for a function call or an assignment. Then toString() will be
13541 called on it to turn it into a String object. */
13542
13543static tree
13544build_string_concatenation (op1, op2)
13545 tree op1, op2;
13546{
13547 tree result;
dc0b3eff 13548 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13549
13550 if (flag_emit_xref)
13551 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13552
13553 /* Try to do some static optimization */
13554 if ((result = string_constant_concatenation (op1, op2)))
13555 return result;
13556
c0d87ff6
PB
13557 /* Discard empty strings on either side of the expression */
13558 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13559 {
13560 op1 = op2;
13561 op2 = NULL_TREE;
13562 }
c0d87ff6 13563 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13564 op2 = NULL_TREE;
b67d701b 13565
acd663ee 13566 /* If operands are string constant, turn then into object references */
b67d701b
PB
13567 if (TREE_CODE (op1) == STRING_CST)
13568 op1 = patch_string_cst (op1);
acd663ee 13569 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13570 op2 = patch_string_cst (op2);
13571
acd663ee
APB
13572 /* If either one of the constant is null and the other non null
13573 operand is a String object, return it. */
13574 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13575 return op1;
13576
b67d701b
PB
13577 /* If OP1 isn't already a StringBuffer, create and
13578 initialize a new one */
13579 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13580 {
13581 /* Two solutions here:
c52b5771
AG
13582 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13583 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13584 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13585 op1 = BUILD_STRING_BUFFER (op1);
13586 else
13587 {
13588 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13589 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13590 }
13591 }
13592
acd663ee
APB
13593 if (op2)
13594 {
13595 /* OP1 is no longer the last node holding a crafted StringBuffer */
13596 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13597 /* Create a node for `{new...,xxx}.append (op2)' */
13598 if (op2)
13599 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13600 }
13601
b67d701b
PB
13602 /* Mark the last node holding a crafted StringBuffer */
13603 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13604
13605 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13606 return op1;
13607}
13608
13609/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13610 StringBuffer. If no string were found to be patched, return
13611 NULL. */
13612
13613static tree
13614patch_string (node)
13615 tree node;
13616{
1179ebc2
APB
13617 if (node == error_mark_node)
13618 return error_mark_node;
b67d701b
PB
13619 if (TREE_CODE (node) == STRING_CST)
13620 return patch_string_cst (node);
13621 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13622 {
c877974e 13623 int saved = ctxp->explicit_constructor_p;
b67d701b 13624 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13625 tree ret;
13626 /* Temporary disable forbid the use of `this'. */
13627 ctxp->explicit_constructor_p = 0;
13628 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13629 /* String concatenation arguments must be evaluated in order too. */
13630 ret = force_evaluation_order (ret);
c877974e
APB
13631 /* Restore it at its previous value */
13632 ctxp->explicit_constructor_p = saved;
13633 return ret;
b67d701b
PB
13634 }
13635 return NULL_TREE;
13636}
13637
13638/* Build the internal representation of a string constant. */
13639
13640static tree
13641patch_string_cst (node)
13642 tree node;
13643{
13644 int location;
15fdcfe9
PB
13645 if (! flag_emit_class_files)
13646 {
15fdcfe9
PB
13647 node = get_identifier (TREE_STRING_POINTER (node));
13648 location = alloc_name_constant (CONSTANT_String, node);
13649 node = build_ref_from_constant_pool (location);
13650 }
cd9643f7 13651 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13652 TREE_CONSTANT (node) = 1;
13653 return node;
13654}
13655
13656/* Build an incomplete unary operator expression. */
e04a16fb
AG
13657
13658static tree
13659build_unaryop (op_token, op_location, op1)
13660 int op_token, op_location;
13661 tree op1;
13662{
13663 enum tree_code op;
13664 tree unaryop;
13665 switch (op_token)
13666 {
b67d701b 13667 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13668 case MINUS_TK: op = NEGATE_EXPR; break;
13669 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13670 case NOT_TK: op = BIT_NOT_EXPR; break;
400500c4 13671 default: abort ();
e04a16fb
AG
13672 }
13673
13674 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13675 TREE_SIDE_EFFECTS (unaryop) = 1;
13676 /* Store the location of the operator, for better error report. The
13677 string of the operator will be rebuild based on the OP value. */
13678 EXPR_WFL_LINECOL (unaryop) = op_location;
13679 return unaryop;
13680}
13681
13682/* Special case for the ++/-- operators, since they require an extra
13683 argument to build, which is set to NULL and patched
13684 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13685
13686static tree
13687build_incdec (op_token, op_location, op1, is_post_p)
13688 int op_token, op_location;
13689 tree op1;
13690 int is_post_p;
13691{
13692 static enum tree_code lookup [2][2] =
13693 {
13694 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13695 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13696 };
13697 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13698 NULL_TREE, op1, NULL_TREE);
13699 TREE_SIDE_EFFECTS (node) = 1;
13700 /* Store the location of the operator, for better error report. The
13701 string of the operator will be rebuild based on the OP value. */
13702 EXPR_WFL_LINECOL (node) = op_location;
13703 return node;
13704}
13705
13706/* Build an incomplete cast operator, based on the use of the
13707 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13708 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13709 though its type is already set. */
13710
13711static tree
13712build_cast (location, type, exp)
13713 int location;
13714 tree type, exp;
13715{
13716 tree node = build1 (CONVERT_EXPR, type, exp);
13717 EXPR_WFL_LINECOL (node) = location;
13718 return node;
13719}
13720
c2952b01
APB
13721/* Build an incomplete class reference operator. */
13722static tree
13723build_incomplete_class_ref (location, class_name)
13724 int location;
13725 tree class_name;
13726{
13727 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13728 EXPR_WFL_LINECOL (node) = location;
13729 return node;
13730}
13731
13732/* Complete an incomplete class reference operator. */
13733static tree
13734patch_incomplete_class_ref (node)
13735 tree node;
13736{
13737 tree type = TREE_OPERAND (node, 0);
13738 tree ref_type;
13739
13740 if (!(ref_type = resolve_type_during_patch (type)))
13741 return error_mark_node;
13742
165f37bc 13743 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13744 {
13745 /* A class referenced by `foo.class' is initialized. */
13746 return build_class_init (ref_type, build_class_ref (ref_type));
13747 }
165f37bc
APB
13748
13749 /* If we're emitting class files and we have to deal with non
13750 primitive types, we invoke (and consider generating) the
13751 synthetic static method `class$'. */
13752 if (!TYPE_DOT_CLASS (current_class))
13753 build_dot_class_method (current_class);
f0f3a777 13754 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13755 return java_complete_tree (ref_type);
c2952b01
APB
13756}
13757
e04a16fb
AG
13758/* 15.14 Unary operators. We return error_mark_node in case of error,
13759 but preserve the type of NODE if the type is fixed. */
13760
13761static tree
13762patch_unaryop (node, wfl_op)
13763 tree node;
13764 tree wfl_op;
13765{
13766 tree op = TREE_OPERAND (node, 0);
13767 tree op_type = TREE_TYPE (op);
ab3a6dd6 13768 tree prom_type = NULL_TREE, value, decl;
c2952b01 13769 int outer_field_flag = 0;
e04a16fb
AG
13770 int code = TREE_CODE (node);
13771 int error_found = 0;
13772
13773 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13774
13775 switch (code)
13776 {
13777 /* 15.13.2 Postfix Increment Operator ++ */
13778 case POSTINCREMENT_EXPR:
13779 /* 15.13.3 Postfix Increment Operator -- */
13780 case POSTDECREMENT_EXPR:
13781 /* 15.14.1 Prefix Increment Operator ++ */
13782 case PREINCREMENT_EXPR:
13783 /* 15.14.2 Prefix Decrement Operator -- */
13784 case PREDECREMENT_EXPR:
5cbdba64 13785 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13786 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13787 /* We might be trying to change an outer field accessed using
13788 access method. */
13789 if (outer_field_flag)
13790 {
13791 /* Retrieve the decl of the field we're trying to access. We
13792 do that by first retrieving the function we would call to
13793 access the field. It has been already verified that this
13794 field isn't final */
13795 if (flag_emit_class_files)
13796 decl = TREE_OPERAND (op, 0);
13797 else
13798 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13799 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13800 }
b3edebcf 13801 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13802 else if (!JDECL_P (decl)
b3edebcf
APB
13803 && TREE_CODE (decl) != COMPONENT_REF
13804 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13805 && TREE_CODE (decl) != INDIRECT_REF
13806 && !(TREE_CODE (decl) == COMPOUND_EXPR
13807 && TREE_OPERAND (decl, 1)
13808 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13809 {
5e942c50
APB
13810 tree lvalue;
13811 /* Before screaming, check that we're not in fact trying to
13812 increment a optimized static final access, in which case
13813 we issue an different error message. */
13814 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13815 && resolve_expression_name (wfl_op, &lvalue)
13816 && check_final_assignment (lvalue, wfl_op)))
13817 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13818 operator_string (node));
e04a16fb
AG
13819 TREE_TYPE (node) = error_mark_node;
13820 error_found = 1;
13821 }
c2952b01
APB
13822
13823 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13824 error_found = 1;
13825
e04a16fb
AG
13826 /* From now on, we know that op if a variable and that it has a
13827 valid wfl. We use wfl_op to locate errors related to the
13828 ++/-- operand. */
13829 else if (!JNUMERIC_TYPE_P (op_type))
13830 {
13831 parse_error_context
13832 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13833 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13834 TREE_TYPE (node) = error_mark_node;
13835 error_found = 1;
13836 }
13837 else
13838 {
4a5f66c3 13839 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13840 both operands, if really necessary */
13841 if (JINTEGRAL_TYPE_P (op_type))
13842 {
13843 value = build_int_2 (1, 0);
13844 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13845 }
13846 else
13847 {
13848 value = build_int_2 (1, 0);
13849 TREE_TYPE (node) =
13850 binary_numeric_promotion (op_type,
13851 TREE_TYPE (value), &op, &value);
13852 }
c2952b01
APB
13853
13854 /* We remember we might be accessing an outer field */
13855 if (outer_field_flag)
13856 {
13857 /* We re-generate an access to the field */
13858 value = build (PLUS_EXPR, TREE_TYPE (op),
13859 build_outer_field_access (wfl_op, decl), value);
13860
13861 /* And we patch the original access$() into a write
13862 with plus_op as a rhs */
13863 return outer_field_access_fix (node, op, value);
13864 }
13865
5cbdba64 13866 /* And write back into the node. */
4a5f66c3 13867 TREE_OPERAND (node, 0) = op;
e04a16fb 13868 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13869 /* Convert the overall back into its original type, if
13870 necessary, and return */
13871 if (JINTEGRAL_TYPE_P (op_type))
13872 return fold (node);
13873 else
13874 return fold (convert (op_type, node));
e04a16fb
AG
13875 }
13876 break;
13877
13878 /* 15.14.3 Unary Plus Operator + */
b67d701b 13879 case UNARY_PLUS_EXPR:
e04a16fb
AG
13880 /* 15.14.4 Unary Minus Operator - */
13881 case NEGATE_EXPR:
13882 if (!JNUMERIC_TYPE_P (op_type))
13883 {
13884 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13885 TREE_TYPE (node) = error_mark_node;
13886 error_found = 1;
13887 }
13888 /* Unary numeric promotion is performed on operand */
13889 else
13890 {
15fdcfe9
PB
13891 op = do_unary_numeric_promotion (op);
13892 prom_type = TREE_TYPE (op);
b67d701b 13893 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13894 return fold (op);
e04a16fb
AG
13895 }
13896 break;
13897
13898 /* 15.14.5 Bitwise Complement Operator ~ */
13899 case BIT_NOT_EXPR:
13900 if (!JINTEGRAL_TYPE_P (op_type))
13901 {
13902 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13903 TREE_TYPE (node) = error_mark_node;
13904 error_found = 1;
13905 }
13906 else
13907 {
15fdcfe9
PB
13908 op = do_unary_numeric_promotion (op);
13909 prom_type = TREE_TYPE (op);
e04a16fb
AG
13910 }
13911 break;
13912
13913 /* 15.14.6 Logical Complement Operator ! */
13914 case TRUTH_NOT_EXPR:
13915 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13916 {
13917 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13918 /* But the type is known. We will report an error if further
13919 attempt of a assignment is made with this rhs */
e04a16fb
AG
13920 TREE_TYPE (node) = boolean_type_node;
13921 error_found = 1;
13922 }
13923 else
13924 prom_type = boolean_type_node;
13925 break;
13926
13927 /* 15.15 Cast Expression */
13928 case CONVERT_EXPR:
0a2138e2 13929 value = patch_cast (node, wfl_operator);
e04a16fb 13930 if (value == error_mark_node)
c877974e
APB
13931 {
13932 /* If this cast is part of an assignment, we tell the code
13933 that deals with it not to complain about a mismatch,
13934 because things have been cast, anyways */
13935 TREE_TYPE (node) = error_mark_node;
13936 error_found = 1;
13937 }
13938 else
dc0b3eff
PB
13939 {
13940 value = fold (value);
13941 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13942 return value;
13943 }
e04a16fb
AG
13944 break;
13945 }
13946
e04a16fb
AG
13947 if (error_found)
13948 return error_mark_node;
4a5f66c3
APB
13949
13950 /* There are cases where node has been replaced by something else
13951 and we don't end up returning here: UNARY_PLUS_EXPR,
13952 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13953 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13954 TREE_TYPE (node) = prom_type;
dc0b3eff 13955 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13956 return fold (node);
13957}
13958
13959/* Generic type resolution that sometimes takes place during node
13960 patching. Returned the resolved type or generate an error
13961 message. Return the resolved type or NULL_TREE. */
13962
13963static tree
13964resolve_type_during_patch (type)
13965 tree type;
13966{
13967 if (unresolved_type_p (type, NULL))
13968 {
c0b00d37 13969 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13970 if (!type_decl)
13971 {
13972 parse_error_context (type,
13973 "Class `%s' not found in type declaration",
13974 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13975 return NULL_TREE;
13976 }
13977 else
5e942c50
APB
13978 {
13979 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13980 return TREE_TYPE (type_decl);
13981 }
e04a16fb
AG
13982 }
13983 return type;
13984}
13985/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13986 found. Otherwise NODE or something meant to replace it is returned. */
13987
13988static tree
19e223db 13989patch_cast (node, wfl_op)
e04a16fb 13990 tree node;
19e223db 13991 tree wfl_op;
e04a16fb
AG
13992{
13993 tree op = TREE_OPERAND (node, 0);
13994 tree op_type = TREE_TYPE (op);
13995 tree cast_type = TREE_TYPE (node);
13996 char *t1;
13997
13998 /* First resolve OP_TYPE if unresolved */
13999 if (!(cast_type = resolve_type_during_patch (cast_type)))
14000 return error_mark_node;
14001
14002 /* Check on cast that are proven correct at compile time */
14003 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14004 {
e04a16fb
AG
14005 /* Same type */
14006 if (cast_type == op_type)
14007 return node;
14008
0b4d333e
APB
14009 /* float and double type are converted to the original type main
14010 variant and then to the target type. */
14011 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14012 op = convert (integer_type_node, op);
14013
e04a16fb
AG
14014 /* Try widening/narowwing convertion. Potentially, things need
14015 to be worked out in gcc so we implement the extreme cases
14016 correctly. fold_convert() needs to be fixed. */
14017 return convert (cast_type, op);
14018 }
14019
0b4d333e
APB
14020 /* It's also valid to cast a boolean into a boolean */
14021 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14022 return node;
14023
5e942c50
APB
14024 /* null can be casted to references */
14025 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14026 return build_null_of_type (cast_type);
14027
e04a16fb
AG
14028 /* The remaining legal casts involve conversion between reference
14029 types. Check for their compile time correctness. */
14030 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 14031 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
14032 {
14033 TREE_TYPE (node) = promote_type (cast_type);
14034 /* Now, the case can be determined correct at compile time if
14035 OP_TYPE can be converted into CAST_TYPE by assignment
14036 conversion (5.2) */
14037
14038 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
14039 {
14040 TREE_SET_CODE (node, NOP_EXPR);
14041 return node;
14042 }
14043
14044 if (flag_emit_class_files)
14045 {
14046 TREE_SET_CODE (node, CONVERT_EXPR);
14047 return node;
14048 }
e04a16fb
AG
14049
14050 /* The cast requires a run-time check */
14051 return build (CALL_EXPR, promote_type (cast_type),
14052 build_address_of (soft_checkcast_node),
14053 tree_cons (NULL_TREE, build_class_ref (cast_type),
14054 build_tree_list (NULL_TREE, op)),
14055 NULL_TREE);
14056 }
14057
14058 /* Any other casts are proven incorrect at compile time */
c2e3db92 14059 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 14060 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 14061 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
14062 free (t1);
14063 return error_mark_node;
14064}
14065
5e942c50
APB
14066/* Build a null constant and give it the type TYPE. */
14067
14068static tree
14069build_null_of_type (type)
14070 tree type;
14071{
14072 tree node = build_int_2 (0, 0);
14073 TREE_TYPE (node) = promote_type (type);
14074 return node;
14075}
14076
e04a16fb
AG
14077/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14078 a list of indices. */
14079static tree
14080build_array_ref (location, array, index)
14081 int location;
14082 tree array, index;
14083{
14084 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14085 EXPR_WFL_LINECOL (node) = location;
14086 return node;
14087}
14088
14089/* 15.12 Array Access Expression */
14090
14091static tree
c877974e
APB
14092patch_array_ref (node)
14093 tree node;
e04a16fb
AG
14094{
14095 tree array = TREE_OPERAND (node, 0);
14096 tree array_type = TREE_TYPE (array);
14097 tree index = TREE_OPERAND (node, 1);
14098 tree index_type = TREE_TYPE (index);
e04a16fb
AG
14099 int error_found = 0;
14100
14101 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14102
e04a16fb
AG
14103 if (TREE_CODE (array_type) == POINTER_TYPE)
14104 array_type = TREE_TYPE (array_type);
14105
14106 /* The array reference must be an array */
14107 if (!TYPE_ARRAY_P (array_type))
14108 {
14109 parse_error_context
781b0558
KG
14110 (wfl_operator,
14111 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14112 lang_printable_name (array_type, 0));
e04a16fb
AG
14113 TREE_TYPE (node) = error_mark_node;
14114 error_found = 1;
14115 }
14116
c2952b01 14117 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 14118 type must be int */
15fdcfe9
PB
14119 index = do_unary_numeric_promotion (index);
14120 if (TREE_TYPE (index) != int_type_node)
e04a16fb 14121 {
1ebadc60 14122 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
14123 parse_error_context (wfl_operator,
14124 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
14125 lang_printable_name (index_type, 0));
14126 else
781b0558
KG
14127 parse_error_context (wfl_operator,
14128 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 14129 lang_printable_name (index_type, 0));
e04a16fb
AG
14130 TREE_TYPE (node) = error_mark_node;
14131 error_found = 1;
14132 }
14133
e04a16fb
AG
14134 if (error_found)
14135 return error_mark_node;
e04a16fb 14136
5e942c50 14137 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 14138
7f1d4866 14139 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 14140 {
15fdcfe9
PB
14141 TREE_OPERAND (node, 0) = array;
14142 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
14143 }
14144 else
939d7216
PB
14145 {
14146 /* The save_expr is for correct evaluation order. It would be cleaner
14147 to use force_evaluation_order (see comment there), but that is
14148 difficult when we also have to deal with bounds checking. */
14149 if (TREE_SIDE_EFFECTS (index))
14150 array = save_expr (array);
14151 node = build_java_arrayaccess (array, array_type, index);
14152 if (TREE_SIDE_EFFECTS (index))
14153 node = build (COMPOUND_EXPR, array_type, array, node);
14154 }
e04a16fb
AG
14155 TREE_TYPE (node) = array_type;
14156 return node;
14157}
14158
14159/* 15.9 Array Creation Expressions */
14160
14161static tree
14162build_newarray_node (type, dims, extra_dims)
14163 tree type;
14164 tree dims;
14165 int extra_dims;
14166{
14167 tree node =
b67d701b 14168 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 14169 build_int_2 (extra_dims, 0));
e04a16fb
AG
14170 return node;
14171}
14172
14173static tree
14174patch_newarray (node)
14175 tree node;
14176{
14177 tree type = TREE_OPERAND (node, 0);
14178 tree dims = TREE_OPERAND (node, 1);
14179 tree cdim, array_type;
14180 int error_found = 0;
14181 int ndims = 0;
14182 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
14183
14184 /* Dimension types are verified. It's better for the types to be
14185 verified in order. */
14186 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14187 {
14188 int dim_error = 0;
14189 tree dim = TREE_VALUE (cdim);
14190
14191 /* Dim might have been saved during its evaluation */
dba41d30 14192 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
e04a16fb
AG
14193
14194 /* The type of each specified dimension must be an integral type. */
14195 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14196 dim_error = 1;
14197
14198 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14199 promoted type must be int. */
14200 else
14201 {
15fdcfe9 14202 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
14203 if (TREE_TYPE (dim) != int_type_node)
14204 dim_error = 1;
14205 }
14206
14207 /* Report errors on types here */
14208 if (dim_error)
14209 {
14210 parse_error_context
14211 (TREE_PURPOSE (cdim),
781b0558 14212 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 14213 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 14214 "Explicit cast needed to" : "Can't"),
0a2138e2 14215 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
14216 error_found = 1;
14217 }
14218
e04a16fb
AG
14219 TREE_PURPOSE (cdim) = NULL_TREE;
14220 }
14221
14222 /* Resolve array base type if unresolved */
14223 if (!(type = resolve_type_during_patch (type)))
14224 error_found = 1;
14225
14226 if (error_found)
14227 {
14228 /* We don't want further evaluation of this bogus array creation
14229 operation */
14230 TREE_TYPE (node) = error_mark_node;
14231 return error_mark_node;
14232 }
14233
15fdcfe9
PB
14234 /* Set array_type to the actual (promoted) array type of the result. */
14235 if (TREE_CODE (type) == RECORD_TYPE)
14236 type = build_pointer_type (type);
14237 while (--xdims >= 0)
14238 {
14239 type = promote_type (build_java_array_type (type, -1));
14240 }
14241 dims = nreverse (dims);
14242 array_type = type;
14243 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14244 {
14245 type = array_type;
05bccae2
RK
14246 array_type
14247 = build_java_array_type (type,
14248 TREE_CODE (cdim) == INTEGER_CST
14249 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14250 : -1);
15fdcfe9
PB
14251 array_type = promote_type (array_type);
14252 }
14253 dims = nreverse (dims);
14254
e04a16fb
AG
14255 /* The node is transformed into a function call. Things are done
14256 differently according to the number of dimensions. If the number
14257 of dimension is equal to 1, then the nature of the base type
14258 (primitive or not) matters. */
15fdcfe9 14259 if (ndims == 1)
fdec99c6 14260 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 14261
e04a16fb
AG
14262 /* Can't reuse what's already written in expr.c because it uses the
14263 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 14264 return build (CALL_EXPR, array_type,
e04a16fb 14265 build_address_of (soft_multianewarray_node),
15fdcfe9 14266 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 14267 tree_cons (NULL_TREE,
15fdcfe9 14268 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
14269 NULL_TREE);
14270}
14271
f8976021
APB
14272/* 10.6 Array initializer. */
14273
14274/* Build a wfl for array element that don't have one, so we can
14275 pin-point errors. */
14276
14277static tree
14278maybe_build_array_element_wfl (node)
14279 tree node;
14280{
14281 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14282 return build_expr_wfl (NULL_TREE, ctxp->filename,
14283 ctxp->elc.line, ctxp->elc.prev_col);
14284 else
14285 return NULL_TREE;
14286}
14287
14288/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14289 identification of initialized arrays easier to detect during walk
14290 and expansion. */
14291
14292static tree
14293build_new_array_init (location, values)
14294 int location;
14295 tree values;
14296{
14297 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14298 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 14299 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
14300 return to_return;
14301}
14302
14303/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14304 occurred. Otherwise return NODE after having set its type
14305 appropriately. */
14306
14307static tree
14308patch_new_array_init (type, node)
14309 tree type, node;
f8976021
APB
14310{
14311 int error_seen = 0;
fdec99c6 14312 tree current, element_type;
f8976021 14313 HOST_WIDE_INT length;
fdec99c6
PB
14314 int all_constant = 1;
14315 tree init = TREE_OPERAND (node, 0);
f8976021 14316
fdec99c6
PB
14317 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14318 {
14319 parse_error_context (node,
14320 "Invalid array initializer for non-array type `%s'",
14321 lang_printable_name (type, 1));
14322 return error_mark_node;
14323 }
14324 type = TREE_TYPE (type);
14325 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 14326
fdec99c6
PB
14327 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14328
14329 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14330 current; length++, current = TREE_CHAIN (current))
f8976021 14331 {
fdec99c6
PB
14332 tree elt = TREE_VALUE (current);
14333 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 14334 {
fdec99c6 14335 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
14336 elt = TREE_VALUE (current);
14337 /* When compiling to native code, STRING_CST is converted to
14338 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14339 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 14340 all_constant = 0;
f8976021 14341 }
fdec99c6
PB
14342 else
14343 {
14344 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14345 TREE_PURPOSE (current) = NULL_TREE;
14346 all_constant = 0;
14347 }
9a7ab4b3
APB
14348 if (elt && TREE_CODE (elt) == TREE_LIST
14349 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 14350 error_seen = 1;
f8976021
APB
14351 }
14352
14353 if (error_seen)
14354 return error_mark_node;
14355
14356 /* Create a new type. We can't reuse the one we have here by
14357 patching its dimension because it originally is of dimension -1
14358 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
14359 type = build_java_array_type (element_type, length);
14360 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14361 TREE_TYPE (node) = promote_type (type);
14362 TREE_CONSTANT (init) = all_constant;
bc3ca41b 14363 TREE_CONSTANT (node) = all_constant;
f8976021
APB
14364 return node;
14365}
14366
14367/* Verify that one entry of the initializer element list can be
14368 assigned to the array base type. Report 1 if an error occurred, 0
14369 otherwise. */
14370
14371static int
14372array_constructor_check_entry (type, entry)
14373 tree type, entry;
14374{
14375 char *array_type_string = NULL; /* For error reports */
14376 tree value, type_value, new_value, wfl_value, patched;
14377 int error_seen = 0;
14378
14379 new_value = NULL_TREE;
14380 wfl_value = TREE_VALUE (entry);
14381
f8976021 14382 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 14383 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
14384 if ((patched = patch_string (value)))
14385 value = patched;
1179ebc2
APB
14386 if (value == error_mark_node)
14387 return 1;
f8976021 14388
f8976021
APB
14389 type_value = TREE_TYPE (value);
14390
1179ebc2 14391 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
14392 constant overflow during narrowing. */
14393 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14394 new_value = try_builtin_assignconv (wfl_operator, type, value);
14395 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14396 type_value = promote_type (type);
100f7cd8 14397
f8976021
APB
14398 /* Check and report errors */
14399 if (!new_value)
14400 {
49f48c71 14401 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
14402 "Can't" : "Explicit cast needed to");
14403 if (!array_type_string)
c2e3db92 14404 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
14405 parse_error_context
14406 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14407 msg, lang_printable_name (type_value, 1), array_type_string);
14408 error_seen = 1;
14409 }
14410
14411 if (new_value)
14412 {
b8c5b1c6 14413 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
14414 TREE_VALUE (entry) = new_value;
14415 }
14416
14417 if (array_type_string)
14418 free (array_type_string);
14419
14420 TREE_PURPOSE (entry) = NULL_TREE;
14421 return error_seen;
14422}
14423
e04a16fb
AG
14424static tree
14425build_this (location)
14426 int location;
14427{
9ee9b555 14428 tree node = build_wfl_node (this_identifier_node);
b67d701b 14429 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
14430 EXPR_WFL_LINECOL (node) = location;
14431 return node;
14432}
14433
14434/* 14.15 The return statement. It builds a modify expression that
14435 assigns the returned value to the RESULT_DECL that hold the value
14436 to be returned. */
14437
14438static tree
14439build_return (location, op)
14440 int location;
14441 tree op;
14442{
14443 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14444 EXPR_WFL_LINECOL (node) = location;
b67d701b 14445 node = build_debugable_stmt (location, node);
e04a16fb
AG
14446 return node;
14447}
14448
14449static tree
14450patch_return (node)
14451 tree node;
14452{
14453 tree return_exp = TREE_OPERAND (node, 0);
14454 tree meth = current_function_decl;
14455 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14456 int error_found = 0;
14457
14458 TREE_TYPE (node) = error_mark_node;
14459 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14460
14461 /* It's invalid to have a return value within a function that is
14462 declared with the keyword void or that is a constructor */
14463 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14464 error_found = 1;
14465
f099f336 14466 /* It's invalid to use a return statement in a static block */
c2952b01 14467 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14468 error_found = 1;
14469
e04a16fb
AG
14470 /* It's invalid to have a no return value within a function that
14471 isn't declared with the keyword `void' */
14472 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14473 error_found = 2;
c2952b01
APB
14474
14475 if (in_instance_initializer)
14476 error_found = 1;
e04a16fb
AG
14477
14478 if (error_found)
14479 {
c2952b01 14480 if (in_instance_initializer)
f099f336 14481 parse_error_context (wfl_operator,
c2952b01
APB
14482 "`return' inside instance initializer");
14483
14484 else if (DECL_CLINIT_P (current_function_decl))
14485 parse_error_context (wfl_operator,
14486 "`return' inside static initializer");
f099f336
APB
14487
14488 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14489 {
c2e3db92 14490 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14491 parse_error_context (wfl_operator,
14492 "`return' with%s value from `%s %s'",
14493 (error_found == 1 ? "" : "out"),
14494 t, lang_printable_name (meth, 0));
14495 free (t);
14496 }
14497 else
14498 parse_error_context (wfl_operator,
14499 "`return' with value from constructor `%s'",
14500 lang_printable_name (meth, 0));
e04a16fb
AG
14501 return error_mark_node;
14502 }
14503
5e942c50
APB
14504 /* If we have a return_exp, build a modify expression and expand
14505 it. Note: at that point, the assignment is declared valid, but we
14506 may want to carry some more hacks */
e04a16fb
AG
14507 if (return_exp)
14508 {
5e942c50
APB
14509 tree exp = java_complete_tree (return_exp);
14510 tree modify, patched;
14511
14512 /* If the function returned value and EXP are booleans, EXP has
14513 to be converted into the type of DECL_RESULT, which is integer
14514 (see complete_start_java_method) */
14515 if (TREE_TYPE (exp) == boolean_type_node &&
14516 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14517 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14518
14519 /* `null' can be assigned to a function returning a reference */
14520 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14521 exp == null_pointer_node)
14522 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14523
14524 if ((patched = patch_string (exp)))
14525 exp = patched;
14526
14527 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14528 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14529 modify = java_complete_tree (modify);
5e942c50 14530
e04a16fb
AG
14531 if (modify != error_mark_node)
14532 {
14533 TREE_SIDE_EFFECTS (modify) = 1;
14534 TREE_OPERAND (node, 0) = modify;
14535 }
14536 else
14537 return error_mark_node;
14538 }
14539 TREE_TYPE (node) = void_type_node;
14540 TREE_SIDE_EFFECTS (node) = 1;
14541 return node;
14542}
14543
14544/* 14.8 The if Statement */
14545
14546static tree
14547build_if_else_statement (location, expression, if_body, else_body)
14548 int location;
14549 tree expression, if_body, else_body;
14550{
14551 tree node;
e04a16fb 14552 if (!else_body)
9bbc7d9f 14553 else_body = empty_stmt_node;
e04a16fb
AG
14554 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14555 EXPR_WFL_LINECOL (node) = location;
b67d701b 14556 node = build_debugable_stmt (location, node);
e04a16fb
AG
14557 return node;
14558}
14559
14560static tree
14561patch_if_else_statement (node)
14562 tree node;
14563{
14564 tree expression = TREE_OPERAND (node, 0);
14565
14566 TREE_TYPE (node) = error_mark_node;
14567 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14568
14569 /* The type of expression must be boolean */
b67d701b
PB
14570 if (TREE_TYPE (expression) != boolean_type_node
14571 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14572 {
14573 parse_error_context
14574 (wfl_operator,
14575 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14576 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14577 return error_mark_node;
14578 }
14579
14580 TREE_TYPE (node) = void_type_node;
14581 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14582 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14583 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14584 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14585 return node;
14586}
14587
14588/* 14.6 Labeled Statements */
14589
14590/* Action taken when a lableled statement is parsed. a new
14591 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14592 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14593
14594static tree
0a2138e2 14595build_labeled_block (location, label)
e04a16fb 14596 int location;
0a2138e2 14597 tree label;
e04a16fb 14598{
b635eb2f 14599 tree label_name ;
e04a16fb 14600 tree label_decl, node;
b635eb2f
PB
14601 if (label == NULL_TREE || label == continue_identifier_node)
14602 label_name = label;
14603 else
e04a16fb 14604 {
b635eb2f
PB
14605 label_name = merge_qualified_name (label_id, label);
14606 /* Issue an error if we try to reuse a label that was previously
14607 declared */
14608 if (IDENTIFIER_LOCAL_VALUE (label_name))
14609 {
14610 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14611 parse_error_context (wfl_operator,
14612 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14613 IDENTIFIER_POINTER (label));
14614 EXPR_WFL_LINECOL (wfl_operator) =
14615 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14616 parse_error_context (wfl_operator,
14617 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14618 IDENTIFIER_POINTER (label));
14619 java_error_count--;
14620 }
e04a16fb
AG
14621 }
14622
14623 label_decl = create_label_decl (label_name);
14624 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14625 EXPR_WFL_LINECOL (node) = location;
14626 TREE_SIDE_EFFECTS (node) = 1;
14627 return node;
14628}
14629
b67d701b 14630/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14631
14632static tree
b635eb2f 14633finish_labeled_statement (lbe, statement)
e04a16fb
AG
14634 tree lbe; /* Labeled block expr */
14635 tree statement;
14636{
14637 /* In anyways, tie the loop to its statement */
14638 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14639 pop_labeled_block ();
14640 POP_LABELED_BLOCK ();
e04a16fb
AG
14641 return lbe;
14642}
14643
14644/* 14.10, 14.11, 14.12 Loop Statements */
14645
14646/* Create an empty LOOP_EXPR and make it the last in the nested loop
14647 list. */
14648
14649static tree
14650build_new_loop (loop_body)
14651 tree loop_body;
14652{
14653 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14654 TREE_SIDE_EFFECTS (loop) = 1;
14655 PUSH_LOOP (loop);
14656 return loop;
14657}
14658
14659/* Create a loop body according to the following structure:
14660 COMPOUND_EXPR
14661 COMPOUND_EXPR (loop main body)
14662 EXIT_EXPR (this order is for while/for loops.
14663 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14664 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14665 BODY end of this labeled block)
14666 INCREMENT (if any)
14667
14668 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14669 after the body, like in the do-while loop.
14670
14671 To obtain a loop, the loop body structure described above is
14672 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14673
14674 LABELED_BLOCK_EXPR
14675 LABEL_DECL (use this label to exit the loop)
14676 LOOP_EXPR
14677 <structure described above> */
e04a16fb
AG
14678
14679static tree
14680build_loop_body (location, condition, reversed)
14681 int location;
14682 tree condition;
14683 int reversed;
14684{
0a2138e2 14685 tree first, second, body;
e04a16fb
AG
14686
14687 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14688 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14689 condition = build_debugable_stmt (location, condition);
14690 TREE_SIDE_EFFECTS (condition) = 1;
14691
b635eb2f 14692 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14693 first = (reversed ? body : condition);
14694 second = (reversed ? condition : body);
14695 return
14696 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14697 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14698}
14699
14700/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14701 their order) on the current loop. Unlink the current loop from the
14702 loop list. */
14703
14704static tree
b635eb2f 14705finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14706 int location;
14707 tree condition, body;
14708 int reversed;
14709{
14710 tree to_return = ctxp->current_loop;
14711 tree loop_body = LOOP_EXPR_BODY (to_return);
14712 if (condition)
14713 {
14714 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14715 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14716 The real EXIT_EXPR is one operand further. */
14717 EXPR_WFL_LINECOL (cnode) = location;
14718 /* This one is for accurate error reports */
14719 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14720 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14721 }
14722 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14723 POP_LOOP ();
14724 return to_return;
14725}
14726
b635eb2f 14727/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14728 loops feature the condition part */
14729
14730static tree
b635eb2f 14731finish_for_loop (location, condition, update, body)
e04a16fb
AG
14732 int location;
14733 tree condition, update, body;
14734{
14735 /* Put the condition and the loop body in place */
b635eb2f 14736 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14737 /* LOOP is the current loop which has been now popped of the loop
14738 stack. Install the update block */
14739 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14740 return loop;
14741}
14742
5cbdba64
APB
14743/* Try to find the loop a block might be related to. This comprises
14744 the case where the LOOP_EXPR is found as the second operand of a
14745 COMPOUND_EXPR, because the loop happens to have an initialization
14746 part, then expressed as the first operand of the COMPOUND_EXPR. If
14747 the search finds something, 1 is returned. Otherwise, 0 is
14748 returned. The search is assumed to start from a
14749 LABELED_BLOCK_EXPR's block. */
14750
14751static tree
14752search_loop (statement)
14753 tree statement;
14754{
14755 if (TREE_CODE (statement) == LOOP_EXPR)
14756 return statement;
14757
14758 if (TREE_CODE (statement) == BLOCK)
14759 statement = BLOCK_SUBBLOCKS (statement);
14760 else
14761 return NULL_TREE;
14762
14763 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14764 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14765 statement = TREE_OPERAND (statement, 1);
14766
14767 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14768 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14769}
14770
14771/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14772 returned otherwise. */
14773
14774static int
14775labeled_block_contains_loop_p (block, loop)
14776 tree block, loop;
14777{
14778 if (!block)
14779 return 0;
14780
14781 if (LABELED_BLOCK_BODY (block) == loop)
14782 return 1;
14783
c2952b01 14784 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14785 return 1;
14786
14787 return 0;
14788}
14789
e04a16fb 14790/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14791 insert LOOP as its body. */
e04a16fb
AG
14792
14793static tree
14794patch_loop_statement (loop)
14795 tree loop;
14796{
cd9643f7 14797 tree loop_label;
5cbdba64 14798
cd9643f7 14799 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14800 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14801 return loop;
14802
cd9643f7 14803 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14804 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14805 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14806 LABELED_BLOCK_BODY (loop_label) = loop;
14807 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14808 return loop_label;
e04a16fb
AG
14809}
14810
14811/* 14.13, 14.14: break and continue Statements */
14812
14813/* Build a break or a continue statement. a null NAME indicates an
14814 unlabeled break/continue statement. */
14815
14816static tree
14817build_bc_statement (location, is_break, name)
14818 int location, is_break;
14819 tree name;
14820{
14821 tree break_continue, label_block_expr = NULL_TREE;
14822
14823 if (name)
14824 {
14825 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14826 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14827 /* Null means that we don't have a target for this named
14828 break/continue. In this case, we make the target to be the
14829 label name, so that the error can be reported accuratly in
14830 patch_bc_statement. */
14831 label_block_expr = EXPR_WFL_NODE (name);
14832 }
14833 /* Unlabeled break/continue will be handled during the
14834 break/continue patch operation */
14835 break_continue
14836 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14837
14838 IS_BREAK_STMT_P (break_continue) = is_break;
14839 TREE_SIDE_EFFECTS (break_continue) = 1;
14840 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14841 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14842 return break_continue;
14843}
14844
14845/* Verification of a break/continue statement. */
14846
14847static tree
14848patch_bc_statement (node)
14849 tree node;
14850{
14851 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14852 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14853 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14854
e04a16fb 14855 /* Having an identifier here means that the target is unknown. */
b635eb2f 14856 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14857 {
14858 parse_error_context (wfl_operator, "No label definition found for `%s'",
14859 IDENTIFIER_POINTER (bc_label));
14860 return error_mark_node;
14861 }
b635eb2f 14862 if (! IS_BREAK_STMT_P (node))
e04a16fb 14863 {
b635eb2f
PB
14864 /* It's a continue statement. */
14865 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14866 {
b635eb2f
PB
14867 if (labeled_block == NULL_TREE)
14868 {
14869 if (bc_label == NULL_TREE)
14870 parse_error_context (wfl_operator,
14871 "`continue' must be in loop");
14872 else
1504b2b4
APB
14873 parse_error_context
14874 (wfl_operator, "continue label `%s' does not name a loop",
14875 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14876 return error_mark_node;
14877 }
14878 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14879 == continue_identifier_node)
14880 && (bc_label == NULL_TREE
14881 || TREE_CHAIN (labeled_block) == bc_label))
14882 {
14883 bc_label = labeled_block;
14884 break;
14885 }
e04a16fb 14886 }
e04a16fb 14887 }
b635eb2f 14888 else if (!bc_label)
34f4db93 14889 {
b635eb2f 14890 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14891 {
b635eb2f
PB
14892 if (labeled_block == NULL_TREE)
14893 {
14894 parse_error_context (wfl_operator,
14895 "`break' must be in loop or switch");
14896 return error_mark_node;
14897 }
14898 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14899 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14900 || search_loop (target_stmt))
b635eb2f
PB
14901 {
14902 bc_label = labeled_block;
14903 break;
14904 }
e04a16fb 14905 }
e04a16fb
AG
14906 }
14907
b635eb2f 14908 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14909 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14910
e04a16fb
AG
14911 /* Our break/continue don't return values. */
14912 TREE_TYPE (node) = void_type_node;
14913 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14914 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14915 sometimes, like after a if statement) */
14916 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14917 TREE_SIDE_EFFECTS (node) = 1;
14918 return node;
14919}
14920
14921/* Process the exit expression belonging to a loop. Its type must be
14922 boolean. */
14923
14924static tree
14925patch_exit_expr (node)
14926 tree node;
14927{
14928 tree expression = TREE_OPERAND (node, 0);
14929 TREE_TYPE (node) = error_mark_node;
14930 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14931
14932 /* The type of expression must be boolean */
14933 if (TREE_TYPE (expression) != boolean_type_node)
14934 {
14935 parse_error_context
14936 (wfl_operator,
781b0558 14937 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14938 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14939 return error_mark_node;
14940 }
14941 /* Now we know things are allright, invert the condition, fold and
14942 return */
14943 TREE_OPERAND (node, 0) =
14944 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14945
14946 if (! integer_zerop (TREE_OPERAND (node, 0))
14947 && ctxp->current_loop != NULL_TREE
14948 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14949 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14950 if (! integer_onep (TREE_OPERAND (node, 0)))
14951 CAN_COMPLETE_NORMALLY (node) = 1;
14952
14953
e04a16fb
AG
14954 TREE_TYPE (node) = void_type_node;
14955 return node;
14956}
b67d701b
PB
14957
14958/* 14.9 Switch statement */
14959
14960static tree
14961patch_switch_statement (node)
14962 tree node;
14963{
c877974e 14964 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14965
14966 /* Complete the switch expression */
14967 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14968 se_type = TREE_TYPE (se);
14969 /* The type of the switch expression must be char, byte, short or
14970 int */
2e0f0aff 14971 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14972 {
14973 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14974 parse_error_context (wfl_operator,
14975 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14976 lang_printable_name (se_type, 0));
b67d701b
PB
14977 /* This is what java_complete_tree will check */
14978 TREE_OPERAND (node, 0) = error_mark_node;
14979 return error_mark_node;
14980 }
14981
15fdcfe9 14982 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14983
14984 /* Ready to return */
15fdcfe9 14985 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14986 {
14987 TREE_TYPE (node) = error_mark_node;
14988 return error_mark_node;
14989 }
14990 TREE_TYPE (node) = void_type_node;
14991 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14992 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14993 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14994 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14995 return node;
14996}
14997
165f37bc 14998/* 14.18 The try/catch statements */
b67d701b 14999
b67d701b 15000static tree
a7d8d81f 15001build_try_statement (location, try_block, catches)
b67d701b 15002 int location;
a7d8d81f
PB
15003 tree try_block, catches;
15004{
15005 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 15006 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 15007 return node;
b67d701b
PB
15008}
15009
a7d8d81f
PB
15010static tree
15011build_try_finally_statement (location, try_block, finally)
15012 int location;
15013 tree try_block, finally;
b67d701b 15014{
a7d8d81f
PB
15015 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15016 EXPR_WFL_LINECOL (node) = location;
15017 return node;
b67d701b
PB
15018}
15019
15020static tree
15021patch_try_statement (node)
15022 tree node;
15023{
15024 int error_found = 0;
15025 tree try = TREE_OPERAND (node, 0);
15026 /* Exception handlers are considered in left to right order */
15027 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 15028 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
15029
15030 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
15031 to process the next catch clause. We process the catch clause before
15032 the try block so that when processing the try block we can check thrown
15033 exceptions againts the caught type list. */
b67d701b
PB
15034 for (current = catch; current; current = TREE_CHAIN (current))
15035 {
15036 tree carg_decl, carg_type;
15037 tree sub_current, catch_block, catch_clause;
15038 int unreachable;
15039
b67d701b 15040 /* At this point, the structure of the catch clause is
b67d701b
PB
15041 CATCH_EXPR (catch node)
15042 BLOCK (with the decl of the parameter)
15043 COMPOUND_EXPR
7525cc04 15044 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 15045 BLOCK (catch clause block)
a7d8d81f
PB
15046 */
15047 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
15048 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15049 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15050
15051 /* Catch clauses can't have more than one parameter declared,
15052 but it's already enforced by the grammar. Make sure that the
15053 only parameter of the clause statement in of class Throwable
15054 or a subclass of Throwable, but that was done earlier. The
15055 catch clause parameter type has also been resolved. */
15056
15057 /* Just make sure that the catch clause parameter type inherits
15058 from java.lang.Throwable */
15059 if (!inherits_from_p (carg_type, throwable_type_node))
15060 {
15061 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15062 parse_error_context (wfl_operator,
781b0558 15063 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 15064 lang_printable_name (carg_type, 0));
b67d701b
PB
15065 error_found = 1;
15066 continue;
15067 }
15068
15069 /* Partial check for unreachable catch statement: The catch
15070 clause is reachable iff is no earlier catch block A in
15071 the try statement such that the type of the catch
15072 clause's parameter is the same as or a subclass of the
15073 type of A's parameter */
15074 unreachable = 0;
15075 for (sub_current = catch;
15076 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15077 {
15078 tree sub_catch_clause, decl;
a7d8d81f 15079 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
15080 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15081
15082 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15083 {
15084 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15085 parse_error_context
781b0558
KG
15086 (wfl_operator,
15087 "`catch' not reached because of the catch clause at line %d",
15088 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
15089 unreachable = error_found = 1;
15090 break;
15091 }
15092 }
b67d701b
PB
15093 /* Complete the catch clause block */
15094 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15095 if (catch_block == error_mark_node)
15096 {
15097 error_found = 1;
15098 continue;
15099 }
15fdcfe9
PB
15100 if (CAN_COMPLETE_NORMALLY (catch_block))
15101 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 15102 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
15103
15104 if (unreachable)
15105 continue;
15106
15107 /* Things to do here: the exception must be thrown */
15108
15109 /* Link this type to the caught type list */
15110 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
15111 }
15112
b9f7e36c
APB
15113 PUSH_EXCEPTIONS (caught_type_list);
15114 if ((try = java_complete_tree (try)) == error_mark_node)
15115 error_found = 1;
15fdcfe9
PB
15116 if (CAN_COMPLETE_NORMALLY (try))
15117 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
15118 POP_EXCEPTIONS ();
15119
b67d701b
PB
15120 /* Verification ends here */
15121 if (error_found)
15122 return error_mark_node;
15123
15124 TREE_OPERAND (node, 0) = try;
15125 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
15126 TREE_TYPE (node) = void_type_node;
15127 return node;
15128}
b9f7e36c
APB
15129
15130/* 14.17 The synchronized Statement */
15131
15132static tree
15133patch_synchronized_statement (node, wfl_op1)
15134 tree node, wfl_op1;
15135{
5a005d9e 15136 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 15137 tree block = TREE_OPERAND (node, 1);
5a005d9e 15138
c7303e41 15139 tree tmp, enter, exit, expr_decl, assignment;
5a005d9e
PB
15140
15141 if (expr == error_mark_node)
15142 {
15143 block = java_complete_tree (block);
15144 return expr;
15145 }
b9f7e36c 15146
c7303e41
APB
15147 /* We might be trying to synchronize on a STRING_CST */
15148 if ((tmp = patch_string (expr)))
15149 expr = tmp;
15150
b9f7e36c 15151 /* The TYPE of expr must be a reference type */
5a005d9e 15152 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
15153 {
15154 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 15155 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 15156 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
15157 return error_mark_node;
15158 }
15159
ce6e9147
APB
15160 if (flag_emit_xref)
15161 {
15162 TREE_OPERAND (node, 0) = expr;
15163 TREE_OPERAND (node, 1) = java_complete_tree (block);
15164 CAN_COMPLETE_NORMALLY (node) = 1;
15165 return node;
15166 }
15167
b9f7e36c
APB
15168 /* Generate a try-finally for the synchronized statement, except
15169 that the handler that catches all throw exception calls
15170 _Jv_MonitorExit and then rethrow the exception.
15171 The synchronized statement is then implemented as:
15172 TRY
15173 {
15174 _Jv_MonitorEnter (expression)
15175 synchronized_block
15176 _Jv_MonitorExit (expression)
15177 }
15178 CATCH_ALL
15179 {
15180 e = _Jv_exception_info ();
15181 _Jv_MonitorExit (expression)
15182 Throw (e);
15183 } */
15184
5a005d9e
PB
15185 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15186 BUILD_MONITOR_ENTER (enter, expr_decl);
15187 BUILD_MONITOR_EXIT (exit, expr_decl);
15188 CAN_COMPLETE_NORMALLY (enter) = 1;
15189 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
15190 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15191 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
15192 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15193 build (COMPOUND_EXPR, NULL_TREE,
15194 build (WITH_CLEANUP_EXPR, NULL_TREE,
15195 build (COMPOUND_EXPR, NULL_TREE,
96847892 15196 assignment, enter),
5a005d9e
PB
15197 NULL_TREE, exit),
15198 block));
15199 node = build_expr_block (node, expr_decl);
15200
15201 return java_complete_tree (node);
b9f7e36c
APB
15202}
15203
15204/* 14.16 The throw Statement */
15205
15206static tree
15207patch_throw_statement (node, wfl_op1)
15208 tree node, wfl_op1;
15209{
15210 tree expr = TREE_OPERAND (node, 0);
15211 tree type = TREE_TYPE (expr);
15212 int unchecked_ok = 0, tryblock_throws_ok = 0;
15213
15214 /* Thrown expression must be assignable to java.lang.Throwable */
15215 if (!try_reference_assignconv (throwable_type_node, expr))
15216 {
15217 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15218 parse_error_context (wfl_operator,
15219 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 15220 lang_printable_name (type, 0));
b9f7e36c
APB
15221 /* If the thrown expression was a reference, we further the
15222 compile-time check. */
15223 if (!JREFERENCE_TYPE_P (type))
15224 return error_mark_node;
15225 }
15226
15227 /* At least one of the following must be true */
15228
15229 /* The type of the throw expression is a not checked exception,
15230 i.e. is a unchecked expression. */
c877974e 15231 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 15232
c2952b01
APB
15233 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15234 /* An instance can't throw a checked excetion unless that exception
15235 is explicitely declared in the `throws' clause of each
15236 constructor. This doesn't apply to anonymous classes, since they
15237 don't have declared constructors. */
15238 if (!unchecked_ok
15239 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15240 {
15241 tree current;
15242 for (current = TYPE_METHODS (current_class); current;
15243 current = TREE_CHAIN (current))
15244 if (DECL_CONSTRUCTOR_P (current)
15245 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15246 {
15247 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)",
15248 lang_printable_name (TREE_TYPE (expr), 0));
15249 return error_mark_node;
15250 }
15251 }
15252
b9f7e36c
APB
15253 /* Throw is contained in a try statement and at least one catch
15254 clause can receive the thrown expression or the current method is
15255 declared to throw such an exception. Or, the throw statement is
15256 contained in a method or constructor declaration and the type of
15257 the Expression is assignable to at least one type listed in the
15258 throws clause the declaration. */
b9f7e36c 15259 if (!unchecked_ok)
f099f336 15260 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
15261 if (!(unchecked_ok || tryblock_throws_ok))
15262 {
15263 /* If there is a surrounding try block that has no matching
15264 clatch clause, report it first. A surrounding try block exits
15265 only if there is something after the list of checked
15266 exception thrown by the current function (if any). */
15267 if (IN_TRY_BLOCK_P ())
781b0558 15268 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 15269 lang_printable_name (type, 0));
b9f7e36c
APB
15270 /* If we have no surrounding try statement and the method doesn't have
15271 any throws, report it now. FIXME */
f099f336
APB
15272
15273 /* We report that the exception can't be throw from a try block
15274 in all circumstances but when the `throw' is inside a static
15275 block. */
b9f7e36c
APB
15276 else if (!EXCEPTIONS_P (currently_caught_type_list)
15277 && !tryblock_throws_ok)
f099f336 15278 {
c2952b01 15279 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
15280 parse_error_context (wfl_operator,
15281 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
15282 lang_printable_name (type, 0));
15283 else
781b0558
KG
15284 parse_error_context (wfl_operator,
15285 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
15286 lang_printable_name (type, 0));
15287 }
b9f7e36c
APB
15288 /* Otherwise, the current method doesn't have the appropriate
15289 throws declaration */
15290 else
781b0558 15291 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 15292 lang_printable_name (type, 0));
b9f7e36c
APB
15293 return error_mark_node;
15294 }
15295
ce6e9147 15296 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 15297 BUILD_THROW (node, expr);
ce6e9147
APB
15298
15299 /* If doing xrefs, keep the location where the `throw' was seen. */
15300 if (flag_emit_xref)
15301 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
15302 return node;
15303}
15304
15305/* Check that exception said to be thrown by method DECL can be
15306 effectively caught from where DECL is invoked. */
15307
15308static void
15309check_thrown_exceptions (location, decl)
15310 int location;
15311 tree decl;
15312{
15313 tree throws;
15314 /* For all the unchecked exceptions thrown by DECL */
15315 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15316 throws = TREE_CHAIN (throws))
0a2138e2 15317 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 15318 {
3e78f871
PB
15319#if 1
15320 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15321 if (DECL_NAME (decl) == get_identifier ("clone"))
15322 continue;
15323#endif
b9f7e36c 15324 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 15325 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
15326 parse_error_context
15327 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15328 lang_printable_name (TREE_VALUE (throws), 0));
15329 else
15330 {
15331 parse_error_context
781b0558 15332 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 15333 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 15334 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
15335 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15336 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15337 }
b9f7e36c
APB
15338 }
15339}
15340
c877974e 15341/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
15342 try-catch blocks, OR is listed in the `throws' clause of the
15343 current method. */
15344
15345static int
0a2138e2 15346check_thrown_exceptions_do (exception)
b9f7e36c
APB
15347 tree exception;
15348{
15349 tree list = currently_caught_type_list;
c877974e 15350 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
15351 /* First, all the nested try-catch-finally at that stage. The
15352 last element contains `throws' clause exceptions, if any. */
c877974e
APB
15353 if (IS_UNCHECKED_EXCEPTION_P (exception))
15354 return 1;
b9f7e36c
APB
15355 while (list)
15356 {
15357 tree caught;
15358 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15359 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15360 return 1;
15361 list = TREE_CHAIN (list);
15362 }
15363 return 0;
15364}
15365
15366static void
15367purge_unchecked_exceptions (mdecl)
15368 tree mdecl;
15369{
15370 tree throws = DECL_FUNCTION_THROWS (mdecl);
15371 tree new = NULL_TREE;
15372
15373 while (throws)
15374 {
15375 tree next = TREE_CHAIN (throws);
c877974e 15376 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
15377 {
15378 TREE_CHAIN (throws) = new;
15379 new = throws;
15380 }
15381 throws = next;
15382 }
15383 /* List is inverted here, but it doesn't matter */
15384 DECL_FUNCTION_THROWS (mdecl) = new;
15385}
22eed1e6
APB
15386
15387/* 15.24 Conditional Operator ?: */
15388
15389static tree
15390patch_conditional_expr (node, wfl_cond, wfl_op1)
15391 tree node, wfl_cond, wfl_op1;
15392{
15393 tree cond = TREE_OPERAND (node, 0);
15394 tree op1 = TREE_OPERAND (node, 1);
15395 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 15396 tree resulting_type = NULL_TREE;
ac825856 15397 tree t1, t2, patched;
22eed1e6
APB
15398 int error_found = 0;
15399
ac825856
APB
15400 /* Operands of ?: might be StringBuffers crafted as a result of a
15401 string concatenation. Obtain a descent operand here. */
15402 if ((patched = patch_string (op1)))
15403 TREE_OPERAND (node, 1) = op1 = patched;
15404 if ((patched = patch_string (op2)))
15405 TREE_OPERAND (node, 2) = op2 = patched;
15406
15407 t1 = TREE_TYPE (op1);
15408 t2 = TREE_TYPE (op2);
15409
22eed1e6
APB
15410 /* The first expression must be a boolean */
15411 if (TREE_TYPE (cond) != boolean_type_node)
15412 {
15413 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
15414 parse_error_context (wfl_operator,
15415 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
15416 lang_printable_name (TREE_TYPE (cond), 0));
15417 error_found = 1;
15418 }
15419
15420 /* Second and third can be numeric, boolean (i.e. primitive),
15421 references or null. Anything else results in an error */
15422 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15423 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15424 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15425 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15426 error_found = 1;
15427
15428 /* Determine the type of the conditional expression. Same types are
15429 easy to deal with */
15430 else if (t1 == t2)
15431 resulting_type = t1;
15432
15433 /* There are different rules for numeric types */
15434 else if (JNUMERIC_TYPE_P (t1))
15435 {
15436 /* if byte/short found, the resulting type is short */
15437 if ((t1 == byte_type_node && t2 == short_type_node)
15438 || (t1 == short_type_node && t2 == byte_type_node))
15439 resulting_type = short_type_node;
15440
15441 /* If t1 is a constant int and t2 is of type byte, short or char
15442 and t1's value fits in t2, then the resulting type is t2 */
15443 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15444 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15445 resulting_type = t2;
15446
15447 /* If t2 is a constant int and t1 is of type byte, short or char
15448 and t2's value fits in t1, then the resulting type is t1 */
15449 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15450 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15451 resulting_type = t1;
15452
15453 /* Otherwise, binary numeric promotion is applied and the
15454 resulting type is the promoted type of operand 1 and 2 */
15455 else
93024893 15456 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15457 &TREE_OPERAND (node, 1),
15458 &TREE_OPERAND (node, 2));
15459 }
15460
15461 /* Cases of a reference and a null type */
15462 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15463 resulting_type = t1;
15464
15465 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15466 resulting_type = t2;
15467
15468 /* Last case: different reference types. If a type can be converted
15469 into the other one by assignment conversion, the latter
15470 determines the type of the expression */
15471 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15472 resulting_type = promote_type (t1);
15473
15474 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15475 resulting_type = promote_type (t2);
15476
15477 /* If we don't have any resulting type, we're in trouble */
15478 if (!resulting_type)
15479 {
c2e3db92 15480 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15481 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15482 parse_error_context (wfl_operator,
15483 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15484 t, lang_printable_name (t2, 0));
22eed1e6
APB
15485 free (t);
15486 error_found = 1;
15487 }
15488
15489 if (error_found)
15490 {
15491 TREE_TYPE (node) = error_mark_node;
15492 return error_mark_node;
15493 }
15494
15495 TREE_TYPE (node) = resulting_type;
15496 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15497 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15498 return node;
15499}
ac825856 15500
5b09b33e
PB
15501/* Try to constant fold NODE.
15502 If NODE is not a constant expression, return NULL_EXPR.
15503 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15504
15505static tree
15506fold_constant_for_init (node, context)
15507 tree node;
15508 tree context;
15509{
15510 tree op0, op1, val;
15511 enum tree_code code = TREE_CODE (node);
15512
ee97d354 15513 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15514 return node;
93024893 15515
5b09b33e
PB
15516 switch (code)
15517 {
5b09b33e
PB
15518 case PLUS_EXPR:
15519 case MINUS_EXPR:
bc3ca41b
PB
15520 case MULT_EXPR:
15521 case TRUNC_MOD_EXPR:
15522 case RDIV_EXPR:
5b09b33e
PB
15523 case LSHIFT_EXPR:
15524 case RSHIFT_EXPR:
15525 case URSHIFT_EXPR:
15526 case BIT_AND_EXPR:
15527 case BIT_XOR_EXPR:
15528 case BIT_IOR_EXPR:
5b09b33e
PB
15529 case TRUTH_ANDIF_EXPR:
15530 case TRUTH_ORIF_EXPR:
15531 case EQ_EXPR:
15532 case NE_EXPR:
15533 case GT_EXPR:
15534 case GE_EXPR:
15535 case LT_EXPR:
15536 case LE_EXPR:
15537 op0 = TREE_OPERAND (node, 0);
15538 op1 = TREE_OPERAND (node, 1);
15539 val = fold_constant_for_init (op0, context);
15540 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15541 return NULL_TREE;
15542 TREE_OPERAND (node, 0) = val;
15543 val = fold_constant_for_init (op1, context);
15544 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15545 return NULL_TREE;
15546 TREE_OPERAND (node, 1) = val;
15547 return patch_binop (node, op0, op1);
15548
15549 case UNARY_PLUS_EXPR:
15550 case NEGATE_EXPR:
15551 case TRUTH_NOT_EXPR:
15552 case BIT_NOT_EXPR:
15553 case CONVERT_EXPR:
15554 op0 = TREE_OPERAND (node, 0);
15555 val = fold_constant_for_init (op0, context);
15556 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15557 return NULL_TREE;
15558 TREE_OPERAND (node, 0) = val;
5a005d9e 15559 return patch_unaryop (node, op0);
5b09b33e
PB
15560 break;
15561
15562 case COND_EXPR:
15563 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15564 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15565 return NULL_TREE;
15566 TREE_OPERAND (node, 0) = val;
15567 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15568 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15569 return NULL_TREE;
15570 TREE_OPERAND (node, 1) = val;
15571 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15572 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15573 return NULL_TREE;
15574 TREE_OPERAND (node, 2) = val;
15575 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15576 : TREE_OPERAND (node, 2);
15577
15578 case VAR_DECL:
8576f094
APB
15579 case FIELD_DECL:
15580 if (! FIELD_FINAL (node)
5b09b33e
PB
15581 || DECL_INITIAL (node) == NULL_TREE)
15582 return NULL_TREE;
15583 val = DECL_INITIAL (node);
15584 /* Guard against infinite recursion. */
15585 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15586 val = fold_constant_for_init (val, node);
5b09b33e 15587 DECL_INITIAL (node) = val;
c7303e41
APB
15588 if (!val && CLASS_FINAL_VARIABLE_P (node))
15589 DECL_FIELD_FINAL_IUD (node) = 0;
5b09b33e
PB
15590 return val;
15591
15592 case EXPR_WITH_FILE_LOCATION:
15593 /* Compare java_complete_tree and resolve_expression_name. */
15594 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15595 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15596 {
15597 tree name = EXPR_WFL_NODE (node);
15598 tree decl;
15599 if (PRIMARY_P (node))
15600 return NULL_TREE;
15601 else if (! QUALIFIED_P (name))
15602 {
15603 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15604 if (decl == NULL_TREE
15605 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15606 return NULL_TREE;
15607 return fold_constant_for_init (decl, decl);
15608 }
15609 else
15610 {
5b09b33e
PB
15611 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15612 qualify_ambiguous_name (node);
15613 if (resolve_field_access (node, &decl, NULL)
15614 && decl != NULL_TREE)
15615 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15616 return NULL_TREE;
15617 }
15618 }
15619 else
15620 {
15621 op0 = TREE_OPERAND (node, 0);
15622 val = fold_constant_for_init (op0, context);
15623 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15624 return NULL_TREE;
15625 TREE_OPERAND (node, 0) = val;
15626 return val;
15627 }
15628
bc3ca41b
PB
15629#ifdef USE_COMPONENT_REF
15630 case IDENTIFIER:
15631 case COMPONENT_REF:
15632 ?;
15633#endif
15634
5b09b33e
PB
15635 default:
15636 return NULL_TREE;
15637 }
15638}
bc3ca41b
PB
15639
15640#ifdef USE_COMPONENT_REF
15641/* Context is 'T' for TypeName, 'P' for PackageName,
15642 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15643
15644tree
15645resolve_simple_name (name, context)
15646 tree name;
15647 int context;
15648{
15649}
15650
15651tree
15652resolve_qualified_name (name, context)
15653 tree name;
15654 int context;
15655{
15656}
15657#endif
f15b9af9
MM
15658
15659/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15660
15661static void
15662mark_parser_ctxt (p)
15663 void *p;
15664{
15665 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15666 int i;
15667
15668 if (!pc)
15669 return;
15670
15671#ifndef JC1_LITE
15672 for (i = 0; i < 11; ++i)
15673 ggc_mark_tree (pc->modifier_ctx[i]);
15674 ggc_mark_tree (pc->class_type);
15675 ggc_mark_tree (pc->function_decl);
15676 ggc_mark_tree (pc->package);
f15b9af9
MM
15677 ggc_mark_tree (pc->class_list);
15678 ggc_mark_tree (pc->current_parsed_class);
15679 ggc_mark_tree (pc->current_parsed_class_un);
15680 ggc_mark_tree (pc->non_static_initialized);
15681 ggc_mark_tree (pc->static_initialized);
15682 ggc_mark_tree (pc->instance_initializers);
15683 ggc_mark_tree (pc->import_list);
15684 ggc_mark_tree (pc->import_demand_list);
15685 ggc_mark_tree (pc->current_loop);
15686 ggc_mark_tree (pc->current_labeled_block);
15687#endif /* JC1_LITE */
15688
15689 if (pc->next)
15690 mark_parser_ctxt (&pc->next);
15691}
fea2d5da
PB
15692
15693void
15694init_src_parse ()
15695{
15696 /* Register roots with the garbage collector. */
15697 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
15698}
This page took 4.398264 seconds and 5 git commands to generate.