]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
typo
[gcc.git] / gcc / java / parse.y
CommitLineData
e04a16fb
AG
1/* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
df32d2ce 3 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
e04a16fb
AG
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.
22
23Java and all Java-based marks are trademarks or registered trademarks
24of Sun Microsystems, Inc. in the United States and other countries.
25The Free Software Foundation is independent of Sun Microsystems, Inc. */
26
27/* This file parses java source code and issues a tree node image
28suitable for code generation (byte code and targeted CPU assembly
29language).
30
31The grammar conforms to the Java grammar described in "The Java(TM)
32Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
331996, ISBN 0-201-63451-1"
34
35The following modifications were brought to the original grammar:
36
37method_body: added the rule '| block SC_TK'
e04a16fb
AG
38static_initializer: added the rule 'static block SC_TK'.
39
40Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43statement_nsi: 'nsi' should be read no_short_if.
44
45Some rules have been modified to support JDK1.1 inner classes
46definitions and other extensions. */
47
48%{
e04a16fb 49#include "config.h"
36635152
GS
50#include "system.h"
51#include <dirent.h>
e04a16fb
AG
52#include "tree.h"
53#include "rtl.h"
54#include "obstack.h"
0a2138e2 55#include "toplev.h"
e04a16fb
AG
56#include "flags.h"
57#include "java-tree.h"
58#include "jcf.h"
59#include "lex.h"
60#include "parse.h"
61#include "zipfile.h"
5e942c50 62#include "convert.h"
63a212ed 63#include "buffer.h"
f099f336 64#include "xref.h"
b384405b 65#include "function.h"
138657ec 66#include "except.h"
0ae70c6a 67#include "defaults.h"
e04a16fb 68
c2952b01
APB
69#ifndef DIR_SEPARATOR
70#define DIR_SEPARATOR '/'
71#endif
72
82371d41 73/* Local function prototypes */
df32d2ce
KG
74static char *java_accstring_lookup PARAMS ((int));
75static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
df32d2ce
KG
77static tree create_class PARAMS ((int, tree, tree, tree));
78static tree create_interface PARAMS ((int, tree, tree));
c2952b01 79static void end_class_declaration PARAMS ((int));
df32d2ce
KG
80static tree find_field PARAMS ((tree, tree));
81static tree lookup_field_wrapper PARAMS ((tree, tree));
82static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83static void register_fields PARAMS ((int, tree, tree));
c2952b01 84static tree parser_qualified_classname PARAMS ((int, tree));
df32d2ce
KG
85static int parser_check_super PARAMS ((tree, tree, tree));
86static int parser_check_super_interface PARAMS ((tree, tree, tree));
87static void check_modifiers_consistency PARAMS ((int));
88static tree lookup_cl PARAMS ((tree));
89static tree lookup_java_method2 PARAMS ((tree, tree, int));
90static tree method_header PARAMS ((int, tree, tree, tree));
91static void fix_method_argument_names PARAMS ((tree ,tree));
92static tree method_declarator PARAMS ((tree, tree));
93static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
d4476be2 94 ATTRIBUTE_PRINTF_2;
df32d2ce
KG
95static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
96static void parse_ctor_invocation_error PARAMS ((void));
97static tree parse_jdk1_1_error PARAMS ((const char *));
98static void complete_class_report_errors PARAMS ((jdep *));
99static int process_imports PARAMS ((void));
100static void read_import_dir PARAMS ((tree));
101static int find_in_imports_on_demand PARAMS ((tree));
102static int find_in_imports PARAMS ((tree));
103static int check_pkg_class_access PARAMS ((tree, tree));
104static tree resolve_package PARAMS ((tree, tree *));
105static tree lookup_package_type PARAMS ((const char *, int));
106static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
c2952b01 107static tree resolve_class PARAMS ((tree, tree, tree, tree));
df32d2ce
KG
108static void declare_local_variables PARAMS ((int, tree, tree));
109static void source_start_java_method PARAMS ((tree));
110static void source_end_java_method PARAMS ((void));
111static void expand_start_java_method PARAMS ((tree));
112static tree find_name_in_single_imports PARAMS ((tree));
113static void check_abstract_method_header PARAMS ((tree));
114static tree lookup_java_interface_method2 PARAMS ((tree, tree));
115static tree resolve_expression_name PARAMS ((tree, tree *));
c2952b01 116static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
df32d2ce 117static int check_class_interface_creation PARAMS ((int, int, tree,
82371d41 118 tree, tree, tree));
df32d2ce 119static tree patch_method_invocation PARAMS ((tree, tree, tree,
89e09b9a 120 int *, tree *));
df32d2ce
KG
121static int breakdown_qualified PARAMS ((tree *, tree *, tree));
122static tree resolve_and_layout PARAMS ((tree, tree));
123static tree resolve_no_layout PARAMS ((tree, tree));
124static int invocation_mode PARAMS ((tree, int));
125static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
82371d41 126 tree, tree));
df32d2ce 127static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
1982388a 128 tree *, tree *));
df32d2ce
KG
129static tree find_most_specific_methods_list PARAMS ((tree));
130static int argument_types_convertible PARAMS ((tree, tree));
131static tree patch_invoke PARAMS ((tree, tree, tree));
c2952b01 132static int maybe_use_access_method PARAMS ((int, tree *, tree *));
df32d2ce
KG
133static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
134static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
135static tree obtain_incomplete_type PARAMS ((tree));
136static tree java_complete_lhs PARAMS ((tree));
137static tree java_complete_tree PARAMS ((tree));
c2952b01 138static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
df32d2ce
KG
139static void java_complete_expand_method PARAMS ((tree));
140static int unresolved_type_p PARAMS ((tree, tree *));
141static void create_jdep_list PARAMS ((struct parser_ctxt *));
142static tree build_expr_block PARAMS ((tree, tree));
143static tree enter_block PARAMS ((void));
144static tree enter_a_block PARAMS ((tree));
145static tree exit_block PARAMS ((void));
146static tree lookup_name_in_blocks PARAMS ((tree));
147static void maybe_absorb_scoping_blocks PARAMS ((void));
148static tree build_method_invocation PARAMS ((tree, tree));
149static tree build_new_invocation PARAMS ((tree, tree));
150static tree build_assignment PARAMS ((int, int, tree, tree));
151static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
152static int check_final_assignment PARAMS ((tree ,tree));
153static tree patch_assignment PARAMS ((tree, tree, tree ));
154static tree patch_binop PARAMS ((tree, tree, tree));
155static tree build_unaryop PARAMS ((int, int, tree));
156static tree build_incdec PARAMS ((int, int, tree, int));
157static tree patch_unaryop PARAMS ((tree, tree));
158static tree build_cast PARAMS ((int, tree, tree));
159static tree build_null_of_type PARAMS ((tree));
160static tree patch_cast PARAMS ((tree, tree));
161static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
162static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
163static int valid_cast_to_p PARAMS ((tree, tree));
164static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
165static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
166static tree try_reference_assignconv PARAMS ((tree, tree));
167static tree build_unresolved_array_type PARAMS ((tree));
168static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
169static tree build_array_ref PARAMS ((int, tree, tree));
170static tree patch_array_ref PARAMS ((tree));
171static tree make_qualified_name PARAMS ((tree, tree, int));
172static tree merge_qualified_name PARAMS ((tree, tree));
173static tree make_qualified_primary PARAMS ((tree, tree, int));
174static int resolve_qualified_expression_name PARAMS ((tree, tree *,
82371d41 175 tree *, tree *));
df32d2ce 176static void qualify_ambiguous_name PARAMS ((tree));
df32d2ce
KG
177static tree resolve_field_access PARAMS ((tree, tree *, tree *));
178static tree build_newarray_node PARAMS ((tree, tree, int));
179static tree patch_newarray PARAMS ((tree));
180static tree resolve_type_during_patch PARAMS ((tree));
181static tree build_this PARAMS ((int));
c2952b01 182static tree build_wfl_wrap PARAMS ((tree));
df32d2ce
KG
183static tree build_return PARAMS ((int, tree));
184static tree patch_return PARAMS ((tree));
185static tree maybe_access_field PARAMS ((tree, tree, tree));
186static int complete_function_arguments PARAMS ((tree));
c2952b01
APB
187static int check_for_static_method_reference PARAMS ((tree, tree, tree,
188 tree, tree));
df32d2ce
KG
189static int not_accessible_p PARAMS ((tree, tree, int));
190static void check_deprecation PARAMS ((tree, tree));
191static int class_in_current_package PARAMS ((tree));
192static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
193static tree patch_if_else_statement PARAMS ((tree));
194static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
195static tree add_stmt_to_block PARAMS ((tree, tree, tree));
196static tree patch_exit_expr PARAMS ((tree));
197static tree build_labeled_block PARAMS ((int, tree));
198static tree finish_labeled_statement PARAMS ((tree, tree));
199static tree build_bc_statement PARAMS ((int, int, tree));
200static tree patch_bc_statement PARAMS ((tree));
201static tree patch_loop_statement PARAMS ((tree));
202static tree build_new_loop PARAMS ((tree));
203static tree build_loop_body PARAMS ((int, tree, int));
204static tree finish_loop_body PARAMS ((int, tree, tree, int));
205static tree build_debugable_stmt PARAMS ((int, tree));
206static tree finish_for_loop PARAMS ((int, tree, tree, tree));
207static tree patch_switch_statement PARAMS ((tree));
208static tree string_constant_concatenation PARAMS ((tree, tree));
209static tree build_string_concatenation PARAMS ((tree, tree));
210static tree patch_string_cst PARAMS ((tree));
211static tree patch_string PARAMS ((tree));
212static tree build_try_statement PARAMS ((int, tree, tree));
213static tree build_try_finally_statement PARAMS ((int, tree, tree));
214static tree patch_try_statement PARAMS ((tree));
215static tree patch_synchronized_statement PARAMS ((tree, tree));
216static tree patch_throw_statement PARAMS ((tree, tree));
217static void check_thrown_exceptions PARAMS ((int, tree));
218static int check_thrown_exceptions_do PARAMS ((tree));
219static void purge_unchecked_exceptions PARAMS ((tree));
220static void check_throws_clauses PARAMS ((tree, tree, tree));
221static void finish_method_declaration PARAMS ((tree));
222static tree build_super_invocation PARAMS ((tree));
223static int verify_constructor_circularity PARAMS ((tree, tree));
224static char *constructor_circularity_msg PARAMS ((tree, tree));
225static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
82371d41 226 int, int));
df32d2ce
KG
227static const char *get_printable_method_name PARAMS ((tree));
228static tree patch_conditional_expr PARAMS ((tree, tree, tree));
c2952b01
APB
229static tree generate_finit PARAMS ((tree));
230static void add_instance_initializer PARAMS ((tree));
df32d2ce 231static void fix_constructors PARAMS ((tree));
c2952b01
APB
232static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
233 tree, int *));
234static void craft_constructor PARAMS ((tree, tree));
235static int verify_constructor_super PARAMS ((tree));
df32d2ce
KG
236static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
237static void start_artificial_method_body PARAMS ((tree));
238static void end_artificial_method_body PARAMS ((tree));
239static int check_method_redefinition PARAMS ((tree, tree));
240static int reset_method_name PARAMS ((tree));
165f37bc 241static int check_method_types_complete PARAMS ((tree));
df32d2ce
KG
242static void java_check_regular_methods PARAMS ((tree));
243static void java_check_abstract_methods PARAMS ((tree));
244static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
245static void unreachable_stmt_error PARAMS ((tree));
246static tree find_expr_with_wfl PARAMS ((tree));
247static void missing_return_error PARAMS ((tree));
248static tree build_new_array_init PARAMS ((int, tree));
249static tree patch_new_array_init PARAMS ((tree, tree));
250static tree maybe_build_array_element_wfl PARAMS ((tree));
251static int array_constructor_check_entry PARAMS ((tree, tree));
252static const char *purify_type_name PARAMS ((const char *));
253static tree fold_constant_for_init PARAMS ((tree, tree));
254static tree strip_out_static_field_access_decl PARAMS ((tree));
255static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
256static void static_ref_err PARAMS ((tree, tree, tree));
257static void parser_add_interface PARAMS ((tree, tree, tree));
258static void add_superinterfaces PARAMS ((tree, tree));
259static tree jdep_resolve_class PARAMS ((jdep *));
260static int note_possible_classname PARAMS ((const char *, int));
c2952b01
APB
261static void java_complete_expand_classes PARAMS ((void));
262static void java_complete_expand_class PARAMS ((tree));
263static void java_complete_expand_methods PARAMS ((tree));
df32d2ce
KG
264static tree cut_identifier_in_qualified PARAMS ((tree));
265static tree java_stabilize_reference PARAMS ((tree));
266static tree do_unary_numeric_promotion PARAMS ((tree));
267static char * operator_string PARAMS ((tree));
268static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
269static tree merge_string_cste PARAMS ((tree, tree, int));
270static tree java_refold PARAMS ((tree));
271static int java_decl_equiv PARAMS ((tree, tree));
272static int binop_compound_p PARAMS ((enum tree_code));
273static tree search_loop PARAMS ((tree));
274static int labeled_block_contains_loop_p PARAMS ((tree, tree));
275static void check_abstract_method_definitions PARAMS ((int, tree, tree));
276static void java_check_abstract_method_definitions PARAMS ((tree));
277static void java_debug_context_do PARAMS ((int));
c2952b01
APB
278static void java_parser_context_push_initialized_field PARAMS ((void));
279static void java_parser_context_pop_initialized_field PARAMS ((void));
280static tree reorder_static_initialized PARAMS ((tree));
281static void java_parser_context_suspend PARAMS ((void));
282static void java_parser_context_resume PARAMS ((void));
283
284/* JDK 1.1 work. FIXME */
285
286static tree maybe_make_nested_class_name PARAMS ((tree));
287static void make_nested_class_name PARAMS ((tree));
288static void set_nested_class_simple_name_value PARAMS ((tree, int));
289static void link_nested_class_to_enclosing PARAMS ((void));
290static tree find_as_inner_class PARAMS ((tree, tree, tree));
291static tree find_as_inner_class_do PARAMS ((tree, tree));
292static int check_inner_class_redefinition PARAMS ((tree, tree));
293
294static tree build_thisn_assign PARAMS ((void));
295static tree build_current_thisn PARAMS ((tree));
296static tree build_access_to_thisn PARAMS ((tree, tree, int));
297static tree maybe_build_thisn_access_method PARAMS ((tree));
298
299static tree build_outer_field_access PARAMS ((tree, tree));
300static tree build_outer_field_access_methods PARAMS ((tree));
301static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
302 tree, tree));
303static tree build_outer_method_access_method PARAMS ((tree));
304static tree build_new_access_id PARAMS ((void));
305static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
306 tree, tree));
307
308static int outer_field_access_p PARAMS ((tree, tree));
309static int outer_field_expanded_access_p PARAMS ((tree, tree *,
310 tree *, tree *));
311static tree outer_field_access_fix PARAMS ((tree, tree, tree));
312static tree build_incomplete_class_ref PARAMS ((int, tree));
313static tree patch_incomplete_class_ref PARAMS ((tree));
314static tree create_anonymous_class PARAMS ((int, tree));
315static void patch_anonymous_class PARAMS ((tree, tree, tree));
316static void add_inner_class_fields PARAMS ((tree, tree));
82371d41 317
165f37bc
APB
318static tree build_dot_class_method PARAMS ((tree));
319static tree build_dot_class_method_invocation PARAMS ((tree));
c0b864fc 320static void create_new_parser_context PARAMS ((int));
165f37bc 321
e04a16fb
AG
322/* Number of error found so far. */
323int java_error_count;
324/* Number of warning found so far. */
325int java_warning_count;
ce6e9147
APB
326/* Tell when not to fold, when doing xrefs */
327int do_not_fold;
c2952b01
APB
328/* Cyclic inheritance report, as it can be set by layout_class */
329char *cyclic_inheritance_report;
330
331/* Tell when we're within an instance initializer */
332static int in_instance_initializer;
e04a16fb
AG
333
334/* The current parser context */
d4370213 335struct parser_ctxt *ctxp;
e04a16fb 336
d4370213 337/* List of things that were analyzed for which code will be generated */
b351b287
APB
338static struct parser_ctxt *ctxp_for_generation = NULL;
339
e04a16fb
AG
340/* binop_lookup maps token to tree_code. It is used where binary
341 operations are involved and required by the parser. RDIV_EXPR
342 covers both integral/floating point division. The code is changed
343 once the type of both operator is worked out. */
344
345static enum tree_code binop_lookup[19] =
346 {
347 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
348 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
349 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
350 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
351 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
352 };
353#define BINOP_LOOKUP(VALUE) \
354 binop_lookup [((VALUE) - PLUS_TK)% \
355 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
356
5cbdba64
APB
357/* This is the end index for binary operators that can also be used
358 in compound assignements. */
359#define BINOP_COMPOUND_CANDIDATES 11
360
e04a16fb
AG
361/* Fake WFL used to report error message. It is initialized once if
362 needed and reused with it's location information is overriden. */
15fdcfe9 363tree wfl_operator = NULL_TREE;
e04a16fb
AG
364
365/* The "$L" identifier we use to create labels. */
b67d701b
PB
366static tree label_id = NULL_TREE;
367
368/* The "StringBuffer" identifier used for the String `+' operator. */
369static tree wfl_string_buffer = NULL_TREE;
370
371/* The "append" identifier used for String `+' operator. */
372static tree wfl_append = NULL_TREE;
373
374/* The "toString" identifier used for String `+' operator. */
375static tree wfl_to_string = NULL_TREE;
ba179f9f
APB
376
377/* The "java.lang" import qualified name. */
378static tree java_lang_id = NULL_TREE;
09ed0f70 379
c2952b01
APB
380/* The generated `inst$' identifier used for generated enclosing
381 instance/field access functions. */
382static tree inst_id = NULL_TREE;
383
09ed0f70
APB
384/* The "java.lang.Cloneable" qualified name. */
385static tree java_lang_cloneable = NULL_TREE;
f099f336
APB
386
387/* Context and flag for static blocks */
388static tree current_static_block = NULL_TREE;
389
c2952b01
APB
390/* The generated `write_parm_value$' identifier. */
391static tree wpv_id;
392
ee07f4f4
APB
393/* The list of all packages we've seen so far */
394static tree package_list = NULL_TREE;
2884c41e
KG
395
396/* Check modifiers. If one doesn't fit, retrieve it in its declaration
397 line and point it out. */
398/* Should point out the one that don't fit. ASCII/unicode, going
399 backward. FIXME */
400
401#define check_modifiers(__message, __value, __mask) do { \
402 if ((__value) & ~(__mask)) \
403 { \
404 int i, remainder = (__value) & ~(__mask); \
405 for (i = 0; i <= 10; i++) \
406 if ((1 << i) & remainder) \
407 parse_error_context (ctxp->modifier_ctx [i], (__message), \
408 java_accstring_lookup (1 << i)); \
409 } \
410} while (0)
ee07f4f4 411
e04a16fb
AG
412%}
413
414%union {
415 tree node;
416 int sub_token;
417 struct {
418 int token;
419 int location;
420 } operator;
421 int value;
422}
423
9ee9b555
KG
424%{
425#include "lex.c"
426%}
427
e04a16fb
AG
428%pure_parser
429
430/* Things defined here have to match the order of what's in the
431 binop_lookup table. */
432
433%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
434%token LS_TK SRS_TK ZRS_TK
435%token AND_TK XOR_TK OR_TK
436%token BOOL_AND_TK BOOL_OR_TK
437%token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
438
439/* This maps to the same binop_lookup entry than the token above */
440
441%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
442%token REM_ASSIGN_TK
443%token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
444%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
445
446
447/* Modifier TOKEN have to be kept in this order. Don't scramble it */
448
449%token PUBLIC_TK PRIVATE_TK PROTECTED_TK
450%token STATIC_TK FINAL_TK SYNCHRONIZED_TK
451%token VOLATILE_TK TRANSIENT_TK NATIVE_TK
452%token PAD_TK ABSTRACT_TK MODIFIER_TK
453
454/* Keep those two in order, too */
455%token DECR_TK INCR_TK
456
457/* From now one, things can be in any order */
458
459%token DEFAULT_TK IF_TK THROW_TK
460%token BOOLEAN_TK DO_TK IMPLEMENTS_TK
461%token THROWS_TK BREAK_TK IMPORT_TK
462%token ELSE_TK INSTANCEOF_TK RETURN_TK
463%token VOID_TK CATCH_TK INTERFACE_TK
464%token CASE_TK EXTENDS_TK FINALLY_TK
465%token SUPER_TK WHILE_TK CLASS_TK
466%token SWITCH_TK CONST_TK TRY_TK
467%token FOR_TK NEW_TK CONTINUE_TK
468%token GOTO_TK PACKAGE_TK THIS_TK
469
470%token BYTE_TK SHORT_TK INT_TK LONG_TK
471%token CHAR_TK INTEGRAL_TK
472
473%token FLOAT_TK DOUBLE_TK FP_TK
474
475%token ID_TK
476
477%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
478
479%token ASSIGN_ANY_TK ASSIGN_TK
480%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
481
482%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
483%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
484
c2952b01 485%type <value> modifiers MODIFIER_TK final synchronized
e04a16fb
AG
486
487%type <node> super ID_TK identifier
488%type <node> name simple_name qualified_name
c2952b01 489%type <node> type_declaration compilation_unit
e04a16fb
AG
490 field_declaration method_declaration extends_interfaces
491 interfaces interface_type_list
c2952b01 492 class_member_declaration
e04a16fb
AG
493 import_declarations package_declaration
494 type_declarations interface_body
495 interface_member_declaration constant_declaration
496 interface_member_declarations interface_type
497 abstract_method_declaration interface_type_list
498%type <node> class_body_declaration class_member_declaration
499 static_initializer constructor_declaration block
22eed1e6 500%type <node> class_body_declarations constructor_header
e04a16fb
AG
501%type <node> class_or_interface_type class_type class_type_list
502 constructor_declarator explicit_constructor_invocation
b9f7e36c 503%type <node> dim_expr dim_exprs this_or_super throws
e04a16fb
AG
504
505%type <node> variable_declarator_id variable_declarator
506 variable_declarators variable_initializer
22eed1e6 507 variable_initializers constructor_body
ac825856 508 array_initializer
e04a16fb 509
2e5eb5c5 510%type <node> class_body block_end constructor_block_end
e04a16fb
AG
511%type <node> statement statement_without_trailing_substatement
512 labeled_statement if_then_statement label_decl
513 if_then_else_statement while_statement for_statement
514 statement_nsi labeled_statement_nsi do_statement
515 if_then_else_statement_nsi while_statement_nsi
516 for_statement_nsi statement_expression_list for_init
517 for_update statement_expression expression_statement
518 primary_no_new_array expression primary
519 array_creation_expression array_type
520 class_instance_creation_expression field_access
521 method_invocation array_access something_dot_new
522 argument_list postfix_expression while_expression
523 post_increment_expression post_decrement_expression
524 unary_expression_not_plus_minus unary_expression
525 pre_increment_expression pre_decrement_expression
526 unary_expression_not_plus_minus cast_expression
527 multiplicative_expression additive_expression
528 shift_expression relational_expression
529 equality_expression and_expression
530 exclusive_or_expression inclusive_or_expression
531 conditional_and_expression conditional_or_expression
532 conditional_expression assignment_expression
533 left_hand_side assignment for_header for_begin
534 constant_expression do_statement_begin empty_statement
b67d701b 535 switch_statement synchronized_statement throw_statement
f8976021 536 try_statement switch_expression switch_block
15fdcfe9 537 catches catch_clause catch_clause_parameter finally
c2952b01 538 anonymous_class_creation
e04a16fb
AG
539%type <node> return_statement break_statement continue_statement
540
541%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
542%type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
543%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
544%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
545%type <operator> ASSIGN_ANY_TK assignment_operator
546%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
547%token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
548%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
7f10c2e2 549%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
5e942c50 550%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
b9f7e36c
APB
551%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
552%type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
c2952b01 553%type <operator> NEW_TK
e04a16fb
AG
554
555%type <node> method_body
556
557%type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
558 STRING_LIT_TK NULL_TK VOID_TK
559
560%type <node> IF_TK WHILE_TK FOR_TK
561
562%type <node> formal_parameter_list formal_parameter
563 method_declarator method_header
564
c2952b01 565%type <node> primitive_type reference_type type
e04a16fb
AG
566 BOOLEAN_TK INTEGRAL_TK FP_TK
567
c2952b01
APB
568/* Added or modified JDK 1.1 rule types */
569%type <node> type_literals array_type_literal
570
e04a16fb
AG
571%%
572/* 19.2 Production from 2.3: The Syntactic Grammar */
573goal:
574 compilation_unit
575 {}
576;
577
578/* 19.3 Productions from 3: Lexical structure */
579literal:
580 INT_LIT_TK
581| FP_LIT_TK
582| BOOL_LIT_TK
583| CHAR_LIT_TK
584| STRING_LIT_TK
585| NULL_TK
586;
587
588/* 19.4 Productions from 4: Types, Values and Variables */
589type:
590 primitive_type
591| reference_type
592;
593
594primitive_type:
595 INTEGRAL_TK
596| FP_TK
597| BOOLEAN_TK
598;
599
600reference_type:
601 class_or_interface_type
602| array_type
603;
604
605class_or_interface_type:
606 name
607;
608
609class_type:
610 class_or_interface_type /* Default rule */
611;
612
613interface_type:
614 class_or_interface_type
615;
616
617array_type:
618 primitive_type OSB_TK CSB_TK
619 {
620 $$ = build_java_array_type ($1, -1);
621 CLASS_LOADED_P ($$) = 1;
622 }
623| name OSB_TK CSB_TK
624 { $$ = build_unresolved_array_type ($1); }
625| array_type OSB_TK CSB_TK
626 { $$ = build_unresolved_array_type ($1); }
627| primitive_type OSB_TK error
628 {RULE ("']' expected"); RECOVER;}
629| array_type OSB_TK error
630 {RULE ("']' expected"); RECOVER;}
631;
632
633/* 19.5 Productions from 6: Names */
634name:
635 simple_name /* Default rule */
636| qualified_name /* Default rule */
637;
638
639simple_name:
640 identifier /* Default rule */
641;
642
643qualified_name:
644 name DOT_TK identifier
645 { $$ = make_qualified_name ($1, $3, $2.location); }
646;
647
648identifier:
649 ID_TK
650;
651
652/* 19.6: Production from 7: Packages */
653compilation_unit:
654 {$$ = NULL;}
655| package_declaration
656| import_declarations
657| type_declarations
658| package_declaration import_declarations
659| package_declaration type_declarations
660| import_declarations type_declarations
661| package_declaration import_declarations type_declarations
662;
663
664import_declarations:
665 import_declaration
666 {
667 $$ = NULL;
668 }
669| import_declarations import_declaration
670 {
671 $$ = NULL;
672 }
673;
674
675type_declarations:
676 type_declaration
677| type_declarations type_declaration
678;
679
680package_declaration:
681 PACKAGE_TK name SC_TK
ee07f4f4
APB
682 {
683 ctxp->package = EXPR_WFL_NODE ($2);
684 package_list = tree_cons (ctxp->package, NULL, package_list);
685 }
e04a16fb
AG
686| PACKAGE_TK error
687 {yyerror ("Missing name"); RECOVER;}
688| PACKAGE_TK name error
689 {yyerror ("';' expected"); RECOVER;}
690;
691
692import_declaration:
693 single_type_import_declaration
694| type_import_on_demand_declaration
695;
696
697single_type_import_declaration:
698 IMPORT_TK name SC_TK
699 {
700 tree name = EXPR_WFL_NODE ($2), node, last_name;
701 int i = IDENTIFIER_LENGTH (name)-1;
49f48c71 702 const char *last = &IDENTIFIER_POINTER (name)[i];
e04a16fb
AG
703 while (last != IDENTIFIER_POINTER (name))
704 {
705 if (last [0] == '.')
706 break;
707 last--;
708 }
709 last_name = get_identifier (++last);
710 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
711 {
712 tree err = find_name_in_single_imports (last_name);
713 if (err && err != name)
714 parse_error_context
715 ($2, "Ambiguous class: `%s' and `%s'",
716 IDENTIFIER_POINTER (name),
717 IDENTIFIER_POINTER (err));
5e942c50
APB
718 else
719 REGISTER_IMPORT ($2, last_name)
e04a16fb
AG
720 }
721 else
5e942c50 722 REGISTER_IMPORT ($2, last_name);
e04a16fb
AG
723 }
724| IMPORT_TK error
725 {yyerror ("Missing name"); RECOVER;}
726| IMPORT_TK name error
727 {yyerror ("';' expected"); RECOVER;}
728;
729
730type_import_on_demand_declaration:
731 IMPORT_TK name DOT_TK MULT_TK SC_TK
732 {
733 tree name = EXPR_WFL_NODE ($2);
ba179f9f
APB
734 /* Don't import java.lang.* twice. */
735 if (name != java_lang_id)
736 {
737 tree node = build_tree_list ($2, NULL_TREE);
738 read_import_dir ($2);
739 TREE_CHAIN (node) = ctxp->import_demand_list;
740 ctxp->import_demand_list = node;
741 }
e04a16fb
AG
742 }
743| IMPORT_TK name DOT_TK error
744 {yyerror ("'*' expected"); RECOVER;}
745| IMPORT_TK name DOT_TK MULT_TK error
746 {yyerror ("';' expected"); RECOVER;}
747;
748
749type_declaration:
750 class_declaration
c2952b01 751 { end_class_declaration (0); }
e04a16fb 752| interface_declaration
c2952b01 753 { end_class_declaration (0); }
e04a16fb
AG
754| SC_TK
755 { $$ = NULL; }
756| error
757 {
758 YYERROR_NOW;
759 yyerror ("Class or interface declaration expected");
760 }
761;
762
763/* 19.7 Shortened from the original:
764 modifiers: modifier | modifiers modifier
765 modifier: any of public... */
766modifiers:
767 MODIFIER_TK
768 {
769 $$ = (1 << $1);
770 }
771| modifiers MODIFIER_TK
772 {
773 int acc = (1 << $2);
774 if ($$ & acc)
775 parse_error_context
776 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
777 java_accstring_lookup (acc));
778 else
779 {
780 $$ |= acc;
781 }
782 }
783;
784
785/* 19.8.1 Production from $8.1: Class Declaration */
786class_declaration:
787 modifiers CLASS_TK identifier super interfaces
788 { create_class ($1, $3, $4, $5); }
789 class_body
e04a16fb
AG
790| CLASS_TK identifier super interfaces
791 { create_class (0, $2, $3, $4); }
792 class_body
e04a16fb
AG
793| modifiers CLASS_TK error
794 {yyerror ("Missing class name"); RECOVER;}
795| CLASS_TK error
796 {yyerror ("Missing class name"); RECOVER;}
797| CLASS_TK identifier error
0b4d333e
APB
798 {
799 if (!ctxp->class_err) yyerror ("'{' expected");
800 DRECOVER(class1);
801 }
e04a16fb
AG
802| modifiers CLASS_TK identifier error
803 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
804;
805
806super:
807 { $$ = NULL; }
808| EXTENDS_TK class_type
809 { $$ = $2; }
810| EXTENDS_TK class_type error
811 {yyerror ("'{' expected"); ctxp->class_err=1;}
812| EXTENDS_TK error
813 {yyerror ("Missing super class name"); ctxp->class_err=1;}
814;
815
816interfaces:
817 { $$ = NULL_TREE; }
818| IMPLEMENTS_TK interface_type_list
819 { $$ = $2; }
820| IMPLEMENTS_TK error
821 {
822 ctxp->class_err=1;
823 yyerror ("Missing interface name");
824 }
825;
826
827interface_type_list:
828 interface_type
829 {
830 ctxp->interface_number = 1;
831 $$ = build_tree_list ($1, NULL_TREE);
832 }
833| interface_type_list C_TK interface_type
834 {
835 ctxp->interface_number++;
836 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
837 }
838| interface_type_list C_TK error
839 {yyerror ("Missing interface name"); RECOVER;}
840;
841
842class_body:
843 OCB_TK CCB_TK
7f10c2e2
APB
844 {
845 /* Store the location of the `}' when doing xrefs */
846 if (flag_emit_xref)
c2952b01 847 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 848 EXPR_WFL_ADD_COL ($2.location, 1);
c2952b01 849 $$ = GET_CPC ();
7f10c2e2 850 }
e04a16fb 851| OCB_TK class_body_declarations CCB_TK
7f10c2e2
APB
852 {
853 /* Store the location of the `}' when doing xrefs */
854 if (flag_emit_xref)
c2952b01 855 DECL_END_SOURCE_LINE (GET_CPC ()) =
7f10c2e2 856 EXPR_WFL_ADD_COL ($3.location, 1);
c2952b01 857 $$ = GET_CPC ();
7f10c2e2 858 }
e04a16fb
AG
859;
860
861class_body_declarations:
862 class_body_declaration
863| class_body_declarations class_body_declaration
864;
865
866class_body_declaration:
867 class_member_declaration
868| static_initializer
869| constructor_declaration
870| block /* Added, JDK1.1, instance initializer */
c2952b01
APB
871 {
872 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
873 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
874 }
e04a16fb
AG
875;
876
877class_member_declaration:
878 field_declaration
0b4d333e
APB
879| field_declaration SC_TK
880 { $$ = $1; }
e04a16fb
AG
881| method_declaration
882| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
883 { end_class_declaration (1); }
884| interface_declaration /* Added, JDK1.1 inner interfaces */
885 { end_class_declaration (1); }
e04a16fb
AG
886;
887
888/* 19.8.2 Productions from 8.3: Field Declarations */
889field_declaration:
890 type variable_declarators SC_TK
891 { register_fields (0, $1, $2); }
892| modifiers type variable_declarators SC_TK
893 {
e04a16fb
AG
894 check_modifiers
895 ("Illegal modifier `%s' for field declaration",
896 $1, FIELD_MODIFIERS);
897 check_modifiers_consistency ($1);
898 register_fields ($1, $2, $3);
899 }
900;
901
902variable_declarators:
903 /* Should we use build_decl_list () instead ? FIXME */
904 variable_declarator /* Default rule */
905| variable_declarators C_TK variable_declarator
906 { $$ = chainon ($1, $3); }
907| variable_declarators C_TK error
908 {yyerror ("Missing term"); RECOVER;}
909;
910
911variable_declarator:
912 variable_declarator_id
913 { $$ = build_tree_list ($1, NULL_TREE); }
914| variable_declarator_id ASSIGN_TK variable_initializer
915 {
916 if (java_error_count)
917 $3 = NULL_TREE;
918 $$ = build_tree_list
919 ($1, build_assignment ($2.token, $2.location, $1, $3));
920 }
921| variable_declarator_id ASSIGN_TK error
922 {
923 yyerror ("Missing variable initializer");
924 $$ = build_tree_list ($1, NULL_TREE);
925 RECOVER;
926 }
927| variable_declarator_id ASSIGN_TK variable_initializer error
928 {
929 yyerror ("';' expected");
930 $$ = build_tree_list ($1, NULL_TREE);
931 RECOVER;
932 }
933;
934
935variable_declarator_id:
936 identifier
937| variable_declarator_id OSB_TK CSB_TK
c583dd46 938 { $$ = build_unresolved_array_type ($1); }
e04a16fb
AG
939| identifier error
940 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
941| variable_declarator_id OSB_TK error
942 {yyerror ("']' expected"); DRECOVER(vdi);}
943| variable_declarator_id CSB_TK error
944 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
945;
946
947variable_initializer:
948 expression
949| array_initializer
e04a16fb
AG
950;
951
952/* 19.8.3 Productions from 8.4: Method Declarations */
953method_declaration:
954 method_header
955 {
956 current_function_decl = $1;
c2952b01
APB
957 if (current_function_decl
958 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
959 source_start_java_method (current_function_decl);
960 else
961 current_function_decl = NULL_TREE;
e04a16fb
AG
962 }
963 method_body
b635eb2f 964 { finish_method_declaration ($3); }
e04a16fb
AG
965| method_header error
966 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
967;
968
969method_header:
970 type method_declarator throws
b9f7e36c 971 { $$ = method_header (0, $1, $2, $3); }
e04a16fb 972| VOID_TK method_declarator throws
b9f7e36c 973 { $$ = method_header (0, void_type_node, $2, $3); }
e04a16fb 974| modifiers type method_declarator throws
b9f7e36c 975 { $$ = method_header ($1, $2, $3, $4); }
e04a16fb 976| modifiers VOID_TK method_declarator throws
b9f7e36c 977 { $$ = method_header ($1, void_type_node, $3, $4); }
e04a16fb 978| type error
efa0a23f
APB
979 {
980 yyerror ("Invalid method declaration, method name required");
981 RECOVER;
982 }
e04a16fb
AG
983| modifiers type error
984 {RECOVER;}
985| VOID_TK error
986 {yyerror ("Identifier expected"); RECOVER;}
987| modifiers VOID_TK error
988 {yyerror ("Identifier expected"); RECOVER;}
989| modifiers error
990 {
991 yyerror ("Invalid method declaration, return type required");
992 RECOVER;
993 }
994;
995
996method_declarator:
997 identifier OP_TK CP_TK
c2952b01
APB
998 {
999 ctxp->formal_parameter_number = 0;
1000 $$ = method_declarator ($1, NULL_TREE);
1001 }
e04a16fb
AG
1002| identifier OP_TK formal_parameter_list CP_TK
1003 { $$ = method_declarator ($1, $3); }
1004| method_declarator OSB_TK CSB_TK
1005 {
1886c9d8
APB
1006 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1007 TREE_PURPOSE ($1) =
1008 build_unresolved_array_type (TREE_PURPOSE ($1));
1009 parse_warning_context
1010 (wfl_operator,
1011 "Discouraged form of returned type specification");
e04a16fb
AG
1012 }
1013| identifier OP_TK error
1014 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1015| method_declarator OSB_TK error
1016 {yyerror ("']' expected"); RECOVER;}
1017;
1018
1019formal_parameter_list:
1020 formal_parameter
1021 {
1022 ctxp->formal_parameter_number = 1;
1023 }
1024| formal_parameter_list C_TK formal_parameter
1025 {
1026 ctxp->formal_parameter_number += 1;
1027 $$ = chainon ($1, $3);
1028 }
1029| formal_parameter_list C_TK error
c2952b01 1030 { yyerror ("Missing formal parameter term"); RECOVER; }
e04a16fb
AG
1031;
1032
1033formal_parameter:
1034 type variable_declarator_id
1035 {
1036 $$ = build_tree_list ($2, $1);
1037 }
18990de5 1038| final type variable_declarator_id /* Added, JDK1.1 final parms */
5256aa37 1039 {
5256aa37 1040 $$ = build_tree_list ($3, $2);
c2952b01 1041 ARG_FINAL_P ($$) = 1;
5256aa37 1042 }
e04a16fb 1043| type error
f8989a66
APB
1044 {
1045 yyerror ("Missing identifier"); RECOVER;
1046 $$ = NULL_TREE;
1047 }
18990de5 1048| final type error
e04a16fb 1049 {
e04a16fb 1050 yyerror ("Missing identifier"); RECOVER;
f8989a66 1051 $$ = NULL_TREE;
e04a16fb
AG
1052 }
1053;
1054
18990de5
JB
1055final:
1056 modifiers
1057 {
1058 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1059 $1, ACC_FINAL);
1060 if ($1 != ACC_FINAL)
1061 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1062 }
1063;
1064
e04a16fb 1065throws:
b9f7e36c 1066 { $$ = NULL_TREE; }
e04a16fb 1067| THROWS_TK class_type_list
b9f7e36c 1068 { $$ = $2; }
e04a16fb
AG
1069| THROWS_TK error
1070 {yyerror ("Missing class type term"); RECOVER;}
1071;
1072
1073class_type_list:
1074 class_type
c877974e 1075 { $$ = build_tree_list ($1, $1); }
e04a16fb 1076| class_type_list C_TK class_type
c877974e 1077 { $$ = tree_cons ($3, $3, $1); }
e04a16fb
AG
1078| class_type_list C_TK error
1079 {yyerror ("Missing class type term"); RECOVER;}
1080;
1081
1082method_body:
1083 block
1084| block SC_TK
1085| SC_TK
1086 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
1087;
1088
1089/* 19.8.4 Productions from 8.5: Static Initializers */
1090static_initializer:
1091 static block
1092 {
c2952b01
APB
1093 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1094 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1095 }
1096| static block SC_TK /* Shouldn't be here. FIXME */
1097 {
c2952b01
APB
1098 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1099 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
e04a16fb
AG
1100 }
1101;
1102
1103static: /* Test lval.sub_token here */
c2952b01 1104 modifiers
e04a16fb 1105 {
c2952b01
APB
1106 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1107 /* Can't have a static initializer in an innerclass */
1108 if ($1 | ACC_STATIC &&
1109 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1110 parse_error_context
1111 (MODIFIER_WFL (STATIC_TK),
1112 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1113 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
e04a16fb
AG
1114 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1115 }
1116;
1117
1118/* 19.8.5 Productions from 8.6: Constructor Declarations */
e04a16fb 1119constructor_declaration:
22eed1e6 1120 constructor_header
e04a16fb 1121 {
22eed1e6
APB
1122 current_function_decl = $1;
1123 source_start_java_method (current_function_decl);
e04a16fb 1124 }
22eed1e6 1125 constructor_body
b635eb2f 1126 { finish_method_declaration ($3); }
22eed1e6
APB
1127;
1128
1129constructor_header:
1130 constructor_declarator throws
1131 { $$ = method_header (0, NULL_TREE, $1, $2); }
1132| modifiers constructor_declarator throws
1133 { $$ = method_header ($1, NULL_TREE, $2, $3); }
e04a16fb
AG
1134;
1135
1136constructor_declarator:
1137 simple_name OP_TK CP_TK
c2952b01
APB
1138 {
1139 ctxp->formal_parameter_number = 0;
1140 $$ = method_declarator ($1, NULL_TREE);
1141 }
e04a16fb 1142| simple_name OP_TK formal_parameter_list CP_TK
22eed1e6 1143 { $$ = method_declarator ($1, $3); }
e04a16fb
AG
1144;
1145
1146constructor_body:
22eed1e6
APB
1147 /* Unlike regular method, we always need a complete (empty)
1148 body so we can safely perform all the required code
1149 addition (super invocation and field initialization) */
2e5eb5c5 1150 block_begin constructor_block_end
22eed1e6 1151 {
9bbc7d9f 1152 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
22eed1e6
APB
1153 $$ = $2;
1154 }
2e5eb5c5 1155| block_begin explicit_constructor_invocation constructor_block_end
22eed1e6 1156 { $$ = $3; }
2e5eb5c5 1157| block_begin block_statements constructor_block_end
22eed1e6 1158 { $$ = $3; }
2e5eb5c5 1159| block_begin explicit_constructor_invocation block_statements constructor_block_end
22eed1e6 1160 { $$ = $4; }
e04a16fb
AG
1161;
1162
2e5eb5c5
APB
1163constructor_block_end:
1164 block_end
1165| block_end SC_TK
1166
e04a16fb
AG
1167/* Error recovery for that rule moved down expression_statement: rule. */
1168explicit_constructor_invocation:
1169 this_or_super OP_TK CP_TK SC_TK
22eed1e6
APB
1170 {
1171 $$ = build_method_invocation ($1, NULL_TREE);
1172 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1173 $$ = java_method_add_stmt (current_function_decl, $$);
1174 }
e04a16fb 1175| this_or_super OP_TK argument_list CP_TK SC_TK
22eed1e6
APB
1176 {
1177 $$ = build_method_invocation ($1, $3);
1178 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1179 $$ = java_method_add_stmt (current_function_decl, $$);
1180 }
e04a16fb
AG
1181 /* Added, JDK1.1 inner classes. Modified because the rule
1182 'primary' couldn't work. */
1183| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
b67d701b 1184 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb 1185| name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
b67d701b 1186 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
e04a16fb
AG
1187;
1188
1189this_or_super: /* Added, simplifies error diagnostics */
1190 THIS_TK
1191 {
9ee9b555 1192 tree wfl = build_wfl_node (this_identifier_node);
e04a16fb
AG
1193 EXPR_WFL_LINECOL (wfl) = $1.location;
1194 $$ = wfl;
1195 }
1196| SUPER_TK
1197 {
9ee9b555 1198 tree wfl = build_wfl_node (super_identifier_node);
e04a16fb
AG
1199 EXPR_WFL_LINECOL (wfl) = $1.location;
1200 $$ = wfl;
1201 }
1202;
1203
1204/* 19.9 Productions from 9: Interfaces */
1205/* 19.9.1 Productions from 9.1: Interfaces Declarations */
1206interface_declaration:
1207 INTERFACE_TK identifier
1208 { create_interface (0, $2, NULL_TREE); }
1209 interface_body
e04a16fb
AG
1210| modifiers INTERFACE_TK identifier
1211 { create_interface ($1, $3, NULL_TREE); }
1212 interface_body
e04a16fb
AG
1213| INTERFACE_TK identifier extends_interfaces
1214 { create_interface (0, $2, $3); }
1215 interface_body
e04a16fb
AG
1216| modifiers INTERFACE_TK identifier extends_interfaces
1217 { create_interface ($1, $3, $4); }
1218 interface_body
e04a16fb 1219| INTERFACE_TK identifier error
0b4d333e 1220 {yyerror ("'{' expected"); RECOVER;}
e04a16fb 1221| modifiers INTERFACE_TK identifier error
0b4d333e 1222 {yyerror ("'{' expected"); RECOVER;}
e04a16fb
AG
1223;
1224
1225extends_interfaces:
1226 EXTENDS_TK interface_type
1227 {
1228 ctxp->interface_number = 1;
1229 $$ = build_tree_list ($2, NULL_TREE);
1230 }
1231| extends_interfaces C_TK interface_type
1232 {
1233 ctxp->interface_number++;
1234 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1235 }
1236| EXTENDS_TK error
1237 {yyerror ("Invalid interface type"); RECOVER;}
1238| extends_interfaces C_TK error
1239 {yyerror ("Missing term"); RECOVER;}
1240;
1241
1242interface_body:
1243 OCB_TK CCB_TK
1244 { $$ = NULL_TREE; }
1245| OCB_TK interface_member_declarations CCB_TK
1246 { $$ = NULL_TREE; }
1247;
1248
1249interface_member_declarations:
1250 interface_member_declaration
1251| interface_member_declarations interface_member_declaration
1252;
1253
1254interface_member_declaration:
1255 constant_declaration
1256| abstract_method_declaration
1257| class_declaration /* Added, JDK1.1 inner classes */
c2952b01
APB
1258 { end_class_declaration (1); }
1259| interface_declaration /* Added, JDK1.1 inner interfaces */
1260 { end_class_declaration (1); }
e04a16fb
AG
1261;
1262
1263constant_declaration:
1264 field_declaration
1265;
1266
1267abstract_method_declaration:
1268 method_header SC_TK
1269 {
1270 check_abstract_method_header ($1);
1271 current_function_decl = NULL_TREE; /* FIXME ? */
1272 }
1273| method_header error
1274 {yyerror ("';' expected"); RECOVER;}
1275;
1276
1277/* 19.10 Productions from 10: Arrays */
1278array_initializer:
1279 OCB_TK CCB_TK
1179ebc2 1280 { $$ = build_new_array_init ($1.location, NULL_TREE); }
e04a16fb 1281| OCB_TK variable_initializers CCB_TK
f8976021 1282 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb 1283| OCB_TK variable_initializers C_TK CCB_TK
f8976021 1284 { $$ = build_new_array_init ($1.location, $2); }
e04a16fb
AG
1285;
1286
1287variable_initializers:
1288 variable_initializer
f8976021
APB
1289 {
1290 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1291 $1, NULL_TREE);
1292 }
e04a16fb 1293| variable_initializers C_TK variable_initializer
1179ebc2
APB
1294 {
1295 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1296 }
e04a16fb
AG
1297| variable_initializers C_TK error
1298 {yyerror ("Missing term"); RECOVER;}
1299;
1300
1301/* 19.11 Production from 14: Blocks and Statements */
1302block:
1303 OCB_TK CCB_TK
7f10c2e2
APB
1304 {
1305 /* Store the location of the `}' when doing xrefs */
1306 if (current_function_decl && flag_emit_xref)
1307 DECL_END_SOURCE_LINE (current_function_decl) =
1308 EXPR_WFL_ADD_COL ($2.location, 1);
1309 $$ = empty_stmt_node;
1310 }
22eed1e6
APB
1311| block_begin block_statements block_end
1312 { $$ = $3; }
1313;
1314
1315block_begin:
1316 OCB_TK
e04a16fb 1317 { enter_block (); }
22eed1e6
APB
1318;
1319
1320block_end:
e04a16fb
AG
1321 CCB_TK
1322 {
1323 maybe_absorb_scoping_blocks ();
7f10c2e2
APB
1324 /* Store the location of the `}' when doing xrefs */
1325 if (current_function_decl && flag_emit_xref)
1326 DECL_END_SOURCE_LINE (current_function_decl) =
1327 EXPR_WFL_ADD_COL ($1.location, 1);
e04a16fb
AG
1328 $$ = exit_block ();
1329 }
1330;
1331
1332block_statements:
1333 block_statement
1334| block_statements block_statement
1335;
1336
1337block_statement:
1338 local_variable_declaration_statement
1339| statement
15fdcfe9 1340 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1341| class_declaration /* Added, JDK1.1 local classes */
1342 {
1343 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1344 end_class_declaration (1);
1345 }
e04a16fb
AG
1346;
1347
1348local_variable_declaration_statement:
1349 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1350;
1351
1352local_variable_declaration:
1353 type variable_declarators
1354 { declare_local_variables (0, $1, $2); }
a003f638 1355| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1356 { declare_local_variables ($1, $2, $3); }
1357;
1358
1359statement:
1360 statement_without_trailing_substatement
1361| labeled_statement
e04a16fb 1362| if_then_statement
e04a16fb 1363| if_then_else_statement
e04a16fb 1364| while_statement
e04a16fb 1365| for_statement
cd9643f7 1366 { $$ = exit_block (); }
e04a16fb
AG
1367;
1368
1369statement_nsi:
1370 statement_without_trailing_substatement
1371| labeled_statement_nsi
e04a16fb 1372| if_then_else_statement_nsi
e04a16fb 1373| while_statement_nsi
e04a16fb 1374| for_statement_nsi
9dd939b2 1375 { $$ = exit_block (); }
e04a16fb
AG
1376;
1377
1378statement_without_trailing_substatement:
1379 block
e04a16fb 1380| empty_statement
e04a16fb 1381| expression_statement
e04a16fb 1382| switch_statement
e04a16fb 1383| do_statement
e04a16fb 1384| break_statement
e04a16fb 1385| continue_statement
e04a16fb
AG
1386| return_statement
1387| synchronized_statement
e04a16fb 1388| throw_statement
e04a16fb 1389| try_statement
e04a16fb
AG
1390;
1391
1392empty_statement:
1393 SC_TK
9bbc7d9f 1394 { $$ = empty_stmt_node; }
e04a16fb
AG
1395;
1396
1397label_decl:
1398 identifier REL_CL_TK
1399 {
1400 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1401 EXPR_WFL_NODE ($1));
e04a16fb
AG
1402 pushlevel (2);
1403 push_labeled_block ($$);
1404 PUSH_LABELED_BLOCK ($$);
1405 }
1406;
1407
1408labeled_statement:
1409 label_decl statement
b635eb2f 1410 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1411| identifier error
1412 {yyerror ("':' expected"); RECOVER;}
1413;
1414
1415labeled_statement_nsi:
1416 label_decl statement_nsi
b635eb2f 1417 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1418;
1419
1420/* We concentrate here a bunch of error handling rules that we couldn't write
1421 earlier, because expression_statement catches a missing ';'. */
1422expression_statement:
1423 statement_expression SC_TK
1424 {
1425 /* We have a statement. Generate a WFL around it so
1426 we can debug it */
1427 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1428 /* We know we have a statement, so set the debug
1429 info to be eventually generate here. */
1430 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1431 }
1432| error SC_TK
1433 {
1434 if (ctxp->prevent_ese != lineno)
1435 yyerror ("Invalid expression statement");
1436 DRECOVER (expr_stmt);
1437 }
1438| error OCB_TK
1439 {
1440 if (ctxp->prevent_ese != lineno)
1441 yyerror ("Invalid expression statement");
1442 DRECOVER (expr_stmt);
1443 }
1444| error CCB_TK
1445 {
1446 if (ctxp->prevent_ese != lineno)
1447 yyerror ("Invalid expression statement");
1448 DRECOVER (expr_stmt);
1449 }
1450| this_or_super OP_TK error
1451 {yyerror ("')' expected"); RECOVER;}
1452| this_or_super OP_TK CP_TK error
22eed1e6 1453 {
8119c720 1454 parse_ctor_invocation_error ();
22eed1e6
APB
1455 RECOVER;
1456 }
e04a16fb
AG
1457| this_or_super OP_TK argument_list error
1458 {yyerror ("')' expected"); RECOVER;}
1459| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1460 {
8119c720 1461 parse_ctor_invocation_error ();
22eed1e6
APB
1462 RECOVER;
1463 }
e04a16fb
AG
1464| name DOT_TK SUPER_TK error
1465 {yyerror ("'(' expected"); RECOVER;}
1466| name DOT_TK SUPER_TK OP_TK error
1467 {yyerror ("')' expected"); RECOVER;}
1468| name DOT_TK SUPER_TK OP_TK argument_list error
1469 {yyerror ("')' expected"); RECOVER;}
1470| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1471 {yyerror ("';' expected"); RECOVER;}
1472| name DOT_TK SUPER_TK OP_TK CP_TK error
1473 {yyerror ("';' expected"); RECOVER;}
1474;
1475
1476statement_expression:
1477 assignment
1478| pre_increment_expression
e04a16fb 1479| pre_decrement_expression
e04a16fb 1480| post_increment_expression
e04a16fb 1481| post_decrement_expression
e04a16fb
AG
1482| method_invocation
1483| class_instance_creation_expression
e04a16fb
AG
1484;
1485
1486if_then_statement:
1487 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1488 {
1489 $$ = build_if_else_statement ($2.location, $3,
1490 $5, NULL_TREE);
1491 }
e04a16fb
AG
1492| IF_TK error
1493 {yyerror ("'(' expected"); RECOVER;}
1494| IF_TK OP_TK error
1495 {yyerror ("Missing term"); RECOVER;}
1496| IF_TK OP_TK expression error
1497 {yyerror ("')' expected"); RECOVER;}
1498;
1499
1500if_then_else_statement:
1501 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1502 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1503;
1504
1505if_then_else_statement_nsi:
1506 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1507 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1508;
1509
1510switch_statement:
15fdcfe9
PB
1511 switch_expression
1512 {
1513 enter_block ();
1514 }
1515 switch_block
b67d701b 1516 {
15fdcfe9 1517 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1518 I.e. make the last statment also have its own
1519 COMPOUND_EXPR. */
15fdcfe9
PB
1520 maybe_absorb_scoping_blocks ();
1521 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1522 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1523 }
1524;
1525
1526switch_expression:
1527 SWITCH_TK OP_TK expression CP_TK
1528 {
1529 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1530 EXPR_WFL_LINECOL ($$) = $2.location;
1531 }
e04a16fb
AG
1532| SWITCH_TK error
1533 {yyerror ("'(' expected"); RECOVER;}
1534| SWITCH_TK OP_TK error
1535 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1536| SWITCH_TK OP_TK expression CP_TK error
1537 {yyerror ("'{' expected"); RECOVER;}
1538;
1539
f8976021
APB
1540/* Default assignment is there to avoid type node on switch_block
1541 node. */
1542
e04a16fb
AG
1543switch_block:
1544 OCB_TK CCB_TK
f8976021 1545 { $$ = NULL_TREE; }
e04a16fb 1546| OCB_TK switch_labels CCB_TK
f8976021 1547 { $$ = NULL_TREE; }
e04a16fb 1548| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1549 { $$ = NULL_TREE; }
e04a16fb 1550| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1551 { $$ = NULL_TREE; }
e04a16fb
AG
1552;
1553
1554switch_block_statement_groups:
1555 switch_block_statement_group
1556| switch_block_statement_groups switch_block_statement_group
1557;
1558
1559switch_block_statement_group:
15fdcfe9 1560 switch_labels block_statements
e04a16fb
AG
1561;
1562
e04a16fb
AG
1563switch_labels:
1564 switch_label
1565| switch_labels switch_label
1566;
1567
1568switch_label:
1569 CASE_TK constant_expression REL_CL_TK
b67d701b 1570 {
15fdcfe9
PB
1571 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1572 EXPR_WFL_LINECOL (lab) = $1.location;
1573 java_method_add_stmt (current_function_decl, lab);
b67d701b 1574 }
e04a16fb 1575| DEFAULT_TK REL_CL_TK
b67d701b 1576 {
15fdcfe9
PB
1577 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1578 EXPR_WFL_LINECOL (lab) = $1.location;
1579 java_method_add_stmt (current_function_decl, lab);
b67d701b 1580 }
e04a16fb
AG
1581| CASE_TK error
1582 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1583| CASE_TK constant_expression error
1584 {yyerror ("':' expected"); RECOVER;}
1585| DEFAULT_TK error
1586 {yyerror ("':' expected"); RECOVER;}
1587;
1588
1589while_expression:
1590 WHILE_TK OP_TK expression CP_TK
1591 {
1592 tree body = build_loop_body ($2.location, $3, 0);
1593 $$ = build_new_loop (body);
1594 }
1595;
1596
1597while_statement:
1598 while_expression statement
b635eb2f 1599 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1600| WHILE_TK error
1601 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1602| WHILE_TK OP_TK error
1603 {yyerror ("Missing term and ')' expected"); RECOVER;}
1604| WHILE_TK OP_TK expression error
1605 {yyerror ("')' expected"); RECOVER;}
1606;
1607
1608while_statement_nsi:
1609 while_expression statement_nsi
b635eb2f 1610 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1611;
1612
1613do_statement_begin:
1614 DO_TK
1615 {
1616 tree body = build_loop_body (0, NULL_TREE, 1);
1617 $$ = build_new_loop (body);
1618 }
1619 /* Need error handing here. FIXME */
1620;
1621
1622do_statement:
1623 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1624 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1625;
1626
1627for_statement:
1628 for_begin SC_TK expression SC_TK for_update CP_TK statement
b635eb2f 1629 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); }
e04a16fb
AG
1630| for_begin SC_TK SC_TK for_update CP_TK statement
1631 {
b635eb2f 1632 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1633 /* We have not condition, so we get rid of the EXIT_EXPR */
1634 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1635 empty_stmt_node;
e04a16fb
AG
1636 }
1637| for_begin SC_TK error
1638 {yyerror ("Invalid control expression"); RECOVER;}
1639| for_begin SC_TK expression SC_TK error
1640 {yyerror ("Invalid update expression"); RECOVER;}
1641| for_begin SC_TK SC_TK error
1642 {yyerror ("Invalid update expression"); RECOVER;}
1643;
1644
1645for_statement_nsi:
1646 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1647 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1648| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1649 {
b635eb2f 1650 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1651 /* We have not condition, so we get rid of the EXIT_EXPR */
1652 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1653 empty_stmt_node;
e04a16fb
AG
1654 }
1655;
1656
1657for_header:
1658 FOR_TK OP_TK
1659 {
1660 /* This scope defined for local variable that may be
1661 defined within the scope of the for loop */
1662 enter_block ();
1663 }
1664| FOR_TK error
1665 {yyerror ("'(' expected"); DRECOVER(for_1);}
1666| FOR_TK OP_TK error
1667 {yyerror ("Invalid init statement"); RECOVER;}
1668;
1669
1670for_begin:
1671 for_header for_init
1672 {
1673 /* We now declare the loop body. The loop is
1674 declared as a for loop. */
1675 tree body = build_loop_body (0, NULL_TREE, 0);
1676 $$ = build_new_loop (body);
c2952b01 1677 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1678 /* The loop is added to the current block the for
1679 statement is defined within */
1680 java_method_add_stmt (current_function_decl, $$);
1681 }
1682;
1683for_init: /* Can be empty */
9bbc7d9f 1684 { $$ = empty_stmt_node; }
e04a16fb
AG
1685| statement_expression_list
1686 {
1687 /* Init statement recorded within the previously
1688 defined block scope */
1689 $$ = java_method_add_stmt (current_function_decl, $1);
1690 }
1691| local_variable_declaration
1692 {
1693 /* Local variable are recorded within the previously
1694 defined block scope */
1695 $$ = NULL_TREE;
1696 }
1697| statement_expression_list error
1698 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1699;
1700
1701for_update: /* Can be empty */
9bbc7d9f 1702 {$$ = empty_stmt_node;}
e04a16fb
AG
1703| statement_expression_list
1704 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1705;
1706
1707statement_expression_list:
1708 statement_expression
1709 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1710| statement_expression_list C_TK statement_expression
1711 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1712| statement_expression_list C_TK error
1713 {yyerror ("Missing term"); RECOVER;}
1714;
1715
1716break_statement:
1717 BREAK_TK SC_TK
1718 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1719| BREAK_TK identifier SC_TK
1720 { $$ = build_bc_statement ($1.location, 1, $2); }
1721| BREAK_TK error
1722 {yyerror ("Missing term"); RECOVER;}
1723| BREAK_TK identifier error
1724 {yyerror ("';' expected"); RECOVER;}
1725;
1726
1727continue_statement:
1728 CONTINUE_TK SC_TK
1729 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1730| CONTINUE_TK identifier SC_TK
1731 { $$ = build_bc_statement ($1.location, 0, $2); }
1732| CONTINUE_TK error
1733 {yyerror ("Missing term"); RECOVER;}
1734| CONTINUE_TK identifier error
1735 {yyerror ("';' expected"); RECOVER;}
1736;
1737
1738return_statement:
1739 RETURN_TK SC_TK
1740 { $$ = build_return ($1.location, NULL_TREE); }
1741| RETURN_TK expression SC_TK
1742 { $$ = build_return ($1.location, $2); }
1743| RETURN_TK error
1744 {yyerror ("Missing term"); RECOVER;}
1745| RETURN_TK expression error
1746 {yyerror ("';' expected"); RECOVER;}
1747;
1748
1749throw_statement:
1750 THROW_TK expression SC_TK
b9f7e36c
APB
1751 {
1752 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1753 EXPR_WFL_LINECOL ($$) = $1.location;
1754 }
e04a16fb
AG
1755| THROW_TK error
1756 {yyerror ("Missing term"); RECOVER;}
1757| THROW_TK expression error
1758 {yyerror ("';' expected"); RECOVER;}
1759;
1760
1761synchronized_statement:
1762 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1763 {
1764 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1765 EXPR_WFL_LINECOL ($$) =
1766 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1767 }
e04a16fb
AG
1768| synchronized OP_TK expression CP_TK error
1769 {yyerror ("'{' expected"); RECOVER;}
1770| synchronized error
1771 {yyerror ("'(' expected"); RECOVER;}
1772| synchronized OP_TK error CP_TK
1773 {yyerror ("Missing term"); RECOVER;}
1774| synchronized OP_TK error
1775 {yyerror ("Missing term"); RECOVER;}
1776;
1777
b9f7e36c 1778synchronized:
efa0a23f 1779 modifiers
e04a16fb 1780 {
781b0558
KG
1781 check_modifiers (
1782 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1783 $1, ACC_SYNCHRONIZED);
1784 if ($1 != ACC_SYNCHRONIZED)
1785 MODIFIER_WFL (SYNCHRONIZED_TK) =
1786 build_wfl_node (NULL_TREE);
e04a16fb
AG
1787 }
1788;
1789
1790try_statement:
1791 TRY_TK block catches
a7d8d81f 1792 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1793| TRY_TK block finally
a7d8d81f 1794 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1795| TRY_TK block catches finally
2aa11e97
APB
1796 { $$ = build_try_finally_statement
1797 ($1.location, build_try_statement ($1.location,
1798 $2, $3), $4);
1799 }
e04a16fb
AG
1800| TRY_TK error
1801 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1802;
1803
1804catches:
1805 catch_clause
1806| catches catch_clause
b67d701b
PB
1807 {
1808 TREE_CHAIN ($2) = $1;
1809 $$ = $2;
1810 }
e04a16fb
AG
1811;
1812
1813catch_clause:
b67d701b
PB
1814 catch_clause_parameter block
1815 {
1816 java_method_add_stmt (current_function_decl, $2);
1817 exit_block ();
1818 $$ = $1;
1819 }
1820
1821catch_clause_parameter:
1822 CATCH_TK OP_TK formal_parameter CP_TK
1823 {
1824 /* We add a block to define a scope for
1825 formal_parameter (CCBP). The formal parameter is
1826 declared initialized by the appropriate function
1827 call */
1828 tree ccpb = enter_block ();
1829 tree init = build_assignment (ASSIGN_TK, $2.location,
1830 TREE_PURPOSE ($3),
1831 soft_exceptioninfo_call_node);
1832 declare_local_variables (0, TREE_VALUE ($3),
1833 build_tree_list (TREE_PURPOSE ($3),
1834 init));
1835 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1836 EXPR_WFL_LINECOL ($$) = $1.location;
1837 }
e04a16fb 1838| CATCH_TK error
97f30284 1839 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1840| CATCH_TK OP_TK error
97f30284
APB
1841 {
1842 yyerror ("Missing term or ')' expected");
1843 RECOVER; $$ = NULL_TREE;
1844 }
b67d701b 1845| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1846 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1847;
1848
1849finally:
1850 FINALLY_TK block
a7d8d81f 1851 { $$ = $2; }
e04a16fb
AG
1852| FINALLY_TK error
1853 {yyerror ("'{' expected"); RECOVER; }
1854;
1855
1856/* 19.12 Production from 15: Expressions */
1857primary:
1858 primary_no_new_array
1859| array_creation_expression
1860;
1861
1862primary_no_new_array:
1863 literal
1864| THIS_TK
1865 { $$ = build_this ($1.location); }
1866| OP_TK expression CP_TK
1867 {$$ = $2;}
1868| class_instance_creation_expression
1869| field_access
1870| method_invocation
1871| array_access
c2952b01 1872| type_literals
e04a16fb
AG
1873 /* Added, JDK1.1 inner classes. Documentation is wrong
1874 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1875 exist. Used name: instead. */
e04a16fb 1876| name DOT_TK THIS_TK
c2952b01
APB
1877 {
1878 tree wfl = build_wfl_node (this_identifier_node);
1879 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1880 }
e04a16fb
AG
1881| OP_TK expression error
1882 {yyerror ("')' expected"); RECOVER;}
1883| name DOT_TK error
1884 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1885| primitive_type DOT_TK error
1886 {yyerror ("'class' expected" ); RECOVER;}
1887| VOID_TK DOT_TK error
1888 {yyerror ("'class' expected" ); RECOVER;}
1889;
1890
c2952b01
APB
1891/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1892 broke the rule down a bit. */
1893
1894array_type_literal:
1895 primitive_type OSB_TK CSB_TK
1896 {
1897 $$ = build_java_array_type ($1, -1);
1898 CLASS_LOADED_P ($$) = 1;
1899 }
1900| name OSB_TK CSB_TK
1901 { $$ = build_unresolved_array_type ($1); }
1902/* This triggers two reduce/reduce conflict between array_type_literal and
1903 dims. FIXME.
1904| array_type OSB_TK CSB_TK
1905 { $$ = build_unresolved_array_type ($1); }
1906*/
1907;
1908
1909type_literals:
1910 name DOT_TK CLASS_TK
1911 { $$ = build_incomplete_class_ref ($2.location, $1); }
1912| array_type_literal DOT_TK CLASS_TK
1913 { $$ = build_incomplete_class_ref ($2.location, $1); }
1914| primitive_type DOT_TK CLASS_TK
1915 { $$ = build_class_ref ($1); }
1916| VOID_TK DOT_TK CLASS_TK
1917 { $$ = build_class_ref (void_type_node); }
1918;
1919
e04a16fb
AG
1920class_instance_creation_expression:
1921 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1922 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1923| NEW_TK class_type OP_TK CP_TK
b67d701b 1924 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1925| anonymous_class_creation
e04a16fb
AG
1926 /* Added, JDK1.1 inner classes, modified to use name or
1927 primary instead of primary solely which couldn't work in
1928 all situations. */
1929| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1930 {
1931 tree ctor = build_new_invocation ($2, NULL_TREE);
1932 $$ = make_qualified_primary ($1, ctor,
1933 EXPR_WFL_LINECOL ($1));
1934 }
e04a16fb
AG
1935| something_dot_new identifier OP_TK CP_TK class_body
1936| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1937 {
1938 tree ctor = build_new_invocation ($2, $4);
1939 $$ = make_qualified_primary ($1, ctor,
1940 EXPR_WFL_LINECOL ($1));
1941 }
e04a16fb
AG
1942| something_dot_new identifier OP_TK argument_list CP_TK class_body
1943| NEW_TK error SC_TK
1944 {yyerror ("'(' expected"); DRECOVER(new_1);}
1945| NEW_TK class_type error
1946 {yyerror ("'(' expected"); RECOVER;}
1947| NEW_TK class_type OP_TK error
1948 {yyerror ("')' or term expected"); RECOVER;}
1949| NEW_TK class_type OP_TK argument_list error
1950 {yyerror ("')' expected"); RECOVER;}
1951| something_dot_new error
1952 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1953| something_dot_new identifier error
1954 {yyerror ("'(' expected"); RECOVER;}
1955;
1956
c2952b01
APB
1957/* Created after JDK1.1 rules originally added to
1958 class_instance_creation_expression, but modified to use
1959 'class_type' instead of 'TypeName' (type_name) which is mentionned
1960 in the documentation but doesn't exist. */
1961
1962anonymous_class_creation:
1963 NEW_TK class_type OP_TK argument_list CP_TK
1964 { create_anonymous_class ($1.location, $2); }
1965 class_body
1966 {
1967 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1968 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1969
1970 end_class_declaration (1);
1971
1972 /* Now we can craft the new expression */
1973 $$ = build_new_invocation (id, $4);
1974
1975 /* Note that we can't possibly be here if
1976 `class_type' is an interface (in which case the
1977 anonymous class extends Object and implements
1978 `class_type', hence its constructor can't have
1979 arguments.) */
1980
1981 /* Otherwise, the innerclass must feature a
1982 constructor matching `argument_list'. Anonymous
1983 classes are a bit special: it's impossible to
1984 define constructor for them, hence constructors
1985 must be generated following the hints provided by
1986 the `new' expression. Whether a super constructor
1987 of that nature exists or not is to be verified
1988 later on in verify_constructor_super.
1989
1990 It's during the expansion of a `new' statement
1991 refering to an anonymous class that a ctor will
1992 be generated for the anonymous class, with the
1993 right arguments. */
1994
1995 }
1996| NEW_TK class_type OP_TK CP_TK
1997 { create_anonymous_class ($1.location, $2); }
1998 class_body
1999 {
2000 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2001 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2002
2003 end_class_declaration (1);
2004
2005 /* Now we can craft the new expression. The
2006 statement doesn't need to be remember so that a
2007 constructor can be generated, since its signature
2008 is already known. */
2009 $$ = build_new_invocation (id, NULL_TREE);
2010 }
2011;
2012
e04a16fb
AG
2013something_dot_new: /* Added, not part of the specs. */
2014 name DOT_TK NEW_TK
c2952b01 2015 { $$ = $1; }
e04a16fb 2016| primary DOT_TK NEW_TK
c2952b01 2017 { $$ = $1; }
e04a16fb
AG
2018;
2019
2020argument_list:
2021 expression
2022 {
2023 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2024 ctxp->formal_parameter_number = 1;
2025 }
2026| argument_list C_TK expression
2027 {
2028 ctxp->formal_parameter_number += 1;
2029 $$ = tree_cons (NULL_TREE, $3, $1);
2030 }
2031| argument_list C_TK error
2032 {yyerror ("Missing term"); RECOVER;}
2033;
2034
2035array_creation_expression:
2036 NEW_TK primitive_type dim_exprs
2037 { $$ = build_newarray_node ($2, $3, 0); }
2038| NEW_TK class_or_interface_type dim_exprs
2039 { $$ = build_newarray_node ($2, $3, 0); }
2040| NEW_TK primitive_type dim_exprs dims
ba179f9f 2041 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2042| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2043 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2044 /* Added, JDK1.1 anonymous array. Initial documentation rule
2045 modified */
2046| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2047 {
2048 char *sig;
2049 while (CURRENT_OSB (ctxp)--)
2050 obstack_1grow (&temporary_obstack, '[');
2051 sig = obstack_finish (&temporary_obstack);
2052 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2053 $2, get_identifier (sig), $4);
2054 }
e04a16fb 2055| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2056 {
2057 tree type = $2;
2058 while (CURRENT_OSB (ctxp)--)
2059 type = build_java_array_type (type, -1);
2060 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2061 build_pointer_type (type), NULL_TREE, $4);
2062 }
e04a16fb
AG
2063| NEW_TK error CSB_TK
2064 {yyerror ("'[' expected"); DRECOVER ("]");}
2065| NEW_TK error OSB_TK
2066 {yyerror ("']' expected"); RECOVER;}
2067;
2068
2069dim_exprs:
2070 dim_expr
2071 { $$ = build_tree_list (NULL_TREE, $1); }
2072| dim_exprs dim_expr
2073 { $$ = tree_cons (NULL_TREE, $2, $$); }
2074;
2075
2076dim_expr:
2077 OSB_TK expression CSB_TK
2078 {
2079 EXPR_WFL_LINECOL ($2) = $1.location;
2080 $$ = $2;
2081 }
2082| OSB_TK expression error
2083 {yyerror ("']' expected"); RECOVER;}
2084| OSB_TK error
2085 {
2086 yyerror ("Missing term");
2087 yyerror ("']' expected");
2088 RECOVER;
2089 }
2090;
2091
2092dims:
2093 OSB_TK CSB_TK
ba179f9f
APB
2094 {
2095 int allocate = 0;
2096 /* If not initialized, allocate memory for the osb
2097 numbers stack */
2098 if (!ctxp->osb_limit)
2099 {
2100 allocate = ctxp->osb_limit = 32;
2101 ctxp->osb_depth = -1;
2102 }
c2952b01 2103 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2104 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2105 allocate = ctxp->osb_limit << 1;
2106
2107 if (allocate)
2108 {
2109 allocate *= sizeof (int);
2110 if (ctxp->osb_number)
2111 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2112 allocate);
2113 else
2114 ctxp->osb_number = (int *)xmalloc (allocate);
2115 }
2116 ctxp->osb_depth++;
2117 CURRENT_OSB (ctxp) = 1;
2118 }
e04a16fb 2119| dims OSB_TK CSB_TK
ba179f9f 2120 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2121| dims OSB_TK error
2122 { yyerror ("']' expected"); RECOVER;}
2123;
2124
2125field_access:
2126 primary DOT_TK identifier
2127 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2128 /* FIXME - REWRITE TO:
2129 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2130| SUPER_TK DOT_TK identifier
2131 {
2132 tree super_wfl =
9ee9b555 2133 build_wfl_node (super_identifier_node);
e04a16fb
AG
2134 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2135 $$ = make_qualified_name (super_wfl, $3, $2.location);
2136 }
2137| SUPER_TK error
2138 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2139;
2140
2141method_invocation:
2142 name OP_TK CP_TK
2143 { $$ = build_method_invocation ($1, NULL_TREE); }
2144| name OP_TK argument_list CP_TK
2145 { $$ = build_method_invocation ($1, $3); }
2146| primary DOT_TK identifier OP_TK CP_TK
2147 {
22eed1e6
APB
2148 if (TREE_CODE ($1) == THIS_EXPR)
2149 $$ = build_this_super_qualified_invocation
2150 (1, $3, NULL_TREE, 0, $2.location);
2151 else
2152 {
2153 tree invok = build_method_invocation ($3, NULL_TREE);
2154 $$ = make_qualified_primary ($1, invok, $2.location);
2155 }
e04a16fb
AG
2156 }
2157| primary DOT_TK identifier OP_TK argument_list CP_TK
2158 {
22eed1e6
APB
2159 if (TREE_CODE ($1) == THIS_EXPR)
2160 $$ = build_this_super_qualified_invocation
2161 (1, $3, $5, 0, $2.location);
2162 else
2163 {
2164 tree invok = build_method_invocation ($3, $5);
2165 $$ = make_qualified_primary ($1, invok, $2.location);
2166 }
e04a16fb
AG
2167 }
2168| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2169 {
2170 $$ = build_this_super_qualified_invocation
2171 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2172 }
2173| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2174 {
22eed1e6
APB
2175 $$ = build_this_super_qualified_invocation
2176 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2177 }
2178 /* Screws up thing. I let it here until I'm convinced it can
2179 be removed. FIXME
2180| primary DOT_TK error
2181 {yyerror ("'(' expected"); DRECOVER(bad);} */
2182| SUPER_TK DOT_TK error CP_TK
2183 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2184| SUPER_TK DOT_TK error DOT_TK
2185 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2186;
2187
2188array_access:
2189 name OSB_TK expression CSB_TK
2190 { $$ = build_array_ref ($2.location, $1, $3); }
2191| primary_no_new_array OSB_TK expression CSB_TK
2192 { $$ = build_array_ref ($2.location, $1, $3); }
2193| name OSB_TK error
2194 {
2195 yyerror ("Missing term and ']' expected");
2196 DRECOVER(array_access);
2197 }
2198| name OSB_TK expression error
2199 {
2200 yyerror ("']' expected");
2201 DRECOVER(array_access);
2202 }
2203| primary_no_new_array OSB_TK error
2204 {
2205 yyerror ("Missing term and ']' expected");
2206 DRECOVER(array_access);
2207 }
2208| primary_no_new_array OSB_TK expression error
2209 {
2210 yyerror ("']' expected");
2211 DRECOVER(array_access);
2212 }
2213;
2214
2215postfix_expression:
2216 primary
2217| name
2218| post_increment_expression
2219| post_decrement_expression
2220;
2221
2222post_increment_expression:
2223 postfix_expression INCR_TK
2224 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2225;
2226
2227post_decrement_expression:
2228 postfix_expression DECR_TK
2229 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2230;
2231
2232unary_expression:
2233 pre_increment_expression
2234| pre_decrement_expression
2235| PLUS_TK unary_expression
2236 {$$ = build_unaryop ($1.token, $1.location, $2); }
2237| MINUS_TK unary_expression
2238 {$$ = build_unaryop ($1.token, $1.location, $2); }
2239| unary_expression_not_plus_minus
2240| PLUS_TK error
2241 {yyerror ("Missing term"); RECOVER}
2242| MINUS_TK error
2243 {yyerror ("Missing term"); RECOVER}
2244;
2245
2246pre_increment_expression:
2247 INCR_TK unary_expression
2248 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2249| INCR_TK error
2250 {yyerror ("Missing term"); RECOVER}
2251;
2252
2253pre_decrement_expression:
2254 DECR_TK unary_expression
2255 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2256| DECR_TK error
2257 {yyerror ("Missing term"); RECOVER}
2258;
2259
2260unary_expression_not_plus_minus:
2261 postfix_expression
2262| NOT_TK unary_expression
2263 {$$ = build_unaryop ($1.token, $1.location, $2); }
2264| NEG_TK unary_expression
2265 {$$ = build_unaryop ($1.token, $1.location, $2); }
2266| cast_expression
2267| NOT_TK error
2268 {yyerror ("Missing term"); RECOVER}
2269| NEG_TK error
2270 {yyerror ("Missing term"); RECOVER}
2271;
2272
2273cast_expression: /* Error handling here is potentially weak */
2274 OP_TK primitive_type dims CP_TK unary_expression
2275 {
2276 tree type = $2;
ba179f9f 2277 while (CURRENT_OSB (ctxp)--)
e04a16fb 2278 type = build_java_array_type (type, -1);
ba179f9f 2279 ctxp->osb_depth--;
e04a16fb
AG
2280 $$ = build_cast ($1.location, type, $5);
2281 }
2282| OP_TK primitive_type CP_TK unary_expression
2283 { $$ = build_cast ($1.location, $2, $4); }
2284| OP_TK expression CP_TK unary_expression_not_plus_minus
2285 { $$ = build_cast ($1.location, $2, $4); }
2286| OP_TK name dims CP_TK unary_expression_not_plus_minus
2287 {
49f48c71 2288 const char *ptr;
ba179f9f 2289 while (CURRENT_OSB (ctxp)--)
e04a16fb 2290 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2291 ctxp->osb_depth--;
e04a16fb
AG
2292 obstack_grow0 (&temporary_obstack,
2293 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2294 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2295 ptr = obstack_finish (&temporary_obstack);
2296 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2297 $$ = build_cast ($1.location, $2, $5);
2298 }
2299| OP_TK primitive_type OSB_TK error
2300 {yyerror ("']' expected, invalid type expression");}
2301| OP_TK error
2302 {
2303 if (ctxp->prevent_ese != lineno)
2304 yyerror ("Invalid type expression"); RECOVER;
2305 RECOVER;
2306 }
2307| OP_TK primitive_type dims CP_TK error
2308 {yyerror ("Missing term"); RECOVER;}
2309| OP_TK primitive_type CP_TK error
2310 {yyerror ("Missing term"); RECOVER;}
2311| OP_TK name dims CP_TK error
2312 {yyerror ("Missing term"); RECOVER;}
2313;
2314
2315multiplicative_expression:
2316 unary_expression
2317| multiplicative_expression MULT_TK unary_expression
2318 {
2319 $$ = build_binop (BINOP_LOOKUP ($2.token),
2320 $2.location, $1, $3);
2321 }
2322| multiplicative_expression DIV_TK unary_expression
2323 {
2324 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2325 $1, $3);
2326 }
2327| multiplicative_expression REM_TK unary_expression
2328 {
2329 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2330 $1, $3);
2331 }
2332| multiplicative_expression MULT_TK error
2333 {yyerror ("Missing term"); RECOVER;}
2334| multiplicative_expression DIV_TK error
2335 {yyerror ("Missing term"); RECOVER;}
2336| multiplicative_expression REM_TK error
2337 {yyerror ("Missing term"); RECOVER;}
2338;
2339
2340additive_expression:
2341 multiplicative_expression
2342| additive_expression PLUS_TK multiplicative_expression
2343 {
2344 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2345 $1, $3);
2346 }
2347| additive_expression MINUS_TK multiplicative_expression
2348 {
2349 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2350 $1, $3);
2351 }
2352| additive_expression PLUS_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354| additive_expression MINUS_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356;
2357
2358shift_expression:
2359 additive_expression
2360| shift_expression LS_TK additive_expression
2361 {
2362 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2363 $1, $3);
2364 }
2365| shift_expression SRS_TK additive_expression
2366 {
2367 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2368 $1, $3);
2369 }
2370| shift_expression ZRS_TK additive_expression
2371 {
2372 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2373 $1, $3);
2374 }
2375| shift_expression LS_TK error
2376 {yyerror ("Missing term"); RECOVER;}
2377| shift_expression SRS_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| shift_expression ZRS_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381;
2382
2383relational_expression:
2384 shift_expression
2385| relational_expression LT_TK shift_expression
2386 {
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2388 $1, $3);
2389 }
2390| relational_expression GT_TK shift_expression
2391 {
2392 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2393 $1, $3);
2394 }
2395| relational_expression LTE_TK shift_expression
2396 {
2397 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2398 $1, $3);
2399 }
2400| relational_expression GTE_TK shift_expression
2401 {
2402 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2403 $1, $3);
2404 }
2405| relational_expression INSTANCEOF_TK reference_type
5e942c50 2406 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2407| relational_expression LT_TK error
2408 {yyerror ("Missing term"); RECOVER;}
2409| relational_expression GT_TK error
2410 {yyerror ("Missing term"); RECOVER;}
2411| relational_expression LTE_TK error
2412 {yyerror ("Missing term"); RECOVER;}
2413| relational_expression GTE_TK error
2414 {yyerror ("Missing term"); RECOVER;}
2415| relational_expression INSTANCEOF_TK error
2416 {yyerror ("Invalid reference type"); RECOVER;}
2417;
2418
2419equality_expression:
2420 relational_expression
2421| equality_expression EQ_TK relational_expression
2422 {
2423 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2424 $1, $3);
2425 }
2426| equality_expression NEQ_TK relational_expression
2427 {
2428 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2429 $1, $3);
2430 }
2431| equality_expression EQ_TK error
2432 {yyerror ("Missing term"); RECOVER;}
2433| equality_expression NEQ_TK error
2434 {yyerror ("Missing term"); RECOVER;}
2435;
2436
2437and_expression:
2438 equality_expression
2439| and_expression AND_TK equality_expression
2440 {
2441 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 $1, $3);
2443 }
2444| and_expression AND_TK error
2445 {yyerror ("Missing term"); RECOVER;}
2446;
2447
2448exclusive_or_expression:
2449 and_expression
2450| exclusive_or_expression XOR_TK and_expression
2451 {
2452 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2453 $1, $3);
2454 }
2455| exclusive_or_expression XOR_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457;
2458
2459inclusive_or_expression:
2460 exclusive_or_expression
2461| inclusive_or_expression OR_TK exclusive_or_expression
2462 {
2463 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2464 $1, $3);
2465 }
2466| inclusive_or_expression OR_TK error
2467 {yyerror ("Missing term"); RECOVER;}
2468;
2469
2470conditional_and_expression:
2471 inclusive_or_expression
2472| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2473 {
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 $1, $3);
2476 }
2477| conditional_and_expression BOOL_AND_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2479;
2480
2481conditional_or_expression:
2482 conditional_and_expression
2483| conditional_or_expression BOOL_OR_TK conditional_and_expression
2484 {
2485 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 $1, $3);
2487 }
2488| conditional_or_expression BOOL_OR_TK error
2489 {yyerror ("Missing term"); RECOVER;}
2490;
2491
2492conditional_expression: /* Error handling here is weak */
2493 conditional_or_expression
2494| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2495 {
2496 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2497 EXPR_WFL_LINECOL ($$) = $2.location;
2498 }
e04a16fb
AG
2499| conditional_or_expression REL_QM_TK REL_CL_TK error
2500 {
2501 YYERROR_NOW;
2502 yyerror ("Missing term");
2503 DRECOVER (1);
2504 }
2505| conditional_or_expression REL_QM_TK error
2506 {yyerror ("Missing term"); DRECOVER (2);}
2507| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2508 {yyerror ("Missing term"); DRECOVER (3);}
2509;
2510
2511assignment_expression:
2512 conditional_expression
2513| assignment
2514;
2515
2516assignment:
2517 left_hand_side assignment_operator assignment_expression
2518 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2519| left_hand_side assignment_operator error
2520 {
2521 if (ctxp->prevent_ese != lineno)
2522 yyerror ("Missing term");
2523 DRECOVER (assign);
2524 }
2525;
2526
2527left_hand_side:
2528 name
2529| field_access
2530| array_access
2531;
2532
2533assignment_operator:
2534 ASSIGN_ANY_TK
2535| ASSIGN_TK
2536;
2537
2538expression:
2539 assignment_expression
2540;
2541
2542constant_expression:
2543 expression
2544;
2545
2546%%
2547\f
2548
c2952b01
APB
2549/* This section of the code deal with save/restoring parser contexts.
2550 Add mode documentation here. FIXME */
e04a16fb 2551
c2952b01
APB
2552/* Helper function. Create a new parser context. With
2553 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2554 context is copied, otherwise, the new context is zeroed. The newly
2555 created context becomes the current one. */
e04a16fb 2556
c2952b01
APB
2557static void
2558create_new_parser_context (copy_from_previous)
2559 int copy_from_previous;
e04a16fb 2560{
c2952b01 2561 struct parser_ctxt *new;
e04a16fb 2562
c2952b01
APB
2563 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2564 if (copy_from_previous)
2565 {
2566 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2567 new->saved_data_ctx = 1;
2568 }
2569 else
2570 bzero ((PTR) new, sizeof (struct parser_ctxt));
2571
e04a16fb
AG
2572 new->next = ctxp;
2573 ctxp = new;
c2952b01
APB
2574}
2575
2576/* Create a new parser context and make it the current one. */
2577
2578void
2579java_push_parser_context ()
2580{
2581 create_new_parser_context (0);
e04a16fb 2582 if (ctxp->next)
5e942c50
APB
2583 {
2584 ctxp->incomplete_class = ctxp->next->incomplete_class;
2585 ctxp->gclass_list = ctxp->next->gclass_list;
2586 }
e04a16fb
AG
2587}
2588
c2952b01
APB
2589void
2590java_pop_parser_context (generate)
2591 int generate;
2592{
2593 tree current;
2594 struct parser_ctxt *toFree, *next;
2595
2596 if (!ctxp)
2597 return;
2598
2599 toFree = ctxp;
2600 next = ctxp->next;
2601 if (next)
2602 {
2603 next->incomplete_class = ctxp->incomplete_class;
2604 next->gclass_list = ctxp->gclass_list;
2605 lineno = ctxp->lineno;
2606 finput = ctxp->finput;
2607 current_class = ctxp->current_class;
2608 }
2609
2610 /* Set the single import class file flag to 0 for the current list
2611 of imported things */
2612 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2613 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2614
2615 /* And restore those of the previous context */
2616 if ((ctxp = next)) /* Assignment is really meant here */
2617 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2618 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2619
2620 /* If we pushed a context to parse a class intended to be generated,
2621 we keep it so we can remember the class. What we could actually
2622 do is to just update a list of class names. */
2623 if (generate)
2624 {
2625 toFree->next = ctxp_for_generation;
2626 ctxp_for_generation = toFree;
2627 }
2628 else
2629 free (toFree);
2630}
2631
2632/* Create a parser context for the use of saving some global
2633 variables. */
2634
e04a16fb
AG
2635void
2636java_parser_context_save_global ()
2637{
22eed1e6
APB
2638 if (!ctxp)
2639 {
2640 java_push_parser_context ();
ee07f4f4
APB
2641 ctxp->saved_data_ctx = 1;
2642 }
c2952b01
APB
2643
2644 /* If this context already stores data, create a new one suitable
2645 for data storage. */
ee07f4f4 2646 else if (ctxp->saved_data)
c2952b01
APB
2647 create_new_parser_context (1);
2648
e04a16fb
AG
2649 ctxp->finput = finput;
2650 ctxp->lineno = lineno;
2651 ctxp->current_class = current_class;
2652 ctxp->filename = input_filename;
2653 ctxp->current_function_decl = current_function_decl;
ee07f4f4 2654 ctxp->saved_data = 1;
e04a16fb
AG
2655}
2656
c2952b01
APB
2657/* Restore some global variables from the previous context. Make the
2658 previous context the current one. */
2659
e04a16fb
AG
2660void
2661java_parser_context_restore_global ()
2662{
2663 finput = ctxp->finput;
2664 lineno = ctxp->lineno;
2665 current_class = ctxp->current_class;
2666 input_filename = ctxp->filename;
2667 current_function_decl = ctxp->current_function_decl;
c2952b01 2668 ctxp->saved_data = 0;
ee07f4f4
APB
2669 if (ctxp->saved_data_ctx)
2670 java_pop_parser_context (0);
e04a16fb
AG
2671}
2672
c2952b01
APB
2673/* Suspend vital data for the current class/function being parsed so
2674 that an other class can be parsed. Used to let local/anonymous
2675 classes be parsed. */
2676
2677static void
2678java_parser_context_suspend ()
e04a16fb 2679{
c2952b01
APB
2680 /* This makes debugging through java_debug_context easier */
2681 static char *name = "<inner buffer context>";
e04a16fb 2682
c2952b01
APB
2683 /* Duplicate the previous context, use it to save the globals we're
2684 interested in */
2685 create_new_parser_context (1);
2686 ctxp->current_function_decl = current_function_decl;
2687 ctxp->current_class = current_class;
5e942c50 2688
c2952b01
APB
2689 /* Then create a new context which inherits all data from the
2690 previous one. This will be the new current context */
2691 create_new_parser_context (1);
2692
2693 /* Help debugging */
2694 ctxp->next->filename = name;
2695}
2696
2697/* Resume vital data for the current class/function being parsed so
2698 that an other class can be parsed. Used to let local/anonymous
2699 classes be parsed. The trick is the data storing file position
2700 informations must be restored to their current value, so parsing
2701 can resume as if no context was ever saved. */
2702
2703static void
2704java_parser_context_resume ()
2705{
2706 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2707 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2708 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2709
2710 /* We need to inherit the list of classes to complete/generate */
2711 restored->incomplete_class = old->incomplete_class;
2712 restored->gclass_list = old->gclass_list;
2713 restored->classd_list = old->classd_list;
2714 restored->class_list = old->class_list;
2715
2716 /* Restore the current class and function from the saver */
2717 current_class = saver->current_class;
2718 current_function_decl = saver->current_function_decl;
2719
2720 /* Retrive the restored context */
2721 ctxp = restored;
2722
2723 /* Re-installed the data for the parsing to carry on */
2724 bcopy (&old->marker_begining, &ctxp->marker_begining,
2725 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2726
2727 /* Buffer context can now be discarded */
2728 free (saver);
2729 free (old);
2730}
2731
2732/* Add a new anchor node to which all statement(s) initializing static
2733 and non static initialized upon declaration field(s) will be
2734 linked. */
2735
2736static void
2737java_parser_context_push_initialized_field ()
2738{
2739 tree node;
2740
2741 node = build_tree_list (NULL_TREE, NULL_TREE);
2742 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2743 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2744
2745 node = build_tree_list (NULL_TREE, NULL_TREE);
2746 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2747 CPC_INITIALIZER_LIST (ctxp) = node;
2748
2749 node = build_tree_list (NULL_TREE, NULL_TREE);
2750 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2751 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2752}
2753
2754/* Pop the lists of initialized field. If this lists aren't empty,
2755 remember them so we can use it to create and populate the $finit$
2756 or <clinit> functions. */
2757
2758static void
2759java_parser_context_pop_initialized_field ()
2760{
2761 tree stmts;
2762 tree class_type = TREE_TYPE (GET_CPC ());
2763
2764 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2765 {
c2952b01
APB
2766 stmts = CPC_INITIALIZER_STMT (ctxp);
2767 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2768 if (stmts && !java_error_count)
2769 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2770 }
2771
c2952b01
APB
2772 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2773 {
2774 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2775 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2776 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2777 /* Keep initialization in order to enforce 8.5 */
2778 if (stmts && !java_error_count)
2779 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2780 }
e04a16fb 2781
c2952b01
APB
2782 /* JDK 1.1 instance initializers */
2783 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2784 {
c2952b01
APB
2785 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2786 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2787 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2788 if (stmts && !java_error_count)
2789 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2790 }
c2952b01
APB
2791}
2792
2793static tree
2794reorder_static_initialized (list)
2795 tree list;
2796{
2797 /* We have to keep things in order. The alias initializer have to
2798 come first, then the initialized regular field, in reverse to
2799 keep them in lexical order. */
2800 tree marker, previous = NULL_TREE;
2801 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2802 if (TREE_CODE (marker) == TREE_LIST
2803 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2804 break;
2805
2806 /* No static initialized, the list is fine as is */
2807 if (!previous)
2808 list = TREE_CHAIN (marker);
2809
2810 /* No marker? reverse the whole list */
2811 else if (!marker)
2812 list = nreverse (list);
2813
2814 /* Otherwise, reverse what's after the marker and the new reordered
2815 sublist will replace the marker. */
b351b287 2816 else
c2952b01
APB
2817 {
2818 TREE_CHAIN (previous) = NULL_TREE;
2819 list = nreverse (list);
2820 list = chainon (TREE_CHAIN (marker), list);
2821 }
2822 return list;
e04a16fb
AG
2823}
2824
c2952b01
APB
2825/* Helper functions to dump the parser context stack. */
2826
2827#define TAB_CONTEXT(C) \
2828 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2829
2830static void
2831java_debug_context_do (tab)
2832 int tab;
2833{
ee07f4f4
APB
2834 struct parser_ctxt *copy = ctxp;
2835 while (copy)
2836 {
c2952b01 2837 TAB_CONTEXT (tab);
ee07f4f4 2838 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2839 TAB_CONTEXT (tab);
ee07f4f4 2840 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2841 TAB_CONTEXT (tab);
2842 fprintf (stderr, "lineno: %d\n", copy->lineno);
2843 TAB_CONTEXT (tab);
ee07f4f4
APB
2844 fprintf (stderr, "package: %s\n",
2845 (copy->package ?
2846 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2847 TAB_CONTEXT (tab);
ee07f4f4 2848 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2849 TAB_CONTEXT (tab);
ee07f4f4
APB
2850 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2851 copy = copy->next;
2852 tab += 2;
2853 }
ee07f4f4
APB
2854}
2855
c2952b01
APB
2856/* Dump the stacked up parser contexts. Intended to be called from a
2857 debugger. */
2858
ee07f4f4
APB
2859void
2860java_debug_context ()
2861{
2862 java_debug_context_do (0);
2863}
2864
c2952b01
APB
2865\f
2866
2867/* Flag for the error report routine to issue the error the first time
2868 it's called (overriding the default behavior which is to drop the
2869 first invocation and honor the second one, taking advantage of a
2870 richer context. */
2871static int force_error = 0;
ee07f4f4 2872
8119c720
APB
2873/* Reporting an constructor invocation error. */
2874static void
2875parse_ctor_invocation_error ()
2876{
2877 if (DECL_CONSTRUCTOR_P (current_function_decl))
2878 yyerror ("Constructor invocation must be first thing in a constructor");
2879 else
2880 yyerror ("Only constructors can invoke constructors");
2881}
2882
2883/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2884
2885static tree
2886parse_jdk1_1_error (msg)
49f48c71 2887 const char *msg;
b67d701b
PB
2888{
2889 sorry (": `%s' JDK1.1(TM) feature", msg);
2890 java_error_count++;
9bbc7d9f 2891 return empty_stmt_node;
b67d701b
PB
2892}
2893
e04a16fb
AG
2894static int do_warning = 0;
2895
2896void
2897yyerror (msg)
49f48c71 2898 const char *msg;
e04a16fb
AG
2899{
2900 static java_lc elc;
2901 static int prev_lineno;
49f48c71 2902 static const char *prev_msg;
e04a16fb 2903
0a2138e2 2904 int save_lineno;
e04a16fb
AG
2905 char *remainder, *code_from_source;
2906 extern struct obstack temporary_obstack;
2907
2908 if (!force_error && prev_lineno == lineno)
2909 return;
2910
2911 /* Save current error location but report latter, when the context is
2912 richer. */
2913 if (ctxp->java_error_flag == 0)
2914 {
2915 ctxp->java_error_flag = 1;
2916 elc = ctxp->elc;
2917 /* Do something to use the previous line if we're reaching the
2918 end of the file... */
2919#ifdef VERBOSE_SKELETON
2920 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2921#endif
2922 return;
2923 }
2924
2925 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2926 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2927 return;
2928
2929 ctxp->java_error_flag = 0;
2930 if (do_warning)
2931 java_warning_count++;
2932 else
2933 java_error_count++;
2934
2935 if (elc.col == 0 && msg[1] == ';')
2936 {
2937 elc.col = ctxp->p_line->char_col-1;
2938 elc.line = ctxp->p_line->lineno;
2939 }
2940
2941 save_lineno = lineno;
2942 prev_lineno = lineno = elc.line;
2943 prev_msg = msg;
2944
2945 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2946 obstack_grow0 (&temporary_obstack,
2947 code_from_source, strlen (code_from_source));
2948 remainder = obstack_finish (&temporary_obstack);
2949 if (do_warning)
2950 warning ("%s.\n%s", msg, remainder);
2951 else
2952 error ("%s.\n%s", msg, remainder);
2953
2954 /* This allow us to cheaply avoid an extra 'Invalid expression
2955 statement' error report when errors have been already reported on
2956 the same line. This occurs when we report an error but don't have
2957 a synchronization point other than ';', which
2958 expression_statement is the only one to take care of. */
2959 ctxp->prevent_ese = lineno = save_lineno;
2960}
2961
2962static void
15fdcfe9 2963issue_warning_error_from_context (cl, msg, ap)
5e942c50 2964 tree cl;
d4476be2 2965 const char *msg;
15fdcfe9 2966 va_list ap;
5e942c50 2967{
1886c9d8 2968 char *saved, *saved_input_filename;
15fdcfe9
PB
2969 char buffer [4096];
2970 vsprintf (buffer, msg, ap);
2971 force_error = 1;
5e942c50
APB
2972
2973 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2974 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2975 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2976
2977 /* We have a CL, that's a good reason for using it if it contains data */
2978 saved = ctxp->filename;
2979 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2980 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2981 saved_input_filename = input_filename;
2982 input_filename = ctxp->filename;
15fdcfe9
PB
2983 java_error (NULL);
2984 java_error (buffer);
5e942c50 2985 ctxp->filename = saved;
1886c9d8 2986 input_filename = saved_input_filename;
15fdcfe9 2987 force_error = 0;
5e942c50
APB
2988}
2989
e04a16fb
AG
2990/* Issue an error message at a current source line CL */
2991
15fdcfe9 2992void
df32d2ce 2993parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 2994{
d4476be2 2995#ifndef ANSI_PROTOTYPES
e04a16fb 2996 tree cl;
d4476be2 2997 const char *msg;
e04a16fb 2998#endif
e04a16fb
AG
2999 va_list ap;
3000
3001 VA_START (ap, msg);
d4476be2 3002#ifndef ANSI_PROTOTYPES
e04a16fb 3003 cl = va_arg (ap, tree);
d4476be2 3004 msg = va_arg (ap, const char *);
e04a16fb 3005#endif
15fdcfe9
PB
3006 issue_warning_error_from_context (cl, msg, ap);
3007 va_end (ap);
e04a16fb
AG
3008}
3009
3010/* Issue a warning at a current source line CL */
3011
3012static void
df32d2ce 3013parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3014{
d4476be2 3015#ifndef ANSI_PROTOTYPES
e04a16fb 3016 tree cl;
d4476be2 3017 const char *msg;
e04a16fb 3018#endif
e04a16fb
AG
3019 va_list ap;
3020
3021 VA_START (ap, msg);
d4476be2 3022#ifndef ANSI_PROTOTYPES
e04a16fb 3023 cl = va_arg (ap, tree);
d4476be2 3024 msg = va_arg (ap, const char *);
e04a16fb 3025#endif
e04a16fb 3026
c877974e 3027 force_error = do_warning = 1;
15fdcfe9 3028 issue_warning_error_from_context (cl, msg, ap);
c877974e 3029 do_warning = force_error = 0;
15fdcfe9 3030 va_end (ap);
e04a16fb
AG
3031}
3032
82371d41
APB
3033static tree
3034find_expr_with_wfl (node)
3035 tree node;
3036{
3037 while (node)
3038 {
3039 char code;
3040 tree to_return;
3041
3042 switch (TREE_CODE (node))
3043 {
3044 case BLOCK:
c0d87ff6
PB
3045 node = BLOCK_EXPR_BODY (node);
3046 continue;
82371d41
APB
3047
3048 case COMPOUND_EXPR:
3049 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3050 if (to_return)
3051 return to_return;
c0d87ff6
PB
3052 node = TREE_OPERAND (node, 1);
3053 continue;
82371d41
APB
3054
3055 case LOOP_EXPR:
c0d87ff6
PB
3056 node = TREE_OPERAND (node, 0);
3057 continue;
82371d41
APB
3058
3059 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3060 node = TREE_OPERAND (node, 1);
3061 continue;
3062
82371d41
APB
3063 default:
3064 code = TREE_CODE_CLASS (TREE_CODE (node));
3065 if (((code == '1') || (code == '2') || (code == 'e'))
3066 && EXPR_WFL_LINECOL (node))
3067 return node;
ba179f9f 3068 return NULL_TREE;
82371d41
APB
3069 }
3070 }
3071 return NULL_TREE;
3072}
3073
3074/* Issue a missing return statement error. Uses METHOD to figure the
3075 last line of the method the error occurs in. */
3076
3077static void
3078missing_return_error (method)
3079 tree method;
3080{
3081 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3082 parse_error_context (wfl_operator, "Missing return statement");
3083}
3084
3085/* Issue an unreachable statement error. From NODE, find the next
3086 statement to report appropriately. */
3087static void
3088unreachable_stmt_error (node)
3089 tree node;
3090{
3091 /* Browse node to find the next expression node that has a WFL. Use
3092 the location to report the error */
3093 if (TREE_CODE (node) == COMPOUND_EXPR)
3094 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3095 else
3096 node = find_expr_with_wfl (node);
3097
3098 if (node)
3099 {
3100 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3101 parse_error_context (wfl_operator, "Unreachable statement");
3102 }
3103 else
3104 fatal ("Can't get valid statement - unreachable_stmt_error");
3105}
3106
c877974e 3107int
e04a16fb
AG
3108java_report_errors ()
3109{
3110 if (java_error_count)
3111 fprintf (stderr, "%d error%s",
3112 java_error_count, (java_error_count == 1 ? "" : "s"));
3113 if (java_warning_count)
3114 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3115 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3116 if (java_error_count || java_warning_count)
3117 putc ('\n', stderr);
c877974e 3118 return java_error_count;
e04a16fb
AG
3119}
3120
3121static char *
3122java_accstring_lookup (flags)
3123 int flags;
3124{
3125 static char buffer [80];
3126#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3127
3128 /* Access modifier looked-up first for easier report on forbidden
3129 access. */
3130 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3131 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3132 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3133 if (flags & ACC_STATIC) COPY_RETURN ("static");
3134 if (flags & ACC_FINAL) COPY_RETURN ("final");
3135 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3136 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3137 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3138 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3139 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3140 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3141
3142 buffer [0] = '\0';
3143 return buffer;
3144#undef COPY_RETURN
3145}
3146
b67d701b
PB
3147/* Issuing error messages upon redefinition of classes, interfaces or
3148 variables. */
3149
e04a16fb 3150static void
b67d701b 3151classitf_redefinition_error (context, id, decl, cl)
49f48c71 3152 const char *context;
e04a16fb
AG
3153 tree id, decl, cl;
3154{
3155 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3156 context, IDENTIFIER_POINTER (id),
3157 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3158 /* Here we should point out where its redefined. It's a unicode. FIXME */
3159}
3160
b67d701b
PB
3161static void
3162variable_redefinition_error (context, name, type, line)
3163 tree context, name, type;
3164 int line;
3165{
49f48c71 3166 const char *type_name;
b67d701b
PB
3167
3168 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3169 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3170 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3171 else
0a2138e2 3172 type_name = lang_printable_name (type, 0);
b67d701b
PB
3173
3174 parse_error_context (context,
781b0558 3175 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3176 IDENTIFIER_POINTER (name),
3177 type_name, IDENTIFIER_POINTER (name), line);
3178}
3179
c583dd46
APB
3180static tree
3181build_array_from_name (type, type_wfl, name, ret_name)
3182 tree type, type_wfl, name, *ret_name;
3183{
3184 int more_dims = 0;
49f48c71 3185 const char *string;
c583dd46
APB
3186
3187 /* Eventually get more dims */
3188 string = IDENTIFIER_POINTER (name);
3189 while (string [more_dims] == '[')
3190 more_dims++;
3191
3192 /* If we have, then craft a new type for this variable */
3193 if (more_dims)
3194 {
c0d87ff6 3195 name = get_identifier (&string [more_dims]);
c583dd46 3196
34f4db93
APB
3197 /* If we have a pointer, use its type */
3198 if (TREE_CODE (type) == POINTER_TYPE)
3199 type = TREE_TYPE (type);
c583dd46
APB
3200
3201 /* Building the first dimension of a primitive type uses this
3202 function */
3203 if (JPRIMITIVE_TYPE_P (type))
3204 {
3205 type = build_java_array_type (type, -1);
22eed1e6 3206 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3207 more_dims--;
3208 }
3209 /* Otherwise, if we have a WFL for this type, use it (the type
3210 is already an array on an unresolved type, and we just keep
3211 on adding dimensions) */
3212 else if (type_wfl)
3213 type = type_wfl;
3214
3215 /* Add all the dimensions */
3216 while (more_dims--)
3217 type = build_unresolved_array_type (type);
3218
3219 /* The type may have been incomplete in the first place */
3220 if (type_wfl)
3221 type = obtain_incomplete_type (type);
3222 }
3223
c2952b01
APB
3224 if (ret_name)
3225 *ret_name = name;
c583dd46
APB
3226 return type;
3227}
3228
e04a16fb
AG
3229/* Build something that the type identifier resolver will identify as
3230 being an array to an unresolved type. TYPE_WFL is a WFL on a
3231 identifier. */
3232
3233static tree
3234build_unresolved_array_type (type_or_wfl)
3235 tree type_or_wfl;
3236{
49f48c71 3237 const char *ptr;
e04a16fb 3238
1886c9d8 3239 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3240 just create a array type */
3241 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3242 {
3243 tree type = build_java_array_type (type_or_wfl, -1);
3244 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3245 return type;
3246 }
3247
3248 obstack_1grow (&temporary_obstack, '[');
3249 obstack_grow0 (&temporary_obstack,
3250 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3251 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3252 ptr = obstack_finish (&temporary_obstack);
3253 return build_expr_wfl (get_identifier (ptr),
3254 EXPR_WFL_FILENAME (type_or_wfl),
3255 EXPR_WFL_LINENO (type_or_wfl),
3256 EXPR_WFL_COLNO (type_or_wfl));
3257}
3258
e04a16fb
AG
3259static void
3260parser_add_interface (class_decl, interface_decl, wfl)
3261 tree class_decl, interface_decl, wfl;
3262{
3263 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3264 parse_error_context (wfl, "Interface `%s' repeated",
3265 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3266}
3267
3268/* Bulk of common class/interface checks. Return 1 if an error was
3269 encountered. TAG is 0 for a class, 1 for an interface. */
3270
3271static int
3272check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3273 int is_interface, flags;
3274 tree raw_name, qualified_name, decl, cl;
3275{
3276 tree node;
c2952b01
APB
3277 int sca = 0; /* Static class allowed */
3278 int icaf = 0; /* Inner class allowed flags */
3279 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3280
3281 if (!quiet_flag)
c2952b01
APB
3282 fprintf (stderr, " %s%s %s",
3283 (CPC_INNER_P () ? "inner" : ""),
3284 (is_interface ? "interface" : "class"),
e04a16fb
AG
3285 IDENTIFIER_POINTER (qualified_name));
3286
3287 /* Scope of an interface/class type name:
3288 - Can't be imported by a single type import
3289 - Can't already exists in the package */
3290 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3291 && (node = find_name_in_single_imports (raw_name)))
3292 {
3293 parse_error_context
3294 (cl, "%s name `%s' clashes with imported type `%s'",
3295 (is_interface ? "Interface" : "Class"),
3296 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3297 return 1;
3298 }
3299 if (decl && CLASS_COMPLETE_P (decl))
3300 {
b67d701b
PB
3301 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3302 qualified_name, decl, cl);
e04a16fb
AG
3303 return 1;
3304 }
3305
c2952b01
APB
3306 if (check_inner_class_redefinition (raw_name, cl))
3307 return 1;
3308
3309 /* If public, file name should match class/interface name, except
3310 when dealing with an inner class */
3311 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3312 {
49f48c71 3313 const char *f;
e04a16fb
AG
3314
3315 /* Contains OS dependent assumption on path separator. FIXME */
3316 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3317 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3318 f--)
3319 ;
847fe791 3320 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3321 f++;
3322 if (strncmp (IDENTIFIER_POINTER (raw_name),
3323 f , IDENTIFIER_LENGTH (raw_name)) ||
3324 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3325 parse_error_context
3326 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3327 (is_interface ? "interface" : "class"),
3328 IDENTIFIER_POINTER (qualified_name),
3329 IDENTIFIER_POINTER (raw_name));
3330 }
3331
c2952b01
APB
3332 /* Static classes can be declared only in top level classes. Note:
3333 once static, a inner class is a top level class. */
3334 if (flags & ACC_STATIC)
3335 {
3336 /* Catch the specific error of declaring an class inner class
3337 with no toplevel enclosing class. Prevent check_modifiers from
3338 complaining a second time */
3339 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3340 {
3341 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3342 IDENTIFIER_POINTER (qualified_name));
3343 sca = ACC_STATIC;
3344 }
3345 /* Else, in the context of a top-level class declaration, let
3346 `check_modifiers' do its job, otherwise, give it a go */
3347 else
3348 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3349 }
3350
3351 /* Inner classes and interfaces can be declared private or protected
3352 within their enclosing classes. */
3353 if (CPC_INNER_P ())
3354 {
3355 /* A class which is local to a block can't be public, private,
3356 protected or static. But it is created final, so allow this
3357 one. */
3358 if (current_function_decl)
3359 icaf = sca = uaaf = ACC_FINAL;
3360 else
3361 {
3362 check_modifiers_consistency (flags);
3363 icaf = ACC_PRIVATE|ACC_PROTECTED;
3364 }
3365 }
3366
2884c41e
KG
3367 if (is_interface)
3368 check_modifiers ("Illegal modifier `%s' for interface declaration",
3369 flags, INTERFACE_MODIFIERS);
3370 else
3371 check_modifiers ("Illegal modifier `%s' for class declaration",
c2952b01 3372 flags, uaaf|sca|icaf);
e04a16fb
AG
3373 return 0;
3374}
3375
c2952b01
APB
3376static void
3377make_nested_class_name (cpc_list)
3378 tree cpc_list;
3379{
3380 tree name;
3381
3382 if (!cpc_list)
3383 return;
3384 else
3385 make_nested_class_name (TREE_CHAIN (cpc_list));
3386
3387 /* Pick the qualified name when dealing with the first upmost
3388 enclosing class */
3389 name = (TREE_CHAIN (cpc_list) ?
3390 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3391 obstack_grow (&temporary_obstack,
3392 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3393 /* Why is NO_DOLLAR_IN_LABEL defined? */
3394#if 0
3395#ifdef NO_DOLLAR_IN_LABEL
3396 fatal ("make_nested_class_name: Can't use '$' as a separator "
3397 "for inner classes");
3398#endif
3399#endif
3400 obstack_1grow (&temporary_obstack, '$');
3401}
3402
3403/* Can't redefine a class already defined in an earlier scope. */
3404
3405static int
3406check_inner_class_redefinition (raw_name, cl)
3407 tree raw_name, cl;
3408{
3409 tree scope_list;
3410
3411 for (scope_list = GET_CPC_LIST (); scope_list;
3412 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3413 if (raw_name == GET_CPC_UN_NODE (scope_list))
3414 {
3415 parse_error_context
3416 (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",
3417 IDENTIFIER_POINTER (raw_name));
3418 return 1;
3419 }
3420 return 0;
3421}
3422
3423static tree
3424find_as_inner_class (enclosing, name, cl)
3425 tree enclosing, name, cl;
3426{
3427 tree qual, to_return;
3428 if (!enclosing)
3429 return NULL_TREE;
3430
3431 name = TYPE_NAME (name);
3432
3433 /* First search: within the scope of `enclosing', search for name */
3434 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3435 qual = EXPR_WFL_QUALIFICATION (cl);
3436 else if (cl)
3437 qual = build_tree_list (cl, NULL_TREE);
3438 else
3439 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3440
3441 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3442 return to_return;
3443
3444 /* We're dealing with a qualified name. Try to resolve thing until
3445 we get something that is an enclosing class. */
3446 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3447 {
3448 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3449
3450 for(qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3451 qual = TREE_CHAIN (qual))
3452 {
3453 acc = merge_qualified_name (acc,
3454 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3455 BUILD_PTR_FROM_NAME (ptr, acc);
3456 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3457 }
3458
3459 /* A NULL qual and a decl means that the search ended
3460 successfully?!? We have to do something then. FIXME */
3461
3462 if (decl)
3463 enclosing = decl;
3464 else
3465 qual = EXPR_WFL_QUALIFICATION (cl);
3466 }
3467 /* Otherwise, create a qual for the other part of the resolution. */
3468 else
3469 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3470
3471 return find_as_inner_class_do (qual, enclosing);
3472}
3473
3474/* We go inside the list of sub classes and try to find a way
3475 through. */
3476
3477static tree
3478find_as_inner_class_do (qual, enclosing)
3479 tree qual, enclosing;
3480{
3481 if (!qual)
3482 return NULL_TREE;
3483
3484 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3485 {
3486 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3487 tree next_enclosing = NULL_TREE;
3488 tree inner_list;
3489
3490 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3491 inner_list; inner_list = TREE_CHAIN (inner_list))
3492 {
3493 if (TREE_VALUE (inner_list) == name_to_match)
3494 {
3495 next_enclosing = TREE_PURPOSE (inner_list);
3496 break;
3497 }
3498 }
3499 enclosing = next_enclosing;
3500 }
3501
3502 return (!qual && enclosing ? enclosing : NULL_TREE);
3503}
3504
3505/* Reach all inner classes and tie their unqualified name to a
3506 DECL. */
3507
3508static void
3509set_nested_class_simple_name_value (outer, set)
3510 tree outer;
3511 int set;
3512{
3513 tree l;
3514
3515 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3516 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3517 TREE_PURPOSE (l) : NULL_TREE);
3518}
3519
3520static void
3521link_nested_class_to_enclosing ()
3522{
3523 if (GET_ENCLOSING_CPC ())
3524 {
3525 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3526 DECL_INNER_CLASS_LIST (enclosing) =
3527 tree_cons (GET_CPC (), GET_CPC_UN (),
3528 DECL_INNER_CLASS_LIST (enclosing));
3529 enclosing = enclosing;
3530 }
3531}
3532
3533static tree
3534maybe_make_nested_class_name (name)
3535 tree name;
3536{
3537 tree id = NULL_TREE;
3538
3539 if (CPC_INNER_P ())
3540 {
3541 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3542 obstack_grow0 (&temporary_obstack,
3543 IDENTIFIER_POINTER (name),
3544 IDENTIFIER_LENGTH (name));
c2952b01
APB
3545 id = get_identifier (obstack_finish (&temporary_obstack));
3546 if (ctxp->package)
3547 QUALIFIED_P (id) = 1;
3548 }
3549 return id;
3550}
3551
3552/* If DECL is NULL, create and push a new DECL, record the current
3553 line CL and do other maintenance things. */
3554
e04a16fb 3555static tree
c2952b01
APB
3556maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3557 tree decl, raw_name, qualified_name, cl;
e04a16fb 3558{
5e942c50 3559 if (!decl)
e04a16fb 3560 decl = push_class (make_class (), qualified_name);
c2952b01 3561
e04a16fb
AG
3562 /* Take care of the file and line business */
3563 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3564 /* If we're emiting xrefs, store the line/col number information */
3565 if (flag_emit_xref)
3566 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3567 else
3568 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3569 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3570 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3571 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3572
c2952b01
APB
3573 PUSH_CPC (decl, raw_name);
3574 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3575
e04a16fb
AG
3576 /* Link the declaration to the already seen ones */
3577 TREE_CHAIN (decl) = ctxp->class_list;
3578 ctxp->class_list = decl;
5e942c50 3579
23a79c61 3580 /* Create a new nodes in the global lists */
5e942c50 3581 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3582 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3583
e04a16fb
AG
3584 /* Install a new dependency list element */
3585 create_jdep_list (ctxp);
3586
3587 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3588 IDENTIFIER_POINTER (qualified_name)));
3589 return decl;
3590}
3591
3592static void
3593add_superinterfaces (decl, interface_list)
3594 tree decl, interface_list;
3595{
3596 tree node;
3597 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3598 takes care of ensuring that:
3599 - This is an accessible interface type,
3600 - Circularity detection.
3601 parser_add_interface is then called. If present but not defined,
3602 the check operation is delayed until the super interface gets
3603 defined. */
3604 for (node = interface_list; node; node = TREE_CHAIN (node))
3605 {
15fdcfe9 3606 tree current = TREE_PURPOSE (node);
5e942c50
APB
3607 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3608 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3609 {
5e942c50
APB
3610 if (!parser_check_super_interface (idecl, decl, current))
3611 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3612 }
3613 else
3614 register_incomplete_type (JDEP_INTERFACE,
3615 current, decl, NULL_TREE);
3616 }
3617}
3618
3619/* Create an interface in pass1 and return its decl. Return the
3620 interface's decl in pass 2. */
3621
3622static tree
3623create_interface (flags, id, super)
3624 int flags;
3625 tree id, super;
3626{
e04a16fb 3627 tree raw_name = EXPR_WFL_NODE (id);
c2952b01 3628 tree q_name = parser_qualified_classname (flags & ACC_STATIC, raw_name);
e04a16fb
AG
3629 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3630
3631 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3632
3633 /* Basic checks: scope, redefinition, modifiers */
3634 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3635 {
3636 PUSH_ERROR ();
3637 return NULL_TREE;
3638 }
3639
3640 /* Suspend the current parsing context if we're parsing an inner
3641 interface */
3642 if (CPC_INNER_P ())
3643 java_parser_context_suspend ();
3644
3645 /* Push a new context for (static) initialized upon declaration fields */
3646 java_parser_context_push_initialized_field ();
e04a16fb
AG
3647
3648 /* Interface modifiers check
3649 - public/abstract allowed (already done at that point)
3650 - abstract is obsolete (comes first, it's a warning, or should be)
3651 - Can't use twice the same (checked in the modifier rule) */
c877974e 3652 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3653 parse_warning_context
3654 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3655 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3656
3657 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3658 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3659
3660 /* Set super info and mark the class a complete */
2aa11e97 3661 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3662 object_type_node, ctxp->interface_number);
3663 ctxp->interface_number = 0;
3664 CLASS_COMPLETE_P (decl) = 1;
3665 add_superinterfaces (decl, super);
3666
3667 return decl;
3668}
3669
c2952b01
APB
3670/* Anonymous class counter. Will be reset to 1 every time a non
3671 anonymous class gets created. */
3672static int anonymous_class_counter = 1;
3673
3674/* Patch anonymous class CLASS, by either extending or implementing
3675 DEP. */
3676
3677static void
3678patch_anonymous_class (type_decl, class_decl, wfl)
3679 tree type_decl, class_decl, wfl;
3680{
3681 tree class = TREE_TYPE (class_decl);
3682 tree type = TREE_TYPE (type_decl);
3683 tree binfo = TYPE_BINFO (class);
3684
3685 /* If it's an interface, implement it */
3686 if (CLASS_INTERFACE (type_decl))
3687 {
3688 tree s_binfo;
3689 int length;
3690
3691 if (parser_check_super_interface (type_decl, class_decl, wfl))
3692 return;
3693
3694 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3695 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3696 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3697 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3698 /* And add the interface */
3699 parser_add_interface (class_decl, type_decl, wfl);
3700 }
3701 /* Otherwise, it's a type we want to extend */
3702 else
3703 {
3704 if (parser_check_super (type_decl, class_decl, wfl))
3705 return;
3706 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3707 }
3708}
3709
3710static tree
3711create_anonymous_class (location, type_name)
3712 int location;
3713 tree type_name;
3714{
3715 char buffer [80];
3716 tree super = NULL_TREE, itf = NULL_TREE;
3717 tree id, type_decl, class;
3718
3719 /* The unqualified name of the anonymous class. It's just a number. */
3720 sprintf (buffer, "%d", anonymous_class_counter++);
3721 id = build_wfl_node (get_identifier (buffer));
3722 EXPR_WFL_LINECOL (id) = location;
3723
3724 /* We know about the type to extend/implement. We go ahead */
3725 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3726 {
3727 /* Create a class which either implements on extends the designated
3728 class. The class bears an innacessible name. */
3729 if (CLASS_INTERFACE (type_decl))
3730 {
3731 /* It's OK to modify it here. It's been already used and
3732 shouldn't be reused */
3733 ctxp->interface_number = 1;
3734 /* Interfaces should presented as a list of WFLs */
3735 itf = build_tree_list (type_name, NULL_TREE);
3736 }
3737 else
3738 super = type_name;
3739 }
3740
3741 class = create_class (ACC_FINAL, id, super, itf);
3742
3743 /* We didn't know anything about the stuff. We register a dependence. */
3744 if (!type_decl)
3745 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3746
3747 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3748 return class;
3749}
3750
e04a16fb
AG
3751/* Create an class in pass1 and return its decl. Return class
3752 interface's decl in pass 2. */
3753
3754static tree
3755create_class (flags, id, super, interfaces)
3756 int flags;
3757 tree id, super, interfaces;
3758{
e04a16fb
AG
3759 tree raw_name = EXPR_WFL_NODE (id);
3760 tree class_id, decl;
9ee9b555 3761 tree super_decl_type;
e04a16fb 3762
c2952b01 3763 class_id = parser_qualified_classname (0, raw_name);
e04a16fb
AG
3764 decl = IDENTIFIER_CLASS_VALUE (class_id);
3765 EXPR_WFL_NODE (id) = class_id;
3766
3767 /* Basic check: scope, redefinition, modifiers */
3768 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3769 {
3770 PUSH_ERROR ();
3771 return NULL_TREE;
3772 }
3773
3774 /* Suspend the current parsing context if we're parsing an inner
3775 class or an anonymous class. */
3776 if (CPC_INNER_P ())
3777 java_parser_context_suspend ();
3778 /* Push a new context for (static) initialized upon declaration fields */
3779 java_parser_context_push_initialized_field ();
e04a16fb
AG
3780
3781 /* Class modifier check:
3782 - Allowed modifier (already done at that point)
3783 - abstract AND final forbidden
3784 - Public classes defined in the correct file */
3785 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3786 parse_error_context
3787 (id, "Class `%s' can't be declared both abstract and final",
3788 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3789
3790 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3791 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3792
3793 /* If SUPER exists, use it, otherwise use Object */
3794 if (super)
3795 {
3796 /* Can't extend java.lang.Object */
3797 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3798 {
3799 parse_error_context (id, "Can't extend `java.lang.Object'");
3800 return NULL_TREE;
3801 }
3802
2c3199bc
PB
3803 super_decl_type =
3804 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3805 }
3806 else if (TREE_TYPE (decl) != object_type_node)
3807 super_decl_type = object_type_node;
3808 /* We're defining java.lang.Object */
3809 else
3810 super_decl_type = NULL_TREE;
3811
3812 /* Set super info and mark the class a complete */
3813 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3814 ctxp->interface_number);
3815 ctxp->interface_number = 0;
3816 CLASS_COMPLETE_P (decl) = 1;
3817 add_superinterfaces (decl, interfaces);
3818
c2952b01
APB
3819 /* If the class is a top level inner class, install an alias. */
3820 if (INNER_CLASS_DECL_P (decl) && CLASS_STATIC (decl))
3821 {
3822 tree alias = parser_qualified_classname (1, raw_name);
3823 IDENTIFIER_GLOBAL_VALUE (alias) = decl;
3824 }
3825
3826 /* Add the private this$<n> field, Replicate final locals still in
3827 scope as private final fields mangled like val$<local_name>.
3828 This doesn't not occur for top level (static) inner classes. */
3829 if (PURE_INNER_CLASS_DECL_P (decl))
3830 add_inner_class_fields (decl, current_function_decl);
3831
7f10c2e2
APB
3832 /* If doing xref, store the location at which the inherited class
3833 (if any) was seen. */
3834 if (flag_emit_xref && super)
3835 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3836
5e942c50
APB
3837 /* Eventually sets the @deprecated tag flag */
3838 CHECK_DEPRECATED (decl);
3839
165f37bc
APB
3840 /* Reset the anonymous class counter when declaring non inner classes */
3841 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3842 anonymous_class_counter = 1;
3843
e04a16fb
AG
3844 return decl;
3845}
3846
c2952b01
APB
3847/* End a class declaration: register the statements used to create
3848 $finit$ and <clinit>, pop the current class and resume the prior
3849 parser context if necessary. */
3850
3851static void
3852end_class_declaration (resume)
3853 int resume;
3854{
3855 /* If an error occured, context weren't pushed and won't need to be
3856 popped by a resume. */
3857 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3858
3859 java_parser_context_pop_initialized_field ();
3860 POP_CPC ();
3861 if (resume && no_error_occured)
3862 java_parser_context_resume ();
3863}
3864
3865static void
3866add_inner_class_fields (class_decl, fct_decl)
3867 tree class_decl;
3868 tree fct_decl;
3869{
3870 tree block, marker, f;
3871
3872 f = add_field (TREE_TYPE (class_decl),
3873 build_current_thisn (TREE_TYPE (class_decl)),
3874 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3875 ACC_PRIVATE);
3876 FIELD_THISN (f) = 1;
3877
3878 if (!fct_decl)
3879 return;
3880
3881 for (block = GET_CURRENT_BLOCK (fct_decl);
3882 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3883 {
3884 tree decl;
3885 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3886 {
3887 char *name, *pname;
3888 tree wfl, init, list;
3889
3890 /* Avoid non final arguments. */
3891 if (!LOCAL_FINAL (decl))
3892 continue;
3893
3894 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3895 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3896 wfl = build_wfl_node (get_identifier (name));
3897 init = build_wfl_node (get_identifier (pname));
3898 /* Build an initialization for the field: it will be
3899 initialized by a parameter added to $finit$, bearing a
3900 mangled name of the field itself (param$<n>.) The
3901 parameter is provided to $finit$ by the constructor
3902 invoking it (hence the constructor will also feature a
3903 hidden parameter, set to the value of the outer context
3904 local at the time the inner class is created.)
3905
3906 Note: we take into account all possible locals that can
3907 be accessed by the inner class. It's actually not trivial
3908 to minimize these aliases down to the ones really
3909 used. One way to do that would be to expand all regular
3910 methods first, then $finit$ to get a picture of what's
3911 used. It works with the exception that we would have to
3912 go back on all constructor invoked in regular methods to
3913 have their invokation reworked (to include the right amount
3914 of alias initializer parameters.)
3915
3916 The only real way around, I think, is a first pass to
3917 identify locals really used in the inner class. We leave
3918 the flag FIELD_LOCAL_ALIAS_USED around for that future
3919 use.
3920
3921 On the other hand, it only affect local inner classes,
3922 whose constructors (and $finit$ call) will be featuring
3923 unecessary arguments. It's easy for a developper to keep
3924 this number of parameter down by using the `final'
3925 keyword only when necessary. For the time being, we can
3926 issue a warning on unecessary finals. FIXME */
3927 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3928 wfl, init);
3929
3930 /* Register the field. The TREE_LIST holding the part
3931 initialized/initializer will be marked ARG_FINAL_P so
3932 that the created field can be marked
3933 FIELD_LOCAL_ALIAS. */
3934 list = build_tree_list (wfl, init);
3935 ARG_FINAL_P (list) = 1;
3936 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3937 }
3938 }
3939
3940 if (!CPC_INITIALIZER_STMT (ctxp))
3941 return;
3942
3943 /* If we ever registered an alias field, insert and marker to
3944 remeber where the list ends. The second part of the list (the one
3945 featuring initialized fields) so it can be later reversed to
3946 enforce 8.5. The marker will be removed during that operation. */
3947 marker = build_tree_list (NULL_TREE, NULL_TREE);
3948 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3949 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3950}
3951
e04a16fb
AG
3952/* Can't use lookup_field () since we don't want to load the class and
3953 can't set the CLASS_LOADED_P flag */
3954
3955static tree
3956find_field (class, name)
3957 tree class;
3958 tree name;
3959{
3960 tree decl;
3961 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3962 {
3963 if (DECL_NAME (decl) == name)
3964 return decl;
3965 }
3966 return NULL_TREE;
3967}
3968
3969/* Wrap around lookup_field that doesn't potentially upset the value
3970 of CLASS */
3971
3972static tree
3973lookup_field_wrapper (class, name)
3974 tree class, name;
3975{
3976 tree type = class;
5b09b33e 3977 tree decl;
c877974e 3978 java_parser_context_save_global ();
5b09b33e 3979 decl = lookup_field (&type, name);
c877974e 3980 java_parser_context_restore_global ();
93024893 3981 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
3982}
3983
3984/* Find duplicate field within the same class declarations and report
c583dd46
APB
3985 the error. Returns 1 if a duplicated field was found, 0
3986 otherwise. */
e04a16fb
AG
3987
3988static int
c583dd46 3989duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 3990 tree new_field_name, new_type, cl;
e04a16fb
AG
3991{
3992 /* This might be modified to work with method decl as well */
c2952b01 3993 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
3994 if (decl)
3995 {
c2e3db92 3996 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
3997 ((TREE_CODE (new_type) == POINTER_TYPE
3998 && TREE_TYPE (new_type) == NULL_TREE) ?
3999 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4000 lang_printable_name (new_type, 1)));
c877974e
APB
4001 /* The type may not have been completed by the time we report
4002 the error */
c2e3db92 4003 char *t2 = xstrdup (purify_type_name
4a5f66c3 4004 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4005 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4006 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4007 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4008 parse_error_context
4009 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4010 t1, IDENTIFIER_POINTER (new_field_name),
4011 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4012 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4013 free (t1);
4014 free (t2);
c583dd46 4015 return 1;
e04a16fb 4016 }
c583dd46 4017 return 0;
e04a16fb
AG
4018}
4019
4020/* Field registration routine. If TYPE doesn't exist, field
4021 declarations are linked to the undefined TYPE dependency list, to
4022 be later resolved in java_complete_class () */
4023
4024static void
4025register_fields (flags, type, variable_list)
4026 int flags;
4027 tree type, variable_list;
4028{
c583dd46 4029 tree current, saved_type;
c2952b01 4030 tree class_type = NULL_TREE;
e04a16fb
AG
4031 int saved_lineno = lineno;
4032 int must_chain = 0;
4033 tree wfl = NULL_TREE;
4034
c2952b01
APB
4035 if (GET_CPC ())
4036 class_type = TREE_TYPE (GET_CPC ());
4037
4038 if (!class_type || class_type == error_mark_node)
4039 return;
4040
e04a16fb
AG
4041 /* If we're adding fields to interfaces, those fields are public,
4042 static, final */
4043 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4044 {
4045 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4046 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4047 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4048 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4049 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4050 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4051 check_modifiers ("Illegal interface member modifier `%s'", flags,
4052 INTERFACE_FIELD_MODIFIERS);
4053 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4054 }
4055
c583dd46
APB
4056 /* Obtain a suitable type for resolution, if necessary */
4057 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4058
4059 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4060 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4061
c583dd46
APB
4062 for (current = variable_list, saved_type = type; current;
4063 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4064 {
c877974e 4065 tree real_type;
c583dd46 4066 tree field_decl;
e04a16fb
AG
4067 tree cl = TREE_PURPOSE (current);
4068 tree init = TREE_VALUE (current);
4069 tree current_name = EXPR_WFL_NODE (cl);
4070
c2952b01
APB
4071 /* Can't declare static fields in inner classes */
4072 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4073 && !CLASS_INTERFACE (TYPE_NAME (class_type)))
4074 parse_error_context
4075 (cl, "Field `%s' can't be static in innerclass `%s'. Only members of interfaces and top-level classes can be static",
4076 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4077 lang_printable_name (class_type, 0));
4078
c583dd46
APB
4079 /* Process NAME, as it may specify extra dimension(s) for it */
4080 type = build_array_from_name (type, wfl, current_name, &current_name);
4081
c583dd46
APB
4082 /* Type adjustment. We may have just readjusted TYPE because
4083 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4084 a reference if we can and don't have one already. Also
4085 change the name if we have an init. */
4086 if (type != saved_type)
4087 {
1886c9d8 4088 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4089 if (init)
4090 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4091 }
e04a16fb 4092
c877974e
APB
4093 real_type = GET_REAL_TYPE (type);
4094 /* Check for redeclarations */
4095 if (duplicate_declaration_error_p (current_name, real_type, cl))
4096 continue;
4097
c583dd46 4098 /* Set lineno to the line the field was found and create a
5e942c50 4099 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4100 if (flag_emit_xref)
4101 lineno = EXPR_WFL_LINECOL (cl);
4102 else
4103 lineno = EXPR_WFL_LINENO (cl);
c877974e 4104 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4105 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4106
4107 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4108 mark the created field FIELD_LOCAL_ALIAS, so that we can
4109 hide parameters to this inner class $finit$ and constructors. */
4110 if (ARG_FINAL_P (current))
4111 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4112
4113 /* Check if we must chain. */
4114 if (must_chain)
4115 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4116
c583dd46
APB
4117 /* If we have an initialization value tied to the field */
4118 if (init)
4119 {
4120 /* The field is declared static */
e04a16fb 4121 if (flags & ACC_STATIC)
e04a16fb 4122 {
7525cc04
APB
4123 /* We include the field and its initialization part into
4124 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4125 walked, field initializations will be processed and
4126 fields initialized with known constants will be taken
4127 out of <clinit> and have their DECL_INITIAL set
7525cc04 4128 appropriately. */
c2952b01
APB
4129 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4130 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4131 if (TREE_OPERAND (init, 1)
4132 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4133 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4134 }
5e942c50
APB
4135 /* A non-static field declared with an immediate initialization is
4136 to be initialized in <init>, if any. This field is remembered
4137 to be processed at the time of the generation of <init>. */
c583dd46
APB
4138 else
4139 {
c2952b01
APB
4140 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4141 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4142 }
5b09b33e 4143 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4144 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4145 }
4146 }
4147 lineno = saved_lineno;
4148}
4149
c2952b01
APB
4150/* Generate $finit$, using the list of initialized fields to populate
4151 its body. $finit$'s parameter(s) list is adjusted to include the
4152 one(s) used to initialized the field(s) caching outer context
4153 local(s). */
22eed1e6 4154
c2952b01
APB
4155static tree
4156generate_finit (class_type)
4157 tree class_type;
22eed1e6 4158{
c2952b01
APB
4159 int count = 0;
4160 tree list = TYPE_FINIT_STMT_LIST (class_type);
4161 tree mdecl, current, parms;
4162
4163 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4164 class_type, NULL_TREE,
4165 &count);
4166 CRAFTED_PARAM_LIST_FIXUP (parms);
4167 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4168 finit_identifier_node, parms);
4169 fix_method_argument_names (parms, mdecl);
4170 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4171 mdecl, NULL_TREE);
4172 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4173 start_artificial_method_body (mdecl);
4174
c2952b01 4175 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4176 java_method_add_stmt (mdecl,
4177 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4178 current));
22eed1e6 4179 end_artificial_method_body (mdecl);
c2952b01 4180 return mdecl;
22eed1e6
APB
4181}
4182
e04a16fb 4183static void
c2952b01
APB
4184add_instance_initializer (mdecl)
4185 tree mdecl;
e04a16fb 4186{
c2952b01
APB
4187 tree current;
4188 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4189 tree compound = NULL_TREE;
e04a16fb 4190
c2952b01 4191 if (stmt_list)
e04a16fb 4192 {
c2952b01
APB
4193 for (current = stmt_list; current; current = TREE_CHAIN (current))
4194 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4195
c2952b01
APB
4196 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4197 NULL_TREE, compound));
4198 }
e04a16fb
AG
4199}
4200
4201/* Shared accros method_declarator and method_header to remember the
4202 patch stage that was reached during the declaration of the method.
4203 A method DECL is built differently is there is no patch
4204 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4205 pending on the currently defined method. */
4206
4207static int patch_stage;
4208
4209/* Check the method declaration and add the method to its current
4210 class. If the argument list is known to contain incomplete types,
4211 the method is partially added and the registration will be resume
22eed1e6
APB
4212 once the method arguments resolved. If TYPE is NULL, we're dealing
4213 with a constructor. */
e04a16fb
AG
4214
4215static tree
4216method_header (flags, type, mdecl, throws)
4217 int flags;
4218 tree type, mdecl, throws;
4219{
4220 tree meth = TREE_VALUE (mdecl);
4221 tree id = TREE_PURPOSE (mdecl);
1886c9d8 4222 tree type_wfl = NULL_TREE;
79d13333 4223 tree meth_name = NULL_TREE;
c2952b01 4224 tree current, orig_arg, this_class = NULL;
e04a16fb 4225 int saved_lineno;
1886c9d8 4226 int constructor_ok = 0, must_chain;
c2952b01 4227 int count;
e04a16fb
AG
4228
4229 check_modifiers_consistency (flags);
79d13333 4230
c2952b01
APB
4231 if (GET_CPC ())
4232 this_class = TREE_TYPE (GET_CPC ());
4233
4234 if (!this_class || this_class == error_mark_node)
79d13333 4235 return NULL_TREE;
e04a16fb
AG
4236
4237 /* There are some forbidden modifiers for an abstract method and its
4238 class must be abstract as well. */
22eed1e6 4239 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4240 {
4241 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4242 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4243 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4244 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4245 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4246 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4247 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4248 parse_error_context
781b0558 4249 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e04a16fb
AG
4250 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
4251 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4252 }
c2952b01 4253
22eed1e6
APB
4254 /* Things to be checked when declaring a constructor */
4255 if (!type)
4256 {
4257 int ec = java_error_count;
4258 /* 8.6: Constructor declarations: we might be trying to define a
4259 method without specifying a return type. */
c2952b01 4260 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4261 parse_error_context
4262 (id, "Invalid method declaration, return type required");
4263 /* 8.6.3: Constructor modifiers */
4264 else
4265 {
4266 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4267 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4268 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4269 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4270 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4271 }
4272 /* If we found error here, we don't consider it's OK to tread
4273 the method definition as a constructor, for the rest of this
4274 function */
4275 if (ec == java_error_count)
4276 constructor_ok = 1;
4277 }
e04a16fb
AG
4278
4279 /* Method declared within the scope of an interface are implicitly
4280 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4281 modifiers are checked right after. */
e04a16fb
AG
4282
4283 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4284 {
4285 /* If FLAGS isn't set because of a modifier, turn the
4286 corresponding modifier WFL to NULL so we issue a warning on
4287 the obsolete use of the modifier */
4288 if (!(flags & ACC_PUBLIC))
4289 MODIFIER_WFL (PUBLIC_TK) = NULL;
4290 if (!(flags & ACC_ABSTRACT))
4291 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4292 flags |= ACC_PUBLIC;
4293 flags |= ACC_ABSTRACT;
4294 }
4295
c2952b01
APB
4296 /* Inner class can't declare static methods */
4297 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4298 {
4299 parse_error_context
4300 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4301 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4302 lang_printable_name (this_class, 0));
4303 }
4304
e04a16fb
AG
4305 /* Modifiers context reset moved up, so abstract method declaration
4306 modifiers can be later checked. */
4307
22eed1e6
APB
4308 /* Set constructor returned type to void and method name to <init>,
4309 unless we found an error identifier the constructor (in which
4310 case we retain the original name) */
4311 if (!type)
4312 {
4313 type = void_type_node;
4314 if (constructor_ok)
4315 meth_name = init_identifier_node;
4316 }
4317 else
4318 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4319
1886c9d8
APB
4320 /* Do the returned type resolution and registration if necessary */
4321 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4322
4a5f66c3
APB
4323 if (meth_name)
4324 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4325 EXPR_WFL_NODE (id) = meth_name;
4326 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4327
4328 if (must_chain)
e04a16fb 4329 {
1886c9d8
APB
4330 patch_stage = JDEP_METHOD_RETURN;
4331 register_incomplete_type (patch_stage, type_wfl, id, type);
4332 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4333 }
4334 else
1886c9d8 4335 TREE_TYPE (meth) = type;
e04a16fb
AG
4336
4337 saved_lineno = lineno;
4338 /* When defining an abstract or interface method, the curly
4339 bracket at level 1 doesn't exist because there is no function
4340 body */
4341 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4342 EXPR_WFL_LINENO (id));
4343
5e942c50
APB
4344 /* Remember the original argument list */
4345 orig_arg = TYPE_ARG_TYPES (meth);
4346
e04a16fb
AG
4347 if (patch_stage) /* includes ret type and/or all args */
4348 {
4349 jdep *jdep;
4350 meth = add_method_1 (this_class, flags, meth_name, meth);
4351 /* Patch for the return type */
4352 if (patch_stage == JDEP_METHOD_RETURN)
4353 {
4354 jdep = CLASSD_LAST (ctxp->classd_list);
4355 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4356 }
4357 /* This is the stop JDEP. METH allows the function's signature
4358 to be computed. */
4359 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4360 }
4361 else
5e942c50
APB
4362 meth = add_method (this_class, flags, meth_name,
4363 build_java_signature (meth));
4364
c2952b01
APB
4365 /* Remember final parameters */
4366 MARK_FINAL_PARMS (meth, orig_arg);
4367
5e942c50
APB
4368 /* Fix the method argument list so we have the argument name
4369 information */
4370 fix_method_argument_names (orig_arg, meth);
4371
4372 /* Register the parameter number and re-install the current line
4373 number */
e04a16fb
AG
4374 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4375 lineno = saved_lineno;
b9f7e36c
APB
4376
4377 /* Register exception specified by the `throws' keyword for
4378 resolution and set the method decl appropriate field to the list.
4379 Note: the grammar ensures that what we get here are class
4380 types. */
4381 if (throws)
4382 {
4383 throws = nreverse (throws);
4384 for (current = throws; current; current = TREE_CHAIN (current))
4385 {
4386 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4387 NULL_TREE, NULL_TREE);
4388 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4389 &TREE_VALUE (current);
4390 }
4391 DECL_FUNCTION_THROWS (meth) = throws;
4392 }
4393
e04a16fb
AG
4394 /* We set the DECL_NAME to ID so we can track the location where
4395 the function was declared. This allow us to report
4396 redefinition error accurately. When method are verified,
4397 DECL_NAME is reinstalled properly (using the content of the
4398 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4399 when Object is being defined. Constructor <init> names will be
4400 reinstalled the same way. */
c2952b01 4401 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4402 DECL_NAME (meth) = id;
22eed1e6
APB
4403
4404 /* Set the flag if we correctly processed a constructor */
4405 if (constructor_ok)
c2952b01
APB
4406 {
4407 DECL_CONSTRUCTOR_P (meth) = 1;
4408 /* Compute and store the number of artificial parameters declared
4409 for this constructor */
4410 for (count = 0, current = TYPE_FIELDS (this_class); current;
4411 current = TREE_CHAIN (current))
4412 if (FIELD_LOCAL_ALIAS (current))
4413 count++;
4414 DECL_FUNCTION_NAP (meth) = count;
4415 }
22eed1e6 4416
5e942c50
APB
4417 /* Eventually set the @deprecated tag flag */
4418 CHECK_DEPRECATED (meth);
4419
7f10c2e2
APB
4420 /* If doing xref, store column and line number information instead
4421 of the line number only. */
4422 if (flag_emit_xref)
4423 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4424
e04a16fb
AG
4425 return meth;
4426}
4427
5e942c50
APB
4428static void
4429fix_method_argument_names (orig_arg, meth)
4430 tree orig_arg, meth;
4431{
4432 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4433 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4434 {
4435 TREE_PURPOSE (arg) = this_identifier_node;
4436 arg = TREE_CHAIN (arg);
4437 }
de4c7b02 4438 while (orig_arg != end_params_node)
5e942c50
APB
4439 {
4440 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4441 orig_arg = TREE_CHAIN (orig_arg);
4442 arg = TREE_CHAIN (arg);
4443 }
4444}
4445
22eed1e6
APB
4446/* Complete the method declaration with METHOD_BODY. */
4447
4448static void
b635eb2f 4449finish_method_declaration (method_body)
22eed1e6
APB
4450 tree method_body;
4451{
79d13333
APB
4452 int flags;
4453
4454 if (!current_function_decl)
4455 return;
4456
4457 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4458
4459 /* 8.4.5 Method Body */
4460 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4461 {
4462 tree wfl = DECL_NAME (current_function_decl);
4463 parse_error_context (wfl,
4464 "%s method `%s' can't have a body defined",
4465 (METHOD_NATIVE (current_function_decl) ?
4466 "Native" : "Abstract"),
4467 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4468 method_body = NULL_TREE;
4469 }
4470 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4471 {
4472 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4473 parse_error_context
4474 (wfl,
4475 "Non native and non abstract method `%s' must have a body defined",
4476 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4477 method_body = NULL_TREE;
4478 }
4479
2c56429a
APB
4480 if (flag_emit_class_files && method_body
4481 && TREE_CODE (method_body) == NOP_EXPR
4482 && TREE_TYPE (current_function_decl)
4483 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4484 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4485
22eed1e6
APB
4486 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4487 maybe_absorb_scoping_blocks ();
4488 /* Exit function's body */
4489 exit_block ();
4490 /* Merge last line of the function with first line, directly in the
4491 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4492 if (!flag_emit_xref)
4493 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4494
4495 /* Since function's argument's list are shared, reset the
4496 ARG_FINAL_P parameter that might have been set on some of this
4497 function parameters. */
4498 UNMARK_FINAL_PARMS (current_function_decl);
4499
f099f336
APB
4500 /* So we don't have an irrelevant function declaration context for
4501 the next static block we'll see. */
4502 current_function_decl = NULL_TREE;
22eed1e6
APB
4503}
4504
4505/* Build a an error message for constructor circularity errors. */
4506
4507static char *
4508constructor_circularity_msg (from, to)
4509 tree from, to;
4510{
4511 static char string [4096];
c2e3db92 4512 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4513 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4514 free (t);
4515 return string;
4516}
4517
4518/* Verify a circular call to METH. Return 1 if an error is found, 0
4519 otherwise. */
4520
4521static int
4522verify_constructor_circularity (meth, current)
4523 tree meth, current;
4524{
4525 static tree list = NULL_TREE;
4526 tree c;
4527 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4528 {
4529 if (TREE_VALUE (c) == meth)
4530 {
4531 char *t;
4532 if (list)
4533 {
4534 tree liste;
4535 list = nreverse (list);
4536 for (liste = list; liste; liste = TREE_CHAIN (liste))
4537 {
4538 parse_error_context
c63b98cd 4539 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4540 constructor_circularity_msg
4541 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4542 java_error_count--;
4543 }
4544 }
c2e3db92 4545 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4546 parse_error_context (TREE_PURPOSE (c),
4547 "%s: recursive invocation of constructor `%s'",
4548 constructor_circularity_msg (current, meth), t);
4549 free (t);
4550 list = NULL_TREE;
4551 return 1;
4552 }
4553 }
4554 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4555 {
4556 list = tree_cons (c, current, list);
4557 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4558 return 1;
4559 list = TREE_CHAIN (list);
4560 }
4561 return 0;
4562}
4563
e04a16fb
AG
4564/* Check modifiers that can be declared but exclusively */
4565
4566static void
4567check_modifiers_consistency (flags)
4568 int flags;
4569{
4570 int acc_count = 0;
4571 tree cl = NULL_TREE;
4572
e0fc4118
TT
4573 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4574 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4575 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4576 if (acc_count > 1)
4577 parse_error_context
e0fc4118
TT
4578 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4579
4580 acc_count = 0;
4581 cl = NULL_TREE;
4582 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK - PUBLIC_TK,
4583 acc_count, cl);
4584 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK - PUBLIC_TK,
4585 acc_count, cl);
4586 if (acc_count > 1)
4587 parse_error_context (cl,
4588 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4589}
4590
4591/* Check the methode header METH for abstract specifics features */
4592
4593static void
4594check_abstract_method_header (meth)
4595 tree meth;
4596{
4597 int flags = get_access_flags_from_decl (meth);
4598 /* DECL_NAME might still be a WFL node */
c877974e 4599 tree name = GET_METHOD_NAME (meth);
e04a16fb 4600
2884c41e
KG
4601 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4602 ACC_ABSTRACT, "abstract method",
4603 IDENTIFIER_POINTER (name));
4604 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4605 ACC_PUBLIC, "abstract method",
4606 IDENTIFIER_POINTER (name));
e04a16fb
AG
4607
4608 check_modifiers ("Illegal modifier `%s' for interface method",
4609 flags, INTERFACE_METHOD_MODIFIERS);
4610}
4611
4612/* Create a FUNCTION_TYPE node and start augmenting it with the
4613 declared function arguments. Arguments type that can't be resolved
4614 are left as they are, but the returned node is marked as containing
4615 incomplete types. */
4616
4617static tree
4618method_declarator (id, list)
4619 tree id, list;
4620{
4621 tree arg_types = NULL_TREE, current, node;
4622 tree meth = make_node (FUNCTION_TYPE);
4623 jdep *jdep;
e04a16fb
AG
4624
4625 patch_stage = JDEP_NO_PATCH;
c2952b01
APB
4626
4627 /* If we're dealing with an inner class constructor, we hide the
4628 this$<n> decl in the name field of its parameter declaration. We
4629 also might have to hide the outer context local alias
4630 initializers. Not done when the class is a toplevel class. */
4631 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4632 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4633 {
4634 tree aliases_list, type, thisn;
4635 /* First the aliases, linked to the regular parameters */
4636 aliases_list =
4637 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4638 TREE_TYPE (GET_CPC ()),
4639 NULL_TREE, NULL);
4640 list = chainon (nreverse (aliases_list), list);
4641
4642 /* Then this$<n> */
4643 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4644 thisn = build_current_thisn (TYPE_NAME (GET_CPC ()));
4645 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4646 list);
4647 }
e04a16fb
AG
4648
4649 for (current = list; current; current = TREE_CHAIN (current))
4650 {
c583dd46 4651 int must_chain = 0;
e04a16fb
AG
4652 tree wfl_name = TREE_PURPOSE (current);
4653 tree type = TREE_VALUE (current);
4654 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4655 tree already, arg_node;
4656 tree type_wfl = NULL_TREE;
23a79c61 4657 tree real_type;
c583dd46
APB
4658
4659 /* Obtain a suitable type for resolution, if necessary */
4660 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4661
4662 /* Process NAME, as it may specify extra dimension(s) for it */
4663 type = build_array_from_name (type, type_wfl, name, &name);
4664 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4665
23a79c61
APB
4666 real_type = GET_REAL_TYPE (type);
4667 if (TREE_CODE (real_type) == RECORD_TYPE)
4668 {
4669 real_type = promote_type (real_type);
4670 if (TREE_CODE (type) == TREE_LIST)
4671 TREE_PURPOSE (type) = real_type;
4672 }
5e942c50 4673
e04a16fb
AG
4674 /* Check redefinition */
4675 for (already = arg_types; already; already = TREE_CHAIN (already))
4676 if (TREE_PURPOSE (already) == name)
4677 {
781b0558
KG
4678 parse_error_context
4679 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4680 IDENTIFIER_POINTER (name),
e04a16fb
AG
4681 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4682 break;
4683 }
4684
4685 /* If we've an incomplete argument type, we know there is a location
4686 to patch when the type get resolved, later. */
4687 jdep = NULL;
c583dd46 4688 if (must_chain)
e04a16fb 4689 {
c583dd46
APB
4690 patch_stage = JDEP_METHOD;
4691 type = register_incomplete_type (patch_stage,
4692 type_wfl, wfl_name, type);
4693 jdep = CLASSD_LAST (ctxp->classd_list);
4694 JDEP_MISC (jdep) = id;
e04a16fb 4695 }
c583dd46 4696
c2952b01 4697 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4698 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4699 /* Remeber arguments declared final. */
4700 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4701
e04a16fb
AG
4702 if (jdep)
4703 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4704 TREE_CHAIN (arg_node) = arg_types;
4705 arg_types = arg_node;
4706 }
de4c7b02 4707 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4708 node = build_tree_list (id, meth);
4709 return node;
4710}
4711
4712static int
4713unresolved_type_p (wfl, returned)
4714 tree wfl;
4715 tree *returned;
4716
4717{
4718 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4719 {
e04a16fb 4720 if (returned)
165f37bc
APB
4721 {
4722 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4723 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4724 *returned = TREE_TYPE (decl);
4725 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4726 *returned = TREE_TYPE (GET_CPC ());
4727 else
4728 *returned = NULL_TREE;
4729 }
e04a16fb
AG
4730 return 1;
4731 }
4732 if (returned)
4733 *returned = wfl;
4734 return 0;
4735}
4736
4737/* From NAME, build a qualified identifier node using the
4738 qualification from the current package definition. */
4739
4740static tree
c2952b01
APB
4741parser_qualified_classname (is_static, name)
4742 int is_static;
e04a16fb
AG
4743 tree name;
4744{
c2952b01
APB
4745 tree nested_class_name;
4746
4747 if (!is_static
4748 && (nested_class_name = maybe_make_nested_class_name (name)))
4749 return nested_class_name;
4750
e04a16fb 4751 if (ctxp->package)
c2952b01 4752 return merge_qualified_name (ctxp->package, name);
e04a16fb 4753 else
c2952b01 4754 return name;
e04a16fb
AG
4755}
4756
4757/* Called once the type a interface extends is resolved. Returns 0 if
4758 everything is OK. */
4759
4760static int
4761parser_check_super_interface (super_decl, this_decl, this_wfl)
4762 tree super_decl, this_decl, this_wfl;
4763{
4764 tree super_type = TREE_TYPE (super_decl);
4765
4766 /* Has to be an interface */
c2952b01 4767 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4768 {
4769 parse_error_context
4770 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4771 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4772 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4773 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4774 "interface" : "class"),
4775 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4776 return 1;
4777 }
4778
4779 /* Check scope: same package OK, other package: OK if public */
4780 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4781 return 1;
4782
4783 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4784 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4785 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4786 return 0;
4787}
4788
4789/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4790 0 if everthing is OK. */
4791
4792static int
4793parser_check_super (super_decl, this_decl, wfl)
4794 tree super_decl, this_decl, wfl;
4795{
e04a16fb
AG
4796 tree super_type = TREE_TYPE (super_decl);
4797
4798 /* SUPER should be a CLASS (neither an array nor an interface) */
4799 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4800 {
4801 parse_error_context
4802 (wfl, "Class `%s' can't subclass %s `%s'",
4803 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4804 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4805 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4806 return 1;
4807 }
4808
4809 if (CLASS_FINAL (TYPE_NAME (super_type)))
4810 {
4811 parse_error_context (wfl, "Can't subclass final classes: %s",
4812 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4813 return 1;
4814 }
4815
4816 /* Check scope: same package OK, other package: OK if public */
4817 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4818 return 1;
4819
4820 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4821 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4822 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4823 return 0;
4824}
4825
4826/* Create a new dependency list and link it (in a LIFO manner) to the
4827 CTXP list of type dependency list. */
4828
4829static void
4830create_jdep_list (ctxp)
4831 struct parser_ctxt *ctxp;
4832{
23a79c61 4833 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4834 new->first = new->last = NULL;
4835 new->next = ctxp->classd_list;
4836 ctxp->classd_list = new;
4837}
4838
4839static jdeplist *
4840reverse_jdep_list (ctxp)
4841 struct parser_ctxt *ctxp;
4842{
4843 register jdeplist *prev = NULL, *current, *next;
4844 for (current = ctxp->classd_list; current; current = next)
4845 {
4846 next = current->next;
4847 current->next = prev;
4848 prev = current;
4849 }
4850 return prev;
4851}
4852
23a79c61
APB
4853/* Create a fake pointer based on the ID stored in
4854 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4855 registered again. */
e04a16fb
AG
4856
4857static tree
23a79c61
APB
4858obtain_incomplete_type (type_name)
4859 tree type_name;
e04a16fb 4860{
23a79c61
APB
4861 tree ptr, name;
4862
4863 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4864 name = EXPR_WFL_NODE (type_name);
4865 else if (INCOMPLETE_TYPE_P (type_name))
4866 name = TYPE_NAME (type_name);
4867 else
4868 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4869
4870 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4871 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4872 break;
4873
4874 if (!ptr)
4875 {
e04a16fb 4876 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4877 BUILD_PTR_FROM_NAME (ptr, name);
4878 layout_type (ptr);
e04a16fb
AG
4879 pop_obstacks ();
4880 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4881 ctxp->incomplete_class = ptr;
4882 }
4883
4884 return ptr;
4885}
4886
4887/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4888 non NULL instead of computing a new fake type based on WFL. The new
4889 dependency is inserted in the current type dependency list, in FIFO
4890 manner. */
4891
4892static tree
4893register_incomplete_type (kind, wfl, decl, ptr)
4894 int kind;
4895 tree wfl, decl, ptr;
4896{
23a79c61 4897 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4898
e04a16fb
AG
4899 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4900 ptr = obtain_incomplete_type (wfl);
4901
4902 JDEP_KIND (new) = kind;
4903 JDEP_DECL (new) = decl;
4904 JDEP_SOLV (new) = ptr;
4905 JDEP_WFL (new) = wfl;
4906 JDEP_CHAIN (new) = NULL;
4907 JDEP_MISC (new) = NULL_TREE;
165f37bc
APB
4908 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE)
4909 && GET_ENCLOSING_CPC ())
4910 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4911 else
324ed8fd 4912 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4913 JDEP_GET_PATCH (new) = (tree *)NULL;
4914
4915 JDEP_INSERT (ctxp->classd_list, new);
4916
4917 return ptr;
4918}
4919
4920void
4921java_check_circular_reference ()
4922{
4923 tree current;
4924 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4925 {
4926 tree type = TREE_TYPE (current);
e920ebc9 4927 if (CLASS_INTERFACE (current))
e04a16fb
AG
4928 {
4929 /* Check all interfaces this class extends */
4930 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4931 int n, i;
4932
4933 if (!basetype_vec)
4934 return;
4935 n = TREE_VEC_LENGTH (basetype_vec);
4936 for (i = 0; i < n; i++)
4937 {
4938 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4939 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4940 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4941 parse_error_context (lookup_cl (current),
4942 "Cyclic interface inheritance");
4943 }
4944 }
4945 else
4946 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4947 parse_error_context (lookup_cl (current),
c2952b01
APB
4948 "Cyclic class inheritance%s",
4949 (cyclic_inheritance_report ?
4950 cyclic_inheritance_report : ""));
4951 }
4952}
4953
4954/* Augment the parameter list PARM with parameters crafted to
4955 initialize outer context locals aliases. Through ARTIFICIAL, a
4956 count is kept of the number of crafted parameters. MODE governs
4957 what eventually gets created: something suitable for a function
4958 creation or a function invocation, either the constructor or
4959 $finit$. */
4960
4961static tree
4962build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
4963 int mode;
4964 tree class_type, parm;
4965 int *artificial;
4966{
4967 tree field;
4968 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
4969 if (FIELD_LOCAL_ALIAS (field))
4970 {
4971 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
4972 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
4973
4974 switch (mode)
4975 {
4976 case AIPL_FUNCTION_DECLARATION:
4977 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
4978 purpose = build_wfl_node (get_identifier (buffer));
4979 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
4980 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
4981 else
4982 value = TREE_TYPE (field);
4983 break;
4984
4985 case AIPL_FUNCTION_CREATION:
4986 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
4987 purpose = get_identifier (buffer);
4988 value = TREE_TYPE (field);
4989 break;
4990
4991 case AIPL_FUNCTION_FINIT_INVOCATION:
4992 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
4993 /* Now, this is wrong. purpose should always be the NAME
4994 of something and value its matching value (decl, type,
4995 etc...) FIXME -- but there is a lot to fix. */
4996
4997 /* When invoked for this kind of operation, we already
4998 know whether a field is used or not. */
4999 purpose = TREE_TYPE (field);
5000 value = build_wfl_node (get_identifier (buffer));
5001 break;
5002
5003 case AIPL_FUNCTION_CTOR_INVOCATION:
5004 /* There are two case: the constructor invokation happends
5005 outside the local inner, in which case, locales from the outer
5006 context are directly used.
5007
5008 Otherwise, we fold to using the alias directly. */
5009 if (class_type == current_class)
5010 value = field;
5011 else
5012 {
5013 name = get_identifier (&buffer[4]);
5014 value = IDENTIFIER_LOCAL_VALUE (name);
5015 }
5016 break;
5017 }
5018 parm = tree_cons (purpose, value, parm);
5019 if (artificial)
5020 *artificial +=1;
5021 }
5022 return parm;
5023}
5024
5025/* Craft a constructor for CLASS_DECL -- what we should do when none
5026 where found. ARGS is non NULL when a special signature must be
5027 enforced. This is the case for anonymous classes. */
5028
5029static void
5030craft_constructor (class_decl, args)
5031 tree class_decl, args;
5032{
5033 tree class_type = TREE_TYPE (class_decl);
5034 tree parm = NULL_TREE;
5035 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5036 ACC_PUBLIC : 0);
5037 int i = 0, artificial = 0;
5038 tree decl, ctor_name;
5039 char buffer [80];
5040
5041 push_obstacks (&permanent_obstack, &permanent_obstack);
5042
5043 /* The constructor name is <init> unless we're dealing with an
5044 anonymous class, in which case the name will be fixed after having
5045 be expanded. */
5046 if (ANONYMOUS_CLASS_P (class_type))
5047 ctor_name = DECL_NAME (class_decl);
5048 else
5049 ctor_name = init_identifier_node;
5050
5051 /* If we're dealing with an inner class constructor, we hide the
5052 this$<n> decl in the name field of its parameter declaration. */
5053 if (PURE_INNER_CLASS_TYPE_P (class_type))
5054 {
5055 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5056 parm = tree_cons (build_current_thisn (class_type),
5057 build_pointer_type (type), parm);
5058
5059 /* Some more arguments to be hidden here. The values of the local
5060 variables of the outer context that the inner class needs to see. */
5061 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5062 class_type, parm,
5063 &artificial);
5064 }
5065
5066 /* Then if there are any args to be enforced, enforce them now */
5067 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5068 {
5069 sprintf (buffer, "parm%d", i++);
5070 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5071 }
c2952b01
APB
5072
5073 CRAFTED_PARAM_LIST_FIXUP (parm);
5074 decl = create_artificial_method (class_type, flags, void_type_node,
5075 ctor_name, parm);
5076 fix_method_argument_names (parm, decl);
5077 /* Now, mark the artificial parameters. */
5078 DECL_FUNCTION_NAP (decl) = artificial;
5079
5080 pop_obstacks ();
5081 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5082}
5083
c2952b01 5084
e920ebc9
APB
5085/* Fix the constructors. This will be called right after circular
5086 references have been checked. It is necessary to fix constructors
5087 early even if no code generation will take place for that class:
5088 some generated constructor might be required by the class whose
5089 compilation triggered this one to be simply loaded. */
5090
5091void
5092java_fix_constructors ()
5093{
5094 tree current;
5095
5096 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5097 {
e920ebc9
APB
5098 tree class_type = TREE_TYPE (current);
5099 int saw_ctor = 0;
c2952b01
APB
5100 tree decl;
5101
5102 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5103 continue;
e920ebc9
APB
5104
5105 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5106 {
5107 if (DECL_CONSTRUCTOR_P (decl))
5108 {
5109 fix_constructors (decl);
5110 saw_ctor = 1;
5111 }
5112 }
5113
c2952b01
APB
5114 /* Anonymous class constructor can't be generated that early. */
5115 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5116 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5117 }
5118}
5119
23a79c61
APB
5120/* safe_layout_class just makes sure that we can load a class without
5121 disrupting the current_class, input_file, lineno, etc, information
5122 about the class processed currently. */
5123
e04a16fb
AG
5124void
5125safe_layout_class (class)
5126 tree class;
5127{
5128 tree save_current_class = current_class;
5129 char *save_input_filename = input_filename;
5130 int save_lineno = lineno;
5e942c50 5131
e04a16fb 5132 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5133
e04a16fb
AG
5134 layout_class (class);
5135 pop_obstacks ();
5e942c50 5136
e04a16fb
AG
5137 current_class = save_current_class;
5138 input_filename = save_input_filename;
5139 lineno = save_lineno;
5140 CLASS_LOADED_P (class) = 1;
5141}
5142
5143static tree
5144jdep_resolve_class (dep)
5145 jdep *dep;
5146{
5147 tree decl;
5148
23a79c61
APB
5149 if (JDEP_RESOLVED_P (dep))
5150 decl = JDEP_RESOLVED_DECL (dep);
5151 else
e04a16fb 5152 {
c2952b01 5153 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5154 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5155 JDEP_RESOLVED (dep, decl);
5156 }
23a79c61 5157
e04a16fb 5158 if (!decl)
23a79c61
APB
5159 complete_class_report_errors (dep);
5160
e04a16fb
AG
5161 return decl;
5162}
5163
5164/* Complete unsatisfied class declaration and their dependencies */
5165
5166void
5167java_complete_class ()
5168{
e04a16fb
AG
5169 tree cclass;
5170 jdeplist *cclassd;
5171 int error_found;
b67d701b 5172 tree type;
e04a16fb
AG
5173
5174 push_obstacks (&permanent_obstack, &permanent_obstack);
5175
5176 /* Process imports and reverse the import on demand list */
5177 process_imports ();
5178 if (ctxp->import_demand_list)
5179 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
5180
5181 /* Rever things so we have the right order */
5182 ctxp->class_list = nreverse (ctxp->class_list);
5183 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5184
e04a16fb
AG
5185 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5186 cclass && cclassd;
5187 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5188 {
5189 jdep *dep;
5190 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5191 {
5192 tree decl;
e04a16fb
AG
5193 if (!(decl = jdep_resolve_class (dep)))
5194 continue;
5195
5196 /* Now it's time to patch */
5197 switch (JDEP_KIND (dep))
5198 {
5199 case JDEP_SUPER:
5200 /* Simply patch super */
5201 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5202 continue;
5203 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5204 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5205 break;
5206
5207 case JDEP_FIELD:
5208 {
5209 /* We do part of the job done in add_field */
5210 tree field_decl = JDEP_DECL (dep);
5211 tree field_type = TREE_TYPE (decl);
5212 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5213 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5214 field_type = promote_type (field_type);
5215 pop_obstacks ();
5216 TREE_TYPE (field_decl) = field_type;
5e942c50
APB
5217 DECL_ALIGN (field_decl) = 0;
5218 layout_decl (field_decl, 0);
e04a16fb
AG
5219 SOURCE_FRONTEND_DEBUG
5220 (("Completed field/var decl `%s' with `%s'",
5221 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5222 IDENTIFIER_POINTER (DECL_NAME (decl))));
5223 break;
5224 }
5225 case JDEP_METHOD: /* We start patching a method */
5226 case JDEP_METHOD_RETURN:
5227 error_found = 0;
5228 while (1)
5229 {
5230 if (decl)
5231 {
b67d701b
PB
5232 type = TREE_TYPE(decl);
5233 if (TREE_CODE (type) == RECORD_TYPE)
5234 type = promote_type (type);
e04a16fb
AG
5235 JDEP_APPLY_PATCH (dep, type);
5236 SOURCE_FRONTEND_DEBUG
5237 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5238 "Completing fct `%s' with ret type `%s'":
5239 "Completing arg `%s' with type `%s'"),
5240 IDENTIFIER_POINTER (EXPR_WFL_NODE
5241 (JDEP_DECL_WFL (dep))),
5242 IDENTIFIER_POINTER (DECL_NAME (decl))));
5243 }
5244 else
5245 error_found = 1;
5246 dep = JDEP_CHAIN (dep);
5247 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5248 break;
5249 else
5250 decl = jdep_resolve_class (dep);
5251 }
5252 if (!error_found)
5253 {
5254 tree mdecl = JDEP_DECL (dep), signature;
5255 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5256 /* Recompute and reset the signature, check first that
5257 all types are now defined. If they're not,
5258 dont build the signature. */
5259 if (check_method_types_complete (mdecl))
5260 {
5261 signature = build_java_signature (TREE_TYPE (mdecl));
5262 set_java_signature (TREE_TYPE (mdecl), signature);
5263 }
e04a16fb
AG
5264 pop_obstacks ();
5265 }
5266 else
5267 continue;
5268 break;
5269
5270 case JDEP_INTERFACE:
5271 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5272 JDEP_WFL (dep)))
5273 continue;
5274 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5275 break;
5276
b67d701b 5277 case JDEP_PARM:
e04a16fb 5278 case JDEP_VARIABLE:
b67d701b
PB
5279 type = TREE_TYPE(decl);
5280 if (TREE_CODE (type) == RECORD_TYPE)
5281 type = promote_type (type);
5282 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5283 break;
5284
5285 case JDEP_TYPE:
5286 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5287 SOURCE_FRONTEND_DEBUG
5288 (("Completing a random type dependency on a '%s' node",
5289 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5290 break;
5291
b9f7e36c 5292 case JDEP_EXCEPTION:
c877974e
APB
5293 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5294 SOURCE_FRONTEND_DEBUG
5295 (("Completing `%s' `throws' argument node",
5296 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5297 break;
5298
c2952b01
APB
5299 case JDEP_ANONYMOUS:
5300 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5301 break;
5302
e04a16fb 5303 default:
0a2138e2
APB
5304 fatal ("Can't handle patch code %d - java_complete_class",
5305 JDEP_KIND (dep));
e04a16fb
AG
5306 }
5307 }
5308 }
5309 pop_obstacks ();
5310 return;
5311}
5312
5313/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5314 array. */
5315
5316static tree
c2952b01
APB
5317resolve_class (enclosing, class_type, decl, cl)
5318 tree enclosing, class_type, decl, cl;
e04a16fb 5319{
49f48c71
KG
5320 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5321 const char *base = name;
78d21f92
PB
5322 tree resolved_type = TREE_TYPE (class_type);
5323 tree resolved_type_decl;
e04a16fb 5324
78d21f92
PB
5325 if (resolved_type != NULL_TREE)
5326 {
5327 tree resolved_type_decl = TYPE_NAME (resolved_type);
5328 if (resolved_type_decl == NULL_TREE
5329 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5330 {
5331 resolved_type_decl = build_decl (TYPE_DECL,
5332 TYPE_NAME (class_type),
5333 resolved_type);
5334 }
5335 return resolved_type_decl;
5336 }
5337
e04a16fb
AG
5338 /* 1- Check to see if we have an array. If true, find what we really
5339 want to resolve */
5340 while (name[0] == '[')
5341 name++;
5342 if (base != name)
5343 TYPE_NAME (class_type) = get_identifier (name);
5344
5345 /* 2- Resolve the bare type */
c2952b01
APB
5346 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5347 decl, cl)))
e04a16fb
AG
5348 return NULL_TREE;
5349 resolved_type = TREE_TYPE (resolved_type_decl);
5350
5351 /* 3- If we have and array, reconstruct the array down to its nesting */
5352 if (base != name)
5353 {
5354 while (base != name)
5355 {
5356 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5357 resolved_type = promote_type (resolved_type);
5358 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5359 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5360 name--;
5361 }
5362 /* Build a fake decl for this, since this is what is expected to
5363 be returned. */
5364 resolved_type_decl =
5365 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5366 /* Figure how those two things are important for error report. FIXME */
5367 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5368 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5369 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5370 }
78d21f92 5371 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5372 return resolved_type_decl;
5373}
5374
5375/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5376 are used to report error messages. */
5377
78d21f92 5378tree
c2952b01
APB
5379do_resolve_class (enclosing, class_type, decl, cl)
5380 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5381{
5382 tree new_class_decl;
5383 tree original_name = NULL_TREE;
5384
5385 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5386 its is changed by find_in_imports{_on_demand} */
5387
c2952b01
APB
5388 /* 0- Search in the current class as an inner class */
5389
5390 /* Maybe some code here should be added to load the class or
5391 something, at least if the class isn't an inner class and ended
5392 being loaded from class file. FIXME. */
5393 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5394 return new_class_decl;
5395
e04a16fb
AG
5396 /* 1- Check for the type in single imports */
5397 if (find_in_imports (class_type))
5398 return NULL_TREE;
5399
5400 /* 2- And check for the type in the current compilation unit. If it fails,
ee07f4f4 5401 try with a name qualified with the package name we've seen so far */
e04a16fb
AG
5402 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5403 {
5404 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5405 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5406 load_class (TYPE_NAME (class_type), 0);
5407 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5408 }
5409
5410 original_name = TYPE_NAME (class_type);
ee07f4f4 5411 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5412 {
ee07f4f4
APB
5413 tree package;
5414 for (package = package_list; package; package = TREE_CHAIN (package))
c2952b01
APB
5415 {
5416 tree new_qualified;
5417
5418 new_qualified = merge_qualified_name (TREE_PURPOSE (package),
5419 original_name);
5420 TYPE_NAME (class_type) = new_qualified;
5421 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5422 if (!new_class_decl)
5423 load_class (TYPE_NAME (class_type), 0);
5424 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5425 if (new_class_decl)
5426 {
5427 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5428 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5429 load_class (TYPE_NAME (class_type), 0);
5430 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5431 }
bc3ca41b
PB
5432 }
5433 }
c2952b01 5434
e04a16fb
AG
5435 TYPE_NAME (class_type) = original_name;
5436
5437 /* 3- Check an other compilation unit that bears the name of type */
5438 load_class (TYPE_NAME (class_type), 0);
5439 if (check_pkg_class_access (TYPE_NAME (class_type),
5440 (cl ? cl : lookup_cl (decl))))
5441 return NULL_TREE;
5442
5443 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5444 return new_class_decl;
5445
5446 /* 4- Check the import on demands. Don't allow bar.baz to be
5447 imported from foo.* */
5448 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5449 if (find_in_imports_on_demand (class_type))
5450 return NULL_TREE;
5451
5452 /* 5- Last call for a resolution */
5453 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5454}
5455
5456/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5457 parsed class). Return a decl node. This function is meant to be
5458 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5459
5460static tree
c877974e
APB
5461resolve_and_layout (something, cl)
5462 tree something;
e04a16fb
AG
5463 tree cl;
5464{
c877974e
APB
5465 tree decl;
5466
23a79c61
APB
5467 /* Don't do that on the current class */
5468 if (something == current_class)
5469 return TYPE_NAME (current_class);
c877974e 5470
23a79c61 5471 /* Don't do anything for void and other primitive types */
c877974e
APB
5472 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5473 return NULL_TREE;
5474
23a79c61
APB
5475 /* Pointer types can be reall pointer types or fake pointers. When
5476 finding a real pointer, recheck for primitive types */
5477 if (TREE_CODE (something) == POINTER_TYPE)
5478 {
5479 if (TREE_TYPE (something))
5480 {
5481 something = TREE_TYPE (something);
5482 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5483 return NULL_TREE;
5484 }
5485 else
5486 something = TYPE_NAME (something);
5487 }
5488
5489 /* Don't do anything for arrays of primitive types */
5490 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5491 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5492 return NULL_TREE;
5493
c2952b01
APB
5494 /* Something might be a WFL */
5495 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5496 something = EXPR_WFL_NODE (something);
5497
5498 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5499 TYPE_DECL or a real TYPE */
5500 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5501 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5502 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5503
23a79c61
APB
5504 if (!(decl = resolve_no_layout (something, cl)))
5505 return NULL_TREE;
5506
5507 /* Resolve and layout if necessary */
5508 layout_class_methods (TREE_TYPE (decl));
7705e9db
APB
5509 /* Check methods, but only once */
5510 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5511 && !CLASS_LOADED_P (TREE_TYPE (decl)))
23a79c61
APB
5512 CHECK_METHODS (decl);
5513 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
e04a16fb 5514 safe_layout_class (TREE_TYPE (decl));
23a79c61 5515
e04a16fb
AG
5516 return decl;
5517}
5518
5519/* Resolve a class, returns its decl but doesn't perform any
5520 layout. The current parsing context is saved and restored */
5521
5522static tree
5523resolve_no_layout (name, cl)
5524 tree name, cl;
5525{
5526 tree ptr, decl;
5527 BUILD_PTR_FROM_NAME (ptr, name);
5528 java_parser_context_save_global ();
c2952b01 5529 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5530 java_parser_context_restore_global ();
5531
5532 return decl;
5533}
5534
23a79c61
APB
5535/* Called when reporting errors. Skip leader '[' in a complex array
5536 type description that failed to be resolved. */
e04a16fb 5537
49f48c71 5538static const char *
e04a16fb 5539purify_type_name (name)
49f48c71 5540 const char *name;
e04a16fb
AG
5541{
5542 while (*name && *name == '[')
5543 name++;
5544 return name;
5545}
5546
5547/* The type CURRENT refers to can't be found. We print error messages. */
5548
5549static void
5550complete_class_report_errors (dep)
5551 jdep *dep;
5552{
49f48c71 5553 const char *name;
23a79c61
APB
5554
5555 if (!JDEP_WFL (dep))
5556 return;
5557
5558 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5559 switch (JDEP_KIND (dep))
5560 {
5561 case JDEP_SUPER:
5562 parse_error_context
5563 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5564 purify_type_name (name),
e04a16fb
AG
5565 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5566 break;
5567 case JDEP_FIELD:
5568 parse_error_context
5569 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5570 purify_type_name (name),
e04a16fb
AG
5571 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5572 break;
5573 case JDEP_METHOD: /* Covers arguments */
5574 parse_error_context
781b0558 5575 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5576 purify_type_name (name),
e04a16fb
AG
5577 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5578 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5579 break;
5580 case JDEP_METHOD_RETURN: /* Covers return type */
5581 parse_error_context
781b0558 5582 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5583 purify_type_name (name),
e04a16fb
AG
5584 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5585 break;
5586 case JDEP_INTERFACE:
5587 parse_error_context
5588 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5589 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5590 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5591 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5592 break;
5593 case JDEP_VARIABLE:
5594 parse_error_context
781b0558 5595 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5596 purify_type_name (IDENTIFIER_POINTER
5597 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5598 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5599 break;
b9f7e36c
APB
5600 case JDEP_EXCEPTION: /* As specified by `throws' */
5601 parse_error_context
5602 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5603 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5604 break;
0a2138e2
APB
5605 default:
5606 /* Fix for -Wall. Just break doing nothing. The error will be
5607 caught later */
5608 break;
e04a16fb
AG
5609 }
5610}
5611
22eed1e6
APB
5612/* Return a static string containing the DECL prototype string. If
5613 DECL is a constructor, use the class name instead of the form
5614 <init> */
5615
49f48c71 5616static const char *
22eed1e6
APB
5617get_printable_method_name (decl)
5618 tree decl;
5619{
49f48c71 5620 const char *to_return;
9ee9b555 5621 tree name = NULL_TREE;
22eed1e6
APB
5622
5623 if (DECL_CONSTRUCTOR_P (decl))
5624 {
5625 name = DECL_NAME (decl);
5e942c50 5626 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5627 }
5628
5629 to_return = lang_printable_name (decl, 0);
5630 if (DECL_CONSTRUCTOR_P (decl))
5631 DECL_NAME (decl) = name;
5632
5633 return to_return;
5634}
5635
5e942c50
APB
5636/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5637 nevertheless needs to be verfied, 1 otherwise. */
5638
5639static int
5640reset_method_name (method)
5641 tree method;
5642{
c2952b01 5643 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5644 {
5645 /* NAME is just the plain name when Object is being defined */
5646 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5647 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5648 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5649 return 0;
5650 }
5651 else
5652 return 1;
5653}
5654
c877974e
APB
5655/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5656
5657tree
5658java_get_real_method_name (method_decl)
5659 tree method_decl;
5660{
5661 tree method_name = DECL_NAME (method_decl);
5662 if (DECL_CONSTRUCTOR_P (method_decl))
5663 return init_identifier_node;
82371d41
APB
5664
5665 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5666 and still can be a constructor. FIXME */
5667
23a79c61
APB
5668 /* Don't confuse method only bearing the name of their class as
5669 constructors */
82371d41
APB
5670 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5671 && ctxp
c2952b01 5672 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5673 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5674 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5675 return init_identifier_node;
5676 else
5677 return EXPR_WFL_NODE (method_name);
5678}
5679
22eed1e6
APB
5680/* Track method being redefined inside the same class. As a side
5681 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5682 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5683
e04a16fb
AG
5684static int
5685check_method_redefinition (class, method)
5686 tree class, method;
5687{
5688 tree redef, name;
5689 tree cl = DECL_NAME (method);
c3f2a476 5690 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
5691 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5692 fixed and checked */
5e942c50
APB
5693
5694 /* Reset the method name before running the check. If it returns 1,
5695 the method doesn't need to be verified with respect to method
5696 redeclaration and we return 0 */
5697 if (reset_method_name (method))
e04a16fb 5698 return 0;
5e942c50
APB
5699
5700 name = DECL_NAME (method);
e04a16fb
AG
5701 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5702 {
c3f2a476 5703 if (redef == method)
e04a16fb 5704 break;
c3f2a476
APB
5705 if (DECL_NAME (redef) == name
5706 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5707 {
22eed1e6
APB
5708 parse_error_context
5709 (cl, "Duplicate %s declaration `%s'",
5710 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5711 get_printable_method_name (redef));
e04a16fb
AG
5712 return 1;
5713 }
5714 }
5715 return 0;
5716}
5717
d77613be
APB
5718static void
5719check_abstract_method_definitions (do_interface, class_decl, type)
5720 int do_interface;
5721 tree class_decl, type;
5722{
5723 tree class = TREE_TYPE (class_decl);
5724 tree method, end_type;
5725
5726 end_type = (do_interface ? object_type_node : type);
5727 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5728 {
5729 tree other_super, other_method, method_sig, method_name;
5730 int found = 0;
165f37bc 5731 int end_type_reached = 0;
d77613be
APB
5732
5733 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5734 continue;
5735
5736 /* Now verify that somewhere in between TYPE and CLASS,
5737 abstract method METHOD gets a non abstract definition
5738 that is inherited by CLASS. */
5739
5740 method_sig = build_java_signature (TREE_TYPE (method));
5741 method_name = DECL_NAME (method);
5742 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5743 method_name = EXPR_WFL_NODE (method_name);
5744
165f37bc
APB
5745 other_super = class;
5746 do {
5747 if (other_super == end_type)
5748 end_type_reached = 1;
5749
5750 /* Method search */
5751 for (other_method = TYPE_METHODS (other_super); other_method;
5752 other_method = TREE_CHAIN (other_method))
5753 {
5754 tree s = build_java_signature (TREE_TYPE (other_method));
5755 tree other_name = DECL_NAME (other_method);
5756
5757 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5758 other_name = EXPR_WFL_NODE (other_name);
5759 if (!DECL_CLINIT_P (other_method)
5760 && !DECL_CONSTRUCTOR_P (other_method)
5761 && method_name == other_name && method_sig == s)
5762 {
5763 found = 1;
5764 break;
5765 }
5766 }
5767 other_super = CLASSTYPE_SUPER (other_super);
5768 } while (!end_type_reached);
5769
d77613be
APB
5770 /* Report that abstract METHOD didn't find an implementation
5771 that CLASS can use. */
5772 if (!found)
5773 {
c2e3db92 5774 char *t = xstrdup (lang_printable_name
d77613be
APB
5775 (TREE_TYPE (TREE_TYPE (method)), 0));
5776 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5777 tree saved_wfl = NULL_TREE;
5778
5779 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5780 {
5781 saved_wfl = DECL_NAME (method);
5782 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5783 }
5784
5785 parse_error_context
5786 (lookup_cl (class_decl),
781b0558 5787 "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
5788 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5789 t, lang_printable_name (method, 0),
5790 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5791 "interface" : "class"),
5792 IDENTIFIER_POINTER (ccn),
5793 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5794 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5795
5796 free (t);
5797
5798 if (saved_wfl)
5799 DECL_NAME (method) = saved_wfl;
5800 }
5801 }
5802}
5803
614eaae0 5804/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5805 methods. */
5806
5807static void
5808java_check_abstract_method_definitions (class_decl)
5809 tree class_decl;
5810{
5811 tree class = TREE_TYPE (class_decl);
5812 tree super, vector;
5813 int i;
5814
5815 if (CLASS_ABSTRACT (class_decl))
5816 return;
5817
5818 /* Check for inherited types */
165f37bc
APB
5819 super = class;
5820 do {
5821 super = CLASSTYPE_SUPER (super);
5822 check_abstract_method_definitions (0, class_decl, super);
5823 } while (super != object_type_node);
d77613be
APB
5824
5825 /* Check for implemented interfaces. */
5826 vector = TYPE_BINFO_BASETYPES (class);
5827 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5828 {
5829 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5830 check_abstract_method_definitions (1, class_decl, super);
5831 }
5832}
5833
165f37bc
APB
5834/* Check all the types method DECL uses and return 1 if all of them
5835 are now complete, 0 otherwise. This is used to check whether its
5836 safe to build a method signature or not. */
5837
5838static int
5839check_method_types_complete (decl)
5840 tree decl;
5841{
5842 tree type = TREE_TYPE (decl);
5843 tree args;
5844
5845 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5846 return 0;
5847
5848 args = TYPE_ARG_TYPES (type);
5849 if (TREE_CODE (type) == METHOD_TYPE)
5850 args = TREE_CHAIN (args);
5851 for (; args != end_params_node; args = TREE_CHAIN (args))
5852 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5853 return 0;
5854
5855 return 1;
5856}
5857
d77613be
APB
5858/* Check all the methods of CLASS_DECL. Methods are first completed
5859 then checked according to regular method existance rules. If no
5860 constructor for CLASS_DECL were encountered, then build its
5861 declaration. */
e04a16fb
AG
5862
5863static void
5864java_check_regular_methods (class_decl)
5865 tree class_decl;
5866{
c2952b01 5867 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
5868 tree method;
5869 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 5870 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
5871 tree mthrows;
5872
5873 /* It is not necessary to check methods defined in java.lang.Object */
5874 if (class == object_type_node)
5875 return;
e04a16fb 5876
23a79c61
APB
5877 if (!TYPE_NVIRTUALS (class))
5878 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
5879
5880 /* Should take interfaces into account. FIXME */
5881 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5882 {
5e942c50 5883 tree sig;
e04a16fb
AG
5884 tree method_wfl = DECL_NAME (method);
5885 int aflags;
5886
5e942c50
APB
5887 /* If we previously found something and its name was saved,
5888 reinstall it now */
5889 if (found && saved_found_wfl)
ba179f9f
APB
5890 {
5891 DECL_NAME (found) = saved_found_wfl;
5892 saved_found_wfl = NULL_TREE;
5893 }
5e942c50 5894
e04a16fb
AG
5895 /* Check for redefinitions */
5896 if (check_method_redefinition (class, method))
5897 continue;
5898
22eed1e6
APB
5899 /* If we see one constructor a mark so we don't generate the
5900 default one. Also skip other verifications: constructors
5901 can't be inherited hence hiden or overriden */
5902 if (DECL_CONSTRUCTOR_P (method))
5903 {
5904 saw_constructor = 1;
5905 continue;
5906 }
5907
c877974e
APB
5908 /* We verify things thrown by the method. They must inherits from
5909 java.lang.Throwable */
5910 for (mthrows = DECL_FUNCTION_THROWS (method);
5911 mthrows; mthrows = TREE_CHAIN (mthrows))
5912 {
5913 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
5914 parse_error_context
781b0558 5915 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
5916 IDENTIFIER_POINTER
5917 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
5918 }
5919
e04a16fb 5920 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 5921 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 5922
c2952b01
APB
5923 /* Inner class can't declare static methods */
5924 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
5925 {
5926 char *t = xstrdup (lang_printable_name (class, 0));
5927 parse_error_context
5928 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
5929 lang_printable_name (method, 0), t);
5930 free (t);
5931 }
5932
5e942c50 5933 /* Nothing overrides or it's a private method. */
aabd7048 5934 if (!found)
5e942c50 5935 continue;
aabd7048
PB
5936 if (METHOD_PRIVATE (found))
5937 {
5938 found = NULL_TREE;
5939 continue;
5940 }
5e942c50
APB
5941
5942 /* If found wasn't verified, it's DECL_NAME won't be set properly.
5943 We set it temporarily for the sake of the error report. */
5944 saved_found_wfl = DECL_NAME (found);
5945 reset_method_name (found);
5946
614eaae0
APB
5947 /* If `found' is declared in an interface, make sure the
5948 modifier matches. */
5949 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
5950 && clinit_identifier_node != DECL_NAME (found)
5951 && !METHOD_PUBLIC (method))
5952 {
5953 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
5954 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
5955 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5956 lang_printable_name (method, 0),
5957 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
5958 }
5959
e04a16fb
AG
5960 /* Can't override a method with the same name and different return
5961 types. */
5962 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 5963 {
614eaae0
APB
5964 char *t = xstrdup
5965 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 5966 parse_error_context
7f10c2e2 5967 (method_wfl,
b9f7e36c 5968 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 5969 lang_printable_name (found, 0), t,
b9f7e36c
APB
5970 IDENTIFIER_POINTER
5971 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
5972 free (t);
5973 }
e04a16fb 5974
7f10c2e2
APB
5975 aflags = get_access_flags_from_decl (found);
5976 /* If the method has default, access in an other package, then
5977 issue a warning that the current method doesn't override the
5978 one that was found elsewhere. Do not issue this warning when
5979 the match was found in java.lang.Object. */
5980 if (DECL_CONTEXT (found) != object_type_node
a003f638 5981 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 5982 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 5983 && !DECL_CLINIT_P (found)
7f10c2e2
APB
5984 && flag_not_overriding)
5985 {
5986 parse_warning_context
781b0558 5987 (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
5988 lang_printable_name (found, 0),
5989 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5990 IDENTIFIER_POINTER (DECL_NAME
5991 (TYPE_NAME (DECL_CONTEXT (found)))));
5992 continue;
5993 }
5994
e04a16fb
AG
5995 /* Can't override final. Can't override static. */
5996 if (METHOD_FINAL (found) || METHOD_STATIC (found))
5997 {
5998 /* Static *can* override static */
5999 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6000 continue;
6001 parse_error_context
6002 (method_wfl,
6003 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6004 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6005 lang_printable_name (found, 0),
e04a16fb
AG
6006 (METHOD_FINAL (found) ? "final" : "static"),
6007 IDENTIFIER_POINTER
6008 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6009 continue;
6010 }
7f10c2e2 6011
e04a16fb
AG
6012 /* Static method can't override instance method. */
6013 if (METHOD_STATIC (method))
6014 {
6015 parse_error_context
6016 (method_wfl,
781b0558 6017 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6018 lang_printable_name (found, 0),
e04a16fb
AG
6019 IDENTIFIER_POINTER
6020 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6021 continue;
6022 }
5e942c50 6023
5e942c50
APB
6024 /* - Overriding/hiding public must be public
6025 - Overriding/hiding protected must be protected or public
6026 - If the overriden or hidden method has default (package)
6027 access, then the overriding or hiding method must not be
614eaae0
APB
6028 private; otherwise, a compile-time error occurs. If
6029 `found' belongs to an interface, things have been already
6030 taken care of. */
6031 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6032 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6033 || (METHOD_PROTECTED (found)
6034 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6035 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6036 && METHOD_PRIVATE (method))))
e04a16fb
AG
6037 {
6038 parse_error_context
6039 (method_wfl,
781b0558 6040 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6041 (METHOD_PUBLIC (method) ? "public" :
6042 (METHOD_PRIVATE (method) ? "private" : "protected")),
6043 IDENTIFIER_POINTER (DECL_NAME
6044 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6045 continue;
6046 }
6047
b9f7e36c
APB
6048 /* Overriding methods must have compatible `throws' clauses on checked
6049 exceptions, if any */
6050 check_throws_clauses (method, method_wfl, found);
6051
e04a16fb
AG
6052 /* Inheriting multiple methods with the same signature. FIXME */
6053 }
6054
5e942c50
APB
6055 /* Don't forget eventual pending found and saved_found_wfl. Take
6056 into account that we might have exited because we saw an
d77613be 6057 artificial method as the last entry. */
5e942c50
APB
6058
6059 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6060 DECL_NAME (found) = saved_found_wfl;
6061
23a79c61
APB
6062 if (!TYPE_NVIRTUALS (class))
6063 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6064
d77613be
APB
6065 /* Search for inherited abstract method not yet implemented in this
6066 class. */
6067 java_check_abstract_method_definitions (class_decl);
6068
22eed1e6 6069 if (!saw_constructor)
e920ebc9 6070 fatal ("No constructor found");
e04a16fb
AG
6071}
6072
b9f7e36c
APB
6073/* Return a non zero value if the `throws' clause of METHOD (if any)
6074 is incompatible with the `throws' clause of FOUND (if any). */
6075
6076static void
6077check_throws_clauses (method, method_wfl, found)
6078 tree method, method_wfl, found;
6079{
6080 tree mthrows, fthrows;
6081
c877974e
APB
6082 /* Can't check these things with class loaded from bytecode. FIXME */
6083 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6084 return;
6085
b9f7e36c
APB
6086 for (mthrows = DECL_FUNCTION_THROWS (method);
6087 mthrows; mthrows = TREE_CHAIN (mthrows))
6088 {
6089 /* We don't verify unchecked expressions */
c877974e 6090 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6091 continue;
6092 /* Checked expression must be compatible */
6093 for (fthrows = DECL_FUNCTION_THROWS (found);
6094 fthrows; fthrows = TREE_CHAIN (fthrows))
6095 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6096 break;
6097 if (!fthrows)
6098 {
6099 parse_error_context
781b0558 6100 (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 6101 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6102 lang_printable_name (found, 0),
b9f7e36c
APB
6103 IDENTIFIER_POINTER
6104 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6105 }
6106 }
6107}
6108
e04a16fb
AG
6109/* Check abstract method of interface INTERFACE */
6110
6111static void
5e942c50
APB
6112java_check_abstract_methods (interface_decl)
6113 tree interface_decl;
e04a16fb
AG
6114{
6115 int i, n;
6116 tree method, basetype_vec, found;
5e942c50 6117 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6118
6119 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6120 {
b9f7e36c 6121 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6122
6123 /* 2- Check for double definition inside the defining interface */
6124 if (check_method_redefinition (interface, method))
6125 continue;
6126
6127 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6128 the thrown exceptions (FIXME) */
e04a16fb
AG
6129 found = lookup_java_interface_method2 (interface, method);
6130 if (found)
6131 {
5e942c50
APB
6132 char *t;
6133 tree saved_found_wfl = DECL_NAME (found);
6134 reset_method_name (found);
c2e3db92 6135 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6136 parse_error_context
b9f7e36c 6137 (method_wfl,
5e942c50 6138 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6139 lang_printable_name (found, 0), t,
b9f7e36c
APB
6140 IDENTIFIER_POINTER
6141 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6142 free (t);
5e942c50 6143 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6144 continue;
e04a16fb
AG
6145 }
6146 }
6147
6148 /* 4- Inherited methods can't differ by their returned types */
6149 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6150 return;
6151 n = TREE_VEC_LENGTH (basetype_vec);
6152 for (i = 0; i < n; i++)
6153 {
6154 tree sub_interface_method, sub_interface;
6155 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6156 if (!vec_elt)
6157 continue;
6158 sub_interface = BINFO_TYPE (vec_elt);
6159 for (sub_interface_method = TYPE_METHODS (sub_interface);
6160 sub_interface_method;
6161 sub_interface_method = TREE_CHAIN (sub_interface_method))
6162 {
6163 found = lookup_java_interface_method2 (interface,
6164 sub_interface_method);
6165 if (found && (found != sub_interface_method))
5e942c50
APB
6166 {
6167 tree saved_found_wfl = DECL_NAME (found);
6168 reset_method_name (found);
6169 parse_error_context
6170 (lookup_cl (sub_interface_method),
781b0558 6171 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6172 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6173 lang_printable_name (found, 0),
6174 IDENTIFIER_POINTER
6175 (DECL_NAME (TYPE_NAME
6176 (DECL_CONTEXT (sub_interface_method)))),
6177 IDENTIFIER_POINTER
6178 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6179 DECL_NAME (found) = saved_found_wfl;
6180 }
e04a16fb
AG
6181 }
6182 }
6183}
6184
e04a16fb
AG
6185/* Lookup methods in interfaces using their name and partial
6186 signature. Return a matching method only if their types differ. */
6187
6188static tree
6189lookup_java_interface_method2 (class, method_decl)
6190 tree class, method_decl;
6191{
6192 int i, n;
6193 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6194
6195 if (!basetype_vec)
6196 return NULL_TREE;
6197
6198 n = TREE_VEC_LENGTH (basetype_vec);
6199 for (i = 0; i < n; i++)
6200 {
6201 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6202 if ((BINFO_TYPE (vec_elt) != object_type_node)
6203 && (to_return =
6204 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6205 return to_return;
6206 }
6207 for (i = 0; i < n; i++)
6208 {
6209 to_return = lookup_java_interface_method2
6210 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6211 if (to_return)
6212 return to_return;
6213 }
6214
6215 return NULL_TREE;
6216}
6217
6218/* Lookup method using their name and partial signature. Return a
6219 matching method only if their types differ. */
6220
6221static tree
6222lookup_java_method2 (clas, method_decl, do_interface)
6223 tree clas, method_decl;
6224 int do_interface;
6225{
5e942c50
APB
6226 tree method, method_signature, method_name, method_type, name;
6227
e04a16fb 6228 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6229 name = DECL_NAME (method_decl);
6230 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6231 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6232 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6233
6234 while (clas != NULL_TREE)
6235 {
6236 for (method = TYPE_METHODS (clas);
6237 method != NULL_TREE; method = TREE_CHAIN (method))
6238 {
6239 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6240 tree name = DECL_NAME (method);
6241 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6242 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6243 && method_sig == method_signature
6244 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6245 return method;
e04a16fb
AG
6246 }
6247 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6248 }
6249 return NULL_TREE;
6250}
6251
f441f671
APB
6252/* Return the line that matches DECL line number, and try its best to
6253 position the column number. Used during error reports. */
e04a16fb
AG
6254
6255static tree
6256lookup_cl (decl)
6257 tree decl;
6258{
6259 static tree cl = NULL_TREE;
f441f671 6260 char *line, *found;
e04a16fb
AG
6261
6262 if (!decl)
6263 return NULL_TREE;
6264
6265 if (cl == NULL_TREE)
6266 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6267
6268 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6269 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6270
f441f671
APB
6271 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6272 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6273
6274 found = strstr ((const char *)line,
6275 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6276 if (found)
6277 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6278
e04a16fb
AG
6279 return cl;
6280}
6281
6282/* Look for a simple name in the single-type import list */
6283
6284static tree
6285find_name_in_single_imports (name)
6286 tree name;
6287{
6288 tree node;
6289
6290 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6291 if (TREE_VALUE (node) == name)
6292 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6293
6294 return NULL_TREE;
6295}
6296
6297/* Process all single-type import. */
6298
6299static int
6300process_imports ()
6301{
6302 tree import;
6303 int error_found;
6304
6305 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6306 {
6307 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6308
6309 /* Don't load twice something already defined. */
6310 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6311 continue;
6312 QUALIFIED_P (to_be_found) = 1;
6313 load_class (to_be_found, 0);
6314 error_found =
6315 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6316 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6317 {
6318 parse_error_context (TREE_PURPOSE (import),
6319 "Class or interface `%s' not found in import",
6320 IDENTIFIER_POINTER (to_be_found));
6321 return 1;
6322 }
6323 if (error_found)
6324 return 1;
6325 }
6326 return 0;
6327}
6328
6329/* Possibly find a class imported by a single-type import statement. Return
6330 1 if an error occured, 0 otherwise. */
6331
6332static int
6333find_in_imports (class_type)
6334 tree class_type;
6335{
6336 tree import;
6337
6338 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6339 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6340 {
6341 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6342 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb
AG
6343 }
6344 return 0;
6345}
6346
e04a16fb 6347static int
63a212ed 6348note_possible_classname (name, len)
49f48c71 6349 const char *name;
63a212ed 6350 int len;
e04a16fb 6351{
63a212ed
PB
6352 tree node;
6353 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6354 len = len - 5;
6355 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6356 len = len - 6;
e04a16fb 6357 else
63a212ed
PB
6358 return 0;
6359 node = ident_subst (name, len, "", '/', '.', "");
6360 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6361 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6362 return 1;
e04a16fb
AG
6363}
6364
6365/* Read a import directory, gathering potential match for further type
6366 references. Indifferently reads a filesystem or a ZIP archive
6367 directory. */
6368
6369static void
6370read_import_dir (wfl)
6371 tree wfl;
6372{
63a212ed 6373 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6374 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6375 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6376 DIR *dirp = NULL;
d8fccff5 6377 JCF *saved_jcf = current_jcf;
e04a16fb 6378
63a212ed
PB
6379 int found = 0;
6380 int k;
6381 void *entry;
6382 struct buffer filename[1];
6383
6384
6385 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6386 return;
6387 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6388
6389 BUFFER_INIT (filename);
6390 buffer_grow (filename, package_length + 100);
6391
6392 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6393 {
49f48c71 6394 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6395 int entry_length = strlen (entry_name);
6396 if (jcf_path_is_zipfile (entry))
6397 {
6398 ZipFile *zipf;
6399 buffer_grow (filename, entry_length);
6400 memcpy (filename->data, entry_name, entry_length - 1);
6401 filename->data[entry_length-1] = '\0';
6402 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6403 if (zipf == NULL)
6404 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6405 else
6406 {
6407 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6408 BUFFER_RESET (filename);
6409 for (k = 0; k < package_length; k++)
6410 {
6411 char ch = package_name[k];
6412 *filename->ptr++ = ch == '.' ? '/' : ch;
6413 }
6414 *filename->ptr++ = '/';
6415
345137c7 6416 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6417 {
49f48c71 6418 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6419 int current_entry_len = zipd->filename_length;
6420
345137c7
TT
6421 if (current_entry_len >= BUFFER_LENGTH (filename)
6422 && strncmp (filename->data, current_entry,
6423 BUFFER_LENGTH (filename)) != 0)
63a212ed 6424 continue;
345137c7 6425 found |= note_possible_classname (current_entry,
63a212ed
PB
6426 current_entry_len);
6427 }
6428 }
6429 }
6430 else
6431 {
6432 BUFFER_RESET (filename);
6433 buffer_grow (filename, entry_length + package_length + 4);
6434 strcpy (filename->data, entry_name);
6435 filename->ptr = filename->data + entry_length;
6436 for (k = 0; k < package_length; k++)
6437 {
6438 char ch = package_name[k];
6439 *filename->ptr++ = ch == '.' ? '/' : ch;
6440 }
6441 *filename->ptr = '\0';
6442
6443 dirp = opendir (filename->data);
6444 if (dirp == NULL)
6445 continue;
6446 *filename->ptr++ = '/';
6447 for (;;)
6448 {
63a212ed 6449 int len;
49f48c71 6450 const char *d_name;
63a212ed
PB
6451 struct dirent *direntp = readdir (dirp);
6452 if (!direntp)
6453 break;
6454 d_name = direntp->d_name;
6455 len = strlen (direntp->d_name);
6456 buffer_grow (filename, len+1);
6457 strcpy (filename->ptr, d_name);
345137c7 6458 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6459 package_length+len+1);
6460 }
6461 if (dirp)
6462 closedir (dirp);
6463 }
6464 }
e04a16fb 6465
63a212ed 6466 free (filename->data);
e04a16fb 6467
63a212ed
PB
6468 /* Here we should have a unified way of retrieving an entry, to be
6469 indexed. */
6470 if (!found)
e04a16fb
AG
6471 {
6472 static int first = 1;
6473 if (first)
6474 {
781b0558 6475 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6476 java_error_count++;
6477 first = 0;
6478 }
6479 else
63a212ed
PB
6480 parse_error_context (wfl, "Package `%s' not found in import",
6481 package_name);
e04a16fb
AG
6482 current_jcf = saved_jcf;
6483 return;
6484 }
e04a16fb
AG
6485 current_jcf = saved_jcf;
6486}
6487
6488/* Possibly find a type in the import on demands specified
6489 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6490 entire list, to detected potential double definitions. */
6491
6492static int
6493find_in_imports_on_demand (class_type)
6494 tree class_type;
6495{
ab3a6dd6 6496 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6497 int seen_once = -1;
ab3a6dd6 6498 tree cl = NULL_TREE;
e04a16fb
AG
6499
6500 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6501 {
49f48c71 6502 const char *id_name;
e04a16fb
AG
6503 obstack_grow (&temporary_obstack,
6504 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6505 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6506 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6507 obstack_grow0 (&temporary_obstack,
6508 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6509 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6510 id_name = obstack_finish (&temporary_obstack);
6511
6512 node = maybe_get_identifier (id_name);
6513 if (node && IS_A_CLASSFILE_NAME (node))
6514 {
6515 if (seen_once < 0)
6516 {
6517 cl = TREE_PURPOSE (import);
6518 seen_once = 1;
6519 node_to_use = node;
6520 }
6521 else
6522 {
6523 seen_once++;
6524 parse_error_context
6525 (import, "Type `%s' also potentially defined in package `%s'",
6526 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6527 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6528 }
6529 }
6530 }
6531
6532 if (seen_once == 1)
6533 {
6534 /* Setup lineno so that it refers to the line of the import (in
6535 case we parse a class file and encounter errors */
6536 tree decl;
6537 int saved_lineno = lineno;
6538 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6539 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6540 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6541 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6542 /* If there is no DECL set for the class or if the class isn't
6543 loaded and not seen in source yet, the load */
6544 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6545 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6546 load_class (node_to_use, 0);
6547 lineno = saved_lineno;
6548 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6549 }
6550 else
6551 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6552}
6553
5e942c50
APB
6554static tree
6555resolve_package (pkg, next)
6556 tree pkg, *next;
6557{
c2952b01 6558 tree current, acc;
5e942c50 6559 tree type_name = NULL_TREE;
49f48c71 6560 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6561
6562 /* The trick is to determine when the package name stops and were
6563 the name of something contained in the package starts. Then we
6564 return a fully qualified name of what we want to get. */
6565
6566 /* Do a quick search on well known package names */
6567 if (!strncmp (name, "java.lang.reflect", 17))
6568 {
6569 *next =
6570 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6571 type_name = lookup_package_type (name, 17);
6572 }
6573 else if (!strncmp (name, "java.lang", 9))
6574 {
6575 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6576 type_name = lookup_package_type (name, 9);
6577 }
5e942c50 6578
2c56429a
APB
6579 /* If we found something here, return */
6580 if (type_name)
6581 return type_name;
6582
6583 *next = EXPR_WFL_QUALIFICATION (pkg);
6584
6585 /* Try the current package. */
6586 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6587 IDENTIFIER_LENGTH (ctxp->package)))
6588 {
6589 type_name =
6590 lookup_package_type_and_set_next (name,
6591 IDENTIFIER_LENGTH (ctxp->package),
6592 next );
6593 if (type_name)
6594 return type_name;
6595 }
6596
6597 /* Search in imported package */
6598 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6599 {
6600 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6601 int len = IDENTIFIER_LENGTH (current_pkg_name);
6602 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6603 {
6604 tree left, dummy;
6605
6606 breakdown_qualified (&left, &dummy, current_pkg_name);
6607 len = IDENTIFIER_LENGTH (left);
6608 type_name = lookup_package_type_and_set_next (name, len, next);
6609 if (type_name)
6610 break;
6611 }
6612 }
6613
c2952b01
APB
6614 /* Try to progressively construct a type name */
6615 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6616 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6617 current; current = TREE_CHAIN (current))
6618 {
6619 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6620 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6621 {
6622 type_name = acc;
dde1da72 6623 *next = TREE_CHAIN (current);
c2952b01
APB
6624 break;
6625 }
6626 }
2c56429a
APB
6627 return type_name;
6628}
6629
6630static tree
6631lookup_package_type_and_set_next (name, len, next)
49f48c71 6632 const char *name;
2c56429a
APB
6633 int len;
6634 tree *next;
6635{
49f48c71 6636 const char *ptr;
2c56429a
APB
6637 tree type_name = lookup_package_type (name, len);
6638
6639 if (!type_name)
6640 return NULL;
6641
6642 ptr = IDENTIFIER_POINTER (type_name);
6643 while (ptr && (ptr = strchr (ptr, '.')))
6644 {
6645 *next = TREE_CHAIN (*next);
6646 ptr++;
6647 }
5e942c50
APB
6648 return type_name;
6649}
6650
6651static tree
6652lookup_package_type (name, from)
49f48c71 6653 const char *name;
5e942c50
APB
6654 int from;
6655{
6656 char subname [128];
49f48c71 6657 const char *sub = &name[from+1];
5e942c50
APB
6658 while (*sub != '.' && *sub)
6659 sub++;
6660 strncpy (subname, name, sub-name);
6661 subname [sub-name] = '\0';
6662 return get_identifier (subname);
6663}
6664
e04a16fb
AG
6665/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6666 access violations were found, 1 otherwise. */
6667
6668static int
6669check_pkg_class_access (class_name, cl)
6670 tree class_name;
6671 tree cl;
6672{
6673 tree type;
e04a16fb
AG
6674
6675 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6676 return 0;
6677
6678 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6679 return 0;
6680
6681 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6682 {
e28cd97b
APB
6683 /* Access to a private class within the same package is
6684 allowed. */
6685 tree l, r;
6686 breakdown_qualified (&l, &r, class_name);
6687 if (l == ctxp->package)
6688 return 0;
6689
e04a16fb 6690 parse_error_context
781b0558 6691 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6692 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6693 IDENTIFIER_POINTER (class_name));
6694 return 1;
6695 }
6696 return 0;
6697}
6698
6699/* Local variable declaration. */
6700
6701static void
6702declare_local_variables (modifier, type, vlist)
6703 int modifier;
6704 tree type;
6705 tree vlist;
6706{
c583dd46
APB
6707 tree decl, current, saved_type;
6708 tree type_wfl = NULL_TREE;
e04a16fb 6709 int must_chain = 0;
c2952b01 6710 int final_p = 0;
e04a16fb 6711
2aa11e97 6712 /* Push a new block if statements were seen between the last time we
e04a16fb 6713 pushed a block and now. Keep a cound of block to close */
f099f336 6714 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6715 {
f099f336 6716 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6717 tree b = enter_block ();
f099f336 6718 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6719 }
6720
6721 if (modifier)
6722 {
6723 int i;
6724 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6725 if (modifier == ACC_FINAL)
c2952b01 6726 final_p = 1;
c877974e
APB
6727 else
6728 {
6729 parse_error_context
6730 (ctxp->modifier_ctx [i],
6731 "Only `final' is allowed as a local variables modifier");
6732 return;
6733 }
e04a16fb
AG
6734 }
6735
c583dd46
APB
6736 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6737 hold the TYPE value if a new incomplete has to be created (as
6738 opposed to being found already existing and reused). */
6739 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6740
6741 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6742 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6743
6744 /* Go through all the declared variables */
6745 for (current = vlist, saved_type = type; current;
6746 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6747 {
c877974e 6748 tree other, real_type;
e04a16fb
AG
6749 tree wfl = TREE_PURPOSE (current);
6750 tree name = EXPR_WFL_NODE (wfl);
6751 tree init = TREE_VALUE (current);
e04a16fb 6752
c583dd46
APB
6753 /* Process NAME, as it may specify extra dimension(s) for it */
6754 type = build_array_from_name (type, type_wfl, name, &name);
6755
6756 /* Variable redefinition check */
6757 if ((other = lookup_name_in_blocks (name)))
6758 {
6759 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6760 DECL_SOURCE_LINE (other));
6761 continue;
6762 }
6763
6764 /* Type adjustment. We may have just readjusted TYPE because
6765 the variable specified more dimensions. Make sure we have
6766 a reference if we can and don't have one already. */
1886c9d8 6767 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6768
6769 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6770 /* Never layout this decl. This will be done when its scope
6771 will be entered */
c877974e 6772 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6773 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6774 BLOCK_CHAIN_DECL (decl);
6775
d4370213
APB
6776 /* If doing xreferencing, replace the line number with the WFL
6777 compound value */
6778 if (flag_emit_xref)
6779 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6780
e04a16fb
AG
6781 /* Don't try to use an INIT statement when an error was found */
6782 if (init && java_error_count)
6783 init = NULL_TREE;
c583dd46
APB
6784
6785 /* Add the initialization function to the current function's code */
6786 if (init)
e04a16fb 6787 {
c583dd46
APB
6788 /* Name might have been readjusted */
6789 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6790 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6791 java_method_add_stmt (current_function_decl,
6792 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6793 init));
6794 }
6795
6796 /* Setup dependency the type of the decl */
6797 if (must_chain)
6798 {
6799 jdep *dep;
6800 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6801 dep = CLASSD_LAST (ctxp->classd_list);
6802 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6803 }
6804 }
6805 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6806}
6807
6808/* Called during parsing. Build decls from argument list. */
6809
6810static void
6811source_start_java_method (fndecl)
6812 tree fndecl;
6813{
6814 tree tem;
6815 tree parm_decl;
6816 int i;
c2952b01
APB
6817#if 0
6818 int flag_inner = DECL_CONSTRUCTOR_P (fndecl)
6819 && (INNER_CLASS_TYPE_P (DECL_CONTEXT (fndecl)) ? 1 : 0);
6820#endif
e04a16fb 6821
79d13333
APB
6822 if (!fndecl)
6823 return;
6824
e04a16fb
AG
6825 current_function_decl = fndecl;
6826
6827 /* New scope for the function */
6828 enter_block ();
6829 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 6830 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
6831 {
6832 tree type = TREE_VALUE (tem);
6833 tree name = TREE_PURPOSE (tem);
6834
23a79c61
APB
6835 /* If type is incomplete. Create an incomplete decl and ask for
6836 the decl to be patched later */
e04a16fb
AG
6837 if (INCOMPLETE_TYPE_P (type))
6838 {
6839 jdep *jdep;
c877974e
APB
6840 tree real_type = GET_REAL_TYPE (type);
6841 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 6842 type = obtain_incomplete_type (type);
e04a16fb
AG
6843 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6844 jdep = CLASSD_LAST (ctxp->classd_list);
6845 JDEP_MISC (jdep) = name;
6846 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6847 }
6848 else
6849 parm_decl = build_decl (PARM_DECL, name, type);
6850
c2952b01
APB
6851 /* Remember if a local variable was declared final (via its
6852 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6853 if (ARG_FINAL_P (tem))
6854 LOCAL_FINAL (parm_decl) = 1;
6855
e04a16fb 6856 BLOCK_CHAIN_DECL (parm_decl);
c2952b01
APB
6857
6858#if 0
6859 /* If this is a constructor of a inner class, hide the extra
6860 this$<n> parameter */
6861 if (i == 0 && flag_inner)
6862 {
6863 tree link = TREE_CHAIN (tem);
6864 tree type = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (fndecl)));
6865
6866 type = build_pointer_type (TREE_TYPE (type));
6867 parm_decl = build_decl (PARM_DECL,
6868 build_current_thisn (current_class), type);
6869 BLOCK_CHAIN_DECL (parm_decl);
6870 /* We hide the this$<n> decl in the name field of its
6871 parameter declaration. */
6872 parm_decl = build_tree_list (DECL_NAME (parm_decl), type);
6873 TREE_CHAIN (tem) = parm_decl;
6874 TREE_CHAIN (parm_decl) = link;
6875 tem = parm_decl;
6876 i++;
6877 }
6878#endif
6879
e04a16fb
AG
6880 }
6881 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6882 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
6883 nreverse (tem);
6884 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 6885 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
6886}
6887
22eed1e6
APB
6888/* Called during parsing. Creates an artificial method declaration. */
6889
6890static tree
6891create_artificial_method (class, flags, type, name, args)
6892 tree class;
6893 int flags;
6894 tree type, name, args;
6895{
22eed1e6
APB
6896 tree mdecl;
6897
c2952b01 6898 java_parser_context_save_global ();
22eed1e6
APB
6899 lineno = 0;
6900 mdecl = make_node (FUNCTION_TYPE);
6901 TREE_TYPE (mdecl) = type;
6902 TYPE_ARG_TYPES (mdecl) = args;
6903 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 6904 java_parser_context_restore_global ();
22eed1e6
APB
6905 DECL_ARTIFICIAL (mdecl) = 1;
6906 return mdecl;
6907}
6908
6909/* Starts the body if an artifical method. */
6910
6911static void
6912start_artificial_method_body (mdecl)
6913 tree mdecl;
6914{
6915 DECL_SOURCE_LINE (mdecl) = 1;
6916 DECL_SOURCE_LINE_MERGE (mdecl, 1);
6917 source_start_java_method (mdecl);
6918 enter_block ();
6919}
6920
6921static void
6922end_artificial_method_body (mdecl)
6923 tree mdecl;
6924{
6925 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
6926 exit_block ();
6927}
6928
e04a16fb
AG
6929/* Called during expansion. Push decls formerly built from argument
6930 list so they're usable during expansion. */
6931
6932static void
6933expand_start_java_method (fndecl)
6934 tree fndecl;
6935{
6936 tree tem, *ptr;
e04a16fb 6937
e04a16fb
AG
6938 current_function_decl = fndecl;
6939
c2952b01
APB
6940 if (! quiet_flag)
6941 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 6942 announce_function (fndecl);
c2952b01
APB
6943 if (! quiet_flag)
6944 fprintf (stderr, "]");
6945
6946 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
6947 ptr = &DECL_ARGUMENTS (fndecl);
6948 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6949 while (tem)
6950 {
6951 tree next = TREE_CHAIN (tem);
b67d701b 6952 tree type = TREE_TYPE (tem);
e438e1b7
JJ
6953 if (PROMOTE_PROTOTYPES
6954 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
6955 && INTEGRAL_TYPE_P (type))
6956 type = integer_type_node;
b67d701b 6957 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
6958 layout_decl (tem, 0);
6959 pushdecl (tem);
e04a16fb
AG
6960 *ptr = tem;
6961 ptr = &TREE_CHAIN (tem);
6962 tem = next;
6963 }
6964 *ptr = NULL_TREE;
6965 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
6966 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
6967}
6968
6969/* Terminate a function and expand its body. */
6970
6971static void
6972source_end_java_method ()
6973{
6974 tree fndecl = current_function_decl;
138657ec 6975 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 6976
79d13333
APB
6977 if (!fndecl)
6978 return;
6979
e04a16fb
AG
6980 java_parser_context_save_global ();
6981 lineno = ctxp->last_ccb_indent1;
6982
b67d701b
PB
6983 /* Set EH language codes */
6984 java_set_exception_lang_code ();
6985
5423609c
APB
6986 /* Turn function bodies with only a NOP expr null, so they don't get
6987 generated at all and we won't get warnings when using the -W
6988 -Wall flags. */
6989 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
6990 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
6991
e04a16fb
AG
6992 /* Generate function's code */
6993 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
6994 && ! flag_emit_class_files
6995 && ! flag_emit_xref)
e04a16fb
AG
6996 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
6997
6998 /* pop out of its parameters */
6999 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7000 poplevel (1, 0, 1);
7001 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7002
7003 /* Generate rtl for function exit. */
e8fc7396 7004 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7005 {
7006 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7007 /* Emit catch-finally clauses */
7008 emit_handlers ();
e04a16fb
AG
7009 expand_function_end (input_filename, lineno, 0);
7010
138657ec
AH
7011 /* FIXME: If the current method contains any exception handlers,
7012 force asynchronous_exceptions: this is necessary because signal
7013 handlers in libjava may throw exceptions. This is far from being
7014 a perfect solution, but it's better than doing nothing at all.*/
7015 if (catch_clauses)
7016 asynchronous_exceptions = 1;
7017
e04a16fb
AG
7018 /* Run the optimizers and output assembler code for this function. */
7019 rest_of_compilation (fndecl);
7020 }
7021
7022 current_function_decl = NULL_TREE;
8226320b 7023 permanent_allocation (1);
e04a16fb 7024 java_parser_context_restore_global ();
138657ec 7025 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7026}
7027
7028/* Record EXPR in the current function block. Complements compound
7029 expression second operand if necessary. */
7030
7031tree
7032java_method_add_stmt (fndecl, expr)
7033 tree fndecl, expr;
7034{
b771925e
APB
7035 if (!GET_CURRENT_BLOCK (fndecl))
7036 return NULL_TREE;
f099f336 7037 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7038}
e04a16fb 7039
b67d701b
PB
7040static tree
7041add_stmt_to_block (b, type, stmt)
7042 tree b, type, stmt;
7043{
7044 tree body = BLOCK_EXPR_BODY (b), c;
7045
e04a16fb
AG
7046 if (java_error_count)
7047 return body;
b67d701b
PB
7048
7049 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7050 return body;
7051
b67d701b
PB
7052 BLOCK_EXPR_BODY (b) = c;
7053 TREE_SIDE_EFFECTS (c) = 1;
7054 return c;
e04a16fb
AG
7055}
7056
7057/* Add STMT to EXISTING if possible, otherwise create a new
7058 COMPOUND_EXPR and add STMT to it. */
7059
7060static tree
7061add_stmt_to_compound (existing, type, stmt)
7062 tree existing, type, stmt;
7063{
15fdcfe9
PB
7064 if (existing)
7065 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7066 else
15fdcfe9 7067 return stmt;
e04a16fb
AG
7068}
7069
7070/* Hold THIS for the scope of the current public method decl. */
7071static tree current_this;
7072
1886c9d8
APB
7073void java_layout_seen_class_methods ()
7074{
7075 tree previous_list = all_class_list;
7076 tree end = NULL_TREE;
7077 tree current;
7078
7079 while (1)
7080 {
7081 for (current = previous_list;
7082 current != end; current = TREE_CHAIN (current))
7083 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7084
7085 if (previous_list != all_class_list)
7086 {
7087 end = previous_list;
7088 previous_list = all_class_list;
7089 }
7090 else
7091 break;
7092 }
7093}
7094
e04a16fb 7095void
c2952b01 7096java_reorder_fields ()
e04a16fb 7097{
c2952b01 7098 static tree stop_reordering = NULL_TREE;
23a79c61 7099
c2952b01 7100 tree current;
5e942c50 7101 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7102 {
5e942c50 7103 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7104
c2952b01
APB
7105 if (current_class == stop_reordering)
7106 break;
7107
c877974e
APB
7108 /* Reverse the fields, but leave the dummy field in front.
7109 Fields are already ordered for Object and Class */
7110 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7111 && current_class != class_type_node)
7112 {
23a79c61
APB
7113 /* If the dummy field is there, reverse the right fields and
7114 just layout the type for proper fields offset */
c877974e
APB
7115 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7116 {
7117 tree fields = TYPE_FIELDS (current_class);
7118 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7119 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7120 }
23a79c61
APB
7121 /* We don't have a dummy field, we need to layout the class,
7122 after having reversed the fields */
c877974e
APB
7123 else
7124 {
7125 TYPE_FIELDS (current_class) =
7126 nreverse (TYPE_FIELDS (current_class));
7127 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7128 }
7129 }
c2952b01
APB
7130 }
7131 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7132}
7133
7134/* Layout the methods of all classes loaded in one way on an
7135 other. Check methods of source parsed classes. Then reorder the
7136 fields and layout the classes or the type of all source parsed
7137 classes */
7138
7139void
7140java_layout_classes ()
7141{
7142 tree current;
7143 int save_error_count = java_error_count;
7144
7145 /* Layout the methods of all classes seen so far */
7146 java_layout_seen_class_methods ();
7147 java_parse_abort_on_error ();
7148 all_class_list = NULL_TREE;
7149
7150 /* Then check the methods of all parsed classes */
7151 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7152 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7153 CHECK_METHODS (TREE_VALUE (current));
7154 java_parse_abort_on_error ();
7155
7156 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7157 {
7158 current_class = TREE_TYPE (TREE_VALUE (current));
7159 layout_class (current_class);
5e942c50 7160
c877974e
APB
7161 /* From now on, the class is considered completely loaded */
7162 CLASS_LOADED_P (current_class) = 1;
7163
5e942c50
APB
7164 /* Error reported by the caller */
7165 if (java_error_count)
7166 return;
e04a16fb 7167 }
23a79c61
APB
7168
7169 /* We might have reloaded classes durign the process of laying out
7170 classes for code generation. We must layout the methods of those
7171 late additions, as constructor checks might use them */
1886c9d8 7172 java_layout_seen_class_methods ();
23a79c61 7173 java_parse_abort_on_error ();
e04a16fb
AG
7174}
7175
c2952b01
APB
7176/* Expand methods in the current set of classes rememebered for
7177 generation. */
e04a16fb 7178
49f48c71 7179static void
c2952b01 7180java_complete_expand_classes ()
e04a16fb
AG
7181{
7182 tree current;
ce6e9147
APB
7183
7184 do_not_fold = flag_emit_xref;
c2952b01 7185
e04a16fb 7186 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7187 if (!INNER_CLASS_DECL_P (current))
7188 java_complete_expand_class (current);
7189}
e04a16fb 7190
c2952b01
APB
7191/* Expand the methods found in OUTER, starting first by OUTER's inner
7192 classes, if any. */
e04a16fb 7193
c2952b01
APB
7194static void
7195java_complete_expand_class (outer)
7196 tree outer;
7197{
7198 tree inner_list;
e04a16fb 7199
c2952b01 7200 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7201
c2952b01
APB
7202 /* We need to go after all inner classes and start expanding them,
7203 starting with most nested ones. We have to do that because nested
7204 classes might add functions to outer classes */
e04a16fb 7205
c2952b01
APB
7206 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7207 inner_list; inner_list = TREE_CHAIN (inner_list))
7208 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7209
c2952b01
APB
7210 java_complete_expand_methods (outer);
7211 set_nested_class_simple_name_value (outer, 0); /* Reset */
7212}
7213
7214/* Expand methods registered in CLASS_DECL. The general idea is that
7215 we expand regular methods first. This allows us get an estimate on
7216 how outer context local alias fields are really used so we can add
7217 to the constructor just enough code to initialize them properly (it
7218 also lets us generate $finit$ correctly.) Then we expand the
7219 constructors and then <clinit>. */
7220
7221static void
7222java_complete_expand_methods (class_decl)
7223 tree class_decl;
7224{
7225 tree clinit, finit, decl, first_decl;
7226
7227 current_class = TREE_TYPE (class_decl);
7228
7229 /* Initialize a new constant pool */
7230 init_outgoing_cpool ();
7231
7232 /* Pre-expand <clinit> to figure whether we really need it or
7233 not. If we do need it, we pre-expand the static fields so they're
7234 ready to be used somewhere else. <clinit> will be fully expanded
7235 after we processed the constructors. */
7236 first_decl = TYPE_METHODS (current_class);
7237 clinit = maybe_generate_pre_expand_clinit (current_class);
7238
7239 /* Then generate $finit$ (if we need to) because constructor will
7240 try to use it.*/
7241 if (TYPE_FINIT_STMT_LIST (current_class))
7242 {
7243 finit = generate_finit (current_class);
7244 java_complete_expand_method (finit);
7245 }
7246
7247 /* Now do the constructors */
7248 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7249 {
7250 int no_body;
7251
7252 if (!DECL_CONSTRUCTOR_P (decl))
7253 continue;
7254
7255 no_body = !DECL_FUNCTION_BODY (decl);
7256 /* Don't generate debug info on line zero when expanding a
7257 generated constructor. */
7258 if (no_body)
7259 restore_line_number_status (1);
7260
7261 java_complete_expand_method (decl);
7262
7263 if (no_body)
7264 restore_line_number_status (0);
7265 }
7266
7267 /* First, do the ordinary methods. */
7268 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7269 {
7270 /* Skip abstract or native methods */
7271 if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl)
7272 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7273 continue;
7274 java_complete_expand_method (decl);
7275 }
7276
7277 /* If there is indeed a <clinit>, fully expand it now */
7278 if (clinit)
7279 {
7280 /* Prevent the use of `this' inside <clinit> */
7281 ctxp->explicit_constructor_p = 1;
7282 java_complete_expand_method (clinit);
7283 ctxp->explicit_constructor_p = 0;
e04a16fb 7284 }
c2952b01 7285
165f37bc
APB
7286 /* We might have generated a class$ that we now want to expand */
7287 if (TYPE_DOT_CLASS (current_class))
7288 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7289
c2952b01
APB
7290 /* Now verify constructor circularity (stop after the first one we
7291 prove wrong.) */
7292 if (!CLASS_INTERFACE (class_decl))
7293 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7294 if (DECL_CONSTRUCTOR_P (decl)
7295 && verify_constructor_circularity (decl, decl))
7296 break;
7297
7298 /* Save the constant pool. We'll need to restore it later. */
7299 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7300}
7301
b9f7e36c
APB
7302/* Hold a list of catch clauses list. The first element of this list is
7303 the list of the catch clauses of the currently analysed try block. */
7304static tree currently_caught_type_list;
7305
c2952b01
APB
7306/* Attempt to create <clinit>. Pre-expand static fields so they can be
7307 safely used in some other methods/constructors. */
e920ebc9 7308
c2952b01
APB
7309static tree
7310maybe_generate_pre_expand_clinit (class_type)
7311 tree class_type;
e920ebc9 7312{
c2952b01
APB
7313 tree current, mdecl;
7314
7315 if (!TYPE_CLINIT_STMT_LIST (class_type))
7316 return NULL_TREE;
e920ebc9 7317
c2952b01
APB
7318 /* Go through all static fields and pre expand them */
7319 for (current = TYPE_FIELDS (class_type); current;
7320 current = TREE_CHAIN (current))
7321 if (FIELD_STATIC (current))
7322 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7323
7324 /* Then build the <clinit> method */
7325 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7326 clinit_identifier_node, end_params_node);
7327 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7328 mdecl, NULL_TREE);
7329 start_artificial_method_body (mdecl);
7330
7331 /* We process the list of assignment we produced as the result of
7332 the declaration of initialized static field and add them as
7333 statement to the <clinit> method. */
7334 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7335 current = TREE_CHAIN (current))
e920ebc9 7336 {
c2952b01
APB
7337 /* We build the assignment expression that will initialize the
7338 field to its value. There are strict rules on static
7339 initializers (8.5). FIXME */
7340 tree stmt = build_debugable_stmt (EXPR_WFL_LINECOL (current), current);
7341 java_method_add_stmt (mdecl, stmt);
7342 }
e920ebc9 7343
c2952b01
APB
7344 end_artificial_method_body (mdecl);
7345
7346 /* Now we want to place <clinit> as the last method for interface so
7347 that it doesn't interfere with the dispatch table based
7348 lookup. */
7349 if (CLASS_INTERFACE (TYPE_NAME (class_type))
7350 && TREE_CHAIN (TYPE_METHODS (class_type)))
7351 {
7352 tree current =
7353 TYPE_METHODS (class_type) = TREE_CHAIN (TYPE_METHODS (class_type));
7354
7355 while (TREE_CHAIN (current))
7356 current = TREE_CHAIN (current);
7357 TREE_CHAIN (current) = mdecl;
7358 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7359 }
c2952b01
APB
7360
7361 return mdecl;
e920ebc9
APB
7362}
7363
e04a16fb
AG
7364/* Complete and expand a method. */
7365
7366static void
7367java_complete_expand_method (mdecl)
7368 tree mdecl;
7369{
c2952b01 7370 current_function_decl = mdecl;
22eed1e6
APB
7371 /* Fix constructors before expanding them */
7372 if (DECL_CONSTRUCTOR_P (mdecl))
7373 fix_constructors (mdecl);
e04a16fb 7374
22eed1e6 7375 /* Expand functions that have a body */
e04a16fb
AG
7376 if (DECL_FUNCTION_BODY (mdecl))
7377 {
9bbc7d9f
PB
7378 tree fbody = DECL_FUNCTION_BODY (mdecl);
7379 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7380 tree exception_copy = NULL_TREE;
e04a16fb 7381 expand_start_java_method (mdecl);
939d7216 7382 build_result_decl (mdecl);
e04a16fb
AG
7383
7384 current_this
7385 = (!METHOD_STATIC (mdecl) ?
7386 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7387
ce6e9147
APB
7388 /* Purge the `throws' list of unchecked exceptions. If we're
7389 doing xref, save a copy of the list and re-install it
7390 later. */
7391 if (flag_emit_xref)
7392 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7393
b9f7e36c
APB
7394 purge_unchecked_exceptions (mdecl);
7395
7396 /* Install exceptions thrown with `throws' */
7397 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7398
9bbc7d9f 7399 if (block_body != NULL_TREE)
bc3ca41b
PB
7400 {
7401 block_body = java_complete_tree (block_body);
c2952b01 7402
ce6e9147
APB
7403 if (!flag_emit_xref)
7404 check_for_initialization (block_body);
f099f336 7405 ctxp->explicit_constructor_p = 0;
bc3ca41b 7406 }
9bbc7d9f 7407 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7408
c2952b01
APB
7409 /* If we saw a return but couldn't evaluate it properly, we'll
7410 have an error_mark_node here. */
7411 if (block_body != error_mark_node
7412 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7413 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7414 && !flag_emit_xref)
82371d41 7415 missing_return_error (current_function_decl);
7525cc04 7416
939d7216
PB
7417 complete_start_java_method (mdecl);
7418
e04a16fb
AG
7419 /* Don't go any further if we've found error(s) during the
7420 expansion */
7421 if (!java_error_count)
7422 source_end_java_method ();
22eed1e6
APB
7423 else
7424 {
7425 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7426 poplevel (1, 0, 1);
7427 }
b9f7e36c
APB
7428
7429 /* Pop the exceptions and sanity check */
7430 POP_EXCEPTIONS();
7431 if (currently_caught_type_list)
7432 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7433
7434 if (flag_emit_xref)
7435 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7436 }
7437}
7438
c2952b01
APB
7439\f
7440
7441/* This section of the code deals with accessing enclosing context
7442 fields either directly by using the relevant access to this$<n> or
7443 by invoking an access method crafted for that purpose. */
7444
7445/* Build the necessary access from an inner class to an outer
7446 class. This routine could be optimized to cache previous result
7447 (decl, current_class and returned access). When an access method
7448 needs to be generated, it always takes the form of a read. It might
7449 be later turned into a write by calling outer_field_access_fix. */
7450
7451static tree
7452build_outer_field_access (id, decl)
7453 tree id, decl;
7454{
7455 tree access = NULL_TREE;
7456 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7457
7458 /* If decl's class is the direct outer class of the current_class,
7459 build the access as `this$<n>.<field>'. Not that we will break
7460 the `private' barrier if we're not emitting bytecodes. */
7461 if (ctx == DECL_CONTEXT (decl)
7462 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7463 {
7464 tree thisn = build_current_thisn (current_class);
7465 access = make_qualified_primary (build_wfl_node (thisn),
7466 id, EXPR_WFL_LINECOL (id));
7467 }
7468 /* Otherwise, generate access methods to outer this and access the
7469 field (either using an access method or by direct access.) */
7470 else
7471 {
7472 int lc = EXPR_WFL_LINECOL (id);
7473
7474 /* Now we chain the required number of calls to the access$0 to
7475 get a hold to the enclosing instance we need, and the we
7476 build the field access. */
7477 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7478
7479 /* If the field is private and we're generating bytecode, then
7480 we generate an access method */
7481 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7482 {
7483 tree name = build_outer_field_access_methods (decl);
7484 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7485 name, access, NULL_TREE);
7486 }
7487 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7488 Once again we break the `private' access rule from a foreign
7489 class. */
7490 else
7491 access = make_qualified_primary (access, id, lc);
7492 }
7493 return resolve_expression_name (access, NULL);
7494}
7495
7496/* Return a non zero value if NODE describes an outer field inner
7497 access. */
7498
7499static int
7500outer_field_access_p (type, decl)
7501 tree type, decl;
7502{
7503 if (!INNER_CLASS_TYPE_P (type)
7504 || TREE_CODE (decl) != FIELD_DECL
7505 || DECL_CONTEXT (decl) == type)
7506 return 0;
7507
7508 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7509 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7510 {
7511 if (type == DECL_CONTEXT (decl))
7512 return 1;
7513 if (!DECL_CONTEXT (TYPE_NAME (type)))
7514 break;
7515 }
7516
7517 return 0;
7518}
7519
7520/* Return a non zero value if NODE represents an outer field inner
7521 access that was been already expanded. As a side effect, it returns
7522 the name of the field being accessed and the argument passed to the
7523 access function, suitable for a regeneration of the access method
7524 call if necessary. */
7525
7526static int
7527outer_field_expanded_access_p (node, name, arg_type, arg)
7528 tree node, *name, *arg_type, *arg;
7529{
7530 int identified = 0;
7531
7532 if (TREE_CODE (node) != CALL_EXPR)
7533 return 0;
7534
7535 /* Well, gcj generates slightly different tree nodes when compiling
7536 to native or bytecodes. It's the case for function calls. */
7537
7538 if (flag_emit_class_files
7539 && TREE_CODE (node) == CALL_EXPR
7540 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7541 identified = 1;
7542 else if (!flag_emit_class_files)
7543 {
7544 node = TREE_OPERAND (node, 0);
7545
7546 if (node && TREE_OPERAND (node, 0)
7547 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7548 {
7549 node = TREE_OPERAND (node, 0);
7550 if (TREE_OPERAND (node, 0)
7551 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7552 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7553 (DECL_NAME (TREE_OPERAND (node, 0)))))
7554 identified = 1;
7555 }
7556 }
7557
7558 if (identified && name && arg_type && arg)
7559 {
7560 tree argument = TREE_OPERAND (node, 1);
7561 *name = DECL_NAME (TREE_OPERAND (node, 0));
7562 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7563 *arg = TREE_VALUE (argument);
7564 }
7565 return identified;
7566}
7567
7568/* Detect in NODE an outer field read access from an inner class and
7569 transform it into a write with RHS as an argument. This function is
7570 called from the java_complete_lhs when an assignment to a LHS can
7571 be identified. */
7572
7573static tree
7574outer_field_access_fix (wfl, node, rhs)
7575 tree wfl, node, rhs;
7576{
7577 tree name, arg_type, arg;
7578
7579 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7580 {
7581 /* At any rate, check whether we're trying to assign a value to
7582 a final. */
7583 tree accessed = (JDECL_P (node) ? node :
7584 (TREE_CODE (node) == COMPONENT_REF ?
7585 TREE_OPERAND (node, 1) : node));
7586 if (check_final_assignment (accessed, wfl))
7587 return error_mark_node;
7588
7589 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7590 arg_type, name, arg, rhs);
7591 return java_complete_tree (node);
7592 }
7593 return NULL_TREE;
7594}
7595
7596/* Construct the expression that calls an access method:
7597 <type>.access$<n>(<arg1> [, <arg2>]);
7598
7599 ARG2 can be NULL and will be omitted in that case. It will denote a
7600 read access. */
7601
7602static tree
7603build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7604 int lc;
7605 tree type, access_method_name, arg1, arg2;
7606{
7607 tree args, cn, access;
7608
7609 args = arg1 ? arg1 :
7610 build_wfl_node (build_current_thisn (current_class));
7611 args = build_tree_list (NULL_TREE, args);
7612
7613 if (arg2)
7614 args = tree_cons (NULL_TREE, arg2, args);
7615
7616 access = build_method_invocation (build_wfl_node (access_method_name), args);
7617 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7618 return make_qualified_primary (cn, access, lc);
7619}
7620
7621static tree
7622build_new_access_id ()
7623{
7624 static int access_n_counter = 1;
7625 char buffer [128];
7626
7627 sprintf (buffer, "access$%d", access_n_counter++);
7628 return get_identifier (buffer);
7629}
7630
7631/* Create the static access functions for the outer field DECL. We define a
7632 read:
7633 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7634 return inst$.field;
7635 }
7636 and a write access:
7637 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7638 TREE_TYPE (<field>) value$) {
7639 return inst$.field = value$;
7640 }
7641 We should have a usage flags on the DECL so we can lazily turn the ones
7642 we're using for code generation. FIXME.
7643*/
7644
7645static tree
7646build_outer_field_access_methods (decl)
7647 tree decl;
7648{
7649 tree id, args, stmt, mdecl;
7650
7651 /* Check point, to be removed. FIXME */
7652 if (FIELD_INNER_ACCESS (decl)
7653 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7654 abort ();
7655
7656 if (FIELD_INNER_ACCESS (decl))
7657 return FIELD_INNER_ACCESS (decl);
7658
7659 push_obstacks (&permanent_obstack, &permanent_obstack);
7660
7661 /* Create the identifier and a function named after it. */
7662 id = build_new_access_id ();
7663
7664 /* The identifier is marked as bearing the name of a generated write
7665 access function for outer field accessed from inner classes. */
7666 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7667
7668 /* Create the read access */
7669 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7670 TREE_CHAIN (args) = end_params_node;
7671 stmt = make_qualified_primary (build_wfl_node (inst_id),
7672 build_wfl_node (DECL_NAME (decl)), 0);
7673 stmt = build_return (0, stmt);
7674 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7675 TREE_TYPE (decl), id, args, stmt);
7676 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7677
7678 /* Create the write access method */
7679 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7680 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7681 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7682 stmt = make_qualified_primary (build_wfl_node (inst_id),
7683 build_wfl_node (DECL_NAME (decl)), 0);
7684 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7685 build_wfl_node (wpv_id)));
7686
7687 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7688 TREE_TYPE (decl), id, args, stmt);
7689 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7690 pop_obstacks ();
7691
7692 /* Return the access name */
7693 return FIELD_INNER_ACCESS (decl) = id;
7694}
7695
7696/* Build an field access method NAME. */
7697
7698static tree
7699build_outer_field_access_method (class, type, name, args, body)
7700 tree class, type, name, args, body;
7701{
7702 tree saved_current_function_decl, mdecl;
7703
7704 /* Create the method */
7705 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7706 fix_method_argument_names (args, mdecl);
7707 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7708
7709 /* Attach the method body. */
7710 saved_current_function_decl = current_function_decl;
7711 start_artificial_method_body (mdecl);
7712 java_method_add_stmt (mdecl, body);
7713 end_artificial_method_body (mdecl);
7714 current_function_decl = saved_current_function_decl;
7715
7716 return mdecl;
7717}
7718
7719\f
7720/* This section deals with building access function necessary for
7721 certain kinds of method invocation from inner classes. */
7722
7723static tree
7724build_outer_method_access_method (decl)
7725 tree decl;
7726{
7727 tree saved_current_function_decl, mdecl;
7728 tree args = NULL_TREE, call_args = NULL_TREE;
7729 tree carg, id, body, class;
7730 char buffer [80];
7731 int parm_id_count = 0;
7732
7733 /* Test this abort with an access to a private field */
7734 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7735 abort ();
7736
7737 /* Check the cache first */
7738 if (DECL_FUNCTION_INNER_ACCESS (decl))
7739 return DECL_FUNCTION_INNER_ACCESS (decl);
7740
7741 class = DECL_CONTEXT (decl);
7742
7743 /* Obtain an access identifier and mark it */
7744 id = build_new_access_id ();
7745 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7746
7747 push_obstacks (&permanent_obstack, &permanent_obstack);
7748
7749 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7750 /* Create the arguments, as much as the original */
7751 for (; carg && carg != end_params_node;
7752 carg = TREE_CHAIN (carg))
7753 {
7754 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7755 args = chainon (args, build_tree_list (get_identifier (buffer),
7756 TREE_VALUE (carg)));
7757 }
7758 args = chainon (args, end_params_node);
7759
7760 /* Create the method */
7761 mdecl = create_artificial_method (class, ACC_STATIC,
7762 TREE_TYPE (TREE_TYPE (decl)), id, args);
7763 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7764 /* There is a potential bug here. We should be able to use
7765 fix_method_argument_names, but then arg names get mixed up and
7766 eventually a constructor will have its this$0 altered and the
7767 outer context won't be assignment properly. The test case is
7768 stub.java FIXME */
7769 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7770
7771 /* Attach the method body. */
7772 saved_current_function_decl = current_function_decl;
7773 start_artificial_method_body (mdecl);
7774
7775 /* The actual method invocation uses the same args. When invoking a
7776 static methods that way, we don't want to skip the first
7777 argument. */
7778 carg = args;
7779 if (!METHOD_STATIC (decl))
7780 carg = TREE_CHAIN (carg);
7781 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7782 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7783 call_args);
7784
7785 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7786 call_args);
7787 if (!METHOD_STATIC (decl))
7788 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
7789 body, 0);
7790 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
7791 body = build_return (0, body);
7792 java_method_add_stmt (mdecl,body);
7793 end_artificial_method_body (mdecl);
7794 current_function_decl = saved_current_function_decl;
7795 pop_obstacks ();
7796
7797 /* Back tag the access function so it know what it accesses */
7798 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
7799
7800 /* Tag the current method so it knows it has an access generated */
7801 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
7802}
7803
7804\f
7805/* This section of the code deals with building expressions to access
7806 the enclosing instance of an inner class. The enclosing instance is
7807 kept in a generated field called this$<n>, with <n> being the
7808 inner class nesting level (starting from 0.) */
7809
7810/* Build an access to a given this$<n>, possibly by chaining access
7811 call to others. Access methods to this$<n> are build on the fly if
7812 necessary */
7813
7814static tree
7815build_access_to_thisn (from, to, lc)
7816 tree from, to;
7817 int lc;
7818{
7819 tree access = NULL_TREE;
7820
7821 while (from != to)
7822 {
7823 tree access0_wfl, cn;
7824
7825 maybe_build_thisn_access_method (from);
7826 access0_wfl = build_wfl_node (access0_identifier_node);
7827 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
7828 EXPR_WFL_LINECOL (access0_wfl) = lc;
7829
7830 if (!access)
7831 {
7832 access = build_current_thisn (current_class);
7833 access = build_wfl_node (access);
7834 }
7835 access = build_tree_list (NULL_TREE, access);
7836 access = build_method_invocation (access0_wfl, access);
7837 access = make_qualified_primary (cn, access, lc);
7838
7839 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
7840 }
7841 return access;
7842}
7843
7844/* Build an access function to the this$<n> local to TYPE. NULL_TREE
7845 is returned if nothing needs to be generated. Otherwise, the method
7846 generated, fully walked and a method decl is returned.
7847
7848 NOTE: These generated methods should be declared in a class file
7849 attribute so that they can't be referred to directly. */
7850
7851static tree
7852maybe_build_thisn_access_method (type)
7853 tree type;
7854{
7855 tree mdecl, args, stmt, rtype;
7856 tree saved_current_function_decl;
7857
7858 /* If TYPE is a top-level class, no access method is required.
7859 If there already is such an access method, bail out. */
7860 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
7861 return NULL_TREE;
7862
7863 /* We generate the method. The method looks like:
7864 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
7865 */
7866 push_obstacks (&permanent_obstack, &permanent_obstack);
7867 args = build_tree_list (inst_id, build_pointer_type (type));
7868 TREE_CHAIN (args) = end_params_node;
7869 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
7870 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
7871 access0_identifier_node, args);
7872 fix_method_argument_names (args, mdecl);
7873 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
7874 stmt = build_current_thisn (type);
7875 stmt = make_qualified_primary (build_wfl_node (inst_id),
7876 build_wfl_node (stmt), 0);
7877 stmt = build_return (0, stmt);
7878
7879 saved_current_function_decl = current_function_decl;
7880 start_artificial_method_body (mdecl);
7881 java_method_add_stmt (mdecl, stmt);
7882 end_artificial_method_body (mdecl);
7883 current_function_decl = saved_current_function_decl;
7884 pop_obstacks ();
7885
7886 CLASS_ACCESS0_GENERATED_P (type) = 1;
7887
7888 return mdecl;
7889}
7890
7891/* Craft an correctly numbered `this$<n>'string. this$0 is used for
7892 the first level of innerclassing. this$1 for the next one, etc...
7893 This function can be invoked with TYPE to NULL, available and then
7894 has to count the parser context. */
7895
7896static tree
7897build_current_thisn (type)
7898 tree type;
7899{
7900 static int saved_i = -1;
7901 static tree saved_thisn = NULL_TREE;
7902
7903 tree decl;
7904 char buffer [80];
7905 int i = 0;
7906
7907 if (type)
7908 {
7909 static tree saved_type = NULL_TREE;
7910 static int saved_type_i = 0;
7911
7912 if (type == saved_type)
7913 i = saved_type_i;
7914 else
7915 {
7916 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
7917 decl; decl = DECL_CONTEXT (decl), i++)
7918 ;
7919
7920 saved_type = type;
7921 saved_type_i = i;
7922 }
7923 }
7924 else
7925 i = list_length (GET_CPC_LIST ())-2;
7926
7927 if (i == saved_i)
7928 return saved_thisn;
7929
7930 sprintf (buffer, "this$%d", i);
7931 saved_i = i;
7932 saved_thisn = get_identifier (buffer);
7933 return saved_thisn;
7934}
7935
7936/* Return the assignement to the hidden enclosing context `this$<n>'
7937 by the second incoming parameter to the innerclass constructor. The
7938 form used is `this.this$<n> = this$<n>;'. */
7939
7940static tree
7941build_thisn_assign ()
7942{
7943 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
7944 {
7945 tree thisn = build_current_thisn (current_class);
7946 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
7947 build_wfl_node (thisn), 0);
7948 tree rhs = build_wfl_node (thisn);
7949 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
7950 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
7951 }
7952 return NULL_TREE;
7953}
7954
7955\f
165f37bc
APB
7956/* Building the synthetic `class$' used to implement the `.class' 1.1
7957 extension for non primitive types. This method looks like:
7958
7959 static Class class$(String type) throws NoClassDefFoundError
7960 {
7961 try {return (java.lang.Class.forName (String));}
7962 catch (ClassNotFoundException e) {
7963 throw new NoClassDefFoundError(e.getMessage());}
7964 } */
7965
7966static tree
7967build_dot_class_method (class)
7968 tree class;
7969{
7970#define BWF(S) build_wfl_node (get_identifier ((S)))
7971#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
7972 tree args, tmp, saved_current_function_decl, mdecl;
7973 tree stmt, throw_stmt, catch, catch_block, try_block;
7974 tree catch_clause_param;
7975 tree class_not_found_exception, no_class_def_found_error;
7976
7977 static tree get_message_wfl, type_parm_wfl;
7978
7979 if (!get_message_wfl)
7980 {
7981 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
7982 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
7983 }
7984
7985 /* Build the arguments */
7986 args = build_tree_list (get_identifier ("type$"),
7987 build_pointer_type (string_type_node));
7988 TREE_CHAIN (args) = end_params_node;
7989
7990 /* Build the qualified name java.lang.Class.forName */
7991 tmp = MQN (MQN (MQN (BWF ("java"),
7992 BWF ("lang")), BWF ("Class")), BWF ("forName"));
7993
7994 /* For things we have to catch and throw */
7995 class_not_found_exception =
7996 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
7997 no_class_def_found_error =
7998 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
7999 load_class (class_not_found_exception, 1);
8000 load_class (no_class_def_found_error, 1);
8001
8002 /* Create the "class$" function */
8003 mdecl = create_artificial_method (class, ACC_STATIC,
8004 build_pointer_type (class_type_node),
8005 get_identifier ("class$"), args);
8006 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8007 no_class_def_found_error);
8008
8009 /* We start by building the try block. We need to build:
8010 return (java.lang.Class.forName (type)); */
8011 stmt = build_method_invocation (tmp,
8012 build_tree_list (NULL_TREE, type_parm_wfl));
8013 stmt = build_return (0, stmt);
8014 /* Put it in a block. That's the try block */
8015 try_block = build_expr_block (stmt, NULL_TREE);
8016
8017 /* Now onto the catch block. We start by building the expression
8018 throwing a new exception:
8019 throw new NoClassDefFoundError (_.getMessage); */
8020 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8021 get_message_wfl, 0);
8022 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8023
8024 /* Build new NoClassDefFoundError (_.getMessage) */
8025 throw_stmt = build_new_invocation
8026 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8027 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8028
8029 /* Build the throw, (it's too early to use BUILD_THROW) */
8030 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8031
8032 /* Build the catch block to encapsulate all this. We begin by
8033 building an decl for the catch clause parameter and link it to
8034 newly created block, the catch block. */
8035 catch_clause_param =
8036 build_decl (VAR_DECL, wpv_id,
8037 build_pointer_type (class_not_found_exception));
8038 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8039
8040 /* We initialize the variable with the exception handler. */
8041 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8042 soft_exceptioninfo_call_node);
8043 add_stmt_to_block (catch_block, NULL_TREE, catch);
8044
8045 /* We add the statement throwing the new exception */
8046 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8047
8048 /* Build a catch expression for all this */
8049 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8050
8051 /* Build the try/catch sequence */
8052 stmt = build_try_statement (0, try_block, catch_block);
8053
8054 fix_method_argument_names (args, mdecl);
8055 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8056 saved_current_function_decl = current_function_decl;
8057 start_artificial_method_body (mdecl);
8058 java_method_add_stmt (mdecl, stmt);
8059 end_artificial_method_body (mdecl);
8060 current_function_decl = saved_current_function_decl;
8061 TYPE_DOT_CLASS (class) = mdecl;
8062
8063 return mdecl;
8064}
8065
8066static tree
8067build_dot_class_method_invocation (name)
8068 tree name;
8069{
8070 tree s = make_node (STRING_CST);
8071 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8072 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8073 TREE_STRING_LENGTH (s)+1);
8074 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8075 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8076 build_tree_list (NULL_TREE, s));
8077}
8078
c2952b01
APB
8079/* This section of the code deals with constructor. */
8080
22eed1e6
APB
8081/* Craft a body for default constructor. Patch existing constructor
8082 bodies with call to super() and field initialization statements if
8083 necessary. */
8084
8085static void
8086fix_constructors (mdecl)
8087 tree mdecl;
8088{
8089 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8090 tree thisn_assign, compound = NULL_TREE;
8091 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8092
22eed1e6
APB
8093 if (!body)
8094 {
22eed1e6
APB
8095 /* It is an error for the compiler to generate a default
8096 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8097 takes no argument, or the same args for an anonymous class */
8098 if (verify_constructor_super (mdecl))
22eed1e6 8099 {
c2952b01
APB
8100 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8101 tree save = DECL_NAME (mdecl);
49f48c71 8102 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8103 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8104 parse_error_context
c2952b01
APB
8105 (lookup_cl (TYPE_NAME (class_type)),
8106 "No constructor matching `%s' found in class `%s'",
8107 lang_printable_name (mdecl, 0), n);
8108 DECL_NAME (mdecl) = save;
22eed1e6
APB
8109 }
8110
c2952b01
APB
8111 /* The constructor body must be crafted by hand. It's the
8112 constructor we defined when we realize we didn't have the
8113 CLASSNAME() constructor */
22eed1e6
APB
8114 start_artificial_method_body (mdecl);
8115
8116 /* We don't generate a super constructor invocation if we're
8117 compiling java.lang.Object. build_super_invocation takes care
8118 of that. */
e920ebc9 8119 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8120
c2952b01
APB
8121 /* Insert the instance initializer block right here, after the
8122 super invocation. */
8123 add_instance_initializer (mdecl);
8124
8125 /* Insert an assignment to the this$<n> hidden field, if
8126 necessary */
8127 if ((thisn_assign = build_thisn_assign ()))
8128 java_method_add_stmt (mdecl, thisn_assign);
8129
22eed1e6
APB
8130 end_artificial_method_body (mdecl);
8131 }
8132 /* Search for an explicit constructor invocation */
8133 else
8134 {
8135 int found = 0;
8136 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8137
8138 while (body)
8139 switch (TREE_CODE (body))
8140 {
8141 case CALL_EXPR:
8142 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8143 body = NULL_TREE;
8144 break;
8145 case COMPOUND_EXPR:
8146 case EXPR_WITH_FILE_LOCATION:
8147 body = TREE_OPERAND (body, 0);
8148 break;
8149 case BLOCK:
8150 body = BLOCK_EXPR_BODY (body);
8151 break;
8152 default:
8153 found = 0;
8154 body = NULL_TREE;
8155 }
8156 /* The constructor is missing an invocation of super() */
8157 if (!found)
8158 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8159 build_super_invocation (mdecl));
22eed1e6 8160
c2952b01
APB
8161 /* Insert the instance initializer block right here, after the
8162 super invocation. */
8163 add_instance_initializer (mdecl);
8164
8165 /* Generate the assignment to this$<n>, if necessary */
8166 if ((thisn_assign = build_thisn_assign ()))
8167 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8168
22eed1e6
APB
8169 /* Fix the constructor main block if we're adding extra stmts */
8170 if (compound)
8171 {
8172 compound = add_stmt_to_compound (compound, NULL_TREE,
8173 BLOCK_EXPR_BODY (main_block));
8174 BLOCK_EXPR_BODY (main_block) = compound;
8175 }
8176 }
8177}
8178
8179/* Browse constructors in the super class, searching for a constructor
8180 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8181 otherwise. If the current class is an anonymous inner class, look
8182 for something that has the same signature. */
22eed1e6
APB
8183
8184static int
c2952b01
APB
8185verify_constructor_super (mdecl)
8186 tree mdecl;
22eed1e6
APB
8187{
8188 tree class = CLASSTYPE_SUPER (current_class);
c2952b01
APB
8189 tree sdecl;
8190
22eed1e6
APB
8191 if (!class)
8192 return 0;
8193
c2952b01 8194 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8195 {
c2952b01
APB
8196 tree mdecl_arg_type;
8197 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8198 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8199 if (DECL_CONSTRUCTOR_P (sdecl))
8200 {
8201 tree arg_type;
8202 for (arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8203 arg_type != end_params_node &&
8204 mdecl_arg_type != end_params_node;
8205 arg_type = TREE_CHAIN (arg_type),
8206 mdecl_arg_type = TREE_CHAIN (mdecl_arg_type))
8207 if (TREE_VALUE (arg_type) != TREE_VALUE (mdecl_arg_type))
8208 break;
8209
8210 if (arg_type == end_params_node &&
8211 mdecl_arg_type == end_params_node)
8212 return 0;
8213 }
8214 }
8215 else
8216 {
8217 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8218 {
c2952b01
APB
8219 if (DECL_CONSTRUCTOR_P (sdecl)
8220 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)))
d77613be 8221 == end_params_node)
22eed1e6
APB
8222 return 0;
8223 }
8224 }
8225 return 1;
8226}
8227
22eed1e6 8228/* Generate code for all context remembered for code generation. */
b351b287
APB
8229
8230void
8231java_expand_classes ()
8232{
5423609c 8233 int save_error_count = 0;
c2952b01
APB
8234 static struct parser_ctxt *saved_ctxp = NULL;
8235
23a79c61
APB
8236 java_parse_abort_on_error ();
8237 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8238 return;
8239 java_layout_classes ();
8240 java_parse_abort_on_error ();
8241
48a840d9
APB
8242 /* The list of packages declaration seen so far needs to be
8243 reversed, so that package declared in a file being compiled gets
8244 priority over packages declared as a side effect of parsing other
8245 files.*/
8246 package_list = nreverse (package_list);
8247
c2952b01 8248 saved_ctxp = ctxp_for_generation;
b351b287
APB
8249 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8250 {
8251 ctxp = ctxp_for_generation;
8252 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8253 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8254 java_parse_abort_on_error ();
8255 }
c2952b01
APB
8256
8257 /* Find anonymous classes and expand their constructor, now they
8258 have been fixed. */
8259 for (ctxp_for_generation = saved_ctxp;
8260 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8261 {
8262 tree current;
8263 ctxp = ctxp_for_generation;
8264 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8265 {
8266 current_class = TREE_TYPE (current);
8267 if (ANONYMOUS_CLASS_P (current_class))
8268 {
8269 tree d;
8270 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8271 {
8272 if (DECL_CONSTRUCTOR_P (d))
8273 {
8274 restore_line_number_status (1);
8275 reset_method_name (d);
8276 java_complete_expand_method (d);
8277 restore_line_number_status (0);
8278 break; /* We now there are no other ones */
8279 }
8280 }
8281 }
8282 }
8283 }
8284
8285 /* If we've found error at that stage, don't try to generate
8286 anything, unless we're emitting xrefs or checking the syntax only
8287 (but not using -fsyntax-only for the purpose of generating
8288 bytecode. */
8289 if (java_error_count && !flag_emit_xref
8290 && (!flag_syntax_only && !flag_emit_class_files))
8291 return;
8292
8293 /* Now things are stable, go for generation of the class data. */
8294 for (ctxp_for_generation = saved_ctxp;
8295 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8296 {
8297 tree current;
8298 ctxp = ctxp_for_generation;
8299 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8300 {
8301 current_class = TREE_TYPE (current);
8302 outgoing_cpool = TYPE_CPOOL (current_class);
8303 if (flag_emit_class_files)
8304 write_classfile (current_class);
8305 if (flag_emit_xref)
8306 expand_xref (current_class);
8307 else if (! flag_syntax_only)
8308 finish_class ();
8309 }
8310 }
b351b287
APB
8311}
8312
e04a16fb
AG
8313/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8314 a tree list node containing RIGHT. Fore coming RIGHTs will be
8315 chained to this hook. LOCATION contains the location of the
8316 separating `.' operator. */
8317
8318static tree
8319make_qualified_primary (primary, right, location)
8320 tree primary, right;
8321 int location;
8322{
8323 tree wfl;
8324
c2952b01
APB
8325 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8326 wfl = build_wfl_wrap (primary);
e04a16fb
AG
8327 else
8328 {
8329 wfl = primary;
c2952b01
APB
8330 /* If wfl wasn't qualified, we build a first anchor */
8331 if (!EXPR_WFL_QUALIFICATION (wfl))
8332 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8333 }
8334
c2952b01 8335 /* And chain them */
e04a16fb
AG
8336 EXPR_WFL_LINECOL (right) = location;
8337 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8338 PRIMARY_P (wfl) = 1;
8339 return wfl;
8340}
8341
8342/* Simple merge of two name separated by a `.' */
8343
8344static tree
8345merge_qualified_name (left, right)
8346 tree left, right;
8347{
8348 tree node;
c2952b01
APB
8349 if (!left && !right)
8350 return NULL_TREE;
8351
8352 if (!left)
8353 return right;
8354
8355 if (!right)
8356 return left;
8357
e04a16fb
AG
8358 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8359 IDENTIFIER_LENGTH (left));
8360 obstack_1grow (&temporary_obstack, '.');
8361 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8362 IDENTIFIER_LENGTH (right));
8363 node = get_identifier (obstack_base (&temporary_obstack));
8364 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8365 QUALIFIED_P (node) = 1;
8366 return node;
8367}
8368
8369/* Merge the two parts of a qualified name into LEFT. Set the
8370 location information of the resulting node to LOCATION, usually
8371 inherited from the location information of the `.' operator. */
8372
8373static tree
8374make_qualified_name (left, right, location)
8375 tree left, right;
8376 int location;
8377{
bc3ca41b
PB
8378#ifdef USE_COMPONENT_REF
8379 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8380 EXPR_WFL_LINECOL (node) = location;
8381 return node;
8382#else
e04a16fb
AG
8383 tree left_id = EXPR_WFL_NODE (left);
8384 tree right_id = EXPR_WFL_NODE (right);
8385 tree wfl, merge;
8386
8387 merge = merge_qualified_name (left_id, right_id);
8388
8389 /* Left wasn't qualified and is now qualified */
8390 if (!QUALIFIED_P (left_id))
8391 {
8392 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8393 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8394 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8395 }
8396
8397 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8398 EXPR_WFL_LINECOL (wfl) = location;
8399 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8400
8401 EXPR_WFL_NODE (left) = merge;
8402 return left;
bc3ca41b 8403#endif
e04a16fb
AG
8404}
8405
8406/* Extract the last identifier component of the qualified in WFL. The
8407 last identifier is removed from the linked list */
8408
8409static tree
8410cut_identifier_in_qualified (wfl)
8411 tree wfl;
8412{
8413 tree q;
8414 tree previous = NULL_TREE;
8415 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8416 if (!TREE_CHAIN (q))
8417 {
8418 if (!previous)
781b0558 8419 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8420 TREE_CHAIN (previous) = NULL_TREE;
8421 return TREE_PURPOSE (q);
8422 }
8423}
8424
8425/* Resolve the expression name NAME. Return its decl. */
8426
8427static tree
5e942c50 8428resolve_expression_name (id, orig)
e04a16fb 8429 tree id;
5e942c50 8430 tree *orig;
e04a16fb
AG
8431{
8432 tree name = EXPR_WFL_NODE (id);
8433 tree decl;
8434
8435 /* 6.5.5.1: Simple expression names */
8436 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8437 {
8438 /* 15.13.1: NAME can appear within the scope of a local variable
8439 declaration */
8440 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8441 return decl;
8442
8443 /* 15.13.1: NAME can appear within a class declaration */
8444 else
8445 {
8446 decl = lookup_field_wrapper (current_class, name);
c2952b01
APB
8447
8448 /* Last chance: if we're within the context of an inner
8449 class, we might be trying to access a local variable
8450 defined in an outer context. We try to look for it
8451 now. */
8452 if (!decl && INNER_CLASS_TYPE_P (current_class))
8453 {
8454 char *alias_buffer;
8455 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
8456 name = get_identifier (alias_buffer);
8457 decl = lookup_field_wrapper (current_class, name);
8458 if (decl)
8459 FIELD_LOCAL_ALIAS_USED (decl) = 1;
8460 }
8461
e04a16fb
AG
8462 if (decl)
8463 {
c2952b01 8464 tree access = NULL_TREE;
e04a16fb
AG
8465 int fs = FIELD_STATIC (decl);
8466 /* Instance variable (8.3.1.1) can't appear within
8467 static method, static initializer or initializer for
8468 a static variable. */
8469 if (!fs && METHOD_STATIC (current_function_decl))
8470 {
7f10c2e2 8471 static_ref_err (id, name, current_class);
e04a16fb
AG
8472 return error_mark_node;
8473 }
22eed1e6
APB
8474 /* Instance variables can't appear as an argument of
8475 an explicit constructor invocation */
8476 if (!fs && ctxp->explicit_constructor_p)
8477 {
8478 parse_error_context
781b0558 8479 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8480 return error_mark_node;
8481 }
5e942c50 8482
c2952b01
APB
8483 /* If we're processing an inner class and we're trying
8484 to access a field belonging to an outer class, build
8485 the access to the field */
8486 if (!fs && outer_field_access_p (current_class, decl))
8487 return build_outer_field_access (id, decl);
8488
5e942c50 8489 /* Otherwise build what it takes to access the field */
c2952b01
APB
8490 access = build_field_ref ((fs ? NULL_TREE : current_this),
8491 DECL_CONTEXT (decl), name);
e8fc7396 8492 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8493 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8494 /* We may be asked to save the real field access node */
8495 if (orig)
c2952b01 8496 *orig = access;
5e942c50 8497 /* And we return what we got */
c2952b01 8498 return access;
e04a16fb
AG
8499 }
8500 /* Fall down to error report on undefined variable */
8501 }
8502 }
8503 /* 6.5.5.2 Qualified Expression Names */
8504 else
8505 {
5e942c50
APB
8506 if (orig)
8507 *orig = NULL_TREE;
e04a16fb
AG
8508 qualify_ambiguous_name (id);
8509 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8510 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8511 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8512 }
8513
8514 /* We've got an error here */
8515 parse_error_context (id, "Undefined variable `%s'",
8516 IDENTIFIER_POINTER (name));
8517
8518 return error_mark_node;
8519}
8520
7f10c2e2
APB
8521static void
8522static_ref_err (wfl, field_id, class_type)
8523 tree wfl, field_id, class_type;
8524{
8525 parse_error_context
8526 (wfl,
8527 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8528 IDENTIFIER_POINTER (field_id),
8529 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8530}
8531
e04a16fb
AG
8532/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8533 We return something suitable to generate the field access. We also
8534 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8535 recipient's address can be null. */
8536
8537static tree
8538resolve_field_access (qual_wfl, field_decl, field_type)
8539 tree qual_wfl;
8540 tree *field_decl, *field_type;
8541{
8542 int is_static = 0;
8543 tree field_ref;
8544 tree decl, where_found, type_found;
8545
8546 if (resolve_qualified_expression_name (qual_wfl, &decl,
8547 &where_found, &type_found))
8548 return error_mark_node;
8549
8550 /* Resolve the LENGTH field of an array here */
8551 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
e8fc7396 8552 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8553 {
8554 tree length = build_java_array_length_access (where_found);
8555 field_ref =
8556 build_java_arraynull_check (type_found, length, int_type_node);
8557 }
8558 /* We might have been trying to resolve field.method(). In which
8559 case, the resolution is over and decl is the answer */
34f4db93 8560 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8561 field_ref = decl;
34f4db93 8562 else if (JDECL_P (decl))
e04a16fb 8563 {
5e942c50
APB
8564 int static_final_found = 0;
8565 if (!type_found)
8566 type_found = DECL_CONTEXT (decl);
34f4db93 8567 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
5e942c50
APB
8568 if (FIELD_FINAL (decl)
8569 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8570 && DECL_LANG_SPECIFIC (decl)
7525cc04 8571 && DECL_INITIAL (decl))
5e942c50 8572 {
7525cc04 8573 field_ref = DECL_INITIAL (decl);
5e942c50
APB
8574 static_final_found = 1;
8575 }
8576 else
7f10c2e2
APB
8577 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8578 NULL_TREE : where_found),
5e942c50 8579 type_found, DECL_NAME (decl));
e04a16fb
AG
8580 if (field_ref == error_mark_node)
8581 return error_mark_node;
e8fc7396
APB
8582 if (is_static && !static_final_found
8583 && !flag_emit_class_files && !flag_emit_xref)
98f3c1db 8584 field_ref = build_class_init (type_found, field_ref);
e04a16fb
AG
8585 }
8586 else
8587 field_ref = decl;
8588
8589 if (field_decl)
8590 *field_decl = decl;
8591 if (field_type)
c877974e
APB
8592 *field_type = (QUAL_DECL_TYPE (decl) ?
8593 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8594 return field_ref;
8595}
8596
e28cd97b
APB
8597/* If NODE is an access to f static field, strip out the class
8598 initialization part and return the field decl, otherwise, return
8599 NODE. */
8600
8601static tree
8602strip_out_static_field_access_decl (node)
8603 tree node;
8604{
8605 if (TREE_CODE (node) == COMPOUND_EXPR)
8606 {
8607 tree op1 = TREE_OPERAND (node, 1);
8608 if (TREE_CODE (op1) == COMPOUND_EXPR)
8609 {
8610 tree call = TREE_OPERAND (op1, 0);
8611 if (TREE_CODE (call) == CALL_EXPR
8612 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8613 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8614 == soft_initclass_node)
8615 return TREE_OPERAND (op1, 1);
8616 }
2f11d407
TT
8617 else if (JDECL_P (op1))
8618 return op1;
e28cd97b
APB
8619 }
8620 return node;
8621}
8622
e04a16fb
AG
8623/* 6.5.5.2: Qualified Expression Names */
8624
8625static int
8626resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8627 tree wfl;
8628 tree *found_decl, *type_found, *where_found;
8629{
8630 int from_type = 0; /* Field search initiated from a type */
c2952b01 8631 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8632 int previous_call_static = 0;
8633 int is_static;
8634 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8635 /* For certain for of inner class instantiation */
8636 tree saved_current, saved_this;
8637#define RESTORE_THIS_AND_CURRENT_CLASS \
8638 { current_class = saved_current; current_this = saved_this;}
8639
c877974e 8640 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8641
8642 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8643 {
8644 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8645 tree ret_decl; /* for EH checking */
8646 int location; /* for EH checking */
e04a16fb
AG
8647
8648 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8649 switch (TREE_CODE (qual_wfl))
8650 {
8651 case CALL_EXPR:
b67d701b 8652 case NEW_CLASS_EXPR:
e04a16fb
AG
8653 /* If the access to the function call is a non static field,
8654 build the code to access it. */
34f4db93 8655 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8656 {
ac825856
APB
8657 decl = maybe_access_field (decl, *where_found,
8658 DECL_CONTEXT (decl));
e04a16fb
AG
8659 if (decl == error_mark_node)
8660 return 1;
8661 }
c2952b01 8662
e04a16fb
AG
8663 /* And code for the function call */
8664 if (complete_function_arguments (qual_wfl))
8665 return 1;
c2952b01
APB
8666
8667 /* We might have to setup a new current class and a new this
8668 for the search of an inner class, relative to the type of
8669 a expression resolved as `decl'. The current values are
8670 saved and restored shortly after */
8671 saved_current = current_class;
8672 saved_this = current_this;
8673 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8674 {
8675 current_class = type;
8676 current_this = decl;
8677 }
8678
89e09b9a
PB
8679 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8680 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8681 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8682 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8683 *where_found = patch_method_invocation (qual_wfl, decl, type,
8684 &is_static, &ret_decl);
e04a16fb 8685 if (*where_found == error_mark_node)
c2952b01
APB
8686 {
8687 RESTORE_THIS_AND_CURRENT_CLASS;
8688 return 1;
8689 }
e04a16fb
AG
8690 *type_found = type = QUAL_DECL_TYPE (*where_found);
8691
c2952b01
APB
8692 /* If we're creating an inner class instance, check for that
8693 an enclosing instance is in scope */
8694 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8695 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8696 {
8697 parse_error_context
165f37bc
APB
8698 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8699 lang_printable_name (type, 0),
8700 (!current_this ? "" :
8701 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8702 RESTORE_THIS_AND_CURRENT_CLASS;
8703 return 1;
8704 }
8705
8706 /* In case we had to change then to resolve a inner class
8707 instantiation using a primary qualified by a `new' */
8708 RESTORE_THIS_AND_CURRENT_CLASS;
8709
7705e9db
APB
8710 /* EH check */
8711 if (location)
8712 check_thrown_exceptions (location, ret_decl);
8713
e04a16fb
AG
8714 /* If the previous call was static and this one is too,
8715 build a compound expression to hold the two (because in
8716 that case, previous function calls aren't transported as
8717 forcoming function's argument. */
8718 if (previous_call_static && is_static)
8719 {
8720 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8721 TREE_SIDE_EFFECTS (decl) = 1;
8722 }
8723 else
8724 {
8725 previous_call_static = is_static;
8726 decl = *where_found;
8727 }
c2952b01 8728 from_type = 0;
e04a16fb
AG
8729 continue;
8730
d8fccff5 8731 case NEW_ARRAY_EXPR:
c2952b01 8732 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
8733 *where_found = decl = java_complete_tree (qual_wfl);
8734 if (decl == error_mark_node)
8735 return 1;
8736 *type_found = type = QUAL_DECL_TYPE (decl);
8737 CLASS_LOADED_P (type) = 1;
8738 continue;
8739
e04a16fb
AG
8740 case CONVERT_EXPR:
8741 *where_found = decl = java_complete_tree (qual_wfl);
8742 if (decl == error_mark_node)
8743 return 1;
8744 *type_found = type = QUAL_DECL_TYPE (decl);
8745 from_cast = 1;
8746 continue;
8747
22eed1e6 8748 case CONDITIONAL_EXPR:
5e942c50 8749 case STRING_CST:
ac22f9cb 8750 case MODIFY_EXPR:
22eed1e6
APB
8751 *where_found = decl = java_complete_tree (qual_wfl);
8752 if (decl == error_mark_node)
8753 return 1;
8754 *type_found = type = QUAL_DECL_TYPE (decl);
8755 continue;
8756
e04a16fb
AG
8757 case ARRAY_REF:
8758 /* If the access to the function call is a non static field,
8759 build the code to access it. */
34f4db93 8760 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
8761 {
8762 decl = maybe_access_field (decl, *where_found, type);
8763 if (decl == error_mark_node)
8764 return 1;
8765 }
8766 /* And code for the array reference expression */
8767 decl = java_complete_tree (qual_wfl);
8768 if (decl == error_mark_node)
8769 return 1;
8770 type = QUAL_DECL_TYPE (decl);
8771 continue;
0a2138e2 8772
37feda7d
APB
8773 case PLUS_EXPR:
8774 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8775 return 1;
8776 if ((type = patch_string (decl)))
8777 decl = type;
8778 *where_found = QUAL_RESOLUTION (q) = decl;
8779 *type_found = type = TREE_TYPE (decl);
8780 break;
8781
165f37bc
APB
8782 case CLASS_LITERAL:
8783 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8784 return 1;
8785 *where_found = QUAL_RESOLUTION (q) = decl;
8786 *type_found = type = TREE_TYPE (decl);
8787 break;
8788
0a2138e2
APB
8789 default:
8790 /* Fix for -Wall Just go to the next statement. Don't
8791 continue */
a3f406ce 8792 break;
e04a16fb
AG
8793 }
8794
8795 /* If we fall here, we weren't processing a (static) function call. */
8796 previous_call_static = 0;
8797
8798 /* It can be the keyword THIS */
8799 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
8800 {
8801 if (!current_this)
8802 {
8803 parse_error_context
8804 (wfl, "Keyword `this' used outside allowed context");
8805 return 1;
8806 }
f63991a8
APB
8807 if (ctxp->explicit_constructor_p)
8808 {
781b0558 8809 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
8810 return 1;
8811 }
e04a16fb 8812 /* We have to generate code for intermediate acess */
c2952b01
APB
8813 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
8814 {
8815 *where_found = decl = current_this;
8816 *type_found = type = QUAL_DECL_TYPE (decl);
8817 }
8818 /* We're trying to access the this from somewhere else... */
8819 else
8820 {
8821 *where_found = decl = build_current_thisn (type);
8822 from_qualified_this = 1;
8823 }
8824
8825 from_type = 0;
e04a16fb
AG
8826 continue;
8827 }
8828
8829 /* 15.10.2 Accessing Superclass Members using SUPER */
8830 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
8831 {
8832 tree node;
8833 /* Check on the restricted use of SUPER */
8834 if (METHOD_STATIC (current_function_decl)
8835 || current_class == object_type_node)
8836 {
8837 parse_error_context
8838 (wfl, "Keyword `super' used outside allowed context");
8839 return 1;
8840 }
8841 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
8842 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
8843 CLASSTYPE_SUPER (current_class),
8844 build_this (EXPR_WFL_LINECOL (qual_wfl)));
8845 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
8846 if (decl == error_mark_node)
8847 return 1;
e04a16fb
AG
8848 *type_found = type = QUAL_DECL_TYPE (decl);
8849 from_super = from_type = 1;
8850 continue;
8851 }
8852
8853 /* 15.13.1: Can't search for field name in packages, so we
8854 assume a variable/class name was meant. */
8855 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
8856 {
5e942c50
APB
8857 tree name = resolve_package (wfl, &q);
8858 if (name)
8859 {
c2952b01 8860 tree list;
5e942c50
APB
8861 *where_found = decl = resolve_no_layout (name, qual_wfl);
8862 /* We wan't to be absolutely that the class is laid
8863 out. We're going to search something inside it. */
8864 *type_found = type = TREE_TYPE (decl);
8865 layout_class (type);
8866 from_type = 1;
c2952b01 8867
dde1da72
APB
8868 /* Fix them all the way down, if any are left. */
8869 if (q)
c2952b01 8870 {
dde1da72
APB
8871 list = TREE_CHAIN (q);
8872 while (list)
8873 {
8874 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
8875 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
8876 list = TREE_CHAIN (list);
8877 }
c2952b01 8878 }
5e942c50 8879 }
e04a16fb 8880 else
5e942c50
APB
8881 {
8882 if (from_super || from_cast)
8883 parse_error_context
8884 ((from_cast ? qual_wfl : wfl),
8885 "No variable `%s' defined in class `%s'",
8886 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
8887 lang_printable_name (type, 0));
8888 else
8889 parse_error_context
8890 (qual_wfl, "Undefined variable or class name: `%s'",
8891 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
8892 return 1;
8893 }
e04a16fb
AG
8894 }
8895
8896 /* We have a type name. It's been already resolved when the
8897 expression was qualified. */
8898 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
8899 {
8900 if (!(decl = QUAL_RESOLUTION (q)))
8901 return 1; /* Error reported already */
8902
c2952b01
APB
8903 /* Sneak preview. If next we see a `new', we're facing a
8904 qualification with resulted in a type being selected
8905 instead of a field. Report the error */
8906 if(TREE_CHAIN (q)
8907 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
8908 {
8909 parse_error_context (qual_wfl, "Undefined variable `%s'",
8910 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8911 return 1;
8912 }
8913
e04a16fb
AG
8914 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
8915 {
8916 parse_error_context
8917 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
8918 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 8919 GET_TYPE_NAME (type),
e04a16fb
AG
8920 IDENTIFIER_POINTER (DECL_NAME (decl)),
8921 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
8922 return 1;
8923 }
5e942c50 8924 check_deprecation (qual_wfl, decl);
c2952b01 8925
e04a16fb
AG
8926 type = TREE_TYPE (decl);
8927 from_type = 1;
8928 }
8929 /* We resolve and expression name */
8930 else
8931 {
cd531a2e 8932 tree field_decl = NULL_TREE;
e04a16fb
AG
8933
8934 /* If there exists an early resolution, use it. That occurs
8935 only once and we know that there are more things to
8936 come. Don't do that when processing something after SUPER
8937 (we need more thing to be put in place below */
8938 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
8939 {
8940 decl = QUAL_RESOLUTION (q);
c877974e 8941 if (!type)
5e942c50 8942 {
7f10c2e2
APB
8943 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
8944 {
8945 if (current_this)
8946 *where_found = current_this;
8947 else
8948 {
8949 static_ref_err (qual_wfl, DECL_NAME (decl),
8950 current_class);
8951 return 1;
8952 }
8953 }
c877974e
APB
8954 else
8955 {
8956 *where_found = TREE_TYPE (decl);
8957 if (TREE_CODE (*where_found) == POINTER_TYPE)
8958 *where_found = TREE_TYPE (*where_found);
8959 }
5e942c50 8960 }
b67d701b 8961 }
e04a16fb
AG
8962
8963 /* We have to search for a field, knowing the type of its
8964 container. The flag FROM_TYPE indicates that we resolved
8965 the last member of the expression as a type name, which
5e942c50
APB
8966 means that for the resolution of this field, we'll look
8967 for other errors than if it was resolved as a member of
8968 an other field. */
e04a16fb
AG
8969 else
8970 {
8971 int is_static;
5e942c50
APB
8972 tree field_decl_type; /* For layout */
8973
e04a16fb
AG
8974 if (!from_type && !JREFERENCE_TYPE_P (type))
8975 {
8976 parse_error_context
8977 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
8978 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 8979 lang_printable_name (type, 0),
e04a16fb
AG
8980 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
8981 return 1;
8982 }
8983
dc0b3eff
PB
8984 field_decl = lookup_field_wrapper (type,
8985 EXPR_WFL_NODE (qual_wfl));
8986 if (field_decl == NULL_TREE)
e04a16fb
AG
8987 {
8988 parse_error_context
2aa11e97 8989 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 8990 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 8991 GET_TYPE_NAME (type));
e04a16fb
AG
8992 return 1;
8993 }
dc0b3eff
PB
8994 if (field_decl == error_mark_node)
8995 return 1;
5e942c50
APB
8996
8997 /* Layout the type of field_decl, since we may need
c877974e
APB
8998 it. Don't do primitive types or loaded classes. The
8999 situation of non primitive arrays may not handled
9000 properly here. FIXME */
5e942c50
APB
9001 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9002 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9003 else
9004 field_decl_type = TREE_TYPE (field_decl);
9005 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9006 && !CLASS_LOADED_P (field_decl_type)
9007 && !TYPE_ARRAY_P (field_decl_type))
9008 resolve_and_layout (field_decl_type, NULL_TREE);
9009 if (TYPE_ARRAY_P (field_decl_type))
9010 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9011
9012 /* Check on accessibility here */
9013 if (not_accessible_p (type, field_decl, from_super))
9014 {
9015 parse_error_context
9016 (qual_wfl,
9017 "Can't access %s field `%s.%s' from `%s'",
9018 java_accstring_lookup
9019 (get_access_flags_from_decl (field_decl)),
2aa11e97 9020 GET_TYPE_NAME (type),
e04a16fb
AG
9021 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9022 IDENTIFIER_POINTER
9023 (DECL_NAME (TYPE_NAME (current_class))));
9024 return 1;
9025 }
5e942c50 9026 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9027
9028 /* There are things to check when fields are accessed
9029 from type. There are no restrictions on a static
9030 declaration of the field when it is accessed from an
9031 interface */
9032 is_static = FIELD_STATIC (field_decl);
9033 if (!from_super && from_type
c2952b01
APB
9034 && !TYPE_INTERFACE_P (type)
9035 && !is_static
9036 && (current_function_decl
9037 && METHOD_STATIC (current_function_decl)))
e04a16fb 9038 {
7f10c2e2 9039 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9040 return 1;
9041 }
9042 from_cast = from_super = 0;
9043
c2952b01
APB
9044 /* It's an access from a type but it isn't static, we
9045 make it relative to `this'. */
9046 if (!is_static && from_type)
9047 decl = current_this;
9048
5e942c50
APB
9049 /* If we need to generate something to get a proper
9050 handle on what this field is accessed from, do it
9051 now. */
e04a16fb
AG
9052 if (!is_static)
9053 {
c583dd46 9054 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9055 if (decl == error_mark_node)
9056 return 1;
9057 }
9058
9059 /* We want to keep the location were found it, and the type
9060 we found. */
9061 *where_found = decl;
9062 *type_found = type;
9063
c2952b01
APB
9064 /* Generate the correct expression for field access from
9065 qualified this */
9066 if (from_qualified_this)
9067 {
9068 field_decl = build_outer_field_access (qual_wfl, field_decl);
9069 from_qualified_this = 0;
9070 }
9071
e04a16fb
AG
9072 /* This is the decl found and eventually the next one to
9073 search from */
9074 decl = field_decl;
9075 }
e04a16fb
AG
9076 from_type = 0;
9077 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9078
9079 /* Sneak preview. If decl is qualified by a `new', report
9080 the error here to be accurate on the peculiar construct */
9081 if (TREE_CHAIN (q)
9082 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9083 && !JREFERENCE_TYPE_P (type))
9084 {
9085 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9086 lang_printable_name (type, 0));
9087 return 1;
9088 }
e04a16fb 9089 }
dde1da72
APB
9090 /* `q' might have changed due to a after package resolution
9091 re-qualification */
9092 if (!q)
9093 break;
e04a16fb
AG
9094 }
9095 *found_decl = decl;
9096 return 0;
9097}
9098
9099/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9100 can't be accessed from REFERENCE (a record type). */
9101
be245ac0
KG
9102static int
9103not_accessible_p (reference, member, from_super)
e04a16fb
AG
9104 tree reference, member;
9105 int from_super;
9106{
9107 int access_flag = get_access_flags_from_decl (member);
9108
9109 /* Access always granted for members declared public */
9110 if (access_flag & ACC_PUBLIC)
9111 return 0;
9112
9113 /* Check access on protected members */
9114 if (access_flag & ACC_PROTECTED)
9115 {
9116 /* Access granted if it occurs from within the package
9117 containing the class in which the protected member is
9118 declared */
9119 if (class_in_current_package (DECL_CONTEXT (member)))
9120 return 0;
9121
9bbc7d9f
PB
9122 /* If accessed with the form `super.member', then access is granted */
9123 if (from_super)
9124 return 0;
e04a16fb 9125
9bbc7d9f
PB
9126 /* Otherwise, access is granted if occuring from the class where
9127 member is declared or a subclass of it */
9128 if (inherits_from_p (reference, current_class))
9129 return 0;
e04a16fb
AG
9130 return 1;
9131 }
9132
9133 /* Check access on private members. Access is granted only if it
c2952b01
APB
9134 occurs from within the class in witch it is declared. Exceptions
9135 are access from inner-classes. This section is probably not
9136 complete. FIXME */
e04a16fb 9137 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9138 return (current_class == DECL_CONTEXT (member) ? 0 :
9139 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9140
9141 /* Default access are permitted only when occuring within the
9142 package in which the type (REFERENCE) is declared. In other words,
9143 REFERENCE is defined in the current package */
9144 if (ctxp->package)
9145 return !class_in_current_package (reference);
9146
9147 /* Otherwise, access is granted */
9148 return 0;
9149}
9150
5e942c50
APB
9151/* Test deprecated decl access. */
9152static void
9153check_deprecation (wfl, decl)
9154 tree wfl, decl;
9155{
49f48c71 9156 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9157 /* Complain if the field is deprecated and the file it was defined
9158 in isn't compiled at the same time the file which contains its
9159 use is */
9160 if (DECL_DEPRECATED (decl)
9161 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9162 {
9163 char the [20];
9164 switch (TREE_CODE (decl))
9165 {
9166 case FUNCTION_DECL:
9167 strcpy (the, "method");
9168 break;
9169 case FIELD_DECL:
9170 strcpy (the, "field");
9171 break;
9172 case TYPE_DECL:
9173 strcpy (the, "class");
9174 break;
15fdcfe9
PB
9175 default:
9176 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9177 }
9178 parse_warning_context
9179 (wfl, "The %s `%s' in class `%s' has been deprecated",
9180 the, lang_printable_name (decl, 0),
9181 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9182 }
9183}
9184
e04a16fb
AG
9185/* Returns 1 if class was declared in the current package, 0 otherwise */
9186
9187static int
9188class_in_current_package (class)
9189 tree class;
9190{
9191 static tree cache = NULL_TREE;
9192 int qualified_flag;
9193 tree left;
9194
9195 if (cache == class)
9196 return 1;
9197
9198 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9199
9200 /* If the current package is empty and the name of CLASS is
9201 qualified, class isn't in the current package. If there is a
9202 current package and the name of the CLASS is not qualified, class
9203 isn't in the current package */
0a2138e2 9204 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9205 return 0;
9206
9207 /* If there is not package and the name of CLASS isn't qualified,
9208 they belong to the same unnamed package */
9209 if (!ctxp->package && !qualified_flag)
9210 return 1;
9211
9212 /* Compare the left part of the name of CLASS with the package name */
9213 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9214 if (ctxp->package == left)
9215 {
9216 cache = class;
9217 return 1;
9218 }
9219 return 0;
9220}
9221
9222/* This function may generate code to access DECL from WHERE. This is
9223 done only if certain conditions meet. */
9224
9225static tree
9226maybe_access_field (decl, where, type)
9227 tree decl, where, type;
9228{
5e942c50
APB
9229 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9230 && !FIELD_STATIC (decl))
e04a16fb 9231 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9232 (type ? type : DECL_CONTEXT (decl)),
9233 DECL_NAME (decl));
e04a16fb
AG
9234 return decl;
9235}
9236
15fdcfe9 9237/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9238 and according to the situation, PRIMARY and WHERE may be
9239 used. IS_STATIC is set to 1 if the invoked function is static. */
9240
9241static tree
89e09b9a 9242patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9243 tree patch, primary, where;
9244 int *is_static;
b9f7e36c 9245 tree *ret_decl;
e04a16fb
AG
9246{
9247 tree wfl = TREE_OPERAND (patch, 0);
9248 tree args = TREE_OPERAND (patch, 1);
9249 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9250 tree list;
22eed1e6 9251 int is_static_flag = 0;
89e09b9a 9252 int is_super_init = 0;
bccaf73a 9253 tree this_arg = NULL_TREE;
e04a16fb
AG
9254
9255 /* Should be overriden if everything goes well. Otherwise, if
9256 something fails, it should keep this value. It stop the
9257 evaluation of a bogus assignment. See java_complete_tree,
9258 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9259 evaluating an assignment */
9260 TREE_TYPE (patch) = error_mark_node;
9261
9262 /* Since lookup functions are messing with line numbers, save the
9263 context now. */
9264 java_parser_context_save_global ();
9265
9266 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9267
9268 /* Resolution of qualified name, excluding constructors */
9269 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9270 {
dde1da72 9271 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9272 /* Extract the last IDENTIFIER of the qualified
9273 expression. This is a wfl and we will use it's location
9274 data during error report. */
9275 identifier_wfl = cut_identifier_in_qualified (wfl);
9276 identifier = EXPR_WFL_NODE (identifier_wfl);
9277
9278 /* Given the context, IDENTIFIER is syntactically qualified
9279 as a MethodName. We need to qualify what's before */
9280 qualify_ambiguous_name (wfl);
dde1da72 9281 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9282
dde1da72
APB
9283 if (resolved == error_mark_node)
9284 PATCH_METHOD_RETURN_ERROR ();
9285
9286 type = GET_SKIP_TYPE (resolved);
9287 resolve_and_layout (type, NULL_TREE);
9288 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9289 args = nreverse (args);
2c56429a 9290
e04a16fb 9291 /* We're resolving a call from a type */
dde1da72 9292 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9293 {
dde1da72 9294 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9295 {
9296 parse_error_context
781b0558
KG
9297 (identifier_wfl,
9298 "Can't make static reference to method `%s' in interface `%s'",
9299 IDENTIFIER_POINTER (identifier),
e04a16fb 9300 IDENTIFIER_POINTER (name));
b9f7e36c 9301 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9302 }
e04a16fb
AG
9303 if (list && !METHOD_STATIC (list))
9304 {
c2e3db92 9305 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9306 parse_error_context
9307 (identifier_wfl,
9308 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9309 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9310 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9311 free (fct_name);
b9f7e36c 9312 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9313 }
9314 }
e04a16fb 9315 else
dde1da72
APB
9316 this_arg = primary = resolved;
9317
5e942c50 9318 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9319 wfl = identifier_wfl;
9320 }
9321 /* Resolution of simple names, names generated after a primary: or
9322 constructors */
9323 else
9324 {
cd531a2e 9325 tree class_to_search = NULL_TREE;
c2952b01 9326 int lc; /* Looking for Constructor */
e04a16fb
AG
9327
9328 /* We search constructor in their target class */
9329 if (CALL_CONSTRUCTOR_P (patch))
9330 {
22eed1e6
APB
9331 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9332 class_to_search = EXPR_WFL_NODE (wfl);
9333 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9334 this_identifier_node)
9335 class_to_search = NULL_TREE;
9336 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9337 super_identifier_node)
e04a16fb 9338 {
89e09b9a 9339 is_super_init = 1;
22eed1e6
APB
9340 if (CLASSTYPE_SUPER (current_class))
9341 class_to_search =
9342 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9343 else
9344 {
781b0558 9345 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9346 PATCH_METHOD_RETURN_ERROR ();
9347 }
e04a16fb 9348 }
22eed1e6
APB
9349
9350 /* Class to search is NULL if we're searching the current one */
9351 if (class_to_search)
e04a16fb 9352 {
c2952b01
APB
9353 class_to_search = resolve_and_layout (class_to_search, wfl);
9354
22eed1e6
APB
9355 if (!class_to_search)
9356 {
9357 parse_error_context
9358 (wfl, "Class `%s' not found in type declaration",
9359 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9360 PATCH_METHOD_RETURN_ERROR ();
9361 }
9362
5e942c50
APB
9363 /* Can't instantiate an abstract class, but we can
9364 invoke it's constructor. It's use within the `new'
9365 context is denied here. */
9366 if (CLASS_ABSTRACT (class_to_search)
9367 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9368 {
9369 parse_error_context
781b0558
KG
9370 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9371 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9372 PATCH_METHOD_RETURN_ERROR ();
9373 }
c2952b01 9374
22eed1e6 9375 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9376 }
22eed1e6
APB
9377 else
9378 class_to_search = current_class;
e04a16fb
AG
9379 lc = 1;
9380 }
9381 /* This is a regular search in the local class, unless an
9382 alternate class is specified. */
9383 else
9384 {
9385 class_to_search = (where ? where : current_class);
9386 lc = 0;
9387 }
c2952b01 9388
e04a16fb
AG
9389 /* NAME is a simple identifier or comes from a primary. Search
9390 in the class whose declaration contain the method being
9391 invoked. */
c877974e 9392 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9393
c2952b01 9394 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9395 /* Don't continue if no method were found, as the next statement
9396 can't be executed then. */
b9f7e36c
APB
9397 if (!list)
9398 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9399
9400 /* Check for static reference if non static methods */
9401 if (check_for_static_method_reference (wfl, patch, list,
9402 class_to_search, primary))
b9f7e36c 9403 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9404
165f37bc
APB
9405 /* Check for inner classes creation from illegal contexts */
9406 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9407 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9408 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9409 {
9410 parse_error_context
9411 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9412 lang_printable_name (class_to_search, 0),
9413 (!current_this ? "" :
9414 "; an explicit one must be provided when creating this inner class"));
9415 PATCH_METHOD_RETURN_ERROR ();
9416 }
9417
22eed1e6
APB
9418 /* Non static methods are called with the current object extra
9419 argument. If patch a `new TYPE()', the argument is the value
9420 returned by the object allocator. If method is resolved as a
9421 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9422 args = nreverse (args);
bccaf73a 9423 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9424 {
9425 this_arg = primary ? primary : current_this;
9426
9427 /* If we're using an access method, things are different.
9428 There are two familly of cases:
9429
9430 1) We're not generating bytecodes:
9431
9432 - LIST is non static. It's invocation is transformed from
9433 x(a1,...,an) into this$<n>.x(a1,....an).
9434 - LIST is static. It's invocation is transformed from
9435 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9436
9437 2) We're generating bytecodes:
9438
9439 - LIST is non static. It's invocation is transformed from
9440 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9441 - LIST is static. It's invocation is transformed from
9442 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9443
9444 Of course, this$<n> can be abitrary complex, ranging from
9445 this$0 (the immediate outer context) to
9446 access$0(access$0(...(this$0))).
9447
9448 maybe_use_access_method returns a non zero value if the
9449 this_arg has to be deplaced into the (then generated)
9450 stub argument list. In the mean time, the selected
9451 function might have be replaced by a generated stub. */
9452 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9453 args = tree_cons (NULL_TREE, this_arg, args);
9454 }
e04a16fb 9455 }
b67d701b 9456
e04a16fb
AG
9457 /* Merge point of all resolution schemes. If we have nothing, this
9458 is an error, already signaled */
b9f7e36c
APB
9459 if (!list)
9460 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9461
e04a16fb
AG
9462 /* Check accessibility, position the is_static flag, build and
9463 return the call */
9bbc7d9f 9464 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9465 {
c2e3db92 9466 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9467 parse_error_context
9468 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9469 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9470 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9471 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9472 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9473 free (fct_name);
b9f7e36c 9474 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9475 }
5e942c50 9476 check_deprecation (wfl, list);
22eed1e6 9477
c2952b01
APB
9478 /* If invoking a innerclass constructor, there are hidden parameters
9479 to pass */
9480 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9481 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9482 {
9483 /* And make sure we add the accessed local variables to be saved
9484 in field aliases. */
9485 args = build_alias_initializer_parameter_list
9486 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9487
9488 /* We have to reverse things. Find out why. FIXME */
9489 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9490 args = nreverse (args);
9491
9492 /* Secretely pass the current_this/primary as a second argument */
165f37bc
APB
9493 if (primary || current_this)
9494 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9495 else
9496 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9497 }
9498
22eed1e6 9499 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9500 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9501 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9502
c3f2a476
APB
9503 /* In the context of an explicit constructor invocation, we can't
9504 invoke any method relying on `this'. Exceptions are: we're
9505 invoking a static function, primary exists and is not the current
9506 this, we're creating a new object. */
22eed1e6 9507 if (ctxp->explicit_constructor_p
c3f2a476
APB
9508 && !is_static_flag
9509 && (!primary || primary == current_this)
9510 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9511 {
781b0558 9512 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9513 PATCH_METHOD_RETURN_ERROR ();
9514 }
e04a16fb 9515 java_parser_context_restore_global ();
22eed1e6
APB
9516 if (is_static)
9517 *is_static = is_static_flag;
b9f7e36c
APB
9518 /* Sometimes, we want the decl of the selected method. Such as for
9519 EH checking */
9520 if (ret_decl)
9521 *ret_decl = list;
89e09b9a
PB
9522 patch = patch_invoke (patch, list, args);
9523 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9524 {
c2952b01
APB
9525 tree finit_parms, finit_call;
9526
9527 /* Prepare to pass hidden parameters to $finit$, if any. */
9528 finit_parms = build_alias_initializer_parameter_list
9529 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9530
c2952b01
APB
9531 finit_call =
9532 build_method_invocation (build_wfl_node (finit_identifier_node),
9533 finit_parms);
9534
9535 /* Generate the code used to initialize fields declared with an
9536 initialization statement and build a compound statement along
9537 with the super constructor invocation. */
89e09b9a
PB
9538 patch = build (COMPOUND_EXPR, void_type_node, patch,
9539 java_complete_tree (finit_call));
9540 CAN_COMPLETE_NORMALLY (patch) = 1;
9541 }
9542 return patch;
e04a16fb
AG
9543}
9544
9545/* Check that we're not trying to do a static reference to a method in
9546 non static method. Return 1 if it's the case, 0 otherwise. */
9547
9548static int
9549check_for_static_method_reference (wfl, node, method, where, primary)
9550 tree wfl, node, method, where, primary;
9551{
9552 if (METHOD_STATIC (current_function_decl)
9553 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9554 {
c2e3db92 9555 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9556 parse_error_context
9557 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9558 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9559 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9560 free (fct_name);
9561 return 1;
9562 }
9563 return 0;
9564}
9565
c2952b01
APB
9566/* Fix the invocation of *MDECL if necessary in the case of a
9567 invocation from an inner class. *THIS_ARG might be modified
9568 appropriately and an alternative access to *MDECL might be
9569 returned. */
9570
9571static int
9572maybe_use_access_method (is_super_init, mdecl, this_arg)
9573 int is_super_init;
9574 tree *mdecl, *this_arg;
9575{
9576 tree ctx;
9577 tree md = *mdecl, ta = *this_arg;
9578 int to_return = 0;
9579 int non_static_context = !METHOD_STATIC (md);
9580
9581 if (is_super_init
165f37bc
APB
9582 || DECL_CONTEXT (md) == current_class
9583 || !PURE_INNER_CLASS_TYPE_P (current_class)
9584 || DECL_FINIT_P (md))
c2952b01
APB
9585 return 0;
9586
9587 /* If we're calling a method found in an enclosing class, generate
9588 what it takes to retrieve the right this. Don't do that if we're
9589 invoking a static method. */
9590
9591 if (non_static_context)
9592 {
9593 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9594 if (ctx == DECL_CONTEXT (md))
9595 {
9596 ta = build_current_thisn (current_class);
9597 ta = build_wfl_node (ta);
9598 }
9599 else
9600 {
9601 tree type = ctx;
9602 while (type)
9603 {
9604 maybe_build_thisn_access_method (type);
9605 if (type == DECL_CONTEXT (md))
9606 {
9607 ta = build_access_to_thisn (ctx, type, 0);
9608 break;
9609 }
9610 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9611 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9612 }
9613 }
9614 ta = java_complete_tree (ta);
9615 }
9616
9617 /* We might have to use an access method to get to MD. We can
9618 break the method access rule as far as we're not generating
9619 bytecode */
9620 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9621 {
9622 md = build_outer_method_access_method (md);
9623 to_return = 1;
9624 }
9625
9626 *mdecl = md;
9627 *this_arg = ta;
9628
9629 /* Returnin a non zero value indicates we were doing a non static
9630 method invokation that is now a static invocation. It will have
9631 callee displace `this' to insert it in the regular argument
9632 list. */
9633 return (non_static_context && to_return);
9634}
9635
e04a16fb
AG
9636/* Patch an invoke expression METHOD and ARGS, based on its invocation
9637 mode. */
9638
9639static tree
89e09b9a 9640patch_invoke (patch, method, args)
e04a16fb 9641 tree patch, method, args;
e04a16fb
AG
9642{
9643 tree dtable, func;
0a2138e2 9644 tree original_call, t, ta;
e04a16fb 9645
5e942c50
APB
9646 /* Last step for args: convert build-in types. If we're dealing with
9647 a new TYPE() type call, the first argument to the constructor
9648 isn't found in the incomming argument list, but delivered by
9649 `new' */
9650 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9651 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9652 t = TREE_CHAIN (t);
ac825856
APB
9653 for (ta = args; t != end_params_node && ta;
9654 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
9655 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9656 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9657 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
9658
9659 /* Resolve unresolved returned type isses */
9660 t = TREE_TYPE (TREE_TYPE (method));
9661 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9662 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 9663
e8fc7396 9664 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9665 func = method;
9666 else
e04a16fb 9667 {
15fdcfe9 9668 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 9669 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
9670 {
9671 case INVOKE_VIRTUAL:
9672 dtable = invoke_build_dtable (0, args);
9673 func = build_invokevirtual (dtable, method);
9674 break;
b9f7e36c 9675
15fdcfe9
PB
9676 case INVOKE_SUPER:
9677 case INVOKE_STATIC:
9678 func = build_known_method_ref (method, TREE_TYPE (method),
9679 DECL_CONTEXT (method),
9680 signature, args);
9681 break;
e04a16fb 9682
15fdcfe9
PB
9683 case INVOKE_INTERFACE:
9684 dtable = invoke_build_dtable (1, args);
173f556c 9685 func = build_invokeinterface (dtable, method);
15fdcfe9 9686 break;
5e942c50 9687
15fdcfe9 9688 default:
89e09b9a 9689 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
9690 }
9691
9692 /* Ensure self_type is initialized, (invokestatic). FIXME */
9693 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
9694 }
9695
e04a16fb
AG
9696 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
9697 TREE_OPERAND (patch, 0) = func;
9698 TREE_OPERAND (patch, 1) = args;
9699 original_call = patch;
9700
22eed1e6
APB
9701 /* We're processing a `new TYPE ()' form. New is called an its
9702 returned value is the first argument to the constructor. We build
9703 a COMPOUND_EXPR and use saved expression so that the overall NEW
9704 expression value is a pointer to a newly created and initialized
9705 class. */
9706 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
9707 {
9708 tree class = DECL_CONTEXT (method);
9709 tree c1, saved_new, size, new;
e8fc7396 9710 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9711 {
9712 TREE_TYPE (patch) = build_pointer_type (class);
9713 return patch;
9714 }
e04a16fb
AG
9715 if (!TYPE_SIZE (class))
9716 safe_layout_class (class);
9717 size = size_in_bytes (class);
9718 new = build (CALL_EXPR, promote_type (class),
9719 build_address_of (alloc_object_node),
9720 tree_cons (NULL_TREE, build_class_ref (class),
9721 build_tree_list (NULL_TREE,
9722 size_in_bytes (class))),
9723 NULL_TREE);
9724 saved_new = save_expr (new);
9725 c1 = build_tree_list (NULL_TREE, saved_new);
9726 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
9727 TREE_OPERAND (original_call, 1) = c1;
9728 TREE_SET_CODE (original_call, CALL_EXPR);
9729 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
9730 }
9731 return patch;
9732}
9733
9734static int
9735invocation_mode (method, super)
9736 tree method;
9737 int super;
9738{
9739 int access = get_access_flags_from_decl (method);
9740
22eed1e6
APB
9741 if (super)
9742 return INVOKE_SUPER;
9743
82371d41 9744 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
e04a16fb
AG
9745 return INVOKE_STATIC;
9746
9747 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
9748 return INVOKE_STATIC;
9749
e04a16fb
AG
9750 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
9751 return INVOKE_INTERFACE;
9752
9753 if (DECL_CONSTRUCTOR_P (method))
9754 return INVOKE_STATIC;
22eed1e6 9755
e04a16fb
AG
9756 return INVOKE_VIRTUAL;
9757}
9758
b67d701b
PB
9759/* Retrieve a refined list of matching methods. It covers the step
9760 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
9761
9762static tree
9763lookup_method_invoke (lc, cl, class, name, arg_list)
9764 int lc;
9765 tree cl;
9766 tree class, name, arg_list;
9767{
de4c7b02 9768 tree atl = end_params_node; /* Arg Type List */
c877974e 9769 tree method, signature, list, node;
49f48c71 9770 const char *candidates; /* Used for error report */
b5b8a0e7 9771 char *dup;
e04a16fb 9772
5e942c50 9773 /* Fix the arguments */
e04a16fb
AG
9774 for (node = arg_list; node; node = TREE_CHAIN (node))
9775 {
e3884b71 9776 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 9777 /* Non primitive type may have to be resolved */
e3884b71 9778 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
9779 resolve_and_layout (current_arg, NULL_TREE);
9780 /* And promoted */
b67d701b 9781 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 9782 current_arg = promote_type (current_arg);
5e942c50 9783 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 9784 }
e04a16fb 9785
c2952b01
APB
9786 /* Presto. If we're dealing with an anonymous class and a
9787 constructor call, generate the right constructor now, since we
9788 know the arguments' types. */
9789
9790 if (lc && ANONYMOUS_CLASS_P (class))
9791 craft_constructor (TYPE_NAME (class), atl);
9792
5e942c50
APB
9793 /* Find all candidates and then refine the list, searching for the
9794 most specific method. */
9795 list = find_applicable_accessible_methods_list (lc, class, name, atl);
9796 list = find_most_specific_methods_list (list);
b67d701b
PB
9797 if (list && !TREE_CHAIN (list))
9798 return TREE_VALUE (list);
e04a16fb 9799
b67d701b
PB
9800 /* Issue an error. List candidates if any. Candidates are listed
9801 only if accessible (non accessible methods may end-up here for
9802 the sake of a better error report). */
9803 candidates = NULL;
9804 if (list)
e04a16fb 9805 {
e04a16fb 9806 tree current;
b67d701b 9807 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
9808 for (current = list; current; current = TREE_CHAIN (current))
9809 {
b67d701b
PB
9810 tree cm = TREE_VALUE (current);
9811 char string [4096];
9812 if (!cm || not_accessible_p (class, cm, 0))
9813 continue;
b67d701b 9814 sprintf
22eed1e6
APB
9815 (string, " `%s' in `%s'%s",
9816 get_printable_method_name (cm),
b67d701b
PB
9817 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
9818 (TREE_CHAIN (current) ? "\n" : ""));
9819 obstack_grow (&temporary_obstack, string, strlen (string));
9820 }
9821 obstack_1grow (&temporary_obstack, '\0');
9822 candidates = obstack_finish (&temporary_obstack);
9823 }
9824 /* Issue the error message */
c877974e
APB
9825 method = make_node (FUNCTION_TYPE);
9826 TYPE_ARG_TYPES (method) = atl;
b67d701b 9827 signature = build_java_argument_signature (method);
c63b98cd 9828 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 9829 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 9830 (lc ? "constructor" : "method"),
b5b8a0e7
APB
9831 (lc ? dup : IDENTIFIER_POINTER (name)),
9832 IDENTIFIER_POINTER (signature), dup,
b67d701b 9833 (candidates ? candidates : ""));
b5b8a0e7 9834 free (dup);
b67d701b
PB
9835 return NULL_TREE;
9836}
9837
5e942c50
APB
9838/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
9839 when we're looking for a constructor. */
b67d701b
PB
9840
9841static tree
5e942c50
APB
9842find_applicable_accessible_methods_list (lc, class, name, arglist)
9843 int lc;
b67d701b
PB
9844 tree class, name, arglist;
9845{
165f37bc 9846 static int object_done = 0;
b67d701b
PB
9847 tree list = NULL_TREE, all_list = NULL_TREE;
9848
c2952b01
APB
9849 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
9850 {
9851 load_class (class, 1);
9852 safe_layout_class (class);
9853 }
9854
1982388a 9855 /* Search interfaces */
165f37bc 9856 if (CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 9857 {
e0422ed0 9858 static struct hash_table t, *searched_interfaces = NULL;
de0b553f 9859 static int search_not_done = 0;
1982388a
APB
9860 int i, n;
9861 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
9862
e0422ed0
APB
9863 /* Search in the hash table, otherwise create a new one if
9864 necessary and insert the new entry. */
9865
de0b553f 9866 if (searched_interfaces)
e0422ed0
APB
9867 {
9868 if (hash_lookup (searched_interfaces,
9869 (const hash_table_key) class, FALSE, NULL))
9870 return NULL;
de0b553f 9871 }
e0422ed0
APB
9872 else
9873 {
9874 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
9875 java_hash_compare_tree_node);
9876 searched_interfaces = &t;
9877 }
9878
9879 hash_lookup (searched_interfaces,
9880 (const hash_table_key) class, TRUE, NULL);
de0b553f 9881
165f37bc
APB
9882 search_applicable_methods_list (lc, TYPE_METHODS (class),
9883 name, arglist, &list, &all_list);
1982388a 9884 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 9885 for (i = 1; i < n; i++)
b67d701b 9886 {
de0b553f
APB
9887 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
9888 tree rlist;
9889
de0b553f
APB
9890 search_not_done++;
9891 rlist = find_applicable_accessible_methods_list (lc, t, name,
9892 arglist);
165f37bc 9893 list = chainon (rlist, list);
de0b553f
APB
9894 search_not_done--;
9895 }
9896
9897 /* We're done. Reset the searched interfaces list and finally search
9898 java.lang.Object */
9899 if (!search_not_done)
9900 {
165f37bc
APB
9901 if (!object_done)
9902 search_applicable_methods_list (lc,
9903 TYPE_METHODS (object_type_node),
9904 name, arglist, &list, &all_list);
e0422ed0
APB
9905 hash_table_free (searched_interfaces);
9906 searched_interfaces = NULL;
e04a16fb 9907 }
e04a16fb 9908 }
1982388a
APB
9909 /* Search classes */
9910 else
c2952b01 9911 {
165f37bc
APB
9912 tree sc = class;
9913 int seen_inner_class = 0;
c2952b01
APB
9914 search_applicable_methods_list (lc, TYPE_METHODS (class),
9915 name, arglist, &list, &all_list);
9916
165f37bc
APB
9917 /* We must search all interfaces of this class */
9918 if (!lc)
9919 {
9920 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
9921 int n = TREE_VEC_LENGTH (basetype_vec), i;
9922 object_done = 1;
9923 for (i = 1; i < n; i++)
9924 {
9925 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
9926 tree rlist;
9927 if (t != object_type_node)
9928 rlist = find_applicable_accessible_methods_list (lc, t,
9929 name, arglist);
9930 list = chainon (rlist, list);
9931 }
9932 object_done = 0;
9933 }
9934
c2952b01
APB
9935 /* Search enclosing context of inner classes before looking
9936 ancestors up. */
9937 while (!lc && INNER_CLASS_TYPE_P (class))
9938 {
165f37bc
APB
9939 tree rlist;
9940 seen_inner_class = 1;
c2952b01 9941 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
9942 rlist = find_applicable_accessible_methods_list (lc, class,
9943 name, arglist);
9944 list = chainon (rlist, list);
c2952b01 9945 }
165f37bc
APB
9946
9947 if (!lc && seen_inner_class
9948 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
9949 class = CLASSTYPE_SUPER (sc);
9950 else
9951 class = sc;
9952
9953 for (class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
9954 class; class = CLASSTYPE_SUPER (class))
9955 search_applicable_methods_list (lc, TYPE_METHODS (class),
9956 name, arglist, &list, &all_list);
c2952b01 9957 }
1982388a 9958
b67d701b
PB
9959 /* Either return the list obtained or all selected (but
9960 inaccessible) methods for better error report. */
9961 return (!list ? all_list : list);
9962}
e04a16fb 9963
1982388a
APB
9964/* Effectively search for the approriate method in method */
9965
9966static void
c2952b01 9967search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
9968 int lc;
9969 tree method, name, arglist;
9970 tree *list, *all_list;
9971{
9972 for (; method; method = TREE_CHAIN (method))
9973 {
9974 /* When dealing with constructor, stop here, otherwise search
9975 other classes */
9976 if (lc && !DECL_CONSTRUCTOR_P (method))
9977 continue;
9978 else if (!lc && (DECL_CONSTRUCTOR_P (method)
9979 || (GET_METHOD_NAME (method) != name)))
9980 continue;
9981
9982 if (argument_types_convertible (method, arglist))
9983 {
9984 /* Retain accessible methods only */
9985 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
9986 method, 0))
9987 *list = tree_cons (NULL_TREE, method, *list);
9988 else
9989 /* Also retain all selected method here */
9990 *all_list = tree_cons (NULL_TREE, method, *list);
9991 }
9992 }
9993}
9994
b67d701b
PB
9995/* 15.11.2.2 Choose the Most Specific Method */
9996
9997static tree
9998find_most_specific_methods_list (list)
9999 tree list;
10000{
10001 int max = 0;
10002 tree current, new_list = NULL_TREE;
10003 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10004 {
b67d701b
PB
10005 tree method;
10006 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10007
10008 for (method = list; method; method = TREE_CHAIN (method))
10009 {
10010 /* Don't test a method against itself */
10011 if (method == current)
10012 continue;
10013
10014 /* Compare arguments and location where method where declared */
10015 if (argument_types_convertible (TREE_VALUE (method),
10016 TREE_VALUE (current))
10017 && valid_method_invocation_conversion_p
10018 (DECL_CONTEXT (TREE_VALUE (method)),
10019 DECL_CONTEXT (TREE_VALUE (current))))
10020 {
10021 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
10022 max = (v > max ? v : max);
10023 }
10024 }
e04a16fb
AG
10025 }
10026
b67d701b
PB
10027 /* Review the list and select the maximally specific methods */
10028 for (current = list; current; current = TREE_CHAIN (current))
10029 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10030 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10031
165f37bc
APB
10032 /* If we have several and they're all abstract, just pick the
10033 closest one. */
10034
10035 if (new_list && TREE_CHAIN (new_list))
10036 {
10037 tree c;
10038 for (c = new_list; c && METHOD_ABSTRACT (TREE_VALUE (c));
10039 c = TREE_CHAIN (c))
10040 ;
10041 if (!c)
10042 {
10043 new_list = nreverse (new_list);
10044 TREE_CHAIN (new_list) = NULL_TREE;
10045 }
10046 }
10047
b67d701b
PB
10048 /* If we can't find one, lower expectations and try to gather multiple
10049 maximally specific methods */
165f37bc 10050 while (!new_list && max)
b67d701b
PB
10051 {
10052 while (--max > 0)
10053 {
10054 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10055 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10056 }
b67d701b
PB
10057 }
10058
10059 return new_list;
e04a16fb
AG
10060}
10061
b67d701b
PB
10062/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10063 converted by method invocation conversion (5.3) to the type of the
10064 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10065 to change less often than M1. */
e04a16fb 10066
b67d701b
PB
10067static int
10068argument_types_convertible (m1, m2_or_arglist)
10069 tree m1, m2_or_arglist;
e04a16fb 10070{
b67d701b
PB
10071 static tree m2_arg_value = NULL_TREE;
10072 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10073
b67d701b 10074 register tree m1_arg, m2_arg;
e04a16fb 10075
c2952b01 10076 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10077
b67d701b
PB
10078 if (m2_arg_value == m2_or_arglist)
10079 m2_arg = m2_arg_cache;
10080 else
10081 {
10082 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10083 argument types */
10084 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10085 {
10086 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10087 if (!METHOD_STATIC (m2_or_arglist))
10088 m2_arg = TREE_CHAIN (m2_arg);
10089 }
10090 else
10091 m2_arg = m2_or_arglist;
e04a16fb 10092
b67d701b
PB
10093 m2_arg_value = m2_or_arglist;
10094 m2_arg_cache = m2_arg;
10095 }
e04a16fb 10096
de4c7b02 10097 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10098 {
c877974e 10099 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10100 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10101 TREE_VALUE (m2_arg)))
10102 break;
10103 m1_arg = TREE_CHAIN (m1_arg);
10104 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10105 }
de4c7b02 10106 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10107}
10108
10109/* Qualification routines */
10110
10111static void
10112qualify_ambiguous_name (id)
10113 tree id;
10114{
cd531a2e
KG
10115 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10116 saved_current_class;
d8fccff5 10117 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10118 int code;
e04a16fb
AG
10119
10120 /* We first qualify the first element, then derive qualification of
10121 others based on the first one. If the first element is qualified
10122 by a resolution (field or type), this resolution is stored in the
10123 QUAL_RESOLUTION of the qual element being examined. We need to
10124 save the current_class since the use of SUPER might change the
10125 its value. */
10126 saved_current_class = current_class;
10127 qual = EXPR_WFL_QUALIFICATION (id);
10128 do {
10129
10130 /* Simple qualified expression feature a qual_wfl that is a
10131 WFL. Expression derived from a primary feature more complicated
10132 things like a CALL_EXPR. Expression from primary need to be
10133 worked out to extract the part on which the qualification will
10134 take place. */
10135 qual_wfl = QUAL_WFL (qual);
10136 switch (TREE_CODE (qual_wfl))
10137 {
10138 case CALL_EXPR:
10139 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10140 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10141 {
10142 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10143 qual_wfl = QUAL_WFL (qual);
10144 }
10145 break;
d8fccff5 10146 case NEW_ARRAY_EXPR:
c2952b01 10147 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10148 qual = TREE_CHAIN (qual);
1a6d4fb7 10149 again = new_array_found = 1;
d8fccff5 10150 continue;
b67d701b 10151 case NEW_CLASS_EXPR:
e04a16fb 10152 case CONVERT_EXPR:
e04a16fb
AG
10153 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10154 break;
c583dd46
APB
10155 case ARRAY_REF:
10156 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10157 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10158 break;
8576f094
APB
10159 case STRING_CST:
10160 qual = TREE_CHAIN (qual);
10161 qual_wfl = QUAL_WFL (qual);
10162 break;
165f37bc
APB
10163 case CLASS_LITERAL:
10164 qual = TREE_CHAIN (qual);
10165 qual_wfl = QUAL_WFL (qual);
10166 break;
0a2138e2
APB
10167 default:
10168 /* Fix for -Wall. Just break doing nothing */
10169 break;
e04a16fb 10170 }
8576f094 10171
e04a16fb
AG
10172 ptr_type = current_class;
10173 again = 0;
8576f094
APB
10174 code = TREE_CODE (qual_wfl);
10175
10176 /* Pos evaluation: non WFL leading expression nodes */
10177 if (code == CONVERT_EXPR
10178 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10179 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10180
cd7c5840
APB
10181 else if (code == INTEGER_CST)
10182 name = qual_wfl;
10183
ac22f9cb 10184 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10185 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10186 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10187
c2952b01
APB
10188 else if (code == TREE_LIST)
10189 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10190
37feda7d
APB
10191 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10192 || code == PLUS_EXPR)
8576f094
APB
10193 {
10194 qual = TREE_CHAIN (qual);
10195 qual_wfl = QUAL_WFL (qual);
10196 again = 1;
10197 }
10198 else
f441f671
APB
10199 {
10200 name = EXPR_WFL_NODE (qual_wfl);
10201 if (!name)
10202 {
10203 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10204 again = 1;
10205 }
10206 }
10207
e04a16fb
AG
10208 /* If we have a THIS (from a primary), we set the context accordingly */
10209 if (name == this_identifier_node)
10210 {
10211 qual = TREE_CHAIN (qual);
10212 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10213 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10214 again = 1;
10215 else
10216 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10217 this_found = 1;
10218 }
10219 /* If we have a SUPER, we set the context accordingly */
10220 if (name == super_identifier_node)
10221 {
10222 current_class = CLASSTYPE_SUPER (ptr_type);
10223 /* Check that there is such a thing as a super class. If not,
10224 return. The error will be caught later on, during the
10225 resolution */
10226 if (!current_class)
10227 {
10228 current_class = saved_current_class;
10229 return;
10230 }
10231 qual = TREE_CHAIN (qual);
10232 /* Do one more interation to set things up */
10233 super_found = again = 1;
10234 }
10235 } while (again);
10236
10237 /* If name appears within the scope of a location variable
10238 declaration or parameter declaration, then it is an expression
10239 name. We don't carry this test out if we're in the context of the
10240 use of SUPER or THIS */
cd7c5840
APB
10241 if (!this_found && !super_found
10242 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10243 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10244 {
10245 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10246 QUAL_RESOLUTION (qual) = decl;
10247 }
10248
10249 /* If within the class/interface NAME was found to be used there
10250 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10251 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10252 address length, it is OK. */
10253 else if ((decl = lookup_field_wrapper (ptr_type, name))
10254 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10255 {
10256 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10257 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10258 }
10259
1a6d4fb7 10260 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10261 - NAME is a class/interface declared within the compilation
10262 unit containing NAME,
10263 - NAME is imported via a single-type-import declaration,
10264 - NAME is declared in an another compilation unit of the package
10265 of the compilation unit containing NAME,
10266 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10267 of the compilation unit containing NAME.
10268 - NAME is actually a STRING_CST. */
cd7c5840
APB
10269 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10270 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10271 {
10272 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10273 QUAL_RESOLUTION (qual) = decl;
10274 }
10275
10276 /* Method call are expression name */
9bbc7d9f 10277 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10278 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10279 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10280 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10281
10282 /* Check here that NAME isn't declared by more than one
10283 type-import-on-demand declaration of the compilation unit
10284 containing NAME. FIXME */
10285
10286 /* Otherwise, NAME is reclassified as a package name */
10287 else
10288 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10289
10290 /* Propagate the qualification accross other components of the
10291 qualified name */
10292 for (qual = TREE_CHAIN (qual); qual;
10293 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10294 {
10295 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10296 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10297 else
10298 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10299 }
10300
10301 /* Store the global qualification for the ambiguous part of ID back
10302 into ID fields */
10303 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10304 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10305 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10306 RESOLVE_TYPE_NAME_P (id) = 1;
10307 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10308 RESOLVE_PACKAGE_NAME_P (id) = 1;
10309
10310 /* Restore the current class */
10311 current_class = saved_current_class;
10312}
10313
10314static int
10315breakdown_qualified (left, right, source)
10316 tree *left, *right, source;
10317{
10318 char *p = IDENTIFIER_POINTER (source), *base;
10319 int l = IDENTIFIER_LENGTH (source);
10320
10321 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10322 base = p;
10323 p += (l-1);
10324 while (*p != '.' && p != base)
10325 p--;
10326
10327 /* We didn't find a '.'. Return an error */
10328 if (p == base)
10329 return 1;
10330
10331 *p = '\0';
10332 if (right)
10333 *right = get_identifier (p+1);
10334 *left = get_identifier (IDENTIFIER_POINTER (source));
10335 *p = '.';
10336
10337 return 0;
10338}
10339
e04a16fb 10340/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10341 local variable decls if present.
10342 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10343
10344static tree
10345java_complete_tree (node)
10346 tree node;
5b09b33e
PB
10347{
10348 node = java_complete_lhs (node);
10349 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7f10c2e2
APB
10350 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE
10351 && !flag_emit_xref)
5b09b33e
PB
10352 {
10353 tree value = DECL_INITIAL (node);
10354 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10355 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10356 value = fold_constant_for_init (value, node);
100f7cd8 10357 pop_obstacks ();
5b09b33e
PB
10358 DECL_INITIAL (node) = value;
10359 if (value != NULL_TREE)
c2952b01
APB
10360 {
10361 /* fold_constant_for_init sometimes widen the original type
10362 of the constant (i.e. byte to int.) It's not desirable,
10363 especially if NODE is a function argument. */
10364 if (TREE_CODE (value) == INTEGER_CST
10365 && TREE_TYPE (node) != TREE_TYPE (value))
10366 return convert (TREE_TYPE (node), value);
10367 else
10368 return value;
10369 }
5b09b33e
PB
10370 }
10371 return node;
10372}
10373
2aa11e97
APB
10374static tree
10375java_stabilize_reference (node)
10376 tree node;
10377{
10378 if (TREE_CODE (node) == COMPOUND_EXPR)
10379 {
10380 tree op0 = TREE_OPERAND (node, 0);
10381 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10382 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10383 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10384 return node;
10385 }
5cbdba64 10386 return stabilize_reference (node);
2aa11e97
APB
10387}
10388
5b09b33e
PB
10389/* Patch tree nodes in a function body. When a BLOCK is found, push
10390 local variable decls if present.
10391 Same as java_complete_tree, but does not resolve static finals to values. */
10392
10393static tree
10394java_complete_lhs (node)
10395 tree node;
e04a16fb 10396{
22eed1e6 10397 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10398 int flag;
e04a16fb
AG
10399
10400 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10401 worked out. */
e04a16fb
AG
10402 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10403 return node;
10404
10405 /* The switch block implements cases processing container nodes
10406 first. Contained nodes are always written back. Leaves come
10407 next and return a value. */
10408 switch (TREE_CODE (node))
10409 {
10410 case BLOCK:
10411
10412 /* 1- Block section.
10413 Set the local values on decl names so we can identify them
10414 faster when they're referenced. At that stage, identifiers
10415 are legal so we don't check for declaration errors. */
10416 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10417 {
10418 DECL_CONTEXT (cn) = current_function_decl;
10419 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10420 }
15fdcfe9
PB
10421 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10422 CAN_COMPLETE_NORMALLY (node) = 1;
10423 else
e04a16fb 10424 {
15fdcfe9
PB
10425 tree stmt = BLOCK_EXPR_BODY (node);
10426 tree *ptr;
10427 int error_seen = 0;
10428 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10429 {
c877974e
APB
10430 /* Re-order from (((A; B); C); ...; Z) to
10431 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10432 This makes it easier to scan the statements left-to-right
10433 without using recursion (which might overflow the stack
10434 if the block has many statements. */
10435 for (;;)
10436 {
10437 tree left = TREE_OPERAND (stmt, 0);
10438 if (TREE_CODE (left) != COMPOUND_EXPR)
10439 break;
10440 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10441 TREE_OPERAND (left, 1) = stmt;
10442 stmt = left;
10443 }
10444 BLOCK_EXPR_BODY (node) = stmt;
10445 }
10446
c877974e
APB
10447 /* Now do the actual complete, without deep recursion for
10448 long blocks. */
15fdcfe9 10449 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10450 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10451 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10452 {
10453 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10454 tree *next = &TREE_OPERAND (*ptr, 1);
10455 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10456 if (cur == empty_stmt_node)
10457 {
10458 /* Optimization; makes it easier to detect empty bodies.
10459 Most useful for <clinit> with all-constant initializer. */
10460 *ptr = *next;
10461 continue;
10462 }
15fdcfe9
PB
10463 if (TREE_CODE (cur) == ERROR_MARK)
10464 error_seen++;
10465 else if (! CAN_COMPLETE_NORMALLY (cur))
10466 {
10467 wfl_op2 = *next;
10468 for (;;)
10469 {
10470 if (TREE_CODE (wfl_op2) == BLOCK)
10471 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10472 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10473 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10474 else
10475 break;
10476 }
10477 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10478 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10479 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10480 }
10481 ptr = next;
10482 }
10483 *ptr = java_complete_tree (*ptr);
10484
10485 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10486 return error_mark_node;
15fdcfe9 10487 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10488 }
10489 /* Turn local bindings to null */
10490 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10491 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10492
10493 TREE_TYPE (node) = void_type_node;
10494 break;
10495
10496 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10497
b9f7e36c
APB
10498 case THROW_EXPR:
10499 wfl_op1 = TREE_OPERAND (node, 0);
10500 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10501 /* 14.19 A throw statement cannot complete normally. */
10502 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10503 return patch_throw_statement (node, wfl_op1);
10504
10505 case SYNCHRONIZED_EXPR:
10506 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10507 return patch_synchronized_statement (node, wfl_op1);
10508
b67d701b
PB
10509 case TRY_EXPR:
10510 return patch_try_statement (node);
10511
a7d8d81f
PB
10512 case TRY_FINALLY_EXPR:
10513 COMPLETE_CHECK_OP_0 (node);
10514 COMPLETE_CHECK_OP_1 (node);
10515 CAN_COMPLETE_NORMALLY (node)
10516 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10517 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10518 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10519 return node;
10520
5a005d9e
PB
10521 case CLEANUP_POINT_EXPR:
10522 COMPLETE_CHECK_OP_0 (node);
10523 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10524 CAN_COMPLETE_NORMALLY (node) =
10525 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10526 return node;
10527
10528 case WITH_CLEANUP_EXPR:
10529 COMPLETE_CHECK_OP_0 (node);
10530 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10531 CAN_COMPLETE_NORMALLY (node) =
10532 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10533 TREE_TYPE (node) = void_type_node;
10534 return node;
10535
e04a16fb
AG
10536 case LABELED_BLOCK_EXPR:
10537 PUSH_LABELED_BLOCK (node);
10538 if (LABELED_BLOCK_BODY (node))
10539 COMPLETE_CHECK_OP_1 (node);
10540 TREE_TYPE (node) = void_type_node;
10541 POP_LABELED_BLOCK ();
1fb89a4d
APB
10542
10543 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10544 {
10545 LABELED_BLOCK_BODY (node) = NULL_TREE;
10546 CAN_COMPLETE_NORMALLY (node) = 1;
10547 }
1fb89a4d 10548 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10549 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10550 return node;
10551
10552 case EXIT_BLOCK_EXPR:
10553 /* We don't complete operand 1, because it's the return value of
10554 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10555 return patch_bc_statement (node);
10556
15fdcfe9
PB
10557 case CASE_EXPR:
10558 cn = java_complete_tree (TREE_OPERAND (node, 0));
10559 if (cn == error_mark_node)
10560 return cn;
10561
8576f094
APB
10562 /* First, the case expression must be constant. Values of final
10563 fields are accepted. */
15fdcfe9 10564 cn = fold (cn);
8576f094
APB
10565 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10566 && JDECL_P (TREE_OPERAND (cn, 1))
10567 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10568 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
10569 {
10570 push_obstacks (&permanent_obstack, &permanent_obstack);
10571 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10572 TREE_OPERAND (cn, 1));
10573 pop_obstacks ();
10574 }
15fdcfe9 10575
ce6e9147 10576 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
10577 {
10578 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10579 parse_error_context (node, "Constant expression required");
10580 return error_mark_node;
10581 }
10582
10583 nn = ctxp->current_loop;
10584
10585 /* It must be assignable to the type of the switch expression. */
c877974e
APB
10586 if (!try_builtin_assignconv (NULL_TREE,
10587 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
10588 {
10589 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10590 parse_error_context
10591 (wfl_operator,
10592 "Incompatible type for case. Can't convert `%s' to `int'",
10593 lang_printable_name (TREE_TYPE (cn), 0));
10594 return error_mark_node;
10595 }
10596
10597 cn = fold (convert (int_type_node, cn));
10598
10599 /* Multiple instance of a case label bearing the same
10600 value is checked during code generation. The case
10601 expression is allright so far. */
10602 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 10603 TREE_TYPE (node) = void_type_node;
15fdcfe9 10604 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 10605 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10606 break;
10607
10608 case DEFAULT_EXPR:
10609 nn = ctxp->current_loop;
10610 /* Only one default label is allowed per switch statement */
10611 if (SWITCH_HAS_DEFAULT (nn))
10612 {
10613 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10614 parse_error_context (wfl_operator,
10615 "Duplicate case label: `default'");
10616 return error_mark_node;
10617 }
10618 else
10619 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 10620 TREE_TYPE (node) = void_type_node;
10100cc7 10621 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10622 CAN_COMPLETE_NORMALLY (node) = 1;
10623 break;
10624
b67d701b 10625 case SWITCH_EXPR:
e04a16fb
AG
10626 case LOOP_EXPR:
10627 PUSH_LOOP (node);
10628 /* Check whether the loop was enclosed in a labeled
10629 statement. If not, create one, insert the loop in it and
10630 return the node */
10631 nn = patch_loop_statement (node);
b67d701b 10632
e04a16fb 10633 /* Anyways, walk the body of the loop */
b67d701b
PB
10634 if (TREE_CODE (node) == LOOP_EXPR)
10635 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10636 /* Switch statement: walk the switch expression and the cases */
10637 else
10638 node = patch_switch_statement (node);
10639
e04a16fb 10640 if (TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
10641 nn = error_mark_node;
10642 else
15fdcfe9 10643 {
b635eb2f
PB
10644 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
10645 /* If we returned something different, that's because we
10646 inserted a label. Pop the label too. */
10647 if (nn != node)
10648 {
10649 if (CAN_COMPLETE_NORMALLY (node))
10650 CAN_COMPLETE_NORMALLY (nn) = 1;
10651 POP_LABELED_BLOCK ();
10652 }
15fdcfe9 10653 }
e04a16fb
AG
10654 POP_LOOP ();
10655 return nn;
10656
10657 case EXIT_EXPR:
10658 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10659 return patch_exit_expr (node);
10660
10661 case COND_EXPR:
10662 /* Condition */
10663 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10664 if (TREE_OPERAND (node, 0) == error_mark_node)
10665 return error_mark_node;
10666 /* then-else branches */
10667 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10668 if (TREE_OPERAND (node, 1) == error_mark_node)
10669 return error_mark_node;
10670 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
10671 if (TREE_OPERAND (node, 2) == error_mark_node)
10672 return error_mark_node;
10673 return patch_if_else_statement (node);
10674 break;
10675
22eed1e6
APB
10676 case CONDITIONAL_EXPR:
10677 /* Condition */
10678 wfl_op1 = TREE_OPERAND (node, 0);
10679 COMPLETE_CHECK_OP_0 (node);
10680 wfl_op2 = TREE_OPERAND (node, 1);
10681 COMPLETE_CHECK_OP_1 (node);
10682 wfl_op3 = TREE_OPERAND (node, 2);
10683 COMPLETE_CHECK_OP_2 (node);
10684 return patch_conditional_expr (node, wfl_op1, wfl_op2);
10685
e04a16fb
AG
10686 /* 3- Expression section */
10687 case COMPOUND_EXPR:
15fdcfe9 10688 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
10689 TREE_OPERAND (node, 0) = nn =
10690 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
10691 if (wfl_op2 == empty_stmt_node)
10692 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
10693 else
15fdcfe9 10694 {
dc0b3eff 10695 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 10696 {
dc0b3eff
PB
10697 /* An unreachable condition in a do-while statement
10698 is *not* (technically) an unreachable statement. */
10699 nn = wfl_op2;
10700 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
10701 nn = EXPR_WFL_NODE (nn);
10702 if (TREE_CODE (nn) != EXIT_EXPR)
10703 {
10704 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
10705 parse_error_context (wfl_operator, "Unreachable statement");
10706 }
bccaf73a 10707 }
dc0b3eff
PB
10708 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10709 if (TREE_OPERAND (node, 1) == error_mark_node)
10710 return error_mark_node;
10711 CAN_COMPLETE_NORMALLY (node)
10712 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 10713 }
e04a16fb
AG
10714 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
10715 break;
10716
10717 case RETURN_EXPR:
15fdcfe9 10718 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
10719 return patch_return (node);
10720
10721 case EXPR_WITH_FILE_LOCATION:
10722 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
10723 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 10724 {
5423609c 10725 tree wfl = node;
15fdcfe9 10726 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
10727 if (node == error_mark_node)
10728 return node;
5423609c
APB
10729 /* Keep line number information somewhere were it doesn't
10730 disrupt the completion process. */
2c56429a 10731 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
10732 {
10733 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
10734 TREE_OPERAND (node, 1) = wfl;
10735 }
15fdcfe9
PB
10736 CAN_COMPLETE_NORMALLY (node) = 1;
10737 }
e04a16fb
AG
10738 else
10739 {
5b09b33e
PB
10740 tree body;
10741 int save_lineno = lineno;
10742 lineno = EXPR_WFL_LINENO (node);
10743 body = java_complete_tree (EXPR_WFL_NODE (node));
10744 lineno = save_lineno;
15fdcfe9 10745 EXPR_WFL_NODE (node) = body;
dc0b3eff 10746 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 10747 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
10748 if (body == empty_stmt_node)
10749 {
10750 /* Optimization; makes it easier to detect empty bodies. */
10751 return body;
10752 }
dc0b3eff 10753 if (body == error_mark_node)
e04a16fb
AG
10754 {
10755 /* Its important for the evaluation of assignment that
10756 this mark on the TREE_TYPE is propagated. */
10757 TREE_TYPE (node) = error_mark_node;
10758 return error_mark_node;
10759 }
10760 else
10761 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 10762
e04a16fb
AG
10763 }
10764 break;
10765
b67d701b 10766 case NEW_ARRAY_EXPR:
e04a16fb
AG
10767 /* Patch all the dimensions */
10768 flag = 0;
10769 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
10770 {
10771 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
10772 tree dim = convert (int_type_node,
10773 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
10774 if (dim == error_mark_node)
10775 {
10776 flag = 1;
10777 continue;
10778 }
10779 else
10780 {
b9f7e36c 10781 TREE_VALUE (cn) = dim;
e04a16fb
AG
10782 /* Setup the location of the current dimension, for
10783 later error report. */
10784 TREE_PURPOSE (cn) =
10785 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
10786 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
10787 }
10788 }
10789 /* They complete the array creation expression, if no errors
10790 were found. */
15fdcfe9 10791 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
10792 return (flag ? error_mark_node
10793 : force_evaluation_order (patch_newarray (node)));
e04a16fb 10794
c2952b01
APB
10795 case NEW_ANONYMOUS_ARRAY_EXPR:
10796 /* Create the array type if necessary. */
10797 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
10798 {
10799 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
10800 if (!(type = resolve_type_during_patch (type)))
10801 return error_mark_node;
10802 type = build_array_from_name (type, NULL_TREE,
10803 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
10804 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
10805 }
10806 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
10807 ANONYMOUS_ARRAY_INITIALIZER (node));
10808 if (node == error_mark_node)
10809 return error_mark_node;
10810 CAN_COMPLETE_NORMALLY (node) = 1;
10811 return node;
10812
b67d701b 10813 case NEW_CLASS_EXPR:
e04a16fb 10814 case CALL_EXPR:
b67d701b 10815 /* Complete function's argument(s) first */
e04a16fb
AG
10816 if (complete_function_arguments (node))
10817 return error_mark_node;
10818 else
b9f7e36c 10819 {
22eed1e6
APB
10820 tree decl, wfl = TREE_OPERAND (node, 0);
10821 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
10822
c877974e 10823 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 10824 NULL_TREE, 0, &decl);
c877974e
APB
10825 if (node == error_mark_node)
10826 return error_mark_node;
10827
10828 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
10829 /* If we call this(...), register signature and positions */
10830 if (in_this)
10831 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
10832 tree_cons (wfl, decl,
10833 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 10834 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 10835 return force_evaluation_order (node);
b9f7e36c 10836 }
e04a16fb
AG
10837
10838 case MODIFY_EXPR:
10839 /* Save potential wfls */
10840 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 10841 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 10842
cd9643f7
PB
10843 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
10844 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
10845 && DECL_INITIAL (nn) != NULL_TREE)
10846 {
100f7cd8
APB
10847 tree value;
10848
10849 push_obstacks (&permanent_obstack, &permanent_obstack);
10850 value = fold_constant_for_init (nn, nn);
10851 pop_obstacks ();
c2952b01 10852
cd9643f7
PB
10853 if (value != NULL_TREE)
10854 {
10855 tree type = TREE_TYPE (value);
c2952b01
APB
10856 if (JPRIMITIVE_TYPE_P (type) ||
10857 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
10858 return empty_stmt_node;
10859 }
10860 DECL_INITIAL (nn) = NULL_TREE;
10861 }
e04a16fb 10862 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 10863
e04a16fb
AG
10864 if (TREE_OPERAND (node, 0) == error_mark_node)
10865 return error_mark_node;
10866
5cbdba64
APB
10867 flag = COMPOUND_ASSIGN_P (wfl_op2);
10868 if (flag)
e04a16fb 10869 {
c2952b01
APB
10870 /* This might break when accessing outer field from inner
10871 class. TESTME, FIXME */
2aa11e97 10872 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
10873
10874 /* Hand stablize the lhs on both places */
e04a16fb 10875 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
10876 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
10877 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 10878
5cbdba64 10879 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
10880 /* Now complete the RHS. We write it back later on. */
10881 nn = java_complete_tree (TREE_OPERAND (node, 1));
10882
642f15d1
APB
10883 if ((cn = patch_string (nn)))
10884 nn = cn;
10885
2aa11e97
APB
10886 /* The last part of the rewrite for E1 op= E2 is to have
10887 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
10888 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
10889 TREE_TYPE (lvalue), nn));
5cbdba64
APB
10890
10891 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
10892 }
10893
f8976021 10894 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
10895 function to complete this RHS. Note that a NEW_ARRAY_INIT
10896 might have been already fully expanded if created as a result
10897 of processing an anonymous array initializer. We avoid doing
10898 the operation twice by testing whether the node already bears
10899 a type. */
10900 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 10901 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 10902 TREE_OPERAND (node, 1));
2aa11e97 10903 /* Otherwise we simply complete the RHS */
f8976021
APB
10904 else
10905 nn = java_complete_tree (TREE_OPERAND (node, 1));
10906
e04a16fb 10907 if (nn == error_mark_node)
c0d87ff6 10908 return error_mark_node;
2aa11e97
APB
10909
10910 /* Write back the RHS as we evaluated it. */
e04a16fb 10911 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
10912
10913 /* In case we're handling = with a String as a RHS, we need to
10914 produce a String out of the RHS (it might still be a
10915 STRING_CST or a StringBuffer at this stage */
10916 if ((nn = patch_string (TREE_OPERAND (node, 1))))
10917 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
10918
10919 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
10920 TREE_OPERAND (node, 1))))
10921 {
10922 /* We return error_mark_node if outer_field_access_fix
10923 detects we write into a final. */
10924 if (nn == error_mark_node)
10925 return error_mark_node;
10926 node = nn;
10927 }
10928 else
10929 {
10930 node = patch_assignment (node, wfl_op1, wfl_op2);
10931 /* Reorganize the tree if necessary. */
10932 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
10933 || JSTRING_P (TREE_TYPE (node))))
10934 node = java_refold (node);
10935 }
10936
15fdcfe9
PB
10937 CAN_COMPLETE_NORMALLY (node) = 1;
10938 return node;
e04a16fb
AG
10939
10940 case MULT_EXPR:
10941 case PLUS_EXPR:
10942 case MINUS_EXPR:
10943 case LSHIFT_EXPR:
10944 case RSHIFT_EXPR:
10945 case URSHIFT_EXPR:
10946 case BIT_AND_EXPR:
10947 case BIT_XOR_EXPR:
10948 case BIT_IOR_EXPR:
10949 case TRUNC_MOD_EXPR:
c2952b01 10950 case TRUNC_DIV_EXPR:
e04a16fb
AG
10951 case RDIV_EXPR:
10952 case TRUTH_ANDIF_EXPR:
10953 case TRUTH_ORIF_EXPR:
10954 case EQ_EXPR:
10955 case NE_EXPR:
10956 case GT_EXPR:
10957 case GE_EXPR:
10958 case LT_EXPR:
10959 case LE_EXPR:
10960 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
10961 knows how to handle those cases. */
10962 wfl_op1 = TREE_OPERAND (node, 0);
10963 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 10964
15fdcfe9 10965 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
10966 /* Don't complete string nodes if dealing with the PLUS operand. */
10967 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
10968 {
10969 nn = java_complete_tree (wfl_op1);
10970 if (nn == error_mark_node)
10971 return error_mark_node;
48a840d9 10972
2aa11e97
APB
10973 TREE_OPERAND (node, 0) = nn;
10974 }
b67d701b 10975 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
10976 {
10977 nn = java_complete_tree (wfl_op2);
10978 if (nn == error_mark_node)
10979 return error_mark_node;
48a840d9 10980
2aa11e97
APB
10981 TREE_OPERAND (node, 1) = nn;
10982 }
dc0b3eff 10983 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 10984
5e942c50
APB
10985 case INSTANCEOF_EXPR:
10986 wfl_op1 = TREE_OPERAND (node, 0);
10987 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
10988 if (flag_emit_xref)
10989 {
10990 TREE_TYPE (node) = boolean_type_node;
10991 return node;
10992 }
5e942c50
APB
10993 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
10994
b67d701b 10995 case UNARY_PLUS_EXPR:
e04a16fb
AG
10996 case NEGATE_EXPR:
10997 case TRUTH_NOT_EXPR:
10998 case BIT_NOT_EXPR:
10999 case PREDECREMENT_EXPR:
11000 case PREINCREMENT_EXPR:
11001 case POSTDECREMENT_EXPR:
11002 case POSTINCREMENT_EXPR:
11003 case CONVERT_EXPR:
11004 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11005 how to handle those cases. */
11006 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11007 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11008 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11009 if (TREE_OPERAND (node, 0) == error_mark_node)
11010 return error_mark_node;
4a5f66c3
APB
11011 node = patch_unaryop (node, wfl_op1);
11012 CAN_COMPLETE_NORMALLY (node) = 1;
11013 break;
e04a16fb
AG
11014
11015 case ARRAY_REF:
11016 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11017 how to handle those cases. */
11018 wfl_op1 = TREE_OPERAND (node, 0);
11019 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11020 if (TREE_OPERAND (node, 0) == error_mark_node)
11021 return error_mark_node;
7f1d4866 11022 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11023 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11024 /* The same applies to wfl_op2 */
11025 wfl_op2 = TREE_OPERAND (node, 1);
11026 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11027 if (TREE_OPERAND (node, 1) == error_mark_node)
11028 return error_mark_node;
7f1d4866 11029 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11030 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11031 return patch_array_ref (node);
e04a16fb 11032
63a212ed
PB
11033 case RECORD_TYPE:
11034 return node;;
11035
11036 case COMPONENT_REF:
11037 /* The first step in the re-write of qualified name handling. FIXME.
11038 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11039 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11040 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11041 {
11042 tree name = TREE_OPERAND (node, 1);
11043 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11044 if (field == NULL_TREE)
11045 {
11046 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11047 return error_mark_node;
11048 }
11049 if (! FIELD_STATIC (field))
11050 {
11051 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11052 return error_mark_node;
11053 }
11054 return field;
11055 }
11056 else
11057 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11058 break;
9bbc7d9f 11059
b67d701b 11060 case THIS_EXPR:
e04a16fb
AG
11061 /* Can't use THIS in a static environment */
11062 if (!current_this)
11063 {
11064 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11065 parse_error_context (wfl_operator,
11066 "Keyword `this' used outside allowed context");
e04a16fb
AG
11067 TREE_TYPE (node) = error_mark_node;
11068 return error_mark_node;
11069 }
22eed1e6
APB
11070 if (ctxp->explicit_constructor_p)
11071 {
11072 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11073 parse_error_context
781b0558 11074 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11075 TREE_TYPE (node) = error_mark_node;
11076 return error_mark_node;
11077 }
e04a16fb 11078 return current_this;
c2952b01
APB
11079
11080 case CLASS_LITERAL:
11081 CAN_COMPLETE_NORMALLY (node) = 1;
11082 node = patch_incomplete_class_ref (node);
11083 if (node == error_mark_node)
11084 return error_mark_node;
11085 break;
11086
11087 case INSTANCE_INITIALIZERS_EXPR:
11088 in_instance_initializer++;
11089 node = java_complete_tree (TREE_OPERAND (node, 0));
11090 in_instance_initializer--;
11091 if (node != error_mark_node)
11092 TREE_TYPE (node) = void_type_node;
11093 else
11094 return error_mark_node;
11095 break;
e04a16fb 11096
e04a16fb 11097 default:
15fdcfe9 11098 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11099 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11100 and it's time to turn it into the appropriate String object */
11101 if ((nn = patch_string (node)))
11102 node = nn;
11103 else
11104 fatal ("No case for tree code `%s' - java_complete_tree\n",
11105 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11106 }
11107 return node;
11108}
11109
11110/* Complete function call's argument. Return a non zero value is an
11111 error was found. */
11112
11113static int
11114complete_function_arguments (node)
11115 tree node;
11116{
11117 int flag = 0;
11118 tree cn;
11119
f63991a8 11120 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11121 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11122 {
b67d701b 11123 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11124 parm = java_complete_tree (wfl);
c2952b01 11125
e04a16fb
AG
11126 if (parm == error_mark_node)
11127 {
11128 flag = 1;
11129 continue;
11130 }
b67d701b
PB
11131 /* If have a string literal that we haven't transformed yet or a
11132 crafted string buffer, as a result of use of the the String
11133 `+' operator. Build `parm.toString()' and expand it. */
11134 if ((temp = patch_string (parm)))
b9f7e36c 11135 parm = temp;
5e942c50
APB
11136 /* Inline PRIMTYPE.TYPE read access */
11137 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11138
5e942c50 11139 TREE_VALUE (cn) = parm;
e04a16fb 11140 }
f63991a8 11141 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11142 return flag;
11143}
11144
11145/* Sometimes (for loops and variable initialized during their
11146 declaration), we want to wrap a statement around a WFL and turn it
11147 debugable. */
11148
11149static tree
11150build_debugable_stmt (location, stmt)
11151 int location;
11152 tree stmt;
11153{
11154 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11155 {
11156 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11157 EXPR_WFL_LINECOL (stmt) = location;
11158 }
11159 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11160 return stmt;
11161}
11162
11163static tree
11164build_expr_block (body, decls)
11165 tree body, decls;
11166{
11167 tree node = make_node (BLOCK);
11168 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11169 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11170 if (body)
11171 TREE_TYPE (node) = TREE_TYPE (body);
11172 TREE_SIDE_EFFECTS (node) = 1;
11173 return node;
11174}
11175
b67d701b
PB
11176/* Create a new function block and link it approriately to current
11177 function block chain */
e04a16fb
AG
11178
11179static tree
11180enter_block ()
11181{
b67d701b
PB
11182 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11183}
11184
11185/* Link block B supercontext to the previous block. The current
11186 function DECL is used as supercontext when enter_a_block is called
11187 for the first time for a given function. The current function body
11188 (DECL_FUNCTION_BODY) is set to be block B. */
11189
11190static tree
11191enter_a_block (b)
11192 tree b;
11193{
e04a16fb
AG
11194 tree fndecl = current_function_decl;
11195
f099f336
APB
11196 if (!fndecl) {
11197 BLOCK_SUPERCONTEXT (b) = current_static_block;
11198 current_static_block = b;
11199 }
11200
11201 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11202 {
11203 BLOCK_SUPERCONTEXT (b) = fndecl;
11204 DECL_FUNCTION_BODY (fndecl) = b;
11205 }
11206 else
11207 {
11208 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11209 DECL_FUNCTION_BODY (fndecl) = b;
11210 }
11211 return b;
11212}
11213
11214/* Exit a block by changing the current function body
11215 (DECL_FUNCTION_BODY) to the current block super context, only if
11216 the block being exited isn't the method's top level one. */
11217
11218static tree
11219exit_block ()
11220{
f099f336
APB
11221 tree b;
11222 if (current_function_decl)
11223 {
11224 b = DECL_FUNCTION_BODY (current_function_decl);
11225 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11226 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11227 }
11228 else
11229 {
11230 b = current_static_block;
e04a16fb 11231
f099f336
APB
11232 if (BLOCK_SUPERCONTEXT (b))
11233 current_static_block = BLOCK_SUPERCONTEXT (b);
11234 }
e04a16fb
AG
11235 return b;
11236}
11237
11238/* Lookup for NAME in the nested function's blocks, all the way up to
11239 the current toplevel one. It complies with Java's local variable
11240 scoping rules. */
11241
11242static tree
11243lookup_name_in_blocks (name)
11244 tree name;
11245{
f099f336 11246 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11247
11248 while (b != current_function_decl)
11249 {
11250 tree current;
11251
11252 /* Paranoid sanity check. To be removed */
11253 if (TREE_CODE (b) != BLOCK)
11254 fatal ("non block expr function body - lookup_name_in_blocks");
11255
11256 for (current = BLOCK_EXPR_DECLS (b); current;
11257 current = TREE_CHAIN (current))
11258 if (DECL_NAME (current) == name)
11259 return current;
11260 b = BLOCK_SUPERCONTEXT (b);
11261 }
11262 return NULL_TREE;
11263}
11264
11265static void
11266maybe_absorb_scoping_blocks ()
11267{
f099f336 11268 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11269 {
11270 tree b = exit_block ();
11271 java_method_add_stmt (current_function_decl, b);
11272 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11273 }
11274}
11275
11276\f
11277/* This section of the source is reserved to build_* functions that
11278 are building incomplete tree nodes and the patch_* functions that
11279 are completing them. */
11280
c2952b01
APB
11281/* Wrap a non WFL node around a WFL. */
11282static tree
11283build_wfl_wrap (node)
11284 tree node;
11285{
11286 tree wfl, node_to_insert = node;
11287
11288 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11289 with the way we're processing SUPER. A THIS from a primary as a
11290 different form than a SUPER. Turn THIS into something symbolic */
11291 if (TREE_CODE (node) == THIS_EXPR)
11292 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11293 else
11294 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11295
11296 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (node);
11297 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11298 return wfl;
11299}
11300
11301
9bbc7d9f 11302/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11303 we're currently dealing with the class java.lang.Object. */
11304
11305static tree
e920ebc9
APB
11306build_super_invocation (mdecl)
11307 tree mdecl;
22eed1e6 11308{
e920ebc9 11309 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11310 return empty_stmt_node;
22eed1e6
APB
11311 else
11312 {
9ee9b555 11313 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11314 tree a = NULL_TREE, t;
11315 /* If we're dealing with an anonymous class, pass the arguments
11316 of the crafted constructor along. */
11317 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11318 {
11319 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11320 for (; t != end_params_node; t = TREE_CHAIN (t))
11321 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11322 }
11323 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11324 }
11325}
11326
11327/* Build a SUPER/THIS qualified method invocation. */
11328
11329static tree
11330build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11331 int use_this;
11332 tree name, args;
11333 int lloc, rloc;
22eed1e6
APB
11334{
11335 tree invok;
11336 tree wfl =
9ee9b555 11337 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11338 EXPR_WFL_LINECOL (wfl) = lloc;
11339 invok = build_method_invocation (name, args);
11340 return make_qualified_primary (wfl, invok, rloc);
11341}
11342
b67d701b 11343/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11344
11345static tree
11346build_method_invocation (name, args)
11347 tree name;
11348 tree args;
11349{
11350 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11351 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11352 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11353 return call;
11354}
11355
11356/* Build an incomplete new xxx(...) node. */
11357
11358static tree
11359build_new_invocation (name, args)
11360 tree name, args;
11361{
11362 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11363 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11364 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11365 return call;
11366}
11367
11368/* Build an incomplete assignment expression. */
11369
11370static tree
11371build_assignment (op, op_location, lhs, rhs)
11372 int op, op_location;
11373 tree lhs, rhs;
11374{
11375 tree assignment;
11376 /* Build the corresponding binop if we deal with a Compound
11377 Assignment operator. Mark the binop sub-tree as part of a
11378 Compound Assignment expression */
11379 if (op != ASSIGN_TK)
11380 {
11381 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11382 COMPOUND_ASSIGN_P (rhs) = 1;
11383 }
11384 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11385 TREE_SIDE_EFFECTS (assignment) = 1;
11386 EXPR_WFL_LINECOL (assignment) = op_location;
11387 return assignment;
11388}
11389
11390/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11391
15fdcfe9 11392char *
e04a16fb
AG
11393print_int_node (node)
11394 tree node;
11395{
11396 static char buffer [80];
11397 if (TREE_CONSTANT_OVERFLOW (node))
11398 sprintf (buffer, "<overflow>");
11399
11400 if (TREE_INT_CST_HIGH (node) == 0)
11401 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11402 TREE_INT_CST_LOW (node));
11403 else if (TREE_INT_CST_HIGH (node) == -1
11404 && TREE_INT_CST_LOW (node) != 0)
11405 {
11406 buffer [0] = '-';
11407 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11408 -TREE_INT_CST_LOW (node));
11409 }
11410 else
11411 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11412 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11413
11414 return buffer;
11415}
11416
7f1d4866
APB
11417/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11418 context. */
5e942c50
APB
11419
11420static int
11421check_final_assignment (lvalue, wfl)
11422 tree lvalue, wfl;
11423{
6632dcdd
APB
11424 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11425 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11426 lvalue = TREE_OPERAND (lvalue, 1);
11427
bc2874c9
TT
11428 /* When generating class files, references to the `length' field
11429 look a bit different. */
11430 if ((flag_emit_class_files
11431 && TREE_CODE (lvalue) == COMPONENT_REF
11432 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11433 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11434 || (TREE_CODE (lvalue) == FIELD_DECL
11435 && FIELD_FINAL (lvalue)
11436 && !DECL_CLINIT_P (current_function_decl)
11437 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11438 {
11439 parse_error_context
11440 (wfl, "Can't assign a value to the final variable `%s'",
11441 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11442 return 1;
11443 }
11444 return 0;
11445}
11446
11447/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11448 read. This is needed to avoid circularities in the implementation
11449 of these fields in libjava. */
11450
11451static tree
11452maybe_build_primttype_type_ref (rhs, wfl)
11453 tree rhs, wfl;
11454{
11455 tree to_return = NULL_TREE;
11456 tree rhs_type = TREE_TYPE (rhs);
11457 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11458 {
11459 tree n = TREE_OPERAND (rhs, 1);
11460 if (TREE_CODE (n) == VAR_DECL
11461 && DECL_NAME (n) == TYPE_identifier_node
11462 && rhs_type == class_ptr_type)
11463 {
49f48c71 11464 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11465 if (!strncmp (self_name, "java.lang.", 10))
11466 to_return = build_primtype_type_ref (self_name);
11467 }
11468 }
11469 return (to_return ? to_return : rhs );
11470}
11471
e04a16fb
AG
11472/* 15.25 Assignment operators. */
11473
11474static tree
11475patch_assignment (node, wfl_op1, wfl_op2)
11476 tree node;
11477 tree wfl_op1;
11478 tree wfl_op2;
11479{
0a2138e2 11480 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11481 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11482 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11483 int error_found = 0;
11484 int lvalue_from_array = 0;
11485
c2952b01 11486 /* Can't assign to a (blank) final. */
5e942c50
APB
11487 if (check_final_assignment (lvalue, wfl_op1))
11488 error_found = 1;
e04a16fb
AG
11489
11490 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11491
11492 /* Lhs can be a named variable */
34f4db93 11493 if (JDECL_P (lvalue))
e04a16fb 11494 {
e04a16fb
AG
11495 lhs_type = TREE_TYPE (lvalue);
11496 }
11497 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11498 comment on reason why */
11499 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11500 {
11501 lhs_type = TREE_TYPE (lvalue);
11502 lvalue_from_array = 1;
11503 }
11504 /* Or a field access */
11505 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11506 lhs_type = TREE_TYPE (lvalue);
11507 /* Or a function return slot */
11508 else if (TREE_CODE (lvalue) == RESULT_DECL)
11509 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11510 /* Otherwise, we might want to try to write into an optimized static
11511 final, this is an of a different nature, reported further on. */
11512 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11513 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11514 {
6632dcdd 11515 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11516 {
11517 /* What we should do instead is resetting the all the flags
11518 previously set, exchange lvalue for llvalue and continue. */
11519 error_found = 1;
11520 return error_mark_node;
11521 }
11522 else
11523 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11524 }
11525 else
e04a16fb
AG
11526 {
11527 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11528 error_found = 1;
11529 }
11530
11531 rhs_type = TREE_TYPE (rhs);
b67d701b 11532 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11533 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11534
b67d701b 11535 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11536 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11537 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11538
11539 /* 15.25.2 If we have a compound assignment, convert RHS into the
11540 type of the LHS */
11541 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11542 new_rhs = convert (lhs_type, rhs);
11543
11544 /* Explicit cast required. This is an error */
11545 if (!new_rhs)
11546 {
c2e3db92
KG
11547 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11548 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11549 tree wfl;
11550 char operation [32]; /* Max size known */
11551
11552 /* If the assignment is part of a declaration, we use the WFL of
11553 the declared variable to point out the error and call it a
11554 declaration problem. If the assignment is a genuine =
11555 operator, we call is a operator `=' problem, otherwise we
11556 call it an assignment problem. In both of these last cases,
11557 we use the WFL of the operator to indicate the error. */
11558
11559 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11560 {
11561 wfl = wfl_op1;
11562 strcpy (operation, "declaration");
11563 }
11564 else
11565 {
11566 wfl = wfl_operator;
11567 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11568 strcpy (operation, "assignment");
11569 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11570 strcpy (operation, "`return'");
11571 else
11572 strcpy (operation, "`='");
11573 }
11574
1ebadc60 11575 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
11576 parse_error_context
11577 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11578 operation, t1, t2);
1ebadc60 11579 else
781b0558 11580 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 11581 operation, t1, t2);
e04a16fb
AG
11582 free (t1); free (t2);
11583 error_found = 1;
11584 }
11585
c877974e
APB
11586 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11587 if (new_rhs)
11588 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 11589
e04a16fb
AG
11590 if (error_found)
11591 return error_mark_node;
11592
2622b947
APB
11593 /* 10.10: Array Store Exception runtime check */
11594 if (!flag_emit_class_files
e8fc7396 11595 && !flag_emit_xref
2622b947 11596 && lvalue_from_array
afc390b1 11597 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
11598 {
11599 tree check;
11600 tree base = lvalue;
11601
11602 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11603 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11604 base = TREE_OPERAND (lvalue, 0);
11605 else
11606 {
11607 if (flag_bounds_check)
11608 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11609 else
11610 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11611 }
11612
11613 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 11614 new_rhs = save_expr (new_rhs);
2622b947
APB
11615 check = build (CALL_EXPR, void_type_node,
11616 build_address_of (soft_checkarraystore_node),
11617 tree_cons (NULL_TREE, base,
11618 build_tree_list (NULL_TREE, new_rhs)),
11619 NULL_TREE);
11620 TREE_SIDE_EFFECTS (check) = 1;
11621
11622 /* We have to decide on an insertion point */
11623 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11624 {
11625 tree t;
11626 if (flag_bounds_check)
11627 {
11628 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11629 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11630 build (COMPOUND_EXPR, void_type_node, t, check);
11631 }
11632 else
11633 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
11634 check, TREE_OPERAND (lvalue, 1));
11635 }
11636 else
11637 {
11638 /* Make sure the bound check will happen before the store check */
11639 if (flag_bounds_check)
11640 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
11641 build (COMPOUND_EXPR, void_type_node,
11642 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
11643 else
11644 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
11645 }
11646 }
22eed1e6 11647
e04a16fb
AG
11648 TREE_OPERAND (node, 0) = lvalue;
11649 TREE_OPERAND (node, 1) = new_rhs;
11650 TREE_TYPE (node) = lhs_type;
11651 return node;
11652}
11653
b67d701b
PB
11654/* Check that type SOURCE can be cast into type DEST. If the cast
11655 can't occur at all, return 0 otherwise 1. This function is used to
11656 produce accurate error messages on the reasons why an assignment
11657 failed. */
e04a16fb 11658
b67d701b
PB
11659static tree
11660try_reference_assignconv (lhs_type, rhs)
11661 tree lhs_type, rhs;
e04a16fb 11662{
b67d701b
PB
11663 tree new_rhs = NULL_TREE;
11664 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 11665
b67d701b
PB
11666 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
11667 {
11668 /* `null' may be assigned to any reference type */
11669 if (rhs == null_pointer_node)
11670 new_rhs = null_pointer_node;
11671 /* Try the reference assignment conversion */
11672 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
11673 new_rhs = rhs;
11674 /* This is a magic assignment that we process differently */
11675 else if (rhs == soft_exceptioninfo_call_node)
11676 new_rhs = rhs;
11677 }
11678 return new_rhs;
11679}
11680
11681/* Check that RHS can be converted into LHS_TYPE by the assignment
11682 conversion (5.2), for the cases of RHS being a builtin type. Return
11683 NULL_TREE if the conversion fails or if because RHS isn't of a
11684 builtin type. Return a converted RHS if the conversion is possible. */
11685
11686static tree
11687try_builtin_assignconv (wfl_op1, lhs_type, rhs)
11688 tree wfl_op1, lhs_type, rhs;
11689{
11690 tree new_rhs = NULL_TREE;
11691 tree rhs_type = TREE_TYPE (rhs);
11692
5e942c50
APB
11693 /* Zero accepted everywhere */
11694 if (TREE_CODE (rhs) == INTEGER_CST
11695 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
11696 && JPRIMITIVE_TYPE_P (rhs_type))
11697 new_rhs = convert (lhs_type, rhs);
11698
b67d701b
PB
11699 /* 5.1.1 Try Identity Conversion,
11700 5.1.2 Try Widening Primitive Conversion */
5e942c50 11701 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
11702 new_rhs = convert (lhs_type, rhs);
11703
11704 /* Try a narrowing primitive conversion (5.1.3):
11705 - expression is a constant expression of type int AND
11706 - variable is byte, short or char AND
11707 - The value of the expression is representable in the type of the
11708 variable */
11709 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
11710 && (lhs_type == byte_type_node || lhs_type == char_type_node
11711 || lhs_type == short_type_node))
11712 {
11713 if (int_fits_type_p (rhs, lhs_type))
11714 new_rhs = convert (lhs_type, rhs);
11715 else if (wfl_op1) /* Might be called with a NULL */
11716 parse_warning_context
781b0558 11717 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 11718 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
11719 /* Reported a warning that will turn into an error further
11720 down, so we don't return */
11721 }
11722
11723 return new_rhs;
11724}
11725
11726/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
11727 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
11728 0 is the conversion test fails. This implements parts the method
11729 invocation convertion (5.3). */
11730
11731static int
11732valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
11733 tree lhs_type, rhs_type;
11734{
acd663ee 11735 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
11736 if (lhs_type == rhs_type)
11737 return 1;
11738
acd663ee
APB
11739 /* Reject non primitive types */
11740 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
11741 return 0;
11742
acd663ee
APB
11743 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
11744 than a char can't be converted into a char. Short can't too, but
11745 the < test below takes care of that */
b67d701b
PB
11746 if (lhs_type == char_type_node && rhs_type == byte_type_node)
11747 return 0;
11748
5e942c50
APB
11749 /* Accept all promoted type here. Note, we can't use <= in the test
11750 below, because we still need to bounce out assignments of short
11751 to char and the likes */
11752 if (lhs_type == int_type_node
11753 && (rhs_type == promoted_byte_type_node
11754 || rhs_type == promoted_short_type_node
11755 || rhs_type == promoted_char_type_node
11756 || rhs_type == promoted_boolean_type_node))
11757 return 1;
11758
acd663ee
APB
11759 /* From here, an integral is widened if its precision is smaller
11760 than the precision of the LHS or if the LHS is a floating point
11761 type, or the RHS is a float and the RHS a double. */
11762 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
11763 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
11764 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
11765 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
11766 return 1;
11767
11768 return 0;
e04a16fb
AG
11769}
11770
11771/* Check that something of SOURCE type can be assigned or cast to
11772 something of DEST type at runtime. Return 1 if the operation is
11773 valid, 0 otherwise. If CAST is set to 1, we're treating the case
11774 were SOURCE is cast into DEST, which borrows a lot of the
11775 assignment check. */
11776
11777static int
11778valid_ref_assignconv_cast_p (source, dest, cast)
11779 tree source;
11780 tree dest;
11781 int cast;
11782{
09ed0f70
APB
11783 /* SOURCE or DEST might be null if not from a declared entity. */
11784 if (!source || !dest)
11785 return 0;
5e942c50
APB
11786 if (JNULLP_TYPE_P (source))
11787 return 1;
e04a16fb
AG
11788 if (TREE_CODE (source) == POINTER_TYPE)
11789 source = TREE_TYPE (source);
11790 if (TREE_CODE (dest) == POINTER_TYPE)
11791 dest = TREE_TYPE (dest);
11792 /* Case where SOURCE is a class type */
11793 if (TYPE_CLASS_P (source))
11794 {
11795 if (TYPE_CLASS_P (dest))
c2952b01
APB
11796 return (source == dest
11797 || inherits_from_p (source, dest)
11798 || enclosing_context_p (dest, source /*source, dest*/)
11799 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
11800 if (TYPE_INTERFACE_P (dest))
11801 {
11802 /* If doing a cast and SOURCE is final, the operation is
11803 always correct a compile time (because even if SOURCE
11804 does not implement DEST, a subclass of SOURCE might). */
11805 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
11806 return 1;
11807 /* Otherwise, SOURCE must implement DEST */
11808 return interface_of_p (dest, source);
11809 }
11810 /* DEST is an array, cast permited if SOURCE is of Object type */
11811 return (cast && source == object_type_node ? 1 : 0);
11812 }
11813 if (TYPE_INTERFACE_P (source))
11814 {
11815 if (TYPE_CLASS_P (dest))
11816 {
11817 /* If not casting, DEST must be the Object type */
11818 if (!cast)
11819 return dest == object_type_node;
11820 /* We're doing a cast. The cast is always valid is class
11821 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 11822 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
11823 return 1;
11824 else
11825 return interface_of_p (source, dest);
11826 }
11827 if (TYPE_INTERFACE_P (dest))
11828 {
11829 /* If doing a cast, then if SOURCE and DEST contain method
11830 with the same signature but different return type, then
11831 this is a (compile time) error */
11832 if (cast)
11833 {
11834 tree method_source, method_dest;
11835 tree source_type;
0a2138e2 11836 tree source_sig;
e04a16fb
AG
11837 tree source_name;
11838 for (method_source = TYPE_METHODS (source); method_source;
11839 method_source = TREE_CHAIN (method_source))
11840 {
11841 source_sig =
11842 build_java_argument_signature (TREE_TYPE (method_source));
11843 source_type = TREE_TYPE (TREE_TYPE (method_source));
11844 source_name = DECL_NAME (method_source);
11845 for (method_dest = TYPE_METHODS (dest);
11846 method_dest; method_dest = TREE_CHAIN (method_dest))
11847 if (source_sig ==
11848 build_java_argument_signature (TREE_TYPE (method_dest))
11849 && source_name == DECL_NAME (method_dest)
11850 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
11851 return 0;
11852 }
11853 return 1;
11854 }
11855 else
11856 return source == dest || interface_of_p (dest, source);
11857 }
11858 else /* Array */
93024893
APB
11859 return (cast ?
11860 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
11861 }
11862 if (TYPE_ARRAY_P (source))
11863 {
11864 if (TYPE_CLASS_P (dest))
11865 return dest == object_type_node;
09ed0f70
APB
11866 /* Can't cast an array to an interface unless the interface is
11867 java.lang.Cloneable */
e04a16fb 11868 if (TYPE_INTERFACE_P (dest))
09ed0f70 11869 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
11870 else /* Arrays */
11871 {
11872 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
11873 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
11874
b9f7e36c
APB
11875 /* In case of severe errors, they turn out null */
11876 if (!dest_element_type || !source_element_type)
11877 return 0;
e04a16fb
AG
11878 if (source_element_type == dest_element_type)
11879 return 1;
11880 return valid_ref_assignconv_cast_p (source_element_type,
11881 dest_element_type, cast);
11882 }
11883 return 0;
11884 }
11885 return 0;
11886}
11887
b67d701b
PB
11888static int
11889valid_cast_to_p (source, dest)
11890 tree source;
11891 tree dest;
11892{
11893 if (TREE_CODE (source) == POINTER_TYPE)
11894 source = TREE_TYPE (source);
11895 if (TREE_CODE (dest) == POINTER_TYPE)
11896 dest = TREE_TYPE (dest);
11897
11898 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
11899 return valid_ref_assignconv_cast_p (source, dest, 1);
11900
11901 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
11902 return 1;
11903
11904 return 0;
11905}
11906
11907/* Method invocation conversion test. Return 1 if type SOURCE can be
11908 converted to type DEST through the methond invocation conversion
11909 process (5.3) */
11910
15fdcfe9
PB
11911static tree
11912do_unary_numeric_promotion (arg)
11913 tree arg;
11914{
11915 tree type = TREE_TYPE (arg);
11916 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
11917 : TREE_CODE (type) == CHAR_TYPE)
11918 arg = convert (int_type_node, arg);
11919 return arg;
11920}
11921
acd663ee
APB
11922/* Return a non zero value if SOURCE can be converted into DEST using
11923 the method invocation conversion rule (5.3). */
b67d701b
PB
11924static int
11925valid_method_invocation_conversion_p (dest, source)
11926 tree dest, source;
11927{
e3884b71 11928 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
11929 && valid_builtin_assignconv_identity_widening_p (dest, source))
11930 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
11931 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
11932 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
11933}
11934
e04a16fb
AG
11935/* Build an incomplete binop expression. */
11936
11937static tree
11938build_binop (op, op_location, op1, op2)
11939 enum tree_code op;
11940 int op_location;
11941 tree op1, op2;
11942{
5e942c50 11943 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
11944 TREE_SIDE_EFFECTS (binop) = 1;
11945 /* Store the location of the operator, for better error report. The
11946 string of the operator will be rebuild based on the OP value. */
11947 EXPR_WFL_LINECOL (binop) = op_location;
11948 return binop;
11949}
11950
11951/* Build the string of the operator retained by NODE. If NODE is part
11952 of a compound expression, add an '=' at the end of the string. This
11953 function is called when an error needs to be reported on an
11954 operator. The string is returned as a pointer to a static character
11955 buffer. */
11956
11957static char *
11958operator_string (node)
11959 tree node;
11960{
11961#define BUILD_OPERATOR_STRING(S) \
11962 { \
11963 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
11964 return buffer; \
11965 }
11966
11967 static char buffer [10];
11968 switch (TREE_CODE (node))
11969 {
11970 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
11971 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
11972 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
11973 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
11974 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
11975 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
11976 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
11977 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
11978 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
11979 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
11980 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
11981 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
11982 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
11983 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
11984 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
11985 case GT_EXPR: BUILD_OPERATOR_STRING (">");
11986 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
11987 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
11988 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 11989 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
11990 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
11991 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
11992 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
11993 case PREINCREMENT_EXPR: /* Fall through */
11994 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
11995 case PREDECREMENT_EXPR: /* Fall through */
11996 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
11997 default:
11998 fatal ("unregistered operator %s - operator_string",
11999 tree_code_name [TREE_CODE (node)]);
12000 }
12001 return NULL;
12002#undef BUILD_OPERATOR_STRING
12003}
12004
5cbdba64
APB
12005/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12006
12007static int
12008java_decl_equiv (var_acc1, var_acc2)
12009 tree var_acc1, var_acc2;
12010{
12011 if (JDECL_P (var_acc1))
12012 return (var_acc1 == var_acc2);
12013
12014 return (TREE_CODE (var_acc1) == COMPONENT_REF
12015 && TREE_CODE (var_acc2) == COMPONENT_REF
12016 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12017 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12018 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12019}
12020
12021/* Return a non zero value if CODE is one of the operators that can be
12022 used in conjunction with the `=' operator in a compound assignment. */
12023
12024static int
12025binop_compound_p (code)
12026 enum tree_code code;
12027{
12028 int i;
12029 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12030 if (binop_lookup [i] == code)
12031 break;
12032
12033 return i < BINOP_COMPOUND_CANDIDATES;
12034}
12035
12036/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12037
12038static tree
12039java_refold (t)
12040 tree t;
12041{
12042 tree c, b, ns, decl;
12043
12044 if (TREE_CODE (t) != MODIFY_EXPR)
12045 return t;
12046
12047 c = TREE_OPERAND (t, 1);
12048 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12049 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12050 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12051 return t;
12052
12053 /* Now the left branch of the binary operator. */
12054 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12055 if (! (b && TREE_CODE (b) == NOP_EXPR
12056 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12057 return t;
12058
12059 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12060 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12061 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12062 return t;
12063
12064 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12065 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12066 /* It's got to be the an equivalent decl */
12067 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12068 {
12069 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12070 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12071 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12072 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12073 /* Change the right part of the BINOP_EXPR */
12074 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12075 }
12076
12077 return t;
12078}
12079
e04a16fb
AG
12080/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12081 errors but we modify NODE so that it contains the type computed
12082 according to the expression, when it's fixed. Otherwise, we write
12083 error_mark_node as the type. It allows us to further the analysis
12084 of remaining nodes and detects more errors in certain cases. */
12085
12086static tree
12087patch_binop (node, wfl_op1, wfl_op2)
12088 tree node;
12089 tree wfl_op1;
12090 tree wfl_op2;
12091{
12092 tree op1 = TREE_OPERAND (node, 0);
12093 tree op2 = TREE_OPERAND (node, 1);
12094 tree op1_type = TREE_TYPE (op1);
12095 tree op2_type = TREE_TYPE (op2);
48a840d9 12096 tree prom_type = NULL_TREE, cn;
e04a16fb 12097 int code = TREE_CODE (node);
b67d701b 12098
e04a16fb
AG
12099 /* If 1, tell the routine that we have to return error_mark_node
12100 after checking for the initialization of the RHS */
12101 int error_found = 0;
12102
e04a16fb
AG
12103 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12104
e04a16fb
AG
12105 switch (code)
12106 {
12107 /* 15.16 Multiplicative operators */
12108 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12109 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12110 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12111 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12112 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12113 {
12114 if (!JPRIMITIVE_TYPE_P (op1_type))
12115 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12116 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12117 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12118 TREE_TYPE (node) = error_mark_node;
12119 error_found = 1;
12120 break;
12121 }
12122 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12123 /* Change the division operator if necessary */
12124 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12125 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12126
aa4759c1
AH
12127 if (TREE_CODE (prom_type) == INTEGER_TYPE
12128 && flag_use_divide_subroutine
12129 && ! flag_emit_class_files
12130 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12131 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12132
0b4d333e
APB
12133 /* This one is more complicated. FLOATs are processed by a
12134 function call to soft_fmod. Duplicate the value of the
12135 COMPOUND_ASSIGN_P flag. */
e04a16fb 12136 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12137 {
12138 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12139 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12140 TREE_SIDE_EFFECTS (mod)
12141 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12142 return mod;
12143 }
e04a16fb
AG
12144 break;
12145
12146 /* 15.17 Additive Operators */
12147 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12148
12149 /* Operation is valid if either one argument is a string
12150 constant, a String object or a StringBuffer crafted for the
12151 purpose of the a previous usage of the String concatenation
12152 operator */
12153
12154 if (TREE_CODE (op1) == STRING_CST
12155 || TREE_CODE (op2) == STRING_CST
12156 || JSTRING_TYPE_P (op1_type)
12157 || JSTRING_TYPE_P (op2_type)
12158 || IS_CRAFTED_STRING_BUFFER_P (op1)
12159 || IS_CRAFTED_STRING_BUFFER_P (op2))
12160 return build_string_concatenation (op1, op2);
12161
e04a16fb
AG
12162 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12163 Numeric Types */
12164 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12165 {
12166 if (!JPRIMITIVE_TYPE_P (op1_type))
12167 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12168 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12169 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12170 TREE_TYPE (node) = error_mark_node;
12171 error_found = 1;
12172 break;
12173 }
12174 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12175 break;
12176
12177 /* 15.18 Shift Operators */
12178 case LSHIFT_EXPR:
12179 case RSHIFT_EXPR:
12180 case URSHIFT_EXPR:
12181 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12182 {
12183 if (!JINTEGRAL_TYPE_P (op1_type))
12184 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12185 else
1ebadc60
KG
12186 {
12187 if (JPRIMITIVE_TYPE_P (op2_type))
12188 parse_error_context (wfl_operator,
781b0558 12189 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12190 operator_string (node),
12191 lang_printable_name (op2_type, 0));
12192 else
781b0558
KG
12193 parse_error_context (wfl_operator,
12194 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12195 operator_string (node),
12196 lang_printable_name (op2_type, 0));
12197 }
e04a16fb
AG
12198 TREE_TYPE (node) = error_mark_node;
12199 error_found = 1;
12200 break;
12201 }
12202
12203 /* Unary numeric promotion (5.6.1) is performed on each operand
12204 separatly */
15fdcfe9
PB
12205 op1 = do_unary_numeric_promotion (op1);
12206 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12207
12208 /* The type of the shift expression is the type of the promoted
12209 type of the left-hand operand */
12210 prom_type = TREE_TYPE (op1);
12211
c2952b01
APB
12212 /* Shift int only up to 0x1f and long up to 0x3f */
12213 if (prom_type == int_type_node)
12214 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12215 build_int_2 (0x1f, 0)));
12216 else
12217 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12218 build_int_2 (0x3f, 0)));
e04a16fb
AG
12219
12220 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12221 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12222 {
0b4d333e 12223 tree to_return;
73333a87
AH
12224 tree utype = unsigned_type (prom_type);
12225 op1 = convert (utype, op1);
e04a16fb 12226 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12227 TREE_OPERAND (node, 0) = op1;
12228 TREE_OPERAND (node, 1) = op2;
12229 TREE_TYPE (node) = utype;
0b4d333e
APB
12230 to_return = convert (prom_type, node);
12231 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12232 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12233 TREE_SIDE_EFFECTS (to_return)
12234 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12235 return to_return;
e04a16fb
AG
12236 }
12237 break;
5e942c50
APB
12238
12239 /* 15.19.1 Type Comparison Operator instaceof */
12240 case INSTANCEOF_EXPR:
12241
12242 TREE_TYPE (node) = boolean_type_node;
12243
12244 if (!(op2_type = resolve_type_during_patch (op2)))
12245 return error_mark_node;
12246
12247 /* The first operand must be a reference type or the null type */
12248 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12249 error_found = 1; /* Error reported further below */
12250
12251 /* The second operand must be a reference type */
12252 if (!JREFERENCE_TYPE_P (op2_type))
12253 {
12254 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12255 parse_error_context
12256 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12257 lang_printable_name (op2_type, 0));
12258 error_found = 1;
12259 }
12260
12261 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12262 {
12263 /* If the first operand is null, the result is always false */
12264 if (op1 == null_pointer_node)
12265 return boolean_false_node;
15fdcfe9
PB
12266 else if (flag_emit_class_files)
12267 {
12268 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12269 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12270 return node;
12271 }
5e942c50
APB
12272 /* Otherwise we have to invoke instance of to figure it out */
12273 else
12274 {
12275 tree call =
12276 build (CALL_EXPR, boolean_type_node,
12277 build_address_of (soft_instanceof_node),
12278 tree_cons
12279 (NULL_TREE, op1,
12280 build_tree_list (NULL_TREE,
12281 build_class_ref (op2_type))),
12282 NULL_TREE);
dc0b3eff 12283 TREE_SIDE_EFFECTS (call) = TREE_SIDE_EFFECTS (op1);
5e942c50
APB
12284 return call;
12285 }
12286 }
12287 /* There is no way the expression operand can be an instance of
12288 the type operand. This is a compile time error. */
12289 else
12290 {
c2e3db92 12291 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12292 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12293 parse_error_context
12294 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12295 t1, lang_printable_name (op2_type, 0));
12296 free (t1);
12297 error_found = 1;
12298 }
e04a16fb 12299
5e942c50 12300 break;
e04a16fb
AG
12301
12302 /* 15.21 Bitwise and Logical Operators */
12303 case BIT_AND_EXPR:
12304 case BIT_XOR_EXPR:
12305 case BIT_IOR_EXPR:
12306 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12307 /* Binary numeric promotion is performed on both operand and the
12308 expression retain that type */
12309 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12310
12311 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12312 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12313 /* The type of the bitwise operator expression is BOOLEAN */
12314 prom_type = boolean_type_node;
12315 else
12316 {
12317 if (!JINTEGRAL_TYPE_P (op1_type))
12318 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12319 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12320 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12321 TREE_TYPE (node) = error_mark_node;
12322 error_found = 1;
12323 /* Insert a break here if adding thing before the switch's
12324 break for this case */
12325 }
12326 break;
12327
12328 /* 15.22 Conditional-And Operator */
12329 case TRUTH_ANDIF_EXPR:
12330 /* 15.23 Conditional-Or Operator */
12331 case TRUTH_ORIF_EXPR:
12332 /* Operands must be of BOOLEAN type */
12333 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12334 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12335 {
12336 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12337 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12338 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12339 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12340 TREE_TYPE (node) = boolean_type_node;
12341 error_found = 1;
12342 break;
12343 }
12344 /* The type of the conditional operators is BOOLEAN */
12345 prom_type = boolean_type_node;
12346 break;
12347
12348 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12349 case LT_EXPR:
12350 case GT_EXPR:
12351 case LE_EXPR:
12352 case GE_EXPR:
12353 /* The type of each of the operands must be a primitive numeric
12354 type */
12355 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12356 {
12357 if (!JNUMERIC_TYPE_P (op1_type))
12358 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12359 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12360 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12361 TREE_TYPE (node) = boolean_type_node;
12362 error_found = 1;
12363 break;
12364 }
12365 /* Binary numeric promotion is performed on the operands */
12366 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12367 /* The type of the relation expression is always BOOLEAN */
12368 prom_type = boolean_type_node;
12369 break;
12370
12371 /* 15.20 Equality Operator */
12372 case EQ_EXPR:
12373 case NE_EXPR:
48a840d9
APB
12374 /* It's time for us to patch the strings. */
12375 if ((cn = patch_string (op1)))
12376 {
12377 op1 = cn;
12378 op1_type = TREE_TYPE (op1);
12379 }
12380 if ((cn = patch_string (op2)))
12381 {
12382 op2 = cn;
12383 op2_type = TREE_TYPE (op2);
12384 }
12385
e04a16fb
AG
12386 /* 15.20.1 Numerical Equality Operators == and != */
12387 /* Binary numeric promotion is performed on the operands */
5e942c50 12388 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12389 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12390
12391 /* 15.20.2 Boolean Equality Operators == and != */
12392 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12393 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12394 ; /* Nothing to do here */
12395
12396 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12397 /* Types have to be either references or the null type. If
12398 they're references, it must be possible to convert either
12399 type to the other by casting conversion. */
b9f7e36c
APB
12400 else if (op1 == null_pointer_node || op2 == null_pointer_node
12401 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12402 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12403 || valid_ref_assignconv_cast_p (op2_type,
12404 op1_type, 1))))
e04a16fb
AG
12405 ; /* Nothing to do here */
12406
12407 /* Else we have an error figure what can't be converted into
12408 what and report the error */
12409 else
12410 {
12411 char *t1;
c2e3db92 12412 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12413 parse_error_context
781b0558
KG
12414 (wfl_operator,
12415 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12416 operator_string (node), t1,
0a2138e2 12417 lang_printable_name (op2_type, 0));
e04a16fb
AG
12418 free (t1);
12419 TREE_TYPE (node) = boolean_type_node;
12420 error_found = 1;
12421 break;
12422 }
12423 prom_type = boolean_type_node;
12424 break;
12425 }
12426
e04a16fb
AG
12427 if (error_found)
12428 return error_mark_node;
12429
12430 TREE_OPERAND (node, 0) = op1;
12431 TREE_OPERAND (node, 1) = op2;
12432 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12433 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12434
ce6e9147
APB
12435 if (flag_emit_xref)
12436 return node;
12437
d1472141
PB
12438 /* fold does not respect side-effect order as required for Java but not C.
12439 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12440 * bytecode.
12441 */
12442 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12443 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12444 node = fold (node);
12445 return node;
e04a16fb
AG
12446}
12447
b67d701b
PB
12448/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12449 zero value, the value of CSTE comes after the valude of STRING */
12450
12451static tree
12452do_merge_string_cste (cste, string, string_len, after)
12453 tree cste;
49f48c71 12454 const char *string;
b67d701b
PB
12455 int string_len, after;
12456{
12457 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 12458 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
12459 TREE_STRING_LENGTH (cste) = len;
12460 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12461 if (after)
12462 {
12463 strcpy (TREE_STRING_POINTER (cste), string);
12464 strcat (TREE_STRING_POINTER (cste), old);
12465 }
12466 else
12467 {
12468 strcpy (TREE_STRING_POINTER (cste), old);
12469 strcat (TREE_STRING_POINTER (cste), string);
12470 }
12471 return cste;
12472}
12473
12474/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12475 new STRING_CST on success, NULL_TREE on failure */
12476
12477static tree
12478merge_string_cste (op1, op2, after)
12479 tree op1, op2;
12480 int after;
12481{
12482 /* Handle two string constants right away */
12483 if (TREE_CODE (op2) == STRING_CST)
12484 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12485 TREE_STRING_LENGTH (op2), after);
12486
12487 /* Reasonable integer constant can be treated right away */
12488 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12489 {
49f48c71
KG
12490 static const char *boolean_true = "true";
12491 static const char *boolean_false = "false";
12492 static const char *null_pointer = "null";
b67d701b 12493 char ch[3];
49f48c71 12494 const char *string;
b67d701b
PB
12495
12496 if (op2 == boolean_true_node)
12497 string = boolean_true;
12498 else if (op2 == boolean_false_node)
12499 string = boolean_false;
12500 else if (op2 == null_pointer_node)
12501 string = null_pointer;
12502 else if (TREE_TYPE (op2) == char_type_node)
12503 {
12504 ch[0] = (char )TREE_INT_CST_LOW (op2);
12505 ch[1] = '\0';
12506 string = ch;
12507 }
12508 else
12509 string = print_int_node (op2);
12510
12511 return do_merge_string_cste (op1, string, strlen (string), after);
12512 }
12513 return NULL_TREE;
12514}
12515
12516/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12517 has to be a STRING_CST and the other part must be a STRING_CST or a
12518 INTEGRAL constant. Return a new STRING_CST if the operation
12519 succeed, NULL_TREE otherwise.
12520
12521 If the case we want to optimize for space, we might want to return
12522 NULL_TREE for each invocation of this routine. FIXME */
12523
12524static tree
12525string_constant_concatenation (op1, op2)
12526 tree op1, op2;
12527{
12528 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12529 {
0a2138e2 12530 tree string, rest;
b67d701b
PB
12531 int invert;
12532
12533 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12534 rest = (string == op1 ? op2 : op1);
12535 invert = (string == op1 ? 0 : 1 );
12536
12537 /* Walk REST, only if it looks reasonable */
12538 if (TREE_CODE (rest) != STRING_CST
12539 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12540 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12541 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12542 {
12543 rest = java_complete_tree (rest);
12544 if (rest == error_mark_node)
12545 return error_mark_node;
12546 rest = fold (rest);
12547 }
12548 return merge_string_cste (string, rest, invert);
12549 }
12550 return NULL_TREE;
12551}
12552
12553/* Implement the `+' operator. Does static optimization if possible,
12554 otherwise create (if necessary) and append elements to a
12555 StringBuffer. The StringBuffer will be carried around until it is
12556 used for a function call or an assignment. Then toString() will be
12557 called on it to turn it into a String object. */
12558
12559static tree
12560build_string_concatenation (op1, op2)
12561 tree op1, op2;
12562{
12563 tree result;
dc0b3eff 12564 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
12565
12566 if (flag_emit_xref)
12567 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
12568
12569 /* Try to do some static optimization */
12570 if ((result = string_constant_concatenation (op1, op2)))
12571 return result;
12572
c0d87ff6
PB
12573 /* Discard empty strings on either side of the expression */
12574 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
12575 {
12576 op1 = op2;
12577 op2 = NULL_TREE;
12578 }
c0d87ff6 12579 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 12580 op2 = NULL_TREE;
b67d701b 12581
acd663ee 12582 /* If operands are string constant, turn then into object references */
b67d701b
PB
12583 if (TREE_CODE (op1) == STRING_CST)
12584 op1 = patch_string_cst (op1);
acd663ee 12585 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
12586 op2 = patch_string_cst (op2);
12587
acd663ee
APB
12588 /* If either one of the constant is null and the other non null
12589 operand is a String object, return it. */
12590 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12591 return op1;
12592
b67d701b
PB
12593 /* If OP1 isn't already a StringBuffer, create and
12594 initialize a new one */
12595 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12596 {
12597 /* Two solutions here:
c52b5771
AG
12598 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12599 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12600 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
12601 op1 = BUILD_STRING_BUFFER (op1);
12602 else
12603 {
12604 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12605 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12606 }
12607 }
12608
acd663ee
APB
12609 if (op2)
12610 {
12611 /* OP1 is no longer the last node holding a crafted StringBuffer */
12612 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12613 /* Create a node for `{new...,xxx}.append (op2)' */
12614 if (op2)
12615 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12616 }
12617
b67d701b
PB
12618 /* Mark the last node holding a crafted StringBuffer */
12619 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
12620
12621 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
12622 return op1;
12623}
12624
12625/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12626 StringBuffer. If no string were found to be patched, return
12627 NULL. */
12628
12629static tree
12630patch_string (node)
12631 tree node;
12632{
1179ebc2
APB
12633 if (node == error_mark_node)
12634 return error_mark_node;
b67d701b
PB
12635 if (TREE_CODE (node) == STRING_CST)
12636 return patch_string_cst (node);
12637 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12638 {
c877974e 12639 int saved = ctxp->explicit_constructor_p;
b67d701b 12640 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
12641 tree ret;
12642 /* Temporary disable forbid the use of `this'. */
12643 ctxp->explicit_constructor_p = 0;
12644 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
12645 /* String concatenation arguments must be evaluated in order too. */
12646 ret = force_evaluation_order (ret);
c877974e
APB
12647 /* Restore it at its previous value */
12648 ctxp->explicit_constructor_p = saved;
12649 return ret;
b67d701b
PB
12650 }
12651 return NULL_TREE;
12652}
12653
12654/* Build the internal representation of a string constant. */
12655
12656static tree
12657patch_string_cst (node)
12658 tree node;
12659{
12660 int location;
15fdcfe9
PB
12661 if (! flag_emit_class_files)
12662 {
12663 push_obstacks (&permanent_obstack, &permanent_obstack);
12664 node = get_identifier (TREE_STRING_POINTER (node));
12665 location = alloc_name_constant (CONSTANT_String, node);
12666 node = build_ref_from_constant_pool (location);
8226320b 12667 pop_obstacks ();
15fdcfe9 12668 }
cd9643f7 12669 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
12670 TREE_CONSTANT (node) = 1;
12671 return node;
12672}
12673
12674/* Build an incomplete unary operator expression. */
e04a16fb
AG
12675
12676static tree
12677build_unaryop (op_token, op_location, op1)
12678 int op_token, op_location;
12679 tree op1;
12680{
12681 enum tree_code op;
12682 tree unaryop;
12683 switch (op_token)
12684 {
b67d701b 12685 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
12686 case MINUS_TK: op = NEGATE_EXPR; break;
12687 case NEG_TK: op = TRUTH_NOT_EXPR; break;
12688 case NOT_TK: op = BIT_NOT_EXPR; break;
12689 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
12690 op_token);
12691 }
12692
12693 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
12694 TREE_SIDE_EFFECTS (unaryop) = 1;
12695 /* Store the location of the operator, for better error report. The
12696 string of the operator will be rebuild based on the OP value. */
12697 EXPR_WFL_LINECOL (unaryop) = op_location;
12698 return unaryop;
12699}
12700
12701/* Special case for the ++/-- operators, since they require an extra
12702 argument to build, which is set to NULL and patched
12703 later. IS_POST_P is 1 if the operator, 0 otherwise. */
12704
12705static tree
12706build_incdec (op_token, op_location, op1, is_post_p)
12707 int op_token, op_location;
12708 tree op1;
12709 int is_post_p;
12710{
12711 static enum tree_code lookup [2][2] =
12712 {
12713 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
12714 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
12715 };
12716 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
12717 NULL_TREE, op1, NULL_TREE);
12718 TREE_SIDE_EFFECTS (node) = 1;
12719 /* Store the location of the operator, for better error report. The
12720 string of the operator will be rebuild based on the OP value. */
12721 EXPR_WFL_LINECOL (node) = op_location;
12722 return node;
12723}
12724
12725/* Build an incomplete cast operator, based on the use of the
12726 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
12727 set. java_complete_tree is trained to walk a CONVERT_EXPR even
12728 though its type is already set. */
12729
12730static tree
12731build_cast (location, type, exp)
12732 int location;
12733 tree type, exp;
12734{
12735 tree node = build1 (CONVERT_EXPR, type, exp);
12736 EXPR_WFL_LINECOL (node) = location;
12737 return node;
12738}
12739
c2952b01
APB
12740/* Build an incomplete class reference operator. */
12741static tree
12742build_incomplete_class_ref (location, class_name)
12743 int location;
12744 tree class_name;
12745{
12746 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
12747 EXPR_WFL_LINECOL (node) = location;
12748 return node;
12749}
12750
12751/* Complete an incomplete class reference operator. */
12752static tree
12753patch_incomplete_class_ref (node)
12754 tree node;
12755{
12756 tree type = TREE_OPERAND (node, 0);
12757 tree ref_type;
12758
12759 if (!(ref_type = resolve_type_during_patch (type)))
12760 return error_mark_node;
12761
165f37bc
APB
12762 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
12763 return build_class_ref (ref_type);
12764
12765 /* If we're emitting class files and we have to deal with non
12766 primitive types, we invoke (and consider generating) the
12767 synthetic static method `class$'. */
12768 if (!TYPE_DOT_CLASS (current_class))
12769 build_dot_class_method (current_class);
12770 ref_type =
12771 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
12772 return java_complete_tree (ref_type);
c2952b01
APB
12773}
12774
e04a16fb
AG
12775/* 15.14 Unary operators. We return error_mark_node in case of error,
12776 but preserve the type of NODE if the type is fixed. */
12777
12778static tree
12779patch_unaryop (node, wfl_op)
12780 tree node;
12781 tree wfl_op;
12782{
12783 tree op = TREE_OPERAND (node, 0);
12784 tree op_type = TREE_TYPE (op);
ab3a6dd6 12785 tree prom_type = NULL_TREE, value, decl;
c2952b01 12786 int outer_field_flag = 0;
e04a16fb
AG
12787 int code = TREE_CODE (node);
12788 int error_found = 0;
12789
12790 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12791
12792 switch (code)
12793 {
12794 /* 15.13.2 Postfix Increment Operator ++ */
12795 case POSTINCREMENT_EXPR:
12796 /* 15.13.3 Postfix Increment Operator -- */
12797 case POSTDECREMENT_EXPR:
12798 /* 15.14.1 Prefix Increment Operator ++ */
12799 case PREINCREMENT_EXPR:
12800 /* 15.14.2 Prefix Decrement Operator -- */
12801 case PREDECREMENT_EXPR:
5cbdba64 12802 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
12803 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
12804 /* We might be trying to change an outer field accessed using
12805 access method. */
12806 if (outer_field_flag)
12807 {
12808 /* Retrieve the decl of the field we're trying to access. We
12809 do that by first retrieving the function we would call to
12810 access the field. It has been already verified that this
12811 field isn't final */
12812 if (flag_emit_class_files)
12813 decl = TREE_OPERAND (op, 0);
12814 else
12815 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
12816 decl = DECL_FUNCTION_ACCESS_DECL (decl);
12817 }
b3edebcf 12818 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 12819 else if (!JDECL_P (decl)
b3edebcf
APB
12820 && TREE_CODE (decl) != COMPONENT_REF
12821 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
12822 && TREE_CODE (decl) != INDIRECT_REF
12823 && !(TREE_CODE (decl) == COMPOUND_EXPR
12824 && TREE_OPERAND (decl, 1)
12825 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 12826 {
5e942c50
APB
12827 tree lvalue;
12828 /* Before screaming, check that we're not in fact trying to
12829 increment a optimized static final access, in which case
12830 we issue an different error message. */
12831 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
12832 && resolve_expression_name (wfl_op, &lvalue)
12833 && check_final_assignment (lvalue, wfl_op)))
12834 parse_error_context (wfl_operator, "Invalid argument to `%s'",
12835 operator_string (node));
e04a16fb
AG
12836 TREE_TYPE (node) = error_mark_node;
12837 error_found = 1;
12838 }
c2952b01
APB
12839
12840 if (check_final_assignment (op, wfl_op))
5e942c50
APB
12841 error_found = 1;
12842
e04a16fb
AG
12843 /* From now on, we know that op if a variable and that it has a
12844 valid wfl. We use wfl_op to locate errors related to the
12845 ++/-- operand. */
12846 else if (!JNUMERIC_TYPE_P (op_type))
12847 {
12848 parse_error_context
12849 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 12850 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
12851 TREE_TYPE (node) = error_mark_node;
12852 error_found = 1;
12853 }
12854 else
12855 {
4a5f66c3 12856 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
12857 both operands, if really necessary */
12858 if (JINTEGRAL_TYPE_P (op_type))
12859 {
12860 value = build_int_2 (1, 0);
12861 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
12862 }
12863 else
12864 {
12865 value = build_int_2 (1, 0);
12866 TREE_TYPE (node) =
12867 binary_numeric_promotion (op_type,
12868 TREE_TYPE (value), &op, &value);
12869 }
c2952b01
APB
12870
12871 /* We remember we might be accessing an outer field */
12872 if (outer_field_flag)
12873 {
12874 /* We re-generate an access to the field */
12875 value = build (PLUS_EXPR, TREE_TYPE (op),
12876 build_outer_field_access (wfl_op, decl), value);
12877
12878 /* And we patch the original access$() into a write
12879 with plus_op as a rhs */
12880 return outer_field_access_fix (node, op, value);
12881 }
12882
5cbdba64 12883 /* And write back into the node. */
4a5f66c3 12884 TREE_OPERAND (node, 0) = op;
e04a16fb 12885 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
12886 /* Convert the overall back into its original type, if
12887 necessary, and return */
12888 if (JINTEGRAL_TYPE_P (op_type))
12889 return fold (node);
12890 else
12891 return fold (convert (op_type, node));
e04a16fb
AG
12892 }
12893 break;
12894
12895 /* 15.14.3 Unary Plus Operator + */
b67d701b 12896 case UNARY_PLUS_EXPR:
e04a16fb
AG
12897 /* 15.14.4 Unary Minus Operator - */
12898 case NEGATE_EXPR:
12899 if (!JNUMERIC_TYPE_P (op_type))
12900 {
12901 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
12902 TREE_TYPE (node) = error_mark_node;
12903 error_found = 1;
12904 }
12905 /* Unary numeric promotion is performed on operand */
12906 else
12907 {
15fdcfe9
PB
12908 op = do_unary_numeric_promotion (op);
12909 prom_type = TREE_TYPE (op);
b67d701b 12910 if (code == UNARY_PLUS_EXPR)
4a5f66c3 12911 return fold (op);
e04a16fb
AG
12912 }
12913 break;
12914
12915 /* 15.14.5 Bitwise Complement Operator ~ */
12916 case BIT_NOT_EXPR:
12917 if (!JINTEGRAL_TYPE_P (op_type))
12918 {
12919 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
12920 TREE_TYPE (node) = error_mark_node;
12921 error_found = 1;
12922 }
12923 else
12924 {
15fdcfe9
PB
12925 op = do_unary_numeric_promotion (op);
12926 prom_type = TREE_TYPE (op);
e04a16fb
AG
12927 }
12928 break;
12929
12930 /* 15.14.6 Logical Complement Operator ! */
12931 case TRUTH_NOT_EXPR:
12932 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
12933 {
12934 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
12935 /* But the type is known. We will report an error if further
12936 attempt of a assignment is made with this rhs */
e04a16fb
AG
12937 TREE_TYPE (node) = boolean_type_node;
12938 error_found = 1;
12939 }
12940 else
12941 prom_type = boolean_type_node;
12942 break;
12943
12944 /* 15.15 Cast Expression */
12945 case CONVERT_EXPR:
0a2138e2 12946 value = patch_cast (node, wfl_operator);
e04a16fb 12947 if (value == error_mark_node)
c877974e
APB
12948 {
12949 /* If this cast is part of an assignment, we tell the code
12950 that deals with it not to complain about a mismatch,
12951 because things have been cast, anyways */
12952 TREE_TYPE (node) = error_mark_node;
12953 error_found = 1;
12954 }
12955 else
dc0b3eff
PB
12956 {
12957 value = fold (value);
12958 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
12959 return value;
12960 }
e04a16fb
AG
12961 break;
12962 }
12963
e04a16fb
AG
12964 if (error_found)
12965 return error_mark_node;
4a5f66c3
APB
12966
12967 /* There are cases where node has been replaced by something else
12968 and we don't end up returning here: UNARY_PLUS_EXPR,
12969 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 12970 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 12971 TREE_TYPE (node) = prom_type;
dc0b3eff 12972 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
12973 return fold (node);
12974}
12975
12976/* Generic type resolution that sometimes takes place during node
12977 patching. Returned the resolved type or generate an error
12978 message. Return the resolved type or NULL_TREE. */
12979
12980static tree
12981resolve_type_during_patch (type)
12982 tree type;
12983{
12984 if (unresolved_type_p (type, NULL))
12985 {
12986 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
12987 if (!type_decl)
12988 {
12989 parse_error_context (type,
12990 "Class `%s' not found in type declaration",
12991 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
12992 return NULL_TREE;
12993 }
12994 else
5e942c50
APB
12995 {
12996 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
12997 return TREE_TYPE (type_decl);
12998 }
e04a16fb
AG
12999 }
13000 return type;
13001}
13002/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13003 found. Otherwise NODE or something meant to replace it is returned. */
13004
13005static tree
0a2138e2 13006patch_cast (node, wfl_operator)
e04a16fb 13007 tree node;
e04a16fb
AG
13008 tree wfl_operator;
13009{
13010 tree op = TREE_OPERAND (node, 0);
13011 tree op_type = TREE_TYPE (op);
13012 tree cast_type = TREE_TYPE (node);
13013 char *t1;
13014
13015 /* First resolve OP_TYPE if unresolved */
13016 if (!(cast_type = resolve_type_during_patch (cast_type)))
13017 return error_mark_node;
13018
13019 /* Check on cast that are proven correct at compile time */
13020 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13021 {
e04a16fb
AG
13022 /* Same type */
13023 if (cast_type == op_type)
13024 return node;
13025
0b4d333e
APB
13026 /* float and double type are converted to the original type main
13027 variant and then to the target type. */
13028 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13029 op = convert (integer_type_node, op);
13030
e04a16fb
AG
13031 /* Try widening/narowwing convertion. Potentially, things need
13032 to be worked out in gcc so we implement the extreme cases
13033 correctly. fold_convert() needs to be fixed. */
13034 return convert (cast_type, op);
13035 }
13036
0b4d333e
APB
13037 /* It's also valid to cast a boolean into a boolean */
13038 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13039 return node;
13040
5e942c50
APB
13041 /* null can be casted to references */
13042 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13043 return build_null_of_type (cast_type);
13044
e04a16fb
AG
13045 /* The remaining legal casts involve conversion between reference
13046 types. Check for their compile time correctness. */
13047 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13048 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13049 {
13050 TREE_TYPE (node) = promote_type (cast_type);
13051 /* Now, the case can be determined correct at compile time if
13052 OP_TYPE can be converted into CAST_TYPE by assignment
13053 conversion (5.2) */
13054
13055 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13056 {
13057 TREE_SET_CODE (node, NOP_EXPR);
13058 return node;
13059 }
13060
13061 if (flag_emit_class_files)
13062 {
13063 TREE_SET_CODE (node, CONVERT_EXPR);
13064 return node;
13065 }
e04a16fb
AG
13066
13067 /* The cast requires a run-time check */
13068 return build (CALL_EXPR, promote_type (cast_type),
13069 build_address_of (soft_checkcast_node),
13070 tree_cons (NULL_TREE, build_class_ref (cast_type),
13071 build_tree_list (NULL_TREE, op)),
13072 NULL_TREE);
13073 }
13074
13075 /* Any other casts are proven incorrect at compile time */
c2e3db92 13076 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13077 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13078 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13079 free (t1);
13080 return error_mark_node;
13081}
13082
5e942c50
APB
13083/* Build a null constant and give it the type TYPE. */
13084
13085static tree
13086build_null_of_type (type)
13087 tree type;
13088{
13089 tree node = build_int_2 (0, 0);
13090 TREE_TYPE (node) = promote_type (type);
13091 return node;
13092}
13093
e04a16fb
AG
13094/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13095 a list of indices. */
13096static tree
13097build_array_ref (location, array, index)
13098 int location;
13099 tree array, index;
13100{
13101 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13102 EXPR_WFL_LINECOL (node) = location;
13103 return node;
13104}
13105
13106/* 15.12 Array Access Expression */
13107
13108static tree
c877974e
APB
13109patch_array_ref (node)
13110 tree node;
e04a16fb
AG
13111{
13112 tree array = TREE_OPERAND (node, 0);
13113 tree array_type = TREE_TYPE (array);
13114 tree index = TREE_OPERAND (node, 1);
13115 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13116 int error_found = 0;
13117
13118 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13119
e04a16fb
AG
13120 if (TREE_CODE (array_type) == POINTER_TYPE)
13121 array_type = TREE_TYPE (array_type);
13122
13123 /* The array reference must be an array */
13124 if (!TYPE_ARRAY_P (array_type))
13125 {
13126 parse_error_context
781b0558
KG
13127 (wfl_operator,
13128 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13129 lang_printable_name (array_type, 0));
e04a16fb
AG
13130 TREE_TYPE (node) = error_mark_node;
13131 error_found = 1;
13132 }
13133
c2952b01 13134 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13135 type must be int */
15fdcfe9
PB
13136 index = do_unary_numeric_promotion (index);
13137 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13138 {
1ebadc60 13139 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13140 parse_error_context (wfl_operator,
13141 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13142 lang_printable_name (index_type, 0));
13143 else
781b0558
KG
13144 parse_error_context (wfl_operator,
13145 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13146 lang_printable_name (index_type, 0));
e04a16fb
AG
13147 TREE_TYPE (node) = error_mark_node;
13148 error_found = 1;
13149 }
13150
e04a16fb
AG
13151 if (error_found)
13152 return error_mark_node;
e04a16fb 13153
5e942c50 13154 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13155
7f1d4866 13156 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13157 {
15fdcfe9
PB
13158 TREE_OPERAND (node, 0) = array;
13159 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13160 }
13161 else
939d7216
PB
13162 {
13163 /* The save_expr is for correct evaluation order. It would be cleaner
13164 to use force_evaluation_order (see comment there), but that is
13165 difficult when we also have to deal with bounds checking. */
13166 if (TREE_SIDE_EFFECTS (index))
13167 array = save_expr (array);
13168 node = build_java_arrayaccess (array, array_type, index);
13169 if (TREE_SIDE_EFFECTS (index))
13170 node = build (COMPOUND_EXPR, array_type, array, node);
13171 }
e04a16fb
AG
13172 TREE_TYPE (node) = array_type;
13173 return node;
13174}
13175
13176/* 15.9 Array Creation Expressions */
13177
13178static tree
13179build_newarray_node (type, dims, extra_dims)
13180 tree type;
13181 tree dims;
13182 int extra_dims;
13183{
13184 tree node =
b67d701b 13185 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13186 build_int_2 (extra_dims, 0));
e04a16fb
AG
13187 return node;
13188}
13189
13190static tree
13191patch_newarray (node)
13192 tree node;
13193{
13194 tree type = TREE_OPERAND (node, 0);
13195 tree dims = TREE_OPERAND (node, 1);
13196 tree cdim, array_type;
13197 int error_found = 0;
13198 int ndims = 0;
13199 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13200
13201 /* Dimension types are verified. It's better for the types to be
13202 verified in order. */
13203 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13204 {
13205 int dim_error = 0;
13206 tree dim = TREE_VALUE (cdim);
13207
13208 /* Dim might have been saved during its evaluation */
13209 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13210
13211 /* The type of each specified dimension must be an integral type. */
13212 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13213 dim_error = 1;
13214
13215 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13216 promoted type must be int. */
13217 else
13218 {
15fdcfe9 13219 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13220 if (TREE_TYPE (dim) != int_type_node)
13221 dim_error = 1;
13222 }
13223
13224 /* Report errors on types here */
13225 if (dim_error)
13226 {
13227 parse_error_context
13228 (TREE_PURPOSE (cdim),
781b0558 13229 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13230 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13231 "Explicit cast needed to" : "Can't"),
0a2138e2 13232 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13233 error_found = 1;
13234 }
13235
e04a16fb
AG
13236 TREE_PURPOSE (cdim) = NULL_TREE;
13237 }
13238
13239 /* Resolve array base type if unresolved */
13240 if (!(type = resolve_type_during_patch (type)))
13241 error_found = 1;
13242
13243 if (error_found)
13244 {
13245 /* We don't want further evaluation of this bogus array creation
13246 operation */
13247 TREE_TYPE (node) = error_mark_node;
13248 return error_mark_node;
13249 }
13250
15fdcfe9
PB
13251 /* Set array_type to the actual (promoted) array type of the result. */
13252 if (TREE_CODE (type) == RECORD_TYPE)
13253 type = build_pointer_type (type);
13254 while (--xdims >= 0)
13255 {
13256 type = promote_type (build_java_array_type (type, -1));
13257 }
13258 dims = nreverse (dims);
13259 array_type = type;
13260 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13261 {
13262 type = array_type;
05bccae2
RK
13263 array_type
13264 = build_java_array_type (type,
13265 TREE_CODE (cdim) == INTEGER_CST
13266 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13267 : -1);
15fdcfe9
PB
13268 array_type = promote_type (array_type);
13269 }
13270 dims = nreverse (dims);
13271
e04a16fb
AG
13272 /* The node is transformed into a function call. Things are done
13273 differently according to the number of dimensions. If the number
13274 of dimension is equal to 1, then the nature of the base type
13275 (primitive or not) matters. */
15fdcfe9 13276 if (ndims == 1)
fdec99c6 13277 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13278
e04a16fb
AG
13279 /* Can't reuse what's already written in expr.c because it uses the
13280 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13281 return build (CALL_EXPR, array_type,
e04a16fb 13282 build_address_of (soft_multianewarray_node),
15fdcfe9 13283 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13284 tree_cons (NULL_TREE,
15fdcfe9 13285 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13286 NULL_TREE);
13287}
13288
f8976021
APB
13289/* 10.6 Array initializer. */
13290
13291/* Build a wfl for array element that don't have one, so we can
13292 pin-point errors. */
13293
13294static tree
13295maybe_build_array_element_wfl (node)
13296 tree node;
13297{
13298 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13299 return build_expr_wfl (NULL_TREE, ctxp->filename,
13300 ctxp->elc.line, ctxp->elc.prev_col);
13301 else
13302 return NULL_TREE;
13303}
13304
13305/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13306 identification of initialized arrays easier to detect during walk
13307 and expansion. */
13308
13309static tree
13310build_new_array_init (location, values)
13311 int location;
13312 tree values;
13313{
13314 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13315 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13316 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13317 return to_return;
13318}
13319
13320/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13321 occurred. Otherwise return NODE after having set its type
13322 appropriately. */
13323
13324static tree
13325patch_new_array_init (type, node)
13326 tree type, node;
f8976021
APB
13327{
13328 int error_seen = 0;
fdec99c6 13329 tree current, element_type;
f8976021 13330 HOST_WIDE_INT length;
fdec99c6
PB
13331 int all_constant = 1;
13332 tree init = TREE_OPERAND (node, 0);
f8976021 13333
fdec99c6
PB
13334 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13335 {
13336 parse_error_context (node,
13337 "Invalid array initializer for non-array type `%s'",
13338 lang_printable_name (type, 1));
13339 return error_mark_node;
13340 }
13341 type = TREE_TYPE (type);
13342 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13343
fdec99c6
PB
13344 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13345
13346 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13347 current; length++, current = TREE_CHAIN (current))
f8976021 13348 {
fdec99c6
PB
13349 tree elt = TREE_VALUE (current);
13350 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13351 {
fdec99c6 13352 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13353 elt = TREE_VALUE (current);
13354 /* When compiling to native code, STRING_CST is converted to
13355 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13356 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13357 all_constant = 0;
f8976021 13358 }
fdec99c6
PB
13359 else
13360 {
13361 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13362 TREE_PURPOSE (current) = NULL_TREE;
13363 all_constant = 0;
13364 }
13365 if (elt && TREE_VALUE (elt) == error_mark_node)
13366 error_seen = 1;
f8976021
APB
13367 }
13368
13369 if (error_seen)
13370 return error_mark_node;
13371
13372 /* Create a new type. We can't reuse the one we have here by
13373 patching its dimension because it originally is of dimension -1
13374 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13375 type = build_java_array_type (element_type, length);
13376 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13377 TREE_TYPE (node) = promote_type (type);
13378 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13379 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13380 return node;
13381}
13382
13383/* Verify that one entry of the initializer element list can be
13384 assigned to the array base type. Report 1 if an error occurred, 0
13385 otherwise. */
13386
13387static int
13388array_constructor_check_entry (type, entry)
13389 tree type, entry;
13390{
13391 char *array_type_string = NULL; /* For error reports */
13392 tree value, type_value, new_value, wfl_value, patched;
13393 int error_seen = 0;
13394
13395 new_value = NULL_TREE;
13396 wfl_value = TREE_VALUE (entry);
13397
100f7cd8 13398 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13399 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13400 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13401 if ((patched = patch_string (value)))
13402 value = patched;
1179ebc2
APB
13403 if (value == error_mark_node)
13404 return 1;
f8976021 13405
f8976021
APB
13406 type_value = TREE_TYPE (value);
13407
1179ebc2 13408 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13409 constant overflow during narrowing. */
13410 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13411 new_value = try_builtin_assignconv (wfl_operator, type, value);
13412 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13413 type_value = promote_type (type);
100f7cd8
APB
13414
13415 pop_obstacks ();
f8976021
APB
13416 /* Check and report errors */
13417 if (!new_value)
13418 {
49f48c71 13419 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13420 "Can't" : "Explicit cast needed to");
13421 if (!array_type_string)
c2e3db92 13422 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13423 parse_error_context
13424 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13425 msg, lang_printable_name (type_value, 1), array_type_string);
13426 error_seen = 1;
13427 }
13428
13429 if (new_value)
13430 {
13431 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
13432 TREE_VALUE (entry) = new_value;
13433 }
13434
13435 if (array_type_string)
13436 free (array_type_string);
13437
13438 TREE_PURPOSE (entry) = NULL_TREE;
13439 return error_seen;
13440}
13441
e04a16fb
AG
13442static tree
13443build_this (location)
13444 int location;
13445{
9ee9b555 13446 tree node = build_wfl_node (this_identifier_node);
b67d701b 13447 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13448 EXPR_WFL_LINECOL (node) = location;
13449 return node;
13450}
13451
13452/* 14.15 The return statement. It builds a modify expression that
13453 assigns the returned value to the RESULT_DECL that hold the value
13454 to be returned. */
13455
13456static tree
13457build_return (location, op)
13458 int location;
13459 tree op;
13460{
13461 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13462 EXPR_WFL_LINECOL (node) = location;
b67d701b 13463 node = build_debugable_stmt (location, node);
e04a16fb
AG
13464 return node;
13465}
13466
13467static tree
13468patch_return (node)
13469 tree node;
13470{
13471 tree return_exp = TREE_OPERAND (node, 0);
13472 tree meth = current_function_decl;
13473 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13474 int error_found = 0;
13475
13476 TREE_TYPE (node) = error_mark_node;
13477 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13478
13479 /* It's invalid to have a return value within a function that is
13480 declared with the keyword void or that is a constructor */
13481 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13482 error_found = 1;
13483
f099f336 13484 /* It's invalid to use a return statement in a static block */
c2952b01 13485 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13486 error_found = 1;
13487
e04a16fb
AG
13488 /* It's invalid to have a no return value within a function that
13489 isn't declared with the keyword `void' */
13490 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13491 error_found = 2;
c2952b01
APB
13492
13493 if (in_instance_initializer)
13494 error_found = 1;
e04a16fb
AG
13495
13496 if (error_found)
13497 {
c2952b01 13498 if (in_instance_initializer)
f099f336 13499 parse_error_context (wfl_operator,
c2952b01
APB
13500 "`return' inside instance initializer");
13501
13502 else if (DECL_CLINIT_P (current_function_decl))
13503 parse_error_context (wfl_operator,
13504 "`return' inside static initializer");
f099f336
APB
13505
13506 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13507 {
c2e3db92 13508 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13509 parse_error_context (wfl_operator,
13510 "`return' with%s value from `%s %s'",
13511 (error_found == 1 ? "" : "out"),
13512 t, lang_printable_name (meth, 0));
13513 free (t);
13514 }
13515 else
13516 parse_error_context (wfl_operator,
13517 "`return' with value from constructor `%s'",
13518 lang_printable_name (meth, 0));
e04a16fb
AG
13519 return error_mark_node;
13520 }
13521
5e942c50
APB
13522 /* If we have a return_exp, build a modify expression and expand
13523 it. Note: at that point, the assignment is declared valid, but we
13524 may want to carry some more hacks */
e04a16fb
AG
13525 if (return_exp)
13526 {
5e942c50
APB
13527 tree exp = java_complete_tree (return_exp);
13528 tree modify, patched;
13529
13530 /* If the function returned value and EXP are booleans, EXP has
13531 to be converted into the type of DECL_RESULT, which is integer
13532 (see complete_start_java_method) */
13533 if (TREE_TYPE (exp) == boolean_type_node &&
13534 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13535 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13536
13537 /* `null' can be assigned to a function returning a reference */
13538 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13539 exp == null_pointer_node)
13540 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13541
13542 if ((patched = patch_string (exp)))
13543 exp = patched;
13544
13545 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
13546 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13547 modify = java_complete_tree (modify);
5e942c50 13548
e04a16fb
AG
13549 if (modify != error_mark_node)
13550 {
13551 TREE_SIDE_EFFECTS (modify) = 1;
13552 TREE_OPERAND (node, 0) = modify;
13553 }
13554 else
13555 return error_mark_node;
13556 }
13557 TREE_TYPE (node) = void_type_node;
13558 TREE_SIDE_EFFECTS (node) = 1;
13559 return node;
13560}
13561
13562/* 14.8 The if Statement */
13563
13564static tree
13565build_if_else_statement (location, expression, if_body, else_body)
13566 int location;
13567 tree expression, if_body, else_body;
13568{
13569 tree node;
e04a16fb 13570 if (!else_body)
9bbc7d9f 13571 else_body = empty_stmt_node;
e04a16fb
AG
13572 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13573 EXPR_WFL_LINECOL (node) = location;
b67d701b 13574 node = build_debugable_stmt (location, node);
e04a16fb
AG
13575 return node;
13576}
13577
13578static tree
13579patch_if_else_statement (node)
13580 tree node;
13581{
13582 tree expression = TREE_OPERAND (node, 0);
13583
13584 TREE_TYPE (node) = error_mark_node;
13585 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13586
13587 /* The type of expression must be boolean */
b67d701b
PB
13588 if (TREE_TYPE (expression) != boolean_type_node
13589 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
13590 {
13591 parse_error_context
13592 (wfl_operator,
13593 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 13594 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
13595 return error_mark_node;
13596 }
13597
13598 TREE_TYPE (node) = void_type_node;
13599 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 13600 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
13601 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13602 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
13603 return node;
13604}
13605
13606/* 14.6 Labeled Statements */
13607
13608/* Action taken when a lableled statement is parsed. a new
13609 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 13610 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
13611
13612static tree
0a2138e2 13613build_labeled_block (location, label)
e04a16fb 13614 int location;
0a2138e2 13615 tree label;
e04a16fb 13616{
b635eb2f 13617 tree label_name ;
e04a16fb 13618 tree label_decl, node;
b635eb2f
PB
13619 if (label == NULL_TREE || label == continue_identifier_node)
13620 label_name = label;
13621 else
e04a16fb 13622 {
b635eb2f
PB
13623 label_name = merge_qualified_name (label_id, label);
13624 /* Issue an error if we try to reuse a label that was previously
13625 declared */
13626 if (IDENTIFIER_LOCAL_VALUE (label_name))
13627 {
13628 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
13629 parse_error_context (wfl_operator,
13630 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
13631 IDENTIFIER_POINTER (label));
13632 EXPR_WFL_LINECOL (wfl_operator) =
13633 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
13634 parse_error_context (wfl_operator,
13635 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
13636 IDENTIFIER_POINTER (label));
13637 java_error_count--;
13638 }
e04a16fb
AG
13639 }
13640
13641 label_decl = create_label_decl (label_name);
13642 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
13643 EXPR_WFL_LINECOL (node) = location;
13644 TREE_SIDE_EFFECTS (node) = 1;
13645 return node;
13646}
13647
b67d701b 13648/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
13649
13650static tree
b635eb2f 13651finish_labeled_statement (lbe, statement)
e04a16fb
AG
13652 tree lbe; /* Labeled block expr */
13653 tree statement;
13654{
13655 /* In anyways, tie the loop to its statement */
13656 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
13657 pop_labeled_block ();
13658 POP_LABELED_BLOCK ();
e04a16fb
AG
13659 return lbe;
13660}
13661
13662/* 14.10, 14.11, 14.12 Loop Statements */
13663
13664/* Create an empty LOOP_EXPR and make it the last in the nested loop
13665 list. */
13666
13667static tree
13668build_new_loop (loop_body)
13669 tree loop_body;
13670{
13671 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
13672 TREE_SIDE_EFFECTS (loop) = 1;
13673 PUSH_LOOP (loop);
13674 return loop;
13675}
13676
13677/* Create a loop body according to the following structure:
13678 COMPOUND_EXPR
13679 COMPOUND_EXPR (loop main body)
13680 EXIT_EXPR (this order is for while/for loops.
13681 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 13682 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
13683 BODY end of this labeled block)
13684 INCREMENT (if any)
13685
13686 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
13687 after the body, like in the do-while loop.
13688
13689 To obtain a loop, the loop body structure described above is
13690 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
13691
13692 LABELED_BLOCK_EXPR
13693 LABEL_DECL (use this label to exit the loop)
13694 LOOP_EXPR
13695 <structure described above> */
e04a16fb
AG
13696
13697static tree
13698build_loop_body (location, condition, reversed)
13699 int location;
13700 tree condition;
13701 int reversed;
13702{
0a2138e2 13703 tree first, second, body;
e04a16fb
AG
13704
13705 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
13706 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
13707 condition = build_debugable_stmt (location, condition);
13708 TREE_SIDE_EFFECTS (condition) = 1;
13709
b635eb2f 13710 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
13711 first = (reversed ? body : condition);
13712 second = (reversed ? condition : body);
13713 return
13714 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 13715 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
13716}
13717
13718/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
13719 their order) on the current loop. Unlink the current loop from the
13720 loop list. */
13721
13722static tree
b635eb2f 13723finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
13724 int location;
13725 tree condition, body;
13726 int reversed;
13727{
13728 tree to_return = ctxp->current_loop;
13729 tree loop_body = LOOP_EXPR_BODY (to_return);
13730 if (condition)
13731 {
13732 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
13733 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
13734 The real EXIT_EXPR is one operand further. */
13735 EXPR_WFL_LINECOL (cnode) = location;
13736 /* This one is for accurate error reports */
13737 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
13738 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
13739 }
13740 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
13741 POP_LOOP ();
13742 return to_return;
13743}
13744
b635eb2f 13745/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
13746 loops feature the condition part */
13747
13748static tree
b635eb2f 13749finish_for_loop (location, condition, update, body)
e04a16fb
AG
13750 int location;
13751 tree condition, update, body;
13752{
13753 /* Put the condition and the loop body in place */
b635eb2f 13754 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
13755 /* LOOP is the current loop which has been now popped of the loop
13756 stack. Install the update block */
13757 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
13758 return loop;
13759}
13760
5cbdba64
APB
13761/* Try to find the loop a block might be related to. This comprises
13762 the case where the LOOP_EXPR is found as the second operand of a
13763 COMPOUND_EXPR, because the loop happens to have an initialization
13764 part, then expressed as the first operand of the COMPOUND_EXPR. If
13765 the search finds something, 1 is returned. Otherwise, 0 is
13766 returned. The search is assumed to start from a
13767 LABELED_BLOCK_EXPR's block. */
13768
13769static tree
13770search_loop (statement)
13771 tree statement;
13772{
13773 if (TREE_CODE (statement) == LOOP_EXPR)
13774 return statement;
13775
13776 if (TREE_CODE (statement) == BLOCK)
13777 statement = BLOCK_SUBBLOCKS (statement);
13778 else
13779 return NULL_TREE;
13780
13781 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13782 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13783 statement = TREE_OPERAND (statement, 1);
13784
13785 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 13786 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
13787}
13788
13789/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
13790 returned otherwise. */
13791
13792static int
13793labeled_block_contains_loop_p (block, loop)
13794 tree block, loop;
13795{
13796 if (!block)
13797 return 0;
13798
13799 if (LABELED_BLOCK_BODY (block) == loop)
13800 return 1;
13801
c2952b01 13802 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
13803 return 1;
13804
13805 return 0;
13806}
13807
e04a16fb 13808/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 13809 insert LOOP as its body. */
e04a16fb
AG
13810
13811static tree
13812patch_loop_statement (loop)
13813 tree loop;
13814{
cd9643f7 13815 tree loop_label;
5cbdba64 13816
cd9643f7 13817 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
13818 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
13819 return loop;
13820
cd9643f7 13821 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
13822 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
13823 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
13824 LABELED_BLOCK_BODY (loop_label) = loop;
13825 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 13826 return loop_label;
e04a16fb
AG
13827}
13828
13829/* 14.13, 14.14: break and continue Statements */
13830
13831/* Build a break or a continue statement. a null NAME indicates an
13832 unlabeled break/continue statement. */
13833
13834static tree
13835build_bc_statement (location, is_break, name)
13836 int location, is_break;
13837 tree name;
13838{
13839 tree break_continue, label_block_expr = NULL_TREE;
13840
13841 if (name)
13842 {
13843 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
13844 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
13845 /* Null means that we don't have a target for this named
13846 break/continue. In this case, we make the target to be the
13847 label name, so that the error can be reported accuratly in
13848 patch_bc_statement. */
13849 label_block_expr = EXPR_WFL_NODE (name);
13850 }
13851 /* Unlabeled break/continue will be handled during the
13852 break/continue patch operation */
13853 break_continue
13854 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
13855
13856 IS_BREAK_STMT_P (break_continue) = is_break;
13857 TREE_SIDE_EFFECTS (break_continue) = 1;
13858 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 13859 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
13860 return break_continue;
13861}
13862
13863/* Verification of a break/continue statement. */
13864
13865static tree
13866patch_bc_statement (node)
13867 tree node;
13868{
13869 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 13870 tree labeled_block = ctxp->current_labeled_block;
b67d701b 13871 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 13872
e04a16fb 13873 /* Having an identifier here means that the target is unknown. */
b635eb2f 13874 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
13875 {
13876 parse_error_context (wfl_operator, "No label definition found for `%s'",
13877 IDENTIFIER_POINTER (bc_label));
13878 return error_mark_node;
13879 }
b635eb2f 13880 if (! IS_BREAK_STMT_P (node))
e04a16fb 13881 {
b635eb2f
PB
13882 /* It's a continue statement. */
13883 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 13884 {
b635eb2f
PB
13885 if (labeled_block == NULL_TREE)
13886 {
13887 if (bc_label == NULL_TREE)
13888 parse_error_context (wfl_operator,
13889 "`continue' must be in loop");
13890 else
1504b2b4
APB
13891 parse_error_context
13892 (wfl_operator, "continue label `%s' does not name a loop",
13893 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
13894 return error_mark_node;
13895 }
13896 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
13897 == continue_identifier_node)
13898 && (bc_label == NULL_TREE
13899 || TREE_CHAIN (labeled_block) == bc_label))
13900 {
13901 bc_label = labeled_block;
13902 break;
13903 }
e04a16fb 13904 }
e04a16fb 13905 }
b635eb2f 13906 else if (!bc_label)
34f4db93 13907 {
b635eb2f 13908 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 13909 {
b635eb2f
PB
13910 if (labeled_block == NULL_TREE)
13911 {
13912 parse_error_context (wfl_operator,
13913 "`break' must be in loop or switch");
13914 return error_mark_node;
13915 }
13916 target_stmt = LABELED_BLOCK_BODY (labeled_block);
13917 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 13918 || search_loop (target_stmt))
b635eb2f
PB
13919 {
13920 bc_label = labeled_block;
13921 break;
13922 }
e04a16fb 13923 }
e04a16fb
AG
13924 }
13925
b635eb2f 13926 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
13927 CAN_COMPLETE_NORMALLY (bc_label) = 1;
13928
e04a16fb
AG
13929 /* Our break/continue don't return values. */
13930 TREE_TYPE (node) = void_type_node;
13931 /* Encapsulate the break within a compound statement so that it's
5cbdba64 13932 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
13933 sometimes, like after a if statement) */
13934 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
13935 TREE_SIDE_EFFECTS (node) = 1;
13936 return node;
13937}
13938
13939/* Process the exit expression belonging to a loop. Its type must be
13940 boolean. */
13941
13942static tree
13943patch_exit_expr (node)
13944 tree node;
13945{
13946 tree expression = TREE_OPERAND (node, 0);
13947 TREE_TYPE (node) = error_mark_node;
13948 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13949
13950 /* The type of expression must be boolean */
13951 if (TREE_TYPE (expression) != boolean_type_node)
13952 {
13953 parse_error_context
13954 (wfl_operator,
781b0558 13955 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 13956 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
13957 return error_mark_node;
13958 }
13959 /* Now we know things are allright, invert the condition, fold and
13960 return */
13961 TREE_OPERAND (node, 0) =
13962 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
13963
13964 if (! integer_zerop (TREE_OPERAND (node, 0))
13965 && ctxp->current_loop != NULL_TREE
13966 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
13967 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
13968 if (! integer_onep (TREE_OPERAND (node, 0)))
13969 CAN_COMPLETE_NORMALLY (node) = 1;
13970
13971
e04a16fb
AG
13972 TREE_TYPE (node) = void_type_node;
13973 return node;
13974}
b67d701b
PB
13975
13976/* 14.9 Switch statement */
13977
13978static tree
13979patch_switch_statement (node)
13980 tree node;
13981{
c877974e 13982 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
13983
13984 /* Complete the switch expression */
13985 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
13986 se_type = TREE_TYPE (se);
13987 /* The type of the switch expression must be char, byte, short or
13988 int */
13989 if (!JINTEGRAL_TYPE_P (se_type))
13990 {
13991 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
13992 parse_error_context (wfl_operator,
13993 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 13994 lang_printable_name (se_type, 0));
b67d701b
PB
13995 /* This is what java_complete_tree will check */
13996 TREE_OPERAND (node, 0) = error_mark_node;
13997 return error_mark_node;
13998 }
13999
15fdcfe9 14000 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14001
14002 /* Ready to return */
15fdcfe9 14003 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14004 {
14005 TREE_TYPE (node) = error_mark_node;
14006 return error_mark_node;
14007 }
14008 TREE_TYPE (node) = void_type_node;
14009 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14010 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14011 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14012 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14013 return node;
14014}
14015
165f37bc 14016/* 14.18 The try/catch statements */
b67d701b 14017
b67d701b 14018static tree
a7d8d81f 14019build_try_statement (location, try_block, catches)
b67d701b 14020 int location;
a7d8d81f
PB
14021 tree try_block, catches;
14022{
14023 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14024 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14025 return node;
b67d701b
PB
14026}
14027
a7d8d81f
PB
14028static tree
14029build_try_finally_statement (location, try_block, finally)
14030 int location;
14031 tree try_block, finally;
b67d701b 14032{
a7d8d81f
PB
14033 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14034 EXPR_WFL_LINECOL (node) = location;
14035 return node;
b67d701b
PB
14036}
14037
14038static tree
14039patch_try_statement (node)
14040 tree node;
14041{
14042 int error_found = 0;
14043 tree try = TREE_OPERAND (node, 0);
14044 /* Exception handlers are considered in left to right order */
14045 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14046 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14047
14048 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14049 to process the next catch clause. We process the catch clause before
14050 the try block so that when processing the try block we can check thrown
14051 exceptions againts the caught type list. */
b67d701b
PB
14052 for (current = catch; current; current = TREE_CHAIN (current))
14053 {
14054 tree carg_decl, carg_type;
14055 tree sub_current, catch_block, catch_clause;
14056 int unreachable;
14057
b67d701b 14058 /* At this point, the structure of the catch clause is
b67d701b
PB
14059 CATCH_EXPR (catch node)
14060 BLOCK (with the decl of the parameter)
14061 COMPOUND_EXPR
7525cc04 14062 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14063 BLOCK (catch clause block)
a7d8d81f
PB
14064 */
14065 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14066 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14067 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14068
14069 /* Catch clauses can't have more than one parameter declared,
14070 but it's already enforced by the grammar. Make sure that the
14071 only parameter of the clause statement in of class Throwable
14072 or a subclass of Throwable, but that was done earlier. The
14073 catch clause parameter type has also been resolved. */
14074
14075 /* Just make sure that the catch clause parameter type inherits
14076 from java.lang.Throwable */
14077 if (!inherits_from_p (carg_type, throwable_type_node))
14078 {
14079 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14080 parse_error_context (wfl_operator,
781b0558 14081 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14082 lang_printable_name (carg_type, 0));
b67d701b
PB
14083 error_found = 1;
14084 continue;
14085 }
14086
14087 /* Partial check for unreachable catch statement: The catch
14088 clause is reachable iff is no earlier catch block A in
14089 the try statement such that the type of the catch
14090 clause's parameter is the same as or a subclass of the
14091 type of A's parameter */
14092 unreachable = 0;
14093 for (sub_current = catch;
14094 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14095 {
14096 tree sub_catch_clause, decl;
a7d8d81f 14097 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14098 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14099
14100 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14101 {
14102 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14103 parse_error_context
781b0558
KG
14104 (wfl_operator,
14105 "`catch' not reached because of the catch clause at line %d",
14106 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14107 unreachable = error_found = 1;
14108 break;
14109 }
14110 }
b67d701b
PB
14111 /* Complete the catch clause block */
14112 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14113 if (catch_block == error_mark_node)
14114 {
14115 error_found = 1;
14116 continue;
14117 }
15fdcfe9
PB
14118 if (CAN_COMPLETE_NORMALLY (catch_block))
14119 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14120 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14121
14122 if (unreachable)
14123 continue;
14124
14125 /* Things to do here: the exception must be thrown */
14126
14127 /* Link this type to the caught type list */
14128 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14129 }
14130
b9f7e36c
APB
14131 PUSH_EXCEPTIONS (caught_type_list);
14132 if ((try = java_complete_tree (try)) == error_mark_node)
14133 error_found = 1;
15fdcfe9
PB
14134 if (CAN_COMPLETE_NORMALLY (try))
14135 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14136 POP_EXCEPTIONS ();
14137
b67d701b
PB
14138 /* Verification ends here */
14139 if (error_found)
14140 return error_mark_node;
14141
14142 TREE_OPERAND (node, 0) = try;
14143 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14144 TREE_TYPE (node) = void_type_node;
14145 return node;
14146}
b9f7e36c
APB
14147
14148/* 14.17 The synchronized Statement */
14149
14150static tree
14151patch_synchronized_statement (node, wfl_op1)
14152 tree node, wfl_op1;
14153{
5a005d9e 14154 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14155 tree block = TREE_OPERAND (node, 1);
5a005d9e 14156
d8fccff5 14157 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14158
14159 if (expr == error_mark_node)
14160 {
14161 block = java_complete_tree (block);
14162 return expr;
14163 }
b9f7e36c
APB
14164
14165 /* The TYPE of expr must be a reference type */
5a005d9e 14166 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14167 {
14168 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14169 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14170 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14171 return error_mark_node;
14172 }
14173
ce6e9147
APB
14174 if (flag_emit_xref)
14175 {
14176 TREE_OPERAND (node, 0) = expr;
14177 TREE_OPERAND (node, 1) = java_complete_tree (block);
14178 CAN_COMPLETE_NORMALLY (node) = 1;
14179 return node;
14180 }
14181
b9f7e36c
APB
14182 /* Generate a try-finally for the synchronized statement, except
14183 that the handler that catches all throw exception calls
14184 _Jv_MonitorExit and then rethrow the exception.
14185 The synchronized statement is then implemented as:
14186 TRY
14187 {
14188 _Jv_MonitorEnter (expression)
14189 synchronized_block
14190 _Jv_MonitorExit (expression)
14191 }
14192 CATCH_ALL
14193 {
14194 e = _Jv_exception_info ();
14195 _Jv_MonitorExit (expression)
14196 Throw (e);
14197 } */
14198
5a005d9e
PB
14199 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14200 BUILD_MONITOR_ENTER (enter, expr_decl);
14201 BUILD_MONITOR_EXIT (exit, expr_decl);
14202 CAN_COMPLETE_NORMALLY (enter) = 1;
14203 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14204 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14205 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14206 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14207 build (COMPOUND_EXPR, NULL_TREE,
14208 build (WITH_CLEANUP_EXPR, NULL_TREE,
14209 build (COMPOUND_EXPR, NULL_TREE,
96847892 14210 assignment, enter),
5a005d9e
PB
14211 NULL_TREE, exit),
14212 block));
14213 node = build_expr_block (node, expr_decl);
14214
14215 return java_complete_tree (node);
b9f7e36c
APB
14216}
14217
14218/* 14.16 The throw Statement */
14219
14220static tree
14221patch_throw_statement (node, wfl_op1)
14222 tree node, wfl_op1;
14223{
14224 tree expr = TREE_OPERAND (node, 0);
14225 tree type = TREE_TYPE (expr);
14226 int unchecked_ok = 0, tryblock_throws_ok = 0;
14227
14228 /* Thrown expression must be assignable to java.lang.Throwable */
14229 if (!try_reference_assignconv (throwable_type_node, expr))
14230 {
14231 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14232 parse_error_context (wfl_operator,
14233 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14234 lang_printable_name (type, 0));
b9f7e36c
APB
14235 /* If the thrown expression was a reference, we further the
14236 compile-time check. */
14237 if (!JREFERENCE_TYPE_P (type))
14238 return error_mark_node;
14239 }
14240
14241 /* At least one of the following must be true */
14242
14243 /* The type of the throw expression is a not checked exception,
14244 i.e. is a unchecked expression. */
c877974e 14245 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14246
c2952b01
APB
14247 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14248 /* An instance can't throw a checked excetion unless that exception
14249 is explicitely declared in the `throws' clause of each
14250 constructor. This doesn't apply to anonymous classes, since they
14251 don't have declared constructors. */
14252 if (!unchecked_ok
14253 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14254 {
14255 tree current;
14256 for (current = TYPE_METHODS (current_class); current;
14257 current = TREE_CHAIN (current))
14258 if (DECL_CONSTRUCTOR_P (current)
14259 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14260 {
14261 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)",
14262 lang_printable_name (TREE_TYPE (expr), 0));
14263 return error_mark_node;
14264 }
14265 }
14266
b9f7e36c
APB
14267 /* Throw is contained in a try statement and at least one catch
14268 clause can receive the thrown expression or the current method is
14269 declared to throw such an exception. Or, the throw statement is
14270 contained in a method or constructor declaration and the type of
14271 the Expression is assignable to at least one type listed in the
14272 throws clause the declaration. */
b9f7e36c 14273 if (!unchecked_ok)
f099f336 14274 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14275 if (!(unchecked_ok || tryblock_throws_ok))
14276 {
14277 /* If there is a surrounding try block that has no matching
14278 clatch clause, report it first. A surrounding try block exits
14279 only if there is something after the list of checked
14280 exception thrown by the current function (if any). */
14281 if (IN_TRY_BLOCK_P ())
781b0558 14282 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 14283 lang_printable_name (type, 0));
b9f7e36c
APB
14284 /* If we have no surrounding try statement and the method doesn't have
14285 any throws, report it now. FIXME */
f099f336
APB
14286
14287 /* We report that the exception can't be throw from a try block
14288 in all circumstances but when the `throw' is inside a static
14289 block. */
b9f7e36c
APB
14290 else if (!EXCEPTIONS_P (currently_caught_type_list)
14291 && !tryblock_throws_ok)
f099f336 14292 {
c2952b01 14293 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14294 parse_error_context (wfl_operator,
14295 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14296 lang_printable_name (type, 0));
14297 else
781b0558
KG
14298 parse_error_context (wfl_operator,
14299 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14300 lang_printable_name (type, 0));
14301 }
b9f7e36c
APB
14302 /* Otherwise, the current method doesn't have the appropriate
14303 throws declaration */
14304 else
781b0558 14305 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14306 lang_printable_name (type, 0));
b9f7e36c
APB
14307 return error_mark_node;
14308 }
14309
ce6e9147 14310 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14311 BUILD_THROW (node, expr);
ce6e9147
APB
14312
14313 /* If doing xrefs, keep the location where the `throw' was seen. */
14314 if (flag_emit_xref)
14315 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14316 return node;
14317}
14318
14319/* Check that exception said to be thrown by method DECL can be
14320 effectively caught from where DECL is invoked. */
14321
14322static void
14323check_thrown_exceptions (location, decl)
14324 int location;
14325 tree decl;
14326{
14327 tree throws;
14328 /* For all the unchecked exceptions thrown by DECL */
14329 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14330 throws = TREE_CHAIN (throws))
0a2138e2 14331 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14332 {
3e78f871
PB
14333#if 1
14334 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14335 if (DECL_NAME (decl) == get_identifier ("clone"))
14336 continue;
14337#endif
b9f7e36c 14338 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14339 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14340 parse_error_context
14341 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14342 lang_printable_name (TREE_VALUE (throws), 0));
14343 else
14344 {
14345 parse_error_context
781b0558 14346 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14347 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14348 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14349 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14350 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14351 }
b9f7e36c
APB
14352 }
14353}
14354
c877974e 14355/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14356 try-catch blocks, OR is listed in the `throws' clause of the
14357 current method. */
14358
14359static int
0a2138e2 14360check_thrown_exceptions_do (exception)
b9f7e36c
APB
14361 tree exception;
14362{
14363 tree list = currently_caught_type_list;
c877974e 14364 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14365 /* First, all the nested try-catch-finally at that stage. The
14366 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14367 if (IS_UNCHECKED_EXCEPTION_P (exception))
14368 return 1;
b9f7e36c
APB
14369 while (list)
14370 {
14371 tree caught;
14372 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14373 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14374 return 1;
14375 list = TREE_CHAIN (list);
14376 }
14377 return 0;
14378}
14379
14380static void
14381purge_unchecked_exceptions (mdecl)
14382 tree mdecl;
14383{
14384 tree throws = DECL_FUNCTION_THROWS (mdecl);
14385 tree new = NULL_TREE;
14386
14387 while (throws)
14388 {
14389 tree next = TREE_CHAIN (throws);
c877974e 14390 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14391 {
14392 TREE_CHAIN (throws) = new;
14393 new = throws;
14394 }
14395 throws = next;
14396 }
14397 /* List is inverted here, but it doesn't matter */
14398 DECL_FUNCTION_THROWS (mdecl) = new;
14399}
22eed1e6
APB
14400
14401/* 15.24 Conditional Operator ?: */
14402
14403static tree
14404patch_conditional_expr (node, wfl_cond, wfl_op1)
14405 tree node, wfl_cond, wfl_op1;
14406{
14407 tree cond = TREE_OPERAND (node, 0);
14408 tree op1 = TREE_OPERAND (node, 1);
14409 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14410 tree resulting_type = NULL_TREE;
ac825856 14411 tree t1, t2, patched;
22eed1e6
APB
14412 int error_found = 0;
14413
ac825856
APB
14414 /* Operands of ?: might be StringBuffers crafted as a result of a
14415 string concatenation. Obtain a descent operand here. */
14416 if ((patched = patch_string (op1)))
14417 TREE_OPERAND (node, 1) = op1 = patched;
14418 if ((patched = patch_string (op2)))
14419 TREE_OPERAND (node, 2) = op2 = patched;
14420
14421 t1 = TREE_TYPE (op1);
14422 t2 = TREE_TYPE (op2);
14423
22eed1e6
APB
14424 /* The first expression must be a boolean */
14425 if (TREE_TYPE (cond) != boolean_type_node)
14426 {
14427 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14428 parse_error_context (wfl_operator,
14429 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14430 lang_printable_name (TREE_TYPE (cond), 0));
14431 error_found = 1;
14432 }
14433
14434 /* Second and third can be numeric, boolean (i.e. primitive),
14435 references or null. Anything else results in an error */
14436 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14437 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14438 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14439 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14440 error_found = 1;
14441
14442 /* Determine the type of the conditional expression. Same types are
14443 easy to deal with */
14444 else if (t1 == t2)
14445 resulting_type = t1;
14446
14447 /* There are different rules for numeric types */
14448 else if (JNUMERIC_TYPE_P (t1))
14449 {
14450 /* if byte/short found, the resulting type is short */
14451 if ((t1 == byte_type_node && t2 == short_type_node)
14452 || (t1 == short_type_node && t2 == byte_type_node))
14453 resulting_type = short_type_node;
14454
14455 /* If t1 is a constant int and t2 is of type byte, short or char
14456 and t1's value fits in t2, then the resulting type is t2 */
14457 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14458 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14459 resulting_type = t2;
14460
14461 /* If t2 is a constant int and t1 is of type byte, short or char
14462 and t2's value fits in t1, then the resulting type is t1 */
14463 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14464 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14465 resulting_type = t1;
14466
14467 /* Otherwise, binary numeric promotion is applied and the
14468 resulting type is the promoted type of operand 1 and 2 */
14469 else
93024893 14470 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14471 &TREE_OPERAND (node, 1),
14472 &TREE_OPERAND (node, 2));
14473 }
14474
14475 /* Cases of a reference and a null type */
14476 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14477 resulting_type = t1;
14478
14479 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14480 resulting_type = t2;
14481
14482 /* Last case: different reference types. If a type can be converted
14483 into the other one by assignment conversion, the latter
14484 determines the type of the expression */
14485 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14486 resulting_type = promote_type (t1);
14487
14488 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14489 resulting_type = promote_type (t2);
14490
14491 /* If we don't have any resulting type, we're in trouble */
14492 if (!resulting_type)
14493 {
c2e3db92 14494 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14495 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14496 parse_error_context (wfl_operator,
14497 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14498 t, lang_printable_name (t2, 0));
22eed1e6
APB
14499 free (t);
14500 error_found = 1;
14501 }
14502
14503 if (error_found)
14504 {
14505 TREE_TYPE (node) = error_mark_node;
14506 return error_mark_node;
14507 }
14508
14509 TREE_TYPE (node) = resulting_type;
14510 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14511 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14512 return node;
14513}
ac825856 14514
5b09b33e
PB
14515/* Try to constant fold NODE.
14516 If NODE is not a constant expression, return NULL_EXPR.
14517 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14518
14519static tree
14520fold_constant_for_init (node, context)
14521 tree node;
14522 tree context;
14523{
14524 tree op0, op1, val;
14525 enum tree_code code = TREE_CODE (node);
14526
93024893 14527 if (code == STRING_CST)
5b09b33e 14528 return node;
93024893
APB
14529
14530 if (code == INTEGER_CST || code == REAL_CST)
14531 return convert (TREE_TYPE (context), node);
8576f094 14532 if (TREE_TYPE (node) != NULL_TREE && code != VAR_DECL && code != FIELD_DECL)
5b09b33e
PB
14533 return NULL_TREE;
14534
14535 switch (code)
14536 {
5b09b33e
PB
14537 case PLUS_EXPR:
14538 case MINUS_EXPR:
bc3ca41b
PB
14539 case MULT_EXPR:
14540 case TRUNC_MOD_EXPR:
14541 case RDIV_EXPR:
5b09b33e
PB
14542 case LSHIFT_EXPR:
14543 case RSHIFT_EXPR:
14544 case URSHIFT_EXPR:
14545 case BIT_AND_EXPR:
14546 case BIT_XOR_EXPR:
14547 case BIT_IOR_EXPR:
5b09b33e
PB
14548 case TRUTH_ANDIF_EXPR:
14549 case TRUTH_ORIF_EXPR:
14550 case EQ_EXPR:
14551 case NE_EXPR:
14552 case GT_EXPR:
14553 case GE_EXPR:
14554 case LT_EXPR:
14555 case LE_EXPR:
14556 op0 = TREE_OPERAND (node, 0);
14557 op1 = TREE_OPERAND (node, 1);
14558 val = fold_constant_for_init (op0, context);
14559 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14560 return NULL_TREE;
14561 TREE_OPERAND (node, 0) = val;
14562 val = fold_constant_for_init (op1, context);
14563 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14564 return NULL_TREE;
14565 TREE_OPERAND (node, 1) = val;
14566 return patch_binop (node, op0, op1);
14567
14568 case UNARY_PLUS_EXPR:
14569 case NEGATE_EXPR:
14570 case TRUTH_NOT_EXPR:
14571 case BIT_NOT_EXPR:
14572 case CONVERT_EXPR:
14573 op0 = TREE_OPERAND (node, 0);
14574 val = fold_constant_for_init (op0, context);
14575 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14576 return NULL_TREE;
14577 TREE_OPERAND (node, 0) = val;
5a005d9e 14578 return patch_unaryop (node, op0);
5b09b33e
PB
14579 break;
14580
14581 case COND_EXPR:
14582 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14583 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14584 return NULL_TREE;
14585 TREE_OPERAND (node, 0) = val;
14586 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14587 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14588 return NULL_TREE;
14589 TREE_OPERAND (node, 1) = val;
14590 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14591 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14592 return NULL_TREE;
14593 TREE_OPERAND (node, 2) = val;
14594 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14595 : TREE_OPERAND (node, 2);
14596
14597 case VAR_DECL:
8576f094
APB
14598 case FIELD_DECL:
14599 if (! FIELD_FINAL (node)
5b09b33e
PB
14600 || DECL_INITIAL (node) == NULL_TREE)
14601 return NULL_TREE;
14602 val = DECL_INITIAL (node);
14603 /* Guard against infinite recursion. */
14604 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 14605 val = fold_constant_for_init (val, node);
5b09b33e
PB
14606 DECL_INITIAL (node) = val;
14607 return val;
14608
14609 case EXPR_WITH_FILE_LOCATION:
14610 /* Compare java_complete_tree and resolve_expression_name. */
14611 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14612 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14613 {
14614 tree name = EXPR_WFL_NODE (node);
14615 tree decl;
14616 if (PRIMARY_P (node))
14617 return NULL_TREE;
14618 else if (! QUALIFIED_P (name))
14619 {
14620 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
14621 if (decl == NULL_TREE
14622 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
14623 return NULL_TREE;
14624 return fold_constant_for_init (decl, decl);
14625 }
14626 else
14627 {
5b09b33e
PB
14628 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14629 qualify_ambiguous_name (node);
14630 if (resolve_field_access (node, &decl, NULL)
14631 && decl != NULL_TREE)
14632 return fold_constant_for_init (decl, decl);
5b09b33e
PB
14633 return NULL_TREE;
14634 }
14635 }
14636 else
14637 {
14638 op0 = TREE_OPERAND (node, 0);
14639 val = fold_constant_for_init (op0, context);
14640 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14641 return NULL_TREE;
14642 TREE_OPERAND (node, 0) = val;
14643 return val;
14644 }
14645
bc3ca41b
PB
14646#ifdef USE_COMPONENT_REF
14647 case IDENTIFIER:
14648 case COMPONENT_REF:
14649 ?;
14650#endif
14651
5b09b33e
PB
14652 default:
14653 return NULL_TREE;
14654 }
14655}
bc3ca41b
PB
14656
14657#ifdef USE_COMPONENT_REF
14658/* Context is 'T' for TypeName, 'P' for PackageName,
14659 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
14660
14661tree
14662resolve_simple_name (name, context)
14663 tree name;
14664 int context;
14665{
14666}
14667
14668tree
14669resolve_qualified_name (name, context)
14670 tree name;
14671 int context;
14672{
14673}
14674#endif
This page took 2.261888 seconds and 5 git commands to generate.