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