]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
stdstreams.cc: Include <libio.h>, not "libio.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));
1175b9b4 279static int check_abstract_method_definitions PARAMS ((int, tree, tree));
df32d2ce
KG
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) \
6e2aa220 358 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
e04a16fb 359
5cbdba64
APB
360/* This is the end index for binary operators that can also be used
361 in compound assignements. */
362#define BINOP_COMPOUND_CANDIDATES 11
363
e04a16fb
AG
364/* Fake WFL used to report error message. It is initialized once if
365 needed and reused with it's location information is overriden. */
15fdcfe9 366tree wfl_operator = NULL_TREE;
e04a16fb
AG
367
368/* The "$L" identifier we use to create labels. */
b67d701b
PB
369static tree label_id = NULL_TREE;
370
371/* The "StringBuffer" identifier used for the String `+' operator. */
372static tree wfl_string_buffer = NULL_TREE;
373
374/* The "append" identifier used for String `+' operator. */
375static tree wfl_append = NULL_TREE;
376
377/* The "toString" identifier used for String `+' operator. */
378static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
379
380/* The "java.lang" import qualified name. */
381static tree java_lang_id = NULL_TREE;
09ed0f70 382
c2952b01
APB
383/* The generated `inst$' identifier used for generated enclosing
384 instance/field access functions. */
385static tree inst_id = NULL_TREE;
386
09ed0f70
APB
387/* The "java.lang.Cloneable" qualified name. */
388static tree java_lang_cloneable = NULL_TREE;
f099f336 389
ee17a290
TT
390/* The "java.io.Serializable" qualified name. */
391static tree java_io_serializable = NULL_TREE;
392
f099f336
APB
393/* Context and flag for static blocks */
394static tree current_static_block = NULL_TREE;
395
c2952b01
APB
396/* The generated `write_parm_value$' identifier. */
397static tree wpv_id;
398
ee07f4f4
APB
399/* The list of all packages we've seen so far */
400static tree package_list = NULL_TREE;
2884c41e
KG
401
402/* Check modifiers. If one doesn't fit, retrieve it in its declaration
403 line and point it out. */
404/* Should point out the one that don't fit. ASCII/unicode, going
405 backward. FIXME */
406
407#define check_modifiers(__message, __value, __mask) do { \
408 if ((__value) & ~(__mask)) \
409 { \
410 int i, remainder = (__value) & ~(__mask); \
411 for (i = 0; i <= 10; i++) \
412 if ((1 << i) & remainder) \
413 parse_error_context (ctxp->modifier_ctx [i], (__message), \
414 java_accstring_lookup (1 << i)); \
415 } \
416} while (0)
ee07f4f4 417
e04a16fb
AG
418%}
419
420%union {
421 tree node;
422 int sub_token;
423 struct {
424 int token;
425 int location;
426 } operator;
427 int value;
428}
429
9ee9b555
KG
430%{
431#include "lex.c"
432%}
433
e04a16fb
AG
434%pure_parser
435
436/* Things defined here have to match the order of what's in the
437 binop_lookup table. */
438
439%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
440%token LS_TK SRS_TK ZRS_TK
441%token AND_TK XOR_TK OR_TK
442%token BOOL_AND_TK BOOL_OR_TK
443%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
444
445/* This maps to the same binop_lookup entry than the token above */
446
447%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
448%token REM_ASSIGN_TK
449%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
450%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
451
452
453/* Modifier TOKEN have to be kept in this order. Don't scramble it */
454
455%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
456%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
457%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
458%token PAD_TK ABSTRACT_TK MODIFIER_TK
459
460/* Keep those two in order, too */
461%token DECR_TK INCR_TK
462
463/* From now one, things can be in any order */
464
465%token DEFAULT_TK IF_TK THROW_TK
466%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
467%token THROWS_TK BREAK_TK IMPORT_TK
468%token ELSE_TK INSTANCEOF_TK RETURN_TK
469%token VOID_TK CATCH_TK INTERFACE_TK
470%token CASE_TK EXTENDS_TK FINALLY_TK
471%token SUPER_TK WHILE_TK CLASS_TK
472%token SWITCH_TK CONST_TK TRY_TK
473%token FOR_TK NEW_TK CONTINUE_TK
474%token GOTO_TK PACKAGE_TK THIS_TK
475
476%token BYTE_TK SHORT_TK INT_TK LONG_TK
477%token CHAR_TK INTEGRAL_TK
478
479%token FLOAT_TK DOUBLE_TK FP_TK
480
481%token ID_TK
482
483%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
484
485%token ASSIGN_ANY_TK ASSIGN_TK
486%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
487
488%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
489%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
490
c2952b01 491%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
492
493%type <node> super ID_TK identifier
494%type <node> name simple_name qualified_name
c2952b01 495%type <node> type_declaration compilation_unit
e04a16fb
AG
496 field_declaration method_declaration extends_interfaces
497 interfaces interface_type_list
c2952b01 498 class_member_declaration
e04a16fb
AG
499 import_declarations package_declaration
500 type_declarations interface_body
501 interface_member_declaration constant_declaration
502 interface_member_declarations interface_type
503 abstract_method_declaration interface_type_list
504%type <node> class_body_declaration class_member_declaration
505 static_initializer constructor_declaration block
22eed1e6 506%type <node> class_body_declarations constructor_header
e04a16fb
AG
507%type <node> class_or_interface_type class_type class_type_list
508 constructor_declarator explicit_constructor_invocation
b9f7e36c 509%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
510
511%type <node> variable_declarator_id variable_declarator
512 variable_declarators variable_initializer
22eed1e6 513 variable_initializers constructor_body
ac825856 514 array_initializer
e04a16fb 515
2e5eb5c5 516%type <node> class_body block_end constructor_block_end
e04a16fb
AG
517%type <node> statement statement_without_trailing_substatement
518 labeled_statement if_then_statement label_decl
519 if_then_else_statement while_statement for_statement
520 statement_nsi labeled_statement_nsi do_statement
521 if_then_else_statement_nsi while_statement_nsi
522 for_statement_nsi statement_expression_list for_init
523 for_update statement_expression expression_statement
524 primary_no_new_array expression primary
525 array_creation_expression array_type
526 class_instance_creation_expression field_access
527 method_invocation array_access something_dot_new
528 argument_list postfix_expression while_expression
529 post_increment_expression post_decrement_expression
530 unary_expression_not_plus_minus unary_expression
531 pre_increment_expression pre_decrement_expression
532 unary_expression_not_plus_minus cast_expression
533 multiplicative_expression additive_expression
534 shift_expression relational_expression
535 equality_expression and_expression
536 exclusive_or_expression inclusive_or_expression
537 conditional_and_expression conditional_or_expression
538 conditional_expression assignment_expression
539 left_hand_side assignment for_header for_begin
540 constant_expression do_statement_begin empty_statement
b67d701b 541 switch_statement synchronized_statement throw_statement
f8976021 542 try_statement switch_expression switch_block
15fdcfe9 543 catches catch_clause catch_clause_parameter finally
c2952b01 544 anonymous_class_creation
e04a16fb
AG
545%type <node> return_statement break_statement continue_statement
546
547%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
548%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
549%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
550%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
551%type <operator> ASSIGN_ANY_TK assignment_operator
552%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
553%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
554%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 555%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 556%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
557%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
558%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 559%type <operator> NEW_TK
e04a16fb
AG
560
561%type <node> method_body
562
563%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
564 STRING_LIT_TK NULL_TK VOID_TK
565
566%type <node> IF_TK WHILE_TK FOR_TK
567
568%type <node> formal_parameter_list formal_parameter
569 method_declarator method_header
570
c2952b01 571%type <node> primitive_type reference_type type
e04a16fb
AG
572 BOOLEAN_TK INTEGRAL_TK FP_TK
573
c2952b01
APB
574/* Added or modified JDK 1.1 rule types */
575%type <node> type_literals array_type_literal
576
e04a16fb
AG
577%%
578/* 19.2 Production from 2.3: The Syntactic Grammar */
579goal:
580 compilation_unit
581 {}
582;
583
584/* 19.3 Productions from 3: Lexical structure */
585literal:
586 INT_LIT_TK
587| FP_LIT_TK
588| BOOL_LIT_TK
589| CHAR_LIT_TK
590| STRING_LIT_TK
591| NULL_TK
592;
593
594/* 19.4 Productions from 4: Types, Values and Variables */
595type:
596 primitive_type
597| reference_type
598;
599
600primitive_type:
601 INTEGRAL_TK
602| FP_TK
603| BOOLEAN_TK
604;
605
606reference_type:
607 class_or_interface_type
608| array_type
609;
610
611class_or_interface_type:
612 name
613;
614
615class_type:
616 class_or_interface_type /* Default rule */
617;
618
619interface_type:
620 class_or_interface_type
621;
622
623array_type:
624 primitive_type OSB_TK CSB_TK
625 {
626 $$ = build_java_array_type ($1, -1);
627 CLASS_LOADED_P ($$) = 1;
628 }
629| name OSB_TK CSB_TK
630 { $$ = build_unresolved_array_type ($1); }
631| array_type OSB_TK CSB_TK
632 { $$ = build_unresolved_array_type ($1); }
633| primitive_type OSB_TK error
634 {RULE ("']' expected"); RECOVER;}
635| array_type OSB_TK error
636 {RULE ("']' expected"); RECOVER;}
637;
638
639/* 19.5 Productions from 6: Names */
640name:
641 simple_name /* Default rule */
642| qualified_name /* Default rule */
643;
644
645simple_name:
646 identifier /* Default rule */
647;
648
649qualified_name:
650 name DOT_TK identifier
651 { $$ = make_qualified_name ($1, $3, $2.location); }
652;
653
654identifier:
655 ID_TK
656;
657
658/* 19.6: Production from 7: Packages */
659compilation_unit:
660 {$$ = NULL;}
661| package_declaration
662| import_declarations
663| type_declarations
664| package_declaration import_declarations
665| package_declaration type_declarations
666| import_declarations type_declarations
667| package_declaration import_declarations type_declarations
668;
669
670import_declarations:
671 import_declaration
672 {
673 $$ = NULL;
674 }
675| import_declarations import_declaration
676 {
677 $$ = NULL;
678 }
679;
680
681type_declarations:
682 type_declaration
683| type_declarations type_declaration
684;
685
686package_declaration:
687 PACKAGE_TK name SC_TK
ee07f4f4
APB
688 {
689 ctxp->package = EXPR_WFL_NODE ($2);
9a7ab4b3 690 register_package (ctxp->package);
ee07f4f4 691 }
e04a16fb
AG
692| PACKAGE_TK error
693 {yyerror ("Missing name"); RECOVER;}
694| PACKAGE_TK name error
695 {yyerror ("';' expected"); RECOVER;}
696;
697
698import_declaration:
699 single_type_import_declaration
700| type_import_on_demand_declaration
701;
702
703single_type_import_declaration:
704 IMPORT_TK name SC_TK
705 {
9a7ab4b3 706 tree name = EXPR_WFL_NODE ($2), last_name;
e04a16fb 707 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 708 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
709 while (last != IDENTIFIER_POINTER (name))
710 {
711 if (last [0] == '.')
712 break;
713 last--;
714 }
715 last_name = get_identifier (++last);
716 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
717 {
718 tree err = find_name_in_single_imports (last_name);
719 if (err && err != name)
720 parse_error_context
721 ($2, "Ambiguous class: `%s' and `%s'",
722 IDENTIFIER_POINTER (name),
723 IDENTIFIER_POINTER (err));
5e942c50 724 else
9a7ab4b3 725 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
726 }
727 else
5e942c50 728 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
729 }
730| IMPORT_TK error
731 {yyerror ("Missing name"); RECOVER;}
732| IMPORT_TK name error
733 {yyerror ("';' expected"); RECOVER;}
734;
735
736type_import_on_demand_declaration:
737 IMPORT_TK name DOT_TK MULT_TK SC_TK
738 {
739 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
740 /* Don't import java.lang.* twice. */
741 if (name != java_lang_id)
742 {
ba179f9f 743 read_import_dir ($2);
9a7ab4b3
APB
744 ctxp->import_demand_list =
745 chainon (ctxp->import_demand_list,
746 build_tree_list ($2, NULL_TREE));
ba179f9f 747 }
e04a16fb
AG
748 }
749| IMPORT_TK name DOT_TK error
750 {yyerror ("'*' expected"); RECOVER;}
751| IMPORT_TK name DOT_TK MULT_TK error
752 {yyerror ("';' expected"); RECOVER;}
753;
754
755type_declaration:
756 class_declaration
c2952b01 757 { end_class_declaration (0); }
e04a16fb 758| interface_declaration
c2952b01 759 { end_class_declaration (0); }
5f1c312a 760| empty_statement
e04a16fb
AG
761| error
762 {
763 YYERROR_NOW;
764 yyerror ("Class or interface declaration expected");
765 }
766;
767
768/* 19.7 Shortened from the original:
769 modifiers: modifier | modifiers modifier
770 modifier: any of public... */
771modifiers:
772 MODIFIER_TK
773 {
774 $$ = (1 << $1);
775 }
776| modifiers MODIFIER_TK
777 {
778 int acc = (1 << $2);
779 if ($$ & acc)
780 parse_error_context
781 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
782 java_accstring_lookup (acc));
783 else
784 {
785 $$ |= acc;
786 }
787 }
788;
789
790/* 19.8.1 Production from $8.1: Class Declaration */
791class_declaration:
792 modifiers CLASS_TK identifier super interfaces
793 { create_class ($1, $3, $4, $5); }
794 class_body
e04a16fb
AG
795| CLASS_TK identifier super interfaces
796 { create_class (0, $2, $3, $4); }
797 class_body
e04a16fb
AG
798| modifiers CLASS_TK error
799 {yyerror ("Missing class name"); RECOVER;}
800| CLASS_TK error
801 {yyerror ("Missing class name"); RECOVER;}
802| CLASS_TK identifier error
0b4d333e
APB
803 {
804 if (!ctxp->class_err) yyerror ("'{' expected");
805 DRECOVER(class1);
806 }
e04a16fb
AG
807| modifiers CLASS_TK identifier error
808 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
809;
810
811super:
812 { $$ = NULL; }
813| EXTENDS_TK class_type
814 { $$ = $2; }
815| EXTENDS_TK class_type error
816 {yyerror ("'{' expected"); ctxp->class_err=1;}
817| EXTENDS_TK error
818 {yyerror ("Missing super class name"); ctxp->class_err=1;}
819;
820
821interfaces:
822 { $$ = NULL_TREE; }
823| IMPLEMENTS_TK interface_type_list
824 { $$ = $2; }
825| IMPLEMENTS_TK error
826 {
827 ctxp->class_err=1;
828 yyerror ("Missing interface name");
829 }
830;
831
832interface_type_list:
833 interface_type
834 {
835 ctxp->interface_number = 1;
836 $$ = build_tree_list ($1, NULL_TREE);
837 }
838| interface_type_list C_TK interface_type
839 {
840 ctxp->interface_number++;
841 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
842 }
843| interface_type_list C_TK error
844 {yyerror ("Missing interface name"); RECOVER;}
845;
846
847class_body:
848 OCB_TK CCB_TK
7f10c2e2
APB
849 {
850 /* Store the location of the `}' when doing xrefs */
851 if (flag_emit_xref)
c2952b01 852 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 853 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 854 $$ = GET_CPC ();
7f10c2e2 855 }
e04a16fb 856| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
857 {
858 /* Store the location of the `}' when doing xrefs */
859 if (flag_emit_xref)
c2952b01 860 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 861 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 862 $$ = GET_CPC ();
7f10c2e2 863 }
e04a16fb
AG
864;
865
866class_body_declarations:
867 class_body_declaration
868| class_body_declarations class_body_declaration
869;
870
871class_body_declaration:
872 class_member_declaration
873| static_initializer
874| constructor_declaration
875| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
876 {
877 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
878 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
879 }
e04a16fb
AG
880;
881
882class_member_declaration:
883 field_declaration
884| method_declaration
885| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
886 { end_class_declaration (1); }
887| interface_declaration /* Added, JDK1.1 inner interfaces */
888 { end_class_declaration (1); }
5f1c312a 889| empty_statement
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
5f1c312a 1088| SC_TK { $$ = NULL_TREE; }
e04a16fb
AG
1089;
1090
1091/* 19.8.4 Productions from 8.5: Static Initializers */
1092static_initializer:
1093 static block
1094 {
c2952b01
APB
1095 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1096 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb 1097 }
e04a16fb
AG
1098;
1099
1100static: /* Test lval.sub_token here */
c2952b01 1101 modifiers
e04a16fb 1102 {
c2952b01
APB
1103 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1104 /* Can't have a static initializer in an innerclass */
1105 if ($1 | ACC_STATIC &&
1106 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1107 parse_error_context
1108 (MODIFIER_WFL (STATIC_TK),
1109 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1110 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1111 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1112 }
1113;
1114
1115/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1116constructor_declaration:
22eed1e6 1117 constructor_header
e04a16fb 1118 {
22eed1e6
APB
1119 current_function_decl = $1;
1120 source_start_java_method (current_function_decl);
e04a16fb 1121 }
22eed1e6 1122 constructor_body
b635eb2f 1123 { finish_method_declaration ($3); }
22eed1e6
APB
1124;
1125
1126constructor_header:
1127 constructor_declarator throws
1128 { $$ = method_header (0, NULL_TREE, $1, $2); }
1129| modifiers constructor_declarator throws
1130 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1131;
1132
1133constructor_declarator:
1134 simple_name OP_TK CP_TK
c2952b01
APB
1135 {
1136 ctxp->formal_parameter_number = 0;
1137 $$ = method_declarator ($1, NULL_TREE);
1138 }
e04a16fb 1139| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1140 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1141;
1142
1143constructor_body:
22eed1e6
APB
1144 /* Unlike regular method, we always need a complete (empty)
1145 body so we can safely perform all the required code
1146 addition (super invocation and field initialization) */
2e5eb5c5 1147 block_begin constructor_block_end
22eed1e6 1148 {
9bbc7d9f 1149 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1150 $$ = $2;
1151 }
2e5eb5c5 1152| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1153 { $$ = $3; }
2e5eb5c5 1154| block_begin block_statements constructor_block_end
22eed1e6 1155 { $$ = $3; }
2e5eb5c5 1156| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1157 { $$ = $4; }
e04a16fb
AG
1158;
1159
2e5eb5c5
APB
1160constructor_block_end:
1161 block_end
5f1c312a 1162;
2e5eb5c5 1163
e04a16fb
AG
1164/* Error recovery for that rule moved down expression_statement: rule. */
1165explicit_constructor_invocation:
1166 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1167 {
1168 $$ = build_method_invocation ($1, NULL_TREE);
1169 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1170 $$ = java_method_add_stmt (current_function_decl, $$);
1171 }
e04a16fb 1172| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1173 {
1174 $$ = build_method_invocation ($1, $3);
1175 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1176 $$ = java_method_add_stmt (current_function_decl, $$);
1177 }
e04a16fb
AG
1178 /* Added, JDK1.1 inner classes. Modified because the rule
1179 'primary' couldn't work. */
1180| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1181 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1182| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1183 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1184;
1185
1186this_or_super: /* Added, simplifies error diagnostics */
1187 THIS_TK
1188 {
9ee9b555 1189 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1190 EXPR_WFL_LINECOL (wfl) = $1.location;
1191 $$ = wfl;
1192 }
1193| SUPER_TK
1194 {
9ee9b555 1195 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1196 EXPR_WFL_LINECOL (wfl) = $1.location;
1197 $$ = wfl;
1198 }
1199;
1200
1201/* 19.9 Productions from 9: Interfaces */
1202/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1203interface_declaration:
1204 INTERFACE_TK identifier
1205 { create_interface (0, $2, NULL_TREE); }
1206 interface_body
e04a16fb
AG
1207| modifiers INTERFACE_TK identifier
1208 { create_interface ($1, $3, NULL_TREE); }
1209 interface_body
e04a16fb
AG
1210| INTERFACE_TK identifier extends_interfaces
1211 { create_interface (0, $2, $3); }
1212 interface_body
e04a16fb
AG
1213| modifiers INTERFACE_TK identifier extends_interfaces
1214 { create_interface ($1, $3, $4); }
1215 interface_body
e04a16fb 1216| INTERFACE_TK identifier error
0b4d333e 1217 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1218| modifiers INTERFACE_TK identifier error
0b4d333e 1219 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1220;
1221
1222extends_interfaces:
1223 EXTENDS_TK interface_type
1224 {
1225 ctxp->interface_number = 1;
1226 $$ = build_tree_list ($2, NULL_TREE);
1227 }
1228| extends_interfaces C_TK interface_type
1229 {
1230 ctxp->interface_number++;
1231 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1232 }
1233| EXTENDS_TK error
1234 {yyerror ("Invalid interface type"); RECOVER;}
1235| extends_interfaces C_TK error
1236 {yyerror ("Missing term"); RECOVER;}
1237;
1238
1239interface_body:
1240 OCB_TK CCB_TK
1241 { $$ = NULL_TREE; }
1242| OCB_TK interface_member_declarations CCB_TK
1243 { $$ = NULL_TREE; }
1244;
1245
1246interface_member_declarations:
1247 interface_member_declaration
1248| interface_member_declarations interface_member_declaration
1249;
1250
1251interface_member_declaration:
1252 constant_declaration
1253| abstract_method_declaration
1254| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1255 { end_class_declaration (1); }
1256| interface_declaration /* Added, JDK1.1 inner interfaces */
1257 { end_class_declaration (1); }
e04a16fb
AG
1258;
1259
1260constant_declaration:
1261 field_declaration
1262;
1263
1264abstract_method_declaration:
1265 method_header SC_TK
1266 {
1267 check_abstract_method_header ($1);
1268 current_function_decl = NULL_TREE; /* FIXME ? */
1269 }
1270| method_header error
1271 {yyerror ("';' expected"); RECOVER;}
1272;
1273
1274/* 19.10 Productions from 10: Arrays */
1275array_initializer:
1276 OCB_TK CCB_TK
1179ebc2 1277 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1278| OCB_TK variable_initializers CCB_TK
f8976021 1279 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1280| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1281 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1282;
1283
1284variable_initializers:
1285 variable_initializer
f8976021
APB
1286 {
1287 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1288 $1, NULL_TREE);
1289 }
e04a16fb 1290| variable_initializers C_TK variable_initializer
1179ebc2
APB
1291 {
1292 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1293 }
e04a16fb
AG
1294| variable_initializers C_TK error
1295 {yyerror ("Missing term"); RECOVER;}
1296;
1297
1298/* 19.11 Production from 14: Blocks and Statements */
1299block:
1300 OCB_TK CCB_TK
7f10c2e2
APB
1301 {
1302 /* Store the location of the `}' when doing xrefs */
1303 if (current_function_decl && flag_emit_xref)
1304 DECL_END_SOURCE_LINE (current_function_decl) =
1305 EXPR_WFL_ADD_COL ($2.location, 1);
1306 $$ = empty_stmt_node;
1307 }
22eed1e6
APB
1308| block_begin block_statements block_end
1309 { $$ = $3; }
1310;
1311
1312block_begin:
1313 OCB_TK
e04a16fb 1314 { enter_block (); }
22eed1e6
APB
1315;
1316
1317block_end:
e04a16fb
AG
1318 CCB_TK
1319 {
1320 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1321 /* Store the location of the `}' when doing xrefs */
1322 if (current_function_decl && flag_emit_xref)
1323 DECL_END_SOURCE_LINE (current_function_decl) =
1324 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb 1325 $$ = exit_block ();
c280e37a
APB
1326 if (!BLOCK_SUBBLOCKS ($$))
1327 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1328 }
1329;
1330
1331block_statements:
1332 block_statement
1333| block_statements block_statement
1334;
1335
1336block_statement:
1337 local_variable_declaration_statement
1338| statement
15fdcfe9 1339 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1340| class_declaration /* Added, JDK1.1 local classes */
1341 {
1342 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1343 end_class_declaration (1);
1344 }
e04a16fb
AG
1345;
1346
1347local_variable_declaration_statement:
1348 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1349;
1350
1351local_variable_declaration:
1352 type variable_declarators
1353 { declare_local_variables (0, $1, $2); }
a003f638 1354| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1355 { declare_local_variables ($1, $2, $3); }
1356;
1357
1358statement:
1359 statement_without_trailing_substatement
1360| labeled_statement
e04a16fb 1361| if_then_statement
e04a16fb 1362| if_then_else_statement
e04a16fb 1363| while_statement
e04a16fb 1364| for_statement
cd9643f7 1365 { $$ = exit_block (); }
e04a16fb
AG
1366;
1367
1368statement_nsi:
1369 statement_without_trailing_substatement
1370| labeled_statement_nsi
e04a16fb 1371| if_then_else_statement_nsi
e04a16fb 1372| while_statement_nsi
e04a16fb 1373| for_statement_nsi
9dd939b2 1374 { $$ = exit_block (); }
e04a16fb
AG
1375;
1376
1377statement_without_trailing_substatement:
1378 block
e04a16fb 1379| empty_statement
e04a16fb 1380| expression_statement
e04a16fb 1381| switch_statement
e04a16fb 1382| do_statement
e04a16fb 1383| break_statement
e04a16fb 1384| continue_statement
e04a16fb
AG
1385| return_statement
1386| synchronized_statement
e04a16fb 1387| throw_statement
e04a16fb 1388| try_statement
e04a16fb
AG
1389;
1390
1391empty_statement:
1392 SC_TK
5f1c312a
APB
1393 {
1394 if (flag_extraneous_semicolon)
1395 {
1396 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1397 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1398 }
1399 $$ = empty_stmt_node;
1400 }
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;
c2952b01
APB
2621 current_class = ctxp->current_class;
2622 }
2623
d19cbcb5
TT
2624 /* If the old and new lexers differ, then free the old one. */
2625 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2626 java_destroy_lexer (ctxp->lexer);
2627
c2952b01
APB
2628 /* Set the single import class file flag to 0 for the current list
2629 of imported things */
2630 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2631 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2632
2633 /* And restore those of the previous context */
2634 if ((ctxp = next)) /* Assignment is really meant here */
2635 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2636 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2637
2638 /* If we pushed a context to parse a class intended to be generated,
2639 we keep it so we can remember the class. What we could actually
2640 do is to just update a list of class names. */
2641 if (generate)
2642 {
2643 toFree->next = ctxp_for_generation;
2644 ctxp_for_generation = toFree;
2645 }
2646 else
2647 free (toFree);
2648}
2649
2650/* Create a parser context for the use of saving some global
2651 variables. */
2652
e04a16fb
AG
2653void
2654java_parser_context_save_global ()
2655{
22eed1e6
APB
2656 if (!ctxp)
2657 {
2658 java_push_parser_context ();
ee07f4f4
APB
2659 ctxp->saved_data_ctx = 1;
2660 }
c2952b01
APB
2661
2662 /* If this context already stores data, create a new one suitable
2663 for data storage. */
ee07f4f4 2664 else if (ctxp->saved_data)
c2952b01
APB
2665 create_new_parser_context (1);
2666
e04a16fb
AG
2667 ctxp->lineno = lineno;
2668 ctxp->current_class = current_class;
2669 ctxp->filename = input_filename;
2670 ctxp->current_function_decl = current_function_decl;
ee07f4f4 2671 ctxp->saved_data = 1;
e04a16fb
AG
2672}
2673
c2952b01
APB
2674/* Restore some global variables from the previous context. Make the
2675 previous context the current one. */
2676
e04a16fb
AG
2677void
2678java_parser_context_restore_global ()
2679{
e04a16fb
AG
2680 lineno = ctxp->lineno;
2681 current_class = ctxp->current_class;
2682 input_filename = ctxp->filename;
2683 current_function_decl = ctxp->current_function_decl;
c2952b01 2684 ctxp->saved_data = 0;
ee07f4f4
APB
2685 if (ctxp->saved_data_ctx)
2686 java_pop_parser_context (0);
e04a16fb
AG
2687}
2688
c2952b01
APB
2689/* Suspend vital data for the current class/function being parsed so
2690 that an other class can be parsed. Used to let local/anonymous
2691 classes be parsed. */
2692
2693static void
2694java_parser_context_suspend ()
e04a16fb 2695{
c2952b01 2696 /* This makes debugging through java_debug_context easier */
3b304f5b 2697 static const char *name = "<inner buffer context>";
e04a16fb 2698
c2952b01
APB
2699 /* Duplicate the previous context, use it to save the globals we're
2700 interested in */
2701 create_new_parser_context (1);
2702 ctxp->current_function_decl = current_function_decl;
2703 ctxp->current_class = current_class;
5e942c50 2704
c2952b01
APB
2705 /* Then create a new context which inherits all data from the
2706 previous one. This will be the new current context */
2707 create_new_parser_context (1);
2708
2709 /* Help debugging */
2710 ctxp->next->filename = name;
2711}
2712
2713/* Resume vital data for the current class/function being parsed so
2714 that an other class can be parsed. Used to let local/anonymous
2715 classes be parsed. The trick is the data storing file position
2716 informations must be restored to their current value, so parsing
2717 can resume as if no context was ever saved. */
2718
2719static void
2720java_parser_context_resume ()
2721{
2722 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2723 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2724 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2725
2726 /* We need to inherit the list of classes to complete/generate */
2727 restored->incomplete_class = old->incomplete_class;
2728 restored->gclass_list = old->gclass_list;
2729 restored->classd_list = old->classd_list;
2730 restored->class_list = old->class_list;
2731
2732 /* Restore the current class and function from the saver */
2733 current_class = saver->current_class;
2734 current_function_decl = saver->current_function_decl;
2735
2736 /* Retrive the restored context */
2737 ctxp = restored;
2738
2739 /* Re-installed the data for the parsing to carry on */
2740 bcopy (&old->marker_begining, &ctxp->marker_begining,
2741 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2742
2743 /* Buffer context can now be discarded */
2744 free (saver);
2745 free (old);
2746}
2747
2748/* Add a new anchor node to which all statement(s) initializing static
2749 and non static initialized upon declaration field(s) will be
2750 linked. */
2751
2752static void
2753java_parser_context_push_initialized_field ()
2754{
2755 tree node;
2756
2757 node = build_tree_list (NULL_TREE, NULL_TREE);
2758 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2759 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2760
2761 node = build_tree_list (NULL_TREE, NULL_TREE);
2762 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2763 CPC_INITIALIZER_LIST (ctxp) = node;
2764
2765 node = build_tree_list (NULL_TREE, NULL_TREE);
2766 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2767 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2768}
2769
2770/* Pop the lists of initialized field. If this lists aren't empty,
c00f0fb2 2771 remember them so we can use it to create and populate the finit$
c2952b01
APB
2772 or <clinit> functions. */
2773
2774static void
2775java_parser_context_pop_initialized_field ()
2776{
2777 tree stmts;
2778 tree class_type = TREE_TYPE (GET_CPC ());
2779
2780 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2781 {
c2952b01
APB
2782 stmts = CPC_INITIALIZER_STMT (ctxp);
2783 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2784 if (stmts && !java_error_count)
2785 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2786 }
2787
c2952b01
APB
2788 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2789 {
2790 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2791 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2792 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2793 /* Keep initialization in order to enforce 8.5 */
2794 if (stmts && !java_error_count)
2795 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2796 }
e04a16fb 2797
c2952b01
APB
2798 /* JDK 1.1 instance initializers */
2799 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2800 {
c2952b01
APB
2801 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2802 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2803 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2804 if (stmts && !java_error_count)
2805 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2806 }
c2952b01
APB
2807}
2808
2809static tree
2810reorder_static_initialized (list)
2811 tree list;
2812{
2813 /* We have to keep things in order. The alias initializer have to
2814 come first, then the initialized regular field, in reverse to
2815 keep them in lexical order. */
2816 tree marker, previous = NULL_TREE;
2817 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2818 if (TREE_CODE (marker) == TREE_LIST
2819 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2820 break;
2821
2822 /* No static initialized, the list is fine as is */
2823 if (!previous)
2824 list = TREE_CHAIN (marker);
2825
2826 /* No marker? reverse the whole list */
2827 else if (!marker)
2828 list = nreverse (list);
2829
2830 /* Otherwise, reverse what's after the marker and the new reordered
2831 sublist will replace the marker. */
b351b287 2832 else
c2952b01
APB
2833 {
2834 TREE_CHAIN (previous) = NULL_TREE;
2835 list = nreverse (list);
2836 list = chainon (TREE_CHAIN (marker), list);
2837 }
2838 return list;
e04a16fb
AG
2839}
2840
c2952b01
APB
2841/* Helper functions to dump the parser context stack. */
2842
2843#define TAB_CONTEXT(C) \
2844 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2845
2846static void
2847java_debug_context_do (tab)
2848 int tab;
2849{
ee07f4f4
APB
2850 struct parser_ctxt *copy = ctxp;
2851 while (copy)
2852 {
c2952b01 2853 TAB_CONTEXT (tab);
ee07f4f4 2854 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2855 TAB_CONTEXT (tab);
ee07f4f4 2856 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2857 TAB_CONTEXT (tab);
2858 fprintf (stderr, "lineno: %d\n", copy->lineno);
2859 TAB_CONTEXT (tab);
ee07f4f4
APB
2860 fprintf (stderr, "package: %s\n",
2861 (copy->package ?
2862 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2863 TAB_CONTEXT (tab);
ee07f4f4 2864 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2865 TAB_CONTEXT (tab);
ee07f4f4
APB
2866 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2867 copy = copy->next;
2868 tab += 2;
2869 }
ee07f4f4
APB
2870}
2871
c2952b01
APB
2872/* Dump the stacked up parser contexts. Intended to be called from a
2873 debugger. */
2874
ee07f4f4
APB
2875void
2876java_debug_context ()
2877{
2878 java_debug_context_do (0);
2879}
2880
c2952b01
APB
2881\f
2882
2883/* Flag for the error report routine to issue the error the first time
2884 it's called (overriding the default behavior which is to drop the
2885 first invocation and honor the second one, taking advantage of a
2886 richer context. */
2887static int force_error = 0;
ee07f4f4 2888
8119c720
APB
2889/* Reporting an constructor invocation error. */
2890static void
2891parse_ctor_invocation_error ()
2892{
2893 if (DECL_CONSTRUCTOR_P (current_function_decl))
2894 yyerror ("Constructor invocation must be first thing in a constructor");
2895 else
2896 yyerror ("Only constructors can invoke constructors");
2897}
2898
2899/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2900
2901static tree
2902parse_jdk1_1_error (msg)
49f48c71 2903 const char *msg;
b67d701b
PB
2904{
2905 sorry (": `%s' JDK1.1(TM) feature", msg);
2906 java_error_count++;
9bbc7d9f 2907 return empty_stmt_node;
b67d701b
PB
2908}
2909
e04a16fb
AG
2910static int do_warning = 0;
2911
2912void
2913yyerror (msg)
49f48c71 2914 const char *msg;
e04a16fb
AG
2915{
2916 static java_lc elc;
2917 static int prev_lineno;
49f48c71 2918 static const char *prev_msg;
e04a16fb 2919
0a2138e2 2920 int save_lineno;
e04a16fb
AG
2921 char *remainder, *code_from_source;
2922 extern struct obstack temporary_obstack;
2923
2924 if (!force_error && prev_lineno == lineno)
2925 return;
2926
2927 /* Save current error location but report latter, when the context is
2928 richer. */
2929 if (ctxp->java_error_flag == 0)
2930 {
2931 ctxp->java_error_flag = 1;
2932 elc = ctxp->elc;
2933 /* Do something to use the previous line if we're reaching the
2934 end of the file... */
2935#ifdef VERBOSE_SKELETON
2936 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2937#endif
2938 return;
2939 }
2940
2941 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2942 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2943 return;
2944
2945 ctxp->java_error_flag = 0;
2946 if (do_warning)
2947 java_warning_count++;
2948 else
2949 java_error_count++;
2950
807bc1db 2951 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2952 {
2953 elc.col = ctxp->p_line->char_col-1;
2954 elc.line = ctxp->p_line->lineno;
2955 }
2956
2957 save_lineno = lineno;
2958 prev_lineno = lineno = elc.line;
2959 prev_msg = msg;
2960
2961 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2962 obstack_grow0 (&temporary_obstack,
2963 code_from_source, strlen (code_from_source));
2964 remainder = obstack_finish (&temporary_obstack);
2965 if (do_warning)
2966 warning ("%s.\n%s", msg, remainder);
2967 else
2968 error ("%s.\n%s", msg, remainder);
2969
2970 /* This allow us to cheaply avoid an extra 'Invalid expression
2971 statement' error report when errors have been already reported on
2972 the same line. This occurs when we report an error but don't have
2973 a synchronization point other than ';', which
2974 expression_statement is the only one to take care of. */
2975 ctxp->prevent_ese = lineno = save_lineno;
2976}
2977
2978static void
15fdcfe9 2979issue_warning_error_from_context (cl, msg, ap)
5e942c50 2980 tree cl;
d4476be2 2981 const char *msg;
15fdcfe9 2982 va_list ap;
5e942c50 2983{
3b304f5b 2984 const char *saved, *saved_input_filename;
15fdcfe9
PB
2985 char buffer [4096];
2986 vsprintf (buffer, msg, ap);
2987 force_error = 1;
5e942c50
APB
2988
2989 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2990 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2991 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2992
2993 /* We have a CL, that's a good reason for using it if it contains data */
2994 saved = ctxp->filename;
2995 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2996 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2997 saved_input_filename = input_filename;
2998 input_filename = ctxp->filename;
15fdcfe9
PB
2999 java_error (NULL);
3000 java_error (buffer);
5e942c50 3001 ctxp->filename = saved;
1886c9d8 3002 input_filename = saved_input_filename;
15fdcfe9 3003 force_error = 0;
5e942c50
APB
3004}
3005
e04a16fb
AG
3006/* Issue an error message at a current source line CL */
3007
15fdcfe9 3008void
df32d2ce 3009parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3010{
d4476be2 3011#ifndef ANSI_PROTOTYPES
e04a16fb 3012 tree cl;
d4476be2 3013 const char *msg;
e04a16fb 3014#endif
e04a16fb
AG
3015 va_list ap;
3016
3017 VA_START (ap, msg);
d4476be2 3018#ifndef ANSI_PROTOTYPES
e04a16fb 3019 cl = va_arg (ap, tree);
d4476be2 3020 msg = va_arg (ap, const char *);
e04a16fb 3021#endif
15fdcfe9
PB
3022 issue_warning_error_from_context (cl, msg, ap);
3023 va_end (ap);
e04a16fb
AG
3024}
3025
3026/* Issue a warning at a current source line CL */
3027
3028static void
df32d2ce 3029parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3030{
d4476be2 3031#ifndef ANSI_PROTOTYPES
e04a16fb 3032 tree cl;
d4476be2 3033 const char *msg;
e04a16fb 3034#endif
e04a16fb
AG
3035 va_list ap;
3036
3037 VA_START (ap, msg);
d4476be2 3038#ifndef ANSI_PROTOTYPES
e04a16fb 3039 cl = va_arg (ap, tree);
d4476be2 3040 msg = va_arg (ap, const char *);
e04a16fb 3041#endif
e04a16fb 3042
c877974e 3043 force_error = do_warning = 1;
15fdcfe9 3044 issue_warning_error_from_context (cl, msg, ap);
c877974e 3045 do_warning = force_error = 0;
15fdcfe9 3046 va_end (ap);
e04a16fb
AG
3047}
3048
82371d41
APB
3049static tree
3050find_expr_with_wfl (node)
3051 tree node;
3052{
3053 while (node)
3054 {
3055 char code;
3056 tree to_return;
3057
3058 switch (TREE_CODE (node))
3059 {
3060 case BLOCK:
c0d87ff6
PB
3061 node = BLOCK_EXPR_BODY (node);
3062 continue;
82371d41
APB
3063
3064 case COMPOUND_EXPR:
3065 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3066 if (to_return)
3067 return to_return;
c0d87ff6
PB
3068 node = TREE_OPERAND (node, 1);
3069 continue;
82371d41
APB
3070
3071 case LOOP_EXPR:
c0d87ff6
PB
3072 node = TREE_OPERAND (node, 0);
3073 continue;
82371d41
APB
3074
3075 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3076 node = TREE_OPERAND (node, 1);
3077 continue;
3078
82371d41
APB
3079 default:
3080 code = TREE_CODE_CLASS (TREE_CODE (node));
3081 if (((code == '1') || (code == '2') || (code == 'e'))
3082 && EXPR_WFL_LINECOL (node))
3083 return node;
ba179f9f 3084 return NULL_TREE;
82371d41
APB
3085 }
3086 }
3087 return NULL_TREE;
3088}
3089
3090/* Issue a missing return statement error. Uses METHOD to figure the
3091 last line of the method the error occurs in. */
3092
3093static void
3094missing_return_error (method)
3095 tree method;
3096{
3097 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3098 parse_error_context (wfl_operator, "Missing return statement");
3099}
3100
3101/* Issue an unreachable statement error. From NODE, find the next
3102 statement to report appropriately. */
3103static void
3104unreachable_stmt_error (node)
3105 tree node;
3106{
3107 /* Browse node to find the next expression node that has a WFL. Use
3108 the location to report the error */
3109 if (TREE_CODE (node) == COMPOUND_EXPR)
3110 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3111 else
3112 node = find_expr_with_wfl (node);
3113
3114 if (node)
3115 {
3116 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3117 parse_error_context (wfl_operator, "Unreachable statement");
3118 }
3119 else
3120 fatal ("Can't get valid statement - unreachable_stmt_error");
3121}
3122
c877974e 3123int
e04a16fb
AG
3124java_report_errors ()
3125{
3126 if (java_error_count)
3127 fprintf (stderr, "%d error%s",
3128 java_error_count, (java_error_count == 1 ? "" : "s"));
3129 if (java_warning_count)
3130 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3131 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3132 if (java_error_count || java_warning_count)
3133 putc ('\n', stderr);
c877974e 3134 return java_error_count;
e04a16fb
AG
3135}
3136
3137static char *
3138java_accstring_lookup (flags)
3139 int flags;
3140{
3141 static char buffer [80];
3142#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3143
3144 /* Access modifier looked-up first for easier report on forbidden
3145 access. */
3146 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3147 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3148 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3149 if (flags & ACC_STATIC) COPY_RETURN ("static");
3150 if (flags & ACC_FINAL) COPY_RETURN ("final");
3151 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3152 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3153 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3154 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3155 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3156 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3157
3158 buffer [0] = '\0';
3159 return buffer;
3160#undef COPY_RETURN
3161}
3162
b67d701b
PB
3163/* Issuing error messages upon redefinition of classes, interfaces or
3164 variables. */
3165
e04a16fb 3166static void
b67d701b 3167classitf_redefinition_error (context, id, decl, cl)
49f48c71 3168 const char *context;
e04a16fb
AG
3169 tree id, decl, cl;
3170{
3171 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3172 context, IDENTIFIER_POINTER (id),
3173 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3174 /* Here we should point out where its redefined. It's a unicode. FIXME */
3175}
3176
b67d701b
PB
3177static void
3178variable_redefinition_error (context, name, type, line)
3179 tree context, name, type;
3180 int line;
3181{
49f48c71 3182 const char *type_name;
b67d701b
PB
3183
3184 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3185 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3186 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3187 else
0a2138e2 3188 type_name = lang_printable_name (type, 0);
b67d701b
PB
3189
3190 parse_error_context (context,
781b0558 3191 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3192 IDENTIFIER_POINTER (name),
3193 type_name, IDENTIFIER_POINTER (name), line);
3194}
3195
c583dd46
APB
3196static tree
3197build_array_from_name (type, type_wfl, name, ret_name)
3198 tree type, type_wfl, name, *ret_name;
3199{
3200 int more_dims = 0;
49f48c71 3201 const char *string;
c583dd46
APB
3202
3203 /* Eventually get more dims */
3204 string = IDENTIFIER_POINTER (name);
3205 while (string [more_dims] == '[')
3206 more_dims++;
3207
3208 /* If we have, then craft a new type for this variable */
3209 if (more_dims)
3210 {
c0d87ff6 3211 name = get_identifier (&string [more_dims]);
c583dd46 3212
34f4db93
APB
3213 /* If we have a pointer, use its type */
3214 if (TREE_CODE (type) == POINTER_TYPE)
3215 type = TREE_TYPE (type);
c583dd46
APB
3216
3217 /* Building the first dimension of a primitive type uses this
3218 function */
3219 if (JPRIMITIVE_TYPE_P (type))
3220 {
3221 type = build_java_array_type (type, -1);
22eed1e6 3222 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3223 more_dims--;
3224 }
3225 /* Otherwise, if we have a WFL for this type, use it (the type
3226 is already an array on an unresolved type, and we just keep
3227 on adding dimensions) */
3228 else if (type_wfl)
3229 type = type_wfl;
3230
3231 /* Add all the dimensions */
3232 while (more_dims--)
3233 type = build_unresolved_array_type (type);
3234
3235 /* The type may have been incomplete in the first place */
3236 if (type_wfl)
3237 type = obtain_incomplete_type (type);
3238 }
3239
c2952b01
APB
3240 if (ret_name)
3241 *ret_name = name;
c583dd46
APB
3242 return type;
3243}
3244
e04a16fb
AG
3245/* Build something that the type identifier resolver will identify as
3246 being an array to an unresolved type. TYPE_WFL is a WFL on a
3247 identifier. */
3248
3249static tree
3250build_unresolved_array_type (type_or_wfl)
3251 tree type_or_wfl;
3252{
49f48c71 3253 const char *ptr;
e04a16fb 3254
1886c9d8 3255 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3256 just create a array type */
3257 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3258 {
3259 tree type = build_java_array_type (type_or_wfl, -1);
3260 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3261 return type;
3262 }
3263
3264 obstack_1grow (&temporary_obstack, '[');
3265 obstack_grow0 (&temporary_obstack,
3266 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3267 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3268 ptr = obstack_finish (&temporary_obstack);
34d4df06
APB
3269 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3270 return type_or_wfl;
e04a16fb
AG
3271}
3272
e04a16fb
AG
3273static void
3274parser_add_interface (class_decl, interface_decl, wfl)
3275 tree class_decl, interface_decl, wfl;
3276{
3277 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3278 parse_error_context (wfl, "Interface `%s' repeated",
3279 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3280}
3281
3282/* Bulk of common class/interface checks. Return 1 if an error was
3283 encountered. TAG is 0 for a class, 1 for an interface. */
3284
3285static int
3286check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3287 int is_interface, flags;
3288 tree raw_name, qualified_name, decl, cl;
3289{
3290 tree node;
c2952b01
APB
3291 int sca = 0; /* Static class allowed */
3292 int icaf = 0; /* Inner class allowed flags */
3293 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3294
3295 if (!quiet_flag)
c2952b01
APB
3296 fprintf (stderr, " %s%s %s",
3297 (CPC_INNER_P () ? "inner" : ""),
3298 (is_interface ? "interface" : "class"),
e04a16fb
AG
3299 IDENTIFIER_POINTER (qualified_name));
3300
3301 /* Scope of an interface/class type name:
3302 - Can't be imported by a single type import
3303 - Can't already exists in the package */
3304 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
34d4df06
APB
3305 && (node = find_name_in_single_imports (raw_name))
3306 && !CPC_INNER_P ())
e04a16fb
AG
3307 {
3308 parse_error_context
3309 (cl, "%s name `%s' clashes with imported type `%s'",
3310 (is_interface ? "Interface" : "Class"),
3311 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3312 return 1;
3313 }
3314 if (decl && CLASS_COMPLETE_P (decl))
3315 {
b67d701b
PB
3316 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3317 qualified_name, decl, cl);
e04a16fb
AG
3318 return 1;
3319 }
3320
c2952b01
APB
3321 if (check_inner_class_redefinition (raw_name, cl))
3322 return 1;
3323
3324 /* If public, file name should match class/interface name, except
3325 when dealing with an inner class */
3326 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3327 {
49f48c71 3328 const char *f;
e04a16fb
AG
3329
3330 /* Contains OS dependent assumption on path separator. FIXME */
3331 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3332 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3333 f--)
3334 ;
847fe791 3335 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3336 f++;
3337 if (strncmp (IDENTIFIER_POINTER (raw_name),
3338 f , IDENTIFIER_LENGTH (raw_name)) ||
3339 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3340 parse_error_context
3341 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3342 (is_interface ? "interface" : "class"),
3343 IDENTIFIER_POINTER (qualified_name),
3344 IDENTIFIER_POINTER (raw_name));
3345 }
3346
c2952b01
APB
3347 /* Static classes can be declared only in top level classes. Note:
3348 once static, a inner class is a top level class. */
3349 if (flags & ACC_STATIC)
3350 {
3351 /* Catch the specific error of declaring an class inner class
3352 with no toplevel enclosing class. Prevent check_modifiers from
3353 complaining a second time */
3354 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3355 {
3356 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3357 IDENTIFIER_POINTER (qualified_name));
3358 sca = ACC_STATIC;
3359 }
3360 /* Else, in the context of a top-level class declaration, let
3361 `check_modifiers' do its job, otherwise, give it a go */
3362 else
3363 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3364 }
3365
a40d21da 3366 /* Inner classes can be declared private or protected
c2952b01
APB
3367 within their enclosing classes. */
3368 if (CPC_INNER_P ())
3369 {
3370 /* A class which is local to a block can't be public, private,
3371 protected or static. But it is created final, so allow this
3372 one. */
3373 if (current_function_decl)
3374 icaf = sca = uaaf = ACC_FINAL;
3375 else
3376 {
3377 check_modifiers_consistency (flags);
3378 icaf = ACC_PRIVATE|ACC_PROTECTED;
3379 }
3380 }
3381
a40d21da
APB
3382 if (is_interface)
3383 {
3384 if (CPC_INNER_P ())
3385 uaaf = INTERFACE_INNER_MODIFIERS;
3386 else
3387 uaaf = INTERFACE_MODIFIERS;
3388
3389 check_modifiers ("Illegal modifier `%s' for interface declaration",
3390 flags, uaaf);
3391 }
2884c41e 3392 else
a40d21da
APB
3393 check_modifiers ((current_function_decl ?
3394 "Illegal modifier `%s' for local class declaration" :
3395 "Illegal modifier `%s' for class declaration"),
c2952b01 3396 flags, uaaf|sca|icaf);
e04a16fb
AG
3397 return 0;
3398}
3399
c2952b01
APB
3400static void
3401make_nested_class_name (cpc_list)
3402 tree cpc_list;
3403{
3404 tree name;
3405
3406 if (!cpc_list)
3407 return;
3408 else
3409 make_nested_class_name (TREE_CHAIN (cpc_list));
3410
3411 /* Pick the qualified name when dealing with the first upmost
3412 enclosing class */
3413 name = (TREE_CHAIN (cpc_list) ?
3414 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3415 obstack_grow (&temporary_obstack,
3416 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3417 /* Why is NO_DOLLAR_IN_LABEL defined? */
3418#if 0
3419#ifdef NO_DOLLAR_IN_LABEL
3420 fatal ("make_nested_class_name: Can't use '$' as a separator "
3421 "for inner classes");
3422#endif
3423#endif
3424 obstack_1grow (&temporary_obstack, '$');
3425}
3426
3427/* Can't redefine a class already defined in an earlier scope. */
3428
3429static int
3430check_inner_class_redefinition (raw_name, cl)
3431 tree raw_name, cl;
3432{
3433 tree scope_list;
3434
3435 for (scope_list = GET_CPC_LIST (); scope_list;
3436 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3437 if (raw_name == GET_CPC_UN_NODE (scope_list))
3438 {
3439 parse_error_context
3440 (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",
3441 IDENTIFIER_POINTER (raw_name));
3442 return 1;
3443 }
3444 return 0;
3445}
3446
3447static tree
3448find_as_inner_class (enclosing, name, cl)
3449 tree enclosing, name, cl;
3450{
3451 tree qual, to_return;
3452 if (!enclosing)
3453 return NULL_TREE;
3454
3455 name = TYPE_NAME (name);
3456
3457 /* First search: within the scope of `enclosing', search for name */
3458 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3459 qual = EXPR_WFL_QUALIFICATION (cl);
3460 else if (cl)
3461 qual = build_tree_list (cl, NULL_TREE);
3462 else
3463 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3464
3465 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3466 return to_return;
3467
3468 /* We're dealing with a qualified name. Try to resolve thing until
3469 we get something that is an enclosing class. */
3470 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3471 {
3472 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3473
0c2b8145
APB
3474 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3475 qual = TREE_CHAIN (qual))
c2952b01
APB
3476 {
3477 acc = merge_qualified_name (acc,
3478 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3479 BUILD_PTR_FROM_NAME (ptr, acc);
3480 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3481 }
3482
3483 /* A NULL qual and a decl means that the search ended
3484 successfully?!? We have to do something then. FIXME */
3485
3486 if (decl)
3487 enclosing = decl;
3488 else
3489 qual = EXPR_WFL_QUALIFICATION (cl);
3490 }
3491 /* Otherwise, create a qual for the other part of the resolution. */
3492 else
3493 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3494
1e12ab9b 3495 return find_as_inner_class_do (qual, enclosing);
c2952b01
APB
3496}
3497
3498/* We go inside the list of sub classes and try to find a way
3499 through. */
3500
3501static tree
3502find_as_inner_class_do (qual, enclosing)
3503 tree qual, enclosing;
3504{
3505 if (!qual)
3506 return NULL_TREE;
3507
3508 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3509 {
3510 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3511 tree next_enclosing = NULL_TREE;
3512 tree inner_list;
3513
3514 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3515 inner_list; inner_list = TREE_CHAIN (inner_list))
3516 {
3517 if (TREE_VALUE (inner_list) == name_to_match)
3518 {
3519 next_enclosing = TREE_PURPOSE (inner_list);
3520 break;
3521 }
3522 }
3523 enclosing = next_enclosing;
3524 }
3525
3526 return (!qual && enclosing ? enclosing : NULL_TREE);
3527}
3528
3529/* Reach all inner classes and tie their unqualified name to a
3530 DECL. */
3531
3532static void
3533set_nested_class_simple_name_value (outer, set)
3534 tree outer;
3535 int set;
3536{
3537 tree l;
3538
3539 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3540 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3541 TREE_PURPOSE (l) : NULL_TREE);
3542}
3543
3544static void
3545link_nested_class_to_enclosing ()
3546{
3547 if (GET_ENCLOSING_CPC ())
3548 {
3549 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3550 DECL_INNER_CLASS_LIST (enclosing) =
3551 tree_cons (GET_CPC (), GET_CPC_UN (),
3552 DECL_INNER_CLASS_LIST (enclosing));
3553 enclosing = enclosing;
3554 }
3555}
3556
3557static tree
3558maybe_make_nested_class_name (name)
3559 tree name;
3560{
3561 tree id = NULL_TREE;
3562
3563 if (CPC_INNER_P ())
3564 {
3565 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3566 obstack_grow0 (&temporary_obstack,
3567 IDENTIFIER_POINTER (name),
3568 IDENTIFIER_LENGTH (name));
c2952b01
APB
3569 id = get_identifier (obstack_finish (&temporary_obstack));
3570 if (ctxp->package)
3571 QUALIFIED_P (id) = 1;
3572 }
3573 return id;
3574}
3575
3576/* If DECL is NULL, create and push a new DECL, record the current
3577 line CL and do other maintenance things. */
3578
e04a16fb 3579static tree
c2952b01
APB
3580maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3581 tree decl, raw_name, qualified_name, cl;
e04a16fb 3582{
5e942c50 3583 if (!decl)
e04a16fb 3584 decl = push_class (make_class (), qualified_name);
c2952b01 3585
e04a16fb
AG
3586 /* Take care of the file and line business */
3587 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3588 /* If we're emiting xrefs, store the line/col number information */
3589 if (flag_emit_xref)
3590 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3591 else
3592 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3593 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3594 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3595 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3596
c2952b01
APB
3597 PUSH_CPC (decl, raw_name);
3598 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3599
e04a16fb
AG
3600 /* Link the declaration to the already seen ones */
3601 TREE_CHAIN (decl) = ctxp->class_list;
3602 ctxp->class_list = decl;
5e942c50 3603
23a79c61 3604 /* Create a new nodes in the global lists */
5e942c50 3605 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3606 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3607
e04a16fb
AG
3608 /* Install a new dependency list element */
3609 create_jdep_list (ctxp);
3610
3611 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3612 IDENTIFIER_POINTER (qualified_name)));
3613 return decl;
3614}
3615
3616static void
3617add_superinterfaces (decl, interface_list)
3618 tree decl, interface_list;
3619{
3620 tree node;
3621 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3622 takes care of ensuring that:
3623 - This is an accessible interface type,
3624 - Circularity detection.
3625 parser_add_interface is then called. If present but not defined,
3626 the check operation is delayed until the super interface gets
3627 defined. */
3628 for (node = interface_list; node; node = TREE_CHAIN (node))
3629 {
15fdcfe9 3630 tree current = TREE_PURPOSE (node);
5e942c50
APB
3631 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3632 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3633 {
5e942c50
APB
3634 if (!parser_check_super_interface (idecl, decl, current))
3635 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3636 }
3637 else
3638 register_incomplete_type (JDEP_INTERFACE,
3639 current, decl, NULL_TREE);
3640 }
3641}
3642
3643/* Create an interface in pass1 and return its decl. Return the
3644 interface's decl in pass 2. */
3645
3646static tree
3647create_interface (flags, id, super)
3648 int flags;
3649 tree id, super;
3650{
e04a16fb 3651 tree raw_name = EXPR_WFL_NODE (id);
98a52c2c 3652 tree q_name = parser_qualified_classname (raw_name);
e04a16fb
AG
3653 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3654
3655 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3656
3657 /* Basic checks: scope, redefinition, modifiers */
3658 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3659 {
3660 PUSH_ERROR ();
3661 return NULL_TREE;
3662 }
3663
3664 /* Suspend the current parsing context if we're parsing an inner
3665 interface */
3666 if (CPC_INNER_P ())
3667 java_parser_context_suspend ();
3668
3669 /* Push a new context for (static) initialized upon declaration fields */
3670 java_parser_context_push_initialized_field ();
e04a16fb
AG
3671
3672 /* Interface modifiers check
3673 - public/abstract allowed (already done at that point)
3674 - abstract is obsolete (comes first, it's a warning, or should be)
3675 - Can't use twice the same (checked in the modifier rule) */
c877974e 3676 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3677 parse_warning_context
3678 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3679 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3680
3681 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3682 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3683
3684 /* Set super info and mark the class a complete */
2aa11e97 3685 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3686 object_type_node, ctxp->interface_number);
3687 ctxp->interface_number = 0;
3688 CLASS_COMPLETE_P (decl) = 1;
3689 add_superinterfaces (decl, super);
3690
3691 return decl;
3692}
3693
c2952b01
APB
3694/* Anonymous class counter. Will be reset to 1 every time a non
3695 anonymous class gets created. */
3696static int anonymous_class_counter = 1;
3697
3698/* Patch anonymous class CLASS, by either extending or implementing
3699 DEP. */
3700
3701static void
3702patch_anonymous_class (type_decl, class_decl, wfl)
3703 tree type_decl, class_decl, wfl;
3704{
3705 tree class = TREE_TYPE (class_decl);
3706 tree type = TREE_TYPE (type_decl);
3707 tree binfo = TYPE_BINFO (class);
3708
3709 /* If it's an interface, implement it */
3710 if (CLASS_INTERFACE (type_decl))
3711 {
3712 tree s_binfo;
3713 int length;
3714
3715 if (parser_check_super_interface (type_decl, class_decl, wfl))
3716 return;
3717
3718 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3719 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3720 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3721 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3722 /* And add the interface */
3723 parser_add_interface (class_decl, type_decl, wfl);
3724 }
3725 /* Otherwise, it's a type we want to extend */
3726 else
3727 {
3728 if (parser_check_super (type_decl, class_decl, wfl))
3729 return;
3730 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3731 }
3732}
3733
3734static tree
3735create_anonymous_class (location, type_name)
3736 int location;
3737 tree type_name;
3738{
3739 char buffer [80];
3740 tree super = NULL_TREE, itf = NULL_TREE;
3741 tree id, type_decl, class;
3742
3743 /* The unqualified name of the anonymous class. It's just a number. */
3744 sprintf (buffer, "%d", anonymous_class_counter++);
3745 id = build_wfl_node (get_identifier (buffer));
3746 EXPR_WFL_LINECOL (id) = location;
3747
3748 /* We know about the type to extend/implement. We go ahead */
3749 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3750 {
3751 /* Create a class which either implements on extends the designated
3752 class. The class bears an innacessible name. */
3753 if (CLASS_INTERFACE (type_decl))
3754 {
3755 /* It's OK to modify it here. It's been already used and
3756 shouldn't be reused */
3757 ctxp->interface_number = 1;
3758 /* Interfaces should presented as a list of WFLs */
3759 itf = build_tree_list (type_name, NULL_TREE);
3760 }
3761 else
3762 super = type_name;
3763 }
3764
3765 class = create_class (ACC_FINAL, id, super, itf);
3766
3767 /* We didn't know anything about the stuff. We register a dependence. */
3768 if (!type_decl)
3769 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3770
3771 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3772 return class;
3773}
3774
a40d21da 3775/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3776 interface's decl in pass 2. */
3777
3778static tree
3779create_class (flags, id, super, interfaces)
3780 int flags;
3781 tree id, super, interfaces;
3782{
e04a16fb
AG
3783 tree raw_name = EXPR_WFL_NODE (id);
3784 tree class_id, decl;
9ee9b555 3785 tree super_decl_type;
e04a16fb 3786
98a52c2c 3787 class_id = parser_qualified_classname (raw_name);
e04a16fb
AG
3788 decl = IDENTIFIER_CLASS_VALUE (class_id);
3789 EXPR_WFL_NODE (id) = class_id;
3790
3791 /* Basic check: scope, redefinition, modifiers */
3792 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3793 {
3794 PUSH_ERROR ();
3795 return NULL_TREE;
3796 }
3797
3798 /* Suspend the current parsing context if we're parsing an inner
3799 class or an anonymous class. */
3800 if (CPC_INNER_P ())
3801 java_parser_context_suspend ();
3802 /* Push a new context for (static) initialized upon declaration fields */
3803 java_parser_context_push_initialized_field ();
e04a16fb
AG
3804
3805 /* Class modifier check:
3806 - Allowed modifier (already done at that point)
3807 - abstract AND final forbidden
3808 - Public classes defined in the correct file */
3809 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3810 parse_error_context
3811 (id, "Class `%s' can't be declared both abstract and final",
3812 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3813
3814 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3815 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3816
3817 /* If SUPER exists, use it, otherwise use Object */
3818 if (super)
3819 {
3820 /* Can't extend java.lang.Object */
3821 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3822 {
3823 parse_error_context (id, "Can't extend `java.lang.Object'");
3824 return NULL_TREE;
3825 }
3826
2c3199bc
PB
3827 super_decl_type =
3828 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3829 }
3830 else if (TREE_TYPE (decl) != object_type_node)
3831 super_decl_type = object_type_node;
3832 /* We're defining java.lang.Object */
3833 else
3834 super_decl_type = NULL_TREE;
3835
3836 /* Set super info and mark the class a complete */
3837 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3838 ctxp->interface_number);
3839 ctxp->interface_number = 0;
3840 CLASS_COMPLETE_P (decl) = 1;
3841 add_superinterfaces (decl, interfaces);
3842
c2952b01
APB
3843 /* Add the private this$<n> field, Replicate final locals still in
3844 scope as private final fields mangled like val$<local_name>.
3845 This doesn't not occur for top level (static) inner classes. */
3846 if (PURE_INNER_CLASS_DECL_P (decl))
3847 add_inner_class_fields (decl, current_function_decl);
3848
7f10c2e2
APB
3849 /* If doing xref, store the location at which the inherited class
3850 (if any) was seen. */
3851 if (flag_emit_xref && super)
3852 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3853
5e942c50
APB
3854 /* Eventually sets the @deprecated tag flag */
3855 CHECK_DEPRECATED (decl);
3856
165f37bc
APB
3857 /* Reset the anonymous class counter when declaring non inner classes */
3858 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3859 anonymous_class_counter = 1;
3860
e04a16fb
AG
3861 return decl;
3862}
3863
c2952b01 3864/* End a class declaration: register the statements used to create
c00f0fb2 3865 finit$ and <clinit>, pop the current class and resume the prior
c2952b01
APB
3866 parser context if necessary. */
3867
3868static void
3869end_class_declaration (resume)
3870 int resume;
3871{
3872 /* If an error occured, context weren't pushed and won't need to be
3873 popped by a resume. */
3874 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3875
3876 java_parser_context_pop_initialized_field ();
3877 POP_CPC ();
3878 if (resume && no_error_occured)
3879 java_parser_context_resume ();
93220702
APB
3880
3881 /* We're ending a class declaration, this is a good time to reset
3882 the interface cout. Note that might have been already done in
3883 create_interface, but if at that time an inner class was being
3884 dealt with, the interface count was reset in a context created
3885 for the sake of handling inner classes declaration. */
3886 ctxp->interface_number = 0;
c2952b01
APB
3887}
3888
3889static void
3890add_inner_class_fields (class_decl, fct_decl)
3891 tree class_decl;
3892 tree fct_decl;
3893{
3894 tree block, marker, f;
3895
3896 f = add_field (TREE_TYPE (class_decl),
3897 build_current_thisn (TREE_TYPE (class_decl)),
3898 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3899 ACC_PRIVATE);
3900 FIELD_THISN (f) = 1;
3901
3902 if (!fct_decl)
3903 return;
3904
3905 for (block = GET_CURRENT_BLOCK (fct_decl);
3906 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3907 {
3908 tree decl;
3909 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3910 {
3911 char *name, *pname;
3912 tree wfl, init, list;
3913
3914 /* Avoid non final arguments. */
3915 if (!LOCAL_FINAL (decl))
3916 continue;
3917
3918 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3919 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3920 wfl = build_wfl_node (get_identifier (name));
3921 init = build_wfl_node (get_identifier (pname));
3922 /* Build an initialization for the field: it will be
c00f0fb2 3923 initialized by a parameter added to finit$, bearing a
c2952b01 3924 mangled name of the field itself (param$<n>.) The
c00f0fb2 3925 parameter is provided to finit$ by the constructor
c2952b01
APB
3926 invoking it (hence the constructor will also feature a
3927 hidden parameter, set to the value of the outer context
3928 local at the time the inner class is created.)
3929
3930 Note: we take into account all possible locals that can
3931 be accessed by the inner class. It's actually not trivial
3932 to minimize these aliases down to the ones really
3933 used. One way to do that would be to expand all regular
c00f0fb2 3934 methods first, then finit$ to get a picture of what's
c2952b01
APB
3935 used. It works with the exception that we would have to
3936 go back on all constructor invoked in regular methods to
3937 have their invokation reworked (to include the right amount
3938 of alias initializer parameters.)
3939
3940 The only real way around, I think, is a first pass to
3941 identify locals really used in the inner class. We leave
3942 the flag FIELD_LOCAL_ALIAS_USED around for that future
3943 use.
3944
3945 On the other hand, it only affect local inner classes,
c00f0fb2 3946 whose constructors (and finit$ call) will be featuring
c2952b01
APB
3947 unecessary arguments. It's easy for a developper to keep
3948 this number of parameter down by using the `final'
3949 keyword only when necessary. For the time being, we can
3950 issue a warning on unecessary finals. FIXME */
3951 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3952 wfl, init);
3953
3954 /* Register the field. The TREE_LIST holding the part
3955 initialized/initializer will be marked ARG_FINAL_P so
3956 that the created field can be marked
3957 FIELD_LOCAL_ALIAS. */
3958 list = build_tree_list (wfl, init);
3959 ARG_FINAL_P (list) = 1;
3960 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3961 }
3962 }
3963
3964 if (!CPC_INITIALIZER_STMT (ctxp))
3965 return;
3966
3967 /* If we ever registered an alias field, insert and marker to
3968 remeber where the list ends. The second part of the list (the one
3969 featuring initialized fields) so it can be later reversed to
3970 enforce 8.5. The marker will be removed during that operation. */
3971 marker = build_tree_list (NULL_TREE, NULL_TREE);
3972 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3973 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3974}
3975
e04a16fb
AG
3976/* Can't use lookup_field () since we don't want to load the class and
3977 can't set the CLASS_LOADED_P flag */
3978
3979static tree
3980find_field (class, name)
3981 tree class;
3982 tree name;
3983{
3984 tree decl;
3985 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3986 {
3987 if (DECL_NAME (decl) == name)
3988 return decl;
3989 }
3990 return NULL_TREE;
3991}
3992
3993/* Wrap around lookup_field that doesn't potentially upset the value
3994 of CLASS */
3995
3996static tree
3997lookup_field_wrapper (class, name)
3998 tree class, name;
3999{
4000 tree type = class;
9a7ab4b3 4001 tree decl = NULL_TREE;
c877974e 4002 java_parser_context_save_global ();
f2760b27
APB
4003
4004 /* Last chance: if we're within the context of an inner class, we
4005 might be trying to access a local variable defined in an outer
4006 context. We try to look for it now. */
9a7ab4b3 4007 if (INNER_CLASS_TYPE_P (class))
f2760b27
APB
4008 {
4009 char *alias_buffer;
9a7ab4b3 4010 tree new_name;
f2760b27 4011 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
9a7ab4b3
APB
4012 new_name = get_identifier (alias_buffer);
4013 decl = lookup_field (&type, new_name);
f2760b27
APB
4014 if (decl && decl != error_mark_node)
4015 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4016 }
9a7ab4b3
APB
4017 if (!decl || decl == error_mark_node)
4018 {
4019 type = class;
4020 decl = lookup_field (&type, name);
4021 }
f2760b27 4022
c877974e 4023 java_parser_context_restore_global ();
93024893 4024 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4025}
4026
4027/* Find duplicate field within the same class declarations and report
c583dd46
APB
4028 the error. Returns 1 if a duplicated field was found, 0
4029 otherwise. */
e04a16fb
AG
4030
4031static int
c583dd46 4032duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4033 tree new_field_name, new_type, cl;
e04a16fb
AG
4034{
4035 /* This might be modified to work with method decl as well */
c2952b01 4036 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4037 if (decl)
4038 {
c2e3db92 4039 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4040 ((TREE_CODE (new_type) == POINTER_TYPE
4041 && TREE_TYPE (new_type) == NULL_TREE) ?
4042 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4043 lang_printable_name (new_type, 1)));
c877974e
APB
4044 /* The type may not have been completed by the time we report
4045 the error */
c2e3db92 4046 char *t2 = xstrdup (purify_type_name
4a5f66c3 4047 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4048 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4049 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4050 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4051 parse_error_context
4052 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4053 t1, IDENTIFIER_POINTER (new_field_name),
4054 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4055 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4056 free (t1);
4057 free (t2);
c583dd46 4058 return 1;
e04a16fb 4059 }
c583dd46 4060 return 0;
e04a16fb
AG
4061}
4062
4063/* Field registration routine. If TYPE doesn't exist, field
4064 declarations are linked to the undefined TYPE dependency list, to
4065 be later resolved in java_complete_class () */
4066
4067static void
4068register_fields (flags, type, variable_list)
4069 int flags;
4070 tree type, variable_list;
4071{
c583dd46 4072 tree current, saved_type;
c2952b01 4073 tree class_type = NULL_TREE;
e04a16fb
AG
4074 int saved_lineno = lineno;
4075 int must_chain = 0;
4076 tree wfl = NULL_TREE;
4077
c2952b01
APB
4078 if (GET_CPC ())
4079 class_type = TREE_TYPE (GET_CPC ());
4080
4081 if (!class_type || class_type == error_mark_node)
4082 return;
4083
e04a16fb
AG
4084 /* If we're adding fields to interfaces, those fields are public,
4085 static, final */
4086 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4087 {
4088 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4089 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4090 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4091 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4092 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4093 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4094 check_modifiers ("Illegal interface member modifier `%s'", flags,
4095 INTERFACE_FIELD_MODIFIERS);
4096 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4097 }
4098
c583dd46
APB
4099 /* Obtain a suitable type for resolution, if necessary */
4100 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4101
4102 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4103 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4104
c583dd46
APB
4105 for (current = variable_list, saved_type = type; current;
4106 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4107 {
c877974e 4108 tree real_type;
c583dd46 4109 tree field_decl;
e04a16fb
AG
4110 tree cl = TREE_PURPOSE (current);
4111 tree init = TREE_VALUE (current);
4112 tree current_name = EXPR_WFL_NODE (cl);
4113
cf1748bf 4114 /* Can't declare non-final static fields in inner classes */
c2952b01 4115 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
cf1748bf 4116 && !(flags & ACC_FINAL))
c2952b01 4117 parse_error_context
cf1748bf 4118 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
c2952b01
APB
4119 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4120 lang_printable_name (class_type, 0));
4121
c583dd46
APB
4122 /* Process NAME, as it may specify extra dimension(s) for it */
4123 type = build_array_from_name (type, wfl, current_name, &current_name);
4124
c583dd46
APB
4125 /* Type adjustment. We may have just readjusted TYPE because
4126 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4127 a reference if we can and don't have one already. Also
4128 change the name if we have an init. */
4129 if (type != saved_type)
4130 {
1886c9d8 4131 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4132 if (init)
4133 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4134 }
e04a16fb 4135
c877974e
APB
4136 real_type = GET_REAL_TYPE (type);
4137 /* Check for redeclarations */
4138 if (duplicate_declaration_error_p (current_name, real_type, cl))
4139 continue;
4140
c583dd46 4141 /* Set lineno to the line the field was found and create a
5e942c50 4142 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4143 if (flag_emit_xref)
4144 lineno = EXPR_WFL_LINECOL (cl);
4145 else
4146 lineno = EXPR_WFL_LINENO (cl);
c877974e 4147 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4148 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4149
4150 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4151 mark the created field FIELD_LOCAL_ALIAS, so that we can
c00f0fb2 4152 hide parameters to this inner class finit$ and constructors. */
c2952b01
APB
4153 if (ARG_FINAL_P (current))
4154 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4155
4156 /* Check if we must chain. */
4157 if (must_chain)
4158 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4159
c583dd46
APB
4160 /* If we have an initialization value tied to the field */
4161 if (init)
4162 {
4163 /* The field is declared static */
e04a16fb 4164 if (flags & ACC_STATIC)
e04a16fb 4165 {
7525cc04
APB
4166 /* We include the field and its initialization part into
4167 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4168 walked, field initializations will be processed and
4169 fields initialized with known constants will be taken
4170 out of <clinit> and have their DECL_INITIAL set
7525cc04 4171 appropriately. */
c2952b01
APB
4172 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4173 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4174 if (TREE_OPERAND (init, 1)
4175 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4176 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4177 }
5e942c50
APB
4178 /* A non-static field declared with an immediate initialization is
4179 to be initialized in <init>, if any. This field is remembered
4180 to be processed at the time of the generation of <init>. */
c583dd46
APB
4181 else
4182 {
c2952b01
APB
4183 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4184 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4185 }
5b09b33e 4186 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4187 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4188 }
4189 }
4190 lineno = saved_lineno;
4191}
4192
c00f0fb2
APB
4193/* Generate finit$, using the list of initialized fields to populate
4194 its body. finit$'s parameter(s) list is adjusted to include the
c2952b01
APB
4195 one(s) used to initialized the field(s) caching outer context
4196 local(s). */
22eed1e6 4197
c2952b01
APB
4198static tree
4199generate_finit (class_type)
4200 tree class_type;
22eed1e6 4201{
c2952b01
APB
4202 int count = 0;
4203 tree list = TYPE_FINIT_STMT_LIST (class_type);
4204 tree mdecl, current, parms;
4205
4206 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4207 class_type, NULL_TREE,
4208 &count);
4209 CRAFTED_PARAM_LIST_FIXUP (parms);
4210 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4211 finit_identifier_node, parms);
4212 fix_method_argument_names (parms, mdecl);
4213 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4214 mdecl, NULL_TREE);
4215 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4216 start_artificial_method_body (mdecl);
4217
c2952b01 4218 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4219 java_method_add_stmt (mdecl,
4220 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4221 current));
22eed1e6 4222 end_artificial_method_body (mdecl);
c2952b01 4223 return mdecl;
22eed1e6
APB
4224}
4225
e04a16fb 4226static void
c2952b01
APB
4227add_instance_initializer (mdecl)
4228 tree mdecl;
e04a16fb 4229{
c2952b01
APB
4230 tree current;
4231 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4232 tree compound = NULL_TREE;
e04a16fb 4233
c2952b01 4234 if (stmt_list)
e04a16fb 4235 {
c2952b01
APB
4236 for (current = stmt_list; current; current = TREE_CHAIN (current))
4237 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4238
c2952b01
APB
4239 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4240 NULL_TREE, compound));
4241 }
e04a16fb
AG
4242}
4243
4244/* Shared accros method_declarator and method_header to remember the
4245 patch stage that was reached during the declaration of the method.
4246 A method DECL is built differently is there is no patch
4247 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4248 pending on the currently defined method. */
4249
4250static int patch_stage;
4251
4252/* Check the method declaration and add the method to its current
4253 class. If the argument list is known to contain incomplete types,
4254 the method is partially added and the registration will be resume
22eed1e6
APB
4255 once the method arguments resolved. If TYPE is NULL, we're dealing
4256 with a constructor. */
e04a16fb
AG
4257
4258static tree
4259method_header (flags, type, mdecl, throws)
4260 int flags;
4261 tree type, mdecl, throws;
4262{
1886c9d8 4263 tree type_wfl = NULL_TREE;
79d13333 4264 tree meth_name = NULL_TREE;
c2952b01 4265 tree current, orig_arg, this_class = NULL;
34d4df06 4266 tree id, meth;
e04a16fb 4267 int saved_lineno;
1886c9d8 4268 int constructor_ok = 0, must_chain;
c2952b01 4269 int count;
34d4df06
APB
4270
4271 if (mdecl == error_mark_node)
4272 return error_mark_node;
4273 meth = TREE_VALUE (mdecl);
4274 id = TREE_PURPOSE (mdecl);
e04a16fb
AG
4275
4276 check_modifiers_consistency (flags);
79d13333 4277
c2952b01
APB
4278 if (GET_CPC ())
4279 this_class = TREE_TYPE (GET_CPC ());
4280
4281 if (!this_class || this_class == error_mark_node)
79d13333 4282 return NULL_TREE;
e04a16fb
AG
4283
4284 /* There are some forbidden modifiers for an abstract method and its
4285 class must be abstract as well. */
22eed1e6 4286 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4287 {
4288 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4289 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4290 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4291 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4292 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4293 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4294 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4295 parse_error_context
781b0558 4296 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e7c7bcef 4297 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
e04a16fb
AG
4298 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4299 }
c2952b01 4300
22eed1e6
APB
4301 /* Things to be checked when declaring a constructor */
4302 if (!type)
4303 {
4304 int ec = java_error_count;
4305 /* 8.6: Constructor declarations: we might be trying to define a
4306 method without specifying a return type. */
c2952b01 4307 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4308 parse_error_context
4309 (id, "Invalid method declaration, return type required");
4310 /* 8.6.3: Constructor modifiers */
4311 else
4312 {
4313 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4314 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4315 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4316 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4317 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4318 }
4319 /* If we found error here, we don't consider it's OK to tread
4320 the method definition as a constructor, for the rest of this
4321 function */
4322 if (ec == java_error_count)
4323 constructor_ok = 1;
4324 }
e04a16fb
AG
4325
4326 /* Method declared within the scope of an interface are implicitly
4327 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4328 modifiers are checked right after. */
e04a16fb
AG
4329
4330 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4331 {
4332 /* If FLAGS isn't set because of a modifier, turn the
4333 corresponding modifier WFL to NULL so we issue a warning on
4334 the obsolete use of the modifier */
4335 if (!(flags & ACC_PUBLIC))
4336 MODIFIER_WFL (PUBLIC_TK) = NULL;
4337 if (!(flags & ACC_ABSTRACT))
4338 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4339 flags |= ACC_PUBLIC;
4340 flags |= ACC_ABSTRACT;
4341 }
4342
c2952b01
APB
4343 /* Inner class can't declare static methods */
4344 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4345 {
4346 parse_error_context
4347 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4348 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4349 lang_printable_name (this_class, 0));
4350 }
4351
e04a16fb
AG
4352 /* Modifiers context reset moved up, so abstract method declaration
4353 modifiers can be later checked. */
4354
22eed1e6
APB
4355 /* Set constructor returned type to void and method name to <init>,
4356 unless we found an error identifier the constructor (in which
4357 case we retain the original name) */
4358 if (!type)
4359 {
4360 type = void_type_node;
4361 if (constructor_ok)
4362 meth_name = init_identifier_node;
4363 }
4364 else
4365 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4366
1886c9d8
APB
4367 /* Do the returned type resolution and registration if necessary */
4368 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4369
4a5f66c3
APB
4370 if (meth_name)
4371 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4372 EXPR_WFL_NODE (id) = meth_name;
4373 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4374
4375 if (must_chain)
e04a16fb 4376 {
1886c9d8
APB
4377 patch_stage = JDEP_METHOD_RETURN;
4378 register_incomplete_type (patch_stage, type_wfl, id, type);
4379 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4380 }
4381 else
1886c9d8 4382 TREE_TYPE (meth) = type;
e04a16fb
AG
4383
4384 saved_lineno = lineno;
4385 /* When defining an abstract or interface method, the curly
4386 bracket at level 1 doesn't exist because there is no function
4387 body */
4388 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4389 EXPR_WFL_LINENO (id));
4390
5e942c50
APB
4391 /* Remember the original argument list */
4392 orig_arg = TYPE_ARG_TYPES (meth);
4393
e04a16fb
AG
4394 if (patch_stage) /* includes ret type and/or all args */
4395 {
4396 jdep *jdep;
4397 meth = add_method_1 (this_class, flags, meth_name, meth);
4398 /* Patch for the return type */
4399 if (patch_stage == JDEP_METHOD_RETURN)
4400 {
4401 jdep = CLASSD_LAST (ctxp->classd_list);
4402 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4403 }
4404 /* This is the stop JDEP. METH allows the function's signature
4405 to be computed. */
4406 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4407 }
4408 else
5e942c50
APB
4409 meth = add_method (this_class, flags, meth_name,
4410 build_java_signature (meth));
4411
c2952b01
APB
4412 /* Remember final parameters */
4413 MARK_FINAL_PARMS (meth, orig_arg);
4414
5e942c50
APB
4415 /* Fix the method argument list so we have the argument name
4416 information */
4417 fix_method_argument_names (orig_arg, meth);
4418
4419 /* Register the parameter number and re-install the current line
4420 number */
e04a16fb
AG
4421 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4422 lineno = saved_lineno;
b9f7e36c
APB
4423
4424 /* Register exception specified by the `throws' keyword for
4425 resolution and set the method decl appropriate field to the list.
4426 Note: the grammar ensures that what we get here are class
4427 types. */
4428 if (throws)
4429 {
4430 throws = nreverse (throws);
4431 for (current = throws; current; current = TREE_CHAIN (current))
4432 {
4433 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4434 NULL_TREE, NULL_TREE);
4435 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4436 &TREE_VALUE (current);
4437 }
4438 DECL_FUNCTION_THROWS (meth) = throws;
4439 }
4440
e04a16fb
AG
4441 /* We set the DECL_NAME to ID so we can track the location where
4442 the function was declared. This allow us to report
4443 redefinition error accurately. When method are verified,
4444 DECL_NAME is reinstalled properly (using the content of the
4445 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4446 when Object is being defined. Constructor <init> names will be
4447 reinstalled the same way. */
c2952b01 4448 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4449 DECL_NAME (meth) = id;
22eed1e6
APB
4450
4451 /* Set the flag if we correctly processed a constructor */
4452 if (constructor_ok)
c2952b01
APB
4453 {
4454 DECL_CONSTRUCTOR_P (meth) = 1;
4455 /* Compute and store the number of artificial parameters declared
4456 for this constructor */
4457 for (count = 0, current = TYPE_FIELDS (this_class); current;
4458 current = TREE_CHAIN (current))
4459 if (FIELD_LOCAL_ALIAS (current))
4460 count++;
4461 DECL_FUNCTION_NAP (meth) = count;
4462 }
22eed1e6 4463
5e942c50
APB
4464 /* Eventually set the @deprecated tag flag */
4465 CHECK_DEPRECATED (meth);
4466
7f10c2e2
APB
4467 /* If doing xref, store column and line number information instead
4468 of the line number only. */
4469 if (flag_emit_xref)
4470 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4471
e04a16fb
AG
4472 return meth;
4473}
4474
5e942c50
APB
4475static void
4476fix_method_argument_names (orig_arg, meth)
4477 tree orig_arg, meth;
4478{
4479 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4480 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4481 {
4482 TREE_PURPOSE (arg) = this_identifier_node;
4483 arg = TREE_CHAIN (arg);
4484 }
de4c7b02 4485 while (orig_arg != end_params_node)
5e942c50
APB
4486 {
4487 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4488 orig_arg = TREE_CHAIN (orig_arg);
4489 arg = TREE_CHAIN (arg);
4490 }
4491}
4492
22eed1e6
APB
4493/* Complete the method declaration with METHOD_BODY. */
4494
4495static void
b635eb2f 4496finish_method_declaration (method_body)
22eed1e6
APB
4497 tree method_body;
4498{
79d13333
APB
4499 int flags;
4500
4501 if (!current_function_decl)
4502 return;
4503
4504 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4505
4506 /* 8.4.5 Method Body */
4507 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4508 {
4509 tree wfl = DECL_NAME (current_function_decl);
4510 parse_error_context (wfl,
4511 "%s method `%s' can't have a body defined",
4512 (METHOD_NATIVE (current_function_decl) ?
4513 "Native" : "Abstract"),
4514 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4515 method_body = NULL_TREE;
4516 }
4517 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4518 {
4519 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4520 parse_error_context
4521 (wfl,
4522 "Non native and non abstract method `%s' must have a body defined",
4523 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4524 method_body = NULL_TREE;
4525 }
4526
2c56429a
APB
4527 if (flag_emit_class_files && method_body
4528 && TREE_CODE (method_body) == NOP_EXPR
4529 && TREE_TYPE (current_function_decl)
4530 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4531 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4532
22eed1e6
APB
4533 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4534 maybe_absorb_scoping_blocks ();
4535 /* Exit function's body */
4536 exit_block ();
4537 /* Merge last line of the function with first line, directly in the
4538 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4539 if (!flag_emit_xref)
4540 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4541
4542 /* Since function's argument's list are shared, reset the
4543 ARG_FINAL_P parameter that might have been set on some of this
4544 function parameters. */
4545 UNMARK_FINAL_PARMS (current_function_decl);
4546
f099f336
APB
4547 /* So we don't have an irrelevant function declaration context for
4548 the next static block we'll see. */
4549 current_function_decl = NULL_TREE;
22eed1e6
APB
4550}
4551
4552/* Build a an error message for constructor circularity errors. */
4553
4554static char *
4555constructor_circularity_msg (from, to)
4556 tree from, to;
4557{
4558 static char string [4096];
c2e3db92 4559 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4560 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4561 free (t);
4562 return string;
4563}
4564
4565/* Verify a circular call to METH. Return 1 if an error is found, 0
4566 otherwise. */
4567
4568static int
4569verify_constructor_circularity (meth, current)
4570 tree meth, current;
4571{
4572 static tree list = NULL_TREE;
4573 tree c;
4574 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4575 {
4576 if (TREE_VALUE (c) == meth)
4577 {
4578 char *t;
4579 if (list)
4580 {
4581 tree liste;
4582 list = nreverse (list);
4583 for (liste = list; liste; liste = TREE_CHAIN (liste))
4584 {
4585 parse_error_context
c63b98cd 4586 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4587 constructor_circularity_msg
4588 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4589 java_error_count--;
4590 }
4591 }
c2e3db92 4592 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4593 parse_error_context (TREE_PURPOSE (c),
4594 "%s: recursive invocation of constructor `%s'",
4595 constructor_circularity_msg (current, meth), t);
4596 free (t);
4597 list = NULL_TREE;
4598 return 1;
4599 }
4600 }
4601 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4602 {
4603 list = tree_cons (c, current, list);
4604 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4605 return 1;
4606 list = TREE_CHAIN (list);
4607 }
4608 return 0;
4609}
4610
e04a16fb
AG
4611/* Check modifiers that can be declared but exclusively */
4612
4613static void
4614check_modifiers_consistency (flags)
4615 int flags;
4616{
4617 int acc_count = 0;
4618 tree cl = NULL_TREE;
4619
e0fc4118
TT
4620 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4621 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4622 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4623 if (acc_count > 1)
4624 parse_error_context
e0fc4118
TT
4625 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4626
4627 acc_count = 0;
4628 cl = NULL_TREE;
14d075d8
TT
4629 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4630 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
e0fc4118
TT
4631 if (acc_count > 1)
4632 parse_error_context (cl,
4633 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4634}
4635
4636/* Check the methode header METH for abstract specifics features */
4637
4638static void
4639check_abstract_method_header (meth)
4640 tree meth;
4641{
4642 int flags = get_access_flags_from_decl (meth);
4643 /* DECL_NAME might still be a WFL node */
c877974e 4644 tree name = GET_METHOD_NAME (meth);
e04a16fb 4645
2884c41e
KG
4646 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4647 ACC_ABSTRACT, "abstract method",
4648 IDENTIFIER_POINTER (name));
4649 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4650 ACC_PUBLIC, "abstract method",
4651 IDENTIFIER_POINTER (name));
e04a16fb
AG
4652
4653 check_modifiers ("Illegal modifier `%s' for interface method",
4654 flags, INTERFACE_METHOD_MODIFIERS);
4655}
4656
4657/* Create a FUNCTION_TYPE node and start augmenting it with the
4658 declared function arguments. Arguments type that can't be resolved
4659 are left as they are, but the returned node is marked as containing
4660 incomplete types. */
4661
4662static tree
4663method_declarator (id, list)
4664 tree id, list;
4665{
4666 tree arg_types = NULL_TREE, current, node;
4667 tree meth = make_node (FUNCTION_TYPE);
4668 jdep *jdep;
e04a16fb
AG
4669
4670 patch_stage = JDEP_NO_PATCH;
c2952b01 4671
34d4df06
APB
4672 if (GET_CPC () == error_mark_node)
4673 return error_mark_node;
4674
c2952b01
APB
4675 /* If we're dealing with an inner class constructor, we hide the
4676 this$<n> decl in the name field of its parameter declaration. We
4677 also might have to hide the outer context local alias
4678 initializers. Not done when the class is a toplevel class. */
4679 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4680 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4681 {
4682 tree aliases_list, type, thisn;
4683 /* First the aliases, linked to the regular parameters */
4684 aliases_list =
4685 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4686 TREE_TYPE (GET_CPC ()),
4687 NULL_TREE, NULL);
4688 list = chainon (nreverse (aliases_list), list);
4689
4690 /* Then this$<n> */
4691 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
9a7ab4b3 4692 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
c2952b01
APB
4693 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4694 list);
4695 }
e04a16fb
AG
4696
4697 for (current = list; current; current = TREE_CHAIN (current))
4698 {
c583dd46 4699 int must_chain = 0;
e04a16fb
AG
4700 tree wfl_name = TREE_PURPOSE (current);
4701 tree type = TREE_VALUE (current);
4702 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4703 tree already, arg_node;
4704 tree type_wfl = NULL_TREE;
23a79c61 4705 tree real_type;
c583dd46
APB
4706
4707 /* Obtain a suitable type for resolution, if necessary */
4708 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4709
4710 /* Process NAME, as it may specify extra dimension(s) for it */
4711 type = build_array_from_name (type, type_wfl, name, &name);
4712 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4713
23a79c61
APB
4714 real_type = GET_REAL_TYPE (type);
4715 if (TREE_CODE (real_type) == RECORD_TYPE)
4716 {
4717 real_type = promote_type (real_type);
4718 if (TREE_CODE (type) == TREE_LIST)
4719 TREE_PURPOSE (type) = real_type;
4720 }
5e942c50 4721
e04a16fb
AG
4722 /* Check redefinition */
4723 for (already = arg_types; already; already = TREE_CHAIN (already))
4724 if (TREE_PURPOSE (already) == name)
4725 {
781b0558
KG
4726 parse_error_context
4727 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4728 IDENTIFIER_POINTER (name),
e04a16fb
AG
4729 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4730 break;
4731 }
4732
4733 /* If we've an incomplete argument type, we know there is a location
4734 to patch when the type get resolved, later. */
4735 jdep = NULL;
c583dd46 4736 if (must_chain)
e04a16fb 4737 {
c583dd46
APB
4738 patch_stage = JDEP_METHOD;
4739 type = register_incomplete_type (patch_stage,
4740 type_wfl, wfl_name, type);
4741 jdep = CLASSD_LAST (ctxp->classd_list);
4742 JDEP_MISC (jdep) = id;
e04a16fb 4743 }
c583dd46 4744
c2952b01 4745 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4746 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4747 /* Remeber arguments declared final. */
4748 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4749
e04a16fb
AG
4750 if (jdep)
4751 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4752 TREE_CHAIN (arg_node) = arg_types;
4753 arg_types = arg_node;
4754 }
de4c7b02 4755 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4756 node = build_tree_list (id, meth);
4757 return node;
4758}
4759
4760static int
4761unresolved_type_p (wfl, returned)
4762 tree wfl;
4763 tree *returned;
4764
4765{
4766 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4767 {
e04a16fb 4768 if (returned)
165f37bc
APB
4769 {
4770 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4771 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4772 *returned = TREE_TYPE (decl);
4773 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4774 *returned = TREE_TYPE (GET_CPC ());
4775 else
4776 *returned = NULL_TREE;
4777 }
e04a16fb
AG
4778 return 1;
4779 }
4780 if (returned)
4781 *returned = wfl;
4782 return 0;
4783}
4784
4785/* From NAME, build a qualified identifier node using the
4786 qualification from the current package definition. */
4787
4788static tree
98a52c2c 4789parser_qualified_classname (name)
e04a16fb
AG
4790 tree name;
4791{
c2952b01
APB
4792 tree nested_class_name;
4793
98a52c2c 4794 if ((nested_class_name = maybe_make_nested_class_name (name)))
c2952b01
APB
4795 return nested_class_name;
4796
e04a16fb 4797 if (ctxp->package)
c2952b01 4798 return merge_qualified_name (ctxp->package, name);
e04a16fb 4799 else
c2952b01 4800 return name;
e04a16fb
AG
4801}
4802
4803/* Called once the type a interface extends is resolved. Returns 0 if
4804 everything is OK. */
4805
4806static int
4807parser_check_super_interface (super_decl, this_decl, this_wfl)
4808 tree super_decl, this_decl, this_wfl;
4809{
4810 tree super_type = TREE_TYPE (super_decl);
4811
4812 /* Has to be an interface */
c2952b01 4813 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4814 {
4815 parse_error_context
4816 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4817 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4818 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4819 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4820 "interface" : "class"),
4821 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4822 return 1;
4823 }
4824
4825 /* Check scope: same package OK, other package: OK if public */
4826 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4827 return 1;
4828
4829 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4830 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4831 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4832 return 0;
4833}
4834
4835/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4836 0 if everthing is OK. */
4837
4838static int
4839parser_check_super (super_decl, this_decl, wfl)
4840 tree super_decl, this_decl, wfl;
4841{
e04a16fb
AG
4842 tree super_type = TREE_TYPE (super_decl);
4843
4844 /* SUPER should be a CLASS (neither an array nor an interface) */
4845 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4846 {
4847 parse_error_context
4848 (wfl, "Class `%s' can't subclass %s `%s'",
4849 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4850 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4851 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4852 return 1;
4853 }
4854
4855 if (CLASS_FINAL (TYPE_NAME (super_type)))
4856 {
4857 parse_error_context (wfl, "Can't subclass final classes: %s",
4858 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4859 return 1;
4860 }
4861
4862 /* Check scope: same package OK, other package: OK if public */
4863 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4864 return 1;
4865
4866 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4867 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4868 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4869 return 0;
4870}
4871
4872/* Create a new dependency list and link it (in a LIFO manner) to the
4873 CTXP list of type dependency list. */
4874
4875static void
4876create_jdep_list (ctxp)
4877 struct parser_ctxt *ctxp;
4878{
23a79c61 4879 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4880 new->first = new->last = NULL;
4881 new->next = ctxp->classd_list;
4882 ctxp->classd_list = new;
4883}
4884
4885static jdeplist *
4886reverse_jdep_list (ctxp)
4887 struct parser_ctxt *ctxp;
4888{
4889 register jdeplist *prev = NULL, *current, *next;
4890 for (current = ctxp->classd_list; current; current = next)
4891 {
4892 next = current->next;
4893 current->next = prev;
4894 prev = current;
4895 }
4896 return prev;
4897}
4898
23a79c61
APB
4899/* Create a fake pointer based on the ID stored in
4900 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4901 registered again. */
e04a16fb
AG
4902
4903static tree
23a79c61
APB
4904obtain_incomplete_type (type_name)
4905 tree type_name;
e04a16fb 4906{
23a79c61
APB
4907 tree ptr, name;
4908
4909 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4910 name = EXPR_WFL_NODE (type_name);
4911 else if (INCOMPLETE_TYPE_P (type_name))
4912 name = TYPE_NAME (type_name);
4913 else
4914 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4915
4916 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4917 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4918 break;
4919
4920 if (!ptr)
4921 {
e04a16fb 4922 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4923 BUILD_PTR_FROM_NAME (ptr, name);
4924 layout_type (ptr);
e04a16fb
AG
4925 pop_obstacks ();
4926 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4927 ctxp->incomplete_class = ptr;
4928 }
4929
4930 return ptr;
4931}
4932
4933/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4934 non NULL instead of computing a new fake type based on WFL. The new
4935 dependency is inserted in the current type dependency list, in FIFO
4936 manner. */
4937
4938static tree
4939register_incomplete_type (kind, wfl, decl, ptr)
4940 int kind;
4941 tree wfl, decl, ptr;
4942{
23a79c61 4943 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4944
e04a16fb
AG
4945 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4946 ptr = obtain_incomplete_type (wfl);
4947
4948 JDEP_KIND (new) = kind;
4949 JDEP_DECL (new) = decl;
4950 JDEP_SOLV (new) = ptr;
4951 JDEP_WFL (new) = wfl;
4952 JDEP_CHAIN (new) = NULL;
4953 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4954 /* For some dependencies, set the enclosing class of the current
4955 class to be the enclosing context */
4956 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4957 && GET_ENCLOSING_CPC ())
4958 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4959 else
324ed8fd 4960 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4961 JDEP_GET_PATCH (new) = (tree *)NULL;
4962
4963 JDEP_INSERT (ctxp->classd_list, new);
4964
4965 return ptr;
4966}
4967
4968void
4969java_check_circular_reference ()
4970{
4971 tree current;
4972 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4973 {
4974 tree type = TREE_TYPE (current);
e920ebc9 4975 if (CLASS_INTERFACE (current))
e04a16fb
AG
4976 {
4977 /* Check all interfaces this class extends */
4978 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4979 int n, i;
4980
4981 if (!basetype_vec)
4982 return;
4983 n = TREE_VEC_LENGTH (basetype_vec);
4984 for (i = 0; i < n; i++)
4985 {
4986 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4987 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4988 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4989 parse_error_context (lookup_cl (current),
4990 "Cyclic interface inheritance");
4991 }
4992 }
4993 else
4994 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4995 parse_error_context (lookup_cl (current),
c2952b01
APB
4996 "Cyclic class inheritance%s",
4997 (cyclic_inheritance_report ?
4998 cyclic_inheritance_report : ""));
4999 }
5000}
5001
5002/* Augment the parameter list PARM with parameters crafted to
5003 initialize outer context locals aliases. Through ARTIFICIAL, a
5004 count is kept of the number of crafted parameters. MODE governs
5005 what eventually gets created: something suitable for a function
5006 creation or a function invocation, either the constructor or
c00f0fb2 5007 finit$. */
c2952b01
APB
5008
5009static tree
5010build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5011 int mode;
5012 tree class_type, parm;
5013 int *artificial;
5014{
5015 tree field;
da632f2c
APB
5016 tree additional_parms = NULL_TREE;
5017
c2952b01
APB
5018 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5019 if (FIELD_LOCAL_ALIAS (field))
5020 {
63ad61ed 5021 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
c2952b01
APB
5022 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5023
5024 switch (mode)
5025 {
5026 case AIPL_FUNCTION_DECLARATION:
5027 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5028 purpose = build_wfl_node (get_identifier (buffer));
5029 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5030 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5031 else
5032 value = TREE_TYPE (field);
5033 break;
5034
5035 case AIPL_FUNCTION_CREATION:
5036 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5037 purpose = get_identifier (buffer);
5038 value = TREE_TYPE (field);
5039 break;
5040
5041 case AIPL_FUNCTION_FINIT_INVOCATION:
5042 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5043 /* Now, this is wrong. purpose should always be the NAME
5044 of something and value its matching value (decl, type,
5045 etc...) FIXME -- but there is a lot to fix. */
5046
5047 /* When invoked for this kind of operation, we already
5048 know whether a field is used or not. */
5049 purpose = TREE_TYPE (field);
5050 value = build_wfl_node (get_identifier (buffer));
5051 break;
5052
5053 case AIPL_FUNCTION_CTOR_INVOCATION:
5054 /* There are two case: the constructor invokation happends
5055 outside the local inner, in which case, locales from the outer
5056 context are directly used.
5057
5058 Otherwise, we fold to using the alias directly. */
5059 if (class_type == current_class)
5060 value = field;
5061 else
5062 {
5063 name = get_identifier (&buffer[4]);
5064 value = IDENTIFIER_LOCAL_VALUE (name);
5065 }
5066 break;
5067 }
da632f2c 5068 additional_parms = tree_cons (purpose, value, additional_parms);
c2952b01
APB
5069 if (artificial)
5070 *artificial +=1;
5071 }
da632f2c
APB
5072 if (additional_parms)
5073 {
5074 if (ANONYMOUS_CLASS_P (class_type)
5075 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5076 additional_parms = nreverse (additional_parms);
5077 parm = chainon (additional_parms, parm);
5078 }
5079
5080 return parm;
c2952b01
APB
5081}
5082
5083/* Craft a constructor for CLASS_DECL -- what we should do when none
5084 where found. ARGS is non NULL when a special signature must be
5085 enforced. This is the case for anonymous classes. */
5086
5087static void
5088craft_constructor (class_decl, args)
5089 tree class_decl, args;
5090{
5091 tree class_type = TREE_TYPE (class_decl);
5092 tree parm = NULL_TREE;
5093 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5094 ACC_PUBLIC : 0);
5095 int i = 0, artificial = 0;
5096 tree decl, ctor_name;
5097 char buffer [80];
5098
5099 push_obstacks (&permanent_obstack, &permanent_obstack);
5100
5101 /* The constructor name is <init> unless we're dealing with an
5102 anonymous class, in which case the name will be fixed after having
5103 be expanded. */
5104 if (ANONYMOUS_CLASS_P (class_type))
5105 ctor_name = DECL_NAME (class_decl);
5106 else
5107 ctor_name = init_identifier_node;
5108
5109 /* If we're dealing with an inner class constructor, we hide the
5110 this$<n> decl in the name field of its parameter declaration. */
5111 if (PURE_INNER_CLASS_TYPE_P (class_type))
5112 {
5113 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5114 parm = tree_cons (build_current_thisn (class_type),
5115 build_pointer_type (type), parm);
5116
5117 /* Some more arguments to be hidden here. The values of the local
5118 variables of the outer context that the inner class needs to see. */
5119 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5120 class_type, parm,
5121 &artificial);
5122 }
5123
5124 /* Then if there are any args to be enforced, enforce them now */
5125 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5126 {
5127 sprintf (buffer, "parm%d", i++);
5128 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5129 }
c2952b01
APB
5130
5131 CRAFTED_PARAM_LIST_FIXUP (parm);
5132 decl = create_artificial_method (class_type, flags, void_type_node,
5133 ctor_name, parm);
5134 fix_method_argument_names (parm, decl);
5135 /* Now, mark the artificial parameters. */
5136 DECL_FUNCTION_NAP (decl) = artificial;
5137
5138 pop_obstacks ();
5139 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5140}
5141
c2952b01 5142
e920ebc9
APB
5143/* Fix the constructors. This will be called right after circular
5144 references have been checked. It is necessary to fix constructors
5145 early even if no code generation will take place for that class:
5146 some generated constructor might be required by the class whose
5147 compilation triggered this one to be simply loaded. */
5148
5149void
5150java_fix_constructors ()
5151{
5152 tree current;
5153
5154 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5155 {
e920ebc9
APB
5156 tree class_type = TREE_TYPE (current);
5157 int saw_ctor = 0;
c2952b01
APB
5158 tree decl;
5159
5160 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5161 continue;
e920ebc9
APB
5162
5163 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5164 {
5165 if (DECL_CONSTRUCTOR_P (decl))
5166 {
5167 fix_constructors (decl);
5168 saw_ctor = 1;
5169 }
5170 }
5171
c2952b01
APB
5172 /* Anonymous class constructor can't be generated that early. */
5173 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5174 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5175 }
5176}
5177
23a79c61
APB
5178/* safe_layout_class just makes sure that we can load a class without
5179 disrupting the current_class, input_file, lineno, etc, information
5180 about the class processed currently. */
5181
e04a16fb
AG
5182void
5183safe_layout_class (class)
5184 tree class;
5185{
5186 tree save_current_class = current_class;
3b304f5b 5187 const char *save_input_filename = input_filename;
e04a16fb 5188 int save_lineno = lineno;
5e942c50 5189
e04a16fb 5190 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5191
e04a16fb
AG
5192 layout_class (class);
5193 pop_obstacks ();
5e942c50 5194
e04a16fb
AG
5195 current_class = save_current_class;
5196 input_filename = save_input_filename;
5197 lineno = save_lineno;
5198 CLASS_LOADED_P (class) = 1;
5199}
5200
5201static tree
5202jdep_resolve_class (dep)
5203 jdep *dep;
5204{
5205 tree decl;
5206
23a79c61
APB
5207 if (JDEP_RESOLVED_P (dep))
5208 decl = JDEP_RESOLVED_DECL (dep);
5209 else
e04a16fb 5210 {
c2952b01 5211 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5212 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5213 JDEP_RESOLVED (dep, decl);
5214 }
23a79c61 5215
e04a16fb 5216 if (!decl)
23a79c61 5217 complete_class_report_errors (dep);
1e12ab9b 5218 else if (PURE_INNER_CLASS_DECL_P (decl))
4dbf4496 5219 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
e04a16fb
AG
5220 return decl;
5221}
5222
5223/* Complete unsatisfied class declaration and their dependencies */
5224
5225void
5226java_complete_class ()
5227{
e04a16fb
AG
5228 tree cclass;
5229 jdeplist *cclassd;
5230 int error_found;
b67d701b 5231 tree type;
e04a16fb
AG
5232
5233 push_obstacks (&permanent_obstack, &permanent_obstack);
5234
9a7ab4b3 5235 /* Process imports */
e04a16fb 5236 process_imports ();
e04a16fb
AG
5237
5238 /* Rever things so we have the right order */
5239 ctxp->class_list = nreverse (ctxp->class_list);
5240 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5241
e04a16fb
AG
5242 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5243 cclass && cclassd;
5244 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5245 {
5246 jdep *dep;
5247 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5248 {
5249 tree decl;
e04a16fb
AG
5250 if (!(decl = jdep_resolve_class (dep)))
5251 continue;
5252
5253 /* Now it's time to patch */
5254 switch (JDEP_KIND (dep))
5255 {
5256 case JDEP_SUPER:
5257 /* Simply patch super */
5258 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5259 continue;
5260 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5261 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5262 break;
5263
5264 case JDEP_FIELD:
5265 {
5266 /* We do part of the job done in add_field */
5267 tree field_decl = JDEP_DECL (dep);
5268 tree field_type = TREE_TYPE (decl);
5269 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5270 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5271 field_type = promote_type (field_type);
5272 pop_obstacks ();
5273 TREE_TYPE (field_decl) = field_type;
5e942c50 5274 DECL_ALIGN (field_decl) = 0;
11cf4d18 5275 DECL_USER_ALIGN (field_decl) = 0;
5e942c50 5276 layout_decl (field_decl, 0);
e04a16fb
AG
5277 SOURCE_FRONTEND_DEBUG
5278 (("Completed field/var decl `%s' with `%s'",
5279 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5280 IDENTIFIER_POINTER (DECL_NAME (decl))));
5281 break;
5282 }
5283 case JDEP_METHOD: /* We start patching a method */
5284 case JDEP_METHOD_RETURN:
5285 error_found = 0;
5286 while (1)
5287 {
5288 if (decl)
5289 {
b67d701b
PB
5290 type = TREE_TYPE(decl);
5291 if (TREE_CODE (type) == RECORD_TYPE)
5292 type = promote_type (type);
e04a16fb
AG
5293 JDEP_APPLY_PATCH (dep, type);
5294 SOURCE_FRONTEND_DEBUG
5295 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5296 "Completing fct `%s' with ret type `%s'":
5297 "Completing arg `%s' with type `%s'"),
5298 IDENTIFIER_POINTER (EXPR_WFL_NODE
5299 (JDEP_DECL_WFL (dep))),
5300 IDENTIFIER_POINTER (DECL_NAME (decl))));
5301 }
5302 else
5303 error_found = 1;
5304 dep = JDEP_CHAIN (dep);
5305 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5306 break;
5307 else
5308 decl = jdep_resolve_class (dep);
5309 }
5310 if (!error_found)
5311 {
5312 tree mdecl = JDEP_DECL (dep), signature;
5313 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5314 /* Recompute and reset the signature, check first that
5315 all types are now defined. If they're not,
5316 dont build the signature. */
5317 if (check_method_types_complete (mdecl))
5318 {
5319 signature = build_java_signature (TREE_TYPE (mdecl));
5320 set_java_signature (TREE_TYPE (mdecl), signature);
5321 }
e04a16fb
AG
5322 pop_obstacks ();
5323 }
5324 else
5325 continue;
5326 break;
5327
5328 case JDEP_INTERFACE:
5329 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5330 JDEP_WFL (dep)))
5331 continue;
5332 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5333 break;
5334
b67d701b 5335 case JDEP_PARM:
e04a16fb 5336 case JDEP_VARIABLE:
b67d701b
PB
5337 type = TREE_TYPE(decl);
5338 if (TREE_CODE (type) == RECORD_TYPE)
5339 type = promote_type (type);
5340 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5341 break;
5342
5343 case JDEP_TYPE:
5344 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5345 SOURCE_FRONTEND_DEBUG
5346 (("Completing a random type dependency on a '%s' node",
5347 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5348 break;
5349
b9f7e36c 5350 case JDEP_EXCEPTION:
c877974e
APB
5351 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5352 SOURCE_FRONTEND_DEBUG
5353 (("Completing `%s' `throws' argument node",
5354 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5355 break;
5356
c2952b01
APB
5357 case JDEP_ANONYMOUS:
5358 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5359 break;
5360
e04a16fb 5361 default:
0a2138e2
APB
5362 fatal ("Can't handle patch code %d - java_complete_class",
5363 JDEP_KIND (dep));
e04a16fb
AG
5364 }
5365 }
5366 }
5367 pop_obstacks ();
5368 return;
5369}
5370
5371/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5372 array. */
5373
5374static tree
c2952b01
APB
5375resolve_class (enclosing, class_type, decl, cl)
5376 tree enclosing, class_type, decl, cl;
e04a16fb 5377{
49f48c71
KG
5378 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5379 const char *base = name;
78d21f92
PB
5380 tree resolved_type = TREE_TYPE (class_type);
5381 tree resolved_type_decl;
e04a16fb 5382
78d21f92
PB
5383 if (resolved_type != NULL_TREE)
5384 {
5385 tree resolved_type_decl = TYPE_NAME (resolved_type);
5386 if (resolved_type_decl == NULL_TREE
5387 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5388 {
5389 resolved_type_decl = build_decl (TYPE_DECL,
5390 TYPE_NAME (class_type),
5391 resolved_type);
5392 }
5393 return resolved_type_decl;
5394 }
5395
e04a16fb
AG
5396 /* 1- Check to see if we have an array. If true, find what we really
5397 want to resolve */
5398 while (name[0] == '[')
5399 name++;
5400 if (base != name)
34d4df06
APB
5401 {
5402 TYPE_NAME (class_type) = get_identifier (name);
5403 WFL_STRIP_BRACKET (cl, cl);
5404 }
e04a16fb
AG
5405
5406 /* 2- Resolve the bare type */
c2952b01
APB
5407 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5408 decl, cl)))
e04a16fb
AG
5409 return NULL_TREE;
5410 resolved_type = TREE_TYPE (resolved_type_decl);
5411
5412 /* 3- If we have and array, reconstruct the array down to its nesting */
5413 if (base != name)
5414 {
5415 while (base != name)
5416 {
5417 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5418 resolved_type = promote_type (resolved_type);
5419 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5420 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5421 name--;
5422 }
5423 /* Build a fake decl for this, since this is what is expected to
5424 be returned. */
5425 resolved_type_decl =
5426 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5427 /* Figure how those two things are important for error report. FIXME */
5428 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5429 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5430 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5431 }
78d21f92 5432 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5433 return resolved_type_decl;
5434}
5435
5436/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5437 are used to report error messages. */
5438
78d21f92 5439tree
c2952b01
APB
5440do_resolve_class (enclosing, class_type, decl, cl)
5441 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5442{
5443 tree new_class_decl;
e04a16fb
AG
5444
5445 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
9a7ab4b3
APB
5446 it is changed by find_in_imports{_on_demand} and (but it doesn't
5447 really matter) qualify_and_find */
e04a16fb 5448
c2952b01
APB
5449 /* 0- Search in the current class as an inner class */
5450
5451 /* Maybe some code here should be added to load the class or
5452 something, at least if the class isn't an inner class and ended
5453 being loaded from class file. FIXME. */
a40d21da
APB
5454 while (enclosing)
5455 {
5456 tree name;
5457
5458 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5459 return new_class_decl;
5460
0c2b8145
APB
5461 /* Explore enclosing contexts. */
5462 while (INNER_CLASS_DECL_P (enclosing))
5463 {
5464 enclosing = DECL_CONTEXT (enclosing);
5465 if ((new_class_decl = find_as_inner_class (enclosing,
5466 class_type, cl)))
5467 return new_class_decl;
5468 }
5469
a40d21da
APB
5470 /* Now go to the upper classes, bail out if necessary. */
5471 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5472 if (!enclosing || enclosing == object_type_node)
5473 break;
5474
5475 if (TREE_CODE (enclosing) == RECORD_TYPE)
5476 {
5477 enclosing = TYPE_NAME (enclosing);
5478 continue;
5479 }
5480
5481 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
0c2b8145 5482 BUILD_PTR_FROM_NAME (name, enclosing);
a40d21da
APB
5483 else
5484 name = enclosing;
5485 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5486 }
c2952b01 5487
9a7ab4b3
APB
5488 /* 1- Check for the type in single imports. This will change
5489 TYPE_NAME() if something relevant is found */
5490 find_in_imports (class_type);
e04a16fb 5491
9a7ab4b3 5492 /* 2- And check for the type in the current compilation unit */
e04a16fb
AG
5493 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5494 {
5495 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5496 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5497 load_class (TYPE_NAME (class_type), 0);
5498 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5499 }
5500
9a7ab4b3
APB
5501 /* 3- Search according to the current package definition */
5502 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5503 {
5504 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5505 TYPE_NAME (class_type))))
5506 return new_class_decl;
5507 }
5508
5509 /* 4- Check the import on demands. Don't allow bar.baz to be
5510 imported from foo.* */
5511 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5512 if (find_in_imports_on_demand (class_type))
5513 return NULL_TREE;
5514
5515 /* If found in find_in_imports_on_demant, the type has already been
5516 loaded. */
5517 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5518 return new_class_decl;
5519
5520 /* 5- Try with a name qualified with the package name we've seen so far */
ee07f4f4 5521 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5522 {
ee07f4f4 5523 tree package;
d6baf6f5
APB
5524
5525 /* If there is a current package (ctxp->package), it's the first
5526 element of package_list and we can skip it. */
5527 for (package = (ctxp->package ?
5528 TREE_CHAIN (package_list) : package_list);
5529 package; package = TREE_CHAIN (package))
9a7ab4b3
APB
5530 if ((new_class_decl = qualify_and_find (class_type,
5531 TREE_PURPOSE (package),
5532 TYPE_NAME (class_type))))
5533 return new_class_decl;
5534 }
5535
5536 /* 5- Check an other compilation unit that bears the name of type */
e04a16fb
AG
5537 load_class (TYPE_NAME (class_type), 0);
5538 if (check_pkg_class_access (TYPE_NAME (class_type),
5539 (cl ? cl : lookup_cl (decl))))
5540 return NULL_TREE;
5541
9a7ab4b3 5542 /* 6- Last call for a resolution */
e04a16fb
AG
5543 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5544}
5545
9a7ab4b3
APB
5546static tree
5547qualify_and_find (class_type, package, name)
5548 tree class_type, package, name;
5549{
5550 tree new_qualified = merge_qualified_name (package, name);
5551 tree new_class_decl;
5552
5553 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5554 load_class (new_qualified, 0);
5555 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5556 {
5557 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5558 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5559 load_class (new_qualified, 0);
5560 TYPE_NAME (class_type) = new_qualified;
5561 return IDENTIFIER_CLASS_VALUE (new_qualified);
5562 }
5563 return NULL_TREE;
5564}
5565
e04a16fb 5566/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5567 parsed class). Return a decl node. This function is meant to be
5568 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5569
5570static tree
c877974e
APB
5571resolve_and_layout (something, cl)
5572 tree something;
e04a16fb
AG
5573 tree cl;
5574{
34d4df06 5575 tree decl, decl_type;
c877974e 5576
23a79c61
APB
5577 /* Don't do that on the current class */
5578 if (something == current_class)
5579 return TYPE_NAME (current_class);
c877974e 5580
23a79c61 5581 /* Don't do anything for void and other primitive types */
c877974e
APB
5582 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5583 return NULL_TREE;
5584
23a79c61
APB
5585 /* Pointer types can be reall pointer types or fake pointers. When
5586 finding a real pointer, recheck for primitive types */
5587 if (TREE_CODE (something) == POINTER_TYPE)
5588 {
5589 if (TREE_TYPE (something))
5590 {
5591 something = TREE_TYPE (something);
5592 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5593 return NULL_TREE;
5594 }
5595 else
5596 something = TYPE_NAME (something);
5597 }
5598
5599 /* Don't do anything for arrays of primitive types */
5600 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5601 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5602 return NULL_TREE;
5603
c2952b01
APB
5604 /* Something might be a WFL */
5605 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5606 something = EXPR_WFL_NODE (something);
5607
5608 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5609 TYPE_DECL or a real TYPE */
5610 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5611 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5612 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5613
23a79c61
APB
5614 if (!(decl = resolve_no_layout (something, cl)))
5615 return NULL_TREE;
5616
5617 /* Resolve and layout if necessary */
34d4df06
APB
5618 decl_type = TREE_TYPE (decl);
5619 layout_class_methods (decl_type);
5620 /* Check methods */
5621 if (CLASS_FROM_SOURCE_P (decl_type))
5622 java_check_methods (decl);
5623 /* Layout the type if necessary */
5624 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5625 safe_layout_class (decl_type);
23a79c61 5626
e04a16fb
AG
5627 return decl;
5628}
5629
5630/* Resolve a class, returns its decl but doesn't perform any
5631 layout. The current parsing context is saved and restored */
5632
5633static tree
5634resolve_no_layout (name, cl)
5635 tree name, cl;
5636{
5637 tree ptr, decl;
5638 BUILD_PTR_FROM_NAME (ptr, name);
5639 java_parser_context_save_global ();
c2952b01 5640 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5641 java_parser_context_restore_global ();
5642
5643 return decl;
5644}
5645
23a79c61
APB
5646/* Called when reporting errors. Skip leader '[' in a complex array
5647 type description that failed to be resolved. */
e04a16fb 5648
49f48c71 5649static const char *
e04a16fb 5650purify_type_name (name)
49f48c71 5651 const char *name;
e04a16fb
AG
5652{
5653 while (*name && *name == '[')
5654 name++;
5655 return name;
5656}
5657
5658/* The type CURRENT refers to can't be found. We print error messages. */
5659
5660static void
5661complete_class_report_errors (dep)
5662 jdep *dep;
5663{
49f48c71 5664 const char *name;
23a79c61
APB
5665
5666 if (!JDEP_WFL (dep))
5667 return;
5668
5669 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5670 switch (JDEP_KIND (dep))
5671 {
5672 case JDEP_SUPER:
5673 parse_error_context
5674 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5675 purify_type_name (name),
e04a16fb
AG
5676 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5677 break;
5678 case JDEP_FIELD:
5679 parse_error_context
5680 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5681 purify_type_name (name),
e04a16fb
AG
5682 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5683 break;
5684 case JDEP_METHOD: /* Covers arguments */
5685 parse_error_context
781b0558 5686 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5687 purify_type_name (name),
e04a16fb
AG
5688 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5689 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5690 break;
5691 case JDEP_METHOD_RETURN: /* Covers return type */
5692 parse_error_context
781b0558 5693 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5694 purify_type_name (name),
e04a16fb
AG
5695 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5696 break;
5697 case JDEP_INTERFACE:
5698 parse_error_context
5699 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5700 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5701 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5702 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5703 break;
5704 case JDEP_VARIABLE:
5705 parse_error_context
781b0558 5706 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5707 purify_type_name (IDENTIFIER_POINTER
5708 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5709 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5710 break;
b9f7e36c
APB
5711 case JDEP_EXCEPTION: /* As specified by `throws' */
5712 parse_error_context
5713 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5714 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5715 break;
0a2138e2
APB
5716 default:
5717 /* Fix for -Wall. Just break doing nothing. The error will be
5718 caught later */
5719 break;
e04a16fb
AG
5720 }
5721}
5722
22eed1e6
APB
5723/* Return a static string containing the DECL prototype string. If
5724 DECL is a constructor, use the class name instead of the form
5725 <init> */
5726
49f48c71 5727static const char *
22eed1e6
APB
5728get_printable_method_name (decl)
5729 tree decl;
5730{
49f48c71 5731 const char *to_return;
9ee9b555 5732 tree name = NULL_TREE;
22eed1e6
APB
5733
5734 if (DECL_CONSTRUCTOR_P (decl))
5735 {
5736 name = DECL_NAME (decl);
5e942c50 5737 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5738 }
5739
5740 to_return = lang_printable_name (decl, 0);
5741 if (DECL_CONSTRUCTOR_P (decl))
5742 DECL_NAME (decl) = name;
5743
5744 return to_return;
5745}
5746
5e942c50
APB
5747/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5748 nevertheless needs to be verfied, 1 otherwise. */
5749
5750static int
5751reset_method_name (method)
5752 tree method;
5753{
c2952b01 5754 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5755 {
5756 /* NAME is just the plain name when Object is being defined */
5757 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5758 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5759 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5760 return 0;
5761 }
5762 else
5763 return 1;
5764}
5765
c877974e
APB
5766/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5767
5768tree
5769java_get_real_method_name (method_decl)
5770 tree method_decl;
5771{
5772 tree method_name = DECL_NAME (method_decl);
5773 if (DECL_CONSTRUCTOR_P (method_decl))
5774 return init_identifier_node;
82371d41
APB
5775
5776 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5777 and still can be a constructor. FIXME */
5778
23a79c61
APB
5779 /* Don't confuse method only bearing the name of their class as
5780 constructors */
82371d41
APB
5781 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5782 && ctxp
c2952b01 5783 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5784 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5785 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5786 return init_identifier_node;
5787 else
5788 return EXPR_WFL_NODE (method_name);
5789}
5790
22eed1e6
APB
5791/* Track method being redefined inside the same class. As a side
5792 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5793 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5794
e04a16fb
AG
5795static int
5796check_method_redefinition (class, method)
5797 tree class, method;
5798{
5799 tree redef, name;
5800 tree cl = DECL_NAME (method);
c3f2a476 5801 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
c00f0fb2 5802 /* decl name of artificial <clinit> and finit$ doesn't need to be
ba179f9f 5803 fixed and checked */
5e942c50
APB
5804
5805 /* Reset the method name before running the check. If it returns 1,
5806 the method doesn't need to be verified with respect to method
5807 redeclaration and we return 0 */
5808 if (reset_method_name (method))
e04a16fb 5809 return 0;
5e942c50
APB
5810
5811 name = DECL_NAME (method);
e04a16fb
AG
5812 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5813 {
c3f2a476 5814 if (redef == method)
e04a16fb 5815 break;
c3f2a476
APB
5816 if (DECL_NAME (redef) == name
5817 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5818 {
22eed1e6
APB
5819 parse_error_context
5820 (cl, "Duplicate %s declaration `%s'",
5821 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5822 get_printable_method_name (redef));
e04a16fb
AG
5823 return 1;
5824 }
5825 }
5826 return 0;
5827}
5828
1175b9b4
TT
5829/* Return 1 if check went ok, 0 otherwise. */
5830static int
d77613be
APB
5831check_abstract_method_definitions (do_interface, class_decl, type)
5832 int do_interface;
5833 tree class_decl, type;
5834{
5835 tree class = TREE_TYPE (class_decl);
5836 tree method, end_type;
1175b9b4 5837 int ok = 1;
d77613be
APB
5838
5839 end_type = (do_interface ? object_type_node : type);
5840 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5841 {
5842 tree other_super, other_method, method_sig, method_name;
5843 int found = 0;
165f37bc 5844 int end_type_reached = 0;
d77613be
APB
5845
5846 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5847 continue;
5848
5849 /* Now verify that somewhere in between TYPE and CLASS,
5850 abstract method METHOD gets a non abstract definition
5851 that is inherited by CLASS. */
5852
5853 method_sig = build_java_signature (TREE_TYPE (method));
5854 method_name = DECL_NAME (method);
5855 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5856 method_name = EXPR_WFL_NODE (method_name);
5857
165f37bc
APB
5858 other_super = class;
5859 do {
5860 if (other_super == end_type)
5861 end_type_reached = 1;
5862
5863 /* Method search */
5864 for (other_method = TYPE_METHODS (other_super); other_method;
5865 other_method = TREE_CHAIN (other_method))
5866 {
5867 tree s = build_java_signature (TREE_TYPE (other_method));
5868 tree other_name = DECL_NAME (other_method);
5869
5870 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5871 other_name = EXPR_WFL_NODE (other_name);
5872 if (!DECL_CLINIT_P (other_method)
5873 && !DECL_CONSTRUCTOR_P (other_method)
120f0c10
TT
5874 && method_name == other_name
5875 && method_sig == s
5876 && !METHOD_ABSTRACT (other_method))
165f37bc
APB
5877 {
5878 found = 1;
5879 break;
5880 }
5881 }
5882 other_super = CLASSTYPE_SUPER (other_super);
5883 } while (!end_type_reached);
5884
d77613be
APB
5885 /* Report that abstract METHOD didn't find an implementation
5886 that CLASS can use. */
5887 if (!found)
5888 {
c2e3db92 5889 char *t = xstrdup (lang_printable_name
d77613be
APB
5890 (TREE_TYPE (TREE_TYPE (method)), 0));
5891 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5892 tree saved_wfl = NULL_TREE;
5893
5894 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5895 {
5896 saved_wfl = DECL_NAME (method);
5897 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5898 }
5899
5900 parse_error_context
5901 (lookup_cl (class_decl),
781b0558 5902 "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
5903 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5904 t, lang_printable_name (method, 0),
5905 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5906 "interface" : "class"),
5907 IDENTIFIER_POINTER (ccn),
5908 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5909 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
1175b9b4 5910 ok = 0;
d77613be 5911 free (t);
1175b9b4 5912
d77613be
APB
5913 if (saved_wfl)
5914 DECL_NAME (method) = saved_wfl;
5915 }
5916 }
1175b9b4
TT
5917
5918 if (ok && do_interface)
5919 {
5920 /* Check for implemented interfaces. */
5921 int i;
5922 tree vector = TYPE_BINFO_BASETYPES (type);
5923 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5924 {
5925 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5926 ok = check_abstract_method_definitions (1, class_decl, super);
5927 }
5928 }
5929
5930 return ok;
d77613be
APB
5931}
5932
614eaae0 5933/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5934 methods. */
5935
5936static void
5937java_check_abstract_method_definitions (class_decl)
5938 tree class_decl;
5939{
5940 tree class = TREE_TYPE (class_decl);
5941 tree super, vector;
5942 int i;
5943
5944 if (CLASS_ABSTRACT (class_decl))
5945 return;
5946
5947 /* Check for inherited types */
165f37bc
APB
5948 super = class;
5949 do {
5950 super = CLASSTYPE_SUPER (super);
5951 check_abstract_method_definitions (0, class_decl, super);
5952 } while (super != object_type_node);
d77613be
APB
5953
5954 /* Check for implemented interfaces. */
5955 vector = TYPE_BINFO_BASETYPES (class);
5956 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5957 {
5958 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5959 check_abstract_method_definitions (1, class_decl, super);
5960 }
5961}
5962
165f37bc
APB
5963/* Check all the types method DECL uses and return 1 if all of them
5964 are now complete, 0 otherwise. This is used to check whether its
5965 safe to build a method signature or not. */
5966
5967static int
5968check_method_types_complete (decl)
5969 tree decl;
5970{
5971 tree type = TREE_TYPE (decl);
5972 tree args;
5973
5974 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5975 return 0;
5976
5977 args = TYPE_ARG_TYPES (type);
5978 if (TREE_CODE (type) == METHOD_TYPE)
5979 args = TREE_CHAIN (args);
5980 for (; args != end_params_node; args = TREE_CHAIN (args))
5981 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5982 return 0;
5983
5984 return 1;
5985}
5986
34d4df06
APB
5987/* Visible interface to check methods contained in CLASS_DECL */
5988
5989void
5990java_check_methods (class_decl)
5991 tree class_decl;
5992{
5993 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5994 return;
5995
5996 if (CLASS_INTERFACE (class_decl))
5997 java_check_abstract_methods (class_decl);
5998 else
5999 java_check_regular_methods (class_decl);
6000
6001 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6002}
6003
d77613be
APB
6004/* Check all the methods of CLASS_DECL. Methods are first completed
6005 then checked according to regular method existance rules. If no
6006 constructor for CLASS_DECL were encountered, then build its
6007 declaration. */
e04a16fb
AG
6008
6009static void
6010java_check_regular_methods (class_decl)
6011 tree class_decl;
6012{
c2952b01 6013 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
6014 tree method;
6015 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 6016 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
6017 tree mthrows;
6018
6019 /* It is not necessary to check methods defined in java.lang.Object */
6020 if (class == object_type_node)
6021 return;
e04a16fb 6022
23a79c61
APB
6023 if (!TYPE_NVIRTUALS (class))
6024 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
6025
6026 /* Should take interfaces into account. FIXME */
6027 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6028 {
5e942c50 6029 tree sig;
e04a16fb
AG
6030 tree method_wfl = DECL_NAME (method);
6031 int aflags;
6032
5e942c50
APB
6033 /* If we previously found something and its name was saved,
6034 reinstall it now */
6035 if (found && saved_found_wfl)
ba179f9f
APB
6036 {
6037 DECL_NAME (found) = saved_found_wfl;
6038 saved_found_wfl = NULL_TREE;
6039 }
5e942c50 6040
e04a16fb
AG
6041 /* Check for redefinitions */
6042 if (check_method_redefinition (class, method))
6043 continue;
6044
22eed1e6
APB
6045 /* If we see one constructor a mark so we don't generate the
6046 default one. Also skip other verifications: constructors
6047 can't be inherited hence hiden or overriden */
6048 if (DECL_CONSTRUCTOR_P (method))
6049 {
6050 saw_constructor = 1;
6051 continue;
6052 }
6053
c877974e
APB
6054 /* We verify things thrown by the method. They must inherits from
6055 java.lang.Throwable */
6056 for (mthrows = DECL_FUNCTION_THROWS (method);
6057 mthrows; mthrows = TREE_CHAIN (mthrows))
6058 {
6059 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6060 parse_error_context
781b0558 6061 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
6062 IDENTIFIER_POINTER
6063 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6064 }
6065
e04a16fb 6066 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 6067 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 6068
c2952b01
APB
6069 /* Inner class can't declare static methods */
6070 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6071 {
6072 char *t = xstrdup (lang_printable_name (class, 0));
6073 parse_error_context
6074 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6075 lang_printable_name (method, 0), t);
6076 free (t);
6077 }
6078
5e942c50 6079 /* Nothing overrides or it's a private method. */
aabd7048 6080 if (!found)
5e942c50 6081 continue;
aabd7048
PB
6082 if (METHOD_PRIVATE (found))
6083 {
6084 found = NULL_TREE;
6085 continue;
6086 }
5e942c50
APB
6087
6088 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6089 We set it temporarily for the sake of the error report. */
6090 saved_found_wfl = DECL_NAME (found);
6091 reset_method_name (found);
6092
614eaae0
APB
6093 /* If `found' is declared in an interface, make sure the
6094 modifier matches. */
6095 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6096 && clinit_identifier_node != DECL_NAME (found)
6097 && !METHOD_PUBLIC (method))
6098 {
6099 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6100 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6101 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6102 lang_printable_name (method, 0),
6103 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6104 }
6105
e04a16fb
AG
6106 /* Can't override a method with the same name and different return
6107 types. */
6108 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6109 {
614eaae0
APB
6110 char *t = xstrdup
6111 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6112 parse_error_context
7f10c2e2 6113 (method_wfl,
b9f7e36c 6114 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6115 lang_printable_name (found, 0), t,
b9f7e36c
APB
6116 IDENTIFIER_POINTER
6117 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6118 free (t);
6119 }
e04a16fb 6120
7f10c2e2
APB
6121 aflags = get_access_flags_from_decl (found);
6122 /* If the method has default, access in an other package, then
6123 issue a warning that the current method doesn't override the
6124 one that was found elsewhere. Do not issue this warning when
6125 the match was found in java.lang.Object. */
6126 if (DECL_CONTEXT (found) != object_type_node
a003f638 6127 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6128 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6129 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6130 && flag_not_overriding)
6131 {
6132 parse_warning_context
781b0558 6133 (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
6134 lang_printable_name (found, 0),
6135 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6136 IDENTIFIER_POINTER (DECL_NAME
6137 (TYPE_NAME (DECL_CONTEXT (found)))));
6138 continue;
6139 }
6140
e04a16fb
AG
6141 /* Can't override final. Can't override static. */
6142 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6143 {
6144 /* Static *can* override static */
6145 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6146 continue;
6147 parse_error_context
6148 (method_wfl,
6149 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6150 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6151 lang_printable_name (found, 0),
e04a16fb
AG
6152 (METHOD_FINAL (found) ? "final" : "static"),
6153 IDENTIFIER_POINTER
6154 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6155 continue;
6156 }
7f10c2e2 6157
e04a16fb
AG
6158 /* Static method can't override instance method. */
6159 if (METHOD_STATIC (method))
6160 {
6161 parse_error_context
6162 (method_wfl,
781b0558 6163 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6164 lang_printable_name (found, 0),
e04a16fb
AG
6165 IDENTIFIER_POINTER
6166 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6167 continue;
6168 }
5e942c50 6169
5e942c50
APB
6170 /* - Overriding/hiding public must be public
6171 - Overriding/hiding protected must be protected or public
6172 - If the overriden or hidden method has default (package)
6173 access, then the overriding or hiding method must not be
614eaae0
APB
6174 private; otherwise, a compile-time error occurs. If
6175 `found' belongs to an interface, things have been already
6176 taken care of. */
6177 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6178 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6179 || (METHOD_PROTECTED (found)
6180 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6181 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6182 && METHOD_PRIVATE (method))))
e04a16fb
AG
6183 {
6184 parse_error_context
6185 (method_wfl,
781b0558 6186 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6187 (METHOD_PUBLIC (method) ? "public" :
6188 (METHOD_PRIVATE (method) ? "private" : "protected")),
6189 IDENTIFIER_POINTER (DECL_NAME
6190 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6191 continue;
6192 }
6193
b9f7e36c
APB
6194 /* Overriding methods must have compatible `throws' clauses on checked
6195 exceptions, if any */
6196 check_throws_clauses (method, method_wfl, found);
6197
e04a16fb
AG
6198 /* Inheriting multiple methods with the same signature. FIXME */
6199 }
6200
5e942c50
APB
6201 /* Don't forget eventual pending found and saved_found_wfl. Take
6202 into account that we might have exited because we saw an
d77613be 6203 artificial method as the last entry. */
5e942c50
APB
6204
6205 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6206 DECL_NAME (found) = saved_found_wfl;
6207
23a79c61
APB
6208 if (!TYPE_NVIRTUALS (class))
6209 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6210
d77613be
APB
6211 /* Search for inherited abstract method not yet implemented in this
6212 class. */
6213 java_check_abstract_method_definitions (class_decl);
6214
22eed1e6 6215 if (!saw_constructor)
e920ebc9 6216 fatal ("No constructor found");
e04a16fb
AG
6217}
6218
b9f7e36c
APB
6219/* Return a non zero value if the `throws' clause of METHOD (if any)
6220 is incompatible with the `throws' clause of FOUND (if any). */
6221
6222static void
6223check_throws_clauses (method, method_wfl, found)
6224 tree method, method_wfl, found;
6225{
6226 tree mthrows, fthrows;
6227
c877974e
APB
6228 /* Can't check these things with class loaded from bytecode. FIXME */
6229 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6230 return;
6231
b9f7e36c
APB
6232 for (mthrows = DECL_FUNCTION_THROWS (method);
6233 mthrows; mthrows = TREE_CHAIN (mthrows))
6234 {
6235 /* We don't verify unchecked expressions */
c877974e 6236 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6237 continue;
6238 /* Checked expression must be compatible */
6239 for (fthrows = DECL_FUNCTION_THROWS (found);
6240 fthrows; fthrows = TREE_CHAIN (fthrows))
6241 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6242 break;
6243 if (!fthrows)
6244 {
6245 parse_error_context
781b0558 6246 (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 6247 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6248 lang_printable_name (found, 0),
b9f7e36c
APB
6249 IDENTIFIER_POINTER
6250 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6251 }
6252 }
6253}
6254
e04a16fb
AG
6255/* Check abstract method of interface INTERFACE */
6256
6257static void
5e942c50
APB
6258java_check_abstract_methods (interface_decl)
6259 tree interface_decl;
e04a16fb
AG
6260{
6261 int i, n;
6262 tree method, basetype_vec, found;
5e942c50 6263 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6264
6265 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6266 {
b9f7e36c 6267 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6268
6269 /* 2- Check for double definition inside the defining interface */
6270 if (check_method_redefinition (interface, method))
6271 continue;
6272
6273 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6274 the thrown exceptions (FIXME) */
e04a16fb
AG
6275 found = lookup_java_interface_method2 (interface, method);
6276 if (found)
6277 {
5e942c50
APB
6278 char *t;
6279 tree saved_found_wfl = DECL_NAME (found);
6280 reset_method_name (found);
c2e3db92 6281 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6282 parse_error_context
b9f7e36c 6283 (method_wfl,
5e942c50 6284 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6285 lang_printable_name (found, 0), t,
b9f7e36c
APB
6286 IDENTIFIER_POINTER
6287 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6288 free (t);
5e942c50 6289 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6290 continue;
e04a16fb
AG
6291 }
6292 }
6293
6294 /* 4- Inherited methods can't differ by their returned types */
6295 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6296 return;
6297 n = TREE_VEC_LENGTH (basetype_vec);
6298 for (i = 0; i < n; i++)
6299 {
6300 tree sub_interface_method, sub_interface;
6301 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6302 if (!vec_elt)
6303 continue;
6304 sub_interface = BINFO_TYPE (vec_elt);
6305 for (sub_interface_method = TYPE_METHODS (sub_interface);
6306 sub_interface_method;
6307 sub_interface_method = TREE_CHAIN (sub_interface_method))
6308 {
6309 found = lookup_java_interface_method2 (interface,
6310 sub_interface_method);
6311 if (found && (found != sub_interface_method))
5e942c50
APB
6312 {
6313 tree saved_found_wfl = DECL_NAME (found);
6314 reset_method_name (found);
6315 parse_error_context
6316 (lookup_cl (sub_interface_method),
781b0558 6317 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6318 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6319 lang_printable_name (found, 0),
6320 IDENTIFIER_POINTER
6321 (DECL_NAME (TYPE_NAME
6322 (DECL_CONTEXT (sub_interface_method)))),
6323 IDENTIFIER_POINTER
6324 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6325 DECL_NAME (found) = saved_found_wfl;
6326 }
e04a16fb
AG
6327 }
6328 }
6329}
6330
e04a16fb
AG
6331/* Lookup methods in interfaces using their name and partial
6332 signature. Return a matching method only if their types differ. */
6333
6334static tree
6335lookup_java_interface_method2 (class, method_decl)
6336 tree class, method_decl;
6337{
6338 int i, n;
6339 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6340
6341 if (!basetype_vec)
6342 return NULL_TREE;
6343
6344 n = TREE_VEC_LENGTH (basetype_vec);
6345 for (i = 0; i < n; i++)
6346 {
6347 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6348 if ((BINFO_TYPE (vec_elt) != object_type_node)
6349 && (to_return =
6350 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6351 return to_return;
6352 }
6353 for (i = 0; i < n; i++)
6354 {
6355 to_return = lookup_java_interface_method2
6356 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6357 if (to_return)
6358 return to_return;
6359 }
6360
6361 return NULL_TREE;
6362}
6363
6364/* Lookup method using their name and partial signature. Return a
6365 matching method only if their types differ. */
6366
6367static tree
6368lookup_java_method2 (clas, method_decl, do_interface)
6369 tree clas, method_decl;
6370 int do_interface;
6371{
5e942c50
APB
6372 tree method, method_signature, method_name, method_type, name;
6373
e04a16fb 6374 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6375 name = DECL_NAME (method_decl);
6376 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6377 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6378 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6379
6380 while (clas != NULL_TREE)
6381 {
6382 for (method = TYPE_METHODS (clas);
6383 method != NULL_TREE; method = TREE_CHAIN (method))
6384 {
6385 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6386 tree name = DECL_NAME (method);
6387 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6388 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6389 && method_sig == method_signature
6390 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6391 return method;
e04a16fb
AG
6392 }
6393 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6394 }
6395 return NULL_TREE;
6396}
6397
f441f671
APB
6398/* Return the line that matches DECL line number, and try its best to
6399 position the column number. Used during error reports. */
e04a16fb
AG
6400
6401static tree
6402lookup_cl (decl)
6403 tree decl;
6404{
6405 static tree cl = NULL_TREE;
f441f671 6406 char *line, *found;
e04a16fb
AG
6407
6408 if (!decl)
6409 return NULL_TREE;
6410
6411 if (cl == NULL_TREE)
6412 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6413
6414 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6415 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6416
f441f671
APB
6417 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6418 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6419
6420 found = strstr ((const char *)line,
6421 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6422 if (found)
6423 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6424
e04a16fb
AG
6425 return cl;
6426}
6427
6428/* Look for a simple name in the single-type import list */
6429
6430static tree
6431find_name_in_single_imports (name)
6432 tree name;
6433{
6434 tree node;
6435
6436 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6437 if (TREE_VALUE (node) == name)
6438 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6439
6440 return NULL_TREE;
6441}
6442
6443/* Process all single-type import. */
6444
6445static int
6446process_imports ()
6447{
6448 tree import;
6449 int error_found;
6450
6451 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6452 {
6453 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6454
6455 /* Don't load twice something already defined. */
6456 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6457 continue;
6458 QUALIFIED_P (to_be_found) = 1;
6459 load_class (to_be_found, 0);
6460 error_found =
6461 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6462 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6463 {
6464 parse_error_context (TREE_PURPOSE (import),
6465 "Class or interface `%s' not found in import",
6466 IDENTIFIER_POINTER (to_be_found));
6467 return 1;
6468 }
6469 if (error_found)
6470 return 1;
6471 }
6472 return 0;
6473}
6474
9a7ab4b3
APB
6475/* Possibly find and mark a class imported by a single-type import
6476 statement. */
e04a16fb 6477
9a7ab4b3 6478static void
e04a16fb
AG
6479find_in_imports (class_type)
6480 tree class_type;
6481{
6482 tree import;
6483
6484 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6485 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6486 {
6487 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6488 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb 6489 }
e04a16fb
AG
6490}
6491
e04a16fb 6492static int
63a212ed 6493note_possible_classname (name, len)
49f48c71 6494 const char *name;
63a212ed 6495 int len;
e04a16fb 6496{
63a212ed
PB
6497 tree node;
6498 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6499 len = len - 5;
6500 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6501 len = len - 6;
e04a16fb 6502 else
63a212ed
PB
6503 return 0;
6504 node = ident_subst (name, len, "", '/', '.', "");
6505 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6506 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6507 return 1;
e04a16fb
AG
6508}
6509
6510/* Read a import directory, gathering potential match for further type
6511 references. Indifferently reads a filesystem or a ZIP archive
6512 directory. */
6513
6514static void
6515read_import_dir (wfl)
6516 tree wfl;
6517{
63a212ed 6518 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6519 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6520 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6521 DIR *dirp = NULL;
d8fccff5 6522 JCF *saved_jcf = current_jcf;
e04a16fb 6523
63a212ed
PB
6524 int found = 0;
6525 int k;
6526 void *entry;
6527 struct buffer filename[1];
6528
6529
6530 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6531 return;
6532 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6533
6534 BUFFER_INIT (filename);
6535 buffer_grow (filename, package_length + 100);
6536
6537 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6538 {
49f48c71 6539 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6540 int entry_length = strlen (entry_name);
6541 if (jcf_path_is_zipfile (entry))
6542 {
6543 ZipFile *zipf;
6544 buffer_grow (filename, entry_length);
6545 memcpy (filename->data, entry_name, entry_length - 1);
6546 filename->data[entry_length-1] = '\0';
6547 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6548 if (zipf == NULL)
6549 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6550 else
6551 {
6552 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6553 BUFFER_RESET (filename);
6554 for (k = 0; k < package_length; k++)
6555 {
6556 char ch = package_name[k];
6557 *filename->ptr++ = ch == '.' ? '/' : ch;
6558 }
6559 *filename->ptr++ = '/';
6560
345137c7 6561 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6562 {
49f48c71 6563 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6564 int current_entry_len = zipd->filename_length;
6565
345137c7
TT
6566 if (current_entry_len >= BUFFER_LENGTH (filename)
6567 && strncmp (filename->data, current_entry,
6568 BUFFER_LENGTH (filename)) != 0)
63a212ed 6569 continue;
345137c7 6570 found |= note_possible_classname (current_entry,
63a212ed
PB
6571 current_entry_len);
6572 }
6573 }
6574 }
6575 else
6576 {
6577 BUFFER_RESET (filename);
6578 buffer_grow (filename, entry_length + package_length + 4);
6579 strcpy (filename->data, entry_name);
6580 filename->ptr = filename->data + entry_length;
6581 for (k = 0; k < package_length; k++)
6582 {
6583 char ch = package_name[k];
6584 *filename->ptr++ = ch == '.' ? '/' : ch;
6585 }
6586 *filename->ptr = '\0';
6587
6588 dirp = opendir (filename->data);
6589 if (dirp == NULL)
6590 continue;
6591 *filename->ptr++ = '/';
6592 for (;;)
6593 {
63a212ed 6594 int len;
49f48c71 6595 const char *d_name;
63a212ed
PB
6596 struct dirent *direntp = readdir (dirp);
6597 if (!direntp)
6598 break;
6599 d_name = direntp->d_name;
6600 len = strlen (direntp->d_name);
6601 buffer_grow (filename, len+1);
6602 strcpy (filename->ptr, d_name);
345137c7 6603 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6604 package_length+len+1);
6605 }
6606 if (dirp)
6607 closedir (dirp);
6608 }
6609 }
e04a16fb 6610
63a212ed 6611 free (filename->data);
e04a16fb 6612
63a212ed
PB
6613 /* Here we should have a unified way of retrieving an entry, to be
6614 indexed. */
6615 if (!found)
e04a16fb
AG
6616 {
6617 static int first = 1;
6618 if (first)
6619 {
781b0558 6620 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6621 java_error_count++;
6622 first = 0;
6623 }
6624 else
63a212ed
PB
6625 parse_error_context (wfl, "Package `%s' not found in import",
6626 package_name);
e04a16fb
AG
6627 current_jcf = saved_jcf;
6628 return;
6629 }
e04a16fb
AG
6630 current_jcf = saved_jcf;
6631}
6632
6633/* Possibly find a type in the import on demands specified
6634 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6635 entire list, to detected potential double definitions. */
6636
6637static int
6638find_in_imports_on_demand (class_type)
6639 tree class_type;
6640{
ab3a6dd6 6641 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6642 int seen_once = -1;
ab3a6dd6 6643 tree cl = NULL_TREE;
e04a16fb
AG
6644
6645 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6646 {
49f48c71 6647 const char *id_name;
e04a16fb
AG
6648 obstack_grow (&temporary_obstack,
6649 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6650 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6651 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6652 obstack_grow0 (&temporary_obstack,
6653 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6654 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6655 id_name = obstack_finish (&temporary_obstack);
6656
6657 node = maybe_get_identifier (id_name);
6658 if (node && IS_A_CLASSFILE_NAME (node))
6659 {
6660 if (seen_once < 0)
6661 {
6662 cl = TREE_PURPOSE (import);
6663 seen_once = 1;
6664 node_to_use = node;
6665 }
6666 else
6667 {
6668 seen_once++;
6669 parse_error_context
1e12ab9b
APB
6670 (TREE_PURPOSE (import),
6671 "Type `%s' also potentially defined in package `%s'",
e04a16fb
AG
6672 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6673 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6674 }
6675 }
6676 }
6677
6678 if (seen_once == 1)
6679 {
6680 /* Setup lineno so that it refers to the line of the import (in
6681 case we parse a class file and encounter errors */
6682 tree decl;
6683 int saved_lineno = lineno;
6684 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6685 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6686 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6687 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6688 /* If there is no DECL set for the class or if the class isn't
6689 loaded and not seen in source yet, the load */
6690 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6691 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6692 load_class (node_to_use, 0);
6693 lineno = saved_lineno;
6694 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6695 }
6696 else
6697 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6698}
6699
9a7ab4b3
APB
6700/* Add package NAME to the list of package encountered so far. To
6701 speed up class lookup in do_resolve_class, we make sure a
6702 particular package is added only once. */
6703
6704static void
6705register_package (name)
6706 tree name;
6707{
6708 static struct hash_table _pht, *pht = NULL;
6709
6710 if (!pht)
6711 {
6712 hash_table_init (&_pht, hash_newfunc,
6713 java_hash_hash_tree_node, java_hash_compare_tree_node);
6714 pht = &_pht;
6715 }
6716
6717 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6718 {
6719 package_list = chainon (package_list, build_tree_list (name, NULL));
6720 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6721 }
6722}
6723
5e942c50
APB
6724static tree
6725resolve_package (pkg, next)
6726 tree pkg, *next;
6727{
c2952b01 6728 tree current, acc;
5e942c50 6729 tree type_name = NULL_TREE;
49f48c71 6730 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6731
6732 /* The trick is to determine when the package name stops and were
6733 the name of something contained in the package starts. Then we
6734 return a fully qualified name of what we want to get. */
6735
6736 /* Do a quick search on well known package names */
6737 if (!strncmp (name, "java.lang.reflect", 17))
6738 {
6739 *next =
6740 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6741 type_name = lookup_package_type (name, 17);
6742 }
6743 else if (!strncmp (name, "java.lang", 9))
6744 {
6745 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6746 type_name = lookup_package_type (name, 9);
6747 }
5e942c50 6748
2c56429a
APB
6749 /* If we found something here, return */
6750 if (type_name)
6751 return type_name;
6752
6753 *next = EXPR_WFL_QUALIFICATION (pkg);
6754
6755 /* Try the current package. */
6756 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6757 IDENTIFIER_LENGTH (ctxp->package)))
6758 {
6759 type_name =
6760 lookup_package_type_and_set_next (name,
6761 IDENTIFIER_LENGTH (ctxp->package),
6762 next );
6763 if (type_name)
6764 return type_name;
6765 }
6766
6767 /* Search in imported package */
6768 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6769 {
6770 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6771 int len = IDENTIFIER_LENGTH (current_pkg_name);
6772 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6773 {
6774 tree left, dummy;
6775
6776 breakdown_qualified (&left, &dummy, current_pkg_name);
6777 len = IDENTIFIER_LENGTH (left);
6778 type_name = lookup_package_type_and_set_next (name, len, next);
6779 if (type_name)
6780 break;
6781 }
6782 }
6783
c2952b01
APB
6784 /* Try to progressively construct a type name */
6785 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6786 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6787 current; current = TREE_CHAIN (current))
6788 {
6789 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6790 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6791 {
6792 type_name = acc;
6b48deee
APB
6793 /* resolve_package should be used in a loop, hence we
6794 point at this one to naturally process the next one at
6795 the next iteration. */
6796 *next = current;
c2952b01
APB
6797 break;
6798 }
6799 }
2c56429a
APB
6800 return type_name;
6801}
6802
6803static tree
6804lookup_package_type_and_set_next (name, len, next)
49f48c71 6805 const char *name;
2c56429a
APB
6806 int len;
6807 tree *next;
6808{
49f48c71 6809 const char *ptr;
2c56429a
APB
6810 tree type_name = lookup_package_type (name, len);
6811
6812 if (!type_name)
6813 return NULL;
6814
6815 ptr = IDENTIFIER_POINTER (type_name);
6816 while (ptr && (ptr = strchr (ptr, '.')))
6817 {
6818 *next = TREE_CHAIN (*next);
6819 ptr++;
6820 }
5e942c50
APB
6821 return type_name;
6822}
6823
6824static tree
6825lookup_package_type (name, from)
49f48c71 6826 const char *name;
5e942c50
APB
6827 int from;
6828{
6829 char subname [128];
49f48c71 6830 const char *sub = &name[from+1];
5e942c50
APB
6831 while (*sub != '.' && *sub)
6832 sub++;
6833 strncpy (subname, name, sub-name);
6834 subname [sub-name] = '\0';
6835 return get_identifier (subname);
6836}
6837
cf1748bf 6838static void
4dbf4496
APB
6839check_inner_class_access (decl, enclosing_decl, cl)
6840 tree decl, enclosing_decl, cl;
cf1748bf 6841{
4dbf4496
APB
6842 int access = 0;
6843
cf1748bf 6844 /* We don't issue an error message when CL is null. CL can be null
4dbf4496
APB
6845 as a result of processing a JDEP crafted by source_start_java_method
6846 for the purpose of patching its parm decl. But the error would
6847 have been already trapped when fixing the method's signature.
6848 DECL can also be NULL in case of earlier errors. */
6849 if (!decl || !cl)
cf1748bf
APB
6850 return;
6851
4dbf4496
APB
6852 /* We grant access to private and protected inner classes if the
6853 location from where we're trying to access DECL is an enclosing
6854 context for DECL or if both have a common enclosing context. */
6855 if (CLASS_PRIVATE (decl))
6856 access = 1;
6857 if (CLASS_PROTECTED (decl))
6858 access = 2;
6859 if (!access)
6860 return;
6861
6862 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6863 TREE_TYPE (decl))
6864 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6865 TREE_TYPE (decl)))
6866 return;
6867
6868 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6869 (access == 1 ? "private" : "protected"),
cf1748bf
APB
6870 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6871 lang_printable_name (decl, 0));
6872}
6873
e04a16fb
AG
6874/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6875 access violations were found, 1 otherwise. */
6876
6877static int
6878check_pkg_class_access (class_name, cl)
6879 tree class_name;
6880 tree cl;
6881{
6882 tree type;
e04a16fb
AG
6883
6884 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6885 return 0;
6886
6887 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6888 return 0;
6889
6890 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6891 {
e28cd97b
APB
6892 /* Access to a private class within the same package is
6893 allowed. */
6894 tree l, r;
6895 breakdown_qualified (&l, &r, class_name);
6896 if (l == ctxp->package)
6897 return 0;
6898
e04a16fb 6899 parse_error_context
781b0558 6900 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6901 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6902 IDENTIFIER_POINTER (class_name));
6903 return 1;
6904 }
6905 return 0;
6906}
6907
6908/* Local variable declaration. */
6909
6910static void
6911declare_local_variables (modifier, type, vlist)
6912 int modifier;
6913 tree type;
6914 tree vlist;
6915{
c583dd46
APB
6916 tree decl, current, saved_type;
6917 tree type_wfl = NULL_TREE;
e04a16fb 6918 int must_chain = 0;
c2952b01 6919 int final_p = 0;
e04a16fb 6920
2aa11e97 6921 /* Push a new block if statements were seen between the last time we
e04a16fb 6922 pushed a block and now. Keep a cound of block to close */
f099f336 6923 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6924 {
f099f336 6925 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6926 tree b = enter_block ();
f099f336 6927 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6928 }
6929
6930 if (modifier)
6931 {
6932 int i;
6933 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6934 if (modifier == ACC_FINAL)
c2952b01 6935 final_p = 1;
c877974e
APB
6936 else
6937 {
6938 parse_error_context
6939 (ctxp->modifier_ctx [i],
6940 "Only `final' is allowed as a local variables modifier");
6941 return;
6942 }
e04a16fb
AG
6943 }
6944
c583dd46
APB
6945 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6946 hold the TYPE value if a new incomplete has to be created (as
6947 opposed to being found already existing and reused). */
6948 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6949
6950 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6951 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6952
6953 /* Go through all the declared variables */
6954 for (current = vlist, saved_type = type; current;
6955 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6956 {
c877974e 6957 tree other, real_type;
e04a16fb
AG
6958 tree wfl = TREE_PURPOSE (current);
6959 tree name = EXPR_WFL_NODE (wfl);
6960 tree init = TREE_VALUE (current);
e04a16fb 6961
c583dd46
APB
6962 /* Process NAME, as it may specify extra dimension(s) for it */
6963 type = build_array_from_name (type, type_wfl, name, &name);
6964
6965 /* Variable redefinition check */
6966 if ((other = lookup_name_in_blocks (name)))
6967 {
6968 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6969 DECL_SOURCE_LINE (other));
6970 continue;
6971 }
6972
6973 /* Type adjustment. We may have just readjusted TYPE because
6974 the variable specified more dimensions. Make sure we have
6975 a reference if we can and don't have one already. */
1886c9d8 6976 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6977
6978 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6979 /* Never layout this decl. This will be done when its scope
6980 will be entered */
c877974e 6981 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6982 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6983 BLOCK_CHAIN_DECL (decl);
6984
d4370213
APB
6985 /* If doing xreferencing, replace the line number with the WFL
6986 compound value */
6987 if (flag_emit_xref)
6988 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6989
e04a16fb
AG
6990 /* Don't try to use an INIT statement when an error was found */
6991 if (init && java_error_count)
6992 init = NULL_TREE;
c583dd46
APB
6993
6994 /* Add the initialization function to the current function's code */
6995 if (init)
e04a16fb 6996 {
c583dd46
APB
6997 /* Name might have been readjusted */
6998 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6999 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7000 java_method_add_stmt (current_function_decl,
7001 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7002 init));
7003 }
7004
7005 /* Setup dependency the type of the decl */
7006 if (must_chain)
7007 {
7008 jdep *dep;
7009 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7010 dep = CLASSD_LAST (ctxp->classd_list);
7011 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
7012 }
7013 }
7014 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7015}
7016
7017/* Called during parsing. Build decls from argument list. */
7018
7019static void
7020source_start_java_method (fndecl)
7021 tree fndecl;
7022{
7023 tree tem;
7024 tree parm_decl;
7025 int i;
7026
79d13333
APB
7027 if (!fndecl)
7028 return;
7029
e04a16fb
AG
7030 current_function_decl = fndecl;
7031
7032 /* New scope for the function */
7033 enter_block ();
7034 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 7035 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
7036 {
7037 tree type = TREE_VALUE (tem);
7038 tree name = TREE_PURPOSE (tem);
7039
23a79c61
APB
7040 /* If type is incomplete. Create an incomplete decl and ask for
7041 the decl to be patched later */
e04a16fb
AG
7042 if (INCOMPLETE_TYPE_P (type))
7043 {
7044 jdep *jdep;
c877974e
APB
7045 tree real_type = GET_REAL_TYPE (type);
7046 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 7047 type = obtain_incomplete_type (type);
e04a16fb
AG
7048 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7049 jdep = CLASSD_LAST (ctxp->classd_list);
7050 JDEP_MISC (jdep) = name;
7051 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7052 }
7053 else
7054 parm_decl = build_decl (PARM_DECL, name, type);
7055
c2952b01
APB
7056 /* Remember if a local variable was declared final (via its
7057 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7058 if (ARG_FINAL_P (tem))
7059 LOCAL_FINAL (parm_decl) = 1;
7060
e04a16fb
AG
7061 BLOCK_CHAIN_DECL (parm_decl);
7062 }
7063 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7064 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7065 nreverse (tem);
7066 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 7067 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
7068}
7069
22eed1e6
APB
7070/* Called during parsing. Creates an artificial method declaration. */
7071
7072static tree
7073create_artificial_method (class, flags, type, name, args)
7074 tree class;
7075 int flags;
7076 tree type, name, args;
7077{
22eed1e6
APB
7078 tree mdecl;
7079
c2952b01 7080 java_parser_context_save_global ();
22eed1e6
APB
7081 lineno = 0;
7082 mdecl = make_node (FUNCTION_TYPE);
7083 TREE_TYPE (mdecl) = type;
7084 TYPE_ARG_TYPES (mdecl) = args;
7085 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 7086 java_parser_context_restore_global ();
22eed1e6
APB
7087 DECL_ARTIFICIAL (mdecl) = 1;
7088 return mdecl;
7089}
7090
7091/* Starts the body if an artifical method. */
7092
7093static void
7094start_artificial_method_body (mdecl)
7095 tree mdecl;
7096{
7097 DECL_SOURCE_LINE (mdecl) = 1;
7098 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7099 source_start_java_method (mdecl);
7100 enter_block ();
7101}
7102
7103static void
7104end_artificial_method_body (mdecl)
7105 tree mdecl;
7106{
7107 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7108 exit_block ();
7109}
7110
e04a16fb
AG
7111/* Called during expansion. Push decls formerly built from argument
7112 list so they're usable during expansion. */
7113
7114static void
7115expand_start_java_method (fndecl)
7116 tree fndecl;
7117{
7118 tree tem, *ptr;
e04a16fb 7119
e04a16fb
AG
7120 current_function_decl = fndecl;
7121
c2952b01
APB
7122 if (! quiet_flag)
7123 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 7124 announce_function (fndecl);
c2952b01
APB
7125 if (! quiet_flag)
7126 fprintf (stderr, "]");
7127
7128 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
7129 ptr = &DECL_ARGUMENTS (fndecl);
7130 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7131 while (tem)
7132 {
7133 tree next = TREE_CHAIN (tem);
b67d701b 7134 tree type = TREE_TYPE (tem);
e438e1b7
JJ
7135 if (PROMOTE_PROTOTYPES
7136 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
7137 && INTEGRAL_TYPE_P (type))
7138 type = integer_type_node;
b67d701b 7139 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
7140 layout_decl (tem, 0);
7141 pushdecl (tem);
e04a16fb
AG
7142 *ptr = tem;
7143 ptr = &TREE_CHAIN (tem);
7144 tem = next;
7145 }
7146 *ptr = NULL_TREE;
7147 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7148 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7149}
7150
7151/* Terminate a function and expand its body. */
7152
7153static void
7154source_end_java_method ()
7155{
7156 tree fndecl = current_function_decl;
138657ec 7157 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7158
79d13333
APB
7159 if (!fndecl)
7160 return;
7161
e04a16fb
AG
7162 java_parser_context_save_global ();
7163 lineno = ctxp->last_ccb_indent1;
7164
b67d701b
PB
7165 /* Set EH language codes */
7166 java_set_exception_lang_code ();
7167
5423609c
APB
7168 /* Turn function bodies with only a NOP expr null, so they don't get
7169 generated at all and we won't get warnings when using the -W
7170 -Wall flags. */
7171 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7172 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7173
e04a16fb
AG
7174 /* Generate function's code */
7175 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7176 && ! flag_emit_class_files
7177 && ! flag_emit_xref)
e04a16fb
AG
7178 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7179
7180 /* pop out of its parameters */
7181 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7182 poplevel (1, 0, 1);
7183 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7184
7185 /* Generate rtl for function exit. */
e8fc7396 7186 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7187 {
7188 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7189 /* Emit catch-finally clauses */
7190 emit_handlers ();
e04a16fb
AG
7191 expand_function_end (input_filename, lineno, 0);
7192
138657ec
AH
7193 /* FIXME: If the current method contains any exception handlers,
7194 force asynchronous_exceptions: this is necessary because signal
7195 handlers in libjava may throw exceptions. This is far from being
7196 a perfect solution, but it's better than doing nothing at all.*/
7197 if (catch_clauses)
7198 asynchronous_exceptions = 1;
7199
e04a16fb
AG
7200 /* Run the optimizers and output assembler code for this function. */
7201 rest_of_compilation (fndecl);
7202 }
7203
7204 current_function_decl = NULL_TREE;
8226320b 7205 permanent_allocation (1);
e04a16fb 7206 java_parser_context_restore_global ();
138657ec 7207 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7208}
7209
7210/* Record EXPR in the current function block. Complements compound
7211 expression second operand if necessary. */
7212
7213tree
7214java_method_add_stmt (fndecl, expr)
7215 tree fndecl, expr;
7216{
b771925e
APB
7217 if (!GET_CURRENT_BLOCK (fndecl))
7218 return NULL_TREE;
f099f336 7219 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7220}
e04a16fb 7221
b67d701b
PB
7222static tree
7223add_stmt_to_block (b, type, stmt)
7224 tree b, type, stmt;
7225{
7226 tree body = BLOCK_EXPR_BODY (b), c;
7227
e04a16fb
AG
7228 if (java_error_count)
7229 return body;
b67d701b
PB
7230
7231 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7232 return body;
7233
b67d701b
PB
7234 BLOCK_EXPR_BODY (b) = c;
7235 TREE_SIDE_EFFECTS (c) = 1;
7236 return c;
e04a16fb
AG
7237}
7238
7239/* Add STMT to EXISTING if possible, otherwise create a new
7240 COMPOUND_EXPR and add STMT to it. */
7241
7242static tree
7243add_stmt_to_compound (existing, type, stmt)
7244 tree existing, type, stmt;
7245{
15fdcfe9
PB
7246 if (existing)
7247 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7248 else
15fdcfe9 7249 return stmt;
e04a16fb
AG
7250}
7251
7252/* Hold THIS for the scope of the current public method decl. */
7253static tree current_this;
7254
1886c9d8
APB
7255void java_layout_seen_class_methods ()
7256{
7257 tree previous_list = all_class_list;
7258 tree end = NULL_TREE;
7259 tree current;
7260
7261 while (1)
7262 {
7263 for (current = previous_list;
7264 current != end; current = TREE_CHAIN (current))
7265 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7266
7267 if (previous_list != all_class_list)
7268 {
7269 end = previous_list;
7270 previous_list = all_class_list;
7271 }
7272 else
7273 break;
7274 }
7275}
7276
e04a16fb 7277void
c2952b01 7278java_reorder_fields ()
e04a16fb 7279{
c2952b01 7280 static tree stop_reordering = NULL_TREE;
23a79c61 7281
c2952b01 7282 tree current;
5e942c50 7283 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7284 {
5e942c50 7285 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7286
c2952b01
APB
7287 if (current_class == stop_reordering)
7288 break;
7289
c877974e
APB
7290 /* Reverse the fields, but leave the dummy field in front.
7291 Fields are already ordered for Object and Class */
7292 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7293 && current_class != class_type_node)
7294 {
23a79c61
APB
7295 /* If the dummy field is there, reverse the right fields and
7296 just layout the type for proper fields offset */
c877974e
APB
7297 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7298 {
7299 tree fields = TYPE_FIELDS (current_class);
7300 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7301 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7302 }
23a79c61
APB
7303 /* We don't have a dummy field, we need to layout the class,
7304 after having reversed the fields */
c877974e
APB
7305 else
7306 {
7307 TYPE_FIELDS (current_class) =
7308 nreverse (TYPE_FIELDS (current_class));
7309 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7310 }
7311 }
c2952b01
APB
7312 }
7313 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7314}
7315
7316/* Layout the methods of all classes loaded in one way on an
7317 other. Check methods of source parsed classes. Then reorder the
7318 fields and layout the classes or the type of all source parsed
7319 classes */
7320
7321void
7322java_layout_classes ()
7323{
7324 tree current;
7325 int save_error_count = java_error_count;
7326
7327 /* Layout the methods of all classes seen so far */
7328 java_layout_seen_class_methods ();
7329 java_parse_abort_on_error ();
7330 all_class_list = NULL_TREE;
7331
7332 /* Then check the methods of all parsed classes */
7333 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7334 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
34d4df06 7335 java_check_methods (TREE_VALUE (current));
c2952b01
APB
7336 java_parse_abort_on_error ();
7337
7338 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7339 {
7340 current_class = TREE_TYPE (TREE_VALUE (current));
7341 layout_class (current_class);
5e942c50 7342
c877974e
APB
7343 /* From now on, the class is considered completely loaded */
7344 CLASS_LOADED_P (current_class) = 1;
7345
5e942c50
APB
7346 /* Error reported by the caller */
7347 if (java_error_count)
7348 return;
e04a16fb 7349 }
23a79c61
APB
7350
7351 /* We might have reloaded classes durign the process of laying out
7352 classes for code generation. We must layout the methods of those
7353 late additions, as constructor checks might use them */
1886c9d8 7354 java_layout_seen_class_methods ();
23a79c61 7355 java_parse_abort_on_error ();
e04a16fb
AG
7356}
7357
c2952b01
APB
7358/* Expand methods in the current set of classes rememebered for
7359 generation. */
e04a16fb 7360
49f48c71 7361static void
c2952b01 7362java_complete_expand_classes ()
e04a16fb
AG
7363{
7364 tree current;
ce6e9147
APB
7365
7366 do_not_fold = flag_emit_xref;
c2952b01 7367
e04a16fb 7368 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7369 if (!INNER_CLASS_DECL_P (current))
7370 java_complete_expand_class (current);
7371}
e04a16fb 7372
c2952b01
APB
7373/* Expand the methods found in OUTER, starting first by OUTER's inner
7374 classes, if any. */
e04a16fb 7375
c2952b01
APB
7376static void
7377java_complete_expand_class (outer)
7378 tree outer;
7379{
7380 tree inner_list;
e04a16fb 7381
c2952b01 7382 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7383
c2952b01
APB
7384 /* We need to go after all inner classes and start expanding them,
7385 starting with most nested ones. We have to do that because nested
7386 classes might add functions to outer classes */
e04a16fb 7387
c2952b01
APB
7388 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7389 inner_list; inner_list = TREE_CHAIN (inner_list))
7390 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7391
c2952b01
APB
7392 java_complete_expand_methods (outer);
7393 set_nested_class_simple_name_value (outer, 0); /* Reset */
7394}
7395
7396/* Expand methods registered in CLASS_DECL. The general idea is that
7397 we expand regular methods first. This allows us get an estimate on
7398 how outer context local alias fields are really used so we can add
7399 to the constructor just enough code to initialize them properly (it
c00f0fb2 7400 also lets us generate finit$ correctly.) Then we expand the
c2952b01
APB
7401 constructors and then <clinit>. */
7402
7403static void
7404java_complete_expand_methods (class_decl)
7405 tree class_decl;
7406{
7407 tree clinit, finit, decl, first_decl;
7408
7409 current_class = TREE_TYPE (class_decl);
7410
7411 /* Initialize a new constant pool */
7412 init_outgoing_cpool ();
7413
7414 /* Pre-expand <clinit> to figure whether we really need it or
7415 not. If we do need it, we pre-expand the static fields so they're
7416 ready to be used somewhere else. <clinit> will be fully expanded
7417 after we processed the constructors. */
7418 first_decl = TYPE_METHODS (current_class);
7419 clinit = maybe_generate_pre_expand_clinit (current_class);
7420
c00f0fb2 7421 /* Then generate finit$ (if we need to) because constructor will
c2952b01
APB
7422 try to use it.*/
7423 if (TYPE_FINIT_STMT_LIST (current_class))
7424 {
7425 finit = generate_finit (current_class);
7426 java_complete_expand_method (finit);
7427 }
7428
7429 /* Now do the constructors */
7430 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7431 {
7432 int no_body;
7433
7434 if (!DECL_CONSTRUCTOR_P (decl))
7435 continue;
7436
7437 no_body = !DECL_FUNCTION_BODY (decl);
7438 /* Don't generate debug info on line zero when expanding a
7439 generated constructor. */
7440 if (no_body)
7441 restore_line_number_status (1);
7442
7443 java_complete_expand_method (decl);
7444
7445 if (no_body)
7446 restore_line_number_status (0);
7447 }
7448
7449 /* First, do the ordinary methods. */
7450 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7451 {
7145d9fe
TT
7452 /* Skip abstract or native methods -- but do handle native
7453 methods when generating JNI stubs. */
7454 if (METHOD_ABSTRACT (decl)
7455 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7456 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7457 continue;
7145d9fe
TT
7458
7459 if (METHOD_NATIVE (decl))
7460 {
7461 tree body = build_jni_stub (decl);
7462 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7463 }
7464
c2952b01
APB
7465 java_complete_expand_method (decl);
7466 }
7467
7468 /* If there is indeed a <clinit>, fully expand it now */
7469 if (clinit)
7470 {
7471 /* Prevent the use of `this' inside <clinit> */
7472 ctxp->explicit_constructor_p = 1;
7473 java_complete_expand_method (clinit);
7474 ctxp->explicit_constructor_p = 0;
e04a16fb 7475 }
c2952b01 7476
165f37bc
APB
7477 /* We might have generated a class$ that we now want to expand */
7478 if (TYPE_DOT_CLASS (current_class))
7479 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7480
c2952b01
APB
7481 /* Now verify constructor circularity (stop after the first one we
7482 prove wrong.) */
7483 if (!CLASS_INTERFACE (class_decl))
7484 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7485 if (DECL_CONSTRUCTOR_P (decl)
7486 && verify_constructor_circularity (decl, decl))
7487 break;
7488
7489 /* Save the constant pool. We'll need to restore it later. */
7490 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7491}
7492
b9f7e36c
APB
7493/* Hold a list of catch clauses list. The first element of this list is
7494 the list of the catch clauses of the currently analysed try block. */
7495static tree currently_caught_type_list;
7496
c2952b01
APB
7497/* Attempt to create <clinit>. Pre-expand static fields so they can be
7498 safely used in some other methods/constructors. */
e920ebc9 7499
c2952b01
APB
7500static tree
7501maybe_generate_pre_expand_clinit (class_type)
7502 tree class_type;
e920ebc9 7503{
c2952b01
APB
7504 tree current, mdecl;
7505
7506 if (!TYPE_CLINIT_STMT_LIST (class_type))
7507 return NULL_TREE;
e920ebc9 7508
c2952b01
APB
7509 /* Go through all static fields and pre expand them */
7510 for (current = TYPE_FIELDS (class_type); current;
7511 current = TREE_CHAIN (current))
7512 if (FIELD_STATIC (current))
7513 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7514
7515 /* Then build the <clinit> method */
7516 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7517 clinit_identifier_node, end_params_node);
7518 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7519 mdecl, NULL_TREE);
7520 start_artificial_method_body (mdecl);
7521
7522 /* We process the list of assignment we produced as the result of
7523 the declaration of initialized static field and add them as
7524 statement to the <clinit> method. */
7525 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7526 current = TREE_CHAIN (current))
e920ebc9 7527 {
9a7ab4b3 7528 tree stmt = current;
c2952b01
APB
7529 /* We build the assignment expression that will initialize the
7530 field to its value. There are strict rules on static
7531 initializers (8.5). FIXME */
98a52c2c 7532 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
9a7ab4b3 7533 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
c2952b01
APB
7534 java_method_add_stmt (mdecl, stmt);
7535 }
e920ebc9 7536
c2952b01
APB
7537 end_artificial_method_body (mdecl);
7538
92d83515
APB
7539 /* Now we want to place <clinit> as the last method (because we need
7540 it at least for interface so that it doesn't interfere with the
7541 dispatch table based lookup. */
7542 if (TREE_CHAIN (TYPE_METHODS (class_type)))
c2952b01 7543 {
92d83515
APB
7544 current = TREE_CHAIN (TYPE_METHODS (class_type));
7545 TYPE_METHODS (class_type) = current;
c2952b01
APB
7546
7547 while (TREE_CHAIN (current))
7548 current = TREE_CHAIN (current);
92d83515 7549
c2952b01
APB
7550 TREE_CHAIN (current) = mdecl;
7551 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7552 }
c2952b01
APB
7553
7554 return mdecl;
e920ebc9
APB
7555}
7556
92d83515
APB
7557/* See whether we could get rid of <clinit>. Criteria are: all static
7558 final fields have constant initial values and the body of <clinit>
7559 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7560
7561static int
7562maybe_yank_clinit (mdecl)
7563 tree mdecl;
7564{
7565 tree type, current;
7566 tree fbody, bbody;
7567
7568 if (!DECL_CLINIT_P (mdecl))
7569 return 0;
f0f3a777
APB
7570
7571 /* If the body isn't empty, then we keep <clinit>. Note that if
7572 we're emitting classfiles, this isn't enough not to rule it
7573 out. */
92d83515
APB
7574 fbody = DECL_FUNCTION_BODY (mdecl);
7575 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7576 bbody = BLOCK_EXPR_BODY (bbody);
f0f3a777 7577 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
92d83515
APB
7578 return 0;
7579
7580 type = DECL_CONTEXT (mdecl);
7581 current = TYPE_FIELDS (type);
7582
7583 for (current = (current ? TREE_CHAIN (current) : current);
7584 current; current = TREE_CHAIN (current))
f0f3a777
APB
7585 {
7586 tree f_init;
7587
7588 /* We're not interested in non static field */
7589 if (!FIELD_STATIC (current))
7590 continue;
7591
7592 /* Anything that isn't String or a basic type is ruled out -- or
7593 if we now how to deal with it (when doing things natively) we
7594 should generated an empty <clinit> so that SUID are computed
7595 correctly. */
7596 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7597 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7598 break;
7599
7600 f_init = DECL_INITIAL (current);
7601 /* If we're emitting native code, we want static final fields to
7602 have constant initializers. If we don't meet these
7603 conditions, we keep <clinit> */
7604 if (!flag_emit_class_files
7605 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7606 break;
7607 /* If we're emitting bytecode, we want static fields to have
7608 constant initializers or no initializer. If we don't meet
7609 these conditions, we keep <clinit> */
7610 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7611 break;
7612 }
92d83515
APB
7613
7614 if (current)
7615 return 0;
7616
7617 /* Get rid of <clinit> in the class' list of methods */
7618 if (TYPE_METHODS (type) == mdecl)
7619 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7620 else
7621 for (current = TYPE_METHODS (type); current;
7622 current = TREE_CHAIN (current))
7623 if (TREE_CHAIN (current) == mdecl)
7624 {
7625 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7626 break;
7627 }
7628
7629 return 1;
7630}
7631
7632
e04a16fb
AG
7633/* Complete and expand a method. */
7634
7635static void
7636java_complete_expand_method (mdecl)
7637 tree mdecl;
7638{
92d83515
APB
7639 int yank_clinit = 0;
7640
c2952b01 7641 current_function_decl = mdecl;
22eed1e6
APB
7642 /* Fix constructors before expanding them */
7643 if (DECL_CONSTRUCTOR_P (mdecl))
7644 fix_constructors (mdecl);
e04a16fb 7645
22eed1e6 7646 /* Expand functions that have a body */
e04a16fb
AG
7647 if (DECL_FUNCTION_BODY (mdecl))
7648 {
9bbc7d9f
PB
7649 tree fbody = DECL_FUNCTION_BODY (mdecl);
7650 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7651 tree exception_copy = NULL_TREE;
e04a16fb 7652 expand_start_java_method (mdecl);
939d7216 7653 build_result_decl (mdecl);
e04a16fb
AG
7654
7655 current_this
7656 = (!METHOD_STATIC (mdecl) ?
7657 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7658
ce6e9147
APB
7659 /* Purge the `throws' list of unchecked exceptions. If we're
7660 doing xref, save a copy of the list and re-install it
7661 later. */
7662 if (flag_emit_xref)
7663 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7664
b9f7e36c
APB
7665 purge_unchecked_exceptions (mdecl);
7666
7667 /* Install exceptions thrown with `throws' */
7668 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7669
9bbc7d9f 7670 if (block_body != NULL_TREE)
bc3ca41b
PB
7671 {
7672 block_body = java_complete_tree (block_body);
c2952b01 7673
7145d9fe 7674 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7675 check_for_initialization (block_body);
f099f336 7676 ctxp->explicit_constructor_p = 0;
bc3ca41b 7677 }
e803d3b2 7678
9bbc7d9f 7679 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7680
c2952b01
APB
7681 /* If we saw a return but couldn't evaluate it properly, we'll
7682 have an error_mark_node here. */
7683 if (block_body != error_mark_node
7684 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7685 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7686 && !flag_emit_xref)
82371d41 7687 missing_return_error (current_function_decl);
7525cc04 7688
92d83515
APB
7689 /* Check wether we could just get rid of clinit, now the picture
7690 is complete. */
7691 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7692 complete_start_java_method (mdecl);
7693
e04a16fb 7694 /* Don't go any further if we've found error(s) during the
92d83515
APB
7695 expansion */
7696 if (!java_error_count && !yank_clinit)
e04a16fb 7697 source_end_java_method ();
22eed1e6
APB
7698 else
7699 {
92d83515
APB
7700 if (java_error_count)
7701 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
22eed1e6
APB
7702 poplevel (1, 0, 1);
7703 }
b9f7e36c
APB
7704
7705 /* Pop the exceptions and sanity check */
7706 POP_EXCEPTIONS();
7707 if (currently_caught_type_list)
7708 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7709
7710 if (flag_emit_xref)
7711 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7712 }
7713}
7714
c2952b01
APB
7715\f
7716
7717/* This section of the code deals with accessing enclosing context
7718 fields either directly by using the relevant access to this$<n> or
7719 by invoking an access method crafted for that purpose. */
7720
7721/* Build the necessary access from an inner class to an outer
7722 class. This routine could be optimized to cache previous result
7723 (decl, current_class and returned access). When an access method
7724 needs to be generated, it always takes the form of a read. It might
7725 be later turned into a write by calling outer_field_access_fix. */
7726
7727static tree
7728build_outer_field_access (id, decl)
7729 tree id, decl;
7730{
7731 tree access = NULL_TREE;
7732 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7733
7734 /* If decl's class is the direct outer class of the current_class,
f0f3a777 7735 build the access as `this$<n>.<field>'. Note that we will break
c2952b01
APB
7736 the `private' barrier if we're not emitting bytecodes. */
7737 if (ctx == DECL_CONTEXT (decl)
7738 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7739 {
7740 tree thisn = build_current_thisn (current_class);
7741 access = make_qualified_primary (build_wfl_node (thisn),
7742 id, EXPR_WFL_LINECOL (id));
7743 }
7744 /* Otherwise, generate access methods to outer this and access the
7745 field (either using an access method or by direct access.) */
7746 else
7747 {
7748 int lc = EXPR_WFL_LINECOL (id);
7749
7750 /* Now we chain the required number of calls to the access$0 to
f0f3a777 7751 get a hold to the enclosing instance we need, and then we
c2952b01
APB
7752 build the field access. */
7753 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7754
7755 /* If the field is private and we're generating bytecode, then
7756 we generate an access method */
7757 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7758 {
7759 tree name = build_outer_field_access_methods (decl);
7760 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7761 name, access, NULL_TREE);
7762 }
7763 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7764 Once again we break the `private' access rule from a foreign
7765 class. */
7766 else
7767 access = make_qualified_primary (access, id, lc);
7768 }
7769 return resolve_expression_name (access, NULL);
7770}
7771
7772/* Return a non zero value if NODE describes an outer field inner
7773 access. */
7774
7775static int
7776outer_field_access_p (type, decl)
7777 tree type, decl;
7778{
7779 if (!INNER_CLASS_TYPE_P (type)
7780 || TREE_CODE (decl) != FIELD_DECL
7781 || DECL_CONTEXT (decl) == type)
7782 return 0;
7783
7784 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7785 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7786 {
7787 if (type == DECL_CONTEXT (decl))
7788 return 1;
7789 if (!DECL_CONTEXT (TYPE_NAME (type)))
7790 break;
7791 }
7792
7793 return 0;
7794}
7795
7796/* Return a non zero value if NODE represents an outer field inner
7797 access that was been already expanded. As a side effect, it returns
7798 the name of the field being accessed and the argument passed to the
7799 access function, suitable for a regeneration of the access method
7800 call if necessary. */
7801
7802static int
7803outer_field_expanded_access_p (node, name, arg_type, arg)
7804 tree node, *name, *arg_type, *arg;
7805{
7806 int identified = 0;
7807
7808 if (TREE_CODE (node) != CALL_EXPR)
7809 return 0;
7810
7811 /* Well, gcj generates slightly different tree nodes when compiling
7812 to native or bytecodes. It's the case for function calls. */
7813
7814 if (flag_emit_class_files
7815 && TREE_CODE (node) == CALL_EXPR
7816 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7817 identified = 1;
7818 else if (!flag_emit_class_files)
7819 {
7820 node = TREE_OPERAND (node, 0);
7821
7822 if (node && TREE_OPERAND (node, 0)
7823 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7824 {
7825 node = TREE_OPERAND (node, 0);
7826 if (TREE_OPERAND (node, 0)
7827 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7828 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7829 (DECL_NAME (TREE_OPERAND (node, 0)))))
7830 identified = 1;
7831 }
7832 }
7833
7834 if (identified && name && arg_type && arg)
7835 {
7836 tree argument = TREE_OPERAND (node, 1);
7837 *name = DECL_NAME (TREE_OPERAND (node, 0));
7838 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7839 *arg = TREE_VALUE (argument);
7840 }
7841 return identified;
7842}
7843
7844/* Detect in NODE an outer field read access from an inner class and
7845 transform it into a write with RHS as an argument. This function is
7846 called from the java_complete_lhs when an assignment to a LHS can
7847 be identified. */
7848
7849static tree
7850outer_field_access_fix (wfl, node, rhs)
7851 tree wfl, node, rhs;
7852{
7853 tree name, arg_type, arg;
7854
7855 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7856 {
7857 /* At any rate, check whether we're trying to assign a value to
7858 a final. */
7859 tree accessed = (JDECL_P (node) ? node :
7860 (TREE_CODE (node) == COMPONENT_REF ?
7861 TREE_OPERAND (node, 1) : node));
7862 if (check_final_assignment (accessed, wfl))
7863 return error_mark_node;
7864
7865 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7866 arg_type, name, arg, rhs);
7867 return java_complete_tree (node);
7868 }
7869 return NULL_TREE;
7870}
7871
7872/* Construct the expression that calls an access method:
7873 <type>.access$<n>(<arg1> [, <arg2>]);
7874
7875 ARG2 can be NULL and will be omitted in that case. It will denote a
7876 read access. */
7877
7878static tree
7879build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7880 int lc;
7881 tree type, access_method_name, arg1, arg2;
7882{
7883 tree args, cn, access;
7884
7885 args = arg1 ? arg1 :
7886 build_wfl_node (build_current_thisn (current_class));
7887 args = build_tree_list (NULL_TREE, args);
7888
7889 if (arg2)
7890 args = tree_cons (NULL_TREE, arg2, args);
7891
7892 access = build_method_invocation (build_wfl_node (access_method_name), args);
7893 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7894 return make_qualified_primary (cn, access, lc);
7895}
7896
7897static tree
7898build_new_access_id ()
7899{
7900 static int access_n_counter = 1;
7901 char buffer [128];
7902
7903 sprintf (buffer, "access$%d", access_n_counter++);
7904 return get_identifier (buffer);
7905}
7906
7907/* Create the static access functions for the outer field DECL. We define a
7908 read:
7909 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7910 return inst$.field;
7911 }
7912 and a write access:
7913 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7914 TREE_TYPE (<field>) value$) {
7915 return inst$.field = value$;
7916 }
7917 We should have a usage flags on the DECL so we can lazily turn the ones
7918 we're using for code generation. FIXME.
7919*/
7920
7921static tree
7922build_outer_field_access_methods (decl)
7923 tree decl;
7924{
7925 tree id, args, stmt, mdecl;
7926
7927 /* Check point, to be removed. FIXME */
7928 if (FIELD_INNER_ACCESS (decl)
7929 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7930 abort ();
7931
7932 if (FIELD_INNER_ACCESS (decl))
7933 return FIELD_INNER_ACCESS (decl);
7934
7935 push_obstacks (&permanent_obstack, &permanent_obstack);
7936
7937 /* Create the identifier and a function named after it. */
7938 id = build_new_access_id ();
7939
7940 /* The identifier is marked as bearing the name of a generated write
7941 access function for outer field accessed from inner classes. */
7942 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7943
7944 /* Create the read access */
7945 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7946 TREE_CHAIN (args) = end_params_node;
7947 stmt = make_qualified_primary (build_wfl_node (inst_id),
7948 build_wfl_node (DECL_NAME (decl)), 0);
7949 stmt = build_return (0, stmt);
7950 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7951 TREE_TYPE (decl), id, args, stmt);
7952 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7953
7954 /* Create the write access method */
7955 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7956 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7957 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7958 stmt = make_qualified_primary (build_wfl_node (inst_id),
7959 build_wfl_node (DECL_NAME (decl)), 0);
7960 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7961 build_wfl_node (wpv_id)));
7962
7963 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7964 TREE_TYPE (decl), id, args, stmt);
7965 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7966 pop_obstacks ();
7967
7968 /* Return the access name */
7969 return FIELD_INNER_ACCESS (decl) = id;
7970}
7971
7972/* Build an field access method NAME. */
7973
7974static tree
7975build_outer_field_access_method (class, type, name, args, body)
7976 tree class, type, name, args, body;
7977{
7978 tree saved_current_function_decl, mdecl;
7979
7980 /* Create the method */
7981 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7982 fix_method_argument_names (args, mdecl);
7983 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7984
7985 /* Attach the method body. */
7986 saved_current_function_decl = current_function_decl;
7987 start_artificial_method_body (mdecl);
7988 java_method_add_stmt (mdecl, body);
7989 end_artificial_method_body (mdecl);
7990 current_function_decl = saved_current_function_decl;
7991
7992 return mdecl;
7993}
7994
7995\f
7996/* This section deals with building access function necessary for
7997 certain kinds of method invocation from inner classes. */
7998
7999static tree
8000build_outer_method_access_method (decl)
8001 tree decl;
8002{
8003 tree saved_current_function_decl, mdecl;
8004 tree args = NULL_TREE, call_args = NULL_TREE;
8005 tree carg, id, body, class;
8006 char buffer [80];
8007 int parm_id_count = 0;
8008
8009 /* Test this abort with an access to a private field */
8010 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8011 abort ();
8012
8013 /* Check the cache first */
8014 if (DECL_FUNCTION_INNER_ACCESS (decl))
8015 return DECL_FUNCTION_INNER_ACCESS (decl);
8016
8017 class = DECL_CONTEXT (decl);
8018
8019 /* Obtain an access identifier and mark it */
8020 id = build_new_access_id ();
8021 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8022
8023 push_obstacks (&permanent_obstack, &permanent_obstack);
8024
8025 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8026 /* Create the arguments, as much as the original */
8027 for (; carg && carg != end_params_node;
8028 carg = TREE_CHAIN (carg))
8029 {
8030 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8031 args = chainon (args, build_tree_list (get_identifier (buffer),
8032 TREE_VALUE (carg)));
8033 }
8034 args = chainon (args, end_params_node);
8035
8036 /* Create the method */
8037 mdecl = create_artificial_method (class, ACC_STATIC,
8038 TREE_TYPE (TREE_TYPE (decl)), id, args);
8039 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8040 /* There is a potential bug here. We should be able to use
8041 fix_method_argument_names, but then arg names get mixed up and
8042 eventually a constructor will have its this$0 altered and the
8043 outer context won't be assignment properly. The test case is
8044 stub.java FIXME */
8045 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8046
8047 /* Attach the method body. */
8048 saved_current_function_decl = current_function_decl;
8049 start_artificial_method_body (mdecl);
8050
8051 /* The actual method invocation uses the same args. When invoking a
8052 static methods that way, we don't want to skip the first
8053 argument. */
8054 carg = args;
8055 if (!METHOD_STATIC (decl))
8056 carg = TREE_CHAIN (carg);
8057 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8058 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8059 call_args);
8060
8061 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8062 call_args);
8063 if (!METHOD_STATIC (decl))
8064 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8065 body, 0);
8066 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8067 body = build_return (0, body);
8068 java_method_add_stmt (mdecl,body);
8069 end_artificial_method_body (mdecl);
8070 current_function_decl = saved_current_function_decl;
8071 pop_obstacks ();
8072
8073 /* Back tag the access function so it know what it accesses */
8074 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8075
8076 /* Tag the current method so it knows it has an access generated */
8077 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8078}
8079
8080\f
8081/* This section of the code deals with building expressions to access
8082 the enclosing instance of an inner class. The enclosing instance is
8083 kept in a generated field called this$<n>, with <n> being the
8084 inner class nesting level (starting from 0.) */
8085
8086/* Build an access to a given this$<n>, possibly by chaining access
8087 call to others. Access methods to this$<n> are build on the fly if
8088 necessary */
8089
8090static tree
8091build_access_to_thisn (from, to, lc)
8092 tree from, to;
8093 int lc;
8094{
8095 tree access = NULL_TREE;
8096
8097 while (from != to)
8098 {
8099 tree access0_wfl, cn;
8100
8101 maybe_build_thisn_access_method (from);
8102 access0_wfl = build_wfl_node (access0_identifier_node);
8103 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8104 EXPR_WFL_LINECOL (access0_wfl) = lc;
8105
8106 if (!access)
8107 {
8108 access = build_current_thisn (current_class);
8109 access = build_wfl_node (access);
8110 }
8111 access = build_tree_list (NULL_TREE, access);
8112 access = build_method_invocation (access0_wfl, access);
8113 access = make_qualified_primary (cn, access, lc);
8114
8115 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8116 }
8117 return access;
8118}
8119
8120/* Build an access function to the this$<n> local to TYPE. NULL_TREE
8121 is returned if nothing needs to be generated. Otherwise, the method
152de068 8122 generated and a method decl is returned.
c2952b01
APB
8123
8124 NOTE: These generated methods should be declared in a class file
8125 attribute so that they can't be referred to directly. */
8126
8127static tree
8128maybe_build_thisn_access_method (type)
8129 tree type;
8130{
8131 tree mdecl, args, stmt, rtype;
8132 tree saved_current_function_decl;
8133
8134 /* If TYPE is a top-level class, no access method is required.
8135 If there already is such an access method, bail out. */
8136 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8137 return NULL_TREE;
8138
8139 /* We generate the method. The method looks like:
8140 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8141 */
8142 push_obstacks (&permanent_obstack, &permanent_obstack);
8143 args = build_tree_list (inst_id, build_pointer_type (type));
8144 TREE_CHAIN (args) = end_params_node;
8145 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8146 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8147 access0_identifier_node, args);
8148 fix_method_argument_names (args, mdecl);
8149 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8150 stmt = build_current_thisn (type);
8151 stmt = make_qualified_primary (build_wfl_node (inst_id),
8152 build_wfl_node (stmt), 0);
8153 stmt = build_return (0, stmt);
8154
8155 saved_current_function_decl = current_function_decl;
8156 start_artificial_method_body (mdecl);
8157 java_method_add_stmt (mdecl, stmt);
8158 end_artificial_method_body (mdecl);
8159 current_function_decl = saved_current_function_decl;
8160 pop_obstacks ();
8161
8162 CLASS_ACCESS0_GENERATED_P (type) = 1;
8163
8164 return mdecl;
8165}
8166
8167/* Craft an correctly numbered `this$<n>'string. this$0 is used for
8168 the first level of innerclassing. this$1 for the next one, etc...
8169 This function can be invoked with TYPE to NULL, available and then
8170 has to count the parser context. */
8171
8172static tree
8173build_current_thisn (type)
8174 tree type;
8175{
8176 static int saved_i = -1;
8177 static tree saved_thisn = NULL_TREE;
8178
8179 tree decl;
8180 char buffer [80];
8181 int i = 0;
8182
8183 if (type)
8184 {
8185 static tree saved_type = NULL_TREE;
8186 static int saved_type_i = 0;
8187
8188 if (type == saved_type)
8189 i = saved_type_i;
8190 else
8191 {
8192 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8193 decl; decl = DECL_CONTEXT (decl), i++)
8194 ;
8195
8196 saved_type = type;
8197 saved_type_i = i;
8198 }
8199 }
8200 else
8201 i = list_length (GET_CPC_LIST ())-2;
8202
8203 if (i == saved_i)
8204 return saved_thisn;
8205
8206 sprintf (buffer, "this$%d", i);
8207 saved_i = i;
8208 saved_thisn = get_identifier (buffer);
8209 return saved_thisn;
8210}
8211
8212/* Return the assignement to the hidden enclosing context `this$<n>'
8213 by the second incoming parameter to the innerclass constructor. The
8214 form used is `this.this$<n> = this$<n>;'. */
8215
8216static tree
8217build_thisn_assign ()
8218{
8219 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8220 {
8221 tree thisn = build_current_thisn (current_class);
8222 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8223 build_wfl_node (thisn), 0);
8224 tree rhs = build_wfl_node (thisn);
8225 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8226 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8227 }
8228 return NULL_TREE;
8229}
8230
8231\f
165f37bc
APB
8232/* Building the synthetic `class$' used to implement the `.class' 1.1
8233 extension for non primitive types. This method looks like:
8234
8235 static Class class$(String type) throws NoClassDefFoundError
8236 {
8237 try {return (java.lang.Class.forName (String));}
8238 catch (ClassNotFoundException e) {
8239 throw new NoClassDefFoundError(e.getMessage());}
8240 } */
8241
8242static tree
8243build_dot_class_method (class)
8244 tree class;
8245{
8246#define BWF(S) build_wfl_node (get_identifier ((S)))
8247#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8248 tree args, tmp, saved_current_function_decl, mdecl;
8249 tree stmt, throw_stmt, catch, catch_block, try_block;
8250 tree catch_clause_param;
8251 tree class_not_found_exception, no_class_def_found_error;
8252
8253 static tree get_message_wfl, type_parm_wfl;
8254
8255 if (!get_message_wfl)
8256 {
8257 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8258 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8259 }
8260
8261 /* Build the arguments */
8262 args = build_tree_list (get_identifier ("type$"),
8263 build_pointer_type (string_type_node));
8264 TREE_CHAIN (args) = end_params_node;
8265
8266 /* Build the qualified name java.lang.Class.forName */
8267 tmp = MQN (MQN (MQN (BWF ("java"),
8268 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8269
8270 /* For things we have to catch and throw */
8271 class_not_found_exception =
8272 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8273 no_class_def_found_error =
8274 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8275 load_class (class_not_found_exception, 1);
8276 load_class (no_class_def_found_error, 1);
8277
8278 /* Create the "class$" function */
8279 mdecl = create_artificial_method (class, ACC_STATIC,
8280 build_pointer_type (class_type_node),
8281 get_identifier ("class$"), args);
8282 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8283 no_class_def_found_error);
8284
8285 /* We start by building the try block. We need to build:
8286 return (java.lang.Class.forName (type)); */
8287 stmt = build_method_invocation (tmp,
8288 build_tree_list (NULL_TREE, type_parm_wfl));
8289 stmt = build_return (0, stmt);
8290 /* Put it in a block. That's the try block */
8291 try_block = build_expr_block (stmt, NULL_TREE);
8292
8293 /* Now onto the catch block. We start by building the expression
8294 throwing a new exception:
8295 throw new NoClassDefFoundError (_.getMessage); */
8296 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8297 get_message_wfl, 0);
8298 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8299
8300 /* Build new NoClassDefFoundError (_.getMessage) */
8301 throw_stmt = build_new_invocation
8302 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8303 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8304
8305 /* Build the throw, (it's too early to use BUILD_THROW) */
8306 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8307
8308 /* Build the catch block to encapsulate all this. We begin by
8309 building an decl for the catch clause parameter and link it to
8310 newly created block, the catch block. */
8311 catch_clause_param =
8312 build_decl (VAR_DECL, wpv_id,
8313 build_pointer_type (class_not_found_exception));
8314 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8315
8316 /* We initialize the variable with the exception handler. */
8317 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8318 soft_exceptioninfo_call_node);
8319 add_stmt_to_block (catch_block, NULL_TREE, catch);
8320
8321 /* We add the statement throwing the new exception */
8322 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8323
8324 /* Build a catch expression for all this */
8325 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8326
8327 /* Build the try/catch sequence */
8328 stmt = build_try_statement (0, try_block, catch_block);
8329
8330 fix_method_argument_names (args, mdecl);
8331 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8332 saved_current_function_decl = current_function_decl;
8333 start_artificial_method_body (mdecl);
8334 java_method_add_stmt (mdecl, stmt);
8335 end_artificial_method_body (mdecl);
8336 current_function_decl = saved_current_function_decl;
8337 TYPE_DOT_CLASS (class) = mdecl;
8338
8339 return mdecl;
8340}
8341
8342static tree
f0f3a777
APB
8343build_dot_class_method_invocation (type)
8344 tree type;
165f37bc 8345{
f0f3a777
APB
8346 tree sig_id, s;
8347
8348 if (TYPE_ARRAY_P (type))
8349 sig_id = build_java_signature (type);
8350 else
8351 sig_id = DECL_NAME (TYPE_NAME (type));
8352
8353 s = make_node (STRING_CST);
8354 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (sig_id);
165f37bc
APB
8355 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8356 TREE_STRING_LENGTH (s)+1);
f0f3a777 8357 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (sig_id));
165f37bc
APB
8358 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8359 build_tree_list (NULL_TREE, s));
8360}
8361
c2952b01
APB
8362/* This section of the code deals with constructor. */
8363
22eed1e6
APB
8364/* Craft a body for default constructor. Patch existing constructor
8365 bodies with call to super() and field initialization statements if
8366 necessary. */
8367
8368static void
8369fix_constructors (mdecl)
8370 tree mdecl;
8371{
8372 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8373 tree thisn_assign, compound = NULL_TREE;
8374 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8375
22eed1e6
APB
8376 if (!body)
8377 {
22eed1e6
APB
8378 /* It is an error for the compiler to generate a default
8379 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8380 takes no argument, or the same args for an anonymous class */
8381 if (verify_constructor_super (mdecl))
22eed1e6 8382 {
c2952b01
APB
8383 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8384 tree save = DECL_NAME (mdecl);
49f48c71 8385 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8386 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8387 parse_error_context
c2952b01
APB
8388 (lookup_cl (TYPE_NAME (class_type)),
8389 "No constructor matching `%s' found in class `%s'",
8390 lang_printable_name (mdecl, 0), n);
8391 DECL_NAME (mdecl) = save;
22eed1e6
APB
8392 }
8393
c2952b01
APB
8394 /* The constructor body must be crafted by hand. It's the
8395 constructor we defined when we realize we didn't have the
8396 CLASSNAME() constructor */
22eed1e6
APB
8397 start_artificial_method_body (mdecl);
8398
f0f3a777
APB
8399 /* Insert an assignment to the this$<n> hidden field, if
8400 necessary */
8401 if ((thisn_assign = build_thisn_assign ()))
8402 java_method_add_stmt (mdecl, thisn_assign);
8403
22eed1e6
APB
8404 /* We don't generate a super constructor invocation if we're
8405 compiling java.lang.Object. build_super_invocation takes care
8406 of that. */
e920ebc9 8407 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8408
c2952b01
APB
8409 /* Insert the instance initializer block right here, after the
8410 super invocation. */
8411 add_instance_initializer (mdecl);
8412
22eed1e6
APB
8413 end_artificial_method_body (mdecl);
8414 }
8415 /* Search for an explicit constructor invocation */
8416 else
8417 {
8418 int found = 0;
8419 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8420
8421 while (body)
8422 switch (TREE_CODE (body))
8423 {
8424 case CALL_EXPR:
8425 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8426 body = NULL_TREE;
8427 break;
8428 case COMPOUND_EXPR:
8429 case EXPR_WITH_FILE_LOCATION:
8430 body = TREE_OPERAND (body, 0);
8431 break;
8432 case BLOCK:
8433 body = BLOCK_EXPR_BODY (body);
8434 break;
8435 default:
8436 found = 0;
8437 body = NULL_TREE;
8438 }
8439 /* The constructor is missing an invocation of super() */
8440 if (!found)
8441 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8442 build_super_invocation (mdecl));
22eed1e6 8443
c2952b01
APB
8444 /* Generate the assignment to this$<n>, if necessary */
8445 if ((thisn_assign = build_thisn_assign ()))
8446 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8447
f0f3a777
APB
8448 /* Insert the instance initializer block right here, after the
8449 super invocation. */
8450 add_instance_initializer (mdecl);
8451
22eed1e6
APB
8452 /* Fix the constructor main block if we're adding extra stmts */
8453 if (compound)
8454 {
8455 compound = add_stmt_to_compound (compound, NULL_TREE,
8456 BLOCK_EXPR_BODY (main_block));
8457 BLOCK_EXPR_BODY (main_block) = compound;
8458 }
8459 }
8460}
8461
8462/* Browse constructors in the super class, searching for a constructor
8463 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8464 otherwise. If the current class is an anonymous inner class, look
8465 for something that has the same signature. */
22eed1e6
APB
8466
8467static int
c2952b01
APB
8468verify_constructor_super (mdecl)
8469 tree mdecl;
22eed1e6
APB
8470{
8471 tree class = CLASSTYPE_SUPER (current_class);
152de068 8472 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
c2952b01
APB
8473 tree sdecl;
8474
22eed1e6
APB
8475 if (!class)
8476 return 0;
8477
c2952b01 8478 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8479 {
c2952b01
APB
8480 tree mdecl_arg_type;
8481 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8482 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8483 if (DECL_CONSTRUCTOR_P (sdecl))
8484 {
cf1b2274 8485 tree m_arg_type;
152de068
APB
8486 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8487 if (super_inner)
8488 arg_type = TREE_CHAIN (arg_type);
cf1b2274
APB
8489 for (m_arg_type = mdecl_arg_type;
8490 (arg_type != end_params_node
8491 && m_arg_type != end_params_node);
c2952b01 8492 arg_type = TREE_CHAIN (arg_type),
cf1b2274
APB
8493 m_arg_type = TREE_CHAIN (m_arg_type))
8494 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
c2952b01
APB
8495 break;
8496
cf1b2274 8497 if (arg_type == end_params_node && m_arg_type == end_params_node)
c2952b01
APB
8498 return 0;
8499 }
8500 }
8501 else
8502 {
8503 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8504 {
152de068
APB
8505 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8506 if (super_inner)
8507 arg = TREE_CHAIN (arg);
8508 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
22eed1e6
APB
8509 return 0;
8510 }
8511 }
8512 return 1;
8513}
8514
22eed1e6 8515/* Generate code for all context remembered for code generation. */
b351b287
APB
8516
8517void
8518java_expand_classes ()
8519{
5423609c 8520 int save_error_count = 0;
c2952b01
APB
8521 static struct parser_ctxt *saved_ctxp = NULL;
8522
23a79c61
APB
8523 java_parse_abort_on_error ();
8524 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8525 return;
8526 java_layout_classes ();
8527 java_parse_abort_on_error ();
8528
c2952b01 8529 saved_ctxp = ctxp_for_generation;
b351b287
APB
8530 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8531 {
8532 ctxp = ctxp_for_generation;
8533 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8534 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8535 java_parse_abort_on_error ();
8536 }
c2952b01
APB
8537
8538 /* Find anonymous classes and expand their constructor, now they
8539 have been fixed. */
8540 for (ctxp_for_generation = saved_ctxp;
8541 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8542 {
8543 tree current;
8544 ctxp = ctxp_for_generation;
8545 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8546 {
8547 current_class = TREE_TYPE (current);
8548 if (ANONYMOUS_CLASS_P (current_class))
8549 {
8550 tree d;
8551 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8552 {
8553 if (DECL_CONSTRUCTOR_P (d))
8554 {
8555 restore_line_number_status (1);
8556 reset_method_name (d);
8557 java_complete_expand_method (d);
8558 restore_line_number_status (0);
8559 break; /* We now there are no other ones */
8560 }
8561 }
8562 }
8563 }
8564 }
8565
8566 /* If we've found error at that stage, don't try to generate
8567 anything, unless we're emitting xrefs or checking the syntax only
8568 (but not using -fsyntax-only for the purpose of generating
8569 bytecode. */
8570 if (java_error_count && !flag_emit_xref
8571 && (!flag_syntax_only && !flag_emit_class_files))
8572 return;
8573
8574 /* Now things are stable, go for generation of the class data. */
8575 for (ctxp_for_generation = saved_ctxp;
8576 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8577 {
8578 tree current;
8579 ctxp = ctxp_for_generation;
8580 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8581 {
8582 current_class = TREE_TYPE (current);
8583 outgoing_cpool = TYPE_CPOOL (current_class);
8584 if (flag_emit_class_files)
8585 write_classfile (current_class);
8586 if (flag_emit_xref)
8587 expand_xref (current_class);
8588 else if (! flag_syntax_only)
8589 finish_class ();
8590 }
8591 }
b351b287
APB
8592}
8593
e04a16fb
AG
8594/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8595 a tree list node containing RIGHT. Fore coming RIGHTs will be
8596 chained to this hook. LOCATION contains the location of the
8597 separating `.' operator. */
8598
8599static tree
8600make_qualified_primary (primary, right, location)
8601 tree primary, right;
8602 int location;
8603{
8604 tree wfl;
8605
c2952b01 8606 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9a7ab4b3 8607 wfl = build_wfl_wrap (primary, location);
e04a16fb
AG
8608 else
8609 {
8610 wfl = primary;
c2952b01
APB
8611 /* If wfl wasn't qualified, we build a first anchor */
8612 if (!EXPR_WFL_QUALIFICATION (wfl))
8613 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8614 }
8615
c2952b01 8616 /* And chain them */
e04a16fb
AG
8617 EXPR_WFL_LINECOL (right) = location;
8618 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8619 PRIMARY_P (wfl) = 1;
8620 return wfl;
8621}
8622
8623/* Simple merge of two name separated by a `.' */
8624
8625static tree
8626merge_qualified_name (left, right)
8627 tree left, right;
8628{
8629 tree node;
c2952b01
APB
8630 if (!left && !right)
8631 return NULL_TREE;
8632
8633 if (!left)
8634 return right;
8635
8636 if (!right)
8637 return left;
8638
e04a16fb
AG
8639 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8640 IDENTIFIER_LENGTH (left));
8641 obstack_1grow (&temporary_obstack, '.');
8642 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8643 IDENTIFIER_LENGTH (right));
8644 node = get_identifier (obstack_base (&temporary_obstack));
8645 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8646 QUALIFIED_P (node) = 1;
8647 return node;
8648}
8649
8650/* Merge the two parts of a qualified name into LEFT. Set the
8651 location information of the resulting node to LOCATION, usually
8652 inherited from the location information of the `.' operator. */
8653
8654static tree
8655make_qualified_name (left, right, location)
8656 tree left, right;
8657 int location;
8658{
bc3ca41b
PB
8659#ifdef USE_COMPONENT_REF
8660 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8661 EXPR_WFL_LINECOL (node) = location;
8662 return node;
8663#else
e04a16fb
AG
8664 tree left_id = EXPR_WFL_NODE (left);
8665 tree right_id = EXPR_WFL_NODE (right);
8666 tree wfl, merge;
8667
8668 merge = merge_qualified_name (left_id, right_id);
8669
8670 /* Left wasn't qualified and is now qualified */
8671 if (!QUALIFIED_P (left_id))
8672 {
8673 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8674 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8675 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8676 }
8677
8678 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8679 EXPR_WFL_LINECOL (wfl) = location;
8680 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8681
8682 EXPR_WFL_NODE (left) = merge;
8683 return left;
bc3ca41b 8684#endif
e04a16fb
AG
8685}
8686
8687/* Extract the last identifier component of the qualified in WFL. The
8688 last identifier is removed from the linked list */
8689
8690static tree
8691cut_identifier_in_qualified (wfl)
8692 tree wfl;
8693{
8694 tree q;
8695 tree previous = NULL_TREE;
8696 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8697 if (!TREE_CHAIN (q))
8698 {
8699 if (!previous)
781b0558 8700 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8701 TREE_CHAIN (previous) = NULL_TREE;
8702 return TREE_PURPOSE (q);
8703 }
8704}
8705
8706/* Resolve the expression name NAME. Return its decl. */
8707
8708static tree
5e942c50 8709resolve_expression_name (id, orig)
e04a16fb 8710 tree id;
5e942c50 8711 tree *orig;
e04a16fb
AG
8712{
8713 tree name = EXPR_WFL_NODE (id);
8714 tree decl;
8715
8716 /* 6.5.5.1: Simple expression names */
8717 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8718 {
8719 /* 15.13.1: NAME can appear within the scope of a local variable
8720 declaration */
8721 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8722 return decl;
8723
8724 /* 15.13.1: NAME can appear within a class declaration */
8725 else
8726 {
8727 decl = lookup_field_wrapper (current_class, name);
8728 if (decl)
8729 {
c2952b01 8730 tree access = NULL_TREE;
e04a16fb 8731 int fs = FIELD_STATIC (decl);
f2760b27
APB
8732
8733 /* If we're accessing an outer scope local alias, make
8734 sure we change the name of the field we're going to
8735 build access to. */
8736 if (FIELD_LOCAL_ALIAS_USED (decl))
8737 name = DECL_NAME (decl);
8738
e04a16fb
AG
8739 /* Instance variable (8.3.1.1) can't appear within
8740 static method, static initializer or initializer for
8741 a static variable. */
8742 if (!fs && METHOD_STATIC (current_function_decl))
8743 {
7f10c2e2 8744 static_ref_err (id, name, current_class);
e04a16fb
AG
8745 return error_mark_node;
8746 }
22eed1e6
APB
8747 /* Instance variables can't appear as an argument of
8748 an explicit constructor invocation */
8749 if (!fs && ctxp->explicit_constructor_p)
8750 {
8751 parse_error_context
781b0558 8752 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8753 return error_mark_node;
8754 }
5e942c50 8755
c2952b01
APB
8756 /* If we're processing an inner class and we're trying
8757 to access a field belonging to an outer class, build
8758 the access to the field */
8759 if (!fs && outer_field_access_p (current_class, decl))
8760 return build_outer_field_access (id, decl);
8761
5e942c50 8762 /* Otherwise build what it takes to access the field */
c2952b01
APB
8763 access = build_field_ref ((fs ? NULL_TREE : current_this),
8764 DECL_CONTEXT (decl), name);
e8fc7396 8765 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8766 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8767 /* We may be asked to save the real field access node */
8768 if (orig)
c2952b01 8769 *orig = access;
5e942c50 8770 /* And we return what we got */
c2952b01 8771 return access;
e04a16fb
AG
8772 }
8773 /* Fall down to error report on undefined variable */
8774 }
8775 }
8776 /* 6.5.5.2 Qualified Expression Names */
8777 else
8778 {
5e942c50
APB
8779 if (orig)
8780 *orig = NULL_TREE;
e04a16fb
AG
8781 qualify_ambiguous_name (id);
8782 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8783 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8784 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8785 }
8786
8787 /* We've got an error here */
8788 parse_error_context (id, "Undefined variable `%s'",
8789 IDENTIFIER_POINTER (name));
8790
8791 return error_mark_node;
8792}
8793
7f10c2e2
APB
8794static void
8795static_ref_err (wfl, field_id, class_type)
8796 tree wfl, field_id, class_type;
8797{
8798 parse_error_context
8799 (wfl,
8800 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8801 IDENTIFIER_POINTER (field_id),
8802 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8803}
8804
e04a16fb
AG
8805/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8806 We return something suitable to generate the field access. We also
8807 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8808 recipient's address can be null. */
8809
8810static tree
8811resolve_field_access (qual_wfl, field_decl, field_type)
8812 tree qual_wfl;
8813 tree *field_decl, *field_type;
8814{
8815 int is_static = 0;
8816 tree field_ref;
8817 tree decl, where_found, type_found;
8818
8819 if (resolve_qualified_expression_name (qual_wfl, &decl,
8820 &where_found, &type_found))
8821 return error_mark_node;
8822
8823 /* Resolve the LENGTH field of an array here */
9a7ab4b3 8824 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
6eaeeb55 8825 && type_found && TYPE_ARRAY_P (type_found)
e8fc7396 8826 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8827 {
8828 tree length = build_java_array_length_access (where_found);
8829 field_ref =
8830 build_java_arraynull_check (type_found, length, int_type_node);
611a4b87
APB
8831
8832 /* In case we're dealing with a static array, we need to
8833 initialize its class before the array length can be fetched.
8834 It's also a good time to create a DECL_RTL for the field if
8835 none already exists, otherwise if the field was declared in a
8836 class found in an external file and hasn't been (and won't
8837 be) accessed for its value, none will be created. */
8838 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8839 {
8840 build_static_field_ref (where_found);
8841 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8842 }
e04a16fb
AG
8843 }
8844 /* We might have been trying to resolve field.method(). In which
8845 case, the resolution is over and decl is the answer */
34f4db93 8846 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8847 field_ref = decl;
34f4db93 8848 else if (JDECL_P (decl))
e04a16fb 8849 {
5e942c50
APB
8850 int static_final_found = 0;
8851 if (!type_found)
8852 type_found = DECL_CONTEXT (decl);
34f4db93 8853 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
0c2b8145 8854 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
5e942c50 8855 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
7525cc04 8856 && DECL_INITIAL (decl))
5e942c50 8857 {
0c2b8145
APB
8858 /* When called on a FIELD_DECL of the right (primitive)
8859 type, java_complete_tree will try to substitue the decl
8860 for it's initial value. */
70541f45 8861 field_ref = java_complete_tree (decl);
5e942c50
APB
8862 static_final_found = 1;
8863 }
8864 else
7f10c2e2
APB
8865 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8866 NULL_TREE : where_found),
5e942c50 8867 type_found, DECL_NAME (decl));
e04a16fb
AG
8868 if (field_ref == error_mark_node)
8869 return error_mark_node;
e8fc7396
APB
8870 if (is_static && !static_final_found
8871 && !flag_emit_class_files && !flag_emit_xref)
40aaba2b 8872 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
e04a16fb
AG
8873 }
8874 else
8875 field_ref = decl;
8876
8877 if (field_decl)
8878 *field_decl = decl;
8879 if (field_type)
c877974e
APB
8880 *field_type = (QUAL_DECL_TYPE (decl) ?
8881 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8882 return field_ref;
8883}
8884
e28cd97b
APB
8885/* If NODE is an access to f static field, strip out the class
8886 initialization part and return the field decl, otherwise, return
8887 NODE. */
8888
8889static tree
8890strip_out_static_field_access_decl (node)
8891 tree node;
8892{
8893 if (TREE_CODE (node) == COMPOUND_EXPR)
8894 {
8895 tree op1 = TREE_OPERAND (node, 1);
8896 if (TREE_CODE (op1) == COMPOUND_EXPR)
8897 {
8898 tree call = TREE_OPERAND (op1, 0);
8899 if (TREE_CODE (call) == CALL_EXPR
8900 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8901 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8902 == soft_initclass_node)
8903 return TREE_OPERAND (op1, 1);
8904 }
2f11d407
TT
8905 else if (JDECL_P (op1))
8906 return op1;
e28cd97b
APB
8907 }
8908 return node;
8909}
8910
e04a16fb
AG
8911/* 6.5.5.2: Qualified Expression Names */
8912
8913static int
8914resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8915 tree wfl;
8916 tree *found_decl, *type_found, *where_found;
8917{
8918 int from_type = 0; /* Field search initiated from a type */
c2952b01 8919 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8920 int previous_call_static = 0;
8921 int is_static;
8922 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8923 /* For certain for of inner class instantiation */
8924 tree saved_current, saved_this;
8925#define RESTORE_THIS_AND_CURRENT_CLASS \
8926 { current_class = saved_current; current_this = saved_this;}
8927
c877974e 8928 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8929
8930 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8931 {
8932 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8933 tree ret_decl; /* for EH checking */
8934 int location; /* for EH checking */
e04a16fb
AG
8935
8936 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8937 switch (TREE_CODE (qual_wfl))
8938 {
8939 case CALL_EXPR:
b67d701b 8940 case NEW_CLASS_EXPR:
e04a16fb
AG
8941 /* If the access to the function call is a non static field,
8942 build the code to access it. */
34f4db93 8943 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8944 {
ac825856
APB
8945 decl = maybe_access_field (decl, *where_found,
8946 DECL_CONTEXT (decl));
e04a16fb
AG
8947 if (decl == error_mark_node)
8948 return 1;
8949 }
c2952b01 8950
e04a16fb
AG
8951 /* And code for the function call */
8952 if (complete_function_arguments (qual_wfl))
8953 return 1;
c2952b01
APB
8954
8955 /* We might have to setup a new current class and a new this
8956 for the search of an inner class, relative to the type of
8957 a expression resolved as `decl'. The current values are
8958 saved and restored shortly after */
8959 saved_current = current_class;
8960 saved_this = current_this;
8961 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8962 {
8963 current_class = type;
8964 current_this = decl;
8965 }
8966
89e09b9a
PB
8967 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8968 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8969 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8970 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8971 *where_found = patch_method_invocation (qual_wfl, decl, type,
8972 &is_static, &ret_decl);
e04a16fb 8973 if (*where_found == error_mark_node)
c2952b01
APB
8974 {
8975 RESTORE_THIS_AND_CURRENT_CLASS;
8976 return 1;
8977 }
e04a16fb
AG
8978 *type_found = type = QUAL_DECL_TYPE (*where_found);
8979
c2952b01
APB
8980 /* If we're creating an inner class instance, check for that
8981 an enclosing instance is in scope */
8982 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8983 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8984 {
8985 parse_error_context
165f37bc
APB
8986 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8987 lang_printable_name (type, 0),
8988 (!current_this ? "" :
8989 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8990 RESTORE_THIS_AND_CURRENT_CLASS;
8991 return 1;
8992 }
8993
8994 /* In case we had to change then to resolve a inner class
8995 instantiation using a primary qualified by a `new' */
8996 RESTORE_THIS_AND_CURRENT_CLASS;
8997
34d4df06
APB
8998 /* EH check. No check on access$<n> functions */
8999 if (location
9000 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9001 (DECL_NAME (current_function_decl)))
7705e9db
APB
9002 check_thrown_exceptions (location, ret_decl);
9003
e04a16fb
AG
9004 /* If the previous call was static and this one is too,
9005 build a compound expression to hold the two (because in
9006 that case, previous function calls aren't transported as
9007 forcoming function's argument. */
9008 if (previous_call_static && is_static)
9009 {
9010 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9011 TREE_SIDE_EFFECTS (decl) = 1;
9012 }
9013 else
9014 {
9015 previous_call_static = is_static;
9016 decl = *where_found;
9017 }
c2952b01 9018 from_type = 0;
e04a16fb
AG
9019 continue;
9020
d8fccff5 9021 case NEW_ARRAY_EXPR:
c2952b01 9022 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
9023 *where_found = decl = java_complete_tree (qual_wfl);
9024 if (decl == error_mark_node)
9025 return 1;
9026 *type_found = type = QUAL_DECL_TYPE (decl);
9027 CLASS_LOADED_P (type) = 1;
9028 continue;
9029
e04a16fb
AG
9030 case CONVERT_EXPR:
9031 *where_found = decl = java_complete_tree (qual_wfl);
9032 if (decl == error_mark_node)
9033 return 1;
9034 *type_found = type = QUAL_DECL_TYPE (decl);
9035 from_cast = 1;
9036 continue;
9037
22eed1e6 9038 case CONDITIONAL_EXPR:
5e942c50 9039 case STRING_CST:
ac22f9cb 9040 case MODIFY_EXPR:
22eed1e6
APB
9041 *where_found = decl = java_complete_tree (qual_wfl);
9042 if (decl == error_mark_node)
9043 return 1;
9044 *type_found = type = QUAL_DECL_TYPE (decl);
9045 continue;
9046
e04a16fb
AG
9047 case ARRAY_REF:
9048 /* If the access to the function call is a non static field,
9049 build the code to access it. */
34f4db93 9050 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
9051 {
9052 decl = maybe_access_field (decl, *where_found, type);
9053 if (decl == error_mark_node)
9054 return 1;
9055 }
9056 /* And code for the array reference expression */
9057 decl = java_complete_tree (qual_wfl);
9058 if (decl == error_mark_node)
9059 return 1;
9060 type = QUAL_DECL_TYPE (decl);
9061 continue;
0a2138e2 9062
37feda7d
APB
9063 case PLUS_EXPR:
9064 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9065 return 1;
9066 if ((type = patch_string (decl)))
9067 decl = type;
9068 *where_found = QUAL_RESOLUTION (q) = decl;
9069 *type_found = type = TREE_TYPE (decl);
9070 break;
9071
165f37bc
APB
9072 case CLASS_LITERAL:
9073 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9074 return 1;
9075 *where_found = QUAL_RESOLUTION (q) = decl;
9076 *type_found = type = TREE_TYPE (decl);
9077 break;
9078
0a2138e2
APB
9079 default:
9080 /* Fix for -Wall Just go to the next statement. Don't
9081 continue */
a3f406ce 9082 break;
e04a16fb
AG
9083 }
9084
9085 /* If we fall here, we weren't processing a (static) function call. */
9086 previous_call_static = 0;
9087
9088 /* It can be the keyword THIS */
9089 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9090 {
9091 if (!current_this)
9092 {
9093 parse_error_context
9094 (wfl, "Keyword `this' used outside allowed context");
9095 return 1;
9096 }
f63991a8
APB
9097 if (ctxp->explicit_constructor_p)
9098 {
781b0558 9099 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
9100 return 1;
9101 }
e04a16fb 9102 /* We have to generate code for intermediate acess */
c2952b01
APB
9103 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9104 {
9105 *where_found = decl = current_this;
9106 *type_found = type = QUAL_DECL_TYPE (decl);
9107 }
4dbf4496
APB
9108 /* We're trying to access the this from somewhere else. Make sure
9109 it's allowed before doing so. */
c2952b01
APB
9110 else
9111 {
4dbf4496
APB
9112 if (!enclosing_context_p (type, current_class))
9113 {
9114 char *p = xstrdup (lang_printable_name (type, 0));
9115 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9116 p, p,
9117 lang_printable_name (current_class, 0));
9118 free (p);
9119 return 1;
9120 }
c2952b01
APB
9121 *where_found = decl = build_current_thisn (type);
9122 from_qualified_this = 1;
9123 }
9124
9125 from_type = 0;
e04a16fb
AG
9126 continue;
9127 }
9128
9129 /* 15.10.2 Accessing Superclass Members using SUPER */
9130 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9131 {
9132 tree node;
9133 /* Check on the restricted use of SUPER */
9134 if (METHOD_STATIC (current_function_decl)
9135 || current_class == object_type_node)
9136 {
9137 parse_error_context
9138 (wfl, "Keyword `super' used outside allowed context");
9139 return 1;
9140 }
9141 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9142 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9143 CLASSTYPE_SUPER (current_class),
9144 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9145 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
9146 if (decl == error_mark_node)
9147 return 1;
e04a16fb
AG
9148 *type_found = type = QUAL_DECL_TYPE (decl);
9149 from_super = from_type = 1;
9150 continue;
9151 }
9152
9153 /* 15.13.1: Can't search for field name in packages, so we
9154 assume a variable/class name was meant. */
9155 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9156 {
5e942c50
APB
9157 tree name = resolve_package (wfl, &q);
9158 if (name)
9159 {
c2952b01 9160 tree list;
5e942c50 9161 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 9162 /* We want to be absolutely sure that the class is laid
5e942c50
APB
9163 out. We're going to search something inside it. */
9164 *type_found = type = TREE_TYPE (decl);
9165 layout_class (type);
9166 from_type = 1;
c2952b01 9167
dde1da72
APB
9168 /* Fix them all the way down, if any are left. */
9169 if (q)
c2952b01 9170 {
dde1da72
APB
9171 list = TREE_CHAIN (q);
9172 while (list)
9173 {
9174 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9175 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9176 list = TREE_CHAIN (list);
9177 }
c2952b01 9178 }
5e942c50 9179 }
e04a16fb 9180 else
5e942c50
APB
9181 {
9182 if (from_super || from_cast)
9183 parse_error_context
9184 ((from_cast ? qual_wfl : wfl),
9185 "No variable `%s' defined in class `%s'",
9186 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9187 lang_printable_name (type, 0));
9188 else
9189 parse_error_context
9190 (qual_wfl, "Undefined variable or class name: `%s'",
9191 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9192 return 1;
9193 }
e04a16fb
AG
9194 }
9195
9196 /* We have a type name. It's been already resolved when the
9197 expression was qualified. */
9198 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9199 {
9200 if (!(decl = QUAL_RESOLUTION (q)))
9201 return 1; /* Error reported already */
9202
c2952b01
APB
9203 /* Sneak preview. If next we see a `new', we're facing a
9204 qualification with resulted in a type being selected
9205 instead of a field. Report the error */
9206 if(TREE_CHAIN (q)
9207 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9208 {
9209 parse_error_context (qual_wfl, "Undefined variable `%s'",
9210 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9211 return 1;
9212 }
9213
e04a16fb
AG
9214 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9215 {
9216 parse_error_context
9217 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9218 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 9219 GET_TYPE_NAME (type),
e04a16fb
AG
9220 IDENTIFIER_POINTER (DECL_NAME (decl)),
9221 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9222 return 1;
9223 }
5e942c50 9224 check_deprecation (qual_wfl, decl);
c2952b01 9225
e04a16fb
AG
9226 type = TREE_TYPE (decl);
9227 from_type = 1;
9228 }
9229 /* We resolve and expression name */
9230 else
9231 {
cd531a2e 9232 tree field_decl = NULL_TREE;
e04a16fb
AG
9233
9234 /* If there exists an early resolution, use it. That occurs
9235 only once and we know that there are more things to
9236 come. Don't do that when processing something after SUPER
9237 (we need more thing to be put in place below */
9238 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
9239 {
9240 decl = QUAL_RESOLUTION (q);
c877974e 9241 if (!type)
5e942c50 9242 {
7f10c2e2
APB
9243 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9244 {
9245 if (current_this)
9246 *where_found = current_this;
9247 else
9248 {
9249 static_ref_err (qual_wfl, DECL_NAME (decl),
9250 current_class);
9251 return 1;
9252 }
f0f3a777
APB
9253 if (outer_field_access_p (current_class, decl))
9254 decl = build_outer_field_access (qual_wfl, decl);
7f10c2e2 9255 }
c877974e
APB
9256 else
9257 {
9258 *where_found = TREE_TYPE (decl);
9259 if (TREE_CODE (*where_found) == POINTER_TYPE)
9260 *where_found = TREE_TYPE (*where_found);
9261 }
5e942c50 9262 }
b67d701b 9263 }
e04a16fb
AG
9264
9265 /* We have to search for a field, knowing the type of its
9266 container. The flag FROM_TYPE indicates that we resolved
9267 the last member of the expression as a type name, which
5e942c50
APB
9268 means that for the resolution of this field, we'll look
9269 for other errors than if it was resolved as a member of
9270 an other field. */
e04a16fb
AG
9271 else
9272 {
9273 int is_static;
5e942c50
APB
9274 tree field_decl_type; /* For layout */
9275
e04a16fb
AG
9276 if (!from_type && !JREFERENCE_TYPE_P (type))
9277 {
9278 parse_error_context
9279 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9280 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9281 lang_printable_name (type, 0),
e04a16fb
AG
9282 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9283 return 1;
9284 }
9285
dc0b3eff
PB
9286 field_decl = lookup_field_wrapper (type,
9287 EXPR_WFL_NODE (qual_wfl));
4dbf4496
APB
9288
9289 /* Maybe what we're trying to access an inner class. */
9290 if (!field_decl)
9291 {
9292 tree ptr, inner_decl;
9293
9294 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9295 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9296 if (inner_decl)
9297 {
9298 check_inner_class_access (inner_decl, decl, qual_wfl);
9299 type = TREE_TYPE (inner_decl);
9300 decl = inner_decl;
9301 from_type = 1;
9302 continue;
9303 }
9304 }
9305
dc0b3eff 9306 if (field_decl == NULL_TREE)
e04a16fb
AG
9307 {
9308 parse_error_context
2aa11e97 9309 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9310 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9311 GET_TYPE_NAME (type));
e04a16fb
AG
9312 return 1;
9313 }
dc0b3eff
PB
9314 if (field_decl == error_mark_node)
9315 return 1;
5e942c50
APB
9316
9317 /* Layout the type of field_decl, since we may need
c877974e
APB
9318 it. Don't do primitive types or loaded classes. The
9319 situation of non primitive arrays may not handled
9320 properly here. FIXME */
5e942c50
APB
9321 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9322 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9323 else
9324 field_decl_type = TREE_TYPE (field_decl);
9325 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9326 && !CLASS_LOADED_P (field_decl_type)
9327 && !TYPE_ARRAY_P (field_decl_type))
9328 resolve_and_layout (field_decl_type, NULL_TREE);
9329 if (TYPE_ARRAY_P (field_decl_type))
9330 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9331
9332 /* Check on accessibility here */
9333 if (not_accessible_p (type, field_decl, from_super))
9334 {
9335 parse_error_context
9336 (qual_wfl,
9337 "Can't access %s field `%s.%s' from `%s'",
9338 java_accstring_lookup
9339 (get_access_flags_from_decl (field_decl)),
2aa11e97 9340 GET_TYPE_NAME (type),
e04a16fb
AG
9341 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9342 IDENTIFIER_POINTER
9343 (DECL_NAME (TYPE_NAME (current_class))));
9344 return 1;
9345 }
5e942c50 9346 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9347
9348 /* There are things to check when fields are accessed
9349 from type. There are no restrictions on a static
9350 declaration of the field when it is accessed from an
9351 interface */
9352 is_static = FIELD_STATIC (field_decl);
9353 if (!from_super && from_type
c2952b01
APB
9354 && !TYPE_INTERFACE_P (type)
9355 && !is_static
9356 && (current_function_decl
9357 && METHOD_STATIC (current_function_decl)))
e04a16fb 9358 {
7f10c2e2 9359 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9360 return 1;
9361 }
9362 from_cast = from_super = 0;
9363
c2952b01
APB
9364 /* It's an access from a type but it isn't static, we
9365 make it relative to `this'. */
9366 if (!is_static && from_type)
9367 decl = current_this;
9368
5e942c50
APB
9369 /* If we need to generate something to get a proper
9370 handle on what this field is accessed from, do it
9371 now. */
e04a16fb
AG
9372 if (!is_static)
9373 {
c583dd46 9374 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9375 if (decl == error_mark_node)
9376 return 1;
9377 }
9378
9379 /* We want to keep the location were found it, and the type
9380 we found. */
9381 *where_found = decl;
9382 *type_found = type;
9383
c2952b01
APB
9384 /* Generate the correct expression for field access from
9385 qualified this */
9386 if (from_qualified_this)
9387 {
9388 field_decl = build_outer_field_access (qual_wfl, field_decl);
9389 from_qualified_this = 0;
9390 }
9391
e04a16fb
AG
9392 /* This is the decl found and eventually the next one to
9393 search from */
9394 decl = field_decl;
9395 }
e04a16fb
AG
9396 from_type = 0;
9397 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9398
9399 /* Sneak preview. If decl is qualified by a `new', report
9400 the error here to be accurate on the peculiar construct */
9401 if (TREE_CHAIN (q)
9402 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9403 && !JREFERENCE_TYPE_P (type))
9404 {
9405 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9406 lang_printable_name (type, 0));
9407 return 1;
9408 }
e04a16fb 9409 }
dde1da72
APB
9410 /* `q' might have changed due to a after package resolution
9411 re-qualification */
9412 if (!q)
9413 break;
e04a16fb
AG
9414 }
9415 *found_decl = decl;
9416 return 0;
9417}
9418
9419/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
4dbf4496
APB
9420 can't be accessed from REFERENCE (a record type). This should be
9421 used when decl is a field or a method.*/
e04a16fb 9422
be245ac0
KG
9423static int
9424not_accessible_p (reference, member, from_super)
e04a16fb
AG
9425 tree reference, member;
9426 int from_super;
9427{
9428 int access_flag = get_access_flags_from_decl (member);
9429
4dbf4496
APB
9430 /* Inner classes are processed by check_inner_class_access */
9431 if (INNER_CLASS_TYPE_P (reference))
9432 return 0;
9433
e04a16fb
AG
9434 /* Access always granted for members declared public */
9435 if (access_flag & ACC_PUBLIC)
9436 return 0;
9437
9438 /* Check access on protected members */
9439 if (access_flag & ACC_PROTECTED)
9440 {
9441 /* Access granted if it occurs from within the package
9442 containing the class in which the protected member is
9443 declared */
9444 if (class_in_current_package (DECL_CONTEXT (member)))
9445 return 0;
9446
9bbc7d9f
PB
9447 /* If accessed with the form `super.member', then access is granted */
9448 if (from_super)
9449 return 0;
e04a16fb 9450
9bbc7d9f 9451 /* Otherwise, access is granted if occuring from the class where
4dbf4496
APB
9452 member is declared or a subclass of it. Find the right
9453 context to perform the check */
9454 if (PURE_INNER_CLASS_TYPE_P (reference))
9455 {
9456 while (INNER_CLASS_TYPE_P (reference))
9457 {
9458 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9459 return 0;
9460 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9461 }
9462 }
473e7b07 9463 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9464 return 0;
e04a16fb
AG
9465 return 1;
9466 }
9467
9468 /* Check access on private members. Access is granted only if it
473e7b07 9469 occurs from within the class in which it is declared. Exceptions
4dbf4496 9470 are accesses from inner-classes. */
e04a16fb 9471 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9472 return (current_class == DECL_CONTEXT (member) ? 0 :
9473 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9474
9475 /* Default access are permitted only when occuring within the
9476 package in which the type (REFERENCE) is declared. In other words,
9477 REFERENCE is defined in the current package */
9478 if (ctxp->package)
9479 return !class_in_current_package (reference);
473e7b07 9480
e04a16fb
AG
9481 /* Otherwise, access is granted */
9482 return 0;
9483}
9484
5e942c50
APB
9485/* Test deprecated decl access. */
9486static void
9487check_deprecation (wfl, decl)
9488 tree wfl, decl;
9489{
49f48c71 9490 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9491 /* Complain if the field is deprecated and the file it was defined
9492 in isn't compiled at the same time the file which contains its
9493 use is */
9494 if (DECL_DEPRECATED (decl)
9495 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9496 {
9497 char the [20];
9498 switch (TREE_CODE (decl))
9499 {
9500 case FUNCTION_DECL:
9501 strcpy (the, "method");
9502 break;
9503 case FIELD_DECL:
9504 strcpy (the, "field");
9505 break;
9506 case TYPE_DECL:
9507 strcpy (the, "class");
9508 break;
15fdcfe9
PB
9509 default:
9510 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9511 }
9512 parse_warning_context
9513 (wfl, "The %s `%s' in class `%s' has been deprecated",
9514 the, lang_printable_name (decl, 0),
9515 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9516 }
9517}
9518
e04a16fb
AG
9519/* Returns 1 if class was declared in the current package, 0 otherwise */
9520
9521static int
9522class_in_current_package (class)
9523 tree class;
9524{
9525 static tree cache = NULL_TREE;
9526 int qualified_flag;
9527 tree left;
9528
9529 if (cache == class)
9530 return 1;
9531
9532 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9533
9534 /* If the current package is empty and the name of CLASS is
9535 qualified, class isn't in the current package. If there is a
9536 current package and the name of the CLASS is not qualified, class
9537 isn't in the current package */
0a2138e2 9538 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9539 return 0;
9540
9541 /* If there is not package and the name of CLASS isn't qualified,
9542 they belong to the same unnamed package */
9543 if (!ctxp->package && !qualified_flag)
9544 return 1;
9545
9546 /* Compare the left part of the name of CLASS with the package name */
9547 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9548 if (ctxp->package == left)
9549 {
9550 cache = class;
9551 return 1;
9552 }
9553 return 0;
9554}
9555
9556/* This function may generate code to access DECL from WHERE. This is
9557 done only if certain conditions meet. */
9558
9559static tree
9560maybe_access_field (decl, where, type)
9561 tree decl, where, type;
9562{
5e942c50
APB
9563 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9564 && !FIELD_STATIC (decl))
e04a16fb 9565 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9566 (type ? type : DECL_CONTEXT (decl)),
9567 DECL_NAME (decl));
e04a16fb
AG
9568 return decl;
9569}
9570
15fdcfe9 9571/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9572 and according to the situation, PRIMARY and WHERE may be
9573 used. IS_STATIC is set to 1 if the invoked function is static. */
9574
9575static tree
89e09b9a 9576patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9577 tree patch, primary, where;
9578 int *is_static;
b9f7e36c 9579 tree *ret_decl;
e04a16fb
AG
9580{
9581 tree wfl = TREE_OPERAND (patch, 0);
9582 tree args = TREE_OPERAND (patch, 1);
9583 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9584 tree list;
22eed1e6 9585 int is_static_flag = 0;
89e09b9a 9586 int is_super_init = 0;
bccaf73a 9587 tree this_arg = NULL_TREE;
e04a16fb
AG
9588
9589 /* Should be overriden if everything goes well. Otherwise, if
9590 something fails, it should keep this value. It stop the
9591 evaluation of a bogus assignment. See java_complete_tree,
9592 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9593 evaluating an assignment */
9594 TREE_TYPE (patch) = error_mark_node;
9595
9596 /* Since lookup functions are messing with line numbers, save the
9597 context now. */
9598 java_parser_context_save_global ();
9599
9600 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9601
9602 /* Resolution of qualified name, excluding constructors */
9603 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9604 {
dde1da72 9605 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9606 /* Extract the last IDENTIFIER of the qualified
9607 expression. This is a wfl and we will use it's location
9608 data during error report. */
9609 identifier_wfl = cut_identifier_in_qualified (wfl);
9610 identifier = EXPR_WFL_NODE (identifier_wfl);
9611
9612 /* Given the context, IDENTIFIER is syntactically qualified
9613 as a MethodName. We need to qualify what's before */
9614 qualify_ambiguous_name (wfl);
dde1da72 9615 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9616
dde1da72
APB
9617 if (resolved == error_mark_node)
9618 PATCH_METHOD_RETURN_ERROR ();
9619
9620 type = GET_SKIP_TYPE (resolved);
9621 resolve_and_layout (type, NULL_TREE);
6518c7b5
BM
9622
9623 if (JPRIMITIVE_TYPE_P (type))
9624 {
9625 parse_error_context
9626 (identifier_wfl,
9627 "Can't invoke a method on primitive type `%s'",
9628 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9629 PATCH_METHOD_RETURN_ERROR ();
9630 }
9631
dde1da72
APB
9632 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9633 args = nreverse (args);
2c56429a 9634
e04a16fb 9635 /* We're resolving a call from a type */
dde1da72 9636 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9637 {
dde1da72 9638 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9639 {
9640 parse_error_context
781b0558
KG
9641 (identifier_wfl,
9642 "Can't make static reference to method `%s' in interface `%s'",
9643 IDENTIFIER_POINTER (identifier),
e04a16fb 9644 IDENTIFIER_POINTER (name));
b9f7e36c 9645 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9646 }
e04a16fb
AG
9647 if (list && !METHOD_STATIC (list))
9648 {
c2e3db92 9649 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9650 parse_error_context
9651 (identifier_wfl,
9652 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9653 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9654 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9655 free (fct_name);
b9f7e36c 9656 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9657 }
9658 }
e04a16fb 9659 else
dde1da72
APB
9660 this_arg = primary = resolved;
9661
5e942c50 9662 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9663 wfl = identifier_wfl;
9664 }
9665 /* Resolution of simple names, names generated after a primary: or
9666 constructors */
9667 else
9668 {
cd531a2e 9669 tree class_to_search = NULL_TREE;
c2952b01 9670 int lc; /* Looking for Constructor */
e04a16fb
AG
9671
9672 /* We search constructor in their target class */
9673 if (CALL_CONSTRUCTOR_P (patch))
9674 {
22eed1e6
APB
9675 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9676 class_to_search = EXPR_WFL_NODE (wfl);
9677 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9678 this_identifier_node)
9679 class_to_search = NULL_TREE;
9680 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9681 super_identifier_node)
e04a16fb 9682 {
89e09b9a 9683 is_super_init = 1;
22eed1e6
APB
9684 if (CLASSTYPE_SUPER (current_class))
9685 class_to_search =
9686 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9687 else
9688 {
781b0558 9689 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9690 PATCH_METHOD_RETURN_ERROR ();
9691 }
e04a16fb 9692 }
22eed1e6
APB
9693
9694 /* Class to search is NULL if we're searching the current one */
9695 if (class_to_search)
e04a16fb 9696 {
c2952b01
APB
9697 class_to_search = resolve_and_layout (class_to_search, wfl);
9698
22eed1e6
APB
9699 if (!class_to_search)
9700 {
9701 parse_error_context
9702 (wfl, "Class `%s' not found in type declaration",
9703 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9704 PATCH_METHOD_RETURN_ERROR ();
9705 }
9706
5e942c50
APB
9707 /* Can't instantiate an abstract class, but we can
9708 invoke it's constructor. It's use within the `new'
9709 context is denied here. */
9710 if (CLASS_ABSTRACT (class_to_search)
9711 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9712 {
9713 parse_error_context
781b0558
KG
9714 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9715 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9716 PATCH_METHOD_RETURN_ERROR ();
9717 }
c2952b01 9718
22eed1e6 9719 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9720 }
22eed1e6
APB
9721 else
9722 class_to_search = current_class;
e04a16fb
AG
9723 lc = 1;
9724 }
9725 /* This is a regular search in the local class, unless an
9726 alternate class is specified. */
9727 else
9728 {
9729 class_to_search = (where ? where : current_class);
9730 lc = 0;
9731 }
c2952b01 9732
e04a16fb
AG
9733 /* NAME is a simple identifier or comes from a primary. Search
9734 in the class whose declaration contain the method being
9735 invoked. */
c877974e 9736 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9737
c2952b01 9738 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9739 /* Don't continue if no method were found, as the next statement
9740 can't be executed then. */
b9f7e36c
APB
9741 if (!list)
9742 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9743
9744 /* Check for static reference if non static methods */
9745 if (check_for_static_method_reference (wfl, patch, list,
9746 class_to_search, primary))
b9f7e36c 9747 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9748
165f37bc
APB
9749 /* Check for inner classes creation from illegal contexts */
9750 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9751 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9752 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9753 {
9754 parse_error_context
9755 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9756 lang_printable_name (class_to_search, 0),
9757 (!current_this ? "" :
9758 "; an explicit one must be provided when creating this inner class"));
9759 PATCH_METHOD_RETURN_ERROR ();
9760 }
9761
22eed1e6
APB
9762 /* Non static methods are called with the current object extra
9763 argument. If patch a `new TYPE()', the argument is the value
9764 returned by the object allocator. If method is resolved as a
9765 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9766 args = nreverse (args);
bccaf73a 9767 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9768 {
9769 this_arg = primary ? primary : current_this;
9770
9771 /* If we're using an access method, things are different.
9772 There are two familly of cases:
9773
9774 1) We're not generating bytecodes:
9775
9776 - LIST is non static. It's invocation is transformed from
9777 x(a1,...,an) into this$<n>.x(a1,....an).
9778 - LIST is static. It's invocation is transformed from
9779 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9780
9781 2) We're generating bytecodes:
9782
9783 - LIST is non static. It's invocation is transformed from
9784 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9785 - LIST is static. It's invocation is transformed from
9786 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9787
9788 Of course, this$<n> can be abitrary complex, ranging from
9789 this$0 (the immediate outer context) to
9790 access$0(access$0(...(this$0))).
9791
9792 maybe_use_access_method returns a non zero value if the
dfb99c83 9793 this_arg has to be moved into the (then generated) stub
4dbf4496 9794 argument list. In the meantime, the selected function
dfb99c83 9795 might have be replaced by a generated stub. */
c2952b01
APB
9796 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9797 args = tree_cons (NULL_TREE, this_arg, args);
9798 }
e04a16fb 9799 }
b67d701b 9800
e04a16fb
AG
9801 /* Merge point of all resolution schemes. If we have nothing, this
9802 is an error, already signaled */
b9f7e36c
APB
9803 if (!list)
9804 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9805
e04a16fb
AG
9806 /* Check accessibility, position the is_static flag, build and
9807 return the call */
9bbc7d9f 9808 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9809 {
c2e3db92 9810 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9811 parse_error_context
9812 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9813 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9814 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9815 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9816 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9817 free (fct_name);
b9f7e36c 9818 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9819 }
5e942c50 9820 check_deprecation (wfl, list);
22eed1e6 9821
c2952b01
APB
9822 /* If invoking a innerclass constructor, there are hidden parameters
9823 to pass */
9824 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9825 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9826 {
9827 /* And make sure we add the accessed local variables to be saved
9828 in field aliases. */
9829 args = build_alias_initializer_parameter_list
9830 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9831
da632f2c 9832 /* Secretly pass the current_this/primary as a second argument */
165f37bc
APB
9833 if (primary || current_this)
9834 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9835 else
9836 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9837 }
9838
152de068
APB
9839 /* This handles the situation where a constructor invocation needs
9840 to have an enclosing context passed as a second parameter (the
9841 constructor is one of an inner class. We extract it from the
9842 current function. */
9843 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9844 {
9845 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9846 tree extra_arg;
9847
9848 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9849 {
9850 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9851 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9852 }
9853 else
9854 {
9855 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9856 extra_arg =
9857 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9858 extra_arg = java_complete_tree (extra_arg);
9859 }
9860 args = tree_cons (NULL_TREE, extra_arg, args);
9861 }
9862
22eed1e6 9863 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9864 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9865 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9866
c3f2a476
APB
9867 /* In the context of an explicit constructor invocation, we can't
9868 invoke any method relying on `this'. Exceptions are: we're
9869 invoking a static function, primary exists and is not the current
9870 this, we're creating a new object. */
22eed1e6 9871 if (ctxp->explicit_constructor_p
c3f2a476
APB
9872 && !is_static_flag
9873 && (!primary || primary == current_this)
9874 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9875 {
781b0558 9876 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9877 PATCH_METHOD_RETURN_ERROR ();
9878 }
e04a16fb 9879 java_parser_context_restore_global ();
22eed1e6
APB
9880 if (is_static)
9881 *is_static = is_static_flag;
b9f7e36c
APB
9882 /* Sometimes, we want the decl of the selected method. Such as for
9883 EH checking */
9884 if (ret_decl)
9885 *ret_decl = list;
89e09b9a
PB
9886 patch = patch_invoke (patch, list, args);
9887 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9888 {
c2952b01
APB
9889 tree finit_parms, finit_call;
9890
c00f0fb2 9891 /* Prepare to pass hidden parameters to finit$, if any. */
c2952b01
APB
9892 finit_parms = build_alias_initializer_parameter_list
9893 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9894
c2952b01
APB
9895 finit_call =
9896 build_method_invocation (build_wfl_node (finit_identifier_node),
9897 finit_parms);
9898
9899 /* Generate the code used to initialize fields declared with an
9900 initialization statement and build a compound statement along
9901 with the super constructor invocation. */
89e09b9a
PB
9902 patch = build (COMPOUND_EXPR, void_type_node, patch,
9903 java_complete_tree (finit_call));
9904 CAN_COMPLETE_NORMALLY (patch) = 1;
9905 }
9906 return patch;
e04a16fb
AG
9907}
9908
9909/* Check that we're not trying to do a static reference to a method in
9910 non static method. Return 1 if it's the case, 0 otherwise. */
9911
9912static int
9913check_for_static_method_reference (wfl, node, method, where, primary)
9914 tree wfl, node, method, where, primary;
9915{
9916 if (METHOD_STATIC (current_function_decl)
9917 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9918 {
c2e3db92 9919 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9920 parse_error_context
9921 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9922 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9923 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9924 free (fct_name);
9925 return 1;
9926 }
9927 return 0;
9928}
9929
c2952b01
APB
9930/* Fix the invocation of *MDECL if necessary in the case of a
9931 invocation from an inner class. *THIS_ARG might be modified
9932 appropriately and an alternative access to *MDECL might be
9933 returned. */
9934
9935static int
9936maybe_use_access_method (is_super_init, mdecl, this_arg)
9937 int is_super_init;
9938 tree *mdecl, *this_arg;
9939{
9940 tree ctx;
9941 tree md = *mdecl, ta = *this_arg;
9942 int to_return = 0;
9943 int non_static_context = !METHOD_STATIC (md);
9944
9945 if (is_super_init
165f37bc
APB
9946 || DECL_CONTEXT (md) == current_class
9947 || !PURE_INNER_CLASS_TYPE_P (current_class)
9948 || DECL_FINIT_P (md))
c2952b01
APB
9949 return 0;
9950
9951 /* If we're calling a method found in an enclosing class, generate
9952 what it takes to retrieve the right this. Don't do that if we're
9953 invoking a static method. */
9954
9955 if (non_static_context)
9956 {
9957 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
4dbf4496 9958 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
c2952b01
APB
9959 {
9960 ta = build_current_thisn (current_class);
9961 ta = build_wfl_node (ta);
9962 }
9963 else
9964 {
9965 tree type = ctx;
9966 while (type)
9967 {
9968 maybe_build_thisn_access_method (type);
4dbf4496 9969 if (inherits_from_p (type, DECL_CONTEXT (md)))
c2952b01
APB
9970 {
9971 ta = build_access_to_thisn (ctx, type, 0);
9972 break;
9973 }
9974 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9975 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9976 }
9977 }
9978 ta = java_complete_tree (ta);
9979 }
9980
9981 /* We might have to use an access method to get to MD. We can
9982 break the method access rule as far as we're not generating
9983 bytecode */
9984 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9985 {
9986 md = build_outer_method_access_method (md);
9987 to_return = 1;
9988 }
9989
9990 *mdecl = md;
9991 *this_arg = ta;
9992
9993 /* Returnin a non zero value indicates we were doing a non static
9994 method invokation that is now a static invocation. It will have
9995 callee displace `this' to insert it in the regular argument
9996 list. */
9997 return (non_static_context && to_return);
9998}
9999
e04a16fb
AG
10000/* Patch an invoke expression METHOD and ARGS, based on its invocation
10001 mode. */
10002
10003static tree
89e09b9a 10004patch_invoke (patch, method, args)
e04a16fb 10005 tree patch, method, args;
e04a16fb
AG
10006{
10007 tree dtable, func;
0a2138e2 10008 tree original_call, t, ta;
e815887f 10009 tree cond = NULL_TREE;
e04a16fb 10010
5e942c50
APB
10011 /* Last step for args: convert build-in types. If we're dealing with
10012 a new TYPE() type call, the first argument to the constructor
e815887f 10013 isn't found in the incoming argument list, but delivered by
5e942c50
APB
10014 `new' */
10015 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10016 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10017 t = TREE_CHAIN (t);
ac825856
APB
10018 for (ta = args; t != end_params_node && ta;
10019 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
10020 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10021 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10022 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
10023
10024 /* Resolve unresolved returned type isses */
10025 t = TREE_TYPE (TREE_TYPE (method));
10026 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10027 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 10028
e8fc7396 10029 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10030 func = method;
10031 else
e04a16fb 10032 {
15fdcfe9 10033 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 10034 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
10035 {
10036 case INVOKE_VIRTUAL:
10037 dtable = invoke_build_dtable (0, args);
10038 func = build_invokevirtual (dtable, method);
10039 break;
b9f7e36c 10040
e815887f
TT
10041 case INVOKE_NONVIRTUAL:
10042 /* If the object for the method call is null, we throw an
10043 exception. We don't do this if the object is the current
10044 method's `this'. In other cases we just rely on an
10045 optimization pass to eliminate redundant checks. */
10046 if (TREE_VALUE (args) != current_this)
10047 {
10048 /* We use a SAVE_EXPR here to make sure we only evaluate
10049 the new `self' expression once. */
10050 tree save_arg = save_expr (TREE_VALUE (args));
10051 TREE_VALUE (args) = save_arg;
10052 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10053 null_pointer_node);
10054 }
10055 /* Fall through. */
10056
15fdcfe9
PB
10057 case INVOKE_SUPER:
10058 case INVOKE_STATIC:
10059 func = build_known_method_ref (method, TREE_TYPE (method),
10060 DECL_CONTEXT (method),
10061 signature, args);
10062 break;
e04a16fb 10063
15fdcfe9
PB
10064 case INVOKE_INTERFACE:
10065 dtable = invoke_build_dtable (1, args);
173f556c 10066 func = build_invokeinterface (dtable, method);
15fdcfe9 10067 break;
5e942c50 10068
15fdcfe9 10069 default:
89e09b9a 10070 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
10071 }
10072
10073 /* Ensure self_type is initialized, (invokestatic). FIXME */
10074 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
10075 }
10076
e04a16fb
AG
10077 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10078 TREE_OPERAND (patch, 0) = func;
10079 TREE_OPERAND (patch, 1) = args;
10080 original_call = patch;
10081
e815887f 10082 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
10083 returned value is the first argument to the constructor. We build
10084 a COMPOUND_EXPR and use saved expression so that the overall NEW
10085 expression value is a pointer to a newly created and initialized
10086 class. */
10087 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
10088 {
10089 tree class = DECL_CONTEXT (method);
10090 tree c1, saved_new, size, new;
e8fc7396 10091 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
10092 {
10093 TREE_TYPE (patch) = build_pointer_type (class);
10094 return patch;
10095 }
e04a16fb
AG
10096 if (!TYPE_SIZE (class))
10097 safe_layout_class (class);
10098 size = size_in_bytes (class);
10099 new = build (CALL_EXPR, promote_type (class),
10100 build_address_of (alloc_object_node),
10101 tree_cons (NULL_TREE, build_class_ref (class),
10102 build_tree_list (NULL_TREE,
10103 size_in_bytes (class))),
10104 NULL_TREE);
10105 saved_new = save_expr (new);
10106 c1 = build_tree_list (NULL_TREE, saved_new);
10107 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10108 TREE_OPERAND (original_call, 1) = c1;
10109 TREE_SET_CODE (original_call, CALL_EXPR);
10110 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10111 }
e815887f
TT
10112
10113 /* If COND is set, then we are building a check to see if the object
10114 is NULL. */
10115 if (cond != NULL_TREE)
10116 {
10117 /* We have to make the `then' branch a compound expression to
10118 make the types turn out right. This seems bizarre. */
10119 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10120 build (COMPOUND_EXPR, TREE_TYPE (patch),
10121 build (CALL_EXPR, void_type_node,
10122 build_address_of (soft_nullpointer_node),
10123 NULL_TREE, NULL_TREE),
10124 (FLOAT_TYPE_P (TREE_TYPE (patch))
10125 ? build_real (TREE_TYPE (patch), dconst0)
10126 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10127 integer_zero_node))),
10128 patch);
10129 TREE_SIDE_EFFECTS (patch) = 1;
10130 }
10131
e04a16fb
AG
10132 return patch;
10133}
10134
10135static int
10136invocation_mode (method, super)
10137 tree method;
10138 int super;
10139{
10140 int access = get_access_flags_from_decl (method);
10141
22eed1e6
APB
10142 if (super)
10143 return INVOKE_SUPER;
10144
e815887f 10145 if (access & ACC_STATIC)
e04a16fb
AG
10146 return INVOKE_STATIC;
10147
e815887f
TT
10148 /* We have to look for a constructor before we handle nonvirtual
10149 calls; otherwise the constructor will look nonvirtual. */
10150 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 10151 return INVOKE_STATIC;
e815887f
TT
10152
10153 if (access & ACC_FINAL || access & ACC_PRIVATE)
10154 return INVOKE_NONVIRTUAL;
10155
10156 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10157 return INVOKE_NONVIRTUAL;
10158
e04a16fb
AG
10159 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10160 return INVOKE_INTERFACE;
22eed1e6 10161
e04a16fb
AG
10162 return INVOKE_VIRTUAL;
10163}
10164
b67d701b
PB
10165/* Retrieve a refined list of matching methods. It covers the step
10166 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
10167
10168static tree
10169lookup_method_invoke (lc, cl, class, name, arg_list)
10170 int lc;
10171 tree cl;
10172 tree class, name, arg_list;
10173{
de4c7b02 10174 tree atl = end_params_node; /* Arg Type List */
c877974e 10175 tree method, signature, list, node;
49f48c71 10176 const char *candidates; /* Used for error report */
b5b8a0e7 10177 char *dup;
e04a16fb 10178
5e942c50 10179 /* Fix the arguments */
e04a16fb
AG
10180 for (node = arg_list; node; node = TREE_CHAIN (node))
10181 {
e3884b71 10182 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 10183 /* Non primitive type may have to be resolved */
e3884b71 10184 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
10185 resolve_and_layout (current_arg, NULL_TREE);
10186 /* And promoted */
b67d701b 10187 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 10188 current_arg = promote_type (current_arg);
5e942c50 10189 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 10190 }
e04a16fb 10191
c2952b01
APB
10192 /* Presto. If we're dealing with an anonymous class and a
10193 constructor call, generate the right constructor now, since we
10194 know the arguments' types. */
10195
10196 if (lc && ANONYMOUS_CLASS_P (class))
10197 craft_constructor (TYPE_NAME (class), atl);
10198
5e942c50
APB
10199 /* Find all candidates and then refine the list, searching for the
10200 most specific method. */
10201 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10202 list = find_most_specific_methods_list (list);
b67d701b
PB
10203 if (list && !TREE_CHAIN (list))
10204 return TREE_VALUE (list);
e04a16fb 10205
b67d701b
PB
10206 /* Issue an error. List candidates if any. Candidates are listed
10207 only if accessible (non accessible methods may end-up here for
10208 the sake of a better error report). */
10209 candidates = NULL;
10210 if (list)
e04a16fb 10211 {
e04a16fb 10212 tree current;
b67d701b 10213 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
10214 for (current = list; current; current = TREE_CHAIN (current))
10215 {
b67d701b
PB
10216 tree cm = TREE_VALUE (current);
10217 char string [4096];
10218 if (!cm || not_accessible_p (class, cm, 0))
10219 continue;
b67d701b 10220 sprintf
22eed1e6
APB
10221 (string, " `%s' in `%s'%s",
10222 get_printable_method_name (cm),
b67d701b
PB
10223 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10224 (TREE_CHAIN (current) ? "\n" : ""));
10225 obstack_grow (&temporary_obstack, string, strlen (string));
10226 }
10227 obstack_1grow (&temporary_obstack, '\0');
10228 candidates = obstack_finish (&temporary_obstack);
10229 }
10230 /* Issue the error message */
c877974e
APB
10231 method = make_node (FUNCTION_TYPE);
10232 TYPE_ARG_TYPES (method) = atl;
b67d701b 10233 signature = build_java_argument_signature (method);
c63b98cd 10234 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 10235 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 10236 (lc ? "constructor" : "method"),
b5b8a0e7
APB
10237 (lc ? dup : IDENTIFIER_POINTER (name)),
10238 IDENTIFIER_POINTER (signature), dup,
b67d701b 10239 (candidates ? candidates : ""));
b5b8a0e7 10240 free (dup);
b67d701b
PB
10241 return NULL_TREE;
10242}
10243
5e942c50
APB
10244/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10245 when we're looking for a constructor. */
b67d701b
PB
10246
10247static tree
5e942c50
APB
10248find_applicable_accessible_methods_list (lc, class, name, arglist)
10249 int lc;
b67d701b
PB
10250 tree class, name, arglist;
10251{
ad69b5b6
BM
10252 static struct hash_table t, *searched_classes = NULL;
10253 static int search_not_done = 0;
b67d701b
PB
10254 tree list = NULL_TREE, all_list = NULL_TREE;
10255
ad69b5b6
BM
10256 /* Check the hash table to determine if this class has been searched
10257 already. */
10258 if (searched_classes)
10259 {
10260 if (hash_lookup (searched_classes,
10261 (const hash_table_key) class, FALSE, NULL))
10262 return NULL;
10263 }
10264 else
10265 {
10266 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10267 java_hash_compare_tree_node);
10268 searched_classes = &t;
10269 }
10270
10271 search_not_done++;
10272 hash_lookup (searched_classes,
0c2b8145 10273 (const hash_table_key) class, TRUE, NULL);
ad69b5b6 10274
c2952b01
APB
10275 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10276 {
10277 load_class (class, 1);
10278 safe_layout_class (class);
10279 }
10280
1982388a 10281 /* Search interfaces */
9a7ab4b3
APB
10282 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10283 && CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 10284 {
1982388a
APB
10285 int i, n;
10286 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
165f37bc
APB
10287 search_applicable_methods_list (lc, TYPE_METHODS (class),
10288 name, arglist, &list, &all_list);
1982388a 10289 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 10290 for (i = 1; i < n; i++)
b67d701b 10291 {
de0b553f
APB
10292 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10293 tree rlist;
10294
de0b553f
APB
10295 rlist = find_applicable_accessible_methods_list (lc, t, name,
10296 arglist);
165f37bc 10297 list = chainon (rlist, list);
e04a16fb 10298 }
e04a16fb 10299 }
1982388a
APB
10300 /* Search classes */
10301 else
c2952b01 10302 {
165f37bc
APB
10303 tree sc = class;
10304 int seen_inner_class = 0;
c2952b01
APB
10305 search_applicable_methods_list (lc, TYPE_METHODS (class),
10306 name, arglist, &list, &all_list);
10307
165f37bc
APB
10308 /* We must search all interfaces of this class */
10309 if (!lc)
10310 {
10311 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10312 int n = TREE_VEC_LENGTH (basetype_vec), i;
165f37bc
APB
10313 for (i = 1; i < n; i++)
10314 {
10315 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
165f37bc 10316 if (t != object_type_node)
30a3caef
ZW
10317 {
10318 tree rlist
10319 = find_applicable_accessible_methods_list (lc, t,
10320 name, arglist);
10321 list = chainon (rlist, list);
10322 }
165f37bc 10323 }
165f37bc
APB
10324 }
10325
c2952b01
APB
10326 /* Search enclosing context of inner classes before looking
10327 ancestors up. */
10328 while (!lc && INNER_CLASS_TYPE_P (class))
10329 {
165f37bc
APB
10330 tree rlist;
10331 seen_inner_class = 1;
c2952b01 10332 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10333 rlist = find_applicable_accessible_methods_list (lc, class,
10334 name, arglist);
10335 list = chainon (rlist, list);
c2952b01 10336 }
165f37bc
APB
10337
10338 if (!lc && seen_inner_class
10339 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10340 class = CLASSTYPE_SUPER (sc);
10341 else
10342 class = sc;
10343
ad69b5b6
BM
10344 /* Search superclass */
10345 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10346 {
10347 tree rlist;
10348 class = CLASSTYPE_SUPER (class);
10349 rlist = find_applicable_accessible_methods_list (lc, class,
10350 name, arglist);
10351 list = chainon (rlist, list);
10352 }
10353 }
10354
10355 search_not_done--;
10356
10357 /* We're done. Reset the searched classes list and finally search
10358 java.lang.Object if it wasn't searched already. */
10359 if (!search_not_done)
10360 {
10361 if (!lc
10362 && TYPE_METHODS (object_type_node)
10363 && !hash_lookup (searched_classes,
10364 (const hash_table_key) object_type_node,
10365 FALSE, NULL))
10366 {
10367 search_applicable_methods_list (lc,
10368 TYPE_METHODS (object_type_node),
10369 name, arglist, &list, &all_list);
10370 }
10371 hash_table_free (searched_classes);
10372 searched_classes = NULL;
c2952b01 10373 }
1982388a 10374
b67d701b
PB
10375 /* Either return the list obtained or all selected (but
10376 inaccessible) methods for better error report. */
10377 return (!list ? all_list : list);
10378}
e04a16fb 10379
ad69b5b6 10380/* Effectively search for the appropriate method in method */
1982388a
APB
10381
10382static void
c2952b01 10383search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10384 int lc;
10385 tree method, name, arglist;
10386 tree *list, *all_list;
10387{
10388 for (; method; method = TREE_CHAIN (method))
10389 {
10390 /* When dealing with constructor, stop here, otherwise search
10391 other classes */
10392 if (lc && !DECL_CONSTRUCTOR_P (method))
10393 continue;
10394 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10395 || (GET_METHOD_NAME (method) != name)))
10396 continue;
10397
10398 if (argument_types_convertible (method, arglist))
10399 {
10400 /* Retain accessible methods only */
10401 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10402 method, 0))
10403 *list = tree_cons (NULL_TREE, method, *list);
10404 else
10405 /* Also retain all selected method here */
10406 *all_list = tree_cons (NULL_TREE, method, *list);
10407 }
10408 }
ad69b5b6 10409}
1982388a 10410
b67d701b
PB
10411/* 15.11.2.2 Choose the Most Specific Method */
10412
10413static tree
10414find_most_specific_methods_list (list)
10415 tree list;
10416{
10417 int max = 0;
9a7ab4b3 10418 int abstract, candidates;
b67d701b
PB
10419 tree current, new_list = NULL_TREE;
10420 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10421 {
b67d701b
PB
10422 tree method;
10423 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10424
10425 for (method = list; method; method = TREE_CHAIN (method))
10426 {
0c2b8145 10427 tree method_v, current_v;
b67d701b
PB
10428 /* Don't test a method against itself */
10429 if (method == current)
10430 continue;
10431
0c2b8145
APB
10432 method_v = TREE_VALUE (method);
10433 current_v = TREE_VALUE (current);
10434
10435 /* Compare arguments and location where methods where declared */
10436 if (argument_types_convertible (method_v, current_v))
b67d701b 10437 {
0c2b8145
APB
10438 if (valid_method_invocation_conversion_p
10439 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10440 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10441 && enclosing_context_p (DECL_CONTEXT (method_v),
10442 DECL_CONTEXT (current_v))))
10443 {
10444 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10445 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10446 max = (v > max ? v : max);
10447 }
b67d701b
PB
10448 }
10449 }
e04a16fb
AG
10450 }
10451
b67d701b 10452 /* Review the list and select the maximally specific methods */
9a7ab4b3
APB
10453 for (current = list, abstract = -1, candidates = -1;
10454 current; current = TREE_CHAIN (current))
b67d701b 10455 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
9a7ab4b3
APB
10456 {
10457 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10458 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10459 candidates++;
10460 }
b67d701b 10461
165f37bc
APB
10462 /* If we have several and they're all abstract, just pick the
10463 closest one. */
9a7ab4b3
APB
10464 if (candidates > 0 && (candidates == abstract))
10465 {
10466 new_list = nreverse (new_list);
10467 TREE_CHAIN (new_list) = NULL_TREE;
10468 }
165f37bc 10469
9a7ab4b3
APB
10470 /* We have several, we couldn't find a most specific, all but one are
10471 abstract, we pick the only non abstract one. */
10472 if (candidates > 0 && !max && (candidates == abstract+1))
165f37bc 10473 {
9a7ab4b3
APB
10474 for (current = new_list; current; current = TREE_CHAIN (current))
10475 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10476 {
10477 TREE_CHAIN (current) = NULL_TREE;
10478 new_list = current;
10479 }
165f37bc
APB
10480 }
10481
b67d701b
PB
10482 /* If we can't find one, lower expectations and try to gather multiple
10483 maximally specific methods */
165f37bc 10484 while (!new_list && max)
b67d701b
PB
10485 {
10486 while (--max > 0)
10487 {
10488 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10489 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10490 }
b67d701b
PB
10491 }
10492
10493 return new_list;
e04a16fb
AG
10494}
10495
b67d701b
PB
10496/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10497 converted by method invocation conversion (5.3) to the type of the
10498 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10499 to change less often than M1. */
e04a16fb 10500
b67d701b
PB
10501static int
10502argument_types_convertible (m1, m2_or_arglist)
10503 tree m1, m2_or_arglist;
e04a16fb 10504{
b67d701b
PB
10505 static tree m2_arg_value = NULL_TREE;
10506 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10507
b67d701b 10508 register tree m1_arg, m2_arg;
e04a16fb 10509
c2952b01 10510 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10511
b67d701b
PB
10512 if (m2_arg_value == m2_or_arglist)
10513 m2_arg = m2_arg_cache;
10514 else
10515 {
10516 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10517 argument types */
10518 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10519 {
10520 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10521 if (!METHOD_STATIC (m2_or_arglist))
10522 m2_arg = TREE_CHAIN (m2_arg);
10523 }
10524 else
10525 m2_arg = m2_or_arglist;
e04a16fb 10526
b67d701b
PB
10527 m2_arg_value = m2_or_arglist;
10528 m2_arg_cache = m2_arg;
10529 }
e04a16fb 10530
de4c7b02 10531 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10532 {
c877974e 10533 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10534 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10535 TREE_VALUE (m2_arg)))
10536 break;
10537 m1_arg = TREE_CHAIN (m1_arg);
10538 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10539 }
de4c7b02 10540 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10541}
10542
10543/* Qualification routines */
10544
10545static void
10546qualify_ambiguous_name (id)
10547 tree id;
10548{
cd531a2e
KG
10549 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10550 saved_current_class;
d8fccff5 10551 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10552 int code;
e04a16fb
AG
10553
10554 /* We first qualify the first element, then derive qualification of
10555 others based on the first one. If the first element is qualified
10556 by a resolution (field or type), this resolution is stored in the
10557 QUAL_RESOLUTION of the qual element being examined. We need to
10558 save the current_class since the use of SUPER might change the
10559 its value. */
10560 saved_current_class = current_class;
10561 qual = EXPR_WFL_QUALIFICATION (id);
10562 do {
10563
10564 /* Simple qualified expression feature a qual_wfl that is a
10565 WFL. Expression derived from a primary feature more complicated
10566 things like a CALL_EXPR. Expression from primary need to be
10567 worked out to extract the part on which the qualification will
10568 take place. */
10569 qual_wfl = QUAL_WFL (qual);
10570 switch (TREE_CODE (qual_wfl))
10571 {
10572 case CALL_EXPR:
10573 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10574 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10575 {
10576 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10577 qual_wfl = QUAL_WFL (qual);
10578 }
10579 break;
d8fccff5 10580 case NEW_ARRAY_EXPR:
c2952b01 10581 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10582 qual = TREE_CHAIN (qual);
1a6d4fb7 10583 again = new_array_found = 1;
d8fccff5 10584 continue;
e04a16fb 10585 case CONVERT_EXPR:
f2760b27
APB
10586 break;
10587 case NEW_CLASS_EXPR:
e04a16fb
AG
10588 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10589 break;
c583dd46
APB
10590 case ARRAY_REF:
10591 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10592 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10593 break;
8576f094
APB
10594 case STRING_CST:
10595 qual = TREE_CHAIN (qual);
10596 qual_wfl = QUAL_WFL (qual);
10597 break;
165f37bc
APB
10598 case CLASS_LITERAL:
10599 qual = TREE_CHAIN (qual);
10600 qual_wfl = QUAL_WFL (qual);
10601 break;
0a2138e2
APB
10602 default:
10603 /* Fix for -Wall. Just break doing nothing */
10604 break;
e04a16fb 10605 }
8576f094 10606
e04a16fb
AG
10607 ptr_type = current_class;
10608 again = 0;
8576f094
APB
10609 code = TREE_CODE (qual_wfl);
10610
10611 /* Pos evaluation: non WFL leading expression nodes */
10612 if (code == CONVERT_EXPR
10613 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10614 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10615
cd7c5840
APB
10616 else if (code == INTEGER_CST)
10617 name = qual_wfl;
10618
ac22f9cb 10619 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10620 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10621 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10622
c2952b01
APB
10623 else if (code == TREE_LIST)
10624 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10625
37feda7d
APB
10626 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10627 || code == PLUS_EXPR)
8576f094
APB
10628 {
10629 qual = TREE_CHAIN (qual);
10630 qual_wfl = QUAL_WFL (qual);
10631 again = 1;
10632 }
10633 else
f441f671
APB
10634 {
10635 name = EXPR_WFL_NODE (qual_wfl);
10636 if (!name)
10637 {
10638 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10639 again = 1;
10640 }
10641 }
10642
e04a16fb
AG
10643 /* If we have a THIS (from a primary), we set the context accordingly */
10644 if (name == this_identifier_node)
10645 {
10646 qual = TREE_CHAIN (qual);
10647 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10648 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10649 again = 1;
10650 else
10651 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10652 this_found = 1;
10653 }
10654 /* If we have a SUPER, we set the context accordingly */
10655 if (name == super_identifier_node)
10656 {
10657 current_class = CLASSTYPE_SUPER (ptr_type);
10658 /* Check that there is such a thing as a super class. If not,
10659 return. The error will be caught later on, during the
10660 resolution */
10661 if (!current_class)
10662 {
10663 current_class = saved_current_class;
10664 return;
10665 }
10666 qual = TREE_CHAIN (qual);
10667 /* Do one more interation to set things up */
10668 super_found = again = 1;
10669 }
10670 } while (again);
10671
f2760b27
APB
10672 /* If name appears within the scope of a local variable declaration
10673 or parameter declaration, then it is an expression name. We don't
10674 carry this test out if we're in the context of the use of SUPER
10675 or THIS */
cd7c5840
APB
10676 if (!this_found && !super_found
10677 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10678 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10679 {
10680 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10681 QUAL_RESOLUTION (qual) = decl;
10682 }
10683
10684 /* If within the class/interface NAME was found to be used there
10685 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10686 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10687 address length, it is OK. */
10688 else if ((decl = lookup_field_wrapper (ptr_type, name))
10689 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10690 {
10691 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10692 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10693 }
10694
1a6d4fb7 10695 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10696 - NAME is a class/interface declared within the compilation
10697 unit containing NAME,
10698 - NAME is imported via a single-type-import declaration,
10699 - NAME is declared in an another compilation unit of the package
10700 of the compilation unit containing NAME,
10701 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10702 of the compilation unit containing NAME.
10703 - NAME is actually a STRING_CST. */
cd7c5840
APB
10704 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10705 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10706 {
10707 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10708 QUAL_RESOLUTION (qual) = decl;
10709 }
10710
f2760b27 10711 /* Method call, array references and cast are expression name */
9bbc7d9f 10712 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10713 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10714 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10715 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10716
10717 /* Check here that NAME isn't declared by more than one
10718 type-import-on-demand declaration of the compilation unit
10719 containing NAME. FIXME */
10720
10721 /* Otherwise, NAME is reclassified as a package name */
10722 else
10723 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10724
10725 /* Propagate the qualification accross other components of the
10726 qualified name */
10727 for (qual = TREE_CHAIN (qual); qual;
10728 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10729 {
10730 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10731 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10732 else
10733 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10734 }
10735
10736 /* Store the global qualification for the ambiguous part of ID back
10737 into ID fields */
10738 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10739 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10740 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10741 RESOLVE_TYPE_NAME_P (id) = 1;
10742 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10743 RESOLVE_PACKAGE_NAME_P (id) = 1;
10744
10745 /* Restore the current class */
10746 current_class = saved_current_class;
10747}
10748
10749static int
10750breakdown_qualified (left, right, source)
10751 tree *left, *right, source;
10752{
63ad61ed 10753 char *p, *base;
e04a16fb
AG
10754 int l = IDENTIFIER_LENGTH (source);
10755
63ad61ed
ZW
10756 base = alloca (l + 1);
10757 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10758
e04a16fb 10759 /* Breakdown NAME into REMAINDER . IDENTIFIER */
63ad61ed 10760 p = base + l - 1;
e04a16fb
AG
10761 while (*p != '.' && p != base)
10762 p--;
10763
10764 /* We didn't find a '.'. Return an error */
10765 if (p == base)
10766 return 1;
10767
10768 *p = '\0';
10769 if (right)
10770 *right = get_identifier (p+1);
63ad61ed 10771 *left = get_identifier (base);
e04a16fb
AG
10772
10773 return 0;
10774}
10775
e04a16fb 10776/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10777 local variable decls if present.
10778 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10779
10780static tree
10781java_complete_tree (node)
10782 tree node;
5b09b33e
PB
10783{
10784 node = java_complete_lhs (node);
0c2b8145
APB
10785 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10786 && DECL_INITIAL (node) != NULL_TREE
7f10c2e2 10787 && !flag_emit_xref)
5b09b33e
PB
10788 {
10789 tree value = DECL_INITIAL (node);
10790 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10791 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10792 value = fold_constant_for_init (value, node);
100f7cd8 10793 pop_obstacks ();
5b09b33e
PB
10794 DECL_INITIAL (node) = value;
10795 if (value != NULL_TREE)
c2952b01
APB
10796 {
10797 /* fold_constant_for_init sometimes widen the original type
10798 of the constant (i.e. byte to int.) It's not desirable,
10799 especially if NODE is a function argument. */
10800 if (TREE_CODE (value) == INTEGER_CST
10801 && TREE_TYPE (node) != TREE_TYPE (value))
10802 return convert (TREE_TYPE (node), value);
10803 else
10804 return value;
10805 }
5b09b33e
PB
10806 }
10807 return node;
10808}
10809
2aa11e97
APB
10810static tree
10811java_stabilize_reference (node)
10812 tree node;
10813{
10814 if (TREE_CODE (node) == COMPOUND_EXPR)
10815 {
10816 tree op0 = TREE_OPERAND (node, 0);
10817 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10818 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10819 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10820 return node;
10821 }
5cbdba64 10822 return stabilize_reference (node);
2aa11e97
APB
10823}
10824
5b09b33e
PB
10825/* Patch tree nodes in a function body. When a BLOCK is found, push
10826 local variable decls if present.
10827 Same as java_complete_tree, but does not resolve static finals to values. */
10828
10829static tree
10830java_complete_lhs (node)
10831 tree node;
e04a16fb 10832{
22eed1e6 10833 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10834 int flag;
e04a16fb
AG
10835
10836 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10837 worked out. */
e04a16fb
AG
10838 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10839 return node;
10840
10841 /* The switch block implements cases processing container nodes
10842 first. Contained nodes are always written back. Leaves come
10843 next and return a value. */
10844 switch (TREE_CODE (node))
10845 {
10846 case BLOCK:
10847
10848 /* 1- Block section.
10849 Set the local values on decl names so we can identify them
10850 faster when they're referenced. At that stage, identifiers
10851 are legal so we don't check for declaration errors. */
10852 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10853 {
10854 DECL_CONTEXT (cn) = current_function_decl;
10855 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10856 }
15fdcfe9
PB
10857 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10858 CAN_COMPLETE_NORMALLY (node) = 1;
10859 else
e04a16fb 10860 {
15fdcfe9
PB
10861 tree stmt = BLOCK_EXPR_BODY (node);
10862 tree *ptr;
10863 int error_seen = 0;
10864 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10865 {
c877974e
APB
10866 /* Re-order from (((A; B); C); ...; Z) to
10867 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10868 This makes it easier to scan the statements left-to-right
10869 without using recursion (which might overflow the stack
10870 if the block has many statements. */
10871 for (;;)
10872 {
10873 tree left = TREE_OPERAND (stmt, 0);
10874 if (TREE_CODE (left) != COMPOUND_EXPR)
10875 break;
10876 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10877 TREE_OPERAND (left, 1) = stmt;
10878 stmt = left;
10879 }
10880 BLOCK_EXPR_BODY (node) = stmt;
10881 }
10882
c877974e
APB
10883 /* Now do the actual complete, without deep recursion for
10884 long blocks. */
15fdcfe9 10885 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10886 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10887 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10888 {
10889 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10890 tree *next = &TREE_OPERAND (*ptr, 1);
10891 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10892 if (cur == empty_stmt_node)
10893 {
10894 /* Optimization; makes it easier to detect empty bodies.
10895 Most useful for <clinit> with all-constant initializer. */
10896 *ptr = *next;
10897 continue;
10898 }
15fdcfe9
PB
10899 if (TREE_CODE (cur) == ERROR_MARK)
10900 error_seen++;
10901 else if (! CAN_COMPLETE_NORMALLY (cur))
10902 {
10903 wfl_op2 = *next;
10904 for (;;)
10905 {
10906 if (TREE_CODE (wfl_op2) == BLOCK)
10907 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10908 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10909 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10910 else
10911 break;
10912 }
10913 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10914 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10915 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10916 }
10917 ptr = next;
10918 }
10919 *ptr = java_complete_tree (*ptr);
10920
10921 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10922 return error_mark_node;
15fdcfe9 10923 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10924 }
10925 /* Turn local bindings to null */
10926 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10927 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10928
10929 TREE_TYPE (node) = void_type_node;
10930 break;
10931
10932 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10933
b9f7e36c
APB
10934 case THROW_EXPR:
10935 wfl_op1 = TREE_OPERAND (node, 0);
10936 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10937 /* 14.19 A throw statement cannot complete normally. */
10938 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10939 return patch_throw_statement (node, wfl_op1);
10940
10941 case SYNCHRONIZED_EXPR:
10942 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10943 return patch_synchronized_statement (node, wfl_op1);
10944
b67d701b
PB
10945 case TRY_EXPR:
10946 return patch_try_statement (node);
10947
a7d8d81f
PB
10948 case TRY_FINALLY_EXPR:
10949 COMPLETE_CHECK_OP_0 (node);
10950 COMPLETE_CHECK_OP_1 (node);
10951 CAN_COMPLETE_NORMALLY (node)
10952 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10953 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10954 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10955 return node;
10956
5a005d9e
PB
10957 case CLEANUP_POINT_EXPR:
10958 COMPLETE_CHECK_OP_0 (node);
10959 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10960 CAN_COMPLETE_NORMALLY (node) =
10961 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10962 return node;
10963
10964 case WITH_CLEANUP_EXPR:
10965 COMPLETE_CHECK_OP_0 (node);
10966 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10967 CAN_COMPLETE_NORMALLY (node) =
10968 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10969 TREE_TYPE (node) = void_type_node;
10970 return node;
10971
e04a16fb
AG
10972 case LABELED_BLOCK_EXPR:
10973 PUSH_LABELED_BLOCK (node);
10974 if (LABELED_BLOCK_BODY (node))
10975 COMPLETE_CHECK_OP_1 (node);
10976 TREE_TYPE (node) = void_type_node;
10977 POP_LABELED_BLOCK ();
1fb89a4d
APB
10978
10979 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10980 {
10981 LABELED_BLOCK_BODY (node) = NULL_TREE;
10982 CAN_COMPLETE_NORMALLY (node) = 1;
10983 }
1fb89a4d 10984 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10985 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10986 return node;
10987
10988 case EXIT_BLOCK_EXPR:
10989 /* We don't complete operand 1, because it's the return value of
10990 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10991 return patch_bc_statement (node);
10992
15fdcfe9
PB
10993 case CASE_EXPR:
10994 cn = java_complete_tree (TREE_OPERAND (node, 0));
10995 if (cn == error_mark_node)
10996 return cn;
10997
8576f094
APB
10998 /* First, the case expression must be constant. Values of final
10999 fields are accepted. */
15fdcfe9 11000 cn = fold (cn);
8576f094
APB
11001 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11002 && JDECL_P (TREE_OPERAND (cn, 1))
11003 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11004 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
11005 {
11006 push_obstacks (&permanent_obstack, &permanent_obstack);
11007 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11008 TREE_OPERAND (cn, 1));
11009 pop_obstacks ();
11010 }
15fdcfe9 11011
ce6e9147 11012 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
11013 {
11014 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11015 parse_error_context (node, "Constant expression required");
11016 return error_mark_node;
11017 }
11018
11019 nn = ctxp->current_loop;
11020
11021 /* It must be assignable to the type of the switch expression. */
c877974e
APB
11022 if (!try_builtin_assignconv (NULL_TREE,
11023 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
11024 {
11025 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11026 parse_error_context
11027 (wfl_operator,
11028 "Incompatible type for case. Can't convert `%s' to `int'",
11029 lang_printable_name (TREE_TYPE (cn), 0));
11030 return error_mark_node;
11031 }
11032
11033 cn = fold (convert (int_type_node, cn));
11034
11035 /* Multiple instance of a case label bearing the same
11036 value is checked during code generation. The case
11037 expression is allright so far. */
34d4df06
APB
11038 if (TREE_CODE (cn) == VAR_DECL)
11039 cn = DECL_INITIAL (cn);
15fdcfe9 11040 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 11041 TREE_TYPE (node) = void_type_node;
15fdcfe9 11042 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 11043 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11044 break;
11045
11046 case DEFAULT_EXPR:
11047 nn = ctxp->current_loop;
11048 /* Only one default label is allowed per switch statement */
11049 if (SWITCH_HAS_DEFAULT (nn))
11050 {
11051 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11052 parse_error_context (wfl_operator,
11053 "Duplicate case label: `default'");
11054 return error_mark_node;
11055 }
11056 else
11057 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 11058 TREE_TYPE (node) = void_type_node;
10100cc7 11059 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
11060 CAN_COMPLETE_NORMALLY (node) = 1;
11061 break;
11062
b67d701b 11063 case SWITCH_EXPR:
e04a16fb
AG
11064 case LOOP_EXPR:
11065 PUSH_LOOP (node);
11066 /* Check whether the loop was enclosed in a labeled
11067 statement. If not, create one, insert the loop in it and
11068 return the node */
11069 nn = patch_loop_statement (node);
b67d701b 11070
e04a16fb 11071 /* Anyways, walk the body of the loop */
b67d701b
PB
11072 if (TREE_CODE (node) == LOOP_EXPR)
11073 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11074 /* Switch statement: walk the switch expression and the cases */
11075 else
11076 node = patch_switch_statement (node);
11077
e7c7bcef 11078 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
11079 nn = error_mark_node;
11080 else
15fdcfe9 11081 {
b635eb2f
PB
11082 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11083 /* If we returned something different, that's because we
11084 inserted a label. Pop the label too. */
11085 if (nn != node)
11086 {
11087 if (CAN_COMPLETE_NORMALLY (node))
11088 CAN_COMPLETE_NORMALLY (nn) = 1;
11089 POP_LABELED_BLOCK ();
11090 }
15fdcfe9 11091 }
e04a16fb
AG
11092 POP_LOOP ();
11093 return nn;
11094
11095 case EXIT_EXPR:
11096 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11097 return patch_exit_expr (node);
11098
11099 case COND_EXPR:
11100 /* Condition */
11101 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11102 if (TREE_OPERAND (node, 0) == error_mark_node)
11103 return error_mark_node;
11104 /* then-else branches */
11105 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11106 if (TREE_OPERAND (node, 1) == error_mark_node)
11107 return error_mark_node;
11108 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11109 if (TREE_OPERAND (node, 2) == error_mark_node)
11110 return error_mark_node;
11111 return patch_if_else_statement (node);
11112 break;
11113
22eed1e6
APB
11114 case CONDITIONAL_EXPR:
11115 /* Condition */
11116 wfl_op1 = TREE_OPERAND (node, 0);
11117 COMPLETE_CHECK_OP_0 (node);
11118 wfl_op2 = TREE_OPERAND (node, 1);
11119 COMPLETE_CHECK_OP_1 (node);
11120 wfl_op3 = TREE_OPERAND (node, 2);
11121 COMPLETE_CHECK_OP_2 (node);
11122 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11123
e04a16fb
AG
11124 /* 3- Expression section */
11125 case COMPOUND_EXPR:
15fdcfe9 11126 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
11127 TREE_OPERAND (node, 0) = nn =
11128 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
11129 if (wfl_op2 == empty_stmt_node)
11130 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11131 else
15fdcfe9 11132 {
dc0b3eff 11133 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 11134 {
dc0b3eff
PB
11135 /* An unreachable condition in a do-while statement
11136 is *not* (technically) an unreachable statement. */
11137 nn = wfl_op2;
11138 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11139 nn = EXPR_WFL_NODE (nn);
11140 if (TREE_CODE (nn) != EXIT_EXPR)
11141 {
11142 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11143 parse_error_context (wfl_operator, "Unreachable statement");
11144 }
bccaf73a 11145 }
dc0b3eff
PB
11146 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11147 if (TREE_OPERAND (node, 1) == error_mark_node)
11148 return error_mark_node;
11149 CAN_COMPLETE_NORMALLY (node)
11150 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 11151 }
e04a16fb
AG
11152 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11153 break;
11154
11155 case RETURN_EXPR:
15fdcfe9 11156 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
11157 return patch_return (node);
11158
11159 case EXPR_WITH_FILE_LOCATION:
11160 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11161 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 11162 {
5423609c 11163 tree wfl = node;
15fdcfe9 11164 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
11165 if (node == error_mark_node)
11166 return node;
5423609c
APB
11167 /* Keep line number information somewhere were it doesn't
11168 disrupt the completion process. */
2c56429a 11169 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
11170 {
11171 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11172 TREE_OPERAND (node, 1) = wfl;
11173 }
15fdcfe9
PB
11174 CAN_COMPLETE_NORMALLY (node) = 1;
11175 }
e04a16fb
AG
11176 else
11177 {
5b09b33e
PB
11178 tree body;
11179 int save_lineno = lineno;
11180 lineno = EXPR_WFL_LINENO (node);
11181 body = java_complete_tree (EXPR_WFL_NODE (node));
11182 lineno = save_lineno;
15fdcfe9 11183 EXPR_WFL_NODE (node) = body;
dc0b3eff 11184 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 11185 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
11186 if (body == empty_stmt_node)
11187 {
11188 /* Optimization; makes it easier to detect empty bodies. */
11189 return body;
11190 }
dc0b3eff 11191 if (body == error_mark_node)
e04a16fb
AG
11192 {
11193 /* Its important for the evaluation of assignment that
11194 this mark on the TREE_TYPE is propagated. */
11195 TREE_TYPE (node) = error_mark_node;
11196 return error_mark_node;
11197 }
11198 else
11199 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 11200
e04a16fb
AG
11201 }
11202 break;
11203
b67d701b 11204 case NEW_ARRAY_EXPR:
e04a16fb
AG
11205 /* Patch all the dimensions */
11206 flag = 0;
11207 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11208 {
11209 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
11210 tree dim = convert (int_type_node,
11211 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
11212 if (dim == error_mark_node)
11213 {
11214 flag = 1;
11215 continue;
11216 }
11217 else
11218 {
b9f7e36c 11219 TREE_VALUE (cn) = dim;
e04a16fb
AG
11220 /* Setup the location of the current dimension, for
11221 later error report. */
11222 TREE_PURPOSE (cn) =
11223 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11224 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11225 }
11226 }
11227 /* They complete the array creation expression, if no errors
11228 were found. */
15fdcfe9 11229 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
11230 return (flag ? error_mark_node
11231 : force_evaluation_order (patch_newarray (node)));
e04a16fb 11232
c2952b01
APB
11233 case NEW_ANONYMOUS_ARRAY_EXPR:
11234 /* Create the array type if necessary. */
11235 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11236 {
11237 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11238 if (!(type = resolve_type_during_patch (type)))
11239 return error_mark_node;
11240 type = build_array_from_name (type, NULL_TREE,
11241 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11242 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11243 }
11244 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11245 ANONYMOUS_ARRAY_INITIALIZER (node));
11246 if (node == error_mark_node)
11247 return error_mark_node;
11248 CAN_COMPLETE_NORMALLY (node) = 1;
11249 return node;
11250
b67d701b 11251 case NEW_CLASS_EXPR:
e04a16fb 11252 case CALL_EXPR:
b67d701b 11253 /* Complete function's argument(s) first */
e04a16fb
AG
11254 if (complete_function_arguments (node))
11255 return error_mark_node;
11256 else
b9f7e36c 11257 {
22eed1e6
APB
11258 tree decl, wfl = TREE_OPERAND (node, 0);
11259 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11260
c877974e 11261 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 11262 NULL_TREE, 0, &decl);
c877974e
APB
11263 if (node == error_mark_node)
11264 return error_mark_node;
11265
11266 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11267 /* If we call this(...), register signature and positions */
11268 if (in_this)
11269 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11270 tree_cons (wfl, decl,
11271 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 11272 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 11273 return force_evaluation_order (node);
b9f7e36c 11274 }
e04a16fb
AG
11275
11276 case MODIFY_EXPR:
11277 /* Save potential wfls */
11278 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 11279 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 11280
cd9643f7
PB
11281 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11282 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11283 && DECL_INITIAL (nn) != NULL_TREE)
11284 {
100f7cd8
APB
11285 tree value;
11286
11287 push_obstacks (&permanent_obstack, &permanent_obstack);
11288 value = fold_constant_for_init (nn, nn);
11289 pop_obstacks ();
c2952b01 11290
cd9643f7
PB
11291 if (value != NULL_TREE)
11292 {
11293 tree type = TREE_TYPE (value);
c2952b01
APB
11294 if (JPRIMITIVE_TYPE_P (type) ||
11295 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
11296 return empty_stmt_node;
11297 }
629d4b4d
APB
11298 if (! flag_emit_class_files)
11299 DECL_INITIAL (nn) = NULL_TREE;
cd9643f7 11300 }
e04a16fb 11301 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 11302
e04a16fb
AG
11303 if (TREE_OPERAND (node, 0) == error_mark_node)
11304 return error_mark_node;
11305
5cbdba64
APB
11306 flag = COMPOUND_ASSIGN_P (wfl_op2);
11307 if (flag)
e04a16fb 11308 {
c2952b01
APB
11309 /* This might break when accessing outer field from inner
11310 class. TESTME, FIXME */
2aa11e97 11311 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
11312
11313 /* Hand stablize the lhs on both places */
e04a16fb 11314 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
11315 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11316 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 11317
5cbdba64 11318 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
11319 /* Now complete the RHS. We write it back later on. */
11320 nn = java_complete_tree (TREE_OPERAND (node, 1));
11321
642f15d1
APB
11322 if ((cn = patch_string (nn)))
11323 nn = cn;
11324
2aa11e97
APB
11325 /* The last part of the rewrite for E1 op= E2 is to have
11326 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
11327 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11328 TREE_TYPE (lvalue), nn));
5cbdba64
APB
11329
11330 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
11331 }
11332
f8976021 11333 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
11334 function to complete this RHS. Note that a NEW_ARRAY_INIT
11335 might have been already fully expanded if created as a result
11336 of processing an anonymous array initializer. We avoid doing
11337 the operation twice by testing whether the node already bears
11338 a type. */
11339 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 11340 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 11341 TREE_OPERAND (node, 1));
2aa11e97 11342 /* Otherwise we simply complete the RHS */
f8976021
APB
11343 else
11344 nn = java_complete_tree (TREE_OPERAND (node, 1));
11345
e04a16fb 11346 if (nn == error_mark_node)
c0d87ff6 11347 return error_mark_node;
2aa11e97
APB
11348
11349 /* Write back the RHS as we evaluated it. */
e04a16fb 11350 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
11351
11352 /* In case we're handling = with a String as a RHS, we need to
11353 produce a String out of the RHS (it might still be a
11354 STRING_CST or a StringBuffer at this stage */
11355 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11356 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
11357
11358 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11359 TREE_OPERAND (node, 1))))
11360 {
11361 /* We return error_mark_node if outer_field_access_fix
11362 detects we write into a final. */
11363 if (nn == error_mark_node)
11364 return error_mark_node;
11365 node = nn;
11366 }
11367 else
11368 {
11369 node = patch_assignment (node, wfl_op1, wfl_op2);
11370 /* Reorganize the tree if necessary. */
11371 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11372 || JSTRING_P (TREE_TYPE (node))))
11373 node = java_refold (node);
11374 }
11375
15fdcfe9
PB
11376 CAN_COMPLETE_NORMALLY (node) = 1;
11377 return node;
e04a16fb
AG
11378
11379 case MULT_EXPR:
11380 case PLUS_EXPR:
11381 case MINUS_EXPR:
11382 case LSHIFT_EXPR:
11383 case RSHIFT_EXPR:
11384 case URSHIFT_EXPR:
11385 case BIT_AND_EXPR:
11386 case BIT_XOR_EXPR:
11387 case BIT_IOR_EXPR:
11388 case TRUNC_MOD_EXPR:
c2952b01 11389 case TRUNC_DIV_EXPR:
e04a16fb
AG
11390 case RDIV_EXPR:
11391 case TRUTH_ANDIF_EXPR:
11392 case TRUTH_ORIF_EXPR:
11393 case EQ_EXPR:
11394 case NE_EXPR:
11395 case GT_EXPR:
11396 case GE_EXPR:
11397 case LT_EXPR:
11398 case LE_EXPR:
11399 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11400 knows how to handle those cases. */
11401 wfl_op1 = TREE_OPERAND (node, 0);
11402 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11403
15fdcfe9 11404 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11405 /* Don't complete string nodes if dealing with the PLUS operand. */
11406 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11407 {
11408 nn = java_complete_tree (wfl_op1);
11409 if (nn == error_mark_node)
11410 return error_mark_node;
48a840d9 11411
2aa11e97
APB
11412 TREE_OPERAND (node, 0) = nn;
11413 }
b67d701b 11414 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11415 {
11416 nn = java_complete_tree (wfl_op2);
11417 if (nn == error_mark_node)
11418 return error_mark_node;
48a840d9 11419
2aa11e97
APB
11420 TREE_OPERAND (node, 1) = nn;
11421 }
dc0b3eff 11422 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11423
5e942c50
APB
11424 case INSTANCEOF_EXPR:
11425 wfl_op1 = TREE_OPERAND (node, 0);
11426 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11427 if (flag_emit_xref)
11428 {
11429 TREE_TYPE (node) = boolean_type_node;
11430 return node;
11431 }
5e942c50
APB
11432 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11433
b67d701b 11434 case UNARY_PLUS_EXPR:
e04a16fb
AG
11435 case NEGATE_EXPR:
11436 case TRUTH_NOT_EXPR:
11437 case BIT_NOT_EXPR:
11438 case PREDECREMENT_EXPR:
11439 case PREINCREMENT_EXPR:
11440 case POSTDECREMENT_EXPR:
11441 case POSTINCREMENT_EXPR:
11442 case CONVERT_EXPR:
11443 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11444 how to handle those cases. */
11445 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11446 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11447 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11448 if (TREE_OPERAND (node, 0) == error_mark_node)
11449 return error_mark_node;
4a5f66c3
APB
11450 node = patch_unaryop (node, wfl_op1);
11451 CAN_COMPLETE_NORMALLY (node) = 1;
11452 break;
e04a16fb
AG
11453
11454 case ARRAY_REF:
11455 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11456 how to handle those cases. */
11457 wfl_op1 = TREE_OPERAND (node, 0);
11458 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11459 if (TREE_OPERAND (node, 0) == error_mark_node)
11460 return error_mark_node;
7f1d4866 11461 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11462 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11463 /* The same applies to wfl_op2 */
11464 wfl_op2 = TREE_OPERAND (node, 1);
11465 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11466 if (TREE_OPERAND (node, 1) == error_mark_node)
11467 return error_mark_node;
7f1d4866 11468 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11469 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11470 return patch_array_ref (node);
e04a16fb 11471
63a212ed
PB
11472 case RECORD_TYPE:
11473 return node;;
11474
11475 case COMPONENT_REF:
11476 /* The first step in the re-write of qualified name handling. FIXME.
11477 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11478 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11479 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11480 {
11481 tree name = TREE_OPERAND (node, 1);
11482 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11483 if (field == NULL_TREE)
11484 {
11485 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11486 return error_mark_node;
11487 }
11488 if (! FIELD_STATIC (field))
11489 {
11490 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11491 return error_mark_node;
11492 }
11493 return field;
11494 }
11495 else
11496 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11497 break;
9bbc7d9f 11498
b67d701b 11499 case THIS_EXPR:
e04a16fb
AG
11500 /* Can't use THIS in a static environment */
11501 if (!current_this)
11502 {
11503 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11504 parse_error_context (wfl_operator,
11505 "Keyword `this' used outside allowed context");
e04a16fb
AG
11506 TREE_TYPE (node) = error_mark_node;
11507 return error_mark_node;
11508 }
22eed1e6
APB
11509 if (ctxp->explicit_constructor_p)
11510 {
11511 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11512 parse_error_context
781b0558 11513 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11514 TREE_TYPE (node) = error_mark_node;
11515 return error_mark_node;
11516 }
e04a16fb 11517 return current_this;
c2952b01
APB
11518
11519 case CLASS_LITERAL:
11520 CAN_COMPLETE_NORMALLY (node) = 1;
11521 node = patch_incomplete_class_ref (node);
11522 if (node == error_mark_node)
11523 return error_mark_node;
11524 break;
11525
11526 case INSTANCE_INITIALIZERS_EXPR:
11527 in_instance_initializer++;
11528 node = java_complete_tree (TREE_OPERAND (node, 0));
11529 in_instance_initializer--;
11530 if (node != error_mark_node)
11531 TREE_TYPE (node) = void_type_node;
11532 else
11533 return error_mark_node;
11534 break;
e04a16fb 11535
e04a16fb 11536 default:
15fdcfe9 11537 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11538 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11539 and it's time to turn it into the appropriate String object */
11540 if ((nn = patch_string (node)))
11541 node = nn;
11542 else
11543 fatal ("No case for tree code `%s' - java_complete_tree\n",
11544 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11545 }
11546 return node;
11547}
11548
11549/* Complete function call's argument. Return a non zero value is an
11550 error was found. */
11551
11552static int
11553complete_function_arguments (node)
11554 tree node;
11555{
11556 int flag = 0;
11557 tree cn;
11558
f63991a8 11559 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11560 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11561 {
b67d701b 11562 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11563 parm = java_complete_tree (wfl);
c2952b01 11564
e04a16fb
AG
11565 if (parm == error_mark_node)
11566 {
11567 flag = 1;
11568 continue;
11569 }
b67d701b
PB
11570 /* If have a string literal that we haven't transformed yet or a
11571 crafted string buffer, as a result of use of the the String
11572 `+' operator. Build `parm.toString()' and expand it. */
11573 if ((temp = patch_string (parm)))
b9f7e36c 11574 parm = temp;
5e942c50
APB
11575 /* Inline PRIMTYPE.TYPE read access */
11576 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11577
5e942c50 11578 TREE_VALUE (cn) = parm;
e04a16fb 11579 }
f63991a8 11580 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11581 return flag;
11582}
11583
11584/* Sometimes (for loops and variable initialized during their
11585 declaration), we want to wrap a statement around a WFL and turn it
11586 debugable. */
11587
11588static tree
11589build_debugable_stmt (location, stmt)
11590 int location;
11591 tree stmt;
11592{
11593 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11594 {
11595 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11596 EXPR_WFL_LINECOL (stmt) = location;
11597 }
11598 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11599 return stmt;
11600}
11601
11602static tree
11603build_expr_block (body, decls)
11604 tree body, decls;
11605{
11606 tree node = make_node (BLOCK);
11607 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11608 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11609 if (body)
11610 TREE_TYPE (node) = TREE_TYPE (body);
11611 TREE_SIDE_EFFECTS (node) = 1;
11612 return node;
11613}
11614
b67d701b
PB
11615/* Create a new function block and link it approriately to current
11616 function block chain */
e04a16fb
AG
11617
11618static tree
11619enter_block ()
11620{
b67d701b
PB
11621 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11622}
11623
11624/* Link block B supercontext to the previous block. The current
11625 function DECL is used as supercontext when enter_a_block is called
11626 for the first time for a given function. The current function body
11627 (DECL_FUNCTION_BODY) is set to be block B. */
11628
11629static tree
11630enter_a_block (b)
11631 tree b;
11632{
e04a16fb
AG
11633 tree fndecl = current_function_decl;
11634
f099f336
APB
11635 if (!fndecl) {
11636 BLOCK_SUPERCONTEXT (b) = current_static_block;
11637 current_static_block = b;
11638 }
11639
11640 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11641 {
11642 BLOCK_SUPERCONTEXT (b) = fndecl;
11643 DECL_FUNCTION_BODY (fndecl) = b;
11644 }
11645 else
11646 {
11647 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11648 DECL_FUNCTION_BODY (fndecl) = b;
11649 }
11650 return b;
11651}
11652
11653/* Exit a block by changing the current function body
11654 (DECL_FUNCTION_BODY) to the current block super context, only if
11655 the block being exited isn't the method's top level one. */
11656
11657static tree
11658exit_block ()
11659{
f099f336
APB
11660 tree b;
11661 if (current_function_decl)
11662 {
11663 b = DECL_FUNCTION_BODY (current_function_decl);
11664 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11665 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11666 }
11667 else
11668 {
11669 b = current_static_block;
e04a16fb 11670
f099f336
APB
11671 if (BLOCK_SUPERCONTEXT (b))
11672 current_static_block = BLOCK_SUPERCONTEXT (b);
11673 }
e04a16fb
AG
11674 return b;
11675}
11676
11677/* Lookup for NAME in the nested function's blocks, all the way up to
11678 the current toplevel one. It complies with Java's local variable
11679 scoping rules. */
11680
11681static tree
11682lookup_name_in_blocks (name)
11683 tree name;
11684{
f099f336 11685 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11686
11687 while (b != current_function_decl)
11688 {
11689 tree current;
11690
11691 /* Paranoid sanity check. To be removed */
11692 if (TREE_CODE (b) != BLOCK)
11693 fatal ("non block expr function body - lookup_name_in_blocks");
11694
11695 for (current = BLOCK_EXPR_DECLS (b); current;
11696 current = TREE_CHAIN (current))
11697 if (DECL_NAME (current) == name)
11698 return current;
11699 b = BLOCK_SUPERCONTEXT (b);
11700 }
11701 return NULL_TREE;
11702}
11703
11704static void
11705maybe_absorb_scoping_blocks ()
11706{
f099f336 11707 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11708 {
11709 tree b = exit_block ();
11710 java_method_add_stmt (current_function_decl, b);
11711 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11712 }
11713}
11714
11715\f
11716/* This section of the source is reserved to build_* functions that
11717 are building incomplete tree nodes and the patch_* functions that
11718 are completing them. */
11719
c2952b01
APB
11720/* Wrap a non WFL node around a WFL. */
11721static tree
9a7ab4b3 11722build_wfl_wrap (node, location)
c2952b01 11723 tree node;
9a7ab4b3 11724 int location;
c2952b01
APB
11725{
11726 tree wfl, node_to_insert = node;
11727
11728 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11729 with the way we're processing SUPER. A THIS from a primary as a
11730 different form than a SUPER. Turn THIS into something symbolic */
11731 if (TREE_CODE (node) == THIS_EXPR)
11732 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11733 else
11734 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11735
9a7ab4b3 11736 EXPR_WFL_LINECOL (wfl) = location;
c2952b01
APB
11737 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11738 return wfl;
11739}
11740
11741
9bbc7d9f 11742/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11743 we're currently dealing with the class java.lang.Object. */
11744
11745static tree
e920ebc9
APB
11746build_super_invocation (mdecl)
11747 tree mdecl;
22eed1e6 11748{
e920ebc9 11749 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11750 return empty_stmt_node;
22eed1e6
APB
11751 else
11752 {
9ee9b555 11753 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11754 tree a = NULL_TREE, t;
11755 /* If we're dealing with an anonymous class, pass the arguments
11756 of the crafted constructor along. */
11757 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11758 {
11759 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11760 for (; t != end_params_node; t = TREE_CHAIN (t))
11761 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11762 }
11763 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11764 }
11765}
11766
11767/* Build a SUPER/THIS qualified method invocation. */
11768
11769static tree
11770build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11771 int use_this;
11772 tree name, args;
11773 int lloc, rloc;
22eed1e6
APB
11774{
11775 tree invok;
11776 tree wfl =
9ee9b555 11777 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11778 EXPR_WFL_LINECOL (wfl) = lloc;
11779 invok = build_method_invocation (name, args);
11780 return make_qualified_primary (wfl, invok, rloc);
11781}
11782
b67d701b 11783/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11784
11785static tree
11786build_method_invocation (name, args)
11787 tree name;
11788 tree args;
11789{
11790 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11791 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11792 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11793 return call;
11794}
11795
11796/* Build an incomplete new xxx(...) node. */
11797
11798static tree
11799build_new_invocation (name, args)
11800 tree name, args;
11801{
11802 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11803 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11804 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11805 return call;
11806}
11807
11808/* Build an incomplete assignment expression. */
11809
11810static tree
11811build_assignment (op, op_location, lhs, rhs)
11812 int op, op_location;
11813 tree lhs, rhs;
11814{
11815 tree assignment;
11816 /* Build the corresponding binop if we deal with a Compound
11817 Assignment operator. Mark the binop sub-tree as part of a
11818 Compound Assignment expression */
11819 if (op != ASSIGN_TK)
11820 {
11821 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11822 COMPOUND_ASSIGN_P (rhs) = 1;
11823 }
11824 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11825 TREE_SIDE_EFFECTS (assignment) = 1;
11826 EXPR_WFL_LINECOL (assignment) = op_location;
11827 return assignment;
11828}
11829
11830/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11831
15fdcfe9 11832char *
e04a16fb
AG
11833print_int_node (node)
11834 tree node;
11835{
11836 static char buffer [80];
11837 if (TREE_CONSTANT_OVERFLOW (node))
11838 sprintf (buffer, "<overflow>");
11839
11840 if (TREE_INT_CST_HIGH (node) == 0)
11841 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11842 TREE_INT_CST_LOW (node));
11843 else if (TREE_INT_CST_HIGH (node) == -1
11844 && TREE_INT_CST_LOW (node) != 0)
11845 {
11846 buffer [0] = '-';
11847 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11848 -TREE_INT_CST_LOW (node));
11849 }
11850 else
11851 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11852 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11853
11854 return buffer;
11855}
11856
7f1d4866
APB
11857/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11858 context. */
5e942c50
APB
11859
11860static int
11861check_final_assignment (lvalue, wfl)
11862 tree lvalue, wfl;
11863{
6632dcdd
APB
11864 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11865 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11866 lvalue = TREE_OPERAND (lvalue, 1);
11867
bc2874c9
TT
11868 /* When generating class files, references to the `length' field
11869 look a bit different. */
11870 if ((flag_emit_class_files
11871 && TREE_CODE (lvalue) == COMPONENT_REF
11872 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11873 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11874 || (TREE_CODE (lvalue) == FIELD_DECL
11875 && FIELD_FINAL (lvalue)
11876 && !DECL_CLINIT_P (current_function_decl)
11877 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11878 {
11879 parse_error_context
11880 (wfl, "Can't assign a value to the final variable `%s'",
11881 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11882 return 1;
11883 }
11884 return 0;
11885}
11886
11887/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11888 read. This is needed to avoid circularities in the implementation
11889 of these fields in libjava. */
11890
11891static tree
11892maybe_build_primttype_type_ref (rhs, wfl)
11893 tree rhs, wfl;
11894{
11895 tree to_return = NULL_TREE;
11896 tree rhs_type = TREE_TYPE (rhs);
11897 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11898 {
11899 tree n = TREE_OPERAND (rhs, 1);
11900 if (TREE_CODE (n) == VAR_DECL
11901 && DECL_NAME (n) == TYPE_identifier_node
9a7ab4b3
APB
11902 && rhs_type == class_ptr_type
11903 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
5e942c50 11904 {
49f48c71 11905 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11906 if (!strncmp (self_name, "java.lang.", 10))
11907 to_return = build_primtype_type_ref (self_name);
11908 }
11909 }
11910 return (to_return ? to_return : rhs );
11911}
11912
e04a16fb
AG
11913/* 15.25 Assignment operators. */
11914
11915static tree
11916patch_assignment (node, wfl_op1, wfl_op2)
11917 tree node;
11918 tree wfl_op1;
11919 tree wfl_op2;
11920{
0a2138e2 11921 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11922 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11923 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11924 int error_found = 0;
11925 int lvalue_from_array = 0;
11926
c2952b01 11927 /* Can't assign to a (blank) final. */
5e942c50
APB
11928 if (check_final_assignment (lvalue, wfl_op1))
11929 error_found = 1;
e04a16fb
AG
11930
11931 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11932
11933 /* Lhs can be a named variable */
34f4db93 11934 if (JDECL_P (lvalue))
e04a16fb 11935 {
e04a16fb
AG
11936 lhs_type = TREE_TYPE (lvalue);
11937 }
11938 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11939 comment on reason why */
11940 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11941 {
11942 lhs_type = TREE_TYPE (lvalue);
11943 lvalue_from_array = 1;
11944 }
11945 /* Or a field access */
11946 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11947 lhs_type = TREE_TYPE (lvalue);
11948 /* Or a function return slot */
11949 else if (TREE_CODE (lvalue) == RESULT_DECL)
11950 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11951 /* Otherwise, we might want to try to write into an optimized static
11952 final, this is an of a different nature, reported further on. */
11953 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11954 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11955 {
6632dcdd 11956 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11957 {
11958 /* What we should do instead is resetting the all the flags
11959 previously set, exchange lvalue for llvalue and continue. */
11960 error_found = 1;
11961 return error_mark_node;
11962 }
11963 else
11964 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11965 }
11966 else
e04a16fb
AG
11967 {
11968 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11969 error_found = 1;
11970 }
11971
11972 rhs_type = TREE_TYPE (rhs);
b67d701b 11973 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11974 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11975
b67d701b 11976 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11977 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11978 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11979
11980 /* 15.25.2 If we have a compound assignment, convert RHS into the
11981 type of the LHS */
11982 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11983 new_rhs = convert (lhs_type, rhs);
11984
11985 /* Explicit cast required. This is an error */
11986 if (!new_rhs)
11987 {
c2e3db92
KG
11988 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11989 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11990 tree wfl;
11991 char operation [32]; /* Max size known */
11992
11993 /* If the assignment is part of a declaration, we use the WFL of
11994 the declared variable to point out the error and call it a
11995 declaration problem. If the assignment is a genuine =
11996 operator, we call is a operator `=' problem, otherwise we
11997 call it an assignment problem. In both of these last cases,
11998 we use the WFL of the operator to indicate the error. */
11999
12000 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12001 {
12002 wfl = wfl_op1;
12003 strcpy (operation, "declaration");
12004 }
12005 else
12006 {
12007 wfl = wfl_operator;
12008 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12009 strcpy (operation, "assignment");
12010 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12011 strcpy (operation, "`return'");
12012 else
12013 strcpy (operation, "`='");
12014 }
12015
1ebadc60 12016 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
12017 parse_error_context
12018 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12019 operation, t1, t2);
1ebadc60 12020 else
781b0558 12021 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 12022 operation, t1, t2);
e04a16fb
AG
12023 free (t1); free (t2);
12024 error_found = 1;
12025 }
12026
c877974e
APB
12027 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12028 if (new_rhs)
12029 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 12030
e04a16fb
AG
12031 if (error_found)
12032 return error_mark_node;
12033
2622b947
APB
12034 /* 10.10: Array Store Exception runtime check */
12035 if (!flag_emit_class_files
e8fc7396 12036 && !flag_emit_xref
2622b947 12037 && lvalue_from_array
afc390b1 12038 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
12039 {
12040 tree check;
12041 tree base = lvalue;
12042
12043 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12044 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12045 base = TREE_OPERAND (lvalue, 0);
12046 else
12047 {
12048 if (flag_bounds_check)
12049 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12050 else
12051 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12052 }
12053
12054 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 12055 new_rhs = save_expr (new_rhs);
2622b947
APB
12056 check = build (CALL_EXPR, void_type_node,
12057 build_address_of (soft_checkarraystore_node),
12058 tree_cons (NULL_TREE, base,
12059 build_tree_list (NULL_TREE, new_rhs)),
12060 NULL_TREE);
12061 TREE_SIDE_EFFECTS (check) = 1;
12062
12063 /* We have to decide on an insertion point */
12064 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12065 {
12066 tree t;
12067 if (flag_bounds_check)
12068 {
12069 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12070 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12071 build (COMPOUND_EXPR, void_type_node, t, check);
12072 }
12073 else
12074 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12075 check, TREE_OPERAND (lvalue, 1));
12076 }
12077 else
12078 {
12079 /* Make sure the bound check will happen before the store check */
12080 if (flag_bounds_check)
12081 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12082 build (COMPOUND_EXPR, void_type_node,
12083 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12084 else
12085 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12086 }
12087 }
22eed1e6 12088
34d4df06
APB
12089 /* Final locals can be used as case values in switch
12090 statement. Prepare them for this eventuality. */
12091 if (TREE_CODE (lvalue) == VAR_DECL
12092 && LOCAL_FINAL (lvalue)
12093 && TREE_CONSTANT (new_rhs)
12094 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12095 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12096 )
12097 {
12098 TREE_CONSTANT (lvalue) = 1;
12099 DECL_INITIAL (lvalue) = new_rhs;
12100 }
12101
e04a16fb
AG
12102 TREE_OPERAND (node, 0) = lvalue;
12103 TREE_OPERAND (node, 1) = new_rhs;
12104 TREE_TYPE (node) = lhs_type;
12105 return node;
12106}
12107
b67d701b
PB
12108/* Check that type SOURCE can be cast into type DEST. If the cast
12109 can't occur at all, return 0 otherwise 1. This function is used to
12110 produce accurate error messages on the reasons why an assignment
12111 failed. */
e04a16fb 12112
b67d701b
PB
12113static tree
12114try_reference_assignconv (lhs_type, rhs)
12115 tree lhs_type, rhs;
e04a16fb 12116{
b67d701b
PB
12117 tree new_rhs = NULL_TREE;
12118 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 12119
b67d701b
PB
12120 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12121 {
12122 /* `null' may be assigned to any reference type */
12123 if (rhs == null_pointer_node)
12124 new_rhs = null_pointer_node;
12125 /* Try the reference assignment conversion */
12126 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12127 new_rhs = rhs;
12128 /* This is a magic assignment that we process differently */
12129 else if (rhs == soft_exceptioninfo_call_node)
12130 new_rhs = rhs;
12131 }
12132 return new_rhs;
12133}
12134
12135/* Check that RHS can be converted into LHS_TYPE by the assignment
12136 conversion (5.2), for the cases of RHS being a builtin type. Return
12137 NULL_TREE if the conversion fails or if because RHS isn't of a
12138 builtin type. Return a converted RHS if the conversion is possible. */
12139
12140static tree
12141try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12142 tree wfl_op1, lhs_type, rhs;
12143{
12144 tree new_rhs = NULL_TREE;
12145 tree rhs_type = TREE_TYPE (rhs);
12146
5e942c50
APB
12147 /* Zero accepted everywhere */
12148 if (TREE_CODE (rhs) == INTEGER_CST
12149 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12150 && JPRIMITIVE_TYPE_P (rhs_type))
12151 new_rhs = convert (lhs_type, rhs);
12152
b67d701b
PB
12153 /* 5.1.1 Try Identity Conversion,
12154 5.1.2 Try Widening Primitive Conversion */
5e942c50 12155 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
12156 new_rhs = convert (lhs_type, rhs);
12157
12158 /* Try a narrowing primitive conversion (5.1.3):
12159 - expression is a constant expression of type int AND
12160 - variable is byte, short or char AND
12161 - The value of the expression is representable in the type of the
12162 variable */
12163 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12164 && (lhs_type == byte_type_node || lhs_type == char_type_node
12165 || lhs_type == short_type_node))
12166 {
12167 if (int_fits_type_p (rhs, lhs_type))
12168 new_rhs = convert (lhs_type, rhs);
12169 else if (wfl_op1) /* Might be called with a NULL */
12170 parse_warning_context
781b0558 12171 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 12172 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
12173 /* Reported a warning that will turn into an error further
12174 down, so we don't return */
12175 }
12176
12177 return new_rhs;
12178}
12179
12180/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
c00f0fb2 12181 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
b67d701b
PB
12182 0 is the conversion test fails. This implements parts the method
12183 invocation convertion (5.3). */
12184
12185static int
12186valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12187 tree lhs_type, rhs_type;
12188{
acd663ee 12189 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
12190 if (lhs_type == rhs_type)
12191 return 1;
12192
acd663ee
APB
12193 /* Reject non primitive types */
12194 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
12195 return 0;
12196
acd663ee
APB
12197 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12198 than a char can't be converted into a char. Short can't too, but
12199 the < test below takes care of that */
b67d701b
PB
12200 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12201 return 0;
12202
5e942c50
APB
12203 /* Accept all promoted type here. Note, we can't use <= in the test
12204 below, because we still need to bounce out assignments of short
12205 to char and the likes */
12206 if (lhs_type == int_type_node
12207 && (rhs_type == promoted_byte_type_node
12208 || rhs_type == promoted_short_type_node
12209 || rhs_type == promoted_char_type_node
12210 || rhs_type == promoted_boolean_type_node))
12211 return 1;
12212
acd663ee
APB
12213 /* From here, an integral is widened if its precision is smaller
12214 than the precision of the LHS or if the LHS is a floating point
12215 type, or the RHS is a float and the RHS a double. */
12216 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12217 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12218 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12219 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
12220 return 1;
12221
12222 return 0;
e04a16fb
AG
12223}
12224
12225/* Check that something of SOURCE type can be assigned or cast to
12226 something of DEST type at runtime. Return 1 if the operation is
12227 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12228 were SOURCE is cast into DEST, which borrows a lot of the
12229 assignment check. */
12230
12231static int
12232valid_ref_assignconv_cast_p (source, dest, cast)
12233 tree source;
12234 tree dest;
12235 int cast;
12236{
09ed0f70
APB
12237 /* SOURCE or DEST might be null if not from a declared entity. */
12238 if (!source || !dest)
12239 return 0;
5e942c50
APB
12240 if (JNULLP_TYPE_P (source))
12241 return 1;
e04a16fb
AG
12242 if (TREE_CODE (source) == POINTER_TYPE)
12243 source = TREE_TYPE (source);
12244 if (TREE_CODE (dest) == POINTER_TYPE)
12245 dest = TREE_TYPE (dest);
12246 /* Case where SOURCE is a class type */
12247 if (TYPE_CLASS_P (source))
12248 {
12249 if (TYPE_CLASS_P (dest))
c2952b01
APB
12250 return (source == dest
12251 || inherits_from_p (source, dest)
c2952b01 12252 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
12253 if (TYPE_INTERFACE_P (dest))
12254 {
12255 /* If doing a cast and SOURCE is final, the operation is
12256 always correct a compile time (because even if SOURCE
12257 does not implement DEST, a subclass of SOURCE might). */
12258 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12259 return 1;
12260 /* Otherwise, SOURCE must implement DEST */
12261 return interface_of_p (dest, source);
12262 }
12263 /* DEST is an array, cast permited if SOURCE is of Object type */
12264 return (cast && source == object_type_node ? 1 : 0);
12265 }
12266 if (TYPE_INTERFACE_P (source))
12267 {
12268 if (TYPE_CLASS_P (dest))
12269 {
12270 /* If not casting, DEST must be the Object type */
12271 if (!cast)
12272 return dest == object_type_node;
12273 /* We're doing a cast. The cast is always valid is class
12274 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 12275 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
12276 return 1;
12277 else
12278 return interface_of_p (source, dest);
12279 }
12280 if (TYPE_INTERFACE_P (dest))
12281 {
12282 /* If doing a cast, then if SOURCE and DEST contain method
12283 with the same signature but different return type, then
12284 this is a (compile time) error */
12285 if (cast)
12286 {
12287 tree method_source, method_dest;
12288 tree source_type;
0a2138e2 12289 tree source_sig;
e04a16fb
AG
12290 tree source_name;
12291 for (method_source = TYPE_METHODS (source); method_source;
12292 method_source = TREE_CHAIN (method_source))
12293 {
12294 source_sig =
12295 build_java_argument_signature (TREE_TYPE (method_source));
12296 source_type = TREE_TYPE (TREE_TYPE (method_source));
12297 source_name = DECL_NAME (method_source);
12298 for (method_dest = TYPE_METHODS (dest);
12299 method_dest; method_dest = TREE_CHAIN (method_dest))
12300 if (source_sig ==
12301 build_java_argument_signature (TREE_TYPE (method_dest))
12302 && source_name == DECL_NAME (method_dest)
12303 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12304 return 0;
12305 }
12306 return 1;
12307 }
12308 else
12309 return source == dest || interface_of_p (dest, source);
12310 }
ee17a290
TT
12311 else
12312 {
12313 /* Array */
12314 return (cast
12315 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12316 || (DECL_NAME (TYPE_NAME (source))
12317 == java_io_serializable)));
12318 }
e04a16fb
AG
12319 }
12320 if (TYPE_ARRAY_P (source))
12321 {
12322 if (TYPE_CLASS_P (dest))
12323 return dest == object_type_node;
09ed0f70 12324 /* Can't cast an array to an interface unless the interface is
ee17a290 12325 java.lang.Cloneable or java.io.Serializable. */
e04a16fb 12326 if (TYPE_INTERFACE_P (dest))
ee17a290
TT
12327 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12328 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
e04a16fb
AG
12329 else /* Arrays */
12330 {
12331 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12332 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12333
b9f7e36c
APB
12334 /* In case of severe errors, they turn out null */
12335 if (!dest_element_type || !source_element_type)
12336 return 0;
e04a16fb
AG
12337 if (source_element_type == dest_element_type)
12338 return 1;
12339 return valid_ref_assignconv_cast_p (source_element_type,
12340 dest_element_type, cast);
12341 }
12342 return 0;
12343 }
12344 return 0;
12345}
12346
b67d701b
PB
12347static int
12348valid_cast_to_p (source, dest)
12349 tree source;
12350 tree dest;
12351{
12352 if (TREE_CODE (source) == POINTER_TYPE)
12353 source = TREE_TYPE (source);
12354 if (TREE_CODE (dest) == POINTER_TYPE)
12355 dest = TREE_TYPE (dest);
12356
12357 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12358 return valid_ref_assignconv_cast_p (source, dest, 1);
12359
12360 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12361 return 1;
12362
12363 return 0;
12364}
12365
12366/* Method invocation conversion test. Return 1 if type SOURCE can be
12367 converted to type DEST through the methond invocation conversion
12368 process (5.3) */
12369
15fdcfe9
PB
12370static tree
12371do_unary_numeric_promotion (arg)
12372 tree arg;
12373{
12374 tree type = TREE_TYPE (arg);
12375 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12376 : TREE_CODE (type) == CHAR_TYPE)
12377 arg = convert (int_type_node, arg);
12378 return arg;
12379}
12380
acd663ee
APB
12381/* Return a non zero value if SOURCE can be converted into DEST using
12382 the method invocation conversion rule (5.3). */
b67d701b
PB
12383static int
12384valid_method_invocation_conversion_p (dest, source)
12385 tree dest, source;
12386{
e3884b71 12387 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12388 && valid_builtin_assignconv_identity_widening_p (dest, source))
12389 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12390 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12391 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12392}
12393
e04a16fb
AG
12394/* Build an incomplete binop expression. */
12395
12396static tree
12397build_binop (op, op_location, op1, op2)
12398 enum tree_code op;
12399 int op_location;
12400 tree op1, op2;
12401{
5e942c50 12402 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12403 TREE_SIDE_EFFECTS (binop) = 1;
12404 /* Store the location of the operator, for better error report. The
12405 string of the operator will be rebuild based on the OP value. */
12406 EXPR_WFL_LINECOL (binop) = op_location;
12407 return binop;
12408}
12409
12410/* Build the string of the operator retained by NODE. If NODE is part
12411 of a compound expression, add an '=' at the end of the string. This
12412 function is called when an error needs to be reported on an
12413 operator. The string is returned as a pointer to a static character
12414 buffer. */
12415
12416static char *
12417operator_string (node)
12418 tree node;
12419{
12420#define BUILD_OPERATOR_STRING(S) \
12421 { \
12422 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12423 return buffer; \
12424 }
12425
12426 static char buffer [10];
12427 switch (TREE_CODE (node))
12428 {
12429 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12430 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12431 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12432 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12433 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12434 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12435 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12436 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12437 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12438 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12439 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12440 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12441 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12442 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12443 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12444 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12445 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12446 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12447 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12448 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12449 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12450 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12451 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12452 case PREINCREMENT_EXPR: /* Fall through */
12453 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12454 case PREDECREMENT_EXPR: /* Fall through */
12455 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12456 default:
12457 fatal ("unregistered operator %s - operator_string",
12458 tree_code_name [TREE_CODE (node)]);
12459 }
12460 return NULL;
12461#undef BUILD_OPERATOR_STRING
12462}
12463
5cbdba64
APB
12464/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12465
12466static int
12467java_decl_equiv (var_acc1, var_acc2)
12468 tree var_acc1, var_acc2;
12469{
12470 if (JDECL_P (var_acc1))
12471 return (var_acc1 == var_acc2);
12472
12473 return (TREE_CODE (var_acc1) == COMPONENT_REF
12474 && TREE_CODE (var_acc2) == COMPONENT_REF
12475 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12476 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12477 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12478}
12479
12480/* Return a non zero value if CODE is one of the operators that can be
12481 used in conjunction with the `=' operator in a compound assignment. */
12482
12483static int
12484binop_compound_p (code)
12485 enum tree_code code;
12486{
12487 int i;
12488 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12489 if (binop_lookup [i] == code)
12490 break;
12491
12492 return i < BINOP_COMPOUND_CANDIDATES;
12493}
12494
12495/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12496
12497static tree
12498java_refold (t)
12499 tree t;
12500{
12501 tree c, b, ns, decl;
12502
12503 if (TREE_CODE (t) != MODIFY_EXPR)
12504 return t;
12505
12506 c = TREE_OPERAND (t, 1);
12507 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12508 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12509 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12510 return t;
12511
12512 /* Now the left branch of the binary operator. */
12513 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12514 if (! (b && TREE_CODE (b) == NOP_EXPR
12515 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12516 return t;
12517
12518 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12519 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12520 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12521 return t;
12522
12523 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12524 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12525 /* It's got to be the an equivalent decl */
12526 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12527 {
12528 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12529 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12530 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12531 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12532 /* Change the right part of the BINOP_EXPR */
12533 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12534 }
12535
12536 return t;
12537}
12538
e04a16fb
AG
12539/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12540 errors but we modify NODE so that it contains the type computed
12541 according to the expression, when it's fixed. Otherwise, we write
12542 error_mark_node as the type. It allows us to further the analysis
12543 of remaining nodes and detects more errors in certain cases. */
12544
12545static tree
12546patch_binop (node, wfl_op1, wfl_op2)
12547 tree node;
12548 tree wfl_op1;
12549 tree wfl_op2;
12550{
12551 tree op1 = TREE_OPERAND (node, 0);
12552 tree op2 = TREE_OPERAND (node, 1);
12553 tree op1_type = TREE_TYPE (op1);
12554 tree op2_type = TREE_TYPE (op2);
48a840d9 12555 tree prom_type = NULL_TREE, cn;
e04a16fb 12556 int code = TREE_CODE (node);
b67d701b 12557
e04a16fb
AG
12558 /* If 1, tell the routine that we have to return error_mark_node
12559 after checking for the initialization of the RHS */
12560 int error_found = 0;
12561
e04a16fb
AG
12562 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12563
e04a16fb
AG
12564 switch (code)
12565 {
12566 /* 15.16 Multiplicative operators */
12567 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12568 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12569 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12570 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12571 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12572 {
12573 if (!JPRIMITIVE_TYPE_P (op1_type))
12574 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12575 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12576 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12577 TREE_TYPE (node) = error_mark_node;
12578 error_found = 1;
12579 break;
12580 }
12581 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12582 /* Change the division operator if necessary */
12583 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12584 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12585
aa4759c1
AH
12586 if (TREE_CODE (prom_type) == INTEGER_TYPE
12587 && flag_use_divide_subroutine
12588 && ! flag_emit_class_files
12589 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12590 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12591
0b4d333e
APB
12592 /* This one is more complicated. FLOATs are processed by a
12593 function call to soft_fmod. Duplicate the value of the
12594 COMPOUND_ASSIGN_P flag. */
e04a16fb 12595 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12596 {
12597 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12598 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12599 TREE_SIDE_EFFECTS (mod)
12600 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12601 return mod;
12602 }
e04a16fb
AG
12603 break;
12604
12605 /* 15.17 Additive Operators */
12606 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12607
12608 /* Operation is valid if either one argument is a string
12609 constant, a String object or a StringBuffer crafted for the
12610 purpose of the a previous usage of the String concatenation
12611 operator */
12612
12613 if (TREE_CODE (op1) == STRING_CST
12614 || TREE_CODE (op2) == STRING_CST
12615 || JSTRING_TYPE_P (op1_type)
12616 || JSTRING_TYPE_P (op2_type)
12617 || IS_CRAFTED_STRING_BUFFER_P (op1)
12618 || IS_CRAFTED_STRING_BUFFER_P (op2))
12619 return build_string_concatenation (op1, op2);
12620
e04a16fb
AG
12621 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12622 Numeric Types */
12623 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12624 {
12625 if (!JPRIMITIVE_TYPE_P (op1_type))
12626 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12627 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12628 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12629 TREE_TYPE (node) = error_mark_node;
12630 error_found = 1;
12631 break;
12632 }
12633 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12634 break;
12635
12636 /* 15.18 Shift Operators */
12637 case LSHIFT_EXPR:
12638 case RSHIFT_EXPR:
12639 case URSHIFT_EXPR:
12640 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12641 {
12642 if (!JINTEGRAL_TYPE_P (op1_type))
12643 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12644 else
1ebadc60
KG
12645 {
12646 if (JPRIMITIVE_TYPE_P (op2_type))
12647 parse_error_context (wfl_operator,
781b0558 12648 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12649 operator_string (node),
12650 lang_printable_name (op2_type, 0));
12651 else
781b0558
KG
12652 parse_error_context (wfl_operator,
12653 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12654 operator_string (node),
12655 lang_printable_name (op2_type, 0));
12656 }
e04a16fb
AG
12657 TREE_TYPE (node) = error_mark_node;
12658 error_found = 1;
12659 break;
12660 }
12661
12662 /* Unary numeric promotion (5.6.1) is performed on each operand
12663 separatly */
15fdcfe9
PB
12664 op1 = do_unary_numeric_promotion (op1);
12665 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12666
12667 /* The type of the shift expression is the type of the promoted
12668 type of the left-hand operand */
12669 prom_type = TREE_TYPE (op1);
12670
c2952b01
APB
12671 /* Shift int only up to 0x1f and long up to 0x3f */
12672 if (prom_type == int_type_node)
12673 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12674 build_int_2 (0x1f, 0)));
12675 else
12676 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12677 build_int_2 (0x3f, 0)));
e04a16fb
AG
12678
12679 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12680 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12681 {
0b4d333e 12682 tree to_return;
73333a87
AH
12683 tree utype = unsigned_type (prom_type);
12684 op1 = convert (utype, op1);
e04a16fb 12685 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12686 TREE_OPERAND (node, 0) = op1;
12687 TREE_OPERAND (node, 1) = op2;
12688 TREE_TYPE (node) = utype;
0b4d333e
APB
12689 to_return = convert (prom_type, node);
12690 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12691 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12692 TREE_SIDE_EFFECTS (to_return)
12693 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12694 return to_return;
e04a16fb
AG
12695 }
12696 break;
5e942c50
APB
12697
12698 /* 15.19.1 Type Comparison Operator instaceof */
12699 case INSTANCEOF_EXPR:
12700
12701 TREE_TYPE (node) = boolean_type_node;
12702
12703 if (!(op2_type = resolve_type_during_patch (op2)))
12704 return error_mark_node;
12705
12706 /* The first operand must be a reference type or the null type */
12707 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12708 error_found = 1; /* Error reported further below */
12709
12710 /* The second operand must be a reference type */
12711 if (!JREFERENCE_TYPE_P (op2_type))
12712 {
12713 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12714 parse_error_context
12715 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12716 lang_printable_name (op2_type, 0));
12717 error_found = 1;
12718 }
12719
12720 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12721 {
12722 /* If the first operand is null, the result is always false */
12723 if (op1 == null_pointer_node)
12724 return boolean_false_node;
15fdcfe9
PB
12725 else if (flag_emit_class_files)
12726 {
12727 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12728 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12729 return node;
12730 }
5e942c50
APB
12731 /* Otherwise we have to invoke instance of to figure it out */
12732 else
67db0ce7 12733 return build_instanceof (op1, op2_type);
5e942c50
APB
12734 }
12735 /* There is no way the expression operand can be an instance of
12736 the type operand. This is a compile time error. */
12737 else
12738 {
c2e3db92 12739 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12740 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12741 parse_error_context
12742 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12743 t1, lang_printable_name (op2_type, 0));
12744 free (t1);
12745 error_found = 1;
12746 }
e04a16fb 12747
5e942c50 12748 break;
e04a16fb
AG
12749
12750 /* 15.21 Bitwise and Logical Operators */
12751 case BIT_AND_EXPR:
12752 case BIT_XOR_EXPR:
12753 case BIT_IOR_EXPR:
12754 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12755 /* Binary numeric promotion is performed on both operand and the
12756 expression retain that type */
12757 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12758
12759 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12760 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12761 /* The type of the bitwise operator expression is BOOLEAN */
12762 prom_type = boolean_type_node;
12763 else
12764 {
12765 if (!JINTEGRAL_TYPE_P (op1_type))
12766 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12767 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12768 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12769 TREE_TYPE (node) = error_mark_node;
12770 error_found = 1;
12771 /* Insert a break here if adding thing before the switch's
12772 break for this case */
12773 }
12774 break;
12775
12776 /* 15.22 Conditional-And Operator */
12777 case TRUTH_ANDIF_EXPR:
12778 /* 15.23 Conditional-Or Operator */
12779 case TRUTH_ORIF_EXPR:
12780 /* Operands must be of BOOLEAN type */
12781 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12782 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12783 {
12784 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12785 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12786 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12787 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12788 TREE_TYPE (node) = boolean_type_node;
12789 error_found = 1;
12790 break;
12791 }
12792 /* The type of the conditional operators is BOOLEAN */
12793 prom_type = boolean_type_node;
12794 break;
12795
12796 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12797 case LT_EXPR:
12798 case GT_EXPR:
12799 case LE_EXPR:
12800 case GE_EXPR:
12801 /* The type of each of the operands must be a primitive numeric
12802 type */
12803 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12804 {
12805 if (!JNUMERIC_TYPE_P (op1_type))
12806 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12807 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12808 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12809 TREE_TYPE (node) = boolean_type_node;
12810 error_found = 1;
12811 break;
12812 }
12813 /* Binary numeric promotion is performed on the operands */
12814 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12815 /* The type of the relation expression is always BOOLEAN */
12816 prom_type = boolean_type_node;
12817 break;
12818
12819 /* 15.20 Equality Operator */
12820 case EQ_EXPR:
12821 case NE_EXPR:
48a840d9
APB
12822 /* It's time for us to patch the strings. */
12823 if ((cn = patch_string (op1)))
12824 {
12825 op1 = cn;
12826 op1_type = TREE_TYPE (op1);
12827 }
12828 if ((cn = patch_string (op2)))
12829 {
12830 op2 = cn;
12831 op2_type = TREE_TYPE (op2);
12832 }
12833
e04a16fb
AG
12834 /* 15.20.1 Numerical Equality Operators == and != */
12835 /* Binary numeric promotion is performed on the operands */
5e942c50 12836 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12837 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12838
12839 /* 15.20.2 Boolean Equality Operators == and != */
12840 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12841 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12842 ; /* Nothing to do here */
12843
12844 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12845 /* Types have to be either references or the null type. If
12846 they're references, it must be possible to convert either
12847 type to the other by casting conversion. */
b9f7e36c
APB
12848 else if (op1 == null_pointer_node || op2 == null_pointer_node
12849 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12850 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12851 || valid_ref_assignconv_cast_p (op2_type,
12852 op1_type, 1))))
e04a16fb
AG
12853 ; /* Nothing to do here */
12854
12855 /* Else we have an error figure what can't be converted into
12856 what and report the error */
12857 else
12858 {
12859 char *t1;
c2e3db92 12860 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12861 parse_error_context
781b0558
KG
12862 (wfl_operator,
12863 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12864 operator_string (node), t1,
0a2138e2 12865 lang_printable_name (op2_type, 0));
e04a16fb
AG
12866 free (t1);
12867 TREE_TYPE (node) = boolean_type_node;
12868 error_found = 1;
12869 break;
12870 }
12871 prom_type = boolean_type_node;
12872 break;
12873 }
12874
e04a16fb
AG
12875 if (error_found)
12876 return error_mark_node;
12877
12878 TREE_OPERAND (node, 0) = op1;
12879 TREE_OPERAND (node, 1) = op2;
12880 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12881 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12882
ce6e9147
APB
12883 if (flag_emit_xref)
12884 return node;
12885
d1472141
PB
12886 /* fold does not respect side-effect order as required for Java but not C.
12887 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12888 * bytecode.
12889 */
12890 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12891 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12892 node = fold (node);
12893 return node;
e04a16fb
AG
12894}
12895
b67d701b
PB
12896/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12897 zero value, the value of CSTE comes after the valude of STRING */
12898
12899static tree
12900do_merge_string_cste (cste, string, string_len, after)
12901 tree cste;
49f48c71 12902 const char *string;
b67d701b
PB
12903 int string_len, after;
12904{
49f48c71 12905 const char *old = TREE_STRING_POINTER (cste);
354e99ce
APB
12906 int old_len = TREE_STRING_LENGTH (cste);
12907 int len = old_len + string_len;
12908 char *new;
12909
12910 cste = make_node (STRING_CST);
b67d701b 12911 TREE_STRING_LENGTH (cste) = len;
354e99ce
APB
12912 new = TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12913
b67d701b
PB
12914 if (after)
12915 {
354e99ce
APB
12916 memcpy (new, string, string_len);
12917 memcpy (&new [string_len], old, old_len);
b67d701b
PB
12918 }
12919 else
12920 {
354e99ce
APB
12921 memcpy (new, old, old_len);
12922 memcpy (&new [old_len], string, string_len);
b67d701b 12923 }
354e99ce 12924 new [len] = '\0';
b67d701b
PB
12925 return cste;
12926}
12927
12928/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12929 new STRING_CST on success, NULL_TREE on failure */
12930
12931static tree
12932merge_string_cste (op1, op2, after)
12933 tree op1, op2;
12934 int after;
12935{
12936 /* Handle two string constants right away */
12937 if (TREE_CODE (op2) == STRING_CST)
12938 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12939 TREE_STRING_LENGTH (op2), after);
12940
12941 /* Reasonable integer constant can be treated right away */
12942 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12943 {
49f48c71
KG
12944 static const char *boolean_true = "true";
12945 static const char *boolean_false = "false";
12946 static const char *null_pointer = "null";
b67d701b 12947 char ch[3];
49f48c71 12948 const char *string;
b67d701b
PB
12949
12950 if (op2 == boolean_true_node)
12951 string = boolean_true;
12952 else if (op2 == boolean_false_node)
12953 string = boolean_false;
12954 else if (op2 == null_pointer_node)
12955 string = null_pointer;
12956 else if (TREE_TYPE (op2) == char_type_node)
12957 {
12958 ch[0] = (char )TREE_INT_CST_LOW (op2);
12959 ch[1] = '\0';
12960 string = ch;
12961 }
12962 else
12963 string = print_int_node (op2);
12964
12965 return do_merge_string_cste (op1, string, strlen (string), after);
12966 }
12967 return NULL_TREE;
12968}
12969
12970/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12971 has to be a STRING_CST and the other part must be a STRING_CST or a
12972 INTEGRAL constant. Return a new STRING_CST if the operation
12973 succeed, NULL_TREE otherwise.
12974
12975 If the case we want to optimize for space, we might want to return
12976 NULL_TREE for each invocation of this routine. FIXME */
12977
12978static tree
12979string_constant_concatenation (op1, op2)
12980 tree op1, op2;
12981{
12982 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12983 {
0a2138e2 12984 tree string, rest;
b67d701b
PB
12985 int invert;
12986
12987 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12988 rest = (string == op1 ? op2 : op1);
12989 invert = (string == op1 ? 0 : 1 );
12990
12991 /* Walk REST, only if it looks reasonable */
12992 if (TREE_CODE (rest) != STRING_CST
12993 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12994 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12995 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12996 {
12997 rest = java_complete_tree (rest);
12998 if (rest == error_mark_node)
12999 return error_mark_node;
13000 rest = fold (rest);
13001 }
13002 return merge_string_cste (string, rest, invert);
13003 }
13004 return NULL_TREE;
13005}
13006
13007/* Implement the `+' operator. Does static optimization if possible,
13008 otherwise create (if necessary) and append elements to a
13009 StringBuffer. The StringBuffer will be carried around until it is
13010 used for a function call or an assignment. Then toString() will be
13011 called on it to turn it into a String object. */
13012
13013static tree
13014build_string_concatenation (op1, op2)
13015 tree op1, op2;
13016{
13017 tree result;
dc0b3eff 13018 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
13019
13020 if (flag_emit_xref)
13021 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
13022
13023 /* Try to do some static optimization */
13024 if ((result = string_constant_concatenation (op1, op2)))
13025 return result;
13026
c0d87ff6
PB
13027 /* Discard empty strings on either side of the expression */
13028 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
13029 {
13030 op1 = op2;
13031 op2 = NULL_TREE;
13032 }
c0d87ff6 13033 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 13034 op2 = NULL_TREE;
b67d701b 13035
acd663ee 13036 /* If operands are string constant, turn then into object references */
b67d701b
PB
13037 if (TREE_CODE (op1) == STRING_CST)
13038 op1 = patch_string_cst (op1);
acd663ee 13039 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
13040 op2 = patch_string_cst (op2);
13041
acd663ee
APB
13042 /* If either one of the constant is null and the other non null
13043 operand is a String object, return it. */
13044 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13045 return op1;
13046
b67d701b
PB
13047 /* If OP1 isn't already a StringBuffer, create and
13048 initialize a new one */
13049 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13050 {
13051 /* Two solutions here:
c52b5771
AG
13052 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13053 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13054 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
13055 op1 = BUILD_STRING_BUFFER (op1);
13056 else
13057 {
13058 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13059 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13060 }
13061 }
13062
acd663ee
APB
13063 if (op2)
13064 {
13065 /* OP1 is no longer the last node holding a crafted StringBuffer */
13066 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13067 /* Create a node for `{new...,xxx}.append (op2)' */
13068 if (op2)
13069 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13070 }
13071
b67d701b
PB
13072 /* Mark the last node holding a crafted StringBuffer */
13073 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
13074
13075 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
13076 return op1;
13077}
13078
13079/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13080 StringBuffer. If no string were found to be patched, return
13081 NULL. */
13082
13083static tree
13084patch_string (node)
13085 tree node;
13086{
1179ebc2
APB
13087 if (node == error_mark_node)
13088 return error_mark_node;
b67d701b
PB
13089 if (TREE_CODE (node) == STRING_CST)
13090 return patch_string_cst (node);
13091 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13092 {
c877974e 13093 int saved = ctxp->explicit_constructor_p;
b67d701b 13094 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
13095 tree ret;
13096 /* Temporary disable forbid the use of `this'. */
13097 ctxp->explicit_constructor_p = 0;
13098 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
13099 /* String concatenation arguments must be evaluated in order too. */
13100 ret = force_evaluation_order (ret);
c877974e
APB
13101 /* Restore it at its previous value */
13102 ctxp->explicit_constructor_p = saved;
13103 return ret;
b67d701b
PB
13104 }
13105 return NULL_TREE;
13106}
13107
13108/* Build the internal representation of a string constant. */
13109
13110static tree
13111patch_string_cst (node)
13112 tree node;
13113{
13114 int location;
15fdcfe9
PB
13115 if (! flag_emit_class_files)
13116 {
13117 push_obstacks (&permanent_obstack, &permanent_obstack);
13118 node = get_identifier (TREE_STRING_POINTER (node));
13119 location = alloc_name_constant (CONSTANT_String, node);
13120 node = build_ref_from_constant_pool (location);
8226320b 13121 pop_obstacks ();
15fdcfe9 13122 }
cd9643f7 13123 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
13124 TREE_CONSTANT (node) = 1;
13125 return node;
13126}
13127
13128/* Build an incomplete unary operator expression. */
e04a16fb
AG
13129
13130static tree
13131build_unaryop (op_token, op_location, op1)
13132 int op_token, op_location;
13133 tree op1;
13134{
13135 enum tree_code op;
13136 tree unaryop;
13137 switch (op_token)
13138 {
b67d701b 13139 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
13140 case MINUS_TK: op = NEGATE_EXPR; break;
13141 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13142 case NOT_TK: op = BIT_NOT_EXPR; break;
13143 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13144 op_token);
13145 }
13146
13147 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
13148 TREE_SIDE_EFFECTS (unaryop) = 1;
13149 /* Store the location of the operator, for better error report. The
13150 string of the operator will be rebuild based on the OP value. */
13151 EXPR_WFL_LINECOL (unaryop) = op_location;
13152 return unaryop;
13153}
13154
13155/* Special case for the ++/-- operators, since they require an extra
13156 argument to build, which is set to NULL and patched
13157 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13158
13159static tree
13160build_incdec (op_token, op_location, op1, is_post_p)
13161 int op_token, op_location;
13162 tree op1;
13163 int is_post_p;
13164{
13165 static enum tree_code lookup [2][2] =
13166 {
13167 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13168 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13169 };
13170 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13171 NULL_TREE, op1, NULL_TREE);
13172 TREE_SIDE_EFFECTS (node) = 1;
13173 /* Store the location of the operator, for better error report. The
13174 string of the operator will be rebuild based on the OP value. */
13175 EXPR_WFL_LINECOL (node) = op_location;
13176 return node;
13177}
13178
13179/* Build an incomplete cast operator, based on the use of the
13180 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13181 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13182 though its type is already set. */
13183
13184static tree
13185build_cast (location, type, exp)
13186 int location;
13187 tree type, exp;
13188{
13189 tree node = build1 (CONVERT_EXPR, type, exp);
13190 EXPR_WFL_LINECOL (node) = location;
13191 return node;
13192}
13193
c2952b01
APB
13194/* Build an incomplete class reference operator. */
13195static tree
13196build_incomplete_class_ref (location, class_name)
13197 int location;
13198 tree class_name;
13199{
13200 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13201 EXPR_WFL_LINECOL (node) = location;
13202 return node;
13203}
13204
13205/* Complete an incomplete class reference operator. */
13206static tree
13207patch_incomplete_class_ref (node)
13208 tree node;
13209{
13210 tree type = TREE_OPERAND (node, 0);
13211 tree ref_type;
13212
13213 if (!(ref_type = resolve_type_during_patch (type)))
13214 return error_mark_node;
13215
165f37bc 13216 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
f1ff439a
TT
13217 {
13218 /* A class referenced by `foo.class' is initialized. */
13219 return build_class_init (ref_type, build_class_ref (ref_type));
13220 }
165f37bc
APB
13221
13222 /* If we're emitting class files and we have to deal with non
13223 primitive types, we invoke (and consider generating) the
13224 synthetic static method `class$'. */
13225 if (!TYPE_DOT_CLASS (current_class))
13226 build_dot_class_method (current_class);
f0f3a777 13227 ref_type = build_dot_class_method_invocation (ref_type);
165f37bc 13228 return java_complete_tree (ref_type);
c2952b01
APB
13229}
13230
e04a16fb
AG
13231/* 15.14 Unary operators. We return error_mark_node in case of error,
13232 but preserve the type of NODE if the type is fixed. */
13233
13234static tree
13235patch_unaryop (node, wfl_op)
13236 tree node;
13237 tree wfl_op;
13238{
13239 tree op = TREE_OPERAND (node, 0);
13240 tree op_type = TREE_TYPE (op);
ab3a6dd6 13241 tree prom_type = NULL_TREE, value, decl;
c2952b01 13242 int outer_field_flag = 0;
e04a16fb
AG
13243 int code = TREE_CODE (node);
13244 int error_found = 0;
13245
13246 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13247
13248 switch (code)
13249 {
13250 /* 15.13.2 Postfix Increment Operator ++ */
13251 case POSTINCREMENT_EXPR:
13252 /* 15.13.3 Postfix Increment Operator -- */
13253 case POSTDECREMENT_EXPR:
13254 /* 15.14.1 Prefix Increment Operator ++ */
13255 case PREINCREMENT_EXPR:
13256 /* 15.14.2 Prefix Decrement Operator -- */
13257 case PREDECREMENT_EXPR:
5cbdba64 13258 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
13259 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13260 /* We might be trying to change an outer field accessed using
13261 access method. */
13262 if (outer_field_flag)
13263 {
13264 /* Retrieve the decl of the field we're trying to access. We
13265 do that by first retrieving the function we would call to
13266 access the field. It has been already verified that this
13267 field isn't final */
13268 if (flag_emit_class_files)
13269 decl = TREE_OPERAND (op, 0);
13270 else
13271 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13272 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13273 }
b3edebcf 13274 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 13275 else if (!JDECL_P (decl)
b3edebcf
APB
13276 && TREE_CODE (decl) != COMPONENT_REF
13277 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13278 && TREE_CODE (decl) != INDIRECT_REF
13279 && !(TREE_CODE (decl) == COMPOUND_EXPR
13280 && TREE_OPERAND (decl, 1)
13281 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 13282 {
5e942c50
APB
13283 tree lvalue;
13284 /* Before screaming, check that we're not in fact trying to
13285 increment a optimized static final access, in which case
13286 we issue an different error message. */
13287 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13288 && resolve_expression_name (wfl_op, &lvalue)
13289 && check_final_assignment (lvalue, wfl_op)))
13290 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13291 operator_string (node));
e04a16fb
AG
13292 TREE_TYPE (node) = error_mark_node;
13293 error_found = 1;
13294 }
c2952b01
APB
13295
13296 if (check_final_assignment (op, wfl_op))
5e942c50
APB
13297 error_found = 1;
13298
e04a16fb
AG
13299 /* From now on, we know that op if a variable and that it has a
13300 valid wfl. We use wfl_op to locate errors related to the
13301 ++/-- operand. */
13302 else if (!JNUMERIC_TYPE_P (op_type))
13303 {
13304 parse_error_context
13305 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 13306 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
13307 TREE_TYPE (node) = error_mark_node;
13308 error_found = 1;
13309 }
13310 else
13311 {
4a5f66c3 13312 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
13313 both operands, if really necessary */
13314 if (JINTEGRAL_TYPE_P (op_type))
13315 {
13316 value = build_int_2 (1, 0);
13317 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13318 }
13319 else
13320 {
13321 value = build_int_2 (1, 0);
13322 TREE_TYPE (node) =
13323 binary_numeric_promotion (op_type,
13324 TREE_TYPE (value), &op, &value);
13325 }
c2952b01
APB
13326
13327 /* We remember we might be accessing an outer field */
13328 if (outer_field_flag)
13329 {
13330 /* We re-generate an access to the field */
13331 value = build (PLUS_EXPR, TREE_TYPE (op),
13332 build_outer_field_access (wfl_op, decl), value);
13333
13334 /* And we patch the original access$() into a write
13335 with plus_op as a rhs */
13336 return outer_field_access_fix (node, op, value);
13337 }
13338
5cbdba64 13339 /* And write back into the node. */
4a5f66c3 13340 TREE_OPERAND (node, 0) = op;
e04a16fb 13341 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
13342 /* Convert the overall back into its original type, if
13343 necessary, and return */
13344 if (JINTEGRAL_TYPE_P (op_type))
13345 return fold (node);
13346 else
13347 return fold (convert (op_type, node));
e04a16fb
AG
13348 }
13349 break;
13350
13351 /* 15.14.3 Unary Plus Operator + */
b67d701b 13352 case UNARY_PLUS_EXPR:
e04a16fb
AG
13353 /* 15.14.4 Unary Minus Operator - */
13354 case NEGATE_EXPR:
13355 if (!JNUMERIC_TYPE_P (op_type))
13356 {
13357 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13358 TREE_TYPE (node) = error_mark_node;
13359 error_found = 1;
13360 }
13361 /* Unary numeric promotion is performed on operand */
13362 else
13363 {
15fdcfe9
PB
13364 op = do_unary_numeric_promotion (op);
13365 prom_type = TREE_TYPE (op);
b67d701b 13366 if (code == UNARY_PLUS_EXPR)
4a5f66c3 13367 return fold (op);
e04a16fb
AG
13368 }
13369 break;
13370
13371 /* 15.14.5 Bitwise Complement Operator ~ */
13372 case BIT_NOT_EXPR:
13373 if (!JINTEGRAL_TYPE_P (op_type))
13374 {
13375 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13376 TREE_TYPE (node) = error_mark_node;
13377 error_found = 1;
13378 }
13379 else
13380 {
15fdcfe9
PB
13381 op = do_unary_numeric_promotion (op);
13382 prom_type = TREE_TYPE (op);
e04a16fb
AG
13383 }
13384 break;
13385
13386 /* 15.14.6 Logical Complement Operator ! */
13387 case TRUTH_NOT_EXPR:
13388 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13389 {
13390 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13391 /* But the type is known. We will report an error if further
13392 attempt of a assignment is made with this rhs */
e04a16fb
AG
13393 TREE_TYPE (node) = boolean_type_node;
13394 error_found = 1;
13395 }
13396 else
13397 prom_type = boolean_type_node;
13398 break;
13399
13400 /* 15.15 Cast Expression */
13401 case CONVERT_EXPR:
0a2138e2 13402 value = patch_cast (node, wfl_operator);
e04a16fb 13403 if (value == error_mark_node)
c877974e
APB
13404 {
13405 /* If this cast is part of an assignment, we tell the code
13406 that deals with it not to complain about a mismatch,
13407 because things have been cast, anyways */
13408 TREE_TYPE (node) = error_mark_node;
13409 error_found = 1;
13410 }
13411 else
dc0b3eff
PB
13412 {
13413 value = fold (value);
13414 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13415 return value;
13416 }
e04a16fb
AG
13417 break;
13418 }
13419
e04a16fb
AG
13420 if (error_found)
13421 return error_mark_node;
4a5f66c3
APB
13422
13423 /* There are cases where node has been replaced by something else
13424 and we don't end up returning here: UNARY_PLUS_EXPR,
13425 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13426 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13427 TREE_TYPE (node) = prom_type;
dc0b3eff 13428 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13429 return fold (node);
13430}
13431
13432/* Generic type resolution that sometimes takes place during node
13433 patching. Returned the resolved type or generate an error
13434 message. Return the resolved type or NULL_TREE. */
13435
13436static tree
13437resolve_type_during_patch (type)
13438 tree type;
13439{
13440 if (unresolved_type_p (type, NULL))
13441 {
4142b247 13442 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
e04a16fb
AG
13443 if (!type_decl)
13444 {
13445 parse_error_context (type,
13446 "Class `%s' not found in type declaration",
13447 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13448 return NULL_TREE;
13449 }
13450 else
5e942c50
APB
13451 {
13452 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13453 return TREE_TYPE (type_decl);
13454 }
e04a16fb
AG
13455 }
13456 return type;
13457}
13458/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13459 found. Otherwise NODE or something meant to replace it is returned. */
13460
13461static tree
0a2138e2 13462patch_cast (node, wfl_operator)
e04a16fb 13463 tree node;
e04a16fb
AG
13464 tree wfl_operator;
13465{
13466 tree op = TREE_OPERAND (node, 0);
13467 tree op_type = TREE_TYPE (op);
13468 tree cast_type = TREE_TYPE (node);
13469 char *t1;
13470
13471 /* First resolve OP_TYPE if unresolved */
13472 if (!(cast_type = resolve_type_during_patch (cast_type)))
13473 return error_mark_node;
13474
13475 /* Check on cast that are proven correct at compile time */
13476 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13477 {
e04a16fb
AG
13478 /* Same type */
13479 if (cast_type == op_type)
13480 return node;
13481
0b4d333e
APB
13482 /* float and double type are converted to the original type main
13483 variant and then to the target type. */
13484 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13485 op = convert (integer_type_node, op);
13486
e04a16fb
AG
13487 /* Try widening/narowwing convertion. Potentially, things need
13488 to be worked out in gcc so we implement the extreme cases
13489 correctly. fold_convert() needs to be fixed. */
13490 return convert (cast_type, op);
13491 }
13492
0b4d333e
APB
13493 /* It's also valid to cast a boolean into a boolean */
13494 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13495 return node;
13496
5e942c50
APB
13497 /* null can be casted to references */
13498 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13499 return build_null_of_type (cast_type);
13500
e04a16fb
AG
13501 /* The remaining legal casts involve conversion between reference
13502 types. Check for their compile time correctness. */
13503 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13504 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13505 {
13506 TREE_TYPE (node) = promote_type (cast_type);
13507 /* Now, the case can be determined correct at compile time if
13508 OP_TYPE can be converted into CAST_TYPE by assignment
13509 conversion (5.2) */
13510
13511 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13512 {
13513 TREE_SET_CODE (node, NOP_EXPR);
13514 return node;
13515 }
13516
13517 if (flag_emit_class_files)
13518 {
13519 TREE_SET_CODE (node, CONVERT_EXPR);
13520 return node;
13521 }
e04a16fb
AG
13522
13523 /* The cast requires a run-time check */
13524 return build (CALL_EXPR, promote_type (cast_type),
13525 build_address_of (soft_checkcast_node),
13526 tree_cons (NULL_TREE, build_class_ref (cast_type),
13527 build_tree_list (NULL_TREE, op)),
13528 NULL_TREE);
13529 }
13530
13531 /* Any other casts are proven incorrect at compile time */
c2e3db92 13532 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13533 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13534 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13535 free (t1);
13536 return error_mark_node;
13537}
13538
5e942c50
APB
13539/* Build a null constant and give it the type TYPE. */
13540
13541static tree
13542build_null_of_type (type)
13543 tree type;
13544{
13545 tree node = build_int_2 (0, 0);
13546 TREE_TYPE (node) = promote_type (type);
13547 return node;
13548}
13549
e04a16fb
AG
13550/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13551 a list of indices. */
13552static tree
13553build_array_ref (location, array, index)
13554 int location;
13555 tree array, index;
13556{
13557 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13558 EXPR_WFL_LINECOL (node) = location;
13559 return node;
13560}
13561
13562/* 15.12 Array Access Expression */
13563
13564static tree
c877974e
APB
13565patch_array_ref (node)
13566 tree node;
e04a16fb
AG
13567{
13568 tree array = TREE_OPERAND (node, 0);
13569 tree array_type = TREE_TYPE (array);
13570 tree index = TREE_OPERAND (node, 1);
13571 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13572 int error_found = 0;
13573
13574 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13575
e04a16fb
AG
13576 if (TREE_CODE (array_type) == POINTER_TYPE)
13577 array_type = TREE_TYPE (array_type);
13578
13579 /* The array reference must be an array */
13580 if (!TYPE_ARRAY_P (array_type))
13581 {
13582 parse_error_context
781b0558
KG
13583 (wfl_operator,
13584 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13585 lang_printable_name (array_type, 0));
e04a16fb
AG
13586 TREE_TYPE (node) = error_mark_node;
13587 error_found = 1;
13588 }
13589
c2952b01 13590 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13591 type must be int */
15fdcfe9
PB
13592 index = do_unary_numeric_promotion (index);
13593 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13594 {
1ebadc60 13595 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13596 parse_error_context (wfl_operator,
13597 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13598 lang_printable_name (index_type, 0));
13599 else
781b0558
KG
13600 parse_error_context (wfl_operator,
13601 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13602 lang_printable_name (index_type, 0));
e04a16fb
AG
13603 TREE_TYPE (node) = error_mark_node;
13604 error_found = 1;
13605 }
13606
e04a16fb
AG
13607 if (error_found)
13608 return error_mark_node;
e04a16fb 13609
5e942c50 13610 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13611
7f1d4866 13612 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13613 {
15fdcfe9
PB
13614 TREE_OPERAND (node, 0) = array;
13615 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13616 }
13617 else
939d7216
PB
13618 {
13619 /* The save_expr is for correct evaluation order. It would be cleaner
13620 to use force_evaluation_order (see comment there), but that is
13621 difficult when we also have to deal with bounds checking. */
13622 if (TREE_SIDE_EFFECTS (index))
13623 array = save_expr (array);
13624 node = build_java_arrayaccess (array, array_type, index);
13625 if (TREE_SIDE_EFFECTS (index))
13626 node = build (COMPOUND_EXPR, array_type, array, node);
13627 }
e04a16fb
AG
13628 TREE_TYPE (node) = array_type;
13629 return node;
13630}
13631
13632/* 15.9 Array Creation Expressions */
13633
13634static tree
13635build_newarray_node (type, dims, extra_dims)
13636 tree type;
13637 tree dims;
13638 int extra_dims;
13639{
13640 tree node =
b67d701b 13641 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13642 build_int_2 (extra_dims, 0));
e04a16fb
AG
13643 return node;
13644}
13645
13646static tree
13647patch_newarray (node)
13648 tree node;
13649{
13650 tree type = TREE_OPERAND (node, 0);
13651 tree dims = TREE_OPERAND (node, 1);
13652 tree cdim, array_type;
13653 int error_found = 0;
13654 int ndims = 0;
13655 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13656
13657 /* Dimension types are verified. It's better for the types to be
13658 verified in order. */
13659 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13660 {
13661 int dim_error = 0;
13662 tree dim = TREE_VALUE (cdim);
13663
13664 /* Dim might have been saved during its evaluation */
13665 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13666
13667 /* The type of each specified dimension must be an integral type. */
13668 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13669 dim_error = 1;
13670
13671 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13672 promoted type must be int. */
13673 else
13674 {
15fdcfe9 13675 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13676 if (TREE_TYPE (dim) != int_type_node)
13677 dim_error = 1;
13678 }
13679
13680 /* Report errors on types here */
13681 if (dim_error)
13682 {
13683 parse_error_context
13684 (TREE_PURPOSE (cdim),
781b0558 13685 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13686 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13687 "Explicit cast needed to" : "Can't"),
0a2138e2 13688 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13689 error_found = 1;
13690 }
13691
e04a16fb
AG
13692 TREE_PURPOSE (cdim) = NULL_TREE;
13693 }
13694
13695 /* Resolve array base type if unresolved */
13696 if (!(type = resolve_type_during_patch (type)))
13697 error_found = 1;
13698
13699 if (error_found)
13700 {
13701 /* We don't want further evaluation of this bogus array creation
13702 operation */
13703 TREE_TYPE (node) = error_mark_node;
13704 return error_mark_node;
13705 }
13706
15fdcfe9
PB
13707 /* Set array_type to the actual (promoted) array type of the result. */
13708 if (TREE_CODE (type) == RECORD_TYPE)
13709 type = build_pointer_type (type);
13710 while (--xdims >= 0)
13711 {
13712 type = promote_type (build_java_array_type (type, -1));
13713 }
13714 dims = nreverse (dims);
13715 array_type = type;
13716 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13717 {
13718 type = array_type;
05bccae2
RK
13719 array_type
13720 = build_java_array_type (type,
13721 TREE_CODE (cdim) == INTEGER_CST
13722 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13723 : -1);
15fdcfe9
PB
13724 array_type = promote_type (array_type);
13725 }
13726 dims = nreverse (dims);
13727
e04a16fb
AG
13728 /* The node is transformed into a function call. Things are done
13729 differently according to the number of dimensions. If the number
13730 of dimension is equal to 1, then the nature of the base type
13731 (primitive or not) matters. */
15fdcfe9 13732 if (ndims == 1)
fdec99c6 13733 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13734
e04a16fb
AG
13735 /* Can't reuse what's already written in expr.c because it uses the
13736 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13737 return build (CALL_EXPR, array_type,
e04a16fb 13738 build_address_of (soft_multianewarray_node),
15fdcfe9 13739 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13740 tree_cons (NULL_TREE,
15fdcfe9 13741 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13742 NULL_TREE);
13743}
13744
f8976021
APB
13745/* 10.6 Array initializer. */
13746
13747/* Build a wfl for array element that don't have one, so we can
13748 pin-point errors. */
13749
13750static tree
13751maybe_build_array_element_wfl (node)
13752 tree node;
13753{
13754 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13755 return build_expr_wfl (NULL_TREE, ctxp->filename,
13756 ctxp->elc.line, ctxp->elc.prev_col);
13757 else
13758 return NULL_TREE;
13759}
13760
13761/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13762 identification of initialized arrays easier to detect during walk
13763 and expansion. */
13764
13765static tree
13766build_new_array_init (location, values)
13767 int location;
13768 tree values;
13769{
13770 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13771 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13772 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13773 return to_return;
13774}
13775
13776/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13777 occurred. Otherwise return NODE after having set its type
13778 appropriately. */
13779
13780static tree
13781patch_new_array_init (type, node)
13782 tree type, node;
f8976021
APB
13783{
13784 int error_seen = 0;
fdec99c6 13785 tree current, element_type;
f8976021 13786 HOST_WIDE_INT length;
fdec99c6
PB
13787 int all_constant = 1;
13788 tree init = TREE_OPERAND (node, 0);
f8976021 13789
fdec99c6
PB
13790 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13791 {
13792 parse_error_context (node,
13793 "Invalid array initializer for non-array type `%s'",
13794 lang_printable_name (type, 1));
13795 return error_mark_node;
13796 }
13797 type = TREE_TYPE (type);
13798 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13799
fdec99c6
PB
13800 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13801
13802 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13803 current; length++, current = TREE_CHAIN (current))
f8976021 13804 {
fdec99c6
PB
13805 tree elt = TREE_VALUE (current);
13806 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13807 {
fdec99c6 13808 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13809 elt = TREE_VALUE (current);
13810 /* When compiling to native code, STRING_CST is converted to
13811 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13812 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13813 all_constant = 0;
f8976021 13814 }
fdec99c6
PB
13815 else
13816 {
13817 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13818 TREE_PURPOSE (current) = NULL_TREE;
13819 all_constant = 0;
13820 }
9a7ab4b3
APB
13821 if (elt && TREE_CODE (elt) == TREE_LIST
13822 && TREE_VALUE (elt) == error_mark_node)
fdec99c6 13823 error_seen = 1;
f8976021
APB
13824 }
13825
13826 if (error_seen)
13827 return error_mark_node;
13828
13829 /* Create a new type. We can't reuse the one we have here by
13830 patching its dimension because it originally is of dimension -1
13831 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13832 type = build_java_array_type (element_type, length);
13833 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13834 TREE_TYPE (node) = promote_type (type);
13835 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13836 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13837 return node;
13838}
13839
13840/* Verify that one entry of the initializer element list can be
13841 assigned to the array base type. Report 1 if an error occurred, 0
13842 otherwise. */
13843
13844static int
13845array_constructor_check_entry (type, entry)
13846 tree type, entry;
13847{
13848 char *array_type_string = NULL; /* For error reports */
13849 tree value, type_value, new_value, wfl_value, patched;
13850 int error_seen = 0;
13851
13852 new_value = NULL_TREE;
13853 wfl_value = TREE_VALUE (entry);
13854
100f7cd8 13855 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13856 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13857 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13858 if ((patched = patch_string (value)))
13859 value = patched;
1179ebc2
APB
13860 if (value == error_mark_node)
13861 return 1;
f8976021 13862
f8976021
APB
13863 type_value = TREE_TYPE (value);
13864
1179ebc2 13865 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13866 constant overflow during narrowing. */
13867 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13868 new_value = try_builtin_assignconv (wfl_operator, type, value);
13869 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13870 type_value = promote_type (type);
100f7cd8
APB
13871
13872 pop_obstacks ();
f8976021
APB
13873 /* Check and report errors */
13874 if (!new_value)
13875 {
49f48c71 13876 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13877 "Can't" : "Explicit cast needed to");
13878 if (!array_type_string)
c2e3db92 13879 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13880 parse_error_context
13881 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13882 msg, lang_printable_name (type_value, 1), array_type_string);
13883 error_seen = 1;
13884 }
13885
13886 if (new_value)
13887 {
b8c5b1c6 13888 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
f8976021
APB
13889 TREE_VALUE (entry) = new_value;
13890 }
13891
13892 if (array_type_string)
13893 free (array_type_string);
13894
13895 TREE_PURPOSE (entry) = NULL_TREE;
13896 return error_seen;
13897}
13898
e04a16fb
AG
13899static tree
13900build_this (location)
13901 int location;
13902{
9ee9b555 13903 tree node = build_wfl_node (this_identifier_node);
b67d701b 13904 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13905 EXPR_WFL_LINECOL (node) = location;
13906 return node;
13907}
13908
13909/* 14.15 The return statement. It builds a modify expression that
13910 assigns the returned value to the RESULT_DECL that hold the value
13911 to be returned. */
13912
13913static tree
13914build_return (location, op)
13915 int location;
13916 tree op;
13917{
13918 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13919 EXPR_WFL_LINECOL (node) = location;
b67d701b 13920 node = build_debugable_stmt (location, node);
e04a16fb
AG
13921 return node;
13922}
13923
13924static tree
13925patch_return (node)
13926 tree node;
13927{
13928 tree return_exp = TREE_OPERAND (node, 0);
13929 tree meth = current_function_decl;
13930 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13931 int error_found = 0;
13932
13933 TREE_TYPE (node) = error_mark_node;
13934 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13935
13936 /* It's invalid to have a return value within a function that is
13937 declared with the keyword void or that is a constructor */
13938 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13939 error_found = 1;
13940
f099f336 13941 /* It's invalid to use a return statement in a static block */
c2952b01 13942 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13943 error_found = 1;
13944
e04a16fb
AG
13945 /* It's invalid to have a no return value within a function that
13946 isn't declared with the keyword `void' */
13947 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13948 error_found = 2;
c2952b01
APB
13949
13950 if (in_instance_initializer)
13951 error_found = 1;
e04a16fb
AG
13952
13953 if (error_found)
13954 {
c2952b01 13955 if (in_instance_initializer)
f099f336 13956 parse_error_context (wfl_operator,
c2952b01
APB
13957 "`return' inside instance initializer");
13958
13959 else if (DECL_CLINIT_P (current_function_decl))
13960 parse_error_context (wfl_operator,
13961 "`return' inside static initializer");
f099f336
APB
13962
13963 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13964 {
c2e3db92 13965 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13966 parse_error_context (wfl_operator,
13967 "`return' with%s value from `%s %s'",
13968 (error_found == 1 ? "" : "out"),
13969 t, lang_printable_name (meth, 0));
13970 free (t);
13971 }
13972 else
13973 parse_error_context (wfl_operator,
13974 "`return' with value from constructor `%s'",
13975 lang_printable_name (meth, 0));
e04a16fb
AG
13976 return error_mark_node;
13977 }
13978
5e942c50
APB
13979 /* If we have a return_exp, build a modify expression and expand
13980 it. Note: at that point, the assignment is declared valid, but we
13981 may want to carry some more hacks */
e04a16fb
AG
13982 if (return_exp)
13983 {
5e942c50
APB
13984 tree exp = java_complete_tree (return_exp);
13985 tree modify, patched;
13986
13987 /* If the function returned value and EXP are booleans, EXP has
13988 to be converted into the type of DECL_RESULT, which is integer
13989 (see complete_start_java_method) */
13990 if (TREE_TYPE (exp) == boolean_type_node &&
13991 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13992 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13993
13994 /* `null' can be assigned to a function returning a reference */
13995 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13996 exp == null_pointer_node)
13997 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13998
13999 if ((patched = patch_string (exp)))
14000 exp = patched;
14001
14002 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
14003 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14004 modify = java_complete_tree (modify);
5e942c50 14005
e04a16fb
AG
14006 if (modify != error_mark_node)
14007 {
14008 TREE_SIDE_EFFECTS (modify) = 1;
14009 TREE_OPERAND (node, 0) = modify;
14010 }
14011 else
14012 return error_mark_node;
14013 }
14014 TREE_TYPE (node) = void_type_node;
14015 TREE_SIDE_EFFECTS (node) = 1;
14016 return node;
14017}
14018
14019/* 14.8 The if Statement */
14020
14021static tree
14022build_if_else_statement (location, expression, if_body, else_body)
14023 int location;
14024 tree expression, if_body, else_body;
14025{
14026 tree node;
e04a16fb 14027 if (!else_body)
9bbc7d9f 14028 else_body = empty_stmt_node;
e04a16fb
AG
14029 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14030 EXPR_WFL_LINECOL (node) = location;
b67d701b 14031 node = build_debugable_stmt (location, node);
e04a16fb
AG
14032 return node;
14033}
14034
14035static tree
14036patch_if_else_statement (node)
14037 tree node;
14038{
14039 tree expression = TREE_OPERAND (node, 0);
14040
14041 TREE_TYPE (node) = error_mark_node;
14042 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14043
14044 /* The type of expression must be boolean */
b67d701b
PB
14045 if (TREE_TYPE (expression) != boolean_type_node
14046 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
14047 {
14048 parse_error_context
14049 (wfl_operator,
14050 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 14051 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14052 return error_mark_node;
14053 }
14054
14055 TREE_TYPE (node) = void_type_node;
14056 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14057 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
14058 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14059 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
14060 return node;
14061}
14062
14063/* 14.6 Labeled Statements */
14064
14065/* Action taken when a lableled statement is parsed. a new
14066 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 14067 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
14068
14069static tree
0a2138e2 14070build_labeled_block (location, label)
e04a16fb 14071 int location;
0a2138e2 14072 tree label;
e04a16fb 14073{
b635eb2f 14074 tree label_name ;
e04a16fb 14075 tree label_decl, node;
b635eb2f
PB
14076 if (label == NULL_TREE || label == continue_identifier_node)
14077 label_name = label;
14078 else
e04a16fb 14079 {
b635eb2f
PB
14080 label_name = merge_qualified_name (label_id, label);
14081 /* Issue an error if we try to reuse a label that was previously
14082 declared */
14083 if (IDENTIFIER_LOCAL_VALUE (label_name))
14084 {
14085 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
14086 parse_error_context (wfl_operator,
14087 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
14088 IDENTIFIER_POINTER (label));
14089 EXPR_WFL_LINECOL (wfl_operator) =
14090 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
14091 parse_error_context (wfl_operator,
14092 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
14093 IDENTIFIER_POINTER (label));
14094 java_error_count--;
14095 }
e04a16fb
AG
14096 }
14097
14098 label_decl = create_label_decl (label_name);
14099 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14100 EXPR_WFL_LINECOL (node) = location;
14101 TREE_SIDE_EFFECTS (node) = 1;
14102 return node;
14103}
14104
b67d701b 14105/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
14106
14107static tree
b635eb2f 14108finish_labeled_statement (lbe, statement)
e04a16fb
AG
14109 tree lbe; /* Labeled block expr */
14110 tree statement;
14111{
14112 /* In anyways, tie the loop to its statement */
14113 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
14114 pop_labeled_block ();
14115 POP_LABELED_BLOCK ();
e04a16fb
AG
14116 return lbe;
14117}
14118
14119/* 14.10, 14.11, 14.12 Loop Statements */
14120
14121/* Create an empty LOOP_EXPR and make it the last in the nested loop
14122 list. */
14123
14124static tree
14125build_new_loop (loop_body)
14126 tree loop_body;
14127{
14128 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14129 TREE_SIDE_EFFECTS (loop) = 1;
14130 PUSH_LOOP (loop);
14131 return loop;
14132}
14133
14134/* Create a loop body according to the following structure:
14135 COMPOUND_EXPR
14136 COMPOUND_EXPR (loop main body)
14137 EXIT_EXPR (this order is for while/for loops.
14138 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 14139 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
14140 BODY end of this labeled block)
14141 INCREMENT (if any)
14142
14143 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
14144 after the body, like in the do-while loop.
14145
14146 To obtain a loop, the loop body structure described above is
14147 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14148
14149 LABELED_BLOCK_EXPR
14150 LABEL_DECL (use this label to exit the loop)
14151 LOOP_EXPR
14152 <structure described above> */
e04a16fb
AG
14153
14154static tree
14155build_loop_body (location, condition, reversed)
14156 int location;
14157 tree condition;
14158 int reversed;
14159{
0a2138e2 14160 tree first, second, body;
e04a16fb
AG
14161
14162 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14163 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14164 condition = build_debugable_stmt (location, condition);
14165 TREE_SIDE_EFFECTS (condition) = 1;
14166
b635eb2f 14167 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
14168 first = (reversed ? body : condition);
14169 second = (reversed ? condition : body);
14170 return
14171 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 14172 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
14173}
14174
14175/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14176 their order) on the current loop. Unlink the current loop from the
14177 loop list. */
14178
14179static tree
b635eb2f 14180finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
14181 int location;
14182 tree condition, body;
14183 int reversed;
14184{
14185 tree to_return = ctxp->current_loop;
14186 tree loop_body = LOOP_EXPR_BODY (to_return);
14187 if (condition)
14188 {
14189 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14190 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14191 The real EXIT_EXPR is one operand further. */
14192 EXPR_WFL_LINECOL (cnode) = location;
14193 /* This one is for accurate error reports */
14194 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14195 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14196 }
14197 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14198 POP_LOOP ();
14199 return to_return;
14200}
14201
b635eb2f 14202/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
14203 loops feature the condition part */
14204
14205static tree
b635eb2f 14206finish_for_loop (location, condition, update, body)
e04a16fb
AG
14207 int location;
14208 tree condition, update, body;
14209{
14210 /* Put the condition and the loop body in place */
b635eb2f 14211 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
14212 /* LOOP is the current loop which has been now popped of the loop
14213 stack. Install the update block */
14214 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14215 return loop;
14216}
14217
5cbdba64
APB
14218/* Try to find the loop a block might be related to. This comprises
14219 the case where the LOOP_EXPR is found as the second operand of a
14220 COMPOUND_EXPR, because the loop happens to have an initialization
14221 part, then expressed as the first operand of the COMPOUND_EXPR. If
14222 the search finds something, 1 is returned. Otherwise, 0 is
14223 returned. The search is assumed to start from a
14224 LABELED_BLOCK_EXPR's block. */
14225
14226static tree
14227search_loop (statement)
14228 tree statement;
14229{
14230 if (TREE_CODE (statement) == LOOP_EXPR)
14231 return statement;
14232
14233 if (TREE_CODE (statement) == BLOCK)
14234 statement = BLOCK_SUBBLOCKS (statement);
14235 else
14236 return NULL_TREE;
14237
14238 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14239 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14240 statement = TREE_OPERAND (statement, 1);
14241
14242 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 14243 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
14244}
14245
14246/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14247 returned otherwise. */
14248
14249static int
14250labeled_block_contains_loop_p (block, loop)
14251 tree block, loop;
14252{
14253 if (!block)
14254 return 0;
14255
14256 if (LABELED_BLOCK_BODY (block) == loop)
14257 return 1;
14258
c2952b01 14259 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
14260 return 1;
14261
14262 return 0;
14263}
14264
e04a16fb 14265/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 14266 insert LOOP as its body. */
e04a16fb
AG
14267
14268static tree
14269patch_loop_statement (loop)
14270 tree loop;
14271{
cd9643f7 14272 tree loop_label;
5cbdba64 14273
cd9643f7 14274 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
14275 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14276 return loop;
14277
cd9643f7 14278 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
14279 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14280 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
14281 LABELED_BLOCK_BODY (loop_label) = loop;
14282 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 14283 return loop_label;
e04a16fb
AG
14284}
14285
14286/* 14.13, 14.14: break and continue Statements */
14287
14288/* Build a break or a continue statement. a null NAME indicates an
14289 unlabeled break/continue statement. */
14290
14291static tree
14292build_bc_statement (location, is_break, name)
14293 int location, is_break;
14294 tree name;
14295{
14296 tree break_continue, label_block_expr = NULL_TREE;
14297
14298 if (name)
14299 {
14300 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14301 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14302 /* Null means that we don't have a target for this named
14303 break/continue. In this case, we make the target to be the
14304 label name, so that the error can be reported accuratly in
14305 patch_bc_statement. */
14306 label_block_expr = EXPR_WFL_NODE (name);
14307 }
14308 /* Unlabeled break/continue will be handled during the
14309 break/continue patch operation */
14310 break_continue
14311 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14312
14313 IS_BREAK_STMT_P (break_continue) = is_break;
14314 TREE_SIDE_EFFECTS (break_continue) = 1;
14315 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 14316 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
14317 return break_continue;
14318}
14319
14320/* Verification of a break/continue statement. */
14321
14322static tree
14323patch_bc_statement (node)
14324 tree node;
14325{
14326 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 14327 tree labeled_block = ctxp->current_labeled_block;
b67d701b 14328 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 14329
e04a16fb 14330 /* Having an identifier here means that the target is unknown. */
b635eb2f 14331 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
14332 {
14333 parse_error_context (wfl_operator, "No label definition found for `%s'",
14334 IDENTIFIER_POINTER (bc_label));
14335 return error_mark_node;
14336 }
b635eb2f 14337 if (! IS_BREAK_STMT_P (node))
e04a16fb 14338 {
b635eb2f
PB
14339 /* It's a continue statement. */
14340 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14341 {
b635eb2f
PB
14342 if (labeled_block == NULL_TREE)
14343 {
14344 if (bc_label == NULL_TREE)
14345 parse_error_context (wfl_operator,
14346 "`continue' must be in loop");
14347 else
1504b2b4
APB
14348 parse_error_context
14349 (wfl_operator, "continue label `%s' does not name a loop",
14350 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
14351 return error_mark_node;
14352 }
14353 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14354 == continue_identifier_node)
14355 && (bc_label == NULL_TREE
14356 || TREE_CHAIN (labeled_block) == bc_label))
14357 {
14358 bc_label = labeled_block;
14359 break;
14360 }
e04a16fb 14361 }
e04a16fb 14362 }
b635eb2f 14363 else if (!bc_label)
34f4db93 14364 {
b635eb2f 14365 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 14366 {
b635eb2f
PB
14367 if (labeled_block == NULL_TREE)
14368 {
14369 parse_error_context (wfl_operator,
14370 "`break' must be in loop or switch");
14371 return error_mark_node;
14372 }
14373 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14374 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 14375 || search_loop (target_stmt))
b635eb2f
PB
14376 {
14377 bc_label = labeled_block;
14378 break;
14379 }
e04a16fb 14380 }
e04a16fb
AG
14381 }
14382
b635eb2f 14383 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
14384 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14385
e04a16fb
AG
14386 /* Our break/continue don't return values. */
14387 TREE_TYPE (node) = void_type_node;
14388 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14389 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14390 sometimes, like after a if statement) */
14391 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14392 TREE_SIDE_EFFECTS (node) = 1;
14393 return node;
14394}
14395
14396/* Process the exit expression belonging to a loop. Its type must be
14397 boolean. */
14398
14399static tree
14400patch_exit_expr (node)
14401 tree node;
14402{
14403 tree expression = TREE_OPERAND (node, 0);
14404 TREE_TYPE (node) = error_mark_node;
14405 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14406
14407 /* The type of expression must be boolean */
14408 if (TREE_TYPE (expression) != boolean_type_node)
14409 {
14410 parse_error_context
14411 (wfl_operator,
781b0558 14412 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14413 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14414 return error_mark_node;
14415 }
14416 /* Now we know things are allright, invert the condition, fold and
14417 return */
14418 TREE_OPERAND (node, 0) =
14419 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14420
14421 if (! integer_zerop (TREE_OPERAND (node, 0))
14422 && ctxp->current_loop != NULL_TREE
14423 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14424 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14425 if (! integer_onep (TREE_OPERAND (node, 0)))
14426 CAN_COMPLETE_NORMALLY (node) = 1;
14427
14428
e04a16fb
AG
14429 TREE_TYPE (node) = void_type_node;
14430 return node;
14431}
b67d701b
PB
14432
14433/* 14.9 Switch statement */
14434
14435static tree
14436patch_switch_statement (node)
14437 tree node;
14438{
c877974e 14439 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14440
14441 /* Complete the switch expression */
14442 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14443 se_type = TREE_TYPE (se);
14444 /* The type of the switch expression must be char, byte, short or
14445 int */
2e0f0aff 14446 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
b67d701b
PB
14447 {
14448 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14449 parse_error_context (wfl_operator,
14450 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14451 lang_printable_name (se_type, 0));
b67d701b
PB
14452 /* This is what java_complete_tree will check */
14453 TREE_OPERAND (node, 0) = error_mark_node;
14454 return error_mark_node;
14455 }
14456
15fdcfe9 14457 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14458
14459 /* Ready to return */
15fdcfe9 14460 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14461 {
14462 TREE_TYPE (node) = error_mark_node;
14463 return error_mark_node;
14464 }
14465 TREE_TYPE (node) = void_type_node;
14466 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14467 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14468 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14469 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14470 return node;
14471}
14472
165f37bc 14473/* 14.18 The try/catch statements */
b67d701b 14474
b67d701b 14475static tree
a7d8d81f 14476build_try_statement (location, try_block, catches)
b67d701b 14477 int location;
a7d8d81f
PB
14478 tree try_block, catches;
14479{
14480 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14481 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14482 return node;
b67d701b
PB
14483}
14484
a7d8d81f
PB
14485static tree
14486build_try_finally_statement (location, try_block, finally)
14487 int location;
14488 tree try_block, finally;
b67d701b 14489{
a7d8d81f
PB
14490 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14491 EXPR_WFL_LINECOL (node) = location;
14492 return node;
b67d701b
PB
14493}
14494
14495static tree
14496patch_try_statement (node)
14497 tree node;
14498{
14499 int error_found = 0;
14500 tree try = TREE_OPERAND (node, 0);
14501 /* Exception handlers are considered in left to right order */
14502 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14503 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14504
14505 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14506 to process the next catch clause. We process the catch clause before
14507 the try block so that when processing the try block we can check thrown
14508 exceptions againts the caught type list. */
b67d701b
PB
14509 for (current = catch; current; current = TREE_CHAIN (current))
14510 {
14511 tree carg_decl, carg_type;
14512 tree sub_current, catch_block, catch_clause;
14513 int unreachable;
14514
b67d701b 14515 /* At this point, the structure of the catch clause is
b67d701b
PB
14516 CATCH_EXPR (catch node)
14517 BLOCK (with the decl of the parameter)
14518 COMPOUND_EXPR
7525cc04 14519 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14520 BLOCK (catch clause block)
a7d8d81f
PB
14521 */
14522 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14523 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14524 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14525
14526 /* Catch clauses can't have more than one parameter declared,
14527 but it's already enforced by the grammar. Make sure that the
14528 only parameter of the clause statement in of class Throwable
14529 or a subclass of Throwable, but that was done earlier. The
14530 catch clause parameter type has also been resolved. */
14531
14532 /* Just make sure that the catch clause parameter type inherits
14533 from java.lang.Throwable */
14534 if (!inherits_from_p (carg_type, throwable_type_node))
14535 {
14536 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14537 parse_error_context (wfl_operator,
781b0558 14538 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14539 lang_printable_name (carg_type, 0));
b67d701b
PB
14540 error_found = 1;
14541 continue;
14542 }
14543
14544 /* Partial check for unreachable catch statement: The catch
14545 clause is reachable iff is no earlier catch block A in
14546 the try statement such that the type of the catch
14547 clause's parameter is the same as or a subclass of the
14548 type of A's parameter */
14549 unreachable = 0;
14550 for (sub_current = catch;
14551 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14552 {
14553 tree sub_catch_clause, decl;
a7d8d81f 14554 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14555 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14556
14557 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14558 {
14559 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14560 parse_error_context
781b0558
KG
14561 (wfl_operator,
14562 "`catch' not reached because of the catch clause at line %d",
14563 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14564 unreachable = error_found = 1;
14565 break;
14566 }
14567 }
b67d701b
PB
14568 /* Complete the catch clause block */
14569 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14570 if (catch_block == error_mark_node)
14571 {
14572 error_found = 1;
14573 continue;
14574 }
15fdcfe9
PB
14575 if (CAN_COMPLETE_NORMALLY (catch_block))
14576 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14577 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14578
14579 if (unreachable)
14580 continue;
14581
14582 /* Things to do here: the exception must be thrown */
14583
14584 /* Link this type to the caught type list */
14585 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14586 }
14587
b9f7e36c
APB
14588 PUSH_EXCEPTIONS (caught_type_list);
14589 if ((try = java_complete_tree (try)) == error_mark_node)
14590 error_found = 1;
15fdcfe9
PB
14591 if (CAN_COMPLETE_NORMALLY (try))
14592 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14593 POP_EXCEPTIONS ();
14594
b67d701b
PB
14595 /* Verification ends here */
14596 if (error_found)
14597 return error_mark_node;
14598
14599 TREE_OPERAND (node, 0) = try;
14600 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14601 TREE_TYPE (node) = void_type_node;
14602 return node;
14603}
b9f7e36c
APB
14604
14605/* 14.17 The synchronized Statement */
14606
14607static tree
14608patch_synchronized_statement (node, wfl_op1)
14609 tree node, wfl_op1;
14610{
5a005d9e 14611 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14612 tree block = TREE_OPERAND (node, 1);
5a005d9e 14613
d8fccff5 14614 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14615
14616 if (expr == error_mark_node)
14617 {
14618 block = java_complete_tree (block);
14619 return expr;
14620 }
b9f7e36c
APB
14621
14622 /* The TYPE of expr must be a reference type */
5a005d9e 14623 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14624 {
14625 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14626 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14627 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14628 return error_mark_node;
14629 }
14630
ce6e9147
APB
14631 if (flag_emit_xref)
14632 {
14633 TREE_OPERAND (node, 0) = expr;
14634 TREE_OPERAND (node, 1) = java_complete_tree (block);
14635 CAN_COMPLETE_NORMALLY (node) = 1;
14636 return node;
14637 }
14638
b9f7e36c
APB
14639 /* Generate a try-finally for the synchronized statement, except
14640 that the handler that catches all throw exception calls
14641 _Jv_MonitorExit and then rethrow the exception.
14642 The synchronized statement is then implemented as:
14643 TRY
14644 {
14645 _Jv_MonitorEnter (expression)
14646 synchronized_block
14647 _Jv_MonitorExit (expression)
14648 }
14649 CATCH_ALL
14650 {
14651 e = _Jv_exception_info ();
14652 _Jv_MonitorExit (expression)
14653 Throw (e);
14654 } */
14655
5a005d9e
PB
14656 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14657 BUILD_MONITOR_ENTER (enter, expr_decl);
14658 BUILD_MONITOR_EXIT (exit, expr_decl);
14659 CAN_COMPLETE_NORMALLY (enter) = 1;
14660 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14661 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14662 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14663 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14664 build (COMPOUND_EXPR, NULL_TREE,
14665 build (WITH_CLEANUP_EXPR, NULL_TREE,
14666 build (COMPOUND_EXPR, NULL_TREE,
96847892 14667 assignment, enter),
5a005d9e
PB
14668 NULL_TREE, exit),
14669 block));
14670 node = build_expr_block (node, expr_decl);
14671
14672 return java_complete_tree (node);
b9f7e36c
APB
14673}
14674
14675/* 14.16 The throw Statement */
14676
14677static tree
14678patch_throw_statement (node, wfl_op1)
14679 tree node, wfl_op1;
14680{
14681 tree expr = TREE_OPERAND (node, 0);
14682 tree type = TREE_TYPE (expr);
14683 int unchecked_ok = 0, tryblock_throws_ok = 0;
14684
14685 /* Thrown expression must be assignable to java.lang.Throwable */
14686 if (!try_reference_assignconv (throwable_type_node, expr))
14687 {
14688 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14689 parse_error_context (wfl_operator,
14690 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14691 lang_printable_name (type, 0));
b9f7e36c
APB
14692 /* If the thrown expression was a reference, we further the
14693 compile-time check. */
14694 if (!JREFERENCE_TYPE_P (type))
14695 return error_mark_node;
14696 }
14697
14698 /* At least one of the following must be true */
14699
14700 /* The type of the throw expression is a not checked exception,
14701 i.e. is a unchecked expression. */
c877974e 14702 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14703
c2952b01
APB
14704 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14705 /* An instance can't throw a checked excetion unless that exception
14706 is explicitely declared in the `throws' clause of each
14707 constructor. This doesn't apply to anonymous classes, since they
14708 don't have declared constructors. */
14709 if (!unchecked_ok
14710 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14711 {
14712 tree current;
14713 for (current = TYPE_METHODS (current_class); current;
14714 current = TREE_CHAIN (current))
14715 if (DECL_CONSTRUCTOR_P (current)
14716 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14717 {
14718 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)",
14719 lang_printable_name (TREE_TYPE (expr), 0));
14720 return error_mark_node;
14721 }
14722 }
14723
b9f7e36c
APB
14724 /* Throw is contained in a try statement and at least one catch
14725 clause can receive the thrown expression or the current method is
14726 declared to throw such an exception. Or, the throw statement is
14727 contained in a method or constructor declaration and the type of
14728 the Expression is assignable to at least one type listed in the
14729 throws clause the declaration. */
b9f7e36c 14730 if (!unchecked_ok)
f099f336 14731 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14732 if (!(unchecked_ok || tryblock_throws_ok))
14733 {
14734 /* If there is a surrounding try block that has no matching
14735 clatch clause, report it first. A surrounding try block exits
14736 only if there is something after the list of checked
14737 exception thrown by the current function (if any). */
14738 if (IN_TRY_BLOCK_P ())
781b0558 14739 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 14740 lang_printable_name (type, 0));
b9f7e36c
APB
14741 /* If we have no surrounding try statement and the method doesn't have
14742 any throws, report it now. FIXME */
f099f336
APB
14743
14744 /* We report that the exception can't be throw from a try block
14745 in all circumstances but when the `throw' is inside a static
14746 block. */
b9f7e36c
APB
14747 else if (!EXCEPTIONS_P (currently_caught_type_list)
14748 && !tryblock_throws_ok)
f099f336 14749 {
c2952b01 14750 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14751 parse_error_context (wfl_operator,
14752 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14753 lang_printable_name (type, 0));
14754 else
781b0558
KG
14755 parse_error_context (wfl_operator,
14756 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14757 lang_printable_name (type, 0));
14758 }
b9f7e36c
APB
14759 /* Otherwise, the current method doesn't have the appropriate
14760 throws declaration */
14761 else
781b0558 14762 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14763 lang_printable_name (type, 0));
b9f7e36c
APB
14764 return error_mark_node;
14765 }
14766
ce6e9147 14767 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14768 BUILD_THROW (node, expr);
ce6e9147
APB
14769
14770 /* If doing xrefs, keep the location where the `throw' was seen. */
14771 if (flag_emit_xref)
14772 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14773 return node;
14774}
14775
14776/* Check that exception said to be thrown by method DECL can be
14777 effectively caught from where DECL is invoked. */
14778
14779static void
14780check_thrown_exceptions (location, decl)
14781 int location;
14782 tree decl;
14783{
14784 tree throws;
14785 /* For all the unchecked exceptions thrown by DECL */
14786 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14787 throws = TREE_CHAIN (throws))
0a2138e2 14788 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14789 {
3e78f871
PB
14790#if 1
14791 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14792 if (DECL_NAME (decl) == get_identifier ("clone"))
14793 continue;
14794#endif
b9f7e36c 14795 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14796 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14797 parse_error_context
14798 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14799 lang_printable_name (TREE_VALUE (throws), 0));
14800 else
14801 {
14802 parse_error_context
781b0558 14803 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14804 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14805 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14806 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14807 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14808 }
b9f7e36c
APB
14809 }
14810}
14811
c877974e 14812/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14813 try-catch blocks, OR is listed in the `throws' clause of the
14814 current method. */
14815
14816static int
0a2138e2 14817check_thrown_exceptions_do (exception)
b9f7e36c
APB
14818 tree exception;
14819{
14820 tree list = currently_caught_type_list;
c877974e 14821 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14822 /* First, all the nested try-catch-finally at that stage. The
14823 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14824 if (IS_UNCHECKED_EXCEPTION_P (exception))
14825 return 1;
b9f7e36c
APB
14826 while (list)
14827 {
14828 tree caught;
14829 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14830 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14831 return 1;
14832 list = TREE_CHAIN (list);
14833 }
14834 return 0;
14835}
14836
14837static void
14838purge_unchecked_exceptions (mdecl)
14839 tree mdecl;
14840{
14841 tree throws = DECL_FUNCTION_THROWS (mdecl);
14842 tree new = NULL_TREE;
14843
14844 while (throws)
14845 {
14846 tree next = TREE_CHAIN (throws);
c877974e 14847 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14848 {
14849 TREE_CHAIN (throws) = new;
14850 new = throws;
14851 }
14852 throws = next;
14853 }
14854 /* List is inverted here, but it doesn't matter */
14855 DECL_FUNCTION_THROWS (mdecl) = new;
14856}
22eed1e6
APB
14857
14858/* 15.24 Conditional Operator ?: */
14859
14860static tree
14861patch_conditional_expr (node, wfl_cond, wfl_op1)
14862 tree node, wfl_cond, wfl_op1;
14863{
14864 tree cond = TREE_OPERAND (node, 0);
14865 tree op1 = TREE_OPERAND (node, 1);
14866 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14867 tree resulting_type = NULL_TREE;
ac825856 14868 tree t1, t2, patched;
22eed1e6
APB
14869 int error_found = 0;
14870
ac825856
APB
14871 /* Operands of ?: might be StringBuffers crafted as a result of a
14872 string concatenation. Obtain a descent operand here. */
14873 if ((patched = patch_string (op1)))
14874 TREE_OPERAND (node, 1) = op1 = patched;
14875 if ((patched = patch_string (op2)))
14876 TREE_OPERAND (node, 2) = op2 = patched;
14877
14878 t1 = TREE_TYPE (op1);
14879 t2 = TREE_TYPE (op2);
14880
22eed1e6
APB
14881 /* The first expression must be a boolean */
14882 if (TREE_TYPE (cond) != boolean_type_node)
14883 {
14884 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14885 parse_error_context (wfl_operator,
14886 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14887 lang_printable_name (TREE_TYPE (cond), 0));
14888 error_found = 1;
14889 }
14890
14891 /* Second and third can be numeric, boolean (i.e. primitive),
14892 references or null. Anything else results in an error */
14893 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14894 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14895 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14896 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14897 error_found = 1;
14898
14899 /* Determine the type of the conditional expression. Same types are
14900 easy to deal with */
14901 else if (t1 == t2)
14902 resulting_type = t1;
14903
14904 /* There are different rules for numeric types */
14905 else if (JNUMERIC_TYPE_P (t1))
14906 {
14907 /* if byte/short found, the resulting type is short */
14908 if ((t1 == byte_type_node && t2 == short_type_node)
14909 || (t1 == short_type_node && t2 == byte_type_node))
14910 resulting_type = short_type_node;
14911
14912 /* If t1 is a constant int and t2 is of type byte, short or char
14913 and t1's value fits in t2, then the resulting type is t2 */
14914 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14915 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14916 resulting_type = t2;
14917
14918 /* If t2 is a constant int and t1 is of type byte, short or char
14919 and t2's value fits in t1, then the resulting type is t1 */
14920 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14921 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14922 resulting_type = t1;
14923
14924 /* Otherwise, binary numeric promotion is applied and the
14925 resulting type is the promoted type of operand 1 and 2 */
14926 else
93024893 14927 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14928 &TREE_OPERAND (node, 1),
14929 &TREE_OPERAND (node, 2));
14930 }
14931
14932 /* Cases of a reference and a null type */
14933 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14934 resulting_type = t1;
14935
14936 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14937 resulting_type = t2;
14938
14939 /* Last case: different reference types. If a type can be converted
14940 into the other one by assignment conversion, the latter
14941 determines the type of the expression */
14942 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14943 resulting_type = promote_type (t1);
14944
14945 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14946 resulting_type = promote_type (t2);
14947
14948 /* If we don't have any resulting type, we're in trouble */
14949 if (!resulting_type)
14950 {
c2e3db92 14951 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14952 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14953 parse_error_context (wfl_operator,
14954 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14955 t, lang_printable_name (t2, 0));
22eed1e6
APB
14956 free (t);
14957 error_found = 1;
14958 }
14959
14960 if (error_found)
14961 {
14962 TREE_TYPE (node) = error_mark_node;
14963 return error_mark_node;
14964 }
14965
14966 TREE_TYPE (node) = resulting_type;
14967 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14968 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14969 return node;
14970}
ac825856 14971
5b09b33e
PB
14972/* Try to constant fold NODE.
14973 If NODE is not a constant expression, return NULL_EXPR.
14974 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14975
14976static tree
14977fold_constant_for_init (node, context)
14978 tree node;
14979 tree context;
14980{
14981 tree op0, op1, val;
14982 enum tree_code code = TREE_CODE (node);
14983
ee97d354 14984 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
5b09b33e 14985 return node;
93024893 14986
5b09b33e
PB
14987 switch (code)
14988 {
5b09b33e
PB
14989 case PLUS_EXPR:
14990 case MINUS_EXPR:
bc3ca41b
PB
14991 case MULT_EXPR:
14992 case TRUNC_MOD_EXPR:
14993 case RDIV_EXPR:
5b09b33e
PB
14994 case LSHIFT_EXPR:
14995 case RSHIFT_EXPR:
14996 case URSHIFT_EXPR:
14997 case BIT_AND_EXPR:
14998 case BIT_XOR_EXPR:
14999 case BIT_IOR_EXPR:
5b09b33e
PB
15000 case TRUTH_ANDIF_EXPR:
15001 case TRUTH_ORIF_EXPR:
15002 case EQ_EXPR:
15003 case NE_EXPR:
15004 case GT_EXPR:
15005 case GE_EXPR:
15006 case LT_EXPR:
15007 case LE_EXPR:
15008 op0 = TREE_OPERAND (node, 0);
15009 op1 = TREE_OPERAND (node, 1);
15010 val = fold_constant_for_init (op0, context);
15011 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15012 return NULL_TREE;
15013 TREE_OPERAND (node, 0) = val;
15014 val = fold_constant_for_init (op1, context);
15015 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15016 return NULL_TREE;
15017 TREE_OPERAND (node, 1) = val;
15018 return patch_binop (node, op0, op1);
15019
15020 case UNARY_PLUS_EXPR:
15021 case NEGATE_EXPR:
15022 case TRUTH_NOT_EXPR:
15023 case BIT_NOT_EXPR:
15024 case CONVERT_EXPR:
15025 op0 = TREE_OPERAND (node, 0);
15026 val = fold_constant_for_init (op0, context);
15027 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15028 return NULL_TREE;
15029 TREE_OPERAND (node, 0) = val;
5a005d9e 15030 return patch_unaryop (node, op0);
5b09b33e
PB
15031 break;
15032
15033 case COND_EXPR:
15034 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15035 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15036 return NULL_TREE;
15037 TREE_OPERAND (node, 0) = val;
15038 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15039 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15040 return NULL_TREE;
15041 TREE_OPERAND (node, 1) = val;
15042 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15043 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15044 return NULL_TREE;
15045 TREE_OPERAND (node, 2) = val;
15046 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15047 : TREE_OPERAND (node, 2);
15048
15049 case VAR_DECL:
8576f094
APB
15050 case FIELD_DECL:
15051 if (! FIELD_FINAL (node)
5b09b33e
PB
15052 || DECL_INITIAL (node) == NULL_TREE)
15053 return NULL_TREE;
15054 val = DECL_INITIAL (node);
15055 /* Guard against infinite recursion. */
15056 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 15057 val = fold_constant_for_init (val, node);
5b09b33e
PB
15058 DECL_INITIAL (node) = val;
15059 return val;
15060
15061 case EXPR_WITH_FILE_LOCATION:
15062 /* Compare java_complete_tree and resolve_expression_name. */
15063 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15064 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15065 {
15066 tree name = EXPR_WFL_NODE (node);
15067 tree decl;
15068 if (PRIMARY_P (node))
15069 return NULL_TREE;
15070 else if (! QUALIFIED_P (name))
15071 {
15072 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
15073 if (decl == NULL_TREE
15074 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
15075 return NULL_TREE;
15076 return fold_constant_for_init (decl, decl);
15077 }
15078 else
15079 {
5b09b33e
PB
15080 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15081 qualify_ambiguous_name (node);
15082 if (resolve_field_access (node, &decl, NULL)
15083 && decl != NULL_TREE)
15084 return fold_constant_for_init (decl, decl);
5b09b33e
PB
15085 return NULL_TREE;
15086 }
15087 }
15088 else
15089 {
15090 op0 = TREE_OPERAND (node, 0);
15091 val = fold_constant_for_init (op0, context);
15092 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15093 return NULL_TREE;
15094 TREE_OPERAND (node, 0) = val;
15095 return val;
15096 }
15097
bc3ca41b
PB
15098#ifdef USE_COMPONENT_REF
15099 case IDENTIFIER:
15100 case COMPONENT_REF:
15101 ?;
15102#endif
15103
5b09b33e
PB
15104 default:
15105 return NULL_TREE;
15106 }
15107}
bc3ca41b
PB
15108
15109#ifdef USE_COMPONENT_REF
15110/* Context is 'T' for TypeName, 'P' for PackageName,
15111 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15112
15113tree
15114resolve_simple_name (name, context)
15115 tree name;
15116 int context;
15117{
15118}
15119
15120tree
15121resolve_qualified_name (name, context)
15122 tree name;
15123 int context;
15124{
15125}
15126#endif
This page took 2.525184 seconds and 5 git commands to generate.