]> gcc.gnu.org Git - gcc.git/blame - gcc/java/parse.y
* recog.c (peephole2_optimize): Use INSN_P.
[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 1328 $$ = exit_block ();
c280e37a
APB
1329 if (!BLOCK_SUBBLOCKS ($$))
1330 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
e04a16fb
AG
1331 }
1332;
1333
1334block_statements:
1335 block_statement
1336| block_statements block_statement
1337;
1338
1339block_statement:
1340 local_variable_declaration_statement
1341| statement
15fdcfe9 1342 { java_method_add_stmt (current_function_decl, $1); }
c2952b01
APB
1343| class_declaration /* Added, JDK1.1 local classes */
1344 {
1345 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1346 end_class_declaration (1);
1347 }
e04a16fb
AG
1348;
1349
1350local_variable_declaration_statement:
1351 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1352;
1353
1354local_variable_declaration:
1355 type variable_declarators
1356 { declare_local_variables (0, $1, $2); }
a003f638 1357| final type variable_declarators /* Added, JDK1.1 final locals */
e04a16fb
AG
1358 { declare_local_variables ($1, $2, $3); }
1359;
1360
1361statement:
1362 statement_without_trailing_substatement
1363| labeled_statement
e04a16fb 1364| if_then_statement
e04a16fb 1365| if_then_else_statement
e04a16fb 1366| while_statement
e04a16fb 1367| for_statement
cd9643f7 1368 { $$ = exit_block (); }
e04a16fb
AG
1369;
1370
1371statement_nsi:
1372 statement_without_trailing_substatement
1373| labeled_statement_nsi
e04a16fb 1374| if_then_else_statement_nsi
e04a16fb 1375| while_statement_nsi
e04a16fb 1376| for_statement_nsi
9dd939b2 1377 { $$ = exit_block (); }
e04a16fb
AG
1378;
1379
1380statement_without_trailing_substatement:
1381 block
e04a16fb 1382| empty_statement
e04a16fb 1383| expression_statement
e04a16fb 1384| switch_statement
e04a16fb 1385| do_statement
e04a16fb 1386| break_statement
e04a16fb 1387| continue_statement
e04a16fb
AG
1388| return_statement
1389| synchronized_statement
e04a16fb 1390| throw_statement
e04a16fb 1391| try_statement
e04a16fb
AG
1392;
1393
1394empty_statement:
1395 SC_TK
9bbc7d9f 1396 { $$ = empty_stmt_node; }
e04a16fb
AG
1397;
1398
1399label_decl:
1400 identifier REL_CL_TK
1401 {
1402 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
0a2138e2 1403 EXPR_WFL_NODE ($1));
e04a16fb
AG
1404 pushlevel (2);
1405 push_labeled_block ($$);
1406 PUSH_LABELED_BLOCK ($$);
1407 }
1408;
1409
1410labeled_statement:
1411 label_decl statement
b635eb2f 1412 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1413| identifier error
1414 {yyerror ("':' expected"); RECOVER;}
1415;
1416
1417labeled_statement_nsi:
1418 label_decl statement_nsi
b635eb2f 1419 { $$ = finish_labeled_statement ($1, $2); }
e04a16fb
AG
1420;
1421
1422/* We concentrate here a bunch of error handling rules that we couldn't write
1423 earlier, because expression_statement catches a missing ';'. */
1424expression_statement:
1425 statement_expression SC_TK
1426 {
1427 /* We have a statement. Generate a WFL around it so
1428 we can debug it */
1429 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1430 /* We know we have a statement, so set the debug
1431 info to be eventually generate here. */
1432 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1433 }
1434| error SC_TK
1435 {
1436 if (ctxp->prevent_ese != lineno)
1437 yyerror ("Invalid expression statement");
1438 DRECOVER (expr_stmt);
1439 }
1440| error OCB_TK
1441 {
1442 if (ctxp->prevent_ese != lineno)
1443 yyerror ("Invalid expression statement");
1444 DRECOVER (expr_stmt);
1445 }
1446| error CCB_TK
1447 {
1448 if (ctxp->prevent_ese != lineno)
1449 yyerror ("Invalid expression statement");
1450 DRECOVER (expr_stmt);
1451 }
1452| this_or_super OP_TK error
1453 {yyerror ("')' expected"); RECOVER;}
1454| this_or_super OP_TK CP_TK error
22eed1e6 1455 {
8119c720 1456 parse_ctor_invocation_error ();
22eed1e6
APB
1457 RECOVER;
1458 }
e04a16fb
AG
1459| this_or_super OP_TK argument_list error
1460 {yyerror ("')' expected"); RECOVER;}
1461| this_or_super OP_TK argument_list CP_TK error
22eed1e6 1462 {
8119c720 1463 parse_ctor_invocation_error ();
22eed1e6
APB
1464 RECOVER;
1465 }
e04a16fb
AG
1466| name DOT_TK SUPER_TK error
1467 {yyerror ("'(' expected"); RECOVER;}
1468| name DOT_TK SUPER_TK OP_TK error
1469 {yyerror ("')' expected"); RECOVER;}
1470| name DOT_TK SUPER_TK OP_TK argument_list error
1471 {yyerror ("')' expected"); RECOVER;}
1472| name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1473 {yyerror ("';' expected"); RECOVER;}
1474| name DOT_TK SUPER_TK OP_TK CP_TK error
1475 {yyerror ("';' expected"); RECOVER;}
1476;
1477
1478statement_expression:
1479 assignment
1480| pre_increment_expression
e04a16fb 1481| pre_decrement_expression
e04a16fb 1482| post_increment_expression
e04a16fb 1483| post_decrement_expression
e04a16fb
AG
1484| method_invocation
1485| class_instance_creation_expression
e04a16fb
AG
1486;
1487
1488if_then_statement:
1489 IF_TK OP_TK expression CP_TK statement
2aa11e97
APB
1490 {
1491 $$ = build_if_else_statement ($2.location, $3,
1492 $5, NULL_TREE);
1493 }
e04a16fb
AG
1494| IF_TK error
1495 {yyerror ("'(' expected"); RECOVER;}
1496| IF_TK OP_TK error
1497 {yyerror ("Missing term"); RECOVER;}
1498| IF_TK OP_TK expression error
1499 {yyerror ("')' expected"); RECOVER;}
1500;
1501
1502if_then_else_statement:
1503 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
2aa11e97 1504 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1505;
1506
1507if_then_else_statement_nsi:
1508 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
2aa11e97 1509 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
e04a16fb
AG
1510;
1511
1512switch_statement:
15fdcfe9
PB
1513 switch_expression
1514 {
1515 enter_block ();
1516 }
1517 switch_block
b67d701b 1518 {
15fdcfe9 1519 /* Make into "proper list" of COMPOUND_EXPRs.
f8976021
APB
1520 I.e. make the last statment also have its own
1521 COMPOUND_EXPR. */
15fdcfe9
PB
1522 maybe_absorb_scoping_blocks ();
1523 TREE_OPERAND ($1, 1) = exit_block ();
b67d701b
PB
1524 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1525 }
1526;
1527
1528switch_expression:
1529 SWITCH_TK OP_TK expression CP_TK
1530 {
1531 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1532 EXPR_WFL_LINECOL ($$) = $2.location;
1533 }
e04a16fb
AG
1534| SWITCH_TK error
1535 {yyerror ("'(' expected"); RECOVER;}
1536| SWITCH_TK OP_TK error
1537 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1538| SWITCH_TK OP_TK expression CP_TK error
1539 {yyerror ("'{' expected"); RECOVER;}
1540;
1541
f8976021
APB
1542/* Default assignment is there to avoid type node on switch_block
1543 node. */
1544
e04a16fb
AG
1545switch_block:
1546 OCB_TK CCB_TK
f8976021 1547 { $$ = NULL_TREE; }
e04a16fb 1548| OCB_TK switch_labels CCB_TK
f8976021 1549 { $$ = NULL_TREE; }
e04a16fb 1550| OCB_TK switch_block_statement_groups CCB_TK
f8976021 1551 { $$ = NULL_TREE; }
e04a16fb 1552| OCB_TK switch_block_statement_groups switch_labels CCB_TK
f8976021 1553 { $$ = NULL_TREE; }
e04a16fb
AG
1554;
1555
1556switch_block_statement_groups:
1557 switch_block_statement_group
1558| switch_block_statement_groups switch_block_statement_group
1559;
1560
1561switch_block_statement_group:
15fdcfe9 1562 switch_labels block_statements
e04a16fb
AG
1563;
1564
e04a16fb
AG
1565switch_labels:
1566 switch_label
1567| switch_labels switch_label
1568;
1569
1570switch_label:
1571 CASE_TK constant_expression REL_CL_TK
b67d701b 1572 {
15fdcfe9
PB
1573 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1574 EXPR_WFL_LINECOL (lab) = $1.location;
1575 java_method_add_stmt (current_function_decl, lab);
b67d701b 1576 }
e04a16fb 1577| DEFAULT_TK REL_CL_TK
b67d701b 1578 {
15fdcfe9
PB
1579 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1580 EXPR_WFL_LINECOL (lab) = $1.location;
1581 java_method_add_stmt (current_function_decl, lab);
b67d701b 1582 }
e04a16fb
AG
1583| CASE_TK error
1584 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1585| CASE_TK constant_expression error
1586 {yyerror ("':' expected"); RECOVER;}
1587| DEFAULT_TK error
1588 {yyerror ("':' expected"); RECOVER;}
1589;
1590
1591while_expression:
1592 WHILE_TK OP_TK expression CP_TK
1593 {
1594 tree body = build_loop_body ($2.location, $3, 0);
1595 $$ = build_new_loop (body);
1596 }
1597;
1598
1599while_statement:
1600 while_expression statement
b635eb2f 1601 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1602| WHILE_TK error
1603 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1604| WHILE_TK OP_TK error
1605 {yyerror ("Missing term and ')' expected"); RECOVER;}
1606| WHILE_TK OP_TK expression error
1607 {yyerror ("')' expected"); RECOVER;}
1608;
1609
1610while_statement_nsi:
1611 while_expression statement_nsi
b635eb2f 1612 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
e04a16fb
AG
1613;
1614
1615do_statement_begin:
1616 DO_TK
1617 {
1618 tree body = build_loop_body (0, NULL_TREE, 1);
1619 $$ = build_new_loop (body);
1620 }
1621 /* Need error handing here. FIXME */
1622;
1623
1624do_statement:
1625 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
b635eb2f 1626 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
e04a16fb
AG
1627;
1628
1629for_statement:
1630 for_begin SC_TK expression SC_TK for_update CP_TK statement
b635eb2f 1631 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7); }
e04a16fb
AG
1632| for_begin SC_TK SC_TK for_update CP_TK statement
1633 {
b635eb2f 1634 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1635 /* We have not condition, so we get rid of the EXIT_EXPR */
1636 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1637 empty_stmt_node;
e04a16fb
AG
1638 }
1639| for_begin SC_TK error
1640 {yyerror ("Invalid control expression"); RECOVER;}
1641| for_begin SC_TK expression SC_TK error
1642 {yyerror ("Invalid update expression"); RECOVER;}
1643| for_begin SC_TK SC_TK error
1644 {yyerror ("Invalid update expression"); RECOVER;}
1645;
1646
1647for_statement_nsi:
1648 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
b635eb2f 1649 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
e04a16fb
AG
1650| for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1651 {
b635eb2f 1652 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
e04a16fb
AG
1653 /* We have not condition, so we get rid of the EXIT_EXPR */
1654 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
9bbc7d9f 1655 empty_stmt_node;
e04a16fb
AG
1656 }
1657;
1658
1659for_header:
1660 FOR_TK OP_TK
1661 {
1662 /* This scope defined for local variable that may be
1663 defined within the scope of the for loop */
1664 enter_block ();
1665 }
1666| FOR_TK error
1667 {yyerror ("'(' expected"); DRECOVER(for_1);}
1668| FOR_TK OP_TK error
1669 {yyerror ("Invalid init statement"); RECOVER;}
1670;
1671
1672for_begin:
1673 for_header for_init
1674 {
1675 /* We now declare the loop body. The loop is
1676 declared as a for loop. */
1677 tree body = build_loop_body (0, NULL_TREE, 0);
1678 $$ = build_new_loop (body);
c2952b01 1679 FOR_LOOP_P ($$) = 1;
e04a16fb
AG
1680 /* The loop is added to the current block the for
1681 statement is defined within */
1682 java_method_add_stmt (current_function_decl, $$);
1683 }
1684;
1685for_init: /* Can be empty */
9bbc7d9f 1686 { $$ = empty_stmt_node; }
e04a16fb
AG
1687| statement_expression_list
1688 {
1689 /* Init statement recorded within the previously
1690 defined block scope */
1691 $$ = java_method_add_stmt (current_function_decl, $1);
1692 }
1693| local_variable_declaration
1694 {
1695 /* Local variable are recorded within the previously
1696 defined block scope */
1697 $$ = NULL_TREE;
1698 }
1699| statement_expression_list error
1700 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1701;
1702
1703for_update: /* Can be empty */
9bbc7d9f 1704 {$$ = empty_stmt_node;}
e04a16fb
AG
1705| statement_expression_list
1706 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1707;
1708
1709statement_expression_list:
1710 statement_expression
1711 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1712| statement_expression_list C_TK statement_expression
1713 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1714| statement_expression_list C_TK error
1715 {yyerror ("Missing term"); RECOVER;}
1716;
1717
1718break_statement:
1719 BREAK_TK SC_TK
1720 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1721| BREAK_TK identifier SC_TK
1722 { $$ = build_bc_statement ($1.location, 1, $2); }
1723| BREAK_TK error
1724 {yyerror ("Missing term"); RECOVER;}
1725| BREAK_TK identifier error
1726 {yyerror ("';' expected"); RECOVER;}
1727;
1728
1729continue_statement:
1730 CONTINUE_TK SC_TK
1731 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1732| CONTINUE_TK identifier SC_TK
1733 { $$ = build_bc_statement ($1.location, 0, $2); }
1734| CONTINUE_TK error
1735 {yyerror ("Missing term"); RECOVER;}
1736| CONTINUE_TK identifier error
1737 {yyerror ("';' expected"); RECOVER;}
1738;
1739
1740return_statement:
1741 RETURN_TK SC_TK
1742 { $$ = build_return ($1.location, NULL_TREE); }
1743| RETURN_TK expression SC_TK
1744 { $$ = build_return ($1.location, $2); }
1745| RETURN_TK error
1746 {yyerror ("Missing term"); RECOVER;}
1747| RETURN_TK expression error
1748 {yyerror ("';' expected"); RECOVER;}
1749;
1750
1751throw_statement:
1752 THROW_TK expression SC_TK
b9f7e36c
APB
1753 {
1754 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1755 EXPR_WFL_LINECOL ($$) = $1.location;
1756 }
e04a16fb
AG
1757| THROW_TK error
1758 {yyerror ("Missing term"); RECOVER;}
1759| THROW_TK expression error
1760 {yyerror ("';' expected"); RECOVER;}
1761;
1762
1763synchronized_statement:
1764 synchronized OP_TK expression CP_TK block
b9f7e36c
APB
1765 {
1766 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1767 EXPR_WFL_LINECOL ($$) =
1768 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1769 }
e04a16fb
AG
1770| synchronized OP_TK expression CP_TK error
1771 {yyerror ("'{' expected"); RECOVER;}
1772| synchronized error
1773 {yyerror ("'(' expected"); RECOVER;}
1774| synchronized OP_TK error CP_TK
1775 {yyerror ("Missing term"); RECOVER;}
1776| synchronized OP_TK error
1777 {yyerror ("Missing term"); RECOVER;}
1778;
1779
b9f7e36c 1780synchronized:
efa0a23f 1781 modifiers
e04a16fb 1782 {
781b0558
KG
1783 check_modifiers (
1784 "Illegal modifier `%s'. Only `synchronized' was expected here",
efa0a23f
APB
1785 $1, ACC_SYNCHRONIZED);
1786 if ($1 != ACC_SYNCHRONIZED)
1787 MODIFIER_WFL (SYNCHRONIZED_TK) =
1788 build_wfl_node (NULL_TREE);
e04a16fb
AG
1789 }
1790;
1791
1792try_statement:
1793 TRY_TK block catches
a7d8d81f 1794 { $$ = build_try_statement ($1.location, $2, $3); }
e04a16fb 1795| TRY_TK block finally
a7d8d81f 1796 { $$ = build_try_finally_statement ($1.location, $2, $3); }
e04a16fb 1797| TRY_TK block catches finally
2aa11e97
APB
1798 { $$ = build_try_finally_statement
1799 ($1.location, build_try_statement ($1.location,
1800 $2, $3), $4);
1801 }
e04a16fb
AG
1802| TRY_TK error
1803 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1804;
1805
1806catches:
1807 catch_clause
1808| catches catch_clause
b67d701b
PB
1809 {
1810 TREE_CHAIN ($2) = $1;
1811 $$ = $2;
1812 }
e04a16fb
AG
1813;
1814
1815catch_clause:
b67d701b
PB
1816 catch_clause_parameter block
1817 {
1818 java_method_add_stmt (current_function_decl, $2);
1819 exit_block ();
1820 $$ = $1;
1821 }
1822
1823catch_clause_parameter:
1824 CATCH_TK OP_TK formal_parameter CP_TK
1825 {
1826 /* We add a block to define a scope for
1827 formal_parameter (CCBP). The formal parameter is
1828 declared initialized by the appropriate function
1829 call */
1830 tree ccpb = enter_block ();
1831 tree init = build_assignment (ASSIGN_TK, $2.location,
1832 TREE_PURPOSE ($3),
1833 soft_exceptioninfo_call_node);
1834 declare_local_variables (0, TREE_VALUE ($3),
1835 build_tree_list (TREE_PURPOSE ($3),
1836 init));
1837 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1838 EXPR_WFL_LINECOL ($$) = $1.location;
1839 }
e04a16fb 1840| CATCH_TK error
97f30284 1841 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
e04a16fb 1842| CATCH_TK OP_TK error
97f30284
APB
1843 {
1844 yyerror ("Missing term or ')' expected");
1845 RECOVER; $$ = NULL_TREE;
1846 }
b67d701b 1847| CATCH_TK OP_TK error CP_TK /* That's for () */
97f30284 1848 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
e04a16fb
AG
1849;
1850
1851finally:
1852 FINALLY_TK block
a7d8d81f 1853 { $$ = $2; }
e04a16fb
AG
1854| FINALLY_TK error
1855 {yyerror ("'{' expected"); RECOVER; }
1856;
1857
1858/* 19.12 Production from 15: Expressions */
1859primary:
1860 primary_no_new_array
1861| array_creation_expression
1862;
1863
1864primary_no_new_array:
1865 literal
1866| THIS_TK
1867 { $$ = build_this ($1.location); }
1868| OP_TK expression CP_TK
1869 {$$ = $2;}
1870| class_instance_creation_expression
1871| field_access
1872| method_invocation
1873| array_access
c2952b01 1874| type_literals
e04a16fb
AG
1875 /* Added, JDK1.1 inner classes. Documentation is wrong
1876 refering to a 'ClassName' (class_name) rule that doesn't
c2952b01 1877 exist. Used name: instead. */
e04a16fb 1878| name DOT_TK THIS_TK
c2952b01
APB
1879 {
1880 tree wfl = build_wfl_node (this_identifier_node);
1881 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1882 }
e04a16fb
AG
1883| OP_TK expression error
1884 {yyerror ("')' expected"); RECOVER;}
1885| name DOT_TK error
1886 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1887| primitive_type DOT_TK error
1888 {yyerror ("'class' expected" ); RECOVER;}
1889| VOID_TK DOT_TK error
1890 {yyerror ("'class' expected" ); RECOVER;}
1891;
1892
c2952b01
APB
1893/* Added, JDK1.1 type literals. We can't use `type' directly, so we
1894 broke the rule down a bit. */
1895
1896array_type_literal:
1897 primitive_type OSB_TK CSB_TK
1898 {
1899 $$ = build_java_array_type ($1, -1);
1900 CLASS_LOADED_P ($$) = 1;
1901 }
1902| name OSB_TK CSB_TK
1903 { $$ = build_unresolved_array_type ($1); }
1904/* This triggers two reduce/reduce conflict between array_type_literal and
1905 dims. FIXME.
1906| array_type OSB_TK CSB_TK
1907 { $$ = build_unresolved_array_type ($1); }
1908*/
1909;
1910
1911type_literals:
1912 name DOT_TK CLASS_TK
1913 { $$ = build_incomplete_class_ref ($2.location, $1); }
1914| array_type_literal DOT_TK CLASS_TK
1915 { $$ = build_incomplete_class_ref ($2.location, $1); }
1916| primitive_type DOT_TK CLASS_TK
1917 { $$ = build_class_ref ($1); }
1918| VOID_TK DOT_TK CLASS_TK
1919 { $$ = build_class_ref (void_type_node); }
1920;
1921
e04a16fb
AG
1922class_instance_creation_expression:
1923 NEW_TK class_type OP_TK argument_list CP_TK
b67d701b 1924 { $$ = build_new_invocation ($2, $4); }
e04a16fb 1925| NEW_TK class_type OP_TK CP_TK
b67d701b 1926 { $$ = build_new_invocation ($2, NULL_TREE); }
c2952b01 1927| anonymous_class_creation
e04a16fb
AG
1928 /* Added, JDK1.1 inner classes, modified to use name or
1929 primary instead of primary solely which couldn't work in
1930 all situations. */
1931| something_dot_new identifier OP_TK CP_TK
c2952b01
APB
1932 {
1933 tree ctor = build_new_invocation ($2, NULL_TREE);
1934 $$ = make_qualified_primary ($1, ctor,
1935 EXPR_WFL_LINECOL ($1));
1936 }
e04a16fb
AG
1937| something_dot_new identifier OP_TK CP_TK class_body
1938| something_dot_new identifier OP_TK argument_list CP_TK
c2952b01
APB
1939 {
1940 tree ctor = build_new_invocation ($2, $4);
1941 $$ = make_qualified_primary ($1, ctor,
1942 EXPR_WFL_LINECOL ($1));
1943 }
e04a16fb
AG
1944| something_dot_new identifier OP_TK argument_list CP_TK class_body
1945| NEW_TK error SC_TK
1946 {yyerror ("'(' expected"); DRECOVER(new_1);}
1947| NEW_TK class_type error
1948 {yyerror ("'(' expected"); RECOVER;}
1949| NEW_TK class_type OP_TK error
1950 {yyerror ("')' or term expected"); RECOVER;}
1951| NEW_TK class_type OP_TK argument_list error
1952 {yyerror ("')' expected"); RECOVER;}
1953| something_dot_new error
1954 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1955| something_dot_new identifier error
1956 {yyerror ("'(' expected"); RECOVER;}
1957;
1958
c2952b01
APB
1959/* Created after JDK1.1 rules originally added to
1960 class_instance_creation_expression, but modified to use
1961 'class_type' instead of 'TypeName' (type_name) which is mentionned
1962 in the documentation but doesn't exist. */
1963
1964anonymous_class_creation:
1965 NEW_TK class_type OP_TK argument_list CP_TK
1966 { create_anonymous_class ($1.location, $2); }
1967 class_body
1968 {
1969 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1970 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1971
1972 end_class_declaration (1);
1973
1974 /* Now we can craft the new expression */
1975 $$ = build_new_invocation (id, $4);
1976
1977 /* Note that we can't possibly be here if
1978 `class_type' is an interface (in which case the
1979 anonymous class extends Object and implements
1980 `class_type', hence its constructor can't have
1981 arguments.) */
1982
1983 /* Otherwise, the innerclass must feature a
1984 constructor matching `argument_list'. Anonymous
1985 classes are a bit special: it's impossible to
1986 define constructor for them, hence constructors
1987 must be generated following the hints provided by
1988 the `new' expression. Whether a super constructor
1989 of that nature exists or not is to be verified
1990 later on in verify_constructor_super.
1991
1992 It's during the expansion of a `new' statement
1993 refering to an anonymous class that a ctor will
1994 be generated for the anonymous class, with the
1995 right arguments. */
1996
1997 }
1998| NEW_TK class_type OP_TK CP_TK
1999 { create_anonymous_class ($1.location, $2); }
2000 class_body
2001 {
2002 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2003 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2004
2005 end_class_declaration (1);
2006
2007 /* Now we can craft the new expression. The
2008 statement doesn't need to be remember so that a
2009 constructor can be generated, since its signature
2010 is already known. */
2011 $$ = build_new_invocation (id, NULL_TREE);
2012 }
2013;
2014
e04a16fb
AG
2015something_dot_new: /* Added, not part of the specs. */
2016 name DOT_TK NEW_TK
c2952b01 2017 { $$ = $1; }
e04a16fb 2018| primary DOT_TK NEW_TK
c2952b01 2019 { $$ = $1; }
e04a16fb
AG
2020;
2021
2022argument_list:
2023 expression
2024 {
2025 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2026 ctxp->formal_parameter_number = 1;
2027 }
2028| argument_list C_TK expression
2029 {
2030 ctxp->formal_parameter_number += 1;
2031 $$ = tree_cons (NULL_TREE, $3, $1);
2032 }
2033| argument_list C_TK error
2034 {yyerror ("Missing term"); RECOVER;}
2035;
2036
2037array_creation_expression:
2038 NEW_TK primitive_type dim_exprs
2039 { $$ = build_newarray_node ($2, $3, 0); }
2040| NEW_TK class_or_interface_type dim_exprs
2041 { $$ = build_newarray_node ($2, $3, 0); }
2042| NEW_TK primitive_type dim_exprs dims
ba179f9f 2043 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb 2044| NEW_TK class_or_interface_type dim_exprs dims
ba179f9f 2045 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
e04a16fb
AG
2046 /* Added, JDK1.1 anonymous array. Initial documentation rule
2047 modified */
2048| NEW_TK class_or_interface_type dims array_initializer
c2952b01
APB
2049 {
2050 char *sig;
2051 while (CURRENT_OSB (ctxp)--)
2052 obstack_1grow (&temporary_obstack, '[');
2053 sig = obstack_finish (&temporary_obstack);
2054 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2055 $2, get_identifier (sig), $4);
2056 }
e04a16fb 2057| NEW_TK primitive_type dims array_initializer
c2952b01
APB
2058 {
2059 tree type = $2;
2060 while (CURRENT_OSB (ctxp)--)
2061 type = build_java_array_type (type, -1);
2062 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2063 build_pointer_type (type), NULL_TREE, $4);
2064 }
e04a16fb
AG
2065| NEW_TK error CSB_TK
2066 {yyerror ("'[' expected"); DRECOVER ("]");}
2067| NEW_TK error OSB_TK
2068 {yyerror ("']' expected"); RECOVER;}
2069;
2070
2071dim_exprs:
2072 dim_expr
2073 { $$ = build_tree_list (NULL_TREE, $1); }
2074| dim_exprs dim_expr
2075 { $$ = tree_cons (NULL_TREE, $2, $$); }
2076;
2077
2078dim_expr:
2079 OSB_TK expression CSB_TK
2080 {
2081 EXPR_WFL_LINECOL ($2) = $1.location;
2082 $$ = $2;
2083 }
2084| OSB_TK expression error
2085 {yyerror ("']' expected"); RECOVER;}
2086| OSB_TK error
2087 {
2088 yyerror ("Missing term");
2089 yyerror ("']' expected");
2090 RECOVER;
2091 }
2092;
2093
2094dims:
2095 OSB_TK CSB_TK
ba179f9f
APB
2096 {
2097 int allocate = 0;
2098 /* If not initialized, allocate memory for the osb
2099 numbers stack */
2100 if (!ctxp->osb_limit)
2101 {
2102 allocate = ctxp->osb_limit = 32;
2103 ctxp->osb_depth = -1;
2104 }
c2952b01 2105 /* If capacity overflown, reallocate a bigger chunk */
ba179f9f
APB
2106 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2107 allocate = ctxp->osb_limit << 1;
2108
2109 if (allocate)
2110 {
2111 allocate *= sizeof (int);
2112 if (ctxp->osb_number)
2113 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2114 allocate);
2115 else
2116 ctxp->osb_number = (int *)xmalloc (allocate);
2117 }
2118 ctxp->osb_depth++;
2119 CURRENT_OSB (ctxp) = 1;
2120 }
e04a16fb 2121| dims OSB_TK CSB_TK
ba179f9f 2122 { CURRENT_OSB (ctxp)++; }
e04a16fb
AG
2123| dims OSB_TK error
2124 { yyerror ("']' expected"); RECOVER;}
2125;
2126
2127field_access:
2128 primary DOT_TK identifier
2129 { $$ = make_qualified_primary ($1, $3, $2.location); }
9bbc7d9f
PB
2130 /* FIXME - REWRITE TO:
2131 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
e04a16fb
AG
2132| SUPER_TK DOT_TK identifier
2133 {
2134 tree super_wfl =
9ee9b555 2135 build_wfl_node (super_identifier_node);
e04a16fb
AG
2136 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2137 $$ = make_qualified_name (super_wfl, $3, $2.location);
2138 }
2139| SUPER_TK error
2140 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2141;
2142
2143method_invocation:
2144 name OP_TK CP_TK
2145 { $$ = build_method_invocation ($1, NULL_TREE); }
2146| name OP_TK argument_list CP_TK
2147 { $$ = build_method_invocation ($1, $3); }
2148| primary DOT_TK identifier OP_TK CP_TK
2149 {
22eed1e6
APB
2150 if (TREE_CODE ($1) == THIS_EXPR)
2151 $$ = build_this_super_qualified_invocation
2152 (1, $3, NULL_TREE, 0, $2.location);
2153 else
2154 {
2155 tree invok = build_method_invocation ($3, NULL_TREE);
2156 $$ = make_qualified_primary ($1, invok, $2.location);
2157 }
e04a16fb
AG
2158 }
2159| primary DOT_TK identifier OP_TK argument_list CP_TK
2160 {
22eed1e6
APB
2161 if (TREE_CODE ($1) == THIS_EXPR)
2162 $$ = build_this_super_qualified_invocation
2163 (1, $3, $5, 0, $2.location);
2164 else
2165 {
2166 tree invok = build_method_invocation ($3, $5);
2167 $$ = make_qualified_primary ($1, invok, $2.location);
2168 }
e04a16fb
AG
2169 }
2170| SUPER_TK DOT_TK identifier OP_TK CP_TK
22eed1e6
APB
2171 {
2172 $$ = build_this_super_qualified_invocation
2173 (0, $3, NULL_TREE, $1.location, $2.location);
e04a16fb
AG
2174 }
2175| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2176 {
22eed1e6
APB
2177 $$ = build_this_super_qualified_invocation
2178 (0, $3, $5, $1.location, $2.location);
e04a16fb
AG
2179 }
2180 /* Screws up thing. I let it here until I'm convinced it can
2181 be removed. FIXME
2182| primary DOT_TK error
2183 {yyerror ("'(' expected"); DRECOVER(bad);} */
2184| SUPER_TK DOT_TK error CP_TK
2185 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2186| SUPER_TK DOT_TK error DOT_TK
2187 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2188;
2189
2190array_access:
2191 name OSB_TK expression CSB_TK
2192 { $$ = build_array_ref ($2.location, $1, $3); }
2193| primary_no_new_array OSB_TK expression CSB_TK
2194 { $$ = build_array_ref ($2.location, $1, $3); }
2195| name OSB_TK error
2196 {
2197 yyerror ("Missing term and ']' expected");
2198 DRECOVER(array_access);
2199 }
2200| name OSB_TK expression error
2201 {
2202 yyerror ("']' expected");
2203 DRECOVER(array_access);
2204 }
2205| primary_no_new_array OSB_TK error
2206 {
2207 yyerror ("Missing term and ']' expected");
2208 DRECOVER(array_access);
2209 }
2210| primary_no_new_array OSB_TK expression error
2211 {
2212 yyerror ("']' expected");
2213 DRECOVER(array_access);
2214 }
2215;
2216
2217postfix_expression:
2218 primary
2219| name
2220| post_increment_expression
2221| post_decrement_expression
2222;
2223
2224post_increment_expression:
2225 postfix_expression INCR_TK
2226 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2227;
2228
2229post_decrement_expression:
2230 postfix_expression DECR_TK
2231 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2232;
2233
2234unary_expression:
2235 pre_increment_expression
2236| pre_decrement_expression
2237| PLUS_TK unary_expression
2238 {$$ = build_unaryop ($1.token, $1.location, $2); }
2239| MINUS_TK unary_expression
2240 {$$ = build_unaryop ($1.token, $1.location, $2); }
2241| unary_expression_not_plus_minus
2242| PLUS_TK error
2243 {yyerror ("Missing term"); RECOVER}
2244| MINUS_TK error
2245 {yyerror ("Missing term"); RECOVER}
2246;
2247
2248pre_increment_expression:
2249 INCR_TK unary_expression
2250 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2251| INCR_TK error
2252 {yyerror ("Missing term"); RECOVER}
2253;
2254
2255pre_decrement_expression:
2256 DECR_TK unary_expression
2257 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2258| DECR_TK error
2259 {yyerror ("Missing term"); RECOVER}
2260;
2261
2262unary_expression_not_plus_minus:
2263 postfix_expression
2264| NOT_TK unary_expression
2265 {$$ = build_unaryop ($1.token, $1.location, $2); }
2266| NEG_TK unary_expression
2267 {$$ = build_unaryop ($1.token, $1.location, $2); }
2268| cast_expression
2269| NOT_TK error
2270 {yyerror ("Missing term"); RECOVER}
2271| NEG_TK error
2272 {yyerror ("Missing term"); RECOVER}
2273;
2274
2275cast_expression: /* Error handling here is potentially weak */
2276 OP_TK primitive_type dims CP_TK unary_expression
2277 {
2278 tree type = $2;
ba179f9f 2279 while (CURRENT_OSB (ctxp)--)
e04a16fb 2280 type = build_java_array_type (type, -1);
ba179f9f 2281 ctxp->osb_depth--;
e04a16fb
AG
2282 $$ = build_cast ($1.location, type, $5);
2283 }
2284| OP_TK primitive_type CP_TK unary_expression
2285 { $$ = build_cast ($1.location, $2, $4); }
2286| OP_TK expression CP_TK unary_expression_not_plus_minus
2287 { $$ = build_cast ($1.location, $2, $4); }
2288| OP_TK name dims CP_TK unary_expression_not_plus_minus
2289 {
49f48c71 2290 const char *ptr;
ba179f9f 2291 while (CURRENT_OSB (ctxp)--)
e04a16fb 2292 obstack_1grow (&temporary_obstack, '[');
ba179f9f 2293 ctxp->osb_depth--;
e04a16fb
AG
2294 obstack_grow0 (&temporary_obstack,
2295 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2296 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2297 ptr = obstack_finish (&temporary_obstack);
2298 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2299 $$ = build_cast ($1.location, $2, $5);
2300 }
2301| OP_TK primitive_type OSB_TK error
2302 {yyerror ("']' expected, invalid type expression");}
2303| OP_TK error
2304 {
2305 if (ctxp->prevent_ese != lineno)
2306 yyerror ("Invalid type expression"); RECOVER;
2307 RECOVER;
2308 }
2309| OP_TK primitive_type dims CP_TK error
2310 {yyerror ("Missing term"); RECOVER;}
2311| OP_TK primitive_type CP_TK error
2312 {yyerror ("Missing term"); RECOVER;}
2313| OP_TK name dims CP_TK error
2314 {yyerror ("Missing term"); RECOVER;}
2315;
2316
2317multiplicative_expression:
2318 unary_expression
2319| multiplicative_expression MULT_TK unary_expression
2320 {
2321 $$ = build_binop (BINOP_LOOKUP ($2.token),
2322 $2.location, $1, $3);
2323 }
2324| multiplicative_expression DIV_TK unary_expression
2325 {
2326 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2327 $1, $3);
2328 }
2329| multiplicative_expression REM_TK unary_expression
2330 {
2331 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2332 $1, $3);
2333 }
2334| multiplicative_expression MULT_TK error
2335 {yyerror ("Missing term"); RECOVER;}
2336| multiplicative_expression DIV_TK error
2337 {yyerror ("Missing term"); RECOVER;}
2338| multiplicative_expression REM_TK error
2339 {yyerror ("Missing term"); RECOVER;}
2340;
2341
2342additive_expression:
2343 multiplicative_expression
2344| additive_expression PLUS_TK multiplicative_expression
2345 {
2346 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2347 $1, $3);
2348 }
2349| additive_expression MINUS_TK multiplicative_expression
2350 {
2351 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2352 $1, $3);
2353 }
2354| additive_expression PLUS_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356| additive_expression MINUS_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358;
2359
2360shift_expression:
2361 additive_expression
2362| shift_expression LS_TK additive_expression
2363 {
2364 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2365 $1, $3);
2366 }
2367| shift_expression SRS_TK additive_expression
2368 {
2369 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2370 $1, $3);
2371 }
2372| shift_expression ZRS_TK additive_expression
2373 {
2374 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2375 $1, $3);
2376 }
2377| shift_expression LS_TK error
2378 {yyerror ("Missing term"); RECOVER;}
2379| shift_expression SRS_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381| shift_expression ZRS_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383;
2384
2385relational_expression:
2386 shift_expression
2387| relational_expression LT_TK shift_expression
2388 {
2389 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2390 $1, $3);
2391 }
2392| relational_expression GT_TK shift_expression
2393 {
2394 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2395 $1, $3);
2396 }
2397| relational_expression LTE_TK shift_expression
2398 {
2399 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2400 $1, $3);
2401 }
2402| relational_expression GTE_TK shift_expression
2403 {
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2405 $1, $3);
2406 }
2407| relational_expression INSTANCEOF_TK reference_type
5e942c50 2408 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
e04a16fb
AG
2409| relational_expression LT_TK error
2410 {yyerror ("Missing term"); RECOVER;}
2411| relational_expression GT_TK error
2412 {yyerror ("Missing term"); RECOVER;}
2413| relational_expression LTE_TK error
2414 {yyerror ("Missing term"); RECOVER;}
2415| relational_expression GTE_TK error
2416 {yyerror ("Missing term"); RECOVER;}
2417| relational_expression INSTANCEOF_TK error
2418 {yyerror ("Invalid reference type"); RECOVER;}
2419;
2420
2421equality_expression:
2422 relational_expression
2423| equality_expression EQ_TK relational_expression
2424 {
2425 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2426 $1, $3);
2427 }
2428| equality_expression NEQ_TK relational_expression
2429 {
2430 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2431 $1, $3);
2432 }
2433| equality_expression EQ_TK error
2434 {yyerror ("Missing term"); RECOVER;}
2435| equality_expression NEQ_TK error
2436 {yyerror ("Missing term"); RECOVER;}
2437;
2438
2439and_expression:
2440 equality_expression
2441| and_expression AND_TK equality_expression
2442 {
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 $1, $3);
2445 }
2446| and_expression AND_TK error
2447 {yyerror ("Missing term"); RECOVER;}
2448;
2449
2450exclusive_or_expression:
2451 and_expression
2452| exclusive_or_expression XOR_TK and_expression
2453 {
2454 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2455 $1, $3);
2456 }
2457| exclusive_or_expression XOR_TK error
2458 {yyerror ("Missing term"); RECOVER;}
2459;
2460
2461inclusive_or_expression:
2462 exclusive_or_expression
2463| inclusive_or_expression OR_TK exclusive_or_expression
2464 {
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2466 $1, $3);
2467 }
2468| inclusive_or_expression OR_TK error
2469 {yyerror ("Missing term"); RECOVER;}
2470;
2471
2472conditional_and_expression:
2473 inclusive_or_expression
2474| conditional_and_expression BOOL_AND_TK inclusive_or_expression
2475 {
2476 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477 $1, $3);
2478 }
2479| conditional_and_expression BOOL_AND_TK error
2480 {yyerror ("Missing term"); RECOVER;}
2481;
2482
2483conditional_or_expression:
2484 conditional_and_expression
2485| conditional_or_expression BOOL_OR_TK conditional_and_expression
2486 {
2487 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488 $1, $3);
2489 }
2490| conditional_or_expression BOOL_OR_TK error
2491 {yyerror ("Missing term"); RECOVER;}
2492;
2493
2494conditional_expression: /* Error handling here is weak */
2495 conditional_or_expression
2496| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
22eed1e6
APB
2497 {
2498 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2499 EXPR_WFL_LINECOL ($$) = $2.location;
2500 }
e04a16fb
AG
2501| conditional_or_expression REL_QM_TK REL_CL_TK error
2502 {
2503 YYERROR_NOW;
2504 yyerror ("Missing term");
2505 DRECOVER (1);
2506 }
2507| conditional_or_expression REL_QM_TK error
2508 {yyerror ("Missing term"); DRECOVER (2);}
2509| conditional_or_expression REL_QM_TK expression REL_CL_TK error
2510 {yyerror ("Missing term"); DRECOVER (3);}
2511;
2512
2513assignment_expression:
2514 conditional_expression
2515| assignment
2516;
2517
2518assignment:
2519 left_hand_side assignment_operator assignment_expression
2520 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2521| left_hand_side assignment_operator error
2522 {
2523 if (ctxp->prevent_ese != lineno)
2524 yyerror ("Missing term");
2525 DRECOVER (assign);
2526 }
2527;
2528
2529left_hand_side:
2530 name
2531| field_access
2532| array_access
2533;
2534
2535assignment_operator:
2536 ASSIGN_ANY_TK
2537| ASSIGN_TK
2538;
2539
2540expression:
2541 assignment_expression
2542;
2543
2544constant_expression:
2545 expression
2546;
2547
2548%%
2549\f
2550
c2952b01
APB
2551/* This section of the code deal with save/restoring parser contexts.
2552 Add mode documentation here. FIXME */
e04a16fb 2553
c2952b01
APB
2554/* Helper function. Create a new parser context. With
2555 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2556 context is copied, otherwise, the new context is zeroed. The newly
2557 created context becomes the current one. */
e04a16fb 2558
c2952b01
APB
2559static void
2560create_new_parser_context (copy_from_previous)
2561 int copy_from_previous;
e04a16fb 2562{
c2952b01 2563 struct parser_ctxt *new;
e04a16fb 2564
c2952b01
APB
2565 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2566 if (copy_from_previous)
2567 {
2568 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2569 new->saved_data_ctx = 1;
2570 }
2571 else
2572 bzero ((PTR) new, sizeof (struct parser_ctxt));
2573
e04a16fb
AG
2574 new->next = ctxp;
2575 ctxp = new;
c2952b01
APB
2576}
2577
2578/* Create a new parser context and make it the current one. */
2579
2580void
2581java_push_parser_context ()
2582{
2583 create_new_parser_context (0);
e04a16fb 2584 if (ctxp->next)
5e942c50
APB
2585 {
2586 ctxp->incomplete_class = ctxp->next->incomplete_class;
2587 ctxp->gclass_list = ctxp->next->gclass_list;
2588 }
e04a16fb
AG
2589}
2590
c2952b01
APB
2591void
2592java_pop_parser_context (generate)
2593 int generate;
2594{
2595 tree current;
2596 struct parser_ctxt *toFree, *next;
2597
2598 if (!ctxp)
2599 return;
2600
2601 toFree = ctxp;
2602 next = ctxp->next;
2603 if (next)
2604 {
2605 next->incomplete_class = ctxp->incomplete_class;
2606 next->gclass_list = ctxp->gclass_list;
2607 lineno = ctxp->lineno;
2608 finput = ctxp->finput;
2609 current_class = ctxp->current_class;
2610 }
2611
2612 /* Set the single import class file flag to 0 for the current list
2613 of imported things */
2614 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2615 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2616
2617 /* And restore those of the previous context */
2618 if ((ctxp = next)) /* Assignment is really meant here */
2619 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2620 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2621
2622 /* If we pushed a context to parse a class intended to be generated,
2623 we keep it so we can remember the class. What we could actually
2624 do is to just update a list of class names. */
2625 if (generate)
2626 {
2627 toFree->next = ctxp_for_generation;
2628 ctxp_for_generation = toFree;
2629 }
2630 else
2631 free (toFree);
2632}
2633
2634/* Create a parser context for the use of saving some global
2635 variables. */
2636
e04a16fb
AG
2637void
2638java_parser_context_save_global ()
2639{
22eed1e6
APB
2640 if (!ctxp)
2641 {
2642 java_push_parser_context ();
ee07f4f4
APB
2643 ctxp->saved_data_ctx = 1;
2644 }
c2952b01
APB
2645
2646 /* If this context already stores data, create a new one suitable
2647 for data storage. */
ee07f4f4 2648 else if (ctxp->saved_data)
c2952b01
APB
2649 create_new_parser_context (1);
2650
e04a16fb
AG
2651 ctxp->finput = finput;
2652 ctxp->lineno = lineno;
2653 ctxp->current_class = current_class;
2654 ctxp->filename = input_filename;
2655 ctxp->current_function_decl = current_function_decl;
ee07f4f4 2656 ctxp->saved_data = 1;
e04a16fb
AG
2657}
2658
c2952b01
APB
2659/* Restore some global variables from the previous context. Make the
2660 previous context the current one. */
2661
e04a16fb
AG
2662void
2663java_parser_context_restore_global ()
2664{
2665 finput = ctxp->finput;
2666 lineno = ctxp->lineno;
2667 current_class = ctxp->current_class;
2668 input_filename = ctxp->filename;
2669 current_function_decl = ctxp->current_function_decl;
c2952b01 2670 ctxp->saved_data = 0;
ee07f4f4
APB
2671 if (ctxp->saved_data_ctx)
2672 java_pop_parser_context (0);
e04a16fb
AG
2673}
2674
c2952b01
APB
2675/* Suspend vital data for the current class/function being parsed so
2676 that an other class can be parsed. Used to let local/anonymous
2677 classes be parsed. */
2678
2679static void
2680java_parser_context_suspend ()
e04a16fb 2681{
c2952b01 2682 /* This makes debugging through java_debug_context easier */
3b304f5b 2683 static const char *name = "<inner buffer context>";
e04a16fb 2684
c2952b01
APB
2685 /* Duplicate the previous context, use it to save the globals we're
2686 interested in */
2687 create_new_parser_context (1);
2688 ctxp->current_function_decl = current_function_decl;
2689 ctxp->current_class = current_class;
5e942c50 2690
c2952b01
APB
2691 /* Then create a new context which inherits all data from the
2692 previous one. This will be the new current context */
2693 create_new_parser_context (1);
2694
2695 /* Help debugging */
2696 ctxp->next->filename = name;
2697}
2698
2699/* Resume vital data for the current class/function being parsed so
2700 that an other class can be parsed. Used to let local/anonymous
2701 classes be parsed. The trick is the data storing file position
2702 informations must be restored to their current value, so parsing
2703 can resume as if no context was ever saved. */
2704
2705static void
2706java_parser_context_resume ()
2707{
2708 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2709 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2710 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2711
2712 /* We need to inherit the list of classes to complete/generate */
2713 restored->incomplete_class = old->incomplete_class;
2714 restored->gclass_list = old->gclass_list;
2715 restored->classd_list = old->classd_list;
2716 restored->class_list = old->class_list;
2717
2718 /* Restore the current class and function from the saver */
2719 current_class = saver->current_class;
2720 current_function_decl = saver->current_function_decl;
2721
2722 /* Retrive the restored context */
2723 ctxp = restored;
2724
2725 /* Re-installed the data for the parsing to carry on */
2726 bcopy (&old->marker_begining, &ctxp->marker_begining,
2727 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2728
2729 /* Buffer context can now be discarded */
2730 free (saver);
2731 free (old);
2732}
2733
2734/* Add a new anchor node to which all statement(s) initializing static
2735 and non static initialized upon declaration field(s) will be
2736 linked. */
2737
2738static void
2739java_parser_context_push_initialized_field ()
2740{
2741 tree node;
2742
2743 node = build_tree_list (NULL_TREE, NULL_TREE);
2744 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2745 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2746
2747 node = build_tree_list (NULL_TREE, NULL_TREE);
2748 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2749 CPC_INITIALIZER_LIST (ctxp) = node;
2750
2751 node = build_tree_list (NULL_TREE, NULL_TREE);
2752 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2753 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2754}
2755
2756/* Pop the lists of initialized field. If this lists aren't empty,
2757 remember them so we can use it to create and populate the $finit$
2758 or <clinit> functions. */
2759
2760static void
2761java_parser_context_pop_initialized_field ()
2762{
2763 tree stmts;
2764 tree class_type = TREE_TYPE (GET_CPC ());
2765
2766 if (CPC_INITIALIZER_LIST (ctxp))
e04a16fb 2767 {
c2952b01
APB
2768 stmts = CPC_INITIALIZER_STMT (ctxp);
2769 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2770 if (stmts && !java_error_count)
2771 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
e04a16fb
AG
2772 }
2773
c2952b01
APB
2774 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2775 {
2776 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2777 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2778 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2779 /* Keep initialization in order to enforce 8.5 */
2780 if (stmts && !java_error_count)
2781 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2782 }
e04a16fb 2783
c2952b01
APB
2784 /* JDK 1.1 instance initializers */
2785 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
b351b287 2786 {
c2952b01
APB
2787 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2788 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2789 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2790 if (stmts && !java_error_count)
2791 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
b351b287 2792 }
c2952b01
APB
2793}
2794
2795static tree
2796reorder_static_initialized (list)
2797 tree list;
2798{
2799 /* We have to keep things in order. The alias initializer have to
2800 come first, then the initialized regular field, in reverse to
2801 keep them in lexical order. */
2802 tree marker, previous = NULL_TREE;
2803 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2804 if (TREE_CODE (marker) == TREE_LIST
2805 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2806 break;
2807
2808 /* No static initialized, the list is fine as is */
2809 if (!previous)
2810 list = TREE_CHAIN (marker);
2811
2812 /* No marker? reverse the whole list */
2813 else if (!marker)
2814 list = nreverse (list);
2815
2816 /* Otherwise, reverse what's after the marker and the new reordered
2817 sublist will replace the marker. */
b351b287 2818 else
c2952b01
APB
2819 {
2820 TREE_CHAIN (previous) = NULL_TREE;
2821 list = nreverse (list);
2822 list = chainon (TREE_CHAIN (marker), list);
2823 }
2824 return list;
e04a16fb
AG
2825}
2826
c2952b01
APB
2827/* Helper functions to dump the parser context stack. */
2828
2829#define TAB_CONTEXT(C) \
2830 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
ee07f4f4
APB
2831
2832static void
2833java_debug_context_do (tab)
2834 int tab;
2835{
ee07f4f4
APB
2836 struct parser_ctxt *copy = ctxp;
2837 while (copy)
2838 {
c2952b01 2839 TAB_CONTEXT (tab);
ee07f4f4 2840 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
c2952b01 2841 TAB_CONTEXT (tab);
ee07f4f4 2842 fprintf (stderr, "filename: %s\n", copy->filename);
c2952b01
APB
2843 TAB_CONTEXT (tab);
2844 fprintf (stderr, "lineno: %d\n", copy->lineno);
2845 TAB_CONTEXT (tab);
ee07f4f4
APB
2846 fprintf (stderr, "package: %s\n",
2847 (copy->package ?
2848 IDENTIFIER_POINTER (copy->package) : "<none>"));
c2952b01 2849 TAB_CONTEXT (tab);
ee07f4f4 2850 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
c2952b01 2851 TAB_CONTEXT (tab);
ee07f4f4
APB
2852 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2853 copy = copy->next;
2854 tab += 2;
2855 }
ee07f4f4
APB
2856}
2857
c2952b01
APB
2858/* Dump the stacked up parser contexts. Intended to be called from a
2859 debugger. */
2860
ee07f4f4
APB
2861void
2862java_debug_context ()
2863{
2864 java_debug_context_do (0);
2865}
2866
c2952b01
APB
2867\f
2868
2869/* Flag for the error report routine to issue the error the first time
2870 it's called (overriding the default behavior which is to drop the
2871 first invocation and honor the second one, taking advantage of a
2872 richer context. */
2873static int force_error = 0;
ee07f4f4 2874
8119c720
APB
2875/* Reporting an constructor invocation error. */
2876static void
2877parse_ctor_invocation_error ()
2878{
2879 if (DECL_CONSTRUCTOR_P (current_function_decl))
2880 yyerror ("Constructor invocation must be first thing in a constructor");
2881 else
2882 yyerror ("Only constructors can invoke constructors");
2883}
2884
2885/* Reporting JDK1.1 features not implemented. */
b67d701b
PB
2886
2887static tree
2888parse_jdk1_1_error (msg)
49f48c71 2889 const char *msg;
b67d701b
PB
2890{
2891 sorry (": `%s' JDK1.1(TM) feature", msg);
2892 java_error_count++;
9bbc7d9f 2893 return empty_stmt_node;
b67d701b
PB
2894}
2895
e04a16fb
AG
2896static int do_warning = 0;
2897
2898void
2899yyerror (msg)
49f48c71 2900 const char *msg;
e04a16fb
AG
2901{
2902 static java_lc elc;
2903 static int prev_lineno;
49f48c71 2904 static const char *prev_msg;
e04a16fb 2905
0a2138e2 2906 int save_lineno;
e04a16fb
AG
2907 char *remainder, *code_from_source;
2908 extern struct obstack temporary_obstack;
2909
2910 if (!force_error && prev_lineno == lineno)
2911 return;
2912
2913 /* Save current error location but report latter, when the context is
2914 richer. */
2915 if (ctxp->java_error_flag == 0)
2916 {
2917 ctxp->java_error_flag = 1;
2918 elc = ctxp->elc;
2919 /* Do something to use the previous line if we're reaching the
2920 end of the file... */
2921#ifdef VERBOSE_SKELETON
2922 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2923#endif
2924 return;
2925 }
2926
2927 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2928 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2929 return;
2930
2931 ctxp->java_error_flag = 0;
2932 if (do_warning)
2933 java_warning_count++;
2934 else
2935 java_error_count++;
2936
807bc1db 2937 if (elc.col == 0 && msg && msg[1] == ';')
e04a16fb
AG
2938 {
2939 elc.col = ctxp->p_line->char_col-1;
2940 elc.line = ctxp->p_line->lineno;
2941 }
2942
2943 save_lineno = lineno;
2944 prev_lineno = lineno = elc.line;
2945 prev_msg = msg;
2946
2947 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2948 obstack_grow0 (&temporary_obstack,
2949 code_from_source, strlen (code_from_source));
2950 remainder = obstack_finish (&temporary_obstack);
2951 if (do_warning)
2952 warning ("%s.\n%s", msg, remainder);
2953 else
2954 error ("%s.\n%s", msg, remainder);
2955
2956 /* This allow us to cheaply avoid an extra 'Invalid expression
2957 statement' error report when errors have been already reported on
2958 the same line. This occurs when we report an error but don't have
2959 a synchronization point other than ';', which
2960 expression_statement is the only one to take care of. */
2961 ctxp->prevent_ese = lineno = save_lineno;
2962}
2963
2964static void
15fdcfe9 2965issue_warning_error_from_context (cl, msg, ap)
5e942c50 2966 tree cl;
d4476be2 2967 const char *msg;
15fdcfe9 2968 va_list ap;
5e942c50 2969{
3b304f5b 2970 const char *saved, *saved_input_filename;
15fdcfe9
PB
2971 char buffer [4096];
2972 vsprintf (buffer, msg, ap);
2973 force_error = 1;
5e942c50
APB
2974
2975 ctxp->elc.line = EXPR_WFL_LINENO (cl);
82371d41
APB
2976 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2977 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
5e942c50
APB
2978
2979 /* We have a CL, that's a good reason for using it if it contains data */
2980 saved = ctxp->filename;
2981 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2982 ctxp->filename = EXPR_WFL_FILENAME (cl);
1886c9d8
APB
2983 saved_input_filename = input_filename;
2984 input_filename = ctxp->filename;
15fdcfe9
PB
2985 java_error (NULL);
2986 java_error (buffer);
5e942c50 2987 ctxp->filename = saved;
1886c9d8 2988 input_filename = saved_input_filename;
15fdcfe9 2989 force_error = 0;
5e942c50
APB
2990}
2991
e04a16fb
AG
2992/* Issue an error message at a current source line CL */
2993
15fdcfe9 2994void
df32d2ce 2995parse_error_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 2996{
d4476be2 2997#ifndef ANSI_PROTOTYPES
e04a16fb 2998 tree cl;
d4476be2 2999 const char *msg;
e04a16fb 3000#endif
e04a16fb
AG
3001 va_list ap;
3002
3003 VA_START (ap, msg);
d4476be2 3004#ifndef ANSI_PROTOTYPES
e04a16fb 3005 cl = va_arg (ap, tree);
d4476be2 3006 msg = va_arg (ap, const char *);
e04a16fb 3007#endif
15fdcfe9
PB
3008 issue_warning_error_from_context (cl, msg, ap);
3009 va_end (ap);
e04a16fb
AG
3010}
3011
3012/* Issue a warning at a current source line CL */
3013
3014static void
df32d2ce 3015parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
e04a16fb 3016{
d4476be2 3017#ifndef ANSI_PROTOTYPES
e04a16fb 3018 tree cl;
d4476be2 3019 const char *msg;
e04a16fb 3020#endif
e04a16fb
AG
3021 va_list ap;
3022
3023 VA_START (ap, msg);
d4476be2 3024#ifndef ANSI_PROTOTYPES
e04a16fb 3025 cl = va_arg (ap, tree);
d4476be2 3026 msg = va_arg (ap, const char *);
e04a16fb 3027#endif
e04a16fb 3028
c877974e 3029 force_error = do_warning = 1;
15fdcfe9 3030 issue_warning_error_from_context (cl, msg, ap);
c877974e 3031 do_warning = force_error = 0;
15fdcfe9 3032 va_end (ap);
e04a16fb
AG
3033}
3034
82371d41
APB
3035static tree
3036find_expr_with_wfl (node)
3037 tree node;
3038{
3039 while (node)
3040 {
3041 char code;
3042 tree to_return;
3043
3044 switch (TREE_CODE (node))
3045 {
3046 case BLOCK:
c0d87ff6
PB
3047 node = BLOCK_EXPR_BODY (node);
3048 continue;
82371d41
APB
3049
3050 case COMPOUND_EXPR:
3051 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3052 if (to_return)
3053 return to_return;
c0d87ff6
PB
3054 node = TREE_OPERAND (node, 1);
3055 continue;
82371d41
APB
3056
3057 case LOOP_EXPR:
c0d87ff6
PB
3058 node = TREE_OPERAND (node, 0);
3059 continue;
82371d41
APB
3060
3061 case LABELED_BLOCK_EXPR:
c0d87ff6
PB
3062 node = TREE_OPERAND (node, 1);
3063 continue;
3064
82371d41
APB
3065 default:
3066 code = TREE_CODE_CLASS (TREE_CODE (node));
3067 if (((code == '1') || (code == '2') || (code == 'e'))
3068 && EXPR_WFL_LINECOL (node))
3069 return node;
ba179f9f 3070 return NULL_TREE;
82371d41
APB
3071 }
3072 }
3073 return NULL_TREE;
3074}
3075
3076/* Issue a missing return statement error. Uses METHOD to figure the
3077 last line of the method the error occurs in. */
3078
3079static void
3080missing_return_error (method)
3081 tree method;
3082{
3083 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3084 parse_error_context (wfl_operator, "Missing return statement");
3085}
3086
3087/* Issue an unreachable statement error. From NODE, find the next
3088 statement to report appropriately. */
3089static void
3090unreachable_stmt_error (node)
3091 tree node;
3092{
3093 /* Browse node to find the next expression node that has a WFL. Use
3094 the location to report the error */
3095 if (TREE_CODE (node) == COMPOUND_EXPR)
3096 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3097 else
3098 node = find_expr_with_wfl (node);
3099
3100 if (node)
3101 {
3102 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3103 parse_error_context (wfl_operator, "Unreachable statement");
3104 }
3105 else
3106 fatal ("Can't get valid statement - unreachable_stmt_error");
3107}
3108
c877974e 3109int
e04a16fb
AG
3110java_report_errors ()
3111{
3112 if (java_error_count)
3113 fprintf (stderr, "%d error%s",
3114 java_error_count, (java_error_count == 1 ? "" : "s"));
3115 if (java_warning_count)
3116 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3117 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3118 if (java_error_count || java_warning_count)
3119 putc ('\n', stderr);
c877974e 3120 return java_error_count;
e04a16fb
AG
3121}
3122
3123static char *
3124java_accstring_lookup (flags)
3125 int flags;
3126{
3127 static char buffer [80];
3128#define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3129
3130 /* Access modifier looked-up first for easier report on forbidden
3131 access. */
3132 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3133 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3134 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3135 if (flags & ACC_STATIC) COPY_RETURN ("static");
3136 if (flags & ACC_FINAL) COPY_RETURN ("final");
3137 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3138 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3139 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3140 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3141 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3142 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3143
3144 buffer [0] = '\0';
3145 return buffer;
3146#undef COPY_RETURN
3147}
3148
b67d701b
PB
3149/* Issuing error messages upon redefinition of classes, interfaces or
3150 variables. */
3151
e04a16fb 3152static void
b67d701b 3153classitf_redefinition_error (context, id, decl, cl)
49f48c71 3154 const char *context;
e04a16fb
AG
3155 tree id, decl, cl;
3156{
3157 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3158 context, IDENTIFIER_POINTER (id),
3159 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3160 /* Here we should point out where its redefined. It's a unicode. FIXME */
3161}
3162
b67d701b
PB
3163static void
3164variable_redefinition_error (context, name, type, line)
3165 tree context, name, type;
3166 int line;
3167{
49f48c71 3168 const char *type_name;
b67d701b
PB
3169
3170 /* Figure a proper name for type. We might haven't resolved it */
c877974e
APB
3171 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3172 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
b67d701b 3173 else
0a2138e2 3174 type_name = lang_printable_name (type, 0);
b67d701b
PB
3175
3176 parse_error_context (context,
781b0558 3177 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
b67d701b
PB
3178 IDENTIFIER_POINTER (name),
3179 type_name, IDENTIFIER_POINTER (name), line);
3180}
3181
c583dd46
APB
3182static tree
3183build_array_from_name (type, type_wfl, name, ret_name)
3184 tree type, type_wfl, name, *ret_name;
3185{
3186 int more_dims = 0;
49f48c71 3187 const char *string;
c583dd46
APB
3188
3189 /* Eventually get more dims */
3190 string = IDENTIFIER_POINTER (name);
3191 while (string [more_dims] == '[')
3192 more_dims++;
3193
3194 /* If we have, then craft a new type for this variable */
3195 if (more_dims)
3196 {
c0d87ff6 3197 name = get_identifier (&string [more_dims]);
c583dd46 3198
34f4db93
APB
3199 /* If we have a pointer, use its type */
3200 if (TREE_CODE (type) == POINTER_TYPE)
3201 type = TREE_TYPE (type);
c583dd46
APB
3202
3203 /* Building the first dimension of a primitive type uses this
3204 function */
3205 if (JPRIMITIVE_TYPE_P (type))
3206 {
3207 type = build_java_array_type (type, -1);
22eed1e6 3208 CLASS_LOADED_P (type) = 1;
c583dd46
APB
3209 more_dims--;
3210 }
3211 /* Otherwise, if we have a WFL for this type, use it (the type
3212 is already an array on an unresolved type, and we just keep
3213 on adding dimensions) */
3214 else if (type_wfl)
3215 type = type_wfl;
3216
3217 /* Add all the dimensions */
3218 while (more_dims--)
3219 type = build_unresolved_array_type (type);
3220
3221 /* The type may have been incomplete in the first place */
3222 if (type_wfl)
3223 type = obtain_incomplete_type (type);
3224 }
3225
c2952b01
APB
3226 if (ret_name)
3227 *ret_name = name;
c583dd46
APB
3228 return type;
3229}
3230
e04a16fb
AG
3231/* Build something that the type identifier resolver will identify as
3232 being an array to an unresolved type. TYPE_WFL is a WFL on a
3233 identifier. */
3234
3235static tree
3236build_unresolved_array_type (type_or_wfl)
3237 tree type_or_wfl;
3238{
49f48c71 3239 const char *ptr;
e04a16fb 3240
1886c9d8 3241 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
e04a16fb
AG
3242 just create a array type */
3243 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3244 {
3245 tree type = build_java_array_type (type_or_wfl, -1);
3246 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3247 return type;
3248 }
3249
3250 obstack_1grow (&temporary_obstack, '[');
3251 obstack_grow0 (&temporary_obstack,
3252 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3253 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3254 ptr = obstack_finish (&temporary_obstack);
3255 return build_expr_wfl (get_identifier (ptr),
3256 EXPR_WFL_FILENAME (type_or_wfl),
3257 EXPR_WFL_LINENO (type_or_wfl),
3258 EXPR_WFL_COLNO (type_or_wfl));
3259}
3260
e04a16fb
AG
3261static void
3262parser_add_interface (class_decl, interface_decl, wfl)
3263 tree class_decl, interface_decl, wfl;
3264{
3265 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3266 parse_error_context (wfl, "Interface `%s' repeated",
3267 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3268}
3269
3270/* Bulk of common class/interface checks. Return 1 if an error was
3271 encountered. TAG is 0 for a class, 1 for an interface. */
3272
3273static int
3274check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3275 int is_interface, flags;
3276 tree raw_name, qualified_name, decl, cl;
3277{
3278 tree node;
c2952b01
APB
3279 int sca = 0; /* Static class allowed */
3280 int icaf = 0; /* Inner class allowed flags */
3281 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
e04a16fb
AG
3282
3283 if (!quiet_flag)
c2952b01
APB
3284 fprintf (stderr, " %s%s %s",
3285 (CPC_INNER_P () ? "inner" : ""),
3286 (is_interface ? "interface" : "class"),
e04a16fb
AG
3287 IDENTIFIER_POINTER (qualified_name));
3288
3289 /* Scope of an interface/class type name:
3290 - Can't be imported by a single type import
3291 - Can't already exists in the package */
3292 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3293 && (node = find_name_in_single_imports (raw_name)))
3294 {
3295 parse_error_context
3296 (cl, "%s name `%s' clashes with imported type `%s'",
3297 (is_interface ? "Interface" : "Class"),
3298 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3299 return 1;
3300 }
3301 if (decl && CLASS_COMPLETE_P (decl))
3302 {
b67d701b
PB
3303 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3304 qualified_name, decl, cl);
e04a16fb
AG
3305 return 1;
3306 }
3307
c2952b01
APB
3308 if (check_inner_class_redefinition (raw_name, cl))
3309 return 1;
3310
3311 /* If public, file name should match class/interface name, except
3312 when dealing with an inner class */
3313 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
e04a16fb 3314 {
49f48c71 3315 const char *f;
e04a16fb
AG
3316
3317 /* Contains OS dependent assumption on path separator. FIXME */
3318 for (f = &input_filename [strlen (input_filename)];
fa322ab5
TT
3319 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3320 f--)
3321 ;
847fe791 3322 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
e04a16fb
AG
3323 f++;
3324 if (strncmp (IDENTIFIER_POINTER (raw_name),
3325 f , IDENTIFIER_LENGTH (raw_name)) ||
3326 f [IDENTIFIER_LENGTH (raw_name)] != '.')
781b0558
KG
3327 parse_error_context
3328 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
e04a16fb
AG
3329 (is_interface ? "interface" : "class"),
3330 IDENTIFIER_POINTER (qualified_name),
3331 IDENTIFIER_POINTER (raw_name));
3332 }
3333
c2952b01
APB
3334 /* Static classes can be declared only in top level classes. Note:
3335 once static, a inner class is a top level class. */
3336 if (flags & ACC_STATIC)
3337 {
3338 /* Catch the specific error of declaring an class inner class
3339 with no toplevel enclosing class. Prevent check_modifiers from
3340 complaining a second time */
3341 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3342 {
3343 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3344 IDENTIFIER_POINTER (qualified_name));
3345 sca = ACC_STATIC;
3346 }
3347 /* Else, in the context of a top-level class declaration, let
3348 `check_modifiers' do its job, otherwise, give it a go */
3349 else
3350 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3351 }
3352
a40d21da 3353 /* Inner classes can be declared private or protected
c2952b01
APB
3354 within their enclosing classes. */
3355 if (CPC_INNER_P ())
3356 {
3357 /* A class which is local to a block can't be public, private,
3358 protected or static. But it is created final, so allow this
3359 one. */
3360 if (current_function_decl)
3361 icaf = sca = uaaf = ACC_FINAL;
3362 else
3363 {
3364 check_modifiers_consistency (flags);
3365 icaf = ACC_PRIVATE|ACC_PROTECTED;
3366 }
3367 }
3368
a40d21da
APB
3369 if (is_interface)
3370 {
3371 if (CPC_INNER_P ())
3372 uaaf = INTERFACE_INNER_MODIFIERS;
3373 else
3374 uaaf = INTERFACE_MODIFIERS;
3375
3376 check_modifiers ("Illegal modifier `%s' for interface declaration",
3377 flags, uaaf);
3378 }
2884c41e 3379 else
a40d21da
APB
3380 check_modifiers ((current_function_decl ?
3381 "Illegal modifier `%s' for local class declaration" :
3382 "Illegal modifier `%s' for class declaration"),
c2952b01 3383 flags, uaaf|sca|icaf);
e04a16fb
AG
3384 return 0;
3385}
3386
c2952b01
APB
3387static void
3388make_nested_class_name (cpc_list)
3389 tree cpc_list;
3390{
3391 tree name;
3392
3393 if (!cpc_list)
3394 return;
3395 else
3396 make_nested_class_name (TREE_CHAIN (cpc_list));
3397
3398 /* Pick the qualified name when dealing with the first upmost
3399 enclosing class */
3400 name = (TREE_CHAIN (cpc_list) ?
3401 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3402 obstack_grow (&temporary_obstack,
3403 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3404 /* Why is NO_DOLLAR_IN_LABEL defined? */
3405#if 0
3406#ifdef NO_DOLLAR_IN_LABEL
3407 fatal ("make_nested_class_name: Can't use '$' as a separator "
3408 "for inner classes");
3409#endif
3410#endif
3411 obstack_1grow (&temporary_obstack, '$');
3412}
3413
3414/* Can't redefine a class already defined in an earlier scope. */
3415
3416static int
3417check_inner_class_redefinition (raw_name, cl)
3418 tree raw_name, cl;
3419{
3420 tree scope_list;
3421
3422 for (scope_list = GET_CPC_LIST (); scope_list;
3423 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3424 if (raw_name == GET_CPC_UN_NODE (scope_list))
3425 {
3426 parse_error_context
3427 (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",
3428 IDENTIFIER_POINTER (raw_name));
3429 return 1;
3430 }
3431 return 0;
3432}
3433
3434static tree
3435find_as_inner_class (enclosing, name, cl)
3436 tree enclosing, name, cl;
3437{
3438 tree qual, to_return;
3439 if (!enclosing)
3440 return NULL_TREE;
3441
3442 name = TYPE_NAME (name);
3443
3444 /* First search: within the scope of `enclosing', search for name */
3445 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3446 qual = EXPR_WFL_QUALIFICATION (cl);
3447 else if (cl)
3448 qual = build_tree_list (cl, NULL_TREE);
3449 else
3450 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3451
3452 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3453 return to_return;
3454
3455 /* We're dealing with a qualified name. Try to resolve thing until
3456 we get something that is an enclosing class. */
3457 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3458 {
3459 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3460
3461 for(qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3462 qual = TREE_CHAIN (qual))
3463 {
3464 acc = merge_qualified_name (acc,
3465 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3466 BUILD_PTR_FROM_NAME (ptr, acc);
3467 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3468 }
3469
3470 /* A NULL qual and a decl means that the search ended
3471 successfully?!? We have to do something then. FIXME */
3472
3473 if (decl)
3474 enclosing = decl;
3475 else
3476 qual = EXPR_WFL_QUALIFICATION (cl);
3477 }
3478 /* Otherwise, create a qual for the other part of the resolution. */
3479 else
3480 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3481
3482 return find_as_inner_class_do (qual, enclosing);
3483}
3484
3485/* We go inside the list of sub classes and try to find a way
3486 through. */
3487
3488static tree
3489find_as_inner_class_do (qual, enclosing)
3490 tree qual, enclosing;
3491{
3492 if (!qual)
3493 return NULL_TREE;
3494
3495 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3496 {
3497 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3498 tree next_enclosing = NULL_TREE;
3499 tree inner_list;
3500
3501 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3502 inner_list; inner_list = TREE_CHAIN (inner_list))
3503 {
3504 if (TREE_VALUE (inner_list) == name_to_match)
3505 {
3506 next_enclosing = TREE_PURPOSE (inner_list);
3507 break;
3508 }
3509 }
3510 enclosing = next_enclosing;
3511 }
3512
3513 return (!qual && enclosing ? enclosing : NULL_TREE);
3514}
3515
3516/* Reach all inner classes and tie their unqualified name to a
3517 DECL. */
3518
3519static void
3520set_nested_class_simple_name_value (outer, set)
3521 tree outer;
3522 int set;
3523{
3524 tree l;
3525
3526 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3527 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3528 TREE_PURPOSE (l) : NULL_TREE);
3529}
3530
3531static void
3532link_nested_class_to_enclosing ()
3533{
3534 if (GET_ENCLOSING_CPC ())
3535 {
3536 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3537 DECL_INNER_CLASS_LIST (enclosing) =
3538 tree_cons (GET_CPC (), GET_CPC_UN (),
3539 DECL_INNER_CLASS_LIST (enclosing));
3540 enclosing = enclosing;
3541 }
3542}
3543
3544static tree
3545maybe_make_nested_class_name (name)
3546 tree name;
3547{
3548 tree id = NULL_TREE;
3549
3550 if (CPC_INNER_P ())
3551 {
3552 make_nested_class_name (GET_CPC_LIST ());
48a840d9
APB
3553 obstack_grow0 (&temporary_obstack,
3554 IDENTIFIER_POINTER (name),
3555 IDENTIFIER_LENGTH (name));
c2952b01
APB
3556 id = get_identifier (obstack_finish (&temporary_obstack));
3557 if (ctxp->package)
3558 QUALIFIED_P (id) = 1;
3559 }
3560 return id;
3561}
3562
3563/* If DECL is NULL, create and push a new DECL, record the current
3564 line CL and do other maintenance things. */
3565
e04a16fb 3566static tree
c2952b01
APB
3567maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3568 tree decl, raw_name, qualified_name, cl;
e04a16fb 3569{
5e942c50 3570 if (!decl)
e04a16fb 3571 decl = push_class (make_class (), qualified_name);
c2952b01 3572
e04a16fb
AG
3573 /* Take care of the file and line business */
3574 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
f099f336
APB
3575 /* If we're emiting xrefs, store the line/col number information */
3576 if (flag_emit_xref)
3577 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3578 else
3579 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
e04a16fb 3580 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
b351b287
APB
3581 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3582 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
e04a16fb 3583
c2952b01
APB
3584 PUSH_CPC (decl, raw_name);
3585 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3586
e04a16fb
AG
3587 /* Link the declaration to the already seen ones */
3588 TREE_CHAIN (decl) = ctxp->class_list;
3589 ctxp->class_list = decl;
5e942c50 3590
23a79c61 3591 /* Create a new nodes in the global lists */
5e942c50 3592 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
23a79c61 3593 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
5e942c50 3594
e04a16fb
AG
3595 /* Install a new dependency list element */
3596 create_jdep_list (ctxp);
3597
3598 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3599 IDENTIFIER_POINTER (qualified_name)));
3600 return decl;
3601}
3602
3603static void
3604add_superinterfaces (decl, interface_list)
3605 tree decl, interface_list;
3606{
3607 tree node;
3608 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3609 takes care of ensuring that:
3610 - This is an accessible interface type,
3611 - Circularity detection.
3612 parser_add_interface is then called. If present but not defined,
3613 the check operation is delayed until the super interface gets
3614 defined. */
3615 for (node = interface_list; node; node = TREE_CHAIN (node))
3616 {
15fdcfe9 3617 tree current = TREE_PURPOSE (node);
5e942c50
APB
3618 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3619 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
e04a16fb 3620 {
5e942c50
APB
3621 if (!parser_check_super_interface (idecl, decl, current))
3622 parser_add_interface (decl, idecl, current);
e04a16fb
AG
3623 }
3624 else
3625 register_incomplete_type (JDEP_INTERFACE,
3626 current, decl, NULL_TREE);
3627 }
3628}
3629
3630/* Create an interface in pass1 and return its decl. Return the
3631 interface's decl in pass 2. */
3632
3633static tree
3634create_interface (flags, id, super)
3635 int flags;
3636 tree id, super;
3637{
e04a16fb 3638 tree raw_name = EXPR_WFL_NODE (id);
c2952b01 3639 tree q_name = parser_qualified_classname (flags & ACC_STATIC, raw_name);
e04a16fb
AG
3640 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3641
3642 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3643
3644 /* Basic checks: scope, redefinition, modifiers */
3645 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
c2952b01
APB
3646 {
3647 PUSH_ERROR ();
3648 return NULL_TREE;
3649 }
3650
3651 /* Suspend the current parsing context if we're parsing an inner
3652 interface */
3653 if (CPC_INNER_P ())
3654 java_parser_context_suspend ();
3655
3656 /* Push a new context for (static) initialized upon declaration fields */
3657 java_parser_context_push_initialized_field ();
e04a16fb
AG
3658
3659 /* Interface modifiers check
3660 - public/abstract allowed (already done at that point)
3661 - abstract is obsolete (comes first, it's a warning, or should be)
3662 - Can't use twice the same (checked in the modifier rule) */
c877974e 3663 if ((flags & ACC_ABSTRACT) && flag_redundant)
e04a16fb
AG
3664 parse_warning_context
3665 (MODIFIER_WFL (ABSTRACT_TK),
781b0558 3666 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3667
3668 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3669 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
e04a16fb
AG
3670
3671 /* Set super info and mark the class a complete */
2aa11e97 3672 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
e04a16fb
AG
3673 object_type_node, ctxp->interface_number);
3674 ctxp->interface_number = 0;
3675 CLASS_COMPLETE_P (decl) = 1;
3676 add_superinterfaces (decl, super);
3677
3678 return decl;
3679}
3680
c2952b01
APB
3681/* Anonymous class counter. Will be reset to 1 every time a non
3682 anonymous class gets created. */
3683static int anonymous_class_counter = 1;
3684
3685/* Patch anonymous class CLASS, by either extending or implementing
3686 DEP. */
3687
3688static void
3689patch_anonymous_class (type_decl, class_decl, wfl)
3690 tree type_decl, class_decl, wfl;
3691{
3692 tree class = TREE_TYPE (class_decl);
3693 tree type = TREE_TYPE (type_decl);
3694 tree binfo = TYPE_BINFO (class);
3695
3696 /* If it's an interface, implement it */
3697 if (CLASS_INTERFACE (type_decl))
3698 {
3699 tree s_binfo;
3700 int length;
3701
3702 if (parser_check_super_interface (type_decl, class_decl, wfl))
3703 return;
3704
3705 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3706 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3707 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3708 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3709 /* And add the interface */
3710 parser_add_interface (class_decl, type_decl, wfl);
3711 }
3712 /* Otherwise, it's a type we want to extend */
3713 else
3714 {
3715 if (parser_check_super (type_decl, class_decl, wfl))
3716 return;
3717 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3718 }
3719}
3720
3721static tree
3722create_anonymous_class (location, type_name)
3723 int location;
3724 tree type_name;
3725{
3726 char buffer [80];
3727 tree super = NULL_TREE, itf = NULL_TREE;
3728 tree id, type_decl, class;
3729
3730 /* The unqualified name of the anonymous class. It's just a number. */
3731 sprintf (buffer, "%d", anonymous_class_counter++);
3732 id = build_wfl_node (get_identifier (buffer));
3733 EXPR_WFL_LINECOL (id) = location;
3734
3735 /* We know about the type to extend/implement. We go ahead */
3736 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3737 {
3738 /* Create a class which either implements on extends the designated
3739 class. The class bears an innacessible name. */
3740 if (CLASS_INTERFACE (type_decl))
3741 {
3742 /* It's OK to modify it here. It's been already used and
3743 shouldn't be reused */
3744 ctxp->interface_number = 1;
3745 /* Interfaces should presented as a list of WFLs */
3746 itf = build_tree_list (type_name, NULL_TREE);
3747 }
3748 else
3749 super = type_name;
3750 }
3751
3752 class = create_class (ACC_FINAL, id, super, itf);
3753
3754 /* We didn't know anything about the stuff. We register a dependence. */
3755 if (!type_decl)
3756 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3757
3758 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3759 return class;
3760}
3761
a40d21da 3762/* Create a class in pass1 and return its decl. Return class
e04a16fb
AG
3763 interface's decl in pass 2. */
3764
3765static tree
3766create_class (flags, id, super, interfaces)
3767 int flags;
3768 tree id, super, interfaces;
3769{
e04a16fb
AG
3770 tree raw_name = EXPR_WFL_NODE (id);
3771 tree class_id, decl;
9ee9b555 3772 tree super_decl_type;
e04a16fb 3773
c2952b01 3774 class_id = parser_qualified_classname (0, raw_name);
e04a16fb
AG
3775 decl = IDENTIFIER_CLASS_VALUE (class_id);
3776 EXPR_WFL_NODE (id) = class_id;
3777
3778 /* Basic check: scope, redefinition, modifiers */
3779 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
c2952b01
APB
3780 {
3781 PUSH_ERROR ();
3782 return NULL_TREE;
3783 }
3784
3785 /* Suspend the current parsing context if we're parsing an inner
3786 class or an anonymous class. */
3787 if (CPC_INNER_P ())
3788 java_parser_context_suspend ();
3789 /* Push a new context for (static) initialized upon declaration fields */
3790 java_parser_context_push_initialized_field ();
e04a16fb
AG
3791
3792 /* Class modifier check:
3793 - Allowed modifier (already done at that point)
3794 - abstract AND final forbidden
3795 - Public classes defined in the correct file */
3796 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
781b0558
KG
3797 parse_error_context
3798 (id, "Class `%s' can't be declared both abstract and final",
3799 IDENTIFIER_POINTER (raw_name));
e04a16fb
AG
3800
3801 /* Create a new decl if DECL is NULL, otherwise fix it */
c2952b01 3802 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
e04a16fb
AG
3803
3804 /* If SUPER exists, use it, otherwise use Object */
3805 if (super)
3806 {
3807 /* Can't extend java.lang.Object */
3808 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3809 {
3810 parse_error_context (id, "Can't extend `java.lang.Object'");
3811 return NULL_TREE;
3812 }
3813
2c3199bc
PB
3814 super_decl_type =
3815 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
e04a16fb
AG
3816 }
3817 else if (TREE_TYPE (decl) != object_type_node)
3818 super_decl_type = object_type_node;
3819 /* We're defining java.lang.Object */
3820 else
3821 super_decl_type = NULL_TREE;
3822
3823 /* Set super info and mark the class a complete */
3824 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3825 ctxp->interface_number);
3826 ctxp->interface_number = 0;
3827 CLASS_COMPLETE_P (decl) = 1;
3828 add_superinterfaces (decl, interfaces);
3829
c2952b01
APB
3830 /* If the class is a top level inner class, install an alias. */
3831 if (INNER_CLASS_DECL_P (decl) && CLASS_STATIC (decl))
3832 {
3833 tree alias = parser_qualified_classname (1, raw_name);
3834 IDENTIFIER_GLOBAL_VALUE (alias) = decl;
3835 }
3836
3837 /* Add the private this$<n> field, Replicate final locals still in
3838 scope as private final fields mangled like val$<local_name>.
3839 This doesn't not occur for top level (static) inner classes. */
3840 if (PURE_INNER_CLASS_DECL_P (decl))
3841 add_inner_class_fields (decl, current_function_decl);
3842
7f10c2e2
APB
3843 /* If doing xref, store the location at which the inherited class
3844 (if any) was seen. */
3845 if (flag_emit_xref && super)
3846 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3847
5e942c50
APB
3848 /* Eventually sets the @deprecated tag flag */
3849 CHECK_DEPRECATED (decl);
3850
165f37bc
APB
3851 /* Reset the anonymous class counter when declaring non inner classes */
3852 if (!INNER_CLASS_DECL_P (decl))
c2952b01
APB
3853 anonymous_class_counter = 1;
3854
e04a16fb
AG
3855 return decl;
3856}
3857
c2952b01
APB
3858/* End a class declaration: register the statements used to create
3859 $finit$ and <clinit>, pop the current class and resume the prior
3860 parser context if necessary. */
3861
3862static void
3863end_class_declaration (resume)
3864 int resume;
3865{
3866 /* If an error occured, context weren't pushed and won't need to be
3867 popped by a resume. */
3868 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3869
3870 java_parser_context_pop_initialized_field ();
3871 POP_CPC ();
3872 if (resume && no_error_occured)
3873 java_parser_context_resume ();
93220702
APB
3874
3875 /* We're ending a class declaration, this is a good time to reset
3876 the interface cout. Note that might have been already done in
3877 create_interface, but if at that time an inner class was being
3878 dealt with, the interface count was reset in a context created
3879 for the sake of handling inner classes declaration. */
3880 ctxp->interface_number = 0;
c2952b01
APB
3881}
3882
3883static void
3884add_inner_class_fields (class_decl, fct_decl)
3885 tree class_decl;
3886 tree fct_decl;
3887{
3888 tree block, marker, f;
3889
3890 f = add_field (TREE_TYPE (class_decl),
3891 build_current_thisn (TREE_TYPE (class_decl)),
3892 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3893 ACC_PRIVATE);
3894 FIELD_THISN (f) = 1;
3895
3896 if (!fct_decl)
3897 return;
3898
3899 for (block = GET_CURRENT_BLOCK (fct_decl);
3900 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3901 {
3902 tree decl;
3903 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3904 {
3905 char *name, *pname;
3906 tree wfl, init, list;
3907
3908 /* Avoid non final arguments. */
3909 if (!LOCAL_FINAL (decl))
3910 continue;
3911
3912 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3913 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3914 wfl = build_wfl_node (get_identifier (name));
3915 init = build_wfl_node (get_identifier (pname));
3916 /* Build an initialization for the field: it will be
3917 initialized by a parameter added to $finit$, bearing a
3918 mangled name of the field itself (param$<n>.) The
3919 parameter is provided to $finit$ by the constructor
3920 invoking it (hence the constructor will also feature a
3921 hidden parameter, set to the value of the outer context
3922 local at the time the inner class is created.)
3923
3924 Note: we take into account all possible locals that can
3925 be accessed by the inner class. It's actually not trivial
3926 to minimize these aliases down to the ones really
3927 used. One way to do that would be to expand all regular
3928 methods first, then $finit$ to get a picture of what's
3929 used. It works with the exception that we would have to
3930 go back on all constructor invoked in regular methods to
3931 have their invokation reworked (to include the right amount
3932 of alias initializer parameters.)
3933
3934 The only real way around, I think, is a first pass to
3935 identify locals really used in the inner class. We leave
3936 the flag FIELD_LOCAL_ALIAS_USED around for that future
3937 use.
3938
3939 On the other hand, it only affect local inner classes,
3940 whose constructors (and $finit$ call) will be featuring
3941 unecessary arguments. It's easy for a developper to keep
3942 this number of parameter down by using the `final'
3943 keyword only when necessary. For the time being, we can
3944 issue a warning on unecessary finals. FIXME */
3945 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3946 wfl, init);
3947
3948 /* Register the field. The TREE_LIST holding the part
3949 initialized/initializer will be marked ARG_FINAL_P so
3950 that the created field can be marked
3951 FIELD_LOCAL_ALIAS. */
3952 list = build_tree_list (wfl, init);
3953 ARG_FINAL_P (list) = 1;
3954 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3955 }
3956 }
3957
3958 if (!CPC_INITIALIZER_STMT (ctxp))
3959 return;
3960
3961 /* If we ever registered an alias field, insert and marker to
3962 remeber where the list ends. The second part of the list (the one
3963 featuring initialized fields) so it can be later reversed to
3964 enforce 8.5. The marker will be removed during that operation. */
3965 marker = build_tree_list (NULL_TREE, NULL_TREE);
3966 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3967 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3968}
3969
e04a16fb
AG
3970/* Can't use lookup_field () since we don't want to load the class and
3971 can't set the CLASS_LOADED_P flag */
3972
3973static tree
3974find_field (class, name)
3975 tree class;
3976 tree name;
3977{
3978 tree decl;
3979 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
3980 {
3981 if (DECL_NAME (decl) == name)
3982 return decl;
3983 }
3984 return NULL_TREE;
3985}
3986
3987/* Wrap around lookup_field that doesn't potentially upset the value
3988 of CLASS */
3989
3990static tree
3991lookup_field_wrapper (class, name)
3992 tree class, name;
3993{
3994 tree type = class;
5b09b33e 3995 tree decl;
c877974e 3996 java_parser_context_save_global ();
5b09b33e 3997 decl = lookup_field (&type, name);
f2760b27
APB
3998
3999 /* Last chance: if we're within the context of an inner class, we
4000 might be trying to access a local variable defined in an outer
4001 context. We try to look for it now. */
4002 if (INNER_CLASS_TYPE_P (class) && (!decl || decl == error_mark_node))
4003 {
4004 char *alias_buffer;
4005 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
4006 name = get_identifier (alias_buffer);
4007 type = class;
4008 decl = lookup_field (&type, name);
4009 if (decl && decl != error_mark_node)
4010 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4011 }
4012
c877974e 4013 java_parser_context_restore_global ();
93024893 4014 return decl == error_mark_node ? NULL : decl;
e04a16fb
AG
4015}
4016
4017/* Find duplicate field within the same class declarations and report
c583dd46
APB
4018 the error. Returns 1 if a duplicated field was found, 0
4019 otherwise. */
e04a16fb
AG
4020
4021static int
c583dd46 4022duplicate_declaration_error_p (new_field_name, new_type, cl)
0a2138e2 4023 tree new_field_name, new_type, cl;
e04a16fb
AG
4024{
4025 /* This might be modified to work with method decl as well */
c2952b01 4026 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
e04a16fb
AG
4027 if (decl)
4028 {
c2e3db92 4029 char *t1 = xstrdup (purify_type_name
4a5f66c3
APB
4030 ((TREE_CODE (new_type) == POINTER_TYPE
4031 && TREE_TYPE (new_type) == NULL_TREE) ?
4032 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4033 lang_printable_name (new_type, 1)));
c877974e
APB
4034 /* The type may not have been completed by the time we report
4035 the error */
c2e3db92 4036 char *t2 = xstrdup (purify_type_name
4a5f66c3 4037 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
c877974e
APB
4038 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4039 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4040 lang_printable_name (TREE_TYPE (decl), 1)));
e04a16fb
AG
4041 parse_error_context
4042 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4043 t1, IDENTIFIER_POINTER (new_field_name),
4044 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4045 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4046 free (t1);
4047 free (t2);
c583dd46 4048 return 1;
e04a16fb 4049 }
c583dd46 4050 return 0;
e04a16fb
AG
4051}
4052
4053/* Field registration routine. If TYPE doesn't exist, field
4054 declarations are linked to the undefined TYPE dependency list, to
4055 be later resolved in java_complete_class () */
4056
4057static void
4058register_fields (flags, type, variable_list)
4059 int flags;
4060 tree type, variable_list;
4061{
c583dd46 4062 tree current, saved_type;
c2952b01 4063 tree class_type = NULL_TREE;
e04a16fb
AG
4064 int saved_lineno = lineno;
4065 int must_chain = 0;
4066 tree wfl = NULL_TREE;
4067
c2952b01
APB
4068 if (GET_CPC ())
4069 class_type = TREE_TYPE (GET_CPC ());
4070
4071 if (!class_type || class_type == error_mark_node)
4072 return;
4073
e04a16fb
AG
4074 /* If we're adding fields to interfaces, those fields are public,
4075 static, final */
4076 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4077 {
4078 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2884c41e 4079 flags, ACC_PUBLIC, "interface field(s)");
e04a16fb 4080 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2884c41e 4081 flags, ACC_STATIC, "interface field(s)");
e04a16fb 4082 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2884c41e 4083 flags, ACC_FINAL, "interface field(s)");
e04a16fb
AG
4084 check_modifiers ("Illegal interface member modifier `%s'", flags,
4085 INTERFACE_FIELD_MODIFIERS);
4086 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4087 }
4088
c583dd46
APB
4089 /* Obtain a suitable type for resolution, if necessary */
4090 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4091
4092 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 4093 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
e04a16fb 4094
c583dd46
APB
4095 for (current = variable_list, saved_type = type; current;
4096 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 4097 {
c877974e 4098 tree real_type;
c583dd46 4099 tree field_decl;
e04a16fb
AG
4100 tree cl = TREE_PURPOSE (current);
4101 tree init = TREE_VALUE (current);
4102 tree current_name = EXPR_WFL_NODE (cl);
4103
c2952b01
APB
4104 /* Can't declare static fields in inner classes */
4105 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4106 && !CLASS_INTERFACE (TYPE_NAME (class_type)))
4107 parse_error_context
4108 (cl, "Field `%s' can't be static in innerclass `%s'. Only members of interfaces and top-level classes can be static",
4109 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4110 lang_printable_name (class_type, 0));
4111
c583dd46
APB
4112 /* Process NAME, as it may specify extra dimension(s) for it */
4113 type = build_array_from_name (type, wfl, current_name, &current_name);
4114
c583dd46
APB
4115 /* Type adjustment. We may have just readjusted TYPE because
4116 the variable specified more dimensions. Make sure we have
22eed1e6
APB
4117 a reference if we can and don't have one already. Also
4118 change the name if we have an init. */
4119 if (type != saved_type)
4120 {
1886c9d8 4121 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
22eed1e6
APB
4122 if (init)
4123 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4124 }
e04a16fb 4125
c877974e
APB
4126 real_type = GET_REAL_TYPE (type);
4127 /* Check for redeclarations */
4128 if (duplicate_declaration_error_p (current_name, real_type, cl))
4129 continue;
4130
c583dd46 4131 /* Set lineno to the line the field was found and create a
5e942c50 4132 declaration for it. Eventually sets the @deprecated tag flag. */
f099f336
APB
4133 if (flag_emit_xref)
4134 lineno = EXPR_WFL_LINECOL (cl);
4135 else
4136 lineno = EXPR_WFL_LINENO (cl);
c877974e 4137 field_decl = add_field (class_type, current_name, real_type, flags);
5e942c50 4138 CHECK_DEPRECATED (field_decl);
c2952b01
APB
4139
4140 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4141 mark the created field FIELD_LOCAL_ALIAS, so that we can
4142 hide parameters to this inner class $finit$ and constructors. */
4143 if (ARG_FINAL_P (current))
4144 FIELD_LOCAL_ALIAS (field_decl) = 1;
c583dd46
APB
4145
4146 /* Check if we must chain. */
4147 if (must_chain)
4148 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
e04a16fb 4149
c583dd46
APB
4150 /* If we have an initialization value tied to the field */
4151 if (init)
4152 {
4153 /* The field is declared static */
e04a16fb 4154 if (flags & ACC_STATIC)
e04a16fb 4155 {
7525cc04
APB
4156 /* We include the field and its initialization part into
4157 a list used to generate <clinit>. After <clinit> is
ba179f9f
APB
4158 walked, field initializations will be processed and
4159 fields initialized with known constants will be taken
4160 out of <clinit> and have their DECL_INITIAL set
7525cc04 4161 appropriately. */
c2952b01
APB
4162 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4163 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
7f10c2e2
APB
4164 if (TREE_OPERAND (init, 1)
4165 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
5bba4807 4166 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
e04a16fb 4167 }
5e942c50
APB
4168 /* A non-static field declared with an immediate initialization is
4169 to be initialized in <init>, if any. This field is remembered
4170 to be processed at the time of the generation of <init>. */
c583dd46
APB
4171 else
4172 {
c2952b01
APB
4173 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4174 SET_CPC_INITIALIZER_STMT (ctxp, init);
c583dd46 4175 }
5b09b33e 4176 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
8576f094 4177 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
e04a16fb
AG
4178 }
4179 }
4180 lineno = saved_lineno;
4181}
4182
c2952b01
APB
4183/* Generate $finit$, using the list of initialized fields to populate
4184 its body. $finit$'s parameter(s) list is adjusted to include the
4185 one(s) used to initialized the field(s) caching outer context
4186 local(s). */
22eed1e6 4187
c2952b01
APB
4188static tree
4189generate_finit (class_type)
4190 tree class_type;
22eed1e6 4191{
c2952b01
APB
4192 int count = 0;
4193 tree list = TYPE_FINIT_STMT_LIST (class_type);
4194 tree mdecl, current, parms;
4195
4196 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4197 class_type, NULL_TREE,
4198 &count);
4199 CRAFTED_PARAM_LIST_FIXUP (parms);
4200 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4201 finit_identifier_node, parms);
4202 fix_method_argument_names (parms, mdecl);
4203 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4204 mdecl, NULL_TREE);
4205 DECL_FUNCTION_NAP (mdecl) = count;
22eed1e6
APB
4206 start_artificial_method_body (mdecl);
4207
c2952b01 4208 for (current = list; current; current = TREE_CHAIN (current))
22eed1e6
APB
4209 java_method_add_stmt (mdecl,
4210 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4211 current));
22eed1e6 4212 end_artificial_method_body (mdecl);
c2952b01 4213 return mdecl;
22eed1e6
APB
4214}
4215
e04a16fb 4216static void
c2952b01
APB
4217add_instance_initializer (mdecl)
4218 tree mdecl;
e04a16fb 4219{
c2952b01
APB
4220 tree current;
4221 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4222 tree compound = NULL_TREE;
e04a16fb 4223
c2952b01 4224 if (stmt_list)
e04a16fb 4225 {
c2952b01
APB
4226 for (current = stmt_list; current; current = TREE_CHAIN (current))
4227 compound = add_stmt_to_compound (compound, NULL_TREE, current);
e04a16fb 4228
c2952b01
APB
4229 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4230 NULL_TREE, compound));
4231 }
e04a16fb
AG
4232}
4233
4234/* Shared accros method_declarator and method_header to remember the
4235 patch stage that was reached during the declaration of the method.
4236 A method DECL is built differently is there is no patch
4237 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4238 pending on the currently defined method. */
4239
4240static int patch_stage;
4241
4242/* Check the method declaration and add the method to its current
4243 class. If the argument list is known to contain incomplete types,
4244 the method is partially added and the registration will be resume
22eed1e6
APB
4245 once the method arguments resolved. If TYPE is NULL, we're dealing
4246 with a constructor. */
e04a16fb
AG
4247
4248static tree
4249method_header (flags, type, mdecl, throws)
4250 int flags;
4251 tree type, mdecl, throws;
4252{
4253 tree meth = TREE_VALUE (mdecl);
4254 tree id = TREE_PURPOSE (mdecl);
1886c9d8 4255 tree type_wfl = NULL_TREE;
79d13333 4256 tree meth_name = NULL_TREE;
c2952b01 4257 tree current, orig_arg, this_class = NULL;
e04a16fb 4258 int saved_lineno;
1886c9d8 4259 int constructor_ok = 0, must_chain;
c2952b01 4260 int count;
e04a16fb
AG
4261
4262 check_modifiers_consistency (flags);
79d13333 4263
c2952b01
APB
4264 if (GET_CPC ())
4265 this_class = TREE_TYPE (GET_CPC ());
4266
4267 if (!this_class || this_class == error_mark_node)
79d13333 4268 return NULL_TREE;
e04a16fb
AG
4269
4270 /* There are some forbidden modifiers for an abstract method and its
4271 class must be abstract as well. */
22eed1e6 4272 if (type && (flags & ACC_ABSTRACT))
e04a16fb
AG
4273 {
4274 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4275 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4276 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4277 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4278 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2aa11e97
APB
4279 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4280 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
e04a16fb 4281 parse_error_context
781b0558 4282 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
e04a16fb
AG
4283 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
4284 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4285 }
c2952b01 4286
22eed1e6
APB
4287 /* Things to be checked when declaring a constructor */
4288 if (!type)
4289 {
4290 int ec = java_error_count;
4291 /* 8.6: Constructor declarations: we might be trying to define a
4292 method without specifying a return type. */
c2952b01 4293 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
22eed1e6
APB
4294 parse_error_context
4295 (id, "Invalid method declaration, return type required");
4296 /* 8.6.3: Constructor modifiers */
4297 else
4298 {
4299 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4300 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4301 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4302 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4303 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4304 }
4305 /* If we found error here, we don't consider it's OK to tread
4306 the method definition as a constructor, for the rest of this
4307 function */
4308 if (ec == java_error_count)
4309 constructor_ok = 1;
4310 }
e04a16fb
AG
4311
4312 /* Method declared within the scope of an interface are implicitly
4313 abstract and public. Conflicts with other erroneously provided
c0d87ff6 4314 modifiers are checked right after. */
e04a16fb
AG
4315
4316 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4317 {
4318 /* If FLAGS isn't set because of a modifier, turn the
4319 corresponding modifier WFL to NULL so we issue a warning on
4320 the obsolete use of the modifier */
4321 if (!(flags & ACC_PUBLIC))
4322 MODIFIER_WFL (PUBLIC_TK) = NULL;
4323 if (!(flags & ACC_ABSTRACT))
4324 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4325 flags |= ACC_PUBLIC;
4326 flags |= ACC_ABSTRACT;
4327 }
4328
c2952b01
APB
4329 /* Inner class can't declare static methods */
4330 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4331 {
4332 parse_error_context
4333 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4334 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4335 lang_printable_name (this_class, 0));
4336 }
4337
e04a16fb
AG
4338 /* Modifiers context reset moved up, so abstract method declaration
4339 modifiers can be later checked. */
4340
22eed1e6
APB
4341 /* Set constructor returned type to void and method name to <init>,
4342 unless we found an error identifier the constructor (in which
4343 case we retain the original name) */
4344 if (!type)
4345 {
4346 type = void_type_node;
4347 if (constructor_ok)
4348 meth_name = init_identifier_node;
4349 }
4350 else
4351 meth_name = EXPR_WFL_NODE (id);
e04a16fb 4352
1886c9d8
APB
4353 /* Do the returned type resolution and registration if necessary */
4354 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4355
4a5f66c3
APB
4356 if (meth_name)
4357 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
1886c9d8
APB
4358 EXPR_WFL_NODE (id) = meth_name;
4359 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4360
4361 if (must_chain)
e04a16fb 4362 {
1886c9d8
APB
4363 patch_stage = JDEP_METHOD_RETURN;
4364 register_incomplete_type (patch_stage, type_wfl, id, type);
4365 TREE_TYPE (meth) = GET_REAL_TYPE (type);
e04a16fb
AG
4366 }
4367 else
1886c9d8 4368 TREE_TYPE (meth) = type;
e04a16fb
AG
4369
4370 saved_lineno = lineno;
4371 /* When defining an abstract or interface method, the curly
4372 bracket at level 1 doesn't exist because there is no function
4373 body */
4374 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4375 EXPR_WFL_LINENO (id));
4376
5e942c50
APB
4377 /* Remember the original argument list */
4378 orig_arg = TYPE_ARG_TYPES (meth);
4379
e04a16fb
AG
4380 if (patch_stage) /* includes ret type and/or all args */
4381 {
4382 jdep *jdep;
4383 meth = add_method_1 (this_class, flags, meth_name, meth);
4384 /* Patch for the return type */
4385 if (patch_stage == JDEP_METHOD_RETURN)
4386 {
4387 jdep = CLASSD_LAST (ctxp->classd_list);
4388 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4389 }
4390 /* This is the stop JDEP. METH allows the function's signature
4391 to be computed. */
4392 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4393 }
4394 else
5e942c50
APB
4395 meth = add_method (this_class, flags, meth_name,
4396 build_java_signature (meth));
4397
c2952b01
APB
4398 /* Remember final parameters */
4399 MARK_FINAL_PARMS (meth, orig_arg);
4400
5e942c50
APB
4401 /* Fix the method argument list so we have the argument name
4402 information */
4403 fix_method_argument_names (orig_arg, meth);
4404
4405 /* Register the parameter number and re-install the current line
4406 number */
e04a16fb
AG
4407 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4408 lineno = saved_lineno;
b9f7e36c
APB
4409
4410 /* Register exception specified by the `throws' keyword for
4411 resolution and set the method decl appropriate field to the list.
4412 Note: the grammar ensures that what we get here are class
4413 types. */
4414 if (throws)
4415 {
4416 throws = nreverse (throws);
4417 for (current = throws; current; current = TREE_CHAIN (current))
4418 {
4419 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4420 NULL_TREE, NULL_TREE);
4421 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4422 &TREE_VALUE (current);
4423 }
4424 DECL_FUNCTION_THROWS (meth) = throws;
4425 }
4426
e04a16fb
AG
4427 /* We set the DECL_NAME to ID so we can track the location where
4428 the function was declared. This allow us to report
4429 redefinition error accurately. When method are verified,
4430 DECL_NAME is reinstalled properly (using the content of the
4431 WFL node ID) (see check_method_redefinition). We don't do that
22eed1e6
APB
4432 when Object is being defined. Constructor <init> names will be
4433 reinstalled the same way. */
c2952b01 4434 if (TREE_TYPE (GET_CPC ()) != object_type_node)
e04a16fb 4435 DECL_NAME (meth) = id;
22eed1e6
APB
4436
4437 /* Set the flag if we correctly processed a constructor */
4438 if (constructor_ok)
c2952b01
APB
4439 {
4440 DECL_CONSTRUCTOR_P (meth) = 1;
4441 /* Compute and store the number of artificial parameters declared
4442 for this constructor */
4443 for (count = 0, current = TYPE_FIELDS (this_class); current;
4444 current = TREE_CHAIN (current))
4445 if (FIELD_LOCAL_ALIAS (current))
4446 count++;
4447 DECL_FUNCTION_NAP (meth) = count;
4448 }
22eed1e6 4449
5e942c50
APB
4450 /* Eventually set the @deprecated tag flag */
4451 CHECK_DEPRECATED (meth);
4452
7f10c2e2
APB
4453 /* If doing xref, store column and line number information instead
4454 of the line number only. */
4455 if (flag_emit_xref)
4456 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4457
e04a16fb
AG
4458 return meth;
4459}
4460
5e942c50
APB
4461static void
4462fix_method_argument_names (orig_arg, meth)
4463 tree orig_arg, meth;
4464{
4465 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4466 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4467 {
4468 TREE_PURPOSE (arg) = this_identifier_node;
4469 arg = TREE_CHAIN (arg);
4470 }
de4c7b02 4471 while (orig_arg != end_params_node)
5e942c50
APB
4472 {
4473 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4474 orig_arg = TREE_CHAIN (orig_arg);
4475 arg = TREE_CHAIN (arg);
4476 }
4477}
4478
22eed1e6
APB
4479/* Complete the method declaration with METHOD_BODY. */
4480
4481static void
b635eb2f 4482finish_method_declaration (method_body)
22eed1e6
APB
4483 tree method_body;
4484{
79d13333
APB
4485 int flags;
4486
4487 if (!current_function_decl)
4488 return;
4489
4490 flags = get_access_flags_from_decl (current_function_decl);
5256aa37
APB
4491
4492 /* 8.4.5 Method Body */
4493 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4494 {
4495 tree wfl = DECL_NAME (current_function_decl);
4496 parse_error_context (wfl,
4497 "%s method `%s' can't have a body defined",
4498 (METHOD_NATIVE (current_function_decl) ?
4499 "Native" : "Abstract"),
4500 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4501 method_body = NULL_TREE;
4502 }
4503 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4504 {
4505 tree wfl = DECL_NAME (current_function_decl);
781b0558
KG
4506 parse_error_context
4507 (wfl,
4508 "Non native and non abstract method `%s' must have a body defined",
4509 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5256aa37
APB
4510 method_body = NULL_TREE;
4511 }
4512
2c56429a
APB
4513 if (flag_emit_class_files && method_body
4514 && TREE_CODE (method_body) == NOP_EXPR
4515 && TREE_TYPE (current_function_decl)
4516 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4517 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
e803d3b2 4518
22eed1e6
APB
4519 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4520 maybe_absorb_scoping_blocks ();
4521 /* Exit function's body */
4522 exit_block ();
4523 /* Merge last line of the function with first line, directly in the
4524 function decl. It will be used to emit correct debug info. */
7f10c2e2
APB
4525 if (!flag_emit_xref)
4526 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
c2952b01
APB
4527
4528 /* Since function's argument's list are shared, reset the
4529 ARG_FINAL_P parameter that might have been set on some of this
4530 function parameters. */
4531 UNMARK_FINAL_PARMS (current_function_decl);
4532
f099f336
APB
4533 /* So we don't have an irrelevant function declaration context for
4534 the next static block we'll see. */
4535 current_function_decl = NULL_TREE;
22eed1e6
APB
4536}
4537
4538/* Build a an error message for constructor circularity errors. */
4539
4540static char *
4541constructor_circularity_msg (from, to)
4542 tree from, to;
4543{
4544 static char string [4096];
c2e3db92 4545 char *t = xstrdup (lang_printable_name (from, 0));
22eed1e6
APB
4546 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4547 free (t);
4548 return string;
4549}
4550
4551/* Verify a circular call to METH. Return 1 if an error is found, 0
4552 otherwise. */
4553
4554static int
4555verify_constructor_circularity (meth, current)
4556 tree meth, current;
4557{
4558 static tree list = NULL_TREE;
4559 tree c;
4560 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4561 {
4562 if (TREE_VALUE (c) == meth)
4563 {
4564 char *t;
4565 if (list)
4566 {
4567 tree liste;
4568 list = nreverse (list);
4569 for (liste = list; liste; liste = TREE_CHAIN (liste))
4570 {
4571 parse_error_context
c63b98cd 4572 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
22eed1e6
APB
4573 constructor_circularity_msg
4574 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4575 java_error_count--;
4576 }
4577 }
c2e3db92 4578 t = xstrdup (lang_printable_name (meth, 0));
22eed1e6
APB
4579 parse_error_context (TREE_PURPOSE (c),
4580 "%s: recursive invocation of constructor `%s'",
4581 constructor_circularity_msg (current, meth), t);
4582 free (t);
4583 list = NULL_TREE;
4584 return 1;
4585 }
4586 }
4587 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4588 {
4589 list = tree_cons (c, current, list);
4590 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4591 return 1;
4592 list = TREE_CHAIN (list);
4593 }
4594 return 0;
4595}
4596
e04a16fb
AG
4597/* Check modifiers that can be declared but exclusively */
4598
4599static void
4600check_modifiers_consistency (flags)
4601 int flags;
4602{
4603 int acc_count = 0;
4604 tree cl = NULL_TREE;
4605
e0fc4118
TT
4606 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4607 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4608 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
e04a16fb
AG
4609 if (acc_count > 1)
4610 parse_error_context
e0fc4118
TT
4611 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4612
4613 acc_count = 0;
4614 cl = NULL_TREE;
4615 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK - PUBLIC_TK,
4616 acc_count, cl);
4617 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK - PUBLIC_TK,
4618 acc_count, cl);
4619 if (acc_count > 1)
4620 parse_error_context (cl,
4621 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
e04a16fb
AG
4622}
4623
4624/* Check the methode header METH for abstract specifics features */
4625
4626static void
4627check_abstract_method_header (meth)
4628 tree meth;
4629{
4630 int flags = get_access_flags_from_decl (meth);
4631 /* DECL_NAME might still be a WFL node */
c877974e 4632 tree name = GET_METHOD_NAME (meth);
e04a16fb 4633
2884c41e
KG
4634 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4635 ACC_ABSTRACT, "abstract method",
4636 IDENTIFIER_POINTER (name));
4637 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4638 ACC_PUBLIC, "abstract method",
4639 IDENTIFIER_POINTER (name));
e04a16fb
AG
4640
4641 check_modifiers ("Illegal modifier `%s' for interface method",
4642 flags, INTERFACE_METHOD_MODIFIERS);
4643}
4644
4645/* Create a FUNCTION_TYPE node and start augmenting it with the
4646 declared function arguments. Arguments type that can't be resolved
4647 are left as they are, but the returned node is marked as containing
4648 incomplete types. */
4649
4650static tree
4651method_declarator (id, list)
4652 tree id, list;
4653{
4654 tree arg_types = NULL_TREE, current, node;
4655 tree meth = make_node (FUNCTION_TYPE);
4656 jdep *jdep;
e04a16fb
AG
4657
4658 patch_stage = JDEP_NO_PATCH;
c2952b01
APB
4659
4660 /* If we're dealing with an inner class constructor, we hide the
4661 this$<n> decl in the name field of its parameter declaration. We
4662 also might have to hide the outer context local alias
4663 initializers. Not done when the class is a toplevel class. */
4664 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4665 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4666 {
4667 tree aliases_list, type, thisn;
4668 /* First the aliases, linked to the regular parameters */
4669 aliases_list =
4670 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4671 TREE_TYPE (GET_CPC ()),
4672 NULL_TREE, NULL);
4673 list = chainon (nreverse (aliases_list), list);
4674
4675 /* Then this$<n> */
4676 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4677 thisn = build_current_thisn (TYPE_NAME (GET_CPC ()));
4678 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4679 list);
4680 }
e04a16fb
AG
4681
4682 for (current = list; current; current = TREE_CHAIN (current))
4683 {
c583dd46 4684 int must_chain = 0;
e04a16fb
AG
4685 tree wfl_name = TREE_PURPOSE (current);
4686 tree type = TREE_VALUE (current);
4687 tree name = EXPR_WFL_NODE (wfl_name);
c583dd46
APB
4688 tree already, arg_node;
4689 tree type_wfl = NULL_TREE;
23a79c61 4690 tree real_type;
c583dd46
APB
4691
4692 /* Obtain a suitable type for resolution, if necessary */
4693 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4694
4695 /* Process NAME, as it may specify extra dimension(s) for it */
4696 type = build_array_from_name (type, type_wfl, name, &name);
4697 EXPR_WFL_NODE (wfl_name) = name;
e04a16fb 4698
23a79c61
APB
4699 real_type = GET_REAL_TYPE (type);
4700 if (TREE_CODE (real_type) == RECORD_TYPE)
4701 {
4702 real_type = promote_type (real_type);
4703 if (TREE_CODE (type) == TREE_LIST)
4704 TREE_PURPOSE (type) = real_type;
4705 }
5e942c50 4706
e04a16fb
AG
4707 /* Check redefinition */
4708 for (already = arg_types; already; already = TREE_CHAIN (already))
4709 if (TREE_PURPOSE (already) == name)
4710 {
781b0558
KG
4711 parse_error_context
4712 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4713 IDENTIFIER_POINTER (name),
e04a16fb
AG
4714 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4715 break;
4716 }
4717
4718 /* If we've an incomplete argument type, we know there is a location
4719 to patch when the type get resolved, later. */
4720 jdep = NULL;
c583dd46 4721 if (must_chain)
e04a16fb 4722 {
c583dd46
APB
4723 patch_stage = JDEP_METHOD;
4724 type = register_incomplete_type (patch_stage,
4725 type_wfl, wfl_name, type);
4726 jdep = CLASSD_LAST (ctxp->classd_list);
4727 JDEP_MISC (jdep) = id;
e04a16fb 4728 }
c583dd46 4729
c2952b01 4730 /* The argument node: a name and a (possibly) incomplete type. */
23a79c61 4731 arg_node = build_tree_list (name, real_type);
c2952b01
APB
4732 /* Remeber arguments declared final. */
4733 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4734
e04a16fb
AG
4735 if (jdep)
4736 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4737 TREE_CHAIN (arg_node) = arg_types;
4738 arg_types = arg_node;
4739 }
de4c7b02 4740 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
e04a16fb
AG
4741 node = build_tree_list (id, meth);
4742 return node;
4743}
4744
4745static int
4746unresolved_type_p (wfl, returned)
4747 tree wfl;
4748 tree *returned;
4749
4750{
4751 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4752 {
e04a16fb 4753 if (returned)
165f37bc
APB
4754 {
4755 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4756 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4757 *returned = TREE_TYPE (decl);
4758 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4759 *returned = TREE_TYPE (GET_CPC ());
4760 else
4761 *returned = NULL_TREE;
4762 }
e04a16fb
AG
4763 return 1;
4764 }
4765 if (returned)
4766 *returned = wfl;
4767 return 0;
4768}
4769
4770/* From NAME, build a qualified identifier node using the
4771 qualification from the current package definition. */
4772
4773static tree
c2952b01
APB
4774parser_qualified_classname (is_static, name)
4775 int is_static;
e04a16fb
AG
4776 tree name;
4777{
c2952b01
APB
4778 tree nested_class_name;
4779
4780 if (!is_static
4781 && (nested_class_name = maybe_make_nested_class_name (name)))
4782 return nested_class_name;
4783
e04a16fb 4784 if (ctxp->package)
c2952b01 4785 return merge_qualified_name (ctxp->package, name);
e04a16fb 4786 else
c2952b01 4787 return name;
e04a16fb
AG
4788}
4789
4790/* Called once the type a interface extends is resolved. Returns 0 if
4791 everything is OK. */
4792
4793static int
4794parser_check_super_interface (super_decl, this_decl, this_wfl)
4795 tree super_decl, this_decl, this_wfl;
4796{
4797 tree super_type = TREE_TYPE (super_decl);
4798
4799 /* Has to be an interface */
c2952b01 4800 if (!CLASS_INTERFACE (super_decl))
e04a16fb
AG
4801 {
4802 parse_error_context
4803 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4804 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4805 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4806 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4807 "interface" : "class"),
4808 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4809 return 1;
4810 }
4811
4812 /* Check scope: same package OK, other package: OK if public */
4813 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4814 return 1;
4815
4816 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4817 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4818 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4819 return 0;
4820}
4821
4822/* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4823 0 if everthing is OK. */
4824
4825static int
4826parser_check_super (super_decl, this_decl, wfl)
4827 tree super_decl, this_decl, wfl;
4828{
e04a16fb
AG
4829 tree super_type = TREE_TYPE (super_decl);
4830
4831 /* SUPER should be a CLASS (neither an array nor an interface) */
4832 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4833 {
4834 parse_error_context
4835 (wfl, "Class `%s' can't subclass %s `%s'",
4836 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4837 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4838 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4839 return 1;
4840 }
4841
4842 if (CLASS_FINAL (TYPE_NAME (super_type)))
4843 {
4844 parse_error_context (wfl, "Can't subclass final classes: %s",
4845 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4846 return 1;
4847 }
4848
4849 /* Check scope: same package OK, other package: OK if public */
4850 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4851 return 1;
4852
4853 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4854 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4855 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4856 return 0;
4857}
4858
4859/* Create a new dependency list and link it (in a LIFO manner) to the
4860 CTXP list of type dependency list. */
4861
4862static void
4863create_jdep_list (ctxp)
4864 struct parser_ctxt *ctxp;
4865{
23a79c61 4866 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
e04a16fb
AG
4867 new->first = new->last = NULL;
4868 new->next = ctxp->classd_list;
4869 ctxp->classd_list = new;
4870}
4871
4872static jdeplist *
4873reverse_jdep_list (ctxp)
4874 struct parser_ctxt *ctxp;
4875{
4876 register jdeplist *prev = NULL, *current, *next;
4877 for (current = ctxp->classd_list; current; current = next)
4878 {
4879 next = current->next;
4880 current->next = prev;
4881 prev = current;
4882 }
4883 return prev;
4884}
4885
23a79c61
APB
4886/* Create a fake pointer based on the ID stored in
4887 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4888 registered again. */
e04a16fb
AG
4889
4890static tree
23a79c61
APB
4891obtain_incomplete_type (type_name)
4892 tree type_name;
e04a16fb 4893{
23a79c61
APB
4894 tree ptr, name;
4895
4896 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4897 name = EXPR_WFL_NODE (type_name);
4898 else if (INCOMPLETE_TYPE_P (type_name))
4899 name = TYPE_NAME (type_name);
4900 else
4901 fatal ("invalid type name - obtain_incomplete_type");
e04a16fb
AG
4902
4903 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
78d21f92 4904 if (TYPE_NAME (ptr) == name)
e04a16fb
AG
4905 break;
4906
4907 if (!ptr)
4908 {
e04a16fb 4909 push_obstacks (&permanent_obstack, &permanent_obstack);
78d21f92
PB
4910 BUILD_PTR_FROM_NAME (ptr, name);
4911 layout_type (ptr);
e04a16fb
AG
4912 pop_obstacks ();
4913 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4914 ctxp->incomplete_class = ptr;
4915 }
4916
4917 return ptr;
4918}
4919
4920/* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4921 non NULL instead of computing a new fake type based on WFL. The new
4922 dependency is inserted in the current type dependency list, in FIFO
4923 manner. */
4924
4925static tree
4926register_incomplete_type (kind, wfl, decl, ptr)
4927 int kind;
4928 tree wfl, decl, ptr;
4929{
23a79c61 4930 jdep *new = (jdep *)xmalloc (sizeof (jdep));
e04a16fb 4931
e04a16fb
AG
4932 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4933 ptr = obtain_incomplete_type (wfl);
4934
4935 JDEP_KIND (new) = kind;
4936 JDEP_DECL (new) = decl;
4937 JDEP_SOLV (new) = ptr;
4938 JDEP_WFL (new) = wfl;
4939 JDEP_CHAIN (new) = NULL;
4940 JDEP_MISC (new) = NULL_TREE;
e803d3b2
APB
4941 /* For some dependencies, set the enclosing class of the current
4942 class to be the enclosing context */
4943 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
165f37bc
APB
4944 && GET_ENCLOSING_CPC ())
4945 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4946 else
324ed8fd 4947 JDEP_ENCLOSING (new) = GET_CPC ();
e04a16fb
AG
4948 JDEP_GET_PATCH (new) = (tree *)NULL;
4949
4950 JDEP_INSERT (ctxp->classd_list, new);
4951
4952 return ptr;
4953}
4954
4955void
4956java_check_circular_reference ()
4957{
4958 tree current;
4959 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4960 {
4961 tree type = TREE_TYPE (current);
e920ebc9 4962 if (CLASS_INTERFACE (current))
e04a16fb
AG
4963 {
4964 /* Check all interfaces this class extends */
4965 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4966 int n, i;
4967
4968 if (!basetype_vec)
4969 return;
4970 n = TREE_VEC_LENGTH (basetype_vec);
4971 for (i = 0; i < n; i++)
4972 {
4973 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4974 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4975 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4976 parse_error_context (lookup_cl (current),
4977 "Cyclic interface inheritance");
4978 }
4979 }
4980 else
4981 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
4982 parse_error_context (lookup_cl (current),
c2952b01
APB
4983 "Cyclic class inheritance%s",
4984 (cyclic_inheritance_report ?
4985 cyclic_inheritance_report : ""));
4986 }
4987}
4988
4989/* Augment the parameter list PARM with parameters crafted to
4990 initialize outer context locals aliases. Through ARTIFICIAL, a
4991 count is kept of the number of crafted parameters. MODE governs
4992 what eventually gets created: something suitable for a function
4993 creation or a function invocation, either the constructor or
4994 $finit$. */
4995
4996static tree
4997build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
4998 int mode;
4999 tree class_type, parm;
5000 int *artificial;
5001{
5002 tree field;
5003 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5004 if (FIELD_LOCAL_ALIAS (field))
5005 {
5006 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5007 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5008
5009 switch (mode)
5010 {
5011 case AIPL_FUNCTION_DECLARATION:
5012 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5013 purpose = build_wfl_node (get_identifier (buffer));
5014 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5015 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5016 else
5017 value = TREE_TYPE (field);
5018 break;
5019
5020 case AIPL_FUNCTION_CREATION:
5021 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5022 purpose = get_identifier (buffer);
5023 value = TREE_TYPE (field);
5024 break;
5025
5026 case AIPL_FUNCTION_FINIT_INVOCATION:
5027 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5028 /* Now, this is wrong. purpose should always be the NAME
5029 of something and value its matching value (decl, type,
5030 etc...) FIXME -- but there is a lot to fix. */
5031
5032 /* When invoked for this kind of operation, we already
5033 know whether a field is used or not. */
5034 purpose = TREE_TYPE (field);
5035 value = build_wfl_node (get_identifier (buffer));
5036 break;
5037
5038 case AIPL_FUNCTION_CTOR_INVOCATION:
5039 /* There are two case: the constructor invokation happends
5040 outside the local inner, in which case, locales from the outer
5041 context are directly used.
5042
5043 Otherwise, we fold to using the alias directly. */
5044 if (class_type == current_class)
5045 value = field;
5046 else
5047 {
5048 name = get_identifier (&buffer[4]);
5049 value = IDENTIFIER_LOCAL_VALUE (name);
5050 }
5051 break;
5052 }
5053 parm = tree_cons (purpose, value, parm);
5054 if (artificial)
5055 *artificial +=1;
5056 }
5057 return parm;
5058}
5059
5060/* Craft a constructor for CLASS_DECL -- what we should do when none
5061 where found. ARGS is non NULL when a special signature must be
5062 enforced. This is the case for anonymous classes. */
5063
5064static void
5065craft_constructor (class_decl, args)
5066 tree class_decl, args;
5067{
5068 tree class_type = TREE_TYPE (class_decl);
5069 tree parm = NULL_TREE;
5070 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5071 ACC_PUBLIC : 0);
5072 int i = 0, artificial = 0;
5073 tree decl, ctor_name;
5074 char buffer [80];
5075
5076 push_obstacks (&permanent_obstack, &permanent_obstack);
5077
5078 /* The constructor name is <init> unless we're dealing with an
5079 anonymous class, in which case the name will be fixed after having
5080 be expanded. */
5081 if (ANONYMOUS_CLASS_P (class_type))
5082 ctor_name = DECL_NAME (class_decl);
5083 else
5084 ctor_name = init_identifier_node;
5085
5086 /* If we're dealing with an inner class constructor, we hide the
5087 this$<n> decl in the name field of its parameter declaration. */
5088 if (PURE_INNER_CLASS_TYPE_P (class_type))
5089 {
5090 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5091 parm = tree_cons (build_current_thisn (class_type),
5092 build_pointer_type (type), parm);
5093
5094 /* Some more arguments to be hidden here. The values of the local
5095 variables of the outer context that the inner class needs to see. */
5096 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5097 class_type, parm,
5098 &artificial);
5099 }
5100
5101 /* Then if there are any args to be enforced, enforce them now */
5102 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5103 {
5104 sprintf (buffer, "parm%d", i++);
5105 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
e04a16fb 5106 }
c2952b01
APB
5107
5108 CRAFTED_PARAM_LIST_FIXUP (parm);
5109 decl = create_artificial_method (class_type, flags, void_type_node,
5110 ctor_name, parm);
5111 fix_method_argument_names (parm, decl);
5112 /* Now, mark the artificial parameters. */
5113 DECL_FUNCTION_NAP (decl) = artificial;
5114
5115 pop_obstacks ();
5116 DECL_CONSTRUCTOR_P (decl) = 1;
e04a16fb
AG
5117}
5118
c2952b01 5119
e920ebc9
APB
5120/* Fix the constructors. This will be called right after circular
5121 references have been checked. It is necessary to fix constructors
5122 early even if no code generation will take place for that class:
5123 some generated constructor might be required by the class whose
5124 compilation triggered this one to be simply loaded. */
5125
5126void
5127java_fix_constructors ()
5128{
5129 tree current;
5130
5131 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5132 {
e920ebc9
APB
5133 tree class_type = TREE_TYPE (current);
5134 int saw_ctor = 0;
c2952b01
APB
5135 tree decl;
5136
5137 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5138 continue;
e920ebc9
APB
5139
5140 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5141 {
5142 if (DECL_CONSTRUCTOR_P (decl))
5143 {
5144 fix_constructors (decl);
5145 saw_ctor = 1;
5146 }
5147 }
5148
c2952b01
APB
5149 /* Anonymous class constructor can't be generated that early. */
5150 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5151 craft_constructor (current, NULL_TREE);
e920ebc9
APB
5152 }
5153}
5154
23a79c61
APB
5155/* safe_layout_class just makes sure that we can load a class without
5156 disrupting the current_class, input_file, lineno, etc, information
5157 about the class processed currently. */
5158
e04a16fb
AG
5159void
5160safe_layout_class (class)
5161 tree class;
5162{
5163 tree save_current_class = current_class;
3b304f5b 5164 const char *save_input_filename = input_filename;
e04a16fb 5165 int save_lineno = lineno;
5e942c50 5166
e04a16fb 5167 push_obstacks (&permanent_obstack, &permanent_obstack);
5e942c50 5168
e04a16fb
AG
5169 layout_class (class);
5170 pop_obstacks ();
5e942c50 5171
e04a16fb
AG
5172 current_class = save_current_class;
5173 input_filename = save_input_filename;
5174 lineno = save_lineno;
5175 CLASS_LOADED_P (class) = 1;
5176}
5177
5178static tree
5179jdep_resolve_class (dep)
5180 jdep *dep;
5181{
5182 tree decl;
5183
23a79c61
APB
5184 if (JDEP_RESOLVED_P (dep))
5185 decl = JDEP_RESOLVED_DECL (dep);
5186 else
e04a16fb 5187 {
c2952b01 5188 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
23a79c61 5189 JDEP_DECL (dep), JDEP_WFL (dep));
e04a16fb
AG
5190 JDEP_RESOLVED (dep, decl);
5191 }
23a79c61 5192
e04a16fb 5193 if (!decl)
23a79c61
APB
5194 complete_class_report_errors (dep);
5195
e04a16fb
AG
5196 return decl;
5197}
5198
5199/* Complete unsatisfied class declaration and their dependencies */
5200
5201void
5202java_complete_class ()
5203{
e04a16fb
AG
5204 tree cclass;
5205 jdeplist *cclassd;
5206 int error_found;
b67d701b 5207 tree type;
e04a16fb
AG
5208
5209 push_obstacks (&permanent_obstack, &permanent_obstack);
5210
5211 /* Process imports and reverse the import on demand list */
5212 process_imports ();
5213 if (ctxp->import_demand_list)
5214 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
5215
5216 /* Rever things so we have the right order */
5217 ctxp->class_list = nreverse (ctxp->class_list);
5218 ctxp->classd_list = reverse_jdep_list (ctxp);
c877974e 5219
e04a16fb
AG
5220 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5221 cclass && cclassd;
5222 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5223 {
5224 jdep *dep;
5225 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5226 {
5227 tree decl;
e04a16fb
AG
5228 if (!(decl = jdep_resolve_class (dep)))
5229 continue;
5230
5231 /* Now it's time to patch */
5232 switch (JDEP_KIND (dep))
5233 {
5234 case JDEP_SUPER:
5235 /* Simply patch super */
5236 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5237 continue;
5238 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5239 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5240 break;
5241
5242 case JDEP_FIELD:
5243 {
5244 /* We do part of the job done in add_field */
5245 tree field_decl = JDEP_DECL (dep);
5246 tree field_type = TREE_TYPE (decl);
5247 push_obstacks (&permanent_obstack, &permanent_obstack);
e04a16fb 5248 if (TREE_CODE (field_type) == RECORD_TYPE)
e04a16fb
AG
5249 field_type = promote_type (field_type);
5250 pop_obstacks ();
5251 TREE_TYPE (field_decl) = field_type;
5e942c50
APB
5252 DECL_ALIGN (field_decl) = 0;
5253 layout_decl (field_decl, 0);
e04a16fb
AG
5254 SOURCE_FRONTEND_DEBUG
5255 (("Completed field/var decl `%s' with `%s'",
5256 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5257 IDENTIFIER_POINTER (DECL_NAME (decl))));
5258 break;
5259 }
5260 case JDEP_METHOD: /* We start patching a method */
5261 case JDEP_METHOD_RETURN:
5262 error_found = 0;
5263 while (1)
5264 {
5265 if (decl)
5266 {
b67d701b
PB
5267 type = TREE_TYPE(decl);
5268 if (TREE_CODE (type) == RECORD_TYPE)
5269 type = promote_type (type);
e04a16fb
AG
5270 JDEP_APPLY_PATCH (dep, type);
5271 SOURCE_FRONTEND_DEBUG
5272 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5273 "Completing fct `%s' with ret type `%s'":
5274 "Completing arg `%s' with type `%s'"),
5275 IDENTIFIER_POINTER (EXPR_WFL_NODE
5276 (JDEP_DECL_WFL (dep))),
5277 IDENTIFIER_POINTER (DECL_NAME (decl))));
5278 }
5279 else
5280 error_found = 1;
5281 dep = JDEP_CHAIN (dep);
5282 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5283 break;
5284 else
5285 decl = jdep_resolve_class (dep);
5286 }
5287 if (!error_found)
5288 {
5289 tree mdecl = JDEP_DECL (dep), signature;
5290 push_obstacks (&permanent_obstack, &permanent_obstack);
165f37bc
APB
5291 /* Recompute and reset the signature, check first that
5292 all types are now defined. If they're not,
5293 dont build the signature. */
5294 if (check_method_types_complete (mdecl))
5295 {
5296 signature = build_java_signature (TREE_TYPE (mdecl));
5297 set_java_signature (TREE_TYPE (mdecl), signature);
5298 }
e04a16fb
AG
5299 pop_obstacks ();
5300 }
5301 else
5302 continue;
5303 break;
5304
5305 case JDEP_INTERFACE:
5306 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5307 JDEP_WFL (dep)))
5308 continue;
5309 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5310 break;
5311
b67d701b 5312 case JDEP_PARM:
e04a16fb 5313 case JDEP_VARIABLE:
b67d701b
PB
5314 type = TREE_TYPE(decl);
5315 if (TREE_CODE (type) == RECORD_TYPE)
5316 type = promote_type (type);
5317 JDEP_APPLY_PATCH (dep, type);
e04a16fb
AG
5318 break;
5319
5320 case JDEP_TYPE:
5321 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5322 SOURCE_FRONTEND_DEBUG
5323 (("Completing a random type dependency on a '%s' node",
5324 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5325 break;
5326
b9f7e36c 5327 case JDEP_EXCEPTION:
c877974e
APB
5328 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5329 SOURCE_FRONTEND_DEBUG
5330 (("Completing `%s' `throws' argument node",
5331 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
b9f7e36c
APB
5332 break;
5333
c2952b01
APB
5334 case JDEP_ANONYMOUS:
5335 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5336 break;
5337
e04a16fb 5338 default:
0a2138e2
APB
5339 fatal ("Can't handle patch code %d - java_complete_class",
5340 JDEP_KIND (dep));
e04a16fb
AG
5341 }
5342 }
5343 }
5344 pop_obstacks ();
5345 return;
5346}
5347
5348/* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5349 array. */
5350
5351static tree
c2952b01
APB
5352resolve_class (enclosing, class_type, decl, cl)
5353 tree enclosing, class_type, decl, cl;
e04a16fb 5354{
49f48c71
KG
5355 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5356 const char *base = name;
78d21f92
PB
5357 tree resolved_type = TREE_TYPE (class_type);
5358 tree resolved_type_decl;
e04a16fb 5359
78d21f92
PB
5360 if (resolved_type != NULL_TREE)
5361 {
5362 tree resolved_type_decl = TYPE_NAME (resolved_type);
5363 if (resolved_type_decl == NULL_TREE
5364 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5365 {
5366 resolved_type_decl = build_decl (TYPE_DECL,
5367 TYPE_NAME (class_type),
5368 resolved_type);
5369 }
5370 return resolved_type_decl;
5371 }
5372
e04a16fb
AG
5373 /* 1- Check to see if we have an array. If true, find what we really
5374 want to resolve */
5375 while (name[0] == '[')
5376 name++;
5377 if (base != name)
5378 TYPE_NAME (class_type) = get_identifier (name);
5379
5380 /* 2- Resolve the bare type */
c2952b01
APB
5381 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5382 decl, cl)))
e04a16fb
AG
5383 return NULL_TREE;
5384 resolved_type = TREE_TYPE (resolved_type_decl);
5385
5386 /* 3- If we have and array, reconstruct the array down to its nesting */
5387 if (base != name)
5388 {
5389 while (base != name)
5390 {
5391 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5392 resolved_type = promote_type (resolved_type);
5393 resolved_type = build_java_array_type (resolved_type, -1);
c583dd46 5394 CLASS_LOADED_P (resolved_type) = 1;
e04a16fb
AG
5395 name--;
5396 }
5397 /* Build a fake decl for this, since this is what is expected to
5398 be returned. */
5399 resolved_type_decl =
5400 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5401 /* Figure how those two things are important for error report. FIXME */
5402 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5403 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
78d21f92 5404 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
e04a16fb 5405 }
78d21f92 5406 TREE_TYPE (class_type) = resolved_type;
e04a16fb
AG
5407 return resolved_type_decl;
5408}
5409
5410/* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5411 are used to report error messages. */
5412
78d21f92 5413tree
c2952b01
APB
5414do_resolve_class (enclosing, class_type, decl, cl)
5415 tree enclosing, class_type, decl, cl;
e04a16fb
AG
5416{
5417 tree new_class_decl;
5418 tree original_name = NULL_TREE;
5419
5420 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5421 its is changed by find_in_imports{_on_demand} */
5422
c2952b01
APB
5423 /* 0- Search in the current class as an inner class */
5424
5425 /* Maybe some code here should be added to load the class or
5426 something, at least if the class isn't an inner class and ended
5427 being loaded from class file. FIXME. */
a40d21da
APB
5428 while (enclosing)
5429 {
5430 tree name;
5431
5432 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5433 return new_class_decl;
5434
5435 /* Now go to the upper classes, bail out if necessary. */
5436 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5437 if (!enclosing || enclosing == object_type_node)
5438 break;
5439
5440 if (TREE_CODE (enclosing) == RECORD_TYPE)
5441 {
5442 enclosing = TYPE_NAME (enclosing);
5443 continue;
5444 }
5445
5446 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5447 {
5448 BUILD_PTR_FROM_NAME (name, enclosing);
5449 }
5450 else
5451 name = enclosing;
5452 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5453 }
c2952b01 5454
e04a16fb
AG
5455 /* 1- Check for the type in single imports */
5456 if (find_in_imports (class_type))
5457 return NULL_TREE;
5458
5459 /* 2- And check for the type in the current compilation unit. If it fails,
ee07f4f4 5460 try with a name qualified with the package name we've seen so far */
e04a16fb
AG
5461 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5462 {
5463 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5464 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5465 load_class (TYPE_NAME (class_type), 0);
5466 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5467 }
5468
5469 original_name = TYPE_NAME (class_type);
ee07f4f4 5470 if (!QUALIFIED_P (TYPE_NAME (class_type)))
bc3ca41b 5471 {
ee07f4f4
APB
5472 tree package;
5473 for (package = package_list; package; package = TREE_CHAIN (package))
c2952b01
APB
5474 {
5475 tree new_qualified;
5476
5477 new_qualified = merge_qualified_name (TREE_PURPOSE (package),
5478 original_name);
5479 TYPE_NAME (class_type) = new_qualified;
5480 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5481 if (!new_class_decl)
5482 load_class (TYPE_NAME (class_type), 0);
5483 new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5484 if (new_class_decl)
5485 {
5486 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5487 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5488 load_class (TYPE_NAME (class_type), 0);
5489 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5490 }
bc3ca41b
PB
5491 }
5492 }
c2952b01 5493
e04a16fb
AG
5494 TYPE_NAME (class_type) = original_name;
5495
5496 /* 3- Check an other compilation unit that bears the name of type */
5497 load_class (TYPE_NAME (class_type), 0);
5498 if (check_pkg_class_access (TYPE_NAME (class_type),
5499 (cl ? cl : lookup_cl (decl))))
5500 return NULL_TREE;
5501
5502 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5503 return new_class_decl;
5504
5505 /* 4- Check the import on demands. Don't allow bar.baz to be
5506 imported from foo.* */
5507 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5508 if (find_in_imports_on_demand (class_type))
5509 return NULL_TREE;
5510
5511 /* 5- Last call for a resolution */
5512 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5513}
5514
5515/* Resolve NAME and lay it out (if not done and if not the current
23a79c61
APB
5516 parsed class). Return a decl node. This function is meant to be
5517 called when type resolution is necessary during the walk pass. */
e04a16fb
AG
5518
5519static tree
c877974e
APB
5520resolve_and_layout (something, cl)
5521 tree something;
e04a16fb
AG
5522 tree cl;
5523{
c877974e
APB
5524 tree decl;
5525
23a79c61
APB
5526 /* Don't do that on the current class */
5527 if (something == current_class)
5528 return TYPE_NAME (current_class);
c877974e 5529
23a79c61 5530 /* Don't do anything for void and other primitive types */
c877974e
APB
5531 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5532 return NULL_TREE;
5533
23a79c61
APB
5534 /* Pointer types can be reall pointer types or fake pointers. When
5535 finding a real pointer, recheck for primitive types */
5536 if (TREE_CODE (something) == POINTER_TYPE)
5537 {
5538 if (TREE_TYPE (something))
5539 {
5540 something = TREE_TYPE (something);
5541 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5542 return NULL_TREE;
5543 }
5544 else
5545 something = TYPE_NAME (something);
5546 }
5547
5548 /* Don't do anything for arrays of primitive types */
5549 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5550 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5551 return NULL_TREE;
5552
c2952b01
APB
5553 /* Something might be a WFL */
5554 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5555 something = EXPR_WFL_NODE (something);
5556
5557 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5558 TYPE_DECL or a real TYPE */
5559 else if (TREE_CODE (something) != IDENTIFIER_NODE)
c877974e
APB
5560 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5561 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5562
23a79c61
APB
5563 if (!(decl = resolve_no_layout (something, cl)))
5564 return NULL_TREE;
5565
5566 /* Resolve and layout if necessary */
5567 layout_class_methods (TREE_TYPE (decl));
7705e9db
APB
5568 /* Check methods, but only once */
5569 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5570 && !CLASS_LOADED_P (TREE_TYPE (decl)))
23a79c61
APB
5571 CHECK_METHODS (decl);
5572 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
e04a16fb 5573 safe_layout_class (TREE_TYPE (decl));
23a79c61 5574
e04a16fb
AG
5575 return decl;
5576}
5577
5578/* Resolve a class, returns its decl but doesn't perform any
5579 layout. The current parsing context is saved and restored */
5580
5581static tree
5582resolve_no_layout (name, cl)
5583 tree name, cl;
5584{
5585 tree ptr, decl;
5586 BUILD_PTR_FROM_NAME (ptr, name);
5587 java_parser_context_save_global ();
c2952b01 5588 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
e04a16fb
AG
5589 java_parser_context_restore_global ();
5590
5591 return decl;
5592}
5593
23a79c61
APB
5594/* Called when reporting errors. Skip leader '[' in a complex array
5595 type description that failed to be resolved. */
e04a16fb 5596
49f48c71 5597static const char *
e04a16fb 5598purify_type_name (name)
49f48c71 5599 const char *name;
e04a16fb
AG
5600{
5601 while (*name && *name == '[')
5602 name++;
5603 return name;
5604}
5605
5606/* The type CURRENT refers to can't be found. We print error messages. */
5607
5608static void
5609complete_class_report_errors (dep)
5610 jdep *dep;
5611{
49f48c71 5612 const char *name;
23a79c61
APB
5613
5614 if (!JDEP_WFL (dep))
5615 return;
5616
5617 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
e04a16fb
AG
5618 switch (JDEP_KIND (dep))
5619 {
5620 case JDEP_SUPER:
5621 parse_error_context
5622 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
23a79c61 5623 purify_type_name (name),
e04a16fb
AG
5624 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5625 break;
5626 case JDEP_FIELD:
5627 parse_error_context
5628 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
23a79c61 5629 purify_type_name (name),
e04a16fb
AG
5630 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5631 break;
5632 case JDEP_METHOD: /* Covers arguments */
5633 parse_error_context
781b0558 5634 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
23a79c61 5635 purify_type_name (name),
e04a16fb
AG
5636 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5637 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5638 break;
5639 case JDEP_METHOD_RETURN: /* Covers return type */
5640 parse_error_context
781b0558 5641 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
23a79c61 5642 purify_type_name (name),
e04a16fb
AG
5643 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5644 break;
5645 case JDEP_INTERFACE:
5646 parse_error_context
5647 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5648 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5649 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5650 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5651 break;
5652 case JDEP_VARIABLE:
5653 parse_error_context
781b0558 5654 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
b67d701b
PB
5655 purify_type_name (IDENTIFIER_POINTER
5656 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
e04a16fb
AG
5657 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5658 break;
b9f7e36c
APB
5659 case JDEP_EXCEPTION: /* As specified by `throws' */
5660 parse_error_context
5661 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5662 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5663 break;
0a2138e2
APB
5664 default:
5665 /* Fix for -Wall. Just break doing nothing. The error will be
5666 caught later */
5667 break;
e04a16fb
AG
5668 }
5669}
5670
22eed1e6
APB
5671/* Return a static string containing the DECL prototype string. If
5672 DECL is a constructor, use the class name instead of the form
5673 <init> */
5674
49f48c71 5675static const char *
22eed1e6
APB
5676get_printable_method_name (decl)
5677 tree decl;
5678{
49f48c71 5679 const char *to_return;
9ee9b555 5680 tree name = NULL_TREE;
22eed1e6
APB
5681
5682 if (DECL_CONSTRUCTOR_P (decl))
5683 {
5684 name = DECL_NAME (decl);
5e942c50 5685 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
22eed1e6
APB
5686 }
5687
5688 to_return = lang_printable_name (decl, 0);
5689 if (DECL_CONSTRUCTOR_P (decl))
5690 DECL_NAME (decl) = name;
5691
5692 return to_return;
5693}
5694
5e942c50
APB
5695/* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5696 nevertheless needs to be verfied, 1 otherwise. */
5697
5698static int
5699reset_method_name (method)
5700 tree method;
5701{
c2952b01 5702 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5e942c50
APB
5703 {
5704 /* NAME is just the plain name when Object is being defined */
5705 if (DECL_CONTEXT (method) != object_type_node)
c877974e
APB
5706 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5707 init_identifier_node : GET_METHOD_NAME (method));
5e942c50
APB
5708 return 0;
5709 }
5710 else
5711 return 1;
5712}
5713
c877974e
APB
5714/* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5715
5716tree
5717java_get_real_method_name (method_decl)
5718 tree method_decl;
5719{
5720 tree method_name = DECL_NAME (method_decl);
5721 if (DECL_CONSTRUCTOR_P (method_decl))
5722 return init_identifier_node;
82371d41
APB
5723
5724 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5725 and still can be a constructor. FIXME */
5726
23a79c61
APB
5727 /* Don't confuse method only bearing the name of their class as
5728 constructors */
82371d41
APB
5729 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5730 && ctxp
c2952b01 5731 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
23a79c61
APB
5732 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5733 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
c877974e
APB
5734 return init_identifier_node;
5735 else
5736 return EXPR_WFL_NODE (method_name);
5737}
5738
22eed1e6
APB
5739/* Track method being redefined inside the same class. As a side
5740 effect, set DECL_NAME to an IDENTIFIER (prior entering this
d77613be 5741 function it's a FWL, so we can track errors more accurately.) */
22eed1e6 5742
e04a16fb
AG
5743static int
5744check_method_redefinition (class, method)
5745 tree class, method;
5746{
5747 tree redef, name;
5748 tree cl = DECL_NAME (method);
c3f2a476 5749 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
ba179f9f
APB
5750 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5751 fixed and checked */
5e942c50
APB
5752
5753 /* Reset the method name before running the check. If it returns 1,
5754 the method doesn't need to be verified with respect to method
5755 redeclaration and we return 0 */
5756 if (reset_method_name (method))
e04a16fb 5757 return 0;
5e942c50
APB
5758
5759 name = DECL_NAME (method);
e04a16fb
AG
5760 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5761 {
c3f2a476 5762 if (redef == method)
e04a16fb 5763 break;
c3f2a476
APB
5764 if (DECL_NAME (redef) == name
5765 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
e04a16fb 5766 {
22eed1e6
APB
5767 parse_error_context
5768 (cl, "Duplicate %s declaration `%s'",
5769 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5770 get_printable_method_name (redef));
e04a16fb
AG
5771 return 1;
5772 }
5773 }
5774 return 0;
5775}
5776
d77613be
APB
5777static void
5778check_abstract_method_definitions (do_interface, class_decl, type)
5779 int do_interface;
5780 tree class_decl, type;
5781{
5782 tree class = TREE_TYPE (class_decl);
5783 tree method, end_type;
5784
5785 end_type = (do_interface ? object_type_node : type);
5786 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5787 {
5788 tree other_super, other_method, method_sig, method_name;
5789 int found = 0;
165f37bc 5790 int end_type_reached = 0;
d77613be
APB
5791
5792 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5793 continue;
5794
5795 /* Now verify that somewhere in between TYPE and CLASS,
5796 abstract method METHOD gets a non abstract definition
5797 that is inherited by CLASS. */
5798
5799 method_sig = build_java_signature (TREE_TYPE (method));
5800 method_name = DECL_NAME (method);
5801 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5802 method_name = EXPR_WFL_NODE (method_name);
5803
165f37bc
APB
5804 other_super = class;
5805 do {
5806 if (other_super == end_type)
5807 end_type_reached = 1;
5808
5809 /* Method search */
5810 for (other_method = TYPE_METHODS (other_super); other_method;
5811 other_method = TREE_CHAIN (other_method))
5812 {
5813 tree s = build_java_signature (TREE_TYPE (other_method));
5814 tree other_name = DECL_NAME (other_method);
5815
5816 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5817 other_name = EXPR_WFL_NODE (other_name);
5818 if (!DECL_CLINIT_P (other_method)
5819 && !DECL_CONSTRUCTOR_P (other_method)
5820 && method_name == other_name && method_sig == s)
5821 {
5822 found = 1;
5823 break;
5824 }
5825 }
5826 other_super = CLASSTYPE_SUPER (other_super);
5827 } while (!end_type_reached);
5828
d77613be
APB
5829 /* Report that abstract METHOD didn't find an implementation
5830 that CLASS can use. */
5831 if (!found)
5832 {
c2e3db92 5833 char *t = xstrdup (lang_printable_name
d77613be
APB
5834 (TREE_TYPE (TREE_TYPE (method)), 0));
5835 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5836 tree saved_wfl = NULL_TREE;
5837
5838 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5839 {
5840 saved_wfl = DECL_NAME (method);
5841 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5842 }
5843
5844 parse_error_context
5845 (lookup_cl (class_decl),
781b0558 5846 "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
5847 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5848 t, lang_printable_name (method, 0),
5849 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5850 "interface" : "class"),
5851 IDENTIFIER_POINTER (ccn),
5852 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5853 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5854
5855 free (t);
5856
5857 if (saved_wfl)
5858 DECL_NAME (method) = saved_wfl;
5859 }
5860 }
5861}
5862
614eaae0 5863/* Check that CLASS_DECL somehow implements all inherited abstract
d77613be
APB
5864 methods. */
5865
5866static void
5867java_check_abstract_method_definitions (class_decl)
5868 tree class_decl;
5869{
5870 tree class = TREE_TYPE (class_decl);
5871 tree super, vector;
5872 int i;
5873
5874 if (CLASS_ABSTRACT (class_decl))
5875 return;
5876
5877 /* Check for inherited types */
165f37bc
APB
5878 super = class;
5879 do {
5880 super = CLASSTYPE_SUPER (super);
5881 check_abstract_method_definitions (0, class_decl, super);
5882 } while (super != object_type_node);
d77613be
APB
5883
5884 /* Check for implemented interfaces. */
5885 vector = TYPE_BINFO_BASETYPES (class);
5886 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5887 {
5888 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5889 check_abstract_method_definitions (1, class_decl, super);
5890 }
5891}
5892
165f37bc
APB
5893/* Check all the types method DECL uses and return 1 if all of them
5894 are now complete, 0 otherwise. This is used to check whether its
5895 safe to build a method signature or not. */
5896
5897static int
5898check_method_types_complete (decl)
5899 tree decl;
5900{
5901 tree type = TREE_TYPE (decl);
5902 tree args;
5903
5904 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5905 return 0;
5906
5907 args = TYPE_ARG_TYPES (type);
5908 if (TREE_CODE (type) == METHOD_TYPE)
5909 args = TREE_CHAIN (args);
5910 for (; args != end_params_node; args = TREE_CHAIN (args))
5911 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5912 return 0;
5913
5914 return 1;
5915}
5916
d77613be
APB
5917/* Check all the methods of CLASS_DECL. Methods are first completed
5918 then checked according to regular method existance rules. If no
5919 constructor for CLASS_DECL were encountered, then build its
5920 declaration. */
e04a16fb
AG
5921
5922static void
5923java_check_regular_methods (class_decl)
5924 tree class_decl;
5925{
c2952b01 5926 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
e04a16fb
AG
5927 tree method;
5928 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5e942c50 5929 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
c877974e
APB
5930 tree mthrows;
5931
5932 /* It is not necessary to check methods defined in java.lang.Object */
5933 if (class == object_type_node)
5934 return;
e04a16fb 5935
23a79c61
APB
5936 if (!TYPE_NVIRTUALS (class))
5937 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb
AG
5938
5939 /* Should take interfaces into account. FIXME */
5940 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5941 {
5e942c50 5942 tree sig;
e04a16fb
AG
5943 tree method_wfl = DECL_NAME (method);
5944 int aflags;
5945
5e942c50
APB
5946 /* If we previously found something and its name was saved,
5947 reinstall it now */
5948 if (found && saved_found_wfl)
ba179f9f
APB
5949 {
5950 DECL_NAME (found) = saved_found_wfl;
5951 saved_found_wfl = NULL_TREE;
5952 }
5e942c50 5953
e04a16fb
AG
5954 /* Check for redefinitions */
5955 if (check_method_redefinition (class, method))
5956 continue;
5957
22eed1e6
APB
5958 /* If we see one constructor a mark so we don't generate the
5959 default one. Also skip other verifications: constructors
5960 can't be inherited hence hiden or overriden */
5961 if (DECL_CONSTRUCTOR_P (method))
5962 {
5963 saw_constructor = 1;
5964 continue;
5965 }
5966
c877974e
APB
5967 /* We verify things thrown by the method. They must inherits from
5968 java.lang.Throwable */
5969 for (mthrows = DECL_FUNCTION_THROWS (method);
5970 mthrows; mthrows = TREE_CHAIN (mthrows))
5971 {
5972 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
5973 parse_error_context
781b0558 5974 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
c877974e
APB
5975 IDENTIFIER_POINTER
5976 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
5977 }
5978
e04a16fb 5979 sig = build_java_argument_signature (TREE_TYPE (method));
614eaae0 5980 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
b9f7e36c 5981
c2952b01
APB
5982 /* Inner class can't declare static methods */
5983 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
5984 {
5985 char *t = xstrdup (lang_printable_name (class, 0));
5986 parse_error_context
5987 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
5988 lang_printable_name (method, 0), t);
5989 free (t);
5990 }
5991
5e942c50 5992 /* Nothing overrides or it's a private method. */
aabd7048 5993 if (!found)
5e942c50 5994 continue;
aabd7048
PB
5995 if (METHOD_PRIVATE (found))
5996 {
5997 found = NULL_TREE;
5998 continue;
5999 }
5e942c50
APB
6000
6001 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6002 We set it temporarily for the sake of the error report. */
6003 saved_found_wfl = DECL_NAME (found);
6004 reset_method_name (found);
6005
614eaae0
APB
6006 /* If `found' is declared in an interface, make sure the
6007 modifier matches. */
6008 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6009 && clinit_identifier_node != DECL_NAME (found)
6010 && !METHOD_PUBLIC (method))
6011 {
6012 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6013 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6014 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6015 lang_printable_name (method, 0),
6016 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6017 }
6018
e04a16fb
AG
6019 /* Can't override a method with the same name and different return
6020 types. */
6021 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
b9f7e36c 6022 {
614eaae0
APB
6023 char *t = xstrdup
6024 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
b9f7e36c 6025 parse_error_context
7f10c2e2 6026 (method_wfl,
b9f7e36c 6027 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6028 lang_printable_name (found, 0), t,
b9f7e36c
APB
6029 IDENTIFIER_POINTER
6030 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6031 free (t);
6032 }
e04a16fb 6033
7f10c2e2
APB
6034 aflags = get_access_flags_from_decl (found);
6035 /* If the method has default, access in an other package, then
6036 issue a warning that the current method doesn't override the
6037 one that was found elsewhere. Do not issue this warning when
6038 the match was found in java.lang.Object. */
6039 if (DECL_CONTEXT (found) != object_type_node
a003f638 6040 && ((aflags & ACC_VISIBILITY) == 0)
7f10c2e2 6041 && !class_in_current_package (DECL_CONTEXT (found))
c2952b01 6042 && !DECL_CLINIT_P (found)
7f10c2e2
APB
6043 && flag_not_overriding)
6044 {
6045 parse_warning_context
781b0558 6046 (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
6047 lang_printable_name (found, 0),
6048 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6049 IDENTIFIER_POINTER (DECL_NAME
6050 (TYPE_NAME (DECL_CONTEXT (found)))));
6051 continue;
6052 }
6053
e04a16fb
AG
6054 /* Can't override final. Can't override static. */
6055 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6056 {
6057 /* Static *can* override static */
6058 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6059 continue;
6060 parse_error_context
6061 (method_wfl,
6062 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6063 (METHOD_FINAL (found) ? "Final" : "Static"),
0a2138e2 6064 lang_printable_name (found, 0),
e04a16fb
AG
6065 (METHOD_FINAL (found) ? "final" : "static"),
6066 IDENTIFIER_POINTER
6067 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6068 continue;
6069 }
7f10c2e2 6070
e04a16fb
AG
6071 /* Static method can't override instance method. */
6072 if (METHOD_STATIC (method))
6073 {
6074 parse_error_context
6075 (method_wfl,
781b0558 6076 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
0a2138e2 6077 lang_printable_name (found, 0),
e04a16fb
AG
6078 IDENTIFIER_POINTER
6079 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6080 continue;
6081 }
5e942c50 6082
5e942c50
APB
6083 /* - Overriding/hiding public must be public
6084 - Overriding/hiding protected must be protected or public
6085 - If the overriden or hidden method has default (package)
6086 access, then the overriding or hiding method must not be
614eaae0
APB
6087 private; otherwise, a compile-time error occurs. If
6088 `found' belongs to an interface, things have been already
6089 taken care of. */
6090 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6091 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6092 || (METHOD_PROTECTED (found)
6093 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6094 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6095 && METHOD_PRIVATE (method))))
e04a16fb
AG
6096 {
6097 parse_error_context
6098 (method_wfl,
781b0558 6099 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
5e942c50
APB
6100 (METHOD_PUBLIC (method) ? "public" :
6101 (METHOD_PRIVATE (method) ? "private" : "protected")),
6102 IDENTIFIER_POINTER (DECL_NAME
6103 (TYPE_NAME (DECL_CONTEXT (found)))));
e04a16fb
AG
6104 continue;
6105 }
6106
b9f7e36c
APB
6107 /* Overriding methods must have compatible `throws' clauses on checked
6108 exceptions, if any */
6109 check_throws_clauses (method, method_wfl, found);
6110
e04a16fb
AG
6111 /* Inheriting multiple methods with the same signature. FIXME */
6112 }
6113
5e942c50
APB
6114 /* Don't forget eventual pending found and saved_found_wfl. Take
6115 into account that we might have exited because we saw an
d77613be 6116 artificial method as the last entry. */
5e942c50
APB
6117
6118 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6119 DECL_NAME (found) = saved_found_wfl;
6120
23a79c61
APB
6121 if (!TYPE_NVIRTUALS (class))
6122 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
e04a16fb 6123
d77613be
APB
6124 /* Search for inherited abstract method not yet implemented in this
6125 class. */
6126 java_check_abstract_method_definitions (class_decl);
6127
22eed1e6 6128 if (!saw_constructor)
e920ebc9 6129 fatal ("No constructor found");
e04a16fb
AG
6130}
6131
b9f7e36c
APB
6132/* Return a non zero value if the `throws' clause of METHOD (if any)
6133 is incompatible with the `throws' clause of FOUND (if any). */
6134
6135static void
6136check_throws_clauses (method, method_wfl, found)
6137 tree method, method_wfl, found;
6138{
6139 tree mthrows, fthrows;
6140
c877974e
APB
6141 /* Can't check these things with class loaded from bytecode. FIXME */
6142 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6143 return;
6144
b9f7e36c
APB
6145 for (mthrows = DECL_FUNCTION_THROWS (method);
6146 mthrows; mthrows = TREE_CHAIN (mthrows))
6147 {
6148 /* We don't verify unchecked expressions */
c877974e 6149 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
b9f7e36c
APB
6150 continue;
6151 /* Checked expression must be compatible */
6152 for (fthrows = DECL_FUNCTION_THROWS (found);
6153 fthrows; fthrows = TREE_CHAIN (fthrows))
6154 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6155 break;
6156 if (!fthrows)
6157 {
6158 parse_error_context
781b0558 6159 (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 6160 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
0a2138e2 6161 lang_printable_name (found, 0),
b9f7e36c
APB
6162 IDENTIFIER_POINTER
6163 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6164 }
6165 }
6166}
6167
e04a16fb
AG
6168/* Check abstract method of interface INTERFACE */
6169
6170static void
5e942c50
APB
6171java_check_abstract_methods (interface_decl)
6172 tree interface_decl;
e04a16fb
AG
6173{
6174 int i, n;
6175 tree method, basetype_vec, found;
5e942c50 6176 tree interface = TREE_TYPE (interface_decl);
e04a16fb
AG
6177
6178 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6179 {
b9f7e36c 6180 tree method_wfl = DECL_NAME (method);
e04a16fb
AG
6181
6182 /* 2- Check for double definition inside the defining interface */
6183 if (check_method_redefinition (interface, method))
6184 continue;
6185
6186 /* 3- Overriding is OK as far as we preserve the return type and
b9f7e36c 6187 the thrown exceptions (FIXME) */
e04a16fb
AG
6188 found = lookup_java_interface_method2 (interface, method);
6189 if (found)
6190 {
5e942c50
APB
6191 char *t;
6192 tree saved_found_wfl = DECL_NAME (found);
6193 reset_method_name (found);
c2e3db92 6194 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
e04a16fb 6195 parse_error_context
b9f7e36c 6196 (method_wfl,
5e942c50 6197 "Method `%s' was defined with return type `%s' in class `%s'",
0a2138e2 6198 lang_printable_name (found, 0), t,
b9f7e36c
APB
6199 IDENTIFIER_POINTER
6200 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6201 free (t);
5e942c50 6202 DECL_NAME (found) = saved_found_wfl;
c63b98cd 6203 continue;
e04a16fb
AG
6204 }
6205 }
6206
6207 /* 4- Inherited methods can't differ by their returned types */
6208 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6209 return;
6210 n = TREE_VEC_LENGTH (basetype_vec);
6211 for (i = 0; i < n; i++)
6212 {
6213 tree sub_interface_method, sub_interface;
6214 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6215 if (!vec_elt)
6216 continue;
6217 sub_interface = BINFO_TYPE (vec_elt);
6218 for (sub_interface_method = TYPE_METHODS (sub_interface);
6219 sub_interface_method;
6220 sub_interface_method = TREE_CHAIN (sub_interface_method))
6221 {
6222 found = lookup_java_interface_method2 (interface,
6223 sub_interface_method);
6224 if (found && (found != sub_interface_method))
5e942c50
APB
6225 {
6226 tree saved_found_wfl = DECL_NAME (found);
6227 reset_method_name (found);
6228 parse_error_context
6229 (lookup_cl (sub_interface_method),
781b0558 6230 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
5e942c50
APB
6231 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6232 lang_printable_name (found, 0),
6233 IDENTIFIER_POINTER
6234 (DECL_NAME (TYPE_NAME
6235 (DECL_CONTEXT (sub_interface_method)))),
6236 IDENTIFIER_POINTER
6237 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6238 DECL_NAME (found) = saved_found_wfl;
6239 }
e04a16fb
AG
6240 }
6241 }
6242}
6243
e04a16fb
AG
6244/* Lookup methods in interfaces using their name and partial
6245 signature. Return a matching method only if their types differ. */
6246
6247static tree
6248lookup_java_interface_method2 (class, method_decl)
6249 tree class, method_decl;
6250{
6251 int i, n;
6252 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6253
6254 if (!basetype_vec)
6255 return NULL_TREE;
6256
6257 n = TREE_VEC_LENGTH (basetype_vec);
6258 for (i = 0; i < n; i++)
6259 {
6260 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6261 if ((BINFO_TYPE (vec_elt) != object_type_node)
6262 && (to_return =
6263 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6264 return to_return;
6265 }
6266 for (i = 0; i < n; i++)
6267 {
6268 to_return = lookup_java_interface_method2
6269 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6270 if (to_return)
6271 return to_return;
6272 }
6273
6274 return NULL_TREE;
6275}
6276
6277/* Lookup method using their name and partial signature. Return a
6278 matching method only if their types differ. */
6279
6280static tree
6281lookup_java_method2 (clas, method_decl, do_interface)
6282 tree clas, method_decl;
6283 int do_interface;
6284{
5e942c50
APB
6285 tree method, method_signature, method_name, method_type, name;
6286
e04a16fb 6287 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
5e942c50
APB
6288 name = DECL_NAME (method_decl);
6289 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6290 EXPR_WFL_NODE (name) : name);
e04a16fb
AG
6291 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6292
6293 while (clas != NULL_TREE)
6294 {
6295 for (method = TYPE_METHODS (clas);
6296 method != NULL_TREE; method = TREE_CHAIN (method))
6297 {
6298 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
5e942c50
APB
6299 tree name = DECL_NAME (method);
6300 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6301 EXPR_WFL_NODE (name) : name) == method_name
e04a16fb
AG
6302 && method_sig == method_signature
6303 && TREE_TYPE (TREE_TYPE (method)) != method_type)
5e942c50 6304 return method;
e04a16fb
AG
6305 }
6306 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6307 }
6308 return NULL_TREE;
6309}
6310
f441f671
APB
6311/* Return the line that matches DECL line number, and try its best to
6312 position the column number. Used during error reports. */
e04a16fb
AG
6313
6314static tree
6315lookup_cl (decl)
6316 tree decl;
6317{
6318 static tree cl = NULL_TREE;
f441f671 6319 char *line, *found;
e04a16fb
AG
6320
6321 if (!decl)
6322 return NULL_TREE;
6323
6324 if (cl == NULL_TREE)
6325 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6326
6327 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6328 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6329
f441f671
APB
6330 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6331 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6332
6333 found = strstr ((const char *)line,
6334 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6335 if (found)
6336 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6337
e04a16fb
AG
6338 return cl;
6339}
6340
6341/* Look for a simple name in the single-type import list */
6342
6343static tree
6344find_name_in_single_imports (name)
6345 tree name;
6346{
6347 tree node;
6348
6349 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6350 if (TREE_VALUE (node) == name)
6351 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6352
6353 return NULL_TREE;
6354}
6355
6356/* Process all single-type import. */
6357
6358static int
6359process_imports ()
6360{
6361 tree import;
6362 int error_found;
6363
6364 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6365 {
6366 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6367
6368 /* Don't load twice something already defined. */
6369 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6370 continue;
6371 QUALIFIED_P (to_be_found) = 1;
6372 load_class (to_be_found, 0);
6373 error_found =
6374 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6375 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6376 {
6377 parse_error_context (TREE_PURPOSE (import),
6378 "Class or interface `%s' not found in import",
6379 IDENTIFIER_POINTER (to_be_found));
6380 return 1;
6381 }
6382 if (error_found)
6383 return 1;
6384 }
6385 return 0;
6386}
6387
6388/* Possibly find a class imported by a single-type import statement. Return
6389 1 if an error occured, 0 otherwise. */
6390
6391static int
6392find_in_imports (class_type)
6393 tree class_type;
6394{
6395 tree import;
6396
6397 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6398 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6399 {
6400 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6401 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
e04a16fb
AG
6402 }
6403 return 0;
6404}
6405
e04a16fb 6406static int
63a212ed 6407note_possible_classname (name, len)
49f48c71 6408 const char *name;
63a212ed 6409 int len;
e04a16fb 6410{
63a212ed
PB
6411 tree node;
6412 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6413 len = len - 5;
6414 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6415 len = len - 6;
e04a16fb 6416 else
63a212ed
PB
6417 return 0;
6418 node = ident_subst (name, len, "", '/', '.', "");
6419 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
fe0e4d76 6420 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
63a212ed 6421 return 1;
e04a16fb
AG
6422}
6423
6424/* Read a import directory, gathering potential match for further type
6425 references. Indifferently reads a filesystem or a ZIP archive
6426 directory. */
6427
6428static void
6429read_import_dir (wfl)
6430 tree wfl;
6431{
63a212ed 6432 tree package_id = EXPR_WFL_NODE (wfl);
49f48c71 6433 const char *package_name = IDENTIFIER_POINTER (package_id);
63a212ed 6434 int package_length = IDENTIFIER_LENGTH (package_id);
e04a16fb 6435 DIR *dirp = NULL;
d8fccff5 6436 JCF *saved_jcf = current_jcf;
e04a16fb 6437
63a212ed
PB
6438 int found = 0;
6439 int k;
6440 void *entry;
6441 struct buffer filename[1];
6442
6443
6444 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6445 return;
6446 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6447
6448 BUFFER_INIT (filename);
6449 buffer_grow (filename, package_length + 100);
6450
6451 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6452 {
49f48c71 6453 const char *entry_name = jcf_path_name (entry);
63a212ed
PB
6454 int entry_length = strlen (entry_name);
6455 if (jcf_path_is_zipfile (entry))
6456 {
6457 ZipFile *zipf;
6458 buffer_grow (filename, entry_length);
6459 memcpy (filename->data, entry_name, entry_length - 1);
6460 filename->data[entry_length-1] = '\0';
6461 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6462 if (zipf == NULL)
6463 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6464 else
6465 {
6466 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6467 BUFFER_RESET (filename);
6468 for (k = 0; k < package_length; k++)
6469 {
6470 char ch = package_name[k];
6471 *filename->ptr++ = ch == '.' ? '/' : ch;
6472 }
6473 *filename->ptr++ = '/';
6474
345137c7 6475 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
63a212ed 6476 {
49f48c71 6477 const char *current_entry = ZIPDIR_FILENAME (zipd);
63a212ed
PB
6478 int current_entry_len = zipd->filename_length;
6479
345137c7
TT
6480 if (current_entry_len >= BUFFER_LENGTH (filename)
6481 && strncmp (filename->data, current_entry,
6482 BUFFER_LENGTH (filename)) != 0)
63a212ed 6483 continue;
345137c7 6484 found |= note_possible_classname (current_entry,
63a212ed
PB
6485 current_entry_len);
6486 }
6487 }
6488 }
6489 else
6490 {
6491 BUFFER_RESET (filename);
6492 buffer_grow (filename, entry_length + package_length + 4);
6493 strcpy (filename->data, entry_name);
6494 filename->ptr = filename->data + entry_length;
6495 for (k = 0; k < package_length; k++)
6496 {
6497 char ch = package_name[k];
6498 *filename->ptr++ = ch == '.' ? '/' : ch;
6499 }
6500 *filename->ptr = '\0';
6501
6502 dirp = opendir (filename->data);
6503 if (dirp == NULL)
6504 continue;
6505 *filename->ptr++ = '/';
6506 for (;;)
6507 {
63a212ed 6508 int len;
49f48c71 6509 const char *d_name;
63a212ed
PB
6510 struct dirent *direntp = readdir (dirp);
6511 if (!direntp)
6512 break;
6513 d_name = direntp->d_name;
6514 len = strlen (direntp->d_name);
6515 buffer_grow (filename, len+1);
6516 strcpy (filename->ptr, d_name);
345137c7 6517 found |= note_possible_classname (filename->data + entry_length,
63a212ed
PB
6518 package_length+len+1);
6519 }
6520 if (dirp)
6521 closedir (dirp);
6522 }
6523 }
e04a16fb 6524
63a212ed 6525 free (filename->data);
e04a16fb 6526
63a212ed
PB
6527 /* Here we should have a unified way of retrieving an entry, to be
6528 indexed. */
6529 if (!found)
e04a16fb
AG
6530 {
6531 static int first = 1;
6532 if (first)
6533 {
781b0558 6534 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
e04a16fb
AG
6535 java_error_count++;
6536 first = 0;
6537 }
6538 else
63a212ed
PB
6539 parse_error_context (wfl, "Package `%s' not found in import",
6540 package_name);
e04a16fb
AG
6541 current_jcf = saved_jcf;
6542 return;
6543 }
e04a16fb
AG
6544 current_jcf = saved_jcf;
6545}
6546
6547/* Possibly find a type in the import on demands specified
6548 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6549 entire list, to detected potential double definitions. */
6550
6551static int
6552find_in_imports_on_demand (class_type)
6553 tree class_type;
6554{
ab3a6dd6 6555 tree node, import, node_to_use = NULL_TREE;
e04a16fb 6556 int seen_once = -1;
ab3a6dd6 6557 tree cl = NULL_TREE;
e04a16fb
AG
6558
6559 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6560 {
49f48c71 6561 const char *id_name;
e04a16fb
AG
6562 obstack_grow (&temporary_obstack,
6563 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6564 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
63a212ed 6565 obstack_1grow (&temporary_obstack, '.');
e04a16fb
AG
6566 obstack_grow0 (&temporary_obstack,
6567 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6568 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6569 id_name = obstack_finish (&temporary_obstack);
6570
6571 node = maybe_get_identifier (id_name);
6572 if (node && IS_A_CLASSFILE_NAME (node))
6573 {
6574 if (seen_once < 0)
6575 {
6576 cl = TREE_PURPOSE (import);
6577 seen_once = 1;
6578 node_to_use = node;
6579 }
6580 else
6581 {
6582 seen_once++;
6583 parse_error_context
6584 (import, "Type `%s' also potentially defined in package `%s'",
6585 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6586 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6587 }
6588 }
6589 }
6590
6591 if (seen_once == 1)
6592 {
6593 /* Setup lineno so that it refers to the line of the import (in
6594 case we parse a class file and encounter errors */
6595 tree decl;
6596 int saved_lineno = lineno;
6597 lineno = EXPR_WFL_LINENO (cl);
63a212ed 6598 TYPE_NAME (class_type) = node_to_use;
e04a16fb
AG
6599 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6600 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6601 /* If there is no DECL set for the class or if the class isn't
6602 loaded and not seen in source yet, the load */
6603 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6604 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6605 load_class (node_to_use, 0);
6606 lineno = saved_lineno;
6607 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6608 }
6609 else
6610 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6611}
6612
5e942c50
APB
6613static tree
6614resolve_package (pkg, next)
6615 tree pkg, *next;
6616{
c2952b01 6617 tree current, acc;
5e942c50 6618 tree type_name = NULL_TREE;
49f48c71 6619 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5e942c50
APB
6620
6621 /* The trick is to determine when the package name stops and were
6622 the name of something contained in the package starts. Then we
6623 return a fully qualified name of what we want to get. */
6624
6625 /* Do a quick search on well known package names */
6626 if (!strncmp (name, "java.lang.reflect", 17))
6627 {
6628 *next =
6629 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6630 type_name = lookup_package_type (name, 17);
6631 }
6632 else if (!strncmp (name, "java.lang", 9))
6633 {
6634 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6635 type_name = lookup_package_type (name, 9);
6636 }
5e942c50 6637
2c56429a
APB
6638 /* If we found something here, return */
6639 if (type_name)
6640 return type_name;
6641
6642 *next = EXPR_WFL_QUALIFICATION (pkg);
6643
6644 /* Try the current package. */
6645 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6646 IDENTIFIER_LENGTH (ctxp->package)))
6647 {
6648 type_name =
6649 lookup_package_type_and_set_next (name,
6650 IDENTIFIER_LENGTH (ctxp->package),
6651 next );
6652 if (type_name)
6653 return type_name;
6654 }
6655
6656 /* Search in imported package */
6657 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6658 {
6659 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6660 int len = IDENTIFIER_LENGTH (current_pkg_name);
6661 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6662 {
6663 tree left, dummy;
6664
6665 breakdown_qualified (&left, &dummy, current_pkg_name);
6666 len = IDENTIFIER_LENGTH (left);
6667 type_name = lookup_package_type_and_set_next (name, len, next);
6668 if (type_name)
6669 break;
6670 }
6671 }
6672
c2952b01
APB
6673 /* Try to progressively construct a type name */
6674 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6675 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6676 current; current = TREE_CHAIN (current))
6677 {
6678 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6679 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6680 {
6681 type_name = acc;
6b48deee
APB
6682 /* resolve_package should be used in a loop, hence we
6683 point at this one to naturally process the next one at
6684 the next iteration. */
6685 *next = current;
c2952b01
APB
6686 break;
6687 }
6688 }
2c56429a
APB
6689 return type_name;
6690}
6691
6692static tree
6693lookup_package_type_and_set_next (name, len, next)
49f48c71 6694 const char *name;
2c56429a
APB
6695 int len;
6696 tree *next;
6697{
49f48c71 6698 const char *ptr;
2c56429a
APB
6699 tree type_name = lookup_package_type (name, len);
6700
6701 if (!type_name)
6702 return NULL;
6703
6704 ptr = IDENTIFIER_POINTER (type_name);
6705 while (ptr && (ptr = strchr (ptr, '.')))
6706 {
6707 *next = TREE_CHAIN (*next);
6708 ptr++;
6709 }
5e942c50
APB
6710 return type_name;
6711}
6712
6713static tree
6714lookup_package_type (name, from)
49f48c71 6715 const char *name;
5e942c50
APB
6716 int from;
6717{
6718 char subname [128];
49f48c71 6719 const char *sub = &name[from+1];
5e942c50
APB
6720 while (*sub != '.' && *sub)
6721 sub++;
6722 strncpy (subname, name, sub-name);
6723 subname [sub-name] = '\0';
6724 return get_identifier (subname);
6725}
6726
e04a16fb
AG
6727/* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6728 access violations were found, 1 otherwise. */
6729
6730static int
6731check_pkg_class_access (class_name, cl)
6732 tree class_name;
6733 tree cl;
6734{
6735 tree type;
e04a16fb
AG
6736
6737 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6738 return 0;
6739
6740 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6741 return 0;
6742
6743 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6744 {
e28cd97b
APB
6745 /* Access to a private class within the same package is
6746 allowed. */
6747 tree l, r;
6748 breakdown_qualified (&l, &r, class_name);
6749 if (l == ctxp->package)
6750 return 0;
6751
e04a16fb 6752 parse_error_context
781b0558 6753 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
e04a16fb
AG
6754 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6755 IDENTIFIER_POINTER (class_name));
6756 return 1;
6757 }
6758 return 0;
6759}
6760
6761/* Local variable declaration. */
6762
6763static void
6764declare_local_variables (modifier, type, vlist)
6765 int modifier;
6766 tree type;
6767 tree vlist;
6768{
c583dd46
APB
6769 tree decl, current, saved_type;
6770 tree type_wfl = NULL_TREE;
e04a16fb 6771 int must_chain = 0;
c2952b01 6772 int final_p = 0;
e04a16fb 6773
2aa11e97 6774 /* Push a new block if statements were seen between the last time we
e04a16fb 6775 pushed a block and now. Keep a cound of block to close */
f099f336 6776 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb 6777 {
f099f336 6778 tree body = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb 6779 tree b = enter_block ();
f099f336 6780 BLOCK_EXPR_ORIGIN (b) = body;
e04a16fb
AG
6781 }
6782
6783 if (modifier)
6784 {
6785 int i;
6786 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
c877974e 6787 if (modifier == ACC_FINAL)
c2952b01 6788 final_p = 1;
c877974e
APB
6789 else
6790 {
6791 parse_error_context
6792 (ctxp->modifier_ctx [i],
6793 "Only `final' is allowed as a local variables modifier");
6794 return;
6795 }
e04a16fb
AG
6796 }
6797
c583dd46
APB
6798 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6799 hold the TYPE value if a new incomplete has to be created (as
6800 opposed to being found already existing and reused). */
6801 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6802
6803 /* If TYPE is fully resolved and we don't have a reference, make one */
1886c9d8 6804 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c583dd46
APB
6805
6806 /* Go through all the declared variables */
6807 for (current = vlist, saved_type = type; current;
6808 current = TREE_CHAIN (current), type = saved_type)
e04a16fb 6809 {
c877974e 6810 tree other, real_type;
e04a16fb
AG
6811 tree wfl = TREE_PURPOSE (current);
6812 tree name = EXPR_WFL_NODE (wfl);
6813 tree init = TREE_VALUE (current);
e04a16fb 6814
c583dd46
APB
6815 /* Process NAME, as it may specify extra dimension(s) for it */
6816 type = build_array_from_name (type, type_wfl, name, &name);
6817
6818 /* Variable redefinition check */
6819 if ((other = lookup_name_in_blocks (name)))
6820 {
6821 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6822 DECL_SOURCE_LINE (other));
6823 continue;
6824 }
6825
6826 /* Type adjustment. We may have just readjusted TYPE because
6827 the variable specified more dimensions. Make sure we have
6828 a reference if we can and don't have one already. */
1886c9d8 6829 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
c877974e
APB
6830
6831 real_type = GET_REAL_TYPE (type);
c583dd46
APB
6832 /* Never layout this decl. This will be done when its scope
6833 will be entered */
c877974e 6834 decl = build_decl (VAR_DECL, name, real_type);
c2952b01 6835 LOCAL_FINAL (decl) = final_p;
c583dd46
APB
6836 BLOCK_CHAIN_DECL (decl);
6837
d4370213
APB
6838 /* If doing xreferencing, replace the line number with the WFL
6839 compound value */
6840 if (flag_emit_xref)
6841 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6842
e04a16fb
AG
6843 /* Don't try to use an INIT statement when an error was found */
6844 if (init && java_error_count)
6845 init = NULL_TREE;
c583dd46
APB
6846
6847 /* Add the initialization function to the current function's code */
6848 if (init)
e04a16fb 6849 {
c583dd46
APB
6850 /* Name might have been readjusted */
6851 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6852 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6853 java_method_add_stmt (current_function_decl,
6854 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6855 init));
6856 }
6857
6858 /* Setup dependency the type of the decl */
6859 if (must_chain)
6860 {
6861 jdep *dep;
6862 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6863 dep = CLASSD_LAST (ctxp->classd_list);
6864 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
e04a16fb
AG
6865 }
6866 }
6867 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6868}
6869
6870/* Called during parsing. Build decls from argument list. */
6871
6872static void
6873source_start_java_method (fndecl)
6874 tree fndecl;
6875{
6876 tree tem;
6877 tree parm_decl;
6878 int i;
6879
79d13333
APB
6880 if (!fndecl)
6881 return;
6882
e04a16fb
AG
6883 current_function_decl = fndecl;
6884
6885 /* New scope for the function */
6886 enter_block ();
6887 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
de4c7b02 6888 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
e04a16fb
AG
6889 {
6890 tree type = TREE_VALUE (tem);
6891 tree name = TREE_PURPOSE (tem);
6892
23a79c61
APB
6893 /* If type is incomplete. Create an incomplete decl and ask for
6894 the decl to be patched later */
e04a16fb
AG
6895 if (INCOMPLETE_TYPE_P (type))
6896 {
6897 jdep *jdep;
c877974e
APB
6898 tree real_type = GET_REAL_TYPE (type);
6899 parm_decl = build_decl (PARM_DECL, name, real_type);
23a79c61 6900 type = obtain_incomplete_type (type);
e04a16fb
AG
6901 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6902 jdep = CLASSD_LAST (ctxp->classd_list);
6903 JDEP_MISC (jdep) = name;
6904 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6905 }
6906 else
6907 parm_decl = build_decl (PARM_DECL, name, type);
6908
c2952b01
APB
6909 /* Remember if a local variable was declared final (via its
6910 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6911 if (ARG_FINAL_P (tem))
6912 LOCAL_FINAL (parm_decl) = 1;
6913
e04a16fb
AG
6914 BLOCK_CHAIN_DECL (parm_decl);
6915 }
6916 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6917 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
6918 nreverse (tem);
6919 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
c2952b01 6920 DECL_MAX_LOCALS (current_function_decl) = i;
e04a16fb
AG
6921}
6922
22eed1e6
APB
6923/* Called during parsing. Creates an artificial method declaration. */
6924
6925static tree
6926create_artificial_method (class, flags, type, name, args)
6927 tree class;
6928 int flags;
6929 tree type, name, args;
6930{
22eed1e6
APB
6931 tree mdecl;
6932
c2952b01 6933 java_parser_context_save_global ();
22eed1e6
APB
6934 lineno = 0;
6935 mdecl = make_node (FUNCTION_TYPE);
6936 TREE_TYPE (mdecl) = type;
6937 TYPE_ARG_TYPES (mdecl) = args;
6938 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
c2952b01 6939 java_parser_context_restore_global ();
22eed1e6
APB
6940 DECL_ARTIFICIAL (mdecl) = 1;
6941 return mdecl;
6942}
6943
6944/* Starts the body if an artifical method. */
6945
6946static void
6947start_artificial_method_body (mdecl)
6948 tree mdecl;
6949{
6950 DECL_SOURCE_LINE (mdecl) = 1;
6951 DECL_SOURCE_LINE_MERGE (mdecl, 1);
6952 source_start_java_method (mdecl);
6953 enter_block ();
6954}
6955
6956static void
6957end_artificial_method_body (mdecl)
6958 tree mdecl;
6959{
6960 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
6961 exit_block ();
6962}
6963
e04a16fb
AG
6964/* Called during expansion. Push decls formerly built from argument
6965 list so they're usable during expansion. */
6966
6967static void
6968expand_start_java_method (fndecl)
6969 tree fndecl;
6970{
6971 tree tem, *ptr;
e04a16fb 6972
e04a16fb
AG
6973 current_function_decl = fndecl;
6974
c2952b01
APB
6975 if (! quiet_flag)
6976 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
e04a16fb 6977 announce_function (fndecl);
c2952b01
APB
6978 if (! quiet_flag)
6979 fprintf (stderr, "]");
6980
6981 pushlevel (1); /* Prepare for a parameter push */
e04a16fb
AG
6982 ptr = &DECL_ARGUMENTS (fndecl);
6983 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
6984 while (tem)
6985 {
6986 tree next = TREE_CHAIN (tem);
b67d701b 6987 tree type = TREE_TYPE (tem);
e438e1b7
JJ
6988 if (PROMOTE_PROTOTYPES
6989 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
b67d701b
PB
6990 && INTEGRAL_TYPE_P (type))
6991 type = integer_type_node;
b67d701b 6992 DECL_ARG_TYPE (tem) = type;
e04a16fb
AG
6993 layout_decl (tem, 0);
6994 pushdecl (tem);
e04a16fb
AG
6995 *ptr = tem;
6996 ptr = &TREE_CHAIN (tem);
6997 tem = next;
6998 }
6999 *ptr = NULL_TREE;
7000 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7001 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
e04a16fb
AG
7002}
7003
7004/* Terminate a function and expand its body. */
7005
7006static void
7007source_end_java_method ()
7008{
7009 tree fndecl = current_function_decl;
138657ec 7010 int flag_asynchronous_exceptions = asynchronous_exceptions;
e04a16fb 7011
79d13333
APB
7012 if (!fndecl)
7013 return;
7014
e04a16fb
AG
7015 java_parser_context_save_global ();
7016 lineno = ctxp->last_ccb_indent1;
7017
b67d701b
PB
7018 /* Set EH language codes */
7019 java_set_exception_lang_code ();
7020
5423609c
APB
7021 /* Turn function bodies with only a NOP expr null, so they don't get
7022 generated at all and we won't get warnings when using the -W
7023 -Wall flags. */
7024 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7025 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7026
e04a16fb
AG
7027 /* Generate function's code */
7028 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
e8fc7396
APB
7029 && ! flag_emit_class_files
7030 && ! flag_emit_xref)
e04a16fb
AG
7031 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7032
7033 /* pop out of its parameters */
7034 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7035 poplevel (1, 0, 1);
7036 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7037
7038 /* Generate rtl for function exit. */
e8fc7396 7039 if (! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
7040 {
7041 lineno = DECL_SOURCE_LINE_LAST (fndecl);
b67d701b
PB
7042 /* Emit catch-finally clauses */
7043 emit_handlers ();
e04a16fb
AG
7044 expand_function_end (input_filename, lineno, 0);
7045
138657ec
AH
7046 /* FIXME: If the current method contains any exception handlers,
7047 force asynchronous_exceptions: this is necessary because signal
7048 handlers in libjava may throw exceptions. This is far from being
7049 a perfect solution, but it's better than doing nothing at all.*/
7050 if (catch_clauses)
7051 asynchronous_exceptions = 1;
7052
e04a16fb
AG
7053 /* Run the optimizers and output assembler code for this function. */
7054 rest_of_compilation (fndecl);
7055 }
7056
7057 current_function_decl = NULL_TREE;
8226320b 7058 permanent_allocation (1);
e04a16fb 7059 java_parser_context_restore_global ();
138657ec 7060 asynchronous_exceptions = flag_asynchronous_exceptions;
e04a16fb
AG
7061}
7062
7063/* Record EXPR in the current function block. Complements compound
7064 expression second operand if necessary. */
7065
7066tree
7067java_method_add_stmt (fndecl, expr)
7068 tree fndecl, expr;
7069{
b771925e
APB
7070 if (!GET_CURRENT_BLOCK (fndecl))
7071 return NULL_TREE;
f099f336 7072 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
b67d701b 7073}
e04a16fb 7074
b67d701b
PB
7075static tree
7076add_stmt_to_block (b, type, stmt)
7077 tree b, type, stmt;
7078{
7079 tree body = BLOCK_EXPR_BODY (b), c;
7080
e04a16fb
AG
7081 if (java_error_count)
7082 return body;
b67d701b
PB
7083
7084 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
e04a16fb
AG
7085 return body;
7086
b67d701b
PB
7087 BLOCK_EXPR_BODY (b) = c;
7088 TREE_SIDE_EFFECTS (c) = 1;
7089 return c;
e04a16fb
AG
7090}
7091
7092/* Add STMT to EXISTING if possible, otherwise create a new
7093 COMPOUND_EXPR and add STMT to it. */
7094
7095static tree
7096add_stmt_to_compound (existing, type, stmt)
7097 tree existing, type, stmt;
7098{
15fdcfe9
PB
7099 if (existing)
7100 return build (COMPOUND_EXPR, type, existing, stmt);
e04a16fb 7101 else
15fdcfe9 7102 return stmt;
e04a16fb
AG
7103}
7104
7105/* Hold THIS for the scope of the current public method decl. */
7106static tree current_this;
7107
1886c9d8
APB
7108void java_layout_seen_class_methods ()
7109{
7110 tree previous_list = all_class_list;
7111 tree end = NULL_TREE;
7112 tree current;
7113
7114 while (1)
7115 {
7116 for (current = previous_list;
7117 current != end; current = TREE_CHAIN (current))
7118 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7119
7120 if (previous_list != all_class_list)
7121 {
7122 end = previous_list;
7123 previous_list = all_class_list;
7124 }
7125 else
7126 break;
7127 }
7128}
7129
e04a16fb 7130void
c2952b01 7131java_reorder_fields ()
e04a16fb 7132{
c2952b01 7133 static tree stop_reordering = NULL_TREE;
23a79c61 7134
c2952b01 7135 tree current;
5e942c50 7136 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
e04a16fb 7137 {
5e942c50 7138 current_class = TREE_TYPE (TREE_VALUE (current));
22eed1e6 7139
c2952b01
APB
7140 if (current_class == stop_reordering)
7141 break;
7142
c877974e
APB
7143 /* Reverse the fields, but leave the dummy field in front.
7144 Fields are already ordered for Object and Class */
7145 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7146 && current_class != class_type_node)
7147 {
23a79c61
APB
7148 /* If the dummy field is there, reverse the right fields and
7149 just layout the type for proper fields offset */
c877974e
APB
7150 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7151 {
7152 tree fields = TYPE_FIELDS (current_class);
7153 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7154 TYPE_SIZE (current_class) = NULL_TREE;
c877974e 7155 }
23a79c61
APB
7156 /* We don't have a dummy field, we need to layout the class,
7157 after having reversed the fields */
c877974e
APB
7158 else
7159 {
7160 TYPE_FIELDS (current_class) =
7161 nreverse (TYPE_FIELDS (current_class));
7162 TYPE_SIZE (current_class) = NULL_TREE;
c877974e
APB
7163 }
7164 }
c2952b01
APB
7165 }
7166 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7167}
7168
7169/* Layout the methods of all classes loaded in one way on an
7170 other. Check methods of source parsed classes. Then reorder the
7171 fields and layout the classes or the type of all source parsed
7172 classes */
7173
7174void
7175java_layout_classes ()
7176{
7177 tree current;
7178 int save_error_count = java_error_count;
7179
7180 /* Layout the methods of all classes seen so far */
7181 java_layout_seen_class_methods ();
7182 java_parse_abort_on_error ();
7183 all_class_list = NULL_TREE;
7184
7185 /* Then check the methods of all parsed classes */
7186 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7187 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7188 CHECK_METHODS (TREE_VALUE (current));
7189 java_parse_abort_on_error ();
7190
7191 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7192 {
7193 current_class = TREE_TYPE (TREE_VALUE (current));
7194 layout_class (current_class);
5e942c50 7195
c877974e
APB
7196 /* From now on, the class is considered completely loaded */
7197 CLASS_LOADED_P (current_class) = 1;
7198
5e942c50
APB
7199 /* Error reported by the caller */
7200 if (java_error_count)
7201 return;
e04a16fb 7202 }
23a79c61
APB
7203
7204 /* We might have reloaded classes durign the process of laying out
7205 classes for code generation. We must layout the methods of those
7206 late additions, as constructor checks might use them */
1886c9d8 7207 java_layout_seen_class_methods ();
23a79c61 7208 java_parse_abort_on_error ();
e04a16fb
AG
7209}
7210
c2952b01
APB
7211/* Expand methods in the current set of classes rememebered for
7212 generation. */
e04a16fb 7213
49f48c71 7214static void
c2952b01 7215java_complete_expand_classes ()
e04a16fb
AG
7216{
7217 tree current;
ce6e9147
APB
7218
7219 do_not_fold = flag_emit_xref;
c2952b01 7220
e04a16fb 7221 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
c2952b01
APB
7222 if (!INNER_CLASS_DECL_P (current))
7223 java_complete_expand_class (current);
7224}
e04a16fb 7225
c2952b01
APB
7226/* Expand the methods found in OUTER, starting first by OUTER's inner
7227 classes, if any. */
e04a16fb 7228
c2952b01
APB
7229static void
7230java_complete_expand_class (outer)
7231 tree outer;
7232{
7233 tree inner_list;
e04a16fb 7234
c2952b01 7235 set_nested_class_simple_name_value (outer, 1); /* Set */
cd9643f7 7236
c2952b01
APB
7237 /* We need to go after all inner classes and start expanding them,
7238 starting with most nested ones. We have to do that because nested
7239 classes might add functions to outer classes */
e04a16fb 7240
c2952b01
APB
7241 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7242 inner_list; inner_list = TREE_CHAIN (inner_list))
7243 java_complete_expand_class (TREE_PURPOSE (inner_list));
22eed1e6 7244
c2952b01
APB
7245 java_complete_expand_methods (outer);
7246 set_nested_class_simple_name_value (outer, 0); /* Reset */
7247}
7248
7249/* Expand methods registered in CLASS_DECL. The general idea is that
7250 we expand regular methods first. This allows us get an estimate on
7251 how outer context local alias fields are really used so we can add
7252 to the constructor just enough code to initialize them properly (it
7253 also lets us generate $finit$ correctly.) Then we expand the
7254 constructors and then <clinit>. */
7255
7256static void
7257java_complete_expand_methods (class_decl)
7258 tree class_decl;
7259{
7260 tree clinit, finit, decl, first_decl;
7261
7262 current_class = TREE_TYPE (class_decl);
7263
7264 /* Initialize a new constant pool */
7265 init_outgoing_cpool ();
7266
7267 /* Pre-expand <clinit> to figure whether we really need it or
7268 not. If we do need it, we pre-expand the static fields so they're
7269 ready to be used somewhere else. <clinit> will be fully expanded
7270 after we processed the constructors. */
7271 first_decl = TYPE_METHODS (current_class);
7272 clinit = maybe_generate_pre_expand_clinit (current_class);
7273
7274 /* Then generate $finit$ (if we need to) because constructor will
7275 try to use it.*/
7276 if (TYPE_FINIT_STMT_LIST (current_class))
7277 {
7278 finit = generate_finit (current_class);
7279 java_complete_expand_method (finit);
7280 }
7281
7282 /* Now do the constructors */
7283 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7284 {
7285 int no_body;
7286
7287 if (!DECL_CONSTRUCTOR_P (decl))
7288 continue;
7289
7290 no_body = !DECL_FUNCTION_BODY (decl);
7291 /* Don't generate debug info on line zero when expanding a
7292 generated constructor. */
7293 if (no_body)
7294 restore_line_number_status (1);
7295
7296 java_complete_expand_method (decl);
7297
7298 if (no_body)
7299 restore_line_number_status (0);
7300 }
7301
7302 /* First, do the ordinary methods. */
7303 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7304 {
7145d9fe
TT
7305 /* Skip abstract or native methods -- but do handle native
7306 methods when generating JNI stubs. */
7307 if (METHOD_ABSTRACT (decl)
7308 || (! flag_jni && METHOD_NATIVE (decl))
b7805411 7309 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
c2952b01 7310 continue;
7145d9fe
TT
7311
7312 if (METHOD_NATIVE (decl))
7313 {
7314 tree body = build_jni_stub (decl);
7315 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7316 }
7317
c2952b01
APB
7318 java_complete_expand_method (decl);
7319 }
7320
7321 /* If there is indeed a <clinit>, fully expand it now */
7322 if (clinit)
7323 {
7324 /* Prevent the use of `this' inside <clinit> */
7325 ctxp->explicit_constructor_p = 1;
7326 java_complete_expand_method (clinit);
7327 ctxp->explicit_constructor_p = 0;
e04a16fb 7328 }
c2952b01 7329
165f37bc
APB
7330 /* We might have generated a class$ that we now want to expand */
7331 if (TYPE_DOT_CLASS (current_class))
7332 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7333
c2952b01
APB
7334 /* Now verify constructor circularity (stop after the first one we
7335 prove wrong.) */
7336 if (!CLASS_INTERFACE (class_decl))
7337 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7338 if (DECL_CONSTRUCTOR_P (decl)
7339 && verify_constructor_circularity (decl, decl))
7340 break;
7341
7342 /* Save the constant pool. We'll need to restore it later. */
7343 TYPE_CPOOL (current_class) = outgoing_cpool;
e04a16fb
AG
7344}
7345
b9f7e36c
APB
7346/* Hold a list of catch clauses list. The first element of this list is
7347 the list of the catch clauses of the currently analysed try block. */
7348static tree currently_caught_type_list;
7349
c2952b01
APB
7350/* Attempt to create <clinit>. Pre-expand static fields so they can be
7351 safely used in some other methods/constructors. */
e920ebc9 7352
c2952b01
APB
7353static tree
7354maybe_generate_pre_expand_clinit (class_type)
7355 tree class_type;
e920ebc9 7356{
c2952b01
APB
7357 tree current, mdecl;
7358
7359 if (!TYPE_CLINIT_STMT_LIST (class_type))
7360 return NULL_TREE;
e920ebc9 7361
c2952b01
APB
7362 /* Go through all static fields and pre expand them */
7363 for (current = TYPE_FIELDS (class_type); current;
7364 current = TREE_CHAIN (current))
7365 if (FIELD_STATIC (current))
7366 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7367
7368 /* Then build the <clinit> method */
7369 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7370 clinit_identifier_node, end_params_node);
7371 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7372 mdecl, NULL_TREE);
7373 start_artificial_method_body (mdecl);
7374
7375 /* We process the list of assignment we produced as the result of
7376 the declaration of initialized static field and add them as
7377 statement to the <clinit> method. */
7378 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7379 current = TREE_CHAIN (current))
e920ebc9 7380 {
c2952b01
APB
7381 /* We build the assignment expression that will initialize the
7382 field to its value. There are strict rules on static
7383 initializers (8.5). FIXME */
7384 tree stmt = build_debugable_stmt (EXPR_WFL_LINECOL (current), current);
7385 java_method_add_stmt (mdecl, stmt);
7386 }
e920ebc9 7387
c2952b01
APB
7388 end_artificial_method_body (mdecl);
7389
7390 /* Now we want to place <clinit> as the last method for interface so
7391 that it doesn't interfere with the dispatch table based
7392 lookup. */
7393 if (CLASS_INTERFACE (TYPE_NAME (class_type))
7394 && TREE_CHAIN (TYPE_METHODS (class_type)))
7395 {
7396 tree current =
7397 TYPE_METHODS (class_type) = TREE_CHAIN (TYPE_METHODS (class_type));
7398
7399 while (TREE_CHAIN (current))
7400 current = TREE_CHAIN (current);
7401 TREE_CHAIN (current) = mdecl;
7402 TREE_CHAIN (mdecl) = NULL_TREE;
e920ebc9 7403 }
c2952b01
APB
7404
7405 return mdecl;
e920ebc9
APB
7406}
7407
e04a16fb
AG
7408/* Complete and expand a method. */
7409
7410static void
7411java_complete_expand_method (mdecl)
7412 tree mdecl;
7413{
c2952b01 7414 current_function_decl = mdecl;
22eed1e6
APB
7415 /* Fix constructors before expanding them */
7416 if (DECL_CONSTRUCTOR_P (mdecl))
7417 fix_constructors (mdecl);
e04a16fb 7418
22eed1e6 7419 /* Expand functions that have a body */
e04a16fb
AG
7420 if (DECL_FUNCTION_BODY (mdecl))
7421 {
9bbc7d9f
PB
7422 tree fbody = DECL_FUNCTION_BODY (mdecl);
7423 tree block_body = BLOCK_EXPR_BODY (fbody);
cd531a2e 7424 tree exception_copy = NULL_TREE;
e04a16fb 7425 expand_start_java_method (mdecl);
939d7216 7426 build_result_decl (mdecl);
e04a16fb
AG
7427
7428 current_this
7429 = (!METHOD_STATIC (mdecl) ?
7430 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7431
ce6e9147
APB
7432 /* Purge the `throws' list of unchecked exceptions. If we're
7433 doing xref, save a copy of the list and re-install it
7434 later. */
7435 if (flag_emit_xref)
7436 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7437
b9f7e36c
APB
7438 purge_unchecked_exceptions (mdecl);
7439
7440 /* Install exceptions thrown with `throws' */
7441 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7442
9bbc7d9f 7443 if (block_body != NULL_TREE)
bc3ca41b
PB
7444 {
7445 block_body = java_complete_tree (block_body);
c2952b01 7446
7145d9fe 7447 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
ce6e9147 7448 check_for_initialization (block_body);
f099f336 7449 ctxp->explicit_constructor_p = 0;
bc3ca41b 7450 }
e803d3b2 7451
9bbc7d9f 7452 BLOCK_EXPR_BODY (fbody) = block_body;
5e942c50 7453
c2952b01
APB
7454 /* If we saw a return but couldn't evaluate it properly, we'll
7455 have an error_mark_node here. */
7456 if (block_body != error_mark_node
7457 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
ce6e9147
APB
7458 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7459 && !flag_emit_xref)
82371d41 7460 missing_return_error (current_function_decl);
7525cc04 7461
939d7216
PB
7462 complete_start_java_method (mdecl);
7463
e04a16fb
AG
7464 /* Don't go any further if we've found error(s) during the
7465 expansion */
7466 if (!java_error_count)
7467 source_end_java_method ();
22eed1e6
APB
7468 else
7469 {
7470 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7471 poplevel (1, 0, 1);
7472 }
b9f7e36c
APB
7473
7474 /* Pop the exceptions and sanity check */
7475 POP_EXCEPTIONS();
7476 if (currently_caught_type_list)
7477 fatal ("Exception list non empty - java_complete_expand_method");
ce6e9147
APB
7478
7479 if (flag_emit_xref)
7480 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
e04a16fb
AG
7481 }
7482}
7483
c2952b01
APB
7484\f
7485
7486/* This section of the code deals with accessing enclosing context
7487 fields either directly by using the relevant access to this$<n> or
7488 by invoking an access method crafted for that purpose. */
7489
7490/* Build the necessary access from an inner class to an outer
7491 class. This routine could be optimized to cache previous result
7492 (decl, current_class and returned access). When an access method
7493 needs to be generated, it always takes the form of a read. It might
7494 be later turned into a write by calling outer_field_access_fix. */
7495
7496static tree
7497build_outer_field_access (id, decl)
7498 tree id, decl;
7499{
7500 tree access = NULL_TREE;
7501 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7502
7503 /* If decl's class is the direct outer class of the current_class,
7504 build the access as `this$<n>.<field>'. Not that we will break
7505 the `private' barrier if we're not emitting bytecodes. */
7506 if (ctx == DECL_CONTEXT (decl)
7507 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7508 {
7509 tree thisn = build_current_thisn (current_class);
7510 access = make_qualified_primary (build_wfl_node (thisn),
7511 id, EXPR_WFL_LINECOL (id));
7512 }
7513 /* Otherwise, generate access methods to outer this and access the
7514 field (either using an access method or by direct access.) */
7515 else
7516 {
7517 int lc = EXPR_WFL_LINECOL (id);
7518
7519 /* Now we chain the required number of calls to the access$0 to
7520 get a hold to the enclosing instance we need, and the we
7521 build the field access. */
7522 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7523
7524 /* If the field is private and we're generating bytecode, then
7525 we generate an access method */
7526 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7527 {
7528 tree name = build_outer_field_access_methods (decl);
7529 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7530 name, access, NULL_TREE);
7531 }
7532 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7533 Once again we break the `private' access rule from a foreign
7534 class. */
7535 else
7536 access = make_qualified_primary (access, id, lc);
7537 }
7538 return resolve_expression_name (access, NULL);
7539}
7540
7541/* Return a non zero value if NODE describes an outer field inner
7542 access. */
7543
7544static int
7545outer_field_access_p (type, decl)
7546 tree type, decl;
7547{
7548 if (!INNER_CLASS_TYPE_P (type)
7549 || TREE_CODE (decl) != FIELD_DECL
7550 || DECL_CONTEXT (decl) == type)
7551 return 0;
7552
7553 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7554 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7555 {
7556 if (type == DECL_CONTEXT (decl))
7557 return 1;
7558 if (!DECL_CONTEXT (TYPE_NAME (type)))
7559 break;
7560 }
7561
7562 return 0;
7563}
7564
7565/* Return a non zero value if NODE represents an outer field inner
7566 access that was been already expanded. As a side effect, it returns
7567 the name of the field being accessed and the argument passed to the
7568 access function, suitable for a regeneration of the access method
7569 call if necessary. */
7570
7571static int
7572outer_field_expanded_access_p (node, name, arg_type, arg)
7573 tree node, *name, *arg_type, *arg;
7574{
7575 int identified = 0;
7576
7577 if (TREE_CODE (node) != CALL_EXPR)
7578 return 0;
7579
7580 /* Well, gcj generates slightly different tree nodes when compiling
7581 to native or bytecodes. It's the case for function calls. */
7582
7583 if (flag_emit_class_files
7584 && TREE_CODE (node) == CALL_EXPR
7585 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7586 identified = 1;
7587 else if (!flag_emit_class_files)
7588 {
7589 node = TREE_OPERAND (node, 0);
7590
7591 if (node && TREE_OPERAND (node, 0)
7592 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7593 {
7594 node = TREE_OPERAND (node, 0);
7595 if (TREE_OPERAND (node, 0)
7596 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7597 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7598 (DECL_NAME (TREE_OPERAND (node, 0)))))
7599 identified = 1;
7600 }
7601 }
7602
7603 if (identified && name && arg_type && arg)
7604 {
7605 tree argument = TREE_OPERAND (node, 1);
7606 *name = DECL_NAME (TREE_OPERAND (node, 0));
7607 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7608 *arg = TREE_VALUE (argument);
7609 }
7610 return identified;
7611}
7612
7613/* Detect in NODE an outer field read access from an inner class and
7614 transform it into a write with RHS as an argument. This function is
7615 called from the java_complete_lhs when an assignment to a LHS can
7616 be identified. */
7617
7618static tree
7619outer_field_access_fix (wfl, node, rhs)
7620 tree wfl, node, rhs;
7621{
7622 tree name, arg_type, arg;
7623
7624 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7625 {
7626 /* At any rate, check whether we're trying to assign a value to
7627 a final. */
7628 tree accessed = (JDECL_P (node) ? node :
7629 (TREE_CODE (node) == COMPONENT_REF ?
7630 TREE_OPERAND (node, 1) : node));
7631 if (check_final_assignment (accessed, wfl))
7632 return error_mark_node;
7633
7634 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7635 arg_type, name, arg, rhs);
7636 return java_complete_tree (node);
7637 }
7638 return NULL_TREE;
7639}
7640
7641/* Construct the expression that calls an access method:
7642 <type>.access$<n>(<arg1> [, <arg2>]);
7643
7644 ARG2 can be NULL and will be omitted in that case. It will denote a
7645 read access. */
7646
7647static tree
7648build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7649 int lc;
7650 tree type, access_method_name, arg1, arg2;
7651{
7652 tree args, cn, access;
7653
7654 args = arg1 ? arg1 :
7655 build_wfl_node (build_current_thisn (current_class));
7656 args = build_tree_list (NULL_TREE, args);
7657
7658 if (arg2)
7659 args = tree_cons (NULL_TREE, arg2, args);
7660
7661 access = build_method_invocation (build_wfl_node (access_method_name), args);
7662 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7663 return make_qualified_primary (cn, access, lc);
7664}
7665
7666static tree
7667build_new_access_id ()
7668{
7669 static int access_n_counter = 1;
7670 char buffer [128];
7671
7672 sprintf (buffer, "access$%d", access_n_counter++);
7673 return get_identifier (buffer);
7674}
7675
7676/* Create the static access functions for the outer field DECL. We define a
7677 read:
7678 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7679 return inst$.field;
7680 }
7681 and a write access:
7682 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7683 TREE_TYPE (<field>) value$) {
7684 return inst$.field = value$;
7685 }
7686 We should have a usage flags on the DECL so we can lazily turn the ones
7687 we're using for code generation. FIXME.
7688*/
7689
7690static tree
7691build_outer_field_access_methods (decl)
7692 tree decl;
7693{
7694 tree id, args, stmt, mdecl;
7695
7696 /* Check point, to be removed. FIXME */
7697 if (FIELD_INNER_ACCESS (decl)
7698 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7699 abort ();
7700
7701 if (FIELD_INNER_ACCESS (decl))
7702 return FIELD_INNER_ACCESS (decl);
7703
7704 push_obstacks (&permanent_obstack, &permanent_obstack);
7705
7706 /* Create the identifier and a function named after it. */
7707 id = build_new_access_id ();
7708
7709 /* The identifier is marked as bearing the name of a generated write
7710 access function for outer field accessed from inner classes. */
7711 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7712
7713 /* Create the read access */
7714 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7715 TREE_CHAIN (args) = end_params_node;
7716 stmt = make_qualified_primary (build_wfl_node (inst_id),
7717 build_wfl_node (DECL_NAME (decl)), 0);
7718 stmt = build_return (0, stmt);
7719 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7720 TREE_TYPE (decl), id, args, stmt);
7721 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7722
7723 /* Create the write access method */
7724 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7725 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7726 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7727 stmt = make_qualified_primary (build_wfl_node (inst_id),
7728 build_wfl_node (DECL_NAME (decl)), 0);
7729 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7730 build_wfl_node (wpv_id)));
7731
7732 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7733 TREE_TYPE (decl), id, args, stmt);
7734 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7735 pop_obstacks ();
7736
7737 /* Return the access name */
7738 return FIELD_INNER_ACCESS (decl) = id;
7739}
7740
7741/* Build an field access method NAME. */
7742
7743static tree
7744build_outer_field_access_method (class, type, name, args, body)
7745 tree class, type, name, args, body;
7746{
7747 tree saved_current_function_decl, mdecl;
7748
7749 /* Create the method */
7750 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7751 fix_method_argument_names (args, mdecl);
7752 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7753
7754 /* Attach the method body. */
7755 saved_current_function_decl = current_function_decl;
7756 start_artificial_method_body (mdecl);
7757 java_method_add_stmt (mdecl, body);
7758 end_artificial_method_body (mdecl);
7759 current_function_decl = saved_current_function_decl;
7760
7761 return mdecl;
7762}
7763
7764\f
7765/* This section deals with building access function necessary for
7766 certain kinds of method invocation from inner classes. */
7767
7768static tree
7769build_outer_method_access_method (decl)
7770 tree decl;
7771{
7772 tree saved_current_function_decl, mdecl;
7773 tree args = NULL_TREE, call_args = NULL_TREE;
7774 tree carg, id, body, class;
7775 char buffer [80];
7776 int parm_id_count = 0;
7777
7778 /* Test this abort with an access to a private field */
7779 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7780 abort ();
7781
7782 /* Check the cache first */
7783 if (DECL_FUNCTION_INNER_ACCESS (decl))
7784 return DECL_FUNCTION_INNER_ACCESS (decl);
7785
7786 class = DECL_CONTEXT (decl);
7787
7788 /* Obtain an access identifier and mark it */
7789 id = build_new_access_id ();
7790 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7791
7792 push_obstacks (&permanent_obstack, &permanent_obstack);
7793
7794 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7795 /* Create the arguments, as much as the original */
7796 for (; carg && carg != end_params_node;
7797 carg = TREE_CHAIN (carg))
7798 {
7799 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7800 args = chainon (args, build_tree_list (get_identifier (buffer),
7801 TREE_VALUE (carg)));
7802 }
7803 args = chainon (args, end_params_node);
7804
7805 /* Create the method */
7806 mdecl = create_artificial_method (class, ACC_STATIC,
7807 TREE_TYPE (TREE_TYPE (decl)), id, args);
7808 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7809 /* There is a potential bug here. We should be able to use
7810 fix_method_argument_names, but then arg names get mixed up and
7811 eventually a constructor will have its this$0 altered and the
7812 outer context won't be assignment properly. The test case is
7813 stub.java FIXME */
7814 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7815
7816 /* Attach the method body. */
7817 saved_current_function_decl = current_function_decl;
7818 start_artificial_method_body (mdecl);
7819
7820 /* The actual method invocation uses the same args. When invoking a
7821 static methods that way, we don't want to skip the first
7822 argument. */
7823 carg = args;
7824 if (!METHOD_STATIC (decl))
7825 carg = TREE_CHAIN (carg);
7826 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7827 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7828 call_args);
7829
7830 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7831 call_args);
7832 if (!METHOD_STATIC (decl))
7833 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
7834 body, 0);
7835 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
7836 body = build_return (0, body);
7837 java_method_add_stmt (mdecl,body);
7838 end_artificial_method_body (mdecl);
7839 current_function_decl = saved_current_function_decl;
7840 pop_obstacks ();
7841
7842 /* Back tag the access function so it know what it accesses */
7843 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
7844
7845 /* Tag the current method so it knows it has an access generated */
7846 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
7847}
7848
7849\f
7850/* This section of the code deals with building expressions to access
7851 the enclosing instance of an inner class. The enclosing instance is
7852 kept in a generated field called this$<n>, with <n> being the
7853 inner class nesting level (starting from 0.) */
7854
7855/* Build an access to a given this$<n>, possibly by chaining access
7856 call to others. Access methods to this$<n> are build on the fly if
7857 necessary */
7858
7859static tree
7860build_access_to_thisn (from, to, lc)
7861 tree from, to;
7862 int lc;
7863{
7864 tree access = NULL_TREE;
7865
7866 while (from != to)
7867 {
7868 tree access0_wfl, cn;
7869
7870 maybe_build_thisn_access_method (from);
7871 access0_wfl = build_wfl_node (access0_identifier_node);
7872 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
7873 EXPR_WFL_LINECOL (access0_wfl) = lc;
7874
7875 if (!access)
7876 {
7877 access = build_current_thisn (current_class);
7878 access = build_wfl_node (access);
7879 }
7880 access = build_tree_list (NULL_TREE, access);
7881 access = build_method_invocation (access0_wfl, access);
7882 access = make_qualified_primary (cn, access, lc);
7883
7884 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
7885 }
7886 return access;
7887}
7888
7889/* Build an access function to the this$<n> local to TYPE. NULL_TREE
7890 is returned if nothing needs to be generated. Otherwise, the method
7891 generated, fully walked and a method decl is returned.
7892
7893 NOTE: These generated methods should be declared in a class file
7894 attribute so that they can't be referred to directly. */
7895
7896static tree
7897maybe_build_thisn_access_method (type)
7898 tree type;
7899{
7900 tree mdecl, args, stmt, rtype;
7901 tree saved_current_function_decl;
7902
7903 /* If TYPE is a top-level class, no access method is required.
7904 If there already is such an access method, bail out. */
7905 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
7906 return NULL_TREE;
7907
7908 /* We generate the method. The method looks like:
7909 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
7910 */
7911 push_obstacks (&permanent_obstack, &permanent_obstack);
7912 args = build_tree_list (inst_id, build_pointer_type (type));
7913 TREE_CHAIN (args) = end_params_node;
7914 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
7915 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
7916 access0_identifier_node, args);
7917 fix_method_argument_names (args, mdecl);
7918 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
7919 stmt = build_current_thisn (type);
7920 stmt = make_qualified_primary (build_wfl_node (inst_id),
7921 build_wfl_node (stmt), 0);
7922 stmt = build_return (0, stmt);
7923
7924 saved_current_function_decl = current_function_decl;
7925 start_artificial_method_body (mdecl);
7926 java_method_add_stmt (mdecl, stmt);
7927 end_artificial_method_body (mdecl);
7928 current_function_decl = saved_current_function_decl;
7929 pop_obstacks ();
7930
7931 CLASS_ACCESS0_GENERATED_P (type) = 1;
7932
7933 return mdecl;
7934}
7935
7936/* Craft an correctly numbered `this$<n>'string. this$0 is used for
7937 the first level of innerclassing. this$1 for the next one, etc...
7938 This function can be invoked with TYPE to NULL, available and then
7939 has to count the parser context. */
7940
7941static tree
7942build_current_thisn (type)
7943 tree type;
7944{
7945 static int saved_i = -1;
7946 static tree saved_thisn = NULL_TREE;
7947
7948 tree decl;
7949 char buffer [80];
7950 int i = 0;
7951
7952 if (type)
7953 {
7954 static tree saved_type = NULL_TREE;
7955 static int saved_type_i = 0;
7956
7957 if (type == saved_type)
7958 i = saved_type_i;
7959 else
7960 {
7961 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
7962 decl; decl = DECL_CONTEXT (decl), i++)
7963 ;
7964
7965 saved_type = type;
7966 saved_type_i = i;
7967 }
7968 }
7969 else
7970 i = list_length (GET_CPC_LIST ())-2;
7971
7972 if (i == saved_i)
7973 return saved_thisn;
7974
7975 sprintf (buffer, "this$%d", i);
7976 saved_i = i;
7977 saved_thisn = get_identifier (buffer);
7978 return saved_thisn;
7979}
7980
7981/* Return the assignement to the hidden enclosing context `this$<n>'
7982 by the second incoming parameter to the innerclass constructor. The
7983 form used is `this.this$<n> = this$<n>;'. */
7984
7985static tree
7986build_thisn_assign ()
7987{
7988 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
7989 {
7990 tree thisn = build_current_thisn (current_class);
7991 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
7992 build_wfl_node (thisn), 0);
7993 tree rhs = build_wfl_node (thisn);
7994 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
7995 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
7996 }
7997 return NULL_TREE;
7998}
7999
8000\f
165f37bc
APB
8001/* Building the synthetic `class$' used to implement the `.class' 1.1
8002 extension for non primitive types. This method looks like:
8003
8004 static Class class$(String type) throws NoClassDefFoundError
8005 {
8006 try {return (java.lang.Class.forName (String));}
8007 catch (ClassNotFoundException e) {
8008 throw new NoClassDefFoundError(e.getMessage());}
8009 } */
8010
8011static tree
8012build_dot_class_method (class)
8013 tree class;
8014{
8015#define BWF(S) build_wfl_node (get_identifier ((S)))
8016#define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8017 tree args, tmp, saved_current_function_decl, mdecl;
8018 tree stmt, throw_stmt, catch, catch_block, try_block;
8019 tree catch_clause_param;
8020 tree class_not_found_exception, no_class_def_found_error;
8021
8022 static tree get_message_wfl, type_parm_wfl;
8023
8024 if (!get_message_wfl)
8025 {
8026 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8027 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8028 }
8029
8030 /* Build the arguments */
8031 args = build_tree_list (get_identifier ("type$"),
8032 build_pointer_type (string_type_node));
8033 TREE_CHAIN (args) = end_params_node;
8034
8035 /* Build the qualified name java.lang.Class.forName */
8036 tmp = MQN (MQN (MQN (BWF ("java"),
8037 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8038
8039 /* For things we have to catch and throw */
8040 class_not_found_exception =
8041 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8042 no_class_def_found_error =
8043 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8044 load_class (class_not_found_exception, 1);
8045 load_class (no_class_def_found_error, 1);
8046
8047 /* Create the "class$" function */
8048 mdecl = create_artificial_method (class, ACC_STATIC,
8049 build_pointer_type (class_type_node),
8050 get_identifier ("class$"), args);
8051 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8052 no_class_def_found_error);
8053
8054 /* We start by building the try block. We need to build:
8055 return (java.lang.Class.forName (type)); */
8056 stmt = build_method_invocation (tmp,
8057 build_tree_list (NULL_TREE, type_parm_wfl));
8058 stmt = build_return (0, stmt);
8059 /* Put it in a block. That's the try block */
8060 try_block = build_expr_block (stmt, NULL_TREE);
8061
8062 /* Now onto the catch block. We start by building the expression
8063 throwing a new exception:
8064 throw new NoClassDefFoundError (_.getMessage); */
8065 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8066 get_message_wfl, 0);
8067 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8068
8069 /* Build new NoClassDefFoundError (_.getMessage) */
8070 throw_stmt = build_new_invocation
8071 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8072 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8073
8074 /* Build the throw, (it's too early to use BUILD_THROW) */
8075 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8076
8077 /* Build the catch block to encapsulate all this. We begin by
8078 building an decl for the catch clause parameter and link it to
8079 newly created block, the catch block. */
8080 catch_clause_param =
8081 build_decl (VAR_DECL, wpv_id,
8082 build_pointer_type (class_not_found_exception));
8083 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8084
8085 /* We initialize the variable with the exception handler. */
8086 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8087 soft_exceptioninfo_call_node);
8088 add_stmt_to_block (catch_block, NULL_TREE, catch);
8089
8090 /* We add the statement throwing the new exception */
8091 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8092
8093 /* Build a catch expression for all this */
8094 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8095
8096 /* Build the try/catch sequence */
8097 stmt = build_try_statement (0, try_block, catch_block);
8098
8099 fix_method_argument_names (args, mdecl);
8100 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8101 saved_current_function_decl = current_function_decl;
8102 start_artificial_method_body (mdecl);
8103 java_method_add_stmt (mdecl, stmt);
8104 end_artificial_method_body (mdecl);
8105 current_function_decl = saved_current_function_decl;
8106 TYPE_DOT_CLASS (class) = mdecl;
8107
8108 return mdecl;
8109}
8110
8111static tree
8112build_dot_class_method_invocation (name)
8113 tree name;
8114{
8115 tree s = make_node (STRING_CST);
8116 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8117 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8118 TREE_STRING_LENGTH (s)+1);
8119 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8120 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8121 build_tree_list (NULL_TREE, s));
8122}
8123
c2952b01
APB
8124/* This section of the code deals with constructor. */
8125
22eed1e6
APB
8126/* Craft a body for default constructor. Patch existing constructor
8127 bodies with call to super() and field initialization statements if
8128 necessary. */
8129
8130static void
8131fix_constructors (mdecl)
8132 tree mdecl;
8133{
8134 tree body = DECL_FUNCTION_BODY (mdecl);
c2952b01
APB
8135 tree thisn_assign, compound = NULL_TREE;
8136 tree class_type = DECL_CONTEXT (mdecl);
22eed1e6 8137
22eed1e6
APB
8138 if (!body)
8139 {
22eed1e6
APB
8140 /* It is an error for the compiler to generate a default
8141 constructor if the superclass doesn't have a constructor that
c2952b01
APB
8142 takes no argument, or the same args for an anonymous class */
8143 if (verify_constructor_super (mdecl))
22eed1e6 8144 {
c2952b01
APB
8145 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8146 tree save = DECL_NAME (mdecl);
49f48c71 8147 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
c2952b01 8148 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
781b0558 8149 parse_error_context
c2952b01
APB
8150 (lookup_cl (TYPE_NAME (class_type)),
8151 "No constructor matching `%s' found in class `%s'",
8152 lang_printable_name (mdecl, 0), n);
8153 DECL_NAME (mdecl) = save;
22eed1e6
APB
8154 }
8155
c2952b01
APB
8156 /* The constructor body must be crafted by hand. It's the
8157 constructor we defined when we realize we didn't have the
8158 CLASSNAME() constructor */
22eed1e6
APB
8159 start_artificial_method_body (mdecl);
8160
8161 /* We don't generate a super constructor invocation if we're
8162 compiling java.lang.Object. build_super_invocation takes care
8163 of that. */
e920ebc9 8164 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
22eed1e6 8165
c2952b01
APB
8166 /* Insert the instance initializer block right here, after the
8167 super invocation. */
8168 add_instance_initializer (mdecl);
8169
8170 /* Insert an assignment to the this$<n> hidden field, if
8171 necessary */
8172 if ((thisn_assign = build_thisn_assign ()))
8173 java_method_add_stmt (mdecl, thisn_assign);
8174
22eed1e6
APB
8175 end_artificial_method_body (mdecl);
8176 }
8177 /* Search for an explicit constructor invocation */
8178 else
8179 {
8180 int found = 0;
8181 tree main_block = BLOCK_EXPR_BODY (body);
22eed1e6
APB
8182
8183 while (body)
8184 switch (TREE_CODE (body))
8185 {
8186 case CALL_EXPR:
8187 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8188 body = NULL_TREE;
8189 break;
8190 case COMPOUND_EXPR:
8191 case EXPR_WITH_FILE_LOCATION:
8192 body = TREE_OPERAND (body, 0);
8193 break;
8194 case BLOCK:
8195 body = BLOCK_EXPR_BODY (body);
8196 break;
8197 default:
8198 found = 0;
8199 body = NULL_TREE;
8200 }
8201 /* The constructor is missing an invocation of super() */
8202 if (!found)
8203 compound = add_stmt_to_compound (compound, NULL_TREE,
c2952b01 8204 build_super_invocation (mdecl));
22eed1e6 8205
c2952b01
APB
8206 /* Insert the instance initializer block right here, after the
8207 super invocation. */
8208 add_instance_initializer (mdecl);
8209
8210 /* Generate the assignment to this$<n>, if necessary */
8211 if ((thisn_assign = build_thisn_assign ()))
8212 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8213
22eed1e6
APB
8214 /* Fix the constructor main block if we're adding extra stmts */
8215 if (compound)
8216 {
8217 compound = add_stmt_to_compound (compound, NULL_TREE,
8218 BLOCK_EXPR_BODY (main_block));
8219 BLOCK_EXPR_BODY (main_block) = compound;
8220 }
8221 }
8222}
8223
8224/* Browse constructors in the super class, searching for a constructor
8225 that doesn't take any argument. Return 0 if one is found, 1
c2952b01
APB
8226 otherwise. If the current class is an anonymous inner class, look
8227 for something that has the same signature. */
22eed1e6
APB
8228
8229static int
c2952b01
APB
8230verify_constructor_super (mdecl)
8231 tree mdecl;
22eed1e6
APB
8232{
8233 tree class = CLASSTYPE_SUPER (current_class);
c2952b01
APB
8234 tree sdecl;
8235
22eed1e6
APB
8236 if (!class)
8237 return 0;
8238
c2952b01 8239 if (ANONYMOUS_CLASS_P (current_class))
22eed1e6 8240 {
c2952b01
APB
8241 tree mdecl_arg_type;
8242 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8243 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8244 if (DECL_CONSTRUCTOR_P (sdecl))
8245 {
8246 tree arg_type;
8247 for (arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8248 arg_type != end_params_node &&
8249 mdecl_arg_type != end_params_node;
8250 arg_type = TREE_CHAIN (arg_type),
8251 mdecl_arg_type = TREE_CHAIN (mdecl_arg_type))
8252 if (TREE_VALUE (arg_type) != TREE_VALUE (mdecl_arg_type))
8253 break;
8254
8255 if (arg_type == end_params_node &&
8256 mdecl_arg_type == end_params_node)
8257 return 0;
8258 }
8259 }
8260 else
8261 {
8262 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
22eed1e6 8263 {
c2952b01
APB
8264 if (DECL_CONSTRUCTOR_P (sdecl)
8265 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)))
d77613be 8266 == end_params_node)
22eed1e6
APB
8267 return 0;
8268 }
8269 }
8270 return 1;
8271}
8272
22eed1e6 8273/* Generate code for all context remembered for code generation. */
b351b287
APB
8274
8275void
8276java_expand_classes ()
8277{
5423609c 8278 int save_error_count = 0;
c2952b01
APB
8279 static struct parser_ctxt *saved_ctxp = NULL;
8280
23a79c61
APB
8281 java_parse_abort_on_error ();
8282 if (!(ctxp = ctxp_for_generation))
5e942c50
APB
8283 return;
8284 java_layout_classes ();
8285 java_parse_abort_on_error ();
8286
48a840d9
APB
8287 /* The list of packages declaration seen so far needs to be
8288 reversed, so that package declared in a file being compiled gets
8289 priority over packages declared as a side effect of parsing other
8290 files.*/
8291 package_list = nreverse (package_list);
8292
c2952b01 8293 saved_ctxp = ctxp_for_generation;
b351b287
APB
8294 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8295 {
8296 ctxp = ctxp_for_generation;
8297 lang_init_source (2); /* Error msgs have method prototypes */
c2952b01 8298 java_complete_expand_classes (); /* Complete and expand classes */
b351b287
APB
8299 java_parse_abort_on_error ();
8300 }
c2952b01
APB
8301
8302 /* Find anonymous classes and expand their constructor, now they
8303 have been fixed. */
8304 for (ctxp_for_generation = saved_ctxp;
8305 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8306 {
8307 tree current;
8308 ctxp = ctxp_for_generation;
8309 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8310 {
8311 current_class = TREE_TYPE (current);
8312 if (ANONYMOUS_CLASS_P (current_class))
8313 {
8314 tree d;
8315 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8316 {
8317 if (DECL_CONSTRUCTOR_P (d))
8318 {
8319 restore_line_number_status (1);
8320 reset_method_name (d);
8321 java_complete_expand_method (d);
8322 restore_line_number_status (0);
8323 break; /* We now there are no other ones */
8324 }
8325 }
8326 }
8327 }
8328 }
8329
8330 /* If we've found error at that stage, don't try to generate
8331 anything, unless we're emitting xrefs or checking the syntax only
8332 (but not using -fsyntax-only for the purpose of generating
8333 bytecode. */
8334 if (java_error_count && !flag_emit_xref
8335 && (!flag_syntax_only && !flag_emit_class_files))
8336 return;
8337
8338 /* Now things are stable, go for generation of the class data. */
8339 for (ctxp_for_generation = saved_ctxp;
8340 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8341 {
8342 tree current;
8343 ctxp = ctxp_for_generation;
8344 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8345 {
8346 current_class = TREE_TYPE (current);
8347 outgoing_cpool = TYPE_CPOOL (current_class);
8348 if (flag_emit_class_files)
8349 write_classfile (current_class);
8350 if (flag_emit_xref)
8351 expand_xref (current_class);
8352 else if (! flag_syntax_only)
8353 finish_class ();
8354 }
8355 }
b351b287
APB
8356}
8357
e04a16fb
AG
8358/* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8359 a tree list node containing RIGHT. Fore coming RIGHTs will be
8360 chained to this hook. LOCATION contains the location of the
8361 separating `.' operator. */
8362
8363static tree
8364make_qualified_primary (primary, right, location)
8365 tree primary, right;
8366 int location;
8367{
8368 tree wfl;
8369
c2952b01
APB
8370 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8371 wfl = build_wfl_wrap (primary);
e04a16fb
AG
8372 else
8373 {
8374 wfl = primary;
c2952b01
APB
8375 /* If wfl wasn't qualified, we build a first anchor */
8376 if (!EXPR_WFL_QUALIFICATION (wfl))
8377 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
e04a16fb
AG
8378 }
8379
c2952b01 8380 /* And chain them */
e04a16fb
AG
8381 EXPR_WFL_LINECOL (right) = location;
8382 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8383 PRIMARY_P (wfl) = 1;
8384 return wfl;
8385}
8386
8387/* Simple merge of two name separated by a `.' */
8388
8389static tree
8390merge_qualified_name (left, right)
8391 tree left, right;
8392{
8393 tree node;
c2952b01
APB
8394 if (!left && !right)
8395 return NULL_TREE;
8396
8397 if (!left)
8398 return right;
8399
8400 if (!right)
8401 return left;
8402
e04a16fb
AG
8403 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8404 IDENTIFIER_LENGTH (left));
8405 obstack_1grow (&temporary_obstack, '.');
8406 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8407 IDENTIFIER_LENGTH (right));
8408 node = get_identifier (obstack_base (&temporary_obstack));
8409 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8410 QUALIFIED_P (node) = 1;
8411 return node;
8412}
8413
8414/* Merge the two parts of a qualified name into LEFT. Set the
8415 location information of the resulting node to LOCATION, usually
8416 inherited from the location information of the `.' operator. */
8417
8418static tree
8419make_qualified_name (left, right, location)
8420 tree left, right;
8421 int location;
8422{
bc3ca41b
PB
8423#ifdef USE_COMPONENT_REF
8424 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8425 EXPR_WFL_LINECOL (node) = location;
8426 return node;
8427#else
e04a16fb
AG
8428 tree left_id = EXPR_WFL_NODE (left);
8429 tree right_id = EXPR_WFL_NODE (right);
8430 tree wfl, merge;
8431
8432 merge = merge_qualified_name (left_id, right_id);
8433
8434 /* Left wasn't qualified and is now qualified */
8435 if (!QUALIFIED_P (left_id))
8436 {
8437 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8438 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8439 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8440 }
8441
8442 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8443 EXPR_WFL_LINECOL (wfl) = location;
8444 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8445
8446 EXPR_WFL_NODE (left) = merge;
8447 return left;
bc3ca41b 8448#endif
e04a16fb
AG
8449}
8450
8451/* Extract the last identifier component of the qualified in WFL. The
8452 last identifier is removed from the linked list */
8453
8454static tree
8455cut_identifier_in_qualified (wfl)
8456 tree wfl;
8457{
8458 tree q;
8459 tree previous = NULL_TREE;
8460 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8461 if (!TREE_CHAIN (q))
8462 {
8463 if (!previous)
781b0558 8464 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
e04a16fb
AG
8465 TREE_CHAIN (previous) = NULL_TREE;
8466 return TREE_PURPOSE (q);
8467 }
8468}
8469
8470/* Resolve the expression name NAME. Return its decl. */
8471
8472static tree
5e942c50 8473resolve_expression_name (id, orig)
e04a16fb 8474 tree id;
5e942c50 8475 tree *orig;
e04a16fb
AG
8476{
8477 tree name = EXPR_WFL_NODE (id);
8478 tree decl;
8479
8480 /* 6.5.5.1: Simple expression names */
8481 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8482 {
8483 /* 15.13.1: NAME can appear within the scope of a local variable
8484 declaration */
8485 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8486 return decl;
8487
8488 /* 15.13.1: NAME can appear within a class declaration */
8489 else
8490 {
8491 decl = lookup_field_wrapper (current_class, name);
8492 if (decl)
8493 {
c2952b01 8494 tree access = NULL_TREE;
e04a16fb 8495 int fs = FIELD_STATIC (decl);
f2760b27
APB
8496
8497 /* If we're accessing an outer scope local alias, make
8498 sure we change the name of the field we're going to
8499 build access to. */
8500 if (FIELD_LOCAL_ALIAS_USED (decl))
8501 name = DECL_NAME (decl);
8502
e04a16fb
AG
8503 /* Instance variable (8.3.1.1) can't appear within
8504 static method, static initializer or initializer for
8505 a static variable. */
8506 if (!fs && METHOD_STATIC (current_function_decl))
8507 {
7f10c2e2 8508 static_ref_err (id, name, current_class);
e04a16fb
AG
8509 return error_mark_node;
8510 }
22eed1e6
APB
8511 /* Instance variables can't appear as an argument of
8512 an explicit constructor invocation */
8513 if (!fs && ctxp->explicit_constructor_p)
8514 {
8515 parse_error_context
781b0558 8516 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
22eed1e6
APB
8517 return error_mark_node;
8518 }
5e942c50 8519
c2952b01
APB
8520 /* If we're processing an inner class and we're trying
8521 to access a field belonging to an outer class, build
8522 the access to the field */
8523 if (!fs && outer_field_access_p (current_class, decl))
8524 return build_outer_field_access (id, decl);
8525
5e942c50 8526 /* Otherwise build what it takes to access the field */
c2952b01
APB
8527 access = build_field_ref ((fs ? NULL_TREE : current_this),
8528 DECL_CONTEXT (decl), name);
e8fc7396 8529 if (fs && !flag_emit_class_files && !flag_emit_xref)
c2952b01 8530 access = build_class_init (DECL_CONTEXT (access), access);
5e942c50
APB
8531 /* We may be asked to save the real field access node */
8532 if (orig)
c2952b01 8533 *orig = access;
5e942c50 8534 /* And we return what we got */
c2952b01 8535 return access;
e04a16fb
AG
8536 }
8537 /* Fall down to error report on undefined variable */
8538 }
8539 }
8540 /* 6.5.5.2 Qualified Expression Names */
8541 else
8542 {
5e942c50
APB
8543 if (orig)
8544 *orig = NULL_TREE;
e04a16fb
AG
8545 qualify_ambiguous_name (id);
8546 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8547 /* 15.10.2: Accessing Superclass Members using super */
98f3c1db 8548 return resolve_field_access (id, orig, NULL);
e04a16fb
AG
8549 }
8550
8551 /* We've got an error here */
8552 parse_error_context (id, "Undefined variable `%s'",
8553 IDENTIFIER_POINTER (name));
8554
8555 return error_mark_node;
8556}
8557
7f10c2e2
APB
8558static void
8559static_ref_err (wfl, field_id, class_type)
8560 tree wfl, field_id, class_type;
8561{
8562 parse_error_context
8563 (wfl,
8564 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8565 IDENTIFIER_POINTER (field_id),
8566 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8567}
8568
e04a16fb
AG
8569/* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8570 We return something suitable to generate the field access. We also
8571 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8572 recipient's address can be null. */
8573
8574static tree
8575resolve_field_access (qual_wfl, field_decl, field_type)
8576 tree qual_wfl;
8577 tree *field_decl, *field_type;
8578{
8579 int is_static = 0;
8580 tree field_ref;
8581 tree decl, where_found, type_found;
8582
8583 if (resolve_qualified_expression_name (qual_wfl, &decl,
8584 &where_found, &type_found))
8585 return error_mark_node;
8586
8587 /* Resolve the LENGTH field of an array here */
8588 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
e8fc7396 8589 && ! flag_emit_class_files && ! flag_emit_xref)
e04a16fb
AG
8590 {
8591 tree length = build_java_array_length_access (where_found);
8592 field_ref =
8593 build_java_arraynull_check (type_found, length, int_type_node);
8594 }
8595 /* We might have been trying to resolve field.method(). In which
8596 case, the resolution is over and decl is the answer */
34f4db93 8597 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
e04a16fb 8598 field_ref = decl;
34f4db93 8599 else if (JDECL_P (decl))
e04a16fb 8600 {
5e942c50
APB
8601 int static_final_found = 0;
8602 if (!type_found)
8603 type_found = DECL_CONTEXT (decl);
34f4db93 8604 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
5e942c50
APB
8605 if (FIELD_FINAL (decl)
8606 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8607 && DECL_LANG_SPECIFIC (decl)
7525cc04 8608 && DECL_INITIAL (decl))
5e942c50 8609 {
7525cc04 8610 field_ref = DECL_INITIAL (decl);
5e942c50
APB
8611 static_final_found = 1;
8612 }
8613 else
7f10c2e2
APB
8614 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8615 NULL_TREE : where_found),
5e942c50 8616 type_found, DECL_NAME (decl));
e04a16fb
AG
8617 if (field_ref == error_mark_node)
8618 return error_mark_node;
e8fc7396
APB
8619 if (is_static && !static_final_found
8620 && !flag_emit_class_files && !flag_emit_xref)
98f3c1db 8621 field_ref = build_class_init (type_found, field_ref);
e04a16fb
AG
8622 }
8623 else
8624 field_ref = decl;
8625
8626 if (field_decl)
8627 *field_decl = decl;
8628 if (field_type)
c877974e
APB
8629 *field_type = (QUAL_DECL_TYPE (decl) ?
8630 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
e04a16fb
AG
8631 return field_ref;
8632}
8633
e28cd97b
APB
8634/* If NODE is an access to f static field, strip out the class
8635 initialization part and return the field decl, otherwise, return
8636 NODE. */
8637
8638static tree
8639strip_out_static_field_access_decl (node)
8640 tree node;
8641{
8642 if (TREE_CODE (node) == COMPOUND_EXPR)
8643 {
8644 tree op1 = TREE_OPERAND (node, 1);
8645 if (TREE_CODE (op1) == COMPOUND_EXPR)
8646 {
8647 tree call = TREE_OPERAND (op1, 0);
8648 if (TREE_CODE (call) == CALL_EXPR
8649 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8650 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8651 == soft_initclass_node)
8652 return TREE_OPERAND (op1, 1);
8653 }
2f11d407
TT
8654 else if (JDECL_P (op1))
8655 return op1;
e28cd97b
APB
8656 }
8657 return node;
8658}
8659
e04a16fb
AG
8660/* 6.5.5.2: Qualified Expression Names */
8661
8662static int
8663resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8664 tree wfl;
8665 tree *found_decl, *type_found, *where_found;
8666{
8667 int from_type = 0; /* Field search initiated from a type */
c2952b01 8668 int from_super = 0, from_cast = 0, from_qualified_this = 0;
e04a16fb
AG
8669 int previous_call_static = 0;
8670 int is_static;
8671 tree decl = NULL_TREE, type = NULL_TREE, q;
c2952b01
APB
8672 /* For certain for of inner class instantiation */
8673 tree saved_current, saved_this;
8674#define RESTORE_THIS_AND_CURRENT_CLASS \
8675 { current_class = saved_current; current_this = saved_this;}
8676
c877974e 8677 *type_found = *where_found = NULL_TREE;
e04a16fb
AG
8678
8679 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8680 {
8681 tree qual_wfl = QUAL_WFL (q);
7705e9db
APB
8682 tree ret_decl; /* for EH checking */
8683 int location; /* for EH checking */
e04a16fb
AG
8684
8685 /* 15.10.1 Field Access Using a Primary */
e04a16fb
AG
8686 switch (TREE_CODE (qual_wfl))
8687 {
8688 case CALL_EXPR:
b67d701b 8689 case NEW_CLASS_EXPR:
e04a16fb
AG
8690 /* If the access to the function call is a non static field,
8691 build the code to access it. */
34f4db93 8692 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb 8693 {
ac825856
APB
8694 decl = maybe_access_field (decl, *where_found,
8695 DECL_CONTEXT (decl));
e04a16fb
AG
8696 if (decl == error_mark_node)
8697 return 1;
8698 }
c2952b01 8699
e04a16fb
AG
8700 /* And code for the function call */
8701 if (complete_function_arguments (qual_wfl))
8702 return 1;
c2952b01
APB
8703
8704 /* We might have to setup a new current class and a new this
8705 for the search of an inner class, relative to the type of
8706 a expression resolved as `decl'. The current values are
8707 saved and restored shortly after */
8708 saved_current = current_class;
8709 saved_this = current_this;
8710 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8711 {
8712 current_class = type;
8713 current_this = decl;
8714 }
8715
89e09b9a
PB
8716 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8717 CALL_USING_SUPER (qual_wfl) = 1;
7705e9db
APB
8718 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8719 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8720 *where_found = patch_method_invocation (qual_wfl, decl, type,
8721 &is_static, &ret_decl);
e04a16fb 8722 if (*where_found == error_mark_node)
c2952b01
APB
8723 {
8724 RESTORE_THIS_AND_CURRENT_CLASS;
8725 return 1;
8726 }
e04a16fb
AG
8727 *type_found = type = QUAL_DECL_TYPE (*where_found);
8728
c2952b01
APB
8729 /* If we're creating an inner class instance, check for that
8730 an enclosing instance is in scope */
8731 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
165f37bc 8732 && INNER_ENCLOSING_SCOPE_CHECK (type))
c2952b01
APB
8733 {
8734 parse_error_context
165f37bc
APB
8735 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8736 lang_printable_name (type, 0),
8737 (!current_this ? "" :
8738 "; an explicit one must be provided when creating this inner class"));
c2952b01
APB
8739 RESTORE_THIS_AND_CURRENT_CLASS;
8740 return 1;
8741 }
8742
8743 /* In case we had to change then to resolve a inner class
8744 instantiation using a primary qualified by a `new' */
8745 RESTORE_THIS_AND_CURRENT_CLASS;
8746
7705e9db
APB
8747 /* EH check */
8748 if (location)
8749 check_thrown_exceptions (location, ret_decl);
8750
e04a16fb
AG
8751 /* If the previous call was static and this one is too,
8752 build a compound expression to hold the two (because in
8753 that case, previous function calls aren't transported as
8754 forcoming function's argument. */
8755 if (previous_call_static && is_static)
8756 {
8757 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8758 TREE_SIDE_EFFECTS (decl) = 1;
8759 }
8760 else
8761 {
8762 previous_call_static = is_static;
8763 decl = *where_found;
8764 }
c2952b01 8765 from_type = 0;
e04a16fb
AG
8766 continue;
8767
d8fccff5 8768 case NEW_ARRAY_EXPR:
c2952b01 8769 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5
APB
8770 *where_found = decl = java_complete_tree (qual_wfl);
8771 if (decl == error_mark_node)
8772 return 1;
8773 *type_found = type = QUAL_DECL_TYPE (decl);
8774 CLASS_LOADED_P (type) = 1;
8775 continue;
8776
e04a16fb
AG
8777 case CONVERT_EXPR:
8778 *where_found = decl = java_complete_tree (qual_wfl);
8779 if (decl == error_mark_node)
8780 return 1;
8781 *type_found = type = QUAL_DECL_TYPE (decl);
8782 from_cast = 1;
8783 continue;
8784
22eed1e6 8785 case CONDITIONAL_EXPR:
5e942c50 8786 case STRING_CST:
ac22f9cb 8787 case MODIFY_EXPR:
22eed1e6
APB
8788 *where_found = decl = java_complete_tree (qual_wfl);
8789 if (decl == error_mark_node)
8790 return 1;
8791 *type_found = type = QUAL_DECL_TYPE (decl);
8792 continue;
8793
e04a16fb
AG
8794 case ARRAY_REF:
8795 /* If the access to the function call is a non static field,
8796 build the code to access it. */
34f4db93 8797 if (JDECL_P (decl) && !FIELD_STATIC (decl))
e04a16fb
AG
8798 {
8799 decl = maybe_access_field (decl, *where_found, type);
8800 if (decl == error_mark_node)
8801 return 1;
8802 }
8803 /* And code for the array reference expression */
8804 decl = java_complete_tree (qual_wfl);
8805 if (decl == error_mark_node)
8806 return 1;
8807 type = QUAL_DECL_TYPE (decl);
8808 continue;
0a2138e2 8809
37feda7d
APB
8810 case PLUS_EXPR:
8811 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8812 return 1;
8813 if ((type = patch_string (decl)))
8814 decl = type;
8815 *where_found = QUAL_RESOLUTION (q) = decl;
8816 *type_found = type = TREE_TYPE (decl);
8817 break;
8818
165f37bc
APB
8819 case CLASS_LITERAL:
8820 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8821 return 1;
8822 *where_found = QUAL_RESOLUTION (q) = decl;
8823 *type_found = type = TREE_TYPE (decl);
8824 break;
8825
0a2138e2
APB
8826 default:
8827 /* Fix for -Wall Just go to the next statement. Don't
8828 continue */
a3f406ce 8829 break;
e04a16fb
AG
8830 }
8831
8832 /* If we fall here, we weren't processing a (static) function call. */
8833 previous_call_static = 0;
8834
8835 /* It can be the keyword THIS */
8836 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
8837 {
8838 if (!current_this)
8839 {
8840 parse_error_context
8841 (wfl, "Keyword `this' used outside allowed context");
8842 return 1;
8843 }
f63991a8
APB
8844 if (ctxp->explicit_constructor_p)
8845 {
781b0558 8846 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
f63991a8
APB
8847 return 1;
8848 }
e04a16fb 8849 /* We have to generate code for intermediate acess */
c2952b01
APB
8850 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
8851 {
8852 *where_found = decl = current_this;
8853 *type_found = type = QUAL_DECL_TYPE (decl);
8854 }
8855 /* We're trying to access the this from somewhere else... */
8856 else
8857 {
8858 *where_found = decl = build_current_thisn (type);
8859 from_qualified_this = 1;
8860 }
8861
8862 from_type = 0;
e04a16fb
AG
8863 continue;
8864 }
8865
8866 /* 15.10.2 Accessing Superclass Members using SUPER */
8867 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
8868 {
8869 tree node;
8870 /* Check on the restricted use of SUPER */
8871 if (METHOD_STATIC (current_function_decl)
8872 || current_class == object_type_node)
8873 {
8874 parse_error_context
8875 (wfl, "Keyword `super' used outside allowed context");
8876 return 1;
8877 }
8878 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
8879 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
8880 CLASSTYPE_SUPER (current_class),
8881 build_this (EXPR_WFL_LINECOL (qual_wfl)));
8882 *where_found = decl = java_complete_tree (node);
22eed1e6
APB
8883 if (decl == error_mark_node)
8884 return 1;
e04a16fb
AG
8885 *type_found = type = QUAL_DECL_TYPE (decl);
8886 from_super = from_type = 1;
8887 continue;
8888 }
8889
8890 /* 15.13.1: Can't search for field name in packages, so we
8891 assume a variable/class name was meant. */
8892 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
8893 {
5e942c50
APB
8894 tree name = resolve_package (wfl, &q);
8895 if (name)
8896 {
c2952b01 8897 tree list;
5e942c50 8898 *where_found = decl = resolve_no_layout (name, qual_wfl);
6b48deee 8899 /* We want to be absolutely sure that the class is laid
5e942c50
APB
8900 out. We're going to search something inside it. */
8901 *type_found = type = TREE_TYPE (decl);
8902 layout_class (type);
8903 from_type = 1;
c2952b01 8904
dde1da72
APB
8905 /* Fix them all the way down, if any are left. */
8906 if (q)
c2952b01 8907 {
dde1da72
APB
8908 list = TREE_CHAIN (q);
8909 while (list)
8910 {
8911 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
8912 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
8913 list = TREE_CHAIN (list);
8914 }
c2952b01 8915 }
5e942c50 8916 }
e04a16fb 8917 else
5e942c50
APB
8918 {
8919 if (from_super || from_cast)
8920 parse_error_context
8921 ((from_cast ? qual_wfl : wfl),
8922 "No variable `%s' defined in class `%s'",
8923 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
8924 lang_printable_name (type, 0));
8925 else
8926 parse_error_context
8927 (qual_wfl, "Undefined variable or class name: `%s'",
8928 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
8929 return 1;
8930 }
e04a16fb
AG
8931 }
8932
8933 /* We have a type name. It's been already resolved when the
8934 expression was qualified. */
8935 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
8936 {
8937 if (!(decl = QUAL_RESOLUTION (q)))
8938 return 1; /* Error reported already */
8939
c2952b01
APB
8940 /* Sneak preview. If next we see a `new', we're facing a
8941 qualification with resulted in a type being selected
8942 instead of a field. Report the error */
8943 if(TREE_CHAIN (q)
8944 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
8945 {
8946 parse_error_context (qual_wfl, "Undefined variable `%s'",
8947 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8948 return 1;
8949 }
8950
e04a16fb
AG
8951 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
8952 {
8953 parse_error_context
8954 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
8955 java_accstring_lookup (get_access_flags_from_decl (decl)),
2aa11e97 8956 GET_TYPE_NAME (type),
e04a16fb
AG
8957 IDENTIFIER_POINTER (DECL_NAME (decl)),
8958 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
8959 return 1;
8960 }
5e942c50 8961 check_deprecation (qual_wfl, decl);
c2952b01 8962
e04a16fb
AG
8963 type = TREE_TYPE (decl);
8964 from_type = 1;
8965 }
8966 /* We resolve and expression name */
8967 else
8968 {
cd531a2e 8969 tree field_decl = NULL_TREE;
e04a16fb
AG
8970
8971 /* If there exists an early resolution, use it. That occurs
8972 only once and we know that there are more things to
8973 come. Don't do that when processing something after SUPER
8974 (we need more thing to be put in place below */
8975 if (!from_super && QUAL_RESOLUTION (q))
b67d701b
PB
8976 {
8977 decl = QUAL_RESOLUTION (q);
c877974e 8978 if (!type)
5e942c50 8979 {
7f10c2e2
APB
8980 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
8981 {
8982 if (current_this)
8983 *where_found = current_this;
8984 else
8985 {
8986 static_ref_err (qual_wfl, DECL_NAME (decl),
8987 current_class);
8988 return 1;
8989 }
8990 }
c877974e
APB
8991 else
8992 {
8993 *where_found = TREE_TYPE (decl);
8994 if (TREE_CODE (*where_found) == POINTER_TYPE)
8995 *where_found = TREE_TYPE (*where_found);
8996 }
5e942c50 8997 }
b67d701b 8998 }
e04a16fb
AG
8999
9000 /* We have to search for a field, knowing the type of its
9001 container. The flag FROM_TYPE indicates that we resolved
9002 the last member of the expression as a type name, which
5e942c50
APB
9003 means that for the resolution of this field, we'll look
9004 for other errors than if it was resolved as a member of
9005 an other field. */
e04a16fb
AG
9006 else
9007 {
9008 int is_static;
5e942c50
APB
9009 tree field_decl_type; /* For layout */
9010
e04a16fb
AG
9011 if (!from_type && !JREFERENCE_TYPE_P (type))
9012 {
9013 parse_error_context
9014 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9015 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
0a2138e2 9016 lang_printable_name (type, 0),
e04a16fb
AG
9017 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9018 return 1;
9019 }
9020
dc0b3eff
PB
9021 field_decl = lookup_field_wrapper (type,
9022 EXPR_WFL_NODE (qual_wfl));
9023 if (field_decl == NULL_TREE)
e04a16fb
AG
9024 {
9025 parse_error_context
2aa11e97 9026 (qual_wfl, "No variable `%s' defined in type `%s'",
e04a16fb 9027 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
2aa11e97 9028 GET_TYPE_NAME (type));
e04a16fb
AG
9029 return 1;
9030 }
dc0b3eff
PB
9031 if (field_decl == error_mark_node)
9032 return 1;
5e942c50
APB
9033
9034 /* Layout the type of field_decl, since we may need
c877974e
APB
9035 it. Don't do primitive types or loaded classes. The
9036 situation of non primitive arrays may not handled
9037 properly here. FIXME */
5e942c50
APB
9038 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9039 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9040 else
9041 field_decl_type = TREE_TYPE (field_decl);
9042 if (!JPRIMITIVE_TYPE_P (field_decl_type)
c877974e
APB
9043 && !CLASS_LOADED_P (field_decl_type)
9044 && !TYPE_ARRAY_P (field_decl_type))
9045 resolve_and_layout (field_decl_type, NULL_TREE);
9046 if (TYPE_ARRAY_P (field_decl_type))
9047 CLASS_LOADED_P (field_decl_type) = 1;
e04a16fb
AG
9048
9049 /* Check on accessibility here */
9050 if (not_accessible_p (type, field_decl, from_super))
9051 {
9052 parse_error_context
9053 (qual_wfl,
9054 "Can't access %s field `%s.%s' from `%s'",
9055 java_accstring_lookup
9056 (get_access_flags_from_decl (field_decl)),
2aa11e97 9057 GET_TYPE_NAME (type),
e04a16fb
AG
9058 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9059 IDENTIFIER_POINTER
9060 (DECL_NAME (TYPE_NAME (current_class))));
9061 return 1;
9062 }
5e942c50 9063 check_deprecation (qual_wfl, field_decl);
e04a16fb
AG
9064
9065 /* There are things to check when fields are accessed
9066 from type. There are no restrictions on a static
9067 declaration of the field when it is accessed from an
9068 interface */
9069 is_static = FIELD_STATIC (field_decl);
9070 if (!from_super && from_type
c2952b01
APB
9071 && !TYPE_INTERFACE_P (type)
9072 && !is_static
9073 && (current_function_decl
9074 && METHOD_STATIC (current_function_decl)))
e04a16fb 9075 {
7f10c2e2 9076 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
e04a16fb
AG
9077 return 1;
9078 }
9079 from_cast = from_super = 0;
9080
c2952b01
APB
9081 /* It's an access from a type but it isn't static, we
9082 make it relative to `this'. */
9083 if (!is_static && from_type)
9084 decl = current_this;
9085
5e942c50
APB
9086 /* If we need to generate something to get a proper
9087 handle on what this field is accessed from, do it
9088 now. */
e04a16fb
AG
9089 if (!is_static)
9090 {
c583dd46 9091 decl = maybe_access_field (decl, *where_found, *type_found);
e04a16fb
AG
9092 if (decl == error_mark_node)
9093 return 1;
9094 }
9095
9096 /* We want to keep the location were found it, and the type
9097 we found. */
9098 *where_found = decl;
9099 *type_found = type;
9100
c2952b01
APB
9101 /* Generate the correct expression for field access from
9102 qualified this */
9103 if (from_qualified_this)
9104 {
9105 field_decl = build_outer_field_access (qual_wfl, field_decl);
9106 from_qualified_this = 0;
9107 }
9108
e04a16fb
AG
9109 /* This is the decl found and eventually the next one to
9110 search from */
9111 decl = field_decl;
9112 }
e04a16fb
AG
9113 from_type = 0;
9114 type = QUAL_DECL_TYPE (decl);
c2952b01
APB
9115
9116 /* Sneak preview. If decl is qualified by a `new', report
9117 the error here to be accurate on the peculiar construct */
9118 if (TREE_CHAIN (q)
9119 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9120 && !JREFERENCE_TYPE_P (type))
9121 {
9122 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9123 lang_printable_name (type, 0));
9124 return 1;
9125 }
e04a16fb 9126 }
dde1da72
APB
9127 /* `q' might have changed due to a after package resolution
9128 re-qualification */
9129 if (!q)
9130 break;
e04a16fb
AG
9131 }
9132 *found_decl = decl;
9133 return 0;
9134}
9135
9136/* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9137 can't be accessed from REFERENCE (a record type). */
9138
be245ac0
KG
9139static int
9140not_accessible_p (reference, member, from_super)
e04a16fb
AG
9141 tree reference, member;
9142 int from_super;
9143{
9144 int access_flag = get_access_flags_from_decl (member);
9145
9146 /* Access always granted for members declared public */
9147 if (access_flag & ACC_PUBLIC)
9148 return 0;
9149
9150 /* Check access on protected members */
9151 if (access_flag & ACC_PROTECTED)
9152 {
9153 /* Access granted if it occurs from within the package
9154 containing the class in which the protected member is
9155 declared */
9156 if (class_in_current_package (DECL_CONTEXT (member)))
9157 return 0;
9158
9bbc7d9f
PB
9159 /* If accessed with the form `super.member', then access is granted */
9160 if (from_super)
9161 return 0;
e04a16fb 9162
9bbc7d9f
PB
9163 /* Otherwise, access is granted if occuring from the class where
9164 member is declared or a subclass of it */
473e7b07 9165 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9bbc7d9f 9166 return 0;
e04a16fb
AG
9167 return 1;
9168 }
9169
9170 /* Check access on private members. Access is granted only if it
473e7b07
TT
9171 occurs from within the class in which it is declared. Exceptions
9172 are accesses from inner-classes. This section is probably not
c2952b01 9173 complete. FIXME */
e04a16fb 9174 if (access_flag & ACC_PRIVATE)
c2952b01
APB
9175 return (current_class == DECL_CONTEXT (member) ? 0 :
9176 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
e04a16fb
AG
9177
9178 /* Default access are permitted only when occuring within the
9179 package in which the type (REFERENCE) is declared. In other words,
9180 REFERENCE is defined in the current package */
9181 if (ctxp->package)
9182 return !class_in_current_package (reference);
473e7b07 9183
e04a16fb
AG
9184 /* Otherwise, access is granted */
9185 return 0;
9186}
9187
5e942c50
APB
9188/* Test deprecated decl access. */
9189static void
9190check_deprecation (wfl, decl)
9191 tree wfl, decl;
9192{
49f48c71 9193 const char *file = DECL_SOURCE_FILE (decl);
5e942c50
APB
9194 /* Complain if the field is deprecated and the file it was defined
9195 in isn't compiled at the same time the file which contains its
9196 use is */
9197 if (DECL_DEPRECATED (decl)
9198 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9199 {
9200 char the [20];
9201 switch (TREE_CODE (decl))
9202 {
9203 case FUNCTION_DECL:
9204 strcpy (the, "method");
9205 break;
9206 case FIELD_DECL:
9207 strcpy (the, "field");
9208 break;
9209 case TYPE_DECL:
9210 strcpy (the, "class");
9211 break;
15fdcfe9
PB
9212 default:
9213 fatal ("unexpected DECL code - check_deprecation");
5e942c50
APB
9214 }
9215 parse_warning_context
9216 (wfl, "The %s `%s' in class `%s' has been deprecated",
9217 the, lang_printable_name (decl, 0),
9218 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9219 }
9220}
9221
e04a16fb
AG
9222/* Returns 1 if class was declared in the current package, 0 otherwise */
9223
9224static int
9225class_in_current_package (class)
9226 tree class;
9227{
9228 static tree cache = NULL_TREE;
9229 int qualified_flag;
9230 tree left;
9231
9232 if (cache == class)
9233 return 1;
9234
9235 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9236
9237 /* If the current package is empty and the name of CLASS is
9238 qualified, class isn't in the current package. If there is a
9239 current package and the name of the CLASS is not qualified, class
9240 isn't in the current package */
0a2138e2 9241 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
e04a16fb
AG
9242 return 0;
9243
9244 /* If there is not package and the name of CLASS isn't qualified,
9245 they belong to the same unnamed package */
9246 if (!ctxp->package && !qualified_flag)
9247 return 1;
9248
9249 /* Compare the left part of the name of CLASS with the package name */
9250 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9251 if (ctxp->package == left)
9252 {
9253 cache = class;
9254 return 1;
9255 }
9256 return 0;
9257}
9258
9259/* This function may generate code to access DECL from WHERE. This is
9260 done only if certain conditions meet. */
9261
9262static tree
9263maybe_access_field (decl, where, type)
9264 tree decl, where, type;
9265{
5e942c50
APB
9266 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9267 && !FIELD_STATIC (decl))
e04a16fb 9268 decl = build_field_ref (where ? where : current_this,
c583dd46
APB
9269 (type ? type : DECL_CONTEXT (decl)),
9270 DECL_NAME (decl));
e04a16fb
AG
9271 return decl;
9272}
9273
15fdcfe9 9274/* Build a method invocation, by patching PATCH. If non NULL
e04a16fb
AG
9275 and according to the situation, PRIMARY and WHERE may be
9276 used. IS_STATIC is set to 1 if the invoked function is static. */
9277
9278static tree
89e09b9a 9279patch_method_invocation (patch, primary, where, is_static, ret_decl)
e04a16fb
AG
9280 tree patch, primary, where;
9281 int *is_static;
b9f7e36c 9282 tree *ret_decl;
e04a16fb
AG
9283{
9284 tree wfl = TREE_OPERAND (patch, 0);
9285 tree args = TREE_OPERAND (patch, 1);
9286 tree name = EXPR_WFL_NODE (wfl);
5e942c50 9287 tree list;
22eed1e6 9288 int is_static_flag = 0;
89e09b9a 9289 int is_super_init = 0;
bccaf73a 9290 tree this_arg = NULL_TREE;
e04a16fb
AG
9291
9292 /* Should be overriden if everything goes well. Otherwise, if
9293 something fails, it should keep this value. It stop the
9294 evaluation of a bogus assignment. See java_complete_tree,
9295 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9296 evaluating an assignment */
9297 TREE_TYPE (patch) = error_mark_node;
9298
9299 /* Since lookup functions are messing with line numbers, save the
9300 context now. */
9301 java_parser_context_save_global ();
9302
9303 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9304
9305 /* Resolution of qualified name, excluding constructors */
9306 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9307 {
dde1da72 9308 tree identifier, identifier_wfl, type, resolved;
e04a16fb
AG
9309 /* Extract the last IDENTIFIER of the qualified
9310 expression. This is a wfl and we will use it's location
9311 data during error report. */
9312 identifier_wfl = cut_identifier_in_qualified (wfl);
9313 identifier = EXPR_WFL_NODE (identifier_wfl);
9314
9315 /* Given the context, IDENTIFIER is syntactically qualified
9316 as a MethodName. We need to qualify what's before */
9317 qualify_ambiguous_name (wfl);
dde1da72 9318 resolved = resolve_field_access (wfl, NULL, NULL);
e04a16fb 9319
dde1da72
APB
9320 if (resolved == error_mark_node)
9321 PATCH_METHOD_RETURN_ERROR ();
9322
9323 type = GET_SKIP_TYPE (resolved);
9324 resolve_and_layout (type, NULL_TREE);
9325 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9326 args = nreverse (args);
2c56429a 9327
e04a16fb 9328 /* We're resolving a call from a type */
dde1da72 9329 if (TREE_CODE (resolved) == TYPE_DECL)
e04a16fb 9330 {
dde1da72 9331 if (CLASS_INTERFACE (resolved))
e04a16fb
AG
9332 {
9333 parse_error_context
781b0558
KG
9334 (identifier_wfl,
9335 "Can't make static reference to method `%s' in interface `%s'",
9336 IDENTIFIER_POINTER (identifier),
e04a16fb 9337 IDENTIFIER_POINTER (name));
b9f7e36c 9338 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9339 }
e04a16fb
AG
9340 if (list && !METHOD_STATIC (list))
9341 {
c2e3db92 9342 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9343 parse_error_context
9344 (identifier_wfl,
9345 "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2
APB
9346 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9347 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
e04a16fb 9348 free (fct_name);
b9f7e36c 9349 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9350 }
9351 }
e04a16fb 9352 else
dde1da72
APB
9353 this_arg = primary = resolved;
9354
5e942c50 9355 /* IDENTIFIER_WFL will be used to report any problem further */
e04a16fb
AG
9356 wfl = identifier_wfl;
9357 }
9358 /* Resolution of simple names, names generated after a primary: or
9359 constructors */
9360 else
9361 {
cd531a2e 9362 tree class_to_search = NULL_TREE;
c2952b01 9363 int lc; /* Looking for Constructor */
e04a16fb
AG
9364
9365 /* We search constructor in their target class */
9366 if (CALL_CONSTRUCTOR_P (patch))
9367 {
22eed1e6
APB
9368 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9369 class_to_search = EXPR_WFL_NODE (wfl);
9370 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9371 this_identifier_node)
9372 class_to_search = NULL_TREE;
9373 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9374 super_identifier_node)
e04a16fb 9375 {
89e09b9a 9376 is_super_init = 1;
22eed1e6
APB
9377 if (CLASSTYPE_SUPER (current_class))
9378 class_to_search =
9379 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9380 else
9381 {
781b0558 9382 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
22eed1e6
APB
9383 PATCH_METHOD_RETURN_ERROR ();
9384 }
e04a16fb 9385 }
22eed1e6
APB
9386
9387 /* Class to search is NULL if we're searching the current one */
9388 if (class_to_search)
e04a16fb 9389 {
c2952b01
APB
9390 class_to_search = resolve_and_layout (class_to_search, wfl);
9391
22eed1e6
APB
9392 if (!class_to_search)
9393 {
9394 parse_error_context
9395 (wfl, "Class `%s' not found in type declaration",
9396 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9397 PATCH_METHOD_RETURN_ERROR ();
9398 }
9399
5e942c50
APB
9400 /* Can't instantiate an abstract class, but we can
9401 invoke it's constructor. It's use within the `new'
9402 context is denied here. */
9403 if (CLASS_ABSTRACT (class_to_search)
9404 && TREE_CODE (patch) == NEW_CLASS_EXPR)
22eed1e6
APB
9405 {
9406 parse_error_context
781b0558
KG
9407 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9408 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
22eed1e6
APB
9409 PATCH_METHOD_RETURN_ERROR ();
9410 }
c2952b01 9411
22eed1e6 9412 class_to_search = TREE_TYPE (class_to_search);
e04a16fb 9413 }
22eed1e6
APB
9414 else
9415 class_to_search = current_class;
e04a16fb
AG
9416 lc = 1;
9417 }
9418 /* This is a regular search in the local class, unless an
9419 alternate class is specified. */
9420 else
9421 {
9422 class_to_search = (where ? where : current_class);
9423 lc = 0;
9424 }
c2952b01 9425
e04a16fb
AG
9426 /* NAME is a simple identifier or comes from a primary. Search
9427 in the class whose declaration contain the method being
9428 invoked. */
c877974e 9429 resolve_and_layout (class_to_search, NULL_TREE);
e04a16fb 9430
c2952b01 9431 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
e04a16fb
AG
9432 /* Don't continue if no method were found, as the next statement
9433 can't be executed then. */
b9f7e36c
APB
9434 if (!list)
9435 PATCH_METHOD_RETURN_ERROR ();
e04a16fb
AG
9436
9437 /* Check for static reference if non static methods */
9438 if (check_for_static_method_reference (wfl, patch, list,
9439 class_to_search, primary))
b9f7e36c 9440 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9441
165f37bc
APB
9442 /* Check for inner classes creation from illegal contexts */
9443 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9444 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9445 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9446 {
9447 parse_error_context
9448 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9449 lang_printable_name (class_to_search, 0),
9450 (!current_this ? "" :
9451 "; an explicit one must be provided when creating this inner class"));
9452 PATCH_METHOD_RETURN_ERROR ();
9453 }
9454
22eed1e6
APB
9455 /* Non static methods are called with the current object extra
9456 argument. If patch a `new TYPE()', the argument is the value
9457 returned by the object allocator. If method is resolved as a
9458 primary, use the primary otherwise use the current THIS. */
b9f7e36c 9459 args = nreverse (args);
bccaf73a 9460 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
c2952b01
APB
9461 {
9462 this_arg = primary ? primary : current_this;
9463
9464 /* If we're using an access method, things are different.
9465 There are two familly of cases:
9466
9467 1) We're not generating bytecodes:
9468
9469 - LIST is non static. It's invocation is transformed from
9470 x(a1,...,an) into this$<n>.x(a1,....an).
9471 - LIST is static. It's invocation is transformed from
9472 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9473
9474 2) We're generating bytecodes:
9475
9476 - LIST is non static. It's invocation is transformed from
9477 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9478 - LIST is static. It's invocation is transformed from
9479 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9480
9481 Of course, this$<n> can be abitrary complex, ranging from
9482 this$0 (the immediate outer context) to
9483 access$0(access$0(...(this$0))).
9484
9485 maybe_use_access_method returns a non zero value if the
dfb99c83
APB
9486 this_arg has to be moved into the (then generated) stub
9487 argument list. In the mean time, the selected function
9488 might have be replaced by a generated stub. */
c2952b01
APB
9489 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9490 args = tree_cons (NULL_TREE, this_arg, args);
9491 }
e04a16fb 9492 }
b67d701b 9493
e04a16fb
AG
9494 /* Merge point of all resolution schemes. If we have nothing, this
9495 is an error, already signaled */
b9f7e36c
APB
9496 if (!list)
9497 PATCH_METHOD_RETURN_ERROR ();
b67d701b 9498
e04a16fb
AG
9499 /* Check accessibility, position the is_static flag, build and
9500 return the call */
9bbc7d9f 9501 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
e04a16fb 9502 {
c2e3db92 9503 char *fct_name = xstrdup (lang_printable_name (list, 0));
e04a16fb
AG
9504 parse_error_context
9505 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9506 java_accstring_lookup (get_access_flags_from_decl (list)),
0a2138e2 9507 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
5e942c50
APB
9508 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9509 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
e04a16fb 9510 free (fct_name);
b9f7e36c 9511 PATCH_METHOD_RETURN_ERROR ();
e04a16fb 9512 }
5e942c50 9513 check_deprecation (wfl, list);
22eed1e6 9514
c2952b01
APB
9515 /* If invoking a innerclass constructor, there are hidden parameters
9516 to pass */
9517 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9518 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9519 {
9520 /* And make sure we add the accessed local variables to be saved
9521 in field aliases. */
9522 args = build_alias_initializer_parameter_list
9523 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9524
9525 /* We have to reverse things. Find out why. FIXME */
9526 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9527 args = nreverse (args);
9528
9529 /* Secretely pass the current_this/primary as a second argument */
165f37bc
APB
9530 if (primary || current_this)
9531 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9532 else
9533 args = tree_cons (NULL_TREE, integer_zero_node, args);
c2952b01
APB
9534 }
9535
22eed1e6 9536 is_static_flag = METHOD_STATIC (list);
bccaf73a
PB
9537 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9538 args = tree_cons (NULL_TREE, this_arg, args);
22eed1e6 9539
c3f2a476
APB
9540 /* In the context of an explicit constructor invocation, we can't
9541 invoke any method relying on `this'. Exceptions are: we're
9542 invoking a static function, primary exists and is not the current
9543 this, we're creating a new object. */
22eed1e6 9544 if (ctxp->explicit_constructor_p
c3f2a476
APB
9545 && !is_static_flag
9546 && (!primary || primary == current_this)
9547 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
22eed1e6 9548 {
781b0558 9549 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
22eed1e6
APB
9550 PATCH_METHOD_RETURN_ERROR ();
9551 }
e04a16fb 9552 java_parser_context_restore_global ();
22eed1e6
APB
9553 if (is_static)
9554 *is_static = is_static_flag;
b9f7e36c
APB
9555 /* Sometimes, we want the decl of the selected method. Such as for
9556 EH checking */
9557 if (ret_decl)
9558 *ret_decl = list;
89e09b9a
PB
9559 patch = patch_invoke (patch, list, args);
9560 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9561 {
c2952b01
APB
9562 tree finit_parms, finit_call;
9563
9564 /* Prepare to pass hidden parameters to $finit$, if any. */
9565 finit_parms = build_alias_initializer_parameter_list
9566 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
89e09b9a 9567
c2952b01
APB
9568 finit_call =
9569 build_method_invocation (build_wfl_node (finit_identifier_node),
9570 finit_parms);
9571
9572 /* Generate the code used to initialize fields declared with an
9573 initialization statement and build a compound statement along
9574 with the super constructor invocation. */
89e09b9a
PB
9575 patch = build (COMPOUND_EXPR, void_type_node, patch,
9576 java_complete_tree (finit_call));
9577 CAN_COMPLETE_NORMALLY (patch) = 1;
9578 }
9579 return patch;
e04a16fb
AG
9580}
9581
9582/* Check that we're not trying to do a static reference to a method in
9583 non static method. Return 1 if it's the case, 0 otherwise. */
9584
9585static int
9586check_for_static_method_reference (wfl, node, method, where, primary)
9587 tree wfl, node, method, where, primary;
9588{
9589 if (METHOD_STATIC (current_function_decl)
9590 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9591 {
c2e3db92 9592 char *fct_name = xstrdup (lang_printable_name (method, 0));
e04a16fb
AG
9593 parse_error_context
9594 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
0a2138e2 9595 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
e04a16fb
AG
9596 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9597 free (fct_name);
9598 return 1;
9599 }
9600 return 0;
9601}
9602
c2952b01
APB
9603/* Fix the invocation of *MDECL if necessary in the case of a
9604 invocation from an inner class. *THIS_ARG might be modified
9605 appropriately and an alternative access to *MDECL might be
9606 returned. */
9607
9608static int
9609maybe_use_access_method (is_super_init, mdecl, this_arg)
9610 int is_super_init;
9611 tree *mdecl, *this_arg;
9612{
9613 tree ctx;
9614 tree md = *mdecl, ta = *this_arg;
9615 int to_return = 0;
9616 int non_static_context = !METHOD_STATIC (md);
9617
9618 if (is_super_init
165f37bc
APB
9619 || DECL_CONTEXT (md) == current_class
9620 || !PURE_INNER_CLASS_TYPE_P (current_class)
9621 || DECL_FINIT_P (md))
c2952b01
APB
9622 return 0;
9623
9624 /* If we're calling a method found in an enclosing class, generate
9625 what it takes to retrieve the right this. Don't do that if we're
9626 invoking a static method. */
9627
9628 if (non_static_context)
9629 {
9630 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9631 if (ctx == DECL_CONTEXT (md))
9632 {
9633 ta = build_current_thisn (current_class);
9634 ta = build_wfl_node (ta);
9635 }
9636 else
9637 {
9638 tree type = ctx;
9639 while (type)
9640 {
9641 maybe_build_thisn_access_method (type);
9642 if (type == DECL_CONTEXT (md))
9643 {
9644 ta = build_access_to_thisn (ctx, type, 0);
9645 break;
9646 }
9647 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9648 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9649 }
9650 }
9651 ta = java_complete_tree (ta);
9652 }
9653
9654 /* We might have to use an access method to get to MD. We can
9655 break the method access rule as far as we're not generating
9656 bytecode */
9657 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9658 {
9659 md = build_outer_method_access_method (md);
9660 to_return = 1;
9661 }
9662
9663 *mdecl = md;
9664 *this_arg = ta;
9665
9666 /* Returnin a non zero value indicates we were doing a non static
9667 method invokation that is now a static invocation. It will have
9668 callee displace `this' to insert it in the regular argument
9669 list. */
9670 return (non_static_context && to_return);
9671}
9672
e04a16fb
AG
9673/* Patch an invoke expression METHOD and ARGS, based on its invocation
9674 mode. */
9675
9676static tree
89e09b9a 9677patch_invoke (patch, method, args)
e04a16fb 9678 tree patch, method, args;
e04a16fb
AG
9679{
9680 tree dtable, func;
0a2138e2 9681 tree original_call, t, ta;
e815887f 9682 tree cond = NULL_TREE;
e04a16fb 9683
5e942c50
APB
9684 /* Last step for args: convert build-in types. If we're dealing with
9685 a new TYPE() type call, the first argument to the constructor
e815887f 9686 isn't found in the incoming argument list, but delivered by
5e942c50
APB
9687 `new' */
9688 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9689 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9690 t = TREE_CHAIN (t);
ac825856
APB
9691 for (ta = args; t != end_params_node && ta;
9692 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
b9f7e36c
APB
9693 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9694 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9695 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
1a6d4fb7
APB
9696
9697 /* Resolve unresolved returned type isses */
9698 t = TREE_TYPE (TREE_TYPE (method));
9699 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9700 resolve_and_layout (TREE_TYPE (t), NULL);
c2952b01 9701
e8fc7396 9702 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9703 func = method;
9704 else
e04a16fb 9705 {
15fdcfe9 9706 tree signature = build_java_signature (TREE_TYPE (method));
89e09b9a 9707 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
15fdcfe9
PB
9708 {
9709 case INVOKE_VIRTUAL:
9710 dtable = invoke_build_dtable (0, args);
9711 func = build_invokevirtual (dtable, method);
9712 break;
b9f7e36c 9713
e815887f
TT
9714 case INVOKE_NONVIRTUAL:
9715 /* If the object for the method call is null, we throw an
9716 exception. We don't do this if the object is the current
9717 method's `this'. In other cases we just rely on an
9718 optimization pass to eliminate redundant checks. */
9719 if (TREE_VALUE (args) != current_this)
9720 {
9721 /* We use a SAVE_EXPR here to make sure we only evaluate
9722 the new `self' expression once. */
9723 tree save_arg = save_expr (TREE_VALUE (args));
9724 TREE_VALUE (args) = save_arg;
9725 cond = build (EQ_EXPR, boolean_type_node, save_arg,
9726 null_pointer_node);
9727 }
9728 /* Fall through. */
9729
15fdcfe9
PB
9730 case INVOKE_SUPER:
9731 case INVOKE_STATIC:
9732 func = build_known_method_ref (method, TREE_TYPE (method),
9733 DECL_CONTEXT (method),
9734 signature, args);
9735 break;
e04a16fb 9736
15fdcfe9
PB
9737 case INVOKE_INTERFACE:
9738 dtable = invoke_build_dtable (1, args);
173f556c 9739 func = build_invokeinterface (dtable, method);
15fdcfe9 9740 break;
5e942c50 9741
15fdcfe9 9742 default:
89e09b9a 9743 fatal ("internal error - unknown invocation_mode result");
15fdcfe9
PB
9744 }
9745
9746 /* Ensure self_type is initialized, (invokestatic). FIXME */
9747 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
e04a16fb
AG
9748 }
9749
e04a16fb
AG
9750 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
9751 TREE_OPERAND (patch, 0) = func;
9752 TREE_OPERAND (patch, 1) = args;
9753 original_call = patch;
9754
e815887f 9755 /* We're processing a `new TYPE ()' form. New is called and its
22eed1e6
APB
9756 returned value is the first argument to the constructor. We build
9757 a COMPOUND_EXPR and use saved expression so that the overall NEW
9758 expression value is a pointer to a newly created and initialized
9759 class. */
9760 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
e04a16fb
AG
9761 {
9762 tree class = DECL_CONTEXT (method);
9763 tree c1, saved_new, size, new;
e8fc7396 9764 if (flag_emit_class_files || flag_emit_xref)
15fdcfe9
PB
9765 {
9766 TREE_TYPE (patch) = build_pointer_type (class);
9767 return patch;
9768 }
e04a16fb
AG
9769 if (!TYPE_SIZE (class))
9770 safe_layout_class (class);
9771 size = size_in_bytes (class);
9772 new = build (CALL_EXPR, promote_type (class),
9773 build_address_of (alloc_object_node),
9774 tree_cons (NULL_TREE, build_class_ref (class),
9775 build_tree_list (NULL_TREE,
9776 size_in_bytes (class))),
9777 NULL_TREE);
9778 saved_new = save_expr (new);
9779 c1 = build_tree_list (NULL_TREE, saved_new);
9780 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
9781 TREE_OPERAND (original_call, 1) = c1;
9782 TREE_SET_CODE (original_call, CALL_EXPR);
9783 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
9784 }
e815887f
TT
9785
9786 /* If COND is set, then we are building a check to see if the object
9787 is NULL. */
9788 if (cond != NULL_TREE)
9789 {
9790 /* We have to make the `then' branch a compound expression to
9791 make the types turn out right. This seems bizarre. */
9792 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
9793 build (COMPOUND_EXPR, TREE_TYPE (patch),
9794 build (CALL_EXPR, void_type_node,
9795 build_address_of (soft_nullpointer_node),
9796 NULL_TREE, NULL_TREE),
9797 (FLOAT_TYPE_P (TREE_TYPE (patch))
9798 ? build_real (TREE_TYPE (patch), dconst0)
9799 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
9800 integer_zero_node))),
9801 patch);
9802 TREE_SIDE_EFFECTS (patch) = 1;
9803 }
9804
e04a16fb
AG
9805 return patch;
9806}
9807
9808static int
9809invocation_mode (method, super)
9810 tree method;
9811 int super;
9812{
9813 int access = get_access_flags_from_decl (method);
9814
22eed1e6
APB
9815 if (super)
9816 return INVOKE_SUPER;
9817
e815887f 9818 if (access & ACC_STATIC)
e04a16fb
AG
9819 return INVOKE_STATIC;
9820
e815887f
TT
9821 /* We have to look for a constructor before we handle nonvirtual
9822 calls; otherwise the constructor will look nonvirtual. */
9823 if (DECL_CONSTRUCTOR_P (method))
e04a16fb 9824 return INVOKE_STATIC;
e815887f
TT
9825
9826 if (access & ACC_FINAL || access & ACC_PRIVATE)
9827 return INVOKE_NONVIRTUAL;
9828
9829 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
9830 return INVOKE_NONVIRTUAL;
9831
e04a16fb
AG
9832 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
9833 return INVOKE_INTERFACE;
22eed1e6 9834
e04a16fb
AG
9835 return INVOKE_VIRTUAL;
9836}
9837
b67d701b
PB
9838/* Retrieve a refined list of matching methods. It covers the step
9839 15.11.2 (Compile-Time Step 2) */
e04a16fb
AG
9840
9841static tree
9842lookup_method_invoke (lc, cl, class, name, arg_list)
9843 int lc;
9844 tree cl;
9845 tree class, name, arg_list;
9846{
de4c7b02 9847 tree atl = end_params_node; /* Arg Type List */
c877974e 9848 tree method, signature, list, node;
49f48c71 9849 const char *candidates; /* Used for error report */
b5b8a0e7 9850 char *dup;
e04a16fb 9851
5e942c50 9852 /* Fix the arguments */
e04a16fb
AG
9853 for (node = arg_list; node; node = TREE_CHAIN (node))
9854 {
e3884b71 9855 tree current_arg = TREE_TYPE (TREE_VALUE (node));
c877974e 9856 /* Non primitive type may have to be resolved */
e3884b71 9857 if (!JPRIMITIVE_TYPE_P (current_arg))
c877974e
APB
9858 resolve_and_layout (current_arg, NULL_TREE);
9859 /* And promoted */
b67d701b 9860 if (TREE_CODE (current_arg) == RECORD_TYPE)
c877974e 9861 current_arg = promote_type (current_arg);
5e942c50 9862 atl = tree_cons (NULL_TREE, current_arg, atl);
e04a16fb 9863 }
e04a16fb 9864
c2952b01
APB
9865 /* Presto. If we're dealing with an anonymous class and a
9866 constructor call, generate the right constructor now, since we
9867 know the arguments' types. */
9868
9869 if (lc && ANONYMOUS_CLASS_P (class))
9870 craft_constructor (TYPE_NAME (class), atl);
9871
5e942c50
APB
9872 /* Find all candidates and then refine the list, searching for the
9873 most specific method. */
9874 list = find_applicable_accessible_methods_list (lc, class, name, atl);
9875 list = find_most_specific_methods_list (list);
b67d701b
PB
9876 if (list && !TREE_CHAIN (list))
9877 return TREE_VALUE (list);
e04a16fb 9878
b67d701b
PB
9879 /* Issue an error. List candidates if any. Candidates are listed
9880 only if accessible (non accessible methods may end-up here for
9881 the sake of a better error report). */
9882 candidates = NULL;
9883 if (list)
e04a16fb 9884 {
e04a16fb 9885 tree current;
b67d701b 9886 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
e04a16fb
AG
9887 for (current = list; current; current = TREE_CHAIN (current))
9888 {
b67d701b
PB
9889 tree cm = TREE_VALUE (current);
9890 char string [4096];
9891 if (!cm || not_accessible_p (class, cm, 0))
9892 continue;
b67d701b 9893 sprintf
22eed1e6
APB
9894 (string, " `%s' in `%s'%s",
9895 get_printable_method_name (cm),
b67d701b
PB
9896 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
9897 (TREE_CHAIN (current) ? "\n" : ""));
9898 obstack_grow (&temporary_obstack, string, strlen (string));
9899 }
9900 obstack_1grow (&temporary_obstack, '\0');
9901 candidates = obstack_finish (&temporary_obstack);
9902 }
9903 /* Issue the error message */
c877974e
APB
9904 method = make_node (FUNCTION_TYPE);
9905 TYPE_ARG_TYPES (method) = atl;
b67d701b 9906 signature = build_java_argument_signature (method);
c63b98cd 9907 dup = xstrdup (lang_printable_name (class, 0));
b5b8a0e7 9908 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
22eed1e6 9909 (lc ? "constructor" : "method"),
b5b8a0e7
APB
9910 (lc ? dup : IDENTIFIER_POINTER (name)),
9911 IDENTIFIER_POINTER (signature), dup,
b67d701b 9912 (candidates ? candidates : ""));
b5b8a0e7 9913 free (dup);
b67d701b
PB
9914 return NULL_TREE;
9915}
9916
5e942c50
APB
9917/* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
9918 when we're looking for a constructor. */
b67d701b
PB
9919
9920static tree
5e942c50
APB
9921find_applicable_accessible_methods_list (lc, class, name, arglist)
9922 int lc;
b67d701b
PB
9923 tree class, name, arglist;
9924{
165f37bc 9925 static int object_done = 0;
b67d701b
PB
9926 tree list = NULL_TREE, all_list = NULL_TREE;
9927
c2952b01
APB
9928 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
9929 {
9930 load_class (class, 1);
9931 safe_layout_class (class);
9932 }
9933
1982388a 9934 /* Search interfaces */
165f37bc 9935 if (CLASS_INTERFACE (TYPE_NAME (class)))
b67d701b 9936 {
e0422ed0 9937 static struct hash_table t, *searched_interfaces = NULL;
de0b553f 9938 static int search_not_done = 0;
1982388a
APB
9939 int i, n;
9940 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
9941
e0422ed0
APB
9942 /* Search in the hash table, otherwise create a new one if
9943 necessary and insert the new entry. */
9944
de0b553f 9945 if (searched_interfaces)
e0422ed0
APB
9946 {
9947 if (hash_lookup (searched_interfaces,
9948 (const hash_table_key) class, FALSE, NULL))
9949 return NULL;
de0b553f 9950 }
e0422ed0
APB
9951 else
9952 {
9953 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
9954 java_hash_compare_tree_node);
9955 searched_interfaces = &t;
9956 }
9957
9958 hash_lookup (searched_interfaces,
9959 (const hash_table_key) class, TRUE, NULL);
de0b553f 9960
165f37bc
APB
9961 search_applicable_methods_list (lc, TYPE_METHODS (class),
9962 name, arglist, &list, &all_list);
1982388a 9963 n = TREE_VEC_LENGTH (basetype_vec);
165f37bc 9964 for (i = 1; i < n; i++)
b67d701b 9965 {
de0b553f
APB
9966 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
9967 tree rlist;
9968
de0b553f
APB
9969 search_not_done++;
9970 rlist = find_applicable_accessible_methods_list (lc, t, name,
9971 arglist);
165f37bc 9972 list = chainon (rlist, list);
de0b553f
APB
9973 search_not_done--;
9974 }
9975
9976 /* We're done. Reset the searched interfaces list and finally search
9977 java.lang.Object */
9978 if (!search_not_done)
9979 {
165f37bc
APB
9980 if (!object_done)
9981 search_applicable_methods_list (lc,
9982 TYPE_METHODS (object_type_node),
9983 name, arglist, &list, &all_list);
e0422ed0
APB
9984 hash_table_free (searched_interfaces);
9985 searched_interfaces = NULL;
e04a16fb 9986 }
e04a16fb 9987 }
1982388a
APB
9988 /* Search classes */
9989 else
c2952b01 9990 {
165f37bc
APB
9991 tree sc = class;
9992 int seen_inner_class = 0;
c2952b01
APB
9993 search_applicable_methods_list (lc, TYPE_METHODS (class),
9994 name, arglist, &list, &all_list);
9995
165f37bc
APB
9996 /* We must search all interfaces of this class */
9997 if (!lc)
9998 {
9999 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10000 int n = TREE_VEC_LENGTH (basetype_vec), i;
10001 object_done = 1;
10002 for (i = 1; i < n; i++)
10003 {
10004 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10005 tree rlist;
10006 if (t != object_type_node)
10007 rlist = find_applicable_accessible_methods_list (lc, t,
10008 name, arglist);
10009 list = chainon (rlist, list);
10010 }
10011 object_done = 0;
10012 }
10013
c2952b01
APB
10014 /* Search enclosing context of inner classes before looking
10015 ancestors up. */
10016 while (!lc && INNER_CLASS_TYPE_P (class))
10017 {
165f37bc
APB
10018 tree rlist;
10019 seen_inner_class = 1;
c2952b01 10020 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
165f37bc
APB
10021 rlist = find_applicable_accessible_methods_list (lc, class,
10022 name, arglist);
10023 list = chainon (rlist, list);
c2952b01 10024 }
165f37bc
APB
10025
10026 if (!lc && seen_inner_class
10027 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10028 class = CLASSTYPE_SUPER (sc);
10029 else
10030 class = sc;
10031
10032 for (class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
10033 class; class = CLASSTYPE_SUPER (class))
10034 search_applicable_methods_list (lc, TYPE_METHODS (class),
10035 name, arglist, &list, &all_list);
c2952b01 10036 }
1982388a 10037
b67d701b
PB
10038 /* Either return the list obtained or all selected (but
10039 inaccessible) methods for better error report. */
10040 return (!list ? all_list : list);
10041}
e04a16fb 10042
1982388a
APB
10043/* Effectively search for the approriate method in method */
10044
10045static void
c2952b01 10046search_applicable_methods_list (lc, method, name, arglist, list, all_list)
1982388a
APB
10047 int lc;
10048 tree method, name, arglist;
10049 tree *list, *all_list;
10050{
10051 for (; method; method = TREE_CHAIN (method))
10052 {
10053 /* When dealing with constructor, stop here, otherwise search
10054 other classes */
10055 if (lc && !DECL_CONSTRUCTOR_P (method))
10056 continue;
10057 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10058 || (GET_METHOD_NAME (method) != name)))
10059 continue;
10060
10061 if (argument_types_convertible (method, arglist))
10062 {
10063 /* Retain accessible methods only */
10064 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10065 method, 0))
10066 *list = tree_cons (NULL_TREE, method, *list);
10067 else
10068 /* Also retain all selected method here */
10069 *all_list = tree_cons (NULL_TREE, method, *list);
10070 }
10071 }
10072}
10073
b67d701b
PB
10074/* 15.11.2.2 Choose the Most Specific Method */
10075
10076static tree
10077find_most_specific_methods_list (list)
10078 tree list;
10079{
10080 int max = 0;
10081 tree current, new_list = NULL_TREE;
10082 for (current = list; current; current = TREE_CHAIN (current))
e04a16fb 10083 {
b67d701b
PB
10084 tree method;
10085 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10086
10087 for (method = list; method; method = TREE_CHAIN (method))
10088 {
10089 /* Don't test a method against itself */
10090 if (method == current)
10091 continue;
10092
10093 /* Compare arguments and location where method where declared */
10094 if (argument_types_convertible (TREE_VALUE (method),
10095 TREE_VALUE (current))
10096 && valid_method_invocation_conversion_p
10097 (DECL_CONTEXT (TREE_VALUE (method)),
10098 DECL_CONTEXT (TREE_VALUE (current))))
10099 {
10100 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
10101 max = (v > max ? v : max);
10102 }
10103 }
e04a16fb
AG
10104 }
10105
b67d701b
PB
10106 /* Review the list and select the maximally specific methods */
10107 for (current = list; current; current = TREE_CHAIN (current))
10108 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10109 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10110
165f37bc
APB
10111 /* If we have several and they're all abstract, just pick the
10112 closest one. */
10113
10114 if (new_list && TREE_CHAIN (new_list))
10115 {
10116 tree c;
10117 for (c = new_list; c && METHOD_ABSTRACT (TREE_VALUE (c));
10118 c = TREE_CHAIN (c))
10119 ;
10120 if (!c)
10121 {
10122 new_list = nreverse (new_list);
10123 TREE_CHAIN (new_list) = NULL_TREE;
10124 }
10125 }
10126
b67d701b
PB
10127 /* If we can't find one, lower expectations and try to gather multiple
10128 maximally specific methods */
165f37bc 10129 while (!new_list && max)
b67d701b
PB
10130 {
10131 while (--max > 0)
10132 {
10133 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10134 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10135 }
b67d701b
PB
10136 }
10137
10138 return new_list;
e04a16fb
AG
10139}
10140
b67d701b
PB
10141/* Make sure that the type of each M2_OR_ARGLIST arguments can be
10142 converted by method invocation conversion (5.3) to the type of the
10143 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10144 to change less often than M1. */
e04a16fb 10145
b67d701b
PB
10146static int
10147argument_types_convertible (m1, m2_or_arglist)
10148 tree m1, m2_or_arglist;
e04a16fb 10149{
b67d701b
PB
10150 static tree m2_arg_value = NULL_TREE;
10151 static tree m2_arg_cache = NULL_TREE;
e04a16fb 10152
b67d701b 10153 register tree m1_arg, m2_arg;
e04a16fb 10154
c2952b01 10155 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
e04a16fb 10156
b67d701b
PB
10157 if (m2_arg_value == m2_or_arglist)
10158 m2_arg = m2_arg_cache;
10159 else
10160 {
10161 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10162 argument types */
10163 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10164 {
10165 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10166 if (!METHOD_STATIC (m2_or_arglist))
10167 m2_arg = TREE_CHAIN (m2_arg);
10168 }
10169 else
10170 m2_arg = m2_or_arglist;
e04a16fb 10171
b67d701b
PB
10172 m2_arg_value = m2_or_arglist;
10173 m2_arg_cache = m2_arg;
10174 }
e04a16fb 10175
de4c7b02 10176 while (m1_arg != end_params_node && m2_arg != end_params_node)
b67d701b 10177 {
c877974e 10178 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
b67d701b
PB
10179 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10180 TREE_VALUE (m2_arg)))
10181 break;
10182 m1_arg = TREE_CHAIN (m1_arg);
10183 m2_arg = TREE_CHAIN (m2_arg);
e04a16fb 10184 }
de4c7b02 10185 return m1_arg == end_params_node && m2_arg == end_params_node;
e04a16fb
AG
10186}
10187
10188/* Qualification routines */
10189
10190static void
10191qualify_ambiguous_name (id)
10192 tree id;
10193{
cd531a2e
KG
10194 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10195 saved_current_class;
d8fccff5 10196 int again, super_found = 0, this_found = 0, new_array_found = 0;
8576f094 10197 int code;
e04a16fb
AG
10198
10199 /* We first qualify the first element, then derive qualification of
10200 others based on the first one. If the first element is qualified
10201 by a resolution (field or type), this resolution is stored in the
10202 QUAL_RESOLUTION of the qual element being examined. We need to
10203 save the current_class since the use of SUPER might change the
10204 its value. */
10205 saved_current_class = current_class;
10206 qual = EXPR_WFL_QUALIFICATION (id);
10207 do {
10208
10209 /* Simple qualified expression feature a qual_wfl that is a
10210 WFL. Expression derived from a primary feature more complicated
10211 things like a CALL_EXPR. Expression from primary need to be
10212 worked out to extract the part on which the qualification will
10213 take place. */
10214 qual_wfl = QUAL_WFL (qual);
10215 switch (TREE_CODE (qual_wfl))
10216 {
10217 case CALL_EXPR:
10218 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10219 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10220 {
10221 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10222 qual_wfl = QUAL_WFL (qual);
10223 }
10224 break;
d8fccff5 10225 case NEW_ARRAY_EXPR:
c2952b01 10226 case NEW_ANONYMOUS_ARRAY_EXPR:
d8fccff5 10227 qual = TREE_CHAIN (qual);
1a6d4fb7 10228 again = new_array_found = 1;
d8fccff5 10229 continue;
e04a16fb 10230 case CONVERT_EXPR:
f2760b27
APB
10231 break;
10232 case NEW_CLASS_EXPR:
e04a16fb
AG
10233 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10234 break;
c583dd46
APB
10235 case ARRAY_REF:
10236 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10237 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10238 break;
8576f094
APB
10239 case STRING_CST:
10240 qual = TREE_CHAIN (qual);
10241 qual_wfl = QUAL_WFL (qual);
10242 break;
165f37bc
APB
10243 case CLASS_LITERAL:
10244 qual = TREE_CHAIN (qual);
10245 qual_wfl = QUAL_WFL (qual);
10246 break;
0a2138e2
APB
10247 default:
10248 /* Fix for -Wall. Just break doing nothing */
10249 break;
e04a16fb 10250 }
8576f094 10251
e04a16fb
AG
10252 ptr_type = current_class;
10253 again = 0;
8576f094
APB
10254 code = TREE_CODE (qual_wfl);
10255
10256 /* Pos evaluation: non WFL leading expression nodes */
10257 if (code == CONVERT_EXPR
10258 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10259 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10260
cd7c5840
APB
10261 else if (code == INTEGER_CST)
10262 name = qual_wfl;
10263
ac22f9cb 10264 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
8576f094
APB
10265 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10266 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10267
c2952b01
APB
10268 else if (code == TREE_LIST)
10269 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10270
37feda7d
APB
10271 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10272 || code == PLUS_EXPR)
8576f094
APB
10273 {
10274 qual = TREE_CHAIN (qual);
10275 qual_wfl = QUAL_WFL (qual);
10276 again = 1;
10277 }
10278 else
f441f671
APB
10279 {
10280 name = EXPR_WFL_NODE (qual_wfl);
10281 if (!name)
10282 {
10283 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10284 again = 1;
10285 }
10286 }
10287
e04a16fb
AG
10288 /* If we have a THIS (from a primary), we set the context accordingly */
10289 if (name == this_identifier_node)
10290 {
10291 qual = TREE_CHAIN (qual);
10292 qual_wfl = QUAL_WFL (qual);
22eed1e6
APB
10293 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10294 again = 1;
10295 else
10296 name = EXPR_WFL_NODE (qual_wfl);
e04a16fb
AG
10297 this_found = 1;
10298 }
10299 /* If we have a SUPER, we set the context accordingly */
10300 if (name == super_identifier_node)
10301 {
10302 current_class = CLASSTYPE_SUPER (ptr_type);
10303 /* Check that there is such a thing as a super class. If not,
10304 return. The error will be caught later on, during the
10305 resolution */
10306 if (!current_class)
10307 {
10308 current_class = saved_current_class;
10309 return;
10310 }
10311 qual = TREE_CHAIN (qual);
10312 /* Do one more interation to set things up */
10313 super_found = again = 1;
10314 }
10315 } while (again);
10316
f2760b27
APB
10317 /* If name appears within the scope of a local variable declaration
10318 or parameter declaration, then it is an expression name. We don't
10319 carry this test out if we're in the context of the use of SUPER
10320 or THIS */
cd7c5840
APB
10321 if (!this_found && !super_found
10322 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10323 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
e04a16fb
AG
10324 {
10325 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10326 QUAL_RESOLUTION (qual) = decl;
10327 }
10328
10329 /* If within the class/interface NAME was found to be used there
10330 exists a (possibly inherited) field named NAME, then this is an
d8fccff5
APB
10331 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10332 address length, it is OK. */
10333 else if ((decl = lookup_field_wrapper (ptr_type, name))
10334 || (new_array_found && name == length_identifier_node))
e04a16fb
AG
10335 {
10336 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
d8fccff5 10337 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
e04a16fb
AG
10338 }
10339
1a6d4fb7 10340 /* We reclassify NAME as yielding to a type name resolution if:
e04a16fb
AG
10341 - NAME is a class/interface declared within the compilation
10342 unit containing NAME,
10343 - NAME is imported via a single-type-import declaration,
10344 - NAME is declared in an another compilation unit of the package
10345 of the compilation unit containing NAME,
10346 - NAME is declared by exactly on type-import-on-demand declaration
1a6d4fb7
APB
10347 of the compilation unit containing NAME.
10348 - NAME is actually a STRING_CST. */
cd7c5840
APB
10349 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10350 || (decl = resolve_and_layout (name, NULL_TREE)))
e04a16fb
AG
10351 {
10352 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10353 QUAL_RESOLUTION (qual) = decl;
10354 }
10355
f2760b27 10356 /* Method call, array references and cast are expression name */
9bbc7d9f 10357 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
8576f094
APB
10358 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10359 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
e04a16fb
AG
10360 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10361
10362 /* Check here that NAME isn't declared by more than one
10363 type-import-on-demand declaration of the compilation unit
10364 containing NAME. FIXME */
10365
10366 /* Otherwise, NAME is reclassified as a package name */
10367 else
10368 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10369
10370 /* Propagate the qualification accross other components of the
10371 qualified name */
10372 for (qual = TREE_CHAIN (qual); qual;
10373 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10374 {
10375 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10376 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10377 else
10378 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10379 }
10380
10381 /* Store the global qualification for the ambiguous part of ID back
10382 into ID fields */
10383 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10384 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10385 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10386 RESOLVE_TYPE_NAME_P (id) = 1;
10387 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10388 RESOLVE_PACKAGE_NAME_P (id) = 1;
10389
10390 /* Restore the current class */
10391 current_class = saved_current_class;
10392}
10393
10394static int
10395breakdown_qualified (left, right, source)
10396 tree *left, *right, source;
10397{
10398 char *p = IDENTIFIER_POINTER (source), *base;
10399 int l = IDENTIFIER_LENGTH (source);
10400
10401 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10402 base = p;
10403 p += (l-1);
10404 while (*p != '.' && p != base)
10405 p--;
10406
10407 /* We didn't find a '.'. Return an error */
10408 if (p == base)
10409 return 1;
10410
10411 *p = '\0';
10412 if (right)
10413 *right = get_identifier (p+1);
10414 *left = get_identifier (IDENTIFIER_POINTER (source));
10415 *p = '.';
10416
10417 return 0;
10418}
10419
e04a16fb 10420/* Patch tree nodes in a function body. When a BLOCK is found, push
5b09b33e
PB
10421 local variable decls if present.
10422 Same as java_complete_lhs, but does resolve static finals to values. */
e04a16fb
AG
10423
10424static tree
10425java_complete_tree (node)
10426 tree node;
5b09b33e
PB
10427{
10428 node = java_complete_lhs (node);
10429 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7f10c2e2
APB
10430 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE
10431 && !flag_emit_xref)
5b09b33e
PB
10432 {
10433 tree value = DECL_INITIAL (node);
10434 DECL_INITIAL (node) = NULL_TREE;
100f7cd8 10435 push_obstacks (&permanent_obstack, &permanent_obstack);
5b09b33e 10436 value = fold_constant_for_init (value, node);
100f7cd8 10437 pop_obstacks ();
5b09b33e
PB
10438 DECL_INITIAL (node) = value;
10439 if (value != NULL_TREE)
c2952b01
APB
10440 {
10441 /* fold_constant_for_init sometimes widen the original type
10442 of the constant (i.e. byte to int.) It's not desirable,
10443 especially if NODE is a function argument. */
10444 if (TREE_CODE (value) == INTEGER_CST
10445 && TREE_TYPE (node) != TREE_TYPE (value))
10446 return convert (TREE_TYPE (node), value);
10447 else
10448 return value;
10449 }
5b09b33e
PB
10450 }
10451 return node;
10452}
10453
2aa11e97
APB
10454static tree
10455java_stabilize_reference (node)
10456 tree node;
10457{
10458 if (TREE_CODE (node) == COMPOUND_EXPR)
10459 {
10460 tree op0 = TREE_OPERAND (node, 0);
10461 tree op1 = TREE_OPERAND (node, 1);
642f15d1 10462 TREE_OPERAND (node, 0) = save_expr (op0);
2aa11e97
APB
10463 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10464 return node;
10465 }
5cbdba64 10466 return stabilize_reference (node);
2aa11e97
APB
10467}
10468
5b09b33e
PB
10469/* Patch tree nodes in a function body. When a BLOCK is found, push
10470 local variable decls if present.
10471 Same as java_complete_tree, but does not resolve static finals to values. */
10472
10473static tree
10474java_complete_lhs (node)
10475 tree node;
e04a16fb 10476{
22eed1e6 10477 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
b67d701b 10478 int flag;
e04a16fb
AG
10479
10480 /* CONVERT_EXPR always has its type set, even though it needs to be
b67d701b 10481 worked out. */
e04a16fb
AG
10482 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10483 return node;
10484
10485 /* The switch block implements cases processing container nodes
10486 first. Contained nodes are always written back. Leaves come
10487 next and return a value. */
10488 switch (TREE_CODE (node))
10489 {
10490 case BLOCK:
10491
10492 /* 1- Block section.
10493 Set the local values on decl names so we can identify them
10494 faster when they're referenced. At that stage, identifiers
10495 are legal so we don't check for declaration errors. */
10496 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10497 {
10498 DECL_CONTEXT (cn) = current_function_decl;
10499 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
e04a16fb 10500 }
15fdcfe9
PB
10501 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10502 CAN_COMPLETE_NORMALLY (node) = 1;
10503 else
e04a16fb 10504 {
15fdcfe9
PB
10505 tree stmt = BLOCK_EXPR_BODY (node);
10506 tree *ptr;
10507 int error_seen = 0;
10508 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10509 {
c877974e
APB
10510 /* Re-order from (((A; B); C); ...; Z) to
10511 (A; (B; (C ; (...; Z)))).
15fdcfe9
PB
10512 This makes it easier to scan the statements left-to-right
10513 without using recursion (which might overflow the stack
10514 if the block has many statements. */
10515 for (;;)
10516 {
10517 tree left = TREE_OPERAND (stmt, 0);
10518 if (TREE_CODE (left) != COMPOUND_EXPR)
10519 break;
10520 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10521 TREE_OPERAND (left, 1) = stmt;
10522 stmt = left;
10523 }
10524 BLOCK_EXPR_BODY (node) = stmt;
10525 }
10526
c877974e
APB
10527 /* Now do the actual complete, without deep recursion for
10528 long blocks. */
15fdcfe9 10529 ptr = &BLOCK_EXPR_BODY (node);
dc0b3eff
PB
10530 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10531 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
15fdcfe9
PB
10532 {
10533 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10534 tree *next = &TREE_OPERAND (*ptr, 1);
10535 TREE_OPERAND (*ptr, 0) = cur;
cd9643f7
PB
10536 if (cur == empty_stmt_node)
10537 {
10538 /* Optimization; makes it easier to detect empty bodies.
10539 Most useful for <clinit> with all-constant initializer. */
10540 *ptr = *next;
10541 continue;
10542 }
15fdcfe9
PB
10543 if (TREE_CODE (cur) == ERROR_MARK)
10544 error_seen++;
10545 else if (! CAN_COMPLETE_NORMALLY (cur))
10546 {
10547 wfl_op2 = *next;
10548 for (;;)
10549 {
10550 if (TREE_CODE (wfl_op2) == BLOCK)
10551 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10552 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10553 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10554 else
10555 break;
10556 }
10557 if (TREE_CODE (wfl_op2) != CASE_EXPR
dc0b3eff 10558 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
82371d41 10559 unreachable_stmt_error (*ptr);
15fdcfe9
PB
10560 }
10561 ptr = next;
10562 }
10563 *ptr = java_complete_tree (*ptr);
10564
10565 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
e04a16fb 10566 return error_mark_node;
15fdcfe9 10567 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
e04a16fb
AG
10568 }
10569 /* Turn local bindings to null */
10570 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10571 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10572
10573 TREE_TYPE (node) = void_type_node;
10574 break;
10575
10576 /* 2- They are expressions but ultimately deal with statements */
b67d701b 10577
b9f7e36c
APB
10578 case THROW_EXPR:
10579 wfl_op1 = TREE_OPERAND (node, 0);
10580 COMPLETE_CHECK_OP_0 (node);
c2952b01
APB
10581 /* 14.19 A throw statement cannot complete normally. */
10582 CAN_COMPLETE_NORMALLY (node) = 0;
b9f7e36c
APB
10583 return patch_throw_statement (node, wfl_op1);
10584
10585 case SYNCHRONIZED_EXPR:
10586 wfl_op1 = TREE_OPERAND (node, 0);
b9f7e36c
APB
10587 return patch_synchronized_statement (node, wfl_op1);
10588
b67d701b
PB
10589 case TRY_EXPR:
10590 return patch_try_statement (node);
10591
a7d8d81f
PB
10592 case TRY_FINALLY_EXPR:
10593 COMPLETE_CHECK_OP_0 (node);
10594 COMPLETE_CHECK_OP_1 (node);
10595 CAN_COMPLETE_NORMALLY (node)
10596 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10597 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10598 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10599 return node;
10600
5a005d9e
PB
10601 case CLEANUP_POINT_EXPR:
10602 COMPLETE_CHECK_OP_0 (node);
10603 TREE_TYPE (node) = void_type_node;
2aa11e97
APB
10604 CAN_COMPLETE_NORMALLY (node) =
10605 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10606 return node;
10607
10608 case WITH_CLEANUP_EXPR:
10609 COMPLETE_CHECK_OP_0 (node);
10610 COMPLETE_CHECK_OP_2 (node);
2aa11e97
APB
10611 CAN_COMPLETE_NORMALLY (node) =
10612 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
5a005d9e
PB
10613 TREE_TYPE (node) = void_type_node;
10614 return node;
10615
e04a16fb
AG
10616 case LABELED_BLOCK_EXPR:
10617 PUSH_LABELED_BLOCK (node);
10618 if (LABELED_BLOCK_BODY (node))
10619 COMPLETE_CHECK_OP_1 (node);
10620 TREE_TYPE (node) = void_type_node;
10621 POP_LABELED_BLOCK ();
1fb89a4d
APB
10622
10623 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
9dd939b2
APB
10624 {
10625 LABELED_BLOCK_BODY (node) = NULL_TREE;
10626 CAN_COMPLETE_NORMALLY (node) = 1;
10627 }
1fb89a4d 10628 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
15fdcfe9 10629 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
10630 return node;
10631
10632 case EXIT_BLOCK_EXPR:
10633 /* We don't complete operand 1, because it's the return value of
10634 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10635 return patch_bc_statement (node);
10636
15fdcfe9
PB
10637 case CASE_EXPR:
10638 cn = java_complete_tree (TREE_OPERAND (node, 0));
10639 if (cn == error_mark_node)
10640 return cn;
10641
8576f094
APB
10642 /* First, the case expression must be constant. Values of final
10643 fields are accepted. */
15fdcfe9 10644 cn = fold (cn);
8576f094
APB
10645 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10646 && JDECL_P (TREE_OPERAND (cn, 1))
10647 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10648 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
100f7cd8
APB
10649 {
10650 push_obstacks (&permanent_obstack, &permanent_obstack);
10651 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10652 TREE_OPERAND (cn, 1));
10653 pop_obstacks ();
10654 }
15fdcfe9 10655
ce6e9147 10656 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
15fdcfe9
PB
10657 {
10658 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10659 parse_error_context (node, "Constant expression required");
10660 return error_mark_node;
10661 }
10662
10663 nn = ctxp->current_loop;
10664
10665 /* It must be assignable to the type of the switch expression. */
c877974e
APB
10666 if (!try_builtin_assignconv (NULL_TREE,
10667 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
15fdcfe9
PB
10668 {
10669 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10670 parse_error_context
10671 (wfl_operator,
10672 "Incompatible type for case. Can't convert `%s' to `int'",
10673 lang_printable_name (TREE_TYPE (cn), 0));
10674 return error_mark_node;
10675 }
10676
10677 cn = fold (convert (int_type_node, cn));
10678
10679 /* Multiple instance of a case label bearing the same
10680 value is checked during code generation. The case
10681 expression is allright so far. */
10682 TREE_OPERAND (node, 0) = cn;
9bbc7d9f 10683 TREE_TYPE (node) = void_type_node;
15fdcfe9 10684 CAN_COMPLETE_NORMALLY (node) = 1;
10100cc7 10685 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10686 break;
10687
10688 case DEFAULT_EXPR:
10689 nn = ctxp->current_loop;
10690 /* Only one default label is allowed per switch statement */
10691 if (SWITCH_HAS_DEFAULT (nn))
10692 {
10693 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10694 parse_error_context (wfl_operator,
10695 "Duplicate case label: `default'");
10696 return error_mark_node;
10697 }
10698 else
10699 SWITCH_HAS_DEFAULT (nn) = 1;
9bbc7d9f 10700 TREE_TYPE (node) = void_type_node;
10100cc7 10701 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9
PB
10702 CAN_COMPLETE_NORMALLY (node) = 1;
10703 break;
10704
b67d701b 10705 case SWITCH_EXPR:
e04a16fb
AG
10706 case LOOP_EXPR:
10707 PUSH_LOOP (node);
10708 /* Check whether the loop was enclosed in a labeled
10709 statement. If not, create one, insert the loop in it and
10710 return the node */
10711 nn = patch_loop_statement (node);
b67d701b 10712
e04a16fb 10713 /* Anyways, walk the body of the loop */
b67d701b
PB
10714 if (TREE_CODE (node) == LOOP_EXPR)
10715 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10716 /* Switch statement: walk the switch expression and the cases */
10717 else
10718 node = patch_switch_statement (node);
10719
e04a16fb 10720 if (TREE_OPERAND (node, 0) == error_mark_node)
b635eb2f
PB
10721 nn = error_mark_node;
10722 else
15fdcfe9 10723 {
b635eb2f
PB
10724 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
10725 /* If we returned something different, that's because we
10726 inserted a label. Pop the label too. */
10727 if (nn != node)
10728 {
10729 if (CAN_COMPLETE_NORMALLY (node))
10730 CAN_COMPLETE_NORMALLY (nn) = 1;
10731 POP_LABELED_BLOCK ();
10732 }
15fdcfe9 10733 }
e04a16fb
AG
10734 POP_LOOP ();
10735 return nn;
10736
10737 case EXIT_EXPR:
10738 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10739 return patch_exit_expr (node);
10740
10741 case COND_EXPR:
10742 /* Condition */
10743 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10744 if (TREE_OPERAND (node, 0) == error_mark_node)
10745 return error_mark_node;
10746 /* then-else branches */
10747 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10748 if (TREE_OPERAND (node, 1) == error_mark_node)
10749 return error_mark_node;
10750 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
10751 if (TREE_OPERAND (node, 2) == error_mark_node)
10752 return error_mark_node;
10753 return patch_if_else_statement (node);
10754 break;
10755
22eed1e6
APB
10756 case CONDITIONAL_EXPR:
10757 /* Condition */
10758 wfl_op1 = TREE_OPERAND (node, 0);
10759 COMPLETE_CHECK_OP_0 (node);
10760 wfl_op2 = TREE_OPERAND (node, 1);
10761 COMPLETE_CHECK_OP_1 (node);
10762 wfl_op3 = TREE_OPERAND (node, 2);
10763 COMPLETE_CHECK_OP_2 (node);
10764 return patch_conditional_expr (node, wfl_op1, wfl_op2);
10765
e04a16fb
AG
10766 /* 3- Expression section */
10767 case COMPOUND_EXPR:
15fdcfe9 10768 wfl_op2 = TREE_OPERAND (node, 1);
ac825856
APB
10769 TREE_OPERAND (node, 0) = nn =
10770 java_complete_tree (TREE_OPERAND (node, 0));
dc0b3eff
PB
10771 if (wfl_op2 == empty_stmt_node)
10772 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
10773 else
15fdcfe9 10774 {
dc0b3eff 10775 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
bccaf73a 10776 {
dc0b3eff
PB
10777 /* An unreachable condition in a do-while statement
10778 is *not* (technically) an unreachable statement. */
10779 nn = wfl_op2;
10780 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
10781 nn = EXPR_WFL_NODE (nn);
10782 if (TREE_CODE (nn) != EXIT_EXPR)
10783 {
10784 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
10785 parse_error_context (wfl_operator, "Unreachable statement");
10786 }
bccaf73a 10787 }
dc0b3eff
PB
10788 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10789 if (TREE_OPERAND (node, 1) == error_mark_node)
10790 return error_mark_node;
10791 CAN_COMPLETE_NORMALLY (node)
10792 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
15fdcfe9 10793 }
e04a16fb
AG
10794 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
10795 break;
10796
10797 case RETURN_EXPR:
15fdcfe9 10798 /* CAN_COMPLETE_NORMALLY (node) = 0; */
e04a16fb
AG
10799 return patch_return (node);
10800
10801 case EXPR_WITH_FILE_LOCATION:
10802 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
10803 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15fdcfe9 10804 {
5423609c 10805 tree wfl = node;
15fdcfe9 10806 node = resolve_expression_name (node, NULL);
dc0b3eff
PB
10807 if (node == error_mark_node)
10808 return node;
5423609c
APB
10809 /* Keep line number information somewhere were it doesn't
10810 disrupt the completion process. */
2c56429a 10811 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
5423609c
APB
10812 {
10813 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
10814 TREE_OPERAND (node, 1) = wfl;
10815 }
15fdcfe9
PB
10816 CAN_COMPLETE_NORMALLY (node) = 1;
10817 }
e04a16fb
AG
10818 else
10819 {
5b09b33e
PB
10820 tree body;
10821 int save_lineno = lineno;
10822 lineno = EXPR_WFL_LINENO (node);
10823 body = java_complete_tree (EXPR_WFL_NODE (node));
10824 lineno = save_lineno;
15fdcfe9 10825 EXPR_WFL_NODE (node) = body;
dc0b3eff 10826 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
15fdcfe9 10827 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
cd9643f7
PB
10828 if (body == empty_stmt_node)
10829 {
10830 /* Optimization; makes it easier to detect empty bodies. */
10831 return body;
10832 }
dc0b3eff 10833 if (body == error_mark_node)
e04a16fb
AG
10834 {
10835 /* Its important for the evaluation of assignment that
10836 this mark on the TREE_TYPE is propagated. */
10837 TREE_TYPE (node) = error_mark_node;
10838 return error_mark_node;
10839 }
10840 else
10841 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
15fdcfe9 10842
e04a16fb
AG
10843 }
10844 break;
10845
b67d701b 10846 case NEW_ARRAY_EXPR:
e04a16fb
AG
10847 /* Patch all the dimensions */
10848 flag = 0;
10849 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
10850 {
10851 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
3a1760ac
APB
10852 tree dim = convert (int_type_node,
10853 java_complete_tree (TREE_VALUE (cn)));
e04a16fb
AG
10854 if (dim == error_mark_node)
10855 {
10856 flag = 1;
10857 continue;
10858 }
10859 else
10860 {
b9f7e36c 10861 TREE_VALUE (cn) = dim;
e04a16fb
AG
10862 /* Setup the location of the current dimension, for
10863 later error report. */
10864 TREE_PURPOSE (cn) =
10865 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
10866 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
10867 }
10868 }
10869 /* They complete the array creation expression, if no errors
10870 were found. */
15fdcfe9 10871 CAN_COMPLETE_NORMALLY (node) = 1;
aee48ef8
PB
10872 return (flag ? error_mark_node
10873 : force_evaluation_order (patch_newarray (node)));
e04a16fb 10874
c2952b01
APB
10875 case NEW_ANONYMOUS_ARRAY_EXPR:
10876 /* Create the array type if necessary. */
10877 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
10878 {
10879 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
10880 if (!(type = resolve_type_during_patch (type)))
10881 return error_mark_node;
10882 type = build_array_from_name (type, NULL_TREE,
10883 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
10884 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
10885 }
10886 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
10887 ANONYMOUS_ARRAY_INITIALIZER (node));
10888 if (node == error_mark_node)
10889 return error_mark_node;
10890 CAN_COMPLETE_NORMALLY (node) = 1;
10891 return node;
10892
b67d701b 10893 case NEW_CLASS_EXPR:
e04a16fb 10894 case CALL_EXPR:
b67d701b 10895 /* Complete function's argument(s) first */
e04a16fb
AG
10896 if (complete_function_arguments (node))
10897 return error_mark_node;
10898 else
b9f7e36c 10899 {
22eed1e6
APB
10900 tree decl, wfl = TREE_OPERAND (node, 0);
10901 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
10902
c877974e 10903 node = patch_method_invocation (node, NULL_TREE,
89e09b9a 10904 NULL_TREE, 0, &decl);
c877974e
APB
10905 if (node == error_mark_node)
10906 return error_mark_node;
10907
10908 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
10909 /* If we call this(...), register signature and positions */
10910 if (in_this)
10911 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
10912 tree_cons (wfl, decl,
10913 DECL_CONSTRUCTOR_CALLS (current_function_decl));
de4c7b02 10914 CAN_COMPLETE_NORMALLY (node) = 1;
dc0b3eff 10915 return force_evaluation_order (node);
b9f7e36c 10916 }
e04a16fb
AG
10917
10918 case MODIFY_EXPR:
10919 /* Save potential wfls */
10920 wfl_op1 = TREE_OPERAND (node, 0);
cd9643f7 10921 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
c2952b01 10922
cd9643f7
PB
10923 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
10924 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
10925 && DECL_INITIAL (nn) != NULL_TREE)
10926 {
100f7cd8
APB
10927 tree value;
10928
10929 push_obstacks (&permanent_obstack, &permanent_obstack);
10930 value = fold_constant_for_init (nn, nn);
10931 pop_obstacks ();
c2952b01 10932
cd9643f7
PB
10933 if (value != NULL_TREE)
10934 {
10935 tree type = TREE_TYPE (value);
c2952b01
APB
10936 if (JPRIMITIVE_TYPE_P (type) ||
10937 (type == string_ptr_type_node && ! flag_emit_class_files))
cd9643f7
PB
10938 return empty_stmt_node;
10939 }
10940 DECL_INITIAL (nn) = NULL_TREE;
10941 }
e04a16fb 10942 wfl_op2 = TREE_OPERAND (node, 1);
cd9643f7 10943
e04a16fb
AG
10944 if (TREE_OPERAND (node, 0) == error_mark_node)
10945 return error_mark_node;
10946
5cbdba64
APB
10947 flag = COMPOUND_ASSIGN_P (wfl_op2);
10948 if (flag)
e04a16fb 10949 {
c2952b01
APB
10950 /* This might break when accessing outer field from inner
10951 class. TESTME, FIXME */
2aa11e97 10952 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
e04a16fb
AG
10953
10954 /* Hand stablize the lhs on both places */
e04a16fb 10955 TREE_OPERAND (node, 0) = lvalue;
5cbdba64
APB
10956 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
10957 (flag_emit_class_files ? lvalue : save_expr (lvalue));
2aa11e97 10958
5cbdba64 10959 /* 15.25.2.a: Left hand is not an array access. FIXME */
2aa11e97
APB
10960 /* Now complete the RHS. We write it back later on. */
10961 nn = java_complete_tree (TREE_OPERAND (node, 1));
10962
642f15d1
APB
10963 if ((cn = patch_string (nn)))
10964 nn = cn;
10965
2aa11e97
APB
10966 /* The last part of the rewrite for E1 op= E2 is to have
10967 E1 = (T)(E1 op E2), with T being the type of E1. */
642f15d1
APB
10968 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
10969 TREE_TYPE (lvalue), nn));
5cbdba64
APB
10970
10971 /* 15.25.2.b: Left hand is an array access. FIXME */
e04a16fb
AG
10972 }
10973
f8976021 10974 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
c2952b01
APB
10975 function to complete this RHS. Note that a NEW_ARRAY_INIT
10976 might have been already fully expanded if created as a result
10977 of processing an anonymous array initializer. We avoid doing
10978 the operation twice by testing whether the node already bears
10979 a type. */
10980 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
fdec99c6 10981 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
f8976021 10982 TREE_OPERAND (node, 1));
2aa11e97 10983 /* Otherwise we simply complete the RHS */
f8976021
APB
10984 else
10985 nn = java_complete_tree (TREE_OPERAND (node, 1));
10986
e04a16fb 10987 if (nn == error_mark_node)
c0d87ff6 10988 return error_mark_node;
2aa11e97
APB
10989
10990 /* Write back the RHS as we evaluated it. */
e04a16fb 10991 TREE_OPERAND (node, 1) = nn;
b67d701b
PB
10992
10993 /* In case we're handling = with a String as a RHS, we need to
10994 produce a String out of the RHS (it might still be a
10995 STRING_CST or a StringBuffer at this stage */
10996 if ((nn = patch_string (TREE_OPERAND (node, 1))))
10997 TREE_OPERAND (node, 1) = nn;
c2952b01
APB
10998
10999 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11000 TREE_OPERAND (node, 1))))
11001 {
11002 /* We return error_mark_node if outer_field_access_fix
11003 detects we write into a final. */
11004 if (nn == error_mark_node)
11005 return error_mark_node;
11006 node = nn;
11007 }
11008 else
11009 {
11010 node = patch_assignment (node, wfl_op1, wfl_op2);
11011 /* Reorganize the tree if necessary. */
11012 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11013 || JSTRING_P (TREE_TYPE (node))))
11014 node = java_refold (node);
11015 }
11016
15fdcfe9
PB
11017 CAN_COMPLETE_NORMALLY (node) = 1;
11018 return node;
e04a16fb
AG
11019
11020 case MULT_EXPR:
11021 case PLUS_EXPR:
11022 case MINUS_EXPR:
11023 case LSHIFT_EXPR:
11024 case RSHIFT_EXPR:
11025 case URSHIFT_EXPR:
11026 case BIT_AND_EXPR:
11027 case BIT_XOR_EXPR:
11028 case BIT_IOR_EXPR:
11029 case TRUNC_MOD_EXPR:
c2952b01 11030 case TRUNC_DIV_EXPR:
e04a16fb
AG
11031 case RDIV_EXPR:
11032 case TRUTH_ANDIF_EXPR:
11033 case TRUTH_ORIF_EXPR:
11034 case EQ_EXPR:
11035 case NE_EXPR:
11036 case GT_EXPR:
11037 case GE_EXPR:
11038 case LT_EXPR:
11039 case LE_EXPR:
11040 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11041 knows how to handle those cases. */
11042 wfl_op1 = TREE_OPERAND (node, 0);
11043 wfl_op2 = TREE_OPERAND (node, 1);
b67d701b 11044
15fdcfe9 11045 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b
PB
11046 /* Don't complete string nodes if dealing with the PLUS operand. */
11047 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
2aa11e97
APB
11048 {
11049 nn = java_complete_tree (wfl_op1);
11050 if (nn == error_mark_node)
11051 return error_mark_node;
48a840d9 11052
2aa11e97
APB
11053 TREE_OPERAND (node, 0) = nn;
11054 }
b67d701b 11055 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
2aa11e97
APB
11056 {
11057 nn = java_complete_tree (wfl_op2);
11058 if (nn == error_mark_node)
11059 return error_mark_node;
48a840d9 11060
2aa11e97
APB
11061 TREE_OPERAND (node, 1) = nn;
11062 }
dc0b3eff 11063 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
e04a16fb 11064
5e942c50
APB
11065 case INSTANCEOF_EXPR:
11066 wfl_op1 = TREE_OPERAND (node, 0);
11067 COMPLETE_CHECK_OP_0 (node);
ce6e9147
APB
11068 if (flag_emit_xref)
11069 {
11070 TREE_TYPE (node) = boolean_type_node;
11071 return node;
11072 }
5e942c50
APB
11073 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11074
b67d701b 11075 case UNARY_PLUS_EXPR:
e04a16fb
AG
11076 case NEGATE_EXPR:
11077 case TRUTH_NOT_EXPR:
11078 case BIT_NOT_EXPR:
11079 case PREDECREMENT_EXPR:
11080 case PREINCREMENT_EXPR:
11081 case POSTDECREMENT_EXPR:
11082 case POSTINCREMENT_EXPR:
11083 case CONVERT_EXPR:
11084 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11085 how to handle those cases. */
11086 wfl_op1 = TREE_OPERAND (node, 0);
15fdcfe9 11087 CAN_COMPLETE_NORMALLY (node) = 1;
e04a16fb
AG
11088 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11089 if (TREE_OPERAND (node, 0) == error_mark_node)
11090 return error_mark_node;
4a5f66c3
APB
11091 node = patch_unaryop (node, wfl_op1);
11092 CAN_COMPLETE_NORMALLY (node) = 1;
11093 break;
e04a16fb
AG
11094
11095 case ARRAY_REF:
11096 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11097 how to handle those cases. */
11098 wfl_op1 = TREE_OPERAND (node, 0);
11099 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11100 if (TREE_OPERAND (node, 0) == error_mark_node)
11101 return error_mark_node;
7f1d4866 11102 if (!flag_emit_class_files && !flag_emit_xref)
b67d701b 11103 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
e04a16fb
AG
11104 /* The same applies to wfl_op2 */
11105 wfl_op2 = TREE_OPERAND (node, 1);
11106 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11107 if (TREE_OPERAND (node, 1) == error_mark_node)
11108 return error_mark_node;
7f1d4866 11109 if (!flag_emit_class_files && !flag_emit_xref)
22eed1e6 11110 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
939d7216 11111 return patch_array_ref (node);
e04a16fb 11112
63a212ed
PB
11113 case RECORD_TYPE:
11114 return node;;
11115
11116 case COMPONENT_REF:
11117 /* The first step in the re-write of qualified name handling. FIXME.
11118 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
9bbc7d9f 11119 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
63a212ed
PB
11120 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11121 {
11122 tree name = TREE_OPERAND (node, 1);
11123 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11124 if (field == NULL_TREE)
11125 {
11126 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11127 return error_mark_node;
11128 }
11129 if (! FIELD_STATIC (field))
11130 {
11131 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11132 return error_mark_node;
11133 }
11134 return field;
11135 }
11136 else
11137 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
9bbc7d9f 11138 break;
9bbc7d9f 11139
b67d701b 11140 case THIS_EXPR:
e04a16fb
AG
11141 /* Can't use THIS in a static environment */
11142 if (!current_this)
11143 {
11144 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
11145 parse_error_context (wfl_operator,
11146 "Keyword `this' used outside allowed context");
e04a16fb
AG
11147 TREE_TYPE (node) = error_mark_node;
11148 return error_mark_node;
11149 }
22eed1e6
APB
11150 if (ctxp->explicit_constructor_p)
11151 {
11152 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11153 parse_error_context
781b0558 11154 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
22eed1e6
APB
11155 TREE_TYPE (node) = error_mark_node;
11156 return error_mark_node;
11157 }
e04a16fb 11158 return current_this;
c2952b01
APB
11159
11160 case CLASS_LITERAL:
11161 CAN_COMPLETE_NORMALLY (node) = 1;
11162 node = patch_incomplete_class_ref (node);
11163 if (node == error_mark_node)
11164 return error_mark_node;
11165 break;
11166
11167 case INSTANCE_INITIALIZERS_EXPR:
11168 in_instance_initializer++;
11169 node = java_complete_tree (TREE_OPERAND (node, 0));
11170 in_instance_initializer--;
11171 if (node != error_mark_node)
11172 TREE_TYPE (node) = void_type_node;
11173 else
11174 return error_mark_node;
11175 break;
e04a16fb 11176
e04a16fb 11177 default:
15fdcfe9 11178 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 11179 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
c2952b01
APB
11180 and it's time to turn it into the appropriate String object */
11181 if ((nn = patch_string (node)))
11182 node = nn;
11183 else
11184 fatal ("No case for tree code `%s' - java_complete_tree\n",
11185 tree_code_name [TREE_CODE (node)]);
e04a16fb
AG
11186 }
11187 return node;
11188}
11189
11190/* Complete function call's argument. Return a non zero value is an
11191 error was found. */
11192
11193static int
11194complete_function_arguments (node)
11195 tree node;
11196{
11197 int flag = 0;
11198 tree cn;
11199
f63991a8 11200 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11201 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11202 {
b67d701b 11203 tree wfl = TREE_VALUE (cn), parm, temp;
e04a16fb 11204 parm = java_complete_tree (wfl);
c2952b01 11205
e04a16fb
AG
11206 if (parm == error_mark_node)
11207 {
11208 flag = 1;
11209 continue;
11210 }
b67d701b
PB
11211 /* If have a string literal that we haven't transformed yet or a
11212 crafted string buffer, as a result of use of the the String
11213 `+' operator. Build `parm.toString()' and expand it. */
11214 if ((temp = patch_string (parm)))
b9f7e36c 11215 parm = temp;
5e942c50
APB
11216 /* Inline PRIMTYPE.TYPE read access */
11217 parm = maybe_build_primttype_type_ref (parm, wfl);
b9f7e36c 11218
5e942c50 11219 TREE_VALUE (cn) = parm;
e04a16fb 11220 }
f63991a8 11221 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
e04a16fb
AG
11222 return flag;
11223}
11224
11225/* Sometimes (for loops and variable initialized during their
11226 declaration), we want to wrap a statement around a WFL and turn it
11227 debugable. */
11228
11229static tree
11230build_debugable_stmt (location, stmt)
11231 int location;
11232 tree stmt;
11233{
11234 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11235 {
11236 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11237 EXPR_WFL_LINECOL (stmt) = location;
11238 }
11239 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11240 return stmt;
11241}
11242
11243static tree
11244build_expr_block (body, decls)
11245 tree body, decls;
11246{
11247 tree node = make_node (BLOCK);
11248 BLOCK_EXPR_DECLS (node) = decls;
b67d701b 11249 BLOCK_EXPR_BODY (node) = body;
e04a16fb
AG
11250 if (body)
11251 TREE_TYPE (node) = TREE_TYPE (body);
11252 TREE_SIDE_EFFECTS (node) = 1;
11253 return node;
11254}
11255
b67d701b
PB
11256/* Create a new function block and link it approriately to current
11257 function block chain */
e04a16fb
AG
11258
11259static tree
11260enter_block ()
11261{
b67d701b
PB
11262 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11263}
11264
11265/* Link block B supercontext to the previous block. The current
11266 function DECL is used as supercontext when enter_a_block is called
11267 for the first time for a given function. The current function body
11268 (DECL_FUNCTION_BODY) is set to be block B. */
11269
11270static tree
11271enter_a_block (b)
11272 tree b;
11273{
e04a16fb
AG
11274 tree fndecl = current_function_decl;
11275
f099f336
APB
11276 if (!fndecl) {
11277 BLOCK_SUPERCONTEXT (b) = current_static_block;
11278 current_static_block = b;
11279 }
11280
11281 else if (!DECL_FUNCTION_BODY (fndecl))
e04a16fb
AG
11282 {
11283 BLOCK_SUPERCONTEXT (b) = fndecl;
11284 DECL_FUNCTION_BODY (fndecl) = b;
11285 }
11286 else
11287 {
11288 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11289 DECL_FUNCTION_BODY (fndecl) = b;
11290 }
11291 return b;
11292}
11293
11294/* Exit a block by changing the current function body
11295 (DECL_FUNCTION_BODY) to the current block super context, only if
11296 the block being exited isn't the method's top level one. */
11297
11298static tree
11299exit_block ()
11300{
f099f336
APB
11301 tree b;
11302 if (current_function_decl)
11303 {
11304 b = DECL_FUNCTION_BODY (current_function_decl);
11305 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11306 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11307 }
11308 else
11309 {
11310 b = current_static_block;
e04a16fb 11311
f099f336
APB
11312 if (BLOCK_SUPERCONTEXT (b))
11313 current_static_block = BLOCK_SUPERCONTEXT (b);
11314 }
e04a16fb
AG
11315 return b;
11316}
11317
11318/* Lookup for NAME in the nested function's blocks, all the way up to
11319 the current toplevel one. It complies with Java's local variable
11320 scoping rules. */
11321
11322static tree
11323lookup_name_in_blocks (name)
11324 tree name;
11325{
f099f336 11326 tree b = GET_CURRENT_BLOCK (current_function_decl);
e04a16fb
AG
11327
11328 while (b != current_function_decl)
11329 {
11330 tree current;
11331
11332 /* Paranoid sanity check. To be removed */
11333 if (TREE_CODE (b) != BLOCK)
11334 fatal ("non block expr function body - lookup_name_in_blocks");
11335
11336 for (current = BLOCK_EXPR_DECLS (b); current;
11337 current = TREE_CHAIN (current))
11338 if (DECL_NAME (current) == name)
11339 return current;
11340 b = BLOCK_SUPERCONTEXT (b);
11341 }
11342 return NULL_TREE;
11343}
11344
11345static void
11346maybe_absorb_scoping_blocks ()
11347{
f099f336 11348 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
e04a16fb
AG
11349 {
11350 tree b = exit_block ();
11351 java_method_add_stmt (current_function_decl, b);
11352 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11353 }
11354}
11355
11356\f
11357/* This section of the source is reserved to build_* functions that
11358 are building incomplete tree nodes and the patch_* functions that
11359 are completing them. */
11360
c2952b01
APB
11361/* Wrap a non WFL node around a WFL. */
11362static tree
11363build_wfl_wrap (node)
11364 tree node;
11365{
11366 tree wfl, node_to_insert = node;
11367
11368 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11369 with the way we're processing SUPER. A THIS from a primary as a
11370 different form than a SUPER. Turn THIS into something symbolic */
11371 if (TREE_CODE (node) == THIS_EXPR)
11372 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11373 else
11374 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11375
11376 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (node);
11377 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11378 return wfl;
11379}
11380
11381
9bbc7d9f 11382/* Build a super() constructor invocation. Returns empty_stmt_node if
22eed1e6
APB
11383 we're currently dealing with the class java.lang.Object. */
11384
11385static tree
e920ebc9
APB
11386build_super_invocation (mdecl)
11387 tree mdecl;
22eed1e6 11388{
e920ebc9 11389 if (DECL_CONTEXT (mdecl) == object_type_node)
9bbc7d9f 11390 return empty_stmt_node;
22eed1e6
APB
11391 else
11392 {
9ee9b555 11393 tree super_wfl = build_wfl_node (super_identifier_node);
c2952b01
APB
11394 tree a = NULL_TREE, t;
11395 /* If we're dealing with an anonymous class, pass the arguments
11396 of the crafted constructor along. */
11397 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11398 {
11399 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11400 for (; t != end_params_node; t = TREE_CHAIN (t))
11401 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11402 }
11403 return build_method_invocation (super_wfl, a);
22eed1e6
APB
11404 }
11405}
11406
11407/* Build a SUPER/THIS qualified method invocation. */
11408
11409static tree
11410build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11411 int use_this;
11412 tree name, args;
11413 int lloc, rloc;
22eed1e6
APB
11414{
11415 tree invok;
11416 tree wfl =
9ee9b555 11417 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
22eed1e6
APB
11418 EXPR_WFL_LINECOL (wfl) = lloc;
11419 invok = build_method_invocation (name, args);
11420 return make_qualified_primary (wfl, invok, rloc);
11421}
11422
b67d701b 11423/* Build an incomplete CALL_EXPR node. */
e04a16fb
AG
11424
11425static tree
11426build_method_invocation (name, args)
11427 tree name;
11428 tree args;
11429{
11430 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11431 TREE_SIDE_EFFECTS (call) = 1;
b67d701b
PB
11432 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11433 return call;
11434}
11435
11436/* Build an incomplete new xxx(...) node. */
11437
11438static tree
11439build_new_invocation (name, args)
11440 tree name, args;
11441{
11442 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11443 TREE_SIDE_EFFECTS (call) = 1;
e04a16fb
AG
11444 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11445 return call;
11446}
11447
11448/* Build an incomplete assignment expression. */
11449
11450static tree
11451build_assignment (op, op_location, lhs, rhs)
11452 int op, op_location;
11453 tree lhs, rhs;
11454{
11455 tree assignment;
11456 /* Build the corresponding binop if we deal with a Compound
11457 Assignment operator. Mark the binop sub-tree as part of a
11458 Compound Assignment expression */
11459 if (op != ASSIGN_TK)
11460 {
11461 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11462 COMPOUND_ASSIGN_P (rhs) = 1;
11463 }
11464 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11465 TREE_SIDE_EFFECTS (assignment) = 1;
11466 EXPR_WFL_LINECOL (assignment) = op_location;
11467 return assignment;
11468}
11469
11470/* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11471
15fdcfe9 11472char *
e04a16fb
AG
11473print_int_node (node)
11474 tree node;
11475{
11476 static char buffer [80];
11477 if (TREE_CONSTANT_OVERFLOW (node))
11478 sprintf (buffer, "<overflow>");
11479
11480 if (TREE_INT_CST_HIGH (node) == 0)
11481 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11482 TREE_INT_CST_LOW (node));
11483 else if (TREE_INT_CST_HIGH (node) == -1
11484 && TREE_INT_CST_LOW (node) != 0)
11485 {
11486 buffer [0] = '-';
11487 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11488 -TREE_INT_CST_LOW (node));
11489 }
11490 else
11491 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11492 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11493
11494 return buffer;
11495}
11496
7f1d4866
APB
11497/* Return 1 if an assignment to a FINAL is attempted in a non suitable
11498 context. */
5e942c50
APB
11499
11500static int
11501check_final_assignment (lvalue, wfl)
11502 tree lvalue, wfl;
11503{
6632dcdd
APB
11504 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11505 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11506 lvalue = TREE_OPERAND (lvalue, 1);
11507
bc2874c9
TT
11508 /* When generating class files, references to the `length' field
11509 look a bit different. */
11510 if ((flag_emit_class_files
11511 && TREE_CODE (lvalue) == COMPONENT_REF
11512 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11513 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11514 || (TREE_CODE (lvalue) == FIELD_DECL
11515 && FIELD_FINAL (lvalue)
11516 && !DECL_CLINIT_P (current_function_decl)
11517 && !DECL_FINIT_P (current_function_decl)))
5e942c50
APB
11518 {
11519 parse_error_context
11520 (wfl, "Can't assign a value to the final variable `%s'",
11521 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11522 return 1;
11523 }
11524 return 0;
11525}
11526
11527/* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11528 read. This is needed to avoid circularities in the implementation
11529 of these fields in libjava. */
11530
11531static tree
11532maybe_build_primttype_type_ref (rhs, wfl)
11533 tree rhs, wfl;
11534{
11535 tree to_return = NULL_TREE;
11536 tree rhs_type = TREE_TYPE (rhs);
11537 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11538 {
11539 tree n = TREE_OPERAND (rhs, 1);
11540 if (TREE_CODE (n) == VAR_DECL
11541 && DECL_NAME (n) == TYPE_identifier_node
11542 && rhs_type == class_ptr_type)
11543 {
49f48c71 11544 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
5e942c50
APB
11545 if (!strncmp (self_name, "java.lang.", 10))
11546 to_return = build_primtype_type_ref (self_name);
11547 }
11548 }
11549 return (to_return ? to_return : rhs );
11550}
11551
e04a16fb
AG
11552/* 15.25 Assignment operators. */
11553
11554static tree
11555patch_assignment (node, wfl_op1, wfl_op2)
11556 tree node;
11557 tree wfl_op1;
11558 tree wfl_op2;
11559{
0a2138e2 11560 tree rhs = TREE_OPERAND (node, 1);
5e942c50 11561 tree lvalue = TREE_OPERAND (node, 0), llvalue;
cd531a2e 11562 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
e04a16fb
AG
11563 int error_found = 0;
11564 int lvalue_from_array = 0;
11565
c2952b01 11566 /* Can't assign to a (blank) final. */
5e942c50
APB
11567 if (check_final_assignment (lvalue, wfl_op1))
11568 error_found = 1;
e04a16fb
AG
11569
11570 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11571
11572 /* Lhs can be a named variable */
34f4db93 11573 if (JDECL_P (lvalue))
e04a16fb 11574 {
e04a16fb
AG
11575 lhs_type = TREE_TYPE (lvalue);
11576 }
11577 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11578 comment on reason why */
11579 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11580 {
11581 lhs_type = TREE_TYPE (lvalue);
11582 lvalue_from_array = 1;
11583 }
11584 /* Or a field access */
11585 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11586 lhs_type = TREE_TYPE (lvalue);
11587 /* Or a function return slot */
11588 else if (TREE_CODE (lvalue) == RESULT_DECL)
11589 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11590 /* Otherwise, we might want to try to write into an optimized static
11591 final, this is an of a different nature, reported further on. */
11592 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
1504b2b4 11593 && resolve_expression_name (wfl_op1, &llvalue))
5e942c50 11594 {
6632dcdd 11595 if (!error_found && check_final_assignment (llvalue, wfl_op1))
1504b2b4
APB
11596 {
11597 /* What we should do instead is resetting the all the flags
11598 previously set, exchange lvalue for llvalue and continue. */
11599 error_found = 1;
11600 return error_mark_node;
11601 }
11602 else
11603 lhs_type = TREE_TYPE (lvalue);
5e942c50
APB
11604 }
11605 else
e04a16fb
AG
11606 {
11607 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11608 error_found = 1;
11609 }
11610
11611 rhs_type = TREE_TYPE (rhs);
b67d701b 11612 /* 5.1 Try the assignment conversion for builtin type. */
0a2138e2 11613 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
e04a16fb 11614
b67d701b 11615 /* 5.2 If it failed, try a reference conversion */
0a2138e2 11616 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
b67d701b 11617 lhs_type = promote_type (rhs_type);
e04a16fb
AG
11618
11619 /* 15.25.2 If we have a compound assignment, convert RHS into the
11620 type of the LHS */
11621 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11622 new_rhs = convert (lhs_type, rhs);
11623
11624 /* Explicit cast required. This is an error */
11625 if (!new_rhs)
11626 {
c2e3db92
KG
11627 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11628 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
e04a16fb
AG
11629 tree wfl;
11630 char operation [32]; /* Max size known */
11631
11632 /* If the assignment is part of a declaration, we use the WFL of
11633 the declared variable to point out the error and call it a
11634 declaration problem. If the assignment is a genuine =
11635 operator, we call is a operator `=' problem, otherwise we
11636 call it an assignment problem. In both of these last cases,
11637 we use the WFL of the operator to indicate the error. */
11638
11639 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11640 {
11641 wfl = wfl_op1;
11642 strcpy (operation, "declaration");
11643 }
11644 else
11645 {
11646 wfl = wfl_operator;
11647 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11648 strcpy (operation, "assignment");
11649 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11650 strcpy (operation, "`return'");
11651 else
11652 strcpy (operation, "`='");
11653 }
11654
1ebadc60 11655 if (!valid_cast_to_p (rhs_type, lhs_type))
781b0558
KG
11656 parse_error_context
11657 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11658 operation, t1, t2);
1ebadc60 11659 else
781b0558 11660 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
1ebadc60 11661 operation, t1, t2);
e04a16fb
AG
11662 free (t1); free (t2);
11663 error_found = 1;
11664 }
11665
c877974e
APB
11666 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11667 if (new_rhs)
11668 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
5e942c50 11669
e04a16fb
AG
11670 if (error_found)
11671 return error_mark_node;
11672
2622b947
APB
11673 /* 10.10: Array Store Exception runtime check */
11674 if (!flag_emit_class_files
e8fc7396 11675 && !flag_emit_xref
2622b947 11676 && lvalue_from_array
afc390b1 11677 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
2622b947
APB
11678 {
11679 tree check;
11680 tree base = lvalue;
11681
11682 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11683 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11684 base = TREE_OPERAND (lvalue, 0);
11685 else
11686 {
11687 if (flag_bounds_check)
11688 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11689 else
11690 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11691 }
11692
11693 /* Build the invocation of _Jv_CheckArrayStore */
dc4e6ccf 11694 new_rhs = save_expr (new_rhs);
2622b947
APB
11695 check = build (CALL_EXPR, void_type_node,
11696 build_address_of (soft_checkarraystore_node),
11697 tree_cons (NULL_TREE, base,
11698 build_tree_list (NULL_TREE, new_rhs)),
11699 NULL_TREE);
11700 TREE_SIDE_EFFECTS (check) = 1;
11701
11702 /* We have to decide on an insertion point */
11703 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11704 {
11705 tree t;
11706 if (flag_bounds_check)
11707 {
11708 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11709 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11710 build (COMPOUND_EXPR, void_type_node, t, check);
11711 }
11712 else
11713 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
11714 check, TREE_OPERAND (lvalue, 1));
11715 }
11716 else
11717 {
11718 /* Make sure the bound check will happen before the store check */
11719 if (flag_bounds_check)
11720 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
11721 build (COMPOUND_EXPR, void_type_node,
11722 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
11723 else
11724 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
11725 }
11726 }
22eed1e6 11727
e04a16fb
AG
11728 TREE_OPERAND (node, 0) = lvalue;
11729 TREE_OPERAND (node, 1) = new_rhs;
11730 TREE_TYPE (node) = lhs_type;
11731 return node;
11732}
11733
b67d701b
PB
11734/* Check that type SOURCE can be cast into type DEST. If the cast
11735 can't occur at all, return 0 otherwise 1. This function is used to
11736 produce accurate error messages on the reasons why an assignment
11737 failed. */
e04a16fb 11738
b67d701b
PB
11739static tree
11740try_reference_assignconv (lhs_type, rhs)
11741 tree lhs_type, rhs;
e04a16fb 11742{
b67d701b
PB
11743 tree new_rhs = NULL_TREE;
11744 tree rhs_type = TREE_TYPE (rhs);
e04a16fb 11745
b67d701b
PB
11746 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
11747 {
11748 /* `null' may be assigned to any reference type */
11749 if (rhs == null_pointer_node)
11750 new_rhs = null_pointer_node;
11751 /* Try the reference assignment conversion */
11752 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
11753 new_rhs = rhs;
11754 /* This is a magic assignment that we process differently */
11755 else if (rhs == soft_exceptioninfo_call_node)
11756 new_rhs = rhs;
11757 }
11758 return new_rhs;
11759}
11760
11761/* Check that RHS can be converted into LHS_TYPE by the assignment
11762 conversion (5.2), for the cases of RHS being a builtin type. Return
11763 NULL_TREE if the conversion fails or if because RHS isn't of a
11764 builtin type. Return a converted RHS if the conversion is possible. */
11765
11766static tree
11767try_builtin_assignconv (wfl_op1, lhs_type, rhs)
11768 tree wfl_op1, lhs_type, rhs;
11769{
11770 tree new_rhs = NULL_TREE;
11771 tree rhs_type = TREE_TYPE (rhs);
11772
5e942c50
APB
11773 /* Zero accepted everywhere */
11774 if (TREE_CODE (rhs) == INTEGER_CST
11775 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
11776 && JPRIMITIVE_TYPE_P (rhs_type))
11777 new_rhs = convert (lhs_type, rhs);
11778
b67d701b
PB
11779 /* 5.1.1 Try Identity Conversion,
11780 5.1.2 Try Widening Primitive Conversion */
5e942c50 11781 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
b67d701b
PB
11782 new_rhs = convert (lhs_type, rhs);
11783
11784 /* Try a narrowing primitive conversion (5.1.3):
11785 - expression is a constant expression of type int AND
11786 - variable is byte, short or char AND
11787 - The value of the expression is representable in the type of the
11788 variable */
11789 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
11790 && (lhs_type == byte_type_node || lhs_type == char_type_node
11791 || lhs_type == short_type_node))
11792 {
11793 if (int_fits_type_p (rhs, lhs_type))
11794 new_rhs = convert (lhs_type, rhs);
11795 else if (wfl_op1) /* Might be called with a NULL */
11796 parse_warning_context
781b0558 11797 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
0a2138e2 11798 print_int_node (rhs), lang_printable_name (lhs_type, 0));
b67d701b
PB
11799 /* Reported a warning that will turn into an error further
11800 down, so we don't return */
11801 }
11802
11803 return new_rhs;
11804}
11805
11806/* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
11807 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
11808 0 is the conversion test fails. This implements parts the method
11809 invocation convertion (5.3). */
11810
11811static int
11812valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
11813 tree lhs_type, rhs_type;
11814{
acd663ee 11815 /* 5.1.1: This is the identity conversion part. */
5e942c50
APB
11816 if (lhs_type == rhs_type)
11817 return 1;
11818
acd663ee
APB
11819 /* Reject non primitive types */
11820 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
b67d701b
PB
11821 return 0;
11822
acd663ee
APB
11823 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
11824 than a char can't be converted into a char. Short can't too, but
11825 the < test below takes care of that */
b67d701b
PB
11826 if (lhs_type == char_type_node && rhs_type == byte_type_node)
11827 return 0;
11828
5e942c50
APB
11829 /* Accept all promoted type here. Note, we can't use <= in the test
11830 below, because we still need to bounce out assignments of short
11831 to char and the likes */
11832 if (lhs_type == int_type_node
11833 && (rhs_type == promoted_byte_type_node
11834 || rhs_type == promoted_short_type_node
11835 || rhs_type == promoted_char_type_node
11836 || rhs_type == promoted_boolean_type_node))
11837 return 1;
11838
acd663ee
APB
11839 /* From here, an integral is widened if its precision is smaller
11840 than the precision of the LHS or if the LHS is a floating point
11841 type, or the RHS is a float and the RHS a double. */
11842 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
11843 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
11844 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
11845 || (rhs_type == float_type_node && lhs_type == double_type_node))
b67d701b
PB
11846 return 1;
11847
11848 return 0;
e04a16fb
AG
11849}
11850
11851/* Check that something of SOURCE type can be assigned or cast to
11852 something of DEST type at runtime. Return 1 if the operation is
11853 valid, 0 otherwise. If CAST is set to 1, we're treating the case
11854 were SOURCE is cast into DEST, which borrows a lot of the
11855 assignment check. */
11856
11857static int
11858valid_ref_assignconv_cast_p (source, dest, cast)
11859 tree source;
11860 tree dest;
11861 int cast;
11862{
09ed0f70
APB
11863 /* SOURCE or DEST might be null if not from a declared entity. */
11864 if (!source || !dest)
11865 return 0;
5e942c50
APB
11866 if (JNULLP_TYPE_P (source))
11867 return 1;
e04a16fb
AG
11868 if (TREE_CODE (source) == POINTER_TYPE)
11869 source = TREE_TYPE (source);
11870 if (TREE_CODE (dest) == POINTER_TYPE)
11871 dest = TREE_TYPE (dest);
11872 /* Case where SOURCE is a class type */
11873 if (TYPE_CLASS_P (source))
11874 {
11875 if (TYPE_CLASS_P (dest))
c2952b01
APB
11876 return (source == dest
11877 || inherits_from_p (source, dest)
11878 || enclosing_context_p (dest, source /*source, dest*/)
11879 || (cast && inherits_from_p (dest, source)));
e04a16fb
AG
11880 if (TYPE_INTERFACE_P (dest))
11881 {
11882 /* If doing a cast and SOURCE is final, the operation is
11883 always correct a compile time (because even if SOURCE
11884 does not implement DEST, a subclass of SOURCE might). */
11885 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
11886 return 1;
11887 /* Otherwise, SOURCE must implement DEST */
11888 return interface_of_p (dest, source);
11889 }
11890 /* DEST is an array, cast permited if SOURCE is of Object type */
11891 return (cast && source == object_type_node ? 1 : 0);
11892 }
11893 if (TYPE_INTERFACE_P (source))
11894 {
11895 if (TYPE_CLASS_P (dest))
11896 {
11897 /* If not casting, DEST must be the Object type */
11898 if (!cast)
11899 return dest == object_type_node;
11900 /* We're doing a cast. The cast is always valid is class
11901 DEST is not final, otherwise, DEST must implement SOURCE */
b67d701b 11902 else if (!CLASS_FINAL (TYPE_NAME (dest)))
e04a16fb
AG
11903 return 1;
11904 else
11905 return interface_of_p (source, dest);
11906 }
11907 if (TYPE_INTERFACE_P (dest))
11908 {
11909 /* If doing a cast, then if SOURCE and DEST contain method
11910 with the same signature but different return type, then
11911 this is a (compile time) error */
11912 if (cast)
11913 {
11914 tree method_source, method_dest;
11915 tree source_type;
0a2138e2 11916 tree source_sig;
e04a16fb
AG
11917 tree source_name;
11918 for (method_source = TYPE_METHODS (source); method_source;
11919 method_source = TREE_CHAIN (method_source))
11920 {
11921 source_sig =
11922 build_java_argument_signature (TREE_TYPE (method_source));
11923 source_type = TREE_TYPE (TREE_TYPE (method_source));
11924 source_name = DECL_NAME (method_source);
11925 for (method_dest = TYPE_METHODS (dest);
11926 method_dest; method_dest = TREE_CHAIN (method_dest))
11927 if (source_sig ==
11928 build_java_argument_signature (TREE_TYPE (method_dest))
11929 && source_name == DECL_NAME (method_dest)
11930 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
11931 return 0;
11932 }
11933 return 1;
11934 }
11935 else
11936 return source == dest || interface_of_p (dest, source);
11937 }
11938 else /* Array */
93024893
APB
11939 return (cast ?
11940 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
e04a16fb
AG
11941 }
11942 if (TYPE_ARRAY_P (source))
11943 {
11944 if (TYPE_CLASS_P (dest))
11945 return dest == object_type_node;
09ed0f70
APB
11946 /* Can't cast an array to an interface unless the interface is
11947 java.lang.Cloneable */
e04a16fb 11948 if (TYPE_INTERFACE_P (dest))
09ed0f70 11949 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
e04a16fb
AG
11950 else /* Arrays */
11951 {
11952 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
11953 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
11954
b9f7e36c
APB
11955 /* In case of severe errors, they turn out null */
11956 if (!dest_element_type || !source_element_type)
11957 return 0;
e04a16fb
AG
11958 if (source_element_type == dest_element_type)
11959 return 1;
11960 return valid_ref_assignconv_cast_p (source_element_type,
11961 dest_element_type, cast);
11962 }
11963 return 0;
11964 }
11965 return 0;
11966}
11967
b67d701b
PB
11968static int
11969valid_cast_to_p (source, dest)
11970 tree source;
11971 tree dest;
11972{
11973 if (TREE_CODE (source) == POINTER_TYPE)
11974 source = TREE_TYPE (source);
11975 if (TREE_CODE (dest) == POINTER_TYPE)
11976 dest = TREE_TYPE (dest);
11977
11978 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
11979 return valid_ref_assignconv_cast_p (source, dest, 1);
11980
11981 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
11982 return 1;
11983
11984 return 0;
11985}
11986
11987/* Method invocation conversion test. Return 1 if type SOURCE can be
11988 converted to type DEST through the methond invocation conversion
11989 process (5.3) */
11990
15fdcfe9
PB
11991static tree
11992do_unary_numeric_promotion (arg)
11993 tree arg;
11994{
11995 tree type = TREE_TYPE (arg);
11996 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
11997 : TREE_CODE (type) == CHAR_TYPE)
11998 arg = convert (int_type_node, arg);
11999 return arg;
12000}
12001
acd663ee
APB
12002/* Return a non zero value if SOURCE can be converted into DEST using
12003 the method invocation conversion rule (5.3). */
b67d701b
PB
12004static int
12005valid_method_invocation_conversion_p (dest, source)
12006 tree dest, source;
12007{
e3884b71 12008 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
acd663ee
APB
12009 && valid_builtin_assignconv_identity_widening_p (dest, source))
12010 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12011 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12012 && valid_ref_assignconv_cast_p (source, dest, 0)));
b67d701b
PB
12013}
12014
e04a16fb
AG
12015/* Build an incomplete binop expression. */
12016
12017static tree
12018build_binop (op, op_location, op1, op2)
12019 enum tree_code op;
12020 int op_location;
12021 tree op1, op2;
12022{
5e942c50 12023 tree binop = build (op, NULL_TREE, op1, op2);
e04a16fb
AG
12024 TREE_SIDE_EFFECTS (binop) = 1;
12025 /* Store the location of the operator, for better error report. The
12026 string of the operator will be rebuild based on the OP value. */
12027 EXPR_WFL_LINECOL (binop) = op_location;
12028 return binop;
12029}
12030
12031/* Build the string of the operator retained by NODE. If NODE is part
12032 of a compound expression, add an '=' at the end of the string. This
12033 function is called when an error needs to be reported on an
12034 operator. The string is returned as a pointer to a static character
12035 buffer. */
12036
12037static char *
12038operator_string (node)
12039 tree node;
12040{
12041#define BUILD_OPERATOR_STRING(S) \
12042 { \
12043 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12044 return buffer; \
12045 }
12046
12047 static char buffer [10];
12048 switch (TREE_CODE (node))
12049 {
12050 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12051 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12052 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12053 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12054 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12055 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12056 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12057 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12058 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12059 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12060 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12061 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12062 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12063 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12064 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12065 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12066 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12067 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12068 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
b67d701b 12069 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
e04a16fb
AG
12070 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12071 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12072 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12073 case PREINCREMENT_EXPR: /* Fall through */
12074 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12075 case PREDECREMENT_EXPR: /* Fall through */
12076 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12077 default:
12078 fatal ("unregistered operator %s - operator_string",
12079 tree_code_name [TREE_CODE (node)]);
12080 }
12081 return NULL;
12082#undef BUILD_OPERATOR_STRING
12083}
12084
5cbdba64
APB
12085/* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12086
12087static int
12088java_decl_equiv (var_acc1, var_acc2)
12089 tree var_acc1, var_acc2;
12090{
12091 if (JDECL_P (var_acc1))
12092 return (var_acc1 == var_acc2);
12093
12094 return (TREE_CODE (var_acc1) == COMPONENT_REF
12095 && TREE_CODE (var_acc2) == COMPONENT_REF
12096 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12097 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12098 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12099}
12100
12101/* Return a non zero value if CODE is one of the operators that can be
12102 used in conjunction with the `=' operator in a compound assignment. */
12103
12104static int
12105binop_compound_p (code)
12106 enum tree_code code;
12107{
12108 int i;
12109 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12110 if (binop_lookup [i] == code)
12111 break;
12112
12113 return i < BINOP_COMPOUND_CANDIDATES;
12114}
12115
12116/* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12117
12118static tree
12119java_refold (t)
12120 tree t;
12121{
12122 tree c, b, ns, decl;
12123
12124 if (TREE_CODE (t) != MODIFY_EXPR)
12125 return t;
12126
12127 c = TREE_OPERAND (t, 1);
12128 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12129 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12130 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12131 return t;
12132
12133 /* Now the left branch of the binary operator. */
12134 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12135 if (! (b && TREE_CODE (b) == NOP_EXPR
12136 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12137 return t;
12138
12139 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12140 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12141 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12142 return t;
12143
12144 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12145 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12146 /* It's got to be the an equivalent decl */
12147 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12148 {
12149 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12150 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12151 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12152 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12153 /* Change the right part of the BINOP_EXPR */
12154 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12155 }
12156
12157 return t;
12158}
12159
e04a16fb
AG
12160/* Binary operators (15.16 up to 15.18). We return error_mark_node on
12161 errors but we modify NODE so that it contains the type computed
12162 according to the expression, when it's fixed. Otherwise, we write
12163 error_mark_node as the type. It allows us to further the analysis
12164 of remaining nodes and detects more errors in certain cases. */
12165
12166static tree
12167patch_binop (node, wfl_op1, wfl_op2)
12168 tree node;
12169 tree wfl_op1;
12170 tree wfl_op2;
12171{
12172 tree op1 = TREE_OPERAND (node, 0);
12173 tree op2 = TREE_OPERAND (node, 1);
12174 tree op1_type = TREE_TYPE (op1);
12175 tree op2_type = TREE_TYPE (op2);
48a840d9 12176 tree prom_type = NULL_TREE, cn;
e04a16fb 12177 int code = TREE_CODE (node);
b67d701b 12178
e04a16fb
AG
12179 /* If 1, tell the routine that we have to return error_mark_node
12180 after checking for the initialization of the RHS */
12181 int error_found = 0;
12182
e04a16fb
AG
12183 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12184
e04a16fb
AG
12185 switch (code)
12186 {
12187 /* 15.16 Multiplicative operators */
12188 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12189 case RDIV_EXPR: /* 15.16.2 Division Operator / */
c2952b01 12190 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
e04a16fb
AG
12191 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12192 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12193 {
12194 if (!JPRIMITIVE_TYPE_P (op1_type))
12195 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12196 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12197 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12198 TREE_TYPE (node) = error_mark_node;
12199 error_found = 1;
12200 break;
12201 }
12202 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12203 /* Change the division operator if necessary */
12204 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12205 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
0b4d333e 12206
aa4759c1
AH
12207 if (TREE_CODE (prom_type) == INTEGER_TYPE
12208 && flag_use_divide_subroutine
12209 && ! flag_emit_class_files
12210 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12211 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12212
0b4d333e
APB
12213 /* This one is more complicated. FLOATs are processed by a
12214 function call to soft_fmod. Duplicate the value of the
12215 COMPOUND_ASSIGN_P flag. */
e04a16fb 12216 if (code == TRUNC_MOD_EXPR)
0b4d333e
APB
12217 {
12218 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12219 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12220 TREE_SIDE_EFFECTS (mod)
12221 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e
APB
12222 return mod;
12223 }
e04a16fb
AG
12224 break;
12225
12226 /* 15.17 Additive Operators */
12227 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
b67d701b
PB
12228
12229 /* Operation is valid if either one argument is a string
12230 constant, a String object or a StringBuffer crafted for the
12231 purpose of the a previous usage of the String concatenation
12232 operator */
12233
12234 if (TREE_CODE (op1) == STRING_CST
12235 || TREE_CODE (op2) == STRING_CST
12236 || JSTRING_TYPE_P (op1_type)
12237 || JSTRING_TYPE_P (op2_type)
12238 || IS_CRAFTED_STRING_BUFFER_P (op1)
12239 || IS_CRAFTED_STRING_BUFFER_P (op2))
12240 return build_string_concatenation (op1, op2);
12241
e04a16fb
AG
12242 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12243 Numeric Types */
12244 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12245 {
12246 if (!JPRIMITIVE_TYPE_P (op1_type))
12247 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12248 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12249 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12250 TREE_TYPE (node) = error_mark_node;
12251 error_found = 1;
12252 break;
12253 }
12254 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12255 break;
12256
12257 /* 15.18 Shift Operators */
12258 case LSHIFT_EXPR:
12259 case RSHIFT_EXPR:
12260 case URSHIFT_EXPR:
12261 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12262 {
12263 if (!JINTEGRAL_TYPE_P (op1_type))
12264 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12265 else
1ebadc60
KG
12266 {
12267 if (JPRIMITIVE_TYPE_P (op2_type))
12268 parse_error_context (wfl_operator,
781b0558 12269 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
1ebadc60
KG
12270 operator_string (node),
12271 lang_printable_name (op2_type, 0));
12272 else
781b0558
KG
12273 parse_error_context (wfl_operator,
12274 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
1ebadc60
KG
12275 operator_string (node),
12276 lang_printable_name (op2_type, 0));
12277 }
e04a16fb
AG
12278 TREE_TYPE (node) = error_mark_node;
12279 error_found = 1;
12280 break;
12281 }
12282
12283 /* Unary numeric promotion (5.6.1) is performed on each operand
12284 separatly */
15fdcfe9
PB
12285 op1 = do_unary_numeric_promotion (op1);
12286 op2 = do_unary_numeric_promotion (op2);
e04a16fb
AG
12287
12288 /* The type of the shift expression is the type of the promoted
12289 type of the left-hand operand */
12290 prom_type = TREE_TYPE (op1);
12291
c2952b01
APB
12292 /* Shift int only up to 0x1f and long up to 0x3f */
12293 if (prom_type == int_type_node)
12294 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12295 build_int_2 (0x1f, 0)));
12296 else
12297 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12298 build_int_2 (0x3f, 0)));
e04a16fb
AG
12299
12300 /* The >>> operator is a >> operating on unsigned quantities */
15fdcfe9 12301 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
e04a16fb 12302 {
0b4d333e 12303 tree to_return;
73333a87
AH
12304 tree utype = unsigned_type (prom_type);
12305 op1 = convert (utype, op1);
e04a16fb 12306 TREE_SET_CODE (node, RSHIFT_EXPR);
73333a87
AH
12307 TREE_OPERAND (node, 0) = op1;
12308 TREE_OPERAND (node, 1) = op2;
12309 TREE_TYPE (node) = utype;
0b4d333e
APB
12310 to_return = convert (prom_type, node);
12311 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12312 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
dc0b3eff
PB
12313 TREE_SIDE_EFFECTS (to_return)
12314 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
0b4d333e 12315 return to_return;
e04a16fb
AG
12316 }
12317 break;
5e942c50
APB
12318
12319 /* 15.19.1 Type Comparison Operator instaceof */
12320 case INSTANCEOF_EXPR:
12321
12322 TREE_TYPE (node) = boolean_type_node;
12323
12324 if (!(op2_type = resolve_type_during_patch (op2)))
12325 return error_mark_node;
12326
12327 /* The first operand must be a reference type or the null type */
12328 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12329 error_found = 1; /* Error reported further below */
12330
12331 /* The second operand must be a reference type */
12332 if (!JREFERENCE_TYPE_P (op2_type))
12333 {
12334 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12335 parse_error_context
12336 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12337 lang_printable_name (op2_type, 0));
12338 error_found = 1;
12339 }
12340
12341 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12342 {
12343 /* If the first operand is null, the result is always false */
12344 if (op1 == null_pointer_node)
12345 return boolean_false_node;
15fdcfe9
PB
12346 else if (flag_emit_class_files)
12347 {
12348 TREE_OPERAND (node, 1) = op2_type;
dc0b3eff 12349 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
15fdcfe9
PB
12350 return node;
12351 }
5e942c50
APB
12352 /* Otherwise we have to invoke instance of to figure it out */
12353 else
67db0ce7 12354 return build_instanceof (op1, op2_type);
5e942c50
APB
12355 }
12356 /* There is no way the expression operand can be an instance of
12357 the type operand. This is a compile time error. */
12358 else
12359 {
c2e3db92 12360 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
5e942c50
APB
12361 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12362 parse_error_context
12363 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12364 t1, lang_printable_name (op2_type, 0));
12365 free (t1);
12366 error_found = 1;
12367 }
e04a16fb 12368
5e942c50 12369 break;
e04a16fb
AG
12370
12371 /* 15.21 Bitwise and Logical Operators */
12372 case BIT_AND_EXPR:
12373 case BIT_XOR_EXPR:
12374 case BIT_IOR_EXPR:
12375 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12376 /* Binary numeric promotion is performed on both operand and the
12377 expression retain that type */
12378 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12379
12380 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12381 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12382 /* The type of the bitwise operator expression is BOOLEAN */
12383 prom_type = boolean_type_node;
12384 else
12385 {
12386 if (!JINTEGRAL_TYPE_P (op1_type))
12387 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12388 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12389 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12390 TREE_TYPE (node) = error_mark_node;
12391 error_found = 1;
12392 /* Insert a break here if adding thing before the switch's
12393 break for this case */
12394 }
12395 break;
12396
12397 /* 15.22 Conditional-And Operator */
12398 case TRUTH_ANDIF_EXPR:
12399 /* 15.23 Conditional-Or Operator */
12400 case TRUTH_ORIF_EXPR:
12401 /* Operands must be of BOOLEAN type */
12402 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12403 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12404 {
12405 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12406 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12407 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12408 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12409 TREE_TYPE (node) = boolean_type_node;
12410 error_found = 1;
12411 break;
12412 }
12413 /* The type of the conditional operators is BOOLEAN */
12414 prom_type = boolean_type_node;
12415 break;
12416
12417 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12418 case LT_EXPR:
12419 case GT_EXPR:
12420 case LE_EXPR:
12421 case GE_EXPR:
12422 /* The type of each of the operands must be a primitive numeric
12423 type */
12424 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12425 {
12426 if (!JNUMERIC_TYPE_P (op1_type))
12427 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12428 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12429 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12430 TREE_TYPE (node) = boolean_type_node;
12431 error_found = 1;
12432 break;
12433 }
12434 /* Binary numeric promotion is performed on the operands */
12435 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12436 /* The type of the relation expression is always BOOLEAN */
12437 prom_type = boolean_type_node;
12438 break;
12439
12440 /* 15.20 Equality Operator */
12441 case EQ_EXPR:
12442 case NE_EXPR:
48a840d9
APB
12443 /* It's time for us to patch the strings. */
12444 if ((cn = patch_string (op1)))
12445 {
12446 op1 = cn;
12447 op1_type = TREE_TYPE (op1);
12448 }
12449 if ((cn = patch_string (op2)))
12450 {
12451 op2 = cn;
12452 op2_type = TREE_TYPE (op2);
12453 }
12454
e04a16fb
AG
12455 /* 15.20.1 Numerical Equality Operators == and != */
12456 /* Binary numeric promotion is performed on the operands */
5e942c50 12457 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
e04a16fb
AG
12458 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12459
12460 /* 15.20.2 Boolean Equality Operators == and != */
12461 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12462 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12463 ; /* Nothing to do here */
12464
12465 /* 15.20.3 Reference Equality Operators == and != */
5e942c50
APB
12466 /* Types have to be either references or the null type. If
12467 they're references, it must be possible to convert either
12468 type to the other by casting conversion. */
b9f7e36c
APB
12469 else if (op1 == null_pointer_node || op2 == null_pointer_node
12470 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
5e942c50
APB
12471 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12472 || valid_ref_assignconv_cast_p (op2_type,
12473 op1_type, 1))))
e04a16fb
AG
12474 ; /* Nothing to do here */
12475
12476 /* Else we have an error figure what can't be converted into
12477 what and report the error */
12478 else
12479 {
12480 char *t1;
c2e3db92 12481 t1 = xstrdup (lang_printable_name (op1_type, 0));
e04a16fb 12482 parse_error_context
781b0558
KG
12483 (wfl_operator,
12484 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12485 operator_string (node), t1,
0a2138e2 12486 lang_printable_name (op2_type, 0));
e04a16fb
AG
12487 free (t1);
12488 TREE_TYPE (node) = boolean_type_node;
12489 error_found = 1;
12490 break;
12491 }
12492 prom_type = boolean_type_node;
12493 break;
12494 }
12495
e04a16fb
AG
12496 if (error_found)
12497 return error_mark_node;
12498
12499 TREE_OPERAND (node, 0) = op1;
12500 TREE_OPERAND (node, 1) = op2;
12501 TREE_TYPE (node) = prom_type;
dc0b3eff
PB
12502 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12503
ce6e9147
APB
12504 if (flag_emit_xref)
12505 return node;
12506
d1472141
PB
12507 /* fold does not respect side-effect order as required for Java but not C.
12508 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12509 * bytecode.
12510 */
12511 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12512 : ! TREE_SIDE_EFFECTS (node))
aee48ef8
PB
12513 node = fold (node);
12514 return node;
e04a16fb
AG
12515}
12516
b67d701b
PB
12517/* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12518 zero value, the value of CSTE comes after the valude of STRING */
12519
12520static tree
12521do_merge_string_cste (cste, string, string_len, after)
12522 tree cste;
49f48c71 12523 const char *string;
b67d701b
PB
12524 int string_len, after;
12525{
12526 int len = TREE_STRING_LENGTH (cste) + string_len;
49f48c71 12527 const char *old = TREE_STRING_POINTER (cste);
b67d701b
PB
12528 TREE_STRING_LENGTH (cste) = len;
12529 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12530 if (after)
12531 {
12532 strcpy (TREE_STRING_POINTER (cste), string);
12533 strcat (TREE_STRING_POINTER (cste), old);
12534 }
12535 else
12536 {
12537 strcpy (TREE_STRING_POINTER (cste), old);
12538 strcat (TREE_STRING_POINTER (cste), string);
12539 }
12540 return cste;
12541}
12542
12543/* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12544 new STRING_CST on success, NULL_TREE on failure */
12545
12546static tree
12547merge_string_cste (op1, op2, after)
12548 tree op1, op2;
12549 int after;
12550{
12551 /* Handle two string constants right away */
12552 if (TREE_CODE (op2) == STRING_CST)
12553 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12554 TREE_STRING_LENGTH (op2), after);
12555
12556 /* Reasonable integer constant can be treated right away */
12557 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12558 {
49f48c71
KG
12559 static const char *boolean_true = "true";
12560 static const char *boolean_false = "false";
12561 static const char *null_pointer = "null";
b67d701b 12562 char ch[3];
49f48c71 12563 const char *string;
b67d701b
PB
12564
12565 if (op2 == boolean_true_node)
12566 string = boolean_true;
12567 else if (op2 == boolean_false_node)
12568 string = boolean_false;
12569 else if (op2 == null_pointer_node)
12570 string = null_pointer;
12571 else if (TREE_TYPE (op2) == char_type_node)
12572 {
12573 ch[0] = (char )TREE_INT_CST_LOW (op2);
12574 ch[1] = '\0';
12575 string = ch;
12576 }
12577 else
12578 string = print_int_node (op2);
12579
12580 return do_merge_string_cste (op1, string, strlen (string), after);
12581 }
12582 return NULL_TREE;
12583}
12584
12585/* Tries to statically concatenate OP1 and OP2 if possible. Either one
12586 has to be a STRING_CST and the other part must be a STRING_CST or a
12587 INTEGRAL constant. Return a new STRING_CST if the operation
12588 succeed, NULL_TREE otherwise.
12589
12590 If the case we want to optimize for space, we might want to return
12591 NULL_TREE for each invocation of this routine. FIXME */
12592
12593static tree
12594string_constant_concatenation (op1, op2)
12595 tree op1, op2;
12596{
12597 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12598 {
0a2138e2 12599 tree string, rest;
b67d701b
PB
12600 int invert;
12601
12602 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12603 rest = (string == op1 ? op2 : op1);
12604 invert = (string == op1 ? 0 : 1 );
12605
12606 /* Walk REST, only if it looks reasonable */
12607 if (TREE_CODE (rest) != STRING_CST
12608 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12609 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12610 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12611 {
12612 rest = java_complete_tree (rest);
12613 if (rest == error_mark_node)
12614 return error_mark_node;
12615 rest = fold (rest);
12616 }
12617 return merge_string_cste (string, rest, invert);
12618 }
12619 return NULL_TREE;
12620}
12621
12622/* Implement the `+' operator. Does static optimization if possible,
12623 otherwise create (if necessary) and append elements to a
12624 StringBuffer. The StringBuffer will be carried around until it is
12625 used for a function call or an assignment. Then toString() will be
12626 called on it to turn it into a String object. */
12627
12628static tree
12629build_string_concatenation (op1, op2)
12630 tree op1, op2;
12631{
12632 tree result;
dc0b3eff 12633 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
ce6e9147
APB
12634
12635 if (flag_emit_xref)
12636 return build (PLUS_EXPR, string_type_node, op1, op2);
b67d701b
PB
12637
12638 /* Try to do some static optimization */
12639 if ((result = string_constant_concatenation (op1, op2)))
12640 return result;
12641
c0d87ff6
PB
12642 /* Discard empty strings on either side of the expression */
12643 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
acd663ee
APB
12644 {
12645 op1 = op2;
12646 op2 = NULL_TREE;
12647 }
c0d87ff6 12648 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
acd663ee 12649 op2 = NULL_TREE;
b67d701b 12650
acd663ee 12651 /* If operands are string constant, turn then into object references */
b67d701b
PB
12652 if (TREE_CODE (op1) == STRING_CST)
12653 op1 = patch_string_cst (op1);
acd663ee 12654 if (op2 && TREE_CODE (op2) == STRING_CST)
b67d701b
PB
12655 op2 = patch_string_cst (op2);
12656
acd663ee
APB
12657 /* If either one of the constant is null and the other non null
12658 operand is a String object, return it. */
12659 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12660 return op1;
12661
b67d701b
PB
12662 /* If OP1 isn't already a StringBuffer, create and
12663 initialize a new one */
12664 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12665 {
12666 /* Two solutions here:
c52b5771
AG
12667 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12668 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12669 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
b67d701b
PB
12670 op1 = BUILD_STRING_BUFFER (op1);
12671 else
12672 {
12673 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12674 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12675 }
12676 }
12677
acd663ee
APB
12678 if (op2)
12679 {
12680 /* OP1 is no longer the last node holding a crafted StringBuffer */
12681 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12682 /* Create a node for `{new...,xxx}.append (op2)' */
12683 if (op2)
12684 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12685 }
12686
b67d701b
PB
12687 /* Mark the last node holding a crafted StringBuffer */
12688 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
dc0b3eff
PB
12689
12690 TREE_SIDE_EFFECTS (op1) = side_effects;
b67d701b
PB
12691 return op1;
12692}
12693
12694/* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12695 StringBuffer. If no string were found to be patched, return
12696 NULL. */
12697
12698static tree
12699patch_string (node)
12700 tree node;
12701{
1179ebc2
APB
12702 if (node == error_mark_node)
12703 return error_mark_node;
b67d701b
PB
12704 if (TREE_CODE (node) == STRING_CST)
12705 return patch_string_cst (node);
12706 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12707 {
c877974e 12708 int saved = ctxp->explicit_constructor_p;
b67d701b 12709 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
c877974e
APB
12710 tree ret;
12711 /* Temporary disable forbid the use of `this'. */
12712 ctxp->explicit_constructor_p = 0;
12713 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
1729c265
APB
12714 /* String concatenation arguments must be evaluated in order too. */
12715 ret = force_evaluation_order (ret);
c877974e
APB
12716 /* Restore it at its previous value */
12717 ctxp->explicit_constructor_p = saved;
12718 return ret;
b67d701b
PB
12719 }
12720 return NULL_TREE;
12721}
12722
12723/* Build the internal representation of a string constant. */
12724
12725static tree
12726patch_string_cst (node)
12727 tree node;
12728{
12729 int location;
15fdcfe9
PB
12730 if (! flag_emit_class_files)
12731 {
12732 push_obstacks (&permanent_obstack, &permanent_obstack);
12733 node = get_identifier (TREE_STRING_POINTER (node));
12734 location = alloc_name_constant (CONSTANT_String, node);
12735 node = build_ref_from_constant_pool (location);
8226320b 12736 pop_obstacks ();
15fdcfe9 12737 }
cd9643f7 12738 TREE_TYPE (node) = string_ptr_type_node;
b67d701b
PB
12739 TREE_CONSTANT (node) = 1;
12740 return node;
12741}
12742
12743/* Build an incomplete unary operator expression. */
e04a16fb
AG
12744
12745static tree
12746build_unaryop (op_token, op_location, op1)
12747 int op_token, op_location;
12748 tree op1;
12749{
12750 enum tree_code op;
12751 tree unaryop;
12752 switch (op_token)
12753 {
b67d701b 12754 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
e04a16fb
AG
12755 case MINUS_TK: op = NEGATE_EXPR; break;
12756 case NEG_TK: op = TRUTH_NOT_EXPR; break;
12757 case NOT_TK: op = BIT_NOT_EXPR; break;
12758 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
12759 op_token);
12760 }
12761
12762 unaryop = build1 (op, NULL_TREE, op1);
e04a16fb
AG
12763 TREE_SIDE_EFFECTS (unaryop) = 1;
12764 /* Store the location of the operator, for better error report. The
12765 string of the operator will be rebuild based on the OP value. */
12766 EXPR_WFL_LINECOL (unaryop) = op_location;
12767 return unaryop;
12768}
12769
12770/* Special case for the ++/-- operators, since they require an extra
12771 argument to build, which is set to NULL and patched
12772 later. IS_POST_P is 1 if the operator, 0 otherwise. */
12773
12774static tree
12775build_incdec (op_token, op_location, op1, is_post_p)
12776 int op_token, op_location;
12777 tree op1;
12778 int is_post_p;
12779{
12780 static enum tree_code lookup [2][2] =
12781 {
12782 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
12783 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
12784 };
12785 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
12786 NULL_TREE, op1, NULL_TREE);
12787 TREE_SIDE_EFFECTS (node) = 1;
12788 /* Store the location of the operator, for better error report. The
12789 string of the operator will be rebuild based on the OP value. */
12790 EXPR_WFL_LINECOL (node) = op_location;
12791 return node;
12792}
12793
12794/* Build an incomplete cast operator, based on the use of the
12795 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
12796 set. java_complete_tree is trained to walk a CONVERT_EXPR even
12797 though its type is already set. */
12798
12799static tree
12800build_cast (location, type, exp)
12801 int location;
12802 tree type, exp;
12803{
12804 tree node = build1 (CONVERT_EXPR, type, exp);
12805 EXPR_WFL_LINECOL (node) = location;
12806 return node;
12807}
12808
c2952b01
APB
12809/* Build an incomplete class reference operator. */
12810static tree
12811build_incomplete_class_ref (location, class_name)
12812 int location;
12813 tree class_name;
12814{
12815 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
12816 EXPR_WFL_LINECOL (node) = location;
12817 return node;
12818}
12819
12820/* Complete an incomplete class reference operator. */
12821static tree
12822patch_incomplete_class_ref (node)
12823 tree node;
12824{
12825 tree type = TREE_OPERAND (node, 0);
12826 tree ref_type;
12827
12828 if (!(ref_type = resolve_type_during_patch (type)))
12829 return error_mark_node;
12830
165f37bc
APB
12831 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
12832 return build_class_ref (ref_type);
12833
12834 /* If we're emitting class files and we have to deal with non
12835 primitive types, we invoke (and consider generating) the
12836 synthetic static method `class$'. */
12837 if (!TYPE_DOT_CLASS (current_class))
12838 build_dot_class_method (current_class);
12839 ref_type =
12840 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
12841 return java_complete_tree (ref_type);
c2952b01
APB
12842}
12843
e04a16fb
AG
12844/* 15.14 Unary operators. We return error_mark_node in case of error,
12845 but preserve the type of NODE if the type is fixed. */
12846
12847static tree
12848patch_unaryop (node, wfl_op)
12849 tree node;
12850 tree wfl_op;
12851{
12852 tree op = TREE_OPERAND (node, 0);
12853 tree op_type = TREE_TYPE (op);
ab3a6dd6 12854 tree prom_type = NULL_TREE, value, decl;
c2952b01 12855 int outer_field_flag = 0;
e04a16fb
AG
12856 int code = TREE_CODE (node);
12857 int error_found = 0;
12858
12859 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12860
12861 switch (code)
12862 {
12863 /* 15.13.2 Postfix Increment Operator ++ */
12864 case POSTINCREMENT_EXPR:
12865 /* 15.13.3 Postfix Increment Operator -- */
12866 case POSTDECREMENT_EXPR:
12867 /* 15.14.1 Prefix Increment Operator ++ */
12868 case PREINCREMENT_EXPR:
12869 /* 15.14.2 Prefix Decrement Operator -- */
12870 case PREDECREMENT_EXPR:
5cbdba64 12871 op = decl = strip_out_static_field_access_decl (op);
c2952b01
APB
12872 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
12873 /* We might be trying to change an outer field accessed using
12874 access method. */
12875 if (outer_field_flag)
12876 {
12877 /* Retrieve the decl of the field we're trying to access. We
12878 do that by first retrieving the function we would call to
12879 access the field. It has been already verified that this
12880 field isn't final */
12881 if (flag_emit_class_files)
12882 decl = TREE_OPERAND (op, 0);
12883 else
12884 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
12885 decl = DECL_FUNCTION_ACCESS_DECL (decl);
12886 }
b3edebcf 12887 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
c2952b01 12888 else if (!JDECL_P (decl)
b3edebcf
APB
12889 && TREE_CODE (decl) != COMPONENT_REF
12890 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
12891 && TREE_CODE (decl) != INDIRECT_REF
12892 && !(TREE_CODE (decl) == COMPOUND_EXPR
12893 && TREE_OPERAND (decl, 1)
12894 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
e04a16fb 12895 {
5e942c50
APB
12896 tree lvalue;
12897 /* Before screaming, check that we're not in fact trying to
12898 increment a optimized static final access, in which case
12899 we issue an different error message. */
12900 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
12901 && resolve_expression_name (wfl_op, &lvalue)
12902 && check_final_assignment (lvalue, wfl_op)))
12903 parse_error_context (wfl_operator, "Invalid argument to `%s'",
12904 operator_string (node));
e04a16fb
AG
12905 TREE_TYPE (node) = error_mark_node;
12906 error_found = 1;
12907 }
c2952b01
APB
12908
12909 if (check_final_assignment (op, wfl_op))
5e942c50
APB
12910 error_found = 1;
12911
e04a16fb
AG
12912 /* From now on, we know that op if a variable and that it has a
12913 valid wfl. We use wfl_op to locate errors related to the
12914 ++/-- operand. */
12915 else if (!JNUMERIC_TYPE_P (op_type))
12916 {
12917 parse_error_context
12918 (wfl_op, "Invalid argument type `%s' to `%s'",
0a2138e2 12919 lang_printable_name (op_type, 0), operator_string (node));
e04a16fb
AG
12920 TREE_TYPE (node) = error_mark_node;
12921 error_found = 1;
12922 }
12923 else
12924 {
4a5f66c3 12925 /* Before the addition, binary numeric promotion is performed on
5cbdba64
APB
12926 both operands, if really necessary */
12927 if (JINTEGRAL_TYPE_P (op_type))
12928 {
12929 value = build_int_2 (1, 0);
12930 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
12931 }
12932 else
12933 {
12934 value = build_int_2 (1, 0);
12935 TREE_TYPE (node) =
12936 binary_numeric_promotion (op_type,
12937 TREE_TYPE (value), &op, &value);
12938 }
c2952b01
APB
12939
12940 /* We remember we might be accessing an outer field */
12941 if (outer_field_flag)
12942 {
12943 /* We re-generate an access to the field */
12944 value = build (PLUS_EXPR, TREE_TYPE (op),
12945 build_outer_field_access (wfl_op, decl), value);
12946
12947 /* And we patch the original access$() into a write
12948 with plus_op as a rhs */
12949 return outer_field_access_fix (node, op, value);
12950 }
12951
5cbdba64 12952 /* And write back into the node. */
4a5f66c3 12953 TREE_OPERAND (node, 0) = op;
e04a16fb 12954 TREE_OPERAND (node, 1) = value;
5cbdba64
APB
12955 /* Convert the overall back into its original type, if
12956 necessary, and return */
12957 if (JINTEGRAL_TYPE_P (op_type))
12958 return fold (node);
12959 else
12960 return fold (convert (op_type, node));
e04a16fb
AG
12961 }
12962 break;
12963
12964 /* 15.14.3 Unary Plus Operator + */
b67d701b 12965 case UNARY_PLUS_EXPR:
e04a16fb
AG
12966 /* 15.14.4 Unary Minus Operator - */
12967 case NEGATE_EXPR:
12968 if (!JNUMERIC_TYPE_P (op_type))
12969 {
12970 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
12971 TREE_TYPE (node) = error_mark_node;
12972 error_found = 1;
12973 }
12974 /* Unary numeric promotion is performed on operand */
12975 else
12976 {
15fdcfe9
PB
12977 op = do_unary_numeric_promotion (op);
12978 prom_type = TREE_TYPE (op);
b67d701b 12979 if (code == UNARY_PLUS_EXPR)
4a5f66c3 12980 return fold (op);
e04a16fb
AG
12981 }
12982 break;
12983
12984 /* 15.14.5 Bitwise Complement Operator ~ */
12985 case BIT_NOT_EXPR:
12986 if (!JINTEGRAL_TYPE_P (op_type))
12987 {
12988 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
12989 TREE_TYPE (node) = error_mark_node;
12990 error_found = 1;
12991 }
12992 else
12993 {
15fdcfe9
PB
12994 op = do_unary_numeric_promotion (op);
12995 prom_type = TREE_TYPE (op);
e04a16fb
AG
12996 }
12997 break;
12998
12999 /* 15.14.6 Logical Complement Operator ! */
13000 case TRUTH_NOT_EXPR:
13001 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13002 {
13003 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
c877974e
APB
13004 /* But the type is known. We will report an error if further
13005 attempt of a assignment is made with this rhs */
e04a16fb
AG
13006 TREE_TYPE (node) = boolean_type_node;
13007 error_found = 1;
13008 }
13009 else
13010 prom_type = boolean_type_node;
13011 break;
13012
13013 /* 15.15 Cast Expression */
13014 case CONVERT_EXPR:
0a2138e2 13015 value = patch_cast (node, wfl_operator);
e04a16fb 13016 if (value == error_mark_node)
c877974e
APB
13017 {
13018 /* If this cast is part of an assignment, we tell the code
13019 that deals with it not to complain about a mismatch,
13020 because things have been cast, anyways */
13021 TREE_TYPE (node) = error_mark_node;
13022 error_found = 1;
13023 }
13024 else
dc0b3eff
PB
13025 {
13026 value = fold (value);
13027 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13028 return value;
13029 }
e04a16fb
AG
13030 break;
13031 }
13032
e04a16fb
AG
13033 if (error_found)
13034 return error_mark_node;
4a5f66c3
APB
13035
13036 /* There are cases where node has been replaced by something else
13037 and we don't end up returning here: UNARY_PLUS_EXPR,
13038 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
7525cc04 13039 TREE_OPERAND (node, 0) = fold (op);
4a5f66c3 13040 TREE_TYPE (node) = prom_type;
dc0b3eff 13041 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
e04a16fb
AG
13042 return fold (node);
13043}
13044
13045/* Generic type resolution that sometimes takes place during node
13046 patching. Returned the resolved type or generate an error
13047 message. Return the resolved type or NULL_TREE. */
13048
13049static tree
13050resolve_type_during_patch (type)
13051 tree type;
13052{
13053 if (unresolved_type_p (type, NULL))
13054 {
13055 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
13056 if (!type_decl)
13057 {
13058 parse_error_context (type,
13059 "Class `%s' not found in type declaration",
13060 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13061 return NULL_TREE;
13062 }
13063 else
5e942c50
APB
13064 {
13065 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13066 return TREE_TYPE (type_decl);
13067 }
e04a16fb
AG
13068 }
13069 return type;
13070}
13071/* 5.5 Casting Conversion. error_mark_node is returned if an error is
13072 found. Otherwise NODE or something meant to replace it is returned. */
13073
13074static tree
0a2138e2 13075patch_cast (node, wfl_operator)
e04a16fb 13076 tree node;
e04a16fb
AG
13077 tree wfl_operator;
13078{
13079 tree op = TREE_OPERAND (node, 0);
13080 tree op_type = TREE_TYPE (op);
13081 tree cast_type = TREE_TYPE (node);
13082 char *t1;
13083
13084 /* First resolve OP_TYPE if unresolved */
13085 if (!(cast_type = resolve_type_during_patch (cast_type)))
13086 return error_mark_node;
13087
13088 /* Check on cast that are proven correct at compile time */
13089 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13090 {
e04a16fb
AG
13091 /* Same type */
13092 if (cast_type == op_type)
13093 return node;
13094
0b4d333e
APB
13095 /* float and double type are converted to the original type main
13096 variant and then to the target type. */
13097 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13098 op = convert (integer_type_node, op);
13099
e04a16fb
AG
13100 /* Try widening/narowwing convertion. Potentially, things need
13101 to be worked out in gcc so we implement the extreme cases
13102 correctly. fold_convert() needs to be fixed. */
13103 return convert (cast_type, op);
13104 }
13105
0b4d333e
APB
13106 /* It's also valid to cast a boolean into a boolean */
13107 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13108 return node;
13109
5e942c50
APB
13110 /* null can be casted to references */
13111 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13112 return build_null_of_type (cast_type);
13113
e04a16fb
AG
13114 /* The remaining legal casts involve conversion between reference
13115 types. Check for their compile time correctness. */
13116 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
09ed0f70 13117 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
e04a16fb
AG
13118 {
13119 TREE_TYPE (node) = promote_type (cast_type);
13120 /* Now, the case can be determined correct at compile time if
13121 OP_TYPE can be converted into CAST_TYPE by assignment
13122 conversion (5.2) */
13123
13124 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
15fdcfe9
PB
13125 {
13126 TREE_SET_CODE (node, NOP_EXPR);
13127 return node;
13128 }
13129
13130 if (flag_emit_class_files)
13131 {
13132 TREE_SET_CODE (node, CONVERT_EXPR);
13133 return node;
13134 }
e04a16fb
AG
13135
13136 /* The cast requires a run-time check */
13137 return build (CALL_EXPR, promote_type (cast_type),
13138 build_address_of (soft_checkcast_node),
13139 tree_cons (NULL_TREE, build_class_ref (cast_type),
13140 build_tree_list (NULL_TREE, op)),
13141 NULL_TREE);
13142 }
13143
13144 /* Any other casts are proven incorrect at compile time */
c2e3db92 13145 t1 = xstrdup (lang_printable_name (op_type, 0));
e04a16fb 13146 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
0a2138e2 13147 t1, lang_printable_name (cast_type, 0));
e04a16fb
AG
13148 free (t1);
13149 return error_mark_node;
13150}
13151
5e942c50
APB
13152/* Build a null constant and give it the type TYPE. */
13153
13154static tree
13155build_null_of_type (type)
13156 tree type;
13157{
13158 tree node = build_int_2 (0, 0);
13159 TREE_TYPE (node) = promote_type (type);
13160 return node;
13161}
13162
e04a16fb
AG
13163/* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13164 a list of indices. */
13165static tree
13166build_array_ref (location, array, index)
13167 int location;
13168 tree array, index;
13169{
13170 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13171 EXPR_WFL_LINECOL (node) = location;
13172 return node;
13173}
13174
13175/* 15.12 Array Access Expression */
13176
13177static tree
c877974e
APB
13178patch_array_ref (node)
13179 tree node;
e04a16fb
AG
13180{
13181 tree array = TREE_OPERAND (node, 0);
13182 tree array_type = TREE_TYPE (array);
13183 tree index = TREE_OPERAND (node, 1);
13184 tree index_type = TREE_TYPE (index);
e04a16fb
AG
13185 int error_found = 0;
13186
13187 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13188
e04a16fb
AG
13189 if (TREE_CODE (array_type) == POINTER_TYPE)
13190 array_type = TREE_TYPE (array_type);
13191
13192 /* The array reference must be an array */
13193 if (!TYPE_ARRAY_P (array_type))
13194 {
13195 parse_error_context
781b0558
KG
13196 (wfl_operator,
13197 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13198 lang_printable_name (array_type, 0));
e04a16fb
AG
13199 TREE_TYPE (node) = error_mark_node;
13200 error_found = 1;
13201 }
13202
c2952b01 13203 /* The array index undergoes unary numeric promotion. The promoted
e04a16fb 13204 type must be int */
15fdcfe9
PB
13205 index = do_unary_numeric_promotion (index);
13206 if (TREE_TYPE (index) != int_type_node)
e04a16fb 13207 {
1ebadc60 13208 if (valid_cast_to_p (index_type, int_type_node))
781b0558
KG
13209 parse_error_context (wfl_operator,
13210 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
1ebadc60
KG
13211 lang_printable_name (index_type, 0));
13212 else
781b0558
KG
13213 parse_error_context (wfl_operator,
13214 "Incompatible type for `[]'. Can't convert `%s' to `int'",
1ebadc60 13215 lang_printable_name (index_type, 0));
e04a16fb
AG
13216 TREE_TYPE (node) = error_mark_node;
13217 error_found = 1;
13218 }
13219
e04a16fb
AG
13220 if (error_found)
13221 return error_mark_node;
e04a16fb 13222
5e942c50 13223 array_type = TYPE_ARRAY_ELEMENT (array_type);
5e942c50 13224
7f1d4866 13225 if (flag_emit_class_files || flag_emit_xref)
e04a16fb 13226 {
15fdcfe9
PB
13227 TREE_OPERAND (node, 0) = array;
13228 TREE_OPERAND (node, 1) = index;
e04a16fb
AG
13229 }
13230 else
939d7216
PB
13231 {
13232 /* The save_expr is for correct evaluation order. It would be cleaner
13233 to use force_evaluation_order (see comment there), but that is
13234 difficult when we also have to deal with bounds checking. */
13235 if (TREE_SIDE_EFFECTS (index))
13236 array = save_expr (array);
13237 node = build_java_arrayaccess (array, array_type, index);
13238 if (TREE_SIDE_EFFECTS (index))
13239 node = build (COMPOUND_EXPR, array_type, array, node);
13240 }
e04a16fb
AG
13241 TREE_TYPE (node) = array_type;
13242 return node;
13243}
13244
13245/* 15.9 Array Creation Expressions */
13246
13247static tree
13248build_newarray_node (type, dims, extra_dims)
13249 tree type;
13250 tree dims;
13251 int extra_dims;
13252{
13253 tree node =
b67d701b 13254 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
e04a16fb 13255 build_int_2 (extra_dims, 0));
e04a16fb
AG
13256 return node;
13257}
13258
13259static tree
13260patch_newarray (node)
13261 tree node;
13262{
13263 tree type = TREE_OPERAND (node, 0);
13264 tree dims = TREE_OPERAND (node, 1);
13265 tree cdim, array_type;
13266 int error_found = 0;
13267 int ndims = 0;
13268 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
e04a16fb
AG
13269
13270 /* Dimension types are verified. It's better for the types to be
13271 verified in order. */
13272 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13273 {
13274 int dim_error = 0;
13275 tree dim = TREE_VALUE (cdim);
13276
13277 /* Dim might have been saved during its evaluation */
13278 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13279
13280 /* The type of each specified dimension must be an integral type. */
13281 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13282 dim_error = 1;
13283
13284 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13285 promoted type must be int. */
13286 else
13287 {
15fdcfe9 13288 dim = do_unary_numeric_promotion (dim);
e04a16fb
AG
13289 if (TREE_TYPE (dim) != int_type_node)
13290 dim_error = 1;
13291 }
13292
13293 /* Report errors on types here */
13294 if (dim_error)
13295 {
13296 parse_error_context
13297 (TREE_PURPOSE (cdim),
781b0558 13298 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
b67d701b 13299 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
e04a16fb 13300 "Explicit cast needed to" : "Can't"),
0a2138e2 13301 lang_printable_name (TREE_TYPE (dim), 0));
e04a16fb
AG
13302 error_found = 1;
13303 }
13304
e04a16fb
AG
13305 TREE_PURPOSE (cdim) = NULL_TREE;
13306 }
13307
13308 /* Resolve array base type if unresolved */
13309 if (!(type = resolve_type_during_patch (type)))
13310 error_found = 1;
13311
13312 if (error_found)
13313 {
13314 /* We don't want further evaluation of this bogus array creation
13315 operation */
13316 TREE_TYPE (node) = error_mark_node;
13317 return error_mark_node;
13318 }
13319
15fdcfe9
PB
13320 /* Set array_type to the actual (promoted) array type of the result. */
13321 if (TREE_CODE (type) == RECORD_TYPE)
13322 type = build_pointer_type (type);
13323 while (--xdims >= 0)
13324 {
13325 type = promote_type (build_java_array_type (type, -1));
13326 }
13327 dims = nreverse (dims);
13328 array_type = type;
13329 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13330 {
13331 type = array_type;
05bccae2
RK
13332 array_type
13333 = build_java_array_type (type,
13334 TREE_CODE (cdim) == INTEGER_CST
13335 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13336 : -1);
15fdcfe9
PB
13337 array_type = promote_type (array_type);
13338 }
13339 dims = nreverse (dims);
13340
e04a16fb
AG
13341 /* The node is transformed into a function call. Things are done
13342 differently according to the number of dimensions. If the number
13343 of dimension is equal to 1, then the nature of the base type
13344 (primitive or not) matters. */
15fdcfe9 13345 if (ndims == 1)
fdec99c6 13346 return build_new_array (type, TREE_VALUE (dims));
e04a16fb 13347
e04a16fb
AG
13348 /* Can't reuse what's already written in expr.c because it uses the
13349 JVM stack representation. Provide a build_multianewarray. FIXME */
15fdcfe9 13350 return build (CALL_EXPR, array_type,
e04a16fb 13351 build_address_of (soft_multianewarray_node),
15fdcfe9 13352 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
e04a16fb 13353 tree_cons (NULL_TREE,
15fdcfe9 13354 build_int_2 (ndims, 0), dims )),
e04a16fb
AG
13355 NULL_TREE);
13356}
13357
f8976021
APB
13358/* 10.6 Array initializer. */
13359
13360/* Build a wfl for array element that don't have one, so we can
13361 pin-point errors. */
13362
13363static tree
13364maybe_build_array_element_wfl (node)
13365 tree node;
13366{
13367 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13368 return build_expr_wfl (NULL_TREE, ctxp->filename,
13369 ctxp->elc.line, ctxp->elc.prev_col);
13370 else
13371 return NULL_TREE;
13372}
13373
13374/* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13375 identification of initialized arrays easier to detect during walk
13376 and expansion. */
13377
13378static tree
13379build_new_array_init (location, values)
13380 int location;
13381 tree values;
13382{
13383 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13384 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
5bba4807 13385 EXPR_WFL_LINECOL (to_return) = location;
f8976021
APB
13386 return to_return;
13387}
13388
13389/* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13390 occurred. Otherwise return NODE after having set its type
13391 appropriately. */
13392
13393static tree
13394patch_new_array_init (type, node)
13395 tree type, node;
f8976021
APB
13396{
13397 int error_seen = 0;
fdec99c6 13398 tree current, element_type;
f8976021 13399 HOST_WIDE_INT length;
fdec99c6
PB
13400 int all_constant = 1;
13401 tree init = TREE_OPERAND (node, 0);
f8976021 13402
fdec99c6
PB
13403 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13404 {
13405 parse_error_context (node,
13406 "Invalid array initializer for non-array type `%s'",
13407 lang_printable_name (type, 1));
13408 return error_mark_node;
13409 }
13410 type = TREE_TYPE (type);
13411 element_type = TYPE_ARRAY_ELEMENT (type);
f8976021 13412
fdec99c6
PB
13413 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13414
13415 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13416 current; length++, current = TREE_CHAIN (current))
f8976021 13417 {
fdec99c6
PB
13418 tree elt = TREE_VALUE (current);
13419 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
f8976021 13420 {
fdec99c6 13421 error_seen |= array_constructor_check_entry (element_type, current);
5bba4807
PB
13422 elt = TREE_VALUE (current);
13423 /* When compiling to native code, STRING_CST is converted to
13424 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13425 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
fdec99c6 13426 all_constant = 0;
f8976021 13427 }
fdec99c6
PB
13428 else
13429 {
13430 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13431 TREE_PURPOSE (current) = NULL_TREE;
13432 all_constant = 0;
13433 }
13434 if (elt && TREE_VALUE (elt) == error_mark_node)
13435 error_seen = 1;
f8976021
APB
13436 }
13437
13438 if (error_seen)
13439 return error_mark_node;
13440
13441 /* Create a new type. We can't reuse the one we have here by
13442 patching its dimension because it originally is of dimension -1
13443 hence reused by gcc. This would prevent triangular arrays. */
fdec99c6
PB
13444 type = build_java_array_type (element_type, length);
13445 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13446 TREE_TYPE (node) = promote_type (type);
13447 TREE_CONSTANT (init) = all_constant;
bc3ca41b 13448 TREE_CONSTANT (node) = all_constant;
f8976021
APB
13449 return node;
13450}
13451
13452/* Verify that one entry of the initializer element list can be
13453 assigned to the array base type. Report 1 if an error occurred, 0
13454 otherwise. */
13455
13456static int
13457array_constructor_check_entry (type, entry)
13458 tree type, entry;
13459{
13460 char *array_type_string = NULL; /* For error reports */
13461 tree value, type_value, new_value, wfl_value, patched;
13462 int error_seen = 0;
13463
13464 new_value = NULL_TREE;
13465 wfl_value = TREE_VALUE (entry);
13466
100f7cd8 13467 push_obstacks (&permanent_obstack, &permanent_obstack);
f8976021 13468 value = java_complete_tree (TREE_VALUE (entry));
1179ebc2 13469 /* patch_string return error_mark_node if arg is error_mark_node */
f8976021
APB
13470 if ((patched = patch_string (value)))
13471 value = patched;
1179ebc2
APB
13472 if (value == error_mark_node)
13473 return 1;
f8976021 13474
f8976021
APB
13475 type_value = TREE_TYPE (value);
13476
1179ebc2 13477 /* At anytime, try_builtin_assignconv can report a warning on
f8976021
APB
13478 constant overflow during narrowing. */
13479 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13480 new_value = try_builtin_assignconv (wfl_operator, type, value);
13481 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13482 type_value = promote_type (type);
100f7cd8
APB
13483
13484 pop_obstacks ();
f8976021
APB
13485 /* Check and report errors */
13486 if (!new_value)
13487 {
49f48c71 13488 const char *msg = (!valid_cast_to_p (type_value, type) ?
f8976021
APB
13489 "Can't" : "Explicit cast needed to");
13490 if (!array_type_string)
c2e3db92 13491 array_type_string = xstrdup (lang_printable_name (type, 1));
f8976021
APB
13492 parse_error_context
13493 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13494 msg, lang_printable_name (type_value, 1), array_type_string);
13495 error_seen = 1;
13496 }
13497
13498 if (new_value)
13499 {
13500 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
13501 TREE_VALUE (entry) = new_value;
13502 }
13503
13504 if (array_type_string)
13505 free (array_type_string);
13506
13507 TREE_PURPOSE (entry) = NULL_TREE;
13508 return error_seen;
13509}
13510
e04a16fb
AG
13511static tree
13512build_this (location)
13513 int location;
13514{
9ee9b555 13515 tree node = build_wfl_node (this_identifier_node);
b67d701b 13516 TREE_SET_CODE (node, THIS_EXPR);
e04a16fb
AG
13517 EXPR_WFL_LINECOL (node) = location;
13518 return node;
13519}
13520
13521/* 14.15 The return statement. It builds a modify expression that
13522 assigns the returned value to the RESULT_DECL that hold the value
13523 to be returned. */
13524
13525static tree
13526build_return (location, op)
13527 int location;
13528 tree op;
13529{
13530 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13531 EXPR_WFL_LINECOL (node) = location;
b67d701b 13532 node = build_debugable_stmt (location, node);
e04a16fb
AG
13533 return node;
13534}
13535
13536static tree
13537patch_return (node)
13538 tree node;
13539{
13540 tree return_exp = TREE_OPERAND (node, 0);
13541 tree meth = current_function_decl;
13542 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
e04a16fb
AG
13543 int error_found = 0;
13544
13545 TREE_TYPE (node) = error_mark_node;
13546 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13547
13548 /* It's invalid to have a return value within a function that is
13549 declared with the keyword void or that is a constructor */
13550 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13551 error_found = 1;
13552
f099f336 13553 /* It's invalid to use a return statement in a static block */
c2952b01 13554 if (DECL_CLINIT_P (current_function_decl))
f099f336
APB
13555 error_found = 1;
13556
e04a16fb
AG
13557 /* It's invalid to have a no return value within a function that
13558 isn't declared with the keyword `void' */
13559 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13560 error_found = 2;
c2952b01
APB
13561
13562 if (in_instance_initializer)
13563 error_found = 1;
e04a16fb
AG
13564
13565 if (error_found)
13566 {
c2952b01 13567 if (in_instance_initializer)
f099f336 13568 parse_error_context (wfl_operator,
c2952b01
APB
13569 "`return' inside instance initializer");
13570
13571 else if (DECL_CLINIT_P (current_function_decl))
13572 parse_error_context (wfl_operator,
13573 "`return' inside static initializer");
f099f336
APB
13574
13575 else if (!DECL_CONSTRUCTOR_P (meth))
22eed1e6 13576 {
c2e3db92 13577 char *t = xstrdup (lang_printable_name (mtype, 0));
22eed1e6
APB
13578 parse_error_context (wfl_operator,
13579 "`return' with%s value from `%s %s'",
13580 (error_found == 1 ? "" : "out"),
13581 t, lang_printable_name (meth, 0));
13582 free (t);
13583 }
13584 else
13585 parse_error_context (wfl_operator,
13586 "`return' with value from constructor `%s'",
13587 lang_printable_name (meth, 0));
e04a16fb
AG
13588 return error_mark_node;
13589 }
13590
5e942c50
APB
13591 /* If we have a return_exp, build a modify expression and expand
13592 it. Note: at that point, the assignment is declared valid, but we
13593 may want to carry some more hacks */
e04a16fb
AG
13594 if (return_exp)
13595 {
5e942c50
APB
13596 tree exp = java_complete_tree (return_exp);
13597 tree modify, patched;
13598
13599 /* If the function returned value and EXP are booleans, EXP has
13600 to be converted into the type of DECL_RESULT, which is integer
13601 (see complete_start_java_method) */
13602 if (TREE_TYPE (exp) == boolean_type_node &&
13603 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13604 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13605
13606 /* `null' can be assigned to a function returning a reference */
13607 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13608 exp == null_pointer_node)
13609 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13610
13611 if ((patched = patch_string (exp)))
13612 exp = patched;
13613
13614 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
e04a16fb
AG
13615 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13616 modify = java_complete_tree (modify);
5e942c50 13617
e04a16fb
AG
13618 if (modify != error_mark_node)
13619 {
13620 TREE_SIDE_EFFECTS (modify) = 1;
13621 TREE_OPERAND (node, 0) = modify;
13622 }
13623 else
13624 return error_mark_node;
13625 }
13626 TREE_TYPE (node) = void_type_node;
13627 TREE_SIDE_EFFECTS (node) = 1;
13628 return node;
13629}
13630
13631/* 14.8 The if Statement */
13632
13633static tree
13634build_if_else_statement (location, expression, if_body, else_body)
13635 int location;
13636 tree expression, if_body, else_body;
13637{
13638 tree node;
e04a16fb 13639 if (!else_body)
9bbc7d9f 13640 else_body = empty_stmt_node;
e04a16fb
AG
13641 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13642 EXPR_WFL_LINECOL (node) = location;
b67d701b 13643 node = build_debugable_stmt (location, node);
e04a16fb
AG
13644 return node;
13645}
13646
13647static tree
13648patch_if_else_statement (node)
13649 tree node;
13650{
13651 tree expression = TREE_OPERAND (node, 0);
13652
13653 TREE_TYPE (node) = error_mark_node;
13654 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13655
13656 /* The type of expression must be boolean */
b67d701b
PB
13657 if (TREE_TYPE (expression) != boolean_type_node
13658 && TREE_TYPE (expression) != promoted_boolean_type_node)
e04a16fb
AG
13659 {
13660 parse_error_context
13661 (wfl_operator,
13662 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
0a2138e2 13663 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
13664 return error_mark_node;
13665 }
13666
13667 TREE_TYPE (node) = void_type_node;
13668 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 13669 CAN_COMPLETE_NORMALLY (node)
9bbc7d9f
PB
13670 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13671 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
e04a16fb
AG
13672 return node;
13673}
13674
13675/* 14.6 Labeled Statements */
13676
13677/* Action taken when a lableled statement is parsed. a new
13678 LABELED_BLOCK_EXPR is created. No statement is attached to the
b635eb2f 13679 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
e04a16fb
AG
13680
13681static tree
0a2138e2 13682build_labeled_block (location, label)
e04a16fb 13683 int location;
0a2138e2 13684 tree label;
e04a16fb 13685{
b635eb2f 13686 tree label_name ;
e04a16fb 13687 tree label_decl, node;
b635eb2f
PB
13688 if (label == NULL_TREE || label == continue_identifier_node)
13689 label_name = label;
13690 else
e04a16fb 13691 {
b635eb2f
PB
13692 label_name = merge_qualified_name (label_id, label);
13693 /* Issue an error if we try to reuse a label that was previously
13694 declared */
13695 if (IDENTIFIER_LOCAL_VALUE (label_name))
13696 {
13697 EXPR_WFL_LINECOL (wfl_operator) = location;
781b0558
KG
13698 parse_error_context (wfl_operator,
13699 "Declaration of `%s' shadows a previous label declaration",
b635eb2f
PB
13700 IDENTIFIER_POINTER (label));
13701 EXPR_WFL_LINECOL (wfl_operator) =
13702 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
781b0558
KG
13703 parse_error_context (wfl_operator,
13704 "This is the location of the previous declaration of label `%s'",
b635eb2f
PB
13705 IDENTIFIER_POINTER (label));
13706 java_error_count--;
13707 }
e04a16fb
AG
13708 }
13709
13710 label_decl = create_label_decl (label_name);
13711 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
13712 EXPR_WFL_LINECOL (node) = location;
13713 TREE_SIDE_EFFECTS (node) = 1;
13714 return node;
13715}
13716
b67d701b 13717/* A labeled statement LBE is attached a statement. */
e04a16fb
AG
13718
13719static tree
b635eb2f 13720finish_labeled_statement (lbe, statement)
e04a16fb
AG
13721 tree lbe; /* Labeled block expr */
13722 tree statement;
13723{
13724 /* In anyways, tie the loop to its statement */
13725 LABELED_BLOCK_BODY (lbe) = statement;
b635eb2f
PB
13726 pop_labeled_block ();
13727 POP_LABELED_BLOCK ();
e04a16fb
AG
13728 return lbe;
13729}
13730
13731/* 14.10, 14.11, 14.12 Loop Statements */
13732
13733/* Create an empty LOOP_EXPR and make it the last in the nested loop
13734 list. */
13735
13736static tree
13737build_new_loop (loop_body)
13738 tree loop_body;
13739{
13740 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
13741 TREE_SIDE_EFFECTS (loop) = 1;
13742 PUSH_LOOP (loop);
13743 return loop;
13744}
13745
13746/* Create a loop body according to the following structure:
13747 COMPOUND_EXPR
13748 COMPOUND_EXPR (loop main body)
13749 EXIT_EXPR (this order is for while/for loops.
13750 LABELED_BLOCK_EXPR the order is reversed for do loops)
34f4db93 13751 LABEL_DECL (a continue occuring here branches at the
e04a16fb
AG
13752 BODY end of this labeled block)
13753 INCREMENT (if any)
13754
13755 REVERSED, if non zero, tells that the loop condition expr comes
b67d701b
PB
13756 after the body, like in the do-while loop.
13757
13758 To obtain a loop, the loop body structure described above is
13759 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
13760
13761 LABELED_BLOCK_EXPR
13762 LABEL_DECL (use this label to exit the loop)
13763 LOOP_EXPR
13764 <structure described above> */
e04a16fb
AG
13765
13766static tree
13767build_loop_body (location, condition, reversed)
13768 int location;
13769 tree condition;
13770 int reversed;
13771{
0a2138e2 13772 tree first, second, body;
e04a16fb
AG
13773
13774 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
13775 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
13776 condition = build_debugable_stmt (location, condition);
13777 TREE_SIDE_EFFECTS (condition) = 1;
13778
b635eb2f 13779 body = build_labeled_block (0, continue_identifier_node);
e04a16fb
AG
13780 first = (reversed ? body : condition);
13781 second = (reversed ? condition : body);
13782 return
13783 build (COMPOUND_EXPR, NULL_TREE,
9bbc7d9f 13784 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
e04a16fb
AG
13785}
13786
13787/* Install CONDITION (if any) and loop BODY (using REVERSED to tell
13788 their order) on the current loop. Unlink the current loop from the
13789 loop list. */
13790
13791static tree
b635eb2f 13792finish_loop_body (location, condition, body, reversed)
e04a16fb
AG
13793 int location;
13794 tree condition, body;
13795 int reversed;
13796{
13797 tree to_return = ctxp->current_loop;
13798 tree loop_body = LOOP_EXPR_BODY (to_return);
13799 if (condition)
13800 {
13801 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
13802 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
13803 The real EXIT_EXPR is one operand further. */
13804 EXPR_WFL_LINECOL (cnode) = location;
13805 /* This one is for accurate error reports */
13806 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
13807 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
13808 }
13809 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
13810 POP_LOOP ();
13811 return to_return;
13812}
13813
b635eb2f 13814/* Tailored version of finish_loop_body for FOR loops, when FOR
e04a16fb
AG
13815 loops feature the condition part */
13816
13817static tree
b635eb2f 13818finish_for_loop (location, condition, update, body)
e04a16fb
AG
13819 int location;
13820 tree condition, update, body;
13821{
13822 /* Put the condition and the loop body in place */
b635eb2f 13823 tree loop = finish_loop_body (location, condition, body, 0);
e04a16fb
AG
13824 /* LOOP is the current loop which has been now popped of the loop
13825 stack. Install the update block */
13826 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
13827 return loop;
13828}
13829
5cbdba64
APB
13830/* Try to find the loop a block might be related to. This comprises
13831 the case where the LOOP_EXPR is found as the second operand of a
13832 COMPOUND_EXPR, because the loop happens to have an initialization
13833 part, then expressed as the first operand of the COMPOUND_EXPR. If
13834 the search finds something, 1 is returned. Otherwise, 0 is
13835 returned. The search is assumed to start from a
13836 LABELED_BLOCK_EXPR's block. */
13837
13838static tree
13839search_loop (statement)
13840 tree statement;
13841{
13842 if (TREE_CODE (statement) == LOOP_EXPR)
13843 return statement;
13844
13845 if (TREE_CODE (statement) == BLOCK)
13846 statement = BLOCK_SUBBLOCKS (statement);
13847 else
13848 return NULL_TREE;
13849
13850 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13851 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
13852 statement = TREE_OPERAND (statement, 1);
13853
13854 return (TREE_CODE (statement) == LOOP_EXPR
c2952b01 13855 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
5cbdba64
APB
13856}
13857
13858/* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
13859 returned otherwise. */
13860
13861static int
13862labeled_block_contains_loop_p (block, loop)
13863 tree block, loop;
13864{
13865 if (!block)
13866 return 0;
13867
13868 if (LABELED_BLOCK_BODY (block) == loop)
13869 return 1;
13870
c2952b01 13871 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
5cbdba64
APB
13872 return 1;
13873
13874 return 0;
13875}
13876
e04a16fb 13877/* If the loop isn't surrounded by a labeled statement, create one and
b635eb2f 13878 insert LOOP as its body. */
e04a16fb
AG
13879
13880static tree
13881patch_loop_statement (loop)
13882 tree loop;
13883{
cd9643f7 13884 tree loop_label;
5cbdba64 13885
cd9643f7 13886 TREE_TYPE (loop) = void_type_node;
5cbdba64
APB
13887 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
13888 return loop;
13889
cd9643f7 13890 loop_label = build_labeled_block (0, NULL_TREE);
5cbdba64
APB
13891 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
13892 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
cd9643f7
PB
13893 LABELED_BLOCK_BODY (loop_label) = loop;
13894 PUSH_LABELED_BLOCK (loop_label);
5cbdba64 13895 return loop_label;
e04a16fb
AG
13896}
13897
13898/* 14.13, 14.14: break and continue Statements */
13899
13900/* Build a break or a continue statement. a null NAME indicates an
13901 unlabeled break/continue statement. */
13902
13903static tree
13904build_bc_statement (location, is_break, name)
13905 int location, is_break;
13906 tree name;
13907{
13908 tree break_continue, label_block_expr = NULL_TREE;
13909
13910 if (name)
13911 {
13912 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
13913 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
13914 /* Null means that we don't have a target for this named
13915 break/continue. In this case, we make the target to be the
13916 label name, so that the error can be reported accuratly in
13917 patch_bc_statement. */
13918 label_block_expr = EXPR_WFL_NODE (name);
13919 }
13920 /* Unlabeled break/continue will be handled during the
13921 break/continue patch operation */
13922 break_continue
13923 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
13924
13925 IS_BREAK_STMT_P (break_continue) = is_break;
13926 TREE_SIDE_EFFECTS (break_continue) = 1;
13927 EXPR_WFL_LINECOL (break_continue) = location;
b67d701b 13928 break_continue = build_debugable_stmt (location, break_continue);
e04a16fb
AG
13929 return break_continue;
13930}
13931
13932/* Verification of a break/continue statement. */
13933
13934static tree
13935patch_bc_statement (node)
13936 tree node;
13937{
13938 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
b635eb2f 13939 tree labeled_block = ctxp->current_labeled_block;
b67d701b 13940 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
e04a16fb 13941
e04a16fb 13942 /* Having an identifier here means that the target is unknown. */
b635eb2f 13943 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
e04a16fb
AG
13944 {
13945 parse_error_context (wfl_operator, "No label definition found for `%s'",
13946 IDENTIFIER_POINTER (bc_label));
13947 return error_mark_node;
13948 }
b635eb2f 13949 if (! IS_BREAK_STMT_P (node))
e04a16fb 13950 {
b635eb2f
PB
13951 /* It's a continue statement. */
13952 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 13953 {
b635eb2f
PB
13954 if (labeled_block == NULL_TREE)
13955 {
13956 if (bc_label == NULL_TREE)
13957 parse_error_context (wfl_operator,
13958 "`continue' must be in loop");
13959 else
1504b2b4
APB
13960 parse_error_context
13961 (wfl_operator, "continue label `%s' does not name a loop",
13962 IDENTIFIER_POINTER (bc_label));
b635eb2f
PB
13963 return error_mark_node;
13964 }
13965 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
13966 == continue_identifier_node)
13967 && (bc_label == NULL_TREE
13968 || TREE_CHAIN (labeled_block) == bc_label))
13969 {
13970 bc_label = labeled_block;
13971 break;
13972 }
e04a16fb 13973 }
e04a16fb 13974 }
b635eb2f 13975 else if (!bc_label)
34f4db93 13976 {
b635eb2f 13977 for (;; labeled_block = TREE_CHAIN (labeled_block))
e04a16fb 13978 {
b635eb2f
PB
13979 if (labeled_block == NULL_TREE)
13980 {
13981 parse_error_context (wfl_operator,
13982 "`break' must be in loop or switch");
13983 return error_mark_node;
13984 }
13985 target_stmt = LABELED_BLOCK_BODY (labeled_block);
13986 if (TREE_CODE (target_stmt) == SWITCH_EXPR
5cbdba64 13987 || search_loop (target_stmt))
b635eb2f
PB
13988 {
13989 bc_label = labeled_block;
13990 break;
13991 }
e04a16fb 13992 }
e04a16fb
AG
13993 }
13994
b635eb2f 13995 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15fdcfe9
PB
13996 CAN_COMPLETE_NORMALLY (bc_label) = 1;
13997
e04a16fb
AG
13998 /* Our break/continue don't return values. */
13999 TREE_TYPE (node) = void_type_node;
14000 /* Encapsulate the break within a compound statement so that it's
5cbdba64 14001 expanded all the times by expand_expr (and not clobbered
e04a16fb
AG
14002 sometimes, like after a if statement) */
14003 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14004 TREE_SIDE_EFFECTS (node) = 1;
14005 return node;
14006}
14007
14008/* Process the exit expression belonging to a loop. Its type must be
14009 boolean. */
14010
14011static tree
14012patch_exit_expr (node)
14013 tree node;
14014{
14015 tree expression = TREE_OPERAND (node, 0);
14016 TREE_TYPE (node) = error_mark_node;
14017 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14018
14019 /* The type of expression must be boolean */
14020 if (TREE_TYPE (expression) != boolean_type_node)
14021 {
14022 parse_error_context
14023 (wfl_operator,
781b0558 14024 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
0a2138e2 14025 lang_printable_name (TREE_TYPE (expression), 0));
e04a16fb
AG
14026 return error_mark_node;
14027 }
14028 /* Now we know things are allright, invert the condition, fold and
14029 return */
14030 TREE_OPERAND (node, 0) =
14031 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15fdcfe9
PB
14032
14033 if (! integer_zerop (TREE_OPERAND (node, 0))
14034 && ctxp->current_loop != NULL_TREE
14035 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14036 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14037 if (! integer_onep (TREE_OPERAND (node, 0)))
14038 CAN_COMPLETE_NORMALLY (node) = 1;
14039
14040
e04a16fb
AG
14041 TREE_TYPE (node) = void_type_node;
14042 return node;
14043}
b67d701b
PB
14044
14045/* 14.9 Switch statement */
14046
14047static tree
14048patch_switch_statement (node)
14049 tree node;
14050{
c877974e 14051 tree se = TREE_OPERAND (node, 0), se_type;
b67d701b
PB
14052
14053 /* Complete the switch expression */
14054 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14055 se_type = TREE_TYPE (se);
14056 /* The type of the switch expression must be char, byte, short or
14057 int */
14058 if (!JINTEGRAL_TYPE_P (se_type))
14059 {
14060 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
781b0558
KG
14061 parse_error_context (wfl_operator,
14062 "Incompatible type for `switch'. Can't convert `%s' to `int'",
0a2138e2 14063 lang_printable_name (se_type, 0));
b67d701b
PB
14064 /* This is what java_complete_tree will check */
14065 TREE_OPERAND (node, 0) = error_mark_node;
14066 return error_mark_node;
14067 }
14068
15fdcfe9 14069 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
b67d701b
PB
14070
14071 /* Ready to return */
15fdcfe9 14072 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
b67d701b
PB
14073 {
14074 TREE_TYPE (node) = error_mark_node;
14075 return error_mark_node;
14076 }
14077 TREE_TYPE (node) = void_type_node;
14078 TREE_SIDE_EFFECTS (node) = 1;
15fdcfe9 14079 CAN_COMPLETE_NORMALLY (node)
c877974e
APB
14080 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14081 || ! SWITCH_HAS_DEFAULT (node);
b67d701b
PB
14082 return node;
14083}
14084
165f37bc 14085/* 14.18 The try/catch statements */
b67d701b 14086
b67d701b 14087static tree
a7d8d81f 14088build_try_statement (location, try_block, catches)
b67d701b 14089 int location;
a7d8d81f
PB
14090 tree try_block, catches;
14091{
14092 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
b67d701b 14093 EXPR_WFL_LINECOL (node) = location;
a7d8d81f 14094 return node;
b67d701b
PB
14095}
14096
a7d8d81f
PB
14097static tree
14098build_try_finally_statement (location, try_block, finally)
14099 int location;
14100 tree try_block, finally;
b67d701b 14101{
a7d8d81f
PB
14102 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14103 EXPR_WFL_LINECOL (node) = location;
14104 return node;
b67d701b
PB
14105}
14106
14107static tree
14108patch_try_statement (node)
14109 tree node;
14110{
14111 int error_found = 0;
14112 tree try = TREE_OPERAND (node, 0);
14113 /* Exception handlers are considered in left to right order */
14114 tree catch = nreverse (TREE_OPERAND (node, 1));
b9f7e36c 14115 tree current, caught_type_list = NULL_TREE;
b67d701b
PB
14116
14117 /* Check catch clauses, if any. Every time we find an error, we try
b9f7e36c
APB
14118 to process the next catch clause. We process the catch clause before
14119 the try block so that when processing the try block we can check thrown
14120 exceptions againts the caught type list. */
b67d701b
PB
14121 for (current = catch; current; current = TREE_CHAIN (current))
14122 {
14123 tree carg_decl, carg_type;
14124 tree sub_current, catch_block, catch_clause;
14125 int unreachable;
14126
b67d701b 14127 /* At this point, the structure of the catch clause is
b67d701b
PB
14128 CATCH_EXPR (catch node)
14129 BLOCK (with the decl of the parameter)
14130 COMPOUND_EXPR
7525cc04 14131 MODIFY_EXPR (assignment of the catch parameter)
b67d701b 14132 BLOCK (catch clause block)
a7d8d81f
PB
14133 */
14134 catch_clause = TREE_OPERAND (current, 0);
b67d701b
PB
14135 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14136 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14137
14138 /* Catch clauses can't have more than one parameter declared,
14139 but it's already enforced by the grammar. Make sure that the
14140 only parameter of the clause statement in of class Throwable
14141 or a subclass of Throwable, but that was done earlier. The
14142 catch clause parameter type has also been resolved. */
14143
14144 /* Just make sure that the catch clause parameter type inherits
14145 from java.lang.Throwable */
14146 if (!inherits_from_p (carg_type, throwable_type_node))
14147 {
14148 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14149 parse_error_context (wfl_operator,
781b0558 14150 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
0a2138e2 14151 lang_printable_name (carg_type, 0));
b67d701b
PB
14152 error_found = 1;
14153 continue;
14154 }
14155
14156 /* Partial check for unreachable catch statement: The catch
14157 clause is reachable iff is no earlier catch block A in
14158 the try statement such that the type of the catch
14159 clause's parameter is the same as or a subclass of the
14160 type of A's parameter */
14161 unreachable = 0;
14162 for (sub_current = catch;
14163 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14164 {
14165 tree sub_catch_clause, decl;
a7d8d81f 14166 sub_catch_clause = TREE_OPERAND (sub_current, 0);
b67d701b
PB
14167 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14168
14169 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14170 {
14171 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14172 parse_error_context
781b0558
KG
14173 (wfl_operator,
14174 "`catch' not reached because of the catch clause at line %d",
14175 EXPR_WFL_LINENO (sub_current));
b67d701b
PB
14176 unreachable = error_found = 1;
14177 break;
14178 }
14179 }
b67d701b
PB
14180 /* Complete the catch clause block */
14181 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14182 if (catch_block == error_mark_node)
14183 {
14184 error_found = 1;
14185 continue;
14186 }
15fdcfe9
PB
14187 if (CAN_COMPLETE_NORMALLY (catch_block))
14188 CAN_COMPLETE_NORMALLY (node) = 1;
b67d701b 14189 TREE_OPERAND (current, 0) = catch_block;
15fdcfe9
PB
14190
14191 if (unreachable)
14192 continue;
14193
14194 /* Things to do here: the exception must be thrown */
14195
14196 /* Link this type to the caught type list */
14197 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
b67d701b
PB
14198 }
14199
b9f7e36c
APB
14200 PUSH_EXCEPTIONS (caught_type_list);
14201 if ((try = java_complete_tree (try)) == error_mark_node)
14202 error_found = 1;
15fdcfe9
PB
14203 if (CAN_COMPLETE_NORMALLY (try))
14204 CAN_COMPLETE_NORMALLY (node) = 1;
b9f7e36c
APB
14205 POP_EXCEPTIONS ();
14206
b67d701b
PB
14207 /* Verification ends here */
14208 if (error_found)
14209 return error_mark_node;
14210
14211 TREE_OPERAND (node, 0) = try;
14212 TREE_OPERAND (node, 1) = catch;
b67d701b
PB
14213 TREE_TYPE (node) = void_type_node;
14214 return node;
14215}
b9f7e36c
APB
14216
14217/* 14.17 The synchronized Statement */
14218
14219static tree
14220patch_synchronized_statement (node, wfl_op1)
14221 tree node, wfl_op1;
14222{
5a005d9e 14223 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
b9f7e36c 14224 tree block = TREE_OPERAND (node, 1);
5a005d9e 14225
d8fccff5 14226 tree enter, exit, expr_decl, assignment;
5a005d9e
PB
14227
14228 if (expr == error_mark_node)
14229 {
14230 block = java_complete_tree (block);
14231 return expr;
14232 }
b9f7e36c
APB
14233
14234 /* The TYPE of expr must be a reference type */
5a005d9e 14235 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
b9f7e36c
APB
14236 {
14237 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558 14238 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
0a2138e2 14239 lang_printable_name (TREE_TYPE (expr), 0));
b9f7e36c
APB
14240 return error_mark_node;
14241 }
14242
ce6e9147
APB
14243 if (flag_emit_xref)
14244 {
14245 TREE_OPERAND (node, 0) = expr;
14246 TREE_OPERAND (node, 1) = java_complete_tree (block);
14247 CAN_COMPLETE_NORMALLY (node) = 1;
14248 return node;
14249 }
14250
b9f7e36c
APB
14251 /* Generate a try-finally for the synchronized statement, except
14252 that the handler that catches all throw exception calls
14253 _Jv_MonitorExit and then rethrow the exception.
14254 The synchronized statement is then implemented as:
14255 TRY
14256 {
14257 _Jv_MonitorEnter (expression)
14258 synchronized_block
14259 _Jv_MonitorExit (expression)
14260 }
14261 CATCH_ALL
14262 {
14263 e = _Jv_exception_info ();
14264 _Jv_MonitorExit (expression)
14265 Throw (e);
14266 } */
14267
5a005d9e
PB
14268 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14269 BUILD_MONITOR_ENTER (enter, expr_decl);
14270 BUILD_MONITOR_EXIT (exit, expr_decl);
14271 CAN_COMPLETE_NORMALLY (enter) = 1;
14272 CAN_COMPLETE_NORMALLY (exit) = 1;
96847892
AH
14273 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14274 TREE_SIDE_EFFECTS (assignment) = 1;
5a005d9e
PB
14275 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14276 build (COMPOUND_EXPR, NULL_TREE,
14277 build (WITH_CLEANUP_EXPR, NULL_TREE,
14278 build (COMPOUND_EXPR, NULL_TREE,
96847892 14279 assignment, enter),
5a005d9e
PB
14280 NULL_TREE, exit),
14281 block));
14282 node = build_expr_block (node, expr_decl);
14283
14284 return java_complete_tree (node);
b9f7e36c
APB
14285}
14286
14287/* 14.16 The throw Statement */
14288
14289static tree
14290patch_throw_statement (node, wfl_op1)
14291 tree node, wfl_op1;
14292{
14293 tree expr = TREE_OPERAND (node, 0);
14294 tree type = TREE_TYPE (expr);
14295 int unchecked_ok = 0, tryblock_throws_ok = 0;
14296
14297 /* Thrown expression must be assignable to java.lang.Throwable */
14298 if (!try_reference_assignconv (throwable_type_node, expr))
14299 {
14300 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14301 parse_error_context (wfl_operator,
14302 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
0a2138e2 14303 lang_printable_name (type, 0));
b9f7e36c
APB
14304 /* If the thrown expression was a reference, we further the
14305 compile-time check. */
14306 if (!JREFERENCE_TYPE_P (type))
14307 return error_mark_node;
14308 }
14309
14310 /* At least one of the following must be true */
14311
14312 /* The type of the throw expression is a not checked exception,
14313 i.e. is a unchecked expression. */
c877974e 14314 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
b9f7e36c 14315
c2952b01
APB
14316 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14317 /* An instance can't throw a checked excetion unless that exception
14318 is explicitely declared in the `throws' clause of each
14319 constructor. This doesn't apply to anonymous classes, since they
14320 don't have declared constructors. */
14321 if (!unchecked_ok
14322 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14323 {
14324 tree current;
14325 for (current = TYPE_METHODS (current_class); current;
14326 current = TREE_CHAIN (current))
14327 if (DECL_CONSTRUCTOR_P (current)
14328 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14329 {
14330 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)",
14331 lang_printable_name (TREE_TYPE (expr), 0));
14332 return error_mark_node;
14333 }
14334 }
14335
b9f7e36c
APB
14336 /* Throw is contained in a try statement and at least one catch
14337 clause can receive the thrown expression or the current method is
14338 declared to throw such an exception. Or, the throw statement is
14339 contained in a method or constructor declaration and the type of
14340 the Expression is assignable to at least one type listed in the
14341 throws clause the declaration. */
b9f7e36c 14342 if (!unchecked_ok)
f099f336 14343 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
b9f7e36c
APB
14344 if (!(unchecked_ok || tryblock_throws_ok))
14345 {
14346 /* If there is a surrounding try block that has no matching
14347 clatch clause, report it first. A surrounding try block exits
14348 only if there is something after the list of checked
14349 exception thrown by the current function (if any). */
14350 if (IN_TRY_BLOCK_P ())
781b0558 14351 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 14352 lang_printable_name (type, 0));
b9f7e36c
APB
14353 /* If we have no surrounding try statement and the method doesn't have
14354 any throws, report it now. FIXME */
f099f336
APB
14355
14356 /* We report that the exception can't be throw from a try block
14357 in all circumstances but when the `throw' is inside a static
14358 block. */
b9f7e36c
APB
14359 else if (!EXCEPTIONS_P (currently_caught_type_list)
14360 && !tryblock_throws_ok)
f099f336 14361 {
c2952b01 14362 if (DECL_CLINIT_P (current_function_decl))
781b0558
KG
14363 parse_error_context (wfl_operator,
14364 "Checked exception `%s' can't be thrown in initializer",
f099f336
APB
14365 lang_printable_name (type, 0));
14366 else
781b0558
KG
14367 parse_error_context (wfl_operator,
14368 "Checked exception `%s' isn't thrown from a `try' block",
f099f336
APB
14369 lang_printable_name (type, 0));
14370 }
b9f7e36c
APB
14371 /* Otherwise, the current method doesn't have the appropriate
14372 throws declaration */
14373 else
781b0558 14374 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
0a2138e2 14375 lang_printable_name (type, 0));
b9f7e36c
APB
14376 return error_mark_node;
14377 }
14378
ce6e9147 14379 if (! flag_emit_class_files && ! flag_emit_xref)
15fdcfe9 14380 BUILD_THROW (node, expr);
ce6e9147
APB
14381
14382 /* If doing xrefs, keep the location where the `throw' was seen. */
14383 if (flag_emit_xref)
14384 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
b9f7e36c
APB
14385 return node;
14386}
14387
14388/* Check that exception said to be thrown by method DECL can be
14389 effectively caught from where DECL is invoked. */
14390
14391static void
14392check_thrown_exceptions (location, decl)
14393 int location;
14394 tree decl;
14395{
14396 tree throws;
14397 /* For all the unchecked exceptions thrown by DECL */
14398 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14399 throws = TREE_CHAIN (throws))
0a2138e2 14400 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
b9f7e36c 14401 {
3e78f871
PB
14402#if 1
14403 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14404 if (DECL_NAME (decl) == get_identifier ("clone"))
14405 continue;
14406#endif
b9f7e36c 14407 EXPR_WFL_LINECOL (wfl_operator) = location;
c2952b01 14408 if (DECL_FINIT_P (current_function_decl))
7705e9db
APB
14409 parse_error_context
14410 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14411 lang_printable_name (TREE_VALUE (throws), 0));
14412 else
14413 {
14414 parse_error_context
781b0558 14415 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
7705e9db 14416 lang_printable_name (TREE_VALUE (throws), 0),
c2952b01 14417 (DECL_INIT_P (current_function_decl) ?
7705e9db
APB
14418 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14419 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14420 }
b9f7e36c
APB
14421 }
14422}
14423
c877974e 14424/* Return 1 if checked EXCEPTION is caught at the current nesting level of
b9f7e36c
APB
14425 try-catch blocks, OR is listed in the `throws' clause of the
14426 current method. */
14427
14428static int
0a2138e2 14429check_thrown_exceptions_do (exception)
b9f7e36c
APB
14430 tree exception;
14431{
14432 tree list = currently_caught_type_list;
c877974e 14433 resolve_and_layout (exception, NULL_TREE);
b9f7e36c
APB
14434 /* First, all the nested try-catch-finally at that stage. The
14435 last element contains `throws' clause exceptions, if any. */
c877974e
APB
14436 if (IS_UNCHECKED_EXCEPTION_P (exception))
14437 return 1;
b9f7e36c
APB
14438 while (list)
14439 {
14440 tree caught;
14441 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14442 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14443 return 1;
14444 list = TREE_CHAIN (list);
14445 }
14446 return 0;
14447}
14448
14449static void
14450purge_unchecked_exceptions (mdecl)
14451 tree mdecl;
14452{
14453 tree throws = DECL_FUNCTION_THROWS (mdecl);
14454 tree new = NULL_TREE;
14455
14456 while (throws)
14457 {
14458 tree next = TREE_CHAIN (throws);
c877974e 14459 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
b9f7e36c
APB
14460 {
14461 TREE_CHAIN (throws) = new;
14462 new = throws;
14463 }
14464 throws = next;
14465 }
14466 /* List is inverted here, but it doesn't matter */
14467 DECL_FUNCTION_THROWS (mdecl) = new;
14468}
22eed1e6
APB
14469
14470/* 15.24 Conditional Operator ?: */
14471
14472static tree
14473patch_conditional_expr (node, wfl_cond, wfl_op1)
14474 tree node, wfl_cond, wfl_op1;
14475{
14476 tree cond = TREE_OPERAND (node, 0);
14477 tree op1 = TREE_OPERAND (node, 1);
14478 tree op2 = TREE_OPERAND (node, 2);
22eed1e6 14479 tree resulting_type = NULL_TREE;
ac825856 14480 tree t1, t2, patched;
22eed1e6
APB
14481 int error_found = 0;
14482
ac825856
APB
14483 /* Operands of ?: might be StringBuffers crafted as a result of a
14484 string concatenation. Obtain a descent operand here. */
14485 if ((patched = patch_string (op1)))
14486 TREE_OPERAND (node, 1) = op1 = patched;
14487 if ((patched = patch_string (op2)))
14488 TREE_OPERAND (node, 2) = op2 = patched;
14489
14490 t1 = TREE_TYPE (op1);
14491 t2 = TREE_TYPE (op2);
14492
22eed1e6
APB
14493 /* The first expression must be a boolean */
14494 if (TREE_TYPE (cond) != boolean_type_node)
14495 {
14496 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
781b0558
KG
14497 parse_error_context (wfl_operator,
14498 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
22eed1e6
APB
14499 lang_printable_name (TREE_TYPE (cond), 0));
14500 error_found = 1;
14501 }
14502
14503 /* Second and third can be numeric, boolean (i.e. primitive),
14504 references or null. Anything else results in an error */
14505 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14506 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14507 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14508 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14509 error_found = 1;
14510
14511 /* Determine the type of the conditional expression. Same types are
14512 easy to deal with */
14513 else if (t1 == t2)
14514 resulting_type = t1;
14515
14516 /* There are different rules for numeric types */
14517 else if (JNUMERIC_TYPE_P (t1))
14518 {
14519 /* if byte/short found, the resulting type is short */
14520 if ((t1 == byte_type_node && t2 == short_type_node)
14521 || (t1 == short_type_node && t2 == byte_type_node))
14522 resulting_type = short_type_node;
14523
14524 /* If t1 is a constant int and t2 is of type byte, short or char
14525 and t1's value fits in t2, then the resulting type is t2 */
14526 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14527 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14528 resulting_type = t2;
14529
14530 /* If t2 is a constant int and t1 is of type byte, short or char
14531 and t2's value fits in t1, then the resulting type is t1 */
14532 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14533 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14534 resulting_type = t1;
14535
14536 /* Otherwise, binary numeric promotion is applied and the
14537 resulting type is the promoted type of operand 1 and 2 */
14538 else
93024893 14539 resulting_type = binary_numeric_promotion (t1, t2,
22eed1e6
APB
14540 &TREE_OPERAND (node, 1),
14541 &TREE_OPERAND (node, 2));
14542 }
14543
14544 /* Cases of a reference and a null type */
14545 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14546 resulting_type = t1;
14547
14548 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14549 resulting_type = t2;
14550
14551 /* Last case: different reference types. If a type can be converted
14552 into the other one by assignment conversion, the latter
14553 determines the type of the expression */
14554 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14555 resulting_type = promote_type (t1);
14556
14557 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14558 resulting_type = promote_type (t2);
14559
14560 /* If we don't have any resulting type, we're in trouble */
14561 if (!resulting_type)
14562 {
c2e3db92 14563 char *t = xstrdup (lang_printable_name (t1, 0));
22eed1e6 14564 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
781b0558
KG
14565 parse_error_context (wfl_operator,
14566 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14567 t, lang_printable_name (t2, 0));
22eed1e6
APB
14568 free (t);
14569 error_found = 1;
14570 }
14571
14572 if (error_found)
14573 {
14574 TREE_TYPE (node) = error_mark_node;
14575 return error_mark_node;
14576 }
14577
14578 TREE_TYPE (node) = resulting_type;
14579 TREE_SET_CODE (node, COND_EXPR);
15fdcfe9 14580 CAN_COMPLETE_NORMALLY (node) = 1;
22eed1e6
APB
14581 return node;
14582}
ac825856 14583
5b09b33e
PB
14584/* Try to constant fold NODE.
14585 If NODE is not a constant expression, return NULL_EXPR.
14586 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14587
14588static tree
14589fold_constant_for_init (node, context)
14590 tree node;
14591 tree context;
14592{
14593 tree op0, op1, val;
14594 enum tree_code code = TREE_CODE (node);
14595
93024893 14596 if (code == STRING_CST)
5b09b33e 14597 return node;
93024893
APB
14598
14599 if (code == INTEGER_CST || code == REAL_CST)
14600 return convert (TREE_TYPE (context), node);
5b09b33e
PB
14601
14602 switch (code)
14603 {
5b09b33e
PB
14604 case PLUS_EXPR:
14605 case MINUS_EXPR:
bc3ca41b
PB
14606 case MULT_EXPR:
14607 case TRUNC_MOD_EXPR:
14608 case RDIV_EXPR:
5b09b33e
PB
14609 case LSHIFT_EXPR:
14610 case RSHIFT_EXPR:
14611 case URSHIFT_EXPR:
14612 case BIT_AND_EXPR:
14613 case BIT_XOR_EXPR:
14614 case BIT_IOR_EXPR:
5b09b33e
PB
14615 case TRUTH_ANDIF_EXPR:
14616 case TRUTH_ORIF_EXPR:
14617 case EQ_EXPR:
14618 case NE_EXPR:
14619 case GT_EXPR:
14620 case GE_EXPR:
14621 case LT_EXPR:
14622 case LE_EXPR:
14623 op0 = TREE_OPERAND (node, 0);
14624 op1 = TREE_OPERAND (node, 1);
14625 val = fold_constant_for_init (op0, context);
14626 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14627 return NULL_TREE;
14628 TREE_OPERAND (node, 0) = val;
14629 val = fold_constant_for_init (op1, context);
14630 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14631 return NULL_TREE;
14632 TREE_OPERAND (node, 1) = val;
14633 return patch_binop (node, op0, op1);
14634
14635 case UNARY_PLUS_EXPR:
14636 case NEGATE_EXPR:
14637 case TRUTH_NOT_EXPR:
14638 case BIT_NOT_EXPR:
14639 case CONVERT_EXPR:
14640 op0 = TREE_OPERAND (node, 0);
14641 val = fold_constant_for_init (op0, context);
14642 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14643 return NULL_TREE;
14644 TREE_OPERAND (node, 0) = val;
5a005d9e 14645 return patch_unaryop (node, op0);
5b09b33e
PB
14646 break;
14647
14648 case COND_EXPR:
14649 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14650 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14651 return NULL_TREE;
14652 TREE_OPERAND (node, 0) = val;
14653 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14654 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14655 return NULL_TREE;
14656 TREE_OPERAND (node, 1) = val;
14657 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14658 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14659 return NULL_TREE;
14660 TREE_OPERAND (node, 2) = val;
14661 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14662 : TREE_OPERAND (node, 2);
14663
14664 case VAR_DECL:
8576f094
APB
14665 case FIELD_DECL:
14666 if (! FIELD_FINAL (node)
5b09b33e
PB
14667 || DECL_INITIAL (node) == NULL_TREE)
14668 return NULL_TREE;
14669 val = DECL_INITIAL (node);
14670 /* Guard against infinite recursion. */
14671 DECL_INITIAL (node) = NULL_TREE;
cd9643f7 14672 val = fold_constant_for_init (val, node);
5b09b33e
PB
14673 DECL_INITIAL (node) = val;
14674 return val;
14675
14676 case EXPR_WITH_FILE_LOCATION:
14677 /* Compare java_complete_tree and resolve_expression_name. */
14678 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14679 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14680 {
14681 tree name = EXPR_WFL_NODE (node);
14682 tree decl;
14683 if (PRIMARY_P (node))
14684 return NULL_TREE;
14685 else if (! QUALIFIED_P (name))
14686 {
14687 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
8576f094
APB
14688 if (decl == NULL_TREE
14689 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
5b09b33e
PB
14690 return NULL_TREE;
14691 return fold_constant_for_init (decl, decl);
14692 }
14693 else
14694 {
5b09b33e
PB
14695 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14696 qualify_ambiguous_name (node);
14697 if (resolve_field_access (node, &decl, NULL)
14698 && decl != NULL_TREE)
14699 return fold_constant_for_init (decl, decl);
5b09b33e
PB
14700 return NULL_TREE;
14701 }
14702 }
14703 else
14704 {
14705 op0 = TREE_OPERAND (node, 0);
14706 val = fold_constant_for_init (op0, context);
14707 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14708 return NULL_TREE;
14709 TREE_OPERAND (node, 0) = val;
14710 return val;
14711 }
14712
bc3ca41b
PB
14713#ifdef USE_COMPONENT_REF
14714 case IDENTIFIER:
14715 case COMPONENT_REF:
14716 ?;
14717#endif
14718
5b09b33e
PB
14719 default:
14720 return NULL_TREE;
14721 }
14722}
bc3ca41b
PB
14723
14724#ifdef USE_COMPONENT_REF
14725/* Context is 'T' for TypeName, 'P' for PackageName,
14726 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
14727
14728tree
14729resolve_simple_name (name, context)
14730 tree name;
14731 int context;
14732{
14733}
14734
14735tree
14736resolve_qualified_name (name, context)
14737 tree name;
14738 int context;
14739{
14740}
14741#endif
This page took 2.416869 seconds and 5 git commands to generate.