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