]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
c-lang.c: #include diagnostic.h
[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));
98a52c2c 84static tree parser_qualified_classname PARAMS ((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));
9a7ab4b3 102static void find_in_imports PARAMS ((tree));
cf1748bf 103static void check_inner_class_access PARAMS ((tree, tree, tree));
df32d2ce 104static int check_pkg_class_access PARAMS ((tree, tree));
9a7ab4b3 105static void register_package PARAMS ((tree));
df32d2ce
KG
106static tree resolve_package PARAMS ((tree, tree *));
107static tree lookup_package_type PARAMS ((const char *, int));
108static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 109static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
110static void declare_local_variables PARAMS ((int, tree, tree));
111static void source_start_java_method PARAMS ((tree));
112static void source_end_java_method PARAMS ((void));
113static void expand_start_java_method PARAMS ((tree));
114static tree find_name_in_single_imports PARAMS ((tree));
115static void check_abstract_method_header PARAMS ((tree));
116static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 118static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 119static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 120 tree, tree, tree));
df32d2ce 121static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 122 int *, tree *));
df32d2ce
KG
123static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124static tree resolve_and_layout PARAMS ((tree, tree));
9a7ab4b3 125static tree qualify_and_find PARAMS ((tree, tree, tree));
df32d2ce
KG
126static tree resolve_no_layout PARAMS ((tree, tree));
127static int invocation_mode PARAMS ((tree, int));
128static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 129 tree, tree));
df32d2ce 130static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 131 tree *, tree *));
df32d2ce
KG
132static tree find_most_specific_methods_list PARAMS ((tree));
133static int argument_types_convertible PARAMS ((tree, tree));
134static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 135static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
136static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
137static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
138static tree obtain_incomplete_type PARAMS ((tree));
139static tree java_complete_lhs PARAMS ((tree));
140static tree java_complete_tree PARAMS ((tree));
c2952b01 141static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
92d83515 142static int maybe_yank_clinit PARAMS ((tree));
df32d2ce
KG
143static void java_complete_expand_method PARAMS ((tree));
144static int unresolved_type_p PARAMS ((tree, tree *));
145static void create_jdep_list PARAMS ((struct parser_ctxt *));
146static tree build_expr_block PARAMS ((tree, tree));
147static tree enter_block PARAMS ((void));
148static tree enter_a_block PARAMS ((tree));
149static tree exit_block PARAMS ((void));
150static tree lookup_name_in_blocks PARAMS ((tree));
151static void maybe_absorb_scoping_blocks PARAMS ((void));
152static tree build_method_invocation PARAMS ((tree, tree));
153static tree build_new_invocation PARAMS ((tree, tree));
154static tree build_assignment PARAMS ((int, int, tree, tree));
155static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
156static int check_final_assignment PARAMS ((tree ,tree));
157static tree patch_assignment PARAMS ((tree, tree, tree ));
158static tree patch_binop PARAMS ((tree, tree, tree));
159static tree build_unaryop PARAMS ((int, int, tree));
160static tree build_incdec PARAMS ((int, int, tree, int));
161static tree patch_unaryop PARAMS ((tree, tree));
162static tree build_cast PARAMS ((int, tree, tree));
163static tree build_null_of_type PARAMS ((tree));
164static tree patch_cast PARAMS ((tree, tree));
165static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
166static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
167static int valid_cast_to_p PARAMS ((tree, tree));
168static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
169static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
170static tree try_reference_assignconv PARAMS ((tree, tree));
171static tree build_unresolved_array_type PARAMS ((tree));
172static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
173static tree build_array_ref PARAMS ((int, tree, tree));
174static tree patch_array_ref PARAMS ((tree));
175static tree make_qualified_name PARAMS ((tree, tree, int));
176static tree merge_qualified_name PARAMS ((tree, tree));
177static tree make_qualified_primary PARAMS ((tree, tree, int));
178static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 179 tree *, tree *));
df32d2ce 180static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
181static tree resolve_field_access PARAMS ((tree, tree *, tree *));
182static tree build_newarray_node PARAMS ((tree, tree, int));
183static tree patch_newarray PARAMS ((tree));
184static tree resolve_type_during_patch PARAMS ((tree));
185static tree build_this PARAMS ((int));
9a7ab4b3 186static tree build_wfl_wrap PARAMS ((tree, int));
df32d2ce
KG
187static tree build_return PARAMS ((int, tree));
188static tree patch_return PARAMS ((tree));
189static tree maybe_access_field PARAMS ((tree, tree, tree));
190static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
191static int check_for_static_method_reference PARAMS ((tree, tree, tree,
192 tree, tree));
df32d2ce
KG
193static int not_accessible_p PARAMS ((tree, tree, int));
194static void check_deprecation PARAMS ((tree, tree));
195static int class_in_current_package PARAMS ((tree));
196static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
197static tree patch_if_else_statement PARAMS ((tree));
198static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
199static tree add_stmt_to_block PARAMS ((tree, tree, tree));
200static tree patch_exit_expr PARAMS ((tree));
201static tree build_labeled_block PARAMS ((int, tree));
202static tree finish_labeled_statement PARAMS ((tree, tree));
203static tree build_bc_statement PARAMS ((int, int, tree));
204static tree patch_bc_statement PARAMS ((tree));
205static tree patch_loop_statement PARAMS ((tree));
206static tree build_new_loop PARAMS ((tree));
207static tree build_loop_body PARAMS ((int, tree, int));
208static tree finish_loop_body PARAMS ((int, tree, tree, int));
209static tree build_debugable_stmt PARAMS ((int, tree));
210static tree finish_for_loop PARAMS ((int, tree, tree, tree));
211static tree patch_switch_statement PARAMS ((tree));
212static tree string_constant_concatenation PARAMS ((tree, tree));
213static tree build_string_concatenation PARAMS ((tree, tree));
214static tree patch_string_cst PARAMS ((tree));
215static tree patch_string PARAMS ((tree));
216static tree build_try_statement PARAMS ((int, tree, tree));
217static tree build_try_finally_statement PARAMS ((int, tree, tree));
218static tree patch_try_statement PARAMS ((tree));
219static tree patch_synchronized_statement PARAMS ((tree, tree));
220static tree patch_throw_statement PARAMS ((tree, tree));
221static void check_thrown_exceptions PARAMS ((int, tree));
222static int check_thrown_exceptions_do PARAMS ((tree));
223static void purge_unchecked_exceptions PARAMS ((tree));
224static void check_throws_clauses PARAMS ((tree, tree, tree));
225static void finish_method_declaration PARAMS ((tree));
226static tree build_super_invocation PARAMS ((tree));
227static int verify_constructor_circularity PARAMS ((tree, tree));
228static char *constructor_circularity_msg PARAMS ((tree, tree));
229static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 230 int, int));
df32d2ce
KG
231static const char *get_printable_method_name PARAMS ((tree));
232static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
233static tree generate_finit PARAMS ((tree));
234static void add_instance_initializer PARAMS ((tree));
df32d2ce 235static void fix_constructors PARAMS ((tree));
c2952b01
APB
236static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
237 tree, int *));
238static void craft_constructor PARAMS ((tree, tree));
239static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
240static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
241static void start_artificial_method_body PARAMS ((tree));
242static void end_artificial_method_body PARAMS ((tree));
243static int check_method_redefinition PARAMS ((tree, tree));
244static int reset_method_name PARAMS ((tree));
165f37bc 245static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
246static void java_check_regular_methods PARAMS ((tree));
247static void java_check_abstract_methods PARAMS ((tree));
248static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
249static void unreachable_stmt_error PARAMS ((tree));
250static tree find_expr_with_wfl PARAMS ((tree));
251static void missing_return_error PARAMS ((tree));
252static tree build_new_array_init PARAMS ((int, tree));
253static tree patch_new_array_init PARAMS ((tree, tree));
254static tree maybe_build_array_element_wfl PARAMS ((tree));
255static int array_constructor_check_entry PARAMS ((tree, tree));
256static const char *purify_type_name PARAMS ((const char *));
257static tree fold_constant_for_init PARAMS ((tree, tree));
258static tree strip_out_static_field_access_decl PARAMS ((tree));
259static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
260static void static_ref_err PARAMS ((tree, tree, tree));
261static void parser_add_interface PARAMS ((tree, tree, tree));
262static void add_superinterfaces PARAMS ((tree, tree));
263static tree jdep_resolve_class PARAMS ((jdep *));
264static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
265static void java_complete_expand_classes PARAMS ((void));
266static void java_complete_expand_class PARAMS ((tree));
267static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
268static tree cut_identifier_in_qualified PARAMS ((tree));
269static tree java_stabilize_reference PARAMS ((tree));
270static tree do_unary_numeric_promotion PARAMS ((tree));
271static char * operator_string PARAMS ((tree));
272static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
273static tree merge_string_cste PARAMS ((tree, tree, int));
274static tree java_refold PARAMS ((tree));
275static int java_decl_equiv PARAMS ((tree, tree));
276static int binop_compound_p PARAMS ((enum tree_code));
277static tree search_loop PARAMS ((tree));
278static int labeled_block_contains_loop_p PARAMS ((tree, tree));
279static void check_abstract_method_definitions PARAMS ((int, tree, tree));
280static void java_check_abstract_method_definitions PARAMS ((tree));
281static void java_debug_context_do PARAMS ((int));
c2952b01
APB
282static void java_parser_context_push_initialized_field PARAMS ((void));
283static void java_parser_context_pop_initialized_field PARAMS ((void));
284static tree reorder_static_initialized PARAMS ((tree));
285static void java_parser_context_suspend PARAMS ((void));
286static void java_parser_context_resume PARAMS ((void));
287
288/* JDK 1.1 work. FIXME */
289
290static tree maybe_make_nested_class_name PARAMS ((tree));
291static void make_nested_class_name PARAMS ((tree));
292static void set_nested_class_simple_name_value PARAMS ((tree, int));
293static void link_nested_class_to_enclosing PARAMS ((void));
294static tree find_as_inner_class PARAMS ((tree, tree, tree));
295static tree find_as_inner_class_do PARAMS ((tree, tree));
296static int check_inner_class_redefinition PARAMS ((tree, tree));
297
298static tree build_thisn_assign PARAMS ((void));
299static tree build_current_thisn PARAMS ((tree));
300static tree build_access_to_thisn PARAMS ((tree, tree, int));
301static tree maybe_build_thisn_access_method PARAMS ((tree));
302
303static tree build_outer_field_access PARAMS ((tree, tree));
304static tree build_outer_field_access_methods PARAMS ((tree));
305static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
306 tree, tree));
307static tree build_outer_method_access_method PARAMS ((tree));
308static tree build_new_access_id PARAMS ((void));
309static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
310 tree, tree));
311
312static int outer_field_access_p PARAMS ((tree, tree));
313static int outer_field_expanded_access_p PARAMS ((tree, tree *,
314 tree *, tree *));
315static tree outer_field_access_fix PARAMS ((tree, tree, tree));
316static tree build_incomplete_class_ref PARAMS ((int, tree));
317static tree patch_incomplete_class_ref PARAMS ((tree));
318static tree create_anonymous_class PARAMS ((int, tree));
319static void patch_anonymous_class PARAMS ((tree, tree, tree));
320static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 321
165f37bc
APB
322static tree build_dot_class_method PARAMS ((tree));
323static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 324static void create_new_parser_context PARAMS ((int));
165f37bc 325
e04a16fb
AG
326/* Number of error found so far. */
327int java_error_count;
328/* Number of warning found so far. */
329int java_warning_count;
ce6e9147
APB
330/* Tell when not to fold, when doing xrefs */
331int do_not_fold;
c2952b01
APB
332/* Cyclic inheritance report, as it can be set by layout_class */
333char *cyclic_inheritance_report;
334
335/* Tell when we're within an instance initializer */
336static int in_instance_initializer;
e04a16fb
AG
337
338/* The current parser context */
d4370213 339struct parser_ctxt *ctxp;
e04a16fb 340
d4370213 341/* List of things that were analyzed for which code will be generated */
b351b287
APB
342static struct parser_ctxt *ctxp_for_generation = NULL;
343
e04a16fb
AG
344/* binop_lookup maps token to tree_code. It is used where binary
345 operations are involved and required by the parser. RDIV_EXPR
346 covers both integral/floating point division. The code is changed
347 once the type of both operator is worked out. */
348
349static enum tree_code binop_lookup[19] =
350 {
351 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
352 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
353 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
354 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
355 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
356 };
357#define BINOP_LOOKUP(VALUE) \
358 binop_lookup [((VALUE) - PLUS_TK)% \
359 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
360
5cbdba64
APB
361/* This is the end index for binary operators that can also be used
362 in compound assignements. */
363#define BINOP_COMPOUND_CANDIDATES 11
364
e04a16fb
AG
365/* Fake WFL used to report error message. It is initialized once if
366 needed and reused with it's location information is overriden. */
15fdcfe9 367tree wfl_operator = NULL_TREE;
e04a16fb
AG
368
369/* The "$L" identifier we use to create labels. */
b67d701b
PB
370static tree label_id = NULL_TREE;
371
372/* The "StringBuffer" identifier used for the String `+' operator. */
373static tree wfl_string_buffer = NULL_TREE;
374
375/* The "append" identifier used for String `+' operator. */
376static tree wfl_append = NULL_TREE;
377
378/* The "toString" identifier used for String `+' operator. */
379static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
380
381/* The "java.lang" import qualified name. */
382static tree java_lang_id = NULL_TREE;
09ed0f70 383
c2952b01
APB
384/* The generated `inst$' identifier used for generated enclosing
385 instance/field access functions. */
386static tree inst_id = NULL_TREE;
387
09ed0f70
APB
388/* The "java.lang.Cloneable" qualified name. */
389static tree java_lang_cloneable = NULL_TREE;
f099f336
APB
390
391/* Context and flag for static blocks */
392static tree current_static_block = NULL_TREE;
393
c2952b01
APB
394/* The generated `write_parm_value$' identifier. */
395static tree wpv_id;
396
ee07f4f4
APB
397/* The list of all packages we've seen so far */
398static tree package_list = NULL_TREE;
2884c41e
KG
399
400/* Check modifiers. If one doesn't fit, retrieve it in its declaration
401 line and point it out. */
402/* Should point out the one that don't fit. ASCII/unicode, going
403 backward. FIXME */
404
405#define check_modifiers(__message, __value, __mask) do { \
406 if ((__value) & ~(__mask)) \
407 { \
408 int i, remainder = (__value) & ~(__mask); \
409 for (i = 0; i <= 10; i++) \
410 if ((1 << i) & remainder) \
411 parse_error_context (ctxp->modifier_ctx [i], (__message), \
412 java_accstring_lookup (1 << i)); \
413 } \
414} while (0)
ee07f4f4 415
e04a16fb
AG
416%}
417
418%union {
419 tree node;
420 int sub_token;
421 struct {
422 int token;
423 int location;
424 } operator;
425 int value;
426}
427
9ee9b555
KG
428%{
429#include "lex.c"
430%}
431
e04a16fb
AG
432%pure_parser
433
434/* Things defined here have to match the order of what's in the
435 binop_lookup table. */
436
437%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
438%token LS_TK SRS_TK ZRS_TK
439%token AND_TK XOR_TK OR_TK
440%token BOOL_AND_TK BOOL_OR_TK
441%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
442
443/* This maps to the same binop_lookup entry than the token above */
444
445%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
446%token REM_ASSIGN_TK
447%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
448%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
449
450
451/* Modifier TOKEN have to be kept in this order. Don't scramble it */
452
453%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
454%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
455%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
456%token PAD_TK ABSTRACT_TK MODIFIER_TK
457
458/* Keep those two in order, too */
459%token DECR_TK INCR_TK
460
461/* From now one, things can be in any order */
462
463%token DEFAULT_TK IF_TK THROW_TK
464%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
465%token THROWS_TK BREAK_TK IMPORT_TK
466%token ELSE_TK INSTANCEOF_TK RETURN_TK
467%token VOID_TK CATCH_TK INTERFACE_TK
468%token CASE_TK EXTENDS_TK FINALLY_TK
469%token SUPER_TK WHILE_TK CLASS_TK
470%token SWITCH_TK CONST_TK TRY_TK
471%token FOR_TK NEW_TK CONTINUE_TK
472%token GOTO_TK PACKAGE_TK THIS_TK
473
474%token BYTE_TK SHORT_TK INT_TK LONG_TK
475%token CHAR_TK INTEGRAL_TK
476
477%token FLOAT_TK DOUBLE_TK FP_TK
478
479%token ID_TK
480
481%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
482
483%token ASSIGN_ANY_TK ASSIGN_TK
484%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
485
486%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
487%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
488
c2952b01 489%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
490
491%type <node> super ID_TK identifier
492%type <node> name simple_name qualified_name
c2952b01 493%type <node> type_declaration compilation_unit
e04a16fb
AG
494 field_declaration method_declaration extends_interfaces
495 interfaces interface_type_list
c2952b01 496 class_member_declaration
e04a16fb
AG
497 import_declarations package_declaration
498 type_declarations interface_body
499 interface_member_declaration constant_declaration
500 interface_member_declarations interface_type
501 abstract_method_declaration interface_type_list
502%type <node> class_body_declaration class_member_declaration
503 static_initializer constructor_declaration block
22eed1e6 504%type <node> class_body_declarations constructor_header
e04a16fb
AG
505%type <node> class_or_interface_type class_type class_type_list
506 constructor_declarator explicit_constructor_invocation
b9f7e36c 507%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
508
509%type <node> variable_declarator_id variable_declarator
510 variable_declarators variable_initializer
22eed1e6 511 variable_initializers constructor_body
ac825856 512 array_initializer
e04a16fb 513
2e5eb5c5 514%type <node> class_body block_end constructor_block_end
e04a16fb
AG
515%type <node> statement statement_without_trailing_substatement
516 labeled_statement if_then_statement label_decl
517 if_then_else_statement while_statement for_statement
518 statement_nsi labeled_statement_nsi do_statement
519 if_then_else_statement_nsi while_statement_nsi
520 for_statement_nsi statement_expression_list for_init
521 for_update statement_expression expression_statement
522 primary_no_new_array expression primary
523 array_creation_expression array_type
524 class_instance_creation_expression field_access
525 method_invocation array_access something_dot_new
526 argument_list postfix_expression while_expression
527 post_increment_expression post_decrement_expression
528 unary_expression_not_plus_minus unary_expression
529 pre_increment_expression pre_decrement_expression
530 unary_expression_not_plus_minus cast_expression
531 multiplicative_expression additive_expression
532 shift_expression relational_expression
533 equality_expression and_expression
534 exclusive_or_expression inclusive_or_expression
535 conditional_and_expression conditional_or_expression
536 conditional_expression assignment_expression
537 left_hand_side assignment for_header for_begin
538 constant_expression do_statement_begin empty_statement
b67d701b 539 switch_statement synchronized_statement throw_statement
f8976021 540 try_statement switch_expression switch_block
15fdcfe9 541 catches catch_clause catch_clause_parameter finally
c2952b01 542 anonymous_class_creation
e04a16fb
AG
543%type <node> return_statement break_statement continue_statement
544
545%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
546%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
547%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
548%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
549%type <operator> ASSIGN_ANY_TK assignment_operator
550%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
551%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
552%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 553%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 554%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
555%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
556%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 557%type <operator> NEW_TK
e04a16fb
AG
558
559%type <node> method_body
560
561%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
562 STRING_LIT_TK NULL_TK VOID_TK
563
564%type <node> IF_TK WHILE_TK FOR_TK
565
566%type <node> formal_parameter_list formal_parameter
567 method_declarator method_header
568
c2952b01 569%type <node> primitive_type reference_type type
e04a16fb
AG
570 BOOLEAN_TK INTEGRAL_TK FP_TK
571
c2952b01
APB
572/* Added or modified JDK 1.1 rule types */
573%type <node> type_literals array_type_literal
574
e04a16fb
AG
575%%
576/* 19.2 Production from 2.3: The Syntactic Grammar */
577goal:
578 compilation_unit
579 {}
580;
581
582/* 19.3 Productions from 3: Lexical structure */
583literal:
584 INT_LIT_TK
585| FP_LIT_TK
586| BOOL_LIT_TK
587| CHAR_LIT_TK
588| STRING_LIT_TK
589| NULL_TK
590;
591
592/* 19.4 Productions from 4: Types, Values and Variables */
593type:
594 primitive_type
595| reference_type
596;
597
598primitive_type:
599 INTEGRAL_TK
600| FP_TK
601| BOOLEAN_TK
602;
603
604reference_type:
605 class_or_interface_type
606| array_type
607;
608
609class_or_interface_type:
610 name
611;
612
613class_type:
614 class_or_interface_type /* Default rule */
615;
616
617interface_type:
618 class_or_interface_type
619;
620
621array_type:
622 primitive_type OSB_TK CSB_TK
623 {
624 $$ = build_java_array_type ($1, -1);
625 CLASS_LOADED_P ($$) = 1;
626 }
627| name OSB_TK CSB_TK
628 { $$ = build_unresolved_array_type ($1); }
629| array_type OSB_TK CSB_TK
630 { $$ = build_unresolved_array_type ($1); }
631| primitive_type OSB_TK error
632 {RULE ("']' expected"); RECOVER;}
633| array_type OSB_TK error
634 {RULE ("']' expected"); RECOVER;}
635;
636
637/* 19.5 Productions from 6: Names */
638name:
639 simple_name /* Default rule */
640| qualified_name /* Default rule */
641;
642
643simple_name:
644 identifier /* Default rule */
645;
646
647qualified_name:
648 name DOT_TK identifier
649 { $$ = make_qualified_name ($1, $3, $2.location); }
650;
651
652identifier:
653 ID_TK
654;
655
656/* 19.6: Production from 7: Packages */
657compilation_unit:
658 {$$ = NULL;}
659| package_declaration
660| import_declarations
661| type_declarations
662| package_declaration import_declarations
663| package_declaration type_declarations
664| import_declarations type_declarations
665| package_declaration import_declarations type_declarations
666;
667
668import_declarations:
669 import_declaration
670 {
671 $$ = NULL;
672 }
673| import_declarations import_declaration
674 {
675 $$ = NULL;
676 }
677;
678
679type_declarations:
680 type_declaration
681| type_declarations type_declaration
682;
683
684package_declaration:
685 PACKAGE_TK name SC_TK
ee07f4f4
APB
686 {
687 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 688 register_package (ctxp->package);
ee07f4f4 689 }
e04a16fb
AG
690| PACKAGE_TK error
691 {yyerror ("Missing name"); RECOVER;}
692| PACKAGE_TK name error
693 {yyerror ("';' expected"); RECOVER;}
694;
695
696import_declaration:
697 single_type_import_declaration
698| type_import_on_demand_declaration
699;
700
701single_type_import_declaration:
702 IMPORT_TK name SC_TK
703 {
9a7ab4b3 704 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 705 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 706 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
707 while (last != IDENTIFIER_POINTER (name))
708 {
709 if (last [0] == '.')
710 break;
711 last--;
712 }
713 last_name = get_identifier (++last);
714 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
715 {
716 tree err = find_name_in_single_imports (last_name);
717 if (err && err != name)
718 parse_error_context
719 ($2, "Ambiguous class: `%s' and `%s'",
720 IDENTIFIER_POINTER (name),
721 IDENTIFIER_POINTER (err));
5e942c50 722 else
9a7ab4b3 723 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
724 }
725 else
5e942c50 726 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
727 }
728| IMPORT_TK error
729 {yyerror ("Missing name"); RECOVER;}
730| IMPORT_TK name error
731 {yyerror ("';' expected"); RECOVER;}
732;
733
734type_import_on_demand_declaration:
735 IMPORT_TK name DOT_TK MULT_TK SC_TK
736 {
737 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
738 /* Don't import java.lang.* twice. */
739 if (name != java_lang_id)
740 {
ba179f9f 741 read_import_dir ($2);
9a7ab4b3
APB
742 ctxp->import_demand_list =
743 chainon (ctxp->import_demand_list,
744 build_tree_list ($2, NULL_TREE));
ba179f9f 745 }
e04a16fb
AG
746 }
747| IMPORT_TK name DOT_TK error
748 {yyerror ("'*' expected"); RECOVER;}
749| IMPORT_TK name DOT_TK MULT_TK error
750 {yyerror ("';' expected"); RECOVER;}
751;
752
753type_declaration:
754 class_declaration
c2952b01 755 { end_class_declaration (0); }
e04a16fb 756| interface_declaration
c2952b01 757 { end_class_declaration (0); }
e04a16fb
AG
758| SC_TK
759 { $$ = NULL; }
760| error
761 {
762 YYERROR_NOW;
763 yyerror ("Class or interface declaration expected");
764 }
765;
766
767/* 19.7 Shortened from the original:
768 modifiers: modifier | modifiers modifier
769 modifier: any of public... */
770modifiers:
771 MODIFIER_TK
772 {
773 $$ = (1 << $1);
774 }
775| modifiers MODIFIER_TK
776 {
777 int acc = (1 << $2);
778 if ($$ & acc)
779 parse_error_context
780 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
781 java_accstring_lookup (acc));
782 else
783 {
784 $$ |= acc;
785 }
786 }
787;
788
789/* 19.8.1 Production from $8.1: Class Declaration */
790class_declaration:
791 modifiers CLASS_TK identifier super interfaces
792 { create_class ($1, $3, $4, $5); }
793 class_body
e04a16fb
AG
794| CLASS_TK identifier super interfaces
795 { create_class (0, $2, $3, $4); }
796 class_body
e04a16fb
AG
797| modifiers CLASS_TK error
798 {yyerror ("Missing class name"); RECOVER;}
799| CLASS_TK error
800 {yyerror ("Missing class name"); RECOVER;}
801| CLASS_TK identifier error
0b4d333e
APB
802 {
803 if (!ctxp->class_err) yyerror ("'{' expected");
804 DRECOVER(class1);
805 }
e04a16fb
AG
806| modifiers CLASS_TK identifier error
807 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
808;
809
810super:
811 { $$ = NULL; }
812| EXTENDS_TK class_type
813 { $$ = $2; }
814| EXTENDS_TK class_type error
815 {yyerror ("'{' expected"); ctxp->class_err=1;}
816| EXTENDS_TK error
817 {yyerror ("Missing super class name"); ctxp->class_err=1;}
818;
819
820interfaces:
821 { $$ = NULL_TREE; }
822| IMPLEMENTS_TK interface_type_list
823 { $$ = $2; }
824| IMPLEMENTS_TK error
825 {
826 ctxp->class_err=1;
827 yyerror ("Missing interface name");
828 }
829;
830
831interface_type_list:
832 interface_type
833 {
834 ctxp->interface_number = 1;
835 $$ = build_tree_list ($1, NULL_TREE);
836 }
837| interface_type_list C_TK interface_type
838 {
839 ctxp->interface_number++;
840 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
841 }
842| interface_type_list C_TK error
843 {yyerror ("Missing interface name"); RECOVER;}
844;
845
846class_body:
847 OCB_TK CCB_TK
7f10c2e2
APB
848 {
849 /* Store the location of the `}' when doing xrefs */
850 if (flag_emit_xref)
c2952b01 851 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 852 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 853 $$ = GET_CPC ();
7f10c2e2 854 }
e04a16fb 855| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
856 {
857 /* Store the location of the `}' when doing xrefs */
858 if (flag_emit_xref)
c2952b01 859 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 860 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 861 $$ = GET_CPC ();
7f10c2e2 862 }
e04a16fb
AG
863;
864
865class_body_declarations:
866 class_body_declaration
867| class_body_declarations class_body_declaration
868;
869
870class_body_declaration:
871 class_member_declaration
872| static_initializer
873| constructor_declaration
874| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
875 {
876 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
877 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
878 }
e04a16fb
AG
879;
880
881class_member_declaration:
882 field_declaration
0b4d333e
APB
883| field_declaration SC_TK
884 { $$ = $1; }
e04a16fb
AG
885| method_declaration
886| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
887 { end_class_declaration (1); }
888| interface_declaration /* Added, JDK1.1 inner interfaces */
889 { end_class_declaration (1); }
e04a16fb
AG
890;
891
892/* 19.8.2 Productions from 8.3: Field Declarations */
893field_declaration:
894 type variable_declarators SC_TK
895 { register_fields (0, $1, $2); }
896| modifiers type variable_declarators SC_TK
897 {
e04a16fb
AG
898 check_modifiers
899 ("Illegal modifier `%s' for field declaration",
900 $1, FIELD_MODIFIERS);
901 check_modifiers_consistency ($1);
902 register_fields ($1, $2, $3);
903 }
904;
905
906variable_declarators:
907 /* Should we use build_decl_list () instead ? FIXME */
908 variable_declarator /* Default rule */
909| variable_declarators C_TK variable_declarator
910 { $$ = chainon ($1, $3); }
911| variable_declarators C_TK error
912 {yyerror ("Missing term"); RECOVER;}
913;
914
915variable_declarator:
916 variable_declarator_id
917 { $$ = build_tree_list ($1, NULL_TREE); }
918| variable_declarator_id ASSIGN_TK variable_initializer
919 {
920 if (java_error_count)
921 $3 = NULL_TREE;
922 $$ = build_tree_list
923 ($1, build_assignment ($2.token, $2.location, $1, $3));
924 }
925| variable_declarator_id ASSIGN_TK error
926 {
927 yyerror ("Missing variable initializer");
928 $$ = build_tree_list ($1, NULL_TREE);
929 RECOVER;
930 }
931| variable_declarator_id ASSIGN_TK variable_initializer error
932 {
933 yyerror ("';' expected");
934 $$ = build_tree_list ($1, NULL_TREE);
935 RECOVER;
936 }
937;
938
939variable_declarator_id:
940 identifier
941| variable_declarator_id OSB_TK CSB_TK
c583dd46 942 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
943| identifier error
944 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
945| variable_declarator_id OSB_TK error
946 {yyerror ("']' expected"); DRECOVER(vdi);}
947| variable_declarator_id CSB_TK error
948 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
949;
950
951variable_initializer:
952 expression
953| array_initializer
e04a16fb
AG
954;
955
956/* 19.8.3 Productions from 8.4: Method Declarations */
957method_declaration:
958 method_header
959 {
960 current_function_decl = $1;
c2952b01
APB
961 if (current_function_decl
962 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
963 source_start_java_method (current_function_decl);
964 else
965 current_function_decl = NULL_TREE;
e04a16fb
AG
966 }
967 method_body
b635eb2f 968 { finish_method_declaration ($3); }
e04a16fb
AG
969| method_header error
970 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
971;
972
973method_header:
974 type method_declarator throws
b9f7e36c 975 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 976| VOID_TK method_declarator throws
b9f7e36c 977 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 978| modifiers type method_declarator throws
b9f7e36c 979 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 980| modifiers VOID_TK method_declarator throws
b9f7e36c 981 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 982| type error
efa0a23f
APB
983 {
984 yyerror ("Invalid method declaration, method name required");
985 RECOVER;
986 }
e04a16fb
AG
987| modifiers type error
988 {RECOVER;}
989| VOID_TK error
990 {yyerror ("Identifier expected"); RECOVER;}
991| modifiers VOID_TK error
992 {yyerror ("Identifier expected"); RECOVER;}
993| modifiers error
994 {
995 yyerror ("Invalid method declaration, return type required");
996 RECOVER;
997 }
998;
999
1000method_declarator:
1001 identifier OP_TK CP_TK
c2952b01
APB
1002 {
1003 ctxp->formal_parameter_number = 0;
1004 $$ = method_declarator ($1, NULL_TREE);
1005 }
e04a16fb
AG
1006| identifier OP_TK formal_parameter_list CP_TK
1007 { $$ = method_declarator ($1, $3); }
1008| method_declarator OSB_TK CSB_TK
1009 {
1886c9d8
APB
1010 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1011 TREE_PURPOSE ($1) =
1012 build_unresolved_array_type (TREE_PURPOSE ($1));
1013 parse_warning_context
1014 (wfl_operator,
1015 "Discouraged form of returned type specification");
e04a16fb
AG
1016 }
1017| identifier OP_TK error
1018 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1019| method_declarator OSB_TK error
1020 {yyerror ("']' expected"); RECOVER;}
1021;
1022
1023formal_parameter_list:
1024 formal_parameter
1025 {
1026 ctxp->formal_parameter_number = 1;
1027 }
1028| formal_parameter_list C_TK formal_parameter
1029 {
1030 ctxp->formal_parameter_number += 1;
1031 $$ = chainon ($1, $3);
1032 }
1033| formal_parameter_list C_TK error
c2952b01 1034 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1035;
1036
1037formal_parameter:
1038 type variable_declarator_id
1039 {
1040 $$ = build_tree_list ($2, $1);
1041 }
18990de5 1042| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1043 {
5256aa37 1044 $$ = build_tree_list ($3, $2);
c2952b01 1045 ARG_FINAL_P ($$) = 1;
5256aa37 1046 }
e04a16fb 1047| type error
f8989a66
APB
1048 {
1049 yyerror ("Missing identifier"); RECOVER;
1050 $$ = NULL_TREE;
1051 }
18990de5 1052| final type error
e04a16fb 1053 {
e04a16fb 1054 yyerror ("Missing identifier"); RECOVER;
f8989a66 1055 $$ = NULL_TREE;
e04a16fb
AG
1056 }
1057;
1058
18990de5
JB
1059final:
1060 modifiers
1061 {
1062 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1063 $1, ACC_FINAL);
1064 if ($1 != ACC_FINAL)
1065 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1066 }
1067;
1068
e04a16fb 1069throws:
b9f7e36c 1070 { $$ = NULL_TREE; }
e04a16fb 1071| THROWS_TK class_type_list
b9f7e36c 1072 { $$ = $2; }
e04a16fb
AG
1073| THROWS_TK error
1074 {yyerror ("Missing class type term"); RECOVER;}
1075;
1076
1077class_type_list:
1078 class_type
c877974e 1079 { $$ = build_tree_list ($1, $1); }
e04a16fb 1080| class_type_list C_TK class_type
c877974e 1081 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1082| class_type_list C_TK error
1083 {yyerror ("Missing class type term"); RECOVER;}
1084;
1085
1086method_body:
1087 block
1088| block SC_TK
1089| SC_TK
1090 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
1091;
1092
1093/* 19.8.4 Productions from 8.5: Static Initializers */
1094static_initializer:
1095 static block
1096 {
c2952b01
APB
1097 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1098 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1099 }
1100| static block SC_TK /* Shouldn't be here. FIXME */
1101 {
c2952b01
APB
1102 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1103 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1104 }
1105;
1106
1107static: /* Test lval.sub_token here */
c2952b01 1108 modifiers
e04a16fb 1109 {
c2952b01
APB
1110 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1111 /* Can't have a static initializer in an innerclass */
1112 if ($1 | ACC_STATIC &&
1113 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1114 parse_error_context
1115 (MODIFIER_WFL (STATIC_TK),
1116 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1117 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1118 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1119 }
1120;
1121
1122/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1123constructor_declaration:
22eed1e6 1124 constructor_header
e04a16fb 1125 {
22eed1e6
APB
1126 current_function_decl = $1;
1127 source_start_java_method (current_function_decl);
e04a16fb 1128 }
22eed1e6 1129 constructor_body
b635eb2f 1130 { finish_method_declaration ($3); }
22eed1e6
APB
1131;
1132
1133constructor_header:
1134 constructor_declarator throws
1135 { $$ = method_header (0, NULL_TREE, $1, $2); }
1136| modifiers constructor_declarator throws
1137 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1138;
1139
1140constructor_declarator:
1141 simple_name OP_TK CP_TK
c2952b01
APB
1142 {
1143 ctxp->formal_parameter_number = 0;
1144 $$ = method_declarator ($1, NULL_TREE);
1145 }
e04a16fb 1146| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1147 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1148;
1149
1150constructor_body:
22eed1e6
APB
1151 /* Unlike regular method, we always need a complete (empty)
1152 body so we can safely perform all the required code
1153 addition (super invocation and field initialization) */
2e5eb5c5 1154 block_begin constructor_block_end
22eed1e6 1155 {
9bbc7d9f 1156 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1157 $$ = $2;
1158 }
2e5eb5c5 1159| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1160 { $$ = $3; }
2e5eb5c5 1161| block_begin block_statements constructor_block_end
22eed1e6 1162 { $$ = $3; }
2e5eb5c5 1163| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1164 { $$ = $4; }
e04a16fb
AG
1165;
1166
2e5eb5c5
APB
1167constructor_block_end:
1168 block_end
1169| block_end SC_TK
1170
e04a16fb
AG
1171/* Error recovery for that rule moved down expression_statement: rule. */
1172explicit_constructor_invocation:
1173 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1174 {
1175 $$ = build_method_invocation ($1, NULL_TREE);
1176 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1177 $$ = java_method_add_stmt (current_function_decl, $$);
1178 }
e04a16fb 1179| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1180 {
1181 $$ = build_method_invocation ($1, $3);
1182 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1183 $$ = java_method_add_stmt (current_function_decl, $$);
1184 }
e04a16fb
AG
1185 /* Added, JDK1.1 inner classes. Modified because the rule
1186 'primary' couldn't work. */
1187| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1188 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1189| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1190 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1191;
1192
1193this_or_super: /* Added, simplifies error diagnostics */
1194 THIS_TK
1195 {
9ee9b555 1196 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1197 EXPR_WFL_LINECOL (wfl) = $1.location;
1198 $$ = wfl;
1199 }
1200| SUPER_TK
1201 {
9ee9b555 1202 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1203 EXPR_WFL_LINECOL (wfl) = $1.location;
1204 $$ = wfl;
1205 }
1206;
1207
1208/* 19.9 Productions from 9: Interfaces */
1209/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1210interface_declaration:
1211 INTERFACE_TK identifier
1212 { create_interface (0, $2, NULL_TREE); }
1213 interface_body
e04a16fb
AG
1214| modifiers INTERFACE_TK identifier
1215 { create_interface ($1, $3, NULL_TREE); }
1216 interface_body
e04a16fb
AG
1217| INTERFACE_TK identifier extends_interfaces
1218 { create_interface (0, $2, $3); }
1219 interface_body
e04a16fb
AG
1220| modifiers INTERFACE_TK identifier extends_interfaces
1221 { create_interface ($1, $3, $4); }
1222 interface_body
e04a16fb 1223| INTERFACE_TK identifier error
0b4d333e 1224 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1225| modifiers INTERFACE_TK identifier error
0b4d333e 1226 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1227;
1228
1229extends_interfaces:
1230 EXTENDS_TK interface_type
1231 {
1232 ctxp->interface_number = 1;
1233 $$ = build_tree_list ($2, NULL_TREE);
1234 }
1235| extends_interfaces C_TK interface_type
1236 {
1237 ctxp->interface_number++;
1238 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1239 }
1240| EXTENDS_TK error
1241 {yyerror ("Invalid interface type"); RECOVER;}
1242| extends_interfaces C_TK error
1243 {yyerror ("Missing term"); RECOVER;}
1244;
1245
1246interface_body:
1247 OCB_TK CCB_TK
1248 { $$ = NULL_TREE; }
1249| OCB_TK interface_member_declarations CCB_TK
1250 { $$ = NULL_TREE; }
1251;
1252
1253interface_member_declarations:
1254 interface_member_declaration
1255| interface_member_declarations interface_member_declaration
1256;
1257
1258interface_member_declaration:
1259 constant_declaration
1260| abstract_method_declaration
1261| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1262 { end_class_declaration (1); }
1263| interface_declaration /* Added, JDK1.1 inner interfaces */
1264 { end_class_declaration (1); }
e04a16fb
AG
1265;
1266
1267constant_declaration:
1268 field_declaration
1269;
1270
1271abstract_method_declaration:
1272 method_header SC_TK
1273 {
1274 check_abstract_method_header ($1);
1275 current_function_decl = NULL_TREE; /* FIXME ? */
1276 }
1277| method_header error
1278 {yyerror ("';' expected"); RECOVER;}
1279;
1280
1281/* 19.10 Productions from 10: Arrays */
1282array_initializer:
1283 OCB_TK CCB_TK
1179ebc2 1284 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1285| OCB_TK variable_initializers CCB_TK
f8976021 1286 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1287| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1288 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1289;
1290
1291variable_initializers:
1292 variable_initializer
f8976021
APB
1293 {
1294 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1295 $1, NULL_TREE);
1296 }
e04a16fb 1297| variable_initializers C_TK variable_initializer
1179ebc2
APB
1298 {
1299 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1300 }
e04a16fb
AG
1301| variable_initializers C_TK error
1302 {yyerror ("Missing term"); RECOVER;}
1303;
1304
1305/* 19.11 Production from 14: Blocks and Statements */
1306block:
1307 OCB_TK CCB_TK
7f10c2e2
APB
1308 {
1309 /* Store the location of the `}' when doing xrefs */
1310 if (current_function_decl && flag_emit_xref)
1311 DECL_END_SOURCE_LINE (current_function_decl) =
1312 EXPR_WFL_ADD_COL ($2.location, 1);
1313 $$ = empty_stmt_node;
1314 }
22eed1e6
APB
1315| block_begin block_statements block_end
1316 { $$ = $3; }
1317;
1318
1319block_begin:
1320 OCB_TK
e04a16fb 1321 { enter_block (); }
22eed1e6
APB
1322;
1323
1324block_end:
e04a16fb
AG
1325 CCB_TK
1326 {
1327 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1328 /* Store the location of the `}' when doing xrefs */
1329 if (current_function_decl && flag_emit_xref)
1330 DECL_END_SOURCE_LINE (current_function_decl) =
1331 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1332 $$ = exit_block ();
c280e37a
APB
1333 if (!BLOCK_SUBBLOCKS ($$))
1334 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1335 }
1336;
1337
1338block_statements:
1339 block_statement
1340| block_statements block_statement
1341;
1342
1343block_statement:
1344 local_variable_declaration_statement
1345| statement
15fdcfe9 1346 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1347| class_declaration /* Added, JDK1.1 local classes */
1348 {
1349 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1350 end_class_declaration (1);
1351 }
e04a16fb
AG
1352;
1353
1354local_variable_declaration_statement:
1355 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1356;
1357
1358local_variable_declaration:
1359 type variable_declarators
1360 { declare_local_variables (0, $1, $2); }
a003f638 1361| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1362 { declare_local_variables ($1, $2, $3); }
1363;
1364
1365statement:
1366 statement_without_trailing_substatement
1367| labeled_statement
e04a16fb 1368| if_then_statement
e04a16fb 1369| if_then_else_statement
e04a16fb 1370| while_statement
e04a16fb 1371| for_statement
cd9643f7 1372 { $$ = exit_block (); }
e04a16fb
AG
1373;
1374
1375statement_nsi:
1376 statement_without_trailing_substatement
1377| labeled_statement_nsi
e04a16fb 1378| if_then_else_statement_nsi
e04a16fb 1379| while_statement_nsi
e04a16fb 1380| for_statement_nsi
9dd939b2 1381 { $$ = exit_block (); }
e04a16fb
AG
1382;
1383
1384statement_without_trailing_substatement:
1385 block
e04a16fb 1386| empty_statement
e04a16fb 1387| expression_statement
e04a16fb 1388| switch_statement
e04a16fb 1389| do_statement
e04a16fb 1390| break_statement
e04a16fb 1391| continue_statement
e04a16fb
AG
1392| return_statement
1393| synchronized_statement
e04a16fb 1394| throw_statement
e04a16fb 1395| try_statement
e04a16fb
AG
1396;
1397
1398empty_statement:
1399 SC_TK
9bbc7d9f 1400 { $$ = empty_stmt_node; }
e04a16fb
AG
1401;
1402
1403label_decl:
1404 identifier REL_CL_TK
1405 {
1406 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1407 EXPR_WFL_NODE ($1));
e04a16fb
AG
1408 pushlevel (2);
1409 push_labeled_block ($$);
1410 PUSH_LABELED_BLOCK ($$);
1411 }
1412;
1413
1414labeled_statement:
1415 label_decl statement
b635eb2f 1416 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1417| identifier error
1418 {yyerror ("':' expected"); RECOVER;}
1419;
1420
1421labeled_statement_nsi:
1422 label_decl statement_nsi
b635eb2f 1423 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1424;
1425
1426/* We concentrate here a bunch of error handling rules that we couldn't write
1427 earlier, because expression_statement catches a missing ';'. */
1428expression_statement:
1429 statement_expression SC_TK
1430 {
1431 /* We have a statement. Generate a WFL around it so
1432 we can debug it */
1433 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1434 /* We know we have a statement, so set the debug
1435 info to be eventually generate here. */
1436 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1437 }
1438| error SC_TK
1439 {
1440 if (ctxp->prevent_ese != lineno)
1441 yyerror ("Invalid expression statement");
1442 DRECOVER (expr_stmt);
1443 }
1444| error OCB_TK
1445 {
1446 if (ctxp->prevent_ese != lineno)
1447 yyerror ("Invalid expression statement");
1448 DRECOVER (expr_stmt);
1449 }
1450| error CCB_TK
1451 {
1452 if (ctxp->prevent_ese != lineno)
1453 yyerror ("Invalid expression statement");
1454 DRECOVER (expr_stmt);
1455 }
1456| this_or_super OP_TK error
1457 {yyerror ("')' expected"); RECOVER;}
1458| this_or_super OP_TK CP_TK error
22eed1e6 1459 {
8119c720 1460 parse_ctor_invocation_error ();
22eed1e6
APB
1461 RECOVER;
1462 }
e04a16fb
AG
1463| this_or_super OP_TK argument_list error
1464 {yyerror ("')' expected"); RECOVER;}
1465| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1466 {
8119c720 1467 parse_ctor_invocation_error ();
22eed1e6
APB
1468 RECOVER;
1469 }
e04a16fb
AG
1470| name DOT_TK SUPER_TK error
1471 {yyerror ("'(' expected"); RECOVER;}
1472| name DOT_TK SUPER_TK OP_TK error
1473 {yyerror ("')' expected"); RECOVER;}
1474| name DOT_TK SUPER_TK OP_TK argument_list error
1475 {yyerror ("')' expected"); RECOVER;}
1476| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1477 {yyerror ("';' expected"); RECOVER;}
1478| name DOT_TK SUPER_TK OP_TK CP_TK error
1479 {yyerror ("';' expected"); RECOVER;}
1480;
1481
1482statement_expression:
1483 assignment
1484| pre_increment_expression
e04a16fb 1485| pre_decrement_expression
e04a16fb 1486| post_increment_expression
e04a16fb 1487| post_decrement_expression
e04a16fb
AG
1488| method_invocation
1489| class_instance_creation_expression
e04a16fb
AG
1490;
1491
1492if_then_statement:
1493 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1494 {
1495 $$ = build_if_else_statement ($2.location, $3,
1496 $5, NULL_TREE);
1497 }
e04a16fb
AG
1498| IF_TK error
1499 {yyerror ("'(' expected"); RECOVER;}
1500| IF_TK OP_TK error
1501 {yyerror ("Missing term"); RECOVER;}
1502| IF_TK OP_TK expression error
1503 {yyerror ("')' expected"); RECOVER;}
1504;
1505
1506if_then_else_statement:
1507 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1508 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1509;
1510
1511if_then_else_statement_nsi:
1512 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1513 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1514;
1515
1516switch_statement:
15fdcfe9
PB
1517 switch_expression
1518 {
1519 enter_block ();
1520 }
1521 switch_block
b67d701b 1522 {
15fdcfe9 1523 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1524 I.e. make the last statment also have its own
1525 COMPOUND_EXPR. */
15fdcfe9
PB
1526 maybe_absorb_scoping_blocks ();
1527 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1528 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1529 }
1530;
1531
1532switch_expression:
1533 SWITCH_TK OP_TK expression CP_TK
1534 {
1535 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1536 EXPR_WFL_LINECOL ($$) = $2.location;
1537 }
e04a16fb
AG
1538| SWITCH_TK error
1539 {yyerror ("'(' expected"); RECOVER;}
1540| SWITCH_TK OP_TK error
1541 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1542| SWITCH_TK OP_TK expression CP_TK error
1543 {yyerror ("'{' expected"); RECOVER;}
1544;
1545
f8976021
APB
1546/* Default assignment is there to avoid type node on switch_block
1547 node. */
1548
e04a16fb
AG
1549switch_block:
1550 OCB_TK CCB_TK
f8976021 1551 { $$ = NULL_TREE; }
e04a16fb 1552| OCB_TK switch_labels CCB_TK
f8976021 1553 { $$ = NULL_TREE; }
e04a16fb 1554| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1555 { $$ = NULL_TREE; }
e04a16fb 1556| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1557 { $$ = NULL_TREE; }
e04a16fb
AG
1558;
1559
1560switch_block_statement_groups:
1561 switch_block_statement_group
1562| switch_block_statement_groups switch_block_statement_group
1563;
1564
1565switch_block_statement_group:
15fdcfe9 1566 switch_labels block_statements
e04a16fb
AG
1567;
1568
e04a16fb
AG
1569switch_labels:
1570 switch_label
1571| switch_labels switch_label
1572;
1573
1574switch_label:
1575 CASE_TK constant_expression REL_CL_TK
b67d701b 1576 {
15fdcfe9
PB
1577 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1578 EXPR_WFL_LINECOL (lab) = $1.location;
1579 java_method_add_stmt (current_function_decl, lab);
b67d701b 1580 }
e04a16fb 1581| DEFAULT_TK REL_CL_TK
b67d701b 1582 {
15fdcfe9
PB
1583 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1584 EXPR_WFL_LINECOL (lab) = $1.location;
1585 java_method_add_stmt (current_function_decl, lab);
b67d701b 1586 }
e04a16fb
AG
1587| CASE_TK error
1588 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1589| CASE_TK constant_expression error
1590 {yyerror ("':' expected"); RECOVER;}
1591| DEFAULT_TK error
1592 {yyerror ("':' expected"); RECOVER;}
1593;
1594
1595while_expression:
1596 WHILE_TK OP_TK expression CP_TK
1597 {
1598 tree body = build_loop_body ($2.location, $3, 0);
1599 $$ = build_new_loop (body);
1600 }
1601;
1602
1603while_statement:
1604 while_expression statement
b635eb2f 1605 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1606| WHILE_TK error
1607 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1608| WHILE_TK OP_TK error
1609 {yyerror ("Missing term and ')' expected"); RECOVER;}
1610| WHILE_TK OP_TK expression error
1611 {yyerror ("')' expected"); RECOVER;}
1612;
1613
1614while_statement_nsi:
1615 while_expression statement_nsi
b635eb2f 1616 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1617;
1618
1619do_statement_begin:
1620 DO_TK
1621 {
1622 tree body = build_loop_body (0, NULL_TREE, 1);
1623 $$ = build_new_loop (body);
1624 }
1625 /* Need error handing here. FIXME */
1626;
1627
1628do_statement:
1629 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1630 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1631;
1632
1633for_statement:
1634 for_begin SC_TK expression SC_TK for_update CP_TK statement
774d2baf
TT
1635 {
1636 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1637 $3 = build_wfl_node ($3);
1638 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1639 }
e04a16fb
AG
1640| for_begin SC_TK SC_TK for_update CP_TK statement
1641 {
b635eb2f 1642 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1643 /* We have not condition, so we get rid of the EXIT_EXPR */
1644 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1645 empty_stmt_node;
e04a16fb
AG
1646 }
1647| for_begin SC_TK error
1648 {yyerror ("Invalid control expression"); RECOVER;}
1649| for_begin SC_TK expression SC_TK error
1650 {yyerror ("Invalid update expression"); RECOVER;}
1651| for_begin SC_TK SC_TK error
1652 {yyerror ("Invalid update expression"); RECOVER;}
1653;
1654
1655for_statement_nsi:
1656 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1657 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1658| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1659 {
b635eb2f 1660 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1661 /* We have not condition, so we get rid of the EXIT_EXPR */
1662 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1663 empty_stmt_node;
e04a16fb
AG
1664 }
1665;
1666
1667for_header:
1668 FOR_TK OP_TK
1669 {
1670 /* This scope defined for local variable that may be
1671 defined within the scope of the for loop */
1672 enter_block ();
1673 }
1674| FOR_TK error
1675 {yyerror ("'(' expected"); DRECOVER(for_1);}
1676| FOR_TK OP_TK error
1677 {yyerror ("Invalid init statement"); RECOVER;}
1678;
1679
1680for_begin:
1681 for_header for_init
1682 {
1683 /* We now declare the loop body. The loop is
1684 declared as a for loop. */
1685 tree body = build_loop_body (0, NULL_TREE, 0);
1686 $$ = build_new_loop (body);
c2952b01 1687 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1688 /* The loop is added to the current block the for
1689 statement is defined within */
1690 java_method_add_stmt (current_function_decl, $$);
1691 }
1692;
1693for_init: /* Can be empty */
9bbc7d9f 1694 { $$ = empty_stmt_node; }
e04a16fb
AG
1695| statement_expression_list
1696 {
1697 /* Init statement recorded within the previously
1698 defined block scope */
1699 $$ = java_method_add_stmt (current_function_decl, $1);
1700 }
1701| local_variable_declaration
1702 {
1703 /* Local variable are recorded within the previously
1704 defined block scope */
1705 $$ = NULL_TREE;
1706 }
1707| statement_expression_list error
1708 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1709;
1710
1711for_update: /* Can be empty */
9bbc7d9f 1712 {$$ = empty_stmt_node;}
e04a16fb
AG
1713| statement_expression_list
1714 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1715;
1716
1717statement_expression_list:
1718 statement_expression
1719 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1720| statement_expression_list C_TK statement_expression
1721 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1722| statement_expression_list C_TK error
1723 {yyerror ("Missing term"); RECOVER;}
1724;
1725
1726break_statement:
1727 BREAK_TK SC_TK
1728 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1729| BREAK_TK identifier SC_TK
1730 { $$ = build_bc_statement ($1.location, 1, $2); }
1731| BREAK_TK error
1732 {yyerror ("Missing term"); RECOVER;}
1733| BREAK_TK identifier error
1734 {yyerror ("';' expected"); RECOVER;}
1735;
1736
1737continue_statement:
1738 CONTINUE_TK SC_TK
1739 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1740| CONTINUE_TK identifier SC_TK
1741 { $$ = build_bc_statement ($1.location, 0, $2); }
1742| CONTINUE_TK error
1743 {yyerror ("Missing term"); RECOVER;}
1744| CONTINUE_TK identifier error
1745 {yyerror ("';' expected"); RECOVER;}
1746;
1747
1748return_statement:
1749 RETURN_TK SC_TK
1750 { $$ = build_return ($1.location, NULL_TREE); }
1751| RETURN_TK expression SC_TK
1752 { $$ = build_return ($1.location, $2); }
1753| RETURN_TK error
1754 {yyerror ("Missing term"); RECOVER;}
1755| RETURN_TK expression error
1756 {yyerror ("';' expected"); RECOVER;}
1757;
1758
1759throw_statement:
1760 THROW_TK expression SC_TK
b9f7e36c
APB
1761 {
1762 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1763 EXPR_WFL_LINECOL ($$) = $1.location;
1764 }
e04a16fb
AG
1765| THROW_TK error
1766 {yyerror ("Missing term"); RECOVER;}
1767| THROW_TK expression error
1768 {yyerror ("';' expected"); RECOVER;}
1769;
1770
1771synchronized_statement:
1772 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1773 {
1774 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1775 EXPR_WFL_LINECOL ($$) =
1776 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1777 }
e04a16fb
AG
1778| synchronized OP_TK expression CP_TK error
1779 {yyerror ("'{' expected"); RECOVER;}
1780| synchronized error
1781 {yyerror ("'(' expected"); RECOVER;}
1782| synchronized OP_TK error CP_TK
1783 {yyerror ("Missing term"); RECOVER;}
1784| synchronized OP_TK error
1785 {yyerror ("Missing term"); RECOVER;}
1786;
1787
b9f7e36c 1788synchronized:
efa0a23f 1789 modifiers
e04a16fb 1790 {
781b0558
KG
1791 check_modifiers (
1792 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1793 $1, ACC_SYNCHRONIZED);
1794 if ($1 != ACC_SYNCHRONIZED)
1795 MODIFIER_WFL (SYNCHRONIZED_TK) =
1796 build_wfl_node (NULL_TREE);
e04a16fb
AG
1797 }
1798;
1799
1800try_statement:
1801 TRY_TK block catches
a7d8d81f 1802 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1803| TRY_TK block finally
a7d8d81f 1804 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1805| TRY_TK block catches finally
2aa11e97
APB
1806 { $$ = build_try_finally_statement
1807 ($1.location, build_try_statement ($1.location,
1808 $2, $3), $4);
1809 }
e04a16fb
AG
1810| TRY_TK error
1811 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1812;
1813
1814catches:
1815 catch_clause
1816| catches catch_clause
b67d701b
PB
1817 {
1818 TREE_CHAIN ($2) = $1;
1819 $$ = $2;
1820 }
e04a16fb
AG
1821;
1822
1823catch_clause:
b67d701b
PB
1824 catch_clause_parameter block
1825 {
1826 java_method_add_stmt (current_function_decl, $2);
1827 exit_block ();
1828 $$ = $1;
1829 }
1830
1831catch_clause_parameter:
1832 CATCH_TK OP_TK formal_parameter CP_TK
1833 {
1834 /* We add a block to define a scope for
1835 formal_parameter (CCBP). The formal parameter is
1836 declared initialized by the appropriate function
1837 call */
1838 tree ccpb = enter_block ();
1839 tree init = build_assignment (ASSIGN_TK, $2.location,
1840 TREE_PURPOSE ($3),
1841 soft_exceptioninfo_call_node);
1842 declare_local_variables (0, TREE_VALUE ($3),
1843 build_tree_list (TREE_PURPOSE ($3),
1844 init));
1845 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1846 EXPR_WFL_LINECOL ($$) = $1.location;
1847 }
e04a16fb 1848| CATCH_TK error
97f30284 1849 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1850| CATCH_TK OP_TK error
97f30284
APB
1851 {
1852 yyerror ("Missing term or ')' expected");
1853 RECOVER; $$ = NULL_TREE;
1854 }
b67d701b 1855| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1856 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1857;
1858
1859finally:
1860 FINALLY_TK block
a7d8d81f 1861 { $$ = $2; }
e04a16fb
AG
1862| FINALLY_TK error
1863 {yyerror ("'{' expected"); RECOVER; }
1864;
1865
1866/* 19.12 Production from 15: Expressions */
1867primary:
1868 primary_no_new_array
1869| array_creation_expression
1870;
1871
1872primary_no_new_array:
1873 literal
1874| THIS_TK
1875 { $$ = build_this ($1.location); }
1876| OP_TK expression CP_TK
1877 {$$ = $2;}
1878| class_instance_creation_expression
1879| field_access
1880| method_invocation
1881| array_access
c2952b01 1882| type_literals
e04a16fb
AG
1883 /* Added, JDK1.1 inner classes. Documentation is wrong
1884 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1885 exist. Used name: instead. */
e04a16fb 1886| name DOT_TK THIS_TK
c2952b01
APB
1887 {
1888 tree wfl = build_wfl_node (this_identifier_node);
1889 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1890 }
e04a16fb
AG
1891| OP_TK expression error
1892 {yyerror ("')' expected"); RECOVER;}
1893| name DOT_TK error
1894 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1895| primitive_type DOT_TK error
1896 {yyerror ("'class' expected" ); RECOVER;}
1897| VOID_TK DOT_TK error
1898 {yyerror ("'class' expected" ); RECOVER;}
1899;
1900
c2952b01
APB
1901/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1902 broke the rule down a bit. */
1903
1904array_type_literal:
1905 primitive_type OSB_TK CSB_TK
1906 {
1907 $$ = build_java_array_type ($1, -1);
1908 CLASS_LOADED_P ($$) = 1;
1909 }
1910| name OSB_TK CSB_TK
1911 { $$ = build_unresolved_array_type ($1); }
1912/* This triggers two reduce/reduce conflict between array_type_literal and
1913 dims. FIXME.
1914| array_type OSB_TK CSB_TK
1915 { $$ = build_unresolved_array_type ($1); }
1916*/
1917;
1918
1919type_literals:
1920 name DOT_TK CLASS_TK
1921 { $$ = build_incomplete_class_ref ($2.location, $1); }
1922| array_type_literal DOT_TK CLASS_TK
1923 { $$ = build_incomplete_class_ref ($2.location, $1); }
1924| primitive_type DOT_TK CLASS_TK
1925 { $$ = build_class_ref ($1); }
1926| VOID_TK DOT_TK CLASS_TK
1927 { $$ = build_class_ref (void_type_node); }
1928;
1929
e04a16fb
AG
1930class_instance_creation_expression:
1931 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1932 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1933| NEW_TK class_type OP_TK CP_TK
b67d701b 1934 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1935| anonymous_class_creation
e04a16fb
AG
1936 /* Added, JDK1.1 inner classes, modified to use name or
1937 primary instead of primary solely which couldn't work in
1938 all situations. */
1939| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1940 {
1941 tree ctor = build_new_invocation ($2, NULL_TREE);
1942 $$ = make_qualified_primary ($1, ctor,
1943 EXPR_WFL_LINECOL ($1));
1944 }
e04a16fb
AG
1945| something_dot_new identifier OP_TK CP_TK class_body
1946| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1947 {
1948 tree ctor = build_new_invocation ($2, $4);
1949 $$ = make_qualified_primary ($1, ctor,
1950 EXPR_WFL_LINECOL ($1));
1951 }
e04a16fb
AG
1952| something_dot_new identifier OP_TK argument_list CP_TK class_body
1953| NEW_TK error SC_TK
1954 {yyerror ("'(' expected"); DRECOVER(new_1);}
1955| NEW_TK class_type error
1956 {yyerror ("'(' expected"); RECOVER;}
1957| NEW_TK class_type OP_TK error
1958 {yyerror ("')' or term expected"); RECOVER;}
1959| NEW_TK class_type OP_TK argument_list error
1960 {yyerror ("')' expected"); RECOVER;}
1961| something_dot_new error
1962 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1963| something_dot_new identifier error
1964 {yyerror ("'(' expected"); RECOVER;}
1965;
1966
c2952b01
APB
1967/* Created after JDK1.1 rules originally added to
1968 class_instance_creation_expression, but modified to use
1969 'class_type' instead of 'TypeName' (type_name) which is mentionned
1970 in the documentation but doesn't exist. */
1971
1972anonymous_class_creation:
1973 NEW_TK class_type OP_TK argument_list CP_TK
1974 { create_anonymous_class ($1.location, $2); }
1975 class_body
1976 {
1977 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1978 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1979
1980 end_class_declaration (1);
1981
1982 /* Now we can craft the new expression */
1983 $$ = build_new_invocation (id, $4);
1984
1985 /* Note that we can't possibly be here if
1986 `class_type' is an interface (in which case the
1987 anonymous class extends Object and implements
1988 `class_type', hence its constructor can't have
1989 arguments.) */
1990
1991 /* Otherwise, the innerclass must feature a
1992 constructor matching `argument_list'. Anonymous
1993 classes are a bit special: it's impossible to
1994 define constructor for them, hence constructors
1995 must be generated following the hints provided by
1996 the `new' expression. Whether a super constructor
1997 of that nature exists or not is to be verified
1998 later on in verify_constructor_super.
1999
2000 It's during the expansion of a `new' statement
2001 refering to an anonymous class that a ctor will
2002 be generated for the anonymous class, with the
2003 right arguments. */
2004
2005 }
2006| NEW_TK class_type OP_TK CP_TK
2007 { create_anonymous_class ($1.location, $2); }
2008 class_body
2009 {
2010 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2011 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2012
2013 end_class_declaration (1);
2014
2015 /* Now we can craft the new expression. The
2016 statement doesn't need to be remember so that a
2017 constructor can be generated, since its signature
2018 is already known. */
2019 $$ = build_new_invocation (id, NULL_TREE);
2020 }
2021;
2022
e04a16fb
AG
2023something_dot_new: /* Added, not part of the specs. */
2024 name DOT_TK NEW_TK
c2952b01 2025 { $$ = $1; }
e04a16fb 2026| primary DOT_TK NEW_TK
c2952b01 2027 { $$ = $1; }
e04a16fb
AG
2028;
2029
2030argument_list:
2031 expression
2032 {
2033 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2034 ctxp->formal_parameter_number = 1;
2035 }
2036| argument_list C_TK expression
2037 {
2038 ctxp->formal_parameter_number += 1;
2039 $$ = tree_cons (NULL_TREE, $3, $1);
2040 }
2041| argument_list C_TK error
2042 {yyerror ("Missing term"); RECOVER;}
2043;
2044
2045array_creation_expression:
2046 NEW_TK primitive_type dim_exprs
2047 { $$ = build_newarray_node ($2, $3, 0); }
2048| NEW_TK class_or_interface_type dim_exprs
2049 { $$ = build_newarray_node ($2, $3, 0); }
2050| NEW_TK primitive_type dim_exprs dims
ba179f9f 2051 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2052| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2053 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2054 /* Added, JDK1.1 anonymous array. Initial documentation rule
2055 modified */
2056| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2057 {
2058 char *sig;
2059 while (CURRENT_OSB (ctxp)--)
2060 obstack_1grow (&temporary_obstack, '[');
2061 sig = obstack_finish (&temporary_obstack);
2062 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2063 $2, get_identifier (sig), $4);
2064 }
e04a16fb 2065| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2066 {
2067 tree type = $2;
2068 while (CURRENT_OSB (ctxp)--)
2069 type = build_java_array_type (type, -1);
2070 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2071 build_pointer_type (type), NULL_TREE, $4);
2072 }
e04a16fb
AG
2073| NEW_TK error CSB_TK
2074 {yyerror ("'[' expected"); DRECOVER ("]");}
2075| NEW_TK error OSB_TK
2076 {yyerror ("']' expected"); RECOVER;}
2077;
2078
2079dim_exprs:
2080 dim_expr
2081 { $$ = build_tree_list (NULL_TREE, $1); }
2082| dim_exprs dim_expr
2083 { $$ = tree_cons (NULL_TREE, $2, $$); }
2084;
2085
2086dim_expr:
2087 OSB_TK expression CSB_TK
2088 {
9a7ab4b3
APB
2089 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2090 {
2091 $2 = build_wfl_node ($2);
2092 TREE_TYPE ($2) = NULL_TREE;
2093 }
e04a16fb
AG
2094 EXPR_WFL_LINECOL ($2) = $1.location;
2095 $$ = $2;
2096 }
2097| OSB_TK expression error
2098 {yyerror ("']' expected"); RECOVER;}
2099| OSB_TK error
2100 {
2101 yyerror ("Missing term");
2102 yyerror ("']' expected");
2103 RECOVER;
2104 }
2105;
2106
2107dims:
2108 OSB_TK CSB_TK
ba179f9f
APB
2109 {
2110 int allocate = 0;
2111 /* If not initialized, allocate memory for the osb
2112 numbers stack */
2113 if (!ctxp->osb_limit)
2114 {
2115 allocate = ctxp->osb_limit = 32;
2116 ctxp->osb_depth = -1;
2117 }
c2952b01 2118 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2119 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2120 allocate = ctxp->osb_limit << 1;
2121
2122 if (allocate)
2123 {
2124 allocate *= sizeof (int);
2125 if (ctxp->osb_number)
2126 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2127 allocate);
2128 else
2129 ctxp->osb_number = (int *)xmalloc (allocate);
2130 }
2131 ctxp->osb_depth++;
2132 CURRENT_OSB (ctxp) = 1;
2133 }
e04a16fb 2134| dims OSB_TK CSB_TK
ba179f9f 2135 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2136| dims OSB_TK error
2137 { yyerror ("']' expected"); RECOVER;}
2138;
2139
2140field_access:
2141 primary DOT_TK identifier
2142 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2143 /* FIXME - REWRITE TO:
2144 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2145| SUPER_TK DOT_TK identifier
2146 {
2147 tree super_wfl =
9ee9b555 2148 build_wfl_node (super_identifier_node);
e04a16fb
AG
2149 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2150 $$ = make_qualified_name (super_wfl, $3, $2.location);
2151 }
2152| SUPER_TK error
2153 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2154;
2155
2156method_invocation:
2157 name OP_TK CP_TK
2158 { $$ = build_method_invocation ($1, NULL_TREE); }
2159| name OP_TK argument_list CP_TK
2160 { $$ = build_method_invocation ($1, $3); }
2161| primary DOT_TK identifier OP_TK CP_TK
2162 {
22eed1e6
APB
2163 if (TREE_CODE ($1) == THIS_EXPR)
2164 $$ = build_this_super_qualified_invocation
2165 (1, $3, NULL_TREE, 0, $2.location);
2166 else
2167 {
2168 tree invok = build_method_invocation ($3, NULL_TREE);
2169 $$ = make_qualified_primary ($1, invok, $2.location);
2170 }
e04a16fb
AG
2171 }
2172| primary DOT_TK identifier OP_TK argument_list CP_TK
2173 {
22eed1e6
APB
2174 if (TREE_CODE ($1) == THIS_EXPR)
2175 $$ = build_this_super_qualified_invocation
2176 (1, $3, $5, 0, $2.location);
2177 else
2178 {
2179 tree invok = build_method_invocation ($3, $5);
2180 $$ = make_qualified_primary ($1, invok, $2.location);
2181 }
e04a16fb
AG
2182 }
2183| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2184 {
2185 $$ = build_this_super_qualified_invocation
2186 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2187 }
2188| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2189 {
22eed1e6
APB
2190 $$ = build_this_super_qualified_invocation
2191 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2192 }
2193 /* Screws up thing. I let it here until I'm convinced it can
2194 be removed. FIXME
2195| primary DOT_TK error
2196 {yyerror ("'(' expected"); DRECOVER(bad);} */
2197| SUPER_TK DOT_TK error CP_TK
2198 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2199| SUPER_TK DOT_TK error DOT_TK
2200 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2201;
2202
2203array_access:
2204 name OSB_TK expression CSB_TK
2205 { $$ = build_array_ref ($2.location, $1, $3); }
2206| primary_no_new_array OSB_TK expression CSB_TK
2207 { $$ = build_array_ref ($2.location, $1, $3); }
2208| name OSB_TK error
2209 {
2210 yyerror ("Missing term and ']' expected");
2211 DRECOVER(array_access);
2212 }
2213| name OSB_TK expression error
2214 {
2215 yyerror ("']' expected");
2216 DRECOVER(array_access);
2217 }
2218| primary_no_new_array OSB_TK error
2219 {
2220 yyerror ("Missing term and ']' expected");
2221 DRECOVER(array_access);
2222 }
2223| primary_no_new_array OSB_TK expression error
2224 {
2225 yyerror ("']' expected");
2226 DRECOVER(array_access);
2227 }
2228;
2229
2230postfix_expression:
2231 primary
2232| name
2233| post_increment_expression
2234| post_decrement_expression
2235;
2236
2237post_increment_expression:
2238 postfix_expression INCR_TK
2239 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2240;
2241
2242post_decrement_expression:
2243 postfix_expression DECR_TK
2244 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2245;
2246
2247unary_expression:
2248 pre_increment_expression
2249| pre_decrement_expression
2250| PLUS_TK unary_expression
2251 {$$ = build_unaryop ($1.token, $1.location, $2); }
2252| MINUS_TK unary_expression
2253 {$$ = build_unaryop ($1.token, $1.location, $2); }
2254| unary_expression_not_plus_minus
2255| PLUS_TK error
2256 {yyerror ("Missing term"); RECOVER}
2257| MINUS_TK error
2258 {yyerror ("Missing term"); RECOVER}
2259;
2260
2261pre_increment_expression:
2262 INCR_TK unary_expression
2263 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2264| INCR_TK error
2265 {yyerror ("Missing term"); RECOVER}
2266;
2267
2268pre_decrement_expression:
2269 DECR_TK unary_expression
2270 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2271| DECR_TK error
2272 {yyerror ("Missing term"); RECOVER}
2273;
2274
2275unary_expression_not_plus_minus:
2276 postfix_expression
2277| NOT_TK unary_expression
2278 {$$ = build_unaryop ($1.token, $1.location, $2); }
2279| NEG_TK unary_expression
2280 {$$ = build_unaryop ($1.token, $1.location, $2); }
2281| cast_expression
2282| NOT_TK error
2283 {yyerror ("Missing term"); RECOVER}
2284| NEG_TK error
2285 {yyerror ("Missing term"); RECOVER}
2286;
2287
2288cast_expression: /* Error handling here is potentially weak */
2289 OP_TK primitive_type dims CP_TK unary_expression
2290 {
2291 tree type = $2;
ba179f9f 2292 while (CURRENT_OSB (ctxp)--)
e04a16fb 2293 type = build_java_array_type (type, -1);
ba179f9f 2294 ctxp->osb_depth--;
e04a16fb
AG
2295 $$ = build_cast ($1.location, type, $5);
2296 }
2297| OP_TK primitive_type CP_TK unary_expression
2298 { $$ = build_cast ($1.location, $2, $4); }
2299| OP_TK expression CP_TK unary_expression_not_plus_minus
2300 { $$ = build_cast ($1.location, $2, $4); }
2301| OP_TK name dims CP_TK unary_expression_not_plus_minus
2302 {
49f48c71 2303 const char *ptr;
ba179f9f 2304 while (CURRENT_OSB (ctxp)--)
e04a16fb 2305 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2306 ctxp->osb_depth--;
e04a16fb
AG
2307 obstack_grow0 (&temporary_obstack,
2308 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2309 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2310 ptr = obstack_finish (&temporary_obstack);
2311 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2312 $$ = build_cast ($1.location, $2, $5);
2313 }
2314| OP_TK primitive_type OSB_TK error
2315 {yyerror ("']' expected, invalid type expression");}
2316| OP_TK error
2317 {
2318 if (ctxp->prevent_ese != lineno)
2319 yyerror ("Invalid type expression"); RECOVER;
2320 RECOVER;
2321 }
2322| OP_TK primitive_type dims CP_TK error
2323 {yyerror ("Missing term"); RECOVER;}
2324| OP_TK primitive_type CP_TK error
2325 {yyerror ("Missing term"); RECOVER;}
2326| OP_TK name dims CP_TK error
2327 {yyerror ("Missing term"); RECOVER;}
2328;
2329
2330multiplicative_expression:
2331 unary_expression
2332| multiplicative_expression MULT_TK unary_expression
2333 {
2334 $$ = build_binop (BINOP_LOOKUP ($2.token),
2335 $2.location, $1, $3);
2336 }
2337| multiplicative_expression DIV_TK unary_expression
2338 {
2339 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2340 $1, $3);
2341 }
2342| multiplicative_expression REM_TK unary_expression
2343 {
2344 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2345 $1, $3);
2346 }
2347| multiplicative_expression MULT_TK error
2348 {yyerror ("Missing term"); RECOVER;}
2349| multiplicative_expression DIV_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351| multiplicative_expression REM_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353;
2354
2355additive_expression:
2356 multiplicative_expression
2357| additive_expression PLUS_TK multiplicative_expression
2358 {
2359 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2360 $1, $3);
2361 }
2362| additive_expression MINUS_TK multiplicative_expression
2363 {
2364 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2365 $1, $3);
2366 }
2367| additive_expression PLUS_TK error
2368 {yyerror ("Missing term"); RECOVER;}
2369| additive_expression MINUS_TK error
2370 {yyerror ("Missing term"); RECOVER;}
2371;
2372
2373shift_expression:
2374 additive_expression
2375| shift_expression LS_TK additive_expression
2376 {
2377 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2378 $1, $3);
2379 }
2380| shift_expression SRS_TK additive_expression
2381 {
2382 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2383 $1, $3);
2384 }
2385| shift_expression ZRS_TK additive_expression
2386 {
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2388 $1, $3);
2389 }
2390| shift_expression LS_TK error
2391 {yyerror ("Missing term"); RECOVER;}
2392| shift_expression SRS_TK error
2393 {yyerror ("Missing term"); RECOVER;}
2394| shift_expression ZRS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2396;
2397
2398relational_expression:
2399 shift_expression
2400| relational_expression LT_TK shift_expression
2401 {
2402 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2403 $1, $3);
2404 }
2405| relational_expression GT_TK shift_expression
2406 {
2407 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2408 $1, $3);
2409 }
2410| relational_expression LTE_TK shift_expression
2411 {
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413 $1, $3);
2414 }
2415| relational_expression GTE_TK shift_expression
2416 {
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418 $1, $3);
2419 }
2420| relational_expression INSTANCEOF_TK reference_type
5e942c50 2421 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2422| relational_expression LT_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424| relational_expression GT_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426| relational_expression LTE_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2428| relational_expression GTE_TK error
2429 {yyerror ("Missing term"); RECOVER;}
2430| relational_expression INSTANCEOF_TK error
2431 {yyerror ("Invalid reference type"); RECOVER;}
2432;
2433
2434equality_expression:
2435 relational_expression
2436| equality_expression EQ_TK relational_expression
2437 {
2438 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439 $1, $3);
2440 }
2441| equality_expression NEQ_TK relational_expression
2442 {
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2445 }
2446| equality_expression EQ_TK error
2447 {yyerror ("Missing term"); RECOVER;}
2448| equality_expression NEQ_TK error
2449 {yyerror ("Missing term"); RECOVER;}
2450;
2451
2452and_expression:
2453 equality_expression
2454| and_expression AND_TK equality_expression
2455 {
2456 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2457 $1, $3);
2458 }
2459| and_expression AND_TK error
2460 {yyerror ("Missing term"); RECOVER;}
2461;
2462
2463exclusive_or_expression:
2464 and_expression
2465| exclusive_or_expression XOR_TK and_expression
2466 {
2467 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468 $1, $3);
2469 }
2470| exclusive_or_expression XOR_TK error
2471 {yyerror ("Missing term"); RECOVER;}
2472;
2473
2474inclusive_or_expression:
2475 exclusive_or_expression
2476| inclusive_or_expression OR_TK exclusive_or_expression
2477 {
2478 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2479 $1, $3);
2480 }
2481| inclusive_or_expression OR_TK error
2482 {yyerror ("Missing term"); RECOVER;}
2483;
2484
2485conditional_and_expression:
2486 inclusive_or_expression
2487| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2488 {
2489 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2490 $1, $3);
2491 }
2492| conditional_and_expression BOOL_AND_TK error
2493 {yyerror ("Missing term"); RECOVER;}
2494;
2495
2496conditional_or_expression:
2497 conditional_and_expression
2498| conditional_or_expression BOOL_OR_TK conditional_and_expression
2499 {
2500 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2501 $1, $3);
2502 }
2503| conditional_or_expression BOOL_OR_TK error
2504 {yyerror ("Missing term"); RECOVER;}
2505;
2506
2507conditional_expression: /* Error handling here is weak */
2508 conditional_or_expression
2509| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2510 {
2511 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2512 EXPR_WFL_LINECOL ($$) = $2.location;
2513 }
e04a16fb
AG
2514| conditional_or_expression REL_QM_TK REL_CL_TK error
2515 {
2516 YYERROR_NOW;
2517 yyerror ("Missing term");
2518 DRECOVER (1);
2519 }
2520| conditional_or_expression REL_QM_TK error
2521 {yyerror ("Missing term"); DRECOVER (2);}
2522| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2523 {yyerror ("Missing term"); DRECOVER (3);}
2524;
2525
2526assignment_expression:
2527 conditional_expression
2528| assignment
2529;
2530
2531assignment:
2532 left_hand_side assignment_operator assignment_expression
2533 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2534| left_hand_side assignment_operator error
2535 {
2536 if (ctxp->prevent_ese != lineno)
2537 yyerror ("Missing term");
2538 DRECOVER (assign);
2539 }
2540;
2541
2542left_hand_side:
2543 name
2544| field_access
2545| array_access
2546;
2547
2548assignment_operator:
2549 ASSIGN_ANY_TK
2550| ASSIGN_TK
2551;
2552
2553expression:
2554 assignment_expression
2555;
2556
2557constant_expression:
2558 expression
2559;
2560
2561%%
2562\f
2563
c2952b01
APB
2564/* This section of the code deal with save/restoring parser contexts.
2565 Add mode documentation here. FIXME */
e04a16fb 2566
c2952b01
APB
2567/* Helper function. Create a new parser context. With
2568 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2569 context is copied, otherwise, the new context is zeroed. The newly
2570 created context becomes the current one. */
e04a16fb 2571
c2952b01
APB
2572static void
2573create_new_parser_context (copy_from_previous)
2574 int copy_from_previous;
e04a16fb 2575{
c2952b01 2576 struct parser_ctxt *new;
e04a16fb 2577
c2952b01
APB
2578 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2579 if (copy_from_previous)
2580 {
2581 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2582 new->saved_data_ctx = 1;
2583 }
2584 else
2585 bzero ((PTR) new, sizeof (struct parser_ctxt));
2586
e04a16fb
AG
2587 new->next = ctxp;
2588 ctxp = new;
c2952b01
APB
2589}
2590
2591/* Create a new parser context and make it the current one. */
2592
2593void
2594java_push_parser_context ()
2595{
2596 create_new_parser_context (0);
e04a16fb 2597 if (ctxp->next)
5e942c50
APB
2598 {
2599 ctxp->incomplete_class = ctxp->next->incomplete_class;
2600 ctxp->gclass_list = ctxp->next->gclass_list;
2601 }
e04a16fb
AG
2602}
2603
c2952b01
APB
2604void
2605java_pop_parser_context (generate)
2606 int generate;
2607{
2608 tree current;
2609 struct parser_ctxt *toFree, *next;
2610
2611 if (!ctxp)
2612 return;
2613
2614 toFree = ctxp;
2615 next = ctxp->next;
2616 if (next)
2617 {
2618 next->incomplete_class = ctxp->incomplete_class;
2619 next->gclass_list = ctxp->gclass_list;
2620 lineno = ctxp->lineno;
2621 finput = ctxp->finput;
2622 current_class = ctxp->current_class;
2623 }
2624
2625 /* Set the single import class file flag to 0 for the current list
2626 of imported things */
2627 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2628 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2629
2630 /* And restore those of the previous context */
2631 if ((ctxp = next)) /* Assignment is really meant here */
2632 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2633 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2634
2635 /* If we pushed a context to parse a class intended to be generated,
2636 we keep it so we can remember the class. What we could actually
2637 do is to just update a list of class names. */
2638 if (generate)
2639 {
2640 toFree->next = ctxp_for_generation;
2641 ctxp_for_generation = toFree;
2642 }
2643 else
2644 free (toFree);
2645}
2646
2647/* Create a parser context for the use of saving some global
2648 variables. */
2649
e04a16fb
AG
2650void
2651java_parser_context_save_global ()
2652{
22eed1e6
APB
2653 if (!ctxp)
2654 {
2655 java_push_parser_context ();
ee07f4f4
APB
2656 ctxp->saved_data_ctx = 1;
2657 }
c2952b01
APB
2658
2659 /* If this context already stores data, create a new one suitable
2660 for data storage. */
ee07f4f4 2661 else if (ctxp->saved_data)
c2952b01
APB
2662 create_new_parser_context (1);
2663
e04a16fb
AG
2664 ctxp->finput = finput;
2665 ctxp->lineno = lineno;
2666 ctxp->current_class = current_class;
2667 ctxp->filename = input_filename;
2668 ctxp->current_function_decl = current_function_decl;
ee07f4f4 2669 ctxp->saved_data = 1;
e04a16fb
AG
2670}
2671
c2952b01
APB
2672/* Restore some global variables from the previous context. Make the
2673 previous context the current one. */
2674
e04a16fb
AG
2675void
2676java_parser_context_restore_global ()
2677{
2678 finput = ctxp->finput;
2679 lineno = ctxp->lineno;
2680 current_class = ctxp->current_class;
2681 input_filename = ctxp->filename;
2682 current_function_decl = ctxp->current_function_decl;
c2952b01 2683 ctxp->saved_data = 0;
ee07f4f4
APB
2684 if (ctxp->saved_data_ctx)
2685 java_pop_parser_context (0);
e04a16fb
AG
2686}
2687
c2952b01
APB
2688/* Suspend vital data for the current class/function being parsed so
2689 that an other class can be parsed. Used to let local/anonymous
2690 classes be parsed. */
2691
2692static void
2693java_parser_context_suspend ()
e04a16fb 2694{
c2952b01 2695 /* This makes debugging through java_debug_context easier */
3b304f5b 2696 static const char *name = "<inner buffer context>";
e04a16fb 2697
c2952b01
APB
2698 /* Duplicate the previous context, use it to save the globals we're
2699 interested in */
2700 create_new_parser_context (1);
2701 ctxp->current_function_decl = current_function_decl;
2702 ctxp->current_class = current_class;
5e942c50 2703
c2952b01
APB
2704 /* Then create a new context which inherits all data from the
2705 previous one. This will be the new current context */
2706 create_new_parser_context (1);
2707
2708 /* Help debugging */
2709 ctxp->next->filename = name;
2710}
2711
2712/* Resume vital data for the current class/function being parsed so
2713 that an other class can be parsed. Used to let local/anonymous
2714 classes be parsed. The trick is the data storing file position
2715 informations must be restored to their current value, so parsing
2716 can resume as if no context was ever saved. */
2717
2718static void
2719java_parser_context_resume ()
2720{
2721 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2722 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2723 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2724
2725 /* We need to inherit the list of classes to complete/generate */
2726 restored->incomplete_class = old->incomplete_class;
2727 restored->gclass_list = old->gclass_list;
2728 restored->classd_list = old->classd_list;
2729 restored->class_list = old->class_list;
2730
2731 /* Restore the current class and function from the saver */
2732 current_class = saver->current_class;
2733 current_function_decl = saver->current_function_decl;
2734
2735 /* Retrive the restored context */
2736 ctxp = restored;
2737
2738 /* Re-installed the data for the parsing to carry on */
2739 bcopy (&old->marker_begining, &ctxp->marker_begining,
2740 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2741
2742 /* Buffer context can now be discarded */
2743 free (saver);
2744 free (old);
2745}
2746
2747/* Add a new anchor node to which all statement(s) initializing static
2748 and non static initialized upon declaration field(s) will be
2749 linked. */
2750
2751static void
2752java_parser_context_push_initialized_field ()
2753{
2754 tree node;
2755
2756 node = build_tree_list (NULL_TREE, NULL_TREE);
2757 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2758 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2759
2760 node = build_tree_list (NULL_TREE, NULL_TREE);
2761 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2762 CPC_INITIALIZER_LIST (ctxp) = node;
2763
2764 node = build_tree_list (NULL_TREE, NULL_TREE);
2765 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2766 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2767}
2768
2769/* Pop the lists of initialized field. If this lists aren't empty,
2770 remember them so we can use it to create and populate the $finit$
2771 or <clinit> functions. */
2772
2773static void
2774java_parser_context_pop_initialized_field ()
2775{
2776 tree stmts;
2777 tree class_type = TREE_TYPE (GET_CPC ());
2778
2779 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2780 {
c2952b01
APB
2781 stmts = CPC_INITIALIZER_STMT (ctxp);
2782 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2783 if (stmts && !java_error_count)
2784 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2785 }
2786
c2952b01
APB
2787 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2788 {
2789 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2790 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2791 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2792 /* Keep initialization in order to enforce 8.5 */
2793 if (stmts && !java_error_count)
2794 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2795 }
e04a16fb 2796
c2952b01
APB
2797 /* JDK 1.1 instance initializers */
2798 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2799 {
c2952b01
APB
2800 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2801 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2802 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2803 if (stmts && !java_error_count)
2804 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2805 }
c2952b01
APB
2806}
2807
2808static tree
2809reorder_static_initialized (list)
2810 tree list;
2811{
2812 /* We have to keep things in order. The alias initializer have to
2813 come first, then the initialized regular field, in reverse to
2814 keep them in lexical order. */
2815 tree marker, previous = NULL_TREE;
2816 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2817 if (TREE_CODE (marker) == TREE_LIST
2818 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2819 break;
2820
2821 /* No static initialized, the list is fine as is */
2822 if (!previous)
2823 list = TREE_CHAIN (marker);
2824
2825 /* No marker? reverse the whole list */
2826 else if (!marker)
2827 list = nreverse (list);
2828
2829 /* Otherwise, reverse what's after the marker and the new reordered
2830 sublist will replace the marker. */
b351b287 2831 else
c2952b01
APB
2832 {
2833 TREE_CHAIN (previous) = NULL_TREE;
2834 list = nreverse (list);
2835 list = chainon (TREE_CHAIN (marker), list);
2836 }
2837 return list;
e04a16fb
AG
2838}
2839
c2952b01
APB
2840/* Helper functions to dump the parser context stack. */
2841
2842#define TAB_CONTEXT(C) \
2843 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2844
2845static void
2846java_debug_context_do (tab)
2847 int tab;
2848{
ee07f4f4
APB
2849 struct parser_ctxt *copy = ctxp;
2850 while (copy)
2851 {
c2952b01 2852 TAB_CONTEXT (tab);
ee07f4f4 2853 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2854 TAB_CONTEXT (tab);
ee07f4f4 2855 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2856 TAB_CONTEXT (tab);
2857 fprintf (stderr, "lineno: %d\n", copy->lineno);
2858 TAB_CONTEXT (tab);
ee07f4f4
APB
2859 fprintf (stderr, "package: %s\n",
2860 (copy->package ?
2861 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2862 TAB_CONTEXT (tab);
ee07f4f4 2863 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2864 TAB_CONTEXT (tab);
ee07f4f4
APB
2865 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2866 copy = copy->next;
2867 tab += 2;
2868 }
ee07f4f4
APB
2869}
2870
c2952b01
APB
2871/* Dump the stacked up parser contexts. Intended to be called from a
2872 debugger. */
2873
ee07f4f4
APB
2874void
2875java_debug_context ()
2876{
2877 java_debug_context_do (0);
2878}
2879
c2952b01
APB
2880\f
2881
2882/* Flag for the error report routine to issue the error the first time
2883 it's called (overriding the default behavior which is to drop the
2884 first invocation and honor the second one, taking advantage of a
2885 richer context. */
2886static int force_error = 0;
ee07f4f4 2887
8119c720
APB
2888/* Reporting an constructor invocation error. */
2889static void
2890parse_ctor_invocation_error ()
2891{
2892 if (DECL_CONSTRUCTOR_P (current_function_decl))
2893 yyerror ("Constructor invocation must be first thing in a constructor");
2894 else
2895 yyerror ("Only constructors can invoke constructors");
2896}
2897
2898/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2899
2900static tree
2901parse_jdk1_1_error (msg)
49f48c71 2902 const char *msg;
b67d701b
PB
2903{
2904 sorry (": `%s' JDK1.1(TM) feature", msg);
2905 java_error_count++;
9bbc7d9f 2906 return empty_stmt_node;
b67d701b
PB
2907}
2908
e04a16fb
AG
2909static int do_warning = 0;
2910
2911void
2912yyerror (msg)
49f48c71 2913 const char *msg;
e04a16fb
AG
2914{
2915 static java_lc elc;
2916 static int prev_lineno;
49f48c71 2917 static const char *prev_msg;
e04a16fb 2918
0a2138e2 2919 int save_lineno;
e04a16fb
AG
2920 char *remainder, *code_from_source;
2921 extern struct obstack temporary_obstack;
2922
2923 if (!force_error && prev_lineno == lineno)
2924 return;
2925
2926 /* Save current error location but report latter, when the context is
2927 richer. */
2928 if (ctxp->java_error_flag == 0)
2929 {
2930 ctxp->java_error_flag = 1;
2931 elc = ctxp->elc;
2932 /* Do something to use the previous line if we're reaching the
2933 end of the file... */
2934#ifdef VERBOSE_SKELETON
2935 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2936#endif
2937 return;
2938 }
2939
2940 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2941 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2942 return;
2943
2944 ctxp->java_error_flag = 0;
2945 if (do_warning)
2946 java_warning_count++;
2947 else
2948 java_error_count++;
2949
807bc1db 2950 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2951 {
2952 elc.col = ctxp->p_line->char_col-1;
2953 elc.line = ctxp->p_line->lineno;
2954 }
2955
2956 save_lineno = lineno;
2957 prev_lineno = lineno = elc.line;
2958 prev_msg = msg;
2959
2960 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2961 obstack_grow0 (&temporary_obstack,
2962 code_from_source, strlen (code_from_source));
2963 remainder = obstack_finish (&temporary_obstack);
2964 if (do_warning)
2965 warning ("%s.\n%s", msg, remainder);
2966 else
2967 error ("%s.\n%s", msg, remainder);
2968
2969 /* This allow us to cheaply avoid an extra 'Invalid expression
2970 statement' error report when errors have been already reported on
2971 the same line. This occurs when we report an error but don't have
2972 a synchronization point other than ';', which
2973 expression_statement is the only one to take care of. */
2974 ctxp->prevent_ese = lineno = save_lineno;
2975}
2976
2977static void
15fdcfe9 2978issue_warning_error_from_context (cl, msg, ap)
5e942c50 2979 tree cl;
d4476be2 2980 const char *msg;
15fdcfe9 2981 va_list ap;
5e942c50 2982{
3b304f5b 2983 const char *saved, *saved_input_filename;
15fdcfe9
PB
2984 char buffer [4096];
2985 vsprintf (buffer, msg, ap);
2986 force_error = 1;
5e942c50
APB
2987
2988 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2989 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2990 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2991
2992 /* We have a CL, that's a good reason for using it if it contains data */
2993 saved = ctxp->filename;
2994 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2995 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2996 saved_input_filename = input_filename;
2997 input_filename = ctxp->filename;
15fdcfe9
PB
2998 java_error (NULL);
2999 java_error (buffer);
5e942c50 3000 ctxp->filename = saved;
1886c9d8 3001 input_filename = saved_input_filename;
15fdcfe9 3002 force_error = 0;
5e942c50
APB
3003}
3004
e04a16fb
AG
3005/* Issue an error message at a current source line CL */
3006
15fdcfe9 3007void
df32d2ce 3008parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3009{
d4476be2 3010#ifndef ANSI_PROTOTYPES
e04a16fb 3011 tree cl;
d4476be2 3012 const char *msg;
e04a16fb 3013#endif
e04a16fb
AG
3014 va_list ap;
3015
3016 VA_START (ap, msg);
d4476be2 3017#ifndef ANSI_PROTOTYPES
e04a16fb 3018 cl = va_arg (ap, tree);
d4476be2 3019 msg = va_arg (ap, const char *);
e04a16fb 3020#endif
15fdcfe9
PB
3021 issue_warning_error_from_context (cl, msg, ap);
3022 va_end (ap);
e04a16fb
AG
3023}
3024
3025/* Issue a warning at a current source line CL */
3026
3027static void
df32d2ce 3028parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3029{
d4476be2 3030#ifndef ANSI_PROTOTYPES
e04a16fb 3031 tree cl;
d4476be2 3032 const char *msg;
e04a16fb 3033#endif
e04a16fb
AG
3034 va_list ap;
3035
3036 VA_START (ap, msg);
d4476be2 3037#ifndef ANSI_PROTOTYPES
e04a16fb 3038 cl = va_arg (ap, tree);
d4476be2 3039 msg = va_arg (ap, const char *);
e04a16fb 3040#endif
e04a16fb 3041
c877974e 3042 force_error = do_warning = 1;
15fdcfe9 3043 issue_warning_error_from_context (cl, msg, ap);
c877974e 3044 do_warning = force_error = 0;
15fdcfe9 3045 va_end (ap);
e04a16fb
AG
3046}
3047
82371d41
APB
3048static tree
3049find_expr_with_wfl (node)
3050 tree node;
3051{
3052 while (node)
3053 {
3054 char code;
3055 tree to_return;
3056
3057 switch (TREE_CODE (node))
3058 {
3059 case BLOCK:
c0d87ff6
PB
3060 node = BLOCK_EXPR_BODY (node);
3061 continue;
82371d41
APB
3062
3063 case COMPOUND_EXPR:
3064 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3065 if (to_return)
3066 return to_return;
c0d87ff6
PB
3067 node = TREE_OPERAND (node, 1);
3068 continue;
82371d41
APB
3069
3070 case LOOP_EXPR:
c0d87ff6
PB
3071 node = TREE_OPERAND (node, 0);
3072 continue;
82371d41
APB
3073
3074 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3075 node = TREE_OPERAND (node, 1);
3076 continue;
3077
82371d41
APB
3078 default:
3079 code = TREE_CODE_CLASS (TREE_CODE (node));
3080 if (((code == '1') || (code == '2') || (code == 'e'))
3081 && EXPR_WFL_LINECOL (node))
3082 return node;
ba179f9f 3083 return NULL_TREE;
82371d41
APB
3084 }
3085 }
3086 return NULL_TREE;
3087}
3088
3089/* Issue a missing return statement error. Uses METHOD to figure the
3090 last line of the method the error occurs in. */
3091
3092static void
3093missing_return_error (method)
3094 tree method;
3095{
3096 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3097 parse_error_context (wfl_operator, "Missing return statement");
3098}
3099
3100/* Issue an unreachable statement error. From NODE, find the next
3101 statement to report appropriately. */
3102static void
3103unreachable_stmt_error (node)
3104 tree node;
3105{
3106 /* Browse node to find the next expression node that has a WFL. Use
3107 the location to report the error */
3108 if (TREE_CODE (node) == COMPOUND_EXPR)
3109 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3110 else
3111 node = find_expr_with_wfl (node);
3112
3113 if (node)
3114 {
3115 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3116 parse_error_context (wfl_operator, "Unreachable statement");
3117 }
3118 else
3119 fatal ("Can't get valid statement - unreachable_stmt_error");
3120}
3121
c877974e 3122int
e04a16fb
AG
3123java_report_errors ()
3124{
3125 if (java_error_count)
3126 fprintf (stderr, "%d error%s",
3127 java_error_count, (java_error_count == 1 ? "" : "s"));
3128 if (java_warning_count)
3129 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3130 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3131 if (java_error_count || java_warning_count)
3132 putc ('\n', stderr);
c877974e 3133 return java_error_count;
e04a16fb
AG
3134}
3135
3136static char *
3137java_accstring_lookup (flags)
3138 int flags;
3139{
3140 static char buffer [80];
3141#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3142
3143 /* Access modifier looked-up first for easier report on forbidden
3144 access. */
3145 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3146 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3147 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3148 if (flags & ACC_STATIC) COPY_RETURN ("static");
3149 if (flags & ACC_FINAL) COPY_RETURN ("final");
3150 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3151 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3152 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3153 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3154 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3155 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3156
3157 buffer [0] = '\0';
3158 return buffer;
3159#undef COPY_RETURN
3160}
3161
b67d701b
PB
3162/* Issuing error messages upon redefinition of classes, interfaces or
3163 variables. */
3164
e04a16fb 3165static void
b67d701b 3166classitf_redefinition_error (context, id, decl, cl)
49f48c71 3167 const char *context;
e04a16fb
AG
3168 tree id, decl, cl;
3169{
3170 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3171 context, IDENTIFIER_POINTER (id),
3172 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3173 /* Here we should point out where its redefined. It's a unicode. FIXME */
3174}
3175
b67d701b
PB
3176static void
3177variable_redefinition_error (context, name, type, line)
3178 tree context, name, type;
3179 int line;
3180{
49f48c71 3181 const char *type_name;
b67d701b
PB
3182
3183 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3184 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3185 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3186 else
0a2138e2 3187 type_name = lang_printable_name (type, 0);
b67d701b
PB
3188
3189 parse_error_context (context,
781b0558 3190 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3191 IDENTIFIER_POINTER (name),
3192 type_name, IDENTIFIER_POINTER (name), line);
3193}
3194
c583dd46
APB
3195static tree
3196build_array_from_name (type, type_wfl, name, ret_name)
3197 tree type, type_wfl, name, *ret_name;
3198{
3199 int more_dims = 0;
49f48c71 3200 const char *string;
c583dd46
APB
3201
3202 /* Eventually get more dims */
3203 string = IDENTIFIER_POINTER (name);
3204 while (string [more_dims] == '[')
3205 more_dims++;
3206
3207 /* If we have, then craft a new type for this variable */
3208 if (more_dims)
3209 {
c0d87ff6 3210 name = get_identifier (&string [more_dims]);
c583dd46 3211
34f4db93
APB
3212 /* If we have a pointer, use its type */
3213 if (TREE_CODE (type) == POINTER_TYPE)
3214 type = TREE_TYPE (type);
c583dd46
APB
3215
3216 /* Building the first dimension of a primitive type uses this
3217 function */
3218 if (JPRIMITIVE_TYPE_P (type))
3219 {
3220 type = build_java_array_type (type, -1);
22eed1e6 3221 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3222 more_dims--;
3223 }
3224 /* Otherwise, if we have a WFL for this type, use it (the type
3225 is already an array on an unresolved type, and we just keep
3226 on adding dimensions) */
3227 else if (type_wfl)
3228 type = type_wfl;
3229
3230 /* Add all the dimensions */
3231 while (more_dims--)
3232 type = build_unresolved_array_type (type);
3233
3234 /* The type may have been incomplete in the first place */
3235 if (type_wfl)
3236 type = obtain_incomplete_type (type);
3237 }
3238
c2952b01
APB
3239 if (ret_name)
3240 *ret_name = name;
c583dd46
APB
3241 return type;
3242}
3243
e04a16fb
AG
3244/* Build something that the type identifier resolver will identify as
3245 being an array to an unresolved type. TYPE_WFL is a WFL on a
3246 identifier. */
3247
3248static tree
3249build_unresolved_array_type (type_or_wfl)
3250 tree type_or_wfl;
3251{
49f48c71 3252 const char *ptr;
e04a16fb 3253
1886c9d8 3254 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3255 just create a array type */
3256 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3257 {
3258 tree type = build_java_array_type (type_or_wfl, -1);
3259 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3260 return type;
3261 }
3262
3263 obstack_1grow (&temporary_obstack, '[');
3264 obstack_grow0 (&temporary_obstack,
3265 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3266 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3267 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3268 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3269 return type_or_wfl;
e04a16fb
AG
3270}
3271
e04a16fb
AG
3272static void
3273parser_add_interface (class_decl, interface_decl, wfl)
3274 tree class_decl, interface_decl, wfl;
3275{
3276 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3277 parse_error_context (wfl, "Interface `%s' repeated",
3278 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3279}
3280
3281/* Bulk of common class/interface checks. Return 1 if an error was
3282 encountered. TAG is 0 for a class, 1 for an interface. */
3283
3284static int
3285check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3286 int is_interface, flags;
3287 tree raw_name, qualified_name, decl, cl;
3288{
3289 tree node;
c2952b01
APB
3290 int sca = 0; /* Static class allowed */
3291 int icaf = 0; /* Inner class allowed flags */
3292 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3293
3294 if (!quiet_flag)
c2952b01
APB
3295 fprintf (stderr, " %s%s %s",
3296 (CPC_INNER_P () ? "inner" : ""),
3297 (is_interface ? "interface" : "class"),
e04a16fb
AG
3298 IDENTIFIER_POINTER (qualified_name));
3299
3300 /* Scope of an interface/class type name:
3301 - Can't be imported by a single type import
3302 - Can't already exists in the package */
3303 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3304 && (node = find_name_in_single_imports (raw_name))
3305 && !CPC_INNER_P ())
e04a16fb
AG
3306 {
3307 parse_error_context
3308 (cl, "%s name `%s' clashes with imported type `%s'",
3309 (is_interface ? "Interface" : "Class"),
3310 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3311 return 1;
3312 }
3313 if (decl && CLASS_COMPLETE_P (decl))
3314 {
b67d701b
PB
3315 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3316 qualified_name, decl, cl);
e04a16fb
AG
3317 return 1;
3318 }
3319
c2952b01
APB
3320 if (check_inner_class_redefinition (raw_name, cl))
3321 return 1;
3322
3323 /* If public, file name should match class/interface name, except
3324 when dealing with an inner class */
3325 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3326 {
49f48c71 3327 const char *f;
e04a16fb
AG
3328
3329 /* Contains OS dependent assumption on path separator. FIXME */
3330 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3331 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3332 f--)
3333 ;
847fe791 3334 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3335 f++;
3336 if (strncmp (IDENTIFIER_POINTER (raw_name),
3337 f , IDENTIFIER_LENGTH (raw_name)) ||
3338 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3339 parse_error_context
3340 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3341 (is_interface ? "interface" : "class"),
3342 IDENTIFIER_POINTER (qualified_name),
3343 IDENTIFIER_POINTER (raw_name));
3344 }
3345
c2952b01
APB
3346 /* Static classes can be declared only in top level classes. Note:
3347 once static, a inner class is a top level class. */
3348 if (flags & ACC_STATIC)
3349 {
3350 /* Catch the specific error of declaring an class inner class
3351 with no toplevel enclosing class. Prevent check_modifiers from
3352 complaining a second time */
3353 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3354 {
3355 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3356 IDENTIFIER_POINTER (qualified_name));
3357 sca = ACC_STATIC;
3358 }
3359 /* Else, in the context of a top-level class declaration, let
3360 `check_modifiers' do its job, otherwise, give it a go */
3361 else
3362 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3363 }
3364
a40d21da 3365 /* Inner classes can be declared private or protected
c2952b01
APB
3366 within their enclosing classes. */
3367 if (CPC_INNER_P ())
3368 {
3369 /* A class which is local to a block can't be public, private,
3370 protected or static. But it is created final, so allow this
3371 one. */
3372 if (current_function_decl)
3373 icaf = sca = uaaf = ACC_FINAL;
3374 else
3375 {
3376 check_modifiers_consistency (flags);
3377 icaf = ACC_PRIVATE|ACC_PROTECTED;
3378 }
3379 }
3380
a40d21da
APB
3381 if (is_interface)
3382 {
3383 if (CPC_INNER_P ())
3384 uaaf = INTERFACE_INNER_MODIFIERS;
3385 else
3386 uaaf = INTERFACE_MODIFIERS;
3387
3388 check_modifiers ("Illegal modifier `%s' for interface declaration",
3389 flags, uaaf);
3390 }
2884c41e 3391 else
a40d21da
APB
3392 check_modifiers ((current_function_decl ?
3393 "Illegal modifier `%s' for local class declaration" :
3394 "Illegal modifier `%s' for class declaration"),
c2952b01 3395 flags, uaaf|sca|icaf);
e04a16fb
AG
3396 return 0;
3397}
3398
c2952b01
APB
3399static void
3400make_nested_class_name (cpc_list)
3401 tree cpc_list;
3402{
3403 tree name;
3404
3405 if (!cpc_list)
3406 return;
3407 else
3408 make_nested_class_name (TREE_CHAIN (cpc_list));
3409
3410 /* Pick the qualified name when dealing with the first upmost
3411 enclosing class */
3412 name = (TREE_CHAIN (cpc_list) ?
3413 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3414 obstack_grow (&temporary_obstack,
3415 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3416 /* Why is NO_DOLLAR_IN_LABEL defined? */
3417#if 0
3418#ifdef NO_DOLLAR_IN_LABEL
3419 fatal ("make_nested_class_name: Can't use '$' as a separator "
3420 "for inner classes");
3421#endif
3422#endif
3423 obstack_1grow (&temporary_obstack, '$');
3424}
3425
3426/* Can't redefine a class already defined in an earlier scope. */
3427
3428static int
3429check_inner_class_redefinition (raw_name, cl)
3430 tree raw_name, cl;
3431{
3432 tree scope_list;
3433
3434 for (scope_list = GET_CPC_LIST (); scope_list;
3435 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3436 if (raw_name == GET_CPC_UN_NODE (scope_list))
3437 {
3438 parse_error_context
3439 (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",
3440 IDENTIFIER_POINTER (raw_name));
3441 return 1;
3442 }
3443 return 0;
3444}
3445
3446static tree
3447find_as_inner_class (enclosing, name, cl)
3448 tree enclosing, name, cl;
3449{
3450 tree qual, to_return;
3451 if (!enclosing)
3452 return NULL_TREE;
3453
3454 name = TYPE_NAME (name);
3455
3456 /* First search: within the scope of `enclosing', search for name */
3457 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3458 qual = EXPR_WFL_QUALIFICATION (cl);
3459 else if (cl)
3460 qual = build_tree_list (cl, NULL_TREE);
3461 else
3462 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3463
3464 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3465 return to_return;
3466
3467 /* We're dealing with a qualified name. Try to resolve thing until
3468 we get something that is an enclosing class. */
3469 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3470 {
3471 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3472
0c2b8145
APB
3473 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3474 qual = TREE_CHAIN (qual))
c2952b01
APB
3475 {
3476 acc = merge_qualified_name (acc,
3477 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3478 BUILD_PTR_FROM_NAME (ptr, acc);
3479 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3480 }
3481
3482 /* A NULL qual and a decl means that the search ended
3483 successfully?!? We have to do something then. FIXME */
3484
3485 if (decl)
3486 enclosing = decl;
3487 else
3488 qual = EXPR_WFL_QUALIFICATION (cl);
3489 }
3490 /* Otherwise, create a qual for the other part of the resolution. */
3491 else
3492 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3493
1e12ab9b 3494 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3495}
3496
3497/* We go inside the list of sub classes and try to find a way
3498 through. */
3499
3500static tree
3501find_as_inner_class_do (qual, enclosing)
3502 tree qual, enclosing;
3503{
3504 if (!qual)
3505 return NULL_TREE;
3506
3507 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3508 {
3509 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3510 tree next_enclosing = NULL_TREE;
3511 tree inner_list;
3512
3513 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3514 inner_list; inner_list = TREE_CHAIN (inner_list))
3515 {
3516 if (TREE_VALUE (inner_list) == name_to_match)
3517 {
3518 next_enclosing = TREE_PURPOSE (inner_list);
3519 break;
3520 }
3521 }
3522 enclosing = next_enclosing;
3523 }
3524
3525 return (!qual && enclosing ? enclosing : NULL_TREE);
3526}
3527
3528/* Reach all inner classes and tie their unqualified name to a
3529 DECL. */
3530
3531static void
3532set_nested_class_simple_name_value (outer, set)
3533 tree outer;
3534 int set;
3535{
3536 tree l;
3537
3538 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3539 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3540 TREE_PURPOSE (l) : NULL_TREE);
3541}
3542
3543static void
3544link_nested_class_to_enclosing ()
3545{
3546 if (GET_ENCLOSING_CPC ())
3547 {
3548 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3549 DECL_INNER_CLASS_LIST (enclosing) =
3550 tree_cons (GET_CPC (), GET_CPC_UN (),
3551 DECL_INNER_CLASS_LIST (enclosing));
3552 enclosing = enclosing;
3553 }
3554}
3555
3556static tree
3557maybe_make_nested_class_name (name)
3558 tree name;
3559{
3560 tree id = NULL_TREE;
3561
3562 if (CPC_INNER_P ())
3563 {
3564 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3565 obstack_grow0 (&temporary_obstack,
3566 IDENTIFIER_POINTER (name),
3567 IDENTIFIER_LENGTH (name));
c2952b01
APB
3568 id = get_identifier (obstack_finish (&temporary_obstack));
3569 if (ctxp->package)
3570 QUALIFIED_P (id) = 1;
3571 }
3572 return id;
3573}
3574
3575/* If DECL is NULL, create and push a new DECL, record the current
3576 line CL and do other maintenance things. */
3577
e04a16fb 3578static tree
c2952b01
APB
3579maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3580 tree decl, raw_name, qualified_name, cl;
e04a16fb 3581{
5e942c50 3582 if (!decl)
e04a16fb 3583 decl = push_class (make_class (), qualified_name);
c2952b01 3584
e04a16fb
AG
3585 /* Take care of the file and line business */
3586 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3587 /* If we're emiting xrefs, store the line/col number information */
3588 if (flag_emit_xref)
3589 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3590 else
3591 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3592 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3593 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3594 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3595
c2952b01
APB
3596 PUSH_CPC (decl, raw_name);
3597 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3598
e04a16fb
AG
3599 /* Link the declaration to the already seen ones */
3600 TREE_CHAIN (decl) = ctxp->class_list;
3601 ctxp->class_list = decl;
5e942c50 3602
23a79c61 3603 /* Create a new nodes in the global lists */
5e942c50 3604 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3605 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3606
e04a16fb
AG
3607 /* Install a new dependency list element */
3608 create_jdep_list (ctxp);
3609
3610 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3611 IDENTIFIER_POINTER (qualified_name)));
3612 return decl;
3613}
3614
3615static void
3616add_superinterfaces (decl, interface_list)
3617 tree decl, interface_list;
3618{
3619 tree node;
3620 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3621 takes care of ensuring that:
3622 - This is an accessible interface type,
3623 - Circularity detection.
3624 parser_add_interface is then called. If present but not defined,
3625 the check operation is delayed until the super interface gets
3626 defined. */
3627 for (node = interface_list; node; node = TREE_CHAIN (node))
3628 {
15fdcfe9 3629 tree current = TREE_PURPOSE (node);
5e942c50
APB
3630 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3631 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3632 {
5e942c50
APB
3633 if (!parser_check_super_interface (idecl, decl, current))
3634 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3635 }
3636 else
3637 register_incomplete_type (JDEP_INTERFACE,
3638 current, decl, NULL_TREE);
3639 }
3640}
3641
3642/* Create an interface in pass1 and return its decl. Return the
3643 interface's decl in pass 2. */
3644
3645static tree
3646create_interface (flags, id, super)
3647 int flags;
3648 tree id, super;
3649{
e04a16fb 3650 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3651 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3652 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3653
3654 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3655
3656 /* Basic checks: scope, redefinition, modifiers */
3657 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3658 {
3659 PUSH_ERROR ();
3660 return NULL_TREE;
3661 }
3662
3663 /* Suspend the current parsing context if we're parsing an inner
3664 interface */
3665 if (CPC_INNER_P ())
3666 java_parser_context_suspend ();
3667
3668 /* Push a new context for (static) initialized upon declaration fields */
3669 java_parser_context_push_initialized_field ();
e04a16fb
AG
3670
3671 /* Interface modifiers check
3672 - public/abstract allowed (already done at that point)
3673 - abstract is obsolete (comes first, it's a warning, or should be)
3674 - Can't use twice the same (checked in the modifier rule) */
c877974e 3675 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3676 parse_warning_context
3677 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3678 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3679
3680 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3681 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3682
3683 /* Set super info and mark the class a complete */
2aa11e97 3684 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3685 object_type_node, ctxp->interface_number);
3686 ctxp->interface_number = 0;
3687 CLASS_COMPLETE_P (decl) = 1;
3688 add_superinterfaces (decl, super);
3689
3690 return decl;
3691}
3692
c2952b01
APB
3693/* Anonymous class counter. Will be reset to 1 every time a non
3694 anonymous class gets created. */
3695static int anonymous_class_counter = 1;
3696
3697/* Patch anonymous class CLASS, by either extending or implementing
3698 DEP. */
3699
3700static void
3701patch_anonymous_class (type_decl, class_decl, wfl)
3702 tree type_decl, class_decl, wfl;
3703{
3704 tree class = TREE_TYPE (class_decl);
3705 tree type = TREE_TYPE (type_decl);
3706 tree binfo = TYPE_BINFO (class);
3707
3708 /* If it's an interface, implement it */
3709 if (CLASS_INTERFACE (type_decl))
3710 {
3711 tree s_binfo;
3712 int length;
3713
3714 if (parser_check_super_interface (type_decl, class_decl, wfl))
3715 return;
3716
3717 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3718 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3719 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3720 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3721 /* And add the interface */
3722 parser_add_interface (class_decl, type_decl, wfl);
3723 }
3724 /* Otherwise, it's a type we want to extend */
3725 else
3726 {
3727 if (parser_check_super (type_decl, class_decl, wfl))
3728 return;
3729 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3730 }
3731}
3732
3733static tree
3734create_anonymous_class (location, type_name)
3735 int location;
3736 tree type_name;
3737{
3738 char buffer [80];
3739 tree super = NULL_TREE, itf = NULL_TREE;
3740 tree id, type_decl, class;
3741
3742 /* The unqualified name of the anonymous class. It's just a number. */
3743 sprintf (buffer, "%d", anonymous_class_counter++);
3744 id = build_wfl_node (get_identifier (buffer));
3745 EXPR_WFL_LINECOL (id) = location;
3746
3747 /* We know about the type to extend/implement. We go ahead */
3748 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3749 {
3750 /* Create a class which either implements on extends the designated
3751 class. The class bears an innacessible name. */
3752 if (CLASS_INTERFACE (type_decl))
3753 {
3754 /* It's OK to modify it here. It's been already used and
3755 shouldn't be reused */
3756 ctxp->interface_number = 1;
3757 /* Interfaces should presented as a list of WFLs */
3758 itf = build_tree_list (type_name, NULL_TREE);
3759 }
3760 else
3761 super = type_name;
3762 }
3763
3764 class = create_class (ACC_FINAL, id, super, itf);
3765
3766 /* We didn't know anything about the stuff. We register a dependence. */
3767 if (!type_decl)
3768 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3769
3770 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3771 return class;
3772}
3773
a40d21da 3774/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3775 interface's decl in pass 2. */
3776
3777static tree
3778create_class (flags, id, super, interfaces)
3779 int flags;
3780 tree id, super, interfaces;
3781{
e04a16fb
AG
3782 tree raw_name = EXPR_WFL_NODE (id);
3783 tree class_id, decl;
9ee9b555 3784 tree super_decl_type;
e04a16fb 3785
98a52c2c 3786 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3787 decl = IDENTIFIER_CLASS_VALUE (class_id);
3788 EXPR_WFL_NODE (id) = class_id;
3789
3790 /* Basic check: scope, redefinition, modifiers */
3791 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3792 {
3793 PUSH_ERROR ();
3794 return NULL_TREE;
3795 }
3796
3797 /* Suspend the current parsing context if we're parsing an inner
3798 class or an anonymous class. */
3799 if (CPC_INNER_P ())
3800 java_parser_context_suspend ();
3801 /* Push a new context for (static) initialized upon declaration fields */
3802 java_parser_context_push_initialized_field ();
e04a16fb
AG
3803
3804 /* Class modifier check:
3805 - Allowed modifier (already done at that point)
3806 - abstract AND final forbidden
3807 - Public classes defined in the correct file */
3808 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3809 parse_error_context
3810 (id, "Class `%s' can't be declared both abstract and final",
3811 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3812
3813 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3814 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3815
3816 /* If SUPER exists, use it, otherwise use Object */
3817 if (super)
3818 {
3819 /* Can't extend java.lang.Object */
3820 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3821 {
3822 parse_error_context (id, "Can't extend `java.lang.Object'");
3823 return NULL_TREE;
3824 }
3825
2c3199bc
PB
3826 super_decl_type =
3827 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3828 }
3829 else if (TREE_TYPE (decl) != object_type_node)
3830 super_decl_type = object_type_node;
3831 /* We're defining java.lang.Object */
3832 else
3833 super_decl_type = NULL_TREE;
3834
3835 /* Set super info and mark the class a complete */
3836 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3837 ctxp->interface_number);
3838 ctxp->interface_number = 0;
3839 CLASS_COMPLETE_P (decl) = 1;
3840 add_superinterfaces (decl, interfaces);
3841
c2952b01
APB
3842 /* Add the private this$<n> field, Replicate final locals still in
3843 scope as private final fields mangled like val$<local_name>.
3844 This doesn't not occur for top level (static) inner classes. */
3845 if (PURE_INNER_CLASS_DECL_P (decl))
3846 add_inner_class_fields (decl, current_function_decl);
3847
7f10c2e2
APB
3848 /* If doing xref, store the location at which the inherited class
3849 (if any) was seen. */
3850 if (flag_emit_xref && super)
3851 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3852
5e942c50
APB
3853 /* Eventually sets the @deprecated tag flag */
3854 CHECK_DEPRECATED (decl);
3855
165f37bc
APB
3856 /* Reset the anonymous class counter when declaring non inner classes */
3857 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3858 anonymous_class_counter = 1;
3859
e04a16fb
AG
3860 return decl;
3861}
3862
c2952b01
APB
3863/* End a class declaration: register the statements used to create
3864 $finit$ and <clinit>, pop the current class and resume the prior
3865 parser context if necessary. */
3866
3867static void
3868end_class_declaration (resume)
3869 int resume;
3870{
3871 /* If an error occured, context weren't pushed and won't need to be
3872 popped by a resume. */
3873 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3874
3875 java_parser_context_pop_initialized_field ();
3876 POP_CPC ();
3877 if (resume && no_error_occured)
3878 java_parser_context_resume ();
93220702
APB
3879
3880 /* We're ending a class declaration, this is a good time to reset
3881 the interface cout. Note that might have been already done in
3882 create_interface, but if at that time an inner class was being
3883 dealt with, the interface count was reset in a context created
3884 for the sake of handling inner classes declaration. */
3885 ctxp->interface_number = 0;
c2952b01
APB
3886}
3887
3888static void
3889add_inner_class_fields (class_decl, fct_decl)
3890 tree class_decl;
3891 tree fct_decl;
3892{
3893 tree block, marker, f;
3894
3895 f = add_field (TREE_TYPE (class_decl),
3896 build_current_thisn (TREE_TYPE (class_decl)),
3897 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3898 ACC_PRIVATE);
3899 FIELD_THISN (f) = 1;
3900
3901 if (!fct_decl)
3902 return;
3903
3904 for (block = GET_CURRENT_BLOCK (fct_decl);
3905 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3906 {
3907 tree decl;
3908 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3909 {
3910 char *name, *pname;
3911 tree wfl, init, list;
3912
3913 /* Avoid non final arguments. */
3914 if (!LOCAL_FINAL (decl))
3915 continue;
3916
3917 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3918 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3919 wfl = build_wfl_node (get_identifier (name));
3920 init = build_wfl_node (get_identifier (pname));
3921 /* Build an initialization for the field: it will be
3922 initialized by a parameter added to $finit$, bearing a
3923 mangled name of the field itself (param$<n>.) The
3924 parameter is provided to $finit$ by the constructor
3925 invoking it (hence the constructor will also feature a
3926 hidden parameter, set to the value of the outer context
3927 local at the time the inner class is created.)
3928
3929 Note: we take into account all possible locals that can
3930 be accessed by the inner class. It's actually not trivial
3931 to minimize these aliases down to the ones really
3932 used. One way to do that would be to expand all regular
3933 methods first, then $finit$ to get a picture of what's
3934 used. It works with the exception that we would have to
3935 go back on all constructor invoked in regular methods to
3936 have their invokation reworked (to include the right amount
3937 of alias initializer parameters.)
3938
3939 The only real way around, I think, is a first pass to
3940 identify locals really used in the inner class. We leave
3941 the flag FIELD_LOCAL_ALIAS_USED around for that future
3942 use.
3943
3944 On the other hand, it only affect local inner classes,
3945 whose constructors (and $finit$ call) will be featuring
3946 unecessary arguments. It's easy for a developper to keep
3947 this number of parameter down by using the `final'
3948 keyword only when necessary. For the time being, we can
3949 issue a warning on unecessary finals. FIXME */
3950 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3951 wfl, init);
3952
3953 /* Register the field. The TREE_LIST holding the part
3954 initialized/initializer will be marked ARG_FINAL_P so
3955 that the created field can be marked
3956 FIELD_LOCAL_ALIAS. */
3957 list = build_tree_list (wfl, init);
3958 ARG_FINAL_P (list) = 1;
3959 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3960 }
3961 }
3962
3963 if (!CPC_INITIALIZER_STMT (ctxp))
3964 return;
3965
3966 /* If we ever registered an alias field, insert and marker to
3967 remeber where the list ends. The second part of the list (the one
3968 featuring initialized fields) so it can be later reversed to
3969 enforce 8.5. The marker will be removed during that operation. */
3970 marker = build_tree_list (NULL_TREE, NULL_TREE);
3971 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3972 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3973}
3974
e04a16fb
AG
3975/* Can't use lookup_field () since we don't want to load the class and
3976 can't set the CLASS_LOADED_P flag */
3977
3978static tree
3979find_field (class, name)
3980 tree class;
3981 tree name;
3982{
3983 tree decl;
3984 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3985 {
3986 if (DECL_NAME (decl) == name)
3987 return decl;
3988 }
3989 return NULL_TREE;
3990}
3991
3992/* Wrap around lookup_field that doesn't potentially upset the value
3993 of CLASS */
3994
3995static tree
3996lookup_field_wrapper (class, name)
3997 tree class, name;
3998{
3999 tree type = class;
9a7ab4b3 4000 tree decl = NULL_TREE;
c877974e 4001 java_parser_context_save_global ();
f2760b27
APB
4002
4003 /* Last chance: if we're within the context of an inner class, we
4004 might be trying to access a local variable defined in an outer
4005 context. We try to look for it now. */
9a7ab4b3 4006 if (INNER_CLASS_TYPE_P (class))
f2760b27
APB
4007 {
4008 char *alias_buffer;
9a7ab4b3 4009 tree new_name;
f2760b27 4010 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
9a7ab4b3
APB
4011 new_name = get_identifier (alias_buffer);
4012 decl = lookup_field (&type, new_name);
f2760b27
APB
4013 if (decl && decl != error_mark_node)
4014 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4015 }
9a7ab4b3
APB
4016 if (!decl || decl == error_mark_node)
4017 {
4018 type = class;
4019 decl = lookup_field (&type, name);
4020 }
f2760b27 4021
c877974e 4022 java_parser_context_restore_global ();
93024893 4023 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4024}
4025
4026/* Find duplicate field within the same class declarations and report
c583dd46
APB
4027 the error. Returns 1 if a duplicated field was found, 0
4028 otherwise. */
e04a16fb
AG
4029
4030static int
c583dd46 4031duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4032 tree new_field_name, new_type, cl;
e04a16fb
AG
4033{
4034 /* This might be modified to work with method decl as well */
c2952b01 4035 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4036 if (decl)
4037 {
c2e3db92 4038 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4039 ((TREE_CODE (new_type) == POINTER_TYPE
4040 && TREE_TYPE (new_type) == NULL_TREE) ?
4041 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4042 lang_printable_name (new_type, 1)));
c877974e
APB
4043 /* The type may not have been completed by the time we report
4044 the error */
c2e3db92 4045 char *t2 = xstrdup (purify_type_name
4a5f66c3 4046 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4047 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4048 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4049 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4050 parse_error_context
4051 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4052 t1, IDENTIFIER_POINTER (new_field_name),
4053 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4054 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4055 free (t1);
4056 free (t2);
c583dd46 4057 return 1;
e04a16fb 4058 }
c583dd46 4059 return 0;
e04a16fb
AG
4060}
4061
4062/* Field registration routine. If TYPE doesn't exist, field
4063 declarations are linked to the undefined TYPE dependency list, to
4064 be later resolved in java_complete_class () */
4065
4066static void
4067register_fields (flags, type, variable_list)
4068 int flags;
4069 tree type, variable_list;
4070{
c583dd46 4071 tree current, saved_type;
c2952b01 4072 tree class_type = NULL_TREE;
e04a16fb
AG
4073 int saved_lineno = lineno;
4074 int must_chain = 0;
4075 tree wfl = NULL_TREE;
4076
c2952b01
APB
4077 if (GET_CPC ())
4078 class_type = TREE_TYPE (GET_CPC ());
4079
4080 if (!class_type || class_type == error_mark_node)
4081 return;
4082
e04a16fb
AG
4083 /* If we're adding fields to interfaces, those fields are public,
4084 static, final */
4085 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4086 {
4087 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4088 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4089 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4090 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4091 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4092 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4093 check_modifiers ("Illegal interface member modifier `%s'", flags,
4094 INTERFACE_FIELD_MODIFIERS);
4095 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4096 }
4097
c583dd46
APB
4098 /* Obtain a suitable type for resolution, if necessary */
4099 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4100
4101 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4102 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4103
c583dd46
APB
4104 for (current = variable_list, saved_type = type; current;
4105 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4106 {
c877974e 4107 tree real_type;
c583dd46 4108 tree field_decl;
e04a16fb
AG
4109 tree cl = TREE_PURPOSE (current);
4110 tree init = TREE_VALUE (current);
4111 tree current_name = EXPR_WFL_NODE (cl);
4112
cf1748bf 4113 /* Can't declare non-final static fields in inner classes */
c2952b01 4114 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4115 && !(flags & ACC_FINAL))
c2952b01 4116 parse_error_context
cf1748bf 4117 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4118 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4119 lang_printable_name (class_type, 0));
4120
c583dd46
APB
4121 /* Process NAME, as it may specify extra dimension(s) for it */
4122 type = build_array_from_name (type, wfl, current_name, &current_name);
4123
c583dd46
APB
4124 /* Type adjustment. We may have just readjusted TYPE because
4125 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4126 a reference if we can and don't have one already. Also
4127 change the name if we have an init. */
4128 if (type != saved_type)
4129 {
1886c9d8 4130 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4131 if (init)
4132 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4133 }
e04a16fb 4134
c877974e
APB
4135 real_type = GET_REAL_TYPE (type);
4136 /* Check for redeclarations */
4137 if (duplicate_declaration_error_p (current_name, real_type, cl))
4138 continue;
4139
c583dd46 4140 /* Set lineno to the line the field was found and create a
5e942c50 4141 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4142 if (flag_emit_xref)
4143 lineno = EXPR_WFL_LINECOL (cl);
4144 else
4145 lineno = EXPR_WFL_LINENO (cl);
c877974e 4146 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4147 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4148
4149 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4150 mark the created field FIELD_LOCAL_ALIAS, so that we can
4151 hide parameters to this inner class $finit$ and constructors. */
4152 if (ARG_FINAL_P (current))
4153 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4154
4155 /* Check if we must chain. */
4156 if (must_chain)
4157 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4158
c583dd46
APB
4159 /* If we have an initialization value tied to the field */
4160 if (init)
4161 {
4162 /* The field is declared static */
e04a16fb 4163 if (flags & ACC_STATIC)
e04a16fb 4164 {
7525cc04
APB
4165 /* We include the field and its initialization part into
4166 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4167 walked, field initializations will be processed and
4168 fields initialized with known constants will be taken
4169 out of <clinit> and have their DECL_INITIAL set
7525cc04 4170 appropriately. */
c2952b01
APB
4171 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4172 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4173 if (TREE_OPERAND (init, 1)
4174 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4175 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4176 }
5e942c50
APB
4177 /* A non-static field declared with an immediate initialization is
4178 to be initialized in <init>, if any. This field is remembered
4179 to be processed at the time of the generation of <init>. */
c583dd46
APB
4180 else
4181 {
c2952b01
APB
4182 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4183 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4184 }
5b09b33e 4185 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4186 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4187 }
4188 }
4189 lineno = saved_lineno;
4190}
4191
c2952b01
APB
4192/* Generate $finit$, using the list of initialized fields to populate
4193 its body. $finit$'s parameter(s) list is adjusted to include the
4194 one(s) used to initialized the field(s) caching outer context
4195 local(s). */
22eed1e6 4196
c2952b01
APB
4197static tree
4198generate_finit (class_type)
4199 tree class_type;
22eed1e6 4200{
c2952b01
APB
4201 int count = 0;
4202 tree list = TYPE_FINIT_STMT_LIST (class_type);
4203 tree mdecl, current, parms;
4204
4205 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4206 class_type, NULL_TREE,
4207 &count);
4208 CRAFTED_PARAM_LIST_FIXUP (parms);
4209 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4210 finit_identifier_node, parms);
4211 fix_method_argument_names (parms, mdecl);
4212 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4213 mdecl, NULL_TREE);
4214 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4215 start_artificial_method_body (mdecl);
4216
c2952b01 4217 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4218 java_method_add_stmt (mdecl,
4219 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4220 current));
22eed1e6 4221 end_artificial_method_body (mdecl);
c2952b01 4222 return mdecl;
22eed1e6
APB
4223}
4224
e04a16fb 4225static void
c2952b01
APB
4226add_instance_initializer (mdecl)
4227 tree mdecl;
e04a16fb 4228{
c2952b01
APB
4229 tree current;
4230 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4231 tree compound = NULL_TREE;
e04a16fb 4232
c2952b01 4233 if (stmt_list)
e04a16fb 4234 {
c2952b01
APB
4235 for (current = stmt_list; current; current = TREE_CHAIN (current))
4236 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4237
c2952b01
APB
4238 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4239 NULL_TREE, compound));
4240 }
e04a16fb
AG
4241}
4242
4243/* Shared accros method_declarator and method_header to remember the
4244 patch stage that was reached during the declaration of the method.
4245 A method DECL is built differently is there is no patch
4246 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4247 pending on the currently defined method. */
4248
4249static int patch_stage;
4250
4251/* Check the method declaration and add the method to its current
4252 class. If the argument list is known to contain incomplete types,
4253 the method is partially added and the registration will be resume
22eed1e6
APB
4254 once the method arguments resolved. If TYPE is NULL, we're dealing
4255 with a constructor. */
e04a16fb
AG
4256
4257static tree
4258method_header (flags, type, mdecl, throws)
4259 int flags;
4260 tree type, mdecl, throws;
4261{
1886c9d8 4262 tree type_wfl = NULL_TREE;
79d13333 4263 tree meth_name = NULL_TREE;
c2952b01 4264 tree current, orig_arg, this_class = NULL;
34d4df06 4265 tree id, meth;
e04a16fb 4266 int saved_lineno;
1886c9d8 4267 int constructor_ok = 0, must_chain;
c2952b01 4268 int count;
34d4df06
APB
4269
4270 if (mdecl == error_mark_node)
4271 return error_mark_node;
4272 meth = TREE_VALUE (mdecl);
4273 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4274
4275 check_modifiers_consistency (flags);
79d13333 4276
c2952b01
APB
4277 if (GET_CPC ())
4278 this_class = TREE_TYPE (GET_CPC ());
4279
4280 if (!this_class || this_class == error_mark_node)
79d13333 4281 return NULL_TREE;
e04a16fb
AG
4282
4283 /* There are some forbidden modifiers for an abstract method and its
4284 class must be abstract as well. */
22eed1e6 4285 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4286 {
4287 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4288 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4289 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4290 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4291 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4292 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4293 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4294 parse_error_context
781b0558 4295 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4296 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4297 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4298 }
c2952b01 4299
22eed1e6
APB
4300 /* Things to be checked when declaring a constructor */
4301 if (!type)
4302 {
4303 int ec = java_error_count;
4304 /* 8.6: Constructor declarations: we might be trying to define a
4305 method without specifying a return type. */
c2952b01 4306 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4307 parse_error_context
4308 (id, "Invalid method declaration, return type required");
4309 /* 8.6.3: Constructor modifiers */
4310 else
4311 {
4312 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4313 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4314 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4315 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4316 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4317 }
4318 /* If we found error here, we don't consider it's OK to tread
4319 the method definition as a constructor, for the rest of this
4320 function */
4321 if (ec == java_error_count)
4322 constructor_ok = 1;
4323 }
e04a16fb
AG
4324
4325 /* Method declared within the scope of an interface are implicitly
4326 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4327 modifiers are checked right after. */
e04a16fb
AG
4328
4329 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4330 {
4331 /* If FLAGS isn't set because of a modifier, turn the
4332 corresponding modifier WFL to NULL so we issue a warning on
4333 the obsolete use of the modifier */
4334 if (!(flags & ACC_PUBLIC))
4335 MODIFIER_WFL (PUBLIC_TK) = NULL;
4336 if (!(flags & ACC_ABSTRACT))
4337 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4338 flags |= ACC_PUBLIC;
4339 flags |= ACC_ABSTRACT;
4340 }
4341
c2952b01
APB
4342 /* Inner class can't declare static methods */
4343 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4344 {
4345 parse_error_context
4346 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4347 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4348 lang_printable_name (this_class, 0));
4349 }
4350
e04a16fb
AG
4351 /* Modifiers context reset moved up, so abstract method declaration
4352 modifiers can be later checked. */
4353
22eed1e6
APB
4354 /* Set constructor returned type to void and method name to <init>,
4355 unless we found an error identifier the constructor (in which
4356 case we retain the original name) */
4357 if (!type)
4358 {
4359 type = void_type_node;
4360 if (constructor_ok)
4361 meth_name = init_identifier_node;
4362 }
4363 else
4364 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4365
1886c9d8
APB
4366 /* Do the returned type resolution and registration if necessary */
4367 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4368
4a5f66c3
APB
4369 if (meth_name)
4370 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4371 EXPR_WFL_NODE (id) = meth_name;
4372 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4373
4374 if (must_chain)
e04a16fb 4375 {
1886c9d8
APB
4376 patch_stage = JDEP_METHOD_RETURN;
4377 register_incomplete_type (patch_stage, type_wfl, id, type);
4378 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4379 }
4380 else
1886c9d8 4381 TREE_TYPE (meth) = type;
e04a16fb
AG
4382
4383 saved_lineno = lineno;
4384 /* When defining an abstract or interface method, the curly
4385 bracket at level 1 doesn't exist because there is no function
4386 body */
4387 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4388 EXPR_WFL_LINENO (id));
4389
5e942c50
APB
4390 /* Remember the original argument list */
4391 orig_arg = TYPE_ARG_TYPES (meth);
4392
e04a16fb
AG
4393 if (patch_stage) /* includes ret type and/or all args */
4394 {
4395 jdep *jdep;
4396 meth = add_method_1 (this_class, flags, meth_name, meth);
4397 /* Patch for the return type */
4398 if (patch_stage == JDEP_METHOD_RETURN)
4399 {
4400 jdep = CLASSD_LAST (ctxp->classd_list);
4401 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4402 }
4403 /* This is the stop JDEP. METH allows the function's signature
4404 to be computed. */
4405 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4406 }
4407 else
5e942c50
APB
4408 meth = add_method (this_class, flags, meth_name,
4409 build_java_signature (meth));
4410
c2952b01
APB
4411 /* Remember final parameters */
4412 MARK_FINAL_PARMS (meth, orig_arg);
4413
5e942c50
APB
4414 /* Fix the method argument list so we have the argument name
4415 information */
4416 fix_method_argument_names (orig_arg, meth);
4417
4418 /* Register the parameter number and re-install the current line
4419 number */
e04a16fb
AG
4420 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4421 lineno = saved_lineno;
b9f7e36c
APB
4422
4423 /* Register exception specified by the `throws' keyword for
4424 resolution and set the method decl appropriate field to the list.
4425 Note: the grammar ensures that what we get here are class
4426 types. */
4427 if (throws)
4428 {
4429 throws = nreverse (throws);
4430 for (current = throws; current; current = TREE_CHAIN (current))
4431 {
4432 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4433 NULL_TREE, NULL_TREE);
4434 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4435 &TREE_VALUE (current);
4436 }
4437 DECL_FUNCTION_THROWS (meth) = throws;
4438 }
4439
e04a16fb
AG
4440 /* We set the DECL_NAME to ID so we can track the location where
4441 the function was declared. This allow us to report
4442 redefinition error accurately. When method are verified,
4443 DECL_NAME is reinstalled properly (using the content of the
4444 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4445 when Object is being defined. Constructor <init> names will be
4446 reinstalled the same way. */
c2952b01 4447 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4448 DECL_NAME (meth) = id;
22eed1e6
APB
4449
4450 /* Set the flag if we correctly processed a constructor */
4451 if (constructor_ok)
c2952b01
APB
4452 {
4453 DECL_CONSTRUCTOR_P (meth) = 1;
4454 /* Compute and store the number of artificial parameters declared
4455 for this constructor */
4456 for (count = 0, current = TYPE_FIELDS (this_class); current;
4457 current = TREE_CHAIN (current))
4458 if (FIELD_LOCAL_ALIAS (current))
4459 count++;
4460 DECL_FUNCTION_NAP (meth) = count;
4461 }
22eed1e6 4462
5e942c50
APB
4463 /* Eventually set the @deprecated tag flag */
4464 CHECK_DEPRECATED (meth);
4465
7f10c2e2
APB
4466 /* If doing xref, store column and line number information instead
4467 of the line number only. */
4468 if (flag_emit_xref)
4469 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4470
e04a16fb
AG
4471 return meth;
4472}
4473
5e942c50
APB
4474static void
4475fix_method_argument_names (orig_arg, meth)
4476 tree orig_arg, meth;
4477{
4478 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4479 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4480 {
4481 TREE_PURPOSE (arg) = this_identifier_node;
4482 arg = TREE_CHAIN (arg);
4483 }
de4c7b02 4484 while (orig_arg != end_params_node)
5e942c50
APB
4485 {
4486 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4487 orig_arg = TREE_CHAIN (orig_arg);
4488 arg = TREE_CHAIN (arg);
4489 }
4490}
4491
22eed1e6
APB
4492/* Complete the method declaration with METHOD_BODY. */
4493
4494static void
b635eb2f 4495finish_method_declaration (method_body)
22eed1e6
APB
4496 tree method_body;
4497{
79d13333
APB
4498 int flags;
4499
4500 if (!current_function_decl)
4501 return;
4502
4503 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4504
4505 /* 8.4.5 Method Body */
4506 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4507 {
4508 tree wfl = DECL_NAME (current_function_decl);
4509 parse_error_context (wfl,
4510 "%s method `%s' can't have a body defined",
4511 (METHOD_NATIVE (current_function_decl) ?
4512 "Native" : "Abstract"),
4513 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4514 method_body = NULL_TREE;
4515 }
4516 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4517 {
4518 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4519 parse_error_context
4520 (wfl,
4521 "Non native and non abstract method `%s' must have a body defined",
4522 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4523 method_body = NULL_TREE;
4524 }
4525
2c56429a
APB
4526 if (flag_emit_class_files && method_body
4527 && TREE_CODE (method_body) == NOP_EXPR
4528 && TREE_TYPE (current_function_decl)
4529 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4530 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4531
22eed1e6
APB
4532 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4533 maybe_absorb_scoping_blocks ();
4534 /* Exit function's body */
4535 exit_block ();
4536 /* Merge last line of the function with first line, directly in the
4537 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4538 if (!flag_emit_xref)
4539 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4540
4541 /* Since function's argument's list are shared, reset the
4542 ARG_FINAL_P parameter that might have been set on some of this
4543 function parameters. */
4544 UNMARK_FINAL_PARMS (current_function_decl);
4545
f099f336
APB
4546 /* So we don't have an irrelevant function declaration context for
4547 the next static block we'll see. */
4548 current_function_decl = NULL_TREE;
22eed1e6
APB
4549}
4550
4551/* Build a an error message for constructor circularity errors. */
4552
4553static char *
4554constructor_circularity_msg (from, to)
4555 tree from, to;
4556{
4557 static char string [4096];
c2e3db92 4558 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4559 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4560 free (t);
4561 return string;
4562}
4563
4564/* Verify a circular call to METH. Return 1 if an error is found, 0
4565 otherwise. */
4566
4567static int
4568verify_constructor_circularity (meth, current)
4569 tree meth, current;
4570{
4571 static tree list = NULL_TREE;
4572 tree c;
4573 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4574 {
4575 if (TREE_VALUE (c) == meth)
4576 {
4577 char *t;
4578 if (list)
4579 {
4580 tree liste;
4581 list = nreverse (list);
4582 for (liste = list; liste; liste = TREE_CHAIN (liste))
4583 {
4584 parse_error_context
c63b98cd 4585 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4586 constructor_circularity_msg
4587 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4588 java_error_count--;
4589 }
4590 }
c2e3db92 4591 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4592 parse_error_context (TREE_PURPOSE (c),
4593 "%s: recursive invocation of constructor `%s'",
4594 constructor_circularity_msg (current, meth), t);
4595 free (t);
4596 list = NULL_TREE;
4597 return 1;
4598 }
4599 }
4600 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4601 {
4602 list = tree_cons (c, current, list);
4603 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4604 return 1;
4605 list = TREE_CHAIN (list);
4606 }
4607 return 0;
4608}
4609
e04a16fb
AG
4610/* Check modifiers that can be declared but exclusively */
4611
4612static void
4613check_modifiers_consistency (flags)
4614 int flags;
4615{
4616 int acc_count = 0;
4617 tree cl = NULL_TREE;
4618
e0fc4118
TT
4619 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4620 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4621 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4622 if (acc_count > 1)
4623 parse_error_context
e0fc4118
TT
4624 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4625
4626 acc_count = 0;
4627 cl = NULL_TREE;
14d075d8
TT
4628 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4629 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4630 if (acc_count > 1)
4631 parse_error_context (cl,
4632 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4633}
4634
4635/* Check the methode header METH for abstract specifics features */
4636
4637static void
4638check_abstract_method_header (meth)
4639 tree meth;
4640{
4641 int flags = get_access_flags_from_decl (meth);
4642 /* DECL_NAME might still be a WFL node */
c877974e 4643 tree name = GET_METHOD_NAME (meth);
e04a16fb 4644
2884c41e
KG
4645 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4646 ACC_ABSTRACT, "abstract method",
4647 IDENTIFIER_POINTER (name));
4648 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4649 ACC_PUBLIC, "abstract method",
4650 IDENTIFIER_POINTER (name));
e04a16fb
AG
4651
4652 check_modifiers ("Illegal modifier `%s' for interface method",
4653 flags, INTERFACE_METHOD_MODIFIERS);
4654}
4655
4656/* Create a FUNCTION_TYPE node and start augmenting it with the
4657 declared function arguments. Arguments type that can't be resolved
4658 are left as they are, but the returned node is marked as containing
4659 incomplete types. */
4660
4661static tree
4662method_declarator (id, list)
4663 tree id, list;
4664{
4665 tree arg_types = NULL_TREE, current, node;
4666 tree meth = make_node (FUNCTION_TYPE);
4667 jdep *jdep;
e04a16fb
AG
4668
4669 patch_stage = JDEP_NO_PATCH;
c2952b01 4670
34d4df06
APB
4671 if (GET_CPC () == error_mark_node)
4672 return error_mark_node;
4673
c2952b01
APB
4674 /* If we're dealing with an inner class constructor, we hide the
4675 this$<n> decl in the name field of its parameter declaration. We
4676 also might have to hide the outer context local alias
4677 initializers. Not done when the class is a toplevel class. */
4678 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4679 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4680 {
4681 tree aliases_list, type, thisn;
4682 /* First the aliases, linked to the regular parameters */
4683 aliases_list =
4684 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4685 TREE_TYPE (GET_CPC ()),
4686 NULL_TREE, NULL);
4687 list = chainon (nreverse (aliases_list), list);
4688
4689 /* Then this$<n> */
4690 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4691 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4692 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4693 list);
4694 }
e04a16fb
AG
4695
4696 for (current = list; current; current = TREE_CHAIN (current))
4697 {
c583dd46 4698 int must_chain = 0;
e04a16fb
AG
4699 tree wfl_name = TREE_PURPOSE (current);
4700 tree type = TREE_VALUE (current);
4701 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4702 tree already, arg_node;
4703 tree type_wfl = NULL_TREE;
23a79c61 4704 tree real_type;
c583dd46
APB
4705
4706 /* Obtain a suitable type for resolution, if necessary */
4707 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4708
4709 /* Process NAME, as it may specify extra dimension(s) for it */
4710 type = build_array_from_name (type, type_wfl, name, &name);
4711 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4712
23a79c61
APB
4713 real_type = GET_REAL_TYPE (type);
4714 if (TREE_CODE (real_type) == RECORD_TYPE)
4715 {
4716 real_type = promote_type (real_type);
4717 if (TREE_CODE (type) == TREE_LIST)
4718 TREE_PURPOSE (type) = real_type;
4719 }
5e942c50 4720
e04a16fb
AG
4721 /* Check redefinition */
4722 for (already = arg_types; already; already = TREE_CHAIN (already))
4723 if (TREE_PURPOSE (already) == name)
4724 {
781b0558
KG
4725 parse_error_context
4726 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4727 IDENTIFIER_POINTER (name),
e04a16fb
AG
4728 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4729 break;
4730 }
4731
4732 /* If we've an incomplete argument type, we know there is a location
4733 to patch when the type get resolved, later. */
4734 jdep = NULL;
c583dd46 4735 if (must_chain)
e04a16fb 4736 {
c583dd46
APB
4737 patch_stage = JDEP_METHOD;
4738 type = register_incomplete_type (patch_stage,
4739 type_wfl, wfl_name, type);
4740 jdep = CLASSD_LAST (ctxp->classd_list);
4741 JDEP_MISC (jdep) = id;
e04a16fb 4742 }
c583dd46 4743
c2952b01 4744 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4745 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4746 /* Remeber arguments declared final. */
4747 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4748
e04a16fb
AG
4749 if (jdep)
4750 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4751 TREE_CHAIN (arg_node) = arg_types;
4752 arg_types = arg_node;
4753 }
de4c7b02 4754 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4755 node = build_tree_list (id, meth);
4756 return node;
4757}
4758
4759static int
4760unresolved_type_p (wfl, returned)
4761 tree wfl;
4762 tree *returned;
4763
4764{
4765 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4766 {
e04a16fb 4767 if (returned)
165f37bc
APB
4768 {
4769 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4770 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4771 *returned = TREE_TYPE (decl);
4772 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4773 *returned = TREE_TYPE (GET_CPC ());
4774 else
4775 *returned = NULL_TREE;
4776 }
e04a16fb
AG
4777 return 1;
4778 }
4779 if (returned)
4780 *returned = wfl;
4781 return 0;
4782}
4783
4784/* From NAME, build a qualified identifier node using the
4785 qualification from the current package definition. */
4786
4787static tree
98a52c2c 4788parser_qualified_classname (name)
e04a16fb
AG
4789 tree name;
4790{
c2952b01
APB
4791 tree nested_class_name;
4792
98a52c2c 4793 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4794 return nested_class_name;
4795
e04a16fb 4796 if (ctxp->package)
c2952b01 4797 return merge_qualified_name (ctxp->package, name);
e04a16fb 4798 else
c2952b01 4799 return name;
e04a16fb
AG
4800}
4801
4802/* Called once the type a interface extends is resolved. Returns 0 if
4803 everything is OK. */
4804
4805static int
4806parser_check_super_interface (super_decl, this_decl, this_wfl)
4807 tree super_decl, this_decl, this_wfl;
4808{
4809 tree super_type = TREE_TYPE (super_decl);
4810
4811 /* Has to be an interface */
c2952b01 4812 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4813 {
4814 parse_error_context
4815 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4816 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4817 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4818 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4819 "interface" : "class"),
4820 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4821 return 1;
4822 }
4823
4824 /* Check scope: same package OK, other package: OK if public */
4825 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4826 return 1;
4827
4828 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4829 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4830 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4831 return 0;
4832}
4833
4834/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4835 0 if everthing is OK. */
4836
4837static int
4838parser_check_super (super_decl, this_decl, wfl)
4839 tree super_decl, this_decl, wfl;
4840{
e04a16fb
AG
4841 tree super_type = TREE_TYPE (super_decl);
4842
4843 /* SUPER should be a CLASS (neither an array nor an interface) */
4844 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4845 {
4846 parse_error_context
4847 (wfl, "Class `%s' can't subclass %s `%s'",
4848 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4849 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4850 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4851 return 1;
4852 }
4853
4854 if (CLASS_FINAL (TYPE_NAME (super_type)))
4855 {
4856 parse_error_context (wfl, "Can't subclass final classes: %s",
4857 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4858 return 1;
4859 }
4860
4861 /* Check scope: same package OK, other package: OK if public */
4862 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4863 return 1;
4864
4865 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4866 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4867 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4868 return 0;
4869}
4870
4871/* Create a new dependency list and link it (in a LIFO manner) to the
4872 CTXP list of type dependency list. */
4873
4874static void
4875create_jdep_list (ctxp)
4876 struct parser_ctxt *ctxp;
4877{
23a79c61 4878 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4879 new->first = new->last = NULL;
4880 new->next = ctxp->classd_list;
4881 ctxp->classd_list = new;
4882}
4883
4884static jdeplist *
4885reverse_jdep_list (ctxp)
4886 struct parser_ctxt *ctxp;
4887{
4888 register jdeplist *prev = NULL, *current, *next;
4889 for (current = ctxp->classd_list; current; current = next)
4890 {
4891 next = current->next;
4892 current->next = prev;
4893 prev = current;
4894 }
4895 return prev;
4896}
4897
23a79c61
APB
4898/* Create a fake pointer based on the ID stored in
4899 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4900 registered again. */
e04a16fb
AG
4901
4902static tree
23a79c61
APB
4903obtain_incomplete_type (type_name)
4904 tree type_name;
e04a16fb 4905{
23a79c61
APB
4906 tree ptr, name;
4907
4908 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4909 name = EXPR_WFL_NODE (type_name);
4910 else if (INCOMPLETE_TYPE_P (type_name))
4911 name = TYPE_NAME (type_name);
4912 else
4913 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4914
4915 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4916 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4917 break;
4918
4919 if (!ptr)
4920 {
e04a16fb 4921 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4922 BUILD_PTR_FROM_NAME (ptr, name);
4923 layout_type (ptr);
e04a16fb
AG
4924 pop_obstacks ();
4925 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4926 ctxp->incomplete_class = ptr;
4927 }
4928
4929 return ptr;
4930}
4931
4932/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4933 non NULL instead of computing a new fake type based on WFL. The new
4934 dependency is inserted in the current type dependency list, in FIFO
4935 manner. */
4936
4937static tree
4938register_incomplete_type (kind, wfl, decl, ptr)
4939 int kind;
4940 tree wfl, decl, ptr;
4941{
23a79c61 4942 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4943
e04a16fb
AG
4944 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4945 ptr = obtain_incomplete_type (wfl);
4946
4947 JDEP_KIND (new) = kind;
4948 JDEP_DECL (new) = decl;
4949 JDEP_SOLV (new) = ptr;
4950 JDEP_WFL (new) = wfl;
4951 JDEP_CHAIN (new) = NULL;
4952 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4953 /* For some dependencies, set the enclosing class of the current
4954 class to be the enclosing context */
4955 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4956 && GET_ENCLOSING_CPC ())
4957 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4958 else
324ed8fd 4959 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4960 JDEP_GET_PATCH (new) = (tree *)NULL;
4961
4962 JDEP_INSERT (ctxp->classd_list, new);
4963
4964 return ptr;
4965}
4966
4967void
4968java_check_circular_reference ()
4969{
4970 tree current;
4971 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4972 {
4973 tree type = TREE_TYPE (current);
e920ebc9 4974 if (CLASS_INTERFACE (current))
e04a16fb
AG
4975 {
4976 /* Check all interfaces this class extends */
4977 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4978 int n, i;
4979
4980 if (!basetype_vec)
4981 return;
4982 n = TREE_VEC_LENGTH (basetype_vec);
4983 for (i = 0; i < n; i++)
4984 {
4985 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4986 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4987 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4988 parse_error_context (lookup_cl (current),
4989 "Cyclic interface inheritance");
4990 }
4991 }
4992 else
4993 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4994 parse_error_context (lookup_cl (current),
c2952b01
APB
4995 "Cyclic class inheritance%s",
4996 (cyclic_inheritance_report ?
4997 cyclic_inheritance_report : ""));
4998 }
4999}
5000
5001/* Augment the parameter list PARM with parameters crafted to
5002 initialize outer context locals aliases. Through ARTIFICIAL, a
5003 count is kept of the number of crafted parameters. MODE governs
5004 what eventually gets created: something suitable for a function
5005 creation or a function invocation, either the constructor or
5006 $finit$. */
5007
5008static tree
5009build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5010 int mode;
5011 tree class_type, parm;
5012 int *artificial;
5013{
5014 tree field;
da632f2c
APB
5015 tree additional_parms = NULL_TREE;
5016
c2952b01
APB
5017 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5018 if (FIELD_LOCAL_ALIAS (field))
5019 {
5020 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5021 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5022
5023 switch (mode)
5024 {
5025 case AIPL_FUNCTION_DECLARATION:
5026 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5027 purpose = build_wfl_node (get_identifier (buffer));
5028 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5029 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5030 else
5031 value = TREE_TYPE (field);
5032 break;
5033
5034 case AIPL_FUNCTION_CREATION:
5035 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5036 purpose = get_identifier (buffer);
5037 value = TREE_TYPE (field);
5038 break;
5039
5040 case AIPL_FUNCTION_FINIT_INVOCATION:
5041 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5042 /* Now, this is wrong. purpose should always be the NAME
5043 of something and value its matching value (decl, type,
5044 etc...) FIXME -- but there is a lot to fix. */
5045
5046 /* When invoked for this kind of operation, we already
5047 know whether a field is used or not. */
5048 purpose = TREE_TYPE (field);
5049 value = build_wfl_node (get_identifier (buffer));
5050 break;
5051
5052 case AIPL_FUNCTION_CTOR_INVOCATION:
5053 /* There are two case: the constructor invokation happends
5054 outside the local inner, in which case, locales from the outer
5055 context are directly used.
5056
5057 Otherwise, we fold to using the alias directly. */
5058 if (class_type == current_class)
5059 value = field;
5060 else
5061 {
5062 name = get_identifier (&buffer[4]);
5063 value = IDENTIFIER_LOCAL_VALUE (name);
5064 }
5065 break;
5066 }
da632f2c 5067 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5068 if (artificial)
5069 *artificial +=1;
5070 }
da632f2c
APB
5071 if (additional_parms)
5072 {
5073 if (ANONYMOUS_CLASS_P (class_type)
5074 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5075 additional_parms = nreverse (additional_parms);
5076 parm = chainon (additional_parms, parm);
5077 }
5078
5079 return parm;
c2952b01
APB
5080}
5081
5082/* Craft a constructor for CLASS_DECL -- what we should do when none
5083 where found. ARGS is non NULL when a special signature must be
5084 enforced. This is the case for anonymous classes. */
5085
5086static void
5087craft_constructor (class_decl, args)
5088 tree class_decl, args;
5089{
5090 tree class_type = TREE_TYPE (class_decl);
5091 tree parm = NULL_TREE;
5092 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5093 ACC_PUBLIC : 0);
5094 int i = 0, artificial = 0;
5095 tree decl, ctor_name;
5096 char buffer [80];
5097
5098 push_obstacks (&permanent_obstack, &permanent_obstack);
5099
5100 /* The constructor name is <init> unless we're dealing with an
5101 anonymous class, in which case the name will be fixed after having
5102 be expanded. */
5103 if (ANONYMOUS_CLASS_P (class_type))
5104 ctor_name = DECL_NAME (class_decl);
5105 else
5106 ctor_name = init_identifier_node;
5107
5108 /* If we're dealing with an inner class constructor, we hide the
5109 this$<n> decl in the name field of its parameter declaration. */
5110 if (PURE_INNER_CLASS_TYPE_P (class_type))
5111 {
5112 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5113 parm = tree_cons (build_current_thisn (class_type),
5114 build_pointer_type (type), parm);
5115
5116 /* Some more arguments to be hidden here. The values of the local
5117 variables of the outer context that the inner class needs to see. */
5118 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5119 class_type, parm,
5120 &artificial);
5121 }
5122
5123 /* Then if there are any args to be enforced, enforce them now */
5124 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5125 {
5126 sprintf (buffer, "parm%d", i++);
5127 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5128 }
c2952b01
APB
5129
5130 CRAFTED_PARAM_LIST_FIXUP (parm);
5131 decl = create_artificial_method (class_type, flags, void_type_node,
5132 ctor_name, parm);
5133 fix_method_argument_names (parm, decl);
5134 /* Now, mark the artificial parameters. */
5135 DECL_FUNCTION_NAP (decl) = artificial;
5136
5137 pop_obstacks ();
5138 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5139}
5140
c2952b01 5141
e920ebc9
APB
5142/* Fix the constructors. This will be called right after circular
5143 references have been checked. It is necessary to fix constructors
5144 early even if no code generation will take place for that class:
5145 some generated constructor might be required by the class whose
5146 compilation triggered this one to be simply loaded. */
5147
5148void
5149java_fix_constructors ()
5150{
5151 tree current;
5152
5153 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5154 {
e920ebc9
APB
5155 tree class_type = TREE_TYPE (current);
5156 int saw_ctor = 0;
c2952b01
APB
5157 tree decl;
5158
5159 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5160 continue;
e920ebc9
APB
5161
5162 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5163 {
5164 if (DECL_CONSTRUCTOR_P (decl))
5165 {
5166 fix_constructors (decl);
5167 saw_ctor = 1;
5168 }
5169 }
5170
c2952b01
APB
5171 /* Anonymous class constructor can't be generated that early. */
5172 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5173 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5174 }
5175}
5176
23a79c61
APB
5177/* safe_layout_class just makes sure that we can load a class without
5178 disrupting the current_class, input_file, lineno, etc, information
5179 about the class processed currently. */
5180
e04a16fb
AG
5181void
5182safe_layout_class (class)
5183 tree class;
5184{
5185 tree save_current_class = current_class;
3b304f5b 5186 const char *save_input_filename = input_filename;
e04a16fb 5187 int save_lineno = lineno;
5e942c50 5188
e04a16fb 5189 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5190
e04a16fb
AG
5191 layout_class (class);
5192 pop_obstacks ();
5e942c50 5193
e04a16fb
AG
5194 current_class = save_current_class;
5195 input_filename = save_input_filename;
5196 lineno = save_lineno;
5197 CLASS_LOADED_P (class) = 1;
5198}
5199
5200static tree
5201jdep_resolve_class (dep)
5202 jdep *dep;
5203{
5204 tree decl;
5205
23a79c61
APB
5206 if (JDEP_RESOLVED_P (dep))
5207 decl = JDEP_RESOLVED_DECL (dep);
5208 else
e04a16fb 5209 {
c2952b01 5210 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5211 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5212 JDEP_RESOLVED (dep, decl);
5213 }
23a79c61 5214
e04a16fb 5215 if (!decl)
23a79c61 5216 complete_class_report_errors (dep);
1e12ab9b 5217 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5218 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5219 return decl;
5220}
5221
5222/* Complete unsatisfied class declaration and their dependencies */
5223
5224void
5225java_complete_class ()
5226{
e04a16fb
AG
5227 tree cclass;
5228 jdeplist *cclassd;
5229 int error_found;
b67d701b 5230 tree type;
e04a16fb
AG
5231
5232 push_obstacks (&permanent_obstack, &permanent_obstack);
5233
9a7ab4b3 5234 /* Process imports */
e04a16fb 5235 process_imports ();
e04a16fb
AG
5236
5237 /* Rever things so we have the right order */
5238 ctxp->class_list = nreverse (ctxp->class_list);
5239 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5240
e04a16fb
AG
5241 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5242 cclass && cclassd;
5243 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5244 {
5245 jdep *dep;
5246 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5247 {
5248 tree decl;
e04a16fb
AG
5249 if (!(decl = jdep_resolve_class (dep)))
5250 continue;
5251
5252 /* Now it's time to patch */
5253 switch (JDEP_KIND (dep))
5254 {
5255 case JDEP_SUPER:
5256 /* Simply patch super */
5257 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5258 continue;
5259 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5260 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5261 break;
5262
5263 case JDEP_FIELD:
5264 {
5265 /* We do part of the job done in add_field */
5266 tree field_decl = JDEP_DECL (dep);
5267 tree field_type = TREE_TYPE (decl);
5268 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5269 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5270 field_type = promote_type (field_type);
5271 pop_obstacks ();
5272 TREE_TYPE (field_decl) = field_type;
5e942c50 5273 DECL_ALIGN (field_decl) = 0;
11cf4d18 5274 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5275 layout_decl (field_decl, 0);
e04a16fb
AG
5276 SOURCE_FRONTEND_DEBUG
5277 (("Completed field/var decl `%s' with `%s'",
5278 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5279 IDENTIFIER_POINTER (DECL_NAME (decl))));
5280 break;
5281 }
5282 case JDEP_METHOD: /* We start patching a method */
5283 case JDEP_METHOD_RETURN:
5284 error_found = 0;
5285 while (1)
5286 {
5287 if (decl)
5288 {
b67d701b
PB
5289 type = TREE_TYPE(decl);
5290 if (TREE_CODE (type) == RECORD_TYPE)
5291 type = promote_type (type);
e04a16fb
AG
5292 JDEP_APPLY_PATCH (dep, type);
5293 SOURCE_FRONTEND_DEBUG
5294 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5295 "Completing fct `%s' with ret type `%s'":
5296 "Completing arg `%s' with type `%s'"),
5297 IDENTIFIER_POINTER (EXPR_WFL_NODE
5298 (JDEP_DECL_WFL (dep))),
5299 IDENTIFIER_POINTER (DECL_NAME (decl))));
5300 }
5301 else
5302 error_found = 1;
5303 dep = JDEP_CHAIN (dep);
5304 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5305 break;
5306 else
5307 decl = jdep_resolve_class (dep);
5308 }
5309 if (!error_found)
5310 {
5311 tree mdecl = JDEP_DECL (dep), signature;
5312 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5313 /* Recompute and reset the signature, check first that
5314 all types are now defined. If they're not,
5315 dont build the signature. */
5316 if (check_method_types_complete (mdecl))
5317 {
5318 signature = build_java_signature (TREE_TYPE (mdecl));
5319 set_java_signature (TREE_TYPE (mdecl), signature);
5320 }
e04a16fb
AG
5321 pop_obstacks ();
5322 }
5323 else
5324 continue;
5325 break;
5326
5327 case JDEP_INTERFACE:
5328 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5329 JDEP_WFL (dep)))
5330 continue;
5331 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5332 break;
5333
b67d701b 5334 case JDEP_PARM:
e04a16fb 5335 case JDEP_VARIABLE:
b67d701b
PB
5336 type = TREE_TYPE(decl);
5337 if (TREE_CODE (type) == RECORD_TYPE)
5338 type = promote_type (type);
5339 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5340 break;
5341
5342 case JDEP_TYPE:
5343 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5344 SOURCE_FRONTEND_DEBUG
5345 (("Completing a random type dependency on a '%s' node",
5346 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5347 break;
5348
b9f7e36c 5349 case JDEP_EXCEPTION:
c877974e
APB
5350 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5351 SOURCE_FRONTEND_DEBUG
5352 (("Completing `%s' `throws' argument node",
5353 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5354 break;
5355
c2952b01
APB
5356 case JDEP_ANONYMOUS:
5357 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5358 break;
5359
e04a16fb 5360 default:
0a2138e2
APB
5361 fatal ("Can't handle patch code %d - java_complete_class",
5362 JDEP_KIND (dep));
e04a16fb
AG
5363 }
5364 }
5365 }
5366 pop_obstacks ();
5367 return;
5368}
5369
5370/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5371 array. */
5372
5373static tree
c2952b01
APB
5374resolve_class (enclosing, class_type, decl, cl)
5375 tree enclosing, class_type, decl, cl;
e04a16fb 5376{
49f48c71
KG
5377 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5378 const char *base = name;
78d21f92
PB
5379 tree resolved_type = TREE_TYPE (class_type);
5380 tree resolved_type_decl;
e04a16fb 5381
78d21f92
PB
5382 if (resolved_type != NULL_TREE)
5383 {
5384 tree resolved_type_decl = TYPE_NAME (resolved_type);
5385 if (resolved_type_decl == NULL_TREE
5386 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5387 {
5388 resolved_type_decl = build_decl (TYPE_DECL,
5389 TYPE_NAME (class_type),
5390 resolved_type);
5391 }
5392 return resolved_type_decl;
5393 }
5394
e04a16fb
AG
5395 /* 1- Check to see if we have an array. If true, find what we really
5396 want to resolve */
5397 while (name[0] == '[')
5398 name++;
5399 if (base != name)
34d4df06
APB
5400 {
5401 TYPE_NAME (class_type) = get_identifier (name);
5402 WFL_STRIP_BRACKET (cl, cl);
5403 }
e04a16fb
AG
5404
5405 /* 2- Resolve the bare type */
c2952b01
APB
5406 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5407 decl, cl)))
e04a16fb
AG
5408 return NULL_TREE;
5409 resolved_type = TREE_TYPE (resolved_type_decl);
5410
5411 /* 3- If we have and array, reconstruct the array down to its nesting */
5412 if (base != name)
5413 {
5414 while (base != name)
5415 {
5416 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5417 resolved_type = promote_type (resolved_type);
5418 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5419 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5420 name--;
5421 }
5422 /* Build a fake decl for this, since this is what is expected to
5423 be returned. */
5424 resolved_type_decl =
5425 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5426 /* Figure how those two things are important for error report. FIXME */
5427 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5428 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5429 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5430 }
78d21f92 5431 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5432 return resolved_type_decl;
5433}
5434
5435/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5436 are used to report error messages. */
5437
78d21f92 5438tree
c2952b01
APB
5439do_resolve_class (enclosing, class_type, decl, cl)
5440 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5441{
5442 tree new_class_decl;
e04a16fb
AG
5443
5444 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5445 it is changed by find_in_imports{_on_demand} and (but it doesn't
5446 really matter) qualify_and_find */
e04a16fb 5447
c2952b01
APB
5448 /* 0- Search in the current class as an inner class */
5449
5450 /* Maybe some code here should be added to load the class or
5451 something, at least if the class isn't an inner class and ended
5452 being loaded from class file. FIXME. */
a40d21da
APB
5453 while (enclosing)
5454 {
5455 tree name;
5456
5457 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5458 return new_class_decl;
5459
0c2b8145
APB
5460 /* Explore enclosing contexts. */
5461 while (INNER_CLASS_DECL_P (enclosing))
5462 {
5463 enclosing = DECL_CONTEXT (enclosing);
5464 if ((new_class_decl = find_as_inner_class (enclosing,
5465 class_type, cl)))
5466 return new_class_decl;
5467 }
5468
a40d21da
APB
5469 /* Now go to the upper classes, bail out if necessary. */
5470 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5471 if (!enclosing || enclosing == object_type_node)
5472 break;
5473
5474 if (TREE_CODE (enclosing) == RECORD_TYPE)
5475 {
5476 enclosing = TYPE_NAME (enclosing);
5477 continue;
5478 }
5479
5480 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5481 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5482 else
5483 name = enclosing;
5484 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5485 }
c2952b01 5486
9a7ab4b3
APB
5487 /* 1- Check for the type in single imports. This will change
5488 TYPE_NAME() if something relevant is found */
5489 find_in_imports (class_type);
e04a16fb 5490
9a7ab4b3 5491 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5492 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5493 {
5494 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5495 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5496 load_class (TYPE_NAME (class_type), 0);
5497 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5498 }
5499
9a7ab4b3
APB
5500 /* 3- Search according to the current package definition */
5501 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5502 {
5503 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5504 TYPE_NAME (class_type))))
5505 return new_class_decl;
5506 }
5507
5508 /* 4- Check the import on demands. Don't allow bar.baz to be
5509 imported from foo.* */
5510 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5511 if (find_in_imports_on_demand (class_type))
5512 return NULL_TREE;
5513
5514 /* If found in find_in_imports_on_demant, the type has already been
5515 loaded. */
5516 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5517 return new_class_decl;
5518
5519 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5520 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5521 {
ee07f4f4 5522 tree package;
d6baf6f5
APB
5523
5524 /* If there is a current package (ctxp->package), it's the first
5525 element of package_list and we can skip it. */
5526 for (package = (ctxp->package ?
5527 TREE_CHAIN (package_list) : package_list);
5528 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5529 if ((new_class_decl = qualify_and_find (class_type,
5530 TREE_PURPOSE (package),
5531 TYPE_NAME (class_type))))
5532 return new_class_decl;
5533 }
5534
5535 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5536 load_class (TYPE_NAME (class_type), 0);
5537 if (check_pkg_class_access (TYPE_NAME (class_type),
5538 (cl ? cl : lookup_cl (decl))))
5539 return NULL_TREE;
5540
9a7ab4b3 5541 /* 6- Last call for a resolution */
e04a16fb
AG
5542 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5543}
5544
9a7ab4b3
APB
5545static tree
5546qualify_and_find (class_type, package, name)
5547 tree class_type, package, name;
5548{
5549 tree new_qualified = merge_qualified_name (package, name);
5550 tree new_class_decl;
5551
5552 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5553 load_class (new_qualified, 0);
5554 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5555 {
5556 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5557 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5558 load_class (new_qualified, 0);
5559 TYPE_NAME (class_type) = new_qualified;
5560 return IDENTIFIER_CLASS_VALUE (new_qualified);
5561 }
5562 return NULL_TREE;
5563}
5564
e04a16fb 5565/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5566 parsed class). Return a decl node. This function is meant to be
5567 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5568
5569static tree
c877974e
APB
5570resolve_and_layout (something, cl)
5571 tree something;
e04a16fb
AG
5572 tree cl;
5573{
34d4df06 5574 tree decl, decl_type;
c877974e 5575
23a79c61
APB
5576 /* Don't do that on the current class */
5577 if (something == current_class)
5578 return TYPE_NAME (current_class);
c877974e 5579
23a79c61 5580 /* Don't do anything for void and other primitive types */
c877974e
APB
5581 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5582 return NULL_TREE;
5583
23a79c61
APB
5584 /* Pointer types can be reall pointer types or fake pointers. When
5585 finding a real pointer, recheck for primitive types */
5586 if (TREE_CODE (something) == POINTER_TYPE)
5587 {
5588 if (TREE_TYPE (something))
5589 {
5590 something = TREE_TYPE (something);
5591 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5592 return NULL_TREE;
5593 }
5594 else
5595 something = TYPE_NAME (something);
5596 }
5597
5598 /* Don't do anything for arrays of primitive types */
5599 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5600 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5601 return NULL_TREE;
5602
c2952b01
APB
5603 /* Something might be a WFL */
5604 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5605 something = EXPR_WFL_NODE (something);
5606
5607 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5608 TYPE_DECL or a real TYPE */
5609 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5610 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5611 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5612
23a79c61
APB
5613 if (!(decl = resolve_no_layout (something, cl)))
5614 return NULL_TREE;
5615
5616 /* Resolve and layout if necessary */
34d4df06
APB
5617 decl_type = TREE_TYPE (decl);
5618 layout_class_methods (decl_type);
5619 /* Check methods */
5620 if (CLASS_FROM_SOURCE_P (decl_type))
5621 java_check_methods (decl);
5622 /* Layout the type if necessary */
5623 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5624 safe_layout_class (decl_type);
23a79c61 5625
e04a16fb
AG
5626 return decl;
5627}
5628
5629/* Resolve a class, returns its decl but doesn't perform any
5630 layout. The current parsing context is saved and restored */
5631
5632static tree
5633resolve_no_layout (name, cl)
5634 tree name, cl;
5635{
5636 tree ptr, decl;
5637 BUILD_PTR_FROM_NAME (ptr, name);
5638 java_parser_context_save_global ();
c2952b01 5639 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5640 java_parser_context_restore_global ();
5641
5642 return decl;
5643}
5644
23a79c61
APB
5645/* Called when reporting errors. Skip leader '[' in a complex array
5646 type description that failed to be resolved. */
e04a16fb 5647
49f48c71 5648static const char *
e04a16fb 5649purify_type_name (name)
49f48c71 5650 const char *name;
e04a16fb
AG
5651{
5652 while (*name && *name == '[')
5653 name++;
5654 return name;
5655}
5656
5657/* The type CURRENT refers to can't be found. We print error messages. */
5658
5659static void
5660complete_class_report_errors (dep)
5661 jdep *dep;
5662{
49f48c71 5663 const char *name;
23a79c61
APB
5664
5665 if (!JDEP_WFL (dep))
5666 return;
5667
5668 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5669 switch (JDEP_KIND (dep))
5670 {
5671 case JDEP_SUPER:
5672 parse_error_context
5673 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5674 purify_type_name (name),
e04a16fb
AG
5675 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5676 break;
5677 case JDEP_FIELD:
5678 parse_error_context
5679 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5680 purify_type_name (name),
e04a16fb
AG
5681 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5682 break;
5683 case JDEP_METHOD: /* Covers arguments */
5684 parse_error_context
781b0558 5685 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5686 purify_type_name (name),
e04a16fb
AG
5687 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5688 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5689 break;
5690 case JDEP_METHOD_RETURN: /* Covers return type */
5691 parse_error_context
781b0558 5692 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5693 purify_type_name (name),
e04a16fb
AG
5694 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5695 break;
5696 case JDEP_INTERFACE:
5697 parse_error_context
5698 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5699 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5700 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5701 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5702 break;
5703 case JDEP_VARIABLE:
5704 parse_error_context
781b0558 5705 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5706 purify_type_name (IDENTIFIER_POINTER
5707 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5708 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5709 break;
b9f7e36c
APB
5710 case JDEP_EXCEPTION: /* As specified by `throws' */
5711 parse_error_context
5712 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5713 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5714 break;
0a2138e2
APB
5715 default:
5716 /* Fix for -Wall. Just break doing nothing. The error will be
5717 caught later */
5718 break;
e04a16fb
AG
5719 }
5720}
5721
22eed1e6
APB
5722/* Return a static string containing the DECL prototype string. If
5723 DECL is a constructor, use the class name instead of the form
5724 <init> */
5725
49f48c71 5726static const char *
22eed1e6
APB
5727get_printable_method_name (decl)
5728 tree decl;
5729{
49f48c71 5730 const char *to_return;
9ee9b555 5731 tree name = NULL_TREE;
22eed1e6
APB
5732
5733 if (DECL_CONSTRUCTOR_P (decl))
5734 {
5735 name = DECL_NAME (decl);
5e942c50 5736 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5737 }
5738
5739 to_return = lang_printable_name (decl, 0);
5740 if (DECL_CONSTRUCTOR_P (decl))
5741 DECL_NAME (decl) = name;
5742
5743 return to_return;
5744}
5745
5e942c50
APB
5746/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5747 nevertheless needs to be verfied, 1 otherwise. */
5748
5749static int
5750reset_method_name (method)
5751 tree method;
5752{
c2952b01 5753 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5754 {
5755 /* NAME is just the plain name when Object is being defined */
5756 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5757 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5758 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5759 return 0;
5760 }
5761 else
5762 return 1;
5763}
5764
c877974e
APB
5765/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5766
5767tree
5768java_get_real_method_name (method_decl)
5769 tree method_decl;
5770{
5771 tree method_name = DECL_NAME (method_decl);
5772 if (DECL_CONSTRUCTOR_P (method_decl))
5773 return init_identifier_node;
82371d41
APB
5774
5775 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5776 and still can be a constructor. FIXME */
5777
23a79c61
APB
5778 /* Don't confuse method only bearing the name of their class as
5779 constructors */
82371d41
APB
5780 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5781 && ctxp
c2952b01 5782 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5783 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5784 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5785 return init_identifier_node;
5786 else
5787 return EXPR_WFL_NODE (method_name);
5788}
5789
22eed1e6
APB
5790/* Track method being redefined inside the same class. As a side
5791 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5792 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5793
e04a16fb
AG
5794static int
5795check_method_redefinition (class, method)
5796 tree class, method;
5797{
5798 tree redef, name;
5799 tree cl = DECL_NAME (method);
c3f2a476 5800 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
5801 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5802 fixed and checked */
5e942c50
APB
5803
5804 /* Reset the method name before running the check. If it returns 1,
5805 the method doesn't need to be verified with respect to method
5806 redeclaration and we return 0 */
5807 if (reset_method_name (method))
e04a16fb 5808 return 0;
5e942c50
APB
5809
5810 name = DECL_NAME (method);
e04a16fb
AG
5811 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5812 {
c3f2a476 5813 if (redef == method)
e04a16fb 5814 break;
c3f2a476
APB
5815 if (DECL_NAME (redef) == name
5816 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5817 {
22eed1e6
APB
5818 parse_error_context
5819 (cl, "Duplicate %s declaration `%s'",
5820 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5821 get_printable_method_name (redef));
e04a16fb
AG
5822 return 1;
5823 }
5824 }
5825 return 0;
5826}
5827
d77613be
APB
5828static void
5829check_abstract_method_definitions (do_interface, class_decl, type)
5830 int do_interface;
5831 tree class_decl, type;
5832{
5833 tree class = TREE_TYPE (class_decl);
5834 tree method, end_type;
5835
5836 end_type = (do_interface ? object_type_node : type);
5837 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5838 {
5839 tree other_super, other_method, method_sig, method_name;
5840 int found = 0;
165f37bc 5841 int end_type_reached = 0;
d77613be
APB
5842
5843 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5844 continue;
5845
5846 /* Now verify that somewhere in between TYPE and CLASS,
5847 abstract method METHOD gets a non abstract definition
5848 that is inherited by CLASS. */
5849
5850 method_sig = build_java_signature (TREE_TYPE (method));
5851 method_name = DECL_NAME (method);
5852 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5853 method_name = EXPR_WFL_NODE (method_name);
5854
165f37bc
APB
5855 other_super = class;
5856 do {
5857 if (other_super == end_type)
5858 end_type_reached = 1;
5859
5860 /* Method search */
5861 for (other_method = TYPE_METHODS (other_super); other_method;
5862 other_method = TREE_CHAIN (other_method))
5863 {
5864 tree s = build_java_signature (TREE_TYPE (other_method));
5865 tree other_name = DECL_NAME (other_method);
5866
5867 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5868 other_name = EXPR_WFL_NODE (other_name);
5869 if (!DECL_CLINIT_P (other_method)
5870 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5871 && method_name == other_name
5872 && method_sig == s
5873 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5874 {
5875 found = 1;
5876 break;
5877 }
5878 }
5879 other_super = CLASSTYPE_SUPER (other_super);
5880 } while (!end_type_reached);
5881
d77613be
APB
5882 /* Report that abstract METHOD didn't find an implementation
5883 that CLASS can use. */
5884 if (!found)
5885 {
c2e3db92 5886 char *t = xstrdup (lang_printable_name
d77613be
APB
5887 (TREE_TYPE (TREE_TYPE (method)), 0));
5888 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5889 tree saved_wfl = NULL_TREE;
5890
5891 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5892 {
5893 saved_wfl = DECL_NAME (method);
5894 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5895 }
5896
5897 parse_error_context
5898 (lookup_cl (class_decl),
781b0558 5899 "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
5900 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5901 t, lang_printable_name (method, 0),
5902 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5903 "interface" : "class"),
5904 IDENTIFIER_POINTER (ccn),
5905 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5906 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5907
5908 free (t);
5909
5910 if (saved_wfl)
5911 DECL_NAME (method) = saved_wfl;
5912 }
5913 }
5914}
5915
614eaae0 5916/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5917 methods. */
5918
5919static void
5920java_check_abstract_method_definitions (class_decl)
5921 tree class_decl;
5922{
5923 tree class = TREE_TYPE (class_decl);
5924 tree super, vector;
5925 int i;
5926
5927 if (CLASS_ABSTRACT (class_decl))
5928 return;
5929
5930 /* Check for inherited types */
165f37bc
APB
5931 super = class;
5932 do {
5933 super = CLASSTYPE_SUPER (super);
5934 check_abstract_method_definitions (0, class_decl, super);
5935 } while (super != object_type_node);
d77613be
APB
5936
5937 /* Check for implemented interfaces. */
5938 vector = TYPE_BINFO_BASETYPES (class);
5939 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5940 {
5941 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5942 check_abstract_method_definitions (1, class_decl, super);
5943 }
5944}
5945
165f37bc
APB
5946/* Check all the types method DECL uses and return 1 if all of them
5947 are now complete, 0 otherwise. This is used to check whether its
5948 safe to build a method signature or not. */
5949
5950static int
5951check_method_types_complete (decl)
5952 tree decl;
5953{
5954 tree type = TREE_TYPE (decl);
5955 tree args;
5956
5957 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5958 return 0;
5959
5960 args = TYPE_ARG_TYPES (type);
5961 if (TREE_CODE (type) == METHOD_TYPE)
5962 args = TREE_CHAIN (args);
5963 for (; args != end_params_node; args = TREE_CHAIN (args))
5964 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5965 return 0;
5966
5967 return 1;
5968}
5969
34d4df06
APB
5970/* Visible interface to check methods contained in CLASS_DECL */
5971
5972void
5973java_check_methods (class_decl)
5974 tree class_decl;
5975{
5976 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5977 return;
5978
5979 if (CLASS_INTERFACE (class_decl))
5980 java_check_abstract_methods (class_decl);
5981 else
5982 java_check_regular_methods (class_decl);
5983
5984 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
5985}
5986
d77613be
APB
5987/* Check all the methods of CLASS_DECL. Methods are first completed
5988 then checked according to regular method existance rules. If no
5989 constructor for CLASS_DECL were encountered, then build its
5990 declaration. */
e04a16fb
AG
5991
5992static void
5993java_check_regular_methods (class_decl)
5994 tree class_decl;
5995{
c2952b01 5996 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
5997 tree method;
5998 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 5999 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6000 tree mthrows;
6001
6002 /* It is not necessary to check methods defined in java.lang.Object */
6003 if (class == object_type_node)
6004 return;
e04a16fb 6005
23a79c61
APB
6006 if (!TYPE_NVIRTUALS (class))
6007 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6008
6009 /* Should take interfaces into account. FIXME */
6010 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6011 {
5e942c50 6012 tree sig;
e04a16fb
AG
6013 tree method_wfl = DECL_NAME (method);
6014 int aflags;
6015
5e942c50
APB
6016 /* If we previously found something and its name was saved,
6017 reinstall it now */
6018 if (found && saved_found_wfl)
ba179f9f
APB
6019 {
6020 DECL_NAME (found) = saved_found_wfl;
6021 saved_found_wfl = NULL_TREE;
6022 }
5e942c50 6023
e04a16fb
AG
6024 /* Check for redefinitions */
6025 if (check_method_redefinition (class, method))
6026 continue;
6027
22eed1e6
APB
6028 /* If we see one constructor a mark so we don't generate the
6029 default one. Also skip other verifications: constructors
6030 can't be inherited hence hiden or overriden */
6031 if (DECL_CONSTRUCTOR_P (method))
6032 {
6033 saw_constructor = 1;
6034 continue;
6035 }
6036
c877974e
APB
6037 /* We verify things thrown by the method. They must inherits from
6038 java.lang.Throwable */
6039 for (mthrows = DECL_FUNCTION_THROWS (method);
6040 mthrows; mthrows = TREE_CHAIN (mthrows))
6041 {
6042 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6043 parse_error_context
781b0558 6044 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6045 IDENTIFIER_POINTER
6046 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6047 }
6048
e04a16fb 6049 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6050 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6051
c2952b01
APB
6052 /* Inner class can't declare static methods */
6053 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6054 {
6055 char *t = xstrdup (lang_printable_name (class, 0));
6056 parse_error_context
6057 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6058 lang_printable_name (method, 0), t);
6059 free (t);
6060 }
6061
5e942c50 6062 /* Nothing overrides or it's a private method. */
aabd7048 6063 if (!found)
5e942c50 6064 continue;
aabd7048
PB
6065 if (METHOD_PRIVATE (found))
6066 {
6067 found = NULL_TREE;
6068 continue;
6069 }
5e942c50
APB
6070
6071 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6072 We set it temporarily for the sake of the error report. */
6073 saved_found_wfl = DECL_NAME (found);
6074 reset_method_name (found);
6075
614eaae0
APB
6076 /* If `found' is declared in an interface, make sure the
6077 modifier matches. */
6078 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6079 && clinit_identifier_node != DECL_NAME (found)
6080 && !METHOD_PUBLIC (method))
6081 {
6082 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6083 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6084 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6085 lang_printable_name (method, 0),
6086 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6087 }
6088
e04a16fb
AG
6089 /* Can't override a method with the same name and different return
6090 types. */
6091 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6092 {
614eaae0
APB
6093 char *t = xstrdup
6094 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6095 parse_error_context
7f10c2e2 6096 (method_wfl,
b9f7e36c 6097 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6098 lang_printable_name (found, 0), t,
b9f7e36c
APB
6099 IDENTIFIER_POINTER
6100 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6101 free (t);
6102 }
e04a16fb 6103
7f10c2e2
APB
6104 aflags = get_access_flags_from_decl (found);
6105 /* If the method has default, access in an other package, then
6106 issue a warning that the current method doesn't override the
6107 one that was found elsewhere. Do not issue this warning when
6108 the match was found in java.lang.Object. */
6109 if (DECL_CONTEXT (found) != object_type_node
a003f638 6110 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6111 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6112 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6113 && flag_not_overriding)
6114 {
6115 parse_warning_context
781b0558 6116 (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
6117 lang_printable_name (found, 0),
6118 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6119 IDENTIFIER_POINTER (DECL_NAME
6120 (TYPE_NAME (DECL_CONTEXT (found)))));
6121 continue;
6122 }
6123
e04a16fb
AG
6124 /* Can't override final. Can't override static. */
6125 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6126 {
6127 /* Static *can* override static */
6128 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6129 continue;
6130 parse_error_context
6131 (method_wfl,
6132 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6133 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6134 lang_printable_name (found, 0),
e04a16fb
AG
6135 (METHOD_FINAL (found) ? "final" : "static"),
6136 IDENTIFIER_POINTER
6137 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6138 continue;
6139 }
7f10c2e2 6140
e04a16fb
AG
6141 /* Static method can't override instance method. */
6142 if (METHOD_STATIC (method))
6143 {
6144 parse_error_context
6145 (method_wfl,
781b0558 6146 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6147 lang_printable_name (found, 0),
e04a16fb
AG
6148 IDENTIFIER_POINTER
6149 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6150 continue;
6151 }
5e942c50 6152
5e942c50
APB
6153 /* - Overriding/hiding public must be public
6154 - Overriding/hiding protected must be protected or public
6155 - If the overriden or hidden method has default (package)
6156 access, then the overriding or hiding method must not be
614eaae0
APB
6157 private; otherwise, a compile-time error occurs. If
6158 `found' belongs to an interface, things have been already
6159 taken care of. */
6160 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6161 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6162 || (METHOD_PROTECTED (found)
6163 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6164 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6165 && METHOD_PRIVATE (method))))
e04a16fb
AG
6166 {
6167 parse_error_context
6168 (method_wfl,
781b0558 6169 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6170 (METHOD_PUBLIC (method) ? "public" :
6171 (METHOD_PRIVATE (method) ? "private" : "protected")),
6172 IDENTIFIER_POINTER (DECL_NAME
6173 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6174 continue;
6175 }
6176
b9f7e36c
APB
6177 /* Overriding methods must have compatible `throws' clauses on checked
6178 exceptions, if any */
6179 check_throws_clauses (method, method_wfl, found);
6180
e04a16fb
AG
6181 /* Inheriting multiple methods with the same signature. FIXME */
6182 }
6183
5e942c50
APB
6184 /* Don't forget eventual pending found and saved_found_wfl. Take
6185 into account that we might have exited because we saw an
d77613be 6186 artificial method as the last entry. */
5e942c50
APB
6187
6188 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6189 DECL_NAME (found) = saved_found_wfl;
6190
23a79c61
APB
6191 if (!TYPE_NVIRTUALS (class))
6192 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6193
d77613be
APB
6194 /* Search for inherited abstract method not yet implemented in this
6195 class. */
6196 java_check_abstract_method_definitions (class_decl);
6197
22eed1e6 6198 if (!saw_constructor)
e920ebc9 6199 fatal ("No constructor found");
e04a16fb
AG
6200}
6201
b9f7e36c
APB
6202/* Return a non zero value if the `throws' clause of METHOD (if any)
6203 is incompatible with the `throws' clause of FOUND (if any). */
6204
6205static void
6206check_throws_clauses (method, method_wfl, found)
6207 tree method, method_wfl, found;
6208{
6209 tree mthrows, fthrows;
6210
c877974e
APB
6211 /* Can't check these things with class loaded from bytecode. FIXME */
6212 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6213 return;
6214
b9f7e36c
APB
6215 for (mthrows = DECL_FUNCTION_THROWS (method);
6216 mthrows; mthrows = TREE_CHAIN (mthrows))
6217 {
6218 /* We don't verify unchecked expressions */
c877974e 6219 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6220 continue;
6221 /* Checked expression must be compatible */
6222 for (fthrows = DECL_FUNCTION_THROWS (found);
6223 fthrows; fthrows = TREE_CHAIN (fthrows))
6224 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6225 break;
6226 if (!fthrows)
6227 {
6228 parse_error_context
781b0558 6229 (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 6230 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6231 lang_printable_name (found, 0),
b9f7e36c
APB
6232 IDENTIFIER_POINTER
6233 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6234 }
6235 }
6236}
6237
e04a16fb
AG
6238/* Check abstract method of interface INTERFACE */
6239
6240static void
5e942c50
APB
6241java_check_abstract_methods (interface_decl)
6242 tree interface_decl;
e04a16fb
AG
6243{
6244 int i, n;
6245 tree method, basetype_vec, found;
5e942c50 6246 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6247
6248 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6249 {
b9f7e36c 6250 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6251
6252 /* 2- Check for double definition inside the defining interface */
6253 if (check_method_redefinition (interface, method))
6254 continue;
6255
6256 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6257 the thrown exceptions (FIXME) */
e04a16fb
AG
6258 found = lookup_java_interface_method2 (interface, method);
6259 if (found)
6260 {
5e942c50
APB
6261 char *t;
6262 tree saved_found_wfl = DECL_NAME (found);
6263 reset_method_name (found);
c2e3db92 6264 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6265 parse_error_context
b9f7e36c 6266 (method_wfl,
5e942c50 6267 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6268 lang_printable_name (found, 0), t,
b9f7e36c
APB
6269 IDENTIFIER_POINTER
6270 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6271 free (t);
5e942c50 6272 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6273 continue;
e04a16fb
AG
6274 }
6275 }
6276
6277 /* 4- Inherited methods can't differ by their returned types */
6278 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6279 return;
6280 n = TREE_VEC_LENGTH (basetype_vec);
6281 for (i = 0; i < n; i++)
6282 {
6283 tree sub_interface_method, sub_interface;
6284 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6285 if (!vec_elt)
6286 continue;
6287 sub_interface = BINFO_TYPE (vec_elt);
6288 for (sub_interface_method = TYPE_METHODS (sub_interface);
6289 sub_interface_method;
6290 sub_interface_method = TREE_CHAIN (sub_interface_method))
6291 {
6292 found = lookup_java_interface_method2 (interface,
6293 sub_interface_method);
6294 if (found && (found != sub_interface_method))
5e942c50
APB
6295 {
6296 tree saved_found_wfl = DECL_NAME (found);
6297 reset_method_name (found);
6298 parse_error_context
6299 (lookup_cl (sub_interface_method),
781b0558 6300 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6301 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6302 lang_printable_name (found, 0),
6303 IDENTIFIER_POINTER
6304 (DECL_NAME (TYPE_NAME
6305 (DECL_CONTEXT (sub_interface_method)))),
6306 IDENTIFIER_POINTER
6307 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6308 DECL_NAME (found) = saved_found_wfl;
6309 }
e04a16fb
AG
6310 }
6311 }
6312}
6313
e04a16fb
AG
6314/* Lookup methods in interfaces using their name and partial
6315 signature. Return a matching method only if their types differ. */
6316
6317static tree
6318lookup_java_interface_method2 (class, method_decl)
6319 tree class, method_decl;
6320{
6321 int i, n;
6322 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6323
6324 if (!basetype_vec)
6325 return NULL_TREE;
6326
6327 n = TREE_VEC_LENGTH (basetype_vec);
6328 for (i = 0; i < n; i++)
6329 {
6330 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6331 if ((BINFO_TYPE (vec_elt) != object_type_node)
6332 && (to_return =
6333 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6334 return to_return;
6335 }
6336 for (i = 0; i < n; i++)
6337 {
6338 to_return = lookup_java_interface_method2
6339 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6340 if (to_return)
6341 return to_return;
6342 }
6343
6344 return NULL_TREE;
6345}
6346
6347/* Lookup method using their name and partial signature. Return a
6348 matching method only if their types differ. */
6349
6350static tree
6351lookup_java_method2 (clas, method_decl, do_interface)
6352 tree clas, method_decl;
6353 int do_interface;
6354{
5e942c50
APB
6355 tree method, method_signature, method_name, method_type, name;
6356
e04a16fb 6357 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6358 name = DECL_NAME (method_decl);
6359 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6360 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6361 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6362
6363 while (clas != NULL_TREE)
6364 {
6365 for (method = TYPE_METHODS (clas);
6366 method != NULL_TREE; method = TREE_CHAIN (method))
6367 {
6368 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6369 tree name = DECL_NAME (method);
6370 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6371 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6372 && method_sig == method_signature
6373 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6374 return method;
e04a16fb
AG
6375 }
6376 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6377 }
6378 return NULL_TREE;
6379}
6380
f441f671
APB
6381/* Return the line that matches DECL line number, and try its best to
6382 position the column number. Used during error reports. */
e04a16fb
AG
6383
6384static tree
6385lookup_cl (decl)
6386 tree decl;
6387{
6388 static tree cl = NULL_TREE;
f441f671 6389 char *line, *found;
e04a16fb
AG
6390
6391 if (!decl)
6392 return NULL_TREE;
6393
6394 if (cl == NULL_TREE)
6395 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6396
6397 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6398 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6399
f441f671
APB
6400 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6401 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6402
6403 found = strstr ((const char *)line,
6404 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6405 if (found)
6406 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6407
e04a16fb
AG
6408 return cl;
6409}
6410
6411/* Look for a simple name in the single-type import list */
6412
6413static tree
6414find_name_in_single_imports (name)
6415 tree name;
6416{
6417 tree node;
6418
6419 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6420 if (TREE_VALUE (node) == name)
6421 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6422
6423 return NULL_TREE;
6424}
6425
6426/* Process all single-type import. */
6427
6428static int
6429process_imports ()
6430{
6431 tree import;
6432 int error_found;
6433
6434 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6435 {
6436 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6437
6438 /* Don't load twice something already defined. */
6439 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6440 continue;
6441 QUALIFIED_P (to_be_found) = 1;
6442 load_class (to_be_found, 0);
6443 error_found =
6444 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6445 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6446 {
6447 parse_error_context (TREE_PURPOSE (import),
6448 "Class or interface `%s' not found in import",
6449 IDENTIFIER_POINTER (to_be_found));
6450 return 1;
6451 }
6452 if (error_found)
6453 return 1;
6454 }
6455 return 0;
6456}
6457
9a7ab4b3
APB
6458/* Possibly find and mark a class imported by a single-type import
6459 statement. */
e04a16fb 6460
9a7ab4b3 6461static void
e04a16fb
AG
6462find_in_imports (class_type)
6463 tree class_type;
6464{
6465 tree import;
6466
6467 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6468 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6469 {
6470 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6471 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6472 }
e04a16fb
AG
6473}
6474
e04a16fb 6475static int
63a212ed 6476note_possible_classname (name, len)
49f48c71 6477 const char *name;
63a212ed 6478 int len;
e04a16fb 6479{
63a212ed
PB
6480 tree node;
6481 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6482 len = len - 5;
6483 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6484 len = len - 6;
e04a16fb 6485 else
63a212ed
PB
6486 return 0;
6487 node = ident_subst (name, len, "", '/', '.', "");
6488 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6489 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6490 return 1;
e04a16fb
AG
6491}
6492
6493/* Read a import directory, gathering potential match for further type
6494 references. Indifferently reads a filesystem or a ZIP archive
6495 directory. */
6496
6497static void
6498read_import_dir (wfl)
6499 tree wfl;
6500{
63a212ed 6501 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6502 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6503 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6504 DIR *dirp = NULL;
d8fccff5 6505 JCF *saved_jcf = current_jcf;
e04a16fb 6506
63a212ed
PB
6507 int found = 0;
6508 int k;
6509 void *entry;
6510 struct buffer filename[1];
6511
6512
6513 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6514 return;
6515 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6516
6517 BUFFER_INIT (filename);
6518 buffer_grow (filename, package_length + 100);
6519
6520 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6521 {
49f48c71 6522 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6523 int entry_length = strlen (entry_name);
6524 if (jcf_path_is_zipfile (entry))
6525 {
6526 ZipFile *zipf;
6527 buffer_grow (filename, entry_length);
6528 memcpy (filename->data, entry_name, entry_length - 1);
6529 filename->data[entry_length-1] = '\0';
6530 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6531 if (zipf == NULL)
6532 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6533 else
6534 {
6535 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6536 BUFFER_RESET (filename);
6537 for (k = 0; k < package_length; k++)
6538 {
6539 char ch = package_name[k];
6540 *filename->ptr++ = ch == '.' ? '/' : ch;
6541 }
6542 *filename->ptr++ = '/';
6543
345137c7 6544 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6545 {
49f48c71 6546 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6547 int current_entry_len = zipd->filename_length;
6548
345137c7
TT
6549 if (current_entry_len >= BUFFER_LENGTH (filename)
6550 && strncmp (filename->data, current_entry,
6551 BUFFER_LENGTH (filename)) != 0)
63a212ed 6552 continue;
345137c7 6553 found |= note_possible_classname (current_entry,
63a212ed
PB
6554 current_entry_len);
6555 }
6556 }
6557 }
6558 else
6559 {
6560 BUFFER_RESET (filename);
6561 buffer_grow (filename, entry_length + package_length + 4);
6562 strcpy (filename->data, entry_name);
6563 filename->ptr = filename->data + entry_length;
6564 for (k = 0; k < package_length; k++)
6565 {
6566 char ch = package_name[k];
6567 *filename->ptr++ = ch == '.' ? '/' : ch;
6568 }
6569 *filename->ptr = '\0';
6570
6571 dirp = opendir (filename->data);
6572 if (dirp == NULL)
6573 continue;
6574 *filename->ptr++ = '/';
6575 for (;;)
6576 {
63a212ed 6577 int len;
49f48c71 6578 const char *d_name;
63a212ed
PB
6579 struct dirent *direntp = readdir (dirp);
6580 if (!direntp)
6581 break;
6582 d_name = direntp->d_name;
6583 len = strlen (direntp->d_name);
6584 buffer_grow (filename, len+1);
6585 strcpy (filename->ptr, d_name);
345137c7 6586 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6587 package_length+len+1);
6588 }
6589 if (dirp)
6590 closedir (dirp);
6591 }
6592 }
e04a16fb 6593
63a212ed 6594 free (filename->data);
e04a16fb 6595
63a212ed
PB
6596 /* Here we should have a unified way of retrieving an entry, to be
6597 indexed. */
6598 if (!found)
e04a16fb
AG
6599 {
6600 static int first = 1;
6601 if (first)
6602 {
781b0558 6603 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6604 java_error_count++;
6605 first = 0;
6606 }
6607 else
63a212ed
PB
6608 parse_error_context (wfl, "Package `%s' not found in import",
6609 package_name);
e04a16fb
AG
6610 current_jcf = saved_jcf;
6611 return;
6612 }
e04a16fb
AG
6613 current_jcf = saved_jcf;
6614}
6615
6616/* Possibly find a type in the import on demands specified
6617 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6618 entire list, to detected potential double definitions. */
6619
6620static int
6621find_in_imports_on_demand (class_type)
6622 tree class_type;
6623{
ab3a6dd6 6624 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6625 int seen_once = -1;
ab3a6dd6 6626 tree cl = NULL_TREE;
e04a16fb
AG
6627
6628 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6629 {
49f48c71 6630 const char *id_name;
e04a16fb
AG
6631 obstack_grow (&temporary_obstack,
6632 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6633 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6634 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6635 obstack_grow0 (&temporary_obstack,
6636 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6637 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6638 id_name = obstack_finish (&temporary_obstack);
6639
6640 node = maybe_get_identifier (id_name);
6641 if (node && IS_A_CLASSFILE_NAME (node))
6642 {
6643 if (seen_once < 0)
6644 {
6645 cl = TREE_PURPOSE (import);
6646 seen_once = 1;
6647 node_to_use = node;
6648 }
6649 else
6650 {
6651 seen_once++;
6652 parse_error_context
1e12ab9b
APB
6653 (TREE_PURPOSE (import),
6654 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6655 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6656 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6657 }
6658 }
6659 }
6660
6661 if (seen_once == 1)
6662 {
6663 /* Setup lineno so that it refers to the line of the import (in
6664 case we parse a class file and encounter errors */
6665 tree decl;
6666 int saved_lineno = lineno;
6667 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6668 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6669 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6670 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6671 /* If there is no DECL set for the class or if the class isn't
6672 loaded and not seen in source yet, the load */
6673 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6674 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6675 load_class (node_to_use, 0);
6676 lineno = saved_lineno;
6677 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6678 }
6679 else
6680 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6681}
6682
9a7ab4b3
APB
6683/* Add package NAME to the list of package encountered so far. To
6684 speed up class lookup in do_resolve_class, we make sure a
6685 particular package is added only once. */
6686
6687static void
6688register_package (name)
6689 tree name;
6690{
6691 static struct hash_table _pht, *pht = NULL;
6692
6693 if (!pht)
6694 {
6695 hash_table_init (&_pht, hash_newfunc,
6696 java_hash_hash_tree_node, java_hash_compare_tree_node);
6697 pht = &_pht;
6698 }
6699
6700 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6701 {
6702 package_list = chainon (package_list, build_tree_list (name, NULL));
6703 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6704 }
6705}
6706
5e942c50
APB
6707static tree
6708resolve_package (pkg, next)
6709 tree pkg, *next;
6710{
c2952b01 6711 tree current, acc;
5e942c50 6712 tree type_name = NULL_TREE;
49f48c71 6713 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6714
6715 /* The trick is to determine when the package name stops and were
6716 the name of something contained in the package starts. Then we
6717 return a fully qualified name of what we want to get. */
6718
6719 /* Do a quick search on well known package names */
6720 if (!strncmp (name, "java.lang.reflect", 17))
6721 {
6722 *next =
6723 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6724 type_name = lookup_package_type (name, 17);
6725 }
6726 else if (!strncmp (name, "java.lang", 9))
6727 {
6728 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6729 type_name = lookup_package_type (name, 9);
6730 }
5e942c50 6731
2c56429a
APB
6732 /* If we found something here, return */
6733 if (type_name)
6734 return type_name;
6735
6736 *next = EXPR_WFL_QUALIFICATION (pkg);
6737
6738 /* Try the current package. */
6739 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6740 IDENTIFIER_LENGTH (ctxp->package)))
6741 {
6742 type_name =
6743 lookup_package_type_and_set_next (name,
6744 IDENTIFIER_LENGTH (ctxp->package),
6745 next );
6746 if (type_name)
6747 return type_name;
6748 }
6749
6750 /* Search in imported package */
6751 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6752 {
6753 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6754 int len = IDENTIFIER_LENGTH (current_pkg_name);
6755 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6756 {
6757 tree left, dummy;
6758
6759 breakdown_qualified (&left, &dummy, current_pkg_name);
6760 len = IDENTIFIER_LENGTH (left);
6761 type_name = lookup_package_type_and_set_next (name, len, next);
6762 if (type_name)
6763 break;
6764 }
6765 }
6766
c2952b01
APB
6767 /* Try to progressively construct a type name */
6768 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6769 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6770 current; current = TREE_CHAIN (current))
6771 {
6772 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6773 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6774 {
6775 type_name = acc;
6b48deee
APB
6776 /* resolve_package should be used in a loop, hence we
6777 point at this one to naturally process the next one at
6778 the next iteration. */
6779 *next = current;
c2952b01
APB
6780 break;
6781 }
6782 }
2c56429a
APB
6783 return type_name;
6784}
6785
6786static tree
6787lookup_package_type_and_set_next (name, len, next)
49f48c71 6788 const char *name;
2c56429a
APB
6789 int len;
6790 tree *next;
6791{
49f48c71 6792 const char *ptr;
2c56429a
APB
6793 tree type_name = lookup_package_type (name, len);
6794
6795 if (!type_name)
6796 return NULL;
6797
6798 ptr = IDENTIFIER_POINTER (type_name);
6799 while (ptr && (ptr = strchr (ptr, '.')))
6800 {
6801 *next = TREE_CHAIN (*next);
6802 ptr++;
6803 }
5e942c50
APB
6804 return type_name;
6805}
6806
6807static tree
6808lookup_package_type (name, from)
49f48c71 6809 const char *name;
5e942c50
APB
6810 int from;
6811{
6812 char subname [128];
49f48c71 6813 const char *sub = &name[from+1];
5e942c50
APB
6814 while (*sub != '.' && *sub)
6815 sub++;
6816 strncpy (subname, name, sub-name);
6817 subname [sub-name] = '\0';
6818 return get_identifier (subname);
6819}
6820
cf1748bf 6821static void
4dbf4496
APB
6822check_inner_class_access (decl, enclosing_decl, cl)
6823 tree decl, enclosing_decl, cl;
cf1748bf 6824{
4dbf4496
APB
6825 int access = 0;
6826
cf1748bf 6827 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6828 as a result of processing a JDEP crafted by source_start_java_method
6829 for the purpose of patching its parm decl. But the error would
6830 have been already trapped when fixing the method's signature.
6831 DECL can also be NULL in case of earlier errors. */
6832 if (!decl || !cl)
cf1748bf
APB
6833 return;
6834
4dbf4496
APB
6835 /* We grant access to private and protected inner classes if the
6836 location from where we're trying to access DECL is an enclosing
6837 context for DECL or if both have a common enclosing context. */
6838 if (CLASS_PRIVATE (decl))
6839 access = 1;
6840 if (CLASS_PROTECTED (decl))
6841 access = 2;
6842 if (!access)
6843 return;
6844
6845 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6846 TREE_TYPE (decl))
6847 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6848 TREE_TYPE (decl)))
6849 return;
6850
6851 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6852 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6853 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6854 lang_printable_name (decl, 0));
6855}
6856
e04a16fb
AG
6857/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6858 access violations were found, 1 otherwise. */
6859
6860static int
6861check_pkg_class_access (class_name, cl)
6862 tree class_name;
6863 tree cl;
6864{
6865 tree type;
e04a16fb
AG
6866
6867 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6868 return 0;
6869
6870 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6871 return 0;
6872
6873 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6874 {
e28cd97b
APB
6875 /* Access to a private class within the same package is
6876 allowed. */
6877 tree l, r;
6878 breakdown_qualified (&l, &r, class_name);
6879 if (l == ctxp->package)
6880 return 0;
6881
e04a16fb 6882 parse_error_context
781b0558 6883 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6884 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6885 IDENTIFIER_POINTER (class_name));
6886 return 1;
6887 }
6888 return 0;
6889}
6890
6891/* Local variable declaration. */
6892
6893static void
6894declare_local_variables (modifier, type, vlist)
6895 int modifier;
6896 tree type;
6897 tree vlist;
6898{
c583dd46
APB
6899 tree decl, current, saved_type;
6900 tree type_wfl = NULL_TREE;
e04a16fb 6901 int must_chain = 0;
c2952b01 6902 int final_p = 0;
e04a16fb 6903
2aa11e97 6904 /* Push a new block if statements were seen between the last time we
e04a16fb 6905 pushed a block and now. Keep a cound of block to close */
f099f336 6906 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6907 {
f099f336 6908 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6909 tree b = enter_block ();
f099f336 6910 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6911 }
6912
6913 if (modifier)
6914 {
6915 int i;
6916 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6917 if (modifier == ACC_FINAL)
c2952b01 6918 final_p = 1;
c877974e
APB
6919 else
6920 {
6921 parse_error_context
6922 (ctxp->modifier_ctx [i],
6923 "Only `final' is allowed as a local variables modifier");
6924 return;
6925 }
e04a16fb
AG
6926 }
6927
c583dd46
APB
6928 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6929 hold the TYPE value if a new incomplete has to be created (as
6930 opposed to being found already existing and reused). */
6931 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6932
6933 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6934 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6935
6936 /* Go through all the declared variables */
6937 for (current = vlist, saved_type = type; current;
6938 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6939 {
c877974e 6940 tree other, real_type;
e04a16fb
AG
6941 tree wfl = TREE_PURPOSE (current);
6942 tree name = EXPR_WFL_NODE (wfl);
6943 tree init = TREE_VALUE (current);
e04a16fb 6944
c583dd46
APB
6945 /* Process NAME, as it may specify extra dimension(s) for it */
6946 type = build_array_from_name (type, type_wfl, name, &name);
6947
6948 /* Variable redefinition check */
6949 if ((other = lookup_name_in_blocks (name)))
6950 {
6951 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6952 DECL_SOURCE_LINE (other));
6953 continue;
6954 }
6955
6956 /* Type adjustment. We may have just readjusted TYPE because
6957 the variable specified more dimensions. Make sure we have
6958 a reference if we can and don't have one already. */
1886c9d8 6959 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6960
6961 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6962 /* Never layout this decl. This will be done when its scope
6963 will be entered */
c877974e 6964 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6965 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6966 BLOCK_CHAIN_DECL (decl);
6967
d4370213
APB
6968 /* If doing xreferencing, replace the line number with the WFL
6969 compound value */
6970 if (flag_emit_xref)
6971 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6972
e04a16fb
AG
6973 /* Don't try to use an INIT statement when an error was found */
6974 if (init && java_error_count)
6975 init = NULL_TREE;
c583dd46
APB
6976
6977 /* Add the initialization function to the current function's code */
6978 if (init)
e04a16fb 6979 {
c583dd46
APB
6980 /* Name might have been readjusted */
6981 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6982 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6983 java_method_add_stmt (current_function_decl,
6984 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6985 init));
6986 }
6987
6988 /* Setup dependency the type of the decl */
6989 if (must_chain)
6990 {
6991 jdep *dep;
6992 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6993 dep = CLASSD_LAST (ctxp->classd_list);
6994 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6995 }
6996 }
6997 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6998}
6999
7000/* Called during parsing. Build decls from argument list. */
7001
7002static void
7003source_start_java_method (fndecl)
7004 tree fndecl;
7005{
7006 tree tem;
7007 tree parm_decl;
7008 int i;
7009
79d13333
APB
7010 if (!fndecl)
7011 return;
7012
e04a16fb
AG
7013 current_function_decl = fndecl;
7014
7015 /* New scope for the function */
7016 enter_block ();
7017 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7018 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7019 {
7020 tree type = TREE_VALUE (tem);
7021 tree name = TREE_PURPOSE (tem);
7022
23a79c61
APB
7023 /* If type is incomplete. Create an incomplete decl and ask for
7024 the decl to be patched later */
e04a16fb
AG
7025 if (INCOMPLETE_TYPE_P (type))
7026 {
7027 jdep *jdep;
c877974e
APB
7028 tree real_type = GET_REAL_TYPE (type);
7029 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7030 type = obtain_incomplete_type (type);
e04a16fb
AG
7031 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7032 jdep = CLASSD_LAST (ctxp->classd_list);
7033 JDEP_MISC (jdep) = name;
7034 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7035 }
7036 else
7037 parm_decl = build_decl (PARM_DECL, name, type);
7038
c2952b01
APB
7039 /* Remember if a local variable was declared final (via its
7040 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7041 if (ARG_FINAL_P (tem))
7042 LOCAL_FINAL (parm_decl) = 1;
7043
e04a16fb
AG
7044 BLOCK_CHAIN_DECL (parm_decl);
7045 }
7046 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7047 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7048 nreverse (tem);
7049 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7050 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7051}
7052
22eed1e6
APB
7053/* Called during parsing. Creates an artificial method declaration. */
7054
7055static tree
7056create_artificial_method (class, flags, type, name, args)
7057 tree class;
7058 int flags;
7059 tree type, name, args;
7060{
22eed1e6
APB
7061 tree mdecl;
7062
c2952b01 7063 java_parser_context_save_global ();
22eed1e6
APB
7064 lineno = 0;
7065 mdecl = make_node (FUNCTION_TYPE);
7066 TREE_TYPE (mdecl) = type;
7067 TYPE_ARG_TYPES (mdecl) = args;
7068 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7069 java_parser_context_restore_global ();
22eed1e6
APB
7070 DECL_ARTIFICIAL (mdecl) = 1;
7071 return mdecl;
7072}
7073
7074/* Starts the body if an artifical method. */
7075
7076static void
7077start_artificial_method_body (mdecl)
7078 tree mdecl;
7079{
7080 DECL_SOURCE_LINE (mdecl) = 1;
7081 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7082 source_start_java_method (mdecl);
7083 enter_block ();
7084}
7085
7086static void
7087end_artificial_method_body (mdecl)
7088 tree mdecl;
7089{
7090 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7091 exit_block ();
7092}
7093
e04a16fb
AG
7094/* Called during expansion. Push decls formerly built from argument
7095 list so they're usable during expansion. */
7096
7097static void
7098expand_start_java_method (fndecl)
7099 tree fndecl;
7100{
7101 tree tem, *ptr;
e04a16fb 7102
e04a16fb
AG
7103 current_function_decl = fndecl;
7104
c2952b01
APB
7105 if (! quiet_flag)
7106 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7107 announce_function (fndecl);
c2952b01
APB
7108 if (! quiet_flag)
7109 fprintf (stderr, "]");
7110
7111 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7112 ptr = &DECL_ARGUMENTS (fndecl);
7113 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7114 while (tem)
7115 {
7116 tree next = TREE_CHAIN (tem);
b67d701b 7117 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7118 if (PROMOTE_PROTOTYPES
7119 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7120 && INTEGRAL_TYPE_P (type))
7121 type = integer_type_node;
b67d701b 7122 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7123 layout_decl (tem, 0);
7124 pushdecl (tem);
e04a16fb
AG
7125 *ptr = tem;
7126 ptr = &TREE_CHAIN (tem);
7127 tem = next;
7128 }
7129 *ptr = NULL_TREE;
7130 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7131 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7132}
7133
7134/* Terminate a function and expand its body. */
7135
7136static void
7137source_end_java_method ()
7138{
7139 tree fndecl = current_function_decl;
138657ec 7140 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7141
79d13333
APB
7142 if (!fndecl)
7143 return;
7144
e04a16fb
AG
7145 java_parser_context_save_global ();
7146 lineno = ctxp->last_ccb_indent1;
7147
b67d701b
PB
7148 /* Set EH language codes */
7149 java_set_exception_lang_code ();
7150
5423609c
APB
7151 /* Turn function bodies with only a NOP expr null, so they don't get
7152 generated at all and we won't get warnings when using the -W
7153 -Wall flags. */
7154 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7155 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7156
e04a16fb
AG
7157 /* Generate function's code */
7158 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7159 && ! flag_emit_class_files
7160 && ! flag_emit_xref)
e04a16fb
AG
7161 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7162
7163 /* pop out of its parameters */
7164 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7165 poplevel (1, 0, 1);
7166 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7167
7168 /* Generate rtl for function exit. */
e8fc7396 7169 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7170 {
7171 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7172 /* Emit catch-finally clauses */
7173 emit_handlers ();
e04a16fb
AG
7174 expand_function_end (input_filename, lineno, 0);
7175
138657ec
AH
7176 /* FIXME: If the current method contains any exception handlers,
7177 force asynchronous_exceptions: this is necessary because signal
7178 handlers in libjava may throw exceptions. This is far from being
7179 a perfect solution, but it's better than doing nothing at all.*/
7180 if (catch_clauses)
7181 asynchronous_exceptions = 1;
7182
e04a16fb
AG
7183 /* Run the optimizers and output assembler code for this function. */
7184 rest_of_compilation (fndecl);
7185 }
7186
7187 current_function_decl = NULL_TREE;
8226320b 7188 permanent_allocation (1);
e04a16fb 7189 java_parser_context_restore_global ();
138657ec 7190 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7191}
7192
7193/* Record EXPR in the current function block. Complements compound
7194 expression second operand if necessary. */
7195
7196tree
7197java_method_add_stmt (fndecl, expr)
7198 tree fndecl, expr;
7199{
b771925e
APB
7200 if (!GET_CURRENT_BLOCK (fndecl))
7201 return NULL_TREE;
f099f336 7202 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7203}
e04a16fb 7204
b67d701b
PB
7205static tree
7206add_stmt_to_block (b, type, stmt)
7207 tree b, type, stmt;
7208{
7209 tree body = BLOCK_EXPR_BODY (b), c;
7210
e04a16fb
AG
7211 if (java_error_count)
7212 return body;
b67d701b
PB
7213
7214 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7215 return body;
7216
b67d701b
PB
7217 BLOCK_EXPR_BODY (b) = c;
7218 TREE_SIDE_EFFECTS (c) = 1;
7219 return c;
e04a16fb
AG
7220}
7221
7222/* Add STMT to EXISTING if possible, otherwise create a new
7223 COMPOUND_EXPR and add STMT to it. */
7224
7225static tree
7226add_stmt_to_compound (existing, type, stmt)
7227 tree existing, type, stmt;
7228{
15fdcfe9
PB
7229 if (existing)
7230 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7231 else
15fdcfe9 7232 return stmt;
e04a16fb
AG
7233}
7234
7235/* Hold THIS for the scope of the current public method decl. */
7236static tree current_this;
7237
1886c9d8
APB
7238void java_layout_seen_class_methods ()
7239{
7240 tree previous_list = all_class_list;
7241 tree end = NULL_TREE;
7242 tree current;
7243
7244 while (1)
7245 {
7246 for (current = previous_list;
7247 current != end; current = TREE_CHAIN (current))
7248 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7249
7250 if (previous_list != all_class_list)
7251 {
7252 end = previous_list;
7253 previous_list = all_class_list;
7254 }
7255 else
7256 break;
7257 }
7258}
7259
e04a16fb 7260void
c2952b01 7261java_reorder_fields ()
e04a16fb 7262{
c2952b01 7263 static tree stop_reordering = NULL_TREE;
23a79c61 7264
c2952b01 7265 tree current;
5e942c50 7266 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7267 {
5e942c50 7268 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7269
c2952b01
APB
7270 if (current_class == stop_reordering)
7271 break;
7272
c877974e
APB
7273 /* Reverse the fields, but leave the dummy field in front.
7274 Fields are already ordered for Object and Class */
7275 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7276 && current_class != class_type_node)
7277 {
23a79c61
APB
7278 /* If the dummy field is there, reverse the right fields and
7279 just layout the type for proper fields offset */
c877974e
APB
7280 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7281 {
7282 tree fields = TYPE_FIELDS (current_class);
7283 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7284 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7285 }
23a79c61
APB
7286 /* We don't have a dummy field, we need to layout the class,
7287 after having reversed the fields */
c877974e
APB
7288 else
7289 {
7290 TYPE_FIELDS (current_class) =
7291 nreverse (TYPE_FIELDS (current_class));
7292 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7293 }
7294 }
c2952b01
APB
7295 }
7296 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7297}
7298
7299/* Layout the methods of all classes loaded in one way on an
7300 other. Check methods of source parsed classes. Then reorder the
7301 fields and layout the classes or the type of all source parsed
7302 classes */
7303
7304void
7305java_layout_classes ()
7306{
7307 tree current;
7308 int save_error_count = java_error_count;
7309
7310 /* Layout the methods of all classes seen so far */
7311 java_layout_seen_class_methods ();
7312 java_parse_abort_on_error ();
7313 all_class_list = NULL_TREE;
7314
7315 /* Then check the methods of all parsed classes */
7316 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7317 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7318 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7319 java_parse_abort_on_error ();
7320
7321 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7322 {
7323 current_class = TREE_TYPE (TREE_VALUE (current));
7324 layout_class (current_class);
5e942c50 7325
c877974e
APB
7326 /* From now on, the class is considered completely loaded */
7327 CLASS_LOADED_P (current_class) = 1;
7328
5e942c50
APB
7329 /* Error reported by the caller */
7330 if (java_error_count)
7331 return;
e04a16fb 7332 }
23a79c61
APB
7333
7334 /* We might have reloaded classes durign the process of laying out
7335 classes for code generation. We must layout the methods of those
7336 late additions, as constructor checks might use them */
1886c9d8 7337 java_layout_seen_class_methods ();
23a79c61 7338 java_parse_abort_on_error ();
e04a16fb
AG
7339}
7340
c2952b01
APB
7341/* Expand methods in the current set of classes rememebered for
7342 generation. */
e04a16fb 7343
49f48c71 7344static void
c2952b01 7345java_complete_expand_classes ()
e04a16fb
AG
7346{
7347 tree current;
ce6e9147
APB
7348
7349 do_not_fold = flag_emit_xref;
c2952b01 7350
e04a16fb 7351 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7352 if (!INNER_CLASS_DECL_P (current))
7353 java_complete_expand_class (current);
7354}
e04a16fb 7355
c2952b01
APB
7356/* Expand the methods found in OUTER, starting first by OUTER's inner
7357 classes, if any. */
e04a16fb 7358
c2952b01
APB
7359static void
7360java_complete_expand_class (outer)
7361 tree outer;
7362{
7363 tree inner_list;
e04a16fb 7364
c2952b01 7365 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7366
c2952b01
APB
7367 /* We need to go after all inner classes and start expanding them,
7368 starting with most nested ones. We have to do that because nested
7369 classes might add functions to outer classes */
e04a16fb 7370
c2952b01
APB
7371 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7372 inner_list; inner_list = TREE_CHAIN (inner_list))
7373 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7374
c2952b01
APB
7375 java_complete_expand_methods (outer);
7376 set_nested_class_simple_name_value (outer, 0); /* Reset */
7377}
7378
7379/* Expand methods registered in CLASS_DECL. The general idea is that
7380 we expand regular methods first. This allows us get an estimate on
7381 how outer context local alias fields are really used so we can add
7382 to the constructor just enough code to initialize them properly (it
7383 also lets us generate $finit$ correctly.) Then we expand the
7384 constructors and then <clinit>. */
7385
7386static void
7387java_complete_expand_methods (class_decl)
7388 tree class_decl;
7389{
7390 tree clinit, finit, decl, first_decl;
7391
7392 current_class = TREE_TYPE (class_decl);
7393
7394 /* Initialize a new constant pool */
7395 init_outgoing_cpool ();
7396
7397 /* Pre-expand <clinit> to figure whether we really need it or
7398 not. If we do need it, we pre-expand the static fields so they're
7399 ready to be used somewhere else. <clinit> will be fully expanded
7400 after we processed the constructors. */
7401 first_decl = TYPE_METHODS (current_class);
7402 clinit = maybe_generate_pre_expand_clinit (current_class);
7403
7404 /* Then generate $finit$ (if we need to) because constructor will
7405 try to use it.*/
7406 if (TYPE_FINIT_STMT_LIST (current_class))
7407 {
7408 finit = generate_finit (current_class);
7409 java_complete_expand_method (finit);
7410 }
7411
7412 /* Now do the constructors */
7413 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7414 {
7415 int no_body;
7416
7417 if (!DECL_CONSTRUCTOR_P (decl))
7418 continue;
7419
7420 no_body = !DECL_FUNCTION_BODY (decl);
7421 /* Don't generate debug info on line zero when expanding a
7422 generated constructor. */
7423 if (no_body)
7424 restore_line_number_status (1);
7425
7426 java_complete_expand_method (decl);
7427
7428 if (no_body)
7429 restore_line_number_status (0);
7430 }
7431
7432 /* First, do the ordinary methods. */
7433 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7434 {
7145d9fe
TT
7435 /* Skip abstract or native methods -- but do handle native
7436 methods when generating JNI stubs. */
7437 if (METHOD_ABSTRACT (decl)
7438 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7439 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7440 continue;
7145d9fe
TT
7441
7442 if (METHOD_NATIVE (decl))
7443 {
7444 tree body = build_jni_stub (decl);
7445 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7446 }
7447
c2952b01
APB
7448 java_complete_expand_method (decl);
7449 }
7450
7451 /* If there is indeed a <clinit>, fully expand it now */
7452 if (clinit)
7453 {
7454 /* Prevent the use of `this' inside <clinit> */
7455 ctxp->explicit_constructor_p = 1;
7456 java_complete_expand_method (clinit);
7457 ctxp->explicit_constructor_p = 0;
e04a16fb 7458 }
c2952b01 7459
165f37bc
APB
7460 /* We might have generated a class$ that we now want to expand */
7461 if (TYPE_DOT_CLASS (current_class))
7462 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7463
c2952b01
APB
7464 /* Now verify constructor circularity (stop after the first one we
7465 prove wrong.) */
7466 if (!CLASS_INTERFACE (class_decl))
7467 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7468 if (DECL_CONSTRUCTOR_P (decl)
7469 && verify_constructor_circularity (decl, decl))
7470 break;
7471
7472 /* Save the constant pool. We'll need to restore it later. */
7473 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7474}
7475
b9f7e36c
APB
7476/* Hold a list of catch clauses list. The first element of this list is
7477 the list of the catch clauses of the currently analysed try block. */
7478static tree currently_caught_type_list;
7479
c2952b01
APB
7480/* Attempt to create <clinit>. Pre-expand static fields so they can be
7481 safely used in some other methods/constructors. */
e920ebc9 7482
c2952b01
APB
7483static tree
7484maybe_generate_pre_expand_clinit (class_type)
7485 tree class_type;
e920ebc9 7486{
c2952b01
APB
7487 tree current, mdecl;
7488
7489 if (!TYPE_CLINIT_STMT_LIST (class_type))
7490 return NULL_TREE;
e920ebc9 7491
c2952b01
APB
7492 /* Go through all static fields and pre expand them */
7493 for (current = TYPE_FIELDS (class_type); current;
7494 current = TREE_CHAIN (current))
7495 if (FIELD_STATIC (current))
7496 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7497
7498 /* Then build the <clinit> method */
7499 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7500 clinit_identifier_node, end_params_node);
7501 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7502 mdecl, NULL_TREE);
7503 start_artificial_method_body (mdecl);
7504
7505 /* We process the list of assignment we produced as the result of
7506 the declaration of initialized static field and add them as
7507 statement to the <clinit> method. */
7508 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7509 current = TREE_CHAIN (current))
e920ebc9 7510 {
9a7ab4b3 7511 tree stmt = current;
c2952b01
APB
7512 /* We build the assignment expression that will initialize the
7513 field to its value. There are strict rules on static
7514 initializers (8.5). FIXME */
98a52c2c 7515 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7516 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7517 java_method_add_stmt (mdecl, stmt);
7518 }
e920ebc9 7519
c2952b01
APB
7520 end_artificial_method_body (mdecl);
7521
92d83515
APB
7522 /* Now we want to place <clinit> as the last method (because we need
7523 it at least for interface so that it doesn't interfere with the
7524 dispatch table based lookup. */
7525 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7526 {
92d83515
APB
7527 current = TREE_CHAIN (TYPE_METHODS (class_type));
7528 TYPE_METHODS (class_type) = current;
c2952b01
APB
7529
7530 while (TREE_CHAIN (current))
7531 current = TREE_CHAIN (current);
92d83515 7532
c2952b01
APB
7533 TREE_CHAIN (current) = mdecl;
7534 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7535 }
c2952b01
APB
7536
7537 return mdecl;
e920ebc9
APB
7538}
7539
92d83515
APB
7540/* See whether we could get rid of <clinit>. Criteria are: all static
7541 final fields have constant initial values and the body of <clinit>
7542 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7543
7544static int
7545maybe_yank_clinit (mdecl)
7546 tree mdecl;
7547{
7548 tree type, current;
7549 tree fbody, bbody;
7550
7551 if (!DECL_CLINIT_P (mdecl))
7552 return 0;
f0f3a777
APB
7553
7554 /* If the body isn't empty, then we keep <clinit>. Note that if
7555 we're emitting classfiles, this isn't enough not to rule it
7556 out. */
92d83515
APB
7557 fbody = DECL_FUNCTION_BODY (mdecl);
7558 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7559 bbody = BLOCK_EXPR_BODY (bbody);
f0f3a777 7560 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7561 return 0;
7562
7563 type = DECL_CONTEXT (mdecl);
7564 current = TYPE_FIELDS (type);
7565
7566 for (current = (current ? TREE_CHAIN (current) : current);
7567 current; current = TREE_CHAIN (current))
f0f3a777
APB
7568 {
7569 tree f_init;
7570
7571 /* We're not interested in non static field */
7572 if (!FIELD_STATIC (current))
7573 continue;
7574
7575 /* Anything that isn't String or a basic type is ruled out -- or
7576 if we now how to deal with it (when doing things natively) we
7577 should generated an empty <clinit> so that SUID are computed
7578 correctly. */
7579 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7580 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7581 break;
7582
7583 f_init = DECL_INITIAL (current);
7584 /* If we're emitting native code, we want static final fields to
7585 have constant initializers. If we don't meet these
7586 conditions, we keep <clinit> */
7587 if (!flag_emit_class_files
7588 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7589 break;
7590 /* If we're emitting bytecode, we want static fields to have
7591 constant initializers or no initializer. If we don't meet
7592 these conditions, we keep <clinit> */
7593 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7594 break;
7595 }
92d83515
APB
7596
7597 if (current)
7598 return 0;
7599
7600 /* Get rid of <clinit> in the class' list of methods */
7601 if (TYPE_METHODS (type) == mdecl)
7602 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7603 else
7604 for (current = TYPE_METHODS (type); current;
7605 current = TREE_CHAIN (current))
7606 if (TREE_CHAIN (current) == mdecl)
7607 {
7608 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7609 break;
7610 }
7611
7612 return 1;
7613}
7614
7615
e04a16fb
AG
7616/* Complete and expand a method. */
7617
7618static void
7619java_complete_expand_method (mdecl)
7620 tree mdecl;
7621{
92d83515
APB
7622 int yank_clinit = 0;
7623
c2952b01 7624 current_function_decl = mdecl;
22eed1e6
APB
7625 /* Fix constructors before expanding them */
7626 if (DECL_CONSTRUCTOR_P (mdecl))
7627 fix_constructors (mdecl);
e04a16fb 7628
22eed1e6 7629 /* Expand functions that have a body */
e04a16fb
AG
7630 if (DECL_FUNCTION_BODY (mdecl))
7631 {
9bbc7d9f
PB
7632 tree fbody = DECL_FUNCTION_BODY (mdecl);
7633 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7634 tree exception_copy = NULL_TREE;
e04a16fb 7635 expand_start_java_method (mdecl);
939d7216 7636 build_result_decl (mdecl);
e04a16fb
AG
7637
7638 current_this
7639 = (!METHOD_STATIC (mdecl) ?
7640 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7641
ce6e9147
APB
7642 /* Purge the `throws' list of unchecked exceptions. If we're
7643 doing xref, save a copy of the list and re-install it
7644 later. */
7645 if (flag_emit_xref)
7646 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7647
b9f7e36c
APB
7648 purge_unchecked_exceptions (mdecl);
7649
7650 /* Install exceptions thrown with `throws' */
7651 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7652
9bbc7d9f 7653 if (block_body != NULL_TREE)
bc3ca41b
PB
7654 {
7655 block_body = java_complete_tree (block_body);
c2952b01 7656
7145d9fe 7657 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7658 check_for_initialization (block_body);
f099f336 7659 ctxp->explicit_constructor_p = 0;
bc3ca41b 7660 }
e803d3b2 7661
9bbc7d9f 7662 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7663
c2952b01
APB
7664 /* If we saw a return but couldn't evaluate it properly, we'll
7665 have an error_mark_node here. */
7666 if (block_body != error_mark_node
7667 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7668 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7669 && !flag_emit_xref)
82371d41 7670 missing_return_error (current_function_decl);
7525cc04 7671
92d83515
APB
7672 /* Check wether we could just get rid of clinit, now the picture
7673 is complete. */
7674 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7675 complete_start_java_method (mdecl);
7676
e04a16fb 7677 /* Don't go any further if we've found error(s) during the
92d83515
APB
7678 expansion */
7679 if (!java_error_count && !yank_clinit)
e04a16fb 7680 source_end_java_method ();
22eed1e6
APB
7681 else
7682 {
92d83515
APB
7683 if (java_error_count)
7684 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7685 poplevel (1, 0, 1);
7686 }
b9f7e36c
APB
7687
7688 /* Pop the exceptions and sanity check */
7689 POP_EXCEPTIONS();
7690 if (currently_caught_type_list)
7691 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7692
7693 if (flag_emit_xref)
7694 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7695 }
7696}
7697
c2952b01
APB
7698\f
7699
7700/* This section of the code deals with accessing enclosing context
7701 fields either directly by using the relevant access to this$<n> or
7702 by invoking an access method crafted for that purpose. */
7703
7704/* Build the necessary access from an inner class to an outer
7705 class. This routine could be optimized to cache previous result
7706 (decl, current_class and returned access). When an access method
7707 needs to be generated, it always takes the form of a read. It might
7708 be later turned into a write by calling outer_field_access_fix. */
7709
7710static tree
7711build_outer_field_access (id, decl)
7712 tree id, decl;
7713{
7714 tree access = NULL_TREE;
7715 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7716
7717 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7718 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7719 the `private' barrier if we're not emitting bytecodes. */
7720 if (ctx == DECL_CONTEXT (decl)
7721 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7722 {
7723 tree thisn = build_current_thisn (current_class);
7724 access = make_qualified_primary (build_wfl_node (thisn),
7725 id, EXPR_WFL_LINECOL (id));
7726 }
7727 /* Otherwise, generate access methods to outer this and access the
7728 field (either using an access method or by direct access.) */
7729 else
7730 {
7731 int lc = EXPR_WFL_LINECOL (id);
7732
7733 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7734 get a hold to the enclosing instance we need, and then we
c2952b01
APB
7735 build the field access. */
7736 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7737
7738 /* If the field is private and we're generating bytecode, then
7739 we generate an access method */
7740 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7741 {
7742 tree name = build_outer_field_access_methods (decl);
7743 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7744 name, access, NULL_TREE);
7745 }
7746 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7747 Once again we break the `private' access rule from a foreign
7748 class. */
7749 else
7750 access = make_qualified_primary (access, id, lc);
7751 }
7752 return resolve_expression_name (access, NULL);
7753}
7754
7755/* Return a non zero value if NODE describes an outer field inner
7756 access. */
7757
7758static int
7759outer_field_access_p (type, decl)
7760 tree type, decl;
7761{
7762 if (!INNER_CLASS_TYPE_P (type)
7763 || TREE_CODE (decl) != FIELD_DECL
7764 || DECL_CONTEXT (decl) == type)
7765 return 0;
7766
7767 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7768 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7769 {
7770 if (type == DECL_CONTEXT (decl))
7771 return 1;
7772 if (!DECL_CONTEXT (TYPE_NAME (type)))
7773 break;
7774 }
7775
7776 return 0;
7777}
7778
7779/* Return a non zero value if NODE represents an outer field inner
7780 access that was been already expanded. As a side effect, it returns
7781 the name of the field being accessed and the argument passed to the
7782 access function, suitable for a regeneration of the access method
7783 call if necessary. */
7784
7785static int
7786outer_field_expanded_access_p (node, name, arg_type, arg)
7787 tree node, *name, *arg_type, *arg;
7788{
7789 int identified = 0;
7790
7791 if (TREE_CODE (node) != CALL_EXPR)
7792 return 0;
7793
7794 /* Well, gcj generates slightly different tree nodes when compiling
7795 to native or bytecodes. It's the case for function calls. */
7796
7797 if (flag_emit_class_files
7798 && TREE_CODE (node) == CALL_EXPR
7799 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7800 identified = 1;
7801 else if (!flag_emit_class_files)
7802 {
7803 node = TREE_OPERAND (node, 0);
7804
7805 if (node && TREE_OPERAND (node, 0)
7806 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7807 {
7808 node = TREE_OPERAND (node, 0);
7809 if (TREE_OPERAND (node, 0)
7810 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7811 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7812 (DECL_NAME (TREE_OPERAND (node, 0)))))
7813 identified = 1;
7814 }
7815 }
7816
7817 if (identified && name && arg_type && arg)
7818 {
7819 tree argument = TREE_OPERAND (node, 1);
7820 *name = DECL_NAME (TREE_OPERAND (node, 0));
7821 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7822 *arg = TREE_VALUE (argument);
7823 }
7824 return identified;
7825}
7826
7827/* Detect in NODE an outer field read access from an inner class and
7828 transform it into a write with RHS as an argument. This function is
7829 called from the java_complete_lhs when an assignment to a LHS can
7830 be identified. */
7831
7832static tree
7833outer_field_access_fix (wfl, node, rhs)
7834 tree wfl, node, rhs;
7835{
7836 tree name, arg_type, arg;
7837
7838 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7839 {
7840 /* At any rate, check whether we're trying to assign a value to
7841 a final. */
7842 tree accessed = (JDECL_P (node) ? node :
7843 (TREE_CODE (node) == COMPONENT_REF ?
7844 TREE_OPERAND (node, 1) : node));
7845 if (check_final_assignment (accessed, wfl))
7846 return error_mark_node;
7847
7848 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7849 arg_type, name, arg, rhs);
7850 return java_complete_tree (node);
7851 }
7852 return NULL_TREE;
7853}
7854
7855/* Construct the expression that calls an access method:
7856 <type>.access$<n>(<arg1> [, <arg2>]);
7857
7858 ARG2 can be NULL and will be omitted in that case. It will denote a
7859 read access. */
7860
7861static tree
7862build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7863 int lc;
7864 tree type, access_method_name, arg1, arg2;
7865{
7866 tree args, cn, access;
7867
7868 args = arg1 ? arg1 :
7869 build_wfl_node (build_current_thisn (current_class));
7870 args = build_tree_list (NULL_TREE, args);
7871
7872 if (arg2)
7873 args = tree_cons (NULL_TREE, arg2, args);
7874
7875 access = build_method_invocation (build_wfl_node (access_method_name), args);
7876 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7877 return make_qualified_primary (cn, access, lc);
7878}
7879
7880static tree
7881build_new_access_id ()
7882{
7883 static int access_n_counter = 1;
7884 char buffer [128];
7885
7886 sprintf (buffer, "access$%d", access_n_counter++);
7887 return get_identifier (buffer);
7888}
7889
7890/* Create the static access functions for the outer field DECL. We define a
7891 read:
7892 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7893 return inst$.field;
7894 }
7895 and a write access:
7896 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7897 TREE_TYPE (<field>) value$) {
7898 return inst$.field = value$;
7899 }
7900 We should have a usage flags on the DECL so we can lazily turn the ones
7901 we're using for code generation. FIXME.
7902*/
7903
7904static tree
7905build_outer_field_access_methods (decl)
7906 tree decl;
7907{
7908 tree id, args, stmt, mdecl;
7909
7910 /* Check point, to be removed. FIXME */
7911 if (FIELD_INNER_ACCESS (decl)
7912 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7913 abort ();
7914
7915 if (FIELD_INNER_ACCESS (decl))
7916 return FIELD_INNER_ACCESS (decl);
7917
7918 push_obstacks (&permanent_obstack, &permanent_obstack);
7919
7920 /* Create the identifier and a function named after it. */
7921 id = build_new_access_id ();
7922
7923 /* The identifier is marked as bearing the name of a generated write
7924 access function for outer field accessed from inner classes. */
7925 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7926
7927 /* Create the read access */
7928 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7929 TREE_CHAIN (args) = end_params_node;
7930 stmt = make_qualified_primary (build_wfl_node (inst_id),
7931 build_wfl_node (DECL_NAME (decl)), 0);
7932 stmt = build_return (0, stmt);
7933 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7934 TREE_TYPE (decl), id, args, stmt);
7935 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7936
7937 /* Create the write access method */
7938 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7939 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7940 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7941 stmt = make_qualified_primary (build_wfl_node (inst_id),
7942 build_wfl_node (DECL_NAME (decl)), 0);
7943 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7944 build_wfl_node (wpv_id)));
7945
7946 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7947 TREE_TYPE (decl), id, args, stmt);
7948 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7949 pop_obstacks ();
7950
7951 /* Return the access name */
7952 return FIELD_INNER_ACCESS (decl) = id;
7953}
7954
7955/* Build an field access method NAME. */
7956
7957static tree
7958build_outer_field_access_method (class, type, name, args, body)
7959 tree class, type, name, args, body;
7960{
7961 tree saved_current_function_decl, mdecl;
7962
7963 /* Create the method */
7964 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7965 fix_method_argument_names (args, mdecl);
7966 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7967
7968 /* Attach the method body. */
7969 saved_current_function_decl = current_function_decl;
7970 start_artificial_method_body (mdecl);
7971 java_method_add_stmt (mdecl, body);
7972 end_artificial_method_body (mdecl);
7973 current_function_decl = saved_current_function_decl;
7974
7975 return mdecl;
7976}
7977
7978\f
7979/* This section deals with building access function necessary for
7980 certain kinds of method invocation from inner classes. */
7981
7982static tree
7983build_outer_method_access_method (decl)
7984 tree decl;
7985{
7986 tree saved_current_function_decl, mdecl;
7987 tree args = NULL_TREE, call_args = NULL_TREE;
7988 tree carg, id, body, class;
7989 char buffer [80];
7990 int parm_id_count = 0;
7991
7992 /* Test this abort with an access to a private field */
7993 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7994 abort ();
7995
7996 /* Check the cache first */
7997 if (DECL_FUNCTION_INNER_ACCESS (decl))
7998 return DECL_FUNCTION_INNER_ACCESS (decl);
7999
8000 class = DECL_CONTEXT (decl);
8001
8002 /* Obtain an access identifier and mark it */
8003 id = build_new_access_id ();
8004 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8005
8006 push_obstacks (&permanent_obstack, &permanent_obstack);
8007
8008 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8009 /* Create the arguments, as much as the original */
8010 for (; carg && carg != end_params_node;
8011 carg = TREE_CHAIN (carg))
8012 {
8013 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8014 args = chainon (args, build_tree_list (get_identifier (buffer),
8015 TREE_VALUE (carg)));
8016 }
8017 args = chainon (args, end_params_node);
8018
8019 /* Create the method */
8020 mdecl = create_artificial_method (class, ACC_STATIC,
8021 TREE_TYPE (TREE_TYPE (decl)), id, args);
8022 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8023 /* There is a potential bug here. We should be able to use
8024 fix_method_argument_names, but then arg names get mixed up and
8025 eventually a constructor will have its this$0 altered and the
8026 outer context won't be assignment properly. The test case is
8027 stub.java FIXME */
8028 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8029
8030 /* Attach the method body. */
8031 saved_current_function_decl = current_function_decl;
8032 start_artificial_method_body (mdecl);
8033
8034 /* The actual method invocation uses the same args. When invoking a
8035 static methods that way, we don't want to skip the first
8036 argument. */
8037 carg = args;
8038 if (!METHOD_STATIC (decl))
8039 carg = TREE_CHAIN (carg);
8040 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8041 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8042 call_args);
8043
8044 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8045 call_args);
8046 if (!METHOD_STATIC (decl))
8047 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8048 body, 0);
8049 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8050 body = build_return (0, body);
8051 java_method_add_stmt (mdecl,body);
8052 end_artificial_method_body (mdecl);
8053 current_function_decl = saved_current_function_decl;
8054 pop_obstacks ();
8055
8056 /* Back tag the access function so it know what it accesses */
8057 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8058
8059 /* Tag the current method so it knows it has an access generated */
8060 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8061}
8062
8063\f
8064/* This section of the code deals with building expressions to access
8065 the enclosing instance of an inner class. The enclosing instance is
8066 kept in a generated field called this$<n>, with <n> being the
8067 inner class nesting level (starting from 0.) */
8068
8069/* Build an access to a given this$<n>, possibly by chaining access
8070 call to others. Access methods to this$<n> are build on the fly if
8071 necessary */
8072
8073static tree
8074build_access_to_thisn (from, to, lc)
8075 tree from, to;
8076 int lc;
8077{
8078 tree access = NULL_TREE;
8079
8080 while (from != to)
8081 {
8082 tree access0_wfl, cn;
8083
8084 maybe_build_thisn_access_method (from);
8085 access0_wfl = build_wfl_node (access0_identifier_node);
8086 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8087 EXPR_WFL_LINECOL (access0_wfl) = lc;
8088
8089 if (!access)
8090 {
8091 access = build_current_thisn (current_class);
8092 access = build_wfl_node (access);
8093 }
8094 access = build_tree_list (NULL_TREE, access);
8095 access = build_method_invocation (access0_wfl, access);
8096 access = make_qualified_primary (cn, access, lc);
8097
8098 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8099 }
8100 return access;
8101}
8102
8103/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8104 is returned if nothing needs to be generated. Otherwise, the method
152de068 8105 generated and a method decl is returned.
c2952b01
APB
8106
8107 NOTE: These generated methods should be declared in a class file
8108 attribute so that they can't be referred to directly. */
8109
8110static tree
8111maybe_build_thisn_access_method (type)
8112 tree type;
8113{
8114 tree mdecl, args, stmt, rtype;
8115 tree saved_current_function_decl;
8116
8117 /* If TYPE is a top-level class, no access method is required.
8118 If there already is such an access method, bail out. */
8119 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8120 return NULL_TREE;
8121
8122 /* We generate the method. The method looks like:
8123 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8124 */
8125 push_obstacks (&permanent_obstack, &permanent_obstack);
8126 args = build_tree_list (inst_id, build_pointer_type (type));
8127 TREE_CHAIN (args) = end_params_node;
8128 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8129 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8130 access0_identifier_node, args);
8131 fix_method_argument_names (args, mdecl);
8132 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8133 stmt = build_current_thisn (type);
8134 stmt = make_qualified_primary (build_wfl_node (inst_id),
8135 build_wfl_node (stmt), 0);
8136 stmt = build_return (0, stmt);
8137
8138 saved_current_function_decl = current_function_decl;
8139 start_artificial_method_body (mdecl);
8140 java_method_add_stmt (mdecl, stmt);
8141 end_artificial_method_body (mdecl);
8142 current_function_decl = saved_current_function_decl;
8143 pop_obstacks ();
8144
8145 CLASS_ACCESS0_GENERATED_P (type) = 1;
8146
8147 return mdecl;
8148}
8149
8150/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8151 the first level of innerclassing. this$1 for the next one, etc...
8152 This function can be invoked with TYPE to NULL, available and then
8153 has to count the parser context. */
8154
8155static tree
8156build_current_thisn (type)
8157 tree type;
8158{
8159 static int saved_i = -1;
8160 static tree saved_thisn = NULL_TREE;
8161
8162 tree decl;
8163 char buffer [80];
8164 int i = 0;
8165
8166 if (type)
8167 {
8168 static tree saved_type = NULL_TREE;
8169 static int saved_type_i = 0;
8170
8171 if (type == saved_type)
8172 i = saved_type_i;
8173 else
8174 {
8175 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8176 decl; decl = DECL_CONTEXT (decl), i++)
8177 ;
8178
8179 saved_type = type;
8180 saved_type_i = i;
8181 }
8182 }
8183 else
8184 i = list_length (GET_CPC_LIST ())-2;
8185
8186 if (i == saved_i)
8187 return saved_thisn;
8188
8189 sprintf (buffer, "this$%d", i);
8190 saved_i = i;
8191 saved_thisn = get_identifier (buffer);
8192 return saved_thisn;
8193}
8194
8195/* Return the assignement to the hidden enclosing context `this$<n>'
8196 by the second incoming parameter to the innerclass constructor. The
8197 form used is `this.this$<n> = this$<n>;'. */
8198
8199static tree
8200build_thisn_assign ()
8201{
8202 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8203 {
8204 tree thisn = build_current_thisn (current_class);
8205 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8206 build_wfl_node (thisn), 0);
8207 tree rhs = build_wfl_node (thisn);
8208 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8209 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8210 }
8211 return NULL_TREE;
8212}
8213
8214\f
165f37bc
APB
8215/* Building the synthetic `class$' used to implement the `.class' 1.1
8216 extension for non primitive types. This method looks like:
8217
8218 static Class class$(String type) throws NoClassDefFoundError
8219 {
8220 try {return (java.lang.Class.forName (String));}
8221 catch (ClassNotFoundException e) {
8222 throw new NoClassDefFoundError(e.getMessage());}
8223 } */
8224
8225static tree
8226build_dot_class_method (class)
8227 tree class;
8228{
8229#define BWF(S) build_wfl_node (get_identifier ((S)))
8230#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8231 tree args, tmp, saved_current_function_decl, mdecl;
8232 tree stmt, throw_stmt, catch, catch_block, try_block;
8233 tree catch_clause_param;
8234 tree class_not_found_exception, no_class_def_found_error;
8235
8236 static tree get_message_wfl, type_parm_wfl;
8237
8238 if (!get_message_wfl)
8239 {
8240 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8241 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8242 }
8243
8244 /* Build the arguments */
8245 args = build_tree_list (get_identifier ("type$"),
8246 build_pointer_type (string_type_node));
8247 TREE_CHAIN (args) = end_params_node;
8248
8249 /* Build the qualified name java.lang.Class.forName */
8250 tmp = MQN (MQN (MQN (BWF ("java"),
8251 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8252
8253 /* For things we have to catch and throw */
8254 class_not_found_exception =
8255 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8256 no_class_def_found_error =
8257 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8258 load_class (class_not_found_exception, 1);
8259 load_class (no_class_def_found_error, 1);
8260
8261 /* Create the "class$" function */
8262 mdecl = create_artificial_method (class, ACC_STATIC,
8263 build_pointer_type (class_type_node),
8264 get_identifier ("class$"), args);
8265 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8266 no_class_def_found_error);
8267
8268 /* We start by building the try block. We need to build:
8269 return (java.lang.Class.forName (type)); */
8270 stmt = build_method_invocation (tmp,
8271 build_tree_list (NULL_TREE, type_parm_wfl));
8272 stmt = build_return (0, stmt);
8273 /* Put it in a block. That's the try block */
8274 try_block = build_expr_block (stmt, NULL_TREE);
8275
8276 /* Now onto the catch block. We start by building the expression
8277 throwing a new exception:
8278 throw new NoClassDefFoundError (_.getMessage); */
8279 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8280 get_message_wfl, 0);
8281 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8282
8283 /* Build new NoClassDefFoundError (_.getMessage) */
8284 throw_stmt = build_new_invocation
8285 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8286 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8287
8288 /* Build the throw, (it's too early to use BUILD_THROW) */
8289 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8290
8291 /* Build the catch block to encapsulate all this. We begin by
8292 building an decl for the catch clause parameter and link it to
8293 newly created block, the catch block. */
8294 catch_clause_param =
8295 build_decl (VAR_DECL, wpv_id,
8296 build_pointer_type (class_not_found_exception));
8297 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8298
8299 /* We initialize the variable with the exception handler. */
8300 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8301 soft_exceptioninfo_call_node);
8302 add_stmt_to_block (catch_block, NULL_TREE, catch);
8303
8304 /* We add the statement throwing the new exception */
8305 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8306
8307 /* Build a catch expression for all this */
8308 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8309
8310 /* Build the try/catch sequence */
8311 stmt = build_try_statement (0, try_block, catch_block);
8312
8313 fix_method_argument_names (args, mdecl);
8314 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8315 saved_current_function_decl = current_function_decl;
8316 start_artificial_method_body (mdecl);
8317 java_method_add_stmt (mdecl, stmt);
8318 end_artificial_method_body (mdecl);
8319 current_function_decl = saved_current_function_decl;
8320 TYPE_DOT_CLASS (class) = mdecl;
8321
8322 return mdecl;
8323}
8324
8325static tree
f0f3a777
APB
8326build_dot_class_method_invocation (type)
8327 tree type;
165f37bc 8328{
f0f3a777
APB
8329 tree sig_id, s;
8330
8331 if (TYPE_ARRAY_P (type))
8332 sig_id = build_java_signature (type);
8333 else
8334 sig_id = DECL_NAME (TYPE_NAME (type));
8335
8336 s = make_node (STRING_CST);
8337 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (sig_id);
165f37bc
APB
8338 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8339 TREE_STRING_LENGTH (s)+1);
f0f3a777 8340 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8341 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8342 build_tree_list (NULL_TREE, s));
8343}
8344
c2952b01
APB
8345/* This section of the code deals with constructor. */
8346
22eed1e6
APB
8347/* Craft a body for default constructor. Patch existing constructor
8348 bodies with call to super() and field initialization statements if
8349 necessary. */
8350
8351static void
8352fix_constructors (mdecl)
8353 tree mdecl;
8354{
8355 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8356 tree thisn_assign, compound = NULL_TREE;
8357 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8358
22eed1e6
APB
8359 if (!body)
8360 {
22eed1e6
APB
8361 /* It is an error for the compiler to generate a default
8362 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8363 takes no argument, or the same args for an anonymous class */
8364 if (verify_constructor_super (mdecl))
22eed1e6 8365 {
c2952b01
APB
8366 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8367 tree save = DECL_NAME (mdecl);
49f48c71 8368 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8369 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8370 parse_error_context
c2952b01
APB
8371 (lookup_cl (TYPE_NAME (class_type)),
8372 "No constructor matching `%s' found in class `%s'",
8373 lang_printable_name (mdecl, 0), n);
8374 DECL_NAME (mdecl) = save;
22eed1e6
APB
8375 }
8376
c2952b01
APB
8377 /* The constructor body must be crafted by hand. It's the
8378 constructor we defined when we realize we didn't have the
8379 CLASSNAME() constructor */
22eed1e6
APB
8380 start_artificial_method_body (mdecl);
8381
f0f3a777
APB
8382 /* Insert an assignment to the this$<n> hidden field, if
8383 necessary */
8384 if ((thisn_assign = build_thisn_assign ()))
8385 java_method_add_stmt (mdecl, thisn_assign);
8386
22eed1e6
APB
8387 /* We don't generate a super constructor invocation if we're
8388 compiling java.lang.Object. build_super_invocation takes care
8389 of that. */
e920ebc9 8390 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8391
c2952b01
APB
8392 /* Insert the instance initializer block right here, after the
8393 super invocation. */
8394 add_instance_initializer (mdecl);
8395
22eed1e6
APB
8396 end_artificial_method_body (mdecl);
8397 }
8398 /* Search for an explicit constructor invocation */
8399 else
8400 {
8401 int found = 0;
8402 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8403
8404 while (body)
8405 switch (TREE_CODE (body))
8406 {
8407 case CALL_EXPR:
8408 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8409 body = NULL_TREE;
8410 break;
8411 case COMPOUND_EXPR:
8412 case EXPR_WITH_FILE_LOCATION:
8413 body = TREE_OPERAND (body, 0);
8414 break;
8415 case BLOCK:
8416 body = BLOCK_EXPR_BODY (body);
8417 break;
8418 default:
8419 found = 0;
8420 body = NULL_TREE;
8421 }
8422 /* The constructor is missing an invocation of super() */
8423 if (!found)
8424 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8425 build_super_invocation (mdecl));
22eed1e6 8426
c2952b01
APB
8427 /* Generate the assignment to this$<n>, if necessary */
8428 if ((thisn_assign = build_thisn_assign ()))
8429 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8430
f0f3a777
APB
8431 /* Insert the instance initializer block right here, after the
8432 super invocation. */
8433 add_instance_initializer (mdecl);
8434
22eed1e6
APB
8435 /* Fix the constructor main block if we're adding extra stmts */
8436 if (compound)
8437 {
8438 compound = add_stmt_to_compound (compound, NULL_TREE,
8439 BLOCK_EXPR_BODY (main_block));
8440 BLOCK_EXPR_BODY (main_block) = compound;
8441 }
8442 }
8443}
8444
8445/* Browse constructors in the super class, searching for a constructor
8446 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8447 otherwise. If the current class is an anonymous inner class, look
8448 for something that has the same signature. */
22eed1e6
APB
8449
8450static int
c2952b01
APB
8451verify_constructor_super (mdecl)
8452 tree mdecl;
22eed1e6
APB
8453{
8454 tree class = CLASSTYPE_SUPER (current_class);
152de068 8455 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8456 tree sdecl;
8457
22eed1e6
APB
8458 if (!class)
8459 return 0;
8460
c2952b01 8461 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8462 {
c2952b01
APB
8463 tree mdecl_arg_type;
8464 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8465 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8466 if (DECL_CONSTRUCTOR_P (sdecl))
8467 {
cf1b2274 8468 tree m_arg_type;
152de068
APB
8469 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8470 if (super_inner)
8471 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8472 for (m_arg_type = mdecl_arg_type;
8473 (arg_type != end_params_node
8474 && m_arg_type != end_params_node);
c2952b01 8475 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8476 m_arg_type = TREE_CHAIN (m_arg_type))
8477 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8478 break;
8479
cf1b2274 8480 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8481 return 0;
8482 }
8483 }
8484 else
8485 {
8486 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8487 {
152de068
APB
8488 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8489 if (super_inner)
8490 arg = TREE_CHAIN (arg);
8491 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8492 return 0;
8493 }
8494 }
8495 return 1;
8496}
8497
22eed1e6 8498/* Generate code for all context remembered for code generation. */
b351b287
APB
8499
8500void
8501java_expand_classes ()
8502{
5423609c 8503 int save_error_count = 0;
c2952b01
APB
8504 static struct parser_ctxt *saved_ctxp = NULL;
8505
23a79c61
APB
8506 java_parse_abort_on_error ();
8507 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8508 return;
8509 java_layout_classes ();
8510 java_parse_abort_on_error ();
8511
c2952b01 8512 saved_ctxp = ctxp_for_generation;
b351b287
APB
8513 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8514 {
8515 ctxp = ctxp_for_generation;
8516 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8517 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8518 java_parse_abort_on_error ();
8519 }
c2952b01
APB
8520
8521 /* Find anonymous classes and expand their constructor, now they
8522 have been fixed. */
8523 for (ctxp_for_generation = saved_ctxp;
8524 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8525 {
8526 tree current;
8527 ctxp = ctxp_for_generation;
8528 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8529 {
8530 current_class = TREE_TYPE (current);
8531 if (ANONYMOUS_CLASS_P (current_class))
8532 {
8533 tree d;
8534 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8535 {
8536 if (DECL_CONSTRUCTOR_P (d))
8537 {
8538 restore_line_number_status (1);
8539 reset_method_name (d);
8540 java_complete_expand_method (d);
8541 restore_line_number_status (0);
8542 break; /* We now there are no other ones */
8543 }
8544 }
8545 }
8546 }
8547 }
8548
8549 /* If we've found error at that stage, don't try to generate
8550 anything, unless we're emitting xrefs or checking the syntax only
8551 (but not using -fsyntax-only for the purpose of generating
8552 bytecode. */
8553 if (java_error_count && !flag_emit_xref
8554 && (!flag_syntax_only && !flag_emit_class_files))
8555 return;
8556
8557 /* Now things are stable, go for generation of the class data. */
8558 for (ctxp_for_generation = saved_ctxp;
8559 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8560 {
8561 tree current;
8562 ctxp = ctxp_for_generation;
8563 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8564 {
8565 current_class = TREE_TYPE (current);
8566 outgoing_cpool = TYPE_CPOOL (current_class);
8567 if (flag_emit_class_files)
8568 write_classfile (current_class);
8569 if (flag_emit_xref)
8570 expand_xref (current_class);
8571 else if (! flag_syntax_only)
8572 finish_class ();
8573 }
8574 }
b351b287
APB
8575}
8576
e04a16fb
AG
8577/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8578 a tree list node containing RIGHT. Fore coming RIGHTs will be
8579 chained to this hook. LOCATION contains the location of the
8580 separating `.' operator. */
8581
8582static tree
8583make_qualified_primary (primary, right, location)
8584 tree primary, right;
8585 int location;
8586{
8587 tree wfl;
8588
c2952b01 8589 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8590 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8591 else
8592 {
8593 wfl = primary;
c2952b01
APB
8594 /* If wfl wasn't qualified, we build a first anchor */
8595 if (!EXPR_WFL_QUALIFICATION (wfl))
8596 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8597 }
8598
c2952b01 8599 /* And chain them */
e04a16fb
AG
8600 EXPR_WFL_LINECOL (right) = location;
8601 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8602 PRIMARY_P (wfl) = 1;
8603 return wfl;
8604}
8605
8606/* Simple merge of two name separated by a `.' */
8607
8608static tree
8609merge_qualified_name (left, right)
8610 tree left, right;
8611{
8612 tree node;
c2952b01
APB
8613 if (!left && !right)
8614 return NULL_TREE;
8615
8616 if (!left)
8617 return right;
8618
8619 if (!right)
8620 return left;
8621
e04a16fb
AG
8622 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8623 IDENTIFIER_LENGTH (left));
8624 obstack_1grow (&temporary_obstack, '.');
8625 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8626 IDENTIFIER_LENGTH (right));
8627 node = get_identifier (obstack_base (&temporary_obstack));
8628 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8629 QUALIFIED_P (node) = 1;
8630 return node;
8631}
8632
8633/* Merge the two parts of a qualified name into LEFT. Set the
8634 location information of the resulting node to LOCATION, usually
8635 inherited from the location information of the `.' operator. */
8636
8637static tree
8638make_qualified_name (left, right, location)
8639 tree left, right;
8640 int location;
8641{
bc3ca41b
PB
8642#ifdef USE_COMPONENT_REF
8643 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8644 EXPR_WFL_LINECOL (node) = location;
8645 return node;
8646#else
e04a16fb
AG
8647 tree left_id = EXPR_WFL_NODE (left);
8648 tree right_id = EXPR_WFL_NODE (right);
8649 tree wfl, merge;
8650
8651 merge = merge_qualified_name (left_id, right_id);
8652
8653 /* Left wasn't qualified and is now qualified */
8654 if (!QUALIFIED_P (left_id))
8655 {
8656 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8657 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8658 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8659 }
8660
8661 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8662 EXPR_WFL_LINECOL (wfl) = location;
8663 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8664
8665 EXPR_WFL_NODE (left) = merge;
8666 return left;
bc3ca41b 8667#endif
e04a16fb
AG
8668}
8669
8670/* Extract the last identifier component of the qualified in WFL. The
8671 last identifier is removed from the linked list */
8672
8673static tree
8674cut_identifier_in_qualified (wfl)
8675 tree wfl;
8676{
8677 tree q;
8678 tree previous = NULL_TREE;
8679 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8680 if (!TREE_CHAIN (q))
8681 {
8682 if (!previous)
781b0558 8683 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8684 TREE_CHAIN (previous) = NULL_TREE;
8685 return TREE_PURPOSE (q);
8686 }
8687}
8688
8689/* Resolve the expression name NAME. Return its decl. */
8690
8691static tree
5e942c50 8692resolve_expression_name (id, orig)
e04a16fb 8693 tree id;
5e942c50 8694 tree *orig;
e04a16fb
AG
8695{
8696 tree name = EXPR_WFL_NODE (id);
8697 tree decl;
8698
8699 /* 6.5.5.1: Simple expression names */
8700 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8701 {
8702 /* 15.13.1: NAME can appear within the scope of a local variable
8703 declaration */
8704 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8705 return decl;
8706
8707 /* 15.13.1: NAME can appear within a class declaration */
8708 else
8709 {
8710 decl = lookup_field_wrapper (current_class, name);
8711 if (decl)
8712 {
c2952b01 8713 tree access = NULL_TREE;
e04a16fb 8714 int fs = FIELD_STATIC (decl);
f2760b27
APB
8715
8716 /* If we're accessing an outer scope local alias, make
8717 sure we change the name of the field we're going to
8718 build access to. */
8719 if (FIELD_LOCAL_ALIAS_USED (decl))
8720 name = DECL_NAME (decl);
8721
e04a16fb
AG
8722 /* Instance variable (8.3.1.1) can't appear within
8723 static method, static initializer or initializer for
8724 a static variable. */
8725 if (!fs && METHOD_STATIC (current_function_decl))
8726 {
7f10c2e2 8727 static_ref_err (id, name, current_class);
e04a16fb
AG
8728 return error_mark_node;
8729 }
22eed1e6
APB
8730 /* Instance variables can't appear as an argument of
8731 an explicit constructor invocation */
8732 if (!fs && ctxp->explicit_constructor_p)
8733 {
8734 parse_error_context
781b0558 8735 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8736 return error_mark_node;
8737 }
5e942c50 8738
c2952b01
APB
8739 /* If we're processing an inner class and we're trying
8740 to access a field belonging to an outer class, build
8741 the access to the field */
8742 if (!fs && outer_field_access_p (current_class, decl))
8743 return build_outer_field_access (id, decl);
8744
5e942c50 8745 /* Otherwise build what it takes to access the field */
c2952b01
APB
8746 access = build_field_ref ((fs ? NULL_TREE : current_this),
8747 DECL_CONTEXT (decl), name);
e8fc7396 8748 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8749 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8750 /* We may be asked to save the real field access node */
8751 if (orig)
c2952b01 8752 *orig = access;
5e942c50 8753 /* And we return what we got */
c2952b01 8754 return access;
e04a16fb
AG
8755 }
8756 /* Fall down to error report on undefined variable */
8757 }
8758 }
8759 /* 6.5.5.2 Qualified Expression Names */
8760 else
8761 {
5e942c50
APB
8762 if (orig)
8763 *orig = NULL_TREE;
e04a16fb
AG
8764 qualify_ambiguous_name (id);
8765 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8766 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8767 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8768 }
8769
8770 /* We've got an error here */
8771 parse_error_context (id, "Undefined variable `%s'",
8772 IDENTIFIER_POINTER (name));
8773
8774 return error_mark_node;
8775}
8776
7f10c2e2
APB
8777static void
8778static_ref_err (wfl, field_id, class_type)
8779 tree wfl, field_id, class_type;
8780{
8781 parse_error_context
8782 (wfl,
8783 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8784 IDENTIFIER_POINTER (field_id),
8785 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8786}
8787
e04a16fb
AG
8788/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8789 We return something suitable to generate the field access. We also
8790 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8791 recipient's address can be null. */
8792
8793static tree
8794resolve_field_access (qual_wfl, field_decl, field_type)
8795 tree qual_wfl;
8796 tree *field_decl, *field_type;
8797{
8798 int is_static = 0;
8799 tree field_ref;
8800 tree decl, where_found, type_found;
8801
8802 if (resolve_qualified_expression_name (qual_wfl, &decl,
8803 &where_found, &type_found))
8804 return error_mark_node;
8805
8806 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8807 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8808 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8809 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8810 {
8811 tree length = build_java_array_length_access (where_found);
8812 field_ref =
8813 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8814
8815 /* In case we're dealing with a static array, we need to
8816 initialize its class before the array length can be fetched.
8817 It's also a good time to create a DECL_RTL for the field if
8818 none already exists, otherwise if the field was declared in a
8819 class found in an external file and hasn't been (and won't
8820 be) accessed for its value, none will be created. */
8821 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8822 {
8823 build_static_field_ref (where_found);
8824 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8825 }
e04a16fb
AG
8826 }
8827 /* We might have been trying to resolve field.method(). In which
8828 case, the resolution is over and decl is the answer */
34f4db93 8829 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8830 field_ref = decl;
34f4db93 8831 else if (JDECL_P (decl))
e04a16fb 8832 {
5e942c50
APB
8833 int static_final_found = 0;
8834 if (!type_found)
8835 type_found = DECL_CONTEXT (decl);
34f4db93 8836 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8837 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8838 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8839 && DECL_INITIAL (decl))
5e942c50 8840 {
0c2b8145
APB
8841 /* When called on a FIELD_DECL of the right (primitive)
8842 type, java_complete_tree will try to substitue the decl
8843 for it's initial value. */
70541f45 8844 field_ref = java_complete_tree (decl);
5e942c50
APB
8845 static_final_found = 1;
8846 }
8847 else
7f10c2e2
APB
8848 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8849 NULL_TREE : where_found),
5e942c50 8850 type_found, DECL_NAME (decl));
e04a16fb
AG
8851 if (field_ref == error_mark_node)
8852 return error_mark_node;
e8fc7396
APB
8853 if (is_static && !static_final_found
8854 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8855 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8856 }
8857 else
8858 field_ref = decl;
8859
8860 if (field_decl)
8861 *field_decl = decl;
8862 if (field_type)
c877974e
APB
8863 *field_type = (QUAL_DECL_TYPE (decl) ?
8864 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8865 return field_ref;
8866}
8867
e28cd97b
APB
8868/* If NODE is an access to f static field, strip out the class
8869 initialization part and return the field decl, otherwise, return
8870 NODE. */
8871
8872static tree
8873strip_out_static_field_access_decl (node)
8874 tree node;
8875{
8876 if (TREE_CODE (node) == COMPOUND_EXPR)
8877 {
8878 tree op1 = TREE_OPERAND (node, 1);
8879 if (TREE_CODE (op1) == COMPOUND_EXPR)
8880 {
8881 tree call = TREE_OPERAND (op1, 0);
8882 if (TREE_CODE (call) == CALL_EXPR
8883 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8884 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8885 == soft_initclass_node)
8886 return TREE_OPERAND (op1, 1);
8887 }
2f11d407
TT
8888 else if (JDECL_P (op1))
8889 return op1;
e28cd97b
APB
8890 }
8891 return node;
8892}
8893
e04a16fb
AG
8894/* 6.5.5.2: Qualified Expression Names */
8895
8896static int
8897resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8898 tree wfl;
8899 tree *found_decl, *type_found, *where_found;
8900{
8901 int from_type = 0; /* Field search initiated from a type */
c2952b01 8902 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8903 int previous_call_static = 0;
8904 int is_static;
8905 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8906 /* For certain for of inner class instantiation */
8907 tree saved_current, saved_this;
8908#define RESTORE_THIS_AND_CURRENT_CLASS \
8909 { current_class = saved_current; current_this = saved_this;}
8910
c877974e 8911 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8912
8913 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8914 {
8915 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8916 tree ret_decl; /* for EH checking */
8917 int location; /* for EH checking */
e04a16fb
AG
8918
8919 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8920 switch (TREE_CODE (qual_wfl))
8921 {
8922 case CALL_EXPR:
b67d701b 8923 case NEW_CLASS_EXPR:
e04a16fb
AG
8924 /* If the access to the function call is a non static field,
8925 build the code to access it. */
34f4db93 8926 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8927 {
ac825856
APB
8928 decl = maybe_access_field (decl, *where_found,
8929 DECL_CONTEXT (decl));
e04a16fb
AG
8930 if (decl == error_mark_node)
8931 return 1;
8932 }
c2952b01 8933
e04a16fb
AG
8934 /* And code for the function call */
8935 if (complete_function_arguments (qual_wfl))
8936 return 1;
c2952b01
APB
8937
8938 /* We might have to setup a new current class and a new this
8939 for the search of an inner class, relative to the type of
8940 a expression resolved as `decl'. The current values are
8941 saved and restored shortly after */
8942 saved_current = current_class;
8943 saved_this = current_this;
8944 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8945 {
8946 current_class = type;
8947 current_this = decl;
8948 }
8949
89e09b9a
PB
8950 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8951 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8952 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8953 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8954 *where_found = patch_method_invocation (qual_wfl, decl, type,
8955 &is_static, &ret_decl);
e04a16fb 8956 if (*where_found == error_mark_node)
c2952b01
APB
8957 {
8958 RESTORE_THIS_AND_CURRENT_CLASS;
8959 return 1;
8960 }
e04a16fb
AG
8961 *type_found = type = QUAL_DECL_TYPE (*where_found);
8962
c2952b01
APB
8963 /* If we're creating an inner class instance, check for that
8964 an enclosing instance is in scope */
8965 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8966 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8967 {
8968 parse_error_context
165f37bc
APB
8969 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8970 lang_printable_name (type, 0),
8971 (!current_this ? "" :
8972 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8973 RESTORE_THIS_AND_CURRENT_CLASS;
8974 return 1;
8975 }
8976
8977 /* In case we had to change then to resolve a inner class
8978 instantiation using a primary qualified by a `new' */
8979 RESTORE_THIS_AND_CURRENT_CLASS;
8980
34d4df06
APB
8981 /* EH check. No check on access$<n> functions */
8982 if (location
8983 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
8984 (DECL_NAME (current_function_decl)))
7705e9db
APB
8985 check_thrown_exceptions (location, ret_decl);
8986
e04a16fb
AG
8987 /* If the previous call was static and this one is too,
8988 build a compound expression to hold the two (because in
8989 that case, previous function calls aren't transported as
8990 forcoming function's argument. */
8991 if (previous_call_static && is_static)
8992 {
8993 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8994 TREE_SIDE_EFFECTS (decl) = 1;
8995 }
8996 else
8997 {
8998 previous_call_static = is_static;
8999 decl = *where_found;
9000 }
c2952b01 9001 from_type = 0;
e04a16fb
AG
9002 continue;
9003
d8fccff5 9004 case NEW_ARRAY_EXPR:
c2952b01 9005 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9006 *where_found = decl = java_complete_tree (qual_wfl);
9007 if (decl == error_mark_node)
9008 return 1;
9009 *type_found = type = QUAL_DECL_TYPE (decl);
9010 CLASS_LOADED_P (type) = 1;
9011 continue;
9012
e04a16fb
AG
9013 case CONVERT_EXPR:
9014 *where_found = decl = java_complete_tree (qual_wfl);
9015 if (decl == error_mark_node)
9016 return 1;
9017 *type_found = type = QUAL_DECL_TYPE (decl);
9018 from_cast = 1;
9019 continue;
9020
22eed1e6 9021 case CONDITIONAL_EXPR:
5e942c50 9022 case STRING_CST:
ac22f9cb 9023 case MODIFY_EXPR:
22eed1e6
APB
9024 *where_found = decl = java_complete_tree (qual_wfl);
9025 if (decl == error_mark_node)
9026 return 1;
9027 *type_found = type = QUAL_DECL_TYPE (decl);
9028 continue;
9029
e04a16fb
AG
9030 case ARRAY_REF:
9031 /* If the access to the function call is a non static field,
9032 build the code to access it. */
34f4db93 9033 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9034 {
9035 decl = maybe_access_field (decl, *where_found, type);
9036 if (decl == error_mark_node)
9037 return 1;
9038 }
9039 /* And code for the array reference expression */
9040 decl = java_complete_tree (qual_wfl);
9041 if (decl == error_mark_node)
9042 return 1;
9043 type = QUAL_DECL_TYPE (decl);
9044 continue;
0a2138e2 9045
37feda7d
APB
9046 case PLUS_EXPR:
9047 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9048 return 1;
9049 if ((type = patch_string (decl)))
9050 decl = type;
9051 *where_found = QUAL_RESOLUTION (q) = decl;
9052 *type_found = type = TREE_TYPE (decl);
9053 break;
9054
165f37bc
APB
9055 case CLASS_LITERAL:
9056 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9057 return 1;
9058 *where_found = QUAL_RESOLUTION (q) = decl;
9059 *type_found = type = TREE_TYPE (decl);
9060 break;
9061
0a2138e2
APB
9062 default:
9063 /* Fix for -Wall Just go to the next statement. Don't
9064 continue */
a3f406ce 9065 break;
e04a16fb
AG
9066 }
9067
9068 /* If we fall here, we weren't processing a (static) function call. */
9069 previous_call_static = 0;
9070
9071 /* It can be the keyword THIS */
9072 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9073 {
9074 if (!current_this)
9075 {
9076 parse_error_context
9077 (wfl, "Keyword `this' used outside allowed context");
9078 return 1;
9079 }
f63991a8
APB
9080 if (ctxp->explicit_constructor_p)
9081 {
781b0558 9082 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9083 return 1;
9084 }
e04a16fb 9085 /* We have to generate code for intermediate acess */
c2952b01
APB
9086 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9087 {
9088 *where_found = decl = current_this;
9089 *type_found = type = QUAL_DECL_TYPE (decl);
9090 }
4dbf4496
APB
9091 /* We're trying to access the this from somewhere else. Make sure
9092 it's allowed before doing so. */
c2952b01
APB
9093 else
9094 {
4dbf4496
APB
9095 if (!enclosing_context_p (type, current_class))
9096 {
9097 char *p = xstrdup (lang_printable_name (type, 0));
9098 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9099 p, p,
9100 lang_printable_name (current_class, 0));
9101 free (p);
9102 return 1;
9103 }
c2952b01
APB
9104 *where_found = decl = build_current_thisn (type);
9105 from_qualified_this = 1;
9106 }
9107
9108 from_type = 0;
e04a16fb
AG
9109 continue;
9110 }
9111
9112 /* 15.10.2 Accessing Superclass Members using SUPER */
9113 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9114 {
9115 tree node;
9116 /* Check on the restricted use of SUPER */
9117 if (METHOD_STATIC (current_function_decl)
9118 || current_class == object_type_node)
9119 {
9120 parse_error_context
9121 (wfl, "Keyword `super' used outside allowed context");
9122 return 1;
9123 }
9124 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9125 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9126 CLASSTYPE_SUPER (current_class),
9127 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9128 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9129 if (decl == error_mark_node)
9130 return 1;
e04a16fb
AG
9131 *type_found = type = QUAL_DECL_TYPE (decl);
9132 from_super = from_type = 1;
9133 continue;
9134 }
9135
9136 /* 15.13.1: Can't search for field name in packages, so we
9137 assume a variable/class name was meant. */
9138 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9139 {
5e942c50
APB
9140 tree name = resolve_package (wfl, &q);
9141 if (name)
9142 {
c2952b01 9143 tree list;
5e942c50 9144 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9145 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9146 out. We're going to search something inside it. */
9147 *type_found = type = TREE_TYPE (decl);
9148 layout_class (type);
9149 from_type = 1;
c2952b01 9150
dde1da72
APB
9151 /* Fix them all the way down, if any are left. */
9152 if (q)
c2952b01 9153 {
dde1da72
APB
9154 list = TREE_CHAIN (q);
9155 while (list)
9156 {
9157 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9158 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9159 list = TREE_CHAIN (list);
9160 }
c2952b01 9161 }
5e942c50 9162 }
e04a16fb 9163 else
5e942c50
APB
9164 {
9165 if (from_super || from_cast)
9166 parse_error_context
9167 ((from_cast ? qual_wfl : wfl),
9168 "No variable `%s' defined in class `%s'",
9169 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9170 lang_printable_name (type, 0));
9171 else
9172 parse_error_context
9173 (qual_wfl, "Undefined variable or class name: `%s'",
9174 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9175 return 1;
9176 }
e04a16fb
AG
9177 }
9178
9179 /* We have a type name. It's been already resolved when the
9180 expression was qualified. */
9181 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9182 {
9183 if (!(decl = QUAL_RESOLUTION (q)))
9184 return 1; /* Error reported already */
9185
c2952b01
APB
9186 /* Sneak preview. If next we see a `new', we're facing a
9187 qualification with resulted in a type being selected
9188 instead of a field. Report the error */
9189 if(TREE_CHAIN (q)
9190 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9191 {
9192 parse_error_context (qual_wfl, "Undefined variable `%s'",
9193 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9194 return 1;
9195 }
9196
e04a16fb
AG
9197 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9198 {
9199 parse_error_context
9200 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9201 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9202 GET_TYPE_NAME (type),
e04a16fb
AG
9203 IDENTIFIER_POINTER (DECL_NAME (decl)),
9204 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9205 return 1;
9206 }
5e942c50 9207 check_deprecation (qual_wfl, decl);
c2952b01 9208
e04a16fb
AG
9209 type = TREE_TYPE (decl);
9210 from_type = 1;
9211 }
9212 /* We resolve and expression name */
9213 else
9214 {
cd531a2e 9215 tree field_decl = NULL_TREE;
e04a16fb
AG
9216
9217 /* If there exists an early resolution, use it. That occurs
9218 only once and we know that there are more things to
9219 come. Don't do that when processing something after SUPER
9220 (we need more thing to be put in place below */
9221 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9222 {
9223 decl = QUAL_RESOLUTION (q);
c877974e 9224 if (!type)
5e942c50 9225 {
7f10c2e2
APB
9226 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9227 {
9228 if (current_this)
9229 *where_found = current_this;
9230 else
9231 {
9232 static_ref_err (qual_wfl, DECL_NAME (decl),
9233 current_class);
9234 return 1;
9235 }
f0f3a777
APB
9236 if (outer_field_access_p (current_class, decl))
9237 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9238 }
c877974e
APB
9239 else
9240 {
9241 *where_found = TREE_TYPE (decl);
9242 if (TREE_CODE (*where_found) == POINTER_TYPE)
9243 *where_found = TREE_TYPE (*where_found);
9244 }
5e942c50 9245 }
b67d701b 9246 }
e04a16fb
AG
9247
9248 /* We have to search for a field, knowing the type of its
9249 container. The flag FROM_TYPE indicates that we resolved
9250 the last member of the expression as a type name, which
5e942c50
APB
9251 means that for the resolution of this field, we'll look
9252 for other errors than if it was resolved as a member of
9253 an other field. */
e04a16fb
AG
9254 else
9255 {
9256 int is_static;
5e942c50
APB
9257 tree field_decl_type; /* For layout */
9258
e04a16fb
AG
9259 if (!from_type && !JREFERENCE_TYPE_P (type))
9260 {
9261 parse_error_context
9262 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9263 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9264 lang_printable_name (type, 0),
e04a16fb
AG
9265 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9266 return 1;
9267 }
9268
dc0b3eff
PB
9269 field_decl = lookup_field_wrapper (type,
9270 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9271
9272 /* Maybe what we're trying to access an inner class. */
9273 if (!field_decl)
9274 {
9275 tree ptr, inner_decl;
9276
9277 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9278 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9279 if (inner_decl)
9280 {
9281 check_inner_class_access (inner_decl, decl, qual_wfl);
9282 type = TREE_TYPE (inner_decl);
9283 decl = inner_decl;
9284 from_type = 1;
9285 continue;
9286 }
9287 }
9288
dc0b3eff 9289 if (field_decl == NULL_TREE)
e04a16fb
AG
9290 {
9291 parse_error_context
2aa11e97 9292 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9293 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9294 GET_TYPE_NAME (type));
e04a16fb
AG
9295 return 1;
9296 }
dc0b3eff
PB
9297 if (field_decl == error_mark_node)
9298 return 1;
5e942c50
APB
9299
9300 /* Layout the type of field_decl, since we may need
c877974e
APB
9301 it. Don't do primitive types or loaded classes. The
9302 situation of non primitive arrays may not handled
9303 properly here. FIXME */
5e942c50
APB
9304 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9305 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9306 else
9307 field_decl_type = TREE_TYPE (field_decl);
9308 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9309 && !CLASS_LOADED_P (field_decl_type)
9310 && !TYPE_ARRAY_P (field_decl_type))
9311 resolve_and_layout (field_decl_type, NULL_TREE);
9312 if (TYPE_ARRAY_P (field_decl_type))
9313 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9314
9315 /* Check on accessibility here */
9316 if (not_accessible_p (type, field_decl, from_super))
9317 {
9318 parse_error_context
9319 (qual_wfl,
9320 "Can't access %s field `%s.%s' from `%s'",
9321 java_accstring_lookup
9322 (get_access_flags_from_decl (field_decl)),
2aa11e97 9323 GET_TYPE_NAME (type),
e04a16fb
AG
9324 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9325 IDENTIFIER_POINTER
9326 (DECL_NAME (TYPE_NAME (current_class))));
9327 return 1;
9328 }
5e942c50 9329 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9330
9331 /* There are things to check when fields are accessed
9332 from type. There are no restrictions on a static
9333 declaration of the field when it is accessed from an
9334 interface */
9335 is_static = FIELD_STATIC (field_decl);
9336 if (!from_super && from_type
c2952b01
APB
9337 && !TYPE_INTERFACE_P (type)
9338 && !is_static
9339 && (current_function_decl
9340 && METHOD_STATIC (current_function_decl)))
e04a16fb 9341 {
7f10c2e2 9342 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9343 return 1;
9344 }
9345 from_cast = from_super = 0;
9346
c2952b01
APB
9347 /* It's an access from a type but it isn't static, we
9348 make it relative to `this'. */
9349 if (!is_static && from_type)
9350 decl = current_this;
9351
5e942c50
APB
9352 /* If we need to generate something to get a proper
9353 handle on what this field is accessed from, do it
9354 now. */
e04a16fb
AG
9355 if (!is_static)
9356 {
c583dd46 9357 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9358 if (decl == error_mark_node)
9359 return 1;
9360 }
9361
9362 /* We want to keep the location were found it, and the type
9363 we found. */
9364 *where_found = decl;
9365 *type_found = type;
9366
c2952b01
APB
9367 /* Generate the correct expression for field access from
9368 qualified this */
9369 if (from_qualified_this)
9370 {
9371 field_decl = build_outer_field_access (qual_wfl, field_decl);
9372 from_qualified_this = 0;
9373 }
9374
e04a16fb
AG
9375 /* This is the decl found and eventually the next one to
9376 search from */
9377 decl = field_decl;
9378 }
e04a16fb
AG
9379 from_type = 0;
9380 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9381
9382 /* Sneak preview. If decl is qualified by a `new', report
9383 the error here to be accurate on the peculiar construct */
9384 if (TREE_CHAIN (q)
9385 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9386 && !JREFERENCE_TYPE_P (type))
9387 {
9388 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9389 lang_printable_name (type, 0));
9390 return 1;
9391 }
e04a16fb 9392 }
dde1da72
APB
9393 /* `q' might have changed due to a after package resolution
9394 re-qualification */
9395 if (!q)
9396 break;
e04a16fb
AG
9397 }
9398 *found_decl = decl;
9399 return 0;
9400}
9401
9402/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9403 can't be accessed from REFERENCE (a record type). This should be
9404 used when decl is a field or a method.*/
e04a16fb 9405
be245ac0
KG
9406static int
9407not_accessible_p (reference, member, from_super)
e04a16fb
AG
9408 tree reference, member;
9409 int from_super;
9410{
9411 int access_flag = get_access_flags_from_decl (member);
9412
4dbf4496
APB
9413 /* Inner classes are processed by check_inner_class_access */
9414 if (INNER_CLASS_TYPE_P (reference))
9415 return 0;
9416
e04a16fb
AG
9417 /* Access always granted for members declared public */
9418 if (access_flag & ACC_PUBLIC)
9419 return 0;
9420
9421 /* Check access on protected members */
9422 if (access_flag & ACC_PROTECTED)
9423 {
9424 /* Access granted if it occurs from within the package
9425 containing the class in which the protected member is
9426 declared */
9427 if (class_in_current_package (DECL_CONTEXT (member)))
9428 return 0;
9429
9bbc7d9f
PB
9430 /* If accessed with the form `super.member', then access is granted */
9431 if (from_super)
9432 return 0;
e04a16fb 9433
9bbc7d9f 9434 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9435 member is declared or a subclass of it. Find the right
9436 context to perform the check */
9437 if (PURE_INNER_CLASS_TYPE_P (reference))
9438 {
9439 while (INNER_CLASS_TYPE_P (reference))
9440 {
9441 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9442 return 0;
9443 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9444 }
9445 }
473e7b07 9446 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9447 return 0;
e04a16fb
AG
9448 return 1;
9449 }
9450
9451 /* Check access on private members. Access is granted only if it
473e7b07 9452 occurs from within the class in which it is declared. Exceptions
4dbf4496 9453 are accesses from inner-classes. */
e04a16fb 9454 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9455 return (current_class == DECL_CONTEXT (member) ? 0 :
9456 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9457
9458 /* Default access are permitted only when occuring within the
9459 package in which the type (REFERENCE) is declared. In other words,
9460 REFERENCE is defined in the current package */
9461 if (ctxp->package)
9462 return !class_in_current_package (reference);
473e7b07 9463
e04a16fb
AG
9464 /* Otherwise, access is granted */
9465 return 0;
9466}
9467
5e942c50
APB
9468/* Test deprecated decl access. */
9469static void
9470check_deprecation (wfl, decl)
9471 tree wfl, decl;
9472{
49f48c71 9473 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9474 /* Complain if the field is deprecated and the file it was defined
9475 in isn't compiled at the same time the file which contains its
9476 use is */
9477 if (DECL_DEPRECATED (decl)
9478 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9479 {
9480 char the [20];
9481 switch (TREE_CODE (decl))
9482 {
9483 case FUNCTION_DECL:
9484 strcpy (the, "method");
9485 break;
9486 case FIELD_DECL:
9487 strcpy (the, "field");
9488 break;
9489 case TYPE_DECL:
9490 strcpy (the, "class");
9491 break;
15fdcfe9
PB
9492 default:
9493 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9494 }
9495 parse_warning_context
9496 (wfl, "The %s `%s' in class `%s' has been deprecated",
9497 the, lang_printable_name (decl, 0),
9498 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9499 }
9500}
9501
e04a16fb
AG
9502/* Returns 1 if class was declared in the current package, 0 otherwise */
9503
9504static int
9505class_in_current_package (class)
9506 tree class;
9507{
9508 static tree cache = NULL_TREE;
9509 int qualified_flag;
9510 tree left;
9511
9512 if (cache == class)
9513 return 1;
9514
9515 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9516
9517 /* If the current package is empty and the name of CLASS is
9518 qualified, class isn't in the current package. If there is a
9519 current package and the name of the CLASS is not qualified, class
9520 isn't in the current package */
0a2138e2 9521 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9522 return 0;
9523
9524 /* If there is not package and the name of CLASS isn't qualified,
9525 they belong to the same unnamed package */
9526 if (!ctxp->package && !qualified_flag)
9527 return 1;
9528
9529 /* Compare the left part of the name of CLASS with the package name */
9530 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9531 if (ctxp->package == left)
9532 {
9533 cache = class;
9534 return 1;
9535 }
9536 return 0;
9537}
9538
9539/* This function may generate code to access DECL from WHERE. This is
9540 done only if certain conditions meet. */
9541
9542static tree
9543maybe_access_field (decl, where, type)
9544 tree decl, where, type;
9545{
5e942c50
APB
9546 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9547 && !FIELD_STATIC (decl))
e04a16fb 9548 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9549 (type ? type : DECL_CONTEXT (decl)),
9550 DECL_NAME (decl));
e04a16fb
AG
9551 return decl;
9552}
9553
15fdcfe9 9554/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9555 and according to the situation, PRIMARY and WHERE may be
9556 used. IS_STATIC is set to 1 if the invoked function is static. */
9557
9558static tree
89e09b9a 9559patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9560 tree patch, primary, where;
9561 int *is_static;
b9f7e36c 9562 tree *ret_decl;
e04a16fb
AG
9563{
9564 tree wfl = TREE_OPERAND (patch, 0);
9565 tree args = TREE_OPERAND (patch, 1);
9566 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9567 tree list;
22eed1e6 9568 int is_static_flag = 0;
89e09b9a 9569 int is_super_init = 0;
bccaf73a 9570 tree this_arg = NULL_TREE;
e04a16fb
AG
9571
9572 /* Should be overriden if everything goes well. Otherwise, if
9573 something fails, it should keep this value. It stop the
9574 evaluation of a bogus assignment. See java_complete_tree,
9575 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9576 evaluating an assignment */
9577 TREE_TYPE (patch) = error_mark_node;
9578
9579 /* Since lookup functions are messing with line numbers, save the
9580 context now. */
9581 java_parser_context_save_global ();
9582
9583 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9584
9585 /* Resolution of qualified name, excluding constructors */
9586 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9587 {
dde1da72 9588 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9589 /* Extract the last IDENTIFIER of the qualified
9590 expression. This is a wfl and we will use it's location
9591 data during error report. */
9592 identifier_wfl = cut_identifier_in_qualified (wfl);
9593 identifier = EXPR_WFL_NODE (identifier_wfl);
9594
9595 /* Given the context, IDENTIFIER is syntactically qualified
9596 as a MethodName. We need to qualify what's before */
9597 qualify_ambiguous_name (wfl);
dde1da72 9598 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9599
dde1da72
APB
9600 if (resolved == error_mark_node)
9601 PATCH_METHOD_RETURN_ERROR ();
9602
9603 type = GET_SKIP_TYPE (resolved);
9604 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9605
9606 if (JPRIMITIVE_TYPE_P (type))
9607 {
9608 parse_error_context
9609 (identifier_wfl,
9610 "Can't invoke a method on primitive type `%s'",
9611 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9612 PATCH_METHOD_RETURN_ERROR ();
9613 }
9614
dde1da72
APB
9615 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9616 args = nreverse (args);
2c56429a 9617
e04a16fb 9618 /* We're resolving a call from a type */
dde1da72 9619 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9620 {
dde1da72 9621 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9622 {
9623 parse_error_context
781b0558
KG
9624 (identifier_wfl,
9625 "Can't make static reference to method `%s' in interface `%s'",
9626 IDENTIFIER_POINTER (identifier),
e04a16fb 9627 IDENTIFIER_POINTER (name));
b9f7e36c 9628 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9629 }
e04a16fb
AG
9630 if (list && !METHOD_STATIC (list))
9631 {
c2e3db92 9632 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9633 parse_error_context
9634 (identifier_wfl,
9635 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9636 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9637 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9638 free (fct_name);
b9f7e36c 9639 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9640 }
9641 }
e04a16fb 9642 else
dde1da72
APB
9643 this_arg = primary = resolved;
9644
5e942c50 9645 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9646 wfl = identifier_wfl;
9647 }
9648 /* Resolution of simple names, names generated after a primary: or
9649 constructors */
9650 else
9651 {
cd531a2e 9652 tree class_to_search = NULL_TREE;
c2952b01 9653 int lc; /* Looking for Constructor */
e04a16fb
AG
9654
9655 /* We search constructor in their target class */
9656 if (CALL_CONSTRUCTOR_P (patch))
9657 {
22eed1e6
APB
9658 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9659 class_to_search = EXPR_WFL_NODE (wfl);
9660 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9661 this_identifier_node)
9662 class_to_search = NULL_TREE;
9663 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9664 super_identifier_node)
e04a16fb 9665 {
89e09b9a 9666 is_super_init = 1;
22eed1e6
APB
9667 if (CLASSTYPE_SUPER (current_class))
9668 class_to_search =
9669 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9670 else
9671 {
781b0558 9672 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9673 PATCH_METHOD_RETURN_ERROR ();
9674 }
e04a16fb 9675 }
22eed1e6
APB
9676
9677 /* Class to search is NULL if we're searching the current one */
9678 if (class_to_search)
e04a16fb 9679 {
c2952b01
APB
9680 class_to_search = resolve_and_layout (class_to_search, wfl);
9681
22eed1e6
APB
9682 if (!class_to_search)
9683 {
9684 parse_error_context
9685 (wfl, "Class `%s' not found in type declaration",
9686 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9687 PATCH_METHOD_RETURN_ERROR ();
9688 }
9689
5e942c50
APB
9690 /* Can't instantiate an abstract class, but we can
9691 invoke it's constructor. It's use within the `new'
9692 context is denied here. */
9693 if (CLASS_ABSTRACT (class_to_search)
9694 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9695 {
9696 parse_error_context
781b0558
KG
9697 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9698 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9699 PATCH_METHOD_RETURN_ERROR ();
9700 }
c2952b01 9701
22eed1e6 9702 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9703 }
22eed1e6
APB
9704 else
9705 class_to_search = current_class;
e04a16fb
AG
9706 lc = 1;
9707 }
9708 /* This is a regular search in the local class, unless an
9709 alternate class is specified. */
9710 else
9711 {
9712 class_to_search = (where ? where : current_class);
9713 lc = 0;
9714 }
c2952b01 9715
e04a16fb
AG
9716 /* NAME is a simple identifier or comes from a primary. Search
9717 in the class whose declaration contain the method being
9718 invoked. */
c877974e 9719 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9720
c2952b01 9721 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9722 /* Don't continue if no method were found, as the next statement
9723 can't be executed then. */
b9f7e36c
APB
9724 if (!list)
9725 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9726
9727 /* Check for static reference if non static methods */
9728 if (check_for_static_method_reference (wfl, patch, list,
9729 class_to_search, primary))
b9f7e36c 9730 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9731
165f37bc
APB
9732 /* Check for inner classes creation from illegal contexts */
9733 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9734 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9735 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9736 {
9737 parse_error_context
9738 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9739 lang_printable_name (class_to_search, 0),
9740 (!current_this ? "" :
9741 "; an explicit one must be provided when creating this inner class"));
9742 PATCH_METHOD_RETURN_ERROR ();
9743 }
9744
22eed1e6
APB
9745 /* Non static methods are called with the current object extra
9746 argument. If patch a `new TYPE()', the argument is the value
9747 returned by the object allocator. If method is resolved as a
9748 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9749 args = nreverse (args);
bccaf73a 9750 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9751 {
9752 this_arg = primary ? primary : current_this;
9753
9754 /* If we're using an access method, things are different.
9755 There are two familly of cases:
9756
9757 1) We're not generating bytecodes:
9758
9759 - LIST is non static. It's invocation is transformed from
9760 x(a1,...,an) into this$<n>.x(a1,....an).
9761 - LIST is static. It's invocation is transformed from
9762 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9763
9764 2) We're generating bytecodes:
9765
9766 - LIST is non static. It's invocation is transformed from
9767 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9768 - LIST is static. It's invocation is transformed from
9769 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9770
9771 Of course, this$<n> can be abitrary complex, ranging from
9772 this$0 (the immediate outer context) to
9773 access$0(access$0(...(this$0))).
9774
9775 maybe_use_access_method returns a non zero value if the
dfb99c83 9776 this_arg has to be moved into the (then generated) stub
4dbf4496 9777 argument list. In the meantime, the selected function
dfb99c83 9778 might have be replaced by a generated stub. */
c2952b01
APB
9779 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9780 args = tree_cons (NULL_TREE, this_arg, args);
9781 }
e04a16fb 9782 }
b67d701b 9783
e04a16fb
AG
9784 /* Merge point of all resolution schemes. If we have nothing, this
9785 is an error, already signaled */
b9f7e36c
APB
9786 if (!list)
9787 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9788
e04a16fb
AG
9789 /* Check accessibility, position the is_static flag, build and
9790 return the call */
9bbc7d9f 9791 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9792 {
c2e3db92 9793 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9794 parse_error_context
9795 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9796 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9797 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9798 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9799 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9800 free (fct_name);
b9f7e36c 9801 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9802 }
5e942c50 9803 check_deprecation (wfl, list);
22eed1e6 9804
c2952b01
APB
9805 /* If invoking a innerclass constructor, there are hidden parameters
9806 to pass */
9807 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9808 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9809 {
9810 /* And make sure we add the accessed local variables to be saved
9811 in field aliases. */
9812 args = build_alias_initializer_parameter_list
9813 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9814
da632f2c 9815 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9816 if (primary || current_this)
9817 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9818 else
9819 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9820 }
9821
152de068
APB
9822 /* This handles the situation where a constructor invocation needs
9823 to have an enclosing context passed as a second parameter (the
9824 constructor is one of an inner class. We extract it from the
9825 current function. */
9826 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9827 {
9828 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9829 tree extra_arg;
9830
9831 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9832 {
9833 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9834 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9835 }
9836 else
9837 {
9838 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9839 extra_arg =
9840 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9841 extra_arg = java_complete_tree (extra_arg);
9842 }
9843 args = tree_cons (NULL_TREE, extra_arg, args);
9844 }
9845
22eed1e6 9846 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9847 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9848 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9849
c3f2a476
APB
9850 /* In the context of an explicit constructor invocation, we can't
9851 invoke any method relying on `this'. Exceptions are: we're
9852 invoking a static function, primary exists and is not the current
9853 this, we're creating a new object. */
22eed1e6 9854 if (ctxp->explicit_constructor_p
c3f2a476
APB
9855 && !is_static_flag
9856 && (!primary || primary == current_this)
9857 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9858 {
781b0558 9859 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9860 PATCH_METHOD_RETURN_ERROR ();
9861 }
e04a16fb 9862 java_parser_context_restore_global ();
22eed1e6
APB
9863 if (is_static)
9864 *is_static = is_static_flag;
b9f7e36c
APB
9865 /* Sometimes, we want the decl of the selected method. Such as for
9866 EH checking */
9867 if (ret_decl)
9868 *ret_decl = list;
89e09b9a
PB
9869 patch = patch_invoke (patch, list, args);
9870 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9871 {
c2952b01
APB
9872 tree finit_parms, finit_call;
9873
9874 /* Prepare to pass hidden parameters to $finit$, if any. */
9875 finit_parms = build_alias_initializer_parameter_list
9876 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9877
c2952b01
APB
9878 finit_call =
9879 build_method_invocation (build_wfl_node (finit_identifier_node),
9880 finit_parms);
9881
9882 /* Generate the code used to initialize fields declared with an
9883 initialization statement and build a compound statement along
9884 with the super constructor invocation. */
89e09b9a
PB
9885 patch = build (COMPOUND_EXPR, void_type_node, patch,
9886 java_complete_tree (finit_call));
9887 CAN_COMPLETE_NORMALLY (patch) = 1;
9888 }
9889 return patch;
e04a16fb
AG
9890}
9891
9892/* Check that we're not trying to do a static reference to a method in
9893 non static method. Return 1 if it's the case, 0 otherwise. */
9894
9895static int
9896check_for_static_method_reference (wfl, node, method, where, primary)
9897 tree wfl, node, method, where, primary;
9898{
9899 if (METHOD_STATIC (current_function_decl)
9900 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9901 {
c2e3db92 9902 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9903 parse_error_context
9904 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9905 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9906 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9907 free (fct_name);
9908 return 1;
9909 }
9910 return 0;
9911}
9912
c2952b01
APB
9913/* Fix the invocation of *MDECL if necessary in the case of a
9914 invocation from an inner class. *THIS_ARG might be modified
9915 appropriately and an alternative access to *MDECL might be
9916 returned. */
9917
9918static int
9919maybe_use_access_method (is_super_init, mdecl, this_arg)
9920 int is_super_init;
9921 tree *mdecl, *this_arg;
9922{
9923 tree ctx;
9924 tree md = *mdecl, ta = *this_arg;
9925 int to_return = 0;
9926 int non_static_context = !METHOD_STATIC (md);
9927
9928 if (is_super_init
165f37bc
APB
9929 || DECL_CONTEXT (md) == current_class
9930 || !PURE_INNER_CLASS_TYPE_P (current_class)
9931 || DECL_FINIT_P (md))
c2952b01
APB
9932 return 0;
9933
9934 /* If we're calling a method found in an enclosing class, generate
9935 what it takes to retrieve the right this. Don't do that if we're
9936 invoking a static method. */
9937
9938 if (non_static_context)
9939 {
9940 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 9941 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
9942 {
9943 ta = build_current_thisn (current_class);
9944 ta = build_wfl_node (ta);
9945 }
9946 else
9947 {
9948 tree type = ctx;
9949 while (type)
9950 {
9951 maybe_build_thisn_access_method (type);
4dbf4496 9952 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
9953 {
9954 ta = build_access_to_thisn (ctx, type, 0);
9955 break;
9956 }
9957 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9958 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9959 }
9960 }
9961 ta = java_complete_tree (ta);
9962 }
9963
9964 /* We might have to use an access method to get to MD. We can
9965 break the method access rule as far as we're not generating
9966 bytecode */
9967 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9968 {
9969 md = build_outer_method_access_method (md);
9970 to_return = 1;
9971 }
9972
9973 *mdecl = md;
9974 *this_arg = ta;
9975
9976 /* Returnin a non zero value indicates we were doing a non static
9977 method invokation that is now a static invocation. It will have
9978 callee displace `this' to insert it in the regular argument
9979 list. */
9980 return (non_static_context && to_return);
9981}
9982
e04a16fb
AG
9983/* Patch an invoke expression METHOD and ARGS, based on its invocation
9984 mode. */
9985
9986static tree
89e09b9a 9987patch_invoke (patch, method, args)
e04a16fb 9988 tree patch, method, args;
e04a16fb
AG
9989{
9990 tree dtable, func;
0a2138e2 9991 tree original_call, t, ta;
e815887f 9992 tree cond = NULL_TREE;
e04a16fb 9993
5e942c50
APB
9994 /* Last step for args: convert build-in types. If we're dealing with
9995 a new TYPE() type call, the first argument to the constructor
e815887f 9996 isn't found in the incoming argument list, but delivered by
5e942c50
APB
9997 `new' */
9998 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9999 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10000 t = TREE_CHAIN (t);
ac825856
APB
10001 for (ta = args; t != end_params_node && ta;
10002 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10003 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10004 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10005 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10006
10007 /* Resolve unresolved returned type isses */
10008 t = TREE_TYPE (TREE_TYPE (method));
10009 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10010 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10011
e8fc7396 10012 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10013 func = method;
10014 else
e04a16fb 10015 {
15fdcfe9 10016 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10017 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10018 {
10019 case INVOKE_VIRTUAL:
10020 dtable = invoke_build_dtable (0, args);
10021 func = build_invokevirtual (dtable, method);
10022 break;
b9f7e36c 10023
e815887f
TT
10024 case INVOKE_NONVIRTUAL:
10025 /* If the object for the method call is null, we throw an
10026 exception. We don't do this if the object is the current
10027 method's `this'. In other cases we just rely on an
10028 optimization pass to eliminate redundant checks. */
10029 if (TREE_VALUE (args) != current_this)
10030 {
10031 /* We use a SAVE_EXPR here to make sure we only evaluate
10032 the new `self' expression once. */
10033 tree save_arg = save_expr (TREE_VALUE (args));
10034 TREE_VALUE (args) = save_arg;
10035 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10036 null_pointer_node);
10037 }
10038 /* Fall through. */
10039
15fdcfe9
PB
10040 case INVOKE_SUPER:
10041 case INVOKE_STATIC:
10042 func = build_known_method_ref (method, TREE_TYPE (method),
10043 DECL_CONTEXT (method),
10044 signature, args);
10045 break;
e04a16fb 10046
15fdcfe9
PB
10047 case INVOKE_INTERFACE:
10048 dtable = invoke_build_dtable (1, args);
173f556c 10049 func = build_invokeinterface (dtable, method);
15fdcfe9 10050 break;
5e942c50 10051
15fdcfe9 10052 default:
89e09b9a 10053 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10054 }
10055
10056 /* Ensure self_type is initialized, (invokestatic). FIXME */
10057 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10058 }
10059
e04a16fb
AG
10060 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10061 TREE_OPERAND (patch, 0) = func;
10062 TREE_OPERAND (patch, 1) = args;
10063 original_call = patch;
10064
e815887f 10065 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10066 returned value is the first argument to the constructor. We build
10067 a COMPOUND_EXPR and use saved expression so that the overall NEW
10068 expression value is a pointer to a newly created and initialized
10069 class. */
10070 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10071 {
10072 tree class = DECL_CONTEXT (method);
10073 tree c1, saved_new, size, new;
e8fc7396 10074 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10075 {
10076 TREE_TYPE (patch) = build_pointer_type (class);
10077 return patch;
10078 }
e04a16fb
AG
10079 if (!TYPE_SIZE (class))
10080 safe_layout_class (class);
10081 size = size_in_bytes (class);
10082 new = build (CALL_EXPR, promote_type (class),
10083 build_address_of (alloc_object_node),
10084 tree_cons (NULL_TREE, build_class_ref (class),
10085 build_tree_list (NULL_TREE,
10086 size_in_bytes (class))),
10087 NULL_TREE);
10088 saved_new = save_expr (new);
10089 c1 = build_tree_list (NULL_TREE, saved_new);
10090 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10091 TREE_OPERAND (original_call, 1) = c1;
10092 TREE_SET_CODE (original_call, CALL_EXPR);
10093 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10094 }
e815887f
TT
10095
10096 /* If COND is set, then we are building a check to see if the object
10097 is NULL. */
10098 if (cond != NULL_TREE)
10099 {
10100 /* We have to make the `then' branch a compound expression to
10101 make the types turn out right. This seems bizarre. */
10102 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10103 build (COMPOUND_EXPR, TREE_TYPE (patch),
10104 build (CALL_EXPR, void_type_node,
10105 build_address_of (soft_nullpointer_node),
10106 NULL_TREE, NULL_TREE),
10107 (FLOAT_TYPE_P (TREE_TYPE (patch))
10108 ? build_real (TREE_TYPE (patch), dconst0)
10109 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10110 integer_zero_node))),
10111 patch);
10112 TREE_SIDE_EFFECTS (patch) = 1;
10113 }
10114
e04a16fb
AG
10115 return patch;
10116}
10117
10118static int
10119invocation_mode (method, super)
10120 tree method;
10121 int super;
10122{
10123 int access = get_access_flags_from_decl (method);
10124
22eed1e6
APB
10125 if (super)
10126 return INVOKE_SUPER;
10127
e815887f 10128 if (access & ACC_STATIC)
e04a16fb
AG
10129 return INVOKE_STATIC;
10130
e815887f
TT
10131 /* We have to look for a constructor before we handle nonvirtual
10132 calls; otherwise the constructor will look nonvirtual. */
10133 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10134 return INVOKE_STATIC;
e815887f
TT
10135
10136 if (access & ACC_FINAL || access & ACC_PRIVATE)
10137 return INVOKE_NONVIRTUAL;
10138
10139 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10140 return INVOKE_NONVIRTUAL;
10141
e04a16fb
AG
10142 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10143 return INVOKE_INTERFACE;
22eed1e6 10144
e04a16fb
AG
10145 return INVOKE_VIRTUAL;
10146}
10147
b67d701b
PB
10148/* Retrieve a refined list of matching methods. It covers the step
10149 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10150
10151static tree
10152lookup_method_invoke (lc, cl, class, name, arg_list)
10153 int lc;
10154 tree cl;
10155 tree class, name, arg_list;
10156{
de4c7b02 10157 tree atl = end_params_node; /* Arg Type List */
c877974e 10158 tree method, signature, list, node;
49f48c71 10159 const char *candidates; /* Used for error report */
b5b8a0e7 10160 char *dup;
e04a16fb 10161
5e942c50 10162 /* Fix the arguments */
e04a16fb
AG
10163 for (node = arg_list; node; node = TREE_CHAIN (node))
10164 {
e3884b71 10165 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10166 /* Non primitive type may have to be resolved */
e3884b71 10167 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10168 resolve_and_layout (current_arg, NULL_TREE);
10169 /* And promoted */
b67d701b 10170 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10171 current_arg = promote_type (current_arg);
5e942c50 10172 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10173 }
e04a16fb 10174
c2952b01
APB
10175 /* Presto. If we're dealing with an anonymous class and a
10176 constructor call, generate the right constructor now, since we
10177 know the arguments' types. */
10178
10179 if (lc && ANONYMOUS_CLASS_P (class))
10180 craft_constructor (TYPE_NAME (class), atl);
10181
5e942c50
APB
10182 /* Find all candidates and then refine the list, searching for the
10183 most specific method. */
10184 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10185 list = find_most_specific_methods_list (list);
b67d701b
PB
10186 if (list && !TREE_CHAIN (list))
10187 return TREE_VALUE (list);
e04a16fb 10188
b67d701b
PB
10189 /* Issue an error. List candidates if any. Candidates are listed
10190 only if accessible (non accessible methods may end-up here for
10191 the sake of a better error report). */
10192 candidates = NULL;
10193 if (list)
e04a16fb 10194 {
e04a16fb 10195 tree current;
b67d701b 10196 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10197 for (current = list; current; current = TREE_CHAIN (current))
10198 {
b67d701b
PB
10199 tree cm = TREE_VALUE (current);
10200 char string [4096];
10201 if (!cm || not_accessible_p (class, cm, 0))
10202 continue;
b67d701b 10203 sprintf
22eed1e6
APB
10204 (string, " `%s' in `%s'%s",
10205 get_printable_method_name (cm),
b67d701b
PB
10206 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10207 (TREE_CHAIN (current) ? "\n" : ""));
10208 obstack_grow (&temporary_obstack, string, strlen (string));
10209 }
10210 obstack_1grow (&temporary_obstack, '\0');
10211 candidates = obstack_finish (&temporary_obstack);
10212 }
10213 /* Issue the error message */
c877974e
APB
10214 method = make_node (FUNCTION_TYPE);
10215 TYPE_ARG_TYPES (method) = atl;
b67d701b 10216 signature = build_java_argument_signature (method);
c63b98cd 10217 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10218 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10219 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10220 (lc ? dup : IDENTIFIER_POINTER (name)),
10221 IDENTIFIER_POINTER (signature), dup,
b67d701b 10222 (candidates ? candidates : ""));
b5b8a0e7 10223 free (dup);
b67d701b
PB
10224 return NULL_TREE;
10225}
10226
5e942c50
APB
10227/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10228 when we're looking for a constructor. */
b67d701b
PB
10229
10230static tree
5e942c50
APB
10231find_applicable_accessible_methods_list (lc, class, name, arglist)
10232 int lc;
b67d701b
PB
10233 tree class, name, arglist;
10234{
ad69b5b6
BM
10235 static struct hash_table t, *searched_classes = NULL;
10236 static int search_not_done = 0;
b67d701b
PB
10237 tree list = NULL_TREE, all_list = NULL_TREE;
10238
ad69b5b6
BM
10239 /* Check the hash table to determine if this class has been searched
10240 already. */
10241 if (searched_classes)
10242 {
10243 if (hash_lookup (searched_classes,
10244 (const hash_table_key) class, FALSE, NULL))
10245 return NULL;
10246 }
10247 else
10248 {
10249 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10250 java_hash_compare_tree_node);
10251 searched_classes = &t;
10252 }
10253
10254 search_not_done++;
10255 hash_lookup (searched_classes,
0c2b8145 10256 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10257
c2952b01
APB
10258 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10259 {
10260 load_class (class, 1);
10261 safe_layout_class (class);
10262 }
10263
1982388a 10264 /* Search interfaces */
9a7ab4b3
APB
10265 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10266 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10267 {
1982388a
APB
10268 int i, n;
10269 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10270 search_applicable_methods_list (lc, TYPE_METHODS (class),
10271 name, arglist, &list, &all_list);
1982388a 10272 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10273 for (i = 1; i < n; i++)
b67d701b 10274 {
de0b553f
APB
10275 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10276 tree rlist;
10277
de0b553f
APB
10278 rlist = find_applicable_accessible_methods_list (lc, t, name,
10279 arglist);
165f37bc 10280 list = chainon (rlist, list);
e04a16fb 10281 }
e04a16fb 10282 }
1982388a
APB
10283 /* Search classes */
10284 else
c2952b01 10285 {
165f37bc
APB
10286 tree sc = class;
10287 int seen_inner_class = 0;
c2952b01
APB
10288 search_applicable_methods_list (lc, TYPE_METHODS (class),
10289 name, arglist, &list, &all_list);
10290
165f37bc
APB
10291 /* We must search all interfaces of this class */
10292 if (!lc)
10293 {
10294 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10295 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10296 for (i = 1; i < n; i++)
10297 {
10298 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10299 if (t != object_type_node)
30a3caef
ZW
10300 {
10301 tree rlist
10302 = find_applicable_accessible_methods_list (lc, t,
10303 name, arglist);
10304 list = chainon (rlist, list);
10305 }
165f37bc 10306 }
165f37bc
APB
10307 }
10308
c2952b01
APB
10309 /* Search enclosing context of inner classes before looking
10310 ancestors up. */
10311 while (!lc && INNER_CLASS_TYPE_P (class))
10312 {
165f37bc
APB
10313 tree rlist;
10314 seen_inner_class = 1;
c2952b01 10315 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10316 rlist = find_applicable_accessible_methods_list (lc, class,
10317 name, arglist);
10318 list = chainon (rlist, list);
c2952b01 10319 }
165f37bc
APB
10320
10321 if (!lc && seen_inner_class
10322 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10323 class = CLASSTYPE_SUPER (sc);
10324 else
10325 class = sc;
10326
ad69b5b6
BM
10327 /* Search superclass */
10328 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10329 {
10330 tree rlist;
10331 class = CLASSTYPE_SUPER (class);
10332 rlist = find_applicable_accessible_methods_list (lc, class,
10333 name, arglist);
10334 list = chainon (rlist, list);
10335 }
10336 }
10337
10338 search_not_done--;
10339
10340 /* We're done. Reset the searched classes list and finally search
10341 java.lang.Object if it wasn't searched already. */
10342 if (!search_not_done)
10343 {
10344 if (!lc
10345 && TYPE_METHODS (object_type_node)
10346 && !hash_lookup (searched_classes,
10347 (const hash_table_key) object_type_node,
10348 FALSE, NULL))
10349 {
10350 search_applicable_methods_list (lc,
10351 TYPE_METHODS (object_type_node),
10352 name, arglist, &list, &all_list);
10353 }
10354 hash_table_free (searched_classes);
10355 searched_classes = NULL;
c2952b01 10356 }
1982388a 10357
b67d701b
PB
10358 /* Either return the list obtained or all selected (but
10359 inaccessible) methods for better error report. */
10360 return (!list ? all_list : list);
10361}
e04a16fb 10362
ad69b5b6 10363/* Effectively search for the appropriate method in method */
1982388a
APB
10364
10365static void
c2952b01 10366search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10367 int lc;
10368 tree method, name, arglist;
10369 tree *list, *all_list;
10370{
10371 for (; method; method = TREE_CHAIN (method))
10372 {
10373 /* When dealing with constructor, stop here, otherwise search
10374 other classes */
10375 if (lc && !DECL_CONSTRUCTOR_P (method))
10376 continue;
10377 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10378 || (GET_METHOD_NAME (method) != name)))
10379 continue;
10380
10381 if (argument_types_convertible (method, arglist))
10382 {
10383 /* Retain accessible methods only */
10384 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10385 method, 0))
10386 *list = tree_cons (NULL_TREE, method, *list);
10387 else
10388 /* Also retain all selected method here */
10389 *all_list = tree_cons (NULL_TREE, method, *list);
10390 }
10391 }
ad69b5b6 10392}
1982388a 10393
b67d701b
PB
10394/* 15.11.2.2 Choose the Most Specific Method */
10395
10396static tree
10397find_most_specific_methods_list (list)
10398 tree list;
10399{
10400 int max = 0;
9a7ab4b3 10401 int abstract, candidates;
b67d701b
PB
10402 tree current, new_list = NULL_TREE;
10403 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10404 {
b67d701b
PB
10405 tree method;
10406 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10407
10408 for (method = list; method; method = TREE_CHAIN (method))
10409 {
0c2b8145 10410 tree method_v, current_v;
b67d701b
PB
10411 /* Don't test a method against itself */
10412 if (method == current)
10413 continue;
10414
0c2b8145
APB
10415 method_v = TREE_VALUE (method);
10416 current_v = TREE_VALUE (current);
10417
10418 /* Compare arguments and location where methods where declared */
10419 if (argument_types_convertible (method_v, current_v))
b67d701b 10420 {
0c2b8145
APB
10421 if (valid_method_invocation_conversion_p
10422 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10423 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10424 && enclosing_context_p (DECL_CONTEXT (method_v),
10425 DECL_CONTEXT (current_v))))
10426 {
10427 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10428 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10429 max = (v > max ? v : max);
10430 }
b67d701b
PB
10431 }
10432 }
e04a16fb
AG
10433 }
10434
b67d701b 10435 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10436 for (current = list, abstract = -1, candidates = -1;
10437 current; current = TREE_CHAIN (current))
b67d701b 10438 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10439 {
10440 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10441 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10442 candidates++;
10443 }
b67d701b 10444
165f37bc
APB
10445 /* If we have several and they're all abstract, just pick the
10446 closest one. */
9a7ab4b3
APB
10447 if (candidates > 0 && (candidates == abstract))
10448 {
10449 new_list = nreverse (new_list);
10450 TREE_CHAIN (new_list) = NULL_TREE;
10451 }
165f37bc 10452
9a7ab4b3
APB
10453 /* We have several, we couldn't find a most specific, all but one are
10454 abstract, we pick the only non abstract one. */
10455 if (candidates > 0 && !max && (candidates == abstract+1))
165f37bc 10456 {
9a7ab4b3
APB
10457 for (current = new_list; current; current = TREE_CHAIN (current))
10458 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10459 {
10460 TREE_CHAIN (current) = NULL_TREE;
10461 new_list = current;
10462 }
165f37bc
APB
10463 }
10464
b67d701b
PB
10465 /* If we can't find one, lower expectations and try to gather multiple
10466 maximally specific methods */
165f37bc 10467 while (!new_list && max)
b67d701b
PB
10468 {
10469 while (--max > 0)
10470 {
10471 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10472 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10473 }
b67d701b
PB
10474 }
10475
10476 return new_list;
e04a16fb
AG
10477}
10478
b67d701b
PB
10479/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10480 converted by method invocation conversion (5.3) to the type of the
10481 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10482 to change less often than M1. */
e04a16fb 10483
b67d701b
PB
10484static int
10485argument_types_convertible (m1, m2_or_arglist)
10486 tree m1, m2_or_arglist;
e04a16fb 10487{
b67d701b
PB
10488 static tree m2_arg_value = NULL_TREE;
10489 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10490
b67d701b 10491 register tree m1_arg, m2_arg;
e04a16fb 10492
c2952b01 10493 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10494
b67d701b
PB
10495 if (m2_arg_value == m2_or_arglist)
10496 m2_arg = m2_arg_cache;
10497 else
10498 {
10499 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10500 argument types */
10501 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10502 {
10503 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10504 if (!METHOD_STATIC (m2_or_arglist))
10505 m2_arg = TREE_CHAIN (m2_arg);
10506 }
10507 else
10508 m2_arg = m2_or_arglist;
e04a16fb 10509
b67d701b
PB
10510 m2_arg_value = m2_or_arglist;
10511 m2_arg_cache = m2_arg;
10512 }
e04a16fb 10513
de4c7b02 10514 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10515 {
c877974e 10516 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10517 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10518 TREE_VALUE (m2_arg)))
10519 break;
10520 m1_arg = TREE_CHAIN (m1_arg);
10521 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10522 }
de4c7b02 10523 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10524}
10525
10526/* Qualification routines */
10527
10528static void
10529qualify_ambiguous_name (id)
10530 tree id;
10531{
cd531a2e
KG
10532 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10533 saved_current_class;
d8fccff5 10534 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10535 int code;
e04a16fb
AG
10536
10537 /* We first qualify the first element, then derive qualification of
10538 others based on the first one. If the first element is qualified
10539 by a resolution (field or type), this resolution is stored in the
10540 QUAL_RESOLUTION of the qual element being examined. We need to
10541 save the current_class since the use of SUPER might change the
10542 its value. */
10543 saved_current_class = current_class;
10544 qual = EXPR_WFL_QUALIFICATION (id);
10545 do {
10546
10547 /* Simple qualified expression feature a qual_wfl that is a
10548 WFL. Expression derived from a primary feature more complicated
10549 things like a CALL_EXPR. Expression from primary need to be
10550 worked out to extract the part on which the qualification will
10551 take place. */
10552 qual_wfl = QUAL_WFL (qual);
10553 switch (TREE_CODE (qual_wfl))
10554 {
10555 case CALL_EXPR:
10556 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10557 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10558 {
10559 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10560 qual_wfl = QUAL_WFL (qual);
10561 }
10562 break;
d8fccff5 10563 case NEW_ARRAY_EXPR:
c2952b01 10564 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10565 qual = TREE_CHAIN (qual);
1a6d4fb7 10566 again = new_array_found = 1;
d8fccff5 10567 continue;
e04a16fb 10568 case CONVERT_EXPR:
f2760b27
APB
10569 break;
10570 case NEW_CLASS_EXPR:
e04a16fb
AG
10571 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10572 break;
c583dd46
APB
10573 case ARRAY_REF:
10574 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10575 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10576 break;
8576f094
APB
10577 case STRING_CST:
10578 qual = TREE_CHAIN (qual);
10579 qual_wfl = QUAL_WFL (qual);
10580 break;
165f37bc
APB
10581 case CLASS_LITERAL:
10582 qual = TREE_CHAIN (qual);
10583 qual_wfl = QUAL_WFL (qual);
10584 break;
0a2138e2
APB
10585 default:
10586 /* Fix for -Wall. Just break doing nothing */
10587 break;
e04a16fb 10588 }
8576f094 10589
e04a16fb
AG
10590 ptr_type = current_class;
10591 again = 0;
8576f094
APB
10592 code = TREE_CODE (qual_wfl);
10593
10594 /* Pos evaluation: non WFL leading expression nodes */
10595 if (code == CONVERT_EXPR
10596 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10597 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10598
cd7c5840
APB
10599 else if (code == INTEGER_CST)
10600 name = qual_wfl;
10601
ac22f9cb 10602 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10603 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10604 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10605
c2952b01
APB
10606 else if (code == TREE_LIST)
10607 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10608
37feda7d
APB
10609 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10610 || code == PLUS_EXPR)
8576f094
APB
10611 {
10612 qual = TREE_CHAIN (qual);
10613 qual_wfl = QUAL_WFL (qual);
10614 again = 1;
10615 }
10616 else
f441f671
APB
10617 {
10618 name = EXPR_WFL_NODE (qual_wfl);
10619 if (!name)
10620 {
10621 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10622 again = 1;
10623 }
10624 }
10625
e04a16fb
AG
10626 /* If we have a THIS (from a primary), we set the context accordingly */
10627 if (name == this_identifier_node)
10628 {
10629 qual = TREE_CHAIN (qual);
10630 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10631 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10632 again = 1;
10633 else
10634 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10635 this_found = 1;
10636 }
10637 /* If we have a SUPER, we set the context accordingly */
10638 if (name == super_identifier_node)
10639 {
10640 current_class = CLASSTYPE_SUPER (ptr_type);
10641 /* Check that there is such a thing as a super class. If not,
10642 return. The error will be caught later on, during the
10643 resolution */
10644 if (!current_class)
10645 {
10646 current_class = saved_current_class;
10647 return;
10648 }
10649 qual = TREE_CHAIN (qual);
10650 /* Do one more interation to set things up */
10651 super_found = again = 1;
10652 }
10653 } while (again);
10654
f2760b27
APB
10655 /* If name appears within the scope of a local variable declaration
10656 or parameter declaration, then it is an expression name. We don't
10657 carry this test out if we're in the context of the use of SUPER
10658 or THIS */
cd7c5840
APB
10659 if (!this_found && !super_found
10660 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10661 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10662 {
10663 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10664 QUAL_RESOLUTION (qual) = decl;
10665 }
10666
10667 /* If within the class/interface NAME was found to be used there
10668 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10669 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10670 address length, it is OK. */
10671 else if ((decl = lookup_field_wrapper (ptr_type, name))
10672 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10673 {
10674 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10675 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10676 }
10677
1a6d4fb7 10678 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10679 - NAME is a class/interface declared within the compilation
10680 unit containing NAME,
10681 - NAME is imported via a single-type-import declaration,
10682 - NAME is declared in an another compilation unit of the package
10683 of the compilation unit containing NAME,
10684 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10685 of the compilation unit containing NAME.
10686 - NAME is actually a STRING_CST. */
cd7c5840
APB
10687 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10688 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10689 {
10690 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10691 QUAL_RESOLUTION (qual) = decl;
10692 }
10693
f2760b27 10694 /* Method call, array references and cast are expression name */
9bbc7d9f 10695 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10696 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10697 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10698 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10699
10700 /* Check here that NAME isn't declared by more than one
10701 type-import-on-demand declaration of the compilation unit
10702 containing NAME. FIXME */
10703
10704 /* Otherwise, NAME is reclassified as a package name */
10705 else
10706 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10707
10708 /* Propagate the qualification accross other components of the
10709 qualified name */
10710 for (qual = TREE_CHAIN (qual); qual;
10711 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10712 {
10713 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10714 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10715 else
10716 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10717 }
10718
10719 /* Store the global qualification for the ambiguous part of ID back
10720 into ID fields */
10721 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10722 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10723 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10724 RESOLVE_TYPE_NAME_P (id) = 1;
10725 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10726 RESOLVE_PACKAGE_NAME_P (id) = 1;
10727
10728 /* Restore the current class */
10729 current_class = saved_current_class;
10730}
10731
10732static int
10733breakdown_qualified (left, right, source)
10734 tree *left, *right, source;
10735{
10736 char *p = IDENTIFIER_POINTER (source), *base;
10737 int l = IDENTIFIER_LENGTH (source);
10738
10739 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10740 base = p;
10741 p += (l-1);
10742 while (*p != '.' && p != base)
10743 p--;
10744
10745 /* We didn't find a '.'. Return an error */
10746 if (p == base)
10747 return 1;
10748
10749 *p = '\0';
10750 if (right)
10751 *right = get_identifier (p+1);
10752 *left = get_identifier (IDENTIFIER_POINTER (source));
10753 *p = '.';
10754
10755 return 0;
10756}
10757
e04a16fb 10758/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10759 local variable decls if present.
10760 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10761
10762static tree
10763java_complete_tree (node)
10764 tree node;
5b09b33e
PB
10765{
10766 node = java_complete_lhs (node);
0c2b8145
APB
10767 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10768 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10769 && !flag_emit_xref)
5b09b33e
PB
10770 {
10771 tree value = DECL_INITIAL (node);
10772 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10773 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10774 value = fold_constant_for_init (value, node);
100f7cd8 10775 pop_obstacks ();
5b09b33e
PB
10776 DECL_INITIAL (node) = value;
10777 if (value != NULL_TREE)
c2952b01
APB
10778 {
10779 /* fold_constant_for_init sometimes widen the original type
10780 of the constant (i.e. byte to int.) It's not desirable,
10781 especially if NODE is a function argument. */
10782 if (TREE_CODE (value) == INTEGER_CST
10783 && TREE_TYPE (node) != TREE_TYPE (value))
10784 return convert (TREE_TYPE (node), value);
10785 else
10786 return value;
10787 }
5b09b33e
PB
10788 }
10789 return node;
10790}
10791
2aa11e97
APB
10792static tree
10793java_stabilize_reference (node)
10794 tree node;
10795{
10796 if (TREE_CODE (node) == COMPOUND_EXPR)
10797 {
10798 tree op0 = TREE_OPERAND (node, 0);
10799 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10800 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10801 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10802 return node;
10803 }
5cbdba64 10804 return stabilize_reference (node);
2aa11e97
APB
10805}
10806
5b09b33e
PB
10807/* Patch tree nodes in a function body. When a BLOCK is found, push
10808 local variable decls if present.
10809 Same as java_complete_tree, but does not resolve static finals to values. */
10810
10811static tree
10812java_complete_lhs (node)
10813 tree node;
e04a16fb 10814{
22eed1e6 10815 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10816 int flag;
e04a16fb
AG
10817
10818 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10819 worked out. */
e04a16fb
AG
10820 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10821 return node;
10822
10823 /* The switch block implements cases processing container nodes
10824 first. Contained nodes are always written back. Leaves come
10825 next and return a value. */
10826 switch (TREE_CODE (node))
10827 {
10828 case BLOCK:
10829
10830 /* 1- Block section.
10831 Set the local values on decl names so we can identify them
10832 faster when they're referenced. At that stage, identifiers
10833 are legal so we don't check for declaration errors. */
10834 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10835 {
10836 DECL_CONTEXT (cn) = current_function_decl;
10837 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10838 }
15fdcfe9
PB
10839 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10840 CAN_COMPLETE_NORMALLY (node) = 1;
10841 else
e04a16fb 10842 {
15fdcfe9
PB
10843 tree stmt = BLOCK_EXPR_BODY (node);
10844 tree *ptr;
10845 int error_seen = 0;
10846 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10847 {
c877974e
APB
10848 /* Re-order from (((A; B); C); ...; Z) to
10849 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10850 This makes it easier to scan the statements left-to-right
10851 without using recursion (which might overflow the stack
10852 if the block has many statements. */
10853 for (;;)
10854 {
10855 tree left = TREE_OPERAND (stmt, 0);
10856 if (TREE_CODE (left) != COMPOUND_EXPR)
10857 break;
10858 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10859 TREE_OPERAND (left, 1) = stmt;
10860 stmt = left;
10861 }
10862 BLOCK_EXPR_BODY (node) = stmt;
10863 }
10864
c877974e
APB
10865 /* Now do the actual complete, without deep recursion for
10866 long blocks. */
15fdcfe9 10867 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10868 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10869 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10870 {
10871 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10872 tree *next = &TREE_OPERAND (*ptr, 1);
10873 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10874 if (cur == empty_stmt_node)
10875 {
10876 /* Optimization; makes it easier to detect empty bodies.
10877 Most useful for <clinit> with all-constant initializer. */
10878 *ptr = *next;
10879 continue;
10880 }
15fdcfe9
PB
10881 if (TREE_CODE (cur) == ERROR_MARK)
10882 error_seen++;
10883 else if (! CAN_COMPLETE_NORMALLY (cur))
10884 {
10885 wfl_op2 = *next;
10886 for (;;)
10887 {
10888 if (TREE_CODE (wfl_op2) == BLOCK)
10889 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10890 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10891 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10892 else
10893 break;
10894 }
10895 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10896 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10897 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10898 }
10899 ptr = next;
10900 }
10901 *ptr = java_complete_tree (*ptr);
10902
10903 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10904 return error_mark_node;
15fdcfe9 10905 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10906 }
10907 /* Turn local bindings to null */
10908 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10909 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10910
10911 TREE_TYPE (node) = void_type_node;
10912 break;
10913
10914 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10915
b9f7e36c
APB
10916 case THROW_EXPR:
10917 wfl_op1 = TREE_OPERAND (node, 0);
10918 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10919 /* 14.19 A throw statement cannot complete normally. */
10920 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10921 return patch_throw_statement (node, wfl_op1);
10922
10923 case SYNCHRONIZED_EXPR:
10924 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10925 return patch_synchronized_statement (node, wfl_op1);
10926
b67d701b
PB
10927 case TRY_EXPR:
10928 return patch_try_statement (node);
10929
a7d8d81f
PB
10930 case TRY_FINALLY_EXPR:
10931 COMPLETE_CHECK_OP_0 (node);
10932 COMPLETE_CHECK_OP_1 (node);
10933 CAN_COMPLETE_NORMALLY (node)
10934 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10935 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10936 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10937 return node;
10938
5a005d9e
PB
10939 case CLEANUP_POINT_EXPR:
10940 COMPLETE_CHECK_OP_0 (node);
10941 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10942 CAN_COMPLETE_NORMALLY (node) =
10943 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10944 return node;
10945
10946 case WITH_CLEANUP_EXPR:
10947 COMPLETE_CHECK_OP_0 (node);
10948 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10949 CAN_COMPLETE_NORMALLY (node) =
10950 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10951 TREE_TYPE (node) = void_type_node;
10952 return node;
10953
e04a16fb
AG
10954 case LABELED_BLOCK_EXPR:
10955 PUSH_LABELED_BLOCK (node);
10956 if (LABELED_BLOCK_BODY (node))
10957 COMPLETE_CHECK_OP_1 (node);
10958 TREE_TYPE (node) = void_type_node;
10959 POP_LABELED_BLOCK ();
1fb89a4d
APB
10960
10961 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10962 {
10963 LABELED_BLOCK_BODY (node) = NULL_TREE;
10964 CAN_COMPLETE_NORMALLY (node) = 1;
10965 }
1fb89a4d 10966 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10967 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10968 return node;
10969
10970 case EXIT_BLOCK_EXPR:
10971 /* We don't complete operand 1, because it's the return value of
10972 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10973 return patch_bc_statement (node);
10974
15fdcfe9
PB
10975 case CASE_EXPR:
10976 cn = java_complete_tree (TREE_OPERAND (node, 0));
10977 if (cn == error_mark_node)
10978 return cn;
10979
8576f094
APB
10980 /* First, the case expression must be constant. Values of final
10981 fields are accepted. */
15fdcfe9 10982 cn = fold (cn);
8576f094
APB
10983 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10984 && JDECL_P (TREE_OPERAND (cn, 1))
10985 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10986 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
10987 {
10988 push_obstacks (&permanent_obstack, &permanent_obstack);
10989 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10990 TREE_OPERAND (cn, 1));
10991 pop_obstacks ();
10992 }
15fdcfe9 10993
ce6e9147 10994 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
10995 {
10996 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10997 parse_error_context (node, "Constant expression required");
10998 return error_mark_node;
10999 }
11000
11001 nn = ctxp->current_loop;
11002
11003 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11004 if (!try_builtin_assignconv (NULL_TREE,
11005 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11006 {
11007 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11008 parse_error_context
11009 (wfl_operator,
11010 "Incompatible type for case. Can't convert `%s' to `int'",
11011 lang_printable_name (TREE_TYPE (cn), 0));
11012 return error_mark_node;
11013 }
11014
11015 cn = fold (convert (int_type_node, cn));
11016
11017 /* Multiple instance of a case label bearing the same
11018 value is checked during code generation. The case
11019 expression is allright so far. */
34d4df06
APB
11020 if (TREE_CODE (cn) == VAR_DECL)
11021 cn = DECL_INITIAL (cn);
15fdcfe9 11022 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11023 TREE_TYPE (node) = void_type_node;
15fdcfe9 11024 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11025 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11026 break;
11027
11028 case DEFAULT_EXPR:
11029 nn = ctxp->current_loop;
11030 /* Only one default label is allowed per switch statement */
11031 if (SWITCH_HAS_DEFAULT (nn))
11032 {
11033 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11034 parse_error_context (wfl_operator,
11035 "Duplicate case label: `default'");
11036 return error_mark_node;
11037 }
11038 else
11039 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11040 TREE_TYPE (node) = void_type_node;
10100cc7 11041 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11042 CAN_COMPLETE_NORMALLY (node) = 1;
11043 break;
11044
b67d701b 11045 case SWITCH_EXPR:
e04a16fb
AG
11046 case LOOP_EXPR:
11047 PUSH_LOOP (node);
11048 /* Check whether the loop was enclosed in a labeled
11049 statement. If not, create one, insert the loop in it and
11050 return the node */
11051 nn = patch_loop_statement (node);
b67d701b 11052
e04a16fb 11053 /* Anyways, walk the body of the loop */
b67d701b
PB
11054 if (TREE_CODE (node) == LOOP_EXPR)
11055 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11056 /* Switch statement: walk the switch expression and the cases */
11057 else
11058 node = patch_switch_statement (node);
11059
e7c7bcef 11060 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11061 nn = error_mark_node;
11062 else
15fdcfe9 11063 {
b635eb2f
PB
11064 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11065 /* If we returned something different, that's because we
11066 inserted a label. Pop the label too. */
11067 if (nn != node)
11068 {
11069 if (CAN_COMPLETE_NORMALLY (node))
11070 CAN_COMPLETE_NORMALLY (nn) = 1;
11071 POP_LABELED_BLOCK ();
11072 }
15fdcfe9 11073 }
e04a16fb
AG
11074 POP_LOOP ();
11075 return nn;
11076
11077 case EXIT_EXPR:
11078 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11079 return patch_exit_expr (node);
11080
11081 case COND_EXPR:
11082 /* Condition */
11083 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11084 if (TREE_OPERAND (node, 0) == error_mark_node)
11085 return error_mark_node;
11086 /* then-else branches */
11087 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11088 if (TREE_OPERAND (node, 1) == error_mark_node)
11089 return error_mark_node;
11090 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11091 if (TREE_OPERAND (node, 2) == error_mark_node)
11092 return error_mark_node;
11093 return patch_if_else_statement (node);
11094 break;
11095
22eed1e6
APB
11096 case CONDITIONAL_EXPR:
11097 /* Condition */
11098 wfl_op1 = TREE_OPERAND (node, 0);
11099 COMPLETE_CHECK_OP_0 (node);
11100 wfl_op2 = TREE_OPERAND (node, 1);
11101 COMPLETE_CHECK_OP_1 (node);
11102 wfl_op3 = TREE_OPERAND (node, 2);
11103 COMPLETE_CHECK_OP_2 (node);
11104 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11105
e04a16fb
AG
11106 /* 3- Expression section */
11107 case COMPOUND_EXPR:
15fdcfe9 11108 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11109 TREE_OPERAND (node, 0) = nn =
11110 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11111 if (wfl_op2 == empty_stmt_node)
11112 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11113 else
15fdcfe9 11114 {
dc0b3eff 11115 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11116 {
dc0b3eff
PB
11117 /* An unreachable condition in a do-while statement
11118 is *not* (technically) an unreachable statement. */
11119 nn = wfl_op2;
11120 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11121 nn = EXPR_WFL_NODE (nn);
11122 if (TREE_CODE (nn) != EXIT_EXPR)
11123 {
11124 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11125 parse_error_context (wfl_operator, "Unreachable statement");
11126 }
bccaf73a 11127 }
dc0b3eff
PB
11128 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11129 if (TREE_OPERAND (node, 1) == error_mark_node)
11130 return error_mark_node;
11131 CAN_COMPLETE_NORMALLY (node)
11132 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11133 }
e04a16fb
AG
11134 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11135 break;
11136
11137 case RETURN_EXPR:
15fdcfe9 11138 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11139 return patch_return (node);
11140
11141 case EXPR_WITH_FILE_LOCATION:
11142 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11143 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11144 {
5423609c 11145 tree wfl = node;
15fdcfe9 11146 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11147 if (node == error_mark_node)
11148 return node;
5423609c
APB
11149 /* Keep line number information somewhere were it doesn't
11150 disrupt the completion process. */
2c56429a 11151 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11152 {
11153 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11154 TREE_OPERAND (node, 1) = wfl;
11155 }
15fdcfe9
PB
11156 CAN_COMPLETE_NORMALLY (node) = 1;
11157 }
e04a16fb
AG
11158 else
11159 {
5b09b33e
PB
11160 tree body;
11161 int save_lineno = lineno;
11162 lineno = EXPR_WFL_LINENO (node);
11163 body = java_complete_tree (EXPR_WFL_NODE (node));
11164 lineno = save_lineno;
15fdcfe9 11165 EXPR_WFL_NODE (node) = body;
dc0b3eff 11166 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11167 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11168 if (body == empty_stmt_node)
11169 {
11170 /* Optimization; makes it easier to detect empty bodies. */
11171 return body;
11172 }
dc0b3eff 11173 if (body == error_mark_node)
e04a16fb
AG
11174 {
11175 /* Its important for the evaluation of assignment that
11176 this mark on the TREE_TYPE is propagated. */
11177 TREE_TYPE (node) = error_mark_node;
11178 return error_mark_node;
11179 }
11180 else
11181 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11182
e04a16fb
AG
11183 }
11184 break;
11185
b67d701b 11186 case NEW_ARRAY_EXPR:
e04a16fb
AG
11187 /* Patch all the dimensions */
11188 flag = 0;
11189 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11190 {
11191 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11192 tree dim = convert (int_type_node,
11193 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11194 if (dim == error_mark_node)
11195 {
11196 flag = 1;
11197 continue;
11198 }
11199 else
11200 {
b9f7e36c 11201 TREE_VALUE (cn) = dim;
e04a16fb
AG
11202 /* Setup the location of the current dimension, for
11203 later error report. */
11204 TREE_PURPOSE (cn) =
11205 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11206 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11207 }
11208 }
11209 /* They complete the array creation expression, if no errors
11210 were found. */
15fdcfe9 11211 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11212 return (flag ? error_mark_node
11213 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11214
c2952b01
APB
11215 case NEW_ANONYMOUS_ARRAY_EXPR:
11216 /* Create the array type if necessary. */
11217 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11218 {
11219 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11220 if (!(type = resolve_type_during_patch (type)))
11221 return error_mark_node;
11222 type = build_array_from_name (type, NULL_TREE,
11223 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11224 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11225 }
11226 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11227 ANONYMOUS_ARRAY_INITIALIZER (node));
11228 if (node == error_mark_node)
11229 return error_mark_node;
11230 CAN_COMPLETE_NORMALLY (node) = 1;
11231 return node;
11232
b67d701b 11233 case NEW_CLASS_EXPR:
e04a16fb 11234 case CALL_EXPR:
b67d701b 11235 /* Complete function's argument(s) first */
e04a16fb
AG
11236 if (complete_function_arguments (node))
11237 return error_mark_node;
11238 else
b9f7e36c 11239 {
22eed1e6
APB
11240 tree decl, wfl = TREE_OPERAND (node, 0);
11241 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11242
c877974e 11243 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11244 NULL_TREE, 0, &decl);
c877974e
APB
11245 if (node == error_mark_node)
11246 return error_mark_node;
11247
11248 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11249 /* If we call this(...), register signature and positions */
11250 if (in_this)
11251 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11252 tree_cons (wfl, decl,
11253 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11254 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11255 return force_evaluation_order (node);
b9f7e36c 11256 }
e04a16fb
AG
11257
11258 case MODIFY_EXPR:
11259 /* Save potential wfls */
11260 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11261 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11262
cd9643f7
PB
11263 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11264 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11265 && DECL_INITIAL (nn) != NULL_TREE)
11266 {
100f7cd8
APB
11267 tree value;
11268
11269 push_obstacks (&permanent_obstack, &permanent_obstack);
11270 value = fold_constant_for_init (nn, nn);
11271 pop_obstacks ();
c2952b01 11272
cd9643f7
PB
11273 if (value != NULL_TREE)
11274 {
11275 tree type = TREE_TYPE (value);
c2952b01
APB
11276 if (JPRIMITIVE_TYPE_P (type) ||
11277 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11278 return empty_stmt_node;
11279 }
629d4b4d
APB
11280 if (! flag_emit_class_files)
11281 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11282 }
e04a16fb 11283 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11284
e04a16fb
AG
11285 if (TREE_OPERAND (node, 0) == error_mark_node)
11286 return error_mark_node;
11287
5cbdba64
APB
11288 flag = COMPOUND_ASSIGN_P (wfl_op2);
11289 if (flag)
e04a16fb 11290 {
c2952b01
APB
11291 /* This might break when accessing outer field from inner
11292 class. TESTME, FIXME */
2aa11e97 11293 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11294
11295 /* Hand stablize the lhs on both places */
e04a16fb 11296 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11297 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11298 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11299
5cbdba64 11300 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11301 /* Now complete the RHS. We write it back later on. */
11302 nn = java_complete_tree (TREE_OPERAND (node, 1));
11303
642f15d1
APB
11304 if ((cn = patch_string (nn)))
11305 nn = cn;
11306
2aa11e97
APB
11307 /* The last part of the rewrite for E1 op= E2 is to have
11308 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11309 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11310 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11311
11312 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11313 }
11314
f8976021 11315 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11316 function to complete this RHS. Note that a NEW_ARRAY_INIT
11317 might have been already fully expanded if created as a result
11318 of processing an anonymous array initializer. We avoid doing
11319 the operation twice by testing whether the node already bears
11320 a type. */
11321 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11322 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11323 TREE_OPERAND (node, 1));
2aa11e97 11324 /* Otherwise we simply complete the RHS */
f8976021
APB
11325 else
11326 nn = java_complete_tree (TREE_OPERAND (node, 1));
11327
e04a16fb 11328 if (nn == error_mark_node)
c0d87ff6 11329 return error_mark_node;
2aa11e97
APB
11330
11331 /* Write back the RHS as we evaluated it. */
e04a16fb 11332 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11333
11334 /* In case we're handling = with a String as a RHS, we need to
11335 produce a String out of the RHS (it might still be a
11336 STRING_CST or a StringBuffer at this stage */
11337 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11338 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11339
11340 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11341 TREE_OPERAND (node, 1))))
11342 {
11343 /* We return error_mark_node if outer_field_access_fix
11344 detects we write into a final. */
11345 if (nn == error_mark_node)
11346 return error_mark_node;
11347 node = nn;
11348 }
11349 else
11350 {
11351 node = patch_assignment (node, wfl_op1, wfl_op2);
11352 /* Reorganize the tree if necessary. */
11353 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11354 || JSTRING_P (TREE_TYPE (node))))
11355 node = java_refold (node);
11356 }
11357
15fdcfe9
PB
11358 CAN_COMPLETE_NORMALLY (node) = 1;
11359 return node;
e04a16fb
AG
11360
11361 case MULT_EXPR:
11362 case PLUS_EXPR:
11363 case MINUS_EXPR:
11364 case LSHIFT_EXPR:
11365 case RSHIFT_EXPR:
11366 case URSHIFT_EXPR:
11367 case BIT_AND_EXPR:
11368 case BIT_XOR_EXPR:
11369 case BIT_IOR_EXPR:
11370 case TRUNC_MOD_EXPR:
c2952b01 11371 case TRUNC_DIV_EXPR:
e04a16fb
AG
11372 case RDIV_EXPR:
11373 case TRUTH_ANDIF_EXPR:
11374 case TRUTH_ORIF_EXPR:
11375 case EQ_EXPR:
11376 case NE_EXPR:
11377 case GT_EXPR:
11378 case GE_EXPR:
11379 case LT_EXPR:
11380 case LE_EXPR:
11381 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11382 knows how to handle those cases. */
11383 wfl_op1 = TREE_OPERAND (node, 0);
11384 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11385
15fdcfe9 11386 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11387 /* Don't complete string nodes if dealing with the PLUS operand. */
11388 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11389 {
11390 nn = java_complete_tree (wfl_op1);
11391 if (nn == error_mark_node)
11392 return error_mark_node;
48a840d9 11393
2aa11e97
APB
11394 TREE_OPERAND (node, 0) = nn;
11395 }
b67d701b 11396 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11397 {
11398 nn = java_complete_tree (wfl_op2);
11399 if (nn == error_mark_node)
11400 return error_mark_node;
48a840d9 11401
2aa11e97
APB
11402 TREE_OPERAND (node, 1) = nn;
11403 }
dc0b3eff 11404 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11405
5e942c50
APB
11406 case INSTANCEOF_EXPR:
11407 wfl_op1 = TREE_OPERAND (node, 0);
11408 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11409 if (flag_emit_xref)
11410 {
11411 TREE_TYPE (node) = boolean_type_node;
11412 return node;
11413 }
5e942c50
APB
11414 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11415
b67d701b 11416 case UNARY_PLUS_EXPR:
e04a16fb
AG
11417 case NEGATE_EXPR:
11418 case TRUTH_NOT_EXPR:
11419 case BIT_NOT_EXPR:
11420 case PREDECREMENT_EXPR:
11421 case PREINCREMENT_EXPR:
11422 case POSTDECREMENT_EXPR:
11423 case POSTINCREMENT_EXPR:
11424 case CONVERT_EXPR:
11425 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11426 how to handle those cases. */
11427 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11428 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11429 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11430 if (TREE_OPERAND (node, 0) == error_mark_node)
11431 return error_mark_node;
4a5f66c3
APB
11432 node = patch_unaryop (node, wfl_op1);
11433 CAN_COMPLETE_NORMALLY (node) = 1;
11434 break;
e04a16fb
AG
11435
11436 case ARRAY_REF:
11437 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11438 how to handle those cases. */
11439 wfl_op1 = TREE_OPERAND (node, 0);
11440 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11441 if (TREE_OPERAND (node, 0) == error_mark_node)
11442 return error_mark_node;
7f1d4866 11443 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11444 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11445 /* The same applies to wfl_op2 */
11446 wfl_op2 = TREE_OPERAND (node, 1);
11447 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11448 if (TREE_OPERAND (node, 1) == error_mark_node)
11449 return error_mark_node;
7f1d4866 11450 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11451 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11452 return patch_array_ref (node);
e04a16fb 11453
63a212ed
PB
11454 case RECORD_TYPE:
11455 return node;;
11456
11457 case COMPONENT_REF:
11458 /* The first step in the re-write of qualified name handling. FIXME.
11459 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11460 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11461 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11462 {
11463 tree name = TREE_OPERAND (node, 1);
11464 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11465 if (field == NULL_TREE)
11466 {
11467 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11468 return error_mark_node;
11469 }
11470 if (! FIELD_STATIC (field))
11471 {
11472 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11473 return error_mark_node;
11474 }
11475 return field;
11476 }
11477 else
11478 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11479 break;
9bbc7d9f 11480
b67d701b 11481 case THIS_EXPR:
e04a16fb
AG
11482 /* Can't use THIS in a static environment */
11483 if (!current_this)
11484 {
11485 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11486 parse_error_context (wfl_operator,
11487 "Keyword `this' used outside allowed context");
e04a16fb
AG
11488 TREE_TYPE (node) = error_mark_node;
11489 return error_mark_node;
11490 }
22eed1e6
APB
11491 if (ctxp->explicit_constructor_p)
11492 {
11493 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11494 parse_error_context
781b0558 11495 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11496 TREE_TYPE (node) = error_mark_node;
11497 return error_mark_node;
11498 }
e04a16fb 11499 return current_this;
c2952b01
APB
11500
11501 case CLASS_LITERAL:
11502 CAN_COMPLETE_NORMALLY (node) = 1;
11503 node = patch_incomplete_class_ref (node);
11504 if (node == error_mark_node)
11505 return error_mark_node;
11506 break;
11507
11508 case INSTANCE_INITIALIZERS_EXPR:
11509 in_instance_initializer++;
11510 node = java_complete_tree (TREE_OPERAND (node, 0));
11511 in_instance_initializer--;
11512 if (node != error_mark_node)
11513 TREE_TYPE (node) = void_type_node;
11514 else
11515 return error_mark_node;
11516 break;
e04a16fb 11517
e04a16fb 11518 default:
15fdcfe9 11519 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11520 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11521 and it's time to turn it into the appropriate String object */
11522 if ((nn = patch_string (node)))
11523 node = nn;
11524 else
11525 fatal ("No case for tree code `%s' - java_complete_tree\n",
11526 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11527 }
11528 return node;
11529}
11530
11531/* Complete function call's argument. Return a non zero value is an
11532 error was found. */
11533
11534static int
11535complete_function_arguments (node)
11536 tree node;
11537{
11538 int flag = 0;
11539 tree cn;
11540
f63991a8 11541 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11542 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11543 {
b67d701b 11544 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11545 parm = java_complete_tree (wfl);
c2952b01 11546
e04a16fb
AG
11547 if (parm == error_mark_node)
11548 {
11549 flag = 1;
11550 continue;
11551 }
b67d701b
PB
11552 /* If have a string literal that we haven't transformed yet or a
11553 crafted string buffer, as a result of use of the the String
11554 `+' operator. Build `parm.toString()' and expand it. */
11555 if ((temp = patch_string (parm)))
b9f7e36c 11556 parm = temp;
5e942c50
APB
11557 /* Inline PRIMTYPE.TYPE read access */
11558 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11559
5e942c50 11560 TREE_VALUE (cn) = parm;
e04a16fb 11561 }
f63991a8 11562 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11563 return flag;
11564}
11565
11566/* Sometimes (for loops and variable initialized during their
11567 declaration), we want to wrap a statement around a WFL and turn it
11568 debugable. */
11569
11570static tree
11571build_debugable_stmt (location, stmt)
11572 int location;
11573 tree stmt;
11574{
11575 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11576 {
11577 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11578 EXPR_WFL_LINECOL (stmt) = location;
11579 }
11580 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11581 return stmt;
11582}
11583
11584static tree
11585build_expr_block (body, decls)
11586 tree body, decls;
11587{
11588 tree node = make_node (BLOCK);
11589 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11590 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11591 if (body)
11592 TREE_TYPE (node) = TREE_TYPE (body);
11593 TREE_SIDE_EFFECTS (node) = 1;
11594 return node;
11595}
11596
b67d701b
PB
11597/* Create a new function block and link it approriately to current
11598 function block chain */
e04a16fb
AG
11599
11600static tree
11601enter_block ()
11602{
b67d701b
PB
11603 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11604}
11605
11606/* Link block B supercontext to the previous block. The current
11607 function DECL is used as supercontext when enter_a_block is called
11608 for the first time for a given function. The current function body
11609 (DECL_FUNCTION_BODY) is set to be block B. */
11610
11611static tree
11612enter_a_block (b)
11613 tree b;
11614{
e04a16fb
AG
11615 tree fndecl = current_function_decl;
11616
f099f336
APB
11617 if (!fndecl) {
11618 BLOCK_SUPERCONTEXT (b) = current_static_block;
11619 current_static_block = b;
11620 }
11621
11622 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11623 {
11624 BLOCK_SUPERCONTEXT (b) = fndecl;
11625 DECL_FUNCTION_BODY (fndecl) = b;
11626 }
11627 else
11628 {
11629 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11630 DECL_FUNCTION_BODY (fndecl) = b;
11631 }
11632 return b;
11633}
11634
11635/* Exit a block by changing the current function body
11636 (DECL_FUNCTION_BODY) to the current block super context, only if
11637 the block being exited isn't the method's top level one. */
11638
11639static tree
11640exit_block ()
11641{
f099f336
APB
11642 tree b;
11643 if (current_function_decl)
11644 {
11645 b = DECL_FUNCTION_BODY (current_function_decl);
11646 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11647 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11648 }
11649 else
11650 {
11651 b = current_static_block;
e04a16fb 11652
f099f336
APB
11653 if (BLOCK_SUPERCONTEXT (b))
11654 current_static_block = BLOCK_SUPERCONTEXT (b);
11655 }
e04a16fb
AG
11656 return b;
11657}
11658
11659/* Lookup for NAME in the nested function's blocks, all the way up to
11660 the current toplevel one. It complies with Java's local variable
11661 scoping rules. */
11662
11663static tree
11664lookup_name_in_blocks (name)
11665 tree name;
11666{
f099f336 11667 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11668
11669 while (b != current_function_decl)
11670 {
11671 tree current;
11672
11673 /* Paranoid sanity check. To be removed */
11674 if (TREE_CODE (b) != BLOCK)
11675 fatal ("non block expr function body - lookup_name_in_blocks");
11676
11677 for (current = BLOCK_EXPR_DECLS (b); current;
11678 current = TREE_CHAIN (current))
11679 if (DECL_NAME (current) == name)
11680 return current;
11681 b = BLOCK_SUPERCONTEXT (b);
11682 }
11683 return NULL_TREE;
11684}
11685
11686static void
11687maybe_absorb_scoping_blocks ()
11688{
f099f336 11689 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11690 {
11691 tree b = exit_block ();
11692 java_method_add_stmt (current_function_decl, b);
11693 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11694 }
11695}
11696
11697\f
11698/* This section of the source is reserved to build_* functions that
11699 are building incomplete tree nodes and the patch_* functions that
11700 are completing them. */
11701
c2952b01
APB
11702/* Wrap a non WFL node around a WFL. */
11703static tree
9a7ab4b3 11704build_wfl_wrap (node, location)
c2952b01 11705 tree node;
9a7ab4b3 11706 int location;
c2952b01
APB
11707{
11708 tree wfl, node_to_insert = node;
11709
11710 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11711 with the way we're processing SUPER. A THIS from a primary as a
11712 different form than a SUPER. Turn THIS into something symbolic */
11713 if (TREE_CODE (node) == THIS_EXPR)
11714 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11715 else
11716 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11717
9a7ab4b3 11718 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11719 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11720 return wfl;
11721}
11722
11723
9bbc7d9f 11724/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11725 we're currently dealing with the class java.lang.Object. */
11726
11727static tree
e920ebc9
APB
11728build_super_invocation (mdecl)
11729 tree mdecl;
22eed1e6 11730{
e920ebc9 11731 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11732 return empty_stmt_node;
22eed1e6
APB
11733 else
11734 {
9ee9b555 11735 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11736 tree a = NULL_TREE, t;
11737 /* If we're dealing with an anonymous class, pass the arguments
11738 of the crafted constructor along. */
11739 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11740 {
11741 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11742 for (; t != end_params_node; t = TREE_CHAIN (t))
11743 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11744 }
11745 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11746 }
11747}
11748
11749/* Build a SUPER/THIS qualified method invocation. */
11750
11751static tree
11752build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11753 int use_this;
11754 tree name, args;
11755 int lloc, rloc;
22eed1e6
APB
11756{
11757 tree invok;
11758 tree wfl =
9ee9b555 11759 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11760 EXPR_WFL_LINECOL (wfl) = lloc;
11761 invok = build_method_invocation (name, args);
11762 return make_qualified_primary (wfl, invok, rloc);
11763}
11764
b67d701b 11765/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11766
11767static tree
11768build_method_invocation (name, args)
11769 tree name;
11770 tree args;
11771{
11772 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11773 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11774 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11775 return call;
11776}
11777
11778/* Build an incomplete new xxx(...) node. */
11779
11780static tree
11781build_new_invocation (name, args)
11782 tree name, args;
11783{
11784 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11785 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11786 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11787 return call;
11788}
11789
11790/* Build an incomplete assignment expression. */
11791
11792static tree
11793build_assignment (op, op_location, lhs, rhs)
11794 int op, op_location;
11795 tree lhs, rhs;
11796{
11797 tree assignment;
11798 /* Build the corresponding binop if we deal with a Compound
11799 Assignment operator. Mark the binop sub-tree as part of a
11800 Compound Assignment expression */
11801 if (op != ASSIGN_TK)
11802 {
11803 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11804 COMPOUND_ASSIGN_P (rhs) = 1;
11805 }
11806 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11807 TREE_SIDE_EFFECTS (assignment) = 1;
11808 EXPR_WFL_LINECOL (assignment) = op_location;
11809 return assignment;
11810}
11811
11812/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11813
15fdcfe9 11814char *
e04a16fb
AG
11815print_int_node (node)
11816 tree node;
11817{
11818 static char buffer [80];
11819 if (TREE_CONSTANT_OVERFLOW (node))
11820 sprintf (buffer, "<overflow>");
11821
11822 if (TREE_INT_CST_HIGH (node) == 0)
11823 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11824 TREE_INT_CST_LOW (node));
11825 else if (TREE_INT_CST_HIGH (node) == -1
11826 && TREE_INT_CST_LOW (node) != 0)
11827 {
11828 buffer [0] = '-';
11829 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11830 -TREE_INT_CST_LOW (node));
11831 }
11832 else
11833 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11834 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11835
11836 return buffer;
11837}
11838
7f1d4866
APB
11839/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11840 context. */
5e942c50
APB
11841
11842static int
11843check_final_assignment (lvalue, wfl)
11844 tree lvalue, wfl;
11845{
6632dcdd
APB
11846 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11847 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11848 lvalue = TREE_OPERAND (lvalue, 1);
11849
bc2874c9
TT
11850 /* When generating class files, references to the `length' field
11851 look a bit different. */
11852 if ((flag_emit_class_files
11853 && TREE_CODE (lvalue) == COMPONENT_REF
11854 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11855 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11856 || (TREE_CODE (lvalue) == FIELD_DECL
11857 && FIELD_FINAL (lvalue)
11858 && !DECL_CLINIT_P (current_function_decl)
11859 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11860 {
11861 parse_error_context
11862 (wfl, "Can't assign a value to the final variable `%s'",
11863 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11864 return 1;
11865 }
11866 return 0;
11867}
11868
11869/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11870 read. This is needed to avoid circularities in the implementation
11871 of these fields in libjava. */
11872
11873static tree
11874maybe_build_primttype_type_ref (rhs, wfl)
11875 tree rhs, wfl;
11876{
11877 tree to_return = NULL_TREE;
11878 tree rhs_type = TREE_TYPE (rhs);
11879 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11880 {
11881 tree n = TREE_OPERAND (rhs, 1);
11882 if (TREE_CODE (n) == VAR_DECL
11883 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
11884 && rhs_type == class_ptr_type
11885 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 11886 {
49f48c71 11887 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11888 if (!strncmp (self_name, "java.lang.", 10))
11889 to_return = build_primtype_type_ref (self_name);
11890 }
11891 }
11892 return (to_return ? to_return : rhs );
11893}
11894
e04a16fb
AG
11895/* 15.25 Assignment operators. */
11896
11897static tree
11898patch_assignment (node, wfl_op1, wfl_op2)
11899 tree node;
11900 tree wfl_op1;
11901 tree wfl_op2;
11902{
0a2138e2 11903 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11904 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11905 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11906 int error_found = 0;
11907 int lvalue_from_array = 0;
11908
c2952b01 11909 /* Can't assign to a (blank) final. */
5e942c50
APB
11910 if (check_final_assignment (lvalue, wfl_op1))
11911 error_found = 1;
e04a16fb
AG
11912
11913 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11914
11915 /* Lhs can be a named variable */
34f4db93 11916 if (JDECL_P (lvalue))
e04a16fb 11917 {
e04a16fb
AG
11918 lhs_type = TREE_TYPE (lvalue);
11919 }
11920 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11921 comment on reason why */
11922 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11923 {
11924 lhs_type = TREE_TYPE (lvalue);
11925 lvalue_from_array = 1;
11926 }
11927 /* Or a field access */
11928 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11929 lhs_type = TREE_TYPE (lvalue);
11930 /* Or a function return slot */
11931 else if (TREE_CODE (lvalue) == RESULT_DECL)
11932 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11933 /* Otherwise, we might want to try to write into an optimized static
11934 final, this is an of a different nature, reported further on. */
11935 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11936 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11937 {
6632dcdd 11938 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11939 {
11940 /* What we should do instead is resetting the all the flags
11941 previously set, exchange lvalue for llvalue and continue. */
11942 error_found = 1;
11943 return error_mark_node;
11944 }
11945 else
11946 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11947 }
11948 else
e04a16fb
AG
11949 {
11950 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11951 error_found = 1;
11952 }
11953
11954 rhs_type = TREE_TYPE (rhs);
b67d701b 11955 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11956 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11957
b67d701b 11958 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11959 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11960 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11961
11962 /* 15.25.2 If we have a compound assignment, convert RHS into the
11963 type of the LHS */
11964 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11965 new_rhs = convert (lhs_type, rhs);
11966
11967 /* Explicit cast required. This is an error */
11968 if (!new_rhs)
11969 {
c2e3db92
KG
11970 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11971 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11972 tree wfl;
11973 char operation [32]; /* Max size known */
11974
11975 /* If the assignment is part of a declaration, we use the WFL of
11976 the declared variable to point out the error and call it a
11977 declaration problem. If the assignment is a genuine =
11978 operator, we call is a operator `=' problem, otherwise we
11979 call it an assignment problem. In both of these last cases,
11980 we use the WFL of the operator to indicate the error. */
11981
11982 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11983 {
11984 wfl = wfl_op1;
11985 strcpy (operation, "declaration");
11986 }
11987 else
11988 {
11989 wfl = wfl_operator;
11990 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11991 strcpy (operation, "assignment");
11992 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11993 strcpy (operation, "`return'");
11994 else
11995 strcpy (operation, "`='");
11996 }
11997
1ebadc60 11998 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
11999 parse_error_context
12000 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12001 operation, t1, t2);
1ebadc60 12002 else
781b0558 12003 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12004 operation, t1, t2);
e04a16fb
AG
12005 free (t1); free (t2);
12006 error_found = 1;
12007 }
12008
c877974e
APB
12009 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12010 if (new_rhs)
12011 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12012
e04a16fb
AG
12013 if (error_found)
12014 return error_mark_node;
12015
2622b947
APB
12016 /* 10.10: Array Store Exception runtime check */
12017 if (!flag_emit_class_files
e8fc7396 12018 && !flag_emit_xref
2622b947 12019 && lvalue_from_array
afc390b1 12020 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12021 {
12022 tree check;
12023 tree base = lvalue;
12024
12025 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12026 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12027 base = TREE_OPERAND (lvalue, 0);
12028 else
12029 {
12030 if (flag_bounds_check)
12031 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12032 else
12033 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12034 }
12035
12036 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12037 new_rhs = save_expr (new_rhs);
2622b947
APB
12038 check = build (CALL_EXPR, void_type_node,
12039 build_address_of (soft_checkarraystore_node),
12040 tree_cons (NULL_TREE, base,
12041 build_tree_list (NULL_TREE, new_rhs)),
12042 NULL_TREE);
12043 TREE_SIDE_EFFECTS (check) = 1;
12044
12045 /* We have to decide on an insertion point */
12046 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12047 {
12048 tree t;
12049 if (flag_bounds_check)
12050 {
12051 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12052 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12053 build (COMPOUND_EXPR, void_type_node, t, check);
12054 }
12055 else
12056 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12057 check, TREE_OPERAND (lvalue, 1));
12058 }
12059 else
12060 {
12061 /* Make sure the bound check will happen before the store check */
12062 if (flag_bounds_check)
12063 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12064 build (COMPOUND_EXPR, void_type_node,
12065 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12066 else
12067 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12068 }
12069 }
22eed1e6 12070
34d4df06
APB
12071 /* Final locals can be used as case values in switch
12072 statement. Prepare them for this eventuality. */
12073 if (TREE_CODE (lvalue) == VAR_DECL
12074 && LOCAL_FINAL (lvalue)
12075 && TREE_CONSTANT (new_rhs)
12076 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12077 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12078 )
12079 {
12080 TREE_CONSTANT (lvalue) = 1;
12081 DECL_INITIAL (lvalue) = new_rhs;
12082 }
12083
e04a16fb
AG
12084 TREE_OPERAND (node, 0) = lvalue;
12085 TREE_OPERAND (node, 1) = new_rhs;
12086 TREE_TYPE (node) = lhs_type;
12087 return node;
12088}
12089
b67d701b
PB
12090/* Check that type SOURCE can be cast into type DEST. If the cast
12091 can't occur at all, return 0 otherwise 1. This function is used to
12092 produce accurate error messages on the reasons why an assignment
12093 failed. */
e04a16fb 12094
b67d701b
PB
12095static tree
12096try_reference_assignconv (lhs_type, rhs)
12097 tree lhs_type, rhs;
e04a16fb 12098{
b67d701b
PB
12099 tree new_rhs = NULL_TREE;
12100 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12101
b67d701b
PB
12102 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12103 {
12104 /* `null' may be assigned to any reference type */
12105 if (rhs == null_pointer_node)
12106 new_rhs = null_pointer_node;
12107 /* Try the reference assignment conversion */
12108 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12109 new_rhs = rhs;
12110 /* This is a magic assignment that we process differently */
12111 else if (rhs == soft_exceptioninfo_call_node)
12112 new_rhs = rhs;
12113 }
12114 return new_rhs;
12115}
12116
12117/* Check that RHS can be converted into LHS_TYPE by the assignment
12118 conversion (5.2), for the cases of RHS being a builtin type. Return
12119 NULL_TREE if the conversion fails or if because RHS isn't of a
12120 builtin type. Return a converted RHS if the conversion is possible. */
12121
12122static tree
12123try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12124 tree wfl_op1, lhs_type, rhs;
12125{
12126 tree new_rhs = NULL_TREE;
12127 tree rhs_type = TREE_TYPE (rhs);
12128
5e942c50
APB
12129 /* Zero accepted everywhere */
12130 if (TREE_CODE (rhs) == INTEGER_CST
12131 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12132 && JPRIMITIVE_TYPE_P (rhs_type))
12133 new_rhs = convert (lhs_type, rhs);
12134
b67d701b
PB
12135 /* 5.1.1 Try Identity Conversion,
12136 5.1.2 Try Widening Primitive Conversion */
5e942c50 12137 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12138 new_rhs = convert (lhs_type, rhs);
12139
12140 /* Try a narrowing primitive conversion (5.1.3):
12141 - expression is a constant expression of type int AND
12142 - variable is byte, short or char AND
12143 - The value of the expression is representable in the type of the
12144 variable */
12145 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12146 && (lhs_type == byte_type_node || lhs_type == char_type_node
12147 || lhs_type == short_type_node))
12148 {
12149 if (int_fits_type_p (rhs, lhs_type))
12150 new_rhs = convert (lhs_type, rhs);
12151 else if (wfl_op1) /* Might be called with a NULL */
12152 parse_warning_context
781b0558 12153 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 12154 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12155 /* Reported a warning that will turn into an error further
12156 down, so we don't return */
12157 }
12158
12159 return new_rhs;
12160}
12161
12162/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12163 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
12164 0 is the conversion test fails. This implements parts the method
12165 invocation convertion (5.3). */
12166
12167static int
12168valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12169 tree lhs_type, rhs_type;
12170{
acd663ee 12171 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12172 if (lhs_type == rhs_type)
12173 return 1;
12174
acd663ee
APB
12175 /* Reject non primitive types */
12176 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
12177 return 0;
12178
acd663ee
APB
12179 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12180 than a char can't be converted into a char. Short can't too, but
12181 the < test below takes care of that */
b67d701b
PB
12182 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12183 return 0;
12184
5e942c50
APB
12185 /* Accept all promoted type here. Note, we can't use <= in the test
12186 below, because we still need to bounce out assignments of short
12187 to char and the likes */
12188 if (lhs_type == int_type_node
12189 && (rhs_type == promoted_byte_type_node
12190 || rhs_type == promoted_short_type_node
12191 || rhs_type == promoted_char_type_node
12192 || rhs_type == promoted_boolean_type_node))
12193 return 1;
12194
acd663ee
APB
12195 /* From here, an integral is widened if its precision is smaller
12196 than the precision of the LHS or if the LHS is a floating point
12197 type, or the RHS is a float and the RHS a double. */
12198 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12199 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12200 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12201 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12202 return 1;
12203
12204 return 0;
e04a16fb
AG
12205}
12206
12207/* Check that something of SOURCE type can be assigned or cast to
12208 something of DEST type at runtime. Return 1 if the operation is
12209 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12210 were SOURCE is cast into DEST, which borrows a lot of the
12211 assignment check. */
12212
12213static int
12214valid_ref_assignconv_cast_p (source, dest, cast)
12215 tree source;
12216 tree dest;
12217 int cast;
12218{
09ed0f70
APB
12219 /* SOURCE or DEST might be null if not from a declared entity. */
12220 if (!source || !dest)
12221 return 0;
5e942c50
APB
12222 if (JNULLP_TYPE_P (source))
12223 return 1;
e04a16fb
AG
12224 if (TREE_CODE (source) == POINTER_TYPE)
12225 source = TREE_TYPE (source);
12226 if (TREE_CODE (dest) == POINTER_TYPE)
12227 dest = TREE_TYPE (dest);
12228 /* Case where SOURCE is a class type */
12229 if (TYPE_CLASS_P (source))
12230 {
12231 if (TYPE_CLASS_P (dest))
c2952b01
APB
12232 return (source == dest
12233 || inherits_from_p (source, dest)
c2952b01 12234 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12235 if (TYPE_INTERFACE_P (dest))
12236 {
12237 /* If doing a cast and SOURCE is final, the operation is
12238 always correct a compile time (because even if SOURCE
12239 does not implement DEST, a subclass of SOURCE might). */
12240 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12241 return 1;
12242 /* Otherwise, SOURCE must implement DEST */
12243 return interface_of_p (dest, source);
12244 }
12245 /* DEST is an array, cast permited if SOURCE is of Object type */
12246 return (cast && source == object_type_node ? 1 : 0);
12247 }
12248 if (TYPE_INTERFACE_P (source))
12249 {
12250 if (TYPE_CLASS_P (dest))
12251 {
12252 /* If not casting, DEST must be the Object type */
12253 if (!cast)
12254 return dest == object_type_node;
12255 /* We're doing a cast. The cast is always valid is class
12256 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12257 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12258 return 1;
12259 else
12260 return interface_of_p (source, dest);
12261 }
12262 if (TYPE_INTERFACE_P (dest))
12263 {
12264 /* If doing a cast, then if SOURCE and DEST contain method
12265 with the same signature but different return type, then
12266 this is a (compile time) error */
12267 if (cast)
12268 {
12269 tree method_source, method_dest;
12270 tree source_type;
0a2138e2 12271 tree source_sig;
e04a16fb
AG
12272 tree source_name;
12273 for (method_source = TYPE_METHODS (source); method_source;
12274 method_source = TREE_CHAIN (method_source))
12275 {
12276 source_sig =
12277 build_java_argument_signature (TREE_TYPE (method_source));
12278 source_type = TREE_TYPE (TREE_TYPE (method_source));
12279 source_name = DECL_NAME (method_source);
12280 for (method_dest = TYPE_METHODS (dest);
12281 method_dest; method_dest = TREE_CHAIN (method_dest))
12282 if (source_sig ==
12283 build_java_argument_signature (TREE_TYPE (method_dest))
12284 && source_name == DECL_NAME (method_dest)
12285 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12286 return 0;
12287 }
12288 return 1;
12289 }
12290 else
12291 return source == dest || interface_of_p (dest, source);
12292 }
12293 else /* Array */
93024893
APB
12294 return (cast ?
12295 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
12296 }
12297 if (TYPE_ARRAY_P (source))
12298 {
12299 if (TYPE_CLASS_P (dest))
12300 return dest == object_type_node;
09ed0f70
APB
12301 /* Can't cast an array to an interface unless the interface is
12302 java.lang.Cloneable */
e04a16fb 12303 if (TYPE_INTERFACE_P (dest))
09ed0f70 12304 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
12305 else /* Arrays */
12306 {
12307 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12308 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12309
b9f7e36c
APB
12310 /* In case of severe errors, they turn out null */
12311 if (!dest_element_type || !source_element_type)
12312 return 0;
e04a16fb
AG
12313 if (source_element_type == dest_element_type)
12314 return 1;
12315 return valid_ref_assignconv_cast_p (source_element_type,
12316 dest_element_type, cast);
12317 }
12318 return 0;
12319 }
12320 return 0;
12321}
12322
b67d701b
PB
12323static int
12324valid_cast_to_p (source, dest)
12325 tree source;
12326 tree dest;
12327{
12328 if (TREE_CODE (source) == POINTER_TYPE)
12329 source = TREE_TYPE (source);
12330 if (TREE_CODE (dest) == POINTER_TYPE)
12331 dest = TREE_TYPE (dest);
12332
12333 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12334 return valid_ref_assignconv_cast_p (source, dest, 1);
12335
12336 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12337 return 1;
12338
12339 return 0;
12340}
12341
12342/* Method invocation conversion test. Return 1 if type SOURCE can be
12343 converted to type DEST through the methond invocation conversion
12344 process (5.3) */
12345
15fdcfe9
PB
12346static tree
12347do_unary_numeric_promotion (arg)
12348 tree arg;
12349{
12350 tree type = TREE_TYPE (arg);
12351 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12352 : TREE_CODE (type) == CHAR_TYPE)
12353 arg = convert (int_type_node, arg);
12354 return arg;
12355}
12356
acd663ee
APB
12357/* Return a non zero value if SOURCE can be converted into DEST using
12358 the method invocation conversion rule (5.3). */
b67d701b
PB
12359static int
12360valid_method_invocation_conversion_p (dest, source)
12361 tree dest, source;
12362{
e3884b71 12363 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12364 && valid_builtin_assignconv_identity_widening_p (dest, source))
12365 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12366 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12367 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12368}
12369
e04a16fb
AG
12370/* Build an incomplete binop expression. */
12371
12372static tree
12373build_binop (op, op_location, op1, op2)
12374 enum tree_code op;
12375 int op_location;
12376 tree op1, op2;
12377{
5e942c50 12378 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12379 TREE_SIDE_EFFECTS (binop) = 1;
12380 /* Store the location of the operator, for better error report. The
12381 string of the operator will be rebuild based on the OP value. */
12382 EXPR_WFL_LINECOL (binop) = op_location;
12383 return binop;
12384}
12385
12386/* Build the string of the operator retained by NODE. If NODE is part
12387 of a compound expression, add an '=' at the end of the string. This
12388 function is called when an error needs to be reported on an
12389 operator. The string is returned as a pointer to a static character
12390 buffer. */
12391
12392static char *
12393operator_string (node)
12394 tree node;
12395{
12396#define BUILD_OPERATOR_STRING(S) \
12397 { \
12398 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12399 return buffer; \
12400 }
12401
12402 static char buffer [10];
12403 switch (TREE_CODE (node))
12404 {
12405 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12406 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12407 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12408 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12409 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12410 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12411 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12412 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12413 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12414 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12415 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12416 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12417 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12418 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12419 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12420 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12421 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12422 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12423 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12424 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12425 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12426 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12427 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12428 case PREINCREMENT_EXPR: /* Fall through */
12429 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12430 case PREDECREMENT_EXPR: /* Fall through */
12431 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12432 default:
12433 fatal ("unregistered operator %s - operator_string",
12434 tree_code_name [TREE_CODE (node)]);
12435 }
12436 return NULL;
12437#undef BUILD_OPERATOR_STRING
12438}
12439
5cbdba64
APB
12440/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12441
12442static int
12443java_decl_equiv (var_acc1, var_acc2)
12444 tree var_acc1, var_acc2;
12445{
12446 if (JDECL_P (var_acc1))
12447 return (var_acc1 == var_acc2);
12448
12449 return (TREE_CODE (var_acc1) == COMPONENT_REF
12450 && TREE_CODE (var_acc2) == COMPONENT_REF
12451 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12452 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12453 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12454}
12455
12456/* Return a non zero value if CODE is one of the operators that can be
12457 used in conjunction with the `=' operator in a compound assignment. */
12458
12459static int
12460binop_compound_p (code)
12461 enum tree_code code;
12462{
12463 int i;
12464 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12465 if (binop_lookup [i] == code)
12466 break;
12467
12468 return i < BINOP_COMPOUND_CANDIDATES;
12469}
12470
12471/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12472
12473static tree
12474java_refold (t)
12475 tree t;
12476{
12477 tree c, b, ns, decl;
12478
12479 if (TREE_CODE (t) != MODIFY_EXPR)
12480 return t;
12481
12482 c = TREE_OPERAND (t, 1);
12483 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12484 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12485 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12486 return t;
12487
12488 /* Now the left branch of the binary operator. */
12489 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12490 if (! (b && TREE_CODE (b) == NOP_EXPR
12491 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12492 return t;
12493
12494 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12495 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12496 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12497 return t;
12498
12499 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12500 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12501 /* It's got to be the an equivalent decl */
12502 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12503 {
12504 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12505 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12506 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12507 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12508 /* Change the right part of the BINOP_EXPR */
12509 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12510 }
12511
12512 return t;
12513}
12514
e04a16fb
AG
12515/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12516 errors but we modify NODE so that it contains the type computed
12517 according to the expression, when it's fixed. Otherwise, we write
12518 error_mark_node as the type. It allows us to further the analysis
12519 of remaining nodes and detects more errors in certain cases. */
12520
12521static tree
12522patch_binop (node, wfl_op1, wfl_op2)
12523 tree node;
12524 tree wfl_op1;
12525 tree wfl_op2;
12526{
12527 tree op1 = TREE_OPERAND (node, 0);
12528 tree op2 = TREE_OPERAND (node, 1);
12529 tree op1_type = TREE_TYPE (op1);
12530 tree op2_type = TREE_TYPE (op2);
48a840d9 12531 tree prom_type = NULL_TREE, cn;
e04a16fb 12532 int code = TREE_CODE (node);
b67d701b 12533
e04a16fb
AG
12534 /* If 1, tell the routine that we have to return error_mark_node
12535 after checking for the initialization of the RHS */
12536 int error_found = 0;
12537
e04a16fb
AG
12538 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12539
e04a16fb
AG
12540 switch (code)
12541 {
12542 /* 15.16 Multiplicative operators */
12543 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12544 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12545 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12546 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12547 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12548 {
12549 if (!JPRIMITIVE_TYPE_P (op1_type))
12550 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12551 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12552 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12553 TREE_TYPE (node) = error_mark_node;
12554 error_found = 1;
12555 break;
12556 }
12557 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12558 /* Change the division operator if necessary */
12559 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12560 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12561
aa4759c1
AH
12562 if (TREE_CODE (prom_type) == INTEGER_TYPE
12563 && flag_use_divide_subroutine
12564 && ! flag_emit_class_files
12565 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12566 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12567
0b4d333e
APB
12568 /* This one is more complicated. FLOATs are processed by a
12569 function call to soft_fmod. Duplicate the value of the
12570 COMPOUND_ASSIGN_P flag. */
e04a16fb 12571 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12572 {
12573 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12574 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12575 TREE_SIDE_EFFECTS (mod)
12576 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12577 return mod;
12578 }
e04a16fb
AG
12579 break;
12580
12581 /* 15.17 Additive Operators */
12582 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12583
12584 /* Operation is valid if either one argument is a string
12585 constant, a String object or a StringBuffer crafted for the
12586 purpose of the a previous usage of the String concatenation
12587 operator */
12588
12589 if (TREE_CODE (op1) == STRING_CST
12590 || TREE_CODE (op2) == STRING_CST
12591 || JSTRING_TYPE_P (op1_type)
12592 || JSTRING_TYPE_P (op2_type)
12593 || IS_CRAFTED_STRING_BUFFER_P (op1)
12594 || IS_CRAFTED_STRING_BUFFER_P (op2))
12595 return build_string_concatenation (op1, op2);
12596
e04a16fb
AG
12597 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12598 Numeric Types */
12599 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12600 {
12601 if (!JPRIMITIVE_TYPE_P (op1_type))
12602 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12603 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12604 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12605 TREE_TYPE (node) = error_mark_node;
12606 error_found = 1;
12607 break;
12608 }
12609 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12610 break;
12611
12612 /* 15.18 Shift Operators */
12613 case LSHIFT_EXPR:
12614 case RSHIFT_EXPR:
12615 case URSHIFT_EXPR:
12616 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12617 {
12618 if (!JINTEGRAL_TYPE_P (op1_type))
12619 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12620 else
1ebadc60
KG
12621 {
12622 if (JPRIMITIVE_TYPE_P (op2_type))
12623 parse_error_context (wfl_operator,
781b0558 12624 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12625 operator_string (node),
12626 lang_printable_name (op2_type, 0));
12627 else
781b0558
KG
12628 parse_error_context (wfl_operator,
12629 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12630 operator_string (node),
12631 lang_printable_name (op2_type, 0));
12632 }
e04a16fb
AG
12633 TREE_TYPE (node) = error_mark_node;
12634 error_found = 1;
12635 break;
12636 }
12637
12638 /* Unary numeric promotion (5.6.1) is performed on each operand
12639 separatly */
15fdcfe9
PB
12640 op1 = do_unary_numeric_promotion (op1);
12641 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12642
12643 /* The type of the shift expression is the type of the promoted
12644 type of the left-hand operand */
12645 prom_type = TREE_TYPE (op1);
12646
c2952b01
APB
12647 /* Shift int only up to 0x1f and long up to 0x3f */
12648 if (prom_type == int_type_node)
12649 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12650 build_int_2 (0x1f, 0)));
12651 else
12652 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12653 build_int_2 (0x3f, 0)));
e04a16fb
AG
12654
12655 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12656 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12657 {
0b4d333e 12658 tree to_return;
73333a87
AH
12659 tree utype = unsigned_type (prom_type);
12660 op1 = convert (utype, op1);
e04a16fb 12661 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12662 TREE_OPERAND (node, 0) = op1;
12663 TREE_OPERAND (node, 1) = op2;
12664 TREE_TYPE (node) = utype;
0b4d333e
APB
12665 to_return = convert (prom_type, node);
12666 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12667 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12668 TREE_SIDE_EFFECTS (to_return)
12669 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12670 return to_return;
e04a16fb
AG
12671 }
12672 break;
5e942c50
APB
12673
12674 /* 15.19.1 Type Comparison Operator instaceof */
12675 case INSTANCEOF_EXPR:
12676
12677 TREE_TYPE (node) = boolean_type_node;
12678
12679 if (!(op2_type = resolve_type_during_patch (op2)))
12680 return error_mark_node;
12681
12682 /* The first operand must be a reference type or the null type */
12683 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12684 error_found = 1; /* Error reported further below */
12685
12686 /* The second operand must be a reference type */
12687 if (!JREFERENCE_TYPE_P (op2_type))
12688 {
12689 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12690 parse_error_context
12691 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12692 lang_printable_name (op2_type, 0));
12693 error_found = 1;
12694 }
12695
12696 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12697 {
12698 /* If the first operand is null, the result is always false */
12699 if (op1 == null_pointer_node)
12700 return boolean_false_node;
15fdcfe9
PB
12701 else if (flag_emit_class_files)
12702 {
12703 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12704 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12705 return node;
12706 }
5e942c50
APB
12707 /* Otherwise we have to invoke instance of to figure it out */
12708 else
67db0ce7 12709 return build_instanceof (op1, op2_type);
5e942c50
APB
12710 }
12711 /* There is no way the expression operand can be an instance of
12712 the type operand. This is a compile time error. */
12713 else
12714 {
c2e3db92 12715 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12716 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12717 parse_error_context
12718 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12719 t1, lang_printable_name (op2_type, 0));
12720 free (t1);
12721 error_found = 1;
12722 }
e04a16fb 12723
5e942c50 12724 break;
e04a16fb
AG
12725
12726 /* 15.21 Bitwise and Logical Operators */
12727 case BIT_AND_EXPR:
12728 case BIT_XOR_EXPR:
12729 case BIT_IOR_EXPR:
12730 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12731 /* Binary numeric promotion is performed on both operand and the
12732 expression retain that type */
12733 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12734
12735 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12736 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12737 /* The type of the bitwise operator expression is BOOLEAN */
12738 prom_type = boolean_type_node;
12739 else
12740 {
12741 if (!JINTEGRAL_TYPE_P (op1_type))
12742 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12743 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12744 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12745 TREE_TYPE (node) = error_mark_node;
12746 error_found = 1;
12747 /* Insert a break here if adding thing before the switch's
12748 break for this case */
12749 }
12750 break;
12751
12752 /* 15.22 Conditional-And Operator */
12753 case TRUTH_ANDIF_EXPR:
12754 /* 15.23 Conditional-Or Operator */
12755 case TRUTH_ORIF_EXPR:
12756 /* Operands must be of BOOLEAN type */
12757 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12758 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12759 {
12760 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12761 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12762 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12763 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12764 TREE_TYPE (node) = boolean_type_node;
12765 error_found = 1;
12766 break;
12767 }
12768 /* The type of the conditional operators is BOOLEAN */
12769 prom_type = boolean_type_node;
12770 break;
12771
12772 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12773 case LT_EXPR:
12774 case GT_EXPR:
12775 case LE_EXPR:
12776 case GE_EXPR:
12777 /* The type of each of the operands must be a primitive numeric
12778 type */
12779 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12780 {
12781 if (!JNUMERIC_TYPE_P (op1_type))
12782 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12783 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12784 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12785 TREE_TYPE (node) = boolean_type_node;
12786 error_found = 1;
12787 break;
12788 }
12789 /* Binary numeric promotion is performed on the operands */
12790 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12791 /* The type of the relation expression is always BOOLEAN */
12792 prom_type = boolean_type_node;
12793 break;
12794
12795 /* 15.20 Equality Operator */
12796 case EQ_EXPR:
12797 case NE_EXPR:
48a840d9
APB
12798 /* It's time for us to patch the strings. */
12799 if ((cn = patch_string (op1)))
12800 {
12801 op1 = cn;
12802 op1_type = TREE_TYPE (op1);
12803 }
12804 if ((cn = patch_string (op2)))
12805 {
12806 op2 = cn;
12807 op2_type = TREE_TYPE (op2);
12808 }
12809
e04a16fb
AG
12810 /* 15.20.1 Numerical Equality Operators == and != */
12811 /* Binary numeric promotion is performed on the operands */
5e942c50 12812 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12813 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12814
12815 /* 15.20.2 Boolean Equality Operators == and != */
12816 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12817 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12818 ; /* Nothing to do here */
12819
12820 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12821 /* Types have to be either references or the null type. If
12822 they're references, it must be possible to convert either
12823 type to the other by casting conversion. */
b9f7e36c
APB
12824 else if (op1 == null_pointer_node || op2 == null_pointer_node
12825 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12826 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12827 || valid_ref_assignconv_cast_p (op2_type,
12828 op1_type, 1))))
e04a16fb
AG
12829 ; /* Nothing to do here */
12830
12831 /* Else we have an error figure what can't be converted into
12832 what and report the error */
12833 else
12834 {
12835 char *t1;
c2e3db92 12836 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12837 parse_error_context
781b0558
KG
12838 (wfl_operator,
12839 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12840 operator_string (node), t1,
0a2138e2 12841 lang_printable_name (op2_type, 0));
e04a16fb
AG
12842 free (t1);
12843 TREE_TYPE (node) = boolean_type_node;
12844 error_found = 1;
12845 break;
12846 }
12847 prom_type = boolean_type_node;
12848 break;
12849 }
12850
e04a16fb
AG
12851 if (error_found)
12852 return error_mark_node;
12853
12854 TREE_OPERAND (node, 0) = op1;
12855 TREE_OPERAND (node, 1) = op2;
12856 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12857 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12858
ce6e9147
APB
12859 if (flag_emit_xref)
12860 return node;
12861
d1472141
PB
12862 /* fold does not respect side-effect order as required for Java but not C.
12863 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12864 * bytecode.
12865 */
12866 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12867 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12868 node = fold (node);
12869 return node;
e04a16fb
AG
12870}
12871
b67d701b
PB
12872/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12873 zero value, the value of CSTE comes after the valude of STRING */
12874
12875static tree
12876do_merge_string_cste (cste, string, string_len, after)
12877 tree cste;
49f48c71 12878 const char *string;
b67d701b
PB
12879 int string_len, after;
12880{
12881 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 12882 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
12883 TREE_STRING_LENGTH (cste) = len;
12884 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12885 if (after)
12886 {
12887 strcpy (TREE_STRING_POINTER (cste), string);
12888 strcat (TREE_STRING_POINTER (cste), old);
12889 }
12890 else
12891 {
12892 strcpy (TREE_STRING_POINTER (cste), old);
12893 strcat (TREE_STRING_POINTER (cste), string);
12894 }
12895 return cste;
12896}
12897
12898/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12899 new STRING_CST on success, NULL_TREE on failure */
12900
12901static tree
12902merge_string_cste (op1, op2, after)
12903 tree op1, op2;
12904 int after;
12905{
12906 /* Handle two string constants right away */
12907 if (TREE_CODE (op2) == STRING_CST)
12908 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12909 TREE_STRING_LENGTH (op2), after);
12910
12911 /* Reasonable integer constant can be treated right away */
12912 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12913 {
49f48c71
KG
12914 static const char *boolean_true = "true";
12915 static const char *boolean_false = "false";
12916 static const char *null_pointer = "null";
b67d701b 12917 char ch[3];
49f48c71 12918 const char *string;
b67d701b
PB
12919
12920 if (op2 == boolean_true_node)
12921 string = boolean_true;
12922 else if (op2 == boolean_false_node)
12923 string = boolean_false;
12924 else if (op2 == null_pointer_node)
12925 string = null_pointer;
12926 else if (TREE_TYPE (op2) == char_type_node)
12927 {
12928 ch[0] = (char )TREE_INT_CST_LOW (op2);
12929 ch[1] = '\0';
12930 string = ch;
12931 }
12932 else
12933 string = print_int_node (op2);
12934
12935 return do_merge_string_cste (op1, string, strlen (string), after);
12936 }
12937 return NULL_TREE;
12938}
12939
12940/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12941 has to be a STRING_CST and the other part must be a STRING_CST or a
12942 INTEGRAL constant. Return a new STRING_CST if the operation
12943 succeed, NULL_TREE otherwise.
12944
12945 If the case we want to optimize for space, we might want to return
12946 NULL_TREE for each invocation of this routine. FIXME */
12947
12948static tree
12949string_constant_concatenation (op1, op2)
12950 tree op1, op2;
12951{
12952 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12953 {
0a2138e2 12954 tree string, rest;
b67d701b
PB
12955 int invert;
12956
12957 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12958 rest = (string == op1 ? op2 : op1);
12959 invert = (string == op1 ? 0 : 1 );
12960
12961 /* Walk REST, only if it looks reasonable */
12962 if (TREE_CODE (rest) != STRING_CST
12963 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12964 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12965 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12966 {
12967 rest = java_complete_tree (rest);
12968 if (rest == error_mark_node)
12969 return error_mark_node;
12970 rest = fold (rest);
12971 }
12972 return merge_string_cste (string, rest, invert);
12973 }
12974 return NULL_TREE;
12975}
12976
12977/* Implement the `+' operator. Does static optimization if possible,
12978 otherwise create (if necessary) and append elements to a
12979 StringBuffer. The StringBuffer will be carried around until it is
12980 used for a function call or an assignment. Then toString() will be
12981 called on it to turn it into a String object. */
12982
12983static tree
12984build_string_concatenation (op1, op2)
12985 tree op1, op2;
12986{
12987 tree result;
dc0b3eff 12988 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
12989
12990 if (flag_emit_xref)
12991 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
12992
12993 /* Try to do some static optimization */
12994 if ((result = string_constant_concatenation (op1, op2)))
12995 return result;
12996
c0d87ff6
PB
12997 /* Discard empty strings on either side of the expression */
12998 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
12999 {
13000 op1 = op2;
13001 op2 = NULL_TREE;
13002 }
c0d87ff6 13003 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13004 op2 = NULL_TREE;
b67d701b 13005
acd663ee 13006 /* If operands are string constant, turn then into object references */
b67d701b
PB
13007 if (TREE_CODE (op1) == STRING_CST)
13008 op1 = patch_string_cst (op1);
acd663ee 13009 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13010 op2 = patch_string_cst (op2);
13011
acd663ee
APB
13012 /* If either one of the constant is null and the other non null
13013 operand is a String object, return it. */
13014 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13015 return op1;
13016
b67d701b
PB
13017 /* If OP1 isn't already a StringBuffer, create and
13018 initialize a new one */
13019 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13020 {
13021 /* Two solutions here:
c52b5771
AG
13022 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13023 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13024 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13025 op1 = BUILD_STRING_BUFFER (op1);
13026 else
13027 {
13028 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13029 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13030 }
13031 }
13032
acd663ee
APB
13033 if (op2)
13034 {
13035 /* OP1 is no longer the last node holding a crafted StringBuffer */
13036 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13037 /* Create a node for `{new...,xxx}.append (op2)' */
13038 if (op2)
13039 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13040 }
13041
b67d701b
PB
13042 /* Mark the last node holding a crafted StringBuffer */
13043 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13044
13045 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13046 return op1;
13047}
13048
13049/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13050 StringBuffer. If no string were found to be patched, return
13051 NULL. */
13052
13053static tree
13054patch_string (node)
13055 tree node;
13056{
1179ebc2
APB
13057 if (node == error_mark_node)
13058 return error_mark_node;
b67d701b
PB
13059 if (TREE_CODE (node) == STRING_CST)
13060 return patch_string_cst (node);
13061 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13062 {
c877974e 13063 int saved = ctxp->explicit_constructor_p;
b67d701b 13064 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13065 tree ret;
13066 /* Temporary disable forbid the use of `this'. */
13067 ctxp->explicit_constructor_p = 0;
13068 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13069 /* String concatenation arguments must be evaluated in order too. */
13070 ret = force_evaluation_order (ret);
c877974e
APB
13071 /* Restore it at its previous value */
13072 ctxp->explicit_constructor_p = saved;
13073 return ret;
b67d701b
PB
13074 }
13075 return NULL_TREE;
13076}
13077
13078/* Build the internal representation of a string constant. */
13079
13080static tree
13081patch_string_cst (node)
13082 tree node;
13083{
13084 int location;
15fdcfe9
PB
13085 if (! flag_emit_class_files)
13086 {
13087 push_obstacks (&permanent_obstack, &permanent_obstack);
13088 node = get_identifier (TREE_STRING_POINTER (node));
13089 location = alloc_name_constant (CONSTANT_String, node);
13090 node = build_ref_from_constant_pool (location);
8226320b 13091 pop_obstacks ();
15fdcfe9 13092 }
cd9643f7 13093 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13094 TREE_CONSTANT (node) = 1;
13095 return node;
13096}
13097
13098/* Build an incomplete unary operator expression. */
e04a16fb
AG
13099
13100static tree
13101build_unaryop (op_token, op_location, op1)
13102 int op_token, op_location;
13103 tree op1;
13104{
13105 enum tree_code op;
13106 tree unaryop;
13107 switch (op_token)
13108 {
b67d701b 13109 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13110 case MINUS_TK: op = NEGATE_EXPR; break;
13111 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13112 case NOT_TK: op = BIT_NOT_EXPR; break;
13113 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13114 op_token);
13115 }
13116
13117 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13118 TREE_SIDE_EFFECTS (unaryop) = 1;
13119 /* Store the location of the operator, for better error report. The
13120 string of the operator will be rebuild based on the OP value. */
13121 EXPR_WFL_LINECOL (unaryop) = op_location;
13122 return unaryop;
13123}
13124
13125/* Special case for the ++/-- operators, since they require an extra
13126 argument to build, which is set to NULL and patched
13127 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13128
13129static tree
13130build_incdec (op_token, op_location, op1, is_post_p)
13131 int op_token, op_location;
13132 tree op1;
13133 int is_post_p;
13134{
13135 static enum tree_code lookup [2][2] =
13136 {
13137 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13138 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13139 };
13140 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13141 NULL_TREE, op1, NULL_TREE);
13142 TREE_SIDE_EFFECTS (node) = 1;
13143 /* Store the location of the operator, for better error report. The
13144 string of the operator will be rebuild based on the OP value. */
13145 EXPR_WFL_LINECOL (node) = op_location;
13146 return node;
13147}
13148
13149/* Build an incomplete cast operator, based on the use of the
13150 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13151 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13152 though its type is already set. */
13153
13154static tree
13155build_cast (location, type, exp)
13156 int location;
13157 tree type, exp;
13158{
13159 tree node = build1 (CONVERT_EXPR, type, exp);
13160 EXPR_WFL_LINECOL (node) = location;
13161 return node;
13162}
13163
c2952b01
APB
13164/* Build an incomplete class reference operator. */
13165static tree
13166build_incomplete_class_ref (location, class_name)
13167 int location;
13168 tree class_name;
13169{
13170 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13171 EXPR_WFL_LINECOL (node) = location;
13172 return node;
13173}
13174
13175/* Complete an incomplete class reference operator. */
13176static tree
13177patch_incomplete_class_ref (node)
13178 tree node;
13179{
13180 tree type = TREE_OPERAND (node, 0);
13181 tree ref_type;
13182
13183 if (!(ref_type = resolve_type_during_patch (type)))
13184 return error_mark_node;
13185
165f37bc 13186 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13187 {
13188 /* A class referenced by `foo.class' is initialized. */
13189 return build_class_init (ref_type, build_class_ref (ref_type));
13190 }
165f37bc
APB
13191
13192 /* If we're emitting class files and we have to deal with non
13193 primitive types, we invoke (and consider generating) the
13194 synthetic static method `class$'. */
13195 if (!TYPE_DOT_CLASS (current_class))
13196 build_dot_class_method (current_class);
f0f3a777 13197 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13198 return java_complete_tree (ref_type);
c2952b01
APB
13199}
13200
e04a16fb
AG
13201/* 15.14 Unary operators. We return error_mark_node in case of error,
13202 but preserve the type of NODE if the type is fixed. */
13203
13204static tree
13205patch_unaryop (node, wfl_op)
13206 tree node;
13207 tree wfl_op;
13208{
13209 tree op = TREE_OPERAND (node, 0);
13210 tree op_type = TREE_TYPE (op);
ab3a6dd6 13211 tree prom_type = NULL_TREE, value, decl;
c2952b01 13212 int outer_field_flag = 0;
e04a16fb
AG
13213 int code = TREE_CODE (node);
13214 int error_found = 0;
13215
13216 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13217
13218 switch (code)
13219 {
13220 /* 15.13.2 Postfix Increment Operator ++ */
13221 case POSTINCREMENT_EXPR:
13222 /* 15.13.3 Postfix Increment Operator -- */
13223 case POSTDECREMENT_EXPR:
13224 /* 15.14.1 Prefix Increment Operator ++ */
13225 case PREINCREMENT_EXPR:
13226 /* 15.14.2 Prefix Decrement Operator -- */
13227 case PREDECREMENT_EXPR:
5cbdba64 13228 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13229 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13230 /* We might be trying to change an outer field accessed using
13231 access method. */
13232 if (outer_field_flag)
13233 {
13234 /* Retrieve the decl of the field we're trying to access. We
13235 do that by first retrieving the function we would call to
13236 access the field. It has been already verified that this
13237 field isn't final */
13238 if (flag_emit_class_files)
13239 decl = TREE_OPERAND (op, 0);
13240 else
13241 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13242 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13243 }
b3edebcf 13244 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13245 else if (!JDECL_P (decl)
b3edebcf
APB
13246 && TREE_CODE (decl) != COMPONENT_REF
13247 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13248 && TREE_CODE (decl) != INDIRECT_REF
13249 && !(TREE_CODE (decl) == COMPOUND_EXPR
13250 && TREE_OPERAND (decl, 1)
13251 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13252 {
5e942c50
APB
13253 tree lvalue;
13254 /* Before screaming, check that we're not in fact trying to
13255 increment a optimized static final access, in which case
13256 we issue an different error message. */
13257 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13258 && resolve_expression_name (wfl_op, &lvalue)
13259 && check_final_assignment (lvalue, wfl_op)))
13260 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13261 operator_string (node));
e04a16fb
AG
13262 TREE_TYPE (node) = error_mark_node;
13263 error_found = 1;
13264 }
c2952b01
APB
13265
13266 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13267 error_found = 1;
13268
e04a16fb
AG
13269 /* From now on, we know that op if a variable and that it has a
13270 valid wfl. We use wfl_op to locate errors related to the
13271 ++/-- operand. */
13272 else if (!JNUMERIC_TYPE_P (op_type))
13273 {
13274 parse_error_context
13275 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13276 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13277 TREE_TYPE (node) = error_mark_node;
13278 error_found = 1;
13279 }
13280 else
13281 {
4a5f66c3 13282 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13283 both operands, if really necessary */
13284 if (JINTEGRAL_TYPE_P (op_type))
13285 {
13286 value = build_int_2 (1, 0);
13287 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13288 }
13289 else
13290 {
13291 value = build_int_2 (1, 0);
13292 TREE_TYPE (node) =
13293 binary_numeric_promotion (op_type,
13294 TREE_TYPE (value), &op, &value);
13295 }
c2952b01
APB
13296
13297 /* We remember we might be accessing an outer field */
13298 if (outer_field_flag)
13299 {
13300 /* We re-generate an access to the field */
13301 value = build (PLUS_EXPR, TREE_TYPE (op),
13302 build_outer_field_access (wfl_op, decl), value);
13303
13304 /* And we patch the original access$() into a write
13305 with plus_op as a rhs */
13306 return outer_field_access_fix (node, op, value);
13307 }
13308
5cbdba64 13309 /* And write back into the node. */
4a5f66c3 13310 TREE_OPERAND (node, 0) = op;
e04a16fb 13311 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13312 /* Convert the overall back into its original type, if
13313 necessary, and return */
13314 if (JINTEGRAL_TYPE_P (op_type))
13315 return fold (node);
13316 else
13317 return fold (convert (op_type, node));
e04a16fb
AG
13318 }
13319 break;
13320
13321 /* 15.14.3 Unary Plus Operator + */
b67d701b 13322 case UNARY_PLUS_EXPR:
e04a16fb
AG
13323 /* 15.14.4 Unary Minus Operator - */
13324 case NEGATE_EXPR:
13325 if (!JNUMERIC_TYPE_P (op_type))
13326 {
13327 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13328 TREE_TYPE (node) = error_mark_node;
13329 error_found = 1;
13330 }
13331 /* Unary numeric promotion is performed on operand */
13332 else
13333 {
15fdcfe9
PB
13334 op = do_unary_numeric_promotion (op);
13335 prom_type = TREE_TYPE (op);
b67d701b 13336 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13337 return fold (op);
e04a16fb
AG
13338 }
13339 break;
13340
13341 /* 15.14.5 Bitwise Complement Operator ~ */
13342 case BIT_NOT_EXPR:
13343 if (!JINTEGRAL_TYPE_P (op_type))
13344 {
13345 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13346 TREE_TYPE (node) = error_mark_node;
13347 error_found = 1;
13348 }
13349 else
13350 {
15fdcfe9
PB
13351 op = do_unary_numeric_promotion (op);
13352 prom_type = TREE_TYPE (op);
e04a16fb
AG
13353 }
13354 break;
13355
13356 /* 15.14.6 Logical Complement Operator ! */
13357 case TRUTH_NOT_EXPR:
13358 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13359 {
13360 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13361 /* But the type is known. We will report an error if further
13362 attempt of a assignment is made with this rhs */
e04a16fb
AG
13363 TREE_TYPE (node) = boolean_type_node;
13364 error_found = 1;
13365 }
13366 else
13367 prom_type = boolean_type_node;
13368 break;
13369
13370 /* 15.15 Cast Expression */
13371 case CONVERT_EXPR:
0a2138e2 13372 value = patch_cast (node, wfl_operator);
e04a16fb 13373 if (value == error_mark_node)
c877974e
APB
13374 {
13375 /* If this cast is part of an assignment, we tell the code
13376 that deals with it not to complain about a mismatch,
13377 because things have been cast, anyways */
13378 TREE_TYPE (node) = error_mark_node;
13379 error_found = 1;
13380 }
13381 else
dc0b3eff
PB
13382 {
13383 value = fold (value);
13384 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13385 return value;
13386 }
e04a16fb
AG
13387 break;
13388 }
13389
e04a16fb
AG
13390 if (error_found)
13391 return error_mark_node;
4a5f66c3
APB
13392
13393 /* There are cases where node has been replaced by something else
13394 and we don't end up returning here: UNARY_PLUS_EXPR,
13395 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13396 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13397 TREE_TYPE (node) = prom_type;
dc0b3eff 13398 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13399 return fold (node);
13400}
13401
13402/* Generic type resolution that sometimes takes place during node
13403 patching. Returned the resolved type or generate an error
13404 message. Return the resolved type or NULL_TREE. */
13405
13406static tree
13407resolve_type_during_patch (type)
13408 tree type;
13409{
13410 if (unresolved_type_p (type, NULL))
13411 {
4142b247 13412 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13413 if (!type_decl)
13414 {
13415 parse_error_context (type,
13416 "Class `%s' not found in type declaration",
13417 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13418 return NULL_TREE;
13419 }
13420 else
5e942c50
APB
13421 {
13422 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13423 return TREE_TYPE (type_decl);
13424 }
e04a16fb
AG
13425 }
13426 return type;
13427}
13428/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13429 found. Otherwise NODE or something meant to replace it is returned. */
13430
13431static tree
0a2138e2 13432patch_cast (node, wfl_operator)
e04a16fb 13433 tree node;
e04a16fb
AG
13434 tree wfl_operator;
13435{
13436 tree op = TREE_OPERAND (node, 0);
13437 tree op_type = TREE_TYPE (op);
13438 tree cast_type = TREE_TYPE (node);
13439 char *t1;
13440
13441 /* First resolve OP_TYPE if unresolved */
13442 if (!(cast_type = resolve_type_during_patch (cast_type)))
13443 return error_mark_node;
13444
13445 /* Check on cast that are proven correct at compile time */
13446 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13447 {
e04a16fb
AG
13448 /* Same type */
13449 if (cast_type == op_type)
13450 return node;
13451
0b4d333e
APB
13452 /* float and double type are converted to the original type main
13453 variant and then to the target type. */
13454 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13455 op = convert (integer_type_node, op);
13456
e04a16fb
AG
13457 /* Try widening/narowwing convertion. Potentially, things need
13458 to be worked out in gcc so we implement the extreme cases
13459 correctly. fold_convert() needs to be fixed. */
13460 return convert (cast_type, op);
13461 }
13462
0b4d333e
APB
13463 /* It's also valid to cast a boolean into a boolean */
13464 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13465 return node;
13466
5e942c50
APB
13467 /* null can be casted to references */
13468 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13469 return build_null_of_type (cast_type);
13470
e04a16fb
AG
13471 /* The remaining legal casts involve conversion between reference
13472 types. Check for their compile time correctness. */
13473 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13474 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13475 {
13476 TREE_TYPE (node) = promote_type (cast_type);
13477 /* Now, the case can be determined correct at compile time if
13478 OP_TYPE can be converted into CAST_TYPE by assignment
13479 conversion (5.2) */
13480
13481 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13482 {
13483 TREE_SET_CODE (node, NOP_EXPR);
13484 return node;
13485 }
13486
13487 if (flag_emit_class_files)
13488 {
13489 TREE_SET_CODE (node, CONVERT_EXPR);
13490 return node;
13491 }
e04a16fb
AG
13492
13493 /* The cast requires a run-time check */
13494 return build (CALL_EXPR, promote_type (cast_type),
13495 build_address_of (soft_checkcast_node),
13496 tree_cons (NULL_TREE, build_class_ref (cast_type),
13497 build_tree_list (NULL_TREE, op)),
13498 NULL_TREE);
13499 }
13500
13501 /* Any other casts are proven incorrect at compile time */
c2e3db92 13502 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13503 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13504 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13505 free (t1);
13506 return error_mark_node;
13507}
13508
5e942c50
APB
13509/* Build a null constant and give it the type TYPE. */
13510
13511static tree
13512build_null_of_type (type)
13513 tree type;
13514{
13515 tree node = build_int_2 (0, 0);
13516 TREE_TYPE (node) = promote_type (type);
13517 return node;
13518}
13519
e04a16fb
AG
13520/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13521 a list of indices. */
13522static tree
13523build_array_ref (location, array, index)
13524 int location;
13525 tree array, index;
13526{
13527 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13528 EXPR_WFL_LINECOL (node) = location;
13529 return node;
13530}
13531
13532/* 15.12 Array Access Expression */
13533
13534static tree
c877974e
APB
13535patch_array_ref (node)
13536 tree node;
e04a16fb
AG
13537{
13538 tree array = TREE_OPERAND (node, 0);
13539 tree array_type = TREE_TYPE (array);
13540 tree index = TREE_OPERAND (node, 1);
13541 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13542 int error_found = 0;
13543
13544 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13545
e04a16fb
AG
13546 if (TREE_CODE (array_type) == POINTER_TYPE)
13547 array_type = TREE_TYPE (array_type);
13548
13549 /* The array reference must be an array */
13550 if (!TYPE_ARRAY_P (array_type))
13551 {
13552 parse_error_context
781b0558
KG
13553 (wfl_operator,
13554 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13555 lang_printable_name (array_type, 0));
e04a16fb
AG
13556 TREE_TYPE (node) = error_mark_node;
13557 error_found = 1;
13558 }
13559
c2952b01 13560 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13561 type must be int */
15fdcfe9
PB
13562 index = do_unary_numeric_promotion (index);
13563 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13564 {
1ebadc60 13565 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13566 parse_error_context (wfl_operator,
13567 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13568 lang_printable_name (index_type, 0));
13569 else
781b0558
KG
13570 parse_error_context (wfl_operator,
13571 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13572 lang_printable_name (index_type, 0));
e04a16fb
AG
13573 TREE_TYPE (node) = error_mark_node;
13574 error_found = 1;
13575 }
13576
e04a16fb
AG
13577 if (error_found)
13578 return error_mark_node;
e04a16fb 13579
5e942c50 13580 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13581
7f1d4866 13582 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13583 {
15fdcfe9
PB
13584 TREE_OPERAND (node, 0) = array;
13585 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13586 }
13587 else
939d7216
PB
13588 {
13589 /* The save_expr is for correct evaluation order. It would be cleaner
13590 to use force_evaluation_order (see comment there), but that is
13591 difficult when we also have to deal with bounds checking. */
13592 if (TREE_SIDE_EFFECTS (index))
13593 array = save_expr (array);
13594 node = build_java_arrayaccess (array, array_type, index);
13595 if (TREE_SIDE_EFFECTS (index))
13596 node = build (COMPOUND_EXPR, array_type, array, node);
13597 }
e04a16fb
AG
13598 TREE_TYPE (node) = array_type;
13599 return node;
13600}
13601
13602/* 15.9 Array Creation Expressions */
13603
13604static tree
13605build_newarray_node (type, dims, extra_dims)
13606 tree type;
13607 tree dims;
13608 int extra_dims;
13609{
13610 tree node =
b67d701b 13611 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13612 build_int_2 (extra_dims, 0));
e04a16fb
AG
13613 return node;
13614}
13615
13616static tree
13617patch_newarray (node)
13618 tree node;
13619{
13620 tree type = TREE_OPERAND (node, 0);
13621 tree dims = TREE_OPERAND (node, 1);
13622 tree cdim, array_type;
13623 int error_found = 0;
13624 int ndims = 0;
13625 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13626
13627 /* Dimension types are verified. It's better for the types to be
13628 verified in order. */
13629 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13630 {
13631 int dim_error = 0;
13632 tree dim = TREE_VALUE (cdim);
13633
13634 /* Dim might have been saved during its evaluation */
13635 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13636
13637 /* The type of each specified dimension must be an integral type. */
13638 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13639 dim_error = 1;
13640
13641 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13642 promoted type must be int. */
13643 else
13644 {
15fdcfe9 13645 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13646 if (TREE_TYPE (dim) != int_type_node)
13647 dim_error = 1;
13648 }
13649
13650 /* Report errors on types here */
13651 if (dim_error)
13652 {
13653 parse_error_context
13654 (TREE_PURPOSE (cdim),
781b0558 13655 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13656 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13657 "Explicit cast needed to" : "Can't"),
0a2138e2 13658 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13659 error_found = 1;
13660 }
13661
e04a16fb
AG
13662 TREE_PURPOSE (cdim) = NULL_TREE;
13663 }
13664
13665 /* Resolve array base type if unresolved */
13666 if (!(type = resolve_type_during_patch (type)))
13667 error_found = 1;
13668
13669 if (error_found)
13670 {
13671 /* We don't want further evaluation of this bogus array creation
13672 operation */
13673 TREE_TYPE (node) = error_mark_node;
13674 return error_mark_node;
13675 }
13676
15fdcfe9
PB
13677 /* Set array_type to the actual (promoted) array type of the result. */
13678 if (TREE_CODE (type) == RECORD_TYPE)
13679 type = build_pointer_type (type);
13680 while (--xdims >= 0)
13681 {
13682 type = promote_type (build_java_array_type (type, -1));
13683 }
13684 dims = nreverse (dims);
13685 array_type = type;
13686 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13687 {
13688 type = array_type;
05bccae2
RK
13689 array_type
13690 = build_java_array_type (type,
13691 TREE_CODE (cdim) == INTEGER_CST
13692 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13693 : -1);
15fdcfe9
PB
13694 array_type = promote_type (array_type);
13695 }
13696 dims = nreverse (dims);
13697
e04a16fb
AG
13698 /* The node is transformed into a function call. Things are done
13699 differently according to the number of dimensions. If the number
13700 of dimension is equal to 1, then the nature of the base type
13701 (primitive or not) matters. */
15fdcfe9 13702 if (ndims == 1)
fdec99c6 13703 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13704
e04a16fb
AG
13705 /* Can't reuse what's already written in expr.c because it uses the
13706 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13707 return build (CALL_EXPR, array_type,
e04a16fb 13708 build_address_of (soft_multianewarray_node),
15fdcfe9 13709 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13710 tree_cons (NULL_TREE,
15fdcfe9 13711 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13712 NULL_TREE);
13713}
13714
f8976021
APB
13715/* 10.6 Array initializer. */
13716
13717/* Build a wfl for array element that don't have one, so we can
13718 pin-point errors. */
13719
13720static tree
13721maybe_build_array_element_wfl (node)
13722 tree node;
13723{
13724 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13725 return build_expr_wfl (NULL_TREE, ctxp->filename,
13726 ctxp->elc.line, ctxp->elc.prev_col);
13727 else
13728 return NULL_TREE;
13729}
13730
13731/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13732 identification of initialized arrays easier to detect during walk
13733 and expansion. */
13734
13735static tree
13736build_new_array_init (location, values)
13737 int location;
13738 tree values;
13739{
13740 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13741 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13742 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13743 return to_return;
13744}
13745
13746/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13747 occurred. Otherwise return NODE after having set its type
13748 appropriately. */
13749
13750static tree
13751patch_new_array_init (type, node)
13752 tree type, node;
f8976021
APB
13753{
13754 int error_seen = 0;
fdec99c6 13755 tree current, element_type;
f8976021 13756 HOST_WIDE_INT length;
fdec99c6
PB
13757 int all_constant = 1;
13758 tree init = TREE_OPERAND (node, 0);
f8976021 13759
fdec99c6
PB
13760 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13761 {
13762 parse_error_context (node,
13763 "Invalid array initializer for non-array type `%s'",
13764 lang_printable_name (type, 1));
13765 return error_mark_node;
13766 }
13767 type = TREE_TYPE (type);
13768 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13769
fdec99c6
PB
13770 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13771
13772 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13773 current; length++, current = TREE_CHAIN (current))
f8976021 13774 {
fdec99c6
PB
13775 tree elt = TREE_VALUE (current);
13776 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13777 {
fdec99c6 13778 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13779 elt = TREE_VALUE (current);
13780 /* When compiling to native code, STRING_CST is converted to
13781 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13782 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13783 all_constant = 0;
f8976021 13784 }
fdec99c6
PB
13785 else
13786 {
13787 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13788 TREE_PURPOSE (current) = NULL_TREE;
13789 all_constant = 0;
13790 }
9a7ab4b3
APB
13791 if (elt && TREE_CODE (elt) == TREE_LIST
13792 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13793 error_seen = 1;
f8976021
APB
13794 }
13795
13796 if (error_seen)
13797 return error_mark_node;
13798
13799 /* Create a new type. We can't reuse the one we have here by
13800 patching its dimension because it originally is of dimension -1
13801 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13802 type = build_java_array_type (element_type, length);
13803 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13804 TREE_TYPE (node) = promote_type (type);
13805 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13806 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13807 return node;
13808}
13809
13810/* Verify that one entry of the initializer element list can be
13811 assigned to the array base type. Report 1 if an error occurred, 0
13812 otherwise. */
13813
13814static int
13815array_constructor_check_entry (type, entry)
13816 tree type, entry;
13817{
13818 char *array_type_string = NULL; /* For error reports */
13819 tree value, type_value, new_value, wfl_value, patched;
13820 int error_seen = 0;
13821
13822 new_value = NULL_TREE;
13823 wfl_value = TREE_VALUE (entry);
13824
100f7cd8 13825 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13826 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13827 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13828 if ((patched = patch_string (value)))
13829 value = patched;
1179ebc2
APB
13830 if (value == error_mark_node)
13831 return 1;
f8976021 13832
f8976021
APB
13833 type_value = TREE_TYPE (value);
13834
1179ebc2 13835 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13836 constant overflow during narrowing. */
13837 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13838 new_value = try_builtin_assignconv (wfl_operator, type, value);
13839 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13840 type_value = promote_type (type);
100f7cd8
APB
13841
13842 pop_obstacks ();
f8976021
APB
13843 /* Check and report errors */
13844 if (!new_value)
13845 {
49f48c71 13846 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13847 "Can't" : "Explicit cast needed to");
13848 if (!array_type_string)
c2e3db92 13849 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13850 parse_error_context
13851 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13852 msg, lang_printable_name (type_value, 1), array_type_string);
13853 error_seen = 1;
13854 }
13855
13856 if (new_value)
13857 {
b8c5b1c6 13858 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13859 TREE_VALUE (entry) = new_value;
13860 }
13861
13862 if (array_type_string)
13863 free (array_type_string);
13864
13865 TREE_PURPOSE (entry) = NULL_TREE;
13866 return error_seen;
13867}
13868
e04a16fb
AG
13869static tree
13870build_this (location)
13871 int location;
13872{
9ee9b555 13873 tree node = build_wfl_node (this_identifier_node);
b67d701b 13874 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13875 EXPR_WFL_LINECOL (node) = location;
13876 return node;
13877}
13878
13879/* 14.15 The return statement. It builds a modify expression that
13880 assigns the returned value to the RESULT_DECL that hold the value
13881 to be returned. */
13882
13883static tree
13884build_return (location, op)
13885 int location;
13886 tree op;
13887{
13888 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13889 EXPR_WFL_LINECOL (node) = location;
b67d701b 13890 node = build_debugable_stmt (location, node);
e04a16fb
AG
13891 return node;
13892}
13893
13894static tree
13895patch_return (node)
13896 tree node;
13897{
13898 tree return_exp = TREE_OPERAND (node, 0);
13899 tree meth = current_function_decl;
13900 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13901 int error_found = 0;
13902
13903 TREE_TYPE (node) = error_mark_node;
13904 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13905
13906 /* It's invalid to have a return value within a function that is
13907 declared with the keyword void or that is a constructor */
13908 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13909 error_found = 1;
13910
f099f336 13911 /* It's invalid to use a return statement in a static block */
c2952b01 13912 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13913 error_found = 1;
13914
e04a16fb
AG
13915 /* It's invalid to have a no return value within a function that
13916 isn't declared with the keyword `void' */
13917 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13918 error_found = 2;
c2952b01
APB
13919
13920 if (in_instance_initializer)
13921 error_found = 1;
e04a16fb
AG
13922
13923 if (error_found)
13924 {
c2952b01 13925 if (in_instance_initializer)
f099f336 13926 parse_error_context (wfl_operator,
c2952b01
APB
13927 "`return' inside instance initializer");
13928
13929 else if (DECL_CLINIT_P (current_function_decl))
13930 parse_error_context (wfl_operator,
13931 "`return' inside static initializer");
f099f336
APB
13932
13933 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13934 {
c2e3db92 13935 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13936 parse_error_context (wfl_operator,
13937 "`return' with%s value from `%s %s'",
13938 (error_found == 1 ? "" : "out"),
13939 t, lang_printable_name (meth, 0));
13940 free (t);
13941 }
13942 else
13943 parse_error_context (wfl_operator,
13944 "`return' with value from constructor `%s'",
13945 lang_printable_name (meth, 0));
e04a16fb
AG
13946 return error_mark_node;
13947 }
13948
5e942c50
APB
13949 /* If we have a return_exp, build a modify expression and expand
13950 it. Note: at that point, the assignment is declared valid, but we
13951 may want to carry some more hacks */
e04a16fb
AG
13952 if (return_exp)
13953 {
5e942c50
APB
13954 tree exp = java_complete_tree (return_exp);
13955 tree modify, patched;
13956
13957 /* If the function returned value and EXP are booleans, EXP has
13958 to be converted into the type of DECL_RESULT, which is integer
13959 (see complete_start_java_method) */
13960 if (TREE_TYPE (exp) == boolean_type_node &&
13961 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13962 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13963
13964 /* `null' can be assigned to a function returning a reference */
13965 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13966 exp == null_pointer_node)
13967 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13968
13969 if ((patched = patch_string (exp)))
13970 exp = patched;
13971
13972 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
13973 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13974 modify = java_complete_tree (modify);
5e942c50 13975
e04a16fb
AG
13976 if (modify != error_mark_node)
13977 {
13978 TREE_SIDE_EFFECTS (modify) = 1;
13979 TREE_OPERAND (node, 0) = modify;
13980 }
13981 else
13982 return error_mark_node;
13983 }
13984 TREE_TYPE (node) = void_type_node;
13985 TREE_SIDE_EFFECTS (node) = 1;
13986 return node;
13987}
13988
13989/* 14.8 The if Statement */
13990
13991static tree
13992build_if_else_statement (location, expression, if_body, else_body)
13993 int location;
13994 tree expression, if_body, else_body;
13995{
13996 tree node;
e04a16fb 13997 if (!else_body)
9bbc7d9f 13998 else_body = empty_stmt_node;
e04a16fb
AG
13999 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14000 EXPR_WFL_LINECOL (node) = location;
b67d701b 14001 node = build_debugable_stmt (location, node);
e04a16fb
AG
14002 return node;
14003}
14004
14005static tree
14006patch_if_else_statement (node)
14007 tree node;
14008{
14009 tree expression = TREE_OPERAND (node, 0);
14010
14011 TREE_TYPE (node) = error_mark_node;
14012 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14013
14014 /* The type of expression must be boolean */
b67d701b
PB
14015 if (TREE_TYPE (expression) != boolean_type_node
14016 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14017 {
14018 parse_error_context
14019 (wfl_operator,
14020 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14021 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14022 return error_mark_node;
14023 }
14024
14025 TREE_TYPE (node) = void_type_node;
14026 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14027 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14028 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14029 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14030 return node;
14031}
14032
14033/* 14.6 Labeled Statements */
14034
14035/* Action taken when a lableled statement is parsed. a new
14036 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14037 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14038
14039static tree
0a2138e2 14040build_labeled_block (location, label)
e04a16fb 14041 int location;
0a2138e2 14042 tree label;
e04a16fb 14043{
b635eb2f 14044 tree label_name ;
e04a16fb 14045 tree label_decl, node;
b635eb2f
PB
14046 if (label == NULL_TREE || label == continue_identifier_node)
14047 label_name = label;
14048 else
e04a16fb 14049 {
b635eb2f
PB
14050 label_name = merge_qualified_name (label_id, label);
14051 /* Issue an error if we try to reuse a label that was previously
14052 declared */
14053 if (IDENTIFIER_LOCAL_VALUE (label_name))
14054 {
14055 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14056 parse_error_context (wfl_operator,
14057 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14058 IDENTIFIER_POINTER (label));
14059 EXPR_WFL_LINECOL (wfl_operator) =
14060 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14061 parse_error_context (wfl_operator,
14062 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14063 IDENTIFIER_POINTER (label));
14064 java_error_count--;
14065 }
e04a16fb
AG
14066 }
14067
14068 label_decl = create_label_decl (label_name);
14069 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14070 EXPR_WFL_LINECOL (node) = location;
14071 TREE_SIDE_EFFECTS (node) = 1;
14072 return node;
14073}
14074
b67d701b 14075/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14076
14077static tree
b635eb2f 14078finish_labeled_statement (lbe, statement)
e04a16fb
AG
14079 tree lbe; /* Labeled block expr */
14080 tree statement;
14081{
14082 /* In anyways, tie the loop to its statement */
14083 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14084 pop_labeled_block ();
14085 POP_LABELED_BLOCK ();
e04a16fb
AG
14086 return lbe;
14087}
14088
14089/* 14.10, 14.11, 14.12 Loop Statements */
14090
14091/* Create an empty LOOP_EXPR and make it the last in the nested loop
14092 list. */
14093
14094static tree
14095build_new_loop (loop_body)
14096 tree loop_body;
14097{
14098 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14099 TREE_SIDE_EFFECTS (loop) = 1;
14100 PUSH_LOOP (loop);
14101 return loop;
14102}
14103
14104/* Create a loop body according to the following structure:
14105 COMPOUND_EXPR
14106 COMPOUND_EXPR (loop main body)
14107 EXIT_EXPR (this order is for while/for loops.
14108 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14109 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14110 BODY end of this labeled block)
14111 INCREMENT (if any)
14112
14113 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14114 after the body, like in the do-while loop.
14115
14116 To obtain a loop, the loop body structure described above is
14117 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14118
14119 LABELED_BLOCK_EXPR
14120 LABEL_DECL (use this label to exit the loop)
14121 LOOP_EXPR
14122 <structure described above> */
e04a16fb
AG
14123
14124static tree
14125build_loop_body (location, condition, reversed)
14126 int location;
14127 tree condition;
14128 int reversed;
14129{
0a2138e2 14130 tree first, second, body;
e04a16fb
AG
14131
14132 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14133 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14134 condition = build_debugable_stmt (location, condition);
14135 TREE_SIDE_EFFECTS (condition) = 1;
14136
b635eb2f 14137 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14138 first = (reversed ? body : condition);
14139 second = (reversed ? condition : body);
14140 return
14141 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14142 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14143}
14144
14145/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14146 their order) on the current loop. Unlink the current loop from the
14147 loop list. */
14148
14149static tree
b635eb2f 14150finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14151 int location;
14152 tree condition, body;
14153 int reversed;
14154{
14155 tree to_return = ctxp->current_loop;
14156 tree loop_body = LOOP_EXPR_BODY (to_return);
14157 if (condition)
14158 {
14159 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14160 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14161 The real EXIT_EXPR is one operand further. */
14162 EXPR_WFL_LINECOL (cnode) = location;
14163 /* This one is for accurate error reports */
14164 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14165 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14166 }
14167 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14168 POP_LOOP ();
14169 return to_return;
14170}
14171
b635eb2f 14172/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14173 loops feature the condition part */
14174
14175static tree
b635eb2f 14176finish_for_loop (location, condition, update, body)
e04a16fb
AG
14177 int location;
14178 tree condition, update, body;
14179{
14180 /* Put the condition and the loop body in place */
b635eb2f 14181 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14182 /* LOOP is the current loop which has been now popped of the loop
14183 stack. Install the update block */
14184 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14185 return loop;
14186}
14187
5cbdba64
APB
14188/* Try to find the loop a block might be related to. This comprises
14189 the case where the LOOP_EXPR is found as the second operand of a
14190 COMPOUND_EXPR, because the loop happens to have an initialization
14191 part, then expressed as the first operand of the COMPOUND_EXPR. If
14192 the search finds something, 1 is returned. Otherwise, 0 is
14193 returned. The search is assumed to start from a
14194 LABELED_BLOCK_EXPR's block. */
14195
14196static tree
14197search_loop (statement)
14198 tree statement;
14199{
14200 if (TREE_CODE (statement) == LOOP_EXPR)
14201 return statement;
14202
14203 if (TREE_CODE (statement) == BLOCK)
14204 statement = BLOCK_SUBBLOCKS (statement);
14205 else
14206 return NULL_TREE;
14207
14208 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14209 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14210 statement = TREE_OPERAND (statement, 1);
14211
14212 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14213 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14214}
14215
14216/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14217 returned otherwise. */
14218
14219static int
14220labeled_block_contains_loop_p (block, loop)
14221 tree block, loop;
14222{
14223 if (!block)
14224 return 0;
14225
14226 if (LABELED_BLOCK_BODY (block) == loop)
14227 return 1;
14228
c2952b01 14229 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14230 return 1;
14231
14232 return 0;
14233}
14234
e04a16fb 14235/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14236 insert LOOP as its body. */
e04a16fb
AG
14237
14238static tree
14239patch_loop_statement (loop)
14240 tree loop;
14241{
cd9643f7 14242 tree loop_label;
5cbdba64 14243
cd9643f7 14244 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14245 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14246 return loop;
14247
cd9643f7 14248 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14249 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14250 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14251 LABELED_BLOCK_BODY (loop_label) = loop;
14252 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14253 return loop_label;
e04a16fb
AG
14254}
14255
14256/* 14.13, 14.14: break and continue Statements */
14257
14258/* Build a break or a continue statement. a null NAME indicates an
14259 unlabeled break/continue statement. */
14260
14261static tree
14262build_bc_statement (location, is_break, name)
14263 int location, is_break;
14264 tree name;
14265{
14266 tree break_continue, label_block_expr = NULL_TREE;
14267
14268 if (name)
14269 {
14270 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14271 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14272 /* Null means that we don't have a target for this named
14273 break/continue. In this case, we make the target to be the
14274 label name, so that the error can be reported accuratly in
14275 patch_bc_statement. */
14276 label_block_expr = EXPR_WFL_NODE (name);
14277 }
14278 /* Unlabeled break/continue will be handled during the
14279 break/continue patch operation */
14280 break_continue
14281 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14282
14283 IS_BREAK_STMT_P (break_continue) = is_break;
14284 TREE_SIDE_EFFECTS (break_continue) = 1;
14285 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14286 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14287 return break_continue;
14288}
14289
14290/* Verification of a break/continue statement. */
14291
14292static tree
14293patch_bc_statement (node)
14294 tree node;
14295{
14296 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14297 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14298 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14299
e04a16fb 14300 /* Having an identifier here means that the target is unknown. */
b635eb2f 14301 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14302 {
14303 parse_error_context (wfl_operator, "No label definition found for `%s'",
14304 IDENTIFIER_POINTER (bc_label));
14305 return error_mark_node;
14306 }
b635eb2f 14307 if (! IS_BREAK_STMT_P (node))
e04a16fb 14308 {
b635eb2f
PB
14309 /* It's a continue statement. */
14310 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14311 {
b635eb2f
PB
14312 if (labeled_block == NULL_TREE)
14313 {
14314 if (bc_label == NULL_TREE)
14315 parse_error_context (wfl_operator,
14316 "`continue' must be in loop");
14317 else
1504b2b4
APB
14318 parse_error_context
14319 (wfl_operator, "continue label `%s' does not name a loop",
14320 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14321 return error_mark_node;
14322 }
14323 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14324 == continue_identifier_node)
14325 && (bc_label == NULL_TREE
14326 || TREE_CHAIN (labeled_block) == bc_label))
14327 {
14328 bc_label = labeled_block;
14329 break;
14330 }
e04a16fb 14331 }
e04a16fb 14332 }
b635eb2f 14333 else if (!bc_label)
34f4db93 14334 {
b635eb2f 14335 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14336 {
b635eb2f
PB
14337 if (labeled_block == NULL_TREE)
14338 {
14339 parse_error_context (wfl_operator,
14340 "`break' must be in loop or switch");
14341 return error_mark_node;
14342 }
14343 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14344 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14345 || search_loop (target_stmt))
b635eb2f
PB
14346 {
14347 bc_label = labeled_block;
14348 break;
14349 }
e04a16fb 14350 }
e04a16fb
AG
14351 }
14352
b635eb2f 14353 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14354 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14355
e04a16fb
AG
14356 /* Our break/continue don't return values. */
14357 TREE_TYPE (node) = void_type_node;
14358 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14359 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14360 sometimes, like after a if statement) */
14361 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14362 TREE_SIDE_EFFECTS (node) = 1;
14363 return node;
14364}
14365
14366/* Process the exit expression belonging to a loop. Its type must be
14367 boolean. */
14368
14369static tree
14370patch_exit_expr (node)
14371 tree node;
14372{
14373 tree expression = TREE_OPERAND (node, 0);
14374 TREE_TYPE (node) = error_mark_node;
14375 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14376
14377 /* The type of expression must be boolean */
14378 if (TREE_TYPE (expression) != boolean_type_node)
14379 {
14380 parse_error_context
14381 (wfl_operator,
781b0558 14382 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14383 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14384 return error_mark_node;
14385 }
14386 /* Now we know things are allright, invert the condition, fold and
14387 return */
14388 TREE_OPERAND (node, 0) =
14389 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14390
14391 if (! integer_zerop (TREE_OPERAND (node, 0))
14392 && ctxp->current_loop != NULL_TREE
14393 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14394 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14395 if (! integer_onep (TREE_OPERAND (node, 0)))
14396 CAN_COMPLETE_NORMALLY (node) = 1;
14397
14398
e04a16fb
AG
14399 TREE_TYPE (node) = void_type_node;
14400 return node;
14401}
b67d701b
PB
14402
14403/* 14.9 Switch statement */
14404
14405static tree
14406patch_switch_statement (node)
14407 tree node;
14408{
c877974e 14409 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14410
14411 /* Complete the switch expression */
14412 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14413 se_type = TREE_TYPE (se);
14414 /* The type of the switch expression must be char, byte, short or
14415 int */
14416 if (!JINTEGRAL_TYPE_P (se_type))
14417 {
14418 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14419 parse_error_context (wfl_operator,
14420 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14421 lang_printable_name (se_type, 0));
b67d701b
PB
14422 /* This is what java_complete_tree will check */
14423 TREE_OPERAND (node, 0) = error_mark_node;
14424 return error_mark_node;
14425 }
14426
15fdcfe9 14427 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14428
14429 /* Ready to return */
15fdcfe9 14430 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14431 {
14432 TREE_TYPE (node) = error_mark_node;
14433 return error_mark_node;
14434 }
14435 TREE_TYPE (node) = void_type_node;
14436 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14437 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14438 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14439 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14440 return node;
14441}
14442
165f37bc 14443/* 14.18 The try/catch statements */
b67d701b 14444
b67d701b 14445static tree
a7d8d81f 14446build_try_statement (location, try_block, catches)
b67d701b 14447 int location;
a7d8d81f
PB
14448 tree try_block, catches;
14449{
14450 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14451 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14452 return node;
b67d701b
PB
14453}
14454
a7d8d81f
PB
14455static tree
14456build_try_finally_statement (location, try_block, finally)
14457 int location;
14458 tree try_block, finally;
b67d701b 14459{
a7d8d81f
PB
14460 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14461 EXPR_WFL_LINECOL (node) = location;
14462 return node;
b67d701b
PB
14463}
14464
14465static tree
14466patch_try_statement (node)
14467 tree node;
14468{
14469 int error_found = 0;
14470 tree try = TREE_OPERAND (node, 0);
14471 /* Exception handlers are considered in left to right order */
14472 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14473 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14474
14475 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14476 to process the next catch clause. We process the catch clause before
14477 the try block so that when processing the try block we can check thrown
14478 exceptions againts the caught type list. */
b67d701b
PB
14479 for (current = catch; current; current = TREE_CHAIN (current))
14480 {
14481 tree carg_decl, carg_type;
14482 tree sub_current, catch_block, catch_clause;
14483 int unreachable;
14484
b67d701b 14485 /* At this point, the structure of the catch clause is
b67d701b
PB
14486 CATCH_EXPR (catch node)
14487 BLOCK (with the decl of the parameter)
14488 COMPOUND_EXPR
7525cc04 14489 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14490 BLOCK (catch clause block)
a7d8d81f
PB
14491 */
14492 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14493 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14494 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14495
14496 /* Catch clauses can't have more than one parameter declared,
14497 but it's already enforced by the grammar. Make sure that the
14498 only parameter of the clause statement in of class Throwable
14499 or a subclass of Throwable, but that was done earlier. The
14500 catch clause parameter type has also been resolved. */
14501
14502 /* Just make sure that the catch clause parameter type inherits
14503 from java.lang.Throwable */
14504 if (!inherits_from_p (carg_type, throwable_type_node))
14505 {
14506 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14507 parse_error_context (wfl_operator,
781b0558 14508 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14509 lang_printable_name (carg_type, 0));
b67d701b
PB
14510 error_found = 1;
14511 continue;
14512 }
14513
14514 /* Partial check for unreachable catch statement: The catch
14515 clause is reachable iff is no earlier catch block A in
14516 the try statement such that the type of the catch
14517 clause's parameter is the same as or a subclass of the
14518 type of A's parameter */
14519 unreachable = 0;
14520 for (sub_current = catch;
14521 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14522 {
14523 tree sub_catch_clause, decl;
a7d8d81f 14524 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14525 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14526
14527 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14528 {
14529 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14530 parse_error_context
781b0558
KG
14531 (wfl_operator,
14532 "`catch' not reached because of the catch clause at line %d",
14533 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14534 unreachable = error_found = 1;
14535 break;
14536 }
14537 }
b67d701b
PB
14538 /* Complete the catch clause block */
14539 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14540 if (catch_block == error_mark_node)
14541 {
14542 error_found = 1;
14543 continue;
14544 }
15fdcfe9
PB
14545 if (CAN_COMPLETE_NORMALLY (catch_block))
14546 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14547 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14548
14549 if (unreachable)
14550 continue;
14551
14552 /* Things to do here: the exception must be thrown */
14553
14554 /* Link this type to the caught type list */
14555 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14556 }
14557
b9f7e36c
APB
14558 PUSH_EXCEPTIONS (caught_type_list);
14559 if ((try = java_complete_tree (try)) == error_mark_node)
14560 error_found = 1;
15fdcfe9
PB
14561 if (CAN_COMPLETE_NORMALLY (try))
14562 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14563 POP_EXCEPTIONS ();
14564
b67d701b
PB
14565 /* Verification ends here */
14566 if (error_found)
14567 return error_mark_node;
14568
14569 TREE_OPERAND (node, 0) = try;
14570 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14571 TREE_TYPE (node) = void_type_node;
14572 return node;
14573}
b9f7e36c
APB
14574
14575/* 14.17 The synchronized Statement */
14576
14577static tree
14578patch_synchronized_statement (node, wfl_op1)
14579 tree node, wfl_op1;
14580{
5a005d9e 14581 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14582 tree block = TREE_OPERAND (node, 1);
5a005d9e 14583
d8fccff5 14584 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14585
14586 if (expr == error_mark_node)
14587 {
14588 block = java_complete_tree (block);
14589 return expr;
14590 }
b9f7e36c
APB
14591
14592 /* The TYPE of expr must be a reference type */
5a005d9e 14593 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14594 {
14595 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14596 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14597 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14598 return error_mark_node;
14599 }
14600
ce6e9147
APB
14601 if (flag_emit_xref)
14602 {
14603 TREE_OPERAND (node, 0) = expr;
14604 TREE_OPERAND (node, 1) = java_complete_tree (block);
14605 CAN_COMPLETE_NORMALLY (node) = 1;
14606 return node;
14607 }
14608
b9f7e36c
APB
14609 /* Generate a try-finally for the synchronized statement, except
14610 that the handler that catches all throw exception calls
14611 _Jv_MonitorExit and then rethrow the exception.
14612 The synchronized statement is then implemented as:
14613 TRY
14614 {
14615 _Jv_MonitorEnter (expression)
14616 synchronized_block
14617 _Jv_MonitorExit (expression)
14618 }
14619 CATCH_ALL
14620 {
14621 e = _Jv_exception_info ();
14622 _Jv_MonitorExit (expression)
14623 Throw (e);
14624 } */
14625
5a005d9e
PB
14626 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14627 BUILD_MONITOR_ENTER (enter, expr_decl);
14628 BUILD_MONITOR_EXIT (exit, expr_decl);
14629 CAN_COMPLETE_NORMALLY (enter) = 1;
14630 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14631 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14632 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14633 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14634 build (COMPOUND_EXPR, NULL_TREE,
14635 build (WITH_CLEANUP_EXPR, NULL_TREE,
14636 build (COMPOUND_EXPR, NULL_TREE,
96847892 14637 assignment, enter),
5a005d9e
PB
14638 NULL_TREE, exit),
14639 block));
14640 node = build_expr_block (node, expr_decl);
14641
14642 return java_complete_tree (node);
b9f7e36c
APB
14643}
14644
14645/* 14.16 The throw Statement */
14646
14647static tree
14648patch_throw_statement (node, wfl_op1)
14649 tree node, wfl_op1;
14650{
14651 tree expr = TREE_OPERAND (node, 0);
14652 tree type = TREE_TYPE (expr);
14653 int unchecked_ok = 0, tryblock_throws_ok = 0;
14654
14655 /* Thrown expression must be assignable to java.lang.Throwable */
14656 if (!try_reference_assignconv (throwable_type_node, expr))
14657 {
14658 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14659 parse_error_context (wfl_operator,
14660 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14661 lang_printable_name (type, 0));
b9f7e36c
APB
14662 /* If the thrown expression was a reference, we further the
14663 compile-time check. */
14664 if (!JREFERENCE_TYPE_P (type))
14665 return error_mark_node;
14666 }
14667
14668 /* At least one of the following must be true */
14669
14670 /* The type of the throw expression is a not checked exception,
14671 i.e. is a unchecked expression. */
c877974e 14672 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14673
c2952b01
APB
14674 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14675 /* An instance can't throw a checked excetion unless that exception
14676 is explicitely declared in the `throws' clause of each
14677 constructor. This doesn't apply to anonymous classes, since they
14678 don't have declared constructors. */
14679 if (!unchecked_ok
14680 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14681 {
14682 tree current;
14683 for (current = TYPE_METHODS (current_class); current;
14684 current = TREE_CHAIN (current))
14685 if (DECL_CONSTRUCTOR_P (current)
14686 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14687 {
14688 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)",
14689 lang_printable_name (TREE_TYPE (expr), 0));
14690 return error_mark_node;
14691 }
14692 }
14693
b9f7e36c
APB
14694 /* Throw is contained in a try statement and at least one catch
14695 clause can receive the thrown expression or the current method is
14696 declared to throw such an exception. Or, the throw statement is
14697 contained in a method or constructor declaration and the type of
14698 the Expression is assignable to at least one type listed in the
14699 throws clause the declaration. */
b9f7e36c 14700 if (!unchecked_ok)
f099f336 14701 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14702 if (!(unchecked_ok || tryblock_throws_ok))
14703 {
14704 /* If there is a surrounding try block that has no matching
14705 clatch clause, report it first. A surrounding try block exits
14706 only if there is something after the list of checked
14707 exception thrown by the current function (if any). */
14708 if (IN_TRY_BLOCK_P ())
781b0558 14709 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 14710 lang_printable_name (type, 0));
b9f7e36c
APB
14711 /* If we have no surrounding try statement and the method doesn't have
14712 any throws, report it now. FIXME */
f099f336
APB
14713
14714 /* We report that the exception can't be throw from a try block
14715 in all circumstances but when the `throw' is inside a static
14716 block. */
b9f7e36c
APB
14717 else if (!EXCEPTIONS_P (currently_caught_type_list)
14718 && !tryblock_throws_ok)
f099f336 14719 {
c2952b01 14720 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14721 parse_error_context (wfl_operator,
14722 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14723 lang_printable_name (type, 0));
14724 else
781b0558
KG
14725 parse_error_context (wfl_operator,
14726 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14727 lang_printable_name (type, 0));
14728 }
b9f7e36c
APB
14729 /* Otherwise, the current method doesn't have the appropriate
14730 throws declaration */
14731 else
781b0558 14732 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14733 lang_printable_name (type, 0));
b9f7e36c
APB
14734 return error_mark_node;
14735 }
14736
ce6e9147 14737 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14738 BUILD_THROW (node, expr);
ce6e9147
APB
14739
14740 /* If doing xrefs, keep the location where the `throw' was seen. */
14741 if (flag_emit_xref)
14742 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14743 return node;
14744}
14745
14746/* Check that exception said to be thrown by method DECL can be
14747 effectively caught from where DECL is invoked. */
14748
14749static void
14750check_thrown_exceptions (location, decl)
14751 int location;
14752 tree decl;
14753{
14754 tree throws;
14755 /* For all the unchecked exceptions thrown by DECL */
14756 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14757 throws = TREE_CHAIN (throws))
0a2138e2 14758 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14759 {
3e78f871
PB
14760#if 1
14761 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14762 if (DECL_NAME (decl) == get_identifier ("clone"))
14763 continue;
14764#endif
b9f7e36c 14765 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14766 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14767 parse_error_context
14768 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14769 lang_printable_name (TREE_VALUE (throws), 0));
14770 else
14771 {
14772 parse_error_context
781b0558 14773 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14774 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14775 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14776 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14777 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14778 }
b9f7e36c
APB
14779 }
14780}
14781
c877974e 14782/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14783 try-catch blocks, OR is listed in the `throws' clause of the
14784 current method. */
14785
14786static int
0a2138e2 14787check_thrown_exceptions_do (exception)
b9f7e36c
APB
14788 tree exception;
14789{
14790 tree list = currently_caught_type_list;
c877974e 14791 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14792 /* First, all the nested try-catch-finally at that stage. The
14793 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14794 if (IS_UNCHECKED_EXCEPTION_P (exception))
14795 return 1;
b9f7e36c
APB
14796 while (list)
14797 {
14798 tree caught;
14799 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14800 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14801 return 1;
14802 list = TREE_CHAIN (list);
14803 }
14804 return 0;
14805}
14806
14807static void
14808purge_unchecked_exceptions (mdecl)
14809 tree mdecl;
14810{
14811 tree throws = DECL_FUNCTION_THROWS (mdecl);
14812 tree new = NULL_TREE;
14813
14814 while (throws)
14815 {
14816 tree next = TREE_CHAIN (throws);
c877974e 14817 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14818 {
14819 TREE_CHAIN (throws) = new;
14820 new = throws;
14821 }
14822 throws = next;
14823 }
14824 /* List is inverted here, but it doesn't matter */
14825 DECL_FUNCTION_THROWS (mdecl) = new;
14826}
22eed1e6
APB
14827
14828/* 15.24 Conditional Operator ?: */
14829
14830static tree
14831patch_conditional_expr (node, wfl_cond, wfl_op1)
14832 tree node, wfl_cond, wfl_op1;
14833{
14834 tree cond = TREE_OPERAND (node, 0);
14835 tree op1 = TREE_OPERAND (node, 1);
14836 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14837 tree resulting_type = NULL_TREE;
ac825856 14838 tree t1, t2, patched;
22eed1e6
APB
14839 int error_found = 0;
14840
ac825856
APB
14841 /* Operands of ?: might be StringBuffers crafted as a result of a
14842 string concatenation. Obtain a descent operand here. */
14843 if ((patched = patch_string (op1)))
14844 TREE_OPERAND (node, 1) = op1 = patched;
14845 if ((patched = patch_string (op2)))
14846 TREE_OPERAND (node, 2) = op2 = patched;
14847
14848 t1 = TREE_TYPE (op1);
14849 t2 = TREE_TYPE (op2);
14850
22eed1e6
APB
14851 /* The first expression must be a boolean */
14852 if (TREE_TYPE (cond) != boolean_type_node)
14853 {
14854 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14855 parse_error_context (wfl_operator,
14856 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14857 lang_printable_name (TREE_TYPE (cond), 0));
14858 error_found = 1;
14859 }
14860
14861 /* Second and third can be numeric, boolean (i.e. primitive),
14862 references or null. Anything else results in an error */
14863 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14864 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14865 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14866 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14867 error_found = 1;
14868
14869 /* Determine the type of the conditional expression. Same types are
14870 easy to deal with */
14871 else if (t1 == t2)
14872 resulting_type = t1;
14873
14874 /* There are different rules for numeric types */
14875 else if (JNUMERIC_TYPE_P (t1))
14876 {
14877 /* if byte/short found, the resulting type is short */
14878 if ((t1 == byte_type_node && t2 == short_type_node)
14879 || (t1 == short_type_node && t2 == byte_type_node))
14880 resulting_type = short_type_node;
14881
14882 /* If t1 is a constant int and t2 is of type byte, short or char
14883 and t1's value fits in t2, then the resulting type is t2 */
14884 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14885 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14886 resulting_type = t2;
14887
14888 /* If t2 is a constant int and t1 is of type byte, short or char
14889 and t2's value fits in t1, then the resulting type is t1 */
14890 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14891 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14892 resulting_type = t1;
14893
14894 /* Otherwise, binary numeric promotion is applied and the
14895 resulting type is the promoted type of operand 1 and 2 */
14896 else
93024893 14897 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14898 &TREE_OPERAND (node, 1),
14899 &TREE_OPERAND (node, 2));
14900 }
14901
14902 /* Cases of a reference and a null type */
14903 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14904 resulting_type = t1;
14905
14906 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14907 resulting_type = t2;
14908
14909 /* Last case: different reference types. If a type can be converted
14910 into the other one by assignment conversion, the latter
14911 determines the type of the expression */
14912 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14913 resulting_type = promote_type (t1);
14914
14915 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14916 resulting_type = promote_type (t2);
14917
14918 /* If we don't have any resulting type, we're in trouble */
14919 if (!resulting_type)
14920 {
c2e3db92 14921 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14922 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14923 parse_error_context (wfl_operator,
14924 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14925 t, lang_printable_name (t2, 0));
22eed1e6
APB
14926 free (t);
14927 error_found = 1;
14928 }
14929
14930 if (error_found)
14931 {
14932 TREE_TYPE (node) = error_mark_node;
14933 return error_mark_node;
14934 }
14935
14936 TREE_TYPE (node) = resulting_type;
14937 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14938 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14939 return node;
14940}
ac825856 14941
5b09b33e
PB
14942/* Try to constant fold NODE.
14943 If NODE is not a constant expression, return NULL_EXPR.
14944 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14945
14946static tree
14947fold_constant_for_init (node, context)
14948 tree node;
14949 tree context;
14950{
14951 tree op0, op1, val;
14952 enum tree_code code = TREE_CODE (node);
14953
ee97d354 14954 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 14955 return node;
93024893 14956
5b09b33e
PB
14957 switch (code)
14958 {
5b09b33e
PB
14959 case PLUS_EXPR:
14960 case MINUS_EXPR:
bc3ca41b
PB
14961 case MULT_EXPR:
14962 case TRUNC_MOD_EXPR:
14963 case RDIV_EXPR:
5b09b33e
PB
14964 case LSHIFT_EXPR:
14965 case RSHIFT_EXPR:
14966 case URSHIFT_EXPR:
14967 case BIT_AND_EXPR:
14968 case BIT_XOR_EXPR:
14969 case BIT_IOR_EXPR:
5b09b33e
PB
14970 case TRUTH_ANDIF_EXPR:
14971 case TRUTH_ORIF_EXPR:
14972 case EQ_EXPR:
14973 case NE_EXPR:
14974 case GT_EXPR:
14975 case GE_EXPR:
14976 case LT_EXPR:
14977 case LE_EXPR:
14978 op0 = TREE_OPERAND (node, 0);
14979 op1 = TREE_OPERAND (node, 1);
14980 val = fold_constant_for_init (op0, context);
14981 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14982 return NULL_TREE;
14983 TREE_OPERAND (node, 0) = val;
14984 val = fold_constant_for_init (op1, context);
14985 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14986 return NULL_TREE;
14987 TREE_OPERAND (node, 1) = val;
14988 return patch_binop (node, op0, op1);
14989
14990 case UNARY_PLUS_EXPR:
14991 case NEGATE_EXPR:
14992 case TRUTH_NOT_EXPR:
14993 case BIT_NOT_EXPR:
14994 case CONVERT_EXPR:
14995 op0 = TREE_OPERAND (node, 0);
14996 val = fold_constant_for_init (op0, context);
14997 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14998 return NULL_TREE;
14999 TREE_OPERAND (node, 0) = val;
5a005d9e 15000 return patch_unaryop (node, op0);
5b09b33e
PB
15001 break;
15002
15003 case COND_EXPR:
15004 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15005 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15006 return NULL_TREE;
15007 TREE_OPERAND (node, 0) = val;
15008 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15009 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15010 return NULL_TREE;
15011 TREE_OPERAND (node, 1) = val;
15012 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15013 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15014 return NULL_TREE;
15015 TREE_OPERAND (node, 2) = val;
15016 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15017 : TREE_OPERAND (node, 2);
15018
15019 case VAR_DECL:
8576f094
APB
15020 case FIELD_DECL:
15021 if (! FIELD_FINAL (node)
5b09b33e
PB
15022 || DECL_INITIAL (node) == NULL_TREE)
15023 return NULL_TREE;
15024 val = DECL_INITIAL (node);
15025 /* Guard against infinite recursion. */
15026 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15027 val = fold_constant_for_init (val, node);
5b09b33e
PB
15028 DECL_INITIAL (node) = val;
15029 return val;
15030
15031 case EXPR_WITH_FILE_LOCATION:
15032 /* Compare java_complete_tree and resolve_expression_name. */
15033 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15034 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15035 {
15036 tree name = EXPR_WFL_NODE (node);
15037 tree decl;
15038 if (PRIMARY_P (node))
15039 return NULL_TREE;
15040 else if (! QUALIFIED_P (name))
15041 {
15042 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15043 if (decl == NULL_TREE
15044 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15045 return NULL_TREE;
15046 return fold_constant_for_init (decl, decl);
15047 }
15048 else
15049 {
5b09b33e
PB
15050 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15051 qualify_ambiguous_name (node);
15052 if (resolve_field_access (node, &decl, NULL)
15053 && decl != NULL_TREE)
15054 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15055 return NULL_TREE;
15056 }
15057 }
15058 else
15059 {
15060 op0 = TREE_OPERAND (node, 0);
15061 val = fold_constant_for_init (op0, context);
15062 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15063 return NULL_TREE;
15064 TREE_OPERAND (node, 0) = val;
15065 return val;
15066 }
15067
bc3ca41b
PB
15068#ifdef USE_COMPONENT_REF
15069 case IDENTIFIER:
15070 case COMPONENT_REF:
15071 ?;
15072#endif
15073
5b09b33e
PB
15074 default:
15075 return NULL_TREE;
15076 }
15077}
bc3ca41b
PB
15078
15079#ifdef USE_COMPONENT_REF
15080/* Context is 'T' for TypeName, 'P' for PackageName,
15081 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15082
15083tree
15084resolve_simple_name (name, context)
15085 tree name;
15086 int context;
15087{
15088}
15089
15090tree
15091resolve_qualified_name (name, context)
15092 tree name;
15093 int context;
15094{
15095}
15096#endif
This page took 2.456216 seconds and 5 git commands to generate.