]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/java/parse.y
Remove obstacks.
[thirdparty/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));
f15b9af9 326static void mark_parser_ctxt PARAMS ((void *));
165f37bc 327
e04a16fb
AG
328/* Number of error found so far. */
329int java_error_count;
330/* Number of warning found so far. */
331int java_warning_count;
ce6e9147
APB
332/* Tell when not to fold, when doing xrefs */
333int do_not_fold;
c2952b01
APB
334/* Cyclic inheritance report, as it can be set by layout_class */
335char *cyclic_inheritance_report;
f15b9af9 336
c2952b01
APB
337/* Tell when we're within an instance initializer */
338static int in_instance_initializer;
e04a16fb
AG
339
340/* The current parser context */
d4370213 341struct parser_ctxt *ctxp;
e04a16fb 342
d4370213 343/* List of things that were analyzed for which code will be generated */
b351b287
APB
344static struct parser_ctxt *ctxp_for_generation = NULL;
345
e04a16fb
AG
346/* binop_lookup maps token to tree_code. It is used where binary
347 operations are involved and required by the parser. RDIV_EXPR
348 covers both integral/floating point division. The code is changed
349 once the type of both operator is worked out. */
350
351static enum tree_code binop_lookup[19] =
352 {
353 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
354 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
355 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
356 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
357 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
358 };
359#define BINOP_LOOKUP(VALUE) \
6e2aa220 360 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 361
5cbdba64
APB
362/* This is the end index for binary operators that can also be used
363 in compound assignements. */
364#define BINOP_COMPOUND_CANDIDATES 11
365
e04a16fb 366/* The "$L" identifier we use to create labels. */
b67d701b
PB
367static tree label_id = NULL_TREE;
368
369/* The "StringBuffer" identifier used for the String `+' operator. */
370static tree wfl_string_buffer = NULL_TREE;
371
372/* The "append" identifier used for String `+' operator. */
373static tree wfl_append = NULL_TREE;
374
375/* The "toString" identifier used for String `+' operator. */
376static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
377
378/* The "java.lang" import qualified name. */
379static tree java_lang_id = NULL_TREE;
09ed0f70 380
c2952b01
APB
381/* The generated `inst$' identifier used for generated enclosing
382 instance/field access functions. */
383static tree inst_id = NULL_TREE;
384
09ed0f70
APB
385/* The "java.lang.Cloneable" qualified name. */
386static tree java_lang_cloneable = NULL_TREE;
f099f336 387
ee17a290
TT
388/* The "java.io.Serializable" qualified name. */
389static tree java_io_serializable = NULL_TREE;
390
f099f336
APB
391/* Context and flag for static blocks */
392static tree current_static_block = NULL_TREE;
393
c2952b01
APB
394/* The generated `write_parm_value$' identifier. */
395static tree wpv_id;
396
ee07f4f4
APB
397/* The list of all packages we've seen so far */
398static tree package_list = NULL_TREE;
2884c41e 399
19e223db
MM
400/* Hold THIS for the scope of the current public method decl. */
401static tree current_this;
402
403/* Hold a list of catch clauses list. The first element of this list is
404 the list of the catch clauses of the currently analysed try block. */
405static tree currently_caught_type_list;
406
2884c41e
KG
407/* Check modifiers. If one doesn't fit, retrieve it in its declaration
408 line and point it out. */
409/* Should point out the one that don't fit. ASCII/unicode, going
410 backward. FIXME */
411
412#define check_modifiers(__message, __value, __mask) do { \
413 if ((__value) & ~(__mask)) \
414 { \
415 int i, remainder = (__value) & ~(__mask); \
416 for (i = 0; i <= 10; i++) \
417 if ((1 << i) & remainder) \
418 parse_error_context (ctxp->modifier_ctx [i], (__message), \
419 java_accstring_lookup (1 << i)); \
420 } \
421} while (0)
ee07f4f4 422
e04a16fb
AG
423%}
424
425%union {
426 tree node;
427 int sub_token;
428 struct {
429 int token;
430 int location;
431 } operator;
432 int value;
433}
434
9ee9b555
KG
435%{
436#include "lex.c"
437%}
438
e04a16fb
AG
439%pure_parser
440
441/* Things defined here have to match the order of what's in the
442 binop_lookup table. */
443
444%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
445%token LS_TK SRS_TK ZRS_TK
446%token AND_TK XOR_TK OR_TK
447%token BOOL_AND_TK BOOL_OR_TK
448%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
449
450/* This maps to the same binop_lookup entry than the token above */
451
452%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
453%token REM_ASSIGN_TK
454%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
455%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
456
457
458/* Modifier TOKEN have to be kept in this order. Don't scramble it */
459
460%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
461%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
462%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
463%token PAD_TK ABSTRACT_TK MODIFIER_TK
464
465/* Keep those two in order, too */
466%token DECR_TK INCR_TK
467
468/* From now one, things can be in any order */
469
470%token DEFAULT_TK IF_TK THROW_TK
471%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
472%token THROWS_TK BREAK_TK IMPORT_TK
473%token ELSE_TK INSTANCEOF_TK RETURN_TK
474%token VOID_TK CATCH_TK INTERFACE_TK
475%token CASE_TK EXTENDS_TK FINALLY_TK
476%token SUPER_TK WHILE_TK CLASS_TK
477%token SWITCH_TK CONST_TK TRY_TK
478%token FOR_TK NEW_TK CONTINUE_TK
479%token GOTO_TK PACKAGE_TK THIS_TK
480
481%token BYTE_TK SHORT_TK INT_TK LONG_TK
482%token CHAR_TK INTEGRAL_TK
483
484%token FLOAT_TK DOUBLE_TK FP_TK
485
486%token ID_TK
487
488%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
489
490%token ASSIGN_ANY_TK ASSIGN_TK
491%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
492
493%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
494%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
495
c2952b01 496%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
497
498%type <node> super ID_TK identifier
499%type <node> name simple_name qualified_name
c2952b01 500%type <node> type_declaration compilation_unit
e04a16fb
AG
501 field_declaration method_declaration extends_interfaces
502 interfaces interface_type_list
c2952b01 503 class_member_declaration
e04a16fb
AG
504 import_declarations package_declaration
505 type_declarations interface_body
506 interface_member_declaration constant_declaration
507 interface_member_declarations interface_type
508 abstract_method_declaration interface_type_list
509%type <node> class_body_declaration class_member_declaration
510 static_initializer constructor_declaration block
22eed1e6 511%type <node> class_body_declarations constructor_header
e04a16fb
AG
512%type <node> class_or_interface_type class_type class_type_list
513 constructor_declarator explicit_constructor_invocation
b9f7e36c 514%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
515
516%type <node> variable_declarator_id variable_declarator
517 variable_declarators variable_initializer
22eed1e6 518 variable_initializers constructor_body
ac825856 519 array_initializer
e04a16fb 520
2e5eb5c5 521%type <node> class_body block_end constructor_block_end
e04a16fb
AG
522%type <node> statement statement_without_trailing_substatement
523 labeled_statement if_then_statement label_decl
524 if_then_else_statement while_statement for_statement
525 statement_nsi labeled_statement_nsi do_statement
526 if_then_else_statement_nsi while_statement_nsi
527 for_statement_nsi statement_expression_list for_init
528 for_update statement_expression expression_statement
529 primary_no_new_array expression primary
530 array_creation_expression array_type
531 class_instance_creation_expression field_access
532 method_invocation array_access something_dot_new
533 argument_list postfix_expression while_expression
534 post_increment_expression post_decrement_expression
535 unary_expression_not_plus_minus unary_expression
536 pre_increment_expression pre_decrement_expression
537 unary_expression_not_plus_minus cast_expression
538 multiplicative_expression additive_expression
539 shift_expression relational_expression
540 equality_expression and_expression
541 exclusive_or_expression inclusive_or_expression
542 conditional_and_expression conditional_or_expression
543 conditional_expression assignment_expression
544 left_hand_side assignment for_header for_begin
545 constant_expression do_statement_begin empty_statement
b67d701b 546 switch_statement synchronized_statement throw_statement
f8976021 547 try_statement switch_expression switch_block
15fdcfe9 548 catches catch_clause catch_clause_parameter finally
c2952b01 549 anonymous_class_creation
e04a16fb
AG
550%type <node> return_statement break_statement continue_statement
551
552%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
553%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
554%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
555%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
556%type <operator> ASSIGN_ANY_TK assignment_operator
557%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
558%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
559%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 560%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 561%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
562%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
563%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 564%type <operator> NEW_TK
e04a16fb
AG
565
566%type <node> method_body
567
568%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
569 STRING_LIT_TK NULL_TK VOID_TK
570
571%type <node> IF_TK WHILE_TK FOR_TK
572
573%type <node> formal_parameter_list formal_parameter
574 method_declarator method_header
575
c2952b01 576%type <node> primitive_type reference_type type
e04a16fb
AG
577 BOOLEAN_TK INTEGRAL_TK FP_TK
578
c2952b01
APB
579/* Added or modified JDK 1.1 rule types */
580%type <node> type_literals array_type_literal
581
e04a16fb
AG
582%%
583/* 19.2 Production from 2.3: The Syntactic Grammar */
584goal:
19e223db
MM
585 {
586 /* Register static variables with the garbage
587 collector. */
588 ggc_add_tree_root (&label_id, 1);
589 ggc_add_tree_root (&wfl_string_buffer, 1);
590 ggc_add_tree_root (&wfl_append, 1);
591 ggc_add_tree_root (&wfl_to_string, 1);
592 ggc_add_tree_root (&java_lang_id, 1);
593 ggc_add_tree_root (&inst_id, 1);
594 ggc_add_tree_root (&java_lang_cloneable, 1);
595 ggc_add_tree_root (&java_io_serializable, 1);
596 ggc_add_tree_root (&current_static_block, 1);
597 ggc_add_tree_root (&wpv_id, 1);
598 ggc_add_tree_root (&package_list, 1);
599 ggc_add_tree_root (&current_this, 1);
600 ggc_add_tree_root (&currently_caught_type_list, 1);
1f8f4a0b 601 ggc_add_string_root (&cyclic_inheritance_report, 1);
f15b9af9
MM
602 ggc_add_root (&ctxp, 1,
603 sizeof (struct parser_ctxt *),
604 mark_parser_ctxt);
605 ggc_add_root (&ctxp_for_generation, 1,
606 sizeof (struct parser_ctxt *),
607 mark_parser_ctxt);
19e223db 608 }
e04a16fb
AG
609 compilation_unit
610 {}
611;
612
613/* 19.3 Productions from 3: Lexical structure */
614literal:
615 INT_LIT_TK
616| FP_LIT_TK
617| BOOL_LIT_TK
618| CHAR_LIT_TK
619| STRING_LIT_TK
620| NULL_TK
621;
622
623/* 19.4 Productions from 4: Types, Values and Variables */
624type:
625 primitive_type
626| reference_type
627;
628
629primitive_type:
630 INTEGRAL_TK
631| FP_TK
632| BOOLEAN_TK
633;
634
635reference_type:
636 class_or_interface_type
637| array_type
638;
639
640class_or_interface_type:
641 name
642;
643
644class_type:
645 class_or_interface_type /* Default rule */
646;
647
648interface_type:
649 class_or_interface_type
650;
651
652array_type:
653 primitive_type OSB_TK CSB_TK
654 {
655 $$ = build_java_array_type ($1, -1);
656 CLASS_LOADED_P ($$) = 1;
657 }
658| name OSB_TK CSB_TK
659 { $$ = build_unresolved_array_type ($1); }
660| array_type OSB_TK CSB_TK
661 { $$ = build_unresolved_array_type ($1); }
662| primitive_type OSB_TK error
663 {RULE ("']' expected"); RECOVER;}
664| array_type OSB_TK error
665 {RULE ("']' expected"); RECOVER;}
666;
667
668/* 19.5 Productions from 6: Names */
669name:
670 simple_name /* Default rule */
671| qualified_name /* Default rule */
672;
673
674simple_name:
675 identifier /* Default rule */
676;
677
678qualified_name:
679 name DOT_TK identifier
680 { $$ = make_qualified_name ($1, $3, $2.location); }
681;
682
683identifier:
684 ID_TK
685;
686
687/* 19.6: Production from 7: Packages */
688compilation_unit:
689 {$$ = NULL;}
690| package_declaration
691| import_declarations
692| type_declarations
693| package_declaration import_declarations
694| package_declaration type_declarations
695| import_declarations type_declarations
696| package_declaration import_declarations type_declarations
697;
698
699import_declarations:
700 import_declaration
701 {
702 $$ = NULL;
703 }
704| import_declarations import_declaration
705 {
706 $$ = NULL;
707 }
708;
709
710type_declarations:
711 type_declaration
712| type_declarations type_declaration
713;
714
715package_declaration:
716 PACKAGE_TK name SC_TK
ee07f4f4
APB
717 {
718 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 719 register_package (ctxp->package);
ee07f4f4 720 }
e04a16fb
AG
721| PACKAGE_TK error
722 {yyerror ("Missing name"); RECOVER;}
723| PACKAGE_TK name error
724 {yyerror ("';' expected"); RECOVER;}
725;
726
727import_declaration:
728 single_type_import_declaration
729| type_import_on_demand_declaration
730;
731
732single_type_import_declaration:
733 IMPORT_TK name SC_TK
734 {
9a7ab4b3 735 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 736 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 737 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
738 while (last != IDENTIFIER_POINTER (name))
739 {
740 if (last [0] == '.')
741 break;
742 last--;
743 }
744 last_name = get_identifier (++last);
745 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
746 {
747 tree err = find_name_in_single_imports (last_name);
748 if (err && err != name)
749 parse_error_context
750 ($2, "Ambiguous class: `%s' and `%s'",
751 IDENTIFIER_POINTER (name),
752 IDENTIFIER_POINTER (err));
5e942c50 753 else
9a7ab4b3 754 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
755 }
756 else
5e942c50 757 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
758 }
759| IMPORT_TK error
760 {yyerror ("Missing name"); RECOVER;}
761| IMPORT_TK name error
762 {yyerror ("';' expected"); RECOVER;}
763;
764
765type_import_on_demand_declaration:
766 IMPORT_TK name DOT_TK MULT_TK SC_TK
767 {
768 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
769 /* Don't import java.lang.* twice. */
770 if (name != java_lang_id)
771 {
ba179f9f 772 read_import_dir ($2);
9a7ab4b3
APB
773 ctxp->import_demand_list =
774 chainon (ctxp->import_demand_list,
775 build_tree_list ($2, NULL_TREE));
ba179f9f 776 }
e04a16fb
AG
777 }
778| IMPORT_TK name DOT_TK error
779 {yyerror ("'*' expected"); RECOVER;}
780| IMPORT_TK name DOT_TK MULT_TK error
781 {yyerror ("';' expected"); RECOVER;}
782;
783
784type_declaration:
785 class_declaration
c2952b01 786 { end_class_declaration (0); }
e04a16fb 787| interface_declaration
c2952b01 788 { end_class_declaration (0); }
5f1c312a 789| empty_statement
e04a16fb
AG
790| error
791 {
792 YYERROR_NOW;
793 yyerror ("Class or interface declaration expected");
794 }
795;
796
797/* 19.7 Shortened from the original:
798 modifiers: modifier | modifiers modifier
799 modifier: any of public... */
800modifiers:
801 MODIFIER_TK
802 {
803 $$ = (1 << $1);
804 }
805| modifiers MODIFIER_TK
806 {
807 int acc = (1 << $2);
808 if ($$ & acc)
809 parse_error_context
810 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
811 java_accstring_lookup (acc));
812 else
813 {
814 $$ |= acc;
815 }
816 }
817;
818
819/* 19.8.1 Production from $8.1: Class Declaration */
820class_declaration:
821 modifiers CLASS_TK identifier super interfaces
822 { create_class ($1, $3, $4, $5); }
823 class_body
e04a16fb
AG
824| CLASS_TK identifier super interfaces
825 { create_class (0, $2, $3, $4); }
826 class_body
e04a16fb
AG
827| modifiers CLASS_TK error
828 {yyerror ("Missing class name"); RECOVER;}
829| CLASS_TK error
830 {yyerror ("Missing class name"); RECOVER;}
831| CLASS_TK identifier error
0b4d333e
APB
832 {
833 if (!ctxp->class_err) yyerror ("'{' expected");
834 DRECOVER(class1);
835 }
e04a16fb
AG
836| modifiers CLASS_TK identifier error
837 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
838;
839
840super:
841 { $$ = NULL; }
842| EXTENDS_TK class_type
843 { $$ = $2; }
844| EXTENDS_TK class_type error
845 {yyerror ("'{' expected"); ctxp->class_err=1;}
846| EXTENDS_TK error
847 {yyerror ("Missing super class name"); ctxp->class_err=1;}
848;
849
850interfaces:
851 { $$ = NULL_TREE; }
852| IMPLEMENTS_TK interface_type_list
853 { $$ = $2; }
854| IMPLEMENTS_TK error
855 {
856 ctxp->class_err=1;
857 yyerror ("Missing interface name");
858 }
859;
860
861interface_type_list:
862 interface_type
863 {
864 ctxp->interface_number = 1;
865 $$ = build_tree_list ($1, NULL_TREE);
866 }
867| interface_type_list C_TK interface_type
868 {
869 ctxp->interface_number++;
870 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
871 }
872| interface_type_list C_TK error
873 {yyerror ("Missing interface name"); RECOVER;}
874;
875
876class_body:
877 OCB_TK 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 ($2.location, 1);
c2952b01 883 $$ = GET_CPC ();
7f10c2e2 884 }
e04a16fb 885| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
886 {
887 /* Store the location of the `}' when doing xrefs */
888 if (flag_emit_xref)
c2952b01 889 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 890 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 891 $$ = GET_CPC ();
7f10c2e2 892 }
e04a16fb
AG
893;
894
895class_body_declarations:
896 class_body_declaration
897| class_body_declarations class_body_declaration
898;
899
900class_body_declaration:
901 class_member_declaration
902| static_initializer
903| constructor_declaration
904| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
905 {
906 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
907 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
908 }
e04a16fb
AG
909;
910
911class_member_declaration:
912 field_declaration
913| method_declaration
914| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
915 { end_class_declaration (1); }
916| interface_declaration /* Added, JDK1.1 inner interfaces */
917 { end_class_declaration (1); }
5f1c312a 918| empty_statement
e04a16fb
AG
919;
920
921/* 19.8.2 Productions from 8.3: Field Declarations */
922field_declaration:
923 type variable_declarators SC_TK
924 { register_fields (0, $1, $2); }
925| modifiers type variable_declarators SC_TK
926 {
e04a16fb
AG
927 check_modifiers
928 ("Illegal modifier `%s' for field declaration",
929 $1, FIELD_MODIFIERS);
930 check_modifiers_consistency ($1);
931 register_fields ($1, $2, $3);
932 }
933;
934
935variable_declarators:
936 /* Should we use build_decl_list () instead ? FIXME */
937 variable_declarator /* Default rule */
938| variable_declarators C_TK variable_declarator
939 { $$ = chainon ($1, $3); }
940| variable_declarators C_TK error
941 {yyerror ("Missing term"); RECOVER;}
942;
943
944variable_declarator:
945 variable_declarator_id
946 { $$ = build_tree_list ($1, NULL_TREE); }
947| variable_declarator_id ASSIGN_TK variable_initializer
948 {
949 if (java_error_count)
950 $3 = NULL_TREE;
951 $$ = build_tree_list
952 ($1, build_assignment ($2.token, $2.location, $1, $3));
953 }
954| variable_declarator_id ASSIGN_TK error
955 {
956 yyerror ("Missing variable initializer");
957 $$ = build_tree_list ($1, NULL_TREE);
958 RECOVER;
959 }
960| variable_declarator_id ASSIGN_TK variable_initializer error
961 {
962 yyerror ("';' expected");
963 $$ = build_tree_list ($1, NULL_TREE);
964 RECOVER;
965 }
966;
967
968variable_declarator_id:
969 identifier
970| variable_declarator_id OSB_TK CSB_TK
c583dd46 971 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
972| identifier error
973 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
974| variable_declarator_id OSB_TK error
29f8b718
APB
975 {
976 tree node = java_lval.node;
977 if (node && (TREE_CODE (node) == INTEGER_CST
978 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
979 yyerror ("Can't specify array dimension in a declaration");
980 else
981 yyerror ("']' expected");
982 DRECOVER(vdi);
983 }
e04a16fb
AG
984| variable_declarator_id CSB_TK error
985 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
986;
987
988variable_initializer:
989 expression
990| array_initializer
e04a16fb
AG
991;
992
993/* 19.8.3 Productions from 8.4: Method Declarations */
994method_declaration:
995 method_header
996 {
997 current_function_decl = $1;
c2952b01
APB
998 if (current_function_decl
999 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1000 source_start_java_method (current_function_decl);
1001 else
1002 current_function_decl = NULL_TREE;
e04a16fb
AG
1003 }
1004 method_body
b635eb2f 1005 { finish_method_declaration ($3); }
e04a16fb
AG
1006| method_header error
1007 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1008;
1009
1010method_header:
1011 type method_declarator throws
b9f7e36c 1012 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 1013| VOID_TK method_declarator throws
b9f7e36c 1014 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 1015| modifiers type method_declarator throws
b9f7e36c 1016 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 1017| modifiers VOID_TK method_declarator throws
b9f7e36c 1018 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 1019| type error
efa0a23f
APB
1020 {
1021 yyerror ("Invalid method declaration, method name required");
1022 RECOVER;
1023 }
e04a16fb
AG
1024| modifiers type error
1025 {RECOVER;}
1026| VOID_TK error
1027 {yyerror ("Identifier expected"); RECOVER;}
1028| modifiers VOID_TK error
1029 {yyerror ("Identifier expected"); RECOVER;}
1030| modifiers error
1031 {
1032 yyerror ("Invalid method declaration, return type required");
1033 RECOVER;
1034 }
1035;
1036
1037method_declarator:
1038 identifier OP_TK CP_TK
c2952b01
APB
1039 {
1040 ctxp->formal_parameter_number = 0;
1041 $$ = method_declarator ($1, NULL_TREE);
1042 }
e04a16fb
AG
1043| identifier OP_TK formal_parameter_list CP_TK
1044 { $$ = method_declarator ($1, $3); }
1045| method_declarator OSB_TK CSB_TK
1046 {
1886c9d8
APB
1047 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1048 TREE_PURPOSE ($1) =
1049 build_unresolved_array_type (TREE_PURPOSE ($1));
1050 parse_warning_context
1051 (wfl_operator,
1052 "Discouraged form of returned type specification");
e04a16fb
AG
1053 }
1054| identifier OP_TK error
1055 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1056| method_declarator OSB_TK error
1057 {yyerror ("']' expected"); RECOVER;}
1058;
1059
1060formal_parameter_list:
1061 formal_parameter
1062 {
1063 ctxp->formal_parameter_number = 1;
1064 }
1065| formal_parameter_list C_TK formal_parameter
1066 {
1067 ctxp->formal_parameter_number += 1;
1068 $$ = chainon ($1, $3);
1069 }
1070| formal_parameter_list C_TK error
c2952b01 1071 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1072;
1073
1074formal_parameter:
1075 type variable_declarator_id
1076 {
1077 $$ = build_tree_list ($2, $1);
1078 }
18990de5 1079| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1080 {
5256aa37 1081 $$ = build_tree_list ($3, $2);
c2952b01 1082 ARG_FINAL_P ($$) = 1;
5256aa37 1083 }
e04a16fb 1084| type error
f8989a66
APB
1085 {
1086 yyerror ("Missing identifier"); RECOVER;
1087 $$ = NULL_TREE;
1088 }
18990de5 1089| final type error
e04a16fb 1090 {
e04a16fb 1091 yyerror ("Missing identifier"); RECOVER;
f8989a66 1092 $$ = NULL_TREE;
e04a16fb
AG
1093 }
1094;
1095
18990de5
JB
1096final:
1097 modifiers
1098 {
1099 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1100 $1, ACC_FINAL);
1101 if ($1 != ACC_FINAL)
1102 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1103 }
1104;
1105
e04a16fb 1106throws:
b9f7e36c 1107 { $$ = NULL_TREE; }
e04a16fb 1108| THROWS_TK class_type_list
b9f7e36c 1109 { $$ = $2; }
e04a16fb
AG
1110| THROWS_TK error
1111 {yyerror ("Missing class type term"); RECOVER;}
1112;
1113
1114class_type_list:
1115 class_type
c877974e 1116 { $$ = build_tree_list ($1, $1); }
e04a16fb 1117| class_type_list C_TK class_type
c877974e 1118 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1119| class_type_list C_TK error
1120 {yyerror ("Missing class type term"); RECOVER;}
1121;
1122
1123method_body:
1124 block
5f1c312a 1125| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1126;
1127
1128/* 19.8.4 Productions from 8.5: Static Initializers */
1129static_initializer:
1130 static block
1131 {
c2952b01
APB
1132 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1133 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb 1134 }
e04a16fb
AG
1135;
1136
1137static: /* Test lval.sub_token here */
c2952b01 1138 modifiers
e04a16fb 1139 {
c2952b01
APB
1140 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1141 /* Can't have a static initializer in an innerclass */
1142 if ($1 | ACC_STATIC &&
1143 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1144 parse_error_context
1145 (MODIFIER_WFL (STATIC_TK),
1146 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1147 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1148 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1149 }
1150;
1151
1152/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1153constructor_declaration:
22eed1e6 1154 constructor_header
e04a16fb 1155 {
22eed1e6
APB
1156 current_function_decl = $1;
1157 source_start_java_method (current_function_decl);
e04a16fb 1158 }
22eed1e6 1159 constructor_body
b635eb2f 1160 { finish_method_declaration ($3); }
22eed1e6
APB
1161;
1162
1163constructor_header:
1164 constructor_declarator throws
1165 { $$ = method_header (0, NULL_TREE, $1, $2); }
1166| modifiers constructor_declarator throws
1167 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1168;
1169
1170constructor_declarator:
1171 simple_name OP_TK CP_TK
c2952b01
APB
1172 {
1173 ctxp->formal_parameter_number = 0;
1174 $$ = method_declarator ($1, NULL_TREE);
1175 }
e04a16fb 1176| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1177 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1178;
1179
1180constructor_body:
22eed1e6
APB
1181 /* Unlike regular method, we always need a complete (empty)
1182 body so we can safely perform all the required code
1183 addition (super invocation and field initialization) */
2e5eb5c5 1184 block_begin constructor_block_end
22eed1e6 1185 {
9bbc7d9f 1186 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1187 $$ = $2;
1188 }
2e5eb5c5 1189| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1190 { $$ = $3; }
2e5eb5c5 1191| block_begin block_statements constructor_block_end
22eed1e6 1192 { $$ = $3; }
2e5eb5c5 1193| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1194 { $$ = $4; }
e04a16fb
AG
1195;
1196
2e5eb5c5
APB
1197constructor_block_end:
1198 block_end
5f1c312a 1199;
2e5eb5c5 1200
e04a16fb
AG
1201/* Error recovery for that rule moved down expression_statement: rule. */
1202explicit_constructor_invocation:
1203 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1204 {
1205 $$ = build_method_invocation ($1, NULL_TREE);
1206 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1207 $$ = java_method_add_stmt (current_function_decl, $$);
1208 }
e04a16fb 1209| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1210 {
1211 $$ = build_method_invocation ($1, $3);
1212 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1213 $$ = java_method_add_stmt (current_function_decl, $$);
1214 }
e04a16fb
AG
1215 /* Added, JDK1.1 inner classes. Modified because the rule
1216 'primary' couldn't work. */
1217| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1218 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1219| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1220 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1221;
1222
1223this_or_super: /* Added, simplifies error diagnostics */
1224 THIS_TK
1225 {
9ee9b555 1226 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1227 EXPR_WFL_LINECOL (wfl) = $1.location;
1228 $$ = wfl;
1229 }
1230| SUPER_TK
1231 {
9ee9b555 1232 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1233 EXPR_WFL_LINECOL (wfl) = $1.location;
1234 $$ = wfl;
1235 }
1236;
1237
1238/* 19.9 Productions from 9: Interfaces */
1239/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1240interface_declaration:
1241 INTERFACE_TK identifier
1242 { create_interface (0, $2, NULL_TREE); }
1243 interface_body
e04a16fb
AG
1244| modifiers INTERFACE_TK identifier
1245 { create_interface ($1, $3, NULL_TREE); }
1246 interface_body
e04a16fb
AG
1247| INTERFACE_TK identifier extends_interfaces
1248 { create_interface (0, $2, $3); }
1249 interface_body
e04a16fb
AG
1250| modifiers INTERFACE_TK identifier extends_interfaces
1251 { create_interface ($1, $3, $4); }
1252 interface_body
e04a16fb 1253| INTERFACE_TK identifier error
0b4d333e 1254 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1255| modifiers INTERFACE_TK identifier error
0b4d333e 1256 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1257;
1258
1259extends_interfaces:
1260 EXTENDS_TK interface_type
1261 {
1262 ctxp->interface_number = 1;
1263 $$ = build_tree_list ($2, NULL_TREE);
1264 }
1265| extends_interfaces C_TK interface_type
1266 {
1267 ctxp->interface_number++;
1268 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1269 }
1270| EXTENDS_TK error
1271 {yyerror ("Invalid interface type"); RECOVER;}
1272| extends_interfaces C_TK error
1273 {yyerror ("Missing term"); RECOVER;}
1274;
1275
1276interface_body:
1277 OCB_TK CCB_TK
1278 { $$ = NULL_TREE; }
1279| OCB_TK interface_member_declarations CCB_TK
1280 { $$ = NULL_TREE; }
1281;
1282
1283interface_member_declarations:
1284 interface_member_declaration
1285| interface_member_declarations interface_member_declaration
1286;
1287
1288interface_member_declaration:
1289 constant_declaration
1290| abstract_method_declaration
1291| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1292 { end_class_declaration (1); }
1293| interface_declaration /* Added, JDK1.1 inner interfaces */
1294 { end_class_declaration (1); }
e04a16fb
AG
1295;
1296
1297constant_declaration:
1298 field_declaration
1299;
1300
1301abstract_method_declaration:
1302 method_header SC_TK
1303 {
1304 check_abstract_method_header ($1);
1305 current_function_decl = NULL_TREE; /* FIXME ? */
1306 }
1307| method_header error
1308 {yyerror ("';' expected"); RECOVER;}
1309;
1310
1311/* 19.10 Productions from 10: Arrays */
1312array_initializer:
1313 OCB_TK CCB_TK
1179ebc2 1314 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1315| OCB_TK variable_initializers CCB_TK
f8976021 1316 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1317| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1318 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1319;
1320
1321variable_initializers:
1322 variable_initializer
f8976021
APB
1323 {
1324 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1325 $1, NULL_TREE);
1326 }
e04a16fb 1327| variable_initializers C_TK variable_initializer
1179ebc2
APB
1328 {
1329 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1330 }
e04a16fb
AG
1331| variable_initializers C_TK error
1332 {yyerror ("Missing term"); RECOVER;}
1333;
1334
1335/* 19.11 Production from 14: Blocks and Statements */
1336block:
1337 OCB_TK CCB_TK
7f10c2e2
APB
1338 {
1339 /* Store the location of the `}' when doing xrefs */
1340 if (current_function_decl && flag_emit_xref)
1341 DECL_END_SOURCE_LINE (current_function_decl) =
1342 EXPR_WFL_ADD_COL ($2.location, 1);
1343 $$ = empty_stmt_node;
1344 }
22eed1e6
APB
1345| block_begin block_statements block_end
1346 { $$ = $3; }
1347;
1348
1349block_begin:
1350 OCB_TK
e04a16fb 1351 { enter_block (); }
22eed1e6
APB
1352;
1353
1354block_end:
e04a16fb
AG
1355 CCB_TK
1356 {
1357 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1358 /* Store the location of the `}' when doing xrefs */
1359 if (current_function_decl && flag_emit_xref)
1360 DECL_END_SOURCE_LINE (current_function_decl) =
1361 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1362 $$ = exit_block ();
c280e37a
APB
1363 if (!BLOCK_SUBBLOCKS ($$))
1364 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1365 }
1366;
1367
1368block_statements:
1369 block_statement
1370| block_statements block_statement
1371;
1372
1373block_statement:
1374 local_variable_declaration_statement
1375| statement
15fdcfe9 1376 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1377| class_declaration /* Added, JDK1.1 local classes */
1378 {
1379 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1380 end_class_declaration (1);
1381 }
e04a16fb
AG
1382;
1383
1384local_variable_declaration_statement:
1385 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1386;
1387
1388local_variable_declaration:
1389 type variable_declarators
1390 { declare_local_variables (0, $1, $2); }
a003f638 1391| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1392 { declare_local_variables ($1, $2, $3); }
1393;
1394
1395statement:
1396 statement_without_trailing_substatement
1397| labeled_statement
e04a16fb 1398| if_then_statement
e04a16fb 1399| if_then_else_statement
e04a16fb 1400| while_statement
e04a16fb 1401| for_statement
cd9643f7 1402 { $$ = exit_block (); }
e04a16fb
AG
1403;
1404
1405statement_nsi:
1406 statement_without_trailing_substatement
1407| labeled_statement_nsi
e04a16fb 1408| if_then_else_statement_nsi
e04a16fb 1409| while_statement_nsi
e04a16fb 1410| for_statement_nsi
9dd939b2 1411 { $$ = exit_block (); }
e04a16fb
AG
1412;
1413
1414statement_without_trailing_substatement:
1415 block
e04a16fb 1416| empty_statement
e04a16fb 1417| expression_statement
e04a16fb 1418| switch_statement
e04a16fb 1419| do_statement
e04a16fb 1420| break_statement
e04a16fb 1421| continue_statement
e04a16fb
AG
1422| return_statement
1423| synchronized_statement
e04a16fb 1424| throw_statement
e04a16fb 1425| try_statement
e04a16fb
AG
1426;
1427
1428empty_statement:
1429 SC_TK
5f1c312a
APB
1430 {
1431 if (flag_extraneous_semicolon)
1432 {
1433 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1434 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1435 }
1436 $$ = empty_stmt_node;
1437 }
e04a16fb
AG
1438;
1439
1440label_decl:
1441 identifier REL_CL_TK
1442 {
1443 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1444 EXPR_WFL_NODE ($1));
e04a16fb
AG
1445 pushlevel (2);
1446 push_labeled_block ($$);
1447 PUSH_LABELED_BLOCK ($$);
1448 }
1449;
1450
1451labeled_statement:
1452 label_decl statement
b635eb2f 1453 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1454| identifier error
1455 {yyerror ("':' expected"); RECOVER;}
1456;
1457
1458labeled_statement_nsi:
1459 label_decl statement_nsi
b635eb2f 1460 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1461;
1462
1463/* We concentrate here a bunch of error handling rules that we couldn't write
1464 earlier, because expression_statement catches a missing ';'. */
1465expression_statement:
1466 statement_expression SC_TK
1467 {
1468 /* We have a statement. Generate a WFL around it so
1469 we can debug it */
1470 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1471 /* We know we have a statement, so set the debug
1472 info to be eventually generate here. */
1473 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1474 }
1475| error SC_TK
1476 {
29f8b718 1477 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1478 DRECOVER (expr_stmt);
1479 }
1480| error OCB_TK
1481 {
29f8b718 1482 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1483 DRECOVER (expr_stmt);
1484 }
1485| error CCB_TK
1486 {
29f8b718 1487 YYNOT_TWICE yyerror ("Invalid expression statement");
e04a16fb
AG
1488 DRECOVER (expr_stmt);
1489 }
1490| this_or_super OP_TK error
1491 {yyerror ("')' expected"); RECOVER;}
1492| this_or_super OP_TK CP_TK error
22eed1e6 1493 {
8119c720 1494 parse_ctor_invocation_error ();
22eed1e6
APB
1495 RECOVER;
1496 }
e04a16fb
AG
1497| this_or_super OP_TK argument_list error
1498 {yyerror ("')' expected"); RECOVER;}
1499| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1500 {
8119c720 1501 parse_ctor_invocation_error ();
22eed1e6
APB
1502 RECOVER;
1503 }
e04a16fb
AG
1504| name DOT_TK SUPER_TK error
1505 {yyerror ("'(' expected"); RECOVER;}
1506| name DOT_TK SUPER_TK OP_TK error
1507 {yyerror ("')' expected"); RECOVER;}
1508| name DOT_TK SUPER_TK OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1511 {yyerror ("';' expected"); RECOVER;}
1512| name DOT_TK SUPER_TK OP_TK CP_TK error
1513 {yyerror ("';' expected"); RECOVER;}
1514;
1515
1516statement_expression:
1517 assignment
1518| pre_increment_expression
e04a16fb 1519| pre_decrement_expression
e04a16fb 1520| post_increment_expression
e04a16fb 1521| post_decrement_expression
e04a16fb
AG
1522| method_invocation
1523| class_instance_creation_expression
e04a16fb
AG
1524;
1525
1526if_then_statement:
1527 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1528 {
1529 $$ = build_if_else_statement ($2.location, $3,
1530 $5, NULL_TREE);
1531 }
e04a16fb
AG
1532| IF_TK error
1533 {yyerror ("'(' expected"); RECOVER;}
1534| IF_TK OP_TK error
1535 {yyerror ("Missing term"); RECOVER;}
1536| IF_TK OP_TK expression error
1537 {yyerror ("')' expected"); RECOVER;}
1538;
1539
1540if_then_else_statement:
1541 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1542 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1543;
1544
1545if_then_else_statement_nsi:
1546 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1547 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1548;
1549
1550switch_statement:
15fdcfe9
PB
1551 switch_expression
1552 {
1553 enter_block ();
1554 }
1555 switch_block
b67d701b 1556 {
15fdcfe9 1557 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1558 I.e. make the last statment also have its own
1559 COMPOUND_EXPR. */
15fdcfe9
PB
1560 maybe_absorb_scoping_blocks ();
1561 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1562 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1563 }
1564;
1565
1566switch_expression:
1567 SWITCH_TK OP_TK expression CP_TK
1568 {
1569 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1570 EXPR_WFL_LINECOL ($$) = $2.location;
1571 }
e04a16fb
AG
1572| SWITCH_TK error
1573 {yyerror ("'(' expected"); RECOVER;}
1574| SWITCH_TK OP_TK error
1575 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1576| SWITCH_TK OP_TK expression CP_TK error
1577 {yyerror ("'{' expected"); RECOVER;}
1578;
1579
f8976021
APB
1580/* Default assignment is there to avoid type node on switch_block
1581 node. */
1582
e04a16fb
AG
1583switch_block:
1584 OCB_TK CCB_TK
f8976021 1585 { $$ = NULL_TREE; }
e04a16fb 1586| OCB_TK switch_labels CCB_TK
f8976021 1587 { $$ = NULL_TREE; }
e04a16fb 1588| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1589 { $$ = NULL_TREE; }
e04a16fb 1590| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1591 { $$ = NULL_TREE; }
e04a16fb
AG
1592;
1593
1594switch_block_statement_groups:
1595 switch_block_statement_group
1596| switch_block_statement_groups switch_block_statement_group
1597;
1598
1599switch_block_statement_group:
15fdcfe9 1600 switch_labels block_statements
e04a16fb
AG
1601;
1602
e04a16fb
AG
1603switch_labels:
1604 switch_label
1605| switch_labels switch_label
1606;
1607
1608switch_label:
1609 CASE_TK constant_expression REL_CL_TK
b67d701b 1610 {
15fdcfe9
PB
1611 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1612 EXPR_WFL_LINECOL (lab) = $1.location;
1613 java_method_add_stmt (current_function_decl, lab);
b67d701b 1614 }
e04a16fb 1615| DEFAULT_TK REL_CL_TK
b67d701b 1616 {
15fdcfe9
PB
1617 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1618 EXPR_WFL_LINECOL (lab) = $1.location;
1619 java_method_add_stmt (current_function_decl, lab);
b67d701b 1620 }
e04a16fb
AG
1621| CASE_TK error
1622 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1623| CASE_TK constant_expression error
1624 {yyerror ("':' expected"); RECOVER;}
1625| DEFAULT_TK error
1626 {yyerror ("':' expected"); RECOVER;}
1627;
1628
1629while_expression:
1630 WHILE_TK OP_TK expression CP_TK
1631 {
1632 tree body = build_loop_body ($2.location, $3, 0);
1633 $$ = build_new_loop (body);
1634 }
1635;
1636
1637while_statement:
1638 while_expression statement
b635eb2f 1639 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1640| WHILE_TK error
1641 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1642| WHILE_TK OP_TK error
1643 {yyerror ("Missing term and ')' expected"); RECOVER;}
1644| WHILE_TK OP_TK expression error
1645 {yyerror ("')' expected"); RECOVER;}
1646;
1647
1648while_statement_nsi:
1649 while_expression statement_nsi
b635eb2f 1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1651;
1652
1653do_statement_begin:
1654 DO_TK
1655 {
1656 tree body = build_loop_body (0, NULL_TREE, 1);
1657 $$ = build_new_loop (body);
1658 }
1659 /* Need error handing here. FIXME */
1660;
1661
1662do_statement:
1663 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1664 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1665;
1666
1667for_statement:
1668 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1669 {
1670 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1671 $3 = build_wfl_node ($3);
1672 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1673 }
e04a16fb
AG
1674| for_begin SC_TK SC_TK for_update CP_TK statement
1675 {
b635eb2f 1676 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1677 /* We have not condition, so we get rid of the EXIT_EXPR */
1678 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1679 empty_stmt_node;
e04a16fb
AG
1680 }
1681| for_begin SC_TK error
1682 {yyerror ("Invalid control expression"); RECOVER;}
1683| for_begin SC_TK expression SC_TK error
1684 {yyerror ("Invalid update expression"); RECOVER;}
1685| for_begin SC_TK SC_TK error
1686 {yyerror ("Invalid update expression"); RECOVER;}
1687;
1688
1689for_statement_nsi:
1690 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1691 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1692| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1693 {
b635eb2f 1694 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1695 /* We have not condition, so we get rid of the EXIT_EXPR */
1696 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1697 empty_stmt_node;
e04a16fb
AG
1698 }
1699;
1700
1701for_header:
1702 FOR_TK OP_TK
1703 {
1704 /* This scope defined for local variable that may be
1705 defined within the scope of the for loop */
1706 enter_block ();
1707 }
1708| FOR_TK error
1709 {yyerror ("'(' expected"); DRECOVER(for_1);}
1710| FOR_TK OP_TK error
1711 {yyerror ("Invalid init statement"); RECOVER;}
1712;
1713
1714for_begin:
1715 for_header for_init
1716 {
1717 /* We now declare the loop body. The loop is
1718 declared as a for loop. */
1719 tree body = build_loop_body (0, NULL_TREE, 0);
1720 $$ = build_new_loop (body);
c2952b01 1721 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1722 /* The loop is added to the current block the for
1723 statement is defined within */
1724 java_method_add_stmt (current_function_decl, $$);
1725 }
1726;
1727for_init: /* Can be empty */
9bbc7d9f 1728 { $$ = empty_stmt_node; }
e04a16fb
AG
1729| statement_expression_list
1730 {
1731 /* Init statement recorded within the previously
1732 defined block scope */
1733 $$ = java_method_add_stmt (current_function_decl, $1);
1734 }
1735| local_variable_declaration
1736 {
1737 /* Local variable are recorded within the previously
1738 defined block scope */
1739 $$ = NULL_TREE;
1740 }
1741| statement_expression_list error
1742 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1743;
1744
1745for_update: /* Can be empty */
9bbc7d9f 1746 {$$ = empty_stmt_node;}
e04a16fb
AG
1747| statement_expression_list
1748 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1749;
1750
1751statement_expression_list:
1752 statement_expression
1753 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1754| statement_expression_list C_TK statement_expression
1755 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1756| statement_expression_list C_TK error
1757 {yyerror ("Missing term"); RECOVER;}
1758;
1759
1760break_statement:
1761 BREAK_TK SC_TK
1762 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1763| BREAK_TK identifier SC_TK
1764 { $$ = build_bc_statement ($1.location, 1, $2); }
1765| BREAK_TK error
1766 {yyerror ("Missing term"); RECOVER;}
1767| BREAK_TK identifier error
1768 {yyerror ("';' expected"); RECOVER;}
1769;
1770
1771continue_statement:
1772 CONTINUE_TK SC_TK
1773 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1774| CONTINUE_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 0, $2); }
1776| CONTINUE_TK error
1777 {yyerror ("Missing term"); RECOVER;}
1778| CONTINUE_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1780;
1781
1782return_statement:
1783 RETURN_TK SC_TK
1784 { $$ = build_return ($1.location, NULL_TREE); }
1785| RETURN_TK expression SC_TK
1786 { $$ = build_return ($1.location, $2); }
1787| RETURN_TK error
1788 {yyerror ("Missing term"); RECOVER;}
1789| RETURN_TK expression error
1790 {yyerror ("';' expected"); RECOVER;}
1791;
1792
1793throw_statement:
1794 THROW_TK expression SC_TK
b9f7e36c
APB
1795 {
1796 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1797 EXPR_WFL_LINECOL ($$) = $1.location;
1798 }
e04a16fb
AG
1799| THROW_TK error
1800 {yyerror ("Missing term"); RECOVER;}
1801| THROW_TK expression error
1802 {yyerror ("';' expected"); RECOVER;}
1803;
1804
1805synchronized_statement:
1806 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1807 {
1808 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1809 EXPR_WFL_LINECOL ($$) =
1810 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1811 }
e04a16fb
AG
1812| synchronized OP_TK expression CP_TK error
1813 {yyerror ("'{' expected"); RECOVER;}
1814| synchronized error
1815 {yyerror ("'(' expected"); RECOVER;}
1816| synchronized OP_TK error CP_TK
1817 {yyerror ("Missing term"); RECOVER;}
1818| synchronized OP_TK error
1819 {yyerror ("Missing term"); RECOVER;}
1820;
1821
b9f7e36c 1822synchronized:
efa0a23f 1823 modifiers
e04a16fb 1824 {
781b0558
KG
1825 check_modifiers (
1826 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1827 $1, ACC_SYNCHRONIZED);
1828 if ($1 != ACC_SYNCHRONIZED)
1829 MODIFIER_WFL (SYNCHRONIZED_TK) =
1830 build_wfl_node (NULL_TREE);
e04a16fb
AG
1831 }
1832;
1833
1834try_statement:
1835 TRY_TK block catches
a7d8d81f 1836 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1837| TRY_TK block finally
a7d8d81f 1838 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1839| TRY_TK block catches finally
2aa11e97
APB
1840 { $$ = build_try_finally_statement
1841 ($1.location, build_try_statement ($1.location,
1842 $2, $3), $4);
1843 }
e04a16fb
AG
1844| TRY_TK error
1845 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1846;
1847
1848catches:
1849 catch_clause
1850| catches catch_clause
b67d701b
PB
1851 {
1852 TREE_CHAIN ($2) = $1;
1853 $$ = $2;
1854 }
e04a16fb
AG
1855;
1856
1857catch_clause:
b67d701b
PB
1858 catch_clause_parameter block
1859 {
1860 java_method_add_stmt (current_function_decl, $2);
1861 exit_block ();
1862 $$ = $1;
1863 }
1864
1865catch_clause_parameter:
1866 CATCH_TK OP_TK formal_parameter CP_TK
1867 {
1868 /* We add a block to define a scope for
1869 formal_parameter (CCBP). The formal parameter is
1870 declared initialized by the appropriate function
1871 call */
1872 tree ccpb = enter_block ();
1873 tree init = build_assignment (ASSIGN_TK, $2.location,
1874 TREE_PURPOSE ($3),
1875 soft_exceptioninfo_call_node);
1876 declare_local_variables (0, TREE_VALUE ($3),
1877 build_tree_list (TREE_PURPOSE ($3),
1878 init));
1879 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1880 EXPR_WFL_LINECOL ($$) = $1.location;
1881 }
e04a16fb 1882| CATCH_TK error
97f30284 1883 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1884| CATCH_TK OP_TK error
97f30284
APB
1885 {
1886 yyerror ("Missing term or ')' expected");
1887 RECOVER; $$ = NULL_TREE;
1888 }
b67d701b 1889| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1890 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1891;
1892
1893finally:
1894 FINALLY_TK block
a7d8d81f 1895 { $$ = $2; }
e04a16fb
AG
1896| FINALLY_TK error
1897 {yyerror ("'{' expected"); RECOVER; }
1898;
1899
1900/* 19.12 Production from 15: Expressions */
1901primary:
1902 primary_no_new_array
1903| array_creation_expression
1904;
1905
1906primary_no_new_array:
1907 literal
1908| THIS_TK
1909 { $$ = build_this ($1.location); }
1910| OP_TK expression CP_TK
1911 {$$ = $2;}
1912| class_instance_creation_expression
1913| field_access
1914| method_invocation
1915| array_access
c2952b01 1916| type_literals
e04a16fb
AG
1917 /* Added, JDK1.1 inner classes. Documentation is wrong
1918 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1919 exist. Used name: instead. */
e04a16fb 1920| name DOT_TK THIS_TK
c2952b01
APB
1921 {
1922 tree wfl = build_wfl_node (this_identifier_node);
1923 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1924 }
e04a16fb
AG
1925| OP_TK expression error
1926 {yyerror ("')' expected"); RECOVER;}
1927| name DOT_TK error
1928 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1929| primitive_type DOT_TK error
1930 {yyerror ("'class' expected" ); RECOVER;}
1931| VOID_TK DOT_TK error
1932 {yyerror ("'class' expected" ); RECOVER;}
1933;
1934
c2952b01
APB
1935/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1936 broke the rule down a bit. */
1937
1938array_type_literal:
1939 primitive_type OSB_TK CSB_TK
1940 {
1941 $$ = build_java_array_type ($1, -1);
1942 CLASS_LOADED_P ($$) = 1;
1943 }
1944| name OSB_TK CSB_TK
1945 { $$ = build_unresolved_array_type ($1); }
1946/* This triggers two reduce/reduce conflict between array_type_literal and
1947 dims. FIXME.
1948| array_type OSB_TK CSB_TK
1949 { $$ = build_unresolved_array_type ($1); }
1950*/
1951;
1952
1953type_literals:
1954 name DOT_TK CLASS_TK
1955 { $$ = build_incomplete_class_ref ($2.location, $1); }
1956| array_type_literal DOT_TK CLASS_TK
1957 { $$ = build_incomplete_class_ref ($2.location, $1); }
1958| primitive_type DOT_TK CLASS_TK
1959 { $$ = build_class_ref ($1); }
1960| VOID_TK DOT_TK CLASS_TK
1961 { $$ = build_class_ref (void_type_node); }
1962;
1963
e04a16fb
AG
1964class_instance_creation_expression:
1965 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1966 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1967| NEW_TK class_type OP_TK CP_TK
b67d701b 1968 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1969| anonymous_class_creation
e04a16fb
AG
1970 /* Added, JDK1.1 inner classes, modified to use name or
1971 primary instead of primary solely which couldn't work in
1972 all situations. */
1973| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1974 {
1975 tree ctor = build_new_invocation ($2, NULL_TREE);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1978 }
e04a16fb
AG
1979| something_dot_new identifier OP_TK CP_TK class_body
1980| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1981 {
1982 tree ctor = build_new_invocation ($2, $4);
1983 $$ = make_qualified_primary ($1, ctor,
1984 EXPR_WFL_LINECOL ($1));
1985 }
e04a16fb
AG
1986| something_dot_new identifier OP_TK argument_list CP_TK class_body
1987| NEW_TK error SC_TK
1988 {yyerror ("'(' expected"); DRECOVER(new_1);}
1989| NEW_TK class_type error
1990 {yyerror ("'(' expected"); RECOVER;}
1991| NEW_TK class_type OP_TK error
1992 {yyerror ("')' or term expected"); RECOVER;}
1993| NEW_TK class_type OP_TK argument_list error
1994 {yyerror ("')' expected"); RECOVER;}
1995| something_dot_new error
1996 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1997| something_dot_new identifier error
1998 {yyerror ("'(' expected"); RECOVER;}
1999;
2000
c2952b01
APB
2001/* Created after JDK1.1 rules originally added to
2002 class_instance_creation_expression, but modified to use
2003 'class_type' instead of 'TypeName' (type_name) which is mentionned
2004 in the documentation but doesn't exist. */
2005
2006anonymous_class_creation:
2007 NEW_TK class_type OP_TK argument_list CP_TK
2008 { create_anonymous_class ($1.location, $2); }
2009 class_body
2010 {
2011 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2012 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2013
2014 end_class_declaration (1);
2015
2016 /* Now we can craft the new expression */
2017 $$ = build_new_invocation (id, $4);
2018
2019 /* Note that we can't possibly be here if
2020 `class_type' is an interface (in which case the
2021 anonymous class extends Object and implements
2022 `class_type', hence its constructor can't have
2023 arguments.) */
2024
2025 /* Otherwise, the innerclass must feature a
2026 constructor matching `argument_list'. Anonymous
2027 classes are a bit special: it's impossible to
2028 define constructor for them, hence constructors
2029 must be generated following the hints provided by
2030 the `new' expression. Whether a super constructor
2031 of that nature exists or not is to be verified
2032 later on in verify_constructor_super.
2033
2034 It's during the expansion of a `new' statement
2035 refering to an anonymous class that a ctor will
2036 be generated for the anonymous class, with the
2037 right arguments. */
2038
2039 }
2040| NEW_TK class_type OP_TK CP_TK
2041 { create_anonymous_class ($1.location, $2); }
2042 class_body
2043 {
2044 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2045 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2046
2047 end_class_declaration (1);
2048
2049 /* Now we can craft the new expression. The
2050 statement doesn't need to be remember so that a
2051 constructor can be generated, since its signature
2052 is already known. */
2053 $$ = build_new_invocation (id, NULL_TREE);
2054 }
2055;
2056
e04a16fb
AG
2057something_dot_new: /* Added, not part of the specs. */
2058 name DOT_TK NEW_TK
c2952b01 2059 { $$ = $1; }
e04a16fb 2060| primary DOT_TK NEW_TK
c2952b01 2061 { $$ = $1; }
e04a16fb
AG
2062;
2063
2064argument_list:
2065 expression
2066 {
2067 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2068 ctxp->formal_parameter_number = 1;
2069 }
2070| argument_list C_TK expression
2071 {
2072 ctxp->formal_parameter_number += 1;
2073 $$ = tree_cons (NULL_TREE, $3, $1);
2074 }
2075| argument_list C_TK error
2076 {yyerror ("Missing term"); RECOVER;}
2077;
2078
2079array_creation_expression:
2080 NEW_TK primitive_type dim_exprs
2081 { $$ = build_newarray_node ($2, $3, 0); }
2082| NEW_TK class_or_interface_type dim_exprs
2083 { $$ = build_newarray_node ($2, $3, 0); }
2084| NEW_TK primitive_type dim_exprs dims
ba179f9f 2085 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2086| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2087 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2088 /* Added, JDK1.1 anonymous array. Initial documentation rule
2089 modified */
2090| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2091 {
2092 char *sig;
2093 while (CURRENT_OSB (ctxp)--)
2094 obstack_1grow (&temporary_obstack, '[');
2095 sig = obstack_finish (&temporary_obstack);
2096 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2097 $2, get_identifier (sig), $4);
2098 }
e04a16fb 2099| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2100 {
2101 tree type = $2;
2102 while (CURRENT_OSB (ctxp)--)
2103 type = build_java_array_type (type, -1);
2104 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2105 build_pointer_type (type), NULL_TREE, $4);
2106 }
e04a16fb
AG
2107| NEW_TK error CSB_TK
2108 {yyerror ("'[' expected"); DRECOVER ("]");}
2109| NEW_TK error OSB_TK
2110 {yyerror ("']' expected"); RECOVER;}
2111;
2112
2113dim_exprs:
2114 dim_expr
2115 { $$ = build_tree_list (NULL_TREE, $1); }
2116| dim_exprs dim_expr
2117 { $$ = tree_cons (NULL_TREE, $2, $$); }
2118;
2119
2120dim_expr:
2121 OSB_TK expression CSB_TK
2122 {
9a7ab4b3
APB
2123 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2124 {
2125 $2 = build_wfl_node ($2);
2126 TREE_TYPE ($2) = NULL_TREE;
2127 }
e04a16fb
AG
2128 EXPR_WFL_LINECOL ($2) = $1.location;
2129 $$ = $2;
2130 }
2131| OSB_TK expression error
2132 {yyerror ("']' expected"); RECOVER;}
2133| OSB_TK error
2134 {
2135 yyerror ("Missing term");
2136 yyerror ("']' expected");
2137 RECOVER;
2138 }
2139;
2140
2141dims:
2142 OSB_TK CSB_TK
ba179f9f
APB
2143 {
2144 int allocate = 0;
2145 /* If not initialized, allocate memory for the osb
2146 numbers stack */
2147 if (!ctxp->osb_limit)
2148 {
2149 allocate = ctxp->osb_limit = 32;
2150 ctxp->osb_depth = -1;
2151 }
c2952b01 2152 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2153 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2154 allocate = ctxp->osb_limit << 1;
2155
2156 if (allocate)
2157 {
2158 allocate *= sizeof (int);
2159 if (ctxp->osb_number)
2160 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2161 allocate);
2162 else
2163 ctxp->osb_number = (int *)xmalloc (allocate);
2164 }
2165 ctxp->osb_depth++;
2166 CURRENT_OSB (ctxp) = 1;
2167 }
e04a16fb 2168| dims OSB_TK CSB_TK
ba179f9f 2169 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2170| dims OSB_TK error
2171 { yyerror ("']' expected"); RECOVER;}
2172;
2173
2174field_access:
2175 primary DOT_TK identifier
2176 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2177 /* FIXME - REWRITE TO:
2178 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2179| SUPER_TK DOT_TK identifier
2180 {
6e22695a 2181 tree super_wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
2182 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2183 $$ = make_qualified_name (super_wfl, $3, $2.location);
2184 }
2185| SUPER_TK error
2186 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2187;
2188
2189method_invocation:
2190 name OP_TK CP_TK
2191 { $$ = build_method_invocation ($1, NULL_TREE); }
2192| name OP_TK argument_list CP_TK
2193 { $$ = build_method_invocation ($1, $3); }
2194| primary DOT_TK identifier OP_TK CP_TK
2195 {
22eed1e6
APB
2196 if (TREE_CODE ($1) == THIS_EXPR)
2197 $$ = build_this_super_qualified_invocation
2198 (1, $3, NULL_TREE, 0, $2.location);
2199 else
2200 {
2201 tree invok = build_method_invocation ($3, NULL_TREE);
2202 $$ = make_qualified_primary ($1, invok, $2.location);
2203 }
e04a16fb
AG
2204 }
2205| primary DOT_TK identifier OP_TK argument_list CP_TK
2206 {
22eed1e6
APB
2207 if (TREE_CODE ($1) == THIS_EXPR)
2208 $$ = build_this_super_qualified_invocation
2209 (1, $3, $5, 0, $2.location);
2210 else
2211 {
2212 tree invok = build_method_invocation ($3, $5);
2213 $$ = make_qualified_primary ($1, invok, $2.location);
2214 }
e04a16fb
AG
2215 }
2216| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2217 {
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2220 }
2221| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2222 {
22eed1e6
APB
2223 $$ = build_this_super_qualified_invocation
2224 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2225 }
2226 /* Screws up thing. I let it here until I'm convinced it can
2227 be removed. FIXME
2228| primary DOT_TK error
2229 {yyerror ("'(' expected"); DRECOVER(bad);} */
2230| SUPER_TK DOT_TK error CP_TK
2231 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232| SUPER_TK DOT_TK error DOT_TK
2233 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2234;
2235
2236array_access:
2237 name OSB_TK expression CSB_TK
2238 { $$ = build_array_ref ($2.location, $1, $3); }
2239| primary_no_new_array OSB_TK expression CSB_TK
2240 { $$ = build_array_ref ($2.location, $1, $3); }
2241| name OSB_TK error
2242 {
2243 yyerror ("Missing term and ']' expected");
2244 DRECOVER(array_access);
2245 }
2246| name OSB_TK expression error
2247 {
2248 yyerror ("']' expected");
2249 DRECOVER(array_access);
2250 }
2251| primary_no_new_array OSB_TK error
2252 {
2253 yyerror ("Missing term and ']' expected");
2254 DRECOVER(array_access);
2255 }
2256| primary_no_new_array OSB_TK expression error
2257 {
2258 yyerror ("']' expected");
2259 DRECOVER(array_access);
2260 }
2261;
2262
2263postfix_expression:
2264 primary
2265| name
2266| post_increment_expression
2267| post_decrement_expression
2268;
2269
2270post_increment_expression:
2271 postfix_expression INCR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2273;
2274
2275post_decrement_expression:
2276 postfix_expression DECR_TK
2277 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2278;
2279
2280unary_expression:
2281 pre_increment_expression
2282| pre_decrement_expression
2283| PLUS_TK unary_expression
2284 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285| MINUS_TK unary_expression
2286 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287| unary_expression_not_plus_minus
2288| PLUS_TK error
2289 {yyerror ("Missing term"); RECOVER}
2290| MINUS_TK error
2291 {yyerror ("Missing term"); RECOVER}
2292;
2293
2294pre_increment_expression:
2295 INCR_TK unary_expression
2296 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2297| INCR_TK error
2298 {yyerror ("Missing term"); RECOVER}
2299;
2300
2301pre_decrement_expression:
2302 DECR_TK unary_expression
2303 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2304| DECR_TK error
2305 {yyerror ("Missing term"); RECOVER}
2306;
2307
2308unary_expression_not_plus_minus:
2309 postfix_expression
2310| NOT_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312| NEG_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2314| cast_expression
2315| NOT_TK error
2316 {yyerror ("Missing term"); RECOVER}
2317| NEG_TK error
2318 {yyerror ("Missing term"); RECOVER}
2319;
2320
2321cast_expression: /* Error handling here is potentially weak */
2322 OP_TK primitive_type dims CP_TK unary_expression
2323 {
2324 tree type = $2;
ba179f9f 2325 while (CURRENT_OSB (ctxp)--)
e04a16fb 2326 type = build_java_array_type (type, -1);
ba179f9f 2327 ctxp->osb_depth--;
e04a16fb
AG
2328 $$ = build_cast ($1.location, type, $5);
2329 }
2330| OP_TK primitive_type CP_TK unary_expression
2331 { $$ = build_cast ($1.location, $2, $4); }
2332| OP_TK expression CP_TK unary_expression_not_plus_minus
2333 { $$ = build_cast ($1.location, $2, $4); }
2334| OP_TK name dims CP_TK unary_expression_not_plus_minus
2335 {
49f48c71 2336 const char *ptr;
ba179f9f 2337 while (CURRENT_OSB (ctxp)--)
e04a16fb 2338 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2339 ctxp->osb_depth--;
e04a16fb
AG
2340 obstack_grow0 (&temporary_obstack,
2341 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2342 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2343 ptr = obstack_finish (&temporary_obstack);
2344 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2345 $$ = build_cast ($1.location, $2, $5);
2346 }
2347| OP_TK primitive_type OSB_TK error
2348 {yyerror ("']' expected, invalid type expression");}
2349| OP_TK error
2350 {
29f8b718 2351 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
e04a16fb
AG
2352 RECOVER;
2353 }
2354| OP_TK primitive_type dims CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356| OP_TK primitive_type CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358| OP_TK name dims CP_TK error
2359 {yyerror ("Missing term"); RECOVER;}
2360;
2361
2362multiplicative_expression:
2363 unary_expression
2364| multiplicative_expression MULT_TK unary_expression
2365 {
2366 $$ = build_binop (BINOP_LOOKUP ($2.token),
2367 $2.location, $1, $3);
2368 }
2369| multiplicative_expression DIV_TK unary_expression
2370 {
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372 $1, $3);
2373 }
2374| multiplicative_expression REM_TK unary_expression
2375 {
2376 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2377 $1, $3);
2378 }
2379| multiplicative_expression MULT_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381| multiplicative_expression DIV_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383| multiplicative_expression REM_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2385;
2386
2387additive_expression:
2388 multiplicative_expression
2389| additive_expression PLUS_TK multiplicative_expression
2390 {
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2392 $1, $3);
2393 }
2394| additive_expression MINUS_TK multiplicative_expression
2395 {
2396 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2397 $1, $3);
2398 }
2399| additive_expression PLUS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401| additive_expression MINUS_TK error
2402 {yyerror ("Missing term"); RECOVER;}
2403;
2404
2405shift_expression:
2406 additive_expression
2407| shift_expression LS_TK additive_expression
2408 {
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 $1, $3);
2411 }
2412| shift_expression SRS_TK additive_expression
2413 {
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 $1, $3);
2416 }
2417| shift_expression ZRS_TK additive_expression
2418 {
2419 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2420 $1, $3);
2421 }
2422| shift_expression LS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424| shift_expression SRS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426| shift_expression ZRS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428;
2429
2430relational_expression:
2431 shift_expression
2432| relational_expression LT_TK shift_expression
2433 {
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435 $1, $3);
2436 }
2437| relational_expression GT_TK shift_expression
2438 {
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440 $1, $3);
2441 }
2442| relational_expression LTE_TK shift_expression
2443 {
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445 $1, $3);
2446 }
2447| relational_expression GTE_TK shift_expression
2448 {
2449 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2450 $1, $3);
2451 }
2452| relational_expression INSTANCEOF_TK reference_type
5e942c50 2453 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2454| relational_expression LT_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456| relational_expression GT_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458| relational_expression LTE_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460| relational_expression GTE_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2462| relational_expression INSTANCEOF_TK error
2463 {yyerror ("Invalid reference type"); RECOVER;}
2464;
2465
2466equality_expression:
2467 relational_expression
2468| equality_expression EQ_TK relational_expression
2469 {
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2471 $1, $3);
2472 }
2473| equality_expression NEQ_TK relational_expression
2474 {
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2476 $1, $3);
2477 }
2478| equality_expression EQ_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2480| equality_expression NEQ_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482;
2483
2484and_expression:
2485 equality_expression
2486| and_expression AND_TK equality_expression
2487 {
2488 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2489 $1, $3);
2490 }
2491| and_expression AND_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2493;
2494
2495exclusive_or_expression:
2496 and_expression
2497| exclusive_or_expression XOR_TK and_expression
2498 {
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 $1, $3);
2501 }
2502| exclusive_or_expression XOR_TK error
2503 {yyerror ("Missing term"); RECOVER;}
2504;
2505
2506inclusive_or_expression:
2507 exclusive_or_expression
2508| inclusive_or_expression OR_TK exclusive_or_expression
2509 {
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511 $1, $3);
2512 }
2513| inclusive_or_expression OR_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2515;
2516
2517conditional_and_expression:
2518 inclusive_or_expression
2519| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2520 {
2521 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2522 $1, $3);
2523 }
2524| conditional_and_expression BOOL_AND_TK error
2525 {yyerror ("Missing term"); RECOVER;}
2526;
2527
2528conditional_or_expression:
2529 conditional_and_expression
2530| conditional_or_expression BOOL_OR_TK conditional_and_expression
2531 {
2532 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2533 $1, $3);
2534 }
2535| conditional_or_expression BOOL_OR_TK error
2536 {yyerror ("Missing term"); RECOVER;}
2537;
2538
2539conditional_expression: /* Error handling here is weak */
2540 conditional_or_expression
2541| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2542 {
2543 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2544 EXPR_WFL_LINECOL ($$) = $2.location;
2545 }
e04a16fb
AG
2546| conditional_or_expression REL_QM_TK REL_CL_TK error
2547 {
2548 YYERROR_NOW;
2549 yyerror ("Missing term");
2550 DRECOVER (1);
2551 }
2552| conditional_or_expression REL_QM_TK error
2553 {yyerror ("Missing term"); DRECOVER (2);}
2554| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2555 {yyerror ("Missing term"); DRECOVER (3);}
2556;
2557
2558assignment_expression:
2559 conditional_expression
2560| assignment
2561;
2562
2563assignment:
2564 left_hand_side assignment_operator assignment_expression
2565 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2566| left_hand_side assignment_operator error
2567 {
29f8b718 2568 YYNOT_TWICE yyerror ("Missing term");
e04a16fb
AG
2569 DRECOVER (assign);
2570 }
2571;
2572
2573left_hand_side:
2574 name
2575| field_access
2576| array_access
2577;
2578
2579assignment_operator:
2580 ASSIGN_ANY_TK
2581| ASSIGN_TK
2582;
2583
2584expression:
2585 assignment_expression
2586;
2587
2588constant_expression:
2589 expression
2590;
2591
2592%%
2593\f
2594
c2952b01
APB
2595/* This section of the code deal with save/restoring parser contexts.
2596 Add mode documentation here. FIXME */
e04a16fb 2597
c2952b01
APB
2598/* Helper function. Create a new parser context. With
2599 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2600 context is copied, otherwise, the new context is zeroed. The newly
2601 created context becomes the current one. */
e04a16fb 2602
c2952b01
APB
2603static void
2604create_new_parser_context (copy_from_previous)
2605 int copy_from_previous;
e04a16fb 2606{
c2952b01 2607 struct parser_ctxt *new;
e04a16fb 2608
c2952b01
APB
2609 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2610 if (copy_from_previous)
2611 {
2612 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2613 new->saved_data_ctx = 1;
2614 }
2615 else
2616 bzero ((PTR) new, sizeof (struct parser_ctxt));
2617
e04a16fb
AG
2618 new->next = ctxp;
2619 ctxp = new;
c2952b01
APB
2620}
2621
2622/* Create a new parser context and make it the current one. */
2623
2624void
2625java_push_parser_context ()
2626{
2627 create_new_parser_context (0);
e04a16fb 2628 if (ctxp->next)
5e942c50
APB
2629 {
2630 ctxp->incomplete_class = ctxp->next->incomplete_class;
2631 ctxp->gclass_list = ctxp->next->gclass_list;
2632 }
e04a16fb
AG
2633}
2634
c2952b01
APB
2635void
2636java_pop_parser_context (generate)
2637 int generate;
2638{
2639 tree current;
2640 struct parser_ctxt *toFree, *next;
2641
2642 if (!ctxp)
2643 return;
2644
2645 toFree = ctxp;
2646 next = ctxp->next;
2647 if (next)
2648 {
2649 next->incomplete_class = ctxp->incomplete_class;
2650 next->gclass_list = ctxp->gclass_list;
2651 lineno = ctxp->lineno;
19e223db 2652 current_class = ctxp->class_type;
c2952b01
APB
2653 }
2654
d19cbcb5
TT
2655 /* If the old and new lexers differ, then free the old one. */
2656 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2657 java_destroy_lexer (ctxp->lexer);
2658
c2952b01
APB
2659 /* Set the single import class file flag to 0 for the current list
2660 of imported things */
2661 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2662 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2663
2664 /* And restore those of the previous context */
2665 if ((ctxp = next)) /* Assignment is really meant here */
2666 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2667 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2668
2669 /* If we pushed a context to parse a class intended to be generated,
2670 we keep it so we can remember the class. What we could actually
2671 do is to just update a list of class names. */
2672 if (generate)
2673 {
2674 toFree->next = ctxp_for_generation;
2675 ctxp_for_generation = toFree;
2676 }
2677 else
2678 free (toFree);
2679}
2680
2681/* Create a parser context for the use of saving some global
2682 variables. */
2683
e04a16fb
AG
2684void
2685java_parser_context_save_global ()
2686{
22eed1e6
APB
2687 if (!ctxp)
2688 {
2689 java_push_parser_context ();
ee07f4f4
APB
2690 ctxp->saved_data_ctx = 1;
2691 }
c2952b01
APB
2692
2693 /* If this context already stores data, create a new one suitable
2694 for data storage. */
ee07f4f4 2695 else if (ctxp->saved_data)
c2952b01
APB
2696 create_new_parser_context (1);
2697
e04a16fb 2698 ctxp->lineno = lineno;
19e223db 2699 ctxp->class_type = current_class;
e04a16fb 2700 ctxp->filename = input_filename;
19e223db 2701 ctxp->function_decl = current_function_decl;
ee07f4f4 2702 ctxp->saved_data = 1;
e04a16fb
AG
2703}
2704
c2952b01
APB
2705/* Restore some global variables from the previous context. Make the
2706 previous context the current one. */
2707
e04a16fb
AG
2708void
2709java_parser_context_restore_global ()
2710{
e04a16fb 2711 lineno = ctxp->lineno;
19e223db 2712 current_class = ctxp->class_type;
e04a16fb 2713 input_filename = ctxp->filename;
19e223db 2714 current_function_decl = ctxp->function_decl;
c2952b01 2715 ctxp->saved_data = 0;
ee07f4f4
APB
2716 if (ctxp->saved_data_ctx)
2717 java_pop_parser_context (0);
e04a16fb
AG
2718}
2719
c2952b01
APB
2720/* Suspend vital data for the current class/function being parsed so
2721 that an other class can be parsed. Used to let local/anonymous
2722 classes be parsed. */
2723
2724static void
2725java_parser_context_suspend ()
e04a16fb 2726{
c2952b01 2727 /* This makes debugging through java_debug_context easier */
3b304f5b 2728 static const char *name = "<inner buffer context>";
e04a16fb 2729
c2952b01
APB
2730 /* Duplicate the previous context, use it to save the globals we're
2731 interested in */
2732 create_new_parser_context (1);
19e223db
MM
2733 ctxp->function_decl = current_function_decl;
2734 ctxp->class_type = current_class;
5e942c50 2735
c2952b01
APB
2736 /* Then create a new context which inherits all data from the
2737 previous one. This will be the new current context */
2738 create_new_parser_context (1);
2739
2740 /* Help debugging */
2741 ctxp->next->filename = name;
2742}
2743
2744/* Resume vital data for the current class/function being parsed so
2745 that an other class can be parsed. Used to let local/anonymous
2746 classes be parsed. The trick is the data storing file position
2747 informations must be restored to their current value, so parsing
2748 can resume as if no context was ever saved. */
2749
2750static void
2751java_parser_context_resume ()
2752{
2753 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2754 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2755 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2756
2757 /* We need to inherit the list of classes to complete/generate */
2758 restored->incomplete_class = old->incomplete_class;
2759 restored->gclass_list = old->gclass_list;
2760 restored->classd_list = old->classd_list;
2761 restored->class_list = old->class_list;
2762
2763 /* Restore the current class and function from the saver */
19e223db
MM
2764 current_class = saver->class_type;
2765 current_function_decl = saver->function_decl;
c2952b01
APB
2766
2767 /* Retrive the restored context */
2768 ctxp = restored;
2769
2770 /* Re-installed the data for the parsing to carry on */
2771 bcopy (&old->marker_begining, &ctxp->marker_begining,
2772 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2773
2774 /* Buffer context can now be discarded */
2775 free (saver);
2776 free (old);
2777}
2778
2779/* Add a new anchor node to which all statement(s) initializing static
2780 and non static initialized upon declaration field(s) will be
2781 linked. */
2782
2783static void
2784java_parser_context_push_initialized_field ()
2785{
2786 tree node;
2787
2788 node = build_tree_list (NULL_TREE, NULL_TREE);
2789 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2790 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2791
2792 node = build_tree_list (NULL_TREE, NULL_TREE);
2793 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2794 CPC_INITIALIZER_LIST (ctxp) = node;
2795
2796 node = build_tree_list (NULL_TREE, NULL_TREE);
2797 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2798 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2799}
2800
2801/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2802 remember them so we can use it to create and populate the finit$
c2952b01
APB
2803 or <clinit> functions. */
2804
2805static void
2806java_parser_context_pop_initialized_field ()
2807{
2808 tree stmts;
2809 tree class_type = TREE_TYPE (GET_CPC ());
2810
2811 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2812 {
c2952b01
APB
2813 stmts = CPC_INITIALIZER_STMT (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2815 if (stmts && !java_error_count)
2816 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2817 }
2818
c2952b01
APB
2819 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2820 {
2821 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2822 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2823 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2824 /* Keep initialization in order to enforce 8.5 */
2825 if (stmts && !java_error_count)
2826 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2827 }
e04a16fb 2828
c2952b01
APB
2829 /* JDK 1.1 instance initializers */
2830 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2831 {
c2952b01
APB
2832 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2833 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2834 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2837 }
c2952b01
APB
2838}
2839
2840static tree
2841reorder_static_initialized (list)
2842 tree list;
2843{
2844 /* We have to keep things in order. The alias initializer have to
2845 come first, then the initialized regular field, in reverse to
2846 keep them in lexical order. */
2847 tree marker, previous = NULL_TREE;
2848 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2849 if (TREE_CODE (marker) == TREE_LIST
2850 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2851 break;
2852
2853 /* No static initialized, the list is fine as is */
2854 if (!previous)
2855 list = TREE_CHAIN (marker);
2856
2857 /* No marker? reverse the whole list */
2858 else if (!marker)
2859 list = nreverse (list);
2860
2861 /* Otherwise, reverse what's after the marker and the new reordered
2862 sublist will replace the marker. */
b351b287 2863 else
c2952b01
APB
2864 {
2865 TREE_CHAIN (previous) = NULL_TREE;
2866 list = nreverse (list);
2867 list = chainon (TREE_CHAIN (marker), list);
2868 }
2869 return list;
e04a16fb
AG
2870}
2871
c2952b01
APB
2872/* Helper functions to dump the parser context stack. */
2873
2874#define TAB_CONTEXT(C) \
2875 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2876
2877static void
2878java_debug_context_do (tab)
2879 int tab;
2880{
ee07f4f4
APB
2881 struct parser_ctxt *copy = ctxp;
2882 while (copy)
2883 {
c2952b01 2884 TAB_CONTEXT (tab);
ee07f4f4 2885 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2886 TAB_CONTEXT (tab);
ee07f4f4 2887 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2888 TAB_CONTEXT (tab);
2889 fprintf (stderr, "lineno: %d\n", copy->lineno);
2890 TAB_CONTEXT (tab);
ee07f4f4
APB
2891 fprintf (stderr, "package: %s\n",
2892 (copy->package ?
2893 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2894 TAB_CONTEXT (tab);
ee07f4f4 2895 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2896 TAB_CONTEXT (tab);
ee07f4f4
APB
2897 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2898 copy = copy->next;
2899 tab += 2;
2900 }
ee07f4f4
APB
2901}
2902
c2952b01
APB
2903/* Dump the stacked up parser contexts. Intended to be called from a
2904 debugger. */
2905
ee07f4f4
APB
2906void
2907java_debug_context ()
2908{
2909 java_debug_context_do (0);
2910}
2911
c2952b01
APB
2912\f
2913
2914/* Flag for the error report routine to issue the error the first time
2915 it's called (overriding the default behavior which is to drop the
2916 first invocation and honor the second one, taking advantage of a
2917 richer context. */
2918static int force_error = 0;
ee07f4f4 2919
8119c720
APB
2920/* Reporting an constructor invocation error. */
2921static void
2922parse_ctor_invocation_error ()
2923{
2924 if (DECL_CONSTRUCTOR_P (current_function_decl))
2925 yyerror ("Constructor invocation must be first thing in a constructor");
2926 else
2927 yyerror ("Only constructors can invoke constructors");
2928}
2929
2930/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2931
2932static tree
2933parse_jdk1_1_error (msg)
49f48c71 2934 const char *msg;
b67d701b
PB
2935{
2936 sorry (": `%s' JDK1.1(TM) feature", msg);
2937 java_error_count++;
9bbc7d9f 2938 return empty_stmt_node;
b67d701b
PB
2939}
2940
e04a16fb
AG
2941static int do_warning = 0;
2942
2943void
2944yyerror (msg)
49f48c71 2945 const char *msg;
e04a16fb
AG
2946{
2947 static java_lc elc;
2948 static int prev_lineno;
49f48c71 2949 static const char *prev_msg;
e04a16fb 2950
0a2138e2 2951 int save_lineno;
e04a16fb
AG
2952 char *remainder, *code_from_source;
2953 extern struct obstack temporary_obstack;
2954
2955 if (!force_error && prev_lineno == lineno)
2956 return;
2957
2958 /* Save current error location but report latter, when the context is
2959 richer. */
2960 if (ctxp->java_error_flag == 0)
2961 {
2962 ctxp->java_error_flag = 1;
2963 elc = ctxp->elc;
2964 /* Do something to use the previous line if we're reaching the
2965 end of the file... */
2966#ifdef VERBOSE_SKELETON
2967 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2968#endif
2969 return;
2970 }
2971
2972 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2973 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2974 return;
2975
2976 ctxp->java_error_flag = 0;
2977 if (do_warning)
2978 java_warning_count++;
2979 else
2980 java_error_count++;
2981
807bc1db 2982 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2983 {
2984 elc.col = ctxp->p_line->char_col-1;
2985 elc.line = ctxp->p_line->lineno;
2986 }
2987
2988 save_lineno = lineno;
2989 prev_lineno = lineno = elc.line;
2990 prev_msg = msg;
2991
2992 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2993 obstack_grow0 (&temporary_obstack,
2994 code_from_source, strlen (code_from_source));
2995 remainder = obstack_finish (&temporary_obstack);
2996 if (do_warning)
2997 warning ("%s.\n%s", msg, remainder);
2998 else
2999 error ("%s.\n%s", msg, remainder);
3000
3001 /* This allow us to cheaply avoid an extra 'Invalid expression
3002 statement' error report when errors have been already reported on
3003 the same line. This occurs when we report an error but don't have
3004 a synchronization point other than ';', which
3005 expression_statement is the only one to take care of. */
3006 ctxp->prevent_ese = lineno = save_lineno;
3007}
3008
3009static void
15fdcfe9 3010issue_warning_error_from_context (cl, msg, ap)
5e942c50 3011 tree cl;
d4476be2 3012 const char *msg;
15fdcfe9 3013 va_list ap;
5e942c50 3014{
3b304f5b 3015 const char *saved, *saved_input_filename;
15fdcfe9
PB
3016 char buffer [4096];
3017 vsprintf (buffer, msg, ap);
3018 force_error = 1;
5e942c50
APB
3019
3020 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
3021 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3022 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
3023
3024 /* We have a CL, that's a good reason for using it if it contains data */
3025 saved = ctxp->filename;
3026 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3027 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
3028 saved_input_filename = input_filename;
3029 input_filename = ctxp->filename;
15fdcfe9
PB
3030 java_error (NULL);
3031 java_error (buffer);
5e942c50 3032 ctxp->filename = saved;
1886c9d8 3033 input_filename = saved_input_filename;
15fdcfe9 3034 force_error = 0;
5e942c50
APB
3035}
3036
e04a16fb
AG
3037/* Issue an error message at a current source line CL */
3038
15fdcfe9 3039void
df32d2ce 3040parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3041{
d4476be2 3042#ifndef ANSI_PROTOTYPES
e04a16fb 3043 tree cl;
d4476be2 3044 const char *msg;
e04a16fb 3045#endif
e04a16fb
AG
3046 va_list ap;
3047
3048 VA_START (ap, msg);
d4476be2 3049#ifndef ANSI_PROTOTYPES
e04a16fb 3050 cl = va_arg (ap, tree);
d4476be2 3051 msg = va_arg (ap, const char *);
e04a16fb 3052#endif
15fdcfe9
PB
3053 issue_warning_error_from_context (cl, msg, ap);
3054 va_end (ap);
e04a16fb
AG
3055}
3056
3057/* Issue a warning at a current source line CL */
3058
3059static void
df32d2ce 3060parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3061{
d4476be2 3062#ifndef ANSI_PROTOTYPES
e04a16fb 3063 tree cl;
d4476be2 3064 const char *msg;
e04a16fb 3065#endif
e04a16fb
AG
3066 va_list ap;
3067
3068 VA_START (ap, msg);
d4476be2 3069#ifndef ANSI_PROTOTYPES
e04a16fb 3070 cl = va_arg (ap, tree);
d4476be2 3071 msg = va_arg (ap, const char *);
e04a16fb 3072#endif
e04a16fb 3073
c877974e 3074 force_error = do_warning = 1;
15fdcfe9 3075 issue_warning_error_from_context (cl, msg, ap);
c877974e 3076 do_warning = force_error = 0;
15fdcfe9 3077 va_end (ap);
e04a16fb
AG
3078}
3079
82371d41
APB
3080static tree
3081find_expr_with_wfl (node)
3082 tree node;
3083{
3084 while (node)
3085 {
3086 char code;
3087 tree to_return;
3088
3089 switch (TREE_CODE (node))
3090 {
3091 case BLOCK:
c0d87ff6
PB
3092 node = BLOCK_EXPR_BODY (node);
3093 continue;
82371d41
APB
3094
3095 case COMPOUND_EXPR:
3096 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3097 if (to_return)
3098 return to_return;
c0d87ff6
PB
3099 node = TREE_OPERAND (node, 1);
3100 continue;
82371d41
APB
3101
3102 case LOOP_EXPR:
c0d87ff6
PB
3103 node = TREE_OPERAND (node, 0);
3104 continue;
82371d41
APB
3105
3106 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3107 node = TREE_OPERAND (node, 1);
3108 continue;
3109
82371d41
APB
3110 default:
3111 code = TREE_CODE_CLASS (TREE_CODE (node));
3112 if (((code == '1') || (code == '2') || (code == 'e'))
3113 && EXPR_WFL_LINECOL (node))
3114 return node;
ba179f9f 3115 return NULL_TREE;
82371d41
APB
3116 }
3117 }
3118 return NULL_TREE;
3119}
3120
3121/* Issue a missing return statement error. Uses METHOD to figure the
3122 last line of the method the error occurs in. */
3123
3124static void
3125missing_return_error (method)
3126 tree method;
3127{
3128 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3129 parse_error_context (wfl_operator, "Missing return statement");
3130}
3131
3132/* Issue an unreachable statement error. From NODE, find the next
3133 statement to report appropriately. */
3134static void
3135unreachable_stmt_error (node)
3136 tree node;
3137{
3138 /* Browse node to find the next expression node that has a WFL. Use
3139 the location to report the error */
3140 if (TREE_CODE (node) == COMPOUND_EXPR)
3141 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3142 else
3143 node = find_expr_with_wfl (node);
3144
3145 if (node)
3146 {
3147 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3148 parse_error_context (wfl_operator, "Unreachable statement");
3149 }
3150 else
3151 fatal ("Can't get valid statement - unreachable_stmt_error");
3152}
3153
c877974e 3154int
e04a16fb
AG
3155java_report_errors ()
3156{
3157 if (java_error_count)
3158 fprintf (stderr, "%d error%s",
3159 java_error_count, (java_error_count == 1 ? "" : "s"));
3160 if (java_warning_count)
3161 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3162 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3163 if (java_error_count || java_warning_count)
3164 putc ('\n', stderr);
c877974e 3165 return java_error_count;
e04a16fb
AG
3166}
3167
3168static char *
3169java_accstring_lookup (flags)
3170 int flags;
3171{
3172 static char buffer [80];
3173#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3174
3175 /* Access modifier looked-up first for easier report on forbidden
3176 access. */
3177 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3178 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3179 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3180 if (flags & ACC_STATIC) COPY_RETURN ("static");
3181 if (flags & ACC_FINAL) COPY_RETURN ("final");
3182 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3183 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3184 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3185 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3186 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3187 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3188
3189 buffer [0] = '\0';
3190 return buffer;
3191#undef COPY_RETURN
3192}
3193
b67d701b
PB
3194/* Issuing error messages upon redefinition of classes, interfaces or
3195 variables. */
3196
e04a16fb 3197static void
b67d701b 3198classitf_redefinition_error (context, id, decl, cl)
49f48c71 3199 const char *context;
e04a16fb
AG
3200 tree id, decl, cl;
3201{
3202 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3203 context, IDENTIFIER_POINTER (id),
3204 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3205 /* Here we should point out where its redefined. It's a unicode. FIXME */
3206}
3207
b67d701b
PB
3208static void
3209variable_redefinition_error (context, name, type, line)
3210 tree context, name, type;
3211 int line;
3212{
49f48c71 3213 const char *type_name;
b67d701b
PB
3214
3215 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3216 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3217 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3218 else
0a2138e2 3219 type_name = lang_printable_name (type, 0);
b67d701b
PB
3220
3221 parse_error_context (context,
781b0558 3222 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3223 IDENTIFIER_POINTER (name),
3224 type_name, IDENTIFIER_POINTER (name), line);
3225}
3226
c583dd46
APB
3227static tree
3228build_array_from_name (type, type_wfl, name, ret_name)
3229 tree type, type_wfl, name, *ret_name;
3230{
3231 int more_dims = 0;
49f48c71 3232 const char *string;
c583dd46
APB
3233
3234 /* Eventually get more dims */
3235 string = IDENTIFIER_POINTER (name);
3236 while (string [more_dims] == '[')
3237 more_dims++;
3238
3239 /* If we have, then craft a new type for this variable */
3240 if (more_dims)
3241 {
c0d87ff6 3242 name = get_identifier (&string [more_dims]);
c583dd46 3243
34f4db93
APB
3244 /* If we have a pointer, use its type */
3245 if (TREE_CODE (type) == POINTER_TYPE)
3246 type = TREE_TYPE (type);
c583dd46
APB
3247
3248 /* Building the first dimension of a primitive type uses this
3249 function */
3250 if (JPRIMITIVE_TYPE_P (type))
3251 {
3252 type = build_java_array_type (type, -1);
22eed1e6 3253 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3254 more_dims--;
3255 }
3256 /* Otherwise, if we have a WFL for this type, use it (the type
3257 is already an array on an unresolved type, and we just keep
3258 on adding dimensions) */
3259 else if (type_wfl)
3260 type = type_wfl;
3261
3262 /* Add all the dimensions */
3263 while (more_dims--)
3264 type = build_unresolved_array_type (type);
3265
3266 /* The type may have been incomplete in the first place */
3267 if (type_wfl)
3268 type = obtain_incomplete_type (type);
3269 }
3270
c2952b01
APB
3271 if (ret_name)
3272 *ret_name = name;
c583dd46
APB
3273 return type;
3274}
3275
e04a16fb
AG
3276/* Build something that the type identifier resolver will identify as
3277 being an array to an unresolved type. TYPE_WFL is a WFL on a
3278 identifier. */
3279
3280static tree
3281build_unresolved_array_type (type_or_wfl)
3282 tree type_or_wfl;
3283{
49f48c71 3284 const char *ptr;
e04a16fb 3285
1886c9d8 3286 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3287 just create a array type */
3288 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3289 {
3290 tree type = build_java_array_type (type_or_wfl, -1);
3291 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3292 return type;
3293 }
3294
3295 obstack_1grow (&temporary_obstack, '[');
3296 obstack_grow0 (&temporary_obstack,
3297 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3298 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3299 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3300 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3301 return type_or_wfl;
e04a16fb
AG
3302}
3303
e04a16fb
AG
3304static void
3305parser_add_interface (class_decl, interface_decl, wfl)
3306 tree class_decl, interface_decl, wfl;
3307{
3308 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3309 parse_error_context (wfl, "Interface `%s' repeated",
3310 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3311}
3312
3313/* Bulk of common class/interface checks. Return 1 if an error was
3314 encountered. TAG is 0 for a class, 1 for an interface. */
3315
3316static int
3317check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3318 int is_interface, flags;
3319 tree raw_name, qualified_name, decl, cl;
3320{
3321 tree node;
c2952b01
APB
3322 int sca = 0; /* Static class allowed */
3323 int icaf = 0; /* Inner class allowed flags */
3324 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3325
3326 if (!quiet_flag)
c2952b01
APB
3327 fprintf (stderr, " %s%s %s",
3328 (CPC_INNER_P () ? "inner" : ""),
3329 (is_interface ? "interface" : "class"),
e04a16fb
AG
3330 IDENTIFIER_POINTER (qualified_name));
3331
3332 /* Scope of an interface/class type name:
3333 - Can't be imported by a single type import
3334 - Can't already exists in the package */
3335 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3336 && (node = find_name_in_single_imports (raw_name))
3337 && !CPC_INNER_P ())
e04a16fb
AG
3338 {
3339 parse_error_context
3340 (cl, "%s name `%s' clashes with imported type `%s'",
3341 (is_interface ? "Interface" : "Class"),
3342 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3343 return 1;
3344 }
3345 if (decl && CLASS_COMPLETE_P (decl))
3346 {
b67d701b
PB
3347 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3348 qualified_name, decl, cl);
e04a16fb
AG
3349 return 1;
3350 }
3351
c2952b01
APB
3352 if (check_inner_class_redefinition (raw_name, cl))
3353 return 1;
3354
3355 /* If public, file name should match class/interface name, except
3356 when dealing with an inner class */
3357 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3358 {
49f48c71 3359 const char *f;
e04a16fb
AG
3360
3361 /* Contains OS dependent assumption on path separator. FIXME */
3362 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3363 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3364 f--)
3365 ;
847fe791 3366 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3367 f++;
3368 if (strncmp (IDENTIFIER_POINTER (raw_name),
3369 f , IDENTIFIER_LENGTH (raw_name)) ||
3370 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3371 parse_error_context
3372 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3373 (is_interface ? "interface" : "class"),
3374 IDENTIFIER_POINTER (qualified_name),
3375 IDENTIFIER_POINTER (raw_name));
3376 }
3377
c2952b01
APB
3378 /* Static classes can be declared only in top level classes. Note:
3379 once static, a inner class is a top level class. */
3380 if (flags & ACC_STATIC)
3381 {
3382 /* Catch the specific error of declaring an class inner class
3383 with no toplevel enclosing class. Prevent check_modifiers from
3384 complaining a second time */
3385 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3386 {
3387 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3388 IDENTIFIER_POINTER (qualified_name));
3389 sca = ACC_STATIC;
3390 }
3391 /* Else, in the context of a top-level class declaration, let
3392 `check_modifiers' do its job, otherwise, give it a go */
3393 else
3394 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3395 }
3396
a40d21da 3397 /* Inner classes can be declared private or protected
c2952b01
APB
3398 within their enclosing classes. */
3399 if (CPC_INNER_P ())
3400 {
3401 /* A class which is local to a block can't be public, private,
3402 protected or static. But it is created final, so allow this
3403 one. */
3404 if (current_function_decl)
3405 icaf = sca = uaaf = ACC_FINAL;
3406 else
3407 {
3408 check_modifiers_consistency (flags);
3409 icaf = ACC_PRIVATE|ACC_PROTECTED;
3410 }
3411 }
3412
a40d21da
APB
3413 if (is_interface)
3414 {
3415 if (CPC_INNER_P ())
3416 uaaf = INTERFACE_INNER_MODIFIERS;
3417 else
3418 uaaf = INTERFACE_MODIFIERS;
3419
3420 check_modifiers ("Illegal modifier `%s' for interface declaration",
3421 flags, uaaf);
3422 }
2884c41e 3423 else
a40d21da
APB
3424 check_modifiers ((current_function_decl ?
3425 "Illegal modifier `%s' for local class declaration" :
3426 "Illegal modifier `%s' for class declaration"),
c2952b01 3427 flags, uaaf|sca|icaf);
e04a16fb
AG
3428 return 0;
3429}
3430
c2952b01
APB
3431static void
3432make_nested_class_name (cpc_list)
3433 tree cpc_list;
3434{
3435 tree name;
3436
3437 if (!cpc_list)
3438 return;
3439 else
3440 make_nested_class_name (TREE_CHAIN (cpc_list));
3441
3442 /* Pick the qualified name when dealing with the first upmost
3443 enclosing class */
3444 name = (TREE_CHAIN (cpc_list) ?
3445 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3446 obstack_grow (&temporary_obstack,
3447 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3448 /* Why is NO_DOLLAR_IN_LABEL defined? */
3449#if 0
3450#ifdef NO_DOLLAR_IN_LABEL
3451 fatal ("make_nested_class_name: Can't use '$' as a separator "
3452 "for inner classes");
3453#endif
3454#endif
3455 obstack_1grow (&temporary_obstack, '$');
3456}
3457
3458/* Can't redefine a class already defined in an earlier scope. */
3459
3460static int
3461check_inner_class_redefinition (raw_name, cl)
3462 tree raw_name, cl;
3463{
3464 tree scope_list;
3465
3466 for (scope_list = GET_CPC_LIST (); scope_list;
3467 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3468 if (raw_name == GET_CPC_UN_NODE (scope_list))
3469 {
3470 parse_error_context
3471 (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",
3472 IDENTIFIER_POINTER (raw_name));
3473 return 1;
3474 }
3475 return 0;
3476}
3477
3478static tree
3479find_as_inner_class (enclosing, name, cl)
3480 tree enclosing, name, cl;
3481{
3482 tree qual, to_return;
3483 if (!enclosing)
3484 return NULL_TREE;
3485
3486 name = TYPE_NAME (name);
3487
3488 /* First search: within the scope of `enclosing', search for name */
3489 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3490 qual = EXPR_WFL_QUALIFICATION (cl);
3491 else if (cl)
3492 qual = build_tree_list (cl, NULL_TREE);
3493 else
3494 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3495
3496 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3497 return to_return;
3498
3499 /* We're dealing with a qualified name. Try to resolve thing until
3500 we get something that is an enclosing class. */
3501 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3502 {
3503 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3504
0c2b8145
APB
3505 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3506 qual = TREE_CHAIN (qual))
c2952b01
APB
3507 {
3508 acc = merge_qualified_name (acc,
3509 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3510 BUILD_PTR_FROM_NAME (ptr, acc);
3511 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3512 }
3513
3514 /* A NULL qual and a decl means that the search ended
3515 successfully?!? We have to do something then. FIXME */
3516
3517 if (decl)
3518 enclosing = decl;
3519 else
3520 qual = EXPR_WFL_QUALIFICATION (cl);
3521 }
3522 /* Otherwise, create a qual for the other part of the resolution. */
3523 else
3524 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3525
1e12ab9b 3526 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3527}
3528
3529/* We go inside the list of sub classes and try to find a way
3530 through. */
3531
3532static tree
3533find_as_inner_class_do (qual, enclosing)
3534 tree qual, enclosing;
3535{
3536 if (!qual)
3537 return NULL_TREE;
3538
3539 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3540 {
3541 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3542 tree next_enclosing = NULL_TREE;
3543 tree inner_list;
3544
3545 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3546 inner_list; inner_list = TREE_CHAIN (inner_list))
3547 {
3548 if (TREE_VALUE (inner_list) == name_to_match)
3549 {
3550 next_enclosing = TREE_PURPOSE (inner_list);
3551 break;
3552 }
3553 }
3554 enclosing = next_enclosing;
3555 }
3556
3557 return (!qual && enclosing ? enclosing : NULL_TREE);
3558}
3559
3560/* Reach all inner classes and tie their unqualified name to a
3561 DECL. */
3562
3563static void
3564set_nested_class_simple_name_value (outer, set)
3565 tree outer;
3566 int set;
3567{
3568 tree l;
3569
3570 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3571 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3572 TREE_PURPOSE (l) : NULL_TREE);
3573}
3574
3575static void
3576link_nested_class_to_enclosing ()
3577{
3578 if (GET_ENCLOSING_CPC ())
3579 {
3580 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3581 DECL_INNER_CLASS_LIST (enclosing) =
3582 tree_cons (GET_CPC (), GET_CPC_UN (),
3583 DECL_INNER_CLASS_LIST (enclosing));
3584 enclosing = enclosing;
3585 }
3586}
3587
3588static tree
3589maybe_make_nested_class_name (name)
3590 tree name;
3591{
3592 tree id = NULL_TREE;
3593
3594 if (CPC_INNER_P ())
3595 {
3596 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3597 obstack_grow0 (&temporary_obstack,
3598 IDENTIFIER_POINTER (name),
3599 IDENTIFIER_LENGTH (name));
c2952b01
APB
3600 id = get_identifier (obstack_finish (&temporary_obstack));
3601 if (ctxp->package)
3602 QUALIFIED_P (id) = 1;
3603 }
3604 return id;
3605}
3606
3607/* If DECL is NULL, create and push a new DECL, record the current
3608 line CL and do other maintenance things. */
3609
e04a16fb 3610static tree
c2952b01
APB
3611maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3612 tree decl, raw_name, qualified_name, cl;
e04a16fb 3613{
5e942c50 3614 if (!decl)
e04a16fb 3615 decl = push_class (make_class (), qualified_name);
c2952b01 3616
e04a16fb
AG
3617 /* Take care of the file and line business */
3618 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3619 /* If we're emiting xrefs, store the line/col number information */
3620 if (flag_emit_xref)
3621 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3622 else
3623 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3624 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3625 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3626 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3627
c2952b01
APB
3628 PUSH_CPC (decl, raw_name);
3629 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3630
e04a16fb
AG
3631 /* Link the declaration to the already seen ones */
3632 TREE_CHAIN (decl) = ctxp->class_list;
3633 ctxp->class_list = decl;
5e942c50 3634
23a79c61 3635 /* Create a new nodes in the global lists */
5e942c50 3636 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3637 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3638
e04a16fb
AG
3639 /* Install a new dependency list element */
3640 create_jdep_list (ctxp);
3641
3642 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3643 IDENTIFIER_POINTER (qualified_name)));
3644 return decl;
3645}
3646
3647static void
3648add_superinterfaces (decl, interface_list)
3649 tree decl, interface_list;
3650{
3651 tree node;
3652 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3653 takes care of ensuring that:
3654 - This is an accessible interface type,
3655 - Circularity detection.
3656 parser_add_interface is then called. If present but not defined,
3657 the check operation is delayed until the super interface gets
3658 defined. */
3659 for (node = interface_list; node; node = TREE_CHAIN (node))
3660 {
15fdcfe9 3661 tree current = TREE_PURPOSE (node);
5e942c50
APB
3662 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3663 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3664 {
5e942c50
APB
3665 if (!parser_check_super_interface (idecl, decl, current))
3666 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3667 }
3668 else
3669 register_incomplete_type (JDEP_INTERFACE,
3670 current, decl, NULL_TREE);
3671 }
3672}
3673
3674/* Create an interface in pass1 and return its decl. Return the
3675 interface's decl in pass 2. */
3676
3677static tree
3678create_interface (flags, id, super)
3679 int flags;
3680 tree id, super;
3681{
e04a16fb 3682 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3683 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3684 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3685
3686 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3687
3688 /* Basic checks: scope, redefinition, modifiers */
3689 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3690 {
3691 PUSH_ERROR ();
3692 return NULL_TREE;
3693 }
3694
3695 /* Suspend the current parsing context if we're parsing an inner
3696 interface */
3697 if (CPC_INNER_P ())
3698 java_parser_context_suspend ();
3699
3700 /* Push a new context for (static) initialized upon declaration fields */
3701 java_parser_context_push_initialized_field ();
e04a16fb
AG
3702
3703 /* Interface modifiers check
3704 - public/abstract allowed (already done at that point)
3705 - abstract is obsolete (comes first, it's a warning, or should be)
3706 - Can't use twice the same (checked in the modifier rule) */
c877974e 3707 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3708 parse_warning_context
3709 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3710 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3711
3712 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3713 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3714
3715 /* Set super info and mark the class a complete */
2aa11e97 3716 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3717 object_type_node, ctxp->interface_number);
3718 ctxp->interface_number = 0;
3719 CLASS_COMPLETE_P (decl) = 1;
3720 add_superinterfaces (decl, super);
3721
3722 return decl;
3723}
3724
c2952b01
APB
3725/* Anonymous class counter. Will be reset to 1 every time a non
3726 anonymous class gets created. */
3727static int anonymous_class_counter = 1;
3728
3729/* Patch anonymous class CLASS, by either extending or implementing
3730 DEP. */
3731
3732static void
3733patch_anonymous_class (type_decl, class_decl, wfl)
3734 tree type_decl, class_decl, wfl;
3735{
3736 tree class = TREE_TYPE (class_decl);
3737 tree type = TREE_TYPE (type_decl);
3738 tree binfo = TYPE_BINFO (class);
3739
3740 /* If it's an interface, implement it */
3741 if (CLASS_INTERFACE (type_decl))
3742 {
3743 tree s_binfo;
3744 int length;
3745
3746 if (parser_check_super_interface (type_decl, class_decl, wfl))
3747 return;
3748
3749 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3750 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3751 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3752 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3753 /* And add the interface */
3754 parser_add_interface (class_decl, type_decl, wfl);
3755 }
3756 /* Otherwise, it's a type we want to extend */
3757 else
3758 {
3759 if (parser_check_super (type_decl, class_decl, wfl))
3760 return;
3761 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3762 }
3763}
3764
3765static tree
3766create_anonymous_class (location, type_name)
3767 int location;
3768 tree type_name;
3769{
3770 char buffer [80];
3771 tree super = NULL_TREE, itf = NULL_TREE;
3772 tree id, type_decl, class;
3773
3774 /* The unqualified name of the anonymous class. It's just a number. */
3775 sprintf (buffer, "%d", anonymous_class_counter++);
3776 id = build_wfl_node (get_identifier (buffer));
3777 EXPR_WFL_LINECOL (id) = location;
3778
3779 /* We know about the type to extend/implement. We go ahead */
3780 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3781 {
3782 /* Create a class which either implements on extends the designated
3783 class. The class bears an innacessible name. */
3784 if (CLASS_INTERFACE (type_decl))
3785 {
3786 /* It's OK to modify it here. It's been already used and
3787 shouldn't be reused */
3788 ctxp->interface_number = 1;
3789 /* Interfaces should presented as a list of WFLs */
3790 itf = build_tree_list (type_name, NULL_TREE);
3791 }
3792 else
3793 super = type_name;
3794 }
3795
3796 class = create_class (ACC_FINAL, id, super, itf);
3797
3798 /* We didn't know anything about the stuff. We register a dependence. */
3799 if (!type_decl)
3800 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3801
3802 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3803 return class;
3804}
3805
a40d21da 3806/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3807 interface's decl in pass 2. */
3808
3809static tree
3810create_class (flags, id, super, interfaces)
3811 int flags;
3812 tree id, super, interfaces;
3813{
e04a16fb
AG
3814 tree raw_name = EXPR_WFL_NODE (id);
3815 tree class_id, decl;
9ee9b555 3816 tree super_decl_type;
e04a16fb 3817
98a52c2c 3818 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3819 decl = IDENTIFIER_CLASS_VALUE (class_id);
3820 EXPR_WFL_NODE (id) = class_id;
3821
3822 /* Basic check: scope, redefinition, modifiers */
3823 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3824 {
3825 PUSH_ERROR ();
3826 return NULL_TREE;
3827 }
3828
3829 /* Suspend the current parsing context if we're parsing an inner
3830 class or an anonymous class. */
3831 if (CPC_INNER_P ())
3832 java_parser_context_suspend ();
3833 /* Push a new context for (static) initialized upon declaration fields */
3834 java_parser_context_push_initialized_field ();
e04a16fb
AG
3835
3836 /* Class modifier check:
3837 - Allowed modifier (already done at that point)
3838 - abstract AND final forbidden
3839 - Public classes defined in the correct file */
3840 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3841 parse_error_context
3842 (id, "Class `%s' can't be declared both abstract and final",
3843 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3844
3845 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3846 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3847
3848 /* If SUPER exists, use it, otherwise use Object */
3849 if (super)
3850 {
3851 /* Can't extend java.lang.Object */
3852 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3853 {
3854 parse_error_context (id, "Can't extend `java.lang.Object'");
3855 return NULL_TREE;
3856 }
3857
2c3199bc
PB
3858 super_decl_type =
3859 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3860 }
3861 else if (TREE_TYPE (decl) != object_type_node)
3862 super_decl_type = object_type_node;
3863 /* We're defining java.lang.Object */
3864 else
3865 super_decl_type = NULL_TREE;
3866
3867 /* Set super info and mark the class a complete */
3868 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3869 ctxp->interface_number);
3870 ctxp->interface_number = 0;
3871 CLASS_COMPLETE_P (decl) = 1;
3872 add_superinterfaces (decl, interfaces);
3873
c2952b01
APB
3874 /* Add the private this$<n> field, Replicate final locals still in
3875 scope as private final fields mangled like val$<local_name>.
3876 This doesn't not occur for top level (static) inner classes. */
3877 if (PURE_INNER_CLASS_DECL_P (decl))
3878 add_inner_class_fields (decl, current_function_decl);
3879
7f10c2e2
APB
3880 /* If doing xref, store the location at which the inherited class
3881 (if any) was seen. */
3882 if (flag_emit_xref && super)
3883 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3884
5e942c50
APB
3885 /* Eventually sets the @deprecated tag flag */
3886 CHECK_DEPRECATED (decl);
3887
165f37bc
APB
3888 /* Reset the anonymous class counter when declaring non inner classes */
3889 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3890 anonymous_class_counter = 1;
3891
e04a16fb
AG
3892 return decl;
3893}
3894
c2952b01 3895/* End a class declaration: register the statements used to create
c00f0fb2 3896 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3897 parser context if necessary. */
3898
3899static void
3900end_class_declaration (resume)
3901 int resume;
3902{
3903 /* If an error occured, context weren't pushed and won't need to be
3904 popped by a resume. */
3905 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3906
3907 java_parser_context_pop_initialized_field ();
3908 POP_CPC ();
3909 if (resume && no_error_occured)
3910 java_parser_context_resume ();
93220702
APB
3911
3912 /* We're ending a class declaration, this is a good time to reset
3913 the interface cout. Note that might have been already done in
3914 create_interface, but if at that time an inner class was being
3915 dealt with, the interface count was reset in a context created
3916 for the sake of handling inner classes declaration. */
3917 ctxp->interface_number = 0;
c2952b01
APB
3918}
3919
3920static void
3921add_inner_class_fields (class_decl, fct_decl)
3922 tree class_decl;
3923 tree fct_decl;
3924{
3925 tree block, marker, f;
3926
3927 f = add_field (TREE_TYPE (class_decl),
3928 build_current_thisn (TREE_TYPE (class_decl)),
3929 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3930 ACC_PRIVATE);
3931 FIELD_THISN (f) = 1;
3932
3933 if (!fct_decl)
3934 return;
3935
3936 for (block = GET_CURRENT_BLOCK (fct_decl);
3937 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3938 {
3939 tree decl;
3940 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3941 {
1f8f4a0b 3942 tree name, pname;
c2952b01
APB
3943 tree wfl, init, list;
3944
3945 /* Avoid non final arguments. */
3946 if (!LOCAL_FINAL (decl))
3947 continue;
3948
3949 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3950 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
1f8f4a0b
MM
3951 wfl = build_wfl_node (name);
3952 init = build_wfl_node (pname);
c2952b01 3953 /* Build an initialization for the field: it will be
c00f0fb2 3954 initialized by a parameter added to finit$, bearing a
c2952b01 3955 mangled name of the field itself (param$<n>.) The
c00f0fb2 3956 parameter is provided to finit$ by the constructor
c2952b01
APB
3957 invoking it (hence the constructor will also feature a
3958 hidden parameter, set to the value of the outer context
3959 local at the time the inner class is created.)
3960
3961 Note: we take into account all possible locals that can
3962 be accessed by the inner class. It's actually not trivial
3963 to minimize these aliases down to the ones really
3964 used. One way to do that would be to expand all regular
c00f0fb2 3965 methods first, then finit$ to get a picture of what's
c2952b01
APB
3966 used. It works with the exception that we would have to
3967 go back on all constructor invoked in regular methods to
3968 have their invokation reworked (to include the right amount
3969 of alias initializer parameters.)
3970
3971 The only real way around, I think, is a first pass to
3972 identify locals really used in the inner class. We leave
3973 the flag FIELD_LOCAL_ALIAS_USED around for that future
3974 use.
3975
3976 On the other hand, it only affect local inner classes,
c00f0fb2 3977 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3978 unecessary arguments. It's easy for a developper to keep
3979 this number of parameter down by using the `final'
3980 keyword only when necessary. For the time being, we can
3981 issue a warning on unecessary finals. FIXME */
3982 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3983 wfl, init);
3984
3985 /* Register the field. The TREE_LIST holding the part
3986 initialized/initializer will be marked ARG_FINAL_P so
3987 that the created field can be marked
3988 FIELD_LOCAL_ALIAS. */
3989 list = build_tree_list (wfl, init);
3990 ARG_FINAL_P (list) = 1;
3991 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3992 }
3993 }
3994
3995 if (!CPC_INITIALIZER_STMT (ctxp))
3996 return;
3997
3998 /* If we ever registered an alias field, insert and marker to
3999 remeber where the list ends. The second part of the list (the one
4000 featuring initialized fields) so it can be later reversed to
4001 enforce 8.5. The marker will be removed during that operation. */
4002 marker = build_tree_list (NULL_TREE, NULL_TREE);
4003 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4004 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4005}
4006
e04a16fb
AG
4007/* Can't use lookup_field () since we don't want to load the class and
4008 can't set the CLASS_LOADED_P flag */
4009
4010static tree
4011find_field (class, name)
4012 tree class;
4013 tree name;
4014{
4015 tree decl;
4016 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4017 {
4018 if (DECL_NAME (decl) == name)
4019 return decl;
4020 }
4021 return NULL_TREE;
4022}
4023
4024/* Wrap around lookup_field that doesn't potentially upset the value
4025 of CLASS */
4026
4027static tree
4028lookup_field_wrapper (class, name)
4029 tree class, name;
4030{
4031 tree type = class;
9a7ab4b3 4032 tree decl = NULL_TREE;
c877974e 4033 java_parser_context_save_global ();
f2760b27
APB
4034
4035 /* Last chance: if we're within the context of an inner class, we
4036 might be trying to access a local variable defined in an outer
4037 context. We try to look for it now. */
9a7ab4b3 4038 if (INNER_CLASS_TYPE_P (class))
f2760b27 4039 {
9a7ab4b3 4040 tree new_name;
1f8f4a0b 4041 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
9a7ab4b3 4042 decl = lookup_field (&type, new_name);
f2760b27
APB
4043 if (decl && decl != error_mark_node)
4044 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4045 }
9a7ab4b3
APB
4046 if (!decl || decl == error_mark_node)
4047 {
4048 type = class;
4049 decl = lookup_field (&type, name);
4050 }
f2760b27 4051
c877974e 4052 java_parser_context_restore_global ();
93024893 4053 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4054}
4055
4056/* Find duplicate field within the same class declarations and report
c583dd46
APB
4057 the error. Returns 1 if a duplicated field was found, 0
4058 otherwise. */
e04a16fb
AG
4059
4060static int
c583dd46 4061duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4062 tree new_field_name, new_type, cl;
e04a16fb
AG
4063{
4064 /* This might be modified to work with method decl as well */
c2952b01 4065 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4066 if (decl)
4067 {
c2e3db92 4068 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4069 ((TREE_CODE (new_type) == POINTER_TYPE
4070 && TREE_TYPE (new_type) == NULL_TREE) ?
4071 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4072 lang_printable_name (new_type, 1)));
c877974e
APB
4073 /* The type may not have been completed by the time we report
4074 the error */
c2e3db92 4075 char *t2 = xstrdup (purify_type_name
4a5f66c3 4076 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4077 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4078 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4079 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4080 parse_error_context
4081 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4082 t1, IDENTIFIER_POINTER (new_field_name),
4083 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4084 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4085 free (t1);
4086 free (t2);
c583dd46 4087 return 1;
e04a16fb 4088 }
c583dd46 4089 return 0;
e04a16fb
AG
4090}
4091
4092/* Field registration routine. If TYPE doesn't exist, field
4093 declarations are linked to the undefined TYPE dependency list, to
4094 be later resolved in java_complete_class () */
4095
4096static void
4097register_fields (flags, type, variable_list)
4098 int flags;
4099 tree type, variable_list;
4100{
c583dd46 4101 tree current, saved_type;
c2952b01 4102 tree class_type = NULL_TREE;
e04a16fb
AG
4103 int saved_lineno = lineno;
4104 int must_chain = 0;
4105 tree wfl = NULL_TREE;
4106
c2952b01
APB
4107 if (GET_CPC ())
4108 class_type = TREE_TYPE (GET_CPC ());
4109
4110 if (!class_type || class_type == error_mark_node)
4111 return;
4112
e04a16fb
AG
4113 /* If we're adding fields to interfaces, those fields are public,
4114 static, final */
4115 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4116 {
4117 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4118 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4119 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4120 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4121 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4122 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4123 check_modifiers ("Illegal interface member modifier `%s'", flags,
4124 INTERFACE_FIELD_MODIFIERS);
4125 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4126 }
4127
c583dd46
APB
4128 /* Obtain a suitable type for resolution, if necessary */
4129 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4130
4131 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4132 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4133
c583dd46
APB
4134 for (current = variable_list, saved_type = type; current;
4135 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4136 {
c877974e 4137 tree real_type;
c583dd46 4138 tree field_decl;
e04a16fb
AG
4139 tree cl = TREE_PURPOSE (current);
4140 tree init = TREE_VALUE (current);
4141 tree current_name = EXPR_WFL_NODE (cl);
4142
cf1748bf 4143 /* Can't declare non-final static fields in inner classes */
c2952b01 4144 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4145 && !(flags & ACC_FINAL))
c2952b01 4146 parse_error_context
cf1748bf 4147 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4148 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4149 lang_printable_name (class_type, 0));
4150
c583dd46
APB
4151 /* Process NAME, as it may specify extra dimension(s) for it */
4152 type = build_array_from_name (type, wfl, current_name, &current_name);
4153
c583dd46
APB
4154 /* Type adjustment. We may have just readjusted TYPE because
4155 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4156 a reference if we can and don't have one already. Also
4157 change the name if we have an init. */
4158 if (type != saved_type)
4159 {
1886c9d8 4160 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4161 if (init)
4162 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4163 }
e04a16fb 4164
c877974e
APB
4165 real_type = GET_REAL_TYPE (type);
4166 /* Check for redeclarations */
4167 if (duplicate_declaration_error_p (current_name, real_type, cl))
4168 continue;
4169
c583dd46 4170 /* Set lineno to the line the field was found and create a
5e942c50 4171 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4172 if (flag_emit_xref)
4173 lineno = EXPR_WFL_LINECOL (cl);
4174 else
4175 lineno = EXPR_WFL_LINENO (cl);
c877974e 4176 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4177 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4178
4179 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4180 mark the created field FIELD_LOCAL_ALIAS, so that we can
c00f0fb2 4181 hide parameters to this inner class finit$ and constructors. */
c2952b01
APB
4182 if (ARG_FINAL_P (current))
4183 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4184
4185 /* Check if we must chain. */
4186 if (must_chain)
4187 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4188
c583dd46
APB
4189 /* If we have an initialization value tied to the field */
4190 if (init)
4191 {
4192 /* The field is declared static */
e04a16fb 4193 if (flags & ACC_STATIC)
e04a16fb 4194 {
7525cc04
APB
4195 /* We include the field and its initialization part into
4196 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4197 walked, field initializations will be processed and
4198 fields initialized with known constants will be taken
4199 out of <clinit> and have their DECL_INITIAL set
7525cc04 4200 appropriately. */
c2952b01
APB
4201 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4202 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4203 if (TREE_OPERAND (init, 1)
4204 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4205 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4206 }
5e942c50
APB
4207 /* A non-static field declared with an immediate initialization is
4208 to be initialized in <init>, if any. This field is remembered
4209 to be processed at the time of the generation of <init>. */
c583dd46
APB
4210 else
4211 {
c2952b01
APB
4212 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4213 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4214 }
5b09b33e 4215 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4216 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4217 }
4218 }
4219 lineno = saved_lineno;
4220}
4221
c00f0fb2
APB
4222/* Generate finit$, using the list of initialized fields to populate
4223 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4224 one(s) used to initialized the field(s) caching outer context
4225 local(s). */
22eed1e6 4226
c2952b01
APB
4227static tree
4228generate_finit (class_type)
4229 tree class_type;
22eed1e6 4230{
c2952b01
APB
4231 int count = 0;
4232 tree list = TYPE_FINIT_STMT_LIST (class_type);
4233 tree mdecl, current, parms;
4234
4235 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4236 class_type, NULL_TREE,
4237 &count);
4238 CRAFTED_PARAM_LIST_FIXUP (parms);
4239 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4240 finit_identifier_node, parms);
4241 fix_method_argument_names (parms, mdecl);
4242 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4243 mdecl, NULL_TREE);
4244 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4245 start_artificial_method_body (mdecl);
4246
c2952b01 4247 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4248 java_method_add_stmt (mdecl,
4249 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4250 current));
22eed1e6 4251 end_artificial_method_body (mdecl);
c2952b01 4252 return mdecl;
22eed1e6
APB
4253}
4254
e04a16fb 4255static void
c2952b01
APB
4256add_instance_initializer (mdecl)
4257 tree mdecl;
e04a16fb 4258{
c2952b01
APB
4259 tree current;
4260 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4261 tree compound = NULL_TREE;
e04a16fb 4262
c2952b01 4263 if (stmt_list)
e04a16fb 4264 {
c2952b01
APB
4265 for (current = stmt_list; current; current = TREE_CHAIN (current))
4266 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4267
c2952b01
APB
4268 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4269 NULL_TREE, compound));
4270 }
e04a16fb
AG
4271}
4272
4273/* Shared accros method_declarator and method_header to remember the
4274 patch stage that was reached during the declaration of the method.
4275 A method DECL is built differently is there is no patch
4276 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4277 pending on the currently defined method. */
4278
4279static int patch_stage;
4280
4281/* Check the method declaration and add the method to its current
4282 class. If the argument list is known to contain incomplete types,
4283 the method is partially added and the registration will be resume
22eed1e6
APB
4284 once the method arguments resolved. If TYPE is NULL, we're dealing
4285 with a constructor. */
e04a16fb
AG
4286
4287static tree
4288method_header (flags, type, mdecl, throws)
4289 int flags;
4290 tree type, mdecl, throws;
4291{
1886c9d8 4292 tree type_wfl = NULL_TREE;
79d13333 4293 tree meth_name = NULL_TREE;
c2952b01 4294 tree current, orig_arg, this_class = NULL;
34d4df06 4295 tree id, meth;
e04a16fb 4296 int saved_lineno;
1886c9d8 4297 int constructor_ok = 0, must_chain;
c2952b01 4298 int count;
34d4df06
APB
4299
4300 if (mdecl == error_mark_node)
4301 return error_mark_node;
4302 meth = TREE_VALUE (mdecl);
4303 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4304
4305 check_modifiers_consistency (flags);
79d13333 4306
c2952b01
APB
4307 if (GET_CPC ())
4308 this_class = TREE_TYPE (GET_CPC ());
4309
4310 if (!this_class || this_class == error_mark_node)
79d13333 4311 return NULL_TREE;
e04a16fb
AG
4312
4313 /* There are some forbidden modifiers for an abstract method and its
4314 class must be abstract as well. */
22eed1e6 4315 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4316 {
4317 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4318 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4319 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4320 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4321 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4322 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4323 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4324 parse_error_context
781b0558 4325 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4326 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4327 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4328 }
c2952b01 4329
22eed1e6
APB
4330 /* Things to be checked when declaring a constructor */
4331 if (!type)
4332 {
4333 int ec = java_error_count;
4334 /* 8.6: Constructor declarations: we might be trying to define a
4335 method without specifying a return type. */
c2952b01 4336 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4337 parse_error_context
4338 (id, "Invalid method declaration, return type required");
4339 /* 8.6.3: Constructor modifiers */
4340 else
4341 {
4342 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4343 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4344 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4345 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4346 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4347 }
4348 /* If we found error here, we don't consider it's OK to tread
4349 the method definition as a constructor, for the rest of this
4350 function */
4351 if (ec == java_error_count)
4352 constructor_ok = 1;
4353 }
e04a16fb
AG
4354
4355 /* Method declared within the scope of an interface are implicitly
4356 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4357 modifiers are checked right after. */
e04a16fb
AG
4358
4359 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4360 {
4361 /* If FLAGS isn't set because of a modifier, turn the
4362 corresponding modifier WFL to NULL so we issue a warning on
4363 the obsolete use of the modifier */
4364 if (!(flags & ACC_PUBLIC))
4365 MODIFIER_WFL (PUBLIC_TK) = NULL;
4366 if (!(flags & ACC_ABSTRACT))
4367 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4368 flags |= ACC_PUBLIC;
4369 flags |= ACC_ABSTRACT;
4370 }
4371
c2952b01
APB
4372 /* Inner class can't declare static methods */
4373 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4374 {
4375 parse_error_context
4376 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4377 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4378 lang_printable_name (this_class, 0));
4379 }
4380
e04a16fb
AG
4381 /* Modifiers context reset moved up, so abstract method declaration
4382 modifiers can be later checked. */
4383
22eed1e6
APB
4384 /* Set constructor returned type to void and method name to <init>,
4385 unless we found an error identifier the constructor (in which
4386 case we retain the original name) */
4387 if (!type)
4388 {
4389 type = void_type_node;
4390 if (constructor_ok)
4391 meth_name = init_identifier_node;
4392 }
4393 else
4394 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4395
1886c9d8
APB
4396 /* Do the returned type resolution and registration if necessary */
4397 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4398
4a5f66c3
APB
4399 if (meth_name)
4400 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4401 EXPR_WFL_NODE (id) = meth_name;
4402 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4403
4404 if (must_chain)
e04a16fb 4405 {
1886c9d8
APB
4406 patch_stage = JDEP_METHOD_RETURN;
4407 register_incomplete_type (patch_stage, type_wfl, id, type);
4408 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4409 }
4410 else
1886c9d8 4411 TREE_TYPE (meth) = type;
e04a16fb
AG
4412
4413 saved_lineno = lineno;
4414 /* When defining an abstract or interface method, the curly
4415 bracket at level 1 doesn't exist because there is no function
4416 body */
4417 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4418 EXPR_WFL_LINENO (id));
4419
5e942c50
APB
4420 /* Remember the original argument list */
4421 orig_arg = TYPE_ARG_TYPES (meth);
4422
e04a16fb
AG
4423 if (patch_stage) /* includes ret type and/or all args */
4424 {
4425 jdep *jdep;
4426 meth = add_method_1 (this_class, flags, meth_name, meth);
4427 /* Patch for the return type */
4428 if (patch_stage == JDEP_METHOD_RETURN)
4429 {
4430 jdep = CLASSD_LAST (ctxp->classd_list);
4431 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4432 }
4433 /* This is the stop JDEP. METH allows the function's signature
4434 to be computed. */
4435 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4436 }
4437 else
5e942c50
APB
4438 meth = add_method (this_class, flags, meth_name,
4439 build_java_signature (meth));
4440
c2952b01
APB
4441 /* Remember final parameters */
4442 MARK_FINAL_PARMS (meth, orig_arg);
4443
5e942c50
APB
4444 /* Fix the method argument list so we have the argument name
4445 information */
4446 fix_method_argument_names (orig_arg, meth);
4447
4448 /* Register the parameter number and re-install the current line
4449 number */
e04a16fb
AG
4450 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4451 lineno = saved_lineno;
b9f7e36c
APB
4452
4453 /* Register exception specified by the `throws' keyword for
4454 resolution and set the method decl appropriate field to the list.
4455 Note: the grammar ensures that what we get here are class
4456 types. */
4457 if (throws)
4458 {
4459 throws = nreverse (throws);
4460 for (current = throws; current; current = TREE_CHAIN (current))
4461 {
4462 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4463 NULL_TREE, NULL_TREE);
4464 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4465 &TREE_VALUE (current);
4466 }
4467 DECL_FUNCTION_THROWS (meth) = throws;
4468 }
4469
e04a16fb
AG
4470 /* We set the DECL_NAME to ID so we can track the location where
4471 the function was declared. This allow us to report
4472 redefinition error accurately. When method are verified,
4473 DECL_NAME is reinstalled properly (using the content of the
4474 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4475 when Object is being defined. Constructor <init> names will be
4476 reinstalled the same way. */
c2952b01 4477 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4478 DECL_NAME (meth) = id;
22eed1e6
APB
4479
4480 /* Set the flag if we correctly processed a constructor */
4481 if (constructor_ok)
c2952b01
APB
4482 {
4483 DECL_CONSTRUCTOR_P (meth) = 1;
4484 /* Compute and store the number of artificial parameters declared
4485 for this constructor */
4486 for (count = 0, current = TYPE_FIELDS (this_class); current;
4487 current = TREE_CHAIN (current))
4488 if (FIELD_LOCAL_ALIAS (current))
4489 count++;
4490 DECL_FUNCTION_NAP (meth) = count;
4491 }
22eed1e6 4492
5e942c50
APB
4493 /* Eventually set the @deprecated tag flag */
4494 CHECK_DEPRECATED (meth);
4495
7f10c2e2
APB
4496 /* If doing xref, store column and line number information instead
4497 of the line number only. */
4498 if (flag_emit_xref)
4499 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4500
e04a16fb
AG
4501 return meth;
4502}
4503
5e942c50
APB
4504static void
4505fix_method_argument_names (orig_arg, meth)
4506 tree orig_arg, meth;
4507{
4508 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4509 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4510 {
4511 TREE_PURPOSE (arg) = this_identifier_node;
4512 arg = TREE_CHAIN (arg);
4513 }
de4c7b02 4514 while (orig_arg != end_params_node)
5e942c50
APB
4515 {
4516 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4517 orig_arg = TREE_CHAIN (orig_arg);
4518 arg = TREE_CHAIN (arg);
4519 }
4520}
4521
22eed1e6
APB
4522/* Complete the method declaration with METHOD_BODY. */
4523
4524static void
b635eb2f 4525finish_method_declaration (method_body)
22eed1e6
APB
4526 tree method_body;
4527{
79d13333
APB
4528 int flags;
4529
4530 if (!current_function_decl)
4531 return;
4532
4533 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4534
4535 /* 8.4.5 Method Body */
4536 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4537 {
4538 tree wfl = DECL_NAME (current_function_decl);
4539 parse_error_context (wfl,
4540 "%s method `%s' can't have a body defined",
4541 (METHOD_NATIVE (current_function_decl) ?
4542 "Native" : "Abstract"),
4543 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4544 method_body = NULL_TREE;
4545 }
4546 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4547 {
4548 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4549 parse_error_context
4550 (wfl,
4551 "Non native and non abstract method `%s' must have a body defined",
4552 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4553 method_body = NULL_TREE;
4554 }
4555
2c56429a
APB
4556 if (flag_emit_class_files && method_body
4557 && TREE_CODE (method_body) == NOP_EXPR
4558 && TREE_TYPE (current_function_decl)
4559 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4560 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4561
22eed1e6
APB
4562 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4563 maybe_absorb_scoping_blocks ();
4564 /* Exit function's body */
4565 exit_block ();
4566 /* Merge last line of the function with first line, directly in the
4567 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4568 if (!flag_emit_xref)
4569 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4570
4571 /* Since function's argument's list are shared, reset the
4572 ARG_FINAL_P parameter that might have been set on some of this
4573 function parameters. */
4574 UNMARK_FINAL_PARMS (current_function_decl);
4575
f099f336
APB
4576 /* So we don't have an irrelevant function declaration context for
4577 the next static block we'll see. */
4578 current_function_decl = NULL_TREE;
22eed1e6
APB
4579}
4580
4581/* Build a an error message for constructor circularity errors. */
4582
4583static char *
4584constructor_circularity_msg (from, to)
4585 tree from, to;
4586{
4587 static char string [4096];
c2e3db92 4588 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4589 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4590 free (t);
4591 return string;
4592}
4593
4594/* Verify a circular call to METH. Return 1 if an error is found, 0
4595 otherwise. */
4596
4597static int
4598verify_constructor_circularity (meth, current)
4599 tree meth, current;
4600{
4601 static tree list = NULL_TREE;
19e223db 4602 static int initialized_p;
22eed1e6 4603 tree c;
19e223db
MM
4604
4605 /* If we haven't already registered LIST with the garbage collector,
4606 do so now. */
4607 if (!initialized_p)
4608 {
4609 ggc_add_tree_root (&list, 1);
4610 initialized_p = 1;
4611 }
4612
22eed1e6
APB
4613 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4614 {
4615 if (TREE_VALUE (c) == meth)
4616 {
4617 char *t;
4618 if (list)
4619 {
4620 tree liste;
4621 list = nreverse (list);
4622 for (liste = list; liste; liste = TREE_CHAIN (liste))
4623 {
4624 parse_error_context
c63b98cd 4625 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4626 constructor_circularity_msg
4627 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4628 java_error_count--;
4629 }
4630 }
c2e3db92 4631 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4632 parse_error_context (TREE_PURPOSE (c),
4633 "%s: recursive invocation of constructor `%s'",
4634 constructor_circularity_msg (current, meth), t);
4635 free (t);
4636 list = NULL_TREE;
4637 return 1;
4638 }
4639 }
4640 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4641 {
4642 list = tree_cons (c, current, list);
4643 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4644 return 1;
4645 list = TREE_CHAIN (list);
4646 }
4647 return 0;
4648}
4649
e04a16fb
AG
4650/* Check modifiers that can be declared but exclusively */
4651
4652static void
4653check_modifiers_consistency (flags)
4654 int flags;
4655{
4656 int acc_count = 0;
4657 tree cl = NULL_TREE;
4658
e0fc4118
TT
4659 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4660 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4661 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4662 if (acc_count > 1)
4663 parse_error_context
e0fc4118
TT
4664 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4665
4666 acc_count = 0;
4667 cl = NULL_TREE;
14d075d8
TT
4668 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4669 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4670 if (acc_count > 1)
4671 parse_error_context (cl,
4672 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4673}
4674
4675/* Check the methode header METH for abstract specifics features */
4676
4677static void
4678check_abstract_method_header (meth)
4679 tree meth;
4680{
4681 int flags = get_access_flags_from_decl (meth);
4682 /* DECL_NAME might still be a WFL node */
c877974e 4683 tree name = GET_METHOD_NAME (meth);
e04a16fb 4684
2884c41e
KG
4685 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4686 ACC_ABSTRACT, "abstract method",
4687 IDENTIFIER_POINTER (name));
4688 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4689 ACC_PUBLIC, "abstract method",
4690 IDENTIFIER_POINTER (name));
e04a16fb
AG
4691
4692 check_modifiers ("Illegal modifier `%s' for interface method",
4693 flags, INTERFACE_METHOD_MODIFIERS);
4694}
4695
4696/* Create a FUNCTION_TYPE node and start augmenting it with the
4697 declared function arguments. Arguments type that can't be resolved
4698 are left as they are, but the returned node is marked as containing
4699 incomplete types. */
4700
4701static tree
4702method_declarator (id, list)
4703 tree id, list;
4704{
4705 tree arg_types = NULL_TREE, current, node;
4706 tree meth = make_node (FUNCTION_TYPE);
4707 jdep *jdep;
e04a16fb
AG
4708
4709 patch_stage = JDEP_NO_PATCH;
c2952b01 4710
34d4df06
APB
4711 if (GET_CPC () == error_mark_node)
4712 return error_mark_node;
4713
c2952b01
APB
4714 /* If we're dealing with an inner class constructor, we hide the
4715 this$<n> decl in the name field of its parameter declaration. We
4716 also might have to hide the outer context local alias
4717 initializers. Not done when the class is a toplevel class. */
4718 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4719 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4720 {
4721 tree aliases_list, type, thisn;
4722 /* First the aliases, linked to the regular parameters */
4723 aliases_list =
4724 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4725 TREE_TYPE (GET_CPC ()),
4726 NULL_TREE, NULL);
4727 list = chainon (nreverse (aliases_list), list);
4728
4729 /* Then this$<n> */
4730 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4731 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4732 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4733 list);
4734 }
e04a16fb
AG
4735
4736 for (current = list; current; current = TREE_CHAIN (current))
4737 {
c583dd46 4738 int must_chain = 0;
e04a16fb
AG
4739 tree wfl_name = TREE_PURPOSE (current);
4740 tree type = TREE_VALUE (current);
4741 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4742 tree already, arg_node;
4743 tree type_wfl = NULL_TREE;
23a79c61 4744 tree real_type;
c583dd46
APB
4745
4746 /* Obtain a suitable type for resolution, if necessary */
4747 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4748
4749 /* Process NAME, as it may specify extra dimension(s) for it */
4750 type = build_array_from_name (type, type_wfl, name, &name);
4751 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4752
23a79c61
APB
4753 real_type = GET_REAL_TYPE (type);
4754 if (TREE_CODE (real_type) == RECORD_TYPE)
4755 {
4756 real_type = promote_type (real_type);
4757 if (TREE_CODE (type) == TREE_LIST)
4758 TREE_PURPOSE (type) = real_type;
4759 }
5e942c50 4760
e04a16fb
AG
4761 /* Check redefinition */
4762 for (already = arg_types; already; already = TREE_CHAIN (already))
4763 if (TREE_PURPOSE (already) == name)
4764 {
781b0558
KG
4765 parse_error_context
4766 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4767 IDENTIFIER_POINTER (name),
e04a16fb
AG
4768 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4769 break;
4770 }
4771
4772 /* If we've an incomplete argument type, we know there is a location
4773 to patch when the type get resolved, later. */
4774 jdep = NULL;
c583dd46 4775 if (must_chain)
e04a16fb 4776 {
c583dd46
APB
4777 patch_stage = JDEP_METHOD;
4778 type = register_incomplete_type (patch_stage,
4779 type_wfl, wfl_name, type);
4780 jdep = CLASSD_LAST (ctxp->classd_list);
4781 JDEP_MISC (jdep) = id;
e04a16fb 4782 }
c583dd46 4783
c2952b01 4784 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4785 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4786 /* Remeber arguments declared final. */
4787 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4788
e04a16fb
AG
4789 if (jdep)
4790 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4791 TREE_CHAIN (arg_node) = arg_types;
4792 arg_types = arg_node;
4793 }
de4c7b02 4794 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4795 node = build_tree_list (id, meth);
4796 return node;
4797}
4798
4799static int
4800unresolved_type_p (wfl, returned)
4801 tree wfl;
4802 tree *returned;
4803
4804{
4805 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4806 {
e04a16fb 4807 if (returned)
165f37bc
APB
4808 {
4809 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4810 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4811 *returned = TREE_TYPE (decl);
4812 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4813 *returned = TREE_TYPE (GET_CPC ());
4814 else
4815 *returned = NULL_TREE;
4816 }
e04a16fb
AG
4817 return 1;
4818 }
4819 if (returned)
4820 *returned = wfl;
4821 return 0;
4822}
4823
4824/* From NAME, build a qualified identifier node using the
4825 qualification from the current package definition. */
4826
4827static tree
98a52c2c 4828parser_qualified_classname (name)
e04a16fb
AG
4829 tree name;
4830{
c2952b01
APB
4831 tree nested_class_name;
4832
98a52c2c 4833 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4834 return nested_class_name;
4835
e04a16fb 4836 if (ctxp->package)
c2952b01 4837 return merge_qualified_name (ctxp->package, name);
e04a16fb 4838 else
c2952b01 4839 return name;
e04a16fb
AG
4840}
4841
4842/* Called once the type a interface extends is resolved. Returns 0 if
4843 everything is OK. */
4844
4845static int
4846parser_check_super_interface (super_decl, this_decl, this_wfl)
4847 tree super_decl, this_decl, this_wfl;
4848{
4849 tree super_type = TREE_TYPE (super_decl);
4850
4851 /* Has to be an interface */
c2952b01 4852 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4853 {
4854 parse_error_context
4855 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4856 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4857 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4858 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4859 "interface" : "class"),
4860 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4861 return 1;
4862 }
4863
4864 /* Check scope: same package OK, other package: OK if public */
4865 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4866 return 1;
4867
4868 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4869 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4870 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4871 return 0;
4872}
4873
4874/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4875 0 if everthing is OK. */
4876
4877static int
4878parser_check_super (super_decl, this_decl, wfl)
4879 tree super_decl, this_decl, wfl;
4880{
e04a16fb
AG
4881 tree super_type = TREE_TYPE (super_decl);
4882
4883 /* SUPER should be a CLASS (neither an array nor an interface) */
4884 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4885 {
4886 parse_error_context
4887 (wfl, "Class `%s' can't subclass %s `%s'",
4888 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4889 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4890 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4891 return 1;
4892 }
4893
4894 if (CLASS_FINAL (TYPE_NAME (super_type)))
4895 {
4896 parse_error_context (wfl, "Can't subclass final classes: %s",
4897 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4898 return 1;
4899 }
4900
4901 /* Check scope: same package OK, other package: OK if public */
4902 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4903 return 1;
4904
4905 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4906 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4907 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4908 return 0;
4909}
4910
4911/* Create a new dependency list and link it (in a LIFO manner) to the
4912 CTXP list of type dependency list. */
4913
4914static void
4915create_jdep_list (ctxp)
4916 struct parser_ctxt *ctxp;
4917{
23a79c61 4918 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4919 new->first = new->last = NULL;
4920 new->next = ctxp->classd_list;
4921 ctxp->classd_list = new;
4922}
4923
4924static jdeplist *
4925reverse_jdep_list (ctxp)
4926 struct parser_ctxt *ctxp;
4927{
4928 register jdeplist *prev = NULL, *current, *next;
4929 for (current = ctxp->classd_list; current; current = next)
4930 {
4931 next = current->next;
4932 current->next = prev;
4933 prev = current;
4934 }
4935 return prev;
4936}
4937
23a79c61
APB
4938/* Create a fake pointer based on the ID stored in
4939 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4940 registered again. */
e04a16fb
AG
4941
4942static tree
23a79c61
APB
4943obtain_incomplete_type (type_name)
4944 tree type_name;
e04a16fb 4945{
23a79c61
APB
4946 tree ptr, name;
4947
4948 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4949 name = EXPR_WFL_NODE (type_name);
4950 else if (INCOMPLETE_TYPE_P (type_name))
4951 name = TYPE_NAME (type_name);
4952 else
4953 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4954
4955 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4956 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4957 break;
4958
4959 if (!ptr)
4960 {
78d21f92
PB
4961 BUILD_PTR_FROM_NAME (ptr, name);
4962 layout_type (ptr);
e04a16fb
AG
4963 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4964 ctxp->incomplete_class = ptr;
4965 }
4966
4967 return ptr;
4968}
4969
4970/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4971 non NULL instead of computing a new fake type based on WFL. The new
4972 dependency is inserted in the current type dependency list, in FIFO
4973 manner. */
4974
4975static tree
4976register_incomplete_type (kind, wfl, decl, ptr)
4977 int kind;
4978 tree wfl, decl, ptr;
4979{
23a79c61 4980 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4981
e04a16fb
AG
4982 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4983 ptr = obtain_incomplete_type (wfl);
4984
4985 JDEP_KIND (new) = kind;
4986 JDEP_DECL (new) = decl;
4987 JDEP_SOLV (new) = ptr;
4988 JDEP_WFL (new) = wfl;
4989 JDEP_CHAIN (new) = NULL;
4990 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4991 /* For some dependencies, set the enclosing class of the current
4992 class to be the enclosing context */
4993 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4994 && GET_ENCLOSING_CPC ())
4995 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4996 else
324ed8fd 4997 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4998 JDEP_GET_PATCH (new) = (tree *)NULL;
4999
5000 JDEP_INSERT (ctxp->classd_list, new);
5001
5002 return ptr;
5003}
5004
5005void
5006java_check_circular_reference ()
5007{
5008 tree current;
5009 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5010 {
5011 tree type = TREE_TYPE (current);
e920ebc9 5012 if (CLASS_INTERFACE (current))
e04a16fb
AG
5013 {
5014 /* Check all interfaces this class extends */
5015 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5016 int n, i;
5017
5018 if (!basetype_vec)
5019 return;
5020 n = TREE_VEC_LENGTH (basetype_vec);
5021 for (i = 0; i < n; i++)
5022 {
5023 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5024 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5025 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5026 parse_error_context (lookup_cl (current),
5027 "Cyclic interface inheritance");
5028 }
5029 }
5030 else
5031 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5032 parse_error_context (lookup_cl (current),
c2952b01
APB
5033 "Cyclic class inheritance%s",
5034 (cyclic_inheritance_report ?
5035 cyclic_inheritance_report : ""));
5036 }
5037}
5038
5039/* Augment the parameter list PARM with parameters crafted to
5040 initialize outer context locals aliases. Through ARTIFICIAL, a
5041 count is kept of the number of crafted parameters. MODE governs
5042 what eventually gets created: something suitable for a function
5043 creation or a function invocation, either the constructor or
c00f0fb2 5044 finit$. */
c2952b01
APB
5045
5046static tree
5047build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5048 int mode;
5049 tree class_type, parm;
5050 int *artificial;
5051{
5052 tree field;
da632f2c
APB
5053 tree additional_parms = NULL_TREE;
5054
c2952b01
APB
5055 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5056 if (FIELD_LOCAL_ALIAS (field))
5057 {
63ad61ed 5058 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01 5059 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
1f8f4a0b 5060 tree mangled_id;
c2952b01
APB
5061
5062 switch (mode)
5063 {
5064 case AIPL_FUNCTION_DECLARATION:
1f8f4a0b
MM
5065 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5066 &buffer [4]);
5067 purpose = build_wfl_node (mangled_id);
c2952b01
APB
5068 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5069 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5070 else
5071 value = TREE_TYPE (field);
5072 break;
5073
5074 case AIPL_FUNCTION_CREATION:
1f8f4a0b
MM
5075 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5076 &buffer [4]);
c2952b01
APB
5077 value = TREE_TYPE (field);
5078 break;
5079
5080 case AIPL_FUNCTION_FINIT_INVOCATION:
1f8f4a0b
MM
5081 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5082 &buffer [4]);
c2952b01
APB
5083 /* Now, this is wrong. purpose should always be the NAME
5084 of something and value its matching value (decl, type,
5085 etc...) FIXME -- but there is a lot to fix. */
5086
5087 /* When invoked for this kind of operation, we already
5088 know whether a field is used or not. */
5089 purpose = TREE_TYPE (field);
1f8f4a0b 5090 value = build_wfl_node (mangled_id);
c2952b01
APB
5091 break;
5092
5093 case AIPL_FUNCTION_CTOR_INVOCATION:
5094 /* There are two case: the constructor invokation happends
5095 outside the local inner, in which case, locales from the outer
5096 context are directly used.
5097
5098 Otherwise, we fold to using the alias directly. */
5099 if (class_type == current_class)
5100 value = field;
5101 else
5102 {
5103 name = get_identifier (&buffer[4]);
5104 value = IDENTIFIER_LOCAL_VALUE (name);
5105 }
5106 break;
5107 }
da632f2c 5108 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5109 if (artificial)
5110 *artificial +=1;
5111 }
da632f2c
APB
5112 if (additional_parms)
5113 {
5114 if (ANONYMOUS_CLASS_P (class_type)
5115 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5116 additional_parms = nreverse (additional_parms);
5117 parm = chainon (additional_parms, parm);
5118 }
5119
5120 return parm;
c2952b01
APB
5121}
5122
5123/* Craft a constructor for CLASS_DECL -- what we should do when none
5124 where found. ARGS is non NULL when a special signature must be
5125 enforced. This is the case for anonymous classes. */
5126
5127static void
5128craft_constructor (class_decl, args)
5129 tree class_decl, args;
5130{
5131 tree class_type = TREE_TYPE (class_decl);
5132 tree parm = NULL_TREE;
5133 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5134 ACC_PUBLIC : 0);
5135 int i = 0, artificial = 0;
5136 tree decl, ctor_name;
5137 char buffer [80];
5138
c2952b01
APB
5139 /* The constructor name is <init> unless we're dealing with an
5140 anonymous class, in which case the name will be fixed after having
5141 be expanded. */
5142 if (ANONYMOUS_CLASS_P (class_type))
5143 ctor_name = DECL_NAME (class_decl);
5144 else
5145 ctor_name = init_identifier_node;
5146
5147 /* If we're dealing with an inner class constructor, we hide the
5148 this$<n> decl in the name field of its parameter declaration. */
5149 if (PURE_INNER_CLASS_TYPE_P (class_type))
5150 {
5151 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5152 parm = tree_cons (build_current_thisn (class_type),
5153 build_pointer_type (type), parm);
5154
5155 /* Some more arguments to be hidden here. The values of the local
5156 variables of the outer context that the inner class needs to see. */
5157 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5158 class_type, parm,
5159 &artificial);
5160 }
5161
5162 /* Then if there are any args to be enforced, enforce them now */
5163 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5164 {
5165 sprintf (buffer, "parm%d", i++);
5166 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5167 }
c2952b01
APB
5168
5169 CRAFTED_PARAM_LIST_FIXUP (parm);
5170 decl = create_artificial_method (class_type, flags, void_type_node,
5171 ctor_name, parm);
5172 fix_method_argument_names (parm, decl);
5173 /* Now, mark the artificial parameters. */
5174 DECL_FUNCTION_NAP (decl) = artificial;
c2952b01 5175 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5176}
5177
c2952b01 5178
e920ebc9
APB
5179/* Fix the constructors. This will be called right after circular
5180 references have been checked. It is necessary to fix constructors
5181 early even if no code generation will take place for that class:
5182 some generated constructor might be required by the class whose
5183 compilation triggered this one to be simply loaded. */
5184
5185void
5186java_fix_constructors ()
5187{
5188 tree current;
5189
5190 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5191 {
e920ebc9
APB
5192 tree class_type = TREE_TYPE (current);
5193 int saw_ctor = 0;
c2952b01
APB
5194 tree decl;
5195
5196 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5197 continue;
e920ebc9
APB
5198
5199 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5200 {
5201 if (DECL_CONSTRUCTOR_P (decl))
5202 {
5203 fix_constructors (decl);
5204 saw_ctor = 1;
5205 }
5206 }
5207
c2952b01
APB
5208 /* Anonymous class constructor can't be generated that early. */
5209 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5210 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5211 }
5212}
5213
23a79c61
APB
5214/* safe_layout_class just makes sure that we can load a class without
5215 disrupting the current_class, input_file, lineno, etc, information
5216 about the class processed currently. */
5217
e04a16fb
AG
5218void
5219safe_layout_class (class)
5220 tree class;
5221{
5222 tree save_current_class = current_class;
3b304f5b 5223 const char *save_input_filename = input_filename;
e04a16fb 5224 int save_lineno = lineno;
5e942c50 5225
e04a16fb 5226 layout_class (class);
5e942c50 5227
e04a16fb
AG
5228 current_class = save_current_class;
5229 input_filename = save_input_filename;
5230 lineno = save_lineno;
5231 CLASS_LOADED_P (class) = 1;
5232}
5233
5234static tree
5235jdep_resolve_class (dep)
5236 jdep *dep;
5237{
5238 tree decl;
5239
23a79c61
APB
5240 if (JDEP_RESOLVED_P (dep))
5241 decl = JDEP_RESOLVED_DECL (dep);
5242 else
e04a16fb 5243 {
c2952b01 5244 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5245 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5246 JDEP_RESOLVED (dep, decl);
5247 }
23a79c61 5248
e04a16fb 5249 if (!decl)
23a79c61 5250 complete_class_report_errors (dep);
1e12ab9b 5251 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5252 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5253 return decl;
5254}
5255
5256/* Complete unsatisfied class declaration and their dependencies */
5257
5258void
5259java_complete_class ()
5260{
e04a16fb
AG
5261 tree cclass;
5262 jdeplist *cclassd;
5263 int error_found;
b67d701b 5264 tree type;
e04a16fb 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);
e04a16fb 5300 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb 5301 field_type = promote_type (field_type);
e04a16fb 5302 TREE_TYPE (field_decl) = field_type;
5e942c50 5303 DECL_ALIGN (field_decl) = 0;
11cf4d18 5304 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5305 layout_decl (field_decl, 0);
e04a16fb
AG
5306 SOURCE_FRONTEND_DEBUG
5307 (("Completed field/var decl `%s' with `%s'",
5308 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5309 IDENTIFIER_POINTER (DECL_NAME (decl))));
5310 break;
5311 }
5312 case JDEP_METHOD: /* We start patching a method */
5313 case JDEP_METHOD_RETURN:
5314 error_found = 0;
5315 while (1)
5316 {
5317 if (decl)
5318 {
b67d701b
PB
5319 type = TREE_TYPE(decl);
5320 if (TREE_CODE (type) == RECORD_TYPE)
5321 type = promote_type (type);
e04a16fb
AG
5322 JDEP_APPLY_PATCH (dep, type);
5323 SOURCE_FRONTEND_DEBUG
5324 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5325 "Completing fct `%s' with ret type `%s'":
5326 "Completing arg `%s' with type `%s'"),
5327 IDENTIFIER_POINTER (EXPR_WFL_NODE
5328 (JDEP_DECL_WFL (dep))),
5329 IDENTIFIER_POINTER (DECL_NAME (decl))));
5330 }
5331 else
5332 error_found = 1;
5333 dep = JDEP_CHAIN (dep);
5334 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5335 break;
5336 else
5337 decl = jdep_resolve_class (dep);
5338 }
5339 if (!error_found)
5340 {
5341 tree mdecl = JDEP_DECL (dep), signature;
165f37bc
APB
5342 /* Recompute and reset the signature, check first that
5343 all types are now defined. If they're not,
5344 dont build the signature. */
5345 if (check_method_types_complete (mdecl))
5346 {
5347 signature = build_java_signature (TREE_TYPE (mdecl));
5348 set_java_signature (TREE_TYPE (mdecl), signature);
5349 }
e04a16fb
AG
5350 }
5351 else
5352 continue;
5353 break;
5354
5355 case JDEP_INTERFACE:
5356 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5357 JDEP_WFL (dep)))
5358 continue;
5359 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5360 break;
5361
b67d701b 5362 case JDEP_PARM:
e04a16fb 5363 case JDEP_VARIABLE:
b67d701b
PB
5364 type = TREE_TYPE(decl);
5365 if (TREE_CODE (type) == RECORD_TYPE)
5366 type = promote_type (type);
5367 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5368 break;
5369
5370 case JDEP_TYPE:
5371 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5372 SOURCE_FRONTEND_DEBUG
5373 (("Completing a random type dependency on a '%s' node",
5374 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5375 break;
5376
b9f7e36c 5377 case JDEP_EXCEPTION:
c877974e
APB
5378 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5379 SOURCE_FRONTEND_DEBUG
5380 (("Completing `%s' `throws' argument node",
5381 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5382 break;
5383
c2952b01
APB
5384 case JDEP_ANONYMOUS:
5385 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5386 break;
5387
e04a16fb 5388 default:
0a2138e2
APB
5389 fatal ("Can't handle patch code %d - java_complete_class",
5390 JDEP_KIND (dep));
e04a16fb
AG
5391 }
5392 }
5393 }
e04a16fb
AG
5394 return;
5395}
5396
5397/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5398 array. */
5399
5400static tree
c2952b01
APB
5401resolve_class (enclosing, class_type, decl, cl)
5402 tree enclosing, class_type, decl, cl;
e04a16fb 5403{
49f48c71
KG
5404 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5405 const char *base = name;
78d21f92
PB
5406 tree resolved_type = TREE_TYPE (class_type);
5407 tree resolved_type_decl;
e04a16fb 5408
78d21f92
PB
5409 if (resolved_type != NULL_TREE)
5410 {
5411 tree resolved_type_decl = TYPE_NAME (resolved_type);
5412 if (resolved_type_decl == NULL_TREE
5413 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5414 {
5415 resolved_type_decl = build_decl (TYPE_DECL,
5416 TYPE_NAME (class_type),
5417 resolved_type);
5418 }
5419 return resolved_type_decl;
5420 }
5421
e04a16fb
AG
5422 /* 1- Check to see if we have an array. If true, find what we really
5423 want to resolve */
5424 while (name[0] == '[')
5425 name++;
5426 if (base != name)
34d4df06
APB
5427 {
5428 TYPE_NAME (class_type) = get_identifier (name);
5429 WFL_STRIP_BRACKET (cl, cl);
5430 }
e04a16fb
AG
5431
5432 /* 2- Resolve the bare type */
c2952b01
APB
5433 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5434 decl, cl)))
e04a16fb
AG
5435 return NULL_TREE;
5436 resolved_type = TREE_TYPE (resolved_type_decl);
5437
5438 /* 3- If we have and array, reconstruct the array down to its nesting */
5439 if (base != name)
5440 {
5441 while (base != name)
5442 {
5443 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5444 resolved_type = promote_type (resolved_type);
5445 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5446 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5447 name--;
5448 }
5449 /* Build a fake decl for this, since this is what is expected to
5450 be returned. */
5451 resolved_type_decl =
5452 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5453 /* Figure how those two things are important for error report. FIXME */
5454 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5455 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5456 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5457 }
78d21f92 5458 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5459 return resolved_type_decl;
5460}
5461
5462/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5463 are used to report error messages. */
5464
78d21f92 5465tree
c2952b01
APB
5466do_resolve_class (enclosing, class_type, decl, cl)
5467 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5468{
5469 tree new_class_decl;
e04a16fb
AG
5470
5471 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5472 it is changed by find_in_imports{_on_demand} and (but it doesn't
5473 really matter) qualify_and_find */
e04a16fb 5474
c2952b01
APB
5475 /* 0- Search in the current class as an inner class */
5476
5477 /* Maybe some code here should be added to load the class or
5478 something, at least if the class isn't an inner class and ended
5479 being loaded from class file. FIXME. */
a40d21da
APB
5480 while (enclosing)
5481 {
5482 tree name;
5483
5484 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5485 return new_class_decl;
5486
0c2b8145
APB
5487 /* Explore enclosing contexts. */
5488 while (INNER_CLASS_DECL_P (enclosing))
5489 {
5490 enclosing = DECL_CONTEXT (enclosing);
5491 if ((new_class_decl = find_as_inner_class (enclosing,
5492 class_type, cl)))
5493 return new_class_decl;
5494 }
5495
a40d21da
APB
5496 /* Now go to the upper classes, bail out if necessary. */
5497 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5498 if (!enclosing || enclosing == object_type_node)
5499 break;
5500
5501 if (TREE_CODE (enclosing) == RECORD_TYPE)
5502 {
5503 enclosing = TYPE_NAME (enclosing);
5504 continue;
5505 }
5506
5507 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5508 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5509 else
5510 name = enclosing;
5511 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5512 }
c2952b01 5513
9a7ab4b3
APB
5514 /* 1- Check for the type in single imports. This will change
5515 TYPE_NAME() if something relevant is found */
5516 find_in_imports (class_type);
e04a16fb 5517
9a7ab4b3 5518 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5519 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5520 {
5521 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5522 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5523 load_class (TYPE_NAME (class_type), 0);
5524 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5525 }
5526
9a7ab4b3
APB
5527 /* 3- Search according to the current package definition */
5528 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5529 {
5530 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5531 TYPE_NAME (class_type))))
5532 return new_class_decl;
5533 }
5534
5535 /* 4- Check the import on demands. Don't allow bar.baz to be
5536 imported from foo.* */
5537 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5538 if (find_in_imports_on_demand (class_type))
5539 return NULL_TREE;
5540
5541 /* If found in find_in_imports_on_demant, the type has already been
5542 loaded. */
5543 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5544 return new_class_decl;
5545
5546 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5547 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5548 {
ee07f4f4 5549 tree package;
d6baf6f5
APB
5550
5551 /* If there is a current package (ctxp->package), it's the first
5552 element of package_list and we can skip it. */
5553 for (package = (ctxp->package ?
5554 TREE_CHAIN (package_list) : package_list);
5555 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5556 if ((new_class_decl = qualify_and_find (class_type,
5557 TREE_PURPOSE (package),
5558 TYPE_NAME (class_type))))
5559 return new_class_decl;
5560 }
5561
5562 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5563 load_class (TYPE_NAME (class_type), 0);
5564 if (check_pkg_class_access (TYPE_NAME (class_type),
5565 (cl ? cl : lookup_cl (decl))))
5566 return NULL_TREE;
5567
9a7ab4b3 5568 /* 6- Last call for a resolution */
e04a16fb
AG
5569 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5570}
5571
9a7ab4b3
APB
5572static tree
5573qualify_and_find (class_type, package, name)
5574 tree class_type, package, name;
5575{
5576 tree new_qualified = merge_qualified_name (package, name);
5577 tree new_class_decl;
5578
5579 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5580 load_class (new_qualified, 0);
5581 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5582 {
5583 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5584 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5585 load_class (new_qualified, 0);
5586 TYPE_NAME (class_type) = new_qualified;
5587 return IDENTIFIER_CLASS_VALUE (new_qualified);
5588 }
5589 return NULL_TREE;
5590}
5591
e04a16fb 5592/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5593 parsed class). Return a decl node. This function is meant to be
5594 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5595
5596static tree
c877974e
APB
5597resolve_and_layout (something, cl)
5598 tree something;
e04a16fb
AG
5599 tree cl;
5600{
34d4df06 5601 tree decl, decl_type;
c877974e 5602
23a79c61
APB
5603 /* Don't do that on the current class */
5604 if (something == current_class)
5605 return TYPE_NAME (current_class);
c877974e 5606
23a79c61 5607 /* Don't do anything for void and other primitive types */
c877974e
APB
5608 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5609 return NULL_TREE;
5610
23a79c61
APB
5611 /* Pointer types can be reall pointer types or fake pointers. When
5612 finding a real pointer, recheck for primitive types */
5613 if (TREE_CODE (something) == POINTER_TYPE)
5614 {
5615 if (TREE_TYPE (something))
5616 {
5617 something = TREE_TYPE (something);
5618 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5619 return NULL_TREE;
5620 }
5621 else
5622 something = TYPE_NAME (something);
5623 }
5624
5625 /* Don't do anything for arrays of primitive types */
5626 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5627 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5628 return NULL_TREE;
5629
c2952b01
APB
5630 /* Something might be a WFL */
5631 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5632 something = EXPR_WFL_NODE (something);
5633
5634 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5635 TYPE_DECL or a real TYPE */
5636 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5637 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5638 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5639
23a79c61
APB
5640 if (!(decl = resolve_no_layout (something, cl)))
5641 return NULL_TREE;
5642
5643 /* Resolve and layout if necessary */
34d4df06
APB
5644 decl_type = TREE_TYPE (decl);
5645 layout_class_methods (decl_type);
5646 /* Check methods */
5647 if (CLASS_FROM_SOURCE_P (decl_type))
5648 java_check_methods (decl);
5649 /* Layout the type if necessary */
5650 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5651 safe_layout_class (decl_type);
23a79c61 5652
e04a16fb
AG
5653 return decl;
5654}
5655
5656/* Resolve a class, returns its decl but doesn't perform any
5657 layout. The current parsing context is saved and restored */
5658
5659static tree
5660resolve_no_layout (name, cl)
5661 tree name, cl;
5662{
5663 tree ptr, decl;
5664 BUILD_PTR_FROM_NAME (ptr, name);
5665 java_parser_context_save_global ();
c2952b01 5666 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5667 java_parser_context_restore_global ();
5668
5669 return decl;
5670}
5671
23a79c61
APB
5672/* Called when reporting errors. Skip leader '[' in a complex array
5673 type description that failed to be resolved. */
e04a16fb 5674
49f48c71 5675static const char *
e04a16fb 5676purify_type_name (name)
49f48c71 5677 const char *name;
e04a16fb
AG
5678{
5679 while (*name && *name == '[')
5680 name++;
5681 return name;
5682}
5683
5684/* The type CURRENT refers to can't be found. We print error messages. */
5685
5686static void
5687complete_class_report_errors (dep)
5688 jdep *dep;
5689{
49f48c71 5690 const char *name;
23a79c61
APB
5691
5692 if (!JDEP_WFL (dep))
5693 return;
5694
5695 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5696 switch (JDEP_KIND (dep))
5697 {
5698 case JDEP_SUPER:
5699 parse_error_context
5700 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5701 purify_type_name (name),
e04a16fb
AG
5702 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5703 break;
5704 case JDEP_FIELD:
5705 parse_error_context
5706 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5707 purify_type_name (name),
e04a16fb
AG
5708 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5709 break;
5710 case JDEP_METHOD: /* Covers arguments */
5711 parse_error_context
781b0558 5712 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5713 purify_type_name (name),
e04a16fb
AG
5714 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5715 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5716 break;
5717 case JDEP_METHOD_RETURN: /* Covers return type */
5718 parse_error_context
781b0558 5719 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5720 purify_type_name (name),
e04a16fb
AG
5721 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5722 break;
5723 case JDEP_INTERFACE:
5724 parse_error_context
5725 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5726 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5727 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5728 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5729 break;
5730 case JDEP_VARIABLE:
5731 parse_error_context
781b0558 5732 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5733 purify_type_name (IDENTIFIER_POINTER
5734 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5735 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5736 break;
b9f7e36c
APB
5737 case JDEP_EXCEPTION: /* As specified by `throws' */
5738 parse_error_context
5739 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5740 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5741 break;
0a2138e2
APB
5742 default:
5743 /* Fix for -Wall. Just break doing nothing. The error will be
5744 caught later */
5745 break;
e04a16fb
AG
5746 }
5747}
5748
22eed1e6
APB
5749/* Return a static string containing the DECL prototype string. If
5750 DECL is a constructor, use the class name instead of the form
5751 <init> */
5752
49f48c71 5753static const char *
22eed1e6
APB
5754get_printable_method_name (decl)
5755 tree decl;
5756{
49f48c71 5757 const char *to_return;
9ee9b555 5758 tree name = NULL_TREE;
22eed1e6
APB
5759
5760 if (DECL_CONSTRUCTOR_P (decl))
5761 {
5762 name = DECL_NAME (decl);
5e942c50 5763 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5764 }
5765
5766 to_return = lang_printable_name (decl, 0);
5767 if (DECL_CONSTRUCTOR_P (decl))
5768 DECL_NAME (decl) = name;
5769
5770 return to_return;
5771}
5772
5e942c50
APB
5773/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5774 nevertheless needs to be verfied, 1 otherwise. */
5775
5776static int
5777reset_method_name (method)
5778 tree method;
5779{
c2952b01 5780 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5781 {
5782 /* NAME is just the plain name when Object is being defined */
5783 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5784 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5785 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5786 return 0;
5787 }
5788 else
5789 return 1;
5790}
5791
c877974e
APB
5792/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5793
5794tree
5795java_get_real_method_name (method_decl)
5796 tree method_decl;
5797{
5798 tree method_name = DECL_NAME (method_decl);
5799 if (DECL_CONSTRUCTOR_P (method_decl))
5800 return init_identifier_node;
82371d41
APB
5801
5802 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5803 and still can be a constructor. FIXME */
5804
23a79c61
APB
5805 /* Don't confuse method only bearing the name of their class as
5806 constructors */
82371d41
APB
5807 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5808 && ctxp
c2952b01 5809 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5810 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5811 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5812 return init_identifier_node;
5813 else
5814 return EXPR_WFL_NODE (method_name);
5815}
5816
22eed1e6
APB
5817/* Track method being redefined inside the same class. As a side
5818 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5819 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5820
e04a16fb
AG
5821static int
5822check_method_redefinition (class, method)
5823 tree class, method;
5824{
5825 tree redef, name;
5826 tree cl = DECL_NAME (method);
c3f2a476 5827 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5828 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5829 fixed and checked */
5e942c50
APB
5830
5831 /* Reset the method name before running the check. If it returns 1,
5832 the method doesn't need to be verified with respect to method
5833 redeclaration and we return 0 */
5834 if (reset_method_name (method))
e04a16fb 5835 return 0;
5e942c50
APB
5836
5837 name = DECL_NAME (method);
e04a16fb
AG
5838 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5839 {
c3f2a476 5840 if (redef == method)
e04a16fb 5841 break;
c3f2a476
APB
5842 if (DECL_NAME (redef) == name
5843 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5844 {
22eed1e6
APB
5845 parse_error_context
5846 (cl, "Duplicate %s declaration `%s'",
5847 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5848 get_printable_method_name (redef));
e04a16fb
AG
5849 return 1;
5850 }
5851 }
5852 return 0;
5853}
5854
1175b9b4
TT
5855/* Return 1 if check went ok, 0 otherwise. */
5856static int
d77613be
APB
5857check_abstract_method_definitions (do_interface, class_decl, type)
5858 int do_interface;
5859 tree class_decl, type;
5860{
5861 tree class = TREE_TYPE (class_decl);
5862 tree method, end_type;
1175b9b4 5863 int ok = 1;
d77613be
APB
5864
5865 end_type = (do_interface ? object_type_node : type);
5866 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5867 {
5868 tree other_super, other_method, method_sig, method_name;
5869 int found = 0;
165f37bc 5870 int end_type_reached = 0;
d77613be
APB
5871
5872 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5873 continue;
5874
5875 /* Now verify that somewhere in between TYPE and CLASS,
5876 abstract method METHOD gets a non abstract definition
5877 that is inherited by CLASS. */
5878
5879 method_sig = build_java_signature (TREE_TYPE (method));
5880 method_name = DECL_NAME (method);
5881 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5882 method_name = EXPR_WFL_NODE (method_name);
5883
165f37bc
APB
5884 other_super = class;
5885 do {
5886 if (other_super == end_type)
5887 end_type_reached = 1;
5888
5889 /* Method search */
5890 for (other_method = TYPE_METHODS (other_super); other_method;
5891 other_method = TREE_CHAIN (other_method))
5892 {
5893 tree s = build_java_signature (TREE_TYPE (other_method));
5894 tree other_name = DECL_NAME (other_method);
5895
5896 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5897 other_name = EXPR_WFL_NODE (other_name);
5898 if (!DECL_CLINIT_P (other_method)
5899 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5900 && method_name == other_name
5901 && method_sig == s
5902 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5903 {
5904 found = 1;
5905 break;
5906 }
5907 }
5908 other_super = CLASSTYPE_SUPER (other_super);
5909 } while (!end_type_reached);
5910
d77613be
APB
5911 /* Report that abstract METHOD didn't find an implementation
5912 that CLASS can use. */
5913 if (!found)
5914 {
c2e3db92 5915 char *t = xstrdup (lang_printable_name
d77613be
APB
5916 (TREE_TYPE (TREE_TYPE (method)), 0));
5917 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5918 tree saved_wfl = NULL_TREE;
5919
5920 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5921 {
5922 saved_wfl = DECL_NAME (method);
5923 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5924 }
5925
5926 parse_error_context
5927 (lookup_cl (class_decl),
781b0558 5928 "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
5929 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5930 t, lang_printable_name (method, 0),
5931 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5932 "interface" : "class"),
5933 IDENTIFIER_POINTER (ccn),
5934 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5935 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5936 ok = 0;
d77613be 5937 free (t);
1175b9b4 5938
d77613be
APB
5939 if (saved_wfl)
5940 DECL_NAME (method) = saved_wfl;
5941 }
5942 }
1175b9b4
TT
5943
5944 if (ok && do_interface)
5945 {
5946 /* Check for implemented interfaces. */
5947 int i;
5948 tree vector = TYPE_BINFO_BASETYPES (type);
5949 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5950 {
5951 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5952 ok = check_abstract_method_definitions (1, class_decl, super);
5953 }
5954 }
5955
5956 return ok;
d77613be
APB
5957}
5958
614eaae0 5959/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5960 methods. */
5961
5962static void
5963java_check_abstract_method_definitions (class_decl)
5964 tree class_decl;
5965{
5966 tree class = TREE_TYPE (class_decl);
5967 tree super, vector;
5968 int i;
5969
5970 if (CLASS_ABSTRACT (class_decl))
5971 return;
5972
5973 /* Check for inherited types */
165f37bc
APB
5974 super = class;
5975 do {
5976 super = CLASSTYPE_SUPER (super);
5977 check_abstract_method_definitions (0, class_decl, super);
5978 } while (super != object_type_node);
d77613be
APB
5979
5980 /* Check for implemented interfaces. */
5981 vector = TYPE_BINFO_BASETYPES (class);
5982 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5983 {
5984 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5985 check_abstract_method_definitions (1, class_decl, super);
5986 }
5987}
5988
165f37bc
APB
5989/* Check all the types method DECL uses and return 1 if all of them
5990 are now complete, 0 otherwise. This is used to check whether its
5991 safe to build a method signature or not. */
5992
5993static int
5994check_method_types_complete (decl)
5995 tree decl;
5996{
5997 tree type = TREE_TYPE (decl);
5998 tree args;
5999
6000 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6001 return 0;
6002
6003 args = TYPE_ARG_TYPES (type);
6004 if (TREE_CODE (type) == METHOD_TYPE)
6005 args = TREE_CHAIN (args);
6006 for (; args != end_params_node; args = TREE_CHAIN (args))
6007 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6008 return 0;
6009
6010 return 1;
6011}
6012
34d4df06
APB
6013/* Visible interface to check methods contained in CLASS_DECL */
6014
6015void
6016java_check_methods (class_decl)
6017 tree class_decl;
6018{
6019 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6020 return;
6021
6022 if (CLASS_INTERFACE (class_decl))
6023 java_check_abstract_methods (class_decl);
6024 else
6025 java_check_regular_methods (class_decl);
6026
6027 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6028}
6029
d77613be
APB
6030/* Check all the methods of CLASS_DECL. Methods are first completed
6031 then checked according to regular method existance rules. If no
6032 constructor for CLASS_DECL were encountered, then build its
6033 declaration. */
e04a16fb
AG
6034
6035static void
6036java_check_regular_methods (class_decl)
6037 tree class_decl;
6038{
c2952b01 6039 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6040 tree method;
6041 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6042 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6043 tree mthrows;
6044
6045 /* It is not necessary to check methods defined in java.lang.Object */
6046 if (class == object_type_node)
6047 return;
e04a16fb 6048
23a79c61
APB
6049 if (!TYPE_NVIRTUALS (class))
6050 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6051
6052 /* Should take interfaces into account. FIXME */
6053 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6054 {
5e942c50 6055 tree sig;
e04a16fb
AG
6056 tree method_wfl = DECL_NAME (method);
6057 int aflags;
6058
5e942c50
APB
6059 /* If we previously found something and its name was saved,
6060 reinstall it now */
6061 if (found && saved_found_wfl)
ba179f9f
APB
6062 {
6063 DECL_NAME (found) = saved_found_wfl;
6064 saved_found_wfl = NULL_TREE;
6065 }
5e942c50 6066
e04a16fb
AG
6067 /* Check for redefinitions */
6068 if (check_method_redefinition (class, method))
6069 continue;
6070
22eed1e6
APB
6071 /* If we see one constructor a mark so we don't generate the
6072 default one. Also skip other verifications: constructors
6073 can't be inherited hence hiden or overriden */
6074 if (DECL_CONSTRUCTOR_P (method))
6075 {
6076 saw_constructor = 1;
6077 continue;
6078 }
6079
c877974e
APB
6080 /* We verify things thrown by the method. They must inherits from
6081 java.lang.Throwable */
6082 for (mthrows = DECL_FUNCTION_THROWS (method);
6083 mthrows; mthrows = TREE_CHAIN (mthrows))
6084 {
6085 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6086 parse_error_context
781b0558 6087 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6088 IDENTIFIER_POINTER
6089 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6090 }
6091
e04a16fb 6092 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6093 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6094
c2952b01
APB
6095 /* Inner class can't declare static methods */
6096 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6097 {
6098 char *t = xstrdup (lang_printable_name (class, 0));
6099 parse_error_context
6100 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6101 lang_printable_name (method, 0), t);
6102 free (t);
6103 }
6104
5e942c50 6105 /* Nothing overrides or it's a private method. */
aabd7048 6106 if (!found)
5e942c50 6107 continue;
aabd7048
PB
6108 if (METHOD_PRIVATE (found))
6109 {
6110 found = NULL_TREE;
6111 continue;
6112 }
5e942c50
APB
6113
6114 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6115 We set it temporarily for the sake of the error report. */
6116 saved_found_wfl = DECL_NAME (found);
6117 reset_method_name (found);
6118
614eaae0
APB
6119 /* If `found' is declared in an interface, make sure the
6120 modifier matches. */
6121 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6122 && clinit_identifier_node != DECL_NAME (found)
6123 && !METHOD_PUBLIC (method))
6124 {
6125 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6126 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6127 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6128 lang_printable_name (method, 0),
6129 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6130 }
6131
e04a16fb
AG
6132 /* Can't override a method with the same name and different return
6133 types. */
6134 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6135 {
614eaae0
APB
6136 char *t = xstrdup
6137 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6138 parse_error_context
7f10c2e2 6139 (method_wfl,
b9f7e36c 6140 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6141 lang_printable_name (found, 0), t,
b9f7e36c
APB
6142 IDENTIFIER_POINTER
6143 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6144 free (t);
6145 }
e04a16fb 6146
7f10c2e2
APB
6147 aflags = get_access_flags_from_decl (found);
6148 /* If the method has default, access in an other package, then
6149 issue a warning that the current method doesn't override the
6150 one that was found elsewhere. Do not issue this warning when
6151 the match was found in java.lang.Object. */
6152 if (DECL_CONTEXT (found) != object_type_node
a003f638 6153 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6154 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6155 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6156 && flag_not_overriding)
6157 {
6158 parse_warning_context
781b0558 6159 (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
6160 lang_printable_name (found, 0),
6161 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6162 IDENTIFIER_POINTER (DECL_NAME
6163 (TYPE_NAME (DECL_CONTEXT (found)))));
6164 continue;
6165 }
6166
e04a16fb
AG
6167 /* Can't override final. Can't override static. */
6168 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6169 {
6170 /* Static *can* override static */
6171 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6172 continue;
6173 parse_error_context
6174 (method_wfl,
6175 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6176 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6177 lang_printable_name (found, 0),
e04a16fb
AG
6178 (METHOD_FINAL (found) ? "final" : "static"),
6179 IDENTIFIER_POINTER
6180 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6181 continue;
6182 }
7f10c2e2 6183
e04a16fb
AG
6184 /* Static method can't override instance method. */
6185 if (METHOD_STATIC (method))
6186 {
6187 parse_error_context
6188 (method_wfl,
781b0558 6189 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6190 lang_printable_name (found, 0),
e04a16fb
AG
6191 IDENTIFIER_POINTER
6192 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6193 continue;
6194 }
5e942c50 6195
5e942c50
APB
6196 /* - Overriding/hiding public must be public
6197 - Overriding/hiding protected must be protected or public
6198 - If the overriden or hidden method has default (package)
6199 access, then the overriding or hiding method must not be
614eaae0
APB
6200 private; otherwise, a compile-time error occurs. If
6201 `found' belongs to an interface, things have been already
6202 taken care of. */
6203 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6204 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6205 || (METHOD_PROTECTED (found)
6206 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6207 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6208 && METHOD_PRIVATE (method))))
e04a16fb
AG
6209 {
6210 parse_error_context
6211 (method_wfl,
781b0558 6212 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6213 (METHOD_PUBLIC (method) ? "public" :
6214 (METHOD_PRIVATE (method) ? "private" : "protected")),
6215 IDENTIFIER_POINTER (DECL_NAME
6216 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6217 continue;
6218 }
6219
b9f7e36c
APB
6220 /* Overriding methods must have compatible `throws' clauses on checked
6221 exceptions, if any */
6222 check_throws_clauses (method, method_wfl, found);
6223
e04a16fb
AG
6224 /* Inheriting multiple methods with the same signature. FIXME */
6225 }
6226
5e942c50
APB
6227 /* Don't forget eventual pending found and saved_found_wfl. Take
6228 into account that we might have exited because we saw an
d77613be 6229 artificial method as the last entry. */
5e942c50
APB
6230
6231 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6232 DECL_NAME (found) = saved_found_wfl;
6233
23a79c61
APB
6234 if (!TYPE_NVIRTUALS (class))
6235 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6236
d77613be
APB
6237 /* Search for inherited abstract method not yet implemented in this
6238 class. */
6239 java_check_abstract_method_definitions (class_decl);
6240
22eed1e6 6241 if (!saw_constructor)
e920ebc9 6242 fatal ("No constructor found");
e04a16fb
AG
6243}
6244
b9f7e36c
APB
6245/* Return a non zero value if the `throws' clause of METHOD (if any)
6246 is incompatible with the `throws' clause of FOUND (if any). */
6247
6248static void
6249check_throws_clauses (method, method_wfl, found)
6250 tree method, method_wfl, found;
6251{
6252 tree mthrows, fthrows;
6253
c877974e
APB
6254 /* Can't check these things with class loaded from bytecode. FIXME */
6255 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6256 return;
6257
b9f7e36c
APB
6258 for (mthrows = DECL_FUNCTION_THROWS (method);
6259 mthrows; mthrows = TREE_CHAIN (mthrows))
6260 {
6261 /* We don't verify unchecked expressions */
c877974e 6262 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6263 continue;
6264 /* Checked expression must be compatible */
6265 for (fthrows = DECL_FUNCTION_THROWS (found);
6266 fthrows; fthrows = TREE_CHAIN (fthrows))
6267 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6268 break;
6269 if (!fthrows)
6270 {
6271 parse_error_context
781b0558 6272 (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 6273 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6274 lang_printable_name (found, 0),
b9f7e36c
APB
6275 IDENTIFIER_POINTER
6276 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6277 }
6278 }
6279}
6280
e04a16fb
AG
6281/* Check abstract method of interface INTERFACE */
6282
6283static void
5e942c50
APB
6284java_check_abstract_methods (interface_decl)
6285 tree interface_decl;
e04a16fb
AG
6286{
6287 int i, n;
6288 tree method, basetype_vec, found;
5e942c50 6289 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6290
6291 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6292 {
b9f7e36c 6293 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6294
6295 /* 2- Check for double definition inside the defining interface */
6296 if (check_method_redefinition (interface, method))
6297 continue;
6298
6299 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6300 the thrown exceptions (FIXME) */
e04a16fb
AG
6301 found = lookup_java_interface_method2 (interface, method);
6302 if (found)
6303 {
5e942c50
APB
6304 char *t;
6305 tree saved_found_wfl = DECL_NAME (found);
6306 reset_method_name (found);
c2e3db92 6307 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6308 parse_error_context
b9f7e36c 6309 (method_wfl,
5e942c50 6310 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6311 lang_printable_name (found, 0), t,
b9f7e36c
APB
6312 IDENTIFIER_POINTER
6313 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6314 free (t);
5e942c50 6315 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6316 continue;
e04a16fb
AG
6317 }
6318 }
6319
6320 /* 4- Inherited methods can't differ by their returned types */
6321 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6322 return;
6323 n = TREE_VEC_LENGTH (basetype_vec);
6324 for (i = 0; i < n; i++)
6325 {
6326 tree sub_interface_method, sub_interface;
6327 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6328 if (!vec_elt)
6329 continue;
6330 sub_interface = BINFO_TYPE (vec_elt);
6331 for (sub_interface_method = TYPE_METHODS (sub_interface);
6332 sub_interface_method;
6333 sub_interface_method = TREE_CHAIN (sub_interface_method))
6334 {
6335 found = lookup_java_interface_method2 (interface,
6336 sub_interface_method);
6337 if (found && (found != sub_interface_method))
5e942c50
APB
6338 {
6339 tree saved_found_wfl = DECL_NAME (found);
6340 reset_method_name (found);
6341 parse_error_context
6342 (lookup_cl (sub_interface_method),
781b0558 6343 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6344 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6345 lang_printable_name (found, 0),
6346 IDENTIFIER_POINTER
6347 (DECL_NAME (TYPE_NAME
6348 (DECL_CONTEXT (sub_interface_method)))),
6349 IDENTIFIER_POINTER
6350 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6351 DECL_NAME (found) = saved_found_wfl;
6352 }
e04a16fb
AG
6353 }
6354 }
6355}
6356
e04a16fb
AG
6357/* Lookup methods in interfaces using their name and partial
6358 signature. Return a matching method only if their types differ. */
6359
6360static tree
6361lookup_java_interface_method2 (class, method_decl)
6362 tree class, method_decl;
6363{
6364 int i, n;
6365 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6366
6367 if (!basetype_vec)
6368 return NULL_TREE;
6369
6370 n = TREE_VEC_LENGTH (basetype_vec);
6371 for (i = 0; i < n; i++)
6372 {
6373 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6374 if ((BINFO_TYPE (vec_elt) != object_type_node)
6375 && (to_return =
6376 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6377 return to_return;
6378 }
6379 for (i = 0; i < n; i++)
6380 {
6381 to_return = lookup_java_interface_method2
6382 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6383 if (to_return)
6384 return to_return;
6385 }
6386
6387 return NULL_TREE;
6388}
6389
6390/* Lookup method using their name and partial signature. Return a
6391 matching method only if their types differ. */
6392
6393static tree
6394lookup_java_method2 (clas, method_decl, do_interface)
6395 tree clas, method_decl;
6396 int do_interface;
6397{
5e942c50
APB
6398 tree method, method_signature, method_name, method_type, name;
6399
e04a16fb 6400 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6401 name = DECL_NAME (method_decl);
6402 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6403 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6404 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6405
6406 while (clas != NULL_TREE)
6407 {
6408 for (method = TYPE_METHODS (clas);
6409 method != NULL_TREE; method = TREE_CHAIN (method))
6410 {
6411 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6412 tree name = DECL_NAME (method);
6413 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6414 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6415 && method_sig == method_signature
6416 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6417 return method;
e04a16fb
AG
6418 }
6419 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6420 }
6421 return NULL_TREE;
6422}
6423
f441f671
APB
6424/* Return the line that matches DECL line number, and try its best to
6425 position the column number. Used during error reports. */
e04a16fb
AG
6426
6427static tree
6428lookup_cl (decl)
6429 tree decl;
6430{
6431 static tree cl = NULL_TREE;
f441f671 6432 char *line, *found;
e04a16fb
AG
6433
6434 if (!decl)
6435 return NULL_TREE;
6436
6437 if (cl == NULL_TREE)
19e223db
MM
6438 {
6439 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6440 ggc_add_tree_root (&cl, 1);
6441 }
e04a16fb
AG
6442
6443 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6444 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6445
f441f671
APB
6446 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6447 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6448
6449 found = strstr ((const char *)line,
6450 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6451 if (found)
6452 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6453
e04a16fb
AG
6454 return cl;
6455}
6456
6457/* Look for a simple name in the single-type import list */
6458
6459static tree
6460find_name_in_single_imports (name)
6461 tree name;
6462{
6463 tree node;
6464
6465 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6466 if (TREE_VALUE (node) == name)
6467 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6468
6469 return NULL_TREE;
6470}
6471
6472/* Process all single-type import. */
6473
6474static int
6475process_imports ()
6476{
6477 tree import;
6478 int error_found;
6479
6480 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6481 {
6482 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6483
6484 /* Don't load twice something already defined. */
6485 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6486 continue;
6487 QUALIFIED_P (to_be_found) = 1;
6488 load_class (to_be_found, 0);
6489 error_found =
6490 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6491 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6492 {
6493 parse_error_context (TREE_PURPOSE (import),
6494 "Class or interface `%s' not found in import",
6495 IDENTIFIER_POINTER (to_be_found));
6496 return 1;
6497 }
6498 if (error_found)
6499 return 1;
6500 }
6501 return 0;
6502}
6503
9a7ab4b3
APB
6504/* Possibly find and mark a class imported by a single-type import
6505 statement. */
e04a16fb 6506
9a7ab4b3 6507static void
e04a16fb
AG
6508find_in_imports (class_type)
6509 tree class_type;
6510{
6511 tree import;
6512
6513 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6514 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6515 {
6516 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6517 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6518 }
e04a16fb
AG
6519}
6520
e04a16fb 6521static int
63a212ed 6522note_possible_classname (name, len)
49f48c71 6523 const char *name;
63a212ed 6524 int len;
e04a16fb 6525{
63a212ed
PB
6526 tree node;
6527 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6528 len = len - 5;
6529 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6530 len = len - 6;
e04a16fb 6531 else
63a212ed
PB
6532 return 0;
6533 node = ident_subst (name, len, "", '/', '.', "");
6534 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6535 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6536 return 1;
e04a16fb
AG
6537}
6538
6539/* Read a import directory, gathering potential match for further type
6540 references. Indifferently reads a filesystem or a ZIP archive
6541 directory. */
6542
6543static void
6544read_import_dir (wfl)
6545 tree wfl;
6546{
63a212ed 6547 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6548 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6549 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6550 DIR *dirp = NULL;
d8fccff5 6551 JCF *saved_jcf = current_jcf;
e04a16fb 6552
63a212ed
PB
6553 int found = 0;
6554 int k;
6555 void *entry;
6556 struct buffer filename[1];
6557
6558
6559 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6560 return;
6561 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6562
6563 BUFFER_INIT (filename);
6564 buffer_grow (filename, package_length + 100);
6565
6566 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6567 {
49f48c71 6568 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6569 int entry_length = strlen (entry_name);
6570 if (jcf_path_is_zipfile (entry))
6571 {
6572 ZipFile *zipf;
6573 buffer_grow (filename, entry_length);
6574 memcpy (filename->data, entry_name, entry_length - 1);
6575 filename->data[entry_length-1] = '\0';
6576 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6577 if (zipf == NULL)
6578 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6579 else
6580 {
6581 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6582 BUFFER_RESET (filename);
6583 for (k = 0; k < package_length; k++)
6584 {
6585 char ch = package_name[k];
6586 *filename->ptr++ = ch == '.' ? '/' : ch;
6587 }
6588 *filename->ptr++ = '/';
6589
345137c7 6590 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6591 {
49f48c71 6592 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6593 int current_entry_len = zipd->filename_length;
6594
345137c7
TT
6595 if (current_entry_len >= BUFFER_LENGTH (filename)
6596 && strncmp (filename->data, current_entry,
6597 BUFFER_LENGTH (filename)) != 0)
63a212ed 6598 continue;
345137c7 6599 found |= note_possible_classname (current_entry,
63a212ed
PB
6600 current_entry_len);
6601 }
6602 }
6603 }
6604 else
6605 {
6606 BUFFER_RESET (filename);
6607 buffer_grow (filename, entry_length + package_length + 4);
6608 strcpy (filename->data, entry_name);
6609 filename->ptr = filename->data + entry_length;
6610 for (k = 0; k < package_length; k++)
6611 {
6612 char ch = package_name[k];
6613 *filename->ptr++ = ch == '.' ? '/' : ch;
6614 }
6615 *filename->ptr = '\0';
6616
6617 dirp = opendir (filename->data);
6618 if (dirp == NULL)
6619 continue;
6620 *filename->ptr++ = '/';
6621 for (;;)
6622 {
63a212ed 6623 int len;
49f48c71 6624 const char *d_name;
63a212ed
PB
6625 struct dirent *direntp = readdir (dirp);
6626 if (!direntp)
6627 break;
6628 d_name = direntp->d_name;
6629 len = strlen (direntp->d_name);
6630 buffer_grow (filename, len+1);
6631 strcpy (filename->ptr, d_name);
345137c7 6632 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6633 package_length+len+1);
6634 }
6635 if (dirp)
6636 closedir (dirp);
6637 }
6638 }
e04a16fb 6639
63a212ed 6640 free (filename->data);
e04a16fb 6641
63a212ed
PB
6642 /* Here we should have a unified way of retrieving an entry, to be
6643 indexed. */
6644 if (!found)
e04a16fb
AG
6645 {
6646 static int first = 1;
6647 if (first)
6648 {
781b0558 6649 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6650 java_error_count++;
6651 first = 0;
6652 }
6653 else
63a212ed
PB
6654 parse_error_context (wfl, "Package `%s' not found in import",
6655 package_name);
e04a16fb
AG
6656 current_jcf = saved_jcf;
6657 return;
6658 }
e04a16fb
AG
6659 current_jcf = saved_jcf;
6660}
6661
6662/* Possibly find a type in the import on demands specified
6663 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6664 entire list, to detected potential double definitions. */
6665
6666static int
6667find_in_imports_on_demand (class_type)
6668 tree class_type;
6669{
ab3a6dd6 6670 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6671 int seen_once = -1;
ab3a6dd6 6672 tree cl = NULL_TREE;
e04a16fb
AG
6673
6674 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6675 {
49f48c71 6676 const char *id_name;
e04a16fb
AG
6677 obstack_grow (&temporary_obstack,
6678 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6679 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6680 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6681 obstack_grow0 (&temporary_obstack,
6682 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6683 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6684 id_name = obstack_finish (&temporary_obstack);
6685
6686 node = maybe_get_identifier (id_name);
6687 if (node && IS_A_CLASSFILE_NAME (node))
6688 {
6689 if (seen_once < 0)
6690 {
6691 cl = TREE_PURPOSE (import);
6692 seen_once = 1;
6693 node_to_use = node;
6694 }
6695 else
6696 {
6697 seen_once++;
6698 parse_error_context
1e12ab9b
APB
6699 (TREE_PURPOSE (import),
6700 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6701 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6702 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6703 }
6704 }
6705 }
6706
6707 if (seen_once == 1)
6708 {
6709 /* Setup lineno so that it refers to the line of the import (in
6710 case we parse a class file and encounter errors */
6711 tree decl;
6712 int saved_lineno = lineno;
6713 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6714 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6715 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6716 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6717 /* If there is no DECL set for the class or if the class isn't
6718 loaded and not seen in source yet, the load */
6719 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6720 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6721 load_class (node_to_use, 0);
6722 lineno = saved_lineno;
6723 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6724 }
6725 else
6726 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6727}
6728
9a7ab4b3
APB
6729/* Add package NAME to the list of package encountered so far. To
6730 speed up class lookup in do_resolve_class, we make sure a
6731 particular package is added only once. */
6732
6733static void
6734register_package (name)
6735 tree name;
6736{
6737 static struct hash_table _pht, *pht = NULL;
6738
6739 if (!pht)
6740 {
6741 hash_table_init (&_pht, hash_newfunc,
6742 java_hash_hash_tree_node, java_hash_compare_tree_node);
6743 pht = &_pht;
6744 }
6745
6746 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6747 {
6748 package_list = chainon (package_list, build_tree_list (name, NULL));
6749 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6750 }
6751}
6752
5e942c50
APB
6753static tree
6754resolve_package (pkg, next)
6755 tree pkg, *next;
6756{
c2952b01 6757 tree current, acc;
5e942c50 6758 tree type_name = NULL_TREE;
49f48c71 6759 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6760
6761 /* The trick is to determine when the package name stops and were
6762 the name of something contained in the package starts. Then we
6763 return a fully qualified name of what we want to get. */
6764
6765 /* Do a quick search on well known package names */
6766 if (!strncmp (name, "java.lang.reflect", 17))
6767 {
6768 *next =
6769 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6770 type_name = lookup_package_type (name, 17);
6771 }
6772 else if (!strncmp (name, "java.lang", 9))
6773 {
6774 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6775 type_name = lookup_package_type (name, 9);
6776 }
5e942c50 6777
2c56429a
APB
6778 /* If we found something here, return */
6779 if (type_name)
6780 return type_name;
6781
6782 *next = EXPR_WFL_QUALIFICATION (pkg);
6783
6784 /* Try the current package. */
6785 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6786 IDENTIFIER_LENGTH (ctxp->package)))
6787 {
6788 type_name =
6789 lookup_package_type_and_set_next (name,
6790 IDENTIFIER_LENGTH (ctxp->package),
6791 next );
6792 if (type_name)
6793 return type_name;
6794 }
6795
6796 /* Search in imported package */
6797 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6798 {
6799 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6800 int len = IDENTIFIER_LENGTH (current_pkg_name);
6801 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6802 {
6803 tree left, dummy;
6804
6805 breakdown_qualified (&left, &dummy, current_pkg_name);
6806 len = IDENTIFIER_LENGTH (left);
6807 type_name = lookup_package_type_and_set_next (name, len, next);
6808 if (type_name)
6809 break;
6810 }
6811 }
6812
c2952b01
APB
6813 /* Try to progressively construct a type name */
6814 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6815 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6816 current; current = TREE_CHAIN (current))
6817 {
6818 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6819 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6820 {
6821 type_name = acc;
6b48deee
APB
6822 /* resolve_package should be used in a loop, hence we
6823 point at this one to naturally process the next one at
6824 the next iteration. */
6825 *next = current;
c2952b01
APB
6826 break;
6827 }
6828 }
2c56429a
APB
6829 return type_name;
6830}
6831
6832static tree
6833lookup_package_type_and_set_next (name, len, next)
49f48c71 6834 const char *name;
2c56429a
APB
6835 int len;
6836 tree *next;
6837{
49f48c71 6838 const char *ptr;
2c56429a
APB
6839 tree type_name = lookup_package_type (name, len);
6840
6841 if (!type_name)
6842 return NULL;
6843
6844 ptr = IDENTIFIER_POINTER (type_name);
6845 while (ptr && (ptr = strchr (ptr, '.')))
6846 {
6847 *next = TREE_CHAIN (*next);
6848 ptr++;
6849 }
5e942c50
APB
6850 return type_name;
6851}
6852
6853static tree
6854lookup_package_type (name, from)
49f48c71 6855 const char *name;
5e942c50
APB
6856 int from;
6857{
6858 char subname [128];
49f48c71 6859 const char *sub = &name[from+1];
5e942c50
APB
6860 while (*sub != '.' && *sub)
6861 sub++;
6862 strncpy (subname, name, sub-name);
6863 subname [sub-name] = '\0';
6864 return get_identifier (subname);
6865}
6866
cf1748bf 6867static void
4dbf4496
APB
6868check_inner_class_access (decl, enclosing_decl, cl)
6869 tree decl, enclosing_decl, cl;
cf1748bf 6870{
4dbf4496
APB
6871 int access = 0;
6872
cf1748bf 6873 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6874 as a result of processing a JDEP crafted by source_start_java_method
6875 for the purpose of patching its parm decl. But the error would
6876 have been already trapped when fixing the method's signature.
6877 DECL can also be NULL in case of earlier errors. */
6878 if (!decl || !cl)
cf1748bf
APB
6879 return;
6880
4dbf4496
APB
6881 /* We grant access to private and protected inner classes if the
6882 location from where we're trying to access DECL is an enclosing
6883 context for DECL or if both have a common enclosing context. */
6884 if (CLASS_PRIVATE (decl))
6885 access = 1;
6886 if (CLASS_PROTECTED (decl))
6887 access = 2;
6888 if (!access)
6889 return;
6890
6891 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6892 TREE_TYPE (decl))
6893 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6894 TREE_TYPE (decl)))
6895 return;
6896
6897 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6898 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6899 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6900 lang_printable_name (decl, 0));
6901}
6902
e04a16fb
AG
6903/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6904 access violations were found, 1 otherwise. */
6905
6906static int
6907check_pkg_class_access (class_name, cl)
6908 tree class_name;
6909 tree cl;
6910{
6911 tree type;
e04a16fb
AG
6912
6913 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6914 return 0;
6915
6916 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6917 return 0;
6918
6919 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6920 {
e28cd97b
APB
6921 /* Access to a private class within the same package is
6922 allowed. */
6923 tree l, r;
6924 breakdown_qualified (&l, &r, class_name);
6925 if (l == ctxp->package)
6926 return 0;
6927
e04a16fb 6928 parse_error_context
781b0558 6929 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6930 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6931 IDENTIFIER_POINTER (class_name));
6932 return 1;
6933 }
6934 return 0;
6935}
6936
6937/* Local variable declaration. */
6938
6939static void
6940declare_local_variables (modifier, type, vlist)
6941 int modifier;
6942 tree type;
6943 tree vlist;
6944{
c583dd46
APB
6945 tree decl, current, saved_type;
6946 tree type_wfl = NULL_TREE;
e04a16fb 6947 int must_chain = 0;
c2952b01 6948 int final_p = 0;
e04a16fb 6949
2aa11e97 6950 /* Push a new block if statements were seen between the last time we
e04a16fb 6951 pushed a block and now. Keep a cound of block to close */
f099f336 6952 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6953 {
f099f336 6954 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6955 tree b = enter_block ();
f099f336 6956 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6957 }
6958
6959 if (modifier)
6960 {
6961 int i;
6962 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6963 if (modifier == ACC_FINAL)
c2952b01 6964 final_p = 1;
c877974e
APB
6965 else
6966 {
6967 parse_error_context
6968 (ctxp->modifier_ctx [i],
6969 "Only `final' is allowed as a local variables modifier");
6970 return;
6971 }
e04a16fb
AG
6972 }
6973
c583dd46
APB
6974 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6975 hold the TYPE value if a new incomplete has to be created (as
6976 opposed to being found already existing and reused). */
6977 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6978
6979 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6980 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6981
6982 /* Go through all the declared variables */
6983 for (current = vlist, saved_type = type; current;
6984 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6985 {
c877974e 6986 tree other, real_type;
e04a16fb
AG
6987 tree wfl = TREE_PURPOSE (current);
6988 tree name = EXPR_WFL_NODE (wfl);
6989 tree init = TREE_VALUE (current);
e04a16fb 6990
c583dd46
APB
6991 /* Process NAME, as it may specify extra dimension(s) for it */
6992 type = build_array_from_name (type, type_wfl, name, &name);
6993
6994 /* Variable redefinition check */
6995 if ((other = lookup_name_in_blocks (name)))
6996 {
6997 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6998 DECL_SOURCE_LINE (other));
6999 continue;
7000 }
7001
7002 /* Type adjustment. We may have just readjusted TYPE because
7003 the variable specified more dimensions. Make sure we have
7004 a reference if we can and don't have one already. */
1886c9d8 7005 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
7006
7007 real_type = GET_REAL_TYPE (type);
c583dd46
APB
7008 /* Never layout this decl. This will be done when its scope
7009 will be entered */
c877974e 7010 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 7011 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
7012 BLOCK_CHAIN_DECL (decl);
7013
d4370213
APB
7014 /* If doing xreferencing, replace the line number with the WFL
7015 compound value */
7016 if (flag_emit_xref)
7017 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7018
e04a16fb
AG
7019 /* Don't try to use an INIT statement when an error was found */
7020 if (init && java_error_count)
7021 init = NULL_TREE;
c583dd46
APB
7022
7023 /* Add the initialization function to the current function's code */
7024 if (init)
e04a16fb 7025 {
c583dd46
APB
7026 /* Name might have been readjusted */
7027 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7028 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7029 java_method_add_stmt (current_function_decl,
7030 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7031 init));
7032 }
7033
7034 /* Setup dependency the type of the decl */
7035 if (must_chain)
7036 {
7037 jdep *dep;
7038 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7039 dep = CLASSD_LAST (ctxp->classd_list);
7040 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7041 }
7042 }
7043 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7044}
7045
7046/* Called during parsing. Build decls from argument list. */
7047
7048static void
7049source_start_java_method (fndecl)
7050 tree fndecl;
7051{
7052 tree tem;
7053 tree parm_decl;
7054 int i;
7055
79d13333
APB
7056 if (!fndecl)
7057 return;
7058
e04a16fb
AG
7059 current_function_decl = fndecl;
7060
7061 /* New scope for the function */
7062 enter_block ();
7063 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7064 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7065 {
7066 tree type = TREE_VALUE (tem);
7067 tree name = TREE_PURPOSE (tem);
7068
23a79c61
APB
7069 /* If type is incomplete. Create an incomplete decl and ask for
7070 the decl to be patched later */
e04a16fb
AG
7071 if (INCOMPLETE_TYPE_P (type))
7072 {
7073 jdep *jdep;
c877974e
APB
7074 tree real_type = GET_REAL_TYPE (type);
7075 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7076 type = obtain_incomplete_type (type);
e04a16fb
AG
7077 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7078 jdep = CLASSD_LAST (ctxp->classd_list);
7079 JDEP_MISC (jdep) = name;
7080 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7081 }
7082 else
7083 parm_decl = build_decl (PARM_DECL, name, type);
7084
c2952b01
APB
7085 /* Remember if a local variable was declared final (via its
7086 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7087 if (ARG_FINAL_P (tem))
7088 LOCAL_FINAL (parm_decl) = 1;
7089
e04a16fb
AG
7090 BLOCK_CHAIN_DECL (parm_decl);
7091 }
7092 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7093 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7094 nreverse (tem);
7095 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7096 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7097}
7098
22eed1e6
APB
7099/* Called during parsing. Creates an artificial method declaration. */
7100
7101static tree
7102create_artificial_method (class, flags, type, name, args)
7103 tree class;
7104 int flags;
7105 tree type, name, args;
7106{
22eed1e6
APB
7107 tree mdecl;
7108
c2952b01 7109 java_parser_context_save_global ();
22eed1e6
APB
7110 lineno = 0;
7111 mdecl = make_node (FUNCTION_TYPE);
7112 TREE_TYPE (mdecl) = type;
7113 TYPE_ARG_TYPES (mdecl) = args;
7114 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7115 java_parser_context_restore_global ();
22eed1e6
APB
7116 DECL_ARTIFICIAL (mdecl) = 1;
7117 return mdecl;
7118}
7119
7120/* Starts the body if an artifical method. */
7121
7122static void
7123start_artificial_method_body (mdecl)
7124 tree mdecl;
7125{
7126 DECL_SOURCE_LINE (mdecl) = 1;
7127 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7128 source_start_java_method (mdecl);
7129 enter_block ();
7130}
7131
7132static void
7133end_artificial_method_body (mdecl)
7134 tree mdecl;
7135{
7136 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7137 exit_block ();
7138}
7139
e04a16fb
AG
7140/* Called during expansion. Push decls formerly built from argument
7141 list so they're usable during expansion. */
7142
7143static void
7144expand_start_java_method (fndecl)
7145 tree fndecl;
7146{
7147 tree tem, *ptr;
e04a16fb 7148
e04a16fb
AG
7149 current_function_decl = fndecl;
7150
c2952b01
APB
7151 if (! quiet_flag)
7152 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7153 announce_function (fndecl);
c2952b01
APB
7154 if (! quiet_flag)
7155 fprintf (stderr, "]");
7156
7157 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7158 ptr = &DECL_ARGUMENTS (fndecl);
7159 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7160 while (tem)
7161 {
7162 tree next = TREE_CHAIN (tem);
b67d701b 7163 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7164 if (PROMOTE_PROTOTYPES
7165 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7166 && INTEGRAL_TYPE_P (type))
7167 type = integer_type_node;
b67d701b 7168 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7169 layout_decl (tem, 0);
7170 pushdecl (tem);
e04a16fb
AG
7171 *ptr = tem;
7172 ptr = &TREE_CHAIN (tem);
7173 tem = next;
7174 }
7175 *ptr = NULL_TREE;
7176 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7177 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7178}
7179
7180/* Terminate a function and expand its body. */
7181
7182static void
7183source_end_java_method ()
7184{
7185 tree fndecl = current_function_decl;
138657ec 7186 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7187
79d13333
APB
7188 if (!fndecl)
7189 return;
7190
e04a16fb
AG
7191 java_parser_context_save_global ();
7192 lineno = ctxp->last_ccb_indent1;
7193
b67d701b
PB
7194 /* Set EH language codes */
7195 java_set_exception_lang_code ();
7196
5423609c
APB
7197 /* Turn function bodies with only a NOP expr null, so they don't get
7198 generated at all and we won't get warnings when using the -W
7199 -Wall flags. */
7200 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7201 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7202
e04a16fb
AG
7203 /* Generate function's code */
7204 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7205 && ! flag_emit_class_files
7206 && ! flag_emit_xref)
e04a16fb
AG
7207 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7208
7209 /* pop out of its parameters */
7210 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7211 poplevel (1, 0, 1);
7212 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7213
7214 /* Generate rtl for function exit. */
e8fc7396 7215 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7216 {
7217 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7218 /* Emit catch-finally clauses */
7219 emit_handlers ();
e04a16fb
AG
7220 expand_function_end (input_filename, lineno, 0);
7221
138657ec
AH
7222 /* FIXME: If the current method contains any exception handlers,
7223 force asynchronous_exceptions: this is necessary because signal
7224 handlers in libjava may throw exceptions. This is far from being
7225 a perfect solution, but it's better than doing nothing at all.*/
7226 if (catch_clauses)
7227 asynchronous_exceptions = 1;
7228
e04a16fb
AG
7229 /* Run the optimizers and output assembler code for this function. */
7230 rest_of_compilation (fndecl);
7231 }
7232
7233 current_function_decl = NULL_TREE;
e04a16fb 7234 java_parser_context_restore_global ();
138657ec 7235 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7236}
7237
7238/* Record EXPR in the current function block. Complements compound
7239 expression second operand if necessary. */
7240
7241tree
7242java_method_add_stmt (fndecl, expr)
7243 tree fndecl, expr;
7244{
b771925e
APB
7245 if (!GET_CURRENT_BLOCK (fndecl))
7246 return NULL_TREE;
f099f336 7247 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7248}
e04a16fb 7249
b67d701b
PB
7250static tree
7251add_stmt_to_block (b, type, stmt)
7252 tree b, type, stmt;
7253{
7254 tree body = BLOCK_EXPR_BODY (b), c;
7255
e04a16fb
AG
7256 if (java_error_count)
7257 return body;
b67d701b
PB
7258
7259 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7260 return body;
7261
b67d701b
PB
7262 BLOCK_EXPR_BODY (b) = c;
7263 TREE_SIDE_EFFECTS (c) = 1;
7264 return c;
e04a16fb
AG
7265}
7266
7267/* Add STMT to EXISTING if possible, otherwise create a new
7268 COMPOUND_EXPR and add STMT to it. */
7269
7270static tree
7271add_stmt_to_compound (existing, type, stmt)
7272 tree existing, type, stmt;
7273{
15fdcfe9
PB
7274 if (existing)
7275 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7276 else
15fdcfe9 7277 return stmt;
e04a16fb
AG
7278}
7279
1886c9d8
APB
7280void java_layout_seen_class_methods ()
7281{
7282 tree previous_list = all_class_list;
7283 tree end = NULL_TREE;
7284 tree current;
7285
7286 while (1)
7287 {
7288 for (current = previous_list;
7289 current != end; current = TREE_CHAIN (current))
7290 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7291
7292 if (previous_list != all_class_list)
7293 {
7294 end = previous_list;
7295 previous_list = all_class_list;
7296 }
7297 else
7298 break;
7299 }
7300}
7301
e04a16fb 7302void
c2952b01 7303java_reorder_fields ()
e04a16fb 7304{
c2952b01 7305 static tree stop_reordering = NULL_TREE;
19e223db 7306 static int initialized_p;
c2952b01 7307 tree current;
19e223db
MM
7308
7309 /* Register STOP_REORDERING with the garbage collector. */
7310 if (!initialized_p)
7311 {
7312 ggc_add_tree_root (&stop_reordering, 1);
7313 initialized_p = 1;
7314 }
7315
5e942c50 7316 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7317 {
5e942c50 7318 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7319
c2952b01
APB
7320 if (current_class == stop_reordering)
7321 break;
7322
c877974e
APB
7323 /* Reverse the fields, but leave the dummy field in front.
7324 Fields are already ordered for Object and Class */
7325 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7326 && current_class != class_type_node)
7327 {
23a79c61
APB
7328 /* If the dummy field is there, reverse the right fields and
7329 just layout the type for proper fields offset */
c877974e
APB
7330 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7331 {
7332 tree fields = TYPE_FIELDS (current_class);
7333 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7334 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7335 }
23a79c61
APB
7336 /* We don't have a dummy field, we need to layout the class,
7337 after having reversed the fields */
c877974e
APB
7338 else
7339 {
7340 TYPE_FIELDS (current_class) =
7341 nreverse (TYPE_FIELDS (current_class));
7342 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7343 }
7344 }
c2952b01
APB
7345 }
7346 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7347}
7348
7349/* Layout the methods of all classes loaded in one way on an
7350 other. Check methods of source parsed classes. Then reorder the
7351 fields and layout the classes or the type of all source parsed
7352 classes */
7353
7354void
7355java_layout_classes ()
7356{
7357 tree current;
7358 int save_error_count = java_error_count;
7359
7360 /* Layout the methods of all classes seen so far */
7361 java_layout_seen_class_methods ();
7362 java_parse_abort_on_error ();
7363 all_class_list = NULL_TREE;
7364
7365 /* Then check the methods of all parsed classes */
7366 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7367 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7368 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7369 java_parse_abort_on_error ();
7370
7371 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7372 {
7373 current_class = TREE_TYPE (TREE_VALUE (current));
7374 layout_class (current_class);
5e942c50 7375
c877974e
APB
7376 /* From now on, the class is considered completely loaded */
7377 CLASS_LOADED_P (current_class) = 1;
7378
5e942c50
APB
7379 /* Error reported by the caller */
7380 if (java_error_count)
7381 return;
e04a16fb 7382 }
23a79c61
APB
7383
7384 /* We might have reloaded classes durign the process of laying out
7385 classes for code generation. We must layout the methods of those
7386 late additions, as constructor checks might use them */
1886c9d8 7387 java_layout_seen_class_methods ();
23a79c61 7388 java_parse_abort_on_error ();
e04a16fb
AG
7389}
7390
c2952b01
APB
7391/* Expand methods in the current set of classes rememebered for
7392 generation. */
e04a16fb 7393
49f48c71 7394static void
c2952b01 7395java_complete_expand_classes ()
e04a16fb
AG
7396{
7397 tree current;
ce6e9147
APB
7398
7399 do_not_fold = flag_emit_xref;
c2952b01 7400
e04a16fb 7401 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7402 if (!INNER_CLASS_DECL_P (current))
7403 java_complete_expand_class (current);
7404}
e04a16fb 7405
c2952b01
APB
7406/* Expand the methods found in OUTER, starting first by OUTER's inner
7407 classes, if any. */
e04a16fb 7408
c2952b01
APB
7409static void
7410java_complete_expand_class (outer)
7411 tree outer;
7412{
7413 tree inner_list;
e04a16fb 7414
c2952b01 7415 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7416
c2952b01
APB
7417 /* We need to go after all inner classes and start expanding them,
7418 starting with most nested ones. We have to do that because nested
7419 classes might add functions to outer classes */
e04a16fb 7420
c2952b01
APB
7421 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7422 inner_list; inner_list = TREE_CHAIN (inner_list))
7423 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7424
c2952b01
APB
7425 java_complete_expand_methods (outer);
7426 set_nested_class_simple_name_value (outer, 0); /* Reset */
7427}
7428
7429/* Expand methods registered in CLASS_DECL. The general idea is that
7430 we expand regular methods first. This allows us get an estimate on
7431 how outer context local alias fields are really used so we can add
7432 to the constructor just enough code to initialize them properly (it
c00f0fb2 7433 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7434 constructors and then <clinit>. */
7435
7436static void
7437java_complete_expand_methods (class_decl)
7438 tree class_decl;
7439{
7440 tree clinit, finit, decl, first_decl;
7441
7442 current_class = TREE_TYPE (class_decl);
7443
7444 /* Initialize a new constant pool */
7445 init_outgoing_cpool ();
7446
7447 /* Pre-expand <clinit> to figure whether we really need it or
7448 not. If we do need it, we pre-expand the static fields so they're
7449 ready to be used somewhere else. <clinit> will be fully expanded
7450 after we processed the constructors. */
7451 first_decl = TYPE_METHODS (current_class);
7452 clinit = maybe_generate_pre_expand_clinit (current_class);
7453
c00f0fb2 7454 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7455 try to use it.*/
7456 if (TYPE_FINIT_STMT_LIST (current_class))
7457 {
7458 finit = generate_finit (current_class);
7459 java_complete_expand_method (finit);
7460 }
7461
7462 /* Now do the constructors */
7463 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7464 {
7465 int no_body;
7466
7467 if (!DECL_CONSTRUCTOR_P (decl))
7468 continue;
7469
7470 no_body = !DECL_FUNCTION_BODY (decl);
7471 /* Don't generate debug info on line zero when expanding a
7472 generated constructor. */
7473 if (no_body)
7474 restore_line_number_status (1);
7475
7476 java_complete_expand_method (decl);
7477
7478 if (no_body)
7479 restore_line_number_status (0);
7480 }
7481
7482 /* First, do the ordinary methods. */
7483 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7484 {
7145d9fe
TT
7485 /* Skip abstract or native methods -- but do handle native
7486 methods when generating JNI stubs. */
7487 if (METHOD_ABSTRACT (decl)
7488 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7489 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7490 continue;
7145d9fe
TT
7491
7492 if (METHOD_NATIVE (decl))
7493 {
7494 tree body = build_jni_stub (decl);
7495 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7496 }
7497
c2952b01
APB
7498 java_complete_expand_method (decl);
7499 }
7500
7501 /* If there is indeed a <clinit>, fully expand it now */
7502 if (clinit)
7503 {
7504 /* Prevent the use of `this' inside <clinit> */
7505 ctxp->explicit_constructor_p = 1;
7506 java_complete_expand_method (clinit);
7507 ctxp->explicit_constructor_p = 0;
e04a16fb 7508 }
c2952b01 7509
165f37bc
APB
7510 /* We might have generated a class$ that we now want to expand */
7511 if (TYPE_DOT_CLASS (current_class))
7512 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7513
c2952b01
APB
7514 /* Now verify constructor circularity (stop after the first one we
7515 prove wrong.) */
7516 if (!CLASS_INTERFACE (class_decl))
7517 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7518 if (DECL_CONSTRUCTOR_P (decl)
7519 && verify_constructor_circularity (decl, decl))
7520 break;
7521
7522 /* Save the constant pool. We'll need to restore it later. */
7523 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7524}
7525
c2952b01
APB
7526/* Attempt to create <clinit>. Pre-expand static fields so they can be
7527 safely used in some other methods/constructors. */
e920ebc9 7528
c2952b01
APB
7529static tree
7530maybe_generate_pre_expand_clinit (class_type)
7531 tree class_type;
e920ebc9 7532{
c2952b01
APB
7533 tree current, mdecl;
7534
7535 if (!TYPE_CLINIT_STMT_LIST (class_type))
7536 return NULL_TREE;
e920ebc9 7537
c2952b01
APB
7538 /* Go through all static fields and pre expand them */
7539 for (current = TYPE_FIELDS (class_type); current;
7540 current = TREE_CHAIN (current))
7541 if (FIELD_STATIC (current))
7542 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7543
7544 /* Then build the <clinit> method */
7545 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7546 clinit_identifier_node, end_params_node);
7547 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7548 mdecl, NULL_TREE);
7549 start_artificial_method_body (mdecl);
7550
7551 /* We process the list of assignment we produced as the result of
7552 the declaration of initialized static field and add them as
7553 statement to the <clinit> method. */
7554 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7555 current = TREE_CHAIN (current))
e920ebc9 7556 {
9a7ab4b3 7557 tree stmt = current;
c2952b01
APB
7558 /* We build the assignment expression that will initialize the
7559 field to its value. There are strict rules on static
7560 initializers (8.5). FIXME */
98a52c2c 7561 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7562 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7563 java_method_add_stmt (mdecl, stmt);
7564 }
e920ebc9 7565
c2952b01
APB
7566 end_artificial_method_body (mdecl);
7567
92d83515
APB
7568 /* Now we want to place <clinit> as the last method (because we need
7569 it at least for interface so that it doesn't interfere with the
7570 dispatch table based lookup. */
7571 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7572 {
92d83515
APB
7573 current = TREE_CHAIN (TYPE_METHODS (class_type));
7574 TYPE_METHODS (class_type) = current;
c2952b01
APB
7575
7576 while (TREE_CHAIN (current))
7577 current = TREE_CHAIN (current);
92d83515 7578
c2952b01
APB
7579 TREE_CHAIN (current) = mdecl;
7580 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7581 }
c2952b01
APB
7582
7583 return mdecl;
e920ebc9
APB
7584}
7585
92d83515
APB
7586/* See whether we could get rid of <clinit>. Criteria are: all static
7587 final fields have constant initial values and the body of <clinit>
7588 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7589
7590static int
7591maybe_yank_clinit (mdecl)
7592 tree mdecl;
7593{
7594 tree type, current;
7595 tree fbody, bbody;
99eaf8d4 7596 int found = 0;
92d83515
APB
7597
7598 if (!DECL_CLINIT_P (mdecl))
7599 return 0;
f0f3a777
APB
7600
7601 /* If the body isn't empty, then we keep <clinit>. Note that if
7602 we're emitting classfiles, this isn't enough not to rule it
7603 out. */
92d83515
APB
7604 fbody = DECL_FUNCTION_BODY (mdecl);
7605 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7606 bbody = BLOCK_EXPR_BODY (bbody);
f0f3a777 7607 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7608 return 0;
7609
7610 type = DECL_CONTEXT (mdecl);
7611 current = TYPE_FIELDS (type);
7612
7613 for (current = (current ? TREE_CHAIN (current) : current);
7614 current; current = TREE_CHAIN (current))
f0f3a777
APB
7615 {
7616 tree f_init;
7617
7618 /* We're not interested in non static field */
7619 if (!FIELD_STATIC (current))
7620 continue;
7621
7622 /* Anything that isn't String or a basic type is ruled out -- or
7623 if we now how to deal with it (when doing things natively) we
7624 should generated an empty <clinit> so that SUID are computed
7625 correctly. */
7626 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7627 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7628 break;
7629
7630 f_init = DECL_INITIAL (current);
7631 /* If we're emitting native code, we want static final fields to
7632 have constant initializers. If we don't meet these
7633 conditions, we keep <clinit> */
7634 if (!flag_emit_class_files
7635 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7636 break;
7637 /* If we're emitting bytecode, we want static fields to have
7638 constant initializers or no initializer. If we don't meet
7639 these conditions, we keep <clinit> */
7640 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7641 break;
7642 }
92d83515 7643
99eaf8d4
APB
7644 /* Now we analyze the method body and look for something that
7645 isn't a MODIFY_EXPR */
7646 if (bbody == empty_stmt_node)
7647 bbody = NULL_TREE;
7648 while (bbody)
7649 switch (TREE_CODE (bbody))
7650 {
7651 case BLOCK:
7652 bbody = BLOCK_EXPR_BODY (bbody);
7653 break;
7654
7655 case EXPR_WITH_FILE_LOCATION:
7656 bbody = EXPR_WFL_NODE (bbody);
7657 break;
7658
7659 case COMPOUND_EXPR:
7660 bbody = TREE_OPERAND (bbody, 0);
7661 break;
7662
7663 case MODIFY_EXPR:
7664 bbody = NULL_TREE;
7665 break;
7666
7667 default:
7668 bbody = NULL_TREE;
7669 found = 1;
7670 }
7671
7672 if (current || found)
92d83515
APB
7673 return 0;
7674
7675 /* Get rid of <clinit> in the class' list of methods */
7676 if (TYPE_METHODS (type) == mdecl)
7677 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7678 else
7679 for (current = TYPE_METHODS (type); current;
7680 current = TREE_CHAIN (current))
7681 if (TREE_CHAIN (current) == mdecl)
7682 {
7683 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7684 break;
7685 }
7686
7687 return 1;
7688}
7689
7690
e04a16fb
AG
7691/* Complete and expand a method. */
7692
7693static void
7694java_complete_expand_method (mdecl)
7695 tree mdecl;
7696{
92d83515
APB
7697 int yank_clinit = 0;
7698
c2952b01 7699 current_function_decl = mdecl;
22eed1e6
APB
7700 /* Fix constructors before expanding them */
7701 if (DECL_CONSTRUCTOR_P (mdecl))
7702 fix_constructors (mdecl);
e04a16fb 7703
22eed1e6 7704 /* Expand functions that have a body */
e04a16fb
AG
7705 if (DECL_FUNCTION_BODY (mdecl))
7706 {
9bbc7d9f
PB
7707 tree fbody = DECL_FUNCTION_BODY (mdecl);
7708 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7709 tree exception_copy = NULL_TREE;
e04a16fb 7710 expand_start_java_method (mdecl);
939d7216 7711 build_result_decl (mdecl);
e04a16fb
AG
7712
7713 current_this
7714 = (!METHOD_STATIC (mdecl) ?
7715 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7716
ce6e9147
APB
7717 /* Purge the `throws' list of unchecked exceptions. If we're
7718 doing xref, save a copy of the list and re-install it
7719 later. */
7720 if (flag_emit_xref)
7721 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7722
b9f7e36c
APB
7723 purge_unchecked_exceptions (mdecl);
7724
7725 /* Install exceptions thrown with `throws' */
7726 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7727
9bbc7d9f 7728 if (block_body != NULL_TREE)
bc3ca41b
PB
7729 {
7730 block_body = java_complete_tree (block_body);
c2952b01 7731
7145d9fe 7732 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7733 check_for_initialization (block_body);
f099f336 7734 ctxp->explicit_constructor_p = 0;
bc3ca41b 7735 }
e803d3b2 7736
9bbc7d9f 7737 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7738
c2952b01
APB
7739 /* If we saw a return but couldn't evaluate it properly, we'll
7740 have an error_mark_node here. */
7741 if (block_body != error_mark_node
7742 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7743 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7744 && !flag_emit_xref)
82371d41 7745 missing_return_error (current_function_decl);
7525cc04 7746
92d83515
APB
7747 /* Check wether we could just get rid of clinit, now the picture
7748 is complete. */
7749 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7750 complete_start_java_method (mdecl);
7751
e04a16fb 7752 /* Don't go any further if we've found error(s) during the
92d83515
APB
7753 expansion */
7754 if (!java_error_count && !yank_clinit)
e04a16fb 7755 source_end_java_method ();
22eed1e6
APB
7756 else
7757 {
92d83515
APB
7758 if (java_error_count)
7759 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7760 poplevel (1, 0, 1);
7761 }
b9f7e36c
APB
7762
7763 /* Pop the exceptions and sanity check */
7764 POP_EXCEPTIONS();
7765 if (currently_caught_type_list)
7766 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7767
7768 if (flag_emit_xref)
7769 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7770 }
7771}
7772
c2952b01
APB
7773\f
7774
7775/* This section of the code deals with accessing enclosing context
7776 fields either directly by using the relevant access to this$<n> or
7777 by invoking an access method crafted for that purpose. */
7778
7779/* Build the necessary access from an inner class to an outer
7780 class. This routine could be optimized to cache previous result
7781 (decl, current_class and returned access). When an access method
7782 needs to be generated, it always takes the form of a read. It might
7783 be later turned into a write by calling outer_field_access_fix. */
7784
7785static tree
7786build_outer_field_access (id, decl)
7787 tree id, decl;
7788{
7789 tree access = NULL_TREE;
7790 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7791
7792 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7793 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7794 the `private' barrier if we're not emitting bytecodes. */
7795 if (ctx == DECL_CONTEXT (decl)
7796 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7797 {
7798 tree thisn = build_current_thisn (current_class);
7799 access = make_qualified_primary (build_wfl_node (thisn),
7800 id, EXPR_WFL_LINECOL (id));
7801 }
7802 /* Otherwise, generate access methods to outer this and access the
7803 field (either using an access method or by direct access.) */
7804 else
7805 {
7806 int lc = EXPR_WFL_LINECOL (id);
7807
7808 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7809 get a hold to the enclosing instance we need, and then we
c2952b01
APB
7810 build the field access. */
7811 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7812
7813 /* If the field is private and we're generating bytecode, then
7814 we generate an access method */
7815 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7816 {
7817 tree name = build_outer_field_access_methods (decl);
7818 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7819 name, access, NULL_TREE);
7820 }
7821 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7822 Once again we break the `private' access rule from a foreign
7823 class. */
7824 else
7825 access = make_qualified_primary (access, id, lc);
7826 }
7827 return resolve_expression_name (access, NULL);
7828}
7829
7830/* Return a non zero value if NODE describes an outer field inner
7831 access. */
7832
7833static int
7834outer_field_access_p (type, decl)
7835 tree type, decl;
7836{
7837 if (!INNER_CLASS_TYPE_P (type)
7838 || TREE_CODE (decl) != FIELD_DECL
7839 || DECL_CONTEXT (decl) == type)
7840 return 0;
7841
7842 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7843 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7844 {
7845 if (type == DECL_CONTEXT (decl))
7846 return 1;
7847 if (!DECL_CONTEXT (TYPE_NAME (type)))
7848 break;
7849 }
7850
7851 return 0;
7852}
7853
7854/* Return a non zero value if NODE represents an outer field inner
7855 access that was been already expanded. As a side effect, it returns
7856 the name of the field being accessed and the argument passed to the
7857 access function, suitable for a regeneration of the access method
7858 call if necessary. */
7859
7860static int
7861outer_field_expanded_access_p (node, name, arg_type, arg)
7862 tree node, *name, *arg_type, *arg;
7863{
7864 int identified = 0;
7865
7866 if (TREE_CODE (node) != CALL_EXPR)
7867 return 0;
7868
7869 /* Well, gcj generates slightly different tree nodes when compiling
7870 to native or bytecodes. It's the case for function calls. */
7871
7872 if (flag_emit_class_files
7873 && TREE_CODE (node) == CALL_EXPR
7874 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7875 identified = 1;
7876 else if (!flag_emit_class_files)
7877 {
7878 node = TREE_OPERAND (node, 0);
7879
7880 if (node && TREE_OPERAND (node, 0)
7881 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7882 {
7883 node = TREE_OPERAND (node, 0);
7884 if (TREE_OPERAND (node, 0)
7885 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7886 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7887 (DECL_NAME (TREE_OPERAND (node, 0)))))
7888 identified = 1;
7889 }
7890 }
7891
7892 if (identified && name && arg_type && arg)
7893 {
7894 tree argument = TREE_OPERAND (node, 1);
7895 *name = DECL_NAME (TREE_OPERAND (node, 0));
7896 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7897 *arg = TREE_VALUE (argument);
7898 }
7899 return identified;
7900}
7901
7902/* Detect in NODE an outer field read access from an inner class and
7903 transform it into a write with RHS as an argument. This function is
7904 called from the java_complete_lhs when an assignment to a LHS can
7905 be identified. */
7906
7907static tree
7908outer_field_access_fix (wfl, node, rhs)
7909 tree wfl, node, rhs;
7910{
7911 tree name, arg_type, arg;
7912
7913 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7914 {
7915 /* At any rate, check whether we're trying to assign a value to
7916 a final. */
7917 tree accessed = (JDECL_P (node) ? node :
7918 (TREE_CODE (node) == COMPONENT_REF ?
7919 TREE_OPERAND (node, 1) : node));
7920 if (check_final_assignment (accessed, wfl))
7921 return error_mark_node;
7922
7923 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7924 arg_type, name, arg, rhs);
7925 return java_complete_tree (node);
7926 }
7927 return NULL_TREE;
7928}
7929
7930/* Construct the expression that calls an access method:
7931 <type>.access$<n>(<arg1> [, <arg2>]);
7932
7933 ARG2 can be NULL and will be omitted in that case. It will denote a
7934 read access. */
7935
7936static tree
7937build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7938 int lc;
7939 tree type, access_method_name, arg1, arg2;
7940{
7941 tree args, cn, access;
7942
7943 args = arg1 ? arg1 :
7944 build_wfl_node (build_current_thisn (current_class));
7945 args = build_tree_list (NULL_TREE, args);
7946
7947 if (arg2)
7948 args = tree_cons (NULL_TREE, arg2, args);
7949
7950 access = build_method_invocation (build_wfl_node (access_method_name), args);
7951 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7952 return make_qualified_primary (cn, access, lc);
7953}
7954
7955static tree
7956build_new_access_id ()
7957{
7958 static int access_n_counter = 1;
7959 char buffer [128];
7960
7961 sprintf (buffer, "access$%d", access_n_counter++);
7962 return get_identifier (buffer);
7963}
7964
7965/* Create the static access functions for the outer field DECL. We define a
7966 read:
7967 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7968 return inst$.field;
7969 }
7970 and a write access:
7971 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7972 TREE_TYPE (<field>) value$) {
7973 return inst$.field = value$;
7974 }
7975 We should have a usage flags on the DECL so we can lazily turn the ones
7976 we're using for code generation. FIXME.
7977*/
7978
7979static tree
7980build_outer_field_access_methods (decl)
7981 tree decl;
7982{
7983 tree id, args, stmt, mdecl;
7984
7985 /* Check point, to be removed. FIXME */
7986 if (FIELD_INNER_ACCESS (decl)
7987 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7988 abort ();
7989
7990 if (FIELD_INNER_ACCESS (decl))
7991 return FIELD_INNER_ACCESS (decl);
7992
c2952b01
APB
7993 /* Create the identifier and a function named after it. */
7994 id = build_new_access_id ();
7995
7996 /* The identifier is marked as bearing the name of a generated write
7997 access function for outer field accessed from inner classes. */
7998 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7999
8000 /* Create the read access */
8001 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8002 TREE_CHAIN (args) = end_params_node;
8003 stmt = make_qualified_primary (build_wfl_node (inst_id),
8004 build_wfl_node (DECL_NAME (decl)), 0);
8005 stmt = build_return (0, stmt);
8006 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8007 TREE_TYPE (decl), id, args, stmt);
8008 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8009
8010 /* Create the write access method */
8011 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8012 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8013 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8014 stmt = make_qualified_primary (build_wfl_node (inst_id),
8015 build_wfl_node (DECL_NAME (decl)), 0);
8016 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8017 build_wfl_node (wpv_id)));
8018
8019 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8020 TREE_TYPE (decl), id, args, stmt);
8021 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
c2952b01
APB
8022
8023 /* Return the access name */
8024 return FIELD_INNER_ACCESS (decl) = id;
8025}
8026
8027/* Build an field access method NAME. */
8028
8029static tree
8030build_outer_field_access_method (class, type, name, args, body)
8031 tree class, type, name, args, body;
8032{
8033 tree saved_current_function_decl, mdecl;
8034
8035 /* Create the method */
8036 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8037 fix_method_argument_names (args, mdecl);
8038 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8039
8040 /* Attach the method body. */
8041 saved_current_function_decl = current_function_decl;
8042 start_artificial_method_body (mdecl);
8043 java_method_add_stmt (mdecl, body);
8044 end_artificial_method_body (mdecl);
8045 current_function_decl = saved_current_function_decl;
8046
8047 return mdecl;
8048}
8049
8050\f
8051/* This section deals with building access function necessary for
8052 certain kinds of method invocation from inner classes. */
8053
8054static tree
8055build_outer_method_access_method (decl)
8056 tree decl;
8057{
8058 tree saved_current_function_decl, mdecl;
8059 tree args = NULL_TREE, call_args = NULL_TREE;
8060 tree carg, id, body, class;
8061 char buffer [80];
8062 int parm_id_count = 0;
8063
8064 /* Test this abort with an access to a private field */
8065 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8066 abort ();
8067
8068 /* Check the cache first */
8069 if (DECL_FUNCTION_INNER_ACCESS (decl))
8070 return DECL_FUNCTION_INNER_ACCESS (decl);
8071
8072 class = DECL_CONTEXT (decl);
8073
8074 /* Obtain an access identifier and mark it */
8075 id = build_new_access_id ();
8076 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8077
c2952b01
APB
8078 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8079 /* Create the arguments, as much as the original */
8080 for (; carg && carg != end_params_node;
8081 carg = TREE_CHAIN (carg))
8082 {
8083 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8084 args = chainon (args, build_tree_list (get_identifier (buffer),
8085 TREE_VALUE (carg)));
8086 }
8087 args = chainon (args, end_params_node);
8088
8089 /* Create the method */
8090 mdecl = create_artificial_method (class, ACC_STATIC,
8091 TREE_TYPE (TREE_TYPE (decl)), id, args);
8092 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8093 /* There is a potential bug here. We should be able to use
8094 fix_method_argument_names, but then arg names get mixed up and
8095 eventually a constructor will have its this$0 altered and the
8096 outer context won't be assignment properly. The test case is
8097 stub.java FIXME */
8098 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8099
8100 /* Attach the method body. */
8101 saved_current_function_decl = current_function_decl;
8102 start_artificial_method_body (mdecl);
8103
8104 /* The actual method invocation uses the same args. When invoking a
8105 static methods that way, we don't want to skip the first
8106 argument. */
8107 carg = args;
8108 if (!METHOD_STATIC (decl))
8109 carg = TREE_CHAIN (carg);
8110 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8111 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8112 call_args);
8113
8114 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8115 call_args);
8116 if (!METHOD_STATIC (decl))
8117 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8118 body, 0);
8119 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8120 body = build_return (0, body);
8121 java_method_add_stmt (mdecl,body);
8122 end_artificial_method_body (mdecl);
8123 current_function_decl = saved_current_function_decl;
c2952b01
APB
8124
8125 /* Back tag the access function so it know what it accesses */
8126 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8127
8128 /* Tag the current method so it knows it has an access generated */
8129 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8130}
8131
8132\f
8133/* This section of the code deals with building expressions to access
8134 the enclosing instance of an inner class. The enclosing instance is
8135 kept in a generated field called this$<n>, with <n> being the
8136 inner class nesting level (starting from 0.) */
8137
8138/* Build an access to a given this$<n>, possibly by chaining access
8139 call to others. Access methods to this$<n> are build on the fly if
8140 necessary */
8141
8142static tree
8143build_access_to_thisn (from, to, lc)
8144 tree from, to;
8145 int lc;
8146{
8147 tree access = NULL_TREE;
8148
8149 while (from != to)
8150 {
8151 tree access0_wfl, cn;
8152
8153 maybe_build_thisn_access_method (from);
8154 access0_wfl = build_wfl_node (access0_identifier_node);
8155 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8156 EXPR_WFL_LINECOL (access0_wfl) = lc;
8157
8158 if (!access)
8159 {
8160 access = build_current_thisn (current_class);
8161 access = build_wfl_node (access);
8162 }
8163 access = build_tree_list (NULL_TREE, access);
8164 access = build_method_invocation (access0_wfl, access);
8165 access = make_qualified_primary (cn, access, lc);
8166
8167 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8168 }
8169 return access;
8170}
8171
8172/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8173 is returned if nothing needs to be generated. Otherwise, the method
152de068 8174 generated and a method decl is returned.
c2952b01
APB
8175
8176 NOTE: These generated methods should be declared in a class file
8177 attribute so that they can't be referred to directly. */
8178
8179static tree
8180maybe_build_thisn_access_method (type)
8181 tree type;
8182{
8183 tree mdecl, args, stmt, rtype;
8184 tree saved_current_function_decl;
8185
8186 /* If TYPE is a top-level class, no access method is required.
8187 If there already is such an access method, bail out. */
8188 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8189 return NULL_TREE;
8190
8191 /* We generate the method. The method looks like:
8192 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8193 */
c2952b01
APB
8194 args = build_tree_list (inst_id, build_pointer_type (type));
8195 TREE_CHAIN (args) = end_params_node;
8196 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8197 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8198 access0_identifier_node, args);
8199 fix_method_argument_names (args, mdecl);
8200 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8201 stmt = build_current_thisn (type);
8202 stmt = make_qualified_primary (build_wfl_node (inst_id),
8203 build_wfl_node (stmt), 0);
8204 stmt = build_return (0, stmt);
8205
8206 saved_current_function_decl = current_function_decl;
8207 start_artificial_method_body (mdecl);
8208 java_method_add_stmt (mdecl, stmt);
8209 end_artificial_method_body (mdecl);
8210 current_function_decl = saved_current_function_decl;
c2952b01
APB
8211
8212 CLASS_ACCESS0_GENERATED_P (type) = 1;
8213
8214 return mdecl;
8215}
8216
8217/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8218 the first level of innerclassing. this$1 for the next one, etc...
8219 This function can be invoked with TYPE to NULL, available and then
8220 has to count the parser context. */
8221
8222static tree
8223build_current_thisn (type)
8224 tree type;
8225{
8226 static int saved_i = -1;
8227 static tree saved_thisn = NULL_TREE;
19e223db
MM
8228 static tree saved_type = NULL_TREE;
8229 static int saved_type_i = 0;
8230 static int initialized_p;
c2952b01
APB
8231 tree decl;
8232 char buffer [80];
8233 int i = 0;
8234
19e223db
MM
8235 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8236 if (!initialized_p)
c2952b01 8237 {
19e223db
MM
8238 ggc_add_tree_root (&saved_thisn, 1);
8239 ggc_add_tree_root (&saved_type, 1);
8240 initialized_p = 1;
8241 }
c2952b01 8242
19e223db
MM
8243 if (type)
8244 {
c2952b01
APB
8245 if (type == saved_type)
8246 i = saved_type_i;
8247 else
8248 {
8249 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8250 decl; decl = DECL_CONTEXT (decl), i++)
8251 ;
8252
8253 saved_type = type;
8254 saved_type_i = i;
8255 }
8256 }
8257 else
8258 i = list_length (GET_CPC_LIST ())-2;
8259
8260 if (i == saved_i)
8261 return saved_thisn;
8262
8263 sprintf (buffer, "this$%d", i);
8264 saved_i = i;
8265 saved_thisn = get_identifier (buffer);
8266 return saved_thisn;
8267}
8268
8269/* Return the assignement to the hidden enclosing context `this$<n>'
8270 by the second incoming parameter to the innerclass constructor. The
8271 form used is `this.this$<n> = this$<n>;'. */
8272
8273static tree
8274build_thisn_assign ()
8275{
8276 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8277 {
8278 tree thisn = build_current_thisn (current_class);
8279 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8280 build_wfl_node (thisn), 0);
8281 tree rhs = build_wfl_node (thisn);
8282 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8283 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8284 }
8285 return NULL_TREE;
8286}
8287
8288\f
165f37bc
APB
8289/* Building the synthetic `class$' used to implement the `.class' 1.1
8290 extension for non primitive types. This method looks like:
8291
8292 static Class class$(String type) throws NoClassDefFoundError
8293 {
8294 try {return (java.lang.Class.forName (String));}
8295 catch (ClassNotFoundException e) {
8296 throw new NoClassDefFoundError(e.getMessage());}
8297 } */
8298
8299static tree
8300build_dot_class_method (class)
8301 tree class;
8302{
8303#define BWF(S) build_wfl_node (get_identifier ((S)))
8304#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8305 tree args, tmp, saved_current_function_decl, mdecl;
8306 tree stmt, throw_stmt, catch, catch_block, try_block;
8307 tree catch_clause_param;
8308 tree class_not_found_exception, no_class_def_found_error;
8309
8310 static tree get_message_wfl, type_parm_wfl;
8311
8312 if (!get_message_wfl)
8313 {
8314 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8315 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
19e223db
MM
8316 ggc_add_tree_root (&get_message_wfl, 1);
8317 ggc_add_tree_root (&type_parm_wfl, 1);
165f37bc
APB
8318 }
8319
8320 /* Build the arguments */
8321 args = build_tree_list (get_identifier ("type$"),
8322 build_pointer_type (string_type_node));
8323 TREE_CHAIN (args) = end_params_node;
8324
8325 /* Build the qualified name java.lang.Class.forName */
8326 tmp = MQN (MQN (MQN (BWF ("java"),
8327 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8328
8329 /* For things we have to catch and throw */
8330 class_not_found_exception =
8331 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8332 no_class_def_found_error =
8333 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8334 load_class (class_not_found_exception, 1);
8335 load_class (no_class_def_found_error, 1);
8336
8337 /* Create the "class$" function */
8338 mdecl = create_artificial_method (class, ACC_STATIC,
8339 build_pointer_type (class_type_node),
8340 get_identifier ("class$"), args);
8341 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8342 no_class_def_found_error);
8343
8344 /* We start by building the try block. We need to build:
8345 return (java.lang.Class.forName (type)); */
8346 stmt = build_method_invocation (tmp,
8347 build_tree_list (NULL_TREE, type_parm_wfl));
8348 stmt = build_return (0, stmt);
8349 /* Put it in a block. That's the try block */
8350 try_block = build_expr_block (stmt, NULL_TREE);
8351
8352 /* Now onto the catch block. We start by building the expression
8353 throwing a new exception:
8354 throw new NoClassDefFoundError (_.getMessage); */
8355 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8356 get_message_wfl, 0);
8357 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8358
8359 /* Build new NoClassDefFoundError (_.getMessage) */
8360 throw_stmt = build_new_invocation
8361 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8362 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8363
8364 /* Build the throw, (it's too early to use BUILD_THROW) */
8365 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8366
8367 /* Build the catch block to encapsulate all this. We begin by
8368 building an decl for the catch clause parameter and link it to
8369 newly created block, the catch block. */
8370 catch_clause_param =
8371 build_decl (VAR_DECL, wpv_id,
8372 build_pointer_type (class_not_found_exception));
8373 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8374
8375 /* We initialize the variable with the exception handler. */
8376 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8377 soft_exceptioninfo_call_node);
8378 add_stmt_to_block (catch_block, NULL_TREE, catch);
8379
8380 /* We add the statement throwing the new exception */
8381 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8382
8383 /* Build a catch expression for all this */
8384 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8385
8386 /* Build the try/catch sequence */
8387 stmt = build_try_statement (0, try_block, catch_block);
8388
8389 fix_method_argument_names (args, mdecl);
8390 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8391 saved_current_function_decl = current_function_decl;
8392 start_artificial_method_body (mdecl);
8393 java_method_add_stmt (mdecl, stmt);
8394 end_artificial_method_body (mdecl);
8395 current_function_decl = saved_current_function_decl;
8396 TYPE_DOT_CLASS (class) = mdecl;
8397
8398 return mdecl;
8399}
8400
8401static tree
f0f3a777
APB
8402build_dot_class_method_invocation (type)
8403 tree type;
165f37bc 8404{
f0f3a777
APB
8405 tree sig_id, s;
8406
8407 if (TYPE_ARRAY_P (type))
8408 sig_id = build_java_signature (type);
8409 else
8410 sig_id = DECL_NAME (TYPE_NAME (type));
8411
1f8f4a0b
MM
8412 s = build_string (IDENTIFIER_LENGTH (sig_id),
8413 IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8414 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8415 build_tree_list (NULL_TREE, s));
8416}
8417
c2952b01
APB
8418/* This section of the code deals with constructor. */
8419
22eed1e6
APB
8420/* Craft a body for default constructor. Patch existing constructor
8421 bodies with call to super() and field initialization statements if
8422 necessary. */
8423
8424static void
8425fix_constructors (mdecl)
8426 tree mdecl;
8427{
8428 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8429 tree thisn_assign, compound = NULL_TREE;
8430 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8431
22eed1e6
APB
8432 if (!body)
8433 {
22eed1e6
APB
8434 /* It is an error for the compiler to generate a default
8435 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8436 takes no argument, or the same args for an anonymous class */
8437 if (verify_constructor_super (mdecl))
22eed1e6 8438 {
c2952b01
APB
8439 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8440 tree save = DECL_NAME (mdecl);
49f48c71 8441 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8442 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8443 parse_error_context
c2952b01
APB
8444 (lookup_cl (TYPE_NAME (class_type)),
8445 "No constructor matching `%s' found in class `%s'",
8446 lang_printable_name (mdecl, 0), n);
8447 DECL_NAME (mdecl) = save;
22eed1e6
APB
8448 }
8449
c2952b01
APB
8450 /* The constructor body must be crafted by hand. It's the
8451 constructor we defined when we realize we didn't have the
8452 CLASSNAME() constructor */
22eed1e6
APB
8453 start_artificial_method_body (mdecl);
8454
f0f3a777
APB
8455 /* Insert an assignment to the this$<n> hidden field, if
8456 necessary */
8457 if ((thisn_assign = build_thisn_assign ()))
8458 java_method_add_stmt (mdecl, thisn_assign);
8459
22eed1e6
APB
8460 /* We don't generate a super constructor invocation if we're
8461 compiling java.lang.Object. build_super_invocation takes care
8462 of that. */
e920ebc9 8463 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8464
c2952b01
APB
8465 /* Insert the instance initializer block right here, after the
8466 super invocation. */
8467 add_instance_initializer (mdecl);
8468
22eed1e6
APB
8469 end_artificial_method_body (mdecl);
8470 }
8471 /* Search for an explicit constructor invocation */
8472 else
8473 {
8474 int found = 0;
8475 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8476
8477 while (body)
8478 switch (TREE_CODE (body))
8479 {
8480 case CALL_EXPR:
8481 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8482 body = NULL_TREE;
8483 break;
8484 case COMPOUND_EXPR:
8485 case EXPR_WITH_FILE_LOCATION:
8486 body = TREE_OPERAND (body, 0);
8487 break;
8488 case BLOCK:
8489 body = BLOCK_EXPR_BODY (body);
8490 break;
8491 default:
8492 found = 0;
8493 body = NULL_TREE;
8494 }
8495 /* The constructor is missing an invocation of super() */
8496 if (!found)
8497 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8498 build_super_invocation (mdecl));
22eed1e6 8499
c2952b01
APB
8500 /* Generate the assignment to this$<n>, if necessary */
8501 if ((thisn_assign = build_thisn_assign ()))
8502 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8503
f0f3a777
APB
8504 /* Insert the instance initializer block right here, after the
8505 super invocation. */
8506 add_instance_initializer (mdecl);
8507
22eed1e6
APB
8508 /* Fix the constructor main block if we're adding extra stmts */
8509 if (compound)
8510 {
8511 compound = add_stmt_to_compound (compound, NULL_TREE,
8512 BLOCK_EXPR_BODY (main_block));
8513 BLOCK_EXPR_BODY (main_block) = compound;
8514 }
8515 }
8516}
8517
8518/* Browse constructors in the super class, searching for a constructor
8519 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8520 otherwise. If the current class is an anonymous inner class, look
8521 for something that has the same signature. */
22eed1e6
APB
8522
8523static int
c2952b01
APB
8524verify_constructor_super (mdecl)
8525 tree mdecl;
22eed1e6
APB
8526{
8527 tree class = CLASSTYPE_SUPER (current_class);
152de068 8528 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8529 tree sdecl;
8530
22eed1e6
APB
8531 if (!class)
8532 return 0;
8533
c2952b01 8534 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8535 {
c2952b01
APB
8536 tree mdecl_arg_type;
8537 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8538 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8539 if (DECL_CONSTRUCTOR_P (sdecl))
8540 {
cf1b2274 8541 tree m_arg_type;
152de068
APB
8542 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8543 if (super_inner)
8544 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8545 for (m_arg_type = mdecl_arg_type;
8546 (arg_type != end_params_node
8547 && m_arg_type != end_params_node);
c2952b01 8548 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8549 m_arg_type = TREE_CHAIN (m_arg_type))
8550 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8551 break;
8552
cf1b2274 8553 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8554 return 0;
8555 }
8556 }
8557 else
8558 {
8559 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8560 {
152de068
APB
8561 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8562 if (super_inner)
8563 arg = TREE_CHAIN (arg);
8564 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8565 return 0;
8566 }
8567 }
8568 return 1;
8569}
8570
22eed1e6 8571/* Generate code for all context remembered for code generation. */
b351b287
APB
8572
8573void
8574java_expand_classes ()
8575{
5423609c 8576 int save_error_count = 0;
c2952b01
APB
8577 static struct parser_ctxt *saved_ctxp = NULL;
8578
23a79c61
APB
8579 java_parse_abort_on_error ();
8580 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8581 return;
8582 java_layout_classes ();
8583 java_parse_abort_on_error ();
8584
c2952b01 8585 saved_ctxp = ctxp_for_generation;
b351b287
APB
8586 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8587 {
8588 ctxp = ctxp_for_generation;
8589 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8590 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8591 java_parse_abort_on_error ();
8592 }
c2952b01
APB
8593
8594 /* Find anonymous classes and expand their constructor, now they
8595 have been fixed. */
8596 for (ctxp_for_generation = saved_ctxp;
8597 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8598 {
8599 tree current;
8600 ctxp = ctxp_for_generation;
8601 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8602 {
8603 current_class = TREE_TYPE (current);
8604 if (ANONYMOUS_CLASS_P (current_class))
8605 {
8606 tree d;
8607 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8608 {
8609 if (DECL_CONSTRUCTOR_P (d))
8610 {
8611 restore_line_number_status (1);
8612 reset_method_name (d);
8613 java_complete_expand_method (d);
8614 restore_line_number_status (0);
8615 break; /* We now there are no other ones */
8616 }
8617 }
8618 }
8619 }
8620 }
8621
8622 /* If we've found error at that stage, don't try to generate
8623 anything, unless we're emitting xrefs or checking the syntax only
8624 (but not using -fsyntax-only for the purpose of generating
8625 bytecode. */
8626 if (java_error_count && !flag_emit_xref
8627 && (!flag_syntax_only && !flag_emit_class_files))
8628 return;
8629
8630 /* Now things are stable, go for generation of the class data. */
8631 for (ctxp_for_generation = saved_ctxp;
8632 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8633 {
8634 tree current;
8635 ctxp = ctxp_for_generation;
8636 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8637 {
8638 current_class = TREE_TYPE (current);
8639 outgoing_cpool = TYPE_CPOOL (current_class);
8640 if (flag_emit_class_files)
8641 write_classfile (current_class);
8642 if (flag_emit_xref)
8643 expand_xref (current_class);
8644 else if (! flag_syntax_only)
8645 finish_class ();
8646 }
8647 }
b351b287
APB
8648}
8649
e04a16fb
AG
8650/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8651 a tree list node containing RIGHT. Fore coming RIGHTs will be
8652 chained to this hook. LOCATION contains the location of the
8653 separating `.' operator. */
8654
8655static tree
8656make_qualified_primary (primary, right, location)
8657 tree primary, right;
8658 int location;
8659{
8660 tree wfl;
8661
c2952b01 8662 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8663 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8664 else
8665 {
8666 wfl = primary;
c2952b01
APB
8667 /* If wfl wasn't qualified, we build a first anchor */
8668 if (!EXPR_WFL_QUALIFICATION (wfl))
8669 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8670 }
8671
c2952b01 8672 /* And chain them */
e04a16fb
AG
8673 EXPR_WFL_LINECOL (right) = location;
8674 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8675 PRIMARY_P (wfl) = 1;
8676 return wfl;
8677}
8678
8679/* Simple merge of two name separated by a `.' */
8680
8681static tree
8682merge_qualified_name (left, right)
8683 tree left, right;
8684{
8685 tree node;
c2952b01
APB
8686 if (!left && !right)
8687 return NULL_TREE;
8688
8689 if (!left)
8690 return right;
8691
8692 if (!right)
8693 return left;
8694
e04a16fb
AG
8695 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8696 IDENTIFIER_LENGTH (left));
8697 obstack_1grow (&temporary_obstack, '.');
8698 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8699 IDENTIFIER_LENGTH (right));
8700 node = get_identifier (obstack_base (&temporary_obstack));
8701 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8702 QUALIFIED_P (node) = 1;
8703 return node;
8704}
8705
8706/* Merge the two parts of a qualified name into LEFT. Set the
8707 location information of the resulting node to LOCATION, usually
8708 inherited from the location information of the `.' operator. */
8709
8710static tree
8711make_qualified_name (left, right, location)
8712 tree left, right;
8713 int location;
8714{
bc3ca41b
PB
8715#ifdef USE_COMPONENT_REF
8716 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8717 EXPR_WFL_LINECOL (node) = location;
8718 return node;
8719#else
e04a16fb
AG
8720 tree left_id = EXPR_WFL_NODE (left);
8721 tree right_id = EXPR_WFL_NODE (right);
8722 tree wfl, merge;
8723
8724 merge = merge_qualified_name (left_id, right_id);
8725
8726 /* Left wasn't qualified and is now qualified */
8727 if (!QUALIFIED_P (left_id))
8728 {
8729 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8730 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8731 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8732 }
8733
8734 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8735 EXPR_WFL_LINECOL (wfl) = location;
8736 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8737
8738 EXPR_WFL_NODE (left) = merge;
8739 return left;
bc3ca41b 8740#endif
e04a16fb
AG
8741}
8742
8743/* Extract the last identifier component of the qualified in WFL. The
8744 last identifier is removed from the linked list */
8745
8746static tree
8747cut_identifier_in_qualified (wfl)
8748 tree wfl;
8749{
8750 tree q;
8751 tree previous = NULL_TREE;
8752 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8753 if (!TREE_CHAIN (q))
8754 {
8755 if (!previous)
781b0558 8756 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8757 TREE_CHAIN (previous) = NULL_TREE;
8758 return TREE_PURPOSE (q);
8759 }
8760}
8761
8762/* Resolve the expression name NAME. Return its decl. */
8763
8764static tree
5e942c50 8765resolve_expression_name (id, orig)
e04a16fb 8766 tree id;
5e942c50 8767 tree *orig;
e04a16fb
AG
8768{
8769 tree name = EXPR_WFL_NODE (id);
8770 tree decl;
8771
8772 /* 6.5.5.1: Simple expression names */
8773 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8774 {
8775 /* 15.13.1: NAME can appear within the scope of a local variable
8776 declaration */
8777 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8778 return decl;
8779
8780 /* 15.13.1: NAME can appear within a class declaration */
8781 else
8782 {
8783 decl = lookup_field_wrapper (current_class, name);
8784 if (decl)
8785 {
c2952b01 8786 tree access = NULL_TREE;
e04a16fb 8787 int fs = FIELD_STATIC (decl);
f2760b27
APB
8788
8789 /* If we're accessing an outer scope local alias, make
8790 sure we change the name of the field we're going to
8791 build access to. */
8792 if (FIELD_LOCAL_ALIAS_USED (decl))
8793 name = DECL_NAME (decl);
8794
e04a16fb
AG
8795 /* Instance variable (8.3.1.1) can't appear within
8796 static method, static initializer or initializer for
8797 a static variable. */
8798 if (!fs && METHOD_STATIC (current_function_decl))
8799 {
7f10c2e2 8800 static_ref_err (id, name, current_class);
e04a16fb
AG
8801 return error_mark_node;
8802 }
22eed1e6
APB
8803 /* Instance variables can't appear as an argument of
8804 an explicit constructor invocation */
8805 if (!fs && ctxp->explicit_constructor_p)
8806 {
8807 parse_error_context
781b0558 8808 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8809 return error_mark_node;
8810 }
5e942c50 8811
c2952b01
APB
8812 /* If we're processing an inner class and we're trying
8813 to access a field belonging to an outer class, build
8814 the access to the field */
8815 if (!fs && outer_field_access_p (current_class, decl))
8816 return build_outer_field_access (id, decl);
8817
5e942c50 8818 /* Otherwise build what it takes to access the field */
c2952b01
APB
8819 access = build_field_ref ((fs ? NULL_TREE : current_this),
8820 DECL_CONTEXT (decl), name);
e8fc7396 8821 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8822 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8823 /* We may be asked to save the real field access node */
8824 if (orig)
c2952b01 8825 *orig = access;
5e942c50 8826 /* And we return what we got */
c2952b01 8827 return access;
e04a16fb
AG
8828 }
8829 /* Fall down to error report on undefined variable */
8830 }
8831 }
8832 /* 6.5.5.2 Qualified Expression Names */
8833 else
8834 {
5e942c50
APB
8835 if (orig)
8836 *orig = NULL_TREE;
e04a16fb
AG
8837 qualify_ambiguous_name (id);
8838 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8839 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8840 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8841 }
8842
8843 /* We've got an error here */
8844 parse_error_context (id, "Undefined variable `%s'",
8845 IDENTIFIER_POINTER (name));
8846
8847 return error_mark_node;
8848}
8849
7f10c2e2
APB
8850static void
8851static_ref_err (wfl, field_id, class_type)
8852 tree wfl, field_id, class_type;
8853{
8854 parse_error_context
8855 (wfl,
8856 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8857 IDENTIFIER_POINTER (field_id),
8858 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8859}
8860
e04a16fb
AG
8861/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8862 We return something suitable to generate the field access. We also
8863 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8864 recipient's address can be null. */
8865
8866static tree
8867resolve_field_access (qual_wfl, field_decl, field_type)
8868 tree qual_wfl;
8869 tree *field_decl, *field_type;
8870{
8871 int is_static = 0;
8872 tree field_ref;
8873 tree decl, where_found, type_found;
8874
8875 if (resolve_qualified_expression_name (qual_wfl, &decl,
8876 &where_found, &type_found))
8877 return error_mark_node;
8878
8879 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8880 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8881 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8882 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8883 {
8884 tree length = build_java_array_length_access (where_found);
8885 field_ref =
8886 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8887
8888 /* In case we're dealing with a static array, we need to
8889 initialize its class before the array length can be fetched.
8890 It's also a good time to create a DECL_RTL for the field if
8891 none already exists, otherwise if the field was declared in a
8892 class found in an external file and hasn't been (and won't
8893 be) accessed for its value, none will be created. */
8894 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8895 {
8896 build_static_field_ref (where_found);
8897 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8898 }
e04a16fb
AG
8899 }
8900 /* We might have been trying to resolve field.method(). In which
8901 case, the resolution is over and decl is the answer */
34f4db93 8902 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8903 field_ref = decl;
34f4db93 8904 else if (JDECL_P (decl))
e04a16fb 8905 {
5e942c50
APB
8906 int static_final_found = 0;
8907 if (!type_found)
8908 type_found = DECL_CONTEXT (decl);
34f4db93 8909 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8910 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8911 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8912 && DECL_INITIAL (decl))
5e942c50 8913 {
0c2b8145
APB
8914 /* When called on a FIELD_DECL of the right (primitive)
8915 type, java_complete_tree will try to substitue the decl
8916 for it's initial value. */
70541f45 8917 field_ref = java_complete_tree (decl);
5e942c50
APB
8918 static_final_found = 1;
8919 }
8920 else
7f10c2e2
APB
8921 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8922 NULL_TREE : where_found),
5e942c50 8923 type_found, DECL_NAME (decl));
e04a16fb
AG
8924 if (field_ref == error_mark_node)
8925 return error_mark_node;
e8fc7396
APB
8926 if (is_static && !static_final_found
8927 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8928 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8929 }
8930 else
8931 field_ref = decl;
8932
8933 if (field_decl)
8934 *field_decl = decl;
8935 if (field_type)
c877974e
APB
8936 *field_type = (QUAL_DECL_TYPE (decl) ?
8937 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8938 return field_ref;
8939}
8940
e28cd97b
APB
8941/* If NODE is an access to f static field, strip out the class
8942 initialization part and return the field decl, otherwise, return
8943 NODE. */
8944
8945static tree
8946strip_out_static_field_access_decl (node)
8947 tree node;
8948{
8949 if (TREE_CODE (node) == COMPOUND_EXPR)
8950 {
8951 tree op1 = TREE_OPERAND (node, 1);
8952 if (TREE_CODE (op1) == COMPOUND_EXPR)
8953 {
8954 tree call = TREE_OPERAND (op1, 0);
8955 if (TREE_CODE (call) == CALL_EXPR
8956 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8957 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8958 == soft_initclass_node)
8959 return TREE_OPERAND (op1, 1);
8960 }
2f11d407
TT
8961 else if (JDECL_P (op1))
8962 return op1;
e28cd97b
APB
8963 }
8964 return node;
8965}
8966
e04a16fb
AG
8967/* 6.5.5.2: Qualified Expression Names */
8968
8969static int
8970resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8971 tree wfl;
8972 tree *found_decl, *type_found, *where_found;
8973{
8974 int from_type = 0; /* Field search initiated from a type */
c2952b01 8975 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8976 int previous_call_static = 0;
8977 int is_static;
8978 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8979 /* For certain for of inner class instantiation */
8980 tree saved_current, saved_this;
8981#define RESTORE_THIS_AND_CURRENT_CLASS \
8982 { current_class = saved_current; current_this = saved_this;}
8983
c877974e 8984 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8985
8986 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8987 {
8988 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8989 tree ret_decl; /* for EH checking */
8990 int location; /* for EH checking */
e04a16fb
AG
8991
8992 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8993 switch (TREE_CODE (qual_wfl))
8994 {
8995 case CALL_EXPR:
b67d701b 8996 case NEW_CLASS_EXPR:
e04a16fb
AG
8997 /* If the access to the function call is a non static field,
8998 build the code to access it. */
34f4db93 8999 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 9000 {
ac825856
APB
9001 decl = maybe_access_field (decl, *where_found,
9002 DECL_CONTEXT (decl));
e04a16fb
AG
9003 if (decl == error_mark_node)
9004 return 1;
9005 }
c2952b01 9006
e04a16fb
AG
9007 /* And code for the function call */
9008 if (complete_function_arguments (qual_wfl))
9009 return 1;
c2952b01
APB
9010
9011 /* We might have to setup a new current class and a new this
9012 for the search of an inner class, relative to the type of
9013 a expression resolved as `decl'. The current values are
9014 saved and restored shortly after */
9015 saved_current = current_class;
9016 saved_this = current_this;
9017 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
9018 {
9019 current_class = type;
9020 current_this = decl;
9021 }
9022
89e09b9a
PB
9023 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9024 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
9025 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9026 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9027 *where_found = patch_method_invocation (qual_wfl, decl, type,
9028 &is_static, &ret_decl);
e04a16fb 9029 if (*where_found == error_mark_node)
c2952b01
APB
9030 {
9031 RESTORE_THIS_AND_CURRENT_CLASS;
9032 return 1;
9033 }
e04a16fb
AG
9034 *type_found = type = QUAL_DECL_TYPE (*where_found);
9035
c2952b01
APB
9036 /* If we're creating an inner class instance, check for that
9037 an enclosing instance is in scope */
9038 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 9039 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
9040 {
9041 parse_error_context
165f37bc
APB
9042 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9043 lang_printable_name (type, 0),
9044 (!current_this ? "" :
9045 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
9046 RESTORE_THIS_AND_CURRENT_CLASS;
9047 return 1;
9048 }
9049
9050 /* In case we had to change then to resolve a inner class
9051 instantiation using a primary qualified by a `new' */
9052 RESTORE_THIS_AND_CURRENT_CLASS;
9053
34d4df06
APB
9054 /* EH check. No check on access$<n> functions */
9055 if (location
9056 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9057 (DECL_NAME (current_function_decl)))
7705e9db
APB
9058 check_thrown_exceptions (location, ret_decl);
9059
e04a16fb
AG
9060 /* If the previous call was static and this one is too,
9061 build a compound expression to hold the two (because in
9062 that case, previous function calls aren't transported as
9063 forcoming function's argument. */
9064 if (previous_call_static && is_static)
9065 {
9066 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9067 TREE_SIDE_EFFECTS (decl) = 1;
9068 }
9069 else
9070 {
9071 previous_call_static = is_static;
9072 decl = *where_found;
9073 }
c2952b01 9074 from_type = 0;
e04a16fb
AG
9075 continue;
9076
d8fccff5 9077 case NEW_ARRAY_EXPR:
c2952b01 9078 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9079 *where_found = decl = java_complete_tree (qual_wfl);
9080 if (decl == error_mark_node)
9081 return 1;
9082 *type_found = type = QUAL_DECL_TYPE (decl);
9083 CLASS_LOADED_P (type) = 1;
9084 continue;
9085
e04a16fb
AG
9086 case CONVERT_EXPR:
9087 *where_found = decl = java_complete_tree (qual_wfl);
9088 if (decl == error_mark_node)
9089 return 1;
9090 *type_found = type = QUAL_DECL_TYPE (decl);
9091 from_cast = 1;
9092 continue;
9093
22eed1e6 9094 case CONDITIONAL_EXPR:
5e942c50 9095 case STRING_CST:
ac22f9cb 9096 case MODIFY_EXPR:
22eed1e6
APB
9097 *where_found = decl = java_complete_tree (qual_wfl);
9098 if (decl == error_mark_node)
9099 return 1;
9100 *type_found = type = QUAL_DECL_TYPE (decl);
9101 continue;
9102
e04a16fb
AG
9103 case ARRAY_REF:
9104 /* If the access to the function call is a non static field,
9105 build the code to access it. */
34f4db93 9106 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9107 {
9108 decl = maybe_access_field (decl, *where_found, type);
9109 if (decl == error_mark_node)
9110 return 1;
9111 }
9112 /* And code for the array reference expression */
9113 decl = java_complete_tree (qual_wfl);
9114 if (decl == error_mark_node)
9115 return 1;
9116 type = QUAL_DECL_TYPE (decl);
9117 continue;
0a2138e2 9118
37feda7d
APB
9119 case PLUS_EXPR:
9120 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9121 return 1;
9122 if ((type = patch_string (decl)))
9123 decl = type;
9124 *where_found = QUAL_RESOLUTION (q) = decl;
9125 *type_found = type = TREE_TYPE (decl);
9126 break;
9127
165f37bc
APB
9128 case CLASS_LITERAL:
9129 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9130 return 1;
9131 *where_found = QUAL_RESOLUTION (q) = decl;
9132 *type_found = type = TREE_TYPE (decl);
9133 break;
9134
0a2138e2
APB
9135 default:
9136 /* Fix for -Wall Just go to the next statement. Don't
9137 continue */
a3f406ce 9138 break;
e04a16fb
AG
9139 }
9140
9141 /* If we fall here, we weren't processing a (static) function call. */
9142 previous_call_static = 0;
9143
9144 /* It can be the keyword THIS */
9145 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9146 {
9147 if (!current_this)
9148 {
9149 parse_error_context
9150 (wfl, "Keyword `this' used outside allowed context");
9151 return 1;
9152 }
f63991a8
APB
9153 if (ctxp->explicit_constructor_p)
9154 {
781b0558 9155 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9156 return 1;
9157 }
e04a16fb 9158 /* We have to generate code for intermediate acess */
c2952b01
APB
9159 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9160 {
9161 *where_found = decl = current_this;
9162 *type_found = type = QUAL_DECL_TYPE (decl);
9163 }
4dbf4496
APB
9164 /* We're trying to access the this from somewhere else. Make sure
9165 it's allowed before doing so. */
c2952b01
APB
9166 else
9167 {
4dbf4496
APB
9168 if (!enclosing_context_p (type, current_class))
9169 {
9170 char *p = xstrdup (lang_printable_name (type, 0));
9171 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9172 p, p,
9173 lang_printable_name (current_class, 0));
9174 free (p);
9175 return 1;
9176 }
c2952b01
APB
9177 *where_found = decl = build_current_thisn (type);
9178 from_qualified_this = 1;
9179 }
9180
9181 from_type = 0;
e04a16fb
AG
9182 continue;
9183 }
9184
9185 /* 15.10.2 Accessing Superclass Members using SUPER */
9186 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9187 {
9188 tree node;
9189 /* Check on the restricted use of SUPER */
9190 if (METHOD_STATIC (current_function_decl)
9191 || current_class == object_type_node)
9192 {
9193 parse_error_context
9194 (wfl, "Keyword `super' used outside allowed context");
9195 return 1;
9196 }
9197 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9198 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9199 CLASSTYPE_SUPER (current_class),
9200 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9201 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9202 if (decl == error_mark_node)
9203 return 1;
e04a16fb
AG
9204 *type_found = type = QUAL_DECL_TYPE (decl);
9205 from_super = from_type = 1;
9206 continue;
9207 }
9208
9209 /* 15.13.1: Can't search for field name in packages, so we
9210 assume a variable/class name was meant. */
9211 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9212 {
5e942c50
APB
9213 tree name = resolve_package (wfl, &q);
9214 if (name)
9215 {
c2952b01 9216 tree list;
5e942c50 9217 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9218 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9219 out. We're going to search something inside it. */
9220 *type_found = type = TREE_TYPE (decl);
9221 layout_class (type);
9222 from_type = 1;
c2952b01 9223
dde1da72
APB
9224 /* Fix them all the way down, if any are left. */
9225 if (q)
c2952b01 9226 {
dde1da72
APB
9227 list = TREE_CHAIN (q);
9228 while (list)
9229 {
9230 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9231 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9232 list = TREE_CHAIN (list);
9233 }
c2952b01 9234 }
5e942c50 9235 }
e04a16fb 9236 else
5e942c50
APB
9237 {
9238 if (from_super || from_cast)
9239 parse_error_context
9240 ((from_cast ? qual_wfl : wfl),
9241 "No variable `%s' defined in class `%s'",
9242 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9243 lang_printable_name (type, 0));
9244 else
9245 parse_error_context
9246 (qual_wfl, "Undefined variable or class name: `%s'",
9247 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9248 return 1;
9249 }
e04a16fb
AG
9250 }
9251
9252 /* We have a type name. It's been already resolved when the
9253 expression was qualified. */
9254 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9255 {
9256 if (!(decl = QUAL_RESOLUTION (q)))
9257 return 1; /* Error reported already */
9258
c2952b01
APB
9259 /* Sneak preview. If next we see a `new', we're facing a
9260 qualification with resulted in a type being selected
9261 instead of a field. Report the error */
9262 if(TREE_CHAIN (q)
9263 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9264 {
9265 parse_error_context (qual_wfl, "Undefined variable `%s'",
9266 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9267 return 1;
9268 }
9269
e04a16fb
AG
9270 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9271 {
9272 parse_error_context
9273 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9274 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9275 GET_TYPE_NAME (type),
e04a16fb
AG
9276 IDENTIFIER_POINTER (DECL_NAME (decl)),
9277 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9278 return 1;
9279 }
5e942c50 9280 check_deprecation (qual_wfl, decl);
c2952b01 9281
e04a16fb
AG
9282 type = TREE_TYPE (decl);
9283 from_type = 1;
9284 }
9285 /* We resolve and expression name */
9286 else
9287 {
cd531a2e 9288 tree field_decl = NULL_TREE;
e04a16fb
AG
9289
9290 /* If there exists an early resolution, use it. That occurs
9291 only once and we know that there are more things to
9292 come. Don't do that when processing something after SUPER
9293 (we need more thing to be put in place below */
9294 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9295 {
9296 decl = QUAL_RESOLUTION (q);
c877974e 9297 if (!type)
5e942c50 9298 {
7f10c2e2
APB
9299 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9300 {
9301 if (current_this)
9302 *where_found = current_this;
9303 else
9304 {
9305 static_ref_err (qual_wfl, DECL_NAME (decl),
9306 current_class);
9307 return 1;
9308 }
f0f3a777
APB
9309 if (outer_field_access_p (current_class, decl))
9310 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9311 }
c877974e
APB
9312 else
9313 {
9314 *where_found = TREE_TYPE (decl);
9315 if (TREE_CODE (*where_found) == POINTER_TYPE)
9316 *where_found = TREE_TYPE (*where_found);
9317 }
5e942c50 9318 }
b67d701b 9319 }
e04a16fb
AG
9320
9321 /* We have to search for a field, knowing the type of its
9322 container. The flag FROM_TYPE indicates that we resolved
9323 the last member of the expression as a type name, which
5e942c50
APB
9324 means that for the resolution of this field, we'll look
9325 for other errors than if it was resolved as a member of
9326 an other field. */
e04a16fb
AG
9327 else
9328 {
9329 int is_static;
5e942c50
APB
9330 tree field_decl_type; /* For layout */
9331
e04a16fb
AG
9332 if (!from_type && !JREFERENCE_TYPE_P (type))
9333 {
9334 parse_error_context
9335 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9336 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9337 lang_printable_name (type, 0),
e04a16fb
AG
9338 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9339 return 1;
9340 }
9341
dc0b3eff
PB
9342 field_decl = lookup_field_wrapper (type,
9343 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9344
9345 /* Maybe what we're trying to access an inner class. */
9346 if (!field_decl)
9347 {
9348 tree ptr, inner_decl;
9349
9350 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9351 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9352 if (inner_decl)
9353 {
9354 check_inner_class_access (inner_decl, decl, qual_wfl);
9355 type = TREE_TYPE (inner_decl);
9356 decl = inner_decl;
9357 from_type = 1;
9358 continue;
9359 }
9360 }
9361
dc0b3eff 9362 if (field_decl == NULL_TREE)
e04a16fb
AG
9363 {
9364 parse_error_context
2aa11e97 9365 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9366 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9367 GET_TYPE_NAME (type));
e04a16fb
AG
9368 return 1;
9369 }
dc0b3eff
PB
9370 if (field_decl == error_mark_node)
9371 return 1;
5e942c50
APB
9372
9373 /* Layout the type of field_decl, since we may need
c877974e
APB
9374 it. Don't do primitive types or loaded classes. The
9375 situation of non primitive arrays may not handled
9376 properly here. FIXME */
5e942c50
APB
9377 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9378 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9379 else
9380 field_decl_type = TREE_TYPE (field_decl);
9381 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9382 && !CLASS_LOADED_P (field_decl_type)
9383 && !TYPE_ARRAY_P (field_decl_type))
9384 resolve_and_layout (field_decl_type, NULL_TREE);
9385 if (TYPE_ARRAY_P (field_decl_type))
9386 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9387
9388 /* Check on accessibility here */
9389 if (not_accessible_p (type, field_decl, from_super))
9390 {
9391 parse_error_context
9392 (qual_wfl,
9393 "Can't access %s field `%s.%s' from `%s'",
9394 java_accstring_lookup
9395 (get_access_flags_from_decl (field_decl)),
2aa11e97 9396 GET_TYPE_NAME (type),
e04a16fb
AG
9397 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9398 IDENTIFIER_POINTER
9399 (DECL_NAME (TYPE_NAME (current_class))));
9400 return 1;
9401 }
5e942c50 9402 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9403
9404 /* There are things to check when fields are accessed
9405 from type. There are no restrictions on a static
9406 declaration of the field when it is accessed from an
9407 interface */
9408 is_static = FIELD_STATIC (field_decl);
9409 if (!from_super && from_type
c2952b01
APB
9410 && !TYPE_INTERFACE_P (type)
9411 && !is_static
9412 && (current_function_decl
9413 && METHOD_STATIC (current_function_decl)))
e04a16fb 9414 {
7f10c2e2 9415 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9416 return 1;
9417 }
9418 from_cast = from_super = 0;
9419
c2952b01
APB
9420 /* It's an access from a type but it isn't static, we
9421 make it relative to `this'. */
9422 if (!is_static && from_type)
9423 decl = current_this;
9424
5e942c50
APB
9425 /* If we need to generate something to get a proper
9426 handle on what this field is accessed from, do it
9427 now. */
e04a16fb
AG
9428 if (!is_static)
9429 {
c583dd46 9430 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9431 if (decl == error_mark_node)
9432 return 1;
9433 }
9434
9435 /* We want to keep the location were found it, and the type
9436 we found. */
9437 *where_found = decl;
9438 *type_found = type;
9439
c2952b01
APB
9440 /* Generate the correct expression for field access from
9441 qualified this */
9442 if (from_qualified_this)
9443 {
9444 field_decl = build_outer_field_access (qual_wfl, field_decl);
9445 from_qualified_this = 0;
9446 }
9447
e04a16fb
AG
9448 /* This is the decl found and eventually the next one to
9449 search from */
9450 decl = field_decl;
9451 }
e04a16fb
AG
9452 from_type = 0;
9453 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9454
9455 /* Sneak preview. If decl is qualified by a `new', report
9456 the error here to be accurate on the peculiar construct */
9457 if (TREE_CHAIN (q)
9458 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9459 && !JREFERENCE_TYPE_P (type))
9460 {
9461 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9462 lang_printable_name (type, 0));
9463 return 1;
9464 }
e04a16fb 9465 }
dde1da72
APB
9466 /* `q' might have changed due to a after package resolution
9467 re-qualification */
9468 if (!q)
9469 break;
e04a16fb
AG
9470 }
9471 *found_decl = decl;
9472 return 0;
9473}
9474
9475/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9476 can't be accessed from REFERENCE (a record type). This should be
9477 used when decl is a field or a method.*/
e04a16fb 9478
be245ac0
KG
9479static int
9480not_accessible_p (reference, member, from_super)
e04a16fb
AG
9481 tree reference, member;
9482 int from_super;
9483{
9484 int access_flag = get_access_flags_from_decl (member);
9485
4dbf4496
APB
9486 /* Inner classes are processed by check_inner_class_access */
9487 if (INNER_CLASS_TYPE_P (reference))
9488 return 0;
9489
e04a16fb
AG
9490 /* Access always granted for members declared public */
9491 if (access_flag & ACC_PUBLIC)
9492 return 0;
9493
9494 /* Check access on protected members */
9495 if (access_flag & ACC_PROTECTED)
9496 {
9497 /* Access granted if it occurs from within the package
9498 containing the class in which the protected member is
9499 declared */
9500 if (class_in_current_package (DECL_CONTEXT (member)))
9501 return 0;
9502
9bbc7d9f
PB
9503 /* If accessed with the form `super.member', then access is granted */
9504 if (from_super)
9505 return 0;
e04a16fb 9506
9bbc7d9f 9507 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9508 member is declared or a subclass of it. Find the right
9509 context to perform the check */
9510 if (PURE_INNER_CLASS_TYPE_P (reference))
9511 {
9512 while (INNER_CLASS_TYPE_P (reference))
9513 {
9514 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9515 return 0;
9516 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9517 }
9518 }
473e7b07 9519 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9520 return 0;
e04a16fb
AG
9521 return 1;
9522 }
9523
9524 /* Check access on private members. Access is granted only if it
473e7b07 9525 occurs from within the class in which it is declared. Exceptions
4dbf4496 9526 are accesses from inner-classes. */
e04a16fb 9527 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9528 return (current_class == DECL_CONTEXT (member) ? 0 :
9529 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9530
9531 /* Default access are permitted only when occuring within the
9532 package in which the type (REFERENCE) is declared. In other words,
9533 REFERENCE is defined in the current package */
9534 if (ctxp->package)
9535 return !class_in_current_package (reference);
473e7b07 9536
e04a16fb
AG
9537 /* Otherwise, access is granted */
9538 return 0;
9539}
9540
5e942c50
APB
9541/* Test deprecated decl access. */
9542static void
9543check_deprecation (wfl, decl)
9544 tree wfl, decl;
9545{
49f48c71 9546 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9547 /* Complain if the field is deprecated and the file it was defined
9548 in isn't compiled at the same time the file which contains its
9549 use is */
9550 if (DECL_DEPRECATED (decl)
9551 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9552 {
9553 char the [20];
9554 switch (TREE_CODE (decl))
9555 {
9556 case FUNCTION_DECL:
9557 strcpy (the, "method");
9558 break;
9559 case FIELD_DECL:
9560 strcpy (the, "field");
9561 break;
9562 case TYPE_DECL:
9563 strcpy (the, "class");
9564 break;
15fdcfe9
PB
9565 default:
9566 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9567 }
9568 parse_warning_context
9569 (wfl, "The %s `%s' in class `%s' has been deprecated",
9570 the, lang_printable_name (decl, 0),
9571 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9572 }
9573}
9574
e04a16fb
AG
9575/* Returns 1 if class was declared in the current package, 0 otherwise */
9576
9577static int
9578class_in_current_package (class)
9579 tree class;
9580{
9581 static tree cache = NULL_TREE;
9582 int qualified_flag;
9583 tree left;
9584
9585 if (cache == class)
9586 return 1;
9587
9588 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9589
9590 /* If the current package is empty and the name of CLASS is
9591 qualified, class isn't in the current package. If there is a
9592 current package and the name of the CLASS is not qualified, class
9593 isn't in the current package */
0a2138e2 9594 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9595 return 0;
9596
9597 /* If there is not package and the name of CLASS isn't qualified,
9598 they belong to the same unnamed package */
9599 if (!ctxp->package && !qualified_flag)
9600 return 1;
9601
9602 /* Compare the left part of the name of CLASS with the package name */
9603 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9604 if (ctxp->package == left)
9605 {
19e223db
MM
9606 static int initialized_p;
9607 /* Register CACHE with the garbage collector. */
9608 if (!initialized_p)
9609 {
9610 ggc_add_tree_root (&cache, 1);
9611 initialized_p = 1;
9612 }
9613
e04a16fb
AG
9614 cache = class;
9615 return 1;
9616 }
9617 return 0;
9618}
9619
9620/* This function may generate code to access DECL from WHERE. This is
9621 done only if certain conditions meet. */
9622
9623static tree
9624maybe_access_field (decl, where, type)
9625 tree decl, where, type;
9626{
5e942c50
APB
9627 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9628 && !FIELD_STATIC (decl))
e04a16fb 9629 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9630 (type ? type : DECL_CONTEXT (decl)),
9631 DECL_NAME (decl));
e04a16fb
AG
9632 return decl;
9633}
9634
15fdcfe9 9635/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9636 and according to the situation, PRIMARY and WHERE may be
9637 used. IS_STATIC is set to 1 if the invoked function is static. */
9638
9639static tree
89e09b9a 9640patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9641 tree patch, primary, where;
9642 int *is_static;
b9f7e36c 9643 tree *ret_decl;
e04a16fb
AG
9644{
9645 tree wfl = TREE_OPERAND (patch, 0);
9646 tree args = TREE_OPERAND (patch, 1);
9647 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9648 tree list;
22eed1e6 9649 int is_static_flag = 0;
89e09b9a 9650 int is_super_init = 0;
bccaf73a 9651 tree this_arg = NULL_TREE;
e04a16fb
AG
9652
9653 /* Should be overriden if everything goes well. Otherwise, if
9654 something fails, it should keep this value. It stop the
9655 evaluation of a bogus assignment. See java_complete_tree,
9656 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9657 evaluating an assignment */
9658 TREE_TYPE (patch) = error_mark_node;
9659
9660 /* Since lookup functions are messing with line numbers, save the
9661 context now. */
9662 java_parser_context_save_global ();
9663
9664 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9665
9666 /* Resolution of qualified name, excluding constructors */
9667 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9668 {
dde1da72 9669 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9670 /* Extract the last IDENTIFIER of the qualified
9671 expression. This is a wfl and we will use it's location
9672 data during error report. */
9673 identifier_wfl = cut_identifier_in_qualified (wfl);
9674 identifier = EXPR_WFL_NODE (identifier_wfl);
9675
9676 /* Given the context, IDENTIFIER is syntactically qualified
9677 as a MethodName. We need to qualify what's before */
9678 qualify_ambiguous_name (wfl);
dde1da72 9679 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9680
dde1da72
APB
9681 if (resolved == error_mark_node)
9682 PATCH_METHOD_RETURN_ERROR ();
9683
9684 type = GET_SKIP_TYPE (resolved);
9685 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9686
9687 if (JPRIMITIVE_TYPE_P (type))
9688 {
7e51098e
TT
9689 parse_error_context
9690 (identifier_wfl,
9691 "Can't invoke a method on primitive type `%s'",
9692 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9693 PATCH_METHOD_RETURN_ERROR ();
9694 }
9695
dde1da72
APB
9696 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9697 args = nreverse (args);
2c56429a 9698
e04a16fb 9699 /* We're resolving a call from a type */
dde1da72 9700 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9701 {
dde1da72 9702 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9703 {
9704 parse_error_context
781b0558
KG
9705 (identifier_wfl,
9706 "Can't make static reference to method `%s' in interface `%s'",
9707 IDENTIFIER_POINTER (identifier),
e04a16fb 9708 IDENTIFIER_POINTER (name));
b9f7e36c 9709 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9710 }
e04a16fb
AG
9711 if (list && !METHOD_STATIC (list))
9712 {
c2e3db92 9713 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9714 parse_error_context
9715 (identifier_wfl,
9716 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9717 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9718 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9719 free (fct_name);
b9f7e36c 9720 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9721 }
9722 }
e04a16fb 9723 else
dde1da72
APB
9724 this_arg = primary = resolved;
9725
5e942c50 9726 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9727 wfl = identifier_wfl;
9728 }
9729 /* Resolution of simple names, names generated after a primary: or
9730 constructors */
9731 else
9732 {
cd531a2e 9733 tree class_to_search = NULL_TREE;
c2952b01 9734 int lc; /* Looking for Constructor */
e04a16fb
AG
9735
9736 /* We search constructor in their target class */
9737 if (CALL_CONSTRUCTOR_P (patch))
9738 {
22eed1e6
APB
9739 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9740 class_to_search = EXPR_WFL_NODE (wfl);
9741 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9742 this_identifier_node)
9743 class_to_search = NULL_TREE;
9744 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9745 super_identifier_node)
e04a16fb 9746 {
89e09b9a 9747 is_super_init = 1;
22eed1e6
APB
9748 if (CLASSTYPE_SUPER (current_class))
9749 class_to_search =
9750 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9751 else
9752 {
781b0558 9753 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9754 PATCH_METHOD_RETURN_ERROR ();
9755 }
e04a16fb 9756 }
22eed1e6
APB
9757
9758 /* Class to search is NULL if we're searching the current one */
9759 if (class_to_search)
e04a16fb 9760 {
c2952b01
APB
9761 class_to_search = resolve_and_layout (class_to_search, wfl);
9762
22eed1e6
APB
9763 if (!class_to_search)
9764 {
9765 parse_error_context
9766 (wfl, "Class `%s' not found in type declaration",
9767 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9768 PATCH_METHOD_RETURN_ERROR ();
9769 }
9770
5e942c50
APB
9771 /* Can't instantiate an abstract class, but we can
9772 invoke it's constructor. It's use within the `new'
9773 context is denied here. */
9774 if (CLASS_ABSTRACT (class_to_search)
9775 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9776 {
9777 parse_error_context
781b0558
KG
9778 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9779 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9780 PATCH_METHOD_RETURN_ERROR ();
9781 }
c2952b01 9782
22eed1e6 9783 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9784 }
22eed1e6
APB
9785 else
9786 class_to_search = current_class;
e04a16fb
AG
9787 lc = 1;
9788 }
9789 /* This is a regular search in the local class, unless an
9790 alternate class is specified. */
9791 else
9792 {
9793 class_to_search = (where ? where : current_class);
9794 lc = 0;
9795 }
c2952b01 9796
e04a16fb
AG
9797 /* NAME is a simple identifier or comes from a primary. Search
9798 in the class whose declaration contain the method being
9799 invoked. */
c877974e 9800 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9801
c2952b01 9802 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9803 /* Don't continue if no method were found, as the next statement
9804 can't be executed then. */
b9f7e36c
APB
9805 if (!list)
9806 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9807
9808 /* Check for static reference if non static methods */
9809 if (check_for_static_method_reference (wfl, patch, list,
9810 class_to_search, primary))
b9f7e36c 9811 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9812
165f37bc
APB
9813 /* Check for inner classes creation from illegal contexts */
9814 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9815 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9816 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9817 {
9818 parse_error_context
9819 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9820 lang_printable_name (class_to_search, 0),
9821 (!current_this ? "" :
9822 "; an explicit one must be provided when creating this inner class"));
9823 PATCH_METHOD_RETURN_ERROR ();
9824 }
9825
22eed1e6
APB
9826 /* Non static methods are called with the current object extra
9827 argument. If patch a `new TYPE()', the argument is the value
9828 returned by the object allocator. If method is resolved as a
9829 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9830 args = nreverse (args);
bccaf73a 9831 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9832 {
9833 this_arg = primary ? primary : current_this;
9834
9835 /* If we're using an access method, things are different.
9836 There are two familly of cases:
9837
9838 1) We're not generating bytecodes:
9839
9840 - LIST is non static. It's invocation is transformed from
9841 x(a1,...,an) into this$<n>.x(a1,....an).
9842 - LIST is static. It's invocation is transformed from
9843 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9844
9845 2) We're generating bytecodes:
9846
9847 - LIST is non static. It's invocation is transformed from
9848 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9849 - LIST is static. It's invocation is transformed from
9850 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9851
9852 Of course, this$<n> can be abitrary complex, ranging from
9853 this$0 (the immediate outer context) to
9854 access$0(access$0(...(this$0))).
9855
9856 maybe_use_access_method returns a non zero value if the
dfb99c83 9857 this_arg has to be moved into the (then generated) stub
4dbf4496 9858 argument list. In the meantime, the selected function
dfb99c83 9859 might have be replaced by a generated stub. */
c2952b01
APB
9860 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9861 args = tree_cons (NULL_TREE, this_arg, args);
9862 }
e04a16fb 9863 }
b67d701b 9864
e04a16fb
AG
9865 /* Merge point of all resolution schemes. If we have nothing, this
9866 is an error, already signaled */
b9f7e36c
APB
9867 if (!list)
9868 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9869
e04a16fb
AG
9870 /* Check accessibility, position the is_static flag, build and
9871 return the call */
9bbc7d9f 9872 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9873 {
c2e3db92 9874 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9875 parse_error_context
9876 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9877 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9878 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9879 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9880 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9881 free (fct_name);
b9f7e36c 9882 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9883 }
5e942c50 9884 check_deprecation (wfl, list);
22eed1e6 9885
c2952b01
APB
9886 /* If invoking a innerclass constructor, there are hidden parameters
9887 to pass */
9888 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9889 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9890 {
9891 /* And make sure we add the accessed local variables to be saved
9892 in field aliases. */
9893 args = build_alias_initializer_parameter_list
9894 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9895
da632f2c 9896 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9897 if (primary || current_this)
9898 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9899 else
9900 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9901 }
9902
152de068
APB
9903 /* This handles the situation where a constructor invocation needs
9904 to have an enclosing context passed as a second parameter (the
9905 constructor is one of an inner class. We extract it from the
9906 current function. */
9907 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9908 {
9909 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9910 tree extra_arg;
9911
9912 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9913 {
9914 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9915 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9916 }
9917 else
9918 {
9919 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9920 extra_arg =
9921 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9922 extra_arg = java_complete_tree (extra_arg);
9923 }
9924 args = tree_cons (NULL_TREE, extra_arg, args);
9925 }
9926
22eed1e6 9927 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9928 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9929 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9930
c3f2a476
APB
9931 /* In the context of an explicit constructor invocation, we can't
9932 invoke any method relying on `this'. Exceptions are: we're
9933 invoking a static function, primary exists and is not the current
9934 this, we're creating a new object. */
22eed1e6 9935 if (ctxp->explicit_constructor_p
c3f2a476
APB
9936 && !is_static_flag
9937 && (!primary || primary == current_this)
9938 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9939 {
781b0558 9940 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9941 PATCH_METHOD_RETURN_ERROR ();
9942 }
e04a16fb 9943 java_parser_context_restore_global ();
22eed1e6
APB
9944 if (is_static)
9945 *is_static = is_static_flag;
b9f7e36c
APB
9946 /* Sometimes, we want the decl of the selected method. Such as for
9947 EH checking */
9948 if (ret_decl)
9949 *ret_decl = list;
89e09b9a
PB
9950 patch = patch_invoke (patch, list, args);
9951 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9952 {
c2952b01
APB
9953 tree finit_parms, finit_call;
9954
c00f0fb2 9955 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
9956 finit_parms = build_alias_initializer_parameter_list
9957 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9958
c2952b01
APB
9959 finit_call =
9960 build_method_invocation (build_wfl_node (finit_identifier_node),
9961 finit_parms);
9962
9963 /* Generate the code used to initialize fields declared with an
9964 initialization statement and build a compound statement along
9965 with the super constructor invocation. */
89e09b9a
PB
9966 patch = build (COMPOUND_EXPR, void_type_node, patch,
9967 java_complete_tree (finit_call));
9968 CAN_COMPLETE_NORMALLY (patch) = 1;
9969 }
9970 return patch;
e04a16fb
AG
9971}
9972
9973/* Check that we're not trying to do a static reference to a method in
9974 non static method. Return 1 if it's the case, 0 otherwise. */
9975
9976static int
9977check_for_static_method_reference (wfl, node, method, where, primary)
9978 tree wfl, node, method, where, primary;
9979{
9980 if (METHOD_STATIC (current_function_decl)
9981 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9982 {
c2e3db92 9983 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9984 parse_error_context
9985 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9986 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9987 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9988 free (fct_name);
9989 return 1;
9990 }
9991 return 0;
9992}
9993
c2952b01
APB
9994/* Fix the invocation of *MDECL if necessary in the case of a
9995 invocation from an inner class. *THIS_ARG might be modified
9996 appropriately and an alternative access to *MDECL might be
9997 returned. */
9998
9999static int
10000maybe_use_access_method (is_super_init, mdecl, this_arg)
10001 int is_super_init;
10002 tree *mdecl, *this_arg;
10003{
10004 tree ctx;
10005 tree md = *mdecl, ta = *this_arg;
10006 int to_return = 0;
10007 int non_static_context = !METHOD_STATIC (md);
10008
10009 if (is_super_init
165f37bc
APB
10010 || DECL_CONTEXT (md) == current_class
10011 || !PURE_INNER_CLASS_TYPE_P (current_class)
10012 || DECL_FINIT_P (md))
c2952b01
APB
10013 return 0;
10014
10015 /* If we're calling a method found in an enclosing class, generate
10016 what it takes to retrieve the right this. Don't do that if we're
10017 invoking a static method. */
10018
10019 if (non_static_context)
10020 {
10021 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 10022 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
10023 {
10024 ta = build_current_thisn (current_class);
10025 ta = build_wfl_node (ta);
10026 }
10027 else
10028 {
10029 tree type = ctx;
10030 while (type)
10031 {
10032 maybe_build_thisn_access_method (type);
4dbf4496 10033 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
10034 {
10035 ta = build_access_to_thisn (ctx, type, 0);
10036 break;
10037 }
10038 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10039 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10040 }
10041 }
10042 ta = java_complete_tree (ta);
10043 }
10044
10045 /* We might have to use an access method to get to MD. We can
10046 break the method access rule as far as we're not generating
10047 bytecode */
10048 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10049 {
10050 md = build_outer_method_access_method (md);
10051 to_return = 1;
10052 }
10053
10054 *mdecl = md;
10055 *this_arg = ta;
10056
10057 /* Returnin a non zero value indicates we were doing a non static
10058 method invokation that is now a static invocation. It will have
10059 callee displace `this' to insert it in the regular argument
10060 list. */
10061 return (non_static_context && to_return);
10062}
10063
e04a16fb
AG
10064/* Patch an invoke expression METHOD and ARGS, based on its invocation
10065 mode. */
10066
10067static tree
89e09b9a 10068patch_invoke (patch, method, args)
e04a16fb 10069 tree patch, method, args;
e04a16fb
AG
10070{
10071 tree dtable, func;
0a2138e2 10072 tree original_call, t, ta;
e815887f 10073 tree cond = NULL_TREE;
e04a16fb 10074
5e942c50
APB
10075 /* Last step for args: convert build-in types. If we're dealing with
10076 a new TYPE() type call, the first argument to the constructor
e815887f 10077 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10078 `new' */
10079 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10080 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10081 t = TREE_CHAIN (t);
ac825856
APB
10082 for (ta = args; t != end_params_node && ta;
10083 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10084 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10085 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10086 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10087
10088 /* Resolve unresolved returned type isses */
10089 t = TREE_TYPE (TREE_TYPE (method));
10090 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10091 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10092
e8fc7396 10093 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10094 func = method;
10095 else
e04a16fb 10096 {
15fdcfe9 10097 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10098 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10099 {
10100 case INVOKE_VIRTUAL:
10101 dtable = invoke_build_dtable (0, args);
10102 func = build_invokevirtual (dtable, method);
10103 break;
b9f7e36c 10104
e815887f
TT
10105 case INVOKE_NONVIRTUAL:
10106 /* If the object for the method call is null, we throw an
10107 exception. We don't do this if the object is the current
10108 method's `this'. In other cases we just rely on an
10109 optimization pass to eliminate redundant checks. */
10110 if (TREE_VALUE (args) != current_this)
10111 {
10112 /* We use a SAVE_EXPR here to make sure we only evaluate
10113 the new `self' expression once. */
10114 tree save_arg = save_expr (TREE_VALUE (args));
10115 TREE_VALUE (args) = save_arg;
10116 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10117 null_pointer_node);
10118 }
10119 /* Fall through. */
10120
15fdcfe9
PB
10121 case INVOKE_SUPER:
10122 case INVOKE_STATIC:
10123 func = build_known_method_ref (method, TREE_TYPE (method),
10124 DECL_CONTEXT (method),
10125 signature, args);
10126 break;
e04a16fb 10127
15fdcfe9
PB
10128 case INVOKE_INTERFACE:
10129 dtable = invoke_build_dtable (1, args);
173f556c 10130 func = build_invokeinterface (dtable, method);
15fdcfe9 10131 break;
5e942c50 10132
15fdcfe9 10133 default:
89e09b9a 10134 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10135 }
10136
10137 /* Ensure self_type is initialized, (invokestatic). FIXME */
10138 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10139 }
10140
e04a16fb
AG
10141 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10142 TREE_OPERAND (patch, 0) = func;
10143 TREE_OPERAND (patch, 1) = args;
10144 original_call = patch;
10145
e815887f 10146 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10147 returned value is the first argument to the constructor. We build
10148 a COMPOUND_EXPR and use saved expression so that the overall NEW
10149 expression value is a pointer to a newly created and initialized
10150 class. */
10151 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10152 {
10153 tree class = DECL_CONTEXT (method);
10154 tree c1, saved_new, size, new;
e8fc7396 10155 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10156 {
10157 TREE_TYPE (patch) = build_pointer_type (class);
10158 return patch;
10159 }
e04a16fb
AG
10160 if (!TYPE_SIZE (class))
10161 safe_layout_class (class);
10162 size = size_in_bytes (class);
10163 new = build (CALL_EXPR, promote_type (class),
10164 build_address_of (alloc_object_node),
10165 tree_cons (NULL_TREE, build_class_ref (class),
10166 build_tree_list (NULL_TREE,
10167 size_in_bytes (class))),
10168 NULL_TREE);
10169 saved_new = save_expr (new);
10170 c1 = build_tree_list (NULL_TREE, saved_new);
10171 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10172 TREE_OPERAND (original_call, 1) = c1;
10173 TREE_SET_CODE (original_call, CALL_EXPR);
10174 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10175 }
e815887f
TT
10176
10177 /* If COND is set, then we are building a check to see if the object
10178 is NULL. */
10179 if (cond != NULL_TREE)
10180 {
10181 /* We have to make the `then' branch a compound expression to
10182 make the types turn out right. This seems bizarre. */
10183 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10184 build (COMPOUND_EXPR, TREE_TYPE (patch),
10185 build (CALL_EXPR, void_type_node,
10186 build_address_of (soft_nullpointer_node),
10187 NULL_TREE, NULL_TREE),
10188 (FLOAT_TYPE_P (TREE_TYPE (patch))
10189 ? build_real (TREE_TYPE (patch), dconst0)
10190 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10191 integer_zero_node))),
10192 patch);
10193 TREE_SIDE_EFFECTS (patch) = 1;
10194 }
10195
e04a16fb
AG
10196 return patch;
10197}
10198
10199static int
10200invocation_mode (method, super)
10201 tree method;
10202 int super;
10203{
10204 int access = get_access_flags_from_decl (method);
10205
22eed1e6
APB
10206 if (super)
10207 return INVOKE_SUPER;
10208
e815887f 10209 if (access & ACC_STATIC)
e04a16fb
AG
10210 return INVOKE_STATIC;
10211
e815887f
TT
10212 /* We have to look for a constructor before we handle nonvirtual
10213 calls; otherwise the constructor will look nonvirtual. */
10214 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10215 return INVOKE_STATIC;
e815887f
TT
10216
10217 if (access & ACC_FINAL || access & ACC_PRIVATE)
10218 return INVOKE_NONVIRTUAL;
10219
10220 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10221 return INVOKE_NONVIRTUAL;
10222
e04a16fb
AG
10223 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10224 return INVOKE_INTERFACE;
22eed1e6 10225
e04a16fb
AG
10226 return INVOKE_VIRTUAL;
10227}
10228
b67d701b
PB
10229/* Retrieve a refined list of matching methods. It covers the step
10230 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10231
10232static tree
10233lookup_method_invoke (lc, cl, class, name, arg_list)
10234 int lc;
10235 tree cl;
10236 tree class, name, arg_list;
10237{
de4c7b02 10238 tree atl = end_params_node; /* Arg Type List */
c877974e 10239 tree method, signature, list, node;
49f48c71 10240 const char *candidates; /* Used for error report */
b5b8a0e7 10241 char *dup;
e04a16fb 10242
5e942c50 10243 /* Fix the arguments */
e04a16fb
AG
10244 for (node = arg_list; node; node = TREE_CHAIN (node))
10245 {
e3884b71 10246 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10247 /* Non primitive type may have to be resolved */
e3884b71 10248 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10249 resolve_and_layout (current_arg, NULL_TREE);
10250 /* And promoted */
b67d701b 10251 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10252 current_arg = promote_type (current_arg);
5e942c50 10253 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10254 }
e04a16fb 10255
c2952b01
APB
10256 /* Presto. If we're dealing with an anonymous class and a
10257 constructor call, generate the right constructor now, since we
10258 know the arguments' types. */
10259
10260 if (lc && ANONYMOUS_CLASS_P (class))
10261 craft_constructor (TYPE_NAME (class), atl);
10262
5e942c50
APB
10263 /* Find all candidates and then refine the list, searching for the
10264 most specific method. */
10265 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10266 list = find_most_specific_methods_list (list);
b67d701b
PB
10267 if (list && !TREE_CHAIN (list))
10268 return TREE_VALUE (list);
e04a16fb 10269
b67d701b
PB
10270 /* Issue an error. List candidates if any. Candidates are listed
10271 only if accessible (non accessible methods may end-up here for
10272 the sake of a better error report). */
10273 candidates = NULL;
10274 if (list)
e04a16fb 10275 {
e04a16fb 10276 tree current;
b67d701b 10277 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10278 for (current = list; current; current = TREE_CHAIN (current))
10279 {
b67d701b
PB
10280 tree cm = TREE_VALUE (current);
10281 char string [4096];
10282 if (!cm || not_accessible_p (class, cm, 0))
10283 continue;
b67d701b 10284 sprintf
22eed1e6
APB
10285 (string, " `%s' in `%s'%s",
10286 get_printable_method_name (cm),
b67d701b
PB
10287 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10288 (TREE_CHAIN (current) ? "\n" : ""));
10289 obstack_grow (&temporary_obstack, string, strlen (string));
10290 }
10291 obstack_1grow (&temporary_obstack, '\0');
10292 candidates = obstack_finish (&temporary_obstack);
10293 }
10294 /* Issue the error message */
c877974e
APB
10295 method = make_node (FUNCTION_TYPE);
10296 TYPE_ARG_TYPES (method) = atl;
b67d701b 10297 signature = build_java_argument_signature (method);
c63b98cd 10298 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10299 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10300 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10301 (lc ? dup : IDENTIFIER_POINTER (name)),
10302 IDENTIFIER_POINTER (signature), dup,
b67d701b 10303 (candidates ? candidates : ""));
b5b8a0e7 10304 free (dup);
b67d701b
PB
10305 return NULL_TREE;
10306}
10307
5e942c50
APB
10308/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10309 when we're looking for a constructor. */
b67d701b
PB
10310
10311static tree
5e942c50
APB
10312find_applicable_accessible_methods_list (lc, class, name, arglist)
10313 int lc;
b67d701b
PB
10314 tree class, name, arglist;
10315{
ad69b5b6
BM
10316 static struct hash_table t, *searched_classes = NULL;
10317 static int search_not_done = 0;
b67d701b
PB
10318 tree list = NULL_TREE, all_list = NULL_TREE;
10319
ad69b5b6
BM
10320 /* Check the hash table to determine if this class has been searched
10321 already. */
10322 if (searched_classes)
10323 {
10324 if (hash_lookup (searched_classes,
10325 (const hash_table_key) class, FALSE, NULL))
10326 return NULL;
10327 }
10328 else
10329 {
10330 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10331 java_hash_compare_tree_node);
10332 searched_classes = &t;
10333 }
10334
10335 search_not_done++;
10336 hash_lookup (searched_classes,
0c2b8145 10337 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10338
c2952b01
APB
10339 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10340 {
10341 load_class (class, 1);
10342 safe_layout_class (class);
10343 }
10344
1982388a 10345 /* Search interfaces */
9a7ab4b3
APB
10346 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10347 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10348 {
1982388a
APB
10349 int i, n;
10350 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10351 search_applicable_methods_list (lc, TYPE_METHODS (class),
10352 name, arglist, &list, &all_list);
1982388a 10353 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10354 for (i = 1; i < n; i++)
b67d701b 10355 {
de0b553f
APB
10356 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10357 tree rlist;
10358
de0b553f
APB
10359 rlist = find_applicable_accessible_methods_list (lc, t, name,
10360 arglist);
165f37bc 10361 list = chainon (rlist, list);
e04a16fb 10362 }
e04a16fb 10363 }
1982388a
APB
10364 /* Search classes */
10365 else
c2952b01 10366 {
165f37bc
APB
10367 tree sc = class;
10368 int seen_inner_class = 0;
c2952b01
APB
10369 search_applicable_methods_list (lc, TYPE_METHODS (class),
10370 name, arglist, &list, &all_list);
10371
165f37bc
APB
10372 /* We must search all interfaces of this class */
10373 if (!lc)
10374 {
10375 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10376 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10377 for (i = 1; i < n; i++)
10378 {
10379 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10380 if (t != object_type_node)
30a3caef
ZW
10381 {
10382 tree rlist
10383 = find_applicable_accessible_methods_list (lc, t,
10384 name, arglist);
10385 list = chainon (rlist, list);
10386 }
165f37bc 10387 }
165f37bc
APB
10388 }
10389
c2952b01
APB
10390 /* Search enclosing context of inner classes before looking
10391 ancestors up. */
10392 while (!lc && INNER_CLASS_TYPE_P (class))
10393 {
165f37bc
APB
10394 tree rlist;
10395 seen_inner_class = 1;
c2952b01 10396 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10397 rlist = find_applicable_accessible_methods_list (lc, class,
10398 name, arglist);
10399 list = chainon (rlist, list);
c2952b01 10400 }
165f37bc
APB
10401
10402 if (!lc && seen_inner_class
10403 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10404 class = CLASSTYPE_SUPER (sc);
10405 else
10406 class = sc;
10407
ad69b5b6
BM
10408 /* Search superclass */
10409 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10410 {
10411 tree rlist;
10412 class = CLASSTYPE_SUPER (class);
10413 rlist = find_applicable_accessible_methods_list (lc, class,
10414 name, arglist);
10415 list = chainon (rlist, list);
10416 }
10417 }
10418
10419 search_not_done--;
10420
10421 /* We're done. Reset the searched classes list and finally search
10422 java.lang.Object if it wasn't searched already. */
10423 if (!search_not_done)
10424 {
10425 if (!lc
10426 && TYPE_METHODS (object_type_node)
10427 && !hash_lookup (searched_classes,
10428 (const hash_table_key) object_type_node,
10429 FALSE, NULL))
10430 {
10431 search_applicable_methods_list (lc,
10432 TYPE_METHODS (object_type_node),
10433 name, arglist, &list, &all_list);
10434 }
10435 hash_table_free (searched_classes);
10436 searched_classes = NULL;
c2952b01 10437 }
1982388a 10438
b67d701b
PB
10439 /* Either return the list obtained or all selected (but
10440 inaccessible) methods for better error report. */
10441 return (!list ? all_list : list);
10442}
e04a16fb 10443
ad69b5b6 10444/* Effectively search for the appropriate method in method */
1982388a
APB
10445
10446static void
c2952b01 10447search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10448 int lc;
10449 tree method, name, arglist;
10450 tree *list, *all_list;
10451{
10452 for (; method; method = TREE_CHAIN (method))
10453 {
10454 /* When dealing with constructor, stop here, otherwise search
10455 other classes */
10456 if (lc && !DECL_CONSTRUCTOR_P (method))
10457 continue;
10458 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10459 || (GET_METHOD_NAME (method) != name)))
10460 continue;
10461
10462 if (argument_types_convertible (method, arglist))
10463 {
10464 /* Retain accessible methods only */
10465 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10466 method, 0))
10467 *list = tree_cons (NULL_TREE, method, *list);
10468 else
10469 /* Also retain all selected method here */
10470 *all_list = tree_cons (NULL_TREE, method, *list);
10471 }
10472 }
ad69b5b6 10473}
1982388a 10474
b67d701b
PB
10475/* 15.11.2.2 Choose the Most Specific Method */
10476
10477static tree
10478find_most_specific_methods_list (list)
10479 tree list;
10480{
10481 int max = 0;
9a7ab4b3 10482 int abstract, candidates;
b67d701b
PB
10483 tree current, new_list = NULL_TREE;
10484 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10485 {
b67d701b
PB
10486 tree method;
10487 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10488
10489 for (method = list; method; method = TREE_CHAIN (method))
10490 {
0c2b8145 10491 tree method_v, current_v;
b67d701b
PB
10492 /* Don't test a method against itself */
10493 if (method == current)
10494 continue;
10495
0c2b8145
APB
10496 method_v = TREE_VALUE (method);
10497 current_v = TREE_VALUE (current);
10498
10499 /* Compare arguments and location where methods where declared */
10500 if (argument_types_convertible (method_v, current_v))
b67d701b 10501 {
0c2b8145
APB
10502 if (valid_method_invocation_conversion_p
10503 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10504 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10505 && enclosing_context_p (DECL_CONTEXT (method_v),
10506 DECL_CONTEXT (current_v))))
10507 {
10508 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10509 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10510 max = (v > max ? v : max);
10511 }
b67d701b
PB
10512 }
10513 }
e04a16fb
AG
10514 }
10515
b67d701b 10516 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10517 for (current = list, abstract = -1, candidates = -1;
10518 current; current = TREE_CHAIN (current))
b67d701b 10519 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10520 {
10521 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10522 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10523 candidates++;
10524 }
b67d701b 10525
165f37bc
APB
10526 /* If we have several and they're all abstract, just pick the
10527 closest one. */
9a7ab4b3
APB
10528 if (candidates > 0 && (candidates == abstract))
10529 {
10530 new_list = nreverse (new_list);
10531 TREE_CHAIN (new_list) = NULL_TREE;
10532 }
165f37bc 10533
9a7ab4b3
APB
10534 /* We have several, we couldn't find a most specific, all but one are
10535 abstract, we pick the only non abstract one. */
10536 if (candidates > 0 && !max && (candidates == abstract+1))
165f37bc 10537 {
9a7ab4b3
APB
10538 for (current = new_list; current; current = TREE_CHAIN (current))
10539 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10540 {
10541 TREE_CHAIN (current) = NULL_TREE;
10542 new_list = current;
10543 }
165f37bc
APB
10544 }
10545
b67d701b
PB
10546 /* If we can't find one, lower expectations and try to gather multiple
10547 maximally specific methods */
165f37bc 10548 while (!new_list && max)
b67d701b
PB
10549 {
10550 while (--max > 0)
10551 {
10552 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10553 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10554 }
b67d701b
PB
10555 }
10556
10557 return new_list;
e04a16fb
AG
10558}
10559
b67d701b
PB
10560/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10561 converted by method invocation conversion (5.3) to the type of the
10562 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10563 to change less often than M1. */
e04a16fb 10564
b67d701b
PB
10565static int
10566argument_types_convertible (m1, m2_or_arglist)
10567 tree m1, m2_or_arglist;
e04a16fb 10568{
b67d701b
PB
10569 static tree m2_arg_value = NULL_TREE;
10570 static tree m2_arg_cache = NULL_TREE;
19e223db 10571 static int initialized_p;
e04a16fb 10572
b67d701b 10573 register tree m1_arg, m2_arg;
e04a16fb 10574
19e223db
MM
10575 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10576 collector. */
10577 if (!initialized_p)
10578 {
10579 ggc_add_tree_root (&m2_arg_value, 1);
10580 ggc_add_tree_root (&m2_arg_cache, 1);
10581 initialized_p = 1;
10582 }
10583
c2952b01 10584 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10585
b67d701b
PB
10586 if (m2_arg_value == m2_or_arglist)
10587 m2_arg = m2_arg_cache;
10588 else
10589 {
10590 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10591 argument types */
10592 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10593 {
10594 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10595 if (!METHOD_STATIC (m2_or_arglist))
10596 m2_arg = TREE_CHAIN (m2_arg);
10597 }
10598 else
10599 m2_arg = m2_or_arglist;
e04a16fb 10600
b67d701b
PB
10601 m2_arg_value = m2_or_arglist;
10602 m2_arg_cache = m2_arg;
10603 }
e04a16fb 10604
de4c7b02 10605 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10606 {
c877974e 10607 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10608 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10609 TREE_VALUE (m2_arg)))
10610 break;
10611 m1_arg = TREE_CHAIN (m1_arg);
10612 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10613 }
de4c7b02 10614 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10615}
10616
10617/* Qualification routines */
10618
10619static void
10620qualify_ambiguous_name (id)
10621 tree id;
10622{
cd531a2e
KG
10623 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10624 saved_current_class;
d8fccff5 10625 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10626 int code;
e04a16fb
AG
10627
10628 /* We first qualify the first element, then derive qualification of
10629 others based on the first one. If the first element is qualified
10630 by a resolution (field or type), this resolution is stored in the
10631 QUAL_RESOLUTION of the qual element being examined. We need to
10632 save the current_class since the use of SUPER might change the
10633 its value. */
10634 saved_current_class = current_class;
10635 qual = EXPR_WFL_QUALIFICATION (id);
10636 do {
10637
10638 /* Simple qualified expression feature a qual_wfl that is a
10639 WFL. Expression derived from a primary feature more complicated
10640 things like a CALL_EXPR. Expression from primary need to be
10641 worked out to extract the part on which the qualification will
10642 take place. */
10643 qual_wfl = QUAL_WFL (qual);
10644 switch (TREE_CODE (qual_wfl))
10645 {
10646 case CALL_EXPR:
10647 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10648 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10649 {
10650 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10651 qual_wfl = QUAL_WFL (qual);
10652 }
10653 break;
d8fccff5 10654 case NEW_ARRAY_EXPR:
c2952b01 10655 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10656 qual = TREE_CHAIN (qual);
1a6d4fb7 10657 again = new_array_found = 1;
d8fccff5 10658 continue;
e04a16fb 10659 case CONVERT_EXPR:
f2760b27
APB
10660 break;
10661 case NEW_CLASS_EXPR:
e04a16fb
AG
10662 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10663 break;
c583dd46
APB
10664 case ARRAY_REF:
10665 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10666 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10667 break;
8576f094
APB
10668 case STRING_CST:
10669 qual = TREE_CHAIN (qual);
10670 qual_wfl = QUAL_WFL (qual);
10671 break;
165f37bc
APB
10672 case CLASS_LITERAL:
10673 qual = TREE_CHAIN (qual);
10674 qual_wfl = QUAL_WFL (qual);
10675 break;
0a2138e2
APB
10676 default:
10677 /* Fix for -Wall. Just break doing nothing */
10678 break;
e04a16fb 10679 }
8576f094 10680
e04a16fb
AG
10681 ptr_type = current_class;
10682 again = 0;
8576f094
APB
10683 code = TREE_CODE (qual_wfl);
10684
10685 /* Pos evaluation: non WFL leading expression nodes */
10686 if (code == CONVERT_EXPR
10687 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10688 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10689
cd7c5840
APB
10690 else if (code == INTEGER_CST)
10691 name = qual_wfl;
10692
ac22f9cb 10693 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10694 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10695 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10696
c2952b01
APB
10697 else if (code == TREE_LIST)
10698 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10699
37feda7d
APB
10700 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10701 || code == PLUS_EXPR)
8576f094
APB
10702 {
10703 qual = TREE_CHAIN (qual);
10704 qual_wfl = QUAL_WFL (qual);
10705 again = 1;
10706 }
10707 else
f441f671
APB
10708 {
10709 name = EXPR_WFL_NODE (qual_wfl);
10710 if (!name)
10711 {
10712 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10713 again = 1;
10714 }
10715 }
10716
e04a16fb
AG
10717 /* If we have a THIS (from a primary), we set the context accordingly */
10718 if (name == this_identifier_node)
10719 {
6e22695a
APB
10720 /* This isn't really elegant. One more added irregularity
10721 before I start using COMPONENT_REF (hopefully very soon.) */
10722 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10723 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10724 EXPR_WITH_FILE_LOCATION
10725 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10726 this_identifier_node)
10727 {
10728 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10729 qual = EXPR_WFL_QUALIFICATION (qual);
10730 }
e04a16fb
AG
10731 qual = TREE_CHAIN (qual);
10732 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10733 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10734 again = 1;
10735 else
10736 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10737 this_found = 1;
10738 }
10739 /* If we have a SUPER, we set the context accordingly */
10740 if (name == super_identifier_node)
10741 {
10742 current_class = CLASSTYPE_SUPER (ptr_type);
10743 /* Check that there is such a thing as a super class. If not,
10744 return. The error will be caught later on, during the
10745 resolution */
10746 if (!current_class)
10747 {
10748 current_class = saved_current_class;
10749 return;
10750 }
10751 qual = TREE_CHAIN (qual);
10752 /* Do one more interation to set things up */
10753 super_found = again = 1;
10754 }
10755 } while (again);
10756
f2760b27
APB
10757 /* If name appears within the scope of a local variable declaration
10758 or parameter declaration, then it is an expression name. We don't
10759 carry this test out if we're in the context of the use of SUPER
10760 or THIS */
cd7c5840
APB
10761 if (!this_found && !super_found
10762 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10763 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10764 {
10765 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10766 QUAL_RESOLUTION (qual) = decl;
10767 }
10768
10769 /* If within the class/interface NAME was found to be used there
10770 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10771 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10772 address length, it is OK. */
10773 else if ((decl = lookup_field_wrapper (ptr_type, name))
10774 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10775 {
10776 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10777 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10778 }
10779
1a6d4fb7 10780 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10781 - NAME is a class/interface declared within the compilation
10782 unit containing NAME,
10783 - NAME is imported via a single-type-import declaration,
10784 - NAME is declared in an another compilation unit of the package
10785 of the compilation unit containing NAME,
10786 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10787 of the compilation unit containing NAME.
10788 - NAME is actually a STRING_CST. */
cd7c5840
APB
10789 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10790 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10791 {
10792 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10793 QUAL_RESOLUTION (qual) = decl;
10794 }
10795
f2760b27 10796 /* Method call, array references and cast are expression name */
9bbc7d9f 10797 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10798 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10799 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10800 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10801
10802 /* Check here that NAME isn't declared by more than one
10803 type-import-on-demand declaration of the compilation unit
10804 containing NAME. FIXME */
10805
10806 /* Otherwise, NAME is reclassified as a package name */
10807 else
10808 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10809
10810 /* Propagate the qualification accross other components of the
10811 qualified name */
10812 for (qual = TREE_CHAIN (qual); qual;
10813 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10814 {
10815 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10816 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10817 else
10818 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10819 }
10820
10821 /* Store the global qualification for the ambiguous part of ID back
10822 into ID fields */
10823 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10824 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10825 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10826 RESOLVE_TYPE_NAME_P (id) = 1;
10827 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10828 RESOLVE_PACKAGE_NAME_P (id) = 1;
10829
10830 /* Restore the current class */
10831 current_class = saved_current_class;
10832}
10833
10834static int
10835breakdown_qualified (left, right, source)
10836 tree *left, *right, source;
10837{
63ad61ed 10838 char *p, *base;
e04a16fb
AG
10839 int l = IDENTIFIER_LENGTH (source);
10840
63ad61ed
ZW
10841 base = alloca (l + 1);
10842 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10843
e04a16fb 10844 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10845 p = base + l - 1;
e04a16fb
AG
10846 while (*p != '.' && p != base)
10847 p--;
10848
10849 /* We didn't find a '.'. Return an error */
10850 if (p == base)
10851 return 1;
10852
10853 *p = '\0';
10854 if (right)
10855 *right = get_identifier (p+1);
63ad61ed 10856 *left = get_identifier (base);
e04a16fb
AG
10857
10858 return 0;
10859}
10860
e04a16fb 10861/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10862 local variable decls if present.
10863 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10864
10865static tree
10866java_complete_tree (node)
10867 tree node;
5b09b33e
PB
10868{
10869 node = java_complete_lhs (node);
0c2b8145
APB
10870 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10871 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10872 && !flag_emit_xref)
5b09b33e
PB
10873 {
10874 tree value = DECL_INITIAL (node);
10875 DECL_INITIAL (node) = NULL_TREE;
10876 value = fold_constant_for_init (value, node);
10877 DECL_INITIAL (node) = value;
10878 if (value != NULL_TREE)
c2952b01
APB
10879 {
10880 /* fold_constant_for_init sometimes widen the original type
10881 of the constant (i.e. byte to int.) It's not desirable,
10882 especially if NODE is a function argument. */
10883 if (TREE_CODE (value) == INTEGER_CST
10884 && TREE_TYPE (node) != TREE_TYPE (value))
10885 return convert (TREE_TYPE (node), value);
10886 else
10887 return value;
10888 }
5b09b33e
PB
10889 }
10890 return node;
10891}
10892
2aa11e97
APB
10893static tree
10894java_stabilize_reference (node)
10895 tree node;
10896{
10897 if (TREE_CODE (node) == COMPOUND_EXPR)
10898 {
10899 tree op0 = TREE_OPERAND (node, 0);
10900 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10901 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10902 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10903 return node;
10904 }
5cbdba64 10905 return stabilize_reference (node);
2aa11e97
APB
10906}
10907
5b09b33e
PB
10908/* Patch tree nodes in a function body. When a BLOCK is found, push
10909 local variable decls if present.
10910 Same as java_complete_tree, but does not resolve static finals to values. */
10911
10912static tree
10913java_complete_lhs (node)
10914 tree node;
e04a16fb 10915{
22eed1e6 10916 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10917 int flag;
e04a16fb
AG
10918
10919 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10920 worked out. */
e04a16fb
AG
10921 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10922 return node;
10923
10924 /* The switch block implements cases processing container nodes
10925 first. Contained nodes are always written back. Leaves come
10926 next and return a value. */
10927 switch (TREE_CODE (node))
10928 {
10929 case BLOCK:
10930
10931 /* 1- Block section.
10932 Set the local values on decl names so we can identify them
10933 faster when they're referenced. At that stage, identifiers
10934 are legal so we don't check for declaration errors. */
10935 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10936 {
10937 DECL_CONTEXT (cn) = current_function_decl;
10938 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10939 }
15fdcfe9
PB
10940 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10941 CAN_COMPLETE_NORMALLY (node) = 1;
10942 else
e04a16fb 10943 {
15fdcfe9
PB
10944 tree stmt = BLOCK_EXPR_BODY (node);
10945 tree *ptr;
10946 int error_seen = 0;
10947 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10948 {
c877974e
APB
10949 /* Re-order from (((A; B); C); ...; Z) to
10950 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10951 This makes it easier to scan the statements left-to-right
10952 without using recursion (which might overflow the stack
10953 if the block has many statements. */
10954 for (;;)
10955 {
10956 tree left = TREE_OPERAND (stmt, 0);
10957 if (TREE_CODE (left) != COMPOUND_EXPR)
10958 break;
10959 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10960 TREE_OPERAND (left, 1) = stmt;
10961 stmt = left;
10962 }
10963 BLOCK_EXPR_BODY (node) = stmt;
10964 }
10965
c877974e
APB
10966 /* Now do the actual complete, without deep recursion for
10967 long blocks. */
15fdcfe9 10968 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10969 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10970 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10971 {
10972 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10973 tree *next = &TREE_OPERAND (*ptr, 1);
10974 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10975 if (cur == empty_stmt_node)
10976 {
10977 /* Optimization; makes it easier to detect empty bodies.
10978 Most useful for <clinit> with all-constant initializer. */
10979 *ptr = *next;
10980 continue;
10981 }
15fdcfe9
PB
10982 if (TREE_CODE (cur) == ERROR_MARK)
10983 error_seen++;
10984 else if (! CAN_COMPLETE_NORMALLY (cur))
10985 {
10986 wfl_op2 = *next;
10987 for (;;)
10988 {
10989 if (TREE_CODE (wfl_op2) == BLOCK)
10990 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10991 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10992 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10993 else
10994 break;
10995 }
10996 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10997 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10998 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10999 }
11000 ptr = next;
11001 }
11002 *ptr = java_complete_tree (*ptr);
11003
11004 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 11005 return error_mark_node;
15fdcfe9 11006 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
11007 }
11008 /* Turn local bindings to null */
11009 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11010 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11011
11012 TREE_TYPE (node) = void_type_node;
11013 break;
11014
11015 /* 2- They are expressions but ultimately deal with statements */
b67d701b 11016
b9f7e36c
APB
11017 case THROW_EXPR:
11018 wfl_op1 = TREE_OPERAND (node, 0);
11019 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
11020 /* 14.19 A throw statement cannot complete normally. */
11021 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
11022 return patch_throw_statement (node, wfl_op1);
11023
11024 case SYNCHRONIZED_EXPR:
11025 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
11026 return patch_synchronized_statement (node, wfl_op1);
11027
b67d701b
PB
11028 case TRY_EXPR:
11029 return patch_try_statement (node);
11030
a7d8d81f
PB
11031 case TRY_FINALLY_EXPR:
11032 COMPLETE_CHECK_OP_0 (node);
11033 COMPLETE_CHECK_OP_1 (node);
11034 CAN_COMPLETE_NORMALLY (node)
11035 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11036 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11037 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11038 return node;
11039
5a005d9e
PB
11040 case CLEANUP_POINT_EXPR:
11041 COMPLETE_CHECK_OP_0 (node);
11042 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
11043 CAN_COMPLETE_NORMALLY (node) =
11044 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11045 return node;
11046
11047 case WITH_CLEANUP_EXPR:
11048 COMPLETE_CHECK_OP_0 (node);
11049 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
11050 CAN_COMPLETE_NORMALLY (node) =
11051 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
11052 TREE_TYPE (node) = void_type_node;
11053 return node;
11054
e04a16fb
AG
11055 case LABELED_BLOCK_EXPR:
11056 PUSH_LABELED_BLOCK (node);
11057 if (LABELED_BLOCK_BODY (node))
11058 COMPLETE_CHECK_OP_1 (node);
11059 TREE_TYPE (node) = void_type_node;
11060 POP_LABELED_BLOCK ();
1fb89a4d
APB
11061
11062 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
11063 {
11064 LABELED_BLOCK_BODY (node) = NULL_TREE;
11065 CAN_COMPLETE_NORMALLY (node) = 1;
11066 }
1fb89a4d 11067 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 11068 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11069 return node;
11070
11071 case EXIT_BLOCK_EXPR:
11072 /* We don't complete operand 1, because it's the return value of
11073 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11074 return patch_bc_statement (node);
11075
15fdcfe9
PB
11076 case CASE_EXPR:
11077 cn = java_complete_tree (TREE_OPERAND (node, 0));
11078 if (cn == error_mark_node)
11079 return cn;
11080
8576f094
APB
11081 /* First, the case expression must be constant. Values of final
11082 fields are accepted. */
15fdcfe9 11083 cn = fold (cn);
8576f094
APB
11084 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11085 && JDECL_P (TREE_OPERAND (cn, 1))
11086 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11087 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8 11088 {
100f7cd8
APB
11089 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11090 TREE_OPERAND (cn, 1));
100f7cd8 11091 }
15fdcfe9 11092
ce6e9147 11093 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11094 {
11095 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11096 parse_error_context (node, "Constant expression required");
11097 return error_mark_node;
11098 }
11099
11100 nn = ctxp->current_loop;
11101
11102 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11103 if (!try_builtin_assignconv (NULL_TREE,
11104 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11105 {
11106 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11107 parse_error_context
11108 (wfl_operator,
11109 "Incompatible type for case. Can't convert `%s' to `int'",
11110 lang_printable_name (TREE_TYPE (cn), 0));
11111 return error_mark_node;
11112 }
11113
11114 cn = fold (convert (int_type_node, cn));
11115
11116 /* Multiple instance of a case label bearing the same
11117 value is checked during code generation. The case
11118 expression is allright so far. */
34d4df06
APB
11119 if (TREE_CODE (cn) == VAR_DECL)
11120 cn = DECL_INITIAL (cn);
15fdcfe9 11121 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11122 TREE_TYPE (node) = void_type_node;
15fdcfe9 11123 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11124 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11125 break;
11126
11127 case DEFAULT_EXPR:
11128 nn = ctxp->current_loop;
11129 /* Only one default label is allowed per switch statement */
11130 if (SWITCH_HAS_DEFAULT (nn))
11131 {
11132 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11133 parse_error_context (wfl_operator,
11134 "Duplicate case label: `default'");
11135 return error_mark_node;
11136 }
11137 else
11138 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11139 TREE_TYPE (node) = void_type_node;
10100cc7 11140 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11141 CAN_COMPLETE_NORMALLY (node) = 1;
11142 break;
11143
b67d701b 11144 case SWITCH_EXPR:
e04a16fb
AG
11145 case LOOP_EXPR:
11146 PUSH_LOOP (node);
11147 /* Check whether the loop was enclosed in a labeled
11148 statement. If not, create one, insert the loop in it and
11149 return the node */
11150 nn = patch_loop_statement (node);
b67d701b 11151
e04a16fb 11152 /* Anyways, walk the body of the loop */
b67d701b
PB
11153 if (TREE_CODE (node) == LOOP_EXPR)
11154 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11155 /* Switch statement: walk the switch expression and the cases */
11156 else
11157 node = patch_switch_statement (node);
11158
e7c7bcef 11159 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11160 nn = error_mark_node;
11161 else
15fdcfe9 11162 {
b635eb2f
PB
11163 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11164 /* If we returned something different, that's because we
11165 inserted a label. Pop the label too. */
11166 if (nn != node)
11167 {
11168 if (CAN_COMPLETE_NORMALLY (node))
11169 CAN_COMPLETE_NORMALLY (nn) = 1;
11170 POP_LABELED_BLOCK ();
11171 }
15fdcfe9 11172 }
e04a16fb
AG
11173 POP_LOOP ();
11174 return nn;
11175
11176 case EXIT_EXPR:
11177 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11178 return patch_exit_expr (node);
11179
11180 case COND_EXPR:
11181 /* Condition */
11182 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11183 if (TREE_OPERAND (node, 0) == error_mark_node)
11184 return error_mark_node;
11185 /* then-else branches */
11186 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11187 if (TREE_OPERAND (node, 1) == error_mark_node)
11188 return error_mark_node;
11189 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11190 if (TREE_OPERAND (node, 2) == error_mark_node)
11191 return error_mark_node;
11192 return patch_if_else_statement (node);
11193 break;
11194
22eed1e6
APB
11195 case CONDITIONAL_EXPR:
11196 /* Condition */
11197 wfl_op1 = TREE_OPERAND (node, 0);
11198 COMPLETE_CHECK_OP_0 (node);
11199 wfl_op2 = TREE_OPERAND (node, 1);
11200 COMPLETE_CHECK_OP_1 (node);
11201 wfl_op3 = TREE_OPERAND (node, 2);
11202 COMPLETE_CHECK_OP_2 (node);
11203 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11204
e04a16fb
AG
11205 /* 3- Expression section */
11206 case COMPOUND_EXPR:
15fdcfe9 11207 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11208 TREE_OPERAND (node, 0) = nn =
11209 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11210 if (wfl_op2 == empty_stmt_node)
11211 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11212 else
15fdcfe9 11213 {
dc0b3eff 11214 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11215 {
dc0b3eff
PB
11216 /* An unreachable condition in a do-while statement
11217 is *not* (technically) an unreachable statement. */
11218 nn = wfl_op2;
11219 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11220 nn = EXPR_WFL_NODE (nn);
11221 if (TREE_CODE (nn) != EXIT_EXPR)
11222 {
11223 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11224 parse_error_context (wfl_operator, "Unreachable statement");
11225 }
bccaf73a 11226 }
dc0b3eff
PB
11227 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11228 if (TREE_OPERAND (node, 1) == error_mark_node)
11229 return error_mark_node;
11230 CAN_COMPLETE_NORMALLY (node)
11231 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11232 }
e04a16fb
AG
11233 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11234 break;
11235
11236 case RETURN_EXPR:
15fdcfe9 11237 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11238 return patch_return (node);
11239
11240 case EXPR_WITH_FILE_LOCATION:
11241 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11242 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11243 {
5423609c 11244 tree wfl = node;
15fdcfe9 11245 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11246 if (node == error_mark_node)
11247 return node;
5423609c
APB
11248 /* Keep line number information somewhere were it doesn't
11249 disrupt the completion process. */
2c56429a 11250 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11251 {
11252 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11253 TREE_OPERAND (node, 1) = wfl;
11254 }
15fdcfe9
PB
11255 CAN_COMPLETE_NORMALLY (node) = 1;
11256 }
e04a16fb
AG
11257 else
11258 {
5b09b33e
PB
11259 tree body;
11260 int save_lineno = lineno;
11261 lineno = EXPR_WFL_LINENO (node);
11262 body = java_complete_tree (EXPR_WFL_NODE (node));
11263 lineno = save_lineno;
15fdcfe9 11264 EXPR_WFL_NODE (node) = body;
dc0b3eff 11265 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11266 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11267 if (body == empty_stmt_node)
11268 {
11269 /* Optimization; makes it easier to detect empty bodies. */
11270 return body;
11271 }
dc0b3eff 11272 if (body == error_mark_node)
e04a16fb
AG
11273 {
11274 /* Its important for the evaluation of assignment that
11275 this mark on the TREE_TYPE is propagated. */
11276 TREE_TYPE (node) = error_mark_node;
11277 return error_mark_node;
11278 }
11279 else
11280 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11281
e04a16fb
AG
11282 }
11283 break;
11284
b67d701b 11285 case NEW_ARRAY_EXPR:
e04a16fb
AG
11286 /* Patch all the dimensions */
11287 flag = 0;
11288 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11289 {
11290 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11291 tree dim = convert (int_type_node,
11292 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11293 if (dim == error_mark_node)
11294 {
11295 flag = 1;
11296 continue;
11297 }
11298 else
11299 {
b9f7e36c 11300 TREE_VALUE (cn) = dim;
e04a16fb
AG
11301 /* Setup the location of the current dimension, for
11302 later error report. */
11303 TREE_PURPOSE (cn) =
11304 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11305 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11306 }
11307 }
11308 /* They complete the array creation expression, if no errors
11309 were found. */
15fdcfe9 11310 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11311 return (flag ? error_mark_node
11312 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11313
c2952b01
APB
11314 case NEW_ANONYMOUS_ARRAY_EXPR:
11315 /* Create the array type if necessary. */
11316 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11317 {
11318 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11319 if (!(type = resolve_type_during_patch (type)))
11320 return error_mark_node;
11321 type = build_array_from_name (type, NULL_TREE,
11322 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11323 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11324 }
11325 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11326 ANONYMOUS_ARRAY_INITIALIZER (node));
11327 if (node == error_mark_node)
11328 return error_mark_node;
11329 CAN_COMPLETE_NORMALLY (node) = 1;
11330 return node;
11331
b67d701b 11332 case NEW_CLASS_EXPR:
e04a16fb 11333 case CALL_EXPR:
b67d701b 11334 /* Complete function's argument(s) first */
e04a16fb
AG
11335 if (complete_function_arguments (node))
11336 return error_mark_node;
11337 else
b9f7e36c 11338 {
22eed1e6
APB
11339 tree decl, wfl = TREE_OPERAND (node, 0);
11340 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11341
c877974e 11342 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11343 NULL_TREE, 0, &decl);
c877974e
APB
11344 if (node == error_mark_node)
11345 return error_mark_node;
11346
11347 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11348 /* If we call this(...), register signature and positions */
11349 if (in_this)
11350 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11351 tree_cons (wfl, decl,
11352 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11353 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11354 return force_evaluation_order (node);
b9f7e36c 11355 }
e04a16fb
AG
11356
11357 case MODIFY_EXPR:
11358 /* Save potential wfls */
11359 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11360 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11361
cd9643f7
PB
11362 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11363 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11364 && DECL_INITIAL (nn) != NULL_TREE)
11365 {
100f7cd8
APB
11366 tree value;
11367
100f7cd8 11368 value = fold_constant_for_init (nn, nn);
c2952b01 11369
cd9643f7
PB
11370 if (value != NULL_TREE)
11371 {
11372 tree type = TREE_TYPE (value);
c2952b01
APB
11373 if (JPRIMITIVE_TYPE_P (type) ||
11374 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11375 return empty_stmt_node;
11376 }
629d4b4d
APB
11377 if (! flag_emit_class_files)
11378 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11379 }
e04a16fb 11380 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11381
e04a16fb
AG
11382 if (TREE_OPERAND (node, 0) == error_mark_node)
11383 return error_mark_node;
11384
5cbdba64
APB
11385 flag = COMPOUND_ASSIGN_P (wfl_op2);
11386 if (flag)
e04a16fb 11387 {
c2952b01
APB
11388 /* This might break when accessing outer field from inner
11389 class. TESTME, FIXME */
2aa11e97 11390 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11391
11392 /* Hand stablize the lhs on both places */
e04a16fb 11393 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11394 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11395 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11396
5cbdba64 11397 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11398 /* Now complete the RHS. We write it back later on. */
11399 nn = java_complete_tree (TREE_OPERAND (node, 1));
11400
642f15d1
APB
11401 if ((cn = patch_string (nn)))
11402 nn = cn;
11403
2aa11e97
APB
11404 /* The last part of the rewrite for E1 op= E2 is to have
11405 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11406 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11407 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11408
11409 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11410 }
11411
f8976021 11412 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11413 function to complete this RHS. Note that a NEW_ARRAY_INIT
11414 might have been already fully expanded if created as a result
11415 of processing an anonymous array initializer. We avoid doing
11416 the operation twice by testing whether the node already bears
11417 a type. */
11418 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11419 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11420 TREE_OPERAND (node, 1));
2aa11e97 11421 /* Otherwise we simply complete the RHS */
f8976021
APB
11422 else
11423 nn = java_complete_tree (TREE_OPERAND (node, 1));
11424
e04a16fb 11425 if (nn == error_mark_node)
c0d87ff6 11426 return error_mark_node;
2aa11e97
APB
11427
11428 /* Write back the RHS as we evaluated it. */
e04a16fb 11429 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11430
11431 /* In case we're handling = with a String as a RHS, we need to
11432 produce a String out of the RHS (it might still be a
11433 STRING_CST or a StringBuffer at this stage */
11434 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11435 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11436
11437 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11438 TREE_OPERAND (node, 1))))
11439 {
11440 /* We return error_mark_node if outer_field_access_fix
11441 detects we write into a final. */
11442 if (nn == error_mark_node)
11443 return error_mark_node;
11444 node = nn;
11445 }
11446 else
11447 {
11448 node = patch_assignment (node, wfl_op1, wfl_op2);
11449 /* Reorganize the tree if necessary. */
11450 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11451 || JSTRING_P (TREE_TYPE (node))))
11452 node = java_refold (node);
11453 }
11454
15fdcfe9
PB
11455 CAN_COMPLETE_NORMALLY (node) = 1;
11456 return node;
e04a16fb
AG
11457
11458 case MULT_EXPR:
11459 case PLUS_EXPR:
11460 case MINUS_EXPR:
11461 case LSHIFT_EXPR:
11462 case RSHIFT_EXPR:
11463 case URSHIFT_EXPR:
11464 case BIT_AND_EXPR:
11465 case BIT_XOR_EXPR:
11466 case BIT_IOR_EXPR:
11467 case TRUNC_MOD_EXPR:
c2952b01 11468 case TRUNC_DIV_EXPR:
e04a16fb
AG
11469 case RDIV_EXPR:
11470 case TRUTH_ANDIF_EXPR:
11471 case TRUTH_ORIF_EXPR:
11472 case EQ_EXPR:
11473 case NE_EXPR:
11474 case GT_EXPR:
11475 case GE_EXPR:
11476 case LT_EXPR:
11477 case LE_EXPR:
11478 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11479 knows how to handle those cases. */
11480 wfl_op1 = TREE_OPERAND (node, 0);
11481 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11482
15fdcfe9 11483 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11484 /* Don't complete string nodes if dealing with the PLUS operand. */
11485 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11486 {
11487 nn = java_complete_tree (wfl_op1);
11488 if (nn == error_mark_node)
11489 return error_mark_node;
48a840d9 11490
2aa11e97
APB
11491 TREE_OPERAND (node, 0) = nn;
11492 }
b67d701b 11493 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11494 {
11495 nn = java_complete_tree (wfl_op2);
11496 if (nn == error_mark_node)
11497 return error_mark_node;
48a840d9 11498
2aa11e97
APB
11499 TREE_OPERAND (node, 1) = nn;
11500 }
dc0b3eff 11501 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11502
5e942c50
APB
11503 case INSTANCEOF_EXPR:
11504 wfl_op1 = TREE_OPERAND (node, 0);
11505 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11506 if (flag_emit_xref)
11507 {
11508 TREE_TYPE (node) = boolean_type_node;
11509 return node;
11510 }
5e942c50
APB
11511 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11512
b67d701b 11513 case UNARY_PLUS_EXPR:
e04a16fb
AG
11514 case NEGATE_EXPR:
11515 case TRUTH_NOT_EXPR:
11516 case BIT_NOT_EXPR:
11517 case PREDECREMENT_EXPR:
11518 case PREINCREMENT_EXPR:
11519 case POSTDECREMENT_EXPR:
11520 case POSTINCREMENT_EXPR:
11521 case CONVERT_EXPR:
11522 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11523 how to handle those cases. */
11524 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11525 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11526 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11527 if (TREE_OPERAND (node, 0) == error_mark_node)
11528 return error_mark_node;
4a5f66c3
APB
11529 node = patch_unaryop (node, wfl_op1);
11530 CAN_COMPLETE_NORMALLY (node) = 1;
11531 break;
e04a16fb
AG
11532
11533 case ARRAY_REF:
11534 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11535 how to handle those cases. */
11536 wfl_op1 = TREE_OPERAND (node, 0);
11537 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11538 if (TREE_OPERAND (node, 0) == error_mark_node)
11539 return error_mark_node;
7f1d4866 11540 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11541 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11542 /* The same applies to wfl_op2 */
11543 wfl_op2 = TREE_OPERAND (node, 1);
11544 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11545 if (TREE_OPERAND (node, 1) == error_mark_node)
11546 return error_mark_node;
7f1d4866 11547 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11548 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11549 return patch_array_ref (node);
e04a16fb 11550
63a212ed
PB
11551 case RECORD_TYPE:
11552 return node;;
11553
11554 case COMPONENT_REF:
11555 /* The first step in the re-write of qualified name handling. FIXME.
11556 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11557 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11558 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11559 {
11560 tree name = TREE_OPERAND (node, 1);
11561 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11562 if (field == NULL_TREE)
11563 {
11564 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11565 return error_mark_node;
11566 }
11567 if (! FIELD_STATIC (field))
11568 {
11569 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11570 return error_mark_node;
11571 }
11572 return field;
11573 }
11574 else
11575 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11576 break;
9bbc7d9f 11577
b67d701b 11578 case THIS_EXPR:
e04a16fb
AG
11579 /* Can't use THIS in a static environment */
11580 if (!current_this)
11581 {
11582 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11583 parse_error_context (wfl_operator,
11584 "Keyword `this' used outside allowed context");
e04a16fb
AG
11585 TREE_TYPE (node) = error_mark_node;
11586 return error_mark_node;
11587 }
22eed1e6
APB
11588 if (ctxp->explicit_constructor_p)
11589 {
11590 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11591 parse_error_context
781b0558 11592 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11593 TREE_TYPE (node) = error_mark_node;
11594 return error_mark_node;
11595 }
e04a16fb 11596 return current_this;
c2952b01
APB
11597
11598 case CLASS_LITERAL:
11599 CAN_COMPLETE_NORMALLY (node) = 1;
11600 node = patch_incomplete_class_ref (node);
11601 if (node == error_mark_node)
11602 return error_mark_node;
11603 break;
11604
11605 case INSTANCE_INITIALIZERS_EXPR:
11606 in_instance_initializer++;
11607 node = java_complete_tree (TREE_OPERAND (node, 0));
11608 in_instance_initializer--;
11609 if (node != error_mark_node)
11610 TREE_TYPE (node) = void_type_node;
11611 else
11612 return error_mark_node;
11613 break;
e04a16fb 11614
e04a16fb 11615 default:
15fdcfe9 11616 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11617 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11618 and it's time to turn it into the appropriate String object */
11619 if ((nn = patch_string (node)))
11620 node = nn;
11621 else
11622 fatal ("No case for tree code `%s' - java_complete_tree\n",
11623 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11624 }
11625 return node;
11626}
11627
11628/* Complete function call's argument. Return a non zero value is an
11629 error was found. */
11630
11631static int
11632complete_function_arguments (node)
11633 tree node;
11634{
11635 int flag = 0;
11636 tree cn;
11637
f63991a8 11638 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11639 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11640 {
b67d701b 11641 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11642 parm = java_complete_tree (wfl);
c2952b01 11643
e04a16fb
AG
11644 if (parm == error_mark_node)
11645 {
11646 flag = 1;
11647 continue;
11648 }
b67d701b
PB
11649 /* If have a string literal that we haven't transformed yet or a
11650 crafted string buffer, as a result of use of the the String
11651 `+' operator. Build `parm.toString()' and expand it. */
11652 if ((temp = patch_string (parm)))
b9f7e36c 11653 parm = temp;
5e942c50
APB
11654 /* Inline PRIMTYPE.TYPE read access */
11655 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11656
5e942c50 11657 TREE_VALUE (cn) = parm;
e04a16fb 11658 }
f63991a8 11659 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11660 return flag;
11661}
11662
11663/* Sometimes (for loops and variable initialized during their
11664 declaration), we want to wrap a statement around a WFL and turn it
11665 debugable. */
11666
11667static tree
11668build_debugable_stmt (location, stmt)
11669 int location;
11670 tree stmt;
11671{
11672 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11673 {
11674 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11675 EXPR_WFL_LINECOL (stmt) = location;
11676 }
11677 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11678 return stmt;
11679}
11680
11681static tree
11682build_expr_block (body, decls)
11683 tree body, decls;
11684{
11685 tree node = make_node (BLOCK);
11686 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11687 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11688 if (body)
11689 TREE_TYPE (node) = TREE_TYPE (body);
11690 TREE_SIDE_EFFECTS (node) = 1;
11691 return node;
11692}
11693
b67d701b
PB
11694/* Create a new function block and link it approriately to current
11695 function block chain */
e04a16fb
AG
11696
11697static tree
11698enter_block ()
11699{
b67d701b
PB
11700 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11701}
11702
11703/* Link block B supercontext to the previous block. The current
11704 function DECL is used as supercontext when enter_a_block is called
11705 for the first time for a given function. The current function body
11706 (DECL_FUNCTION_BODY) is set to be block B. */
11707
11708static tree
11709enter_a_block (b)
11710 tree b;
11711{
e04a16fb
AG
11712 tree fndecl = current_function_decl;
11713
f099f336
APB
11714 if (!fndecl) {
11715 BLOCK_SUPERCONTEXT (b) = current_static_block;
11716 current_static_block = b;
11717 }
11718
11719 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11720 {
11721 BLOCK_SUPERCONTEXT (b) = fndecl;
11722 DECL_FUNCTION_BODY (fndecl) = b;
11723 }
11724 else
11725 {
11726 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11727 DECL_FUNCTION_BODY (fndecl) = b;
11728 }
11729 return b;
11730}
11731
11732/* Exit a block by changing the current function body
11733 (DECL_FUNCTION_BODY) to the current block super context, only if
11734 the block being exited isn't the method's top level one. */
11735
11736static tree
11737exit_block ()
11738{
f099f336
APB
11739 tree b;
11740 if (current_function_decl)
11741 {
11742 b = DECL_FUNCTION_BODY (current_function_decl);
11743 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11744 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11745 }
11746 else
11747 {
11748 b = current_static_block;
e04a16fb 11749
f099f336
APB
11750 if (BLOCK_SUPERCONTEXT (b))
11751 current_static_block = BLOCK_SUPERCONTEXT (b);
11752 }
e04a16fb
AG
11753 return b;
11754}
11755
11756/* Lookup for NAME in the nested function's blocks, all the way up to
11757 the current toplevel one. It complies with Java's local variable
11758 scoping rules. */
11759
11760static tree
11761lookup_name_in_blocks (name)
11762 tree name;
11763{
f099f336 11764 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11765
11766 while (b != current_function_decl)
11767 {
11768 tree current;
11769
11770 /* Paranoid sanity check. To be removed */
11771 if (TREE_CODE (b) != BLOCK)
11772 fatal ("non block expr function body - lookup_name_in_blocks");
11773
11774 for (current = BLOCK_EXPR_DECLS (b); current;
11775 current = TREE_CHAIN (current))
11776 if (DECL_NAME (current) == name)
11777 return current;
11778 b = BLOCK_SUPERCONTEXT (b);
11779 }
11780 return NULL_TREE;
11781}
11782
11783static void
11784maybe_absorb_scoping_blocks ()
11785{
f099f336 11786 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11787 {
11788 tree b = exit_block ();
11789 java_method_add_stmt (current_function_decl, b);
11790 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11791 }
11792}
11793
11794\f
11795/* This section of the source is reserved to build_* functions that
11796 are building incomplete tree nodes and the patch_* functions that
11797 are completing them. */
11798
c2952b01
APB
11799/* Wrap a non WFL node around a WFL. */
11800static tree
9a7ab4b3 11801build_wfl_wrap (node, location)
c2952b01 11802 tree node;
9a7ab4b3 11803 int location;
c2952b01
APB
11804{
11805 tree wfl, node_to_insert = node;
11806
11807 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11808 with the way we're processing SUPER. A THIS from a primary as a
11809 different form than a SUPER. Turn THIS into something symbolic */
11810 if (TREE_CODE (node) == THIS_EXPR)
11811 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11812 else
11813 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11814
9a7ab4b3 11815 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11816 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11817 return wfl;
11818}
11819
11820
9bbc7d9f 11821/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11822 we're currently dealing with the class java.lang.Object. */
11823
11824static tree
e920ebc9
APB
11825build_super_invocation (mdecl)
11826 tree mdecl;
22eed1e6 11827{
e920ebc9 11828 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11829 return empty_stmt_node;
22eed1e6
APB
11830 else
11831 {
9ee9b555 11832 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11833 tree a = NULL_TREE, t;
11834 /* If we're dealing with an anonymous class, pass the arguments
11835 of the crafted constructor along. */
11836 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11837 {
11838 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11839 for (; t != end_params_node; t = TREE_CHAIN (t))
11840 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11841 }
11842 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11843 }
11844}
11845
11846/* Build a SUPER/THIS qualified method invocation. */
11847
11848static tree
11849build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11850 int use_this;
11851 tree name, args;
11852 int lloc, rloc;
22eed1e6
APB
11853{
11854 tree invok;
11855 tree wfl =
9ee9b555 11856 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11857 EXPR_WFL_LINECOL (wfl) = lloc;
11858 invok = build_method_invocation (name, args);
11859 return make_qualified_primary (wfl, invok, rloc);
11860}
11861
b67d701b 11862/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11863
11864static tree
11865build_method_invocation (name, args)
11866 tree name;
11867 tree args;
11868{
11869 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11870 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11871 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11872 return call;
11873}
11874
11875/* Build an incomplete new xxx(...) node. */
11876
11877static tree
11878build_new_invocation (name, args)
11879 tree name, args;
11880{
11881 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11882 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11883 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11884 return call;
11885}
11886
11887/* Build an incomplete assignment expression. */
11888
11889static tree
11890build_assignment (op, op_location, lhs, rhs)
11891 int op, op_location;
11892 tree lhs, rhs;
11893{
11894 tree assignment;
11895 /* Build the corresponding binop if we deal with a Compound
11896 Assignment operator. Mark the binop sub-tree as part of a
11897 Compound Assignment expression */
11898 if (op != ASSIGN_TK)
11899 {
11900 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11901 COMPOUND_ASSIGN_P (rhs) = 1;
11902 }
11903 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11904 TREE_SIDE_EFFECTS (assignment) = 1;
11905 EXPR_WFL_LINECOL (assignment) = op_location;
11906 return assignment;
11907}
11908
11909/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11910
15fdcfe9 11911char *
e04a16fb
AG
11912print_int_node (node)
11913 tree node;
11914{
11915 static char buffer [80];
11916 if (TREE_CONSTANT_OVERFLOW (node))
11917 sprintf (buffer, "<overflow>");
11918
11919 if (TREE_INT_CST_HIGH (node) == 0)
11920 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11921 TREE_INT_CST_LOW (node));
11922 else if (TREE_INT_CST_HIGH (node) == -1
11923 && TREE_INT_CST_LOW (node) != 0)
11924 {
11925 buffer [0] = '-';
11926 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11927 -TREE_INT_CST_LOW (node));
11928 }
11929 else
11930 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11931 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11932
11933 return buffer;
11934}
11935
7f1d4866
APB
11936/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11937 context. */
5e942c50
APB
11938
11939static int
11940check_final_assignment (lvalue, wfl)
11941 tree lvalue, wfl;
11942{
6632dcdd
APB
11943 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11944 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11945 lvalue = TREE_OPERAND (lvalue, 1);
11946
bc2874c9
TT
11947 /* When generating class files, references to the `length' field
11948 look a bit different. */
11949 if ((flag_emit_class_files
11950 && TREE_CODE (lvalue) == COMPONENT_REF
11951 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11952 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11953 || (TREE_CODE (lvalue) == FIELD_DECL
11954 && FIELD_FINAL (lvalue)
11955 && !DECL_CLINIT_P (current_function_decl)
11956 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11957 {
11958 parse_error_context
11959 (wfl, "Can't assign a value to the final variable `%s'",
11960 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11961 return 1;
11962 }
11963 return 0;
11964}
11965
11966/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11967 read. This is needed to avoid circularities in the implementation
11968 of these fields in libjava. */
11969
11970static tree
11971maybe_build_primttype_type_ref (rhs, wfl)
11972 tree rhs, wfl;
11973{
11974 tree to_return = NULL_TREE;
11975 tree rhs_type = TREE_TYPE (rhs);
11976 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11977 {
11978 tree n = TREE_OPERAND (rhs, 1);
11979 if (TREE_CODE (n) == VAR_DECL
11980 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
11981 && rhs_type == class_ptr_type
11982 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 11983 {
49f48c71 11984 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11985 if (!strncmp (self_name, "java.lang.", 10))
11986 to_return = build_primtype_type_ref (self_name);
11987 }
11988 }
11989 return (to_return ? to_return : rhs );
11990}
11991
e04a16fb
AG
11992/* 15.25 Assignment operators. */
11993
11994static tree
11995patch_assignment (node, wfl_op1, wfl_op2)
11996 tree node;
11997 tree wfl_op1;
11998 tree wfl_op2;
11999{
0a2138e2 12000 tree rhs = TREE_OPERAND (node, 1);
5e942c50 12001 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 12002 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
12003 int error_found = 0;
12004 int lvalue_from_array = 0;
12005
c2952b01 12006 /* Can't assign to a (blank) final. */
5e942c50
APB
12007 if (check_final_assignment (lvalue, wfl_op1))
12008 error_found = 1;
e04a16fb
AG
12009
12010 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12011
12012 /* Lhs can be a named variable */
34f4db93 12013 if (JDECL_P (lvalue))
e04a16fb 12014 {
e04a16fb
AG
12015 lhs_type = TREE_TYPE (lvalue);
12016 }
12017 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12018 comment on reason why */
12019 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12020 {
12021 lhs_type = TREE_TYPE (lvalue);
12022 lvalue_from_array = 1;
12023 }
12024 /* Or a field access */
12025 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12026 lhs_type = TREE_TYPE (lvalue);
12027 /* Or a function return slot */
12028 else if (TREE_CODE (lvalue) == RESULT_DECL)
12029 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12030 /* Otherwise, we might want to try to write into an optimized static
12031 final, this is an of a different nature, reported further on. */
12032 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 12033 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 12034 {
6632dcdd 12035 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
12036 {
12037 /* What we should do instead is resetting the all the flags
12038 previously set, exchange lvalue for llvalue and continue. */
12039 error_found = 1;
12040 return error_mark_node;
12041 }
12042 else
12043 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
12044 }
12045 else
e04a16fb
AG
12046 {
12047 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12048 error_found = 1;
12049 }
12050
12051 rhs_type = TREE_TYPE (rhs);
b67d701b 12052 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 12053 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 12054
b67d701b 12055 /* 5.2 If it failed, try a reference conversion */
0a2138e2 12056 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 12057 lhs_type = promote_type (rhs_type);
e04a16fb
AG
12058
12059 /* 15.25.2 If we have a compound assignment, convert RHS into the
12060 type of the LHS */
12061 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12062 new_rhs = convert (lhs_type, rhs);
12063
12064 /* Explicit cast required. This is an error */
12065 if (!new_rhs)
12066 {
c2e3db92
KG
12067 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12068 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
12069 tree wfl;
12070 char operation [32]; /* Max size known */
12071
12072 /* If the assignment is part of a declaration, we use the WFL of
12073 the declared variable to point out the error and call it a
12074 declaration problem. If the assignment is a genuine =
12075 operator, we call is a operator `=' problem, otherwise we
12076 call it an assignment problem. In both of these last cases,
12077 we use the WFL of the operator to indicate the error. */
12078
12079 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12080 {
12081 wfl = wfl_op1;
12082 strcpy (operation, "declaration");
12083 }
12084 else
12085 {
12086 wfl = wfl_operator;
12087 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12088 strcpy (operation, "assignment");
12089 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12090 strcpy (operation, "`return'");
12091 else
12092 strcpy (operation, "`='");
12093 }
12094
1ebadc60 12095 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12096 parse_error_context
12097 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12098 operation, t1, t2);
1ebadc60 12099 else
781b0558 12100 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12101 operation, t1, t2);
e04a16fb
AG
12102 free (t1); free (t2);
12103 error_found = 1;
12104 }
12105
c877974e
APB
12106 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12107 if (new_rhs)
12108 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12109
e04a16fb
AG
12110 if (error_found)
12111 return error_mark_node;
12112
2622b947
APB
12113 /* 10.10: Array Store Exception runtime check */
12114 if (!flag_emit_class_files
e8fc7396 12115 && !flag_emit_xref
2622b947 12116 && lvalue_from_array
afc390b1 12117 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12118 {
12119 tree check;
12120 tree base = lvalue;
12121
12122 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12123 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12124 base = TREE_OPERAND (lvalue, 0);
12125 else
12126 {
12127 if (flag_bounds_check)
12128 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12129 else
12130 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12131 }
12132
12133 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12134 new_rhs = save_expr (new_rhs);
2622b947
APB
12135 check = build (CALL_EXPR, void_type_node,
12136 build_address_of (soft_checkarraystore_node),
12137 tree_cons (NULL_TREE, base,
12138 build_tree_list (NULL_TREE, new_rhs)),
12139 NULL_TREE);
12140 TREE_SIDE_EFFECTS (check) = 1;
12141
12142 /* We have to decide on an insertion point */
12143 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12144 {
12145 tree t;
12146 if (flag_bounds_check)
12147 {
12148 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12149 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12150 build (COMPOUND_EXPR, void_type_node, t, check);
12151 }
12152 else
12153 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12154 check, TREE_OPERAND (lvalue, 1));
12155 }
12156 else
12157 {
12158 /* Make sure the bound check will happen before the store check */
12159 if (flag_bounds_check)
12160 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12161 build (COMPOUND_EXPR, void_type_node,
12162 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12163 else
12164 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12165 }
12166 }
22eed1e6 12167
34d4df06
APB
12168 /* Final locals can be used as case values in switch
12169 statement. Prepare them for this eventuality. */
12170 if (TREE_CODE (lvalue) == VAR_DECL
12171 && LOCAL_FINAL (lvalue)
12172 && TREE_CONSTANT (new_rhs)
12173 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12174 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12175 )
12176 {
12177 TREE_CONSTANT (lvalue) = 1;
12178 DECL_INITIAL (lvalue) = new_rhs;
12179 }
12180
e04a16fb
AG
12181 TREE_OPERAND (node, 0) = lvalue;
12182 TREE_OPERAND (node, 1) = new_rhs;
12183 TREE_TYPE (node) = lhs_type;
12184 return node;
12185}
12186
b67d701b
PB
12187/* Check that type SOURCE can be cast into type DEST. If the cast
12188 can't occur at all, return 0 otherwise 1. This function is used to
12189 produce accurate error messages on the reasons why an assignment
12190 failed. */
e04a16fb 12191
b67d701b
PB
12192static tree
12193try_reference_assignconv (lhs_type, rhs)
12194 tree lhs_type, rhs;
e04a16fb 12195{
b67d701b
PB
12196 tree new_rhs = NULL_TREE;
12197 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12198
b67d701b
PB
12199 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12200 {
12201 /* `null' may be assigned to any reference type */
12202 if (rhs == null_pointer_node)
12203 new_rhs = null_pointer_node;
12204 /* Try the reference assignment conversion */
12205 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12206 new_rhs = rhs;
12207 /* This is a magic assignment that we process differently */
12208 else if (rhs == soft_exceptioninfo_call_node)
12209 new_rhs = rhs;
12210 }
12211 return new_rhs;
12212}
12213
12214/* Check that RHS can be converted into LHS_TYPE by the assignment
12215 conversion (5.2), for the cases of RHS being a builtin type. Return
12216 NULL_TREE if the conversion fails or if because RHS isn't of a
12217 builtin type. Return a converted RHS if the conversion is possible. */
12218
12219static tree
12220try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12221 tree wfl_op1, lhs_type, rhs;
12222{
12223 tree new_rhs = NULL_TREE;
12224 tree rhs_type = TREE_TYPE (rhs);
12225
7e51098e
TT
12226 /* Handle boolean specially. */
12227 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12228 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12229 {
12230 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12231 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12232 new_rhs = rhs;
12233 }
12234
5e942c50 12235 /* Zero accepted everywhere */
7e51098e 12236 else if (TREE_CODE (rhs) == INTEGER_CST
5e942c50
APB
12237 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12238 && JPRIMITIVE_TYPE_P (rhs_type))
12239 new_rhs = convert (lhs_type, rhs);
12240
b67d701b
PB
12241 /* 5.1.1 Try Identity Conversion,
12242 5.1.2 Try Widening Primitive Conversion */
5e942c50 12243 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12244 new_rhs = convert (lhs_type, rhs);
12245
12246 /* Try a narrowing primitive conversion (5.1.3):
12247 - expression is a constant expression of type int AND
12248 - variable is byte, short or char AND
12249 - The value of the expression is representable in the type of the
12250 variable */
12251 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12252 && (lhs_type == byte_type_node || lhs_type == char_type_node
12253 || lhs_type == short_type_node))
12254 {
12255 if (int_fits_type_p (rhs, lhs_type))
12256 new_rhs = convert (lhs_type, rhs);
12257 else if (wfl_op1) /* Might be called with a NULL */
12258 parse_warning_context
7e51098e 12259 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
0a2138e2 12260 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12261 /* Reported a warning that will turn into an error further
12262 down, so we don't return */
12263 }
12264
12265 return new_rhs;
12266}
12267
12268/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12269 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12270 0 is the conversion test fails. This implements parts the method
12271 invocation convertion (5.3). */
12272
12273static int
12274valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12275 tree lhs_type, rhs_type;
12276{
acd663ee 12277 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12278 if (lhs_type == rhs_type)
12279 return 1;
12280
7e51098e
TT
12281 /* Reject non primitive types and boolean conversions. */
12282 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
b67d701b
PB
12283 return 0;
12284
acd663ee
APB
12285 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12286 than a char can't be converted into a char. Short can't too, but
12287 the < test below takes care of that */
b67d701b
PB
12288 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12289 return 0;
12290
5e942c50
APB
12291 /* Accept all promoted type here. Note, we can't use <= in the test
12292 below, because we still need to bounce out assignments of short
12293 to char and the likes */
12294 if (lhs_type == int_type_node
12295 && (rhs_type == promoted_byte_type_node
12296 || rhs_type == promoted_short_type_node
12297 || rhs_type == promoted_char_type_node
12298 || rhs_type == promoted_boolean_type_node))
12299 return 1;
12300
acd663ee
APB
12301 /* From here, an integral is widened if its precision is smaller
12302 than the precision of the LHS or if the LHS is a floating point
12303 type, or the RHS is a float and the RHS a double. */
12304 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12305 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12306 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12307 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12308 return 1;
12309
12310 return 0;
e04a16fb
AG
12311}
12312
12313/* Check that something of SOURCE type can be assigned or cast to
12314 something of DEST type at runtime. Return 1 if the operation is
12315 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12316 were SOURCE is cast into DEST, which borrows a lot of the
12317 assignment check. */
12318
12319static int
12320valid_ref_assignconv_cast_p (source, dest, cast)
12321 tree source;
12322 tree dest;
12323 int cast;
12324{
09ed0f70
APB
12325 /* SOURCE or DEST might be null if not from a declared entity. */
12326 if (!source || !dest)
12327 return 0;
5e942c50
APB
12328 if (JNULLP_TYPE_P (source))
12329 return 1;
e04a16fb
AG
12330 if (TREE_CODE (source) == POINTER_TYPE)
12331 source = TREE_TYPE (source);
12332 if (TREE_CODE (dest) == POINTER_TYPE)
12333 dest = TREE_TYPE (dest);
12334 /* Case where SOURCE is a class type */
12335 if (TYPE_CLASS_P (source))
12336 {
12337 if (TYPE_CLASS_P (dest))
c2952b01
APB
12338 return (source == dest
12339 || inherits_from_p (source, dest)
c2952b01 12340 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12341 if (TYPE_INTERFACE_P (dest))
12342 {
12343 /* If doing a cast and SOURCE is final, the operation is
12344 always correct a compile time (because even if SOURCE
12345 does not implement DEST, a subclass of SOURCE might). */
12346 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12347 return 1;
12348 /* Otherwise, SOURCE must implement DEST */
12349 return interface_of_p (dest, source);
12350 }
12351 /* DEST is an array, cast permited if SOURCE is of Object type */
12352 return (cast && source == object_type_node ? 1 : 0);
12353 }
12354 if (TYPE_INTERFACE_P (source))
12355 {
12356 if (TYPE_CLASS_P (dest))
12357 {
12358 /* If not casting, DEST must be the Object type */
12359 if (!cast)
12360 return dest == object_type_node;
12361 /* We're doing a cast. The cast is always valid is class
12362 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12363 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12364 return 1;
12365 else
12366 return interface_of_p (source, dest);
12367 }
12368 if (TYPE_INTERFACE_P (dest))
12369 {
12370 /* If doing a cast, then if SOURCE and DEST contain method
12371 with the same signature but different return type, then
12372 this is a (compile time) error */
12373 if (cast)
12374 {
12375 tree method_source, method_dest;
12376 tree source_type;
0a2138e2 12377 tree source_sig;
e04a16fb
AG
12378 tree source_name;
12379 for (method_source = TYPE_METHODS (source); method_source;
12380 method_source = TREE_CHAIN (method_source))
12381 {
12382 source_sig =
12383 build_java_argument_signature (TREE_TYPE (method_source));
12384 source_type = TREE_TYPE (TREE_TYPE (method_source));
12385 source_name = DECL_NAME (method_source);
12386 for (method_dest = TYPE_METHODS (dest);
12387 method_dest; method_dest = TREE_CHAIN (method_dest))
12388 if (source_sig ==
12389 build_java_argument_signature (TREE_TYPE (method_dest))
12390 && source_name == DECL_NAME (method_dest)
12391 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12392 return 0;
12393 }
12394 return 1;
12395 }
12396 else
12397 return source == dest || interface_of_p (dest, source);
12398 }
ee17a290
TT
12399 else
12400 {
12401 /* Array */
12402 return (cast
12403 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12404 || (DECL_NAME (TYPE_NAME (source))
12405 == java_io_serializable)));
12406 }
e04a16fb
AG
12407 }
12408 if (TYPE_ARRAY_P (source))
12409 {
12410 if (TYPE_CLASS_P (dest))
12411 return dest == object_type_node;
09ed0f70 12412 /* Can't cast an array to an interface unless the interface is
ee17a290 12413 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12414 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12415 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12416 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12417 else /* Arrays */
12418 {
12419 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12420 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12421
b9f7e36c
APB
12422 /* In case of severe errors, they turn out null */
12423 if (!dest_element_type || !source_element_type)
12424 return 0;
e04a16fb
AG
12425 if (source_element_type == dest_element_type)
12426 return 1;
12427 return valid_ref_assignconv_cast_p (source_element_type,
12428 dest_element_type, cast);
12429 }
12430 return 0;
12431 }
12432 return 0;
12433}
12434
b67d701b
PB
12435static int
12436valid_cast_to_p (source, dest)
12437 tree source;
12438 tree dest;
12439{
12440 if (TREE_CODE (source) == POINTER_TYPE)
12441 source = TREE_TYPE (source);
12442 if (TREE_CODE (dest) == POINTER_TYPE)
12443 dest = TREE_TYPE (dest);
12444
12445 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12446 return valid_ref_assignconv_cast_p (source, dest, 1);
12447
12448 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12449 return 1;
12450
7e51098e
TT
12451 else if (TREE_CODE (source) == BOOLEAN_TYPE
12452 && TREE_CODE (dest) == BOOLEAN_TYPE)
12453 return 1;
12454
b67d701b
PB
12455 return 0;
12456}
12457
15fdcfe9
PB
12458static tree
12459do_unary_numeric_promotion (arg)
12460 tree arg;
12461{
12462 tree type = TREE_TYPE (arg);
7e51098e
TT
12463 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12464 || TREE_CODE (type) == CHAR_TYPE)
15fdcfe9
PB
12465 arg = convert (int_type_node, arg);
12466 return arg;
12467}
12468
acd663ee
APB
12469/* Return a non zero value if SOURCE can be converted into DEST using
12470 the method invocation conversion rule (5.3). */
b67d701b
PB
12471static int
12472valid_method_invocation_conversion_p (dest, source)
12473 tree dest, source;
12474{
e3884b71 12475 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12476 && valid_builtin_assignconv_identity_widening_p (dest, source))
12477 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12478 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12479 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12480}
12481
e04a16fb
AG
12482/* Build an incomplete binop expression. */
12483
12484static tree
12485build_binop (op, op_location, op1, op2)
12486 enum tree_code op;
12487 int op_location;
12488 tree op1, op2;
12489{
5e942c50 12490 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12491 TREE_SIDE_EFFECTS (binop) = 1;
12492 /* Store the location of the operator, for better error report. The
12493 string of the operator will be rebuild based on the OP value. */
12494 EXPR_WFL_LINECOL (binop) = op_location;
12495 return binop;
12496}
12497
12498/* Build the string of the operator retained by NODE. If NODE is part
12499 of a compound expression, add an '=' at the end of the string. This
12500 function is called when an error needs to be reported on an
12501 operator. The string is returned as a pointer to a static character
12502 buffer. */
12503
12504static char *
12505operator_string (node)
12506 tree node;
12507{
12508#define BUILD_OPERATOR_STRING(S) \
12509 { \
12510 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12511 return buffer; \
12512 }
12513
12514 static char buffer [10];
12515 switch (TREE_CODE (node))
12516 {
12517 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12518 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12519 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12520 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12521 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12522 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12523 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12524 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12525 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12526 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12527 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12528 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12529 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12530 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12531 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12532 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12533 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12534 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12535 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12536 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12537 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12538 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12539 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12540 case PREINCREMENT_EXPR: /* Fall through */
12541 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12542 case PREDECREMENT_EXPR: /* Fall through */
12543 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12544 default:
12545 fatal ("unregistered operator %s - operator_string",
12546 tree_code_name [TREE_CODE (node)]);
12547 }
12548 return NULL;
12549#undef BUILD_OPERATOR_STRING
12550}
12551
5cbdba64
APB
12552/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12553
12554static int
12555java_decl_equiv (var_acc1, var_acc2)
12556 tree var_acc1, var_acc2;
12557{
12558 if (JDECL_P (var_acc1))
12559 return (var_acc1 == var_acc2);
12560
12561 return (TREE_CODE (var_acc1) == COMPONENT_REF
12562 && TREE_CODE (var_acc2) == COMPONENT_REF
12563 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12564 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12565 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12566}
12567
12568/* Return a non zero value if CODE is one of the operators that can be
12569 used in conjunction with the `=' operator in a compound assignment. */
12570
12571static int
12572binop_compound_p (code)
12573 enum tree_code code;
12574{
12575 int i;
12576 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12577 if (binop_lookup [i] == code)
12578 break;
12579
12580 return i < BINOP_COMPOUND_CANDIDATES;
12581}
12582
12583/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12584
12585static tree
12586java_refold (t)
12587 tree t;
12588{
12589 tree c, b, ns, decl;
12590
12591 if (TREE_CODE (t) != MODIFY_EXPR)
12592 return t;
12593
12594 c = TREE_OPERAND (t, 1);
12595 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12596 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12597 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12598 return t;
12599
12600 /* Now the left branch of the binary operator. */
12601 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12602 if (! (b && TREE_CODE (b) == NOP_EXPR
12603 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12604 return t;
12605
12606 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12607 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12608 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12609 return t;
12610
12611 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12612 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12613 /* It's got to be the an equivalent decl */
12614 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12615 {
12616 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12617 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12618 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12619 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12620 /* Change the right part of the BINOP_EXPR */
12621 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12622 }
12623
12624 return t;
12625}
12626
e04a16fb
AG
12627/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12628 errors but we modify NODE so that it contains the type computed
12629 according to the expression, when it's fixed. Otherwise, we write
12630 error_mark_node as the type. It allows us to further the analysis
12631 of remaining nodes and detects more errors in certain cases. */
12632
12633static tree
12634patch_binop (node, wfl_op1, wfl_op2)
12635 tree node;
12636 tree wfl_op1;
12637 tree wfl_op2;
12638{
12639 tree op1 = TREE_OPERAND (node, 0);
12640 tree op2 = TREE_OPERAND (node, 1);
12641 tree op1_type = TREE_TYPE (op1);
12642 tree op2_type = TREE_TYPE (op2);
48a840d9 12643 tree prom_type = NULL_TREE, cn;
e04a16fb 12644 int code = TREE_CODE (node);
b67d701b 12645
e04a16fb
AG
12646 /* If 1, tell the routine that we have to return error_mark_node
12647 after checking for the initialization of the RHS */
12648 int error_found = 0;
12649
e04a16fb
AG
12650 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12651
e04a16fb
AG
12652 switch (code)
12653 {
12654 /* 15.16 Multiplicative operators */
12655 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12656 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12657 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb 12658 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
7e51098e 12659 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 12660 {
7e51098e 12661 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 12662 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 12663 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
12664 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12665 TREE_TYPE (node) = error_mark_node;
12666 error_found = 1;
12667 break;
12668 }
12669 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12670 /* Change the division operator if necessary */
12671 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12672 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12673
aa4759c1
AH
12674 if (TREE_CODE (prom_type) == INTEGER_TYPE
12675 && flag_use_divide_subroutine
12676 && ! flag_emit_class_files
12677 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12678 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12679
0b4d333e
APB
12680 /* This one is more complicated. FLOATs are processed by a
12681 function call to soft_fmod. Duplicate the value of the
12682 COMPOUND_ASSIGN_P flag. */
e04a16fb 12683 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12684 {
12685 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12686 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12687 TREE_SIDE_EFFECTS (mod)
12688 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12689 return mod;
12690 }
e04a16fb
AG
12691 break;
12692
12693 /* 15.17 Additive Operators */
12694 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12695
12696 /* Operation is valid if either one argument is a string
12697 constant, a String object or a StringBuffer crafted for the
12698 purpose of the a previous usage of the String concatenation
12699 operator */
12700
12701 if (TREE_CODE (op1) == STRING_CST
12702 || TREE_CODE (op2) == STRING_CST
12703 || JSTRING_TYPE_P (op1_type)
12704 || JSTRING_TYPE_P (op2_type)
12705 || IS_CRAFTED_STRING_BUFFER_P (op1)
12706 || IS_CRAFTED_STRING_BUFFER_P (op2))
12707 return build_string_concatenation (op1, op2);
12708
e04a16fb
AG
12709 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12710 Numeric Types */
7e51098e 12711 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
e04a16fb 12712 {
7e51098e 12713 if (!JNUMERIC_TYPE_P (op1_type))
e04a16fb 12714 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
7e51098e 12715 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
e04a16fb
AG
12716 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12717 TREE_TYPE (node) = error_mark_node;
12718 error_found = 1;
12719 break;
12720 }
12721 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12722 break;
12723
12724 /* 15.18 Shift Operators */
12725 case LSHIFT_EXPR:
12726 case RSHIFT_EXPR:
12727 case URSHIFT_EXPR:
12728 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12729 {
12730 if (!JINTEGRAL_TYPE_P (op1_type))
12731 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12732 else
1ebadc60 12733 {
7e51098e 12734 if (JNUMERIC_TYPE_P (op2_type))
1ebadc60 12735 parse_error_context (wfl_operator,
781b0558 12736 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12737 operator_string (node),
12738 lang_printable_name (op2_type, 0));
12739 else
781b0558
KG
12740 parse_error_context (wfl_operator,
12741 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12742 operator_string (node),
12743 lang_printable_name (op2_type, 0));
12744 }
e04a16fb
AG
12745 TREE_TYPE (node) = error_mark_node;
12746 error_found = 1;
12747 break;
12748 }
12749
12750 /* Unary numeric promotion (5.6.1) is performed on each operand
12751 separatly */
15fdcfe9
PB
12752 op1 = do_unary_numeric_promotion (op1);
12753 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12754
12755 /* The type of the shift expression is the type of the promoted
12756 type of the left-hand operand */
12757 prom_type = TREE_TYPE (op1);
12758
c2952b01
APB
12759 /* Shift int only up to 0x1f and long up to 0x3f */
12760 if (prom_type == int_type_node)
12761 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12762 build_int_2 (0x1f, 0)));
12763 else
12764 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12765 build_int_2 (0x3f, 0)));
e04a16fb
AG
12766
12767 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12768 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12769 {
0b4d333e 12770 tree to_return;
73333a87
AH
12771 tree utype = unsigned_type (prom_type);
12772 op1 = convert (utype, op1);
e04a16fb 12773 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12774 TREE_OPERAND (node, 0) = op1;
12775 TREE_OPERAND (node, 1) = op2;
12776 TREE_TYPE (node) = utype;
0b4d333e
APB
12777 to_return = convert (prom_type, node);
12778 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12779 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12780 TREE_SIDE_EFFECTS (to_return)
12781 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12782 return to_return;
e04a16fb
AG
12783 }
12784 break;
5e942c50
APB
12785
12786 /* 15.19.1 Type Comparison Operator instaceof */
12787 case INSTANCEOF_EXPR:
12788
12789 TREE_TYPE (node) = boolean_type_node;
12790
12791 if (!(op2_type = resolve_type_during_patch (op2)))
12792 return error_mark_node;
12793
12794 /* The first operand must be a reference type or the null type */
12795 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12796 error_found = 1; /* Error reported further below */
12797
12798 /* The second operand must be a reference type */
12799 if (!JREFERENCE_TYPE_P (op2_type))
12800 {
12801 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12802 parse_error_context
12803 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12804 lang_printable_name (op2_type, 0));
12805 error_found = 1;
12806 }
12807
12808 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12809 {
12810 /* If the first operand is null, the result is always false */
12811 if (op1 == null_pointer_node)
12812 return boolean_false_node;
15fdcfe9
PB
12813 else if (flag_emit_class_files)
12814 {
12815 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12816 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12817 return node;
12818 }
5e942c50
APB
12819 /* Otherwise we have to invoke instance of to figure it out */
12820 else
67db0ce7 12821 return build_instanceof (op1, op2_type);
5e942c50
APB
12822 }
12823 /* There is no way the expression operand can be an instance of
12824 the type operand. This is a compile time error. */
12825 else
12826 {
c2e3db92 12827 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12828 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12829 parse_error_context
12830 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12831 t1, lang_printable_name (op2_type, 0));
12832 free (t1);
12833 error_found = 1;
12834 }
e04a16fb 12835
5e942c50 12836 break;
e04a16fb
AG
12837
12838 /* 15.21 Bitwise and Logical Operators */
12839 case BIT_AND_EXPR:
12840 case BIT_XOR_EXPR:
12841 case BIT_IOR_EXPR:
12842 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12843 /* Binary numeric promotion is performed on both operand and the
12844 expression retain that type */
12845 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12846
12847 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12848 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12849 /* The type of the bitwise operator expression is BOOLEAN */
12850 prom_type = boolean_type_node;
12851 else
12852 {
12853 if (!JINTEGRAL_TYPE_P (op1_type))
12854 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12855 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12856 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12857 TREE_TYPE (node) = error_mark_node;
12858 error_found = 1;
12859 /* Insert a break here if adding thing before the switch's
12860 break for this case */
12861 }
12862 break;
12863
12864 /* 15.22 Conditional-And Operator */
12865 case TRUTH_ANDIF_EXPR:
12866 /* 15.23 Conditional-Or Operator */
12867 case TRUTH_ORIF_EXPR:
12868 /* Operands must be of BOOLEAN type */
12869 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12870 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12871 {
12872 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12873 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12874 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12875 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12876 TREE_TYPE (node) = boolean_type_node;
12877 error_found = 1;
12878 break;
12879 }
12880 /* The type of the conditional operators is BOOLEAN */
12881 prom_type = boolean_type_node;
12882 break;
12883
12884 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12885 case LT_EXPR:
12886 case GT_EXPR:
12887 case LE_EXPR:
12888 case GE_EXPR:
12889 /* The type of each of the operands must be a primitive numeric
12890 type */
12891 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12892 {
12893 if (!JNUMERIC_TYPE_P (op1_type))
12894 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12895 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12896 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12897 TREE_TYPE (node) = boolean_type_node;
12898 error_found = 1;
12899 break;
12900 }
12901 /* Binary numeric promotion is performed on the operands */
12902 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12903 /* The type of the relation expression is always BOOLEAN */
12904 prom_type = boolean_type_node;
12905 break;
12906
12907 /* 15.20 Equality Operator */
12908 case EQ_EXPR:
12909 case NE_EXPR:
48a840d9
APB
12910 /* It's time for us to patch the strings. */
12911 if ((cn = patch_string (op1)))
12912 {
12913 op1 = cn;
12914 op1_type = TREE_TYPE (op1);
12915 }
12916 if ((cn = patch_string (op2)))
12917 {
12918 op2 = cn;
12919 op2_type = TREE_TYPE (op2);
12920 }
12921
e04a16fb
AG
12922 /* 15.20.1 Numerical Equality Operators == and != */
12923 /* Binary numeric promotion is performed on the operands */
5e942c50 12924 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12925 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12926
12927 /* 15.20.2 Boolean Equality Operators == and != */
12928 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12929 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12930 ; /* Nothing to do here */
12931
12932 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12933 /* Types have to be either references or the null type. If
12934 they're references, it must be possible to convert either
12935 type to the other by casting conversion. */
b9f7e36c
APB
12936 else if (op1 == null_pointer_node || op2 == null_pointer_node
12937 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12938 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12939 || valid_ref_assignconv_cast_p (op2_type,
12940 op1_type, 1))))
e04a16fb
AG
12941 ; /* Nothing to do here */
12942
12943 /* Else we have an error figure what can't be converted into
12944 what and report the error */
12945 else
12946 {
12947 char *t1;
c2e3db92 12948 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12949 parse_error_context
781b0558
KG
12950 (wfl_operator,
12951 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12952 operator_string (node), t1,
0a2138e2 12953 lang_printable_name (op2_type, 0));
e04a16fb
AG
12954 free (t1);
12955 TREE_TYPE (node) = boolean_type_node;
12956 error_found = 1;
12957 break;
12958 }
12959 prom_type = boolean_type_node;
12960 break;
12961 }
12962
e04a16fb
AG
12963 if (error_found)
12964 return error_mark_node;
12965
12966 TREE_OPERAND (node, 0) = op1;
12967 TREE_OPERAND (node, 1) = op2;
12968 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12969 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12970
ce6e9147
APB
12971 if (flag_emit_xref)
12972 return node;
12973
d1472141
PB
12974 /* fold does not respect side-effect order as required for Java but not C.
12975 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12976 * bytecode.
12977 */
12978 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12979 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12980 node = fold (node);
12981 return node;
e04a16fb
AG
12982}
12983
b67d701b
PB
12984/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12985 zero value, the value of CSTE comes after the valude of STRING */
12986
12987static tree
12988do_merge_string_cste (cste, string, string_len, after)
12989 tree cste;
49f48c71 12990 const char *string;
b67d701b
PB
12991 int string_len, after;
12992{
49f48c71 12993 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
12994 int old_len = TREE_STRING_LENGTH (cste);
12995 int len = old_len + string_len;
12996 char *new;
12997
12998 cste = make_node (STRING_CST);
b67d701b 12999 TREE_STRING_LENGTH (cste) = len;
1f8f4a0b 13000 new = TREE_STRING_POINTER (cste) = ggc_alloc (len+1);
354e99ce 13001
b67d701b
PB
13002 if (after)
13003 {
354e99ce
APB
13004 memcpy (new, string, string_len);
13005 memcpy (&new [string_len], old, old_len);
b67d701b
PB
13006 }
13007 else
13008 {
354e99ce
APB
13009 memcpy (new, old, old_len);
13010 memcpy (&new [old_len], string, string_len);
b67d701b 13011 }
354e99ce 13012 new [len] = '\0';
b67d701b
PB
13013 return cste;
13014}
13015
13016/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13017 new STRING_CST on success, NULL_TREE on failure */
13018
13019static tree
13020merge_string_cste (op1, op2, after)
13021 tree op1, op2;
13022 int after;
13023{
13024 /* Handle two string constants right away */
13025 if (TREE_CODE (op2) == STRING_CST)
13026 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13027 TREE_STRING_LENGTH (op2), after);
13028
13029 /* Reasonable integer constant can be treated right away */
13030 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13031 {
49f48c71
KG
13032 static const char *boolean_true = "true";
13033 static const char *boolean_false = "false";
13034 static const char *null_pointer = "null";
b67d701b 13035 char ch[3];
49f48c71 13036 const char *string;
b67d701b
PB
13037
13038 if (op2 == boolean_true_node)
13039 string = boolean_true;
13040 else if (op2 == boolean_false_node)
13041 string = boolean_false;
13042 else if (op2 == null_pointer_node)
13043 string = null_pointer;
13044 else if (TREE_TYPE (op2) == char_type_node)
13045 {
13046 ch[0] = (char )TREE_INT_CST_LOW (op2);
13047 ch[1] = '\0';
13048 string = ch;
13049 }
13050 else
13051 string = print_int_node (op2);
13052
13053 return do_merge_string_cste (op1, string, strlen (string), after);
13054 }
13055 return NULL_TREE;
13056}
13057
13058/* Tries to statically concatenate OP1 and OP2 if possible. Either one
13059 has to be a STRING_CST and the other part must be a STRING_CST or a
13060 INTEGRAL constant. Return a new STRING_CST if the operation
13061 succeed, NULL_TREE otherwise.
13062
13063 If the case we want to optimize for space, we might want to return
13064 NULL_TREE for each invocation of this routine. FIXME */
13065
13066static tree
13067string_constant_concatenation (op1, op2)
13068 tree op1, op2;
13069{
13070 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13071 {
0a2138e2 13072 tree string, rest;
b67d701b
PB
13073 int invert;
13074
13075 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13076 rest = (string == op1 ? op2 : op1);
13077 invert = (string == op1 ? 0 : 1 );
13078
13079 /* Walk REST, only if it looks reasonable */
13080 if (TREE_CODE (rest) != STRING_CST
13081 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13082 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13083 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13084 {
13085 rest = java_complete_tree (rest);
13086 if (rest == error_mark_node)
13087 return error_mark_node;
13088 rest = fold (rest);
13089 }
13090 return merge_string_cste (string, rest, invert);
13091 }
13092 return NULL_TREE;
13093}
13094
13095/* Implement the `+' operator. Does static optimization if possible,
13096 otherwise create (if necessary) and append elements to a
13097 StringBuffer. The StringBuffer will be carried around until it is
13098 used for a function call or an assignment. Then toString() will be
13099 called on it to turn it into a String object. */
13100
13101static tree
13102build_string_concatenation (op1, op2)
13103 tree op1, op2;
13104{
13105 tree result;
dc0b3eff 13106 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13107
13108 if (flag_emit_xref)
13109 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13110
13111 /* Try to do some static optimization */
13112 if ((result = string_constant_concatenation (op1, op2)))
13113 return result;
13114
c0d87ff6
PB
13115 /* Discard empty strings on either side of the expression */
13116 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13117 {
13118 op1 = op2;
13119 op2 = NULL_TREE;
13120 }
c0d87ff6 13121 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13122 op2 = NULL_TREE;
b67d701b 13123
acd663ee 13124 /* If operands are string constant, turn then into object references */
b67d701b
PB
13125 if (TREE_CODE (op1) == STRING_CST)
13126 op1 = patch_string_cst (op1);
acd663ee 13127 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13128 op2 = patch_string_cst (op2);
13129
acd663ee
APB
13130 /* If either one of the constant is null and the other non null
13131 operand is a String object, return it. */
13132 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13133 return op1;
13134
b67d701b
PB
13135 /* If OP1 isn't already a StringBuffer, create and
13136 initialize a new one */
13137 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13138 {
13139 /* Two solutions here:
c52b5771
AG
13140 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13141 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13142 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13143 op1 = BUILD_STRING_BUFFER (op1);
13144 else
13145 {
13146 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13147 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13148 }
13149 }
13150
acd663ee
APB
13151 if (op2)
13152 {
13153 /* OP1 is no longer the last node holding a crafted StringBuffer */
13154 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13155 /* Create a node for `{new...,xxx}.append (op2)' */
13156 if (op2)
13157 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13158 }
13159
b67d701b
PB
13160 /* Mark the last node holding a crafted StringBuffer */
13161 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13162
13163 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13164 return op1;
13165}
13166
13167/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13168 StringBuffer. If no string were found to be patched, return
13169 NULL. */
13170
13171static tree
13172patch_string (node)
13173 tree node;
13174{
1179ebc2
APB
13175 if (node == error_mark_node)
13176 return error_mark_node;
b67d701b
PB
13177 if (TREE_CODE (node) == STRING_CST)
13178 return patch_string_cst (node);
13179 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13180 {
c877974e 13181 int saved = ctxp->explicit_constructor_p;
b67d701b 13182 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13183 tree ret;
13184 /* Temporary disable forbid the use of `this'. */
13185 ctxp->explicit_constructor_p = 0;
13186 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13187 /* String concatenation arguments must be evaluated in order too. */
13188 ret = force_evaluation_order (ret);
c877974e
APB
13189 /* Restore it at its previous value */
13190 ctxp->explicit_constructor_p = saved;
13191 return ret;
b67d701b
PB
13192 }
13193 return NULL_TREE;
13194}
13195
13196/* Build the internal representation of a string constant. */
13197
13198static tree
13199patch_string_cst (node)
13200 tree node;
13201{
13202 int location;
15fdcfe9
PB
13203 if (! flag_emit_class_files)
13204 {
15fdcfe9
PB
13205 node = get_identifier (TREE_STRING_POINTER (node));
13206 location = alloc_name_constant (CONSTANT_String, node);
13207 node = build_ref_from_constant_pool (location);
13208 }
cd9643f7 13209 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13210 TREE_CONSTANT (node) = 1;
13211 return node;
13212}
13213
13214/* Build an incomplete unary operator expression. */
e04a16fb
AG
13215
13216static tree
13217build_unaryop (op_token, op_location, op1)
13218 int op_token, op_location;
13219 tree op1;
13220{
13221 enum tree_code op;
13222 tree unaryop;
13223 switch (op_token)
13224 {
b67d701b 13225 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13226 case MINUS_TK: op = NEGATE_EXPR; break;
13227 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13228 case NOT_TK: op = BIT_NOT_EXPR; break;
13229 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13230 op_token);
13231 }
13232
13233 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13234 TREE_SIDE_EFFECTS (unaryop) = 1;
13235 /* Store the location of the operator, for better error report. The
13236 string of the operator will be rebuild based on the OP value. */
13237 EXPR_WFL_LINECOL (unaryop) = op_location;
13238 return unaryop;
13239}
13240
13241/* Special case for the ++/-- operators, since they require an extra
13242 argument to build, which is set to NULL and patched
13243 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13244
13245static tree
13246build_incdec (op_token, op_location, op1, is_post_p)
13247 int op_token, op_location;
13248 tree op1;
13249 int is_post_p;
13250{
13251 static enum tree_code lookup [2][2] =
13252 {
13253 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13254 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13255 };
13256 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13257 NULL_TREE, op1, NULL_TREE);
13258 TREE_SIDE_EFFECTS (node) = 1;
13259 /* Store the location of the operator, for better error report. The
13260 string of the operator will be rebuild based on the OP value. */
13261 EXPR_WFL_LINECOL (node) = op_location;
13262 return node;
13263}
13264
13265/* Build an incomplete cast operator, based on the use of the
13266 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13267 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13268 though its type is already set. */
13269
13270static tree
13271build_cast (location, type, exp)
13272 int location;
13273 tree type, exp;
13274{
13275 tree node = build1 (CONVERT_EXPR, type, exp);
13276 EXPR_WFL_LINECOL (node) = location;
13277 return node;
13278}
13279
c2952b01
APB
13280/* Build an incomplete class reference operator. */
13281static tree
13282build_incomplete_class_ref (location, class_name)
13283 int location;
13284 tree class_name;
13285{
13286 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13287 EXPR_WFL_LINECOL (node) = location;
13288 return node;
13289}
13290
13291/* Complete an incomplete class reference operator. */
13292static tree
13293patch_incomplete_class_ref (node)
13294 tree node;
13295{
13296 tree type = TREE_OPERAND (node, 0);
13297 tree ref_type;
13298
13299 if (!(ref_type = resolve_type_during_patch (type)))
13300 return error_mark_node;
13301
165f37bc 13302 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13303 {
13304 /* A class referenced by `foo.class' is initialized. */
13305 return build_class_init (ref_type, build_class_ref (ref_type));
13306 }
165f37bc
APB
13307
13308 /* If we're emitting class files and we have to deal with non
13309 primitive types, we invoke (and consider generating) the
13310 synthetic static method `class$'. */
13311 if (!TYPE_DOT_CLASS (current_class))
13312 build_dot_class_method (current_class);
f0f3a777 13313 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13314 return java_complete_tree (ref_type);
c2952b01
APB
13315}
13316
e04a16fb
AG
13317/* 15.14 Unary operators. We return error_mark_node in case of error,
13318 but preserve the type of NODE if the type is fixed. */
13319
13320static tree
13321patch_unaryop (node, wfl_op)
13322 tree node;
13323 tree wfl_op;
13324{
13325 tree op = TREE_OPERAND (node, 0);
13326 tree op_type = TREE_TYPE (op);
ab3a6dd6 13327 tree prom_type = NULL_TREE, value, decl;
c2952b01 13328 int outer_field_flag = 0;
e04a16fb
AG
13329 int code = TREE_CODE (node);
13330 int error_found = 0;
13331
13332 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13333
13334 switch (code)
13335 {
13336 /* 15.13.2 Postfix Increment Operator ++ */
13337 case POSTINCREMENT_EXPR:
13338 /* 15.13.3 Postfix Increment Operator -- */
13339 case POSTDECREMENT_EXPR:
13340 /* 15.14.1 Prefix Increment Operator ++ */
13341 case PREINCREMENT_EXPR:
13342 /* 15.14.2 Prefix Decrement Operator -- */
13343 case PREDECREMENT_EXPR:
5cbdba64 13344 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13345 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13346 /* We might be trying to change an outer field accessed using
13347 access method. */
13348 if (outer_field_flag)
13349 {
13350 /* Retrieve the decl of the field we're trying to access. We
13351 do that by first retrieving the function we would call to
13352 access the field. It has been already verified that this
13353 field isn't final */
13354 if (flag_emit_class_files)
13355 decl = TREE_OPERAND (op, 0);
13356 else
13357 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13358 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13359 }
b3edebcf 13360 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13361 else if (!JDECL_P (decl)
b3edebcf
APB
13362 && TREE_CODE (decl) != COMPONENT_REF
13363 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13364 && TREE_CODE (decl) != INDIRECT_REF
13365 && !(TREE_CODE (decl) == COMPOUND_EXPR
13366 && TREE_OPERAND (decl, 1)
13367 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13368 {
5e942c50
APB
13369 tree lvalue;
13370 /* Before screaming, check that we're not in fact trying to
13371 increment a optimized static final access, in which case
13372 we issue an different error message. */
13373 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13374 && resolve_expression_name (wfl_op, &lvalue)
13375 && check_final_assignment (lvalue, wfl_op)))
13376 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13377 operator_string (node));
e04a16fb
AG
13378 TREE_TYPE (node) = error_mark_node;
13379 error_found = 1;
13380 }
c2952b01
APB
13381
13382 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13383 error_found = 1;
13384
e04a16fb
AG
13385 /* From now on, we know that op if a variable and that it has a
13386 valid wfl. We use wfl_op to locate errors related to the
13387 ++/-- operand. */
13388 else if (!JNUMERIC_TYPE_P (op_type))
13389 {
13390 parse_error_context
13391 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13392 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13393 TREE_TYPE (node) = error_mark_node;
13394 error_found = 1;
13395 }
13396 else
13397 {
4a5f66c3 13398 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13399 both operands, if really necessary */
13400 if (JINTEGRAL_TYPE_P (op_type))
13401 {
13402 value = build_int_2 (1, 0);
13403 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13404 }
13405 else
13406 {
13407 value = build_int_2 (1, 0);
13408 TREE_TYPE (node) =
13409 binary_numeric_promotion (op_type,
13410 TREE_TYPE (value), &op, &value);
13411 }
c2952b01
APB
13412
13413 /* We remember we might be accessing an outer field */
13414 if (outer_field_flag)
13415 {
13416 /* We re-generate an access to the field */
13417 value = build (PLUS_EXPR, TREE_TYPE (op),
13418 build_outer_field_access (wfl_op, decl), value);
13419
13420 /* And we patch the original access$() into a write
13421 with plus_op as a rhs */
13422 return outer_field_access_fix (node, op, value);
13423 }
13424
5cbdba64 13425 /* And write back into the node. */
4a5f66c3 13426 TREE_OPERAND (node, 0) = op;
e04a16fb 13427 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13428 /* Convert the overall back into its original type, if
13429 necessary, and return */
13430 if (JINTEGRAL_TYPE_P (op_type))
13431 return fold (node);
13432 else
13433 return fold (convert (op_type, node));
e04a16fb
AG
13434 }
13435 break;
13436
13437 /* 15.14.3 Unary Plus Operator + */
b67d701b 13438 case UNARY_PLUS_EXPR:
e04a16fb
AG
13439 /* 15.14.4 Unary Minus Operator - */
13440 case NEGATE_EXPR:
13441 if (!JNUMERIC_TYPE_P (op_type))
13442 {
13443 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13444 TREE_TYPE (node) = error_mark_node;
13445 error_found = 1;
13446 }
13447 /* Unary numeric promotion is performed on operand */
13448 else
13449 {
15fdcfe9
PB
13450 op = do_unary_numeric_promotion (op);
13451 prom_type = TREE_TYPE (op);
b67d701b 13452 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13453 return fold (op);
e04a16fb
AG
13454 }
13455 break;
13456
13457 /* 15.14.5 Bitwise Complement Operator ~ */
13458 case BIT_NOT_EXPR:
13459 if (!JINTEGRAL_TYPE_P (op_type))
13460 {
13461 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13462 TREE_TYPE (node) = error_mark_node;
13463 error_found = 1;
13464 }
13465 else
13466 {
15fdcfe9
PB
13467 op = do_unary_numeric_promotion (op);
13468 prom_type = TREE_TYPE (op);
e04a16fb
AG
13469 }
13470 break;
13471
13472 /* 15.14.6 Logical Complement Operator ! */
13473 case TRUTH_NOT_EXPR:
13474 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13475 {
13476 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13477 /* But the type is known. We will report an error if further
13478 attempt of a assignment is made with this rhs */
e04a16fb
AG
13479 TREE_TYPE (node) = boolean_type_node;
13480 error_found = 1;
13481 }
13482 else
13483 prom_type = boolean_type_node;
13484 break;
13485
13486 /* 15.15 Cast Expression */
13487 case CONVERT_EXPR:
0a2138e2 13488 value = patch_cast (node, wfl_operator);
e04a16fb 13489 if (value == error_mark_node)
c877974e
APB
13490 {
13491 /* If this cast is part of an assignment, we tell the code
13492 that deals with it not to complain about a mismatch,
13493 because things have been cast, anyways */
13494 TREE_TYPE (node) = error_mark_node;
13495 error_found = 1;
13496 }
13497 else
dc0b3eff
PB
13498 {
13499 value = fold (value);
13500 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13501 return value;
13502 }
e04a16fb
AG
13503 break;
13504 }
13505
e04a16fb
AG
13506 if (error_found)
13507 return error_mark_node;
4a5f66c3
APB
13508
13509 /* There are cases where node has been replaced by something else
13510 and we don't end up returning here: UNARY_PLUS_EXPR,
13511 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13512 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13513 TREE_TYPE (node) = prom_type;
dc0b3eff 13514 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13515 return fold (node);
13516}
13517
13518/* Generic type resolution that sometimes takes place during node
13519 patching. Returned the resolved type or generate an error
13520 message. Return the resolved type or NULL_TREE. */
13521
13522static tree
13523resolve_type_during_patch (type)
13524 tree type;
13525{
13526 if (unresolved_type_p (type, NULL))
13527 {
4142b247 13528 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13529 if (!type_decl)
13530 {
13531 parse_error_context (type,
13532 "Class `%s' not found in type declaration",
13533 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13534 return NULL_TREE;
13535 }
13536 else
5e942c50
APB
13537 {
13538 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13539 return TREE_TYPE (type_decl);
13540 }
e04a16fb
AG
13541 }
13542 return type;
13543}
13544/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13545 found. Otherwise NODE or something meant to replace it is returned. */
13546
13547static tree
19e223db 13548patch_cast (node, wfl_op)
e04a16fb 13549 tree node;
19e223db 13550 tree wfl_op;
e04a16fb
AG
13551{
13552 tree op = TREE_OPERAND (node, 0);
13553 tree op_type = TREE_TYPE (op);
13554 tree cast_type = TREE_TYPE (node);
13555 char *t1;
13556
13557 /* First resolve OP_TYPE if unresolved */
13558 if (!(cast_type = resolve_type_during_patch (cast_type)))
13559 return error_mark_node;
13560
13561 /* Check on cast that are proven correct at compile time */
13562 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13563 {
e04a16fb
AG
13564 /* Same type */
13565 if (cast_type == op_type)
13566 return node;
13567
0b4d333e
APB
13568 /* float and double type are converted to the original type main
13569 variant and then to the target type. */
13570 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13571 op = convert (integer_type_node, op);
13572
e04a16fb
AG
13573 /* Try widening/narowwing convertion. Potentially, things need
13574 to be worked out in gcc so we implement the extreme cases
13575 correctly. fold_convert() needs to be fixed. */
13576 return convert (cast_type, op);
13577 }
13578
0b4d333e
APB
13579 /* It's also valid to cast a boolean into a boolean */
13580 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13581 return node;
13582
5e942c50
APB
13583 /* null can be casted to references */
13584 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13585 return build_null_of_type (cast_type);
13586
e04a16fb
AG
13587 /* The remaining legal casts involve conversion between reference
13588 types. Check for their compile time correctness. */
13589 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13590 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13591 {
13592 TREE_TYPE (node) = promote_type (cast_type);
13593 /* Now, the case can be determined correct at compile time if
13594 OP_TYPE can be converted into CAST_TYPE by assignment
13595 conversion (5.2) */
13596
13597 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13598 {
13599 TREE_SET_CODE (node, NOP_EXPR);
13600 return node;
13601 }
13602
13603 if (flag_emit_class_files)
13604 {
13605 TREE_SET_CODE (node, CONVERT_EXPR);
13606 return node;
13607 }
e04a16fb
AG
13608
13609 /* The cast requires a run-time check */
13610 return build (CALL_EXPR, promote_type (cast_type),
13611 build_address_of (soft_checkcast_node),
13612 tree_cons (NULL_TREE, build_class_ref (cast_type),
13613 build_tree_list (NULL_TREE, op)),
13614 NULL_TREE);
13615 }
13616
13617 /* Any other casts are proven incorrect at compile time */
c2e3db92 13618 t1 = xstrdup (lang_printable_name (op_type, 0));
19e223db 13619 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
0a2138e2 13620 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13621 free (t1);
13622 return error_mark_node;
13623}
13624
5e942c50
APB
13625/* Build a null constant and give it the type TYPE. */
13626
13627static tree
13628build_null_of_type (type)
13629 tree type;
13630{
13631 tree node = build_int_2 (0, 0);
13632 TREE_TYPE (node) = promote_type (type);
13633 return node;
13634}
13635
e04a16fb
AG
13636/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13637 a list of indices. */
13638static tree
13639build_array_ref (location, array, index)
13640 int location;
13641 tree array, index;
13642{
13643 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13644 EXPR_WFL_LINECOL (node) = location;
13645 return node;
13646}
13647
13648/* 15.12 Array Access Expression */
13649
13650static tree
c877974e
APB
13651patch_array_ref (node)
13652 tree node;
e04a16fb
AG
13653{
13654 tree array = TREE_OPERAND (node, 0);
13655 tree array_type = TREE_TYPE (array);
13656 tree index = TREE_OPERAND (node, 1);
13657 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13658 int error_found = 0;
13659
13660 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13661
e04a16fb
AG
13662 if (TREE_CODE (array_type) == POINTER_TYPE)
13663 array_type = TREE_TYPE (array_type);
13664
13665 /* The array reference must be an array */
13666 if (!TYPE_ARRAY_P (array_type))
13667 {
13668 parse_error_context
781b0558
KG
13669 (wfl_operator,
13670 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13671 lang_printable_name (array_type, 0));
e04a16fb
AG
13672 TREE_TYPE (node) = error_mark_node;
13673 error_found = 1;
13674 }
13675
c2952b01 13676 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13677 type must be int */
15fdcfe9
PB
13678 index = do_unary_numeric_promotion (index);
13679 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13680 {
1ebadc60 13681 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13682 parse_error_context (wfl_operator,
13683 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13684 lang_printable_name (index_type, 0));
13685 else
781b0558
KG
13686 parse_error_context (wfl_operator,
13687 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13688 lang_printable_name (index_type, 0));
e04a16fb
AG
13689 TREE_TYPE (node) = error_mark_node;
13690 error_found = 1;
13691 }
13692
e04a16fb
AG
13693 if (error_found)
13694 return error_mark_node;
e04a16fb 13695
5e942c50 13696 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13697
7f1d4866 13698 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13699 {
15fdcfe9
PB
13700 TREE_OPERAND (node, 0) = array;
13701 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13702 }
13703 else
939d7216
PB
13704 {
13705 /* The save_expr is for correct evaluation order. It would be cleaner
13706 to use force_evaluation_order (see comment there), but that is
13707 difficult when we also have to deal with bounds checking. */
13708 if (TREE_SIDE_EFFECTS (index))
13709 array = save_expr (array);
13710 node = build_java_arrayaccess (array, array_type, index);
13711 if (TREE_SIDE_EFFECTS (index))
13712 node = build (COMPOUND_EXPR, array_type, array, node);
13713 }
e04a16fb
AG
13714 TREE_TYPE (node) = array_type;
13715 return node;
13716}
13717
13718/* 15.9 Array Creation Expressions */
13719
13720static tree
13721build_newarray_node (type, dims, extra_dims)
13722 tree type;
13723 tree dims;
13724 int extra_dims;
13725{
13726 tree node =
b67d701b 13727 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13728 build_int_2 (extra_dims, 0));
e04a16fb
AG
13729 return node;
13730}
13731
13732static tree
13733patch_newarray (node)
13734 tree node;
13735{
13736 tree type = TREE_OPERAND (node, 0);
13737 tree dims = TREE_OPERAND (node, 1);
13738 tree cdim, array_type;
13739 int error_found = 0;
13740 int ndims = 0;
13741 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13742
13743 /* Dimension types are verified. It's better for the types to be
13744 verified in order. */
13745 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13746 {
13747 int dim_error = 0;
13748 tree dim = TREE_VALUE (cdim);
13749
13750 /* Dim might have been saved during its evaluation */
13751 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13752
13753 /* The type of each specified dimension must be an integral type. */
13754 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13755 dim_error = 1;
13756
13757 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13758 promoted type must be int. */
13759 else
13760 {
15fdcfe9 13761 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13762 if (TREE_TYPE (dim) != int_type_node)
13763 dim_error = 1;
13764 }
13765
13766 /* Report errors on types here */
13767 if (dim_error)
13768 {
13769 parse_error_context
13770 (TREE_PURPOSE (cdim),
781b0558 13771 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13772 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13773 "Explicit cast needed to" : "Can't"),
0a2138e2 13774 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13775 error_found = 1;
13776 }
13777
e04a16fb
AG
13778 TREE_PURPOSE (cdim) = NULL_TREE;
13779 }
13780
13781 /* Resolve array base type if unresolved */
13782 if (!(type = resolve_type_during_patch (type)))
13783 error_found = 1;
13784
13785 if (error_found)
13786 {
13787 /* We don't want further evaluation of this bogus array creation
13788 operation */
13789 TREE_TYPE (node) = error_mark_node;
13790 return error_mark_node;
13791 }
13792
15fdcfe9
PB
13793 /* Set array_type to the actual (promoted) array type of the result. */
13794 if (TREE_CODE (type) == RECORD_TYPE)
13795 type = build_pointer_type (type);
13796 while (--xdims >= 0)
13797 {
13798 type = promote_type (build_java_array_type (type, -1));
13799 }
13800 dims = nreverse (dims);
13801 array_type = type;
13802 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13803 {
13804 type = array_type;
05bccae2
RK
13805 array_type
13806 = build_java_array_type (type,
13807 TREE_CODE (cdim) == INTEGER_CST
13808 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13809 : -1);
15fdcfe9
PB
13810 array_type = promote_type (array_type);
13811 }
13812 dims = nreverse (dims);
13813
e04a16fb
AG
13814 /* The node is transformed into a function call. Things are done
13815 differently according to the number of dimensions. If the number
13816 of dimension is equal to 1, then the nature of the base type
13817 (primitive or not) matters. */
15fdcfe9 13818 if (ndims == 1)
fdec99c6 13819 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13820
e04a16fb
AG
13821 /* Can't reuse what's already written in expr.c because it uses the
13822 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13823 return build (CALL_EXPR, array_type,
e04a16fb 13824 build_address_of (soft_multianewarray_node),
15fdcfe9 13825 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13826 tree_cons (NULL_TREE,
15fdcfe9 13827 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13828 NULL_TREE);
13829}
13830
f8976021
APB
13831/* 10.6 Array initializer. */
13832
13833/* Build a wfl for array element that don't have one, so we can
13834 pin-point errors. */
13835
13836static tree
13837maybe_build_array_element_wfl (node)
13838 tree node;
13839{
13840 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13841 return build_expr_wfl (NULL_TREE, ctxp->filename,
13842 ctxp->elc.line, ctxp->elc.prev_col);
13843 else
13844 return NULL_TREE;
13845}
13846
13847/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13848 identification of initialized arrays easier to detect during walk
13849 and expansion. */
13850
13851static tree
13852build_new_array_init (location, values)
13853 int location;
13854 tree values;
13855{
13856 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13857 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13858 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13859 return to_return;
13860}
13861
13862/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13863 occurred. Otherwise return NODE after having set its type
13864 appropriately. */
13865
13866static tree
13867patch_new_array_init (type, node)
13868 tree type, node;
f8976021
APB
13869{
13870 int error_seen = 0;
fdec99c6 13871 tree current, element_type;
f8976021 13872 HOST_WIDE_INT length;
fdec99c6
PB
13873 int all_constant = 1;
13874 tree init = TREE_OPERAND (node, 0);
f8976021 13875
fdec99c6
PB
13876 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13877 {
13878 parse_error_context (node,
13879 "Invalid array initializer for non-array type `%s'",
13880 lang_printable_name (type, 1));
13881 return error_mark_node;
13882 }
13883 type = TREE_TYPE (type);
13884 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13885
fdec99c6
PB
13886 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13887
13888 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13889 current; length++, current = TREE_CHAIN (current))
f8976021 13890 {
fdec99c6
PB
13891 tree elt = TREE_VALUE (current);
13892 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13893 {
fdec99c6 13894 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13895 elt = TREE_VALUE (current);
13896 /* When compiling to native code, STRING_CST is converted to
13897 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13898 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13899 all_constant = 0;
f8976021 13900 }
fdec99c6
PB
13901 else
13902 {
13903 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13904 TREE_PURPOSE (current) = NULL_TREE;
13905 all_constant = 0;
13906 }
9a7ab4b3
APB
13907 if (elt && TREE_CODE (elt) == TREE_LIST
13908 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13909 error_seen = 1;
f8976021
APB
13910 }
13911
13912 if (error_seen)
13913 return error_mark_node;
13914
13915 /* Create a new type. We can't reuse the one we have here by
13916 patching its dimension because it originally is of dimension -1
13917 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13918 type = build_java_array_type (element_type, length);
13919 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13920 TREE_TYPE (node) = promote_type (type);
13921 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13922 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13923 return node;
13924}
13925
13926/* Verify that one entry of the initializer element list can be
13927 assigned to the array base type. Report 1 if an error occurred, 0
13928 otherwise. */
13929
13930static int
13931array_constructor_check_entry (type, entry)
13932 tree type, entry;
13933{
13934 char *array_type_string = NULL; /* For error reports */
13935 tree value, type_value, new_value, wfl_value, patched;
13936 int error_seen = 0;
13937
13938 new_value = NULL_TREE;
13939 wfl_value = TREE_VALUE (entry);
13940
f8976021 13941 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13942 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13943 if ((patched = patch_string (value)))
13944 value = patched;
1179ebc2
APB
13945 if (value == error_mark_node)
13946 return 1;
f8976021 13947
f8976021
APB
13948 type_value = TREE_TYPE (value);
13949
1179ebc2 13950 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13951 constant overflow during narrowing. */
13952 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13953 new_value = try_builtin_assignconv (wfl_operator, type, value);
13954 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13955 type_value = promote_type (type);
100f7cd8 13956
f8976021
APB
13957 /* Check and report errors */
13958 if (!new_value)
13959 {
49f48c71 13960 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13961 "Can't" : "Explicit cast needed to");
13962 if (!array_type_string)
c2e3db92 13963 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13964 parse_error_context
13965 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13966 msg, lang_printable_name (type_value, 1), array_type_string);
13967 error_seen = 1;
13968 }
13969
13970 if (new_value)
13971 {
b8c5b1c6 13972 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13973 TREE_VALUE (entry) = new_value;
13974 }
13975
13976 if (array_type_string)
13977 free (array_type_string);
13978
13979 TREE_PURPOSE (entry) = NULL_TREE;
13980 return error_seen;
13981}
13982
e04a16fb
AG
13983static tree
13984build_this (location)
13985 int location;
13986{
9ee9b555 13987 tree node = build_wfl_node (this_identifier_node);
b67d701b 13988 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13989 EXPR_WFL_LINECOL (node) = location;
13990 return node;
13991}
13992
13993/* 14.15 The return statement. It builds a modify expression that
13994 assigns the returned value to the RESULT_DECL that hold the value
13995 to be returned. */
13996
13997static tree
13998build_return (location, op)
13999 int location;
14000 tree op;
14001{
14002 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14003 EXPR_WFL_LINECOL (node) = location;
b67d701b 14004 node = build_debugable_stmt (location, node);
e04a16fb
AG
14005 return node;
14006}
14007
14008static tree
14009patch_return (node)
14010 tree node;
14011{
14012 tree return_exp = TREE_OPERAND (node, 0);
14013 tree meth = current_function_decl;
14014 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
14015 int error_found = 0;
14016
14017 TREE_TYPE (node) = error_mark_node;
14018 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14019
14020 /* It's invalid to have a return value within a function that is
14021 declared with the keyword void or that is a constructor */
14022 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14023 error_found = 1;
14024
f099f336 14025 /* It's invalid to use a return statement in a static block */
c2952b01 14026 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
14027 error_found = 1;
14028
e04a16fb
AG
14029 /* It's invalid to have a no return value within a function that
14030 isn't declared with the keyword `void' */
14031 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14032 error_found = 2;
c2952b01
APB
14033
14034 if (in_instance_initializer)
14035 error_found = 1;
e04a16fb
AG
14036
14037 if (error_found)
14038 {
c2952b01 14039 if (in_instance_initializer)
f099f336 14040 parse_error_context (wfl_operator,
c2952b01
APB
14041 "`return' inside instance initializer");
14042
14043 else if (DECL_CLINIT_P (current_function_decl))
14044 parse_error_context (wfl_operator,
14045 "`return' inside static initializer");
f099f336
APB
14046
14047 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 14048 {
c2e3db92 14049 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
14050 parse_error_context (wfl_operator,
14051 "`return' with%s value from `%s %s'",
14052 (error_found == 1 ? "" : "out"),
14053 t, lang_printable_name (meth, 0));
14054 free (t);
14055 }
14056 else
14057 parse_error_context (wfl_operator,
14058 "`return' with value from constructor `%s'",
14059 lang_printable_name (meth, 0));
e04a16fb
AG
14060 return error_mark_node;
14061 }
14062
5e942c50
APB
14063 /* If we have a return_exp, build a modify expression and expand
14064 it. Note: at that point, the assignment is declared valid, but we
14065 may want to carry some more hacks */
e04a16fb
AG
14066 if (return_exp)
14067 {
5e942c50
APB
14068 tree exp = java_complete_tree (return_exp);
14069 tree modify, patched;
14070
14071 /* If the function returned value and EXP are booleans, EXP has
14072 to be converted into the type of DECL_RESULT, which is integer
14073 (see complete_start_java_method) */
14074 if (TREE_TYPE (exp) == boolean_type_node &&
14075 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14076 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14077
14078 /* `null' can be assigned to a function returning a reference */
14079 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14080 exp == null_pointer_node)
14081 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14082
14083 if ((patched = patch_string (exp)))
14084 exp = patched;
14085
14086 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14087 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14088 modify = java_complete_tree (modify);
5e942c50 14089
e04a16fb
AG
14090 if (modify != error_mark_node)
14091 {
14092 TREE_SIDE_EFFECTS (modify) = 1;
14093 TREE_OPERAND (node, 0) = modify;
14094 }
14095 else
14096 return error_mark_node;
14097 }
14098 TREE_TYPE (node) = void_type_node;
14099 TREE_SIDE_EFFECTS (node) = 1;
14100 return node;
14101}
14102
14103/* 14.8 The if Statement */
14104
14105static tree
14106build_if_else_statement (location, expression, if_body, else_body)
14107 int location;
14108 tree expression, if_body, else_body;
14109{
14110 tree node;
e04a16fb 14111 if (!else_body)
9bbc7d9f 14112 else_body = empty_stmt_node;
e04a16fb
AG
14113 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14114 EXPR_WFL_LINECOL (node) = location;
b67d701b 14115 node = build_debugable_stmt (location, node);
e04a16fb
AG
14116 return node;
14117}
14118
14119static tree
14120patch_if_else_statement (node)
14121 tree node;
14122{
14123 tree expression = TREE_OPERAND (node, 0);
14124
14125 TREE_TYPE (node) = error_mark_node;
14126 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14127
14128 /* The type of expression must be boolean */
b67d701b
PB
14129 if (TREE_TYPE (expression) != boolean_type_node
14130 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14131 {
14132 parse_error_context
14133 (wfl_operator,
14134 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14135 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14136 return error_mark_node;
14137 }
14138
14139 TREE_TYPE (node) = void_type_node;
14140 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14141 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14142 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14143 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14144 return node;
14145}
14146
14147/* 14.6 Labeled Statements */
14148
14149/* Action taken when a lableled statement is parsed. a new
14150 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14151 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14152
14153static tree
0a2138e2 14154build_labeled_block (location, label)
e04a16fb 14155 int location;
0a2138e2 14156 tree label;
e04a16fb 14157{
b635eb2f 14158 tree label_name ;
e04a16fb 14159 tree label_decl, node;
b635eb2f
PB
14160 if (label == NULL_TREE || label == continue_identifier_node)
14161 label_name = label;
14162 else
e04a16fb 14163 {
b635eb2f
PB
14164 label_name = merge_qualified_name (label_id, label);
14165 /* Issue an error if we try to reuse a label that was previously
14166 declared */
14167 if (IDENTIFIER_LOCAL_VALUE (label_name))
14168 {
14169 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14170 parse_error_context (wfl_operator,
14171 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14172 IDENTIFIER_POINTER (label));
14173 EXPR_WFL_LINECOL (wfl_operator) =
14174 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14175 parse_error_context (wfl_operator,
14176 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14177 IDENTIFIER_POINTER (label));
14178 java_error_count--;
14179 }
e04a16fb
AG
14180 }
14181
14182 label_decl = create_label_decl (label_name);
14183 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14184 EXPR_WFL_LINECOL (node) = location;
14185 TREE_SIDE_EFFECTS (node) = 1;
14186 return node;
14187}
14188
b67d701b 14189/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14190
14191static tree
b635eb2f 14192finish_labeled_statement (lbe, statement)
e04a16fb
AG
14193 tree lbe; /* Labeled block expr */
14194 tree statement;
14195{
14196 /* In anyways, tie the loop to its statement */
14197 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14198 pop_labeled_block ();
14199 POP_LABELED_BLOCK ();
e04a16fb
AG
14200 return lbe;
14201}
14202
14203/* 14.10, 14.11, 14.12 Loop Statements */
14204
14205/* Create an empty LOOP_EXPR and make it the last in the nested loop
14206 list. */
14207
14208static tree
14209build_new_loop (loop_body)
14210 tree loop_body;
14211{
14212 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14213 TREE_SIDE_EFFECTS (loop) = 1;
14214 PUSH_LOOP (loop);
14215 return loop;
14216}
14217
14218/* Create a loop body according to the following structure:
14219 COMPOUND_EXPR
14220 COMPOUND_EXPR (loop main body)
14221 EXIT_EXPR (this order is for while/for loops.
14222 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14223 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14224 BODY end of this labeled block)
14225 INCREMENT (if any)
14226
14227 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14228 after the body, like in the do-while loop.
14229
14230 To obtain a loop, the loop body structure described above is
14231 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14232
14233 LABELED_BLOCK_EXPR
14234 LABEL_DECL (use this label to exit the loop)
14235 LOOP_EXPR
14236 <structure described above> */
e04a16fb
AG
14237
14238static tree
14239build_loop_body (location, condition, reversed)
14240 int location;
14241 tree condition;
14242 int reversed;
14243{
0a2138e2 14244 tree first, second, body;
e04a16fb
AG
14245
14246 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14247 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14248 condition = build_debugable_stmt (location, condition);
14249 TREE_SIDE_EFFECTS (condition) = 1;
14250
b635eb2f 14251 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14252 first = (reversed ? body : condition);
14253 second = (reversed ? condition : body);
14254 return
14255 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14256 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14257}
14258
14259/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14260 their order) on the current loop. Unlink the current loop from the
14261 loop list. */
14262
14263static tree
b635eb2f 14264finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14265 int location;
14266 tree condition, body;
14267 int reversed;
14268{
14269 tree to_return = ctxp->current_loop;
14270 tree loop_body = LOOP_EXPR_BODY (to_return);
14271 if (condition)
14272 {
14273 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14274 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14275 The real EXIT_EXPR is one operand further. */
14276 EXPR_WFL_LINECOL (cnode) = location;
14277 /* This one is for accurate error reports */
14278 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14279 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14280 }
14281 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14282 POP_LOOP ();
14283 return to_return;
14284}
14285
b635eb2f 14286/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14287 loops feature the condition part */
14288
14289static tree
b635eb2f 14290finish_for_loop (location, condition, update, body)
e04a16fb
AG
14291 int location;
14292 tree condition, update, body;
14293{
14294 /* Put the condition and the loop body in place */
b635eb2f 14295 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14296 /* LOOP is the current loop which has been now popped of the loop
14297 stack. Install the update block */
14298 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14299 return loop;
14300}
14301
5cbdba64
APB
14302/* Try to find the loop a block might be related to. This comprises
14303 the case where the LOOP_EXPR is found as the second operand of a
14304 COMPOUND_EXPR, because the loop happens to have an initialization
14305 part, then expressed as the first operand of the COMPOUND_EXPR. If
14306 the search finds something, 1 is returned. Otherwise, 0 is
14307 returned. The search is assumed to start from a
14308 LABELED_BLOCK_EXPR's block. */
14309
14310static tree
14311search_loop (statement)
14312 tree statement;
14313{
14314 if (TREE_CODE (statement) == LOOP_EXPR)
14315 return statement;
14316
14317 if (TREE_CODE (statement) == BLOCK)
14318 statement = BLOCK_SUBBLOCKS (statement);
14319 else
14320 return NULL_TREE;
14321
14322 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14323 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14324 statement = TREE_OPERAND (statement, 1);
14325
14326 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14327 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14328}
14329
14330/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14331 returned otherwise. */
14332
14333static int
14334labeled_block_contains_loop_p (block, loop)
14335 tree block, loop;
14336{
14337 if (!block)
14338 return 0;
14339
14340 if (LABELED_BLOCK_BODY (block) == loop)
14341 return 1;
14342
c2952b01 14343 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14344 return 1;
14345
14346 return 0;
14347}
14348
e04a16fb 14349/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14350 insert LOOP as its body. */
e04a16fb
AG
14351
14352static tree
14353patch_loop_statement (loop)
14354 tree loop;
14355{
cd9643f7 14356 tree loop_label;
5cbdba64 14357
cd9643f7 14358 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14359 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14360 return loop;
14361
cd9643f7 14362 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14363 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14364 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14365 LABELED_BLOCK_BODY (loop_label) = loop;
14366 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14367 return loop_label;
e04a16fb
AG
14368}
14369
14370/* 14.13, 14.14: break and continue Statements */
14371
14372/* Build a break or a continue statement. a null NAME indicates an
14373 unlabeled break/continue statement. */
14374
14375static tree
14376build_bc_statement (location, is_break, name)
14377 int location, is_break;
14378 tree name;
14379{
14380 tree break_continue, label_block_expr = NULL_TREE;
14381
14382 if (name)
14383 {
14384 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14385 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14386 /* Null means that we don't have a target for this named
14387 break/continue. In this case, we make the target to be the
14388 label name, so that the error can be reported accuratly in
14389 patch_bc_statement. */
14390 label_block_expr = EXPR_WFL_NODE (name);
14391 }
14392 /* Unlabeled break/continue will be handled during the
14393 break/continue patch operation */
14394 break_continue
14395 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14396
14397 IS_BREAK_STMT_P (break_continue) = is_break;
14398 TREE_SIDE_EFFECTS (break_continue) = 1;
14399 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14400 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14401 return break_continue;
14402}
14403
14404/* Verification of a break/continue statement. */
14405
14406static tree
14407patch_bc_statement (node)
14408 tree node;
14409{
14410 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14411 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14412 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14413
e04a16fb 14414 /* Having an identifier here means that the target is unknown. */
b635eb2f 14415 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14416 {
14417 parse_error_context (wfl_operator, "No label definition found for `%s'",
14418 IDENTIFIER_POINTER (bc_label));
14419 return error_mark_node;
14420 }
b635eb2f 14421 if (! IS_BREAK_STMT_P (node))
e04a16fb 14422 {
b635eb2f
PB
14423 /* It's a continue statement. */
14424 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14425 {
b635eb2f
PB
14426 if (labeled_block == NULL_TREE)
14427 {
14428 if (bc_label == NULL_TREE)
14429 parse_error_context (wfl_operator,
14430 "`continue' must be in loop");
14431 else
1504b2b4
APB
14432 parse_error_context
14433 (wfl_operator, "continue label `%s' does not name a loop",
14434 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14435 return error_mark_node;
14436 }
14437 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14438 == continue_identifier_node)
14439 && (bc_label == NULL_TREE
14440 || TREE_CHAIN (labeled_block) == bc_label))
14441 {
14442 bc_label = labeled_block;
14443 break;
14444 }
e04a16fb 14445 }
e04a16fb 14446 }
b635eb2f 14447 else if (!bc_label)
34f4db93 14448 {
b635eb2f 14449 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14450 {
b635eb2f
PB
14451 if (labeled_block == NULL_TREE)
14452 {
14453 parse_error_context (wfl_operator,
14454 "`break' must be in loop or switch");
14455 return error_mark_node;
14456 }
14457 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14458 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14459 || search_loop (target_stmt))
b635eb2f
PB
14460 {
14461 bc_label = labeled_block;
14462 break;
14463 }
e04a16fb 14464 }
e04a16fb
AG
14465 }
14466
b635eb2f 14467 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14468 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14469
e04a16fb
AG
14470 /* Our break/continue don't return values. */
14471 TREE_TYPE (node) = void_type_node;
14472 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14473 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14474 sometimes, like after a if statement) */
14475 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14476 TREE_SIDE_EFFECTS (node) = 1;
14477 return node;
14478}
14479
14480/* Process the exit expression belonging to a loop. Its type must be
14481 boolean. */
14482
14483static tree
14484patch_exit_expr (node)
14485 tree node;
14486{
14487 tree expression = TREE_OPERAND (node, 0);
14488 TREE_TYPE (node) = error_mark_node;
14489 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14490
14491 /* The type of expression must be boolean */
14492 if (TREE_TYPE (expression) != boolean_type_node)
14493 {
14494 parse_error_context
14495 (wfl_operator,
781b0558 14496 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14497 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14498 return error_mark_node;
14499 }
14500 /* Now we know things are allright, invert the condition, fold and
14501 return */
14502 TREE_OPERAND (node, 0) =
14503 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14504
14505 if (! integer_zerop (TREE_OPERAND (node, 0))
14506 && ctxp->current_loop != NULL_TREE
14507 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14508 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14509 if (! integer_onep (TREE_OPERAND (node, 0)))
14510 CAN_COMPLETE_NORMALLY (node) = 1;
14511
14512
e04a16fb
AG
14513 TREE_TYPE (node) = void_type_node;
14514 return node;
14515}
b67d701b
PB
14516
14517/* 14.9 Switch statement */
14518
14519static tree
14520patch_switch_statement (node)
14521 tree node;
14522{
c877974e 14523 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14524
14525 /* Complete the switch expression */
14526 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14527 se_type = TREE_TYPE (se);
14528 /* The type of the switch expression must be char, byte, short or
14529 int */
2e0f0aff 14530 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14531 {
14532 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14533 parse_error_context (wfl_operator,
14534 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14535 lang_printable_name (se_type, 0));
b67d701b
PB
14536 /* This is what java_complete_tree will check */
14537 TREE_OPERAND (node, 0) = error_mark_node;
14538 return error_mark_node;
14539 }
14540
15fdcfe9 14541 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14542
14543 /* Ready to return */
15fdcfe9 14544 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14545 {
14546 TREE_TYPE (node) = error_mark_node;
14547 return error_mark_node;
14548 }
14549 TREE_TYPE (node) = void_type_node;
14550 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14551 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14552 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14553 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14554 return node;
14555}
14556
165f37bc 14557/* 14.18 The try/catch statements */
b67d701b 14558
b67d701b 14559static tree
a7d8d81f 14560build_try_statement (location, try_block, catches)
b67d701b 14561 int location;
a7d8d81f
PB
14562 tree try_block, catches;
14563{
14564 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14565 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14566 return node;
b67d701b
PB
14567}
14568
a7d8d81f
PB
14569static tree
14570build_try_finally_statement (location, try_block, finally)
14571 int location;
14572 tree try_block, finally;
b67d701b 14573{
a7d8d81f
PB
14574 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14575 EXPR_WFL_LINECOL (node) = location;
14576 return node;
b67d701b
PB
14577}
14578
14579static tree
14580patch_try_statement (node)
14581 tree node;
14582{
14583 int error_found = 0;
14584 tree try = TREE_OPERAND (node, 0);
14585 /* Exception handlers are considered in left to right order */
14586 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14587 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14588
14589 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14590 to process the next catch clause. We process the catch clause before
14591 the try block so that when processing the try block we can check thrown
14592 exceptions againts the caught type list. */
b67d701b
PB
14593 for (current = catch; current; current = TREE_CHAIN (current))
14594 {
14595 tree carg_decl, carg_type;
14596 tree sub_current, catch_block, catch_clause;
14597 int unreachable;
14598
b67d701b 14599 /* At this point, the structure of the catch clause is
b67d701b
PB
14600 CATCH_EXPR (catch node)
14601 BLOCK (with the decl of the parameter)
14602 COMPOUND_EXPR
7525cc04 14603 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14604 BLOCK (catch clause block)
a7d8d81f
PB
14605 */
14606 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14607 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14608 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14609
14610 /* Catch clauses can't have more than one parameter declared,
14611 but it's already enforced by the grammar. Make sure that the
14612 only parameter of the clause statement in of class Throwable
14613 or a subclass of Throwable, but that was done earlier. The
14614 catch clause parameter type has also been resolved. */
14615
14616 /* Just make sure that the catch clause parameter type inherits
14617 from java.lang.Throwable */
14618 if (!inherits_from_p (carg_type, throwable_type_node))
14619 {
14620 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14621 parse_error_context (wfl_operator,
781b0558 14622 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14623 lang_printable_name (carg_type, 0));
b67d701b
PB
14624 error_found = 1;
14625 continue;
14626 }
14627
14628 /* Partial check for unreachable catch statement: The catch
14629 clause is reachable iff is no earlier catch block A in
14630 the try statement such that the type of the catch
14631 clause's parameter is the same as or a subclass of the
14632 type of A's parameter */
14633 unreachable = 0;
14634 for (sub_current = catch;
14635 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14636 {
14637 tree sub_catch_clause, decl;
a7d8d81f 14638 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14639 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14640
14641 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14642 {
14643 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14644 parse_error_context
781b0558
KG
14645 (wfl_operator,
14646 "`catch' not reached because of the catch clause at line %d",
14647 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14648 unreachable = error_found = 1;
14649 break;
14650 }
14651 }
b67d701b
PB
14652 /* Complete the catch clause block */
14653 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14654 if (catch_block == error_mark_node)
14655 {
14656 error_found = 1;
14657 continue;
14658 }
15fdcfe9
PB
14659 if (CAN_COMPLETE_NORMALLY (catch_block))
14660 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14661 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14662
14663 if (unreachable)
14664 continue;
14665
14666 /* Things to do here: the exception must be thrown */
14667
14668 /* Link this type to the caught type list */
14669 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14670 }
14671
b9f7e36c
APB
14672 PUSH_EXCEPTIONS (caught_type_list);
14673 if ((try = java_complete_tree (try)) == error_mark_node)
14674 error_found = 1;
15fdcfe9
PB
14675 if (CAN_COMPLETE_NORMALLY (try))
14676 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14677 POP_EXCEPTIONS ();
14678
b67d701b
PB
14679 /* Verification ends here */
14680 if (error_found)
14681 return error_mark_node;
14682
14683 TREE_OPERAND (node, 0) = try;
14684 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14685 TREE_TYPE (node) = void_type_node;
14686 return node;
14687}
b9f7e36c
APB
14688
14689/* 14.17 The synchronized Statement */
14690
14691static tree
14692patch_synchronized_statement (node, wfl_op1)
14693 tree node, wfl_op1;
14694{
5a005d9e 14695 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14696 tree block = TREE_OPERAND (node, 1);
5a005d9e 14697
d8fccff5 14698 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14699
14700 if (expr == error_mark_node)
14701 {
14702 block = java_complete_tree (block);
14703 return expr;
14704 }
b9f7e36c
APB
14705
14706 /* The TYPE of expr must be a reference type */
5a005d9e 14707 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14708 {
14709 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14710 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14711 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14712 return error_mark_node;
14713 }
14714
ce6e9147
APB
14715 if (flag_emit_xref)
14716 {
14717 TREE_OPERAND (node, 0) = expr;
14718 TREE_OPERAND (node, 1) = java_complete_tree (block);
14719 CAN_COMPLETE_NORMALLY (node) = 1;
14720 return node;
14721 }
14722
b9f7e36c
APB
14723 /* Generate a try-finally for the synchronized statement, except
14724 that the handler that catches all throw exception calls
14725 _Jv_MonitorExit and then rethrow the exception.
14726 The synchronized statement is then implemented as:
14727 TRY
14728 {
14729 _Jv_MonitorEnter (expression)
14730 synchronized_block
14731 _Jv_MonitorExit (expression)
14732 }
14733 CATCH_ALL
14734 {
14735 e = _Jv_exception_info ();
14736 _Jv_MonitorExit (expression)
14737 Throw (e);
14738 } */
14739
5a005d9e
PB
14740 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14741 BUILD_MONITOR_ENTER (enter, expr_decl);
14742 BUILD_MONITOR_EXIT (exit, expr_decl);
14743 CAN_COMPLETE_NORMALLY (enter) = 1;
14744 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14745 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14746 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14747 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14748 build (COMPOUND_EXPR, NULL_TREE,
14749 build (WITH_CLEANUP_EXPR, NULL_TREE,
14750 build (COMPOUND_EXPR, NULL_TREE,
96847892 14751 assignment, enter),
5a005d9e
PB
14752 NULL_TREE, exit),
14753 block));
14754 node = build_expr_block (node, expr_decl);
14755
14756 return java_complete_tree (node);
b9f7e36c
APB
14757}
14758
14759/* 14.16 The throw Statement */
14760
14761static tree
14762patch_throw_statement (node, wfl_op1)
14763 tree node, wfl_op1;
14764{
14765 tree expr = TREE_OPERAND (node, 0);
14766 tree type = TREE_TYPE (expr);
14767 int unchecked_ok = 0, tryblock_throws_ok = 0;
14768
14769 /* Thrown expression must be assignable to java.lang.Throwable */
14770 if (!try_reference_assignconv (throwable_type_node, expr))
14771 {
14772 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14773 parse_error_context (wfl_operator,
14774 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14775 lang_printable_name (type, 0));
b9f7e36c
APB
14776 /* If the thrown expression was a reference, we further the
14777 compile-time check. */
14778 if (!JREFERENCE_TYPE_P (type))
14779 return error_mark_node;
14780 }
14781
14782 /* At least one of the following must be true */
14783
14784 /* The type of the throw expression is a not checked exception,
14785 i.e. is a unchecked expression. */
c877974e 14786 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14787
c2952b01
APB
14788 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14789 /* An instance can't throw a checked excetion unless that exception
14790 is explicitely declared in the `throws' clause of each
14791 constructor. This doesn't apply to anonymous classes, since they
14792 don't have declared constructors. */
14793 if (!unchecked_ok
14794 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14795 {
14796 tree current;
14797 for (current = TYPE_METHODS (current_class); current;
14798 current = TREE_CHAIN (current))
14799 if (DECL_CONSTRUCTOR_P (current)
14800 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14801 {
14802 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)",
14803 lang_printable_name (TREE_TYPE (expr), 0));
14804 return error_mark_node;
14805 }
14806 }
14807
b9f7e36c
APB
14808 /* Throw is contained in a try statement and at least one catch
14809 clause can receive the thrown expression or the current method is
14810 declared to throw such an exception. Or, the throw statement is
14811 contained in a method or constructor declaration and the type of
14812 the Expression is assignable to at least one type listed in the
14813 throws clause the declaration. */
b9f7e36c 14814 if (!unchecked_ok)
f099f336 14815 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14816 if (!(unchecked_ok || tryblock_throws_ok))
14817 {
14818 /* If there is a surrounding try block that has no matching
14819 clatch clause, report it first. A surrounding try block exits
14820 only if there is something after the list of checked
14821 exception thrown by the current function (if any). */
14822 if (IN_TRY_BLOCK_P ())
781b0558 14823 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 14824 lang_printable_name (type, 0));
b9f7e36c
APB
14825 /* If we have no surrounding try statement and the method doesn't have
14826 any throws, report it now. FIXME */
f099f336
APB
14827
14828 /* We report that the exception can't be throw from a try block
14829 in all circumstances but when the `throw' is inside a static
14830 block. */
b9f7e36c
APB
14831 else if (!EXCEPTIONS_P (currently_caught_type_list)
14832 && !tryblock_throws_ok)
f099f336 14833 {
c2952b01 14834 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14835 parse_error_context (wfl_operator,
14836 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14837 lang_printable_name (type, 0));
14838 else
781b0558
KG
14839 parse_error_context (wfl_operator,
14840 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14841 lang_printable_name (type, 0));
14842 }
b9f7e36c
APB
14843 /* Otherwise, the current method doesn't have the appropriate
14844 throws declaration */
14845 else
781b0558 14846 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14847 lang_printable_name (type, 0));
b9f7e36c
APB
14848 return error_mark_node;
14849 }
14850
ce6e9147 14851 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14852 BUILD_THROW (node, expr);
ce6e9147
APB
14853
14854 /* If doing xrefs, keep the location where the `throw' was seen. */
14855 if (flag_emit_xref)
14856 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14857 return node;
14858}
14859
14860/* Check that exception said to be thrown by method DECL can be
14861 effectively caught from where DECL is invoked. */
14862
14863static void
14864check_thrown_exceptions (location, decl)
14865 int location;
14866 tree decl;
14867{
14868 tree throws;
14869 /* For all the unchecked exceptions thrown by DECL */
14870 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14871 throws = TREE_CHAIN (throws))
0a2138e2 14872 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14873 {
3e78f871
PB
14874#if 1
14875 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14876 if (DECL_NAME (decl) == get_identifier ("clone"))
14877 continue;
14878#endif
b9f7e36c 14879 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14880 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14881 parse_error_context
14882 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14883 lang_printable_name (TREE_VALUE (throws), 0));
14884 else
14885 {
14886 parse_error_context
781b0558 14887 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14888 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14889 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14890 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14891 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14892 }
b9f7e36c
APB
14893 }
14894}
14895
c877974e 14896/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14897 try-catch blocks, OR is listed in the `throws' clause of the
14898 current method. */
14899
14900static int
0a2138e2 14901check_thrown_exceptions_do (exception)
b9f7e36c
APB
14902 tree exception;
14903{
14904 tree list = currently_caught_type_list;
c877974e 14905 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14906 /* First, all the nested try-catch-finally at that stage. The
14907 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14908 if (IS_UNCHECKED_EXCEPTION_P (exception))
14909 return 1;
b9f7e36c
APB
14910 while (list)
14911 {
14912 tree caught;
14913 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14914 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14915 return 1;
14916 list = TREE_CHAIN (list);
14917 }
14918 return 0;
14919}
14920
14921static void
14922purge_unchecked_exceptions (mdecl)
14923 tree mdecl;
14924{
14925 tree throws = DECL_FUNCTION_THROWS (mdecl);
14926 tree new = NULL_TREE;
14927
14928 while (throws)
14929 {
14930 tree next = TREE_CHAIN (throws);
c877974e 14931 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14932 {
14933 TREE_CHAIN (throws) = new;
14934 new = throws;
14935 }
14936 throws = next;
14937 }
14938 /* List is inverted here, but it doesn't matter */
14939 DECL_FUNCTION_THROWS (mdecl) = new;
14940}
22eed1e6
APB
14941
14942/* 15.24 Conditional Operator ?: */
14943
14944static tree
14945patch_conditional_expr (node, wfl_cond, wfl_op1)
14946 tree node, wfl_cond, wfl_op1;
14947{
14948 tree cond = TREE_OPERAND (node, 0);
14949 tree op1 = TREE_OPERAND (node, 1);
14950 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14951 tree resulting_type = NULL_TREE;
ac825856 14952 tree t1, t2, patched;
22eed1e6
APB
14953 int error_found = 0;
14954
ac825856
APB
14955 /* Operands of ?: might be StringBuffers crafted as a result of a
14956 string concatenation. Obtain a descent operand here. */
14957 if ((patched = patch_string (op1)))
14958 TREE_OPERAND (node, 1) = op1 = patched;
14959 if ((patched = patch_string (op2)))
14960 TREE_OPERAND (node, 2) = op2 = patched;
14961
14962 t1 = TREE_TYPE (op1);
14963 t2 = TREE_TYPE (op2);
14964
22eed1e6
APB
14965 /* The first expression must be a boolean */
14966 if (TREE_TYPE (cond) != boolean_type_node)
14967 {
14968 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14969 parse_error_context (wfl_operator,
14970 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14971 lang_printable_name (TREE_TYPE (cond), 0));
14972 error_found = 1;
14973 }
14974
14975 /* Second and third can be numeric, boolean (i.e. primitive),
14976 references or null. Anything else results in an error */
14977 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14978 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14979 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14980 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14981 error_found = 1;
14982
14983 /* Determine the type of the conditional expression. Same types are
14984 easy to deal with */
14985 else if (t1 == t2)
14986 resulting_type = t1;
14987
14988 /* There are different rules for numeric types */
14989 else if (JNUMERIC_TYPE_P (t1))
14990 {
14991 /* if byte/short found, the resulting type is short */
14992 if ((t1 == byte_type_node && t2 == short_type_node)
14993 || (t1 == short_type_node && t2 == byte_type_node))
14994 resulting_type = short_type_node;
14995
14996 /* If t1 is a constant int and t2 is of type byte, short or char
14997 and t1's value fits in t2, then the resulting type is t2 */
14998 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14999 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15000 resulting_type = t2;
15001
15002 /* If t2 is a constant int and t1 is of type byte, short or char
15003 and t2's value fits in t1, then the resulting type is t1 */
15004 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15005 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15006 resulting_type = t1;
15007
15008 /* Otherwise, binary numeric promotion is applied and the
15009 resulting type is the promoted type of operand 1 and 2 */
15010 else
93024893 15011 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
15012 &TREE_OPERAND (node, 1),
15013 &TREE_OPERAND (node, 2));
15014 }
15015
15016 /* Cases of a reference and a null type */
15017 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15018 resulting_type = t1;
15019
15020 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15021 resulting_type = t2;
15022
15023 /* Last case: different reference types. If a type can be converted
15024 into the other one by assignment conversion, the latter
15025 determines the type of the expression */
15026 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15027 resulting_type = promote_type (t1);
15028
15029 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15030 resulting_type = promote_type (t2);
15031
15032 /* If we don't have any resulting type, we're in trouble */
15033 if (!resulting_type)
15034 {
c2e3db92 15035 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 15036 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
15037 parse_error_context (wfl_operator,
15038 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15039 t, lang_printable_name (t2, 0));
22eed1e6
APB
15040 free (t);
15041 error_found = 1;
15042 }
15043
15044 if (error_found)
15045 {
15046 TREE_TYPE (node) = error_mark_node;
15047 return error_mark_node;
15048 }
15049
15050 TREE_TYPE (node) = resulting_type;
15051 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 15052 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
15053 return node;
15054}
ac825856 15055
5b09b33e
PB
15056/* Try to constant fold NODE.
15057 If NODE is not a constant expression, return NULL_EXPR.
15058 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15059
15060static tree
15061fold_constant_for_init (node, context)
15062 tree node;
15063 tree context;
15064{
15065 tree op0, op1, val;
15066 enum tree_code code = TREE_CODE (node);
15067
ee97d354 15068 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 15069 return node;
93024893 15070
5b09b33e
PB
15071 switch (code)
15072 {
5b09b33e
PB
15073 case PLUS_EXPR:
15074 case MINUS_EXPR:
bc3ca41b
PB
15075 case MULT_EXPR:
15076 case TRUNC_MOD_EXPR:
15077 case RDIV_EXPR:
5b09b33e
PB
15078 case LSHIFT_EXPR:
15079 case RSHIFT_EXPR:
15080 case URSHIFT_EXPR:
15081 case BIT_AND_EXPR:
15082 case BIT_XOR_EXPR:
15083 case BIT_IOR_EXPR:
5b09b33e
PB
15084 case TRUTH_ANDIF_EXPR:
15085 case TRUTH_ORIF_EXPR:
15086 case EQ_EXPR:
15087 case NE_EXPR:
15088 case GT_EXPR:
15089 case GE_EXPR:
15090 case LT_EXPR:
15091 case LE_EXPR:
15092 op0 = TREE_OPERAND (node, 0);
15093 op1 = TREE_OPERAND (node, 1);
15094 val = fold_constant_for_init (op0, context);
15095 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15096 return NULL_TREE;
15097 TREE_OPERAND (node, 0) = val;
15098 val = fold_constant_for_init (op1, context);
15099 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15100 return NULL_TREE;
15101 TREE_OPERAND (node, 1) = val;
15102 return patch_binop (node, op0, op1);
15103
15104 case UNARY_PLUS_EXPR:
15105 case NEGATE_EXPR:
15106 case TRUTH_NOT_EXPR:
15107 case BIT_NOT_EXPR:
15108 case CONVERT_EXPR:
15109 op0 = TREE_OPERAND (node, 0);
15110 val = fold_constant_for_init (op0, context);
15111 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15112 return NULL_TREE;
15113 TREE_OPERAND (node, 0) = val;
5a005d9e 15114 return patch_unaryop (node, op0);
5b09b33e
PB
15115 break;
15116
15117 case COND_EXPR:
15118 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15119 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15120 return NULL_TREE;
15121 TREE_OPERAND (node, 0) = val;
15122 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15123 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15124 return NULL_TREE;
15125 TREE_OPERAND (node, 1) = val;
15126 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15127 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15128 return NULL_TREE;
15129 TREE_OPERAND (node, 2) = val;
15130 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15131 : TREE_OPERAND (node, 2);
15132
15133 case VAR_DECL:
8576f094
APB
15134 case FIELD_DECL:
15135 if (! FIELD_FINAL (node)
5b09b33e
PB
15136 || DECL_INITIAL (node) == NULL_TREE)
15137 return NULL_TREE;
15138 val = DECL_INITIAL (node);
15139 /* Guard against infinite recursion. */
15140 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15141 val = fold_constant_for_init (val, node);
5b09b33e
PB
15142 DECL_INITIAL (node) = val;
15143 return val;
15144
15145 case EXPR_WITH_FILE_LOCATION:
15146 /* Compare java_complete_tree and resolve_expression_name. */
15147 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15148 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15149 {
15150 tree name = EXPR_WFL_NODE (node);
15151 tree decl;
15152 if (PRIMARY_P (node))
15153 return NULL_TREE;
15154 else if (! QUALIFIED_P (name))
15155 {
15156 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15157 if (decl == NULL_TREE
15158 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15159 return NULL_TREE;
15160 return fold_constant_for_init (decl, decl);
15161 }
15162 else
15163 {
5b09b33e
PB
15164 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15165 qualify_ambiguous_name (node);
15166 if (resolve_field_access (node, &decl, NULL)
15167 && decl != NULL_TREE)
15168 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15169 return NULL_TREE;
15170 }
15171 }
15172 else
15173 {
15174 op0 = TREE_OPERAND (node, 0);
15175 val = fold_constant_for_init (op0, context);
15176 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15177 return NULL_TREE;
15178 TREE_OPERAND (node, 0) = val;
15179 return val;
15180 }
15181
bc3ca41b
PB
15182#ifdef USE_COMPONENT_REF
15183 case IDENTIFIER:
15184 case COMPONENT_REF:
15185 ?;
15186#endif
15187
5b09b33e
PB
15188 default:
15189 return NULL_TREE;
15190 }
15191}
bc3ca41b
PB
15192
15193#ifdef USE_COMPONENT_REF
15194/* Context is 'T' for TypeName, 'P' for PackageName,
15195 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15196
15197tree
15198resolve_simple_name (name, context)
15199 tree name;
15200 int context;
15201{
15202}
15203
15204tree
15205resolve_qualified_name (name, context)
15206 tree name;
15207 int context;
15208{
15209}
15210#endif
f15b9af9
MM
15211
15212/* Mark P, which is really a `struct parser_ctxt **' for GC. */
15213
15214static void
15215mark_parser_ctxt (p)
15216 void *p;
15217{
15218 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15219 int i;
15220
15221 if (!pc)
15222 return;
15223
15224#ifndef JC1_LITE
15225 for (i = 0; i < 11; ++i)
15226 ggc_mark_tree (pc->modifier_ctx[i]);
15227 ggc_mark_tree (pc->class_type);
15228 ggc_mark_tree (pc->function_decl);
15229 ggc_mark_tree (pc->package);
15230 ggc_mark_tree (pc->incomplete_class);
15231 ggc_mark_tree (pc->gclass_list);
15232 ggc_mark_tree (pc->class_list);
15233 ggc_mark_tree (pc->current_parsed_class);
15234 ggc_mark_tree (pc->current_parsed_class_un);
15235 ggc_mark_tree (pc->non_static_initialized);
15236 ggc_mark_tree (pc->static_initialized);
15237 ggc_mark_tree (pc->instance_initializers);
15238 ggc_mark_tree (pc->import_list);
15239 ggc_mark_tree (pc->import_demand_list);
15240 ggc_mark_tree (pc->current_loop);
15241 ggc_mark_tree (pc->current_labeled_block);
15242#endif /* JC1_LITE */
15243
15244 if (pc->next)
15245 mark_parser_ctxt (&pc->next);
15246}