]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
varray.h (VARRAY_PUSH_GENERIC_PTR, [...]): Fix macro argument typo.
[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"
e04a16fb 68
c2952b01
APB
69#ifndef DIR_SEPARATOR
70#define DIR_SEPARATOR '/'
71#endif
72
82371d41 73/* Local function prototypes */
df32d2ce
KG
74static char *java_accstring_lookup PARAMS ((int));
75static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
77static tree create_class PARAMS ((int, tree, tree, tree));
78static tree create_interface PARAMS ((int, tree, tree));
c2952b01 79static void end_class_declaration PARAMS ((int));
df32d2ce
KG
80static tree find_field PARAMS ((tree, tree));
81static tree lookup_field_wrapper PARAMS ((tree, tree));
82static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83static void register_fields PARAMS ((int, tree, tree));
c2952b01 84static tree parser_qualified_classname PARAMS ((int, tree));
df32d2ce
KG
85static int parser_check_super PARAMS ((tree, tree, tree));
86static int parser_check_super_interface PARAMS ((tree, tree, tree));
87static void check_modifiers_consistency PARAMS ((int));
88static tree lookup_cl PARAMS ((tree));
89static tree lookup_java_method2 PARAMS ((tree, tree, int));
90static tree method_header PARAMS ((int, tree, tree, tree));
91static void fix_method_argument_names PARAMS ((tree ,tree));
92static tree method_declarator PARAMS ((tree, tree));
93static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 94 ATTRIBUTE_PRINTF_2;
df32d2ce
KG
95static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
96static void parse_ctor_invocation_error PARAMS ((void));
97static tree parse_jdk1_1_error PARAMS ((const char *));
98static void complete_class_report_errors PARAMS ((jdep *));
99static int process_imports PARAMS ((void));
100static void read_import_dir PARAMS ((tree));
101static int find_in_imports_on_demand PARAMS ((tree));
102static int find_in_imports PARAMS ((tree));
103static int check_pkg_class_access PARAMS ((tree, tree));
104static tree resolve_package PARAMS ((tree, tree *));
105static tree lookup_package_type PARAMS ((const char *, int));
106static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 107static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
108static void declare_local_variables PARAMS ((int, tree, tree));
109static void source_start_java_method PARAMS ((tree));
110static void source_end_java_method PARAMS ((void));
111static void expand_start_java_method PARAMS ((tree));
112static tree find_name_in_single_imports PARAMS ((tree));
113static void check_abstract_method_header PARAMS ((tree));
114static tree lookup_java_interface_method2 PARAMS ((tree, tree));
115static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 116static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 117static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 118 tree, tree, tree));
df32d2ce 119static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 120 int *, tree *));
df32d2ce
KG
121static int breakdown_qualified PARAMS ((tree *, tree *, tree));
122static tree resolve_and_layout PARAMS ((tree, tree));
123static tree resolve_no_layout PARAMS ((tree, tree));
124static int invocation_mode PARAMS ((tree, int));
125static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 126 tree, tree));
df32d2ce 127static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 128 tree *, tree *));
df32d2ce
KG
129static tree find_most_specific_methods_list PARAMS ((tree));
130static int argument_types_convertible PARAMS ((tree, tree));
131static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 132static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
133static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
134static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
135static tree obtain_incomplete_type PARAMS ((tree));
136static tree java_complete_lhs PARAMS ((tree));
137static tree java_complete_tree PARAMS ((tree));
c2952b01 138static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
92d83515 139static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
140static void java_complete_expand_method PARAMS ((tree));
141static int unresolved_type_p PARAMS ((tree, tree *));
142static void create_jdep_list PARAMS ((struct parser_ctxt *));
143static tree build_expr_block PARAMS ((tree, tree));
144static tree enter_block PARAMS ((void));
145static tree enter_a_block PARAMS ((tree));
146static tree exit_block PARAMS ((void));
147static tree lookup_name_in_blocks PARAMS ((tree));
148static void maybe_absorb_scoping_blocks PARAMS ((void));
149static tree build_method_invocation PARAMS ((tree, tree));
150static tree build_new_invocation PARAMS ((tree, tree));
151static tree build_assignment PARAMS ((int, int, tree, tree));
152static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
153static int check_final_assignment PARAMS ((tree ,tree));
154static tree patch_assignment PARAMS ((tree, tree, tree ));
155static tree patch_binop PARAMS ((tree, tree, tree));
156static tree build_unaryop PARAMS ((int, int, tree));
157static tree build_incdec PARAMS ((int, int, tree, int));
158static tree patch_unaryop PARAMS ((tree, tree));
159static tree build_cast PARAMS ((int, tree, tree));
160static tree build_null_of_type PARAMS ((tree));
161static tree patch_cast PARAMS ((tree, tree));
162static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
163static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
164static int valid_cast_to_p PARAMS ((tree, tree));
165static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
166static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
167static tree try_reference_assignconv PARAMS ((tree, tree));
168static tree build_unresolved_array_type PARAMS ((tree));
169static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
170static tree build_array_ref PARAMS ((int, tree, tree));
171static tree patch_array_ref PARAMS ((tree));
172static tree make_qualified_name PARAMS ((tree, tree, int));
173static tree merge_qualified_name PARAMS ((tree, tree));
174static tree make_qualified_primary PARAMS ((tree, tree, int));
175static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 176 tree *, tree *));
df32d2ce 177static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
178static tree resolve_field_access PARAMS ((tree, tree *, tree *));
179static tree build_newarray_node PARAMS ((tree, tree, int));
180static tree patch_newarray PARAMS ((tree));
181static tree resolve_type_during_patch PARAMS ((tree));
182static tree build_this PARAMS ((int));
c2952b01 183static tree build_wfl_wrap PARAMS ((tree));
df32d2ce
KG
184static tree build_return PARAMS ((int, tree));
185static tree patch_return PARAMS ((tree));
186static tree maybe_access_field PARAMS ((tree, tree, tree));
187static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
188static int check_for_static_method_reference PARAMS ((tree, tree, tree,
189 tree, tree));
df32d2ce
KG
190static int not_accessible_p PARAMS ((tree, tree, int));
191static void check_deprecation PARAMS ((tree, tree));
192static int class_in_current_package PARAMS ((tree));
193static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
194static tree patch_if_else_statement PARAMS ((tree));
195static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
196static tree add_stmt_to_block PARAMS ((tree, tree, tree));
197static tree patch_exit_expr PARAMS ((tree));
198static tree build_labeled_block PARAMS ((int, tree));
199static tree finish_labeled_statement PARAMS ((tree, tree));
200static tree build_bc_statement PARAMS ((int, int, tree));
201static tree patch_bc_statement PARAMS ((tree));
202static tree patch_loop_statement PARAMS ((tree));
203static tree build_new_loop PARAMS ((tree));
204static tree build_loop_body PARAMS ((int, tree, int));
205static tree finish_loop_body PARAMS ((int, tree, tree, int));
206static tree build_debugable_stmt PARAMS ((int, tree));
207static tree finish_for_loop PARAMS ((int, tree, tree, tree));
208static tree patch_switch_statement PARAMS ((tree));
209static tree string_constant_concatenation PARAMS ((tree, tree));
210static tree build_string_concatenation PARAMS ((tree, tree));
211static tree patch_string_cst PARAMS ((tree));
212static tree patch_string PARAMS ((tree));
213static tree build_try_statement PARAMS ((int, tree, tree));
214static tree build_try_finally_statement PARAMS ((int, tree, tree));
215static tree patch_try_statement PARAMS ((tree));
216static tree patch_synchronized_statement PARAMS ((tree, tree));
217static tree patch_throw_statement PARAMS ((tree, tree));
218static void check_thrown_exceptions PARAMS ((int, tree));
219static int check_thrown_exceptions_do PARAMS ((tree));
220static void purge_unchecked_exceptions PARAMS ((tree));
221static void check_throws_clauses PARAMS ((tree, tree, tree));
222static void finish_method_declaration PARAMS ((tree));
223static tree build_super_invocation PARAMS ((tree));
224static int verify_constructor_circularity PARAMS ((tree, tree));
225static char *constructor_circularity_msg PARAMS ((tree, tree));
226static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 227 int, int));
df32d2ce
KG
228static const char *get_printable_method_name PARAMS ((tree));
229static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
230static tree generate_finit PARAMS ((tree));
231static void add_instance_initializer PARAMS ((tree));
df32d2ce 232static void fix_constructors PARAMS ((tree));
c2952b01
APB
233static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
234 tree, int *));
235static void craft_constructor PARAMS ((tree, tree));
236static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
237static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
238static void start_artificial_method_body PARAMS ((tree));
239static void end_artificial_method_body PARAMS ((tree));
240static int check_method_redefinition PARAMS ((tree, tree));
241static int reset_method_name PARAMS ((tree));
165f37bc 242static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
243static void java_check_regular_methods PARAMS ((tree));
244static void java_check_abstract_methods PARAMS ((tree));
245static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
246static void unreachable_stmt_error PARAMS ((tree));
247static tree find_expr_with_wfl PARAMS ((tree));
248static void missing_return_error PARAMS ((tree));
249static tree build_new_array_init PARAMS ((int, tree));
250static tree patch_new_array_init PARAMS ((tree, tree));
251static tree maybe_build_array_element_wfl PARAMS ((tree));
252static int array_constructor_check_entry PARAMS ((tree, tree));
253static const char *purify_type_name PARAMS ((const char *));
254static tree fold_constant_for_init PARAMS ((tree, tree));
255static tree strip_out_static_field_access_decl PARAMS ((tree));
256static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
257static void static_ref_err PARAMS ((tree, tree, tree));
258static void parser_add_interface PARAMS ((tree, tree, tree));
259static void add_superinterfaces PARAMS ((tree, tree));
260static tree jdep_resolve_class PARAMS ((jdep *));
261static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
262static void java_complete_expand_classes PARAMS ((void));
263static void java_complete_expand_class PARAMS ((tree));
264static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
265static tree cut_identifier_in_qualified PARAMS ((tree));
266static tree java_stabilize_reference PARAMS ((tree));
267static tree do_unary_numeric_promotion PARAMS ((tree));
268static char * operator_string PARAMS ((tree));
269static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
270static tree merge_string_cste PARAMS ((tree, tree, int));
271static tree java_refold PARAMS ((tree));
272static int java_decl_equiv PARAMS ((tree, tree));
273static int binop_compound_p PARAMS ((enum tree_code));
274static tree search_loop PARAMS ((tree));
275static int labeled_block_contains_loop_p PARAMS ((tree, tree));
276static void check_abstract_method_definitions PARAMS ((int, tree, tree));
277static void java_check_abstract_method_definitions PARAMS ((tree));
278static void java_debug_context_do PARAMS ((int));
c2952b01
APB
279static void java_parser_context_push_initialized_field PARAMS ((void));
280static void java_parser_context_pop_initialized_field PARAMS ((void));
281static tree reorder_static_initialized PARAMS ((tree));
282static void java_parser_context_suspend PARAMS ((void));
283static void java_parser_context_resume PARAMS ((void));
284
285/* JDK 1.1 work. FIXME */
286
287static tree maybe_make_nested_class_name PARAMS ((tree));
288static void make_nested_class_name PARAMS ((tree));
289static void set_nested_class_simple_name_value PARAMS ((tree, int));
290static void link_nested_class_to_enclosing PARAMS ((void));
291static tree find_as_inner_class PARAMS ((tree, tree, tree));
292static tree find_as_inner_class_do PARAMS ((tree, tree));
293static int check_inner_class_redefinition PARAMS ((tree, tree));
294
295static tree build_thisn_assign PARAMS ((void));
296static tree build_current_thisn PARAMS ((tree));
297static tree build_access_to_thisn PARAMS ((tree, tree, int));
298static tree maybe_build_thisn_access_method PARAMS ((tree));
299
300static tree build_outer_field_access PARAMS ((tree, tree));
301static tree build_outer_field_access_methods PARAMS ((tree));
302static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
303 tree, tree));
304static tree build_outer_method_access_method PARAMS ((tree));
305static tree build_new_access_id PARAMS ((void));
306static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
307 tree, tree));
308
309static int outer_field_access_p PARAMS ((tree, tree));
310static int outer_field_expanded_access_p PARAMS ((tree, tree *,
311 tree *, tree *));
312static tree outer_field_access_fix PARAMS ((tree, tree, tree));
313static tree build_incomplete_class_ref PARAMS ((int, tree));
314static tree patch_incomplete_class_ref PARAMS ((tree));
315static tree create_anonymous_class PARAMS ((int, tree));
316static void patch_anonymous_class PARAMS ((tree, tree, tree));
317static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 318
165f37bc
APB
319static tree build_dot_class_method PARAMS ((tree));
320static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 321static void create_new_parser_context PARAMS ((int));
165f37bc 322
e04a16fb
AG
323/* Number of error found so far. */
324int java_error_count;
325/* Number of warning found so far. */
326int java_warning_count;
ce6e9147
APB
327/* Tell when not to fold, when doing xrefs */
328int do_not_fold;
c2952b01
APB
329/* Cyclic inheritance report, as it can be set by layout_class */
330char *cyclic_inheritance_report;
331
332/* Tell when we're within an instance initializer */
333static int in_instance_initializer;
e04a16fb
AG
334
335/* The current parser context */
d4370213 336struct parser_ctxt *ctxp;
e04a16fb 337
d4370213 338/* List of things that were analyzed for which code will be generated */
b351b287
APB
339static struct parser_ctxt *ctxp_for_generation = NULL;
340
e04a16fb
AG
341/* binop_lookup maps token to tree_code. It is used where binary
342 operations are involved and required by the parser. RDIV_EXPR
343 covers both integral/floating point division. The code is changed
344 once the type of both operator is worked out. */
345
346static enum tree_code binop_lookup[19] =
347 {
348 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
349 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
350 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
351 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
352 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
353 };
354#define BINOP_LOOKUP(VALUE) \
355 binop_lookup [((VALUE) - PLUS_TK)% \
356 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
357
5cbdba64
APB
358/* This is the end index for binary operators that can also be used
359 in compound assignements. */
360#define BINOP_COMPOUND_CANDIDATES 11
361
e04a16fb
AG
362/* Fake WFL used to report error message. It is initialized once if
363 needed and reused with it's location information is overriden. */
15fdcfe9 364tree wfl_operator = NULL_TREE;
e04a16fb
AG
365
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
APB
387
388/* Context and flag for static blocks */
389static tree current_static_block = NULL_TREE;
390
c2952b01
APB
391/* The generated `write_parm_value$' identifier. */
392static tree wpv_id;
393
ee07f4f4
APB
394/* The list of all packages we've seen so far */
395static tree package_list = NULL_TREE;
2884c41e
KG
396
397/* Check modifiers. If one doesn't fit, retrieve it in its declaration
398 line and point it out. */
399/* Should point out the one that don't fit. ASCII/unicode, going
400 backward. FIXME */
401
402#define check_modifiers(__message, __value, __mask) do { \
403 if ((__value) & ~(__mask)) \
404 { \
405 int i, remainder = (__value) & ~(__mask); \
406 for (i = 0; i <= 10; i++) \
407 if ((1 << i) & remainder) \
408 parse_error_context (ctxp->modifier_ctx [i], (__message), \
409 java_accstring_lookup (1 << i)); \
410 } \
411} while (0)
ee07f4f4 412
e04a16fb
AG
413%}
414
415%union {
416 tree node;
417 int sub_token;
418 struct {
419 int token;
420 int location;
421 } operator;
422 int value;
423}
424
9ee9b555
KG
425%{
426#include "lex.c"
427%}
428
e04a16fb
AG
429%pure_parser
430
431/* Things defined here have to match the order of what's in the
432 binop_lookup table. */
433
434%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
435%token LS_TK SRS_TK ZRS_TK
436%token AND_TK XOR_TK OR_TK
437%token BOOL_AND_TK BOOL_OR_TK
438%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
439
440/* This maps to the same binop_lookup entry than the token above */
441
442%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
443%token REM_ASSIGN_TK
444%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
445%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
446
447
448/* Modifier TOKEN have to be kept in this order. Don't scramble it */
449
450%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
451%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
452%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
453%token PAD_TK ABSTRACT_TK MODIFIER_TK
454
455/* Keep those two in order, too */
456%token DECR_TK INCR_TK
457
458/* From now one, things can be in any order */
459
460%token DEFAULT_TK IF_TK THROW_TK
461%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
462%token THROWS_TK BREAK_TK IMPORT_TK
463%token ELSE_TK INSTANCEOF_TK RETURN_TK
464%token VOID_TK CATCH_TK INTERFACE_TK
465%token CASE_TK EXTENDS_TK FINALLY_TK
466%token SUPER_TK WHILE_TK CLASS_TK
467%token SWITCH_TK CONST_TK TRY_TK
468%token FOR_TK NEW_TK CONTINUE_TK
469%token GOTO_TK PACKAGE_TK THIS_TK
470
471%token BYTE_TK SHORT_TK INT_TK LONG_TK
472%token CHAR_TK INTEGRAL_TK
473
474%token FLOAT_TK DOUBLE_TK FP_TK
475
476%token ID_TK
477
478%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
479
480%token ASSIGN_ANY_TK ASSIGN_TK
481%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
482
483%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
484%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
485
c2952b01 486%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
487
488%type <node> super ID_TK identifier
489%type <node> name simple_name qualified_name
c2952b01 490%type <node> type_declaration compilation_unit
e04a16fb
AG
491 field_declaration method_declaration extends_interfaces
492 interfaces interface_type_list
c2952b01 493 class_member_declaration
e04a16fb
AG
494 import_declarations package_declaration
495 type_declarations interface_body
496 interface_member_declaration constant_declaration
497 interface_member_declarations interface_type
498 abstract_method_declaration interface_type_list
499%type <node> class_body_declaration class_member_declaration
500 static_initializer constructor_declaration block
22eed1e6 501%type <node> class_body_declarations constructor_header
e04a16fb
AG
502%type <node> class_or_interface_type class_type class_type_list
503 constructor_declarator explicit_constructor_invocation
b9f7e36c 504%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
505
506%type <node> variable_declarator_id variable_declarator
507 variable_declarators variable_initializer
22eed1e6 508 variable_initializers constructor_body
ac825856 509 array_initializer
e04a16fb 510
2e5eb5c5 511%type <node> class_body block_end constructor_block_end
e04a16fb
AG
512%type <node> statement statement_without_trailing_substatement
513 labeled_statement if_then_statement label_decl
514 if_then_else_statement while_statement for_statement
515 statement_nsi labeled_statement_nsi do_statement
516 if_then_else_statement_nsi while_statement_nsi
517 for_statement_nsi statement_expression_list for_init
518 for_update statement_expression expression_statement
519 primary_no_new_array expression primary
520 array_creation_expression array_type
521 class_instance_creation_expression field_access
522 method_invocation array_access something_dot_new
523 argument_list postfix_expression while_expression
524 post_increment_expression post_decrement_expression
525 unary_expression_not_plus_minus unary_expression
526 pre_increment_expression pre_decrement_expression
527 unary_expression_not_plus_minus cast_expression
528 multiplicative_expression additive_expression
529 shift_expression relational_expression
530 equality_expression and_expression
531 exclusive_or_expression inclusive_or_expression
532 conditional_and_expression conditional_or_expression
533 conditional_expression assignment_expression
534 left_hand_side assignment for_header for_begin
535 constant_expression do_statement_begin empty_statement
b67d701b 536 switch_statement synchronized_statement throw_statement
f8976021 537 try_statement switch_expression switch_block
15fdcfe9 538 catches catch_clause catch_clause_parameter finally
c2952b01 539 anonymous_class_creation
e04a16fb
AG
540%type <node> return_statement break_statement continue_statement
541
542%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
543%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
544%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
545%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
546%type <operator> ASSIGN_ANY_TK assignment_operator
547%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
548%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
549%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 550%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 551%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
552%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
553%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 554%type <operator> NEW_TK
e04a16fb
AG
555
556%type <node> method_body
557
558%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
559 STRING_LIT_TK NULL_TK VOID_TK
560
561%type <node> IF_TK WHILE_TK FOR_TK
562
563%type <node> formal_parameter_list formal_parameter
564 method_declarator method_header
565
c2952b01 566%type <node> primitive_type reference_type type
e04a16fb
AG
567 BOOLEAN_TK INTEGRAL_TK FP_TK
568
c2952b01
APB
569/* Added or modified JDK 1.1 rule types */
570%type <node> type_literals array_type_literal
571
e04a16fb
AG
572%%
573/* 19.2 Production from 2.3: The Syntactic Grammar */
574goal:
575 compilation_unit
576 {}
577;
578
579/* 19.3 Productions from 3: Lexical structure */
580literal:
581 INT_LIT_TK
582| FP_LIT_TK
583| BOOL_LIT_TK
584| CHAR_LIT_TK
585| STRING_LIT_TK
586| NULL_TK
587;
588
589/* 19.4 Productions from 4: Types, Values and Variables */
590type:
591 primitive_type
592| reference_type
593;
594
595primitive_type:
596 INTEGRAL_TK
597| FP_TK
598| BOOLEAN_TK
599;
600
601reference_type:
602 class_or_interface_type
603| array_type
604;
605
606class_or_interface_type:
607 name
608;
609
610class_type:
611 class_or_interface_type /* Default rule */
612;
613
614interface_type:
615 class_or_interface_type
616;
617
618array_type:
619 primitive_type OSB_TK CSB_TK
620 {
621 $$ = build_java_array_type ($1, -1);
622 CLASS_LOADED_P ($$) = 1;
623 }
624| name OSB_TK CSB_TK
625 { $$ = build_unresolved_array_type ($1); }
626| array_type OSB_TK CSB_TK
627 { $$ = build_unresolved_array_type ($1); }
628| primitive_type OSB_TK error
629 {RULE ("']' expected"); RECOVER;}
630| array_type OSB_TK error
631 {RULE ("']' expected"); RECOVER;}
632;
633
634/* 19.5 Productions from 6: Names */
635name:
636 simple_name /* Default rule */
637| qualified_name /* Default rule */
638;
639
640simple_name:
641 identifier /* Default rule */
642;
643
644qualified_name:
645 name DOT_TK identifier
646 { $$ = make_qualified_name ($1, $3, $2.location); }
647;
648
649identifier:
650 ID_TK
651;
652
653/* 19.6: Production from 7: Packages */
654compilation_unit:
655 {$$ = NULL;}
656| package_declaration
657| import_declarations
658| type_declarations
659| package_declaration import_declarations
660| package_declaration type_declarations
661| import_declarations type_declarations
662| package_declaration import_declarations type_declarations
663;
664
665import_declarations:
666 import_declaration
667 {
668 $$ = NULL;
669 }
670| import_declarations import_declaration
671 {
672 $$ = NULL;
673 }
674;
675
676type_declarations:
677 type_declaration
678| type_declarations type_declaration
679;
680
681package_declaration:
682 PACKAGE_TK name SC_TK
ee07f4f4
APB
683 {
684 ctxp->package = EXPR_WFL_NODE ($2);
685 package_list = tree_cons (ctxp->package, NULL, package_list);
686 }
e04a16fb
AG
687| PACKAGE_TK error
688 {yyerror ("Missing name"); RECOVER;}
689| PACKAGE_TK name error
690 {yyerror ("';' expected"); RECOVER;}
691;
692
693import_declaration:
694 single_type_import_declaration
695| type_import_on_demand_declaration
696;
697
698single_type_import_declaration:
699 IMPORT_TK name SC_TK
700 {
701 tree name = EXPR_WFL_NODE ($2), node, last_name;
702 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 703 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
704 while (last != IDENTIFIER_POINTER (name))
705 {
706 if (last [0] == '.')
707 break;
708 last--;
709 }
710 last_name = get_identifier (++last);
711 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
712 {
713 tree err = find_name_in_single_imports (last_name);
714 if (err && err != name)
715 parse_error_context
716 ($2, "Ambiguous class: `%s' and `%s'",
717 IDENTIFIER_POINTER (name),
718 IDENTIFIER_POINTER (err));
5e942c50
APB
719 else
720 REGISTER_IMPORT ($2, last_name)
e04a16fb
AG
721 }
722 else
5e942c50 723 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
724 }
725| IMPORT_TK error
726 {yyerror ("Missing name"); RECOVER;}
727| IMPORT_TK name error
728 {yyerror ("';' expected"); RECOVER;}
729;
730
731type_import_on_demand_declaration:
732 IMPORT_TK name DOT_TK MULT_TK SC_TK
733 {
734 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
735 /* Don't import java.lang.* twice. */
736 if (name != java_lang_id)
737 {
738 tree node = build_tree_list ($2, NULL_TREE);
739 read_import_dir ($2);
740 TREE_CHAIN (node) = ctxp->import_demand_list;
741 ctxp->import_demand_list = node;
742 }
e04a16fb
AG
743 }
744| IMPORT_TK name DOT_TK error
745 {yyerror ("'*' expected"); RECOVER;}
746| IMPORT_TK name DOT_TK MULT_TK error
747 {yyerror ("';' expected"); RECOVER;}
748;
749
750type_declaration:
751 class_declaration
c2952b01 752 { end_class_declaration (0); }
e04a16fb 753| interface_declaration
c2952b01 754 { end_class_declaration (0); }
e04a16fb
AG
755| SC_TK
756 { $$ = NULL; }
757| error
758 {
759 YYERROR_NOW;
760 yyerror ("Class or interface declaration expected");
761 }
762;
763
764/* 19.7 Shortened from the original:
765 modifiers: modifier | modifiers modifier
766 modifier: any of public... */
767modifiers:
768 MODIFIER_TK
769 {
770 $$ = (1 << $1);
771 }
772| modifiers MODIFIER_TK
773 {
774 int acc = (1 << $2);
775 if ($$ & acc)
776 parse_error_context
777 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
778 java_accstring_lookup (acc));
779 else
780 {
781 $$ |= acc;
782 }
783 }
784;
785
786/* 19.8.1 Production from $8.1: Class Declaration */
787class_declaration:
788 modifiers CLASS_TK identifier super interfaces
789 { create_class ($1, $3, $4, $5); }
790 class_body
e04a16fb
AG
791| CLASS_TK identifier super interfaces
792 { create_class (0, $2, $3, $4); }
793 class_body
e04a16fb
AG
794| modifiers CLASS_TK error
795 {yyerror ("Missing class name"); RECOVER;}
796| CLASS_TK error
797 {yyerror ("Missing class name"); RECOVER;}
798| CLASS_TK identifier error
0b4d333e
APB
799 {
800 if (!ctxp->class_err) yyerror ("'{' expected");
801 DRECOVER(class1);
802 }
e04a16fb
AG
803| modifiers CLASS_TK identifier error
804 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
805;
806
807super:
808 { $$ = NULL; }
809| EXTENDS_TK class_type
810 { $$ = $2; }
811| EXTENDS_TK class_type error
812 {yyerror ("'{' expected"); ctxp->class_err=1;}
813| EXTENDS_TK error
814 {yyerror ("Missing super class name"); ctxp->class_err=1;}
815;
816
817interfaces:
818 { $$ = NULL_TREE; }
819| IMPLEMENTS_TK interface_type_list
820 { $$ = $2; }
821| IMPLEMENTS_TK error
822 {
823 ctxp->class_err=1;
824 yyerror ("Missing interface name");
825 }
826;
827
828interface_type_list:
829 interface_type
830 {
831 ctxp->interface_number = 1;
832 $$ = build_tree_list ($1, NULL_TREE);
833 }
834| interface_type_list C_TK interface_type
835 {
836 ctxp->interface_number++;
837 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
838 }
839| interface_type_list C_TK error
840 {yyerror ("Missing interface name"); RECOVER;}
841;
842
843class_body:
844 OCB_TK CCB_TK
7f10c2e2
APB
845 {
846 /* Store the location of the `}' when doing xrefs */
847 if (flag_emit_xref)
c2952b01 848 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 849 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 850 $$ = GET_CPC ();
7f10c2e2 851 }
e04a16fb 852| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
853 {
854 /* Store the location of the `}' when doing xrefs */
855 if (flag_emit_xref)
c2952b01 856 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 857 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 858 $$ = GET_CPC ();
7f10c2e2 859 }
e04a16fb
AG
860;
861
862class_body_declarations:
863 class_body_declaration
864| class_body_declarations class_body_declaration
865;
866
867class_body_declaration:
868 class_member_declaration
869| static_initializer
870| constructor_declaration
871| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
872 {
873 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
874 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
875 }
e04a16fb
AG
876;
877
878class_member_declaration:
879 field_declaration
0b4d333e
APB
880| field_declaration SC_TK
881 { $$ = $1; }
e04a16fb
AG
882| method_declaration
883| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
884 { end_class_declaration (1); }
885| interface_declaration /* Added, JDK1.1 inner interfaces */
886 { end_class_declaration (1); }
e04a16fb
AG
887;
888
889/* 19.8.2 Productions from 8.3: Field Declarations */
890field_declaration:
891 type variable_declarators SC_TK
892 { register_fields (0, $1, $2); }
893| modifiers type variable_declarators SC_TK
894 {
e04a16fb
AG
895 check_modifiers
896 ("Illegal modifier `%s' for field declaration",
897 $1, FIELD_MODIFIERS);
898 check_modifiers_consistency ($1);
899 register_fields ($1, $2, $3);
900 }
901;
902
903variable_declarators:
904 /* Should we use build_decl_list () instead ? FIXME */
905 variable_declarator /* Default rule */
906| variable_declarators C_TK variable_declarator
907 { $$ = chainon ($1, $3); }
908| variable_declarators C_TK error
909 {yyerror ("Missing term"); RECOVER;}
910;
911
912variable_declarator:
913 variable_declarator_id
914 { $$ = build_tree_list ($1, NULL_TREE); }
915| variable_declarator_id ASSIGN_TK variable_initializer
916 {
917 if (java_error_count)
918 $3 = NULL_TREE;
919 $$ = build_tree_list
920 ($1, build_assignment ($2.token, $2.location, $1, $3));
921 }
922| variable_declarator_id ASSIGN_TK error
923 {
924 yyerror ("Missing variable initializer");
925 $$ = build_tree_list ($1, NULL_TREE);
926 RECOVER;
927 }
928| variable_declarator_id ASSIGN_TK variable_initializer error
929 {
930 yyerror ("';' expected");
931 $$ = build_tree_list ($1, NULL_TREE);
932 RECOVER;
933 }
934;
935
936variable_declarator_id:
937 identifier
938| variable_declarator_id OSB_TK CSB_TK
c583dd46 939 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
940| identifier error
941 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
942| variable_declarator_id OSB_TK error
943 {yyerror ("']' expected"); DRECOVER(vdi);}
944| variable_declarator_id CSB_TK error
945 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
946;
947
948variable_initializer:
949 expression
950| array_initializer
e04a16fb
AG
951;
952
953/* 19.8.3 Productions from 8.4: Method Declarations */
954method_declaration:
955 method_header
956 {
957 current_function_decl = $1;
c2952b01
APB
958 if (current_function_decl
959 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
960 source_start_java_method (current_function_decl);
961 else
962 current_function_decl = NULL_TREE;
e04a16fb
AG
963 }
964 method_body
b635eb2f 965 { finish_method_declaration ($3); }
e04a16fb
AG
966| method_header error
967 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
968;
969
970method_header:
971 type method_declarator throws
b9f7e36c 972 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 973| VOID_TK method_declarator throws
b9f7e36c 974 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 975| modifiers type method_declarator throws
b9f7e36c 976 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 977| modifiers VOID_TK method_declarator throws
b9f7e36c 978 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 979| type error
efa0a23f
APB
980 {
981 yyerror ("Invalid method declaration, method name required");
982 RECOVER;
983 }
e04a16fb
AG
984| modifiers type error
985 {RECOVER;}
986| VOID_TK error
987 {yyerror ("Identifier expected"); RECOVER;}
988| modifiers VOID_TK error
989 {yyerror ("Identifier expected"); RECOVER;}
990| modifiers error
991 {
992 yyerror ("Invalid method declaration, return type required");
993 RECOVER;
994 }
995;
996
997method_declarator:
998 identifier OP_TK CP_TK
c2952b01
APB
999 {
1000 ctxp->formal_parameter_number = 0;
1001 $$ = method_declarator ($1, NULL_TREE);
1002 }
e04a16fb
AG
1003| identifier OP_TK formal_parameter_list CP_TK
1004 { $$ = method_declarator ($1, $3); }
1005| method_declarator OSB_TK CSB_TK
1006 {
1886c9d8
APB
1007 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1008 TREE_PURPOSE ($1) =
1009 build_unresolved_array_type (TREE_PURPOSE ($1));
1010 parse_warning_context
1011 (wfl_operator,
1012 "Discouraged form of returned type specification");
e04a16fb
AG
1013 }
1014| identifier OP_TK error
1015 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1016| method_declarator OSB_TK error
1017 {yyerror ("']' expected"); RECOVER;}
1018;
1019
1020formal_parameter_list:
1021 formal_parameter
1022 {
1023 ctxp->formal_parameter_number = 1;
1024 }
1025| formal_parameter_list C_TK formal_parameter
1026 {
1027 ctxp->formal_parameter_number += 1;
1028 $$ = chainon ($1, $3);
1029 }
1030| formal_parameter_list C_TK error
c2952b01 1031 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1032;
1033
1034formal_parameter:
1035 type variable_declarator_id
1036 {
1037 $$ = build_tree_list ($2, $1);
1038 }
18990de5 1039| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1040 {
5256aa37 1041 $$ = build_tree_list ($3, $2);
c2952b01 1042 ARG_FINAL_P ($$) = 1;
5256aa37 1043 }
e04a16fb 1044| type error
f8989a66
APB
1045 {
1046 yyerror ("Missing identifier"); RECOVER;
1047 $$ = NULL_TREE;
1048 }
18990de5 1049| final type error
e04a16fb 1050 {
e04a16fb 1051 yyerror ("Missing identifier"); RECOVER;
f8989a66 1052 $$ = NULL_TREE;
e04a16fb
AG
1053 }
1054;
1055
18990de5
JB
1056final:
1057 modifiers
1058 {
1059 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1060 $1, ACC_FINAL);
1061 if ($1 != ACC_FINAL)
1062 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1063 }
1064;
1065
e04a16fb 1066throws:
b9f7e36c 1067 { $$ = NULL_TREE; }
e04a16fb 1068| THROWS_TK class_type_list
b9f7e36c 1069 { $$ = $2; }
e04a16fb
AG
1070| THROWS_TK error
1071 {yyerror ("Missing class type term"); RECOVER;}
1072;
1073
1074class_type_list:
1075 class_type
c877974e 1076 { $$ = build_tree_list ($1, $1); }
e04a16fb 1077| class_type_list C_TK class_type
c877974e 1078 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1079| class_type_list C_TK error
1080 {yyerror ("Missing class type term"); RECOVER;}
1081;
1082
1083method_body:
1084 block
1085| block SC_TK
1086| SC_TK
1087 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
1088;
1089
1090/* 19.8.4 Productions from 8.5: Static Initializers */
1091static_initializer:
1092 static block
1093 {
c2952b01
APB
1094 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1095 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1096 }
1097| static block SC_TK /* Shouldn't be here. FIXME */
1098 {
c2952b01
APB
1099 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1100 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1101 }
1102;
1103
1104static: /* Test lval.sub_token here */
c2952b01 1105 modifiers
e04a16fb 1106 {
c2952b01
APB
1107 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1108 /* Can't have a static initializer in an innerclass */
1109 if ($1 | ACC_STATIC &&
1110 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1111 parse_error_context
1112 (MODIFIER_WFL (STATIC_TK),
1113 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1114 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1115 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1116 }
1117;
1118
1119/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1120constructor_declaration:
22eed1e6 1121 constructor_header
e04a16fb 1122 {
22eed1e6
APB
1123 current_function_decl = $1;
1124 source_start_java_method (current_function_decl);
e04a16fb 1125 }
22eed1e6 1126 constructor_body
b635eb2f 1127 { finish_method_declaration ($3); }
22eed1e6
APB
1128;
1129
1130constructor_header:
1131 constructor_declarator throws
1132 { $$ = method_header (0, NULL_TREE, $1, $2); }
1133| modifiers constructor_declarator throws
1134 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1135;
1136
1137constructor_declarator:
1138 simple_name OP_TK CP_TK
c2952b01
APB
1139 {
1140 ctxp->formal_parameter_number = 0;
1141 $$ = method_declarator ($1, NULL_TREE);
1142 }
e04a16fb 1143| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1144 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1145;
1146
1147constructor_body:
22eed1e6
APB
1148 /* Unlike regular method, we always need a complete (empty)
1149 body so we can safely perform all the required code
1150 addition (super invocation and field initialization) */
2e5eb5c5 1151 block_begin constructor_block_end
22eed1e6 1152 {
9bbc7d9f 1153 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1154 $$ = $2;
1155 }
2e5eb5c5 1156| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1157 { $$ = $3; }
2e5eb5c5 1158| block_begin block_statements constructor_block_end
22eed1e6 1159 { $$ = $3; }
2e5eb5c5 1160| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1161 { $$ = $4; }
e04a16fb
AG
1162;
1163
2e5eb5c5
APB
1164constructor_block_end:
1165 block_end
1166| block_end SC_TK
1167
e04a16fb
AG
1168/* Error recovery for that rule moved down expression_statement: rule. */
1169explicit_constructor_invocation:
1170 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1171 {
1172 $$ = build_method_invocation ($1, NULL_TREE);
1173 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1174 $$ = java_method_add_stmt (current_function_decl, $$);
1175 }
e04a16fb 1176| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1177 {
1178 $$ = build_method_invocation ($1, $3);
1179 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1180 $$ = java_method_add_stmt (current_function_decl, $$);
1181 }
e04a16fb
AG
1182 /* Added, JDK1.1 inner classes. Modified because the rule
1183 'primary' couldn't work. */
1184| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1185 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1186| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1187 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1188;
1189
1190this_or_super: /* Added, simplifies error diagnostics */
1191 THIS_TK
1192 {
9ee9b555 1193 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1194 EXPR_WFL_LINECOL (wfl) = $1.location;
1195 $$ = wfl;
1196 }
1197| SUPER_TK
1198 {
9ee9b555 1199 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1200 EXPR_WFL_LINECOL (wfl) = $1.location;
1201 $$ = wfl;
1202 }
1203;
1204
1205/* 19.9 Productions from 9: Interfaces */
1206/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1207interface_declaration:
1208 INTERFACE_TK identifier
1209 { create_interface (0, $2, NULL_TREE); }
1210 interface_body
e04a16fb
AG
1211| modifiers INTERFACE_TK identifier
1212 { create_interface ($1, $3, NULL_TREE); }
1213 interface_body
e04a16fb
AG
1214| INTERFACE_TK identifier extends_interfaces
1215 { create_interface (0, $2, $3); }
1216 interface_body
e04a16fb
AG
1217| modifiers INTERFACE_TK identifier extends_interfaces
1218 { create_interface ($1, $3, $4); }
1219 interface_body
e04a16fb 1220| INTERFACE_TK identifier error
0b4d333e 1221 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1222| modifiers INTERFACE_TK identifier error
0b4d333e 1223 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1224;
1225
1226extends_interfaces:
1227 EXTENDS_TK interface_type
1228 {
1229 ctxp->interface_number = 1;
1230 $$ = build_tree_list ($2, NULL_TREE);
1231 }
1232| extends_interfaces C_TK interface_type
1233 {
1234 ctxp->interface_number++;
1235 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1236 }
1237| EXTENDS_TK error
1238 {yyerror ("Invalid interface type"); RECOVER;}
1239| extends_interfaces C_TK error
1240 {yyerror ("Missing term"); RECOVER;}
1241;
1242
1243interface_body:
1244 OCB_TK CCB_TK
1245 { $$ = NULL_TREE; }
1246| OCB_TK interface_member_declarations CCB_TK
1247 { $$ = NULL_TREE; }
1248;
1249
1250interface_member_declarations:
1251 interface_member_declaration
1252| interface_member_declarations interface_member_declaration
1253;
1254
1255interface_member_declaration:
1256 constant_declaration
1257| abstract_method_declaration
1258| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1259 { end_class_declaration (1); }
1260| interface_declaration /* Added, JDK1.1 inner interfaces */
1261 { end_class_declaration (1); }
e04a16fb
AG
1262;
1263
1264constant_declaration:
1265 field_declaration
1266;
1267
1268abstract_method_declaration:
1269 method_header SC_TK
1270 {
1271 check_abstract_method_header ($1);
1272 current_function_decl = NULL_TREE; /* FIXME ? */
1273 }
1274| method_header error
1275 {yyerror ("';' expected"); RECOVER;}
1276;
1277
1278/* 19.10 Productions from 10: Arrays */
1279array_initializer:
1280 OCB_TK CCB_TK
1179ebc2 1281 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1282| OCB_TK variable_initializers CCB_TK
f8976021 1283 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1284| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1285 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1286;
1287
1288variable_initializers:
1289 variable_initializer
f8976021
APB
1290 {
1291 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1292 $1, NULL_TREE);
1293 }
e04a16fb 1294| variable_initializers C_TK variable_initializer
1179ebc2
APB
1295 {
1296 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1297 }
e04a16fb
AG
1298| variable_initializers C_TK error
1299 {yyerror ("Missing term"); RECOVER;}
1300;
1301
1302/* 19.11 Production from 14: Blocks and Statements */
1303block:
1304 OCB_TK CCB_TK
7f10c2e2
APB
1305 {
1306 /* Store the location of the `}' when doing xrefs */
1307 if (current_function_decl && flag_emit_xref)
1308 DECL_END_SOURCE_LINE (current_function_decl) =
1309 EXPR_WFL_ADD_COL ($2.location, 1);
1310 $$ = empty_stmt_node;
1311 }
22eed1e6
APB
1312| block_begin block_statements block_end
1313 { $$ = $3; }
1314;
1315
1316block_begin:
1317 OCB_TK
e04a16fb 1318 { enter_block (); }
22eed1e6
APB
1319;
1320
1321block_end:
e04a16fb
AG
1322 CCB_TK
1323 {
1324 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1325 /* Store the location of the `}' when doing xrefs */
1326 if (current_function_decl && flag_emit_xref)
1327 DECL_END_SOURCE_LINE (current_function_decl) =
1328 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1329 $$ = exit_block ();
c280e37a
APB
1330 if (!BLOCK_SUBBLOCKS ($$))
1331 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1332 }
1333;
1334
1335block_statements:
1336 block_statement
1337| block_statements block_statement
1338;
1339
1340block_statement:
1341 local_variable_declaration_statement
1342| statement
15fdcfe9 1343 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1344| class_declaration /* Added, JDK1.1 local classes */
1345 {
1346 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1347 end_class_declaration (1);
1348 }
e04a16fb
AG
1349;
1350
1351local_variable_declaration_statement:
1352 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1353;
1354
1355local_variable_declaration:
1356 type variable_declarators
1357 { declare_local_variables (0, $1, $2); }
a003f638 1358| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1359 { declare_local_variables ($1, $2, $3); }
1360;
1361
1362statement:
1363 statement_without_trailing_substatement
1364| labeled_statement
e04a16fb 1365| if_then_statement
e04a16fb 1366| if_then_else_statement
e04a16fb 1367| while_statement
e04a16fb 1368| for_statement
cd9643f7 1369 { $$ = exit_block (); }
e04a16fb
AG
1370;
1371
1372statement_nsi:
1373 statement_without_trailing_substatement
1374| labeled_statement_nsi
e04a16fb 1375| if_then_else_statement_nsi
e04a16fb 1376| while_statement_nsi
e04a16fb 1377| for_statement_nsi
9dd939b2 1378 { $$ = exit_block (); }
e04a16fb
AG
1379;
1380
1381statement_without_trailing_substatement:
1382 block
e04a16fb 1383| empty_statement
e04a16fb 1384| expression_statement
e04a16fb 1385| switch_statement
e04a16fb 1386| do_statement
e04a16fb 1387| break_statement
e04a16fb 1388| continue_statement
e04a16fb
AG
1389| return_statement
1390| synchronized_statement
e04a16fb 1391| throw_statement
e04a16fb 1392| try_statement
e04a16fb
AG
1393;
1394
1395empty_statement:
1396 SC_TK
9bbc7d9f 1397 { $$ = empty_stmt_node; }
e04a16fb
AG
1398;
1399
1400label_decl:
1401 identifier REL_CL_TK
1402 {
1403 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1404 EXPR_WFL_NODE ($1));
e04a16fb
AG
1405 pushlevel (2);
1406 push_labeled_block ($$);
1407 PUSH_LABELED_BLOCK ($$);
1408 }
1409;
1410
1411labeled_statement:
1412 label_decl statement
b635eb2f 1413 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1414| identifier error
1415 {yyerror ("':' expected"); RECOVER;}
1416;
1417
1418labeled_statement_nsi:
1419 label_decl statement_nsi
b635eb2f 1420 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1421;
1422
1423/* We concentrate here a bunch of error handling rules that we couldn't write
1424 earlier, because expression_statement catches a missing ';'. */
1425expression_statement:
1426 statement_expression SC_TK
1427 {
1428 /* We have a statement. Generate a WFL around it so
1429 we can debug it */
1430 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1431 /* We know we have a statement, so set the debug
1432 info to be eventually generate here. */
1433 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1434 }
1435| error SC_TK
1436 {
1437 if (ctxp->prevent_ese != lineno)
1438 yyerror ("Invalid expression statement");
1439 DRECOVER (expr_stmt);
1440 }
1441| error OCB_TK
1442 {
1443 if (ctxp->prevent_ese != lineno)
1444 yyerror ("Invalid expression statement");
1445 DRECOVER (expr_stmt);
1446 }
1447| error CCB_TK
1448 {
1449 if (ctxp->prevent_ese != lineno)
1450 yyerror ("Invalid expression statement");
1451 DRECOVER (expr_stmt);
1452 }
1453| this_or_super OP_TK error
1454 {yyerror ("')' expected"); RECOVER;}
1455| this_or_super OP_TK CP_TK error
22eed1e6 1456 {
8119c720 1457 parse_ctor_invocation_error ();
22eed1e6
APB
1458 RECOVER;
1459 }
e04a16fb
AG
1460| this_or_super OP_TK argument_list error
1461 {yyerror ("')' expected"); RECOVER;}
1462| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1463 {
8119c720 1464 parse_ctor_invocation_error ();
22eed1e6
APB
1465 RECOVER;
1466 }
e04a16fb
AG
1467| name DOT_TK SUPER_TK error
1468 {yyerror ("'(' expected"); RECOVER;}
1469| name DOT_TK SUPER_TK OP_TK error
1470 {yyerror ("')' expected"); RECOVER;}
1471| name DOT_TK SUPER_TK OP_TK argument_list error
1472 {yyerror ("')' expected"); RECOVER;}
1473| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1474 {yyerror ("';' expected"); RECOVER;}
1475| name DOT_TK SUPER_TK OP_TK CP_TK error
1476 {yyerror ("';' expected"); RECOVER;}
1477;
1478
1479statement_expression:
1480 assignment
1481| pre_increment_expression
e04a16fb 1482| pre_decrement_expression
e04a16fb 1483| post_increment_expression
e04a16fb 1484| post_decrement_expression
e04a16fb
AG
1485| method_invocation
1486| class_instance_creation_expression
e04a16fb
AG
1487;
1488
1489if_then_statement:
1490 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1491 {
1492 $$ = build_if_else_statement ($2.location, $3,
1493 $5, NULL_TREE);
1494 }
e04a16fb
AG
1495| IF_TK error
1496 {yyerror ("'(' expected"); RECOVER;}
1497| IF_TK OP_TK error
1498 {yyerror ("Missing term"); RECOVER;}
1499| IF_TK OP_TK expression error
1500 {yyerror ("')' expected"); RECOVER;}
1501;
1502
1503if_then_else_statement:
1504 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1505 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1506;
1507
1508if_then_else_statement_nsi:
1509 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1510 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1511;
1512
1513switch_statement:
15fdcfe9
PB
1514 switch_expression
1515 {
1516 enter_block ();
1517 }
1518 switch_block
b67d701b 1519 {
15fdcfe9 1520 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1521 I.e. make the last statment also have its own
1522 COMPOUND_EXPR. */
15fdcfe9
PB
1523 maybe_absorb_scoping_blocks ();
1524 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1525 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1526 }
1527;
1528
1529switch_expression:
1530 SWITCH_TK OP_TK expression CP_TK
1531 {
1532 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1533 EXPR_WFL_LINECOL ($$) = $2.location;
1534 }
e04a16fb
AG
1535| SWITCH_TK error
1536 {yyerror ("'(' expected"); RECOVER;}
1537| SWITCH_TK OP_TK error
1538 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1539| SWITCH_TK OP_TK expression CP_TK error
1540 {yyerror ("'{' expected"); RECOVER;}
1541;
1542
f8976021
APB
1543/* Default assignment is there to avoid type node on switch_block
1544 node. */
1545
e04a16fb
AG
1546switch_block:
1547 OCB_TK CCB_TK
f8976021 1548 { $$ = NULL_TREE; }
e04a16fb 1549| OCB_TK switch_labels CCB_TK
f8976021 1550 { $$ = NULL_TREE; }
e04a16fb 1551| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1552 { $$ = NULL_TREE; }
e04a16fb 1553| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1554 { $$ = NULL_TREE; }
e04a16fb
AG
1555;
1556
1557switch_block_statement_groups:
1558 switch_block_statement_group
1559| switch_block_statement_groups switch_block_statement_group
1560;
1561
1562switch_block_statement_group:
15fdcfe9 1563 switch_labels block_statements
e04a16fb
AG
1564;
1565
e04a16fb
AG
1566switch_labels:
1567 switch_label
1568| switch_labels switch_label
1569;
1570
1571switch_label:
1572 CASE_TK constant_expression REL_CL_TK
b67d701b 1573 {
15fdcfe9
PB
1574 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1575 EXPR_WFL_LINECOL (lab) = $1.location;
1576 java_method_add_stmt (current_function_decl, lab);
b67d701b 1577 }
e04a16fb 1578| DEFAULT_TK REL_CL_TK
b67d701b 1579 {
15fdcfe9
PB
1580 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1581 EXPR_WFL_LINECOL (lab) = $1.location;
1582 java_method_add_stmt (current_function_decl, lab);
b67d701b 1583 }
e04a16fb
AG
1584| CASE_TK error
1585 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1586| CASE_TK constant_expression error
1587 {yyerror ("':' expected"); RECOVER;}
1588| DEFAULT_TK error
1589 {yyerror ("':' expected"); RECOVER;}
1590;
1591
1592while_expression:
1593 WHILE_TK OP_TK expression CP_TK
1594 {
1595 tree body = build_loop_body ($2.location, $3, 0);
1596 $$ = build_new_loop (body);
1597 }
1598;
1599
1600while_statement:
1601 while_expression statement
b635eb2f 1602 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1603| WHILE_TK error
1604 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1605| WHILE_TK OP_TK error
1606 {yyerror ("Missing term and ')' expected"); RECOVER;}
1607| WHILE_TK OP_TK expression error
1608 {yyerror ("')' expected"); RECOVER;}
1609;
1610
1611while_statement_nsi:
1612 while_expression statement_nsi
b635eb2f 1613 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1614;
1615
1616do_statement_begin:
1617 DO_TK
1618 {
1619 tree body = build_loop_body (0, NULL_TREE, 1);
1620 $$ = build_new_loop (body);
1621 }
1622 /* Need error handing here. FIXME */
1623;
1624
1625do_statement:
1626 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1627 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1628;
1629
1630for_statement:
1631 for_begin SC_TK expression SC_TK for_update CP_TK statement
b635eb2f 1632 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); }
e04a16fb
AG
1633| for_begin SC_TK SC_TK for_update CP_TK statement
1634 {
b635eb2f 1635 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1636 /* We have not condition, so we get rid of the EXIT_EXPR */
1637 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1638 empty_stmt_node;
e04a16fb
AG
1639 }
1640| for_begin SC_TK error
1641 {yyerror ("Invalid control expression"); RECOVER;}
1642| for_begin SC_TK expression SC_TK error
1643 {yyerror ("Invalid update expression"); RECOVER;}
1644| for_begin SC_TK SC_TK error
1645 {yyerror ("Invalid update expression"); RECOVER;}
1646;
1647
1648for_statement_nsi:
1649 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1650 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1651| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1652 {
b635eb2f 1653 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1654 /* We have not condition, so we get rid of the EXIT_EXPR */
1655 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1656 empty_stmt_node;
e04a16fb
AG
1657 }
1658;
1659
1660for_header:
1661 FOR_TK OP_TK
1662 {
1663 /* This scope defined for local variable that may be
1664 defined within the scope of the for loop */
1665 enter_block ();
1666 }
1667| FOR_TK error
1668 {yyerror ("'(' expected"); DRECOVER(for_1);}
1669| FOR_TK OP_TK error
1670 {yyerror ("Invalid init statement"); RECOVER;}
1671;
1672
1673for_begin:
1674 for_header for_init
1675 {
1676 /* We now declare the loop body. The loop is
1677 declared as a for loop. */
1678 tree body = build_loop_body (0, NULL_TREE, 0);
1679 $$ = build_new_loop (body);
c2952b01 1680 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1681 /* The loop is added to the current block the for
1682 statement is defined within */
1683 java_method_add_stmt (current_function_decl, $$);
1684 }
1685;
1686for_init: /* Can be empty */
9bbc7d9f 1687 { $$ = empty_stmt_node; }
e04a16fb
AG
1688| statement_expression_list
1689 {
1690 /* Init statement recorded within the previously
1691 defined block scope */
1692 $$ = java_method_add_stmt (current_function_decl, $1);
1693 }
1694| local_variable_declaration
1695 {
1696 /* Local variable are recorded within the previously
1697 defined block scope */
1698 $$ = NULL_TREE;
1699 }
1700| statement_expression_list error
1701 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1702;
1703
1704for_update: /* Can be empty */
9bbc7d9f 1705 {$$ = empty_stmt_node;}
e04a16fb
AG
1706| statement_expression_list
1707 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1708;
1709
1710statement_expression_list:
1711 statement_expression
1712 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1713| statement_expression_list C_TK statement_expression
1714 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1715| statement_expression_list C_TK error
1716 {yyerror ("Missing term"); RECOVER;}
1717;
1718
1719break_statement:
1720 BREAK_TK SC_TK
1721 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1722| BREAK_TK identifier SC_TK
1723 { $$ = build_bc_statement ($1.location, 1, $2); }
1724| BREAK_TK error
1725 {yyerror ("Missing term"); RECOVER;}
1726| BREAK_TK identifier error
1727 {yyerror ("';' expected"); RECOVER;}
1728;
1729
1730continue_statement:
1731 CONTINUE_TK SC_TK
1732 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1733| CONTINUE_TK identifier SC_TK
1734 { $$ = build_bc_statement ($1.location, 0, $2); }
1735| CONTINUE_TK error
1736 {yyerror ("Missing term"); RECOVER;}
1737| CONTINUE_TK identifier error
1738 {yyerror ("';' expected"); RECOVER;}
1739;
1740
1741return_statement:
1742 RETURN_TK SC_TK
1743 { $$ = build_return ($1.location, NULL_TREE); }
1744| RETURN_TK expression SC_TK
1745 { $$ = build_return ($1.location, $2); }
1746| RETURN_TK error
1747 {yyerror ("Missing term"); RECOVER;}
1748| RETURN_TK expression error
1749 {yyerror ("';' expected"); RECOVER;}
1750;
1751
1752throw_statement:
1753 THROW_TK expression SC_TK
b9f7e36c
APB
1754 {
1755 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1756 EXPR_WFL_LINECOL ($$) = $1.location;
1757 }
e04a16fb
AG
1758| THROW_TK error
1759 {yyerror ("Missing term"); RECOVER;}
1760| THROW_TK expression error
1761 {yyerror ("';' expected"); RECOVER;}
1762;
1763
1764synchronized_statement:
1765 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1766 {
1767 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1768 EXPR_WFL_LINECOL ($$) =
1769 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1770 }
e04a16fb
AG
1771| synchronized OP_TK expression CP_TK error
1772 {yyerror ("'{' expected"); RECOVER;}
1773| synchronized error
1774 {yyerror ("'(' expected"); RECOVER;}
1775| synchronized OP_TK error CP_TK
1776 {yyerror ("Missing term"); RECOVER;}
1777| synchronized OP_TK error
1778 {yyerror ("Missing term"); RECOVER;}
1779;
1780
b9f7e36c 1781synchronized:
efa0a23f 1782 modifiers
e04a16fb 1783 {
781b0558
KG
1784 check_modifiers (
1785 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1786 $1, ACC_SYNCHRONIZED);
1787 if ($1 != ACC_SYNCHRONIZED)
1788 MODIFIER_WFL (SYNCHRONIZED_TK) =
1789 build_wfl_node (NULL_TREE);
e04a16fb
AG
1790 }
1791;
1792
1793try_statement:
1794 TRY_TK block catches
a7d8d81f 1795 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1796| TRY_TK block finally
a7d8d81f 1797 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1798| TRY_TK block catches finally
2aa11e97
APB
1799 { $$ = build_try_finally_statement
1800 ($1.location, build_try_statement ($1.location,
1801 $2, $3), $4);
1802 }
e04a16fb
AG
1803| TRY_TK error
1804 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1805;
1806
1807catches:
1808 catch_clause
1809| catches catch_clause
b67d701b
PB
1810 {
1811 TREE_CHAIN ($2) = $1;
1812 $$ = $2;
1813 }
e04a16fb
AG
1814;
1815
1816catch_clause:
b67d701b
PB
1817 catch_clause_parameter block
1818 {
1819 java_method_add_stmt (current_function_decl, $2);
1820 exit_block ();
1821 $$ = $1;
1822 }
1823
1824catch_clause_parameter:
1825 CATCH_TK OP_TK formal_parameter CP_TK
1826 {
1827 /* We add a block to define a scope for
1828 formal_parameter (CCBP). The formal parameter is
1829 declared initialized by the appropriate function
1830 call */
1831 tree ccpb = enter_block ();
1832 tree init = build_assignment (ASSIGN_TK, $2.location,
1833 TREE_PURPOSE ($3),
1834 soft_exceptioninfo_call_node);
1835 declare_local_variables (0, TREE_VALUE ($3),
1836 build_tree_list (TREE_PURPOSE ($3),
1837 init));
1838 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1839 EXPR_WFL_LINECOL ($$) = $1.location;
1840 }
e04a16fb 1841| CATCH_TK error
97f30284 1842 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1843| CATCH_TK OP_TK error
97f30284
APB
1844 {
1845 yyerror ("Missing term or ')' expected");
1846 RECOVER; $$ = NULL_TREE;
1847 }
b67d701b 1848| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1849 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1850;
1851
1852finally:
1853 FINALLY_TK block
a7d8d81f 1854 { $$ = $2; }
e04a16fb
AG
1855| FINALLY_TK error
1856 {yyerror ("'{' expected"); RECOVER; }
1857;
1858
1859/* 19.12 Production from 15: Expressions */
1860primary:
1861 primary_no_new_array
1862| array_creation_expression
1863;
1864
1865primary_no_new_array:
1866 literal
1867| THIS_TK
1868 { $$ = build_this ($1.location); }
1869| OP_TK expression CP_TK
1870 {$$ = $2;}
1871| class_instance_creation_expression
1872| field_access
1873| method_invocation
1874| array_access
c2952b01 1875| type_literals
e04a16fb
AG
1876 /* Added, JDK1.1 inner classes. Documentation is wrong
1877 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1878 exist. Used name: instead. */
e04a16fb 1879| name DOT_TK THIS_TK
c2952b01
APB
1880 {
1881 tree wfl = build_wfl_node (this_identifier_node);
1882 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1883 }
e04a16fb
AG
1884| OP_TK expression error
1885 {yyerror ("')' expected"); RECOVER;}
1886| name DOT_TK error
1887 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1888| primitive_type DOT_TK error
1889 {yyerror ("'class' expected" ); RECOVER;}
1890| VOID_TK DOT_TK error
1891 {yyerror ("'class' expected" ); RECOVER;}
1892;
1893
c2952b01
APB
1894/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1895 broke the rule down a bit. */
1896
1897array_type_literal:
1898 primitive_type OSB_TK CSB_TK
1899 {
1900 $$ = build_java_array_type ($1, -1);
1901 CLASS_LOADED_P ($$) = 1;
1902 }
1903| name OSB_TK CSB_TK
1904 { $$ = build_unresolved_array_type ($1); }
1905/* This triggers two reduce/reduce conflict between array_type_literal and
1906 dims. FIXME.
1907| array_type OSB_TK CSB_TK
1908 { $$ = build_unresolved_array_type ($1); }
1909*/
1910;
1911
1912type_literals:
1913 name DOT_TK CLASS_TK
1914 { $$ = build_incomplete_class_ref ($2.location, $1); }
1915| array_type_literal DOT_TK CLASS_TK
1916 { $$ = build_incomplete_class_ref ($2.location, $1); }
1917| primitive_type DOT_TK CLASS_TK
1918 { $$ = build_class_ref ($1); }
1919| VOID_TK DOT_TK CLASS_TK
1920 { $$ = build_class_ref (void_type_node); }
1921;
1922
e04a16fb
AG
1923class_instance_creation_expression:
1924 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1925 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1926| NEW_TK class_type OP_TK CP_TK
b67d701b 1927 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1928| anonymous_class_creation
e04a16fb
AG
1929 /* Added, JDK1.1 inner classes, modified to use name or
1930 primary instead of primary solely which couldn't work in
1931 all situations. */
1932| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1933 {
1934 tree ctor = build_new_invocation ($2, NULL_TREE);
1935 $$ = make_qualified_primary ($1, ctor,
1936 EXPR_WFL_LINECOL ($1));
1937 }
e04a16fb
AG
1938| something_dot_new identifier OP_TK CP_TK class_body
1939| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1940 {
1941 tree ctor = build_new_invocation ($2, $4);
1942 $$ = make_qualified_primary ($1, ctor,
1943 EXPR_WFL_LINECOL ($1));
1944 }
e04a16fb
AG
1945| something_dot_new identifier OP_TK argument_list CP_TK class_body
1946| NEW_TK error SC_TK
1947 {yyerror ("'(' expected"); DRECOVER(new_1);}
1948| NEW_TK class_type error
1949 {yyerror ("'(' expected"); RECOVER;}
1950| NEW_TK class_type OP_TK error
1951 {yyerror ("')' or term expected"); RECOVER;}
1952| NEW_TK class_type OP_TK argument_list error
1953 {yyerror ("')' expected"); RECOVER;}
1954| something_dot_new error
1955 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1956| something_dot_new identifier error
1957 {yyerror ("'(' expected"); RECOVER;}
1958;
1959
c2952b01
APB
1960/* Created after JDK1.1 rules originally added to
1961 class_instance_creation_expression, but modified to use
1962 'class_type' instead of 'TypeName' (type_name) which is mentionned
1963 in the documentation but doesn't exist. */
1964
1965anonymous_class_creation:
1966 NEW_TK class_type OP_TK argument_list CP_TK
1967 { create_anonymous_class ($1.location, $2); }
1968 class_body
1969 {
1970 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1971 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1972
1973 end_class_declaration (1);
1974
1975 /* Now we can craft the new expression */
1976 $$ = build_new_invocation (id, $4);
1977
1978 /* Note that we can't possibly be here if
1979 `class_type' is an interface (in which case the
1980 anonymous class extends Object and implements
1981 `class_type', hence its constructor can't have
1982 arguments.) */
1983
1984 /* Otherwise, the innerclass must feature a
1985 constructor matching `argument_list'. Anonymous
1986 classes are a bit special: it's impossible to
1987 define constructor for them, hence constructors
1988 must be generated following the hints provided by
1989 the `new' expression. Whether a super constructor
1990 of that nature exists or not is to be verified
1991 later on in verify_constructor_super.
1992
1993 It's during the expansion of a `new' statement
1994 refering to an anonymous class that a ctor will
1995 be generated for the anonymous class, with the
1996 right arguments. */
1997
1998 }
1999| NEW_TK class_type OP_TK CP_TK
2000 { create_anonymous_class ($1.location, $2); }
2001 class_body
2002 {
2003 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2004 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2005
2006 end_class_declaration (1);
2007
2008 /* Now we can craft the new expression. The
2009 statement doesn't need to be remember so that a
2010 constructor can be generated, since its signature
2011 is already known. */
2012 $$ = build_new_invocation (id, NULL_TREE);
2013 }
2014;
2015
e04a16fb
AG
2016something_dot_new: /* Added, not part of the specs. */
2017 name DOT_TK NEW_TK
c2952b01 2018 { $$ = $1; }
e04a16fb 2019| primary DOT_TK NEW_TK
c2952b01 2020 { $$ = $1; }
e04a16fb
AG
2021;
2022
2023argument_list:
2024 expression
2025 {
2026 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2027 ctxp->formal_parameter_number = 1;
2028 }
2029| argument_list C_TK expression
2030 {
2031 ctxp->formal_parameter_number += 1;
2032 $$ = tree_cons (NULL_TREE, $3, $1);
2033 }
2034| argument_list C_TK error
2035 {yyerror ("Missing term"); RECOVER;}
2036;
2037
2038array_creation_expression:
2039 NEW_TK primitive_type dim_exprs
2040 { $$ = build_newarray_node ($2, $3, 0); }
2041| NEW_TK class_or_interface_type dim_exprs
2042 { $$ = build_newarray_node ($2, $3, 0); }
2043| NEW_TK primitive_type dim_exprs dims
ba179f9f 2044 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2045| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2046 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2047 /* Added, JDK1.1 anonymous array. Initial documentation rule
2048 modified */
2049| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2050 {
2051 char *sig;
2052 while (CURRENT_OSB (ctxp)--)
2053 obstack_1grow (&temporary_obstack, '[');
2054 sig = obstack_finish (&temporary_obstack);
2055 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2056 $2, get_identifier (sig), $4);
2057 }
e04a16fb 2058| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2059 {
2060 tree type = $2;
2061 while (CURRENT_OSB (ctxp)--)
2062 type = build_java_array_type (type, -1);
2063 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2064 build_pointer_type (type), NULL_TREE, $4);
2065 }
e04a16fb
AG
2066| NEW_TK error CSB_TK
2067 {yyerror ("'[' expected"); DRECOVER ("]");}
2068| NEW_TK error OSB_TK
2069 {yyerror ("']' expected"); RECOVER;}
2070;
2071
2072dim_exprs:
2073 dim_expr
2074 { $$ = build_tree_list (NULL_TREE, $1); }
2075| dim_exprs dim_expr
2076 { $$ = tree_cons (NULL_TREE, $2, $$); }
2077;
2078
2079dim_expr:
2080 OSB_TK expression CSB_TK
2081 {
2082 EXPR_WFL_LINECOL ($2) = $1.location;
2083 $$ = $2;
2084 }
2085| OSB_TK expression error
2086 {yyerror ("']' expected"); RECOVER;}
2087| OSB_TK error
2088 {
2089 yyerror ("Missing term");
2090 yyerror ("']' expected");
2091 RECOVER;
2092 }
2093;
2094
2095dims:
2096 OSB_TK CSB_TK
ba179f9f
APB
2097 {
2098 int allocate = 0;
2099 /* If not initialized, allocate memory for the osb
2100 numbers stack */
2101 if (!ctxp->osb_limit)
2102 {
2103 allocate = ctxp->osb_limit = 32;
2104 ctxp->osb_depth = -1;
2105 }
c2952b01 2106 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2107 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2108 allocate = ctxp->osb_limit << 1;
2109
2110 if (allocate)
2111 {
2112 allocate *= sizeof (int);
2113 if (ctxp->osb_number)
2114 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2115 allocate);
2116 else
2117 ctxp->osb_number = (int *)xmalloc (allocate);
2118 }
2119 ctxp->osb_depth++;
2120 CURRENT_OSB (ctxp) = 1;
2121 }
e04a16fb 2122| dims OSB_TK CSB_TK
ba179f9f 2123 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2124| dims OSB_TK error
2125 { yyerror ("']' expected"); RECOVER;}
2126;
2127
2128field_access:
2129 primary DOT_TK identifier
2130 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2131 /* FIXME - REWRITE TO:
2132 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2133| SUPER_TK DOT_TK identifier
2134 {
2135 tree super_wfl =
9ee9b555 2136 build_wfl_node (super_identifier_node);
e04a16fb
AG
2137 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2138 $$ = make_qualified_name (super_wfl, $3, $2.location);
2139 }
2140| SUPER_TK error
2141 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2142;
2143
2144method_invocation:
2145 name OP_TK CP_TK
2146 { $$ = build_method_invocation ($1, NULL_TREE); }
2147| name OP_TK argument_list CP_TK
2148 { $$ = build_method_invocation ($1, $3); }
2149| primary DOT_TK identifier OP_TK CP_TK
2150 {
22eed1e6
APB
2151 if (TREE_CODE ($1) == THIS_EXPR)
2152 $$ = build_this_super_qualified_invocation
2153 (1, $3, NULL_TREE, 0, $2.location);
2154 else
2155 {
2156 tree invok = build_method_invocation ($3, NULL_TREE);
2157 $$ = make_qualified_primary ($1, invok, $2.location);
2158 }
e04a16fb
AG
2159 }
2160| primary DOT_TK identifier OP_TK argument_list CP_TK
2161 {
22eed1e6
APB
2162 if (TREE_CODE ($1) == THIS_EXPR)
2163 $$ = build_this_super_qualified_invocation
2164 (1, $3, $5, 0, $2.location);
2165 else
2166 {
2167 tree invok = build_method_invocation ($3, $5);
2168 $$ = make_qualified_primary ($1, invok, $2.location);
2169 }
e04a16fb
AG
2170 }
2171| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2172 {
2173 $$ = build_this_super_qualified_invocation
2174 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2175 }
2176| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2177 {
22eed1e6
APB
2178 $$ = build_this_super_qualified_invocation
2179 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2180 }
2181 /* Screws up thing. I let it here until I'm convinced it can
2182 be removed. FIXME
2183| primary DOT_TK error
2184 {yyerror ("'(' expected"); DRECOVER(bad);} */
2185| SUPER_TK DOT_TK error CP_TK
2186 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2187| SUPER_TK DOT_TK error DOT_TK
2188 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2189;
2190
2191array_access:
2192 name OSB_TK expression CSB_TK
2193 { $$ = build_array_ref ($2.location, $1, $3); }
2194| primary_no_new_array OSB_TK expression CSB_TK
2195 { $$ = build_array_ref ($2.location, $1, $3); }
2196| name OSB_TK error
2197 {
2198 yyerror ("Missing term and ']' expected");
2199 DRECOVER(array_access);
2200 }
2201| name OSB_TK expression error
2202 {
2203 yyerror ("']' expected");
2204 DRECOVER(array_access);
2205 }
2206| primary_no_new_array OSB_TK error
2207 {
2208 yyerror ("Missing term and ']' expected");
2209 DRECOVER(array_access);
2210 }
2211| primary_no_new_array OSB_TK expression error
2212 {
2213 yyerror ("']' expected");
2214 DRECOVER(array_access);
2215 }
2216;
2217
2218postfix_expression:
2219 primary
2220| name
2221| post_increment_expression
2222| post_decrement_expression
2223;
2224
2225post_increment_expression:
2226 postfix_expression INCR_TK
2227 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2228;
2229
2230post_decrement_expression:
2231 postfix_expression DECR_TK
2232 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2233;
2234
2235unary_expression:
2236 pre_increment_expression
2237| pre_decrement_expression
2238| PLUS_TK unary_expression
2239 {$$ = build_unaryop ($1.token, $1.location, $2); }
2240| MINUS_TK unary_expression
2241 {$$ = build_unaryop ($1.token, $1.location, $2); }
2242| unary_expression_not_plus_minus
2243| PLUS_TK error
2244 {yyerror ("Missing term"); RECOVER}
2245| MINUS_TK error
2246 {yyerror ("Missing term"); RECOVER}
2247;
2248
2249pre_increment_expression:
2250 INCR_TK unary_expression
2251 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2252| INCR_TK error
2253 {yyerror ("Missing term"); RECOVER}
2254;
2255
2256pre_decrement_expression:
2257 DECR_TK unary_expression
2258 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2259| DECR_TK error
2260 {yyerror ("Missing term"); RECOVER}
2261;
2262
2263unary_expression_not_plus_minus:
2264 postfix_expression
2265| NOT_TK unary_expression
2266 {$$ = build_unaryop ($1.token, $1.location, $2); }
2267| NEG_TK unary_expression
2268 {$$ = build_unaryop ($1.token, $1.location, $2); }
2269| cast_expression
2270| NOT_TK error
2271 {yyerror ("Missing term"); RECOVER}
2272| NEG_TK error
2273 {yyerror ("Missing term"); RECOVER}
2274;
2275
2276cast_expression: /* Error handling here is potentially weak */
2277 OP_TK primitive_type dims CP_TK unary_expression
2278 {
2279 tree type = $2;
ba179f9f 2280 while (CURRENT_OSB (ctxp)--)
e04a16fb 2281 type = build_java_array_type (type, -1);
ba179f9f 2282 ctxp->osb_depth--;
e04a16fb
AG
2283 $$ = build_cast ($1.location, type, $5);
2284 }
2285| OP_TK primitive_type CP_TK unary_expression
2286 { $$ = build_cast ($1.location, $2, $4); }
2287| OP_TK expression CP_TK unary_expression_not_plus_minus
2288 { $$ = build_cast ($1.location, $2, $4); }
2289| OP_TK name dims CP_TK unary_expression_not_plus_minus
2290 {
49f48c71 2291 const char *ptr;
ba179f9f 2292 while (CURRENT_OSB (ctxp)--)
e04a16fb 2293 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2294 ctxp->osb_depth--;
e04a16fb
AG
2295 obstack_grow0 (&temporary_obstack,
2296 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2297 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2298 ptr = obstack_finish (&temporary_obstack);
2299 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2300 $$ = build_cast ($1.location, $2, $5);
2301 }
2302| OP_TK primitive_type OSB_TK error
2303 {yyerror ("']' expected, invalid type expression");}
2304| OP_TK error
2305 {
2306 if (ctxp->prevent_ese != lineno)
2307 yyerror ("Invalid type expression"); RECOVER;
2308 RECOVER;
2309 }
2310| OP_TK primitive_type dims CP_TK error
2311 {yyerror ("Missing term"); RECOVER;}
2312| OP_TK primitive_type CP_TK error
2313 {yyerror ("Missing term"); RECOVER;}
2314| OP_TK name dims CP_TK error
2315 {yyerror ("Missing term"); RECOVER;}
2316;
2317
2318multiplicative_expression:
2319 unary_expression
2320| multiplicative_expression MULT_TK unary_expression
2321 {
2322 $$ = build_binop (BINOP_LOOKUP ($2.token),
2323 $2.location, $1, $3);
2324 }
2325| multiplicative_expression DIV_TK unary_expression
2326 {
2327 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2328 $1, $3);
2329 }
2330| multiplicative_expression REM_TK unary_expression
2331 {
2332 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2333 $1, $3);
2334 }
2335| multiplicative_expression MULT_TK error
2336 {yyerror ("Missing term"); RECOVER;}
2337| multiplicative_expression DIV_TK error
2338 {yyerror ("Missing term"); RECOVER;}
2339| multiplicative_expression REM_TK error
2340 {yyerror ("Missing term"); RECOVER;}
2341;
2342
2343additive_expression:
2344 multiplicative_expression
2345| additive_expression PLUS_TK multiplicative_expression
2346 {
2347 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2348 $1, $3);
2349 }
2350| additive_expression MINUS_TK multiplicative_expression
2351 {
2352 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2353 $1, $3);
2354 }
2355| additive_expression PLUS_TK error
2356 {yyerror ("Missing term"); RECOVER;}
2357| additive_expression MINUS_TK error
2358 {yyerror ("Missing term"); RECOVER;}
2359;
2360
2361shift_expression:
2362 additive_expression
2363| shift_expression LS_TK additive_expression
2364 {
2365 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2366 $1, $3);
2367 }
2368| shift_expression SRS_TK additive_expression
2369 {
2370 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2371 $1, $3);
2372 }
2373| shift_expression ZRS_TK additive_expression
2374 {
2375 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2376 $1, $3);
2377 }
2378| shift_expression LS_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2380| shift_expression SRS_TK error
2381 {yyerror ("Missing term"); RECOVER;}
2382| shift_expression ZRS_TK error
2383 {yyerror ("Missing term"); RECOVER;}
2384;
2385
2386relational_expression:
2387 shift_expression
2388| relational_expression LT_TK shift_expression
2389 {
2390 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2391 $1, $3);
2392 }
2393| relational_expression GT_TK shift_expression
2394 {
2395 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2396 $1, $3);
2397 }
2398| relational_expression LTE_TK shift_expression
2399 {
2400 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2401 $1, $3);
2402 }
2403| relational_expression GTE_TK shift_expression
2404 {
2405 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2406 $1, $3);
2407 }
2408| relational_expression INSTANCEOF_TK reference_type
5e942c50 2409 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2410| relational_expression LT_TK error
2411 {yyerror ("Missing term"); RECOVER;}
2412| relational_expression GT_TK error
2413 {yyerror ("Missing term"); RECOVER;}
2414| relational_expression LTE_TK error
2415 {yyerror ("Missing term"); RECOVER;}
2416| relational_expression GTE_TK error
2417 {yyerror ("Missing term"); RECOVER;}
2418| relational_expression INSTANCEOF_TK error
2419 {yyerror ("Invalid reference type"); RECOVER;}
2420;
2421
2422equality_expression:
2423 relational_expression
2424| equality_expression EQ_TK relational_expression
2425 {
2426 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2427 $1, $3);
2428 }
2429| equality_expression NEQ_TK relational_expression
2430 {
2431 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2432 $1, $3);
2433 }
2434| equality_expression EQ_TK error
2435 {yyerror ("Missing term"); RECOVER;}
2436| equality_expression NEQ_TK error
2437 {yyerror ("Missing term"); RECOVER;}
2438;
2439
2440and_expression:
2441 equality_expression
2442| and_expression AND_TK equality_expression
2443 {
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445 $1, $3);
2446 }
2447| and_expression AND_TK error
2448 {yyerror ("Missing term"); RECOVER;}
2449;
2450
2451exclusive_or_expression:
2452 and_expression
2453| exclusive_or_expression XOR_TK and_expression
2454 {
2455 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2456 $1, $3);
2457 }
2458| exclusive_or_expression XOR_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460;
2461
2462inclusive_or_expression:
2463 exclusive_or_expression
2464| inclusive_or_expression OR_TK exclusive_or_expression
2465 {
2466 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 $1, $3);
2468 }
2469| inclusive_or_expression OR_TK error
2470 {yyerror ("Missing term"); RECOVER;}
2471;
2472
2473conditional_and_expression:
2474 inclusive_or_expression
2475| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2476 {
2477 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 $1, $3);
2479 }
2480| conditional_and_expression BOOL_AND_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2482;
2483
2484conditional_or_expression:
2485 conditional_and_expression
2486| conditional_or_expression BOOL_OR_TK conditional_and_expression
2487 {
2488 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2489 $1, $3);
2490 }
2491| conditional_or_expression BOOL_OR_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2493;
2494
2495conditional_expression: /* Error handling here is weak */
2496 conditional_or_expression
2497| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2498 {
2499 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2500 EXPR_WFL_LINECOL ($$) = $2.location;
2501 }
e04a16fb
AG
2502| conditional_or_expression REL_QM_TK REL_CL_TK error
2503 {
2504 YYERROR_NOW;
2505 yyerror ("Missing term");
2506 DRECOVER (1);
2507 }
2508| conditional_or_expression REL_QM_TK error
2509 {yyerror ("Missing term"); DRECOVER (2);}
2510| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2511 {yyerror ("Missing term"); DRECOVER (3);}
2512;
2513
2514assignment_expression:
2515 conditional_expression
2516| assignment
2517;
2518
2519assignment:
2520 left_hand_side assignment_operator assignment_expression
2521 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2522| left_hand_side assignment_operator error
2523 {
2524 if (ctxp->prevent_ese != lineno)
2525 yyerror ("Missing term");
2526 DRECOVER (assign);
2527 }
2528;
2529
2530left_hand_side:
2531 name
2532| field_access
2533| array_access
2534;
2535
2536assignment_operator:
2537 ASSIGN_ANY_TK
2538| ASSIGN_TK
2539;
2540
2541expression:
2542 assignment_expression
2543;
2544
2545constant_expression:
2546 expression
2547;
2548
2549%%
2550\f
2551
c2952b01
APB
2552/* This section of the code deal with save/restoring parser contexts.
2553 Add mode documentation here. FIXME */
e04a16fb 2554
c2952b01
APB
2555/* Helper function. Create a new parser context. With
2556 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2557 context is copied, otherwise, the new context is zeroed. The newly
2558 created context becomes the current one. */
e04a16fb 2559
c2952b01
APB
2560static void
2561create_new_parser_context (copy_from_previous)
2562 int copy_from_previous;
e04a16fb 2563{
c2952b01 2564 struct parser_ctxt *new;
e04a16fb 2565
c2952b01
APB
2566 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2567 if (copy_from_previous)
2568 {
2569 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2570 new->saved_data_ctx = 1;
2571 }
2572 else
2573 bzero ((PTR) new, sizeof (struct parser_ctxt));
2574
e04a16fb
AG
2575 new->next = ctxp;
2576 ctxp = new;
c2952b01
APB
2577}
2578
2579/* Create a new parser context and make it the current one. */
2580
2581void
2582java_push_parser_context ()
2583{
2584 create_new_parser_context (0);
e04a16fb 2585 if (ctxp->next)
5e942c50
APB
2586 {
2587 ctxp->incomplete_class = ctxp->next->incomplete_class;
2588 ctxp->gclass_list = ctxp->next->gclass_list;
2589 }
e04a16fb
AG
2590}
2591
c2952b01
APB
2592void
2593java_pop_parser_context (generate)
2594 int generate;
2595{
2596 tree current;
2597 struct parser_ctxt *toFree, *next;
2598
2599 if (!ctxp)
2600 return;
2601
2602 toFree = ctxp;
2603 next = ctxp->next;
2604 if (next)
2605 {
2606 next->incomplete_class = ctxp->incomplete_class;
2607 next->gclass_list = ctxp->gclass_list;
2608 lineno = ctxp->lineno;
2609 finput = ctxp->finput;
2610 current_class = ctxp->current_class;
2611 }
2612
2613 /* Set the single import class file flag to 0 for the current list
2614 of imported things */
2615 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2616 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2617
2618 /* And restore those of the previous context */
2619 if ((ctxp = next)) /* Assignment is really meant here */
2620 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2621 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2622
2623 /* If we pushed a context to parse a class intended to be generated,
2624 we keep it so we can remember the class. What we could actually
2625 do is to just update a list of class names. */
2626 if (generate)
2627 {
2628 toFree->next = ctxp_for_generation;
2629 ctxp_for_generation = toFree;
2630 }
2631 else
2632 free (toFree);
2633}
2634
2635/* Create a parser context for the use of saving some global
2636 variables. */
2637
e04a16fb
AG
2638void
2639java_parser_context_save_global ()
2640{
22eed1e6
APB
2641 if (!ctxp)
2642 {
2643 java_push_parser_context ();
ee07f4f4
APB
2644 ctxp->saved_data_ctx = 1;
2645 }
c2952b01
APB
2646
2647 /* If this context already stores data, create a new one suitable
2648 for data storage. */
ee07f4f4 2649 else if (ctxp->saved_data)
c2952b01
APB
2650 create_new_parser_context (1);
2651
e04a16fb
AG
2652 ctxp->finput = finput;
2653 ctxp->lineno = lineno;
2654 ctxp->current_class = current_class;
2655 ctxp->filename = input_filename;
2656 ctxp->current_function_decl = current_function_decl;
ee07f4f4 2657 ctxp->saved_data = 1;
e04a16fb
AG
2658}
2659
c2952b01
APB
2660/* Restore some global variables from the previous context. Make the
2661 previous context the current one. */
2662
e04a16fb
AG
2663void
2664java_parser_context_restore_global ()
2665{
2666 finput = ctxp->finput;
2667 lineno = ctxp->lineno;
2668 current_class = ctxp->current_class;
2669 input_filename = ctxp->filename;
2670 current_function_decl = ctxp->current_function_decl;
c2952b01 2671 ctxp->saved_data = 0;
ee07f4f4
APB
2672 if (ctxp->saved_data_ctx)
2673 java_pop_parser_context (0);
e04a16fb
AG
2674}
2675
c2952b01
APB
2676/* Suspend vital data for the current class/function being parsed so
2677 that an other class can be parsed. Used to let local/anonymous
2678 classes be parsed. */
2679
2680static void
2681java_parser_context_suspend ()
e04a16fb 2682{
c2952b01 2683 /* This makes debugging through java_debug_context easier */
3b304f5b 2684 static const char *name = "<inner buffer context>";
e04a16fb 2685
c2952b01
APB
2686 /* Duplicate the previous context, use it to save the globals we're
2687 interested in */
2688 create_new_parser_context (1);
2689 ctxp->current_function_decl = current_function_decl;
2690 ctxp->current_class = current_class;
5e942c50 2691
c2952b01
APB
2692 /* Then create a new context which inherits all data from the
2693 previous one. This will be the new current context */
2694 create_new_parser_context (1);
2695
2696 /* Help debugging */
2697 ctxp->next->filename = name;
2698}
2699
2700/* Resume vital data for the current class/function being parsed so
2701 that an other class can be parsed. Used to let local/anonymous
2702 classes be parsed. The trick is the data storing file position
2703 informations must be restored to their current value, so parsing
2704 can resume as if no context was ever saved. */
2705
2706static void
2707java_parser_context_resume ()
2708{
2709 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2710 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2711 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2712
2713 /* We need to inherit the list of classes to complete/generate */
2714 restored->incomplete_class = old->incomplete_class;
2715 restored->gclass_list = old->gclass_list;
2716 restored->classd_list = old->classd_list;
2717 restored->class_list = old->class_list;
2718
2719 /* Restore the current class and function from the saver */
2720 current_class = saver->current_class;
2721 current_function_decl = saver->current_function_decl;
2722
2723 /* Retrive the restored context */
2724 ctxp = restored;
2725
2726 /* Re-installed the data for the parsing to carry on */
2727 bcopy (&old->marker_begining, &ctxp->marker_begining,
2728 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2729
2730 /* Buffer context can now be discarded */
2731 free (saver);
2732 free (old);
2733}
2734
2735/* Add a new anchor node to which all statement(s) initializing static
2736 and non static initialized upon declaration field(s) will be
2737 linked. */
2738
2739static void
2740java_parser_context_push_initialized_field ()
2741{
2742 tree node;
2743
2744 node = build_tree_list (NULL_TREE, NULL_TREE);
2745 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2746 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2747
2748 node = build_tree_list (NULL_TREE, NULL_TREE);
2749 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2750 CPC_INITIALIZER_LIST (ctxp) = node;
2751
2752 node = build_tree_list (NULL_TREE, NULL_TREE);
2753 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2754 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2755}
2756
2757/* Pop the lists of initialized field. If this lists aren't empty,
2758 remember them so we can use it to create and populate the $finit$
2759 or <clinit> functions. */
2760
2761static void
2762java_parser_context_pop_initialized_field ()
2763{
2764 tree stmts;
2765 tree class_type = TREE_TYPE (GET_CPC ());
2766
2767 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2768 {
c2952b01
APB
2769 stmts = CPC_INITIALIZER_STMT (ctxp);
2770 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2771 if (stmts && !java_error_count)
2772 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2773 }
2774
c2952b01
APB
2775 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2776 {
2777 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2778 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2779 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2780 /* Keep initialization in order to enforce 8.5 */
2781 if (stmts && !java_error_count)
2782 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2783 }
e04a16fb 2784
c2952b01
APB
2785 /* JDK 1.1 instance initializers */
2786 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2787 {
c2952b01
APB
2788 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2789 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2790 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2791 if (stmts && !java_error_count)
2792 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2793 }
c2952b01
APB
2794}
2795
2796static tree
2797reorder_static_initialized (list)
2798 tree list;
2799{
2800 /* We have to keep things in order. The alias initializer have to
2801 come first, then the initialized regular field, in reverse to
2802 keep them in lexical order. */
2803 tree marker, previous = NULL_TREE;
2804 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2805 if (TREE_CODE (marker) == TREE_LIST
2806 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2807 break;
2808
2809 /* No static initialized, the list is fine as is */
2810 if (!previous)
2811 list = TREE_CHAIN (marker);
2812
2813 /* No marker? reverse the whole list */
2814 else if (!marker)
2815 list = nreverse (list);
2816
2817 /* Otherwise, reverse what's after the marker and the new reordered
2818 sublist will replace the marker. */
b351b287 2819 else
c2952b01
APB
2820 {
2821 TREE_CHAIN (previous) = NULL_TREE;
2822 list = nreverse (list);
2823 list = chainon (TREE_CHAIN (marker), list);
2824 }
2825 return list;
e04a16fb
AG
2826}
2827
c2952b01
APB
2828/* Helper functions to dump the parser context stack. */
2829
2830#define TAB_CONTEXT(C) \
2831 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2832
2833static void
2834java_debug_context_do (tab)
2835 int tab;
2836{
ee07f4f4
APB
2837 struct parser_ctxt *copy = ctxp;
2838 while (copy)
2839 {
c2952b01 2840 TAB_CONTEXT (tab);
ee07f4f4 2841 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2842 TAB_CONTEXT (tab);
ee07f4f4 2843 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2844 TAB_CONTEXT (tab);
2845 fprintf (stderr, "lineno: %d\n", copy->lineno);
2846 TAB_CONTEXT (tab);
ee07f4f4
APB
2847 fprintf (stderr, "package: %s\n",
2848 (copy->package ?
2849 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2850 TAB_CONTEXT (tab);
ee07f4f4 2851 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2852 TAB_CONTEXT (tab);
ee07f4f4
APB
2853 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2854 copy = copy->next;
2855 tab += 2;
2856 }
ee07f4f4
APB
2857}
2858
c2952b01
APB
2859/* Dump the stacked up parser contexts. Intended to be called from a
2860 debugger. */
2861
ee07f4f4
APB
2862void
2863java_debug_context ()
2864{
2865 java_debug_context_do (0);
2866}
2867
c2952b01
APB
2868\f
2869
2870/* Flag for the error report routine to issue the error the first time
2871 it's called (overriding the default behavior which is to drop the
2872 first invocation and honor the second one, taking advantage of a
2873 richer context. */
2874static int force_error = 0;
ee07f4f4 2875
8119c720
APB
2876/* Reporting an constructor invocation error. */
2877static void
2878parse_ctor_invocation_error ()
2879{
2880 if (DECL_CONSTRUCTOR_P (current_function_decl))
2881 yyerror ("Constructor invocation must be first thing in a constructor");
2882 else
2883 yyerror ("Only constructors can invoke constructors");
2884}
2885
2886/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2887
2888static tree
2889parse_jdk1_1_error (msg)
49f48c71 2890 const char *msg;
b67d701b
PB
2891{
2892 sorry (": `%s' JDK1.1(TM) feature", msg);
2893 java_error_count++;
9bbc7d9f 2894 return empty_stmt_node;
b67d701b
PB
2895}
2896
e04a16fb
AG
2897static int do_warning = 0;
2898
2899void
2900yyerror (msg)
49f48c71 2901 const char *msg;
e04a16fb
AG
2902{
2903 static java_lc elc;
2904 static int prev_lineno;
49f48c71 2905 static const char *prev_msg;
e04a16fb 2906
0a2138e2 2907 int save_lineno;
e04a16fb
AG
2908 char *remainder, *code_from_source;
2909 extern struct obstack temporary_obstack;
2910
2911 if (!force_error && prev_lineno == lineno)
2912 return;
2913
2914 /* Save current error location but report latter, when the context is
2915 richer. */
2916 if (ctxp->java_error_flag == 0)
2917 {
2918 ctxp->java_error_flag = 1;
2919 elc = ctxp->elc;
2920 /* Do something to use the previous line if we're reaching the
2921 end of the file... */
2922#ifdef VERBOSE_SKELETON
2923 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2924#endif
2925 return;
2926 }
2927
2928 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2929 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2930 return;
2931
2932 ctxp->java_error_flag = 0;
2933 if (do_warning)
2934 java_warning_count++;
2935 else
2936 java_error_count++;
2937
807bc1db 2938 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2939 {
2940 elc.col = ctxp->p_line->char_col-1;
2941 elc.line = ctxp->p_line->lineno;
2942 }
2943
2944 save_lineno = lineno;
2945 prev_lineno = lineno = elc.line;
2946 prev_msg = msg;
2947
2948 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2949 obstack_grow0 (&temporary_obstack,
2950 code_from_source, strlen (code_from_source));
2951 remainder = obstack_finish (&temporary_obstack);
2952 if (do_warning)
2953 warning ("%s.\n%s", msg, remainder);
2954 else
2955 error ("%s.\n%s", msg, remainder);
2956
2957 /* This allow us to cheaply avoid an extra 'Invalid expression
2958 statement' error report when errors have been already reported on
2959 the same line. This occurs when we report an error but don't have
2960 a synchronization point other than ';', which
2961 expression_statement is the only one to take care of. */
2962 ctxp->prevent_ese = lineno = save_lineno;
2963}
2964
2965static void
15fdcfe9 2966issue_warning_error_from_context (cl, msg, ap)
5e942c50 2967 tree cl;
d4476be2 2968 const char *msg;
15fdcfe9 2969 va_list ap;
5e942c50 2970{
3b304f5b 2971 const char *saved, *saved_input_filename;
15fdcfe9
PB
2972 char buffer [4096];
2973 vsprintf (buffer, msg, ap);
2974 force_error = 1;
5e942c50
APB
2975
2976 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2977 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2978 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2979
2980 /* We have a CL, that's a good reason for using it if it contains data */
2981 saved = ctxp->filename;
2982 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2983 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2984 saved_input_filename = input_filename;
2985 input_filename = ctxp->filename;
15fdcfe9
PB
2986 java_error (NULL);
2987 java_error (buffer);
5e942c50 2988 ctxp->filename = saved;
1886c9d8 2989 input_filename = saved_input_filename;
15fdcfe9 2990 force_error = 0;
5e942c50
APB
2991}
2992
e04a16fb
AG
2993/* Issue an error message at a current source line CL */
2994
15fdcfe9 2995void
df32d2ce 2996parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 2997{
d4476be2 2998#ifndef ANSI_PROTOTYPES
e04a16fb 2999 tree cl;
d4476be2 3000 const char *msg;
e04a16fb 3001#endif
e04a16fb
AG
3002 va_list ap;
3003
3004 VA_START (ap, msg);
d4476be2 3005#ifndef ANSI_PROTOTYPES
e04a16fb 3006 cl = va_arg (ap, tree);
d4476be2 3007 msg = va_arg (ap, const char *);
e04a16fb 3008#endif
15fdcfe9
PB
3009 issue_warning_error_from_context (cl, msg, ap);
3010 va_end (ap);
e04a16fb
AG
3011}
3012
3013/* Issue a warning at a current source line CL */
3014
3015static void
df32d2ce 3016parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3017{
d4476be2 3018#ifndef ANSI_PROTOTYPES
e04a16fb 3019 tree cl;
d4476be2 3020 const char *msg;
e04a16fb 3021#endif
e04a16fb
AG
3022 va_list ap;
3023
3024 VA_START (ap, msg);
d4476be2 3025#ifndef ANSI_PROTOTYPES
e04a16fb 3026 cl = va_arg (ap, tree);
d4476be2 3027 msg = va_arg (ap, const char *);
e04a16fb 3028#endif
e04a16fb 3029
c877974e 3030 force_error = do_warning = 1;
15fdcfe9 3031 issue_warning_error_from_context (cl, msg, ap);
c877974e 3032 do_warning = force_error = 0;
15fdcfe9 3033 va_end (ap);
e04a16fb
AG
3034}
3035
82371d41
APB
3036static tree
3037find_expr_with_wfl (node)
3038 tree node;
3039{
3040 while (node)
3041 {
3042 char code;
3043 tree to_return;
3044
3045 switch (TREE_CODE (node))
3046 {
3047 case BLOCK:
c0d87ff6
PB
3048 node = BLOCK_EXPR_BODY (node);
3049 continue;
82371d41
APB
3050
3051 case COMPOUND_EXPR:
3052 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3053 if (to_return)
3054 return to_return;
c0d87ff6
PB
3055 node = TREE_OPERAND (node, 1);
3056 continue;
82371d41
APB
3057
3058 case LOOP_EXPR:
c0d87ff6
PB
3059 node = TREE_OPERAND (node, 0);
3060 continue;
82371d41
APB
3061
3062 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3063 node = TREE_OPERAND (node, 1);
3064 continue;
3065
82371d41
APB
3066 default:
3067 code = TREE_CODE_CLASS (TREE_CODE (node));
3068 if (((code == '1') || (code == '2') || (code == 'e'))
3069 && EXPR_WFL_LINECOL (node))
3070 return node;
ba179f9f 3071 return NULL_TREE;
82371d41
APB
3072 }
3073 }
3074 return NULL_TREE;
3075}
3076
3077/* Issue a missing return statement error. Uses METHOD to figure the
3078 last line of the method the error occurs in. */
3079
3080static void
3081missing_return_error (method)
3082 tree method;
3083{
3084 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3085 parse_error_context (wfl_operator, "Missing return statement");
3086}
3087
3088/* Issue an unreachable statement error. From NODE, find the next
3089 statement to report appropriately. */
3090static void
3091unreachable_stmt_error (node)
3092 tree node;
3093{
3094 /* Browse node to find the next expression node that has a WFL. Use
3095 the location to report the error */
3096 if (TREE_CODE (node) == COMPOUND_EXPR)
3097 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3098 else
3099 node = find_expr_with_wfl (node);
3100
3101 if (node)
3102 {
3103 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3104 parse_error_context (wfl_operator, "Unreachable statement");
3105 }
3106 else
3107 fatal ("Can't get valid statement - unreachable_stmt_error");
3108}
3109
c877974e 3110int
e04a16fb
AG
3111java_report_errors ()
3112{
3113 if (java_error_count)
3114 fprintf (stderr, "%d error%s",
3115 java_error_count, (java_error_count == 1 ? "" : "s"));
3116 if (java_warning_count)
3117 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3118 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3119 if (java_error_count || java_warning_count)
3120 putc ('\n', stderr);
c877974e 3121 return java_error_count;
e04a16fb
AG
3122}
3123
3124static char *
3125java_accstring_lookup (flags)
3126 int flags;
3127{
3128 static char buffer [80];
3129#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3130
3131 /* Access modifier looked-up first for easier report on forbidden
3132 access. */
3133 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3134 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3135 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3136 if (flags & ACC_STATIC) COPY_RETURN ("static");
3137 if (flags & ACC_FINAL) COPY_RETURN ("final");
3138 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3139 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3140 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3141 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3142 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3143 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3144
3145 buffer [0] = '\0';
3146 return buffer;
3147#undef COPY_RETURN
3148}
3149
b67d701b
PB
3150/* Issuing error messages upon redefinition of classes, interfaces or
3151 variables. */
3152
e04a16fb 3153static void
b67d701b 3154classitf_redefinition_error (context, id, decl, cl)
49f48c71 3155 const char *context;
e04a16fb
AG
3156 tree id, decl, cl;
3157{
3158 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3159 context, IDENTIFIER_POINTER (id),
3160 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3161 /* Here we should point out where its redefined. It's a unicode. FIXME */
3162}
3163
b67d701b
PB
3164static void
3165variable_redefinition_error (context, name, type, line)
3166 tree context, name, type;
3167 int line;
3168{
49f48c71 3169 const char *type_name;
b67d701b
PB
3170
3171 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3172 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3173 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3174 else
0a2138e2 3175 type_name = lang_printable_name (type, 0);
b67d701b
PB
3176
3177 parse_error_context (context,
781b0558 3178 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3179 IDENTIFIER_POINTER (name),
3180 type_name, IDENTIFIER_POINTER (name), line);
3181}
3182
c583dd46
APB
3183static tree
3184build_array_from_name (type, type_wfl, name, ret_name)
3185 tree type, type_wfl, name, *ret_name;
3186{
3187 int more_dims = 0;
49f48c71 3188 const char *string;
c583dd46
APB
3189
3190 /* Eventually get more dims */
3191 string = IDENTIFIER_POINTER (name);
3192 while (string [more_dims] == '[')
3193 more_dims++;
3194
3195 /* If we have, then craft a new type for this variable */
3196 if (more_dims)
3197 {
c0d87ff6 3198 name = get_identifier (&string [more_dims]);
c583dd46 3199
34f4db93
APB
3200 /* If we have a pointer, use its type */
3201 if (TREE_CODE (type) == POINTER_TYPE)
3202 type = TREE_TYPE (type);
c583dd46
APB
3203
3204 /* Building the first dimension of a primitive type uses this
3205 function */
3206 if (JPRIMITIVE_TYPE_P (type))
3207 {
3208 type = build_java_array_type (type, -1);
22eed1e6 3209 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3210 more_dims--;
3211 }
3212 /* Otherwise, if we have a WFL for this type, use it (the type
3213 is already an array on an unresolved type, and we just keep
3214 on adding dimensions) */
3215 else if (type_wfl)
3216 type = type_wfl;
3217
3218 /* Add all the dimensions */
3219 while (more_dims--)
3220 type = build_unresolved_array_type (type);
3221
3222 /* The type may have been incomplete in the first place */
3223 if (type_wfl)
3224 type = obtain_incomplete_type (type);
3225 }
3226
c2952b01
APB
3227 if (ret_name)
3228 *ret_name = name;
c583dd46
APB
3229 return type;
3230}
3231
e04a16fb
AG
3232/* Build something that the type identifier resolver will identify as
3233 being an array to an unresolved type. TYPE_WFL is a WFL on a
3234 identifier. */
3235
3236static tree
3237build_unresolved_array_type (type_or_wfl)
3238 tree type_or_wfl;
3239{
49f48c71 3240 const char *ptr;
e04a16fb 3241
1886c9d8 3242 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3243 just create a array type */
3244 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3245 {
3246 tree type = build_java_array_type (type_or_wfl, -1);
3247 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3248 return type;
3249 }
3250
3251 obstack_1grow (&temporary_obstack, '[');
3252 obstack_grow0 (&temporary_obstack,
3253 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3254 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3255 ptr = obstack_finish (&temporary_obstack);
3256 return build_expr_wfl (get_identifier (ptr),
3257 EXPR_WFL_FILENAME (type_or_wfl),
3258 EXPR_WFL_LINENO (type_or_wfl),
3259 EXPR_WFL_COLNO (type_or_wfl));
3260}
3261
e04a16fb
AG
3262static void
3263parser_add_interface (class_decl, interface_decl, wfl)
3264 tree class_decl, interface_decl, wfl;
3265{
3266 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3267 parse_error_context (wfl, "Interface `%s' repeated",
3268 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3269}
3270
3271/* Bulk of common class/interface checks. Return 1 if an error was
3272 encountered. TAG is 0 for a class, 1 for an interface. */
3273
3274static int
3275check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3276 int is_interface, flags;
3277 tree raw_name, qualified_name, decl, cl;
3278{
3279 tree node;
c2952b01
APB
3280 int sca = 0; /* Static class allowed */
3281 int icaf = 0; /* Inner class allowed flags */
3282 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3283
3284 if (!quiet_flag)
c2952b01
APB
3285 fprintf (stderr, " %s%s %s",
3286 (CPC_INNER_P () ? "inner" : ""),
3287 (is_interface ? "interface" : "class"),
e04a16fb
AG
3288 IDENTIFIER_POINTER (qualified_name));
3289
3290 /* Scope of an interface/class type name:
3291 - Can't be imported by a single type import
3292 - Can't already exists in the package */
3293 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3294 && (node = find_name_in_single_imports (raw_name)))
3295 {
3296 parse_error_context
3297 (cl, "%s name `%s' clashes with imported type `%s'",
3298 (is_interface ? "Interface" : "Class"),
3299 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3300 return 1;
3301 }
3302 if (decl && CLASS_COMPLETE_P (decl))
3303 {
b67d701b
PB
3304 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3305 qualified_name, decl, cl);
e04a16fb
AG
3306 return 1;
3307 }
3308
c2952b01
APB
3309 if (check_inner_class_redefinition (raw_name, cl))
3310 return 1;
3311
3312 /* If public, file name should match class/interface name, except
3313 when dealing with an inner class */
3314 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3315 {
49f48c71 3316 const char *f;
e04a16fb
AG
3317
3318 /* Contains OS dependent assumption on path separator. FIXME */
3319 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3320 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3321 f--)
3322 ;
847fe791 3323 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3324 f++;
3325 if (strncmp (IDENTIFIER_POINTER (raw_name),
3326 f , IDENTIFIER_LENGTH (raw_name)) ||
3327 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3328 parse_error_context
3329 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3330 (is_interface ? "interface" : "class"),
3331 IDENTIFIER_POINTER (qualified_name),
3332 IDENTIFIER_POINTER (raw_name));
3333 }
3334
c2952b01
APB
3335 /* Static classes can be declared only in top level classes. Note:
3336 once static, a inner class is a top level class. */
3337 if (flags & ACC_STATIC)
3338 {
3339 /* Catch the specific error of declaring an class inner class
3340 with no toplevel enclosing class. Prevent check_modifiers from
3341 complaining a second time */
3342 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3343 {
3344 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3345 IDENTIFIER_POINTER (qualified_name));
3346 sca = ACC_STATIC;
3347 }
3348 /* Else, in the context of a top-level class declaration, let
3349 `check_modifiers' do its job, otherwise, give it a go */
3350 else
3351 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3352 }
3353
a40d21da 3354 /* Inner classes can be declared private or protected
c2952b01
APB
3355 within their enclosing classes. */
3356 if (CPC_INNER_P ())
3357 {
3358 /* A class which is local to a block can't be public, private,
3359 protected or static. But it is created final, so allow this
3360 one. */
3361 if (current_function_decl)
3362 icaf = sca = uaaf = ACC_FINAL;
3363 else
3364 {
3365 check_modifiers_consistency (flags);
3366 icaf = ACC_PRIVATE|ACC_PROTECTED;
3367 }
3368 }
3369
a40d21da
APB
3370 if (is_interface)
3371 {
3372 if (CPC_INNER_P ())
3373 uaaf = INTERFACE_INNER_MODIFIERS;
3374 else
3375 uaaf = INTERFACE_MODIFIERS;
3376
3377 check_modifiers ("Illegal modifier `%s' for interface declaration",
3378 flags, uaaf);
3379 }
2884c41e 3380 else
a40d21da
APB
3381 check_modifiers ((current_function_decl ?
3382 "Illegal modifier `%s' for local class declaration" :
3383 "Illegal modifier `%s' for class declaration"),
c2952b01 3384 flags, uaaf|sca|icaf);
e04a16fb
AG
3385 return 0;
3386}
3387
c2952b01
APB
3388static void
3389make_nested_class_name (cpc_list)
3390 tree cpc_list;
3391{
3392 tree name;
3393
3394 if (!cpc_list)
3395 return;
3396 else
3397 make_nested_class_name (TREE_CHAIN (cpc_list));
3398
3399 /* Pick the qualified name when dealing with the first upmost
3400 enclosing class */
3401 name = (TREE_CHAIN (cpc_list) ?
3402 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3403 obstack_grow (&temporary_obstack,
3404 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3405 /* Why is NO_DOLLAR_IN_LABEL defined? */
3406#if 0
3407#ifdef NO_DOLLAR_IN_LABEL
3408 fatal ("make_nested_class_name: Can't use '$' as a separator "
3409 "for inner classes");
3410#endif
3411#endif
3412 obstack_1grow (&temporary_obstack, '$');
3413}
3414
3415/* Can't redefine a class already defined in an earlier scope. */
3416
3417static int
3418check_inner_class_redefinition (raw_name, cl)
3419 tree raw_name, cl;
3420{
3421 tree scope_list;
3422
3423 for (scope_list = GET_CPC_LIST (); scope_list;
3424 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3425 if (raw_name == GET_CPC_UN_NODE (scope_list))
3426 {
3427 parse_error_context
3428 (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",
3429 IDENTIFIER_POINTER (raw_name));
3430 return 1;
3431 }
3432 return 0;
3433}
3434
3435static tree
3436find_as_inner_class (enclosing, name, cl)
3437 tree enclosing, name, cl;
3438{
3439 tree qual, to_return;
3440 if (!enclosing)
3441 return NULL_TREE;
3442
3443 name = TYPE_NAME (name);
3444
3445 /* First search: within the scope of `enclosing', search for name */
3446 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3447 qual = EXPR_WFL_QUALIFICATION (cl);
3448 else if (cl)
3449 qual = build_tree_list (cl, NULL_TREE);
3450 else
3451 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3452
3453 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3454 return to_return;
3455
3456 /* We're dealing with a qualified name. Try to resolve thing until
3457 we get something that is an enclosing class. */
3458 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3459 {
3460 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3461
3462 for(qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3463 qual = TREE_CHAIN (qual))
3464 {
3465 acc = merge_qualified_name (acc,
3466 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3467 BUILD_PTR_FROM_NAME (ptr, acc);
3468 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3469 }
3470
3471 /* A NULL qual and a decl means that the search ended
3472 successfully?!? We have to do something then. FIXME */
3473
3474 if (decl)
3475 enclosing = decl;
3476 else
3477 qual = EXPR_WFL_QUALIFICATION (cl);
3478 }
3479 /* Otherwise, create a qual for the other part of the resolution. */
3480 else
3481 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3482
3483 return find_as_inner_class_do (qual, enclosing);
3484}
3485
3486/* We go inside the list of sub classes and try to find a way
3487 through. */
3488
3489static tree
3490find_as_inner_class_do (qual, enclosing)
3491 tree qual, enclosing;
3492{
3493 if (!qual)
3494 return NULL_TREE;
3495
3496 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3497 {
3498 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3499 tree next_enclosing = NULL_TREE;
3500 tree inner_list;
3501
3502 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3503 inner_list; inner_list = TREE_CHAIN (inner_list))
3504 {
3505 if (TREE_VALUE (inner_list) == name_to_match)
3506 {
3507 next_enclosing = TREE_PURPOSE (inner_list);
3508 break;
3509 }
3510 }
3511 enclosing = next_enclosing;
3512 }
3513
3514 return (!qual && enclosing ? enclosing : NULL_TREE);
3515}
3516
3517/* Reach all inner classes and tie their unqualified name to a
3518 DECL. */
3519
3520static void
3521set_nested_class_simple_name_value (outer, set)
3522 tree outer;
3523 int set;
3524{
3525 tree l;
3526
3527 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3528 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3529 TREE_PURPOSE (l) : NULL_TREE);
3530}
3531
3532static void
3533link_nested_class_to_enclosing ()
3534{
3535 if (GET_ENCLOSING_CPC ())
3536 {
3537 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3538 DECL_INNER_CLASS_LIST (enclosing) =
3539 tree_cons (GET_CPC (), GET_CPC_UN (),
3540 DECL_INNER_CLASS_LIST (enclosing));
3541 enclosing = enclosing;
3542 }
3543}
3544
3545static tree
3546maybe_make_nested_class_name (name)
3547 tree name;
3548{
3549 tree id = NULL_TREE;
3550
3551 if (CPC_INNER_P ())
3552 {
3553 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3554 obstack_grow0 (&temporary_obstack,
3555 IDENTIFIER_POINTER (name),
3556 IDENTIFIER_LENGTH (name));
c2952b01
APB
3557 id = get_identifier (obstack_finish (&temporary_obstack));
3558 if (ctxp->package)
3559 QUALIFIED_P (id) = 1;
3560 }
3561 return id;
3562}
3563
3564/* If DECL is NULL, create and push a new DECL, record the current
3565 line CL and do other maintenance things. */
3566
e04a16fb 3567static tree
c2952b01
APB
3568maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3569 tree decl, raw_name, qualified_name, cl;
e04a16fb 3570{
5e942c50 3571 if (!decl)
e04a16fb 3572 decl = push_class (make_class (), qualified_name);
c2952b01 3573
e04a16fb
AG
3574 /* Take care of the file and line business */
3575 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3576 /* If we're emiting xrefs, store the line/col number information */
3577 if (flag_emit_xref)
3578 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3579 else
3580 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3581 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3582 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3583 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3584
c2952b01
APB
3585 PUSH_CPC (decl, raw_name);
3586 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3587
e04a16fb
AG
3588 /* Link the declaration to the already seen ones */
3589 TREE_CHAIN (decl) = ctxp->class_list;
3590 ctxp->class_list = decl;
5e942c50 3591
23a79c61 3592 /* Create a new nodes in the global lists */
5e942c50 3593 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3594 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3595
e04a16fb
AG
3596 /* Install a new dependency list element */
3597 create_jdep_list (ctxp);
3598
3599 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3600 IDENTIFIER_POINTER (qualified_name)));
3601 return decl;
3602}
3603
3604static void
3605add_superinterfaces (decl, interface_list)
3606 tree decl, interface_list;
3607{
3608 tree node;
3609 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3610 takes care of ensuring that:
3611 - This is an accessible interface type,
3612 - Circularity detection.
3613 parser_add_interface is then called. If present but not defined,
3614 the check operation is delayed until the super interface gets
3615 defined. */
3616 for (node = interface_list; node; node = TREE_CHAIN (node))
3617 {
15fdcfe9 3618 tree current = TREE_PURPOSE (node);
5e942c50
APB
3619 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3620 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3621 {
5e942c50
APB
3622 if (!parser_check_super_interface (idecl, decl, current))
3623 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3624 }
3625 else
3626 register_incomplete_type (JDEP_INTERFACE,
3627 current, decl, NULL_TREE);
3628 }
3629}
3630
3631/* Create an interface in pass1 and return its decl. Return the
3632 interface's decl in pass 2. */
3633
3634static tree
3635create_interface (flags, id, super)
3636 int flags;
3637 tree id, super;
3638{
e04a16fb 3639 tree raw_name = EXPR_WFL_NODE (id);
c2952b01 3640 tree q_name = parser_qualified_classname (flags & ACC_STATIC, raw_name);
e04a16fb
AG
3641 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3642
3643 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3644
3645 /* Basic checks: scope, redefinition, modifiers */
3646 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3647 {
3648 PUSH_ERROR ();
3649 return NULL_TREE;
3650 }
3651
3652 /* Suspend the current parsing context if we're parsing an inner
3653 interface */
3654 if (CPC_INNER_P ())
3655 java_parser_context_suspend ();
3656
3657 /* Push a new context for (static) initialized upon declaration fields */
3658 java_parser_context_push_initialized_field ();
e04a16fb
AG
3659
3660 /* Interface modifiers check
3661 - public/abstract allowed (already done at that point)
3662 - abstract is obsolete (comes first, it's a warning, or should be)
3663 - Can't use twice the same (checked in the modifier rule) */
c877974e 3664 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3665 parse_warning_context
3666 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3667 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3668
3669 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3670 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3671
3672 /* Set super info and mark the class a complete */
2aa11e97 3673 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3674 object_type_node, ctxp->interface_number);
3675 ctxp->interface_number = 0;
3676 CLASS_COMPLETE_P (decl) = 1;
3677 add_superinterfaces (decl, super);
3678
3679 return decl;
3680}
3681
c2952b01
APB
3682/* Anonymous class counter. Will be reset to 1 every time a non
3683 anonymous class gets created. */
3684static int anonymous_class_counter = 1;
3685
3686/* Patch anonymous class CLASS, by either extending or implementing
3687 DEP. */
3688
3689static void
3690patch_anonymous_class (type_decl, class_decl, wfl)
3691 tree type_decl, class_decl, wfl;
3692{
3693 tree class = TREE_TYPE (class_decl);
3694 tree type = TREE_TYPE (type_decl);
3695 tree binfo = TYPE_BINFO (class);
3696
3697 /* If it's an interface, implement it */
3698 if (CLASS_INTERFACE (type_decl))
3699 {
3700 tree s_binfo;
3701 int length;
3702
3703 if (parser_check_super_interface (type_decl, class_decl, wfl))
3704 return;
3705
3706 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3707 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3708 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3709 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3710 /* And add the interface */
3711 parser_add_interface (class_decl, type_decl, wfl);
3712 }
3713 /* Otherwise, it's a type we want to extend */
3714 else
3715 {
3716 if (parser_check_super (type_decl, class_decl, wfl))
3717 return;
3718 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3719 }
3720}
3721
3722static tree
3723create_anonymous_class (location, type_name)
3724 int location;
3725 tree type_name;
3726{
3727 char buffer [80];
3728 tree super = NULL_TREE, itf = NULL_TREE;
3729 tree id, type_decl, class;
3730
3731 /* The unqualified name of the anonymous class. It's just a number. */
3732 sprintf (buffer, "%d", anonymous_class_counter++);
3733 id = build_wfl_node (get_identifier (buffer));
3734 EXPR_WFL_LINECOL (id) = location;
3735
3736 /* We know about the type to extend/implement. We go ahead */
3737 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3738 {
3739 /* Create a class which either implements on extends the designated
3740 class. The class bears an innacessible name. */
3741 if (CLASS_INTERFACE (type_decl))
3742 {
3743 /* It's OK to modify it here. It's been already used and
3744 shouldn't be reused */
3745 ctxp->interface_number = 1;
3746 /* Interfaces should presented as a list of WFLs */
3747 itf = build_tree_list (type_name, NULL_TREE);
3748 }
3749 else
3750 super = type_name;
3751 }
3752
3753 class = create_class (ACC_FINAL, id, super, itf);
3754
3755 /* We didn't know anything about the stuff. We register a dependence. */
3756 if (!type_decl)
3757 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3758
3759 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3760 return class;
3761}
3762
a40d21da 3763/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3764 interface's decl in pass 2. */
3765
3766static tree
3767create_class (flags, id, super, interfaces)
3768 int flags;
3769 tree id, super, interfaces;
3770{
e04a16fb
AG
3771 tree raw_name = EXPR_WFL_NODE (id);
3772 tree class_id, decl;
9ee9b555 3773 tree super_decl_type;
e04a16fb 3774
c2952b01 3775 class_id = parser_qualified_classname (0, raw_name);
e04a16fb
AG
3776 decl = IDENTIFIER_CLASS_VALUE (class_id);
3777 EXPR_WFL_NODE (id) = class_id;
3778
3779 /* Basic check: scope, redefinition, modifiers */
3780 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3781 {
3782 PUSH_ERROR ();
3783 return NULL_TREE;
3784 }
3785
3786 /* Suspend the current parsing context if we're parsing an inner
3787 class or an anonymous class. */
3788 if (CPC_INNER_P ())
3789 java_parser_context_suspend ();
3790 /* Push a new context for (static) initialized upon declaration fields */
3791 java_parser_context_push_initialized_field ();
e04a16fb
AG
3792
3793 /* Class modifier check:
3794 - Allowed modifier (already done at that point)
3795 - abstract AND final forbidden
3796 - Public classes defined in the correct file */
3797 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3798 parse_error_context
3799 (id, "Class `%s' can't be declared both abstract and final",
3800 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3801
3802 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3803 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3804
3805 /* If SUPER exists, use it, otherwise use Object */
3806 if (super)
3807 {
3808 /* Can't extend java.lang.Object */
3809 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3810 {
3811 parse_error_context (id, "Can't extend `java.lang.Object'");
3812 return NULL_TREE;
3813 }
3814
2c3199bc
PB
3815 super_decl_type =
3816 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3817 }
3818 else if (TREE_TYPE (decl) != object_type_node)
3819 super_decl_type = object_type_node;
3820 /* We're defining java.lang.Object */
3821 else
3822 super_decl_type = NULL_TREE;
3823
3824 /* Set super info and mark the class a complete */
3825 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3826 ctxp->interface_number);
3827 ctxp->interface_number = 0;
3828 CLASS_COMPLETE_P (decl) = 1;
3829 add_superinterfaces (decl, interfaces);
3830
c2952b01
APB
3831 /* If the class is a top level inner class, install an alias. */
3832 if (INNER_CLASS_DECL_P (decl) && CLASS_STATIC (decl))
3833 {
3834 tree alias = parser_qualified_classname (1, raw_name);
3835 IDENTIFIER_GLOBAL_VALUE (alias) = decl;
3836 }
3837
3838 /* Add the private this$<n> field, Replicate final locals still in
3839 scope as private final fields mangled like val$<local_name>.
3840 This doesn't not occur for top level (static) inner classes. */
3841 if (PURE_INNER_CLASS_DECL_P (decl))
3842 add_inner_class_fields (decl, current_function_decl);
3843
7f10c2e2
APB
3844 /* If doing xref, store the location at which the inherited class
3845 (if any) was seen. */
3846 if (flag_emit_xref && super)
3847 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3848
5e942c50
APB
3849 /* Eventually sets the @deprecated tag flag */
3850 CHECK_DEPRECATED (decl);
3851
165f37bc
APB
3852 /* Reset the anonymous class counter when declaring non inner classes */
3853 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3854 anonymous_class_counter = 1;
3855
e04a16fb
AG
3856 return decl;
3857}
3858
c2952b01
APB
3859/* End a class declaration: register the statements used to create
3860 $finit$ and <clinit>, pop the current class and resume the prior
3861 parser context if necessary. */
3862
3863static void
3864end_class_declaration (resume)
3865 int resume;
3866{
3867 /* If an error occured, context weren't pushed and won't need to be
3868 popped by a resume. */
3869 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3870
3871 java_parser_context_pop_initialized_field ();
3872 POP_CPC ();
3873 if (resume && no_error_occured)
3874 java_parser_context_resume ();
93220702
APB
3875
3876 /* We're ending a class declaration, this is a good time to reset
3877 the interface cout. Note that might have been already done in
3878 create_interface, but if at that time an inner class was being
3879 dealt with, the interface count was reset in a context created
3880 for the sake of handling inner classes declaration. */
3881 ctxp->interface_number = 0;
c2952b01
APB
3882}
3883
3884static void
3885add_inner_class_fields (class_decl, fct_decl)
3886 tree class_decl;
3887 tree fct_decl;
3888{
3889 tree block, marker, f;
3890
3891 f = add_field (TREE_TYPE (class_decl),
3892 build_current_thisn (TREE_TYPE (class_decl)),
3893 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3894 ACC_PRIVATE);
3895 FIELD_THISN (f) = 1;
3896
3897 if (!fct_decl)
3898 return;
3899
3900 for (block = GET_CURRENT_BLOCK (fct_decl);
3901 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3902 {
3903 tree decl;
3904 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3905 {
3906 char *name, *pname;
3907 tree wfl, init, list;
3908
3909 /* Avoid non final arguments. */
3910 if (!LOCAL_FINAL (decl))
3911 continue;
3912
3913 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3914 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3915 wfl = build_wfl_node (get_identifier (name));
3916 init = build_wfl_node (get_identifier (pname));
3917 /* Build an initialization for the field: it will be
3918 initialized by a parameter added to $finit$, bearing a
3919 mangled name of the field itself (param$<n>.) The
3920 parameter is provided to $finit$ by the constructor
3921 invoking it (hence the constructor will also feature a
3922 hidden parameter, set to the value of the outer context
3923 local at the time the inner class is created.)
3924
3925 Note: we take into account all possible locals that can
3926 be accessed by the inner class. It's actually not trivial
3927 to minimize these aliases down to the ones really
3928 used. One way to do that would be to expand all regular
3929 methods first, then $finit$ to get a picture of what's
3930 used. It works with the exception that we would have to
3931 go back on all constructor invoked in regular methods to
3932 have their invokation reworked (to include the right amount
3933 of alias initializer parameters.)
3934
3935 The only real way around, I think, is a first pass to
3936 identify locals really used in the inner class. We leave
3937 the flag FIELD_LOCAL_ALIAS_USED around for that future
3938 use.
3939
3940 On the other hand, it only affect local inner classes,
3941 whose constructors (and $finit$ call) will be featuring
3942 unecessary arguments. It's easy for a developper to keep
3943 this number of parameter down by using the `final'
3944 keyword only when necessary. For the time being, we can
3945 issue a warning on unecessary finals. FIXME */
3946 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3947 wfl, init);
3948
3949 /* Register the field. The TREE_LIST holding the part
3950 initialized/initializer will be marked ARG_FINAL_P so
3951 that the created field can be marked
3952 FIELD_LOCAL_ALIAS. */
3953 list = build_tree_list (wfl, init);
3954 ARG_FINAL_P (list) = 1;
3955 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3956 }
3957 }
3958
3959 if (!CPC_INITIALIZER_STMT (ctxp))
3960 return;
3961
3962 /* If we ever registered an alias field, insert and marker to
3963 remeber where the list ends. The second part of the list (the one
3964 featuring initialized fields) so it can be later reversed to
3965 enforce 8.5. The marker will be removed during that operation. */
3966 marker = build_tree_list (NULL_TREE, NULL_TREE);
3967 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3968 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3969}
3970
e04a16fb
AG
3971/* Can't use lookup_field () since we don't want to load the class and
3972 can't set the CLASS_LOADED_P flag */
3973
3974static tree
3975find_field (class, name)
3976 tree class;
3977 tree name;
3978{
3979 tree decl;
3980 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3981 {
3982 if (DECL_NAME (decl) == name)
3983 return decl;
3984 }
3985 return NULL_TREE;
3986}
3987
3988/* Wrap around lookup_field that doesn't potentially upset the value
3989 of CLASS */
3990
3991static tree
3992lookup_field_wrapper (class, name)
3993 tree class, name;
3994{
3995 tree type = class;
5b09b33e 3996 tree decl;
c877974e 3997 java_parser_context_save_global ();
5b09b33e 3998 decl = lookup_field (&type, name);
f2760b27
APB
3999
4000 /* Last chance: if we're within the context of an inner class, we
4001 might be trying to access a local variable defined in an outer
4002 context. We try to look for it now. */
4003 if (INNER_CLASS_TYPE_P (class) && (!decl || decl == error_mark_node))
4004 {
4005 char *alias_buffer;
4006 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
4007 name = get_identifier (alias_buffer);
4008 type = class;
4009 decl = lookup_field (&type, name);
4010 if (decl && decl != error_mark_node)
4011 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4012 }
4013
c877974e 4014 java_parser_context_restore_global ();
93024893 4015 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4016}
4017
4018/* Find duplicate field within the same class declarations and report
c583dd46
APB
4019 the error. Returns 1 if a duplicated field was found, 0
4020 otherwise. */
e04a16fb
AG
4021
4022static int
c583dd46 4023duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4024 tree new_field_name, new_type, cl;
e04a16fb
AG
4025{
4026 /* This might be modified to work with method decl as well */
c2952b01 4027 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4028 if (decl)
4029 {
c2e3db92 4030 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4031 ((TREE_CODE (new_type) == POINTER_TYPE
4032 && TREE_TYPE (new_type) == NULL_TREE) ?
4033 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4034 lang_printable_name (new_type, 1)));
c877974e
APB
4035 /* The type may not have been completed by the time we report
4036 the error */
c2e3db92 4037 char *t2 = xstrdup (purify_type_name
4a5f66c3 4038 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4039 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4040 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4041 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4042 parse_error_context
4043 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4044 t1, IDENTIFIER_POINTER (new_field_name),
4045 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4046 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4047 free (t1);
4048 free (t2);
c583dd46 4049 return 1;
e04a16fb 4050 }
c583dd46 4051 return 0;
e04a16fb
AG
4052}
4053
4054/* Field registration routine. If TYPE doesn't exist, field
4055 declarations are linked to the undefined TYPE dependency list, to
4056 be later resolved in java_complete_class () */
4057
4058static void
4059register_fields (flags, type, variable_list)
4060 int flags;
4061 tree type, variable_list;
4062{
c583dd46 4063 tree current, saved_type;
c2952b01 4064 tree class_type = NULL_TREE;
e04a16fb
AG
4065 int saved_lineno = lineno;
4066 int must_chain = 0;
4067 tree wfl = NULL_TREE;
4068
c2952b01
APB
4069 if (GET_CPC ())
4070 class_type = TREE_TYPE (GET_CPC ());
4071
4072 if (!class_type || class_type == error_mark_node)
4073 return;
4074
e04a16fb
AG
4075 /* If we're adding fields to interfaces, those fields are public,
4076 static, final */
4077 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4078 {
4079 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4080 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4081 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4082 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4083 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4084 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4085 check_modifiers ("Illegal interface member modifier `%s'", flags,
4086 INTERFACE_FIELD_MODIFIERS);
4087 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4088 }
4089
c583dd46
APB
4090 /* Obtain a suitable type for resolution, if necessary */
4091 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4092
4093 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4094 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4095
c583dd46
APB
4096 for (current = variable_list, saved_type = type; current;
4097 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4098 {
c877974e 4099 tree real_type;
c583dd46 4100 tree field_decl;
e04a16fb
AG
4101 tree cl = TREE_PURPOSE (current);
4102 tree init = TREE_VALUE (current);
4103 tree current_name = EXPR_WFL_NODE (cl);
4104
c2952b01
APB
4105 /* Can't declare static fields in inner classes */
4106 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4107 && !CLASS_INTERFACE (TYPE_NAME (class_type)))
4108 parse_error_context
4109 (cl, "Field `%s' can't be static in innerclass `%s'. Only members of interfaces and top-level classes can be static",
4110 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4111 lang_printable_name (class_type, 0));
4112
c583dd46
APB
4113 /* Process NAME, as it may specify extra dimension(s) for it */
4114 type = build_array_from_name (type, wfl, current_name, &current_name);
4115
c583dd46
APB
4116 /* Type adjustment. We may have just readjusted TYPE because
4117 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4118 a reference if we can and don't have one already. Also
4119 change the name if we have an init. */
4120 if (type != saved_type)
4121 {
1886c9d8 4122 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4123 if (init)
4124 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4125 }
e04a16fb 4126
c877974e
APB
4127 real_type = GET_REAL_TYPE (type);
4128 /* Check for redeclarations */
4129 if (duplicate_declaration_error_p (current_name, real_type, cl))
4130 continue;
4131
c583dd46 4132 /* Set lineno to the line the field was found and create a
5e942c50 4133 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4134 if (flag_emit_xref)
4135 lineno = EXPR_WFL_LINECOL (cl);
4136 else
4137 lineno = EXPR_WFL_LINENO (cl);
c877974e 4138 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4139 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4140
4141 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4142 mark the created field FIELD_LOCAL_ALIAS, so that we can
4143 hide parameters to this inner class $finit$ and constructors. */
4144 if (ARG_FINAL_P (current))
4145 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4146
4147 /* Check if we must chain. */
4148 if (must_chain)
4149 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4150
c583dd46
APB
4151 /* If we have an initialization value tied to the field */
4152 if (init)
4153 {
4154 /* The field is declared static */
e04a16fb 4155 if (flags & ACC_STATIC)
e04a16fb 4156 {
7525cc04
APB
4157 /* We include the field and its initialization part into
4158 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4159 walked, field initializations will be processed and
4160 fields initialized with known constants will be taken
4161 out of <clinit> and have their DECL_INITIAL set
7525cc04 4162 appropriately. */
c2952b01
APB
4163 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4164 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4165 if (TREE_OPERAND (init, 1)
4166 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4167 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4168 }
5e942c50
APB
4169 /* A non-static field declared with an immediate initialization is
4170 to be initialized in <init>, if any. This field is remembered
4171 to be processed at the time of the generation of <init>. */
c583dd46
APB
4172 else
4173 {
c2952b01
APB
4174 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4175 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4176 }
5b09b33e 4177 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4178 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4179 }
4180 }
4181 lineno = saved_lineno;
4182}
4183
c2952b01
APB
4184/* Generate $finit$, using the list of initialized fields to populate
4185 its body. $finit$'s parameter(s) list is adjusted to include the
4186 one(s) used to initialized the field(s) caching outer context
4187 local(s). */
22eed1e6 4188
c2952b01
APB
4189static tree
4190generate_finit (class_type)
4191 tree class_type;
22eed1e6 4192{
c2952b01
APB
4193 int count = 0;
4194 tree list = TYPE_FINIT_STMT_LIST (class_type);
4195 tree mdecl, current, parms;
4196
4197 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4198 class_type, NULL_TREE,
4199 &count);
4200 CRAFTED_PARAM_LIST_FIXUP (parms);
4201 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4202 finit_identifier_node, parms);
4203 fix_method_argument_names (parms, mdecl);
4204 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4205 mdecl, NULL_TREE);
4206 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4207 start_artificial_method_body (mdecl);
4208
c2952b01 4209 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4210 java_method_add_stmt (mdecl,
4211 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4212 current));
22eed1e6 4213 end_artificial_method_body (mdecl);
c2952b01 4214 return mdecl;
22eed1e6
APB
4215}
4216
e04a16fb 4217static void
c2952b01
APB
4218add_instance_initializer (mdecl)
4219 tree mdecl;
e04a16fb 4220{
c2952b01
APB
4221 tree current;
4222 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4223 tree compound = NULL_TREE;
e04a16fb 4224
c2952b01 4225 if (stmt_list)
e04a16fb 4226 {
c2952b01
APB
4227 for (current = stmt_list; current; current = TREE_CHAIN (current))
4228 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4229
c2952b01
APB
4230 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4231 NULL_TREE, compound));
4232 }
e04a16fb
AG
4233}
4234
4235/* Shared accros method_declarator and method_header to remember the
4236 patch stage that was reached during the declaration of the method.
4237 A method DECL is built differently is there is no patch
4238 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4239 pending on the currently defined method. */
4240
4241static int patch_stage;
4242
4243/* Check the method declaration and add the method to its current
4244 class. If the argument list is known to contain incomplete types,
4245 the method is partially added and the registration will be resume
22eed1e6
APB
4246 once the method arguments resolved. If TYPE is NULL, we're dealing
4247 with a constructor. */
e04a16fb
AG
4248
4249static tree
4250method_header (flags, type, mdecl, throws)
4251 int flags;
4252 tree type, mdecl, throws;
4253{
4254 tree meth = TREE_VALUE (mdecl);
4255 tree id = TREE_PURPOSE (mdecl);
1886c9d8 4256 tree type_wfl = NULL_TREE;
79d13333 4257 tree meth_name = NULL_TREE;
c2952b01 4258 tree current, orig_arg, this_class = NULL;
e04a16fb 4259 int saved_lineno;
1886c9d8 4260 int constructor_ok = 0, must_chain;
c2952b01 4261 int count;
e04a16fb
AG
4262
4263 check_modifiers_consistency (flags);
79d13333 4264
c2952b01
APB
4265 if (GET_CPC ())
4266 this_class = TREE_TYPE (GET_CPC ());
4267
4268 if (!this_class || this_class == error_mark_node)
79d13333 4269 return NULL_TREE;
e04a16fb
AG
4270
4271 /* There are some forbidden modifiers for an abstract method and its
4272 class must be abstract as well. */
22eed1e6 4273 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4274 {
4275 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4276 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4277 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4278 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4279 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4280 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4281 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4282 parse_error_context
781b0558 4283 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e04a16fb
AG
4284 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
4285 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4286 }
c2952b01 4287
22eed1e6
APB
4288 /* Things to be checked when declaring a constructor */
4289 if (!type)
4290 {
4291 int ec = java_error_count;
4292 /* 8.6: Constructor declarations: we might be trying to define a
4293 method without specifying a return type. */
c2952b01 4294 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4295 parse_error_context
4296 (id, "Invalid method declaration, return type required");
4297 /* 8.6.3: Constructor modifiers */
4298 else
4299 {
4300 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4301 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4302 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4303 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4304 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4305 }
4306 /* If we found error here, we don't consider it's OK to tread
4307 the method definition as a constructor, for the rest of this
4308 function */
4309 if (ec == java_error_count)
4310 constructor_ok = 1;
4311 }
e04a16fb
AG
4312
4313 /* Method declared within the scope of an interface are implicitly
4314 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4315 modifiers are checked right after. */
e04a16fb
AG
4316
4317 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4318 {
4319 /* If FLAGS isn't set because of a modifier, turn the
4320 corresponding modifier WFL to NULL so we issue a warning on
4321 the obsolete use of the modifier */
4322 if (!(flags & ACC_PUBLIC))
4323 MODIFIER_WFL (PUBLIC_TK) = NULL;
4324 if (!(flags & ACC_ABSTRACT))
4325 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4326 flags |= ACC_PUBLIC;
4327 flags |= ACC_ABSTRACT;
4328 }
4329
c2952b01
APB
4330 /* Inner class can't declare static methods */
4331 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4332 {
4333 parse_error_context
4334 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4335 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4336 lang_printable_name (this_class, 0));
4337 }
4338
e04a16fb
AG
4339 /* Modifiers context reset moved up, so abstract method declaration
4340 modifiers can be later checked. */
4341
22eed1e6
APB
4342 /* Set constructor returned type to void and method name to <init>,
4343 unless we found an error identifier the constructor (in which
4344 case we retain the original name) */
4345 if (!type)
4346 {
4347 type = void_type_node;
4348 if (constructor_ok)
4349 meth_name = init_identifier_node;
4350 }
4351 else
4352 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4353
1886c9d8
APB
4354 /* Do the returned type resolution and registration if necessary */
4355 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4356
4a5f66c3
APB
4357 if (meth_name)
4358 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4359 EXPR_WFL_NODE (id) = meth_name;
4360 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4361
4362 if (must_chain)
e04a16fb 4363 {
1886c9d8
APB
4364 patch_stage = JDEP_METHOD_RETURN;
4365 register_incomplete_type (patch_stage, type_wfl, id, type);
4366 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4367 }
4368 else
1886c9d8 4369 TREE_TYPE (meth) = type;
e04a16fb
AG
4370
4371 saved_lineno = lineno;
4372 /* When defining an abstract or interface method, the curly
4373 bracket at level 1 doesn't exist because there is no function
4374 body */
4375 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4376 EXPR_WFL_LINENO (id));
4377
5e942c50
APB
4378 /* Remember the original argument list */
4379 orig_arg = TYPE_ARG_TYPES (meth);
4380
e04a16fb
AG
4381 if (patch_stage) /* includes ret type and/or all args */
4382 {
4383 jdep *jdep;
4384 meth = add_method_1 (this_class, flags, meth_name, meth);
4385 /* Patch for the return type */
4386 if (patch_stage == JDEP_METHOD_RETURN)
4387 {
4388 jdep = CLASSD_LAST (ctxp->classd_list);
4389 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4390 }
4391 /* This is the stop JDEP. METH allows the function's signature
4392 to be computed. */
4393 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4394 }
4395 else
5e942c50
APB
4396 meth = add_method (this_class, flags, meth_name,
4397 build_java_signature (meth));
4398
c2952b01
APB
4399 /* Remember final parameters */
4400 MARK_FINAL_PARMS (meth, orig_arg);
4401
5e942c50
APB
4402 /* Fix the method argument list so we have the argument name
4403 information */
4404 fix_method_argument_names (orig_arg, meth);
4405
4406 /* Register the parameter number and re-install the current line
4407 number */
e04a16fb
AG
4408 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4409 lineno = saved_lineno;
b9f7e36c
APB
4410
4411 /* Register exception specified by the `throws' keyword for
4412 resolution and set the method decl appropriate field to the list.
4413 Note: the grammar ensures that what we get here are class
4414 types. */
4415 if (throws)
4416 {
4417 throws = nreverse (throws);
4418 for (current = throws; current; current = TREE_CHAIN (current))
4419 {
4420 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4421 NULL_TREE, NULL_TREE);
4422 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4423 &TREE_VALUE (current);
4424 }
4425 DECL_FUNCTION_THROWS (meth) = throws;
4426 }
4427
e04a16fb
AG
4428 /* We set the DECL_NAME to ID so we can track the location where
4429 the function was declared. This allow us to report
4430 redefinition error accurately. When method are verified,
4431 DECL_NAME is reinstalled properly (using the content of the
4432 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4433 when Object is being defined. Constructor <init> names will be
4434 reinstalled the same way. */
c2952b01 4435 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4436 DECL_NAME (meth) = id;
22eed1e6
APB
4437
4438 /* Set the flag if we correctly processed a constructor */
4439 if (constructor_ok)
c2952b01
APB
4440 {
4441 DECL_CONSTRUCTOR_P (meth) = 1;
4442 /* Compute and store the number of artificial parameters declared
4443 for this constructor */
4444 for (count = 0, current = TYPE_FIELDS (this_class); current;
4445 current = TREE_CHAIN (current))
4446 if (FIELD_LOCAL_ALIAS (current))
4447 count++;
4448 DECL_FUNCTION_NAP (meth) = count;
4449 }
22eed1e6 4450
5e942c50
APB
4451 /* Eventually set the @deprecated tag flag */
4452 CHECK_DEPRECATED (meth);
4453
7f10c2e2
APB
4454 /* If doing xref, store column and line number information instead
4455 of the line number only. */
4456 if (flag_emit_xref)
4457 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4458
e04a16fb
AG
4459 return meth;
4460}
4461
5e942c50
APB
4462static void
4463fix_method_argument_names (orig_arg, meth)
4464 tree orig_arg, meth;
4465{
4466 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4467 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4468 {
4469 TREE_PURPOSE (arg) = this_identifier_node;
4470 arg = TREE_CHAIN (arg);
4471 }
de4c7b02 4472 while (orig_arg != end_params_node)
5e942c50
APB
4473 {
4474 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4475 orig_arg = TREE_CHAIN (orig_arg);
4476 arg = TREE_CHAIN (arg);
4477 }
4478}
4479
22eed1e6
APB
4480/* Complete the method declaration with METHOD_BODY. */
4481
4482static void
b635eb2f 4483finish_method_declaration (method_body)
22eed1e6
APB
4484 tree method_body;
4485{
79d13333
APB
4486 int flags;
4487
4488 if (!current_function_decl)
4489 return;
4490
4491 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4492
4493 /* 8.4.5 Method Body */
4494 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4495 {
4496 tree wfl = DECL_NAME (current_function_decl);
4497 parse_error_context (wfl,
4498 "%s method `%s' can't have a body defined",
4499 (METHOD_NATIVE (current_function_decl) ?
4500 "Native" : "Abstract"),
4501 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4502 method_body = NULL_TREE;
4503 }
4504 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4505 {
4506 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4507 parse_error_context
4508 (wfl,
4509 "Non native and non abstract method `%s' must have a body defined",
4510 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4511 method_body = NULL_TREE;
4512 }
4513
2c56429a
APB
4514 if (flag_emit_class_files && method_body
4515 && TREE_CODE (method_body) == NOP_EXPR
4516 && TREE_TYPE (current_function_decl)
4517 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4518 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4519
22eed1e6
APB
4520 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4521 maybe_absorb_scoping_blocks ();
4522 /* Exit function's body */
4523 exit_block ();
4524 /* Merge last line of the function with first line, directly in the
4525 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4526 if (!flag_emit_xref)
4527 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4528
4529 /* Since function's argument's list are shared, reset the
4530 ARG_FINAL_P parameter that might have been set on some of this
4531 function parameters. */
4532 UNMARK_FINAL_PARMS (current_function_decl);
4533
f099f336
APB
4534 /* So we don't have an irrelevant function declaration context for
4535 the next static block we'll see. */
4536 current_function_decl = NULL_TREE;
22eed1e6
APB
4537}
4538
4539/* Build a an error message for constructor circularity errors. */
4540
4541static char *
4542constructor_circularity_msg (from, to)
4543 tree from, to;
4544{
4545 static char string [4096];
c2e3db92 4546 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4547 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4548 free (t);
4549 return string;
4550}
4551
4552/* Verify a circular call to METH. Return 1 if an error is found, 0
4553 otherwise. */
4554
4555static int
4556verify_constructor_circularity (meth, current)
4557 tree meth, current;
4558{
4559 static tree list = NULL_TREE;
4560 tree c;
4561 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4562 {
4563 if (TREE_VALUE (c) == meth)
4564 {
4565 char *t;
4566 if (list)
4567 {
4568 tree liste;
4569 list = nreverse (list);
4570 for (liste = list; liste; liste = TREE_CHAIN (liste))
4571 {
4572 parse_error_context
c63b98cd 4573 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4574 constructor_circularity_msg
4575 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4576 java_error_count--;
4577 }
4578 }
c2e3db92 4579 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4580 parse_error_context (TREE_PURPOSE (c),
4581 "%s: recursive invocation of constructor `%s'",
4582 constructor_circularity_msg (current, meth), t);
4583 free (t);
4584 list = NULL_TREE;
4585 return 1;
4586 }
4587 }
4588 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4589 {
4590 list = tree_cons (c, current, list);
4591 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4592 return 1;
4593 list = TREE_CHAIN (list);
4594 }
4595 return 0;
4596}
4597
e04a16fb
AG
4598/* Check modifiers that can be declared but exclusively */
4599
4600static void
4601check_modifiers_consistency (flags)
4602 int flags;
4603{
4604 int acc_count = 0;
4605 tree cl = NULL_TREE;
4606
e0fc4118
TT
4607 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4608 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4609 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4610 if (acc_count > 1)
4611 parse_error_context
e0fc4118
TT
4612 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4613
4614 acc_count = 0;
4615 cl = NULL_TREE;
4616 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK - PUBLIC_TK,
4617 acc_count, cl);
4618 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK - PUBLIC_TK,
4619 acc_count, cl);
4620 if (acc_count > 1)
4621 parse_error_context (cl,
4622 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4623}
4624
4625/* Check the methode header METH for abstract specifics features */
4626
4627static void
4628check_abstract_method_header (meth)
4629 tree meth;
4630{
4631 int flags = get_access_flags_from_decl (meth);
4632 /* DECL_NAME might still be a WFL node */
c877974e 4633 tree name = GET_METHOD_NAME (meth);
e04a16fb 4634
2884c41e
KG
4635 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4636 ACC_ABSTRACT, "abstract method",
4637 IDENTIFIER_POINTER (name));
4638 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4639 ACC_PUBLIC, "abstract method",
4640 IDENTIFIER_POINTER (name));
e04a16fb
AG
4641
4642 check_modifiers ("Illegal modifier `%s' for interface method",
4643 flags, INTERFACE_METHOD_MODIFIERS);
4644}
4645
4646/* Create a FUNCTION_TYPE node and start augmenting it with the
4647 declared function arguments. Arguments type that can't be resolved
4648 are left as they are, but the returned node is marked as containing
4649 incomplete types. */
4650
4651static tree
4652method_declarator (id, list)
4653 tree id, list;
4654{
4655 tree arg_types = NULL_TREE, current, node;
4656 tree meth = make_node (FUNCTION_TYPE);
4657 jdep *jdep;
e04a16fb
AG
4658
4659 patch_stage = JDEP_NO_PATCH;
c2952b01
APB
4660
4661 /* If we're dealing with an inner class constructor, we hide the
4662 this$<n> decl in the name field of its parameter declaration. We
4663 also might have to hide the outer context local alias
4664 initializers. Not done when the class is a toplevel class. */
4665 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4666 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4667 {
4668 tree aliases_list, type, thisn;
4669 /* First the aliases, linked to the regular parameters */
4670 aliases_list =
4671 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4672 TREE_TYPE (GET_CPC ()),
4673 NULL_TREE, NULL);
4674 list = chainon (nreverse (aliases_list), list);
4675
4676 /* Then this$<n> */
4677 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4678 thisn = build_current_thisn (TYPE_NAME (GET_CPC ()));
4679 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4680 list);
4681 }
e04a16fb
AG
4682
4683 for (current = list; current; current = TREE_CHAIN (current))
4684 {
c583dd46 4685 int must_chain = 0;
e04a16fb
AG
4686 tree wfl_name = TREE_PURPOSE (current);
4687 tree type = TREE_VALUE (current);
4688 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4689 tree already, arg_node;
4690 tree type_wfl = NULL_TREE;
23a79c61 4691 tree real_type;
c583dd46
APB
4692
4693 /* Obtain a suitable type for resolution, if necessary */
4694 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4695
4696 /* Process NAME, as it may specify extra dimension(s) for it */
4697 type = build_array_from_name (type, type_wfl, name, &name);
4698 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4699
23a79c61
APB
4700 real_type = GET_REAL_TYPE (type);
4701 if (TREE_CODE (real_type) == RECORD_TYPE)
4702 {
4703 real_type = promote_type (real_type);
4704 if (TREE_CODE (type) == TREE_LIST)
4705 TREE_PURPOSE (type) = real_type;
4706 }
5e942c50 4707
e04a16fb
AG
4708 /* Check redefinition */
4709 for (already = arg_types; already; already = TREE_CHAIN (already))
4710 if (TREE_PURPOSE (already) == name)
4711 {
781b0558
KG
4712 parse_error_context
4713 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4714 IDENTIFIER_POINTER (name),
e04a16fb
AG
4715 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4716 break;
4717 }
4718
4719 /* If we've an incomplete argument type, we know there is a location
4720 to patch when the type get resolved, later. */
4721 jdep = NULL;
c583dd46 4722 if (must_chain)
e04a16fb 4723 {
c583dd46
APB
4724 patch_stage = JDEP_METHOD;
4725 type = register_incomplete_type (patch_stage,
4726 type_wfl, wfl_name, type);
4727 jdep = CLASSD_LAST (ctxp->classd_list);
4728 JDEP_MISC (jdep) = id;
e04a16fb 4729 }
c583dd46 4730
c2952b01 4731 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4732 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4733 /* Remeber arguments declared final. */
4734 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4735
e04a16fb
AG
4736 if (jdep)
4737 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4738 TREE_CHAIN (arg_node) = arg_types;
4739 arg_types = arg_node;
4740 }
de4c7b02 4741 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4742 node = build_tree_list (id, meth);
4743 return node;
4744}
4745
4746static int
4747unresolved_type_p (wfl, returned)
4748 tree wfl;
4749 tree *returned;
4750
4751{
4752 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4753 {
e04a16fb 4754 if (returned)
165f37bc
APB
4755 {
4756 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4757 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4758 *returned = TREE_TYPE (decl);
4759 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4760 *returned = TREE_TYPE (GET_CPC ());
4761 else
4762 *returned = NULL_TREE;
4763 }
e04a16fb
AG
4764 return 1;
4765 }
4766 if (returned)
4767 *returned = wfl;
4768 return 0;
4769}
4770
4771/* From NAME, build a qualified identifier node using the
4772 qualification from the current package definition. */
4773
4774static tree
c2952b01
APB
4775parser_qualified_classname (is_static, name)
4776 int is_static;
e04a16fb
AG
4777 tree name;
4778{
c2952b01
APB
4779 tree nested_class_name;
4780
4781 if (!is_static
4782 && (nested_class_name = maybe_make_nested_class_name (name)))
4783 return nested_class_name;
4784
e04a16fb 4785 if (ctxp->package)
c2952b01 4786 return merge_qualified_name (ctxp->package, name);
e04a16fb 4787 else
c2952b01 4788 return name;
e04a16fb
AG
4789}
4790
4791/* Called once the type a interface extends is resolved. Returns 0 if
4792 everything is OK. */
4793
4794static int
4795parser_check_super_interface (super_decl, this_decl, this_wfl)
4796 tree super_decl, this_decl, this_wfl;
4797{
4798 tree super_type = TREE_TYPE (super_decl);
4799
4800 /* Has to be an interface */
c2952b01 4801 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4802 {
4803 parse_error_context
4804 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4805 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4806 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4807 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4808 "interface" : "class"),
4809 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4810 return 1;
4811 }
4812
4813 /* Check scope: same package OK, other package: OK if public */
4814 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4815 return 1;
4816
4817 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4818 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4819 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4820 return 0;
4821}
4822
4823/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4824 0 if everthing is OK. */
4825
4826static int
4827parser_check_super (super_decl, this_decl, wfl)
4828 tree super_decl, this_decl, wfl;
4829{
e04a16fb
AG
4830 tree super_type = TREE_TYPE (super_decl);
4831
4832 /* SUPER should be a CLASS (neither an array nor an interface) */
4833 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4834 {
4835 parse_error_context
4836 (wfl, "Class `%s' can't subclass %s `%s'",
4837 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4838 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4839 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4840 return 1;
4841 }
4842
4843 if (CLASS_FINAL (TYPE_NAME (super_type)))
4844 {
4845 parse_error_context (wfl, "Can't subclass final classes: %s",
4846 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4847 return 1;
4848 }
4849
4850 /* Check scope: same package OK, other package: OK if public */
4851 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4852 return 1;
4853
4854 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4855 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4856 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4857 return 0;
4858}
4859
4860/* Create a new dependency list and link it (in a LIFO manner) to the
4861 CTXP list of type dependency list. */
4862
4863static void
4864create_jdep_list (ctxp)
4865 struct parser_ctxt *ctxp;
4866{
23a79c61 4867 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4868 new->first = new->last = NULL;
4869 new->next = ctxp->classd_list;
4870 ctxp->classd_list = new;
4871}
4872
4873static jdeplist *
4874reverse_jdep_list (ctxp)
4875 struct parser_ctxt *ctxp;
4876{
4877 register jdeplist *prev = NULL, *current, *next;
4878 for (current = ctxp->classd_list; current; current = next)
4879 {
4880 next = current->next;
4881 current->next = prev;
4882 prev = current;
4883 }
4884 return prev;
4885}
4886
23a79c61
APB
4887/* Create a fake pointer based on the ID stored in
4888 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4889 registered again. */
e04a16fb
AG
4890
4891static tree
23a79c61
APB
4892obtain_incomplete_type (type_name)
4893 tree type_name;
e04a16fb 4894{
23a79c61
APB
4895 tree ptr, name;
4896
4897 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4898 name = EXPR_WFL_NODE (type_name);
4899 else if (INCOMPLETE_TYPE_P (type_name))
4900 name = TYPE_NAME (type_name);
4901 else
4902 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4903
4904 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4905 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4906 break;
4907
4908 if (!ptr)
4909 {
e04a16fb 4910 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4911 BUILD_PTR_FROM_NAME (ptr, name);
4912 layout_type (ptr);
e04a16fb
AG
4913 pop_obstacks ();
4914 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4915 ctxp->incomplete_class = ptr;
4916 }
4917
4918 return ptr;
4919}
4920
4921/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4922 non NULL instead of computing a new fake type based on WFL. The new
4923 dependency is inserted in the current type dependency list, in FIFO
4924 manner. */
4925
4926static tree
4927register_incomplete_type (kind, wfl, decl, ptr)
4928 int kind;
4929 tree wfl, decl, ptr;
4930{
23a79c61 4931 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4932
e04a16fb
AG
4933 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4934 ptr = obtain_incomplete_type (wfl);
4935
4936 JDEP_KIND (new) = kind;
4937 JDEP_DECL (new) = decl;
4938 JDEP_SOLV (new) = ptr;
4939 JDEP_WFL (new) = wfl;
4940 JDEP_CHAIN (new) = NULL;
4941 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4942 /* For some dependencies, set the enclosing class of the current
4943 class to be the enclosing context */
4944 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4945 && GET_ENCLOSING_CPC ())
4946 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4947 else
324ed8fd 4948 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4949 JDEP_GET_PATCH (new) = (tree *)NULL;
4950
4951 JDEP_INSERT (ctxp->classd_list, new);
4952
4953 return ptr;
4954}
4955
4956void
4957java_check_circular_reference ()
4958{
4959 tree current;
4960 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4961 {
4962 tree type = TREE_TYPE (current);
e920ebc9 4963 if (CLASS_INTERFACE (current))
e04a16fb
AG
4964 {
4965 /* Check all interfaces this class extends */
4966 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4967 int n, i;
4968
4969 if (!basetype_vec)
4970 return;
4971 n = TREE_VEC_LENGTH (basetype_vec);
4972 for (i = 0; i < n; i++)
4973 {
4974 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4975 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4976 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4977 parse_error_context (lookup_cl (current),
4978 "Cyclic interface inheritance");
4979 }
4980 }
4981 else
4982 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4983 parse_error_context (lookup_cl (current),
c2952b01
APB
4984 "Cyclic class inheritance%s",
4985 (cyclic_inheritance_report ?
4986 cyclic_inheritance_report : ""));
4987 }
4988}
4989
4990/* Augment the parameter list PARM with parameters crafted to
4991 initialize outer context locals aliases. Through ARTIFICIAL, a
4992 count is kept of the number of crafted parameters. MODE governs
4993 what eventually gets created: something suitable for a function
4994 creation or a function invocation, either the constructor or
4995 $finit$. */
4996
4997static tree
4998build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
4999 int mode;
5000 tree class_type, parm;
5001 int *artificial;
5002{
5003 tree field;
5004 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5005 if (FIELD_LOCAL_ALIAS (field))
5006 {
5007 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5008 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5009
5010 switch (mode)
5011 {
5012 case AIPL_FUNCTION_DECLARATION:
5013 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5014 purpose = build_wfl_node (get_identifier (buffer));
5015 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5016 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5017 else
5018 value = TREE_TYPE (field);
5019 break;
5020
5021 case AIPL_FUNCTION_CREATION:
5022 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5023 purpose = get_identifier (buffer);
5024 value = TREE_TYPE (field);
5025 break;
5026
5027 case AIPL_FUNCTION_FINIT_INVOCATION:
5028 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5029 /* Now, this is wrong. purpose should always be the NAME
5030 of something and value its matching value (decl, type,
5031 etc...) FIXME -- but there is a lot to fix. */
5032
5033 /* When invoked for this kind of operation, we already
5034 know whether a field is used or not. */
5035 purpose = TREE_TYPE (field);
5036 value = build_wfl_node (get_identifier (buffer));
5037 break;
5038
5039 case AIPL_FUNCTION_CTOR_INVOCATION:
5040 /* There are two case: the constructor invokation happends
5041 outside the local inner, in which case, locales from the outer
5042 context are directly used.
5043
5044 Otherwise, we fold to using the alias directly. */
5045 if (class_type == current_class)
5046 value = field;
5047 else
5048 {
5049 name = get_identifier (&buffer[4]);
5050 value = IDENTIFIER_LOCAL_VALUE (name);
5051 }
5052 break;
5053 }
5054 parm = tree_cons (purpose, value, parm);
5055 if (artificial)
5056 *artificial +=1;
5057 }
5058 return parm;
5059}
5060
5061/* Craft a constructor for CLASS_DECL -- what we should do when none
5062 where found. ARGS is non NULL when a special signature must be
5063 enforced. This is the case for anonymous classes. */
5064
5065static void
5066craft_constructor (class_decl, args)
5067 tree class_decl, args;
5068{
5069 tree class_type = TREE_TYPE (class_decl);
5070 tree parm = NULL_TREE;
5071 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5072 ACC_PUBLIC : 0);
5073 int i = 0, artificial = 0;
5074 tree decl, ctor_name;
5075 char buffer [80];
5076
5077 push_obstacks (&permanent_obstack, &permanent_obstack);
5078
5079 /* The constructor name is <init> unless we're dealing with an
5080 anonymous class, in which case the name will be fixed after having
5081 be expanded. */
5082 if (ANONYMOUS_CLASS_P (class_type))
5083 ctor_name = DECL_NAME (class_decl);
5084 else
5085 ctor_name = init_identifier_node;
5086
5087 /* If we're dealing with an inner class constructor, we hide the
5088 this$<n> decl in the name field of its parameter declaration. */
5089 if (PURE_INNER_CLASS_TYPE_P (class_type))
5090 {
5091 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5092 parm = tree_cons (build_current_thisn (class_type),
5093 build_pointer_type (type), parm);
5094
5095 /* Some more arguments to be hidden here. The values of the local
5096 variables of the outer context that the inner class needs to see. */
5097 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5098 class_type, parm,
5099 &artificial);
5100 }
5101
5102 /* Then if there are any args to be enforced, enforce them now */
5103 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5104 {
5105 sprintf (buffer, "parm%d", i++);
5106 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5107 }
c2952b01
APB
5108
5109 CRAFTED_PARAM_LIST_FIXUP (parm);
5110 decl = create_artificial_method (class_type, flags, void_type_node,
5111 ctor_name, parm);
5112 fix_method_argument_names (parm, decl);
5113 /* Now, mark the artificial parameters. */
5114 DECL_FUNCTION_NAP (decl) = artificial;
5115
5116 pop_obstacks ();
5117 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5118}
5119
c2952b01 5120
e920ebc9
APB
5121/* Fix the constructors. This will be called right after circular
5122 references have been checked. It is necessary to fix constructors
5123 early even if no code generation will take place for that class:
5124 some generated constructor might be required by the class whose
5125 compilation triggered this one to be simply loaded. */
5126
5127void
5128java_fix_constructors ()
5129{
5130 tree current;
5131
5132 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5133 {
e920ebc9
APB
5134 tree class_type = TREE_TYPE (current);
5135 int saw_ctor = 0;
c2952b01
APB
5136 tree decl;
5137
5138 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5139 continue;
e920ebc9
APB
5140
5141 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5142 {
5143 if (DECL_CONSTRUCTOR_P (decl))
5144 {
5145 fix_constructors (decl);
5146 saw_ctor = 1;
5147 }
5148 }
5149
c2952b01
APB
5150 /* Anonymous class constructor can't be generated that early. */
5151 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5152 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5153 }
5154}
5155
23a79c61
APB
5156/* safe_layout_class just makes sure that we can load a class without
5157 disrupting the current_class, input_file, lineno, etc, information
5158 about the class processed currently. */
5159
e04a16fb
AG
5160void
5161safe_layout_class (class)
5162 tree class;
5163{
5164 tree save_current_class = current_class;
3b304f5b 5165 const char *save_input_filename = input_filename;
e04a16fb 5166 int save_lineno = lineno;
5e942c50 5167
e04a16fb 5168 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5169
e04a16fb
AG
5170 layout_class (class);
5171 pop_obstacks ();
5e942c50 5172
e04a16fb
AG
5173 current_class = save_current_class;
5174 input_filename = save_input_filename;
5175 lineno = save_lineno;
5176 CLASS_LOADED_P (class) = 1;
5177}
5178
5179static tree
5180jdep_resolve_class (dep)
5181 jdep *dep;
5182{
5183 tree decl;
5184
23a79c61
APB
5185 if (JDEP_RESOLVED_P (dep))
5186 decl = JDEP_RESOLVED_DECL (dep);
5187 else
e04a16fb 5188 {
c2952b01 5189 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5190 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5191 JDEP_RESOLVED (dep, decl);
5192 }
23a79c61 5193
e04a16fb 5194 if (!decl)
23a79c61
APB
5195 complete_class_report_errors (dep);
5196
e04a16fb
AG
5197 return decl;
5198}
5199
5200/* Complete unsatisfied class declaration and their dependencies */
5201
5202void
5203java_complete_class ()
5204{
e04a16fb
AG
5205 tree cclass;
5206 jdeplist *cclassd;
5207 int error_found;
b67d701b 5208 tree type;
e04a16fb
AG
5209
5210 push_obstacks (&permanent_obstack, &permanent_obstack);
5211
5212 /* Process imports and reverse the import on demand list */
5213 process_imports ();
5214 if (ctxp->import_demand_list)
5215 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
5216
5217 /* Rever things so we have the right order */
5218 ctxp->class_list = nreverse (ctxp->class_list);
5219 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5220
e04a16fb
AG
5221 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5222 cclass && cclassd;
5223 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5224 {
5225 jdep *dep;
5226 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5227 {
5228 tree decl;
e04a16fb
AG
5229 if (!(decl = jdep_resolve_class (dep)))
5230 continue;
5231
5232 /* Now it's time to patch */
5233 switch (JDEP_KIND (dep))
5234 {
5235 case JDEP_SUPER:
5236 /* Simply patch super */
5237 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5238 continue;
5239 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5240 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5241 break;
5242
5243 case JDEP_FIELD:
5244 {
5245 /* We do part of the job done in add_field */
5246 tree field_decl = JDEP_DECL (dep);
5247 tree field_type = TREE_TYPE (decl);
5248 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5249 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5250 field_type = promote_type (field_type);
5251 pop_obstacks ();
5252 TREE_TYPE (field_decl) = field_type;
5e942c50
APB
5253 DECL_ALIGN (field_decl) = 0;
5254 layout_decl (field_decl, 0);
e04a16fb
AG
5255 SOURCE_FRONTEND_DEBUG
5256 (("Completed field/var decl `%s' with `%s'",
5257 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5258 IDENTIFIER_POINTER (DECL_NAME (decl))));
5259 break;
5260 }
5261 case JDEP_METHOD: /* We start patching a method */
5262 case JDEP_METHOD_RETURN:
5263 error_found = 0;
5264 while (1)
5265 {
5266 if (decl)
5267 {
b67d701b
PB
5268 type = TREE_TYPE(decl);
5269 if (TREE_CODE (type) == RECORD_TYPE)
5270 type = promote_type (type);
e04a16fb
AG
5271 JDEP_APPLY_PATCH (dep, type);
5272 SOURCE_FRONTEND_DEBUG
5273 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5274 "Completing fct `%s' with ret type `%s'":
5275 "Completing arg `%s' with type `%s'"),
5276 IDENTIFIER_POINTER (EXPR_WFL_NODE
5277 (JDEP_DECL_WFL (dep))),
5278 IDENTIFIER_POINTER (DECL_NAME (decl))));
5279 }
5280 else
5281 error_found = 1;
5282 dep = JDEP_CHAIN (dep);
5283 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5284 break;
5285 else
5286 decl = jdep_resolve_class (dep);
5287 }
5288 if (!error_found)
5289 {
5290 tree mdecl = JDEP_DECL (dep), signature;
5291 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5292 /* Recompute and reset the signature, check first that
5293 all types are now defined. If they're not,
5294 dont build the signature. */
5295 if (check_method_types_complete (mdecl))
5296 {
5297 signature = build_java_signature (TREE_TYPE (mdecl));
5298 set_java_signature (TREE_TYPE (mdecl), signature);
5299 }
e04a16fb
AG
5300 pop_obstacks ();
5301 }
5302 else
5303 continue;
5304 break;
5305
5306 case JDEP_INTERFACE:
5307 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5308 JDEP_WFL (dep)))
5309 continue;
5310 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5311 break;
5312
b67d701b 5313 case JDEP_PARM:
e04a16fb 5314 case JDEP_VARIABLE:
b67d701b
PB
5315 type = TREE_TYPE(decl);
5316 if (TREE_CODE (type) == RECORD_TYPE)
5317 type = promote_type (type);
5318 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5319 break;
5320
5321 case JDEP_TYPE:
5322 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5323 SOURCE_FRONTEND_DEBUG
5324 (("Completing a random type dependency on a '%s' node",
5325 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5326 break;
5327
b9f7e36c 5328 case JDEP_EXCEPTION:
c877974e
APB
5329 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5330 SOURCE_FRONTEND_DEBUG
5331 (("Completing `%s' `throws' argument node",
5332 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5333 break;
5334
c2952b01
APB
5335 case JDEP_ANONYMOUS:
5336 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5337 break;
5338
e04a16fb 5339 default:
0a2138e2
APB
5340 fatal ("Can't handle patch code %d - java_complete_class",
5341 JDEP_KIND (dep));
e04a16fb
AG
5342 }
5343 }
5344 }
5345 pop_obstacks ();
5346 return;
5347}
5348
5349/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5350 array. */
5351
5352static tree
c2952b01
APB
5353resolve_class (enclosing, class_type, decl, cl)
5354 tree enclosing, class_type, decl, cl;
e04a16fb 5355{
49f48c71
KG
5356 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5357 const char *base = name;
78d21f92
PB
5358 tree resolved_type = TREE_TYPE (class_type);
5359 tree resolved_type_decl;
e04a16fb 5360
78d21f92
PB
5361 if (resolved_type != NULL_TREE)
5362 {
5363 tree resolved_type_decl = TYPE_NAME (resolved_type);
5364 if (resolved_type_decl == NULL_TREE
5365 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5366 {
5367 resolved_type_decl = build_decl (TYPE_DECL,
5368 TYPE_NAME (class_type),
5369 resolved_type);
5370 }
5371 return resolved_type_decl;
5372 }
5373
e04a16fb
AG
5374 /* 1- Check to see if we have an array. If true, find what we really
5375 want to resolve */
5376 while (name[0] == '[')
5377 name++;
5378 if (base != name)
5379 TYPE_NAME (class_type) = get_identifier (name);
5380
5381 /* 2- Resolve the bare type */
c2952b01
APB
5382 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5383 decl, cl)))
e04a16fb
AG
5384 return NULL_TREE;
5385 resolved_type = TREE_TYPE (resolved_type_decl);
5386
5387 /* 3- If we have and array, reconstruct the array down to its nesting */
5388 if (base != name)
5389 {
5390 while (base != name)
5391 {
5392 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5393 resolved_type = promote_type (resolved_type);
5394 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5395 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5396 name--;
5397 }
5398 /* Build a fake decl for this, since this is what is expected to
5399 be returned. */
5400 resolved_type_decl =
5401 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5402 /* Figure how those two things are important for error report. FIXME */
5403 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5404 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5405 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5406 }
78d21f92 5407 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5408 return resolved_type_decl;
5409}
5410
5411/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5412 are used to report error messages. */
5413
78d21f92 5414tree
c2952b01
APB
5415do_resolve_class (enclosing, class_type, decl, cl)
5416 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5417{
5418 tree new_class_decl;
5419 tree original_name = NULL_TREE;
5420
5421 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5422 its is changed by find_in_imports{_on_demand} */
5423
c2952b01
APB
5424 /* 0- Search in the current class as an inner class */
5425
5426 /* Maybe some code here should be added to load the class or
5427 something, at least if the class isn't an inner class and ended
5428 being loaded from class file. FIXME. */
a40d21da
APB
5429 while (enclosing)
5430 {
5431 tree name;
5432
5433 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5434 return new_class_decl;
5435
5436 /* Now go to the upper classes, bail out if necessary. */
5437 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5438 if (!enclosing || enclosing == object_type_node)
5439 break;
5440
5441 if (TREE_CODE (enclosing) == RECORD_TYPE)
5442 {
5443 enclosing = TYPE_NAME (enclosing);
5444 continue;
5445 }
5446
5447 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5448 {
5449 BUILD_PTR_FROM_NAME (name, enclosing);
5450 }
5451 else
5452 name = enclosing;
5453 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5454 }
c2952b01 5455
e04a16fb
AG
5456 /* 1- Check for the type in single imports */
5457 if (find_in_imports (class_type))
5458 return NULL_TREE;
5459
5460 /* 2- And check for the type in the current compilation unit. If it fails,
ee07f4f4 5461 try with a name qualified with the package name we've seen so far */
e04a16fb
AG
5462 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5463 {
5464 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5465 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5466 load_class (TYPE_NAME (class_type), 0);
5467 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5468 }
5469
5470 original_name = TYPE_NAME (class_type);
ee07f4f4 5471 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5472 {
ee07f4f4
APB
5473 tree package;
5474 for (package = package_list; package; package = TREE_CHAIN (package))
c2952b01
APB
5475 {
5476 tree new_qualified;
5477
5478 new_qualified = merge_qualified_name (TREE_PURPOSE (package),
5479 original_name);
5480 TYPE_NAME (class_type) = new_qualified;
5481 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5482 if (!new_class_decl)
5483 load_class (TYPE_NAME (class_type), 0);
5484 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5485 if (new_class_decl)
5486 {
5487 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5488 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5489 load_class (TYPE_NAME (class_type), 0);
5490 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5491 }
bc3ca41b
PB
5492 }
5493 }
c2952b01 5494
e04a16fb
AG
5495 TYPE_NAME (class_type) = original_name;
5496
5497 /* 3- Check an other compilation unit that bears the name of type */
5498 load_class (TYPE_NAME (class_type), 0);
5499 if (check_pkg_class_access (TYPE_NAME (class_type),
5500 (cl ? cl : lookup_cl (decl))))
5501 return NULL_TREE;
5502
5503 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5504 return new_class_decl;
5505
5506 /* 4- Check the import on demands. Don't allow bar.baz to be
5507 imported from foo.* */
5508 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5509 if (find_in_imports_on_demand (class_type))
5510 return NULL_TREE;
5511
5512 /* 5- Last call for a resolution */
5513 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5514}
5515
5516/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5517 parsed class). Return a decl node. This function is meant to be
5518 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5519
5520static tree
c877974e
APB
5521resolve_and_layout (something, cl)
5522 tree something;
e04a16fb
AG
5523 tree cl;
5524{
c877974e
APB
5525 tree decl;
5526
23a79c61
APB
5527 /* Don't do that on the current class */
5528 if (something == current_class)
5529 return TYPE_NAME (current_class);
c877974e 5530
23a79c61 5531 /* Don't do anything for void and other primitive types */
c877974e
APB
5532 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5533 return NULL_TREE;
5534
23a79c61
APB
5535 /* Pointer types can be reall pointer types or fake pointers. When
5536 finding a real pointer, recheck for primitive types */
5537 if (TREE_CODE (something) == POINTER_TYPE)
5538 {
5539 if (TREE_TYPE (something))
5540 {
5541 something = TREE_TYPE (something);
5542 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5543 return NULL_TREE;
5544 }
5545 else
5546 something = TYPE_NAME (something);
5547 }
5548
5549 /* Don't do anything for arrays of primitive types */
5550 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5551 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5552 return NULL_TREE;
5553
c2952b01
APB
5554 /* Something might be a WFL */
5555 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5556 something = EXPR_WFL_NODE (something);
5557
5558 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5559 TYPE_DECL or a real TYPE */
5560 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5561 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5562 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5563
23a79c61
APB
5564 if (!(decl = resolve_no_layout (something, cl)))
5565 return NULL_TREE;
5566
5567 /* Resolve and layout if necessary */
5568 layout_class_methods (TREE_TYPE (decl));
7705e9db
APB
5569 /* Check methods, but only once */
5570 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5571 && !CLASS_LOADED_P (TREE_TYPE (decl)))
23a79c61
APB
5572 CHECK_METHODS (decl);
5573 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
e04a16fb 5574 safe_layout_class (TREE_TYPE (decl));
23a79c61 5575
e04a16fb
AG
5576 return decl;
5577}
5578
5579/* Resolve a class, returns its decl but doesn't perform any
5580 layout. The current parsing context is saved and restored */
5581
5582static tree
5583resolve_no_layout (name, cl)
5584 tree name, cl;
5585{
5586 tree ptr, decl;
5587 BUILD_PTR_FROM_NAME (ptr, name);
5588 java_parser_context_save_global ();
c2952b01 5589 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5590 java_parser_context_restore_global ();
5591
5592 return decl;
5593}
5594
23a79c61
APB
5595/* Called when reporting errors. Skip leader '[' in a complex array
5596 type description that failed to be resolved. */
e04a16fb 5597
49f48c71 5598static const char *
e04a16fb 5599purify_type_name (name)
49f48c71 5600 const char *name;
e04a16fb
AG
5601{
5602 while (*name && *name == '[')
5603 name++;
5604 return name;
5605}
5606
5607/* The type CURRENT refers to can't be found. We print error messages. */
5608
5609static void
5610complete_class_report_errors (dep)
5611 jdep *dep;
5612{
49f48c71 5613 const char *name;
23a79c61
APB
5614
5615 if (!JDEP_WFL (dep))
5616 return;
5617
5618 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5619 switch (JDEP_KIND (dep))
5620 {
5621 case JDEP_SUPER:
5622 parse_error_context
5623 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5624 purify_type_name (name),
e04a16fb
AG
5625 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5626 break;
5627 case JDEP_FIELD:
5628 parse_error_context
5629 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5630 purify_type_name (name),
e04a16fb
AG
5631 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5632 break;
5633 case JDEP_METHOD: /* Covers arguments */
5634 parse_error_context
781b0558 5635 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5636 purify_type_name (name),
e04a16fb
AG
5637 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5638 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5639 break;
5640 case JDEP_METHOD_RETURN: /* Covers return type */
5641 parse_error_context
781b0558 5642 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5643 purify_type_name (name),
e04a16fb
AG
5644 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5645 break;
5646 case JDEP_INTERFACE:
5647 parse_error_context
5648 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5649 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5650 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5651 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5652 break;
5653 case JDEP_VARIABLE:
5654 parse_error_context
781b0558 5655 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5656 purify_type_name (IDENTIFIER_POINTER
5657 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5658 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5659 break;
b9f7e36c
APB
5660 case JDEP_EXCEPTION: /* As specified by `throws' */
5661 parse_error_context
5662 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5663 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5664 break;
0a2138e2
APB
5665 default:
5666 /* Fix for -Wall. Just break doing nothing. The error will be
5667 caught later */
5668 break;
e04a16fb
AG
5669 }
5670}
5671
22eed1e6
APB
5672/* Return a static string containing the DECL prototype string. If
5673 DECL is a constructor, use the class name instead of the form
5674 <init> */
5675
49f48c71 5676static const char *
22eed1e6
APB
5677get_printable_method_name (decl)
5678 tree decl;
5679{
49f48c71 5680 const char *to_return;
9ee9b555 5681 tree name = NULL_TREE;
22eed1e6
APB
5682
5683 if (DECL_CONSTRUCTOR_P (decl))
5684 {
5685 name = DECL_NAME (decl);
5e942c50 5686 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5687 }
5688
5689 to_return = lang_printable_name (decl, 0);
5690 if (DECL_CONSTRUCTOR_P (decl))
5691 DECL_NAME (decl) = name;
5692
5693 return to_return;
5694}
5695
5e942c50
APB
5696/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5697 nevertheless needs to be verfied, 1 otherwise. */
5698
5699static int
5700reset_method_name (method)
5701 tree method;
5702{
c2952b01 5703 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5704 {
5705 /* NAME is just the plain name when Object is being defined */
5706 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5707 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5708 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5709 return 0;
5710 }
5711 else
5712 return 1;
5713}
5714
c877974e
APB
5715/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5716
5717tree
5718java_get_real_method_name (method_decl)
5719 tree method_decl;
5720{
5721 tree method_name = DECL_NAME (method_decl);
5722 if (DECL_CONSTRUCTOR_P (method_decl))
5723 return init_identifier_node;
82371d41
APB
5724
5725 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5726 and still can be a constructor. FIXME */
5727
23a79c61
APB
5728 /* Don't confuse method only bearing the name of their class as
5729 constructors */
82371d41
APB
5730 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5731 && ctxp
c2952b01 5732 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5733 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5734 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5735 return init_identifier_node;
5736 else
5737 return EXPR_WFL_NODE (method_name);
5738}
5739
22eed1e6
APB
5740/* Track method being redefined inside the same class. As a side
5741 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5742 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5743
e04a16fb
AG
5744static int
5745check_method_redefinition (class, method)
5746 tree class, method;
5747{
5748 tree redef, name;
5749 tree cl = DECL_NAME (method);
c3f2a476 5750 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
5751 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5752 fixed and checked */
5e942c50
APB
5753
5754 /* Reset the method name before running the check. If it returns 1,
5755 the method doesn't need to be verified with respect to method
5756 redeclaration and we return 0 */
5757 if (reset_method_name (method))
e04a16fb 5758 return 0;
5e942c50
APB
5759
5760 name = DECL_NAME (method);
e04a16fb
AG
5761 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5762 {
c3f2a476 5763 if (redef == method)
e04a16fb 5764 break;
c3f2a476
APB
5765 if (DECL_NAME (redef) == name
5766 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5767 {
22eed1e6
APB
5768 parse_error_context
5769 (cl, "Duplicate %s declaration `%s'",
5770 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5771 get_printable_method_name (redef));
e04a16fb
AG
5772 return 1;
5773 }
5774 }
5775 return 0;
5776}
5777
d77613be
APB
5778static void
5779check_abstract_method_definitions (do_interface, class_decl, type)
5780 int do_interface;
5781 tree class_decl, type;
5782{
5783 tree class = TREE_TYPE (class_decl);
5784 tree method, end_type;
5785
5786 end_type = (do_interface ? object_type_node : type);
5787 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5788 {
5789 tree other_super, other_method, method_sig, method_name;
5790 int found = 0;
165f37bc 5791 int end_type_reached = 0;
d77613be
APB
5792
5793 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5794 continue;
5795
5796 /* Now verify that somewhere in between TYPE and CLASS,
5797 abstract method METHOD gets a non abstract definition
5798 that is inherited by CLASS. */
5799
5800 method_sig = build_java_signature (TREE_TYPE (method));
5801 method_name = DECL_NAME (method);
5802 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5803 method_name = EXPR_WFL_NODE (method_name);
5804
165f37bc
APB
5805 other_super = class;
5806 do {
5807 if (other_super == end_type)
5808 end_type_reached = 1;
5809
5810 /* Method search */
5811 for (other_method = TYPE_METHODS (other_super); other_method;
5812 other_method = TREE_CHAIN (other_method))
5813 {
5814 tree s = build_java_signature (TREE_TYPE (other_method));
5815 tree other_name = DECL_NAME (other_method);
5816
5817 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5818 other_name = EXPR_WFL_NODE (other_name);
5819 if (!DECL_CLINIT_P (other_method)
5820 && !DECL_CONSTRUCTOR_P (other_method)
5821 && method_name == other_name && method_sig == s)
5822 {
5823 found = 1;
5824 break;
5825 }
5826 }
5827 other_super = CLASSTYPE_SUPER (other_super);
5828 } while (!end_type_reached);
5829
d77613be
APB
5830 /* Report that abstract METHOD didn't find an implementation
5831 that CLASS can use. */
5832 if (!found)
5833 {
c2e3db92 5834 char *t = xstrdup (lang_printable_name
d77613be
APB
5835 (TREE_TYPE (TREE_TYPE (method)), 0));
5836 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5837 tree saved_wfl = NULL_TREE;
5838
5839 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5840 {
5841 saved_wfl = DECL_NAME (method);
5842 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5843 }
5844
5845 parse_error_context
5846 (lookup_cl (class_decl),
781b0558 5847 "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
5848 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5849 t, lang_printable_name (method, 0),
5850 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5851 "interface" : "class"),
5852 IDENTIFIER_POINTER (ccn),
5853 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5854 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5855
5856 free (t);
5857
5858 if (saved_wfl)
5859 DECL_NAME (method) = saved_wfl;
5860 }
5861 }
5862}
5863
614eaae0 5864/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5865 methods. */
5866
5867static void
5868java_check_abstract_method_definitions (class_decl)
5869 tree class_decl;
5870{
5871 tree class = TREE_TYPE (class_decl);
5872 tree super, vector;
5873 int i;
5874
5875 if (CLASS_ABSTRACT (class_decl))
5876 return;
5877
5878 /* Check for inherited types */
165f37bc
APB
5879 super = class;
5880 do {
5881 super = CLASSTYPE_SUPER (super);
5882 check_abstract_method_definitions (0, class_decl, super);
5883 } while (super != object_type_node);
d77613be
APB
5884
5885 /* Check for implemented interfaces. */
5886 vector = TYPE_BINFO_BASETYPES (class);
5887 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5888 {
5889 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5890 check_abstract_method_definitions (1, class_decl, super);
5891 }
5892}
5893
165f37bc
APB
5894/* Check all the types method DECL uses and return 1 if all of them
5895 are now complete, 0 otherwise. This is used to check whether its
5896 safe to build a method signature or not. */
5897
5898static int
5899check_method_types_complete (decl)
5900 tree decl;
5901{
5902 tree type = TREE_TYPE (decl);
5903 tree args;
5904
5905 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5906 return 0;
5907
5908 args = TYPE_ARG_TYPES (type);
5909 if (TREE_CODE (type) == METHOD_TYPE)
5910 args = TREE_CHAIN (args);
5911 for (; args != end_params_node; args = TREE_CHAIN (args))
5912 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5913 return 0;
5914
5915 return 1;
5916}
5917
d77613be
APB
5918/* Check all the methods of CLASS_DECL. Methods are first completed
5919 then checked according to regular method existance rules. If no
5920 constructor for CLASS_DECL were encountered, then build its
5921 declaration. */
e04a16fb
AG
5922
5923static void
5924java_check_regular_methods (class_decl)
5925 tree class_decl;
5926{
c2952b01 5927 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
5928 tree method;
5929 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 5930 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
5931 tree mthrows;
5932
5933 /* It is not necessary to check methods defined in java.lang.Object */
5934 if (class == object_type_node)
5935 return;
e04a16fb 5936
23a79c61
APB
5937 if (!TYPE_NVIRTUALS (class))
5938 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
5939
5940 /* Should take interfaces into account. FIXME */
5941 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5942 {
5e942c50 5943 tree sig;
e04a16fb
AG
5944 tree method_wfl = DECL_NAME (method);
5945 int aflags;
5946
5e942c50
APB
5947 /* If we previously found something and its name was saved,
5948 reinstall it now */
5949 if (found && saved_found_wfl)
ba179f9f
APB
5950 {
5951 DECL_NAME (found) = saved_found_wfl;
5952 saved_found_wfl = NULL_TREE;
5953 }
5e942c50 5954
e04a16fb
AG
5955 /* Check for redefinitions */
5956 if (check_method_redefinition (class, method))
5957 continue;
5958
22eed1e6
APB
5959 /* If we see one constructor a mark so we don't generate the
5960 default one. Also skip other verifications: constructors
5961 can't be inherited hence hiden or overriden */
5962 if (DECL_CONSTRUCTOR_P (method))
5963 {
5964 saw_constructor = 1;
5965 continue;
5966 }
5967
c877974e
APB
5968 /* We verify things thrown by the method. They must inherits from
5969 java.lang.Throwable */
5970 for (mthrows = DECL_FUNCTION_THROWS (method);
5971 mthrows; mthrows = TREE_CHAIN (mthrows))
5972 {
5973 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
5974 parse_error_context
781b0558 5975 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
5976 IDENTIFIER_POINTER
5977 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
5978 }
5979
e04a16fb 5980 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 5981 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 5982
c2952b01
APB
5983 /* Inner class can't declare static methods */
5984 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
5985 {
5986 char *t = xstrdup (lang_printable_name (class, 0));
5987 parse_error_context
5988 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
5989 lang_printable_name (method, 0), t);
5990 free (t);
5991 }
5992
5e942c50 5993 /* Nothing overrides or it's a private method. */
aabd7048 5994 if (!found)
5e942c50 5995 continue;
aabd7048
PB
5996 if (METHOD_PRIVATE (found))
5997 {
5998 found = NULL_TREE;
5999 continue;
6000 }
5e942c50
APB
6001
6002 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6003 We set it temporarily for the sake of the error report. */
6004 saved_found_wfl = DECL_NAME (found);
6005 reset_method_name (found);
6006
614eaae0
APB
6007 /* If `found' is declared in an interface, make sure the
6008 modifier matches. */
6009 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6010 && clinit_identifier_node != DECL_NAME (found)
6011 && !METHOD_PUBLIC (method))
6012 {
6013 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6014 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6015 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6016 lang_printable_name (method, 0),
6017 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6018 }
6019
e04a16fb
AG
6020 /* Can't override a method with the same name and different return
6021 types. */
6022 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6023 {
614eaae0
APB
6024 char *t = xstrdup
6025 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6026 parse_error_context
7f10c2e2 6027 (method_wfl,
b9f7e36c 6028 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6029 lang_printable_name (found, 0), t,
b9f7e36c
APB
6030 IDENTIFIER_POINTER
6031 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6032 free (t);
6033 }
e04a16fb 6034
7f10c2e2
APB
6035 aflags = get_access_flags_from_decl (found);
6036 /* If the method has default, access in an other package, then
6037 issue a warning that the current method doesn't override the
6038 one that was found elsewhere. Do not issue this warning when
6039 the match was found in java.lang.Object. */
6040 if (DECL_CONTEXT (found) != object_type_node
a003f638 6041 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6042 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6043 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6044 && flag_not_overriding)
6045 {
6046 parse_warning_context
781b0558 6047 (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
6048 lang_printable_name (found, 0),
6049 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6050 IDENTIFIER_POINTER (DECL_NAME
6051 (TYPE_NAME (DECL_CONTEXT (found)))));
6052 continue;
6053 }
6054
e04a16fb
AG
6055 /* Can't override final. Can't override static. */
6056 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6057 {
6058 /* Static *can* override static */
6059 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6060 continue;
6061 parse_error_context
6062 (method_wfl,
6063 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6064 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6065 lang_printable_name (found, 0),
e04a16fb
AG
6066 (METHOD_FINAL (found) ? "final" : "static"),
6067 IDENTIFIER_POINTER
6068 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6069 continue;
6070 }
7f10c2e2 6071
e04a16fb
AG
6072 /* Static method can't override instance method. */
6073 if (METHOD_STATIC (method))
6074 {
6075 parse_error_context
6076 (method_wfl,
781b0558 6077 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6078 lang_printable_name (found, 0),
e04a16fb
AG
6079 IDENTIFIER_POINTER
6080 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6081 continue;
6082 }
5e942c50 6083
5e942c50
APB
6084 /* - Overriding/hiding public must be public
6085 - Overriding/hiding protected must be protected or public
6086 - If the overriden or hidden method has default (package)
6087 access, then the overriding or hiding method must not be
614eaae0
APB
6088 private; otherwise, a compile-time error occurs. If
6089 `found' belongs to an interface, things have been already
6090 taken care of. */
6091 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6092 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6093 || (METHOD_PROTECTED (found)
6094 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6095 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6096 && METHOD_PRIVATE (method))))
e04a16fb
AG
6097 {
6098 parse_error_context
6099 (method_wfl,
781b0558 6100 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6101 (METHOD_PUBLIC (method) ? "public" :
6102 (METHOD_PRIVATE (method) ? "private" : "protected")),
6103 IDENTIFIER_POINTER (DECL_NAME
6104 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6105 continue;
6106 }
6107
b9f7e36c
APB
6108 /* Overriding methods must have compatible `throws' clauses on checked
6109 exceptions, if any */
6110 check_throws_clauses (method, method_wfl, found);
6111
e04a16fb
AG
6112 /* Inheriting multiple methods with the same signature. FIXME */
6113 }
6114
5e942c50
APB
6115 /* Don't forget eventual pending found and saved_found_wfl. Take
6116 into account that we might have exited because we saw an
d77613be 6117 artificial method as the last entry. */
5e942c50
APB
6118
6119 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6120 DECL_NAME (found) = saved_found_wfl;
6121
23a79c61
APB
6122 if (!TYPE_NVIRTUALS (class))
6123 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6124
d77613be
APB
6125 /* Search for inherited abstract method not yet implemented in this
6126 class. */
6127 java_check_abstract_method_definitions (class_decl);
6128
22eed1e6 6129 if (!saw_constructor)
e920ebc9 6130 fatal ("No constructor found");
e04a16fb
AG
6131}
6132
b9f7e36c
APB
6133/* Return a non zero value if the `throws' clause of METHOD (if any)
6134 is incompatible with the `throws' clause of FOUND (if any). */
6135
6136static void
6137check_throws_clauses (method, method_wfl, found)
6138 tree method, method_wfl, found;
6139{
6140 tree mthrows, fthrows;
6141
c877974e
APB
6142 /* Can't check these things with class loaded from bytecode. FIXME */
6143 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6144 return;
6145
b9f7e36c
APB
6146 for (mthrows = DECL_FUNCTION_THROWS (method);
6147 mthrows; mthrows = TREE_CHAIN (mthrows))
6148 {
6149 /* We don't verify unchecked expressions */
c877974e 6150 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6151 continue;
6152 /* Checked expression must be compatible */
6153 for (fthrows = DECL_FUNCTION_THROWS (found);
6154 fthrows; fthrows = TREE_CHAIN (fthrows))
6155 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6156 break;
6157 if (!fthrows)
6158 {
6159 parse_error_context
781b0558 6160 (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 6161 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6162 lang_printable_name (found, 0),
b9f7e36c
APB
6163 IDENTIFIER_POINTER
6164 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6165 }
6166 }
6167}
6168
e04a16fb
AG
6169/* Check abstract method of interface INTERFACE */
6170
6171static void
5e942c50
APB
6172java_check_abstract_methods (interface_decl)
6173 tree interface_decl;
e04a16fb
AG
6174{
6175 int i, n;
6176 tree method, basetype_vec, found;
5e942c50 6177 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6178
6179 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6180 {
b9f7e36c 6181 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6182
6183 /* 2- Check for double definition inside the defining interface */
6184 if (check_method_redefinition (interface, method))
6185 continue;
6186
6187 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6188 the thrown exceptions (FIXME) */
e04a16fb
AG
6189 found = lookup_java_interface_method2 (interface, method);
6190 if (found)
6191 {
5e942c50
APB
6192 char *t;
6193 tree saved_found_wfl = DECL_NAME (found);
6194 reset_method_name (found);
c2e3db92 6195 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6196 parse_error_context
b9f7e36c 6197 (method_wfl,
5e942c50 6198 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6199 lang_printable_name (found, 0), t,
b9f7e36c
APB
6200 IDENTIFIER_POINTER
6201 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6202 free (t);
5e942c50 6203 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6204 continue;
e04a16fb
AG
6205 }
6206 }
6207
6208 /* 4- Inherited methods can't differ by their returned types */
6209 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6210 return;
6211 n = TREE_VEC_LENGTH (basetype_vec);
6212 for (i = 0; i < n; i++)
6213 {
6214 tree sub_interface_method, sub_interface;
6215 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6216 if (!vec_elt)
6217 continue;
6218 sub_interface = BINFO_TYPE (vec_elt);
6219 for (sub_interface_method = TYPE_METHODS (sub_interface);
6220 sub_interface_method;
6221 sub_interface_method = TREE_CHAIN (sub_interface_method))
6222 {
6223 found = lookup_java_interface_method2 (interface,
6224 sub_interface_method);
6225 if (found && (found != sub_interface_method))
5e942c50
APB
6226 {
6227 tree saved_found_wfl = DECL_NAME (found);
6228 reset_method_name (found);
6229 parse_error_context
6230 (lookup_cl (sub_interface_method),
781b0558 6231 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6232 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6233 lang_printable_name (found, 0),
6234 IDENTIFIER_POINTER
6235 (DECL_NAME (TYPE_NAME
6236 (DECL_CONTEXT (sub_interface_method)))),
6237 IDENTIFIER_POINTER
6238 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6239 DECL_NAME (found) = saved_found_wfl;
6240 }
e04a16fb
AG
6241 }
6242 }
6243}
6244
e04a16fb
AG
6245/* Lookup methods in interfaces using their name and partial
6246 signature. Return a matching method only if their types differ. */
6247
6248static tree
6249lookup_java_interface_method2 (class, method_decl)
6250 tree class, method_decl;
6251{
6252 int i, n;
6253 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6254
6255 if (!basetype_vec)
6256 return NULL_TREE;
6257
6258 n = TREE_VEC_LENGTH (basetype_vec);
6259 for (i = 0; i < n; i++)
6260 {
6261 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6262 if ((BINFO_TYPE (vec_elt) != object_type_node)
6263 && (to_return =
6264 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6265 return to_return;
6266 }
6267 for (i = 0; i < n; i++)
6268 {
6269 to_return = lookup_java_interface_method2
6270 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6271 if (to_return)
6272 return to_return;
6273 }
6274
6275 return NULL_TREE;
6276}
6277
6278/* Lookup method using their name and partial signature. Return a
6279 matching method only if their types differ. */
6280
6281static tree
6282lookup_java_method2 (clas, method_decl, do_interface)
6283 tree clas, method_decl;
6284 int do_interface;
6285{
5e942c50
APB
6286 tree method, method_signature, method_name, method_type, name;
6287
e04a16fb 6288 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6289 name = DECL_NAME (method_decl);
6290 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6291 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6292 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6293
6294 while (clas != NULL_TREE)
6295 {
6296 for (method = TYPE_METHODS (clas);
6297 method != NULL_TREE; method = TREE_CHAIN (method))
6298 {
6299 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6300 tree name = DECL_NAME (method);
6301 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6302 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6303 && method_sig == method_signature
6304 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6305 return method;
e04a16fb
AG
6306 }
6307 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6308 }
6309 return NULL_TREE;
6310}
6311
f441f671
APB
6312/* Return the line that matches DECL line number, and try its best to
6313 position the column number. Used during error reports. */
e04a16fb
AG
6314
6315static tree
6316lookup_cl (decl)
6317 tree decl;
6318{
6319 static tree cl = NULL_TREE;
f441f671 6320 char *line, *found;
e04a16fb
AG
6321
6322 if (!decl)
6323 return NULL_TREE;
6324
6325 if (cl == NULL_TREE)
6326 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6327
6328 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6329 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6330
f441f671
APB
6331 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6332 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6333
6334 found = strstr ((const char *)line,
6335 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6336 if (found)
6337 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6338
e04a16fb
AG
6339 return cl;
6340}
6341
6342/* Look for a simple name in the single-type import list */
6343
6344static tree
6345find_name_in_single_imports (name)
6346 tree name;
6347{
6348 tree node;
6349
6350 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6351 if (TREE_VALUE (node) == name)
6352 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6353
6354 return NULL_TREE;
6355}
6356
6357/* Process all single-type import. */
6358
6359static int
6360process_imports ()
6361{
6362 tree import;
6363 int error_found;
6364
6365 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6366 {
6367 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6368
6369 /* Don't load twice something already defined. */
6370 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6371 continue;
6372 QUALIFIED_P (to_be_found) = 1;
6373 load_class (to_be_found, 0);
6374 error_found =
6375 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6376 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6377 {
6378 parse_error_context (TREE_PURPOSE (import),
6379 "Class or interface `%s' not found in import",
6380 IDENTIFIER_POINTER (to_be_found));
6381 return 1;
6382 }
6383 if (error_found)
6384 return 1;
6385 }
6386 return 0;
6387}
6388
6389/* Possibly find a class imported by a single-type import statement. Return
6390 1 if an error occured, 0 otherwise. */
6391
6392static int
6393find_in_imports (class_type)
6394 tree class_type;
6395{
6396 tree import;
6397
6398 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6399 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6400 {
6401 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6402 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb
AG
6403 }
6404 return 0;
6405}
6406
e04a16fb 6407static int
63a212ed 6408note_possible_classname (name, len)
49f48c71 6409 const char *name;
63a212ed 6410 int len;
e04a16fb 6411{
63a212ed
PB
6412 tree node;
6413 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6414 len = len - 5;
6415 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6416 len = len - 6;
e04a16fb 6417 else
63a212ed
PB
6418 return 0;
6419 node = ident_subst (name, len, "", '/', '.', "");
6420 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6421 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6422 return 1;
e04a16fb
AG
6423}
6424
6425/* Read a import directory, gathering potential match for further type
6426 references. Indifferently reads a filesystem or a ZIP archive
6427 directory. */
6428
6429static void
6430read_import_dir (wfl)
6431 tree wfl;
6432{
63a212ed 6433 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6434 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6435 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6436 DIR *dirp = NULL;
d8fccff5 6437 JCF *saved_jcf = current_jcf;
e04a16fb 6438
63a212ed
PB
6439 int found = 0;
6440 int k;
6441 void *entry;
6442 struct buffer filename[1];
6443
6444
6445 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6446 return;
6447 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6448
6449 BUFFER_INIT (filename);
6450 buffer_grow (filename, package_length + 100);
6451
6452 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6453 {
49f48c71 6454 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6455 int entry_length = strlen (entry_name);
6456 if (jcf_path_is_zipfile (entry))
6457 {
6458 ZipFile *zipf;
6459 buffer_grow (filename, entry_length);
6460 memcpy (filename->data, entry_name, entry_length - 1);
6461 filename->data[entry_length-1] = '\0';
6462 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6463 if (zipf == NULL)
6464 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6465 else
6466 {
6467 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6468 BUFFER_RESET (filename);
6469 for (k = 0; k < package_length; k++)
6470 {
6471 char ch = package_name[k];
6472 *filename->ptr++ = ch == '.' ? '/' : ch;
6473 }
6474 *filename->ptr++ = '/';
6475
345137c7 6476 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6477 {
49f48c71 6478 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6479 int current_entry_len = zipd->filename_length;
6480
345137c7
TT
6481 if (current_entry_len >= BUFFER_LENGTH (filename)
6482 && strncmp (filename->data, current_entry,
6483 BUFFER_LENGTH (filename)) != 0)
63a212ed 6484 continue;
345137c7 6485 found |= note_possible_classname (current_entry,
63a212ed
PB
6486 current_entry_len);
6487 }
6488 }
6489 }
6490 else
6491 {
6492 BUFFER_RESET (filename);
6493 buffer_grow (filename, entry_length + package_length + 4);
6494 strcpy (filename->data, entry_name);
6495 filename->ptr = filename->data + entry_length;
6496 for (k = 0; k < package_length; k++)
6497 {
6498 char ch = package_name[k];
6499 *filename->ptr++ = ch == '.' ? '/' : ch;
6500 }
6501 *filename->ptr = '\0';
6502
6503 dirp = opendir (filename->data);
6504 if (dirp == NULL)
6505 continue;
6506 *filename->ptr++ = '/';
6507 for (;;)
6508 {
63a212ed 6509 int len;
49f48c71 6510 const char *d_name;
63a212ed
PB
6511 struct dirent *direntp = readdir (dirp);
6512 if (!direntp)
6513 break;
6514 d_name = direntp->d_name;
6515 len = strlen (direntp->d_name);
6516 buffer_grow (filename, len+1);
6517 strcpy (filename->ptr, d_name);
345137c7 6518 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6519 package_length+len+1);
6520 }
6521 if (dirp)
6522 closedir (dirp);
6523 }
6524 }
e04a16fb 6525
63a212ed 6526 free (filename->data);
e04a16fb 6527
63a212ed
PB
6528 /* Here we should have a unified way of retrieving an entry, to be
6529 indexed. */
6530 if (!found)
e04a16fb
AG
6531 {
6532 static int first = 1;
6533 if (first)
6534 {
781b0558 6535 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6536 java_error_count++;
6537 first = 0;
6538 }
6539 else
63a212ed
PB
6540 parse_error_context (wfl, "Package `%s' not found in import",
6541 package_name);
e04a16fb
AG
6542 current_jcf = saved_jcf;
6543 return;
6544 }
e04a16fb
AG
6545 current_jcf = saved_jcf;
6546}
6547
6548/* Possibly find a type in the import on demands specified
6549 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6550 entire list, to detected potential double definitions. */
6551
6552static int
6553find_in_imports_on_demand (class_type)
6554 tree class_type;
6555{
ab3a6dd6 6556 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6557 int seen_once = -1;
ab3a6dd6 6558 tree cl = NULL_TREE;
e04a16fb
AG
6559
6560 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6561 {
49f48c71 6562 const char *id_name;
e04a16fb
AG
6563 obstack_grow (&temporary_obstack,
6564 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6565 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6566 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6567 obstack_grow0 (&temporary_obstack,
6568 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6569 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6570 id_name = obstack_finish (&temporary_obstack);
6571
6572 node = maybe_get_identifier (id_name);
6573 if (node && IS_A_CLASSFILE_NAME (node))
6574 {
6575 if (seen_once < 0)
6576 {
6577 cl = TREE_PURPOSE (import);
6578 seen_once = 1;
6579 node_to_use = node;
6580 }
6581 else
6582 {
6583 seen_once++;
6584 parse_error_context
6585 (import, "Type `%s' also potentially defined in package `%s'",
6586 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6587 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6588 }
6589 }
6590 }
6591
6592 if (seen_once == 1)
6593 {
6594 /* Setup lineno so that it refers to the line of the import (in
6595 case we parse a class file and encounter errors */
6596 tree decl;
6597 int saved_lineno = lineno;
6598 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6599 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6600 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6601 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6602 /* If there is no DECL set for the class or if the class isn't
6603 loaded and not seen in source yet, the load */
6604 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6605 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6606 load_class (node_to_use, 0);
6607 lineno = saved_lineno;
6608 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6609 }
6610 else
6611 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6612}
6613
5e942c50
APB
6614static tree
6615resolve_package (pkg, next)
6616 tree pkg, *next;
6617{
c2952b01 6618 tree current, acc;
5e942c50 6619 tree type_name = NULL_TREE;
49f48c71 6620 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6621
6622 /* The trick is to determine when the package name stops and were
6623 the name of something contained in the package starts. Then we
6624 return a fully qualified name of what we want to get. */
6625
6626 /* Do a quick search on well known package names */
6627 if (!strncmp (name, "java.lang.reflect", 17))
6628 {
6629 *next =
6630 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6631 type_name = lookup_package_type (name, 17);
6632 }
6633 else if (!strncmp (name, "java.lang", 9))
6634 {
6635 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6636 type_name = lookup_package_type (name, 9);
6637 }
5e942c50 6638
2c56429a
APB
6639 /* If we found something here, return */
6640 if (type_name)
6641 return type_name;
6642
6643 *next = EXPR_WFL_QUALIFICATION (pkg);
6644
6645 /* Try the current package. */
6646 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6647 IDENTIFIER_LENGTH (ctxp->package)))
6648 {
6649 type_name =
6650 lookup_package_type_and_set_next (name,
6651 IDENTIFIER_LENGTH (ctxp->package),
6652 next );
6653 if (type_name)
6654 return type_name;
6655 }
6656
6657 /* Search in imported package */
6658 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6659 {
6660 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6661 int len = IDENTIFIER_LENGTH (current_pkg_name);
6662 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6663 {
6664 tree left, dummy;
6665
6666 breakdown_qualified (&left, &dummy, current_pkg_name);
6667 len = IDENTIFIER_LENGTH (left);
6668 type_name = lookup_package_type_and_set_next (name, len, next);
6669 if (type_name)
6670 break;
6671 }
6672 }
6673
c2952b01
APB
6674 /* Try to progressively construct a type name */
6675 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6676 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6677 current; current = TREE_CHAIN (current))
6678 {
6679 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6680 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6681 {
6682 type_name = acc;
6b48deee
APB
6683 /* resolve_package should be used in a loop, hence we
6684 point at this one to naturally process the next one at
6685 the next iteration. */
6686 *next = current;
c2952b01
APB
6687 break;
6688 }
6689 }
2c56429a
APB
6690 return type_name;
6691}
6692
6693static tree
6694lookup_package_type_and_set_next (name, len, next)
49f48c71 6695 const char *name;
2c56429a
APB
6696 int len;
6697 tree *next;
6698{
49f48c71 6699 const char *ptr;
2c56429a
APB
6700 tree type_name = lookup_package_type (name, len);
6701
6702 if (!type_name)
6703 return NULL;
6704
6705 ptr = IDENTIFIER_POINTER (type_name);
6706 while (ptr && (ptr = strchr (ptr, '.')))
6707 {
6708 *next = TREE_CHAIN (*next);
6709 ptr++;
6710 }
5e942c50
APB
6711 return type_name;
6712}
6713
6714static tree
6715lookup_package_type (name, from)
49f48c71 6716 const char *name;
5e942c50
APB
6717 int from;
6718{
6719 char subname [128];
49f48c71 6720 const char *sub = &name[from+1];
5e942c50
APB
6721 while (*sub != '.' && *sub)
6722 sub++;
6723 strncpy (subname, name, sub-name);
6724 subname [sub-name] = '\0';
6725 return get_identifier (subname);
6726}
6727
e04a16fb
AG
6728/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6729 access violations were found, 1 otherwise. */
6730
6731static int
6732check_pkg_class_access (class_name, cl)
6733 tree class_name;
6734 tree cl;
6735{
6736 tree type;
e04a16fb
AG
6737
6738 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6739 return 0;
6740
6741 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6742 return 0;
6743
6744 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6745 {
e28cd97b
APB
6746 /* Access to a private class within the same package is
6747 allowed. */
6748 tree l, r;
6749 breakdown_qualified (&l, &r, class_name);
6750 if (l == ctxp->package)
6751 return 0;
6752
e04a16fb 6753 parse_error_context
781b0558 6754 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6755 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6756 IDENTIFIER_POINTER (class_name));
6757 return 1;
6758 }
6759 return 0;
6760}
6761
6762/* Local variable declaration. */
6763
6764static void
6765declare_local_variables (modifier, type, vlist)
6766 int modifier;
6767 tree type;
6768 tree vlist;
6769{
c583dd46
APB
6770 tree decl, current, saved_type;
6771 tree type_wfl = NULL_TREE;
e04a16fb 6772 int must_chain = 0;
c2952b01 6773 int final_p = 0;
e04a16fb 6774
2aa11e97 6775 /* Push a new block if statements were seen between the last time we
e04a16fb 6776 pushed a block and now. Keep a cound of block to close */
f099f336 6777 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6778 {
f099f336 6779 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6780 tree b = enter_block ();
f099f336 6781 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6782 }
6783
6784 if (modifier)
6785 {
6786 int i;
6787 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6788 if (modifier == ACC_FINAL)
c2952b01 6789 final_p = 1;
c877974e
APB
6790 else
6791 {
6792 parse_error_context
6793 (ctxp->modifier_ctx [i],
6794 "Only `final' is allowed as a local variables modifier");
6795 return;
6796 }
e04a16fb
AG
6797 }
6798
c583dd46
APB
6799 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6800 hold the TYPE value if a new incomplete has to be created (as
6801 opposed to being found already existing and reused). */
6802 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6803
6804 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6805 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6806
6807 /* Go through all the declared variables */
6808 for (current = vlist, saved_type = type; current;
6809 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6810 {
c877974e 6811 tree other, real_type;
e04a16fb
AG
6812 tree wfl = TREE_PURPOSE (current);
6813 tree name = EXPR_WFL_NODE (wfl);
6814 tree init = TREE_VALUE (current);
e04a16fb 6815
c583dd46
APB
6816 /* Process NAME, as it may specify extra dimension(s) for it */
6817 type = build_array_from_name (type, type_wfl, name, &name);
6818
6819 /* Variable redefinition check */
6820 if ((other = lookup_name_in_blocks (name)))
6821 {
6822 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6823 DECL_SOURCE_LINE (other));
6824 continue;
6825 }
6826
6827 /* Type adjustment. We may have just readjusted TYPE because
6828 the variable specified more dimensions. Make sure we have
6829 a reference if we can and don't have one already. */
1886c9d8 6830 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6831
6832 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6833 /* Never layout this decl. This will be done when its scope
6834 will be entered */
c877974e 6835 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6836 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6837 BLOCK_CHAIN_DECL (decl);
6838
d4370213
APB
6839 /* If doing xreferencing, replace the line number with the WFL
6840 compound value */
6841 if (flag_emit_xref)
6842 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6843
e04a16fb
AG
6844 /* Don't try to use an INIT statement when an error was found */
6845 if (init && java_error_count)
6846 init = NULL_TREE;
c583dd46
APB
6847
6848 /* Add the initialization function to the current function's code */
6849 if (init)
e04a16fb 6850 {
c583dd46
APB
6851 /* Name might have been readjusted */
6852 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6853 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6854 java_method_add_stmt (current_function_decl,
6855 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6856 init));
6857 }
6858
6859 /* Setup dependency the type of the decl */
6860 if (must_chain)
6861 {
6862 jdep *dep;
6863 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6864 dep = CLASSD_LAST (ctxp->classd_list);
6865 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6866 }
6867 }
6868 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6869}
6870
6871/* Called during parsing. Build decls from argument list. */
6872
6873static void
6874source_start_java_method (fndecl)
6875 tree fndecl;
6876{
6877 tree tem;
6878 tree parm_decl;
6879 int i;
6880
79d13333
APB
6881 if (!fndecl)
6882 return;
6883
e04a16fb
AG
6884 current_function_decl = fndecl;
6885
6886 /* New scope for the function */
6887 enter_block ();
6888 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 6889 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
6890 {
6891 tree type = TREE_VALUE (tem);
6892 tree name = TREE_PURPOSE (tem);
6893
23a79c61
APB
6894 /* If type is incomplete. Create an incomplete decl and ask for
6895 the decl to be patched later */
e04a16fb
AG
6896 if (INCOMPLETE_TYPE_P (type))
6897 {
6898 jdep *jdep;
c877974e
APB
6899 tree real_type = GET_REAL_TYPE (type);
6900 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 6901 type = obtain_incomplete_type (type);
e04a16fb
AG
6902 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6903 jdep = CLASSD_LAST (ctxp->classd_list);
6904 JDEP_MISC (jdep) = name;
6905 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6906 }
6907 else
6908 parm_decl = build_decl (PARM_DECL, name, type);
6909
c2952b01
APB
6910 /* Remember if a local variable was declared final (via its
6911 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6912 if (ARG_FINAL_P (tem))
6913 LOCAL_FINAL (parm_decl) = 1;
6914
e04a16fb
AG
6915 BLOCK_CHAIN_DECL (parm_decl);
6916 }
6917 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6918 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
6919 nreverse (tem);
6920 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 6921 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
6922}
6923
22eed1e6
APB
6924/* Called during parsing. Creates an artificial method declaration. */
6925
6926static tree
6927create_artificial_method (class, flags, type, name, args)
6928 tree class;
6929 int flags;
6930 tree type, name, args;
6931{
22eed1e6
APB
6932 tree mdecl;
6933
c2952b01 6934 java_parser_context_save_global ();
22eed1e6
APB
6935 lineno = 0;
6936 mdecl = make_node (FUNCTION_TYPE);
6937 TREE_TYPE (mdecl) = type;
6938 TYPE_ARG_TYPES (mdecl) = args;
6939 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 6940 java_parser_context_restore_global ();
22eed1e6
APB
6941 DECL_ARTIFICIAL (mdecl) = 1;
6942 return mdecl;
6943}
6944
6945/* Starts the body if an artifical method. */
6946
6947static void
6948start_artificial_method_body (mdecl)
6949 tree mdecl;
6950{
6951 DECL_SOURCE_LINE (mdecl) = 1;
6952 DECL_SOURCE_LINE_MERGE (mdecl, 1);
6953 source_start_java_method (mdecl);
6954 enter_block ();
6955}
6956
6957static void
6958end_artificial_method_body (mdecl)
6959 tree mdecl;
6960{
6961 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
6962 exit_block ();
6963}
6964
e04a16fb
AG
6965/* Called during expansion. Push decls formerly built from argument
6966 list so they're usable during expansion. */
6967
6968static void
6969expand_start_java_method (fndecl)
6970 tree fndecl;
6971{
6972 tree tem, *ptr;
e04a16fb 6973
e04a16fb
AG
6974 current_function_decl = fndecl;
6975
c2952b01
APB
6976 if (! quiet_flag)
6977 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 6978 announce_function (fndecl);
c2952b01
APB
6979 if (! quiet_flag)
6980 fprintf (stderr, "]");
6981
6982 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
6983 ptr = &DECL_ARGUMENTS (fndecl);
6984 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6985 while (tem)
6986 {
6987 tree next = TREE_CHAIN (tem);
b67d701b 6988 tree type = TREE_TYPE (tem);
e438e1b7
JJ
6989 if (PROMOTE_PROTOTYPES
6990 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
6991 && INTEGRAL_TYPE_P (type))
6992 type = integer_type_node;
b67d701b 6993 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
6994 layout_decl (tem, 0);
6995 pushdecl (tem);
e04a16fb
AG
6996 *ptr = tem;
6997 ptr = &TREE_CHAIN (tem);
6998 tem = next;
6999 }
7000 *ptr = NULL_TREE;
7001 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7002 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7003}
7004
7005/* Terminate a function and expand its body. */
7006
7007static void
7008source_end_java_method ()
7009{
7010 tree fndecl = current_function_decl;
138657ec 7011 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7012
79d13333
APB
7013 if (!fndecl)
7014 return;
7015
e04a16fb
AG
7016 java_parser_context_save_global ();
7017 lineno = ctxp->last_ccb_indent1;
7018
b67d701b
PB
7019 /* Set EH language codes */
7020 java_set_exception_lang_code ();
7021
5423609c
APB
7022 /* Turn function bodies with only a NOP expr null, so they don't get
7023 generated at all and we won't get warnings when using the -W
7024 -Wall flags. */
7025 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7026 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7027
e04a16fb
AG
7028 /* Generate function's code */
7029 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7030 && ! flag_emit_class_files
7031 && ! flag_emit_xref)
e04a16fb
AG
7032 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7033
7034 /* pop out of its parameters */
7035 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7036 poplevel (1, 0, 1);
7037 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7038
7039 /* Generate rtl for function exit. */
e8fc7396 7040 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7041 {
7042 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7043 /* Emit catch-finally clauses */
7044 emit_handlers ();
e04a16fb
AG
7045 expand_function_end (input_filename, lineno, 0);
7046
138657ec
AH
7047 /* FIXME: If the current method contains any exception handlers,
7048 force asynchronous_exceptions: this is necessary because signal
7049 handlers in libjava may throw exceptions. This is far from being
7050 a perfect solution, but it's better than doing nothing at all.*/
7051 if (catch_clauses)
7052 asynchronous_exceptions = 1;
7053
e04a16fb
AG
7054 /* Run the optimizers and output assembler code for this function. */
7055 rest_of_compilation (fndecl);
7056 }
7057
7058 current_function_decl = NULL_TREE;
8226320b 7059 permanent_allocation (1);
e04a16fb 7060 java_parser_context_restore_global ();
138657ec 7061 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7062}
7063
7064/* Record EXPR in the current function block. Complements compound
7065 expression second operand if necessary. */
7066
7067tree
7068java_method_add_stmt (fndecl, expr)
7069 tree fndecl, expr;
7070{
b771925e
APB
7071 if (!GET_CURRENT_BLOCK (fndecl))
7072 return NULL_TREE;
f099f336 7073 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7074}
e04a16fb 7075
b67d701b
PB
7076static tree
7077add_stmt_to_block (b, type, stmt)
7078 tree b, type, stmt;
7079{
7080 tree body = BLOCK_EXPR_BODY (b), c;
7081
e04a16fb
AG
7082 if (java_error_count)
7083 return body;
b67d701b
PB
7084
7085 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7086 return body;
7087
b67d701b
PB
7088 BLOCK_EXPR_BODY (b) = c;
7089 TREE_SIDE_EFFECTS (c) = 1;
7090 return c;
e04a16fb
AG
7091}
7092
7093/* Add STMT to EXISTING if possible, otherwise create a new
7094 COMPOUND_EXPR and add STMT to it. */
7095
7096static tree
7097add_stmt_to_compound (existing, type, stmt)
7098 tree existing, type, stmt;
7099{
15fdcfe9
PB
7100 if (existing)
7101 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7102 else
15fdcfe9 7103 return stmt;
e04a16fb
AG
7104}
7105
7106/* Hold THIS for the scope of the current public method decl. */
7107static tree current_this;
7108
1886c9d8
APB
7109void java_layout_seen_class_methods ()
7110{
7111 tree previous_list = all_class_list;
7112 tree end = NULL_TREE;
7113 tree current;
7114
7115 while (1)
7116 {
7117 for (current = previous_list;
7118 current != end; current = TREE_CHAIN (current))
7119 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7120
7121 if (previous_list != all_class_list)
7122 {
7123 end = previous_list;
7124 previous_list = all_class_list;
7125 }
7126 else
7127 break;
7128 }
7129}
7130
e04a16fb 7131void
c2952b01 7132java_reorder_fields ()
e04a16fb 7133{
c2952b01 7134 static tree stop_reordering = NULL_TREE;
23a79c61 7135
c2952b01 7136 tree current;
5e942c50 7137 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7138 {
5e942c50 7139 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7140
c2952b01
APB
7141 if (current_class == stop_reordering)
7142 break;
7143
c877974e
APB
7144 /* Reverse the fields, but leave the dummy field in front.
7145 Fields are already ordered for Object and Class */
7146 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7147 && current_class != class_type_node)
7148 {
23a79c61
APB
7149 /* If the dummy field is there, reverse the right fields and
7150 just layout the type for proper fields offset */
c877974e
APB
7151 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7152 {
7153 tree fields = TYPE_FIELDS (current_class);
7154 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7155 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7156 }
23a79c61
APB
7157 /* We don't have a dummy field, we need to layout the class,
7158 after having reversed the fields */
c877974e
APB
7159 else
7160 {
7161 TYPE_FIELDS (current_class) =
7162 nreverse (TYPE_FIELDS (current_class));
7163 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7164 }
7165 }
c2952b01
APB
7166 }
7167 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7168}
7169
7170/* Layout the methods of all classes loaded in one way on an
7171 other. Check methods of source parsed classes. Then reorder the
7172 fields and layout the classes or the type of all source parsed
7173 classes */
7174
7175void
7176java_layout_classes ()
7177{
7178 tree current;
7179 int save_error_count = java_error_count;
7180
7181 /* Layout the methods of all classes seen so far */
7182 java_layout_seen_class_methods ();
7183 java_parse_abort_on_error ();
7184 all_class_list = NULL_TREE;
7185
7186 /* Then check the methods of all parsed classes */
7187 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7188 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7189 CHECK_METHODS (TREE_VALUE (current));
7190 java_parse_abort_on_error ();
7191
7192 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7193 {
7194 current_class = TREE_TYPE (TREE_VALUE (current));
7195 layout_class (current_class);
5e942c50 7196
c877974e
APB
7197 /* From now on, the class is considered completely loaded */
7198 CLASS_LOADED_P (current_class) = 1;
7199
5e942c50
APB
7200 /* Error reported by the caller */
7201 if (java_error_count)
7202 return;
e04a16fb 7203 }
23a79c61
APB
7204
7205 /* We might have reloaded classes durign the process of laying out
7206 classes for code generation. We must layout the methods of those
7207 late additions, as constructor checks might use them */
1886c9d8 7208 java_layout_seen_class_methods ();
23a79c61 7209 java_parse_abort_on_error ();
e04a16fb
AG
7210}
7211
c2952b01
APB
7212/* Expand methods in the current set of classes rememebered for
7213 generation. */
e04a16fb 7214
49f48c71 7215static void
c2952b01 7216java_complete_expand_classes ()
e04a16fb
AG
7217{
7218 tree current;
ce6e9147
APB
7219
7220 do_not_fold = flag_emit_xref;
c2952b01 7221
e04a16fb 7222 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7223 if (!INNER_CLASS_DECL_P (current))
7224 java_complete_expand_class (current);
7225}
e04a16fb 7226
c2952b01
APB
7227/* Expand the methods found in OUTER, starting first by OUTER's inner
7228 classes, if any. */
e04a16fb 7229
c2952b01
APB
7230static void
7231java_complete_expand_class (outer)
7232 tree outer;
7233{
7234 tree inner_list;
e04a16fb 7235
c2952b01 7236 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7237
c2952b01
APB
7238 /* We need to go after all inner classes and start expanding them,
7239 starting with most nested ones. We have to do that because nested
7240 classes might add functions to outer classes */
e04a16fb 7241
c2952b01
APB
7242 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7243 inner_list; inner_list = TREE_CHAIN (inner_list))
7244 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7245
c2952b01
APB
7246 java_complete_expand_methods (outer);
7247 set_nested_class_simple_name_value (outer, 0); /* Reset */
7248}
7249
7250/* Expand methods registered in CLASS_DECL. The general idea is that
7251 we expand regular methods first. This allows us get an estimate on
7252 how outer context local alias fields are really used so we can add
7253 to the constructor just enough code to initialize them properly (it
7254 also lets us generate $finit$ correctly.) Then we expand the
7255 constructors and then <clinit>. */
7256
7257static void
7258java_complete_expand_methods (class_decl)
7259 tree class_decl;
7260{
7261 tree clinit, finit, decl, first_decl;
7262
7263 current_class = TREE_TYPE (class_decl);
7264
7265 /* Initialize a new constant pool */
7266 init_outgoing_cpool ();
7267
7268 /* Pre-expand <clinit> to figure whether we really need it or
7269 not. If we do need it, we pre-expand the static fields so they're
7270 ready to be used somewhere else. <clinit> will be fully expanded
7271 after we processed the constructors. */
7272 first_decl = TYPE_METHODS (current_class);
7273 clinit = maybe_generate_pre_expand_clinit (current_class);
7274
7275 /* Then generate $finit$ (if we need to) because constructor will
7276 try to use it.*/
7277 if (TYPE_FINIT_STMT_LIST (current_class))
7278 {
7279 finit = generate_finit (current_class);
7280 java_complete_expand_method (finit);
7281 }
7282
7283 /* Now do the constructors */
7284 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7285 {
7286 int no_body;
7287
7288 if (!DECL_CONSTRUCTOR_P (decl))
7289 continue;
7290
7291 no_body = !DECL_FUNCTION_BODY (decl);
7292 /* Don't generate debug info on line zero when expanding a
7293 generated constructor. */
7294 if (no_body)
7295 restore_line_number_status (1);
7296
7297 java_complete_expand_method (decl);
7298
7299 if (no_body)
7300 restore_line_number_status (0);
7301 }
7302
7303 /* First, do the ordinary methods. */
7304 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7305 {
7145d9fe
TT
7306 /* Skip abstract or native methods -- but do handle native
7307 methods when generating JNI stubs. */
7308 if (METHOD_ABSTRACT (decl)
7309 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7310 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7311 continue;
7145d9fe
TT
7312
7313 if (METHOD_NATIVE (decl))
7314 {
7315 tree body = build_jni_stub (decl);
7316 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7317 }
7318
c2952b01
APB
7319 java_complete_expand_method (decl);
7320 }
7321
7322 /* If there is indeed a <clinit>, fully expand it now */
7323 if (clinit)
7324 {
7325 /* Prevent the use of `this' inside <clinit> */
7326 ctxp->explicit_constructor_p = 1;
7327 java_complete_expand_method (clinit);
7328 ctxp->explicit_constructor_p = 0;
e04a16fb 7329 }
c2952b01 7330
165f37bc
APB
7331 /* We might have generated a class$ that we now want to expand */
7332 if (TYPE_DOT_CLASS (current_class))
7333 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7334
c2952b01
APB
7335 /* Now verify constructor circularity (stop after the first one we
7336 prove wrong.) */
7337 if (!CLASS_INTERFACE (class_decl))
7338 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7339 if (DECL_CONSTRUCTOR_P (decl)
7340 && verify_constructor_circularity (decl, decl))
7341 break;
7342
7343 /* Save the constant pool. We'll need to restore it later. */
7344 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7345}
7346
b9f7e36c
APB
7347/* Hold a list of catch clauses list. The first element of this list is
7348 the list of the catch clauses of the currently analysed try block. */
7349static tree currently_caught_type_list;
7350
c2952b01
APB
7351/* Attempt to create <clinit>. Pre-expand static fields so they can be
7352 safely used in some other methods/constructors. */
e920ebc9 7353
c2952b01
APB
7354static tree
7355maybe_generate_pre_expand_clinit (class_type)
7356 tree class_type;
e920ebc9 7357{
c2952b01
APB
7358 tree current, mdecl;
7359
7360 if (!TYPE_CLINIT_STMT_LIST (class_type))
7361 return NULL_TREE;
e920ebc9 7362
c2952b01
APB
7363 /* Go through all static fields and pre expand them */
7364 for (current = TYPE_FIELDS (class_type); current;
7365 current = TREE_CHAIN (current))
7366 if (FIELD_STATIC (current))
7367 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7368
7369 /* Then build the <clinit> method */
7370 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7371 clinit_identifier_node, end_params_node);
7372 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7373 mdecl, NULL_TREE);
7374 start_artificial_method_body (mdecl);
7375
7376 /* We process the list of assignment we produced as the result of
7377 the declaration of initialized static field and add them as
7378 statement to the <clinit> method. */
7379 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7380 current = TREE_CHAIN (current))
e920ebc9 7381 {
c2952b01
APB
7382 /* We build the assignment expression that will initialize the
7383 field to its value. There are strict rules on static
7384 initializers (8.5). FIXME */
7385 tree stmt = build_debugable_stmt (EXPR_WFL_LINECOL (current), current);
7386 java_method_add_stmt (mdecl, stmt);
7387 }
e920ebc9 7388
c2952b01
APB
7389 end_artificial_method_body (mdecl);
7390
92d83515
APB
7391 /* Now we want to place <clinit> as the last method (because we need
7392 it at least for interface so that it doesn't interfere with the
7393 dispatch table based lookup. */
7394 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7395 {
92d83515
APB
7396 current = TREE_CHAIN (TYPE_METHODS (class_type));
7397 TYPE_METHODS (class_type) = current;
c2952b01
APB
7398
7399 while (TREE_CHAIN (current))
7400 current = TREE_CHAIN (current);
92d83515 7401
c2952b01
APB
7402 TREE_CHAIN (current) = mdecl;
7403 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7404 }
c2952b01
APB
7405
7406 return mdecl;
e920ebc9
APB
7407}
7408
92d83515
APB
7409/* See whether we could get rid of <clinit>. Criteria are: all static
7410 final fields have constant initial values and the body of <clinit>
7411 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7412
7413static int
7414maybe_yank_clinit (mdecl)
7415 tree mdecl;
7416{
7417 tree type, current;
7418 tree fbody, bbody;
7419
7420 if (!DECL_CLINIT_P (mdecl))
7421 return 0;
7422
7423 /* If the body isn't empty, then we keep <clinit> */
7424 fbody = DECL_FUNCTION_BODY (mdecl);
7425 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7426 bbody = BLOCK_EXPR_BODY (bbody);
7427 if (bbody && bbody != empty_stmt_node)
7428 return 0;
7429
7430 type = DECL_CONTEXT (mdecl);
7431 current = TYPE_FIELDS (type);
7432
7433 for (current = (current ? TREE_CHAIN (current) : current);
7434 current; current = TREE_CHAIN (current))
7435 if (!(FIELD_STATIC (current) && FIELD_FINAL (current)
7436 && DECL_INITIAL (current) && TREE_CONSTANT (DECL_INITIAL (current))))
7437 break;
7438
7439 if (current)
7440 return 0;
7441
7442 /* Get rid of <clinit> in the class' list of methods */
7443 if (TYPE_METHODS (type) == mdecl)
7444 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7445 else
7446 for (current = TYPE_METHODS (type); current;
7447 current = TREE_CHAIN (current))
7448 if (TREE_CHAIN (current) == mdecl)
7449 {
7450 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7451 break;
7452 }
7453
7454 return 1;
7455}
7456
7457
e04a16fb
AG
7458/* Complete and expand a method. */
7459
7460static void
7461java_complete_expand_method (mdecl)
7462 tree mdecl;
7463{
92d83515
APB
7464 int yank_clinit = 0;
7465
c2952b01 7466 current_function_decl = mdecl;
22eed1e6
APB
7467 /* Fix constructors before expanding them */
7468 if (DECL_CONSTRUCTOR_P (mdecl))
7469 fix_constructors (mdecl);
e04a16fb 7470
22eed1e6 7471 /* Expand functions that have a body */
e04a16fb
AG
7472 if (DECL_FUNCTION_BODY (mdecl))
7473 {
9bbc7d9f
PB
7474 tree fbody = DECL_FUNCTION_BODY (mdecl);
7475 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7476 tree exception_copy = NULL_TREE;
e04a16fb 7477 expand_start_java_method (mdecl);
939d7216 7478 build_result_decl (mdecl);
e04a16fb
AG
7479
7480 current_this
7481 = (!METHOD_STATIC (mdecl) ?
7482 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7483
ce6e9147
APB
7484 /* Purge the `throws' list of unchecked exceptions. If we're
7485 doing xref, save a copy of the list and re-install it
7486 later. */
7487 if (flag_emit_xref)
7488 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7489
b9f7e36c
APB
7490 purge_unchecked_exceptions (mdecl);
7491
7492 /* Install exceptions thrown with `throws' */
7493 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7494
9bbc7d9f 7495 if (block_body != NULL_TREE)
bc3ca41b
PB
7496 {
7497 block_body = java_complete_tree (block_body);
c2952b01 7498
7145d9fe 7499 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7500 check_for_initialization (block_body);
f099f336 7501 ctxp->explicit_constructor_p = 0;
bc3ca41b 7502 }
e803d3b2 7503
9bbc7d9f 7504 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7505
c2952b01
APB
7506 /* If we saw a return but couldn't evaluate it properly, we'll
7507 have an error_mark_node here. */
7508 if (block_body != error_mark_node
7509 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7510 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7511 && !flag_emit_xref)
82371d41 7512 missing_return_error (current_function_decl);
7525cc04 7513
92d83515
APB
7514 /* Check wether we could just get rid of clinit, now the picture
7515 is complete. */
7516 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7517 complete_start_java_method (mdecl);
7518
e04a16fb 7519 /* Don't go any further if we've found error(s) during the
92d83515
APB
7520 expansion */
7521 if (!java_error_count && !yank_clinit)
e04a16fb 7522 source_end_java_method ();
22eed1e6
APB
7523 else
7524 {
92d83515
APB
7525 if (java_error_count)
7526 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7527 poplevel (1, 0, 1);
7528 }
b9f7e36c
APB
7529
7530 /* Pop the exceptions and sanity check */
7531 POP_EXCEPTIONS();
7532 if (currently_caught_type_list)
7533 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7534
7535 if (flag_emit_xref)
7536 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7537 }
7538}
7539
c2952b01
APB
7540\f
7541
7542/* This section of the code deals with accessing enclosing context
7543 fields either directly by using the relevant access to this$<n> or
7544 by invoking an access method crafted for that purpose. */
7545
7546/* Build the necessary access from an inner class to an outer
7547 class. This routine could be optimized to cache previous result
7548 (decl, current_class and returned access). When an access method
7549 needs to be generated, it always takes the form of a read. It might
7550 be later turned into a write by calling outer_field_access_fix. */
7551
7552static tree
7553build_outer_field_access (id, decl)
7554 tree id, decl;
7555{
7556 tree access = NULL_TREE;
7557 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7558
7559 /* If decl's class is the direct outer class of the current_class,
7560 build the access as `this$<n>.<field>'. Not that we will break
7561 the `private' barrier if we're not emitting bytecodes. */
7562 if (ctx == DECL_CONTEXT (decl)
7563 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7564 {
7565 tree thisn = build_current_thisn (current_class);
7566 access = make_qualified_primary (build_wfl_node (thisn),
7567 id, EXPR_WFL_LINECOL (id));
7568 }
7569 /* Otherwise, generate access methods to outer this and access the
7570 field (either using an access method or by direct access.) */
7571 else
7572 {
7573 int lc = EXPR_WFL_LINECOL (id);
7574
7575 /* Now we chain the required number of calls to the access$0 to
7576 get a hold to the enclosing instance we need, and the we
7577 build the field access. */
7578 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7579
7580 /* If the field is private and we're generating bytecode, then
7581 we generate an access method */
7582 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7583 {
7584 tree name = build_outer_field_access_methods (decl);
7585 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7586 name, access, NULL_TREE);
7587 }
7588 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7589 Once again we break the `private' access rule from a foreign
7590 class. */
7591 else
7592 access = make_qualified_primary (access, id, lc);
7593 }
7594 return resolve_expression_name (access, NULL);
7595}
7596
7597/* Return a non zero value if NODE describes an outer field inner
7598 access. */
7599
7600static int
7601outer_field_access_p (type, decl)
7602 tree type, decl;
7603{
7604 if (!INNER_CLASS_TYPE_P (type)
7605 || TREE_CODE (decl) != FIELD_DECL
7606 || DECL_CONTEXT (decl) == type)
7607 return 0;
7608
7609 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7610 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7611 {
7612 if (type == DECL_CONTEXT (decl))
7613 return 1;
7614 if (!DECL_CONTEXT (TYPE_NAME (type)))
7615 break;
7616 }
7617
7618 return 0;
7619}
7620
7621/* Return a non zero value if NODE represents an outer field inner
7622 access that was been already expanded. As a side effect, it returns
7623 the name of the field being accessed and the argument passed to the
7624 access function, suitable for a regeneration of the access method
7625 call if necessary. */
7626
7627static int
7628outer_field_expanded_access_p (node, name, arg_type, arg)
7629 tree node, *name, *arg_type, *arg;
7630{
7631 int identified = 0;
7632
7633 if (TREE_CODE (node) != CALL_EXPR)
7634 return 0;
7635
7636 /* Well, gcj generates slightly different tree nodes when compiling
7637 to native or bytecodes. It's the case for function calls. */
7638
7639 if (flag_emit_class_files
7640 && TREE_CODE (node) == CALL_EXPR
7641 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7642 identified = 1;
7643 else if (!flag_emit_class_files)
7644 {
7645 node = TREE_OPERAND (node, 0);
7646
7647 if (node && TREE_OPERAND (node, 0)
7648 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7649 {
7650 node = TREE_OPERAND (node, 0);
7651 if (TREE_OPERAND (node, 0)
7652 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7653 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7654 (DECL_NAME (TREE_OPERAND (node, 0)))))
7655 identified = 1;
7656 }
7657 }
7658
7659 if (identified && name && arg_type && arg)
7660 {
7661 tree argument = TREE_OPERAND (node, 1);
7662 *name = DECL_NAME (TREE_OPERAND (node, 0));
7663 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7664 *arg = TREE_VALUE (argument);
7665 }
7666 return identified;
7667}
7668
7669/* Detect in NODE an outer field read access from an inner class and
7670 transform it into a write with RHS as an argument. This function is
7671 called from the java_complete_lhs when an assignment to a LHS can
7672 be identified. */
7673
7674static tree
7675outer_field_access_fix (wfl, node, rhs)
7676 tree wfl, node, rhs;
7677{
7678 tree name, arg_type, arg;
7679
7680 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7681 {
7682 /* At any rate, check whether we're trying to assign a value to
7683 a final. */
7684 tree accessed = (JDECL_P (node) ? node :
7685 (TREE_CODE (node) == COMPONENT_REF ?
7686 TREE_OPERAND (node, 1) : node));
7687 if (check_final_assignment (accessed, wfl))
7688 return error_mark_node;
7689
7690 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7691 arg_type, name, arg, rhs);
7692 return java_complete_tree (node);
7693 }
7694 return NULL_TREE;
7695}
7696
7697/* Construct the expression that calls an access method:
7698 <type>.access$<n>(<arg1> [, <arg2>]);
7699
7700 ARG2 can be NULL and will be omitted in that case. It will denote a
7701 read access. */
7702
7703static tree
7704build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7705 int lc;
7706 tree type, access_method_name, arg1, arg2;
7707{
7708 tree args, cn, access;
7709
7710 args = arg1 ? arg1 :
7711 build_wfl_node (build_current_thisn (current_class));
7712 args = build_tree_list (NULL_TREE, args);
7713
7714 if (arg2)
7715 args = tree_cons (NULL_TREE, arg2, args);
7716
7717 access = build_method_invocation (build_wfl_node (access_method_name), args);
7718 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7719 return make_qualified_primary (cn, access, lc);
7720}
7721
7722static tree
7723build_new_access_id ()
7724{
7725 static int access_n_counter = 1;
7726 char buffer [128];
7727
7728 sprintf (buffer, "access$%d", access_n_counter++);
7729 return get_identifier (buffer);
7730}
7731
7732/* Create the static access functions for the outer field DECL. We define a
7733 read:
7734 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7735 return inst$.field;
7736 }
7737 and a write access:
7738 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7739 TREE_TYPE (<field>) value$) {
7740 return inst$.field = value$;
7741 }
7742 We should have a usage flags on the DECL so we can lazily turn the ones
7743 we're using for code generation. FIXME.
7744*/
7745
7746static tree
7747build_outer_field_access_methods (decl)
7748 tree decl;
7749{
7750 tree id, args, stmt, mdecl;
7751
7752 /* Check point, to be removed. FIXME */
7753 if (FIELD_INNER_ACCESS (decl)
7754 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7755 abort ();
7756
7757 if (FIELD_INNER_ACCESS (decl))
7758 return FIELD_INNER_ACCESS (decl);
7759
7760 push_obstacks (&permanent_obstack, &permanent_obstack);
7761
7762 /* Create the identifier and a function named after it. */
7763 id = build_new_access_id ();
7764
7765 /* The identifier is marked as bearing the name of a generated write
7766 access function for outer field accessed from inner classes. */
7767 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7768
7769 /* Create the read access */
7770 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7771 TREE_CHAIN (args) = end_params_node;
7772 stmt = make_qualified_primary (build_wfl_node (inst_id),
7773 build_wfl_node (DECL_NAME (decl)), 0);
7774 stmt = build_return (0, stmt);
7775 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7776 TREE_TYPE (decl), id, args, stmt);
7777 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7778
7779 /* Create the write access method */
7780 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7781 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7782 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7783 stmt = make_qualified_primary (build_wfl_node (inst_id),
7784 build_wfl_node (DECL_NAME (decl)), 0);
7785 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7786 build_wfl_node (wpv_id)));
7787
7788 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7789 TREE_TYPE (decl), id, args, stmt);
7790 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7791 pop_obstacks ();
7792
7793 /* Return the access name */
7794 return FIELD_INNER_ACCESS (decl) = id;
7795}
7796
7797/* Build an field access method NAME. */
7798
7799static tree
7800build_outer_field_access_method (class, type, name, args, body)
7801 tree class, type, name, args, body;
7802{
7803 tree saved_current_function_decl, mdecl;
7804
7805 /* Create the method */
7806 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7807 fix_method_argument_names (args, mdecl);
7808 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7809
7810 /* Attach the method body. */
7811 saved_current_function_decl = current_function_decl;
7812 start_artificial_method_body (mdecl);
7813 java_method_add_stmt (mdecl, body);
7814 end_artificial_method_body (mdecl);
7815 current_function_decl = saved_current_function_decl;
7816
7817 return mdecl;
7818}
7819
7820\f
7821/* This section deals with building access function necessary for
7822 certain kinds of method invocation from inner classes. */
7823
7824static tree
7825build_outer_method_access_method (decl)
7826 tree decl;
7827{
7828 tree saved_current_function_decl, mdecl;
7829 tree args = NULL_TREE, call_args = NULL_TREE;
7830 tree carg, id, body, class;
7831 char buffer [80];
7832 int parm_id_count = 0;
7833
7834 /* Test this abort with an access to a private field */
7835 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7836 abort ();
7837
7838 /* Check the cache first */
7839 if (DECL_FUNCTION_INNER_ACCESS (decl))
7840 return DECL_FUNCTION_INNER_ACCESS (decl);
7841
7842 class = DECL_CONTEXT (decl);
7843
7844 /* Obtain an access identifier and mark it */
7845 id = build_new_access_id ();
7846 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7847
7848 push_obstacks (&permanent_obstack, &permanent_obstack);
7849
7850 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7851 /* Create the arguments, as much as the original */
7852 for (; carg && carg != end_params_node;
7853 carg = TREE_CHAIN (carg))
7854 {
7855 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7856 args = chainon (args, build_tree_list (get_identifier (buffer),
7857 TREE_VALUE (carg)));
7858 }
7859 args = chainon (args, end_params_node);
7860
7861 /* Create the method */
7862 mdecl = create_artificial_method (class, ACC_STATIC,
7863 TREE_TYPE (TREE_TYPE (decl)), id, args);
7864 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7865 /* There is a potential bug here. We should be able to use
7866 fix_method_argument_names, but then arg names get mixed up and
7867 eventually a constructor will have its this$0 altered and the
7868 outer context won't be assignment properly. The test case is
7869 stub.java FIXME */
7870 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7871
7872 /* Attach the method body. */
7873 saved_current_function_decl = current_function_decl;
7874 start_artificial_method_body (mdecl);
7875
7876 /* The actual method invocation uses the same args. When invoking a
7877 static methods that way, we don't want to skip the first
7878 argument. */
7879 carg = args;
7880 if (!METHOD_STATIC (decl))
7881 carg = TREE_CHAIN (carg);
7882 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7883 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7884 call_args);
7885
7886 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7887 call_args);
7888 if (!METHOD_STATIC (decl))
7889 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
7890 body, 0);
7891 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
7892 body = build_return (0, body);
7893 java_method_add_stmt (mdecl,body);
7894 end_artificial_method_body (mdecl);
7895 current_function_decl = saved_current_function_decl;
7896 pop_obstacks ();
7897
7898 /* Back tag the access function so it know what it accesses */
7899 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
7900
7901 /* Tag the current method so it knows it has an access generated */
7902 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
7903}
7904
7905\f
7906/* This section of the code deals with building expressions to access
7907 the enclosing instance of an inner class. The enclosing instance is
7908 kept in a generated field called this$<n>, with <n> being the
7909 inner class nesting level (starting from 0.) */
7910
7911/* Build an access to a given this$<n>, possibly by chaining access
7912 call to others. Access methods to this$<n> are build on the fly if
7913 necessary */
7914
7915static tree
7916build_access_to_thisn (from, to, lc)
7917 tree from, to;
7918 int lc;
7919{
7920 tree access = NULL_TREE;
7921
7922 while (from != to)
7923 {
7924 tree access0_wfl, cn;
7925
7926 maybe_build_thisn_access_method (from);
7927 access0_wfl = build_wfl_node (access0_identifier_node);
7928 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
7929 EXPR_WFL_LINECOL (access0_wfl) = lc;
7930
7931 if (!access)
7932 {
7933 access = build_current_thisn (current_class);
7934 access = build_wfl_node (access);
7935 }
7936 access = build_tree_list (NULL_TREE, access);
7937 access = build_method_invocation (access0_wfl, access);
7938 access = make_qualified_primary (cn, access, lc);
7939
7940 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
7941 }
7942 return access;
7943}
7944
7945/* Build an access function to the this$<n> local to TYPE. NULL_TREE
7946 is returned if nothing needs to be generated. Otherwise, the method
152de068 7947 generated and a method decl is returned.
c2952b01
APB
7948
7949 NOTE: These generated methods should be declared in a class file
7950 attribute so that they can't be referred to directly. */
7951
7952static tree
7953maybe_build_thisn_access_method (type)
7954 tree type;
7955{
7956 tree mdecl, args, stmt, rtype;
7957 tree saved_current_function_decl;
7958
7959 /* If TYPE is a top-level class, no access method is required.
7960 If there already is such an access method, bail out. */
7961 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
7962 return NULL_TREE;
7963
7964 /* We generate the method. The method looks like:
7965 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
7966 */
7967 push_obstacks (&permanent_obstack, &permanent_obstack);
7968 args = build_tree_list (inst_id, build_pointer_type (type));
7969 TREE_CHAIN (args) = end_params_node;
7970 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
7971 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
7972 access0_identifier_node, args);
7973 fix_method_argument_names (args, mdecl);
7974 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
7975 stmt = build_current_thisn (type);
7976 stmt = make_qualified_primary (build_wfl_node (inst_id),
7977 build_wfl_node (stmt), 0);
7978 stmt = build_return (0, stmt);
7979
7980 saved_current_function_decl = current_function_decl;
7981 start_artificial_method_body (mdecl);
7982 java_method_add_stmt (mdecl, stmt);
7983 end_artificial_method_body (mdecl);
7984 current_function_decl = saved_current_function_decl;
7985 pop_obstacks ();
7986
7987 CLASS_ACCESS0_GENERATED_P (type) = 1;
7988
7989 return mdecl;
7990}
7991
7992/* Craft an correctly numbered `this$<n>'string. this$0 is used for
7993 the first level of innerclassing. this$1 for the next one, etc...
7994 This function can be invoked with TYPE to NULL, available and then
7995 has to count the parser context. */
7996
7997static tree
7998build_current_thisn (type)
7999 tree type;
8000{
8001 static int saved_i = -1;
8002 static tree saved_thisn = NULL_TREE;
8003
8004 tree decl;
8005 char buffer [80];
8006 int i = 0;
8007
8008 if (type)
8009 {
8010 static tree saved_type = NULL_TREE;
8011 static int saved_type_i = 0;
8012
8013 if (type == saved_type)
8014 i = saved_type_i;
8015 else
8016 {
8017 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8018 decl; decl = DECL_CONTEXT (decl), i++)
8019 ;
8020
8021 saved_type = type;
8022 saved_type_i = i;
8023 }
8024 }
8025 else
8026 i = list_length (GET_CPC_LIST ())-2;
8027
8028 if (i == saved_i)
8029 return saved_thisn;
8030
8031 sprintf (buffer, "this$%d", i);
8032 saved_i = i;
8033 saved_thisn = get_identifier (buffer);
8034 return saved_thisn;
8035}
8036
8037/* Return the assignement to the hidden enclosing context `this$<n>'
8038 by the second incoming parameter to the innerclass constructor. The
8039 form used is `this.this$<n> = this$<n>;'. */
8040
8041static tree
8042build_thisn_assign ()
8043{
8044 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8045 {
8046 tree thisn = build_current_thisn (current_class);
8047 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8048 build_wfl_node (thisn), 0);
8049 tree rhs = build_wfl_node (thisn);
8050 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8051 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8052 }
8053 return NULL_TREE;
8054}
8055
8056\f
165f37bc
APB
8057/* Building the synthetic `class$' used to implement the `.class' 1.1
8058 extension for non primitive types. This method looks like:
8059
8060 static Class class$(String type) throws NoClassDefFoundError
8061 {
8062 try {return (java.lang.Class.forName (String));}
8063 catch (ClassNotFoundException e) {
8064 throw new NoClassDefFoundError(e.getMessage());}
8065 } */
8066
8067static tree
8068build_dot_class_method (class)
8069 tree class;
8070{
8071#define BWF(S) build_wfl_node (get_identifier ((S)))
8072#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8073 tree args, tmp, saved_current_function_decl, mdecl;
8074 tree stmt, throw_stmt, catch, catch_block, try_block;
8075 tree catch_clause_param;
8076 tree class_not_found_exception, no_class_def_found_error;
8077
8078 static tree get_message_wfl, type_parm_wfl;
8079
8080 if (!get_message_wfl)
8081 {
8082 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8083 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8084 }
8085
8086 /* Build the arguments */
8087 args = build_tree_list (get_identifier ("type$"),
8088 build_pointer_type (string_type_node));
8089 TREE_CHAIN (args) = end_params_node;
8090
8091 /* Build the qualified name java.lang.Class.forName */
8092 tmp = MQN (MQN (MQN (BWF ("java"),
8093 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8094
8095 /* For things we have to catch and throw */
8096 class_not_found_exception =
8097 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8098 no_class_def_found_error =
8099 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8100 load_class (class_not_found_exception, 1);
8101 load_class (no_class_def_found_error, 1);
8102
8103 /* Create the "class$" function */
8104 mdecl = create_artificial_method (class, ACC_STATIC,
8105 build_pointer_type (class_type_node),
8106 get_identifier ("class$"), args);
8107 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8108 no_class_def_found_error);
8109
8110 /* We start by building the try block. We need to build:
8111 return (java.lang.Class.forName (type)); */
8112 stmt = build_method_invocation (tmp,
8113 build_tree_list (NULL_TREE, type_parm_wfl));
8114 stmt = build_return (0, stmt);
8115 /* Put it in a block. That's the try block */
8116 try_block = build_expr_block (stmt, NULL_TREE);
8117
8118 /* Now onto the catch block. We start by building the expression
8119 throwing a new exception:
8120 throw new NoClassDefFoundError (_.getMessage); */
8121 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8122 get_message_wfl, 0);
8123 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8124
8125 /* Build new NoClassDefFoundError (_.getMessage) */
8126 throw_stmt = build_new_invocation
8127 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8128 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8129
8130 /* Build the throw, (it's too early to use BUILD_THROW) */
8131 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8132
8133 /* Build the catch block to encapsulate all this. We begin by
8134 building an decl for the catch clause parameter and link it to
8135 newly created block, the catch block. */
8136 catch_clause_param =
8137 build_decl (VAR_DECL, wpv_id,
8138 build_pointer_type (class_not_found_exception));
8139 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8140
8141 /* We initialize the variable with the exception handler. */
8142 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8143 soft_exceptioninfo_call_node);
8144 add_stmt_to_block (catch_block, NULL_TREE, catch);
8145
8146 /* We add the statement throwing the new exception */
8147 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8148
8149 /* Build a catch expression for all this */
8150 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8151
8152 /* Build the try/catch sequence */
8153 stmt = build_try_statement (0, try_block, catch_block);
8154
8155 fix_method_argument_names (args, mdecl);
8156 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8157 saved_current_function_decl = current_function_decl;
8158 start_artificial_method_body (mdecl);
8159 java_method_add_stmt (mdecl, stmt);
8160 end_artificial_method_body (mdecl);
8161 current_function_decl = saved_current_function_decl;
8162 TYPE_DOT_CLASS (class) = mdecl;
8163
8164 return mdecl;
8165}
8166
8167static tree
8168build_dot_class_method_invocation (name)
8169 tree name;
8170{
8171 tree s = make_node (STRING_CST);
8172 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8173 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8174 TREE_STRING_LENGTH (s)+1);
8175 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8176 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8177 build_tree_list (NULL_TREE, s));
8178}
8179
c2952b01
APB
8180/* This section of the code deals with constructor. */
8181
22eed1e6
APB
8182/* Craft a body for default constructor. Patch existing constructor
8183 bodies with call to super() and field initialization statements if
8184 necessary. */
8185
8186static void
8187fix_constructors (mdecl)
8188 tree mdecl;
8189{
8190 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8191 tree thisn_assign, compound = NULL_TREE;
8192 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8193
22eed1e6
APB
8194 if (!body)
8195 {
22eed1e6
APB
8196 /* It is an error for the compiler to generate a default
8197 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8198 takes no argument, or the same args for an anonymous class */
8199 if (verify_constructor_super (mdecl))
22eed1e6 8200 {
c2952b01
APB
8201 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8202 tree save = DECL_NAME (mdecl);
49f48c71 8203 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8204 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8205 parse_error_context
c2952b01
APB
8206 (lookup_cl (TYPE_NAME (class_type)),
8207 "No constructor matching `%s' found in class `%s'",
8208 lang_printable_name (mdecl, 0), n);
8209 DECL_NAME (mdecl) = save;
22eed1e6
APB
8210 }
8211
c2952b01
APB
8212 /* The constructor body must be crafted by hand. It's the
8213 constructor we defined when we realize we didn't have the
8214 CLASSNAME() constructor */
22eed1e6
APB
8215 start_artificial_method_body (mdecl);
8216
8217 /* We don't generate a super constructor invocation if we're
8218 compiling java.lang.Object. build_super_invocation takes care
8219 of that. */
e920ebc9 8220 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8221
c2952b01
APB
8222 /* Insert the instance initializer block right here, after the
8223 super invocation. */
8224 add_instance_initializer (mdecl);
8225
8226 /* Insert an assignment to the this$<n> hidden field, if
8227 necessary */
8228 if ((thisn_assign = build_thisn_assign ()))
8229 java_method_add_stmt (mdecl, thisn_assign);
8230
22eed1e6
APB
8231 end_artificial_method_body (mdecl);
8232 }
8233 /* Search for an explicit constructor invocation */
8234 else
8235 {
8236 int found = 0;
8237 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8238
8239 while (body)
8240 switch (TREE_CODE (body))
8241 {
8242 case CALL_EXPR:
8243 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8244 body = NULL_TREE;
8245 break;
8246 case COMPOUND_EXPR:
8247 case EXPR_WITH_FILE_LOCATION:
8248 body = TREE_OPERAND (body, 0);
8249 break;
8250 case BLOCK:
8251 body = BLOCK_EXPR_BODY (body);
8252 break;
8253 default:
8254 found = 0;
8255 body = NULL_TREE;
8256 }
8257 /* The constructor is missing an invocation of super() */
8258 if (!found)
8259 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8260 build_super_invocation (mdecl));
22eed1e6 8261
c2952b01
APB
8262 /* Insert the instance initializer block right here, after the
8263 super invocation. */
8264 add_instance_initializer (mdecl);
8265
8266 /* Generate the assignment to this$<n>, if necessary */
8267 if ((thisn_assign = build_thisn_assign ()))
8268 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8269
22eed1e6
APB
8270 /* Fix the constructor main block if we're adding extra stmts */
8271 if (compound)
8272 {
8273 compound = add_stmt_to_compound (compound, NULL_TREE,
8274 BLOCK_EXPR_BODY (main_block));
8275 BLOCK_EXPR_BODY (main_block) = compound;
8276 }
8277 }
8278}
8279
8280/* Browse constructors in the super class, searching for a constructor
8281 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8282 otherwise. If the current class is an anonymous inner class, look
8283 for something that has the same signature. */
22eed1e6
APB
8284
8285static int
c2952b01
APB
8286verify_constructor_super (mdecl)
8287 tree mdecl;
22eed1e6
APB
8288{
8289 tree class = CLASSTYPE_SUPER (current_class);
152de068 8290 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8291 tree sdecl;
8292
22eed1e6
APB
8293 if (!class)
8294 return 0;
8295
c2952b01 8296 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8297 {
c2952b01
APB
8298 tree mdecl_arg_type;
8299 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8300 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8301 if (DECL_CONSTRUCTOR_P (sdecl))
8302 {
152de068
APB
8303 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8304 if (super_inner)
8305 arg_type = TREE_CHAIN (arg_type);
8306 for (; (arg_type != end_params_node
8307 && mdecl_arg_type != end_params_node);
c2952b01
APB
8308 arg_type = TREE_CHAIN (arg_type),
8309 mdecl_arg_type = TREE_CHAIN (mdecl_arg_type))
8310 if (TREE_VALUE (arg_type) != TREE_VALUE (mdecl_arg_type))
8311 break;
8312
8313 if (arg_type == end_params_node &&
8314 mdecl_arg_type == end_params_node)
8315 return 0;
8316 }
8317 }
8318 else
8319 {
8320 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8321 {
152de068
APB
8322 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8323 if (super_inner)
8324 arg = TREE_CHAIN (arg);
8325 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8326 return 0;
8327 }
8328 }
8329 return 1;
8330}
8331
22eed1e6 8332/* Generate code for all context remembered for code generation. */
b351b287
APB
8333
8334void
8335java_expand_classes ()
8336{
5423609c 8337 int save_error_count = 0;
c2952b01
APB
8338 static struct parser_ctxt *saved_ctxp = NULL;
8339
23a79c61
APB
8340 java_parse_abort_on_error ();
8341 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8342 return;
8343 java_layout_classes ();
8344 java_parse_abort_on_error ();
8345
48a840d9
APB
8346 /* The list of packages declaration seen so far needs to be
8347 reversed, so that package declared in a file being compiled gets
8348 priority over packages declared as a side effect of parsing other
8349 files.*/
8350 package_list = nreverse (package_list);
8351
c2952b01 8352 saved_ctxp = ctxp_for_generation;
b351b287
APB
8353 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8354 {
8355 ctxp = ctxp_for_generation;
8356 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8357 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8358 java_parse_abort_on_error ();
8359 }
c2952b01
APB
8360
8361 /* Find anonymous classes and expand their constructor, now they
8362 have been fixed. */
8363 for (ctxp_for_generation = saved_ctxp;
8364 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8365 {
8366 tree current;
8367 ctxp = ctxp_for_generation;
8368 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8369 {
8370 current_class = TREE_TYPE (current);
8371 if (ANONYMOUS_CLASS_P (current_class))
8372 {
8373 tree d;
8374 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8375 {
8376 if (DECL_CONSTRUCTOR_P (d))
8377 {
8378 restore_line_number_status (1);
8379 reset_method_name (d);
8380 java_complete_expand_method (d);
8381 restore_line_number_status (0);
8382 break; /* We now there are no other ones */
8383 }
8384 }
8385 }
8386 }
8387 }
8388
8389 /* If we've found error at that stage, don't try to generate
8390 anything, unless we're emitting xrefs or checking the syntax only
8391 (but not using -fsyntax-only for the purpose of generating
8392 bytecode. */
8393 if (java_error_count && !flag_emit_xref
8394 && (!flag_syntax_only && !flag_emit_class_files))
8395 return;
8396
8397 /* Now things are stable, go for generation of the class data. */
8398 for (ctxp_for_generation = saved_ctxp;
8399 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8400 {
8401 tree current;
8402 ctxp = ctxp_for_generation;
8403 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8404 {
8405 current_class = TREE_TYPE (current);
8406 outgoing_cpool = TYPE_CPOOL (current_class);
8407 if (flag_emit_class_files)
8408 write_classfile (current_class);
8409 if (flag_emit_xref)
8410 expand_xref (current_class);
8411 else if (! flag_syntax_only)
8412 finish_class ();
8413 }
8414 }
b351b287
APB
8415}
8416
e04a16fb
AG
8417/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8418 a tree list node containing RIGHT. Fore coming RIGHTs will be
8419 chained to this hook. LOCATION contains the location of the
8420 separating `.' operator. */
8421
8422static tree
8423make_qualified_primary (primary, right, location)
8424 tree primary, right;
8425 int location;
8426{
8427 tree wfl;
8428
c2952b01
APB
8429 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8430 wfl = build_wfl_wrap (primary);
e04a16fb
AG
8431 else
8432 {
8433 wfl = primary;
c2952b01
APB
8434 /* If wfl wasn't qualified, we build a first anchor */
8435 if (!EXPR_WFL_QUALIFICATION (wfl))
8436 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8437 }
8438
c2952b01 8439 /* And chain them */
e04a16fb
AG
8440 EXPR_WFL_LINECOL (right) = location;
8441 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8442 PRIMARY_P (wfl) = 1;
8443 return wfl;
8444}
8445
8446/* Simple merge of two name separated by a `.' */
8447
8448static tree
8449merge_qualified_name (left, right)
8450 tree left, right;
8451{
8452 tree node;
c2952b01
APB
8453 if (!left && !right)
8454 return NULL_TREE;
8455
8456 if (!left)
8457 return right;
8458
8459 if (!right)
8460 return left;
8461
e04a16fb
AG
8462 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8463 IDENTIFIER_LENGTH (left));
8464 obstack_1grow (&temporary_obstack, '.');
8465 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8466 IDENTIFIER_LENGTH (right));
8467 node = get_identifier (obstack_base (&temporary_obstack));
8468 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8469 QUALIFIED_P (node) = 1;
8470 return node;
8471}
8472
8473/* Merge the two parts of a qualified name into LEFT. Set the
8474 location information of the resulting node to LOCATION, usually
8475 inherited from the location information of the `.' operator. */
8476
8477static tree
8478make_qualified_name (left, right, location)
8479 tree left, right;
8480 int location;
8481{
bc3ca41b
PB
8482#ifdef USE_COMPONENT_REF
8483 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8484 EXPR_WFL_LINECOL (node) = location;
8485 return node;
8486#else
e04a16fb
AG
8487 tree left_id = EXPR_WFL_NODE (left);
8488 tree right_id = EXPR_WFL_NODE (right);
8489 tree wfl, merge;
8490
8491 merge = merge_qualified_name (left_id, right_id);
8492
8493 /* Left wasn't qualified and is now qualified */
8494 if (!QUALIFIED_P (left_id))
8495 {
8496 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8497 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8498 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8499 }
8500
8501 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8502 EXPR_WFL_LINECOL (wfl) = location;
8503 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8504
8505 EXPR_WFL_NODE (left) = merge;
8506 return left;
bc3ca41b 8507#endif
e04a16fb
AG
8508}
8509
8510/* Extract the last identifier component of the qualified in WFL. The
8511 last identifier is removed from the linked list */
8512
8513static tree
8514cut_identifier_in_qualified (wfl)
8515 tree wfl;
8516{
8517 tree q;
8518 tree previous = NULL_TREE;
8519 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8520 if (!TREE_CHAIN (q))
8521 {
8522 if (!previous)
781b0558 8523 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8524 TREE_CHAIN (previous) = NULL_TREE;
8525 return TREE_PURPOSE (q);
8526 }
8527}
8528
8529/* Resolve the expression name NAME. Return its decl. */
8530
8531static tree
5e942c50 8532resolve_expression_name (id, orig)
e04a16fb 8533 tree id;
5e942c50 8534 tree *orig;
e04a16fb
AG
8535{
8536 tree name = EXPR_WFL_NODE (id);
8537 tree decl;
8538
8539 /* 6.5.5.1: Simple expression names */
8540 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8541 {
8542 /* 15.13.1: NAME can appear within the scope of a local variable
8543 declaration */
8544 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8545 return decl;
8546
8547 /* 15.13.1: NAME can appear within a class declaration */
8548 else
8549 {
8550 decl = lookup_field_wrapper (current_class, name);
8551 if (decl)
8552 {
c2952b01 8553 tree access = NULL_TREE;
e04a16fb 8554 int fs = FIELD_STATIC (decl);
f2760b27
APB
8555
8556 /* If we're accessing an outer scope local alias, make
8557 sure we change the name of the field we're going to
8558 build access to. */
8559 if (FIELD_LOCAL_ALIAS_USED (decl))
8560 name = DECL_NAME (decl);
8561
e04a16fb
AG
8562 /* Instance variable (8.3.1.1) can't appear within
8563 static method, static initializer or initializer for
8564 a static variable. */
8565 if (!fs && METHOD_STATIC (current_function_decl))
8566 {
7f10c2e2 8567 static_ref_err (id, name, current_class);
e04a16fb
AG
8568 return error_mark_node;
8569 }
22eed1e6
APB
8570 /* Instance variables can't appear as an argument of
8571 an explicit constructor invocation */
8572 if (!fs && ctxp->explicit_constructor_p)
8573 {
8574 parse_error_context
781b0558 8575 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8576 return error_mark_node;
8577 }
5e942c50 8578
c2952b01
APB
8579 /* If we're processing an inner class and we're trying
8580 to access a field belonging to an outer class, build
8581 the access to the field */
8582 if (!fs && outer_field_access_p (current_class, decl))
8583 return build_outer_field_access (id, decl);
8584
5e942c50 8585 /* Otherwise build what it takes to access the field */
c2952b01
APB
8586 access = build_field_ref ((fs ? NULL_TREE : current_this),
8587 DECL_CONTEXT (decl), name);
e8fc7396 8588 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8589 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8590 /* We may be asked to save the real field access node */
8591 if (orig)
c2952b01 8592 *orig = access;
5e942c50 8593 /* And we return what we got */
c2952b01 8594 return access;
e04a16fb
AG
8595 }
8596 /* Fall down to error report on undefined variable */
8597 }
8598 }
8599 /* 6.5.5.2 Qualified Expression Names */
8600 else
8601 {
5e942c50
APB
8602 if (orig)
8603 *orig = NULL_TREE;
e04a16fb
AG
8604 qualify_ambiguous_name (id);
8605 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8606 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8607 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8608 }
8609
8610 /* We've got an error here */
8611 parse_error_context (id, "Undefined variable `%s'",
8612 IDENTIFIER_POINTER (name));
8613
8614 return error_mark_node;
8615}
8616
7f10c2e2
APB
8617static void
8618static_ref_err (wfl, field_id, class_type)
8619 tree wfl, field_id, class_type;
8620{
8621 parse_error_context
8622 (wfl,
8623 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8624 IDENTIFIER_POINTER (field_id),
8625 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8626}
8627
e04a16fb
AG
8628/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8629 We return something suitable to generate the field access. We also
8630 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8631 recipient's address can be null. */
8632
8633static tree
8634resolve_field_access (qual_wfl, field_decl, field_type)
8635 tree qual_wfl;
8636 tree *field_decl, *field_type;
8637{
8638 int is_static = 0;
8639 tree field_ref;
8640 tree decl, where_found, type_found;
8641
8642 if (resolve_qualified_expression_name (qual_wfl, &decl,
8643 &where_found, &type_found))
8644 return error_mark_node;
8645
8646 /* Resolve the LENGTH field of an array here */
8647 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
e8fc7396 8648 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8649 {
8650 tree length = build_java_array_length_access (where_found);
8651 field_ref =
8652 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8653
8654 /* In case we're dealing with a static array, we need to
8655 initialize its class before the array length can be fetched.
8656 It's also a good time to create a DECL_RTL for the field if
8657 none already exists, otherwise if the field was declared in a
8658 class found in an external file and hasn't been (and won't
8659 be) accessed for its value, none will be created. */
8660 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8661 {
8662 build_static_field_ref (where_found);
8663 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8664 }
e04a16fb
AG
8665 }
8666 /* We might have been trying to resolve field.method(). In which
8667 case, the resolution is over and decl is the answer */
34f4db93 8668 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8669 field_ref = decl;
34f4db93 8670 else if (JDECL_P (decl))
e04a16fb 8671 {
5e942c50
APB
8672 int static_final_found = 0;
8673 if (!type_found)
8674 type_found = DECL_CONTEXT (decl);
34f4db93 8675 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
5e942c50
APB
8676 if (FIELD_FINAL (decl)
8677 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8678 && DECL_INITIAL (decl))
5e942c50 8679 {
0f145be8 8680 field_ref = java_complete_tree (DECL_INITIAL (decl));
5e942c50
APB
8681 static_final_found = 1;
8682 }
8683 else
7f10c2e2
APB
8684 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8685 NULL_TREE : where_found),
5e942c50 8686 type_found, DECL_NAME (decl));
e04a16fb
AG
8687 if (field_ref == error_mark_node)
8688 return error_mark_node;
e8fc7396
APB
8689 if (is_static && !static_final_found
8690 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8691 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8692 }
8693 else
8694 field_ref = decl;
8695
8696 if (field_decl)
8697 *field_decl = decl;
8698 if (field_type)
c877974e
APB
8699 *field_type = (QUAL_DECL_TYPE (decl) ?
8700 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8701 return field_ref;
8702}
8703
e28cd97b
APB
8704/* If NODE is an access to f static field, strip out the class
8705 initialization part and return the field decl, otherwise, return
8706 NODE. */
8707
8708static tree
8709strip_out_static_field_access_decl (node)
8710 tree node;
8711{
8712 if (TREE_CODE (node) == COMPOUND_EXPR)
8713 {
8714 tree op1 = TREE_OPERAND (node, 1);
8715 if (TREE_CODE (op1) == COMPOUND_EXPR)
8716 {
8717 tree call = TREE_OPERAND (op1, 0);
8718 if (TREE_CODE (call) == CALL_EXPR
8719 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8720 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8721 == soft_initclass_node)
8722 return TREE_OPERAND (op1, 1);
8723 }
2f11d407
TT
8724 else if (JDECL_P (op1))
8725 return op1;
e28cd97b
APB
8726 }
8727 return node;
8728}
8729
e04a16fb
AG
8730/* 6.5.5.2: Qualified Expression Names */
8731
8732static int
8733resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8734 tree wfl;
8735 tree *found_decl, *type_found, *where_found;
8736{
8737 int from_type = 0; /* Field search initiated from a type */
c2952b01 8738 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8739 int previous_call_static = 0;
8740 int is_static;
8741 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8742 /* For certain for of inner class instantiation */
8743 tree saved_current, saved_this;
8744#define RESTORE_THIS_AND_CURRENT_CLASS \
8745 { current_class = saved_current; current_this = saved_this;}
8746
c877974e 8747 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8748
8749 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8750 {
8751 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8752 tree ret_decl; /* for EH checking */
8753 int location; /* for EH checking */
e04a16fb
AG
8754
8755 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8756 switch (TREE_CODE (qual_wfl))
8757 {
8758 case CALL_EXPR:
b67d701b 8759 case NEW_CLASS_EXPR:
e04a16fb
AG
8760 /* If the access to the function call is a non static field,
8761 build the code to access it. */
34f4db93 8762 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8763 {
ac825856
APB
8764 decl = maybe_access_field (decl, *where_found,
8765 DECL_CONTEXT (decl));
e04a16fb
AG
8766 if (decl == error_mark_node)
8767 return 1;
8768 }
c2952b01 8769
e04a16fb
AG
8770 /* And code for the function call */
8771 if (complete_function_arguments (qual_wfl))
8772 return 1;
c2952b01
APB
8773
8774 /* We might have to setup a new current class and a new this
8775 for the search of an inner class, relative to the type of
8776 a expression resolved as `decl'. The current values are
8777 saved and restored shortly after */
8778 saved_current = current_class;
8779 saved_this = current_this;
8780 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8781 {
8782 current_class = type;
8783 current_this = decl;
8784 }
8785
89e09b9a
PB
8786 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8787 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8788 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8789 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8790 *where_found = patch_method_invocation (qual_wfl, decl, type,
8791 &is_static, &ret_decl);
e04a16fb 8792 if (*where_found == error_mark_node)
c2952b01
APB
8793 {
8794 RESTORE_THIS_AND_CURRENT_CLASS;
8795 return 1;
8796 }
e04a16fb
AG
8797 *type_found = type = QUAL_DECL_TYPE (*where_found);
8798
c2952b01
APB
8799 /* If we're creating an inner class instance, check for that
8800 an enclosing instance is in scope */
8801 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8802 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8803 {
8804 parse_error_context
165f37bc
APB
8805 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8806 lang_printable_name (type, 0),
8807 (!current_this ? "" :
8808 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8809 RESTORE_THIS_AND_CURRENT_CLASS;
8810 return 1;
8811 }
8812
8813 /* In case we had to change then to resolve a inner class
8814 instantiation using a primary qualified by a `new' */
8815 RESTORE_THIS_AND_CURRENT_CLASS;
8816
7705e9db
APB
8817 /* EH check */
8818 if (location)
8819 check_thrown_exceptions (location, ret_decl);
8820
e04a16fb
AG
8821 /* If the previous call was static and this one is too,
8822 build a compound expression to hold the two (because in
8823 that case, previous function calls aren't transported as
8824 forcoming function's argument. */
8825 if (previous_call_static && is_static)
8826 {
8827 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8828 TREE_SIDE_EFFECTS (decl) = 1;
8829 }
8830 else
8831 {
8832 previous_call_static = is_static;
8833 decl = *where_found;
8834 }
c2952b01 8835 from_type = 0;
e04a16fb
AG
8836 continue;
8837
d8fccff5 8838 case NEW_ARRAY_EXPR:
c2952b01 8839 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
8840 *where_found = decl = java_complete_tree (qual_wfl);
8841 if (decl == error_mark_node)
8842 return 1;
8843 *type_found = type = QUAL_DECL_TYPE (decl);
8844 CLASS_LOADED_P (type) = 1;
8845 continue;
8846
e04a16fb
AG
8847 case CONVERT_EXPR:
8848 *where_found = decl = java_complete_tree (qual_wfl);
8849 if (decl == error_mark_node)
8850 return 1;
8851 *type_found = type = QUAL_DECL_TYPE (decl);
8852 from_cast = 1;
8853 continue;
8854
22eed1e6 8855 case CONDITIONAL_EXPR:
5e942c50 8856 case STRING_CST:
ac22f9cb 8857 case MODIFY_EXPR:
22eed1e6
APB
8858 *where_found = decl = java_complete_tree (qual_wfl);
8859 if (decl == error_mark_node)
8860 return 1;
8861 *type_found = type = QUAL_DECL_TYPE (decl);
8862 continue;
8863
e04a16fb
AG
8864 case ARRAY_REF:
8865 /* If the access to the function call is a non static field,
8866 build the code to access it. */
34f4db93 8867 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
8868 {
8869 decl = maybe_access_field (decl, *where_found, type);
8870 if (decl == error_mark_node)
8871 return 1;
8872 }
8873 /* And code for the array reference expression */
8874 decl = java_complete_tree (qual_wfl);
8875 if (decl == error_mark_node)
8876 return 1;
8877 type = QUAL_DECL_TYPE (decl);
8878 continue;
0a2138e2 8879
37feda7d
APB
8880 case PLUS_EXPR:
8881 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8882 return 1;
8883 if ((type = patch_string (decl)))
8884 decl = type;
8885 *where_found = QUAL_RESOLUTION (q) = decl;
8886 *type_found = type = TREE_TYPE (decl);
8887 break;
8888
165f37bc
APB
8889 case CLASS_LITERAL:
8890 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8891 return 1;
8892 *where_found = QUAL_RESOLUTION (q) = decl;
8893 *type_found = type = TREE_TYPE (decl);
8894 break;
8895
0a2138e2
APB
8896 default:
8897 /* Fix for -Wall Just go to the next statement. Don't
8898 continue */
a3f406ce 8899 break;
e04a16fb
AG
8900 }
8901
8902 /* If we fall here, we weren't processing a (static) function call. */
8903 previous_call_static = 0;
8904
8905 /* It can be the keyword THIS */
8906 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
8907 {
8908 if (!current_this)
8909 {
8910 parse_error_context
8911 (wfl, "Keyword `this' used outside allowed context");
8912 return 1;
8913 }
f63991a8
APB
8914 if (ctxp->explicit_constructor_p)
8915 {
781b0558 8916 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
8917 return 1;
8918 }
e04a16fb 8919 /* We have to generate code for intermediate acess */
c2952b01
APB
8920 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
8921 {
8922 *where_found = decl = current_this;
8923 *type_found = type = QUAL_DECL_TYPE (decl);
8924 }
8925 /* We're trying to access the this from somewhere else... */
8926 else
8927 {
8928 *where_found = decl = build_current_thisn (type);
8929 from_qualified_this = 1;
8930 }
8931
8932 from_type = 0;
e04a16fb
AG
8933 continue;
8934 }
8935
8936 /* 15.10.2 Accessing Superclass Members using SUPER */
8937 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
8938 {
8939 tree node;
8940 /* Check on the restricted use of SUPER */
8941 if (METHOD_STATIC (current_function_decl)
8942 || current_class == object_type_node)
8943 {
8944 parse_error_context
8945 (wfl, "Keyword `super' used outside allowed context");
8946 return 1;
8947 }
8948 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
8949 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
8950 CLASSTYPE_SUPER (current_class),
8951 build_this (EXPR_WFL_LINECOL (qual_wfl)));
8952 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
8953 if (decl == error_mark_node)
8954 return 1;
e04a16fb
AG
8955 *type_found = type = QUAL_DECL_TYPE (decl);
8956 from_super = from_type = 1;
8957 continue;
8958 }
8959
8960 /* 15.13.1: Can't search for field name in packages, so we
8961 assume a variable/class name was meant. */
8962 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
8963 {
5e942c50
APB
8964 tree name = resolve_package (wfl, &q);
8965 if (name)
8966 {
c2952b01 8967 tree list;
5e942c50 8968 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 8969 /* We want to be absolutely sure that the class is laid
5e942c50
APB
8970 out. We're going to search something inside it. */
8971 *type_found = type = TREE_TYPE (decl);
8972 layout_class (type);
8973 from_type = 1;
c2952b01 8974
dde1da72
APB
8975 /* Fix them all the way down, if any are left. */
8976 if (q)
c2952b01 8977 {
dde1da72
APB
8978 list = TREE_CHAIN (q);
8979 while (list)
8980 {
8981 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
8982 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
8983 list = TREE_CHAIN (list);
8984 }
c2952b01 8985 }
5e942c50 8986 }
e04a16fb 8987 else
5e942c50
APB
8988 {
8989 if (from_super || from_cast)
8990 parse_error_context
8991 ((from_cast ? qual_wfl : wfl),
8992 "No variable `%s' defined in class `%s'",
8993 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
8994 lang_printable_name (type, 0));
8995 else
8996 parse_error_context
8997 (qual_wfl, "Undefined variable or class name: `%s'",
8998 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
8999 return 1;
9000 }
e04a16fb
AG
9001 }
9002
9003 /* We have a type name. It's been already resolved when the
9004 expression was qualified. */
9005 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9006 {
9007 if (!(decl = QUAL_RESOLUTION (q)))
9008 return 1; /* Error reported already */
9009
c2952b01
APB
9010 /* Sneak preview. If next we see a `new', we're facing a
9011 qualification with resulted in a type being selected
9012 instead of a field. Report the error */
9013 if(TREE_CHAIN (q)
9014 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9015 {
9016 parse_error_context (qual_wfl, "Undefined variable `%s'",
9017 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9018 return 1;
9019 }
9020
e04a16fb
AG
9021 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9022 {
9023 parse_error_context
9024 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9025 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9026 GET_TYPE_NAME (type),
e04a16fb
AG
9027 IDENTIFIER_POINTER (DECL_NAME (decl)),
9028 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9029 return 1;
9030 }
5e942c50 9031 check_deprecation (qual_wfl, decl);
c2952b01 9032
e04a16fb
AG
9033 type = TREE_TYPE (decl);
9034 from_type = 1;
9035 }
9036 /* We resolve and expression name */
9037 else
9038 {
cd531a2e 9039 tree field_decl = NULL_TREE;
e04a16fb
AG
9040
9041 /* If there exists an early resolution, use it. That occurs
9042 only once and we know that there are more things to
9043 come. Don't do that when processing something after SUPER
9044 (we need more thing to be put in place below */
9045 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9046 {
9047 decl = QUAL_RESOLUTION (q);
c877974e 9048 if (!type)
5e942c50 9049 {
7f10c2e2
APB
9050 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9051 {
9052 if (current_this)
9053 *where_found = current_this;
9054 else
9055 {
9056 static_ref_err (qual_wfl, DECL_NAME (decl),
9057 current_class);
9058 return 1;
9059 }
9060 }
c877974e
APB
9061 else
9062 {
9063 *where_found = TREE_TYPE (decl);
9064 if (TREE_CODE (*where_found) == POINTER_TYPE)
9065 *where_found = TREE_TYPE (*where_found);
9066 }
5e942c50 9067 }
b67d701b 9068 }
e04a16fb
AG
9069
9070 /* We have to search for a field, knowing the type of its
9071 container. The flag FROM_TYPE indicates that we resolved
9072 the last member of the expression as a type name, which
5e942c50
APB
9073 means that for the resolution of this field, we'll look
9074 for other errors than if it was resolved as a member of
9075 an other field. */
e04a16fb
AG
9076 else
9077 {
9078 int is_static;
5e942c50
APB
9079 tree field_decl_type; /* For layout */
9080
e04a16fb
AG
9081 if (!from_type && !JREFERENCE_TYPE_P (type))
9082 {
9083 parse_error_context
9084 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9085 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9086 lang_printable_name (type, 0),
e04a16fb
AG
9087 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9088 return 1;
9089 }
9090
dc0b3eff
PB
9091 field_decl = lookup_field_wrapper (type,
9092 EXPR_WFL_NODE (qual_wfl));
9093 if (field_decl == NULL_TREE)
e04a16fb
AG
9094 {
9095 parse_error_context
2aa11e97 9096 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9097 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9098 GET_TYPE_NAME (type));
e04a16fb
AG
9099 return 1;
9100 }
dc0b3eff
PB
9101 if (field_decl == error_mark_node)
9102 return 1;
5e942c50
APB
9103
9104 /* Layout the type of field_decl, since we may need
c877974e
APB
9105 it. Don't do primitive types or loaded classes. The
9106 situation of non primitive arrays may not handled
9107 properly here. FIXME */
5e942c50
APB
9108 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9109 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9110 else
9111 field_decl_type = TREE_TYPE (field_decl);
9112 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9113 && !CLASS_LOADED_P (field_decl_type)
9114 && !TYPE_ARRAY_P (field_decl_type))
9115 resolve_and_layout (field_decl_type, NULL_TREE);
9116 if (TYPE_ARRAY_P (field_decl_type))
9117 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9118
9119 /* Check on accessibility here */
9120 if (not_accessible_p (type, field_decl, from_super))
9121 {
9122 parse_error_context
9123 (qual_wfl,
9124 "Can't access %s field `%s.%s' from `%s'",
9125 java_accstring_lookup
9126 (get_access_flags_from_decl (field_decl)),
2aa11e97 9127 GET_TYPE_NAME (type),
e04a16fb
AG
9128 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9129 IDENTIFIER_POINTER
9130 (DECL_NAME (TYPE_NAME (current_class))));
9131 return 1;
9132 }
5e942c50 9133 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9134
9135 /* There are things to check when fields are accessed
9136 from type. There are no restrictions on a static
9137 declaration of the field when it is accessed from an
9138 interface */
9139 is_static = FIELD_STATIC (field_decl);
9140 if (!from_super && from_type
c2952b01
APB
9141 && !TYPE_INTERFACE_P (type)
9142 && !is_static
9143 && (current_function_decl
9144 && METHOD_STATIC (current_function_decl)))
e04a16fb 9145 {
7f10c2e2 9146 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9147 return 1;
9148 }
9149 from_cast = from_super = 0;
9150
c2952b01
APB
9151 /* It's an access from a type but it isn't static, we
9152 make it relative to `this'. */
9153 if (!is_static && from_type)
9154 decl = current_this;
9155
5e942c50
APB
9156 /* If we need to generate something to get a proper
9157 handle on what this field is accessed from, do it
9158 now. */
e04a16fb
AG
9159 if (!is_static)
9160 {
c583dd46 9161 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9162 if (decl == error_mark_node)
9163 return 1;
9164 }
9165
9166 /* We want to keep the location were found it, and the type
9167 we found. */
9168 *where_found = decl;
9169 *type_found = type;
9170
c2952b01
APB
9171 /* Generate the correct expression for field access from
9172 qualified this */
9173 if (from_qualified_this)
9174 {
9175 field_decl = build_outer_field_access (qual_wfl, field_decl);
9176 from_qualified_this = 0;
9177 }
9178
e04a16fb
AG
9179 /* This is the decl found and eventually the next one to
9180 search from */
9181 decl = field_decl;
9182 }
e04a16fb
AG
9183 from_type = 0;
9184 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9185
9186 /* Sneak preview. If decl is qualified by a `new', report
9187 the error here to be accurate on the peculiar construct */
9188 if (TREE_CHAIN (q)
9189 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9190 && !JREFERENCE_TYPE_P (type))
9191 {
9192 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9193 lang_printable_name (type, 0));
9194 return 1;
9195 }
e04a16fb 9196 }
dde1da72
APB
9197 /* `q' might have changed due to a after package resolution
9198 re-qualification */
9199 if (!q)
9200 break;
e04a16fb
AG
9201 }
9202 *found_decl = decl;
9203 return 0;
9204}
9205
9206/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9207 can't be accessed from REFERENCE (a record type). */
9208
be245ac0
KG
9209static int
9210not_accessible_p (reference, member, from_super)
e04a16fb
AG
9211 tree reference, member;
9212 int from_super;
9213{
9214 int access_flag = get_access_flags_from_decl (member);
9215
9216 /* Access always granted for members declared public */
9217 if (access_flag & ACC_PUBLIC)
9218 return 0;
9219
9220 /* Check access on protected members */
9221 if (access_flag & ACC_PROTECTED)
9222 {
9223 /* Access granted if it occurs from within the package
9224 containing the class in which the protected member is
9225 declared */
9226 if (class_in_current_package (DECL_CONTEXT (member)))
9227 return 0;
9228
9bbc7d9f
PB
9229 /* If accessed with the form `super.member', then access is granted */
9230 if (from_super)
9231 return 0;
e04a16fb 9232
9bbc7d9f
PB
9233 /* Otherwise, access is granted if occuring from the class where
9234 member is declared or a subclass of it */
473e7b07 9235 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9236 return 0;
e04a16fb
AG
9237 return 1;
9238 }
9239
9240 /* Check access on private members. Access is granted only if it
473e7b07
TT
9241 occurs from within the class in which it is declared. Exceptions
9242 are accesses from inner-classes. This section is probably not
c2952b01 9243 complete. FIXME */
e04a16fb 9244 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9245 return (current_class == DECL_CONTEXT (member) ? 0 :
9246 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9247
9248 /* Default access are permitted only when occuring within the
9249 package in which the type (REFERENCE) is declared. In other words,
9250 REFERENCE is defined in the current package */
9251 if (ctxp->package)
9252 return !class_in_current_package (reference);
473e7b07 9253
e04a16fb
AG
9254 /* Otherwise, access is granted */
9255 return 0;
9256}
9257
5e942c50
APB
9258/* Test deprecated decl access. */
9259static void
9260check_deprecation (wfl, decl)
9261 tree wfl, decl;
9262{
49f48c71 9263 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9264 /* Complain if the field is deprecated and the file it was defined
9265 in isn't compiled at the same time the file which contains its
9266 use is */
9267 if (DECL_DEPRECATED (decl)
9268 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9269 {
9270 char the [20];
9271 switch (TREE_CODE (decl))
9272 {
9273 case FUNCTION_DECL:
9274 strcpy (the, "method");
9275 break;
9276 case FIELD_DECL:
9277 strcpy (the, "field");
9278 break;
9279 case TYPE_DECL:
9280 strcpy (the, "class");
9281 break;
15fdcfe9
PB
9282 default:
9283 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9284 }
9285 parse_warning_context
9286 (wfl, "The %s `%s' in class `%s' has been deprecated",
9287 the, lang_printable_name (decl, 0),
9288 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9289 }
9290}
9291
e04a16fb
AG
9292/* Returns 1 if class was declared in the current package, 0 otherwise */
9293
9294static int
9295class_in_current_package (class)
9296 tree class;
9297{
9298 static tree cache = NULL_TREE;
9299 int qualified_flag;
9300 tree left;
9301
9302 if (cache == class)
9303 return 1;
9304
9305 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9306
9307 /* If the current package is empty and the name of CLASS is
9308 qualified, class isn't in the current package. If there is a
9309 current package and the name of the CLASS is not qualified, class
9310 isn't in the current package */
0a2138e2 9311 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9312 return 0;
9313
9314 /* If there is not package and the name of CLASS isn't qualified,
9315 they belong to the same unnamed package */
9316 if (!ctxp->package && !qualified_flag)
9317 return 1;
9318
9319 /* Compare the left part of the name of CLASS with the package name */
9320 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9321 if (ctxp->package == left)
9322 {
9323 cache = class;
9324 return 1;
9325 }
9326 return 0;
9327}
9328
9329/* This function may generate code to access DECL from WHERE. This is
9330 done only if certain conditions meet. */
9331
9332static tree
9333maybe_access_field (decl, where, type)
9334 tree decl, where, type;
9335{
5e942c50
APB
9336 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9337 && !FIELD_STATIC (decl))
e04a16fb 9338 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9339 (type ? type : DECL_CONTEXT (decl)),
9340 DECL_NAME (decl));
e04a16fb
AG
9341 return decl;
9342}
9343
15fdcfe9 9344/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9345 and according to the situation, PRIMARY and WHERE may be
9346 used. IS_STATIC is set to 1 if the invoked function is static. */
9347
9348static tree
89e09b9a 9349patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9350 tree patch, primary, where;
9351 int *is_static;
b9f7e36c 9352 tree *ret_decl;
e04a16fb
AG
9353{
9354 tree wfl = TREE_OPERAND (patch, 0);
9355 tree args = TREE_OPERAND (patch, 1);
9356 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9357 tree list;
22eed1e6 9358 int is_static_flag = 0;
89e09b9a 9359 int is_super_init = 0;
bccaf73a 9360 tree this_arg = NULL_TREE;
e04a16fb
AG
9361
9362 /* Should be overriden if everything goes well. Otherwise, if
9363 something fails, it should keep this value. It stop the
9364 evaluation of a bogus assignment. See java_complete_tree,
9365 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9366 evaluating an assignment */
9367 TREE_TYPE (patch) = error_mark_node;
9368
9369 /* Since lookup functions are messing with line numbers, save the
9370 context now. */
9371 java_parser_context_save_global ();
9372
9373 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9374
9375 /* Resolution of qualified name, excluding constructors */
9376 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9377 {
dde1da72 9378 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9379 /* Extract the last IDENTIFIER of the qualified
9380 expression. This is a wfl and we will use it's location
9381 data during error report. */
9382 identifier_wfl = cut_identifier_in_qualified (wfl);
9383 identifier = EXPR_WFL_NODE (identifier_wfl);
9384
9385 /* Given the context, IDENTIFIER is syntactically qualified
9386 as a MethodName. We need to qualify what's before */
9387 qualify_ambiguous_name (wfl);
dde1da72 9388 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9389
dde1da72
APB
9390 if (resolved == error_mark_node)
9391 PATCH_METHOD_RETURN_ERROR ();
9392
9393 type = GET_SKIP_TYPE (resolved);
9394 resolve_and_layout (type, NULL_TREE);
9395 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9396 args = nreverse (args);
2c56429a 9397
e04a16fb 9398 /* We're resolving a call from a type */
dde1da72 9399 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9400 {
dde1da72 9401 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9402 {
9403 parse_error_context
781b0558
KG
9404 (identifier_wfl,
9405 "Can't make static reference to method `%s' in interface `%s'",
9406 IDENTIFIER_POINTER (identifier),
e04a16fb 9407 IDENTIFIER_POINTER (name));
b9f7e36c 9408 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9409 }
e04a16fb
AG
9410 if (list && !METHOD_STATIC (list))
9411 {
c2e3db92 9412 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9413 parse_error_context
9414 (identifier_wfl,
9415 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9416 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9417 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9418 free (fct_name);
b9f7e36c 9419 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9420 }
9421 }
e04a16fb 9422 else
dde1da72
APB
9423 this_arg = primary = resolved;
9424
5e942c50 9425 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9426 wfl = identifier_wfl;
9427 }
9428 /* Resolution of simple names, names generated after a primary: or
9429 constructors */
9430 else
9431 {
cd531a2e 9432 tree class_to_search = NULL_TREE;
c2952b01 9433 int lc; /* Looking for Constructor */
e04a16fb
AG
9434
9435 /* We search constructor in their target class */
9436 if (CALL_CONSTRUCTOR_P (patch))
9437 {
22eed1e6
APB
9438 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9439 class_to_search = EXPR_WFL_NODE (wfl);
9440 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9441 this_identifier_node)
9442 class_to_search = NULL_TREE;
9443 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9444 super_identifier_node)
e04a16fb 9445 {
89e09b9a 9446 is_super_init = 1;
22eed1e6
APB
9447 if (CLASSTYPE_SUPER (current_class))
9448 class_to_search =
9449 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9450 else
9451 {
781b0558 9452 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9453 PATCH_METHOD_RETURN_ERROR ();
9454 }
e04a16fb 9455 }
22eed1e6
APB
9456
9457 /* Class to search is NULL if we're searching the current one */
9458 if (class_to_search)
e04a16fb 9459 {
c2952b01
APB
9460 class_to_search = resolve_and_layout (class_to_search, wfl);
9461
22eed1e6
APB
9462 if (!class_to_search)
9463 {
9464 parse_error_context
9465 (wfl, "Class `%s' not found in type declaration",
9466 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9467 PATCH_METHOD_RETURN_ERROR ();
9468 }
9469
5e942c50
APB
9470 /* Can't instantiate an abstract class, but we can
9471 invoke it's constructor. It's use within the `new'
9472 context is denied here. */
9473 if (CLASS_ABSTRACT (class_to_search)
9474 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9475 {
9476 parse_error_context
781b0558
KG
9477 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9478 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9479 PATCH_METHOD_RETURN_ERROR ();
9480 }
c2952b01 9481
22eed1e6 9482 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9483 }
22eed1e6
APB
9484 else
9485 class_to_search = current_class;
e04a16fb
AG
9486 lc = 1;
9487 }
9488 /* This is a regular search in the local class, unless an
9489 alternate class is specified. */
9490 else
9491 {
9492 class_to_search = (where ? where : current_class);
9493 lc = 0;
9494 }
c2952b01 9495
e04a16fb
AG
9496 /* NAME is a simple identifier or comes from a primary. Search
9497 in the class whose declaration contain the method being
9498 invoked. */
c877974e 9499 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9500
c2952b01 9501 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9502 /* Don't continue if no method were found, as the next statement
9503 can't be executed then. */
b9f7e36c
APB
9504 if (!list)
9505 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9506
9507 /* Check for static reference if non static methods */
9508 if (check_for_static_method_reference (wfl, patch, list,
9509 class_to_search, primary))
b9f7e36c 9510 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9511
165f37bc
APB
9512 /* Check for inner classes creation from illegal contexts */
9513 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9514 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9515 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9516 {
9517 parse_error_context
9518 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9519 lang_printable_name (class_to_search, 0),
9520 (!current_this ? "" :
9521 "; an explicit one must be provided when creating this inner class"));
9522 PATCH_METHOD_RETURN_ERROR ();
9523 }
9524
22eed1e6
APB
9525 /* Non static methods are called with the current object extra
9526 argument. If patch a `new TYPE()', the argument is the value
9527 returned by the object allocator. If method is resolved as a
9528 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9529 args = nreverse (args);
bccaf73a 9530 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9531 {
9532 this_arg = primary ? primary : current_this;
9533
9534 /* If we're using an access method, things are different.
9535 There are two familly of cases:
9536
9537 1) We're not generating bytecodes:
9538
9539 - LIST is non static. It's invocation is transformed from
9540 x(a1,...,an) into this$<n>.x(a1,....an).
9541 - LIST is static. It's invocation is transformed from
9542 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9543
9544 2) We're generating bytecodes:
9545
9546 - LIST is non static. It's invocation is transformed from
9547 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9548 - LIST is static. It's invocation is transformed from
9549 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9550
9551 Of course, this$<n> can be abitrary complex, ranging from
9552 this$0 (the immediate outer context) to
9553 access$0(access$0(...(this$0))).
9554
9555 maybe_use_access_method returns a non zero value if the
dfb99c83
APB
9556 this_arg has to be moved into the (then generated) stub
9557 argument list. In the mean time, the selected function
9558 might have be replaced by a generated stub. */
c2952b01
APB
9559 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9560 args = tree_cons (NULL_TREE, this_arg, args);
9561 }
e04a16fb 9562 }
b67d701b 9563
e04a16fb
AG
9564 /* Merge point of all resolution schemes. If we have nothing, this
9565 is an error, already signaled */
b9f7e36c
APB
9566 if (!list)
9567 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9568
e04a16fb
AG
9569 /* Check accessibility, position the is_static flag, build and
9570 return the call */
9bbc7d9f 9571 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9572 {
c2e3db92 9573 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9574 parse_error_context
9575 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9576 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9577 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9578 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9579 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9580 free (fct_name);
b9f7e36c 9581 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9582 }
5e942c50 9583 check_deprecation (wfl, list);
22eed1e6 9584
c2952b01
APB
9585 /* If invoking a innerclass constructor, there are hidden parameters
9586 to pass */
9587 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9588 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9589 {
9590 /* And make sure we add the accessed local variables to be saved
9591 in field aliases. */
9592 args = build_alias_initializer_parameter_list
9593 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9594
9595 /* We have to reverse things. Find out why. FIXME */
9596 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9597 args = nreverse (args);
9598
9599 /* Secretely pass the current_this/primary as a second argument */
165f37bc
APB
9600 if (primary || current_this)
9601 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9602 else
9603 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9604 }
9605
152de068
APB
9606 /* This handles the situation where a constructor invocation needs
9607 to have an enclosing context passed as a second parameter (the
9608 constructor is one of an inner class. We extract it from the
9609 current function. */
9610 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9611 {
9612 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9613 tree extra_arg;
9614
9615 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9616 {
9617 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9618 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9619 }
9620 else
9621 {
9622 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9623 extra_arg =
9624 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9625 extra_arg = java_complete_tree (extra_arg);
9626 }
9627 args = tree_cons (NULL_TREE, extra_arg, args);
9628 }
9629
22eed1e6 9630 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9631 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9632 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9633
c3f2a476
APB
9634 /* In the context of an explicit constructor invocation, we can't
9635 invoke any method relying on `this'. Exceptions are: we're
9636 invoking a static function, primary exists and is not the current
9637 this, we're creating a new object. */
22eed1e6 9638 if (ctxp->explicit_constructor_p
c3f2a476
APB
9639 && !is_static_flag
9640 && (!primary || primary == current_this)
9641 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9642 {
781b0558 9643 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9644 PATCH_METHOD_RETURN_ERROR ();
9645 }
e04a16fb 9646 java_parser_context_restore_global ();
22eed1e6
APB
9647 if (is_static)
9648 *is_static = is_static_flag;
b9f7e36c
APB
9649 /* Sometimes, we want the decl of the selected method. Such as for
9650 EH checking */
9651 if (ret_decl)
9652 *ret_decl = list;
89e09b9a
PB
9653 patch = patch_invoke (patch, list, args);
9654 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9655 {
c2952b01
APB
9656 tree finit_parms, finit_call;
9657
9658 /* Prepare to pass hidden parameters to $finit$, if any. */
9659 finit_parms = build_alias_initializer_parameter_list
9660 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9661
c2952b01
APB
9662 finit_call =
9663 build_method_invocation (build_wfl_node (finit_identifier_node),
9664 finit_parms);
9665
9666 /* Generate the code used to initialize fields declared with an
9667 initialization statement and build a compound statement along
9668 with the super constructor invocation. */
89e09b9a
PB
9669 patch = build (COMPOUND_EXPR, void_type_node, patch,
9670 java_complete_tree (finit_call));
9671 CAN_COMPLETE_NORMALLY (patch) = 1;
9672 }
9673 return patch;
e04a16fb
AG
9674}
9675
9676/* Check that we're not trying to do a static reference to a method in
9677 non static method. Return 1 if it's the case, 0 otherwise. */
9678
9679static int
9680check_for_static_method_reference (wfl, node, method, where, primary)
9681 tree wfl, node, method, where, primary;
9682{
9683 if (METHOD_STATIC (current_function_decl)
9684 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9685 {
c2e3db92 9686 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9687 parse_error_context
9688 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9689 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9690 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9691 free (fct_name);
9692 return 1;
9693 }
9694 return 0;
9695}
9696
c2952b01
APB
9697/* Fix the invocation of *MDECL if necessary in the case of a
9698 invocation from an inner class. *THIS_ARG might be modified
9699 appropriately and an alternative access to *MDECL might be
9700 returned. */
9701
9702static int
9703maybe_use_access_method (is_super_init, mdecl, this_arg)
9704 int is_super_init;
9705 tree *mdecl, *this_arg;
9706{
9707 tree ctx;
9708 tree md = *mdecl, ta = *this_arg;
9709 int to_return = 0;
9710 int non_static_context = !METHOD_STATIC (md);
9711
9712 if (is_super_init
165f37bc
APB
9713 || DECL_CONTEXT (md) == current_class
9714 || !PURE_INNER_CLASS_TYPE_P (current_class)
9715 || DECL_FINIT_P (md))
c2952b01
APB
9716 return 0;
9717
9718 /* If we're calling a method found in an enclosing class, generate
9719 what it takes to retrieve the right this. Don't do that if we're
9720 invoking a static method. */
9721
9722 if (non_static_context)
9723 {
9724 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9725 if (ctx == DECL_CONTEXT (md))
9726 {
9727 ta = build_current_thisn (current_class);
9728 ta = build_wfl_node (ta);
9729 }
9730 else
9731 {
9732 tree type = ctx;
9733 while (type)
9734 {
9735 maybe_build_thisn_access_method (type);
9736 if (type == DECL_CONTEXT (md))
9737 {
9738 ta = build_access_to_thisn (ctx, type, 0);
9739 break;
9740 }
9741 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9742 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9743 }
9744 }
9745 ta = java_complete_tree (ta);
9746 }
9747
9748 /* We might have to use an access method to get to MD. We can
9749 break the method access rule as far as we're not generating
9750 bytecode */
9751 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9752 {
9753 md = build_outer_method_access_method (md);
9754 to_return = 1;
9755 }
9756
9757 *mdecl = md;
9758 *this_arg = ta;
9759
9760 /* Returnin a non zero value indicates we were doing a non static
9761 method invokation that is now a static invocation. It will have
9762 callee displace `this' to insert it in the regular argument
9763 list. */
9764 return (non_static_context && to_return);
9765}
9766
e04a16fb
AG
9767/* Patch an invoke expression METHOD and ARGS, based on its invocation
9768 mode. */
9769
9770static tree
89e09b9a 9771patch_invoke (patch, method, args)
e04a16fb 9772 tree patch, method, args;
e04a16fb
AG
9773{
9774 tree dtable, func;
0a2138e2 9775 tree original_call, t, ta;
e815887f 9776 tree cond = NULL_TREE;
e04a16fb 9777
5e942c50
APB
9778 /* Last step for args: convert build-in types. If we're dealing with
9779 a new TYPE() type call, the first argument to the constructor
e815887f 9780 isn't found in the incoming argument list, but delivered by
5e942c50
APB
9781 `new' */
9782 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9783 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9784 t = TREE_CHAIN (t);
ac825856
APB
9785 for (ta = args; t != end_params_node && ta;
9786 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
9787 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9788 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9789 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
9790
9791 /* Resolve unresolved returned type isses */
9792 t = TREE_TYPE (TREE_TYPE (method));
9793 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9794 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 9795
e8fc7396 9796 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9797 func = method;
9798 else
e04a16fb 9799 {
15fdcfe9 9800 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 9801 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
9802 {
9803 case INVOKE_VIRTUAL:
9804 dtable = invoke_build_dtable (0, args);
9805 func = build_invokevirtual (dtable, method);
9806 break;
b9f7e36c 9807
e815887f
TT
9808 case INVOKE_NONVIRTUAL:
9809 /* If the object for the method call is null, we throw an
9810 exception. We don't do this if the object is the current
9811 method's `this'. In other cases we just rely on an
9812 optimization pass to eliminate redundant checks. */
9813 if (TREE_VALUE (args) != current_this)
9814 {
9815 /* We use a SAVE_EXPR here to make sure we only evaluate
9816 the new `self' expression once. */
9817 tree save_arg = save_expr (TREE_VALUE (args));
9818 TREE_VALUE (args) = save_arg;
9819 cond = build (EQ_EXPR, boolean_type_node, save_arg,
9820 null_pointer_node);
9821 }
9822 /* Fall through. */
9823
15fdcfe9
PB
9824 case INVOKE_SUPER:
9825 case INVOKE_STATIC:
9826 func = build_known_method_ref (method, TREE_TYPE (method),
9827 DECL_CONTEXT (method),
9828 signature, args);
9829 break;
e04a16fb 9830
15fdcfe9
PB
9831 case INVOKE_INTERFACE:
9832 dtable = invoke_build_dtable (1, args);
173f556c 9833 func = build_invokeinterface (dtable, method);
15fdcfe9 9834 break;
5e942c50 9835
15fdcfe9 9836 default:
89e09b9a 9837 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
9838 }
9839
9840 /* Ensure self_type is initialized, (invokestatic). FIXME */
9841 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
9842 }
9843
e04a16fb
AG
9844 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
9845 TREE_OPERAND (patch, 0) = func;
9846 TREE_OPERAND (patch, 1) = args;
9847 original_call = patch;
9848
e815887f 9849 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
9850 returned value is the first argument to the constructor. We build
9851 a COMPOUND_EXPR and use saved expression so that the overall NEW
9852 expression value is a pointer to a newly created and initialized
9853 class. */
9854 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
9855 {
9856 tree class = DECL_CONTEXT (method);
9857 tree c1, saved_new, size, new;
e8fc7396 9858 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9859 {
9860 TREE_TYPE (patch) = build_pointer_type (class);
9861 return patch;
9862 }
e04a16fb
AG
9863 if (!TYPE_SIZE (class))
9864 safe_layout_class (class);
9865 size = size_in_bytes (class);
9866 new = build (CALL_EXPR, promote_type (class),
9867 build_address_of (alloc_object_node),
9868 tree_cons (NULL_TREE, build_class_ref (class),
9869 build_tree_list (NULL_TREE,
9870 size_in_bytes (class))),
9871 NULL_TREE);
9872 saved_new = save_expr (new);
9873 c1 = build_tree_list (NULL_TREE, saved_new);
9874 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
9875 TREE_OPERAND (original_call, 1) = c1;
9876 TREE_SET_CODE (original_call, CALL_EXPR);
9877 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
9878 }
e815887f
TT
9879
9880 /* If COND is set, then we are building a check to see if the object
9881 is NULL. */
9882 if (cond != NULL_TREE)
9883 {
9884 /* We have to make the `then' branch a compound expression to
9885 make the types turn out right. This seems bizarre. */
9886 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
9887 build (COMPOUND_EXPR, TREE_TYPE (patch),
9888 build (CALL_EXPR, void_type_node,
9889 build_address_of (soft_nullpointer_node),
9890 NULL_TREE, NULL_TREE),
9891 (FLOAT_TYPE_P (TREE_TYPE (patch))
9892 ? build_real (TREE_TYPE (patch), dconst0)
9893 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
9894 integer_zero_node))),
9895 patch);
9896 TREE_SIDE_EFFECTS (patch) = 1;
9897 }
9898
e04a16fb
AG
9899 return patch;
9900}
9901
9902static int
9903invocation_mode (method, super)
9904 tree method;
9905 int super;
9906{
9907 int access = get_access_flags_from_decl (method);
9908
22eed1e6
APB
9909 if (super)
9910 return INVOKE_SUPER;
9911
e815887f 9912 if (access & ACC_STATIC)
e04a16fb
AG
9913 return INVOKE_STATIC;
9914
e815887f
TT
9915 /* We have to look for a constructor before we handle nonvirtual
9916 calls; otherwise the constructor will look nonvirtual. */
9917 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 9918 return INVOKE_STATIC;
e815887f
TT
9919
9920 if (access & ACC_FINAL || access & ACC_PRIVATE)
9921 return INVOKE_NONVIRTUAL;
9922
9923 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
9924 return INVOKE_NONVIRTUAL;
9925
e04a16fb
AG
9926 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
9927 return INVOKE_INTERFACE;
22eed1e6 9928
e04a16fb
AG
9929 return INVOKE_VIRTUAL;
9930}
9931
b67d701b
PB
9932/* Retrieve a refined list of matching methods. It covers the step
9933 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
9934
9935static tree
9936lookup_method_invoke (lc, cl, class, name, arg_list)
9937 int lc;
9938 tree cl;
9939 tree class, name, arg_list;
9940{
de4c7b02 9941 tree atl = end_params_node; /* Arg Type List */
c877974e 9942 tree method, signature, list, node;
49f48c71 9943 const char *candidates; /* Used for error report */
b5b8a0e7 9944 char *dup;
e04a16fb 9945
5e942c50 9946 /* Fix the arguments */
e04a16fb
AG
9947 for (node = arg_list; node; node = TREE_CHAIN (node))
9948 {
e3884b71 9949 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 9950 /* Non primitive type may have to be resolved */
e3884b71 9951 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
9952 resolve_and_layout (current_arg, NULL_TREE);
9953 /* And promoted */
b67d701b 9954 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 9955 current_arg = promote_type (current_arg);
5e942c50 9956 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 9957 }
e04a16fb 9958
c2952b01
APB
9959 /* Presto. If we're dealing with an anonymous class and a
9960 constructor call, generate the right constructor now, since we
9961 know the arguments' types. */
9962
9963 if (lc && ANONYMOUS_CLASS_P (class))
9964 craft_constructor (TYPE_NAME (class), atl);
9965
5e942c50
APB
9966 /* Find all candidates and then refine the list, searching for the
9967 most specific method. */
9968 list = find_applicable_accessible_methods_list (lc, class, name, atl);
9969 list = find_most_specific_methods_list (list);
b67d701b
PB
9970 if (list && !TREE_CHAIN (list))
9971 return TREE_VALUE (list);
e04a16fb 9972
b67d701b
PB
9973 /* Issue an error. List candidates if any. Candidates are listed
9974 only if accessible (non accessible methods may end-up here for
9975 the sake of a better error report). */
9976 candidates = NULL;
9977 if (list)
e04a16fb 9978 {
e04a16fb 9979 tree current;
b67d701b 9980 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
9981 for (current = list; current; current = TREE_CHAIN (current))
9982 {
b67d701b
PB
9983 tree cm = TREE_VALUE (current);
9984 char string [4096];
9985 if (!cm || not_accessible_p (class, cm, 0))
9986 continue;
b67d701b 9987 sprintf
22eed1e6
APB
9988 (string, " `%s' in `%s'%s",
9989 get_printable_method_name (cm),
b67d701b
PB
9990 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
9991 (TREE_CHAIN (current) ? "\n" : ""));
9992 obstack_grow (&temporary_obstack, string, strlen (string));
9993 }
9994 obstack_1grow (&temporary_obstack, '\0');
9995 candidates = obstack_finish (&temporary_obstack);
9996 }
9997 /* Issue the error message */
c877974e
APB
9998 method = make_node (FUNCTION_TYPE);
9999 TYPE_ARG_TYPES (method) = atl;
b67d701b 10000 signature = build_java_argument_signature (method);
c63b98cd 10001 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10002 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10003 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10004 (lc ? dup : IDENTIFIER_POINTER (name)),
10005 IDENTIFIER_POINTER (signature), dup,
b67d701b 10006 (candidates ? candidates : ""));
b5b8a0e7 10007 free (dup);
b67d701b
PB
10008 return NULL_TREE;
10009}
10010
5e942c50
APB
10011/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10012 when we're looking for a constructor. */
b67d701b
PB
10013
10014static tree
5e942c50
APB
10015find_applicable_accessible_methods_list (lc, class, name, arglist)
10016 int lc;
b67d701b
PB
10017 tree class, name, arglist;
10018{
165f37bc 10019 static int object_done = 0;
b67d701b
PB
10020 tree list = NULL_TREE, all_list = NULL_TREE;
10021
c2952b01
APB
10022 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10023 {
10024 load_class (class, 1);
10025 safe_layout_class (class);
10026 }
10027
1982388a 10028 /* Search interfaces */
165f37bc 10029 if (CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10030 {
e0422ed0 10031 static struct hash_table t, *searched_interfaces = NULL;
de0b553f 10032 static int search_not_done = 0;
1982388a
APB
10033 int i, n;
10034 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10035
e0422ed0
APB
10036 /* Search in the hash table, otherwise create a new one if
10037 necessary and insert the new entry. */
10038
de0b553f 10039 if (searched_interfaces)
e0422ed0
APB
10040 {
10041 if (hash_lookup (searched_interfaces,
10042 (const hash_table_key) class, FALSE, NULL))
10043 return NULL;
de0b553f 10044 }
e0422ed0
APB
10045 else
10046 {
10047 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10048 java_hash_compare_tree_node);
10049 searched_interfaces = &t;
10050 }
10051
10052 hash_lookup (searched_interfaces,
10053 (const hash_table_key) class, TRUE, NULL);
de0b553f 10054
165f37bc
APB
10055 search_applicable_methods_list (lc, TYPE_METHODS (class),
10056 name, arglist, &list, &all_list);
1982388a 10057 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10058 for (i = 1; i < n; i++)
b67d701b 10059 {
de0b553f
APB
10060 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10061 tree rlist;
10062
de0b553f
APB
10063 search_not_done++;
10064 rlist = find_applicable_accessible_methods_list (lc, t, name,
10065 arglist);
165f37bc 10066 list = chainon (rlist, list);
de0b553f
APB
10067 search_not_done--;
10068 }
10069
10070 /* We're done. Reset the searched interfaces list and finally search
10071 java.lang.Object */
10072 if (!search_not_done)
10073 {
165f37bc
APB
10074 if (!object_done)
10075 search_applicable_methods_list (lc,
10076 TYPE_METHODS (object_type_node),
10077 name, arglist, &list, &all_list);
e0422ed0
APB
10078 hash_table_free (searched_interfaces);
10079 searched_interfaces = NULL;
e04a16fb 10080 }
e04a16fb 10081 }
1982388a
APB
10082 /* Search classes */
10083 else
c2952b01 10084 {
165f37bc
APB
10085 tree sc = class;
10086 int seen_inner_class = 0;
c2952b01
APB
10087 search_applicable_methods_list (lc, TYPE_METHODS (class),
10088 name, arglist, &list, &all_list);
10089
165f37bc
APB
10090 /* We must search all interfaces of this class */
10091 if (!lc)
10092 {
10093 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10094 int n = TREE_VEC_LENGTH (basetype_vec), i;
10095 object_done = 1;
10096 for (i = 1; i < n; i++)
10097 {
10098 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10099 if (t != object_type_node)
30a3caef
ZW
10100 {
10101 tree rlist
10102 = find_applicable_accessible_methods_list (lc, t,
10103 name, arglist);
10104 list = chainon (rlist, list);
10105 }
165f37bc
APB
10106 }
10107 object_done = 0;
10108 }
10109
c2952b01
APB
10110 /* Search enclosing context of inner classes before looking
10111 ancestors up. */
10112 while (!lc && INNER_CLASS_TYPE_P (class))
10113 {
165f37bc
APB
10114 tree rlist;
10115 seen_inner_class = 1;
c2952b01 10116 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10117 rlist = find_applicable_accessible_methods_list (lc, class,
10118 name, arglist);
10119 list = chainon (rlist, list);
c2952b01 10120 }
165f37bc
APB
10121
10122 if (!lc && seen_inner_class
10123 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10124 class = CLASSTYPE_SUPER (sc);
10125 else
10126 class = sc;
10127
10128 for (class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
10129 class; class = CLASSTYPE_SUPER (class))
10130 search_applicable_methods_list (lc, TYPE_METHODS (class),
10131 name, arglist, &list, &all_list);
c2952b01 10132 }
1982388a 10133
b67d701b
PB
10134 /* Either return the list obtained or all selected (but
10135 inaccessible) methods for better error report. */
10136 return (!list ? all_list : list);
10137}
e04a16fb 10138
1982388a
APB
10139/* Effectively search for the approriate method in method */
10140
10141static void
c2952b01 10142search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10143 int lc;
10144 tree method, name, arglist;
10145 tree *list, *all_list;
10146{
10147 for (; method; method = TREE_CHAIN (method))
10148 {
10149 /* When dealing with constructor, stop here, otherwise search
10150 other classes */
10151 if (lc && !DECL_CONSTRUCTOR_P (method))
10152 continue;
10153 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10154 || (GET_METHOD_NAME (method) != name)))
10155 continue;
10156
10157 if (argument_types_convertible (method, arglist))
10158 {
10159 /* Retain accessible methods only */
10160 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10161 method, 0))
10162 *list = tree_cons (NULL_TREE, method, *list);
10163 else
10164 /* Also retain all selected method here */
10165 *all_list = tree_cons (NULL_TREE, method, *list);
10166 }
10167 }
10168}
10169
b67d701b
PB
10170/* 15.11.2.2 Choose the Most Specific Method */
10171
10172static tree
10173find_most_specific_methods_list (list)
10174 tree list;
10175{
10176 int max = 0;
10177 tree current, new_list = NULL_TREE;
10178 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10179 {
b67d701b
PB
10180 tree method;
10181 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10182
10183 for (method = list; method; method = TREE_CHAIN (method))
10184 {
10185 /* Don't test a method against itself */
10186 if (method == current)
10187 continue;
10188
10189 /* Compare arguments and location where method where declared */
10190 if (argument_types_convertible (TREE_VALUE (method),
10191 TREE_VALUE (current))
10192 && valid_method_invocation_conversion_p
10193 (DECL_CONTEXT (TREE_VALUE (method)),
10194 DECL_CONTEXT (TREE_VALUE (current))))
10195 {
10196 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
10197 max = (v > max ? v : max);
10198 }
10199 }
e04a16fb
AG
10200 }
10201
b67d701b
PB
10202 /* Review the list and select the maximally specific methods */
10203 for (current = list; current; current = TREE_CHAIN (current))
10204 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10205 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10206
165f37bc
APB
10207 /* If we have several and they're all abstract, just pick the
10208 closest one. */
10209
10210 if (new_list && TREE_CHAIN (new_list))
10211 {
10212 tree c;
10213 for (c = new_list; c && METHOD_ABSTRACT (TREE_VALUE (c));
10214 c = TREE_CHAIN (c))
10215 ;
10216 if (!c)
10217 {
10218 new_list = nreverse (new_list);
10219 TREE_CHAIN (new_list) = NULL_TREE;
10220 }
10221 }
10222
b67d701b
PB
10223 /* If we can't find one, lower expectations and try to gather multiple
10224 maximally specific methods */
165f37bc 10225 while (!new_list && max)
b67d701b
PB
10226 {
10227 while (--max > 0)
10228 {
10229 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10230 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10231 }
b67d701b
PB
10232 }
10233
10234 return new_list;
e04a16fb
AG
10235}
10236
b67d701b
PB
10237/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10238 converted by method invocation conversion (5.3) to the type of the
10239 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10240 to change less often than M1. */
e04a16fb 10241
b67d701b
PB
10242static int
10243argument_types_convertible (m1, m2_or_arglist)
10244 tree m1, m2_or_arglist;
e04a16fb 10245{
b67d701b
PB
10246 static tree m2_arg_value = NULL_TREE;
10247 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10248
b67d701b 10249 register tree m1_arg, m2_arg;
e04a16fb 10250
c2952b01 10251 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10252
b67d701b
PB
10253 if (m2_arg_value == m2_or_arglist)
10254 m2_arg = m2_arg_cache;
10255 else
10256 {
10257 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10258 argument types */
10259 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10260 {
10261 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10262 if (!METHOD_STATIC (m2_or_arglist))
10263 m2_arg = TREE_CHAIN (m2_arg);
10264 }
10265 else
10266 m2_arg = m2_or_arglist;
e04a16fb 10267
b67d701b
PB
10268 m2_arg_value = m2_or_arglist;
10269 m2_arg_cache = m2_arg;
10270 }
e04a16fb 10271
de4c7b02 10272 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10273 {
c877974e 10274 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10275 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10276 TREE_VALUE (m2_arg)))
10277 break;
10278 m1_arg = TREE_CHAIN (m1_arg);
10279 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10280 }
de4c7b02 10281 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10282}
10283
10284/* Qualification routines */
10285
10286static void
10287qualify_ambiguous_name (id)
10288 tree id;
10289{
cd531a2e
KG
10290 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10291 saved_current_class;
d8fccff5 10292 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10293 int code;
e04a16fb
AG
10294
10295 /* We first qualify the first element, then derive qualification of
10296 others based on the first one. If the first element is qualified
10297 by a resolution (field or type), this resolution is stored in the
10298 QUAL_RESOLUTION of the qual element being examined. We need to
10299 save the current_class since the use of SUPER might change the
10300 its value. */
10301 saved_current_class = current_class;
10302 qual = EXPR_WFL_QUALIFICATION (id);
10303 do {
10304
10305 /* Simple qualified expression feature a qual_wfl that is a
10306 WFL. Expression derived from a primary feature more complicated
10307 things like a CALL_EXPR. Expression from primary need to be
10308 worked out to extract the part on which the qualification will
10309 take place. */
10310 qual_wfl = QUAL_WFL (qual);
10311 switch (TREE_CODE (qual_wfl))
10312 {
10313 case CALL_EXPR:
10314 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10315 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10316 {
10317 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10318 qual_wfl = QUAL_WFL (qual);
10319 }
10320 break;
d8fccff5 10321 case NEW_ARRAY_EXPR:
c2952b01 10322 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10323 qual = TREE_CHAIN (qual);
1a6d4fb7 10324 again = new_array_found = 1;
d8fccff5 10325 continue;
e04a16fb 10326 case CONVERT_EXPR:
f2760b27
APB
10327 break;
10328 case NEW_CLASS_EXPR:
e04a16fb
AG
10329 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10330 break;
c583dd46
APB
10331 case ARRAY_REF:
10332 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10333 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10334 break;
8576f094
APB
10335 case STRING_CST:
10336 qual = TREE_CHAIN (qual);
10337 qual_wfl = QUAL_WFL (qual);
10338 break;
165f37bc
APB
10339 case CLASS_LITERAL:
10340 qual = TREE_CHAIN (qual);
10341 qual_wfl = QUAL_WFL (qual);
10342 break;
0a2138e2
APB
10343 default:
10344 /* Fix for -Wall. Just break doing nothing */
10345 break;
e04a16fb 10346 }
8576f094 10347
e04a16fb
AG
10348 ptr_type = current_class;
10349 again = 0;
8576f094
APB
10350 code = TREE_CODE (qual_wfl);
10351
10352 /* Pos evaluation: non WFL leading expression nodes */
10353 if (code == CONVERT_EXPR
10354 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10355 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10356
cd7c5840
APB
10357 else if (code == INTEGER_CST)
10358 name = qual_wfl;
10359
ac22f9cb 10360 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10361 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10362 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10363
c2952b01
APB
10364 else if (code == TREE_LIST)
10365 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10366
37feda7d
APB
10367 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10368 || code == PLUS_EXPR)
8576f094
APB
10369 {
10370 qual = TREE_CHAIN (qual);
10371 qual_wfl = QUAL_WFL (qual);
10372 again = 1;
10373 }
10374 else
f441f671
APB
10375 {
10376 name = EXPR_WFL_NODE (qual_wfl);
10377 if (!name)
10378 {
10379 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10380 again = 1;
10381 }
10382 }
10383
e04a16fb
AG
10384 /* If we have a THIS (from a primary), we set the context accordingly */
10385 if (name == this_identifier_node)
10386 {
10387 qual = TREE_CHAIN (qual);
10388 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10389 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10390 again = 1;
10391 else
10392 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10393 this_found = 1;
10394 }
10395 /* If we have a SUPER, we set the context accordingly */
10396 if (name == super_identifier_node)
10397 {
10398 current_class = CLASSTYPE_SUPER (ptr_type);
10399 /* Check that there is such a thing as a super class. If not,
10400 return. The error will be caught later on, during the
10401 resolution */
10402 if (!current_class)
10403 {
10404 current_class = saved_current_class;
10405 return;
10406 }
10407 qual = TREE_CHAIN (qual);
10408 /* Do one more interation to set things up */
10409 super_found = again = 1;
10410 }
10411 } while (again);
10412
f2760b27
APB
10413 /* If name appears within the scope of a local variable declaration
10414 or parameter declaration, then it is an expression name. We don't
10415 carry this test out if we're in the context of the use of SUPER
10416 or THIS */
cd7c5840
APB
10417 if (!this_found && !super_found
10418 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10419 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10420 {
10421 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10422 QUAL_RESOLUTION (qual) = decl;
10423 }
10424
10425 /* If within the class/interface NAME was found to be used there
10426 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10427 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10428 address length, it is OK. */
10429 else if ((decl = lookup_field_wrapper (ptr_type, name))
10430 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10431 {
10432 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10433 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10434 }
10435
1a6d4fb7 10436 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10437 - NAME is a class/interface declared within the compilation
10438 unit containing NAME,
10439 - NAME is imported via a single-type-import declaration,
10440 - NAME is declared in an another compilation unit of the package
10441 of the compilation unit containing NAME,
10442 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10443 of the compilation unit containing NAME.
10444 - NAME is actually a STRING_CST. */
cd7c5840
APB
10445 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10446 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10447 {
10448 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10449 QUAL_RESOLUTION (qual) = decl;
10450 }
10451
f2760b27 10452 /* Method call, array references and cast are expression name */
9bbc7d9f 10453 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10454 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10455 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10456 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10457
10458 /* Check here that NAME isn't declared by more than one
10459 type-import-on-demand declaration of the compilation unit
10460 containing NAME. FIXME */
10461
10462 /* Otherwise, NAME is reclassified as a package name */
10463 else
10464 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10465
10466 /* Propagate the qualification accross other components of the
10467 qualified name */
10468 for (qual = TREE_CHAIN (qual); qual;
10469 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10470 {
10471 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10472 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10473 else
10474 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10475 }
10476
10477 /* Store the global qualification for the ambiguous part of ID back
10478 into ID fields */
10479 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10480 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10481 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10482 RESOLVE_TYPE_NAME_P (id) = 1;
10483 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10484 RESOLVE_PACKAGE_NAME_P (id) = 1;
10485
10486 /* Restore the current class */
10487 current_class = saved_current_class;
10488}
10489
10490static int
10491breakdown_qualified (left, right, source)
10492 tree *left, *right, source;
10493{
10494 char *p = IDENTIFIER_POINTER (source), *base;
10495 int l = IDENTIFIER_LENGTH (source);
10496
10497 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10498 base = p;
10499 p += (l-1);
10500 while (*p != '.' && p != base)
10501 p--;
10502
10503 /* We didn't find a '.'. Return an error */
10504 if (p == base)
10505 return 1;
10506
10507 *p = '\0';
10508 if (right)
10509 *right = get_identifier (p+1);
10510 *left = get_identifier (IDENTIFIER_POINTER (source));
10511 *p = '.';
10512
10513 return 0;
10514}
10515
e04a16fb 10516/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10517 local variable decls if present.
10518 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10519
10520static tree
10521java_complete_tree (node)
10522 tree node;
5b09b33e
PB
10523{
10524 node = java_complete_lhs (node);
10525 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7f10c2e2
APB
10526 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE
10527 && !flag_emit_xref)
5b09b33e
PB
10528 {
10529 tree value = DECL_INITIAL (node);
10530 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10531 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10532 value = fold_constant_for_init (value, node);
100f7cd8 10533 pop_obstacks ();
5b09b33e
PB
10534 DECL_INITIAL (node) = value;
10535 if (value != NULL_TREE)
c2952b01
APB
10536 {
10537 /* fold_constant_for_init sometimes widen the original type
10538 of the constant (i.e. byte to int.) It's not desirable,
10539 especially if NODE is a function argument. */
10540 if (TREE_CODE (value) == INTEGER_CST
10541 && TREE_TYPE (node) != TREE_TYPE (value))
10542 return convert (TREE_TYPE (node), value);
10543 else
10544 return value;
10545 }
5b09b33e
PB
10546 }
10547 return node;
10548}
10549
2aa11e97
APB
10550static tree
10551java_stabilize_reference (node)
10552 tree node;
10553{
10554 if (TREE_CODE (node) == COMPOUND_EXPR)
10555 {
10556 tree op0 = TREE_OPERAND (node, 0);
10557 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10558 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10559 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10560 return node;
10561 }
5cbdba64 10562 return stabilize_reference (node);
2aa11e97
APB
10563}
10564
5b09b33e
PB
10565/* Patch tree nodes in a function body. When a BLOCK is found, push
10566 local variable decls if present.
10567 Same as java_complete_tree, but does not resolve static finals to values. */
10568
10569static tree
10570java_complete_lhs (node)
10571 tree node;
e04a16fb 10572{
22eed1e6 10573 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10574 int flag;
e04a16fb
AG
10575
10576 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10577 worked out. */
e04a16fb
AG
10578 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10579 return node;
10580
10581 /* The switch block implements cases processing container nodes
10582 first. Contained nodes are always written back. Leaves come
10583 next and return a value. */
10584 switch (TREE_CODE (node))
10585 {
10586 case BLOCK:
10587
10588 /* 1- Block section.
10589 Set the local values on decl names so we can identify them
10590 faster when they're referenced. At that stage, identifiers
10591 are legal so we don't check for declaration errors. */
10592 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10593 {
10594 DECL_CONTEXT (cn) = current_function_decl;
10595 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10596 }
15fdcfe9
PB
10597 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10598 CAN_COMPLETE_NORMALLY (node) = 1;
10599 else
e04a16fb 10600 {
15fdcfe9
PB
10601 tree stmt = BLOCK_EXPR_BODY (node);
10602 tree *ptr;
10603 int error_seen = 0;
10604 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10605 {
c877974e
APB
10606 /* Re-order from (((A; B); C); ...; Z) to
10607 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10608 This makes it easier to scan the statements left-to-right
10609 without using recursion (which might overflow the stack
10610 if the block has many statements. */
10611 for (;;)
10612 {
10613 tree left = TREE_OPERAND (stmt, 0);
10614 if (TREE_CODE (left) != COMPOUND_EXPR)
10615 break;
10616 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10617 TREE_OPERAND (left, 1) = stmt;
10618 stmt = left;
10619 }
10620 BLOCK_EXPR_BODY (node) = stmt;
10621 }
10622
c877974e
APB
10623 /* Now do the actual complete, without deep recursion for
10624 long blocks. */
15fdcfe9 10625 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10626 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10627 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10628 {
10629 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10630 tree *next = &TREE_OPERAND (*ptr, 1);
10631 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10632 if (cur == empty_stmt_node)
10633 {
10634 /* Optimization; makes it easier to detect empty bodies.
10635 Most useful for <clinit> with all-constant initializer. */
10636 *ptr = *next;
10637 continue;
10638 }
15fdcfe9
PB
10639 if (TREE_CODE (cur) == ERROR_MARK)
10640 error_seen++;
10641 else if (! CAN_COMPLETE_NORMALLY (cur))
10642 {
10643 wfl_op2 = *next;
10644 for (;;)
10645 {
10646 if (TREE_CODE (wfl_op2) == BLOCK)
10647 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10648 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10649 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10650 else
10651 break;
10652 }
10653 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10654 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10655 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10656 }
10657 ptr = next;
10658 }
10659 *ptr = java_complete_tree (*ptr);
10660
10661 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10662 return error_mark_node;
15fdcfe9 10663 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10664 }
10665 /* Turn local bindings to null */
10666 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10667 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10668
10669 TREE_TYPE (node) = void_type_node;
10670 break;
10671
10672 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10673
b9f7e36c
APB
10674 case THROW_EXPR:
10675 wfl_op1 = TREE_OPERAND (node, 0);
10676 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10677 /* 14.19 A throw statement cannot complete normally. */
10678 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10679 return patch_throw_statement (node, wfl_op1);
10680
10681 case SYNCHRONIZED_EXPR:
10682 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10683 return patch_synchronized_statement (node, wfl_op1);
10684
b67d701b
PB
10685 case TRY_EXPR:
10686 return patch_try_statement (node);
10687
a7d8d81f
PB
10688 case TRY_FINALLY_EXPR:
10689 COMPLETE_CHECK_OP_0 (node);
10690 COMPLETE_CHECK_OP_1 (node);
10691 CAN_COMPLETE_NORMALLY (node)
10692 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10693 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10694 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10695 return node;
10696
5a005d9e
PB
10697 case CLEANUP_POINT_EXPR:
10698 COMPLETE_CHECK_OP_0 (node);
10699 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10700 CAN_COMPLETE_NORMALLY (node) =
10701 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10702 return node;
10703
10704 case WITH_CLEANUP_EXPR:
10705 COMPLETE_CHECK_OP_0 (node);
10706 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10707 CAN_COMPLETE_NORMALLY (node) =
10708 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10709 TREE_TYPE (node) = void_type_node;
10710 return node;
10711
e04a16fb
AG
10712 case LABELED_BLOCK_EXPR:
10713 PUSH_LABELED_BLOCK (node);
10714 if (LABELED_BLOCK_BODY (node))
10715 COMPLETE_CHECK_OP_1 (node);
10716 TREE_TYPE (node) = void_type_node;
10717 POP_LABELED_BLOCK ();
1fb89a4d
APB
10718
10719 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10720 {
10721 LABELED_BLOCK_BODY (node) = NULL_TREE;
10722 CAN_COMPLETE_NORMALLY (node) = 1;
10723 }
1fb89a4d 10724 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10725 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10726 return node;
10727
10728 case EXIT_BLOCK_EXPR:
10729 /* We don't complete operand 1, because it's the return value of
10730 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10731 return patch_bc_statement (node);
10732
15fdcfe9
PB
10733 case CASE_EXPR:
10734 cn = java_complete_tree (TREE_OPERAND (node, 0));
10735 if (cn == error_mark_node)
10736 return cn;
10737
8576f094
APB
10738 /* First, the case expression must be constant. Values of final
10739 fields are accepted. */
15fdcfe9 10740 cn = fold (cn);
8576f094
APB
10741 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10742 && JDECL_P (TREE_OPERAND (cn, 1))
10743 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10744 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
10745 {
10746 push_obstacks (&permanent_obstack, &permanent_obstack);
10747 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10748 TREE_OPERAND (cn, 1));
10749 pop_obstacks ();
10750 }
15fdcfe9 10751
ce6e9147 10752 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
10753 {
10754 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10755 parse_error_context (node, "Constant expression required");
10756 return error_mark_node;
10757 }
10758
10759 nn = ctxp->current_loop;
10760
10761 /* It must be assignable to the type of the switch expression. */
c877974e
APB
10762 if (!try_builtin_assignconv (NULL_TREE,
10763 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
10764 {
10765 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10766 parse_error_context
10767 (wfl_operator,
10768 "Incompatible type for case. Can't convert `%s' to `int'",
10769 lang_printable_name (TREE_TYPE (cn), 0));
10770 return error_mark_node;
10771 }
10772
10773 cn = fold (convert (int_type_node, cn));
10774
10775 /* Multiple instance of a case label bearing the same
10776 value is checked during code generation. The case
10777 expression is allright so far. */
10778 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 10779 TREE_TYPE (node) = void_type_node;
15fdcfe9 10780 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 10781 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10782 break;
10783
10784 case DEFAULT_EXPR:
10785 nn = ctxp->current_loop;
10786 /* Only one default label is allowed per switch statement */
10787 if (SWITCH_HAS_DEFAULT (nn))
10788 {
10789 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10790 parse_error_context (wfl_operator,
10791 "Duplicate case label: `default'");
10792 return error_mark_node;
10793 }
10794 else
10795 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 10796 TREE_TYPE (node) = void_type_node;
10100cc7 10797 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10798 CAN_COMPLETE_NORMALLY (node) = 1;
10799 break;
10800
b67d701b 10801 case SWITCH_EXPR:
e04a16fb
AG
10802 case LOOP_EXPR:
10803 PUSH_LOOP (node);
10804 /* Check whether the loop was enclosed in a labeled
10805 statement. If not, create one, insert the loop in it and
10806 return the node */
10807 nn = patch_loop_statement (node);
b67d701b 10808
e04a16fb 10809 /* Anyways, walk the body of the loop */
b67d701b
PB
10810 if (TREE_CODE (node) == LOOP_EXPR)
10811 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10812 /* Switch statement: walk the switch expression and the cases */
10813 else
10814 node = patch_switch_statement (node);
10815
e04a16fb 10816 if (TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
10817 nn = error_mark_node;
10818 else
15fdcfe9 10819 {
b635eb2f
PB
10820 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
10821 /* If we returned something different, that's because we
10822 inserted a label. Pop the label too. */
10823 if (nn != node)
10824 {
10825 if (CAN_COMPLETE_NORMALLY (node))
10826 CAN_COMPLETE_NORMALLY (nn) = 1;
10827 POP_LABELED_BLOCK ();
10828 }
15fdcfe9 10829 }
e04a16fb
AG
10830 POP_LOOP ();
10831 return nn;
10832
10833 case EXIT_EXPR:
10834 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10835 return patch_exit_expr (node);
10836
10837 case COND_EXPR:
10838 /* Condition */
10839 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10840 if (TREE_OPERAND (node, 0) == error_mark_node)
10841 return error_mark_node;
10842 /* then-else branches */
10843 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10844 if (TREE_OPERAND (node, 1) == error_mark_node)
10845 return error_mark_node;
10846 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
10847 if (TREE_OPERAND (node, 2) == error_mark_node)
10848 return error_mark_node;
10849 return patch_if_else_statement (node);
10850 break;
10851
22eed1e6
APB
10852 case CONDITIONAL_EXPR:
10853 /* Condition */
10854 wfl_op1 = TREE_OPERAND (node, 0);
10855 COMPLETE_CHECK_OP_0 (node);
10856 wfl_op2 = TREE_OPERAND (node, 1);
10857 COMPLETE_CHECK_OP_1 (node);
10858 wfl_op3 = TREE_OPERAND (node, 2);
10859 COMPLETE_CHECK_OP_2 (node);
10860 return patch_conditional_expr (node, wfl_op1, wfl_op2);
10861
e04a16fb
AG
10862 /* 3- Expression section */
10863 case COMPOUND_EXPR:
15fdcfe9 10864 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
10865 TREE_OPERAND (node, 0) = nn =
10866 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
10867 if (wfl_op2 == empty_stmt_node)
10868 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
10869 else
15fdcfe9 10870 {
dc0b3eff 10871 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 10872 {
dc0b3eff
PB
10873 /* An unreachable condition in a do-while statement
10874 is *not* (technically) an unreachable statement. */
10875 nn = wfl_op2;
10876 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
10877 nn = EXPR_WFL_NODE (nn);
10878 if (TREE_CODE (nn) != EXIT_EXPR)
10879 {
10880 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
10881 parse_error_context (wfl_operator, "Unreachable statement");
10882 }
bccaf73a 10883 }
dc0b3eff
PB
10884 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10885 if (TREE_OPERAND (node, 1) == error_mark_node)
10886 return error_mark_node;
10887 CAN_COMPLETE_NORMALLY (node)
10888 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 10889 }
e04a16fb
AG
10890 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
10891 break;
10892
10893 case RETURN_EXPR:
15fdcfe9 10894 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
10895 return patch_return (node);
10896
10897 case EXPR_WITH_FILE_LOCATION:
10898 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
10899 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 10900 {
5423609c 10901 tree wfl = node;
15fdcfe9 10902 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
10903 if (node == error_mark_node)
10904 return node;
5423609c
APB
10905 /* Keep line number information somewhere were it doesn't
10906 disrupt the completion process. */
2c56429a 10907 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
10908 {
10909 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
10910 TREE_OPERAND (node, 1) = wfl;
10911 }
15fdcfe9
PB
10912 CAN_COMPLETE_NORMALLY (node) = 1;
10913 }
e04a16fb
AG
10914 else
10915 {
5b09b33e
PB
10916 tree body;
10917 int save_lineno = lineno;
10918 lineno = EXPR_WFL_LINENO (node);
10919 body = java_complete_tree (EXPR_WFL_NODE (node));
10920 lineno = save_lineno;
15fdcfe9 10921 EXPR_WFL_NODE (node) = body;
dc0b3eff 10922 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 10923 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
10924 if (body == empty_stmt_node)
10925 {
10926 /* Optimization; makes it easier to detect empty bodies. */
10927 return body;
10928 }
dc0b3eff 10929 if (body == error_mark_node)
e04a16fb
AG
10930 {
10931 /* Its important for the evaluation of assignment that
10932 this mark on the TREE_TYPE is propagated. */
10933 TREE_TYPE (node) = error_mark_node;
10934 return error_mark_node;
10935 }
10936 else
10937 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 10938
e04a16fb
AG
10939 }
10940 break;
10941
b67d701b 10942 case NEW_ARRAY_EXPR:
e04a16fb
AG
10943 /* Patch all the dimensions */
10944 flag = 0;
10945 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
10946 {
10947 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
10948 tree dim = convert (int_type_node,
10949 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
10950 if (dim == error_mark_node)
10951 {
10952 flag = 1;
10953 continue;
10954 }
10955 else
10956 {
b9f7e36c 10957 TREE_VALUE (cn) = dim;
e04a16fb
AG
10958 /* Setup the location of the current dimension, for
10959 later error report. */
10960 TREE_PURPOSE (cn) =
10961 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
10962 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
10963 }
10964 }
10965 /* They complete the array creation expression, if no errors
10966 were found. */
15fdcfe9 10967 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
10968 return (flag ? error_mark_node
10969 : force_evaluation_order (patch_newarray (node)));
e04a16fb 10970
c2952b01
APB
10971 case NEW_ANONYMOUS_ARRAY_EXPR:
10972 /* Create the array type if necessary. */
10973 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
10974 {
10975 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
10976 if (!(type = resolve_type_during_patch (type)))
10977 return error_mark_node;
10978 type = build_array_from_name (type, NULL_TREE,
10979 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
10980 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
10981 }
10982 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
10983 ANONYMOUS_ARRAY_INITIALIZER (node));
10984 if (node == error_mark_node)
10985 return error_mark_node;
10986 CAN_COMPLETE_NORMALLY (node) = 1;
10987 return node;
10988
b67d701b 10989 case NEW_CLASS_EXPR:
e04a16fb 10990 case CALL_EXPR:
b67d701b 10991 /* Complete function's argument(s) first */
e04a16fb
AG
10992 if (complete_function_arguments (node))
10993 return error_mark_node;
10994 else
b9f7e36c 10995 {
22eed1e6
APB
10996 tree decl, wfl = TREE_OPERAND (node, 0);
10997 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
10998
c877974e 10999 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11000 NULL_TREE, 0, &decl);
c877974e
APB
11001 if (node == error_mark_node)
11002 return error_mark_node;
11003
11004 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11005 /* If we call this(...), register signature and positions */
11006 if (in_this)
11007 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11008 tree_cons (wfl, decl,
11009 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11010 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11011 return force_evaluation_order (node);
b9f7e36c 11012 }
e04a16fb
AG
11013
11014 case MODIFY_EXPR:
11015 /* Save potential wfls */
11016 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11017 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11018
cd9643f7
PB
11019 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11020 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11021 && DECL_INITIAL (nn) != NULL_TREE)
11022 {
100f7cd8
APB
11023 tree value;
11024
11025 push_obstacks (&permanent_obstack, &permanent_obstack);
11026 value = fold_constant_for_init (nn, nn);
11027 pop_obstacks ();
c2952b01 11028
cd9643f7
PB
11029 if (value != NULL_TREE)
11030 {
11031 tree type = TREE_TYPE (value);
c2952b01
APB
11032 if (JPRIMITIVE_TYPE_P (type) ||
11033 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11034 return empty_stmt_node;
11035 }
11036 DECL_INITIAL (nn) = NULL_TREE;
11037 }
e04a16fb 11038 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11039
e04a16fb
AG
11040 if (TREE_OPERAND (node, 0) == error_mark_node)
11041 return error_mark_node;
11042
5cbdba64
APB
11043 flag = COMPOUND_ASSIGN_P (wfl_op2);
11044 if (flag)
e04a16fb 11045 {
c2952b01
APB
11046 /* This might break when accessing outer field from inner
11047 class. TESTME, FIXME */
2aa11e97 11048 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11049
11050 /* Hand stablize the lhs on both places */
e04a16fb 11051 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11052 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11053 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11054
5cbdba64 11055 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11056 /* Now complete the RHS. We write it back later on. */
11057 nn = java_complete_tree (TREE_OPERAND (node, 1));
11058
642f15d1
APB
11059 if ((cn = patch_string (nn)))
11060 nn = cn;
11061
2aa11e97
APB
11062 /* The last part of the rewrite for E1 op= E2 is to have
11063 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11064 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11065 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11066
11067 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11068 }
11069
f8976021 11070 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11071 function to complete this RHS. Note that a NEW_ARRAY_INIT
11072 might have been already fully expanded if created as a result
11073 of processing an anonymous array initializer. We avoid doing
11074 the operation twice by testing whether the node already bears
11075 a type. */
11076 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11077 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11078 TREE_OPERAND (node, 1));
2aa11e97 11079 /* Otherwise we simply complete the RHS */
f8976021
APB
11080 else
11081 nn = java_complete_tree (TREE_OPERAND (node, 1));
11082
e04a16fb 11083 if (nn == error_mark_node)
c0d87ff6 11084 return error_mark_node;
2aa11e97
APB
11085
11086 /* Write back the RHS as we evaluated it. */
e04a16fb 11087 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11088
11089 /* In case we're handling = with a String as a RHS, we need to
11090 produce a String out of the RHS (it might still be a
11091 STRING_CST or a StringBuffer at this stage */
11092 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11093 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11094
11095 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11096 TREE_OPERAND (node, 1))))
11097 {
11098 /* We return error_mark_node if outer_field_access_fix
11099 detects we write into a final. */
11100 if (nn == error_mark_node)
11101 return error_mark_node;
11102 node = nn;
11103 }
11104 else
11105 {
11106 node = patch_assignment (node, wfl_op1, wfl_op2);
11107 /* Reorganize the tree if necessary. */
11108 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11109 || JSTRING_P (TREE_TYPE (node))))
11110 node = java_refold (node);
11111 }
11112
15fdcfe9
PB
11113 CAN_COMPLETE_NORMALLY (node) = 1;
11114 return node;
e04a16fb
AG
11115
11116 case MULT_EXPR:
11117 case PLUS_EXPR:
11118 case MINUS_EXPR:
11119 case LSHIFT_EXPR:
11120 case RSHIFT_EXPR:
11121 case URSHIFT_EXPR:
11122 case BIT_AND_EXPR:
11123 case BIT_XOR_EXPR:
11124 case BIT_IOR_EXPR:
11125 case TRUNC_MOD_EXPR:
c2952b01 11126 case TRUNC_DIV_EXPR:
e04a16fb
AG
11127 case RDIV_EXPR:
11128 case TRUTH_ANDIF_EXPR:
11129 case TRUTH_ORIF_EXPR:
11130 case EQ_EXPR:
11131 case NE_EXPR:
11132 case GT_EXPR:
11133 case GE_EXPR:
11134 case LT_EXPR:
11135 case LE_EXPR:
11136 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11137 knows how to handle those cases. */
11138 wfl_op1 = TREE_OPERAND (node, 0);
11139 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11140
15fdcfe9 11141 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11142 /* Don't complete string nodes if dealing with the PLUS operand. */
11143 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11144 {
11145 nn = java_complete_tree (wfl_op1);
11146 if (nn == error_mark_node)
11147 return error_mark_node;
48a840d9 11148
2aa11e97
APB
11149 TREE_OPERAND (node, 0) = nn;
11150 }
b67d701b 11151 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11152 {
11153 nn = java_complete_tree (wfl_op2);
11154 if (nn == error_mark_node)
11155 return error_mark_node;
48a840d9 11156
2aa11e97
APB
11157 TREE_OPERAND (node, 1) = nn;
11158 }
dc0b3eff 11159 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11160
5e942c50
APB
11161 case INSTANCEOF_EXPR:
11162 wfl_op1 = TREE_OPERAND (node, 0);
11163 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11164 if (flag_emit_xref)
11165 {
11166 TREE_TYPE (node) = boolean_type_node;
11167 return node;
11168 }
5e942c50
APB
11169 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11170
b67d701b 11171 case UNARY_PLUS_EXPR:
e04a16fb
AG
11172 case NEGATE_EXPR:
11173 case TRUTH_NOT_EXPR:
11174 case BIT_NOT_EXPR:
11175 case PREDECREMENT_EXPR:
11176 case PREINCREMENT_EXPR:
11177 case POSTDECREMENT_EXPR:
11178 case POSTINCREMENT_EXPR:
11179 case CONVERT_EXPR:
11180 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11181 how to handle those cases. */
11182 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11183 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11184 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11185 if (TREE_OPERAND (node, 0) == error_mark_node)
11186 return error_mark_node;
4a5f66c3
APB
11187 node = patch_unaryop (node, wfl_op1);
11188 CAN_COMPLETE_NORMALLY (node) = 1;
11189 break;
e04a16fb
AG
11190
11191 case ARRAY_REF:
11192 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11193 how to handle those cases. */
11194 wfl_op1 = TREE_OPERAND (node, 0);
11195 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11196 if (TREE_OPERAND (node, 0) == error_mark_node)
11197 return error_mark_node;
7f1d4866 11198 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11199 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11200 /* The same applies to wfl_op2 */
11201 wfl_op2 = TREE_OPERAND (node, 1);
11202 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11203 if (TREE_OPERAND (node, 1) == error_mark_node)
11204 return error_mark_node;
7f1d4866 11205 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11206 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11207 return patch_array_ref (node);
e04a16fb 11208
63a212ed
PB
11209 case RECORD_TYPE:
11210 return node;;
11211
11212 case COMPONENT_REF:
11213 /* The first step in the re-write of qualified name handling. FIXME.
11214 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11215 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11216 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11217 {
11218 tree name = TREE_OPERAND (node, 1);
11219 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11220 if (field == NULL_TREE)
11221 {
11222 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11223 return error_mark_node;
11224 }
11225 if (! FIELD_STATIC (field))
11226 {
11227 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11228 return error_mark_node;
11229 }
11230 return field;
11231 }
11232 else
11233 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11234 break;
9bbc7d9f 11235
b67d701b 11236 case THIS_EXPR:
e04a16fb
AG
11237 /* Can't use THIS in a static environment */
11238 if (!current_this)
11239 {
11240 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11241 parse_error_context (wfl_operator,
11242 "Keyword `this' used outside allowed context");
e04a16fb
AG
11243 TREE_TYPE (node) = error_mark_node;
11244 return error_mark_node;
11245 }
22eed1e6
APB
11246 if (ctxp->explicit_constructor_p)
11247 {
11248 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11249 parse_error_context
781b0558 11250 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11251 TREE_TYPE (node) = error_mark_node;
11252 return error_mark_node;
11253 }
e04a16fb 11254 return current_this;
c2952b01
APB
11255
11256 case CLASS_LITERAL:
11257 CAN_COMPLETE_NORMALLY (node) = 1;
11258 node = patch_incomplete_class_ref (node);
11259 if (node == error_mark_node)
11260 return error_mark_node;
11261 break;
11262
11263 case INSTANCE_INITIALIZERS_EXPR:
11264 in_instance_initializer++;
11265 node = java_complete_tree (TREE_OPERAND (node, 0));
11266 in_instance_initializer--;
11267 if (node != error_mark_node)
11268 TREE_TYPE (node) = void_type_node;
11269 else
11270 return error_mark_node;
11271 break;
e04a16fb 11272
e04a16fb 11273 default:
15fdcfe9 11274 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11275 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11276 and it's time to turn it into the appropriate String object */
11277 if ((nn = patch_string (node)))
11278 node = nn;
11279 else
11280 fatal ("No case for tree code `%s' - java_complete_tree\n",
11281 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11282 }
11283 return node;
11284}
11285
11286/* Complete function call's argument. Return a non zero value is an
11287 error was found. */
11288
11289static int
11290complete_function_arguments (node)
11291 tree node;
11292{
11293 int flag = 0;
11294 tree cn;
11295
f63991a8 11296 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11297 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11298 {
b67d701b 11299 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11300 parm = java_complete_tree (wfl);
c2952b01 11301
e04a16fb
AG
11302 if (parm == error_mark_node)
11303 {
11304 flag = 1;
11305 continue;
11306 }
b67d701b
PB
11307 /* If have a string literal that we haven't transformed yet or a
11308 crafted string buffer, as a result of use of the the String
11309 `+' operator. Build `parm.toString()' and expand it. */
11310 if ((temp = patch_string (parm)))
b9f7e36c 11311 parm = temp;
5e942c50
APB
11312 /* Inline PRIMTYPE.TYPE read access */
11313 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11314
5e942c50 11315 TREE_VALUE (cn) = parm;
e04a16fb 11316 }
f63991a8 11317 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11318 return flag;
11319}
11320
11321/* Sometimes (for loops and variable initialized during their
11322 declaration), we want to wrap a statement around a WFL and turn it
11323 debugable. */
11324
11325static tree
11326build_debugable_stmt (location, stmt)
11327 int location;
11328 tree stmt;
11329{
11330 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11331 {
11332 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11333 EXPR_WFL_LINECOL (stmt) = location;
11334 }
11335 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11336 return stmt;
11337}
11338
11339static tree
11340build_expr_block (body, decls)
11341 tree body, decls;
11342{
11343 tree node = make_node (BLOCK);
11344 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11345 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11346 if (body)
11347 TREE_TYPE (node) = TREE_TYPE (body);
11348 TREE_SIDE_EFFECTS (node) = 1;
11349 return node;
11350}
11351
b67d701b
PB
11352/* Create a new function block and link it approriately to current
11353 function block chain */
e04a16fb
AG
11354
11355static tree
11356enter_block ()
11357{
b67d701b
PB
11358 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11359}
11360
11361/* Link block B supercontext to the previous block. The current
11362 function DECL is used as supercontext when enter_a_block is called
11363 for the first time for a given function. The current function body
11364 (DECL_FUNCTION_BODY) is set to be block B. */
11365
11366static tree
11367enter_a_block (b)
11368 tree b;
11369{
e04a16fb
AG
11370 tree fndecl = current_function_decl;
11371
f099f336
APB
11372 if (!fndecl) {
11373 BLOCK_SUPERCONTEXT (b) = current_static_block;
11374 current_static_block = b;
11375 }
11376
11377 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11378 {
11379 BLOCK_SUPERCONTEXT (b) = fndecl;
11380 DECL_FUNCTION_BODY (fndecl) = b;
11381 }
11382 else
11383 {
11384 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11385 DECL_FUNCTION_BODY (fndecl) = b;
11386 }
11387 return b;
11388}
11389
11390/* Exit a block by changing the current function body
11391 (DECL_FUNCTION_BODY) to the current block super context, only if
11392 the block being exited isn't the method's top level one. */
11393
11394static tree
11395exit_block ()
11396{
f099f336
APB
11397 tree b;
11398 if (current_function_decl)
11399 {
11400 b = DECL_FUNCTION_BODY (current_function_decl);
11401 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11402 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11403 }
11404 else
11405 {
11406 b = current_static_block;
e04a16fb 11407
f099f336
APB
11408 if (BLOCK_SUPERCONTEXT (b))
11409 current_static_block = BLOCK_SUPERCONTEXT (b);
11410 }
e04a16fb
AG
11411 return b;
11412}
11413
11414/* Lookup for NAME in the nested function's blocks, all the way up to
11415 the current toplevel one. It complies with Java's local variable
11416 scoping rules. */
11417
11418static tree
11419lookup_name_in_blocks (name)
11420 tree name;
11421{
f099f336 11422 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11423
11424 while (b != current_function_decl)
11425 {
11426 tree current;
11427
11428 /* Paranoid sanity check. To be removed */
11429 if (TREE_CODE (b) != BLOCK)
11430 fatal ("non block expr function body - lookup_name_in_blocks");
11431
11432 for (current = BLOCK_EXPR_DECLS (b); current;
11433 current = TREE_CHAIN (current))
11434 if (DECL_NAME (current) == name)
11435 return current;
11436 b = BLOCK_SUPERCONTEXT (b);
11437 }
11438 return NULL_TREE;
11439}
11440
11441static void
11442maybe_absorb_scoping_blocks ()
11443{
f099f336 11444 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11445 {
11446 tree b = exit_block ();
11447 java_method_add_stmt (current_function_decl, b);
11448 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11449 }
11450}
11451
11452\f
11453/* This section of the source is reserved to build_* functions that
11454 are building incomplete tree nodes and the patch_* functions that
11455 are completing them. */
11456
c2952b01
APB
11457/* Wrap a non WFL node around a WFL. */
11458static tree
11459build_wfl_wrap (node)
11460 tree node;
11461{
11462 tree wfl, node_to_insert = node;
11463
11464 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11465 with the way we're processing SUPER. A THIS from a primary as a
11466 different form than a SUPER. Turn THIS into something symbolic */
11467 if (TREE_CODE (node) == THIS_EXPR)
11468 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11469 else
11470 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11471
11472 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (node);
11473 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11474 return wfl;
11475}
11476
11477
9bbc7d9f 11478/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11479 we're currently dealing with the class java.lang.Object. */
11480
11481static tree
e920ebc9
APB
11482build_super_invocation (mdecl)
11483 tree mdecl;
22eed1e6 11484{
e920ebc9 11485 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11486 return empty_stmt_node;
22eed1e6
APB
11487 else
11488 {
9ee9b555 11489 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11490 tree a = NULL_TREE, t;
11491 /* If we're dealing with an anonymous class, pass the arguments
11492 of the crafted constructor along. */
11493 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11494 {
11495 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11496 for (; t != end_params_node; t = TREE_CHAIN (t))
11497 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11498 }
11499 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11500 }
11501}
11502
11503/* Build a SUPER/THIS qualified method invocation. */
11504
11505static tree
11506build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11507 int use_this;
11508 tree name, args;
11509 int lloc, rloc;
22eed1e6
APB
11510{
11511 tree invok;
11512 tree wfl =
9ee9b555 11513 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11514 EXPR_WFL_LINECOL (wfl) = lloc;
11515 invok = build_method_invocation (name, args);
11516 return make_qualified_primary (wfl, invok, rloc);
11517}
11518
b67d701b 11519/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11520
11521static tree
11522build_method_invocation (name, args)
11523 tree name;
11524 tree args;
11525{
11526 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11527 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11528 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11529 return call;
11530}
11531
11532/* Build an incomplete new xxx(...) node. */
11533
11534static tree
11535build_new_invocation (name, args)
11536 tree name, args;
11537{
11538 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11539 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11540 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11541 return call;
11542}
11543
11544/* Build an incomplete assignment expression. */
11545
11546static tree
11547build_assignment (op, op_location, lhs, rhs)
11548 int op, op_location;
11549 tree lhs, rhs;
11550{
11551 tree assignment;
11552 /* Build the corresponding binop if we deal with a Compound
11553 Assignment operator. Mark the binop sub-tree as part of a
11554 Compound Assignment expression */
11555 if (op != ASSIGN_TK)
11556 {
11557 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11558 COMPOUND_ASSIGN_P (rhs) = 1;
11559 }
11560 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11561 TREE_SIDE_EFFECTS (assignment) = 1;
11562 EXPR_WFL_LINECOL (assignment) = op_location;
11563 return assignment;
11564}
11565
11566/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11567
15fdcfe9 11568char *
e04a16fb
AG
11569print_int_node (node)
11570 tree node;
11571{
11572 static char buffer [80];
11573 if (TREE_CONSTANT_OVERFLOW (node))
11574 sprintf (buffer, "<overflow>");
11575
11576 if (TREE_INT_CST_HIGH (node) == 0)
11577 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11578 TREE_INT_CST_LOW (node));
11579 else if (TREE_INT_CST_HIGH (node) == -1
11580 && TREE_INT_CST_LOW (node) != 0)
11581 {
11582 buffer [0] = '-';
11583 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11584 -TREE_INT_CST_LOW (node));
11585 }
11586 else
11587 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11588 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11589
11590 return buffer;
11591}
11592
7f1d4866
APB
11593/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11594 context. */
5e942c50
APB
11595
11596static int
11597check_final_assignment (lvalue, wfl)
11598 tree lvalue, wfl;
11599{
6632dcdd
APB
11600 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11601 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11602 lvalue = TREE_OPERAND (lvalue, 1);
11603
bc2874c9
TT
11604 /* When generating class files, references to the `length' field
11605 look a bit different. */
11606 if ((flag_emit_class_files
11607 && TREE_CODE (lvalue) == COMPONENT_REF
11608 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11609 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11610 || (TREE_CODE (lvalue) == FIELD_DECL
11611 && FIELD_FINAL (lvalue)
11612 && !DECL_CLINIT_P (current_function_decl)
11613 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11614 {
11615 parse_error_context
11616 (wfl, "Can't assign a value to the final variable `%s'",
11617 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11618 return 1;
11619 }
11620 return 0;
11621}
11622
11623/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11624 read. This is needed to avoid circularities in the implementation
11625 of these fields in libjava. */
11626
11627static tree
11628maybe_build_primttype_type_ref (rhs, wfl)
11629 tree rhs, wfl;
11630{
11631 tree to_return = NULL_TREE;
11632 tree rhs_type = TREE_TYPE (rhs);
11633 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11634 {
11635 tree n = TREE_OPERAND (rhs, 1);
11636 if (TREE_CODE (n) == VAR_DECL
11637 && DECL_NAME (n) == TYPE_identifier_node
11638 && rhs_type == class_ptr_type)
11639 {
49f48c71 11640 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11641 if (!strncmp (self_name, "java.lang.", 10))
11642 to_return = build_primtype_type_ref (self_name);
11643 }
11644 }
11645 return (to_return ? to_return : rhs );
11646}
11647
e04a16fb
AG
11648/* 15.25 Assignment operators. */
11649
11650static tree
11651patch_assignment (node, wfl_op1, wfl_op2)
11652 tree node;
11653 tree wfl_op1;
11654 tree wfl_op2;
11655{
0a2138e2 11656 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11657 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11658 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11659 int error_found = 0;
11660 int lvalue_from_array = 0;
11661
c2952b01 11662 /* Can't assign to a (blank) final. */
5e942c50
APB
11663 if (check_final_assignment (lvalue, wfl_op1))
11664 error_found = 1;
e04a16fb
AG
11665
11666 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11667
11668 /* Lhs can be a named variable */
34f4db93 11669 if (JDECL_P (lvalue))
e04a16fb 11670 {
e04a16fb
AG
11671 lhs_type = TREE_TYPE (lvalue);
11672 }
11673 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11674 comment on reason why */
11675 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11676 {
11677 lhs_type = TREE_TYPE (lvalue);
11678 lvalue_from_array = 1;
11679 }
11680 /* Or a field access */
11681 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11682 lhs_type = TREE_TYPE (lvalue);
11683 /* Or a function return slot */
11684 else if (TREE_CODE (lvalue) == RESULT_DECL)
11685 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11686 /* Otherwise, we might want to try to write into an optimized static
11687 final, this is an of a different nature, reported further on. */
11688 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11689 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11690 {
6632dcdd 11691 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11692 {
11693 /* What we should do instead is resetting the all the flags
11694 previously set, exchange lvalue for llvalue and continue. */
11695 error_found = 1;
11696 return error_mark_node;
11697 }
11698 else
11699 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11700 }
11701 else
e04a16fb
AG
11702 {
11703 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11704 error_found = 1;
11705 }
11706
11707 rhs_type = TREE_TYPE (rhs);
b67d701b 11708 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11709 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11710
b67d701b 11711 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11712 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11713 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11714
11715 /* 15.25.2 If we have a compound assignment, convert RHS into the
11716 type of the LHS */
11717 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11718 new_rhs = convert (lhs_type, rhs);
11719
11720 /* Explicit cast required. This is an error */
11721 if (!new_rhs)
11722 {
c2e3db92
KG
11723 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11724 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11725 tree wfl;
11726 char operation [32]; /* Max size known */
11727
11728 /* If the assignment is part of a declaration, we use the WFL of
11729 the declared variable to point out the error and call it a
11730 declaration problem. If the assignment is a genuine =
11731 operator, we call is a operator `=' problem, otherwise we
11732 call it an assignment problem. In both of these last cases,
11733 we use the WFL of the operator to indicate the error. */
11734
11735 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11736 {
11737 wfl = wfl_op1;
11738 strcpy (operation, "declaration");
11739 }
11740 else
11741 {
11742 wfl = wfl_operator;
11743 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11744 strcpy (operation, "assignment");
11745 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11746 strcpy (operation, "`return'");
11747 else
11748 strcpy (operation, "`='");
11749 }
11750
1ebadc60 11751 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
11752 parse_error_context
11753 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11754 operation, t1, t2);
1ebadc60 11755 else
781b0558 11756 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 11757 operation, t1, t2);
e04a16fb
AG
11758 free (t1); free (t2);
11759 error_found = 1;
11760 }
11761
c877974e
APB
11762 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11763 if (new_rhs)
11764 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 11765
e04a16fb
AG
11766 if (error_found)
11767 return error_mark_node;
11768
2622b947
APB
11769 /* 10.10: Array Store Exception runtime check */
11770 if (!flag_emit_class_files
e8fc7396 11771 && !flag_emit_xref
2622b947 11772 && lvalue_from_array
afc390b1 11773 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
11774 {
11775 tree check;
11776 tree base = lvalue;
11777
11778 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11779 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11780 base = TREE_OPERAND (lvalue, 0);
11781 else
11782 {
11783 if (flag_bounds_check)
11784 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11785 else
11786 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11787 }
11788
11789 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 11790 new_rhs = save_expr (new_rhs);
2622b947
APB
11791 check = build (CALL_EXPR, void_type_node,
11792 build_address_of (soft_checkarraystore_node),
11793 tree_cons (NULL_TREE, base,
11794 build_tree_list (NULL_TREE, new_rhs)),
11795 NULL_TREE);
11796 TREE_SIDE_EFFECTS (check) = 1;
11797
11798 /* We have to decide on an insertion point */
11799 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11800 {
11801 tree t;
11802 if (flag_bounds_check)
11803 {
11804 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11805 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11806 build (COMPOUND_EXPR, void_type_node, t, check);
11807 }
11808 else
11809 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
11810 check, TREE_OPERAND (lvalue, 1));
11811 }
11812 else
11813 {
11814 /* Make sure the bound check will happen before the store check */
11815 if (flag_bounds_check)
11816 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
11817 build (COMPOUND_EXPR, void_type_node,
11818 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
11819 else
11820 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
11821 }
11822 }
22eed1e6 11823
e04a16fb
AG
11824 TREE_OPERAND (node, 0) = lvalue;
11825 TREE_OPERAND (node, 1) = new_rhs;
11826 TREE_TYPE (node) = lhs_type;
11827 return node;
11828}
11829
b67d701b
PB
11830/* Check that type SOURCE can be cast into type DEST. If the cast
11831 can't occur at all, return 0 otherwise 1. This function is used to
11832 produce accurate error messages on the reasons why an assignment
11833 failed. */
e04a16fb 11834
b67d701b
PB
11835static tree
11836try_reference_assignconv (lhs_type, rhs)
11837 tree lhs_type, rhs;
e04a16fb 11838{
b67d701b
PB
11839 tree new_rhs = NULL_TREE;
11840 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 11841
b67d701b
PB
11842 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
11843 {
11844 /* `null' may be assigned to any reference type */
11845 if (rhs == null_pointer_node)
11846 new_rhs = null_pointer_node;
11847 /* Try the reference assignment conversion */
11848 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
11849 new_rhs = rhs;
11850 /* This is a magic assignment that we process differently */
11851 else if (rhs == soft_exceptioninfo_call_node)
11852 new_rhs = rhs;
11853 }
11854 return new_rhs;
11855}
11856
11857/* Check that RHS can be converted into LHS_TYPE by the assignment
11858 conversion (5.2), for the cases of RHS being a builtin type. Return
11859 NULL_TREE if the conversion fails or if because RHS isn't of a
11860 builtin type. Return a converted RHS if the conversion is possible. */
11861
11862static tree
11863try_builtin_assignconv (wfl_op1, lhs_type, rhs)
11864 tree wfl_op1, lhs_type, rhs;
11865{
11866 tree new_rhs = NULL_TREE;
11867 tree rhs_type = TREE_TYPE (rhs);
11868
5e942c50
APB
11869 /* Zero accepted everywhere */
11870 if (TREE_CODE (rhs) == INTEGER_CST
11871 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
11872 && JPRIMITIVE_TYPE_P (rhs_type))
11873 new_rhs = convert (lhs_type, rhs);
11874
b67d701b
PB
11875 /* 5.1.1 Try Identity Conversion,
11876 5.1.2 Try Widening Primitive Conversion */
5e942c50 11877 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
11878 new_rhs = convert (lhs_type, rhs);
11879
11880 /* Try a narrowing primitive conversion (5.1.3):
11881 - expression is a constant expression of type int AND
11882 - variable is byte, short or char AND
11883 - The value of the expression is representable in the type of the
11884 variable */
11885 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
11886 && (lhs_type == byte_type_node || lhs_type == char_type_node
11887 || lhs_type == short_type_node))
11888 {
11889 if (int_fits_type_p (rhs, lhs_type))
11890 new_rhs = convert (lhs_type, rhs);
11891 else if (wfl_op1) /* Might be called with a NULL */
11892 parse_warning_context
781b0558 11893 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 11894 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
11895 /* Reported a warning that will turn into an error further
11896 down, so we don't return */
11897 }
11898
11899 return new_rhs;
11900}
11901
11902/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
11903 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
11904 0 is the conversion test fails. This implements parts the method
11905 invocation convertion (5.3). */
11906
11907static int
11908valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
11909 tree lhs_type, rhs_type;
11910{
acd663ee 11911 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
11912 if (lhs_type == rhs_type)
11913 return 1;
11914
acd663ee
APB
11915 /* Reject non primitive types */
11916 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
11917 return 0;
11918
acd663ee
APB
11919 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
11920 than a char can't be converted into a char. Short can't too, but
11921 the < test below takes care of that */
b67d701b
PB
11922 if (lhs_type == char_type_node && rhs_type == byte_type_node)
11923 return 0;
11924
5e942c50
APB
11925 /* Accept all promoted type here. Note, we can't use <= in the test
11926 below, because we still need to bounce out assignments of short
11927 to char and the likes */
11928 if (lhs_type == int_type_node
11929 && (rhs_type == promoted_byte_type_node
11930 || rhs_type == promoted_short_type_node
11931 || rhs_type == promoted_char_type_node
11932 || rhs_type == promoted_boolean_type_node))
11933 return 1;
11934
acd663ee
APB
11935 /* From here, an integral is widened if its precision is smaller
11936 than the precision of the LHS or if the LHS is a floating point
11937 type, or the RHS is a float and the RHS a double. */
11938 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
11939 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
11940 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
11941 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
11942 return 1;
11943
11944 return 0;
e04a16fb
AG
11945}
11946
11947/* Check that something of SOURCE type can be assigned or cast to
11948 something of DEST type at runtime. Return 1 if the operation is
11949 valid, 0 otherwise. If CAST is set to 1, we're treating the case
11950 were SOURCE is cast into DEST, which borrows a lot of the
11951 assignment check. */
11952
11953static int
11954valid_ref_assignconv_cast_p (source, dest, cast)
11955 tree source;
11956 tree dest;
11957 int cast;
11958{
09ed0f70
APB
11959 /* SOURCE or DEST might be null if not from a declared entity. */
11960 if (!source || !dest)
11961 return 0;
5e942c50
APB
11962 if (JNULLP_TYPE_P (source))
11963 return 1;
e04a16fb
AG
11964 if (TREE_CODE (source) == POINTER_TYPE)
11965 source = TREE_TYPE (source);
11966 if (TREE_CODE (dest) == POINTER_TYPE)
11967 dest = TREE_TYPE (dest);
11968 /* Case where SOURCE is a class type */
11969 if (TYPE_CLASS_P (source))
11970 {
11971 if (TYPE_CLASS_P (dest))
c2952b01
APB
11972 return (source == dest
11973 || inherits_from_p (source, dest)
11974 || enclosing_context_p (dest, source /*source, dest*/)
11975 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
11976 if (TYPE_INTERFACE_P (dest))
11977 {
11978 /* If doing a cast and SOURCE is final, the operation is
11979 always correct a compile time (because even if SOURCE
11980 does not implement DEST, a subclass of SOURCE might). */
11981 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
11982 return 1;
11983 /* Otherwise, SOURCE must implement DEST */
11984 return interface_of_p (dest, source);
11985 }
11986 /* DEST is an array, cast permited if SOURCE is of Object type */
11987 return (cast && source == object_type_node ? 1 : 0);
11988 }
11989 if (TYPE_INTERFACE_P (source))
11990 {
11991 if (TYPE_CLASS_P (dest))
11992 {
11993 /* If not casting, DEST must be the Object type */
11994 if (!cast)
11995 return dest == object_type_node;
11996 /* We're doing a cast. The cast is always valid is class
11997 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 11998 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
11999 return 1;
12000 else
12001 return interface_of_p (source, dest);
12002 }
12003 if (TYPE_INTERFACE_P (dest))
12004 {
12005 /* If doing a cast, then if SOURCE and DEST contain method
12006 with the same signature but different return type, then
12007 this is a (compile time) error */
12008 if (cast)
12009 {
12010 tree method_source, method_dest;
12011 tree source_type;
0a2138e2 12012 tree source_sig;
e04a16fb
AG
12013 tree source_name;
12014 for (method_source = TYPE_METHODS (source); method_source;
12015 method_source = TREE_CHAIN (method_source))
12016 {
12017 source_sig =
12018 build_java_argument_signature (TREE_TYPE (method_source));
12019 source_type = TREE_TYPE (TREE_TYPE (method_source));
12020 source_name = DECL_NAME (method_source);
12021 for (method_dest = TYPE_METHODS (dest);
12022 method_dest; method_dest = TREE_CHAIN (method_dest))
12023 if (source_sig ==
12024 build_java_argument_signature (TREE_TYPE (method_dest))
12025 && source_name == DECL_NAME (method_dest)
12026 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12027 return 0;
12028 }
12029 return 1;
12030 }
12031 else
12032 return source == dest || interface_of_p (dest, source);
12033 }
12034 else /* Array */
93024893
APB
12035 return (cast ?
12036 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
12037 }
12038 if (TYPE_ARRAY_P (source))
12039 {
12040 if (TYPE_CLASS_P (dest))
12041 return dest == object_type_node;
09ed0f70
APB
12042 /* Can't cast an array to an interface unless the interface is
12043 java.lang.Cloneable */
e04a16fb 12044 if (TYPE_INTERFACE_P (dest))
09ed0f70 12045 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
12046 else /* Arrays */
12047 {
12048 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12049 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12050
b9f7e36c
APB
12051 /* In case of severe errors, they turn out null */
12052 if (!dest_element_type || !source_element_type)
12053 return 0;
e04a16fb
AG
12054 if (source_element_type == dest_element_type)
12055 return 1;
12056 return valid_ref_assignconv_cast_p (source_element_type,
12057 dest_element_type, cast);
12058 }
12059 return 0;
12060 }
12061 return 0;
12062}
12063
b67d701b
PB
12064static int
12065valid_cast_to_p (source, dest)
12066 tree source;
12067 tree dest;
12068{
12069 if (TREE_CODE (source) == POINTER_TYPE)
12070 source = TREE_TYPE (source);
12071 if (TREE_CODE (dest) == POINTER_TYPE)
12072 dest = TREE_TYPE (dest);
12073
12074 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12075 return valid_ref_assignconv_cast_p (source, dest, 1);
12076
12077 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12078 return 1;
12079
12080 return 0;
12081}
12082
12083/* Method invocation conversion test. Return 1 if type SOURCE can be
12084 converted to type DEST through the methond invocation conversion
12085 process (5.3) */
12086
15fdcfe9
PB
12087static tree
12088do_unary_numeric_promotion (arg)
12089 tree arg;
12090{
12091 tree type = TREE_TYPE (arg);
12092 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12093 : TREE_CODE (type) == CHAR_TYPE)
12094 arg = convert (int_type_node, arg);
12095 return arg;
12096}
12097
acd663ee
APB
12098/* Return a non zero value if SOURCE can be converted into DEST using
12099 the method invocation conversion rule (5.3). */
b67d701b
PB
12100static int
12101valid_method_invocation_conversion_p (dest, source)
12102 tree dest, source;
12103{
e3884b71 12104 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12105 && valid_builtin_assignconv_identity_widening_p (dest, source))
12106 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12107 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12108 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12109}
12110
e04a16fb
AG
12111/* Build an incomplete binop expression. */
12112
12113static tree
12114build_binop (op, op_location, op1, op2)
12115 enum tree_code op;
12116 int op_location;
12117 tree op1, op2;
12118{
5e942c50 12119 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12120 TREE_SIDE_EFFECTS (binop) = 1;
12121 /* Store the location of the operator, for better error report. The
12122 string of the operator will be rebuild based on the OP value. */
12123 EXPR_WFL_LINECOL (binop) = op_location;
12124 return binop;
12125}
12126
12127/* Build the string of the operator retained by NODE. If NODE is part
12128 of a compound expression, add an '=' at the end of the string. This
12129 function is called when an error needs to be reported on an
12130 operator. The string is returned as a pointer to a static character
12131 buffer. */
12132
12133static char *
12134operator_string (node)
12135 tree node;
12136{
12137#define BUILD_OPERATOR_STRING(S) \
12138 { \
12139 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12140 return buffer; \
12141 }
12142
12143 static char buffer [10];
12144 switch (TREE_CODE (node))
12145 {
12146 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12147 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12148 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12149 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12150 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12151 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12152 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12153 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12154 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12155 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12156 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12157 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12158 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12159 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12160 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12161 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12162 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12163 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12164 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12165 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12166 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12167 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12168 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12169 case PREINCREMENT_EXPR: /* Fall through */
12170 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12171 case PREDECREMENT_EXPR: /* Fall through */
12172 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12173 default:
12174 fatal ("unregistered operator %s - operator_string",
12175 tree_code_name [TREE_CODE (node)]);
12176 }
12177 return NULL;
12178#undef BUILD_OPERATOR_STRING
12179}
12180
5cbdba64
APB
12181/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12182
12183static int
12184java_decl_equiv (var_acc1, var_acc2)
12185 tree var_acc1, var_acc2;
12186{
12187 if (JDECL_P (var_acc1))
12188 return (var_acc1 == var_acc2);
12189
12190 return (TREE_CODE (var_acc1) == COMPONENT_REF
12191 && TREE_CODE (var_acc2) == COMPONENT_REF
12192 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12193 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12194 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12195}
12196
12197/* Return a non zero value if CODE is one of the operators that can be
12198 used in conjunction with the `=' operator in a compound assignment. */
12199
12200static int
12201binop_compound_p (code)
12202 enum tree_code code;
12203{
12204 int i;
12205 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12206 if (binop_lookup [i] == code)
12207 break;
12208
12209 return i < BINOP_COMPOUND_CANDIDATES;
12210}
12211
12212/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12213
12214static tree
12215java_refold (t)
12216 tree t;
12217{
12218 tree c, b, ns, decl;
12219
12220 if (TREE_CODE (t) != MODIFY_EXPR)
12221 return t;
12222
12223 c = TREE_OPERAND (t, 1);
12224 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12225 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12226 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12227 return t;
12228
12229 /* Now the left branch of the binary operator. */
12230 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12231 if (! (b && TREE_CODE (b) == NOP_EXPR
12232 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12233 return t;
12234
12235 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12236 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12237 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12238 return t;
12239
12240 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12241 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12242 /* It's got to be the an equivalent decl */
12243 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12244 {
12245 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12246 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12247 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12248 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12249 /* Change the right part of the BINOP_EXPR */
12250 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12251 }
12252
12253 return t;
12254}
12255
e04a16fb
AG
12256/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12257 errors but we modify NODE so that it contains the type computed
12258 according to the expression, when it's fixed. Otherwise, we write
12259 error_mark_node as the type. It allows us to further the analysis
12260 of remaining nodes and detects more errors in certain cases. */
12261
12262static tree
12263patch_binop (node, wfl_op1, wfl_op2)
12264 tree node;
12265 tree wfl_op1;
12266 tree wfl_op2;
12267{
12268 tree op1 = TREE_OPERAND (node, 0);
12269 tree op2 = TREE_OPERAND (node, 1);
12270 tree op1_type = TREE_TYPE (op1);
12271 tree op2_type = TREE_TYPE (op2);
48a840d9 12272 tree prom_type = NULL_TREE, cn;
e04a16fb 12273 int code = TREE_CODE (node);
b67d701b 12274
e04a16fb
AG
12275 /* If 1, tell the routine that we have to return error_mark_node
12276 after checking for the initialization of the RHS */
12277 int error_found = 0;
12278
e04a16fb
AG
12279 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12280
e04a16fb
AG
12281 switch (code)
12282 {
12283 /* 15.16 Multiplicative operators */
12284 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12285 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12286 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12287 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12288 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12289 {
12290 if (!JPRIMITIVE_TYPE_P (op1_type))
12291 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12292 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12293 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12294 TREE_TYPE (node) = error_mark_node;
12295 error_found = 1;
12296 break;
12297 }
12298 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12299 /* Change the division operator if necessary */
12300 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12301 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12302
aa4759c1
AH
12303 if (TREE_CODE (prom_type) == INTEGER_TYPE
12304 && flag_use_divide_subroutine
12305 && ! flag_emit_class_files
12306 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12307 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12308
0b4d333e
APB
12309 /* This one is more complicated. FLOATs are processed by a
12310 function call to soft_fmod. Duplicate the value of the
12311 COMPOUND_ASSIGN_P flag. */
e04a16fb 12312 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12313 {
12314 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12315 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12316 TREE_SIDE_EFFECTS (mod)
12317 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12318 return mod;
12319 }
e04a16fb
AG
12320 break;
12321
12322 /* 15.17 Additive Operators */
12323 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12324
12325 /* Operation is valid if either one argument is a string
12326 constant, a String object or a StringBuffer crafted for the
12327 purpose of the a previous usage of the String concatenation
12328 operator */
12329
12330 if (TREE_CODE (op1) == STRING_CST
12331 || TREE_CODE (op2) == STRING_CST
12332 || JSTRING_TYPE_P (op1_type)
12333 || JSTRING_TYPE_P (op2_type)
12334 || IS_CRAFTED_STRING_BUFFER_P (op1)
12335 || IS_CRAFTED_STRING_BUFFER_P (op2))
12336 return build_string_concatenation (op1, op2);
12337
e04a16fb
AG
12338 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12339 Numeric Types */
12340 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12341 {
12342 if (!JPRIMITIVE_TYPE_P (op1_type))
12343 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12344 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12345 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12346 TREE_TYPE (node) = error_mark_node;
12347 error_found = 1;
12348 break;
12349 }
12350 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12351 break;
12352
12353 /* 15.18 Shift Operators */
12354 case LSHIFT_EXPR:
12355 case RSHIFT_EXPR:
12356 case URSHIFT_EXPR:
12357 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12358 {
12359 if (!JINTEGRAL_TYPE_P (op1_type))
12360 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12361 else
1ebadc60
KG
12362 {
12363 if (JPRIMITIVE_TYPE_P (op2_type))
12364 parse_error_context (wfl_operator,
781b0558 12365 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12366 operator_string (node),
12367 lang_printable_name (op2_type, 0));
12368 else
781b0558
KG
12369 parse_error_context (wfl_operator,
12370 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12371 operator_string (node),
12372 lang_printable_name (op2_type, 0));
12373 }
e04a16fb
AG
12374 TREE_TYPE (node) = error_mark_node;
12375 error_found = 1;
12376 break;
12377 }
12378
12379 /* Unary numeric promotion (5.6.1) is performed on each operand
12380 separatly */
15fdcfe9
PB
12381 op1 = do_unary_numeric_promotion (op1);
12382 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12383
12384 /* The type of the shift expression is the type of the promoted
12385 type of the left-hand operand */
12386 prom_type = TREE_TYPE (op1);
12387
c2952b01
APB
12388 /* Shift int only up to 0x1f and long up to 0x3f */
12389 if (prom_type == int_type_node)
12390 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12391 build_int_2 (0x1f, 0)));
12392 else
12393 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12394 build_int_2 (0x3f, 0)));
e04a16fb
AG
12395
12396 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12397 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12398 {
0b4d333e 12399 tree to_return;
73333a87
AH
12400 tree utype = unsigned_type (prom_type);
12401 op1 = convert (utype, op1);
e04a16fb 12402 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12403 TREE_OPERAND (node, 0) = op1;
12404 TREE_OPERAND (node, 1) = op2;
12405 TREE_TYPE (node) = utype;
0b4d333e
APB
12406 to_return = convert (prom_type, node);
12407 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12408 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12409 TREE_SIDE_EFFECTS (to_return)
12410 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12411 return to_return;
e04a16fb
AG
12412 }
12413 break;
5e942c50
APB
12414
12415 /* 15.19.1 Type Comparison Operator instaceof */
12416 case INSTANCEOF_EXPR:
12417
12418 TREE_TYPE (node) = boolean_type_node;
12419
12420 if (!(op2_type = resolve_type_during_patch (op2)))
12421 return error_mark_node;
12422
12423 /* The first operand must be a reference type or the null type */
12424 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12425 error_found = 1; /* Error reported further below */
12426
12427 /* The second operand must be a reference type */
12428 if (!JREFERENCE_TYPE_P (op2_type))
12429 {
12430 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12431 parse_error_context
12432 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12433 lang_printable_name (op2_type, 0));
12434 error_found = 1;
12435 }
12436
12437 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12438 {
12439 /* If the first operand is null, the result is always false */
12440 if (op1 == null_pointer_node)
12441 return boolean_false_node;
15fdcfe9
PB
12442 else if (flag_emit_class_files)
12443 {
12444 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12445 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12446 return node;
12447 }
5e942c50
APB
12448 /* Otherwise we have to invoke instance of to figure it out */
12449 else
67db0ce7 12450 return build_instanceof (op1, op2_type);
5e942c50
APB
12451 }
12452 /* There is no way the expression operand can be an instance of
12453 the type operand. This is a compile time error. */
12454 else
12455 {
c2e3db92 12456 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12457 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12458 parse_error_context
12459 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12460 t1, lang_printable_name (op2_type, 0));
12461 free (t1);
12462 error_found = 1;
12463 }
e04a16fb 12464
5e942c50 12465 break;
e04a16fb
AG
12466
12467 /* 15.21 Bitwise and Logical Operators */
12468 case BIT_AND_EXPR:
12469 case BIT_XOR_EXPR:
12470 case BIT_IOR_EXPR:
12471 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12472 /* Binary numeric promotion is performed on both operand and the
12473 expression retain that type */
12474 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12475
12476 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12477 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12478 /* The type of the bitwise operator expression is BOOLEAN */
12479 prom_type = boolean_type_node;
12480 else
12481 {
12482 if (!JINTEGRAL_TYPE_P (op1_type))
12483 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12484 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12485 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12486 TREE_TYPE (node) = error_mark_node;
12487 error_found = 1;
12488 /* Insert a break here if adding thing before the switch's
12489 break for this case */
12490 }
12491 break;
12492
12493 /* 15.22 Conditional-And Operator */
12494 case TRUTH_ANDIF_EXPR:
12495 /* 15.23 Conditional-Or Operator */
12496 case TRUTH_ORIF_EXPR:
12497 /* Operands must be of BOOLEAN type */
12498 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12499 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12500 {
12501 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12502 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12503 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12504 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12505 TREE_TYPE (node) = boolean_type_node;
12506 error_found = 1;
12507 break;
12508 }
12509 /* The type of the conditional operators is BOOLEAN */
12510 prom_type = boolean_type_node;
12511 break;
12512
12513 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12514 case LT_EXPR:
12515 case GT_EXPR:
12516 case LE_EXPR:
12517 case GE_EXPR:
12518 /* The type of each of the operands must be a primitive numeric
12519 type */
12520 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12521 {
12522 if (!JNUMERIC_TYPE_P (op1_type))
12523 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12524 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12525 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12526 TREE_TYPE (node) = boolean_type_node;
12527 error_found = 1;
12528 break;
12529 }
12530 /* Binary numeric promotion is performed on the operands */
12531 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12532 /* The type of the relation expression is always BOOLEAN */
12533 prom_type = boolean_type_node;
12534 break;
12535
12536 /* 15.20 Equality Operator */
12537 case EQ_EXPR:
12538 case NE_EXPR:
48a840d9
APB
12539 /* It's time for us to patch the strings. */
12540 if ((cn = patch_string (op1)))
12541 {
12542 op1 = cn;
12543 op1_type = TREE_TYPE (op1);
12544 }
12545 if ((cn = patch_string (op2)))
12546 {
12547 op2 = cn;
12548 op2_type = TREE_TYPE (op2);
12549 }
12550
e04a16fb
AG
12551 /* 15.20.1 Numerical Equality Operators == and != */
12552 /* Binary numeric promotion is performed on the operands */
5e942c50 12553 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12554 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12555
12556 /* 15.20.2 Boolean Equality Operators == and != */
12557 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12558 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12559 ; /* Nothing to do here */
12560
12561 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12562 /* Types have to be either references or the null type. If
12563 they're references, it must be possible to convert either
12564 type to the other by casting conversion. */
b9f7e36c
APB
12565 else if (op1 == null_pointer_node || op2 == null_pointer_node
12566 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12567 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12568 || valid_ref_assignconv_cast_p (op2_type,
12569 op1_type, 1))))
e04a16fb
AG
12570 ; /* Nothing to do here */
12571
12572 /* Else we have an error figure what can't be converted into
12573 what and report the error */
12574 else
12575 {
12576 char *t1;
c2e3db92 12577 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12578 parse_error_context
781b0558
KG
12579 (wfl_operator,
12580 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12581 operator_string (node), t1,
0a2138e2 12582 lang_printable_name (op2_type, 0));
e04a16fb
AG
12583 free (t1);
12584 TREE_TYPE (node) = boolean_type_node;
12585 error_found = 1;
12586 break;
12587 }
12588 prom_type = boolean_type_node;
12589 break;
12590 }
12591
e04a16fb
AG
12592 if (error_found)
12593 return error_mark_node;
12594
12595 TREE_OPERAND (node, 0) = op1;
12596 TREE_OPERAND (node, 1) = op2;
12597 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12598 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12599
ce6e9147
APB
12600 if (flag_emit_xref)
12601 return node;
12602
d1472141
PB
12603 /* fold does not respect side-effect order as required for Java but not C.
12604 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12605 * bytecode.
12606 */
12607 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12608 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12609 node = fold (node);
12610 return node;
e04a16fb
AG
12611}
12612
b67d701b
PB
12613/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12614 zero value, the value of CSTE comes after the valude of STRING */
12615
12616static tree
12617do_merge_string_cste (cste, string, string_len, after)
12618 tree cste;
49f48c71 12619 const char *string;
b67d701b
PB
12620 int string_len, after;
12621{
12622 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 12623 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
12624 TREE_STRING_LENGTH (cste) = len;
12625 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12626 if (after)
12627 {
12628 strcpy (TREE_STRING_POINTER (cste), string);
12629 strcat (TREE_STRING_POINTER (cste), old);
12630 }
12631 else
12632 {
12633 strcpy (TREE_STRING_POINTER (cste), old);
12634 strcat (TREE_STRING_POINTER (cste), string);
12635 }
12636 return cste;
12637}
12638
12639/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12640 new STRING_CST on success, NULL_TREE on failure */
12641
12642static tree
12643merge_string_cste (op1, op2, after)
12644 tree op1, op2;
12645 int after;
12646{
12647 /* Handle two string constants right away */
12648 if (TREE_CODE (op2) == STRING_CST)
12649 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12650 TREE_STRING_LENGTH (op2), after);
12651
12652 /* Reasonable integer constant can be treated right away */
12653 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12654 {
49f48c71
KG
12655 static const char *boolean_true = "true";
12656 static const char *boolean_false = "false";
12657 static const char *null_pointer = "null";
b67d701b 12658 char ch[3];
49f48c71 12659 const char *string;
b67d701b
PB
12660
12661 if (op2 == boolean_true_node)
12662 string = boolean_true;
12663 else if (op2 == boolean_false_node)
12664 string = boolean_false;
12665 else if (op2 == null_pointer_node)
12666 string = null_pointer;
12667 else if (TREE_TYPE (op2) == char_type_node)
12668 {
12669 ch[0] = (char )TREE_INT_CST_LOW (op2);
12670 ch[1] = '\0';
12671 string = ch;
12672 }
12673 else
12674 string = print_int_node (op2);
12675
12676 return do_merge_string_cste (op1, string, strlen (string), after);
12677 }
12678 return NULL_TREE;
12679}
12680
12681/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12682 has to be a STRING_CST and the other part must be a STRING_CST or a
12683 INTEGRAL constant. Return a new STRING_CST if the operation
12684 succeed, NULL_TREE otherwise.
12685
12686 If the case we want to optimize for space, we might want to return
12687 NULL_TREE for each invocation of this routine. FIXME */
12688
12689static tree
12690string_constant_concatenation (op1, op2)
12691 tree op1, op2;
12692{
12693 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12694 {
0a2138e2 12695 tree string, rest;
b67d701b
PB
12696 int invert;
12697
12698 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12699 rest = (string == op1 ? op2 : op1);
12700 invert = (string == op1 ? 0 : 1 );
12701
12702 /* Walk REST, only if it looks reasonable */
12703 if (TREE_CODE (rest) != STRING_CST
12704 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12705 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12706 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12707 {
12708 rest = java_complete_tree (rest);
12709 if (rest == error_mark_node)
12710 return error_mark_node;
12711 rest = fold (rest);
12712 }
12713 return merge_string_cste (string, rest, invert);
12714 }
12715 return NULL_TREE;
12716}
12717
12718/* Implement the `+' operator. Does static optimization if possible,
12719 otherwise create (if necessary) and append elements to a
12720 StringBuffer. The StringBuffer will be carried around until it is
12721 used for a function call or an assignment. Then toString() will be
12722 called on it to turn it into a String object. */
12723
12724static tree
12725build_string_concatenation (op1, op2)
12726 tree op1, op2;
12727{
12728 tree result;
dc0b3eff 12729 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
12730
12731 if (flag_emit_xref)
12732 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
12733
12734 /* Try to do some static optimization */
12735 if ((result = string_constant_concatenation (op1, op2)))
12736 return result;
12737
c0d87ff6
PB
12738 /* Discard empty strings on either side of the expression */
12739 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
12740 {
12741 op1 = op2;
12742 op2 = NULL_TREE;
12743 }
c0d87ff6 12744 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 12745 op2 = NULL_TREE;
b67d701b 12746
acd663ee 12747 /* If operands are string constant, turn then into object references */
b67d701b
PB
12748 if (TREE_CODE (op1) == STRING_CST)
12749 op1 = patch_string_cst (op1);
acd663ee 12750 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
12751 op2 = patch_string_cst (op2);
12752
acd663ee
APB
12753 /* If either one of the constant is null and the other non null
12754 operand is a String object, return it. */
12755 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12756 return op1;
12757
b67d701b
PB
12758 /* If OP1 isn't already a StringBuffer, create and
12759 initialize a new one */
12760 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12761 {
12762 /* Two solutions here:
c52b5771
AG
12763 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12764 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12765 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
12766 op1 = BUILD_STRING_BUFFER (op1);
12767 else
12768 {
12769 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12770 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12771 }
12772 }
12773
acd663ee
APB
12774 if (op2)
12775 {
12776 /* OP1 is no longer the last node holding a crafted StringBuffer */
12777 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12778 /* Create a node for `{new...,xxx}.append (op2)' */
12779 if (op2)
12780 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12781 }
12782
b67d701b
PB
12783 /* Mark the last node holding a crafted StringBuffer */
12784 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
12785
12786 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
12787 return op1;
12788}
12789
12790/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12791 StringBuffer. If no string were found to be patched, return
12792 NULL. */
12793
12794static tree
12795patch_string (node)
12796 tree node;
12797{
1179ebc2
APB
12798 if (node == error_mark_node)
12799 return error_mark_node;
b67d701b
PB
12800 if (TREE_CODE (node) == STRING_CST)
12801 return patch_string_cst (node);
12802 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12803 {
c877974e 12804 int saved = ctxp->explicit_constructor_p;
b67d701b 12805 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
12806 tree ret;
12807 /* Temporary disable forbid the use of `this'. */
12808 ctxp->explicit_constructor_p = 0;
12809 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
12810 /* String concatenation arguments must be evaluated in order too. */
12811 ret = force_evaluation_order (ret);
c877974e
APB
12812 /* Restore it at its previous value */
12813 ctxp->explicit_constructor_p = saved;
12814 return ret;
b67d701b
PB
12815 }
12816 return NULL_TREE;
12817}
12818
12819/* Build the internal representation of a string constant. */
12820
12821static tree
12822patch_string_cst (node)
12823 tree node;
12824{
12825 int location;
15fdcfe9
PB
12826 if (! flag_emit_class_files)
12827 {
12828 push_obstacks (&permanent_obstack, &permanent_obstack);
12829 node = get_identifier (TREE_STRING_POINTER (node));
12830 location = alloc_name_constant (CONSTANT_String, node);
12831 node = build_ref_from_constant_pool (location);
8226320b 12832 pop_obstacks ();
15fdcfe9 12833 }
cd9643f7 12834 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
12835 TREE_CONSTANT (node) = 1;
12836 return node;
12837}
12838
12839/* Build an incomplete unary operator expression. */
e04a16fb
AG
12840
12841static tree
12842build_unaryop (op_token, op_location, op1)
12843 int op_token, op_location;
12844 tree op1;
12845{
12846 enum tree_code op;
12847 tree unaryop;
12848 switch (op_token)
12849 {
b67d701b 12850 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
12851 case MINUS_TK: op = NEGATE_EXPR; break;
12852 case NEG_TK: op = TRUTH_NOT_EXPR; break;
12853 case NOT_TK: op = BIT_NOT_EXPR; break;
12854 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
12855 op_token);
12856 }
12857
12858 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
12859 TREE_SIDE_EFFECTS (unaryop) = 1;
12860 /* Store the location of the operator, for better error report. The
12861 string of the operator will be rebuild based on the OP value. */
12862 EXPR_WFL_LINECOL (unaryop) = op_location;
12863 return unaryop;
12864}
12865
12866/* Special case for the ++/-- operators, since they require an extra
12867 argument to build, which is set to NULL and patched
12868 later. IS_POST_P is 1 if the operator, 0 otherwise. */
12869
12870static tree
12871build_incdec (op_token, op_location, op1, is_post_p)
12872 int op_token, op_location;
12873 tree op1;
12874 int is_post_p;
12875{
12876 static enum tree_code lookup [2][2] =
12877 {
12878 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
12879 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
12880 };
12881 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
12882 NULL_TREE, op1, NULL_TREE);
12883 TREE_SIDE_EFFECTS (node) = 1;
12884 /* Store the location of the operator, for better error report. The
12885 string of the operator will be rebuild based on the OP value. */
12886 EXPR_WFL_LINECOL (node) = op_location;
12887 return node;
12888}
12889
12890/* Build an incomplete cast operator, based on the use of the
12891 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
12892 set. java_complete_tree is trained to walk a CONVERT_EXPR even
12893 though its type is already set. */
12894
12895static tree
12896build_cast (location, type, exp)
12897 int location;
12898 tree type, exp;
12899{
12900 tree node = build1 (CONVERT_EXPR, type, exp);
12901 EXPR_WFL_LINECOL (node) = location;
12902 return node;
12903}
12904
c2952b01
APB
12905/* Build an incomplete class reference operator. */
12906static tree
12907build_incomplete_class_ref (location, class_name)
12908 int location;
12909 tree class_name;
12910{
12911 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
12912 EXPR_WFL_LINECOL (node) = location;
12913 return node;
12914}
12915
12916/* Complete an incomplete class reference operator. */
12917static tree
12918patch_incomplete_class_ref (node)
12919 tree node;
12920{
12921 tree type = TREE_OPERAND (node, 0);
12922 tree ref_type;
12923
12924 if (!(ref_type = resolve_type_during_patch (type)))
12925 return error_mark_node;
12926
165f37bc
APB
12927 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
12928 return build_class_ref (ref_type);
12929
12930 /* If we're emitting class files and we have to deal with non
12931 primitive types, we invoke (and consider generating) the
12932 synthetic static method `class$'. */
12933 if (!TYPE_DOT_CLASS (current_class))
12934 build_dot_class_method (current_class);
12935 ref_type =
12936 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
12937 return java_complete_tree (ref_type);
c2952b01
APB
12938}
12939
e04a16fb
AG
12940/* 15.14 Unary operators. We return error_mark_node in case of error,
12941 but preserve the type of NODE if the type is fixed. */
12942
12943static tree
12944patch_unaryop (node, wfl_op)
12945 tree node;
12946 tree wfl_op;
12947{
12948 tree op = TREE_OPERAND (node, 0);
12949 tree op_type = TREE_TYPE (op);
ab3a6dd6 12950 tree prom_type = NULL_TREE, value, decl;
c2952b01 12951 int outer_field_flag = 0;
e04a16fb
AG
12952 int code = TREE_CODE (node);
12953 int error_found = 0;
12954
12955 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12956
12957 switch (code)
12958 {
12959 /* 15.13.2 Postfix Increment Operator ++ */
12960 case POSTINCREMENT_EXPR:
12961 /* 15.13.3 Postfix Increment Operator -- */
12962 case POSTDECREMENT_EXPR:
12963 /* 15.14.1 Prefix Increment Operator ++ */
12964 case PREINCREMENT_EXPR:
12965 /* 15.14.2 Prefix Decrement Operator -- */
12966 case PREDECREMENT_EXPR:
5cbdba64 12967 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
12968 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
12969 /* We might be trying to change an outer field accessed using
12970 access method. */
12971 if (outer_field_flag)
12972 {
12973 /* Retrieve the decl of the field we're trying to access. We
12974 do that by first retrieving the function we would call to
12975 access the field. It has been already verified that this
12976 field isn't final */
12977 if (flag_emit_class_files)
12978 decl = TREE_OPERAND (op, 0);
12979 else
12980 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
12981 decl = DECL_FUNCTION_ACCESS_DECL (decl);
12982 }
b3edebcf 12983 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 12984 else if (!JDECL_P (decl)
b3edebcf
APB
12985 && TREE_CODE (decl) != COMPONENT_REF
12986 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
12987 && TREE_CODE (decl) != INDIRECT_REF
12988 && !(TREE_CODE (decl) == COMPOUND_EXPR
12989 && TREE_OPERAND (decl, 1)
12990 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 12991 {
5e942c50
APB
12992 tree lvalue;
12993 /* Before screaming, check that we're not in fact trying to
12994 increment a optimized static final access, in which case
12995 we issue an different error message. */
12996 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
12997 && resolve_expression_name (wfl_op, &lvalue)
12998 && check_final_assignment (lvalue, wfl_op)))
12999 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13000 operator_string (node));
e04a16fb
AG
13001 TREE_TYPE (node) = error_mark_node;
13002 error_found = 1;
13003 }
c2952b01
APB
13004
13005 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13006 error_found = 1;
13007
e04a16fb
AG
13008 /* From now on, we know that op if a variable and that it has a
13009 valid wfl. We use wfl_op to locate errors related to the
13010 ++/-- operand. */
13011 else if (!JNUMERIC_TYPE_P (op_type))
13012 {
13013 parse_error_context
13014 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13015 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13016 TREE_TYPE (node) = error_mark_node;
13017 error_found = 1;
13018 }
13019 else
13020 {
4a5f66c3 13021 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13022 both operands, if really necessary */
13023 if (JINTEGRAL_TYPE_P (op_type))
13024 {
13025 value = build_int_2 (1, 0);
13026 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13027 }
13028 else
13029 {
13030 value = build_int_2 (1, 0);
13031 TREE_TYPE (node) =
13032 binary_numeric_promotion (op_type,
13033 TREE_TYPE (value), &op, &value);
13034 }
c2952b01
APB
13035
13036 /* We remember we might be accessing an outer field */
13037 if (outer_field_flag)
13038 {
13039 /* We re-generate an access to the field */
13040 value = build (PLUS_EXPR, TREE_TYPE (op),
13041 build_outer_field_access (wfl_op, decl), value);
13042
13043 /* And we patch the original access$() into a write
13044 with plus_op as a rhs */
13045 return outer_field_access_fix (node, op, value);
13046 }
13047
5cbdba64 13048 /* And write back into the node. */
4a5f66c3 13049 TREE_OPERAND (node, 0) = op;
e04a16fb 13050 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13051 /* Convert the overall back into its original type, if
13052 necessary, and return */
13053 if (JINTEGRAL_TYPE_P (op_type))
13054 return fold (node);
13055 else
13056 return fold (convert (op_type, node));
e04a16fb
AG
13057 }
13058 break;
13059
13060 /* 15.14.3 Unary Plus Operator + */
b67d701b 13061 case UNARY_PLUS_EXPR:
e04a16fb
AG
13062 /* 15.14.4 Unary Minus Operator - */
13063 case NEGATE_EXPR:
13064 if (!JNUMERIC_TYPE_P (op_type))
13065 {
13066 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13067 TREE_TYPE (node) = error_mark_node;
13068 error_found = 1;
13069 }
13070 /* Unary numeric promotion is performed on operand */
13071 else
13072 {
15fdcfe9
PB
13073 op = do_unary_numeric_promotion (op);
13074 prom_type = TREE_TYPE (op);
b67d701b 13075 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13076 return fold (op);
e04a16fb
AG
13077 }
13078 break;
13079
13080 /* 15.14.5 Bitwise Complement Operator ~ */
13081 case BIT_NOT_EXPR:
13082 if (!JINTEGRAL_TYPE_P (op_type))
13083 {
13084 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13085 TREE_TYPE (node) = error_mark_node;
13086 error_found = 1;
13087 }
13088 else
13089 {
15fdcfe9
PB
13090 op = do_unary_numeric_promotion (op);
13091 prom_type = TREE_TYPE (op);
e04a16fb
AG
13092 }
13093 break;
13094
13095 /* 15.14.6 Logical Complement Operator ! */
13096 case TRUTH_NOT_EXPR:
13097 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13098 {
13099 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13100 /* But the type is known. We will report an error if further
13101 attempt of a assignment is made with this rhs */
e04a16fb
AG
13102 TREE_TYPE (node) = boolean_type_node;
13103 error_found = 1;
13104 }
13105 else
13106 prom_type = boolean_type_node;
13107 break;
13108
13109 /* 15.15 Cast Expression */
13110 case CONVERT_EXPR:
0a2138e2 13111 value = patch_cast (node, wfl_operator);
e04a16fb 13112 if (value == error_mark_node)
c877974e
APB
13113 {
13114 /* If this cast is part of an assignment, we tell the code
13115 that deals with it not to complain about a mismatch,
13116 because things have been cast, anyways */
13117 TREE_TYPE (node) = error_mark_node;
13118 error_found = 1;
13119 }
13120 else
dc0b3eff
PB
13121 {
13122 value = fold (value);
13123 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13124 return value;
13125 }
e04a16fb
AG
13126 break;
13127 }
13128
e04a16fb
AG
13129 if (error_found)
13130 return error_mark_node;
4a5f66c3
APB
13131
13132 /* There are cases where node has been replaced by something else
13133 and we don't end up returning here: UNARY_PLUS_EXPR,
13134 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13135 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13136 TREE_TYPE (node) = prom_type;
dc0b3eff 13137 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13138 return fold (node);
13139}
13140
13141/* Generic type resolution that sometimes takes place during node
13142 patching. Returned the resolved type or generate an error
13143 message. Return the resolved type or NULL_TREE. */
13144
13145static tree
13146resolve_type_during_patch (type)
13147 tree type;
13148{
13149 if (unresolved_type_p (type, NULL))
13150 {
13151 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
13152 if (!type_decl)
13153 {
13154 parse_error_context (type,
13155 "Class `%s' not found in type declaration",
13156 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13157 return NULL_TREE;
13158 }
13159 else
5e942c50
APB
13160 {
13161 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13162 return TREE_TYPE (type_decl);
13163 }
e04a16fb
AG
13164 }
13165 return type;
13166}
13167/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13168 found. Otherwise NODE or something meant to replace it is returned. */
13169
13170static tree
0a2138e2 13171patch_cast (node, wfl_operator)
e04a16fb 13172 tree node;
e04a16fb
AG
13173 tree wfl_operator;
13174{
13175 tree op = TREE_OPERAND (node, 0);
13176 tree op_type = TREE_TYPE (op);
13177 tree cast_type = TREE_TYPE (node);
13178 char *t1;
13179
13180 /* First resolve OP_TYPE if unresolved */
13181 if (!(cast_type = resolve_type_during_patch (cast_type)))
13182 return error_mark_node;
13183
13184 /* Check on cast that are proven correct at compile time */
13185 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13186 {
e04a16fb
AG
13187 /* Same type */
13188 if (cast_type == op_type)
13189 return node;
13190
0b4d333e
APB
13191 /* float and double type are converted to the original type main
13192 variant and then to the target type. */
13193 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13194 op = convert (integer_type_node, op);
13195
e04a16fb
AG
13196 /* Try widening/narowwing convertion. Potentially, things need
13197 to be worked out in gcc so we implement the extreme cases
13198 correctly. fold_convert() needs to be fixed. */
13199 return convert (cast_type, op);
13200 }
13201
0b4d333e
APB
13202 /* It's also valid to cast a boolean into a boolean */
13203 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13204 return node;
13205
5e942c50
APB
13206 /* null can be casted to references */
13207 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13208 return build_null_of_type (cast_type);
13209
e04a16fb
AG
13210 /* The remaining legal casts involve conversion between reference
13211 types. Check for their compile time correctness. */
13212 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13213 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13214 {
13215 TREE_TYPE (node) = promote_type (cast_type);
13216 /* Now, the case can be determined correct at compile time if
13217 OP_TYPE can be converted into CAST_TYPE by assignment
13218 conversion (5.2) */
13219
13220 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13221 {
13222 TREE_SET_CODE (node, NOP_EXPR);
13223 return node;
13224 }
13225
13226 if (flag_emit_class_files)
13227 {
13228 TREE_SET_CODE (node, CONVERT_EXPR);
13229 return node;
13230 }
e04a16fb
AG
13231
13232 /* The cast requires a run-time check */
13233 return build (CALL_EXPR, promote_type (cast_type),
13234 build_address_of (soft_checkcast_node),
13235 tree_cons (NULL_TREE, build_class_ref (cast_type),
13236 build_tree_list (NULL_TREE, op)),
13237 NULL_TREE);
13238 }
13239
13240 /* Any other casts are proven incorrect at compile time */
c2e3db92 13241 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13242 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13243 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13244 free (t1);
13245 return error_mark_node;
13246}
13247
5e942c50
APB
13248/* Build a null constant and give it the type TYPE. */
13249
13250static tree
13251build_null_of_type (type)
13252 tree type;
13253{
13254 tree node = build_int_2 (0, 0);
13255 TREE_TYPE (node) = promote_type (type);
13256 return node;
13257}
13258
e04a16fb
AG
13259/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13260 a list of indices. */
13261static tree
13262build_array_ref (location, array, index)
13263 int location;
13264 tree array, index;
13265{
13266 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13267 EXPR_WFL_LINECOL (node) = location;
13268 return node;
13269}
13270
13271/* 15.12 Array Access Expression */
13272
13273static tree
c877974e
APB
13274patch_array_ref (node)
13275 tree node;
e04a16fb
AG
13276{
13277 tree array = TREE_OPERAND (node, 0);
13278 tree array_type = TREE_TYPE (array);
13279 tree index = TREE_OPERAND (node, 1);
13280 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13281 int error_found = 0;
13282
13283 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13284
e04a16fb
AG
13285 if (TREE_CODE (array_type) == POINTER_TYPE)
13286 array_type = TREE_TYPE (array_type);
13287
13288 /* The array reference must be an array */
13289 if (!TYPE_ARRAY_P (array_type))
13290 {
13291 parse_error_context
781b0558
KG
13292 (wfl_operator,
13293 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13294 lang_printable_name (array_type, 0));
e04a16fb
AG
13295 TREE_TYPE (node) = error_mark_node;
13296 error_found = 1;
13297 }
13298
c2952b01 13299 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13300 type must be int */
15fdcfe9
PB
13301 index = do_unary_numeric_promotion (index);
13302 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13303 {
1ebadc60 13304 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13305 parse_error_context (wfl_operator,
13306 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13307 lang_printable_name (index_type, 0));
13308 else
781b0558
KG
13309 parse_error_context (wfl_operator,
13310 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13311 lang_printable_name (index_type, 0));
e04a16fb
AG
13312 TREE_TYPE (node) = error_mark_node;
13313 error_found = 1;
13314 }
13315
e04a16fb
AG
13316 if (error_found)
13317 return error_mark_node;
e04a16fb 13318
5e942c50 13319 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13320
7f1d4866 13321 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13322 {
15fdcfe9
PB
13323 TREE_OPERAND (node, 0) = array;
13324 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13325 }
13326 else
939d7216
PB
13327 {
13328 /* The save_expr is for correct evaluation order. It would be cleaner
13329 to use force_evaluation_order (see comment there), but that is
13330 difficult when we also have to deal with bounds checking. */
13331 if (TREE_SIDE_EFFECTS (index))
13332 array = save_expr (array);
13333 node = build_java_arrayaccess (array, array_type, index);
13334 if (TREE_SIDE_EFFECTS (index))
13335 node = build (COMPOUND_EXPR, array_type, array, node);
13336 }
e04a16fb
AG
13337 TREE_TYPE (node) = array_type;
13338 return node;
13339}
13340
13341/* 15.9 Array Creation Expressions */
13342
13343static tree
13344build_newarray_node (type, dims, extra_dims)
13345 tree type;
13346 tree dims;
13347 int extra_dims;
13348{
13349 tree node =
b67d701b 13350 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13351 build_int_2 (extra_dims, 0));
e04a16fb
AG
13352 return node;
13353}
13354
13355static tree
13356patch_newarray (node)
13357 tree node;
13358{
13359 tree type = TREE_OPERAND (node, 0);
13360 tree dims = TREE_OPERAND (node, 1);
13361 tree cdim, array_type;
13362 int error_found = 0;
13363 int ndims = 0;
13364 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13365
13366 /* Dimension types are verified. It's better for the types to be
13367 verified in order. */
13368 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13369 {
13370 int dim_error = 0;
13371 tree dim = TREE_VALUE (cdim);
13372
13373 /* Dim might have been saved during its evaluation */
13374 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13375
13376 /* The type of each specified dimension must be an integral type. */
13377 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13378 dim_error = 1;
13379
13380 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13381 promoted type must be int. */
13382 else
13383 {
15fdcfe9 13384 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13385 if (TREE_TYPE (dim) != int_type_node)
13386 dim_error = 1;
13387 }
13388
13389 /* Report errors on types here */
13390 if (dim_error)
13391 {
13392 parse_error_context
13393 (TREE_PURPOSE (cdim),
781b0558 13394 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13395 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13396 "Explicit cast needed to" : "Can't"),
0a2138e2 13397 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13398 error_found = 1;
13399 }
13400
e04a16fb
AG
13401 TREE_PURPOSE (cdim) = NULL_TREE;
13402 }
13403
13404 /* Resolve array base type if unresolved */
13405 if (!(type = resolve_type_during_patch (type)))
13406 error_found = 1;
13407
13408 if (error_found)
13409 {
13410 /* We don't want further evaluation of this bogus array creation
13411 operation */
13412 TREE_TYPE (node) = error_mark_node;
13413 return error_mark_node;
13414 }
13415
15fdcfe9
PB
13416 /* Set array_type to the actual (promoted) array type of the result. */
13417 if (TREE_CODE (type) == RECORD_TYPE)
13418 type = build_pointer_type (type);
13419 while (--xdims >= 0)
13420 {
13421 type = promote_type (build_java_array_type (type, -1));
13422 }
13423 dims = nreverse (dims);
13424 array_type = type;
13425 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13426 {
13427 type = array_type;
05bccae2
RK
13428 array_type
13429 = build_java_array_type (type,
13430 TREE_CODE (cdim) == INTEGER_CST
13431 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13432 : -1);
15fdcfe9
PB
13433 array_type = promote_type (array_type);
13434 }
13435 dims = nreverse (dims);
13436
e04a16fb
AG
13437 /* The node is transformed into a function call. Things are done
13438 differently according to the number of dimensions. If the number
13439 of dimension is equal to 1, then the nature of the base type
13440 (primitive or not) matters. */
15fdcfe9 13441 if (ndims == 1)
fdec99c6 13442 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13443
e04a16fb
AG
13444 /* Can't reuse what's already written in expr.c because it uses the
13445 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13446 return build (CALL_EXPR, array_type,
e04a16fb 13447 build_address_of (soft_multianewarray_node),
15fdcfe9 13448 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13449 tree_cons (NULL_TREE,
15fdcfe9 13450 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13451 NULL_TREE);
13452}
13453
f8976021
APB
13454/* 10.6 Array initializer. */
13455
13456/* Build a wfl for array element that don't have one, so we can
13457 pin-point errors. */
13458
13459static tree
13460maybe_build_array_element_wfl (node)
13461 tree node;
13462{
13463 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13464 return build_expr_wfl (NULL_TREE, ctxp->filename,
13465 ctxp->elc.line, ctxp->elc.prev_col);
13466 else
13467 return NULL_TREE;
13468}
13469
13470/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13471 identification of initialized arrays easier to detect during walk
13472 and expansion. */
13473
13474static tree
13475build_new_array_init (location, values)
13476 int location;
13477 tree values;
13478{
13479 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13480 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13481 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13482 return to_return;
13483}
13484
13485/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13486 occurred. Otherwise return NODE after having set its type
13487 appropriately. */
13488
13489static tree
13490patch_new_array_init (type, node)
13491 tree type, node;
f8976021
APB
13492{
13493 int error_seen = 0;
fdec99c6 13494 tree current, element_type;
f8976021 13495 HOST_WIDE_INT length;
fdec99c6
PB
13496 int all_constant = 1;
13497 tree init = TREE_OPERAND (node, 0);
f8976021 13498
fdec99c6
PB
13499 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13500 {
13501 parse_error_context (node,
13502 "Invalid array initializer for non-array type `%s'",
13503 lang_printable_name (type, 1));
13504 return error_mark_node;
13505 }
13506 type = TREE_TYPE (type);
13507 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13508
fdec99c6
PB
13509 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13510
13511 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13512 current; length++, current = TREE_CHAIN (current))
f8976021 13513 {
fdec99c6
PB
13514 tree elt = TREE_VALUE (current);
13515 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13516 {
fdec99c6 13517 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13518 elt = TREE_VALUE (current);
13519 /* When compiling to native code, STRING_CST is converted to
13520 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13521 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13522 all_constant = 0;
f8976021 13523 }
fdec99c6
PB
13524 else
13525 {
13526 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13527 TREE_PURPOSE (current) = NULL_TREE;
13528 all_constant = 0;
13529 }
13530 if (elt && TREE_VALUE (elt) == error_mark_node)
13531 error_seen = 1;
f8976021
APB
13532 }
13533
13534 if (error_seen)
13535 return error_mark_node;
13536
13537 /* Create a new type. We can't reuse the one we have here by
13538 patching its dimension because it originally is of dimension -1
13539 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13540 type = build_java_array_type (element_type, length);
13541 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13542 TREE_TYPE (node) = promote_type (type);
13543 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13544 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13545 return node;
13546}
13547
13548/* Verify that one entry of the initializer element list can be
13549 assigned to the array base type. Report 1 if an error occurred, 0
13550 otherwise. */
13551
13552static int
13553array_constructor_check_entry (type, entry)
13554 tree type, entry;
13555{
13556 char *array_type_string = NULL; /* For error reports */
13557 tree value, type_value, new_value, wfl_value, patched;
13558 int error_seen = 0;
13559
13560 new_value = NULL_TREE;
13561 wfl_value = TREE_VALUE (entry);
13562
100f7cd8 13563 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13564 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13565 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13566 if ((patched = patch_string (value)))
13567 value = patched;
1179ebc2
APB
13568 if (value == error_mark_node)
13569 return 1;
f8976021 13570
f8976021
APB
13571 type_value = TREE_TYPE (value);
13572
1179ebc2 13573 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13574 constant overflow during narrowing. */
13575 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13576 new_value = try_builtin_assignconv (wfl_operator, type, value);
13577 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13578 type_value = promote_type (type);
100f7cd8
APB
13579
13580 pop_obstacks ();
f8976021
APB
13581 /* Check and report errors */
13582 if (!new_value)
13583 {
49f48c71 13584 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13585 "Can't" : "Explicit cast needed to");
13586 if (!array_type_string)
c2e3db92 13587 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13588 parse_error_context
13589 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13590 msg, lang_printable_name (type_value, 1), array_type_string);
13591 error_seen = 1;
13592 }
13593
13594 if (new_value)
13595 {
b8c5b1c6 13596 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13597 TREE_VALUE (entry) = new_value;
13598 }
13599
13600 if (array_type_string)
13601 free (array_type_string);
13602
13603 TREE_PURPOSE (entry) = NULL_TREE;
13604 return error_seen;
13605}
13606
e04a16fb
AG
13607static tree
13608build_this (location)
13609 int location;
13610{
9ee9b555 13611 tree node = build_wfl_node (this_identifier_node);
b67d701b 13612 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13613 EXPR_WFL_LINECOL (node) = location;
13614 return node;
13615}
13616
13617/* 14.15 The return statement. It builds a modify expression that
13618 assigns the returned value to the RESULT_DECL that hold the value
13619 to be returned. */
13620
13621static tree
13622build_return (location, op)
13623 int location;
13624 tree op;
13625{
13626 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13627 EXPR_WFL_LINECOL (node) = location;
b67d701b 13628 node = build_debugable_stmt (location, node);
e04a16fb
AG
13629 return node;
13630}
13631
13632static tree
13633patch_return (node)
13634 tree node;
13635{
13636 tree return_exp = TREE_OPERAND (node, 0);
13637 tree meth = current_function_decl;
13638 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13639 int error_found = 0;
13640
13641 TREE_TYPE (node) = error_mark_node;
13642 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13643
13644 /* It's invalid to have a return value within a function that is
13645 declared with the keyword void or that is a constructor */
13646 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13647 error_found = 1;
13648
f099f336 13649 /* It's invalid to use a return statement in a static block */
c2952b01 13650 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13651 error_found = 1;
13652
e04a16fb
AG
13653 /* It's invalid to have a no return value within a function that
13654 isn't declared with the keyword `void' */
13655 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13656 error_found = 2;
c2952b01
APB
13657
13658 if (in_instance_initializer)
13659 error_found = 1;
e04a16fb
AG
13660
13661 if (error_found)
13662 {
c2952b01 13663 if (in_instance_initializer)
f099f336 13664 parse_error_context (wfl_operator,
c2952b01
APB
13665 "`return' inside instance initializer");
13666
13667 else if (DECL_CLINIT_P (current_function_decl))
13668 parse_error_context (wfl_operator,
13669 "`return' inside static initializer");
f099f336
APB
13670
13671 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13672 {
c2e3db92 13673 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13674 parse_error_context (wfl_operator,
13675 "`return' with%s value from `%s %s'",
13676 (error_found == 1 ? "" : "out"),
13677 t, lang_printable_name (meth, 0));
13678 free (t);
13679 }
13680 else
13681 parse_error_context (wfl_operator,
13682 "`return' with value from constructor `%s'",
13683 lang_printable_name (meth, 0));
e04a16fb
AG
13684 return error_mark_node;
13685 }
13686
5e942c50
APB
13687 /* If we have a return_exp, build a modify expression and expand
13688 it. Note: at that point, the assignment is declared valid, but we
13689 may want to carry some more hacks */
e04a16fb
AG
13690 if (return_exp)
13691 {
5e942c50
APB
13692 tree exp = java_complete_tree (return_exp);
13693 tree modify, patched;
13694
13695 /* If the function returned value and EXP are booleans, EXP has
13696 to be converted into the type of DECL_RESULT, which is integer
13697 (see complete_start_java_method) */
13698 if (TREE_TYPE (exp) == boolean_type_node &&
13699 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13700 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13701
13702 /* `null' can be assigned to a function returning a reference */
13703 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13704 exp == null_pointer_node)
13705 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13706
13707 if ((patched = patch_string (exp)))
13708 exp = patched;
13709
13710 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
13711 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13712 modify = java_complete_tree (modify);
5e942c50 13713
e04a16fb
AG
13714 if (modify != error_mark_node)
13715 {
13716 TREE_SIDE_EFFECTS (modify) = 1;
13717 TREE_OPERAND (node, 0) = modify;
13718 }
13719 else
13720 return error_mark_node;
13721 }
13722 TREE_TYPE (node) = void_type_node;
13723 TREE_SIDE_EFFECTS (node) = 1;
13724 return node;
13725}
13726
13727/* 14.8 The if Statement */
13728
13729static tree
13730build_if_else_statement (location, expression, if_body, else_body)
13731 int location;
13732 tree expression, if_body, else_body;
13733{
13734 tree node;
e04a16fb 13735 if (!else_body)
9bbc7d9f 13736 else_body = empty_stmt_node;
e04a16fb
AG
13737 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13738 EXPR_WFL_LINECOL (node) = location;
b67d701b 13739 node = build_debugable_stmt (location, node);
e04a16fb
AG
13740 return node;
13741}
13742
13743static tree
13744patch_if_else_statement (node)
13745 tree node;
13746{
13747 tree expression = TREE_OPERAND (node, 0);
13748
13749 TREE_TYPE (node) = error_mark_node;
13750 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13751
13752 /* The type of expression must be boolean */
b67d701b
PB
13753 if (TREE_TYPE (expression) != boolean_type_node
13754 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
13755 {
13756 parse_error_context
13757 (wfl_operator,
13758 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 13759 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
13760 return error_mark_node;
13761 }
13762
13763 TREE_TYPE (node) = void_type_node;
13764 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 13765 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
13766 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13767 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
13768 return node;
13769}
13770
13771/* 14.6 Labeled Statements */
13772
13773/* Action taken when a lableled statement is parsed. a new
13774 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 13775 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
13776
13777static tree
0a2138e2 13778build_labeled_block (location, label)
e04a16fb 13779 int location;
0a2138e2 13780 tree label;
e04a16fb 13781{
b635eb2f 13782 tree label_name ;
e04a16fb 13783 tree label_decl, node;
b635eb2f
PB
13784 if (label == NULL_TREE || label == continue_identifier_node)
13785 label_name = label;
13786 else
e04a16fb 13787 {
b635eb2f
PB
13788 label_name = merge_qualified_name (label_id, label);
13789 /* Issue an error if we try to reuse a label that was previously
13790 declared */
13791 if (IDENTIFIER_LOCAL_VALUE (label_name))
13792 {
13793 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
13794 parse_error_context (wfl_operator,
13795 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
13796 IDENTIFIER_POINTER (label));
13797 EXPR_WFL_LINECOL (wfl_operator) =
13798 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
13799 parse_error_context (wfl_operator,
13800 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
13801 IDENTIFIER_POINTER (label));
13802 java_error_count--;
13803 }
e04a16fb
AG
13804 }
13805
13806 label_decl = create_label_decl (label_name);
13807 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
13808 EXPR_WFL_LINECOL (node) = location;
13809 TREE_SIDE_EFFECTS (node) = 1;
13810 return node;
13811}
13812
b67d701b 13813/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
13814
13815static tree
b635eb2f 13816finish_labeled_statement (lbe, statement)
e04a16fb
AG
13817 tree lbe; /* Labeled block expr */
13818 tree statement;
13819{
13820 /* In anyways, tie the loop to its statement */
13821 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
13822 pop_labeled_block ();
13823 POP_LABELED_BLOCK ();
e04a16fb
AG
13824 return lbe;
13825}
13826
13827/* 14.10, 14.11, 14.12 Loop Statements */
13828
13829/* Create an empty LOOP_EXPR and make it the last in the nested loop
13830 list. */
13831
13832static tree
13833build_new_loop (loop_body)
13834 tree loop_body;
13835{
13836 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
13837 TREE_SIDE_EFFECTS (loop) = 1;
13838 PUSH_LOOP (loop);
13839 return loop;
13840}
13841
13842/* Create a loop body according to the following structure:
13843 COMPOUND_EXPR
13844 COMPOUND_EXPR (loop main body)
13845 EXIT_EXPR (this order is for while/for loops.
13846 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 13847 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
13848 BODY end of this labeled block)
13849 INCREMENT (if any)
13850
13851 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
13852 after the body, like in the do-while loop.
13853
13854 To obtain a loop, the loop body structure described above is
13855 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
13856
13857 LABELED_BLOCK_EXPR
13858 LABEL_DECL (use this label to exit the loop)
13859 LOOP_EXPR
13860 <structure described above> */
e04a16fb
AG
13861
13862static tree
13863build_loop_body (location, condition, reversed)
13864 int location;
13865 tree condition;
13866 int reversed;
13867{
0a2138e2 13868 tree first, second, body;
e04a16fb
AG
13869
13870 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
13871 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
13872 condition = build_debugable_stmt (location, condition);
13873 TREE_SIDE_EFFECTS (condition) = 1;
13874
b635eb2f 13875 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
13876 first = (reversed ? body : condition);
13877 second = (reversed ? condition : body);
13878 return
13879 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 13880 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
13881}
13882
13883/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
13884 their order) on the current loop. Unlink the current loop from the
13885 loop list. */
13886
13887static tree
b635eb2f 13888finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
13889 int location;
13890 tree condition, body;
13891 int reversed;
13892{
13893 tree to_return = ctxp->current_loop;
13894 tree loop_body = LOOP_EXPR_BODY (to_return);
13895 if (condition)
13896 {
13897 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
13898 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
13899 The real EXIT_EXPR is one operand further. */
13900 EXPR_WFL_LINECOL (cnode) = location;
13901 /* This one is for accurate error reports */
13902 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
13903 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
13904 }
13905 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
13906 POP_LOOP ();
13907 return to_return;
13908}
13909
b635eb2f 13910/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
13911 loops feature the condition part */
13912
13913static tree
b635eb2f 13914finish_for_loop (location, condition, update, body)
e04a16fb
AG
13915 int location;
13916 tree condition, update, body;
13917{
13918 /* Put the condition and the loop body in place */
b635eb2f 13919 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
13920 /* LOOP is the current loop which has been now popped of the loop
13921 stack. Install the update block */
13922 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
13923 return loop;
13924}
13925
5cbdba64
APB
13926/* Try to find the loop a block might be related to. This comprises
13927 the case where the LOOP_EXPR is found as the second operand of a
13928 COMPOUND_EXPR, because the loop happens to have an initialization
13929 part, then expressed as the first operand of the COMPOUND_EXPR. If
13930 the search finds something, 1 is returned. Otherwise, 0 is
13931 returned. The search is assumed to start from a
13932 LABELED_BLOCK_EXPR's block. */
13933
13934static tree
13935search_loop (statement)
13936 tree statement;
13937{
13938 if (TREE_CODE (statement) == LOOP_EXPR)
13939 return statement;
13940
13941 if (TREE_CODE (statement) == BLOCK)
13942 statement = BLOCK_SUBBLOCKS (statement);
13943 else
13944 return NULL_TREE;
13945
13946 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13947 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13948 statement = TREE_OPERAND (statement, 1);
13949
13950 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 13951 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
13952}
13953
13954/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
13955 returned otherwise. */
13956
13957static int
13958labeled_block_contains_loop_p (block, loop)
13959 tree block, loop;
13960{
13961 if (!block)
13962 return 0;
13963
13964 if (LABELED_BLOCK_BODY (block) == loop)
13965 return 1;
13966
c2952b01 13967 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
13968 return 1;
13969
13970 return 0;
13971}
13972
e04a16fb 13973/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 13974 insert LOOP as its body. */
e04a16fb
AG
13975
13976static tree
13977patch_loop_statement (loop)
13978 tree loop;
13979{
cd9643f7 13980 tree loop_label;
5cbdba64 13981
cd9643f7 13982 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
13983 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
13984 return loop;
13985
cd9643f7 13986 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
13987 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
13988 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
13989 LABELED_BLOCK_BODY (loop_label) = loop;
13990 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 13991 return loop_label;
e04a16fb
AG
13992}
13993
13994/* 14.13, 14.14: break and continue Statements */
13995
13996/* Build a break or a continue statement. a null NAME indicates an
13997 unlabeled break/continue statement. */
13998
13999static tree
14000build_bc_statement (location, is_break, name)
14001 int location, is_break;
14002 tree name;
14003{
14004 tree break_continue, label_block_expr = NULL_TREE;
14005
14006 if (name)
14007 {
14008 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14009 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14010 /* Null means that we don't have a target for this named
14011 break/continue. In this case, we make the target to be the
14012 label name, so that the error can be reported accuratly in
14013 patch_bc_statement. */
14014 label_block_expr = EXPR_WFL_NODE (name);
14015 }
14016 /* Unlabeled break/continue will be handled during the
14017 break/continue patch operation */
14018 break_continue
14019 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14020
14021 IS_BREAK_STMT_P (break_continue) = is_break;
14022 TREE_SIDE_EFFECTS (break_continue) = 1;
14023 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14024 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14025 return break_continue;
14026}
14027
14028/* Verification of a break/continue statement. */
14029
14030static tree
14031patch_bc_statement (node)
14032 tree node;
14033{
14034 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14035 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14036 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14037
e04a16fb 14038 /* Having an identifier here means that the target is unknown. */
b635eb2f 14039 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14040 {
14041 parse_error_context (wfl_operator, "No label definition found for `%s'",
14042 IDENTIFIER_POINTER (bc_label));
14043 return error_mark_node;
14044 }
b635eb2f 14045 if (! IS_BREAK_STMT_P (node))
e04a16fb 14046 {
b635eb2f
PB
14047 /* It's a continue statement. */
14048 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14049 {
b635eb2f
PB
14050 if (labeled_block == NULL_TREE)
14051 {
14052 if (bc_label == NULL_TREE)
14053 parse_error_context (wfl_operator,
14054 "`continue' must be in loop");
14055 else
1504b2b4
APB
14056 parse_error_context
14057 (wfl_operator, "continue label `%s' does not name a loop",
14058 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14059 return error_mark_node;
14060 }
14061 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14062 == continue_identifier_node)
14063 && (bc_label == NULL_TREE
14064 || TREE_CHAIN (labeled_block) == bc_label))
14065 {
14066 bc_label = labeled_block;
14067 break;
14068 }
e04a16fb 14069 }
e04a16fb 14070 }
b635eb2f 14071 else if (!bc_label)
34f4db93 14072 {
b635eb2f 14073 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14074 {
b635eb2f
PB
14075 if (labeled_block == NULL_TREE)
14076 {
14077 parse_error_context (wfl_operator,
14078 "`break' must be in loop or switch");
14079 return error_mark_node;
14080 }
14081 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14082 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14083 || search_loop (target_stmt))
b635eb2f
PB
14084 {
14085 bc_label = labeled_block;
14086 break;
14087 }
e04a16fb 14088 }
e04a16fb
AG
14089 }
14090
b635eb2f 14091 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14092 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14093
e04a16fb
AG
14094 /* Our break/continue don't return values. */
14095 TREE_TYPE (node) = void_type_node;
14096 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14097 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14098 sometimes, like after a if statement) */
14099 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14100 TREE_SIDE_EFFECTS (node) = 1;
14101 return node;
14102}
14103
14104/* Process the exit expression belonging to a loop. Its type must be
14105 boolean. */
14106
14107static tree
14108patch_exit_expr (node)
14109 tree node;
14110{
14111 tree expression = TREE_OPERAND (node, 0);
14112 TREE_TYPE (node) = error_mark_node;
14113 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14114
14115 /* The type of expression must be boolean */
14116 if (TREE_TYPE (expression) != boolean_type_node)
14117 {
14118 parse_error_context
14119 (wfl_operator,
781b0558 14120 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14121 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14122 return error_mark_node;
14123 }
14124 /* Now we know things are allright, invert the condition, fold and
14125 return */
14126 TREE_OPERAND (node, 0) =
14127 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14128
14129 if (! integer_zerop (TREE_OPERAND (node, 0))
14130 && ctxp->current_loop != NULL_TREE
14131 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14132 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14133 if (! integer_onep (TREE_OPERAND (node, 0)))
14134 CAN_COMPLETE_NORMALLY (node) = 1;
14135
14136
e04a16fb
AG
14137 TREE_TYPE (node) = void_type_node;
14138 return node;
14139}
b67d701b
PB
14140
14141/* 14.9 Switch statement */
14142
14143static tree
14144patch_switch_statement (node)
14145 tree node;
14146{
c877974e 14147 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14148
14149 /* Complete the switch expression */
14150 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14151 se_type = TREE_TYPE (se);
14152 /* The type of the switch expression must be char, byte, short or
14153 int */
14154 if (!JINTEGRAL_TYPE_P (se_type))
14155 {
14156 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14157 parse_error_context (wfl_operator,
14158 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14159 lang_printable_name (se_type, 0));
b67d701b
PB
14160 /* This is what java_complete_tree will check */
14161 TREE_OPERAND (node, 0) = error_mark_node;
14162 return error_mark_node;
14163 }
14164
15fdcfe9 14165 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14166
14167 /* Ready to return */
15fdcfe9 14168 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14169 {
14170 TREE_TYPE (node) = error_mark_node;
14171 return error_mark_node;
14172 }
14173 TREE_TYPE (node) = void_type_node;
14174 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14175 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14176 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14177 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14178 return node;
14179}
14180
165f37bc 14181/* 14.18 The try/catch statements */
b67d701b 14182
b67d701b 14183static tree
a7d8d81f 14184build_try_statement (location, try_block, catches)
b67d701b 14185 int location;
a7d8d81f
PB
14186 tree try_block, catches;
14187{
14188 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14189 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14190 return node;
b67d701b
PB
14191}
14192
a7d8d81f
PB
14193static tree
14194build_try_finally_statement (location, try_block, finally)
14195 int location;
14196 tree try_block, finally;
b67d701b 14197{
a7d8d81f
PB
14198 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14199 EXPR_WFL_LINECOL (node) = location;
14200 return node;
b67d701b
PB
14201}
14202
14203static tree
14204patch_try_statement (node)
14205 tree node;
14206{
14207 int error_found = 0;
14208 tree try = TREE_OPERAND (node, 0);
14209 /* Exception handlers are considered in left to right order */
14210 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14211 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14212
14213 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14214 to process the next catch clause. We process the catch clause before
14215 the try block so that when processing the try block we can check thrown
14216 exceptions againts the caught type list. */
b67d701b
PB
14217 for (current = catch; current; current = TREE_CHAIN (current))
14218 {
14219 tree carg_decl, carg_type;
14220 tree sub_current, catch_block, catch_clause;
14221 int unreachable;
14222
b67d701b 14223 /* At this point, the structure of the catch clause is
b67d701b
PB
14224 CATCH_EXPR (catch node)
14225 BLOCK (with the decl of the parameter)
14226 COMPOUND_EXPR
7525cc04 14227 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14228 BLOCK (catch clause block)
a7d8d81f
PB
14229 */
14230 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14231 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14232 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14233
14234 /* Catch clauses can't have more than one parameter declared,
14235 but it's already enforced by the grammar. Make sure that the
14236 only parameter of the clause statement in of class Throwable
14237 or a subclass of Throwable, but that was done earlier. The
14238 catch clause parameter type has also been resolved. */
14239
14240 /* Just make sure that the catch clause parameter type inherits
14241 from java.lang.Throwable */
14242 if (!inherits_from_p (carg_type, throwable_type_node))
14243 {
14244 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14245 parse_error_context (wfl_operator,
781b0558 14246 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14247 lang_printable_name (carg_type, 0));
b67d701b
PB
14248 error_found = 1;
14249 continue;
14250 }
14251
14252 /* Partial check for unreachable catch statement: The catch
14253 clause is reachable iff is no earlier catch block A in
14254 the try statement such that the type of the catch
14255 clause's parameter is the same as or a subclass of the
14256 type of A's parameter */
14257 unreachable = 0;
14258 for (sub_current = catch;
14259 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14260 {
14261 tree sub_catch_clause, decl;
a7d8d81f 14262 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14263 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14264
14265 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14266 {
14267 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14268 parse_error_context
781b0558
KG
14269 (wfl_operator,
14270 "`catch' not reached because of the catch clause at line %d",
14271 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14272 unreachable = error_found = 1;
14273 break;
14274 }
14275 }
b67d701b
PB
14276 /* Complete the catch clause block */
14277 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14278 if (catch_block == error_mark_node)
14279 {
14280 error_found = 1;
14281 continue;
14282 }
15fdcfe9
PB
14283 if (CAN_COMPLETE_NORMALLY (catch_block))
14284 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14285 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14286
14287 if (unreachable)
14288 continue;
14289
14290 /* Things to do here: the exception must be thrown */
14291
14292 /* Link this type to the caught type list */
14293 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14294 }
14295
b9f7e36c
APB
14296 PUSH_EXCEPTIONS (caught_type_list);
14297 if ((try = java_complete_tree (try)) == error_mark_node)
14298 error_found = 1;
15fdcfe9
PB
14299 if (CAN_COMPLETE_NORMALLY (try))
14300 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14301 POP_EXCEPTIONS ();
14302
b67d701b
PB
14303 /* Verification ends here */
14304 if (error_found)
14305 return error_mark_node;
14306
14307 TREE_OPERAND (node, 0) = try;
14308 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14309 TREE_TYPE (node) = void_type_node;
14310 return node;
14311}
b9f7e36c
APB
14312
14313/* 14.17 The synchronized Statement */
14314
14315static tree
14316patch_synchronized_statement (node, wfl_op1)
14317 tree node, wfl_op1;
14318{
5a005d9e 14319 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14320 tree block = TREE_OPERAND (node, 1);
5a005d9e 14321
d8fccff5 14322 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14323
14324 if (expr == error_mark_node)
14325 {
14326 block = java_complete_tree (block);
14327 return expr;
14328 }
b9f7e36c
APB
14329
14330 /* The TYPE of expr must be a reference type */
5a005d9e 14331 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14332 {
14333 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14334 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14335 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14336 return error_mark_node;
14337 }
14338
ce6e9147
APB
14339 if (flag_emit_xref)
14340 {
14341 TREE_OPERAND (node, 0) = expr;
14342 TREE_OPERAND (node, 1) = java_complete_tree (block);
14343 CAN_COMPLETE_NORMALLY (node) = 1;
14344 return node;
14345 }
14346
b9f7e36c
APB
14347 /* Generate a try-finally for the synchronized statement, except
14348 that the handler that catches all throw exception calls
14349 _Jv_MonitorExit and then rethrow the exception.
14350 The synchronized statement is then implemented as:
14351 TRY
14352 {
14353 _Jv_MonitorEnter (expression)
14354 synchronized_block
14355 _Jv_MonitorExit (expression)
14356 }
14357 CATCH_ALL
14358 {
14359 e = _Jv_exception_info ();
14360 _Jv_MonitorExit (expression)
14361 Throw (e);
14362 } */
14363
5a005d9e
PB
14364 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14365 BUILD_MONITOR_ENTER (enter, expr_decl);
14366 BUILD_MONITOR_EXIT (exit, expr_decl);
14367 CAN_COMPLETE_NORMALLY (enter) = 1;
14368 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14369 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14370 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14371 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14372 build (COMPOUND_EXPR, NULL_TREE,
14373 build (WITH_CLEANUP_EXPR, NULL_TREE,
14374 build (COMPOUND_EXPR, NULL_TREE,
96847892 14375 assignment, enter),
5a005d9e
PB
14376 NULL_TREE, exit),
14377 block));
14378 node = build_expr_block (node, expr_decl);
14379
14380 return java_complete_tree (node);
b9f7e36c
APB
14381}
14382
14383/* 14.16 The throw Statement */
14384
14385static tree
14386patch_throw_statement (node, wfl_op1)
14387 tree node, wfl_op1;
14388{
14389 tree expr = TREE_OPERAND (node, 0);
14390 tree type = TREE_TYPE (expr);
14391 int unchecked_ok = 0, tryblock_throws_ok = 0;
14392
14393 /* Thrown expression must be assignable to java.lang.Throwable */
14394 if (!try_reference_assignconv (throwable_type_node, expr))
14395 {
14396 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14397 parse_error_context (wfl_operator,
14398 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14399 lang_printable_name (type, 0));
b9f7e36c
APB
14400 /* If the thrown expression was a reference, we further the
14401 compile-time check. */
14402 if (!JREFERENCE_TYPE_P (type))
14403 return error_mark_node;
14404 }
14405
14406 /* At least one of the following must be true */
14407
14408 /* The type of the throw expression is a not checked exception,
14409 i.e. is a unchecked expression. */
c877974e 14410 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14411
c2952b01
APB
14412 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14413 /* An instance can't throw a checked excetion unless that exception
14414 is explicitely declared in the `throws' clause of each
14415 constructor. This doesn't apply to anonymous classes, since they
14416 don't have declared constructors. */
14417 if (!unchecked_ok
14418 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14419 {
14420 tree current;
14421 for (current = TYPE_METHODS (current_class); current;
14422 current = TREE_CHAIN (current))
14423 if (DECL_CONSTRUCTOR_P (current)
14424 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14425 {
14426 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)",
14427 lang_printable_name (TREE_TYPE (expr), 0));
14428 return error_mark_node;
14429 }
14430 }
14431
b9f7e36c
APB
14432 /* Throw is contained in a try statement and at least one catch
14433 clause can receive the thrown expression or the current method is
14434 declared to throw such an exception. Or, the throw statement is
14435 contained in a method or constructor declaration and the type of
14436 the Expression is assignable to at least one type listed in the
14437 throws clause the declaration. */
b9f7e36c 14438 if (!unchecked_ok)
f099f336 14439 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14440 if (!(unchecked_ok || tryblock_throws_ok))
14441 {
14442 /* If there is a surrounding try block that has no matching
14443 clatch clause, report it first. A surrounding try block exits
14444 only if there is something after the list of checked
14445 exception thrown by the current function (if any). */
14446 if (IN_TRY_BLOCK_P ())
781b0558 14447 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 14448 lang_printable_name (type, 0));
b9f7e36c
APB
14449 /* If we have no surrounding try statement and the method doesn't have
14450 any throws, report it now. FIXME */
f099f336
APB
14451
14452 /* We report that the exception can't be throw from a try block
14453 in all circumstances but when the `throw' is inside a static
14454 block. */
b9f7e36c
APB
14455 else if (!EXCEPTIONS_P (currently_caught_type_list)
14456 && !tryblock_throws_ok)
f099f336 14457 {
c2952b01 14458 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14459 parse_error_context (wfl_operator,
14460 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14461 lang_printable_name (type, 0));
14462 else
781b0558
KG
14463 parse_error_context (wfl_operator,
14464 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14465 lang_printable_name (type, 0));
14466 }
b9f7e36c
APB
14467 /* Otherwise, the current method doesn't have the appropriate
14468 throws declaration */
14469 else
781b0558 14470 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14471 lang_printable_name (type, 0));
b9f7e36c
APB
14472 return error_mark_node;
14473 }
14474
ce6e9147 14475 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14476 BUILD_THROW (node, expr);
ce6e9147
APB
14477
14478 /* If doing xrefs, keep the location where the `throw' was seen. */
14479 if (flag_emit_xref)
14480 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14481 return node;
14482}
14483
14484/* Check that exception said to be thrown by method DECL can be
14485 effectively caught from where DECL is invoked. */
14486
14487static void
14488check_thrown_exceptions (location, decl)
14489 int location;
14490 tree decl;
14491{
14492 tree throws;
14493 /* For all the unchecked exceptions thrown by DECL */
14494 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14495 throws = TREE_CHAIN (throws))
0a2138e2 14496 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14497 {
3e78f871
PB
14498#if 1
14499 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14500 if (DECL_NAME (decl) == get_identifier ("clone"))
14501 continue;
14502#endif
b9f7e36c 14503 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14504 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14505 parse_error_context
14506 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14507 lang_printable_name (TREE_VALUE (throws), 0));
14508 else
14509 {
14510 parse_error_context
781b0558 14511 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14512 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14513 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14514 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14515 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14516 }
b9f7e36c
APB
14517 }
14518}
14519
c877974e 14520/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14521 try-catch blocks, OR is listed in the `throws' clause of the
14522 current method. */
14523
14524static int
0a2138e2 14525check_thrown_exceptions_do (exception)
b9f7e36c
APB
14526 tree exception;
14527{
14528 tree list = currently_caught_type_list;
c877974e 14529 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14530 /* First, all the nested try-catch-finally at that stage. The
14531 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14532 if (IS_UNCHECKED_EXCEPTION_P (exception))
14533 return 1;
b9f7e36c
APB
14534 while (list)
14535 {
14536 tree caught;
14537 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14538 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14539 return 1;
14540 list = TREE_CHAIN (list);
14541 }
14542 return 0;
14543}
14544
14545static void
14546purge_unchecked_exceptions (mdecl)
14547 tree mdecl;
14548{
14549 tree throws = DECL_FUNCTION_THROWS (mdecl);
14550 tree new = NULL_TREE;
14551
14552 while (throws)
14553 {
14554 tree next = TREE_CHAIN (throws);
c877974e 14555 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14556 {
14557 TREE_CHAIN (throws) = new;
14558 new = throws;
14559 }
14560 throws = next;
14561 }
14562 /* List is inverted here, but it doesn't matter */
14563 DECL_FUNCTION_THROWS (mdecl) = new;
14564}
22eed1e6
APB
14565
14566/* 15.24 Conditional Operator ?: */
14567
14568static tree
14569patch_conditional_expr (node, wfl_cond, wfl_op1)
14570 tree node, wfl_cond, wfl_op1;
14571{
14572 tree cond = TREE_OPERAND (node, 0);
14573 tree op1 = TREE_OPERAND (node, 1);
14574 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14575 tree resulting_type = NULL_TREE;
ac825856 14576 tree t1, t2, patched;
22eed1e6
APB
14577 int error_found = 0;
14578
ac825856
APB
14579 /* Operands of ?: might be StringBuffers crafted as a result of a
14580 string concatenation. Obtain a descent operand here. */
14581 if ((patched = patch_string (op1)))
14582 TREE_OPERAND (node, 1) = op1 = patched;
14583 if ((patched = patch_string (op2)))
14584 TREE_OPERAND (node, 2) = op2 = patched;
14585
14586 t1 = TREE_TYPE (op1);
14587 t2 = TREE_TYPE (op2);
14588
22eed1e6
APB
14589 /* The first expression must be a boolean */
14590 if (TREE_TYPE (cond) != boolean_type_node)
14591 {
14592 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14593 parse_error_context (wfl_operator,
14594 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14595 lang_printable_name (TREE_TYPE (cond), 0));
14596 error_found = 1;
14597 }
14598
14599 /* Second and third can be numeric, boolean (i.e. primitive),
14600 references or null. Anything else results in an error */
14601 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14602 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14603 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14604 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14605 error_found = 1;
14606
14607 /* Determine the type of the conditional expression. Same types are
14608 easy to deal with */
14609 else if (t1 == t2)
14610 resulting_type = t1;
14611
14612 /* There are different rules for numeric types */
14613 else if (JNUMERIC_TYPE_P (t1))
14614 {
14615 /* if byte/short found, the resulting type is short */
14616 if ((t1 == byte_type_node && t2 == short_type_node)
14617 || (t1 == short_type_node && t2 == byte_type_node))
14618 resulting_type = short_type_node;
14619
14620 /* If t1 is a constant int and t2 is of type byte, short or char
14621 and t1's value fits in t2, then the resulting type is t2 */
14622 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14623 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14624 resulting_type = t2;
14625
14626 /* If t2 is a constant int and t1 is of type byte, short or char
14627 and t2's value fits in t1, then the resulting type is t1 */
14628 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14629 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14630 resulting_type = t1;
14631
14632 /* Otherwise, binary numeric promotion is applied and the
14633 resulting type is the promoted type of operand 1 and 2 */
14634 else
93024893 14635 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14636 &TREE_OPERAND (node, 1),
14637 &TREE_OPERAND (node, 2));
14638 }
14639
14640 /* Cases of a reference and a null type */
14641 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14642 resulting_type = t1;
14643
14644 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14645 resulting_type = t2;
14646
14647 /* Last case: different reference types. If a type can be converted
14648 into the other one by assignment conversion, the latter
14649 determines the type of the expression */
14650 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14651 resulting_type = promote_type (t1);
14652
14653 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14654 resulting_type = promote_type (t2);
14655
14656 /* If we don't have any resulting type, we're in trouble */
14657 if (!resulting_type)
14658 {
c2e3db92 14659 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14660 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14661 parse_error_context (wfl_operator,
14662 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14663 t, lang_printable_name (t2, 0));
22eed1e6
APB
14664 free (t);
14665 error_found = 1;
14666 }
14667
14668 if (error_found)
14669 {
14670 TREE_TYPE (node) = error_mark_node;
14671 return error_mark_node;
14672 }
14673
14674 TREE_TYPE (node) = resulting_type;
14675 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14676 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14677 return node;
14678}
ac825856 14679
5b09b33e
PB
14680/* Try to constant fold NODE.
14681 If NODE is not a constant expression, return NULL_EXPR.
14682 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14683
14684static tree
14685fold_constant_for_init (node, context)
14686 tree node;
14687 tree context;
14688{
14689 tree op0, op1, val;
14690 enum tree_code code = TREE_CODE (node);
14691
93024893 14692 if (code == STRING_CST)
5b09b33e 14693 return node;
93024893
APB
14694
14695 if (code == INTEGER_CST || code == REAL_CST)
14696 return convert (TREE_TYPE (context), node);
5b09b33e
PB
14697
14698 switch (code)
14699 {
5b09b33e
PB
14700 case PLUS_EXPR:
14701 case MINUS_EXPR:
bc3ca41b
PB
14702 case MULT_EXPR:
14703 case TRUNC_MOD_EXPR:
14704 case RDIV_EXPR:
5b09b33e
PB
14705 case LSHIFT_EXPR:
14706 case RSHIFT_EXPR:
14707 case URSHIFT_EXPR:
14708 case BIT_AND_EXPR:
14709 case BIT_XOR_EXPR:
14710 case BIT_IOR_EXPR:
5b09b33e
PB
14711 case TRUTH_ANDIF_EXPR:
14712 case TRUTH_ORIF_EXPR:
14713 case EQ_EXPR:
14714 case NE_EXPR:
14715 case GT_EXPR:
14716 case GE_EXPR:
14717 case LT_EXPR:
14718 case LE_EXPR:
14719 op0 = TREE_OPERAND (node, 0);
14720 op1 = TREE_OPERAND (node, 1);
14721 val = fold_constant_for_init (op0, context);
14722 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14723 return NULL_TREE;
14724 TREE_OPERAND (node, 0) = val;
14725 val = fold_constant_for_init (op1, context);
14726 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14727 return NULL_TREE;
14728 TREE_OPERAND (node, 1) = val;
14729 return patch_binop (node, op0, op1);
14730
14731 case UNARY_PLUS_EXPR:
14732 case NEGATE_EXPR:
14733 case TRUTH_NOT_EXPR:
14734 case BIT_NOT_EXPR:
14735 case CONVERT_EXPR:
14736 op0 = TREE_OPERAND (node, 0);
14737 val = fold_constant_for_init (op0, context);
14738 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14739 return NULL_TREE;
14740 TREE_OPERAND (node, 0) = val;
5a005d9e 14741 return patch_unaryop (node, op0);
5b09b33e
PB
14742 break;
14743
14744 case COND_EXPR:
14745 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14746 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14747 return NULL_TREE;
14748 TREE_OPERAND (node, 0) = val;
14749 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14750 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14751 return NULL_TREE;
14752 TREE_OPERAND (node, 1) = val;
14753 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14754 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14755 return NULL_TREE;
14756 TREE_OPERAND (node, 2) = val;
14757 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14758 : TREE_OPERAND (node, 2);
14759
14760 case VAR_DECL:
8576f094
APB
14761 case FIELD_DECL:
14762 if (! FIELD_FINAL (node)
5b09b33e
PB
14763 || DECL_INITIAL (node) == NULL_TREE)
14764 return NULL_TREE;
14765 val = DECL_INITIAL (node);
14766 /* Guard against infinite recursion. */
14767 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 14768 val = fold_constant_for_init (val, node);
5b09b33e
PB
14769 DECL_INITIAL (node) = val;
14770 return val;
14771
14772 case EXPR_WITH_FILE_LOCATION:
14773 /* Compare java_complete_tree and resolve_expression_name. */
14774 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14775 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14776 {
14777 tree name = EXPR_WFL_NODE (node);
14778 tree decl;
14779 if (PRIMARY_P (node))
14780 return NULL_TREE;
14781 else if (! QUALIFIED_P (name))
14782 {
14783 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
14784 if (decl == NULL_TREE
14785 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
14786 return NULL_TREE;
14787 return fold_constant_for_init (decl, decl);
14788 }
14789 else
14790 {
5b09b33e
PB
14791 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14792 qualify_ambiguous_name (node);
14793 if (resolve_field_access (node, &decl, NULL)
14794 && decl != NULL_TREE)
14795 return fold_constant_for_init (decl, decl);
5b09b33e
PB
14796 return NULL_TREE;
14797 }
14798 }
14799 else
14800 {
14801 op0 = TREE_OPERAND (node, 0);
14802 val = fold_constant_for_init (op0, context);
14803 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14804 return NULL_TREE;
14805 TREE_OPERAND (node, 0) = val;
14806 return val;
14807 }
14808
bc3ca41b
PB
14809#ifdef USE_COMPONENT_REF
14810 case IDENTIFIER:
14811 case COMPONENT_REF:
14812 ?;
14813#endif
14814
5b09b33e
PB
14815 default:
14816 return NULL_TREE;
14817 }
14818}
bc3ca41b
PB
14819
14820#ifdef USE_COMPONENT_REF
14821/* Context is 'T' for TypeName, 'P' for PackageName,
14822 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
14823
14824tree
14825resolve_simple_name (name, context)
14826 tree name;
14827 int context;
14828{
14829}
14830
14831tree
14832resolve_qualified_name (name, context)
14833 tree name;
14834 int context;
14835{
14836}
14837#endif
This page took 2.80907 seconds and 5 git commands to generate.