]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
inclhack.def (broken_cabs): Generalize regex.
[gcc.git] / gcc / java / parse.y
CommitLineData
e04a16fb
AG
1/* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
df32d2ce 3 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
e04a16fb
AG
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23Java and all Java-based marks are trademarks or registered trademarks
24of Sun Microsystems, Inc. in the United States and other countries.
25The Free Software Foundation is independent of Sun Microsystems, Inc. */
26
27/* This file parses java source code and issues a tree node image
28suitable for code generation (byte code and targeted CPU assembly
29language).
30
31The grammar conforms to the Java grammar described in "The Java(TM)
32Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
331996, ISBN 0-201-63451-1"
34
35The following modifications were brought to the original grammar:
36
37method_body: added the rule '| block SC_TK'
e04a16fb
AG
38static_initializer: added the rule 'static block SC_TK'.
39
40Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43statement_nsi: 'nsi' should be read no_short_if.
44
45Some rules have been modified to support JDK1.1 inner classes
46definitions and other extensions. */
47
48%{
e04a16fb 49#include "config.h"
36635152
GS
50#include "system.h"
51#include <dirent.h>
e04a16fb
AG
52#include "tree.h"
53#include "rtl.h"
54#include "obstack.h"
0a2138e2 55#include "toplev.h"
e04a16fb
AG
56#include "flags.h"
57#include "java-tree.h"
58#include "jcf.h"
59#include "lex.h"
60#include "parse.h"
61#include "zipfile.h"
5e942c50 62#include "convert.h"
63a212ed 63#include "buffer.h"
f099f336 64#include "xref.h"
b384405b 65#include "function.h"
138657ec 66#include "except.h"
0ae70c6a 67#include "defaults.h"
19e223db 68#include "ggc.h"
e04a16fb 69
c2952b01
APB
70#ifndef DIR_SEPARATOR
71#define DIR_SEPARATOR '/'
72#endif
73
82371d41 74/* Local function prototypes */
df32d2ce
KG
75static char *java_accstring_lookup PARAMS ((int));
76static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
78static tree create_class PARAMS ((int, tree, tree, tree));
79static tree create_interface PARAMS ((int, tree, tree));
c2952b01 80static void end_class_declaration PARAMS ((int));
df32d2ce
KG
81static tree find_field PARAMS ((tree, tree));
82static tree lookup_field_wrapper PARAMS ((tree, tree));
83static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84static void register_fields PARAMS ((int, tree, tree));
98a52c2c 85static tree parser_qualified_classname PARAMS ((tree));
df32d2ce
KG
86static int parser_check_super PARAMS ((tree, tree, tree));
87static int parser_check_super_interface PARAMS ((tree, tree, tree));
88static void check_modifiers_consistency PARAMS ((int));
89static tree lookup_cl PARAMS ((tree));
90static tree lookup_java_method2 PARAMS ((tree, tree, int));
91static tree method_header PARAMS ((int, tree, tree, tree));
92static void fix_method_argument_names PARAMS ((tree ,tree));
93static tree method_declarator PARAMS ((tree, tree));
94static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 95 ATTRIBUTE_PRINTF_2;
df32d2ce
KG
96static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
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));
cf1748bf 104static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 105static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 106static void register_package PARAMS ((tree));
df32d2ce
KG
107static tree resolve_package PARAMS ((tree, tree *));
108static tree lookup_package_type PARAMS ((const char *, int));
109static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 110static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
111static void declare_local_variables PARAMS ((int, tree, tree));
112static void source_start_java_method PARAMS ((tree));
113static void source_end_java_method PARAMS ((void));
114static void expand_start_java_method PARAMS ((tree));
115static tree find_name_in_single_imports PARAMS ((tree));
116static void check_abstract_method_header PARAMS ((tree));
117static tree lookup_java_interface_method2 PARAMS ((tree, tree));
118static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 119static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 120static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 121 tree, tree, tree));
df32d2ce 122static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 123 int *, tree *));
df32d2ce
KG
124static int breakdown_qualified PARAMS ((tree *, tree *, tree));
125static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 126static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
127static tree resolve_no_layout PARAMS ((tree, tree));
128static int invocation_mode PARAMS ((tree, int));
129static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 130 tree, tree));
df32d2ce 131static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 132 tree *, tree *));
df32d2ce
KG
133static tree find_most_specific_methods_list PARAMS ((tree));
134static int argument_types_convertible PARAMS ((tree, tree));
135static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 136static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
137static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139static tree obtain_incomplete_type PARAMS ((tree));
140static tree java_complete_lhs PARAMS ((tree));
141static tree java_complete_tree PARAMS ((tree));
c2952b01 142static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
92d83515 143static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
144static void java_complete_expand_method PARAMS ((tree));
145static int unresolved_type_p PARAMS ((tree, tree *));
146static void create_jdep_list PARAMS ((struct parser_ctxt *));
147static tree build_expr_block PARAMS ((tree, tree));
148static tree enter_block PARAMS ((void));
149static tree enter_a_block PARAMS ((tree));
150static tree exit_block PARAMS ((void));
151static tree lookup_name_in_blocks PARAMS ((tree));
152static void maybe_absorb_scoping_blocks PARAMS ((void));
153static tree build_method_invocation PARAMS ((tree, tree));
154static tree build_new_invocation PARAMS ((tree, tree));
155static tree build_assignment PARAMS ((int, int, tree, tree));
156static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
157static int check_final_assignment PARAMS ((tree ,tree));
158static tree patch_assignment PARAMS ((tree, tree, tree ));
159static tree patch_binop PARAMS ((tree, tree, tree));
160static tree build_unaryop PARAMS ((int, int, tree));
161static tree build_incdec PARAMS ((int, int, tree, int));
162static tree patch_unaryop PARAMS ((tree, tree));
163static tree build_cast PARAMS ((int, tree, tree));
164static tree build_null_of_type PARAMS ((tree));
165static tree patch_cast PARAMS ((tree, tree));
166static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
167static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
168static int valid_cast_to_p PARAMS ((tree, tree));
169static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
170static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
171static tree try_reference_assignconv PARAMS ((tree, tree));
172static tree build_unresolved_array_type PARAMS ((tree));
173static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
174static tree build_array_ref PARAMS ((int, tree, tree));
175static tree patch_array_ref PARAMS ((tree));
176static tree make_qualified_name PARAMS ((tree, tree, int));
177static tree merge_qualified_name PARAMS ((tree, tree));
178static tree make_qualified_primary PARAMS ((tree, tree, int));
179static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 180 tree *, tree *));
df32d2ce 181static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
182static tree resolve_field_access PARAMS ((tree, tree *, tree *));
183static tree build_newarray_node PARAMS ((tree, tree, int));
184static tree patch_newarray PARAMS ((tree));
185static tree resolve_type_during_patch PARAMS ((tree));
186static tree build_this PARAMS ((int));
9a7ab4b3 187static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
188static tree build_return PARAMS ((int, tree));
189static tree patch_return PARAMS ((tree));
190static tree maybe_access_field PARAMS ((tree, tree, tree));
191static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
192static int check_for_static_method_reference PARAMS ((tree, tree, tree,
193 tree, tree));
df32d2ce
KG
194static int not_accessible_p PARAMS ((tree, tree, int));
195static void check_deprecation PARAMS ((tree, tree));
196static int class_in_current_package PARAMS ((tree));
197static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
198static tree patch_if_else_statement PARAMS ((tree));
199static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
200static tree add_stmt_to_block PARAMS ((tree, tree, tree));
201static tree patch_exit_expr PARAMS ((tree));
202static tree build_labeled_block PARAMS ((int, tree));
203static tree finish_labeled_statement PARAMS ((tree, tree));
204static tree build_bc_statement PARAMS ((int, int, tree));
205static tree patch_bc_statement PARAMS ((tree));
206static tree patch_loop_statement PARAMS ((tree));
207static tree build_new_loop PARAMS ((tree));
208static tree build_loop_body PARAMS ((int, tree, int));
209static tree finish_loop_body PARAMS ((int, tree, tree, int));
210static tree build_debugable_stmt PARAMS ((int, tree));
211static tree finish_for_loop PARAMS ((int, tree, tree, tree));
212static tree patch_switch_statement PARAMS ((tree));
213static tree string_constant_concatenation PARAMS ((tree, tree));
214static tree build_string_concatenation PARAMS ((tree, tree));
215static tree patch_string_cst PARAMS ((tree));
216static tree patch_string PARAMS ((tree));
217static tree build_try_statement PARAMS ((int, tree, tree));
218static tree build_try_finally_statement PARAMS ((int, tree, tree));
219static tree patch_try_statement PARAMS ((tree));
220static tree patch_synchronized_statement PARAMS ((tree, tree));
221static tree patch_throw_statement PARAMS ((tree, tree));
222static void check_thrown_exceptions PARAMS ((int, tree));
223static int check_thrown_exceptions_do PARAMS ((tree));
224static void purge_unchecked_exceptions PARAMS ((tree));
225static void check_throws_clauses PARAMS ((tree, tree, tree));
226static void finish_method_declaration PARAMS ((tree));
227static tree build_super_invocation PARAMS ((tree));
228static int verify_constructor_circularity PARAMS ((tree, tree));
229static char *constructor_circularity_msg PARAMS ((tree, tree));
230static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 231 int, int));
df32d2ce
KG
232static const char *get_printable_method_name PARAMS ((tree));
233static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
234static tree generate_finit PARAMS ((tree));
235static void add_instance_initializer PARAMS ((tree));
df32d2ce 236static void fix_constructors PARAMS ((tree));
c2952b01
APB
237static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
238 tree, int *));
239static void craft_constructor PARAMS ((tree, tree));
240static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
241static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
242static void start_artificial_method_body PARAMS ((tree));
243static void end_artificial_method_body PARAMS ((tree));
244static int check_method_redefinition PARAMS ((tree, tree));
245static int reset_method_name PARAMS ((tree));
165f37bc 246static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
247static void java_check_regular_methods PARAMS ((tree));
248static void java_check_abstract_methods PARAMS ((tree));
249static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
250static void unreachable_stmt_error PARAMS ((tree));
251static tree find_expr_with_wfl PARAMS ((tree));
252static void missing_return_error PARAMS ((tree));
253static tree build_new_array_init PARAMS ((int, tree));
254static tree patch_new_array_init PARAMS ((tree, tree));
255static tree maybe_build_array_element_wfl PARAMS ((tree));
256static int array_constructor_check_entry PARAMS ((tree, tree));
257static const char *purify_type_name PARAMS ((const char *));
258static tree fold_constant_for_init PARAMS ((tree, tree));
259static tree strip_out_static_field_access_decl PARAMS ((tree));
260static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
261static void static_ref_err PARAMS ((tree, tree, tree));
262static void parser_add_interface PARAMS ((tree, tree, tree));
263static void add_superinterfaces PARAMS ((tree, tree));
264static tree jdep_resolve_class PARAMS ((jdep *));
265static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
266static void java_complete_expand_classes PARAMS ((void));
267static void java_complete_expand_class PARAMS ((tree));
268static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
269static tree cut_identifier_in_qualified PARAMS ((tree));
270static tree java_stabilize_reference PARAMS ((tree));
271static tree do_unary_numeric_promotion PARAMS ((tree));
272static char * operator_string PARAMS ((tree));
273static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
274static tree merge_string_cste PARAMS ((tree, tree, int));
275static tree java_refold PARAMS ((tree));
276static int java_decl_equiv PARAMS ((tree, tree));
277static int binop_compound_p PARAMS ((enum tree_code));
278static tree search_loop PARAMS ((tree));
279static int labeled_block_contains_loop_p PARAMS ((tree, tree));
1175b9b4 280static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
281static void java_check_abstract_method_definitions PARAMS ((tree));
282static void java_debug_context_do PARAMS ((int));
c2952b01
APB
283static void java_parser_context_push_initialized_field PARAMS ((void));
284static void java_parser_context_pop_initialized_field PARAMS ((void));
285static tree reorder_static_initialized PARAMS ((tree));
286static void java_parser_context_suspend PARAMS ((void));
287static void java_parser_context_resume PARAMS ((void));
288
289/* JDK 1.1 work. FIXME */
290
291static tree maybe_make_nested_class_name PARAMS ((tree));
292static void make_nested_class_name PARAMS ((tree));
293static void set_nested_class_simple_name_value PARAMS ((tree, int));
294static void link_nested_class_to_enclosing PARAMS ((void));
295static tree find_as_inner_class PARAMS ((tree, tree, tree));
296static tree find_as_inner_class_do PARAMS ((tree, tree));
297static int check_inner_class_redefinition PARAMS ((tree, tree));
298
299static tree build_thisn_assign PARAMS ((void));
300static tree build_current_thisn PARAMS ((tree));
301static tree build_access_to_thisn PARAMS ((tree, tree, int));
302static tree maybe_build_thisn_access_method PARAMS ((tree));
303
304static tree build_outer_field_access PARAMS ((tree, tree));
305static tree build_outer_field_access_methods PARAMS ((tree));
306static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
307 tree, tree));
308static tree build_outer_method_access_method PARAMS ((tree));
309static tree build_new_access_id PARAMS ((void));
310static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
311 tree, tree));
312
313static int outer_field_access_p PARAMS ((tree, tree));
314static int outer_field_expanded_access_p PARAMS ((tree, tree *,
315 tree *, tree *));
316static tree outer_field_access_fix PARAMS ((tree, tree, tree));
317static tree build_incomplete_class_ref PARAMS ((int, tree));
318static tree patch_incomplete_class_ref PARAMS ((tree));
319static tree create_anonymous_class PARAMS ((int, tree));
320static void patch_anonymous_class PARAMS ((tree, tree, tree));
321static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 322
165f37bc
APB
323static tree build_dot_class_method PARAMS ((tree));
324static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 325static void create_new_parser_context PARAMS ((int));
165f37bc 326
e04a16fb
AG
327/* Number of error found so far. */
328int java_error_count;
329/* Number of warning found so far. */
330int java_warning_count;
ce6e9147
APB
331/* Tell when not to fold, when doing xrefs */
332int do_not_fold;
c2952b01
APB
333/* Cyclic inheritance report, as it can be set by layout_class */
334char *cyclic_inheritance_report;
335
336/* Tell when we're within an instance initializer */
337static int in_instance_initializer;
e04a16fb
AG
338
339/* The current parser context */
d4370213 340struct parser_ctxt *ctxp;
e04a16fb 341
d4370213 342/* List of things that were analyzed for which code will be generated */
b351b287
APB
343static struct parser_ctxt *ctxp_for_generation = NULL;
344
e04a16fb
AG
345/* binop_lookup maps token to tree_code. It is used where binary
346 operations are involved and required by the parser. RDIV_EXPR
347 covers both integral/floating point division. The code is changed
348 once the type of both operator is worked out. */
349
350static enum tree_code binop_lookup[19] =
351 {
352 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
353 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
354 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
355 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
356 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
357 };
358#define BINOP_LOOKUP(VALUE) \
6e2aa220 359 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 360
5cbdba64
APB
361/* This is the end index for binary operators that can also be used
362 in compound assignements. */
363#define BINOP_COMPOUND_CANDIDATES 11
364
e04a16fb 365/* The "$L" identifier we use to create labels. */
b67d701b
PB
366static tree label_id = NULL_TREE;
367
368/* The "StringBuffer" identifier used for the String `+' operator. */
369static tree wfl_string_buffer = NULL_TREE;
370
371/* The "append" identifier used for String `+' operator. */
372static tree wfl_append = NULL_TREE;
373
374/* The "toString" identifier used for String `+' operator. */
375static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
376
377/* The "java.lang" import qualified name. */
378static tree java_lang_id = NULL_TREE;
09ed0f70 379
c2952b01
APB
380/* The generated `inst$' identifier used for generated enclosing
381 instance/field access functions. */
382static tree inst_id = NULL_TREE;
383
09ed0f70
APB
384/* The "java.lang.Cloneable" qualified name. */
385static tree java_lang_cloneable = NULL_TREE;
f099f336 386
ee17a290
TT
387/* The "java.io.Serializable" qualified name. */
388static tree java_io_serializable = NULL_TREE;
389
f099f336
APB
390/* Context and flag for static blocks */
391static tree current_static_block = NULL_TREE;
392
c2952b01
APB
393/* The generated `write_parm_value$' identifier. */
394static tree wpv_id;
395
ee07f4f4
APB
396/* The list of all packages we've seen so far */
397static tree package_list = NULL_TREE;
2884c41e 398
19e223db
MM
399/* Hold THIS for the scope of the current public method decl. */
400static tree current_this;
401
402/* Hold a list of catch clauses list. The first element of this list is
403 the list of the catch clauses of the currently analysed try block. */
404static tree currently_caught_type_list;
405
2884c41e
KG
406/* Check modifiers. If one doesn't fit, retrieve it in its declaration
407 line and point it out. */
408/* Should point out the one that don't fit. ASCII/unicode, going
409 backward. FIXME */
410
411#define check_modifiers(__message, __value, __mask) do { \
412 if ((__value) & ~(__mask)) \
413 { \
414 int i, remainder = (__value) & ~(__mask); \
415 for (i = 0; i <= 10; i++) \
416 if ((1 << i) & remainder) \
417 parse_error_context (ctxp->modifier_ctx [i], (__message), \
418 java_accstring_lookup (1 << i)); \
419 } \
420} while (0)
ee07f4f4 421
e04a16fb
AG
422%}
423
424%union {
425 tree node;
426 int sub_token;
427 struct {
428 int token;
429 int location;
430 } operator;
431 int value;
432}
433
9ee9b555
KG
434%{
435#include "lex.c"
436%}
437
e04a16fb
AG
438%pure_parser
439
440/* Things defined here have to match the order of what's in the
441 binop_lookup table. */
442
443%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
444%token LS_TK SRS_TK ZRS_TK
445%token AND_TK XOR_TK OR_TK
446%token BOOL_AND_TK BOOL_OR_TK
447%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
448
449/* This maps to the same binop_lookup entry than the token above */
450
451%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
452%token REM_ASSIGN_TK
453%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
454%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
455
456
457/* Modifier TOKEN have to be kept in this order. Don't scramble it */
458
459%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
460%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
461%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
462%token PAD_TK ABSTRACT_TK MODIFIER_TK
463
464/* Keep those two in order, too */
465%token DECR_TK INCR_TK
466
467/* From now one, things can be in any order */
468
469%token DEFAULT_TK IF_TK THROW_TK
470%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
471%token THROWS_TK BREAK_TK IMPORT_TK
472%token ELSE_TK INSTANCEOF_TK RETURN_TK
473%token VOID_TK CATCH_TK INTERFACE_TK
474%token CASE_TK EXTENDS_TK FINALLY_TK
475%token SUPER_TK WHILE_TK CLASS_TK
476%token SWITCH_TK CONST_TK TRY_TK
477%token FOR_TK NEW_TK CONTINUE_TK
478%token GOTO_TK PACKAGE_TK THIS_TK
479
480%token BYTE_TK SHORT_TK INT_TK LONG_TK
481%token CHAR_TK INTEGRAL_TK
482
483%token FLOAT_TK DOUBLE_TK FP_TK
484
485%token ID_TK
486
487%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
488
489%token ASSIGN_ANY_TK ASSIGN_TK
490%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
491
492%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
493%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
494
c2952b01 495%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
496
497%type <node> super ID_TK identifier
498%type <node> name simple_name qualified_name
c2952b01 499%type <node> type_declaration compilation_unit
e04a16fb
AG
500 field_declaration method_declaration extends_interfaces
501 interfaces interface_type_list
c2952b01 502 class_member_declaration
e04a16fb
AG
503 import_declarations package_declaration
504 type_declarations interface_body
505 interface_member_declaration constant_declaration
506 interface_member_declarations interface_type
507 abstract_method_declaration interface_type_list
508%type <node> class_body_declaration class_member_declaration
509 static_initializer constructor_declaration block
22eed1e6 510%type <node> class_body_declarations constructor_header
e04a16fb
AG
511%type <node> class_or_interface_type class_type class_type_list
512 constructor_declarator explicit_constructor_invocation
b9f7e36c 513%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
514
515%type <node> variable_declarator_id variable_declarator
516 variable_declarators variable_initializer
22eed1e6 517 variable_initializers constructor_body
ac825856 518 array_initializer
e04a16fb 519
2e5eb5c5 520%type <node> class_body block_end constructor_block_end
e04a16fb
AG
521%type <node> statement statement_without_trailing_substatement
522 labeled_statement if_then_statement label_decl
523 if_then_else_statement while_statement for_statement
524 statement_nsi labeled_statement_nsi do_statement
525 if_then_else_statement_nsi while_statement_nsi
526 for_statement_nsi statement_expression_list for_init
527 for_update statement_expression expression_statement
528 primary_no_new_array expression primary
529 array_creation_expression array_type
530 class_instance_creation_expression field_access
531 method_invocation array_access something_dot_new
532 argument_list postfix_expression while_expression
533 post_increment_expression post_decrement_expression
534 unary_expression_not_plus_minus unary_expression
535 pre_increment_expression pre_decrement_expression
536 unary_expression_not_plus_minus cast_expression
537 multiplicative_expression additive_expression
538 shift_expression relational_expression
539 equality_expression and_expression
540 exclusive_or_expression inclusive_or_expression
541 conditional_and_expression conditional_or_expression
542 conditional_expression assignment_expression
543 left_hand_side assignment for_header for_begin
544 constant_expression do_statement_begin empty_statement
b67d701b 545 switch_statement synchronized_statement throw_statement
f8976021 546 try_statement switch_expression switch_block
15fdcfe9 547 catches catch_clause catch_clause_parameter finally
c2952b01 548 anonymous_class_creation
e04a16fb
AG
549%type <node> return_statement break_statement continue_statement
550
551%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
552%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
553%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
554%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
555%type <operator> ASSIGN_ANY_TK assignment_operator
556%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
557%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
558%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 559%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 560%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
561%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
562%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 563%type <operator> NEW_TK
e04a16fb
AG
564
565%type <node> method_body
566
567%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
568 STRING_LIT_TK NULL_TK VOID_TK
569
570%type <node> IF_TK WHILE_TK FOR_TK
571
572%type <node> formal_parameter_list formal_parameter
573 method_declarator method_header
574
c2952b01 575%type <node> primitive_type reference_type type
e04a16fb
AG
576 BOOLEAN_TK INTEGRAL_TK FP_TK
577
c2952b01
APB
578/* Added or modified JDK 1.1 rule types */
579%type <node> type_literals array_type_literal
580
e04a16fb
AG
581%%
582/* 19.2 Production from 2.3: The Syntactic Grammar */
583goal:
19e223db
MM
584 {
585 /* Register static variables with the garbage
586 collector. */
587 ggc_add_tree_root (&label_id, 1);
588 ggc_add_tree_root (&wfl_string_buffer, 1);
589 ggc_add_tree_root (&wfl_append, 1);
590 ggc_add_tree_root (&wfl_to_string, 1);
591 ggc_add_tree_root (&java_lang_id, 1);
592 ggc_add_tree_root (&inst_id, 1);
593 ggc_add_tree_root (&java_lang_cloneable, 1);
594 ggc_add_tree_root (&java_io_serializable, 1);
595 ggc_add_tree_root (&current_static_block, 1);
596 ggc_add_tree_root (&wpv_id, 1);
597 ggc_add_tree_root (&package_list, 1);
598 ggc_add_tree_root (&current_this, 1);
599 ggc_add_tree_root (&currently_caught_type_list, 1);
600 }
e04a16fb
AG
601 compilation_unit
602 {}
603;
604
605/* 19.3 Productions from 3: Lexical structure */
606literal:
607 INT_LIT_TK
608| FP_LIT_TK
609| BOOL_LIT_TK
610| CHAR_LIT_TK
611| STRING_LIT_TK
612| NULL_TK
613;
614
615/* 19.4 Productions from 4: Types, Values and Variables */
616type:
617 primitive_type
618| reference_type
619;
620
621primitive_type:
622 INTEGRAL_TK
623| FP_TK
624| BOOLEAN_TK
625;
626
627reference_type:
628 class_or_interface_type
629| array_type
630;
631
632class_or_interface_type:
633 name
634;
635
636class_type:
637 class_or_interface_type /* Default rule */
638;
639
640interface_type:
641 class_or_interface_type
642;
643
644array_type:
645 primitive_type OSB_TK CSB_TK
646 {
647 $$ = build_java_array_type ($1, -1);
648 CLASS_LOADED_P ($$) = 1;
649 }
650| name OSB_TK CSB_TK
651 { $$ = build_unresolved_array_type ($1); }
652| array_type OSB_TK CSB_TK
653 { $$ = build_unresolved_array_type ($1); }
654| primitive_type OSB_TK error
655 {RULE ("']' expected"); RECOVER;}
656| array_type OSB_TK error
657 {RULE ("']' expected"); RECOVER;}
658;
659
660/* 19.5 Productions from 6: Names */
661name:
662 simple_name /* Default rule */
663| qualified_name /* Default rule */
664;
665
666simple_name:
667 identifier /* Default rule */
668;
669
670qualified_name:
671 name DOT_TK identifier
672 { $$ = make_qualified_name ($1, $3, $2.location); }
673;
674
675identifier:
676 ID_TK
677;
678
679/* 19.6: Production from 7: Packages */
680compilation_unit:
681 {$$ = NULL;}
682| package_declaration
683| import_declarations
684| type_declarations
685| package_declaration import_declarations
686| package_declaration type_declarations
687| import_declarations type_declarations
688| package_declaration import_declarations type_declarations
689;
690
691import_declarations:
692 import_declaration
693 {
694 $$ = NULL;
695 }
696| import_declarations import_declaration
697 {
698 $$ = NULL;
699 }
700;
701
702type_declarations:
703 type_declaration
704| type_declarations type_declaration
705;
706
707package_declaration:
708 PACKAGE_TK name SC_TK
ee07f4f4
APB
709 {
710 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 711 register_package (ctxp->package);
ee07f4f4 712 }
e04a16fb
AG
713| PACKAGE_TK error
714 {yyerror ("Missing name"); RECOVER;}
715| PACKAGE_TK name error
716 {yyerror ("';' expected"); RECOVER;}
717;
718
719import_declaration:
720 single_type_import_declaration
721| type_import_on_demand_declaration
722;
723
724single_type_import_declaration:
725 IMPORT_TK name SC_TK
726 {
9a7ab4b3 727 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 728 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 729 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
730 while (last != IDENTIFIER_POINTER (name))
731 {
732 if (last [0] == '.')
733 break;
734 last--;
735 }
736 last_name = get_identifier (++last);
737 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
738 {
739 tree err = find_name_in_single_imports (last_name);
740 if (err && err != name)
741 parse_error_context
742 ($2, "Ambiguous class: `%s' and `%s'",
743 IDENTIFIER_POINTER (name),
744 IDENTIFIER_POINTER (err));
5e942c50 745 else
9a7ab4b3 746 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
747 }
748 else
5e942c50 749 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
750 }
751| IMPORT_TK error
752 {yyerror ("Missing name"); RECOVER;}
753| IMPORT_TK name error
754 {yyerror ("';' expected"); RECOVER;}
755;
756
757type_import_on_demand_declaration:
758 IMPORT_TK name DOT_TK MULT_TK SC_TK
759 {
760 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
761 /* Don't import java.lang.* twice. */
762 if (name != java_lang_id)
763 {
ba179f9f 764 read_import_dir ($2);
9a7ab4b3
APB
765 ctxp->import_demand_list =
766 chainon (ctxp->import_demand_list,
767 build_tree_list ($2, NULL_TREE));
ba179f9f 768 }
e04a16fb
AG
769 }
770| IMPORT_TK name DOT_TK error
771 {yyerror ("'*' expected"); RECOVER;}
772| IMPORT_TK name DOT_TK MULT_TK error
773 {yyerror ("';' expected"); RECOVER;}
774;
775
776type_declaration:
777 class_declaration
c2952b01 778 { end_class_declaration (0); }
e04a16fb 779| interface_declaration
c2952b01 780 { end_class_declaration (0); }
5f1c312a 781| empty_statement
e04a16fb
AG
782| error
783 {
784 YYERROR_NOW;
785 yyerror ("Class or interface declaration expected");
786 }
787;
788
789/* 19.7 Shortened from the original:
790 modifiers: modifier | modifiers modifier
791 modifier: any of public... */
792modifiers:
793 MODIFIER_TK
794 {
795 $$ = (1 << $1);
796 }
797| modifiers MODIFIER_TK
798 {
799 int acc = (1 << $2);
800 if ($$ & acc)
801 parse_error_context
802 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
803 java_accstring_lookup (acc));
804 else
805 {
806 $$ |= acc;
807 }
808 }
809;
810
811/* 19.8.1 Production from $8.1: Class Declaration */
812class_declaration:
813 modifiers CLASS_TK identifier super interfaces
814 { create_class ($1, $3, $4, $5); }
815 class_body
e04a16fb
AG
816| CLASS_TK identifier super interfaces
817 { create_class (0, $2, $3, $4); }
818 class_body
e04a16fb
AG
819| modifiers CLASS_TK error
820 {yyerror ("Missing class name"); RECOVER;}
821| CLASS_TK error
822 {yyerror ("Missing class name"); RECOVER;}
823| CLASS_TK identifier error
0b4d333e
APB
824 {
825 if (!ctxp->class_err) yyerror ("'{' expected");
826 DRECOVER(class1);
827 }
e04a16fb
AG
828| modifiers CLASS_TK identifier error
829 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
830;
831
832super:
833 { $$ = NULL; }
834| EXTENDS_TK class_type
835 { $$ = $2; }
836| EXTENDS_TK class_type error
837 {yyerror ("'{' expected"); ctxp->class_err=1;}
838| EXTENDS_TK error
839 {yyerror ("Missing super class name"); ctxp->class_err=1;}
840;
841
842interfaces:
843 { $$ = NULL_TREE; }
844| IMPLEMENTS_TK interface_type_list
845 { $$ = $2; }
846| IMPLEMENTS_TK error
847 {
848 ctxp->class_err=1;
849 yyerror ("Missing interface name");
850 }
851;
852
853interface_type_list:
854 interface_type
855 {
856 ctxp->interface_number = 1;
857 $$ = build_tree_list ($1, NULL_TREE);
858 }
859| interface_type_list C_TK interface_type
860 {
861 ctxp->interface_number++;
862 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
863 }
864| interface_type_list C_TK error
865 {yyerror ("Missing interface name"); RECOVER;}
866;
867
868class_body:
869 OCB_TK CCB_TK
7f10c2e2
APB
870 {
871 /* Store the location of the `}' when doing xrefs */
872 if (flag_emit_xref)
c2952b01 873 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 874 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 875 $$ = GET_CPC ();
7f10c2e2 876 }
e04a16fb 877| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
878 {
879 /* Store the location of the `}' when doing xrefs */
880 if (flag_emit_xref)
c2952b01 881 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 882 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 883 $$ = GET_CPC ();
7f10c2e2 884 }
e04a16fb
AG
885;
886
887class_body_declarations:
888 class_body_declaration
889| class_body_declarations class_body_declaration
890;
891
892class_body_declaration:
893 class_member_declaration
894| static_initializer
895| constructor_declaration
896| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
897 {
898 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
899 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
900 }
e04a16fb
AG
901;
902
903class_member_declaration:
904 field_declaration
905| method_declaration
906| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
907 { end_class_declaration (1); }
908| interface_declaration /* Added, JDK1.1 inner interfaces */
909 { end_class_declaration (1); }
5f1c312a 910| empty_statement
e04a16fb
AG
911;
912
913/* 19.8.2 Productions from 8.3: Field Declarations */
914field_declaration:
915 type variable_declarators SC_TK
916 { register_fields (0, $1, $2); }
917| modifiers type variable_declarators SC_TK
918 {
e04a16fb
AG
919 check_modifiers
920 ("Illegal modifier `%s' for field declaration",
921 $1, FIELD_MODIFIERS);
922 check_modifiers_consistency ($1);
923 register_fields ($1, $2, $3);
924 }
925;
926
927variable_declarators:
928 /* Should we use build_decl_list () instead ? FIXME */
929 variable_declarator /* Default rule */
930| variable_declarators C_TK variable_declarator
931 { $$ = chainon ($1, $3); }
932| variable_declarators C_TK error
933 {yyerror ("Missing term"); RECOVER;}
934;
935
936variable_declarator:
937 variable_declarator_id
938 { $$ = build_tree_list ($1, NULL_TREE); }
939| variable_declarator_id ASSIGN_TK variable_initializer
940 {
941 if (java_error_count)
942 $3 = NULL_TREE;
943 $$ = build_tree_list
944 ($1, build_assignment ($2.token, $2.location, $1, $3));
945 }
946| variable_declarator_id ASSIGN_TK error
947 {
948 yyerror ("Missing variable initializer");
949 $$ = build_tree_list ($1, NULL_TREE);
950 RECOVER;
951 }
952| variable_declarator_id ASSIGN_TK variable_initializer error
953 {
954 yyerror ("';' expected");
955 $$ = build_tree_list ($1, NULL_TREE);
956 RECOVER;
957 }
958;
959
960variable_declarator_id:
961 identifier
962| variable_declarator_id OSB_TK CSB_TK
c583dd46 963 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
964| identifier error
965 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
966| variable_declarator_id OSB_TK error
967 {yyerror ("']' expected"); DRECOVER(vdi);}
968| variable_declarator_id CSB_TK error
969 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
970;
971
972variable_initializer:
973 expression
974| array_initializer
e04a16fb
AG
975;
976
977/* 19.8.3 Productions from 8.4: Method Declarations */
978method_declaration:
979 method_header
980 {
981 current_function_decl = $1;
c2952b01
APB
982 if (current_function_decl
983 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
984 source_start_java_method (current_function_decl);
985 else
986 current_function_decl = NULL_TREE;
e04a16fb
AG
987 }
988 method_body
b635eb2f 989 { finish_method_declaration ($3); }
e04a16fb
AG
990| method_header error
991 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
992;
993
994method_header:
995 type method_declarator throws
b9f7e36c 996 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 997| VOID_TK method_declarator throws
b9f7e36c 998 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 999| modifiers type method_declarator throws
b9f7e36c 1000 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1001| modifiers VOID_TK method_declarator throws
b9f7e36c 1002 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1003| type error
efa0a23f
APB
1004 {
1005 yyerror ("Invalid method declaration, method name required");
1006 RECOVER;
1007 }
e04a16fb
AG
1008| modifiers type error
1009 {RECOVER;}
1010| VOID_TK error
1011 {yyerror ("Identifier expected"); RECOVER;}
1012| modifiers VOID_TK error
1013 {yyerror ("Identifier expected"); RECOVER;}
1014| modifiers error
1015 {
1016 yyerror ("Invalid method declaration, return type required");
1017 RECOVER;
1018 }
1019;
1020
1021method_declarator:
1022 identifier OP_TK CP_TK
c2952b01
APB
1023 {
1024 ctxp->formal_parameter_number = 0;
1025 $$ = method_declarator ($1, NULL_TREE);
1026 }
e04a16fb
AG
1027| identifier OP_TK formal_parameter_list CP_TK
1028 { $$ = method_declarator ($1, $3); }
1029| method_declarator OSB_TK CSB_TK
1030 {
1886c9d8
APB
1031 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1032 TREE_PURPOSE ($1) =
1033 build_unresolved_array_type (TREE_PURPOSE ($1));
1034 parse_warning_context
1035 (wfl_operator,
1036 "Discouraged form of returned type specification");
e04a16fb
AG
1037 }
1038| identifier OP_TK error
1039 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1040| method_declarator OSB_TK error
1041 {yyerror ("']' expected"); RECOVER;}
1042;
1043
1044formal_parameter_list:
1045 formal_parameter
1046 {
1047 ctxp->formal_parameter_number = 1;
1048 }
1049| formal_parameter_list C_TK formal_parameter
1050 {
1051 ctxp->formal_parameter_number += 1;
1052 $$ = chainon ($1, $3);
1053 }
1054| formal_parameter_list C_TK error
c2952b01 1055 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1056;
1057
1058formal_parameter:
1059 type variable_declarator_id
1060 {
1061 $$ = build_tree_list ($2, $1);
1062 }
18990de5 1063| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1064 {
5256aa37 1065 $$ = build_tree_list ($3, $2);
c2952b01 1066 ARG_FINAL_P ($$) = 1;
5256aa37 1067 }
e04a16fb 1068| type error
f8989a66
APB
1069 {
1070 yyerror ("Missing identifier"); RECOVER;
1071 $$ = NULL_TREE;
1072 }
18990de5 1073| final type error
e04a16fb 1074 {
e04a16fb 1075 yyerror ("Missing identifier"); RECOVER;
f8989a66 1076 $$ = NULL_TREE;
e04a16fb
AG
1077 }
1078;
1079
18990de5
JB
1080final:
1081 modifiers
1082 {
1083 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1084 $1, ACC_FINAL);
1085 if ($1 != ACC_FINAL)
1086 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1087 }
1088;
1089
e04a16fb 1090throws:
b9f7e36c 1091 { $$ = NULL_TREE; }
e04a16fb 1092| THROWS_TK class_type_list
b9f7e36c 1093 { $$ = $2; }
e04a16fb
AG
1094| THROWS_TK error
1095 {yyerror ("Missing class type term"); RECOVER;}
1096;
1097
1098class_type_list:
1099 class_type
c877974e 1100 { $$ = build_tree_list ($1, $1); }
e04a16fb 1101| class_type_list C_TK class_type
c877974e 1102 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1103| class_type_list C_TK error
1104 {yyerror ("Missing class type term"); RECOVER;}
1105;
1106
1107method_body:
1108 block
5f1c312a 1109| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1110;
1111
1112/* 19.8.4 Productions from 8.5: Static Initializers */
1113static_initializer:
1114 static block
1115 {
c2952b01
APB
1116 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1117 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb 1118 }
e04a16fb
AG
1119;
1120
1121static: /* Test lval.sub_token here */
c2952b01 1122 modifiers
e04a16fb 1123 {
c2952b01
APB
1124 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1125 /* Can't have a static initializer in an innerclass */
1126 if ($1 | ACC_STATIC &&
1127 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1128 parse_error_context
1129 (MODIFIER_WFL (STATIC_TK),
1130 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1131 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1132 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1133 }
1134;
1135
1136/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1137constructor_declaration:
22eed1e6 1138 constructor_header
e04a16fb 1139 {
22eed1e6
APB
1140 current_function_decl = $1;
1141 source_start_java_method (current_function_decl);
e04a16fb 1142 }
22eed1e6 1143 constructor_body
b635eb2f 1144 { finish_method_declaration ($3); }
22eed1e6
APB
1145;
1146
1147constructor_header:
1148 constructor_declarator throws
1149 { $$ = method_header (0, NULL_TREE, $1, $2); }
1150| modifiers constructor_declarator throws
1151 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1152;
1153
1154constructor_declarator:
1155 simple_name OP_TK CP_TK
c2952b01
APB
1156 {
1157 ctxp->formal_parameter_number = 0;
1158 $$ = method_declarator ($1, NULL_TREE);
1159 }
e04a16fb 1160| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1161 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1162;
1163
1164constructor_body:
22eed1e6
APB
1165 /* Unlike regular method, we always need a complete (empty)
1166 body so we can safely perform all the required code
1167 addition (super invocation and field initialization) */
2e5eb5c5 1168 block_begin constructor_block_end
22eed1e6 1169 {
9bbc7d9f 1170 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1171 $$ = $2;
1172 }
2e5eb5c5 1173| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1174 { $$ = $3; }
2e5eb5c5 1175| block_begin block_statements constructor_block_end
22eed1e6 1176 { $$ = $3; }
2e5eb5c5 1177| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1178 { $$ = $4; }
e04a16fb
AG
1179;
1180
2e5eb5c5
APB
1181constructor_block_end:
1182 block_end
5f1c312a 1183;
2e5eb5c5 1184
e04a16fb
AG
1185/* Error recovery for that rule moved down expression_statement: rule. */
1186explicit_constructor_invocation:
1187 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1188 {
1189 $$ = build_method_invocation ($1, NULL_TREE);
1190 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1191 $$ = java_method_add_stmt (current_function_decl, $$);
1192 }
e04a16fb 1193| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1194 {
1195 $$ = build_method_invocation ($1, $3);
1196 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1197 $$ = java_method_add_stmt (current_function_decl, $$);
1198 }
e04a16fb
AG
1199 /* Added, JDK1.1 inner classes. Modified because the rule
1200 'primary' couldn't work. */
1201| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1202 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1203| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1204 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1205;
1206
1207this_or_super: /* Added, simplifies error diagnostics */
1208 THIS_TK
1209 {
9ee9b555 1210 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1211 EXPR_WFL_LINECOL (wfl) = $1.location;
1212 $$ = wfl;
1213 }
1214| SUPER_TK
1215 {
9ee9b555 1216 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1217 EXPR_WFL_LINECOL (wfl) = $1.location;
1218 $$ = wfl;
1219 }
1220;
1221
1222/* 19.9 Productions from 9: Interfaces */
1223/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1224interface_declaration:
1225 INTERFACE_TK identifier
1226 { create_interface (0, $2, NULL_TREE); }
1227 interface_body
e04a16fb
AG
1228| modifiers INTERFACE_TK identifier
1229 { create_interface ($1, $3, NULL_TREE); }
1230 interface_body
e04a16fb
AG
1231| INTERFACE_TK identifier extends_interfaces
1232 { create_interface (0, $2, $3); }
1233 interface_body
e04a16fb
AG
1234| modifiers INTERFACE_TK identifier extends_interfaces
1235 { create_interface ($1, $3, $4); }
1236 interface_body
e04a16fb 1237| INTERFACE_TK identifier error
0b4d333e 1238 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1239| modifiers INTERFACE_TK identifier error
0b4d333e 1240 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1241;
1242
1243extends_interfaces:
1244 EXTENDS_TK interface_type
1245 {
1246 ctxp->interface_number = 1;
1247 $$ = build_tree_list ($2, NULL_TREE);
1248 }
1249| extends_interfaces C_TK interface_type
1250 {
1251 ctxp->interface_number++;
1252 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1253 }
1254| EXTENDS_TK error
1255 {yyerror ("Invalid interface type"); RECOVER;}
1256| extends_interfaces C_TK error
1257 {yyerror ("Missing term"); RECOVER;}
1258;
1259
1260interface_body:
1261 OCB_TK CCB_TK
1262 { $$ = NULL_TREE; }
1263| OCB_TK interface_member_declarations CCB_TK
1264 { $$ = NULL_TREE; }
1265;
1266
1267interface_member_declarations:
1268 interface_member_declaration
1269| interface_member_declarations interface_member_declaration
1270;
1271
1272interface_member_declaration:
1273 constant_declaration
1274| abstract_method_declaration
1275| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1276 { end_class_declaration (1); }
1277| interface_declaration /* Added, JDK1.1 inner interfaces */
1278 { end_class_declaration (1); }
e04a16fb
AG
1279;
1280
1281constant_declaration:
1282 field_declaration
1283;
1284
1285abstract_method_declaration:
1286 method_header SC_TK
1287 {
1288 check_abstract_method_header ($1);
1289 current_function_decl = NULL_TREE; /* FIXME ? */
1290 }
1291| method_header error
1292 {yyerror ("';' expected"); RECOVER;}
1293;
1294
1295/* 19.10 Productions from 10: Arrays */
1296array_initializer:
1297 OCB_TK CCB_TK
1179ebc2 1298 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1299| OCB_TK variable_initializers CCB_TK
f8976021 1300 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1301| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1302 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1303;
1304
1305variable_initializers:
1306 variable_initializer
f8976021
APB
1307 {
1308 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1309 $1, NULL_TREE);
1310 }
e04a16fb 1311| variable_initializers C_TK variable_initializer
1179ebc2
APB
1312 {
1313 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1314 }
e04a16fb
AG
1315| variable_initializers C_TK error
1316 {yyerror ("Missing term"); RECOVER;}
1317;
1318
1319/* 19.11 Production from 14: Blocks and Statements */
1320block:
1321 OCB_TK CCB_TK
7f10c2e2
APB
1322 {
1323 /* Store the location of the `}' when doing xrefs */
1324 if (current_function_decl && flag_emit_xref)
1325 DECL_END_SOURCE_LINE (current_function_decl) =
1326 EXPR_WFL_ADD_COL ($2.location, 1);
1327 $$ = empty_stmt_node;
1328 }
22eed1e6
APB
1329| block_begin block_statements block_end
1330 { $$ = $3; }
1331;
1332
1333block_begin:
1334 OCB_TK
e04a16fb 1335 { enter_block (); }
22eed1e6
APB
1336;
1337
1338block_end:
e04a16fb
AG
1339 CCB_TK
1340 {
1341 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1342 /* Store the location of the `}' when doing xrefs */
1343 if (current_function_decl && flag_emit_xref)
1344 DECL_END_SOURCE_LINE (current_function_decl) =
1345 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1346 $$ = exit_block ();
c280e37a
APB
1347 if (!BLOCK_SUBBLOCKS ($$))
1348 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1349 }
1350;
1351
1352block_statements:
1353 block_statement
1354| block_statements block_statement
1355;
1356
1357block_statement:
1358 local_variable_declaration_statement
1359| statement
15fdcfe9 1360 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1361| class_declaration /* Added, JDK1.1 local classes */
1362 {
1363 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1364 end_class_declaration (1);
1365 }
e04a16fb
AG
1366;
1367
1368local_variable_declaration_statement:
1369 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1370;
1371
1372local_variable_declaration:
1373 type variable_declarators
1374 { declare_local_variables (0, $1, $2); }
a003f638 1375| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1376 { declare_local_variables ($1, $2, $3); }
1377;
1378
1379statement:
1380 statement_without_trailing_substatement
1381| labeled_statement
e04a16fb 1382| if_then_statement
e04a16fb 1383| if_then_else_statement
e04a16fb 1384| while_statement
e04a16fb 1385| for_statement
cd9643f7 1386 { $$ = exit_block (); }
e04a16fb
AG
1387;
1388
1389statement_nsi:
1390 statement_without_trailing_substatement
1391| labeled_statement_nsi
e04a16fb 1392| if_then_else_statement_nsi
e04a16fb 1393| while_statement_nsi
e04a16fb 1394| for_statement_nsi
9dd939b2 1395 { $$ = exit_block (); }
e04a16fb
AG
1396;
1397
1398statement_without_trailing_substatement:
1399 block
e04a16fb 1400| empty_statement
e04a16fb 1401| expression_statement
e04a16fb 1402| switch_statement
e04a16fb 1403| do_statement
e04a16fb 1404| break_statement
e04a16fb 1405| continue_statement
e04a16fb
AG
1406| return_statement
1407| synchronized_statement
e04a16fb 1408| throw_statement
e04a16fb 1409| try_statement
e04a16fb
AG
1410;
1411
1412empty_statement:
1413 SC_TK
5f1c312a
APB
1414 {
1415 if (flag_extraneous_semicolon)
1416 {
1417 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1418 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1419 }
1420 $$ = empty_stmt_node;
1421 }
e04a16fb
AG
1422;
1423
1424label_decl:
1425 identifier REL_CL_TK
1426 {
1427 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1428 EXPR_WFL_NODE ($1));
e04a16fb
AG
1429 pushlevel (2);
1430 push_labeled_block ($$);
1431 PUSH_LABELED_BLOCK ($$);
1432 }
1433;
1434
1435labeled_statement:
1436 label_decl statement
b635eb2f 1437 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1438| identifier error
1439 {yyerror ("':' expected"); RECOVER;}
1440;
1441
1442labeled_statement_nsi:
1443 label_decl statement_nsi
b635eb2f 1444 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1445;
1446
1447/* We concentrate here a bunch of error handling rules that we couldn't write
1448 earlier, because expression_statement catches a missing ';'. */
1449expression_statement:
1450 statement_expression SC_TK
1451 {
1452 /* We have a statement. Generate a WFL around it so
1453 we can debug it */
1454 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1455 /* We know we have a statement, so set the debug
1456 info to be eventually generate here. */
1457 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1458 }
1459| error SC_TK
1460 {
1461 if (ctxp->prevent_ese != lineno)
1462 yyerror ("Invalid expression statement");
1463 DRECOVER (expr_stmt);
1464 }
1465| error OCB_TK
1466 {
1467 if (ctxp->prevent_ese != lineno)
1468 yyerror ("Invalid expression statement");
1469 DRECOVER (expr_stmt);
1470 }
1471| error CCB_TK
1472 {
1473 if (ctxp->prevent_ese != lineno)
1474 yyerror ("Invalid expression statement");
1475 DRECOVER (expr_stmt);
1476 }
1477| this_or_super OP_TK error
1478 {yyerror ("')' expected"); RECOVER;}
1479| this_or_super OP_TK CP_TK error
22eed1e6 1480 {
8119c720 1481 parse_ctor_invocation_error ();
22eed1e6
APB
1482 RECOVER;
1483 }
e04a16fb
AG
1484| this_or_super OP_TK argument_list error
1485 {yyerror ("')' expected"); RECOVER;}
1486| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1487 {
8119c720 1488 parse_ctor_invocation_error ();
22eed1e6
APB
1489 RECOVER;
1490 }
e04a16fb
AG
1491| name DOT_TK SUPER_TK error
1492 {yyerror ("'(' expected"); RECOVER;}
1493| name DOT_TK SUPER_TK OP_TK error
1494 {yyerror ("')' expected"); RECOVER;}
1495| name DOT_TK SUPER_TK OP_TK argument_list error
1496 {yyerror ("')' expected"); RECOVER;}
1497| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1498 {yyerror ("';' expected"); RECOVER;}
1499| name DOT_TK SUPER_TK OP_TK CP_TK error
1500 {yyerror ("';' expected"); RECOVER;}
1501;
1502
1503statement_expression:
1504 assignment
1505| pre_increment_expression
e04a16fb 1506| pre_decrement_expression
e04a16fb 1507| post_increment_expression
e04a16fb 1508| post_decrement_expression
e04a16fb
AG
1509| method_invocation
1510| class_instance_creation_expression
e04a16fb
AG
1511;
1512
1513if_then_statement:
1514 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1515 {
1516 $$ = build_if_else_statement ($2.location, $3,
1517 $5, NULL_TREE);
1518 }
e04a16fb
AG
1519| IF_TK error
1520 {yyerror ("'(' expected"); RECOVER;}
1521| IF_TK OP_TK error
1522 {yyerror ("Missing term"); RECOVER;}
1523| IF_TK OP_TK expression error
1524 {yyerror ("')' expected"); RECOVER;}
1525;
1526
1527if_then_else_statement:
1528 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1529 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1530;
1531
1532if_then_else_statement_nsi:
1533 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1534 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1535;
1536
1537switch_statement:
15fdcfe9
PB
1538 switch_expression
1539 {
1540 enter_block ();
1541 }
1542 switch_block
b67d701b 1543 {
15fdcfe9 1544 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1545 I.e. make the last statment also have its own
1546 COMPOUND_EXPR. */
15fdcfe9
PB
1547 maybe_absorb_scoping_blocks ();
1548 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1549 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1550 }
1551;
1552
1553switch_expression:
1554 SWITCH_TK OP_TK expression CP_TK
1555 {
1556 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1557 EXPR_WFL_LINECOL ($$) = $2.location;
1558 }
e04a16fb
AG
1559| SWITCH_TK error
1560 {yyerror ("'(' expected"); RECOVER;}
1561| SWITCH_TK OP_TK error
1562 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1563| SWITCH_TK OP_TK expression CP_TK error
1564 {yyerror ("'{' expected"); RECOVER;}
1565;
1566
f8976021
APB
1567/* Default assignment is there to avoid type node on switch_block
1568 node. */
1569
e04a16fb
AG
1570switch_block:
1571 OCB_TK CCB_TK
f8976021 1572 { $$ = NULL_TREE; }
e04a16fb 1573| OCB_TK switch_labels CCB_TK
f8976021 1574 { $$ = NULL_TREE; }
e04a16fb 1575| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1576 { $$ = NULL_TREE; }
e04a16fb 1577| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1578 { $$ = NULL_TREE; }
e04a16fb
AG
1579;
1580
1581switch_block_statement_groups:
1582 switch_block_statement_group
1583| switch_block_statement_groups switch_block_statement_group
1584;
1585
1586switch_block_statement_group:
15fdcfe9 1587 switch_labels block_statements
e04a16fb
AG
1588;
1589
e04a16fb
AG
1590switch_labels:
1591 switch_label
1592| switch_labels switch_label
1593;
1594
1595switch_label:
1596 CASE_TK constant_expression REL_CL_TK
b67d701b 1597 {
15fdcfe9
PB
1598 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1599 EXPR_WFL_LINECOL (lab) = $1.location;
1600 java_method_add_stmt (current_function_decl, lab);
b67d701b 1601 }
e04a16fb 1602| DEFAULT_TK REL_CL_TK
b67d701b 1603 {
15fdcfe9
PB
1604 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1605 EXPR_WFL_LINECOL (lab) = $1.location;
1606 java_method_add_stmt (current_function_decl, lab);
b67d701b 1607 }
e04a16fb
AG
1608| CASE_TK error
1609 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1610| CASE_TK constant_expression error
1611 {yyerror ("':' expected"); RECOVER;}
1612| DEFAULT_TK error
1613 {yyerror ("':' expected"); RECOVER;}
1614;
1615
1616while_expression:
1617 WHILE_TK OP_TK expression CP_TK
1618 {
1619 tree body = build_loop_body ($2.location, $3, 0);
1620 $$ = build_new_loop (body);
1621 }
1622;
1623
1624while_statement:
1625 while_expression statement
b635eb2f 1626 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1627| WHILE_TK error
1628 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1629| WHILE_TK OP_TK error
1630 {yyerror ("Missing term and ')' expected"); RECOVER;}
1631| WHILE_TK OP_TK expression error
1632 {yyerror ("')' expected"); RECOVER;}
1633;
1634
1635while_statement_nsi:
1636 while_expression statement_nsi
b635eb2f 1637 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1638;
1639
1640do_statement_begin:
1641 DO_TK
1642 {
1643 tree body = build_loop_body (0, NULL_TREE, 1);
1644 $$ = build_new_loop (body);
1645 }
1646 /* Need error handing here. FIXME */
1647;
1648
1649do_statement:
1650 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1651 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1652;
1653
1654for_statement:
1655 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1656 {
1657 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1658 $3 = build_wfl_node ($3);
1659 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1660 }
e04a16fb
AG
1661| for_begin SC_TK SC_TK for_update CP_TK statement
1662 {
b635eb2f 1663 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1664 /* We have not condition, so we get rid of the EXIT_EXPR */
1665 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1666 empty_stmt_node;
e04a16fb
AG
1667 }
1668| for_begin SC_TK error
1669 {yyerror ("Invalid control expression"); RECOVER;}
1670| for_begin SC_TK expression SC_TK error
1671 {yyerror ("Invalid update expression"); RECOVER;}
1672| for_begin SC_TK SC_TK error
1673 {yyerror ("Invalid update expression"); RECOVER;}
1674;
1675
1676for_statement_nsi:
1677 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1678 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1679| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1680 {
b635eb2f 1681 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1682 /* We have not condition, so we get rid of the EXIT_EXPR */
1683 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1684 empty_stmt_node;
e04a16fb
AG
1685 }
1686;
1687
1688for_header:
1689 FOR_TK OP_TK
1690 {
1691 /* This scope defined for local variable that may be
1692 defined within the scope of the for loop */
1693 enter_block ();
1694 }
1695| FOR_TK error
1696 {yyerror ("'(' expected"); DRECOVER(for_1);}
1697| FOR_TK OP_TK error
1698 {yyerror ("Invalid init statement"); RECOVER;}
1699;
1700
1701for_begin:
1702 for_header for_init
1703 {
1704 /* We now declare the loop body. The loop is
1705 declared as a for loop. */
1706 tree body = build_loop_body (0, NULL_TREE, 0);
1707 $$ = build_new_loop (body);
c2952b01 1708 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1709 /* The loop is added to the current block the for
1710 statement is defined within */
1711 java_method_add_stmt (current_function_decl, $$);
1712 }
1713;
1714for_init: /* Can be empty */
9bbc7d9f 1715 { $$ = empty_stmt_node; }
e04a16fb
AG
1716| statement_expression_list
1717 {
1718 /* Init statement recorded within the previously
1719 defined block scope */
1720 $$ = java_method_add_stmt (current_function_decl, $1);
1721 }
1722| local_variable_declaration
1723 {
1724 /* Local variable are recorded within the previously
1725 defined block scope */
1726 $$ = NULL_TREE;
1727 }
1728| statement_expression_list error
1729 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1730;
1731
1732for_update: /* Can be empty */
9bbc7d9f 1733 {$$ = empty_stmt_node;}
e04a16fb
AG
1734| statement_expression_list
1735 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1736;
1737
1738statement_expression_list:
1739 statement_expression
1740 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1741| statement_expression_list C_TK statement_expression
1742 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1743| statement_expression_list C_TK error
1744 {yyerror ("Missing term"); RECOVER;}
1745;
1746
1747break_statement:
1748 BREAK_TK SC_TK
1749 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1750| BREAK_TK identifier SC_TK
1751 { $$ = build_bc_statement ($1.location, 1, $2); }
1752| BREAK_TK error
1753 {yyerror ("Missing term"); RECOVER;}
1754| BREAK_TK identifier error
1755 {yyerror ("';' expected"); RECOVER;}
1756;
1757
1758continue_statement:
1759 CONTINUE_TK SC_TK
1760 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1761| CONTINUE_TK identifier SC_TK
1762 { $$ = build_bc_statement ($1.location, 0, $2); }
1763| CONTINUE_TK error
1764 {yyerror ("Missing term"); RECOVER;}
1765| CONTINUE_TK identifier error
1766 {yyerror ("';' expected"); RECOVER;}
1767;
1768
1769return_statement:
1770 RETURN_TK SC_TK
1771 { $$ = build_return ($1.location, NULL_TREE); }
1772| RETURN_TK expression SC_TK
1773 { $$ = build_return ($1.location, $2); }
1774| RETURN_TK error
1775 {yyerror ("Missing term"); RECOVER;}
1776| RETURN_TK expression error
1777 {yyerror ("';' expected"); RECOVER;}
1778;
1779
1780throw_statement:
1781 THROW_TK expression SC_TK
b9f7e36c
APB
1782 {
1783 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1784 EXPR_WFL_LINECOL ($$) = $1.location;
1785 }
e04a16fb
AG
1786| THROW_TK error
1787 {yyerror ("Missing term"); RECOVER;}
1788| THROW_TK expression error
1789 {yyerror ("';' expected"); RECOVER;}
1790;
1791
1792synchronized_statement:
1793 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1794 {
1795 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1796 EXPR_WFL_LINECOL ($$) =
1797 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1798 }
e04a16fb
AG
1799| synchronized OP_TK expression CP_TK error
1800 {yyerror ("'{' expected"); RECOVER;}
1801| synchronized error
1802 {yyerror ("'(' expected"); RECOVER;}
1803| synchronized OP_TK error CP_TK
1804 {yyerror ("Missing term"); RECOVER;}
1805| synchronized OP_TK error
1806 {yyerror ("Missing term"); RECOVER;}
1807;
1808
b9f7e36c 1809synchronized:
efa0a23f 1810 modifiers
e04a16fb 1811 {
781b0558
KG
1812 check_modifiers (
1813 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1814 $1, ACC_SYNCHRONIZED);
1815 if ($1 != ACC_SYNCHRONIZED)
1816 MODIFIER_WFL (SYNCHRONIZED_TK) =
1817 build_wfl_node (NULL_TREE);
e04a16fb
AG
1818 }
1819;
1820
1821try_statement:
1822 TRY_TK block catches
a7d8d81f 1823 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1824| TRY_TK block finally
a7d8d81f 1825 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1826| TRY_TK block catches finally
2aa11e97
APB
1827 { $$ = build_try_finally_statement
1828 ($1.location, build_try_statement ($1.location,
1829 $2, $3), $4);
1830 }
e04a16fb
AG
1831| TRY_TK error
1832 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1833;
1834
1835catches:
1836 catch_clause
1837| catches catch_clause
b67d701b
PB
1838 {
1839 TREE_CHAIN ($2) = $1;
1840 $$ = $2;
1841 }
e04a16fb
AG
1842;
1843
1844catch_clause:
b67d701b
PB
1845 catch_clause_parameter block
1846 {
1847 java_method_add_stmt (current_function_decl, $2);
1848 exit_block ();
1849 $$ = $1;
1850 }
1851
1852catch_clause_parameter:
1853 CATCH_TK OP_TK formal_parameter CP_TK
1854 {
1855 /* We add a block to define a scope for
1856 formal_parameter (CCBP). The formal parameter is
1857 declared initialized by the appropriate function
1858 call */
1859 tree ccpb = enter_block ();
1860 tree init = build_assignment (ASSIGN_TK, $2.location,
1861 TREE_PURPOSE ($3),
1862 soft_exceptioninfo_call_node);
1863 declare_local_variables (0, TREE_VALUE ($3),
1864 build_tree_list (TREE_PURPOSE ($3),
1865 init));
1866 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1867 EXPR_WFL_LINECOL ($$) = $1.location;
1868 }
e04a16fb 1869| CATCH_TK error
97f30284 1870 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1871| CATCH_TK OP_TK error
97f30284
APB
1872 {
1873 yyerror ("Missing term or ')' expected");
1874 RECOVER; $$ = NULL_TREE;
1875 }
b67d701b 1876| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1877 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1878;
1879
1880finally:
1881 FINALLY_TK block
a7d8d81f 1882 { $$ = $2; }
e04a16fb
AG
1883| FINALLY_TK error
1884 {yyerror ("'{' expected"); RECOVER; }
1885;
1886
1887/* 19.12 Production from 15: Expressions */
1888primary:
1889 primary_no_new_array
1890| array_creation_expression
1891;
1892
1893primary_no_new_array:
1894 literal
1895| THIS_TK
1896 { $$ = build_this ($1.location); }
1897| OP_TK expression CP_TK
1898 {$$ = $2;}
1899| class_instance_creation_expression
1900| field_access
1901| method_invocation
1902| array_access
c2952b01 1903| type_literals
e04a16fb
AG
1904 /* Added, JDK1.1 inner classes. Documentation is wrong
1905 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1906 exist. Used name: instead. */
e04a16fb 1907| name DOT_TK THIS_TK
c2952b01
APB
1908 {
1909 tree wfl = build_wfl_node (this_identifier_node);
1910 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1911 }
e04a16fb
AG
1912| OP_TK expression error
1913 {yyerror ("')' expected"); RECOVER;}
1914| name DOT_TK error
1915 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1916| primitive_type DOT_TK error
1917 {yyerror ("'class' expected" ); RECOVER;}
1918| VOID_TK DOT_TK error
1919 {yyerror ("'class' expected" ); RECOVER;}
1920;
1921
c2952b01
APB
1922/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1923 broke the rule down a bit. */
1924
1925array_type_literal:
1926 primitive_type OSB_TK CSB_TK
1927 {
1928 $$ = build_java_array_type ($1, -1);
1929 CLASS_LOADED_P ($$) = 1;
1930 }
1931| name OSB_TK CSB_TK
1932 { $$ = build_unresolved_array_type ($1); }
1933/* This triggers two reduce/reduce conflict between array_type_literal and
1934 dims. FIXME.
1935| array_type OSB_TK CSB_TK
1936 { $$ = build_unresolved_array_type ($1); }
1937*/
1938;
1939
1940type_literals:
1941 name DOT_TK CLASS_TK
1942 { $$ = build_incomplete_class_ref ($2.location, $1); }
1943| array_type_literal DOT_TK CLASS_TK
1944 { $$ = build_incomplete_class_ref ($2.location, $1); }
1945| primitive_type DOT_TK CLASS_TK
1946 { $$ = build_class_ref ($1); }
1947| VOID_TK DOT_TK CLASS_TK
1948 { $$ = build_class_ref (void_type_node); }
1949;
1950
e04a16fb
AG
1951class_instance_creation_expression:
1952 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1953 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1954| NEW_TK class_type OP_TK CP_TK
b67d701b 1955 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1956| anonymous_class_creation
e04a16fb
AG
1957 /* Added, JDK1.1 inner classes, modified to use name or
1958 primary instead of primary solely which couldn't work in
1959 all situations. */
1960| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1961 {
1962 tree ctor = build_new_invocation ($2, NULL_TREE);
1963 $$ = make_qualified_primary ($1, ctor,
1964 EXPR_WFL_LINECOL ($1));
1965 }
e04a16fb
AG
1966| something_dot_new identifier OP_TK CP_TK class_body
1967| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1968 {
1969 tree ctor = build_new_invocation ($2, $4);
1970 $$ = make_qualified_primary ($1, ctor,
1971 EXPR_WFL_LINECOL ($1));
1972 }
e04a16fb
AG
1973| something_dot_new identifier OP_TK argument_list CP_TK class_body
1974| NEW_TK error SC_TK
1975 {yyerror ("'(' expected"); DRECOVER(new_1);}
1976| NEW_TK class_type error
1977 {yyerror ("'(' expected"); RECOVER;}
1978| NEW_TK class_type OP_TK error
1979 {yyerror ("')' or term expected"); RECOVER;}
1980| NEW_TK class_type OP_TK argument_list error
1981 {yyerror ("')' expected"); RECOVER;}
1982| something_dot_new error
1983 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1984| something_dot_new identifier error
1985 {yyerror ("'(' expected"); RECOVER;}
1986;
1987
c2952b01
APB
1988/* Created after JDK1.1 rules originally added to
1989 class_instance_creation_expression, but modified to use
1990 'class_type' instead of 'TypeName' (type_name) which is mentionned
1991 in the documentation but doesn't exist. */
1992
1993anonymous_class_creation:
1994 NEW_TK class_type OP_TK argument_list CP_TK
1995 { create_anonymous_class ($1.location, $2); }
1996 class_body
1997 {
1998 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1999 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2000
2001 end_class_declaration (1);
2002
2003 /* Now we can craft the new expression */
2004 $$ = build_new_invocation (id, $4);
2005
2006 /* Note that we can't possibly be here if
2007 `class_type' is an interface (in which case the
2008 anonymous class extends Object and implements
2009 `class_type', hence its constructor can't have
2010 arguments.) */
2011
2012 /* Otherwise, the innerclass must feature a
2013 constructor matching `argument_list'. Anonymous
2014 classes are a bit special: it's impossible to
2015 define constructor for them, hence constructors
2016 must be generated following the hints provided by
2017 the `new' expression. Whether a super constructor
2018 of that nature exists or not is to be verified
2019 later on in verify_constructor_super.
2020
2021 It's during the expansion of a `new' statement
2022 refering to an anonymous class that a ctor will
2023 be generated for the anonymous class, with the
2024 right arguments. */
2025
2026 }
2027| NEW_TK class_type OP_TK CP_TK
2028 { create_anonymous_class ($1.location, $2); }
2029 class_body
2030 {
2031 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2032 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2033
2034 end_class_declaration (1);
2035
2036 /* Now we can craft the new expression. The
2037 statement doesn't need to be remember so that a
2038 constructor can be generated, since its signature
2039 is already known. */
2040 $$ = build_new_invocation (id, NULL_TREE);
2041 }
2042;
2043
e04a16fb
AG
2044something_dot_new: /* Added, not part of the specs. */
2045 name DOT_TK NEW_TK
c2952b01 2046 { $$ = $1; }
e04a16fb 2047| primary DOT_TK NEW_TK
c2952b01 2048 { $$ = $1; }
e04a16fb
AG
2049;
2050
2051argument_list:
2052 expression
2053 {
2054 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2055 ctxp->formal_parameter_number = 1;
2056 }
2057| argument_list C_TK expression
2058 {
2059 ctxp->formal_parameter_number += 1;
2060 $$ = tree_cons (NULL_TREE, $3, $1);
2061 }
2062| argument_list C_TK error
2063 {yyerror ("Missing term"); RECOVER;}
2064;
2065
2066array_creation_expression:
2067 NEW_TK primitive_type dim_exprs
2068 { $$ = build_newarray_node ($2, $3, 0); }
2069| NEW_TK class_or_interface_type dim_exprs
2070 { $$ = build_newarray_node ($2, $3, 0); }
2071| NEW_TK primitive_type dim_exprs dims
ba179f9f 2072 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2073| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2074 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2075 /* Added, JDK1.1 anonymous array. Initial documentation rule
2076 modified */
2077| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2078 {
2079 char *sig;
2080 while (CURRENT_OSB (ctxp)--)
2081 obstack_1grow (&temporary_obstack, '[');
2082 sig = obstack_finish (&temporary_obstack);
2083 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2084 $2, get_identifier (sig), $4);
2085 }
e04a16fb 2086| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2087 {
2088 tree type = $2;
2089 while (CURRENT_OSB (ctxp)--)
2090 type = build_java_array_type (type, -1);
2091 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2092 build_pointer_type (type), NULL_TREE, $4);
2093 }
e04a16fb
AG
2094| NEW_TK error CSB_TK
2095 {yyerror ("'[' expected"); DRECOVER ("]");}
2096| NEW_TK error OSB_TK
2097 {yyerror ("']' expected"); RECOVER;}
2098;
2099
2100dim_exprs:
2101 dim_expr
2102 { $$ = build_tree_list (NULL_TREE, $1); }
2103| dim_exprs dim_expr
2104 { $$ = tree_cons (NULL_TREE, $2, $$); }
2105;
2106
2107dim_expr:
2108 OSB_TK expression CSB_TK
2109 {
9a7ab4b3
APB
2110 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2111 {
2112 $2 = build_wfl_node ($2);
2113 TREE_TYPE ($2) = NULL_TREE;
2114 }
e04a16fb
AG
2115 EXPR_WFL_LINECOL ($2) = $1.location;
2116 $$ = $2;
2117 }
2118| OSB_TK expression error
2119 {yyerror ("']' expected"); RECOVER;}
2120| OSB_TK error
2121 {
2122 yyerror ("Missing term");
2123 yyerror ("']' expected");
2124 RECOVER;
2125 }
2126;
2127
2128dims:
2129 OSB_TK CSB_TK
ba179f9f
APB
2130 {
2131 int allocate = 0;
2132 /* If not initialized, allocate memory for the osb
2133 numbers stack */
2134 if (!ctxp->osb_limit)
2135 {
2136 allocate = ctxp->osb_limit = 32;
2137 ctxp->osb_depth = -1;
2138 }
c2952b01 2139 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2140 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2141 allocate = ctxp->osb_limit << 1;
2142
2143 if (allocate)
2144 {
2145 allocate *= sizeof (int);
2146 if (ctxp->osb_number)
2147 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2148 allocate);
2149 else
2150 ctxp->osb_number = (int *)xmalloc (allocate);
2151 }
2152 ctxp->osb_depth++;
2153 CURRENT_OSB (ctxp) = 1;
2154 }
e04a16fb 2155| dims OSB_TK CSB_TK
ba179f9f 2156 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2157| dims OSB_TK error
2158 { yyerror ("']' expected"); RECOVER;}
2159;
2160
2161field_access:
2162 primary DOT_TK identifier
2163 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2164 /* FIXME - REWRITE TO:
2165 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2166| SUPER_TK DOT_TK identifier
2167 {
2168 tree super_wfl =
9ee9b555 2169 build_wfl_node (super_identifier_node);
e04a16fb
AG
2170 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2171 $$ = make_qualified_name (super_wfl, $3, $2.location);
2172 }
2173| SUPER_TK error
2174 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2175;
2176
2177method_invocation:
2178 name OP_TK CP_TK
2179 { $$ = build_method_invocation ($1, NULL_TREE); }
2180| name OP_TK argument_list CP_TK
2181 { $$ = build_method_invocation ($1, $3); }
2182| primary DOT_TK identifier OP_TK CP_TK
2183 {
22eed1e6
APB
2184 if (TREE_CODE ($1) == THIS_EXPR)
2185 $$ = build_this_super_qualified_invocation
2186 (1, $3, NULL_TREE, 0, $2.location);
2187 else
2188 {
2189 tree invok = build_method_invocation ($3, NULL_TREE);
2190 $$ = make_qualified_primary ($1, invok, $2.location);
2191 }
e04a16fb
AG
2192 }
2193| primary DOT_TK identifier OP_TK argument_list CP_TK
2194 {
22eed1e6
APB
2195 if (TREE_CODE ($1) == THIS_EXPR)
2196 $$ = build_this_super_qualified_invocation
2197 (1, $3, $5, 0, $2.location);
2198 else
2199 {
2200 tree invok = build_method_invocation ($3, $5);
2201 $$ = make_qualified_primary ($1, invok, $2.location);
2202 }
e04a16fb
AG
2203 }
2204| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2205 {
2206 $$ = build_this_super_qualified_invocation
2207 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2208 }
2209| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2210 {
22eed1e6
APB
2211 $$ = build_this_super_qualified_invocation
2212 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2213 }
2214 /* Screws up thing. I let it here until I'm convinced it can
2215 be removed. FIXME
2216| primary DOT_TK error
2217 {yyerror ("'(' expected"); DRECOVER(bad);} */
2218| SUPER_TK DOT_TK error CP_TK
2219 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2220| SUPER_TK DOT_TK error DOT_TK
2221 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2222;
2223
2224array_access:
2225 name OSB_TK expression CSB_TK
2226 { $$ = build_array_ref ($2.location, $1, $3); }
2227| primary_no_new_array OSB_TK expression CSB_TK
2228 { $$ = build_array_ref ($2.location, $1, $3); }
2229| name OSB_TK error
2230 {
2231 yyerror ("Missing term and ']' expected");
2232 DRECOVER(array_access);
2233 }
2234| name OSB_TK expression error
2235 {
2236 yyerror ("']' expected");
2237 DRECOVER(array_access);
2238 }
2239| primary_no_new_array OSB_TK error
2240 {
2241 yyerror ("Missing term and ']' expected");
2242 DRECOVER(array_access);
2243 }
2244| primary_no_new_array OSB_TK expression error
2245 {
2246 yyerror ("']' expected");
2247 DRECOVER(array_access);
2248 }
2249;
2250
2251postfix_expression:
2252 primary
2253| name
2254| post_increment_expression
2255| post_decrement_expression
2256;
2257
2258post_increment_expression:
2259 postfix_expression INCR_TK
2260 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2261;
2262
2263post_decrement_expression:
2264 postfix_expression DECR_TK
2265 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2266;
2267
2268unary_expression:
2269 pre_increment_expression
2270| pre_decrement_expression
2271| PLUS_TK unary_expression
2272 {$$ = build_unaryop ($1.token, $1.location, $2); }
2273| MINUS_TK unary_expression
2274 {$$ = build_unaryop ($1.token, $1.location, $2); }
2275| unary_expression_not_plus_minus
2276| PLUS_TK error
2277 {yyerror ("Missing term"); RECOVER}
2278| MINUS_TK error
2279 {yyerror ("Missing term"); RECOVER}
2280;
2281
2282pre_increment_expression:
2283 INCR_TK unary_expression
2284 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2285| INCR_TK error
2286 {yyerror ("Missing term"); RECOVER}
2287;
2288
2289pre_decrement_expression:
2290 DECR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2292| DECR_TK error
2293 {yyerror ("Missing term"); RECOVER}
2294;
2295
2296unary_expression_not_plus_minus:
2297 postfix_expression
2298| NOT_TK unary_expression
2299 {$$ = build_unaryop ($1.token, $1.location, $2); }
2300| NEG_TK unary_expression
2301 {$$ = build_unaryop ($1.token, $1.location, $2); }
2302| cast_expression
2303| NOT_TK error
2304 {yyerror ("Missing term"); RECOVER}
2305| NEG_TK error
2306 {yyerror ("Missing term"); RECOVER}
2307;
2308
2309cast_expression: /* Error handling here is potentially weak */
2310 OP_TK primitive_type dims CP_TK unary_expression
2311 {
2312 tree type = $2;
ba179f9f 2313 while (CURRENT_OSB (ctxp)--)
e04a16fb 2314 type = build_java_array_type (type, -1);
ba179f9f 2315 ctxp->osb_depth--;
e04a16fb
AG
2316 $$ = build_cast ($1.location, type, $5);
2317 }
2318| OP_TK primitive_type CP_TK unary_expression
2319 { $$ = build_cast ($1.location, $2, $4); }
2320| OP_TK expression CP_TK unary_expression_not_plus_minus
2321 { $$ = build_cast ($1.location, $2, $4); }
2322| OP_TK name dims CP_TK unary_expression_not_plus_minus
2323 {
49f48c71 2324 const char *ptr;
ba179f9f 2325 while (CURRENT_OSB (ctxp)--)
e04a16fb 2326 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2327 ctxp->osb_depth--;
e04a16fb
AG
2328 obstack_grow0 (&temporary_obstack,
2329 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2330 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2331 ptr = obstack_finish (&temporary_obstack);
2332 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2333 $$ = build_cast ($1.location, $2, $5);
2334 }
2335| OP_TK primitive_type OSB_TK error
2336 {yyerror ("']' expected, invalid type expression");}
2337| OP_TK error
2338 {
2339 if (ctxp->prevent_ese != lineno)
2340 yyerror ("Invalid type expression"); RECOVER;
2341 RECOVER;
2342 }
2343| OP_TK primitive_type dims CP_TK error
2344 {yyerror ("Missing term"); RECOVER;}
2345| OP_TK primitive_type CP_TK error
2346 {yyerror ("Missing term"); RECOVER;}
2347| OP_TK name dims CP_TK error
2348 {yyerror ("Missing term"); RECOVER;}
2349;
2350
2351multiplicative_expression:
2352 unary_expression
2353| multiplicative_expression MULT_TK unary_expression
2354 {
2355 $$ = build_binop (BINOP_LOOKUP ($2.token),
2356 $2.location, $1, $3);
2357 }
2358| multiplicative_expression DIV_TK unary_expression
2359 {
2360 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2361 $1, $3);
2362 }
2363| multiplicative_expression REM_TK unary_expression
2364 {
2365 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2366 $1, $3);
2367 }
2368| multiplicative_expression MULT_TK error
2369 {yyerror ("Missing term"); RECOVER;}
2370| multiplicative_expression DIV_TK error
2371 {yyerror ("Missing term"); RECOVER;}
2372| multiplicative_expression REM_TK error
2373 {yyerror ("Missing term"); RECOVER;}
2374;
2375
2376additive_expression:
2377 multiplicative_expression
2378| additive_expression PLUS_TK multiplicative_expression
2379 {
2380 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2381 $1, $3);
2382 }
2383| additive_expression MINUS_TK multiplicative_expression
2384 {
2385 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2386 $1, $3);
2387 }
2388| additive_expression PLUS_TK error
2389 {yyerror ("Missing term"); RECOVER;}
2390| additive_expression MINUS_TK error
2391 {yyerror ("Missing term"); RECOVER;}
2392;
2393
2394shift_expression:
2395 additive_expression
2396| shift_expression LS_TK additive_expression
2397 {
2398 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 $1, $3);
2400 }
2401| shift_expression SRS_TK additive_expression
2402 {
2403 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2404 $1, $3);
2405 }
2406| shift_expression ZRS_TK additive_expression
2407 {
2408 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2409 $1, $3);
2410 }
2411| shift_expression LS_TK error
2412 {yyerror ("Missing term"); RECOVER;}
2413| shift_expression SRS_TK error
2414 {yyerror ("Missing term"); RECOVER;}
2415| shift_expression ZRS_TK error
2416 {yyerror ("Missing term"); RECOVER;}
2417;
2418
2419relational_expression:
2420 shift_expression
2421| relational_expression LT_TK shift_expression
2422 {
2423 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2424 $1, $3);
2425 }
2426| relational_expression GT_TK shift_expression
2427 {
2428 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2429 $1, $3);
2430 }
2431| relational_expression LTE_TK shift_expression
2432 {
2433 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2434 $1, $3);
2435 }
2436| relational_expression GTE_TK shift_expression
2437 {
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439 $1, $3);
2440 }
2441| relational_expression INSTANCEOF_TK reference_type
5e942c50 2442 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2443| relational_expression LT_TK error
2444 {yyerror ("Missing term"); RECOVER;}
2445| relational_expression GT_TK error
2446 {yyerror ("Missing term"); RECOVER;}
2447| relational_expression LTE_TK error
2448 {yyerror ("Missing term"); RECOVER;}
2449| relational_expression GTE_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451| relational_expression INSTANCEOF_TK error
2452 {yyerror ("Invalid reference type"); RECOVER;}
2453;
2454
2455equality_expression:
2456 relational_expression
2457| equality_expression EQ_TK relational_expression
2458 {
2459 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2460 $1, $3);
2461 }
2462| equality_expression NEQ_TK relational_expression
2463 {
2464 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2465 $1, $3);
2466 }
2467| equality_expression EQ_TK error
2468 {yyerror ("Missing term"); RECOVER;}
2469| equality_expression NEQ_TK error
2470 {yyerror ("Missing term"); RECOVER;}
2471;
2472
2473and_expression:
2474 equality_expression
2475| and_expression AND_TK equality_expression
2476 {
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2479 }
2480| and_expression AND_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482;
2483
2484exclusive_or_expression:
2485 and_expression
2486| exclusive_or_expression XOR_TK and_expression
2487 {
2488 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2489 $1, $3);
2490 }
2491| exclusive_or_expression XOR_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2493;
2494
2495inclusive_or_expression:
2496 exclusive_or_expression
2497| inclusive_or_expression OR_TK exclusive_or_expression
2498 {
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 $1, $3);
2501 }
2502| inclusive_or_expression OR_TK error
2503 {yyerror ("Missing term"); RECOVER;}
2504;
2505
2506conditional_and_expression:
2507 inclusive_or_expression
2508| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2509 {
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511 $1, $3);
2512 }
2513| conditional_and_expression BOOL_AND_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2515;
2516
2517conditional_or_expression:
2518 conditional_and_expression
2519| conditional_or_expression BOOL_OR_TK conditional_and_expression
2520 {
2521 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2522 $1, $3);
2523 }
2524| conditional_or_expression BOOL_OR_TK error
2525 {yyerror ("Missing term"); RECOVER;}
2526;
2527
2528conditional_expression: /* Error handling here is weak */
2529 conditional_or_expression
2530| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2531 {
2532 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2533 EXPR_WFL_LINECOL ($$) = $2.location;
2534 }
e04a16fb
AG
2535| conditional_or_expression REL_QM_TK REL_CL_TK error
2536 {
2537 YYERROR_NOW;
2538 yyerror ("Missing term");
2539 DRECOVER (1);
2540 }
2541| conditional_or_expression REL_QM_TK error
2542 {yyerror ("Missing term"); DRECOVER (2);}
2543| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2544 {yyerror ("Missing term"); DRECOVER (3);}
2545;
2546
2547assignment_expression:
2548 conditional_expression
2549| assignment
2550;
2551
2552assignment:
2553 left_hand_side assignment_operator assignment_expression
2554 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2555| left_hand_side assignment_operator error
2556 {
2557 if (ctxp->prevent_ese != lineno)
2558 yyerror ("Missing term");
2559 DRECOVER (assign);
2560 }
2561;
2562
2563left_hand_side:
2564 name
2565| field_access
2566| array_access
2567;
2568
2569assignment_operator:
2570 ASSIGN_ANY_TK
2571| ASSIGN_TK
2572;
2573
2574expression:
2575 assignment_expression
2576;
2577
2578constant_expression:
2579 expression
2580;
2581
2582%%
2583\f
2584
c2952b01
APB
2585/* This section of the code deal with save/restoring parser contexts.
2586 Add mode documentation here. FIXME */
e04a16fb 2587
c2952b01
APB
2588/* Helper function. Create a new parser context. With
2589 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2590 context is copied, otherwise, the new context is zeroed. The newly
2591 created context becomes the current one. */
e04a16fb 2592
c2952b01
APB
2593static void
2594create_new_parser_context (copy_from_previous)
2595 int copy_from_previous;
e04a16fb 2596{
c2952b01 2597 struct parser_ctxt *new;
e04a16fb 2598
c2952b01
APB
2599 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2600 if (copy_from_previous)
2601 {
2602 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2603 new->saved_data_ctx = 1;
2604 }
2605 else
2606 bzero ((PTR) new, sizeof (struct parser_ctxt));
2607
e04a16fb
AG
2608 new->next = ctxp;
2609 ctxp = new;
c2952b01
APB
2610}
2611
2612/* Create a new parser context and make it the current one. */
2613
2614void
2615java_push_parser_context ()
2616{
2617 create_new_parser_context (0);
e04a16fb 2618 if (ctxp->next)
5e942c50
APB
2619 {
2620 ctxp->incomplete_class = ctxp->next->incomplete_class;
2621 ctxp->gclass_list = ctxp->next->gclass_list;
2622 }
e04a16fb
AG
2623}
2624
c2952b01
APB
2625void
2626java_pop_parser_context (generate)
2627 int generate;
2628{
2629 tree current;
2630 struct parser_ctxt *toFree, *next;
2631
2632 if (!ctxp)
2633 return;
2634
2635 toFree = ctxp;
2636 next = ctxp->next;
2637 if (next)
2638 {
2639 next->incomplete_class = ctxp->incomplete_class;
2640 next->gclass_list = ctxp->gclass_list;
2641 lineno = ctxp->lineno;
19e223db 2642 current_class = ctxp->class_type;
c2952b01
APB
2643 }
2644
d19cbcb5
TT
2645 /* If the old and new lexers differ, then free the old one. */
2646 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2647 java_destroy_lexer (ctxp->lexer);
2648
c2952b01
APB
2649 /* Set the single import class file flag to 0 for the current list
2650 of imported things */
2651 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2652 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2653
2654 /* And restore those of the previous context */
2655 if ((ctxp = next)) /* Assignment is really meant here */
2656 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2657 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2658
2659 /* If we pushed a context to parse a class intended to be generated,
2660 we keep it so we can remember the class. What we could actually
2661 do is to just update a list of class names. */
2662 if (generate)
2663 {
2664 toFree->next = ctxp_for_generation;
2665 ctxp_for_generation = toFree;
2666 }
2667 else
2668 free (toFree);
2669}
2670
2671/* Create a parser context for the use of saving some global
2672 variables. */
2673
e04a16fb
AG
2674void
2675java_parser_context_save_global ()
2676{
22eed1e6
APB
2677 if (!ctxp)
2678 {
2679 java_push_parser_context ();
ee07f4f4
APB
2680 ctxp->saved_data_ctx = 1;
2681 }
c2952b01
APB
2682
2683 /* If this context already stores data, create a new one suitable
2684 for data storage. */
ee07f4f4 2685 else if (ctxp->saved_data)
c2952b01
APB
2686 create_new_parser_context (1);
2687
e04a16fb 2688 ctxp->lineno = lineno;
19e223db 2689 ctxp->class_type = current_class;
e04a16fb 2690 ctxp->filename = input_filename;
19e223db 2691 ctxp->function_decl = current_function_decl;
ee07f4f4 2692 ctxp->saved_data = 1;
e04a16fb
AG
2693}
2694
c2952b01
APB
2695/* Restore some global variables from the previous context. Make the
2696 previous context the current one. */
2697
e04a16fb
AG
2698void
2699java_parser_context_restore_global ()
2700{
e04a16fb 2701 lineno = ctxp->lineno;
19e223db 2702 current_class = ctxp->class_type;
e04a16fb 2703 input_filename = ctxp->filename;
19e223db 2704 current_function_decl = ctxp->function_decl;
c2952b01 2705 ctxp->saved_data = 0;
ee07f4f4
APB
2706 if (ctxp->saved_data_ctx)
2707 java_pop_parser_context (0);
e04a16fb
AG
2708}
2709
c2952b01
APB
2710/* Suspend vital data for the current class/function being parsed so
2711 that an other class can be parsed. Used to let local/anonymous
2712 classes be parsed. */
2713
2714static void
2715java_parser_context_suspend ()
e04a16fb 2716{
c2952b01 2717 /* This makes debugging through java_debug_context easier */
3b304f5b 2718 static const char *name = "<inner buffer context>";
e04a16fb 2719
c2952b01
APB
2720 /* Duplicate the previous context, use it to save the globals we're
2721 interested in */
2722 create_new_parser_context (1);
19e223db
MM
2723 ctxp->function_decl = current_function_decl;
2724 ctxp->class_type = current_class;
5e942c50 2725
c2952b01
APB
2726 /* Then create a new context which inherits all data from the
2727 previous one. This will be the new current context */
2728 create_new_parser_context (1);
2729
2730 /* Help debugging */
2731 ctxp->next->filename = name;
2732}
2733
2734/* Resume vital data for the current class/function being parsed so
2735 that an other class can be parsed. Used to let local/anonymous
2736 classes be parsed. The trick is the data storing file position
2737 informations must be restored to their current value, so parsing
2738 can resume as if no context was ever saved. */
2739
2740static void
2741java_parser_context_resume ()
2742{
2743 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2744 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2745 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2746
2747 /* We need to inherit the list of classes to complete/generate */
2748 restored->incomplete_class = old->incomplete_class;
2749 restored->gclass_list = old->gclass_list;
2750 restored->classd_list = old->classd_list;
2751 restored->class_list = old->class_list;
2752
2753 /* Restore the current class and function from the saver */
19e223db
MM
2754 current_class = saver->class_type;
2755 current_function_decl = saver->function_decl;
c2952b01
APB
2756
2757 /* Retrive the restored context */
2758 ctxp = restored;
2759
2760 /* Re-installed the data for the parsing to carry on */
2761 bcopy (&old->marker_begining, &ctxp->marker_begining,
2762 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2763
2764 /* Buffer context can now be discarded */
2765 free (saver);
2766 free (old);
2767}
2768
2769/* Add a new anchor node to which all statement(s) initializing static
2770 and non static initialized upon declaration field(s) will be
2771 linked. */
2772
2773static void
2774java_parser_context_push_initialized_field ()
2775{
2776 tree node;
2777
2778 node = build_tree_list (NULL_TREE, NULL_TREE);
2779 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2780 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2781
2782 node = build_tree_list (NULL_TREE, NULL_TREE);
2783 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2784 CPC_INITIALIZER_LIST (ctxp) = node;
2785
2786 node = build_tree_list (NULL_TREE, NULL_TREE);
2787 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2788 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2789}
2790
2791/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2792 remember them so we can use it to create and populate the finit$
c2952b01
APB
2793 or <clinit> functions. */
2794
2795static void
2796java_parser_context_pop_initialized_field ()
2797{
2798 tree stmts;
2799 tree class_type = TREE_TYPE (GET_CPC ());
2800
2801 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2802 {
c2952b01
APB
2803 stmts = CPC_INITIALIZER_STMT (ctxp);
2804 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2805 if (stmts && !java_error_count)
2806 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2807 }
2808
c2952b01
APB
2809 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2810 {
2811 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2812 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2813 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2814 /* Keep initialization in order to enforce 8.5 */
2815 if (stmts && !java_error_count)
2816 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2817 }
e04a16fb 2818
c2952b01
APB
2819 /* JDK 1.1 instance initializers */
2820 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2821 {
c2952b01
APB
2822 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2823 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2824 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2825 if (stmts && !java_error_count)
2826 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2827 }
c2952b01
APB
2828}
2829
2830static tree
2831reorder_static_initialized (list)
2832 tree list;
2833{
2834 /* We have to keep things in order. The alias initializer have to
2835 come first, then the initialized regular field, in reverse to
2836 keep them in lexical order. */
2837 tree marker, previous = NULL_TREE;
2838 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2839 if (TREE_CODE (marker) == TREE_LIST
2840 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2841 break;
2842
2843 /* No static initialized, the list is fine as is */
2844 if (!previous)
2845 list = TREE_CHAIN (marker);
2846
2847 /* No marker? reverse the whole list */
2848 else if (!marker)
2849 list = nreverse (list);
2850
2851 /* Otherwise, reverse what's after the marker and the new reordered
2852 sublist will replace the marker. */
b351b287 2853 else
c2952b01
APB
2854 {
2855 TREE_CHAIN (previous) = NULL_TREE;
2856 list = nreverse (list);
2857 list = chainon (TREE_CHAIN (marker), list);
2858 }
2859 return list;
e04a16fb
AG
2860}
2861
c2952b01
APB
2862/* Helper functions to dump the parser context stack. */
2863
2864#define TAB_CONTEXT(C) \
2865 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2866
2867static void
2868java_debug_context_do (tab)
2869 int tab;
2870{
ee07f4f4
APB
2871 struct parser_ctxt *copy = ctxp;
2872 while (copy)
2873 {
c2952b01 2874 TAB_CONTEXT (tab);
ee07f4f4 2875 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2876 TAB_CONTEXT (tab);
ee07f4f4 2877 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2878 TAB_CONTEXT (tab);
2879 fprintf (stderr, "lineno: %d\n", copy->lineno);
2880 TAB_CONTEXT (tab);
ee07f4f4
APB
2881 fprintf (stderr, "package: %s\n",
2882 (copy->package ?
2883 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2884 TAB_CONTEXT (tab);
ee07f4f4 2885 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2886 TAB_CONTEXT (tab);
ee07f4f4
APB
2887 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2888 copy = copy->next;
2889 tab += 2;
2890 }
ee07f4f4
APB
2891}
2892
c2952b01
APB
2893/* Dump the stacked up parser contexts. Intended to be called from a
2894 debugger. */
2895
ee07f4f4
APB
2896void
2897java_debug_context ()
2898{
2899 java_debug_context_do (0);
2900}
2901
c2952b01
APB
2902\f
2903
2904/* Flag for the error report routine to issue the error the first time
2905 it's called (overriding the default behavior which is to drop the
2906 first invocation and honor the second one, taking advantage of a
2907 richer context. */
2908static int force_error = 0;
ee07f4f4 2909
8119c720
APB
2910/* Reporting an constructor invocation error. */
2911static void
2912parse_ctor_invocation_error ()
2913{
2914 if (DECL_CONSTRUCTOR_P (current_function_decl))
2915 yyerror ("Constructor invocation must be first thing in a constructor");
2916 else
2917 yyerror ("Only constructors can invoke constructors");
2918}
2919
2920/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2921
2922static tree
2923parse_jdk1_1_error (msg)
49f48c71 2924 const char *msg;
b67d701b
PB
2925{
2926 sorry (": `%s' JDK1.1(TM) feature", msg);
2927 java_error_count++;
9bbc7d9f 2928 return empty_stmt_node;
b67d701b
PB
2929}
2930
e04a16fb
AG
2931static int do_warning = 0;
2932
2933void
2934yyerror (msg)
49f48c71 2935 const char *msg;
e04a16fb
AG
2936{
2937 static java_lc elc;
2938 static int prev_lineno;
49f48c71 2939 static const char *prev_msg;
e04a16fb 2940
0a2138e2 2941 int save_lineno;
e04a16fb
AG
2942 char *remainder, *code_from_source;
2943 extern struct obstack temporary_obstack;
2944
2945 if (!force_error && prev_lineno == lineno)
2946 return;
2947
2948 /* Save current error location but report latter, when the context is
2949 richer. */
2950 if (ctxp->java_error_flag == 0)
2951 {
2952 ctxp->java_error_flag = 1;
2953 elc = ctxp->elc;
2954 /* Do something to use the previous line if we're reaching the
2955 end of the file... */
2956#ifdef VERBOSE_SKELETON
2957 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2958#endif
2959 return;
2960 }
2961
2962 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2963 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2964 return;
2965
2966 ctxp->java_error_flag = 0;
2967 if (do_warning)
2968 java_warning_count++;
2969 else
2970 java_error_count++;
2971
807bc1db 2972 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2973 {
2974 elc.col = ctxp->p_line->char_col-1;
2975 elc.line = ctxp->p_line->lineno;
2976 }
2977
2978 save_lineno = lineno;
2979 prev_lineno = lineno = elc.line;
2980 prev_msg = msg;
2981
2982 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2983 obstack_grow0 (&temporary_obstack,
2984 code_from_source, strlen (code_from_source));
2985 remainder = obstack_finish (&temporary_obstack);
2986 if (do_warning)
2987 warning ("%s.\n%s", msg, remainder);
2988 else
2989 error ("%s.\n%s", msg, remainder);
2990
2991 /* This allow us to cheaply avoid an extra 'Invalid expression
2992 statement' error report when errors have been already reported on
2993 the same line. This occurs when we report an error but don't have
2994 a synchronization point other than ';', which
2995 expression_statement is the only one to take care of. */
2996 ctxp->prevent_ese = lineno = save_lineno;
2997}
2998
2999static void
15fdcfe9 3000issue_warning_error_from_context (cl, msg, ap)
5e942c50 3001 tree cl;
d4476be2 3002 const char *msg;
15fdcfe9 3003 va_list ap;
5e942c50 3004{
3b304f5b 3005 const char *saved, *saved_input_filename;
15fdcfe9
PB
3006 char buffer [4096];
3007 vsprintf (buffer, msg, ap);
3008 force_error = 1;
5e942c50
APB
3009
3010 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3011 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3012 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3013
3014 /* We have a CL, that's a good reason for using it if it contains data */
3015 saved = ctxp->filename;
3016 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3017 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3018 saved_input_filename = input_filename;
3019 input_filename = ctxp->filename;
15fdcfe9
PB
3020 java_error (NULL);
3021 java_error (buffer);
5e942c50 3022 ctxp->filename = saved;
1886c9d8 3023 input_filename = saved_input_filename;
15fdcfe9 3024 force_error = 0;
5e942c50
APB
3025}
3026
e04a16fb
AG
3027/* Issue an error message at a current source line CL */
3028
15fdcfe9 3029void
df32d2ce 3030parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3031{
d4476be2 3032#ifndef ANSI_PROTOTYPES
e04a16fb 3033 tree cl;
d4476be2 3034 const char *msg;
e04a16fb 3035#endif
e04a16fb
AG
3036 va_list ap;
3037
3038 VA_START (ap, msg);
d4476be2 3039#ifndef ANSI_PROTOTYPES
e04a16fb 3040 cl = va_arg (ap, tree);
d4476be2 3041 msg = va_arg (ap, const char *);
e04a16fb 3042#endif
15fdcfe9
PB
3043 issue_warning_error_from_context (cl, msg, ap);
3044 va_end (ap);
e04a16fb
AG
3045}
3046
3047/* Issue a warning at a current source line CL */
3048
3049static void
df32d2ce 3050parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3051{
d4476be2 3052#ifndef ANSI_PROTOTYPES
e04a16fb 3053 tree cl;
d4476be2 3054 const char *msg;
e04a16fb 3055#endif
e04a16fb
AG
3056 va_list ap;
3057
3058 VA_START (ap, msg);
d4476be2 3059#ifndef ANSI_PROTOTYPES
e04a16fb 3060 cl = va_arg (ap, tree);
d4476be2 3061 msg = va_arg (ap, const char *);
e04a16fb 3062#endif
e04a16fb 3063
c877974e 3064 force_error = do_warning = 1;
15fdcfe9 3065 issue_warning_error_from_context (cl, msg, ap);
c877974e 3066 do_warning = force_error = 0;
15fdcfe9 3067 va_end (ap);
e04a16fb
AG
3068}
3069
82371d41
APB
3070static tree
3071find_expr_with_wfl (node)
3072 tree node;
3073{
3074 while (node)
3075 {
3076 char code;
3077 tree to_return;
3078
3079 switch (TREE_CODE (node))
3080 {
3081 case BLOCK:
c0d87ff6
PB
3082 node = BLOCK_EXPR_BODY (node);
3083 continue;
82371d41
APB
3084
3085 case COMPOUND_EXPR:
3086 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3087 if (to_return)
3088 return to_return;
c0d87ff6
PB
3089 node = TREE_OPERAND (node, 1);
3090 continue;
82371d41
APB
3091
3092 case LOOP_EXPR:
c0d87ff6
PB
3093 node = TREE_OPERAND (node, 0);
3094 continue;
82371d41
APB
3095
3096 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3097 node = TREE_OPERAND (node, 1);
3098 continue;
3099
82371d41
APB
3100 default:
3101 code = TREE_CODE_CLASS (TREE_CODE (node));
3102 if (((code == '1') || (code == '2') || (code == 'e'))
3103 && EXPR_WFL_LINECOL (node))
3104 return node;
ba179f9f 3105 return NULL_TREE;
82371d41
APB
3106 }
3107 }
3108 return NULL_TREE;
3109}
3110
3111/* Issue a missing return statement error. Uses METHOD to figure the
3112 last line of the method the error occurs in. */
3113
3114static void
3115missing_return_error (method)
3116 tree method;
3117{
3118 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3119 parse_error_context (wfl_operator, "Missing return statement");
3120}
3121
3122/* Issue an unreachable statement error. From NODE, find the next
3123 statement to report appropriately. */
3124static void
3125unreachable_stmt_error (node)
3126 tree node;
3127{
3128 /* Browse node to find the next expression node that has a WFL. Use
3129 the location to report the error */
3130 if (TREE_CODE (node) == COMPOUND_EXPR)
3131 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3132 else
3133 node = find_expr_with_wfl (node);
3134
3135 if (node)
3136 {
3137 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3138 parse_error_context (wfl_operator, "Unreachable statement");
3139 }
3140 else
3141 fatal ("Can't get valid statement - unreachable_stmt_error");
3142}
3143
c877974e 3144int
e04a16fb
AG
3145java_report_errors ()
3146{
3147 if (java_error_count)
3148 fprintf (stderr, "%d error%s",
3149 java_error_count, (java_error_count == 1 ? "" : "s"));
3150 if (java_warning_count)
3151 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3152 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3153 if (java_error_count || java_warning_count)
3154 putc ('\n', stderr);
c877974e 3155 return java_error_count;
e04a16fb
AG
3156}
3157
3158static char *
3159java_accstring_lookup (flags)
3160 int flags;
3161{
3162 static char buffer [80];
3163#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3164
3165 /* Access modifier looked-up first for easier report on forbidden
3166 access. */
3167 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3168 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3169 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3170 if (flags & ACC_STATIC) COPY_RETURN ("static");
3171 if (flags & ACC_FINAL) COPY_RETURN ("final");
3172 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3173 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3174 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3175 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3176 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3177 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3178
3179 buffer [0] = '\0';
3180 return buffer;
3181#undef COPY_RETURN
3182}
3183
b67d701b
PB
3184/* Issuing error messages upon redefinition of classes, interfaces or
3185 variables. */
3186
e04a16fb 3187static void
b67d701b 3188classitf_redefinition_error (context, id, decl, cl)
49f48c71 3189 const char *context;
e04a16fb
AG
3190 tree id, decl, cl;
3191{
3192 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3193 context, IDENTIFIER_POINTER (id),
3194 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3195 /* Here we should point out where its redefined. It's a unicode. FIXME */
3196}
3197
b67d701b
PB
3198static void
3199variable_redefinition_error (context, name, type, line)
3200 tree context, name, type;
3201 int line;
3202{
49f48c71 3203 const char *type_name;
b67d701b
PB
3204
3205 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3206 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3207 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3208 else
0a2138e2 3209 type_name = lang_printable_name (type, 0);
b67d701b
PB
3210
3211 parse_error_context (context,
781b0558 3212 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3213 IDENTIFIER_POINTER (name),
3214 type_name, IDENTIFIER_POINTER (name), line);
3215}
3216
c583dd46
APB
3217static tree
3218build_array_from_name (type, type_wfl, name, ret_name)
3219 tree type, type_wfl, name, *ret_name;
3220{
3221 int more_dims = 0;
49f48c71 3222 const char *string;
c583dd46
APB
3223
3224 /* Eventually get more dims */
3225 string = IDENTIFIER_POINTER (name);
3226 while (string [more_dims] == '[')
3227 more_dims++;
3228
3229 /* If we have, then craft a new type for this variable */
3230 if (more_dims)
3231 {
c0d87ff6 3232 name = get_identifier (&string [more_dims]);
c583dd46 3233
34f4db93
APB
3234 /* If we have a pointer, use its type */
3235 if (TREE_CODE (type) == POINTER_TYPE)
3236 type = TREE_TYPE (type);
c583dd46
APB
3237
3238 /* Building the first dimension of a primitive type uses this
3239 function */
3240 if (JPRIMITIVE_TYPE_P (type))
3241 {
3242 type = build_java_array_type (type, -1);
22eed1e6 3243 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3244 more_dims--;
3245 }
3246 /* Otherwise, if we have a WFL for this type, use it (the type
3247 is already an array on an unresolved type, and we just keep
3248 on adding dimensions) */
3249 else if (type_wfl)
3250 type = type_wfl;
3251
3252 /* Add all the dimensions */
3253 while (more_dims--)
3254 type = build_unresolved_array_type (type);
3255
3256 /* The type may have been incomplete in the first place */
3257 if (type_wfl)
3258 type = obtain_incomplete_type (type);
3259 }
3260
c2952b01
APB
3261 if (ret_name)
3262 *ret_name = name;
c583dd46
APB
3263 return type;
3264}
3265
e04a16fb
AG
3266/* Build something that the type identifier resolver will identify as
3267 being an array to an unresolved type. TYPE_WFL is a WFL on a
3268 identifier. */
3269
3270static tree
3271build_unresolved_array_type (type_or_wfl)
3272 tree type_or_wfl;
3273{
49f48c71 3274 const char *ptr;
e04a16fb 3275
1886c9d8 3276 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3277 just create a array type */
3278 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3279 {
3280 tree type = build_java_array_type (type_or_wfl, -1);
3281 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3282 return type;
3283 }
3284
3285 obstack_1grow (&temporary_obstack, '[');
3286 obstack_grow0 (&temporary_obstack,
3287 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3288 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3289 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3290 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3291 return type_or_wfl;
e04a16fb
AG
3292}
3293
e04a16fb
AG
3294static void
3295parser_add_interface (class_decl, interface_decl, wfl)
3296 tree class_decl, interface_decl, wfl;
3297{
3298 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3299 parse_error_context (wfl, "Interface `%s' repeated",
3300 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3301}
3302
3303/* Bulk of common class/interface checks. Return 1 if an error was
3304 encountered. TAG is 0 for a class, 1 for an interface. */
3305
3306static int
3307check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3308 int is_interface, flags;
3309 tree raw_name, qualified_name, decl, cl;
3310{
3311 tree node;
c2952b01
APB
3312 int sca = 0; /* Static class allowed */
3313 int icaf = 0; /* Inner class allowed flags */
3314 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3315
3316 if (!quiet_flag)
c2952b01
APB
3317 fprintf (stderr, " %s%s %s",
3318 (CPC_INNER_P () ? "inner" : ""),
3319 (is_interface ? "interface" : "class"),
e04a16fb
AG
3320 IDENTIFIER_POINTER (qualified_name));
3321
3322 /* Scope of an interface/class type name:
3323 - Can't be imported by a single type import
3324 - Can't already exists in the package */
3325 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3326 && (node = find_name_in_single_imports (raw_name))
3327 && !CPC_INNER_P ())
e04a16fb
AG
3328 {
3329 parse_error_context
3330 (cl, "%s name `%s' clashes with imported type `%s'",
3331 (is_interface ? "Interface" : "Class"),
3332 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3333 return 1;
3334 }
3335 if (decl && CLASS_COMPLETE_P (decl))
3336 {
b67d701b
PB
3337 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3338 qualified_name, decl, cl);
e04a16fb
AG
3339 return 1;
3340 }
3341
c2952b01
APB
3342 if (check_inner_class_redefinition (raw_name, cl))
3343 return 1;
3344
3345 /* If public, file name should match class/interface name, except
3346 when dealing with an inner class */
3347 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3348 {
49f48c71 3349 const char *f;
e04a16fb
AG
3350
3351 /* Contains OS dependent assumption on path separator. FIXME */
3352 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3353 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3354 f--)
3355 ;
847fe791 3356 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3357 f++;
3358 if (strncmp (IDENTIFIER_POINTER (raw_name),
3359 f , IDENTIFIER_LENGTH (raw_name)) ||
3360 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3361 parse_error_context
3362 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3363 (is_interface ? "interface" : "class"),
3364 IDENTIFIER_POINTER (qualified_name),
3365 IDENTIFIER_POINTER (raw_name));
3366 }
3367
c2952b01
APB
3368 /* Static classes can be declared only in top level classes. Note:
3369 once static, a inner class is a top level class. */
3370 if (flags & ACC_STATIC)
3371 {
3372 /* Catch the specific error of declaring an class inner class
3373 with no toplevel enclosing class. Prevent check_modifiers from
3374 complaining a second time */
3375 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3376 {
3377 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3378 IDENTIFIER_POINTER (qualified_name));
3379 sca = ACC_STATIC;
3380 }
3381 /* Else, in the context of a top-level class declaration, let
3382 `check_modifiers' do its job, otherwise, give it a go */
3383 else
3384 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3385 }
3386
a40d21da 3387 /* Inner classes can be declared private or protected
c2952b01
APB
3388 within their enclosing classes. */
3389 if (CPC_INNER_P ())
3390 {
3391 /* A class which is local to a block can't be public, private,
3392 protected or static. But it is created final, so allow this
3393 one. */
3394 if (current_function_decl)
3395 icaf = sca = uaaf = ACC_FINAL;
3396 else
3397 {
3398 check_modifiers_consistency (flags);
3399 icaf = ACC_PRIVATE|ACC_PROTECTED;
3400 }
3401 }
3402
a40d21da
APB
3403 if (is_interface)
3404 {
3405 if (CPC_INNER_P ())
3406 uaaf = INTERFACE_INNER_MODIFIERS;
3407 else
3408 uaaf = INTERFACE_MODIFIERS;
3409
3410 check_modifiers ("Illegal modifier `%s' for interface declaration",
3411 flags, uaaf);
3412 }
2884c41e 3413 else
a40d21da
APB
3414 check_modifiers ((current_function_decl ?
3415 "Illegal modifier `%s' for local class declaration" :
3416 "Illegal modifier `%s' for class declaration"),
c2952b01 3417 flags, uaaf|sca|icaf);
e04a16fb
AG
3418 return 0;
3419}
3420
c2952b01
APB
3421static void
3422make_nested_class_name (cpc_list)
3423 tree cpc_list;
3424{
3425 tree name;
3426
3427 if (!cpc_list)
3428 return;
3429 else
3430 make_nested_class_name (TREE_CHAIN (cpc_list));
3431
3432 /* Pick the qualified name when dealing with the first upmost
3433 enclosing class */
3434 name = (TREE_CHAIN (cpc_list) ?
3435 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3436 obstack_grow (&temporary_obstack,
3437 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3438 /* Why is NO_DOLLAR_IN_LABEL defined? */
3439#if 0
3440#ifdef NO_DOLLAR_IN_LABEL
3441 fatal ("make_nested_class_name: Can't use '$' as a separator "
3442 "for inner classes");
3443#endif
3444#endif
3445 obstack_1grow (&temporary_obstack, '$');
3446}
3447
3448/* Can't redefine a class already defined in an earlier scope. */
3449
3450static int
3451check_inner_class_redefinition (raw_name, cl)
3452 tree raw_name, cl;
3453{
3454 tree scope_list;
3455
3456 for (scope_list = GET_CPC_LIST (); scope_list;
3457 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3458 if (raw_name == GET_CPC_UN_NODE (scope_list))
3459 {
3460 parse_error_context
3461 (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",
3462 IDENTIFIER_POINTER (raw_name));
3463 return 1;
3464 }
3465 return 0;
3466}
3467
3468static tree
3469find_as_inner_class (enclosing, name, cl)
3470 tree enclosing, name, cl;
3471{
3472 tree qual, to_return;
3473 if (!enclosing)
3474 return NULL_TREE;
3475
3476 name = TYPE_NAME (name);
3477
3478 /* First search: within the scope of `enclosing', search for name */
3479 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3480 qual = EXPR_WFL_QUALIFICATION (cl);
3481 else if (cl)
3482 qual = build_tree_list (cl, NULL_TREE);
3483 else
3484 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3485
3486 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3487 return to_return;
3488
3489 /* We're dealing with a qualified name. Try to resolve thing until
3490 we get something that is an enclosing class. */
3491 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3492 {
3493 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3494
0c2b8145
APB
3495 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3496 qual = TREE_CHAIN (qual))
c2952b01
APB
3497 {
3498 acc = merge_qualified_name (acc,
3499 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3500 BUILD_PTR_FROM_NAME (ptr, acc);
3501 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3502 }
3503
3504 /* A NULL qual and a decl means that the search ended
3505 successfully?!? We have to do something then. FIXME */
3506
3507 if (decl)
3508 enclosing = decl;
3509 else
3510 qual = EXPR_WFL_QUALIFICATION (cl);
3511 }
3512 /* Otherwise, create a qual for the other part of the resolution. */
3513 else
3514 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3515
1e12ab9b 3516 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3517}
3518
3519/* We go inside the list of sub classes and try to find a way
3520 through. */
3521
3522static tree
3523find_as_inner_class_do (qual, enclosing)
3524 tree qual, enclosing;
3525{
3526 if (!qual)
3527 return NULL_TREE;
3528
3529 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3530 {
3531 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3532 tree next_enclosing = NULL_TREE;
3533 tree inner_list;
3534
3535 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3536 inner_list; inner_list = TREE_CHAIN (inner_list))
3537 {
3538 if (TREE_VALUE (inner_list) == name_to_match)
3539 {
3540 next_enclosing = TREE_PURPOSE (inner_list);
3541 break;
3542 }
3543 }
3544 enclosing = next_enclosing;
3545 }
3546
3547 return (!qual && enclosing ? enclosing : NULL_TREE);
3548}
3549
3550/* Reach all inner classes and tie their unqualified name to a
3551 DECL. */
3552
3553static void
3554set_nested_class_simple_name_value (outer, set)
3555 tree outer;
3556 int set;
3557{
3558 tree l;
3559
3560 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3561 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3562 TREE_PURPOSE (l) : NULL_TREE);
3563}
3564
3565static void
3566link_nested_class_to_enclosing ()
3567{
3568 if (GET_ENCLOSING_CPC ())
3569 {
3570 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3571 DECL_INNER_CLASS_LIST (enclosing) =
3572 tree_cons (GET_CPC (), GET_CPC_UN (),
3573 DECL_INNER_CLASS_LIST (enclosing));
3574 enclosing = enclosing;
3575 }
3576}
3577
3578static tree
3579maybe_make_nested_class_name (name)
3580 tree name;
3581{
3582 tree id = NULL_TREE;
3583
3584 if (CPC_INNER_P ())
3585 {
3586 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3587 obstack_grow0 (&temporary_obstack,
3588 IDENTIFIER_POINTER (name),
3589 IDENTIFIER_LENGTH (name));
c2952b01
APB
3590 id = get_identifier (obstack_finish (&temporary_obstack));
3591 if (ctxp->package)
3592 QUALIFIED_P (id) = 1;
3593 }
3594 return id;
3595}
3596
3597/* If DECL is NULL, create and push a new DECL, record the current
3598 line CL and do other maintenance things. */
3599
e04a16fb 3600static tree
c2952b01
APB
3601maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3602 tree decl, raw_name, qualified_name, cl;
e04a16fb 3603{
5e942c50 3604 if (!decl)
e04a16fb 3605 decl = push_class (make_class (), qualified_name);
c2952b01 3606
e04a16fb
AG
3607 /* Take care of the file and line business */
3608 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3609 /* If we're emiting xrefs, store the line/col number information */
3610 if (flag_emit_xref)
3611 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3612 else
3613 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3614 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3615 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3616 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3617
c2952b01
APB
3618 PUSH_CPC (decl, raw_name);
3619 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3620
e04a16fb
AG
3621 /* Link the declaration to the already seen ones */
3622 TREE_CHAIN (decl) = ctxp->class_list;
3623 ctxp->class_list = decl;
5e942c50 3624
23a79c61 3625 /* Create a new nodes in the global lists */
5e942c50 3626 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3627 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3628
e04a16fb
AG
3629 /* Install a new dependency list element */
3630 create_jdep_list (ctxp);
3631
3632 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3633 IDENTIFIER_POINTER (qualified_name)));
3634 return decl;
3635}
3636
3637static void
3638add_superinterfaces (decl, interface_list)
3639 tree decl, interface_list;
3640{
3641 tree node;
3642 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3643 takes care of ensuring that:
3644 - This is an accessible interface type,
3645 - Circularity detection.
3646 parser_add_interface is then called. If present but not defined,
3647 the check operation is delayed until the super interface gets
3648 defined. */
3649 for (node = interface_list; node; node = TREE_CHAIN (node))
3650 {
15fdcfe9 3651 tree current = TREE_PURPOSE (node);
5e942c50
APB
3652 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3653 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3654 {
5e942c50
APB
3655 if (!parser_check_super_interface (idecl, decl, current))
3656 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3657 }
3658 else
3659 register_incomplete_type (JDEP_INTERFACE,
3660 current, decl, NULL_TREE);
3661 }
3662}
3663
3664/* Create an interface in pass1 and return its decl. Return the
3665 interface's decl in pass 2. */
3666
3667static tree
3668create_interface (flags, id, super)
3669 int flags;
3670 tree id, super;
3671{
e04a16fb 3672 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3673 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3674 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3675
3676 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3677
3678 /* Basic checks: scope, redefinition, modifiers */
3679 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3680 {
3681 PUSH_ERROR ();
3682 return NULL_TREE;
3683 }
3684
3685 /* Suspend the current parsing context if we're parsing an inner
3686 interface */
3687 if (CPC_INNER_P ())
3688 java_parser_context_suspend ();
3689
3690 /* Push a new context for (static) initialized upon declaration fields */
3691 java_parser_context_push_initialized_field ();
e04a16fb
AG
3692
3693 /* Interface modifiers check
3694 - public/abstract allowed (already done at that point)
3695 - abstract is obsolete (comes first, it's a warning, or should be)
3696 - Can't use twice the same (checked in the modifier rule) */
c877974e 3697 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3698 parse_warning_context
3699 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3700 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3701
3702 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3703 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3704
3705 /* Set super info and mark the class a complete */
2aa11e97 3706 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3707 object_type_node, ctxp->interface_number);
3708 ctxp->interface_number = 0;
3709 CLASS_COMPLETE_P (decl) = 1;
3710 add_superinterfaces (decl, super);
3711
3712 return decl;
3713}
3714
c2952b01
APB
3715/* Anonymous class counter. Will be reset to 1 every time a non
3716 anonymous class gets created. */
3717static int anonymous_class_counter = 1;
3718
3719/* Patch anonymous class CLASS, by either extending or implementing
3720 DEP. */
3721
3722static void
3723patch_anonymous_class (type_decl, class_decl, wfl)
3724 tree type_decl, class_decl, wfl;
3725{
3726 tree class = TREE_TYPE (class_decl);
3727 tree type = TREE_TYPE (type_decl);
3728 tree binfo = TYPE_BINFO (class);
3729
3730 /* If it's an interface, implement it */
3731 if (CLASS_INTERFACE (type_decl))
3732 {
3733 tree s_binfo;
3734 int length;
3735
3736 if (parser_check_super_interface (type_decl, class_decl, wfl))
3737 return;
3738
3739 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3740 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3741 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3742 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3743 /* And add the interface */
3744 parser_add_interface (class_decl, type_decl, wfl);
3745 }
3746 /* Otherwise, it's a type we want to extend */
3747 else
3748 {
3749 if (parser_check_super (type_decl, class_decl, wfl))
3750 return;
3751 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3752 }
3753}
3754
3755static tree
3756create_anonymous_class (location, type_name)
3757 int location;
3758 tree type_name;
3759{
3760 char buffer [80];
3761 tree super = NULL_TREE, itf = NULL_TREE;
3762 tree id, type_decl, class;
3763
3764 /* The unqualified name of the anonymous class. It's just a number. */
3765 sprintf (buffer, "%d", anonymous_class_counter++);
3766 id = build_wfl_node (get_identifier (buffer));
3767 EXPR_WFL_LINECOL (id) = location;
3768
3769 /* We know about the type to extend/implement. We go ahead */
3770 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3771 {
3772 /* Create a class which either implements on extends the designated
3773 class. The class bears an innacessible name. */
3774 if (CLASS_INTERFACE (type_decl))
3775 {
3776 /* It's OK to modify it here. It's been already used and
3777 shouldn't be reused */
3778 ctxp->interface_number = 1;
3779 /* Interfaces should presented as a list of WFLs */
3780 itf = build_tree_list (type_name, NULL_TREE);
3781 }
3782 else
3783 super = type_name;
3784 }
3785
3786 class = create_class (ACC_FINAL, id, super, itf);
3787
3788 /* We didn't know anything about the stuff. We register a dependence. */
3789 if (!type_decl)
3790 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3791
3792 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3793 return class;
3794}
3795
a40d21da 3796/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3797 interface's decl in pass 2. */
3798
3799static tree
3800create_class (flags, id, super, interfaces)
3801 int flags;
3802 tree id, super, interfaces;
3803{
e04a16fb
AG
3804 tree raw_name = EXPR_WFL_NODE (id);
3805 tree class_id, decl;
9ee9b555 3806 tree super_decl_type;
e04a16fb 3807
98a52c2c 3808 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3809 decl = IDENTIFIER_CLASS_VALUE (class_id);
3810 EXPR_WFL_NODE (id) = class_id;
3811
3812 /* Basic check: scope, redefinition, modifiers */
3813 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3814 {
3815 PUSH_ERROR ();
3816 return NULL_TREE;
3817 }
3818
3819 /* Suspend the current parsing context if we're parsing an inner
3820 class or an anonymous class. */
3821 if (CPC_INNER_P ())
3822 java_parser_context_suspend ();
3823 /* Push a new context for (static) initialized upon declaration fields */
3824 java_parser_context_push_initialized_field ();
e04a16fb
AG
3825
3826 /* Class modifier check:
3827 - Allowed modifier (already done at that point)
3828 - abstract AND final forbidden
3829 - Public classes defined in the correct file */
3830 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3831 parse_error_context
3832 (id, "Class `%s' can't be declared both abstract and final",
3833 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3834
3835 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3836 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3837
3838 /* If SUPER exists, use it, otherwise use Object */
3839 if (super)
3840 {
3841 /* Can't extend java.lang.Object */
3842 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3843 {
3844 parse_error_context (id, "Can't extend `java.lang.Object'");
3845 return NULL_TREE;
3846 }
3847
2c3199bc
PB
3848 super_decl_type =
3849 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3850 }
3851 else if (TREE_TYPE (decl) != object_type_node)
3852 super_decl_type = object_type_node;
3853 /* We're defining java.lang.Object */
3854 else
3855 super_decl_type = NULL_TREE;
3856
3857 /* Set super info and mark the class a complete */
3858 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3859 ctxp->interface_number);
3860 ctxp->interface_number = 0;
3861 CLASS_COMPLETE_P (decl) = 1;
3862 add_superinterfaces (decl, interfaces);
3863
c2952b01
APB
3864 /* Add the private this$<n> field, Replicate final locals still in
3865 scope as private final fields mangled like val$<local_name>.
3866 This doesn't not occur for top level (static) inner classes. */
3867 if (PURE_INNER_CLASS_DECL_P (decl))
3868 add_inner_class_fields (decl, current_function_decl);
3869
7f10c2e2
APB
3870 /* If doing xref, store the location at which the inherited class
3871 (if any) was seen. */
3872 if (flag_emit_xref && super)
3873 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3874
5e942c50
APB
3875 /* Eventually sets the @deprecated tag flag */
3876 CHECK_DEPRECATED (decl);
3877
165f37bc
APB
3878 /* Reset the anonymous class counter when declaring non inner classes */
3879 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3880 anonymous_class_counter = 1;
3881
e04a16fb
AG
3882 return decl;
3883}
3884
c2952b01 3885/* End a class declaration: register the statements used to create
c00f0fb2 3886 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3887 parser context if necessary. */
3888
3889static void
3890end_class_declaration (resume)
3891 int resume;
3892{
3893 /* If an error occured, context weren't pushed and won't need to be
3894 popped by a resume. */
3895 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3896
3897 java_parser_context_pop_initialized_field ();
3898 POP_CPC ();
3899 if (resume && no_error_occured)
3900 java_parser_context_resume ();
93220702
APB
3901
3902 /* We're ending a class declaration, this is a good time to reset
3903 the interface cout. Note that might have been already done in
3904 create_interface, but if at that time an inner class was being
3905 dealt with, the interface count was reset in a context created
3906 for the sake of handling inner classes declaration. */
3907 ctxp->interface_number = 0;
c2952b01
APB
3908}
3909
3910static void
3911add_inner_class_fields (class_decl, fct_decl)
3912 tree class_decl;
3913 tree fct_decl;
3914{
3915 tree block, marker, f;
3916
3917 f = add_field (TREE_TYPE (class_decl),
3918 build_current_thisn (TREE_TYPE (class_decl)),
3919 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3920 ACC_PRIVATE);
3921 FIELD_THISN (f) = 1;
3922
3923 if (!fct_decl)
3924 return;
3925
3926 for (block = GET_CURRENT_BLOCK (fct_decl);
3927 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3928 {
3929 tree decl;
3930 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3931 {
3932 char *name, *pname;
3933 tree wfl, init, list;
3934
3935 /* Avoid non final arguments. */
3936 if (!LOCAL_FINAL (decl))
3937 continue;
3938
3939 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3940 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3941 wfl = build_wfl_node (get_identifier (name));
3942 init = build_wfl_node (get_identifier (pname));
3943 /* Build an initialization for the field: it will be
c00f0fb2 3944 initialized by a parameter added to finit$, bearing a
c2952b01 3945 mangled name of the field itself (param$<n>.) The
c00f0fb2 3946 parameter is provided to finit$ by the constructor
c2952b01
APB
3947 invoking it (hence the constructor will also feature a
3948 hidden parameter, set to the value of the outer context
3949 local at the time the inner class is created.)
3950
3951 Note: we take into account all possible locals that can
3952 be accessed by the inner class. It's actually not trivial
3953 to minimize these aliases down to the ones really
3954 used. One way to do that would be to expand all regular
c00f0fb2 3955 methods first, then finit$ to get a picture of what's
c2952b01
APB
3956 used. It works with the exception that we would have to
3957 go back on all constructor invoked in regular methods to
3958 have their invokation reworked (to include the right amount
3959 of alias initializer parameters.)
3960
3961 The only real way around, I think, is a first pass to
3962 identify locals really used in the inner class. We leave
3963 the flag FIELD_LOCAL_ALIAS_USED around for that future
3964 use.
3965
3966 On the other hand, it only affect local inner classes,
c00f0fb2 3967 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3968 unecessary arguments. It's easy for a developper to keep
3969 this number of parameter down by using the `final'
3970 keyword only when necessary. For the time being, we can
3971 issue a warning on unecessary finals. FIXME */
3972 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3973 wfl, init);
3974
3975 /* Register the field. The TREE_LIST holding the part
3976 initialized/initializer will be marked ARG_FINAL_P so
3977 that the created field can be marked
3978 FIELD_LOCAL_ALIAS. */
3979 list = build_tree_list (wfl, init);
3980 ARG_FINAL_P (list) = 1;
3981 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3982 }
3983 }
3984
3985 if (!CPC_INITIALIZER_STMT (ctxp))
3986 return;
3987
3988 /* If we ever registered an alias field, insert and marker to
3989 remeber where the list ends. The second part of the list (the one
3990 featuring initialized fields) so it can be later reversed to
3991 enforce 8.5. The marker will be removed during that operation. */
3992 marker = build_tree_list (NULL_TREE, NULL_TREE);
3993 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3994 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3995}
3996
e04a16fb
AG
3997/* Can't use lookup_field () since we don't want to load the class and
3998 can't set the CLASS_LOADED_P flag */
3999
4000static tree
4001find_field (class, name)
4002 tree class;
4003 tree name;
4004{
4005 tree decl;
4006 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4007 {
4008 if (DECL_NAME (decl) == name)
4009 return decl;
4010 }
4011 return NULL_TREE;
4012}
4013
4014/* Wrap around lookup_field that doesn't potentially upset the value
4015 of CLASS */
4016
4017static tree
4018lookup_field_wrapper (class, name)
4019 tree class, name;
4020{
4021 tree type = class;
9a7ab4b3 4022 tree decl = NULL_TREE;
c877974e 4023 java_parser_context_save_global ();
f2760b27
APB
4024
4025 /* Last chance: if we're within the context of an inner class, we
4026 might be trying to access a local variable defined in an outer
4027 context. We try to look for it now. */
9a7ab4b3 4028 if (INNER_CLASS_TYPE_P (class))
f2760b27
APB
4029 {
4030 char *alias_buffer;
9a7ab4b3 4031 tree new_name;
f2760b27 4032 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
9a7ab4b3
APB
4033 new_name = get_identifier (alias_buffer);
4034 decl = lookup_field (&type, new_name);
f2760b27
APB
4035 if (decl && decl != error_mark_node)
4036 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4037 }
9a7ab4b3
APB
4038 if (!decl || decl == error_mark_node)
4039 {
4040 type = class;
4041 decl = lookup_field (&type, name);
4042 }
f2760b27 4043
c877974e 4044 java_parser_context_restore_global ();
93024893 4045 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4046}
4047
4048/* Find duplicate field within the same class declarations and report
c583dd46
APB
4049 the error. Returns 1 if a duplicated field was found, 0
4050 otherwise. */
e04a16fb
AG
4051
4052static int
c583dd46 4053duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4054 tree new_field_name, new_type, cl;
e04a16fb
AG
4055{
4056 /* This might be modified to work with method decl as well */
c2952b01 4057 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4058 if (decl)
4059 {
c2e3db92 4060 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4061 ((TREE_CODE (new_type) == POINTER_TYPE
4062 && TREE_TYPE (new_type) == NULL_TREE) ?
4063 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4064 lang_printable_name (new_type, 1)));
c877974e
APB
4065 /* The type may not have been completed by the time we report
4066 the error */
c2e3db92 4067 char *t2 = xstrdup (purify_type_name
4a5f66c3 4068 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4069 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4070 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4071 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4072 parse_error_context
4073 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4074 t1, IDENTIFIER_POINTER (new_field_name),
4075 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4076 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4077 free (t1);
4078 free (t2);
c583dd46 4079 return 1;
e04a16fb 4080 }
c583dd46 4081 return 0;
e04a16fb
AG
4082}
4083
4084/* Field registration routine. If TYPE doesn't exist, field
4085 declarations are linked to the undefined TYPE dependency list, to
4086 be later resolved in java_complete_class () */
4087
4088static void
4089register_fields (flags, type, variable_list)
4090 int flags;
4091 tree type, variable_list;
4092{
c583dd46 4093 tree current, saved_type;
c2952b01 4094 tree class_type = NULL_TREE;
e04a16fb
AG
4095 int saved_lineno = lineno;
4096 int must_chain = 0;
4097 tree wfl = NULL_TREE;
4098
c2952b01
APB
4099 if (GET_CPC ())
4100 class_type = TREE_TYPE (GET_CPC ());
4101
4102 if (!class_type || class_type == error_mark_node)
4103 return;
4104
e04a16fb
AG
4105 /* If we're adding fields to interfaces, those fields are public,
4106 static, final */
4107 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4108 {
4109 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4110 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4111 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4112 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4113 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4114 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4115 check_modifiers ("Illegal interface member modifier `%s'", flags,
4116 INTERFACE_FIELD_MODIFIERS);
4117 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4118 }
4119
c583dd46
APB
4120 /* Obtain a suitable type for resolution, if necessary */
4121 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4122
4123 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4124 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4125
c583dd46
APB
4126 for (current = variable_list, saved_type = type; current;
4127 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4128 {
c877974e 4129 tree real_type;
c583dd46 4130 tree field_decl;
e04a16fb
AG
4131 tree cl = TREE_PURPOSE (current);
4132 tree init = TREE_VALUE (current);
4133 tree current_name = EXPR_WFL_NODE (cl);
4134
cf1748bf 4135 /* Can't declare non-final static fields in inner classes */
c2952b01 4136 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4137 && !(flags & ACC_FINAL))
c2952b01 4138 parse_error_context
cf1748bf 4139 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4140 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4141 lang_printable_name (class_type, 0));
4142
c583dd46
APB
4143 /* Process NAME, as it may specify extra dimension(s) for it */
4144 type = build_array_from_name (type, wfl, current_name, &current_name);
4145
c583dd46
APB
4146 /* Type adjustment. We may have just readjusted TYPE because
4147 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4148 a reference if we can and don't have one already. Also
4149 change the name if we have an init. */
4150 if (type != saved_type)
4151 {
1886c9d8 4152 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4153 if (init)
4154 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4155 }
e04a16fb 4156
c877974e
APB
4157 real_type = GET_REAL_TYPE (type);
4158 /* Check for redeclarations */
4159 if (duplicate_declaration_error_p (current_name, real_type, cl))
4160 continue;
4161
c583dd46 4162 /* Set lineno to the line the field was found and create a
5e942c50 4163 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4164 if (flag_emit_xref)
4165 lineno = EXPR_WFL_LINECOL (cl);
4166 else
4167 lineno = EXPR_WFL_LINENO (cl);
c877974e 4168 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4169 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4170
4171 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4172 mark the created field FIELD_LOCAL_ALIAS, so that we can
c00f0fb2 4173 hide parameters to this inner class finit$ and constructors. */
c2952b01
APB
4174 if (ARG_FINAL_P (current))
4175 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4176
4177 /* Check if we must chain. */
4178 if (must_chain)
4179 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4180
c583dd46
APB
4181 /* If we have an initialization value tied to the field */
4182 if (init)
4183 {
4184 /* The field is declared static */
e04a16fb 4185 if (flags & ACC_STATIC)
e04a16fb 4186 {
7525cc04
APB
4187 /* We include the field and its initialization part into
4188 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4189 walked, field initializations will be processed and
4190 fields initialized with known constants will be taken
4191 out of <clinit> and have their DECL_INITIAL set
7525cc04 4192 appropriately. */
c2952b01
APB
4193 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4194 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4195 if (TREE_OPERAND (init, 1)
4196 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4197 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4198 }
5e942c50
APB
4199 /* A non-static field declared with an immediate initialization is
4200 to be initialized in <init>, if any. This field is remembered
4201 to be processed at the time of the generation of <init>. */
c583dd46
APB
4202 else
4203 {
c2952b01
APB
4204 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4205 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4206 }
5b09b33e 4207 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4208 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4209 }
4210 }
4211 lineno = saved_lineno;
4212}
4213
c00f0fb2
APB
4214/* Generate finit$, using the list of initialized fields to populate
4215 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4216 one(s) used to initialized the field(s) caching outer context
4217 local(s). */
22eed1e6 4218
c2952b01
APB
4219static tree
4220generate_finit (class_type)
4221 tree class_type;
22eed1e6 4222{
c2952b01
APB
4223 int count = 0;
4224 tree list = TYPE_FINIT_STMT_LIST (class_type);
4225 tree mdecl, current, parms;
4226
4227 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4228 class_type, NULL_TREE,
4229 &count);
4230 CRAFTED_PARAM_LIST_FIXUP (parms);
4231 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4232 finit_identifier_node, parms);
4233 fix_method_argument_names (parms, mdecl);
4234 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4235 mdecl, NULL_TREE);
4236 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4237 start_artificial_method_body (mdecl);
4238
c2952b01 4239 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4240 java_method_add_stmt (mdecl,
4241 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4242 current));
22eed1e6 4243 end_artificial_method_body (mdecl);
c2952b01 4244 return mdecl;
22eed1e6
APB
4245}
4246
e04a16fb 4247static void
c2952b01
APB
4248add_instance_initializer (mdecl)
4249 tree mdecl;
e04a16fb 4250{
c2952b01
APB
4251 tree current;
4252 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4253 tree compound = NULL_TREE;
e04a16fb 4254
c2952b01 4255 if (stmt_list)
e04a16fb 4256 {
c2952b01
APB
4257 for (current = stmt_list; current; current = TREE_CHAIN (current))
4258 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4259
c2952b01
APB
4260 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4261 NULL_TREE, compound));
4262 }
e04a16fb
AG
4263}
4264
4265/* Shared accros method_declarator and method_header to remember the
4266 patch stage that was reached during the declaration of the method.
4267 A method DECL is built differently is there is no patch
4268 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4269 pending on the currently defined method. */
4270
4271static int patch_stage;
4272
4273/* Check the method declaration and add the method to its current
4274 class. If the argument list is known to contain incomplete types,
4275 the method is partially added and the registration will be resume
22eed1e6
APB
4276 once the method arguments resolved. If TYPE is NULL, we're dealing
4277 with a constructor. */
e04a16fb
AG
4278
4279static tree
4280method_header (flags, type, mdecl, throws)
4281 int flags;
4282 tree type, mdecl, throws;
4283{
1886c9d8 4284 tree type_wfl = NULL_TREE;
79d13333 4285 tree meth_name = NULL_TREE;
c2952b01 4286 tree current, orig_arg, this_class = NULL;
34d4df06 4287 tree id, meth;
e04a16fb 4288 int saved_lineno;
1886c9d8 4289 int constructor_ok = 0, must_chain;
c2952b01 4290 int count;
34d4df06
APB
4291
4292 if (mdecl == error_mark_node)
4293 return error_mark_node;
4294 meth = TREE_VALUE (mdecl);
4295 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4296
4297 check_modifiers_consistency (flags);
79d13333 4298
c2952b01
APB
4299 if (GET_CPC ())
4300 this_class = TREE_TYPE (GET_CPC ());
4301
4302 if (!this_class || this_class == error_mark_node)
79d13333 4303 return NULL_TREE;
e04a16fb
AG
4304
4305 /* There are some forbidden modifiers for an abstract method and its
4306 class must be abstract as well. */
22eed1e6 4307 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4308 {
4309 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4310 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4311 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4312 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4313 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4314 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4315 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4316 parse_error_context
781b0558 4317 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4318 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4319 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4320 }
c2952b01 4321
22eed1e6
APB
4322 /* Things to be checked when declaring a constructor */
4323 if (!type)
4324 {
4325 int ec = java_error_count;
4326 /* 8.6: Constructor declarations: we might be trying to define a
4327 method without specifying a return type. */
c2952b01 4328 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4329 parse_error_context
4330 (id, "Invalid method declaration, return type required");
4331 /* 8.6.3: Constructor modifiers */
4332 else
4333 {
4334 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4335 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4336 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4337 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4338 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4339 }
4340 /* If we found error here, we don't consider it's OK to tread
4341 the method definition as a constructor, for the rest of this
4342 function */
4343 if (ec == java_error_count)
4344 constructor_ok = 1;
4345 }
e04a16fb
AG
4346
4347 /* Method declared within the scope of an interface are implicitly
4348 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4349 modifiers are checked right after. */
e04a16fb
AG
4350
4351 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4352 {
4353 /* If FLAGS isn't set because of a modifier, turn the
4354 corresponding modifier WFL to NULL so we issue a warning on
4355 the obsolete use of the modifier */
4356 if (!(flags & ACC_PUBLIC))
4357 MODIFIER_WFL (PUBLIC_TK) = NULL;
4358 if (!(flags & ACC_ABSTRACT))
4359 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4360 flags |= ACC_PUBLIC;
4361 flags |= ACC_ABSTRACT;
4362 }
4363
c2952b01
APB
4364 /* Inner class can't declare static methods */
4365 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4366 {
4367 parse_error_context
4368 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4369 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4370 lang_printable_name (this_class, 0));
4371 }
4372
e04a16fb
AG
4373 /* Modifiers context reset moved up, so abstract method declaration
4374 modifiers can be later checked. */
4375
22eed1e6
APB
4376 /* Set constructor returned type to void and method name to <init>,
4377 unless we found an error identifier the constructor (in which
4378 case we retain the original name) */
4379 if (!type)
4380 {
4381 type = void_type_node;
4382 if (constructor_ok)
4383 meth_name = init_identifier_node;
4384 }
4385 else
4386 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4387
1886c9d8
APB
4388 /* Do the returned type resolution and registration if necessary */
4389 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4390
4a5f66c3
APB
4391 if (meth_name)
4392 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4393 EXPR_WFL_NODE (id) = meth_name;
4394 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4395
4396 if (must_chain)
e04a16fb 4397 {
1886c9d8
APB
4398 patch_stage = JDEP_METHOD_RETURN;
4399 register_incomplete_type (patch_stage, type_wfl, id, type);
4400 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4401 }
4402 else
1886c9d8 4403 TREE_TYPE (meth) = type;
e04a16fb
AG
4404
4405 saved_lineno = lineno;
4406 /* When defining an abstract or interface method, the curly
4407 bracket at level 1 doesn't exist because there is no function
4408 body */
4409 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4410 EXPR_WFL_LINENO (id));
4411
5e942c50
APB
4412 /* Remember the original argument list */
4413 orig_arg = TYPE_ARG_TYPES (meth);
4414
e04a16fb
AG
4415 if (patch_stage) /* includes ret type and/or all args */
4416 {
4417 jdep *jdep;
4418 meth = add_method_1 (this_class, flags, meth_name, meth);
4419 /* Patch for the return type */
4420 if (patch_stage == JDEP_METHOD_RETURN)
4421 {
4422 jdep = CLASSD_LAST (ctxp->classd_list);
4423 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4424 }
4425 /* This is the stop JDEP. METH allows the function's signature
4426 to be computed. */
4427 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4428 }
4429 else
5e942c50
APB
4430 meth = add_method (this_class, flags, meth_name,
4431 build_java_signature (meth));
4432
c2952b01
APB
4433 /* Remember final parameters */
4434 MARK_FINAL_PARMS (meth, orig_arg);
4435
5e942c50
APB
4436 /* Fix the method argument list so we have the argument name
4437 information */
4438 fix_method_argument_names (orig_arg, meth);
4439
4440 /* Register the parameter number and re-install the current line
4441 number */
e04a16fb
AG
4442 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4443 lineno = saved_lineno;
b9f7e36c
APB
4444
4445 /* Register exception specified by the `throws' keyword for
4446 resolution and set the method decl appropriate field to the list.
4447 Note: the grammar ensures that what we get here are class
4448 types. */
4449 if (throws)
4450 {
4451 throws = nreverse (throws);
4452 for (current = throws; current; current = TREE_CHAIN (current))
4453 {
4454 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4455 NULL_TREE, NULL_TREE);
4456 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4457 &TREE_VALUE (current);
4458 }
4459 DECL_FUNCTION_THROWS (meth) = throws;
4460 }
4461
e04a16fb
AG
4462 /* We set the DECL_NAME to ID so we can track the location where
4463 the function was declared. This allow us to report
4464 redefinition error accurately. When method are verified,
4465 DECL_NAME is reinstalled properly (using the content of the
4466 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4467 when Object is being defined. Constructor <init> names will be
4468 reinstalled the same way. */
c2952b01 4469 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4470 DECL_NAME (meth) = id;
22eed1e6
APB
4471
4472 /* Set the flag if we correctly processed a constructor */
4473 if (constructor_ok)
c2952b01
APB
4474 {
4475 DECL_CONSTRUCTOR_P (meth) = 1;
4476 /* Compute and store the number of artificial parameters declared
4477 for this constructor */
4478 for (count = 0, current = TYPE_FIELDS (this_class); current;
4479 current = TREE_CHAIN (current))
4480 if (FIELD_LOCAL_ALIAS (current))
4481 count++;
4482 DECL_FUNCTION_NAP (meth) = count;
4483 }
22eed1e6 4484
5e942c50
APB
4485 /* Eventually set the @deprecated tag flag */
4486 CHECK_DEPRECATED (meth);
4487
7f10c2e2
APB
4488 /* If doing xref, store column and line number information instead
4489 of the line number only. */
4490 if (flag_emit_xref)
4491 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4492
e04a16fb
AG
4493 return meth;
4494}
4495
5e942c50
APB
4496static void
4497fix_method_argument_names (orig_arg, meth)
4498 tree orig_arg, meth;
4499{
4500 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4501 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4502 {
4503 TREE_PURPOSE (arg) = this_identifier_node;
4504 arg = TREE_CHAIN (arg);
4505 }
de4c7b02 4506 while (orig_arg != end_params_node)
5e942c50
APB
4507 {
4508 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4509 orig_arg = TREE_CHAIN (orig_arg);
4510 arg = TREE_CHAIN (arg);
4511 }
4512}
4513
22eed1e6
APB
4514/* Complete the method declaration with METHOD_BODY. */
4515
4516static void
b635eb2f 4517finish_method_declaration (method_body)
22eed1e6
APB
4518 tree method_body;
4519{
79d13333
APB
4520 int flags;
4521
4522 if (!current_function_decl)
4523 return;
4524
4525 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4526
4527 /* 8.4.5 Method Body */
4528 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4529 {
4530 tree wfl = DECL_NAME (current_function_decl);
4531 parse_error_context (wfl,
4532 "%s method `%s' can't have a body defined",
4533 (METHOD_NATIVE (current_function_decl) ?
4534 "Native" : "Abstract"),
4535 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4536 method_body = NULL_TREE;
4537 }
4538 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4539 {
4540 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4541 parse_error_context
4542 (wfl,
4543 "Non native and non abstract method `%s' must have a body defined",
4544 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4545 method_body = NULL_TREE;
4546 }
4547
2c56429a
APB
4548 if (flag_emit_class_files && method_body
4549 && TREE_CODE (method_body) == NOP_EXPR
4550 && TREE_TYPE (current_function_decl)
4551 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4552 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4553
22eed1e6
APB
4554 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4555 maybe_absorb_scoping_blocks ();
4556 /* Exit function's body */
4557 exit_block ();
4558 /* Merge last line of the function with first line, directly in the
4559 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4560 if (!flag_emit_xref)
4561 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4562
4563 /* Since function's argument's list are shared, reset the
4564 ARG_FINAL_P parameter that might have been set on some of this
4565 function parameters. */
4566 UNMARK_FINAL_PARMS (current_function_decl);
4567
f099f336
APB
4568 /* So we don't have an irrelevant function declaration context for
4569 the next static block we'll see. */
4570 current_function_decl = NULL_TREE;
22eed1e6
APB
4571}
4572
4573/* Build a an error message for constructor circularity errors. */
4574
4575static char *
4576constructor_circularity_msg (from, to)
4577 tree from, to;
4578{
4579 static char string [4096];
c2e3db92 4580 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4581 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4582 free (t);
4583 return string;
4584}
4585
4586/* Verify a circular call to METH. Return 1 if an error is found, 0
4587 otherwise. */
4588
4589static int
4590verify_constructor_circularity (meth, current)
4591 tree meth, current;
4592{
4593 static tree list = NULL_TREE;
19e223db 4594 static int initialized_p;
22eed1e6 4595 tree c;
19e223db
MM
4596
4597 /* If we haven't already registered LIST with the garbage collector,
4598 do so now. */
4599 if (!initialized_p)
4600 {
4601 ggc_add_tree_root (&list, 1);
4602 initialized_p = 1;
4603 }
4604
22eed1e6
APB
4605 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4606 {
4607 if (TREE_VALUE (c) == meth)
4608 {
4609 char *t;
4610 if (list)
4611 {
4612 tree liste;
4613 list = nreverse (list);
4614 for (liste = list; liste; liste = TREE_CHAIN (liste))
4615 {
4616 parse_error_context
c63b98cd 4617 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4618 constructor_circularity_msg
4619 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4620 java_error_count--;
4621 }
4622 }
c2e3db92 4623 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4624 parse_error_context (TREE_PURPOSE (c),
4625 "%s: recursive invocation of constructor `%s'",
4626 constructor_circularity_msg (current, meth), t);
4627 free (t);
4628 list = NULL_TREE;
4629 return 1;
4630 }
4631 }
4632 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4633 {
4634 list = tree_cons (c, current, list);
4635 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4636 return 1;
4637 list = TREE_CHAIN (list);
4638 }
4639 return 0;
4640}
4641
e04a16fb
AG
4642/* Check modifiers that can be declared but exclusively */
4643
4644static void
4645check_modifiers_consistency (flags)
4646 int flags;
4647{
4648 int acc_count = 0;
4649 tree cl = NULL_TREE;
4650
e0fc4118
TT
4651 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4652 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4653 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4654 if (acc_count > 1)
4655 parse_error_context
e0fc4118
TT
4656 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4657
4658 acc_count = 0;
4659 cl = NULL_TREE;
14d075d8
TT
4660 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4661 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4662 if (acc_count > 1)
4663 parse_error_context (cl,
4664 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4665}
4666
4667/* Check the methode header METH for abstract specifics features */
4668
4669static void
4670check_abstract_method_header (meth)
4671 tree meth;
4672{
4673 int flags = get_access_flags_from_decl (meth);
4674 /* DECL_NAME might still be a WFL node */
c877974e 4675 tree name = GET_METHOD_NAME (meth);
e04a16fb 4676
2884c41e
KG
4677 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4678 ACC_ABSTRACT, "abstract method",
4679 IDENTIFIER_POINTER (name));
4680 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4681 ACC_PUBLIC, "abstract method",
4682 IDENTIFIER_POINTER (name));
e04a16fb
AG
4683
4684 check_modifiers ("Illegal modifier `%s' for interface method",
4685 flags, INTERFACE_METHOD_MODIFIERS);
4686}
4687
4688/* Create a FUNCTION_TYPE node and start augmenting it with the
4689 declared function arguments. Arguments type that can't be resolved
4690 are left as they are, but the returned node is marked as containing
4691 incomplete types. */
4692
4693static tree
4694method_declarator (id, list)
4695 tree id, list;
4696{
4697 tree arg_types = NULL_TREE, current, node;
4698 tree meth = make_node (FUNCTION_TYPE);
4699 jdep *jdep;
e04a16fb
AG
4700
4701 patch_stage = JDEP_NO_PATCH;
c2952b01 4702
34d4df06
APB
4703 if (GET_CPC () == error_mark_node)
4704 return error_mark_node;
4705
c2952b01
APB
4706 /* If we're dealing with an inner class constructor, we hide the
4707 this$<n> decl in the name field of its parameter declaration. We
4708 also might have to hide the outer context local alias
4709 initializers. Not done when the class is a toplevel class. */
4710 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4711 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4712 {
4713 tree aliases_list, type, thisn;
4714 /* First the aliases, linked to the regular parameters */
4715 aliases_list =
4716 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4717 TREE_TYPE (GET_CPC ()),
4718 NULL_TREE, NULL);
4719 list = chainon (nreverse (aliases_list), list);
4720
4721 /* Then this$<n> */
4722 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4723 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4724 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4725 list);
4726 }
e04a16fb
AG
4727
4728 for (current = list; current; current = TREE_CHAIN (current))
4729 {
c583dd46 4730 int must_chain = 0;
e04a16fb
AG
4731 tree wfl_name = TREE_PURPOSE (current);
4732 tree type = TREE_VALUE (current);
4733 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4734 tree already, arg_node;
4735 tree type_wfl = NULL_TREE;
23a79c61 4736 tree real_type;
c583dd46
APB
4737
4738 /* Obtain a suitable type for resolution, if necessary */
4739 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4740
4741 /* Process NAME, as it may specify extra dimension(s) for it */
4742 type = build_array_from_name (type, type_wfl, name, &name);
4743 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4744
23a79c61
APB
4745 real_type = GET_REAL_TYPE (type);
4746 if (TREE_CODE (real_type) == RECORD_TYPE)
4747 {
4748 real_type = promote_type (real_type);
4749 if (TREE_CODE (type) == TREE_LIST)
4750 TREE_PURPOSE (type) = real_type;
4751 }
5e942c50 4752
e04a16fb
AG
4753 /* Check redefinition */
4754 for (already = arg_types; already; already = TREE_CHAIN (already))
4755 if (TREE_PURPOSE (already) == name)
4756 {
781b0558
KG
4757 parse_error_context
4758 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4759 IDENTIFIER_POINTER (name),
e04a16fb
AG
4760 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4761 break;
4762 }
4763
4764 /* If we've an incomplete argument type, we know there is a location
4765 to patch when the type get resolved, later. */
4766 jdep = NULL;
c583dd46 4767 if (must_chain)
e04a16fb 4768 {
c583dd46
APB
4769 patch_stage = JDEP_METHOD;
4770 type = register_incomplete_type (patch_stage,
4771 type_wfl, wfl_name, type);
4772 jdep = CLASSD_LAST (ctxp->classd_list);
4773 JDEP_MISC (jdep) = id;
e04a16fb 4774 }
c583dd46 4775
c2952b01 4776 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4777 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4778 /* Remeber arguments declared final. */
4779 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4780
e04a16fb
AG
4781 if (jdep)
4782 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4783 TREE_CHAIN (arg_node) = arg_types;
4784 arg_types = arg_node;
4785 }
de4c7b02 4786 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4787 node = build_tree_list (id, meth);
4788 return node;
4789}
4790
4791static int
4792unresolved_type_p (wfl, returned)
4793 tree wfl;
4794 tree *returned;
4795
4796{
4797 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4798 {
e04a16fb 4799 if (returned)
165f37bc
APB
4800 {
4801 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4802 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4803 *returned = TREE_TYPE (decl);
4804 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4805 *returned = TREE_TYPE (GET_CPC ());
4806 else
4807 *returned = NULL_TREE;
4808 }
e04a16fb
AG
4809 return 1;
4810 }
4811 if (returned)
4812 *returned = wfl;
4813 return 0;
4814}
4815
4816/* From NAME, build a qualified identifier node using the
4817 qualification from the current package definition. */
4818
4819static tree
98a52c2c 4820parser_qualified_classname (name)
e04a16fb
AG
4821 tree name;
4822{
c2952b01
APB
4823 tree nested_class_name;
4824
98a52c2c 4825 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4826 return nested_class_name;
4827
e04a16fb 4828 if (ctxp->package)
c2952b01 4829 return merge_qualified_name (ctxp->package, name);
e04a16fb 4830 else
c2952b01 4831 return name;
e04a16fb
AG
4832}
4833
4834/* Called once the type a interface extends is resolved. Returns 0 if
4835 everything is OK. */
4836
4837static int
4838parser_check_super_interface (super_decl, this_decl, this_wfl)
4839 tree super_decl, this_decl, this_wfl;
4840{
4841 tree super_type = TREE_TYPE (super_decl);
4842
4843 /* Has to be an interface */
c2952b01 4844 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4845 {
4846 parse_error_context
4847 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4848 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4849 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4850 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4851 "interface" : "class"),
4852 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4853 return 1;
4854 }
4855
4856 /* Check scope: same package OK, other package: OK if public */
4857 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4858 return 1;
4859
4860 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4861 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4862 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4863 return 0;
4864}
4865
4866/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4867 0 if everthing is OK. */
4868
4869static int
4870parser_check_super (super_decl, this_decl, wfl)
4871 tree super_decl, this_decl, wfl;
4872{
e04a16fb
AG
4873 tree super_type = TREE_TYPE (super_decl);
4874
4875 /* SUPER should be a CLASS (neither an array nor an interface) */
4876 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4877 {
4878 parse_error_context
4879 (wfl, "Class `%s' can't subclass %s `%s'",
4880 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4881 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4882 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4883 return 1;
4884 }
4885
4886 if (CLASS_FINAL (TYPE_NAME (super_type)))
4887 {
4888 parse_error_context (wfl, "Can't subclass final classes: %s",
4889 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4890 return 1;
4891 }
4892
4893 /* Check scope: same package OK, other package: OK if public */
4894 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4895 return 1;
4896
4897 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4898 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4899 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4900 return 0;
4901}
4902
4903/* Create a new dependency list and link it (in a LIFO manner) to the
4904 CTXP list of type dependency list. */
4905
4906static void
4907create_jdep_list (ctxp)
4908 struct parser_ctxt *ctxp;
4909{
23a79c61 4910 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4911 new->first = new->last = NULL;
4912 new->next = ctxp->classd_list;
4913 ctxp->classd_list = new;
4914}
4915
4916static jdeplist *
4917reverse_jdep_list (ctxp)
4918 struct parser_ctxt *ctxp;
4919{
4920 register jdeplist *prev = NULL, *current, *next;
4921 for (current = ctxp->classd_list; current; current = next)
4922 {
4923 next = current->next;
4924 current->next = prev;
4925 prev = current;
4926 }
4927 return prev;
4928}
4929
23a79c61
APB
4930/* Create a fake pointer based on the ID stored in
4931 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4932 registered again. */
e04a16fb
AG
4933
4934static tree
23a79c61
APB
4935obtain_incomplete_type (type_name)
4936 tree type_name;
e04a16fb 4937{
23a79c61
APB
4938 tree ptr, name;
4939
4940 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4941 name = EXPR_WFL_NODE (type_name);
4942 else if (INCOMPLETE_TYPE_P (type_name))
4943 name = TYPE_NAME (type_name);
4944 else
4945 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4946
4947 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4948 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4949 break;
4950
4951 if (!ptr)
4952 {
e04a16fb 4953 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4954 BUILD_PTR_FROM_NAME (ptr, name);
4955 layout_type (ptr);
e04a16fb
AG
4956 pop_obstacks ();
4957 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4958 ctxp->incomplete_class = ptr;
4959 }
4960
4961 return ptr;
4962}
4963
4964/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4965 non NULL instead of computing a new fake type based on WFL. The new
4966 dependency is inserted in the current type dependency list, in FIFO
4967 manner. */
4968
4969static tree
4970register_incomplete_type (kind, wfl, decl, ptr)
4971 int kind;
4972 tree wfl, decl, ptr;
4973{
23a79c61 4974 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4975
e04a16fb
AG
4976 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4977 ptr = obtain_incomplete_type (wfl);
4978
4979 JDEP_KIND (new) = kind;
4980 JDEP_DECL (new) = decl;
4981 JDEP_SOLV (new) = ptr;
4982 JDEP_WFL (new) = wfl;
4983 JDEP_CHAIN (new) = NULL;
4984 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4985 /* For some dependencies, set the enclosing class of the current
4986 class to be the enclosing context */
4987 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4988 && GET_ENCLOSING_CPC ())
4989 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4990 else
324ed8fd 4991 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4992 JDEP_GET_PATCH (new) = (tree *)NULL;
4993
4994 JDEP_INSERT (ctxp->classd_list, new);
4995
4996 return ptr;
4997}
4998
4999void
5000java_check_circular_reference ()
5001{
5002 tree current;
5003 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5004 {
5005 tree type = TREE_TYPE (current);
e920ebc9 5006 if (CLASS_INTERFACE (current))
e04a16fb
AG
5007 {
5008 /* Check all interfaces this class extends */
5009 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5010 int n, i;
5011
5012 if (!basetype_vec)
5013 return;
5014 n = TREE_VEC_LENGTH (basetype_vec);
5015 for (i = 0; i < n; i++)
5016 {
5017 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5018 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5019 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5020 parse_error_context (lookup_cl (current),
5021 "Cyclic interface inheritance");
5022 }
5023 }
5024 else
5025 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5026 parse_error_context (lookup_cl (current),
c2952b01
APB
5027 "Cyclic class inheritance%s",
5028 (cyclic_inheritance_report ?
5029 cyclic_inheritance_report : ""));
5030 }
5031}
5032
5033/* Augment the parameter list PARM with parameters crafted to
5034 initialize outer context locals aliases. Through ARTIFICIAL, a
5035 count is kept of the number of crafted parameters. MODE governs
5036 what eventually gets created: something suitable for a function
5037 creation or a function invocation, either the constructor or
c00f0fb2 5038 finit$. */
c2952b01
APB
5039
5040static tree
5041build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5042 int mode;
5043 tree class_type, parm;
5044 int *artificial;
5045{
5046 tree field;
da632f2c
APB
5047 tree additional_parms = NULL_TREE;
5048
c2952b01
APB
5049 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5050 if (FIELD_LOCAL_ALIAS (field))
5051 {
63ad61ed 5052 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01
APB
5053 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5054
5055 switch (mode)
5056 {
5057 case AIPL_FUNCTION_DECLARATION:
5058 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5059 purpose = build_wfl_node (get_identifier (buffer));
5060 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5061 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5062 else
5063 value = TREE_TYPE (field);
5064 break;
5065
5066 case AIPL_FUNCTION_CREATION:
5067 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5068 purpose = get_identifier (buffer);
5069 value = TREE_TYPE (field);
5070 break;
5071
5072 case AIPL_FUNCTION_FINIT_INVOCATION:
5073 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5074 /* Now, this is wrong. purpose should always be the NAME
5075 of something and value its matching value (decl, type,
5076 etc...) FIXME -- but there is a lot to fix. */
5077
5078 /* When invoked for this kind of operation, we already
5079 know whether a field is used or not. */
5080 purpose = TREE_TYPE (field);
5081 value = build_wfl_node (get_identifier (buffer));
5082 break;
5083
5084 case AIPL_FUNCTION_CTOR_INVOCATION:
5085 /* There are two case: the constructor invokation happends
5086 outside the local inner, in which case, locales from the outer
5087 context are directly used.
5088
5089 Otherwise, we fold to using the alias directly. */
5090 if (class_type == current_class)
5091 value = field;
5092 else
5093 {
5094 name = get_identifier (&buffer[4]);
5095 value = IDENTIFIER_LOCAL_VALUE (name);
5096 }
5097 break;
5098 }
da632f2c 5099 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5100 if (artificial)
5101 *artificial +=1;
5102 }
da632f2c
APB
5103 if (additional_parms)
5104 {
5105 if (ANONYMOUS_CLASS_P (class_type)
5106 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5107 additional_parms = nreverse (additional_parms);
5108 parm = chainon (additional_parms, parm);
5109 }
5110
5111 return parm;
c2952b01
APB
5112}
5113
5114/* Craft a constructor for CLASS_DECL -- what we should do when none
5115 where found. ARGS is non NULL when a special signature must be
5116 enforced. This is the case for anonymous classes. */
5117
5118static void
5119craft_constructor (class_decl, args)
5120 tree class_decl, args;
5121{
5122 tree class_type = TREE_TYPE (class_decl);
5123 tree parm = NULL_TREE;
5124 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5125 ACC_PUBLIC : 0);
5126 int i = 0, artificial = 0;
5127 tree decl, ctor_name;
5128 char buffer [80];
5129
5130 push_obstacks (&permanent_obstack, &permanent_obstack);
5131
5132 /* The constructor name is <init> unless we're dealing with an
5133 anonymous class, in which case the name will be fixed after having
5134 be expanded. */
5135 if (ANONYMOUS_CLASS_P (class_type))
5136 ctor_name = DECL_NAME (class_decl);
5137 else
5138 ctor_name = init_identifier_node;
5139
5140 /* If we're dealing with an inner class constructor, we hide the
5141 this$<n> decl in the name field of its parameter declaration. */
5142 if (PURE_INNER_CLASS_TYPE_P (class_type))
5143 {
5144 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5145 parm = tree_cons (build_current_thisn (class_type),
5146 build_pointer_type (type), parm);
5147
5148 /* Some more arguments to be hidden here. The values of the local
5149 variables of the outer context that the inner class needs to see. */
5150 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5151 class_type, parm,
5152 &artificial);
5153 }
5154
5155 /* Then if there are any args to be enforced, enforce them now */
5156 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5157 {
5158 sprintf (buffer, "parm%d", i++);
5159 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5160 }
c2952b01
APB
5161
5162 CRAFTED_PARAM_LIST_FIXUP (parm);
5163 decl = create_artificial_method (class_type, flags, void_type_node,
5164 ctor_name, parm);
5165 fix_method_argument_names (parm, decl);
5166 /* Now, mark the artificial parameters. */
5167 DECL_FUNCTION_NAP (decl) = artificial;
5168
5169 pop_obstacks ();
5170 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5171}
5172
c2952b01 5173
e920ebc9
APB
5174/* Fix the constructors. This will be called right after circular
5175 references have been checked. It is necessary to fix constructors
5176 early even if no code generation will take place for that class:
5177 some generated constructor might be required by the class whose
5178 compilation triggered this one to be simply loaded. */
5179
5180void
5181java_fix_constructors ()
5182{
5183 tree current;
5184
5185 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5186 {
e920ebc9
APB
5187 tree class_type = TREE_TYPE (current);
5188 int saw_ctor = 0;
c2952b01
APB
5189 tree decl;
5190
5191 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5192 continue;
e920ebc9
APB
5193
5194 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5195 {
5196 if (DECL_CONSTRUCTOR_P (decl))
5197 {
5198 fix_constructors (decl);
5199 saw_ctor = 1;
5200 }
5201 }
5202
c2952b01
APB
5203 /* Anonymous class constructor can't be generated that early. */
5204 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5205 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5206 }
5207}
5208
23a79c61
APB
5209/* safe_layout_class just makes sure that we can load a class without
5210 disrupting the current_class, input_file, lineno, etc, information
5211 about the class processed currently. */
5212
e04a16fb
AG
5213void
5214safe_layout_class (class)
5215 tree class;
5216{
5217 tree save_current_class = current_class;
3b304f5b 5218 const char *save_input_filename = input_filename;
e04a16fb 5219 int save_lineno = lineno;
5e942c50 5220
e04a16fb 5221 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5222
e04a16fb
AG
5223 layout_class (class);
5224 pop_obstacks ();
5e942c50 5225
e04a16fb
AG
5226 current_class = save_current_class;
5227 input_filename = save_input_filename;
5228 lineno = save_lineno;
5229 CLASS_LOADED_P (class) = 1;
5230}
5231
5232static tree
5233jdep_resolve_class (dep)
5234 jdep *dep;
5235{
5236 tree decl;
5237
23a79c61
APB
5238 if (JDEP_RESOLVED_P (dep))
5239 decl = JDEP_RESOLVED_DECL (dep);
5240 else
e04a16fb 5241 {
c2952b01 5242 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5243 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5244 JDEP_RESOLVED (dep, decl);
5245 }
23a79c61 5246
e04a16fb 5247 if (!decl)
23a79c61 5248 complete_class_report_errors (dep);
1e12ab9b 5249 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5250 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5251 return decl;
5252}
5253
5254/* Complete unsatisfied class declaration and their dependencies */
5255
5256void
5257java_complete_class ()
5258{
e04a16fb
AG
5259 tree cclass;
5260 jdeplist *cclassd;
5261 int error_found;
b67d701b 5262 tree type;
e04a16fb
AG
5263
5264 push_obstacks (&permanent_obstack, &permanent_obstack);
5265
9a7ab4b3 5266 /* Process imports */
e04a16fb 5267 process_imports ();
e04a16fb
AG
5268
5269 /* Rever things so we have the right order */
5270 ctxp->class_list = nreverse (ctxp->class_list);
5271 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5272
e04a16fb
AG
5273 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5274 cclass && cclassd;
5275 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5276 {
5277 jdep *dep;
5278 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5279 {
5280 tree decl;
e04a16fb
AG
5281 if (!(decl = jdep_resolve_class (dep)))
5282 continue;
5283
5284 /* Now it's time to patch */
5285 switch (JDEP_KIND (dep))
5286 {
5287 case JDEP_SUPER:
5288 /* Simply patch super */
5289 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5290 continue;
5291 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5292 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5293 break;
5294
5295 case JDEP_FIELD:
5296 {
5297 /* We do part of the job done in add_field */
5298 tree field_decl = JDEP_DECL (dep);
5299 tree field_type = TREE_TYPE (decl);
5300 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5301 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5302 field_type = promote_type (field_type);
5303 pop_obstacks ();
5304 TREE_TYPE (field_decl) = field_type;
5e942c50 5305 DECL_ALIGN (field_decl) = 0;
11cf4d18 5306 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5307 layout_decl (field_decl, 0);
e04a16fb
AG
5308 SOURCE_FRONTEND_DEBUG
5309 (("Completed field/var decl `%s' with `%s'",
5310 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5311 IDENTIFIER_POINTER (DECL_NAME (decl))));
5312 break;
5313 }
5314 case JDEP_METHOD: /* We start patching a method */
5315 case JDEP_METHOD_RETURN:
5316 error_found = 0;
5317 while (1)
5318 {
5319 if (decl)
5320 {
b67d701b
PB
5321 type = TREE_TYPE(decl);
5322 if (TREE_CODE (type) == RECORD_TYPE)
5323 type = promote_type (type);
e04a16fb
AG
5324 JDEP_APPLY_PATCH (dep, type);
5325 SOURCE_FRONTEND_DEBUG
5326 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5327 "Completing fct `%s' with ret type `%s'":
5328 "Completing arg `%s' with type `%s'"),
5329 IDENTIFIER_POINTER (EXPR_WFL_NODE
5330 (JDEP_DECL_WFL (dep))),
5331 IDENTIFIER_POINTER (DECL_NAME (decl))));
5332 }
5333 else
5334 error_found = 1;
5335 dep = JDEP_CHAIN (dep);
5336 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5337 break;
5338 else
5339 decl = jdep_resolve_class (dep);
5340 }
5341 if (!error_found)
5342 {
5343 tree mdecl = JDEP_DECL (dep), signature;
5344 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5345 /* Recompute and reset the signature, check first that
5346 all types are now defined. If they're not,
5347 dont build the signature. */
5348 if (check_method_types_complete (mdecl))
5349 {
5350 signature = build_java_signature (TREE_TYPE (mdecl));
5351 set_java_signature (TREE_TYPE (mdecl), signature);
5352 }
e04a16fb
AG
5353 pop_obstacks ();
5354 }
5355 else
5356 continue;
5357 break;
5358
5359 case JDEP_INTERFACE:
5360 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5361 JDEP_WFL (dep)))
5362 continue;
5363 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5364 break;
5365
b67d701b 5366 case JDEP_PARM:
e04a16fb 5367 case JDEP_VARIABLE:
b67d701b
PB
5368 type = TREE_TYPE(decl);
5369 if (TREE_CODE (type) == RECORD_TYPE)
5370 type = promote_type (type);
5371 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5372 break;
5373
5374 case JDEP_TYPE:
5375 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5376 SOURCE_FRONTEND_DEBUG
5377 (("Completing a random type dependency on a '%s' node",
5378 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5379 break;
5380
b9f7e36c 5381 case JDEP_EXCEPTION:
c877974e
APB
5382 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5383 SOURCE_FRONTEND_DEBUG
5384 (("Completing `%s' `throws' argument node",
5385 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5386 break;
5387
c2952b01
APB
5388 case JDEP_ANONYMOUS:
5389 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5390 break;
5391
e04a16fb 5392 default:
0a2138e2
APB
5393 fatal ("Can't handle patch code %d - java_complete_class",
5394 JDEP_KIND (dep));
e04a16fb
AG
5395 }
5396 }
5397 }
5398 pop_obstacks ();
5399 return;
5400}
5401
5402/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5403 array. */
5404
5405static tree
c2952b01
APB
5406resolve_class (enclosing, class_type, decl, cl)
5407 tree enclosing, class_type, decl, cl;
e04a16fb 5408{
49f48c71
KG
5409 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5410 const char *base = name;
78d21f92
PB
5411 tree resolved_type = TREE_TYPE (class_type);
5412 tree resolved_type_decl;
e04a16fb 5413
78d21f92
PB
5414 if (resolved_type != NULL_TREE)
5415 {
5416 tree resolved_type_decl = TYPE_NAME (resolved_type);
5417 if (resolved_type_decl == NULL_TREE
5418 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5419 {
5420 resolved_type_decl = build_decl (TYPE_DECL,
5421 TYPE_NAME (class_type),
5422 resolved_type);
5423 }
5424 return resolved_type_decl;
5425 }
5426
e04a16fb
AG
5427 /* 1- Check to see if we have an array. If true, find what we really
5428 want to resolve */
5429 while (name[0] == '[')
5430 name++;
5431 if (base != name)
34d4df06
APB
5432 {
5433 TYPE_NAME (class_type) = get_identifier (name);
5434 WFL_STRIP_BRACKET (cl, cl);
5435 }
e04a16fb
AG
5436
5437 /* 2- Resolve the bare type */
c2952b01
APB
5438 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5439 decl, cl)))
e04a16fb
AG
5440 return NULL_TREE;
5441 resolved_type = TREE_TYPE (resolved_type_decl);
5442
5443 /* 3- If we have and array, reconstruct the array down to its nesting */
5444 if (base != name)
5445 {
5446 while (base != name)
5447 {
5448 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5449 resolved_type = promote_type (resolved_type);
5450 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5451 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5452 name--;
5453 }
5454 /* Build a fake decl for this, since this is what is expected to
5455 be returned. */
5456 resolved_type_decl =
5457 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5458 /* Figure how those two things are important for error report. FIXME */
5459 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5460 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5461 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5462 }
78d21f92 5463 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5464 return resolved_type_decl;
5465}
5466
5467/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5468 are used to report error messages. */
5469
78d21f92 5470tree
c2952b01
APB
5471do_resolve_class (enclosing, class_type, decl, cl)
5472 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5473{
5474 tree new_class_decl;
e04a16fb
AG
5475
5476 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5477 it is changed by find_in_imports{_on_demand} and (but it doesn't
5478 really matter) qualify_and_find */
e04a16fb 5479
c2952b01
APB
5480 /* 0- Search in the current class as an inner class */
5481
5482 /* Maybe some code here should be added to load the class or
5483 something, at least if the class isn't an inner class and ended
5484 being loaded from class file. FIXME. */
a40d21da
APB
5485 while (enclosing)
5486 {
5487 tree name;
5488
5489 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5490 return new_class_decl;
5491
0c2b8145
APB
5492 /* Explore enclosing contexts. */
5493 while (INNER_CLASS_DECL_P (enclosing))
5494 {
5495 enclosing = DECL_CONTEXT (enclosing);
5496 if ((new_class_decl = find_as_inner_class (enclosing,
5497 class_type, cl)))
5498 return new_class_decl;
5499 }
5500
a40d21da
APB
5501 /* Now go to the upper classes, bail out if necessary. */
5502 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5503 if (!enclosing || enclosing == object_type_node)
5504 break;
5505
5506 if (TREE_CODE (enclosing) == RECORD_TYPE)
5507 {
5508 enclosing = TYPE_NAME (enclosing);
5509 continue;
5510 }
5511
5512 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5513 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5514 else
5515 name = enclosing;
5516 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5517 }
c2952b01 5518
9a7ab4b3
APB
5519 /* 1- Check for the type in single imports. This will change
5520 TYPE_NAME() if something relevant is found */
5521 find_in_imports (class_type);
e04a16fb 5522
9a7ab4b3 5523 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5524 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5525 {
5526 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5527 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5528 load_class (TYPE_NAME (class_type), 0);
5529 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5530 }
5531
9a7ab4b3
APB
5532 /* 3- Search according to the current package definition */
5533 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5534 {
5535 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5536 TYPE_NAME (class_type))))
5537 return new_class_decl;
5538 }
5539
5540 /* 4- Check the import on demands. Don't allow bar.baz to be
5541 imported from foo.* */
5542 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5543 if (find_in_imports_on_demand (class_type))
5544 return NULL_TREE;
5545
5546 /* If found in find_in_imports_on_demant, the type has already been
5547 loaded. */
5548 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5549 return new_class_decl;
5550
5551 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5552 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5553 {
ee07f4f4 5554 tree package;
d6baf6f5
APB
5555
5556 /* If there is a current package (ctxp->package), it's the first
5557 element of package_list and we can skip it. */
5558 for (package = (ctxp->package ?
5559 TREE_CHAIN (package_list) : package_list);
5560 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5561 if ((new_class_decl = qualify_and_find (class_type,
5562 TREE_PURPOSE (package),
5563 TYPE_NAME (class_type))))
5564 return new_class_decl;
5565 }
5566
5567 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5568 load_class (TYPE_NAME (class_type), 0);
5569 if (check_pkg_class_access (TYPE_NAME (class_type),
5570 (cl ? cl : lookup_cl (decl))))
5571 return NULL_TREE;
5572
9a7ab4b3 5573 /* 6- Last call for a resolution */
e04a16fb
AG
5574 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5575}
5576
9a7ab4b3
APB
5577static tree
5578qualify_and_find (class_type, package, name)
5579 tree class_type, package, name;
5580{
5581 tree new_qualified = merge_qualified_name (package, name);
5582 tree new_class_decl;
5583
5584 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5585 load_class (new_qualified, 0);
5586 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5587 {
5588 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5589 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5590 load_class (new_qualified, 0);
5591 TYPE_NAME (class_type) = new_qualified;
5592 return IDENTIFIER_CLASS_VALUE (new_qualified);
5593 }
5594 return NULL_TREE;
5595}
5596
e04a16fb 5597/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5598 parsed class). Return a decl node. This function is meant to be
5599 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5600
5601static tree
c877974e
APB
5602resolve_and_layout (something, cl)
5603 tree something;
e04a16fb
AG
5604 tree cl;
5605{
34d4df06 5606 tree decl, decl_type;
c877974e 5607
23a79c61
APB
5608 /* Don't do that on the current class */
5609 if (something == current_class)
5610 return TYPE_NAME (current_class);
c877974e 5611
23a79c61 5612 /* Don't do anything for void and other primitive types */
c877974e
APB
5613 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5614 return NULL_TREE;
5615
23a79c61
APB
5616 /* Pointer types can be reall pointer types or fake pointers. When
5617 finding a real pointer, recheck for primitive types */
5618 if (TREE_CODE (something) == POINTER_TYPE)
5619 {
5620 if (TREE_TYPE (something))
5621 {
5622 something = TREE_TYPE (something);
5623 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5624 return NULL_TREE;
5625 }
5626 else
5627 something = TYPE_NAME (something);
5628 }
5629
5630 /* Don't do anything for arrays of primitive types */
5631 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5632 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5633 return NULL_TREE;
5634
c2952b01
APB
5635 /* Something might be a WFL */
5636 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5637 something = EXPR_WFL_NODE (something);
5638
5639 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5640 TYPE_DECL or a real TYPE */
5641 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5642 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5643 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5644
23a79c61
APB
5645 if (!(decl = resolve_no_layout (something, cl)))
5646 return NULL_TREE;
5647
5648 /* Resolve and layout if necessary */
34d4df06
APB
5649 decl_type = TREE_TYPE (decl);
5650 layout_class_methods (decl_type);
5651 /* Check methods */
5652 if (CLASS_FROM_SOURCE_P (decl_type))
5653 java_check_methods (decl);
5654 /* Layout the type if necessary */
5655 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5656 safe_layout_class (decl_type);
23a79c61 5657
e04a16fb
AG
5658 return decl;
5659}
5660
5661/* Resolve a class, returns its decl but doesn't perform any
5662 layout. The current parsing context is saved and restored */
5663
5664static tree
5665resolve_no_layout (name, cl)
5666 tree name, cl;
5667{
5668 tree ptr, decl;
5669 BUILD_PTR_FROM_NAME (ptr, name);
5670 java_parser_context_save_global ();
c2952b01 5671 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5672 java_parser_context_restore_global ();
5673
5674 return decl;
5675}
5676
23a79c61
APB
5677/* Called when reporting errors. Skip leader '[' in a complex array
5678 type description that failed to be resolved. */
e04a16fb 5679
49f48c71 5680static const char *
e04a16fb 5681purify_type_name (name)
49f48c71 5682 const char *name;
e04a16fb
AG
5683{
5684 while (*name && *name == '[')
5685 name++;
5686 return name;
5687}
5688
5689/* The type CURRENT refers to can't be found. We print error messages. */
5690
5691static void
5692complete_class_report_errors (dep)
5693 jdep *dep;
5694{
49f48c71 5695 const char *name;
23a79c61
APB
5696
5697 if (!JDEP_WFL (dep))
5698 return;
5699
5700 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5701 switch (JDEP_KIND (dep))
5702 {
5703 case JDEP_SUPER:
5704 parse_error_context
5705 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5706 purify_type_name (name),
e04a16fb
AG
5707 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5708 break;
5709 case JDEP_FIELD:
5710 parse_error_context
5711 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5712 purify_type_name (name),
e04a16fb
AG
5713 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5714 break;
5715 case JDEP_METHOD: /* Covers arguments */
5716 parse_error_context
781b0558 5717 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5718 purify_type_name (name),
e04a16fb
AG
5719 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5720 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5721 break;
5722 case JDEP_METHOD_RETURN: /* Covers return type */
5723 parse_error_context
781b0558 5724 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5725 purify_type_name (name),
e04a16fb
AG
5726 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5727 break;
5728 case JDEP_INTERFACE:
5729 parse_error_context
5730 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5731 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5732 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5733 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5734 break;
5735 case JDEP_VARIABLE:
5736 parse_error_context
781b0558 5737 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5738 purify_type_name (IDENTIFIER_POINTER
5739 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5740 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5741 break;
b9f7e36c
APB
5742 case JDEP_EXCEPTION: /* As specified by `throws' */
5743 parse_error_context
5744 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5745 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5746 break;
0a2138e2
APB
5747 default:
5748 /* Fix for -Wall. Just break doing nothing. The error will be
5749 caught later */
5750 break;
e04a16fb
AG
5751 }
5752}
5753
22eed1e6
APB
5754/* Return a static string containing the DECL prototype string. If
5755 DECL is a constructor, use the class name instead of the form
5756 <init> */
5757
49f48c71 5758static const char *
22eed1e6
APB
5759get_printable_method_name (decl)
5760 tree decl;
5761{
49f48c71 5762 const char *to_return;
9ee9b555 5763 tree name = NULL_TREE;
22eed1e6
APB
5764
5765 if (DECL_CONSTRUCTOR_P (decl))
5766 {
5767 name = DECL_NAME (decl);
5e942c50 5768 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5769 }
5770
5771 to_return = lang_printable_name (decl, 0);
5772 if (DECL_CONSTRUCTOR_P (decl))
5773 DECL_NAME (decl) = name;
5774
5775 return to_return;
5776}
5777
5e942c50
APB
5778/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5779 nevertheless needs to be verfied, 1 otherwise. */
5780
5781static int
5782reset_method_name (method)
5783 tree method;
5784{
c2952b01 5785 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5786 {
5787 /* NAME is just the plain name when Object is being defined */
5788 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5789 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5790 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5791 return 0;
5792 }
5793 else
5794 return 1;
5795}
5796
c877974e
APB
5797/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5798
5799tree
5800java_get_real_method_name (method_decl)
5801 tree method_decl;
5802{
5803 tree method_name = DECL_NAME (method_decl);
5804 if (DECL_CONSTRUCTOR_P (method_decl))
5805 return init_identifier_node;
82371d41
APB
5806
5807 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5808 and still can be a constructor. FIXME */
5809
23a79c61
APB
5810 /* Don't confuse method only bearing the name of their class as
5811 constructors */
82371d41
APB
5812 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5813 && ctxp
c2952b01 5814 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5815 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5816 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5817 return init_identifier_node;
5818 else
5819 return EXPR_WFL_NODE (method_name);
5820}
5821
22eed1e6
APB
5822/* Track method being redefined inside the same class. As a side
5823 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5824 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5825
e04a16fb
AG
5826static int
5827check_method_redefinition (class, method)
5828 tree class, method;
5829{
5830 tree redef, name;
5831 tree cl = DECL_NAME (method);
c3f2a476 5832 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5833 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5834 fixed and checked */
5e942c50
APB
5835
5836 /* Reset the method name before running the check. If it returns 1,
5837 the method doesn't need to be verified with respect to method
5838 redeclaration and we return 0 */
5839 if (reset_method_name (method))
e04a16fb 5840 return 0;
5e942c50
APB
5841
5842 name = DECL_NAME (method);
e04a16fb
AG
5843 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5844 {
c3f2a476 5845 if (redef == method)
e04a16fb 5846 break;
c3f2a476
APB
5847 if (DECL_NAME (redef) == name
5848 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5849 {
22eed1e6
APB
5850 parse_error_context
5851 (cl, "Duplicate %s declaration `%s'",
5852 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5853 get_printable_method_name (redef));
e04a16fb
AG
5854 return 1;
5855 }
5856 }
5857 return 0;
5858}
5859
1175b9b4
TT
5860/* Return 1 if check went ok, 0 otherwise. */
5861static int
d77613be
APB
5862check_abstract_method_definitions (do_interface, class_decl, type)
5863 int do_interface;
5864 tree class_decl, type;
5865{
5866 tree class = TREE_TYPE (class_decl);
5867 tree method, end_type;
1175b9b4 5868 int ok = 1;
d77613be
APB
5869
5870 end_type = (do_interface ? object_type_node : type);
5871 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5872 {
5873 tree other_super, other_method, method_sig, method_name;
5874 int found = 0;
165f37bc 5875 int end_type_reached = 0;
d77613be
APB
5876
5877 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5878 continue;
5879
5880 /* Now verify that somewhere in between TYPE and CLASS,
5881 abstract method METHOD gets a non abstract definition
5882 that is inherited by CLASS. */
5883
5884 method_sig = build_java_signature (TREE_TYPE (method));
5885 method_name = DECL_NAME (method);
5886 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5887 method_name = EXPR_WFL_NODE (method_name);
5888
165f37bc
APB
5889 other_super = class;
5890 do {
5891 if (other_super == end_type)
5892 end_type_reached = 1;
5893
5894 /* Method search */
5895 for (other_method = TYPE_METHODS (other_super); other_method;
5896 other_method = TREE_CHAIN (other_method))
5897 {
5898 tree s = build_java_signature (TREE_TYPE (other_method));
5899 tree other_name = DECL_NAME (other_method);
5900
5901 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5902 other_name = EXPR_WFL_NODE (other_name);
5903 if (!DECL_CLINIT_P (other_method)
5904 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5905 && method_name == other_name
5906 && method_sig == s
5907 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5908 {
5909 found = 1;
5910 break;
5911 }
5912 }
5913 other_super = CLASSTYPE_SUPER (other_super);
5914 } while (!end_type_reached);
5915
d77613be
APB
5916 /* Report that abstract METHOD didn't find an implementation
5917 that CLASS can use. */
5918 if (!found)
5919 {
c2e3db92 5920 char *t = xstrdup (lang_printable_name
d77613be
APB
5921 (TREE_TYPE (TREE_TYPE (method)), 0));
5922 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5923 tree saved_wfl = NULL_TREE;
5924
5925 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5926 {
5927 saved_wfl = DECL_NAME (method);
5928 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5929 }
5930
5931 parse_error_context
5932 (lookup_cl (class_decl),
781b0558 5933 "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
5934 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5935 t, lang_printable_name (method, 0),
5936 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5937 "interface" : "class"),
5938 IDENTIFIER_POINTER (ccn),
5939 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5940 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5941 ok = 0;
d77613be 5942 free (t);
1175b9b4 5943
d77613be
APB
5944 if (saved_wfl)
5945 DECL_NAME (method) = saved_wfl;
5946 }
5947 }
1175b9b4
TT
5948
5949 if (ok && do_interface)
5950 {
5951 /* Check for implemented interfaces. */
5952 int i;
5953 tree vector = TYPE_BINFO_BASETYPES (type);
5954 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5955 {
5956 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5957 ok = check_abstract_method_definitions (1, class_decl, super);
5958 }
5959 }
5960
5961 return ok;
d77613be
APB
5962}
5963
614eaae0 5964/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5965 methods. */
5966
5967static void
5968java_check_abstract_method_definitions (class_decl)
5969 tree class_decl;
5970{
5971 tree class = TREE_TYPE (class_decl);
5972 tree super, vector;
5973 int i;
5974
5975 if (CLASS_ABSTRACT (class_decl))
5976 return;
5977
5978 /* Check for inherited types */
165f37bc
APB
5979 super = class;
5980 do {
5981 super = CLASSTYPE_SUPER (super);
5982 check_abstract_method_definitions (0, class_decl, super);
5983 } while (super != object_type_node);
d77613be
APB
5984
5985 /* Check for implemented interfaces. */
5986 vector = TYPE_BINFO_BASETYPES (class);
5987 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5988 {
5989 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5990 check_abstract_method_definitions (1, class_decl, super);
5991 }
5992}
5993
165f37bc
APB
5994/* Check all the types method DECL uses and return 1 if all of them
5995 are now complete, 0 otherwise. This is used to check whether its
5996 safe to build a method signature or not. */
5997
5998static int
5999check_method_types_complete (decl)
6000 tree decl;
6001{
6002 tree type = TREE_TYPE (decl);
6003 tree args;
6004
6005 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6006 return 0;
6007
6008 args = TYPE_ARG_TYPES (type);
6009 if (TREE_CODE (type) == METHOD_TYPE)
6010 args = TREE_CHAIN (args);
6011 for (; args != end_params_node; args = TREE_CHAIN (args))
6012 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6013 return 0;
6014
6015 return 1;
6016}
6017
34d4df06
APB
6018/* Visible interface to check methods contained in CLASS_DECL */
6019
6020void
6021java_check_methods (class_decl)
6022 tree class_decl;
6023{
6024 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6025 return;
6026
6027 if (CLASS_INTERFACE (class_decl))
6028 java_check_abstract_methods (class_decl);
6029 else
6030 java_check_regular_methods (class_decl);
6031
6032 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6033}
6034
d77613be
APB
6035/* Check all the methods of CLASS_DECL. Methods are first completed
6036 then checked according to regular method existance rules. If no
6037 constructor for CLASS_DECL were encountered, then build its
6038 declaration. */
e04a16fb
AG
6039
6040static void
6041java_check_regular_methods (class_decl)
6042 tree class_decl;
6043{
c2952b01 6044 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6045 tree method;
6046 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6047 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6048 tree mthrows;
6049
6050 /* It is not necessary to check methods defined in java.lang.Object */
6051 if (class == object_type_node)
6052 return;
e04a16fb 6053
23a79c61
APB
6054 if (!TYPE_NVIRTUALS (class))
6055 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6056
6057 /* Should take interfaces into account. FIXME */
6058 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6059 {
5e942c50 6060 tree sig;
e04a16fb
AG
6061 tree method_wfl = DECL_NAME (method);
6062 int aflags;
6063
5e942c50
APB
6064 /* If we previously found something and its name was saved,
6065 reinstall it now */
6066 if (found && saved_found_wfl)
ba179f9f
APB
6067 {
6068 DECL_NAME (found) = saved_found_wfl;
6069 saved_found_wfl = NULL_TREE;
6070 }
5e942c50 6071
e04a16fb
AG
6072 /* Check for redefinitions */
6073 if (check_method_redefinition (class, method))
6074 continue;
6075
22eed1e6
APB
6076 /* If we see one constructor a mark so we don't generate the
6077 default one. Also skip other verifications: constructors
6078 can't be inherited hence hiden or overriden */
6079 if (DECL_CONSTRUCTOR_P (method))
6080 {
6081 saw_constructor = 1;
6082 continue;
6083 }
6084
c877974e
APB
6085 /* We verify things thrown by the method. They must inherits from
6086 java.lang.Throwable */
6087 for (mthrows = DECL_FUNCTION_THROWS (method);
6088 mthrows; mthrows = TREE_CHAIN (mthrows))
6089 {
6090 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6091 parse_error_context
781b0558 6092 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6093 IDENTIFIER_POINTER
6094 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6095 }
6096
e04a16fb 6097 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6098 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6099
c2952b01
APB
6100 /* Inner class can't declare static methods */
6101 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6102 {
6103 char *t = xstrdup (lang_printable_name (class, 0));
6104 parse_error_context
6105 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6106 lang_printable_name (method, 0), t);
6107 free (t);
6108 }
6109
5e942c50 6110 /* Nothing overrides or it's a private method. */
aabd7048 6111 if (!found)
5e942c50 6112 continue;
aabd7048
PB
6113 if (METHOD_PRIVATE (found))
6114 {
6115 found = NULL_TREE;
6116 continue;
6117 }
5e942c50
APB
6118
6119 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6120 We set it temporarily for the sake of the error report. */
6121 saved_found_wfl = DECL_NAME (found);
6122 reset_method_name (found);
6123
614eaae0
APB
6124 /* If `found' is declared in an interface, make sure the
6125 modifier matches. */
6126 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6127 && clinit_identifier_node != DECL_NAME (found)
6128 && !METHOD_PUBLIC (method))
6129 {
6130 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6131 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6132 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6133 lang_printable_name (method, 0),
6134 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6135 }
6136
e04a16fb
AG
6137 /* Can't override a method with the same name and different return
6138 types. */
6139 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6140 {
614eaae0
APB
6141 char *t = xstrdup
6142 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6143 parse_error_context
7f10c2e2 6144 (method_wfl,
b9f7e36c 6145 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6146 lang_printable_name (found, 0), t,
b9f7e36c
APB
6147 IDENTIFIER_POINTER
6148 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6149 free (t);
6150 }
e04a16fb 6151
7f10c2e2
APB
6152 aflags = get_access_flags_from_decl (found);
6153 /* If the method has default, access in an other package, then
6154 issue a warning that the current method doesn't override the
6155 one that was found elsewhere. Do not issue this warning when
6156 the match was found in java.lang.Object. */
6157 if (DECL_CONTEXT (found) != object_type_node
a003f638 6158 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6159 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6160 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6161 && flag_not_overriding)
6162 {
6163 parse_warning_context
781b0558 6164 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
7f10c2e2
APB
6165 lang_printable_name (found, 0),
6166 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6167 IDENTIFIER_POINTER (DECL_NAME
6168 (TYPE_NAME (DECL_CONTEXT (found)))));
6169 continue;
6170 }
6171
e04a16fb
AG
6172 /* Can't override final. Can't override static. */
6173 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6174 {
6175 /* Static *can* override static */
6176 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6177 continue;
6178 parse_error_context
6179 (method_wfl,
6180 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6181 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6182 lang_printable_name (found, 0),
e04a16fb
AG
6183 (METHOD_FINAL (found) ? "final" : "static"),
6184 IDENTIFIER_POINTER
6185 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6186 continue;
6187 }
7f10c2e2 6188
e04a16fb
AG
6189 /* Static method can't override instance method. */
6190 if (METHOD_STATIC (method))
6191 {
6192 parse_error_context
6193 (method_wfl,
781b0558 6194 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6195 lang_printable_name (found, 0),
e04a16fb
AG
6196 IDENTIFIER_POINTER
6197 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6198 continue;
6199 }
5e942c50 6200
5e942c50
APB
6201 /* - Overriding/hiding public must be public
6202 - Overriding/hiding protected must be protected or public
6203 - If the overriden or hidden method has default (package)
6204 access, then the overriding or hiding method must not be
614eaae0
APB
6205 private; otherwise, a compile-time error occurs. If
6206 `found' belongs to an interface, things have been already
6207 taken care of. */
6208 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6209 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6210 || (METHOD_PROTECTED (found)
6211 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6212 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6213 && METHOD_PRIVATE (method))))
e04a16fb
AG
6214 {
6215 parse_error_context
6216 (method_wfl,
781b0558 6217 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6218 (METHOD_PUBLIC (method) ? "public" :
6219 (METHOD_PRIVATE (method) ? "private" : "protected")),
6220 IDENTIFIER_POINTER (DECL_NAME
6221 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6222 continue;
6223 }
6224
b9f7e36c
APB
6225 /* Overriding methods must have compatible `throws' clauses on checked
6226 exceptions, if any */
6227 check_throws_clauses (method, method_wfl, found);
6228
e04a16fb
AG
6229 /* Inheriting multiple methods with the same signature. FIXME */
6230 }
6231
5e942c50
APB
6232 /* Don't forget eventual pending found and saved_found_wfl. Take
6233 into account that we might have exited because we saw an
d77613be 6234 artificial method as the last entry. */
5e942c50
APB
6235
6236 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6237 DECL_NAME (found) = saved_found_wfl;
6238
23a79c61
APB
6239 if (!TYPE_NVIRTUALS (class))
6240 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6241
d77613be
APB
6242 /* Search for inherited abstract method not yet implemented in this
6243 class. */
6244 java_check_abstract_method_definitions (class_decl);
6245
22eed1e6 6246 if (!saw_constructor)
e920ebc9 6247 fatal ("No constructor found");
e04a16fb
AG
6248}
6249
b9f7e36c
APB
6250/* Return a non zero value if the `throws' clause of METHOD (if any)
6251 is incompatible with the `throws' clause of FOUND (if any). */
6252
6253static void
6254check_throws_clauses (method, method_wfl, found)
6255 tree method, method_wfl, found;
6256{
6257 tree mthrows, fthrows;
6258
c877974e
APB
6259 /* Can't check these things with class loaded from bytecode. FIXME */
6260 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6261 return;
6262
b9f7e36c
APB
6263 for (mthrows = DECL_FUNCTION_THROWS (method);
6264 mthrows; mthrows = TREE_CHAIN (mthrows))
6265 {
6266 /* We don't verify unchecked expressions */
c877974e 6267 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6268 continue;
6269 /* Checked expression must be compatible */
6270 for (fthrows = DECL_FUNCTION_THROWS (found);
6271 fthrows; fthrows = TREE_CHAIN (fthrows))
6272 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6273 break;
6274 if (!fthrows)
6275 {
6276 parse_error_context
781b0558 6277 (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 6278 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6279 lang_printable_name (found, 0),
b9f7e36c
APB
6280 IDENTIFIER_POINTER
6281 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6282 }
6283 }
6284}
6285
e04a16fb
AG
6286/* Check abstract method of interface INTERFACE */
6287
6288static void
5e942c50
APB
6289java_check_abstract_methods (interface_decl)
6290 tree interface_decl;
e04a16fb
AG
6291{
6292 int i, n;
6293 tree method, basetype_vec, found;
5e942c50 6294 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6295
6296 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6297 {
b9f7e36c 6298 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6299
6300 /* 2- Check for double definition inside the defining interface */
6301 if (check_method_redefinition (interface, method))
6302 continue;
6303
6304 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6305 the thrown exceptions (FIXME) */
e04a16fb
AG
6306 found = lookup_java_interface_method2 (interface, method);
6307 if (found)
6308 {
5e942c50
APB
6309 char *t;
6310 tree saved_found_wfl = DECL_NAME (found);
6311 reset_method_name (found);
c2e3db92 6312 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6313 parse_error_context
b9f7e36c 6314 (method_wfl,
5e942c50 6315 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6316 lang_printable_name (found, 0), t,
b9f7e36c
APB
6317 IDENTIFIER_POINTER
6318 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6319 free (t);
5e942c50 6320 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6321 continue;
e04a16fb
AG
6322 }
6323 }
6324
6325 /* 4- Inherited methods can't differ by their returned types */
6326 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6327 return;
6328 n = TREE_VEC_LENGTH (basetype_vec);
6329 for (i = 0; i < n; i++)
6330 {
6331 tree sub_interface_method, sub_interface;
6332 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6333 if (!vec_elt)
6334 continue;
6335 sub_interface = BINFO_TYPE (vec_elt);
6336 for (sub_interface_method = TYPE_METHODS (sub_interface);
6337 sub_interface_method;
6338 sub_interface_method = TREE_CHAIN (sub_interface_method))
6339 {
6340 found = lookup_java_interface_method2 (interface,
6341 sub_interface_method);
6342 if (found && (found != sub_interface_method))
5e942c50
APB
6343 {
6344 tree saved_found_wfl = DECL_NAME (found);
6345 reset_method_name (found);
6346 parse_error_context
6347 (lookup_cl (sub_interface_method),
781b0558 6348 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6349 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6350 lang_printable_name (found, 0),
6351 IDENTIFIER_POINTER
6352 (DECL_NAME (TYPE_NAME
6353 (DECL_CONTEXT (sub_interface_method)))),
6354 IDENTIFIER_POINTER
6355 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6356 DECL_NAME (found) = saved_found_wfl;
6357 }
e04a16fb
AG
6358 }
6359 }
6360}
6361
e04a16fb
AG
6362/* Lookup methods in interfaces using their name and partial
6363 signature. Return a matching method only if their types differ. */
6364
6365static tree
6366lookup_java_interface_method2 (class, method_decl)
6367 tree class, method_decl;
6368{
6369 int i, n;
6370 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6371
6372 if (!basetype_vec)
6373 return NULL_TREE;
6374
6375 n = TREE_VEC_LENGTH (basetype_vec);
6376 for (i = 0; i < n; i++)
6377 {
6378 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6379 if ((BINFO_TYPE (vec_elt) != object_type_node)
6380 && (to_return =
6381 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6382 return to_return;
6383 }
6384 for (i = 0; i < n; i++)
6385 {
6386 to_return = lookup_java_interface_method2
6387 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6388 if (to_return)
6389 return to_return;
6390 }
6391
6392 return NULL_TREE;
6393}
6394
6395/* Lookup method using their name and partial signature. Return a
6396 matching method only if their types differ. */
6397
6398static tree
6399lookup_java_method2 (clas, method_decl, do_interface)
6400 tree clas, method_decl;
6401 int do_interface;
6402{
5e942c50
APB
6403 tree method, method_signature, method_name, method_type, name;
6404
e04a16fb 6405 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6406 name = DECL_NAME (method_decl);
6407 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6408 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6409 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6410
6411 while (clas != NULL_TREE)
6412 {
6413 for (method = TYPE_METHODS (clas);
6414 method != NULL_TREE; method = TREE_CHAIN (method))
6415 {
6416 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6417 tree name = DECL_NAME (method);
6418 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6419 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6420 && method_sig == method_signature
6421 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6422 return method;
e04a16fb
AG
6423 }
6424 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6425 }
6426 return NULL_TREE;
6427}
6428
f441f671
APB
6429/* Return the line that matches DECL line number, and try its best to
6430 position the column number. Used during error reports. */
e04a16fb
AG
6431
6432static tree
6433lookup_cl (decl)
6434 tree decl;
6435{
6436 static tree cl = NULL_TREE;
f441f671 6437 char *line, *found;
e04a16fb
AG
6438
6439 if (!decl)
6440 return NULL_TREE;
6441
6442 if (cl == NULL_TREE)
19e223db
MM
6443 {
6444 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6445 ggc_add_tree_root (&cl, 1);
6446 }
e04a16fb
AG
6447
6448 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6449 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6450
f441f671
APB
6451 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6452 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6453
6454 found = strstr ((const char *)line,
6455 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6456 if (found)
6457 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6458
e04a16fb
AG
6459 return cl;
6460}
6461
6462/* Look for a simple name in the single-type import list */
6463
6464static tree
6465find_name_in_single_imports (name)
6466 tree name;
6467{
6468 tree node;
6469
6470 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6471 if (TREE_VALUE (node) == name)
6472 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6473
6474 return NULL_TREE;
6475}
6476
6477/* Process all single-type import. */
6478
6479static int
6480process_imports ()
6481{
6482 tree import;
6483 int error_found;
6484
6485 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6486 {
6487 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6488
6489 /* Don't load twice something already defined. */
6490 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6491 continue;
6492 QUALIFIED_P (to_be_found) = 1;
6493 load_class (to_be_found, 0);
6494 error_found =
6495 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6496 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6497 {
6498 parse_error_context (TREE_PURPOSE (import),
6499 "Class or interface `%s' not found in import",
6500 IDENTIFIER_POINTER (to_be_found));
6501 return 1;
6502 }
6503 if (error_found)
6504 return 1;
6505 }
6506 return 0;
6507}
6508
9a7ab4b3
APB
6509/* Possibly find and mark a class imported by a single-type import
6510 statement. */
e04a16fb 6511
9a7ab4b3 6512static void
e04a16fb
AG
6513find_in_imports (class_type)
6514 tree class_type;
6515{
6516 tree import;
6517
6518 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6519 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6520 {
6521 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6522 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6523 }
e04a16fb
AG
6524}
6525
e04a16fb 6526static int
63a212ed 6527note_possible_classname (name, len)
49f48c71 6528 const char *name;
63a212ed 6529 int len;
e04a16fb 6530{
63a212ed
PB
6531 tree node;
6532 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6533 len = len - 5;
6534 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6535 len = len - 6;
e04a16fb 6536 else
63a212ed
PB
6537 return 0;
6538 node = ident_subst (name, len, "", '/', '.', "");
6539 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6540 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6541 return 1;
e04a16fb
AG
6542}
6543
6544/* Read a import directory, gathering potential match for further type
6545 references. Indifferently reads a filesystem or a ZIP archive
6546 directory. */
6547
6548static void
6549read_import_dir (wfl)
6550 tree wfl;
6551{
63a212ed 6552 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6553 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6554 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6555 DIR *dirp = NULL;
d8fccff5 6556 JCF *saved_jcf = current_jcf;
e04a16fb 6557
63a212ed
PB
6558 int found = 0;
6559 int k;
6560 void *entry;
6561 struct buffer filename[1];
6562
6563
6564 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6565 return;
6566 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6567
6568 BUFFER_INIT (filename);
6569 buffer_grow (filename, package_length + 100);
6570
6571 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6572 {
49f48c71 6573 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6574 int entry_length = strlen (entry_name);
6575 if (jcf_path_is_zipfile (entry))
6576 {
6577 ZipFile *zipf;
6578 buffer_grow (filename, entry_length);
6579 memcpy (filename->data, entry_name, entry_length - 1);
6580 filename->data[entry_length-1] = '\0';
6581 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6582 if (zipf == NULL)
6583 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6584 else
6585 {
6586 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6587 BUFFER_RESET (filename);
6588 for (k = 0; k < package_length; k++)
6589 {
6590 char ch = package_name[k];
6591 *filename->ptr++ = ch == '.' ? '/' : ch;
6592 }
6593 *filename->ptr++ = '/';
6594
345137c7 6595 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6596 {
49f48c71 6597 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6598 int current_entry_len = zipd->filename_length;
6599
345137c7
TT
6600 if (current_entry_len >= BUFFER_LENGTH (filename)
6601 && strncmp (filename->data, current_entry,
6602 BUFFER_LENGTH (filename)) != 0)
63a212ed 6603 continue;
345137c7 6604 found |= note_possible_classname (current_entry,
63a212ed
PB
6605 current_entry_len);
6606 }
6607 }
6608 }
6609 else
6610 {
6611 BUFFER_RESET (filename);
6612 buffer_grow (filename, entry_length + package_length + 4);
6613 strcpy (filename->data, entry_name);
6614 filename->ptr = filename->data + entry_length;
6615 for (k = 0; k < package_length; k++)
6616 {
6617 char ch = package_name[k];
6618 *filename->ptr++ = ch == '.' ? '/' : ch;
6619 }
6620 *filename->ptr = '\0';
6621
6622 dirp = opendir (filename->data);
6623 if (dirp == NULL)
6624 continue;
6625 *filename->ptr++ = '/';
6626 for (;;)
6627 {
63a212ed 6628 int len;
49f48c71 6629 const char *d_name;
63a212ed
PB
6630 struct dirent *direntp = readdir (dirp);
6631 if (!direntp)
6632 break;
6633 d_name = direntp->d_name;
6634 len = strlen (direntp->d_name);
6635 buffer_grow (filename, len+1);
6636 strcpy (filename->ptr, d_name);
345137c7 6637 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6638 package_length+len+1);
6639 }
6640 if (dirp)
6641 closedir (dirp);
6642 }
6643 }
e04a16fb 6644
63a212ed 6645 free (filename->data);
e04a16fb 6646
63a212ed
PB
6647 /* Here we should have a unified way of retrieving an entry, to be
6648 indexed. */
6649 if (!found)
e04a16fb
AG
6650 {
6651 static int first = 1;
6652 if (first)
6653 {
781b0558 6654 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6655 java_error_count++;
6656 first = 0;
6657 }
6658 else
63a212ed
PB
6659 parse_error_context (wfl, "Package `%s' not found in import",
6660 package_name);
e04a16fb
AG
6661 current_jcf = saved_jcf;
6662 return;
6663 }
e04a16fb
AG
6664 current_jcf = saved_jcf;
6665}
6666
6667/* Possibly find a type in the import on demands specified
6668 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6669 entire list, to detected potential double definitions. */
6670
6671static int
6672find_in_imports_on_demand (class_type)
6673 tree class_type;
6674{
ab3a6dd6 6675 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6676 int seen_once = -1;
ab3a6dd6 6677 tree cl = NULL_TREE;
e04a16fb
AG
6678
6679 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6680 {
49f48c71 6681 const char *id_name;
e04a16fb
AG
6682 obstack_grow (&temporary_obstack,
6683 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6684 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6685 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6686 obstack_grow0 (&temporary_obstack,
6687 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6688 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6689 id_name = obstack_finish (&temporary_obstack);
6690
6691 node = maybe_get_identifier (id_name);
6692 if (node && IS_A_CLASSFILE_NAME (node))
6693 {
6694 if (seen_once < 0)
6695 {
6696 cl = TREE_PURPOSE (import);
6697 seen_once = 1;
6698 node_to_use = node;
6699 }
6700 else
6701 {
6702 seen_once++;
6703 parse_error_context
1e12ab9b
APB
6704 (TREE_PURPOSE (import),
6705 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6706 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6707 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6708 }
6709 }
6710 }
6711
6712 if (seen_once == 1)
6713 {
6714 /* Setup lineno so that it refers to the line of the import (in
6715 case we parse a class file and encounter errors */
6716 tree decl;
6717 int saved_lineno = lineno;
6718 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6719 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6720 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6721 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6722 /* If there is no DECL set for the class or if the class isn't
6723 loaded and not seen in source yet, the load */
6724 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6725 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6726 load_class (node_to_use, 0);
6727 lineno = saved_lineno;
6728 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6729 }
6730 else
6731 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6732}
6733
9a7ab4b3
APB
6734/* Add package NAME to the list of package encountered so far. To
6735 speed up class lookup in do_resolve_class, we make sure a
6736 particular package is added only once. */
6737
6738static void
6739register_package (name)
6740 tree name;
6741{
6742 static struct hash_table _pht, *pht = NULL;
6743
6744 if (!pht)
6745 {
6746 hash_table_init (&_pht, hash_newfunc,
6747 java_hash_hash_tree_node, java_hash_compare_tree_node);
6748 pht = &_pht;
6749 }
6750
6751 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6752 {
6753 package_list = chainon (package_list, build_tree_list (name, NULL));
6754 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6755 }
6756}
6757
5e942c50
APB
6758static tree
6759resolve_package (pkg, next)
6760 tree pkg, *next;
6761{
c2952b01 6762 tree current, acc;
5e942c50 6763 tree type_name = NULL_TREE;
49f48c71 6764 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6765
6766 /* The trick is to determine when the package name stops and were
6767 the name of something contained in the package starts. Then we
6768 return a fully qualified name of what we want to get. */
6769
6770 /* Do a quick search on well known package names */
6771 if (!strncmp (name, "java.lang.reflect", 17))
6772 {
6773 *next =
6774 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6775 type_name = lookup_package_type (name, 17);
6776 }
6777 else if (!strncmp (name, "java.lang", 9))
6778 {
6779 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6780 type_name = lookup_package_type (name, 9);
6781 }
5e942c50 6782
2c56429a
APB
6783 /* If we found something here, return */
6784 if (type_name)
6785 return type_name;
6786
6787 *next = EXPR_WFL_QUALIFICATION (pkg);
6788
6789 /* Try the current package. */
6790 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6791 IDENTIFIER_LENGTH (ctxp->package)))
6792 {
6793 type_name =
6794 lookup_package_type_and_set_next (name,
6795 IDENTIFIER_LENGTH (ctxp->package),
6796 next );
6797 if (type_name)
6798 return type_name;
6799 }
6800
6801 /* Search in imported package */
6802 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6803 {
6804 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6805 int len = IDENTIFIER_LENGTH (current_pkg_name);
6806 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6807 {
6808 tree left, dummy;
6809
6810 breakdown_qualified (&left, &dummy, current_pkg_name);
6811 len = IDENTIFIER_LENGTH (left);
6812 type_name = lookup_package_type_and_set_next (name, len, next);
6813 if (type_name)
6814 break;
6815 }
6816 }
6817
c2952b01
APB
6818 /* Try to progressively construct a type name */
6819 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6820 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6821 current; current = TREE_CHAIN (current))
6822 {
6823 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6824 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6825 {
6826 type_name = acc;
6b48deee
APB
6827 /* resolve_package should be used in a loop, hence we
6828 point at this one to naturally process the next one at
6829 the next iteration. */
6830 *next = current;
c2952b01
APB
6831 break;
6832 }
6833 }
2c56429a
APB
6834 return type_name;
6835}
6836
6837static tree
6838lookup_package_type_and_set_next (name, len, next)
49f48c71 6839 const char *name;
2c56429a
APB
6840 int len;
6841 tree *next;
6842{
49f48c71 6843 const char *ptr;
2c56429a
APB
6844 tree type_name = lookup_package_type (name, len);
6845
6846 if (!type_name)
6847 return NULL;
6848
6849 ptr = IDENTIFIER_POINTER (type_name);
6850 while (ptr && (ptr = strchr (ptr, '.')))
6851 {
6852 *next = TREE_CHAIN (*next);
6853 ptr++;
6854 }
5e942c50
APB
6855 return type_name;
6856}
6857
6858static tree
6859lookup_package_type (name, from)
49f48c71 6860 const char *name;
5e942c50
APB
6861 int from;
6862{
6863 char subname [128];
49f48c71 6864 const char *sub = &name[from+1];
5e942c50
APB
6865 while (*sub != '.' && *sub)
6866 sub++;
6867 strncpy (subname, name, sub-name);
6868 subname [sub-name] = '\0';
6869 return get_identifier (subname);
6870}
6871
cf1748bf 6872static void
4dbf4496
APB
6873check_inner_class_access (decl, enclosing_decl, cl)
6874 tree decl, enclosing_decl, cl;
cf1748bf 6875{
4dbf4496
APB
6876 int access = 0;
6877
cf1748bf 6878 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6879 as a result of processing a JDEP crafted by source_start_java_method
6880 for the purpose of patching its parm decl. But the error would
6881 have been already trapped when fixing the method's signature.
6882 DECL can also be NULL in case of earlier errors. */
6883 if (!decl || !cl)
cf1748bf
APB
6884 return;
6885
4dbf4496
APB
6886 /* We grant access to private and protected inner classes if the
6887 location from where we're trying to access DECL is an enclosing
6888 context for DECL or if both have a common enclosing context. */
6889 if (CLASS_PRIVATE (decl))
6890 access = 1;
6891 if (CLASS_PROTECTED (decl))
6892 access = 2;
6893 if (!access)
6894 return;
6895
6896 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6897 TREE_TYPE (decl))
6898 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6899 TREE_TYPE (decl)))
6900 return;
6901
6902 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6903 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6904 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6905 lang_printable_name (decl, 0));
6906}
6907
e04a16fb
AG
6908/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6909 access violations were found, 1 otherwise. */
6910
6911static int
6912check_pkg_class_access (class_name, cl)
6913 tree class_name;
6914 tree cl;
6915{
6916 tree type;
e04a16fb
AG
6917
6918 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6919 return 0;
6920
6921 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6922 return 0;
6923
6924 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6925 {
e28cd97b
APB
6926 /* Access to a private class within the same package is
6927 allowed. */
6928 tree l, r;
6929 breakdown_qualified (&l, &r, class_name);
6930 if (l == ctxp->package)
6931 return 0;
6932
e04a16fb 6933 parse_error_context
781b0558 6934 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6935 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6936 IDENTIFIER_POINTER (class_name));
6937 return 1;
6938 }
6939 return 0;
6940}
6941
6942/* Local variable declaration. */
6943
6944static void
6945declare_local_variables (modifier, type, vlist)
6946 int modifier;
6947 tree type;
6948 tree vlist;
6949{
c583dd46
APB
6950 tree decl, current, saved_type;
6951 tree type_wfl = NULL_TREE;
e04a16fb 6952 int must_chain = 0;
c2952b01 6953 int final_p = 0;
e04a16fb 6954
2aa11e97 6955 /* Push a new block if statements were seen between the last time we
e04a16fb 6956 pushed a block and now. Keep a cound of block to close */
f099f336 6957 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6958 {
f099f336 6959 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6960 tree b = enter_block ();
f099f336 6961 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6962 }
6963
6964 if (modifier)
6965 {
6966 int i;
6967 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6968 if (modifier == ACC_FINAL)
c2952b01 6969 final_p = 1;
c877974e
APB
6970 else
6971 {
6972 parse_error_context
6973 (ctxp->modifier_ctx [i],
6974 "Only `final' is allowed as a local variables modifier");
6975 return;
6976 }
e04a16fb
AG
6977 }
6978
c583dd46
APB
6979 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6980 hold the TYPE value if a new incomplete has to be created (as
6981 opposed to being found already existing and reused). */
6982 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6983
6984 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6985 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6986
6987 /* Go through all the declared variables */
6988 for (current = vlist, saved_type = type; current;
6989 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6990 {
c877974e 6991 tree other, real_type;
e04a16fb
AG
6992 tree wfl = TREE_PURPOSE (current);
6993 tree name = EXPR_WFL_NODE (wfl);
6994 tree init = TREE_VALUE (current);
e04a16fb 6995
c583dd46
APB
6996 /* Process NAME, as it may specify extra dimension(s) for it */
6997 type = build_array_from_name (type, type_wfl, name, &name);
6998
6999 /* Variable redefinition check */
7000 if ((other = lookup_name_in_blocks (name)))
7001 {
7002 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7003 DECL_SOURCE_LINE (other));
7004 continue;
7005 }
7006
7007 /* Type adjustment. We may have just readjusted TYPE because
7008 the variable specified more dimensions. Make sure we have
7009 a reference if we can and don't have one already. */
1886c9d8 7010 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7011
7012 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7013 /* Never layout this decl. This will be done when its scope
7014 will be entered */
c877974e 7015 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 7016 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7017 BLOCK_CHAIN_DECL (decl);
7018
d4370213
APB
7019 /* If doing xreferencing, replace the line number with the WFL
7020 compound value */
7021 if (flag_emit_xref)
7022 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7023
e04a16fb
AG
7024 /* Don't try to use an INIT statement when an error was found */
7025 if (init && java_error_count)
7026 init = NULL_TREE;
c583dd46
APB
7027
7028 /* Add the initialization function to the current function's code */
7029 if (init)
e04a16fb 7030 {
c583dd46
APB
7031 /* Name might have been readjusted */
7032 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7033 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7034 java_method_add_stmt (current_function_decl,
7035 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7036 init));
7037 }
7038
7039 /* Setup dependency the type of the decl */
7040 if (must_chain)
7041 {
7042 jdep *dep;
7043 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7044 dep = CLASSD_LAST (ctxp->classd_list);
7045 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7046 }
7047 }
7048 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7049}
7050
7051/* Called during parsing. Build decls from argument list. */
7052
7053static void
7054source_start_java_method (fndecl)
7055 tree fndecl;
7056{
7057 tree tem;
7058 tree parm_decl;
7059 int i;
7060
79d13333
APB
7061 if (!fndecl)
7062 return;
7063
e04a16fb
AG
7064 current_function_decl = fndecl;
7065
7066 /* New scope for the function */
7067 enter_block ();
7068 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7069 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7070 {
7071 tree type = TREE_VALUE (tem);
7072 tree name = TREE_PURPOSE (tem);
7073
23a79c61
APB
7074 /* If type is incomplete. Create an incomplete decl and ask for
7075 the decl to be patched later */
e04a16fb
AG
7076 if (INCOMPLETE_TYPE_P (type))
7077 {
7078 jdep *jdep;
c877974e
APB
7079 tree real_type = GET_REAL_TYPE (type);
7080 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7081 type = obtain_incomplete_type (type);
e04a16fb
AG
7082 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7083 jdep = CLASSD_LAST (ctxp->classd_list);
7084 JDEP_MISC (jdep) = name;
7085 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7086 }
7087 else
7088 parm_decl = build_decl (PARM_DECL, name, type);
7089
c2952b01
APB
7090 /* Remember if a local variable was declared final (via its
7091 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7092 if (ARG_FINAL_P (tem))
7093 LOCAL_FINAL (parm_decl) = 1;
7094
e04a16fb
AG
7095 BLOCK_CHAIN_DECL (parm_decl);
7096 }
7097 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7098 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7099 nreverse (tem);
7100 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7101 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7102}
7103
22eed1e6
APB
7104/* Called during parsing. Creates an artificial method declaration. */
7105
7106static tree
7107create_artificial_method (class, flags, type, name, args)
7108 tree class;
7109 int flags;
7110 tree type, name, args;
7111{
22eed1e6
APB
7112 tree mdecl;
7113
c2952b01 7114 java_parser_context_save_global ();
22eed1e6
APB
7115 lineno = 0;
7116 mdecl = make_node (FUNCTION_TYPE);
7117 TREE_TYPE (mdecl) = type;
7118 TYPE_ARG_TYPES (mdecl) = args;
7119 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7120 java_parser_context_restore_global ();
22eed1e6
APB
7121 DECL_ARTIFICIAL (mdecl) = 1;
7122 return mdecl;
7123}
7124
7125/* Starts the body if an artifical method. */
7126
7127static void
7128start_artificial_method_body (mdecl)
7129 tree mdecl;
7130{
7131 DECL_SOURCE_LINE (mdecl) = 1;
7132 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7133 source_start_java_method (mdecl);
7134 enter_block ();
7135}
7136
7137static void
7138end_artificial_method_body (mdecl)
7139 tree mdecl;
7140{
7141 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7142 exit_block ();
7143}
7144
e04a16fb
AG
7145/* Called during expansion. Push decls formerly built from argument
7146 list so they're usable during expansion. */
7147
7148static void
7149expand_start_java_method (fndecl)
7150 tree fndecl;
7151{
7152 tree tem, *ptr;
e04a16fb 7153
e04a16fb
AG
7154 current_function_decl = fndecl;
7155
c2952b01
APB
7156 if (! quiet_flag)
7157 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7158 announce_function (fndecl);
c2952b01
APB
7159 if (! quiet_flag)
7160 fprintf (stderr, "]");
7161
7162 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7163 ptr = &DECL_ARGUMENTS (fndecl);
7164 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7165 while (tem)
7166 {
7167 tree next = TREE_CHAIN (tem);
b67d701b 7168 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7169 if (PROMOTE_PROTOTYPES
7170 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7171 && INTEGRAL_TYPE_P (type))
7172 type = integer_type_node;
b67d701b 7173 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7174 layout_decl (tem, 0);
7175 pushdecl (tem);
e04a16fb
AG
7176 *ptr = tem;
7177 ptr = &TREE_CHAIN (tem);
7178 tem = next;
7179 }
7180 *ptr = NULL_TREE;
7181 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7182 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7183}
7184
7185/* Terminate a function and expand its body. */
7186
7187static void
7188source_end_java_method ()
7189{
7190 tree fndecl = current_function_decl;
138657ec 7191 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7192
79d13333
APB
7193 if (!fndecl)
7194 return;
7195
e04a16fb
AG
7196 java_parser_context_save_global ();
7197 lineno = ctxp->last_ccb_indent1;
7198
b67d701b
PB
7199 /* Set EH language codes */
7200 java_set_exception_lang_code ();
7201
5423609c
APB
7202 /* Turn function bodies with only a NOP expr null, so they don't get
7203 generated at all and we won't get warnings when using the -W
7204 -Wall flags. */
7205 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7206 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7207
e04a16fb
AG
7208 /* Generate function's code */
7209 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7210 && ! flag_emit_class_files
7211 && ! flag_emit_xref)
e04a16fb
AG
7212 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7213
7214 /* pop out of its parameters */
7215 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7216 poplevel (1, 0, 1);
7217 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7218
7219 /* Generate rtl for function exit. */
e8fc7396 7220 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7221 {
7222 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7223 /* Emit catch-finally clauses */
7224 emit_handlers ();
e04a16fb
AG
7225 expand_function_end (input_filename, lineno, 0);
7226
138657ec
AH
7227 /* FIXME: If the current method contains any exception handlers,
7228 force asynchronous_exceptions: this is necessary because signal
7229 handlers in libjava may throw exceptions. This is far from being
7230 a perfect solution, but it's better than doing nothing at all.*/
7231 if (catch_clauses)
7232 asynchronous_exceptions = 1;
7233
e04a16fb
AG
7234 /* Run the optimizers and output assembler code for this function. */
7235 rest_of_compilation (fndecl);
7236 }
7237
7238 current_function_decl = NULL_TREE;
8226320b 7239 permanent_allocation (1);
e04a16fb 7240 java_parser_context_restore_global ();
138657ec 7241 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7242}
7243
7244/* Record EXPR in the current function block. Complements compound
7245 expression second operand if necessary. */
7246
7247tree
7248java_method_add_stmt (fndecl, expr)
7249 tree fndecl, expr;
7250{
b771925e
APB
7251 if (!GET_CURRENT_BLOCK (fndecl))
7252 return NULL_TREE;
f099f336 7253 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7254}
e04a16fb 7255
b67d701b
PB
7256static tree
7257add_stmt_to_block (b, type, stmt)
7258 tree b, type, stmt;
7259{
7260 tree body = BLOCK_EXPR_BODY (b), c;
7261
e04a16fb
AG
7262 if (java_error_count)
7263 return body;
b67d701b
PB
7264
7265 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7266 return body;
7267
b67d701b
PB
7268 BLOCK_EXPR_BODY (b) = c;
7269 TREE_SIDE_EFFECTS (c) = 1;
7270 return c;
e04a16fb
AG
7271}
7272
7273/* Add STMT to EXISTING if possible, otherwise create a new
7274 COMPOUND_EXPR and add STMT to it. */
7275
7276static tree
7277add_stmt_to_compound (existing, type, stmt)
7278 tree existing, type, stmt;
7279{
15fdcfe9
PB
7280 if (existing)
7281 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7282 else
15fdcfe9 7283 return stmt;
e04a16fb
AG
7284}
7285
1886c9d8
APB
7286void java_layout_seen_class_methods ()
7287{
7288 tree previous_list = all_class_list;
7289 tree end = NULL_TREE;
7290 tree current;
7291
7292 while (1)
7293 {
7294 for (current = previous_list;
7295 current != end; current = TREE_CHAIN (current))
7296 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7297
7298 if (previous_list != all_class_list)
7299 {
7300 end = previous_list;
7301 previous_list = all_class_list;
7302 }
7303 else
7304 break;
7305 }
7306}
7307
e04a16fb 7308void
c2952b01 7309java_reorder_fields ()
e04a16fb 7310{
c2952b01 7311 static tree stop_reordering = NULL_TREE;
19e223db 7312 static int initialized_p;
c2952b01 7313 tree current;
19e223db
MM
7314
7315 /* Register STOP_REORDERING with the garbage collector. */
7316 if (!initialized_p)
7317 {
7318 ggc_add_tree_root (&stop_reordering, 1);
7319 initialized_p = 1;
7320 }
7321
5e942c50 7322 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7323 {
5e942c50 7324 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7325
c2952b01
APB
7326 if (current_class == stop_reordering)
7327 break;
7328
c877974e
APB
7329 /* Reverse the fields, but leave the dummy field in front.
7330 Fields are already ordered for Object and Class */
7331 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7332 && current_class != class_type_node)
7333 {
23a79c61
APB
7334 /* If the dummy field is there, reverse the right fields and
7335 just layout the type for proper fields offset */
c877974e
APB
7336 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7337 {
7338 tree fields = TYPE_FIELDS (current_class);
7339 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7340 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7341 }
23a79c61
APB
7342 /* We don't have a dummy field, we need to layout the class,
7343 after having reversed the fields */
c877974e
APB
7344 else
7345 {
7346 TYPE_FIELDS (current_class) =
7347 nreverse (TYPE_FIELDS (current_class));
7348 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7349 }
7350 }
c2952b01
APB
7351 }
7352 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7353}
7354
7355/* Layout the methods of all classes loaded in one way on an
7356 other. Check methods of source parsed classes. Then reorder the
7357 fields and layout the classes or the type of all source parsed
7358 classes */
7359
7360void
7361java_layout_classes ()
7362{
7363 tree current;
7364 int save_error_count = java_error_count;
7365
7366 /* Layout the methods of all classes seen so far */
7367 java_layout_seen_class_methods ();
7368 java_parse_abort_on_error ();
7369 all_class_list = NULL_TREE;
7370
7371 /* Then check the methods of all parsed classes */
7372 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7373 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7374 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7375 java_parse_abort_on_error ();
7376
7377 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7378 {
7379 current_class = TREE_TYPE (TREE_VALUE (current));
7380 layout_class (current_class);
5e942c50 7381
c877974e
APB
7382 /* From now on, the class is considered completely loaded */
7383 CLASS_LOADED_P (current_class) = 1;
7384
5e942c50
APB
7385 /* Error reported by the caller */
7386 if (java_error_count)
7387 return;
e04a16fb 7388 }
23a79c61
APB
7389
7390 /* We might have reloaded classes durign the process of laying out
7391 classes for code generation. We must layout the methods of those
7392 late additions, as constructor checks might use them */
1886c9d8 7393 java_layout_seen_class_methods ();
23a79c61 7394 java_parse_abort_on_error ();
e04a16fb
AG
7395}
7396
c2952b01
APB
7397/* Expand methods in the current set of classes rememebered for
7398 generation. */
e04a16fb 7399
49f48c71 7400static void
c2952b01 7401java_complete_expand_classes ()
e04a16fb
AG
7402{
7403 tree current;
ce6e9147
APB
7404
7405 do_not_fold = flag_emit_xref;
c2952b01 7406
e04a16fb 7407 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7408 if (!INNER_CLASS_DECL_P (current))
7409 java_complete_expand_class (current);
7410}
e04a16fb 7411
c2952b01
APB
7412/* Expand the methods found in OUTER, starting first by OUTER's inner
7413 classes, if any. */
e04a16fb 7414
c2952b01
APB
7415static void
7416java_complete_expand_class (outer)
7417 tree outer;
7418{
7419 tree inner_list;
e04a16fb 7420
c2952b01 7421 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7422
c2952b01
APB
7423 /* We need to go after all inner classes and start expanding them,
7424 starting with most nested ones. We have to do that because nested
7425 classes might add functions to outer classes */
e04a16fb 7426
c2952b01
APB
7427 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7428 inner_list; inner_list = TREE_CHAIN (inner_list))
7429 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7430
c2952b01
APB
7431 java_complete_expand_methods (outer);
7432 set_nested_class_simple_name_value (outer, 0); /* Reset */
7433}
7434
7435/* Expand methods registered in CLASS_DECL. The general idea is that
7436 we expand regular methods first. This allows us get an estimate on
7437 how outer context local alias fields are really used so we can add
7438 to the constructor just enough code to initialize them properly (it
c00f0fb2 7439 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7440 constructors and then <clinit>. */
7441
7442static void
7443java_complete_expand_methods (class_decl)
7444 tree class_decl;
7445{
7446 tree clinit, finit, decl, first_decl;
7447
7448 current_class = TREE_TYPE (class_decl);
7449
7450 /* Initialize a new constant pool */
7451 init_outgoing_cpool ();
7452
7453 /* Pre-expand <clinit> to figure whether we really need it or
7454 not. If we do need it, we pre-expand the static fields so they're
7455 ready to be used somewhere else. <clinit> will be fully expanded
7456 after we processed the constructors. */
7457 first_decl = TYPE_METHODS (current_class);
7458 clinit = maybe_generate_pre_expand_clinit (current_class);
7459
c00f0fb2 7460 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7461 try to use it.*/
7462 if (TYPE_FINIT_STMT_LIST (current_class))
7463 {
7464 finit = generate_finit (current_class);
7465 java_complete_expand_method (finit);
7466 }
7467
7468 /* Now do the constructors */
7469 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7470 {
7471 int no_body;
7472
7473 if (!DECL_CONSTRUCTOR_P (decl))
7474 continue;
7475
7476 no_body = !DECL_FUNCTION_BODY (decl);
7477 /* Don't generate debug info on line zero when expanding a
7478 generated constructor. */
7479 if (no_body)
7480 restore_line_number_status (1);
7481
7482 java_complete_expand_method (decl);
7483
7484 if (no_body)
7485 restore_line_number_status (0);
7486 }
7487
7488 /* First, do the ordinary methods. */
7489 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7490 {
7145d9fe
TT
7491 /* Skip abstract or native methods -- but do handle native
7492 methods when generating JNI stubs. */
7493 if (METHOD_ABSTRACT (decl)
7494 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7495 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7496 continue;
7145d9fe
TT
7497
7498 if (METHOD_NATIVE (decl))
7499 {
7500 tree body = build_jni_stub (decl);
7501 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7502 }
7503
c2952b01
APB
7504 java_complete_expand_method (decl);
7505 }
7506
7507 /* If there is indeed a <clinit>, fully expand it now */
7508 if (clinit)
7509 {
7510 /* Prevent the use of `this' inside <clinit> */
7511 ctxp->explicit_constructor_p = 1;
7512 java_complete_expand_method (clinit);
7513 ctxp->explicit_constructor_p = 0;
e04a16fb 7514 }
c2952b01 7515
165f37bc
APB
7516 /* We might have generated a class$ that we now want to expand */
7517 if (TYPE_DOT_CLASS (current_class))
7518 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7519
c2952b01
APB
7520 /* Now verify constructor circularity (stop after the first one we
7521 prove wrong.) */
7522 if (!CLASS_INTERFACE (class_decl))
7523 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7524 if (DECL_CONSTRUCTOR_P (decl)
7525 && verify_constructor_circularity (decl, decl))
7526 break;
7527
7528 /* Save the constant pool. We'll need to restore it later. */
7529 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7530}
7531
c2952b01
APB
7532/* Attempt to create <clinit>. Pre-expand static fields so they can be
7533 safely used in some other methods/constructors. */
e920ebc9 7534
c2952b01
APB
7535static tree
7536maybe_generate_pre_expand_clinit (class_type)
7537 tree class_type;
e920ebc9 7538{
c2952b01
APB
7539 tree current, mdecl;
7540
7541 if (!TYPE_CLINIT_STMT_LIST (class_type))
7542 return NULL_TREE;
e920ebc9 7543
c2952b01
APB
7544 /* Go through all static fields and pre expand them */
7545 for (current = TYPE_FIELDS (class_type); current;
7546 current = TREE_CHAIN (current))
7547 if (FIELD_STATIC (current))
7548 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7549
7550 /* Then build the <clinit> method */
7551 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7552 clinit_identifier_node, end_params_node);
7553 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7554 mdecl, NULL_TREE);
7555 start_artificial_method_body (mdecl);
7556
7557 /* We process the list of assignment we produced as the result of
7558 the declaration of initialized static field and add them as
7559 statement to the <clinit> method. */
7560 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7561 current = TREE_CHAIN (current))
e920ebc9 7562 {
9a7ab4b3 7563 tree stmt = current;
c2952b01
APB
7564 /* We build the assignment expression that will initialize the
7565 field to its value. There are strict rules on static
7566 initializers (8.5). FIXME */
98a52c2c 7567 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7568 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7569 java_method_add_stmt (mdecl, stmt);
7570 }
e920ebc9 7571
c2952b01
APB
7572 end_artificial_method_body (mdecl);
7573
92d83515
APB
7574 /* Now we want to place <clinit> as the last method (because we need
7575 it at least for interface so that it doesn't interfere with the
7576 dispatch table based lookup. */
7577 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7578 {
92d83515
APB
7579 current = TREE_CHAIN (TYPE_METHODS (class_type));
7580 TYPE_METHODS (class_type) = current;
c2952b01
APB
7581
7582 while (TREE_CHAIN (current))
7583 current = TREE_CHAIN (current);
92d83515 7584
c2952b01
APB
7585 TREE_CHAIN (current) = mdecl;
7586 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7587 }
c2952b01
APB
7588
7589 return mdecl;
e920ebc9
APB
7590}
7591
92d83515
APB
7592/* See whether we could get rid of <clinit>. Criteria are: all static
7593 final fields have constant initial values and the body of <clinit>
7594 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7595
7596static int
7597maybe_yank_clinit (mdecl)
7598 tree mdecl;
7599{
7600 tree type, current;
7601 tree fbody, bbody;
7602
7603 if (!DECL_CLINIT_P (mdecl))
7604 return 0;
f0f3a777
APB
7605
7606 /* If the body isn't empty, then we keep <clinit>. Note that if
7607 we're emitting classfiles, this isn't enough not to rule it
7608 out. */
92d83515
APB
7609 fbody = DECL_FUNCTION_BODY (mdecl);
7610 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7611 bbody = BLOCK_EXPR_BODY (bbody);
f0f3a777 7612 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7613 return 0;
7614
7615 type = DECL_CONTEXT (mdecl);
7616 current = TYPE_FIELDS (type);
7617
7618 for (current = (current ? TREE_CHAIN (current) : current);
7619 current; current = TREE_CHAIN (current))
f0f3a777
APB
7620 {
7621 tree f_init;
7622
7623 /* We're not interested in non static field */
7624 if (!FIELD_STATIC (current))
7625 continue;
7626
7627 /* Anything that isn't String or a basic type is ruled out -- or
7628 if we now how to deal with it (when doing things natively) we
7629 should generated an empty <clinit> so that SUID are computed
7630 correctly. */
7631 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7632 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7633 break;
7634
7635 f_init = DECL_INITIAL (current);
7636 /* If we're emitting native code, we want static final fields to
7637 have constant initializers. If we don't meet these
7638 conditions, we keep <clinit> */
7639 if (!flag_emit_class_files
7640 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7641 break;
7642 /* If we're emitting bytecode, we want static fields to have
7643 constant initializers or no initializer. If we don't meet
7644 these conditions, we keep <clinit> */
7645 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7646 break;
7647 }
92d83515
APB
7648
7649 if (current)
7650 return 0;
7651
7652 /* Get rid of <clinit> in the class' list of methods */
7653 if (TYPE_METHODS (type) == mdecl)
7654 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7655 else
7656 for (current = TYPE_METHODS (type); current;
7657 current = TREE_CHAIN (current))
7658 if (TREE_CHAIN (current) == mdecl)
7659 {
7660 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7661 break;
7662 }
7663
7664 return 1;
7665}
7666
7667
e04a16fb
AG
7668/* Complete and expand a method. */
7669
7670static void
7671java_complete_expand_method (mdecl)
7672 tree mdecl;
7673{
92d83515
APB
7674 int yank_clinit = 0;
7675
c2952b01 7676 current_function_decl = mdecl;
22eed1e6
APB
7677 /* Fix constructors before expanding them */
7678 if (DECL_CONSTRUCTOR_P (mdecl))
7679 fix_constructors (mdecl);
e04a16fb 7680
22eed1e6 7681 /* Expand functions that have a body */
e04a16fb
AG
7682 if (DECL_FUNCTION_BODY (mdecl))
7683 {
9bbc7d9f
PB
7684 tree fbody = DECL_FUNCTION_BODY (mdecl);
7685 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7686 tree exception_copy = NULL_TREE;
e04a16fb 7687 expand_start_java_method (mdecl);
939d7216 7688 build_result_decl (mdecl);
e04a16fb
AG
7689
7690 current_this
7691 = (!METHOD_STATIC (mdecl) ?
7692 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7693
ce6e9147
APB
7694 /* Purge the `throws' list of unchecked exceptions. If we're
7695 doing xref, save a copy of the list and re-install it
7696 later. */
7697 if (flag_emit_xref)
7698 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7699
b9f7e36c
APB
7700 purge_unchecked_exceptions (mdecl);
7701
7702 /* Install exceptions thrown with `throws' */
7703 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7704
9bbc7d9f 7705 if (block_body != NULL_TREE)
bc3ca41b
PB
7706 {
7707 block_body = java_complete_tree (block_body);
c2952b01 7708
7145d9fe 7709 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7710 check_for_initialization (block_body);
f099f336 7711 ctxp->explicit_constructor_p = 0;
bc3ca41b 7712 }
e803d3b2 7713
9bbc7d9f 7714 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7715
c2952b01
APB
7716 /* If we saw a return but couldn't evaluate it properly, we'll
7717 have an error_mark_node here. */
7718 if (block_body != error_mark_node
7719 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7720 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7721 && !flag_emit_xref)
82371d41 7722 missing_return_error (current_function_decl);
7525cc04 7723
92d83515
APB
7724 /* Check wether we could just get rid of clinit, now the picture
7725 is complete. */
7726 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7727 complete_start_java_method (mdecl);
7728
e04a16fb 7729 /* Don't go any further if we've found error(s) during the
92d83515
APB
7730 expansion */
7731 if (!java_error_count && !yank_clinit)
e04a16fb 7732 source_end_java_method ();
22eed1e6
APB
7733 else
7734 {
92d83515
APB
7735 if (java_error_count)
7736 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7737 poplevel (1, 0, 1);
7738 }
b9f7e36c
APB
7739
7740 /* Pop the exceptions and sanity check */
7741 POP_EXCEPTIONS();
7742 if (currently_caught_type_list)
7743 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7744
7745 if (flag_emit_xref)
7746 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7747 }
7748}
7749
c2952b01
APB
7750\f
7751
7752/* This section of the code deals with accessing enclosing context
7753 fields either directly by using the relevant access to this$<n> or
7754 by invoking an access method crafted for that purpose. */
7755
7756/* Build the necessary access from an inner class to an outer
7757 class. This routine could be optimized to cache previous result
7758 (decl, current_class and returned access). When an access method
7759 needs to be generated, it always takes the form of a read. It might
7760 be later turned into a write by calling outer_field_access_fix. */
7761
7762static tree
7763build_outer_field_access (id, decl)
7764 tree id, decl;
7765{
7766 tree access = NULL_TREE;
7767 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7768
7769 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7770 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7771 the `private' barrier if we're not emitting bytecodes. */
7772 if (ctx == DECL_CONTEXT (decl)
7773 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7774 {
7775 tree thisn = build_current_thisn (current_class);
7776 access = make_qualified_primary (build_wfl_node (thisn),
7777 id, EXPR_WFL_LINECOL (id));
7778 }
7779 /* Otherwise, generate access methods to outer this and access the
7780 field (either using an access method or by direct access.) */
7781 else
7782 {
7783 int lc = EXPR_WFL_LINECOL (id);
7784
7785 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7786 get a hold to the enclosing instance we need, and then we
c2952b01
APB
7787 build the field access. */
7788 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7789
7790 /* If the field is private and we're generating bytecode, then
7791 we generate an access method */
7792 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7793 {
7794 tree name = build_outer_field_access_methods (decl);
7795 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7796 name, access, NULL_TREE);
7797 }
7798 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7799 Once again we break the `private' access rule from a foreign
7800 class. */
7801 else
7802 access = make_qualified_primary (access, id, lc);
7803 }
7804 return resolve_expression_name (access, NULL);
7805}
7806
7807/* Return a non zero value if NODE describes an outer field inner
7808 access. */
7809
7810static int
7811outer_field_access_p (type, decl)
7812 tree type, decl;
7813{
7814 if (!INNER_CLASS_TYPE_P (type)
7815 || TREE_CODE (decl) != FIELD_DECL
7816 || DECL_CONTEXT (decl) == type)
7817 return 0;
7818
7819 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7820 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7821 {
7822 if (type == DECL_CONTEXT (decl))
7823 return 1;
7824 if (!DECL_CONTEXT (TYPE_NAME (type)))
7825 break;
7826 }
7827
7828 return 0;
7829}
7830
7831/* Return a non zero value if NODE represents an outer field inner
7832 access that was been already expanded. As a side effect, it returns
7833 the name of the field being accessed and the argument passed to the
7834 access function, suitable for a regeneration of the access method
7835 call if necessary. */
7836
7837static int
7838outer_field_expanded_access_p (node, name, arg_type, arg)
7839 tree node, *name, *arg_type, *arg;
7840{
7841 int identified = 0;
7842
7843 if (TREE_CODE (node) != CALL_EXPR)
7844 return 0;
7845
7846 /* Well, gcj generates slightly different tree nodes when compiling
7847 to native or bytecodes. It's the case for function calls. */
7848
7849 if (flag_emit_class_files
7850 && TREE_CODE (node) == CALL_EXPR
7851 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7852 identified = 1;
7853 else if (!flag_emit_class_files)
7854 {
7855 node = TREE_OPERAND (node, 0);
7856
7857 if (node && TREE_OPERAND (node, 0)
7858 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7859 {
7860 node = TREE_OPERAND (node, 0);
7861 if (TREE_OPERAND (node, 0)
7862 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7863 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7864 (DECL_NAME (TREE_OPERAND (node, 0)))))
7865 identified = 1;
7866 }
7867 }
7868
7869 if (identified && name && arg_type && arg)
7870 {
7871 tree argument = TREE_OPERAND (node, 1);
7872 *name = DECL_NAME (TREE_OPERAND (node, 0));
7873 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7874 *arg = TREE_VALUE (argument);
7875 }
7876 return identified;
7877}
7878
7879/* Detect in NODE an outer field read access from an inner class and
7880 transform it into a write with RHS as an argument. This function is
7881 called from the java_complete_lhs when an assignment to a LHS can
7882 be identified. */
7883
7884static tree
7885outer_field_access_fix (wfl, node, rhs)
7886 tree wfl, node, rhs;
7887{
7888 tree name, arg_type, arg;
7889
7890 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7891 {
7892 /* At any rate, check whether we're trying to assign a value to
7893 a final. */
7894 tree accessed = (JDECL_P (node) ? node :
7895 (TREE_CODE (node) == COMPONENT_REF ?
7896 TREE_OPERAND (node, 1) : node));
7897 if (check_final_assignment (accessed, wfl))
7898 return error_mark_node;
7899
7900 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7901 arg_type, name, arg, rhs);
7902 return java_complete_tree (node);
7903 }
7904 return NULL_TREE;
7905}
7906
7907/* Construct the expression that calls an access method:
7908 <type>.access$<n>(<arg1> [, <arg2>]);
7909
7910 ARG2 can be NULL and will be omitted in that case. It will denote a
7911 read access. */
7912
7913static tree
7914build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7915 int lc;
7916 tree type, access_method_name, arg1, arg2;
7917{
7918 tree args, cn, access;
7919
7920 args = arg1 ? arg1 :
7921 build_wfl_node (build_current_thisn (current_class));
7922 args = build_tree_list (NULL_TREE, args);
7923
7924 if (arg2)
7925 args = tree_cons (NULL_TREE, arg2, args);
7926
7927 access = build_method_invocation (build_wfl_node (access_method_name), args);
7928 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7929 return make_qualified_primary (cn, access, lc);
7930}
7931
7932static tree
7933build_new_access_id ()
7934{
7935 static int access_n_counter = 1;
7936 char buffer [128];
7937
7938 sprintf (buffer, "access$%d", access_n_counter++);
7939 return get_identifier (buffer);
7940}
7941
7942/* Create the static access functions for the outer field DECL. We define a
7943 read:
7944 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7945 return inst$.field;
7946 }
7947 and a write access:
7948 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7949 TREE_TYPE (<field>) value$) {
7950 return inst$.field = value$;
7951 }
7952 We should have a usage flags on the DECL so we can lazily turn the ones
7953 we're using for code generation. FIXME.
7954*/
7955
7956static tree
7957build_outer_field_access_methods (decl)
7958 tree decl;
7959{
7960 tree id, args, stmt, mdecl;
7961
7962 /* Check point, to be removed. FIXME */
7963 if (FIELD_INNER_ACCESS (decl)
7964 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7965 abort ();
7966
7967 if (FIELD_INNER_ACCESS (decl))
7968 return FIELD_INNER_ACCESS (decl);
7969
7970 push_obstacks (&permanent_obstack, &permanent_obstack);
7971
7972 /* Create the identifier and a function named after it. */
7973 id = build_new_access_id ();
7974
7975 /* The identifier is marked as bearing the name of a generated write
7976 access function for outer field accessed from inner classes. */
7977 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7978
7979 /* Create the read access */
7980 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7981 TREE_CHAIN (args) = end_params_node;
7982 stmt = make_qualified_primary (build_wfl_node (inst_id),
7983 build_wfl_node (DECL_NAME (decl)), 0);
7984 stmt = build_return (0, stmt);
7985 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7986 TREE_TYPE (decl), id, args, stmt);
7987 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7988
7989 /* Create the write access method */
7990 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7991 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7992 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7993 stmt = make_qualified_primary (build_wfl_node (inst_id),
7994 build_wfl_node (DECL_NAME (decl)), 0);
7995 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7996 build_wfl_node (wpv_id)));
7997
7998 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7999 TREE_TYPE (decl), id, args, stmt);
8000 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8001 pop_obstacks ();
8002
8003 /* Return the access name */
8004 return FIELD_INNER_ACCESS (decl) = id;
8005}
8006
8007/* Build an field access method NAME. */
8008
8009static tree
8010build_outer_field_access_method (class, type, name, args, body)
8011 tree class, type, name, args, body;
8012{
8013 tree saved_current_function_decl, mdecl;
8014
8015 /* Create the method */
8016 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8017 fix_method_argument_names (args, mdecl);
8018 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8019
8020 /* Attach the method body. */
8021 saved_current_function_decl = current_function_decl;
8022 start_artificial_method_body (mdecl);
8023 java_method_add_stmt (mdecl, body);
8024 end_artificial_method_body (mdecl);
8025 current_function_decl = saved_current_function_decl;
8026
8027 return mdecl;
8028}
8029
8030\f
8031/* This section deals with building access function necessary for
8032 certain kinds of method invocation from inner classes. */
8033
8034static tree
8035build_outer_method_access_method (decl)
8036 tree decl;
8037{
8038 tree saved_current_function_decl, mdecl;
8039 tree args = NULL_TREE, call_args = NULL_TREE;
8040 tree carg, id, body, class;
8041 char buffer [80];
8042 int parm_id_count = 0;
8043
8044 /* Test this abort with an access to a private field */
8045 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8046 abort ();
8047
8048 /* Check the cache first */
8049 if (DECL_FUNCTION_INNER_ACCESS (decl))
8050 return DECL_FUNCTION_INNER_ACCESS (decl);
8051
8052 class = DECL_CONTEXT (decl);
8053
8054 /* Obtain an access identifier and mark it */
8055 id = build_new_access_id ();
8056 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8057
8058 push_obstacks (&permanent_obstack, &permanent_obstack);
8059
8060 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8061 /* Create the arguments, as much as the original */
8062 for (; carg && carg != end_params_node;
8063 carg = TREE_CHAIN (carg))
8064 {
8065 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8066 args = chainon (args, build_tree_list (get_identifier (buffer),
8067 TREE_VALUE (carg)));
8068 }
8069 args = chainon (args, end_params_node);
8070
8071 /* Create the method */
8072 mdecl = create_artificial_method (class, ACC_STATIC,
8073 TREE_TYPE (TREE_TYPE (decl)), id, args);
8074 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8075 /* There is a potential bug here. We should be able to use
8076 fix_method_argument_names, but then arg names get mixed up and
8077 eventually a constructor will have its this$0 altered and the
8078 outer context won't be assignment properly. The test case is
8079 stub.java FIXME */
8080 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8081
8082 /* Attach the method body. */
8083 saved_current_function_decl = current_function_decl;
8084 start_artificial_method_body (mdecl);
8085
8086 /* The actual method invocation uses the same args. When invoking a
8087 static methods that way, we don't want to skip the first
8088 argument. */
8089 carg = args;
8090 if (!METHOD_STATIC (decl))
8091 carg = TREE_CHAIN (carg);
8092 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8093 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8094 call_args);
8095
8096 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8097 call_args);
8098 if (!METHOD_STATIC (decl))
8099 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8100 body, 0);
8101 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8102 body = build_return (0, body);
8103 java_method_add_stmt (mdecl,body);
8104 end_artificial_method_body (mdecl);
8105 current_function_decl = saved_current_function_decl;
8106 pop_obstacks ();
8107
8108 /* Back tag the access function so it know what it accesses */
8109 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8110
8111 /* Tag the current method so it knows it has an access generated */
8112 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8113}
8114
8115\f
8116/* This section of the code deals with building expressions to access
8117 the enclosing instance of an inner class. The enclosing instance is
8118 kept in a generated field called this$<n>, with <n> being the
8119 inner class nesting level (starting from 0.) */
8120
8121/* Build an access to a given this$<n>, possibly by chaining access
8122 call to others. Access methods to this$<n> are build on the fly if
8123 necessary */
8124
8125static tree
8126build_access_to_thisn (from, to, lc)
8127 tree from, to;
8128 int lc;
8129{
8130 tree access = NULL_TREE;
8131
8132 while (from != to)
8133 {
8134 tree access0_wfl, cn;
8135
8136 maybe_build_thisn_access_method (from);
8137 access0_wfl = build_wfl_node (access0_identifier_node);
8138 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8139 EXPR_WFL_LINECOL (access0_wfl) = lc;
8140
8141 if (!access)
8142 {
8143 access = build_current_thisn (current_class);
8144 access = build_wfl_node (access);
8145 }
8146 access = build_tree_list (NULL_TREE, access);
8147 access = build_method_invocation (access0_wfl, access);
8148 access = make_qualified_primary (cn, access, lc);
8149
8150 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8151 }
8152 return access;
8153}
8154
8155/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8156 is returned if nothing needs to be generated. Otherwise, the method
152de068 8157 generated and a method decl is returned.
c2952b01
APB
8158
8159 NOTE: These generated methods should be declared in a class file
8160 attribute so that they can't be referred to directly. */
8161
8162static tree
8163maybe_build_thisn_access_method (type)
8164 tree type;
8165{
8166 tree mdecl, args, stmt, rtype;
8167 tree saved_current_function_decl;
8168
8169 /* If TYPE is a top-level class, no access method is required.
8170 If there already is such an access method, bail out. */
8171 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8172 return NULL_TREE;
8173
8174 /* We generate the method. The method looks like:
8175 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8176 */
8177 push_obstacks (&permanent_obstack, &permanent_obstack);
8178 args = build_tree_list (inst_id, build_pointer_type (type));
8179 TREE_CHAIN (args) = end_params_node;
8180 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8181 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8182 access0_identifier_node, args);
8183 fix_method_argument_names (args, mdecl);
8184 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8185 stmt = build_current_thisn (type);
8186 stmt = make_qualified_primary (build_wfl_node (inst_id),
8187 build_wfl_node (stmt), 0);
8188 stmt = build_return (0, stmt);
8189
8190 saved_current_function_decl = current_function_decl;
8191 start_artificial_method_body (mdecl);
8192 java_method_add_stmt (mdecl, stmt);
8193 end_artificial_method_body (mdecl);
8194 current_function_decl = saved_current_function_decl;
8195 pop_obstacks ();
8196
8197 CLASS_ACCESS0_GENERATED_P (type) = 1;
8198
8199 return mdecl;
8200}
8201
8202/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8203 the first level of innerclassing. this$1 for the next one, etc...
8204 This function can be invoked with TYPE to NULL, available and then
8205 has to count the parser context. */
8206
8207static tree
8208build_current_thisn (type)
8209 tree type;
8210{
8211 static int saved_i = -1;
8212 static tree saved_thisn = NULL_TREE;
19e223db
MM
8213 static tree saved_type = NULL_TREE;
8214 static int saved_type_i = 0;
8215 static int initialized_p;
c2952b01
APB
8216 tree decl;
8217 char buffer [80];
8218 int i = 0;
8219
19e223db
MM
8220 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8221 if (!initialized_p)
c2952b01 8222 {
19e223db
MM
8223 ggc_add_tree_root (&saved_thisn, 1);
8224 ggc_add_tree_root (&saved_type, 1);
8225 initialized_p = 1;
8226 }
c2952b01 8227
19e223db
MM
8228 if (type)
8229 {
c2952b01
APB
8230 if (type == saved_type)
8231 i = saved_type_i;
8232 else
8233 {
8234 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8235 decl; decl = DECL_CONTEXT (decl), i++)
8236 ;
8237
8238 saved_type = type;
8239 saved_type_i = i;
8240 }
8241 }
8242 else
8243 i = list_length (GET_CPC_LIST ())-2;
8244
8245 if (i == saved_i)
8246 return saved_thisn;
8247
8248 sprintf (buffer, "this$%d", i);
8249 saved_i = i;
8250 saved_thisn = get_identifier (buffer);
8251 return saved_thisn;
8252}
8253
8254/* Return the assignement to the hidden enclosing context `this$<n>'
8255 by the second incoming parameter to the innerclass constructor. The
8256 form used is `this.this$<n> = this$<n>;'. */
8257
8258static tree
8259build_thisn_assign ()
8260{
8261 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8262 {
8263 tree thisn = build_current_thisn (current_class);
8264 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8265 build_wfl_node (thisn), 0);
8266 tree rhs = build_wfl_node (thisn);
8267 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8268 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8269 }
8270 return NULL_TREE;
8271}
8272
8273\f
165f37bc
APB
8274/* Building the synthetic `class$' used to implement the `.class' 1.1
8275 extension for non primitive types. This method looks like:
8276
8277 static Class class$(String type) throws NoClassDefFoundError
8278 {
8279 try {return (java.lang.Class.forName (String));}
8280 catch (ClassNotFoundException e) {
8281 throw new NoClassDefFoundError(e.getMessage());}
8282 } */
8283
8284static tree
8285build_dot_class_method (class)
8286 tree class;
8287{
8288#define BWF(S) build_wfl_node (get_identifier ((S)))
8289#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8290 tree args, tmp, saved_current_function_decl, mdecl;
8291 tree stmt, throw_stmt, catch, catch_block, try_block;
8292 tree catch_clause_param;
8293 tree class_not_found_exception, no_class_def_found_error;
8294
8295 static tree get_message_wfl, type_parm_wfl;
8296
8297 if (!get_message_wfl)
8298 {
8299 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8300 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8301 ggc_add_tree_root (&get_message_wfl, 1);
8302 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8303 }
8304
8305 /* Build the arguments */
8306 args = build_tree_list (get_identifier ("type$"),
8307 build_pointer_type (string_type_node));
8308 TREE_CHAIN (args) = end_params_node;
8309
8310 /* Build the qualified name java.lang.Class.forName */
8311 tmp = MQN (MQN (MQN (BWF ("java"),
8312 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8313
8314 /* For things we have to catch and throw */
8315 class_not_found_exception =
8316 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8317 no_class_def_found_error =
8318 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8319 load_class (class_not_found_exception, 1);
8320 load_class (no_class_def_found_error, 1);
8321
8322 /* Create the "class$" function */
8323 mdecl = create_artificial_method (class, ACC_STATIC,
8324 build_pointer_type (class_type_node),
8325 get_identifier ("class$"), args);
8326 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8327 no_class_def_found_error);
8328
8329 /* We start by building the try block. We need to build:
8330 return (java.lang.Class.forName (type)); */
8331 stmt = build_method_invocation (tmp,
8332 build_tree_list (NULL_TREE, type_parm_wfl));
8333 stmt = build_return (0, stmt);
8334 /* Put it in a block. That's the try block */
8335 try_block = build_expr_block (stmt, NULL_TREE);
8336
8337 /* Now onto the catch block. We start by building the expression
8338 throwing a new exception:
8339 throw new NoClassDefFoundError (_.getMessage); */
8340 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8341 get_message_wfl, 0);
8342 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8343
8344 /* Build new NoClassDefFoundError (_.getMessage) */
8345 throw_stmt = build_new_invocation
8346 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8347 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8348
8349 /* Build the throw, (it's too early to use BUILD_THROW) */
8350 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8351
8352 /* Build the catch block to encapsulate all this. We begin by
8353 building an decl for the catch clause parameter and link it to
8354 newly created block, the catch block. */
8355 catch_clause_param =
8356 build_decl (VAR_DECL, wpv_id,
8357 build_pointer_type (class_not_found_exception));
8358 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8359
8360 /* We initialize the variable with the exception handler. */
8361 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8362 soft_exceptioninfo_call_node);
8363 add_stmt_to_block (catch_block, NULL_TREE, catch);
8364
8365 /* We add the statement throwing the new exception */
8366 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8367
8368 /* Build a catch expression for all this */
8369 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8370
8371 /* Build the try/catch sequence */
8372 stmt = build_try_statement (0, try_block, catch_block);
8373
8374 fix_method_argument_names (args, mdecl);
8375 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8376 saved_current_function_decl = current_function_decl;
8377 start_artificial_method_body (mdecl);
8378 java_method_add_stmt (mdecl, stmt);
8379 end_artificial_method_body (mdecl);
8380 current_function_decl = saved_current_function_decl;
8381 TYPE_DOT_CLASS (class) = mdecl;
8382
8383 return mdecl;
8384}
8385
8386static tree
f0f3a777
APB
8387build_dot_class_method_invocation (type)
8388 tree type;
165f37bc 8389{
f0f3a777
APB
8390 tree sig_id, s;
8391
8392 if (TYPE_ARRAY_P (type))
8393 sig_id = build_java_signature (type);
8394 else
8395 sig_id = DECL_NAME (TYPE_NAME (type));
8396
8397 s = make_node (STRING_CST);
8398 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (sig_id);
165f37bc
APB
8399 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8400 TREE_STRING_LENGTH (s)+1);
f0f3a777 8401 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8402 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8403 build_tree_list (NULL_TREE, s));
8404}
8405
c2952b01
APB
8406/* This section of the code deals with constructor. */
8407
22eed1e6
APB
8408/* Craft a body for default constructor. Patch existing constructor
8409 bodies with call to super() and field initialization statements if
8410 necessary. */
8411
8412static void
8413fix_constructors (mdecl)
8414 tree mdecl;
8415{
8416 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8417 tree thisn_assign, compound = NULL_TREE;
8418 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8419
22eed1e6
APB
8420 if (!body)
8421 {
22eed1e6
APB
8422 /* It is an error for the compiler to generate a default
8423 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8424 takes no argument, or the same args for an anonymous class */
8425 if (verify_constructor_super (mdecl))
22eed1e6 8426 {
c2952b01
APB
8427 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8428 tree save = DECL_NAME (mdecl);
49f48c71 8429 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8430 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8431 parse_error_context
c2952b01
APB
8432 (lookup_cl (TYPE_NAME (class_type)),
8433 "No constructor matching `%s' found in class `%s'",
8434 lang_printable_name (mdecl, 0), n);
8435 DECL_NAME (mdecl) = save;
22eed1e6
APB
8436 }
8437
c2952b01
APB
8438 /* The constructor body must be crafted by hand. It's the
8439 constructor we defined when we realize we didn't have the
8440 CLASSNAME() constructor */
22eed1e6
APB
8441 start_artificial_method_body (mdecl);
8442
f0f3a777
APB
8443 /* Insert an assignment to the this$<n> hidden field, if
8444 necessary */
8445 if ((thisn_assign = build_thisn_assign ()))
8446 java_method_add_stmt (mdecl, thisn_assign);
8447
22eed1e6
APB
8448 /* We don't generate a super constructor invocation if we're
8449 compiling java.lang.Object. build_super_invocation takes care
8450 of that. */
e920ebc9 8451 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8452
c2952b01
APB
8453 /* Insert the instance initializer block right here, after the
8454 super invocation. */
8455 add_instance_initializer (mdecl);
8456
22eed1e6
APB
8457 end_artificial_method_body (mdecl);
8458 }
8459 /* Search for an explicit constructor invocation */
8460 else
8461 {
8462 int found = 0;
8463 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8464
8465 while (body)
8466 switch (TREE_CODE (body))
8467 {
8468 case CALL_EXPR:
8469 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8470 body = NULL_TREE;
8471 break;
8472 case COMPOUND_EXPR:
8473 case EXPR_WITH_FILE_LOCATION:
8474 body = TREE_OPERAND (body, 0);
8475 break;
8476 case BLOCK:
8477 body = BLOCK_EXPR_BODY (body);
8478 break;
8479 default:
8480 found = 0;
8481 body = NULL_TREE;
8482 }
8483 /* The constructor is missing an invocation of super() */
8484 if (!found)
8485 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8486 build_super_invocation (mdecl));
22eed1e6 8487
c2952b01
APB
8488 /* Generate the assignment to this$<n>, if necessary */
8489 if ((thisn_assign = build_thisn_assign ()))
8490 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8491
f0f3a777
APB
8492 /* Insert the instance initializer block right here, after the
8493 super invocation. */
8494 add_instance_initializer (mdecl);
8495
22eed1e6
APB
8496 /* Fix the constructor main block if we're adding extra stmts */
8497 if (compound)
8498 {
8499 compound = add_stmt_to_compound (compound, NULL_TREE,
8500 BLOCK_EXPR_BODY (main_block));
8501 BLOCK_EXPR_BODY (main_block) = compound;
8502 }
8503 }
8504}
8505
8506/* Browse constructors in the super class, searching for a constructor
8507 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8508 otherwise. If the current class is an anonymous inner class, look
8509 for something that has the same signature. */
22eed1e6
APB
8510
8511static int
c2952b01
APB
8512verify_constructor_super (mdecl)
8513 tree mdecl;
22eed1e6
APB
8514{
8515 tree class = CLASSTYPE_SUPER (current_class);
152de068 8516 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8517 tree sdecl;
8518
22eed1e6
APB
8519 if (!class)
8520 return 0;
8521
c2952b01 8522 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8523 {
c2952b01
APB
8524 tree mdecl_arg_type;
8525 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8526 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8527 if (DECL_CONSTRUCTOR_P (sdecl))
8528 {
cf1b2274 8529 tree m_arg_type;
152de068
APB
8530 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8531 if (super_inner)
8532 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8533 for (m_arg_type = mdecl_arg_type;
8534 (arg_type != end_params_node
8535 && m_arg_type != end_params_node);
c2952b01 8536 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8537 m_arg_type = TREE_CHAIN (m_arg_type))
8538 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8539 break;
8540
cf1b2274 8541 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8542 return 0;
8543 }
8544 }
8545 else
8546 {
8547 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8548 {
152de068
APB
8549 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8550 if (super_inner)
8551 arg = TREE_CHAIN (arg);
8552 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8553 return 0;
8554 }
8555 }
8556 return 1;
8557}
8558
22eed1e6 8559/* Generate code for all context remembered for code generation. */
b351b287
APB
8560
8561void
8562java_expand_classes ()
8563{
5423609c 8564 int save_error_count = 0;
c2952b01
APB
8565 static struct parser_ctxt *saved_ctxp = NULL;
8566
23a79c61
APB
8567 java_parse_abort_on_error ();
8568 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8569 return;
8570 java_layout_classes ();
8571 java_parse_abort_on_error ();
8572
c2952b01 8573 saved_ctxp = ctxp_for_generation;
b351b287
APB
8574 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8575 {
8576 ctxp = ctxp_for_generation;
8577 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8578 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8579 java_parse_abort_on_error ();
8580 }
c2952b01
APB
8581
8582 /* Find anonymous classes and expand their constructor, now they
8583 have been fixed. */
8584 for (ctxp_for_generation = saved_ctxp;
8585 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8586 {
8587 tree current;
8588 ctxp = ctxp_for_generation;
8589 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8590 {
8591 current_class = TREE_TYPE (current);
8592 if (ANONYMOUS_CLASS_P (current_class))
8593 {
8594 tree d;
8595 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8596 {
8597 if (DECL_CONSTRUCTOR_P (d))
8598 {
8599 restore_line_number_status (1);
8600 reset_method_name (d);
8601 java_complete_expand_method (d);
8602 restore_line_number_status (0);
8603 break; /* We now there are no other ones */
8604 }
8605 }
8606 }
8607 }
8608 }
8609
8610 /* If we've found error at that stage, don't try to generate
8611 anything, unless we're emitting xrefs or checking the syntax only
8612 (but not using -fsyntax-only for the purpose of generating
8613 bytecode. */
8614 if (java_error_count && !flag_emit_xref
8615 && (!flag_syntax_only && !flag_emit_class_files))
8616 return;
8617
8618 /* Now things are stable, go for generation of the class data. */
8619 for (ctxp_for_generation = saved_ctxp;
8620 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8621 {
8622 tree current;
8623 ctxp = ctxp_for_generation;
8624 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8625 {
8626 current_class = TREE_TYPE (current);
8627 outgoing_cpool = TYPE_CPOOL (current_class);
8628 if (flag_emit_class_files)
8629 write_classfile (current_class);
8630 if (flag_emit_xref)
8631 expand_xref (current_class);
8632 else if (! flag_syntax_only)
8633 finish_class ();
8634 }
8635 }
b351b287
APB
8636}
8637
e04a16fb
AG
8638/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8639 a tree list node containing RIGHT. Fore coming RIGHTs will be
8640 chained to this hook. LOCATION contains the location of the
8641 separating `.' operator. */
8642
8643static tree
8644make_qualified_primary (primary, right, location)
8645 tree primary, right;
8646 int location;
8647{
8648 tree wfl;
8649
c2952b01 8650 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8651 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8652 else
8653 {
8654 wfl = primary;
c2952b01
APB
8655 /* If wfl wasn't qualified, we build a first anchor */
8656 if (!EXPR_WFL_QUALIFICATION (wfl))
8657 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8658 }
8659
c2952b01 8660 /* And chain them */
e04a16fb
AG
8661 EXPR_WFL_LINECOL (right) = location;
8662 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8663 PRIMARY_P (wfl) = 1;
8664 return wfl;
8665}
8666
8667/* Simple merge of two name separated by a `.' */
8668
8669static tree
8670merge_qualified_name (left, right)
8671 tree left, right;
8672{
8673 tree node;
c2952b01
APB
8674 if (!left && !right)
8675 return NULL_TREE;
8676
8677 if (!left)
8678 return right;
8679
8680 if (!right)
8681 return left;
8682
e04a16fb
AG
8683 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8684 IDENTIFIER_LENGTH (left));
8685 obstack_1grow (&temporary_obstack, '.');
8686 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8687 IDENTIFIER_LENGTH (right));
8688 node = get_identifier (obstack_base (&temporary_obstack));
8689 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8690 QUALIFIED_P (node) = 1;
8691 return node;
8692}
8693
8694/* Merge the two parts of a qualified name into LEFT. Set the
8695 location information of the resulting node to LOCATION, usually
8696 inherited from the location information of the `.' operator. */
8697
8698static tree
8699make_qualified_name (left, right, location)
8700 tree left, right;
8701 int location;
8702{
bc3ca41b
PB
8703#ifdef USE_COMPONENT_REF
8704 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8705 EXPR_WFL_LINECOL (node) = location;
8706 return node;
8707#else
e04a16fb
AG
8708 tree left_id = EXPR_WFL_NODE (left);
8709 tree right_id = EXPR_WFL_NODE (right);
8710 tree wfl, merge;
8711
8712 merge = merge_qualified_name (left_id, right_id);
8713
8714 /* Left wasn't qualified and is now qualified */
8715 if (!QUALIFIED_P (left_id))
8716 {
8717 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8718 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8719 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8720 }
8721
8722 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8723 EXPR_WFL_LINECOL (wfl) = location;
8724 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8725
8726 EXPR_WFL_NODE (left) = merge;
8727 return left;
bc3ca41b 8728#endif
e04a16fb
AG
8729}
8730
8731/* Extract the last identifier component of the qualified in WFL. The
8732 last identifier is removed from the linked list */
8733
8734static tree
8735cut_identifier_in_qualified (wfl)
8736 tree wfl;
8737{
8738 tree q;
8739 tree previous = NULL_TREE;
8740 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8741 if (!TREE_CHAIN (q))
8742 {
8743 if (!previous)
781b0558 8744 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8745 TREE_CHAIN (previous) = NULL_TREE;
8746 return TREE_PURPOSE (q);
8747 }
8748}
8749
8750/* Resolve the expression name NAME. Return its decl. */
8751
8752static tree
5e942c50 8753resolve_expression_name (id, orig)
e04a16fb 8754 tree id;
5e942c50 8755 tree *orig;
e04a16fb
AG
8756{
8757 tree name = EXPR_WFL_NODE (id);
8758 tree decl;
8759
8760 /* 6.5.5.1: Simple expression names */
8761 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8762 {
8763 /* 15.13.1: NAME can appear within the scope of a local variable
8764 declaration */
8765 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8766 return decl;
8767
8768 /* 15.13.1: NAME can appear within a class declaration */
8769 else
8770 {
8771 decl = lookup_field_wrapper (current_class, name);
8772 if (decl)
8773 {
c2952b01 8774 tree access = NULL_TREE;
e04a16fb 8775 int fs = FIELD_STATIC (decl);
f2760b27
APB
8776
8777 /* If we're accessing an outer scope local alias, make
8778 sure we change the name of the field we're going to
8779 build access to. */
8780 if (FIELD_LOCAL_ALIAS_USED (decl))
8781 name = DECL_NAME (decl);
8782
e04a16fb
AG
8783 /* Instance variable (8.3.1.1) can't appear within
8784 static method, static initializer or initializer for
8785 a static variable. */
8786 if (!fs && METHOD_STATIC (current_function_decl))
8787 {
7f10c2e2 8788 static_ref_err (id, name, current_class);
e04a16fb
AG
8789 return error_mark_node;
8790 }
22eed1e6
APB
8791 /* Instance variables can't appear as an argument of
8792 an explicit constructor invocation */
8793 if (!fs && ctxp->explicit_constructor_p)
8794 {
8795 parse_error_context
781b0558 8796 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8797 return error_mark_node;
8798 }
5e942c50 8799
c2952b01
APB
8800 /* If we're processing an inner class and we're trying
8801 to access a field belonging to an outer class, build
8802 the access to the field */
8803 if (!fs && outer_field_access_p (current_class, decl))
8804 return build_outer_field_access (id, decl);
8805
5e942c50 8806 /* Otherwise build what it takes to access the field */
c2952b01
APB
8807 access = build_field_ref ((fs ? NULL_TREE : current_this),
8808 DECL_CONTEXT (decl), name);
e8fc7396 8809 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8810 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8811 /* We may be asked to save the real field access node */
8812 if (orig)
c2952b01 8813 *orig = access;
5e942c50 8814 /* And we return what we got */
c2952b01 8815 return access;
e04a16fb
AG
8816 }
8817 /* Fall down to error report on undefined variable */
8818 }
8819 }
8820 /* 6.5.5.2 Qualified Expression Names */
8821 else
8822 {
5e942c50
APB
8823 if (orig)
8824 *orig = NULL_TREE;
e04a16fb
AG
8825 qualify_ambiguous_name (id);
8826 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8827 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8828 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8829 }
8830
8831 /* We've got an error here */
8832 parse_error_context (id, "Undefined variable `%s'",
8833 IDENTIFIER_POINTER (name));
8834
8835 return error_mark_node;
8836}
8837
7f10c2e2
APB
8838static void
8839static_ref_err (wfl, field_id, class_type)
8840 tree wfl, field_id, class_type;
8841{
8842 parse_error_context
8843 (wfl,
8844 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8845 IDENTIFIER_POINTER (field_id),
8846 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8847}
8848
e04a16fb
AG
8849/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8850 We return something suitable to generate the field access. We also
8851 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8852 recipient's address can be null. */
8853
8854static tree
8855resolve_field_access (qual_wfl, field_decl, field_type)
8856 tree qual_wfl;
8857 tree *field_decl, *field_type;
8858{
8859 int is_static = 0;
8860 tree field_ref;
8861 tree decl, where_found, type_found;
8862
8863 if (resolve_qualified_expression_name (qual_wfl, &decl,
8864 &where_found, &type_found))
8865 return error_mark_node;
8866
8867 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8868 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8869 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8870 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8871 {
8872 tree length = build_java_array_length_access (where_found);
8873 field_ref =
8874 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8875
8876 /* In case we're dealing with a static array, we need to
8877 initialize its class before the array length can be fetched.
8878 It's also a good time to create a DECL_RTL for the field if
8879 none already exists, otherwise if the field was declared in a
8880 class found in an external file and hasn't been (and won't
8881 be) accessed for its value, none will be created. */
8882 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8883 {
8884 build_static_field_ref (where_found);
8885 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8886 }
e04a16fb
AG
8887 }
8888 /* We might have been trying to resolve field.method(). In which
8889 case, the resolution is over and decl is the answer */
34f4db93 8890 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8891 field_ref = decl;
34f4db93 8892 else if (JDECL_P (decl))
e04a16fb 8893 {
5e942c50
APB
8894 int static_final_found = 0;
8895 if (!type_found)
8896 type_found = DECL_CONTEXT (decl);
34f4db93 8897 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8898 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8899 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8900 && DECL_INITIAL (decl))
5e942c50 8901 {
0c2b8145
APB
8902 /* When called on a FIELD_DECL of the right (primitive)
8903 type, java_complete_tree will try to substitue the decl
8904 for it's initial value. */
70541f45 8905 field_ref = java_complete_tree (decl);
5e942c50
APB
8906 static_final_found = 1;
8907 }
8908 else
7f10c2e2
APB
8909 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8910 NULL_TREE : where_found),
5e942c50 8911 type_found, DECL_NAME (decl));
e04a16fb
AG
8912 if (field_ref == error_mark_node)
8913 return error_mark_node;
e8fc7396
APB
8914 if (is_static && !static_final_found
8915 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8916 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8917 }
8918 else
8919 field_ref = decl;
8920
8921 if (field_decl)
8922 *field_decl = decl;
8923 if (field_type)
c877974e
APB
8924 *field_type = (QUAL_DECL_TYPE (decl) ?
8925 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8926 return field_ref;
8927}
8928
e28cd97b
APB
8929/* If NODE is an access to f static field, strip out the class
8930 initialization part and return the field decl, otherwise, return
8931 NODE. */
8932
8933static tree
8934strip_out_static_field_access_decl (node)
8935 tree node;
8936{
8937 if (TREE_CODE (node) == COMPOUND_EXPR)
8938 {
8939 tree op1 = TREE_OPERAND (node, 1);
8940 if (TREE_CODE (op1) == COMPOUND_EXPR)
8941 {
8942 tree call = TREE_OPERAND (op1, 0);
8943 if (TREE_CODE (call) == CALL_EXPR
8944 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8945 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8946 == soft_initclass_node)
8947 return TREE_OPERAND (op1, 1);
8948 }
2f11d407
TT
8949 else if (JDECL_P (op1))
8950 return op1;
e28cd97b
APB
8951 }
8952 return node;
8953}
8954
e04a16fb
AG
8955/* 6.5.5.2: Qualified Expression Names */
8956
8957static int
8958resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8959 tree wfl;
8960 tree *found_decl, *type_found, *where_found;
8961{
8962 int from_type = 0; /* Field search initiated from a type */
c2952b01 8963 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8964 int previous_call_static = 0;
8965 int is_static;
8966 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8967 /* For certain for of inner class instantiation */
8968 tree saved_current, saved_this;
8969#define RESTORE_THIS_AND_CURRENT_CLASS \
8970 { current_class = saved_current; current_this = saved_this;}
8971
c877974e 8972 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8973
8974 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8975 {
8976 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8977 tree ret_decl; /* for EH checking */
8978 int location; /* for EH checking */
e04a16fb
AG
8979
8980 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8981 switch (TREE_CODE (qual_wfl))
8982 {
8983 case CALL_EXPR:
b67d701b 8984 case NEW_CLASS_EXPR:
e04a16fb
AG
8985 /* If the access to the function call is a non static field,
8986 build the code to access it. */
34f4db93 8987 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8988 {
ac825856
APB
8989 decl = maybe_access_field (decl, *where_found,
8990 DECL_CONTEXT (decl));
e04a16fb
AG
8991 if (decl == error_mark_node)
8992 return 1;
8993 }
c2952b01 8994
e04a16fb
AG
8995 /* And code for the function call */
8996 if (complete_function_arguments (qual_wfl))
8997 return 1;
c2952b01
APB
8998
8999 /* We might have to setup a new current class and a new this
9000 for the search of an inner class, relative to the type of
9001 a expression resolved as `decl'. The current values are
9002 saved and restored shortly after */
9003 saved_current = current_class;
9004 saved_this = current_this;
9005 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
9006 {
9007 current_class = type;
9008 current_this = decl;
9009 }
9010
89e09b9a
PB
9011 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9012 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9013 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9014 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9015 *where_found = patch_method_invocation (qual_wfl, decl, type,
9016 &is_static, &ret_decl);
e04a16fb 9017 if (*where_found == error_mark_node)
c2952b01
APB
9018 {
9019 RESTORE_THIS_AND_CURRENT_CLASS;
9020 return 1;
9021 }
e04a16fb
AG
9022 *type_found = type = QUAL_DECL_TYPE (*where_found);
9023
c2952b01
APB
9024 /* If we're creating an inner class instance, check for that
9025 an enclosing instance is in scope */
9026 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9027 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9028 {
9029 parse_error_context
165f37bc
APB
9030 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9031 lang_printable_name (type, 0),
9032 (!current_this ? "" :
9033 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9034 RESTORE_THIS_AND_CURRENT_CLASS;
9035 return 1;
9036 }
9037
9038 /* In case we had to change then to resolve a inner class
9039 instantiation using a primary qualified by a `new' */
9040 RESTORE_THIS_AND_CURRENT_CLASS;
9041
34d4df06
APB
9042 /* EH check. No check on access$<n> functions */
9043 if (location
9044 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9045 (DECL_NAME (current_function_decl)))
7705e9db
APB
9046 check_thrown_exceptions (location, ret_decl);
9047
e04a16fb
AG
9048 /* If the previous call was static and this one is too,
9049 build a compound expression to hold the two (because in
9050 that case, previous function calls aren't transported as
9051 forcoming function's argument. */
9052 if (previous_call_static && is_static)
9053 {
9054 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9055 TREE_SIDE_EFFECTS (decl) = 1;
9056 }
9057 else
9058 {
9059 previous_call_static = is_static;
9060 decl = *where_found;
9061 }
c2952b01 9062 from_type = 0;
e04a16fb
AG
9063 continue;
9064
d8fccff5 9065 case NEW_ARRAY_EXPR:
c2952b01 9066 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9067 *where_found = decl = java_complete_tree (qual_wfl);
9068 if (decl == error_mark_node)
9069 return 1;
9070 *type_found = type = QUAL_DECL_TYPE (decl);
9071 CLASS_LOADED_P (type) = 1;
9072 continue;
9073
e04a16fb
AG
9074 case CONVERT_EXPR:
9075 *where_found = decl = java_complete_tree (qual_wfl);
9076 if (decl == error_mark_node)
9077 return 1;
9078 *type_found = type = QUAL_DECL_TYPE (decl);
9079 from_cast = 1;
9080 continue;
9081
22eed1e6 9082 case CONDITIONAL_EXPR:
5e942c50 9083 case STRING_CST:
ac22f9cb 9084 case MODIFY_EXPR:
22eed1e6
APB
9085 *where_found = decl = java_complete_tree (qual_wfl);
9086 if (decl == error_mark_node)
9087 return 1;
9088 *type_found = type = QUAL_DECL_TYPE (decl);
9089 continue;
9090
e04a16fb
AG
9091 case ARRAY_REF:
9092 /* If the access to the function call is a non static field,
9093 build the code to access it. */
34f4db93 9094 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9095 {
9096 decl = maybe_access_field (decl, *where_found, type);
9097 if (decl == error_mark_node)
9098 return 1;
9099 }
9100 /* And code for the array reference expression */
9101 decl = java_complete_tree (qual_wfl);
9102 if (decl == error_mark_node)
9103 return 1;
9104 type = QUAL_DECL_TYPE (decl);
9105 continue;
0a2138e2 9106
37feda7d
APB
9107 case PLUS_EXPR:
9108 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9109 return 1;
9110 if ((type = patch_string (decl)))
9111 decl = type;
9112 *where_found = QUAL_RESOLUTION (q) = decl;
9113 *type_found = type = TREE_TYPE (decl);
9114 break;
9115
165f37bc
APB
9116 case CLASS_LITERAL:
9117 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9118 return 1;
9119 *where_found = QUAL_RESOLUTION (q) = decl;
9120 *type_found = type = TREE_TYPE (decl);
9121 break;
9122
0a2138e2
APB
9123 default:
9124 /* Fix for -Wall Just go to the next statement. Don't
9125 continue */
a3f406ce 9126 break;
e04a16fb
AG
9127 }
9128
9129 /* If we fall here, we weren't processing a (static) function call. */
9130 previous_call_static = 0;
9131
9132 /* It can be the keyword THIS */
9133 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9134 {
9135 if (!current_this)
9136 {
9137 parse_error_context
9138 (wfl, "Keyword `this' used outside allowed context");
9139 return 1;
9140 }
f63991a8
APB
9141 if (ctxp->explicit_constructor_p)
9142 {
781b0558 9143 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9144 return 1;
9145 }
e04a16fb 9146 /* We have to generate code for intermediate acess */
c2952b01
APB
9147 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9148 {
9149 *where_found = decl = current_this;
9150 *type_found = type = QUAL_DECL_TYPE (decl);
9151 }
4dbf4496
APB
9152 /* We're trying to access the this from somewhere else. Make sure
9153 it's allowed before doing so. */
c2952b01
APB
9154 else
9155 {
4dbf4496
APB
9156 if (!enclosing_context_p (type, current_class))
9157 {
9158 char *p = xstrdup (lang_printable_name (type, 0));
9159 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9160 p, p,
9161 lang_printable_name (current_class, 0));
9162 free (p);
9163 return 1;
9164 }
c2952b01
APB
9165 *where_found = decl = build_current_thisn (type);
9166 from_qualified_this = 1;
9167 }
9168
9169 from_type = 0;
e04a16fb
AG
9170 continue;
9171 }
9172
9173 /* 15.10.2 Accessing Superclass Members using SUPER */
9174 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9175 {
9176 tree node;
9177 /* Check on the restricted use of SUPER */
9178 if (METHOD_STATIC (current_function_decl)
9179 || current_class == object_type_node)
9180 {
9181 parse_error_context
9182 (wfl, "Keyword `super' used outside allowed context");
9183 return 1;
9184 }
9185 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9186 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9187 CLASSTYPE_SUPER (current_class),
9188 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9189 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9190 if (decl == error_mark_node)
9191 return 1;
e04a16fb
AG
9192 *type_found = type = QUAL_DECL_TYPE (decl);
9193 from_super = from_type = 1;
9194 continue;
9195 }
9196
9197 /* 15.13.1: Can't search for field name in packages, so we
9198 assume a variable/class name was meant. */
9199 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9200 {
5e942c50
APB
9201 tree name = resolve_package (wfl, &q);
9202 if (name)
9203 {
c2952b01 9204 tree list;
5e942c50 9205 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9206 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9207 out. We're going to search something inside it. */
9208 *type_found = type = TREE_TYPE (decl);
9209 layout_class (type);
9210 from_type = 1;
c2952b01 9211
dde1da72
APB
9212 /* Fix them all the way down, if any are left. */
9213 if (q)
c2952b01 9214 {
dde1da72
APB
9215 list = TREE_CHAIN (q);
9216 while (list)
9217 {
9218 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9219 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9220 list = TREE_CHAIN (list);
9221 }
c2952b01 9222 }
5e942c50 9223 }
e04a16fb 9224 else
5e942c50
APB
9225 {
9226 if (from_super || from_cast)
9227 parse_error_context
9228 ((from_cast ? qual_wfl : wfl),
9229 "No variable `%s' defined in class `%s'",
9230 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9231 lang_printable_name (type, 0));
9232 else
9233 parse_error_context
9234 (qual_wfl, "Undefined variable or class name: `%s'",
9235 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9236 return 1;
9237 }
e04a16fb
AG
9238 }
9239
9240 /* We have a type name. It's been already resolved when the
9241 expression was qualified. */
9242 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9243 {
9244 if (!(decl = QUAL_RESOLUTION (q)))
9245 return 1; /* Error reported already */
9246
c2952b01
APB
9247 /* Sneak preview. If next we see a `new', we're facing a
9248 qualification with resulted in a type being selected
9249 instead of a field. Report the error */
9250 if(TREE_CHAIN (q)
9251 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9252 {
9253 parse_error_context (qual_wfl, "Undefined variable `%s'",
9254 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9255 return 1;
9256 }
9257
e04a16fb
AG
9258 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9259 {
9260 parse_error_context
9261 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9262 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9263 GET_TYPE_NAME (type),
e04a16fb
AG
9264 IDENTIFIER_POINTER (DECL_NAME (decl)),
9265 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9266 return 1;
9267 }
5e942c50 9268 check_deprecation (qual_wfl, decl);
c2952b01 9269
e04a16fb
AG
9270 type = TREE_TYPE (decl);
9271 from_type = 1;
9272 }
9273 /* We resolve and expression name */
9274 else
9275 {
cd531a2e 9276 tree field_decl = NULL_TREE;
e04a16fb
AG
9277
9278 /* If there exists an early resolution, use it. That occurs
9279 only once and we know that there are more things to
9280 come. Don't do that when processing something after SUPER
9281 (we need more thing to be put in place below */
9282 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9283 {
9284 decl = QUAL_RESOLUTION (q);
c877974e 9285 if (!type)
5e942c50 9286 {
7f10c2e2
APB
9287 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9288 {
9289 if (current_this)
9290 *where_found = current_this;
9291 else
9292 {
9293 static_ref_err (qual_wfl, DECL_NAME (decl),
9294 current_class);
9295 return 1;
9296 }
f0f3a777
APB
9297 if (outer_field_access_p (current_class, decl))
9298 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9299 }
c877974e
APB
9300 else
9301 {
9302 *where_found = TREE_TYPE (decl);
9303 if (TREE_CODE (*where_found) == POINTER_TYPE)
9304 *where_found = TREE_TYPE (*where_found);
9305 }
5e942c50 9306 }
b67d701b 9307 }
e04a16fb
AG
9308
9309 /* We have to search for a field, knowing the type of its
9310 container. The flag FROM_TYPE indicates that we resolved
9311 the last member of the expression as a type name, which
5e942c50
APB
9312 means that for the resolution of this field, we'll look
9313 for other errors than if it was resolved as a member of
9314 an other field. */
e04a16fb
AG
9315 else
9316 {
9317 int is_static;
5e942c50
APB
9318 tree field_decl_type; /* For layout */
9319
e04a16fb
AG
9320 if (!from_type && !JREFERENCE_TYPE_P (type))
9321 {
9322 parse_error_context
9323 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9324 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9325 lang_printable_name (type, 0),
e04a16fb
AG
9326 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9327 return 1;
9328 }
9329
dc0b3eff
PB
9330 field_decl = lookup_field_wrapper (type,
9331 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9332
9333 /* Maybe what we're trying to access an inner class. */
9334 if (!field_decl)
9335 {
9336 tree ptr, inner_decl;
9337
9338 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9339 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9340 if (inner_decl)
9341 {
9342 check_inner_class_access (inner_decl, decl, qual_wfl);
9343 type = TREE_TYPE (inner_decl);
9344 decl = inner_decl;
9345 from_type = 1;
9346 continue;
9347 }
9348 }
9349
dc0b3eff 9350 if (field_decl == NULL_TREE)
e04a16fb
AG
9351 {
9352 parse_error_context
2aa11e97 9353 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9354 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9355 GET_TYPE_NAME (type));
e04a16fb
AG
9356 return 1;
9357 }
dc0b3eff
PB
9358 if (field_decl == error_mark_node)
9359 return 1;
5e942c50
APB
9360
9361 /* Layout the type of field_decl, since we may need
c877974e
APB
9362 it. Don't do primitive types or loaded classes. The
9363 situation of non primitive arrays may not handled
9364 properly here. FIXME */
5e942c50
APB
9365 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9366 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9367 else
9368 field_decl_type = TREE_TYPE (field_decl);
9369 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9370 && !CLASS_LOADED_P (field_decl_type)
9371 && !TYPE_ARRAY_P (field_decl_type))
9372 resolve_and_layout (field_decl_type, NULL_TREE);
9373 if (TYPE_ARRAY_P (field_decl_type))
9374 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9375
9376 /* Check on accessibility here */
9377 if (not_accessible_p (type, field_decl, from_super))
9378 {
9379 parse_error_context
9380 (qual_wfl,
9381 "Can't access %s field `%s.%s' from `%s'",
9382 java_accstring_lookup
9383 (get_access_flags_from_decl (field_decl)),
2aa11e97 9384 GET_TYPE_NAME (type),
e04a16fb
AG
9385 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9386 IDENTIFIER_POINTER
9387 (DECL_NAME (TYPE_NAME (current_class))));
9388 return 1;
9389 }
5e942c50 9390 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9391
9392 /* There are things to check when fields are accessed
9393 from type. There are no restrictions on a static
9394 declaration of the field when it is accessed from an
9395 interface */
9396 is_static = FIELD_STATIC (field_decl);
9397 if (!from_super && from_type
c2952b01
APB
9398 && !TYPE_INTERFACE_P (type)
9399 && !is_static
9400 && (current_function_decl
9401 && METHOD_STATIC (current_function_decl)))
e04a16fb 9402 {
7f10c2e2 9403 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9404 return 1;
9405 }
9406 from_cast = from_super = 0;
9407
c2952b01
APB
9408 /* It's an access from a type but it isn't static, we
9409 make it relative to `this'. */
9410 if (!is_static && from_type)
9411 decl = current_this;
9412
5e942c50
APB
9413 /* If we need to generate something to get a proper
9414 handle on what this field is accessed from, do it
9415 now. */
e04a16fb
AG
9416 if (!is_static)
9417 {
c583dd46 9418 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9419 if (decl == error_mark_node)
9420 return 1;
9421 }
9422
9423 /* We want to keep the location were found it, and the type
9424 we found. */
9425 *where_found = decl;
9426 *type_found = type;
9427
c2952b01
APB
9428 /* Generate the correct expression for field access from
9429 qualified this */
9430 if (from_qualified_this)
9431 {
9432 field_decl = build_outer_field_access (qual_wfl, field_decl);
9433 from_qualified_this = 0;
9434 }
9435
e04a16fb
AG
9436 /* This is the decl found and eventually the next one to
9437 search from */
9438 decl = field_decl;
9439 }
e04a16fb
AG
9440 from_type = 0;
9441 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9442
9443 /* Sneak preview. If decl is qualified by a `new', report
9444 the error here to be accurate on the peculiar construct */
9445 if (TREE_CHAIN (q)
9446 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9447 && !JREFERENCE_TYPE_P (type))
9448 {
9449 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9450 lang_printable_name (type, 0));
9451 return 1;
9452 }
e04a16fb 9453 }
dde1da72
APB
9454 /* `q' might have changed due to a after package resolution
9455 re-qualification */
9456 if (!q)
9457 break;
e04a16fb
AG
9458 }
9459 *found_decl = decl;
9460 return 0;
9461}
9462
9463/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9464 can't be accessed from REFERENCE (a record type). This should be
9465 used when decl is a field or a method.*/
e04a16fb 9466
be245ac0
KG
9467static int
9468not_accessible_p (reference, member, from_super)
e04a16fb
AG
9469 tree reference, member;
9470 int from_super;
9471{
9472 int access_flag = get_access_flags_from_decl (member);
9473
4dbf4496
APB
9474 /* Inner classes are processed by check_inner_class_access */
9475 if (INNER_CLASS_TYPE_P (reference))
9476 return 0;
9477
e04a16fb
AG
9478 /* Access always granted for members declared public */
9479 if (access_flag & ACC_PUBLIC)
9480 return 0;
9481
9482 /* Check access on protected members */
9483 if (access_flag & ACC_PROTECTED)
9484 {
9485 /* Access granted if it occurs from within the package
9486 containing the class in which the protected member is
9487 declared */
9488 if (class_in_current_package (DECL_CONTEXT (member)))
9489 return 0;
9490
9bbc7d9f
PB
9491 /* If accessed with the form `super.member', then access is granted */
9492 if (from_super)
9493 return 0;
e04a16fb 9494
9bbc7d9f 9495 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9496 member is declared or a subclass of it. Find the right
9497 context to perform the check */
9498 if (PURE_INNER_CLASS_TYPE_P (reference))
9499 {
9500 while (INNER_CLASS_TYPE_P (reference))
9501 {
9502 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9503 return 0;
9504 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9505 }
9506 }
473e7b07 9507 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9508 return 0;
e04a16fb
AG
9509 return 1;
9510 }
9511
9512 /* Check access on private members. Access is granted only if it
473e7b07 9513 occurs from within the class in which it is declared. Exceptions
4dbf4496 9514 are accesses from inner-classes. */
e04a16fb 9515 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9516 return (current_class == DECL_CONTEXT (member) ? 0 :
9517 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9518
9519 /* Default access are permitted only when occuring within the
9520 package in which the type (REFERENCE) is declared. In other words,
9521 REFERENCE is defined in the current package */
9522 if (ctxp->package)
9523 return !class_in_current_package (reference);
473e7b07 9524
e04a16fb
AG
9525 /* Otherwise, access is granted */
9526 return 0;
9527}
9528
5e942c50
APB
9529/* Test deprecated decl access. */
9530static void
9531check_deprecation (wfl, decl)
9532 tree wfl, decl;
9533{
49f48c71 9534 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9535 /* Complain if the field is deprecated and the file it was defined
9536 in isn't compiled at the same time the file which contains its
9537 use is */
9538 if (DECL_DEPRECATED (decl)
9539 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9540 {
9541 char the [20];
9542 switch (TREE_CODE (decl))
9543 {
9544 case FUNCTION_DECL:
9545 strcpy (the, "method");
9546 break;
9547 case FIELD_DECL:
9548 strcpy (the, "field");
9549 break;
9550 case TYPE_DECL:
9551 strcpy (the, "class");
9552 break;
15fdcfe9
PB
9553 default:
9554 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9555 }
9556 parse_warning_context
9557 (wfl, "The %s `%s' in class `%s' has been deprecated",
9558 the, lang_printable_name (decl, 0),
9559 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9560 }
9561}
9562
e04a16fb
AG
9563/* Returns 1 if class was declared in the current package, 0 otherwise */
9564
9565static int
9566class_in_current_package (class)
9567 tree class;
9568{
9569 static tree cache = NULL_TREE;
9570 int qualified_flag;
9571 tree left;
9572
9573 if (cache == class)
9574 return 1;
9575
9576 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9577
9578 /* If the current package is empty and the name of CLASS is
9579 qualified, class isn't in the current package. If there is a
9580 current package and the name of the CLASS is not qualified, class
9581 isn't in the current package */
0a2138e2 9582 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9583 return 0;
9584
9585 /* If there is not package and the name of CLASS isn't qualified,
9586 they belong to the same unnamed package */
9587 if (!ctxp->package && !qualified_flag)
9588 return 1;
9589
9590 /* Compare the left part of the name of CLASS with the package name */
9591 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9592 if (ctxp->package == left)
9593 {
19e223db
MM
9594 static int initialized_p;
9595 /* Register CACHE with the garbage collector. */
9596 if (!initialized_p)
9597 {
9598 ggc_add_tree_root (&cache, 1);
9599 initialized_p = 1;
9600 }
9601
e04a16fb
AG
9602 cache = class;
9603 return 1;
9604 }
9605 return 0;
9606}
9607
9608/* This function may generate code to access DECL from WHERE. This is
9609 done only if certain conditions meet. */
9610
9611static tree
9612maybe_access_field (decl, where, type)
9613 tree decl, where, type;
9614{
5e942c50
APB
9615 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9616 && !FIELD_STATIC (decl))
e04a16fb 9617 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9618 (type ? type : DECL_CONTEXT (decl)),
9619 DECL_NAME (decl));
e04a16fb
AG
9620 return decl;
9621}
9622
15fdcfe9 9623/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9624 and according to the situation, PRIMARY and WHERE may be
9625 used. IS_STATIC is set to 1 if the invoked function is static. */
9626
9627static tree
89e09b9a 9628patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9629 tree patch, primary, where;
9630 int *is_static;
b9f7e36c 9631 tree *ret_decl;
e04a16fb
AG
9632{
9633 tree wfl = TREE_OPERAND (patch, 0);
9634 tree args = TREE_OPERAND (patch, 1);
9635 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9636 tree list;
22eed1e6 9637 int is_static_flag = 0;
89e09b9a 9638 int is_super_init = 0;
bccaf73a 9639 tree this_arg = NULL_TREE;
e04a16fb
AG
9640
9641 /* Should be overriden if everything goes well. Otherwise, if
9642 something fails, it should keep this value. It stop the
9643 evaluation of a bogus assignment. See java_complete_tree,
9644 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9645 evaluating an assignment */
9646 TREE_TYPE (patch) = error_mark_node;
9647
9648 /* Since lookup functions are messing with line numbers, save the
9649 context now. */
9650 java_parser_context_save_global ();
9651
9652 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9653
9654 /* Resolution of qualified name, excluding constructors */
9655 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9656 {
dde1da72 9657 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9658 /* Extract the last IDENTIFIER of the qualified
9659 expression. This is a wfl and we will use it's location
9660 data during error report. */
9661 identifier_wfl = cut_identifier_in_qualified (wfl);
9662 identifier = EXPR_WFL_NODE (identifier_wfl);
9663
9664 /* Given the context, IDENTIFIER is syntactically qualified
9665 as a MethodName. We need to qualify what's before */
9666 qualify_ambiguous_name (wfl);
dde1da72 9667 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9668
dde1da72
APB
9669 if (resolved == error_mark_node)
9670 PATCH_METHOD_RETURN_ERROR ();
9671
9672 type = GET_SKIP_TYPE (resolved);
9673 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9674
9675 if (JPRIMITIVE_TYPE_P (type))
9676 {
9677 parse_error_context
9678 (identifier_wfl,
9679 "Can't invoke a method on primitive type `%s'",
9680 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9681 PATCH_METHOD_RETURN_ERROR ();
9682 }
9683
dde1da72
APB
9684 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9685 args = nreverse (args);
2c56429a 9686
e04a16fb 9687 /* We're resolving a call from a type */
dde1da72 9688 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9689 {
dde1da72 9690 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9691 {
9692 parse_error_context
781b0558
KG
9693 (identifier_wfl,
9694 "Can't make static reference to method `%s' in interface `%s'",
9695 IDENTIFIER_POINTER (identifier),
e04a16fb 9696 IDENTIFIER_POINTER (name));
b9f7e36c 9697 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9698 }
e04a16fb
AG
9699 if (list && !METHOD_STATIC (list))
9700 {
c2e3db92 9701 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9702 parse_error_context
9703 (identifier_wfl,
9704 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9705 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9706 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9707 free (fct_name);
b9f7e36c 9708 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9709 }
9710 }
e04a16fb 9711 else
dde1da72
APB
9712 this_arg = primary = resolved;
9713
5e942c50 9714 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9715 wfl = identifier_wfl;
9716 }
9717 /* Resolution of simple names, names generated after a primary: or
9718 constructors */
9719 else
9720 {
cd531a2e 9721 tree class_to_search = NULL_TREE;
c2952b01 9722 int lc; /* Looking for Constructor */
e04a16fb
AG
9723
9724 /* We search constructor in their target class */
9725 if (CALL_CONSTRUCTOR_P (patch))
9726 {
22eed1e6
APB
9727 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9728 class_to_search = EXPR_WFL_NODE (wfl);
9729 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9730 this_identifier_node)
9731 class_to_search = NULL_TREE;
9732 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9733 super_identifier_node)
e04a16fb 9734 {
89e09b9a 9735 is_super_init = 1;
22eed1e6
APB
9736 if (CLASSTYPE_SUPER (current_class))
9737 class_to_search =
9738 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9739 else
9740 {
781b0558 9741 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9742 PATCH_METHOD_RETURN_ERROR ();
9743 }
e04a16fb 9744 }
22eed1e6
APB
9745
9746 /* Class to search is NULL if we're searching the current one */
9747 if (class_to_search)
e04a16fb 9748 {
c2952b01
APB
9749 class_to_search = resolve_and_layout (class_to_search, wfl);
9750
22eed1e6
APB
9751 if (!class_to_search)
9752 {
9753 parse_error_context
9754 (wfl, "Class `%s' not found in type declaration",
9755 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9756 PATCH_METHOD_RETURN_ERROR ();
9757 }
9758
5e942c50
APB
9759 /* Can't instantiate an abstract class, but we can
9760 invoke it's constructor. It's use within the `new'
9761 context is denied here. */
9762 if (CLASS_ABSTRACT (class_to_search)
9763 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9764 {
9765 parse_error_context
781b0558
KG
9766 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9767 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9768 PATCH_METHOD_RETURN_ERROR ();
9769 }
c2952b01 9770
22eed1e6 9771 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9772 }
22eed1e6
APB
9773 else
9774 class_to_search = current_class;
e04a16fb
AG
9775 lc = 1;
9776 }
9777 /* This is a regular search in the local class, unless an
9778 alternate class is specified. */
9779 else
9780 {
9781 class_to_search = (where ? where : current_class);
9782 lc = 0;
9783 }
c2952b01 9784
e04a16fb
AG
9785 /* NAME is a simple identifier or comes from a primary. Search
9786 in the class whose declaration contain the method being
9787 invoked. */
c877974e 9788 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9789
c2952b01 9790 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9791 /* Don't continue if no method were found, as the next statement
9792 can't be executed then. */
b9f7e36c
APB
9793 if (!list)
9794 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9795
9796 /* Check for static reference if non static methods */
9797 if (check_for_static_method_reference (wfl, patch, list,
9798 class_to_search, primary))
b9f7e36c 9799 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9800
165f37bc
APB
9801 /* Check for inner classes creation from illegal contexts */
9802 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9803 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9804 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9805 {
9806 parse_error_context
9807 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9808 lang_printable_name (class_to_search, 0),
9809 (!current_this ? "" :
9810 "; an explicit one must be provided when creating this inner class"));
9811 PATCH_METHOD_RETURN_ERROR ();
9812 }
9813
22eed1e6
APB
9814 /* Non static methods are called with the current object extra
9815 argument. If patch a `new TYPE()', the argument is the value
9816 returned by the object allocator. If method is resolved as a
9817 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9818 args = nreverse (args);
bccaf73a 9819 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9820 {
9821 this_arg = primary ? primary : current_this;
9822
9823 /* If we're using an access method, things are different.
9824 There are two familly of cases:
9825
9826 1) We're not generating bytecodes:
9827
9828 - LIST is non static. It's invocation is transformed from
9829 x(a1,...,an) into this$<n>.x(a1,....an).
9830 - LIST is static. It's invocation is transformed from
9831 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9832
9833 2) We're generating bytecodes:
9834
9835 - LIST is non static. It's invocation is transformed from
9836 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9837 - LIST is static. It's invocation is transformed from
9838 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9839
9840 Of course, this$<n> can be abitrary complex, ranging from
9841 this$0 (the immediate outer context) to
9842 access$0(access$0(...(this$0))).
9843
9844 maybe_use_access_method returns a non zero value if the
dfb99c83 9845 this_arg has to be moved into the (then generated) stub
4dbf4496 9846 argument list. In the meantime, the selected function
dfb99c83 9847 might have be replaced by a generated stub. */
c2952b01
APB
9848 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9849 args = tree_cons (NULL_TREE, this_arg, args);
9850 }
e04a16fb 9851 }
b67d701b 9852
e04a16fb
AG
9853 /* Merge point of all resolution schemes. If we have nothing, this
9854 is an error, already signaled */
b9f7e36c
APB
9855 if (!list)
9856 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9857
e04a16fb
AG
9858 /* Check accessibility, position the is_static flag, build and
9859 return the call */
9bbc7d9f 9860 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9861 {
c2e3db92 9862 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9863 parse_error_context
9864 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9865 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9866 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9867 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9868 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9869 free (fct_name);
b9f7e36c 9870 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9871 }
5e942c50 9872 check_deprecation (wfl, list);
22eed1e6 9873
c2952b01
APB
9874 /* If invoking a innerclass constructor, there are hidden parameters
9875 to pass */
9876 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9877 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9878 {
9879 /* And make sure we add the accessed local variables to be saved
9880 in field aliases. */
9881 args = build_alias_initializer_parameter_list
9882 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9883
da632f2c 9884 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9885 if (primary || current_this)
9886 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9887 else
9888 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9889 }
9890
152de068
APB
9891 /* This handles the situation where a constructor invocation needs
9892 to have an enclosing context passed as a second parameter (the
9893 constructor is one of an inner class. We extract it from the
9894 current function. */
9895 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9896 {
9897 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9898 tree extra_arg;
9899
9900 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9901 {
9902 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9903 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9904 }
9905 else
9906 {
9907 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9908 extra_arg =
9909 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9910 extra_arg = java_complete_tree (extra_arg);
9911 }
9912 args = tree_cons (NULL_TREE, extra_arg, args);
9913 }
9914
22eed1e6 9915 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9916 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9917 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9918
c3f2a476
APB
9919 /* In the context of an explicit constructor invocation, we can't
9920 invoke any method relying on `this'. Exceptions are: we're
9921 invoking a static function, primary exists and is not the current
9922 this, we're creating a new object. */
22eed1e6 9923 if (ctxp->explicit_constructor_p
c3f2a476
APB
9924 && !is_static_flag
9925 && (!primary || primary == current_this)
9926 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9927 {
781b0558 9928 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9929 PATCH_METHOD_RETURN_ERROR ();
9930 }
e04a16fb 9931 java_parser_context_restore_global ();
22eed1e6
APB
9932 if (is_static)
9933 *is_static = is_static_flag;
b9f7e36c
APB
9934 /* Sometimes, we want the decl of the selected method. Such as for
9935 EH checking */
9936 if (ret_decl)
9937 *ret_decl = list;
89e09b9a
PB
9938 patch = patch_invoke (patch, list, args);
9939 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9940 {
c2952b01
APB
9941 tree finit_parms, finit_call;
9942
c00f0fb2 9943 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
9944 finit_parms = build_alias_initializer_parameter_list
9945 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9946
c2952b01
APB
9947 finit_call =
9948 build_method_invocation (build_wfl_node (finit_identifier_node),
9949 finit_parms);
9950
9951 /* Generate the code used to initialize fields declared with an
9952 initialization statement and build a compound statement along
9953 with the super constructor invocation. */
89e09b9a
PB
9954 patch = build (COMPOUND_EXPR, void_type_node, patch,
9955 java_complete_tree (finit_call));
9956 CAN_COMPLETE_NORMALLY (patch) = 1;
9957 }
9958 return patch;
e04a16fb
AG
9959}
9960
9961/* Check that we're not trying to do a static reference to a method in
9962 non static method. Return 1 if it's the case, 0 otherwise. */
9963
9964static int
9965check_for_static_method_reference (wfl, node, method, where, primary)
9966 tree wfl, node, method, where, primary;
9967{
9968 if (METHOD_STATIC (current_function_decl)
9969 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9970 {
c2e3db92 9971 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9972 parse_error_context
9973 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9974 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9975 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9976 free (fct_name);
9977 return 1;
9978 }
9979 return 0;
9980}
9981
c2952b01
APB
9982/* Fix the invocation of *MDECL if necessary in the case of a
9983 invocation from an inner class. *THIS_ARG might be modified
9984 appropriately and an alternative access to *MDECL might be
9985 returned. */
9986
9987static int
9988maybe_use_access_method (is_super_init, mdecl, this_arg)
9989 int is_super_init;
9990 tree *mdecl, *this_arg;
9991{
9992 tree ctx;
9993 tree md = *mdecl, ta = *this_arg;
9994 int to_return = 0;
9995 int non_static_context = !METHOD_STATIC (md);
9996
9997 if (is_super_init
165f37bc
APB
9998 || DECL_CONTEXT (md) == current_class
9999 || !PURE_INNER_CLASS_TYPE_P (current_class)
10000 || DECL_FINIT_P (md))
c2952b01
APB
10001 return 0;
10002
10003 /* If we're calling a method found in an enclosing class, generate
10004 what it takes to retrieve the right this. Don't do that if we're
10005 invoking a static method. */
10006
10007 if (non_static_context)
10008 {
10009 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10010 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10011 {
10012 ta = build_current_thisn (current_class);
10013 ta = build_wfl_node (ta);
10014 }
10015 else
10016 {
10017 tree type = ctx;
10018 while (type)
10019 {
10020 maybe_build_thisn_access_method (type);
4dbf4496 10021 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10022 {
10023 ta = build_access_to_thisn (ctx, type, 0);
10024 break;
10025 }
10026 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10027 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10028 }
10029 }
10030 ta = java_complete_tree (ta);
10031 }
10032
10033 /* We might have to use an access method to get to MD. We can
10034 break the method access rule as far as we're not generating
10035 bytecode */
10036 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10037 {
10038 md = build_outer_method_access_method (md);
10039 to_return = 1;
10040 }
10041
10042 *mdecl = md;
10043 *this_arg = ta;
10044
10045 /* Returnin a non zero value indicates we were doing a non static
10046 method invokation that is now a static invocation. It will have
10047 callee displace `this' to insert it in the regular argument
10048 list. */
10049 return (non_static_context && to_return);
10050}
10051
e04a16fb
AG
10052/* Patch an invoke expression METHOD and ARGS, based on its invocation
10053 mode. */
10054
10055static tree
89e09b9a 10056patch_invoke (patch, method, args)
e04a16fb 10057 tree patch, method, args;
e04a16fb
AG
10058{
10059 tree dtable, func;
0a2138e2 10060 tree original_call, t, ta;
e815887f 10061 tree cond = NULL_TREE;
e04a16fb 10062
5e942c50
APB
10063 /* Last step for args: convert build-in types. If we're dealing with
10064 a new TYPE() type call, the first argument to the constructor
e815887f 10065 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10066 `new' */
10067 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10068 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10069 t = TREE_CHAIN (t);
ac825856
APB
10070 for (ta = args; t != end_params_node && ta;
10071 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10072 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10073 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10074 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10075
10076 /* Resolve unresolved returned type isses */
10077 t = TREE_TYPE (TREE_TYPE (method));
10078 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10079 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10080
e8fc7396 10081 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10082 func = method;
10083 else
e04a16fb 10084 {
15fdcfe9 10085 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10086 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10087 {
10088 case INVOKE_VIRTUAL:
10089 dtable = invoke_build_dtable (0, args);
10090 func = build_invokevirtual (dtable, method);
10091 break;
b9f7e36c 10092
e815887f
TT
10093 case INVOKE_NONVIRTUAL:
10094 /* If the object for the method call is null, we throw an
10095 exception. We don't do this if the object is the current
10096 method's `this'. In other cases we just rely on an
10097 optimization pass to eliminate redundant checks. */
10098 if (TREE_VALUE (args) != current_this)
10099 {
10100 /* We use a SAVE_EXPR here to make sure we only evaluate
10101 the new `self' expression once. */
10102 tree save_arg = save_expr (TREE_VALUE (args));
10103 TREE_VALUE (args) = save_arg;
10104 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10105 null_pointer_node);
10106 }
10107 /* Fall through. */
10108
15fdcfe9
PB
10109 case INVOKE_SUPER:
10110 case INVOKE_STATIC:
10111 func = build_known_method_ref (method, TREE_TYPE (method),
10112 DECL_CONTEXT (method),
10113 signature, args);
10114 break;
e04a16fb 10115
15fdcfe9
PB
10116 case INVOKE_INTERFACE:
10117 dtable = invoke_build_dtable (1, args);
173f556c 10118 func = build_invokeinterface (dtable, method);
15fdcfe9 10119 break;
5e942c50 10120
15fdcfe9 10121 default:
89e09b9a 10122 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10123 }
10124
10125 /* Ensure self_type is initialized, (invokestatic). FIXME */
10126 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10127 }
10128
e04a16fb
AG
10129 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10130 TREE_OPERAND (patch, 0) = func;
10131 TREE_OPERAND (patch, 1) = args;
10132 original_call = patch;
10133
e815887f 10134 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10135 returned value is the first argument to the constructor. We build
10136 a COMPOUND_EXPR and use saved expression so that the overall NEW
10137 expression value is a pointer to a newly created and initialized
10138 class. */
10139 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10140 {
10141 tree class = DECL_CONTEXT (method);
10142 tree c1, saved_new, size, new;
e8fc7396 10143 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10144 {
10145 TREE_TYPE (patch) = build_pointer_type (class);
10146 return patch;
10147 }
e04a16fb
AG
10148 if (!TYPE_SIZE (class))
10149 safe_layout_class (class);
10150 size = size_in_bytes (class);
10151 new = build (CALL_EXPR, promote_type (class),
10152 build_address_of (alloc_object_node),
10153 tree_cons (NULL_TREE, build_class_ref (class),
10154 build_tree_list (NULL_TREE,
10155 size_in_bytes (class))),
10156 NULL_TREE);
10157 saved_new = save_expr (new);
10158 c1 = build_tree_list (NULL_TREE, saved_new);
10159 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10160 TREE_OPERAND (original_call, 1) = c1;
10161 TREE_SET_CODE (original_call, CALL_EXPR);
10162 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10163 }
e815887f
TT
10164
10165 /* If COND is set, then we are building a check to see if the object
10166 is NULL. */
10167 if (cond != NULL_TREE)
10168 {
10169 /* We have to make the `then' branch a compound expression to
10170 make the types turn out right. This seems bizarre. */
10171 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10172 build (COMPOUND_EXPR, TREE_TYPE (patch),
10173 build (CALL_EXPR, void_type_node,
10174 build_address_of (soft_nullpointer_node),
10175 NULL_TREE, NULL_TREE),
10176 (FLOAT_TYPE_P (TREE_TYPE (patch))
10177 ? build_real (TREE_TYPE (patch), dconst0)
10178 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10179 integer_zero_node))),
10180 patch);
10181 TREE_SIDE_EFFECTS (patch) = 1;
10182 }
10183
e04a16fb
AG
10184 return patch;
10185}
10186
10187static int
10188invocation_mode (method, super)
10189 tree method;
10190 int super;
10191{
10192 int access = get_access_flags_from_decl (method);
10193
22eed1e6
APB
10194 if (super)
10195 return INVOKE_SUPER;
10196
e815887f 10197 if (access & ACC_STATIC)
e04a16fb
AG
10198 return INVOKE_STATIC;
10199
e815887f
TT
10200 /* We have to look for a constructor before we handle nonvirtual
10201 calls; otherwise the constructor will look nonvirtual. */
10202 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10203 return INVOKE_STATIC;
e815887f
TT
10204
10205 if (access & ACC_FINAL || access & ACC_PRIVATE)
10206 return INVOKE_NONVIRTUAL;
10207
10208 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10209 return INVOKE_NONVIRTUAL;
10210
e04a16fb
AG
10211 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10212 return INVOKE_INTERFACE;
22eed1e6 10213
e04a16fb
AG
10214 return INVOKE_VIRTUAL;
10215}
10216
b67d701b
PB
10217/* Retrieve a refined list of matching methods. It covers the step
10218 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10219
10220static tree
10221lookup_method_invoke (lc, cl, class, name, arg_list)
10222 int lc;
10223 tree cl;
10224 tree class, name, arg_list;
10225{
de4c7b02 10226 tree atl = end_params_node; /* Arg Type List */
c877974e 10227 tree method, signature, list, node;
49f48c71 10228 const char *candidates; /* Used for error report */
b5b8a0e7 10229 char *dup;
e04a16fb 10230
5e942c50 10231 /* Fix the arguments */
e04a16fb
AG
10232 for (node = arg_list; node; node = TREE_CHAIN (node))
10233 {
e3884b71 10234 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10235 /* Non primitive type may have to be resolved */
e3884b71 10236 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10237 resolve_and_layout (current_arg, NULL_TREE);
10238 /* And promoted */
b67d701b 10239 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10240 current_arg = promote_type (current_arg);
5e942c50 10241 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10242 }
e04a16fb 10243
c2952b01
APB
10244 /* Presto. If we're dealing with an anonymous class and a
10245 constructor call, generate the right constructor now, since we
10246 know the arguments' types. */
10247
10248 if (lc && ANONYMOUS_CLASS_P (class))
10249 craft_constructor (TYPE_NAME (class), atl);
10250
5e942c50
APB
10251 /* Find all candidates and then refine the list, searching for the
10252 most specific method. */
10253 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10254 list = find_most_specific_methods_list (list);
b67d701b
PB
10255 if (list && !TREE_CHAIN (list))
10256 return TREE_VALUE (list);
e04a16fb 10257
b67d701b
PB
10258 /* Issue an error. List candidates if any. Candidates are listed
10259 only if accessible (non accessible methods may end-up here for
10260 the sake of a better error report). */
10261 candidates = NULL;
10262 if (list)
e04a16fb 10263 {
e04a16fb 10264 tree current;
b67d701b 10265 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10266 for (current = list; current; current = TREE_CHAIN (current))
10267 {
b67d701b
PB
10268 tree cm = TREE_VALUE (current);
10269 char string [4096];
10270 if (!cm || not_accessible_p (class, cm, 0))
10271 continue;
b67d701b 10272 sprintf
22eed1e6
APB
10273 (string, " `%s' in `%s'%s",
10274 get_printable_method_name (cm),
b67d701b
PB
10275 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10276 (TREE_CHAIN (current) ? "\n" : ""));
10277 obstack_grow (&temporary_obstack, string, strlen (string));
10278 }
10279 obstack_1grow (&temporary_obstack, '\0');
10280 candidates = obstack_finish (&temporary_obstack);
10281 }
10282 /* Issue the error message */
c877974e
APB
10283 method = make_node (FUNCTION_TYPE);
10284 TYPE_ARG_TYPES (method) = atl;
b67d701b 10285 signature = build_java_argument_signature (method);
c63b98cd 10286 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10287 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10288 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10289 (lc ? dup : IDENTIFIER_POINTER (name)),
10290 IDENTIFIER_POINTER (signature), dup,
b67d701b 10291 (candidates ? candidates : ""));
b5b8a0e7 10292 free (dup);
b67d701b
PB
10293 return NULL_TREE;
10294}
10295
5e942c50
APB
10296/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10297 when we're looking for a constructor. */
b67d701b
PB
10298
10299static tree
5e942c50
APB
10300find_applicable_accessible_methods_list (lc, class, name, arglist)
10301 int lc;
b67d701b
PB
10302 tree class, name, arglist;
10303{
ad69b5b6
BM
10304 static struct hash_table t, *searched_classes = NULL;
10305 static int search_not_done = 0;
b67d701b
PB
10306 tree list = NULL_TREE, all_list = NULL_TREE;
10307
ad69b5b6
BM
10308 /* Check the hash table to determine if this class has been searched
10309 already. */
10310 if (searched_classes)
10311 {
10312 if (hash_lookup (searched_classes,
10313 (const hash_table_key) class, FALSE, NULL))
10314 return NULL;
10315 }
10316 else
10317 {
10318 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10319 java_hash_compare_tree_node);
10320 searched_classes = &t;
10321 }
10322
10323 search_not_done++;
10324 hash_lookup (searched_classes,
0c2b8145 10325 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10326
c2952b01
APB
10327 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10328 {
10329 load_class (class, 1);
10330 safe_layout_class (class);
10331 }
10332
1982388a 10333 /* Search interfaces */
9a7ab4b3
APB
10334 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10335 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10336 {
1982388a
APB
10337 int i, n;
10338 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10339 search_applicable_methods_list (lc, TYPE_METHODS (class),
10340 name, arglist, &list, &all_list);
1982388a 10341 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10342 for (i = 1; i < n; i++)
b67d701b 10343 {
de0b553f
APB
10344 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10345 tree rlist;
10346
de0b553f
APB
10347 rlist = find_applicable_accessible_methods_list (lc, t, name,
10348 arglist);
165f37bc 10349 list = chainon (rlist, list);
e04a16fb 10350 }
e04a16fb 10351 }
1982388a
APB
10352 /* Search classes */
10353 else
c2952b01 10354 {
165f37bc
APB
10355 tree sc = class;
10356 int seen_inner_class = 0;
c2952b01
APB
10357 search_applicable_methods_list (lc, TYPE_METHODS (class),
10358 name, arglist, &list, &all_list);
10359
165f37bc
APB
10360 /* We must search all interfaces of this class */
10361 if (!lc)
10362 {
10363 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10364 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10365 for (i = 1; i < n; i++)
10366 {
10367 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10368 if (t != object_type_node)
30a3caef
ZW
10369 {
10370 tree rlist
10371 = find_applicable_accessible_methods_list (lc, t,
10372 name, arglist);
10373 list = chainon (rlist, list);
10374 }
165f37bc 10375 }
165f37bc
APB
10376 }
10377
c2952b01
APB
10378 /* Search enclosing context of inner classes before looking
10379 ancestors up. */
10380 while (!lc && INNER_CLASS_TYPE_P (class))
10381 {
165f37bc
APB
10382 tree rlist;
10383 seen_inner_class = 1;
c2952b01 10384 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10385 rlist = find_applicable_accessible_methods_list (lc, class,
10386 name, arglist);
10387 list = chainon (rlist, list);
c2952b01 10388 }
165f37bc
APB
10389
10390 if (!lc && seen_inner_class
10391 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10392 class = CLASSTYPE_SUPER (sc);
10393 else
10394 class = sc;
10395
ad69b5b6
BM
10396 /* Search superclass */
10397 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10398 {
10399 tree rlist;
10400 class = CLASSTYPE_SUPER (class);
10401 rlist = find_applicable_accessible_methods_list (lc, class,
10402 name, arglist);
10403 list = chainon (rlist, list);
10404 }
10405 }
10406
10407 search_not_done--;
10408
10409 /* We're done. Reset the searched classes list and finally search
10410 java.lang.Object if it wasn't searched already. */
10411 if (!search_not_done)
10412 {
10413 if (!lc
10414 && TYPE_METHODS (object_type_node)
10415 && !hash_lookup (searched_classes,
10416 (const hash_table_key) object_type_node,
10417 FALSE, NULL))
10418 {
10419 search_applicable_methods_list (lc,
10420 TYPE_METHODS (object_type_node),
10421 name, arglist, &list, &all_list);
10422 }
10423 hash_table_free (searched_classes);
10424 searched_classes = NULL;
c2952b01 10425 }
1982388a 10426
b67d701b
PB
10427 /* Either return the list obtained or all selected (but
10428 inaccessible) methods for better error report. */
10429 return (!list ? all_list : list);
10430}
e04a16fb 10431
ad69b5b6 10432/* Effectively search for the appropriate method in method */
1982388a
APB
10433
10434static void
c2952b01 10435search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10436 int lc;
10437 tree method, name, arglist;
10438 tree *list, *all_list;
10439{
10440 for (; method; method = TREE_CHAIN (method))
10441 {
10442 /* When dealing with constructor, stop here, otherwise search
10443 other classes */
10444 if (lc && !DECL_CONSTRUCTOR_P (method))
10445 continue;
10446 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10447 || (GET_METHOD_NAME (method) != name)))
10448 continue;
10449
10450 if (argument_types_convertible (method, arglist))
10451 {
10452 /* Retain accessible methods only */
10453 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10454 method, 0))
10455 *list = tree_cons (NULL_TREE, method, *list);
10456 else
10457 /* Also retain all selected method here */
10458 *all_list = tree_cons (NULL_TREE, method, *list);
10459 }
10460 }
ad69b5b6 10461}
1982388a 10462
b67d701b
PB
10463/* 15.11.2.2 Choose the Most Specific Method */
10464
10465static tree
10466find_most_specific_methods_list (list)
10467 tree list;
10468{
10469 int max = 0;
9a7ab4b3 10470 int abstract, candidates;
b67d701b
PB
10471 tree current, new_list = NULL_TREE;
10472 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10473 {
b67d701b
PB
10474 tree method;
10475 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10476
10477 for (method = list; method; method = TREE_CHAIN (method))
10478 {
0c2b8145 10479 tree method_v, current_v;
b67d701b
PB
10480 /* Don't test a method against itself */
10481 if (method == current)
10482 continue;
10483
0c2b8145
APB
10484 method_v = TREE_VALUE (method);
10485 current_v = TREE_VALUE (current);
10486
10487 /* Compare arguments and location where methods where declared */
10488 if (argument_types_convertible (method_v, current_v))
b67d701b 10489 {
0c2b8145
APB
10490 if (valid_method_invocation_conversion_p
10491 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10492 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10493 && enclosing_context_p (DECL_CONTEXT (method_v),
10494 DECL_CONTEXT (current_v))))
10495 {
10496 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10497 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10498 max = (v > max ? v : max);
10499 }
b67d701b
PB
10500 }
10501 }
e04a16fb
AG
10502 }
10503
b67d701b 10504 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10505 for (current = list, abstract = -1, candidates = -1;
10506 current; current = TREE_CHAIN (current))
b67d701b 10507 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10508 {
10509 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10510 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10511 candidates++;
10512 }
b67d701b 10513
165f37bc
APB
10514 /* If we have several and they're all abstract, just pick the
10515 closest one. */
9a7ab4b3
APB
10516 if (candidates > 0 && (candidates == abstract))
10517 {
10518 new_list = nreverse (new_list);
10519 TREE_CHAIN (new_list) = NULL_TREE;
10520 }
165f37bc 10521
9a7ab4b3
APB
10522 /* We have several, we couldn't find a most specific, all but one are
10523 abstract, we pick the only non abstract one. */
10524 if (candidates > 0 && !max && (candidates == abstract+1))
165f37bc 10525 {
9a7ab4b3
APB
10526 for (current = new_list; current; current = TREE_CHAIN (current))
10527 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10528 {
10529 TREE_CHAIN (current) = NULL_TREE;
10530 new_list = current;
10531 }
165f37bc
APB
10532 }
10533
b67d701b
PB
10534 /* If we can't find one, lower expectations and try to gather multiple
10535 maximally specific methods */
165f37bc 10536 while (!new_list && max)
b67d701b
PB
10537 {
10538 while (--max > 0)
10539 {
10540 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10541 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10542 }
b67d701b
PB
10543 }
10544
10545 return new_list;
e04a16fb
AG
10546}
10547
b67d701b
PB
10548/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10549 converted by method invocation conversion (5.3) to the type of the
10550 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10551 to change less often than M1. */
e04a16fb 10552
b67d701b
PB
10553static int
10554argument_types_convertible (m1, m2_or_arglist)
10555 tree m1, m2_or_arglist;
e04a16fb 10556{
b67d701b
PB
10557 static tree m2_arg_value = NULL_TREE;
10558 static tree m2_arg_cache = NULL_TREE;
19e223db 10559 static int initialized_p;
e04a16fb 10560
b67d701b 10561 register tree m1_arg, m2_arg;
e04a16fb 10562
19e223db
MM
10563 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10564 collector. */
10565 if (!initialized_p)
10566 {
10567 ggc_add_tree_root (&m2_arg_value, 1);
10568 ggc_add_tree_root (&m2_arg_cache, 1);
10569 initialized_p = 1;
10570 }
10571
c2952b01 10572 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10573
b67d701b
PB
10574 if (m2_arg_value == m2_or_arglist)
10575 m2_arg = m2_arg_cache;
10576 else
10577 {
10578 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10579 argument types */
10580 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10581 {
10582 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10583 if (!METHOD_STATIC (m2_or_arglist))
10584 m2_arg = TREE_CHAIN (m2_arg);
10585 }
10586 else
10587 m2_arg = m2_or_arglist;
e04a16fb 10588
b67d701b
PB
10589 m2_arg_value = m2_or_arglist;
10590 m2_arg_cache = m2_arg;
10591 }
e04a16fb 10592
de4c7b02 10593 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10594 {
c877974e 10595 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10596 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10597 TREE_VALUE (m2_arg)))
10598 break;
10599 m1_arg = TREE_CHAIN (m1_arg);
10600 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10601 }
de4c7b02 10602 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10603}
10604
10605/* Qualification routines */
10606
10607static void
10608qualify_ambiguous_name (id)
10609 tree id;
10610{
cd531a2e
KG
10611 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10612 saved_current_class;
d8fccff5 10613 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10614 int code;
e04a16fb
AG
10615
10616 /* We first qualify the first element, then derive qualification of
10617 others based on the first one. If the first element is qualified
10618 by a resolution (field or type), this resolution is stored in the
10619 QUAL_RESOLUTION of the qual element being examined. We need to
10620 save the current_class since the use of SUPER might change the
10621 its value. */
10622 saved_current_class = current_class;
10623 qual = EXPR_WFL_QUALIFICATION (id);
10624 do {
10625
10626 /* Simple qualified expression feature a qual_wfl that is a
10627 WFL. Expression derived from a primary feature more complicated
10628 things like a CALL_EXPR. Expression from primary need to be
10629 worked out to extract the part on which the qualification will
10630 take place. */
10631 qual_wfl = QUAL_WFL (qual);
10632 switch (TREE_CODE (qual_wfl))
10633 {
10634 case CALL_EXPR:
10635 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10636 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10637 {
10638 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10639 qual_wfl = QUAL_WFL (qual);
10640 }
10641 break;
d8fccff5 10642 case NEW_ARRAY_EXPR:
c2952b01 10643 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10644 qual = TREE_CHAIN (qual);
1a6d4fb7 10645 again = new_array_found = 1;
d8fccff5 10646 continue;
e04a16fb 10647 case CONVERT_EXPR:
f2760b27
APB
10648 break;
10649 case NEW_CLASS_EXPR:
e04a16fb
AG
10650 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10651 break;
c583dd46
APB
10652 case ARRAY_REF:
10653 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10654 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10655 break;
8576f094
APB
10656 case STRING_CST:
10657 qual = TREE_CHAIN (qual);
10658 qual_wfl = QUAL_WFL (qual);
10659 break;
165f37bc
APB
10660 case CLASS_LITERAL:
10661 qual = TREE_CHAIN (qual);
10662 qual_wfl = QUAL_WFL (qual);
10663 break;
0a2138e2
APB
10664 default:
10665 /* Fix for -Wall. Just break doing nothing */
10666 break;
e04a16fb 10667 }
8576f094 10668
e04a16fb
AG
10669 ptr_type = current_class;
10670 again = 0;
8576f094
APB
10671 code = TREE_CODE (qual_wfl);
10672
10673 /* Pos evaluation: non WFL leading expression nodes */
10674 if (code == CONVERT_EXPR
10675 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10676 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10677
cd7c5840
APB
10678 else if (code == INTEGER_CST)
10679 name = qual_wfl;
10680
ac22f9cb 10681 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10682 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10683 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10684
c2952b01
APB
10685 else if (code == TREE_LIST)
10686 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10687
37feda7d
APB
10688 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10689 || code == PLUS_EXPR)
8576f094
APB
10690 {
10691 qual = TREE_CHAIN (qual);
10692 qual_wfl = QUAL_WFL (qual);
10693 again = 1;
10694 }
10695 else
f441f671
APB
10696 {
10697 name = EXPR_WFL_NODE (qual_wfl);
10698 if (!name)
10699 {
10700 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10701 again = 1;
10702 }
10703 }
10704
e04a16fb
AG
10705 /* If we have a THIS (from a primary), we set the context accordingly */
10706 if (name == this_identifier_node)
10707 {
10708 qual = TREE_CHAIN (qual);
10709 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10710 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10711 again = 1;
10712 else
10713 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10714 this_found = 1;
10715 }
10716 /* If we have a SUPER, we set the context accordingly */
10717 if (name == super_identifier_node)
10718 {
10719 current_class = CLASSTYPE_SUPER (ptr_type);
10720 /* Check that there is such a thing as a super class. If not,
10721 return. The error will be caught later on, during the
10722 resolution */
10723 if (!current_class)
10724 {
10725 current_class = saved_current_class;
10726 return;
10727 }
10728 qual = TREE_CHAIN (qual);
10729 /* Do one more interation to set things up */
10730 super_found = again = 1;
10731 }
10732 } while (again);
10733
f2760b27
APB
10734 /* If name appears within the scope of a local variable declaration
10735 or parameter declaration, then it is an expression name. We don't
10736 carry this test out if we're in the context of the use of SUPER
10737 or THIS */
cd7c5840
APB
10738 if (!this_found && !super_found
10739 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10740 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10741 {
10742 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10743 QUAL_RESOLUTION (qual) = decl;
10744 }
10745
10746 /* If within the class/interface NAME was found to be used there
10747 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10748 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10749 address length, it is OK. */
10750 else if ((decl = lookup_field_wrapper (ptr_type, name))
10751 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10752 {
10753 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10754 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10755 }
10756
1a6d4fb7 10757 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10758 - NAME is a class/interface declared within the compilation
10759 unit containing NAME,
10760 - NAME is imported via a single-type-import declaration,
10761 - NAME is declared in an another compilation unit of the package
10762 of the compilation unit containing NAME,
10763 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10764 of the compilation unit containing NAME.
10765 - NAME is actually a STRING_CST. */
cd7c5840
APB
10766 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10767 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10768 {
10769 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10770 QUAL_RESOLUTION (qual) = decl;
10771 }
10772
f2760b27 10773 /* Method call, array references and cast are expression name */
9bbc7d9f 10774 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10775 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10776 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10777 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10778
10779 /* Check here that NAME isn't declared by more than one
10780 type-import-on-demand declaration of the compilation unit
10781 containing NAME. FIXME */
10782
10783 /* Otherwise, NAME is reclassified as a package name */
10784 else
10785 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10786
10787 /* Propagate the qualification accross other components of the
10788 qualified name */
10789 for (qual = TREE_CHAIN (qual); qual;
10790 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10791 {
10792 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10793 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10794 else
10795 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10796 }
10797
10798 /* Store the global qualification for the ambiguous part of ID back
10799 into ID fields */
10800 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10801 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10802 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10803 RESOLVE_TYPE_NAME_P (id) = 1;
10804 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10805 RESOLVE_PACKAGE_NAME_P (id) = 1;
10806
10807 /* Restore the current class */
10808 current_class = saved_current_class;
10809}
10810
10811static int
10812breakdown_qualified (left, right, source)
10813 tree *left, *right, source;
10814{
63ad61ed 10815 char *p, *base;
e04a16fb
AG
10816 int l = IDENTIFIER_LENGTH (source);
10817
63ad61ed
ZW
10818 base = alloca (l + 1);
10819 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10820
e04a16fb 10821 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10822 p = base + l - 1;
e04a16fb
AG
10823 while (*p != '.' && p != base)
10824 p--;
10825
10826 /* We didn't find a '.'. Return an error */
10827 if (p == base)
10828 return 1;
10829
10830 *p = '\0';
10831 if (right)
10832 *right = get_identifier (p+1);
63ad61ed 10833 *left = get_identifier (base);
e04a16fb
AG
10834
10835 return 0;
10836}
10837
e04a16fb 10838/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10839 local variable decls if present.
10840 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10841
10842static tree
10843java_complete_tree (node)
10844 tree node;
5b09b33e
PB
10845{
10846 node = java_complete_lhs (node);
0c2b8145
APB
10847 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10848 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10849 && !flag_emit_xref)
5b09b33e
PB
10850 {
10851 tree value = DECL_INITIAL (node);
10852 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10853 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10854 value = fold_constant_for_init (value, node);
100f7cd8 10855 pop_obstacks ();
5b09b33e
PB
10856 DECL_INITIAL (node) = value;
10857 if (value != NULL_TREE)
c2952b01
APB
10858 {
10859 /* fold_constant_for_init sometimes widen the original type
10860 of the constant (i.e. byte to int.) It's not desirable,
10861 especially if NODE is a function argument. */
10862 if (TREE_CODE (value) == INTEGER_CST
10863 && TREE_TYPE (node) != TREE_TYPE (value))
10864 return convert (TREE_TYPE (node), value);
10865 else
10866 return value;
10867 }
5b09b33e
PB
10868 }
10869 return node;
10870}
10871
2aa11e97
APB
10872static tree
10873java_stabilize_reference (node)
10874 tree node;
10875{
10876 if (TREE_CODE (node) == COMPOUND_EXPR)
10877 {
10878 tree op0 = TREE_OPERAND (node, 0);
10879 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10880 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10881 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10882 return node;
10883 }
5cbdba64 10884 return stabilize_reference (node);
2aa11e97
APB
10885}
10886
5b09b33e
PB
10887/* Patch tree nodes in a function body. When a BLOCK is found, push
10888 local variable decls if present.
10889 Same as java_complete_tree, but does not resolve static finals to values. */
10890
10891static tree
10892java_complete_lhs (node)
10893 tree node;
e04a16fb 10894{
22eed1e6 10895 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10896 int flag;
e04a16fb
AG
10897
10898 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10899 worked out. */
e04a16fb
AG
10900 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10901 return node;
10902
10903 /* The switch block implements cases processing container nodes
10904 first. Contained nodes are always written back. Leaves come
10905 next and return a value. */
10906 switch (TREE_CODE (node))
10907 {
10908 case BLOCK:
10909
10910 /* 1- Block section.
10911 Set the local values on decl names so we can identify them
10912 faster when they're referenced. At that stage, identifiers
10913 are legal so we don't check for declaration errors. */
10914 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10915 {
10916 DECL_CONTEXT (cn) = current_function_decl;
10917 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10918 }
15fdcfe9
PB
10919 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10920 CAN_COMPLETE_NORMALLY (node) = 1;
10921 else
e04a16fb 10922 {
15fdcfe9
PB
10923 tree stmt = BLOCK_EXPR_BODY (node);
10924 tree *ptr;
10925 int error_seen = 0;
10926 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10927 {
c877974e
APB
10928 /* Re-order from (((A; B); C); ...; Z) to
10929 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10930 This makes it easier to scan the statements left-to-right
10931 without using recursion (which might overflow the stack
10932 if the block has many statements. */
10933 for (;;)
10934 {
10935 tree left = TREE_OPERAND (stmt, 0);
10936 if (TREE_CODE (left) != COMPOUND_EXPR)
10937 break;
10938 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10939 TREE_OPERAND (left, 1) = stmt;
10940 stmt = left;
10941 }
10942 BLOCK_EXPR_BODY (node) = stmt;
10943 }
10944
c877974e
APB
10945 /* Now do the actual complete, without deep recursion for
10946 long blocks. */
15fdcfe9 10947 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10948 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10949 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10950 {
10951 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10952 tree *next = &TREE_OPERAND (*ptr, 1);
10953 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10954 if (cur == empty_stmt_node)
10955 {
10956 /* Optimization; makes it easier to detect empty bodies.
10957 Most useful for <clinit> with all-constant initializer. */
10958 *ptr = *next;
10959 continue;
10960 }
15fdcfe9
PB
10961 if (TREE_CODE (cur) == ERROR_MARK)
10962 error_seen++;
10963 else if (! CAN_COMPLETE_NORMALLY (cur))
10964 {
10965 wfl_op2 = *next;
10966 for (;;)
10967 {
10968 if (TREE_CODE (wfl_op2) == BLOCK)
10969 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10970 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10971 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10972 else
10973 break;
10974 }
10975 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10976 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10977 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10978 }
10979 ptr = next;
10980 }
10981 *ptr = java_complete_tree (*ptr);
10982
10983 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10984 return error_mark_node;
15fdcfe9 10985 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10986 }
10987 /* Turn local bindings to null */
10988 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10989 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10990
10991 TREE_TYPE (node) = void_type_node;
10992 break;
10993
10994 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10995
b9f7e36c
APB
10996 case THROW_EXPR:
10997 wfl_op1 = TREE_OPERAND (node, 0);
10998 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10999 /* 14.19 A throw statement cannot complete normally. */
11000 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11001 return patch_throw_statement (node, wfl_op1);
11002
11003 case SYNCHRONIZED_EXPR:
11004 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11005 return patch_synchronized_statement (node, wfl_op1);
11006
b67d701b
PB
11007 case TRY_EXPR:
11008 return patch_try_statement (node);
11009
a7d8d81f
PB
11010 case TRY_FINALLY_EXPR:
11011 COMPLETE_CHECK_OP_0 (node);
11012 COMPLETE_CHECK_OP_1 (node);
11013 CAN_COMPLETE_NORMALLY (node)
11014 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11015 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11016 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11017 return node;
11018
5a005d9e
PB
11019 case CLEANUP_POINT_EXPR:
11020 COMPLETE_CHECK_OP_0 (node);
11021 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11022 CAN_COMPLETE_NORMALLY (node) =
11023 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11024 return node;
11025
11026 case WITH_CLEANUP_EXPR:
11027 COMPLETE_CHECK_OP_0 (node);
11028 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11029 CAN_COMPLETE_NORMALLY (node) =
11030 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11031 TREE_TYPE (node) = void_type_node;
11032 return node;
11033
e04a16fb
AG
11034 case LABELED_BLOCK_EXPR:
11035 PUSH_LABELED_BLOCK (node);
11036 if (LABELED_BLOCK_BODY (node))
11037 COMPLETE_CHECK_OP_1 (node);
11038 TREE_TYPE (node) = void_type_node;
11039 POP_LABELED_BLOCK ();
1fb89a4d
APB
11040
11041 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11042 {
11043 LABELED_BLOCK_BODY (node) = NULL_TREE;
11044 CAN_COMPLETE_NORMALLY (node) = 1;
11045 }
1fb89a4d 11046 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11047 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11048 return node;
11049
11050 case EXIT_BLOCK_EXPR:
11051 /* We don't complete operand 1, because it's the return value of
11052 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11053 return patch_bc_statement (node);
11054
15fdcfe9
PB
11055 case CASE_EXPR:
11056 cn = java_complete_tree (TREE_OPERAND (node, 0));
11057 if (cn == error_mark_node)
11058 return cn;
11059
8576f094
APB
11060 /* First, the case expression must be constant. Values of final
11061 fields are accepted. */
15fdcfe9 11062 cn = fold (cn);
8576f094
APB
11063 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11064 && JDECL_P (TREE_OPERAND (cn, 1))
11065 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11066 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
11067 {
11068 push_obstacks (&permanent_obstack, &permanent_obstack);
11069 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11070 TREE_OPERAND (cn, 1));
11071 pop_obstacks ();
11072 }
15fdcfe9 11073
ce6e9147 11074 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11075 {
11076 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11077 parse_error_context (node, "Constant expression required");
11078 return error_mark_node;
11079 }
11080
11081 nn = ctxp->current_loop;
11082
11083 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11084 if (!try_builtin_assignconv (NULL_TREE,
11085 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11086 {
11087 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11088 parse_error_context
11089 (wfl_operator,
11090 "Incompatible type for case. Can't convert `%s' to `int'",
11091 lang_printable_name (TREE_TYPE (cn), 0));
11092 return error_mark_node;
11093 }
11094
11095 cn = fold (convert (int_type_node, cn));
11096
11097 /* Multiple instance of a case label bearing the same
11098 value is checked during code generation. The case
11099 expression is allright so far. */
34d4df06
APB
11100 if (TREE_CODE (cn) == VAR_DECL)
11101 cn = DECL_INITIAL (cn);
15fdcfe9 11102 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11103 TREE_TYPE (node) = void_type_node;
15fdcfe9 11104 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11105 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11106 break;
11107
11108 case DEFAULT_EXPR:
11109 nn = ctxp->current_loop;
11110 /* Only one default label is allowed per switch statement */
11111 if (SWITCH_HAS_DEFAULT (nn))
11112 {
11113 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11114 parse_error_context (wfl_operator,
11115 "Duplicate case label: `default'");
11116 return error_mark_node;
11117 }
11118 else
11119 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11120 TREE_TYPE (node) = void_type_node;
10100cc7 11121 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11122 CAN_COMPLETE_NORMALLY (node) = 1;
11123 break;
11124
b67d701b 11125 case SWITCH_EXPR:
e04a16fb
AG
11126 case LOOP_EXPR:
11127 PUSH_LOOP (node);
11128 /* Check whether the loop was enclosed in a labeled
11129 statement. If not, create one, insert the loop in it and
11130 return the node */
11131 nn = patch_loop_statement (node);
b67d701b 11132
e04a16fb 11133 /* Anyways, walk the body of the loop */
b67d701b
PB
11134 if (TREE_CODE (node) == LOOP_EXPR)
11135 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11136 /* Switch statement: walk the switch expression and the cases */
11137 else
11138 node = patch_switch_statement (node);
11139
e7c7bcef 11140 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11141 nn = error_mark_node;
11142 else
15fdcfe9 11143 {
b635eb2f
PB
11144 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11145 /* If we returned something different, that's because we
11146 inserted a label. Pop the label too. */
11147 if (nn != node)
11148 {
11149 if (CAN_COMPLETE_NORMALLY (node))
11150 CAN_COMPLETE_NORMALLY (nn) = 1;
11151 POP_LABELED_BLOCK ();
11152 }
15fdcfe9 11153 }
e04a16fb
AG
11154 POP_LOOP ();
11155 return nn;
11156
11157 case EXIT_EXPR:
11158 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11159 return patch_exit_expr (node);
11160
11161 case COND_EXPR:
11162 /* Condition */
11163 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11164 if (TREE_OPERAND (node, 0) == error_mark_node)
11165 return error_mark_node;
11166 /* then-else branches */
11167 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11168 if (TREE_OPERAND (node, 1) == error_mark_node)
11169 return error_mark_node;
11170 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11171 if (TREE_OPERAND (node, 2) == error_mark_node)
11172 return error_mark_node;
11173 return patch_if_else_statement (node);
11174 break;
11175
22eed1e6
APB
11176 case CONDITIONAL_EXPR:
11177 /* Condition */
11178 wfl_op1 = TREE_OPERAND (node, 0);
11179 COMPLETE_CHECK_OP_0 (node);
11180 wfl_op2 = TREE_OPERAND (node, 1);
11181 COMPLETE_CHECK_OP_1 (node);
11182 wfl_op3 = TREE_OPERAND (node, 2);
11183 COMPLETE_CHECK_OP_2 (node);
11184 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11185
e04a16fb
AG
11186 /* 3- Expression section */
11187 case COMPOUND_EXPR:
15fdcfe9 11188 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11189 TREE_OPERAND (node, 0) = nn =
11190 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11191 if (wfl_op2 == empty_stmt_node)
11192 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11193 else
15fdcfe9 11194 {
dc0b3eff 11195 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11196 {
dc0b3eff
PB
11197 /* An unreachable condition in a do-while statement
11198 is *not* (technically) an unreachable statement. */
11199 nn = wfl_op2;
11200 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11201 nn = EXPR_WFL_NODE (nn);
11202 if (TREE_CODE (nn) != EXIT_EXPR)
11203 {
11204 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11205 parse_error_context (wfl_operator, "Unreachable statement");
11206 }
bccaf73a 11207 }
dc0b3eff
PB
11208 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11209 if (TREE_OPERAND (node, 1) == error_mark_node)
11210 return error_mark_node;
11211 CAN_COMPLETE_NORMALLY (node)
11212 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11213 }
e04a16fb
AG
11214 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11215 break;
11216
11217 case RETURN_EXPR:
15fdcfe9 11218 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11219 return patch_return (node);
11220
11221 case EXPR_WITH_FILE_LOCATION:
11222 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11223 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11224 {
5423609c 11225 tree wfl = node;
15fdcfe9 11226 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11227 if (node == error_mark_node)
11228 return node;
5423609c
APB
11229 /* Keep line number information somewhere were it doesn't
11230 disrupt the completion process. */
2c56429a 11231 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11232 {
11233 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11234 TREE_OPERAND (node, 1) = wfl;
11235 }
15fdcfe9
PB
11236 CAN_COMPLETE_NORMALLY (node) = 1;
11237 }
e04a16fb
AG
11238 else
11239 {
5b09b33e
PB
11240 tree body;
11241 int save_lineno = lineno;
11242 lineno = EXPR_WFL_LINENO (node);
11243 body = java_complete_tree (EXPR_WFL_NODE (node));
11244 lineno = save_lineno;
15fdcfe9 11245 EXPR_WFL_NODE (node) = body;
dc0b3eff 11246 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11247 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11248 if (body == empty_stmt_node)
11249 {
11250 /* Optimization; makes it easier to detect empty bodies. */
11251 return body;
11252 }
dc0b3eff 11253 if (body == error_mark_node)
e04a16fb
AG
11254 {
11255 /* Its important for the evaluation of assignment that
11256 this mark on the TREE_TYPE is propagated. */
11257 TREE_TYPE (node) = error_mark_node;
11258 return error_mark_node;
11259 }
11260 else
11261 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11262
e04a16fb
AG
11263 }
11264 break;
11265
b67d701b 11266 case NEW_ARRAY_EXPR:
e04a16fb
AG
11267 /* Patch all the dimensions */
11268 flag = 0;
11269 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11270 {
11271 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11272 tree dim = convert (int_type_node,
11273 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11274 if (dim == error_mark_node)
11275 {
11276 flag = 1;
11277 continue;
11278 }
11279 else
11280 {
b9f7e36c 11281 TREE_VALUE (cn) = dim;
e04a16fb
AG
11282 /* Setup the location of the current dimension, for
11283 later error report. */
11284 TREE_PURPOSE (cn) =
11285 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11286 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11287 }
11288 }
11289 /* They complete the array creation expression, if no errors
11290 were found. */
15fdcfe9 11291 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11292 return (flag ? error_mark_node
11293 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11294
c2952b01
APB
11295 case NEW_ANONYMOUS_ARRAY_EXPR:
11296 /* Create the array type if necessary. */
11297 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11298 {
11299 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11300 if (!(type = resolve_type_during_patch (type)))
11301 return error_mark_node;
11302 type = build_array_from_name (type, NULL_TREE,
11303 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11304 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11305 }
11306 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11307 ANONYMOUS_ARRAY_INITIALIZER (node));
11308 if (node == error_mark_node)
11309 return error_mark_node;
11310 CAN_COMPLETE_NORMALLY (node) = 1;
11311 return node;
11312
b67d701b 11313 case NEW_CLASS_EXPR:
e04a16fb 11314 case CALL_EXPR:
b67d701b 11315 /* Complete function's argument(s) first */
e04a16fb
AG
11316 if (complete_function_arguments (node))
11317 return error_mark_node;
11318 else
b9f7e36c 11319 {
22eed1e6
APB
11320 tree decl, wfl = TREE_OPERAND (node, 0);
11321 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11322
c877974e 11323 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11324 NULL_TREE, 0, &decl);
c877974e
APB
11325 if (node == error_mark_node)
11326 return error_mark_node;
11327
11328 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11329 /* If we call this(...), register signature and positions */
11330 if (in_this)
11331 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11332 tree_cons (wfl, decl,
11333 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11334 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11335 return force_evaluation_order (node);
b9f7e36c 11336 }
e04a16fb
AG
11337
11338 case MODIFY_EXPR:
11339 /* Save potential wfls */
11340 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11341 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11342
cd9643f7
PB
11343 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11344 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11345 && DECL_INITIAL (nn) != NULL_TREE)
11346 {
100f7cd8
APB
11347 tree value;
11348
11349 push_obstacks (&permanent_obstack, &permanent_obstack);
11350 value = fold_constant_for_init (nn, nn);
11351 pop_obstacks ();
c2952b01 11352
cd9643f7
PB
11353 if (value != NULL_TREE)
11354 {
11355 tree type = TREE_TYPE (value);
c2952b01
APB
11356 if (JPRIMITIVE_TYPE_P (type) ||
11357 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11358 return empty_stmt_node;
11359 }
629d4b4d
APB
11360 if (! flag_emit_class_files)
11361 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11362 }
e04a16fb 11363 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11364
e04a16fb
AG
11365 if (TREE_OPERAND (node, 0) == error_mark_node)
11366 return error_mark_node;
11367
5cbdba64
APB
11368 flag = COMPOUND_ASSIGN_P (wfl_op2);
11369 if (flag)
e04a16fb 11370 {
c2952b01
APB
11371 /* This might break when accessing outer field from inner
11372 class. TESTME, FIXME */
2aa11e97 11373 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11374
11375 /* Hand stablize the lhs on both places */
e04a16fb 11376 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11377 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11378 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11379
5cbdba64 11380 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11381 /* Now complete the RHS. We write it back later on. */
11382 nn = java_complete_tree (TREE_OPERAND (node, 1));
11383
642f15d1
APB
11384 if ((cn = patch_string (nn)))
11385 nn = cn;
11386
2aa11e97
APB
11387 /* The last part of the rewrite for E1 op= E2 is to have
11388 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11389 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11390 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11391
11392 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11393 }
11394
f8976021 11395 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11396 function to complete this RHS. Note that a NEW_ARRAY_INIT
11397 might have been already fully expanded if created as a result
11398 of processing an anonymous array initializer. We avoid doing
11399 the operation twice by testing whether the node already bears
11400 a type. */
11401 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11402 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11403 TREE_OPERAND (node, 1));
2aa11e97 11404 /* Otherwise we simply complete the RHS */
f8976021
APB
11405 else
11406 nn = java_complete_tree (TREE_OPERAND (node, 1));
11407
e04a16fb 11408 if (nn == error_mark_node)
c0d87ff6 11409 return error_mark_node;
2aa11e97
APB
11410
11411 /* Write back the RHS as we evaluated it. */
e04a16fb 11412 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11413
11414 /* In case we're handling = with a String as a RHS, we need to
11415 produce a String out of the RHS (it might still be a
11416 STRING_CST or a StringBuffer at this stage */
11417 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11418 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11419
11420 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11421 TREE_OPERAND (node, 1))))
11422 {
11423 /* We return error_mark_node if outer_field_access_fix
11424 detects we write into a final. */
11425 if (nn == error_mark_node)
11426 return error_mark_node;
11427 node = nn;
11428 }
11429 else
11430 {
11431 node = patch_assignment (node, wfl_op1, wfl_op2);
11432 /* Reorganize the tree if necessary. */
11433 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11434 || JSTRING_P (TREE_TYPE (node))))
11435 node = java_refold (node);
11436 }
11437
15fdcfe9
PB
11438 CAN_COMPLETE_NORMALLY (node) = 1;
11439 return node;
e04a16fb
AG
11440
11441 case MULT_EXPR:
11442 case PLUS_EXPR:
11443 case MINUS_EXPR:
11444 case LSHIFT_EXPR:
11445 case RSHIFT_EXPR:
11446 case URSHIFT_EXPR:
11447 case BIT_AND_EXPR:
11448 case BIT_XOR_EXPR:
11449 case BIT_IOR_EXPR:
11450 case TRUNC_MOD_EXPR:
c2952b01 11451 case TRUNC_DIV_EXPR:
e04a16fb
AG
11452 case RDIV_EXPR:
11453 case TRUTH_ANDIF_EXPR:
11454 case TRUTH_ORIF_EXPR:
11455 case EQ_EXPR:
11456 case NE_EXPR:
11457 case GT_EXPR:
11458 case GE_EXPR:
11459 case LT_EXPR:
11460 case LE_EXPR:
11461 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11462 knows how to handle those cases. */
11463 wfl_op1 = TREE_OPERAND (node, 0);
11464 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11465
15fdcfe9 11466 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11467 /* Don't complete string nodes if dealing with the PLUS operand. */
11468 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11469 {
11470 nn = java_complete_tree (wfl_op1);
11471 if (nn == error_mark_node)
11472 return error_mark_node;
48a840d9 11473
2aa11e97
APB
11474 TREE_OPERAND (node, 0) = nn;
11475 }
b67d701b 11476 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11477 {
11478 nn = java_complete_tree (wfl_op2);
11479 if (nn == error_mark_node)
11480 return error_mark_node;
48a840d9 11481
2aa11e97
APB
11482 TREE_OPERAND (node, 1) = nn;
11483 }
dc0b3eff 11484 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11485
5e942c50
APB
11486 case INSTANCEOF_EXPR:
11487 wfl_op1 = TREE_OPERAND (node, 0);
11488 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11489 if (flag_emit_xref)
11490 {
11491 TREE_TYPE (node) = boolean_type_node;
11492 return node;
11493 }
5e942c50
APB
11494 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11495
b67d701b 11496 case UNARY_PLUS_EXPR:
e04a16fb
AG
11497 case NEGATE_EXPR:
11498 case TRUTH_NOT_EXPR:
11499 case BIT_NOT_EXPR:
11500 case PREDECREMENT_EXPR:
11501 case PREINCREMENT_EXPR:
11502 case POSTDECREMENT_EXPR:
11503 case POSTINCREMENT_EXPR:
11504 case CONVERT_EXPR:
11505 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11506 how to handle those cases. */
11507 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11508 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11509 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11510 if (TREE_OPERAND (node, 0) == error_mark_node)
11511 return error_mark_node;
4a5f66c3
APB
11512 node = patch_unaryop (node, wfl_op1);
11513 CAN_COMPLETE_NORMALLY (node) = 1;
11514 break;
e04a16fb
AG
11515
11516 case ARRAY_REF:
11517 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11518 how to handle those cases. */
11519 wfl_op1 = TREE_OPERAND (node, 0);
11520 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11521 if (TREE_OPERAND (node, 0) == error_mark_node)
11522 return error_mark_node;
7f1d4866 11523 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11524 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11525 /* The same applies to wfl_op2 */
11526 wfl_op2 = TREE_OPERAND (node, 1);
11527 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11528 if (TREE_OPERAND (node, 1) == error_mark_node)
11529 return error_mark_node;
7f1d4866 11530 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11531 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11532 return patch_array_ref (node);
e04a16fb 11533
63a212ed
PB
11534 case RECORD_TYPE:
11535 return node;;
11536
11537 case COMPONENT_REF:
11538 /* The first step in the re-write of qualified name handling. FIXME.
11539 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11540 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11541 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11542 {
11543 tree name = TREE_OPERAND (node, 1);
11544 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11545 if (field == NULL_TREE)
11546 {
11547 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11548 return error_mark_node;
11549 }
11550 if (! FIELD_STATIC (field))
11551 {
11552 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11553 return error_mark_node;
11554 }
11555 return field;
11556 }
11557 else
11558 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11559 break;
9bbc7d9f 11560
b67d701b 11561 case THIS_EXPR:
e04a16fb
AG
11562 /* Can't use THIS in a static environment */
11563 if (!current_this)
11564 {
11565 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11566 parse_error_context (wfl_operator,
11567 "Keyword `this' used outside allowed context");
e04a16fb
AG
11568 TREE_TYPE (node) = error_mark_node;
11569 return error_mark_node;
11570 }
22eed1e6
APB
11571 if (ctxp->explicit_constructor_p)
11572 {
11573 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11574 parse_error_context
781b0558 11575 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11576 TREE_TYPE (node) = error_mark_node;
11577 return error_mark_node;
11578 }
e04a16fb 11579 return current_this;
c2952b01
APB
11580
11581 case CLASS_LITERAL:
11582 CAN_COMPLETE_NORMALLY (node) = 1;
11583 node = patch_incomplete_class_ref (node);
11584 if (node == error_mark_node)
11585 return error_mark_node;
11586 break;
11587
11588 case INSTANCE_INITIALIZERS_EXPR:
11589 in_instance_initializer++;
11590 node = java_complete_tree (TREE_OPERAND (node, 0));
11591 in_instance_initializer--;
11592 if (node != error_mark_node)
11593 TREE_TYPE (node) = void_type_node;
11594 else
11595 return error_mark_node;
11596 break;
e04a16fb 11597
e04a16fb 11598 default:
15fdcfe9 11599 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11600 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11601 and it's time to turn it into the appropriate String object */
11602 if ((nn = patch_string (node)))
11603 node = nn;
11604 else
11605 fatal ("No case for tree code `%s' - java_complete_tree\n",
11606 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11607 }
11608 return node;
11609}
11610
11611/* Complete function call's argument. Return a non zero value is an
11612 error was found. */
11613
11614static int
11615complete_function_arguments (node)
11616 tree node;
11617{
11618 int flag = 0;
11619 tree cn;
11620
f63991a8 11621 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11622 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11623 {
b67d701b 11624 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11625 parm = java_complete_tree (wfl);
c2952b01 11626
e04a16fb
AG
11627 if (parm == error_mark_node)
11628 {
11629 flag = 1;
11630 continue;
11631 }
b67d701b
PB
11632 /* If have a string literal that we haven't transformed yet or a
11633 crafted string buffer, as a result of use of the the String
11634 `+' operator. Build `parm.toString()' and expand it. */
11635 if ((temp = patch_string (parm)))
b9f7e36c 11636 parm = temp;
5e942c50
APB
11637 /* Inline PRIMTYPE.TYPE read access */
11638 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11639
5e942c50 11640 TREE_VALUE (cn) = parm;
e04a16fb 11641 }
f63991a8 11642 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11643 return flag;
11644}
11645
11646/* Sometimes (for loops and variable initialized during their
11647 declaration), we want to wrap a statement around a WFL and turn it
11648 debugable. */
11649
11650static tree
11651build_debugable_stmt (location, stmt)
11652 int location;
11653 tree stmt;
11654{
11655 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11656 {
11657 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11658 EXPR_WFL_LINECOL (stmt) = location;
11659 }
11660 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11661 return stmt;
11662}
11663
11664static tree
11665build_expr_block (body, decls)
11666 tree body, decls;
11667{
11668 tree node = make_node (BLOCK);
11669 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11670 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11671 if (body)
11672 TREE_TYPE (node) = TREE_TYPE (body);
11673 TREE_SIDE_EFFECTS (node) = 1;
11674 return node;
11675}
11676
b67d701b
PB
11677/* Create a new function block and link it approriately to current
11678 function block chain */
e04a16fb
AG
11679
11680static tree
11681enter_block ()
11682{
b67d701b
PB
11683 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11684}
11685
11686/* Link block B supercontext to the previous block. The current
11687 function DECL is used as supercontext when enter_a_block is called
11688 for the first time for a given function. The current function body
11689 (DECL_FUNCTION_BODY) is set to be block B. */
11690
11691static tree
11692enter_a_block (b)
11693 tree b;
11694{
e04a16fb
AG
11695 tree fndecl = current_function_decl;
11696
f099f336
APB
11697 if (!fndecl) {
11698 BLOCK_SUPERCONTEXT (b) = current_static_block;
11699 current_static_block = b;
11700 }
11701
11702 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11703 {
11704 BLOCK_SUPERCONTEXT (b) = fndecl;
11705 DECL_FUNCTION_BODY (fndecl) = b;
11706 }
11707 else
11708 {
11709 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11710 DECL_FUNCTION_BODY (fndecl) = b;
11711 }
11712 return b;
11713}
11714
11715/* Exit a block by changing the current function body
11716 (DECL_FUNCTION_BODY) to the current block super context, only if
11717 the block being exited isn't the method's top level one. */
11718
11719static tree
11720exit_block ()
11721{
f099f336
APB
11722 tree b;
11723 if (current_function_decl)
11724 {
11725 b = DECL_FUNCTION_BODY (current_function_decl);
11726 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11727 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11728 }
11729 else
11730 {
11731 b = current_static_block;
e04a16fb 11732
f099f336
APB
11733 if (BLOCK_SUPERCONTEXT (b))
11734 current_static_block = BLOCK_SUPERCONTEXT (b);
11735 }
e04a16fb
AG
11736 return b;
11737}
11738
11739/* Lookup for NAME in the nested function's blocks, all the way up to
11740 the current toplevel one. It complies with Java's local variable
11741 scoping rules. */
11742
11743static tree
11744lookup_name_in_blocks (name)
11745 tree name;
11746{
f099f336 11747 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11748
11749 while (b != current_function_decl)
11750 {
11751 tree current;
11752
11753 /* Paranoid sanity check. To be removed */
11754 if (TREE_CODE (b) != BLOCK)
11755 fatal ("non block expr function body - lookup_name_in_blocks");
11756
11757 for (current = BLOCK_EXPR_DECLS (b); current;
11758 current = TREE_CHAIN (current))
11759 if (DECL_NAME (current) == name)
11760 return current;
11761 b = BLOCK_SUPERCONTEXT (b);
11762 }
11763 return NULL_TREE;
11764}
11765
11766static void
11767maybe_absorb_scoping_blocks ()
11768{
f099f336 11769 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11770 {
11771 tree b = exit_block ();
11772 java_method_add_stmt (current_function_decl, b);
11773 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11774 }
11775}
11776
11777\f
11778/* This section of the source is reserved to build_* functions that
11779 are building incomplete tree nodes and the patch_* functions that
11780 are completing them. */
11781
c2952b01
APB
11782/* Wrap a non WFL node around a WFL. */
11783static tree
9a7ab4b3 11784build_wfl_wrap (node, location)
c2952b01 11785 tree node;
9a7ab4b3 11786 int location;
c2952b01
APB
11787{
11788 tree wfl, node_to_insert = node;
11789
11790 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11791 with the way we're processing SUPER. A THIS from a primary as a
11792 different form than a SUPER. Turn THIS into something symbolic */
11793 if (TREE_CODE (node) == THIS_EXPR)
11794 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11795 else
11796 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11797
9a7ab4b3 11798 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11799 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11800 return wfl;
11801}
11802
11803
9bbc7d9f 11804/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11805 we're currently dealing with the class java.lang.Object. */
11806
11807static tree
e920ebc9
APB
11808build_super_invocation (mdecl)
11809 tree mdecl;
22eed1e6 11810{
e920ebc9 11811 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11812 return empty_stmt_node;
22eed1e6
APB
11813 else
11814 {
9ee9b555 11815 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11816 tree a = NULL_TREE, t;
11817 /* If we're dealing with an anonymous class, pass the arguments
11818 of the crafted constructor along. */
11819 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11820 {
11821 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11822 for (; t != end_params_node; t = TREE_CHAIN (t))
11823 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11824 }
11825 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11826 }
11827}
11828
11829/* Build a SUPER/THIS qualified method invocation. */
11830
11831static tree
11832build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11833 int use_this;
11834 tree name, args;
11835 int lloc, rloc;
22eed1e6
APB
11836{
11837 tree invok;
11838 tree wfl =
9ee9b555 11839 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11840 EXPR_WFL_LINECOL (wfl) = lloc;
11841 invok = build_method_invocation (name, args);
11842 return make_qualified_primary (wfl, invok, rloc);
11843}
11844
b67d701b 11845/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11846
11847static tree
11848build_method_invocation (name, args)
11849 tree name;
11850 tree args;
11851{
11852 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11853 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11854 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11855 return call;
11856}
11857
11858/* Build an incomplete new xxx(...) node. */
11859
11860static tree
11861build_new_invocation (name, args)
11862 tree name, args;
11863{
11864 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11865 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11866 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11867 return call;
11868}
11869
11870/* Build an incomplete assignment expression. */
11871
11872static tree
11873build_assignment (op, op_location, lhs, rhs)
11874 int op, op_location;
11875 tree lhs, rhs;
11876{
11877 tree assignment;
11878 /* Build the corresponding binop if we deal with a Compound
11879 Assignment operator. Mark the binop sub-tree as part of a
11880 Compound Assignment expression */
11881 if (op != ASSIGN_TK)
11882 {
11883 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11884 COMPOUND_ASSIGN_P (rhs) = 1;
11885 }
11886 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11887 TREE_SIDE_EFFECTS (assignment) = 1;
11888 EXPR_WFL_LINECOL (assignment) = op_location;
11889 return assignment;
11890}
11891
11892/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11893
15fdcfe9 11894char *
e04a16fb
AG
11895print_int_node (node)
11896 tree node;
11897{
11898 static char buffer [80];
11899 if (TREE_CONSTANT_OVERFLOW (node))
11900 sprintf (buffer, "<overflow>");
11901
11902 if (TREE_INT_CST_HIGH (node) == 0)
11903 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11904 TREE_INT_CST_LOW (node));
11905 else if (TREE_INT_CST_HIGH (node) == -1
11906 && TREE_INT_CST_LOW (node) != 0)
11907 {
11908 buffer [0] = '-';
11909 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11910 -TREE_INT_CST_LOW (node));
11911 }
11912 else
11913 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11914 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11915
11916 return buffer;
11917}
11918
7f1d4866
APB
11919/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11920 context. */
5e942c50
APB
11921
11922static int
11923check_final_assignment (lvalue, wfl)
11924 tree lvalue, wfl;
11925{
6632dcdd
APB
11926 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11927 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11928 lvalue = TREE_OPERAND (lvalue, 1);
11929
bc2874c9
TT
11930 /* When generating class files, references to the `length' field
11931 look a bit different. */
11932 if ((flag_emit_class_files
11933 && TREE_CODE (lvalue) == COMPONENT_REF
11934 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11935 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11936 || (TREE_CODE (lvalue) == FIELD_DECL
11937 && FIELD_FINAL (lvalue)
11938 && !DECL_CLINIT_P (current_function_decl)
11939 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11940 {
11941 parse_error_context
11942 (wfl, "Can't assign a value to the final variable `%s'",
11943 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11944 return 1;
11945 }
11946 return 0;
11947}
11948
11949/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11950 read. This is needed to avoid circularities in the implementation
11951 of these fields in libjava. */
11952
11953static tree
11954maybe_build_primttype_type_ref (rhs, wfl)
11955 tree rhs, wfl;
11956{
11957 tree to_return = NULL_TREE;
11958 tree rhs_type = TREE_TYPE (rhs);
11959 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11960 {
11961 tree n = TREE_OPERAND (rhs, 1);
11962 if (TREE_CODE (n) == VAR_DECL
11963 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
11964 && rhs_type == class_ptr_type
11965 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 11966 {
49f48c71 11967 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11968 if (!strncmp (self_name, "java.lang.", 10))
11969 to_return = build_primtype_type_ref (self_name);
11970 }
11971 }
11972 return (to_return ? to_return : rhs );
11973}
11974
e04a16fb
AG
11975/* 15.25 Assignment operators. */
11976
11977static tree
11978patch_assignment (node, wfl_op1, wfl_op2)
11979 tree node;
11980 tree wfl_op1;
11981 tree wfl_op2;
11982{
0a2138e2 11983 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11984 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11985 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11986 int error_found = 0;
11987 int lvalue_from_array = 0;
11988
c2952b01 11989 /* Can't assign to a (blank) final. */
5e942c50
APB
11990 if (check_final_assignment (lvalue, wfl_op1))
11991 error_found = 1;
e04a16fb
AG
11992
11993 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11994
11995 /* Lhs can be a named variable */
34f4db93 11996 if (JDECL_P (lvalue))
e04a16fb 11997 {
e04a16fb
AG
11998 lhs_type = TREE_TYPE (lvalue);
11999 }
12000 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12001 comment on reason why */
12002 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12003 {
12004 lhs_type = TREE_TYPE (lvalue);
12005 lvalue_from_array = 1;
12006 }
12007 /* Or a field access */
12008 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12009 lhs_type = TREE_TYPE (lvalue);
12010 /* Or a function return slot */
12011 else if (TREE_CODE (lvalue) == RESULT_DECL)
12012 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12013 /* Otherwise, we might want to try to write into an optimized static
12014 final, this is an of a different nature, reported further on. */
12015 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12016 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12017 {
6632dcdd 12018 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12019 {
12020 /* What we should do instead is resetting the all the flags
12021 previously set, exchange lvalue for llvalue and continue. */
12022 error_found = 1;
12023 return error_mark_node;
12024 }
12025 else
12026 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12027 }
12028 else
e04a16fb
AG
12029 {
12030 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12031 error_found = 1;
12032 }
12033
12034 rhs_type = TREE_TYPE (rhs);
b67d701b 12035 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12036 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12037
b67d701b 12038 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12039 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12040 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12041
12042 /* 15.25.2 If we have a compound assignment, convert RHS into the
12043 type of the LHS */
12044 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12045 new_rhs = convert (lhs_type, rhs);
12046
12047 /* Explicit cast required. This is an error */
12048 if (!new_rhs)
12049 {
c2e3db92
KG
12050 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12051 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12052 tree wfl;
12053 char operation [32]; /* Max size known */
12054
12055 /* If the assignment is part of a declaration, we use the WFL of
12056 the declared variable to point out the error and call it a
12057 declaration problem. If the assignment is a genuine =
12058 operator, we call is a operator `=' problem, otherwise we
12059 call it an assignment problem. In both of these last cases,
12060 we use the WFL of the operator to indicate the error. */
12061
12062 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12063 {
12064 wfl = wfl_op1;
12065 strcpy (operation, "declaration");
12066 }
12067 else
12068 {
12069 wfl = wfl_operator;
12070 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12071 strcpy (operation, "assignment");
12072 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12073 strcpy (operation, "`return'");
12074 else
12075 strcpy (operation, "`='");
12076 }
12077
1ebadc60 12078 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12079 parse_error_context
12080 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12081 operation, t1, t2);
1ebadc60 12082 else
781b0558 12083 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12084 operation, t1, t2);
e04a16fb
AG
12085 free (t1); free (t2);
12086 error_found = 1;
12087 }
12088
c877974e
APB
12089 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12090 if (new_rhs)
12091 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12092
e04a16fb
AG
12093 if (error_found)
12094 return error_mark_node;
12095
2622b947
APB
12096 /* 10.10: Array Store Exception runtime check */
12097 if (!flag_emit_class_files
e8fc7396 12098 && !flag_emit_xref
2622b947 12099 && lvalue_from_array
afc390b1 12100 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12101 {
12102 tree check;
12103 tree base = lvalue;
12104
12105 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12106 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12107 base = TREE_OPERAND (lvalue, 0);
12108 else
12109 {
12110 if (flag_bounds_check)
12111 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12112 else
12113 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12114 }
12115
12116 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12117 new_rhs = save_expr (new_rhs);
2622b947
APB
12118 check = build (CALL_EXPR, void_type_node,
12119 build_address_of (soft_checkarraystore_node),
12120 tree_cons (NULL_TREE, base,
12121 build_tree_list (NULL_TREE, new_rhs)),
12122 NULL_TREE);
12123 TREE_SIDE_EFFECTS (check) = 1;
12124
12125 /* We have to decide on an insertion point */
12126 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12127 {
12128 tree t;
12129 if (flag_bounds_check)
12130 {
12131 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12132 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12133 build (COMPOUND_EXPR, void_type_node, t, check);
12134 }
12135 else
12136 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12137 check, TREE_OPERAND (lvalue, 1));
12138 }
12139 else
12140 {
12141 /* Make sure the bound check will happen before the store check */
12142 if (flag_bounds_check)
12143 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12144 build (COMPOUND_EXPR, void_type_node,
12145 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12146 else
12147 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12148 }
12149 }
22eed1e6 12150
34d4df06
APB
12151 /* Final locals can be used as case values in switch
12152 statement. Prepare them for this eventuality. */
12153 if (TREE_CODE (lvalue) == VAR_DECL
12154 && LOCAL_FINAL (lvalue)
12155 && TREE_CONSTANT (new_rhs)
12156 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12157 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12158 )
12159 {
12160 TREE_CONSTANT (lvalue) = 1;
12161 DECL_INITIAL (lvalue) = new_rhs;
12162 }
12163
e04a16fb
AG
12164 TREE_OPERAND (node, 0) = lvalue;
12165 TREE_OPERAND (node, 1) = new_rhs;
12166 TREE_TYPE (node) = lhs_type;
12167 return node;
12168}
12169
b67d701b
PB
12170/* Check that type SOURCE can be cast into type DEST. If the cast
12171 can't occur at all, return 0 otherwise 1. This function is used to
12172 produce accurate error messages on the reasons why an assignment
12173 failed. */
e04a16fb 12174
b67d701b
PB
12175static tree
12176try_reference_assignconv (lhs_type, rhs)
12177 tree lhs_type, rhs;
e04a16fb 12178{
b67d701b
PB
12179 tree new_rhs = NULL_TREE;
12180 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12181
b67d701b
PB
12182 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12183 {
12184 /* `null' may be assigned to any reference type */
12185 if (rhs == null_pointer_node)
12186 new_rhs = null_pointer_node;
12187 /* Try the reference assignment conversion */
12188 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12189 new_rhs = rhs;
12190 /* This is a magic assignment that we process differently */
12191 else if (rhs == soft_exceptioninfo_call_node)
12192 new_rhs = rhs;
12193 }
12194 return new_rhs;
12195}
12196
12197/* Check that RHS can be converted into LHS_TYPE by the assignment
12198 conversion (5.2), for the cases of RHS being a builtin type. Return
12199 NULL_TREE if the conversion fails or if because RHS isn't of a
12200 builtin type. Return a converted RHS if the conversion is possible. */
12201
12202static tree
12203try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12204 tree wfl_op1, lhs_type, rhs;
12205{
12206 tree new_rhs = NULL_TREE;
12207 tree rhs_type = TREE_TYPE (rhs);
12208
5e942c50
APB
12209 /* Zero accepted everywhere */
12210 if (TREE_CODE (rhs) == INTEGER_CST
12211 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12212 && JPRIMITIVE_TYPE_P (rhs_type))
12213 new_rhs = convert (lhs_type, rhs);
12214
b67d701b
PB
12215 /* 5.1.1 Try Identity Conversion,
12216 5.1.2 Try Widening Primitive Conversion */
5e942c50 12217 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12218 new_rhs = convert (lhs_type, rhs);
12219
12220 /* Try a narrowing primitive conversion (5.1.3):
12221 - expression is a constant expression of type int AND
12222 - variable is byte, short or char AND
12223 - The value of the expression is representable in the type of the
12224 variable */
12225 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12226 && (lhs_type == byte_type_node || lhs_type == char_type_node
12227 || lhs_type == short_type_node))
12228 {
12229 if (int_fits_type_p (rhs, lhs_type))
12230 new_rhs = convert (lhs_type, rhs);
12231 else if (wfl_op1) /* Might be called with a NULL */
12232 parse_warning_context
781b0558 12233 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 12234 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12235 /* Reported a warning that will turn into an error further
12236 down, so we don't return */
12237 }
12238
12239 return new_rhs;
12240}
12241
12242/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12243 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12244 0 is the conversion test fails. This implements parts the method
12245 invocation convertion (5.3). */
12246
12247static int
12248valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12249 tree lhs_type, rhs_type;
12250{
acd663ee 12251 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12252 if (lhs_type == rhs_type)
12253 return 1;
12254
acd663ee
APB
12255 /* Reject non primitive types */
12256 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
12257 return 0;
12258
acd663ee
APB
12259 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12260 than a char can't be converted into a char. Short can't too, but
12261 the < test below takes care of that */
b67d701b
PB
12262 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12263 return 0;
12264
5e942c50
APB
12265 /* Accept all promoted type here. Note, we can't use <= in the test
12266 below, because we still need to bounce out assignments of short
12267 to char and the likes */
12268 if (lhs_type == int_type_node
12269 && (rhs_type == promoted_byte_type_node
12270 || rhs_type == promoted_short_type_node
12271 || rhs_type == promoted_char_type_node
12272 || rhs_type == promoted_boolean_type_node))
12273 return 1;
12274
acd663ee
APB
12275 /* From here, an integral is widened if its precision is smaller
12276 than the precision of the LHS or if the LHS is a floating point
12277 type, or the RHS is a float and the RHS a double. */
12278 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12279 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12280 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12281 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12282 return 1;
12283
12284 return 0;
e04a16fb
AG
12285}
12286
12287/* Check that something of SOURCE type can be assigned or cast to
12288 something of DEST type at runtime. Return 1 if the operation is
12289 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12290 were SOURCE is cast into DEST, which borrows a lot of the
12291 assignment check. */
12292
12293static int
12294valid_ref_assignconv_cast_p (source, dest, cast)
12295 tree source;
12296 tree dest;
12297 int cast;
12298{
09ed0f70
APB
12299 /* SOURCE or DEST might be null if not from a declared entity. */
12300 if (!source || !dest)
12301 return 0;
5e942c50
APB
12302 if (JNULLP_TYPE_P (source))
12303 return 1;
e04a16fb
AG
12304 if (TREE_CODE (source) == POINTER_TYPE)
12305 source = TREE_TYPE (source);
12306 if (TREE_CODE (dest) == POINTER_TYPE)
12307 dest = TREE_TYPE (dest);
12308 /* Case where SOURCE is a class type */
12309 if (TYPE_CLASS_P (source))
12310 {
12311 if (TYPE_CLASS_P (dest))
c2952b01
APB
12312 return (source == dest
12313 || inherits_from_p (source, dest)
c2952b01 12314 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12315 if (TYPE_INTERFACE_P (dest))
12316 {
12317 /* If doing a cast and SOURCE is final, the operation is
12318 always correct a compile time (because even if SOURCE
12319 does not implement DEST, a subclass of SOURCE might). */
12320 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12321 return 1;
12322 /* Otherwise, SOURCE must implement DEST */
12323 return interface_of_p (dest, source);
12324 }
12325 /* DEST is an array, cast permited if SOURCE is of Object type */
12326 return (cast && source == object_type_node ? 1 : 0);
12327 }
12328 if (TYPE_INTERFACE_P (source))
12329 {
12330 if (TYPE_CLASS_P (dest))
12331 {
12332 /* If not casting, DEST must be the Object type */
12333 if (!cast)
12334 return dest == object_type_node;
12335 /* We're doing a cast. The cast is always valid is class
12336 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12337 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12338 return 1;
12339 else
12340 return interface_of_p (source, dest);
12341 }
12342 if (TYPE_INTERFACE_P (dest))
12343 {
12344 /* If doing a cast, then if SOURCE and DEST contain method
12345 with the same signature but different return type, then
12346 this is a (compile time) error */
12347 if (cast)
12348 {
12349 tree method_source, method_dest;
12350 tree source_type;
0a2138e2 12351 tree source_sig;
e04a16fb
AG
12352 tree source_name;
12353 for (method_source = TYPE_METHODS (source); method_source;
12354 method_source = TREE_CHAIN (method_source))
12355 {
12356 source_sig =
12357 build_java_argument_signature (TREE_TYPE (method_source));
12358 source_type = TREE_TYPE (TREE_TYPE (method_source));
12359 source_name = DECL_NAME (method_source);
12360 for (method_dest = TYPE_METHODS (dest);
12361 method_dest; method_dest = TREE_CHAIN (method_dest))
12362 if (source_sig ==
12363 build_java_argument_signature (TREE_TYPE (method_dest))
12364 && source_name == DECL_NAME (method_dest)
12365 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12366 return 0;
12367 }
12368 return 1;
12369 }
12370 else
12371 return source == dest || interface_of_p (dest, source);
12372 }
ee17a290
TT
12373 else
12374 {
12375 /* Array */
12376 return (cast
12377 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12378 || (DECL_NAME (TYPE_NAME (source))
12379 == java_io_serializable)));
12380 }
e04a16fb
AG
12381 }
12382 if (TYPE_ARRAY_P (source))
12383 {
12384 if (TYPE_CLASS_P (dest))
12385 return dest == object_type_node;
09ed0f70 12386 /* Can't cast an array to an interface unless the interface is
ee17a290 12387 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12388 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12389 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12390 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12391 else /* Arrays */
12392 {
12393 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12394 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12395
b9f7e36c
APB
12396 /* In case of severe errors, they turn out null */
12397 if (!dest_element_type || !source_element_type)
12398 return 0;
e04a16fb
AG
12399 if (source_element_type == dest_element_type)
12400 return 1;
12401 return valid_ref_assignconv_cast_p (source_element_type,
12402 dest_element_type, cast);
12403 }
12404 return 0;
12405 }
12406 return 0;
12407}
12408
b67d701b
PB
12409static int
12410valid_cast_to_p (source, dest)
12411 tree source;
12412 tree dest;
12413{
12414 if (TREE_CODE (source) == POINTER_TYPE)
12415 source = TREE_TYPE (source);
12416 if (TREE_CODE (dest) == POINTER_TYPE)
12417 dest = TREE_TYPE (dest);
12418
12419 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12420 return valid_ref_assignconv_cast_p (source, dest, 1);
12421
12422 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12423 return 1;
12424
12425 return 0;
12426}
12427
12428/* Method invocation conversion test. Return 1 if type SOURCE can be
12429 converted to type DEST through the methond invocation conversion
12430 process (5.3) */
12431
15fdcfe9
PB
12432static tree
12433do_unary_numeric_promotion (arg)
12434 tree arg;
12435{
12436 tree type = TREE_TYPE (arg);
12437 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12438 : TREE_CODE (type) == CHAR_TYPE)
12439 arg = convert (int_type_node, arg);
12440 return arg;
12441}
12442
acd663ee
APB
12443/* Return a non zero value if SOURCE can be converted into DEST using
12444 the method invocation conversion rule (5.3). */
b67d701b
PB
12445static int
12446valid_method_invocation_conversion_p (dest, source)
12447 tree dest, source;
12448{
e3884b71 12449 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12450 && valid_builtin_assignconv_identity_widening_p (dest, source))
12451 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12452 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12453 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12454}
12455
e04a16fb
AG
12456/* Build an incomplete binop expression. */
12457
12458static tree
12459build_binop (op, op_location, op1, op2)
12460 enum tree_code op;
12461 int op_location;
12462 tree op1, op2;
12463{
5e942c50 12464 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12465 TREE_SIDE_EFFECTS (binop) = 1;
12466 /* Store the location of the operator, for better error report. The
12467 string of the operator will be rebuild based on the OP value. */
12468 EXPR_WFL_LINECOL (binop) = op_location;
12469 return binop;
12470}
12471
12472/* Build the string of the operator retained by NODE. If NODE is part
12473 of a compound expression, add an '=' at the end of the string. This
12474 function is called when an error needs to be reported on an
12475 operator. The string is returned as a pointer to a static character
12476 buffer. */
12477
12478static char *
12479operator_string (node)
12480 tree node;
12481{
12482#define BUILD_OPERATOR_STRING(S) \
12483 { \
12484 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12485 return buffer; \
12486 }
12487
12488 static char buffer [10];
12489 switch (TREE_CODE (node))
12490 {
12491 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12492 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12493 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12494 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12495 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12496 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12497 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12498 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12499 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12500 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12501 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12502 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12503 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12504 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12505 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12506 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12507 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12508 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12509 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12510 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12511 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12512 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12513 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12514 case PREINCREMENT_EXPR: /* Fall through */
12515 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12516 case PREDECREMENT_EXPR: /* Fall through */
12517 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12518 default:
12519 fatal ("unregistered operator %s - operator_string",
12520 tree_code_name [TREE_CODE (node)]);
12521 }
12522 return NULL;
12523#undef BUILD_OPERATOR_STRING
12524}
12525
5cbdba64
APB
12526/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12527
12528static int
12529java_decl_equiv (var_acc1, var_acc2)
12530 tree var_acc1, var_acc2;
12531{
12532 if (JDECL_P (var_acc1))
12533 return (var_acc1 == var_acc2);
12534
12535 return (TREE_CODE (var_acc1) == COMPONENT_REF
12536 && TREE_CODE (var_acc2) == COMPONENT_REF
12537 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12538 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12539 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12540}
12541
12542/* Return a non zero value if CODE is one of the operators that can be
12543 used in conjunction with the `=' operator in a compound assignment. */
12544
12545static int
12546binop_compound_p (code)
12547 enum tree_code code;
12548{
12549 int i;
12550 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12551 if (binop_lookup [i] == code)
12552 break;
12553
12554 return i < BINOP_COMPOUND_CANDIDATES;
12555}
12556
12557/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12558
12559static tree
12560java_refold (t)
12561 tree t;
12562{
12563 tree c, b, ns, decl;
12564
12565 if (TREE_CODE (t) != MODIFY_EXPR)
12566 return t;
12567
12568 c = TREE_OPERAND (t, 1);
12569 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12570 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12571 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12572 return t;
12573
12574 /* Now the left branch of the binary operator. */
12575 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12576 if (! (b && TREE_CODE (b) == NOP_EXPR
12577 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12578 return t;
12579
12580 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12581 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12582 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12583 return t;
12584
12585 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12586 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12587 /* It's got to be the an equivalent decl */
12588 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12589 {
12590 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12591 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12592 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12593 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12594 /* Change the right part of the BINOP_EXPR */
12595 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12596 }
12597
12598 return t;
12599}
12600
e04a16fb
AG
12601/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12602 errors but we modify NODE so that it contains the type computed
12603 according to the expression, when it's fixed. Otherwise, we write
12604 error_mark_node as the type. It allows us to further the analysis
12605 of remaining nodes and detects more errors in certain cases. */
12606
12607static tree
12608patch_binop (node, wfl_op1, wfl_op2)
12609 tree node;
12610 tree wfl_op1;
12611 tree wfl_op2;
12612{
12613 tree op1 = TREE_OPERAND (node, 0);
12614 tree op2 = TREE_OPERAND (node, 1);
12615 tree op1_type = TREE_TYPE (op1);
12616 tree op2_type = TREE_TYPE (op2);
48a840d9 12617 tree prom_type = NULL_TREE, cn;
e04a16fb 12618 int code = TREE_CODE (node);
b67d701b 12619
e04a16fb
AG
12620 /* If 1, tell the routine that we have to return error_mark_node
12621 after checking for the initialization of the RHS */
12622 int error_found = 0;
12623
e04a16fb
AG
12624 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12625
e04a16fb
AG
12626 switch (code)
12627 {
12628 /* 15.16 Multiplicative operators */
12629 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12630 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12631 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12632 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12633 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12634 {
12635 if (!JPRIMITIVE_TYPE_P (op1_type))
12636 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12637 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12638 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12639 TREE_TYPE (node) = error_mark_node;
12640 error_found = 1;
12641 break;
12642 }
12643 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12644 /* Change the division operator if necessary */
12645 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12646 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12647
aa4759c1
AH
12648 if (TREE_CODE (prom_type) == INTEGER_TYPE
12649 && flag_use_divide_subroutine
12650 && ! flag_emit_class_files
12651 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12652 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12653
0b4d333e
APB
12654 /* This one is more complicated. FLOATs are processed by a
12655 function call to soft_fmod. Duplicate the value of the
12656 COMPOUND_ASSIGN_P flag. */
e04a16fb 12657 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12658 {
12659 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12660 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12661 TREE_SIDE_EFFECTS (mod)
12662 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12663 return mod;
12664 }
e04a16fb
AG
12665 break;
12666
12667 /* 15.17 Additive Operators */
12668 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12669
12670 /* Operation is valid if either one argument is a string
12671 constant, a String object or a StringBuffer crafted for the
12672 purpose of the a previous usage of the String concatenation
12673 operator */
12674
12675 if (TREE_CODE (op1) == STRING_CST
12676 || TREE_CODE (op2) == STRING_CST
12677 || JSTRING_TYPE_P (op1_type)
12678 || JSTRING_TYPE_P (op2_type)
12679 || IS_CRAFTED_STRING_BUFFER_P (op1)
12680 || IS_CRAFTED_STRING_BUFFER_P (op2))
12681 return build_string_concatenation (op1, op2);
12682
e04a16fb
AG
12683 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12684 Numeric Types */
12685 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12686 {
12687 if (!JPRIMITIVE_TYPE_P (op1_type))
12688 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12689 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12690 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12691 TREE_TYPE (node) = error_mark_node;
12692 error_found = 1;
12693 break;
12694 }
12695 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12696 break;
12697
12698 /* 15.18 Shift Operators */
12699 case LSHIFT_EXPR:
12700 case RSHIFT_EXPR:
12701 case URSHIFT_EXPR:
12702 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12703 {
12704 if (!JINTEGRAL_TYPE_P (op1_type))
12705 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12706 else
1ebadc60
KG
12707 {
12708 if (JPRIMITIVE_TYPE_P (op2_type))
12709 parse_error_context (wfl_operator,
781b0558 12710 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12711 operator_string (node),
12712 lang_printable_name (op2_type, 0));
12713 else
781b0558
KG
12714 parse_error_context (wfl_operator,
12715 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12716 operator_string (node),
12717 lang_printable_name (op2_type, 0));
12718 }
e04a16fb
AG
12719 TREE_TYPE (node) = error_mark_node;
12720 error_found = 1;
12721 break;
12722 }
12723
12724 /* Unary numeric promotion (5.6.1) is performed on each operand
12725 separatly */
15fdcfe9
PB
12726 op1 = do_unary_numeric_promotion (op1);
12727 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12728
12729 /* The type of the shift expression is the type of the promoted
12730 type of the left-hand operand */
12731 prom_type = TREE_TYPE (op1);
12732
c2952b01
APB
12733 /* Shift int only up to 0x1f and long up to 0x3f */
12734 if (prom_type == int_type_node)
12735 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12736 build_int_2 (0x1f, 0)));
12737 else
12738 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12739 build_int_2 (0x3f, 0)));
e04a16fb
AG
12740
12741 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12742 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12743 {
0b4d333e 12744 tree to_return;
73333a87
AH
12745 tree utype = unsigned_type (prom_type);
12746 op1 = convert (utype, op1);
e04a16fb 12747 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12748 TREE_OPERAND (node, 0) = op1;
12749 TREE_OPERAND (node, 1) = op2;
12750 TREE_TYPE (node) = utype;
0b4d333e
APB
12751 to_return = convert (prom_type, node);
12752 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12753 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12754 TREE_SIDE_EFFECTS (to_return)
12755 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12756 return to_return;
e04a16fb
AG
12757 }
12758 break;
5e942c50
APB
12759
12760 /* 15.19.1 Type Comparison Operator instaceof */
12761 case INSTANCEOF_EXPR:
12762
12763 TREE_TYPE (node) = boolean_type_node;
12764
12765 if (!(op2_type = resolve_type_during_patch (op2)))
12766 return error_mark_node;
12767
12768 /* The first operand must be a reference type or the null type */
12769 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12770 error_found = 1; /* Error reported further below */
12771
12772 /* The second operand must be a reference type */
12773 if (!JREFERENCE_TYPE_P (op2_type))
12774 {
12775 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12776 parse_error_context
12777 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12778 lang_printable_name (op2_type, 0));
12779 error_found = 1;
12780 }
12781
12782 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12783 {
12784 /* If the first operand is null, the result is always false */
12785 if (op1 == null_pointer_node)
12786 return boolean_false_node;
15fdcfe9
PB
12787 else if (flag_emit_class_files)
12788 {
12789 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12790 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12791 return node;
12792 }
5e942c50
APB
12793 /* Otherwise we have to invoke instance of to figure it out */
12794 else
67db0ce7 12795 return build_instanceof (op1, op2_type);
5e942c50
APB
12796 }
12797 /* There is no way the expression operand can be an instance of
12798 the type operand. This is a compile time error. */
12799 else
12800 {
c2e3db92 12801 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12802 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12803 parse_error_context
12804 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12805 t1, lang_printable_name (op2_type, 0));
12806 free (t1);
12807 error_found = 1;
12808 }
e04a16fb 12809
5e942c50 12810 break;
e04a16fb
AG
12811
12812 /* 15.21 Bitwise and Logical Operators */
12813 case BIT_AND_EXPR:
12814 case BIT_XOR_EXPR:
12815 case BIT_IOR_EXPR:
12816 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12817 /* Binary numeric promotion is performed on both operand and the
12818 expression retain that type */
12819 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12820
12821 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12822 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12823 /* The type of the bitwise operator expression is BOOLEAN */
12824 prom_type = boolean_type_node;
12825 else
12826 {
12827 if (!JINTEGRAL_TYPE_P (op1_type))
12828 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12829 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12830 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12831 TREE_TYPE (node) = error_mark_node;
12832 error_found = 1;
12833 /* Insert a break here if adding thing before the switch's
12834 break for this case */
12835 }
12836 break;
12837
12838 /* 15.22 Conditional-And Operator */
12839 case TRUTH_ANDIF_EXPR:
12840 /* 15.23 Conditional-Or Operator */
12841 case TRUTH_ORIF_EXPR:
12842 /* Operands must be of BOOLEAN type */
12843 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12844 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12845 {
12846 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12847 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12848 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12849 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12850 TREE_TYPE (node) = boolean_type_node;
12851 error_found = 1;
12852 break;
12853 }
12854 /* The type of the conditional operators is BOOLEAN */
12855 prom_type = boolean_type_node;
12856 break;
12857
12858 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12859 case LT_EXPR:
12860 case GT_EXPR:
12861 case LE_EXPR:
12862 case GE_EXPR:
12863 /* The type of each of the operands must be a primitive numeric
12864 type */
12865 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12866 {
12867 if (!JNUMERIC_TYPE_P (op1_type))
12868 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12869 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12870 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12871 TREE_TYPE (node) = boolean_type_node;
12872 error_found = 1;
12873 break;
12874 }
12875 /* Binary numeric promotion is performed on the operands */
12876 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12877 /* The type of the relation expression is always BOOLEAN */
12878 prom_type = boolean_type_node;
12879 break;
12880
12881 /* 15.20 Equality Operator */
12882 case EQ_EXPR:
12883 case NE_EXPR:
48a840d9
APB
12884 /* It's time for us to patch the strings. */
12885 if ((cn = patch_string (op1)))
12886 {
12887 op1 = cn;
12888 op1_type = TREE_TYPE (op1);
12889 }
12890 if ((cn = patch_string (op2)))
12891 {
12892 op2 = cn;
12893 op2_type = TREE_TYPE (op2);
12894 }
12895
e04a16fb
AG
12896 /* 15.20.1 Numerical Equality Operators == and != */
12897 /* Binary numeric promotion is performed on the operands */
5e942c50 12898 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12899 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12900
12901 /* 15.20.2 Boolean Equality Operators == and != */
12902 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12903 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12904 ; /* Nothing to do here */
12905
12906 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12907 /* Types have to be either references or the null type. If
12908 they're references, it must be possible to convert either
12909 type to the other by casting conversion. */
b9f7e36c
APB
12910 else if (op1 == null_pointer_node || op2 == null_pointer_node
12911 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12912 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12913 || valid_ref_assignconv_cast_p (op2_type,
12914 op1_type, 1))))
e04a16fb
AG
12915 ; /* Nothing to do here */
12916
12917 /* Else we have an error figure what can't be converted into
12918 what and report the error */
12919 else
12920 {
12921 char *t1;
c2e3db92 12922 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12923 parse_error_context
781b0558
KG
12924 (wfl_operator,
12925 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12926 operator_string (node), t1,
0a2138e2 12927 lang_printable_name (op2_type, 0));
e04a16fb
AG
12928 free (t1);
12929 TREE_TYPE (node) = boolean_type_node;
12930 error_found = 1;
12931 break;
12932 }
12933 prom_type = boolean_type_node;
12934 break;
12935 }
12936
e04a16fb
AG
12937 if (error_found)
12938 return error_mark_node;
12939
12940 TREE_OPERAND (node, 0) = op1;
12941 TREE_OPERAND (node, 1) = op2;
12942 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12943 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12944
ce6e9147
APB
12945 if (flag_emit_xref)
12946 return node;
12947
d1472141
PB
12948 /* fold does not respect side-effect order as required for Java but not C.
12949 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12950 * bytecode.
12951 */
12952 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12953 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12954 node = fold (node);
12955 return node;
e04a16fb
AG
12956}
12957
b67d701b
PB
12958/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12959 zero value, the value of CSTE comes after the valude of STRING */
12960
12961static tree
12962do_merge_string_cste (cste, string, string_len, after)
12963 tree cste;
49f48c71 12964 const char *string;
b67d701b
PB
12965 int string_len, after;
12966{
49f48c71 12967 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
12968 int old_len = TREE_STRING_LENGTH (cste);
12969 int len = old_len + string_len;
12970 char *new;
12971
12972 cste = make_node (STRING_CST);
b67d701b 12973 TREE_STRING_LENGTH (cste) = len;
354e99ce
APB
12974 new = TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12975
b67d701b
PB
12976 if (after)
12977 {
354e99ce
APB
12978 memcpy (new, string, string_len);
12979 memcpy (&new [string_len], old, old_len);
b67d701b
PB
12980 }
12981 else
12982 {
354e99ce
APB
12983 memcpy (new, old, old_len);
12984 memcpy (&new [old_len], string, string_len);
b67d701b 12985 }
354e99ce 12986 new [len] = '\0';
b67d701b
PB
12987 return cste;
12988}
12989
12990/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12991 new STRING_CST on success, NULL_TREE on failure */
12992
12993static tree
12994merge_string_cste (op1, op2, after)
12995 tree op1, op2;
12996 int after;
12997{
12998 /* Handle two string constants right away */
12999 if (TREE_CODE (op2) == STRING_CST)
13000 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13001 TREE_STRING_LENGTH (op2), after);
13002
13003 /* Reasonable integer constant can be treated right away */
13004 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13005 {
49f48c71
KG
13006 static const char *boolean_true = "true";
13007 static const char *boolean_false = "false";
13008 static const char *null_pointer = "null";
b67d701b 13009 char ch[3];
49f48c71 13010 const char *string;
b67d701b
PB
13011
13012 if (op2 == boolean_true_node)
13013 string = boolean_true;
13014 else if (op2 == boolean_false_node)
13015 string = boolean_false;
13016 else if (op2 == null_pointer_node)
13017 string = null_pointer;
13018 else if (TREE_TYPE (op2) == char_type_node)
13019 {
13020 ch[0] = (char )TREE_INT_CST_LOW (op2);
13021 ch[1] = '\0';
13022 string = ch;
13023 }
13024 else
13025 string = print_int_node (op2);
13026
13027 return do_merge_string_cste (op1, string, strlen (string), after);
13028 }
13029 return NULL_TREE;
13030}
13031
13032/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13033 has to be a STRING_CST and the other part must be a STRING_CST or a
13034 INTEGRAL constant. Return a new STRING_CST if the operation
13035 succeed, NULL_TREE otherwise.
13036
13037 If the case we want to optimize for space, we might want to return
13038 NULL_TREE for each invocation of this routine. FIXME */
13039
13040static tree
13041string_constant_concatenation (op1, op2)
13042 tree op1, op2;
13043{
13044 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13045 {
0a2138e2 13046 tree string, rest;
b67d701b
PB
13047 int invert;
13048
13049 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13050 rest = (string == op1 ? op2 : op1);
13051 invert = (string == op1 ? 0 : 1 );
13052
13053 /* Walk REST, only if it looks reasonable */
13054 if (TREE_CODE (rest) != STRING_CST
13055 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13056 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13057 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13058 {
13059 rest = java_complete_tree (rest);
13060 if (rest == error_mark_node)
13061 return error_mark_node;
13062 rest = fold (rest);
13063 }
13064 return merge_string_cste (string, rest, invert);
13065 }
13066 return NULL_TREE;
13067}
13068
13069/* Implement the `+' operator. Does static optimization if possible,
13070 otherwise create (if necessary) and append elements to a
13071 StringBuffer. The StringBuffer will be carried around until it is
13072 used for a function call or an assignment. Then toString() will be
13073 called on it to turn it into a String object. */
13074
13075static tree
13076build_string_concatenation (op1, op2)
13077 tree op1, op2;
13078{
13079 tree result;
dc0b3eff 13080 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13081
13082 if (flag_emit_xref)
13083 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13084
13085 /* Try to do some static optimization */
13086 if ((result = string_constant_concatenation (op1, op2)))
13087 return result;
13088
c0d87ff6
PB
13089 /* Discard empty strings on either side of the expression */
13090 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13091 {
13092 op1 = op2;
13093 op2 = NULL_TREE;
13094 }
c0d87ff6 13095 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13096 op2 = NULL_TREE;
b67d701b 13097
acd663ee 13098 /* If operands are string constant, turn then into object references */
b67d701b
PB
13099 if (TREE_CODE (op1) == STRING_CST)
13100 op1 = patch_string_cst (op1);
acd663ee 13101 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13102 op2 = patch_string_cst (op2);
13103
acd663ee
APB
13104 /* If either one of the constant is null and the other non null
13105 operand is a String object, return it. */
13106 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13107 return op1;
13108
b67d701b
PB
13109 /* If OP1 isn't already a StringBuffer, create and
13110 initialize a new one */
13111 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13112 {
13113 /* Two solutions here:
c52b5771
AG
13114 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13115 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13116 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13117 op1 = BUILD_STRING_BUFFER (op1);
13118 else
13119 {
13120 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13121 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13122 }
13123 }
13124
acd663ee
APB
13125 if (op2)
13126 {
13127 /* OP1 is no longer the last node holding a crafted StringBuffer */
13128 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13129 /* Create a node for `{new...,xxx}.append (op2)' */
13130 if (op2)
13131 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13132 }
13133
b67d701b
PB
13134 /* Mark the last node holding a crafted StringBuffer */
13135 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13136
13137 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13138 return op1;
13139}
13140
13141/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13142 StringBuffer. If no string were found to be patched, return
13143 NULL. */
13144
13145static tree
13146patch_string (node)
13147 tree node;
13148{
1179ebc2
APB
13149 if (node == error_mark_node)
13150 return error_mark_node;
b67d701b
PB
13151 if (TREE_CODE (node) == STRING_CST)
13152 return patch_string_cst (node);
13153 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13154 {
c877974e 13155 int saved = ctxp->explicit_constructor_p;
b67d701b 13156 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13157 tree ret;
13158 /* Temporary disable forbid the use of `this'. */
13159 ctxp->explicit_constructor_p = 0;
13160 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13161 /* String concatenation arguments must be evaluated in order too. */
13162 ret = force_evaluation_order (ret);
c877974e
APB
13163 /* Restore it at its previous value */
13164 ctxp->explicit_constructor_p = saved;
13165 return ret;
b67d701b
PB
13166 }
13167 return NULL_TREE;
13168}
13169
13170/* Build the internal representation of a string constant. */
13171
13172static tree
13173patch_string_cst (node)
13174 tree node;
13175{
13176 int location;
15fdcfe9
PB
13177 if (! flag_emit_class_files)
13178 {
13179 push_obstacks (&permanent_obstack, &permanent_obstack);
13180 node = get_identifier (TREE_STRING_POINTER (node));
13181 location = alloc_name_constant (CONSTANT_String, node);
13182 node = build_ref_from_constant_pool (location);
8226320b 13183 pop_obstacks ();
15fdcfe9 13184 }
cd9643f7 13185 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13186 TREE_CONSTANT (node) = 1;
13187 return node;
13188}
13189
13190/* Build an incomplete unary operator expression. */
e04a16fb
AG
13191
13192static tree
13193build_unaryop (op_token, op_location, op1)
13194 int op_token, op_location;
13195 tree op1;
13196{
13197 enum tree_code op;
13198 tree unaryop;
13199 switch (op_token)
13200 {
b67d701b 13201 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13202 case MINUS_TK: op = NEGATE_EXPR; break;
13203 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13204 case NOT_TK: op = BIT_NOT_EXPR; break;
13205 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13206 op_token);
13207 }
13208
13209 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13210 TREE_SIDE_EFFECTS (unaryop) = 1;
13211 /* Store the location of the operator, for better error report. The
13212 string of the operator will be rebuild based on the OP value. */
13213 EXPR_WFL_LINECOL (unaryop) = op_location;
13214 return unaryop;
13215}
13216
13217/* Special case for the ++/-- operators, since they require an extra
13218 argument to build, which is set to NULL and patched
13219 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13220
13221static tree
13222build_incdec (op_token, op_location, op1, is_post_p)
13223 int op_token, op_location;
13224 tree op1;
13225 int is_post_p;
13226{
13227 static enum tree_code lookup [2][2] =
13228 {
13229 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13230 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13231 };
13232 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13233 NULL_TREE, op1, NULL_TREE);
13234 TREE_SIDE_EFFECTS (node) = 1;
13235 /* Store the location of the operator, for better error report. The
13236 string of the operator will be rebuild based on the OP value. */
13237 EXPR_WFL_LINECOL (node) = op_location;
13238 return node;
13239}
13240
13241/* Build an incomplete cast operator, based on the use of the
13242 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13243 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13244 though its type is already set. */
13245
13246static tree
13247build_cast (location, type, exp)
13248 int location;
13249 tree type, exp;
13250{
13251 tree node = build1 (CONVERT_EXPR, type, exp);
13252 EXPR_WFL_LINECOL (node) = location;
13253 return node;
13254}
13255
c2952b01
APB
13256/* Build an incomplete class reference operator. */
13257static tree
13258build_incomplete_class_ref (location, class_name)
13259 int location;
13260 tree class_name;
13261{
13262 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13263 EXPR_WFL_LINECOL (node) = location;
13264 return node;
13265}
13266
13267/* Complete an incomplete class reference operator. */
13268static tree
13269patch_incomplete_class_ref (node)
13270 tree node;
13271{
13272 tree type = TREE_OPERAND (node, 0);
13273 tree ref_type;
13274
13275 if (!(ref_type = resolve_type_during_patch (type)))
13276 return error_mark_node;
13277
165f37bc 13278 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13279 {
13280 /* A class referenced by `foo.class' is initialized. */
13281 return build_class_init (ref_type, build_class_ref (ref_type));
13282 }
165f37bc
APB
13283
13284 /* If we're emitting class files and we have to deal with non
13285 primitive types, we invoke (and consider generating) the
13286 synthetic static method `class$'. */
13287 if (!TYPE_DOT_CLASS (current_class))
13288 build_dot_class_method (current_class);
f0f3a777 13289 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13290 return java_complete_tree (ref_type);
c2952b01
APB
13291}
13292
e04a16fb
AG
13293/* 15.14 Unary operators. We return error_mark_node in case of error,
13294 but preserve the type of NODE if the type is fixed. */
13295
13296static tree
13297patch_unaryop (node, wfl_op)
13298 tree node;
13299 tree wfl_op;
13300{
13301 tree op = TREE_OPERAND (node, 0);
13302 tree op_type = TREE_TYPE (op);
ab3a6dd6 13303 tree prom_type = NULL_TREE, value, decl;
c2952b01 13304 int outer_field_flag = 0;
e04a16fb
AG
13305 int code = TREE_CODE (node);
13306 int error_found = 0;
13307
13308 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13309
13310 switch (code)
13311 {
13312 /* 15.13.2 Postfix Increment Operator ++ */
13313 case POSTINCREMENT_EXPR:
13314 /* 15.13.3 Postfix Increment Operator -- */
13315 case POSTDECREMENT_EXPR:
13316 /* 15.14.1 Prefix Increment Operator ++ */
13317 case PREINCREMENT_EXPR:
13318 /* 15.14.2 Prefix Decrement Operator -- */
13319 case PREDECREMENT_EXPR:
5cbdba64 13320 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13321 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13322 /* We might be trying to change an outer field accessed using
13323 access method. */
13324 if (outer_field_flag)
13325 {
13326 /* Retrieve the decl of the field we're trying to access. We
13327 do that by first retrieving the function we would call to
13328 access the field. It has been already verified that this
13329 field isn't final */
13330 if (flag_emit_class_files)
13331 decl = TREE_OPERAND (op, 0);
13332 else
13333 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13334 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13335 }
b3edebcf 13336 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13337 else if (!JDECL_P (decl)
b3edebcf
APB
13338 && TREE_CODE (decl) != COMPONENT_REF
13339 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13340 && TREE_CODE (decl) != INDIRECT_REF
13341 && !(TREE_CODE (decl) == COMPOUND_EXPR
13342 && TREE_OPERAND (decl, 1)
13343 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13344 {
5e942c50
APB
13345 tree lvalue;
13346 /* Before screaming, check that we're not in fact trying to
13347 increment a optimized static final access, in which case
13348 we issue an different error message. */
13349 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13350 && resolve_expression_name (wfl_op, &lvalue)
13351 && check_final_assignment (lvalue, wfl_op)))
13352 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13353 operator_string (node));
e04a16fb
AG
13354 TREE_TYPE (node) = error_mark_node;
13355 error_found = 1;
13356 }
c2952b01
APB
13357
13358 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13359 error_found = 1;
13360
e04a16fb
AG
13361 /* From now on, we know that op if a variable and that it has a
13362 valid wfl. We use wfl_op to locate errors related to the
13363 ++/-- operand. */
13364 else if (!JNUMERIC_TYPE_P (op_type))
13365 {
13366 parse_error_context
13367 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13368 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13369 TREE_TYPE (node) = error_mark_node;
13370 error_found = 1;
13371 }
13372 else
13373 {
4a5f66c3 13374 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13375 both operands, if really necessary */
13376 if (JINTEGRAL_TYPE_P (op_type))
13377 {
13378 value = build_int_2 (1, 0);
13379 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13380 }
13381 else
13382 {
13383 value = build_int_2 (1, 0);
13384 TREE_TYPE (node) =
13385 binary_numeric_promotion (op_type,
13386 TREE_TYPE (value), &op, &value);
13387 }
c2952b01
APB
13388
13389 /* We remember we might be accessing an outer field */
13390 if (outer_field_flag)
13391 {
13392 /* We re-generate an access to the field */
13393 value = build (PLUS_EXPR, TREE_TYPE (op),
13394 build_outer_field_access (wfl_op, decl), value);
13395
13396 /* And we patch the original access$() into a write
13397 with plus_op as a rhs */
13398 return outer_field_access_fix (node, op, value);
13399 }
13400
5cbdba64 13401 /* And write back into the node. */
4a5f66c3 13402 TREE_OPERAND (node, 0) = op;
e04a16fb 13403 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13404 /* Convert the overall back into its original type, if
13405 necessary, and return */
13406 if (JINTEGRAL_TYPE_P (op_type))
13407 return fold (node);
13408 else
13409 return fold (convert (op_type, node));
e04a16fb
AG
13410 }
13411 break;
13412
13413 /* 15.14.3 Unary Plus Operator + */
b67d701b 13414 case UNARY_PLUS_EXPR:
e04a16fb
AG
13415 /* 15.14.4 Unary Minus Operator - */
13416 case NEGATE_EXPR:
13417 if (!JNUMERIC_TYPE_P (op_type))
13418 {
13419 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13420 TREE_TYPE (node) = error_mark_node;
13421 error_found = 1;
13422 }
13423 /* Unary numeric promotion is performed on operand */
13424 else
13425 {
15fdcfe9
PB
13426 op = do_unary_numeric_promotion (op);
13427 prom_type = TREE_TYPE (op);
b67d701b 13428 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13429 return fold (op);
e04a16fb
AG
13430 }
13431 break;
13432
13433 /* 15.14.5 Bitwise Complement Operator ~ */
13434 case BIT_NOT_EXPR:
13435 if (!JINTEGRAL_TYPE_P (op_type))
13436 {
13437 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13438 TREE_TYPE (node) = error_mark_node;
13439 error_found = 1;
13440 }
13441 else
13442 {
15fdcfe9
PB
13443 op = do_unary_numeric_promotion (op);
13444 prom_type = TREE_TYPE (op);
e04a16fb
AG
13445 }
13446 break;
13447
13448 /* 15.14.6 Logical Complement Operator ! */
13449 case TRUTH_NOT_EXPR:
13450 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13451 {
13452 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13453 /* But the type is known. We will report an error if further
13454 attempt of a assignment is made with this rhs */
e04a16fb
AG
13455 TREE_TYPE (node) = boolean_type_node;
13456 error_found = 1;
13457 }
13458 else
13459 prom_type = boolean_type_node;
13460 break;
13461
13462 /* 15.15 Cast Expression */
13463 case CONVERT_EXPR:
0a2138e2 13464 value = patch_cast (node, wfl_operator);
e04a16fb 13465 if (value == error_mark_node)
c877974e
APB
13466 {
13467 /* If this cast is part of an assignment, we tell the code
13468 that deals with it not to complain about a mismatch,
13469 because things have been cast, anyways */
13470 TREE_TYPE (node) = error_mark_node;
13471 error_found = 1;
13472 }
13473 else
dc0b3eff
PB
13474 {
13475 value = fold (value);
13476 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13477 return value;
13478 }
e04a16fb
AG
13479 break;
13480 }
13481
e04a16fb
AG
13482 if (error_found)
13483 return error_mark_node;
4a5f66c3
APB
13484
13485 /* There are cases where node has been replaced by something else
13486 and we don't end up returning here: UNARY_PLUS_EXPR,
13487 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13488 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13489 TREE_TYPE (node) = prom_type;
dc0b3eff 13490 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13491 return fold (node);
13492}
13493
13494/* Generic type resolution that sometimes takes place during node
13495 patching. Returned the resolved type or generate an error
13496 message. Return the resolved type or NULL_TREE. */
13497
13498static tree
13499resolve_type_during_patch (type)
13500 tree type;
13501{
13502 if (unresolved_type_p (type, NULL))
13503 {
4142b247 13504 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13505 if (!type_decl)
13506 {
13507 parse_error_context (type,
13508 "Class `%s' not found in type declaration",
13509 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13510 return NULL_TREE;
13511 }
13512 else
5e942c50
APB
13513 {
13514 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13515 return TREE_TYPE (type_decl);
13516 }
e04a16fb
AG
13517 }
13518 return type;
13519}
13520/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13521 found. Otherwise NODE or something meant to replace it is returned. */
13522
13523static tree
19e223db 13524patch_cast (node, wfl_op)
e04a16fb 13525 tree node;
19e223db 13526 tree wfl_op;
e04a16fb
AG
13527{
13528 tree op = TREE_OPERAND (node, 0);
13529 tree op_type = TREE_TYPE (op);
13530 tree cast_type = TREE_TYPE (node);
13531 char *t1;
13532
13533 /* First resolve OP_TYPE if unresolved */
13534 if (!(cast_type = resolve_type_during_patch (cast_type)))
13535 return error_mark_node;
13536
13537 /* Check on cast that are proven correct at compile time */
13538 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13539 {
e04a16fb
AG
13540 /* Same type */
13541 if (cast_type == op_type)
13542 return node;
13543
0b4d333e
APB
13544 /* float and double type are converted to the original type main
13545 variant and then to the target type. */
13546 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13547 op = convert (integer_type_node, op);
13548
e04a16fb
AG
13549 /* Try widening/narowwing convertion. Potentially, things need
13550 to be worked out in gcc so we implement the extreme cases
13551 correctly. fold_convert() needs to be fixed. */
13552 return convert (cast_type, op);
13553 }
13554
0b4d333e
APB
13555 /* It's also valid to cast a boolean into a boolean */
13556 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13557 return node;
13558
5e942c50
APB
13559 /* null can be casted to references */
13560 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13561 return build_null_of_type (cast_type);
13562
e04a16fb
AG
13563 /* The remaining legal casts involve conversion between reference
13564 types. Check for their compile time correctness. */
13565 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13566 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13567 {
13568 TREE_TYPE (node) = promote_type (cast_type);
13569 /* Now, the case can be determined correct at compile time if
13570 OP_TYPE can be converted into CAST_TYPE by assignment
13571 conversion (5.2) */
13572
13573 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13574 {
13575 TREE_SET_CODE (node, NOP_EXPR);
13576 return node;
13577 }
13578
13579 if (flag_emit_class_files)
13580 {
13581 TREE_SET_CODE (node, CONVERT_EXPR);
13582 return node;
13583 }
e04a16fb
AG
13584
13585 /* The cast requires a run-time check */
13586 return build (CALL_EXPR, promote_type (cast_type),
13587 build_address_of (soft_checkcast_node),
13588 tree_cons (NULL_TREE, build_class_ref (cast_type),
13589 build_tree_list (NULL_TREE, op)),
13590 NULL_TREE);
13591 }
13592
13593 /* Any other casts are proven incorrect at compile time */
c2e3db92 13594 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13595 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13596 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13597 free (t1);
13598 return error_mark_node;
13599}
13600
5e942c50
APB
13601/* Build a null constant and give it the type TYPE. */
13602
13603static tree
13604build_null_of_type (type)
13605 tree type;
13606{
13607 tree node = build_int_2 (0, 0);
13608 TREE_TYPE (node) = promote_type (type);
13609 return node;
13610}
13611
e04a16fb
AG
13612/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13613 a list of indices. */
13614static tree
13615build_array_ref (location, array, index)
13616 int location;
13617 tree array, index;
13618{
13619 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13620 EXPR_WFL_LINECOL (node) = location;
13621 return node;
13622}
13623
13624/* 15.12 Array Access Expression */
13625
13626static tree
c877974e
APB
13627patch_array_ref (node)
13628 tree node;
e04a16fb
AG
13629{
13630 tree array = TREE_OPERAND (node, 0);
13631 tree array_type = TREE_TYPE (array);
13632 tree index = TREE_OPERAND (node, 1);
13633 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13634 int error_found = 0;
13635
13636 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13637
e04a16fb
AG
13638 if (TREE_CODE (array_type) == POINTER_TYPE)
13639 array_type = TREE_TYPE (array_type);
13640
13641 /* The array reference must be an array */
13642 if (!TYPE_ARRAY_P (array_type))
13643 {
13644 parse_error_context
781b0558
KG
13645 (wfl_operator,
13646 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13647 lang_printable_name (array_type, 0));
e04a16fb
AG
13648 TREE_TYPE (node) = error_mark_node;
13649 error_found = 1;
13650 }
13651
c2952b01 13652 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13653 type must be int */
15fdcfe9
PB
13654 index = do_unary_numeric_promotion (index);
13655 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13656 {
1ebadc60 13657 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13658 parse_error_context (wfl_operator,
13659 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13660 lang_printable_name (index_type, 0));
13661 else
781b0558
KG
13662 parse_error_context (wfl_operator,
13663 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13664 lang_printable_name (index_type, 0));
e04a16fb
AG
13665 TREE_TYPE (node) = error_mark_node;
13666 error_found = 1;
13667 }
13668
e04a16fb
AG
13669 if (error_found)
13670 return error_mark_node;
e04a16fb 13671
5e942c50 13672 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13673
7f1d4866 13674 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13675 {
15fdcfe9
PB
13676 TREE_OPERAND (node, 0) = array;
13677 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13678 }
13679 else
939d7216
PB
13680 {
13681 /* The save_expr is for correct evaluation order. It would be cleaner
13682 to use force_evaluation_order (see comment there), but that is
13683 difficult when we also have to deal with bounds checking. */
13684 if (TREE_SIDE_EFFECTS (index))
13685 array = save_expr (array);
13686 node = build_java_arrayaccess (array, array_type, index);
13687 if (TREE_SIDE_EFFECTS (index))
13688 node = build (COMPOUND_EXPR, array_type, array, node);
13689 }
e04a16fb
AG
13690 TREE_TYPE (node) = array_type;
13691 return node;
13692}
13693
13694/* 15.9 Array Creation Expressions */
13695
13696static tree
13697build_newarray_node (type, dims, extra_dims)
13698 tree type;
13699 tree dims;
13700 int extra_dims;
13701{
13702 tree node =
b67d701b 13703 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13704 build_int_2 (extra_dims, 0));
e04a16fb
AG
13705 return node;
13706}
13707
13708static tree
13709patch_newarray (node)
13710 tree node;
13711{
13712 tree type = TREE_OPERAND (node, 0);
13713 tree dims = TREE_OPERAND (node, 1);
13714 tree cdim, array_type;
13715 int error_found = 0;
13716 int ndims = 0;
13717 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13718
13719 /* Dimension types are verified. It's better for the types to be
13720 verified in order. */
13721 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13722 {
13723 int dim_error = 0;
13724 tree dim = TREE_VALUE (cdim);
13725
13726 /* Dim might have been saved during its evaluation */
13727 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13728
13729 /* The type of each specified dimension must be an integral type. */
13730 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13731 dim_error = 1;
13732
13733 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13734 promoted type must be int. */
13735 else
13736 {
15fdcfe9 13737 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13738 if (TREE_TYPE (dim) != int_type_node)
13739 dim_error = 1;
13740 }
13741
13742 /* Report errors on types here */
13743 if (dim_error)
13744 {
13745 parse_error_context
13746 (TREE_PURPOSE (cdim),
781b0558 13747 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13748 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13749 "Explicit cast needed to" : "Can't"),
0a2138e2 13750 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13751 error_found = 1;
13752 }
13753
e04a16fb
AG
13754 TREE_PURPOSE (cdim) = NULL_TREE;
13755 }
13756
13757 /* Resolve array base type if unresolved */
13758 if (!(type = resolve_type_during_patch (type)))
13759 error_found = 1;
13760
13761 if (error_found)
13762 {
13763 /* We don't want further evaluation of this bogus array creation
13764 operation */
13765 TREE_TYPE (node) = error_mark_node;
13766 return error_mark_node;
13767 }
13768
15fdcfe9
PB
13769 /* Set array_type to the actual (promoted) array type of the result. */
13770 if (TREE_CODE (type) == RECORD_TYPE)
13771 type = build_pointer_type (type);
13772 while (--xdims >= 0)
13773 {
13774 type = promote_type (build_java_array_type (type, -1));
13775 }
13776 dims = nreverse (dims);
13777 array_type = type;
13778 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13779 {
13780 type = array_type;
05bccae2
RK
13781 array_type
13782 = build_java_array_type (type,
13783 TREE_CODE (cdim) == INTEGER_CST
13784 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13785 : -1);
15fdcfe9
PB
13786 array_type = promote_type (array_type);
13787 }
13788 dims = nreverse (dims);
13789
e04a16fb
AG
13790 /* The node is transformed into a function call. Things are done
13791 differently according to the number of dimensions. If the number
13792 of dimension is equal to 1, then the nature of the base type
13793 (primitive or not) matters. */
15fdcfe9 13794 if (ndims == 1)
fdec99c6 13795 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13796
e04a16fb
AG
13797 /* Can't reuse what's already written in expr.c because it uses the
13798 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13799 return build (CALL_EXPR, array_type,
e04a16fb 13800 build_address_of (soft_multianewarray_node),
15fdcfe9 13801 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13802 tree_cons (NULL_TREE,
15fdcfe9 13803 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13804 NULL_TREE);
13805}
13806
f8976021
APB
13807/* 10.6 Array initializer. */
13808
13809/* Build a wfl for array element that don't have one, so we can
13810 pin-point errors. */
13811
13812static tree
13813maybe_build_array_element_wfl (node)
13814 tree node;
13815{
13816 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13817 return build_expr_wfl (NULL_TREE, ctxp->filename,
13818 ctxp->elc.line, ctxp->elc.prev_col);
13819 else
13820 return NULL_TREE;
13821}
13822
13823/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13824 identification of initialized arrays easier to detect during walk
13825 and expansion. */
13826
13827static tree
13828build_new_array_init (location, values)
13829 int location;
13830 tree values;
13831{
13832 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13833 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13834 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13835 return to_return;
13836}
13837
13838/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13839 occurred. Otherwise return NODE after having set its type
13840 appropriately. */
13841
13842static tree
13843patch_new_array_init (type, node)
13844 tree type, node;
f8976021
APB
13845{
13846 int error_seen = 0;
fdec99c6 13847 tree current, element_type;
f8976021 13848 HOST_WIDE_INT length;
fdec99c6
PB
13849 int all_constant = 1;
13850 tree init = TREE_OPERAND (node, 0);
f8976021 13851
fdec99c6
PB
13852 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13853 {
13854 parse_error_context (node,
13855 "Invalid array initializer for non-array type `%s'",
13856 lang_printable_name (type, 1));
13857 return error_mark_node;
13858 }
13859 type = TREE_TYPE (type);
13860 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13861
fdec99c6
PB
13862 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13863
13864 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13865 current; length++, current = TREE_CHAIN (current))
f8976021 13866 {
fdec99c6
PB
13867 tree elt = TREE_VALUE (current);
13868 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13869 {
fdec99c6 13870 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13871 elt = TREE_VALUE (current);
13872 /* When compiling to native code, STRING_CST is converted to
13873 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13874 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13875 all_constant = 0;
f8976021 13876 }
fdec99c6
PB
13877 else
13878 {
13879 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13880 TREE_PURPOSE (current) = NULL_TREE;
13881 all_constant = 0;
13882 }
9a7ab4b3
APB
13883 if (elt && TREE_CODE (elt) == TREE_LIST
13884 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13885 error_seen = 1;
f8976021
APB
13886 }
13887
13888 if (error_seen)
13889 return error_mark_node;
13890
13891 /* Create a new type. We can't reuse the one we have here by
13892 patching its dimension because it originally is of dimension -1
13893 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13894 type = build_java_array_type (element_type, length);
13895 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13896 TREE_TYPE (node) = promote_type (type);
13897 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13898 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13899 return node;
13900}
13901
13902/* Verify that one entry of the initializer element list can be
13903 assigned to the array base type. Report 1 if an error occurred, 0
13904 otherwise. */
13905
13906static int
13907array_constructor_check_entry (type, entry)
13908 tree type, entry;
13909{
13910 char *array_type_string = NULL; /* For error reports */
13911 tree value, type_value, new_value, wfl_value, patched;
13912 int error_seen = 0;
13913
13914 new_value = NULL_TREE;
13915 wfl_value = TREE_VALUE (entry);
13916
100f7cd8 13917 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13918 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13919 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13920 if ((patched = patch_string (value)))
13921 value = patched;
1179ebc2
APB
13922 if (value == error_mark_node)
13923 return 1;
f8976021 13924
f8976021
APB
13925 type_value = TREE_TYPE (value);
13926
1179ebc2 13927 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13928 constant overflow during narrowing. */
13929 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13930 new_value = try_builtin_assignconv (wfl_operator, type, value);
13931 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13932 type_value = promote_type (type);
100f7cd8
APB
13933
13934 pop_obstacks ();
f8976021
APB
13935 /* Check and report errors */
13936 if (!new_value)
13937 {
49f48c71 13938 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13939 "Can't" : "Explicit cast needed to");
13940 if (!array_type_string)
c2e3db92 13941 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13942 parse_error_context
13943 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13944 msg, lang_printable_name (type_value, 1), array_type_string);
13945 error_seen = 1;
13946 }
13947
13948 if (new_value)
13949 {
b8c5b1c6 13950 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13951 TREE_VALUE (entry) = new_value;
13952 }
13953
13954 if (array_type_string)
13955 free (array_type_string);
13956
13957 TREE_PURPOSE (entry) = NULL_TREE;
13958 return error_seen;
13959}
13960
e04a16fb
AG
13961static tree
13962build_this (location)
13963 int location;
13964{
9ee9b555 13965 tree node = build_wfl_node (this_identifier_node);
b67d701b 13966 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13967 EXPR_WFL_LINECOL (node) = location;
13968 return node;
13969}
13970
13971/* 14.15 The return statement. It builds a modify expression that
13972 assigns the returned value to the RESULT_DECL that hold the value
13973 to be returned. */
13974
13975static tree
13976build_return (location, op)
13977 int location;
13978 tree op;
13979{
13980 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13981 EXPR_WFL_LINECOL (node) = location;
b67d701b 13982 node = build_debugable_stmt (location, node);
e04a16fb
AG
13983 return node;
13984}
13985
13986static tree
13987patch_return (node)
13988 tree node;
13989{
13990 tree return_exp = TREE_OPERAND (node, 0);
13991 tree meth = current_function_decl;
13992 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13993 int error_found = 0;
13994
13995 TREE_TYPE (node) = error_mark_node;
13996 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13997
13998 /* It's invalid to have a return value within a function that is
13999 declared with the keyword void or that is a constructor */
14000 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14001 error_found = 1;
14002
f099f336 14003 /* It's invalid to use a return statement in a static block */
c2952b01 14004 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14005 error_found = 1;
14006
e04a16fb
AG
14007 /* It's invalid to have a no return value within a function that
14008 isn't declared with the keyword `void' */
14009 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14010 error_found = 2;
c2952b01
APB
14011
14012 if (in_instance_initializer)
14013 error_found = 1;
e04a16fb
AG
14014
14015 if (error_found)
14016 {
c2952b01 14017 if (in_instance_initializer)
f099f336 14018 parse_error_context (wfl_operator,
c2952b01
APB
14019 "`return' inside instance initializer");
14020
14021 else if (DECL_CLINIT_P (current_function_decl))
14022 parse_error_context (wfl_operator,
14023 "`return' inside static initializer");
f099f336
APB
14024
14025 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14026 {
c2e3db92 14027 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14028 parse_error_context (wfl_operator,
14029 "`return' with%s value from `%s %s'",
14030 (error_found == 1 ? "" : "out"),
14031 t, lang_printable_name (meth, 0));
14032 free (t);
14033 }
14034 else
14035 parse_error_context (wfl_operator,
14036 "`return' with value from constructor `%s'",
14037 lang_printable_name (meth, 0));
e04a16fb
AG
14038 return error_mark_node;
14039 }
14040
5e942c50
APB
14041 /* If we have a return_exp, build a modify expression and expand
14042 it. Note: at that point, the assignment is declared valid, but we
14043 may want to carry some more hacks */
e04a16fb
AG
14044 if (return_exp)
14045 {
5e942c50
APB
14046 tree exp = java_complete_tree (return_exp);
14047 tree modify, patched;
14048
14049 /* If the function returned value and EXP are booleans, EXP has
14050 to be converted into the type of DECL_RESULT, which is integer
14051 (see complete_start_java_method) */
14052 if (TREE_TYPE (exp) == boolean_type_node &&
14053 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14054 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14055
14056 /* `null' can be assigned to a function returning a reference */
14057 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14058 exp == null_pointer_node)
14059 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14060
14061 if ((patched = patch_string (exp)))
14062 exp = patched;
14063
14064 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14065 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14066 modify = java_complete_tree (modify);
5e942c50 14067
e04a16fb
AG
14068 if (modify != error_mark_node)
14069 {
14070 TREE_SIDE_EFFECTS (modify) = 1;
14071 TREE_OPERAND (node, 0) = modify;
14072 }
14073 else
14074 return error_mark_node;
14075 }
14076 TREE_TYPE (node) = void_type_node;
14077 TREE_SIDE_EFFECTS (node) = 1;
14078 return node;
14079}
14080
14081/* 14.8 The if Statement */
14082
14083static tree
14084build_if_else_statement (location, expression, if_body, else_body)
14085 int location;
14086 tree expression, if_body, else_body;
14087{
14088 tree node;
e04a16fb 14089 if (!else_body)
9bbc7d9f 14090 else_body = empty_stmt_node;
e04a16fb
AG
14091 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14092 EXPR_WFL_LINECOL (node) = location;
b67d701b 14093 node = build_debugable_stmt (location, node);
e04a16fb
AG
14094 return node;
14095}
14096
14097static tree
14098patch_if_else_statement (node)
14099 tree node;
14100{
14101 tree expression = TREE_OPERAND (node, 0);
14102
14103 TREE_TYPE (node) = error_mark_node;
14104 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14105
14106 /* The type of expression must be boolean */
b67d701b
PB
14107 if (TREE_TYPE (expression) != boolean_type_node
14108 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14109 {
14110 parse_error_context
14111 (wfl_operator,
14112 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14113 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14114 return error_mark_node;
14115 }
14116
14117 TREE_TYPE (node) = void_type_node;
14118 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14119 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14120 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14121 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14122 return node;
14123}
14124
14125/* 14.6 Labeled Statements */
14126
14127/* Action taken when a lableled statement is parsed. a new
14128 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14129 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14130
14131static tree
0a2138e2 14132build_labeled_block (location, label)
e04a16fb 14133 int location;
0a2138e2 14134 tree label;
e04a16fb 14135{
b635eb2f 14136 tree label_name ;
e04a16fb 14137 tree label_decl, node;
b635eb2f
PB
14138 if (label == NULL_TREE || label == continue_identifier_node)
14139 label_name = label;
14140 else
e04a16fb 14141 {
b635eb2f
PB
14142 label_name = merge_qualified_name (label_id, label);
14143 /* Issue an error if we try to reuse a label that was previously
14144 declared */
14145 if (IDENTIFIER_LOCAL_VALUE (label_name))
14146 {
14147 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14148 parse_error_context (wfl_operator,
14149 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14150 IDENTIFIER_POINTER (label));
14151 EXPR_WFL_LINECOL (wfl_operator) =
14152 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14153 parse_error_context (wfl_operator,
14154 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14155 IDENTIFIER_POINTER (label));
14156 java_error_count--;
14157 }
e04a16fb
AG
14158 }
14159
14160 label_decl = create_label_decl (label_name);
14161 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14162 EXPR_WFL_LINECOL (node) = location;
14163 TREE_SIDE_EFFECTS (node) = 1;
14164 return node;
14165}
14166
b67d701b 14167/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14168
14169static tree
b635eb2f 14170finish_labeled_statement (lbe, statement)
e04a16fb
AG
14171 tree lbe; /* Labeled block expr */
14172 tree statement;
14173{
14174 /* In anyways, tie the loop to its statement */
14175 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14176 pop_labeled_block ();
14177 POP_LABELED_BLOCK ();
e04a16fb
AG
14178 return lbe;
14179}
14180
14181/* 14.10, 14.11, 14.12 Loop Statements */
14182
14183/* Create an empty LOOP_EXPR and make it the last in the nested loop
14184 list. */
14185
14186static tree
14187build_new_loop (loop_body)
14188 tree loop_body;
14189{
14190 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14191 TREE_SIDE_EFFECTS (loop) = 1;
14192 PUSH_LOOP (loop);
14193 return loop;
14194}
14195
14196/* Create a loop body according to the following structure:
14197 COMPOUND_EXPR
14198 COMPOUND_EXPR (loop main body)
14199 EXIT_EXPR (this order is for while/for loops.
14200 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14201 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14202 BODY end of this labeled block)
14203 INCREMENT (if any)
14204
14205 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14206 after the body, like in the do-while loop.
14207
14208 To obtain a loop, the loop body structure described above is
14209 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14210
14211 LABELED_BLOCK_EXPR
14212 LABEL_DECL (use this label to exit the loop)
14213 LOOP_EXPR
14214 <structure described above> */
e04a16fb
AG
14215
14216static tree
14217build_loop_body (location, condition, reversed)
14218 int location;
14219 tree condition;
14220 int reversed;
14221{
0a2138e2 14222 tree first, second, body;
e04a16fb
AG
14223
14224 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14225 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14226 condition = build_debugable_stmt (location, condition);
14227 TREE_SIDE_EFFECTS (condition) = 1;
14228
b635eb2f 14229 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14230 first = (reversed ? body : condition);
14231 second = (reversed ? condition : body);
14232 return
14233 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14234 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14235}
14236
14237/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14238 their order) on the current loop. Unlink the current loop from the
14239 loop list. */
14240
14241static tree
b635eb2f 14242finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14243 int location;
14244 tree condition, body;
14245 int reversed;
14246{
14247 tree to_return = ctxp->current_loop;
14248 tree loop_body = LOOP_EXPR_BODY (to_return);
14249 if (condition)
14250 {
14251 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14252 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14253 The real EXIT_EXPR is one operand further. */
14254 EXPR_WFL_LINECOL (cnode) = location;
14255 /* This one is for accurate error reports */
14256 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14257 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14258 }
14259 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14260 POP_LOOP ();
14261 return to_return;
14262}
14263
b635eb2f 14264/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14265 loops feature the condition part */
14266
14267static tree
b635eb2f 14268finish_for_loop (location, condition, update, body)
e04a16fb
AG
14269 int location;
14270 tree condition, update, body;
14271{
14272 /* Put the condition and the loop body in place */
b635eb2f 14273 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14274 /* LOOP is the current loop which has been now popped of the loop
14275 stack. Install the update block */
14276 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14277 return loop;
14278}
14279
5cbdba64
APB
14280/* Try to find the loop a block might be related to. This comprises
14281 the case where the LOOP_EXPR is found as the second operand of a
14282 COMPOUND_EXPR, because the loop happens to have an initialization
14283 part, then expressed as the first operand of the COMPOUND_EXPR. If
14284 the search finds something, 1 is returned. Otherwise, 0 is
14285 returned. The search is assumed to start from a
14286 LABELED_BLOCK_EXPR's block. */
14287
14288static tree
14289search_loop (statement)
14290 tree statement;
14291{
14292 if (TREE_CODE (statement) == LOOP_EXPR)
14293 return statement;
14294
14295 if (TREE_CODE (statement) == BLOCK)
14296 statement = BLOCK_SUBBLOCKS (statement);
14297 else
14298 return NULL_TREE;
14299
14300 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14301 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14302 statement = TREE_OPERAND (statement, 1);
14303
14304 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14305 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14306}
14307
14308/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14309 returned otherwise. */
14310
14311static int
14312labeled_block_contains_loop_p (block, loop)
14313 tree block, loop;
14314{
14315 if (!block)
14316 return 0;
14317
14318 if (LABELED_BLOCK_BODY (block) == loop)
14319 return 1;
14320
c2952b01 14321 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14322 return 1;
14323
14324 return 0;
14325}
14326
e04a16fb 14327/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14328 insert LOOP as its body. */
e04a16fb
AG
14329
14330static tree
14331patch_loop_statement (loop)
14332 tree loop;
14333{
cd9643f7 14334 tree loop_label;
5cbdba64 14335
cd9643f7 14336 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14337 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14338 return loop;
14339
cd9643f7 14340 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14341 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14342 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14343 LABELED_BLOCK_BODY (loop_label) = loop;
14344 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14345 return loop_label;
e04a16fb
AG
14346}
14347
14348/* 14.13, 14.14: break and continue Statements */
14349
14350/* Build a break or a continue statement. a null NAME indicates an
14351 unlabeled break/continue statement. */
14352
14353static tree
14354build_bc_statement (location, is_break, name)
14355 int location, is_break;
14356 tree name;
14357{
14358 tree break_continue, label_block_expr = NULL_TREE;
14359
14360 if (name)
14361 {
14362 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14363 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14364 /* Null means that we don't have a target for this named
14365 break/continue. In this case, we make the target to be the
14366 label name, so that the error can be reported accuratly in
14367 patch_bc_statement. */
14368 label_block_expr = EXPR_WFL_NODE (name);
14369 }
14370 /* Unlabeled break/continue will be handled during the
14371 break/continue patch operation */
14372 break_continue
14373 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14374
14375 IS_BREAK_STMT_P (break_continue) = is_break;
14376 TREE_SIDE_EFFECTS (break_continue) = 1;
14377 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14378 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14379 return break_continue;
14380}
14381
14382/* Verification of a break/continue statement. */
14383
14384static tree
14385patch_bc_statement (node)
14386 tree node;
14387{
14388 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14389 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14390 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14391
e04a16fb 14392 /* Having an identifier here means that the target is unknown. */
b635eb2f 14393 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14394 {
14395 parse_error_context (wfl_operator, "No label definition found for `%s'",
14396 IDENTIFIER_POINTER (bc_label));
14397 return error_mark_node;
14398 }
b635eb2f 14399 if (! IS_BREAK_STMT_P (node))
e04a16fb 14400 {
b635eb2f
PB
14401 /* It's a continue statement. */
14402 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14403 {
b635eb2f
PB
14404 if (labeled_block == NULL_TREE)
14405 {
14406 if (bc_label == NULL_TREE)
14407 parse_error_context (wfl_operator,
14408 "`continue' must be in loop");
14409 else
1504b2b4
APB
14410 parse_error_context
14411 (wfl_operator, "continue label `%s' does not name a loop",
14412 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14413 return error_mark_node;
14414 }
14415 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14416 == continue_identifier_node)
14417 && (bc_label == NULL_TREE
14418 || TREE_CHAIN (labeled_block) == bc_label))
14419 {
14420 bc_label = labeled_block;
14421 break;
14422 }
e04a16fb 14423 }
e04a16fb 14424 }
b635eb2f 14425 else if (!bc_label)
34f4db93 14426 {
b635eb2f 14427 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14428 {
b635eb2f
PB
14429 if (labeled_block == NULL_TREE)
14430 {
14431 parse_error_context (wfl_operator,
14432 "`break' must be in loop or switch");
14433 return error_mark_node;
14434 }
14435 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14436 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14437 || search_loop (target_stmt))
b635eb2f
PB
14438 {
14439 bc_label = labeled_block;
14440 break;
14441 }
e04a16fb 14442 }
e04a16fb
AG
14443 }
14444
b635eb2f 14445 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14446 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14447
e04a16fb
AG
14448 /* Our break/continue don't return values. */
14449 TREE_TYPE (node) = void_type_node;
14450 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14451 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14452 sometimes, like after a if statement) */
14453 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14454 TREE_SIDE_EFFECTS (node) = 1;
14455 return node;
14456}
14457
14458/* Process the exit expression belonging to a loop. Its type must be
14459 boolean. */
14460
14461static tree
14462patch_exit_expr (node)
14463 tree node;
14464{
14465 tree expression = TREE_OPERAND (node, 0);
14466 TREE_TYPE (node) = error_mark_node;
14467 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14468
14469 /* The type of expression must be boolean */
14470 if (TREE_TYPE (expression) != boolean_type_node)
14471 {
14472 parse_error_context
14473 (wfl_operator,
781b0558 14474 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14475 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14476 return error_mark_node;
14477 }
14478 /* Now we know things are allright, invert the condition, fold and
14479 return */
14480 TREE_OPERAND (node, 0) =
14481 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14482
14483 if (! integer_zerop (TREE_OPERAND (node, 0))
14484 && ctxp->current_loop != NULL_TREE
14485 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14486 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14487 if (! integer_onep (TREE_OPERAND (node, 0)))
14488 CAN_COMPLETE_NORMALLY (node) = 1;
14489
14490
e04a16fb
AG
14491 TREE_TYPE (node) = void_type_node;
14492 return node;
14493}
b67d701b
PB
14494
14495/* 14.9 Switch statement */
14496
14497static tree
14498patch_switch_statement (node)
14499 tree node;
14500{
c877974e 14501 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14502
14503 /* Complete the switch expression */
14504 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14505 se_type = TREE_TYPE (se);
14506 /* The type of the switch expression must be char, byte, short or
14507 int */
2e0f0aff 14508 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14509 {
14510 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14511 parse_error_context (wfl_operator,
14512 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14513 lang_printable_name (se_type, 0));
b67d701b
PB
14514 /* This is what java_complete_tree will check */
14515 TREE_OPERAND (node, 0) = error_mark_node;
14516 return error_mark_node;
14517 }
14518
15fdcfe9 14519 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14520
14521 /* Ready to return */
15fdcfe9 14522 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14523 {
14524 TREE_TYPE (node) = error_mark_node;
14525 return error_mark_node;
14526 }
14527 TREE_TYPE (node) = void_type_node;
14528 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14529 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14530 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14531 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14532 return node;
14533}
14534
165f37bc 14535/* 14.18 The try/catch statements */
b67d701b 14536
b67d701b 14537static tree
a7d8d81f 14538build_try_statement (location, try_block, catches)
b67d701b 14539 int location;
a7d8d81f
PB
14540 tree try_block, catches;
14541{
14542 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14543 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14544 return node;
b67d701b
PB
14545}
14546
a7d8d81f
PB
14547static tree
14548build_try_finally_statement (location, try_block, finally)
14549 int location;
14550 tree try_block, finally;
b67d701b 14551{
a7d8d81f
PB
14552 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14553 EXPR_WFL_LINECOL (node) = location;
14554 return node;
b67d701b
PB
14555}
14556
14557static tree
14558patch_try_statement (node)
14559 tree node;
14560{
14561 int error_found = 0;
14562 tree try = TREE_OPERAND (node, 0);
14563 /* Exception handlers are considered in left to right order */
14564 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14565 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14566
14567 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14568 to process the next catch clause. We process the catch clause before
14569 the try block so that when processing the try block we can check thrown
14570 exceptions againts the caught type list. */
b67d701b
PB
14571 for (current = catch; current; current = TREE_CHAIN (current))
14572 {
14573 tree carg_decl, carg_type;
14574 tree sub_current, catch_block, catch_clause;
14575 int unreachable;
14576
b67d701b 14577 /* At this point, the structure of the catch clause is
b67d701b
PB
14578 CATCH_EXPR (catch node)
14579 BLOCK (with the decl of the parameter)
14580 COMPOUND_EXPR
7525cc04 14581 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14582 BLOCK (catch clause block)
a7d8d81f
PB
14583 */
14584 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14585 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14586 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14587
14588 /* Catch clauses can't have more than one parameter declared,
14589 but it's already enforced by the grammar. Make sure that the
14590 only parameter of the clause statement in of class Throwable
14591 or a subclass of Throwable, but that was done earlier. The
14592 catch clause parameter type has also been resolved. */
14593
14594 /* Just make sure that the catch clause parameter type inherits
14595 from java.lang.Throwable */
14596 if (!inherits_from_p (carg_type, throwable_type_node))
14597 {
14598 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14599 parse_error_context (wfl_operator,
781b0558 14600 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14601 lang_printable_name (carg_type, 0));
b67d701b
PB
14602 error_found = 1;
14603 continue;
14604 }
14605
14606 /* Partial check for unreachable catch statement: The catch
14607 clause is reachable iff is no earlier catch block A in
14608 the try statement such that the type of the catch
14609 clause's parameter is the same as or a subclass of the
14610 type of A's parameter */
14611 unreachable = 0;
14612 for (sub_current = catch;
14613 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14614 {
14615 tree sub_catch_clause, decl;
a7d8d81f 14616 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14617 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14618
14619 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14620 {
14621 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14622 parse_error_context
781b0558
KG
14623 (wfl_operator,
14624 "`catch' not reached because of the catch clause at line %d",
14625 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14626 unreachable = error_found = 1;
14627 break;
14628 }
14629 }
b67d701b
PB
14630 /* Complete the catch clause block */
14631 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14632 if (catch_block == error_mark_node)
14633 {
14634 error_found = 1;
14635 continue;
14636 }
15fdcfe9
PB
14637 if (CAN_COMPLETE_NORMALLY (catch_block))
14638 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14639 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14640
14641 if (unreachable)
14642 continue;
14643
14644 /* Things to do here: the exception must be thrown */
14645
14646 /* Link this type to the caught type list */
14647 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14648 }
14649
b9f7e36c
APB
14650 PUSH_EXCEPTIONS (caught_type_list);
14651 if ((try = java_complete_tree (try)) == error_mark_node)
14652 error_found = 1;
15fdcfe9
PB
14653 if (CAN_COMPLETE_NORMALLY (try))
14654 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14655 POP_EXCEPTIONS ();
14656
b67d701b
PB
14657 /* Verification ends here */
14658 if (error_found)
14659 return error_mark_node;
14660
14661 TREE_OPERAND (node, 0) = try;
14662 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14663 TREE_TYPE (node) = void_type_node;
14664 return node;
14665}
b9f7e36c
APB
14666
14667/* 14.17 The synchronized Statement */
14668
14669static tree
14670patch_synchronized_statement (node, wfl_op1)
14671 tree node, wfl_op1;
14672{
5a005d9e 14673 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14674 tree block = TREE_OPERAND (node, 1);
5a005d9e 14675
d8fccff5 14676 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14677
14678 if (expr == error_mark_node)
14679 {
14680 block = java_complete_tree (block);
14681 return expr;
14682 }
b9f7e36c
APB
14683
14684 /* The TYPE of expr must be a reference type */
5a005d9e 14685 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14686 {
14687 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14688 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14689 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14690 return error_mark_node;
14691 }
14692
ce6e9147
APB
14693 if (flag_emit_xref)
14694 {
14695 TREE_OPERAND (node, 0) = expr;
14696 TREE_OPERAND (node, 1) = java_complete_tree (block);
14697 CAN_COMPLETE_NORMALLY (node) = 1;
14698 return node;
14699 }
14700
b9f7e36c
APB
14701 /* Generate a try-finally for the synchronized statement, except
14702 that the handler that catches all throw exception calls
14703 _Jv_MonitorExit and then rethrow the exception.
14704 The synchronized statement is then implemented as:
14705 TRY
14706 {
14707 _Jv_MonitorEnter (expression)
14708 synchronized_block
14709 _Jv_MonitorExit (expression)
14710 }
14711 CATCH_ALL
14712 {
14713 e = _Jv_exception_info ();
14714 _Jv_MonitorExit (expression)
14715 Throw (e);
14716 } */
14717
5a005d9e
PB
14718 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14719 BUILD_MONITOR_ENTER (enter, expr_decl);
14720 BUILD_MONITOR_EXIT (exit, expr_decl);
14721 CAN_COMPLETE_NORMALLY (enter) = 1;
14722 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14723 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14724 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14725 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14726 build (COMPOUND_EXPR, NULL_TREE,
14727 build (WITH_CLEANUP_EXPR, NULL_TREE,
14728 build (COMPOUND_EXPR, NULL_TREE,
96847892 14729 assignment, enter),
5a005d9e
PB
14730 NULL_TREE, exit),
14731 block));
14732 node = build_expr_block (node, expr_decl);
14733
14734 return java_complete_tree (node);
b9f7e36c
APB
14735}
14736
14737/* 14.16 The throw Statement */
14738
14739static tree
14740patch_throw_statement (node, wfl_op1)
14741 tree node, wfl_op1;
14742{
14743 tree expr = TREE_OPERAND (node, 0);
14744 tree type = TREE_TYPE (expr);
14745 int unchecked_ok = 0, tryblock_throws_ok = 0;
14746
14747 /* Thrown expression must be assignable to java.lang.Throwable */
14748 if (!try_reference_assignconv (throwable_type_node, expr))
14749 {
14750 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14751 parse_error_context (wfl_operator,
14752 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14753 lang_printable_name (type, 0));
b9f7e36c
APB
14754 /* If the thrown expression was a reference, we further the
14755 compile-time check. */
14756 if (!JREFERENCE_TYPE_P (type))
14757 return error_mark_node;
14758 }
14759
14760 /* At least one of the following must be true */
14761
14762 /* The type of the throw expression is a not checked exception,
14763 i.e. is a unchecked expression. */
c877974e 14764 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14765
c2952b01
APB
14766 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14767 /* An instance can't throw a checked excetion unless that exception
14768 is explicitely declared in the `throws' clause of each
14769 constructor. This doesn't apply to anonymous classes, since they
14770 don't have declared constructors. */
14771 if (!unchecked_ok
14772 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14773 {
14774 tree current;
14775 for (current = TYPE_METHODS (current_class); current;
14776 current = TREE_CHAIN (current))
14777 if (DECL_CONSTRUCTOR_P (current)
14778 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14779 {
14780 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)",
14781 lang_printable_name (TREE_TYPE (expr), 0));
14782 return error_mark_node;
14783 }
14784 }
14785
b9f7e36c
APB
14786 /* Throw is contained in a try statement and at least one catch
14787 clause can receive the thrown expression or the current method is
14788 declared to throw such an exception. Or, the throw statement is
14789 contained in a method or constructor declaration and the type of
14790 the Expression is assignable to at least one type listed in the
14791 throws clause the declaration. */
b9f7e36c 14792 if (!unchecked_ok)
f099f336 14793 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14794 if (!(unchecked_ok || tryblock_throws_ok))
14795 {
14796 /* If there is a surrounding try block that has no matching
14797 clatch clause, report it first. A surrounding try block exits
14798 only if there is something after the list of checked
14799 exception thrown by the current function (if any). */
14800 if (IN_TRY_BLOCK_P ())
781b0558 14801 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 14802 lang_printable_name (type, 0));
b9f7e36c
APB
14803 /* If we have no surrounding try statement and the method doesn't have
14804 any throws, report it now. FIXME */
f099f336
APB
14805
14806 /* We report that the exception can't be throw from a try block
14807 in all circumstances but when the `throw' is inside a static
14808 block. */
b9f7e36c
APB
14809 else if (!EXCEPTIONS_P (currently_caught_type_list)
14810 && !tryblock_throws_ok)
f099f336 14811 {
c2952b01 14812 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14813 parse_error_context (wfl_operator,
14814 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14815 lang_printable_name (type, 0));
14816 else
781b0558
KG
14817 parse_error_context (wfl_operator,
14818 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14819 lang_printable_name (type, 0));
14820 }
b9f7e36c
APB
14821 /* Otherwise, the current method doesn't have the appropriate
14822 throws declaration */
14823 else
781b0558 14824 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14825 lang_printable_name (type, 0));
b9f7e36c
APB
14826 return error_mark_node;
14827 }
14828
ce6e9147 14829 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14830 BUILD_THROW (node, expr);
ce6e9147
APB
14831
14832 /* If doing xrefs, keep the location where the `throw' was seen. */
14833 if (flag_emit_xref)
14834 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14835 return node;
14836}
14837
14838/* Check that exception said to be thrown by method DECL can be
14839 effectively caught from where DECL is invoked. */
14840
14841static void
14842check_thrown_exceptions (location, decl)
14843 int location;
14844 tree decl;
14845{
14846 tree throws;
14847 /* For all the unchecked exceptions thrown by DECL */
14848 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14849 throws = TREE_CHAIN (throws))
0a2138e2 14850 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14851 {
3e78f871
PB
14852#if 1
14853 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14854 if (DECL_NAME (decl) == get_identifier ("clone"))
14855 continue;
14856#endif
b9f7e36c 14857 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14858 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14859 parse_error_context
14860 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14861 lang_printable_name (TREE_VALUE (throws), 0));
14862 else
14863 {
14864 parse_error_context
781b0558 14865 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14866 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14867 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14868 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14869 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14870 }
b9f7e36c
APB
14871 }
14872}
14873
c877974e 14874/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14875 try-catch blocks, OR is listed in the `throws' clause of the
14876 current method. */
14877
14878static int
0a2138e2 14879check_thrown_exceptions_do (exception)
b9f7e36c
APB
14880 tree exception;
14881{
14882 tree list = currently_caught_type_list;
c877974e 14883 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14884 /* First, all the nested try-catch-finally at that stage. The
14885 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14886 if (IS_UNCHECKED_EXCEPTION_P (exception))
14887 return 1;
b9f7e36c
APB
14888 while (list)
14889 {
14890 tree caught;
14891 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14892 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14893 return 1;
14894 list = TREE_CHAIN (list);
14895 }
14896 return 0;
14897}
14898
14899static void
14900purge_unchecked_exceptions (mdecl)
14901 tree mdecl;
14902{
14903 tree throws = DECL_FUNCTION_THROWS (mdecl);
14904 tree new = NULL_TREE;
14905
14906 while (throws)
14907 {
14908 tree next = TREE_CHAIN (throws);
c877974e 14909 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14910 {
14911 TREE_CHAIN (throws) = new;
14912 new = throws;
14913 }
14914 throws = next;
14915 }
14916 /* List is inverted here, but it doesn't matter */
14917 DECL_FUNCTION_THROWS (mdecl) = new;
14918}
22eed1e6
APB
14919
14920/* 15.24 Conditional Operator ?: */
14921
14922static tree
14923patch_conditional_expr (node, wfl_cond, wfl_op1)
14924 tree node, wfl_cond, wfl_op1;
14925{
14926 tree cond = TREE_OPERAND (node, 0);
14927 tree op1 = TREE_OPERAND (node, 1);
14928 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14929 tree resulting_type = NULL_TREE;
ac825856 14930 tree t1, t2, patched;
22eed1e6
APB
14931 int error_found = 0;
14932
ac825856
APB
14933 /* Operands of ?: might be StringBuffers crafted as a result of a
14934 string concatenation. Obtain a descent operand here. */
14935 if ((patched = patch_string (op1)))
14936 TREE_OPERAND (node, 1) = op1 = patched;
14937 if ((patched = patch_string (op2)))
14938 TREE_OPERAND (node, 2) = op2 = patched;
14939
14940 t1 = TREE_TYPE (op1);
14941 t2 = TREE_TYPE (op2);
14942
22eed1e6
APB
14943 /* The first expression must be a boolean */
14944 if (TREE_TYPE (cond) != boolean_type_node)
14945 {
14946 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14947 parse_error_context (wfl_operator,
14948 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14949 lang_printable_name (TREE_TYPE (cond), 0));
14950 error_found = 1;
14951 }
14952
14953 /* Second and third can be numeric, boolean (i.e. primitive),
14954 references or null. Anything else results in an error */
14955 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14956 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14957 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14958 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14959 error_found = 1;
14960
14961 /* Determine the type of the conditional expression. Same types are
14962 easy to deal with */
14963 else if (t1 == t2)
14964 resulting_type = t1;
14965
14966 /* There are different rules for numeric types */
14967 else if (JNUMERIC_TYPE_P (t1))
14968 {
14969 /* if byte/short found, the resulting type is short */
14970 if ((t1 == byte_type_node && t2 == short_type_node)
14971 || (t1 == short_type_node && t2 == byte_type_node))
14972 resulting_type = short_type_node;
14973
14974 /* If t1 is a constant int and t2 is of type byte, short or char
14975 and t1's value fits in t2, then the resulting type is t2 */
14976 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14977 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14978 resulting_type = t2;
14979
14980 /* If t2 is a constant int and t1 is of type byte, short or char
14981 and t2's value fits in t1, then the resulting type is t1 */
14982 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14983 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14984 resulting_type = t1;
14985
14986 /* Otherwise, binary numeric promotion is applied and the
14987 resulting type is the promoted type of operand 1 and 2 */
14988 else
93024893 14989 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14990 &TREE_OPERAND (node, 1),
14991 &TREE_OPERAND (node, 2));
14992 }
14993
14994 /* Cases of a reference and a null type */
14995 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14996 resulting_type = t1;
14997
14998 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14999 resulting_type = t2;
15000
15001 /* Last case: different reference types. If a type can be converted
15002 into the other one by assignment conversion, the latter
15003 determines the type of the expression */
15004 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15005 resulting_type = promote_type (t1);
15006
15007 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15008 resulting_type = promote_type (t2);
15009
15010 /* If we don't have any resulting type, we're in trouble */
15011 if (!resulting_type)
15012 {
c2e3db92 15013 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15014 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15015 parse_error_context (wfl_operator,
15016 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15017 t, lang_printable_name (t2, 0));
22eed1e6
APB
15018 free (t);
15019 error_found = 1;
15020 }
15021
15022 if (error_found)
15023 {
15024 TREE_TYPE (node) = error_mark_node;
15025 return error_mark_node;
15026 }
15027
15028 TREE_TYPE (node) = resulting_type;
15029 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15030 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15031 return node;
15032}
ac825856 15033
5b09b33e
PB
15034/* Try to constant fold NODE.
15035 If NODE is not a constant expression, return NULL_EXPR.
15036 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15037
15038static tree
15039fold_constant_for_init (node, context)
15040 tree node;
15041 tree context;
15042{
15043 tree op0, op1, val;
15044 enum tree_code code = TREE_CODE (node);
15045
ee97d354 15046 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15047 return node;
93024893 15048
5b09b33e
PB
15049 switch (code)
15050 {
5b09b33e
PB
15051 case PLUS_EXPR:
15052 case MINUS_EXPR:
bc3ca41b
PB
15053 case MULT_EXPR:
15054 case TRUNC_MOD_EXPR:
15055 case RDIV_EXPR:
5b09b33e
PB
15056 case LSHIFT_EXPR:
15057 case RSHIFT_EXPR:
15058 case URSHIFT_EXPR:
15059 case BIT_AND_EXPR:
15060 case BIT_XOR_EXPR:
15061 case BIT_IOR_EXPR:
5b09b33e
PB
15062 case TRUTH_ANDIF_EXPR:
15063 case TRUTH_ORIF_EXPR:
15064 case EQ_EXPR:
15065 case NE_EXPR:
15066 case GT_EXPR:
15067 case GE_EXPR:
15068 case LT_EXPR:
15069 case LE_EXPR:
15070 op0 = TREE_OPERAND (node, 0);
15071 op1 = TREE_OPERAND (node, 1);
15072 val = fold_constant_for_init (op0, context);
15073 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15074 return NULL_TREE;
15075 TREE_OPERAND (node, 0) = val;
15076 val = fold_constant_for_init (op1, context);
15077 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15078 return NULL_TREE;
15079 TREE_OPERAND (node, 1) = val;
15080 return patch_binop (node, op0, op1);
15081
15082 case UNARY_PLUS_EXPR:
15083 case NEGATE_EXPR:
15084 case TRUTH_NOT_EXPR:
15085 case BIT_NOT_EXPR:
15086 case CONVERT_EXPR:
15087 op0 = TREE_OPERAND (node, 0);
15088 val = fold_constant_for_init (op0, context);
15089 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15090 return NULL_TREE;
15091 TREE_OPERAND (node, 0) = val;
5a005d9e 15092 return patch_unaryop (node, op0);
5b09b33e
PB
15093 break;
15094
15095 case COND_EXPR:
15096 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15097 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15098 return NULL_TREE;
15099 TREE_OPERAND (node, 0) = val;
15100 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15101 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15102 return NULL_TREE;
15103 TREE_OPERAND (node, 1) = val;
15104 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15105 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15106 return NULL_TREE;
15107 TREE_OPERAND (node, 2) = val;
15108 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15109 : TREE_OPERAND (node, 2);
15110
15111 case VAR_DECL:
8576f094
APB
15112 case FIELD_DECL:
15113 if (! FIELD_FINAL (node)
5b09b33e
PB
15114 || DECL_INITIAL (node) == NULL_TREE)
15115 return NULL_TREE;
15116 val = DECL_INITIAL (node);
15117 /* Guard against infinite recursion. */
15118 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15119 val = fold_constant_for_init (val, node);
5b09b33e
PB
15120 DECL_INITIAL (node) = val;
15121 return val;
15122
15123 case EXPR_WITH_FILE_LOCATION:
15124 /* Compare java_complete_tree and resolve_expression_name. */
15125 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15126 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15127 {
15128 tree name = EXPR_WFL_NODE (node);
15129 tree decl;
15130 if (PRIMARY_P (node))
15131 return NULL_TREE;
15132 else if (! QUALIFIED_P (name))
15133 {
15134 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15135 if (decl == NULL_TREE
15136 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15137 return NULL_TREE;
15138 return fold_constant_for_init (decl, decl);
15139 }
15140 else
15141 {
5b09b33e
PB
15142 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15143 qualify_ambiguous_name (node);
15144 if (resolve_field_access (node, &decl, NULL)
15145 && decl != NULL_TREE)
15146 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15147 return NULL_TREE;
15148 }
15149 }
15150 else
15151 {
15152 op0 = TREE_OPERAND (node, 0);
15153 val = fold_constant_for_init (op0, context);
15154 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15155 return NULL_TREE;
15156 TREE_OPERAND (node, 0) = val;
15157 return val;
15158 }
15159
bc3ca41b
PB
15160#ifdef USE_COMPONENT_REF
15161 case IDENTIFIER:
15162 case COMPONENT_REF:
15163 ?;
15164#endif
15165
5b09b33e
PB
15166 default:
15167 return NULL_TREE;
15168 }
15169}
bc3ca41b
PB
15170
15171#ifdef USE_COMPONENT_REF
15172/* Context is 'T' for TypeName, 'P' for PackageName,
15173 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15174
15175tree
15176resolve_simple_name (name, context)
15177 tree name;
15178 int context;
15179{
15180}
15181
15182tree
15183resolve_qualified_name (name, context)
15184 tree name;
15185 int context;
15186{
15187}
15188#endif
This page took 2.429319 seconds and 5 git commands to generate.