]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
cppfiles.c (cpp_make_system_header): New function.
[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;
14d075d8
TT
4616 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4617 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4618 if (acc_count > 1)
4619 parse_error_context (cl,
4620 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4621}
4622
4623/* Check the methode header METH for abstract specifics features */
4624
4625static void
4626check_abstract_method_header (meth)
4627 tree meth;
4628{
4629 int flags = get_access_flags_from_decl (meth);
4630 /* DECL_NAME might still be a WFL node */
c877974e 4631 tree name = GET_METHOD_NAME (meth);
e04a16fb 4632
2884c41e
KG
4633 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4634 ACC_ABSTRACT, "abstract method",
4635 IDENTIFIER_POINTER (name));
4636 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4637 ACC_PUBLIC, "abstract method",
4638 IDENTIFIER_POINTER (name));
e04a16fb
AG
4639
4640 check_modifiers ("Illegal modifier `%s' for interface method",
4641 flags, INTERFACE_METHOD_MODIFIERS);
4642}
4643
4644/* Create a FUNCTION_TYPE node and start augmenting it with the
4645 declared function arguments. Arguments type that can't be resolved
4646 are left as they are, but the returned node is marked as containing
4647 incomplete types. */
4648
4649static tree
4650method_declarator (id, list)
4651 tree id, list;
4652{
4653 tree arg_types = NULL_TREE, current, node;
4654 tree meth = make_node (FUNCTION_TYPE);
4655 jdep *jdep;
e04a16fb
AG
4656
4657 patch_stage = JDEP_NO_PATCH;
c2952b01
APB
4658
4659 /* If we're dealing with an inner class constructor, we hide the
4660 this$<n> decl in the name field of its parameter declaration. We
4661 also might have to hide the outer context local alias
4662 initializers. Not done when the class is a toplevel class. */
4663 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4664 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4665 {
4666 tree aliases_list, type, thisn;
4667 /* First the aliases, linked to the regular parameters */
4668 aliases_list =
4669 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4670 TREE_TYPE (GET_CPC ()),
4671 NULL_TREE, NULL);
4672 list = chainon (nreverse (aliases_list), list);
4673
4674 /* Then this$<n> */
4675 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4676 thisn = build_current_thisn (TYPE_NAME (GET_CPC ()));
4677 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4678 list);
4679 }
e04a16fb
AG
4680
4681 for (current = list; current; current = TREE_CHAIN (current))
4682 {
c583dd46 4683 int must_chain = 0;
e04a16fb
AG
4684 tree wfl_name = TREE_PURPOSE (current);
4685 tree type = TREE_VALUE (current);
4686 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4687 tree already, arg_node;
4688 tree type_wfl = NULL_TREE;
23a79c61 4689 tree real_type;
c583dd46
APB
4690
4691 /* Obtain a suitable type for resolution, if necessary */
4692 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4693
4694 /* Process NAME, as it may specify extra dimension(s) for it */
4695 type = build_array_from_name (type, type_wfl, name, &name);
4696 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4697
23a79c61
APB
4698 real_type = GET_REAL_TYPE (type);
4699 if (TREE_CODE (real_type) == RECORD_TYPE)
4700 {
4701 real_type = promote_type (real_type);
4702 if (TREE_CODE (type) == TREE_LIST)
4703 TREE_PURPOSE (type) = real_type;
4704 }
5e942c50 4705
e04a16fb
AG
4706 /* Check redefinition */
4707 for (already = arg_types; already; already = TREE_CHAIN (already))
4708 if (TREE_PURPOSE (already) == name)
4709 {
781b0558
KG
4710 parse_error_context
4711 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4712 IDENTIFIER_POINTER (name),
e04a16fb
AG
4713 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4714 break;
4715 }
4716
4717 /* If we've an incomplete argument type, we know there is a location
4718 to patch when the type get resolved, later. */
4719 jdep = NULL;
c583dd46 4720 if (must_chain)
e04a16fb 4721 {
c583dd46
APB
4722 patch_stage = JDEP_METHOD;
4723 type = register_incomplete_type (patch_stage,
4724 type_wfl, wfl_name, type);
4725 jdep = CLASSD_LAST (ctxp->classd_list);
4726 JDEP_MISC (jdep) = id;
e04a16fb 4727 }
c583dd46 4728
c2952b01 4729 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4730 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4731 /* Remeber arguments declared final. */
4732 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4733
e04a16fb
AG
4734 if (jdep)
4735 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4736 TREE_CHAIN (arg_node) = arg_types;
4737 arg_types = arg_node;
4738 }
de4c7b02 4739 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4740 node = build_tree_list (id, meth);
4741 return node;
4742}
4743
4744static int
4745unresolved_type_p (wfl, returned)
4746 tree wfl;
4747 tree *returned;
4748
4749{
4750 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4751 {
e04a16fb 4752 if (returned)
165f37bc
APB
4753 {
4754 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4755 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4756 *returned = TREE_TYPE (decl);
4757 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4758 *returned = TREE_TYPE (GET_CPC ());
4759 else
4760 *returned = NULL_TREE;
4761 }
e04a16fb
AG
4762 return 1;
4763 }
4764 if (returned)
4765 *returned = wfl;
4766 return 0;
4767}
4768
4769/* From NAME, build a qualified identifier node using the
4770 qualification from the current package definition. */
4771
4772static tree
c2952b01
APB
4773parser_qualified_classname (is_static, name)
4774 int is_static;
e04a16fb
AG
4775 tree name;
4776{
c2952b01
APB
4777 tree nested_class_name;
4778
4779 if (!is_static
4780 && (nested_class_name = maybe_make_nested_class_name (name)))
4781 return nested_class_name;
4782
e04a16fb 4783 if (ctxp->package)
c2952b01 4784 return merge_qualified_name (ctxp->package, name);
e04a16fb 4785 else
c2952b01 4786 return name;
e04a16fb
AG
4787}
4788
4789/* Called once the type a interface extends is resolved. Returns 0 if
4790 everything is OK. */
4791
4792static int
4793parser_check_super_interface (super_decl, this_decl, this_wfl)
4794 tree super_decl, this_decl, this_wfl;
4795{
4796 tree super_type = TREE_TYPE (super_decl);
4797
4798 /* Has to be an interface */
c2952b01 4799 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4800 {
4801 parse_error_context
4802 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4803 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4804 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4805 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4806 "interface" : "class"),
4807 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4808 return 1;
4809 }
4810
4811 /* Check scope: same package OK, other package: OK if public */
4812 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4813 return 1;
4814
4815 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4816 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4817 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4818 return 0;
4819}
4820
4821/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4822 0 if everthing is OK. */
4823
4824static int
4825parser_check_super (super_decl, this_decl, wfl)
4826 tree super_decl, this_decl, wfl;
4827{
e04a16fb
AG
4828 tree super_type = TREE_TYPE (super_decl);
4829
4830 /* SUPER should be a CLASS (neither an array nor an interface) */
4831 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4832 {
4833 parse_error_context
4834 (wfl, "Class `%s' can't subclass %s `%s'",
4835 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4836 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4837 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4838 return 1;
4839 }
4840
4841 if (CLASS_FINAL (TYPE_NAME (super_type)))
4842 {
4843 parse_error_context (wfl, "Can't subclass final classes: %s",
4844 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4845 return 1;
4846 }
4847
4848 /* Check scope: same package OK, other package: OK if public */
4849 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4850 return 1;
4851
4852 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4853 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4854 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4855 return 0;
4856}
4857
4858/* Create a new dependency list and link it (in a LIFO manner) to the
4859 CTXP list of type dependency list. */
4860
4861static void
4862create_jdep_list (ctxp)
4863 struct parser_ctxt *ctxp;
4864{
23a79c61 4865 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4866 new->first = new->last = NULL;
4867 new->next = ctxp->classd_list;
4868 ctxp->classd_list = new;
4869}
4870
4871static jdeplist *
4872reverse_jdep_list (ctxp)
4873 struct parser_ctxt *ctxp;
4874{
4875 register jdeplist *prev = NULL, *current, *next;
4876 for (current = ctxp->classd_list; current; current = next)
4877 {
4878 next = current->next;
4879 current->next = prev;
4880 prev = current;
4881 }
4882 return prev;
4883}
4884
23a79c61
APB
4885/* Create a fake pointer based on the ID stored in
4886 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4887 registered again. */
e04a16fb
AG
4888
4889static tree
23a79c61
APB
4890obtain_incomplete_type (type_name)
4891 tree type_name;
e04a16fb 4892{
23a79c61
APB
4893 tree ptr, name;
4894
4895 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4896 name = EXPR_WFL_NODE (type_name);
4897 else if (INCOMPLETE_TYPE_P (type_name))
4898 name = TYPE_NAME (type_name);
4899 else
4900 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4901
4902 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4903 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4904 break;
4905
4906 if (!ptr)
4907 {
e04a16fb 4908 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4909 BUILD_PTR_FROM_NAME (ptr, name);
4910 layout_type (ptr);
e04a16fb
AG
4911 pop_obstacks ();
4912 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4913 ctxp->incomplete_class = ptr;
4914 }
4915
4916 return ptr;
4917}
4918
4919/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4920 non NULL instead of computing a new fake type based on WFL. The new
4921 dependency is inserted in the current type dependency list, in FIFO
4922 manner. */
4923
4924static tree
4925register_incomplete_type (kind, wfl, decl, ptr)
4926 int kind;
4927 tree wfl, decl, ptr;
4928{
23a79c61 4929 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4930
e04a16fb
AG
4931 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4932 ptr = obtain_incomplete_type (wfl);
4933
4934 JDEP_KIND (new) = kind;
4935 JDEP_DECL (new) = decl;
4936 JDEP_SOLV (new) = ptr;
4937 JDEP_WFL (new) = wfl;
4938 JDEP_CHAIN (new) = NULL;
4939 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4940 /* For some dependencies, set the enclosing class of the current
4941 class to be the enclosing context */
4942 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4943 && GET_ENCLOSING_CPC ())
4944 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4945 else
324ed8fd 4946 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4947 JDEP_GET_PATCH (new) = (tree *)NULL;
4948
4949 JDEP_INSERT (ctxp->classd_list, new);
4950
4951 return ptr;
4952}
4953
4954void
4955java_check_circular_reference ()
4956{
4957 tree current;
4958 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4959 {
4960 tree type = TREE_TYPE (current);
e920ebc9 4961 if (CLASS_INTERFACE (current))
e04a16fb
AG
4962 {
4963 /* Check all interfaces this class extends */
4964 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4965 int n, i;
4966
4967 if (!basetype_vec)
4968 return;
4969 n = TREE_VEC_LENGTH (basetype_vec);
4970 for (i = 0; i < n; i++)
4971 {
4972 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4973 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4974 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4975 parse_error_context (lookup_cl (current),
4976 "Cyclic interface inheritance");
4977 }
4978 }
4979 else
4980 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4981 parse_error_context (lookup_cl (current),
c2952b01
APB
4982 "Cyclic class inheritance%s",
4983 (cyclic_inheritance_report ?
4984 cyclic_inheritance_report : ""));
4985 }
4986}
4987
4988/* Augment the parameter list PARM with parameters crafted to
4989 initialize outer context locals aliases. Through ARTIFICIAL, a
4990 count is kept of the number of crafted parameters. MODE governs
4991 what eventually gets created: something suitable for a function
4992 creation or a function invocation, either the constructor or
4993 $finit$. */
4994
4995static tree
4996build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
4997 int mode;
4998 tree class_type, parm;
4999 int *artificial;
5000{
5001 tree field;
5002 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5003 if (FIELD_LOCAL_ALIAS (field))
5004 {
5005 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5006 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5007
5008 switch (mode)
5009 {
5010 case AIPL_FUNCTION_DECLARATION:
5011 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5012 purpose = build_wfl_node (get_identifier (buffer));
5013 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5014 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5015 else
5016 value = TREE_TYPE (field);
5017 break;
5018
5019 case AIPL_FUNCTION_CREATION:
5020 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5021 purpose = get_identifier (buffer);
5022 value = TREE_TYPE (field);
5023 break;
5024
5025 case AIPL_FUNCTION_FINIT_INVOCATION:
5026 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5027 /* Now, this is wrong. purpose should always be the NAME
5028 of something and value its matching value (decl, type,
5029 etc...) FIXME -- but there is a lot to fix. */
5030
5031 /* When invoked for this kind of operation, we already
5032 know whether a field is used or not. */
5033 purpose = TREE_TYPE (field);
5034 value = build_wfl_node (get_identifier (buffer));
5035 break;
5036
5037 case AIPL_FUNCTION_CTOR_INVOCATION:
5038 /* There are two case: the constructor invokation happends
5039 outside the local inner, in which case, locales from the outer
5040 context are directly used.
5041
5042 Otherwise, we fold to using the alias directly. */
5043 if (class_type == current_class)
5044 value = field;
5045 else
5046 {
5047 name = get_identifier (&buffer[4]);
5048 value = IDENTIFIER_LOCAL_VALUE (name);
5049 }
5050 break;
5051 }
5052 parm = tree_cons (purpose, value, parm);
5053 if (artificial)
5054 *artificial +=1;
5055 }
5056 return parm;
5057}
5058
5059/* Craft a constructor for CLASS_DECL -- what we should do when none
5060 where found. ARGS is non NULL when a special signature must be
5061 enforced. This is the case for anonymous classes. */
5062
5063static void
5064craft_constructor (class_decl, args)
5065 tree class_decl, args;
5066{
5067 tree class_type = TREE_TYPE (class_decl);
5068 tree parm = NULL_TREE;
5069 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5070 ACC_PUBLIC : 0);
5071 int i = 0, artificial = 0;
5072 tree decl, ctor_name;
5073 char buffer [80];
5074
5075 push_obstacks (&permanent_obstack, &permanent_obstack);
5076
5077 /* The constructor name is <init> unless we're dealing with an
5078 anonymous class, in which case the name will be fixed after having
5079 be expanded. */
5080 if (ANONYMOUS_CLASS_P (class_type))
5081 ctor_name = DECL_NAME (class_decl);
5082 else
5083 ctor_name = init_identifier_node;
5084
5085 /* If we're dealing with an inner class constructor, we hide the
5086 this$<n> decl in the name field of its parameter declaration. */
5087 if (PURE_INNER_CLASS_TYPE_P (class_type))
5088 {
5089 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5090 parm = tree_cons (build_current_thisn (class_type),
5091 build_pointer_type (type), parm);
5092
5093 /* Some more arguments to be hidden here. The values of the local
5094 variables of the outer context that the inner class needs to see. */
5095 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5096 class_type, parm,
5097 &artificial);
5098 }
5099
5100 /* Then if there are any args to be enforced, enforce them now */
5101 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5102 {
5103 sprintf (buffer, "parm%d", i++);
5104 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5105 }
c2952b01
APB
5106
5107 CRAFTED_PARAM_LIST_FIXUP (parm);
5108 decl = create_artificial_method (class_type, flags, void_type_node,
5109 ctor_name, parm);
5110 fix_method_argument_names (parm, decl);
5111 /* Now, mark the artificial parameters. */
5112 DECL_FUNCTION_NAP (decl) = artificial;
5113
5114 pop_obstacks ();
5115 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5116}
5117
c2952b01 5118
e920ebc9
APB
5119/* Fix the constructors. This will be called right after circular
5120 references have been checked. It is necessary to fix constructors
5121 early even if no code generation will take place for that class:
5122 some generated constructor might be required by the class whose
5123 compilation triggered this one to be simply loaded. */
5124
5125void
5126java_fix_constructors ()
5127{
5128 tree current;
5129
5130 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5131 {
e920ebc9
APB
5132 tree class_type = TREE_TYPE (current);
5133 int saw_ctor = 0;
c2952b01
APB
5134 tree decl;
5135
5136 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5137 continue;
e920ebc9
APB
5138
5139 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5140 {
5141 if (DECL_CONSTRUCTOR_P (decl))
5142 {
5143 fix_constructors (decl);
5144 saw_ctor = 1;
5145 }
5146 }
5147
c2952b01
APB
5148 /* Anonymous class constructor can't be generated that early. */
5149 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5150 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5151 }
5152}
5153
23a79c61
APB
5154/* safe_layout_class just makes sure that we can load a class without
5155 disrupting the current_class, input_file, lineno, etc, information
5156 about the class processed currently. */
5157
e04a16fb
AG
5158void
5159safe_layout_class (class)
5160 tree class;
5161{
5162 tree save_current_class = current_class;
3b304f5b 5163 const char *save_input_filename = input_filename;
e04a16fb 5164 int save_lineno = lineno;
5e942c50 5165
e04a16fb 5166 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5167
e04a16fb
AG
5168 layout_class (class);
5169 pop_obstacks ();
5e942c50 5170
e04a16fb
AG
5171 current_class = save_current_class;
5172 input_filename = save_input_filename;
5173 lineno = save_lineno;
5174 CLASS_LOADED_P (class) = 1;
5175}
5176
5177static tree
5178jdep_resolve_class (dep)
5179 jdep *dep;
5180{
5181 tree decl;
5182
23a79c61
APB
5183 if (JDEP_RESOLVED_P (dep))
5184 decl = JDEP_RESOLVED_DECL (dep);
5185 else
e04a16fb 5186 {
c2952b01 5187 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5188 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5189 JDEP_RESOLVED (dep, decl);
5190 }
23a79c61 5191
e04a16fb 5192 if (!decl)
23a79c61
APB
5193 complete_class_report_errors (dep);
5194
e04a16fb
AG
5195 return decl;
5196}
5197
5198/* Complete unsatisfied class declaration and their dependencies */
5199
5200void
5201java_complete_class ()
5202{
e04a16fb
AG
5203 tree cclass;
5204 jdeplist *cclassd;
5205 int error_found;
b67d701b 5206 tree type;
e04a16fb
AG
5207
5208 push_obstacks (&permanent_obstack, &permanent_obstack);
5209
5210 /* Process imports and reverse the import on demand list */
5211 process_imports ();
5212 if (ctxp->import_demand_list)
5213 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
5214
5215 /* Rever things so we have the right order */
5216 ctxp->class_list = nreverse (ctxp->class_list);
5217 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5218
e04a16fb
AG
5219 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5220 cclass && cclassd;
5221 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5222 {
5223 jdep *dep;
5224 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5225 {
5226 tree decl;
e04a16fb
AG
5227 if (!(decl = jdep_resolve_class (dep)))
5228 continue;
5229
5230 /* Now it's time to patch */
5231 switch (JDEP_KIND (dep))
5232 {
5233 case JDEP_SUPER:
5234 /* Simply patch super */
5235 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5236 continue;
5237 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5238 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5239 break;
5240
5241 case JDEP_FIELD:
5242 {
5243 /* We do part of the job done in add_field */
5244 tree field_decl = JDEP_DECL (dep);
5245 tree field_type = TREE_TYPE (decl);
5246 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5247 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5248 field_type = promote_type (field_type);
5249 pop_obstacks ();
5250 TREE_TYPE (field_decl) = field_type;
5e942c50 5251 DECL_ALIGN (field_decl) = 0;
11cf4d18 5252 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5253 layout_decl (field_decl, 0);
e04a16fb
AG
5254 SOURCE_FRONTEND_DEBUG
5255 (("Completed field/var decl `%s' with `%s'",
5256 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5257 IDENTIFIER_POINTER (DECL_NAME (decl))));
5258 break;
5259 }
5260 case JDEP_METHOD: /* We start patching a method */
5261 case JDEP_METHOD_RETURN:
5262 error_found = 0;
5263 while (1)
5264 {
5265 if (decl)
5266 {
b67d701b
PB
5267 type = TREE_TYPE(decl);
5268 if (TREE_CODE (type) == RECORD_TYPE)
5269 type = promote_type (type);
e04a16fb
AG
5270 JDEP_APPLY_PATCH (dep, type);
5271 SOURCE_FRONTEND_DEBUG
5272 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5273 "Completing fct `%s' with ret type `%s'":
5274 "Completing arg `%s' with type `%s'"),
5275 IDENTIFIER_POINTER (EXPR_WFL_NODE
5276 (JDEP_DECL_WFL (dep))),
5277 IDENTIFIER_POINTER (DECL_NAME (decl))));
5278 }
5279 else
5280 error_found = 1;
5281 dep = JDEP_CHAIN (dep);
5282 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5283 break;
5284 else
5285 decl = jdep_resolve_class (dep);
5286 }
5287 if (!error_found)
5288 {
5289 tree mdecl = JDEP_DECL (dep), signature;
5290 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5291 /* Recompute and reset the signature, check first that
5292 all types are now defined. If they're not,
5293 dont build the signature. */
5294 if (check_method_types_complete (mdecl))
5295 {
5296 signature = build_java_signature (TREE_TYPE (mdecl));
5297 set_java_signature (TREE_TYPE (mdecl), signature);
5298 }
e04a16fb
AG
5299 pop_obstacks ();
5300 }
5301 else
5302 continue;
5303 break;
5304
5305 case JDEP_INTERFACE:
5306 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5307 JDEP_WFL (dep)))
5308 continue;
5309 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5310 break;
5311
b67d701b 5312 case JDEP_PARM:
e04a16fb 5313 case JDEP_VARIABLE:
b67d701b
PB
5314 type = TREE_TYPE(decl);
5315 if (TREE_CODE (type) == RECORD_TYPE)
5316 type = promote_type (type);
5317 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5318 break;
5319
5320 case JDEP_TYPE:
5321 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5322 SOURCE_FRONTEND_DEBUG
5323 (("Completing a random type dependency on a '%s' node",
5324 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5325 break;
5326
b9f7e36c 5327 case JDEP_EXCEPTION:
c877974e
APB
5328 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5329 SOURCE_FRONTEND_DEBUG
5330 (("Completing `%s' `throws' argument node",
5331 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5332 break;
5333
c2952b01
APB
5334 case JDEP_ANONYMOUS:
5335 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5336 break;
5337
e04a16fb 5338 default:
0a2138e2
APB
5339 fatal ("Can't handle patch code %d - java_complete_class",
5340 JDEP_KIND (dep));
e04a16fb
AG
5341 }
5342 }
5343 }
5344 pop_obstacks ();
5345 return;
5346}
5347
5348/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5349 array. */
5350
5351static tree
c2952b01
APB
5352resolve_class (enclosing, class_type, decl, cl)
5353 tree enclosing, class_type, decl, cl;
e04a16fb 5354{
49f48c71
KG
5355 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5356 const char *base = name;
78d21f92
PB
5357 tree resolved_type = TREE_TYPE (class_type);
5358 tree resolved_type_decl;
e04a16fb 5359
78d21f92
PB
5360 if (resolved_type != NULL_TREE)
5361 {
5362 tree resolved_type_decl = TYPE_NAME (resolved_type);
5363 if (resolved_type_decl == NULL_TREE
5364 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5365 {
5366 resolved_type_decl = build_decl (TYPE_DECL,
5367 TYPE_NAME (class_type),
5368 resolved_type);
5369 }
5370 return resolved_type_decl;
5371 }
5372
e04a16fb
AG
5373 /* 1- Check to see if we have an array. If true, find what we really
5374 want to resolve */
5375 while (name[0] == '[')
5376 name++;
5377 if (base != name)
5378 TYPE_NAME (class_type) = get_identifier (name);
5379
5380 /* 2- Resolve the bare type */
c2952b01
APB
5381 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5382 decl, cl)))
e04a16fb
AG
5383 return NULL_TREE;
5384 resolved_type = TREE_TYPE (resolved_type_decl);
5385
5386 /* 3- If we have and array, reconstruct the array down to its nesting */
5387 if (base != name)
5388 {
5389 while (base != name)
5390 {
5391 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5392 resolved_type = promote_type (resolved_type);
5393 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5394 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5395 name--;
5396 }
5397 /* Build a fake decl for this, since this is what is expected to
5398 be returned. */
5399 resolved_type_decl =
5400 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5401 /* Figure how those two things are important for error report. FIXME */
5402 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5403 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5404 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5405 }
78d21f92 5406 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5407 return resolved_type_decl;
5408}
5409
5410/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5411 are used to report error messages. */
5412
78d21f92 5413tree
c2952b01
APB
5414do_resolve_class (enclosing, class_type, decl, cl)
5415 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5416{
5417 tree new_class_decl;
5418 tree original_name = NULL_TREE;
5419
5420 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5421 its is changed by find_in_imports{_on_demand} */
5422
c2952b01
APB
5423 /* 0- Search in the current class as an inner class */
5424
5425 /* Maybe some code here should be added to load the class or
5426 something, at least if the class isn't an inner class and ended
5427 being loaded from class file. FIXME. */
a40d21da
APB
5428 while (enclosing)
5429 {
5430 tree name;
5431
5432 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5433 return new_class_decl;
5434
5435 /* Now go to the upper classes, bail out if necessary. */
5436 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5437 if (!enclosing || enclosing == object_type_node)
5438 break;
5439
5440 if (TREE_CODE (enclosing) == RECORD_TYPE)
5441 {
5442 enclosing = TYPE_NAME (enclosing);
5443 continue;
5444 }
5445
5446 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5447 {
5448 BUILD_PTR_FROM_NAME (name, enclosing);
5449 }
5450 else
5451 name = enclosing;
5452 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5453 }
c2952b01 5454
e04a16fb
AG
5455 /* 1- Check for the type in single imports */
5456 if (find_in_imports (class_type))
5457 return NULL_TREE;
5458
5459 /* 2- And check for the type in the current compilation unit. If it fails,
ee07f4f4 5460 try with a name qualified with the package name we've seen so far */
e04a16fb
AG
5461 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5462 {
5463 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5464 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5465 load_class (TYPE_NAME (class_type), 0);
5466 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5467 }
5468
5469 original_name = TYPE_NAME (class_type);
ee07f4f4 5470 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5471 {
ee07f4f4
APB
5472 tree package;
5473 for (package = package_list; package; package = TREE_CHAIN (package))
c2952b01
APB
5474 {
5475 tree new_qualified;
5476
5477 new_qualified = merge_qualified_name (TREE_PURPOSE (package),
5478 original_name);
5479 TYPE_NAME (class_type) = new_qualified;
5480 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5481 if (!new_class_decl)
5482 load_class (TYPE_NAME (class_type), 0);
5483 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5484 if (new_class_decl)
5485 {
5486 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5487 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5488 load_class (TYPE_NAME (class_type), 0);
5489 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5490 }
bc3ca41b
PB
5491 }
5492 }
c2952b01 5493
e04a16fb
AG
5494 TYPE_NAME (class_type) = original_name;
5495
5496 /* 3- Check an other compilation unit that bears the name of type */
5497 load_class (TYPE_NAME (class_type), 0);
5498 if (check_pkg_class_access (TYPE_NAME (class_type),
5499 (cl ? cl : lookup_cl (decl))))
5500 return NULL_TREE;
5501
5502 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5503 return new_class_decl;
5504
5505 /* 4- Check the import on demands. Don't allow bar.baz to be
5506 imported from foo.* */
5507 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5508 if (find_in_imports_on_demand (class_type))
5509 return NULL_TREE;
5510
5511 /* 5- Last call for a resolution */
5512 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5513}
5514
5515/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5516 parsed class). Return a decl node. This function is meant to be
5517 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5518
5519static tree
c877974e
APB
5520resolve_and_layout (something, cl)
5521 tree something;
e04a16fb
AG
5522 tree cl;
5523{
c877974e
APB
5524 tree decl;
5525
23a79c61
APB
5526 /* Don't do that on the current class */
5527 if (something == current_class)
5528 return TYPE_NAME (current_class);
c877974e 5529
23a79c61 5530 /* Don't do anything for void and other primitive types */
c877974e
APB
5531 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5532 return NULL_TREE;
5533
23a79c61
APB
5534 /* Pointer types can be reall pointer types or fake pointers. When
5535 finding a real pointer, recheck for primitive types */
5536 if (TREE_CODE (something) == POINTER_TYPE)
5537 {
5538 if (TREE_TYPE (something))
5539 {
5540 something = TREE_TYPE (something);
5541 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5542 return NULL_TREE;
5543 }
5544 else
5545 something = TYPE_NAME (something);
5546 }
5547
5548 /* Don't do anything for arrays of primitive types */
5549 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5550 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5551 return NULL_TREE;
5552
c2952b01
APB
5553 /* Something might be a WFL */
5554 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5555 something = EXPR_WFL_NODE (something);
5556
5557 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5558 TYPE_DECL or a real TYPE */
5559 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5560 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5561 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5562
23a79c61
APB
5563 if (!(decl = resolve_no_layout (something, cl)))
5564 return NULL_TREE;
5565
5566 /* Resolve and layout if necessary */
5567 layout_class_methods (TREE_TYPE (decl));
7705e9db
APB
5568 /* Check methods, but only once */
5569 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5570 && !CLASS_LOADED_P (TREE_TYPE (decl)))
23a79c61
APB
5571 CHECK_METHODS (decl);
5572 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
e04a16fb 5573 safe_layout_class (TREE_TYPE (decl));
23a79c61 5574
e04a16fb
AG
5575 return decl;
5576}
5577
5578/* Resolve a class, returns its decl but doesn't perform any
5579 layout. The current parsing context is saved and restored */
5580
5581static tree
5582resolve_no_layout (name, cl)
5583 tree name, cl;
5584{
5585 tree ptr, decl;
5586 BUILD_PTR_FROM_NAME (ptr, name);
5587 java_parser_context_save_global ();
c2952b01 5588 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5589 java_parser_context_restore_global ();
5590
5591 return decl;
5592}
5593
23a79c61
APB
5594/* Called when reporting errors. Skip leader '[' in a complex array
5595 type description that failed to be resolved. */
e04a16fb 5596
49f48c71 5597static const char *
e04a16fb 5598purify_type_name (name)
49f48c71 5599 const char *name;
e04a16fb
AG
5600{
5601 while (*name && *name == '[')
5602 name++;
5603 return name;
5604}
5605
5606/* The type CURRENT refers to can't be found. We print error messages. */
5607
5608static void
5609complete_class_report_errors (dep)
5610 jdep *dep;
5611{
49f48c71 5612 const char *name;
23a79c61
APB
5613
5614 if (!JDEP_WFL (dep))
5615 return;
5616
5617 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5618 switch (JDEP_KIND (dep))
5619 {
5620 case JDEP_SUPER:
5621 parse_error_context
5622 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5623 purify_type_name (name),
e04a16fb
AG
5624 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5625 break;
5626 case JDEP_FIELD:
5627 parse_error_context
5628 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5629 purify_type_name (name),
e04a16fb
AG
5630 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5631 break;
5632 case JDEP_METHOD: /* Covers arguments */
5633 parse_error_context
781b0558 5634 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5635 purify_type_name (name),
e04a16fb
AG
5636 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5637 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5638 break;
5639 case JDEP_METHOD_RETURN: /* Covers return type */
5640 parse_error_context
781b0558 5641 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5642 purify_type_name (name),
e04a16fb
AG
5643 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5644 break;
5645 case JDEP_INTERFACE:
5646 parse_error_context
5647 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5648 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5649 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5650 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5651 break;
5652 case JDEP_VARIABLE:
5653 parse_error_context
781b0558 5654 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5655 purify_type_name (IDENTIFIER_POINTER
5656 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5657 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5658 break;
b9f7e36c
APB
5659 case JDEP_EXCEPTION: /* As specified by `throws' */
5660 parse_error_context
5661 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5662 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5663 break;
0a2138e2
APB
5664 default:
5665 /* Fix for -Wall. Just break doing nothing. The error will be
5666 caught later */
5667 break;
e04a16fb
AG
5668 }
5669}
5670
22eed1e6
APB
5671/* Return a static string containing the DECL prototype string. If
5672 DECL is a constructor, use the class name instead of the form
5673 <init> */
5674
49f48c71 5675static const char *
22eed1e6
APB
5676get_printable_method_name (decl)
5677 tree decl;
5678{
49f48c71 5679 const char *to_return;
9ee9b555 5680 tree name = NULL_TREE;
22eed1e6
APB
5681
5682 if (DECL_CONSTRUCTOR_P (decl))
5683 {
5684 name = DECL_NAME (decl);
5e942c50 5685 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5686 }
5687
5688 to_return = lang_printable_name (decl, 0);
5689 if (DECL_CONSTRUCTOR_P (decl))
5690 DECL_NAME (decl) = name;
5691
5692 return to_return;
5693}
5694
5e942c50
APB
5695/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5696 nevertheless needs to be verfied, 1 otherwise. */
5697
5698static int
5699reset_method_name (method)
5700 tree method;
5701{
c2952b01 5702 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5703 {
5704 /* NAME is just the plain name when Object is being defined */
5705 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5706 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5707 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5708 return 0;
5709 }
5710 else
5711 return 1;
5712}
5713
c877974e
APB
5714/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5715
5716tree
5717java_get_real_method_name (method_decl)
5718 tree method_decl;
5719{
5720 tree method_name = DECL_NAME (method_decl);
5721 if (DECL_CONSTRUCTOR_P (method_decl))
5722 return init_identifier_node;
82371d41
APB
5723
5724 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5725 and still can be a constructor. FIXME */
5726
23a79c61
APB
5727 /* Don't confuse method only bearing the name of their class as
5728 constructors */
82371d41
APB
5729 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5730 && ctxp
c2952b01 5731 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5732 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5733 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5734 return init_identifier_node;
5735 else
5736 return EXPR_WFL_NODE (method_name);
5737}
5738
22eed1e6
APB
5739/* Track method being redefined inside the same class. As a side
5740 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5741 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5742
e04a16fb
AG
5743static int
5744check_method_redefinition (class, method)
5745 tree class, method;
5746{
5747 tree redef, name;
5748 tree cl = DECL_NAME (method);
c3f2a476 5749 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
5750 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5751 fixed and checked */
5e942c50
APB
5752
5753 /* Reset the method name before running the check. If it returns 1,
5754 the method doesn't need to be verified with respect to method
5755 redeclaration and we return 0 */
5756 if (reset_method_name (method))
e04a16fb 5757 return 0;
5e942c50
APB
5758
5759 name = DECL_NAME (method);
e04a16fb
AG
5760 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5761 {
c3f2a476 5762 if (redef == method)
e04a16fb 5763 break;
c3f2a476
APB
5764 if (DECL_NAME (redef) == name
5765 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5766 {
22eed1e6
APB
5767 parse_error_context
5768 (cl, "Duplicate %s declaration `%s'",
5769 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5770 get_printable_method_name (redef));
e04a16fb
AG
5771 return 1;
5772 }
5773 }
5774 return 0;
5775}
5776
d77613be
APB
5777static void
5778check_abstract_method_definitions (do_interface, class_decl, type)
5779 int do_interface;
5780 tree class_decl, type;
5781{
5782 tree class = TREE_TYPE (class_decl);
5783 tree method, end_type;
5784
5785 end_type = (do_interface ? object_type_node : type);
5786 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5787 {
5788 tree other_super, other_method, method_sig, method_name;
5789 int found = 0;
165f37bc 5790 int end_type_reached = 0;
d77613be
APB
5791
5792 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5793 continue;
5794
5795 /* Now verify that somewhere in between TYPE and CLASS,
5796 abstract method METHOD gets a non abstract definition
5797 that is inherited by CLASS. */
5798
5799 method_sig = build_java_signature (TREE_TYPE (method));
5800 method_name = DECL_NAME (method);
5801 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5802 method_name = EXPR_WFL_NODE (method_name);
5803
165f37bc
APB
5804 other_super = class;
5805 do {
5806 if (other_super == end_type)
5807 end_type_reached = 1;
5808
5809 /* Method search */
5810 for (other_method = TYPE_METHODS (other_super); other_method;
5811 other_method = TREE_CHAIN (other_method))
5812 {
5813 tree s = build_java_signature (TREE_TYPE (other_method));
5814 tree other_name = DECL_NAME (other_method);
5815
5816 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5817 other_name = EXPR_WFL_NODE (other_name);
5818 if (!DECL_CLINIT_P (other_method)
5819 && !DECL_CONSTRUCTOR_P (other_method)
5820 && method_name == other_name && method_sig == s)
5821 {
5822 found = 1;
5823 break;
5824 }
5825 }
5826 other_super = CLASSTYPE_SUPER (other_super);
5827 } while (!end_type_reached);
5828
d77613be
APB
5829 /* Report that abstract METHOD didn't find an implementation
5830 that CLASS can use. */
5831 if (!found)
5832 {
c2e3db92 5833 char *t = xstrdup (lang_printable_name
d77613be
APB
5834 (TREE_TYPE (TREE_TYPE (method)), 0));
5835 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5836 tree saved_wfl = NULL_TREE;
5837
5838 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5839 {
5840 saved_wfl = DECL_NAME (method);
5841 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5842 }
5843
5844 parse_error_context
5845 (lookup_cl (class_decl),
781b0558 5846 "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
5847 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5848 t, lang_printable_name (method, 0),
5849 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5850 "interface" : "class"),
5851 IDENTIFIER_POINTER (ccn),
5852 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5853 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5854
5855 free (t);
5856
5857 if (saved_wfl)
5858 DECL_NAME (method) = saved_wfl;
5859 }
5860 }
5861}
5862
614eaae0 5863/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5864 methods. */
5865
5866static void
5867java_check_abstract_method_definitions (class_decl)
5868 tree class_decl;
5869{
5870 tree class = TREE_TYPE (class_decl);
5871 tree super, vector;
5872 int i;
5873
5874 if (CLASS_ABSTRACT (class_decl))
5875 return;
5876
5877 /* Check for inherited types */
165f37bc
APB
5878 super = class;
5879 do {
5880 super = CLASSTYPE_SUPER (super);
5881 check_abstract_method_definitions (0, class_decl, super);
5882 } while (super != object_type_node);
d77613be
APB
5883
5884 /* Check for implemented interfaces. */
5885 vector = TYPE_BINFO_BASETYPES (class);
5886 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5887 {
5888 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5889 check_abstract_method_definitions (1, class_decl, super);
5890 }
5891}
5892
165f37bc
APB
5893/* Check all the types method DECL uses and return 1 if all of them
5894 are now complete, 0 otherwise. This is used to check whether its
5895 safe to build a method signature or not. */
5896
5897static int
5898check_method_types_complete (decl)
5899 tree decl;
5900{
5901 tree type = TREE_TYPE (decl);
5902 tree args;
5903
5904 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5905 return 0;
5906
5907 args = TYPE_ARG_TYPES (type);
5908 if (TREE_CODE (type) == METHOD_TYPE)
5909 args = TREE_CHAIN (args);
5910 for (; args != end_params_node; args = TREE_CHAIN (args))
5911 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5912 return 0;
5913
5914 return 1;
5915}
5916
d77613be
APB
5917/* Check all the methods of CLASS_DECL. Methods are first completed
5918 then checked according to regular method existance rules. If no
5919 constructor for CLASS_DECL were encountered, then build its
5920 declaration. */
e04a16fb
AG
5921
5922static void
5923java_check_regular_methods (class_decl)
5924 tree class_decl;
5925{
c2952b01 5926 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
5927 tree method;
5928 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 5929 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
5930 tree mthrows;
5931
5932 /* It is not necessary to check methods defined in java.lang.Object */
5933 if (class == object_type_node)
5934 return;
e04a16fb 5935
23a79c61
APB
5936 if (!TYPE_NVIRTUALS (class))
5937 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
5938
5939 /* Should take interfaces into account. FIXME */
5940 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5941 {
5e942c50 5942 tree sig;
e04a16fb
AG
5943 tree method_wfl = DECL_NAME (method);
5944 int aflags;
5945
5e942c50
APB
5946 /* If we previously found something and its name was saved,
5947 reinstall it now */
5948 if (found && saved_found_wfl)
ba179f9f
APB
5949 {
5950 DECL_NAME (found) = saved_found_wfl;
5951 saved_found_wfl = NULL_TREE;
5952 }
5e942c50 5953
e04a16fb
AG
5954 /* Check for redefinitions */
5955 if (check_method_redefinition (class, method))
5956 continue;
5957
22eed1e6
APB
5958 /* If we see one constructor a mark so we don't generate the
5959 default one. Also skip other verifications: constructors
5960 can't be inherited hence hiden or overriden */
5961 if (DECL_CONSTRUCTOR_P (method))
5962 {
5963 saw_constructor = 1;
5964 continue;
5965 }
5966
c877974e
APB
5967 /* We verify things thrown by the method. They must inherits from
5968 java.lang.Throwable */
5969 for (mthrows = DECL_FUNCTION_THROWS (method);
5970 mthrows; mthrows = TREE_CHAIN (mthrows))
5971 {
5972 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
5973 parse_error_context
781b0558 5974 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
5975 IDENTIFIER_POINTER
5976 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
5977 }
5978
e04a16fb 5979 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 5980 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 5981
c2952b01
APB
5982 /* Inner class can't declare static methods */
5983 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
5984 {
5985 char *t = xstrdup (lang_printable_name (class, 0));
5986 parse_error_context
5987 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
5988 lang_printable_name (method, 0), t);
5989 free (t);
5990 }
5991
5e942c50 5992 /* Nothing overrides or it's a private method. */
aabd7048 5993 if (!found)
5e942c50 5994 continue;
aabd7048
PB
5995 if (METHOD_PRIVATE (found))
5996 {
5997 found = NULL_TREE;
5998 continue;
5999 }
5e942c50
APB
6000
6001 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6002 We set it temporarily for the sake of the error report. */
6003 saved_found_wfl = DECL_NAME (found);
6004 reset_method_name (found);
6005
614eaae0
APB
6006 /* If `found' is declared in an interface, make sure the
6007 modifier matches. */
6008 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6009 && clinit_identifier_node != DECL_NAME (found)
6010 && !METHOD_PUBLIC (method))
6011 {
6012 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6013 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6014 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6015 lang_printable_name (method, 0),
6016 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6017 }
6018
e04a16fb
AG
6019 /* Can't override a method with the same name and different return
6020 types. */
6021 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6022 {
614eaae0
APB
6023 char *t = xstrdup
6024 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6025 parse_error_context
7f10c2e2 6026 (method_wfl,
b9f7e36c 6027 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6028 lang_printable_name (found, 0), t,
b9f7e36c
APB
6029 IDENTIFIER_POINTER
6030 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6031 free (t);
6032 }
e04a16fb 6033
7f10c2e2
APB
6034 aflags = get_access_flags_from_decl (found);
6035 /* If the method has default, access in an other package, then
6036 issue a warning that the current method doesn't override the
6037 one that was found elsewhere. Do not issue this warning when
6038 the match was found in java.lang.Object. */
6039 if (DECL_CONTEXT (found) != object_type_node
a003f638 6040 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6041 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6042 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6043 && flag_not_overriding)
6044 {
6045 parse_warning_context
781b0558 6046 (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
6047 lang_printable_name (found, 0),
6048 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6049 IDENTIFIER_POINTER (DECL_NAME
6050 (TYPE_NAME (DECL_CONTEXT (found)))));
6051 continue;
6052 }
6053
e04a16fb
AG
6054 /* Can't override final. Can't override static. */
6055 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6056 {
6057 /* Static *can* override static */
6058 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6059 continue;
6060 parse_error_context
6061 (method_wfl,
6062 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6063 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6064 lang_printable_name (found, 0),
e04a16fb
AG
6065 (METHOD_FINAL (found) ? "final" : "static"),
6066 IDENTIFIER_POINTER
6067 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6068 continue;
6069 }
7f10c2e2 6070
e04a16fb
AG
6071 /* Static method can't override instance method. */
6072 if (METHOD_STATIC (method))
6073 {
6074 parse_error_context
6075 (method_wfl,
781b0558 6076 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6077 lang_printable_name (found, 0),
e04a16fb
AG
6078 IDENTIFIER_POINTER
6079 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6080 continue;
6081 }
5e942c50 6082
5e942c50
APB
6083 /* - Overriding/hiding public must be public
6084 - Overriding/hiding protected must be protected or public
6085 - If the overriden or hidden method has default (package)
6086 access, then the overriding or hiding method must not be
614eaae0
APB
6087 private; otherwise, a compile-time error occurs. If
6088 `found' belongs to an interface, things have been already
6089 taken care of. */
6090 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6091 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6092 || (METHOD_PROTECTED (found)
6093 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6094 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6095 && METHOD_PRIVATE (method))))
e04a16fb
AG
6096 {
6097 parse_error_context
6098 (method_wfl,
781b0558 6099 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6100 (METHOD_PUBLIC (method) ? "public" :
6101 (METHOD_PRIVATE (method) ? "private" : "protected")),
6102 IDENTIFIER_POINTER (DECL_NAME
6103 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6104 continue;
6105 }
6106
b9f7e36c
APB
6107 /* Overriding methods must have compatible `throws' clauses on checked
6108 exceptions, if any */
6109 check_throws_clauses (method, method_wfl, found);
6110
e04a16fb
AG
6111 /* Inheriting multiple methods with the same signature. FIXME */
6112 }
6113
5e942c50
APB
6114 /* Don't forget eventual pending found and saved_found_wfl. Take
6115 into account that we might have exited because we saw an
d77613be 6116 artificial method as the last entry. */
5e942c50
APB
6117
6118 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6119 DECL_NAME (found) = saved_found_wfl;
6120
23a79c61
APB
6121 if (!TYPE_NVIRTUALS (class))
6122 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6123
d77613be
APB
6124 /* Search for inherited abstract method not yet implemented in this
6125 class. */
6126 java_check_abstract_method_definitions (class_decl);
6127
22eed1e6 6128 if (!saw_constructor)
e920ebc9 6129 fatal ("No constructor found");
e04a16fb
AG
6130}
6131
b9f7e36c
APB
6132/* Return a non zero value if the `throws' clause of METHOD (if any)
6133 is incompatible with the `throws' clause of FOUND (if any). */
6134
6135static void
6136check_throws_clauses (method, method_wfl, found)
6137 tree method, method_wfl, found;
6138{
6139 tree mthrows, fthrows;
6140
c877974e
APB
6141 /* Can't check these things with class loaded from bytecode. FIXME */
6142 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6143 return;
6144
b9f7e36c
APB
6145 for (mthrows = DECL_FUNCTION_THROWS (method);
6146 mthrows; mthrows = TREE_CHAIN (mthrows))
6147 {
6148 /* We don't verify unchecked expressions */
c877974e 6149 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6150 continue;
6151 /* Checked expression must be compatible */
6152 for (fthrows = DECL_FUNCTION_THROWS (found);
6153 fthrows; fthrows = TREE_CHAIN (fthrows))
6154 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6155 break;
6156 if (!fthrows)
6157 {
6158 parse_error_context
781b0558 6159 (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 6160 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6161 lang_printable_name (found, 0),
b9f7e36c
APB
6162 IDENTIFIER_POINTER
6163 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6164 }
6165 }
6166}
6167
e04a16fb
AG
6168/* Check abstract method of interface INTERFACE */
6169
6170static void
5e942c50
APB
6171java_check_abstract_methods (interface_decl)
6172 tree interface_decl;
e04a16fb
AG
6173{
6174 int i, n;
6175 tree method, basetype_vec, found;
5e942c50 6176 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6177
6178 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6179 {
b9f7e36c 6180 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6181
6182 /* 2- Check for double definition inside the defining interface */
6183 if (check_method_redefinition (interface, method))
6184 continue;
6185
6186 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6187 the thrown exceptions (FIXME) */
e04a16fb
AG
6188 found = lookup_java_interface_method2 (interface, method);
6189 if (found)
6190 {
5e942c50
APB
6191 char *t;
6192 tree saved_found_wfl = DECL_NAME (found);
6193 reset_method_name (found);
c2e3db92 6194 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6195 parse_error_context
b9f7e36c 6196 (method_wfl,
5e942c50 6197 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6198 lang_printable_name (found, 0), t,
b9f7e36c
APB
6199 IDENTIFIER_POINTER
6200 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6201 free (t);
5e942c50 6202 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6203 continue;
e04a16fb
AG
6204 }
6205 }
6206
6207 /* 4- Inherited methods can't differ by their returned types */
6208 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6209 return;
6210 n = TREE_VEC_LENGTH (basetype_vec);
6211 for (i = 0; i < n; i++)
6212 {
6213 tree sub_interface_method, sub_interface;
6214 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6215 if (!vec_elt)
6216 continue;
6217 sub_interface = BINFO_TYPE (vec_elt);
6218 for (sub_interface_method = TYPE_METHODS (sub_interface);
6219 sub_interface_method;
6220 sub_interface_method = TREE_CHAIN (sub_interface_method))
6221 {
6222 found = lookup_java_interface_method2 (interface,
6223 sub_interface_method);
6224 if (found && (found != sub_interface_method))
5e942c50
APB
6225 {
6226 tree saved_found_wfl = DECL_NAME (found);
6227 reset_method_name (found);
6228 parse_error_context
6229 (lookup_cl (sub_interface_method),
781b0558 6230 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6231 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6232 lang_printable_name (found, 0),
6233 IDENTIFIER_POINTER
6234 (DECL_NAME (TYPE_NAME
6235 (DECL_CONTEXT (sub_interface_method)))),
6236 IDENTIFIER_POINTER
6237 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6238 DECL_NAME (found) = saved_found_wfl;
6239 }
e04a16fb
AG
6240 }
6241 }
6242}
6243
e04a16fb
AG
6244/* Lookup methods in interfaces using their name and partial
6245 signature. Return a matching method only if their types differ. */
6246
6247static tree
6248lookup_java_interface_method2 (class, method_decl)
6249 tree class, method_decl;
6250{
6251 int i, n;
6252 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6253
6254 if (!basetype_vec)
6255 return NULL_TREE;
6256
6257 n = TREE_VEC_LENGTH (basetype_vec);
6258 for (i = 0; i < n; i++)
6259 {
6260 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6261 if ((BINFO_TYPE (vec_elt) != object_type_node)
6262 && (to_return =
6263 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6264 return to_return;
6265 }
6266 for (i = 0; i < n; i++)
6267 {
6268 to_return = lookup_java_interface_method2
6269 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6270 if (to_return)
6271 return to_return;
6272 }
6273
6274 return NULL_TREE;
6275}
6276
6277/* Lookup method using their name and partial signature. Return a
6278 matching method only if their types differ. */
6279
6280static tree
6281lookup_java_method2 (clas, method_decl, do_interface)
6282 tree clas, method_decl;
6283 int do_interface;
6284{
5e942c50
APB
6285 tree method, method_signature, method_name, method_type, name;
6286
e04a16fb 6287 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6288 name = DECL_NAME (method_decl);
6289 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6290 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6291 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6292
6293 while (clas != NULL_TREE)
6294 {
6295 for (method = TYPE_METHODS (clas);
6296 method != NULL_TREE; method = TREE_CHAIN (method))
6297 {
6298 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6299 tree name = DECL_NAME (method);
6300 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6301 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6302 && method_sig == method_signature
6303 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6304 return method;
e04a16fb
AG
6305 }
6306 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6307 }
6308 return NULL_TREE;
6309}
6310
f441f671
APB
6311/* Return the line that matches DECL line number, and try its best to
6312 position the column number. Used during error reports. */
e04a16fb
AG
6313
6314static tree
6315lookup_cl (decl)
6316 tree decl;
6317{
6318 static tree cl = NULL_TREE;
f441f671 6319 char *line, *found;
e04a16fb
AG
6320
6321 if (!decl)
6322 return NULL_TREE;
6323
6324 if (cl == NULL_TREE)
6325 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6326
6327 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6328 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6329
f441f671
APB
6330 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6331 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6332
6333 found = strstr ((const char *)line,
6334 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6335 if (found)
6336 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6337
e04a16fb
AG
6338 return cl;
6339}
6340
6341/* Look for a simple name in the single-type import list */
6342
6343static tree
6344find_name_in_single_imports (name)
6345 tree name;
6346{
6347 tree node;
6348
6349 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6350 if (TREE_VALUE (node) == name)
6351 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6352
6353 return NULL_TREE;
6354}
6355
6356/* Process all single-type import. */
6357
6358static int
6359process_imports ()
6360{
6361 tree import;
6362 int error_found;
6363
6364 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6365 {
6366 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6367
6368 /* Don't load twice something already defined. */
6369 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6370 continue;
6371 QUALIFIED_P (to_be_found) = 1;
6372 load_class (to_be_found, 0);
6373 error_found =
6374 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6375 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6376 {
6377 parse_error_context (TREE_PURPOSE (import),
6378 "Class or interface `%s' not found in import",
6379 IDENTIFIER_POINTER (to_be_found));
6380 return 1;
6381 }
6382 if (error_found)
6383 return 1;
6384 }
6385 return 0;
6386}
6387
6388/* Possibly find a class imported by a single-type import statement. Return
6389 1 if an error occured, 0 otherwise. */
6390
6391static int
6392find_in_imports (class_type)
6393 tree class_type;
6394{
6395 tree import;
6396
6397 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6398 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6399 {
6400 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6401 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb
AG
6402 }
6403 return 0;
6404}
6405
e04a16fb 6406static int
63a212ed 6407note_possible_classname (name, len)
49f48c71 6408 const char *name;
63a212ed 6409 int len;
e04a16fb 6410{
63a212ed
PB
6411 tree node;
6412 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6413 len = len - 5;
6414 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6415 len = len - 6;
e04a16fb 6416 else
63a212ed
PB
6417 return 0;
6418 node = ident_subst (name, len, "", '/', '.', "");
6419 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6420 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6421 return 1;
e04a16fb
AG
6422}
6423
6424/* Read a import directory, gathering potential match for further type
6425 references. Indifferently reads a filesystem or a ZIP archive
6426 directory. */
6427
6428static void
6429read_import_dir (wfl)
6430 tree wfl;
6431{
63a212ed 6432 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6433 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6434 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6435 DIR *dirp = NULL;
d8fccff5 6436 JCF *saved_jcf = current_jcf;
e04a16fb 6437
63a212ed
PB
6438 int found = 0;
6439 int k;
6440 void *entry;
6441 struct buffer filename[1];
6442
6443
6444 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6445 return;
6446 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6447
6448 BUFFER_INIT (filename);
6449 buffer_grow (filename, package_length + 100);
6450
6451 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6452 {
49f48c71 6453 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6454 int entry_length = strlen (entry_name);
6455 if (jcf_path_is_zipfile (entry))
6456 {
6457 ZipFile *zipf;
6458 buffer_grow (filename, entry_length);
6459 memcpy (filename->data, entry_name, entry_length - 1);
6460 filename->data[entry_length-1] = '\0';
6461 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6462 if (zipf == NULL)
6463 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6464 else
6465 {
6466 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6467 BUFFER_RESET (filename);
6468 for (k = 0; k < package_length; k++)
6469 {
6470 char ch = package_name[k];
6471 *filename->ptr++ = ch == '.' ? '/' : ch;
6472 }
6473 *filename->ptr++ = '/';
6474
345137c7 6475 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6476 {
49f48c71 6477 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6478 int current_entry_len = zipd->filename_length;
6479
345137c7
TT
6480 if (current_entry_len >= BUFFER_LENGTH (filename)
6481 && strncmp (filename->data, current_entry,
6482 BUFFER_LENGTH (filename)) != 0)
63a212ed 6483 continue;
345137c7 6484 found |= note_possible_classname (current_entry,
63a212ed
PB
6485 current_entry_len);
6486 }
6487 }
6488 }
6489 else
6490 {
6491 BUFFER_RESET (filename);
6492 buffer_grow (filename, entry_length + package_length + 4);
6493 strcpy (filename->data, entry_name);
6494 filename->ptr = filename->data + entry_length;
6495 for (k = 0; k < package_length; k++)
6496 {
6497 char ch = package_name[k];
6498 *filename->ptr++ = ch == '.' ? '/' : ch;
6499 }
6500 *filename->ptr = '\0';
6501
6502 dirp = opendir (filename->data);
6503 if (dirp == NULL)
6504 continue;
6505 *filename->ptr++ = '/';
6506 for (;;)
6507 {
63a212ed 6508 int len;
49f48c71 6509 const char *d_name;
63a212ed
PB
6510 struct dirent *direntp = readdir (dirp);
6511 if (!direntp)
6512 break;
6513 d_name = direntp->d_name;
6514 len = strlen (direntp->d_name);
6515 buffer_grow (filename, len+1);
6516 strcpy (filename->ptr, d_name);
345137c7 6517 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6518 package_length+len+1);
6519 }
6520 if (dirp)
6521 closedir (dirp);
6522 }
6523 }
e04a16fb 6524
63a212ed 6525 free (filename->data);
e04a16fb 6526
63a212ed
PB
6527 /* Here we should have a unified way of retrieving an entry, to be
6528 indexed. */
6529 if (!found)
e04a16fb
AG
6530 {
6531 static int first = 1;
6532 if (first)
6533 {
781b0558 6534 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6535 java_error_count++;
6536 first = 0;
6537 }
6538 else
63a212ed
PB
6539 parse_error_context (wfl, "Package `%s' not found in import",
6540 package_name);
e04a16fb
AG
6541 current_jcf = saved_jcf;
6542 return;
6543 }
e04a16fb
AG
6544 current_jcf = saved_jcf;
6545}
6546
6547/* Possibly find a type in the import on demands specified
6548 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6549 entire list, to detected potential double definitions. */
6550
6551static int
6552find_in_imports_on_demand (class_type)
6553 tree class_type;
6554{
ab3a6dd6 6555 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6556 int seen_once = -1;
ab3a6dd6 6557 tree cl = NULL_TREE;
e04a16fb
AG
6558
6559 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6560 {
49f48c71 6561 const char *id_name;
e04a16fb
AG
6562 obstack_grow (&temporary_obstack,
6563 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6564 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6565 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6566 obstack_grow0 (&temporary_obstack,
6567 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6568 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6569 id_name = obstack_finish (&temporary_obstack);
6570
6571 node = maybe_get_identifier (id_name);
6572 if (node && IS_A_CLASSFILE_NAME (node))
6573 {
6574 if (seen_once < 0)
6575 {
6576 cl = TREE_PURPOSE (import);
6577 seen_once = 1;
6578 node_to_use = node;
6579 }
6580 else
6581 {
6582 seen_once++;
6583 parse_error_context
6584 (import, "Type `%s' also potentially defined in package `%s'",
6585 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6586 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6587 }
6588 }
6589 }
6590
6591 if (seen_once == 1)
6592 {
6593 /* Setup lineno so that it refers to the line of the import (in
6594 case we parse a class file and encounter errors */
6595 tree decl;
6596 int saved_lineno = lineno;
6597 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6598 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6599 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6600 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6601 /* If there is no DECL set for the class or if the class isn't
6602 loaded and not seen in source yet, the load */
6603 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6604 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6605 load_class (node_to_use, 0);
6606 lineno = saved_lineno;
6607 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6608 }
6609 else
6610 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6611}
6612
5e942c50
APB
6613static tree
6614resolve_package (pkg, next)
6615 tree pkg, *next;
6616{
c2952b01 6617 tree current, acc;
5e942c50 6618 tree type_name = NULL_TREE;
49f48c71 6619 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6620
6621 /* The trick is to determine when the package name stops and were
6622 the name of something contained in the package starts. Then we
6623 return a fully qualified name of what we want to get. */
6624
6625 /* Do a quick search on well known package names */
6626 if (!strncmp (name, "java.lang.reflect", 17))
6627 {
6628 *next =
6629 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6630 type_name = lookup_package_type (name, 17);
6631 }
6632 else if (!strncmp (name, "java.lang", 9))
6633 {
6634 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6635 type_name = lookup_package_type (name, 9);
6636 }
5e942c50 6637
2c56429a
APB
6638 /* If we found something here, return */
6639 if (type_name)
6640 return type_name;
6641
6642 *next = EXPR_WFL_QUALIFICATION (pkg);
6643
6644 /* Try the current package. */
6645 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6646 IDENTIFIER_LENGTH (ctxp->package)))
6647 {
6648 type_name =
6649 lookup_package_type_and_set_next (name,
6650 IDENTIFIER_LENGTH (ctxp->package),
6651 next );
6652 if (type_name)
6653 return type_name;
6654 }
6655
6656 /* Search in imported package */
6657 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6658 {
6659 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6660 int len = IDENTIFIER_LENGTH (current_pkg_name);
6661 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6662 {
6663 tree left, dummy;
6664
6665 breakdown_qualified (&left, &dummy, current_pkg_name);
6666 len = IDENTIFIER_LENGTH (left);
6667 type_name = lookup_package_type_and_set_next (name, len, next);
6668 if (type_name)
6669 break;
6670 }
6671 }
6672
c2952b01
APB
6673 /* Try to progressively construct a type name */
6674 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6675 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6676 current; current = TREE_CHAIN (current))
6677 {
6678 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6679 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6680 {
6681 type_name = acc;
6b48deee
APB
6682 /* resolve_package should be used in a loop, hence we
6683 point at this one to naturally process the next one at
6684 the next iteration. */
6685 *next = current;
c2952b01
APB
6686 break;
6687 }
6688 }
2c56429a
APB
6689 return type_name;
6690}
6691
6692static tree
6693lookup_package_type_and_set_next (name, len, next)
49f48c71 6694 const char *name;
2c56429a
APB
6695 int len;
6696 tree *next;
6697{
49f48c71 6698 const char *ptr;
2c56429a
APB
6699 tree type_name = lookup_package_type (name, len);
6700
6701 if (!type_name)
6702 return NULL;
6703
6704 ptr = IDENTIFIER_POINTER (type_name);
6705 while (ptr && (ptr = strchr (ptr, '.')))
6706 {
6707 *next = TREE_CHAIN (*next);
6708 ptr++;
6709 }
5e942c50
APB
6710 return type_name;
6711}
6712
6713static tree
6714lookup_package_type (name, from)
49f48c71 6715 const char *name;
5e942c50
APB
6716 int from;
6717{
6718 char subname [128];
49f48c71 6719 const char *sub = &name[from+1];
5e942c50
APB
6720 while (*sub != '.' && *sub)
6721 sub++;
6722 strncpy (subname, name, sub-name);
6723 subname [sub-name] = '\0';
6724 return get_identifier (subname);
6725}
6726
e04a16fb
AG
6727/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6728 access violations were found, 1 otherwise. */
6729
6730static int
6731check_pkg_class_access (class_name, cl)
6732 tree class_name;
6733 tree cl;
6734{
6735 tree type;
e04a16fb
AG
6736
6737 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6738 return 0;
6739
6740 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6741 return 0;
6742
6743 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6744 {
e28cd97b
APB
6745 /* Access to a private class within the same package is
6746 allowed. */
6747 tree l, r;
6748 breakdown_qualified (&l, &r, class_name);
6749 if (l == ctxp->package)
6750 return 0;
6751
e04a16fb 6752 parse_error_context
781b0558 6753 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6754 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6755 IDENTIFIER_POINTER (class_name));
6756 return 1;
6757 }
6758 return 0;
6759}
6760
6761/* Local variable declaration. */
6762
6763static void
6764declare_local_variables (modifier, type, vlist)
6765 int modifier;
6766 tree type;
6767 tree vlist;
6768{
c583dd46
APB
6769 tree decl, current, saved_type;
6770 tree type_wfl = NULL_TREE;
e04a16fb 6771 int must_chain = 0;
c2952b01 6772 int final_p = 0;
e04a16fb 6773
2aa11e97 6774 /* Push a new block if statements were seen between the last time we
e04a16fb 6775 pushed a block and now. Keep a cound of block to close */
f099f336 6776 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6777 {
f099f336 6778 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6779 tree b = enter_block ();
f099f336 6780 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6781 }
6782
6783 if (modifier)
6784 {
6785 int i;
6786 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6787 if (modifier == ACC_FINAL)
c2952b01 6788 final_p = 1;
c877974e
APB
6789 else
6790 {
6791 parse_error_context
6792 (ctxp->modifier_ctx [i],
6793 "Only `final' is allowed as a local variables modifier");
6794 return;
6795 }
e04a16fb
AG
6796 }
6797
c583dd46
APB
6798 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6799 hold the TYPE value if a new incomplete has to be created (as
6800 opposed to being found already existing and reused). */
6801 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6802
6803 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6804 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6805
6806 /* Go through all the declared variables */
6807 for (current = vlist, saved_type = type; current;
6808 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6809 {
c877974e 6810 tree other, real_type;
e04a16fb
AG
6811 tree wfl = TREE_PURPOSE (current);
6812 tree name = EXPR_WFL_NODE (wfl);
6813 tree init = TREE_VALUE (current);
e04a16fb 6814
c583dd46
APB
6815 /* Process NAME, as it may specify extra dimension(s) for it */
6816 type = build_array_from_name (type, type_wfl, name, &name);
6817
6818 /* Variable redefinition check */
6819 if ((other = lookup_name_in_blocks (name)))
6820 {
6821 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6822 DECL_SOURCE_LINE (other));
6823 continue;
6824 }
6825
6826 /* Type adjustment. We may have just readjusted TYPE because
6827 the variable specified more dimensions. Make sure we have
6828 a reference if we can and don't have one already. */
1886c9d8 6829 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6830
6831 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6832 /* Never layout this decl. This will be done when its scope
6833 will be entered */
c877974e 6834 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6835 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6836 BLOCK_CHAIN_DECL (decl);
6837
d4370213
APB
6838 /* If doing xreferencing, replace the line number with the WFL
6839 compound value */
6840 if (flag_emit_xref)
6841 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6842
e04a16fb
AG
6843 /* Don't try to use an INIT statement when an error was found */
6844 if (init && java_error_count)
6845 init = NULL_TREE;
c583dd46
APB
6846
6847 /* Add the initialization function to the current function's code */
6848 if (init)
e04a16fb 6849 {
c583dd46
APB
6850 /* Name might have been readjusted */
6851 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6852 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6853 java_method_add_stmt (current_function_decl,
6854 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6855 init));
6856 }
6857
6858 /* Setup dependency the type of the decl */
6859 if (must_chain)
6860 {
6861 jdep *dep;
6862 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6863 dep = CLASSD_LAST (ctxp->classd_list);
6864 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6865 }
6866 }
6867 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6868}
6869
6870/* Called during parsing. Build decls from argument list. */
6871
6872static void
6873source_start_java_method (fndecl)
6874 tree fndecl;
6875{
6876 tree tem;
6877 tree parm_decl;
6878 int i;
6879
79d13333
APB
6880 if (!fndecl)
6881 return;
6882
e04a16fb
AG
6883 current_function_decl = fndecl;
6884
6885 /* New scope for the function */
6886 enter_block ();
6887 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 6888 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
6889 {
6890 tree type = TREE_VALUE (tem);
6891 tree name = TREE_PURPOSE (tem);
6892
23a79c61
APB
6893 /* If type is incomplete. Create an incomplete decl and ask for
6894 the decl to be patched later */
e04a16fb
AG
6895 if (INCOMPLETE_TYPE_P (type))
6896 {
6897 jdep *jdep;
c877974e
APB
6898 tree real_type = GET_REAL_TYPE (type);
6899 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 6900 type = obtain_incomplete_type (type);
e04a16fb
AG
6901 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6902 jdep = CLASSD_LAST (ctxp->classd_list);
6903 JDEP_MISC (jdep) = name;
6904 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6905 }
6906 else
6907 parm_decl = build_decl (PARM_DECL, name, type);
6908
c2952b01
APB
6909 /* Remember if a local variable was declared final (via its
6910 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6911 if (ARG_FINAL_P (tem))
6912 LOCAL_FINAL (parm_decl) = 1;
6913
e04a16fb
AG
6914 BLOCK_CHAIN_DECL (parm_decl);
6915 }
6916 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6917 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
6918 nreverse (tem);
6919 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 6920 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
6921}
6922
22eed1e6
APB
6923/* Called during parsing. Creates an artificial method declaration. */
6924
6925static tree
6926create_artificial_method (class, flags, type, name, args)
6927 tree class;
6928 int flags;
6929 tree type, name, args;
6930{
22eed1e6
APB
6931 tree mdecl;
6932
c2952b01 6933 java_parser_context_save_global ();
22eed1e6
APB
6934 lineno = 0;
6935 mdecl = make_node (FUNCTION_TYPE);
6936 TREE_TYPE (mdecl) = type;
6937 TYPE_ARG_TYPES (mdecl) = args;
6938 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 6939 java_parser_context_restore_global ();
22eed1e6
APB
6940 DECL_ARTIFICIAL (mdecl) = 1;
6941 return mdecl;
6942}
6943
6944/* Starts the body if an artifical method. */
6945
6946static void
6947start_artificial_method_body (mdecl)
6948 tree mdecl;
6949{
6950 DECL_SOURCE_LINE (mdecl) = 1;
6951 DECL_SOURCE_LINE_MERGE (mdecl, 1);
6952 source_start_java_method (mdecl);
6953 enter_block ();
6954}
6955
6956static void
6957end_artificial_method_body (mdecl)
6958 tree mdecl;
6959{
6960 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
6961 exit_block ();
6962}
6963
e04a16fb
AG
6964/* Called during expansion. Push decls formerly built from argument
6965 list so they're usable during expansion. */
6966
6967static void
6968expand_start_java_method (fndecl)
6969 tree fndecl;
6970{
6971 tree tem, *ptr;
e04a16fb 6972
e04a16fb
AG
6973 current_function_decl = fndecl;
6974
c2952b01
APB
6975 if (! quiet_flag)
6976 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 6977 announce_function (fndecl);
c2952b01
APB
6978 if (! quiet_flag)
6979 fprintf (stderr, "]");
6980
6981 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
6982 ptr = &DECL_ARGUMENTS (fndecl);
6983 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6984 while (tem)
6985 {
6986 tree next = TREE_CHAIN (tem);
b67d701b 6987 tree type = TREE_TYPE (tem);
e438e1b7
JJ
6988 if (PROMOTE_PROTOTYPES
6989 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
6990 && INTEGRAL_TYPE_P (type))
6991 type = integer_type_node;
b67d701b 6992 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
6993 layout_decl (tem, 0);
6994 pushdecl (tem);
e04a16fb
AG
6995 *ptr = tem;
6996 ptr = &TREE_CHAIN (tem);
6997 tem = next;
6998 }
6999 *ptr = NULL_TREE;
7000 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7001 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7002}
7003
7004/* Terminate a function and expand its body. */
7005
7006static void
7007source_end_java_method ()
7008{
7009 tree fndecl = current_function_decl;
138657ec 7010 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7011
79d13333
APB
7012 if (!fndecl)
7013 return;
7014
e04a16fb
AG
7015 java_parser_context_save_global ();
7016 lineno = ctxp->last_ccb_indent1;
7017
b67d701b
PB
7018 /* Set EH language codes */
7019 java_set_exception_lang_code ();
7020
5423609c
APB
7021 /* Turn function bodies with only a NOP expr null, so they don't get
7022 generated at all and we won't get warnings when using the -W
7023 -Wall flags. */
7024 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7025 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7026
e04a16fb
AG
7027 /* Generate function's code */
7028 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7029 && ! flag_emit_class_files
7030 && ! flag_emit_xref)
e04a16fb
AG
7031 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7032
7033 /* pop out of its parameters */
7034 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7035 poplevel (1, 0, 1);
7036 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7037
7038 /* Generate rtl for function exit. */
e8fc7396 7039 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7040 {
7041 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7042 /* Emit catch-finally clauses */
7043 emit_handlers ();
e04a16fb
AG
7044 expand_function_end (input_filename, lineno, 0);
7045
138657ec
AH
7046 /* FIXME: If the current method contains any exception handlers,
7047 force asynchronous_exceptions: this is necessary because signal
7048 handlers in libjava may throw exceptions. This is far from being
7049 a perfect solution, but it's better than doing nothing at all.*/
7050 if (catch_clauses)
7051 asynchronous_exceptions = 1;
7052
e04a16fb
AG
7053 /* Run the optimizers and output assembler code for this function. */
7054 rest_of_compilation (fndecl);
7055 }
7056
7057 current_function_decl = NULL_TREE;
8226320b 7058 permanent_allocation (1);
e04a16fb 7059 java_parser_context_restore_global ();
138657ec 7060 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7061}
7062
7063/* Record EXPR in the current function block. Complements compound
7064 expression second operand if necessary. */
7065
7066tree
7067java_method_add_stmt (fndecl, expr)
7068 tree fndecl, expr;
7069{
b771925e
APB
7070 if (!GET_CURRENT_BLOCK (fndecl))
7071 return NULL_TREE;
f099f336 7072 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7073}
e04a16fb 7074
b67d701b
PB
7075static tree
7076add_stmt_to_block (b, type, stmt)
7077 tree b, type, stmt;
7078{
7079 tree body = BLOCK_EXPR_BODY (b), c;
7080
e04a16fb
AG
7081 if (java_error_count)
7082 return body;
b67d701b
PB
7083
7084 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7085 return body;
7086
b67d701b
PB
7087 BLOCK_EXPR_BODY (b) = c;
7088 TREE_SIDE_EFFECTS (c) = 1;
7089 return c;
e04a16fb
AG
7090}
7091
7092/* Add STMT to EXISTING if possible, otherwise create a new
7093 COMPOUND_EXPR and add STMT to it. */
7094
7095static tree
7096add_stmt_to_compound (existing, type, stmt)
7097 tree existing, type, stmt;
7098{
15fdcfe9
PB
7099 if (existing)
7100 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7101 else
15fdcfe9 7102 return stmt;
e04a16fb
AG
7103}
7104
7105/* Hold THIS for the scope of the current public method decl. */
7106static tree current_this;
7107
1886c9d8
APB
7108void java_layout_seen_class_methods ()
7109{
7110 tree previous_list = all_class_list;
7111 tree end = NULL_TREE;
7112 tree current;
7113
7114 while (1)
7115 {
7116 for (current = previous_list;
7117 current != end; current = TREE_CHAIN (current))
7118 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7119
7120 if (previous_list != all_class_list)
7121 {
7122 end = previous_list;
7123 previous_list = all_class_list;
7124 }
7125 else
7126 break;
7127 }
7128}
7129
e04a16fb 7130void
c2952b01 7131java_reorder_fields ()
e04a16fb 7132{
c2952b01 7133 static tree stop_reordering = NULL_TREE;
23a79c61 7134
c2952b01 7135 tree current;
5e942c50 7136 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7137 {
5e942c50 7138 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7139
c2952b01
APB
7140 if (current_class == stop_reordering)
7141 break;
7142
c877974e
APB
7143 /* Reverse the fields, but leave the dummy field in front.
7144 Fields are already ordered for Object and Class */
7145 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7146 && current_class != class_type_node)
7147 {
23a79c61
APB
7148 /* If the dummy field is there, reverse the right fields and
7149 just layout the type for proper fields offset */
c877974e
APB
7150 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7151 {
7152 tree fields = TYPE_FIELDS (current_class);
7153 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7154 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7155 }
23a79c61
APB
7156 /* We don't have a dummy field, we need to layout the class,
7157 after having reversed the fields */
c877974e
APB
7158 else
7159 {
7160 TYPE_FIELDS (current_class) =
7161 nreverse (TYPE_FIELDS (current_class));
7162 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7163 }
7164 }
c2952b01
APB
7165 }
7166 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7167}
7168
7169/* Layout the methods of all classes loaded in one way on an
7170 other. Check methods of source parsed classes. Then reorder the
7171 fields and layout the classes or the type of all source parsed
7172 classes */
7173
7174void
7175java_layout_classes ()
7176{
7177 tree current;
7178 int save_error_count = java_error_count;
7179
7180 /* Layout the methods of all classes seen so far */
7181 java_layout_seen_class_methods ();
7182 java_parse_abort_on_error ();
7183 all_class_list = NULL_TREE;
7184
7185 /* Then check the methods of all parsed classes */
7186 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7187 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7188 CHECK_METHODS (TREE_VALUE (current));
7189 java_parse_abort_on_error ();
7190
7191 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7192 {
7193 current_class = TREE_TYPE (TREE_VALUE (current));
7194 layout_class (current_class);
5e942c50 7195
c877974e
APB
7196 /* From now on, the class is considered completely loaded */
7197 CLASS_LOADED_P (current_class) = 1;
7198
5e942c50
APB
7199 /* Error reported by the caller */
7200 if (java_error_count)
7201 return;
e04a16fb 7202 }
23a79c61
APB
7203
7204 /* We might have reloaded classes durign the process of laying out
7205 classes for code generation. We must layout the methods of those
7206 late additions, as constructor checks might use them */
1886c9d8 7207 java_layout_seen_class_methods ();
23a79c61 7208 java_parse_abort_on_error ();
e04a16fb
AG
7209}
7210
c2952b01
APB
7211/* Expand methods in the current set of classes rememebered for
7212 generation. */
e04a16fb 7213
49f48c71 7214static void
c2952b01 7215java_complete_expand_classes ()
e04a16fb
AG
7216{
7217 tree current;
ce6e9147
APB
7218
7219 do_not_fold = flag_emit_xref;
c2952b01 7220
e04a16fb 7221 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7222 if (!INNER_CLASS_DECL_P (current))
7223 java_complete_expand_class (current);
7224}
e04a16fb 7225
c2952b01
APB
7226/* Expand the methods found in OUTER, starting first by OUTER's inner
7227 classes, if any. */
e04a16fb 7228
c2952b01
APB
7229static void
7230java_complete_expand_class (outer)
7231 tree outer;
7232{
7233 tree inner_list;
e04a16fb 7234
c2952b01 7235 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7236
c2952b01
APB
7237 /* We need to go after all inner classes and start expanding them,
7238 starting with most nested ones. We have to do that because nested
7239 classes might add functions to outer classes */
e04a16fb 7240
c2952b01
APB
7241 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7242 inner_list; inner_list = TREE_CHAIN (inner_list))
7243 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7244
c2952b01
APB
7245 java_complete_expand_methods (outer);
7246 set_nested_class_simple_name_value (outer, 0); /* Reset */
7247}
7248
7249/* Expand methods registered in CLASS_DECL. The general idea is that
7250 we expand regular methods first. This allows us get an estimate on
7251 how outer context local alias fields are really used so we can add
7252 to the constructor just enough code to initialize them properly (it
7253 also lets us generate $finit$ correctly.) Then we expand the
7254 constructors and then <clinit>. */
7255
7256static void
7257java_complete_expand_methods (class_decl)
7258 tree class_decl;
7259{
7260 tree clinit, finit, decl, first_decl;
7261
7262 current_class = TREE_TYPE (class_decl);
7263
7264 /* Initialize a new constant pool */
7265 init_outgoing_cpool ();
7266
7267 /* Pre-expand <clinit> to figure whether we really need it or
7268 not. If we do need it, we pre-expand the static fields so they're
7269 ready to be used somewhere else. <clinit> will be fully expanded
7270 after we processed the constructors. */
7271 first_decl = TYPE_METHODS (current_class);
7272 clinit = maybe_generate_pre_expand_clinit (current_class);
7273
7274 /* Then generate $finit$ (if we need to) because constructor will
7275 try to use it.*/
7276 if (TYPE_FINIT_STMT_LIST (current_class))
7277 {
7278 finit = generate_finit (current_class);
7279 java_complete_expand_method (finit);
7280 }
7281
7282 /* Now do the constructors */
7283 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7284 {
7285 int no_body;
7286
7287 if (!DECL_CONSTRUCTOR_P (decl))
7288 continue;
7289
7290 no_body = !DECL_FUNCTION_BODY (decl);
7291 /* Don't generate debug info on line zero when expanding a
7292 generated constructor. */
7293 if (no_body)
7294 restore_line_number_status (1);
7295
7296 java_complete_expand_method (decl);
7297
7298 if (no_body)
7299 restore_line_number_status (0);
7300 }
7301
7302 /* First, do the ordinary methods. */
7303 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7304 {
7145d9fe
TT
7305 /* Skip abstract or native methods -- but do handle native
7306 methods when generating JNI stubs. */
7307 if (METHOD_ABSTRACT (decl)
7308 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7309 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7310 continue;
7145d9fe
TT
7311
7312 if (METHOD_NATIVE (decl))
7313 {
7314 tree body = build_jni_stub (decl);
7315 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7316 }
7317
c2952b01
APB
7318 java_complete_expand_method (decl);
7319 }
7320
7321 /* If there is indeed a <clinit>, fully expand it now */
7322 if (clinit)
7323 {
7324 /* Prevent the use of `this' inside <clinit> */
7325 ctxp->explicit_constructor_p = 1;
7326 java_complete_expand_method (clinit);
7327 ctxp->explicit_constructor_p = 0;
e04a16fb 7328 }
c2952b01 7329
165f37bc
APB
7330 /* We might have generated a class$ that we now want to expand */
7331 if (TYPE_DOT_CLASS (current_class))
7332 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7333
c2952b01
APB
7334 /* Now verify constructor circularity (stop after the first one we
7335 prove wrong.) */
7336 if (!CLASS_INTERFACE (class_decl))
7337 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7338 if (DECL_CONSTRUCTOR_P (decl)
7339 && verify_constructor_circularity (decl, decl))
7340 break;
7341
7342 /* Save the constant pool. We'll need to restore it later. */
7343 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7344}
7345
b9f7e36c
APB
7346/* Hold a list of catch clauses list. The first element of this list is
7347 the list of the catch clauses of the currently analysed try block. */
7348static tree currently_caught_type_list;
7349
c2952b01
APB
7350/* Attempt to create <clinit>. Pre-expand static fields so they can be
7351 safely used in some other methods/constructors. */
e920ebc9 7352
c2952b01
APB
7353static tree
7354maybe_generate_pre_expand_clinit (class_type)
7355 tree class_type;
e920ebc9 7356{
c2952b01
APB
7357 tree current, mdecl;
7358
7359 if (!TYPE_CLINIT_STMT_LIST (class_type))
7360 return NULL_TREE;
e920ebc9 7361
c2952b01
APB
7362 /* Go through all static fields and pre expand them */
7363 for (current = TYPE_FIELDS (class_type); current;
7364 current = TREE_CHAIN (current))
7365 if (FIELD_STATIC (current))
7366 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7367
7368 /* Then build the <clinit> method */
7369 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7370 clinit_identifier_node, end_params_node);
7371 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7372 mdecl, NULL_TREE);
7373 start_artificial_method_body (mdecl);
7374
7375 /* We process the list of assignment we produced as the result of
7376 the declaration of initialized static field and add them as
7377 statement to the <clinit> method. */
7378 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7379 current = TREE_CHAIN (current))
e920ebc9 7380 {
c2952b01
APB
7381 /* We build the assignment expression that will initialize the
7382 field to its value. There are strict rules on static
7383 initializers (8.5). FIXME */
7384 tree stmt = build_debugable_stmt (EXPR_WFL_LINECOL (current), current);
7385 java_method_add_stmt (mdecl, stmt);
7386 }
e920ebc9 7387
c2952b01
APB
7388 end_artificial_method_body (mdecl);
7389
92d83515
APB
7390 /* Now we want to place <clinit> as the last method (because we need
7391 it at least for interface so that it doesn't interfere with the
7392 dispatch table based lookup. */
7393 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7394 {
92d83515
APB
7395 current = TREE_CHAIN (TYPE_METHODS (class_type));
7396 TYPE_METHODS (class_type) = current;
c2952b01
APB
7397
7398 while (TREE_CHAIN (current))
7399 current = TREE_CHAIN (current);
92d83515 7400
c2952b01
APB
7401 TREE_CHAIN (current) = mdecl;
7402 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7403 }
c2952b01
APB
7404
7405 return mdecl;
e920ebc9
APB
7406}
7407
92d83515
APB
7408/* See whether we could get rid of <clinit>. Criteria are: all static
7409 final fields have constant initial values and the body of <clinit>
7410 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7411
7412static int
7413maybe_yank_clinit (mdecl)
7414 tree mdecl;
7415{
7416 tree type, current;
7417 tree fbody, bbody;
7418
7419 if (!DECL_CLINIT_P (mdecl))
7420 return 0;
7421
7422 /* If the body isn't empty, then we keep <clinit> */
7423 fbody = DECL_FUNCTION_BODY (mdecl);
7424 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7425 bbody = BLOCK_EXPR_BODY (bbody);
7426 if (bbody && bbody != empty_stmt_node)
7427 return 0;
7428
7429 type = DECL_CONTEXT (mdecl);
7430 current = TYPE_FIELDS (type);
7431
7432 for (current = (current ? TREE_CHAIN (current) : current);
7433 current; current = TREE_CHAIN (current))
7434 if (!(FIELD_STATIC (current) && FIELD_FINAL (current)
7435 && DECL_INITIAL (current) && TREE_CONSTANT (DECL_INITIAL (current))))
7436 break;
7437
7438 if (current)
7439 return 0;
7440
7441 /* Get rid of <clinit> in the class' list of methods */
7442 if (TYPE_METHODS (type) == mdecl)
7443 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7444 else
7445 for (current = TYPE_METHODS (type); current;
7446 current = TREE_CHAIN (current))
7447 if (TREE_CHAIN (current) == mdecl)
7448 {
7449 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7450 break;
7451 }
7452
7453 return 1;
7454}
7455
7456
e04a16fb
AG
7457/* Complete and expand a method. */
7458
7459static void
7460java_complete_expand_method (mdecl)
7461 tree mdecl;
7462{
92d83515
APB
7463 int yank_clinit = 0;
7464
c2952b01 7465 current_function_decl = mdecl;
22eed1e6
APB
7466 /* Fix constructors before expanding them */
7467 if (DECL_CONSTRUCTOR_P (mdecl))
7468 fix_constructors (mdecl);
e04a16fb 7469
22eed1e6 7470 /* Expand functions that have a body */
e04a16fb
AG
7471 if (DECL_FUNCTION_BODY (mdecl))
7472 {
9bbc7d9f
PB
7473 tree fbody = DECL_FUNCTION_BODY (mdecl);
7474 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7475 tree exception_copy = NULL_TREE;
e04a16fb 7476 expand_start_java_method (mdecl);
939d7216 7477 build_result_decl (mdecl);
e04a16fb
AG
7478
7479 current_this
7480 = (!METHOD_STATIC (mdecl) ?
7481 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7482
ce6e9147
APB
7483 /* Purge the `throws' list of unchecked exceptions. If we're
7484 doing xref, save a copy of the list and re-install it
7485 later. */
7486 if (flag_emit_xref)
7487 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7488
b9f7e36c
APB
7489 purge_unchecked_exceptions (mdecl);
7490
7491 /* Install exceptions thrown with `throws' */
7492 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7493
9bbc7d9f 7494 if (block_body != NULL_TREE)
bc3ca41b
PB
7495 {
7496 block_body = java_complete_tree (block_body);
c2952b01 7497
7145d9fe 7498 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7499 check_for_initialization (block_body);
f099f336 7500 ctxp->explicit_constructor_p = 0;
bc3ca41b 7501 }
e803d3b2 7502
9bbc7d9f 7503 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7504
c2952b01
APB
7505 /* If we saw a return but couldn't evaluate it properly, we'll
7506 have an error_mark_node here. */
7507 if (block_body != error_mark_node
7508 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7509 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7510 && !flag_emit_xref)
82371d41 7511 missing_return_error (current_function_decl);
7525cc04 7512
92d83515
APB
7513 /* Check wether we could just get rid of clinit, now the picture
7514 is complete. */
7515 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7516 complete_start_java_method (mdecl);
7517
e04a16fb 7518 /* Don't go any further if we've found error(s) during the
92d83515
APB
7519 expansion */
7520 if (!java_error_count && !yank_clinit)
e04a16fb 7521 source_end_java_method ();
22eed1e6
APB
7522 else
7523 {
92d83515
APB
7524 if (java_error_count)
7525 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7526 poplevel (1, 0, 1);
7527 }
b9f7e36c
APB
7528
7529 /* Pop the exceptions and sanity check */
7530 POP_EXCEPTIONS();
7531 if (currently_caught_type_list)
7532 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7533
7534 if (flag_emit_xref)
7535 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7536 }
7537}
7538
c2952b01
APB
7539\f
7540
7541/* This section of the code deals with accessing enclosing context
7542 fields either directly by using the relevant access to this$<n> or
7543 by invoking an access method crafted for that purpose. */
7544
7545/* Build the necessary access from an inner class to an outer
7546 class. This routine could be optimized to cache previous result
7547 (decl, current_class and returned access). When an access method
7548 needs to be generated, it always takes the form of a read. It might
7549 be later turned into a write by calling outer_field_access_fix. */
7550
7551static tree
7552build_outer_field_access (id, decl)
7553 tree id, decl;
7554{
7555 tree access = NULL_TREE;
7556 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7557
7558 /* If decl's class is the direct outer class of the current_class,
7559 build the access as `this$<n>.<field>'. Not that we will break
7560 the `private' barrier if we're not emitting bytecodes. */
7561 if (ctx == DECL_CONTEXT (decl)
7562 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7563 {
7564 tree thisn = build_current_thisn (current_class);
7565 access = make_qualified_primary (build_wfl_node (thisn),
7566 id, EXPR_WFL_LINECOL (id));
7567 }
7568 /* Otherwise, generate access methods to outer this and access the
7569 field (either using an access method or by direct access.) */
7570 else
7571 {
7572 int lc = EXPR_WFL_LINECOL (id);
7573
7574 /* Now we chain the required number of calls to the access$0 to
7575 get a hold to the enclosing instance we need, and the we
7576 build the field access. */
7577 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7578
7579 /* If the field is private and we're generating bytecode, then
7580 we generate an access method */
7581 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7582 {
7583 tree name = build_outer_field_access_methods (decl);
7584 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7585 name, access, NULL_TREE);
7586 }
7587 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7588 Once again we break the `private' access rule from a foreign
7589 class. */
7590 else
7591 access = make_qualified_primary (access, id, lc);
7592 }
7593 return resolve_expression_name (access, NULL);
7594}
7595
7596/* Return a non zero value if NODE describes an outer field inner
7597 access. */
7598
7599static int
7600outer_field_access_p (type, decl)
7601 tree type, decl;
7602{
7603 if (!INNER_CLASS_TYPE_P (type)
7604 || TREE_CODE (decl) != FIELD_DECL
7605 || DECL_CONTEXT (decl) == type)
7606 return 0;
7607
7608 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7609 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7610 {
7611 if (type == DECL_CONTEXT (decl))
7612 return 1;
7613 if (!DECL_CONTEXT (TYPE_NAME (type)))
7614 break;
7615 }
7616
7617 return 0;
7618}
7619
7620/* Return a non zero value if NODE represents an outer field inner
7621 access that was been already expanded. As a side effect, it returns
7622 the name of the field being accessed and the argument passed to the
7623 access function, suitable for a regeneration of the access method
7624 call if necessary. */
7625
7626static int
7627outer_field_expanded_access_p (node, name, arg_type, arg)
7628 tree node, *name, *arg_type, *arg;
7629{
7630 int identified = 0;
7631
7632 if (TREE_CODE (node) != CALL_EXPR)
7633 return 0;
7634
7635 /* Well, gcj generates slightly different tree nodes when compiling
7636 to native or bytecodes. It's the case for function calls. */
7637
7638 if (flag_emit_class_files
7639 && TREE_CODE (node) == CALL_EXPR
7640 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7641 identified = 1;
7642 else if (!flag_emit_class_files)
7643 {
7644 node = TREE_OPERAND (node, 0);
7645
7646 if (node && TREE_OPERAND (node, 0)
7647 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7648 {
7649 node = TREE_OPERAND (node, 0);
7650 if (TREE_OPERAND (node, 0)
7651 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7652 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7653 (DECL_NAME (TREE_OPERAND (node, 0)))))
7654 identified = 1;
7655 }
7656 }
7657
7658 if (identified && name && arg_type && arg)
7659 {
7660 tree argument = TREE_OPERAND (node, 1);
7661 *name = DECL_NAME (TREE_OPERAND (node, 0));
7662 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7663 *arg = TREE_VALUE (argument);
7664 }
7665 return identified;
7666}
7667
7668/* Detect in NODE an outer field read access from an inner class and
7669 transform it into a write with RHS as an argument. This function is
7670 called from the java_complete_lhs when an assignment to a LHS can
7671 be identified. */
7672
7673static tree
7674outer_field_access_fix (wfl, node, rhs)
7675 tree wfl, node, rhs;
7676{
7677 tree name, arg_type, arg;
7678
7679 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7680 {
7681 /* At any rate, check whether we're trying to assign a value to
7682 a final. */
7683 tree accessed = (JDECL_P (node) ? node :
7684 (TREE_CODE (node) == COMPONENT_REF ?
7685 TREE_OPERAND (node, 1) : node));
7686 if (check_final_assignment (accessed, wfl))
7687 return error_mark_node;
7688
7689 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7690 arg_type, name, arg, rhs);
7691 return java_complete_tree (node);
7692 }
7693 return NULL_TREE;
7694}
7695
7696/* Construct the expression that calls an access method:
7697 <type>.access$<n>(<arg1> [, <arg2>]);
7698
7699 ARG2 can be NULL and will be omitted in that case. It will denote a
7700 read access. */
7701
7702static tree
7703build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7704 int lc;
7705 tree type, access_method_name, arg1, arg2;
7706{
7707 tree args, cn, access;
7708
7709 args = arg1 ? arg1 :
7710 build_wfl_node (build_current_thisn (current_class));
7711 args = build_tree_list (NULL_TREE, args);
7712
7713 if (arg2)
7714 args = tree_cons (NULL_TREE, arg2, args);
7715
7716 access = build_method_invocation (build_wfl_node (access_method_name), args);
7717 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7718 return make_qualified_primary (cn, access, lc);
7719}
7720
7721static tree
7722build_new_access_id ()
7723{
7724 static int access_n_counter = 1;
7725 char buffer [128];
7726
7727 sprintf (buffer, "access$%d", access_n_counter++);
7728 return get_identifier (buffer);
7729}
7730
7731/* Create the static access functions for the outer field DECL. We define a
7732 read:
7733 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7734 return inst$.field;
7735 }
7736 and a write access:
7737 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7738 TREE_TYPE (<field>) value$) {
7739 return inst$.field = value$;
7740 }
7741 We should have a usage flags on the DECL so we can lazily turn the ones
7742 we're using for code generation. FIXME.
7743*/
7744
7745static tree
7746build_outer_field_access_methods (decl)
7747 tree decl;
7748{
7749 tree id, args, stmt, mdecl;
7750
7751 /* Check point, to be removed. FIXME */
7752 if (FIELD_INNER_ACCESS (decl)
7753 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7754 abort ();
7755
7756 if (FIELD_INNER_ACCESS (decl))
7757 return FIELD_INNER_ACCESS (decl);
7758
7759 push_obstacks (&permanent_obstack, &permanent_obstack);
7760
7761 /* Create the identifier and a function named after it. */
7762 id = build_new_access_id ();
7763
7764 /* The identifier is marked as bearing the name of a generated write
7765 access function for outer field accessed from inner classes. */
7766 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7767
7768 /* Create the read access */
7769 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7770 TREE_CHAIN (args) = end_params_node;
7771 stmt = make_qualified_primary (build_wfl_node (inst_id),
7772 build_wfl_node (DECL_NAME (decl)), 0);
7773 stmt = build_return (0, stmt);
7774 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7775 TREE_TYPE (decl), id, args, stmt);
7776 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7777
7778 /* Create the write access method */
7779 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7780 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7781 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7782 stmt = make_qualified_primary (build_wfl_node (inst_id),
7783 build_wfl_node (DECL_NAME (decl)), 0);
7784 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7785 build_wfl_node (wpv_id)));
7786
7787 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7788 TREE_TYPE (decl), id, args, stmt);
7789 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7790 pop_obstacks ();
7791
7792 /* Return the access name */
7793 return FIELD_INNER_ACCESS (decl) = id;
7794}
7795
7796/* Build an field access method NAME. */
7797
7798static tree
7799build_outer_field_access_method (class, type, name, args, body)
7800 tree class, type, name, args, body;
7801{
7802 tree saved_current_function_decl, mdecl;
7803
7804 /* Create the method */
7805 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7806 fix_method_argument_names (args, mdecl);
7807 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7808
7809 /* Attach the method body. */
7810 saved_current_function_decl = current_function_decl;
7811 start_artificial_method_body (mdecl);
7812 java_method_add_stmt (mdecl, body);
7813 end_artificial_method_body (mdecl);
7814 current_function_decl = saved_current_function_decl;
7815
7816 return mdecl;
7817}
7818
7819\f
7820/* This section deals with building access function necessary for
7821 certain kinds of method invocation from inner classes. */
7822
7823static tree
7824build_outer_method_access_method (decl)
7825 tree decl;
7826{
7827 tree saved_current_function_decl, mdecl;
7828 tree args = NULL_TREE, call_args = NULL_TREE;
7829 tree carg, id, body, class;
7830 char buffer [80];
7831 int parm_id_count = 0;
7832
7833 /* Test this abort with an access to a private field */
7834 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7835 abort ();
7836
7837 /* Check the cache first */
7838 if (DECL_FUNCTION_INNER_ACCESS (decl))
7839 return DECL_FUNCTION_INNER_ACCESS (decl);
7840
7841 class = DECL_CONTEXT (decl);
7842
7843 /* Obtain an access identifier and mark it */
7844 id = build_new_access_id ();
7845 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7846
7847 push_obstacks (&permanent_obstack, &permanent_obstack);
7848
7849 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7850 /* Create the arguments, as much as the original */
7851 for (; carg && carg != end_params_node;
7852 carg = TREE_CHAIN (carg))
7853 {
7854 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7855 args = chainon (args, build_tree_list (get_identifier (buffer),
7856 TREE_VALUE (carg)));
7857 }
7858 args = chainon (args, end_params_node);
7859
7860 /* Create the method */
7861 mdecl = create_artificial_method (class, ACC_STATIC,
7862 TREE_TYPE (TREE_TYPE (decl)), id, args);
7863 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7864 /* There is a potential bug here. We should be able to use
7865 fix_method_argument_names, but then arg names get mixed up and
7866 eventually a constructor will have its this$0 altered and the
7867 outer context won't be assignment properly. The test case is
7868 stub.java FIXME */
7869 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7870
7871 /* Attach the method body. */
7872 saved_current_function_decl = current_function_decl;
7873 start_artificial_method_body (mdecl);
7874
7875 /* The actual method invocation uses the same args. When invoking a
7876 static methods that way, we don't want to skip the first
7877 argument. */
7878 carg = args;
7879 if (!METHOD_STATIC (decl))
7880 carg = TREE_CHAIN (carg);
7881 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7882 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7883 call_args);
7884
7885 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7886 call_args);
7887 if (!METHOD_STATIC (decl))
7888 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
7889 body, 0);
7890 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
7891 body = build_return (0, body);
7892 java_method_add_stmt (mdecl,body);
7893 end_artificial_method_body (mdecl);
7894 current_function_decl = saved_current_function_decl;
7895 pop_obstacks ();
7896
7897 /* Back tag the access function so it know what it accesses */
7898 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
7899
7900 /* Tag the current method so it knows it has an access generated */
7901 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
7902}
7903
7904\f
7905/* This section of the code deals with building expressions to access
7906 the enclosing instance of an inner class. The enclosing instance is
7907 kept in a generated field called this$<n>, with <n> being the
7908 inner class nesting level (starting from 0.) */
7909
7910/* Build an access to a given this$<n>, possibly by chaining access
7911 call to others. Access methods to this$<n> are build on the fly if
7912 necessary */
7913
7914static tree
7915build_access_to_thisn (from, to, lc)
7916 tree from, to;
7917 int lc;
7918{
7919 tree access = NULL_TREE;
7920
7921 while (from != to)
7922 {
7923 tree access0_wfl, cn;
7924
7925 maybe_build_thisn_access_method (from);
7926 access0_wfl = build_wfl_node (access0_identifier_node);
7927 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
7928 EXPR_WFL_LINECOL (access0_wfl) = lc;
7929
7930 if (!access)
7931 {
7932 access = build_current_thisn (current_class);
7933 access = build_wfl_node (access);
7934 }
7935 access = build_tree_list (NULL_TREE, access);
7936 access = build_method_invocation (access0_wfl, access);
7937 access = make_qualified_primary (cn, access, lc);
7938
7939 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
7940 }
7941 return access;
7942}
7943
7944/* Build an access function to the this$<n> local to TYPE. NULL_TREE
7945 is returned if nothing needs to be generated. Otherwise, the method
152de068 7946 generated and a method decl is returned.
c2952b01
APB
7947
7948 NOTE: These generated methods should be declared in a class file
7949 attribute so that they can't be referred to directly. */
7950
7951static tree
7952maybe_build_thisn_access_method (type)
7953 tree type;
7954{
7955 tree mdecl, args, stmt, rtype;
7956 tree saved_current_function_decl;
7957
7958 /* If TYPE is a top-level class, no access method is required.
7959 If there already is such an access method, bail out. */
7960 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
7961 return NULL_TREE;
7962
7963 /* We generate the method. The method looks like:
7964 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
7965 */
7966 push_obstacks (&permanent_obstack, &permanent_obstack);
7967 args = build_tree_list (inst_id, build_pointer_type (type));
7968 TREE_CHAIN (args) = end_params_node;
7969 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
7970 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
7971 access0_identifier_node, args);
7972 fix_method_argument_names (args, mdecl);
7973 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
7974 stmt = build_current_thisn (type);
7975 stmt = make_qualified_primary (build_wfl_node (inst_id),
7976 build_wfl_node (stmt), 0);
7977 stmt = build_return (0, stmt);
7978
7979 saved_current_function_decl = current_function_decl;
7980 start_artificial_method_body (mdecl);
7981 java_method_add_stmt (mdecl, stmt);
7982 end_artificial_method_body (mdecl);
7983 current_function_decl = saved_current_function_decl;
7984 pop_obstacks ();
7985
7986 CLASS_ACCESS0_GENERATED_P (type) = 1;
7987
7988 return mdecl;
7989}
7990
7991/* Craft an correctly numbered `this$<n>'string. this$0 is used for
7992 the first level of innerclassing. this$1 for the next one, etc...
7993 This function can be invoked with TYPE to NULL, available and then
7994 has to count the parser context. */
7995
7996static tree
7997build_current_thisn (type)
7998 tree type;
7999{
8000 static int saved_i = -1;
8001 static tree saved_thisn = NULL_TREE;
8002
8003 tree decl;
8004 char buffer [80];
8005 int i = 0;
8006
8007 if (type)
8008 {
8009 static tree saved_type = NULL_TREE;
8010 static int saved_type_i = 0;
8011
8012 if (type == saved_type)
8013 i = saved_type_i;
8014 else
8015 {
8016 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8017 decl; decl = DECL_CONTEXT (decl), i++)
8018 ;
8019
8020 saved_type = type;
8021 saved_type_i = i;
8022 }
8023 }
8024 else
8025 i = list_length (GET_CPC_LIST ())-2;
8026
8027 if (i == saved_i)
8028 return saved_thisn;
8029
8030 sprintf (buffer, "this$%d", i);
8031 saved_i = i;
8032 saved_thisn = get_identifier (buffer);
8033 return saved_thisn;
8034}
8035
8036/* Return the assignement to the hidden enclosing context `this$<n>'
8037 by the second incoming parameter to the innerclass constructor. The
8038 form used is `this.this$<n> = this$<n>;'. */
8039
8040static tree
8041build_thisn_assign ()
8042{
8043 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8044 {
8045 tree thisn = build_current_thisn (current_class);
8046 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8047 build_wfl_node (thisn), 0);
8048 tree rhs = build_wfl_node (thisn);
8049 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8050 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8051 }
8052 return NULL_TREE;
8053}
8054
8055\f
165f37bc
APB
8056/* Building the synthetic `class$' used to implement the `.class' 1.1
8057 extension for non primitive types. This method looks like:
8058
8059 static Class class$(String type) throws NoClassDefFoundError
8060 {
8061 try {return (java.lang.Class.forName (String));}
8062 catch (ClassNotFoundException e) {
8063 throw new NoClassDefFoundError(e.getMessage());}
8064 } */
8065
8066static tree
8067build_dot_class_method (class)
8068 tree class;
8069{
8070#define BWF(S) build_wfl_node (get_identifier ((S)))
8071#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8072 tree args, tmp, saved_current_function_decl, mdecl;
8073 tree stmt, throw_stmt, catch, catch_block, try_block;
8074 tree catch_clause_param;
8075 tree class_not_found_exception, no_class_def_found_error;
8076
8077 static tree get_message_wfl, type_parm_wfl;
8078
8079 if (!get_message_wfl)
8080 {
8081 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8082 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8083 }
8084
8085 /* Build the arguments */
8086 args = build_tree_list (get_identifier ("type$"),
8087 build_pointer_type (string_type_node));
8088 TREE_CHAIN (args) = end_params_node;
8089
8090 /* Build the qualified name java.lang.Class.forName */
8091 tmp = MQN (MQN (MQN (BWF ("java"),
8092 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8093
8094 /* For things we have to catch and throw */
8095 class_not_found_exception =
8096 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8097 no_class_def_found_error =
8098 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8099 load_class (class_not_found_exception, 1);
8100 load_class (no_class_def_found_error, 1);
8101
8102 /* Create the "class$" function */
8103 mdecl = create_artificial_method (class, ACC_STATIC,
8104 build_pointer_type (class_type_node),
8105 get_identifier ("class$"), args);
8106 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8107 no_class_def_found_error);
8108
8109 /* We start by building the try block. We need to build:
8110 return (java.lang.Class.forName (type)); */
8111 stmt = build_method_invocation (tmp,
8112 build_tree_list (NULL_TREE, type_parm_wfl));
8113 stmt = build_return (0, stmt);
8114 /* Put it in a block. That's the try block */
8115 try_block = build_expr_block (stmt, NULL_TREE);
8116
8117 /* Now onto the catch block. We start by building the expression
8118 throwing a new exception:
8119 throw new NoClassDefFoundError (_.getMessage); */
8120 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8121 get_message_wfl, 0);
8122 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8123
8124 /* Build new NoClassDefFoundError (_.getMessage) */
8125 throw_stmt = build_new_invocation
8126 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8127 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8128
8129 /* Build the throw, (it's too early to use BUILD_THROW) */
8130 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8131
8132 /* Build the catch block to encapsulate all this. We begin by
8133 building an decl for the catch clause parameter and link it to
8134 newly created block, the catch block. */
8135 catch_clause_param =
8136 build_decl (VAR_DECL, wpv_id,
8137 build_pointer_type (class_not_found_exception));
8138 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8139
8140 /* We initialize the variable with the exception handler. */
8141 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8142 soft_exceptioninfo_call_node);
8143 add_stmt_to_block (catch_block, NULL_TREE, catch);
8144
8145 /* We add the statement throwing the new exception */
8146 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8147
8148 /* Build a catch expression for all this */
8149 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8150
8151 /* Build the try/catch sequence */
8152 stmt = build_try_statement (0, try_block, catch_block);
8153
8154 fix_method_argument_names (args, mdecl);
8155 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8156 saved_current_function_decl = current_function_decl;
8157 start_artificial_method_body (mdecl);
8158 java_method_add_stmt (mdecl, stmt);
8159 end_artificial_method_body (mdecl);
8160 current_function_decl = saved_current_function_decl;
8161 TYPE_DOT_CLASS (class) = mdecl;
8162
8163 return mdecl;
8164}
8165
8166static tree
8167build_dot_class_method_invocation (name)
8168 tree name;
8169{
8170 tree s = make_node (STRING_CST);
8171 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8172 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8173 TREE_STRING_LENGTH (s)+1);
8174 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8175 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8176 build_tree_list (NULL_TREE, s));
8177}
8178
c2952b01
APB
8179/* This section of the code deals with constructor. */
8180
22eed1e6
APB
8181/* Craft a body for default constructor. Patch existing constructor
8182 bodies with call to super() and field initialization statements if
8183 necessary. */
8184
8185static void
8186fix_constructors (mdecl)
8187 tree mdecl;
8188{
8189 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8190 tree thisn_assign, compound = NULL_TREE;
8191 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8192
22eed1e6
APB
8193 if (!body)
8194 {
22eed1e6
APB
8195 /* It is an error for the compiler to generate a default
8196 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8197 takes no argument, or the same args for an anonymous class */
8198 if (verify_constructor_super (mdecl))
22eed1e6 8199 {
c2952b01
APB
8200 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8201 tree save = DECL_NAME (mdecl);
49f48c71 8202 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8203 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8204 parse_error_context
c2952b01
APB
8205 (lookup_cl (TYPE_NAME (class_type)),
8206 "No constructor matching `%s' found in class `%s'",
8207 lang_printable_name (mdecl, 0), n);
8208 DECL_NAME (mdecl) = save;
22eed1e6
APB
8209 }
8210
c2952b01
APB
8211 /* The constructor body must be crafted by hand. It's the
8212 constructor we defined when we realize we didn't have the
8213 CLASSNAME() constructor */
22eed1e6
APB
8214 start_artificial_method_body (mdecl);
8215
8216 /* We don't generate a super constructor invocation if we're
8217 compiling java.lang.Object. build_super_invocation takes care
8218 of that. */
e920ebc9 8219 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8220
c2952b01
APB
8221 /* Insert the instance initializer block right here, after the
8222 super invocation. */
8223 add_instance_initializer (mdecl);
8224
8225 /* Insert an assignment to the this$<n> hidden field, if
8226 necessary */
8227 if ((thisn_assign = build_thisn_assign ()))
8228 java_method_add_stmt (mdecl, thisn_assign);
8229
22eed1e6
APB
8230 end_artificial_method_body (mdecl);
8231 }
8232 /* Search for an explicit constructor invocation */
8233 else
8234 {
8235 int found = 0;
8236 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8237
8238 while (body)
8239 switch (TREE_CODE (body))
8240 {
8241 case CALL_EXPR:
8242 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8243 body = NULL_TREE;
8244 break;
8245 case COMPOUND_EXPR:
8246 case EXPR_WITH_FILE_LOCATION:
8247 body = TREE_OPERAND (body, 0);
8248 break;
8249 case BLOCK:
8250 body = BLOCK_EXPR_BODY (body);
8251 break;
8252 default:
8253 found = 0;
8254 body = NULL_TREE;
8255 }
8256 /* The constructor is missing an invocation of super() */
8257 if (!found)
8258 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8259 build_super_invocation (mdecl));
22eed1e6 8260
c2952b01
APB
8261 /* Insert the instance initializer block right here, after the
8262 super invocation. */
8263 add_instance_initializer (mdecl);
8264
8265 /* Generate the assignment to this$<n>, if necessary */
8266 if ((thisn_assign = build_thisn_assign ()))
8267 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8268
22eed1e6
APB
8269 /* Fix the constructor main block if we're adding extra stmts */
8270 if (compound)
8271 {
8272 compound = add_stmt_to_compound (compound, NULL_TREE,
8273 BLOCK_EXPR_BODY (main_block));
8274 BLOCK_EXPR_BODY (main_block) = compound;
8275 }
8276 }
8277}
8278
8279/* Browse constructors in the super class, searching for a constructor
8280 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8281 otherwise. If the current class is an anonymous inner class, look
8282 for something that has the same signature. */
22eed1e6
APB
8283
8284static int
c2952b01
APB
8285verify_constructor_super (mdecl)
8286 tree mdecl;
22eed1e6
APB
8287{
8288 tree class = CLASSTYPE_SUPER (current_class);
152de068 8289 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8290 tree sdecl;
8291
22eed1e6
APB
8292 if (!class)
8293 return 0;
8294
c2952b01 8295 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8296 {
c2952b01
APB
8297 tree mdecl_arg_type;
8298 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8299 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8300 if (DECL_CONSTRUCTOR_P (sdecl))
8301 {
152de068
APB
8302 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8303 if (super_inner)
8304 arg_type = TREE_CHAIN (arg_type);
8305 for (; (arg_type != end_params_node
8306 && mdecl_arg_type != end_params_node);
c2952b01
APB
8307 arg_type = TREE_CHAIN (arg_type),
8308 mdecl_arg_type = TREE_CHAIN (mdecl_arg_type))
8309 if (TREE_VALUE (arg_type) != TREE_VALUE (mdecl_arg_type))
8310 break;
8311
8312 if (arg_type == end_params_node &&
8313 mdecl_arg_type == end_params_node)
8314 return 0;
8315 }
8316 }
8317 else
8318 {
8319 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8320 {
152de068
APB
8321 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8322 if (super_inner)
8323 arg = TREE_CHAIN (arg);
8324 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8325 return 0;
8326 }
8327 }
8328 return 1;
8329}
8330
22eed1e6 8331/* Generate code for all context remembered for code generation. */
b351b287
APB
8332
8333void
8334java_expand_classes ()
8335{
5423609c 8336 int save_error_count = 0;
c2952b01
APB
8337 static struct parser_ctxt *saved_ctxp = NULL;
8338
23a79c61
APB
8339 java_parse_abort_on_error ();
8340 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8341 return;
8342 java_layout_classes ();
8343 java_parse_abort_on_error ();
8344
48a840d9
APB
8345 /* The list of packages declaration seen so far needs to be
8346 reversed, so that package declared in a file being compiled gets
8347 priority over packages declared as a side effect of parsing other
8348 files.*/
8349 package_list = nreverse (package_list);
8350
c2952b01 8351 saved_ctxp = ctxp_for_generation;
b351b287
APB
8352 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8353 {
8354 ctxp = ctxp_for_generation;
8355 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8356 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8357 java_parse_abort_on_error ();
8358 }
c2952b01
APB
8359
8360 /* Find anonymous classes and expand their constructor, now they
8361 have been fixed. */
8362 for (ctxp_for_generation = saved_ctxp;
8363 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8364 {
8365 tree current;
8366 ctxp = ctxp_for_generation;
8367 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8368 {
8369 current_class = TREE_TYPE (current);
8370 if (ANONYMOUS_CLASS_P (current_class))
8371 {
8372 tree d;
8373 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8374 {
8375 if (DECL_CONSTRUCTOR_P (d))
8376 {
8377 restore_line_number_status (1);
8378 reset_method_name (d);
8379 java_complete_expand_method (d);
8380 restore_line_number_status (0);
8381 break; /* We now there are no other ones */
8382 }
8383 }
8384 }
8385 }
8386 }
8387
8388 /* If we've found error at that stage, don't try to generate
8389 anything, unless we're emitting xrefs or checking the syntax only
8390 (but not using -fsyntax-only for the purpose of generating
8391 bytecode. */
8392 if (java_error_count && !flag_emit_xref
8393 && (!flag_syntax_only && !flag_emit_class_files))
8394 return;
8395
8396 /* Now things are stable, go for generation of the class data. */
8397 for (ctxp_for_generation = saved_ctxp;
8398 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8399 {
8400 tree current;
8401 ctxp = ctxp_for_generation;
8402 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8403 {
8404 current_class = TREE_TYPE (current);
8405 outgoing_cpool = TYPE_CPOOL (current_class);
8406 if (flag_emit_class_files)
8407 write_classfile (current_class);
8408 if (flag_emit_xref)
8409 expand_xref (current_class);
8410 else if (! flag_syntax_only)
8411 finish_class ();
8412 }
8413 }
b351b287
APB
8414}
8415
e04a16fb
AG
8416/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8417 a tree list node containing RIGHT. Fore coming RIGHTs will be
8418 chained to this hook. LOCATION contains the location of the
8419 separating `.' operator. */
8420
8421static tree
8422make_qualified_primary (primary, right, location)
8423 tree primary, right;
8424 int location;
8425{
8426 tree wfl;
8427
c2952b01
APB
8428 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8429 wfl = build_wfl_wrap (primary);
e04a16fb
AG
8430 else
8431 {
8432 wfl = primary;
c2952b01
APB
8433 /* If wfl wasn't qualified, we build a first anchor */
8434 if (!EXPR_WFL_QUALIFICATION (wfl))
8435 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8436 }
8437
c2952b01 8438 /* And chain them */
e04a16fb
AG
8439 EXPR_WFL_LINECOL (right) = location;
8440 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8441 PRIMARY_P (wfl) = 1;
8442 return wfl;
8443}
8444
8445/* Simple merge of two name separated by a `.' */
8446
8447static tree
8448merge_qualified_name (left, right)
8449 tree left, right;
8450{
8451 tree node;
c2952b01
APB
8452 if (!left && !right)
8453 return NULL_TREE;
8454
8455 if (!left)
8456 return right;
8457
8458 if (!right)
8459 return left;
8460
e04a16fb
AG
8461 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8462 IDENTIFIER_LENGTH (left));
8463 obstack_1grow (&temporary_obstack, '.');
8464 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8465 IDENTIFIER_LENGTH (right));
8466 node = get_identifier (obstack_base (&temporary_obstack));
8467 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8468 QUALIFIED_P (node) = 1;
8469 return node;
8470}
8471
8472/* Merge the two parts of a qualified name into LEFT. Set the
8473 location information of the resulting node to LOCATION, usually
8474 inherited from the location information of the `.' operator. */
8475
8476static tree
8477make_qualified_name (left, right, location)
8478 tree left, right;
8479 int location;
8480{
bc3ca41b
PB
8481#ifdef USE_COMPONENT_REF
8482 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8483 EXPR_WFL_LINECOL (node) = location;
8484 return node;
8485#else
e04a16fb
AG
8486 tree left_id = EXPR_WFL_NODE (left);
8487 tree right_id = EXPR_WFL_NODE (right);
8488 tree wfl, merge;
8489
8490 merge = merge_qualified_name (left_id, right_id);
8491
8492 /* Left wasn't qualified and is now qualified */
8493 if (!QUALIFIED_P (left_id))
8494 {
8495 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8496 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8497 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8498 }
8499
8500 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8501 EXPR_WFL_LINECOL (wfl) = location;
8502 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8503
8504 EXPR_WFL_NODE (left) = merge;
8505 return left;
bc3ca41b 8506#endif
e04a16fb
AG
8507}
8508
8509/* Extract the last identifier component of the qualified in WFL. The
8510 last identifier is removed from the linked list */
8511
8512static tree
8513cut_identifier_in_qualified (wfl)
8514 tree wfl;
8515{
8516 tree q;
8517 tree previous = NULL_TREE;
8518 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8519 if (!TREE_CHAIN (q))
8520 {
8521 if (!previous)
781b0558 8522 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8523 TREE_CHAIN (previous) = NULL_TREE;
8524 return TREE_PURPOSE (q);
8525 }
8526}
8527
8528/* Resolve the expression name NAME. Return its decl. */
8529
8530static tree
5e942c50 8531resolve_expression_name (id, orig)
e04a16fb 8532 tree id;
5e942c50 8533 tree *orig;
e04a16fb
AG
8534{
8535 tree name = EXPR_WFL_NODE (id);
8536 tree decl;
8537
8538 /* 6.5.5.1: Simple expression names */
8539 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8540 {
8541 /* 15.13.1: NAME can appear within the scope of a local variable
8542 declaration */
8543 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8544 return decl;
8545
8546 /* 15.13.1: NAME can appear within a class declaration */
8547 else
8548 {
8549 decl = lookup_field_wrapper (current_class, name);
8550 if (decl)
8551 {
c2952b01 8552 tree access = NULL_TREE;
e04a16fb 8553 int fs = FIELD_STATIC (decl);
f2760b27
APB
8554
8555 /* If we're accessing an outer scope local alias, make
8556 sure we change the name of the field we're going to
8557 build access to. */
8558 if (FIELD_LOCAL_ALIAS_USED (decl))
8559 name = DECL_NAME (decl);
8560
e04a16fb
AG
8561 /* Instance variable (8.3.1.1) can't appear within
8562 static method, static initializer or initializer for
8563 a static variable. */
8564 if (!fs && METHOD_STATIC (current_function_decl))
8565 {
7f10c2e2 8566 static_ref_err (id, name, current_class);
e04a16fb
AG
8567 return error_mark_node;
8568 }
22eed1e6
APB
8569 /* Instance variables can't appear as an argument of
8570 an explicit constructor invocation */
8571 if (!fs && ctxp->explicit_constructor_p)
8572 {
8573 parse_error_context
781b0558 8574 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8575 return error_mark_node;
8576 }
5e942c50 8577
c2952b01
APB
8578 /* If we're processing an inner class and we're trying
8579 to access a field belonging to an outer class, build
8580 the access to the field */
8581 if (!fs && outer_field_access_p (current_class, decl))
8582 return build_outer_field_access (id, decl);
8583
5e942c50 8584 /* Otherwise build what it takes to access the field */
c2952b01
APB
8585 access = build_field_ref ((fs ? NULL_TREE : current_this),
8586 DECL_CONTEXT (decl), name);
e8fc7396 8587 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8588 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8589 /* We may be asked to save the real field access node */
8590 if (orig)
c2952b01 8591 *orig = access;
5e942c50 8592 /* And we return what we got */
c2952b01 8593 return access;
e04a16fb
AG
8594 }
8595 /* Fall down to error report on undefined variable */
8596 }
8597 }
8598 /* 6.5.5.2 Qualified Expression Names */
8599 else
8600 {
5e942c50
APB
8601 if (orig)
8602 *orig = NULL_TREE;
e04a16fb
AG
8603 qualify_ambiguous_name (id);
8604 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8605 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8606 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8607 }
8608
8609 /* We've got an error here */
8610 parse_error_context (id, "Undefined variable `%s'",
8611 IDENTIFIER_POINTER (name));
8612
8613 return error_mark_node;
8614}
8615
7f10c2e2
APB
8616static void
8617static_ref_err (wfl, field_id, class_type)
8618 tree wfl, field_id, class_type;
8619{
8620 parse_error_context
8621 (wfl,
8622 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8623 IDENTIFIER_POINTER (field_id),
8624 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8625}
8626
e04a16fb
AG
8627/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8628 We return something suitable to generate the field access. We also
8629 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8630 recipient's address can be null. */
8631
8632static tree
8633resolve_field_access (qual_wfl, field_decl, field_type)
8634 tree qual_wfl;
8635 tree *field_decl, *field_type;
8636{
8637 int is_static = 0;
8638 tree field_ref;
8639 tree decl, where_found, type_found;
8640
8641 if (resolve_qualified_expression_name (qual_wfl, &decl,
8642 &where_found, &type_found))
8643 return error_mark_node;
8644
8645 /* Resolve the LENGTH field of an array here */
8646 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
e8fc7396 8647 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8648 {
8649 tree length = build_java_array_length_access (where_found);
8650 field_ref =
8651 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8652
8653 /* In case we're dealing with a static array, we need to
8654 initialize its class before the array length can be fetched.
8655 It's also a good time to create a DECL_RTL for the field if
8656 none already exists, otherwise if the field was declared in a
8657 class found in an external file and hasn't been (and won't
8658 be) accessed for its value, none will be created. */
8659 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8660 {
8661 build_static_field_ref (where_found);
8662 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8663 }
e04a16fb
AG
8664 }
8665 /* We might have been trying to resolve field.method(). In which
8666 case, the resolution is over and decl is the answer */
34f4db93 8667 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8668 field_ref = decl;
34f4db93 8669 else if (JDECL_P (decl))
e04a16fb 8670 {
5e942c50
APB
8671 int static_final_found = 0;
8672 if (!type_found)
8673 type_found = DECL_CONTEXT (decl);
34f4db93 8674 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
5e942c50
APB
8675 if (FIELD_FINAL (decl)
8676 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8677 && DECL_INITIAL (decl))
5e942c50 8678 {
0f145be8 8679 field_ref = java_complete_tree (DECL_INITIAL (decl));
5e942c50
APB
8680 static_final_found = 1;
8681 }
8682 else
7f10c2e2
APB
8683 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8684 NULL_TREE : where_found),
5e942c50 8685 type_found, DECL_NAME (decl));
e04a16fb
AG
8686 if (field_ref == error_mark_node)
8687 return error_mark_node;
e8fc7396
APB
8688 if (is_static && !static_final_found
8689 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8690 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8691 }
8692 else
8693 field_ref = decl;
8694
8695 if (field_decl)
8696 *field_decl = decl;
8697 if (field_type)
c877974e
APB
8698 *field_type = (QUAL_DECL_TYPE (decl) ?
8699 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8700 return field_ref;
8701}
8702
e28cd97b
APB
8703/* If NODE is an access to f static field, strip out the class
8704 initialization part and return the field decl, otherwise, return
8705 NODE. */
8706
8707static tree
8708strip_out_static_field_access_decl (node)
8709 tree node;
8710{
8711 if (TREE_CODE (node) == COMPOUND_EXPR)
8712 {
8713 tree op1 = TREE_OPERAND (node, 1);
8714 if (TREE_CODE (op1) == COMPOUND_EXPR)
8715 {
8716 tree call = TREE_OPERAND (op1, 0);
8717 if (TREE_CODE (call) == CALL_EXPR
8718 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8719 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8720 == soft_initclass_node)
8721 return TREE_OPERAND (op1, 1);
8722 }
2f11d407
TT
8723 else if (JDECL_P (op1))
8724 return op1;
e28cd97b
APB
8725 }
8726 return node;
8727}
8728
e04a16fb
AG
8729/* 6.5.5.2: Qualified Expression Names */
8730
8731static int
8732resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8733 tree wfl;
8734 tree *found_decl, *type_found, *where_found;
8735{
8736 int from_type = 0; /* Field search initiated from a type */
c2952b01 8737 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8738 int previous_call_static = 0;
8739 int is_static;
8740 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8741 /* For certain for of inner class instantiation */
8742 tree saved_current, saved_this;
8743#define RESTORE_THIS_AND_CURRENT_CLASS \
8744 { current_class = saved_current; current_this = saved_this;}
8745
c877974e 8746 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8747
8748 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8749 {
8750 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8751 tree ret_decl; /* for EH checking */
8752 int location; /* for EH checking */
e04a16fb
AG
8753
8754 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8755 switch (TREE_CODE (qual_wfl))
8756 {
8757 case CALL_EXPR:
b67d701b 8758 case NEW_CLASS_EXPR:
e04a16fb
AG
8759 /* If the access to the function call is a non static field,
8760 build the code to access it. */
34f4db93 8761 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8762 {
ac825856
APB
8763 decl = maybe_access_field (decl, *where_found,
8764 DECL_CONTEXT (decl));
e04a16fb
AG
8765 if (decl == error_mark_node)
8766 return 1;
8767 }
c2952b01 8768
e04a16fb
AG
8769 /* And code for the function call */
8770 if (complete_function_arguments (qual_wfl))
8771 return 1;
c2952b01
APB
8772
8773 /* We might have to setup a new current class and a new this
8774 for the search of an inner class, relative to the type of
8775 a expression resolved as `decl'. The current values are
8776 saved and restored shortly after */
8777 saved_current = current_class;
8778 saved_this = current_this;
8779 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8780 {
8781 current_class = type;
8782 current_this = decl;
8783 }
8784
89e09b9a
PB
8785 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8786 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8787 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8788 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8789 *where_found = patch_method_invocation (qual_wfl, decl, type,
8790 &is_static, &ret_decl);
e04a16fb 8791 if (*where_found == error_mark_node)
c2952b01
APB
8792 {
8793 RESTORE_THIS_AND_CURRENT_CLASS;
8794 return 1;
8795 }
e04a16fb
AG
8796 *type_found = type = QUAL_DECL_TYPE (*where_found);
8797
c2952b01
APB
8798 /* If we're creating an inner class instance, check for that
8799 an enclosing instance is in scope */
8800 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8801 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8802 {
8803 parse_error_context
165f37bc
APB
8804 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8805 lang_printable_name (type, 0),
8806 (!current_this ? "" :
8807 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8808 RESTORE_THIS_AND_CURRENT_CLASS;
8809 return 1;
8810 }
8811
8812 /* In case we had to change then to resolve a inner class
8813 instantiation using a primary qualified by a `new' */
8814 RESTORE_THIS_AND_CURRENT_CLASS;
8815
7705e9db
APB
8816 /* EH check */
8817 if (location)
8818 check_thrown_exceptions (location, ret_decl);
8819
e04a16fb
AG
8820 /* If the previous call was static and this one is too,
8821 build a compound expression to hold the two (because in
8822 that case, previous function calls aren't transported as
8823 forcoming function's argument. */
8824 if (previous_call_static && is_static)
8825 {
8826 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8827 TREE_SIDE_EFFECTS (decl) = 1;
8828 }
8829 else
8830 {
8831 previous_call_static = is_static;
8832 decl = *where_found;
8833 }
c2952b01 8834 from_type = 0;
e04a16fb
AG
8835 continue;
8836
d8fccff5 8837 case NEW_ARRAY_EXPR:
c2952b01 8838 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
8839 *where_found = decl = java_complete_tree (qual_wfl);
8840 if (decl == error_mark_node)
8841 return 1;
8842 *type_found = type = QUAL_DECL_TYPE (decl);
8843 CLASS_LOADED_P (type) = 1;
8844 continue;
8845
e04a16fb
AG
8846 case CONVERT_EXPR:
8847 *where_found = decl = java_complete_tree (qual_wfl);
8848 if (decl == error_mark_node)
8849 return 1;
8850 *type_found = type = QUAL_DECL_TYPE (decl);
8851 from_cast = 1;
8852 continue;
8853
22eed1e6 8854 case CONDITIONAL_EXPR:
5e942c50 8855 case STRING_CST:
ac22f9cb 8856 case MODIFY_EXPR:
22eed1e6
APB
8857 *where_found = decl = java_complete_tree (qual_wfl);
8858 if (decl == error_mark_node)
8859 return 1;
8860 *type_found = type = QUAL_DECL_TYPE (decl);
8861 continue;
8862
e04a16fb
AG
8863 case ARRAY_REF:
8864 /* If the access to the function call is a non static field,
8865 build the code to access it. */
34f4db93 8866 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
8867 {
8868 decl = maybe_access_field (decl, *where_found, type);
8869 if (decl == error_mark_node)
8870 return 1;
8871 }
8872 /* And code for the array reference expression */
8873 decl = java_complete_tree (qual_wfl);
8874 if (decl == error_mark_node)
8875 return 1;
8876 type = QUAL_DECL_TYPE (decl);
8877 continue;
0a2138e2 8878
37feda7d
APB
8879 case PLUS_EXPR:
8880 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8881 return 1;
8882 if ((type = patch_string (decl)))
8883 decl = type;
8884 *where_found = QUAL_RESOLUTION (q) = decl;
8885 *type_found = type = TREE_TYPE (decl);
8886 break;
8887
165f37bc
APB
8888 case CLASS_LITERAL:
8889 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8890 return 1;
8891 *where_found = QUAL_RESOLUTION (q) = decl;
8892 *type_found = type = TREE_TYPE (decl);
8893 break;
8894
0a2138e2
APB
8895 default:
8896 /* Fix for -Wall Just go to the next statement. Don't
8897 continue */
a3f406ce 8898 break;
e04a16fb
AG
8899 }
8900
8901 /* If we fall here, we weren't processing a (static) function call. */
8902 previous_call_static = 0;
8903
8904 /* It can be the keyword THIS */
8905 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
8906 {
8907 if (!current_this)
8908 {
8909 parse_error_context
8910 (wfl, "Keyword `this' used outside allowed context");
8911 return 1;
8912 }
f63991a8
APB
8913 if (ctxp->explicit_constructor_p)
8914 {
781b0558 8915 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
8916 return 1;
8917 }
e04a16fb 8918 /* We have to generate code for intermediate acess */
c2952b01
APB
8919 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
8920 {
8921 *where_found = decl = current_this;
8922 *type_found = type = QUAL_DECL_TYPE (decl);
8923 }
8924 /* We're trying to access the this from somewhere else... */
8925 else
8926 {
8927 *where_found = decl = build_current_thisn (type);
8928 from_qualified_this = 1;
8929 }
8930
8931 from_type = 0;
e04a16fb
AG
8932 continue;
8933 }
8934
8935 /* 15.10.2 Accessing Superclass Members using SUPER */
8936 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
8937 {
8938 tree node;
8939 /* Check on the restricted use of SUPER */
8940 if (METHOD_STATIC (current_function_decl)
8941 || current_class == object_type_node)
8942 {
8943 parse_error_context
8944 (wfl, "Keyword `super' used outside allowed context");
8945 return 1;
8946 }
8947 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
8948 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
8949 CLASSTYPE_SUPER (current_class),
8950 build_this (EXPR_WFL_LINECOL (qual_wfl)));
8951 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
8952 if (decl == error_mark_node)
8953 return 1;
e04a16fb
AG
8954 *type_found = type = QUAL_DECL_TYPE (decl);
8955 from_super = from_type = 1;
8956 continue;
8957 }
8958
8959 /* 15.13.1: Can't search for field name in packages, so we
8960 assume a variable/class name was meant. */
8961 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
8962 {
5e942c50
APB
8963 tree name = resolve_package (wfl, &q);
8964 if (name)
8965 {
c2952b01 8966 tree list;
5e942c50 8967 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 8968 /* We want to be absolutely sure that the class is laid
5e942c50
APB
8969 out. We're going to search something inside it. */
8970 *type_found = type = TREE_TYPE (decl);
8971 layout_class (type);
8972 from_type = 1;
c2952b01 8973
dde1da72
APB
8974 /* Fix them all the way down, if any are left. */
8975 if (q)
c2952b01 8976 {
dde1da72
APB
8977 list = TREE_CHAIN (q);
8978 while (list)
8979 {
8980 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
8981 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
8982 list = TREE_CHAIN (list);
8983 }
c2952b01 8984 }
5e942c50 8985 }
e04a16fb 8986 else
5e942c50
APB
8987 {
8988 if (from_super || from_cast)
8989 parse_error_context
8990 ((from_cast ? qual_wfl : wfl),
8991 "No variable `%s' defined in class `%s'",
8992 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
8993 lang_printable_name (type, 0));
8994 else
8995 parse_error_context
8996 (qual_wfl, "Undefined variable or class name: `%s'",
8997 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
8998 return 1;
8999 }
e04a16fb
AG
9000 }
9001
9002 /* We have a type name. It's been already resolved when the
9003 expression was qualified. */
9004 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9005 {
9006 if (!(decl = QUAL_RESOLUTION (q)))
9007 return 1; /* Error reported already */
9008
c2952b01
APB
9009 /* Sneak preview. If next we see a `new', we're facing a
9010 qualification with resulted in a type being selected
9011 instead of a field. Report the error */
9012 if(TREE_CHAIN (q)
9013 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9014 {
9015 parse_error_context (qual_wfl, "Undefined variable `%s'",
9016 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9017 return 1;
9018 }
9019
e04a16fb
AG
9020 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9021 {
9022 parse_error_context
9023 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9024 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9025 GET_TYPE_NAME (type),
e04a16fb
AG
9026 IDENTIFIER_POINTER (DECL_NAME (decl)),
9027 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9028 return 1;
9029 }
5e942c50 9030 check_deprecation (qual_wfl, decl);
c2952b01 9031
e04a16fb
AG
9032 type = TREE_TYPE (decl);
9033 from_type = 1;
9034 }
9035 /* We resolve and expression name */
9036 else
9037 {
cd531a2e 9038 tree field_decl = NULL_TREE;
e04a16fb
AG
9039
9040 /* If there exists an early resolution, use it. That occurs
9041 only once and we know that there are more things to
9042 come. Don't do that when processing something after SUPER
9043 (we need more thing to be put in place below */
9044 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9045 {
9046 decl = QUAL_RESOLUTION (q);
c877974e 9047 if (!type)
5e942c50 9048 {
7f10c2e2
APB
9049 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9050 {
9051 if (current_this)
9052 *where_found = current_this;
9053 else
9054 {
9055 static_ref_err (qual_wfl, DECL_NAME (decl),
9056 current_class);
9057 return 1;
9058 }
9059 }
c877974e
APB
9060 else
9061 {
9062 *where_found = TREE_TYPE (decl);
9063 if (TREE_CODE (*where_found) == POINTER_TYPE)
9064 *where_found = TREE_TYPE (*where_found);
9065 }
5e942c50 9066 }
b67d701b 9067 }
e04a16fb
AG
9068
9069 /* We have to search for a field, knowing the type of its
9070 container. The flag FROM_TYPE indicates that we resolved
9071 the last member of the expression as a type name, which
5e942c50
APB
9072 means that for the resolution of this field, we'll look
9073 for other errors than if it was resolved as a member of
9074 an other field. */
e04a16fb
AG
9075 else
9076 {
9077 int is_static;
5e942c50
APB
9078 tree field_decl_type; /* For layout */
9079
e04a16fb
AG
9080 if (!from_type && !JREFERENCE_TYPE_P (type))
9081 {
9082 parse_error_context
9083 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9084 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9085 lang_printable_name (type, 0),
e04a16fb
AG
9086 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9087 return 1;
9088 }
9089
dc0b3eff
PB
9090 field_decl = lookup_field_wrapper (type,
9091 EXPR_WFL_NODE (qual_wfl));
9092 if (field_decl == NULL_TREE)
e04a16fb
AG
9093 {
9094 parse_error_context
2aa11e97 9095 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9096 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9097 GET_TYPE_NAME (type));
e04a16fb
AG
9098 return 1;
9099 }
dc0b3eff
PB
9100 if (field_decl == error_mark_node)
9101 return 1;
5e942c50
APB
9102
9103 /* Layout the type of field_decl, since we may need
c877974e
APB
9104 it. Don't do primitive types or loaded classes. The
9105 situation of non primitive arrays may not handled
9106 properly here. FIXME */
5e942c50
APB
9107 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9108 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9109 else
9110 field_decl_type = TREE_TYPE (field_decl);
9111 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9112 && !CLASS_LOADED_P (field_decl_type)
9113 && !TYPE_ARRAY_P (field_decl_type))
9114 resolve_and_layout (field_decl_type, NULL_TREE);
9115 if (TYPE_ARRAY_P (field_decl_type))
9116 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9117
9118 /* Check on accessibility here */
9119 if (not_accessible_p (type, field_decl, from_super))
9120 {
9121 parse_error_context
9122 (qual_wfl,
9123 "Can't access %s field `%s.%s' from `%s'",
9124 java_accstring_lookup
9125 (get_access_flags_from_decl (field_decl)),
2aa11e97 9126 GET_TYPE_NAME (type),
e04a16fb
AG
9127 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9128 IDENTIFIER_POINTER
9129 (DECL_NAME (TYPE_NAME (current_class))));
9130 return 1;
9131 }
5e942c50 9132 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9133
9134 /* There are things to check when fields are accessed
9135 from type. There are no restrictions on a static
9136 declaration of the field when it is accessed from an
9137 interface */
9138 is_static = FIELD_STATIC (field_decl);
9139 if (!from_super && from_type
c2952b01
APB
9140 && !TYPE_INTERFACE_P (type)
9141 && !is_static
9142 && (current_function_decl
9143 && METHOD_STATIC (current_function_decl)))
e04a16fb 9144 {
7f10c2e2 9145 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9146 return 1;
9147 }
9148 from_cast = from_super = 0;
9149
c2952b01
APB
9150 /* It's an access from a type but it isn't static, we
9151 make it relative to `this'. */
9152 if (!is_static && from_type)
9153 decl = current_this;
9154
5e942c50
APB
9155 /* If we need to generate something to get a proper
9156 handle on what this field is accessed from, do it
9157 now. */
e04a16fb
AG
9158 if (!is_static)
9159 {
c583dd46 9160 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9161 if (decl == error_mark_node)
9162 return 1;
9163 }
9164
9165 /* We want to keep the location were found it, and the type
9166 we found. */
9167 *where_found = decl;
9168 *type_found = type;
9169
c2952b01
APB
9170 /* Generate the correct expression for field access from
9171 qualified this */
9172 if (from_qualified_this)
9173 {
9174 field_decl = build_outer_field_access (qual_wfl, field_decl);
9175 from_qualified_this = 0;
9176 }
9177
e04a16fb
AG
9178 /* This is the decl found and eventually the next one to
9179 search from */
9180 decl = field_decl;
9181 }
e04a16fb
AG
9182 from_type = 0;
9183 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9184
9185 /* Sneak preview. If decl is qualified by a `new', report
9186 the error here to be accurate on the peculiar construct */
9187 if (TREE_CHAIN (q)
9188 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9189 && !JREFERENCE_TYPE_P (type))
9190 {
9191 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9192 lang_printable_name (type, 0));
9193 return 1;
9194 }
e04a16fb 9195 }
dde1da72
APB
9196 /* `q' might have changed due to a after package resolution
9197 re-qualification */
9198 if (!q)
9199 break;
e04a16fb
AG
9200 }
9201 *found_decl = decl;
9202 return 0;
9203}
9204
9205/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9206 can't be accessed from REFERENCE (a record type). */
9207
be245ac0
KG
9208static int
9209not_accessible_p (reference, member, from_super)
e04a16fb
AG
9210 tree reference, member;
9211 int from_super;
9212{
9213 int access_flag = get_access_flags_from_decl (member);
9214
9215 /* Access always granted for members declared public */
9216 if (access_flag & ACC_PUBLIC)
9217 return 0;
9218
9219 /* Check access on protected members */
9220 if (access_flag & ACC_PROTECTED)
9221 {
9222 /* Access granted if it occurs from within the package
9223 containing the class in which the protected member is
9224 declared */
9225 if (class_in_current_package (DECL_CONTEXT (member)))
9226 return 0;
9227
9bbc7d9f
PB
9228 /* If accessed with the form `super.member', then access is granted */
9229 if (from_super)
9230 return 0;
e04a16fb 9231
9bbc7d9f
PB
9232 /* Otherwise, access is granted if occuring from the class where
9233 member is declared or a subclass of it */
473e7b07 9234 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9235 return 0;
e04a16fb
AG
9236 return 1;
9237 }
9238
9239 /* Check access on private members. Access is granted only if it
473e7b07
TT
9240 occurs from within the class in which it is declared. Exceptions
9241 are accesses from inner-classes. This section is probably not
c2952b01 9242 complete. FIXME */
e04a16fb 9243 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9244 return (current_class == DECL_CONTEXT (member) ? 0 :
9245 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9246
9247 /* Default access are permitted only when occuring within the
9248 package in which the type (REFERENCE) is declared. In other words,
9249 REFERENCE is defined in the current package */
9250 if (ctxp->package)
9251 return !class_in_current_package (reference);
473e7b07 9252
e04a16fb
AG
9253 /* Otherwise, access is granted */
9254 return 0;
9255}
9256
5e942c50
APB
9257/* Test deprecated decl access. */
9258static void
9259check_deprecation (wfl, decl)
9260 tree wfl, decl;
9261{
49f48c71 9262 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9263 /* Complain if the field is deprecated and the file it was defined
9264 in isn't compiled at the same time the file which contains its
9265 use is */
9266 if (DECL_DEPRECATED (decl)
9267 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9268 {
9269 char the [20];
9270 switch (TREE_CODE (decl))
9271 {
9272 case FUNCTION_DECL:
9273 strcpy (the, "method");
9274 break;
9275 case FIELD_DECL:
9276 strcpy (the, "field");
9277 break;
9278 case TYPE_DECL:
9279 strcpy (the, "class");
9280 break;
15fdcfe9
PB
9281 default:
9282 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9283 }
9284 parse_warning_context
9285 (wfl, "The %s `%s' in class `%s' has been deprecated",
9286 the, lang_printable_name (decl, 0),
9287 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9288 }
9289}
9290
e04a16fb
AG
9291/* Returns 1 if class was declared in the current package, 0 otherwise */
9292
9293static int
9294class_in_current_package (class)
9295 tree class;
9296{
9297 static tree cache = NULL_TREE;
9298 int qualified_flag;
9299 tree left;
9300
9301 if (cache == class)
9302 return 1;
9303
9304 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9305
9306 /* If the current package is empty and the name of CLASS is
9307 qualified, class isn't in the current package. If there is a
9308 current package and the name of the CLASS is not qualified, class
9309 isn't in the current package */
0a2138e2 9310 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9311 return 0;
9312
9313 /* If there is not package and the name of CLASS isn't qualified,
9314 they belong to the same unnamed package */
9315 if (!ctxp->package && !qualified_flag)
9316 return 1;
9317
9318 /* Compare the left part of the name of CLASS with the package name */
9319 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9320 if (ctxp->package == left)
9321 {
9322 cache = class;
9323 return 1;
9324 }
9325 return 0;
9326}
9327
9328/* This function may generate code to access DECL from WHERE. This is
9329 done only if certain conditions meet. */
9330
9331static tree
9332maybe_access_field (decl, where, type)
9333 tree decl, where, type;
9334{
5e942c50
APB
9335 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9336 && !FIELD_STATIC (decl))
e04a16fb 9337 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9338 (type ? type : DECL_CONTEXT (decl)),
9339 DECL_NAME (decl));
e04a16fb
AG
9340 return decl;
9341}
9342
15fdcfe9 9343/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9344 and according to the situation, PRIMARY and WHERE may be
9345 used. IS_STATIC is set to 1 if the invoked function is static. */
9346
9347static tree
89e09b9a 9348patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9349 tree patch, primary, where;
9350 int *is_static;
b9f7e36c 9351 tree *ret_decl;
e04a16fb
AG
9352{
9353 tree wfl = TREE_OPERAND (patch, 0);
9354 tree args = TREE_OPERAND (patch, 1);
9355 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9356 tree list;
22eed1e6 9357 int is_static_flag = 0;
89e09b9a 9358 int is_super_init = 0;
bccaf73a 9359 tree this_arg = NULL_TREE;
e04a16fb
AG
9360
9361 /* Should be overriden if everything goes well. Otherwise, if
9362 something fails, it should keep this value. It stop the
9363 evaluation of a bogus assignment. See java_complete_tree,
9364 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9365 evaluating an assignment */
9366 TREE_TYPE (patch) = error_mark_node;
9367
9368 /* Since lookup functions are messing with line numbers, save the
9369 context now. */
9370 java_parser_context_save_global ();
9371
9372 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9373
9374 /* Resolution of qualified name, excluding constructors */
9375 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9376 {
dde1da72 9377 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9378 /* Extract the last IDENTIFIER of the qualified
9379 expression. This is a wfl and we will use it's location
9380 data during error report. */
9381 identifier_wfl = cut_identifier_in_qualified (wfl);
9382 identifier = EXPR_WFL_NODE (identifier_wfl);
9383
9384 /* Given the context, IDENTIFIER is syntactically qualified
9385 as a MethodName. We need to qualify what's before */
9386 qualify_ambiguous_name (wfl);
dde1da72 9387 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9388
dde1da72
APB
9389 if (resolved == error_mark_node)
9390 PATCH_METHOD_RETURN_ERROR ();
9391
9392 type = GET_SKIP_TYPE (resolved);
9393 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9394
9395 if (JPRIMITIVE_TYPE_P (type))
9396 {
9397 parse_error_context
9398 (identifier_wfl,
9399 "Can't invoke a method on primitive type `%s'",
9400 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9401 PATCH_METHOD_RETURN_ERROR ();
9402 }
9403
dde1da72
APB
9404 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9405 args = nreverse (args);
2c56429a 9406
e04a16fb 9407 /* We're resolving a call from a type */
dde1da72 9408 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9409 {
dde1da72 9410 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9411 {
9412 parse_error_context
781b0558
KG
9413 (identifier_wfl,
9414 "Can't make static reference to method `%s' in interface `%s'",
9415 IDENTIFIER_POINTER (identifier),
e04a16fb 9416 IDENTIFIER_POINTER (name));
b9f7e36c 9417 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9418 }
e04a16fb
AG
9419 if (list && !METHOD_STATIC (list))
9420 {
c2e3db92 9421 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9422 parse_error_context
9423 (identifier_wfl,
9424 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9425 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9426 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9427 free (fct_name);
b9f7e36c 9428 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9429 }
9430 }
e04a16fb 9431 else
dde1da72
APB
9432 this_arg = primary = resolved;
9433
5e942c50 9434 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9435 wfl = identifier_wfl;
9436 }
9437 /* Resolution of simple names, names generated after a primary: or
9438 constructors */
9439 else
9440 {
cd531a2e 9441 tree class_to_search = NULL_TREE;
c2952b01 9442 int lc; /* Looking for Constructor */
e04a16fb
AG
9443
9444 /* We search constructor in their target class */
9445 if (CALL_CONSTRUCTOR_P (patch))
9446 {
22eed1e6
APB
9447 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9448 class_to_search = EXPR_WFL_NODE (wfl);
9449 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9450 this_identifier_node)
9451 class_to_search = NULL_TREE;
9452 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9453 super_identifier_node)
e04a16fb 9454 {
89e09b9a 9455 is_super_init = 1;
22eed1e6
APB
9456 if (CLASSTYPE_SUPER (current_class))
9457 class_to_search =
9458 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9459 else
9460 {
781b0558 9461 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9462 PATCH_METHOD_RETURN_ERROR ();
9463 }
e04a16fb 9464 }
22eed1e6
APB
9465
9466 /* Class to search is NULL if we're searching the current one */
9467 if (class_to_search)
e04a16fb 9468 {
c2952b01
APB
9469 class_to_search = resolve_and_layout (class_to_search, wfl);
9470
22eed1e6
APB
9471 if (!class_to_search)
9472 {
9473 parse_error_context
9474 (wfl, "Class `%s' not found in type declaration",
9475 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9476 PATCH_METHOD_RETURN_ERROR ();
9477 }
9478
5e942c50
APB
9479 /* Can't instantiate an abstract class, but we can
9480 invoke it's constructor. It's use within the `new'
9481 context is denied here. */
9482 if (CLASS_ABSTRACT (class_to_search)
9483 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9484 {
9485 parse_error_context
781b0558
KG
9486 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9487 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9488 PATCH_METHOD_RETURN_ERROR ();
9489 }
c2952b01 9490
22eed1e6 9491 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9492 }
22eed1e6
APB
9493 else
9494 class_to_search = current_class;
e04a16fb
AG
9495 lc = 1;
9496 }
9497 /* This is a regular search in the local class, unless an
9498 alternate class is specified. */
9499 else
9500 {
9501 class_to_search = (where ? where : current_class);
9502 lc = 0;
9503 }
c2952b01 9504
e04a16fb
AG
9505 /* NAME is a simple identifier or comes from a primary. Search
9506 in the class whose declaration contain the method being
9507 invoked. */
c877974e 9508 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9509
c2952b01 9510 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9511 /* Don't continue if no method were found, as the next statement
9512 can't be executed then. */
b9f7e36c
APB
9513 if (!list)
9514 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9515
9516 /* Check for static reference if non static methods */
9517 if (check_for_static_method_reference (wfl, patch, list,
9518 class_to_search, primary))
b9f7e36c 9519 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9520
165f37bc
APB
9521 /* Check for inner classes creation from illegal contexts */
9522 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9523 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9524 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9525 {
9526 parse_error_context
9527 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9528 lang_printable_name (class_to_search, 0),
9529 (!current_this ? "" :
9530 "; an explicit one must be provided when creating this inner class"));
9531 PATCH_METHOD_RETURN_ERROR ();
9532 }
9533
22eed1e6
APB
9534 /* Non static methods are called with the current object extra
9535 argument. If patch a `new TYPE()', the argument is the value
9536 returned by the object allocator. If method is resolved as a
9537 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9538 args = nreverse (args);
bccaf73a 9539 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9540 {
9541 this_arg = primary ? primary : current_this;
9542
9543 /* If we're using an access method, things are different.
9544 There are two familly of cases:
9545
9546 1) We're not generating bytecodes:
9547
9548 - LIST is non static. It's invocation is transformed from
9549 x(a1,...,an) into this$<n>.x(a1,....an).
9550 - LIST is static. It's invocation is transformed from
9551 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9552
9553 2) We're generating bytecodes:
9554
9555 - LIST is non static. It's invocation is transformed from
9556 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9557 - LIST is static. It's invocation is transformed from
9558 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9559
9560 Of course, this$<n> can be abitrary complex, ranging from
9561 this$0 (the immediate outer context) to
9562 access$0(access$0(...(this$0))).
9563
9564 maybe_use_access_method returns a non zero value if the
dfb99c83
APB
9565 this_arg has to be moved into the (then generated) stub
9566 argument list. In the mean time, the selected function
9567 might have be replaced by a generated stub. */
c2952b01
APB
9568 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9569 args = tree_cons (NULL_TREE, this_arg, args);
9570 }
e04a16fb 9571 }
b67d701b 9572
e04a16fb
AG
9573 /* Merge point of all resolution schemes. If we have nothing, this
9574 is an error, already signaled */
b9f7e36c
APB
9575 if (!list)
9576 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9577
e04a16fb
AG
9578 /* Check accessibility, position the is_static flag, build and
9579 return the call */
9bbc7d9f 9580 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9581 {
c2e3db92 9582 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9583 parse_error_context
9584 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9585 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9586 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9587 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9588 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9589 free (fct_name);
b9f7e36c 9590 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9591 }
5e942c50 9592 check_deprecation (wfl, list);
22eed1e6 9593
c2952b01
APB
9594 /* If invoking a innerclass constructor, there are hidden parameters
9595 to pass */
9596 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9597 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9598 {
9599 /* And make sure we add the accessed local variables to be saved
9600 in field aliases. */
9601 args = build_alias_initializer_parameter_list
9602 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9603
9604 /* We have to reverse things. Find out why. FIXME */
9605 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9606 args = nreverse (args);
9607
9608 /* Secretely pass the current_this/primary as a second argument */
165f37bc
APB
9609 if (primary || current_this)
9610 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9611 else
9612 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9613 }
9614
152de068
APB
9615 /* This handles the situation where a constructor invocation needs
9616 to have an enclosing context passed as a second parameter (the
9617 constructor is one of an inner class. We extract it from the
9618 current function. */
9619 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9620 {
9621 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9622 tree extra_arg;
9623
9624 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9625 {
9626 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9627 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9628 }
9629 else
9630 {
9631 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9632 extra_arg =
9633 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9634 extra_arg = java_complete_tree (extra_arg);
9635 }
9636 args = tree_cons (NULL_TREE, extra_arg, args);
9637 }
9638
22eed1e6 9639 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9640 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9641 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9642
c3f2a476
APB
9643 /* In the context of an explicit constructor invocation, we can't
9644 invoke any method relying on `this'. Exceptions are: we're
9645 invoking a static function, primary exists and is not the current
9646 this, we're creating a new object. */
22eed1e6 9647 if (ctxp->explicit_constructor_p
c3f2a476
APB
9648 && !is_static_flag
9649 && (!primary || primary == current_this)
9650 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9651 {
781b0558 9652 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9653 PATCH_METHOD_RETURN_ERROR ();
9654 }
e04a16fb 9655 java_parser_context_restore_global ();
22eed1e6
APB
9656 if (is_static)
9657 *is_static = is_static_flag;
b9f7e36c
APB
9658 /* Sometimes, we want the decl of the selected method. Such as for
9659 EH checking */
9660 if (ret_decl)
9661 *ret_decl = list;
89e09b9a
PB
9662 patch = patch_invoke (patch, list, args);
9663 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9664 {
c2952b01
APB
9665 tree finit_parms, finit_call;
9666
9667 /* Prepare to pass hidden parameters to $finit$, if any. */
9668 finit_parms = build_alias_initializer_parameter_list
9669 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9670
c2952b01
APB
9671 finit_call =
9672 build_method_invocation (build_wfl_node (finit_identifier_node),
9673 finit_parms);
9674
9675 /* Generate the code used to initialize fields declared with an
9676 initialization statement and build a compound statement along
9677 with the super constructor invocation. */
89e09b9a
PB
9678 patch = build (COMPOUND_EXPR, void_type_node, patch,
9679 java_complete_tree (finit_call));
9680 CAN_COMPLETE_NORMALLY (patch) = 1;
9681 }
9682 return patch;
e04a16fb
AG
9683}
9684
9685/* Check that we're not trying to do a static reference to a method in
9686 non static method. Return 1 if it's the case, 0 otherwise. */
9687
9688static int
9689check_for_static_method_reference (wfl, node, method, where, primary)
9690 tree wfl, node, method, where, primary;
9691{
9692 if (METHOD_STATIC (current_function_decl)
9693 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9694 {
c2e3db92 9695 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9696 parse_error_context
9697 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9698 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9699 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9700 free (fct_name);
9701 return 1;
9702 }
9703 return 0;
9704}
9705
c2952b01
APB
9706/* Fix the invocation of *MDECL if necessary in the case of a
9707 invocation from an inner class. *THIS_ARG might be modified
9708 appropriately and an alternative access to *MDECL might be
9709 returned. */
9710
9711static int
9712maybe_use_access_method (is_super_init, mdecl, this_arg)
9713 int is_super_init;
9714 tree *mdecl, *this_arg;
9715{
9716 tree ctx;
9717 tree md = *mdecl, ta = *this_arg;
9718 int to_return = 0;
9719 int non_static_context = !METHOD_STATIC (md);
9720
9721 if (is_super_init
165f37bc
APB
9722 || DECL_CONTEXT (md) == current_class
9723 || !PURE_INNER_CLASS_TYPE_P (current_class)
9724 || DECL_FINIT_P (md))
c2952b01
APB
9725 return 0;
9726
9727 /* If we're calling a method found in an enclosing class, generate
9728 what it takes to retrieve the right this. Don't do that if we're
9729 invoking a static method. */
9730
9731 if (non_static_context)
9732 {
9733 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9734 if (ctx == DECL_CONTEXT (md))
9735 {
9736 ta = build_current_thisn (current_class);
9737 ta = build_wfl_node (ta);
9738 }
9739 else
9740 {
9741 tree type = ctx;
9742 while (type)
9743 {
9744 maybe_build_thisn_access_method (type);
9745 if (type == DECL_CONTEXT (md))
9746 {
9747 ta = build_access_to_thisn (ctx, type, 0);
9748 break;
9749 }
9750 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9751 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9752 }
9753 }
9754 ta = java_complete_tree (ta);
9755 }
9756
9757 /* We might have to use an access method to get to MD. We can
9758 break the method access rule as far as we're not generating
9759 bytecode */
9760 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9761 {
9762 md = build_outer_method_access_method (md);
9763 to_return = 1;
9764 }
9765
9766 *mdecl = md;
9767 *this_arg = ta;
9768
9769 /* Returnin a non zero value indicates we were doing a non static
9770 method invokation that is now a static invocation. It will have
9771 callee displace `this' to insert it in the regular argument
9772 list. */
9773 return (non_static_context && to_return);
9774}
9775
e04a16fb
AG
9776/* Patch an invoke expression METHOD and ARGS, based on its invocation
9777 mode. */
9778
9779static tree
89e09b9a 9780patch_invoke (patch, method, args)
e04a16fb 9781 tree patch, method, args;
e04a16fb
AG
9782{
9783 tree dtable, func;
0a2138e2 9784 tree original_call, t, ta;
e815887f 9785 tree cond = NULL_TREE;
e04a16fb 9786
5e942c50
APB
9787 /* Last step for args: convert build-in types. If we're dealing with
9788 a new TYPE() type call, the first argument to the constructor
e815887f 9789 isn't found in the incoming argument list, but delivered by
5e942c50
APB
9790 `new' */
9791 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9792 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9793 t = TREE_CHAIN (t);
ac825856
APB
9794 for (ta = args; t != end_params_node && ta;
9795 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
9796 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9797 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9798 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
9799
9800 /* Resolve unresolved returned type isses */
9801 t = TREE_TYPE (TREE_TYPE (method));
9802 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9803 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 9804
e8fc7396 9805 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9806 func = method;
9807 else
e04a16fb 9808 {
15fdcfe9 9809 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 9810 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
9811 {
9812 case INVOKE_VIRTUAL:
9813 dtable = invoke_build_dtable (0, args);
9814 func = build_invokevirtual (dtable, method);
9815 break;
b9f7e36c 9816
e815887f
TT
9817 case INVOKE_NONVIRTUAL:
9818 /* If the object for the method call is null, we throw an
9819 exception. We don't do this if the object is the current
9820 method's `this'. In other cases we just rely on an
9821 optimization pass to eliminate redundant checks. */
9822 if (TREE_VALUE (args) != current_this)
9823 {
9824 /* We use a SAVE_EXPR here to make sure we only evaluate
9825 the new `self' expression once. */
9826 tree save_arg = save_expr (TREE_VALUE (args));
9827 TREE_VALUE (args) = save_arg;
9828 cond = build (EQ_EXPR, boolean_type_node, save_arg,
9829 null_pointer_node);
9830 }
9831 /* Fall through. */
9832
15fdcfe9
PB
9833 case INVOKE_SUPER:
9834 case INVOKE_STATIC:
9835 func = build_known_method_ref (method, TREE_TYPE (method),
9836 DECL_CONTEXT (method),
9837 signature, args);
9838 break;
e04a16fb 9839
15fdcfe9
PB
9840 case INVOKE_INTERFACE:
9841 dtable = invoke_build_dtable (1, args);
173f556c 9842 func = build_invokeinterface (dtable, method);
15fdcfe9 9843 break;
5e942c50 9844
15fdcfe9 9845 default:
89e09b9a 9846 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
9847 }
9848
9849 /* Ensure self_type is initialized, (invokestatic). FIXME */
9850 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
9851 }
9852
e04a16fb
AG
9853 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
9854 TREE_OPERAND (patch, 0) = func;
9855 TREE_OPERAND (patch, 1) = args;
9856 original_call = patch;
9857
e815887f 9858 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
9859 returned value is the first argument to the constructor. We build
9860 a COMPOUND_EXPR and use saved expression so that the overall NEW
9861 expression value is a pointer to a newly created and initialized
9862 class. */
9863 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
9864 {
9865 tree class = DECL_CONTEXT (method);
9866 tree c1, saved_new, size, new;
e8fc7396 9867 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9868 {
9869 TREE_TYPE (patch) = build_pointer_type (class);
9870 return patch;
9871 }
e04a16fb
AG
9872 if (!TYPE_SIZE (class))
9873 safe_layout_class (class);
9874 size = size_in_bytes (class);
9875 new = build (CALL_EXPR, promote_type (class),
9876 build_address_of (alloc_object_node),
9877 tree_cons (NULL_TREE, build_class_ref (class),
9878 build_tree_list (NULL_TREE,
9879 size_in_bytes (class))),
9880 NULL_TREE);
9881 saved_new = save_expr (new);
9882 c1 = build_tree_list (NULL_TREE, saved_new);
9883 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
9884 TREE_OPERAND (original_call, 1) = c1;
9885 TREE_SET_CODE (original_call, CALL_EXPR);
9886 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
9887 }
e815887f
TT
9888
9889 /* If COND is set, then we are building a check to see if the object
9890 is NULL. */
9891 if (cond != NULL_TREE)
9892 {
9893 /* We have to make the `then' branch a compound expression to
9894 make the types turn out right. This seems bizarre. */
9895 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
9896 build (COMPOUND_EXPR, TREE_TYPE (patch),
9897 build (CALL_EXPR, void_type_node,
9898 build_address_of (soft_nullpointer_node),
9899 NULL_TREE, NULL_TREE),
9900 (FLOAT_TYPE_P (TREE_TYPE (patch))
9901 ? build_real (TREE_TYPE (patch), dconst0)
9902 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
9903 integer_zero_node))),
9904 patch);
9905 TREE_SIDE_EFFECTS (patch) = 1;
9906 }
9907
e04a16fb
AG
9908 return patch;
9909}
9910
9911static int
9912invocation_mode (method, super)
9913 tree method;
9914 int super;
9915{
9916 int access = get_access_flags_from_decl (method);
9917
22eed1e6
APB
9918 if (super)
9919 return INVOKE_SUPER;
9920
e815887f 9921 if (access & ACC_STATIC)
e04a16fb
AG
9922 return INVOKE_STATIC;
9923
e815887f
TT
9924 /* We have to look for a constructor before we handle nonvirtual
9925 calls; otherwise the constructor will look nonvirtual. */
9926 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 9927 return INVOKE_STATIC;
e815887f
TT
9928
9929 if (access & ACC_FINAL || access & ACC_PRIVATE)
9930 return INVOKE_NONVIRTUAL;
9931
9932 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
9933 return INVOKE_NONVIRTUAL;
9934
e04a16fb
AG
9935 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
9936 return INVOKE_INTERFACE;
22eed1e6 9937
e04a16fb
AG
9938 return INVOKE_VIRTUAL;
9939}
9940
b67d701b
PB
9941/* Retrieve a refined list of matching methods. It covers the step
9942 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
9943
9944static tree
9945lookup_method_invoke (lc, cl, class, name, arg_list)
9946 int lc;
9947 tree cl;
9948 tree class, name, arg_list;
9949{
de4c7b02 9950 tree atl = end_params_node; /* Arg Type List */
c877974e 9951 tree method, signature, list, node;
49f48c71 9952 const char *candidates; /* Used for error report */
b5b8a0e7 9953 char *dup;
e04a16fb 9954
5e942c50 9955 /* Fix the arguments */
e04a16fb
AG
9956 for (node = arg_list; node; node = TREE_CHAIN (node))
9957 {
e3884b71 9958 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 9959 /* Non primitive type may have to be resolved */
e3884b71 9960 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
9961 resolve_and_layout (current_arg, NULL_TREE);
9962 /* And promoted */
b67d701b 9963 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 9964 current_arg = promote_type (current_arg);
5e942c50 9965 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 9966 }
e04a16fb 9967
c2952b01
APB
9968 /* Presto. If we're dealing with an anonymous class and a
9969 constructor call, generate the right constructor now, since we
9970 know the arguments' types. */
9971
9972 if (lc && ANONYMOUS_CLASS_P (class))
9973 craft_constructor (TYPE_NAME (class), atl);
9974
5e942c50
APB
9975 /* Find all candidates and then refine the list, searching for the
9976 most specific method. */
9977 list = find_applicable_accessible_methods_list (lc, class, name, atl);
9978 list = find_most_specific_methods_list (list);
b67d701b
PB
9979 if (list && !TREE_CHAIN (list))
9980 return TREE_VALUE (list);
e04a16fb 9981
b67d701b
PB
9982 /* Issue an error. List candidates if any. Candidates are listed
9983 only if accessible (non accessible methods may end-up here for
9984 the sake of a better error report). */
9985 candidates = NULL;
9986 if (list)
e04a16fb 9987 {
e04a16fb 9988 tree current;
b67d701b 9989 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
9990 for (current = list; current; current = TREE_CHAIN (current))
9991 {
b67d701b
PB
9992 tree cm = TREE_VALUE (current);
9993 char string [4096];
9994 if (!cm || not_accessible_p (class, cm, 0))
9995 continue;
b67d701b 9996 sprintf
22eed1e6
APB
9997 (string, " `%s' in `%s'%s",
9998 get_printable_method_name (cm),
b67d701b
PB
9999 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10000 (TREE_CHAIN (current) ? "\n" : ""));
10001 obstack_grow (&temporary_obstack, string, strlen (string));
10002 }
10003 obstack_1grow (&temporary_obstack, '\0');
10004 candidates = obstack_finish (&temporary_obstack);
10005 }
10006 /* Issue the error message */
c877974e
APB
10007 method = make_node (FUNCTION_TYPE);
10008 TYPE_ARG_TYPES (method) = atl;
b67d701b 10009 signature = build_java_argument_signature (method);
c63b98cd 10010 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10011 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10012 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10013 (lc ? dup : IDENTIFIER_POINTER (name)),
10014 IDENTIFIER_POINTER (signature), dup,
b67d701b 10015 (candidates ? candidates : ""));
b5b8a0e7 10016 free (dup);
b67d701b
PB
10017 return NULL_TREE;
10018}
10019
5e942c50
APB
10020/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10021 when we're looking for a constructor. */
b67d701b
PB
10022
10023static tree
5e942c50
APB
10024find_applicable_accessible_methods_list (lc, class, name, arglist)
10025 int lc;
b67d701b
PB
10026 tree class, name, arglist;
10027{
165f37bc 10028 static int object_done = 0;
b67d701b
PB
10029 tree list = NULL_TREE, all_list = NULL_TREE;
10030
c2952b01
APB
10031 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10032 {
10033 load_class (class, 1);
10034 safe_layout_class (class);
10035 }
10036
1982388a 10037 /* Search interfaces */
165f37bc 10038 if (CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10039 {
e0422ed0 10040 static struct hash_table t, *searched_interfaces = NULL;
de0b553f 10041 static int search_not_done = 0;
1982388a
APB
10042 int i, n;
10043 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10044
e0422ed0
APB
10045 /* Search in the hash table, otherwise create a new one if
10046 necessary and insert the new entry. */
10047
de0b553f 10048 if (searched_interfaces)
e0422ed0
APB
10049 {
10050 if (hash_lookup (searched_interfaces,
10051 (const hash_table_key) class, FALSE, NULL))
10052 return NULL;
de0b553f 10053 }
e0422ed0
APB
10054 else
10055 {
10056 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10057 java_hash_compare_tree_node);
10058 searched_interfaces = &t;
10059 }
10060
10061 hash_lookup (searched_interfaces,
10062 (const hash_table_key) class, TRUE, NULL);
de0b553f 10063
165f37bc
APB
10064 search_applicable_methods_list (lc, TYPE_METHODS (class),
10065 name, arglist, &list, &all_list);
1982388a 10066 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10067 for (i = 1; i < n; i++)
b67d701b 10068 {
de0b553f
APB
10069 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10070 tree rlist;
10071
de0b553f
APB
10072 search_not_done++;
10073 rlist = find_applicable_accessible_methods_list (lc, t, name,
10074 arglist);
165f37bc 10075 list = chainon (rlist, list);
de0b553f
APB
10076 search_not_done--;
10077 }
10078
10079 /* We're done. Reset the searched interfaces list and finally search
10080 java.lang.Object */
10081 if (!search_not_done)
10082 {
165f37bc
APB
10083 if (!object_done)
10084 search_applicable_methods_list (lc,
10085 TYPE_METHODS (object_type_node),
10086 name, arglist, &list, &all_list);
e0422ed0
APB
10087 hash_table_free (searched_interfaces);
10088 searched_interfaces = NULL;
e04a16fb 10089 }
e04a16fb 10090 }
1982388a
APB
10091 /* Search classes */
10092 else
c2952b01 10093 {
165f37bc
APB
10094 tree sc = class;
10095 int seen_inner_class = 0;
c2952b01
APB
10096 search_applicable_methods_list (lc, TYPE_METHODS (class),
10097 name, arglist, &list, &all_list);
10098
165f37bc
APB
10099 /* We must search all interfaces of this class */
10100 if (!lc)
10101 {
10102 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10103 int n = TREE_VEC_LENGTH (basetype_vec), i;
10104 object_done = 1;
10105 for (i = 1; i < n; i++)
10106 {
10107 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10108 if (t != object_type_node)
30a3caef
ZW
10109 {
10110 tree rlist
10111 = find_applicable_accessible_methods_list (lc, t,
10112 name, arglist);
10113 list = chainon (rlist, list);
10114 }
165f37bc
APB
10115 }
10116 object_done = 0;
10117 }
10118
c2952b01
APB
10119 /* Search enclosing context of inner classes before looking
10120 ancestors up. */
10121 while (!lc && INNER_CLASS_TYPE_P (class))
10122 {
165f37bc
APB
10123 tree rlist;
10124 seen_inner_class = 1;
c2952b01 10125 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10126 rlist = find_applicable_accessible_methods_list (lc, class,
10127 name, arglist);
10128 list = chainon (rlist, list);
c2952b01 10129 }
165f37bc
APB
10130
10131 if (!lc && seen_inner_class
10132 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10133 class = CLASSTYPE_SUPER (sc);
10134 else
10135 class = sc;
10136
10137 for (class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
10138 class; class = CLASSTYPE_SUPER (class))
10139 search_applicable_methods_list (lc, TYPE_METHODS (class),
10140 name, arglist, &list, &all_list);
c2952b01 10141 }
1982388a 10142
b67d701b
PB
10143 /* Either return the list obtained or all selected (but
10144 inaccessible) methods for better error report. */
10145 return (!list ? all_list : list);
10146}
e04a16fb 10147
1982388a
APB
10148/* Effectively search for the approriate method in method */
10149
10150static void
c2952b01 10151search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10152 int lc;
10153 tree method, name, arglist;
10154 tree *list, *all_list;
10155{
10156 for (; method; method = TREE_CHAIN (method))
10157 {
10158 /* When dealing with constructor, stop here, otherwise search
10159 other classes */
10160 if (lc && !DECL_CONSTRUCTOR_P (method))
10161 continue;
10162 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10163 || (GET_METHOD_NAME (method) != name)))
10164 continue;
10165
10166 if (argument_types_convertible (method, arglist))
10167 {
10168 /* Retain accessible methods only */
10169 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10170 method, 0))
10171 *list = tree_cons (NULL_TREE, method, *list);
10172 else
10173 /* Also retain all selected method here */
10174 *all_list = tree_cons (NULL_TREE, method, *list);
10175 }
10176 }
10177}
10178
b67d701b
PB
10179/* 15.11.2.2 Choose the Most Specific Method */
10180
10181static tree
10182find_most_specific_methods_list (list)
10183 tree list;
10184{
10185 int max = 0;
10186 tree current, new_list = NULL_TREE;
10187 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10188 {
b67d701b
PB
10189 tree method;
10190 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10191
10192 for (method = list; method; method = TREE_CHAIN (method))
10193 {
10194 /* Don't test a method against itself */
10195 if (method == current)
10196 continue;
10197
10198 /* Compare arguments and location where method where declared */
10199 if (argument_types_convertible (TREE_VALUE (method),
10200 TREE_VALUE (current))
10201 && valid_method_invocation_conversion_p
10202 (DECL_CONTEXT (TREE_VALUE (method)),
10203 DECL_CONTEXT (TREE_VALUE (current))))
10204 {
10205 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
10206 max = (v > max ? v : max);
10207 }
10208 }
e04a16fb
AG
10209 }
10210
b67d701b
PB
10211 /* Review the list and select the maximally specific methods */
10212 for (current = list; current; current = TREE_CHAIN (current))
10213 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10214 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10215
165f37bc
APB
10216 /* If we have several and they're all abstract, just pick the
10217 closest one. */
10218
10219 if (new_list && TREE_CHAIN (new_list))
10220 {
10221 tree c;
10222 for (c = new_list; c && METHOD_ABSTRACT (TREE_VALUE (c));
10223 c = TREE_CHAIN (c))
10224 ;
10225 if (!c)
10226 {
10227 new_list = nreverse (new_list);
10228 TREE_CHAIN (new_list) = NULL_TREE;
10229 }
10230 }
10231
b67d701b
PB
10232 /* If we can't find one, lower expectations and try to gather multiple
10233 maximally specific methods */
165f37bc 10234 while (!new_list && max)
b67d701b
PB
10235 {
10236 while (--max > 0)
10237 {
10238 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10239 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10240 }
b67d701b
PB
10241 }
10242
10243 return new_list;
e04a16fb
AG
10244}
10245
b67d701b
PB
10246/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10247 converted by method invocation conversion (5.3) to the type of the
10248 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10249 to change less often than M1. */
e04a16fb 10250
b67d701b
PB
10251static int
10252argument_types_convertible (m1, m2_or_arglist)
10253 tree m1, m2_or_arglist;
e04a16fb 10254{
b67d701b
PB
10255 static tree m2_arg_value = NULL_TREE;
10256 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10257
b67d701b 10258 register tree m1_arg, m2_arg;
e04a16fb 10259
c2952b01 10260 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10261
b67d701b
PB
10262 if (m2_arg_value == m2_or_arglist)
10263 m2_arg = m2_arg_cache;
10264 else
10265 {
10266 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10267 argument types */
10268 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10269 {
10270 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10271 if (!METHOD_STATIC (m2_or_arglist))
10272 m2_arg = TREE_CHAIN (m2_arg);
10273 }
10274 else
10275 m2_arg = m2_or_arglist;
e04a16fb 10276
b67d701b
PB
10277 m2_arg_value = m2_or_arglist;
10278 m2_arg_cache = m2_arg;
10279 }
e04a16fb 10280
de4c7b02 10281 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10282 {
c877974e 10283 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10284 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10285 TREE_VALUE (m2_arg)))
10286 break;
10287 m1_arg = TREE_CHAIN (m1_arg);
10288 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10289 }
de4c7b02 10290 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10291}
10292
10293/* Qualification routines */
10294
10295static void
10296qualify_ambiguous_name (id)
10297 tree id;
10298{
cd531a2e
KG
10299 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10300 saved_current_class;
d8fccff5 10301 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10302 int code;
e04a16fb
AG
10303
10304 /* We first qualify the first element, then derive qualification of
10305 others based on the first one. If the first element is qualified
10306 by a resolution (field or type), this resolution is stored in the
10307 QUAL_RESOLUTION of the qual element being examined. We need to
10308 save the current_class since the use of SUPER might change the
10309 its value. */
10310 saved_current_class = current_class;
10311 qual = EXPR_WFL_QUALIFICATION (id);
10312 do {
10313
10314 /* Simple qualified expression feature a qual_wfl that is a
10315 WFL. Expression derived from a primary feature more complicated
10316 things like a CALL_EXPR. Expression from primary need to be
10317 worked out to extract the part on which the qualification will
10318 take place. */
10319 qual_wfl = QUAL_WFL (qual);
10320 switch (TREE_CODE (qual_wfl))
10321 {
10322 case CALL_EXPR:
10323 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10324 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10325 {
10326 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10327 qual_wfl = QUAL_WFL (qual);
10328 }
10329 break;
d8fccff5 10330 case NEW_ARRAY_EXPR:
c2952b01 10331 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10332 qual = TREE_CHAIN (qual);
1a6d4fb7 10333 again = new_array_found = 1;
d8fccff5 10334 continue;
e04a16fb 10335 case CONVERT_EXPR:
f2760b27
APB
10336 break;
10337 case NEW_CLASS_EXPR:
e04a16fb
AG
10338 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10339 break;
c583dd46
APB
10340 case ARRAY_REF:
10341 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10342 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10343 break;
8576f094
APB
10344 case STRING_CST:
10345 qual = TREE_CHAIN (qual);
10346 qual_wfl = QUAL_WFL (qual);
10347 break;
165f37bc
APB
10348 case CLASS_LITERAL:
10349 qual = TREE_CHAIN (qual);
10350 qual_wfl = QUAL_WFL (qual);
10351 break;
0a2138e2
APB
10352 default:
10353 /* Fix for -Wall. Just break doing nothing */
10354 break;
e04a16fb 10355 }
8576f094 10356
e04a16fb
AG
10357 ptr_type = current_class;
10358 again = 0;
8576f094
APB
10359 code = TREE_CODE (qual_wfl);
10360
10361 /* Pos evaluation: non WFL leading expression nodes */
10362 if (code == CONVERT_EXPR
10363 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10364 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10365
cd7c5840
APB
10366 else if (code == INTEGER_CST)
10367 name = qual_wfl;
10368
ac22f9cb 10369 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10370 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10371 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10372
c2952b01
APB
10373 else if (code == TREE_LIST)
10374 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10375
37feda7d
APB
10376 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10377 || code == PLUS_EXPR)
8576f094
APB
10378 {
10379 qual = TREE_CHAIN (qual);
10380 qual_wfl = QUAL_WFL (qual);
10381 again = 1;
10382 }
10383 else
f441f671
APB
10384 {
10385 name = EXPR_WFL_NODE (qual_wfl);
10386 if (!name)
10387 {
10388 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10389 again = 1;
10390 }
10391 }
10392
e04a16fb
AG
10393 /* If we have a THIS (from a primary), we set the context accordingly */
10394 if (name == this_identifier_node)
10395 {
10396 qual = TREE_CHAIN (qual);
10397 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10398 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10399 again = 1;
10400 else
10401 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10402 this_found = 1;
10403 }
10404 /* If we have a SUPER, we set the context accordingly */
10405 if (name == super_identifier_node)
10406 {
10407 current_class = CLASSTYPE_SUPER (ptr_type);
10408 /* Check that there is such a thing as a super class. If not,
10409 return. The error will be caught later on, during the
10410 resolution */
10411 if (!current_class)
10412 {
10413 current_class = saved_current_class;
10414 return;
10415 }
10416 qual = TREE_CHAIN (qual);
10417 /* Do one more interation to set things up */
10418 super_found = again = 1;
10419 }
10420 } while (again);
10421
f2760b27
APB
10422 /* If name appears within the scope of a local variable declaration
10423 or parameter declaration, then it is an expression name. We don't
10424 carry this test out if we're in the context of the use of SUPER
10425 or THIS */
cd7c5840
APB
10426 if (!this_found && !super_found
10427 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10428 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10429 {
10430 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10431 QUAL_RESOLUTION (qual) = decl;
10432 }
10433
10434 /* If within the class/interface NAME was found to be used there
10435 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10436 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10437 address length, it is OK. */
10438 else if ((decl = lookup_field_wrapper (ptr_type, name))
10439 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10440 {
10441 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10442 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10443 }
10444
1a6d4fb7 10445 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10446 - NAME is a class/interface declared within the compilation
10447 unit containing NAME,
10448 - NAME is imported via a single-type-import declaration,
10449 - NAME is declared in an another compilation unit of the package
10450 of the compilation unit containing NAME,
10451 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10452 of the compilation unit containing NAME.
10453 - NAME is actually a STRING_CST. */
cd7c5840
APB
10454 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10455 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10456 {
10457 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10458 QUAL_RESOLUTION (qual) = decl;
10459 }
10460
f2760b27 10461 /* Method call, array references and cast are expression name */
9bbc7d9f 10462 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10463 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10464 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10465 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10466
10467 /* Check here that NAME isn't declared by more than one
10468 type-import-on-demand declaration of the compilation unit
10469 containing NAME. FIXME */
10470
10471 /* Otherwise, NAME is reclassified as a package name */
10472 else
10473 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10474
10475 /* Propagate the qualification accross other components of the
10476 qualified name */
10477 for (qual = TREE_CHAIN (qual); qual;
10478 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10479 {
10480 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10481 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10482 else
10483 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10484 }
10485
10486 /* Store the global qualification for the ambiguous part of ID back
10487 into ID fields */
10488 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10489 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10490 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10491 RESOLVE_TYPE_NAME_P (id) = 1;
10492 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10493 RESOLVE_PACKAGE_NAME_P (id) = 1;
10494
10495 /* Restore the current class */
10496 current_class = saved_current_class;
10497}
10498
10499static int
10500breakdown_qualified (left, right, source)
10501 tree *left, *right, source;
10502{
10503 char *p = IDENTIFIER_POINTER (source), *base;
10504 int l = IDENTIFIER_LENGTH (source);
10505
10506 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10507 base = p;
10508 p += (l-1);
10509 while (*p != '.' && p != base)
10510 p--;
10511
10512 /* We didn't find a '.'. Return an error */
10513 if (p == base)
10514 return 1;
10515
10516 *p = '\0';
10517 if (right)
10518 *right = get_identifier (p+1);
10519 *left = get_identifier (IDENTIFIER_POINTER (source));
10520 *p = '.';
10521
10522 return 0;
10523}
10524
e04a16fb 10525/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10526 local variable decls if present.
10527 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10528
10529static tree
10530java_complete_tree (node)
10531 tree node;
5b09b33e
PB
10532{
10533 node = java_complete_lhs (node);
10534 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7f10c2e2
APB
10535 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE
10536 && !flag_emit_xref)
5b09b33e
PB
10537 {
10538 tree value = DECL_INITIAL (node);
10539 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10540 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10541 value = fold_constant_for_init (value, node);
100f7cd8 10542 pop_obstacks ();
5b09b33e
PB
10543 DECL_INITIAL (node) = value;
10544 if (value != NULL_TREE)
c2952b01
APB
10545 {
10546 /* fold_constant_for_init sometimes widen the original type
10547 of the constant (i.e. byte to int.) It's not desirable,
10548 especially if NODE is a function argument. */
10549 if (TREE_CODE (value) == INTEGER_CST
10550 && TREE_TYPE (node) != TREE_TYPE (value))
10551 return convert (TREE_TYPE (node), value);
10552 else
10553 return value;
10554 }
5b09b33e
PB
10555 }
10556 return node;
10557}
10558
2aa11e97
APB
10559static tree
10560java_stabilize_reference (node)
10561 tree node;
10562{
10563 if (TREE_CODE (node) == COMPOUND_EXPR)
10564 {
10565 tree op0 = TREE_OPERAND (node, 0);
10566 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10567 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10568 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10569 return node;
10570 }
5cbdba64 10571 return stabilize_reference (node);
2aa11e97
APB
10572}
10573
5b09b33e
PB
10574/* Patch tree nodes in a function body. When a BLOCK is found, push
10575 local variable decls if present.
10576 Same as java_complete_tree, but does not resolve static finals to values. */
10577
10578static tree
10579java_complete_lhs (node)
10580 tree node;
e04a16fb 10581{
22eed1e6 10582 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10583 int flag;
e04a16fb
AG
10584
10585 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10586 worked out. */
e04a16fb
AG
10587 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10588 return node;
10589
10590 /* The switch block implements cases processing container nodes
10591 first. Contained nodes are always written back. Leaves come
10592 next and return a value. */
10593 switch (TREE_CODE (node))
10594 {
10595 case BLOCK:
10596
10597 /* 1- Block section.
10598 Set the local values on decl names so we can identify them
10599 faster when they're referenced. At that stage, identifiers
10600 are legal so we don't check for declaration errors. */
10601 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10602 {
10603 DECL_CONTEXT (cn) = current_function_decl;
10604 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10605 }
15fdcfe9
PB
10606 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10607 CAN_COMPLETE_NORMALLY (node) = 1;
10608 else
e04a16fb 10609 {
15fdcfe9
PB
10610 tree stmt = BLOCK_EXPR_BODY (node);
10611 tree *ptr;
10612 int error_seen = 0;
10613 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10614 {
c877974e
APB
10615 /* Re-order from (((A; B); C); ...; Z) to
10616 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10617 This makes it easier to scan the statements left-to-right
10618 without using recursion (which might overflow the stack
10619 if the block has many statements. */
10620 for (;;)
10621 {
10622 tree left = TREE_OPERAND (stmt, 0);
10623 if (TREE_CODE (left) != COMPOUND_EXPR)
10624 break;
10625 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10626 TREE_OPERAND (left, 1) = stmt;
10627 stmt = left;
10628 }
10629 BLOCK_EXPR_BODY (node) = stmt;
10630 }
10631
c877974e
APB
10632 /* Now do the actual complete, without deep recursion for
10633 long blocks. */
15fdcfe9 10634 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10635 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10636 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10637 {
10638 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10639 tree *next = &TREE_OPERAND (*ptr, 1);
10640 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10641 if (cur == empty_stmt_node)
10642 {
10643 /* Optimization; makes it easier to detect empty bodies.
10644 Most useful for <clinit> with all-constant initializer. */
10645 *ptr = *next;
10646 continue;
10647 }
15fdcfe9
PB
10648 if (TREE_CODE (cur) == ERROR_MARK)
10649 error_seen++;
10650 else if (! CAN_COMPLETE_NORMALLY (cur))
10651 {
10652 wfl_op2 = *next;
10653 for (;;)
10654 {
10655 if (TREE_CODE (wfl_op2) == BLOCK)
10656 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10657 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10658 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10659 else
10660 break;
10661 }
10662 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10663 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10664 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10665 }
10666 ptr = next;
10667 }
10668 *ptr = java_complete_tree (*ptr);
10669
10670 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10671 return error_mark_node;
15fdcfe9 10672 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10673 }
10674 /* Turn local bindings to null */
10675 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10676 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10677
10678 TREE_TYPE (node) = void_type_node;
10679 break;
10680
10681 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10682
b9f7e36c
APB
10683 case THROW_EXPR:
10684 wfl_op1 = TREE_OPERAND (node, 0);
10685 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10686 /* 14.19 A throw statement cannot complete normally. */
10687 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10688 return patch_throw_statement (node, wfl_op1);
10689
10690 case SYNCHRONIZED_EXPR:
10691 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10692 return patch_synchronized_statement (node, wfl_op1);
10693
b67d701b
PB
10694 case TRY_EXPR:
10695 return patch_try_statement (node);
10696
a7d8d81f
PB
10697 case TRY_FINALLY_EXPR:
10698 COMPLETE_CHECK_OP_0 (node);
10699 COMPLETE_CHECK_OP_1 (node);
10700 CAN_COMPLETE_NORMALLY (node)
10701 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10702 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10703 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10704 return node;
10705
5a005d9e
PB
10706 case CLEANUP_POINT_EXPR:
10707 COMPLETE_CHECK_OP_0 (node);
10708 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10709 CAN_COMPLETE_NORMALLY (node) =
10710 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10711 return node;
10712
10713 case WITH_CLEANUP_EXPR:
10714 COMPLETE_CHECK_OP_0 (node);
10715 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10716 CAN_COMPLETE_NORMALLY (node) =
10717 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10718 TREE_TYPE (node) = void_type_node;
10719 return node;
10720
e04a16fb
AG
10721 case LABELED_BLOCK_EXPR:
10722 PUSH_LABELED_BLOCK (node);
10723 if (LABELED_BLOCK_BODY (node))
10724 COMPLETE_CHECK_OP_1 (node);
10725 TREE_TYPE (node) = void_type_node;
10726 POP_LABELED_BLOCK ();
1fb89a4d
APB
10727
10728 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10729 {
10730 LABELED_BLOCK_BODY (node) = NULL_TREE;
10731 CAN_COMPLETE_NORMALLY (node) = 1;
10732 }
1fb89a4d 10733 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10734 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10735 return node;
10736
10737 case EXIT_BLOCK_EXPR:
10738 /* We don't complete operand 1, because it's the return value of
10739 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10740 return patch_bc_statement (node);
10741
15fdcfe9
PB
10742 case CASE_EXPR:
10743 cn = java_complete_tree (TREE_OPERAND (node, 0));
10744 if (cn == error_mark_node)
10745 return cn;
10746
8576f094
APB
10747 /* First, the case expression must be constant. Values of final
10748 fields are accepted. */
15fdcfe9 10749 cn = fold (cn);
8576f094
APB
10750 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10751 && JDECL_P (TREE_OPERAND (cn, 1))
10752 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10753 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
10754 {
10755 push_obstacks (&permanent_obstack, &permanent_obstack);
10756 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10757 TREE_OPERAND (cn, 1));
10758 pop_obstacks ();
10759 }
15fdcfe9 10760
ce6e9147 10761 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
10762 {
10763 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10764 parse_error_context (node, "Constant expression required");
10765 return error_mark_node;
10766 }
10767
10768 nn = ctxp->current_loop;
10769
10770 /* It must be assignable to the type of the switch expression. */
c877974e
APB
10771 if (!try_builtin_assignconv (NULL_TREE,
10772 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
10773 {
10774 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10775 parse_error_context
10776 (wfl_operator,
10777 "Incompatible type for case. Can't convert `%s' to `int'",
10778 lang_printable_name (TREE_TYPE (cn), 0));
10779 return error_mark_node;
10780 }
10781
10782 cn = fold (convert (int_type_node, cn));
10783
10784 /* Multiple instance of a case label bearing the same
10785 value is checked during code generation. The case
10786 expression is allright so far. */
10787 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 10788 TREE_TYPE (node) = void_type_node;
15fdcfe9 10789 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 10790 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10791 break;
10792
10793 case DEFAULT_EXPR:
10794 nn = ctxp->current_loop;
10795 /* Only one default label is allowed per switch statement */
10796 if (SWITCH_HAS_DEFAULT (nn))
10797 {
10798 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10799 parse_error_context (wfl_operator,
10800 "Duplicate case label: `default'");
10801 return error_mark_node;
10802 }
10803 else
10804 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 10805 TREE_TYPE (node) = void_type_node;
10100cc7 10806 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10807 CAN_COMPLETE_NORMALLY (node) = 1;
10808 break;
10809
b67d701b 10810 case SWITCH_EXPR:
e04a16fb
AG
10811 case LOOP_EXPR:
10812 PUSH_LOOP (node);
10813 /* Check whether the loop was enclosed in a labeled
10814 statement. If not, create one, insert the loop in it and
10815 return the node */
10816 nn = patch_loop_statement (node);
b67d701b 10817
e04a16fb 10818 /* Anyways, walk the body of the loop */
b67d701b
PB
10819 if (TREE_CODE (node) == LOOP_EXPR)
10820 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10821 /* Switch statement: walk the switch expression and the cases */
10822 else
10823 node = patch_switch_statement (node);
10824
e04a16fb 10825 if (TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
10826 nn = error_mark_node;
10827 else
15fdcfe9 10828 {
b635eb2f
PB
10829 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
10830 /* If we returned something different, that's because we
10831 inserted a label. Pop the label too. */
10832 if (nn != node)
10833 {
10834 if (CAN_COMPLETE_NORMALLY (node))
10835 CAN_COMPLETE_NORMALLY (nn) = 1;
10836 POP_LABELED_BLOCK ();
10837 }
15fdcfe9 10838 }
e04a16fb
AG
10839 POP_LOOP ();
10840 return nn;
10841
10842 case EXIT_EXPR:
10843 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10844 return patch_exit_expr (node);
10845
10846 case COND_EXPR:
10847 /* Condition */
10848 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10849 if (TREE_OPERAND (node, 0) == error_mark_node)
10850 return error_mark_node;
10851 /* then-else branches */
10852 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10853 if (TREE_OPERAND (node, 1) == error_mark_node)
10854 return error_mark_node;
10855 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
10856 if (TREE_OPERAND (node, 2) == error_mark_node)
10857 return error_mark_node;
10858 return patch_if_else_statement (node);
10859 break;
10860
22eed1e6
APB
10861 case CONDITIONAL_EXPR:
10862 /* Condition */
10863 wfl_op1 = TREE_OPERAND (node, 0);
10864 COMPLETE_CHECK_OP_0 (node);
10865 wfl_op2 = TREE_OPERAND (node, 1);
10866 COMPLETE_CHECK_OP_1 (node);
10867 wfl_op3 = TREE_OPERAND (node, 2);
10868 COMPLETE_CHECK_OP_2 (node);
10869 return patch_conditional_expr (node, wfl_op1, wfl_op2);
10870
e04a16fb
AG
10871 /* 3- Expression section */
10872 case COMPOUND_EXPR:
15fdcfe9 10873 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
10874 TREE_OPERAND (node, 0) = nn =
10875 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
10876 if (wfl_op2 == empty_stmt_node)
10877 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
10878 else
15fdcfe9 10879 {
dc0b3eff 10880 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 10881 {
dc0b3eff
PB
10882 /* An unreachable condition in a do-while statement
10883 is *not* (technically) an unreachable statement. */
10884 nn = wfl_op2;
10885 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
10886 nn = EXPR_WFL_NODE (nn);
10887 if (TREE_CODE (nn) != EXIT_EXPR)
10888 {
10889 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
10890 parse_error_context (wfl_operator, "Unreachable statement");
10891 }
bccaf73a 10892 }
dc0b3eff
PB
10893 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10894 if (TREE_OPERAND (node, 1) == error_mark_node)
10895 return error_mark_node;
10896 CAN_COMPLETE_NORMALLY (node)
10897 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 10898 }
e04a16fb
AG
10899 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
10900 break;
10901
10902 case RETURN_EXPR:
15fdcfe9 10903 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
10904 return patch_return (node);
10905
10906 case EXPR_WITH_FILE_LOCATION:
10907 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
10908 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 10909 {
5423609c 10910 tree wfl = node;
15fdcfe9 10911 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
10912 if (node == error_mark_node)
10913 return node;
5423609c
APB
10914 /* Keep line number information somewhere were it doesn't
10915 disrupt the completion process. */
2c56429a 10916 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
10917 {
10918 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
10919 TREE_OPERAND (node, 1) = wfl;
10920 }
15fdcfe9
PB
10921 CAN_COMPLETE_NORMALLY (node) = 1;
10922 }
e04a16fb
AG
10923 else
10924 {
5b09b33e
PB
10925 tree body;
10926 int save_lineno = lineno;
10927 lineno = EXPR_WFL_LINENO (node);
10928 body = java_complete_tree (EXPR_WFL_NODE (node));
10929 lineno = save_lineno;
15fdcfe9 10930 EXPR_WFL_NODE (node) = body;
dc0b3eff 10931 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 10932 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
10933 if (body == empty_stmt_node)
10934 {
10935 /* Optimization; makes it easier to detect empty bodies. */
10936 return body;
10937 }
dc0b3eff 10938 if (body == error_mark_node)
e04a16fb
AG
10939 {
10940 /* Its important for the evaluation of assignment that
10941 this mark on the TREE_TYPE is propagated. */
10942 TREE_TYPE (node) = error_mark_node;
10943 return error_mark_node;
10944 }
10945 else
10946 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 10947
e04a16fb
AG
10948 }
10949 break;
10950
b67d701b 10951 case NEW_ARRAY_EXPR:
e04a16fb
AG
10952 /* Patch all the dimensions */
10953 flag = 0;
10954 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
10955 {
10956 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
10957 tree dim = convert (int_type_node,
10958 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
10959 if (dim == error_mark_node)
10960 {
10961 flag = 1;
10962 continue;
10963 }
10964 else
10965 {
b9f7e36c 10966 TREE_VALUE (cn) = dim;
e04a16fb
AG
10967 /* Setup the location of the current dimension, for
10968 later error report. */
10969 TREE_PURPOSE (cn) =
10970 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
10971 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
10972 }
10973 }
10974 /* They complete the array creation expression, if no errors
10975 were found. */
15fdcfe9 10976 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
10977 return (flag ? error_mark_node
10978 : force_evaluation_order (patch_newarray (node)));
e04a16fb 10979
c2952b01
APB
10980 case NEW_ANONYMOUS_ARRAY_EXPR:
10981 /* Create the array type if necessary. */
10982 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
10983 {
10984 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
10985 if (!(type = resolve_type_during_patch (type)))
10986 return error_mark_node;
10987 type = build_array_from_name (type, NULL_TREE,
10988 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
10989 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
10990 }
10991 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
10992 ANONYMOUS_ARRAY_INITIALIZER (node));
10993 if (node == error_mark_node)
10994 return error_mark_node;
10995 CAN_COMPLETE_NORMALLY (node) = 1;
10996 return node;
10997
b67d701b 10998 case NEW_CLASS_EXPR:
e04a16fb 10999 case CALL_EXPR:
b67d701b 11000 /* Complete function's argument(s) first */
e04a16fb
AG
11001 if (complete_function_arguments (node))
11002 return error_mark_node;
11003 else
b9f7e36c 11004 {
22eed1e6
APB
11005 tree decl, wfl = TREE_OPERAND (node, 0);
11006 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11007
c877974e 11008 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11009 NULL_TREE, 0, &decl);
c877974e
APB
11010 if (node == error_mark_node)
11011 return error_mark_node;
11012
11013 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11014 /* If we call this(...), register signature and positions */
11015 if (in_this)
11016 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11017 tree_cons (wfl, decl,
11018 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11019 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11020 return force_evaluation_order (node);
b9f7e36c 11021 }
e04a16fb
AG
11022
11023 case MODIFY_EXPR:
11024 /* Save potential wfls */
11025 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11026 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11027
cd9643f7
PB
11028 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11029 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11030 && DECL_INITIAL (nn) != NULL_TREE)
11031 {
100f7cd8
APB
11032 tree value;
11033
11034 push_obstacks (&permanent_obstack, &permanent_obstack);
11035 value = fold_constant_for_init (nn, nn);
11036 pop_obstacks ();
c2952b01 11037
cd9643f7
PB
11038 if (value != NULL_TREE)
11039 {
11040 tree type = TREE_TYPE (value);
c2952b01
APB
11041 if (JPRIMITIVE_TYPE_P (type) ||
11042 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11043 return empty_stmt_node;
11044 }
11045 DECL_INITIAL (nn) = NULL_TREE;
11046 }
e04a16fb 11047 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11048
e04a16fb
AG
11049 if (TREE_OPERAND (node, 0) == error_mark_node)
11050 return error_mark_node;
11051
5cbdba64
APB
11052 flag = COMPOUND_ASSIGN_P (wfl_op2);
11053 if (flag)
e04a16fb 11054 {
c2952b01
APB
11055 /* This might break when accessing outer field from inner
11056 class. TESTME, FIXME */
2aa11e97 11057 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11058
11059 /* Hand stablize the lhs on both places */
e04a16fb 11060 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11061 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11062 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11063
5cbdba64 11064 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11065 /* Now complete the RHS. We write it back later on. */
11066 nn = java_complete_tree (TREE_OPERAND (node, 1));
11067
642f15d1
APB
11068 if ((cn = patch_string (nn)))
11069 nn = cn;
11070
2aa11e97
APB
11071 /* The last part of the rewrite for E1 op= E2 is to have
11072 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11073 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11074 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11075
11076 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11077 }
11078
f8976021 11079 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11080 function to complete this RHS. Note that a NEW_ARRAY_INIT
11081 might have been already fully expanded if created as a result
11082 of processing an anonymous array initializer. We avoid doing
11083 the operation twice by testing whether the node already bears
11084 a type. */
11085 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11086 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11087 TREE_OPERAND (node, 1));
2aa11e97 11088 /* Otherwise we simply complete the RHS */
f8976021
APB
11089 else
11090 nn = java_complete_tree (TREE_OPERAND (node, 1));
11091
e04a16fb 11092 if (nn == error_mark_node)
c0d87ff6 11093 return error_mark_node;
2aa11e97
APB
11094
11095 /* Write back the RHS as we evaluated it. */
e04a16fb 11096 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11097
11098 /* In case we're handling = with a String as a RHS, we need to
11099 produce a String out of the RHS (it might still be a
11100 STRING_CST or a StringBuffer at this stage */
11101 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11102 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11103
11104 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11105 TREE_OPERAND (node, 1))))
11106 {
11107 /* We return error_mark_node if outer_field_access_fix
11108 detects we write into a final. */
11109 if (nn == error_mark_node)
11110 return error_mark_node;
11111 node = nn;
11112 }
11113 else
11114 {
11115 node = patch_assignment (node, wfl_op1, wfl_op2);
11116 /* Reorganize the tree if necessary. */
11117 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11118 || JSTRING_P (TREE_TYPE (node))))
11119 node = java_refold (node);
11120 }
11121
15fdcfe9
PB
11122 CAN_COMPLETE_NORMALLY (node) = 1;
11123 return node;
e04a16fb
AG
11124
11125 case MULT_EXPR:
11126 case PLUS_EXPR:
11127 case MINUS_EXPR:
11128 case LSHIFT_EXPR:
11129 case RSHIFT_EXPR:
11130 case URSHIFT_EXPR:
11131 case BIT_AND_EXPR:
11132 case BIT_XOR_EXPR:
11133 case BIT_IOR_EXPR:
11134 case TRUNC_MOD_EXPR:
c2952b01 11135 case TRUNC_DIV_EXPR:
e04a16fb
AG
11136 case RDIV_EXPR:
11137 case TRUTH_ANDIF_EXPR:
11138 case TRUTH_ORIF_EXPR:
11139 case EQ_EXPR:
11140 case NE_EXPR:
11141 case GT_EXPR:
11142 case GE_EXPR:
11143 case LT_EXPR:
11144 case LE_EXPR:
11145 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11146 knows how to handle those cases. */
11147 wfl_op1 = TREE_OPERAND (node, 0);
11148 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11149
15fdcfe9 11150 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11151 /* Don't complete string nodes if dealing with the PLUS operand. */
11152 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11153 {
11154 nn = java_complete_tree (wfl_op1);
11155 if (nn == error_mark_node)
11156 return error_mark_node;
48a840d9 11157
2aa11e97
APB
11158 TREE_OPERAND (node, 0) = nn;
11159 }
b67d701b 11160 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11161 {
11162 nn = java_complete_tree (wfl_op2);
11163 if (nn == error_mark_node)
11164 return error_mark_node;
48a840d9 11165
2aa11e97
APB
11166 TREE_OPERAND (node, 1) = nn;
11167 }
dc0b3eff 11168 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11169
5e942c50
APB
11170 case INSTANCEOF_EXPR:
11171 wfl_op1 = TREE_OPERAND (node, 0);
11172 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11173 if (flag_emit_xref)
11174 {
11175 TREE_TYPE (node) = boolean_type_node;
11176 return node;
11177 }
5e942c50
APB
11178 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11179
b67d701b 11180 case UNARY_PLUS_EXPR:
e04a16fb
AG
11181 case NEGATE_EXPR:
11182 case TRUTH_NOT_EXPR:
11183 case BIT_NOT_EXPR:
11184 case PREDECREMENT_EXPR:
11185 case PREINCREMENT_EXPR:
11186 case POSTDECREMENT_EXPR:
11187 case POSTINCREMENT_EXPR:
11188 case CONVERT_EXPR:
11189 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11190 how to handle those cases. */
11191 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11192 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11193 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11194 if (TREE_OPERAND (node, 0) == error_mark_node)
11195 return error_mark_node;
4a5f66c3
APB
11196 node = patch_unaryop (node, wfl_op1);
11197 CAN_COMPLETE_NORMALLY (node) = 1;
11198 break;
e04a16fb
AG
11199
11200 case ARRAY_REF:
11201 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11202 how to handle those cases. */
11203 wfl_op1 = TREE_OPERAND (node, 0);
11204 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11205 if (TREE_OPERAND (node, 0) == error_mark_node)
11206 return error_mark_node;
7f1d4866 11207 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11208 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11209 /* The same applies to wfl_op2 */
11210 wfl_op2 = TREE_OPERAND (node, 1);
11211 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11212 if (TREE_OPERAND (node, 1) == error_mark_node)
11213 return error_mark_node;
7f1d4866 11214 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11215 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11216 return patch_array_ref (node);
e04a16fb 11217
63a212ed
PB
11218 case RECORD_TYPE:
11219 return node;;
11220
11221 case COMPONENT_REF:
11222 /* The first step in the re-write of qualified name handling. FIXME.
11223 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11224 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11225 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11226 {
11227 tree name = TREE_OPERAND (node, 1);
11228 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11229 if (field == NULL_TREE)
11230 {
11231 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11232 return error_mark_node;
11233 }
11234 if (! FIELD_STATIC (field))
11235 {
11236 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11237 return error_mark_node;
11238 }
11239 return field;
11240 }
11241 else
11242 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11243 break;
9bbc7d9f 11244
b67d701b 11245 case THIS_EXPR:
e04a16fb
AG
11246 /* Can't use THIS in a static environment */
11247 if (!current_this)
11248 {
11249 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11250 parse_error_context (wfl_operator,
11251 "Keyword `this' used outside allowed context");
e04a16fb
AG
11252 TREE_TYPE (node) = error_mark_node;
11253 return error_mark_node;
11254 }
22eed1e6
APB
11255 if (ctxp->explicit_constructor_p)
11256 {
11257 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11258 parse_error_context
781b0558 11259 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11260 TREE_TYPE (node) = error_mark_node;
11261 return error_mark_node;
11262 }
e04a16fb 11263 return current_this;
c2952b01
APB
11264
11265 case CLASS_LITERAL:
11266 CAN_COMPLETE_NORMALLY (node) = 1;
11267 node = patch_incomplete_class_ref (node);
11268 if (node == error_mark_node)
11269 return error_mark_node;
11270 break;
11271
11272 case INSTANCE_INITIALIZERS_EXPR:
11273 in_instance_initializer++;
11274 node = java_complete_tree (TREE_OPERAND (node, 0));
11275 in_instance_initializer--;
11276 if (node != error_mark_node)
11277 TREE_TYPE (node) = void_type_node;
11278 else
11279 return error_mark_node;
11280 break;
e04a16fb 11281
e04a16fb 11282 default:
15fdcfe9 11283 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11284 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11285 and it's time to turn it into the appropriate String object */
11286 if ((nn = patch_string (node)))
11287 node = nn;
11288 else
11289 fatal ("No case for tree code `%s' - java_complete_tree\n",
11290 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11291 }
11292 return node;
11293}
11294
11295/* Complete function call's argument. Return a non zero value is an
11296 error was found. */
11297
11298static int
11299complete_function_arguments (node)
11300 tree node;
11301{
11302 int flag = 0;
11303 tree cn;
11304
f63991a8 11305 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11306 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11307 {
b67d701b 11308 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11309 parm = java_complete_tree (wfl);
c2952b01 11310
e04a16fb
AG
11311 if (parm == error_mark_node)
11312 {
11313 flag = 1;
11314 continue;
11315 }
b67d701b
PB
11316 /* If have a string literal that we haven't transformed yet or a
11317 crafted string buffer, as a result of use of the the String
11318 `+' operator. Build `parm.toString()' and expand it. */
11319 if ((temp = patch_string (parm)))
b9f7e36c 11320 parm = temp;
5e942c50
APB
11321 /* Inline PRIMTYPE.TYPE read access */
11322 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11323
5e942c50 11324 TREE_VALUE (cn) = parm;
e04a16fb 11325 }
f63991a8 11326 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11327 return flag;
11328}
11329
11330/* Sometimes (for loops and variable initialized during their
11331 declaration), we want to wrap a statement around a WFL and turn it
11332 debugable. */
11333
11334static tree
11335build_debugable_stmt (location, stmt)
11336 int location;
11337 tree stmt;
11338{
11339 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11340 {
11341 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11342 EXPR_WFL_LINECOL (stmt) = location;
11343 }
11344 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11345 return stmt;
11346}
11347
11348static tree
11349build_expr_block (body, decls)
11350 tree body, decls;
11351{
11352 tree node = make_node (BLOCK);
11353 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11354 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11355 if (body)
11356 TREE_TYPE (node) = TREE_TYPE (body);
11357 TREE_SIDE_EFFECTS (node) = 1;
11358 return node;
11359}
11360
b67d701b
PB
11361/* Create a new function block and link it approriately to current
11362 function block chain */
e04a16fb
AG
11363
11364static tree
11365enter_block ()
11366{
b67d701b
PB
11367 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11368}
11369
11370/* Link block B supercontext to the previous block. The current
11371 function DECL is used as supercontext when enter_a_block is called
11372 for the first time for a given function. The current function body
11373 (DECL_FUNCTION_BODY) is set to be block B. */
11374
11375static tree
11376enter_a_block (b)
11377 tree b;
11378{
e04a16fb
AG
11379 tree fndecl = current_function_decl;
11380
f099f336
APB
11381 if (!fndecl) {
11382 BLOCK_SUPERCONTEXT (b) = current_static_block;
11383 current_static_block = b;
11384 }
11385
11386 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11387 {
11388 BLOCK_SUPERCONTEXT (b) = fndecl;
11389 DECL_FUNCTION_BODY (fndecl) = b;
11390 }
11391 else
11392 {
11393 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11394 DECL_FUNCTION_BODY (fndecl) = b;
11395 }
11396 return b;
11397}
11398
11399/* Exit a block by changing the current function body
11400 (DECL_FUNCTION_BODY) to the current block super context, only if
11401 the block being exited isn't the method's top level one. */
11402
11403static tree
11404exit_block ()
11405{
f099f336
APB
11406 tree b;
11407 if (current_function_decl)
11408 {
11409 b = DECL_FUNCTION_BODY (current_function_decl);
11410 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11411 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11412 }
11413 else
11414 {
11415 b = current_static_block;
e04a16fb 11416
f099f336
APB
11417 if (BLOCK_SUPERCONTEXT (b))
11418 current_static_block = BLOCK_SUPERCONTEXT (b);
11419 }
e04a16fb
AG
11420 return b;
11421}
11422
11423/* Lookup for NAME in the nested function's blocks, all the way up to
11424 the current toplevel one. It complies with Java's local variable
11425 scoping rules. */
11426
11427static tree
11428lookup_name_in_blocks (name)
11429 tree name;
11430{
f099f336 11431 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11432
11433 while (b != current_function_decl)
11434 {
11435 tree current;
11436
11437 /* Paranoid sanity check. To be removed */
11438 if (TREE_CODE (b) != BLOCK)
11439 fatal ("non block expr function body - lookup_name_in_blocks");
11440
11441 for (current = BLOCK_EXPR_DECLS (b); current;
11442 current = TREE_CHAIN (current))
11443 if (DECL_NAME (current) == name)
11444 return current;
11445 b = BLOCK_SUPERCONTEXT (b);
11446 }
11447 return NULL_TREE;
11448}
11449
11450static void
11451maybe_absorb_scoping_blocks ()
11452{
f099f336 11453 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11454 {
11455 tree b = exit_block ();
11456 java_method_add_stmt (current_function_decl, b);
11457 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11458 }
11459}
11460
11461\f
11462/* This section of the source is reserved to build_* functions that
11463 are building incomplete tree nodes and the patch_* functions that
11464 are completing them. */
11465
c2952b01
APB
11466/* Wrap a non WFL node around a WFL. */
11467static tree
11468build_wfl_wrap (node)
11469 tree node;
11470{
11471 tree wfl, node_to_insert = node;
11472
11473 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11474 with the way we're processing SUPER. A THIS from a primary as a
11475 different form than a SUPER. Turn THIS into something symbolic */
11476 if (TREE_CODE (node) == THIS_EXPR)
11477 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11478 else
11479 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11480
11481 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (node);
11482 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11483 return wfl;
11484}
11485
11486
9bbc7d9f 11487/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11488 we're currently dealing with the class java.lang.Object. */
11489
11490static tree
e920ebc9
APB
11491build_super_invocation (mdecl)
11492 tree mdecl;
22eed1e6 11493{
e920ebc9 11494 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11495 return empty_stmt_node;
22eed1e6
APB
11496 else
11497 {
9ee9b555 11498 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11499 tree a = NULL_TREE, t;
11500 /* If we're dealing with an anonymous class, pass the arguments
11501 of the crafted constructor along. */
11502 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11503 {
11504 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11505 for (; t != end_params_node; t = TREE_CHAIN (t))
11506 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11507 }
11508 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11509 }
11510}
11511
11512/* Build a SUPER/THIS qualified method invocation. */
11513
11514static tree
11515build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11516 int use_this;
11517 tree name, args;
11518 int lloc, rloc;
22eed1e6
APB
11519{
11520 tree invok;
11521 tree wfl =
9ee9b555 11522 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11523 EXPR_WFL_LINECOL (wfl) = lloc;
11524 invok = build_method_invocation (name, args);
11525 return make_qualified_primary (wfl, invok, rloc);
11526}
11527
b67d701b 11528/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11529
11530static tree
11531build_method_invocation (name, args)
11532 tree name;
11533 tree args;
11534{
11535 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11536 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11537 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11538 return call;
11539}
11540
11541/* Build an incomplete new xxx(...) node. */
11542
11543static tree
11544build_new_invocation (name, args)
11545 tree name, args;
11546{
11547 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11548 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11549 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11550 return call;
11551}
11552
11553/* Build an incomplete assignment expression. */
11554
11555static tree
11556build_assignment (op, op_location, lhs, rhs)
11557 int op, op_location;
11558 tree lhs, rhs;
11559{
11560 tree assignment;
11561 /* Build the corresponding binop if we deal with a Compound
11562 Assignment operator. Mark the binop sub-tree as part of a
11563 Compound Assignment expression */
11564 if (op != ASSIGN_TK)
11565 {
11566 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11567 COMPOUND_ASSIGN_P (rhs) = 1;
11568 }
11569 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11570 TREE_SIDE_EFFECTS (assignment) = 1;
11571 EXPR_WFL_LINECOL (assignment) = op_location;
11572 return assignment;
11573}
11574
11575/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11576
15fdcfe9 11577char *
e04a16fb
AG
11578print_int_node (node)
11579 tree node;
11580{
11581 static char buffer [80];
11582 if (TREE_CONSTANT_OVERFLOW (node))
11583 sprintf (buffer, "<overflow>");
11584
11585 if (TREE_INT_CST_HIGH (node) == 0)
11586 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11587 TREE_INT_CST_LOW (node));
11588 else if (TREE_INT_CST_HIGH (node) == -1
11589 && TREE_INT_CST_LOW (node) != 0)
11590 {
11591 buffer [0] = '-';
11592 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11593 -TREE_INT_CST_LOW (node));
11594 }
11595 else
11596 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11597 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11598
11599 return buffer;
11600}
11601
7f1d4866
APB
11602/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11603 context. */
5e942c50
APB
11604
11605static int
11606check_final_assignment (lvalue, wfl)
11607 tree lvalue, wfl;
11608{
6632dcdd
APB
11609 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11610 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11611 lvalue = TREE_OPERAND (lvalue, 1);
11612
bc2874c9
TT
11613 /* When generating class files, references to the `length' field
11614 look a bit different. */
11615 if ((flag_emit_class_files
11616 && TREE_CODE (lvalue) == COMPONENT_REF
11617 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11618 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11619 || (TREE_CODE (lvalue) == FIELD_DECL
11620 && FIELD_FINAL (lvalue)
11621 && !DECL_CLINIT_P (current_function_decl)
11622 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11623 {
11624 parse_error_context
11625 (wfl, "Can't assign a value to the final variable `%s'",
11626 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11627 return 1;
11628 }
11629 return 0;
11630}
11631
11632/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11633 read. This is needed to avoid circularities in the implementation
11634 of these fields in libjava. */
11635
11636static tree
11637maybe_build_primttype_type_ref (rhs, wfl)
11638 tree rhs, wfl;
11639{
11640 tree to_return = NULL_TREE;
11641 tree rhs_type = TREE_TYPE (rhs);
11642 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11643 {
11644 tree n = TREE_OPERAND (rhs, 1);
11645 if (TREE_CODE (n) == VAR_DECL
11646 && DECL_NAME (n) == TYPE_identifier_node
11647 && rhs_type == class_ptr_type)
11648 {
49f48c71 11649 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11650 if (!strncmp (self_name, "java.lang.", 10))
11651 to_return = build_primtype_type_ref (self_name);
11652 }
11653 }
11654 return (to_return ? to_return : rhs );
11655}
11656
e04a16fb
AG
11657/* 15.25 Assignment operators. */
11658
11659static tree
11660patch_assignment (node, wfl_op1, wfl_op2)
11661 tree node;
11662 tree wfl_op1;
11663 tree wfl_op2;
11664{
0a2138e2 11665 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11666 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11667 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11668 int error_found = 0;
11669 int lvalue_from_array = 0;
11670
c2952b01 11671 /* Can't assign to a (blank) final. */
5e942c50
APB
11672 if (check_final_assignment (lvalue, wfl_op1))
11673 error_found = 1;
e04a16fb
AG
11674
11675 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11676
11677 /* Lhs can be a named variable */
34f4db93 11678 if (JDECL_P (lvalue))
e04a16fb 11679 {
e04a16fb
AG
11680 lhs_type = TREE_TYPE (lvalue);
11681 }
11682 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11683 comment on reason why */
11684 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11685 {
11686 lhs_type = TREE_TYPE (lvalue);
11687 lvalue_from_array = 1;
11688 }
11689 /* Or a field access */
11690 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11691 lhs_type = TREE_TYPE (lvalue);
11692 /* Or a function return slot */
11693 else if (TREE_CODE (lvalue) == RESULT_DECL)
11694 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11695 /* Otherwise, we might want to try to write into an optimized static
11696 final, this is an of a different nature, reported further on. */
11697 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11698 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11699 {
6632dcdd 11700 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11701 {
11702 /* What we should do instead is resetting the all the flags
11703 previously set, exchange lvalue for llvalue and continue. */
11704 error_found = 1;
11705 return error_mark_node;
11706 }
11707 else
11708 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11709 }
11710 else
e04a16fb
AG
11711 {
11712 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11713 error_found = 1;
11714 }
11715
11716 rhs_type = TREE_TYPE (rhs);
b67d701b 11717 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11718 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11719
b67d701b 11720 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11721 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11722 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11723
11724 /* 15.25.2 If we have a compound assignment, convert RHS into the
11725 type of the LHS */
11726 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11727 new_rhs = convert (lhs_type, rhs);
11728
11729 /* Explicit cast required. This is an error */
11730 if (!new_rhs)
11731 {
c2e3db92
KG
11732 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11733 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11734 tree wfl;
11735 char operation [32]; /* Max size known */
11736
11737 /* If the assignment is part of a declaration, we use the WFL of
11738 the declared variable to point out the error and call it a
11739 declaration problem. If the assignment is a genuine =
11740 operator, we call is a operator `=' problem, otherwise we
11741 call it an assignment problem. In both of these last cases,
11742 we use the WFL of the operator to indicate the error. */
11743
11744 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11745 {
11746 wfl = wfl_op1;
11747 strcpy (operation, "declaration");
11748 }
11749 else
11750 {
11751 wfl = wfl_operator;
11752 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11753 strcpy (operation, "assignment");
11754 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11755 strcpy (operation, "`return'");
11756 else
11757 strcpy (operation, "`='");
11758 }
11759
1ebadc60 11760 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
11761 parse_error_context
11762 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11763 operation, t1, t2);
1ebadc60 11764 else
781b0558 11765 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 11766 operation, t1, t2);
e04a16fb
AG
11767 free (t1); free (t2);
11768 error_found = 1;
11769 }
11770
c877974e
APB
11771 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11772 if (new_rhs)
11773 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 11774
e04a16fb
AG
11775 if (error_found)
11776 return error_mark_node;
11777
2622b947
APB
11778 /* 10.10: Array Store Exception runtime check */
11779 if (!flag_emit_class_files
e8fc7396 11780 && !flag_emit_xref
2622b947 11781 && lvalue_from_array
afc390b1 11782 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
11783 {
11784 tree check;
11785 tree base = lvalue;
11786
11787 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11788 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11789 base = TREE_OPERAND (lvalue, 0);
11790 else
11791 {
11792 if (flag_bounds_check)
11793 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11794 else
11795 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11796 }
11797
11798 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 11799 new_rhs = save_expr (new_rhs);
2622b947
APB
11800 check = build (CALL_EXPR, void_type_node,
11801 build_address_of (soft_checkarraystore_node),
11802 tree_cons (NULL_TREE, base,
11803 build_tree_list (NULL_TREE, new_rhs)),
11804 NULL_TREE);
11805 TREE_SIDE_EFFECTS (check) = 1;
11806
11807 /* We have to decide on an insertion point */
11808 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11809 {
11810 tree t;
11811 if (flag_bounds_check)
11812 {
11813 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11814 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11815 build (COMPOUND_EXPR, void_type_node, t, check);
11816 }
11817 else
11818 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
11819 check, TREE_OPERAND (lvalue, 1));
11820 }
11821 else
11822 {
11823 /* Make sure the bound check will happen before the store check */
11824 if (flag_bounds_check)
11825 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
11826 build (COMPOUND_EXPR, void_type_node,
11827 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
11828 else
11829 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
11830 }
11831 }
22eed1e6 11832
e04a16fb
AG
11833 TREE_OPERAND (node, 0) = lvalue;
11834 TREE_OPERAND (node, 1) = new_rhs;
11835 TREE_TYPE (node) = lhs_type;
11836 return node;
11837}
11838
b67d701b
PB
11839/* Check that type SOURCE can be cast into type DEST. If the cast
11840 can't occur at all, return 0 otherwise 1. This function is used to
11841 produce accurate error messages on the reasons why an assignment
11842 failed. */
e04a16fb 11843
b67d701b
PB
11844static tree
11845try_reference_assignconv (lhs_type, rhs)
11846 tree lhs_type, rhs;
e04a16fb 11847{
b67d701b
PB
11848 tree new_rhs = NULL_TREE;
11849 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 11850
b67d701b
PB
11851 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
11852 {
11853 /* `null' may be assigned to any reference type */
11854 if (rhs == null_pointer_node)
11855 new_rhs = null_pointer_node;
11856 /* Try the reference assignment conversion */
11857 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
11858 new_rhs = rhs;
11859 /* This is a magic assignment that we process differently */
11860 else if (rhs == soft_exceptioninfo_call_node)
11861 new_rhs = rhs;
11862 }
11863 return new_rhs;
11864}
11865
11866/* Check that RHS can be converted into LHS_TYPE by the assignment
11867 conversion (5.2), for the cases of RHS being a builtin type. Return
11868 NULL_TREE if the conversion fails or if because RHS isn't of a
11869 builtin type. Return a converted RHS if the conversion is possible. */
11870
11871static tree
11872try_builtin_assignconv (wfl_op1, lhs_type, rhs)
11873 tree wfl_op1, lhs_type, rhs;
11874{
11875 tree new_rhs = NULL_TREE;
11876 tree rhs_type = TREE_TYPE (rhs);
11877
5e942c50
APB
11878 /* Zero accepted everywhere */
11879 if (TREE_CODE (rhs) == INTEGER_CST
11880 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
11881 && JPRIMITIVE_TYPE_P (rhs_type))
11882 new_rhs = convert (lhs_type, rhs);
11883
b67d701b
PB
11884 /* 5.1.1 Try Identity Conversion,
11885 5.1.2 Try Widening Primitive Conversion */
5e942c50 11886 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
11887 new_rhs = convert (lhs_type, rhs);
11888
11889 /* Try a narrowing primitive conversion (5.1.3):
11890 - expression is a constant expression of type int AND
11891 - variable is byte, short or char AND
11892 - The value of the expression is representable in the type of the
11893 variable */
11894 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
11895 && (lhs_type == byte_type_node || lhs_type == char_type_node
11896 || lhs_type == short_type_node))
11897 {
11898 if (int_fits_type_p (rhs, lhs_type))
11899 new_rhs = convert (lhs_type, rhs);
11900 else if (wfl_op1) /* Might be called with a NULL */
11901 parse_warning_context
781b0558 11902 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 11903 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
11904 /* Reported a warning that will turn into an error further
11905 down, so we don't return */
11906 }
11907
11908 return new_rhs;
11909}
11910
11911/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
11912 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
11913 0 is the conversion test fails. This implements parts the method
11914 invocation convertion (5.3). */
11915
11916static int
11917valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
11918 tree lhs_type, rhs_type;
11919{
acd663ee 11920 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
11921 if (lhs_type == rhs_type)
11922 return 1;
11923
acd663ee
APB
11924 /* Reject non primitive types */
11925 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
11926 return 0;
11927
acd663ee
APB
11928 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
11929 than a char can't be converted into a char. Short can't too, but
11930 the < test below takes care of that */
b67d701b
PB
11931 if (lhs_type == char_type_node && rhs_type == byte_type_node)
11932 return 0;
11933
5e942c50
APB
11934 /* Accept all promoted type here. Note, we can't use <= in the test
11935 below, because we still need to bounce out assignments of short
11936 to char and the likes */
11937 if (lhs_type == int_type_node
11938 && (rhs_type == promoted_byte_type_node
11939 || rhs_type == promoted_short_type_node
11940 || rhs_type == promoted_char_type_node
11941 || rhs_type == promoted_boolean_type_node))
11942 return 1;
11943
acd663ee
APB
11944 /* From here, an integral is widened if its precision is smaller
11945 than the precision of the LHS or if the LHS is a floating point
11946 type, or the RHS is a float and the RHS a double. */
11947 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
11948 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
11949 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
11950 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
11951 return 1;
11952
11953 return 0;
e04a16fb
AG
11954}
11955
11956/* Check that something of SOURCE type can be assigned or cast to
11957 something of DEST type at runtime. Return 1 if the operation is
11958 valid, 0 otherwise. If CAST is set to 1, we're treating the case
11959 were SOURCE is cast into DEST, which borrows a lot of the
11960 assignment check. */
11961
11962static int
11963valid_ref_assignconv_cast_p (source, dest, cast)
11964 tree source;
11965 tree dest;
11966 int cast;
11967{
09ed0f70
APB
11968 /* SOURCE or DEST might be null if not from a declared entity. */
11969 if (!source || !dest)
11970 return 0;
5e942c50
APB
11971 if (JNULLP_TYPE_P (source))
11972 return 1;
e04a16fb
AG
11973 if (TREE_CODE (source) == POINTER_TYPE)
11974 source = TREE_TYPE (source);
11975 if (TREE_CODE (dest) == POINTER_TYPE)
11976 dest = TREE_TYPE (dest);
11977 /* Case where SOURCE is a class type */
11978 if (TYPE_CLASS_P (source))
11979 {
11980 if (TYPE_CLASS_P (dest))
c2952b01
APB
11981 return (source == dest
11982 || inherits_from_p (source, dest)
11983 || enclosing_context_p (dest, source /*source, dest*/)
11984 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
11985 if (TYPE_INTERFACE_P (dest))
11986 {
11987 /* If doing a cast and SOURCE is final, the operation is
11988 always correct a compile time (because even if SOURCE
11989 does not implement DEST, a subclass of SOURCE might). */
11990 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
11991 return 1;
11992 /* Otherwise, SOURCE must implement DEST */
11993 return interface_of_p (dest, source);
11994 }
11995 /* DEST is an array, cast permited if SOURCE is of Object type */
11996 return (cast && source == object_type_node ? 1 : 0);
11997 }
11998 if (TYPE_INTERFACE_P (source))
11999 {
12000 if (TYPE_CLASS_P (dest))
12001 {
12002 /* If not casting, DEST must be the Object type */
12003 if (!cast)
12004 return dest == object_type_node;
12005 /* We're doing a cast. The cast is always valid is class
12006 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12007 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12008 return 1;
12009 else
12010 return interface_of_p (source, dest);
12011 }
12012 if (TYPE_INTERFACE_P (dest))
12013 {
12014 /* If doing a cast, then if SOURCE and DEST contain method
12015 with the same signature but different return type, then
12016 this is a (compile time) error */
12017 if (cast)
12018 {
12019 tree method_source, method_dest;
12020 tree source_type;
0a2138e2 12021 tree source_sig;
e04a16fb
AG
12022 tree source_name;
12023 for (method_source = TYPE_METHODS (source); method_source;
12024 method_source = TREE_CHAIN (method_source))
12025 {
12026 source_sig =
12027 build_java_argument_signature (TREE_TYPE (method_source));
12028 source_type = TREE_TYPE (TREE_TYPE (method_source));
12029 source_name = DECL_NAME (method_source);
12030 for (method_dest = TYPE_METHODS (dest);
12031 method_dest; method_dest = TREE_CHAIN (method_dest))
12032 if (source_sig ==
12033 build_java_argument_signature (TREE_TYPE (method_dest))
12034 && source_name == DECL_NAME (method_dest)
12035 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12036 return 0;
12037 }
12038 return 1;
12039 }
12040 else
12041 return source == dest || interface_of_p (dest, source);
12042 }
12043 else /* Array */
93024893
APB
12044 return (cast ?
12045 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
12046 }
12047 if (TYPE_ARRAY_P (source))
12048 {
12049 if (TYPE_CLASS_P (dest))
12050 return dest == object_type_node;
09ed0f70
APB
12051 /* Can't cast an array to an interface unless the interface is
12052 java.lang.Cloneable */
e04a16fb 12053 if (TYPE_INTERFACE_P (dest))
09ed0f70 12054 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
12055 else /* Arrays */
12056 {
12057 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12058 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12059
b9f7e36c
APB
12060 /* In case of severe errors, they turn out null */
12061 if (!dest_element_type || !source_element_type)
12062 return 0;
e04a16fb
AG
12063 if (source_element_type == dest_element_type)
12064 return 1;
12065 return valid_ref_assignconv_cast_p (source_element_type,
12066 dest_element_type, cast);
12067 }
12068 return 0;
12069 }
12070 return 0;
12071}
12072
b67d701b
PB
12073static int
12074valid_cast_to_p (source, dest)
12075 tree source;
12076 tree dest;
12077{
12078 if (TREE_CODE (source) == POINTER_TYPE)
12079 source = TREE_TYPE (source);
12080 if (TREE_CODE (dest) == POINTER_TYPE)
12081 dest = TREE_TYPE (dest);
12082
12083 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12084 return valid_ref_assignconv_cast_p (source, dest, 1);
12085
12086 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12087 return 1;
12088
12089 return 0;
12090}
12091
12092/* Method invocation conversion test. Return 1 if type SOURCE can be
12093 converted to type DEST through the methond invocation conversion
12094 process (5.3) */
12095
15fdcfe9
PB
12096static tree
12097do_unary_numeric_promotion (arg)
12098 tree arg;
12099{
12100 tree type = TREE_TYPE (arg);
12101 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12102 : TREE_CODE (type) == CHAR_TYPE)
12103 arg = convert (int_type_node, arg);
12104 return arg;
12105}
12106
acd663ee
APB
12107/* Return a non zero value if SOURCE can be converted into DEST using
12108 the method invocation conversion rule (5.3). */
b67d701b
PB
12109static int
12110valid_method_invocation_conversion_p (dest, source)
12111 tree dest, source;
12112{
e3884b71 12113 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12114 && valid_builtin_assignconv_identity_widening_p (dest, source))
12115 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12116 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12117 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12118}
12119
e04a16fb
AG
12120/* Build an incomplete binop expression. */
12121
12122static tree
12123build_binop (op, op_location, op1, op2)
12124 enum tree_code op;
12125 int op_location;
12126 tree op1, op2;
12127{
5e942c50 12128 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12129 TREE_SIDE_EFFECTS (binop) = 1;
12130 /* Store the location of the operator, for better error report. The
12131 string of the operator will be rebuild based on the OP value. */
12132 EXPR_WFL_LINECOL (binop) = op_location;
12133 return binop;
12134}
12135
12136/* Build the string of the operator retained by NODE. If NODE is part
12137 of a compound expression, add an '=' at the end of the string. This
12138 function is called when an error needs to be reported on an
12139 operator. The string is returned as a pointer to a static character
12140 buffer. */
12141
12142static char *
12143operator_string (node)
12144 tree node;
12145{
12146#define BUILD_OPERATOR_STRING(S) \
12147 { \
12148 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12149 return buffer; \
12150 }
12151
12152 static char buffer [10];
12153 switch (TREE_CODE (node))
12154 {
12155 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12156 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12157 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12158 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12159 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12160 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12161 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12162 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12163 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12164 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12165 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12166 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12167 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12168 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12169 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12170 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12171 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12172 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12173 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12174 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12175 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12176 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12177 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12178 case PREINCREMENT_EXPR: /* Fall through */
12179 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12180 case PREDECREMENT_EXPR: /* Fall through */
12181 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12182 default:
12183 fatal ("unregistered operator %s - operator_string",
12184 tree_code_name [TREE_CODE (node)]);
12185 }
12186 return NULL;
12187#undef BUILD_OPERATOR_STRING
12188}
12189
5cbdba64
APB
12190/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12191
12192static int
12193java_decl_equiv (var_acc1, var_acc2)
12194 tree var_acc1, var_acc2;
12195{
12196 if (JDECL_P (var_acc1))
12197 return (var_acc1 == var_acc2);
12198
12199 return (TREE_CODE (var_acc1) == COMPONENT_REF
12200 && TREE_CODE (var_acc2) == COMPONENT_REF
12201 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12202 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12203 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12204}
12205
12206/* Return a non zero value if CODE is one of the operators that can be
12207 used in conjunction with the `=' operator in a compound assignment. */
12208
12209static int
12210binop_compound_p (code)
12211 enum tree_code code;
12212{
12213 int i;
12214 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12215 if (binop_lookup [i] == code)
12216 break;
12217
12218 return i < BINOP_COMPOUND_CANDIDATES;
12219}
12220
12221/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12222
12223static tree
12224java_refold (t)
12225 tree t;
12226{
12227 tree c, b, ns, decl;
12228
12229 if (TREE_CODE (t) != MODIFY_EXPR)
12230 return t;
12231
12232 c = TREE_OPERAND (t, 1);
12233 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12234 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12235 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12236 return t;
12237
12238 /* Now the left branch of the binary operator. */
12239 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12240 if (! (b && TREE_CODE (b) == NOP_EXPR
12241 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12242 return t;
12243
12244 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12245 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12246 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12247 return t;
12248
12249 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12250 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12251 /* It's got to be the an equivalent decl */
12252 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12253 {
12254 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12255 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12256 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12257 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12258 /* Change the right part of the BINOP_EXPR */
12259 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12260 }
12261
12262 return t;
12263}
12264
e04a16fb
AG
12265/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12266 errors but we modify NODE so that it contains the type computed
12267 according to the expression, when it's fixed. Otherwise, we write
12268 error_mark_node as the type. It allows us to further the analysis
12269 of remaining nodes and detects more errors in certain cases. */
12270
12271static tree
12272patch_binop (node, wfl_op1, wfl_op2)
12273 tree node;
12274 tree wfl_op1;
12275 tree wfl_op2;
12276{
12277 tree op1 = TREE_OPERAND (node, 0);
12278 tree op2 = TREE_OPERAND (node, 1);
12279 tree op1_type = TREE_TYPE (op1);
12280 tree op2_type = TREE_TYPE (op2);
48a840d9 12281 tree prom_type = NULL_TREE, cn;
e04a16fb 12282 int code = TREE_CODE (node);
b67d701b 12283
e04a16fb
AG
12284 /* If 1, tell the routine that we have to return error_mark_node
12285 after checking for the initialization of the RHS */
12286 int error_found = 0;
12287
e04a16fb
AG
12288 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12289
e04a16fb
AG
12290 switch (code)
12291 {
12292 /* 15.16 Multiplicative operators */
12293 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12294 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12295 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12296 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12297 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12298 {
12299 if (!JPRIMITIVE_TYPE_P (op1_type))
12300 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12301 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12302 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12303 TREE_TYPE (node) = error_mark_node;
12304 error_found = 1;
12305 break;
12306 }
12307 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12308 /* Change the division operator if necessary */
12309 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12310 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12311
aa4759c1
AH
12312 if (TREE_CODE (prom_type) == INTEGER_TYPE
12313 && flag_use_divide_subroutine
12314 && ! flag_emit_class_files
12315 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12316 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12317
0b4d333e
APB
12318 /* This one is more complicated. FLOATs are processed by a
12319 function call to soft_fmod. Duplicate the value of the
12320 COMPOUND_ASSIGN_P flag. */
e04a16fb 12321 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12322 {
12323 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12324 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12325 TREE_SIDE_EFFECTS (mod)
12326 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12327 return mod;
12328 }
e04a16fb
AG
12329 break;
12330
12331 /* 15.17 Additive Operators */
12332 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12333
12334 /* Operation is valid if either one argument is a string
12335 constant, a String object or a StringBuffer crafted for the
12336 purpose of the a previous usage of the String concatenation
12337 operator */
12338
12339 if (TREE_CODE (op1) == STRING_CST
12340 || TREE_CODE (op2) == STRING_CST
12341 || JSTRING_TYPE_P (op1_type)
12342 || JSTRING_TYPE_P (op2_type)
12343 || IS_CRAFTED_STRING_BUFFER_P (op1)
12344 || IS_CRAFTED_STRING_BUFFER_P (op2))
12345 return build_string_concatenation (op1, op2);
12346
e04a16fb
AG
12347 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12348 Numeric Types */
12349 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12350 {
12351 if (!JPRIMITIVE_TYPE_P (op1_type))
12352 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12353 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12354 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12355 TREE_TYPE (node) = error_mark_node;
12356 error_found = 1;
12357 break;
12358 }
12359 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12360 break;
12361
12362 /* 15.18 Shift Operators */
12363 case LSHIFT_EXPR:
12364 case RSHIFT_EXPR:
12365 case URSHIFT_EXPR:
12366 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12367 {
12368 if (!JINTEGRAL_TYPE_P (op1_type))
12369 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12370 else
1ebadc60
KG
12371 {
12372 if (JPRIMITIVE_TYPE_P (op2_type))
12373 parse_error_context (wfl_operator,
781b0558 12374 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12375 operator_string (node),
12376 lang_printable_name (op2_type, 0));
12377 else
781b0558
KG
12378 parse_error_context (wfl_operator,
12379 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12380 operator_string (node),
12381 lang_printable_name (op2_type, 0));
12382 }
e04a16fb
AG
12383 TREE_TYPE (node) = error_mark_node;
12384 error_found = 1;
12385 break;
12386 }
12387
12388 /* Unary numeric promotion (5.6.1) is performed on each operand
12389 separatly */
15fdcfe9
PB
12390 op1 = do_unary_numeric_promotion (op1);
12391 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12392
12393 /* The type of the shift expression is the type of the promoted
12394 type of the left-hand operand */
12395 prom_type = TREE_TYPE (op1);
12396
c2952b01
APB
12397 /* Shift int only up to 0x1f and long up to 0x3f */
12398 if (prom_type == int_type_node)
12399 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12400 build_int_2 (0x1f, 0)));
12401 else
12402 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12403 build_int_2 (0x3f, 0)));
e04a16fb
AG
12404
12405 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12406 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12407 {
0b4d333e 12408 tree to_return;
73333a87
AH
12409 tree utype = unsigned_type (prom_type);
12410 op1 = convert (utype, op1);
e04a16fb 12411 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12412 TREE_OPERAND (node, 0) = op1;
12413 TREE_OPERAND (node, 1) = op2;
12414 TREE_TYPE (node) = utype;
0b4d333e
APB
12415 to_return = convert (prom_type, node);
12416 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12417 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12418 TREE_SIDE_EFFECTS (to_return)
12419 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12420 return to_return;
e04a16fb
AG
12421 }
12422 break;
5e942c50
APB
12423
12424 /* 15.19.1 Type Comparison Operator instaceof */
12425 case INSTANCEOF_EXPR:
12426
12427 TREE_TYPE (node) = boolean_type_node;
12428
12429 if (!(op2_type = resolve_type_during_patch (op2)))
12430 return error_mark_node;
12431
12432 /* The first operand must be a reference type or the null type */
12433 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12434 error_found = 1; /* Error reported further below */
12435
12436 /* The second operand must be a reference type */
12437 if (!JREFERENCE_TYPE_P (op2_type))
12438 {
12439 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12440 parse_error_context
12441 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12442 lang_printable_name (op2_type, 0));
12443 error_found = 1;
12444 }
12445
12446 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12447 {
12448 /* If the first operand is null, the result is always false */
12449 if (op1 == null_pointer_node)
12450 return boolean_false_node;
15fdcfe9
PB
12451 else if (flag_emit_class_files)
12452 {
12453 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12454 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12455 return node;
12456 }
5e942c50
APB
12457 /* Otherwise we have to invoke instance of to figure it out */
12458 else
67db0ce7 12459 return build_instanceof (op1, op2_type);
5e942c50
APB
12460 }
12461 /* There is no way the expression operand can be an instance of
12462 the type operand. This is a compile time error. */
12463 else
12464 {
c2e3db92 12465 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12466 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12467 parse_error_context
12468 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12469 t1, lang_printable_name (op2_type, 0));
12470 free (t1);
12471 error_found = 1;
12472 }
e04a16fb 12473
5e942c50 12474 break;
e04a16fb
AG
12475
12476 /* 15.21 Bitwise and Logical Operators */
12477 case BIT_AND_EXPR:
12478 case BIT_XOR_EXPR:
12479 case BIT_IOR_EXPR:
12480 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12481 /* Binary numeric promotion is performed on both operand and the
12482 expression retain that type */
12483 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12484
12485 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12486 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12487 /* The type of the bitwise operator expression is BOOLEAN */
12488 prom_type = boolean_type_node;
12489 else
12490 {
12491 if (!JINTEGRAL_TYPE_P (op1_type))
12492 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12493 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12494 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12495 TREE_TYPE (node) = error_mark_node;
12496 error_found = 1;
12497 /* Insert a break here if adding thing before the switch's
12498 break for this case */
12499 }
12500 break;
12501
12502 /* 15.22 Conditional-And Operator */
12503 case TRUTH_ANDIF_EXPR:
12504 /* 15.23 Conditional-Or Operator */
12505 case TRUTH_ORIF_EXPR:
12506 /* Operands must be of BOOLEAN type */
12507 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12508 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12509 {
12510 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12511 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12512 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12513 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12514 TREE_TYPE (node) = boolean_type_node;
12515 error_found = 1;
12516 break;
12517 }
12518 /* The type of the conditional operators is BOOLEAN */
12519 prom_type = boolean_type_node;
12520 break;
12521
12522 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12523 case LT_EXPR:
12524 case GT_EXPR:
12525 case LE_EXPR:
12526 case GE_EXPR:
12527 /* The type of each of the operands must be a primitive numeric
12528 type */
12529 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12530 {
12531 if (!JNUMERIC_TYPE_P (op1_type))
12532 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12533 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12534 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12535 TREE_TYPE (node) = boolean_type_node;
12536 error_found = 1;
12537 break;
12538 }
12539 /* Binary numeric promotion is performed on the operands */
12540 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12541 /* The type of the relation expression is always BOOLEAN */
12542 prom_type = boolean_type_node;
12543 break;
12544
12545 /* 15.20 Equality Operator */
12546 case EQ_EXPR:
12547 case NE_EXPR:
48a840d9
APB
12548 /* It's time for us to patch the strings. */
12549 if ((cn = patch_string (op1)))
12550 {
12551 op1 = cn;
12552 op1_type = TREE_TYPE (op1);
12553 }
12554 if ((cn = patch_string (op2)))
12555 {
12556 op2 = cn;
12557 op2_type = TREE_TYPE (op2);
12558 }
12559
e04a16fb
AG
12560 /* 15.20.1 Numerical Equality Operators == and != */
12561 /* Binary numeric promotion is performed on the operands */
5e942c50 12562 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12563 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12564
12565 /* 15.20.2 Boolean Equality Operators == and != */
12566 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12567 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12568 ; /* Nothing to do here */
12569
12570 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12571 /* Types have to be either references or the null type. If
12572 they're references, it must be possible to convert either
12573 type to the other by casting conversion. */
b9f7e36c
APB
12574 else if (op1 == null_pointer_node || op2 == null_pointer_node
12575 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12576 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12577 || valid_ref_assignconv_cast_p (op2_type,
12578 op1_type, 1))))
e04a16fb
AG
12579 ; /* Nothing to do here */
12580
12581 /* Else we have an error figure what can't be converted into
12582 what and report the error */
12583 else
12584 {
12585 char *t1;
c2e3db92 12586 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12587 parse_error_context
781b0558
KG
12588 (wfl_operator,
12589 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12590 operator_string (node), t1,
0a2138e2 12591 lang_printable_name (op2_type, 0));
e04a16fb
AG
12592 free (t1);
12593 TREE_TYPE (node) = boolean_type_node;
12594 error_found = 1;
12595 break;
12596 }
12597 prom_type = boolean_type_node;
12598 break;
12599 }
12600
e04a16fb
AG
12601 if (error_found)
12602 return error_mark_node;
12603
12604 TREE_OPERAND (node, 0) = op1;
12605 TREE_OPERAND (node, 1) = op2;
12606 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12607 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12608
ce6e9147
APB
12609 if (flag_emit_xref)
12610 return node;
12611
d1472141
PB
12612 /* fold does not respect side-effect order as required for Java but not C.
12613 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12614 * bytecode.
12615 */
12616 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12617 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12618 node = fold (node);
12619 return node;
e04a16fb
AG
12620}
12621
b67d701b
PB
12622/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12623 zero value, the value of CSTE comes after the valude of STRING */
12624
12625static tree
12626do_merge_string_cste (cste, string, string_len, after)
12627 tree cste;
49f48c71 12628 const char *string;
b67d701b
PB
12629 int string_len, after;
12630{
12631 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 12632 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
12633 TREE_STRING_LENGTH (cste) = len;
12634 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12635 if (after)
12636 {
12637 strcpy (TREE_STRING_POINTER (cste), string);
12638 strcat (TREE_STRING_POINTER (cste), old);
12639 }
12640 else
12641 {
12642 strcpy (TREE_STRING_POINTER (cste), old);
12643 strcat (TREE_STRING_POINTER (cste), string);
12644 }
12645 return cste;
12646}
12647
12648/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12649 new STRING_CST on success, NULL_TREE on failure */
12650
12651static tree
12652merge_string_cste (op1, op2, after)
12653 tree op1, op2;
12654 int after;
12655{
12656 /* Handle two string constants right away */
12657 if (TREE_CODE (op2) == STRING_CST)
12658 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12659 TREE_STRING_LENGTH (op2), after);
12660
12661 /* Reasonable integer constant can be treated right away */
12662 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12663 {
49f48c71
KG
12664 static const char *boolean_true = "true";
12665 static const char *boolean_false = "false";
12666 static const char *null_pointer = "null";
b67d701b 12667 char ch[3];
49f48c71 12668 const char *string;
b67d701b
PB
12669
12670 if (op2 == boolean_true_node)
12671 string = boolean_true;
12672 else if (op2 == boolean_false_node)
12673 string = boolean_false;
12674 else if (op2 == null_pointer_node)
12675 string = null_pointer;
12676 else if (TREE_TYPE (op2) == char_type_node)
12677 {
12678 ch[0] = (char )TREE_INT_CST_LOW (op2);
12679 ch[1] = '\0';
12680 string = ch;
12681 }
12682 else
12683 string = print_int_node (op2);
12684
12685 return do_merge_string_cste (op1, string, strlen (string), after);
12686 }
12687 return NULL_TREE;
12688}
12689
12690/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12691 has to be a STRING_CST and the other part must be a STRING_CST or a
12692 INTEGRAL constant. Return a new STRING_CST if the operation
12693 succeed, NULL_TREE otherwise.
12694
12695 If the case we want to optimize for space, we might want to return
12696 NULL_TREE for each invocation of this routine. FIXME */
12697
12698static tree
12699string_constant_concatenation (op1, op2)
12700 tree op1, op2;
12701{
12702 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12703 {
0a2138e2 12704 tree string, rest;
b67d701b
PB
12705 int invert;
12706
12707 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12708 rest = (string == op1 ? op2 : op1);
12709 invert = (string == op1 ? 0 : 1 );
12710
12711 /* Walk REST, only if it looks reasonable */
12712 if (TREE_CODE (rest) != STRING_CST
12713 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12714 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12715 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12716 {
12717 rest = java_complete_tree (rest);
12718 if (rest == error_mark_node)
12719 return error_mark_node;
12720 rest = fold (rest);
12721 }
12722 return merge_string_cste (string, rest, invert);
12723 }
12724 return NULL_TREE;
12725}
12726
12727/* Implement the `+' operator. Does static optimization if possible,
12728 otherwise create (if necessary) and append elements to a
12729 StringBuffer. The StringBuffer will be carried around until it is
12730 used for a function call or an assignment. Then toString() will be
12731 called on it to turn it into a String object. */
12732
12733static tree
12734build_string_concatenation (op1, op2)
12735 tree op1, op2;
12736{
12737 tree result;
dc0b3eff 12738 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
12739
12740 if (flag_emit_xref)
12741 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
12742
12743 /* Try to do some static optimization */
12744 if ((result = string_constant_concatenation (op1, op2)))
12745 return result;
12746
c0d87ff6
PB
12747 /* Discard empty strings on either side of the expression */
12748 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
12749 {
12750 op1 = op2;
12751 op2 = NULL_TREE;
12752 }
c0d87ff6 12753 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 12754 op2 = NULL_TREE;
b67d701b 12755
acd663ee 12756 /* If operands are string constant, turn then into object references */
b67d701b
PB
12757 if (TREE_CODE (op1) == STRING_CST)
12758 op1 = patch_string_cst (op1);
acd663ee 12759 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
12760 op2 = patch_string_cst (op2);
12761
acd663ee
APB
12762 /* If either one of the constant is null and the other non null
12763 operand is a String object, return it. */
12764 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12765 return op1;
12766
b67d701b
PB
12767 /* If OP1 isn't already a StringBuffer, create and
12768 initialize a new one */
12769 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12770 {
12771 /* Two solutions here:
c52b5771
AG
12772 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12773 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12774 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
12775 op1 = BUILD_STRING_BUFFER (op1);
12776 else
12777 {
12778 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12779 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12780 }
12781 }
12782
acd663ee
APB
12783 if (op2)
12784 {
12785 /* OP1 is no longer the last node holding a crafted StringBuffer */
12786 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12787 /* Create a node for `{new...,xxx}.append (op2)' */
12788 if (op2)
12789 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12790 }
12791
b67d701b
PB
12792 /* Mark the last node holding a crafted StringBuffer */
12793 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
12794
12795 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
12796 return op1;
12797}
12798
12799/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12800 StringBuffer. If no string were found to be patched, return
12801 NULL. */
12802
12803static tree
12804patch_string (node)
12805 tree node;
12806{
1179ebc2
APB
12807 if (node == error_mark_node)
12808 return error_mark_node;
b67d701b
PB
12809 if (TREE_CODE (node) == STRING_CST)
12810 return patch_string_cst (node);
12811 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12812 {
c877974e 12813 int saved = ctxp->explicit_constructor_p;
b67d701b 12814 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
12815 tree ret;
12816 /* Temporary disable forbid the use of `this'. */
12817 ctxp->explicit_constructor_p = 0;
12818 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
12819 /* String concatenation arguments must be evaluated in order too. */
12820 ret = force_evaluation_order (ret);
c877974e
APB
12821 /* Restore it at its previous value */
12822 ctxp->explicit_constructor_p = saved;
12823 return ret;
b67d701b
PB
12824 }
12825 return NULL_TREE;
12826}
12827
12828/* Build the internal representation of a string constant. */
12829
12830static tree
12831patch_string_cst (node)
12832 tree node;
12833{
12834 int location;
15fdcfe9
PB
12835 if (! flag_emit_class_files)
12836 {
12837 push_obstacks (&permanent_obstack, &permanent_obstack);
12838 node = get_identifier (TREE_STRING_POINTER (node));
12839 location = alloc_name_constant (CONSTANT_String, node);
12840 node = build_ref_from_constant_pool (location);
8226320b 12841 pop_obstacks ();
15fdcfe9 12842 }
cd9643f7 12843 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
12844 TREE_CONSTANT (node) = 1;
12845 return node;
12846}
12847
12848/* Build an incomplete unary operator expression. */
e04a16fb
AG
12849
12850static tree
12851build_unaryop (op_token, op_location, op1)
12852 int op_token, op_location;
12853 tree op1;
12854{
12855 enum tree_code op;
12856 tree unaryop;
12857 switch (op_token)
12858 {
b67d701b 12859 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
12860 case MINUS_TK: op = NEGATE_EXPR; break;
12861 case NEG_TK: op = TRUTH_NOT_EXPR; break;
12862 case NOT_TK: op = BIT_NOT_EXPR; break;
12863 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
12864 op_token);
12865 }
12866
12867 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
12868 TREE_SIDE_EFFECTS (unaryop) = 1;
12869 /* Store the location of the operator, for better error report. The
12870 string of the operator will be rebuild based on the OP value. */
12871 EXPR_WFL_LINECOL (unaryop) = op_location;
12872 return unaryop;
12873}
12874
12875/* Special case for the ++/-- operators, since they require an extra
12876 argument to build, which is set to NULL and patched
12877 later. IS_POST_P is 1 if the operator, 0 otherwise. */
12878
12879static tree
12880build_incdec (op_token, op_location, op1, is_post_p)
12881 int op_token, op_location;
12882 tree op1;
12883 int is_post_p;
12884{
12885 static enum tree_code lookup [2][2] =
12886 {
12887 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
12888 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
12889 };
12890 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
12891 NULL_TREE, op1, NULL_TREE);
12892 TREE_SIDE_EFFECTS (node) = 1;
12893 /* Store the location of the operator, for better error report. The
12894 string of the operator will be rebuild based on the OP value. */
12895 EXPR_WFL_LINECOL (node) = op_location;
12896 return node;
12897}
12898
12899/* Build an incomplete cast operator, based on the use of the
12900 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
12901 set. java_complete_tree is trained to walk a CONVERT_EXPR even
12902 though its type is already set. */
12903
12904static tree
12905build_cast (location, type, exp)
12906 int location;
12907 tree type, exp;
12908{
12909 tree node = build1 (CONVERT_EXPR, type, exp);
12910 EXPR_WFL_LINECOL (node) = location;
12911 return node;
12912}
12913
c2952b01
APB
12914/* Build an incomplete class reference operator. */
12915static tree
12916build_incomplete_class_ref (location, class_name)
12917 int location;
12918 tree class_name;
12919{
12920 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
12921 EXPR_WFL_LINECOL (node) = location;
12922 return node;
12923}
12924
12925/* Complete an incomplete class reference operator. */
12926static tree
12927patch_incomplete_class_ref (node)
12928 tree node;
12929{
12930 tree type = TREE_OPERAND (node, 0);
12931 tree ref_type;
12932
12933 if (!(ref_type = resolve_type_during_patch (type)))
12934 return error_mark_node;
12935
165f37bc
APB
12936 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
12937 return build_class_ref (ref_type);
12938
12939 /* If we're emitting class files and we have to deal with non
12940 primitive types, we invoke (and consider generating) the
12941 synthetic static method `class$'. */
12942 if (!TYPE_DOT_CLASS (current_class))
12943 build_dot_class_method (current_class);
12944 ref_type =
12945 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
12946 return java_complete_tree (ref_type);
c2952b01
APB
12947}
12948
e04a16fb
AG
12949/* 15.14 Unary operators. We return error_mark_node in case of error,
12950 but preserve the type of NODE if the type is fixed. */
12951
12952static tree
12953patch_unaryop (node, wfl_op)
12954 tree node;
12955 tree wfl_op;
12956{
12957 tree op = TREE_OPERAND (node, 0);
12958 tree op_type = TREE_TYPE (op);
ab3a6dd6 12959 tree prom_type = NULL_TREE, value, decl;
c2952b01 12960 int outer_field_flag = 0;
e04a16fb
AG
12961 int code = TREE_CODE (node);
12962 int error_found = 0;
12963
12964 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12965
12966 switch (code)
12967 {
12968 /* 15.13.2 Postfix Increment Operator ++ */
12969 case POSTINCREMENT_EXPR:
12970 /* 15.13.3 Postfix Increment Operator -- */
12971 case POSTDECREMENT_EXPR:
12972 /* 15.14.1 Prefix Increment Operator ++ */
12973 case PREINCREMENT_EXPR:
12974 /* 15.14.2 Prefix Decrement Operator -- */
12975 case PREDECREMENT_EXPR:
5cbdba64 12976 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
12977 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
12978 /* We might be trying to change an outer field accessed using
12979 access method. */
12980 if (outer_field_flag)
12981 {
12982 /* Retrieve the decl of the field we're trying to access. We
12983 do that by first retrieving the function we would call to
12984 access the field. It has been already verified that this
12985 field isn't final */
12986 if (flag_emit_class_files)
12987 decl = TREE_OPERAND (op, 0);
12988 else
12989 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
12990 decl = DECL_FUNCTION_ACCESS_DECL (decl);
12991 }
b3edebcf 12992 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 12993 else if (!JDECL_P (decl)
b3edebcf
APB
12994 && TREE_CODE (decl) != COMPONENT_REF
12995 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
12996 && TREE_CODE (decl) != INDIRECT_REF
12997 && !(TREE_CODE (decl) == COMPOUND_EXPR
12998 && TREE_OPERAND (decl, 1)
12999 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13000 {
5e942c50
APB
13001 tree lvalue;
13002 /* Before screaming, check that we're not in fact trying to
13003 increment a optimized static final access, in which case
13004 we issue an different error message. */
13005 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13006 && resolve_expression_name (wfl_op, &lvalue)
13007 && check_final_assignment (lvalue, wfl_op)))
13008 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13009 operator_string (node));
e04a16fb
AG
13010 TREE_TYPE (node) = error_mark_node;
13011 error_found = 1;
13012 }
c2952b01
APB
13013
13014 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13015 error_found = 1;
13016
e04a16fb
AG
13017 /* From now on, we know that op if a variable and that it has a
13018 valid wfl. We use wfl_op to locate errors related to the
13019 ++/-- operand. */
13020 else if (!JNUMERIC_TYPE_P (op_type))
13021 {
13022 parse_error_context
13023 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13024 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13025 TREE_TYPE (node) = error_mark_node;
13026 error_found = 1;
13027 }
13028 else
13029 {
4a5f66c3 13030 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13031 both operands, if really necessary */
13032 if (JINTEGRAL_TYPE_P (op_type))
13033 {
13034 value = build_int_2 (1, 0);
13035 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13036 }
13037 else
13038 {
13039 value = build_int_2 (1, 0);
13040 TREE_TYPE (node) =
13041 binary_numeric_promotion (op_type,
13042 TREE_TYPE (value), &op, &value);
13043 }
c2952b01
APB
13044
13045 /* We remember we might be accessing an outer field */
13046 if (outer_field_flag)
13047 {
13048 /* We re-generate an access to the field */
13049 value = build (PLUS_EXPR, TREE_TYPE (op),
13050 build_outer_field_access (wfl_op, decl), value);
13051
13052 /* And we patch the original access$() into a write
13053 with plus_op as a rhs */
13054 return outer_field_access_fix (node, op, value);
13055 }
13056
5cbdba64 13057 /* And write back into the node. */
4a5f66c3 13058 TREE_OPERAND (node, 0) = op;
e04a16fb 13059 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13060 /* Convert the overall back into its original type, if
13061 necessary, and return */
13062 if (JINTEGRAL_TYPE_P (op_type))
13063 return fold (node);
13064 else
13065 return fold (convert (op_type, node));
e04a16fb
AG
13066 }
13067 break;
13068
13069 /* 15.14.3 Unary Plus Operator + */
b67d701b 13070 case UNARY_PLUS_EXPR:
e04a16fb
AG
13071 /* 15.14.4 Unary Minus Operator - */
13072 case NEGATE_EXPR:
13073 if (!JNUMERIC_TYPE_P (op_type))
13074 {
13075 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13076 TREE_TYPE (node) = error_mark_node;
13077 error_found = 1;
13078 }
13079 /* Unary numeric promotion is performed on operand */
13080 else
13081 {
15fdcfe9
PB
13082 op = do_unary_numeric_promotion (op);
13083 prom_type = TREE_TYPE (op);
b67d701b 13084 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13085 return fold (op);
e04a16fb
AG
13086 }
13087 break;
13088
13089 /* 15.14.5 Bitwise Complement Operator ~ */
13090 case BIT_NOT_EXPR:
13091 if (!JINTEGRAL_TYPE_P (op_type))
13092 {
13093 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13094 TREE_TYPE (node) = error_mark_node;
13095 error_found = 1;
13096 }
13097 else
13098 {
15fdcfe9
PB
13099 op = do_unary_numeric_promotion (op);
13100 prom_type = TREE_TYPE (op);
e04a16fb
AG
13101 }
13102 break;
13103
13104 /* 15.14.6 Logical Complement Operator ! */
13105 case TRUTH_NOT_EXPR:
13106 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13107 {
13108 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13109 /* But the type is known. We will report an error if further
13110 attempt of a assignment is made with this rhs */
e04a16fb
AG
13111 TREE_TYPE (node) = boolean_type_node;
13112 error_found = 1;
13113 }
13114 else
13115 prom_type = boolean_type_node;
13116 break;
13117
13118 /* 15.15 Cast Expression */
13119 case CONVERT_EXPR:
0a2138e2 13120 value = patch_cast (node, wfl_operator);
e04a16fb 13121 if (value == error_mark_node)
c877974e
APB
13122 {
13123 /* If this cast is part of an assignment, we tell the code
13124 that deals with it not to complain about a mismatch,
13125 because things have been cast, anyways */
13126 TREE_TYPE (node) = error_mark_node;
13127 error_found = 1;
13128 }
13129 else
dc0b3eff
PB
13130 {
13131 value = fold (value);
13132 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13133 return value;
13134 }
e04a16fb
AG
13135 break;
13136 }
13137
e04a16fb
AG
13138 if (error_found)
13139 return error_mark_node;
4a5f66c3
APB
13140
13141 /* There are cases where node has been replaced by something else
13142 and we don't end up returning here: UNARY_PLUS_EXPR,
13143 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13144 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13145 TREE_TYPE (node) = prom_type;
dc0b3eff 13146 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13147 return fold (node);
13148}
13149
13150/* Generic type resolution that sometimes takes place during node
13151 patching. Returned the resolved type or generate an error
13152 message. Return the resolved type or NULL_TREE. */
13153
13154static tree
13155resolve_type_during_patch (type)
13156 tree type;
13157{
13158 if (unresolved_type_p (type, NULL))
13159 {
13160 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
13161 if (!type_decl)
13162 {
13163 parse_error_context (type,
13164 "Class `%s' not found in type declaration",
13165 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13166 return NULL_TREE;
13167 }
13168 else
5e942c50
APB
13169 {
13170 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13171 return TREE_TYPE (type_decl);
13172 }
e04a16fb
AG
13173 }
13174 return type;
13175}
13176/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13177 found. Otherwise NODE or something meant to replace it is returned. */
13178
13179static tree
0a2138e2 13180patch_cast (node, wfl_operator)
e04a16fb 13181 tree node;
e04a16fb
AG
13182 tree wfl_operator;
13183{
13184 tree op = TREE_OPERAND (node, 0);
13185 tree op_type = TREE_TYPE (op);
13186 tree cast_type = TREE_TYPE (node);
13187 char *t1;
13188
13189 /* First resolve OP_TYPE if unresolved */
13190 if (!(cast_type = resolve_type_during_patch (cast_type)))
13191 return error_mark_node;
13192
13193 /* Check on cast that are proven correct at compile time */
13194 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13195 {
e04a16fb
AG
13196 /* Same type */
13197 if (cast_type == op_type)
13198 return node;
13199
0b4d333e
APB
13200 /* float and double type are converted to the original type main
13201 variant and then to the target type. */
13202 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13203 op = convert (integer_type_node, op);
13204
e04a16fb
AG
13205 /* Try widening/narowwing convertion. Potentially, things need
13206 to be worked out in gcc so we implement the extreme cases
13207 correctly. fold_convert() needs to be fixed. */
13208 return convert (cast_type, op);
13209 }
13210
0b4d333e
APB
13211 /* It's also valid to cast a boolean into a boolean */
13212 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13213 return node;
13214
5e942c50
APB
13215 /* null can be casted to references */
13216 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13217 return build_null_of_type (cast_type);
13218
e04a16fb
AG
13219 /* The remaining legal casts involve conversion between reference
13220 types. Check for their compile time correctness. */
13221 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13222 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13223 {
13224 TREE_TYPE (node) = promote_type (cast_type);
13225 /* Now, the case can be determined correct at compile time if
13226 OP_TYPE can be converted into CAST_TYPE by assignment
13227 conversion (5.2) */
13228
13229 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13230 {
13231 TREE_SET_CODE (node, NOP_EXPR);
13232 return node;
13233 }
13234
13235 if (flag_emit_class_files)
13236 {
13237 TREE_SET_CODE (node, CONVERT_EXPR);
13238 return node;
13239 }
e04a16fb
AG
13240
13241 /* The cast requires a run-time check */
13242 return build (CALL_EXPR, promote_type (cast_type),
13243 build_address_of (soft_checkcast_node),
13244 tree_cons (NULL_TREE, build_class_ref (cast_type),
13245 build_tree_list (NULL_TREE, op)),
13246 NULL_TREE);
13247 }
13248
13249 /* Any other casts are proven incorrect at compile time */
c2e3db92 13250 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13251 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13252 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13253 free (t1);
13254 return error_mark_node;
13255}
13256
5e942c50
APB
13257/* Build a null constant and give it the type TYPE. */
13258
13259static tree
13260build_null_of_type (type)
13261 tree type;
13262{
13263 tree node = build_int_2 (0, 0);
13264 TREE_TYPE (node) = promote_type (type);
13265 return node;
13266}
13267
e04a16fb
AG
13268/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13269 a list of indices. */
13270static tree
13271build_array_ref (location, array, index)
13272 int location;
13273 tree array, index;
13274{
13275 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13276 EXPR_WFL_LINECOL (node) = location;
13277 return node;
13278}
13279
13280/* 15.12 Array Access Expression */
13281
13282static tree
c877974e
APB
13283patch_array_ref (node)
13284 tree node;
e04a16fb
AG
13285{
13286 tree array = TREE_OPERAND (node, 0);
13287 tree array_type = TREE_TYPE (array);
13288 tree index = TREE_OPERAND (node, 1);
13289 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13290 int error_found = 0;
13291
13292 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13293
e04a16fb
AG
13294 if (TREE_CODE (array_type) == POINTER_TYPE)
13295 array_type = TREE_TYPE (array_type);
13296
13297 /* The array reference must be an array */
13298 if (!TYPE_ARRAY_P (array_type))
13299 {
13300 parse_error_context
781b0558
KG
13301 (wfl_operator,
13302 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13303 lang_printable_name (array_type, 0));
e04a16fb
AG
13304 TREE_TYPE (node) = error_mark_node;
13305 error_found = 1;
13306 }
13307
c2952b01 13308 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13309 type must be int */
15fdcfe9
PB
13310 index = do_unary_numeric_promotion (index);
13311 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13312 {
1ebadc60 13313 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13314 parse_error_context (wfl_operator,
13315 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13316 lang_printable_name (index_type, 0));
13317 else
781b0558
KG
13318 parse_error_context (wfl_operator,
13319 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13320 lang_printable_name (index_type, 0));
e04a16fb
AG
13321 TREE_TYPE (node) = error_mark_node;
13322 error_found = 1;
13323 }
13324
e04a16fb
AG
13325 if (error_found)
13326 return error_mark_node;
e04a16fb 13327
5e942c50 13328 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13329
7f1d4866 13330 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13331 {
15fdcfe9
PB
13332 TREE_OPERAND (node, 0) = array;
13333 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13334 }
13335 else
939d7216
PB
13336 {
13337 /* The save_expr is for correct evaluation order. It would be cleaner
13338 to use force_evaluation_order (see comment there), but that is
13339 difficult when we also have to deal with bounds checking. */
13340 if (TREE_SIDE_EFFECTS (index))
13341 array = save_expr (array);
13342 node = build_java_arrayaccess (array, array_type, index);
13343 if (TREE_SIDE_EFFECTS (index))
13344 node = build (COMPOUND_EXPR, array_type, array, node);
13345 }
e04a16fb
AG
13346 TREE_TYPE (node) = array_type;
13347 return node;
13348}
13349
13350/* 15.9 Array Creation Expressions */
13351
13352static tree
13353build_newarray_node (type, dims, extra_dims)
13354 tree type;
13355 tree dims;
13356 int extra_dims;
13357{
13358 tree node =
b67d701b 13359 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13360 build_int_2 (extra_dims, 0));
e04a16fb
AG
13361 return node;
13362}
13363
13364static tree
13365patch_newarray (node)
13366 tree node;
13367{
13368 tree type = TREE_OPERAND (node, 0);
13369 tree dims = TREE_OPERAND (node, 1);
13370 tree cdim, array_type;
13371 int error_found = 0;
13372 int ndims = 0;
13373 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13374
13375 /* Dimension types are verified. It's better for the types to be
13376 verified in order. */
13377 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13378 {
13379 int dim_error = 0;
13380 tree dim = TREE_VALUE (cdim);
13381
13382 /* Dim might have been saved during its evaluation */
13383 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13384
13385 /* The type of each specified dimension must be an integral type. */
13386 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13387 dim_error = 1;
13388
13389 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13390 promoted type must be int. */
13391 else
13392 {
15fdcfe9 13393 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13394 if (TREE_TYPE (dim) != int_type_node)
13395 dim_error = 1;
13396 }
13397
13398 /* Report errors on types here */
13399 if (dim_error)
13400 {
13401 parse_error_context
13402 (TREE_PURPOSE (cdim),
781b0558 13403 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13404 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13405 "Explicit cast needed to" : "Can't"),
0a2138e2 13406 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13407 error_found = 1;
13408 }
13409
e04a16fb
AG
13410 TREE_PURPOSE (cdim) = NULL_TREE;
13411 }
13412
13413 /* Resolve array base type if unresolved */
13414 if (!(type = resolve_type_during_patch (type)))
13415 error_found = 1;
13416
13417 if (error_found)
13418 {
13419 /* We don't want further evaluation of this bogus array creation
13420 operation */
13421 TREE_TYPE (node) = error_mark_node;
13422 return error_mark_node;
13423 }
13424
15fdcfe9
PB
13425 /* Set array_type to the actual (promoted) array type of the result. */
13426 if (TREE_CODE (type) == RECORD_TYPE)
13427 type = build_pointer_type (type);
13428 while (--xdims >= 0)
13429 {
13430 type = promote_type (build_java_array_type (type, -1));
13431 }
13432 dims = nreverse (dims);
13433 array_type = type;
13434 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13435 {
13436 type = array_type;
05bccae2
RK
13437 array_type
13438 = build_java_array_type (type,
13439 TREE_CODE (cdim) == INTEGER_CST
13440 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13441 : -1);
15fdcfe9
PB
13442 array_type = promote_type (array_type);
13443 }
13444 dims = nreverse (dims);
13445
e04a16fb
AG
13446 /* The node is transformed into a function call. Things are done
13447 differently according to the number of dimensions. If the number
13448 of dimension is equal to 1, then the nature of the base type
13449 (primitive or not) matters. */
15fdcfe9 13450 if (ndims == 1)
fdec99c6 13451 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13452
e04a16fb
AG
13453 /* Can't reuse what's already written in expr.c because it uses the
13454 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13455 return build (CALL_EXPR, array_type,
e04a16fb 13456 build_address_of (soft_multianewarray_node),
15fdcfe9 13457 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13458 tree_cons (NULL_TREE,
15fdcfe9 13459 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13460 NULL_TREE);
13461}
13462
f8976021
APB
13463/* 10.6 Array initializer. */
13464
13465/* Build a wfl for array element that don't have one, so we can
13466 pin-point errors. */
13467
13468static tree
13469maybe_build_array_element_wfl (node)
13470 tree node;
13471{
13472 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13473 return build_expr_wfl (NULL_TREE, ctxp->filename,
13474 ctxp->elc.line, ctxp->elc.prev_col);
13475 else
13476 return NULL_TREE;
13477}
13478
13479/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13480 identification of initialized arrays easier to detect during walk
13481 and expansion. */
13482
13483static tree
13484build_new_array_init (location, values)
13485 int location;
13486 tree values;
13487{
13488 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13489 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13490 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13491 return to_return;
13492}
13493
13494/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13495 occurred. Otherwise return NODE after having set its type
13496 appropriately. */
13497
13498static tree
13499patch_new_array_init (type, node)
13500 tree type, node;
f8976021
APB
13501{
13502 int error_seen = 0;
fdec99c6 13503 tree current, element_type;
f8976021 13504 HOST_WIDE_INT length;
fdec99c6
PB
13505 int all_constant = 1;
13506 tree init = TREE_OPERAND (node, 0);
f8976021 13507
fdec99c6
PB
13508 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13509 {
13510 parse_error_context (node,
13511 "Invalid array initializer for non-array type `%s'",
13512 lang_printable_name (type, 1));
13513 return error_mark_node;
13514 }
13515 type = TREE_TYPE (type);
13516 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13517
fdec99c6
PB
13518 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13519
13520 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13521 current; length++, current = TREE_CHAIN (current))
f8976021 13522 {
fdec99c6
PB
13523 tree elt = TREE_VALUE (current);
13524 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13525 {
fdec99c6 13526 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13527 elt = TREE_VALUE (current);
13528 /* When compiling to native code, STRING_CST is converted to
13529 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13530 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13531 all_constant = 0;
f8976021 13532 }
fdec99c6
PB
13533 else
13534 {
13535 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13536 TREE_PURPOSE (current) = NULL_TREE;
13537 all_constant = 0;
13538 }
13539 if (elt && TREE_VALUE (elt) == error_mark_node)
13540 error_seen = 1;
f8976021
APB
13541 }
13542
13543 if (error_seen)
13544 return error_mark_node;
13545
13546 /* Create a new type. We can't reuse the one we have here by
13547 patching its dimension because it originally is of dimension -1
13548 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13549 type = build_java_array_type (element_type, length);
13550 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13551 TREE_TYPE (node) = promote_type (type);
13552 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13553 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13554 return node;
13555}
13556
13557/* Verify that one entry of the initializer element list can be
13558 assigned to the array base type. Report 1 if an error occurred, 0
13559 otherwise. */
13560
13561static int
13562array_constructor_check_entry (type, entry)
13563 tree type, entry;
13564{
13565 char *array_type_string = NULL; /* For error reports */
13566 tree value, type_value, new_value, wfl_value, patched;
13567 int error_seen = 0;
13568
13569 new_value = NULL_TREE;
13570 wfl_value = TREE_VALUE (entry);
13571
100f7cd8 13572 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13573 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13574 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13575 if ((patched = patch_string (value)))
13576 value = patched;
1179ebc2
APB
13577 if (value == error_mark_node)
13578 return 1;
f8976021 13579
f8976021
APB
13580 type_value = TREE_TYPE (value);
13581
1179ebc2 13582 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13583 constant overflow during narrowing. */
13584 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13585 new_value = try_builtin_assignconv (wfl_operator, type, value);
13586 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13587 type_value = promote_type (type);
100f7cd8
APB
13588
13589 pop_obstacks ();
f8976021
APB
13590 /* Check and report errors */
13591 if (!new_value)
13592 {
49f48c71 13593 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13594 "Can't" : "Explicit cast needed to");
13595 if (!array_type_string)
c2e3db92 13596 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13597 parse_error_context
13598 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13599 msg, lang_printable_name (type_value, 1), array_type_string);
13600 error_seen = 1;
13601 }
13602
13603 if (new_value)
13604 {
b8c5b1c6 13605 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13606 TREE_VALUE (entry) = new_value;
13607 }
13608
13609 if (array_type_string)
13610 free (array_type_string);
13611
13612 TREE_PURPOSE (entry) = NULL_TREE;
13613 return error_seen;
13614}
13615
e04a16fb
AG
13616static tree
13617build_this (location)
13618 int location;
13619{
9ee9b555 13620 tree node = build_wfl_node (this_identifier_node);
b67d701b 13621 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13622 EXPR_WFL_LINECOL (node) = location;
13623 return node;
13624}
13625
13626/* 14.15 The return statement. It builds a modify expression that
13627 assigns the returned value to the RESULT_DECL that hold the value
13628 to be returned. */
13629
13630static tree
13631build_return (location, op)
13632 int location;
13633 tree op;
13634{
13635 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13636 EXPR_WFL_LINECOL (node) = location;
b67d701b 13637 node = build_debugable_stmt (location, node);
e04a16fb
AG
13638 return node;
13639}
13640
13641static tree
13642patch_return (node)
13643 tree node;
13644{
13645 tree return_exp = TREE_OPERAND (node, 0);
13646 tree meth = current_function_decl;
13647 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13648 int error_found = 0;
13649
13650 TREE_TYPE (node) = error_mark_node;
13651 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13652
13653 /* It's invalid to have a return value within a function that is
13654 declared with the keyword void or that is a constructor */
13655 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13656 error_found = 1;
13657
f099f336 13658 /* It's invalid to use a return statement in a static block */
c2952b01 13659 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13660 error_found = 1;
13661
e04a16fb
AG
13662 /* It's invalid to have a no return value within a function that
13663 isn't declared with the keyword `void' */
13664 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13665 error_found = 2;
c2952b01
APB
13666
13667 if (in_instance_initializer)
13668 error_found = 1;
e04a16fb
AG
13669
13670 if (error_found)
13671 {
c2952b01 13672 if (in_instance_initializer)
f099f336 13673 parse_error_context (wfl_operator,
c2952b01
APB
13674 "`return' inside instance initializer");
13675
13676 else if (DECL_CLINIT_P (current_function_decl))
13677 parse_error_context (wfl_operator,
13678 "`return' inside static initializer");
f099f336
APB
13679
13680 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13681 {
c2e3db92 13682 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13683 parse_error_context (wfl_operator,
13684 "`return' with%s value from `%s %s'",
13685 (error_found == 1 ? "" : "out"),
13686 t, lang_printable_name (meth, 0));
13687 free (t);
13688 }
13689 else
13690 parse_error_context (wfl_operator,
13691 "`return' with value from constructor `%s'",
13692 lang_printable_name (meth, 0));
e04a16fb
AG
13693 return error_mark_node;
13694 }
13695
5e942c50
APB
13696 /* If we have a return_exp, build a modify expression and expand
13697 it. Note: at that point, the assignment is declared valid, but we
13698 may want to carry some more hacks */
e04a16fb
AG
13699 if (return_exp)
13700 {
5e942c50
APB
13701 tree exp = java_complete_tree (return_exp);
13702 tree modify, patched;
13703
13704 /* If the function returned value and EXP are booleans, EXP has
13705 to be converted into the type of DECL_RESULT, which is integer
13706 (see complete_start_java_method) */
13707 if (TREE_TYPE (exp) == boolean_type_node &&
13708 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13709 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13710
13711 /* `null' can be assigned to a function returning a reference */
13712 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13713 exp == null_pointer_node)
13714 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13715
13716 if ((patched = patch_string (exp)))
13717 exp = patched;
13718
13719 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
13720 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13721 modify = java_complete_tree (modify);
5e942c50 13722
e04a16fb
AG
13723 if (modify != error_mark_node)
13724 {
13725 TREE_SIDE_EFFECTS (modify) = 1;
13726 TREE_OPERAND (node, 0) = modify;
13727 }
13728 else
13729 return error_mark_node;
13730 }
13731 TREE_TYPE (node) = void_type_node;
13732 TREE_SIDE_EFFECTS (node) = 1;
13733 return node;
13734}
13735
13736/* 14.8 The if Statement */
13737
13738static tree
13739build_if_else_statement (location, expression, if_body, else_body)
13740 int location;
13741 tree expression, if_body, else_body;
13742{
13743 tree node;
e04a16fb 13744 if (!else_body)
9bbc7d9f 13745 else_body = empty_stmt_node;
e04a16fb
AG
13746 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13747 EXPR_WFL_LINECOL (node) = location;
b67d701b 13748 node = build_debugable_stmt (location, node);
e04a16fb
AG
13749 return node;
13750}
13751
13752static tree
13753patch_if_else_statement (node)
13754 tree node;
13755{
13756 tree expression = TREE_OPERAND (node, 0);
13757
13758 TREE_TYPE (node) = error_mark_node;
13759 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13760
13761 /* The type of expression must be boolean */
b67d701b
PB
13762 if (TREE_TYPE (expression) != boolean_type_node
13763 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
13764 {
13765 parse_error_context
13766 (wfl_operator,
13767 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 13768 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
13769 return error_mark_node;
13770 }
13771
13772 TREE_TYPE (node) = void_type_node;
13773 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 13774 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
13775 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13776 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
13777 return node;
13778}
13779
13780/* 14.6 Labeled Statements */
13781
13782/* Action taken when a lableled statement is parsed. a new
13783 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 13784 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
13785
13786static tree
0a2138e2 13787build_labeled_block (location, label)
e04a16fb 13788 int location;
0a2138e2 13789 tree label;
e04a16fb 13790{
b635eb2f 13791 tree label_name ;
e04a16fb 13792 tree label_decl, node;
b635eb2f
PB
13793 if (label == NULL_TREE || label == continue_identifier_node)
13794 label_name = label;
13795 else
e04a16fb 13796 {
b635eb2f
PB
13797 label_name = merge_qualified_name (label_id, label);
13798 /* Issue an error if we try to reuse a label that was previously
13799 declared */
13800 if (IDENTIFIER_LOCAL_VALUE (label_name))
13801 {
13802 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
13803 parse_error_context (wfl_operator,
13804 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
13805 IDENTIFIER_POINTER (label));
13806 EXPR_WFL_LINECOL (wfl_operator) =
13807 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
13808 parse_error_context (wfl_operator,
13809 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
13810 IDENTIFIER_POINTER (label));
13811 java_error_count--;
13812 }
e04a16fb
AG
13813 }
13814
13815 label_decl = create_label_decl (label_name);
13816 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
13817 EXPR_WFL_LINECOL (node) = location;
13818 TREE_SIDE_EFFECTS (node) = 1;
13819 return node;
13820}
13821
b67d701b 13822/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
13823
13824static tree
b635eb2f 13825finish_labeled_statement (lbe, statement)
e04a16fb
AG
13826 tree lbe; /* Labeled block expr */
13827 tree statement;
13828{
13829 /* In anyways, tie the loop to its statement */
13830 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
13831 pop_labeled_block ();
13832 POP_LABELED_BLOCK ();
e04a16fb
AG
13833 return lbe;
13834}
13835
13836/* 14.10, 14.11, 14.12 Loop Statements */
13837
13838/* Create an empty LOOP_EXPR and make it the last in the nested loop
13839 list. */
13840
13841static tree
13842build_new_loop (loop_body)
13843 tree loop_body;
13844{
13845 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
13846 TREE_SIDE_EFFECTS (loop) = 1;
13847 PUSH_LOOP (loop);
13848 return loop;
13849}
13850
13851/* Create a loop body according to the following structure:
13852 COMPOUND_EXPR
13853 COMPOUND_EXPR (loop main body)
13854 EXIT_EXPR (this order is for while/for loops.
13855 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 13856 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
13857 BODY end of this labeled block)
13858 INCREMENT (if any)
13859
13860 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
13861 after the body, like in the do-while loop.
13862
13863 To obtain a loop, the loop body structure described above is
13864 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
13865
13866 LABELED_BLOCK_EXPR
13867 LABEL_DECL (use this label to exit the loop)
13868 LOOP_EXPR
13869 <structure described above> */
e04a16fb
AG
13870
13871static tree
13872build_loop_body (location, condition, reversed)
13873 int location;
13874 tree condition;
13875 int reversed;
13876{
0a2138e2 13877 tree first, second, body;
e04a16fb
AG
13878
13879 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
13880 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
13881 condition = build_debugable_stmt (location, condition);
13882 TREE_SIDE_EFFECTS (condition) = 1;
13883
b635eb2f 13884 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
13885 first = (reversed ? body : condition);
13886 second = (reversed ? condition : body);
13887 return
13888 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 13889 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
13890}
13891
13892/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
13893 their order) on the current loop. Unlink the current loop from the
13894 loop list. */
13895
13896static tree
b635eb2f 13897finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
13898 int location;
13899 tree condition, body;
13900 int reversed;
13901{
13902 tree to_return = ctxp->current_loop;
13903 tree loop_body = LOOP_EXPR_BODY (to_return);
13904 if (condition)
13905 {
13906 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
13907 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
13908 The real EXIT_EXPR is one operand further. */
13909 EXPR_WFL_LINECOL (cnode) = location;
13910 /* This one is for accurate error reports */
13911 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
13912 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
13913 }
13914 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
13915 POP_LOOP ();
13916 return to_return;
13917}
13918
b635eb2f 13919/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
13920 loops feature the condition part */
13921
13922static tree
b635eb2f 13923finish_for_loop (location, condition, update, body)
e04a16fb
AG
13924 int location;
13925 tree condition, update, body;
13926{
13927 /* Put the condition and the loop body in place */
b635eb2f 13928 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
13929 /* LOOP is the current loop which has been now popped of the loop
13930 stack. Install the update block */
13931 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
13932 return loop;
13933}
13934
5cbdba64
APB
13935/* Try to find the loop a block might be related to. This comprises
13936 the case where the LOOP_EXPR is found as the second operand of a
13937 COMPOUND_EXPR, because the loop happens to have an initialization
13938 part, then expressed as the first operand of the COMPOUND_EXPR. If
13939 the search finds something, 1 is returned. Otherwise, 0 is
13940 returned. The search is assumed to start from a
13941 LABELED_BLOCK_EXPR's block. */
13942
13943static tree
13944search_loop (statement)
13945 tree statement;
13946{
13947 if (TREE_CODE (statement) == LOOP_EXPR)
13948 return statement;
13949
13950 if (TREE_CODE (statement) == BLOCK)
13951 statement = BLOCK_SUBBLOCKS (statement);
13952 else
13953 return NULL_TREE;
13954
13955 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13956 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13957 statement = TREE_OPERAND (statement, 1);
13958
13959 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 13960 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
13961}
13962
13963/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
13964 returned otherwise. */
13965
13966static int
13967labeled_block_contains_loop_p (block, loop)
13968 tree block, loop;
13969{
13970 if (!block)
13971 return 0;
13972
13973 if (LABELED_BLOCK_BODY (block) == loop)
13974 return 1;
13975
c2952b01 13976 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
13977 return 1;
13978
13979 return 0;
13980}
13981
e04a16fb 13982/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 13983 insert LOOP as its body. */
e04a16fb
AG
13984
13985static tree
13986patch_loop_statement (loop)
13987 tree loop;
13988{
cd9643f7 13989 tree loop_label;
5cbdba64 13990
cd9643f7 13991 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
13992 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
13993 return loop;
13994
cd9643f7 13995 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
13996 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
13997 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
13998 LABELED_BLOCK_BODY (loop_label) = loop;
13999 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14000 return loop_label;
e04a16fb
AG
14001}
14002
14003/* 14.13, 14.14: break and continue Statements */
14004
14005/* Build a break or a continue statement. a null NAME indicates an
14006 unlabeled break/continue statement. */
14007
14008static tree
14009build_bc_statement (location, is_break, name)
14010 int location, is_break;
14011 tree name;
14012{
14013 tree break_continue, label_block_expr = NULL_TREE;
14014
14015 if (name)
14016 {
14017 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14018 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14019 /* Null means that we don't have a target for this named
14020 break/continue. In this case, we make the target to be the
14021 label name, so that the error can be reported accuratly in
14022 patch_bc_statement. */
14023 label_block_expr = EXPR_WFL_NODE (name);
14024 }
14025 /* Unlabeled break/continue will be handled during the
14026 break/continue patch operation */
14027 break_continue
14028 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14029
14030 IS_BREAK_STMT_P (break_continue) = is_break;
14031 TREE_SIDE_EFFECTS (break_continue) = 1;
14032 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14033 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14034 return break_continue;
14035}
14036
14037/* Verification of a break/continue statement. */
14038
14039static tree
14040patch_bc_statement (node)
14041 tree node;
14042{
14043 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14044 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14045 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14046
e04a16fb 14047 /* Having an identifier here means that the target is unknown. */
b635eb2f 14048 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14049 {
14050 parse_error_context (wfl_operator, "No label definition found for `%s'",
14051 IDENTIFIER_POINTER (bc_label));
14052 return error_mark_node;
14053 }
b635eb2f 14054 if (! IS_BREAK_STMT_P (node))
e04a16fb 14055 {
b635eb2f
PB
14056 /* It's a continue statement. */
14057 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14058 {
b635eb2f
PB
14059 if (labeled_block == NULL_TREE)
14060 {
14061 if (bc_label == NULL_TREE)
14062 parse_error_context (wfl_operator,
14063 "`continue' must be in loop");
14064 else
1504b2b4
APB
14065 parse_error_context
14066 (wfl_operator, "continue label `%s' does not name a loop",
14067 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14068 return error_mark_node;
14069 }
14070 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14071 == continue_identifier_node)
14072 && (bc_label == NULL_TREE
14073 || TREE_CHAIN (labeled_block) == bc_label))
14074 {
14075 bc_label = labeled_block;
14076 break;
14077 }
e04a16fb 14078 }
e04a16fb 14079 }
b635eb2f 14080 else if (!bc_label)
34f4db93 14081 {
b635eb2f 14082 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14083 {
b635eb2f
PB
14084 if (labeled_block == NULL_TREE)
14085 {
14086 parse_error_context (wfl_operator,
14087 "`break' must be in loop or switch");
14088 return error_mark_node;
14089 }
14090 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14091 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14092 || search_loop (target_stmt))
b635eb2f
PB
14093 {
14094 bc_label = labeled_block;
14095 break;
14096 }
e04a16fb 14097 }
e04a16fb
AG
14098 }
14099
b635eb2f 14100 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14101 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14102
e04a16fb
AG
14103 /* Our break/continue don't return values. */
14104 TREE_TYPE (node) = void_type_node;
14105 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14106 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14107 sometimes, like after a if statement) */
14108 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14109 TREE_SIDE_EFFECTS (node) = 1;
14110 return node;
14111}
14112
14113/* Process the exit expression belonging to a loop. Its type must be
14114 boolean. */
14115
14116static tree
14117patch_exit_expr (node)
14118 tree node;
14119{
14120 tree expression = TREE_OPERAND (node, 0);
14121 TREE_TYPE (node) = error_mark_node;
14122 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14123
14124 /* The type of expression must be boolean */
14125 if (TREE_TYPE (expression) != boolean_type_node)
14126 {
14127 parse_error_context
14128 (wfl_operator,
781b0558 14129 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14130 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14131 return error_mark_node;
14132 }
14133 /* Now we know things are allright, invert the condition, fold and
14134 return */
14135 TREE_OPERAND (node, 0) =
14136 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14137
14138 if (! integer_zerop (TREE_OPERAND (node, 0))
14139 && ctxp->current_loop != NULL_TREE
14140 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14141 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14142 if (! integer_onep (TREE_OPERAND (node, 0)))
14143 CAN_COMPLETE_NORMALLY (node) = 1;
14144
14145
e04a16fb
AG
14146 TREE_TYPE (node) = void_type_node;
14147 return node;
14148}
b67d701b
PB
14149
14150/* 14.9 Switch statement */
14151
14152static tree
14153patch_switch_statement (node)
14154 tree node;
14155{
c877974e 14156 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14157
14158 /* Complete the switch expression */
14159 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14160 se_type = TREE_TYPE (se);
14161 /* The type of the switch expression must be char, byte, short or
14162 int */
14163 if (!JINTEGRAL_TYPE_P (se_type))
14164 {
14165 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14166 parse_error_context (wfl_operator,
14167 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14168 lang_printable_name (se_type, 0));
b67d701b
PB
14169 /* This is what java_complete_tree will check */
14170 TREE_OPERAND (node, 0) = error_mark_node;
14171 return error_mark_node;
14172 }
14173
15fdcfe9 14174 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14175
14176 /* Ready to return */
15fdcfe9 14177 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14178 {
14179 TREE_TYPE (node) = error_mark_node;
14180 return error_mark_node;
14181 }
14182 TREE_TYPE (node) = void_type_node;
14183 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14184 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14185 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14186 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14187 return node;
14188}
14189
165f37bc 14190/* 14.18 The try/catch statements */
b67d701b 14191
b67d701b 14192static tree
a7d8d81f 14193build_try_statement (location, try_block, catches)
b67d701b 14194 int location;
a7d8d81f
PB
14195 tree try_block, catches;
14196{
14197 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14198 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14199 return node;
b67d701b
PB
14200}
14201
a7d8d81f
PB
14202static tree
14203build_try_finally_statement (location, try_block, finally)
14204 int location;
14205 tree try_block, finally;
b67d701b 14206{
a7d8d81f
PB
14207 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14208 EXPR_WFL_LINECOL (node) = location;
14209 return node;
b67d701b
PB
14210}
14211
14212static tree
14213patch_try_statement (node)
14214 tree node;
14215{
14216 int error_found = 0;
14217 tree try = TREE_OPERAND (node, 0);
14218 /* Exception handlers are considered in left to right order */
14219 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14220 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14221
14222 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14223 to process the next catch clause. We process the catch clause before
14224 the try block so that when processing the try block we can check thrown
14225 exceptions againts the caught type list. */
b67d701b
PB
14226 for (current = catch; current; current = TREE_CHAIN (current))
14227 {
14228 tree carg_decl, carg_type;
14229 tree sub_current, catch_block, catch_clause;
14230 int unreachable;
14231
b67d701b 14232 /* At this point, the structure of the catch clause is
b67d701b
PB
14233 CATCH_EXPR (catch node)
14234 BLOCK (with the decl of the parameter)
14235 COMPOUND_EXPR
7525cc04 14236 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14237 BLOCK (catch clause block)
a7d8d81f
PB
14238 */
14239 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14240 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14241 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14242
14243 /* Catch clauses can't have more than one parameter declared,
14244 but it's already enforced by the grammar. Make sure that the
14245 only parameter of the clause statement in of class Throwable
14246 or a subclass of Throwable, but that was done earlier. The
14247 catch clause parameter type has also been resolved. */
14248
14249 /* Just make sure that the catch clause parameter type inherits
14250 from java.lang.Throwable */
14251 if (!inherits_from_p (carg_type, throwable_type_node))
14252 {
14253 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14254 parse_error_context (wfl_operator,
781b0558 14255 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14256 lang_printable_name (carg_type, 0));
b67d701b
PB
14257 error_found = 1;
14258 continue;
14259 }
14260
14261 /* Partial check for unreachable catch statement: The catch
14262 clause is reachable iff is no earlier catch block A in
14263 the try statement such that the type of the catch
14264 clause's parameter is the same as or a subclass of the
14265 type of A's parameter */
14266 unreachable = 0;
14267 for (sub_current = catch;
14268 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14269 {
14270 tree sub_catch_clause, decl;
a7d8d81f 14271 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14272 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14273
14274 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14275 {
14276 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14277 parse_error_context
781b0558
KG
14278 (wfl_operator,
14279 "`catch' not reached because of the catch clause at line %d",
14280 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14281 unreachable = error_found = 1;
14282 break;
14283 }
14284 }
b67d701b
PB
14285 /* Complete the catch clause block */
14286 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14287 if (catch_block == error_mark_node)
14288 {
14289 error_found = 1;
14290 continue;
14291 }
15fdcfe9
PB
14292 if (CAN_COMPLETE_NORMALLY (catch_block))
14293 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14294 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14295
14296 if (unreachable)
14297 continue;
14298
14299 /* Things to do here: the exception must be thrown */
14300
14301 /* Link this type to the caught type list */
14302 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14303 }
14304
b9f7e36c
APB
14305 PUSH_EXCEPTIONS (caught_type_list);
14306 if ((try = java_complete_tree (try)) == error_mark_node)
14307 error_found = 1;
15fdcfe9
PB
14308 if (CAN_COMPLETE_NORMALLY (try))
14309 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14310 POP_EXCEPTIONS ();
14311
b67d701b
PB
14312 /* Verification ends here */
14313 if (error_found)
14314 return error_mark_node;
14315
14316 TREE_OPERAND (node, 0) = try;
14317 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14318 TREE_TYPE (node) = void_type_node;
14319 return node;
14320}
b9f7e36c
APB
14321
14322/* 14.17 The synchronized Statement */
14323
14324static tree
14325patch_synchronized_statement (node, wfl_op1)
14326 tree node, wfl_op1;
14327{
5a005d9e 14328 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14329 tree block = TREE_OPERAND (node, 1);
5a005d9e 14330
d8fccff5 14331 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14332
14333 if (expr == error_mark_node)
14334 {
14335 block = java_complete_tree (block);
14336 return expr;
14337 }
b9f7e36c
APB
14338
14339 /* The TYPE of expr must be a reference type */
5a005d9e 14340 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14341 {
14342 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14343 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14344 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14345 return error_mark_node;
14346 }
14347
ce6e9147
APB
14348 if (flag_emit_xref)
14349 {
14350 TREE_OPERAND (node, 0) = expr;
14351 TREE_OPERAND (node, 1) = java_complete_tree (block);
14352 CAN_COMPLETE_NORMALLY (node) = 1;
14353 return node;
14354 }
14355
b9f7e36c
APB
14356 /* Generate a try-finally for the synchronized statement, except
14357 that the handler that catches all throw exception calls
14358 _Jv_MonitorExit and then rethrow the exception.
14359 The synchronized statement is then implemented as:
14360 TRY
14361 {
14362 _Jv_MonitorEnter (expression)
14363 synchronized_block
14364 _Jv_MonitorExit (expression)
14365 }
14366 CATCH_ALL
14367 {
14368 e = _Jv_exception_info ();
14369 _Jv_MonitorExit (expression)
14370 Throw (e);
14371 } */
14372
5a005d9e
PB
14373 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14374 BUILD_MONITOR_ENTER (enter, expr_decl);
14375 BUILD_MONITOR_EXIT (exit, expr_decl);
14376 CAN_COMPLETE_NORMALLY (enter) = 1;
14377 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14378 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14379 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14380 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14381 build (COMPOUND_EXPR, NULL_TREE,
14382 build (WITH_CLEANUP_EXPR, NULL_TREE,
14383 build (COMPOUND_EXPR, NULL_TREE,
96847892 14384 assignment, enter),
5a005d9e
PB
14385 NULL_TREE, exit),
14386 block));
14387 node = build_expr_block (node, expr_decl);
14388
14389 return java_complete_tree (node);
b9f7e36c
APB
14390}
14391
14392/* 14.16 The throw Statement */
14393
14394static tree
14395patch_throw_statement (node, wfl_op1)
14396 tree node, wfl_op1;
14397{
14398 tree expr = TREE_OPERAND (node, 0);
14399 tree type = TREE_TYPE (expr);
14400 int unchecked_ok = 0, tryblock_throws_ok = 0;
14401
14402 /* Thrown expression must be assignable to java.lang.Throwable */
14403 if (!try_reference_assignconv (throwable_type_node, expr))
14404 {
14405 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14406 parse_error_context (wfl_operator,
14407 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14408 lang_printable_name (type, 0));
b9f7e36c
APB
14409 /* If the thrown expression was a reference, we further the
14410 compile-time check. */
14411 if (!JREFERENCE_TYPE_P (type))
14412 return error_mark_node;
14413 }
14414
14415 /* At least one of the following must be true */
14416
14417 /* The type of the throw expression is a not checked exception,
14418 i.e. is a unchecked expression. */
c877974e 14419 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14420
c2952b01
APB
14421 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14422 /* An instance can't throw a checked excetion unless that exception
14423 is explicitely declared in the `throws' clause of each
14424 constructor. This doesn't apply to anonymous classes, since they
14425 don't have declared constructors. */
14426 if (!unchecked_ok
14427 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14428 {
14429 tree current;
14430 for (current = TYPE_METHODS (current_class); current;
14431 current = TREE_CHAIN (current))
14432 if (DECL_CONSTRUCTOR_P (current)
14433 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14434 {
14435 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)",
14436 lang_printable_name (TREE_TYPE (expr), 0));
14437 return error_mark_node;
14438 }
14439 }
14440
b9f7e36c
APB
14441 /* Throw is contained in a try statement and at least one catch
14442 clause can receive the thrown expression or the current method is
14443 declared to throw such an exception. Or, the throw statement is
14444 contained in a method or constructor declaration and the type of
14445 the Expression is assignable to at least one type listed in the
14446 throws clause the declaration. */
b9f7e36c 14447 if (!unchecked_ok)
f099f336 14448 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14449 if (!(unchecked_ok || tryblock_throws_ok))
14450 {
14451 /* If there is a surrounding try block that has no matching
14452 clatch clause, report it first. A surrounding try block exits
14453 only if there is something after the list of checked
14454 exception thrown by the current function (if any). */
14455 if (IN_TRY_BLOCK_P ())
781b0558 14456 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 14457 lang_printable_name (type, 0));
b9f7e36c
APB
14458 /* If we have no surrounding try statement and the method doesn't have
14459 any throws, report it now. FIXME */
f099f336
APB
14460
14461 /* We report that the exception can't be throw from a try block
14462 in all circumstances but when the `throw' is inside a static
14463 block. */
b9f7e36c
APB
14464 else if (!EXCEPTIONS_P (currently_caught_type_list)
14465 && !tryblock_throws_ok)
f099f336 14466 {
c2952b01 14467 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14468 parse_error_context (wfl_operator,
14469 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14470 lang_printable_name (type, 0));
14471 else
781b0558
KG
14472 parse_error_context (wfl_operator,
14473 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14474 lang_printable_name (type, 0));
14475 }
b9f7e36c
APB
14476 /* Otherwise, the current method doesn't have the appropriate
14477 throws declaration */
14478 else
781b0558 14479 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14480 lang_printable_name (type, 0));
b9f7e36c
APB
14481 return error_mark_node;
14482 }
14483
ce6e9147 14484 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14485 BUILD_THROW (node, expr);
ce6e9147
APB
14486
14487 /* If doing xrefs, keep the location where the `throw' was seen. */
14488 if (flag_emit_xref)
14489 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14490 return node;
14491}
14492
14493/* Check that exception said to be thrown by method DECL can be
14494 effectively caught from where DECL is invoked. */
14495
14496static void
14497check_thrown_exceptions (location, decl)
14498 int location;
14499 tree decl;
14500{
14501 tree throws;
14502 /* For all the unchecked exceptions thrown by DECL */
14503 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14504 throws = TREE_CHAIN (throws))
0a2138e2 14505 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14506 {
3e78f871
PB
14507#if 1
14508 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14509 if (DECL_NAME (decl) == get_identifier ("clone"))
14510 continue;
14511#endif
b9f7e36c 14512 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14513 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14514 parse_error_context
14515 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14516 lang_printable_name (TREE_VALUE (throws), 0));
14517 else
14518 {
14519 parse_error_context
781b0558 14520 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14521 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14522 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14523 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14524 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14525 }
b9f7e36c
APB
14526 }
14527}
14528
c877974e 14529/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14530 try-catch blocks, OR is listed in the `throws' clause of the
14531 current method. */
14532
14533static int
0a2138e2 14534check_thrown_exceptions_do (exception)
b9f7e36c
APB
14535 tree exception;
14536{
14537 tree list = currently_caught_type_list;
c877974e 14538 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14539 /* First, all the nested try-catch-finally at that stage. The
14540 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14541 if (IS_UNCHECKED_EXCEPTION_P (exception))
14542 return 1;
b9f7e36c
APB
14543 while (list)
14544 {
14545 tree caught;
14546 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14547 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14548 return 1;
14549 list = TREE_CHAIN (list);
14550 }
14551 return 0;
14552}
14553
14554static void
14555purge_unchecked_exceptions (mdecl)
14556 tree mdecl;
14557{
14558 tree throws = DECL_FUNCTION_THROWS (mdecl);
14559 tree new = NULL_TREE;
14560
14561 while (throws)
14562 {
14563 tree next = TREE_CHAIN (throws);
c877974e 14564 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14565 {
14566 TREE_CHAIN (throws) = new;
14567 new = throws;
14568 }
14569 throws = next;
14570 }
14571 /* List is inverted here, but it doesn't matter */
14572 DECL_FUNCTION_THROWS (mdecl) = new;
14573}
22eed1e6
APB
14574
14575/* 15.24 Conditional Operator ?: */
14576
14577static tree
14578patch_conditional_expr (node, wfl_cond, wfl_op1)
14579 tree node, wfl_cond, wfl_op1;
14580{
14581 tree cond = TREE_OPERAND (node, 0);
14582 tree op1 = TREE_OPERAND (node, 1);
14583 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14584 tree resulting_type = NULL_TREE;
ac825856 14585 tree t1, t2, patched;
22eed1e6
APB
14586 int error_found = 0;
14587
ac825856
APB
14588 /* Operands of ?: might be StringBuffers crafted as a result of a
14589 string concatenation. Obtain a descent operand here. */
14590 if ((patched = patch_string (op1)))
14591 TREE_OPERAND (node, 1) = op1 = patched;
14592 if ((patched = patch_string (op2)))
14593 TREE_OPERAND (node, 2) = op2 = patched;
14594
14595 t1 = TREE_TYPE (op1);
14596 t2 = TREE_TYPE (op2);
14597
22eed1e6
APB
14598 /* The first expression must be a boolean */
14599 if (TREE_TYPE (cond) != boolean_type_node)
14600 {
14601 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14602 parse_error_context (wfl_operator,
14603 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14604 lang_printable_name (TREE_TYPE (cond), 0));
14605 error_found = 1;
14606 }
14607
14608 /* Second and third can be numeric, boolean (i.e. primitive),
14609 references or null. Anything else results in an error */
14610 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14611 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14612 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14613 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14614 error_found = 1;
14615
14616 /* Determine the type of the conditional expression. Same types are
14617 easy to deal with */
14618 else if (t1 == t2)
14619 resulting_type = t1;
14620
14621 /* There are different rules for numeric types */
14622 else if (JNUMERIC_TYPE_P (t1))
14623 {
14624 /* if byte/short found, the resulting type is short */
14625 if ((t1 == byte_type_node && t2 == short_type_node)
14626 || (t1 == short_type_node && t2 == byte_type_node))
14627 resulting_type = short_type_node;
14628
14629 /* If t1 is a constant int and t2 is of type byte, short or char
14630 and t1's value fits in t2, then the resulting type is t2 */
14631 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14632 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14633 resulting_type = t2;
14634
14635 /* If t2 is a constant int and t1 is of type byte, short or char
14636 and t2's value fits in t1, then the resulting type is t1 */
14637 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14638 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14639 resulting_type = t1;
14640
14641 /* Otherwise, binary numeric promotion is applied and the
14642 resulting type is the promoted type of operand 1 and 2 */
14643 else
93024893 14644 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14645 &TREE_OPERAND (node, 1),
14646 &TREE_OPERAND (node, 2));
14647 }
14648
14649 /* Cases of a reference and a null type */
14650 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14651 resulting_type = t1;
14652
14653 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14654 resulting_type = t2;
14655
14656 /* Last case: different reference types. If a type can be converted
14657 into the other one by assignment conversion, the latter
14658 determines the type of the expression */
14659 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14660 resulting_type = promote_type (t1);
14661
14662 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14663 resulting_type = promote_type (t2);
14664
14665 /* If we don't have any resulting type, we're in trouble */
14666 if (!resulting_type)
14667 {
c2e3db92 14668 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14669 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14670 parse_error_context (wfl_operator,
14671 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14672 t, lang_printable_name (t2, 0));
22eed1e6
APB
14673 free (t);
14674 error_found = 1;
14675 }
14676
14677 if (error_found)
14678 {
14679 TREE_TYPE (node) = error_mark_node;
14680 return error_mark_node;
14681 }
14682
14683 TREE_TYPE (node) = resulting_type;
14684 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14685 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14686 return node;
14687}
ac825856 14688
5b09b33e
PB
14689/* Try to constant fold NODE.
14690 If NODE is not a constant expression, return NULL_EXPR.
14691 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14692
14693static tree
14694fold_constant_for_init (node, context)
14695 tree node;
14696 tree context;
14697{
14698 tree op0, op1, val;
14699 enum tree_code code = TREE_CODE (node);
14700
93024893 14701 if (code == STRING_CST)
5b09b33e 14702 return node;
93024893
APB
14703
14704 if (code == INTEGER_CST || code == REAL_CST)
14705 return convert (TREE_TYPE (context), node);
5b09b33e
PB
14706
14707 switch (code)
14708 {
5b09b33e
PB
14709 case PLUS_EXPR:
14710 case MINUS_EXPR:
bc3ca41b
PB
14711 case MULT_EXPR:
14712 case TRUNC_MOD_EXPR:
14713 case RDIV_EXPR:
5b09b33e
PB
14714 case LSHIFT_EXPR:
14715 case RSHIFT_EXPR:
14716 case URSHIFT_EXPR:
14717 case BIT_AND_EXPR:
14718 case BIT_XOR_EXPR:
14719 case BIT_IOR_EXPR:
5b09b33e
PB
14720 case TRUTH_ANDIF_EXPR:
14721 case TRUTH_ORIF_EXPR:
14722 case EQ_EXPR:
14723 case NE_EXPR:
14724 case GT_EXPR:
14725 case GE_EXPR:
14726 case LT_EXPR:
14727 case LE_EXPR:
14728 op0 = TREE_OPERAND (node, 0);
14729 op1 = TREE_OPERAND (node, 1);
14730 val = fold_constant_for_init (op0, context);
14731 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14732 return NULL_TREE;
14733 TREE_OPERAND (node, 0) = val;
14734 val = fold_constant_for_init (op1, context);
14735 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14736 return NULL_TREE;
14737 TREE_OPERAND (node, 1) = val;
14738 return patch_binop (node, op0, op1);
14739
14740 case UNARY_PLUS_EXPR:
14741 case NEGATE_EXPR:
14742 case TRUTH_NOT_EXPR:
14743 case BIT_NOT_EXPR:
14744 case CONVERT_EXPR:
14745 op0 = TREE_OPERAND (node, 0);
14746 val = fold_constant_for_init (op0, context);
14747 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14748 return NULL_TREE;
14749 TREE_OPERAND (node, 0) = val;
5a005d9e 14750 return patch_unaryop (node, op0);
5b09b33e
PB
14751 break;
14752
14753 case COND_EXPR:
14754 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14755 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14756 return NULL_TREE;
14757 TREE_OPERAND (node, 0) = val;
14758 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14759 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14760 return NULL_TREE;
14761 TREE_OPERAND (node, 1) = val;
14762 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14763 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14764 return NULL_TREE;
14765 TREE_OPERAND (node, 2) = val;
14766 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14767 : TREE_OPERAND (node, 2);
14768
14769 case VAR_DECL:
8576f094
APB
14770 case FIELD_DECL:
14771 if (! FIELD_FINAL (node)
5b09b33e
PB
14772 || DECL_INITIAL (node) == NULL_TREE)
14773 return NULL_TREE;
14774 val = DECL_INITIAL (node);
14775 /* Guard against infinite recursion. */
14776 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 14777 val = fold_constant_for_init (val, node);
5b09b33e
PB
14778 DECL_INITIAL (node) = val;
14779 return val;
14780
14781 case EXPR_WITH_FILE_LOCATION:
14782 /* Compare java_complete_tree and resolve_expression_name. */
14783 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14784 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14785 {
14786 tree name = EXPR_WFL_NODE (node);
14787 tree decl;
14788 if (PRIMARY_P (node))
14789 return NULL_TREE;
14790 else if (! QUALIFIED_P (name))
14791 {
14792 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
14793 if (decl == NULL_TREE
14794 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
14795 return NULL_TREE;
14796 return fold_constant_for_init (decl, decl);
14797 }
14798 else
14799 {
5b09b33e
PB
14800 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14801 qualify_ambiguous_name (node);
14802 if (resolve_field_access (node, &decl, NULL)
14803 && decl != NULL_TREE)
14804 return fold_constant_for_init (decl, decl);
5b09b33e
PB
14805 return NULL_TREE;
14806 }
14807 }
14808 else
14809 {
14810 op0 = TREE_OPERAND (node, 0);
14811 val = fold_constant_for_init (op0, context);
14812 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14813 return NULL_TREE;
14814 TREE_OPERAND (node, 0) = val;
14815 return val;
14816 }
14817
bc3ca41b
PB
14818#ifdef USE_COMPONENT_REF
14819 case IDENTIFIER:
14820 case COMPONENT_REF:
14821 ?;
14822#endif
14823
5b09b33e
PB
14824 default:
14825 return NULL_TREE;
14826 }
14827}
bc3ca41b
PB
14828
14829#ifdef USE_COMPONENT_REF
14830/* Context is 'T' for TypeName, 'P' for PackageName,
14831 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
14832
14833tree
14834resolve_simple_name (name, context)
14835 tree name;
14836 int context;
14837{
14838}
14839
14840tree
14841resolve_qualified_name (name, context)
14842 tree name;
14843 int context;
14844{
14845}
14846#endif
This page took 2.366094 seconds and 5 git commands to generate.