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